Skip to contentSkip to conversation

of: fdt: fix possible overflow during parsing of fdt

PATCH Superseded Abdelrahman Youssef 3 participants 6 messages Nov 12, 2024 – Nov 13, 2024 Device tree

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 of: fdt: fix possible overflow during parsing of fdt

While fuzzing, the name marked by FDT_BEGIN_NODE sometimes extends beyond the struct block area, Causing a heap-overflow.

Since maxlen is an unsigned integer representing the length of name, It can be negative, So it overflows to large numbers, Causing strnlen() to overflow.

So we can just change the type of maxlen to signed and check if it's negative.

---
 drivers/of/fdt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/of/fdt.c +6 -1
index 2c3ea31394..d8d8a4922c 100644
@@ -176,7 +176,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 	void *dt_strings;
 	struct fdt_header f;
 	int ret;
-	unsigned int maxlen;
+	int maxlen;
 	const struct fdt_header *fdt = infdt;
 
 	ret = fdt_parse_header(infdt, size, &f);
@@ -210,6 +210,11 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			maxlen = (unsigned long)fdt + f.off_dt_struct +
 				f.size_dt_struct - (unsigned long)name;
 
+			if (maxlen < 0) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
 			len = strnlen(name, maxlen + 1);
 			if (len > maxlen) {
 				ret = -ESPIPE;
Signature · 1 line
2.43.0
#1 lore raw

Hello Abdelrahman,

Thanks for your patch.

Quoted 11 lines · Abdelrahman Youssef

While fuzzing, the name marked by FDT_BEGIN_NODE sometimes extends beyond the struct block area, Causing a heap-overflow.

Since maxlen is an unsigned integer representing the length of name, It can be negative, So it overflows to large numbers, Causing strnlen() to overflow.

So we can just change the type of maxlen to signed and check if it's negative.

---

Changelog would've been nice. This also should have been v3 not v2.

Quoted 17 lines
 drivers/of/fdt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/of/fdt.c +1 -1
index 2c3ea31394..d8d8a4922c 100644
@@ -176,7 +176,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 	void *dt_strings;
 	struct fdt_header f;
 	int ret;
-	unsigned int maxlen;
+	int maxlen;
 	const struct fdt_header *fdt = infdt;
 
 	ret = fdt_parse_header(infdt, size, &f);

drivers/of/fdt.c static struct device_node *__of_unflatten_dtb(const void *infdt, int size, · line 218

Quoted 9 lines
drivers/of/fdt.c +5 -0
@@ -210,6 +210,11 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			maxlen = (unsigned long)fdt + f.off_dt_struct +
 				f.size_dt_struct - (unsigned long)name;
 
+			if (maxlen < 0) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
 			len = strnlen(name, maxlen + 1);

Hmm is this + 1 correct? I am wondering if we should be dropping the + 1 here and make it maxlen <= 0 above.

What do you think?

Cheers,
Ahmad

Quoted 1 line
drivers/of/fdt.c +0 -0
 			if (len > maxlen) {>  				ret = -ESPIPE;
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw

Hi,

Quoted 1 line

Changelog would've been nice. This also should have been v3 not v2

So should I write a new patch (v4) and add Changelog?

Quoted 4 lines

Hmm is this + 1 correct? I am wondering if we should be dropping the + 1 here and make it maxlen <= 0 above.

What do you think?

Well, I think the + 1 is unnecessary here. But it's been there for over 11 years, So maybe someone
has another opinion on the matter.

Cheers,
Abdelrahman

Quoted 63 lines · Ahmad Fatoum

Hello Abdelrahman,

Thanks for your patch.

Quoted 11 lines · Abdelrahman Youssef

While fuzzing, the name marked by FDT_BEGIN_NODE sometimes extends beyond the struct block area, Causing a heap-overflow.

Since maxlen is an unsigned integer representing the length of name, It can be negative, So it overflows to large numbers, Causing strnlen() to overflow.

So we can just change the type of maxlen to signed and check if it's negative.

---

Changelog would've been nice. This also should have been v3 not v2.

Quoted 17 lines
 drivers/of/fdt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/of/fdt.c +1 -1
index 2c3ea31394..d8d8a4922c 100644
@@ -176,7 +176,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
      void *dt_strings;
      struct fdt_header f;
      int ret;
-     unsigned int maxlen;
+     int maxlen;
      const struct fdt_header *fdt = infdt;
 
      ret = fdt_parse_header(infdt, size, &f);

drivers/of/fdt.c static struct device_node *__of_unflatten_dtb(const void *infdt, int size, · line 218

Quoted 9 lines
drivers/of/fdt.c +5 -0
@@ -210,6 +210,11 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
                      maxlen = (unsigned long)fdt + f.off_dt_struct +
                              f.size_dt_struct - (unsigned long)name;
 
+                     if (maxlen < 0) {
+                             ret = -ESPIPE;
+                             goto err;
+                     }
+
                      len = strnlen(name, maxlen + 1);

Hmm is this + 1 correct? I am wondering if we should be dropping the + 1 here and make it maxlen <= 0 above.

What do you think?

Cheers,
Ahmad

drivers/of/fdt.c static struct device_node *__of_unflatten_dtb(const void *infdt, int size, · line 219

Quoted 1 line
drivers/of/fdt.c +0 -0
                      if (len > maxlen) {>                            ret = -ESPIPE;

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#3 lore raw
In reply to Ahmad Fatoum
Quoted 39 lines · Ahmad Fatoum

Hello Abdelrahman,

Thanks for your patch.

Quoted 11 lines · Abdelrahman Youssef

While fuzzing, the name marked by FDT_BEGIN_NODE sometimes extends beyond the struct block area, Causing a heap-overflow.

Since maxlen is an unsigned integer representing the length of name, It can be negative, So it overflows to large numbers, Causing strnlen() to overflow.

So we can just change the type of maxlen to signed and check if it's negative.

---

Changelog would've been nice. This also should have been v3 not v2.

Quoted 20 lines
 drivers/of/fdt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/of/fdt.c +1 -1
index 2c3ea31394..d8d8a4922c 100644
@@ -176,7 +176,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 	void *dt_strings;
 	struct fdt_header f;
 	int ret;
-	unsigned int maxlen;
+	int maxlen;
 	const struct fdt_header *fdt = infdt;
 
 	ret = fdt_parse_header(infdt, size, &f);
@@ -210,6 +210,11 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			maxlen = (unsigned long)fdt + f.off_dt_struct +
 				f.size_dt_struct - (unsigned long)name;
 
Quoted 9 lines

drivers/of/fdt.c static struct device_node *__of_unflatten_dtb(const void *infdt, int size, · line 218

Quoted 6 lines
drivers/of/fdt.c +5 -0
+			if (maxlen < 0) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
 			len = strnlen(name, maxlen + 1);

Hmm is this + 1 correct? I am wondering if we should be dropping the + 1 here and make it maxlen <= 0 above.

I think maxlen <= 0 is correct indepent of what follows next, because maxlen is the length of a string and a valid string has a minimal length of one byte ('\0').

Next we shouldn't look at bytes exceeding maxlen, so indeed strnlen(name, maxlen) should be correct. When changing this we have to adjust the following if (len > maxlen) check to >=.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

I will update the patch and send it as v4.

Is it enough to just add the changes to Changelog or change the commit message?

Quoted 66 lines · Sascha Hauer
Quoted 39 lines · Ahmad Fatoum

Hello Abdelrahman,

Thanks for your patch.

Quoted 11 lines · Abdelrahman Youssef

While fuzzing, the name marked by FDT_BEGIN_NODE sometimes extends beyond the struct block area, Causing a heap-overflow.

Since maxlen is an unsigned integer representing the length of name, It can be negative, So it overflows to large numbers, Causing strnlen() to overflow.

So we can just change the type of maxlen to signed and check if it's negative.

---

Changelog would've been nice. This also should have been v3 not v2.

Quoted 20 lines
 drivers/of/fdt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/of/fdt.c +1 -1
index 2c3ea31394..d8d8a4922c 100644
@@ -176,7 +176,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
    void *dt_strings;
    struct fdt_header f;
    int ret;
-   unsigned int maxlen;
+   int maxlen;
    const struct fdt_header *fdt = infdt;
 
    ret = fdt_parse_header(infdt, size, &f);
@@ -210,6 +210,11 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
                    maxlen = (unsigned long)fdt + f.off_dt_struct +
                            f.size_dt_struct - (unsigned long)name;
 
Quoted 9 lines

drivers/of/fdt.c static struct device_node *__of_unflatten_dtb(const void *infdt, int size, · line 218

Quoted 6 lines
drivers/of/fdt.c +5 -0
+                   if (maxlen < 0) {
+                           ret = -ESPIPE;
+                           goto err;
+                   }
+
                    len = strnlen(name, maxlen + 1);

Hmm is this + 1 correct? I am wondering if we should be dropping the + 1 here and make it maxlen <= 0 above.

I think maxlen <= 0 is correct indepent of what follows next, because maxlen is the length of a string and a valid string has a minimal length of one byte ('\0').

Next we shouldn't look at bytes exceeding maxlen, so indeed strnlen(name, maxlen) should be correct. When changing this we have to adjust the following if (len > maxlen) check to >=.

Sascha

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#5 lore raw

Hi,

Quoted 3 lines · AbdelRahman Yossef

I will update the patch and send it as v4.

Is it enough to just add the changes to Changelog or change the commit message?

The old commit message wouldn't reflect the new changes, so please rewrite it to be in-line with the diff.

The changelog is separate and should look like this or similar:

---
v3 -> v4:
- replace < 0 with <= 0 (Sascha)
- remove + 1 in strnlen (Sascha)
v2 -> v3:
- did foo to bar ($name_of_person_who_suggested_it)
v1 > v2:
- did baz to bazzer
---

Thanks,
Ahmad

Quoted 69 lines · Sascha Hauer
Quoted 66 lines · Ahmad Fatoum
Quoted 39 lines

Hello Abdelrahman,

Thanks for your patch.

Quoted 11 lines · Abdelrahman Youssef

While fuzzing, the name marked by FDT_BEGIN_NODE sometimes extends beyond the struct block area, Causing a heap-overflow.

Since maxlen is an unsigned integer representing the length of name, It can be negative, So it overflows to large numbers, Causing strnlen() to overflow.

So we can just change the type of maxlen to signed and check if it's negative.

---

Changelog would've been nice. This also should have been v3 not v2.

Quoted 20 lines
 drivers/of/fdt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/of/fdt.c +1 -1
index 2c3ea31394..d8d8a4922c 100644
@@ -176,7 +176,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
    void *dt_strings;
    struct fdt_header f;
    int ret;
-   unsigned int maxlen;
+   int maxlen;
    const struct fdt_header *fdt = infdt;
 
    ret = fdt_parse_header(infdt, size, &f);
@@ -210,6 +210,11 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
                    maxlen = (unsigned long)fdt + f.off_dt_struct +
                            f.size_dt_struct - (unsigned long)name;
 
Quoted 9 lines

drivers/of/fdt.c static struct device_node *__of_unflatten_dtb(const void *infdt, int size, · line 218

Quoted 6 lines
drivers/of/fdt.c +5 -0
+                   if (maxlen < 0) {
+                           ret = -ESPIPE;
+                           goto err;
+                   }
+
                    len = strnlen(name, maxlen + 1);

Hmm is this + 1 correct? I am wondering if we should be dropping the + 1 here and make it maxlen <= 0 above.

I think maxlen <= 0 is correct indepent of what follows next, because maxlen is the length of a string and a valid string has a minimal length of one byte ('\0').

Next we shouldn't look at bytes exceeding maxlen, so indeed strnlen(name, maxlen) should be correct. When changing this we have to adjust the following if (len > maxlen) check to >=.

Sascha

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |