Skip to contentSkip to conversation

cxgb4: allocate the full ethtool filter location array

PATCH Applied Artemii Mashanov 3 participants 3 messages Nov 17 – Nov 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxgb4: allocate the full ethtool filter location array

The ethtool filter location array stores nentries u32 values, but cxgb4_init_ethtool_filters() passes nentries to kvzalloc() as a byte count. The insertion and lookup paths allow indices below nentries, which can address memory beyond the requested allocation.

Allocate nentries elements with kvcalloc() and the element size. The array remains compatible with the existing kvfree() calls.

The size mismatch was found by reviewing the allocation, the array declaration and the indexed accesses in the filter handlers.

Fixes d915c299f1da ("cxgb4: add skeleton for ethtool n-tuple filters")

---
Compile-tested cxgb4_ethtool.o with GCC 14.2 on x86-64 net/main aa40afa831eb using W=1 and KCFLAGS=-Wno-error=format-truncation. Both baseline and patched builds reported the same get_drvinfo() format-truncation warning. Hardware validation is pending.

 drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c +3 -1
index 0092bc16f887..c72c0bcd45c8 100644
@@ -2269,7 +2269,9 @@ int cxgb4_init_ethtool_filters(struct adapter *adap)
 	eth_filter->nentries = nentries;
 
 	for (i = 0; i < adap->params.nports; i++) {
-		eth_filter->port[i].loc_array = kvzalloc(nentries, GFP_KERNEL);
+		eth_filter->port[i].loc_array =
+			kvcalloc(nentries, sizeof(*eth_filter->port[i].loc_array),
+				 GFP_KERNEL);
 		if (!eth_filter->port[i].loc_array) {
 			ret = -ENOMEM;
 			goto free_eth_finfo;
maintainer Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 27 lines · Artemii Mashanov

The ethtool filter location array stores nentries u32 values, but cxgb4_init_ethtool_filters() passes nentries to kvzalloc() as a byte count. The insertion and lookup paths allow indices below nentries, which can address memory beyond the requested allocation.

Allocate nentries elements with kvcalloc() and the element size. The array remains compatible with the existing kvfree() calls.

The size mismatch was found by reviewing the allocation, the array declaration and the indexed accesses in the filter handlers.

Fixes d915c299f1da ("cxgb4: add skeleton for ethtool n-tuple filters")

---
Compile-tested cxgb4_ethtool.o with GCC 14.2 on x86-64 net/main aa40afa831eb using W=1 and KCFLAGS=-Wno-error=format-truncation. Both baseline and patched builds reported the same get_drvinfo() format-truncation warning. Hardware validation is pending.

 drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c +0 -0
index 0092bc16f887..c72c0bcd45c8 100644

drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c int cxgb4_init_ethtool_filters(struct adapter *adap) · line 2274

Quoted 7 lines
drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c +3 -1
@@ -2269,7 +2269,9 @@ int cxgb4_init_ethtool_filters(struct adapter *adap)
 	eth_filter->nentries = nentries;
 
 	for (i = 0; i < adap->params.nports; i++) {
-		eth_filter->port[i].loc_array = kvzalloc(nentries, GFP_KERNEL);
+		eth_filter->port[i].loc_array =
+			kvcalloc(nentries, sizeof(*eth_filter->port[i].loc_array),
+				 GFP_KERNEL);

Thanks, Tested on T6, ethtool -N filters at high locations now insert and delete cleanly with KASAN enabled.

Quoted 3 lines
drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c +0 -0
 		if (!eth_filter->port[i].loc_array) {
 			ret = -ENOMEM;
 			goto free_eth_finfo;
#2 lore raw
In reply to Artemii Mashanov
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Tue, 17 Nov 2026 12:06:08 +0300 you wrote:

The ethtool filter location array stores nentries u32 values, but cxgb4_init_ethtool_filters() passes nentries to kvzalloc() as a byte count. The insertion and lookup paths allow indices below nentries, which can address memory beyond the requested allocation.

Allocate nentries elements with kvcalloc() and the element size. The array remains compatible with the existing kvfree() calls.

[...]

Here is the summary with links:
- [net] cxgb4: allocate the full ethtool filter location array

    https://git.kernel.org/netdev/net/c/ad2c7240a398

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html