From: Daniel Borkmann <daniel@iogearbox.net>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>, Yonghong Song <yhs@fb.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
Kernel Team <kernel-team@fb.com>,
Andrii Nakryiko <andriin@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
Wenbo Zhang <ethercflow@gmail.com>
Subject: Re: [PATCH bpf 1/2] bpf: fix an incorrect branch elimination by verifier
Date: Tue, 30 Jun 2020 22:35:28 +0200 [thread overview]
Message-ID: <64bec9ba-1211-9412-3c34-c8c95ba364b9@iogearbox.net> (raw)
In-Reply-To: <CAEf4BzaTS3gQf0L_KhMu8b-asa3=Pq8H5f_sH=JjbWxy0Q70cQ@mail.gmail.com>
On 6/30/20 9:18 PM, Andrii Nakryiko wrote:
> On Tue, Jun 30, 2020 at 11:46 AM Yonghong Song <yhs@fb.com> wrote:
>>
>> Wenbo reported an issue in [1] where a checking of null
>> pointer is evaluated as always false. In this particular
>> case, the program type is tp_btf and the pointer to
>> compare is a PTR_TO_BTF_ID.
>>
>> The current verifier considers PTR_TO_BTF_ID always
>> reprents a non-null pointer, hence all PTR_TO_BTF_ID compares
>> to 0 will be evaluated as always not-equal, which resulted
>> in the branch elimination.
>>
>> For example,
>> struct bpf_fentry_test_t {
>> struct bpf_fentry_test_t *a;
>> };
>> int BPF_PROG(test7, struct bpf_fentry_test_t *arg)
>> {
>> if (arg == 0)
>> test7_result = 1;
>> return 0;
>> }
>> int BPF_PROG(test8, struct bpf_fentry_test_t *arg)
>> {
>> if (arg->a == 0)
>> test8_result = 1;
>> return 0;
>> }
>>
>> In above bpf programs, both branch arg == 0 and arg->a == 0
>> are removed. This may not be what developer expected.
>>
>> The bug is introduced by Commit cac616db39c2 ("bpf: Verifier
>> track null pointer branch_taken with JNE and JEQ"),
>> where PTR_TO_BTF_ID is considered to be non-null when evaluting
>> pointer vs. scalar comparison. This may be added
>> considering we have PTR_TO_BTF_ID_OR_NULL in the verifier
>> as well.
>>
>> PTR_TO_BTF_ID_OR_NULL is added to explicitly requires
>> a non-NULL testing in selective cases. The current generic
>> pointer tracing framework in verifier always
>> assigns PTR_TO_BTF_ID so users does not need to
>> check NULL pointer at every pointer level like a->b->c->d.
>>
>> We may not want to assign every PTR_TO_BTF_ID as
>> PTR_TO_BTF_ID_OR_NULL as this will require a null test
>> before pointer dereference which may cause inconvenience
>> for developers. But we could avoid branch elimination
>> to preserve original code intention.
>>
>> This patch simply removed PTR_TO_BTD_ID from reg_type_not_null()
>> in verifier, which prevented the above branches from being eliminated.
>>
>> [1]: https://lore.kernel.org/bpf/79dbb7c0-449d-83eb-5f4f-7af0cc269168@fb.com/T/
>>
>> Fixes: cac616db39c2 ("bpf: Verifier track null pointer branch_taken with JNE and JEQ")
>> Cc: Andrii Nakryiko <andriin@fb.com>
>> Cc: John Fastabend <john.fastabend@gmail.com>
>> Cc: Wenbo Zhang <ethercflow@gmail.com>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>
> You missed Reported-by: tag, please add.
Agree, fixed up manually (and also pulled in your ACKs, Andrii).
Thanks,
Daniel
next prev parent reply other threads:[~2020-06-30 20:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-30 17:12 [PATCH bpf 0/2] bpf: fix an incorrect branch elimination by verifier Yonghong Song
2020-06-30 17:12 ` [PATCH bpf 1/2] " Yonghong Song
2020-06-30 17:51 ` John Fastabend
2020-06-30 18:29 ` Yonghong Song
2020-06-30 18:35 ` John Fastabend
2020-06-30 19:16 ` Alexei Starovoitov
2020-06-30 19:20 ` Andrii Nakryiko
2020-06-30 19:18 ` Andrii Nakryiko
2020-06-30 20:35 ` Daniel Borkmann [this message]
2020-06-30 17:12 ` [PATCH bpf 2/2] bpf: add tests for PTR_TO_BTF_ID vs. null comparison Yonghong Song
2020-06-30 18:43 ` John Fastabend
2020-06-30 19:23 ` Andrii Nakryiko
2020-06-30 20:13 ` Yonghong Song
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=64bec9ba-1211-9412-3c34-c8c95ba364b9@iogearbox.net \
--to=daniel@iogearbox.net \
--cc=andrii.nakryiko@gmail.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=ethercflow@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=kernel-team@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).