]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-6.1/bpf-protect-against-int-overflow-for-stack-access-si.patch
6.1-stable patches
[thirdparty/kernel/stable-queue.git] / queue-6.1 / bpf-protect-against-int-overflow-for-stack-access-si.patch
1 From 544247d439c2566d41ebf4ba2620a57b2802e37e Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 26 Mar 2024 22:42:45 -0400
4 Subject: bpf: Protect against int overflow for stack access size
5
6 From: Andrei Matei <andreimatei1@gmail.com>
7
8 [ Upstream commit ecc6a2101840177e57c925c102d2d29f260d37c8 ]
9
10 This patch re-introduces protection against the size of access to stack
11 memory being negative; the access size can appear negative as a result
12 of overflowing its signed int representation. This should not actually
13 happen, as there are other protections along the way, but we should
14 protect against it anyway. One code path was missing such protections
15 (fixed in the previous patch in the series), causing out-of-bounds array
16 accesses in check_stack_range_initialized(). This patch causes the
17 verification of a program with such a non-sensical access size to fail.
18
19 This check used to exist in a more indirect way, but was inadvertendly
20 removed in a833a17aeac7.
21
22 Fixes: a833a17aeac7 ("bpf: Fix verification of indirect var-off stack access")
23 Reported-by: syzbot+33f4297b5f927648741a@syzkaller.appspotmail.com
24 Reported-by: syzbot+aafd0513053a1cbf52ef@syzkaller.appspotmail.com
25 Closes: https://lore.kernel.org/bpf/CAADnVQLORV5PT0iTAhRER+iLBTkByCYNBYyvBSgjN1T31K+gOw@mail.gmail.com/
26 Acked-by: Andrii Nakryiko <andrii@kernel.org>
27 Signed-off-by: Andrei Matei <andreimatei1@gmail.com>
28 Link: https://lore.kernel.org/r/20240327024245.318299-3-andreimatei1@gmail.com
29 Signed-off-by: Alexei Starovoitov <ast@kernel.org>
30 Signed-off-by: Sasha Levin <sashal@kernel.org>
31 ---
32 kernel/bpf/verifier.c | 5 +++++
33 1 file changed, 5 insertions(+)
34
35 diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
36 index 1a29ac4db6eae..27cc6e3db5a86 100644
37 --- a/kernel/bpf/verifier.c
38 +++ b/kernel/bpf/verifier.c
39 @@ -4965,6 +4965,11 @@ static int check_stack_access_within_bounds(
40 err = check_stack_slot_within_bounds(min_off, state, type);
41 if (!err && max_off > 0)
42 err = -EINVAL; /* out of stack access into non-negative offsets */
43 + if (!err && access_size < 0)
44 + /* access_size should not be negative (or overflow an int); others checks
45 + * along the way should have prevented such an access.
46 + */
47 + err = -EFAULT; /* invalid negative access size; integer overflow? */
48
49 if (err) {
50 if (tnum_is_const(reg->var_off)) {
51 --
52 2.43.0
53