From: Sami Tolvanen <samitolvanen@google.com>
To: Yonghong Song <yhs@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Kees Cook <keescook@chromium.org>, Martin Lau <kafai@fb.com>,
Song Liu <songliubraving@fb.com>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] bpf: validate bpf_func when BPF_JIT is enabled
Date: Tue, 10 Sep 2019 10:22:53 -0700 [thread overview]
Message-ID: <20190910172253.GA164966@google.com> (raw)
In-Reply-To: <4f4136f5-db54-f541-2843-ccb35be25ab4@fb.com>
On Tue, Sep 10, 2019 at 08:37:19AM +0000, Yonghong Song wrote:
> You did not mention BPF_BINARY_HEADER_MAGIC and added member
> of `magic` in bpf_binary_header. Could you add some details
> on what is the purpose for this `magic` member?
Sure, I'll add a description to the next version.
The magic is a random number used to identify bpf_binary_header in
memory. The purpose of this patch is to limit the possible call
targets for the function pointer and checking for the magic helps
ensure we are jumping to a page that contains a jited function,
instead of allowing calls to arbitrary targets.
This is particularly useful when combined with the compiler-based
Control-Flow Integrity (CFI) mitigation, which Google started shipping
in Pixel kernels last year. The compiler injects checks to all
indirect calls, but cannot obviously validate jumps to dynamically
generated code.
> > +unsigned int bpf_call_func(const struct bpf_prog *prog, const void *ctx)
> > +{
> > + const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
> > +
> > + if (!IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) && !prog->jited)
> > + return prog->bpf_func(ctx, prog->insnsi);
> > +
> > + if (unlikely(hdr->magic != BPF_BINARY_HEADER_MAGIC ||
> > + !arch_bpf_jit_check_func(prog))) {
> > + WARN(1, "attempt to jump to an invalid address");
> > + return 0;
> > + }
> > +
> > + return prog->bpf_func(ctx, prog->insnsi);
> > +}
> The above can be rewritten as
> if (IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) || prog->jited ||
> hdr->magic != BPF_BINARY_HEADER_MAGIC ||
> !arch_bpf_jit_check_func(prog))) {
> WARN(1, "attempt to jump to an invalid address");
> return 0;
> }
That doesn't look quite equivalent, but yes, this can be rewritten as a
single if statement like this:
if ((IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) ||
prog->jited) &&
(hdr->magic != BPF_BINARY_HEADER_MAGIC ||
!arch_bpf_jit_check_func(prog)))
I think splitting the interpreter and JIT paths would be more readable,
but I can certainly change this if you prefer.
> BPF_PROG_RUN() will be called during xdp fast path.
> Have you measured how much slowdown the above change could
> cost for the performance?
I have not measured the overhead, but it shouldn't be significant. Is
there a particular benchmark you'd like me to run?
Sami
next prev parent reply other threads:[~2019-09-10 17:23 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-09 22:32 [PATCH] bpf: validate bpf_func when BPF_JIT is enabled Sami Tolvanen
2019-09-10 8:37 ` Yonghong Song
2019-09-10 17:22 ` Sami Tolvanen [this message]
2019-09-11 7:42 ` Yonghong Song
2019-09-11 10:39 ` Björn Töpel
2019-09-11 12:09 ` Toke Høiland-Jørgensen
2019-09-11 21:07 ` Sami Tolvanen
2019-09-12 10:46 ` Toke Høiland-Jørgensen
2019-09-12 22:01 ` Sami Tolvanen
2019-09-13 12:19 ` Toke Høiland-Jørgensen
2019-09-11 20:29 ` Sami Tolvanen
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=20190910172253.GA164966@google.com \
--to=samitolvanen@google.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=yhs@fb.com \
/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;
as well as URLs for NNTP newsgroup(s).