[PATCH 6/9] modpost: fix handling of short reads in read_text_file()

From: lzhan011

Date: Mon Oct 05 2026 - 06:47:44 EST


From: lzhan011 <zhangleizhen645@xxxxxxxxx>

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: lzhan011 <zhangleizhen645@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 75374c64b..1772068dc 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