From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1AFFEA00C2; Thu, 3 Feb 2022 08:19:02 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A62E34014F; Thu, 3 Feb 2022 08:19:01 +0100 (CET) Received: from out162-62-58-211.mail.qq.com (out162-62-58-211.mail.qq.com [162.62.58.211]) by mails.dpdk.org (Postfix) with ESMTP id 96CC440143 for ; Thu, 3 Feb 2022 08:19:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1643872737; bh=KYwKLLt+9IIbw5NLMmb1GlDMf1RwiwN5/iBM8VHYb5g=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WivmM6TD8gIM+r3sLXurSbjLASwC9AoriruxPp291ye5hD7ci+9B/DZ7Vu6dI7Hrb /G073ue/YG0SJLgT6Je02ytnUmdLn1pL7RRg4Pntibdnq48gB8uqsnU7DYp7F73fUb 6VTAbx4S6pNZsZJVVM9EAlTU1LGzM6dqlWZiy8Vk= Received: from liwg-ubuntu.lan ([111.193.130.237]) by newxmesmtplogicsvrsza5.qq.com (NewEsmtp) with SMTP id 4AA20043; Thu, 03 Feb 2022 15:18:42 +0800 X-QQ-mid: xmsmtpt1643872722tvdaxvy2l Message-ID: X-QQ-XMAILINFO: MEQfkpQBGjSNiSvR8Evyg3OkkJJxZcVPk3TGpk+BLI51G0ZgNkwN5WxCnCWBHo Am9CU0QP7K7DQMMFmUun4MYGMM0Z/8XqSRa6RpR/E+bh+P0zSS60aSlR8LEak3LzIGHJC1Gz6vUs 9LezvNc0XPGneF7PzNIAtOOIlvSmZyKMSOUBGQHa3XIpUQOqST0GQBfskHOi+XovwMNYcXYhxd7S 65dutYE6NiFhjdRxBiZpexcHzfOeuDeHEUChl4ecPaMmXpcX/cETTVPAE70T9VI4ZfE4+NZ9dn4I JbUmrguU7Vvs+Oc7zWH2AvJPS714UsHFZ2VJxCQUmBo9XDBeP1kRcT8CZ1BvrPUkyF7YKwEXIOwR DK2Djs/dnsb+ooPcokWdhibziLZ7aSXHj2BS76uVhTiaRdNtu4d7E61GgJNLOECr2Nt2in0MU3VR Zi+WeSZxKxLdTiIzLp9qkH8Gj/lINIopyyv/EgDsOnjR49yvtIKJTPkVegwLolnWqTzFX72roJK2 rdIZ3dHonSbTALh7oWj4H2SG+G0hJiFKqGhb3snXbYkJx1CoGJEYxFPCu5gHZvK0lfVJKoY/jh+k 9nz7H9Ep8G4YtQ4A8ahxsG1GUsNxHUGDT+mdrzPYC2OUyQiRkYRHwGUvCxJq1qfFS4AOB2fbYoEG ixNr4LDO51nLYRNsTiC1X9AChns8kGaFdAmBZUm5J3/gZIP0xqTAwK3k+UjGs4n6d603+BkMqJGy 5t4MbvOGPt5+Q2sz3+luiUWrGnZJoU5Qq9UdOSbGVM0cq14k3M81377AnL32WvxNKThsGW76+src 4v21O2COtJkS1CqjMhGQf3DvbQDnzdd7dTsKKoXPppwNbK20j4JH04EgtE/V7727EvYUq5Fg//f0 g2qUjCzJrc From: Weiguo Li To: jay.jayatheerthan@intel.com Cc: ganapati.kundapura@intel.com, s.v.naga.harish.k@intel.com, stephen@networkplumber.org, dev@dpdk.org Subject: [PATCH v3] eventdev/eth_rx: fix memory leak when token parsing finished Date: Thu, 3 Feb 2022 15:18:42 +0800 X-OQ-MSGID: <20220203071842.1861072-1-liwg06@foxmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The memory get from strdup should be freed when parameter parsing finished, and also should be freed when error occurs. Fixes: 814d01709328 ("eventdev/eth_rx: support telemetry") Fixes: 9e583185318f ("eventdev/eth_rx: support telemetry") Signed-off-by: Weiguo Li --- v3: * validate "eth_dev_id" by rte_eth_dev_is_valid_port() as Ganapati's comment. * validate "token" by macros to reduce code redundancy. * use new macros to avoid bypassing the freeing of memory problem. v2: * add memory check after strdup --- lib/eventdev/rte_event_eth_rx_adapter.c | 90 +++++++++++++++++++------ 1 file changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/eventdev/rte_event_eth_rx_adapter.c b/lib/eventdev/rte_event_eth_rx_adapter.c index ae1e260c08..886256c28c 100644 --- a/lib/eventdev/rte_event_eth_rx_adapter.c +++ b/lib/eventdev/rte_event_eth_rx_adapter.c @@ -293,6 +293,30 @@ rxa_event_buf_get(struct event_eth_rx_adapter *rx_adapter, uint16_t eth_dev_id, } \ } while (0) +#define RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(id, retval) do { \ + if (!rxa_validate_id(id)) { \ + RTE_EDEV_LOG_ERR("Invalid eth Rx adapter id = %d\n", id); \ + ret = retval; \ + goto error; \ + } \ +} while (0) + +#define RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, retval) do { \ + if (token == NULL || strlen(token) == 0 || !isdigit(*token)) \ + RTE_EDEV_LOG_ERR("Invalid eth Rx adapter token\n"); \ + ret = retval; \ + goto error; \ + } \ +} while (0) + +#define RTE_ETH_VALID_PORTID_OR_GOTO_ERR_RET(port_id, retval) do { \ + if (!rte_eth_dev_is_valid_port(port_id)) { \ + RTE_ETHDEV_LOG(ERR, "Invalid port_id=%u\n", port_id); \ + ret = retval; \ + goto error; \ + } \ +} while (0) + static inline int rxa_sw_adapter_queue_count(struct event_eth_rx_adapter *rx_adapter) { @@ -3323,7 +3347,7 @@ handle_rxa_get_queue_conf(const char *cmd __rte_unused, { uint8_t rx_adapter_id; uint16_t rx_queue_id; - int eth_dev_id; + int eth_dev_id, ret = -1; char *token, *l_params; struct rte_event_eth_rx_adapter_queue_conf queue_conf; @@ -3332,26 +3356,28 @@ handle_rxa_get_queue_conf(const char *cmd __rte_unused, /* Get Rx adapter ID from parameter string */ l_params = strdup(params); + if (l_params == NULL) + return -ENOMEM; token = strtok(l_params, ","); + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); rx_adapter_id = strtoul(token, NULL, 10); - RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_ERR_RET(rx_adapter_id, -EINVAL); + RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(rx_adapter_id, -EINVAL); token = strtok(NULL, ","); - if (token == NULL || strlen(token) == 0 || !isdigit(*token)) - return -1; + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); /* Get device ID from parameter string */ eth_dev_id = strtoul(token, NULL, 10); - RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(eth_dev_id, -EINVAL); + RTE_ETH_VALID_PORTID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL); token = strtok(NULL, ","); - if (token == NULL || strlen(token) == 0 || !isdigit(*token)) - return -1; + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); /* Get Rx queue ID from parameter string */ rx_queue_id = strtoul(token, NULL, 10); if (rx_queue_id >= rte_eth_devices[eth_dev_id].data->nb_rx_queues) { RTE_EDEV_LOG_ERR("Invalid rx queue_id %u", rx_queue_id); + free(l_params); return -EINVAL; } @@ -3359,6 +3385,8 @@ handle_rxa_get_queue_conf(const char *cmd __rte_unused, if (token != NULL) RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev" " telemetry command, ignoring"); + /* Parsing parameter finished */ + free(l_params); if (rte_event_eth_rx_adapter_queue_conf_get(rx_adapter_id, eth_dev_id, rx_queue_id, &queue_conf)) { @@ -3378,6 +3406,10 @@ handle_rxa_get_queue_conf(const char *cmd __rte_unused, RXA_ADD_DICT(queue_conf.ev, flow_id); return 0; + +error: + free(l_params); + return ret; } static int @@ -3387,7 +3419,7 @@ handle_rxa_get_queue_stats(const char *cmd __rte_unused, { uint8_t rx_adapter_id; uint16_t rx_queue_id; - int eth_dev_id; + int eth_dev_id, ret = -1; char *token, *l_params; struct rte_event_eth_rx_adapter_queue_stats q_stats; @@ -3396,26 +3428,28 @@ handle_rxa_get_queue_stats(const char *cmd __rte_unused, /* Get Rx adapter ID from parameter string */ l_params = strdup(params); + if (l_params == NULL) + return -ENOMEM; token = strtok(l_params, ","); + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); rx_adapter_id = strtoul(token, NULL, 10); - RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_ERR_RET(rx_adapter_id, -EINVAL); + RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(rx_adapter_id, -EINVAL); token = strtok(NULL, ","); - if (token == NULL || strlen(token) == 0 || !isdigit(*token)) - return -1; + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); /* Get device ID from parameter string */ eth_dev_id = strtoul(token, NULL, 10); - RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(eth_dev_id, -EINVAL); + RTE_ETH_VALID_PORTID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL); token = strtok(NULL, ","); - if (token == NULL || strlen(token) == 0 || !isdigit(*token)) - return -1; + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); /* Get Rx queue ID from parameter string */ rx_queue_id = strtoul(token, NULL, 10); if (rx_queue_id >= rte_eth_devices[eth_dev_id].data->nb_rx_queues) { RTE_EDEV_LOG_ERR("Invalid rx queue_id %u", rx_queue_id); + free(l_params); return -EINVAL; } @@ -3423,6 +3457,8 @@ handle_rxa_get_queue_stats(const char *cmd __rte_unused, if (token != NULL) RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev" " telemetry command, ignoring"); + /* Parsing parameter finished */ + free(l_params); if (rte_event_eth_rx_adapter_queue_stats_get(rx_adapter_id, eth_dev_id, rx_queue_id, &q_stats)) { @@ -3441,6 +3477,10 @@ handle_rxa_get_queue_stats(const char *cmd __rte_unused, RXA_ADD_DICT(q_stats, rx_dropped); return 0; + +error: + free(l_params); + return ret; } static int @@ -3450,7 +3490,7 @@ handle_rxa_queue_stats_reset(const char *cmd __rte_unused, { uint8_t rx_adapter_id; uint16_t rx_queue_id; - int eth_dev_id; + int eth_dev_id, ret = -1; char *token, *l_params; if (params == NULL || strlen(params) == 0 || !isdigit(*params)) @@ -3458,26 +3498,28 @@ handle_rxa_queue_stats_reset(const char *cmd __rte_unused, /* Get Rx adapter ID from parameter string */ l_params = strdup(params); + if (l_params == NULL) + return -ENOMEM; token = strtok(l_params, ","); + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); rx_adapter_id = strtoul(token, NULL, 10); - RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_ERR_RET(rx_adapter_id, -EINVAL); + RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(rx_adapter_id, -EINVAL); token = strtok(NULL, ","); - if (token == NULL || strlen(token) == 0 || !isdigit(*token)) - return -1; + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); /* Get device ID from parameter string */ eth_dev_id = strtoul(token, NULL, 10); - RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(eth_dev_id, -EINVAL); + RTE_ETH_VALID_PORTID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL); token = strtok(NULL, ","); - if (token == NULL || strlen(token) == 0 || !isdigit(*token)) - return -1; + RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1); /* Get Rx queue ID from parameter string */ rx_queue_id = strtoul(token, NULL, 10); if (rx_queue_id >= rte_eth_devices[eth_dev_id].data->nb_rx_queues) { RTE_EDEV_LOG_ERR("Invalid rx queue_id %u", rx_queue_id); + free(l_params); return -EINVAL; } @@ -3485,6 +3527,8 @@ handle_rxa_queue_stats_reset(const char *cmd __rte_unused, if (token != NULL) RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev" " telemetry command, ignoring"); + /* Parsing parameter finished */ + free(l_params); if (rte_event_eth_rx_adapter_queue_stats_reset(rx_adapter_id, eth_dev_id, @@ -3494,6 +3538,10 @@ handle_rxa_queue_stats_reset(const char *cmd __rte_unused, } return 0; + +error: + free(l_params); + return ret; } RTE_INIT(rxa_init_telemetry) -- 2.25.1