From: Paul Chaignon <paul.chaignon@orange.com>
To: Yonghong Song <yhs@fb.com>, Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
netdev@vger.kernel.org, bpf@vger.kernel.org
Cc: Xiao Han <xiao.han@orange.com>, Martin KaFai Lau <kafai@fb.com>,
Song Liu <songliubraving@fb.com>
Subject: Re: [PATCH bpf 0/2] bpf: mark registers as safe or unknown in all frames
Date: Mon, 22 Apr 2019 20:30:15 +0200 [thread overview]
Message-ID: <20190422183014.GA12006@Nover> (raw)
In-Reply-To: <83ac6441-526c-94bd-c818-3316809f25c3@fb.com>
On Mon, Apr 22, 2019 04:57PM, Yonghong Song wrote:
> On 4/20/19 5:38 AM, Paul Chaignon wrote:
> > In case of a null check on a pointer inside a subprog, we should mark all
> > registers with this pointer as either safe or unknown, in both the current
> > and previous frames. Currently, only spilled registers and registers in
> > the current frame are marked. This first patch also marks registers in
> > previous frames.
> >
> > A good reproducer looks as follow:
> >
> > 1: ptr = bpf_map_lookup_elem(map, &key);
> > 2: ret = subprog(ptr) {
> > 3: return ptr != NULL;
> > 4: }
> > 5: if (ret)
> > 6: value = *ptr;
> >
> > With the above, the verifier will complain on line 6 because it sees ptr
> > as map_value_or_null despite the null check in subprog 1. The second
> > patch implements the above as a new test case.
> >
> > Note that this patch fixes another resulting bug when using
> > bpf_sk_release():
> >
> > 1: sk = bpf_sk_lookup_tcp();
> > 2: subprog(sk) {
> > 3: if (sk)
> > 4: bpf_sk_release(sk, 0);
>
> The specification for bpf_sk_release() in uapi/linux/bpf.h is:
> int bpf_sk_release(struct bpf_sock *sock)
>
> Do you explain what is bpf_sk_release(sk, 0)?
Thanks for the review Yonghong. I think I took this extra argument by
mistake from the description of the commit introducing bpf_sk_release
(6acc9b432 ("bpf: Add helper to retrieve socket in BPF")). I'll send a v2
with a fixed commit message. Of course, the helper on line 1 also takes
arguments, so it might be better to write it as bpf_sk_lookup_tcp(...).
>
> > 5: }
> > 6: if (!sk)
> > 7: return 0;
> > 8: return sk;
>
> If sk has been released, the program should not really return sk, right?
I'll change in v2. I don't think it matters to reproduce the warning
though. The verifier won't complain as the return value won't be
dereferenced and the register holding sk is readable.
>
> >
> > In the above, mark_ptr_or_null_regs will warn on line 6 because it will
> > try to free the reference state, even though it was already freed on
> > line 3.
> >
> > Paul Chaignon (2):
> > bpf: mark registers as safe or unknown in all frames
> > selftests/bpf: test case for pointer null check in subprog
> >
> > kernel/bpf/verifier.c | 6 ++---
> > tools/testing/selftests/bpf/verifier/calls.c | 25 ++++++++++++++++++++
> > 2 files changed, 28 insertions(+), 3 deletions(-)
> >
prev parent reply other threads:[~2019-04-22 18:30 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-20 12:38 [PATCH bpf 0/2] bpf: mark registers as safe or unknown in all frames Paul Chaignon
2019-04-20 12:39 ` [PATCH bpf 1/2] " Paul Chaignon
2019-04-22 16:58 ` Yonghong Song
2019-04-20 12:40 ` [PATCH bpf 2/2] selftests/bpf: test case for pointer null check in subprog Paul Chaignon
2019-04-22 16:57 ` [PATCH bpf 0/2] bpf: mark registers as safe or unknown in all frames Yonghong Song
2019-04-22 18:30 ` Paul Chaignon [this message]
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=20190422183014.GA12006@Nover \
--to=paul.chaignon@orange.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=xiao.han@orange.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.