From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
X86 ML <x86@kernel.org>,
joao@overdrivepizza.com, hjl.tools@gmail.com,
Josh Poimboeuf <jpoimboe@redhat.com>,
Andrew Cooper <andrew.cooper3@citrix.com>,
LKML <linux-kernel@vger.kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Kees Cook <keescook@chromium.org>,
Sami Tolvanen <samitolvanen@google.com>,
Mark Rutland <mark.rutland@arm.com>,
alyssa.milburn@intel.com, Miroslav Benes <mbenes@suse.cz>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH v4 00/45] x86: Kernel IBT
Date: Wed, 16 Mar 2022 16:42:41 +0530 [thread overview]
Message-ID: <20220316111241.ru77bmspycbar7dx@apollo> (raw)
In-Reply-To: <YjGvauc0NYh2XXoc@hirez.programming.kicks-ass.net>
On Wed, Mar 16, 2022 at 03:05:38PM IST, Peter Zijlstra wrote:
> On Tue, Mar 15, 2022 at 10:00:43AM +0100, Peter Zijlstra wrote:
> > On Tue, Mar 15, 2022 at 02:14:02AM +0530, Kumar Kartikeya Dwivedi wrote:
> >
> > > [ Note: I have no experience with trampoline code or IBT so what follows might
> > > be incorrect. ]
> > >
> > > In case of fexit and fmod_ret, we call original function (but skip
> > > X86_PATCH_SIZE bytes), with ENDBR we must also skip those 4 bytes, but in some
> > > cases like bpf_fentry_test1, for which this test has fmod_ret prog, compiler
> > > (gcc 11) emits endbr64, but not for do_init_module, for which we do fexit.
> > >
> > > This means for do_init_module module, orig_call += X86_PATCH_SIZE +
> > > ENDBR_INSN_SIZE would skip more bytes than needed to emit call to original
> > > function, which explains why I was seeing crash in the middle of
> > > 'mov edx, 0x10' instruction.
> > >
> > > The diff below fixes the problem for me, and allows the test to pass.
> > >
> > > diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> > > index b98e1c95bcc4..760c9a3c075f 100644
> > > --- a/arch/x86/net/bpf_jit_comp.c
> > > +++ b/arch/x86/net/bpf_jit_comp.c
> > > @@ -2031,11 +2031,14 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
> > >
> > > ip_off = stack_size;
> > >
> > > - if (flags & BPF_TRAMP_F_SKIP_FRAME)
> > > + if (flags & BPF_TRAMP_F_SKIP_FRAME) {
> > > /* skip patched call instruction and point orig_call to actual
> > > * body of the kernel function.
> > > */
> > > - orig_call += X86_PATCH_SIZE + ENDBR_INSN_SIZE;
> > > + if (is_endbr(*(u32 *)orig_call))
> > > + orig_call += ENDBR_INSN_SIZE;
> > > + orig_call += X86_PATCH_SIZE;
> > > + }
> > >
> > > prog = image;
> >
> > Hmm, so I was under the impression that this was targeting the NOP from
> > emit_prologue(), and that has an unconditional ENDBR. If this is instead
> > targeting the 'start of random kernel function' then yes, what you
> > propose will work.
>
> Can you confirm that orig_call can be any kernel function? Because if
> so, I'm thinking it will still do the wrong thing for a notrace
> function, that will not have a __fentry__ site, so unconditionally
> skipping those 5 bytes will place you somewhere non-sensible.
>
It fails for notrace functions, e.g. considering fentry prog, when
bpf_trampoline_link_prog -> bpf_trampoline_update eventually calls
register_fentry -> bpf_arch_text_poke, old_addr is NULL, so nop_insn is copied
to old_insn, and then that memcmp(ip, old_insn, X86_PATCH_SIZE) should fail, so
it would return -EBUSY. If you consider modify_fentry case, then register_fentry
for earlier prog must have succeeded, so it must not be notrace function.
The orig_call adjustment is only done for fexit and fmod_ret (they set CALL_ORIG
and SKIP_FRAME flags), because in case of just fentry we just continue after
ret, instead of emitting call to original function in trampoline, for those too
the bpf_arch_text_poke should fail, for the same reason as above.
> This would not be a new issue; but perhaps it should be clarified and or
> fixed.
Based on my inspection it looks fine, others can correct me if I'm wrong.
--
Kartikeya
next prev parent reply other threads:[~2022-03-16 11:12 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20220308153011.021123062@infradead.org>
2022-03-08 20:00 ` [PATCH v4 00/45] x86: Kernel IBT Alexei Starovoitov
2022-03-08 22:01 ` Peter Zijlstra
2022-03-08 22:32 ` Peter Zijlstra
2022-03-09 1:02 ` Peter Zijlstra
2022-03-09 19:09 ` Alexei Starovoitov
2022-03-10 9:35 ` Peter Zijlstra
2022-03-10 13:47 ` Peter Zijlstra
2022-03-10 14:37 ` Steven Rostedt
2022-03-11 15:23 ` Peter Zijlstra
2022-03-10 16:29 ` Peter Zijlstra
2022-03-11 10:40 ` Peter Zijlstra
2022-03-11 17:09 ` Alexei Starovoitov
2022-03-12 15:44 ` Peter Zijlstra
2022-03-13 1:33 ` Alexei Starovoitov
2022-03-13 8:52 ` Peter Zijlstra
2022-03-14 14:59 ` Peter Zijlstra
2022-03-15 8:15 ` Peter Zijlstra
2022-03-15 16:28 ` Masahiro Yamada
2022-03-17 19:44 ` Peter Zijlstra
2022-03-18 2:07 ` David Laight
2022-03-17 18:15 ` Masahiro Yamada
2022-03-17 19:52 ` Peter Zijlstra
2022-03-15 16:26 ` Masahiro Yamada
2022-03-17 19:36 ` Peter Zijlstra
2022-03-14 15:33 ` Peter Zijlstra
2022-03-14 20:44 ` Kumar Kartikeya Dwivedi
2022-03-15 9:00 ` Peter Zijlstra
2022-03-15 10:05 ` Kumar Kartikeya Dwivedi
2022-03-15 10:07 ` Peter Zijlstra
2022-03-15 10:39 ` Peter Zijlstra
2022-03-16 9:35 ` Peter Zijlstra
2022-03-16 11:12 ` Kumar Kartikeya Dwivedi [this message]
2022-03-15 18:26 ` Alexei Starovoitov
2022-03-17 20:27 ` Peter Zijlstra
2022-03-10 0:30 ` Nick Desaulniers
2022-03-10 9:05 ` Peter Zijlstra
2022-03-10 9:22 ` David Laight
2022-03-10 10:16 ` Peter Zijlstra
2022-03-10 20:49 ` Nick Desaulniers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220316111241.ru77bmspycbar7dx@apollo \
--to=memxor@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=alyssa.milburn@intel.com \
--cc=andrew.cooper3@citrix.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=hjl.tools@gmail.com \
--cc=joao@overdrivepizza.com \
--cc=jpoimboe@redhat.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mbenes@suse.cz \
--cc=mhiramat@kernel.org \
--cc=ndesaulniers@google.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=samitolvanen@google.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox