[git commit] fixdep: add fstat error handling

Denys Vlasenko vda.linux at googlemail.com
Fri Sep 27 18:03:30 UTC 2024


commit: https://git.busybox.net/busybox/commit/?id=480a07bd6828285628abbbe3fe8e5e3b25ce1a92
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

When `fstat` fails, `st` is left uninitialised. In our case, Ben Kohler
noticed our release media builds were failing in Gentoo on x86 when building
busybox with occasional SIGBUS. This turned out to be EOVERFLOW (from 32-bit
ino_t) which wasn't being reported because nothing was checking the return value
from `fstat`.

Fix that to avoid UB (use of uninit var) and to give a more friendly
error to the user.

This actually turns out to be fixed already in the kernel from back in
2010 [0] and 2016 [1].

[0] https://github.com/torvalds/linux/commit/a3ba81131aca243bfecfa78c42edec0cd69f72d6
[1] https://github.com/torvalds/linux/commit/46fe94ad18aa7ce6b3dad8c035fb538942020f2b

Reported-by: Ben Kohler <bkohler at gentoo.org>
Signed-off-by: Sam James <sam at gentoo.org>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 scripts/basic/fixdep.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 66be73aad..071c3b407 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -105,6 +105,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <string.h>
@@ -292,7 +293,10 @@ void do_config_file(char *filename)
 		perror(filename);
 		exit(2);
 	}
-	fstat(fd, &st);
+	if (fstat(fd, &st) < 0) {
+		fprintf(stderr, "fixdep: fstat %s %s\n", filename, strerror(errno));
+		exit(2);
+	}
 	if (st.st_size == 0) {
 		close(fd);
 		return;
@@ -368,7 +372,10 @@ void print_deps(void)
 		perror(depfile);
 		exit(2);
 	}
-	fstat(fd, &st);
+	if (fstat(fd, &st) < 0) {
+		fprintf(stderr, "fixdep: fstat %s %s\n", depfile, strerror(errno));
+		exit(2);
+	}
 	if (st.st_size == 0) {
 		fprintf(stderr,"fixdep: %s is empty\n",depfile);
 		close(fd);


More information about the busybox-cvs mailing list