Skip to content

Commit b74f2f7

Browse files
neosys007robherring
authored andcommitted
drivers/of: fdt: validate flat DT string properties before string use
Firmware-supplied flat DT properties are raw byte sequences. Several early FDT helpers fetch properties such as status, model, compatible, and device_type and then use them as C strings with strcmp(), strlen(), or pr_info() without first proving that the property is NUL-terminated within its declared length. Use fdt_stringlist_get() for these string properties instead. That preserves the existing behavior for valid DTBs while rejecting malformed unterminated properties before they are passed to C string helpers. Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn> Link: https://patch.msgid.link/20260403164501.1-drivers-of-fdt-v2-pengpeng@iscas.ac.cn Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
1 parent bb04fcc commit b74f2f7

1 file changed

Lines changed: 14 additions & 24 deletions

File tree

drivers/of/fdt.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void __init of_fdt_limit_memory(int limit)
6868

6969
bool of_fdt_device_is_available(const void *blob, unsigned long node)
7070
{
71-
const char *status = fdt_getprop(blob, node, "status", NULL);
71+
const char *status = fdt_stringlist_get(blob, node, "status", 0, NULL);
7272

7373
if (!status)
7474
return true;
@@ -677,22 +677,15 @@ void __init of_flat_dt_read_addr_size(const __be32 *prop, int entry_index,
677677
* specific compatible values.
678678
*/
679679
static int of_fdt_is_compatible(const void *blob,
680-
unsigned long node, const char *compat)
680+
unsigned long node, const char *compat)
681681
{
682682
const char *cp;
683-
int cplen;
684-
unsigned long l, score = 0;
683+
int idx = 0, score = 0;
685684

686-
cp = fdt_getprop(blob, node, "compatible", &cplen);
687-
if (cp == NULL)
688-
return 0;
689-
while (cplen > 0) {
685+
while ((cp = fdt_stringlist_get(blob, node, "compatible", idx++, NULL))) {
690686
score++;
691687
if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
692688
return score;
693-
l = strlen(cp) + 1;
694-
cp += l;
695-
cplen -= l;
696689
}
697690

698691
return 0;
@@ -741,9 +734,10 @@ const char * __init of_flat_dt_get_machine_name(void)
741734
const char *name;
742735
unsigned long dt_root = of_get_flat_dt_root();
743736

744-
name = of_get_flat_dt_prop(dt_root, "model", NULL);
737+
name = fdt_stringlist_get(initial_boot_params, dt_root, "model", 0, NULL);
745738
if (!name)
746-
name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
739+
name = fdt_stringlist_get(initial_boot_params, dt_root,
740+
"compatible", 0, NULL);
747741
return name;
748742
}
749743

@@ -775,19 +769,14 @@ const void * __init of_flat_dt_match_machine(const void *default_match,
775769
}
776770
if (!best_data) {
777771
const char *prop;
778-
int size;
772+
int idx = 0, size;
779773

780774
pr_err("\n unrecognized device tree list:\n[ ");
781775

782-
prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
783-
if (prop) {
784-
while (size > 0) {
785-
printk("'%s' ", prop);
786-
size -= strlen(prop) + 1;
787-
prop += strlen(prop) + 1;
788-
}
789-
}
790-
printk("]\n\n");
776+
while ((prop = fdt_stringlist_get(initial_boot_params, dt_root,
777+
"compatible", idx++, &size)))
778+
pr_err("'%s' ", prop);
779+
pr_err("]\n\n");
791780
return NULL;
792781
}
793782

@@ -1032,7 +1021,8 @@ int __init early_init_dt_scan_memory(void)
10321021
const void *fdt = initial_boot_params;
10331022

10341023
fdt_for_each_subnode(node, fdt, 0) {
1035-
const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1024+
const char *type = fdt_stringlist_get(fdt, node,
1025+
"device_type", 0, NULL);
10361026
const __be32 *reg;
10371027
int i, l;
10381028
bool hotpluggable;

0 commit comments

Comments
 (0)