From: Alexei Starovoitov For each op one prog needs both bounds of the result, in 64 and in 32 bits, and expects the register as the log has it. Next to it are progs that get to a bad read at run time if a bound is off by one, a bit is shifted out, or the divisor is 0. The verifier must reject them. One prog has what stays unknown: a count of 64 or 32, a signed division. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/verifier_alu_range.c | 168 ++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_alu_range.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index 99f0551c8b59..a771dfbaeb3f 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -9,6 +9,7 @@ #include "cb_refs.skel.h" #include "verifier_aggregate_arg.skel.h" #include "verifier_aggregate_ret.skel.h" +#include "verifier_alu_range.skel.h" #include "verifier_align.skel.h" #include "verifier_and.skel.h" #include "verifier_arena.skel.h" @@ -204,6 +205,7 @@ void test_cb_refs(void) { RUN_TESTS(cb_refs); } void test_verifier_aggregate_arg(void) { RUN_TESTS(verifier_aggregate_arg); } void test_verifier_aggregate_ret(void) { RUN_TESTS(verifier_aggregate_ret); } +void test_verifier_alu_range(void) { RUN(verifier_alu_range); } void test_verifier_align(void) { RUN(verifier_align); } void test_verifier_and(void) { RUN(verifier_and); } void test_verifier_arena(void) { RUN(verifier_arena); } diff --git a/tools/testing/selftests/bpf/progs/verifier_alu_range.c b/tools/testing/selftests/bpf/progs/verifier_alu_range.c new file mode 100644 index 000000000000..7bd13812941c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_alu_range.c @@ -0,0 +1,168 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The count of a shift, or a divisor, is a range and not a constant: + * x >> [a, b] [xmin >> b, xmax >> a] + * x << [a, b] [xmin << a, xmax << b], if no bit is shifted out + * x s>> [a, b] a larger count brings x closer to 0, or to -1 + * x / [a, b] [xmin / b, xmax / a], and x / 0 = 0 + * x % [a, b] [0, b - 1], x if it is below a, and x % 0 = x + * Each program that has to be rejected gets to the bad read at run time. + */ +#include +#include +#include "bpf_misc.h" + +#define OK(text) SEC("socket") __description("alu range: " text) __success __log_level(2) +#define BAD(text) SEC("socket") __description("alu range: " text) __failure \ + __msg("invalid read from stack") + +/* @reg is lo .. lo + mask */ +#define RANGE(reg, lo, mask) \ + "call %[bpf_get_prandom_u32];" \ + #reg " = r0;" \ + #reg " &= " #mask ";" \ + #reg " += " #lo ";" + +/* The read is not allowed */ +#define UNLESS(cond) "if " cond " goto +1; r0 = *(u64 *)(r10 + 8);" + +/* r6 is the second operand. r7 and r8 are x. */ +#define ALU(src_lo, src_mask, x_lo, x_mask, insns) \ + asm volatile ( \ + RANGE(r6, src_lo, src_mask) \ + RANGE(r7, x_lo, x_mask) \ + "r8 = r7;" \ + insns \ + "r0 = 0;" \ + "exit;" \ + :: __imm(bpf_get_prandom_u32) : __clobber_all) + +OK("[128, 255] >> [2, 5]") +__msg("r7 >>= r6 {{.*}} R7=scalar(smin=umin=smin32=umin32=4,smax=umax=smax32=umax32=63,var_off=(0x0; 0x3f))") +__msg("w8 >>= w6 {{.*}} R8=scalar(smin=umin=smin32=umin32=4,smax=umax=smax32=umax32=63,var_off=(0x0; 0x3f))") +__naked void rsh(void) +{ + ALU(2, 3, 128, 127, "r7 >>= r6; w8 >>= w6;" + UNLESS("r7 >= 4") UNLESS("r7 <= 63") UNLESS("w8 >= 4") UNLESS("w8 <= 63")); +} + +BAD("255 >> 2 is 63") +__naked void rsh_max(void) +{ + ALU(2, 3, 128, 127, "r7 >>= r6;" UNLESS("r7 <= 62")); +} + +OK("[3, 10] << [1, 4]") +__msg("r7 <<= r6 {{.*}} R7=scalar(smin=umin=smin32=umin32=6,smax=umax=smax32=umax32=160,var_off=(0x0; 0xfe))") +__msg("w8 <<= w6 {{.*}} R8=scalar(smin=umin=smin32=umin32=6,smax=umax=smax32=umax32=160,var_off=(0x0; 0xfe))") +__naked void lsh(void) +{ + ALU(1, 3, 3, 7, "r7 <<= r6; w8 <<= w6;" + UNLESS("r7 >= 6") UNLESS("r7 <= 160") UNLESS("w8 >= 6") UNLESS("w8 <= 160")); +} + +BAD("10 << 4 is 160, 32 bits") +__naked void lsh32_max(void) +{ + ALU(1, 3, 3, 7, "w7 <<= w6;" UNLESS("w7 <= 159")); +} + +BAD("4 << 62 is 0") +__naked void lsh_overflow(void) +{ + ALU(59, 3, 4, 1, "r7 <<= r6;" UNLESS("r7 != 0")); +} + +BAD("4 << 30 is 0, 32 bits") +__naked void lsh32_overflow(void) +{ + ALU(27, 3, 4, 1, "w7 <<= w6;" UNLESS("w7 != 0")); +} + +OK("[-128, -65] s>> [2, 5], [-128, 127] s>> [2, 5]") +__msg("r7 s>>= r6 {{.*}} R7=scalar(smin=smin32=-32,smax=smax32=-3,") +__msg("w8 s>>= w6 {{.*}} R8=scalar(smin=0,smax=umax=0xffffffff,smin32=-32,smax32=31,") +__naked void arsh(void) +{ + ALU(2, 3, -128, 63, "r7 s>>= r6;" UNLESS("r7 s>= -32") UNLESS("r7 s<= -3") + RANGE(r8, -128, 255) "w8 s>>= w6;" UNLESS("w8 s>= -32") UNLESS("w8 s<= 31")); +} + +BAD("-65 s>> 5 is -3") +__naked void arsh_max(void) +{ + ALU(2, 3, -128, 63, "r7 s>>= r6;" UNLESS("r7 s<= -4")); +} + +OK("[128, 255] / [2, 5], [128, 255] / [0, 3]") +__msg("r7 /= r6 {{.*}} R7=scalar(smin=umin=smin32=umin32=25,smax=umax=smax32=umax32=127,var_off=(0x0; 0x7f))") +__msg("w8 /= w6 {{.*}} R8=scalar(smin=umin=smin32=umin32=25,smax=umax=smax32=umax32=127,var_off=(0x0; 0x7f))") +__msg("r9 /= r6 {{.*}} R9=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff))") +__naked void div(void) +{ + ALU(2, 3, 128, 127, "r9 = r7; r7 /= r6; w8 /= w6;" + UNLESS("r7 >= 25") UNLESS("r7 <= 127") UNLESS("w8 >= 25") UNLESS("w8 <= 127") + "r6 += -2; r9 /= r6;" UNLESS("r9 <= 255")); +} + +BAD("128 / 5 is 25, 32 bits") +__naked void div32_min(void) +{ + ALU(2, 3, 128, 127, "w7 /= w6;" UNLESS("w7 >= 26")); +} + +BAD("x / 0 is 0") +__naked void div_by_zero(void) +{ + ALU(0, 3, 128, 127, "r7 /= r6;" UNLESS("r7 >= 42")); +} + +OK("[128, 255] % [2, 5], [128, 255] % [256, 259], [128, 255] % [0, 3]") +__msg("r7 %= r6 {{.*}} R7=scalar(smin=smin32=0,smax=umax=smax32=umax32=4,var_off=(0x0; 0x7))") +__msg("w8 %= w6 {{.*}} R8=scalar(smin=smin32=0,smax=umax=smax32=umax32=4,var_off=(0x0; 0x7))") +__msg("r9 %= r6 {{.*}} R9=scalar(smin=umin=smin32=umin32=128,smax=umax=smax32=umax32=255,var_off=(0x80; 0x7f))") +__msg("w9 %= w6 {{.*}} R9=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff))") +__naked void mod(void) +{ + ALU(2, 3, 128, 127, "r9 = r7; r7 %%= r6; w8 %%= w6;" UNLESS("r7 <= 4") UNLESS("w8 <= 4") + "r6 += 254; r9 %%= r6;" UNLESS("r9 >= 128") UNLESS("r9 <= 255") + "r6 += -256; w9 %%= w6;" UNLESS("w9 <= 255")); +} + +BAD("129 % 5 is 4") +__naked void mod_max(void) +{ + ALU(2, 3, 128, 127, "r7 %%= r6;" UNLESS("r7 <= 3")); +} + +BAD("255 % 255 is 0") +__naked void mod_same(void) +{ + ALU(255, 3, 128, 127, "r7 %%= r6;" UNLESS("r7 >= 128")); +} + +BAD("x % 0 is x, 32 bits") +__naked void mod32_by_zero(void) +{ + ALU(0, 3, 128, 127, "w7 %%= w6;" UNLESS("w7 <= 2")); +} + +/* The cpu takes the count modulo 64 or 32, and a signed division can change the sign */ +OK("not known: a count of 64 or 32, a signed division") +__msg("r7 >>= r6 {{.*}} R7=scalar()") +__msg("w8 >>= w6 {{.*}} R8=scalar()") +__msg("r7 s/= r6 {{.*}} R7=scalar()") +__msg("w8 s%= w6 {{.*}} R8=scalar()") +__naked void not_known(void) +{ + ALU(63, 1, 128, 127, "r7 >>= r6; r6 += -32; w8 >>= w6;" + RANGE(r7, 128, 127) "r8 = r7; r7 s/= r6; w8 s%%= w6;"); +} + +BAD("x >> 64 is x") +__naked void rsh_64(void) +{ + ALU(63, 1, 128, 127, "r7 >>= r6;" UNLESS("r7 == 0")); +} + +char _license[] SEC("license") = "GPL"; -- 2.55.0