[PATCH v3 3/6] modpost: fix handling of short reads in read_text_file()
From: Leizhen Zhang
Date: Mon Nov 09 2026 - 19:09:01 EST
read_text_file() loops until the whole file has been read, but every
read() call writes to the start of the buffer, so after a short read the
data already read is overwritten and the end of the buffer is left
uninitialized. If read() returns 0 before the expected size has been
read, the loop never terminates.
Read into the correct offset of the buffer, and treat an unexpected end
of file as an error.
This was confirmed by limiting read() to 64 bytes per call with an
LD_PRELOAD shim, which makes modpost fail with a parse error on a valid
Module.symvers.
The analysis and fix were prepared with the help of an AI coding
assistant; I reviewed and tested the result.
Fixes: ac5100f54329 ("modpost: add read_text_file() and get_line() helpers")
Signed-off-by: Leizhen Zhang <lzsx618@xxxxxxxxx>
---
scripts/mod/modpost.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 75374c64b8cc..1772068dc68e 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -143,11 +143,15 @@ char *read_text_file(const char *filename)
while (nbytes) {
ssize_t bytes_read;
- bytes_read = read(fd, buf, nbytes);
+ bytes_read = read(fd, buf + st.st_size - nbytes, nbytes);
if (bytes_read < 0) {
perror(filename);
exit(1);
}
+ if (bytes_read == 0) {
+ fprintf(stderr, "%s: unexpected end of file\n", filename);
+ exit(1);
+ }
nbytes -= bytes_read;
}
--
2.34.1