RE: [tip: x86/bugs] x86/retpoline: Ensure default return thunk isn't used at runtime

From: Kaplan, David
Date: Thu Oct 19 2023 - 10:21:48 EST


[AMD Official Use Only - General]

> -----Original Message-----
> From: Borislav Petkov <bp@xxxxxxxxx>
> Sent: Thursday, October 19, 2023 9:15 AM
> To: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>; linux-kernel@xxxxxxxxxxxxxxx; linux-tip-
> commits@xxxxxxxxxxxxxxx; Kaplan, David <David.Kaplan@xxxxxxx>; Peter
> Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>; x86@xxxxxxxxxx; David Howells
> <dhowells@xxxxxxxxxx>
> Subject: Re: [tip: x86/bugs] x86/retpoline: Ensure default return thunk isn't
> used at runtime
>
> Caution: This message originated from an External Source. Use proper caution
> when opening attachments, clicking links, or responding.
>
>
> On Wed, Oct 18, 2023 at 11:59:28PM -0700, Josh Poimboeuf wrote:
> > One last idea, since the return thunk is used everywhere (even non-ABI
> > compliant functions) it might be possible the "call check_thunks" (and
> > its call to warn_printk) is clobbering some registers which some code
> > (exception handling entry code?) doesn't appreciate.
>
> Yeah, that is still unclean, I'd say. gcc doesn't know that we patch in a CALL insn
> in the alternative. What should work is to have
>
> alternative_call
>
> there which alternates between two calls and gcc knows there's a call so it can
> act accordingly wrt callee-* regs.
>
> Considering how __x86_return_thunk is there only until alternatives have run,
> we could do something like
>
> ALTERNATIVE_CALL nop, check_thunks
>
> where nop is a function which doesn't do anything.
>
> I say "ALTERNATIVE_CALL" because we don't have a _CALL asm macro yet.
>
> And then in check_thunks() we can do all kinds of screaming, tainting and
> setting mitigation status to vulnerable, etc.
>
> Anyway something along those lines.

The return thunk is used for all functions though, including assembly coded functions which may use non-standard calling conventions and aren't visible to gcc. I think the only safe thing would be to preserve all GPRs across the call to check_thunks. Something like PUSH_REGS/call check_thunks/POP_REGS.

--David Kaplan