bpf: Minor cleanup around stack bounds
authorAndrei Matei <andreimatei1@gmail.com>
Fri, 8 Dec 2023 03:25:19 +0000 (22:25 -0500)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 8 Dec 2023 22:19:00 +0000 (14:19 -0800)
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 <andreimatei1@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20231208032519.260451-4-andreimatei1@gmail.com
kernel/bpf/verifier.c

index de1e29fa467ed1f8c94552339f0e35d27392b30f..fb690539d5f635cf7ba6231887d8d1223e468c87 100644 (file)
@@ -1264,7 +1264,11 @@ 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)
 {
-       size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE;
+       size_t old_n = state->allocated_stack / BPF_REG_SIZE, n;
+
+       /* The stack size is always a multiple of BPF_REG_SIZE. */
+       size = round_up(size, BPF_REG_SIZE);
+       n = size / BPF_REG_SIZE;
 
        if (old_n >= n)
                return 0;
@@ -6638,7 +6642,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)