[PATCH bpf-next v1 11/13] selftests/bpf: Add dynptr var_off tests

Kumar Kartikeya Dwivedi
2022-10-18T14:00:20Z

Ensure that variable offset is handled correctly, and verifier takes
both fixed and variable part into account. Also ensure that only
constant var_off is allowed.

Make sure that unprivileged BPF cannot use var_off for dynptr.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
tools/testing/selftests/bpf/verifier/dynptr.c | 38 ++++++++++++++++++-
1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/verifier/dynptr.c b/tools/testing/selftests/bpf/verifier/dynptr.c
index 798f4f7e0c57..1aa7241e8a9e 100644
--- a/tools/testing/selftests/bpf/verifier/dynptr.c
+++ b/tools/testing/selftests/bpf/verifier/dynptr.c
@@ -1,5 +1,5 @@
{
- "dynptr: rewrite dynptr slot",
+ "dynptr: rewrite dynptr slot (pruning)",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_LD_MAP_FD(BPF_REG_6, 0),
@@ -26,7 +26,7 @@
.errstr = "arg 1 is an unacquired reference",
},
{
- "dynptr: type confusion",
+ "dynptr: type confusion (pruning)",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_LD_MAP_FD(BPF_REG_6, 0),
@@ -88,3 +88,37 @@
.result = REJECT,
.errstr = "arg 1 is an unacquired reference",
},
+{
+ "dynptr: rewrite dynptr slot (var_off)",
+ .insns = {
+ BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 16),
+ BPF_LDX_MEM(BPF_W, BPF_REG_8, BPF_REG_10, -4),
+ BPF_JMP_IMM(BPF_JGE, BPF_REG_8, 0, 2),
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ BPF_JMP_IMM(BPF_JLE, BPF_REG_8, 16, 2),
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ BPF_ALU64_IMM(BPF_AND, BPF_REG_8, 16),
+ BPF_LD_MAP_FD(BPF_REG_1, 0),
+ BPF_MOV64_IMM(BPF_REG_2, 8),
+ BPF_MOV64_IMM(BPF_REG_3, 0),
+ BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -32),
+ BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_8),
+ BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_ringbuf_reserve_dynptr),
+ BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, 0xeB9F),
+ BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -32),
+ BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_8),
+ BPF_MOV64_IMM(BPF_REG_2, 0),
+ BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_ringbuf_discard_dynptr),
+ BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+ BPF_EXIT_INSN(),
+ },
+ .fixup_map_ringbuf = { 9 },
+ .result_unpriv = REJECT,
+ .errstr_unpriv = "R4 variable stack access prohibited for !root, var_off=(0x0; 0x10) off=-32",
+ .result = REJECT,
+ .errstr = "dynptr has to be at the constant offset",
+},
--
2.38.0