From nobody Sun Oct 5 22:01:44 2025 Received: from plesk.hostmyservers.fr (plesk.hostmyservers.fr [45.145.164.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AEDD22B8A5; Tue, 29 Jul 2025 09:19:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.145.164.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753780754; cv=none; b=YscFz3kqhhQ1xgrn2uhIiN8FTTWIrEG2KtAfrvHXrji4m+ljoh/3X90hbUcDm5IvdQnn5dbnMX7VBJzdeJa3yR6+hVrEvEY/0OTvK4mSsgzIRCYCgUFOPrGDl7nLA6oQTreA/oQAxCzk41OlgKLtJYv3x01Kls3/AFFrI3AcA7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753780754; c=relaxed/simple; bh=MWN/4Wglm/rTn9ibOPTMJxkL74bcwbr8wWIlo15GG7o=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=fkr7Y4AhXTadyzYlfFyLXP3hLN2BB1OxK1Za9f0rMrBNpIB38VjOqcWNPFeXMlzv2PxC9YvjM+Gh81+Dsu44QvAgPq3xeBKLDirwYpS83AGNEetZDUqQR64+xrb87xJHjoTE/LtkFMh1W1g0b30YjFyOZJR24PLM2jrEv001+Wk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=arnaud-lcm.com; spf=pass smtp.mailfrom=arnaud-lcm.com; arc=none smtp.client-ip=45.145.164.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=arnaud-lcm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arnaud-lcm.com Received: from 7cf34dd3dd11.ant.amazon.com (unknown [15.248.2.230]) by plesk.hostmyservers.fr (Postfix) with ESMTPSA id D2AE640420; Tue, 29 Jul 2025 09:19:08 +0000 (UTC) Authentication-Results: Plesk; spf=pass (sender IP is 15.248.2.230) smtp.mailfrom=contact@arnaud-lcm.com smtp.helo=7cf34dd3dd11.ant.amazon.com Received-SPF: pass (Plesk: connection is authenticated) From: Arnaud Lecomte To: song@kernel.org Cc: jolsa@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com, Arnaud Lecomte Subject: [PATCH] bpf: fix stackmap overflow check in __bpf_get_stackid() Date: Tue, 29 Jul 2025 10:19:01 +0100 Message-ID: <20250729091901.26436-1-contact@arnaud-lcm.com> X-Mailer: git-send-email 2.49.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-PPP-Message-ID: <175378074970.13305.6575133741608671950@Plesk> X-PPP-Vhost: arnaud-lcm.com Content-Type: text/plain; charset="utf-8" Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stackid() when copying stack trace data. The issue occurs when the perf trace contains more stack entries than the stack map bucket can hold, leading to an out-of-bounds write in the bucket's data array. For build_id mode, we use sizeof(struct bpf_stack_build_id) to determine capacity, and for normal mode we use sizeof(u64). Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3Dc9b724fbb41cf2538b7b Tested-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com Signed-off-by: Arnaud Lecomte --- kernel/bpf/stackmap.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 3615c06b7dfa..0f9f6e4b6fe9 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -230,7 +230,7 @@ static long __bpf_get_stackid(struct bpf_map *map, struct bpf_stack_map *smap =3D container_of(map, struct bpf_stack_map, ma= p); struct stack_map_bucket *bucket, *new_bucket, *old_bucket; u32 skip =3D flags & BPF_F_SKIP_FIELD_MASK; - u32 hash, id, trace_nr, trace_len, i; + u32 hash, id, trace_nr, trace_len, i, max_depth; bool user =3D flags & BPF_F_USER_STACK; u64 *ips; bool hash_matches; @@ -241,6 +241,16 @@ static long __bpf_get_stackid(struct bpf_map *map, =20 trace_nr =3D trace->nr - skip; trace_len =3D trace_nr * sizeof(u64); + + /* Clamp the trace to max allowed depth */ + if (stack_map_use_build_id(map)) + max_depth =3D smap->map.value_size / sizeof(struct bpf_stack_build_id); + else + max_depth =3D smap->map.value_size / sizeof(u64); + + if (trace_nr > max_depth) + trace_nr =3D max_depth; + ips =3D trace->ip + skip; hash =3D jhash2((u32 *)ips, trace_len / sizeof(u32), 0); id =3D hash & (smap->n_buckets - 1); --=20 2.43.0