]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: improving robustness in reading kernel messages
authorKarel Zak <kzak@redhat.com>
Wed, 19 Jun 2024 09:19:49 +0000 (11:19 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 27 Jun 2024 10:10:56 +0000 (12:10 +0200)
* Ensure data termination from read() function
* Allocate space for terminator using "sizeof(buf)-1"

Signed-off-by: Karel Zak <kzak@redhat.com>
(cherry picked from commit 0389a9eae9281405d177d0eb9ad979d7bd47a7c6)

libmount/src/hook_mount.c

index 10b226636911dcf91dec993dbf1b634cab7c9127..af84f755eaa499989eccef6be2daefb8afe4a386 100644 (file)
@@ -73,9 +73,15 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd)
        free(cxt->syscall_errmsg);
        cxt->syscall_errmsg = 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)