x86: entry_64.S: always allocate complete "struct pt_regs"
Subsystems (2)
PATCH 1/2 x86: entry_64.S: always allocate complete "struct pt_regs"
64-bit code was using six stack slots less by not saving/restoring registers which are callee-preserved according to C ABI,
and not allocating space for them.
Only when syscall needed a complete "struct pt_regs",
the complete area was allocated and filled in.
As an additional twist, on interrupt entry a "slightly less truncated pt_regs" trick is used, to make nested interrupt stacks easier to unwind.
This proved to be a source of significant obfuscation and subtle bugs. For example, stub_fork had to pop the return address, extend the struct, save registers, and push return address back. Ugly. ia32_ptregs_common pops return address and "returns" via jmp insn, throwing a wrench into CPU return stack cache.
This patch changes code to always allocate a complete "struct pt_regs". The saving of registers is still done lazily.
"Partial pt_regs" trick on interrupt stack is retained.
Macros which manipulate "struct pt_regs" on stack are reworked:ALLOC_PT_GPREGS_ON_STACK allocates the structure.SAVE_C_REGS saves to it those registers which are clobbered by C code. SAVE_EXTRA_REGS saves to it all other registers.
Corresponding RESTORE_* and REMOVE_PT_GPREGS_FROM_STACK macros reverse it.
ia32_ptregs_common, stub_fork and friends lost their ugly dance with return pointer.
LOAD_ARGS32 in ia32entry.S now uses symbolic stack offsets instead of magic numbers.
error_entry and save_paranoid now use SAVE_C_REGS + SAVE_EXTRA_REGS instead of having it open-coded yet again.
Patch was run-tested: 64-bit executables, 32-bit executables, strace works.
Timing tests did not show measurable difference in 32-bit and 64-bit syscalls.
--- arch/x86/ia32/ia32entry.S | 47 +++---- arch/x86/include/asm/calling.h | 222 ++++++++++++++++----------------- arch/x86/include/asm/irqflags.h | 4 +- arch/x86/include/uapi/asm/ptrace-abi.h | 1 - arch/x86/kernel/entry_64.S | 192 +++++++++++----------------- 5 files changed, 207 insertions(+), 259 deletions(-)
arch/x86/ia32/ia32entry.S +25 -22
@@ -62,12 +62,12 @@ */ .macro LOAD_ARGS32 offset, _r9=0 .if \_r9 - movl \offset+16(%rsp),%r9d + movl \offset+R9(%rsp),%r9d .endif - movl \offset+40(%rsp),%ecx - movl \offset+48(%rsp),%edx - movl \offset+56(%rsp),%esi - movl \offset+64(%rsp),%edi + movl \offset+RCX(%rsp),%ecx + movl \offset+RDX(%rsp),%edx + movl \offset+RSI(%rsp),%esi + movl \offset+RDI(%rsp),%edi movl %eax,%eax /* zero extension */ .endm
@@ -144,7 +144,8 @@ ENTRY(ia32_sysenter_target) CFI_REL_OFFSET rip,0 pushq_cfi %rax cld - SAVE_ARGS 0,1,0 + ALLOC_PT_GPREGS_ON_STACK + SAVE_C_REGS_EXCEPT_R891011 /* no need to do an access_ok check here because rbp has been 32bit zero extended */ ASM_STAC
@@ -182,7 +183,8 @@ sysexit_from_sys_call: andl $~0x200,EFLAGS-ARGOFFSET(%rsp) movl RIP-ARGOFFSET(%rsp),%edx /* User %eip */ CFI_REGISTER rip,rdx - RESTORE_ARGS 0,24,0,0,0,0 + RESTORE_RSI_RDI + REMOVE_PT_GPREGS_FROM_STACK 3*8 xorq %r8,%r8 xorq %r9,%r9 xorq %r10,%r10
@@ -256,13 +258,13 @@ sysenter_tracesys: testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET) jz sysenter_auditsys #endif - SAVE_REST + SAVE_EXTRA_REGS CLEAR_RREGS movq $-ENOSYS,RAX(%rsp)/* ptrace can change this for a bad syscall */ movq %rsp,%rdi /* &pt_regs -> arg1 */ call syscall_trace_enter LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ - RESTORE_REST + RESTORE_EXTRA_REGS cmpq $(IA32_NR_syscalls-1),%rax ja int_ret_from_sys_call /* sysenter_tracesys has set RAX(%rsp) */ jmp sysenter_do_call
@@ -304,7 +306,8 @@ ENTRY(ia32_cstar_target) * disabled irqs and here we enable it straight after entry: */ ENABLE_INTERRUPTS(CLBR_NONE) - SAVE_ARGS 8,0,0 + ALLOC_PT_GPREGS_ON_STACK 8 + SAVE_C_REGS_EXCEPT_RCX_R891011 movl %eax,%eax /* zero extension */ movq %rax,ORIG_RAX-ARGOFFSET(%rsp) movq %rcx,RIP-ARGOFFSET(%rsp)
@@ -341,7 +344,7 @@ cstar_dispatch: jnz sysretl_audit sysretl_from_sys_call: andl $~TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET) - RESTORE_ARGS 0,-ARG_SKIP,0,0,0 + RESTORE_RSI_RDI_RDX movl RIP-ARGOFFSET(%rsp),%ecx CFI_REGISTER rip,rcx movl EFLAGS-ARGOFFSET(%rsp),%r11d
@@ -372,13 +375,13 @@ cstar_tracesys: jz cstar_auditsys #endif xchgl %r9d,%ebp - SAVE_REST + SAVE_EXTRA_REGS CLEAR_RREGS 0, r9 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */ movq %rsp,%rdi /* &pt_regs -> arg1 */ call syscall_trace_enter LOAD_ARGS32 ARGOFFSET, 1 /* reload args from stack in case ptrace changed it */ - RESTORE_REST + RESTORE_EXTRA_REGS xchgl %ebp,%r9d cmpq $(IA32_NR_syscalls-1),%rax ja int_ret_from_sys_call /* cstar_tracesys has set RAX(%rsp) */
@@ -433,7 +436,8 @@ ENTRY(ia32_syscall) cld /* note the registers are not zero extended to the sf. this could be a problem. */ - SAVE_ARGS 0,1,0 + ALLOC_PT_GPREGS_ON_STACK + SAVE_C_REGS_EXCEPT_R891011 orl $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET) testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET) jnz ia32_tracesys
@@ -446,16 +450,16 @@ ia32_sysret: movq %rax,RAX-ARGOFFSET(%rsp) ia32_ret_from_sys_call: CLEAR_RREGS -ARGOFFSET - jmp int_ret_from_sys_call + jmp int_ret_from_sys_call -ia32_tracesys: - SAVE_REST +ia32_tracesys: + SAVE_EXTRA_REGS CLEAR_RREGS movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */ movq %rsp,%rdi /* &pt_regs -> arg1 */ call syscall_trace_enter LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */ - RESTORE_REST + RESTORE_EXTRA_REGS cmpq $(IA32_NR_syscalls-1),%rax ja int_ret_from_sys_call /* ia32_tracesys has set RAX(%rsp) */ jmp ia32_do_call
@@ -492,7 +496,6 @@ GLOBAL(stub32_clone) ALIGN ia32_ptregs_common: - popq %r11 CFI_ENDPROC CFI_STARTPROC32 simple CFI_SIGNAL_FRAME
@@ -507,9 +510,9 @@ ia32_ptregs_common: /* CFI_REL_OFFSET rflags,EFLAGS-ARGOFFSET*/ CFI_REL_OFFSET rsp,RSP-ARGOFFSET /* CFI_REL_OFFSET ss,SS-ARGOFFSET*/ - SAVE_REST + SAVE_EXTRA_REGS 8 call *%rax - RESTORE_REST - jmp ia32_sysret /* misbalances the return cache */ + RESTORE_EXTRA_REGS 8 + ret CFI_ENDPROC END(ia32_ptregs_common)
arch/x86/include/asm/calling.h +108 -114
251 lines not on this page · raw message
arch/x86/include/asm/irqflags.h +2 -2
@@ -171,9 +171,9 @@ static inline int arch_irqs_disabled(void)#define ARCH_LOCKDEP_SYS_EXIT_IRQ \ TRACE_IRQS_ON; \ sti; \ - SAVE_REST; \ + SAVE_EXTRA_REGS; \ LOCKDEP_SYS_EXIT; \ - RESTORE_REST; \ + RESTORE_EXTRA_REGS; \ cli; \ TRACE_IRQS_OFF;
arch/x86/include/uapi/asm/ptrace-abi.h +0 -1
@@ -49,7 +49,6 @@#define EFLAGS 144#define RSP 152#define SS 160 -#define ARGOFFSET R11#endif /* __ASSEMBLY__ */ /* top of stack page */
arch/x86/kernel/entry_64.S +72 -120
430 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 2/2 x86: entry_64.S: fix comments. No code changes
Misleading and slightly wrong comments in "struct pt_regs" are fixed (four instances).
Fix wrong comment atop EMPTY_FRAME macro.
Explain in more details what we do with stack layout during hw interrupt.
Correct comments about "partial stack frame" which are no longer true.
--- arch/x86/include/asm/ptrace.h | 13 ++++++++++--- arch/x86/include/uapi/asm/ptrace-abi.h | 15 +++++++++++---- arch/x86/include/uapi/asm/ptrace.h | 13 ++++++++++--- arch/x86/kernel/entry_64.S | 18 ++++++++++++------ 4 files changed, 43 insertions(+), 16 deletions(-)
arch/x86/include/asm/ptrace.h +10 -3
@@ -31,13 +31,17 @@ struct pt_regs { #else /* __i386__ */ struct pt_regs { +/* + * C ABI says these regs are callee-preserved. They aren't saved on kernel entry + * unless syscall needs a complete, fully filled "struct pt_regs". + */ unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; -/* arguments: non interrupts/non tracing syscalls only save up to here*/ +/* These regs are callee-clobbered. Always saved on kernel entry. */ unsigned long r11; unsigned long r10; unsigned long r9;
@@ -47,9 +51,12 @@ struct pt_regs {unsigned long dx;unsigned long si;unsigned long di; +/* +* On syscall entry, this is syscall#. On CPU exception, this is error code. +* On hw interrupt, it's IRQ number: +*/ unsigned long orig_ax; -/* end of arguments */ -/* cpu exception frame or undefined */ +/* Return frame for iretq */ unsigned long ip;unsigned long cs;unsigned long flags;
arch/x86/include/uapi/asm/ptrace-abi.h +11 -4
@@ -25,13 +25,17 @@#else /* __i386__ */ #if defined(__ASSEMBLY__) || defined(__FRAME_OFFSETS) +/* +* C ABI says these regs are callee-preserved. They aren't saved on kernel entry +* unless syscall needs a complete, fully filled "struct pt_regs". +*/ #define R15 0#define R14 8#define R13 16#define R12 24#define RBP 32#define RBX 40 -/* arguments: interrupts/non tracing syscalls only save up to here*/ +/* These regs are callee-clobbered. Always saved on kernel entry. */ #define R11 48#define R10 56#define R9 64
@@ -41,9 +45,12 @@#define RDX 96#define RSI 104#define RDI 112 -#define ORIG_RAX 120/* = ERROR */ -/* end of arguments */ -/* cpu exception frame or undefined in case of fast syscall. */ +/* +* On syscall entry, this is syscall#. On CPU exception, this is error code. +* On hw interrupt, it's IRQ number: +*/ +#define ORIG_RAX 120 +/* Return frame for iretq */ #define RIP 128#define CS 136#define EFLAGS 144
arch/x86/include/uapi/asm/ptrace.h +10 -3
@@ -41,13 +41,17 @@ struct pt_regs { #ifndef __KERNEL__ struct pt_regs { +/* + * C ABI says these regs are callee-preserved. They aren't saved on kernel entry + * unless syscall needs a complete, fully filled "struct pt_regs". + */ unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long rbp; unsigned long rbx; -/* arguments: non interrupts/non tracing syscalls only save up to here*/ +/* These regs are callee-clobbered. Always saved on kernel entry. */ unsigned long r11; unsigned long r10; unsigned long r9;
@@ -57,9 +61,12 @@ struct pt_regs {unsigned long rdx;unsigned long rsi;unsigned long rdi; +/* +* On syscall entry, this is syscall#. On CPU exception, this is error code. +* On hw interrupt, it's IRQ number: +*/ unsigned long orig_rax; -/* end of arguments */ -/* cpu exception frame or undefined */ +/* Return frame for iretq */ unsigned long rip;unsigned long cs;unsigned long eflags;
arch/x86/kernel/entry_64.S +12 -6
@@ -14,9 +14,6 @@ * NOTE: This code handles signal-recognition, which happens every time * after an interrupt and after each system call. * - * Normal syscalls and interrupts don't save a full stack frame, this is - * only done for syscall tracing, signals or fork/exec et.al. - * * A note on terminology: * - top of stack: Architecture defined interrupt frame from SS to RIP * at the top of the kernel process stack.
@@ -151,7 +148,7 @@ ENDPROC(native_usergs_sysret64) .endm /* - * initial frame state for interrupts (and exceptions without error code) + * empty frame */ .macro EMPTY_FRAME start=1 offset=0 .if \start
@@ -378,7 +375,7 @@ tracesys_phase2: call syscall_trace_enter_phase2 /* - * Reload arg registers from stack in case ptrace changed them. + * Reload registers from stack in case ptrace changed them. * We don't reload %rax because syscall_trace_entry_phase2() returned * the value it wants us to use in the table lookup. */
@@ -628,6 +625,13 @@ END(interrupt) /* 0(%rsp): ~(interrupt number) */ .macro interrupt func cld + /* + * Since nothing in interrupt handling code touches r12...r15 members + * of "struct pt_regs", and since interrupts can nest, we can save + * four stack slots and simultaneously provide + * an unwind-friendly stack layout by saving "truncated" pt_regs + * exactly up to rbp slot, without these members. + */ ALLOC_PT_GPREGS_ON_STACK -RBP SAVE_C_REGS -RBP /* this goes to 0(%rsp) for unwinder, not for saving the value: */
@@ -640,6 +644,7 @@ END(interrupt) SWAPGS 1: /* + * Save previous stack pointer, optionally switch to interrupt stack. * irq_count is used to check if a CPU is already on an interrupt stack * or not. While this is essentially redundant with preempt_count it is * a little cheaper to use a separate counter in the PDA (short of
@@ -680,6 +685,7 @@ ret_from_intr: /* Restore saved previous stack */ popq %rsi CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */ + /* return code expects complete pt_regs - adjust rsp accordingly: */ leaq ARGOFFSET-RBP(%rsi), %rsp CFI_DEF_CFA_REGISTER rsp CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
@@ -691,7 +697,7 @@ exit_intr: /* Interrupt came from user space */ /* - * Has a correct top of stack, but a partial stack frame + * Has a correct top of stack. * %rcx: thread info. Interrupts off. */ retint_with_reschedule:
Signature · 1 line
1.8.1.4
Borislav Petkov
maintainerQuoted 51 lines · Denys Vlasenko
64-bit code was using six stack slots less by not saving/restoring registers which are callee-preserved according to C ABI,
and not allocating space for them.
Only when syscall needed a complete "structpt_regs",
the complete area was allocated and filled in.
As an additional twist, on interrupt entry a "slightly less truncatedpt_regs" trick is used, to make nested interrupt stacks easier to unwind.This proved to be a source of significant obfuscation and subtle bugs. For example,
stub_forkhad to pop the return address, extend the struct, save registers, and push return address back. Ugly.ia32_ptregs_commonpops return address and "returns" via jmp insn, throwing a wrench into CPU return stack cache.This patch changes code to always allocate a complete "struct
pt_regs". The saving of registers is still done lazily."Partial
pt_regs" trick on interrupt stack is retained.Macros which manipulate "struct
pt_regs" on stack are reworked:ALLOC_PT_GPREGS_ON_STACKallocates the structure.SAVE_C_REGSsaves to it those registers which are clobbered by C code.SAVE_EXTRA_REGSsaves to it all other registers.
Corresponding RESTORE_* andREMOVE_PT_GPREGS_FROM_STACKmacros reverse it.
ia32_ptregs_common,stub_forkand friends lost their ugly dance with return pointer.
LOAD_ARGS32inia32entry.Snow uses symbolic stack offsets instead of magic numbers.
error_entryandsave_paranoidnow useSAVE_C_REGS+SAVE_EXTRA_REGSinstead of having it open-coded yet again.Patch was run-tested: 64-bit executables, 32-bit executables, strace works.
Timing tests did not show measurable difference in 32-bit and 64-bit syscalls.Signed-off-by Denys Vlasenko <[email protected]>CC Linus Torvalds <[email protected]>CC Oleg Nesterov <[email protected]>CC Borislav Petkov <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andy Lutomirski <[email protected]>CC Frederic Weisbecker <[email protected]>CC X86 ML <[email protected]>CC Alexei Starovoitov <[email protected]>CC Will Drewry <[email protected]>CC Kees Cook <[email protected]>
Quoted 6 lines
--- arch/x86/ia32/ia32entry.S | 47 +++---- arch/x86/include/asm/calling.h | 222 ++++++++++++++++----------------- arch/x86/include/asm/irqflags.h | 4 +- arch/x86/include/uapi/asm/ptrace-abi.h | 1 - arch/x86/kernel/entry_64.S | 192 +++++++++++-----------------
You'd need to redo that patch against latest upstream because of changes it is missing:
Andy's branch x86/entry should have them too:
http://git.kernel.org/cgit/linux/kernel/git/luto/linux.git
arch/x86/kernel/entry_64.S: Assembler messages:arch/x86/kernel/entry_64.S:760: Error: no such instruction: `restore_args 1,8,1' make[2]: *** [arch/x86/kernel/entry_64.o] Error 1
make[1]: *** [arch/x86/kernel] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [arch/x86] Error 2
make: *** Waiting for unfinished jobs....
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Quoted 14 lines · Borislav Petkov
Quoted 6 lines · Denys Vlasenko
Macros which manipulate "struct
pt_regs" on stack are reworked:ALLOC_PT_GPREGS_ON_STACKallocates the structure.SAVE_C_REGSsaves to it those registers which are clobbered by C code.SAVE_EXTRA_REGSsaves to it all other registers.
Corresponding RESTORE_* andREMOVE_PT_GPREGS_FROM_STACKmacros reverse it.You'd need to redo that patch against latest upstream because of changes it is missing:
Andy's branch x86/entry should have them too:
http://git.kernel.org/cgit/linux/kernel/git/luto/linux.git
Quoted 7 lines
arch/x86/kernel/entry_64.S: Assembler messages:arch/x86/kernel/entry_64.S:760: Error: no such instruction: `restore_args 1,8,1' make[2]: *** [arch/x86/kernel/entry_64.o] Error 1
make[1]: *** [arch/x86/kernel] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [arch/x86] Error 2
make: *** Waiting for unfinished jobs....
Thanks!
The renaming of macros caught the bug at compile time, as intended.
I'll send an updated patch set v3 in a minute. It will have additional patch in front, since that location in code also wrongly uses R11 instead of ARGOFFSET.
Quoted 23 lines · Denys Vlasenko
Quoted 14 lines · Borislav Petkov
Quoted 6 lines · Denys Vlasenko
Macros which manipulate "struct
pt_regs" on stack are reworked:ALLOC_PT_GPREGS_ON_STACKallocates the structure.SAVE_C_REGSsaves to it those registers which are clobbered by C code.SAVE_EXTRA_REGSsaves to it all other registers.
Corresponding RESTORE_* andREMOVE_PT_GPREGS_FROM_STACKmacros reverse it.You'd need to redo that patch against latest upstream because of changes it is missing:
Andy's branch x86/entry should have them too:
http://git.kernel.org/cgit/linux/kernel/git/luto/linux.gitQuoted 7 lines
arch/x86/kernel/entry_64.S: Assembler messages:arch/x86/kernel/entry_64.S:760: Error: no such instruction: `restore_args 1,8,1' make[2]: *** [arch/x86/kernel/entry_64.o] Error 1
make[1]: *** [arch/x86/kernel] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [arch/x86] Error 2
make: *** Waiting for unfinished jobs....
Quoted 6 lines
Thanks!
The renaming of macros caught the bug at compile time, as intended.I'll send an updated patch set v3 in a minute. It will have additional patch in front, since that location in code also wrongly uses R11 instead of ARGOFFSET.
If you aren't already, can you base it here:
https://git.kernel.org/cgit/linux/kernel/git/luto/linux.git/log/?h=x86/entry
--Andy
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Quoted 10 lines · Andy Lutomirski
Quoted 6 lines
Thanks!
The renaming of macros caught the bug at compile time, as intended.I'll send an updated patch set v3 in a minute. It will have additional patch in front, since that location in code also wrongly uses R11 instead of ARGOFFSET.
If you aren't already, can you base it here:
https://git.kernel.org/cgit/linux/kernel/git/luto/linux.git/log/?h=x86/entry
Yes, the most recent 3-patch set is on top of that tree.
I'll read this later and get back to you.
Quoted 9 lines · Denys Vlasenko
Quoted 7 lines · Andy Lutomirski
Quoted 6 lines
Thanks!
The renaming of macros caught the bug at compile time, as intended.I'll send an updated patch set v3 in a minute. It will have additional patch in front, since that location in code also wrongly uses R11 instead of ARGOFFSET.
Quoted 10 lines
Quoted 3 lines
If you aren't already, can you base it here:
https://git.kernel.org/cgit/linux/kernel/git/luto/linux.git/log/?h=x86/entry
Yes, the most recent 3-patch set is on top of that tree.
Signature · 4 lines
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�