pstore/ram: Refactor DT size parsing
authorKees Cook <keescook@chromium.org>
Wed, 6 May 2020 18:44:14 +0000 (11:44 -0700)
committerKees Cook <keescook@chromium.org>
Sat, 30 May 2020 17:34:02 +0000 (10:34 -0700)
Refactor device tree size parsing routines to be able to pass a non-zero
default value for providing a configurable default for the coming
"max_reason" field. Also rename the helpers, since we're not always
parsing a size -- we're parsing a u32 and making sure it's not greater
than INT_MAX.

Link: https://lore.kernel.org/lkml/20200506211523.15077-4-keescook@chromium.org/
Link: https://lore.kernel.org/lkml/20200521205223.175957-1-tyhicks@linux.microsoft.com
Signed-off-by: Kees Cook <keescook@chromium.org>
fs/pstore/ram.c

index 3b399f72e495bffc6f4ed9c8ef2afd578e9c1d04..8cdafb686736f219f9fc9d52bf1a260c71e0ba62 100644 (file)
@@ -644,19 +644,25 @@ static int ramoops_init_prz(const char *name,
        return 0;
 }
 
-static int ramoops_parse_dt_size(struct platform_device *pdev,
-                                const char *propname, u32 *value)
+/* Read a u32 from a dt property and make sure it's safe for an int. */
+static int ramoops_parse_dt_u32(struct platform_device *pdev,
+                               const char *propname,
+                               u32 default_value, u32 *value)
 {
        u32 val32 = 0;
        int ret;
 
        ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
-       if (ret < 0 && ret != -EINVAL) {
+       if (ret == -EINVAL) {
+               /* field is missing, use default value. */
+               val32 = default_value;
+       } else if (ret < 0) {
                dev_err(&pdev->dev, "failed to parse property %s: %d\n",
                        propname, ret);
                return ret;
        }
 
+       /* Sanity check our results. */
        if (val32 > INT_MAX) {
                dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
                return -EOVERFLOW;
@@ -689,21 +695,22 @@ static int ramoops_parse_dt(struct platform_device *pdev,
        pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
        pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops");
 
-#define parse_size(name, field) {                                      \
-               ret = ramoops_parse_dt_size(pdev, name, &value);        \
+#define parse_u32(name, field, default_value) {                                \
+               ret = ramoops_parse_dt_u32(pdev, name, default_value,   \
+                                           &value);                    \
                if (ret < 0)                                            \
                        return ret;                                     \
                field = value;                                          \
        }
 
-       parse_size("record-size", pdata->record_size);
-       parse_size("console-size", pdata->console_size);
-       parse_size("ftrace-size", pdata->ftrace_size);
-       parse_size("pmsg-size", pdata->pmsg_size);
-       parse_size("ecc-size", pdata->ecc_info.ecc_size);
-       parse_size("flags", pdata->flags);
+       parse_u32("record-size", pdata->record_size, 0);
+       parse_u32("console-size", pdata->console_size, 0);
+       parse_u32("ftrace-size", pdata->ftrace_size, 0);
+       parse_u32("pmsg-size", pdata->pmsg_size, 0);
+       parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0);
+       parse_u32("flags", pdata->flags, 0);
 
-#undef parse_size
+#undef parse_u32
 
        /*
         * Some old Chromebooks relied on the kernel setting the