mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

Add range tracking for instruction BPF_NEG. Without this logic, a trivial program like the following will fail volatile bool found_value_b; SEC("lsm.s/socket_connect") int BPF_PROG(test_socket_connect) { if (!found_value_b) return -1; return 0; } with verifier log: "At program exit the register R0 has smin=0 smax=4294967295 should have been in [-4095, 0]". This is because range information is lost in BPF_NEG: 0: R1=ctx() R10=fp0 ; if (!found_value_b) @ xxxx.c:24 0: (18) r1 = 0xffa00000011e7048 ; R1_w=map_value(...) 2: (71) r0 = *(u8 *)(r1 +0) ; R0_w=scalar(smin32=0,smax=255) 3: (a4) w0 ^= 1 ; R0_w=scalar(smin32=0,smax=255) 4: (84) w0 = -w0 ; R0_w=scalar(range info lost) Note that, the log above is manually modified to highlight relevant bits. Fix this by maintaining proper range information with BPF_NEG, so that the verifier will know: 4: (84) w0 = -w0 ; R0_w=scalar(smin32=-255,smax=0) Also updated selftests based on the expected behavior. Signed-off-by: Song Liu <song@kernel.org> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20250625164025.3310203-2-song@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
174 lines
4.4 KiB
C
174 lines
4.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Converted from tools/testing/selftests/bpf/verifier/bounds_deduction.c */
|
|
|
|
#include <linux/bpf.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
#include "bpf_misc.h"
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 1")
|
|
__failure __msg("R0 tried to subtract pointer from scalar")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__naked void deducing_bounds_from_const_1(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = 1; \
|
|
if r0 s>= 1 goto l0_%=; \
|
|
l0_%=: r0 -= r1; \
|
|
exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 2")
|
|
__success __failure_unpriv
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__retval(1)
|
|
__naked void deducing_bounds_from_const_2(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = 1; \
|
|
if r0 s>= 1 goto l0_%=; \
|
|
exit; \
|
|
l0_%=: if r0 s<= 1 goto l1_%=; \
|
|
exit; \
|
|
l1_%=: r1 -= r0; \
|
|
exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 3")
|
|
__failure __msg("R0 tried to subtract pointer from scalar")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__naked void deducing_bounds_from_const_3(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = 0; \
|
|
if r0 s<= 0 goto l0_%=; \
|
|
l0_%=: r0 -= r1; \
|
|
exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 4")
|
|
__success __failure_unpriv
|
|
__msg_unpriv("R6 has pointer with unsupported alu operation")
|
|
__retval(0)
|
|
__naked void deducing_bounds_from_const_4(void)
|
|
{
|
|
asm volatile (" \
|
|
r6 = r1; \
|
|
r0 = 0; \
|
|
if r0 s<= 0 goto l0_%=; \
|
|
exit; \
|
|
l0_%=: if r0 s>= 0 goto l1_%=; \
|
|
exit; \
|
|
l1_%=: r6 -= r0; \
|
|
exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 5")
|
|
__failure __msg("R0 tried to subtract pointer from scalar")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__naked void deducing_bounds_from_const_5(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = 0; \
|
|
if r0 s>= 1 goto l0_%=; \
|
|
r0 -= r1; \
|
|
l0_%=: exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 6")
|
|
__failure __msg("R0 tried to subtract pointer from scalar")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__naked void deducing_bounds_from_const_6(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = 0; \
|
|
if r0 s>= 0 goto l0_%=; \
|
|
exit; \
|
|
l0_%=: r0 -= r1; \
|
|
exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 7")
|
|
__failure __msg("dereference of modified ctx ptr")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__flag(BPF_F_ANY_ALIGNMENT)
|
|
__naked void deducing_bounds_from_const_7(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = %[__imm_0]; \
|
|
if r0 s>= 0 goto l0_%=; \
|
|
l0_%=: r1 -= r0; \
|
|
r0 = *(u32*)(r1 + %[__sk_buff_mark]); \
|
|
exit; \
|
|
" :
|
|
: __imm_const(__imm_0, ~0),
|
|
__imm_const(__sk_buff_mark, offsetof(struct __sk_buff, mark))
|
|
: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 8")
|
|
__failure __msg("negative offset ctx ptr R1 off=-1 disallowed")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__flag(BPF_F_ANY_ALIGNMENT)
|
|
__naked void deducing_bounds_from_const_8(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = %[__imm_0]; \
|
|
if r0 s>= 0 goto l0_%=; \
|
|
r1 += r0; \
|
|
l0_%=: r0 = *(u32*)(r1 + %[__sk_buff_mark]); \
|
|
exit; \
|
|
" :
|
|
: __imm_const(__imm_0, ~0),
|
|
__imm_const(__sk_buff_mark, offsetof(struct __sk_buff, mark))
|
|
: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 9")
|
|
__failure __msg("R0 tried to subtract pointer from scalar")
|
|
__msg_unpriv("R1 has pointer with unsupported alu operation")
|
|
__naked void deducing_bounds_from_const_9(void)
|
|
{
|
|
asm volatile (" \
|
|
r0 = 0; \
|
|
if r0 s>= 0 goto l0_%=; \
|
|
l0_%=: r0 -= r1; \
|
|
exit; \
|
|
" ::: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("check deducing bounds from const, 10")
|
|
__failure
|
|
__msg("math between ctx pointer and register with unbounded min value is not allowed")
|
|
__failure_unpriv
|
|
__naked void deducing_bounds_from_const_10(void)
|
|
{
|
|
asm volatile (" \
|
|
r6 = r1; \
|
|
r0 = 0; \
|
|
if r0 s<= 0 goto l0_%=; \
|
|
l0_%=: /* Marks r0 as unknown. */ \
|
|
call %[bpf_get_prandom_u32]; \
|
|
r0 -= r6; \
|
|
exit; \
|
|
" :
|
|
: __imm(bpf_get_prandom_u32)
|
|
: __clobber_all);
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|