[PATCH v2 5/8] modpost: fix handling of short reads in read_text_file()
From: Leizhen Zhang
Date: Thu Oct 08 2026 - 12:44:06 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.
Fixes: ac5100f54329 ("modpost: add read_text_file() and get_line() helpers")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Leizhen Zhang <lzsx618@xxxxxxxxx>
---
v2:
- Use my real name in the From and Signed-off-by lines. No code
changes.
v1: https://lore.kernel.org/r/20261005104050.1786222-7-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 75374c64b8..1772068dc6 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