[PATCH v2] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn

TAKANO Mitsuhiro posted 1 patch 4 weeks, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261024134118.474313-1-takano32@gmail.com
Maintainers: Laurent Vivier <laurent@vivier.eu>, Helge Deller <deller@gmx.de>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
linux-user/i386/signal.c | 28 ++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
[PATCH v2] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn
Posted by TAKANO Mitsuhiro 4 weeks, 1 day ago
restore_sigcontext() handed cs/ss straight to cpu_x86_load_seg(). In
protected mode that ends in helper_load_seg(), which raises #GP for an
invalid selector and calls cpu_loop_exit_restore() -- a longjmp out of
the syscall handler. Landing in cpu_exec() that way trips

    g_assert(cpu == current_cpu)

in cpu_exec_longjmp_cleanup() and aborts the whole emulator.

A guest that builds its own signal frame and executes rt_sigreturn
(SROP) leaves cs/ss zeroed, so this is easy to hit:

    $ qemu-x86_64 ./hello_srop < payload     # frame with cs=0, ss=0
    ERROR:accel/tcg/cpu-exec.c:515:cpu_exec_longjmp_cleanup:
      assertion failed: (cpu == current_cpu)
    Aborted

x86 Linux does not validate the selectors either: restore_sigcontext()
only does

    regs->cs = sc.cs | 0x03;
    regs->ss = sc.ss | 0x03;

and a bogus value matters only on the way back to user mode, where it
costs the task and not the kernel.  QEMU cannot be that casual, because
the selector goes through cpu_x86_load_seg() while the syscall handler
is still running.  So take the existing badframe path instead: accept
the selectors the CPU is already running with and return false for
anything else, which makes do_rt_sigreturn() raise SIGSEGV in the guest.

Tested with an x86_64 guest on an aarch64 host, against qemu master plus
this patch: a frame with cs=0x33, ss=0x2b restores and exits with the
requested code; a frame with cs=0, ss=0 now raises SIGSEGV in the guest
instead of aborting qemu (unpatched qemu 10.2.1 aborts with the
assertion above).

Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
---
v2:
 - author and Signed-off-by now carry my real name (TAKANO Mitsuhiro)
 - changelog: x86 Linux does not validate the selectors either, it only
   restores them into the return frame; describe the QEMU-side longjmp
   instead of claiming that the kernel rejects the frame
 linux-user/i386/signal.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/linux-user/i386/signal.c b/linux-user/i386/signal.c
index 9650fac940..01d54be310 100644
--- a/linux-user/i386/signal.c
+++ b/linux-user/i386/signal.c
@@ -775,8 +775,32 @@ static bool restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc)
     env->eip = tswapl(sc->rip);
 #endif
 
-    cpu_x86_load_seg(env, R_CS, lduw_le_p(&sc->cs) | 3);
-    cpu_x86_load_seg(env, R_SS, lduw_le_p(&sc->ss) | 3);
+    /*
+     * Validate the selectors before loading them.
+     *
+     * In protected mode cpu_x86_load_seg() ends up in helper_load_seg(),
+     * which raises #GP for a bad selector and calls cpu_loop_exit_restore().
+     * That longjmps out of the syscall handler while cpu_exec_longjmp_cleanup()
+     * still expects the CPU it started with, so
+     *
+     *     g_assert(cpu == current_cpu)
+     *
+     * fires and the whole emulator aborts.  A guest that builds its own
+     * signal frame and executes rt_sigreturn (SROP) leaves cs/ss zeroed,
+     * so this is trivial to hit.  Linux treats such a frame as a bad frame
+     * and delivers SIGSEGV, so do the same and take the badframe path.
+     */
+    {
+        uint16_t cs = lduw_le_p(&sc->cs) | 3;
+        uint16_t ss = lduw_le_p(&sc->ss) | 3;
+
+        if (cs != (env->segs[R_CS].selector | 3) ||
+            ss != (env->segs[R_SS].selector | 3)) {
+            return false;
+        }
+        cpu_x86_load_seg(env, R_CS, cs);
+        cpu_x86_load_seg(env, R_SS, ss);
+    }
 
     tmpflags = tswapl(sc->eflags);
     env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
-- 
2.53.0
Re: [PATCH v2] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn
Posted by TAKANO Mitsuhiro 4 weeks, 1 day ago
Hi Helge,

please drop this patch -- it is not needed on master, and I should have
checked that before posting.

The abort reproduces on 10.2.1, but not on master: since

  9097087147 ("linux-user: make syscall emulation interruptible")

a #GP raised while a syscall is being emulated no longer longjmps out of
cpu_exec(), so a frame with zeroed CS/SS already ends up as SIGSEGV in the
guest and qemu survives.  I checked this on a clean master build
(8967b2fe1b): the same frame exits 0 with the SIGSEGV handler running,
while a v10.2.1 build aborts with the cpu == current_cpu assertion.

The same holds for the v1 of this series -- please disregard both.

So the fix for 10.2.x is a backport of 9097087147, not this patch, and for
master there is nothing to fix.

Sorry for the noise, and thanks for catching the name.

Takano
Re: [PATCH v2] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn
Posted by Helge Deller 4 weeks, 1 day ago
On 10/24/26 17:13, TAKANO Mitsuhiro wrote:
> Hi Helge,
> 
> please drop this patch -- it is not needed on master, and I should have
> checked that before posting.
> 
> The abort reproduces on 10.2.1, but not on master: since
> 
>    9097087147 ("linux-user: make syscall emulation interruptible")
> 
> a #GP raised while a syscall is being emulated no longer longjmps out of
> cpu_exec(), so a frame with zeroed CS/SS already ends up as SIGSEGV in the
> guest and qemu survives.  I checked this on a clean master build
> (8967b2fe1b): the same frame exits 0 with the SIGSEGV handler running,
> while a v10.2.1 build aborts with the cpu == current_cpu assertion.
> 
> The same holds for the v1 of this series -- please disregard both.
> 
> So the fix for 10.2.x is a backport of 9097087147, not this patch, and for
> master there is nothing to fix.
> 
> Sorry for the noise, and thanks for catching the name.
Ok, patch dropped now.

Helge
Re: [PATCH v2] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn
Posted by Helge Deller 4 weeks, 1 day ago
On 10/24/26 15:41, TAKANO Mitsuhiro wrote:
> restore_sigcontext() handed cs/ss straight to cpu_x86_load_seg(). In
> protected mode that ends in helper_load_seg(), which raises #GP for an
> invalid selector and calls cpu_loop_exit_restore() -- a longjmp out of
> the syscall handler. Landing in cpu_exec() that way trips
> 
>      g_assert(cpu == current_cpu)
> 
> in cpu_exec_longjmp_cleanup() and aborts the whole emulator.
> 
> A guest that builds its own signal frame and executes rt_sigreturn
> (SROP) leaves cs/ss zeroed, so this is easy to hit:
> 
>      $ qemu-x86_64 ./hello_srop < payload     # frame with cs=0, ss=0

Is this "hello_srop" program from some specific testcase, where people
could test themselves? E.g. can it be found in some repository?
If so, please explain where it comes from....


>      ERROR:accel/tcg/cpu-exec.c:515:cpu_exec_longjmp_cleanup:
>        assertion failed: (cpu == current_cpu)
>      Aborted
> 
> x86 Linux does not validate the selectors either: restore_sigcontext()
> only does
> 
>      regs->cs = sc.cs | 0x03;
>      regs->ss = sc.ss | 0x03;
> 
> and a bogus value matters only on the way back to user mode, where it
> costs the task and not the kernel.  QEMU cannot be that casual, because
> the selector goes through cpu_x86_load_seg() while the syscall handler
> is still running.  So take the existing badframe path instead: accept
> the selectors the CPU is already running with and return false for
> anything else, which makes do_rt_sigreturn() raise SIGSEGV in the guest.
> 
> Tested with an x86_64 guest on an aarch64 host, against qemu master plus
> this patch: a frame with cs=0x33, ss=0x2b restores and exits with the
> requested code; a frame with cs=0, ss=0 now raises SIGSEGV in the guest
> instead of aborting qemu (unpatched qemu 10.2.1 aborts with the
> assertion above).
> 
> Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
> ---
> v2:
>   - author and Signed-off-by now carry my real name (TAKANO Mitsuhiro)

Thanks for fixing!

Helge
Re: [PATCH v2] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn
Posted by TAKANO Mitsuhiro 4 weeks, 1 day ago
Sorry for the silence — I was not watching the list, so the reply to my
first posting sat unnoticed.

My name is TAKANO Mitsuhiro, and the author and Signed-off-by lines of
the v2 patch now carry it.

Thanks,
Takano