Skip to contentSkip to conversation

x86/entry/32: Harden return-to-user

PATCH In review Andrey Zakharov 3 participants 6 messages Oct 27 – Oct 31

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/entry/32: Harden return-to-user

Commit 1e4d3001f59f ("x86/entry: Harden return-to-user") made the check that the IRET frame returns to user mode unconditional on x86-64. The 32-bit entry code has no equivalent: restore_all_switch_stack executes IRET with whatever CS is in the frame. The exception return path tests CS to choose between returning to the kernel and to user mode, but the system call return paths (int $0x80, the SYSENTER fallback and ret_from_fork) do not test it before reaching restore_all_switch_stack.

Add an unconditional check to restore_all_switch_stack, after the registers are restored. It requires that CS in the IRET frame has a user RPL, or that EFLAGS.VM is set for vm86, and traps with UD2 otherwise. It is placed before CLEAR_CPU_BUFFERS so that the buffer clearing stays directly in front of the IRET. The exception return path to the kernel jumps straight to .Lirq_return and does not go through the new check.

Testing: built i386_defconfig plus CONFIG_X86_LEGACY_VM86=y with gcc 16.2.1; the build reported no warnings. Booted it under QEMU (TCG, -cpu max) with a minimal initramfs running the entry_from_vm86, sigreturn, single_step_syscall and syscall_nt selftests. The results match the unpatched kernel, ignoring the FLAGS values printed by entry_from_vm86, which also differ between two runs of the unpatched kernel. entry_from_vm86 reports a UMIP failure on both kernels. As negative controls, making the RPL test always fail kills init with UD2 at the first return to user space, and removing the EFLAGS.VM exception makes entry_from_vm86 hit the UD2.

Not tested: real hardware, performance, the NMI and double fault exits, builds with PTI enabled, and builds with CONFIG_X86_LEGACY_VM86 disabled.

---
 arch/x86/entry/entry_32.S | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/entry/entry_32.S +8 -0
index 92c0b4a94e0a..69f754e5657c 100644
@@ -952,6 +952,14 @@ restore_all_switch_stack:
 
 	/* Restore user state */
 	RESTORE_REGS pop=4			# skip orig_eax/error_code
+
+	/* Assert that the IRET frame returns to user mode or vm86. */
+	testb	$USER_SEGMENT_RPL_MASK, 4(%esp)
+	jnz	.Lreturn_to_user_ok
+	testl	$X86_EFLAGS_VM, 8(%esp)
+	jnz	.Lreturn_to_user_ok
+	ud2
+.Lreturn_to_user_ok:
 	CLEAR_CPU_BUFFERS
 .Lirq_return:
 	/*
Signature · 1 line
2.56.0
#1 lore raw
Quoted 4 lines · Andrey Zakharov
arch/x86/entry/entry_32.S +0 -0
@@ -952,6 +952,14 @@ restore_all_switch_stack:
 

arch/x86/entry/entry_32.S restore_all_switch_stack: · line 962

Quoted 10 lines
arch/x86/entry/entry_32.S +8 -0
 	/* Restore user state */
 	RESTORE_REGS pop=4			# skip orig_eax/error_code
+
+	/* Assert that the IRET frame returns to user mode or vm86. */
+	testb	$USER_SEGMENT_RPL_MASK, 4(%esp)
+	jnz	.Lreturn_to_user_ok
+	testl	$X86_EFLAGS_VM, 8(%esp)
+	jnz	.Lreturn_to_user_ok
+	ud2
+.Lreturn_to_user_ok:

I'm curious who cares.

Is anyone who is actually still running a 32-bit kernel concerned about this kind of hardening?

If no one cares, then I'd rather pass on the complexity, even four instructions of it.

author
#2 lore raw

From: Andrey Zakharov <[email protected]>

Quoted 7 lines · Dave Hansen

I'm curious who cares.

Is anyone who is actually still running a 32-bit kernel concerned about this kind of hardening?

If no one cares, then I'd rather pass on the complexity, even four instructions of it.

I don't know of anyone. The only motivation was KSPP issue #41, which asks for parity with the x86-64 check added by 1e4d3001f59f, and that issue is marked as low priority because 32-bit is legacy. I have no report from a 32-bit user asking for it.

Please drop the patch. Thanks for looking at it.

If you have a moment: this is my first work on arch/x86, and I would like to contribute something that is actually useful. Is there a small task in x86/entry or in kernel hardening that you would suggest as a better starting point, or a place where help with testing or review would be welcome? I can build and run kernels under QEMU.

Thanks,
Andrey Zakharov

maintainer
#3 lore raw
Quoted 5 lines · On Wed, Oct 28, 2026 at 10:21:39PM +0300, s1nkhole wrote:

If you have a moment: this is my first work on arch/x86, and I would like to contribute something that is actually useful. Is there a small task in x86/entry or in kernel hardening that you would suggest as a better starting point, or a place where help with testing or review would be welcome? I can build and run kernels under QEMU.

Review and testing is always welcome. As you can see, we're drowning in patches. What we're not drowning in is reviewers who poke at submitted patches critically and ask nasty questions.

And nowadays, reviewers even have help from Sashiko's LLM so doing that should be even easier.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw
In reply to s1nkhole
Quoted 5 lines · On 10/28/26 12:21, s1nkhole wrote:

If you have a moment: this is my first work on arch/x86, and I would like to contribute something that is actually useful. Is there a small task in x86/entry or in kernel hardening that you would suggest as a better starting point, or a place where help with testing or review would be welcome? I can build and run kernels under QEMU.

It's not small, but kernel shadow stacks are an _incredibly_ powerful hardening tool. You can buy a system off the shelf that sanely supports them (including FRED), but the kernel doesn't support them yet.

My current work in progress is here:

Quoted 1 line

https://git.kernel.org/pub/scm/linux/kernel/git/daveh/devel.git/log/?h=kss-2026-10-23

Another set of eyes on that would be nice. Although I do think I'm going to abandon tacking the shadow stack on to the thread_union for now. So I don't suggest looking at it too deeply until I get another version out the door.

One thing you could do today is go into the list in here:

Quoted 1 line

https://git.kernel.org/pub/scm/linux/kernel/git/daveh/devel.git/commit/?h=kss-2026-10-21&id=4dab5fe58904ee07b3622f25bbe3c1b7ff820bf5

and pick a feature or two and figure out if they need fixing for shadow stacks or not, and if so what a fix might look like.

Or another set of eyes on this:

Quoted 1 line

https://git.kernel.org/pub/scm/linux/kernel/git/daveh/devel.git/log/?h=taskfun-2026-10-26

It's ready to post but I'm just waiting for the merge window to close.

#5 lore raw

Hi Dave,

Thanks for the pointers. I looked at the list in 4dab5fe58904 and at kss-2026-10-23 (b6fa923e47fc). This is code reading and builds only: I have no FRED hardware, so nothing below was run.

Build problems in kss-2026-10-23:

- "make defconfig" (X86_CET=y via X86_KERNEL_IBT, X86_USER_SHADOW_STACK unset) fails in arch/x86/kernel/cet.c with gcc 16.2.1:
:277 no previous prototype for 'kshstk_build'
:330 redefinition of 'arch_kernel_stack_lock_current' :351 'struct thread_struct' has no member named 'kshstk_ssp' :351 implicit declaration of 'kshstk_enable_current'
cet.c has no #ifdef, while the declarations in asm/shstk.h sit under CONFIG_X86_USER_SHADOW_STACK. Only cet.o was built for defconfig.
- With USER and KERN shadow stacks both =y, vmlinux.o gets an objtool
warning: kshstk_enable_current+0x3: call to kshstk_build() leaves .noinstr.text section (it is in .entry.text, kshstk_build is in .text).

A question: PUSH/POP_KERNEL_EVENT_SSP in entry_64_fred.S are under CONFIG_X86_USER_SHADOW_STACK, but regs_set_return_ip() depends on X86_FEATURE_KERN_SHSTK. With KERN=y and USER=n no slot would be pushed, and regs_set_return_ip() would read the word below pt_regs. Should the macros depend on X86_KERN_SHADOW_STACK? That config does not build today, so this is from reading only.

Direct writes to regs->ip in kernel-event paths, as far as I read them (as I understand it, ERETS compares the shadow stack LIP with RIP on the regular stack; please correct me if that is wrong):

- traps.c, handle_bug(): regs->ip += ud_len or = addr, so every handled WARN() goes through it.
- alternative.c, FineIBT decoding (two places): regs->ip = *target.
- text-patching.h, int3_emulate_jmp(), used by int3_emulate_call/ret; callers are in smp_text_poke_int3_handler() (ret, call, jmp, jcc). int3_emulate_push/pop only touch regs->sp and the regular stack. Emulation is reached from jump_label.c (JMP; NOP5 is emulated as JMP32+0), ftrace.c (CALL/JMP32) and static_call.c.
- kgdb: kgdb_arch_set_pc(), the 'c'/'s' commands with an address, kgdb_skipexception(), and dbg_set_reg(), which memcpy()s into pt_regs (ip, cs, flags) through dbg_reg_def.

smp_text_poke_int3_handler() is noinstr and must not call out. cet.c has no noinstr/notrace and is not removed from -pg in the Makefile, so calling regs_set_return_ip() from there as it is looks unsafe.

I found no caller of arch_kernel_stack_lock_current() yet. If it ends up before alternative_instructions(), int3_selftest() would run an emulated call with shadow stacks on.

Not checked: runtime behaviour, whether these paths are reachable in practice, other writers of regs->ip (tdx, vc-shared, mm/fault.c were not read).

Thanks,
Andrey