From: Ren Wei <weir@nebusec.ai>
To: netdev@vger.kernel.org
Cc: jiri@resnulli.us, ivecera@redhat.com, davem@davemloft.net,
edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
willemb@google.com, horms@kernel.org, chi.minghao@zte.com.cn,
vega@nebusec.ai, caoruide123@gmail.com, weir@nebusec.ai
Subject: [PATCH net 1/1] switchdev: fix deferred item flex-array allocation size
Date: Fri, 27 Nov 2026 14:20:51 +0800 [thread overview]
Message-ID: <631cf02ac9643f0bba2bc05b260e30b8d0990cfe.1795576931.git.caoruide123@gmail.com> (raw)
In-Reply-To: <cover.1795576931.git.caoruide123@gmail.com>
From: Ruide Cao <caoruide123@gmail.com>
switchdev_deferred_enqueue() passes data_len, a byte count, to
kmalloc_flex() for the flexible array in struct switchdev_deferred_item.
That array is declared as unsigned long data[], so the allocator treats
data_len as an element count and over-allocates every deferred attr or
object item by sizeof(unsigned long) (8x on 64-bit). The following
memcpy() copies only data_len bytes, so this is not an overflow; it
wastes GFP_ATOMIC memory and makes those atomic allocations more likely
to fail.
This was found with LLM assistance on the Vega platform and reproduced
with the platform PoC in QEMU. On that x86_64 6.12.94 guest, pahole
reported sizeof(struct switchdev_deferred_item) = 32 and
sizeof(struct switchdev_obj_port_mdb) = 56. A deferred HOST_MDB item
therefore requested 32 + 56 * sizeof(unsigned long) = 480 bytes instead
of 32 + 56 = 88. kmem:kmalloc traces from switchdev_deferred_enqueue
showed bytes_req=480 and bytes_alloc=1536 (CONFIG_SLUB_DEBUG=y). A
deferred attr item has the same class of error: 32 + 48 * 8 = 416 bytes
instead of 80.
Declare the payload as u8 data[] __aligned(sizeof(unsigned long)). The
members before data are all pointers, or an empty tracker, so the offset
of data is unchanged and the payload already has long alignment; the
attribute documents that contract. The old unsigned long[] array did not
provide 8-byte alignment on 32-bit either.
Fixes: d8c2858181cc ("net/switchdev: use struct_size over open coded arithmetic")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Signed-off-by: Ruide Cao <caoruide123@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
net/switchdev/switchdev.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/switchdev/switchdev.c b/net/switchdev/switchdev.c
index 474df25e96a0..fbfb25cdfa80 100644
--- a/net/switchdev/switchdev.c
+++ b/net/switchdev/switchdev.c
@@ -59,7 +59,7 @@ struct switchdev_deferred_item {
struct net_device *dev;
netdevice_tracker dev_tracker;
switchdev_deferred_func_t *func;
- unsigned long data[];
+ u8 data[] __aligned(sizeof(unsigned long));
};
static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
--
2.47.3
prev parent reply other threads:[~2026-11-27 6:21 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 6:20 [PATCH net 0/1] switchdev: fix deferred item flex-array allocation size Ren Wei
2026-11-27 6:20 ` Ren Wei [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=631cf02ac9643f0bba2bc05b260e30b8d0990cfe.1795576931.git.caoruide123@gmail.com \
--to=weir@nebusec.ai \
--cc=caoruide123@gmail.com \
--cc=chi.minghao@zte.com.cn \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=ivecera@redhat.com \
--cc=jiri@resnulli.us \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=vega@nebusec.ai \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox