Skip to content

Commit 26961d7

Browse files
committed
pstore/ram: Refactor DT size parsing
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/[email protected]/ Link: https://lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent f858b57 commit 26961d7

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

fs/pstore/ram.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -644,19 +644,25 @@ static int ramoops_init_prz(const char *name,
644644
return 0;
645645
}
646646

647-
static int ramoops_parse_dt_size(struct platform_device *pdev,
648-
const char *propname, u32 *value)
647+
/* Read a u32 from a dt property and make sure it's safe for an int. */
648+
static int ramoops_parse_dt_u32(struct platform_device *pdev,
649+
const char *propname,
650+
u32 default_value, u32 *value)
649651
{
650652
u32 val32 = 0;
651653
int ret;
652654

653655
ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
654-
if (ret < 0 && ret != -EINVAL) {
656+
if (ret == -EINVAL) {
657+
/* field is missing, use default value. */
658+
val32 = default_value;
659+
} else if (ret < 0) {
655660
dev_err(&pdev->dev, "failed to parse property %s: %d\n",
656661
propname, ret);
657662
return ret;
658663
}
659664

665+
/* Sanity check our results. */
660666
if (val32 > INT_MAX) {
661667
dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
662668
return -EOVERFLOW;
@@ -689,21 +695,22 @@ static int ramoops_parse_dt(struct platform_device *pdev,
689695
pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
690696
pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops");
691697

692-
#define parse_size(name, field) { \
693-
ret = ramoops_parse_dt_size(pdev, name, &value); \
698+
#define parse_u32(name, field, default_value) { \
699+
ret = ramoops_parse_dt_u32(pdev, name, default_value, \
700+
&value); \
694701
if (ret < 0) \
695702
return ret; \
696703
field = value; \
697704
}
698705

699-
parse_size("record-size", pdata->record_size);
700-
parse_size("console-size", pdata->console_size);
701-
parse_size("ftrace-size", pdata->ftrace_size);
702-
parse_size("pmsg-size", pdata->pmsg_size);
703-
parse_size("ecc-size", pdata->ecc_info.ecc_size);
704-
parse_size("flags", pdata->flags);
706+
parse_u32("record-size", pdata->record_size, 0);
707+
parse_u32("console-size", pdata->console_size, 0);
708+
parse_u32("ftrace-size", pdata->ftrace_size, 0);
709+
parse_u32("pmsg-size", pdata->pmsg_size, 0);
710+
parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0);
711+
parse_u32("flags", pdata->flags, 0);
705712

706-
#undef parse_size
713+
#undef parse_u32
707714

708715
/*
709716
* Some old Chromebooks relied on the kernel setting the

0 commit comments

Comments
 (0)