tools: fit_common: fix memory leak in mmap_fdt error path
PATCH 1/1 tools: fit_common: fix memory leak in mmap_fdt error path
Static analysis reported that memory mapped by mmap() is not unmapped in the error path of mmap_fdt().
If mmap() succeeds but a subsequent operation fails (fstat, ftruncate, fdt_check_header, or fdt_open_into), the function jumps to the err:
label which closes the file descriptor but does not call munmap() on the mapped region, causing a memory leak.
Fix: add munmap(ptr, sbuf->st_size) in the error path, guarded by ptr != MAP_FAILED to avoid unmapping an invalid pointer.
--- tools/fit_common.c | 2 ++ 1 file changed, 2 insertions(+)
tools/fit_common.c +2 -0
@@ -123,6 +123,8 @@ err:close (fd);if (delete_on_error)unlink (fname); +if (ptr !=MAP_FAILED ) +munmap (ptr, sbuf->st_size);return -1 ; }
Signature · 1 line
2.39.2
PATCH 1/1 tools: ifwitool: fix memory leak in ibbp_dir_add()
--- tools/ifwitool.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/ifwitool.c +8 -1
@@ -1741,8 +1741,10 @@ static enum ifwi_ret ibbp_dir_add(int type)init_signed_pkg_info_ext (ext,0 , subparts[type].name);/* Entry # 2 - IBBL */ -if (buffer_from_file (&ibbl, param.file_name)) +if (buffer_from_file (&ibbl, param.file_name)) { +buffer_delete (&manifest);return COMMAND_ERR ; + }/* Entry # 3 - IBB */ alloc_buffer (&ibb,DUMMY_IBB_SIZE ,"IBB" );
@@ -1755,6 +1757,11 @@ static enum ifwi_ret ibbp_dir_add(int type)create_subpart (&ifwi_image.subpart_buf[type], &info[0 ],ARRAY_SIZE (info), subparts[type].name); +/* Clean up temporary buffers */ +buffer_delete (&manifest); +buffer_delete (&ibbl); +buffer_delete (&ibb); +return REPACK_REQUIRED ; }
Signature · 1 line
2.39.2
Re: [PATCH] tools: ifwitool: fix memory leak in ibbp_dir_add()
Hi Anton,
Quoted 6 lines · Anton Moryakov
tools: ifwitool: fix memory leak in
ibbp_dir_add()Signed-off-by Anton Moryakov <[email protected]>
tools/ifwitool.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 4 lines
tools: ifwitool: fix memory leak in
ibbp_dir_add()Signed-off-by Anton Moryakov <[email protected]>
Please can you add a commit body? Describe which buffers leak on which path, and how you checked (static analysis, valgrind, etc). Something like:
On the error path from buffer_from_file() the manifest buffer
allocated above is never released. On the success path the manifest, ibbl and ibb buffers are copied into the destination subpart buffer by create_subpart() but never freed.
Free them explicitly to plug the leak.
Regards,
Simon
PATCH 1/1 tools: image-host: fix memory leak of info.name in signature functions
In reply to Anton MoryakovStatic analysis reported that info.name (allocated via strdup() in fit_image_setup_sig()) is not freed in two functions:
1. fit_image_process_sig(): info.name leaked on error path and success path
2. fit_config_process_sig(): info.name leaked on error path and success path
Fix: add free(info.name) in both error paths (after fit_image_setup_sig() failure) and at the end of successful execution paths.
--- tools/image-host.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/image-host.c +4 -0
@@ -245,6 +245,7 @@ static int fit_image_process_sig(const char *keydir, const char *keyfile,if (fit_image_setup_sig (&info, keydir, keyfile, fit, image_name, noffset, require_keys ?"image" :NULL , engine_id, algo_name)) +free (info.name);return -1 ; node_name =fit_get_name (fit, noffset,NULL );
@@ -272,6 +273,7 @@ static int fit_image_process_sig(const char *keydir, const char *keyfile,return -1 ; }free (value); +free (info.name);/* Get keyname again, as FDT has changed and invalidated our pointer */ info.keyname =fdt_getprop (fit, noffset,FIT_KEY_HINT ,NULL );
@@ -1094,6 +1096,7 @@ static int fit_config_process_sig(const char *keydir, const char *keyfile,if (fit_image_setup_sig (&info, keydir, keyfile, fit, conf_name, noffset, require_keys ?"conf" :NULL , engine_id, algo_name)) +free (info.name);return -1 ; ret = info.crypto->sign(&info, region, region_count, &value,
@@ -1122,6 +1125,7 @@ static int fit_config_process_sig(const char *keydir, const char *keyfile, }free (value);free (region_prop); +free (info.name);/* Get keyname again, as FDT has changed and invalidated our pointer */ info.keyname =fdt_getprop (fit, noffset,FIT_KEY_HINT ,NULL );
Signature · 1 line
2.39.2
Re: [PATCH] tools: image-host: fix memory leak of info.name in signature functions
Hi Anton,
Quoted 18 lines · Anton Moryakov
tools: image-host: fix memory leak of info.name in signature functions
Static analysis reported that info.name (allocated via
strdup()infit_image_setup_sig()) is not freed in two functions:1.
fit_image_process_sig(): info.name leaked on error path and success path
2.fit_config_process_sig(): info.name leaked on error path and success pathFix: add free(info.name) in both error paths (after
fit_image_setup_sig()failure) and at the end of successful execution paths.Signed-off-by Anton Moryakov <[email protected]>
tools/image-host.c| 4 ++++1 file changed, 4 insertions(+)
tools/image-host.c static int fit_image_process_sig(const char *keydir, const char *keyfile, · line 249
Quoted 5 lines
tools/image-host.c +1
-0@@ -245,6 +245,7 @@ static int fit_image_process_sig(const char *keydir, const char *keyfile,if (fit_image_setup_sig (&info, keydir, keyfile, fit, image_name, noffset, require_keys ?'image' :NULL , engine_id, algo_name)) +free (info.name);return -1 ;
The 'if' has no braces, so return -1 is now unconditional - the function always returns -1, even on success. Same in fit_config_process_sig()
Even with braces, fit_image_setup_sig() can return -1 at line 185 (the fit_image_hash_get_algo() failure) before the memset(.) at line 191, so info.name is an uninitialised stack value and free(info.name) is undefined behaviour.
tools/image-host.c static int fit_image_process_sig(const char *keydir, const char *keyfile, · line 279
Quoted 9 lines
tools/image-host.c +1
-0@@ -272,6 +273,7 @@ static int fit_image_process_sig(const char *keydir, const char *keyfile,return -1 ; }free (value); +free (info.name);/* Get keyname again, as FDT has changed and invalidated our pointer */ info.keyname =fdt_getprop (fit, noffset,FIT_KEY_HINT ,NULL );
Freeing info.name mid-function is fragile - info is still used below by info.crypto->add_verify_data(). Several other exit paths still
leak: the ret == -ENOENT return 0, the return -1 after sign failure, the -ENOSPC return, and the add_verify_data() error return. Please use a single cleanup label with goto, freeing info.name once at the end, rather than sprinkling free() at only some return points. Same for fit_config_process_sig().
BTW please run the FIT signing tests (test/py and binman tests that exercise fit_image_process_sig) before resending - they would have caught the missing-braces bug.
Regards,
Simon
PATCH 1/1 x86: zimage: fix NULL pointer dereference in build_command_line
In reply to Anton MoryakovStatic analysis reported a redundant NULL check at line 68, but the real issue is that env_command_line (from env_get("bootargs")) can be NULL and is dereferenced unconditionally by strstr() at line 56.
If the "bootargs" environment variable is not set, env_get() returns NULL, causing strstr(NULL, ...) to dereference a null pointer and crash.
Fix: add NULL check before strstr() call:
if (env_command_line && !strstr(env_command_line, "console="))
The existing check at line 68 remains necessary to guard the strcat() call and is no longer redundant.
--- arch/x86/lib/zimage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/lib/zimage.c +1 -1
@@ -64,7 +64,7 @@ static void build_command_line(char *command_line, int auto_boot) env_command_line =env_get ("bootargs" );/* set console= argument if we use a serial console */ -if (!strstr (env_command_line,"console=" )) { +if (env_command_line && !strstr (env_command_line,"console=" )) {if (!strcmp (env_get ("stdout" ),"serial" )) {/* We seem to use serial console */
Signature · 1 line
2.39.2
Re: [PATCH] x86: zimage: fix NULL pointer dereference in build_command_line
Hi Anton,
Quoted 22 lines · Anton Moryakov
x86: zimage: fix NULL pointer dereference in
build_command_lineStatic analysis reported a redundant NULL check at line 68, but the real issue is that
env_command_line(fromenv_get('bootargs')) can be NULL and is dereferenced unconditionally bystrstr()at line 56.If the 'bootargs' environment variable is not set,
env_get()returns NULL, causingstrstr(NULL, ...)to dereference a null pointer and crash.Fix: add NULL check before
strstr()call:
if (env_command_line&& !strstr(env_command_line, 'console='))The existing check at line 68 remains necessary to guard the
strcat()call and is no longer redundant.Signed-off-by Anton Moryakov <[email protected]>I
arch/x86/lib/zimage.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/lib/zimage.c static void build_command_line(char *command_line, int auto_boot) · line 67
Quoted 5 lines
arch/x86/lib/zimage.c +1
-1@@ -64,7 +64,7 @@ static void build_command_line(char *command_line, int auto_boot) env_command_line =env_get ('bootargs' );/* set console= argument if we use a serial console */ -if (!strstr (env_command_line,'console=' )) { +if (env_command_line && !strstr (env_command_line,'console=' )) {
Please drop the line numbers from the commit message - they shift and don't seem to match the tree today. Just describe the bug:env_get('bootargs') can return NULL and strstr() dereferences it unconditionally.
Subject should use parentheses: build_command_line()
Regards,
Simon
Hi Anton,
Quoted 20 lines · Anton Moryakov
tools:
fit_common: fix memory leak inmmap_fdterror pathStatic analysis reported that memory mapped by
mmap()is not unmapped in the error path ofmmap_fdt().If
mmap()succeeds but a subsequent operation fails (fstat, ftruncate,fdt_check_header, orfdt_open_into), the function jumps to the err:
label which closes the file descriptor but does not callmunmap()on the mapped region, causing a memory leak.Fix: add
munmap(ptr, sbuf->st_size)in the error path, guarded by ptr !=MAP_FAILEDto avoid unmapping an invalid pointer.Signed-off-by Anton Moryakov <[email protected]>
tools/fit_common.c| 2 ++1 file changed, 2 insertions(+)
tools/fit_common.c err: · line 130
Quoted 8 lines
tools/fit_common.c +2
-0@@ -123,6 +123,8 @@ err:close (fd);if (delete_on_error)unlink (fname); +if (ptr !=MAP_FAILED ) +munmap (ptr, sbuf->st_size);return -1 ; }
Not quite: ptr is declared uninitialised at the top of mmap_fdt() and is only assigned by the mmap() call on line 90. The earlier error paths (open(), fstat(), ftruncate()) all goto err before ptr has been written, so this new check reads an indeterminate value - undefined behaviour, and may call munmap() on garbage.
Please initialise it at declaration:
void *ptr = MAP_FAILED;
sbuf->st_size is only valid after fstat() has succeeded, but MAP_FAILED as the sentinel covers that, since ptr only becomes non-MAP_FAILED after fstat() and (optionally) ftruncate() have populated sbuf->st_size.
BTW, since mmap_fdt() is only used by short-lived host tools (mkimage, fit_info, fit_check_sign, etc) that exit shortly after, the kernel reclaims the mapping anyway - this is a static-analysis cleanup rather than a real leak. Worth mentioning in the commit message.
Regards,
Simon