Skip to contentSkip to conversation

of: fdt: fix possible overflow during parsing of fdt

PATCH Applied Abdelrahman Youssef 4 participants 5 messages Nov 14, 2024 – Nov 25, 2024 2 messages not in the archive Device tree

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 a non-positive value, because name has a minimum length of 1 byte ('\0').

Also in strnlen() we shouldn't check for bytes exceeding maxlen, so we can remove
+ 1 in strnlen(). We also change if (len > maxlen) to >= to count for the null terminator.

---
v3 -> v4:
- replace maxlen < 0 to maxlen <= 0 (Sascha)
- remove + 1 in strnlen() (Sascha)
v2 -> v3
- changed formatting
v1 -> v2
- the overflow was due to integer overflow not out-of-bounds (Ahmad)

---
 drivers/of/fdt.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/of/fdt.c +8 -3
index 2c3ea31394..75af1844f3 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,8 +210,13 @@ 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;
 
-			len = strnlen(name, maxlen + 1);
-			if (len > maxlen) {
+			if (maxlen <= 0) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
+			len = strnlen(name, maxlen);
+			if (len >= maxlen) {
 				ret = -ESPIPE;
 				goto err;
 			}
Signature · 1 line
2.43.0
#1 lore raw

Hi Abdelrahman,
just a remark, nothing wrong with your patch

Quoted 44 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 a non-positive value, because name has a minimum length of 1 byte ('\0').

Also in strnlen() we shouldn't check for bytes exceeding maxlen, so we can remove
+ 1 in strnlen(). We also change if (len > maxlen) to >= to count for the null terminator.

---
v3 -> v4:
- replace maxlen < 0 to maxlen <= 0 (Sascha)
- remove + 1 in strnlen() (Sascha)
v2 -> v3
- changed formatting
v1 -> v2
- the overflow was due to integer overflow not out-of-bounds (Ahmad)
---
drivers/of/fdt.c | 11 ++++++++---

1 file changed, 8 insertions(+), 3 deletions(-)
drivers/of/fdt.c +1 -1
index 2c3ea31394..75af1844f3 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,8 +210,13 @@ 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;

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

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

here len cannot exceed maxlen, but if len == maxlen this means that there is not nul-byte in the name array (up to maxlen). by not passing maxlen + 1 there is a slight behavior change but i don't know if this an issus or not. in the previous case strnlen could read one past maxlen which is suspicious (maybe a bug)

Quoted 3 lines
drivers/of/fdt.c +0 -0
 				ret = -ESPIPE;
 				goto err;
 			}

A message that is not in the archive (sent off-list or not yet received): CABwgSGY3SQGUoc3xY9XVX3aHwRV6s5xFuKABtgCf0vT26-1=_Q@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CABwgSGYcFoMncRWoyOjG19K2GiuMjrEomNGvG3E2fEX3wiM=RA@mail.gmail.com

#4 lore raw

Hi Jules,

I think everything you said is correct, except just the last line because strnlen
returns the minimum of the length of the string and maxlength + 1 (It will not access forbidden location).

So in my opinion it's not a bug or suspicious but only unnecessary.

Cheers,
Abdelrahman

Reviewed-by on v4 1/1
#5 lore raw
In reply to Abdelrahman Youssef
Quoted 7 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.

Quoted 8 lines

So we can just change the type of maxlen to signed and check if it's a non-positive value, because name has a minimum length of 1 byte ('\0').

Also in strnlen() we shouldn't check for bytes exceeding maxlen, so we can remove
+ 1 in strnlen(). We also change if (len > maxlen) to >= to count for the null terminator.

Quoted 42 lines

---
v3 -> v4:
- replace maxlen < 0 to maxlen <= 0 (Sascha)
- remove + 1 in strnlen() (Sascha)
v2 -> v3
- changed formatting
v1 -> v2
- the overflow was due to integer overflow not out-of-bounds (Ahmad)

---
 drivers/of/fdt.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/of/fdt.c +8 -3
index 2c3ea31394..75af1844f3 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,8 +210,13 @@ 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;
 
-			len = strnlen(name, maxlen + 1);
-			if (len > maxlen) {
+			if (maxlen <= 0) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
+			len = strnlen(name, maxlen);
+			if (len >= maxlen) {
 				ret = -ESPIPE;
 				goto err;
 			}
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 |
Applied
#6 lore raw
In reply to Abdelrahman Youssef
Quoted 8 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.

[...]

Applied, thanks!

[1/1] of: fdt: fix possible overflow during parsing of fdt
https://git.pengutronix.de/cgit/barebox/commit/?id=7a3cb7e6fd63 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>