Skip to contentSkip to conversation

selftests/bpf: add negative tests for relaxed fixed offset constraint on trusted pointer arguments

PATCH In review Matt Bobrowski 2 participants 2 messages Jun 17, 2024 – Jun 19, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 selftests/bpf: add negative tests for relaxed fixed offset constraint on trusted pointer arguments

Adding some new negative selftests which are responsible for asserting that the new relaxed fixed offset constraints applicable to BPF helpers and kfuncs taking trusted pointer arguments are enforced correctly by the BPF verifier.

The BPF programs contained within the new negative selftests are mainly responsible for triggering the various branches and checks performed within the check_release_arg_reg_off() helper.

---
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../bpf/progs/verifier_arg_reg_off_reject.c   | 154 ++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arg_reg_off_reject.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 6816ff064516..e315bd0a1502 100644
@@ -87,6 +87,7 @@
 #include "verifier_xdp.skel.h"
 #include "verifier_xdp_direct_packet_access.skel.h"
 #include "verifier_bits_iter.skel.h"
+#include "verifier_arg_reg_off_reject.skel.h"
 
 #define MAX_ENTRIES 11
 
@@ -204,6 +205,7 @@ void test_verifier_xadd(void)                 { RUN(verifier_xadd); }
 void test_verifier_xdp(void)                  { RUN(verifier_xdp); }
 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); }
 void test_verifier_bits_iter(void) { RUN(verifier_bits_iter); }
+void test_verifier_arg_reg_off_reject(void) { RUN(verifier_arg_reg_off_reject); }
 
 static int init_test_val_map(struct bpf_object *obj, char *map_name)
 {
tools/testing/selftests/bpf/progs/verifier_arg_reg_off_reject.c new file +154 -0

154 lines not on this page · raw message

Signature · 3 lines
2.45.2.627.g7a2c4fd464-goog

/M
maintainer Acked-by on v1 2/2
#1 lore raw
Quoted 6 lines · Matt Bobrowski

Adding some new negative selftests which are responsible for asserting that the new relaxed fixed offset constraints applicable to BPF helpers and kfuncs taking trusted pointer arguments are enforced correctly by the BPF verifier.

Quoted 6 lines

The BPF programs contained within the new negative selftests are mainly responsible for triggering the various branches and checks performed within the check_release_arg_reg_off() helper.

---

With the stuff below addressed, please add:

I believe sockmap map_update and dynptr ones are also providing positive coverage (given previous versions made them fail), so I think focusing on negative cases is good enough here. I guess bpf_sk_release is also covered by existing ones.

Still it'd be good to add a few more cases if you find any, as Eduard suggested, just to make sure we're not missing anything.

Quoted 4 lines

[...]

tools/testing/selftests/bpf/progs/verifier_arg_reg_off_reject.c new file +3 -0
+SEC("tp_btf/task_newtask")
+__failure
+__msg("variable trusted_ptr_ access var_off=(0x0; 0xffffffffffffffff) disallowed")

tools/testing/selftests/bpf/progs/verifier_arg_reg_off_reject.c line 142

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_arg_reg_off_reject.c new file +10 -0
+int BPF_PROG(trusted_type_match_mismatch_neg_var_off, struct task_struct *task,
+            u64 clone_flags)
+{
+       s64 var_off = task->start_time;
+       task = bpf_get_current_task_btf();
+
+       bpf_assert_range(var_off, -64, 64);
+       /* Need one bpf_throw() reference, otherwise BTF gen fails. */
+       if (!task)
+               bpf_throw(1);

It seems s390x is failing because it doesn't have support for throw. So I'd just drop this selftest, since on second thought I think it doesn't provide any additional coverage than the pos var_off one.

Quoted 16 lines
tools/testing/selftests/bpf/progs/verifier_arg_reg_off_reject.c new file +12 -1
+
+       task = (void *)task + var_off;
+       /* Passing a trusted pointer with an incorrect variable offset, type
+        * match will succeed due to reg->off == 0, but the later call to
+        * __check_ptr_off_reg should fail as it's responsible for checking
+        * reg->var_off.
+        */
+       task = bpf_task_acquire(task);
+       return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.45.2.627.g7a2c4fd464-goog

/M