Re: [PATCH v2 08/11] arm64/static_call: Fix static call CFI violations

Josh Poimboeuf
2023-03-22T18:09:47Z

On Wed, Mar 22, 2023 at 12:22:07PM +0000, Mark Rutland wrote:
> > The problem is that the __perf_guest_state() static call does an
> > indirect branch to __static_call_return0(), which isn't CFI-compliant.
>
> IIUC that'd be broken even with the old CFI mechanism, since commit:
>
> 87b940a0675e2526 ("perf/core: Use static_call to optimize perf_guest_info_callbacks")
>
> If so, we probably want a Fixes tag?

Yeah, it should definitely get a Fixes tag. I wasn't quite sure if this
bug started with the above commit or with the CFI_CLANG switch to kcfi.
And then I forgot to investigate.

> > +/* Generate a CFI-compliant static call NOP function */
> > +#define __ARCH_DEFINE_STATIC_CALL_CFI(name, insns) \
> > + asm(".align 4 \n" \
> > + ".word __kcfi_typeid_" name " \n" \
> > + ".globl " name " \n" \
> > + name ": \n" \
> > + "bti c \n" \
> > + insns " \n" \
> > + "ret \n" \
> > + ".type " name ", @function \n" \
> > + ".size " name ", . - " name " \n")
> > +
> > +#define __ARCH_DEFINE_STATIC_CALL_RET0_CFI(name) \
> > + GEN_CFI_SYM(STATIC_CALL_RET0_CFI(name)); \
> > + __ARCH_DEFINE_STATIC_CALL_CFI(STATIC_CALL_RET0_CFI_STR(name), "mov x0, xzr")
>
> This looks correct, but given we're generating a regular functions it's
> unfortunate we can't have the compiler generate the actual code with something
> like:
>
> #define __ARCH_DEFINE_STATIC_CALL_RET0_CFI(rettype, name, args...) \
> rettype name(args) \
> { \
> return (rettype)0; \
> }
>
> ... but I guess passing the rettype and args around is painful.

Hm, I hadn't considered that. I'll play around with it.

> Regardless, I gave this a spin atop v6.3-rc3 using LLVM 16.0.0 and CFI_CLANG,
> and it does seem to work, so:
>
> Tested-by: Mark Rutland <mark.rutland@arm.com>

Thanks!

--
Josh