[PATCH v3 17/17] tools/bootconfig: fix fd leak in load_xbc_file() on fstat failure

Josh Law posted 17 patches 3 weeks, 2 days ago
There is a newer version of this series
[PATCH v3 17/17] tools/bootconfig: fix fd leak in load_xbc_file() on fstat failure
Posted by Josh Law 3 weeks, 2 days ago
If fstat() fails after open() succeeds, load_xbc_file() returns
-errno without closing the file descriptor.  Add the missing close()
call on the error path.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 tools/bootconfig/main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 55d59ed507d5..8078fee0b75b 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -162,8 +162,10 @@ static int load_xbc_file(const char *path, char **buf)
 	if (fd < 0)
 		return -errno;
 	ret = fstat(fd, &stat);
-	if (ret < 0)
+	if (ret < 0) {
+		close(fd);
 		return -errno;
+	}
 
 	ret = load_xbc_fd(fd, buf, stat.st_size);
 
-- 
2.34.1
Re: [PATCH v3 17/17] tools/bootconfig: fix fd leak in load_xbc_file() on fstat failure
Posted by Markus Elfring 3 weeks ago
> If fstat() fails after open() succeeds, load_xbc_file() returns
> -errno without closing the file descriptor.  Add the missing close()
> call on the error path.

https://elixir.bootlin.com/linux/v7.0-rc3/source/tools/bootconfig/main.c#L139-L153

How do you think about to use a corresponding goto chain?


Would you like to  add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.0-rc4#n145

Regards,
Markus
Re: [PATCH v3 17/17] tools/bootconfig: fix fd leak in load_xbc_file() on fstat failure
Posted by Josh Law 3 weeks ago

On 16 March 2026 12:15:08 GMT, Markus Elfring <Markus.Elfring@web.de> wrote:
>> If fstat() fails after open() succeeds, load_xbc_file() returns
>> -errno without closing the file descriptor.  Add the missing close()
>> call on the error path.
>
>https://elixir.bootlin.com/linux/v7.0-rc3/source/tools/bootconfig/main.c#L139-L153
>
>How do you think about to use a corresponding goto chain?
>
>
>Would you like to  add any tags (like “Fixes” and “Cc”) accordingly?
>https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.0-rc4#n145
>
>Regards,
>Markus


Hello markus, i submitted a V6 (yes a v6) that responds to all that, and includes extra patches to fix some warnings under W123 (maximum GCC warning level)

Thanks.


V/R


Josh Law