x86: CET-SS related adjustments
v4 cover x86: CET-SS related adjustments
One might think of this as follow-on to XSA-451, but that's not quite the right order of events.
There are a few open aspects; see in particular the final patch.
v4 addresses review feedback along with some significant re-basing that was necessary due to other recent changes. It includes one new tidying patch. See individual patches for details.
1: record SSP at non-guest entry points
2: traps: use entry_ssp in fixup_exception_return() 3: traps: reduce indentation in fixup_exception_return() 4: prefer shadow stack for producing call traces
Jan
PATCH v4 1/4 x86: record SSP at non-guest entry points
We will want to use that value for call trace generation, and likely also to eliminate the somewhat fragile shadow stack searching done in fixup_exception_return(). For those purposes, guest-only entry points do not need to record that value.
To keep the saving code simple, record our own SSP that corresponds to an exception frame, pointing to the top of the shadow stack counterpart of what the CPU has saved on the regular stack. Consuming code can then work its way from there.
In SAVE_ALL / RESTORE_ALL simply drop the use of UREGS_r15. We want the full size in all cases, so what exactly the top-of-stack field is going to be (whose UREGS_* is 0 anyway) doesn't matter this much there. This way we don't need to distinguish between XEN_SHSTK=y and XEN_SHSTK=n.
---
v4: Re-base.
v3: Put new field at the front of struct cpu_user_regs.
v2: Add comment ahead of SAVE_ALL. Add comma between its parameters.
Re-base.
xen/arch/x86/hvm/svm/entry.S +1 -1
@@ -89,7 +89,7 @@ __UNLIKELY_END(nsvm_hap) vmrun - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 GET_CURRENT(bx)
xen/arch/x86/hvm/vmx/entry.S +2 -2
@@ -22,7 +22,7 @@ #include <asm/page.h> FUNC(vmx_asm_vmexit_handler) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 mov %cr2,%rax GET_CURRENT(bx)
@@ -157,7 +157,7 @@ UNLIKELY_END(realmode) .Lvmx_vmentry_fail: sti - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 /* * SPEC_CTRL_ENTRY notes
xen/arch/x86/include/asm/asm_defns.h +13 -2
@@ -220,9 +220,11 @@ static always_inline void stac(void)#ifdef __ASSEMBLER__/* -* Push and clear GPRs +* Push and clear GPRs. +* +* Use sites may override ssp to 0. It should never be overridden to 1. */ -.macro PUSH_AND_CLEAR_GPRS +.macro PUSH_AND_CLEAR_GPRS ssp=IS_ENABLED(CONFIG_XEN_SHSTK) push %rdi xor %edi, %edi push %rsi
@@ -233,6 +235,9 @@ static always_inline void stac(void) xor %ecx, %ecx push %rax xor %eax, %eax + .if \ssp + rdsspq %rcx + .endif push %r8 xor %r8d, %r8d push %r9
@@ -259,12 +264,18 @@ static always_inline void stac(void) xor %r14d, %r14d push %r15 xor %r15d, %r15d +#ifdef CONFIG_XEN_SHSTK + push %rcx +#endif .endm/* * POP GPRs from a UREGS_* frame on the stack. Does not modify flags. */ .macro POP_GPRS skip_rax=0 +#ifdef CONFIG_XEN_SHSTK + pop %rcx +#endif pop %r15 pop %r14 pop %r13
xen/arch/x86/include/asm/cpu-user-regs.h +9 -0
@@ -11,6 +11,15 @@*/ struct cpu_user_regs { +#ifdef CONFIG_XEN_SHSTK +/* +* This points _at_ the corresponding shadow stack frame; it is _not_ the +* outer context's SSP. That, if the outer context has CET-SS enabled, +* is stored in the top slot of the pointed to shadow stack. +*/ +uint64_t entry_ssp; +#endif +union {uint64_t r15;uint32_t r15d;uint16_t r15w;uint8_t r15b; };union {uint64_t r14;uint32_t r14d;uint16_t r14w;uint8_t r14b; };union {uint64_t r13;uint32_t r13d;uint16_t r13w;uint8_t r13b; };
xen/arch/x86/x86_64/asm-offsets.c +3 -0
@@ -53,6 +53,9 @@ void __dummy__(void) OFFSET(UREGS_eflags, struct cpu_user_regs, rflags); OFFSET(UREGS_rsp, struct cpu_user_regs, rsp); OFFSET(UREGS_ss, struct cpu_user_regs, ss); +#ifdef CONFIG_XEN_SHSTK + OFFSET(UREGS_entry_ssp, struct cpu_user_regs, entry_ssp); +#endif DEFINE (UREGS_kernel_sizeof,sizeof (struct cpu_user_regs ));BLANK ();
xen/arch/x86/x86_64/compat/entry.S +1 -1
@@ -18,7 +18,7 @@ FUNC(entry_int82) ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP pushq $0 movb $HYPERCALL_VECTOR, EFRAME_entry_vector(%rsp) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 GET_STACK_END(14)
xen/arch/x86/x86_64/entry.S +4 -4
@@ -305,7 +305,7 @@ FUNC(lstar_enter) pushq $0 BUILD_BUG_ON(TRAP_syscall & 0xff) movb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 GET_STACK_END(14)
@@ -345,7 +345,7 @@ FUNC(cstar_enter) pushq $0 BUILD_BUG_ON(TRAP_syscall & 0xff) movb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 GET_STACK_END(14)
@@ -390,7 +390,7 @@ LABEL(sysenter_eflags_saved, 0) pushq $0 BUILD_BUG_ON(TRAP_syscall & 0xff) movb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 GET_STACK_END(14)
@@ -447,7 +447,7 @@ FUNC(entry_int80) ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP pushq $0 movb $0x80, EFRAME_entry_vector(%rsp) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 GET_STACK_END(14)
xen/arch/x86/x86_64/entry-fred.S +2 -2
@@ -11,7 +11,7 @@ /* The Ring3 entry point is required to be 4k aligned. */ FUNC(entry_FRED_R3, 4096) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 mov %rsp, %rdi call entry_from_pv
@@ -54,7 +54,7 @@ LABEL(eretu, 0) END(eretu_exit_to_guest) FUNC(eretu_error_dom_crash) - PUSH_AND_CLEAR_GPRS + PUSH_AND_CLEAR_GPRS ssp=0 sti call asm_domain_crash_synchronous /* Does not return */ END(eretu_error_dom_crash)
Re: [PATCH v4 1/4] x86: record SSP at non-guest entry points
Quoted 27 lines · Jan Beulich
xen/arch/x86/include/asm/asm_defns.h +7
-2@@ -220,9 +220,11 @@ static always_inline void stac(void)#ifdef __ASSEMBLER__/* -* Push and clear GPRs +* Push and clear GPRs. +* +* Use sites may override ssp to 0. It should never be overridden to 1. */ -.macro PUSH_AND_CLEAR_GPRS +.macro PUSH_AND_CLEAR_GPRS ssp=IS_ENABLED(CONFIG_XEN_SHSTK) push %rdi xor %edi, %edi push %rsi@@ -233,6 +235,9 @@ static always_inline void stac(void) xor %ecx, %ecx push %rax xor %eax, %eax + .if \ssp + rdsspq %rcx + .endif push %r8 xor %r8d, %r8d push %r9
xen/arch/x86/include/asm/asm_defns.h static always_inline void stac(void) · line 270
Quoted 7 lines
xen/arch/x86/include/asm/asm_defns.h +3
-0@@ -259,12 +264,18 @@ static always_inline void stac(void) xor %r14d, %r14d push %r15 xor %r15d, %r15d +#ifdef CONFIG_XEN_SHSTK + push %rcx +#endif .endm
I don't like the ssp= parameter here. It's invasive at may callsites, doesn't 0 the value in all Ring3 frames anyway, and the asymmetry makes the construct fragile. Furthermore, the manual register scheduling really isn't necessary on CPUs where it's not a NOP.
I suggest having this:
+#ifdef CONFIG_XEN_SHSTK
+ rdsspq %rcx
+ push %rcx
+#endif
always filling in the value. rdsspq is pretty close to free (frankly, I would be surprised if it wasn't subject to mov elimination in the pipeline, given how tightly coupled SSP needs to be to RSP for CALL/RET uops to work).
Quoted 15 lines
xen/arch/x86/include/asm/asm_defns.h +3
-0/* * POP GPRs from a UREGS_* frame on the stack. Does not modify flags. */ .macro POP_GPRS skip_rax=0 +#ifdef CONFIG_XEN_SHSTK + pop %rcx +#endif pop %r15 pop %r14 pop %r13xen/arch/x86/include/asm/cpu-user-regs.h +0
-0@@ -11,6 +11,15 @@*/
xen/arch/x86/include/asm/cpu-user-regs.h line 21
Quoted 10 lines
xen/arch/x86/include/asm/cpu-user-regs.h +8
-0struct cpu_user_regs { +#ifdef CONFIG_XEN_SHSTK +/* +* This points _at_ the corresponding shadow stack frame; it is _not_ the +* outer context's SSP. That, if the outer context has CET-SS enabled, +* is stored in the top slot of the pointed to shadow stack. +*/ +uint64_t entry_ssp; +#endif
This needs to say "only valid for interrupt Ring0" or so. Even as written using the ssp= parameter, there will be a non-zero value here on some Ring3 entries.
~Andrew
Re: [PATCH v4 1/4] x86: record SSP at non-guest entry points
Quoted 48 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
xen/arch/x86/include/asm/asm_defns.h +7
-2@@ -220,9 +220,11 @@ static always_inline void stac(void)#ifdef __ASSEMBLER__/* -* Push and clear GPRs +* Push and clear GPRs. +* +* Use sites may override ssp to 0. It should never be overridden to 1. */ -.macro PUSH_AND_CLEAR_GPRS +.macro PUSH_AND_CLEAR_GPRS ssp=IS_ENABLED(CONFIG_XEN_SHSTK) push %rdi xor %edi, %edi push %rsi@@ -233,6 +235,9 @@ static always_inline void stac(void) xor %ecx, %ecx push %rax xor %eax, %eax + .if \ssp + rdsspq %rcx + .endif push %r8 xor %r8d, %r8d push %r9xen/arch/x86/include/asm/asm_defns.h static always_inline void stac(void) · line 270
Quoted 7 lines
xen/arch/x86/include/asm/asm_defns.h +3
-0@@ -259,12 +264,18 @@ static always_inline void stac(void) xor %r14d, %r14d push %r15 xor %r15d, %r15d +#ifdef CONFIG_XEN_SHSTK + push %rcx +#endif .endmI don't like the ssp= parameter here. It's invasive at may callsites, doesn't 0 the value in all Ring3 frames anyway, and the asymmetry makes the construct fragile. Furthermore, the manual register scheduling really isn't necessary on CPUs where it's not a NOP.
I suggest having this:
xen/arch/x86/include/asm/asm_defns.h +4
-0+#ifdef CONFIG_XEN_SHSTK + rdsspq %rcx + push %rcx +#endif
Quoted 4 lines
always filling in the value. rdsspq is pretty close to free (frankly, I would be surprised if it wasn't subject to mov elimination in the pipeline, given how tightly coupled SSP needs to be to RSP for CALL/RET uops to work).
I can make that change, but I didn't code it that way from the start as there continues to be no evidence of what you say (no matter how plausible it reads): None of the latency / throughput documents I can find lists RDSSP.
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/include/asm/cpu-user-regs.h +4
-0@@ -11,6 +11,15 @@*/ struct cpu_user_regs { +#ifdef CONFIG_XEN_SHSTK +/* +* This points _at_ the corresponding shadow stack frame; it is _not_ the +* outer context's SSP. That, if the outer context has CET-SS enabled,
Quoted 8 lines
xen/arch/x86/include/asm/cpu-user-regs.h line 21
Quoted 4 lines
xen/arch/x86/include/asm/cpu-user-regs.h +4
-0+* is stored in the top slot of the pointed to shadow stack. +*/ +uint64_t entry_ssp; +#endif This needs to say "only valid for interrupt Ring0" or so. Even as written using the ssp= parameter, there will be a non-zero value here on some Ring3 entries.
The value being non-zero doesn't mean it's invalid. It may point to the very top of the shadow stack, which is still a valid value. If you want this made explicit, I can add "only meaningful when having interrupted Ring0" or some such. Please clarify.
Jan
Re: [PATCH v4 1/4] x86: record SSP at non-guest entry points
Quoted 49 lines · Jan Beulich
Quoted 48 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
xen/arch/x86/include/asm/asm_defns.h +7
-2@@ -220,9 +220,11 @@ static always_inline void stac(void)#ifdef __ASSEMBLER__/* -* Push and clear GPRs +* Push and clear GPRs. +* +* Use sites may override ssp to 0. It should never be overridden to 1. */ -.macro PUSH_AND_CLEAR_GPRS +.macro PUSH_AND_CLEAR_GPRS ssp=IS_ENABLED(CONFIG_XEN_SHSTK) push %rdi xor %edi, %edi push %rsi@@ -233,6 +235,9 @@ static always_inline void stac(void) xor %ecx, %ecx push %rax xor %eax, %eax + .if \ssp + rdsspq %rcx + .endif push %r8 xor %r8d, %r8d push %r9xen/arch/x86/include/asm/asm_defns.h static always_inline void stac(void) · line 270
Quoted 7 lines
xen/arch/x86/include/asm/asm_defns.h +3
-0@@ -259,12 +264,18 @@ static always_inline void stac(void) xor %r14d, %r14d push %r15 xor %r15d, %r15d +#ifdef CONFIG_XEN_SHSTK + push %rcx +#endif .endmI don't like the ssp= parameter here. It's invasive at may callsites, doesn't 0 the value in all Ring3 frames anyway, and the asymmetry makes the construct fragile. Furthermore, the manual register scheduling really isn't necessary on CPUs where it's not a NOP.
I suggest having this:
xen/arch/x86/include/asm/asm_defns.h +4
-0+#ifdef CONFIG_XEN_SHSTK + rdsspq %rcx + push %rcx +#endif
Quoted 9 lines
Quoted 4 lines
always filling in the value. rdsspq is pretty close to free (frankly, I would be surprised if it wasn't subject to mov elimination in the pipeline, given how tightly coupled SSP needs to be to RSP for CALL/RET uops to work).
I can make that change, but I didn't code it that way from the start as there continues to be no evidence of what you say (no matter how plausible it reads): None of the latency / throughput documents I can find lists RDSSP.
Oh, also: You implicitly indicate then that the code size increase for those entry sequences then isn't a problem either, in your opinion. I can accept that, sure, yet my personal view is that it isn't helpful to have unnecessary insns anywhere, no matter how cheap they may be. They're never zero cost.
Jan
PATCH v4 2/4 x86/traps: use entry_ssp in fixup_exception_return()
In reply to Jan BeulichWith the value recorded on entry there's no need anymore to go hunt for the respective exception frame on the shadow stack. By deriving "ptr" from that field (without any offset), it then ends up pointing one slot lower than before. Therefore all array indexes need incrementing, nicely doing away with all the negative ones.
---
Indentation of the prior inner (but not innermost) if()'s body is deliberately left untouched, to aid review. It'll be adjusted in a separate follow-on patch.
---
v3: Relax the first BUG_ON().
v2: IS_ENABLED() -> #ifdef. Re-base.
xen/arch/x86/traps.c +26 -48
@@ -690,19 +690,6 @@ unsigned long get_stack_trace_bottom(uns } } -static unsigned long get_shstk_bottom (unsigned long sp) -{ -/* SAF-11-safe */ -switch (get_stack_page (sp) ) - { -#ifdef CONFIG_XEN_SHSTK -case 0 :return ROUNDUP (sp,IST_SHSTK_SIZE ) -sizeof (unsigned long ); -case 5 :return ROUNDUP (sp,PAGE_SIZE ) -sizeof (unsigned long ); -#endif -default :return sp -sizeof (unsigned long ); - } -} -unsigned long get_stack_dump_bottom (unsigned long sp) { switch ( get_stack_page(sp) )
@@ -1187,26 +1174,29 @@ void asmlinkage noreturn do_unhandled_trstatic void fixup_exception_return (struct cpu_user_regs *regs,unsigned long fixup,unsigned long stub_ra) { -if (IS_ENABLED (CONFIG_XEN_SHSTK ) ) +#ifdef CONFIG_XEN_SHSTK { -unsigned long ssp, *ptr, *base; +unsigned long ssp =rdssp (); -if ( (ssp =rdssp ()) ==SSP_NO_SHSTK ) - goto shstk_done; +if ( ssp !=SSP_NO_SHSTK ) + { +unsigned long *ptr =_p (regs->entry_ssp); +unsigned long primary_shstk = + (ssp & ~(STACK_SIZE -1 )) + + (PRIMARY_SHSTK_SLOT +1 ) *PAGE_SIZE -8 ; - ptr =_p (ssp); - base =_p (get_shstk_bottom (ssp)); +BUG_ON ((regs->entry_ssp ^ primary_shstk) >> + (PAGE_SHIFT +STACK_ORDER )); -for ( ; ptr < base; ++ptr ) - {/* -* Search for %rip. The shstk currently looks like this: +* The shstk currently looks like this: * * tok [Supervisor token, == &tok | BUSY, only with FRED inactive] * ... [Pointed to by SSP for most exceptions, empty in IST cases] * %cs [== regs->cs] * %rip [== regs->rip] -* SSP [Likely points to 3 slots higher, above %cs] +* SSP [Pointed to by entry_ssp; Likely points to 3 slots +* higher, above %cs] * ... [call tree to this function, likely 2/3 slots] * * and we want to overwrite %rip with fixup. There are two
@@ -1219,13 +1209,10 @@ static void fixup_exception_return(struc* * Check for both regs->rip and regs->cs matching. */ -if ( ptr[0 ] == regs->rip && ptr[1 ] == regs->cs ) - { -unsigned long primary_shstk = - (ssp & ~(STACK_SIZE -1 )) + - (PRIMARY_SHSTK_SLOT +1 ) *PAGE_SIZE -8 ; +BUG_ON (ptr[1 ] != regs->rip || ptr[2 ] != regs->cs); -wrss (fixup, ptr); + { +wrss (fixup, &ptr[1 ]);if ( !stub_ra ) goto shstk_done;
@@ -1242,7 +1229,7 @@ static void fixup_exception_return(struc* - if we're on an IST stack, we need to increment the * original SSP. */ -BUG_ON ((ptr[-1 ] ^ primary_shstk) >>PAGE_SHIFT ); +BUG_ON ((ptr[0 ] ^ primary_shstk) >>PAGE_SHIFT );if ( (ssp ^ primary_shstk) >>PAGE_SHIFT ) {
@@ -1251,39 +1238,30 @@ static void fixup_exception_return(struc* addresses actually match. Then increment the interrupted * context's SSP. */ -BUG_ON (stub_ra != *(unsigned long *)ptr[-1 ]); -wrss (ptr[-1 ] +8 , &ptr[-1 ]); +BUG_ON (stub_ra != *(unsigned long *)ptr[0 ]); +wrss (ptr[0 ] +8 , &ptr[0 ]); goto shstk_done; }/* Make sure the two return addresses actually match. */ -BUG_ON (stub_ra != ptr[2 ]); +BUG_ON (stub_ra != ptr[3 ]);/* Move exception frame, updating SSP there. */ -wrss (ptr[1 ], &ptr[2 ]);/* %cs */ -wrss (ptr[0 ], &ptr[1 ]);/* %rip */ -wrss (ptr[-1 ] +8 , &ptr[0 ]);/* SSP */ +wrss (ptr[2 ], &ptr[3 ]);/* %cs */ +wrss (ptr[1 ], &ptr[2 ]);/* %rip */ +wrss (ptr[0 ] +8 , &ptr[1 ]);/* SSP */ /* Move all newer entries. */ -while ( --ptr !=_p (ssp) ) -wrss (ptr[-1 ], &ptr[0 ]); +while ( ptr-- !=_p (ssp) ) +wrss (ptr[0 ], &ptr[1 ]);/* Finally account for our own stack having shifted up. */ asmvolatile ("incsspd %0" ::"r" (2 ) ); - - goto shstk_done; } } - -/* -* We failed to locate and fix up the shadow IRET frame. This could -* be due to shadow stack corruption, or bad logic above. We cannot -* continue executing the interrupted context. -*/ -BUG (); - } shstk_done: +#endif /* CONFIG_XEN_SHSTK */ /* Fixup the regular stack. */ regs->rip = fixup;
PATCH v4 3/4 x86/traps: reduce indentation in fixup_exception_return()
In reply to Jan BeulichThe earlier "x86/traps: use entry_ssp in fixup_exception_return()" left unnecessary scopes and hence unnecessarily deep indentation. While that was intentional (to improve readabilirty of the diff), rectify this now.
---
v4: New.
xen/arch/x86/traps.c +72 -76
@@ -1175,90 +1175,86 @@ static void fixup_exception_return(struc unsigned long fixup, unsigned long stub_ra) {#ifdef CONFIG_XEN_SHSTK +unsigned long ssp =rdssp (); + +if ( ssp !=SSP_NO_SHSTK ) { -unsigned long ssp =rdssp (); +unsigned long *ptr =_p (regs->entry_ssp); +unsigned long primary_shstk = + (ssp & ~(STACK_SIZE -1 )) + + (PRIMARY_SHSTK_SLOT +1 ) *PAGE_SIZE -8 ; + +BUG_ON ((regs->entry_ssp ^ primary_shstk) >> + (PAGE_SHIFT +STACK_ORDER )); + +/* +* The shstk currently looks like this: +* +* tok [Supervisor token, == &tok | BUSY, only with FRED inactive] +* ... [Pointed to by SSP for most exceptions, empty in IST cases] +* %cs [== regs->cs] +* %rip [== regs->rip] +* SSP [Pointed to by entry_ssp; Likely points to 3 slots +* higher, above %cs] +* ... [call tree to this function, likely 2/3 slots] +* +* and we want to overwrite %rip with fixup. There are two +* complications: +* 1) We cant depend on SSP values, because they won't differ by +* 3 slots if the exception is taken on an IST stack. +* 2) There are synthetic (unrealistic but not impossible) +* scenarios where %rip can end up in the call tree to this +* function, so we can't check against regs->rip alone. +* +* Check for both regs->rip and regs->cs matching. +*/ +BUG_ON (ptr[1 ] != regs->rip || ptr[2 ] != regs->cs); + +wrss (fixup, &ptr[1 ]); + +if ( !stub_ra ) + goto shstk_done; + +/* +* Stub recovery ought to happen only when the outer context +* was on the main shadow stack. We need to also "pop" the +* stub's return address from the interrupted context's shadow +* stack. That is, +* - if we're still on the main stack, we need to move the +* entire stack (up to and including the exception frame) +* up by one slot, incrementing the original SSP in the +* exception frame, +* - if we're on an IST stack, we need to increment the +* original SSP. +*/ +BUG_ON ((ptr[0 ] ^ primary_shstk) >>PAGE_SHIFT ); -if ( ssp !=SSP_NO_SHSTK ) +if ( (ssp ^ primary_shstk) >>PAGE_SHIFT ) { -unsigned long *ptr =_p (regs->entry_ssp); -unsigned long primary_shstk = - (ssp & ~(STACK_SIZE -1 )) + - (PRIMARY_SHSTK_SLOT +1 ) *PAGE_SIZE -8 ; - -BUG_ON ((regs->entry_ssp ^ primary_shstk) >> - (PAGE_SHIFT +STACK_ORDER )); -/* -* The shstk currently looks like this: -* -* tok [Supervisor token, == &tok | BUSY, only with FRED inactive] -* ... [Pointed to by SSP for most exceptions, empty in IST cases] -* %cs [== regs->cs] -* %rip [== regs->rip] -* SSP [Pointed to by entry_ssp; Likely points to 3 slots -* higher, above %cs] -* ... [call tree to this function, likely 2/3 slots] -* -* and we want to overwrite %rip with fixup. There are two -* complications: -* 1) We cant depend on SSP values, because they won't differ by -* 3 slots if the exception is taken on an IST stack. -* 2) There are synthetic (unrealistic but not impossible) -* scenarios where %rip can end up in the call tree to this -* function, so we can't check against regs->rip alone. -* -* Check for both regs->rip and regs->cs matching. +* We're on an IST stack. First make sure the two return +* addresses actually match. Then increment the interrupted +* context's SSP. */ -BUG_ON (ptr[1 ] != regs->rip || ptr[2 ] != regs->cs); +BUG_ON (stub_ra != *(unsigned long *)ptr[0 ]); +wrss (ptr[0 ] +8 , &ptr[0 ]); + goto shstk_done; + } - { -wrss (fixup, &ptr[1 ]); +/* Make sure the two return addresses actually match. */ +BUG_ON (stub_ra != ptr[3 ]); -if ( !stub_ra ) - goto shstk_done; +/* Move exception frame, updating SSP there. */ +wrss (ptr[2 ], &ptr[3 ]);/* %cs */ +wrss (ptr[1 ], &ptr[2 ]);/* %rip */ +wrss (ptr[0 ] +8 , &ptr[1 ]);/* SSP */ + +/* Move all newer entries. */ +while ( ptr-- !=_p (ssp) ) +wrss (ptr[0 ], &ptr[1 ]); -/* -* Stub recovery ought to happen only when the outer context -* was on the main shadow stack. We need to also "pop" the -* stub's return address from the interrupted context's shadow -* stack. That is, -* - if we're still on the main stack, we need to move the -* entire stack (up to and including the exception frame) -* up by one slot, incrementing the original SSP in the -* exception frame, -* - if we're on an IST stack, we need to increment the -* original SSP. -*/ -BUG_ON ((ptr[0 ] ^ primary_shstk) >>PAGE_SHIFT ); - -if ( (ssp ^ primary_shstk) >>PAGE_SHIFT ) - { -/* -* We're on an IST stack. First make sure the two return -* addresses actually match. Then increment the interrupted -* context's SSP. -*/ -BUG_ON (stub_ra != *(unsigned long *)ptr[0 ]); -wrss (ptr[0 ] +8 , &ptr[0 ]); - goto shstk_done; - } - -/* Make sure the two return addresses actually match. */ -BUG_ON (stub_ra != ptr[3 ]); - -/* Move exception frame, updating SSP there. */ -wrss (ptr[2 ], &ptr[3 ]);/* %cs */ -wrss (ptr[1 ], &ptr[2 ]);/* %rip */ -wrss (ptr[0 ] +8 , &ptr[1 ]);/* SSP */ - -/* Move all newer entries. */ -while ( ptr-- !=_p (ssp) ) -wrss (ptr[0 ], &ptr[1 ]); - -/* Finally account for our own stack having shifted up. */ - asmvolatile ("incsspd %0" ::"r" (2 ) ); - } - } +/* Finally account for our own stack having shifted up. */ + asmvolatile ("incsspd %0" ::"r" (2 ) ); } shstk_done:#endif /* CONFIG_XEN_SHSTK */
Re: [PATCH v4 3/4] x86/traps: reduce indentation in fixup_exception_return()
Quoted 5 lines · Jan Beulich
The earlier "x86/traps: use
entry_sspinfixup_exception_return()" left unnecessary scopes and hence unnecessarily deep indentation. While that was intentional (to improve readabilirty of the diff), rectify this now.Signed-off-by Jan Beulich <[email protected]>
PATCH v4 4/4 x86: prefer shadow stack for producing call traces
In reply to Jan BeulichShadow stacks contain little more than return addresses, and they in particular allow precise call traces also with FRAME_POINTER=n:
(XEN) Xen call trace:
(XEN) [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73 (XEN) [<ffff82d04032d802>] C search_exception_table+0xc2/0x177 (XEN) [<ffff82d040358378>] C traps.c#extable_fixup.isra.0+0x18/0x6c (XEN) [<ffff82d040358e3b>] C do_invalid_op+0xab/0x106 (XEN) [<ffff82d040201d98>] C x86_64/entry.S#handle_exception_saved+0x88/0xf4 (XEN) [<ffff82d07fffe044>] E ffff82d07fffe044 (XEN) [<ffff82d040412db0>] C stub_selftest+0xd0/0x168 (XEN) [<ffff82d0403508d6>] C setup.c#init_done+0x116/0x15a
as opposed to this counterpart (earlier during the same boot, before CET is enabled):
(XEN) Xen call trace:
(XEN) [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73 (XEN) [<ffff82d04032d802>] S search_exception_table+0xc2/0x177 (XEN) [<ffff82d040358378>] S traps.c#extable_fixup.isra.0+0x18/0x6c (XEN) [<ffff82d040358e3b>] S do_invalid_op+0xab/0x106 (XEN) [<ffff82d040201d98>] S x86_64/entry.S#handle_exception_saved+0x88/0xf4 (XEN) [<ffff82d040412db0>] S stub_selftest+0xd0/0x168 (XEN) [<ffff82d0403d0cc9>] S do_initcalls+0x29/0x38 (XEN) [<ffff82d04041adf2>] S __start_xen+0x1c72/0x2235 (XEN) [<ffff82d040288a57>] S __high_start+0xb7/0xc0
(note the entirely missing entry for the stub itself [1]; sadly there are no stray entries there).
[1] Arguably we could teach FRAME_POINTER=n traces to recognize stubs as well. But not FRAME_POINTER=y ones. In fact, what's missing there isn't the stub itself, but (of course) its immediate caller.
---
While the 'E' for exception frames is probably okay, I'm not overly happy with the 'C' (for CET). I would have preferred 'S' (for shadow), but we use that character already.
As an alternative to suppressing output for the top level exception frame, adding the new code ahead of the 'R' output line (and then also ahead of the stack top read) could be considered.
Quite likely a number of other uses of is_active_kernel_text() also want amending with in_stub().
---
v3: Correct "link to other shadow stack" check. Don't log a line for the
(impossible) PV case. Add example stack trace to description.
v2: IS_ENABLED() -> #ifdef. Re-base.
xen/arch/x86/traps.c +51 -0
@@ -48,6 +48,7 @@#include <asm/shared.h> #include <asm/shstk.h> #include <asm/smp.h> +#include <asm/stubs.h> #include <asm/system.h> #include <asm/traps.h> #include <asm/uaccess.h>
@@ -705,6 +706,13 @@ unsigned long get_stack_dump_bottom(unsi } } +#ifdef CONFIG_XEN_SHSTK +static bool in_stub (unsigned long addr) +{ +return !((this_cpu (stubs.addr) ^ addr) >>STUB_BUF_SHIFT ); +} +#endif +#if !defined(CONFIG_FRAME_POINTER )/*
@@ -797,6 +805,49 @@ static void show_trace(const struct cpu_ !is_active_kernel_text(tos) )printk (" [<%p>] R %pS\n" ,_p (regs->rip),_p (regs->rip)); +#ifdef CONFIG_XEN_SHSTK +if (rdssp () !=SSP_NO_SHSTK ) + { +const unsigned long *ptr =_p (regs->entry_ssp); +unsigned int n; + +for ( n =0 ; (unsigned long )ptr & (PAGE_SIZE -sizeof (*ptr)); ++n ) + { +unsigned long val = *ptr; + +if (is_active_kernel_text (val) ||in_stub (val) ) + { +/* Normal return address entry. */ +printk (" [<%p>] C %pS\n" ,_p (val),_p (val)); + ++ptr; + } +else if ( !((val ^ (unsigned long )ptr) >> + (PAGE_SHIFT +STACK_ORDER )) ) + { +if ( val & (sizeof (val) -1 ) ) + { +/* Most likely a supervisor token. */ +break ; + } + +/* +* Ought to be a hypervisor interruption frame. But don't +* (re)log the current frame's %rip. +*/ +if ( n || ptr[1 ] != regs->rip ) +printk (" [<%p>] E %pS\n" ,_p (ptr[1 ]),_p (ptr[1 ])); + ptr =_p (val); + } +else /* Bogus. */ +break ; + } + +/* Fall back to legacy stack trace if nothing was logged at all. */ +if ( n ) +return ; + } +#endif /* CONFIG_XEN_SHSTK */ +if ( fault ) {printk (" [Fault on access]\n" );