ice: fix stats array overflow via proper realloc
Subsystems (3)
Przemek Kitszel
maintainerv3 cover ice: fix stats array overflow via proper realloc
Fix OOB access to the stats arrays.
The first three commits are simple refactors to make the rest smaller, the fourth one untangles the logic/lifetime of the stats array entries, then we have the final fix by Michal for VF OOB access to the stats array.
Michal's fix was made prior to the rest, I have only slightly changed it to base it on top of the pre-work commits, that should resolve all the corner/error cases reported by AI.
The series was made anew vs the [v2], but the goal and spirit is similar, but I have started from beginning instead of playing whack-a-mole with yet another corner cases. We are also back to [v0] by Michal, as it turned out to be more elegant finish with my first four patches already intact.
My intention is a direct apply to net, as this was already (v0, v1, v2) in the past-iwl loop.
[v0]
https://lore.kernel.org/netdev/[email protected]
[v2]
https://sashiko.dev/#/message/20260812204619.32253-3-przemyslaw.kitszel%40intel.com
Michal Schmidt (1):
ice: fix stats array overflow when VF requests more queues
Przemek Kitszel (4):
ice: extract __ice_vsi_free_stats()
ice: extract ice_vsi_new_stat_arrays()
ice: extract ice_vsi_get_num_qs()
ice: rebuild ring stats arrays instead of reallocating them in place
drivers/net/ethernet/intel/ice/ice.h | 8 +- drivers/net/ethernet/intel/ice/ice_lib.h | 1 + drivers/net/ethernet/intel/ice/ice_lib.c | 295 ++++++++++++-------- drivers/net/ethernet/intel/ice/ice_vf_lib.c | 4 + 4 files changed, 183 insertions(+), 125 deletions(-)
Signature · 1 line
2.51.1
Przemek Kitszel
maintainerPATCH v3 1/5 ice: extract __ice_vsi_free_stats()
Record the length of each ring stats array in struct ice_vsi_stats, so that freeing the array entries no longer needs the owning VSI. Keep the new fields in sync in both places that size the arrays.
With that, the body of ice_vsi_free_stats() becomes independent of the VSI and can be split out as __ice_vsi_free_stats(). The @free_entries parameter is always true here; a later commit adds a caller that frees only the array container, after its entries have been handed over to a freshly allocated stats structure.
No functional change intended. Just preparation to have easier time reading subsequent commits.
--- drivers/net/ethernet/intel/ice/ice.h | 2 + drivers/net/ethernet/intel/ice/ice_lib.c | 52 ++++++++++++++---------- 2 files changed, 33 insertions(+), 21 deletions(-)
drivers/net/ethernet/intel/ice/ice.h +2 -0
@@ -328,6 +328,8 @@ enum ice_vsi_state {struct ice_vsi_stats {struct ice_ring_stats **tx_ring_stats;/* Tx ring stats array */ struct ice_ring_stats **rx_ring_stats;/* Rx ring stats array */ +u16 tx_ring_stats_len;/* Length of the Tx ring stats array */ +u16 rx_ring_stats_len;/* Length of the Rx ring stats array */ };/* struct that defines a VSI, associated with a dev */
drivers/net/ethernet/intel/ice/ice_lib.c +31 -21
96 lines not on this page · raw message
Signature · 1 line
2.51.1
Przemek Kitszel
maintainerPATCH v3 2/5 ice: extract ice_vsi_new_stat_arrays()
In reply to Przemek KitszelSplit the allocation of a struct ice_vsi_stats and its two ring stats arrays out of ice_vsi_alloc_stat_arrays(), which is left with just the lookup and the store into pf->vsi_stats[].
The sole caller keeps passing the very same queue counts as before, vsi->alloc_txq and vsi->alloc_rxq. A later commit adds a second caller in the rebuild path, which cannot use vsi->alloc_* because it has to size the arrays before the VSI is reconfigured.
Note that the error path no longer clears pf->vsi_stats[vsi->idx]; the early return above guarantees it is already NULL.
No functional change intended.
--- drivers/net/ethernet/intel/ice/ice_lib.c | 47 +++++++++++++----------- 1 file changed, 25 insertions(+), 22 deletions(-)
drivers/net/ethernet/intel/ice/ice_lib.c +25 -22
@@ -519,6 +519,30 @@ static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)return IRQ_HANDLED ; } +static struct ice_vsi_stats *ice_vsi_new_stat_arrays (int txq,int rxq) +{ +struct ice_ring_stats **tx_ring_stats; +struct ice_ring_stats **rx_ring_stats; +struct ice_vsi_stats *vsi_stat; + + vsi_stat =kzalloc_obj (*vsi_stat); + tx_ring_stats =kzalloc_objs (*tx_ring_stats, txq); + rx_ring_stats =kzalloc_objs (*rx_ring_stats, rxq); +if (!vsi_stat || !tx_ring_stats || !rx_ring_stats) { +kfree (vsi_stat); +kfree (tx_ring_stats); +kfree (rx_ring_stats); +return NULL ; + } + + vsi_stat->tx_ring_stats = tx_ring_stats; + vsi_stat->rx_ring_stats = rx_ring_stats; + vsi_stat->tx_ring_stats_len = txq; + vsi_stat->rx_ring_stats_len = rxq; + +return vsi_stat; +} +/** * ice_vsi_alloc_stat_arrays - Allocate statistics arrays * @vsi: VSI pointer
@@ -537,33 +561,12 @@ static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)/* realloc will happen in rebuild path */ return 0 ; - vsi_stat =kzalloc_obj (*vsi_stat); + vsi_stat =ice_vsi_new_stat_arrays (vsi->alloc_txq, vsi->alloc_rxq);if (!vsi_stat)return -ENOMEM ; - vsi_stat->tx_ring_stats = -kzalloc_objs (*vsi_stat->tx_ring_stats, vsi->alloc_txq); -if (!vsi_stat->tx_ring_stats) - goto err_alloc_tx; - vsi_stat->tx_ring_stats_len = vsi->alloc_txq; - - vsi_stat->rx_ring_stats = -kzalloc_objs (*vsi_stat->rx_ring_stats, vsi->alloc_rxq); -if (!vsi_stat->rx_ring_stats) - goto err_alloc_rx; - vsi_stat->rx_ring_stats_len = vsi->alloc_rxq; - pf->vsi_stats[vsi->idx] = vsi_stat; -return 0 ; - -err_alloc_rx: -kfree (vsi_stat->rx_ring_stats); -err_alloc_tx: -kfree (vsi_stat->tx_ring_stats); -kfree (vsi_stat); - pf->vsi_stats[vsi->idx] =NULL ; -return -ENOMEM ; }/**
Signature · 1 line
2.51.1
Przemek Kitszel
maintainerPATCH v3 3/5 ice: extract ice_vsi_get_num_qs()
In reply to Przemek Kitszelice_vsi_set_num_qs() mixed two things: deciding how many queues a VSI gets, and applying all the side effects of that decision. Split the decision out into ice_vsi_get_num_qs(), returning both counts at once.
To make that possible, group alloc_txq and alloc_rxq in struct ice_vsi with struct_group_tagged(), which gives the return type without adding any new storage. Field order changes slightly, num_txq now follows alloc_rxq, but nothing depends on it.
ice_vsi_get_num_qs() takes @held_txq and @held_rxq from the start, even though the only caller so far passes zero for both. They let a caller ask what the counts would be once the queues the VSI currently owns are back in the PF pool. A later commit adds the caller that needs this: the rebuild path has to size its ring stats arrays before ice_vsi_decfg() releases the queues, yet the arrays must match what ice_vsi_set_num_qs() computes afterwards.
No functional change intended.
--- drivers/net/ethernet/intel/ice/ice.h | 6 +- drivers/net/ethernet/intel/ice/ice_lib.c | 86 ++++++++++++++---------- 2 files changed, 55 insertions(+), 37 deletions(-)
drivers/net/ethernet/intel/ice/ice.h +4 -2
@@ -403,9 +403,11 @@ struct ice_vsi {u8 rx_mapping_mode;/* ICE_MAP_MODE_[CONTIG|SCATTER] */ u16 *txq_map;/* index in pf->avail_txqs */ u16 *rxq_map;/* index in pf->avail_rxqs */ -u16 alloc_txq;/* Allocated Tx queues */ +struct_group_tagged (ice_vsi_alloc_queues_params, alloc_txq_rxq, + u16 alloc_txq; /* Allocated Tx queues */ + u16 alloc_rxq; /* Allocated Rx queues */ + );u16 num_txq;/* Used Tx queues */ -u16 alloc_rxq;/* Allocated Rx queues */ u16 num_rxq;/* Used Rx queues */ u16 req_txq;/* User requested Tx queues */ u16 req_rxq;/* User requested Rx queues */
drivers/net/ethernet/intel/ice/ice_lib.c +51 -35
135 lines not on this page · raw message
Signature · 1 line
2.51.1
Przemek Kitszel
maintainerPATCH v3 4/5 ice: rebuild ring stats arrays instead of reallocating them in place
In reply to Przemek Kitszelice_vsi_realloc_stat_arrays() resized the ring stats arrays in place with krealloc_array(), sizing them from vsi->req_txq/req_rxq. That is not what ice_vsi_set_num_qs() computes later in ice_vsi_cfg_def(), so after a rebuild the arrays could end up shorter than vsi->alloc_txq / vsi->alloc_rxq, and ice_vsi_alloc_ring_stats() then walked past their end. Requesting fewer queues than the PF pool can hand out was enough to trigger it.
Replace it with ice_vsi_resize_stat_arrays(), which allocates a fresh struct ice_vsi_stats instead, sized with ice_vsi_get_num_qs() and the queues ice_vsi_decfg() is about to return to the PF pool, which is exactly what ice_vsi_set_num_qs() will compute once they are back there. Copy the surviving entry pointers over and install the new structure, all before ice_vsi_decfg() runs. The entries that did not fit are freed together with the old container, via __ice_vsi_free_stats() with @free_entries set to false.
Doing the allocation up front also means the failure path is a plain unlock and return, with the VSI still fully configured, rather than a half-torn-down VSI to unwind.
While at it, skip the stats handling for ICE_VSI_CHNL, which has no entry in pf->vsi_stats[] to begin with.
---
this is an actual fix/improvement, that removes some of the tech debt, but there is no Fixes tag, as this was not directly caused by an user observable bug, rather as a pre-work / ai-review resolution for the next commit
--- drivers/net/ethernet/intel/ice/ice_lib.c | 118 +++++++++++++---------- 1 file changed, 69 insertions(+), 49 deletions(-)
drivers/net/ethernet/intel/ice/ice_lib.c +69 -49
@@ -2953,6 +2953,51 @@ ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,return vsi->num_q_vectors; } +static void ice_vsi_free_unused_stat_arrays (struct ice_vsi_stats *vsi_stat, +struct ice_vsi_stats *new_vsi_stat) +{ +int new_txq = new_vsi_stat->tx_ring_stats_len; +int new_rxq = new_vsi_stat->rx_ring_stats_len; +int prev_txq = vsi_stat->tx_ring_stats_len; +int prev_rxq = vsi_stat->rx_ring_stats_len; + +for (int i = new_txq; i < prev_txq; i++) { +if (vsi_stat->tx_ring_stats[i]) { +kfree_rcu (vsi_stat->tx_ring_stats[i], rcu); +WRITE_ONCE (vsi_stat->tx_ring_stats[i],NULL ); + } + } +for (int i = new_rxq; i < prev_rxq; i++) { +if (vsi_stat->rx_ring_stats[i]) { +kfree_rcu (vsi_stat->rx_ring_stats[i], rcu); +WRITE_ONCE (vsi_stat->rx_ring_stats[i],NULL ); + } + } +} + +static void ice_vsi_set_stat_arrays (struct ice_vsi *vsi, +struct ice_vsi_stats *new_vsi_stat) +{ +u16 new_txq, new_rxq, prev_txq, prev_rxq; +struct ice_vsi_stats *vsi_stat; +struct ice_pf *pf = vsi->back; + + new_txq = new_vsi_stat->tx_ring_stats_len; + new_rxq = new_vsi_stat->rx_ring_stats_len; + vsi_stat = pf->vsi_stats[vsi->idx]; + pf->vsi_stats[vsi->idx] = new_vsi_stat; +if (!vsi_stat) +return ;/* don't copy if there is no source */ + + prev_txq = vsi_stat->tx_ring_stats_len; + prev_rxq = vsi_stat->rx_ring_stats_len; + +memcpy (new_vsi_stat->tx_ring_stats, vsi_stat->tx_ring_stats, +sizeof (*vsi_stat->tx_ring_stats) *min (prev_txq, new_txq)); +memcpy (new_vsi_stat->rx_ring_stats, vsi_stat->rx_ring_stats, +sizeof (*vsi_stat->rx_ring_stats) *min (prev_rxq, new_rxq)); +} +/** * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays * @vsi: VSI connected with q_vectors
@@ -3039,63 +3084,38 @@ ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi, }/** -* ice_vsi_realloc_stat_arrays - Frees unused stat structures or alloc new ones -* @vsi: VSI pointer +* ice_vsi_resize_stat_arrays - resize ring stats arrays for new queue count +* @vsi: VSI to swap the ring stats arrays of +* +* Call while @vsi still owns its queues and before ice_vsi_decfg() returns them +* to the PF pool, so that the new size is what ice_vsi_set_num_qs() will compute +* afterwards. Surviving entries are carried over, the rest is freed. +* +* Returns 0 on success and negative value on failure */ -static int -ice_vsi_realloc_stat_arrays (struct ice_vsi *vsi) +static int ice_vsi_resize_stat_arrays (struct ice_vsi *vsi) { -u16 req_txq = vsi->req_txq ? vsi->req_txq : vsi->alloc_txq; -u16 req_rxq = vsi->req_rxq ? vsi->req_rxq : vsi->alloc_rxq; -struct ice_ring_stats **tx_ring_stats; -struct ice_ring_stats **rx_ring_stats; -struct ice_vsi_stats *vsi_stat; +struct ice_vsi_alloc_queues_params qs; +struct ice_vsi_stats *old_stat; +struct ice_vsi_stats *new_stat;struct ice_pf *pf = vsi->back; -u16 prev_txq = vsi->alloc_txq; -u16 prev_rxq = vsi->alloc_rxq; -int i; - vsi_stat = pf->vsi_stats[vsi->idx]; +if (vsi->type ==ICE_VSI_CHNL ) +return 0 ; -if (req_txq < prev_txq) { -for (i = req_txq; i < prev_txq; i++) { -if (vsi_stat->tx_ring_stats[i]) { -kfree_rcu (vsi_stat->tx_ring_stats[i], rcu); -WRITE_ONCE (vsi_stat->tx_ring_stats[i],NULL ); - } - } - } + qs =ice_vsi_get_num_qs (vsi, vsi->alloc_txq + vsi->num_xdp_txq, + vsi->alloc_rxq); - tx_ring_stats = vsi_stat->tx_ring_stats; - vsi_stat->tx_ring_stats = -krealloc_array (vsi_stat->tx_ring_stats, req_txq, -sizeof (*vsi_stat->tx_ring_stats), -GFP_KERNEL | __GFP_ZERO); -if (!vsi_stat->tx_ring_stats) { - vsi_stat->tx_ring_stats = tx_ring_stats; + new_stat =ice_vsi_new_stat_arrays (qs.alloc_txq, qs.alloc_rxq); +if (!new_stat)return -ENOMEM ; - } - vsi_stat->tx_ring_stats_len = req_txq; -if (req_rxq < prev_rxq) { -for (i = req_rxq; i < prev_rxq; i++) { -if (vsi_stat->rx_ring_stats[i]) { -kfree_rcu (vsi_stat->rx_ring_stats[i], rcu); -WRITE_ONCE (vsi_stat->rx_ring_stats[i],NULL ); - } - } - } - - rx_ring_stats = vsi_stat->rx_ring_stats; - vsi_stat->rx_ring_stats = -krealloc_array (vsi_stat->rx_ring_stats, req_rxq, -sizeof (*vsi_stat->rx_ring_stats), -GFP_KERNEL | __GFP_ZERO); -if (!vsi_stat->rx_ring_stats) { - vsi_stat->rx_ring_stats = rx_ring_stats; -return -ENOMEM ; + old_stat = pf->vsi_stats[vsi->idx]; +ice_vsi_set_stat_arrays (vsi, new_stat); +if (old_stat) { +ice_vsi_free_unused_stat_arrays (old_stat, new_stat); +__ice_vsi_free_stats (old_stat, false); } - vsi_stat->rx_ring_stats_len = req_rxq;return 0 ; }
@@ -3127,7 +3147,7 @@ int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)mutex_lock (&vsi->xdp_state_lock); - ret =ice_vsi_realloc_stat_arrays (vsi); + ret =ice_vsi_resize_stat_arrays (vsi);if (ret) goto unlock;
Signature · 1 line
2.51.1
Przemek Kitszel
maintainerPATCH v3 5/5 ice: fix stats array overflow when VF requests more queues
In reply to Przemek KitszelFrom: Michal Schmidt <[email protected]>
When a VF increases its queue count via VIRTCHNL_OP_REQUEST_QUEUES, ice_vc_request_qs_msg() sets vf->num_req_qs and triggers a VF reset. The reset calls ice_vf_reconfig_vsi(), which does ice_vsi_decfg() followed by ice_vsi_cfg(). ice_vsi_decfg() does not free the per-ring stats arrays. Inside ice_vsi_cfg_def(), ice_vsi_set_num_qs() updates alloc_txq/alloc_rxq to the new larger value, but ice_vsi_alloc_stat_arrays() returns early because the stats already exist. ice_vsi_alloc_ring_stats() then iterates using the new larger alloc_txq and writes beyond the bounds of the old, smaller tx_ring_stats/rx_ring_stats pointer arrays, corrupting adjacent SLUB metadata.
KASAN detects the bug:
==================================================================
BUG: KASAN: slab-out-of-bounds in ice_vsi_alloc_ring_stats+0x385/0x4a0 [ice]
Read of size 8 at addr ffff88810affea60 by task kworker/u131:7/221
CPU: 24 UID: 0 PID: 221 Comm: kworker/u131:7 Not tainted 7.1.0-rc1+ #1 PREEMPT(lazy)
...
Workqueue: ice ice_service_task [ice]
Call Trace:
<TASK>
...
kasan_report+0xd7/0x120
ice_vsi_alloc_ring_stats+0x385/0x4a0 [ice]
ice_vsi_cfg_def+0x12e2/0x2060 [ice]
ice_vsi_cfg+0xb5/0x3c0 [ice]
ice_reset_vf+0x858/0xf80 [ice]
ice_vc_request_qs_msg+0x1da/0x290 [ice]
ice_vc_process_vf_msg+0xb15/0x1430 [ice]
__ice_clean_ctrlq+0x70d/0x9d0 [ice]
ice_service_task+0x840/0xf20 [ice]
process_one_work+0x690/0xff0
worker_thread+0x4d9/0xd20
kthread+0x322/0x410
ret_from_fork+0x332/0x660
ret_from_fork_asm+0x1a/0x30
</TASK>
Allocated by task 2439:
kasan_save_stack+0x1c/0x40
kasan_save_track+0x10/0x30
__kasan_kmalloc+0x96/0xb0
__kmalloc_noprof+0x1d8/0x580
ice_vsi_cfg_def+0x115c/0x2060 [ice]
ice_vsi_cfg+0xb5/0x3c0 [ice]
ice_vsi_setup+0x180/0x320 [ice]
ice_start_vfs+0x1f3/0x590 [ice]
ice_ena_vfs+0x66d/0x798 [ice]
ice_sriov_configure.cold+0xe4/0x121 [ice]
sriov_numvfs_store+0x279/0x480
kernfs_fop_write_iter+0x331/0x4f0
vfs_write+0x4c4/0xe40
ksys_write+0x10c/0x240
do_syscall_64+0xd9/0x650
entry_SYSCALL_64_after_hwframe+0x76/0x7e
The buggy address belongs to the object at ffff88810affea40
which belongs to the cache kmalloc-32 of size 32
The buggy address is located 0 bytes to the right of
allocated 32-byte region [ffff88810affea40, ffff88810affea60)
...
==================================================================ice_vsi_rebuild() handles this correctly by calling ice_vsi_resize_stat_arrays() before reconfiguration, but ice_vf_reconfig_vsi() was missing this call.
Fix by calling ice_vsi_resize_stat_arrays() in ice_vf_reconfig_vsi() before ice_vsi_decfg(), mirroring the ice_vsi_rebuild() pattern. The helper sizes the new arrays with ice_vsi_get_num_qs(), which for a VF reads vf->num_req_qs directly, so there is nothing else to set up for it.
See the linked RHEL Jira item for a reproducer.
---
v3: rebase on top of all the pre-work commits, now there should be neither
mismatch between the alloc-time and use-time sizes nor regression on failed memory allocation
--- drivers/net/ethernet/intel/ice/ice_lib.h | 1 + drivers/net/ethernet/intel/ice/ice_lib.c | 2 +- drivers/net/ethernet/intel/ice/ice_vf_lib.c | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-)
drivers/net/ethernet/intel/ice/ice_lib.h +1 -0
@@ -67,6 +67,7 @@ void ice_vsi_decfg(struct ice_vsi *vsi);void ice_dis_vsi (struct ice_vsi *vsi,bool locked);int ice_vsi_rebuild (struct ice_vsi *vsi,u32 vsi_flags); +int ice_vsi_resize_stat_arrays (struct ice_vsi *vsi);int ice_vsi_cfg (struct ice_vsi *vsi);struct ice_vsi *ice_vsi_alloc (struct ice_pf *pf);void ice_vsi_free (struct ice_vsi *vsi);
drivers/net/ethernet/intel/ice/ice_lib.c +1 -1
@@ -3093,7 +3093,7 @@ ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,* * Returns 0 on success and negative value on failure */ -static int ice_vsi_resize_stat_arrays (struct ice_vsi *vsi) +int ice_vsi_resize_stat_arrays (struct ice_vsi *vsi) {struct ice_vsi_alloc_queues_params qs;struct ice_vsi_stats *old_stat;
drivers/net/ethernet/intel/ice/ice_vf_lib.c +4 -0
@@ -268,6 +268,10 @@ static int ice_vf_reconfig_vsi(struct ice_vf *vf) vsi->flags =ICE_VSI_FLAG_NO_INIT ; + err =ice_vsi_resize_stat_arrays (vsi); +if (err) +return err; +ice_vsi_decfg (vsi);ice_fltr_remove_all (vsi);
Signature · 1 line
2.51.1
Jakub Kicinski
maintainerQuoted 1 line · On Fri, 11 Sep 2026 13:26:15 +0200 Przemek Kitszel wrote:
Fix OOB access to the stats arrays.
I'm going to assume you mean this for iwl-net, there are already 15 patches from Tony in the queue for net.
Jakub Kicinski
maintainerQuoted 5 lines · On Fri, 11 Sep 2026 09:52:37 -0700 Jakub Kicinski wrote:
Quoted 1 line · On Fri, 11 Sep 2026 13:26:15 +0200 Przemek Kitszel wrote:
Fix OOB access to the stats arrays.
I'm going to assume you mean this for iwl-net, there are already 15 patches from Tony in the queue for net.
FWIW at a glance LGTM code-wise, there are some kdoc warnings tho