Skip to contentSkip to conversation

x86: dovetail: Notify the core before emulating UMIP and vsyscalls on #GP

PATCH New Jan Kiszka 1 participant 1 message Nov 12 x86

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 x86: dovetail: Notify the core before emulating UMIP and vsyscalls on #GP

From: Jan Kiszka <[email protected]>

exc_general_protection() runs the user mode fixups before notifying the companion core about the trap, so they run on the oob stage when an oob thread takes the #GP. Two of them cannot run from there:

- fixup_umip_exception() takes mm->context.ldt_usr_sem, a sleeping lock, to emulate SLDT. If the result cannot be written to a memory operand, e.g. because it lies outside of the user address range, which copy_to_user() rejects without faulting, the emulation sends SIGSEGV via force_sig_fault(), taking the in-band siglock. The core is never told about the trap, the thread returns to user mode on the oob stage and re-executes the instruction. If the write faults instead, the core gets notified about the nested page fault rather than the #GP.

- emulate_vsyscall_gp(), which handles vsyscalls on CPUs with LASS, runs the seccomp filters, sends signals and invokes the gettimeofday, time and getcpu syscall handlers. gettimeofday reads the timekeeper under its seqcount, whose in-band writer may have been preempted by the oob stage on the same CPU, so the reader may spin forever.

Without LASS, vsyscalls trap via #PF, and do_user_addr_fault() already notifies the core before emulating them.

Notify the core before the UMIP and vsyscall emulations. A thread using these instructions or vsyscalls from the oob stage is now demoted, as for any other trap requiring in-band handling.

The vDSO fixup only redirects the register frame and stays ahead of the notification. So does the iopl fixup, which turns CLI/STI into NOPs for tasks which called iopl(3): notifying first would demote such a thread on every CLI/STI it executes.

Assisted-by opencode:Opus-5.5

---

New day, new LLM patch. The reasoning about what could go wrong from OOB while emulating makes sense to me. I'm also not seeing any regressions that could be caused by migrating earlier, enforcing inband - no legitimate OOB call path should have taken this route so far.

 arch/x86/kernel/traps.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/kernel/traps.c +13 -5
index a082670f0e1f8..26a30aa31960d 100644
@@ -1030,14 +1030,22 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 		if (fixup_vdso_exception(regs, X86_TRAP_GP, error_code, 0))
 			goto exit;
 
-		if (fixup_umip_exception(regs))
+		/*
+		 * Dovetail: the UMIP and vsyscall emulations may take
+		 * sleeping locks, run syscalls and send signals, which
+		 * the oob stage cannot do. Have the companion core
+		 * demote the current context first, like
+		 * do_user_addr_fault() does before emulating a
+		 * vsyscall in the #PF path.
+		 */
+		if (!mark_trap_entry(X86_TRAP_GP, regs))
 			goto exit;
 
-		if (emulate_vsyscall_gp(regs))
-			goto exit;
+		if (fixup_umip_exception(regs))
+			goto mark_exit;
 
-		if (!mark_trap_entry(X86_TRAP_GP, regs))
-			goto exit;
+		if (emulate_vsyscall_gp(regs))
+			goto mark_exit;
 
 		gp_user_force_sig_segv(regs, X86_TRAP_GP, error_code, desc);
 		goto mark_exit;
Signature · 1 line
2.47.3