]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
kill: use ul_get_valid_pidfd_or_err() to validate user provided pidfd inodes
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 25 Nov 2025 04:54:39 +0000 (23:54 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Thu, 4 Dec 2025 18:45:03 +0000 (13:45 -0500)
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
misc-utils/kill.c

index 245c3b6b7a3c3719ff1f543c63ba44be9a05c70a..ff0dcae52eb425067c8e30119085c70533fe9d3c 100644 (file)
@@ -534,8 +534,7 @@ static int kill_with_timeout(const struct kill_control *ctl)
        info.si_value.sival_int =
            ctl->use_sigval != 0 ? ctl->use_sigval : ctl->numsig;
 
-       if ((pfd = pidfd_open(ctl->pid, 0)) < 0)
-               err(EXIT_FAILURE, _("pidfd_open() failed: %d"), ctl->pid);
+       pfd = ul_get_valid_pidfd_or_err(ctl->pid, ctl->pidfd_ino);
        p.fd = pfd;
        p.events = POLLIN;
 
@@ -564,22 +563,6 @@ static int kill_with_timeout(const struct kill_control *ctl)
 }
 #endif
 
-#ifdef USE_KILL_WITH_PIDFDINO
-static int validate_pfd_ino(int pfd, ino_t pfd_ino)
-{
-       int rc;
-       struct stat f;
-
-       rc = fstat(pfd, &f);
-       if (rc != 0)
-               return -EINVAL;
-
-       if (f.st_ino != pfd_ino)
-               return -EINVAL;
-       return 0;
-}
-#endif
-
 static int kill_verbose(const struct kill_control *ctl)
 {
        int rc = 0;
@@ -603,15 +586,7 @@ static int kill_verbose(const struct kill_control *ctl)
 #ifdef USE_KILL_WITH_PIDFDINO
                if ((ctl->pidfd_ino > 0)) {
                        int pfd;
-                       pfd = pidfd_open(ctl->pid, 0);
-                       if (pfd < 0)
-                               err(EXIT_FAILURE, _("pidfd_open() failed: %d"), ctl->pid);
-
-                       rc = validate_pfd_ino(pfd, ctl->pidfd_ino);
-                       if (rc < 0)
-                               errx(EXIT_FAILURE, _("pidfd inode %"PRIu64" not found for pid %d: %s"),
-                                                               ctl->pidfd_ino, ctl->pid, strerror(-rc));
-
+                       pfd = ul_get_valid_pidfd_or_err(ctl->pid, ctl->pidfd_ino);
                        rc = pidfd_send_signal(pfd, ctl->numsig, 0, 0);
                        if (rc < 0)
                                err(EXIT_FAILURE, _("pidfd_send_signal() failed"));