From: Alexei Starovoitov Tests for memory locations in scev.c: a jump that tests what was loaded or stored through a pointer refines the stack slot of the caller that the pointer points to. They use LINKED() of verifier_scev.c: r6 is a map value of 64 bytes, r8 an unbounded scalar. What loads is run. - A static subprog clamps memory of its caller in place, 'if (b->len > 63) b->len = 0;', and the caller takes b.len for an index. Negative test: the subprog stores to b->len between load and jump. - The subprog tests what it stored to b->len. - It stores to the next field between load and jump: the same address base, no overlap. Negative test: it stores through a second pointer to b->len, another address base. - A bpf_loop() callback tests a field of its ctx, which is on the stack of the caller, calls a helper and loads the field again. Negative test: the field is written between load and jump. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/progs/verifier_scev.c | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/progs/verifier_scev.c b/tools/testing/selftests/bpf/progs/verifier_scev.c index 3588a0a005e2..0f737ca6bd07 100644 --- a/tools/testing/selftests/bpf/progs/verifier_scev.c +++ b/tools/testing/selftests/bpf/progs/verifier_scev.c @@ -3365,7 +3365,7 @@ __naked void name(void) \ : \ : __imm(bpf_map_lookup_elem), __imm(bpf_get_prandom_u32), __imm(bpf_ktime_get_ns), \ __imm(bpf_dynptr_from_skb), __imm(bpf_dynptr_slice_rdwr), \ - __imm(bpf_ringbuf_reserve), __imm(bpf_ringbuf_discard), \ + __imm(bpf_ringbuf_reserve), __imm(bpf_ringbuf_discard), __imm(bpf_loop), \ __imm_addr(map64), __imm_addr(ringbuf) \ : __clobber_all); \ } @@ -3393,6 +3393,67 @@ LINKED(index_is_empty, VALID __flag(BPF_F_TEST_REG_INVARIANTS), "r7 &= 1; r7 *= 7; r7 += -2; r1 = r6; r1 += r7; r1 += 2; if r7 & 1 goto l_%=; goto out_%=;" "l_%=: if r7 & 2 goto m_%=; goto out_%=; m_%=: r1 += 1;" USE_R6) +/* + * static void clamp(struct buf *b) { if (b->len > 63) b->len = 0; } + * clamp(&b); c = b.data[b.len]; + * The callee tests memory of its caller: the slot is refined. Not if it may have been written since + * the load. r1 and r4 point to b.len, r2 is not bounded. + */ +#define CLAMP(name, before_the_jump) \ +static __naked __noinline __used void name(void) \ +{ \ + asm volatile ( \ + "r3 = *(u64 *)(r1 + 0);" before_the_jump \ + "if r3 <= 63 goto +2; r3 = 0; *(u64 *)(r1 + 0) = r3; r0 = 0; exit;" \ + ::: __clobber_all); \ +} +#define CALL(callee) \ + "*(u64 *)(r10 - 16) = r8; r1 = r10; r1 += -16; r4 = r1; r2 = r8; r2 >>= 1;" \ + "call " #callee "; r2 = *(u64 *)(r10 - 16); r1 = r6; r1 += r2;" + +CLAMP(clamp, "") +LINKED(callee_tests_memory_of_caller, VALID, CALL(clamp)) +CLAMP(store_and_clamp, "*(u64 *)(r1 + 0) = r2;") +LINKED(callee_writes_memory_of_caller, __failure, CALL(store_and_clamp)) +/* a store defines the memory */ +CLAMP(store_and_test, "r3 = r2; *(u64 *)(r1 + 0) = r3;") +LINKED(callee_tests_what_it_stored, VALID, CALL(store_and_test)) +/* the same address base, no overlap */ +CLAMP(store_next_and_clamp, "*(u64 *)(r1 + 8) = r2;") +LINKED(callee_writes_next_field, VALID, CALL(store_next_and_clamp)) +/* another address base can be another name of the memory */ +CLAMP(store_alias_and_clamp, "*(u64 *)(r4 + 0) = r2;") +LINKED(callee_writes_through_alias, __failure, CALL(store_alias_and_clamp)) + +/* + * struct { char *buf; u64 len; } c = { buf, 0 }; bpf_loop(100, append, &c, 0); + * append: if (c->len > 63) return 1; bpf_get_prandom_u32(); c->buf[c->len]; c->len++; + * The compiler loads c->len again after a call. c is on the stack of the caller, where len grows + * until it is widened. The jump refines that slot: it still holds what the tested register was + * loaded from. + */ +#define APPEND(name, before_the_jump) \ +static __naked __used void name(void) \ +{ \ + asm volatile ( \ + "r6 = r2; r1 = *(u64 *)(r6 + 8);" before_the_jump \ + "if r1 > 63 goto l_%=;" \ + "call %[bpf_get_prandom_u32];" \ + "r2 = *(u64 *)(r6 + 8); r1 = *(u64 *)(r6 + 0); r1 += r2; r0 = *(u8 *)(r1 + 0);" \ + "r2 += 1; *(u64 *)(r6 + 8) = r2; r0 = 0; exit;" \ + "l_%=: r0 = 1; exit;" \ + :: __imm(bpf_get_prandom_u32) : __clobber_all); \ +} +#define CALL_APPEND(name) \ + "*(u64 *)(r10 - 16) = r6; r1 = 0; *(u64 *)(r10 - 8) = r1;" \ + "r1 = 100; r2 = " #name " ll; r3 = r10; r3 += -16; r4 = 0; call %[bpf_loop]; r1 = r6;" + +APPEND(append, "") +LINKED(callback_tests_slot_of_caller, VALID, CALL_APPEND(append)) +/* The slot is written between the load and the jump: len is 0 1 3 7 15 31 63, the slot 126 */ +APPEND(append_stale, "r2 = r1; r2 <<= 1; *(u64 *)(r6 + 8) = r2;") +LINKED(callback_tests_stale_copy_of_slot, UNBOUND, CALL_APPEND(append_stale)) + /* * p = buf; i = 0; do { if (i > 62) break; i = *(u16 *)p; p = buf + i; } while (i); * r1 == r6 + r2 at the header in every iteration: it holds at loop entry, the backedge preserves it -- 2.55.0