Skip to contentSkip to conversation

linux-user: Filter /proc/*/task/ and validate tkill targets

PATCH Superseded Ali Raza 2 participants 6 messages Apr 15 – Apr 24

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ali Raza

author
#0 lore raw

cover linux-user: Filter /proc/*/task/ and validate tkill targets

This series fixes a problem where QEMU user-mode exposes internal host threads (RCU, TCG workers) to guest processes via /proc/*/task/ and allows the guest to signal them via tkill/tgkill.

Patch 1: Filters getdents/getdents64 on /proc/<pid>/task/ Patch 2: Validates tkill/tgkill targets against guest CPU list Patch 3: Adds a multiarch test exercising both fixes

---
Ali Raza (3):
linux-user: Filter /proc/*/task/ to hide QEMU-internal threads
linux-user: Validate tkill/tgkill targets are guest threads tests/tcg: Add test for /proc/self/task/ filtering and tkill validation

 linux-user/syscall.c                        | 114 +++++++++++++++++-
 tests/tcg/multiarch/Makefile.target         |   2 +
 tests/tcg/multiarch/linux/linux-proc-task.c | 178 ++++++++++++++++++++++++++++
 3 files changed, 292 insertions(+), 2 deletions(-)

---
base-commit: da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-master-3a0fa2ebf2d5

Best regards,

Signature · 1 line
Ali Raza <[email protected]>
author
#1 lore raw

PATCH 1/3 linux-user: Filter /proc/*/task/ to hide QEMU-internal threads

When a guest process reads /proc/<pid>/task/ via getdents/getdents64, the host kernel returns directory entries for all host threads in the process, including QEMU-internal threads (RCU, TCG workers) that have no guest CPUState. This causes problems for guest libraries like libcap's PSX that enumerate threads via /proc/self/task/ and send signals to each one -- signals sent to QEMU-internal threads are never handled, leading to deadlocks.

Add filtering to do_getdents() and do_getdents64() that detects when the fd refers to a /proc/<pid>/task/ directory and skips entries whose TID does not belong to a guest thread. Guest thread TIDs are identified by iterating the CPU list via CPU_FOREACH(), matching the existing pattern used for /proc/self/stat num_threads.

This fixes the psx_test hang reported in containers using QEMU user-mode emulation for cross-architecture builds.

Resolves: https://github.com/AndrewGMorgan/libcap_mirror/issues/6

Signed-off-by Ali Raza (@locus-x64)
---
 linux-user/syscall.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
linux-user/syscall.c +80 -0
index f4b74ad350..b5a912dc22 100644
@@ -9108,6 +9108,50 @@ static int host_to_target_cpu_mask(const unsigned long *host_mask,
     return 0;
 }
 
+/*
+ * Check if a directory fd refers to /proc/<pid>/task/ for the current
+ * process.  Used to filter out QEMU-internal host threads (RCU, TCG)
+ * that are not guest threads from directory listings.
+ */
+static bool is_proc_pid_task_dir(int dirfd)
+{
+    char link_path[64];
+    char link_target[PATH_MAX];
+    ssize_t len;
+    char expected[80];
+    int expected_len;
+
+    snprintf(link_path, sizeof(link_path), "/proc/self/fd/%d", dirfd);
+    len = readlink(link_path, link_target, sizeof(link_target) - 1);
+    if (len < 0) {
+        return false;
+    }
+    link_target[len] = '\0';
+
+    expected_len = snprintf(expected, sizeof(expected),
+                            "/proc/%d/task", getpid());
+    return strncmp(link_target, expected, expected_len) == 0
+           && (link_target[expected_len] == '\0'
+               || link_target[expected_len] == '/');
+}
+
+/*
+ * Check if a given host TID belongs to a guest thread by looking it up
+ * in the CPU list.  Must be called under RCU read lock.
+ */
+static bool is_guest_tid(pid_t tid)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        TaskState *ts = get_task_state(cpu);
+        if (ts->ts_tid == tid) {
+            return true;
+        }
+    }
+    return false;
+}
+
 #ifdef TARGET_NR_getdents
 static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
 {
@@ -9116,6 +9160,7 @@ static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
     int hlen, hoff, toff;
     int hreclen, treclen;
     off_t prev_diroff = 0;
+    bool filter_task = is_proc_pid_task_dir(dirfd);
 
     hdirp = g_try_malloc(count);
     if (!hdirp) {
@@ -9150,6 +9195,23 @@ static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
 
         namelen = strlen(hde->d_name);
         hreclen = hde->d_reclen;
+
+        /*
+         * Filter /proc/<pid>/task/ listings to hide QEMU-internal
+         * host threads (RCU, TCG) that have no guest CPUState.
+         */
+        if (filter_task) {
+            char *endp;
+            long tid = strtol(hde->d_name, &endp, 10);
+            if (tid > 0 && *endp == '\0') {
+                WITH_RCU_READ_LOCK_GUARD() {
+                    if (!is_guest_tid(tid)) {
+                        treclen = 0;
+                        continue;
+                    }
+                }
+            }
+        }
         treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
         treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
 
@@ -9203,6 +9265,7 @@ static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
     int hlen, hoff, toff;
     int hreclen, treclen;
     off_t prev_diroff = 0;
+    bool filter_task = is_proc_pid_task_dir(dirfd);
 
     hdirp = g_try_malloc(count);
     if (!hdirp) {
@@ -9226,6 +9289,23 @@ static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
 
         namelen = strlen(hde->d_name) + 1;
         hreclen = hde->d_reclen;
+
+        /*
+         * Filter /proc/<pid>/task/ listings to hide QEMU-internal
+         * host threads (RCU, TCG) that have no guest CPUState.
+         */
+        if (filter_task) {
+            char *endp;
+            long tid = strtol(hde->d_name, &endp, 10);
+            if (tid > 0 && *endp == '\0') {
+                WITH_RCU_READ_LOCK_GUARD() {
+                    if (!is_guest_tid(tid)) {
+                        treclen = 0;
+                        continue;
+                    }
+                }
+            }
+        }
         treclen = offsetof(struct target_dirent64, d_name) + namelen;
         treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] linux-user: Filter /proc/*/task/ to hide QEMU-internal threads

Quoted 88 lines · Ali Raza

When a guest process reads /proc/<pid>/task/ via getdents/getdents64, the host kernel returns directory entries for all host threads in the process, including QEMU-internal threads (RCU, TCG workers) that have no guest CPUState. This causes problems for guest libraries like libcap's PSX that enumerate threads via /proc/self/task/ and send signals to each one -- signals sent to QEMU-internal threads are never handled, leading to deadlocks.

Add filtering to do_getdents() and do_getdents64() that detects when the fd refers to a /proc/<pid>/task/ directory and skips entries whose TID does not belong to a guest thread. Guest thread TIDs are identified by iterating the CPU list via CPU_FOREACH(), matching the existing pattern used for /proc/self/stat num_threads.

This fixes the psx_test hang reported in containers using QEMU user-mode emulation for cross-architecture builds.

Resolves: https://github.com/AndrewGMorgan/libcap_mirror/issues/6

Signed-off-by Ali Raza (@locus-x64)
---
  linux-user/syscall.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 80 insertions(+)
linux-user/syscall.c +45 -0
index f4b74ad350..b5a912dc22 100644
@@ -9108,6 +9108,50 @@ static int host_to_target_cpu_mask(const unsigned long *host_mask,
      return 0;
  }
 
+/*
+ * Check if a directory fd refers to /proc/<pid>/task/ for the current
+ * process.  Used to filter out QEMU-internal host threads (RCU, TCG)
+ * that are not guest threads from directory listings.
+ */
+static bool is_proc_pid_task_dir(int dirfd)
+{
+    char link_path[64];
+    char link_target[PATH_MAX];
+    ssize_t len;
+    char expected[80];
+    int expected_len;
+
+    snprintf(link_path, sizeof(link_path), "/proc/self/fd/%d", dirfd);
+    len = readlink(link_path, link_target, sizeof(link_target) - 1);
+    if (len < 0) {
+        return false;
+    }
+    link_target[len] = '\0';
+
+    expected_len = snprintf(expected, sizeof(expected),
+                            "/proc/%d/task", getpid());
+    return strncmp(link_target, expected, expected_len) == 0
+           && (link_target[expected_len] == '\0'
+               || link_target[expected_len] == '/');
+}
+
+/*
+ * Check if a given host TID belongs to a guest thread by looking it up
+ * in the CPU list.  Must be called under RCU read lock.
+ */
+static bool is_guest_tid(pid_t tid)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        TaskState *ts = get_task_state(cpu);
+        if (ts->ts_tid == tid) {
+            return true;
+        }
+    }
+    return false;
+}
+
  #ifdef TARGET_NR_getdents
  static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
  {
@@ -9116,6 +9160,7 @@ static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
      int hlen, hoff, toff;
      int hreclen, treclen;
      off_t prev_diroff = 0;
+    bool filter_task = is_proc_pid_task_dir(dirfd);
 
      hdirp = g_try_malloc(count);
      if (!hdirp) {
@@ -9150,6 +9195,23 @@ static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
 

linux-user/syscall.c static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count) · line 9205

Quoted 10 lines
linux-user/syscall.c +8 -0
          namelen = strlen(hde->d_name);
          hreclen = hde->d_reclen;
+
+        /*
+         * Filter /proc/<pid>/task/ listings to hide QEMU-internal
+         * host threads (RCU, TCG) that have no guest CPUState.
+         */
+        if (filter_task) {
+            char *endp;
+            long tid = strtol(hde->d_name, &endp, 10);

I think we should user type pid_t here:
pid_t tid = strtol(...)

Quoted 22 lines
linux-user/syscall.c +10 -0
+            if (tid > 0 && *endp == '\0') {
+                WITH_RCU_READ_LOCK_GUARD() {
+                    if (!is_guest_tid(tid)) {
+                        treclen = 0;
+                        continue;
+                    }
+                }
+            }
+        }
          treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
          treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
 
@@ -9203,6 +9265,7 @@ static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
      int hlen, hoff, toff;
      int hreclen, treclen;
      off_t prev_diroff = 0;
+    bool filter_task = is_proc_pid_task_dir(dirfd);
 
      hdirp = g_try_malloc(count);
      if (!hdirp) {
@@ -9226,6 +9289,23 @@ static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
 

linux-user/syscall.c static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count) · line 9299

Quoted 10 lines
linux-user/syscall.c +8 -0
          namelen = strlen(hde->d_name) + 1;
          hreclen = hde->d_reclen;
+
+        /*
+         * Filter /proc/<pid>/task/ listings to hide QEMU-internal
+         * host threads (RCU, TCG) that have no guest CPUState.
+         */
+        if (filter_task) {
+            char *endp;
+            long tid = strtol(hde->d_name, &endp, 10);

same here

Quoted 13 lines
linux-user/syscall.c +9 -0
+            if (tid > 0 && *endp == '\0') {
+                WITH_RCU_READ_LOCK_GUARD() {
+                    if (!is_guest_tid(tid)) {
+                        treclen = 0;
+                        continue;
+                    }
+                }
+            }
+        }
          treclen = offsetof(struct target_dirent64, d_name) + namelen;
          treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
 
author
#3 lore raw

Re: [PATCH 1/3] linux-user: Filter /proc/*/task/ to hide QEMU-internal threads

Quoted 5 lines · Helge Deller

Review

Quoted 2 lines
(quoted diff) +2 -0
+            char *endp;
+            long tid = strtol(hde->d_name, &endp, 10);

I think we should use type pid_t here:
pid_t tid = strtol(...)

Thanks for the review, Helge.

Agreed, pid_t is the correct type for a thread id. Fixed in v2 for both do_getdents() and do_getdents64(). I also switched the parsing to qemu_strtoi() so the whole entry name must be numeric (no trailing junk), which avoids relying on errno/endp checks.

v2 also addresses a few other issues that came up on a closer review (TOCTOU on the readlink-based dir check, a false EOF when an entire host batch is filtered out, and tightening the tkill/tgkill validation to host-process-local checks). Full changelog is in the v2 cover letter:

https://lore.kernel.org/qemu-devel/[email protected]/

Thanks,
Ali Raza

author
#4 lore raw

PATCH 2/3 linux-user: Validate tkill/tgkill targets are guest threads

In reply to Ali Raza

The tkill and tgkill syscall handlers pass the guest-supplied TID directly to the host kernel without checking whether it belongs to a guest thread. This allows a guest to send signals to QEMU-internal host threads (RCU, TCG workers) that have no CPUState and no guest signal handlers, which can cause hangs or disrupt QEMU operation.

Add validation that checks the target TID against the guest CPU list before forwarding the signal to the host. For tgkill, also verify that the tgid matches the current process. Return -ESRCH for TIDs that do not correspond to any guest thread, matching the behavior a real kernel would return for a nonexistent thread.

This complements the /proc/*/task/ filtering in the previous commit to provide defense-in-depth: even if a guest discovers or guesses a QEMU-internal thread TID, it cannot send signals to it.

Signed-off-by Ali Raza (@locus-x64)
---
 linux-user/syscall.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
linux-user/syscall.c +32 -2
index b5a912dc22..a075b9a90b 100644
@@ -13449,11 +13449,41 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
 #endif
 
     case TARGET_NR_tkill:
-        return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
+    {
+        int tid = (int)arg1;
+        /*
+         * Reject signals to host threads that are not guest threads.
+         * QEMU-internal threads (RCU, TCG) share the host PID but have
+         * no CPUState and cannot handle guest-originated signals.
+         */
+        WITH_RCU_READ_LOCK_GUARD() {
+            if (!is_guest_tid(tid)) {
+                return -TARGET_ESRCH;
+            }
+        }
+        return get_errno(safe_tkill(tid, target_to_host_signal(arg2)));
+    }
 
     case TARGET_NR_tgkill:
-        return get_errno(safe_tgkill((int)arg1, (int)arg2,
+    {
+        int tgid = (int)arg1;
+        int tid = (int)arg2;
+        /*
+         * Validate that the target TID is a guest thread.  Also verify
+         * that the tgid matches our process, since all guest threads
+         * share the same host tgid.
+         */
+        if (tgid != getpid()) {
+            return -TARGET_ESRCH;
+        }
+        WITH_RCU_READ_LOCK_GUARD() {
+            if (!is_guest_tid(tid)) {
+                return -TARGET_ESRCH;
+            }
+        }
+        return get_errno(safe_tgkill(tgid, tid,
                          target_to_host_signal(arg3)));
+    }
 
 #ifdef TARGET_NR_set_robust_list
     case TARGET_NR_set_robust_list:
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 3/3 tests/tcg: Add test for /proc/self/task/ filtering and tkill validation

In reply to Ali Raza

Add a multiarch linux-user test that verifies:

1. /proc/self/task/ only lists guest threads -- spawns NUM_THREADS pthreads, reads the task directory, and asserts every entry maps to a known guest TID and the total count matches.

2. tkill(guest_tid, 0) succeeds for all guest threads.

3. tkill to a non-guest TID returns ESRCH.

On native Linux this test trivially passes since there are no QEMU-internal threads. Under QEMU user-mode, it exercises the getdents64 filtering and tkill validation added in the previous two commits.

Signed-off-by Ali Raza (@locus-x64)
---
 tests/tcg/multiarch/Makefile.target         |   2 +
 tests/tcg/multiarch/linux/linux-proc-task.c | 178 ++++++++++++++++++++++++++++
 2 files changed, 180 insertions(+)
tests/tcg/multiarch/Makefile.target +2 -0
index 508149d57b..8d6e2af617 100644
@@ -39,6 +39,8 @@ testthread: LDFLAGS+=-lpthread
 
 threadcount: LDFLAGS+=-lpthread
 
+linux-proc-task: LDFLAGS+=-lpthread
+
 signals: LDFLAGS+=-lrt -lpthread
 
 munmap-pthread: CFLAGS+=-pthread
tests/tcg/multiarch/linux/linux-proc-task.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.43.0