Rather than using malloc and free for the request and response buffers
of IPsec and traffic management virtchnl messages, define them on the
stack using RTE_DEFINE_FLEX. This removes the allocation failure and
cleanup paths, avoiding potential leaks on error.

For the traffic management messages, add checks that the number of TCs
and queue pairs do not exceed the maximum sizes of the buffers.

Signed-off-by: Bruce Richardson <[email protected]>
---
 drivers/net/intel/iavf/iavf_ipsec_crypto.c | 270 ++++++---------------
 drivers/net/intel/iavf/iavf_tm.c           |  26 +-
 2 files changed, 79 insertions(+), 217 deletions(-)

diff --git a/drivers/net/intel/iavf/iavf_ipsec_crypto.c 
b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
index be0a0d0df6..d5a6a48092 100644
--- a/drivers/net/intel/iavf/iavf_ipsec_crypto.c
+++ b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
@@ -460,28 +460,14 @@ static uint32_t
 iavf_ipsec_crypto_security_association_add(struct iavf_adapter *adapter,
        struct rte_security_session_conf *conf)
 {
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       struct virtchnl_ipsec_sa_cfg *sa_cfg;
-       size_t request_len, response_len;
-
-       int rc;
-
-       request_len = sizeof(struct inline_ipsec_msg) +
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_cfg, 1);
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.sa_cfg_resp, 1);
+       const size_t request_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_sa_cfg);
-
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
-       response_len = sizeof(struct inline_ipsec_msg) +
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_sa_cfg_resp);
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       struct virtchnl_ipsec_sa_cfg *sa_cfg;
+       int rc;
 
        /* set msg header params */
        request->ipsec_opcode = INLINE_IPSEC_OP_SA_CREATE;
@@ -546,19 +532,14 @@ iavf_ipsec_crypto_security_association_add(struct 
iavf_adapter *adapter,
                        (uint8_t *)request, request_len,
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response id */
        if (response->ipsec_opcode != request->ipsec_opcode ||
                response->req_id != request->req_id)
-               rc = -EFAULT;
-       else
-               rc = response->ipsec_data.sa_cfg_resp->sa_handle;
-update_cleanup:
-       free(response);
-       free(request);
+               return -EFAULT;
 
-       return rc;
+       return response->ipsec_data.sa_cfg_resp->sa_handle;
 }
 
 static void
@@ -722,17 +703,13 @@ iavf_ipsec_crypto_inbound_security_policy_add(struct 
iavf_adapter *adapter,
        bool is_udp,
        uint16_t udp_port)
 {
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       size_t request_len, response_len;
-       int rc = 0;
-
-       request_len = sizeof(struct inline_ipsec_msg) +
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sp_cfg, 1);
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.sp_cfg_resp, 1);
+       const size_t request_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_sp_cfg);
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
+                       sizeof(struct virtchnl_ipsec_sp_cfg_resp);
+       int rc = 0;
 
        /* set msg header params */
        request->ipsec_opcode = INLINE_IPSEC_OP_SP_CREATE;
@@ -767,58 +744,32 @@ iavf_ipsec_crypto_inbound_security_policy_add(struct 
iavf_adapter *adapter,
        request->ipsec_data.sp_cfg->is_udp = is_udp;
        request->ipsec_data.sp_cfg->udp_port = htons(udp_port);
 
-       response_len = sizeof(struct inline_ipsec_msg) +
-                       sizeof(struct virtchnl_ipsec_sp_cfg_resp);
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
        /* send virtual channel request to add SA to hardware database */
        rc = iavf_ipsec_crypto_request(adapter,
                        (uint8_t *)request, request_len,
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response */
        if (response->ipsec_opcode != request->ipsec_opcode ||
                response->req_id != request->req_id)
-               rc = -EFAULT;
-       else
-               rc = response->ipsec_data.sp_cfg_resp->rule_id;
-
-update_cleanup:
-       free(request);
-       free(response);
+               return -EFAULT;
 
-       return rc;
+       return response->ipsec_data.sp_cfg_resp->rule_id;
 }
 
 static uint32_t
 iavf_ipsec_crypto_sa_update_esn(struct iavf_adapter *adapter,
        struct iavf_security_session *sess)
 {
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       size_t request_len, response_len;
-       int rc = 0;
-
-       request_len = sizeof(struct inline_ipsec_msg) +
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_update, 
1);
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.ipsec_resp, 1);
+       const size_t request_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_sa_update);
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
-       response_len = sizeof(struct inline_ipsec_msg) +
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_resp);
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       int rc = 0;
 
        /* set msg header params */
        request->ipsec_opcode = INLINE_IPSEC_OP_SA_UPDATE;
@@ -834,20 +785,14 @@ iavf_ipsec_crypto_sa_update_esn(struct iavf_adapter 
*adapter,
                        (uint8_t *)request, request_len,
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response */
        if (response->ipsec_opcode != request->ipsec_opcode ||
                response->req_id != request->req_id)
-               rc = -EFAULT;
-       else
-               rc = response->ipsec_data.ipsec_resp->resp;
-
-update_cleanup:
-       free(request);
-       free(response);
+               return -EFAULT;
 
-       return rc;
+       return response->ipsec_data.ipsec_resp->resp;
 }
 
 static int
@@ -897,25 +842,13 @@ int
 iavf_ipsec_crypto_security_policy_delete(struct iavf_adapter *adapter,
        uint8_t is_v4, uint32_t flow_id)
 {
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       size_t request_len, response_len;
-       int rc = 0;
-
-       request_len = sizeof(struct inline_ipsec_msg) +
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, 
ipsec_data.sp_destroy, 1);
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.ipsec_resp, 1);
+       const size_t request_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_sp_destroy);
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
-       response_len = sizeof(struct inline_ipsec_msg) +
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_resp);
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       int rc = 0;
 
        /* set msg header params */
        request->ipsec_opcode = INLINE_IPSEC_OP_SP_DESTROY;
@@ -932,48 +865,27 @@ iavf_ipsec_crypto_security_policy_delete(struct 
iavf_adapter *adapter,
                        (uint8_t *)request, request_len,
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response */
        if (response->ipsec_opcode != request->ipsec_opcode ||
                response->req_id != request->req_id)
-               rc = -EFAULT;
-       else
-               rc = response->ipsec_data.ipsec_status->status;
-
-update_cleanup:
-       free(request);
-       free(response);
+               return -EFAULT;
 
-       return rc;
+       return response->ipsec_data.ipsec_status->status;
 }
 
 static uint32_t
 iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
        struct iavf_security_session *sess)
 {
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       size_t request_len, response_len;
-
-       int rc = 0;
-
-       request_len = sizeof(struct inline_ipsec_msg) +
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, 
ipsec_data.sa_destroy, 1);
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.ipsec_resp, 1);
+       const size_t request_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_sa_destroy);
-
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
-       response_len = sizeof(struct inline_ipsec_msg) +
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_resp);
-
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       int rc = 0;
 
        /* set msg header params */
        request->ipsec_opcode = INLINE_IPSEC_OP_SA_DESTROY;
@@ -995,12 +907,12 @@ iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
                        (uint8_t *)request, request_len,
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response */
        if (response->ipsec_opcode != request->ipsec_opcode ||
                response->req_id != request->req_id)
-               rc = -EFAULT;
+               return -EFAULT;
 
        /**
         * Delete status will be the same bitmask as sa_destroy request flag if
@@ -1008,13 +920,9 @@ iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
         */
        if (request->ipsec_data.sa_destroy->flag !=
                        response->ipsec_data.ipsec_status->status)
-               rc = -EFAULT;
-
-update_cleanup:
-       free(response);
-       free(request);
+               return -EFAULT;
 
-       return rc;
+       return 0;
 }
 
 static int
@@ -1160,50 +1068,30 @@ iavf_ipsec_crypto_device_capabilities_get(struct 
iavf_adapter *adapter,
                struct virtchnl_ipsec_cap *capability)
 {
        /* Perform pf-vf comms */
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       size_t request_len, response_len;
-       int rc;
-
-       request_len = sizeof(struct inline_ipsec_msg);
-
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
-       response_len = sizeof(struct inline_ipsec_msg) +
+       struct inline_ipsec_msg request = {0};
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.ipsec_cap, 1);
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_cap);
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       int rc;
 
        /* set msg header params */
-       request->ipsec_opcode = INLINE_IPSEC_OP_GET_CAP;
-       request->req_id = (uint16_t)0xDEADBEEF;
+       request.ipsec_opcode = INLINE_IPSEC_OP_GET_CAP;
+       request.req_id = (uint16_t)0xDEADBEEF;
 
        /* send virtual channel request to add SA to hardware database */
        rc = iavf_ipsec_crypto_request(adapter,
-                       (uint8_t *)request, request_len,
+                       (uint8_t *)&request, sizeof(request),
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response id */
-       if (response->ipsec_opcode != request->ipsec_opcode ||
-               response->req_id != request->req_id){
-               rc = -EFAULT;
-               goto update_cleanup;
-       }
-       memcpy(capability, response->ipsec_data.ipsec_cap, sizeof(*capability));
-
-update_cleanup:
-       free(response);
-       free(request);
+       if (response->ipsec_opcode != request.ipsec_opcode ||
+               response->req_id != request.req_id)
+               return -EFAULT;
 
-       return rc;
+       memcpy(capability, response->ipsec_data.ipsec_cap, sizeof(*capability));
+       return 0;
 }
 
 enum rte_crypto_auth_algorithm auth_maptbl[] = {
@@ -1588,50 +1476,30 @@ iavf_ipsec_crypto_status_get(struct iavf_adapter 
*adapter,
                struct virtchnl_ipsec_status *status)
 {
        /* Perform pf-vf comms */
-       struct inline_ipsec_msg *request = NULL, *response = NULL;
-       size_t request_len, response_len;
-       int rc;
-
-       request_len = sizeof(struct inline_ipsec_msg);
-
-       request = calloc(1, request_len);
-       if (request == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
-
-       response_len = sizeof(struct inline_ipsec_msg) +
+       struct inline_ipsec_msg request = {0};
+       RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, 
ipsec_data.ipsec_status, 1);
+       const size_t response_len = sizeof(struct inline_ipsec_msg) +
                        sizeof(struct virtchnl_ipsec_status);
-       response = calloc(1, response_len);
-       if (response == NULL) {
-               rc = -ENOMEM;
-               goto update_cleanup;
-       }
+       int rc;
 
        /* set msg header params */
-       request->ipsec_opcode = INLINE_IPSEC_OP_GET_STATUS;
-       request->req_id = (uint16_t)0xDEADBEEF;
+       request.ipsec_opcode = INLINE_IPSEC_OP_GET_STATUS;
+       request.req_id = (uint16_t)0xDEADBEEF;
 
        /* send virtual channel request to add SA to hardware database */
        rc = iavf_ipsec_crypto_request(adapter,
-                       (uint8_t *)request, request_len,
+                       (uint8_t *)&request, sizeof(request),
                        (uint8_t *)response, response_len);
        if (rc)
-               goto update_cleanup;
+               return rc;
 
        /* verify response id */
-       if (response->ipsec_opcode != request->ipsec_opcode ||
-               response->req_id != request->req_id){
-               rc = -EFAULT;
-               goto update_cleanup;
-       }
-       memcpy(status, response->ipsec_data.ipsec_status, sizeof(*status));
-
-update_cleanup:
-       free(response);
-       free(request);
+       if (response->ipsec_opcode != request.ipsec_opcode ||
+               response->req_id != request.req_id)
+               return -EFAULT;
 
-       return rc;
+       memcpy(status, response->ipsec_data.ipsec_status, sizeof(*status));
+       return 0;
 }
 
 
diff --git a/drivers/net/intel/iavf/iavf_tm.c b/drivers/net/intel/iavf/iavf_tm.c
index 07ba58f6f5..c7b904e9d7 100644
--- a/drivers/net/intel/iavf/iavf_tm.c
+++ b/drivers/net/intel/iavf/iavf_tm.c
@@ -833,8 +833,9 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
        struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
        struct iavf_adapter *adapter =
                IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
-       struct virtchnl_queue_tc_mapping *q_tc_mapping = NULL;
-       struct virtchnl_queues_bw_cfg *q_bw = NULL;
+       RTE_DEFINE_FLEX(struct virtchnl_queue_tc_mapping, q_tc_mapping, tc,
+                       IAVF_MAX_TRAFFIC_CLASS - 1);
+       RTE_DEFINE_FLEX(struct virtchnl_queues_bw_cfg, q_bw, cfg, 
IAVF_MAX_NUM_QUEUES_LV - 1);
        struct iavf_tm_node_list *queue_list = &vf->tm_conf.queue_list;
        struct iavf_tm_node *tm_node;
        struct iavf_qtc_map *qtc_map = NULL;
@@ -869,21 +870,18 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
                goto fail_clear;
        }
 
-       size = sizeof(*q_tc_mapping) + sizeof(q_tc_mapping->tc[0]) *
-               (vf->qos_cap->num_elem - 1);
-       q_tc_mapping = calloc(1, size);
-       if (!q_tc_mapping) {
-               ret_val = IAVF_ERR_NO_MEMORY;
+       if (vf->qos_cap->num_elem > IAVF_MAX_TRAFFIC_CLASS ||
+                       vf->num_queue_pairs > IAVF_MAX_NUM_QUEUES_LV) {
+               PMD_DRV_LOG(ERR, "Too many TCs (%u) or queues (%u) for TM 
configuration",
+                       vf->qos_cap->num_elem, vf->num_queue_pairs);
+               ret_val = IAVF_ERR_PARAM;
                goto fail_clear;
        }
 
+       size = sizeof(*q_tc_mapping) + sizeof(q_tc_mapping->tc[0]) *
+               (vf->qos_cap->num_elem - 1);
        size_q = sizeof(*q_bw) + sizeof(q_bw->cfg[0]) *
                (vf->num_queue_pairs - 1);
-       q_bw = calloc(1, size_q);
-       if (!q_bw) {
-               ret_val = IAVF_ERR_NO_MEMORY;
-               goto fail_clear;
-       }
 
        q_tc_mapping->vsi_id = vf->vsi.vsi_id;
        q_tc_mapping->num_tc = vf->qos_cap->num_elem;
@@ -950,8 +948,6 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
        free(old_qtc_map);
        if (adapter->stopped == 1)
                vf->tm_conf.committed = true;
-       free(q_bw);
-       free(q_tc_mapping);
        return ret_val;
 
 fail_clear:
@@ -961,8 +957,6 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
                iavf_tm_conf_init(dev);
        }
 err:
-       free(q_bw);
-       free(q_tc_mapping);
        free(qtc_map);
        return ret_val;
 }
-- 
2.55.0

Reply via email to