libutil/util_file: Handle over-read in util_file_read_fd()

In certain situations util_file_read_fd_buf() might return a larger
buffer than printable characters were read (e.g. a file was padded with
zeros). This can lead to util_file_read_fd() returning NULL with a freed
buffer even though a certain amount of printable characters were read.

This behaviour causes a regression introduced with commit 9efd1df31d
("ipl_tools: Refactor read helper using util_file_read_text_file()") in
ipl_tools were the scp_data sysfs attribute is padded with 0 to fit an 8
byte alignment required by the architecture.

Fix this by comparing the size read with the actual string length and
use the smaller value for further processing.

Fixes: 9efd1df31d ("ipl_tools: Refactor read helper using util_file_read_text_file()")
Tested-by: Jan Polensky <japo@linux.ibm.com>
Reviewed-by: Jan Polensky <japo@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Höppner
2025-04-23 12:35:51 +02:00
parent b3db52fd16
commit 0eac97542d

View File

@@ -616,6 +616,10 @@ char *util_file_read_fd(FILE *fd, int chomp)
if (util_file_read_fd_buf(fd, (void **) &buffer, &done))
return NULL;
/* Prevent over-read if buffer is larger than amount of read characters */
if (buffer)
done = MIN(done, strnlen(buffer, done));
/* Check if this is a text file at all (required to filter out
* binary sysfs attributes).
*/