Skip to content

Commit

Permalink
bpf: Protect against int overflow for stack access size
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
andreimatei authored and Alexei Starovoitov committed Mar 27, 2024
1 parent a8d89fe commit ecc6a21
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -6701,6 +6701,11 @@ static int check_stack_access_within_bounds(
err = check_stack_slot_within_bounds(env, min_off, state, type);
if (!err && max_off > 0)
err = -EINVAL; /* out of stack access into non-negative offsets */
if (!err && access_size < 0)
/* access_size should not be negative (or overflow an int); others checks
* along the way should have prevented such an access.
*/
err = -EFAULT; /* invalid negative access size; integer overflow? */

if (err) {
if (tnum_is_const(reg->var_off)) {
Expand Down

0 comments on commit ecc6a21

Please sign in to comment.