Re: [PATCH 09/14] x86/ibt: Implement IBT+

From: Peter Zijlstra
Date: Mon Sep 30 2024 - 04:23:29 EST


On Sun, Sep 29, 2024 at 10:38:58AM -0700, Alexei Starovoitov wrote:
> On Fri, Sep 27, 2024 at 12:50 PM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> >
> > --- a/arch/x86/net/bpf_jit_comp.c
> > +++ b/arch/x86/net/bpf_jit_comp.c
> > @@ -555,6 +555,8 @@ static int emit_patch(u8 **pprog, void *
> >
> > static int emit_call(u8 **pprog, void *func, void *ip)
> > {
> > + if (is_endbr(func))
> > + func += ENDBR_INSN_SIZE;
> > return emit_patch(pprog, func, ip, 0xE8);
> > }
> >
> > @@ -562,11 +564,13 @@ static int emit_rsb_call(u8 **pprog, voi
> > {
> > OPTIMIZER_HIDE_VAR(func);
> > ip += x86_call_depth_emit_accounting(pprog, func, ip);
> > - return emit_patch(pprog, func, ip, 0xE8);
> > + return emit_call(pprog, func, ip);
> > }
> >
> > static int emit_jump(u8 **pprog, void *func, void *ip)
> > {
> > + if (is_endbr(func))
> > + func += ENDBR_INSN_SIZE;
> > return emit_patch(pprog, func, ip, 0xE9);
> > }
>
> Makes sense, but it feels like it's fixing the existing bug
> that we somehow didn't notice earlier?

Before all this func()+0 was a valid call address -- as it's been
forever.

While it is true that with the introduction of ENDBR some compilers will
do direct calls to func()+4 to avoid the ENDBR (less instructions, more
faster etc..) this was very much an optional thing.

Notably, up until this point we would use a 4 byte NOP to seal(*)
functions, specifically so that anybody doing direct calls to func()+0
would continue to work.

These patches however change all that by sealing with a 4 byte UD1
instruction, which makes direct calls to func()+0 fatal. As such, we
must guarantee all direct calls are to func()+4. So what used to be an
optimization is now a strict requirement.

Indirect calls still go to func()+0 (or func()-16 for FineIBT) and will
go *bang* if !ENDBR or UD1 (depending on the hardware having CET/IBT
support).

(*) with sealing we mean the explicit action of disallowing indirect
calls to a given function.