Re: [PATCH v2 4/4] x86/static_call: Add inline static call implementation for x86-64

Andy Lutomirski
2018-11-29T16:59:36Z

> On Nov 29, 2018, at 8:49 AM, Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Thu, Nov 29, 2018 at 10:33:42AM -0600, Josh Poimboeuf wrote:
>>> can't we 'fix' that again? The alternative is moving that IRET-frame and
>>> fixing everything up, which is going to be fragile, ugly and such
>>> things more.
>
>> This seems to work...
>
> That's almost too easy... nice!

It is indeed too easy: you’re putting pt_regs in the wrong place for int3 from user mode, which is probably a root hole if you arrange for a ptraced process to do int3 and try to write to whatever register aliases CS.

If you make it conditional on CPL, do it for 32-bit as well, add comments, and convince yourself that there isn’t a better solution (like pointing IP at a stub that retpolines to the target by reading the function pointer, a la the unoptimizable version), then okay, I guess, with only a small amount of grumbling.

>
>> diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
>> index ce25d84023c0..184523447d35 100644
>> --- a/arch/x86/entry/entry_64.S
>> +++ b/arch/x86/entry/entry_64.S
>> @@ -876,7 +876,7 @@ apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
>> * @paranoid == 2 is special: the stub will never switch stacks. This is for
>> * #DF: if the thread stack is somehow unusable, we'll still get a useful OOPS.
>> */
>> -.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
>> +.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 create_gap=0
>> ENTRY(\sym)
>> UNWIND_HINT_IRET_REGS offset=\has_error_code*8
>>
>> @@ -891,6 +891,12 @@ ENTRY(\sym)
>> pushq $-1 /* ORIG_RAX: no syscall to restart */
>> .endif
>>
>> + .if \create_gap == 1
>> + .rept 6
>> + pushq 5*8(%rsp)
>> + .endr
>> + .endif
>> +
>> .if \paranoid == 1
>> testb $3, CS-ORIG_RAX(%rsp) /* If coming from userspace, switch stacks */
>> jnz .Lfrom_usermode_switch_stack_\@
>> @@ -1126,7 +1132,7 @@ apicinterrupt3 HYPERV_STIMER0_VECTOR \
>> #endif /* CONFIG_HYPERV */
>>
>> idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
>> -idtentry int3 do_int3 has_error_code=0
>> +idtentry int3 do_int3 has_error_code=0 create_gap=1
>> idtentry stack_segment do_stack_segment has_error_code=1
>>
>> #ifdef CONFIG_XEN_PV