From nobody Wed Sep 30 16:20:13 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A97E19B5B1; Fri, 7 Aug 2026 02:06:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786068402; cv=none; b=Sju70OFknf8+PsNxBrtSDxaa610eCsbgfequKRtQ0bqBTYgZVhUtP+llog1RwcnmPs+ZIKTyeMDg6fK1tiwd0DzBpxpugLZH1ibLoydA/2SAx07bop9UcM24DRbLXBtYr2SSHjuiKNspk2sVXWziih4Ckiap6EKuyDxNSq/zmPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786068402; c=relaxed/simple; bh=J3owbIxUWdCS03veoImOxU0NOsa7GlCCZs27wZvsYcg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rk4o/Wf3KDuOtL1Nhj174tUdrxO+VwnFM+U4QCFQpu8yvpiE0mx47vrVRZTGGEnE52xTyVsH9oP1IGFYZ8DOWn08LggTotnzqPZ9XC2gV62yk/GiEBGefqzeseM3yde3x22XwZMPGdjb1wAdWPDJ1SDWE1e0m4rLLVXQxOxOFrE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=aJnOggSU; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="aJnOggSU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=n4 9WzXlUXknWqJJoFZvX46j5Iuso8UbjfkpiYYOIJcM=; b=aJnOggSUPByidclQEB aTHltcv094BPVj2Uk0SdZcPuctVdOnTbEnLpyBA4hrLbnh7wqjJr9A1Zvv83m0So uKJDl+0xnlUmdmfEh9OiIBY5G93p8FMUbeTcGT5koBPCvQkegKRFHy4s6ycBKt6v bml+u68rQGNy2UBHructlsgdE= Received: from nec8-i7 (unknown []) by gzga-smtp-mtada-g1-2 (Coremail) with SMTP id _____wCXnpiGPXVqyp9rNw--.41530S3; Fri, 07 Aug 2026 10:06:03 +0800 (CST) From: chenyuan_fl@163.com To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Yuan Chen Subject: [PATCH 1/2] bpf: Fix queue/stack map u32 index overflow Date: Fri, 7 Aug 2026 10:05:47 +0800 Message-ID: <20260807020548.1376979-2-chenyuan_fl@163.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260807020548.1376979-1-chenyuan_fl@163.com> References: <20260807020548.1376979-1-chenyuan_fl@163.com> 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-CM-TRANSID: _____wCXnpiGPXVqyp9rNw--.41530S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7KFy5XFy5uw1rCr4fuFyxGrg_yoW8Wr47pF s8Ja4rX3WDtr47G3yktw4DWF9xWws5GFZxKF9rWryvyrn3GryxWry8KF4agr9ayrn2y3Wa q3ZFqrZxC3yUZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jjJPiUUUUU= X-CM-SenderInfo: xfkh05pxdqswro6rljoofrz/xtbC5QtpJ2p1PYutRAAA3g Content-Type: text/plain; charset="utf-8" From: Yuan Chen The queue/stack map addresses elements[] with the product of a u32 head/tail index and value_size, but the storage itself is allocated in 64-bit arithmetic. When max_entries * value_size exceeds U32_MAX, the product wraps and push/peek/pop operate on the wrong element, corrupting map data and leaking stale values to user space. The original bound check was removed by c85d69135a91 ("bpf: move memory size checks to bpf_map_charge_init()"), which migrated only the bytes-to-pages conversion and dropped the overflow guard, so oversized queue/stack maps can be created again. Restore the bound in queue_stack_map_alloc_check(): reject maps whose element storage would exceed U32_MAX bytes, keeping the u32 index multiplication overflow-free. Fixes: c85d69135a91 ("bpf: move memory size checks to bpf_map_charge_init()= ") Signed-off-by: Yuan Chen --- kernel/bpf/queue_stack_maps.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/bpf/queue_stack_maps.c b/kernel/bpf/queue_stack_maps.c index c1c9dee4dcdd..f9ff701ab27b 100644 --- a/kernel/bpf/queue_stack_maps.c +++ b/kernel/bpf/queue_stack_maps.c @@ -59,6 +59,13 @@ static int queue_stack_map_alloc_check(union bpf_attr *a= ttr) */ return -E2BIG; =20 + /* + * The u32 head/tail index is multiplied by value_size to address + * elements[]. Bound the map size so the product cannot overflow. + */ + if ((u64)attr->max_entries * attr->value_size > U32_MAX) + return -E2BIG; + return 0; } =20 --=20 2.54.0 From nobody Wed Sep 30 16:20:13 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CAC13C4551; Fri, 7 Aug 2026 02:06:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786068410; cv=none; b=F0Xghu/c9GfewHmKQF7SQ11mXAlerqWhxCXo3BLJ3IX7y3OmYwrDFz7eDh2Cj115PQi+3iSPsgwA3v0AJlrwm9fuGccSnQHf2EXSJ5JNSMjbyllZEyQxKgVqZu8+G8rHALJ24N7D8nd2Oa3rcJ2GZHz/EIAMbfVtr+nt5aPWlzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786068410; c=relaxed/simple; bh=gEPegvIxEWeSeyKAMyz6rS/H2geQgcqk1OwE7i0AdP4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=utpcpUL+rfJqAP+ib0shkWEQfoSbcWyomNNB9iAEEvMjLP+OUWdeIu40Yx4VuPvhSHgoCRGU3t/mv5mMRHWlpJxrK9d6sZuRumpodQLlJh0u2tDEzg+ObQtilesN2eDUVGO9Zy55tu/SaRM1KBUtQ/T86izzQVFDbv+bYl5JTic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=K6aClThy; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="K6aClThy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=Ix i2Nqgqjg61OldYMyrwA7BepICp+2fudiRBHJKOLE8=; b=K6aClThyeSVheiz5Hx TLBHirEoF2UzXyTYBsTtigWEUVn20NEcOedKZOdxFnWDIxJNVL5pQgOxW+zFAvQE PY68e6vjvhnoDnnV//lNnl8ppj+CLYV5SmYGmORWhj0iiEcgwJ7sUCNbG5JNo6bk svNa8HhXMSRry9vJoq98p0thU= Received: from nec8-i7 (unknown []) by gzga-smtp-mtada-g1-2 (Coremail) with SMTP id _____wCXnpiGPXVqyp9rNw--.41530S4; Fri, 07 Aug 2026 10:06:05 +0800 (CST) From: chenyuan_fl@163.com To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Yuan Chen Subject: [PATCH 2/2] selftests/bpf: Add regression test for queue/stack map size limit Date: Fri, 7 Aug 2026 10:05:48 +0800 Message-ID: <20260807020548.1376979-3-chenyuan_fl@163.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260807020548.1376979-1-chenyuan_fl@163.com> References: <20260807020548.1376979-1-chenyuan_fl@163.com> 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-CM-TRANSID: _____wCXnpiGPXVqyp9rNw--.41530S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7Kw1UWFy3Zw1rWFy7CrWruFg_yoW5Jr15pa 95Xw43KFW8JrsrJ3y5tw4UXFZ5Wwn5WryUKFn7Kr9rJrnIqr1xWr48KFyUGFs3CrWkZa15 Zw1vgrZ3Gw47K3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jvVbgUUUUU= X-CM-SenderInfo: xfkh05pxdqswro6rljoofrz/xtbC5g1qKGp1PY1oFwAA35 Content-Type: text/plain; charset="utf-8" From: Yuan Chen queue/stack maps address elements[] with a u32 head/tail index multiplied by value_size, so maps whose element storage exceeds U32_MAX bytes must be rejected at creation time (see the fix in queue_stack_map_alloc_check()). Verify that creating a queue/stack map with max_entries * value_size > U32_MAX (8192 * 1MB) fails with -E2BIG, and that a normal-sized map is still created successfully. Signed-off-by: Yuan Chen --- .../bpf/prog_tests/queue_stack_map.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/queue_stack_map.c b/too= ls/testing/selftests/bpf/prog_tests/queue_stack_map.c index 41441325e179..6f8ac285e719 100644 --- a/tools/testing/selftests/bpf/prog_tests/queue_stack_map.c +++ b/tools/testing/selftests/bpf/prog_tests/queue_stack_map.c @@ -101,8 +101,42 @@ static void test_queue_stack_map_by_type(int type) bpf_object__close(obj); } =20 +static void test_queue_stack_map_alloc_check(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts); + const __u32 big_value =3D 1 << 20; /* 1MB */ + int fd, saved_errno; + + /* Regression test for the u32 index overflow in queue/stack maps: + * a map whose element storage (max_entries * value_size) exceeds + * U32_MAX bytes must be rejected at creation time, otherwise the + * u32 head/tail index multiplication wraps and push/peek/pop + * address the wrong element. 8192 * 1MB =3D 8GB > U32_MAX. + */ + fd =3D bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0, big_value, 8192, &opts= ); + saved_errno =3D errno; + ASSERT_LT(fd, 0, "queue_oversize_fd"); + ASSERT_EQ(saved_errno, E2BIG, "queue_oversize_errno"); + if (fd >=3D 0) + close(fd); + + fd =3D bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 0, big_value, 8192, &opts= ); + saved_errno =3D errno; + ASSERT_LT(fd, 0, "stack_oversize_fd"); + ASSERT_EQ(saved_errno, E2BIG, "stack_oversize_errno"); + if (fd >=3D 0) + close(fd); + + /* A normal-sized map must still be created successfully. */ + fd =3D bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0, 64, 100, &opts); + ASSERT_GE(fd, 0, "queue_normal_fd"); + if (fd >=3D 0) + close(fd); +} + void test_queue_stack_map(void) { test_queue_stack_map_by_type(QUEUE); test_queue_stack_map_by_type(STACK); + test_queue_stack_map_alloc_check(); } --=20 2.54.0