BPF List
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
	<martin.lau@kernel.org>
Cc: <andrii@kernel.org>, <kernel-team@meta.com>,
	Eduard Zingerman <eddyz87@gmail.com>
Subject: [PATCH v2 bpf-next 08/10] selftests/bpf: validate async callback return value check correctness
Date: Tue, 28 Nov 2023 16:36:18 -0800	[thread overview]
Message-ID: <20231129003620.1049610-9-andrii@kernel.org> (raw)
In-Reply-To: <20231129003620.1049610-1-andrii@kernel.org>

Adjust timer/timer_ret_1 test to validate more carefully verifier logic
of enforcing async callback return value. This test will pass only if
return result is marked precise and read.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../selftests/bpf/progs/timer_failure.c       | 36 ++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/bpf/progs/timer_failure.c b/tools/testing/selftests/bpf/progs/timer_failure.c
index 9000da1e2120..312a7af7e072 100644
--- a/tools/testing/selftests/bpf/progs/timer_failure.c
+++ b/tools/testing/selftests/bpf/progs/timer_failure.c
@@ -21,17 +21,37 @@ struct {
 	__type(value, struct elem);
 } timer_map SEC(".maps");
 
-static int timer_cb_ret1(void *map, int *key, struct bpf_timer *timer)
+__naked __noinline __used
+static unsigned long timer_cb_ret_bad()
 {
-	if (bpf_get_smp_processor_id() % 2)
-		return 1;
-	else
-		return 0;
+	asm volatile (
+		"call %[bpf_get_prandom_u32];"
+		"if r0 > 1000 goto 1f;"
+		"r0 = 0;"
+	"1:"
+		"goto +0;" /* checkpoint */
+		/* async callback is expected to return 0, so branch above
+		 * skipping r0 = 0; should lead to a failure, but if exit
+		 * instruction doesn't enforce r0's precision, this callback
+		 * will be successfully verified
+		 */
+		"exit;"
+		:
+		: __imm(bpf_get_prandom_u32)
+		: __clobber_common
+	);
 }
 
 SEC("fentry/bpf_fentry_test1")
-__failure __msg("should have been in [0, 0]")
-int BPF_PROG2(test_ret_1, int, a)
+__log_level(2)
+__flag(BPF_F_TEST_STATE_FREQ)
+__failure
+/* check that fallthrough code path marks r0 as precise */
+__msg("mark_precise: frame0: regs=r0 stack= before 22: (b4) w0 = 0")
+/* check that branch code path marks r0 as precise */
+__msg("mark_precise: frame0: regs=r0 stack= before 24: (85) call bpf_get_prandom_u32#7")
+__msg("should have been in [0, 0]")
+int BPF_PROG2(test_bad_ret, int, a)
 {
 	int key = 0;
 	struct bpf_timer *timer;
@@ -39,7 +59,7 @@ int BPF_PROG2(test_ret_1, int, a)
 	timer = bpf_map_lookup_elem(&timer_map, &key);
 	if (timer) {
 		bpf_timer_init(timer, &timer_map, CLOCK_BOOTTIME);
-		bpf_timer_set_callback(timer, timer_cb_ret1);
+		bpf_timer_set_callback(timer, timer_cb_ret_bad);
 		bpf_timer_start(timer, 1000, 0);
 	}
 
-- 
2.34.1


  parent reply	other threads:[~2023-11-29  0:36 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-29  0:36 [PATCH v2 bpf-next 00/10] BPF verifier retval logic fixes Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 01/10] bpf: provide correct register name for exception callback retval check Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 02/10] bpf: enforce precision of R0 on callback return Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 03/10] bpf: enforce exact retval range on subprog/callback exit Andrii Nakryiko
2023-11-29  3:40   ` Andrii Nakryiko
2023-11-29 10:55   ` Shung-Hsi Yu
2023-11-29 16:23     ` Andrii Nakryiko
2023-11-30  5:23       ` Shung-Hsi Yu
2023-11-30  6:41         ` Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 04/10] selftests/bpf: add selftest validating callback result is enforced Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 05/10] bpf: enforce precise retval range on program exit Andrii Nakryiko
2023-11-29 11:23   ` Shung-Hsi Yu
2023-11-29 16:23     ` Andrii Nakryiko
2023-11-30  5:29       ` Shung-Hsi Yu
2023-11-29  0:36 ` [PATCH v2 bpf-next 06/10] bpf: unify async callback and program retval checks Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 07/10] bpf: enforce precision of R0 on program/async callback return Andrii Nakryiko
2023-11-29  0:36 ` Andrii Nakryiko [this message]
2023-11-29  0:36 ` [PATCH v2 bpf-next 09/10] selftests/bpf: adjust global_func15 test to validate prog exit precision Andrii Nakryiko
2023-11-29  0:36 ` [PATCH v2 bpf-next 10/10] bpf: simplify tnum output if a fully known constant Andrii Nakryiko
2023-11-29 11:27 ` [PATCH v2 bpf-next 00/10] BPF verifier retval logic fixes Shung-Hsi Yu
2023-11-29 16:23   ` Andrii Nakryiko

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=20231129003620.1049610-9-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=martin.lau@kernel.org \
    /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