Skip to content

Commit ecc6a21

Browse files
andreimateiAlexei Starovoitov
authored andcommitted
bpf: Protect against int overflow for stack access size
This patch re-introduces protection against the size of access to stack memory being negative; the access size can appear negative as a result of overflowing its signed int representation. This should not actually happen, as there are other protections along the way, but we should protect against it anyway. One code path was missing such protections (fixed in the previous patch in the series), causing out-of-bounds array accesses in check_stack_range_initialized(). This patch causes the verification of a program with such a non-sensical access size to fail. This check used to exist in a more indirect way, but was inadvertendly removed in a833a17. Fixes: a833a17 ("bpf: Fix verification of indirect var-off stack access") Reported-by: [email protected] Reported-by: [email protected] Closes: https://lore.kernel.org/bpf/CAADnVQLORV5PT0iTAhRER+iLBTkByCYNBYyvBSgjN1T31K+gOw@mail.gmail.com/ Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Andrei Matei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent a8d89fe commit ecc6a21

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6701,6 +6701,11 @@ static int check_stack_access_within_bounds(
67016701
err = check_stack_slot_within_bounds(env, min_off, state, type);
67026702
if (!err && max_off > 0)
67036703
err = -EINVAL; /* out of stack access into non-negative offsets */
6704+
if (!err && access_size < 0)
6705+
/* access_size should not be negative (or overflow an int); others checks
6706+
* along the way should have prevented such an access.
6707+
*/
6708+
err = -EFAULT; /* invalid negative access size; integer overflow? */
67046709

67056710
if (err) {
67066711
if (tnum_is_const(reg->var_off)) {

0 commit comments

Comments
 (0)