linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Sami Tolvanen <samitolvanen@google.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>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Björn Töpel" <bjorn.topel@intel.com>
Subject: Re: [PATCH] bpf: validate bpf_func when BPF_JIT is enabled
Date: Wed, 11 Sep 2019 07:42:39 +0000	[thread overview]
Message-ID: <c7c7668e-6336-0367-42b3-2f6026c466dd@fb.com> (raw)
In-Reply-To: <20190910172253.GA164966@google.com>



On 9/10/19 6:22 PM, Sami Tolvanen wrote:
> 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

Indeed, I made a mistake. Your below change is correct.

> 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.

How about this:

	if (!IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) && !prog->jited)
		goto out;

	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;
	}
out:
	return prog->bpf_func(ctx, prog->insnsi);

> 
>> 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?

I am not an expert in XDP testing. Toke, Björn, could you give some
suggestions what to test for XDP performance here?

> 
> Sami
> 

  reply	other threads:[~2019-09-11  7:43 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
2019-09-11  7:42     ` Yonghong Song [this message]
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=c7c7668e-6336-0367-42b3-2f6026c466dd@fb.com \
    --to=yhs@fb.com \
    --cc=ast@kernel.org \
    --cc=bjorn.topel@intel.com \
    --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=samitolvanen@google.com \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.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).