Skip to contentSkip to conversation

linux-user/i386: reject bad CS/SS selectors on rt_sigreturn

PATCH Superseded takano32 2 participants 7 messages Oct 20 – Oct 24

lore
Share & tools
lore

takano32

author
#0 lore raw

PATCH 1/1 linux-user/i386: reject bad CS/SS selectors on rt_sigreturn

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).

---
 linux-user/i386/signal.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
linux-user/i386/signal.c +26 -2
index 9650fac940..01d54be310 100644
@@ -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);
Signature · 1 line
2.53.0
#1 lore raw

Hi Takano32,

Quoted 30 lines · On 10/20/26 16:44, takano32 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
     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.

Quoted 7 lines

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).

Can you please provide me with your real name when signing patches?

Anyone able to review this patch?

Helge

Quoted 43 lines
---
  linux-user/i386/signal.c | 28 ++++++++++++++++++++++++++--
  1 file changed, 26 insertions(+), 2 deletions(-)
linux-user/i386/signal.c +26 -2
index 9650fac940..01d54be310 100644
@@ -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 lore raw

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

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).

---
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(-)
linux-user/i386/signal.c +26 -2
index 9650fac940..01d54be310 100644
@@ -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);
Signature · 1 line
2.53.0
#3 lore raw

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

#4 lore raw
In reply to TAKANO Mitsuhiro
Quoted 5 lines · TAKANO Mitsuhiro

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

Quoted 8 lines
     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....

Quoted 23 lines
     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).

Quoted 4 lines

---
v2:
- author and Signed-off-by now carry my real name (TAKANO Mitsuhiro)

Thanks for fixing!

Helge

#5 lore raw
In reply to TAKANO Mitsuhiro

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

#6 lore raw
Quoted 15 lines · TAKANO Mitsuhiro

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.

Quoted 6 lines

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