bpf: fix accesses to uninit stack slots
Subsystems (4)
v3 cover bpf: fix accesses to uninit stack slots
Fix two related issues issues around verifying stack accesses:
1. accesses to uninitialized stack memory was allowed inconsistently
2. the maximum stack depth needed for a program was not always maintained correctly
The two issues are fixed together in one commit because the code for one affects the other.
V2 to V3:
- address review comments from Andrii and Eduard
- drop new verifier tests in favor of editing existing tests to check for stack depth
- append a patch with a bit of cleanup coming out of the previous review
Andrei Matei (3):
bpf: add some comments to stack representation
bpf: fix accesses to uninit stack slots
bpf: minor cleanup around stack bounds
include/linux/bpf_verifier.h | 14 ++++ kernel/bpf/verifier.c | 73 +++++++++---------- tools/testing/selftests/bpf/progs/iters.c | 2 +- .../selftests/bpf/progs/test_global_func16.c | 2 +- .../bpf/progs/verifier_basic_stack.c | 8 +- .../selftests/bpf/progs/verifier_int_ptr.c | 5 +- .../selftests/bpf/progs/verifier_raw_stack.c | 5 +- .../selftests/bpf/progs/verifier_var_off.c | 62 +++++++++++++--- .../selftests/bpf/verifier/atomic_cmpxchg.c | 11 --- tools/testing/selftests/bpf/verifier/calls.c | 4 +- 10 files changed, 113 insertions(+), 73 deletions(-)
Signature · 1 line
2.40.1
PATCH v3 1/3 bpf: add some comments to stack representation
--- include/linux/bpf_verifier.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/bpf_verifier.h +14 -0
@@ -316,7 +316,17 @@ struct bpf_func_state {/* The following fields should be last. See copy_func_state() */ int acquired_refs;struct bpf_reference_state *refs; +/* Size of the current stack, in bytes. The stack state is tracked below, in +* `stack`. allocated_stack is always a multiple of BPF_REG_SIZE. +*/ int allocated_stack; +/* The state of the stack. Each element of the array describes BPF_REG_SIZE +* (i.e. 8) bytes worth of stack memory. +* stack[0] represents bytes [*(r10-8)..*(r10-1)] +* stack[1] represents bytes [*(r10-16)..*(r10-9)] +* ... +* stack[allocated_stack/8 - 1] represents [*(r10-allocated_size)..*(r10-allocated_size+7)] +*/ struct bpf_stack_state *stack; };
@@ -630,6 +640,10 @@ struct bpf_verifier_env {int exception_callback_subprog;bool explore_alu_limits;bool allow_ptr_leaks; +/* Allow access to uninitialized stack memory. Writes with fixed offset are +* always allowed, so this refers to reads (with fixed or variable offset), +* to writes with variable offset and to indirect (helper) accesses. +*/ bool allow_uninit_stack;bool bpf_capable;bool bypass_spec_v1;
Signature · 1 line
2.40.1
PATCH v3 2/3 bpf: fix accesses to uninit stack slots
In reply to Andrei MateiPrivileged programs are supposed to be able to read uninitialized stack memory (ever since 6715df8d5) but, before this patch, these accesses were permitted inconsistently. In particular, accesses were permitted above state->allocated_stack, but not below it. In other words, if the stack was already "large enough", the access was permitted, but otherwise the access was rejected instead of being allowed to "grow the stack". This undesired rejection was happening in two places:
- in check_stack_slot_within_bounds()
- in check_stack_range_initialized()
This patch arranges for these accesses to be permitted. A bunch of tests that were relying on the old rejection had to change; all of them were changed to add also run unprivileged, in which case the old behavior persists. One tests couldn't be updated - global_func16 - because it can't run unprivileged for other reasons.
This patch also fixes the tracking of the stack size for variable-offset reads. This second fix is bundled in the same commit as the first one because they're inter-related. Before this patch, writes to the stack using registers containing a variable offset (as opposed to registers with fixed, known values) were not properly contributing to the function's needed stack size. As a result, it was possible for a program to verify, but then to attempt to read out-of-bounds data at runtime because a too small stack had been allocated for it.
Each function tracks the size of the stack it needs in bpf_subprog_info.stack_depth, which is maintained by update_stack_depth(). For regular memory accesses, check_mem_access() was calling update_state_depth() but it was passing in only the fixed part of the offset register, ignoring the variable offset. This was incorrect; the minimum possible value of that register should be used instead.
This tracking is now fixed by centralizing the tracking of stack size in grow_stack_state(), and by lifting the calls to grow_stack_state() to check_stack_access_within_bounds() as suggested by Andrii. The code is now simpler and more convincingly tracks the correct maximum stack size. check_stack_range_initialized() can now rely on enough stack having been allocated for the access; this helps with the fix for the first issue.
A few tests were changed to also check the stack depth computation. The one that fails without this patch is verifier_var_off:stack_write_priv_vs_unpriv.
--- kernel/bpf/verifier.c | 67 ++++++++----------- tools/testing/selftests/bpf/progs/iters.c | 2 +- .../selftests/bpf/progs/test_global_func16.c | 2 +- .../bpf/progs/verifier_basic_stack.c | 8 +-- .../selftests/bpf/progs/verifier_int_ptr.c | 5 +- .../selftests/bpf/progs/verifier_raw_stack.c | 5 +- .../selftests/bpf/progs/verifier_var_off.c | 62 ++++++++++++++--- .../selftests/bpf/verifier/atomic_cmpxchg.c | 11 --- tools/testing/selftests/bpf/verifier/calls.c | 4 +- 9 files changed, 93 insertions(+), 73 deletions(-)
kernel/bpf/verifier.c +27 -40
@@ -1685,7 +1685,10 @@ static int resize_reference_state(struct bpf_func_state *state, size_t n)return 0 ; } -static int grow_stack_state (struct bpf_func_state *state,int size) +/* Possibly update state->allocated_stack to be at least size bytes. Also +* possibly update the function's high-water mark in its bpf_subprog_info. +*/ +static int grow_stack_state (struct bpf_verifier_env *env,struct bpf_func_state *state,int size) {size_t old_n = state->allocated_stack /BPF_REG_SIZE , n = size /BPF_REG_SIZE ;
@@ -1697,6 +1700,11 @@ static int grow_stack_state(struct bpf_func_state *state, int size)return -ENOMEM ; state->allocated_stack = size; + +/* update known max for given subprogram */ +if (env->subprog_info[state->subprogno].stack_depth < size) + env->subprog_info[state->subprogno].stack_depth = size; +return 0 ; }
@@ -4669,9 +4677,6 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,struct bpf_reg_state *reg =NULL ;u32 dst_reg = insn->dst_reg; - err =grow_stack_state (state,round_up (slot +1 ,BPF_REG_SIZE )); -if (err) -return err;/* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, * so it's aligned access and [off, off + size) are within stack limits */
@@ -4827,10 +4832,6 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env, (!value_reg && is_bpf_st_mem(insn) && insn->imm == 0)) writing_zero = true; - err =grow_stack_state (state,round_up (-min_off,BPF_REG_SIZE )); -if (err) -return err; -for (i = min_off; i < max_off; i++) {int spi;
@@ -5959,20 +5960,6 @@ static int check_ptr_alignment(struct bpf_verifier_env *env, strict); } -static int update_stack_depth (struct bpf_verifier_env *env, -const struct bpf_func_state *func, -int off) -{ -u16 stack = env->subprog_info[func->subprogno].stack_depth; - -if (stack >= -off) -return 0 ; - -/* update known max for given subprogram */ - env->subprog_info[func->subprogno].stack_depth = -off; -return 0 ; -} -/* starting from main bpf function walk all instructions of the function * and recursively walk all callees that given function can call. * Ignore jump and exit insns.
@@ -6761,13 +6748,14 @@ static int check_ptr_to_map_access(struct bpf_verifier_env *env,* The minimum valid offset is -MAX_BPF_STACK for writes, and * -state->allocated_stack for reads. */ -static int check_stack_slot_within_bounds (int off, -struct bpf_func_state *state, -enum bpf_access_type t) +static int check_stack_slot_within_bounds (struct bpf_verifier_env *env, +int off, +struct bpf_func_state *state, +enum bpf_access_type t) {int min_valid_off; -if (t ==BPF_WRITE ) +if (t ==BPF_WRITE || env->allow_uninit_stack) min_valid_off = -MAX_BPF_STACK ;else min_valid_off = -state->allocated_stack;
@@ -6822,9 +6810,9 @@ static int check_stack_access_within_bounds( max_off = min_off; } - err =check_stack_slot_within_bounds (min_off, state, type); + err =check_stack_slot_within_bounds (env, min_off, state, type);if (!err) - err =check_stack_slot_within_bounds (max_off, state, type); + err =check_stack_slot_within_bounds (env, max_off, state, type);if (err) {if (tnum_is_const (reg->var_off)) {
@@ -6837,8 +6825,10 @@ static int check_stack_access_within_bounds(verbose (env,"invalid variable-offset%s stack R%d var_off=%s size=%d\n" , err_extra, regno, tn_buf, access_size); } +return err; } -return err; + +return grow_stack_state (env, state,round_up (-min_off,BPF_REG_SIZE )); }/* check whether memory at (regno + off) is accessible for t = (read | write)
@@ -6853,7 +6843,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn {struct bpf_reg_state *regs =cur_regs (env);struct bpf_reg_state *reg = regs + regno; -struct bpf_func_state *state;int size, err =0 ; size =bpf_size_to_bytes (bpf_size);
@@ -6996,11 +6985,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnif (err)return err; - state =func (env, reg); - err =update_stack_depth (env, state, off); -if (err) -return err; -if (t ==BPF_READ ) err =check_stack_read (env, regno, off, size, value_regno);
@@ -7195,7 +7179,8 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i/* When register 'regno' is used to read the stack (either directly or through * a helper function) make sure that it's within stack boundary and, depending -* on the access type, that all elements of the stack are initialized. +* on the access type and privileges, that all elements of the stack are +* initialized. * * 'off' includes 'regno->off', but not its dynamic part (if any). *
@@ -7303,8 +7288,11 @@ static int check_stack_range_initialized( slot = -i -1 ; spi = slot /BPF_REG_SIZE ; -if (state->allocated_stack <= slot) - goto err; +if (state->allocated_stack <= slot) { +verbose (env,"verifier bug: allocated_stack too small" ); +return -EFAULT ; + } + stype = &state->stack[spi].slot_type[slot %BPF_REG_SIZE ];if (*stype ==STACK_MISC ) goto mark;
@@ -7328,7 +7316,6 @@ static int check_stack_range_initialized( goto mark; } -err:if (tnum_is_const (reg->var_off)) {verbose (env,"invalid%s read from stack R%d off %d+%d size %d\n" , err_extra, regno, min_off, i - min_off, access_size);
@@ -7353,7 +7340,7 @@ static int check_stack_range_initialized(* helper may write to the entire memory range. */ } -return update_stack_depth (env, state, min_off); +return 0 ; } static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
tools/testing/selftests/bpf/progs/iters.c +1 -1
@@ -846,7 +846,7 @@ __naked int delayed_precision_mark(void)"call %[bpf_iter_num_next];" "if r0 == 0 goto 2f;" "if r6 != 42 goto 3f;" -"r7 = -32;" +"r7 = -33;" "call %[bpf_get_prandom_u32];" "r6 = r0;" "goto 1b;\n"
tools/testing/selftests/bpf/progs/test_global_func16.c +1 -1
@@ -13,7 +13,7 @@ __noinline int foo(int (*arr)[10]) } SEC("cgroup_skb/ingress") -__failure __msg("invalid indirect read from stack") +__successint global_func16 (struct __sk_buff *skb) {int array[10 ];
tools/testing/selftests/bpf/progs/verifier_basic_stack.c +4 -4
@@ -27,8 +27,8 @@ __naked void stack_out_of_bounds(void) SEC("socket") __description("uninitialized stack1") -__failure __msg("invalid indirect read from stack") -__failure_unpriv +__success __log_level(4) __msg("stack depth 8") +__failure_unpriv __msg_unpriv("invalid indirect read from stack") __naked void uninitialized_stack1(void) { asm volatile (" \
@@ -45,8 +45,8 @@ __naked void uninitialized_stack1(void) SEC("socket") __description("uninitialized stack2") -__failure __msg("invalid read from stack") -__failure_unpriv +__success __log_level(4) __msg("stack depth 8") +__failure_unpriv __msg_unpriv("invalid read from stack") __naked void uninitialized_stack2(void) { asm volatile (" \
tools/testing/selftests/bpf/progs/verifier_int_ptr.c +3 -2
@@ -5,9 +5,10 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" -SEC("cgroup/sysctl") +SEC("socket") __description("ARG_PTR_TO_LONG uninitialized") -__failure __msg("invalid indirect read from stack R4 off -16+0 size 8") +__success +__failure_unpriv __msg_unpriv("invalid indirect read from stack R4 off -16+0 size 8")__naked void arg_ptr_to_long_uninitialized (void ) { asm volatile (" \
tools/testing/selftests/bpf/progs/verifier_raw_stack.c +3 -2
@@ -5,9 +5,10 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" -SEC("tc") +SEC("socket") __description("raw_stack: no skb_load_bytes") -__failure __msg("invalid read from stack R6 off=-8 size=8") +__success +__failure_unpriv __msg_unpriv("invalid read from stack R6 off=-8 size=8")__naked void stack_no_skb_load_bytes (void ) { asm volatile (" \
tools/testing/selftests/bpf/progs/verifier_var_off.c +51 -11
93 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/atomic_cmpxchg.c +0 -11
@@ -83,17 +83,6 @@ .result = REJECT, .errstr = "!read_ok", }, -{ - "Can't use cmpxchg on uninit memory", - .insns = { -BPF_MOV64_IMM (BPF_REG_0 ,3 ), -BPF_MOV64_IMM (BPF_REG_2 ,4 ), -BPF_ATOMIC_OP (BPF_DW ,BPF_CMPXCHG ,BPF_REG_10 ,BPF_REG_2 ,-8 ), - BPF_EXIT_INSN(), - }, - .result =REJECT , - .errstr = "invalid read from stack", -}, { "BPF_W cmpxchg should zero top 32 bits", .insns = {
tools/testing/selftests/bpf/verifier/calls.c +3 -1
@@ -1505,7 +1505,9 @@ .prog_type = BPF_PROG_TYPE_XDP, .fixup_map_hash_8b = { 23 }, .result = REJECT, - .errstr ="invalid read from stack R7 off=-16 size=8" , + .errstr = "R0 invalid mem access 'scalar'", + .result_unpriv = REJECT, + .errstr_unpriv ="invalid read from stack R7 off=-16 size=8" , }, { "calls: two calls that receive map_value via arg=ptr_stack_of_caller. test1",
Signature · 1 line
2.40.1
PATCH v3 3/3 bpf: minor cleanup around stack bounds
In reply to Andrei MateiPush the rounding up of stack offsets into the function responsible for growing the stack, rather than relying on all the callers to do it. Uncertainty about whether the callers did it or not tripped up people in a previous review.
--- kernel/bpf/verifier.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +7 -1
@@ -1690,6 +1690,9 @@ static int resize_reference_state(struct bpf_func_state *state, size_t n)*/ static int grow_stack_state (struct bpf_verifier_env *env,struct bpf_func_state *state,int size) { +// The stack size is always a multiple of BPF_REG_SIZE. + size =round_up (size,BPF_REG_SIZE ); +size_t old_n = state->allocated_stack /BPF_REG_SIZE , n = size /BPF_REG_SIZE ;if (old_n >= n)
@@ -6828,7 +6831,10 @@ static int check_stack_access_within_bounds(return err; } -return grow_stack_state (env, state,round_up (-min_off,BPF_REG_SIZE )); +/* Note that there is no stack access with offset zero, so the needed stack +* size is -min_off, not -min_off+1. +*/ +return grow_stack_state (env, state, -min_off/* size */ ); }/* check whether memory at (regno + off) is accessible for t = (read | write)
Signature · 1 line
2.40.1
[...]
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_raw_stack.c +0
-0@@ -5,9 +5,10 @@#include <bpf/bpf_helpers.h>
tools/testing/selftests/bpf/progs/verifier_raw_stack.c line 13
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_raw_stack.c +3
-2#include "bpf_misc.h" -SEC("tc") +SEC("socket") __description("raw_stack: no skb_load_bytes") -__failure __msg("invalid read from stack R6 off=-8 size=8") +__success +__failure_unpriv __msg_unpriv("invalid read from stack R6 off=-8 size=8")__naked void stack_no_skb_load_bytes (void ) {
Please confirm that changing this program type is OK. I wasn't sure here.
[...]
Eduard Zingerman
maintainerQuoted 19 lines · Andrei Matei
[...]
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_raw_stack.c +0
-0@@ -5,9 +5,10 @@#include <bpf/bpf_helpers.h> tools/testing/selftests/bpf/progs/verifier_raw_stack.c line 13
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_raw_stack.c +3
-2#include "bpf_misc.h" -SEC("tc") +SEC("socket") __description("raw_stack: no skb_load_bytes") -__failure __msg("invalid read from stack R6 off=-8 size=8") +__success +__failure_unpriv __msg_unpriv("invalid read from stack R6 off=-8 size=8")__naked void stack_no_skb_load_bytes (void ) {Please confirm that changing this program type is OK. I wasn't sure here.
Quoted 1 line
[...]
lgtm, does not seem matter in this case.
Re: [PATCH bpf v3 1/3] bpf: add some comments to stack representation
In reply to Andrei MateiQuoted 1 line · Andrei Matei
Please add some commit message here, even if a single sentence one.
Quoted 41 lines
Signed-off-by Andrei Matei <[email protected]>--- include/linux/bpf_verifier.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/linux/bpf_verifier.h +14
-1@@ -316,7 +316,17 @@ struct bpf_func_state {/* The following fields should be last. See copy_func_state() */ int acquired_refs;struct bpf_reference_state *refs; +/* Size of the current stack, in bytes. The stack state is tracked below, in +* `stack`. allocated_stack is always a multiple of BPF_REG_SIZE. +*/ int allocated_stack; +/* The state of the stack. Each element of the array describes BPF_REG_SIZE +* (i.e. 8) bytes worth of stack memory. +* stack[0] represents bytes [*(r10-8)..*(r10-1)] +* stack[1] represents bytes [*(r10-16)..*(r10-9)] +* ... +* stack[allocated_stack/8 - 1] represents [*(r10-allocated_size)..*(r10-allocated_size+7)] +*/ struct bpf_stack_state *stack; };@@ -630,6 +640,10 @@ struct bpf_verifier_env {int exception_callback_subprog;bool explore_alu_limits;bool allow_ptr_leaks; +/* Allow access to uninitialized stack memory. Writes with fixed offset are +* always allowed, so this refers to reads (with fixed or variable offset), +* to writes with variable offset and to indirect (helper) accesses. +*/ bool allow_uninit_stack;bool bpf_capable;bool bypass_spec_v1; --2.40.1
Quoted 47 lines · Andrei Matei
Privileged programs are supposed to be able to read uninitialized stack memory (ever since
6715df8d5) but, before this patch, these accesses were permitted inconsistently. In particular, accesses were permitted abovestate->allocated_stack, but not below it. In other words, if the stack was already "large enough", the access was permitted, but otherwise the access was rejected instead of being allowed to "grow the stack". This undesired rejection was happening in two places:
- incheck_stack_slot_within_bounds()
- incheck_stack_range_initialized()
This patch arranges for these accesses to be permitted. A bunch of tests that were relying on the old rejection had to change; all of them were changed to add also run unprivileged, in which case the old behavior persists. One tests couldn't be updated -global_func16- because it can't run unprivileged for other reasons.This patch also fixes the tracking of the stack size for variable-offset reads. This second fix is bundled in the same commit as the first one because they're inter-related. Before this patch, writes to the stack using registers containing a variable offset (as opposed to registers with fixed, known values) were not properly contributing to the function's needed stack size. As a result, it was possible for a program to verify, but then to attempt to read out-of-bounds data at runtime because a too small stack had been allocated for it.
Each function tracks the size of the stack it needs in bpf_subprog_info.stack_depth, which is maintained by
update_stack_depth(). For regular memory accesses,check_mem_access()was callingupdate_state_depth()but it was passing in only the fixed part of the offset register, ignoring the variable offset. This was incorrect; the minimum possible value of that register should be used instead.This tracking is now fixed by centralizing the tracking of stack size in
grow_stack_state(), and by lifting the calls togrow_stack_state()tocheck_stack_access_within_bounds()as suggested by Andrii. The code is now simpler and more convincingly tracks the correct maximum stack size.check_stack_range_initialized()can now rely on enough stack having been allocated for the access; this helps with the fix for the first issue.A few tests were changed to also check the stack depth computation. The one that fails without this patch is verifier_var_off:stack_write_priv_vs_unpriv.
Reported-by Hao Sun <[email protected]>Fixes 01f810ace9ed3 ("bpf: Allow variable-offset stack access")Closes https://lore.kernel.org/bpf/CABWLsev9g8UP_c3a=1qbuZUi20tGoUXoU07FPf-5FLvhOKOY+Q@mail.gmail.com/ open hereSigned-off-by Andrei Matei <[email protected]>
Quoted 12 lines
--- kernel/bpf/verifier.c | 67 ++++++++----------- tools/testing/selftests/bpf/progs/iters.c | 2 +- .../selftests/bpf/progs/test_global_func16.c | 2 +- .../bpf/progs/verifier_basic_stack.c | 8 +-- .../selftests/bpf/progs/verifier_int_ptr.c | 5 +- .../selftests/bpf/progs/verifier_raw_stack.c | 5 +- .../selftests/bpf/progs/verifier_var_off.c | 62 ++++++++++++++--- .../selftests/bpf/verifier/atomic_cmpxchg.c | 11 --- tools/testing/selftests/bpf/verifier/calls.c | 4 +- 9 files changed, 93 insertions(+), 73 deletions(-)
LGTM, thanks! (and yes, changing prog type for test is fine)
Quoted 8 lines
kernel/bpf/verifier.c +0
-0@@ -1685,7 +1685,10 @@ static int resize_reference_state(struct bpf_func_state *state, size_t n)return 0 ; }
[...]
Re: [PATCH bpf v3 3/3] bpf: minor cleanup around stack bounds
In reply to Andrei MateiQuoted 14 lines · Andrei Matei
Push the rounding up of stack offsets into the function responsible for growing the stack, rather than relying on all the callers to do it. Uncertainty about whether the callers did it or not tripped up people in a previous review.
--- kernel/bpf/verifier.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int resize_reference_state(struct bpf_func_state *state, size_t n) · line 1693
Quoted 4 lines
kernel/bpf/verifier.c +1
-0@@ -1690,6 +1690,9 @@ static int resize_reference_state(struct bpf_func_state *state, size_t n)*/ static int grow_stack_state (struct bpf_verifier_env *env,struct bpf_func_state *state,int size) { +// The stack size is always a multiple of BPF_REG_SIZE.
let's not use C++-style comments
kernel/bpf/verifier.c static int resize_reference_state(struct bpf_func_state *state, size_t n) · line 1695
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ size =round_up (size,BPF_REG_SIZE ); +
C89 style doesn't allow variable declarations intermixed with code, so you'll have to do this after declaring variables
kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 6837
Quoted 12 lines
kernel/bpf/verifier.c +4
-1size_t old_n = state->allocated_stack /BPF_REG_SIZE , n = size /BPF_REG_SIZE ;if (old_n >= n)@@ -6828,7 +6831,10 @@ static int check_stack_access_within_bounds(return err; } -return grow_stack_state (env, state,round_up (-min_off,BPF_REG_SIZE )); +/* Note that there is no stack access with offset zero, so the needed stack +* size is -min_off, not -min_off+1. +*/ +return grow_stack_state (env, state, -min_off/* size */ );
hmm.. there is still a grow_stack_state() call in check_stack_write_fixed_off(), right? Which is not necessary because we do check_stack_access_within_bounds() before that one. Can you drop it as part of patch #2?
Quoted 6 lines
kernel/bpf/verifier.c +0
-1}/* check whether memory at (regno + off) is accessible for t = (read | write) -- 2.40.1
Eduard Zingerman
maintainerRe: [PATCH bpf v3 1/3] bpf: add some comments to stack representation
In reply to Andrei MateiQuoted 2 lines · Andrei Matei
Signed-off-by Andrei Matei <[email protected]>---
[...]
include/linux/bpf_verifier.h struct bpf_func_state { · line 329
Quoted 8 lines
include/linux/bpf_verifier.h +7
-0int allocated_stack; +/* The state of the stack. Each element of the array describes BPF_REG_SIZE +* (i.e. 8) bytes worth of stack memory. +* stack[0] represents bytes [*(r10-8)..*(r10-1)] +* stack[1] represents bytes [*(r10-16)..*(r10-9)] +* ... +* stack[allocated_stack/8 - 1] represents [*(r10-allocated_size)..*(r10-allocated_size+7)] +*/
Nit: this uses variable 'allocated_size' that does not seem defined. Otherwise a nice comment, thanks.
Eduard Zingerman
maintainerRe: [PATCH bpf v3 3/3] bpf: minor cleanup around stack bounds
In reply to Andrii NakryikoOn Mon, 2023-12-04 at 10:19 -0800, Andrii Nakryiko wrote:
[...]
Quoted 4 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -6828,7 +6831,10 @@ static int check_stack_access_within_bounds(return err; }
Quoted 10 lines
kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 6837
Quoted 5 lines
kernel/bpf/verifier.c +4
-1-return grow_stack_state (env, state,round_up (-min_off,BPF_REG_SIZE )); +/* Note that there is no stack access with offset zero, so the needed stack +* size is -min_off, not -min_off+1. +*/ +return grow_stack_state (env, state, -min_off/* size */ );hmm.. there is still a
grow_stack_state()call incheck_stack_write_fixed_off(), right? Which is not necessary because we docheck_stack_access_within_bounds()before that one. Can you drop it as part of patch #2?
I'm not sure I understand what you mean. Patch #2 (v3) drops grow_stack_state() from check_stack_write_fixed_off() so all seems good?
Re: [PATCH bpf v3 3/3] bpf: minor cleanup around stack bounds
Quoted 18 lines · Eduard Zingerman
On Mon, 2023-12-04 at 10:19 -0800, Andrii Nakryiko wrote:
[...]Quoted 4 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -6828,7 +6831,10 @@ static int check_stack_access_within_bounds(return err; }Quoted 10 lines
kernel/bpf/verifier.c static int check_stack_access_within_bounds( · line 6837
Quoted 5 lines
kernel/bpf/verifier.c +4
-1-return grow_stack_state (env, state,round_up (-min_off,BPF_REG_SIZE )); +/* Note that there is no stack access with offset zero, so the needed stack +* size is -min_off, not -min_off+1. +*/ +return grow_stack_state (env, state, -min_off/* size */ );hmm.. there is still a
grow_stack_state()call incheck_stack_write_fixed_off(), right? Which is not necessary because we docheck_stack_access_within_bounds()before that one. Can you drop it as part of patch #2?
Quoted 3 lines
I'm not sure I understand what you mean. Patch #2 (v3) drops
grow_stack_state()fromcheck_stack_write_fixed_off()so all seems good?
I swear I checked for that, both by re-reading the patch and by searching in the browser. And check_stack_write_fixed_off() was nowhere to be found. Now triple checking that I see that it's Gmail's smartness that collapsed that portion of the patch (but not the others!) into a subtle triple dot region, which made all that invisible and non-searchable, sigh...
Sorry for the noise, all good then.