Skip to contentSkip to conversation

tools: fix handle leak in ifdtool.c

PATCH In review [email protected] 3 participants 4 messages May 16, 2025 – Jun 3, 2025

lore
Share & tools
lore
#0 lore raw

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
index b70570361f4..5db6fa7d195 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw

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' 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).

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

#2 lore raw

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' 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).

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

#3 lore raw
In reply to [email protected]
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' 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).

[...]

Applied to u-boot/next, thanks!

[1/1] tools: fix handle leak in ifdtool.c

      commit: a4fb99ce4e6ee62f3278d7472b9b9282d7b289ed
Signature · 1 line
Tom