Re: [RFC PATCH 01/11] x86: kernel FineIBT

From: Josh Poimboeuf
Date: Thu Apr 28 2022 - 21:37:16 EST


On Tue, Apr 19, 2022 at 05:42:31PM -0700, joao@xxxxxxxxxxxxxxxxxx wrote:
> +void __noendbr __fineibt_handler(void){
> + unsigned i;
> + unsigned long flags;
> + bool skip;
> + void * ret;
> + void * caller;
> +
> + DO_ALL_PUSHS;

So this function isn't C ABI compliant, right? e.g. the compiler just
calls the handler without regard for preserving registers?

If this function is going to be implemented in C, it should probably
have an asm thunk wrapper which can properly save/restore the registers
before calling into the C version.

Even better, if the compiler did an invalid op (UD2?), which I think you
mentioned elsewhere, instead of calling the handler directly, and there
were a way for the trap code to properly detect it as a FineIBT
violation, we could get rid of the pushes/pops, plus the uaccess objtool
warning from patch 7, plus I'm guessing a bunch of noinstr validation
warnings.

> +
> + spin_lock_irqsave(&fineibt_lock, flags);
> + skip = false;
> +
> + asm("\t movq 0x90(%%rsp),%0" : "=r"(ret));
> + asm("\t movq 0x98(%%rsp),%0" : "=r"(caller));

This is making some questionable assumptions about the stack layout.

I assume this function is still in the prototype stage ;-)

> + if(!skip) {
> + printk("FineIBT violation: %px:%px:%u\n", ret, caller,
> + vlts_next);
> + }
> + DO_ALL_POPS;
> +}

Right now this handler just does a printk if it hasn't already for this
caller/callee combo, and then resumes control. Which is fine for
debugging, but it really needs to behave similarly to an IBT violation,
by panicking unless "ibt=warn" on the cmdline.

Not sure what would happen for "ibt=off"? Maybe apply_ibt_endbr() could
NOP out all the FineIBT stuff.

--
Josh