linux-user: Filter /proc/*/task/ and validate tkill targets
v2 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
---
Changes in v2:
- linux-user/syscall.c: use pid_t instead of long for the tid variable
in do_getdents() and do_getdents64() (suggested by Helge Deller).
- linux-user/syscall.c: replace ad-hoc readlink-at-getdents check with fd_trans-based tagging at open time, eliminating a TOCTOU race and
a broad-prefix match that also matched /proc/<pid>/task/<tid>/fd.
- linux-user/syscall.c (do_getdents, do_getdents64): retry sys_getdents
when an entire host batch is filtered out, so the guest does not see
a false EOF.
- linux-user/syscall.c (do_getdents, do_getdents64): parse the dirent
TID with qemu_strtoi() instead of strtol() so out-of-range values
cannot wrap silently into pid_t.
- linux-user/syscall.c (do_tkill): validate the guest tid range against
pid_t and only reject signals whose target lives in our own host
process and is not a guest thread (allow cross-process tkill to fall through to the kernel).
- linux-user/syscall.c (do_tgkill): drop the previous tgid==getpid()
blanket rejection and apply the same in-our-process-only filter, so legitimate cross-process tgkill keeps working.
- tests/tcg/multiarch/linux-proc-task.c: hard-assert ESRCH on tkill to
a non-guest tid, and add tgkill coverage (success on guest tids,
ESRCH on a wrong tgid).
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
To: [email protected]
Cc: Laurent Vivier <[email protected]>
Cc: Pierrick Bouvier <[email protected]>
Cc: Alex Bennée <[email protected]>
---
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/fd-trans.c | 6 + linux-user/fd-trans.h | 18 ++ linux-user/syscall.c | 353 +++++++++++++++++++++------- tests/tcg/multiarch/Makefile.target | 2 + tests/tcg/multiarch/linux/linux-proc-task.c | 193 +++++++++++++++ 5 files changed, 492 insertions(+), 80 deletions(-)
---
base-commit: da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-master-3a0fa2ebf2d5
Best regards,
Signature · 1 line
Ali Raza <[email protected]>
PATCH v2 2/3 linux-user: Validate tkill/tgkill targets are guest threads
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.
---
Changes in v2:
- Range-check tid/tgid before narrowing from abi_long to pid_t.
- do_tkill: only reject signals whose target lives in our own host process and is not a guest thread; cross-process tkill is passed through to the kernel unchanged.
- do_tgkill: drop the previous tgid==getpid() blanket rejection and apply the same in-our-process-only filter, so legitimate cross-process tgkill keeps working.
- New tid_is_qemu_internal() helper based on the CPU list and /proc/self/task/<tid> presence.
--- linux-user/syscall.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)
linux-user/syscall.c +53 -2
@@ -9161,6 +9161,27 @@ static bool is_guest_tid(pid_t tid)return false; } +/* +* Return true iff @tid identifies a thread inside our own host process +* that is not one of the guest threads -- i.e. a QEMU-internal helper +* thread (RCU, TCG worker, ...). Cross-process tids and unknown tids +* are not classified here and the caller should pass the syscall +* through to the kernel unchanged. +*/ +static bool tid_is_qemu_internal (pid_t tid) +{ +char path[64 ]; + + WITH_RCU_READ_LOCK_GUARD() { +if (is_guest_tid (tid)) { +return false; + } + } + +snprintf (path,sizeof (path),"/proc/self/task/%d" , (int )tid); +return access (path,F_OK ) ==0 ; +} +#ifdef TARGET_NR_getdentsstatic int do_getdents (abi_long dirfd,abi_long arg2,abi_long count) {
@@ -13511,11 +13532,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))); + { +pid_t tid = (pid_t )arg1; +if ((abi_long )tid != arg1) { +return -TARGET_ESRCH ; + } +/* +* Reject signals that target one of our own QEMU-internal +* helper threads (RCU, TCG worker, ...) which share our +* host PID but have no guest CPUState. Cross-process tids +* are passed through unchanged. +*/ +if (tid_is_qemu_internal (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, + { +pid_t tgid = (pid_t )arg1; +pid_t tid = (pid_t )arg2; +if ((abi_long )tgid != arg1 || (abi_long )tid != arg2) { +return -TARGET_ESRCH ; + } +/* +* Only screen targets in our own process; for cross-process +* tgkill we have no way to know which TIDs are guest threads +* in another QEMU instance, so the call is passed through. +*/ +if (tgid ==getpid () &&tid_is_qemu_internal (tid)) { +return -TARGET_ESRCH ; + } +return get_errno (safe_tgkill (tgid, tid,target_to_host_signal (arg3))); + }#ifdef TARGET_NR_set_robust_listcase TARGET_NR_set_robust_list:
Signature · 1 line
2.43.0
PATCH v2 3/3 tests/tcg: Add test for /proc/self/task/ filtering and tkill validation
In reply to Ali RazaAdd 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.
---
Changes in v2:
- Hard-assert ESRCH on tkill to a non-guest tid (was a printf).
- Add tgkill coverage: success on guest tids; ESRCH on a wrong tgid.
--- tests/tcg/multiarch/Makefile.target | 2 + tests/tcg/multiarch/linux/linux-proc-task.c | 193 ++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+)
tests/tcg/multiarch/Makefile.target +2 -0
@@ -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 +193 -0
193 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 1/3 linux-user: Filter /proc/*/task/ to hide QEMU-internal threads
In reply to Ali RazaWhen 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
---
Changes in v2:
- Use pid_t instead of long for the parsed tid in do_getdents() and do_getdents64() (suggested by Helge Deller).
- Replace the ad-hoc readlink-at-getdents check with fd_trans-based tagging at open time, eliminating a TOCTOU race and a broad-prefix match that also matched /proc/<pid>/task/<tid>/fd.
- Retry sys_getdents when an entire host batch is filtered out, so the guest does not see a false EOF.
- Parse the dirent TID with qemu_strtoi() instead of strtol() so out-of-range values cannot wrap silently into pid_t.
--- linux-user/fd-trans.c | 6 + linux-user/fd-trans.h | 18 +++ linux-user/syscall.c | 298 +++++++++++++++++++++++++++++++++++++------------- 3 files changed, 244 insertions(+), 78 deletions(-)
linux-user/fd-trans.c +6 -0
@@ -1881,3 +1881,9 @@ static abi_long host_to_target_data_inotify(void *buf, size_t len)TargetFdTrans target_inotify_trans = { .host_to_target_data = host_to_target_data_inotify, }; + +/* +* No callbacks: this trans entry is purely a marker to identify fds +* that refer to our own /proc/<pid>/task directory. See getdents(). +*/ +TargetFdTrans target_proc_pid_task_trans;
linux-user/fd-trans.h +18 -0
@@ -142,4 +142,22 @@ extern TargetFdTrans target_signalfd_trans;extern TargetFdTrans target_eventfd_trans;extern TargetFdTrans target_timerfd_trans;extern TargetFdTrans target_inotify_trans; + +/* +* Marker (no callbacks) used to tag fds that refer to /proc/<pid>/task +* for our own process. Used by getdents to filter out QEMU-internal +* host threads (RCU, TCG workers) that have no guest CPUState. +*/ +extern TargetFdTrans target_proc_pid_task_trans; + +static inline bool fd_trans_is_proc_pid_task (int fd) +{ +if (fd <0 ) { +return false; + } + +QEMU_LOCK_GUARD (&target_fd_trans_lock); +return fd < target_fd_max + && target_fd_trans[fd] == &target_proc_pid_task_trans; +}#endif
linux-user/syscall.c +220 -78
379 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 2/3] linux-user: Validate tkill/tgkill targets are guest threads
In reply to Ali RazaQuoted 93 lines · 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
-ESRCHfor 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)---
Changes in v2:
- Range-check tid/tgid before narrowing fromabi_longtopid_t.
-do_tkill: only reject signals whose target lives in our own host process and is not a guest thread; cross-process tkill is passed through to the kernel unchanged.
-do_tgkill: drop the previous tgid==getpid() blanket rejection and apply the same in-our-process-only filter, so legitimate cross-process tgkill keeps working.
- Newtid_is_qemu_internal()helper based on the CPU list and /proc/self/task/<tid> presence.--- linux-user/syscall.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)linux-user/syscall.c +41
-2@@ -9161,6 +9161,27 @@ static bool is_guest_tid(pid_t tid)return false; } +/* +* Return true iff @tid identifies a thread inside our own host process +* that is not one of the guest threads -- i.e. a QEMU-internal helper +* thread (RCU, TCG worker, ...). Cross-process tids and unknown tids +* are not classified here and the caller should pass the syscall +* through to the kernel unchanged. +*/ +static bool tid_is_qemu_internal (pid_t tid) +{ +char path[64 ]; + + WITH_RCU_READ_LOCK_GUARD() { +if (is_guest_tid (tid)) { +return false; + } + } + +snprintf (path,sizeof (path),"/proc/self/task/%d" , (int )tid); +return access (path,F_OK ) ==0 ; +} +#ifdef TARGET_NR_getdentsstatic int do_getdents (abi_long dirfd,abi_long arg2,abi_long count) {@@ -13511,11 +13532,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))); + { +pid_t tid = (pid_t )arg1; +if ((abi_long )tid != arg1) { +return -TARGET_ESRCH ; + } +/* +* Reject signals that target one of our own QEMU-internal +* helper threads (RCU, TCG worker, ...) which share our +* host PID but have no guest CPUState. Cross-process tids +* are passed through unchanged. +*/ +if (tid_is_qemu_internal (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, + { +pid_t tgid = (pid_t )arg1; +pid_t tid = (pid_t )arg2; +if ((abi_long )tgid != arg1 || (abi_long )tid != arg2) {
linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 13566
Quoted 10 lines
linux-user/syscall.c +10
-0+return -TARGET_ESRCH ; + } +/* +* Only screen targets in our own process; for cross-process +* tgkill we have no way to know which TIDs are guest threads +* in another QEMU instance, so the call is passed through. +*/ +if (tgid ==getpid () &&tid_is_qemu_internal (tid)) { +return -TARGET_ESRCH ; + }
I was wondering why getpid() is used and not getgid(), but the comment just above made it clear.
linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 13572
Quoted 7 lines
linux-user/syscall.c +2
-0+return get_errno (safe_tgkill (tgid, tid,target_to_host_signal (arg3))); + }#ifdef TARGET_NR_set_robust_listcase TARGET_NR_set_robust_list:
Overall looks good to me.
Re: [PATCH v2 3/3] tests/tcg: Add test for /proc/self/task/ filtering and tkill validation
In reply to Ali RazaQuoted 17 lines · Ali Raza
Add a multiarch linux-user test that verifies:
1.
/proc/self/task/only lists guest threads -- spawnsNUM_THREADSpthreads, 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)
Quoted 9 lines
---
Changes in v2:
- Hard-assert ESRCH on tkill to a non-guest tid (was a printf).
- Add tgkill coverage: success on guest tids; ESRCH on a wrong tgid.--- tests/tcg/multiarch/Makefile.target | 2 + tests/tcg/multiarch/linux/linux-proc-task.c | 193 ++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+)
Re: [PATCH v2 1/3] linux-user: Filter /proc/*/task/ to hide QEMU-internal threads
In reply to Ali RazaQuoted 342 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()anddo_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 viaCPU_FOREACH(), matching the existing pattern used for/proc/self/statnum_threads.This fixes the
psx_testhang 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)---
Changes in v2:
- Usepid_tinstead of long for the parsed tid indo_getdents()anddo_getdents64()(suggested by Helge Deller).
- Replace the ad-hoc readlink-at-getdents check with fd_trans-based tagging at open time, eliminating a TOCTOU race and a broad-prefix match that also matched /proc/<pid>/task/<tid>/fd.
- Retrysys_getdentswhen an entire host batch is filtered out, so the guest does not see a false EOF.
- Parse the dirent TID withqemu_strtoi()instead ofstrtol()so out-of-range values cannot wrap silently intopid_t.--- linux-user/fd-trans.c | 6 + linux-user/fd-trans.h | 18 +++ linux-user/syscall.c | 298 +++++++++++++++++++++++++++++++++++++------------- 3 files changed, 244 insertions(+), 78 deletions(-)linux-user/fd-trans.c +6
-0@@ -1881,3 +1881,9 @@ static abi_long host_to_target_data_inotify(void *buf, size_t len)TargetFdTrans target_inotify_trans = { .host_to_target_data = host_to_target_data_inotify, }; + +/* +* No callbacks: this trans entry is purely a marker to identify fds +* that refer to our own /proc/<pid>/task directory. See getdents(). +*/ +TargetFdTrans target_proc_pid_task_trans;linux-user/fd-trans.h +18
-0@@ -142,4 +142,22 @@ extern TargetFdTrans target_signalfd_trans;extern TargetFdTrans target_eventfd_trans;extern TargetFdTrans target_timerfd_trans;extern TargetFdTrans target_inotify_trans; + +/* +* Marker (no callbacks) used to tag fds that refer to /proc/<pid>/task +* for our own process. Used by getdents to filter out QEMU-internal +* host threads (RCU, TCG workers) that have no guest CPUState. +*/ +extern TargetFdTrans target_proc_pid_task_trans; + +static inline bool fd_trans_is_proc_pid_task (int fd) +{ +if (fd <0 ) { +return false; + } + +QEMU_LOCK_GUARD (&target_fd_trans_lock); +return fd < target_fd_max + && target_fd_trans[fd] == &target_proc_pid_task_trans; +}#endif linux-user/syscall.c +146
-52@@ -8870,6 +8870,7 @@ static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, }fd_trans_unregister (ret); +maybe_tag_proc_pid_task (ret);unlock_user (pathname, guest_pathname,0 );return ret; }@@ -9108,6 +9109,58 @@ static int host_to_target_cpu_mask(const unsigned long *host_mask,return 0 ; } +/* +* If the freshly-opened host fd refers to our own /proc/<pid>/task +* directory, register a marker on it via fd_trans so getdents() can +* later filter out QEMU-internal host threads (RCU, TCG workers) that +* have no guest CPUState. Tagging at open time (rather than at +* getdents time) avoids TOCTOU on the fd's resolved path and lets us +* piggy-back on the existing fd_trans infrastructure for dup/close. +*/ +static void maybe_tag_proc_pid_task (int fd) +{ +char link_path[64 ]; +char link_target[PATH_MAX ]; +char expected[64 ]; +ssize_t len; +int expected_len; + +if (fd <0 ) { +return ; + } + +snprintf (link_path,sizeof (link_path),"/proc/self/fd/%d" , fd); + len =readlink (link_path, link_target,sizeof (link_target) -1 ); +if (len <0 ) { +return ; + } + link_target[len] ='\0' ; + + expected_len =snprintf (expected,sizeof (expected), +"/proc/%d/task" ,getpid ()); +if (expected_len >0 && (ssize_t )expected_len == len + &&memcmp (link_target, expected, expected_len) ==0 ) { +fd_trans_register (fd, &target_proc_pid_task_trans); + } +} + +/* +* 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_getdentsstatic int do_getdents (abi_long dirfd,abi_long arg2,abi_long count) {@@ -9116,78 +9169,123 @@ 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 =fd_trans_is_proc_pid_task (dirfd); +bool stop = false; hdirp =g_try_malloc (count);if (!hdirp) {return -TARGET_ENOMEM ; } -#ifdef EMULATE_GETDENTS_WITH_GETDENTS - hlen =sys_getdents (dirfd, hdirp, count); -#else - hlen =sys_getdents64 (dirfd, hdirp, count); -#endif - - hlen =get_errno (hlen); -if (is_error (hlen)) { -return hlen; - } - tdirp =lock_user (VERIFY_WRITE , arg2, count,0 );if (!tdirp) {return -TARGET_EFAULT ; } -for (hoff = toff =0 ; hoff < hlen; hoff += hreclen, toff += treclen) { + toff =0 ; +/* +* Loop until we either produce a visible target entry, hit a +* real EOF (host returns 0), or hit an error. Without this +* retry, a host batch consisting entirely of filtered entries +* would be reported to the guest as EOF. +*/ +while (!stop) {#ifdef EMULATE_GETDENTS_WITH_GETDENTS -struct linux_dirent *hde = hdirp + hoff; + hlen =sys_getdents (dirfd, hdirp, count);#else -struct linux_dirent64 *hde = hdirp + hoff; + hlen =sys_getdents64 (dirfd, hdirp, count);#endif -struct target_dirent *tde = tdirp + toff; -int namelen; -uint8_t type; + hlen =get_errno (hlen); +if (is_error (hlen)) { +unlock_user (tdirp, arg2,0 ); +return hlen; + } +if (hlen ==0 ) { +break ;/* real EOF */ + } - namelen =strlen (hde->d_name); - hreclen = hde->d_reclen; - treclen = offsetof(struct target_dirent , d_name) + namelen +2 ; - treclen =QEMU_ALIGN_UP (treclen, __alignof(struct target_dirent )); +for (hoff =0 ; hoff < hlen; hoff += hreclen, toff += treclen) { +#ifdef EMULATE_GETDENTS_WITH_GETDENTS +struct linux_dirent *hde = hdirp + hoff; +#else +struct linux_dirent64 *hde = hdirp + hoff; +#endif +struct target_dirent *tde = tdirp + toff; +int namelen; +uint8_t type; + + namelen =strlen (hde->d_name); + hreclen = hde->d_reclen; -if (toff + treclen > count) {/* -* If the host struct is smaller than the target struct, or -* requires less alignment and thus packs into less space, -* then the host can return more entries than we can pass -* on to the guest. +* Filter /proc/<pid>/task/ listings to hide QEMU-internal +* host threads (RCU, TCG) that have no guest CPUState. */ -if (toff ==0 ) { - toff = -TARGET_EINVAL ;/* result buffer is too small */ +if (filter_task) { +int tid_int; +if (qemu_strtoi (hde->d_name,NULL ,10 , &tid_int) ==0 + && tid_int >0 ) { +pid_t tid = (pid_t )tid_int; +bool drop; + + WITH_RCU_READ_LOCK_GUARD() { + drop = !is_guest_tid (tid); + } +if (drop) { + treclen =0 ; +continue ; + } + } + } + treclen = offsetof(struct target_dirent , d_name) + namelen +2 ; + treclen =QEMU_ALIGN_UP (treclen, __alignof(struct target_dirent )); + +if (toff + treclen > count) { +/* +* If the host struct is smaller than the target struct, or +* requires less alignment and thus packs into less space, +* then the host can return more entries than we can pass +* on to the guest. +*/ +if (toff ==0 ) { + toff = -TARGET_EINVAL ;/* result buffer is too small */ + stop = true; +break ; + } +/* +* Return what we have, resetting the file pointer to the +* location of the first record not returned. +*/ +lseek (dirfd, prev_diroff,SEEK_SET ); + stop = true;break ; } + + prev_diroff = hde->d_off; + tde->d_ino =tswapal (hde->d_ino); + tde->d_off =tswapal (hde->d_off); + tde->d_reclen =tswap16 (treclen); +memcpy (tde->d_name, hde->d_name, namelen +1 ); +/* -* Return what we have, resetting the file pointer to the -* location of the first record not returned. +* The getdents type is in what was formerly a padding byte at the +* end of the structure. */ -lseek (dirfd, prev_diroff,SEEK_SET ); -break ; +#ifdef EMULATE_GETDENTS_WITH_GETDENTS + type = *((uint8_t *)hde + hreclen -1 ); +#else + type = hde->d_type; +#endif + *((uint8_t *)tde + treclen -1 ) = type; } - prev_diroff = hde->d_off; - tde->d_ino =tswapal (hde->d_ino); - tde->d_off =tswapal (hde->d_off); - tde->d_reclen =tswap16 (treclen); -memcpy (tde->d_name, hde->d_name, namelen +1 ); -/* -* The getdents type is in what was formerly a padding byte at the -* end of the structure. +* If we produced any visible target entry, return; otherwise +* loop again to fetch more host entries (avoid false EOF). */ -#ifdef EMULATE_GETDENTS_WITH_GETDENTS - type = *((uint8_t *)hde + hreclen -1 ); -#else - type = hde->d_type; -#endif - *((uint8_t *)tde + treclen -1 ) = type; +if (toff !=0 ) { +break ; + } }unlock_user (tdirp, arg2, toff);@@ -9203,57 +9301,99 @@ 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 =fd_trans_is_proc_pid_task (dirfd); +bool stop = false; hdirp =g_try_malloc (count);if (!hdirp) {return -TARGET_ENOMEM ; } - hlen =get_errno (sys_getdents64 (dirfd, hdirp, count)); -if (is_error (hlen)) { -return hlen; - } - tdirp =lock_user (VERIFY_WRITE , arg2, count,0 );if (!tdirp) {return -TARGET_EFAULT ; } -for (hoff = toff =0 ; hoff < hlen; hoff += hreclen, toff += treclen) {
linux-user/syscall.c static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count) · line 9323
Quoted 10 lines
linux-user/syscall.c +7
-3-struct linux_dirent64 *hde = hdirp + hoff; -struct target_dirent64 *tde = tdirp + toff; -int namelen; + toff =0 ; +/* +* Loop until we either produce a visible target entry, hit a +* real EOF (host returns 0), or hit an error. Without this +* retry, a host batch consisting entirely of filtered entries +* would be reported to the guest as EOF. +*/
It seems like this patch does two different things:
- fix issue when all entries are filtered
- add mechanic to filter non-guest threads
It would be more reviewable to implement this in two different patches.
Quoted 109 lines
linux-user/syscall.c +66
-23+while (!stop) { + hlen =get_errno (sys_getdents64 (dirfd, hdirp, count)); +if (is_error (hlen)) { +unlock_user (tdirp, arg2,0 ); +return hlen; + } +if (hlen ==0 ) { +break ;/* real EOF */ + } + +for (hoff =0 ; hoff < hlen; hoff += hreclen, toff += treclen) { +struct linux_dirent64 *hde = hdirp + hoff; +struct target_dirent64 *tde = tdirp + toff; +int namelen; - namelen =strlen (hde->d_name) +1 ; - hreclen = hde->d_reclen; - treclen = offsetof(struct target_dirent64 , d_name) + namelen; - treclen =QEMU_ALIGN_UP (treclen, __alignof(struct target_dirent64 )); + namelen =strlen (hde->d_name) +1 ; + hreclen = hde->d_reclen; -if (toff + treclen > count) {/* -* If the host struct is smaller than the target struct, or -* requires less alignment and thus packs into less space, -* then the host can return more entries than we can pass -* on to the guest. +* Filter /proc/<pid>/task/ listings to hide QEMU-internal +* host threads (RCU, TCG) that have no guest CPUState. */ -if (toff ==0 ) { - toff = -TARGET_EINVAL ;/* result buffer is too small */ +if (filter_task) { +int tid_int; +if (qemu_strtoi (hde->d_name,NULL ,10 , &tid_int) ==0 + && tid_int >0 ) { +pid_t tid = (pid_t )tid_int; +bool drop; + + WITH_RCU_READ_LOCK_GUARD() { + drop = !is_guest_tid (tid); + } +if (drop) { + treclen =0 ; +continue ; + } + } + } + treclen = offsetof(struct target_dirent64 , d_name) + namelen; + treclen =QEMU_ALIGN_UP (treclen, __alignof(struct target_dirent64 )); + +if (toff + treclen > count) { +/* +* If the host struct is smaller than the target struct, or +* requires less alignment and thus packs into less space, +* then the host can return more entries than we can pass +* on to the guest. +*/ +if (toff ==0 ) { + toff = -TARGET_EINVAL ;/* result buffer is too small */ + stop = true; +break ; + } +/* +* Return what we have, resetting the file pointer to the +* location of the first record not returned. +*/ +lseek (dirfd, prev_diroff,SEEK_SET ); + stop = true;break ; } -/* -* Return what we have, resetting the file pointer to the -* location of the first record not returned. -*/ -lseek (dirfd, prev_diroff,SEEK_SET ); -break ; + + prev_diroff = hde->d_off; + tde->d_ino =tswap64 (hde->d_ino); + tde->d_off =tswap64 (hde->d_off); + tde->d_reclen =tswap16 (treclen); + tde->d_type = hde->d_type; +memcpy (tde->d_name, hde->d_name, namelen); } - prev_diroff = hde->d_off; - tde->d_ino =tswap64 (hde->d_ino); - tde->d_off =tswap64 (hde->d_off); - tde->d_reclen =tswap16 (treclen); - tde->d_type = hde->d_type; -memcpy (tde->d_name, hde->d_name, namelen); +if (toff !=0 ) { +break ; + } }unlock_user (tdirp, arg2, toff);@@ -9732,6 +9872,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,target_to_host_bitmask (arg2, fcntl_flags_tbl), arg3, true));fd_trans_unregister (ret); +maybe_tag_proc_pid_task (ret);unlock_user (p, arg1,0 );return ret;#endif
linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 9889
Quoted 7 lines
linux-user/syscall.c +1
-0@@ -9742,6 +9883,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,target_to_host_bitmask (arg3, fcntl_flags_tbl), arg4, true));fd_trans_unregister (ret); +maybe_tag_proc_pid_task (ret);unlock_user (p, arg2,0 );return ret;case TARGET_NR_openat2:
Else, the logic of the patch seems correct to me, tagging fd for /proc/*/task and reusing this information for getdents* to decide to filter a specific entry or not.
I guess it does not protect from opening directly a specific thread file, but there is no reason someone knows the id without performing a getdents first.
Regards,
Pierrick
Re: [PATCH v2 1/3] linux-user: Filter /proc/*/task/ to hide QEMU-internal threads
Quoted 5 lines · Pierrick Bouvier
It seems like this patch does two different things:
- fix issue when all entries are filtered
- add mechanic to filter non-guest threadsIt would be more reviewable to implement this in two different patches.
Good point. I will split this in v3:
patch 1: refactor do_getdents()/do_getdents64() to wrap the body in
an outer retry loop. Pure refactor with no functional change (without a filter installed, the inner loop always produces an entry per non-EOF host batch, so the outer loop exits on the first iteration and behavior is identical).
patch 2: add the fd_trans tag for /proc/<pid>/task and the
actual entry filter.
Quoted 3 lines
I guess it does not protect from opening directly a specific thread file, but there is no reason someone knows the id without performing a getdents first.
Correct, and this is intentional. The libcap PSX deadlock we are fixing only happens via /proc/self/task/ enumeration, so closing the direct-open path is not required to fix the bug. It would also be hard to do soundly: a guest can already obtain a non-guest TID via /proc/self/status (Tgid/PPid), gettid() in a host signal handler, etc., and blanket-filtering those would break legitimate uses. Happy to revisit if you can point me at a concrete attack/correctness path that direct open would expose.
Thanks for the review,
Ali
Re: [PATCH v2 1/3] linux-user: Filter /proc/*/task/ to hide QEMU-internal threads
Quoted 22 lines · Ali Raza
Quoted 5 lines · Pierrick Bouvier
It seems like this patch does two different things:
- fix issue when all entries are filtered
- add mechanic to filter non-guest threadsIt would be more reviewable to implement this in two different patches.
Good point. I will split this in v3:
patch 1: refactor do_getdents()/do_getdents64() to wrap the body in an outer retry loop. Pure refactor with no functional change (without a filter installed, the inner loop always produces an entry per non-EOF host batch, so the outer loop exits on the first iteration and behavior is identical).
patch 2: add the
fd_transtag for /proc/<pid>/task and the actual entry filter.Quoted 3 lines
I guess it does not protect from opening directly a specific thread file, but there is no reason someone knows the id without performing a getdents first.
Quoted 9 lines
Correct, and this is intentional. The libcap PSX deadlock we are fixing only happens via
/proc/self/task/enumeration, so closing the direct-open path is not required to fix the bug. It would also be hard to do soundly: a guest can already obtain a non-guest TID via/proc/self/status(Tgid/PPid),gettid()in a host signal handler, etc., and blanket-filtering those would break legitimate uses. Happy to revisit if you can point me at a concrete attack/correctness path that direct open would expose.
Yes I don't think it's worth solving this as long as a real world use case doesn't require this kind of filtering.
Quoted 2 lines
Thanks for the review,
Ali