From 0389a9eae9281405d177d0eb9ad979d7bd47a7c6 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 19 Jun 2024 11:19:49 +0200 Subject: [PATCH] libmount: improving robustness in reading kernel messages * Ensure data termination from read() function * Allocate space for terminator using "sizeof(buf)-1" Signed-off-by: Karel Zak --- libmount/src/hook_mount.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libmount/src/hook_mount.c b/libmount/src/hook_mount.c index 995de614444..f5647930318 100644 --- a/libmount/src/hook_mount.c +++ b/libmount/src/hook_mount.c @@ -72,9 +72,15 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd) mnt_context_set_errmsg(cxt, NULL); - while ((rc = read(fd, buf, sizeof(buf))) != -1) { - if (rc > 0 && buf[rc - 1] == '\n') - buf[rc - 1] = '\0'; + while ((rc = read(fd, buf, sizeof(buf) - 1)) != -1) { + + if (rc == 0) + continue; + if (buf[rc - 1] == '\n') + buf[--rc] = '\0'; + else + buf[rc] = '\0'; + DBG(CXT, ul_debug("message from kernel: \"%*s\"", rc, buf)); if (rc < 3 || strncmp((char *) buf, "e ", 2) != 0)