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 56B20A00C3; Fri, 4 Feb 2022 08:49:42 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2EC8440143; Fri, 4 Feb 2022 08:49:42 +0100 (CET) Received: from out162-62-57-210.mail.qq.com (out162-62-57-210.mail.qq.com [162.62.57.210]) by mails.dpdk.org (Postfix) with ESMTP id A2A2B40140 for ; Fri, 4 Feb 2022 08:49:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1643960978; bh=JdmOZ3NJueUGvbOUVfnA0TJDvTa10+oKscHvNIqfm44=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Q8IHWDLguV10MPdW8R31q6F0bmKaNtPu5ZblqIHvJftO6oeRFqbfhwPHImslMPNyd m5zG5mPK/nH3X1D1q4pri8e3M52r5HmFVdJbzP3LcOPLLZt/vks55z/GrPXUpbmKpp Q7lovHkCnDQ1yApT4buVhmFSsQYldVO6v9qNoSLU= Received: from liwg-ubuntu.lan ([111.193.130.237]) by newxmesmtplogicsvrszc11.qq.com (NewEsmtp) with SMTP id C6285A06; Fri, 04 Feb 2022 15:49:34 +0800 X-QQ-mid: xmsmtpt1643960974tfe4qqinq Message-ID: X-QQ-XMAILINFO: M5KKvM8FjyDpJFSTKssTu0kjGiqnQlF1Od43d0GQlbdLnvLsBFEp3F7Err55m0 fbgg9n/qgHLgLfrBPK/zIRk283M+vKmwQ9Q4eISx0Wq+na+QtVhxcwyzO48dg7HpQgxnAx7yBxRp pijPbVl9kwL8S1pKPvPdeT39E8C34tTxvaUalpUAT8f0TDCdxwZOq57g605mTgSIZYOgNkJF2r8g 7i3sNL0E9X+86ldF/JPKPRyXeCYR8+HtueApF7nR1dCql7P+X8ecpxJApDNorqkKwVGwa7X9qUsK 3slqiVuwL49j//H2HMqWSOcgmxxsS6rHvOAuLUE2TOC+e2rvDQceieZUOT5a1iFK6J1K/oX0Ge5/ gZe67Nbv5KVklrSsq18oRfLJP7Pfey662jQS4HDZkhbnFu1FtyO4h7ZFecJV1/Tk9thdW3TomISQ jaPNxe9rgYzdALdZtIHdLlim3+nDB/O0NHOy3f56672AAFELfORj+9MKkStX0hsIwMhYDdi5O6dB QvnJ1YoR6/T4DEYErBD5MznfXgmabSZpURxOz6xQjJWeN9QJ7sTNDqHtebYmMMpkEflYNDTdpk5Y m+E/KyeSivk8pF+2Ti3EQTzxqLedDJfTGvwudJS5G0ad5yJsvvgMnjMMZR6qy+ohvCJQNTmde8gw S7FnGHJnWYpdQf7F+Uoh/goQ96/m0k9fGbAbPOba61EqNOTwTBkZ1m97gkLUUx79H1kXbsiExZdT ftmKL7C6PBN6CUDm4WB4H8SDABOqiGXvW1RdNwnITRGn7n1fZjRdS6KPCTMtmELxFhmOPcorfa7Y cR9YWhp5ySAvB0//kODio7SWtzP7vBH+3fU5hvIbZk0tcA0/uavNLq 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 v5] eventdev/eth_rx: fix memory leak when token parsing finished Date: Fri, 4 Feb 2022 15:49:34 +0800 X-OQ-MSGID: <20220204074934.2169577-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 Acked-by: Ganapati Kundapura --- v5: * fix according to Jay's comment. * fix anothor bypassing the freeing of memory problem v4: * fix compilation issue 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 | 105 ++++++++++++++++++------ 1 file changed, 78 insertions(+), 27 deletions(-) diff --git a/lib/eventdev/rte_event_eth_rx_adapter.c b/lib/eventdev/rte_event_eth_rx_adapter.c index ae1e260c08..719ef520cd 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,38 +3356,43 @@ 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); - return -EINVAL; + ret = -EINVAL; + goto error; } token = strtok(NULL, "\0"); 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)) { RTE_EDEV_LOG_ERR("Failed to get Rx adapter queue config"); - return -1; + ret = -1; + goto error; } rte_tel_data_start_dict(d); @@ -3378,6 +3407,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 +3420,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,38 +3429,43 @@ 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); - return -EINVAL; + ret = -EINVAL; + goto error; } token = strtok(NULL, "\0"); 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)) { RTE_EDEV_LOG_ERR("Failed to get Rx adapter queue stats"); - return -1; + ret = -1; + goto error; } rte_tel_data_start_dict(d); @@ -3441,6 +3479,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 +3492,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,42 +3500,51 @@ 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); - return -EINVAL; + ret = -EINVAL; + goto error; } token = strtok(NULL, "\0"); 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, rx_queue_id)) { RTE_EDEV_LOG_ERR("Failed to reset Rx adapter queue stats"); - return -1; + ret = -1; + goto error; } return 0; + +error: + free(l_params); + return ret; } RTE_INIT(rxa_init_telemetry) -- 2.25.1