From: Alexei Starovoitov verifier_loop_latch.c has the latches in the forms that a compiler makes. Each iteration stores a byte to a map value of 2048 bytes and moves the pointer by one. The progs that load expect the count in the log. In the progs that the verifier must reject, a store is behind the value at run time. - The latch is false in the first iteration, for a constant and for all of a range, next to a second exit with a count of 4 billion. The same with two iterations. A latch that compares pointers, also with the first one in front of the value. - '!=' in 32 bits, down to -1 and up to 0: 2048 iterations, and one too many. - One unsigned test on a counter that goes down: in 32 and 64 bits, from a constant and from a range, and one iteration too many. Two loops that do not end below 0: a 64-bit jump on a 32-bit counter with a bound above 32 bits, and a second back edge under a signed 64-bit jump. - The latch tests a flag: alone, with a store next to it, with a call between the two jumps, and one iteration too many. In 5 progs the loop goes on when the counter gets to 0: a call overwrites the flag, the flag is set again, is changed, is set again on one of two paths, or there is a second way to the jump that counts. verifier_scev.c: - pre_lt_dir_high goes down under an unsigned test and starts above S64_MAX, where the rule does not hold. - 5 rows for '!=' across 0. A step that misses the bound and the way from 4 up to 1 have no count. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/verifier_loop_latch.c | 165 ++++++++++++++++++ .../selftests/bpf/progs/verifier_scev.c | 9 + 3 files changed, 176 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_loop_latch.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index a771dfbaeb3f..574b06c383fa 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -78,6 +78,7 @@ #include "verifier_liveness_exp.skel.h" #include "verifier_load_acquire.skel.h" #include "verifier_loop_hierarchy.skel.h" +#include "verifier_loop_latch.skel.h" #include "verifier_loops1.skel.h" #include "verifier_lwt.skel.h" #include "verifier_map_in_map.skel.h" @@ -272,6 +273,7 @@ void test_verifier_linked_scalars(void) { RUN(verifier_linked_scalars); } void test_verifier_live_stack(void) { RUN(verifier_live_stack); } void test_verifier_liveness_exp(void) { RUN(verifier_liveness_exp); } void test_verifier_loop_hierarchy(void) { RUN(verifier_loop_hierarchy); } +void test_verifier_loop_latch(void) { RUN(verifier_loop_latch); } void test_verifier_loops1(void) { RUN(verifier_loops1); } void test_verifier_lwt(void) { RUN(verifier_lwt); } void test_verifier_map_in_map(void) { RUN(verifier_map_in_map); } diff --git a/tools/testing/selftests/bpf/progs/verifier_loop_latch.c b/tools/testing/selftests/bpf/progs/verifier_loop_latch.c new file mode 100644 index 000000000000..1fcefce9ab01 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_loop_latch.c @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The jumps that tell how many iterations a loop has, in the forms that a compiler makes of + * for (i = first; i < N; i++) from the last index + * for (i = N - 1; i >= 0; i--) while (n--) for (j = N - 1; j >= 0 && j < N; j--) + * + * Each iteration stores a byte and moves the pointer by one. The value has NR bytes. The programs + * that load expect the count in the log. In those that have to be rejected a store is behind the + * value at run time. + */ +#include +#include +#include "bpf_misc.h" + +#define NR 2048 + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, u32); + __type(value, char[NR]); +} map SEC(".maps"); + +#define OK(text, count) \ + SEC("socket") __description("latch: " text) __success __log_level(2) \ + __msg("header_count is " count) +#define BAD(text) \ + SEC("socket") __description("latch: " text) __failure __msg("invalid access to map value") +/* the pointer goes on and on */ +#define NO_COUNT(text) \ + SEC("socket") __description("latch: " text) __failure + +/* r6: the value, r7: a number that is not known. @init, then the loop. */ +#define LOOP(name, init, body) \ +__naked void name(void) \ +{ \ + asm volatile (" \ + r1 = 0; \ + *(u32 *)(r10 - 4) = r1; \ + r2 = r10; \ + r2 += -4; \ + r1 = %[map] ll; \ + call %[bpf_map_lookup_elem]; \ + if r0 == 0 goto 9f; \ + r6 = r0; \ + call %[bpf_get_prandom_u32]; \ + w7 = w0; \ +" init " \ +1: r1 = 0; \ + *(u8 *)(r6 + 0) = r1; \ + r6 += 1; \ +" body " \ +9: r0 = 0; \ + exit; \ +" : \ + : __imm(bpf_map_lookup_elem), __imm(bpf_get_prandom_u32), __imm_addr(map) \ + : __clobber_all); \ +} + +/* + * From the last index the latch is false in the first iteration. That is a count. The other way + * out of the loop has one too: 4 billion. + */ +#define UP_TO_8 "if r8 >= r7 goto 9f; r8 += 1; if r8 < 8 goto 1b;" + +OK("false at once, and another exit", "[0..1]") +LOOP(false_at_once, "r8 = 7; r6 += 2047;", UP_TO_8) + +BAD("two iterations, and another exit") +LOOP(false_in_second, "r8 = 6; r6 += 2047;", UP_TO_8) + +OK("false at once for all of a range", "[0..1]") +LOOP(false_at_once_range, "r8 = r7; r8 &= 3; r8 += 7; r6 += 2047;", UP_TO_8) + +BAD("false at once for all of a range but one") +LOOP(false_at_once_range_bad, "r8 = r7; r8 &= 3; r8 += 6; r6 += 2047;", UP_TO_8) + +/* + * The latch compares pointers. One byte in front of the value is less than a byte in it, + * although -1 is a large unsigned offset: the latch is true in the first iteration. + */ +#define PTR_UP(off) "r8 += 1; r1 = r8; r1 += " #off "; if r1 < r9 goto 1b;" + +OK("pointers", "8") +LOOP(ptr_up, "r8 = r6; r9 = r6; r9 += 8; r6 += 2040;", PTR_UP(0)) + +BAD("pointers, the first one is in front of the value") +LOOP(ptr_up_from_below, "r8 = r6; r9 = r6; r9 += 7; r6 += 2040;", PTR_UP(-2)) + +/* an int goes down to -1, or up from -N to 0: the distance is a 32-bit number */ +#define DOWN_TO_M1 "w8 += -1; if w8 != -1 goto 1b;" +#define UP_TO_0 "w8 += 1; if w8 != 0 goto 1b;" + +OK("down to -1 in 32 bits", "2048") +LOOP(down_to_m1, "w8 = 2047;", DOWN_TO_M1) + +BAD("down to -1 in 32 bits, one too many") +LOOP(down_to_m1_bad, "w8 = 2048;", DOWN_TO_M1) + +OK("up to 0 in 32 bits", "2048") +LOOP(up_to_0, "w8 = -2048;", UP_TO_0) + +BAD("up to 0 in 32 bits, one too many") +LOOP(up_to_0_bad, "w8 = -2049;", UP_TO_0) + +/* 'j >= 0 && j < N' is one unsigned test */ +OK("down, one unsigned test", "2048") +LOOP(down_unsigned, "w8 = 2047;", "w8 += -1; if w8 < 2048 goto 1b;") + +BAD("down, one unsigned test, one too many") +LOOP(down_unsigned_bad, "w8 = 2048;", "w8 += -1; if w8 < 2049 goto 1b;") + +OK("down, one unsigned test, 64 bits", "2048") +LOOP(down_unsigned64, "r8 = 2047;", "r8 += -1; if r8 <= 2047 goto 1b;") + +OK("down from a range, one unsigned test", "[0..2048]") +LOOP(down_unsigned_range, "if r7 > 2047 goto 9f;", "w7 += -1; if w7 < 2048 goto 1b;") + +BAD("down from a range, one unsigned test, one too many") +LOOP(down_unsigned_range_bad, "if r7 > 2048 goto 9f;", "w7 += -1; if w7 < 2049 goto 1b;") + +/* below 0 a 32-bit counter is 0xffffffff for a 64-bit jump */ +BAD("down, a 64-bit test with a bound above 32 bits") +LOOP(down_unsigned_wide_bound, "w8 = 2047; r9 = 0x100000800 ll;", "w8 += -1; if r8 < r9 goto 1b;") + +BAD("down, an unsigned and a signed 64-bit test") +LOOP(down_two_tests, "w8 = 4; r6 += 2043;", + "w8 += -1; if r8 < 5 goto 2f; if r8 s>= 5 goto 1b; goto 9f; 2: if r8 < 5 goto 1b;") + +/* 'while (n--)': the jump that counts sets a flag, the jump that leaves the loop tests it */ +#define FLAG(mid) "w8 += 1; w2 = 1; if w8 == 0 goto 2f; w2 = 0; 2: " mid " if w2 != 1 goto 1b;" + +OK("a flag", "2048") +LOOP(flag, "w8 = -2048;", FLAG("r3 = r6;")) + +BAD("a flag, one too many") +LOOP(flag_bad, "w8 = -2049;", FLAG("r3 = r6;")) + +OK("a flag, a store behind it", "2048") +LOOP(flag_store, "w8 = -2048;", + "w8 += 1; w2 = 1; *(u32 *)(r10 - 4) = r8; if w8 == 0 goto 2f; w2 = 0; 2: if w2 != 1 goto 1b;") + +OK("a flag, a call behind it", "2048") +LOOP(flag_call, "w8 = -2048;", + "w8 += 1; w9 = 1; if w8 == 0 goto 2f; w9 = 0; 2: call %[bpf_get_prandom_u32]; if w9 != 1 goto 1b;") + +/* in these the loop goes on when the counter gets to 0 */ +NO_COUNT("a flag that a call overwrites") +LOOP(flag_call_r0, "w8 = -2048;", + "w8 += 1; w0 = 1; if w8 == 0 goto 2f; w0 = 0; 2: call %[bpf_get_prandom_u32]; if w0 != 1 goto 1b;") + +NO_COUNT("a flag that is set again") +LOOP(flag_set_again, "w8 = -2048;", FLAG("w2 = 0;")) + +NO_COUNT("a flag that is changed") +LOOP(flag_changed, "w8 = -2048;", FLAG("w2 &= w7;")) + +NO_COUNT("a flag that is set again on one path") +LOOP(flag_other_jump, "w8 = -2048;", FLAG("if r7 != 0 goto 4f; w2 = 0; 4:")) + +NO_COUNT("a flag, another way to the jump that counts") +LOOP(flag_two_ways, "w8 = -2048;", + "w8 += 1; w2 = 0; if r7 != 0 goto 3f; w2 = 1; 3: if w8 == 0 goto 2f; w2 = 0; 2: if w2 != 1 goto 1b;") + +char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/verifier_scev.c b/tools/testing/selftests/bpf/progs/verifier_scev.c index 5cc886b92913..07c356fde68d 100644 --- a/tools/testing/selftests/bpf/progs/verifier_scev.c +++ b/tools/testing/selftests/bpf/progs/verifier_scev.c @@ -877,9 +877,12 @@ PRE__COND_TEST (pre_ne_false, "==", 7, 2, * while (++r9 <= 8 && r6 < 7) { // r6 ∈ [5,3,1,U64_MAX] * r6 -= 2; // r6 ∈ [3,1,U64_MAX] * } + * + * From above S64_MAX it's a long way down. */ PRE__COND_TEST2(pre_lt_dir, ">=", 5, -2, 7, "header_count is [0..4] ") PRE__COND_TEST2(pre_le_dir, ">", 5, -2, 7, "header_count is [0..4] ") +PRE__COND_TEST2(pre_lt_dir_high, ">=", ITER_S64_MIN + 5, -2, ITER_S64_MIN + 7, "can't compute iterations count") /* * The counter moves away from the exit; bound fallback verification. @@ -1011,6 +1014,12 @@ POST_COND_TEST32(post32_le, "<=", -9, 2, POST_COND_TEST32(post32_gt, ">", -1, -2u, -7, "header_count is [0..3] ") POST_COND_TEST32(post32_ge, ">=", -1, -2u, -7, "header_count is [0..4] ") POST_COND_TEST32(post32_ne, "!=", -1, -2u, -7, "header_count is [0..3] ") +/* across 0: in order as signed numbers. From 4 up to 1 goes all the way around. */ +POST_COND_TEST32(post32_ne_to_m1, "!=", 3, -1u, -1, "header_count is [0..4] ") +POST_COND_TEST32(post32_ne_to_0, "!=", -4, 1, 0, "header_count is [0..4] ") +POST_COND_TEST32(post32_ne_to_m1_2, "!=", 3, -2u, -1, "header_count is [0..2] ") +POST_COND_TEST32(post32_ne_miss, "!=", 4, -2u, -1, "can't compute iterations count") +POST_COND_TEST32(post32_ne_around, "!=", 4, 1, 1, "can't compute iterations count") /* An exit exactly at S32_MAX is accepted as well. */ POST_COND_TEST32(post32_slt_max, "s<", INT32_MAX - 6, 2, INT32_MAX, "header_count is [0..3] ") -- 2.55.0