From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev,
kernel-team@fb.com, yhs@fb.com,
Eduard Zingerman <eddyz87@gmail.com>
Subject: [PATCH bpf-next v2 0/4] verify scalar ids mapping in regsafe()
Date: Tue, 30 May 2023 20:27:35 +0300 [thread overview]
Message-ID: <20230530172739.447290-1-eddyz87@gmail.com> (raw)
Update regsafe() to use check_ids() for scalar values.
Otherwise the following unsafe pattern is accepted by verifier:
1: r9 = ... some pointer with range X ...
2: r6 = ... unbound scalar ID=a ...
3: r7 = ... unbound scalar ID=b ...
4: if (r6 > r7) goto +1
5: r6 = r7
6: if (r6 > X) goto ...
--- checkpoint ---
7: r9 += r7
8: *(u64 *)r9 = Y
This example is unsafe because not all execution paths verify r7 range.
Because of the jump at (4) the verifier would arrive at (6) in two states:
I. r6{.id=b}, r7{.id=b} via path 1-6;
II. r6{.id=a}, r7{.id=b} via path 1-4, 6.
Currently regsafe() does not call check_ids() for scalar registers,
thus from POV of regsafe() states (I) and (II) are identical.
The change is split in two parts:
- patches #1,2:
- correctness fix for regsafe();
- test cases.
Patch #1 has a big hit on verification performance.
- patches #3,4
- modification for find_equal_scalars() to save ids of registers
that gain range via this function;
- modification for regsafe() to do check_ids() for scalar registers
only for such ids;
- test cases.
Patch #3 restores most of the verification performance.
A note on design decisions for patch #3. The change in patch #1 is
simple but very heavy handed:
@@ -15151,6 +15151,28 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
switch (base_type(rold->type)) {
case SCALAR_VALUE:
+ /* ... */
+ if (!check_ids(rold->id, rcur->id, idmap))
+ return false;
Ideally check_ids() should only be called for 'rold' that either:
(a) gained range via find_equal_scalars() in some child verification
path and was then marked as precise;
(b) was a source of range information for some other register via
find_equal_scalars() in some child verification path, and that
register was then marked as precise.
While rold->precise flag could be a proxy for criteria (a) it is,
unfortunately, cannot be a proxy for criteria (b). E.g. for the
example above precision marks look as follows:
Precise registers
5: r6 = r7 ; r7
6: if (r6 > X) goto ... ; r7
7: r9 += r7 ; r7
Jump at (6) cannot be predicted, thus there is no precision mark on r6.
If there is ever a jump to (6), cached state will not have precise
marks for r6.
This leaves two options:
- Modification of precision tracking to take find_equal_scalars() into
account.
- Find a way to track which ids were used for range information
transfer in find_equal_scalars().
The former is a bit complicated, because information about register id
assignments for instructions in the middle of a state is lost.
It is possible to extend bpf_verifier_state::jmp_history to track a
mask for registers / stack slots that gained range via
find_equal_scalars() and use this mask in backtrack_insn().
However, this is a significant complication for a very non-trivial code.
Thus, in patch #3 I opted for a latter approach, accumulate all ids
that gain range via find_equal_scalars() in a set stored in struct
bpf_verifier_env.
To represent this set I use a u32_hashset data structure derived from
tools/lib/bpf/hashmap.h. I tested it locally (see [1]), but I think
that ideally it should be tested using KUnit. However, AFAIK, this
would be the first use of KUnit in context of BPF verifier.
If people are ok with this, I will prepare the tests and necessary
CI integration.
Changelog:
- V1 -> v2:
- 'rold->precise' and 'rold->id' checks are dropped as unsafe
(thanks to discussion with Yonghong);
- patches #3,4 adding tracking of ids used for range transfer in
order to mitigate performance impact.
- RFC -> V1:
- Function verifier.c:mark_equal_scalars_as_read() is dropped,
as it was an incorrect fix for problem solved by commit [3].
- check_ids() is called only for precise scalar values.
- Test case updated to use inline assembly.
[V1] https://lore.kernel.org/bpf/20230526184126.3104040-1-eddyz87@gmail.com/
[RFC] https://lore.kernel.org/bpf/20221128163442.280187-1-eddyz87@gmail.com/
[1] https://gist.github.com/eddyz87/a32ea7e62a27d3c201117c9a39ab4286
Eduard Zingerman (4):
bpf: verify scalar ids mapping in regsafe() using check_ids()
selftests/bpf: verify that check_ids() is used for scalars in
regsafe()
bpf: filter out scalar ids not used for range transfer in regsafe()
selftests/bpf: check env->that range_transfer_ids has effect
include/linux/bpf_verifier.h | 4 +
kernel/bpf/Makefile | 1 +
kernel/bpf/u32_hashset.c | 137 +++++++++++
kernel/bpf/u32_hashset.h | 30 +++
kernel/bpf/verifier.c | 66 +++++-
.../selftests/bpf/prog_tests/verifier.c | 2 +
.../selftests/bpf/progs/verifier_scalar_ids.c | 214 ++++++++++++++++++
7 files changed, 447 insertions(+), 7 deletions(-)
create mode 100644 kernel/bpf/u32_hashset.c
create mode 100644 kernel/bpf/u32_hashset.h
create mode 100644 tools/testing/selftests/bpf/progs/verifier_scalar_ids.c
--
2.40.1
next reply other threads:[~2023-05-30 17:28 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-30 17:27 Eduard Zingerman [this message]
2023-05-30 17:27 ` [PATCH bpf-next v2 1/4] bpf: verify scalar ids mapping in regsafe() using check_ids() Eduard Zingerman
2023-05-30 21:37 ` Andrii Nakryiko
2023-05-30 23:02 ` Eduard Zingerman
2023-06-01 2:05 ` Alexei Starovoitov
2023-06-01 16:57 ` Eduard Zingerman
2023-06-01 17:13 ` Alexei Starovoitov
2023-06-01 18:52 ` Eduard Zingerman
2023-06-02 18:50 ` Andrii Nakryiko
2023-06-02 19:13 ` Eduard Zingerman
2023-06-02 19:27 ` Alexei Starovoitov
2023-06-02 19:37 ` Eduard Zingerman
2023-06-02 19:43 ` Alexei Starovoitov
2023-06-02 19:51 ` Eduard Zingerman
2023-06-02 20:03 ` Alexei Starovoitov
2023-06-02 20:22 ` Eduard Zingerman
2023-06-02 20:17 ` Andrii Nakryiko
2023-06-02 21:13 ` Eduard Zingerman
2023-06-02 22:07 ` Andrii Nakryiko
2023-06-02 23:20 ` Eduard Zingerman
2023-06-05 20:43 ` Eduard Zingerman
[not found] ` <f2abf39bcd4de841a89bb248de9e242a880aaa93.camel@gmail.com>
2023-05-31 18:29 ` Andrii Nakryiko
2023-05-31 19:30 ` Eduard Zingerman
2023-05-31 20:12 ` Andrii Nakryiko
2023-05-31 20:31 ` Eduard Zingerman
2023-05-31 21:14 ` Andrii Nakryiko
2023-05-31 22:15 ` Eduard Zingerman
2023-05-31 22:54 ` Andrii Nakryiko
2023-05-31 23:42 ` Eduard Zingerman
2023-06-01 0:23 ` Andrii Nakryiko
2023-05-30 17:27 ` [PATCH bpf-next v2 2/4] selftests/bpf: verify that check_ids() is used for scalars in regsafe() Eduard Zingerman
2023-05-30 17:27 ` [PATCH bpf-next v2 3/4] bpf: filter out scalar ids not used for range transfer " Eduard Zingerman
2023-05-30 17:27 ` [PATCH bpf-next v2 4/4] selftests/bpf: check env->that range_transfer_ids has effect Eduard Zingerman
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=20230530172739.447290-1-eddyz87@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@linux.dev \
--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.