tools: fix handle leak in ifdtool.c
PATCH 1/1 tools: fix handle leak in ifdtool.c
From: Anton Moryakov <[email protected]>
Prevent file descriptor leaks by properly closing 'fd' and 'new_fd' when fstat() or write() operations fail.
- Added close(fd) before return in open_for_read() if fstat() fails.
- Added close(new_fd) before return in write_image() if write() fails.
- No close needed if open() fails (fd == -1 is invalid).
--- tools/ifdtool.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/ifdtool.c +6 -2
@@ -499,8 +499,10 @@ static int write_image(char *filename, char *image, int size) S_IWUSR | S_IRGRP | S_IROTH);if (new_fd <0 )return perror_fname ("Could not open file '%s'" , filename); -if (write (new_fd, image, size) != size) +if (write (new_fd, image, size) != size) { +close (new_fd);return perror_fname ("Could not write file '%s'" , filename); + }close (new_fd);return 0 ;
@@ -604,8 +606,10 @@ int open_for_read(const char *fname, int *sizep)if (fd ==-1 )return perror_fname ("Could not open file '%s'" , fname); -if (fstat (fd, &buf) ==-1 ) +if (fstat (fd, &buf) ==-1 ) { +close (fd);return perror_fname ("Could not stat file '%s'" , fname); + } *sizep = buf.st_size;debug ("File %s is %d bytes\n" , fname, *sizep);
Signature · 1 line
2.30.2
Hi Anton,
Quoted 10 lines · On 5/16/25 5:25 PM, [email protected] wrote:
From: Anton Moryakov <[email protected]>
Prevent file descriptor leaks by properly closing 'fd' and '
new_fd' whenfstat()orwrite()operations fail.- Added close(fd) before return in
open_for_read()iffstat()fails.
- Addedclose(new_fd)before return inwrite_image()ifwrite()fails.
- No close needed ifopen()fails (fd == -1 is invalid).Signed-off-by Anton Moryakov <[email protected]>
Looks fine to me
I believe there are a few others in that file?
inject_region returns without closing, same for write_data, same for the main function with bios_fd at the very least.
Thanks!
Quentin
Maybe. However, the static analyser did not swear at others
Пн, 19 мая 2025 г. в 11:47, Quentin Schulz <[email protected]>:
Quoted 26 lines
Hi Anton,
Quoted 10 lines · On 5/16/25 5:25 PM, [email protected] wrote:
From: Anton Moryakov <[email protected]>
Prevent file descriptor leaks by properly closing 'fd' and '
new_fd' whenfstat()orwrite()operations fail.- Added close(fd) before return in
open_for_read()iffstat()fails.
- Addedclose(new_fd)before return inwrite_image()ifwrite()fails.
- No close needed ifopen()fails (fd == -1 is invalid).Signed-off-by Anton Moryakov <[email protected]>Looks fine to me
Reviewed-by Quentin Schulz <[email protected]>I believe there are a few others in that file?
inject_regionreturns without closing, same forwrite_data, same for the main function withbios_fdat the very least.Thanks!
Quentin
Quoted 8 lines · On Fri, 16 May 2025 18:25:38 +0300, [email protected] wrote:
Prevent file descriptor leaks by properly closing 'fd' and '
new_fd' whenfstat()orwrite()operations fail.- Added close(fd) before return in
open_for_read()iffstat()fails.
- Addedclose(new_fd)before return inwrite_image()ifwrite()fails.
- No close needed ifopen()fails (fd == -1 is invalid).[...]
Applied to u-boot/next, thanks!
[1/1] tools: fix handle leak in ifdtool.c
commit: a4fb99ce4e6ee62f3278d7472b9b9282d7b289ed
Signature · 1 line
Tom