bpf: Fix the stack buffer that bpf_dynptr_slice_rdwr() returns
Subsystems (4)
v2 cover bpf: Fix the stack buffer that bpf_dynptr_slice_rdwr() returns
From: Alexei Starovoitov <[email protected]>
bpf_dynptr_slice_rdwr() and bpf_dynptr_slice() can return the buffer. When the buffer is on the stack, the prog has two pointers to the same stack slot, and the verifier knows only one of them. A store through the slice changes data that the verifier tracks in the slot: a known scalar, a spilled pointer, a dynptr, an iterator. Such a prog loads today and can crash the kernel. A load through the slice reads such data as a scalar.
Patch 1 invalidates the slice when the prog stores to the buffer, or when the frame of the buffer returns. Patch 2 has the tests.
The selftests that pass a stack buffer to the kfuncs only read the buffer after the call. They load as before, with the same number of processed insns. Cilium and sched-ext/scx do not use the kfuncs. test_progs and test_verifier have no new failure.
v1 -> v2:
- Read-only slices of bpf_dynptr_slice() follow the same rule (Sashiko).
- Tests for read-only slices, one of them with a file dynptr. The progs end with a load through the slice.
- Shorter tests: a macro for the callees, less in the run-time test.
v1: https://lore.kernel.org/bpf/[email protected]/
Alexei Starovoitov (2):
bpf: Invalidate a dynptr slice when its stack buffer is written selftests/bpf: Test writes to the buffer of bpf_dynptr_slice_rdwr()
include/linux/bpf_verifier.h | 17 ++ kernel/bpf/diagnostics.c | 6 +- kernel/bpf/diagnostics.h | 1 + kernel/bpf/helpers.c | 10 + kernel/bpf/log.c | 2 + kernel/bpf/scev.c | 6 +- kernel/bpf/verifier.c | 65 ++++- .../testing/selftests/bpf/prog_tests/dynptr.c | 1 + .../testing/selftests/bpf/progs/dynptr_fail.c | 264 ++++++++++++++++++ .../selftests/bpf/progs/dynptr_success.c | 34 +++ 10 files changed, 401 insertions(+), 5 deletions(-)
base-commit: 714b7112017fc7f8b2afabccf562acdb12d2ea7d
Signature · 1 line
2.55.0
PATCH v2 1/2 bpf: Invalidate a dynptr slice when its stack buffer is written
From: Alexei Starovoitov <[email protected]>
bpf_dynptr_slice(&ptr, off, buffer, size) and bpf_dynptr_slice_rdwr() return a pointer to the data of a dynptr. When the data is not contiguous, for example in a non-linear skb, they copy it to 'buffer' and return 'buffer'. Usually the buffer is on the stack. For the verifier the result is plain memory (PTR_TO_MEM), and an access through it is not an access to the stack:
- A store makes wrong what the verifier tracks in the buffer: a known scalar, a spilled pointer, a dynptr, an iterator, irq flags.
- A load gives the bytes of a spilled pointer or of a dynptr as a scalar.
- Both are possible after the frame of the buffer returned.
The accesses are safe while the buffer contains only unknown bytes, as it does after the call. Thus keep this rule: the buffer contains no tracked data while a slice that can be the buffer exists. The slice remembers the frame, the offset and the size of its buffer. The verifier invalidates the slice, read-only or not, at each event that breaks the rule:
event slices of the buffer ------------------------------------------------- -------------------- the prog stores to bytes of the buffer invalid a dynptr, an iterator or irq flags are made there invalid the frame of the buffer returns invalid a helper or a kfunc fills the buffer no change a store next to the buffer, a read of the buffer no change
Tracked data gets into a stack slot in check_stack_write(), also the bytes of a new dynptr, iterator or irq flags. Thus one call there, and one call in prepare_func_exit(), are sufficient. Each is one walk over the registers, only in progs that have such a slice. Helpers and kfuncs leave only unknown bytes: mark_raw_stack() goes around check_stack_write().
The range of the buffer is in the part of bpf_reg_state that the verifier copies with the register and compares for pruning. A slice that is the buffer of a second call passes its range on. Local, ringbuf and skb metadata dynptrs never return the buffer: no range. A dynptr argument of a global function counts as an skb dynptr.
The usual pattern only reads the buffer, and it continues to load:
if (p == buffer)
bpf_dynptr_write(&ptr, 0, buffer, sizeof(buffer), 0);A prog that stores to the buffer and then uses the old slice is rejected:
"R7 invalid mem access 'scalar'". The diagnostics name the reason, and the log shows the buffer of a slice: R7=mem(sz=8,buf=fp[0]-8+8).
--- include/linux/bpf_verifier.h | 17 ++++++++++ kernel/bpf/diagnostics.c | 6 +++- kernel/bpf/diagnostics.h | 1 + kernel/bpf/helpers.c | 10 ++++++ kernel/bpf/log.c | 2 ++ kernel/bpf/scev.c | 6 +++- kernel/bpf/verifier.c | 65 ++++++++++++++++++++++++++++++++++-- 7 files changed, 102 insertions(+), 5 deletions(-)
include/linux/bpf_verifier.h +17 -0
@@ -50,6 +50,13 @@ enum bpf_iter_state { BPF_ITER_STATE_DRAINED, }; +/* Bytes [off, off + size) of the stack of frame frameno. Nothing when size is 0. */ +struct bpf_stack_range { +u32 frameno; +s32 off; +u32 size; +}; +struct bpf_reg_state {/* Ordering of fields matters. See states_equal() */ enum bpf_reg_type type;
@@ -83,6 +90,12 @@ struct bpf_reg_state {struct {/* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */ u32 mem_size; +/* +* bpf_dynptr_slice() and bpf_dynptr_slice_rdwr() return the +* buffer when the data is not contiguous: the register may +* point to this part of the stack. +*/ +struct bpf_stack_range buf; };/* For dynptr stack slots */
@@ -1091,6 +1104,8 @@ struct bpf_verifier_env {bool bypass_spec_v1;bool bypass_spec_v4;bool seen_direct_write; +/* a register may point to the stack and is not PTR_TO_STACK, see bpf_reg_state::buf */ +bool seen_stack_slice;/* a global function is verified for BPF_FUNC_CTX_NO_RCU_LOCK */ bool no_rcu_read_lock;bool seen_exception;
@@ -1815,6 +1830,8 @@ struct bpf_call_arg_meta {struct arg_constant_desc arg_constant;/* The type of the dynptr that the kfunc writes through */ enum bpf_dynptr_type dynptr_write_type; +/* The buffer of bpf_dynptr_slice() or bpf_dynptr_slice_rdwr() */ +struct bpf_stack_range slice_buf;/* arg_{btf,btf_id,owning_ref} are used by kfunc-specific handling, * generally to pass info about user-defined local kptr types to later
kernel/bpf/diagnostics.c +5 -1
@@ -1561,7 +1561,8 @@ static void diag_record_mod(struct bpf_verifier_env *env, u32 insn_idx,if (origin) { event.mod.origin = *origin; event.mod.origin_valid = true; - }else if (diag_mod_insn_origin (env, insn_idx, &target, &event.mod.origin)) { + }else if (reason !=BPF_DIAG_MOD_SLICE_BUF &&/* the insn writes the buffer */ +diag_mod_insn_origin (env, insn_idx, &target, &event.mod.origin)) { event.mod.origin_valid = true; } if (old_reg && new_reg && reason == BPF_DIAG_MOD_WRITE &&
@@ -2237,6 +2238,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_hcase BPF_DIAG_MOD_PKT_DATA_CHANGE : reason ="packet data may have moved" ;break ; +case BPF_DIAG_MOD_SLICE_BUF : + reason ="the stack buffer of this dynptr slice was written, or its frame returned" ; +break ;case BPF_DIAG_MOD_NON_OWN_REF : reason ="leaving the protected region invalidated this borrowed pointer" ;break ;
kernel/bpf/diagnostics.h +1 -0
@@ -23,6 +23,7 @@ enum bpf_diag_mod_reason {BPF_DIAG_MOD_VAR_WRITE ,BPF_DIAG_MOD_REF_RELEASE ,BPF_DIAG_MOD_PKT_DATA_CHANGE , +BPF_DIAG_MOD_SLICE_BUF ,BPF_DIAG_MOD_NON_OWN_REF , BPF_DIAG_MOD_CALLER_SAVED, };
kernel/bpf/helpers.c +10 -0
@@ -3088,6 +3088,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * +* Please also note that when the buffer is on the stack, the verifier invalidates +* the returned pointer when the program stores to the buffer (helpers and kfuncs +* may fill it), or returns from the function that has it. +* * Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only * data slice (can be either direct pointer to the data or a pointer to the user * provided buffer, with its contents containing the data, if unable to obtain
@@ -3173,6 +3177,12 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u64 offset,* Please note that, as in the example above, the user must check that the * returned pointer is not null before using it. * +* Please also note that when the buffer is on the stack, the verifier invalidates +* the returned pointer when the program stores to the buffer (helpers and kfuncs +* may fill it), or returns from the function that has it. Change the data through +* the returned pointer, and before the comparison with the buffer, as in the +* example above: after it the compiler may use one pointer in place of the other. +* * Please also note that in the case of skb and xdp dynptrs, bpf_dynptr_slice_rdwr * does not change the underlying packet data pointers, so a call to * bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in
kernel/bpf/log.c +2 -0
@@ -684,6 +684,8 @@ static void print_reg_state(struct bpf_verifier_env *env,if (base_type (t) ==PTR_TO_MEM ) {verbose_a ("sz=" );verbose_unum (env, reg->mem_size); +if (reg->buf.size) +verbose_a ("buf=fp[%u]%d+%u" , reg->buf.frameno, reg->buf.off, reg->buf.size); }if (t ==CONST_PTR_TO_DYNPTR )verbose_a ("type=%s" ,dynptr_type_str (reg->dynptr.type));
kernel/bpf/scev.c +5 -1
@@ -1831,7 +1831,11 @@ scev_reg_for_read(struct bpf_verifier_env *env, const struct bpf_func_state *st,return zero ? &env->scev->zero_reg : &env->scev->unknown_reg; } -/* Materialize a writable value in the current state, growing its stack if needed. */ +/* +* Materialize a writable value in the current state, growing its stack if needed. +* This does not go through check_stack_write(). The callers skip is_unbound_scev_reg() slots, +* thus the buffer of a live dynptr slice, see bpf_reg_state::buf, does not become a spill here. +*/ static int scev_reg_for_write (struct bpf_verifier_env *env,struct bpf_func_state *st,u32 r,struct bpf_reg_state **out) {
kernel/bpf/verifier.c +62 -3
119 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 2/2 selftests/bpf: Test writes to the buffer of bpf_dynptr_slice_rdwr()
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
bpf_dynptr_slice_rdwr() and bpf_dynptr_slice() can return the buffer. When the buffer is on the stack, a store to the buffer invalidates the slice. See "bpf: Invalidate a dynptr slice when its stack buffer is written".
dynptr_fail.c gets short progs with skb, xdp and file dynptrs. They cover the events that invalidate a slice, the borders of the buffer, a copy of the slice on the stack and in a caller, pruning, read-only slices, and events that leave the slice valid. They end with a load through the slice and are only loaded.
test_dynptr_skb_slice_rdwr_buffer in dynptr_success.c runs on a non-linear skb. It shows that the slice is the buffer, and that the pattern of the documentation continues to work. It passes without the fix too.
--- .../testing/selftests/bpf/prog_tests/dynptr.c | 1 + .../testing/selftests/bpf/progs/dynptr_fail.c | 264 ++++++++++++++++++ .../selftests/bpf/progs/dynptr_success.c | 34 +++ 3 files changed, 299 insertions(+)
tools/testing/selftests/bpf/prog_tests/dynptr.c +1 -0
@@ -37,6 +37,7 @@ static struct { {"test_skb_readonly", SETUP_SKB_PROG}, {"test_dynptr_skb_data", SETUP_SKB_PROG}, {"test_dynptr_skb_slice_non_linear", SETUP_SKB_PROG_NONLINEAR}, + {"test_dynptr_skb_slice_rdwr_buffer", SETUP_SKB_PROG_NONLINEAR}, {"test_dynptr_skb_meta_data", SETUP_SKB_PROG}, {"test_dynptr_skb_meta_flags", SETUP_SKB_PROG}, {"test_adjust", SETUP_SYSCALL_SLEEP},
tools/testing/selftests/bpf/progs/dynptr_fail.c +264 -0
270 lines not on this page · raw message
tools/testing/selftests/bpf/progs/dynptr_success.c +34 -0
@@ -231,6 +231,40 @@ int test_dynptr_skb_slice_non_linear(struct __sk_buff *skb)return 1 ; } +/* +* The tcp header is not in the linear part: the slice is the buffer, and a store +* through the slice is seen in the buffer. As in the documentation, the stores +* come before the compare with the buffer: after it a compiler may use one +* pointer in place of the other. +*/ +SEC("?tc") +int test_dynptr_skb_slice_rdwr_buffer (struct __sk_buff *skb) +{ +struct tcphdr buffer = {}, *tcp; +struct bpf_dynptr ptr; + +bpf_dynptr_from_skb (skb,0 , &ptr); + tcp =bpf_dynptr_slice_rdwr (&ptr,TEST_SKB_LINEAR_SIZE , &buffer,sizeof (buffer)); +if (!tcp) { + err =1 ; +return 1 ; + } + + tcp->source =0x1234 ; +if (buffer.source !=0x1234 ) { + err =2 ; +return 1 ; + } +/* a read of the buffer does not invalidate the slice */ + tcp->dest =0x5678 ; +if (tcp != &buffer) + err =3 ; +else if (bpf_dynptr_write (&ptr,TEST_SKB_LINEAR_SIZE , &buffer,sizeof (buffer),0 )) + err =4 ; + +return 1 ; +} + SEC("?tc")int test_dynptr_skb_meta_data (struct __sk_buff *skb) {
Signature · 1 line
2.55.0
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Mon, 2 Nov 2026 17:09:56 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
bpf_dynptr_slice_rdwr()andbpf_dynptr_slice()can return the buffer. When the buffer is on the stack, the prog has two pointers to the same stack slot, and the verifier knows only one of them. A store through the slice changes data that the verifier tracks in the slot: a known scalar, a spilled pointer, a dynptr, an iterator. Such a prog loads today and can crash the kernel. A load through the slice reads such data as a scalar.[...]
Here is the summary with links:
- [bpf,v2,1/2] bpf: Invalidate a dynptr slice when its stack buffer is written
https://git.kernel.org/bpf/bpf/c/51f6627d44f3 - [bpf,v2,2/2] selftests/bpf: Test writes to the buffer of bpf_dynptr_slice_rdwr() https://git.kernel.org/bpf/bpf/c/c820e6f70d12
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html