* [PATCH 2/2] of: fdt: fix possible overflow during parsing of fdt
@ 2024-11-12 16:20 Abdelrahman Youssef
2024-11-12 19:03 ` Ahmad Fatoum
0 siblings, 1 reply; 2+ messages in thread
From: Abdelrahman Youssef @ 2024-11-12 16:20 UTC (permalink / raw)
To: s.hauer; +Cc: barebox, 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.
Signed-off-by: Abdelrahman Youssef <abdelrahmanyossef12@gmail.com>
---
drivers/of/fdt.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 2c3ea31394..13d0b8be54 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -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,12 @@ 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;
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH 2/2] of: fdt: fix possible overflow during parsing of fdt
2024-11-12 16:20 [PATCH 2/2] of: fdt: fix possible overflow during parsing of fdt Abdelrahman Youssef
@ 2024-11-12 19:03 ` Ahmad Fatoum
0 siblings, 0 replies; 2+ messages in thread
From: Ahmad Fatoum @ 2024-11-12 19:03 UTC (permalink / raw)
To: Abdelrahman Youssef, s.hauer; +Cc: barebox
Hello Abdelrahman,
Thanks for your patch.
On 12.11.24 17:20, Abdelrahman Youssef wrote:
> 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.
>
> Signed-off-by: Abdelrahman Youssef <abdelrahmanyossef12@gmail.com>
> ---
Changelog would be nice for future patches.
Also use -v2 instead of -2 as argument to git format-patch / git send-email
(or let b4 handle revision increment).
> drivers/of/fdt.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
> index 2c3ea31394..13d0b8be54 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -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,12 @@ 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)
> + {
Kernel coding style is space betewen if and (. Also move brace to same line as
the if statement. See the other if conditions in the file.
Cheers,
Ahmad
> + ret = -ESPIPE;
> + goto err;
> + }
> +
> len = strnlen(name, maxlen + 1);
> 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 |
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-11-12 19:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-12 16:20 [PATCH 2/2] of: fdt: fix possible overflow during parsing of fdt Abdelrahman Youssef
2024-11-12 19:03 ` Ahmad Fatoum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox