[PATCH] start_cmd: Drop privileges before target command

Mikhail Dmitrichenko m.dmitrichenko222@gmail.com
Wed Nov 18 16:39:43 GMT 2026


stapbpf is installed setuid root and shares start_cmd() with stapio.
Its -c option forks a user-controlled command and previously execed it
while the child still retained the effective user ID of root. A member
of stapusr with a valid BPF module could therefore run an arbitrary
command as root.

Drop the real, effective, and saved group and user IDs to the invoking
user in the child before parsing or executing the target command. This
mirrors the privilege drop performed by run_as() when staprun launches
stapio.

Signed-off-by: Mikhail Dmitrichenko <m.dmitrichenko222@gmail.com>
---
 staprun/start_cmd.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/staprun/start_cmd.c b/staprun/start_cmd.c
index cfc4df49f..3f4ef5d0e 100644
--- a/staprun/start_cmd.c
+++ b/staprun/start_cmd.c
@@ -63,6 +63,24 @@ closefrom(int lowfd)
   }
 }
 
+static void
+drop_target_cmd_privileges(void)
+{
+  uid_t target_uid = getuid();
+  gid_t target_gid = getgid();
+
+  if (setresgid(target_gid, target_gid, target_gid) < 0)
+    {
+      _perr("setresgid");
+      _exit(1);
+    }
+  if (setresuid(target_uid, target_uid, target_uid) < 0)
+    {
+      _perr("setresuid");
+      _exit(1);
+    }
+}
+
 #if WORKAROUND_BZ467568
 /* When a SIGUSR1 signal arrives, set this variable. */
 volatile sig_atomic_t usr1_interrupt = 0;
@@ -134,6 +152,8 @@ void start_cmd(void)
     a.sa_handler = SIG_DFL;
     sigaction(SIGINT, &a, NULL);
 
+    drop_target_cmd_privileges();
+
     /* Close any FDs we still hold, similarly as though this were
      * a program being spawned due to an system("") tapset function. */
     closefrom(3);
-- 
2.43.0


More information about the Systemtap mailing list