This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository terminology.

View the commit online.

commit 7e9f2bd7fa782bed5f7317179cf91672c3ac49c3
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 8 22:45:03 2026 -0600

    termsrv: share the daemon kill
    
    tymux kill ends a session by sending SIGTERM to the process that listens
    on its socket. tybenchd needs the same to clean up after a failed run,
    so the code moves to termsrv_daemon_kill().
    
    It also gets safer. A pid of 0, which is what a process in another PID
    namespace shows up as, or 1 is refused: kill(0, ...) would signal our
    own process group. When built against headers that define
    SO_PEERPIDFD and pidfd_send_signal(), and run on a kernel that has them
    (Linux 6.5), the signal goes through a pidfd, which cannot end up on
    another process that reused the pid of a daemon that just exited. A
    kernel that lacks the option or the syscall falls back to kill(), as
    does a build without the headers; any other failure is reported to the
    caller instead of taking that path.
---
 src/bin/termsrv.c    | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termsrv.h    |   5 ++
 src/bin/tymux.c      |   6 +-
 src/bin/tytest.c     |   1 +
 src/bin/unit_tests.h |   1 +
 5 files changed, 169 insertions(+), 5 deletions(-)

diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index b674b871..3e59ec5f 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -3,6 +3,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <sched.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -11,6 +12,7 @@
 #include <unistd.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 #include <sys/uio.h>
 #include <sys/un.h>
 #include <sys/wait.h>
@@ -182,6 +184,70 @@ termsrv_connect(const char *name)
    return fd;
 }
 
+/* SO_PEERPIDFD and pidfd_send_signal() are Linux 6.5 and 5.1 additions that
+ * only the headers of a recent enough libc define, and whose numbers differ
+ * between architectures. */
+#if defined(SO_PEERPIDFD) && defined(SYS_pidfd_send_signal)
+# define TERMSRV_PIDFD 1
+#endif
+
+/* A pid of 0 or 1 is never signaled: kill(0, ...) would hit our own process
+ * group, and 0 is what a process in another PID namespace shows up as. kill()
+ * can name another process if the daemon exited and its number was reused. */
+static int
+_sigterm_pid_send(pid_t pid)
+{
+   if (pid <= 1)
+     {
+        errno = ESRCH;
+        return -1;
+     }
+   return kill(pid, SIGTERM);
+}
+
+int
+termsrv_daemon_kill(int sock)
+{
+   struct ucred cred;
+   socklen_t cred_len = sizeof(cred);
+
+   if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0)
+     return -1;
+   if (cred.uid != getuid())
+     {
+        errno = EPERM;
+        return -1;
+     }
+#if defined(TERMSRV_PIDFD)
+   {
+      /* The kernel resolves the pidfd from the same record as SO_PEERCRED,
+       * so it cannot name a process that reused the pid. Only a kernel
+       * that does not know the option, or the syscall, falls back to the
+       * pid: any other failure is the caller's to see. */
+      int pidfd = -1, ret, err;
+
+      cred_len = sizeof(pidfd);
+      if (getsockopt(sock, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &cred_len) < 0)
+        {
+           if (errno != ENOPROTOOPT && errno != EOPNOTSUPP)
+             return -1;
+        }
+      else
+        {
+           ret = (int)syscall(SYS_pidfd_send_signal, pidfd, SIGTERM, NULL, 0);
+           err = errno;
+           close(pidfd);
+           if (ret == 0 || err != ENOSYS)
+             {
+                errno = err;
+                return ret;
+             }
+        }
+   }
+#endif
+   return _sigterm_pid_send(cred.pid);
+}
+
 static void
 _daemon_exec(const char *name)
 {
@@ -639,6 +705,101 @@ tytest_termsrv_tymux_dir_ensure(void)
    return 0;
 }
 
+/* Test: termsrv_daemon_kill() sends SIGTERM to the process listening on
+ * the socket, here a forked stand-in for tymuxd, and _sigterm_pid_send()
+ * refuses the pids that would hit more than one process. */
+int
+tytest_termsrv_daemon_kill(void)
+{
+   char base[] = "/tmp/tytest-termsrv-XXXXXX";
+   char dir[4096], parent[4096];
+   struct sockaddr_un sa = { .sun_family = AF_UNIX };
+   char *old_runtime = getenv("XDG_RUNTIME_DIR");
+   char *saved_runtime = old_runtime ? strdup(old_runtime) : NULL;
+   int pass, ready[2], sock, status, waited;
+   char byte;
+   pid_t pid;
+
+   assert(mkdtemp(base));
+   setenv("XDG_RUNTIME_DIR", base, 1);
+   assert(termsrv_ensure_tymux_dir() == 0);
+   assert(termsrv_socket_path("kill", sa.sun_path, sizeof(sa.sun_path)) == 0);
+
+   /* would signal this very process group if it were not refused */
+   assert(_sigterm_pid_send(0) == -1 && errno == ESRCH);
+   assert(_sigterm_pid_send(1) == -1 && errno == ESRCH);
+
+   /* pass 0 goes through termsrv_daemon_kill(), which uses a pidfd where
+    * it was built for and the kernel has one; pass 1 signals the pid */
+   for (pass = 0; pass < 2; pass++)
+     {
+        assert(pipe(ready) == 0);
+        pid = fork();
+        assert(pid >= 0);
+        if (pid == 0)
+          {
+             sigset_t none;
+             int listener = socket(AF_UNIX, SOCK_STREAM, 0);
+
+             /* the unit tests ran Ecore, which handles SIGTERM */
+             signal(SIGTERM, SIG_DFL);
+             sigemptyset(&none);
+             sigprocmask(SIG_SETMASK, &none, NULL);
+             if (listener < 0 ||
+                 bind(listener, (struct sockaddr *)&sa, sizeof(sa)) < 0 ||
+                 listen(listener, 1) < 0 || write(ready[1], "r", 1) != 1)
+               _exit(1);
+             alarm(30);
+             for (;;) pause();
+          }
+        close(ready[1]);
+        if (read(ready[0], &byte, 1) != 1)
+          {
+             kill(pid, SIGKILL);
+             waitpid(pid, NULL, 0);
+             assert(!"stand-in daemon did not start");
+          }
+        close(ready[0]);
+
+        sock = termsrv_connect("kill");
+        if (sock < 0 ||
+            (pass == 0 ? termsrv_daemon_kill(sock) : _sigterm_pid_send(pid)) < 0)
+          {
+             kill(pid, SIGKILL);
+             waitpid(pid, NULL, 0);
+             assert(!"could not signal the stand-in daemon");
+          }
+        close(sock);
+
+        for (waited = 0; waited < 500; waited++)
+          {
+             if (waitpid(pid, &status, WNOHANG) == pid)
+               break;
+             usleep(10 * 1000);
+          }
+        if (waited == 500)
+          {
+             kill(pid, SIGKILL);
+             waitpid(pid, NULL, 0);
+             assert(!"stand-in daemon did not exit");
+          }
+        assert(WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM);
+        assert(unlink(sa.sun_path) == 0);
+     }
+
+   assert(termsrv_tymux_dir(dir, sizeof(dir)) == 0);
+   snprintf(parent, sizeof(parent), "%s/terminology", base);
+   assert(rmdir(dir) == 0);
+   assert(rmdir(parent) == 0);
+   assert(rmdir(base) == 0);
+   if (saved_runtime)
+     setenv("XDG_RUNTIME_DIR", saved_runtime, 1);
+   else
+     unsetenv("XDG_RUNTIME_DIR");
+   free(saved_runtime);
+   return 0;
+}
+
 int
 tytest_termsrv_attach_mode(void)
 {
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index 26b2333f..f6dd6f3d 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -205,6 +205,11 @@ int termsrv_ensure_tymux_dir(void);
  * removed. Returns the connected socket, or -1 with errno set. */
 int termsrv_connect(const char *name);
 
+/* Ask the tymuxd listening on sock, a socket termsrv_connect() returned,
+ * to end its session by sending it SIGTERM. Returns 0 on success, -1 on
+ * error with errno set, EPERM if the daemon belongs to another user. */
+int termsrv_daemon_kill(int sock);
+
 /* Start tymuxd for name in the background, detached from the calling
  * terminal, and wait until it accepts connections. The tymuxd installed
  * next to the running program is preferred over the one in $PATH so that
diff --git a/src/bin/tymux.c b/src/bin/tymux.c
index 9f08eb95..1adff2be 100644
--- a/src/bin/tymux.c
+++ b/src/bin/tymux.c
@@ -780,8 +780,6 @@ static int
 _cmd_kill(int argc, char **argv)
 {
    const struct timespec delay = { .tv_sec = 0, .tv_nsec = 10 * 1000 * 1000 };
-   struct ucred cred;
-   socklen_t cred_len = sizeof(cred);
    int sock, i;
 
    if (argc != 3)
@@ -795,9 +793,7 @@ _cmd_kill(int argc, char **argv)
         fprintf(stderr, "tymux: no tymux named '%s'\n", argv[2]);
         return 1;
      }
-   /* identifies the process that listens: tymuxd */
-   if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0 ||
-       cred.uid != getuid() || kill(cred.pid, SIGTERM) < 0)
+   if (termsrv_daemon_kill(sock) < 0)
      {
         perror("tymux: kill");
         close(sock);
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 0b947c22..8147ce70 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -158,6 +158,7 @@ static struct {
        { "termsrv_name", tytest_termsrv_name},
        { "termsrv_socket_path", tytest_termsrv_socket_path},
        { "termsrv_tymux_dir_ensure", tytest_termsrv_tymux_dir_ensure},
+       { "termsrv_daemon_kill", tytest_termsrv_daemon_kill},
        { "termsrv_attach_mode", tytest_termsrv_attach_mode},
        { "termsrv_msg_round_trip", tytest_termsrv_msg_round_trip},
        { "termsrv_msg_invalid", tytest_termsrv_msg_invalid},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 4e66afdc..b08e6e9e 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -116,6 +116,7 @@ int tytest_fuzz_resize_max(void);
 int tytest_termsrv_name(void);
 int tytest_termsrv_socket_path(void);
 int tytest_termsrv_tymux_dir_ensure(void);
+int tytest_termsrv_daemon_kill(void);
 int tytest_termsrv_attach_mode(void);
 int tytest_termsrv_msg_round_trip(void);
 int tytest_termsrv_msg_invalid(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to