idpf: fix potential memory leak on kcalloc() failure
authorMichal Swiatkowski <michal.swiatkowski@linux.intel.com>
Fri, 4 Apr 2025 10:54:21 +0000 (12:54 +0200)
committerTony Nguyen <anthony.l.nguyen@intel.com>
Tue, 29 Apr 2025 21:28:24 +0000 (14:28 -0700)
In case of failing on rss_data->rss_key allocation the function is
freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes: d4d558718266 ("idpf: initialize interrupts and enable vport")
Reviewed-by: Pavan Kumar Linga <pavan.kumar.linga@intel.com>
Reviewed-by: Aleksandr Loktionov <aleksandr.loktionov@intel.com>
Suggested-by: Pavan Kumar Linga <pavan.kumar.linga@intel.com>
Signed-off-by: Michal Swiatkowski <michal.swiatkowski@linux.intel.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Tested-by: Samuel Salin <Samuel.salin@intel.com>
Signed-off-by: Tony Nguyen <anthony.l.nguyen@intel.com>
drivers/net/ethernet/intel/idpf/idpf_lib.c

index 730a9c7a59f2b0b2ab03e2a2db0e3bd234f68b47..82f09b4030bce23bb39a49e29bcfe9eb98a10b26 100644 (file)
@@ -1113,11 +1113,9 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
 
        num_max_q = max(max_q->max_txq, max_q->max_rxq);
        vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL);
-       if (!vport->q_vector_idxs) {
-               kfree(vport);
+       if (!vport->q_vector_idxs)
+               goto free_vport;
 
-               return NULL;
-       }
        idpf_vport_init(vport, max_q);
 
        /* This alloc is done separate from the LUT because it's not strictly
@@ -1127,11 +1125,9 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
         */
        rss_data = &adapter->vport_config[idx]->user_config.rss_data;
        rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL);
-       if (!rss_data->rss_key) {
-               kfree(vport);
+       if (!rss_data->rss_key)
+               goto free_vector_idxs;
 
-               return NULL;
-       }
        /* Initialize default rss key */
        netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size);
 
@@ -1144,6 +1140,13 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
        adapter->next_vport = idpf_get_free_slot(adapter);
 
        return vport;
+
+free_vector_idxs:
+       kfree(vport->q_vector_idxs);
+free_vport:
+       kfree(vport);
+
+       return NULL;
 }
 
 /**