Re: [PATCH v5 00/15] x86: Add support for Clang CFI

Peter Zijlstra
2021-10-27T14:38:49Z

On Wed, Oct 27, 2021 at 04:18:17PM +0200, Ard Biesheuvel wrote:
> On Wed, 27 Oct 2021 at 16:03, Peter Zijlstra <peterz@infradead.org> wrote:

> > /*
> > * Turns a Clang CFI jump-table entry into an actual function pointer.
> > * These jump-table entries are simply jmp.d32 instruction with their
> > * relative offset pointing to the actual function, therefore decode the
> > * instruction to find the real function.
> > */
> > static __always_inline void *nocfi_ptr(void *func)
> > {
> > union text_poke_insn insn = *(union text_poke_insn *)func;

also, probably, for the paranoid amongst us:

if (WARN_ON_ONCE(insn.opcode != JMP32_INSN_OPCODE))
return func;

> > return func + sizeof(insn) + insn.disp;
> > }
> >
> > But really, that wants to be a compiler intrinsic.
>
> Agreed. We could easily do something similar on arm64, but I'd prefer
> to avoid that too.

Right, because on x86 CET-IBT will force that entry to have a different
form (and size), similar on arm64 with BTI.

I was thinking the compiler really should implicitly do this conversion
when a function pointer is cast to an integer type. But barring that, we
really need an intrinsic to perform this.

Also, perhaps the compiler should admit it's doing dodgy crap and
introduce the notion of address spaces and use the type system to
separate these two forms.