]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
seccomp_user_notif.2: EXAMPLE: correct the check for NUL in buffer returned by read()
authorMichael Kerrisk <mtk.manpages@gmail.com>
Sat, 24 Oct 2020 08:46:28 +0000 (10:46 +0200)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Sun, 25 Oct 2020 21:08:30 +0000 (22:08 +0100)
In the usual case, read(fd, buf, PATH_MAX) will return PATH_MAX
bytes that include trailing garbage after the pathname. So the
right check is to scan from the start of the buffer to see if
there's a NUL, and error if there is not.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
man2/seccomp_user_notif.2

index 7ee9f866cad3a3ea1a50adfb1300d742699f153e..3b2617ed38bcd6795bda4d711c17978c26353b5a 100644 (file)
@@ -1216,7 +1216,6 @@ getTargetPathname(struct seccomp_notif *req, int notifyFd,
                   char *path, size_t len)
 {
     char procMemPath[PATH_MAX];
-    bool res = true;
 
     snprintf(procMemPath, sizeof(procMemPath), "/proc/%d/mem", req\->pid);
 
@@ -1247,18 +1246,19 @@ getTargetPathname(struct seccomp_notif *req, int notifyFd,
         exit(EXIT_FAILURE);
     }
 
+    if (close(procMemFd) == \-1)
+        errExit("close\-/proc/PID/mem");
+
     /* We have no guarantees about what was in the memory of the target
        process. We therefore treat the buffer returned by pread() as
        untrusted input. The buffer should be terminated by a null byte;
        if not, then we will trigger an error for the target process. */
 
-    if (path[nread \- 1] != \(aq\0\(aq)
-        res = false;
-
-    if (close(procMemFd) == \-1)
-        errExit("close\-/proc/PID/mem");
+    for (int j = 0; j < nread; j++)
+        if (path[j] == \(aq\0\(aq)
+            return true;
 
-    return res;
+    return false;
 }
 
 /* Handle notifications that arrive via the SECCOMP_RET_USER_NOTIF file