Skip to content

Commit 2929bfa

Browse files
andreimateianakryiko
authored andcommitted
bpf: Minor cleanup around stack bounds
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. Signed-off-by: Andrei Matei <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6b4a64b commit 2929bfa

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,11 @@ static int resize_reference_state(struct bpf_func_state *state, size_t n)
12641264
*/
12651265
static int grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)
12661266
{
1267-
size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE;
1267+
size_t old_n = state->allocated_stack / BPF_REG_SIZE, n;
1268+
1269+
/* The stack size is always a multiple of BPF_REG_SIZE. */
1270+
size = round_up(size, BPF_REG_SIZE);
1271+
n = size / BPF_REG_SIZE;
12681272

12691273
if (old_n >= n)
12701274
return 0;
@@ -6638,7 +6642,10 @@ static int check_stack_access_within_bounds(
66386642
return err;
66396643
}
66406644

6641-
return grow_stack_state(env, state, round_up(-min_off, BPF_REG_SIZE));
6645+
/* Note that there is no stack access with offset zero, so the needed stack
6646+
* size is -min_off, not -min_off+1.
6647+
*/
6648+
return grow_stack_state(env, state, -min_off /* size */);
66426649
}
66436650

66446651
/* check whether memory at (regno + off) is accessible for t = (read | write)

0 commit comments

Comments
 (0)