boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
PATCH 1/1 boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
From: "Kory Maincent (TI.com)" <[email protected]>
Fix two memory allocation bugs in label_boot_extension():
1. When label->fdtdir is not set, overlay_dir was used without any memory allocation. Add the missing calloc() in the else branch.
2. When label->fdtdir is set, the allocation incorrectly used the 'len' variable instead of 'dir_len'. The 'dir_len' variable is calculated to include the fdtdir length plus the trailing slash, while 'len' was only for the fdtdir length. This caused incorrect memory allocation size.
These issues could lead to memory corruption or undefined behavior when processing device tree overlays via PXE boot.
--- boot/pxe_utils.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
boot/pxe_utils.c +5 -1
@@ -474,7 +474,7 @@ static void label_boot_extension(struct pxe_context *ctx, slash ="" ; dir_len =strlen (label->fdtdir) +strlen (slash) +1 ; - overlay_dir =calloc (1 , len); + overlay_dir =calloc (1 , dir_len);if (!overlay_dir)return ;
@@ -482,6 +482,10 @@ static void label_boot_extension(struct pxe_context *ctx, slash); }else { dir_len =2 ; + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; +snprintf (overlay_dir, dir_len,"/" ); }
Signature · 1 line
2.43.0
Hi Köry,
Quoted 37 lines · Kory Maincent
From: "Kory Maincent (TI.com)" <[email protected]>
Fix two memory allocation bugs in
label_boot_extension():1. When
label->fdtdiris not set,overlay_dirwas used without any memory allocation. Add the missingcalloc()in the else branch.2. When
label->fdtdiris set, the allocation incorrectly used the 'len' variable instead of 'dir_len'. The 'dir_len' variable is calculated to include the fdtdir length plus the trailing slash, while 'len' was only for the fdtdir length. This caused incorrect memory allocation size.These issues could lead to memory corruption or undefined behavior when processing device tree overlays via PXE boot.
Fixes 935109cd9e97 ("boot: pxe_utils: Add extension board devicetree overlay support")Signed-off-by Kory Maincent (TI.com) <[email protected]>--- boot/pxe_utils.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)boot/pxe_utils.c +1
-1@@ -474,7 +474,7 @@ static void label_boot_extension(struct pxe_context *ctx, slash ="" ; dir_len =strlen (label->fdtdir) +strlen (slash) +1 ; - overlay_dir =calloc (1 , len); + overlay_dir =calloc (1 , dir_len);if (!overlay_dir)return ;
boot/pxe_utils.c static void label_boot_extension(struct pxe_context *ctx, · line 491
Quoted 10 lines
boot/pxe_utils.c +4
-0@@ -482,6 +482,10 @@ static void label_boot_extension(struct pxe_context *ctx, slash); }else { dir_len =2 ; + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; +snprintf (overlay_dir, dir_len,"/" ); }
I'm wondering if we couldn't make this easier to maintain by not having two calloc and snprintf calls?
boot/pxe_utils.c +7 -7
@@ -474,17 +474,17 @@ static void label_boot_extension(struct pxe_context *ctx, slash ="" ; dir_len =strlen (label->fdtdir) +strlen (slash) +1 ; - overlay_dir =calloc (1 , len); -if (!overlay_dir) -return ; - -snprintf (overlay_dir, dir_len,"%s%s" , label->fdtdir, - slash); }else { dir_len =2 ; -snprintf (overlay_dir, dir_len,"/" ); + slash ="/" ; } + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; + +snprintf (overlay_dir, dir_len,"%s%s" , label->fdtdir?:"" , slash); +alist_for_each (extension, extension_list) {char *overlay_file;ulong size;
Also, we probably want dir_len = len + strlen(slash) + 1 to avoid a second strlen on label->fdtdir (at the top of the git context here).
Finally, I'm wondering if the snprintf should not be dir_len - 1 considering we calloc with enough room for the trailing NUL character? Or not have + 1 for dir_len and calloc with + 1.
Looks ok to me otherwise!
Cheers,
Quentin
Quoted 63 lines · On Wed, 12 Nov 2025 14:44:33 +0100 Quentin Schulz <[email protected]>…
Hi Köry,
Quoted 37 lines · Kory Maincent
From: "Kory Maincent (TI.com)" <[email protected]>
Fix two memory allocation bugs in
label_boot_extension():1. When
label->fdtdiris not set,overlay_dirwas used without any memory allocation. Add the missingcalloc()in the else branch.2. When
label->fdtdiris set, the allocation incorrectly used the 'len' variable instead of 'dir_len'. The 'dir_len' variable is calculated to include the fdtdir length plus the trailing slash, while 'len' was only for the fdtdir length. This caused incorrect memory allocation size.These issues could lead to memory corruption or undefined behavior when processing device tree overlays via PXE boot.
Fixes 935109cd9e97 ("boot: pxe_utils: Add extension board devicetreeoverlay support") Signed-off-by: Kory Maincent (TI.com) <[email protected]> ---
boot/pxe_utils.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)boot/pxe_utils.c +1
-1@@ -474,7 +474,7 @@ static void label_boot_extension(struct pxe_context *ctx, slash ="" ; dir_len =strlen (label->fdtdir) +strlen (slash) +1 ; - overlay_dir =calloc (1 , len); + overlay_dir =calloc (1 , dir_len);if (!overlay_dir)return ;boot/pxe_utils.c static void label_boot_extension(struct pxe_context · line 491
Quoted 10 lines
boot/pxe_utils.c +4
-0@@ -482,6 +482,10 @@ static void label_boot_extension(struct pxe_context *ctx, slash); }else { dir_len =2 ; + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; +snprintf (overlay_dir, dir_len,"/" ); }I'm wondering if we couldn't make this easier to maintain by not having two calloc and snprintf calls?
boot/pxe_utils.c +0
-0@@ -474,17 +474,17 @@ static void label_boot_extension(structpxe_context *ctx, slash ="" ; dir_len =strlen (label->fdtdir) +strlen (slash) +1 ;
boot/pxe_utils.c static void label_boot_extension(struct · line 480
Quoted 10 lines
boot/pxe_utils.c +1
-7- overlay_dir =calloc (1 , len); -if (!overlay_dir) -return ; - -snprintf (overlay_dir, dir_len,"%s%s" , label->fdtdir, - slash); }else { dir_len =2 ; -snprintf (overlay_dir, dir_len,"/" ); + slash ="/" ;
In fact I think "./" should be the default one here.
boot/pxe_utils.c static void label_boot_extension(struct pxe_context *ctx, · line 486
Quoted 7 lines
boot/pxe_utils.c +5
-0} + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; + +snprintf (overlay_dir, dir_len,"%s%s" , label->fdtdir?:"" , slash);
Yes, that's a good idea!
Quoted 2 lines
Also, we probably want
dir_len= len + strlen(slash) + 1 to avoid a second strlen onlabel->fdtdir(at the top of the git context here).
Indeed!
Quoted 2 lines
Finally, I'm wondering if the snprintf should not be
dir_len- 1 considering we calloc with enough room for the trailing NUL character?
No, snprintf second argument is the max size written including the null character.
Quoted 1 line
Or not have + 1 for
dir_lenand calloc with + 1.
Regards,
Signature · 3 lines
Köry Maincent, Bootlin Embedded Linux and kernel engineering https://bootlin.com
Quoted 65 lines · Kory Maincent
Quoted 62 lines · On Wed, 12 Nov 2025 14:44:33 +0100 Quentin Schulz <[email protected]>…
Hi Köry,
Quoted 37 lines · Kory Maincent
From: "Kory Maincent (TI.com)" <[email protected]>
Fix two memory allocation bugs in
label_boot_extension():1. When
label->fdtdiris not set,overlay_dirwas used without any memory allocation. Add the missingcalloc()in the else branch.2. When
label->fdtdiris set, the allocation incorrectly used the 'len' variable instead of 'dir_len'. The 'dir_len' variable is calculated to include the fdtdir length plus the trailing slash, while 'len' was only for the fdtdir length. This caused incorrect memory allocation size.These issues could lead to memory corruption or undefined behavior when processing device tree overlays via PXE boot.
Fixes 935109cd9e97 ("boot: pxe_utils: Add extension board devicetreeoverlay support") Signed-off-by: Kory Maincent (TI.com) <[email protected]> ---
boot/pxe_utils.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)boot/pxe_utils.c +1
-1@@ -474,7 +474,7 @@ static void label_boot_extension(struct pxe_context *ctx, slash ="" ; dir_len =strlen (label->fdtdir) +strlen (slash) +1 ; - overlay_dir =calloc (1 , len); + overlay_dir =calloc (1 , dir_len);if (!overlay_dir)return ;boot/pxe_utils.c static void label_boot_extension(struct pxe_context · line 491
Quoted 10 lines
boot/pxe_utils.c +4
-0@@ -482,6 +482,10 @@ static void label_boot_extension(struct pxe_context *ctx, slash); }else { dir_len =2 ; + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; +snprintf (overlay_dir, dir_len,"/" ); }I'm wondering if we couldn't make this easier to maintain by not having two calloc and snprintf calls?
boot/pxe_utils.c +0
-0@@ -474,17 +474,17 @@ static void label_boot_extension(structpxe_context *ctx, slash ="" ;
Quoted 14 lines
boot/pxe_utils.c static void label_boot_extension(struct pxe_context *ctx, · line 479
Quoted 11 lines
boot/pxe_utils.c +1
-7dir_len =strlen (label->fdtdir) +strlen (slash) +1 ; - overlay_dir =calloc (1 , len); -if (!overlay_dir) -return ; - -snprintf (overlay_dir, dir_len,"%s%s" , label->fdtdir, - slash); }else { dir_len =2 ; -snprintf (overlay_dir, dir_len,"/" ); + slash ="/" ;In fact I think "./" should be the default one here.
It currently is "/" so that would be a third change/fix (to be explained in the commit log at least; maybe in separate commits).
Quoted 13 lines
boot/pxe_utils.c static void label_boot_extension(struct pxe_context *ctx, · line 486
Quoted 7 lines
boot/pxe_utils.c +5
-0} + overlay_dir =calloc (1 , dir_len); +if (!overlay_dir) +return ; + +snprintf (overlay_dir, dir_len,"%s%s" , label->fdtdir?:"" , slash);Yes, that's a good idea!
Quoted 2 lines
Also, we probably want
dir_len= len + strlen(slash) + 1 to avoid a second strlen onlabel->fdtdir(at the top of the git context here).
Quoted 8 lines
Indeed!
Quoted 2 lines
Finally, I'm wondering if the snprintf should not be
dir_len- 1 considering we calloc with enough room for the trailing NUL character?No, snprintf second argument is the max size written including the null character.
More specifically, it's going to write the NUL character even if not present in the input string(s), at the last index, which is something I had missed while reading the CPP doc of snprintf (the manpage is a bit less clear about that). So never mind about this suggestion :)
Cheers,
Quentin