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 4/4] selftests/bpf: check env->that range_transfer_ids has effect
Date: Tue, 30 May 2023 20:27:39 +0300 [thread overview]
Message-ID: <20230530172739.447290-5-eddyz87@gmail.com> (raw)
In-Reply-To: <20230530172739.447290-1-eddyz87@gmail.com>
Previous commit adds bpf_verifier_env::range_transfer_ids check to
verifier.c:regsafe(). This check allows to skip check_ids() for some
ids in the cached verifier state and thus improves verification
performance.
This commit adds two test cases:
- first: showing that check_ids() is indeed skipped as expected;
- second: modification of first where check_ids() cannot be skipped.
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
.../selftests/bpf/progs/verifier_scalar_ids.c | 106 ++++++++++++++++++
1 file changed, 106 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/verifier_scalar_ids.c b/tools/testing/selftests/bpf/progs/verifier_scalar_ids.c
index 0ea9a1f6e1ae..2c5bb72696ce 100644
--- a/tools/testing/selftests/bpf/progs/verifier_scalar_ids.c
+++ b/tools/testing/selftests/bpf/progs/verifier_scalar_ids.c
@@ -105,4 +105,110 @@ __naked void ids_id_mapping_in_regsafe_2(void)
: __clobber_all);
}
+/* Label l1 could be reached in two combinations:
+ *
+ * (1) r6{.id=A}, r7{.id=A}, r8{.id=B}
+ * (2) r6{.id=B}, r7{.id=A}, r8{.id=B}
+ *
+ * However neither A nor B are used in find_equal_scalars()
+ * to transfer range information in this test.
+ * Thus states (1) and (2) should be considered identical due
+ * to bpf_verifier_env::range_transfer_ids handling.
+ *
+ * Make sure that this is the case by checking that second jump
+ * to l1 hits cached state.
+ */
+SEC("socket")
+__success __log_level(7) __msg("14: safe")
+__flag(BPF_F_TEST_STATE_FREQ)
+__naked void no_range_transfer_ids(void)
+{
+ asm volatile (
+ /* Bump allocated stack */
+ "r1 = 0;"
+ "*(u64*)(r10 - 16) = r1;"
+ /* r9 = pointer to stack */
+ "r9 = r10;"
+ "r9 += -16;"
+ /* r7 = ktime_get_ns() & 0b11 */
+ "call %[bpf_ktime_get_ns];"
+ "r8 = r0;"
+ "r8 &= 3;"
+ /* r6 = ktime_get_ns() & 0b11 */
+ "call %[bpf_ktime_get_ns];"
+ "r7 = r0;"
+ "r7 &= 3;"
+ /* if r6 > r7 is an unpredictable jump */
+ "if r7 > r8 goto l0_%=;"
+ "r6 = r7;"
+ "goto l1_%=;"
+"l0_%=:"
+ "r6 = r8;"
+"l1_%=:"
+ /* insn #14 */
+ "r9 += r6;"
+ "r9 += r7;"
+ "r9 += r8;"
+ "r0 = *(u8*)(r9 + 0);"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_ktime_get_ns)
+ : __clobber_all);
+}
+
+/* Same as above, but cached state for l1 has id used for
+ * range transfer:
+ *
+ * (1) r6{.id=A}, r7{.id=A}, r8{.id=B}
+ * (2) r6{.id=B}, r7{.id=A}, r8{.id=B}
+ *
+ * If (A) is used for range transfer (1) and (2) should not
+ * be considered identical.
+ *
+ * Check this by verifying that instruction immediately following l1
+ * is visited twice.
+ */
+SEC("socket")
+__success __log_level(7) __msg("r9 = r9") __msg("r9 = r9")
+__flag(BPF_F_TEST_STATE_FREQ)
+__naked void has_range_transfer_ids(void)
+{
+ asm volatile (
+ /* Bump allocated stack */
+ "r1 = 0;"
+ "*(u64*)(r10 - 16) = r1;"
+ /* r9 = pointer to stack */
+ "r9 = r10;"
+ "r9 += -16;"
+ /* r7 = ktime_get_ns() & 0b11 */
+ "call %[bpf_ktime_get_ns];"
+ "r8 = r0;"
+ /* r6 = ktime_get_ns() & 0b11 */
+ "call %[bpf_ktime_get_ns];"
+ "r7 = r0;"
+ /* if r6 > r7 is an unpredictable jump */
+ "if r7 > r8 goto l0_%=;"
+ "r6 = r7;"
+ "goto l1_%=;"
+"l0_%=:"
+ "r6 = r8;"
+"l1_%=:"
+ /* just a unique marker, this insn should be verified twice */
+ "r9 = r9;"
+ /* one of the instructions below transfers range for r6 */
+ "if r7 > 2 goto l2_%=;"
+ "if r8 > 2 goto l2_%=;"
+ "r9 += r6;"
+ "r9 += r7;"
+ "r9 += r8;"
+ "r0 = *(u8*)(r9 + 0);"
+"l2_%=:"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_ktime_get_ns)
+ : __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
--
2.40.1
prev parent 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 [PATCH bpf-next v2 0/4] verify scalar ids mapping in regsafe() Eduard Zingerman
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 ` Eduard Zingerman [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=20230530172739.447290-5-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox