]> www.infradead.org Git - linux.git/commitdiff
bpf: Protect against int overflow for stack access size
authorAndrei Matei <andreimatei1@gmail.com>
Wed, 27 Mar 2024 02:42:45 +0000 (22:42 -0400)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 27 Mar 2024 16:56:36 +0000 (09:56 -0700)
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 a833a17aeac7.

Fixes: a833a17aeac7 ("bpf: Fix verification of indirect var-off stack access")
Reported-by: syzbot+33f4297b5f927648741a@syzkaller.appspotmail.com
Reported-by: syzbot+aafd0513053a1cbf52ef@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/bpf/CAADnVQLORV5PT0iTAhRER+iLBTkByCYNBYyvBSgjN1T31K+gOw@mail.gmail.com/
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Andrei Matei <andreimatei1@gmail.com>
Link: https://lore.kernel.org/r/20240327024245.318299-3-andreimatei1@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c

index 0bfc0050db28d46b1e1145483774efde89ecf74c..353985b2b6a279f0d5a1784d753b10002cfb216e 100644 (file)
@@ -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)) {