Commit f732655e authored by Andrei Matei's avatar Andrei Matei Committed by Pu Lehui
Browse files

bpf: Protect against int overflow for stack access size

stable inclusion
from stable-v5.10.215
commit 9970e059af471478455f9534e8c3db82f8c5496d
category: bugfix
bugzilla: https://gitee.com/src-openeuler/kernel/issues/I9QG3A
CVE: CVE-2024-35905

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=9970e059af47



--------------------------------

[ Upstream commit ecc6a2101840177e57c925c102d2d29f260d37c8 ]

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: default avatar <syzbot+33f4297b5f927648741a@syzkaller.appspotmail.com>
Reported-by: default avatar <syzbot+aafd0513053a1cbf52ef@syzkaller.appspotmail.com>
Closes: https://lore.kernel.org/bpf/CAADnVQLORV5PT0iTAhRER+iLBTkByCYNBYyvBSgjN1T31K+gOw@mail.gmail.com/


Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAndrei Matei <andreimatei1@gmail.com>
Link: https://lore.kernel.org/r/20240327024245.318299-3-andreimatei1@gmail.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarPu Lehui <pulehui@huawei.com>
parent ebe8b9e4
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -3946,6 +3946,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)) {