]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib: simplify check_status and different_namespace interface
authorTobias Deiminger <tobias.deiminger@linutronix.de>
Wed, 6 May 2026 12:39:39 +0000 (14:39 +0200)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Tue, 19 May 2026 12:46:55 +0000 (14:46 +0200)
Change the interface of check_status and different_namespace to take pid
and tid instead of a partially constructed path string.

This is simpler and counters bugs like in the previous commit by design.

Signed-off-by: Tobias Deiminger <tobias.deiminger@linutronix.de>
lib/user_busy.c

index 9e5ff78baac805059877b483b6e9c146c035a9a4..aea16b13bcf50254c8020876d45eb9c93e9d8eb9 100644 (file)
@@ -36,7 +36,7 @@
 
 
 #ifdef __linux__
-static int check_status (const char *name, const char *sname, uid_t uid);
+static int check_status (const char *name, uid_t uid, pid_t pid, pid_t tid);
 static int user_busy_processes (const char *name, uid_t uid);
 #else                          /* !__linux__ */
 static int user_busy_utmp (const char *name);
@@ -94,13 +94,13 @@ user_busy_utmp(const char *name)
 #ifdef __linux__
 #ifdef ENABLE_SUBIDS
 #define in_parentuid_range(uid) ((uid) >= parentuid && (uid) < parentuid + range)
-static int different_namespace (const char *sname)
+static int different_namespace (pid_t pid, pid_t tid)
 {
        /* 41: /proc/xxxxxxxxxx/task/xxxxxxxxxx/ns/user + \0 */
        char     path[41];
        char     buf[512], buf2[512];
 
-       stprintf_a(path, "/proc/%s/ns/user", sname);
+       stprintf_a(path, "/proc/%d/task/%d/ns/user", pid, tid);
 
        if (readlinknul_a(path, buf) == -1)
                return 0;
@@ -116,14 +116,14 @@ static int different_namespace (const char *sname)
 #endif                          /* ENABLE_SUBIDS */
 
 
-static int check_status (const char *name, const char *sname, uid_t uid)
+static int check_status (const char *name, uid_t uid, pid_t pid, pid_t tid)
 {
        /* 40: /proc/xxxxxxxxxx/task/xxxxxxxxxx/status + \0 */
        char  status[40];
        char  line[1024];
        FILE  *sfile;
 
-       stprintf_a(status, "/proc/%s/status", sname);
+       stprintf_a(status, "/proc/%d/task/%d/status", pid, tid);
 
        sfile = fopen (status, "r");
        if (NULL == sfile) {
@@ -144,7 +144,7 @@ static int check_status (const char *name, const char *sname, uid_t uid)
                                        return 1;
                                }
 #ifdef ENABLE_SUBIDS
-                               if (    different_namespace (sname)
+                               if (    different_namespace (pid, tid)
                                     && (   have_sub_uids(name, ruid, 1)
                                         || have_sub_uids(name, euid, 1)
                                         || have_sub_uids(name, suid, 1))
@@ -225,7 +225,7 @@ static int user_busy_processes (const char *name, uid_t uid)
                        continue;
                }
 
-               if (check_status (name, tmp_d_name, uid) != 0) {
+               if (check_status (name, uid, pid, pid) != 0) {
                        (void) closedir (proc);
 #ifdef ENABLE_SUBIDS
                        sub_uid_close(true);
@@ -241,17 +241,13 @@ static int user_busy_processes (const char *name, uid_t uid)
                if (task_dir != NULL) {
                        while (NULL != (ent = readdir(task_dir))) {
                                pid_t tid;
-                               /* 27: xxxxxxxxxx/task/xxxxxxxxxx + \0 */
-                               char  tid_sname[27];
-
                                if (get_pid(ent->d_name, &tid) == -1) {
                                        continue;
                                }
                                if (tid == pid) {
                                        continue;
                                }
-                               stprintf_a(tid_sname, "%ld/task/%ld", (long) pid, (long) tid);
-                               if (check_status (name, tid_sname, uid) != 0) {
+                               if (check_status (name, uid, pid, tid) != 0) {
                                        (void) closedir (proc);
                                        (void) closedir (task_dir);
 #ifdef ENABLE_SUBIDS