From 480a07bd6828285628abbbe3fe8e5e3b25ce1a92 Mon Sep 17 00:00:00 2001 From: Sam James Date: Tue, 23 Apr 2024 21:10:18 +0100 Subject: fixdep: add fstat error handling 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 Signed-off-by: Sam James Signed-off-by: Denys Vlasenko --- 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 #include #include +#include #include #include #include @@ -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); -- cgit v1.2.3-55-g6feb