Skip to content

Commit f35e466

Browse files
committed
Merge tag 'x86-int80-20231207' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 int80 fixes from Dave Hansen: "Avoid VMM misuse of 'int 0x80' handling in TDX and SEV guests. It also has the very nice side effect of getting rid of a bunch of assembly entry code" * tag 'x86-int80-20231207' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/tdx: Allow 32-bit emulation by default x86/entry: Do not allow external 0x80 interrupts x86/entry: Convert INT 0x80 emulation to IDTENTRY x86/coco: Disable 32-bit emulation by default on TDX and SEV
2 parents 55b224d + f4116bf commit f35e466

File tree

10 files changed

+118
-85
lines changed

10 files changed

+118
-85
lines changed

arch/x86/coco/tdx/tdx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <asm/coco.h>
1111
#include <asm/tdx.h>
1212
#include <asm/vmx.h>
13+
#include <asm/ia32.h>
1314
#include <asm/insn.h>
1415
#include <asm/insn-eval.h>
1516
#include <asm/pgtable.h>

arch/x86/entry/common.c

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <xen/events.h>
2727
#endif
2828

29+
#include <asm/apic.h>
2930
#include <asm/desc.h>
3031
#include <asm/traps.h>
3132
#include <asm/vdso.h>
@@ -167,7 +168,96 @@ static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs, int nr)
167168
}
168169
}
169170

170-
/* Handles int $0x80 */
171+
#ifdef CONFIG_IA32_EMULATION
172+
static __always_inline bool int80_is_external(void)
173+
{
174+
const unsigned int offs = (0x80 / 32) * 0x10;
175+
const u32 bit = BIT(0x80 % 32);
176+
177+
/* The local APIC on XENPV guests is fake */
178+
if (cpu_feature_enabled(X86_FEATURE_XENPV))
179+
return false;
180+
181+
/*
182+
* If vector 0x80 is set in the APIC ISR then this is an external
183+
* interrupt. Either from broken hardware or injected by a VMM.
184+
*
185+
* Note: In guest mode this is only valid for secure guests where
186+
* the secure module fully controls the vAPIC exposed to the guest.
187+
*/
188+
return apic_read(APIC_ISR + offs) & bit;
189+
}
190+
191+
/**
192+
* int80_emulation - 32-bit legacy syscall entry
193+
*
194+
* This entry point can be used by 32-bit and 64-bit programs to perform
195+
* 32-bit system calls. Instances of INT $0x80 can be found inline in
196+
* various programs and libraries. It is also used by the vDSO's
197+
* __kernel_vsyscall fallback for hardware that doesn't support a faster
198+
* entry method. Restarted 32-bit system calls also fall back to INT
199+
* $0x80 regardless of what instruction was originally used to do the
200+
* system call.
201+
*
202+
* This is considered a slow path. It is not used by most libc
203+
* implementations on modern hardware except during process startup.
204+
*
205+
* The arguments for the INT $0x80 based syscall are on stack in the
206+
* pt_regs structure:
207+
* eax: system call number
208+
* ebx, ecx, edx, esi, edi, ebp: arg1 - arg 6
209+
*/
210+
DEFINE_IDTENTRY_RAW(int80_emulation)
211+
{
212+
int nr;
213+
214+
/* Kernel does not use INT $0x80! */
215+
if (unlikely(!user_mode(regs))) {
216+
irqentry_enter(regs);
217+
instrumentation_begin();
218+
panic("Unexpected external interrupt 0x80\n");
219+
}
220+
221+
/*
222+
* Establish kernel context for instrumentation, including for
223+
* int80_is_external() below which calls into the APIC driver.
224+
* Identical for soft and external interrupts.
225+
*/
226+
enter_from_user_mode(regs);
227+
228+
instrumentation_begin();
229+
add_random_kstack_offset();
230+
231+
/* Validate that this is a soft interrupt to the extent possible */
232+
if (unlikely(int80_is_external()))
233+
panic("Unexpected external interrupt 0x80\n");
234+
235+
/*
236+
* The low level idtentry code pushed -1 into regs::orig_ax
237+
* and regs::ax contains the syscall number.
238+
*
239+
* User tracing code (ptrace or signal handlers) might assume
240+
* that the regs::orig_ax contains a 32-bit number on invoking
241+
* a 32-bit syscall.
242+
*
243+
* Establish the syscall convention by saving the 32bit truncated
244+
* syscall number in regs::orig_ax and by invalidating regs::ax.
245+
*/
246+
regs->orig_ax = regs->ax & GENMASK(31, 0);
247+
regs->ax = -ENOSYS;
248+
249+
nr = syscall_32_enter(regs);
250+
251+
local_irq_enable();
252+
nr = syscall_enter_from_user_mode_work(regs, nr);
253+
do_syscall_32_irqs_on(regs, nr);
254+
255+
instrumentation_end();
256+
syscall_exit_to_user_mode(regs);
257+
}
258+
#else /* CONFIG_IA32_EMULATION */
259+
260+
/* Handles int $0x80 on a 32bit kernel */
171261
__visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
172262
{
173263
int nr = syscall_32_enter(regs);
@@ -186,6 +276,7 @@ __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
186276
instrumentation_end();
187277
syscall_exit_to_user_mode(regs);
188278
}
279+
#endif /* !CONFIG_IA32_EMULATION */
189280

190281
static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
191282
{

arch/x86/entry/entry_64_compat.S

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -275,80 +275,3 @@ SYM_INNER_LABEL(entry_SYSRETL_compat_end, SYM_L_GLOBAL)
275275
ANNOTATE_NOENDBR
276276
int3
277277
SYM_CODE_END(entry_SYSCALL_compat)
278-
279-
/*
280-
* 32-bit legacy system call entry.
281-
*
282-
* 32-bit x86 Linux system calls traditionally used the INT $0x80
283-
* instruction. INT $0x80 lands here.
284-
*
285-
* This entry point can be used by 32-bit and 64-bit programs to perform
286-
* 32-bit system calls. Instances of INT $0x80 can be found inline in
287-
* various programs and libraries. It is also used by the vDSO's
288-
* __kernel_vsyscall fallback for hardware that doesn't support a faster
289-
* entry method. Restarted 32-bit system calls also fall back to INT
290-
* $0x80 regardless of what instruction was originally used to do the
291-
* system call.
292-
*
293-
* This is considered a slow path. It is not used by most libc
294-
* implementations on modern hardware except during process startup.
295-
*
296-
* Arguments:
297-
* eax system call number
298-
* ebx arg1
299-
* ecx arg2
300-
* edx arg3
301-
* esi arg4
302-
* edi arg5
303-
* ebp arg6
304-
*/
305-
SYM_CODE_START(entry_INT80_compat)
306-
UNWIND_HINT_ENTRY
307-
ENDBR
308-
/*
309-
* Interrupts are off on entry.
310-
*/
311-
ASM_CLAC /* Do this early to minimize exposure */
312-
ALTERNATIVE "swapgs", "", X86_FEATURE_XENPV
313-
314-
/*
315-
* User tracing code (ptrace or signal handlers) might assume that
316-
* the saved RAX contains a 32-bit number when we're invoking a 32-bit
317-
* syscall. Just in case the high bits are nonzero, zero-extend
318-
* the syscall number. (This could almost certainly be deleted
319-
* with no ill effects.)
320-
*/
321-
movl %eax, %eax
322-
323-
/* switch to thread stack expects orig_ax and rdi to be pushed */
324-
pushq %rax /* pt_regs->orig_ax */
325-
326-
/* Need to switch before accessing the thread stack. */
327-
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
328-
329-
/* In the Xen PV case we already run on the thread stack. */
330-
ALTERNATIVE "", "jmp .Lint80_keep_stack", X86_FEATURE_XENPV
331-
332-
movq %rsp, %rax
333-
movq PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
334-
335-
pushq 5*8(%rax) /* regs->ss */
336-
pushq 4*8(%rax) /* regs->rsp */
337-
pushq 3*8(%rax) /* regs->eflags */
338-
pushq 2*8(%rax) /* regs->cs */
339-
pushq 1*8(%rax) /* regs->ip */
340-
pushq 0*8(%rax) /* regs->orig_ax */
341-
.Lint80_keep_stack:
342-
343-
PUSH_AND_CLEAR_REGS rax=$-ENOSYS
344-
UNWIND_HINT_REGS
345-
346-
cld
347-
348-
IBRS_ENTER
349-
UNTRAIN_RET
350-
351-
movq %rsp, %rdi
352-
call do_int80_syscall_32
353-
jmp swapgs_restore_regs_and_return_to_usermode
354-
SYM_CODE_END(entry_INT80_compat)

arch/x86/include/asm/ia32.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,20 @@ static inline bool ia32_enabled(void)
7575
return __ia32_enabled;
7676
}
7777

78+
static inline void ia32_disable(void)
79+
{
80+
__ia32_enabled = false;
81+
}
82+
7883
#else /* !CONFIG_IA32_EMULATION */
7984

8085
static inline bool ia32_enabled(void)
8186
{
8287
return IS_ENABLED(CONFIG_X86_32);
8388
}
8489

90+
static inline void ia32_disable(void) {}
91+
8592
#endif
8693

8794
#endif /* _ASM_X86_IA32_H */

arch/x86/include/asm/idtentry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ DECLARE_IDTENTRY_RAW(X86_TRAP_UD, exc_invalid_op);
569569
DECLARE_IDTENTRY_RAW(X86_TRAP_BP, exc_int3);
570570
DECLARE_IDTENTRY_RAW_ERRORCODE(X86_TRAP_PF, exc_page_fault);
571571

572+
#if defined(CONFIG_IA32_EMULATION)
573+
DECLARE_IDTENTRY_RAW(IA32_SYSCALL_VECTOR, int80_emulation);
574+
#endif
575+
572576
#ifdef CONFIG_X86_MCE
573577
#ifdef CONFIG_X86_64
574578
DECLARE_IDTENTRY_MCE(X86_TRAP_MC, exc_machine_check);

arch/x86/include/asm/proto.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ void entry_SYSCALL_compat(void);
3232
void entry_SYSCALL_compat_safe_stack(void);
3333
void entry_SYSRETL_compat_unsafe_stack(void);
3434
void entry_SYSRETL_compat_end(void);
35-
void entry_INT80_compat(void);
36-
#ifdef CONFIG_XEN_PV
37-
void xen_entry_INT80_compat(void);
38-
#endif
3935
#else /* !CONFIG_IA32_EMULATION */
4036
#define entry_SYSCALL_compat NULL
4137
#define entry_SYSENTER_compat NULL

arch/x86/kernel/idt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static const __initconst struct idt_data def_idts[] = {
121121

122122
static const struct idt_data ia32_idt[] __initconst = {
123123
#if defined(CONFIG_IA32_EMULATION)
124-
SYSG(IA32_SYSCALL_VECTOR, entry_INT80_compat),
124+
SYSG(IA32_SYSCALL_VECTOR, asm_int80_emulation),
125125
#elif defined(CONFIG_X86_32)
126126
SYSG(IA32_SYSCALL_VECTOR, entry_INT80_32),
127127
#endif

arch/x86/mm/mem_encrypt_amd.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <asm/msr.h>
3333
#include <asm/cmdline.h>
3434
#include <asm/sev.h>
35+
#include <asm/ia32.h>
3536

3637
#include "mm_internal.h"
3738

@@ -481,6 +482,16 @@ void __init sme_early_init(void)
481482
*/
482483
if (sev_status & MSR_AMD64_SEV_ES_ENABLED)
483484
x86_cpuinit.parallel_bringup = false;
485+
486+
/*
487+
* The VMM is capable of injecting interrupt 0x80 and triggering the
488+
* compatibility syscall path.
489+
*
490+
* By default, the 32-bit emulation is disabled in order to ensure
491+
* the safety of the VM.
492+
*/
493+
if (sev_status & MSR_AMD64_SEV_ENABLED)
494+
ia32_disable();
484495
}
485496

486497
void __init mem_encrypt_free_decrypted_mem(void)

arch/x86/xen/enlighten_pv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ static struct trap_array_entry trap_array[] = {
704704
TRAP_ENTRY(exc_int3, false ),
705705
TRAP_ENTRY(exc_overflow, false ),
706706
#ifdef CONFIG_IA32_EMULATION
707-
{ entry_INT80_compat, xen_entry_INT80_compat, false },
707+
TRAP_ENTRY(int80_emulation, false ),
708708
#endif
709709
TRAP_ENTRY(exc_page_fault, false ),
710710
TRAP_ENTRY(exc_divide_error, false ),

arch/x86/xen/xen-asm.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ xen_pv_trap asm_xenpv_exc_machine_check
156156
#endif /* CONFIG_X86_MCE */
157157
xen_pv_trap asm_exc_simd_coprocessor_error
158158
#ifdef CONFIG_IA32_EMULATION
159-
xen_pv_trap entry_INT80_compat
159+
xen_pv_trap asm_int80_emulation
160160
#endif
161161
xen_pv_trap asm_exc_xen_unknown_trap
162162
xen_pv_trap asm_exc_xen_hypervisor_callback

0 commit comments

Comments
 (0)