Skip to content

Commit 7c17707

Browse files
pvts-matPlaidCat
authored andcommitted
x86/entry: Convert INT 0x80 emulation to IDTENTRY
jira VULN-772 cve CVE-2024-25744 commit-author Thomas Gleixner <tglx@linutronix.de> commit be5341e There is no real reason to have a separate ASM entry point implementation for the legacy INT 0x80 syscall emulation on 64-bit. IDTENTRY provides all the functionality needed with the only difference that it does not: - save the syscall number (AX) into pt_regs::orig_ax - set pt_regs::ax to -ENOSYS Both can be done safely in the C code of an IDTENTRY before invoking any of the syscall related functions which depend on this convention. Aside of ASM code reduction this prepares for detecting and handling a local APIC injected vector 0x80. [ kirill.shutemov: More verbose comments ] Suggested-by: Linus Torvalds <torvalds@linuxfoundation.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Borislav Petkov (AMD) <bp@alien8.de> Cc: <stable@vger.kernel.org> # v6.0+ (cherry picked from commit be5341e) Signed-off-by: Marcin Wcisło <marcin.wcislo@conclusive.pl>
1 parent 67fb9cf commit 7c17707

File tree

7 files changed

+64
-85
lines changed

7 files changed

+64
-85
lines changed

arch/x86/entry/common.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,62 @@ static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs, int nr)
126126
}
127127
}
128128

129-
/* Handles int $0x80 */
129+
#ifdef CONFIG_IA32_EMULATION
130+
/**
131+
* int80_emulation - 32-bit legacy syscall entry
132+
*
133+
* This entry point can be used by 32-bit and 64-bit programs to perform
134+
* 32-bit system calls. Instances of INT $0x80 can be found inline in
135+
* various programs and libraries. It is also used by the vDSO's
136+
* __kernel_vsyscall fallback for hardware that doesn't support a faster
137+
* entry method. Restarted 32-bit system calls also fall back to INT
138+
* $0x80 regardless of what instruction was originally used to do the
139+
* system call.
140+
*
141+
* This is considered a slow path. It is not used by most libc
142+
* implementations on modern hardware except during process startup.
143+
*
144+
* The arguments for the INT $0x80 based syscall are on stack in the
145+
* pt_regs structure:
146+
* eax: system call number
147+
* ebx, ecx, edx, esi, edi, ebp: arg1 - arg 6
148+
*/
149+
DEFINE_IDTENTRY_RAW(int80_emulation)
150+
{
151+
int nr;
152+
153+
/* Establish kernel context. */
154+
enter_from_user_mode(regs);
155+
156+
instrumentation_begin();
157+
add_random_kstack_offset();
158+
159+
/*
160+
* The low level idtentry code pushed -1 into regs::orig_ax
161+
* and regs::ax contains the syscall number.
162+
*
163+
* User tracing code (ptrace or signal handlers) might assume
164+
* that the regs::orig_ax contains a 32-bit number on invoking
165+
* a 32-bit syscall.
166+
*
167+
* Establish the syscall convention by saving the 32bit truncated
168+
* syscall number in regs::orig_ax and by invalidating regs::ax.
169+
*/
170+
regs->orig_ax = regs->ax & GENMASK(31, 0);
171+
regs->ax = -ENOSYS;
172+
173+
nr = syscall_32_enter(regs);
174+
175+
local_irq_enable();
176+
nr = syscall_enter_from_user_mode_work(regs, nr);
177+
do_syscall_32_irqs_on(regs, nr);
178+
179+
instrumentation_end();
180+
syscall_exit_to_user_mode(regs);
181+
}
182+
#else /* CONFIG_IA32_EMULATION */
183+
184+
/* Handles int $0x80 on a 32bit kernel */
130185
__visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
131186
{
132187
int nr = syscall_32_enter(regs);
@@ -145,6 +200,7 @@ __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
145200
instrumentation_end();
146201
syscall_exit_to_user_mode(regs);
147202
}
203+
#endif /* !CONFIG_IA32_EMULATION */
148204

149205
static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
150206
{

arch/x86/entry/entry_64_compat.S

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

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/xen/enlighten_pv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static struct trap_array_entry trap_array[] = {
637637
TRAP_ENTRY(exc_int3, false ),
638638
TRAP_ENTRY(exc_overflow, false ),
639639
#ifdef CONFIG_IA32_EMULATION
640-
{ entry_INT80_compat, xen_entry_INT80_compat, false },
640+
TRAP_ENTRY(int80_emulation, false ),
641641
#endif
642642
TRAP_ENTRY(exc_page_fault, false ),
643643
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)