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 52D2345C76; Mon, 4 Nov 2024 12:15:15 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1BC4B427D1; Mon, 4 Nov 2024 12:11:33 +0100 (CET) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by mails.dpdk.org (Postfix) with ESMTP id 0F9CE40E37 for ; Mon, 4 Nov 2024 12:10:45 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Xhpcx2k7yz2Fbsj; Mon, 4 Nov 2024 19:09:05 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id E3EC11400D2; Mon, 4 Nov 2024 19:10:43 +0800 (CST) Received: from localhost.huawei.com (10.50.165.33) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Mon, 4 Nov 2024 19:10:43 +0800 From: Dengdui Huang To: CC: , , , , , , , , , , Subject: [PATCH v5 32/52] net/failsafe: replace strerror with reentrant version Date: Mon, 4 Nov 2024 19:10:17 +0800 Message-ID: <20241104111037.3632161-33-huangdengdui@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241104111037.3632161-1-huangdengdui@huawei.com> References: <20231114082539.1858594-1-huangdengdui@huawei.com> <20241104111037.3632161-1-huangdengdui@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500011.china.huawei.com (7.185.36.84) 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 function strerror() is insecure in a multi-thread environment. This patch uses strerror_r() to replace it. Signed-off-by: Dengdui Huang Acked-by: Chengwen Feng Acked-by: Morten Brørup Acked-by: Huisong Li --- drivers/net/failsafe/failsafe.c | 13 ++++++++++--- drivers/net/failsafe/failsafe_args.c | 12 +++++++++--- drivers/net/failsafe/failsafe_eal.c | 5 ++++- drivers/net/failsafe/failsafe_ops.c | 5 ++++- drivers/net/failsafe/failsafe_private.h | 17 +++++++++++++---- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/drivers/net/failsafe/failsafe.c b/drivers/net/failsafe/failsafe.c index 32811403b4..345dd13eff 100644 --- a/drivers/net/failsafe/failsafe.c +++ b/drivers/net/failsafe/failsafe.c @@ -135,21 +135,28 @@ fs_mutex_init(struct fs_priv *priv) { int ret; pthread_mutexattr_t attr; + char errmsg[RTE_STRERR_BUFSIZE]; ret = pthread_mutexattr_init(&attr); if (ret) { - ERROR("Cannot initiate mutex attributes - %s", strerror(ret)); + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); + ERROR("Cannot initiate mutex attributes - %s", errmsg); return ret; } /* Allow mutex relocks for the thread holding the mutex. */ ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); if (ret) { - ERROR("Cannot set mutex type - %s", strerror(ret)); + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); + ERROR("Cannot set mutex type - %s", errmsg); return ret; } ret = pthread_mutex_init(&priv->hotplug_mutex, &attr); if (ret) { - ERROR("Cannot initiate mutex - %s", strerror(ret)); + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); + ERROR("Cannot initiate mutex - %s", errmsg); return ret; } return 0; diff --git a/drivers/net/failsafe/failsafe_args.c b/drivers/net/failsafe/failsafe_args.c index 1b8f1d3050..fe53fecda8 100644 --- a/drivers/net/failsafe/failsafe_args.c +++ b/drivers/net/failsafe/failsafe_args.c @@ -89,6 +89,7 @@ fs_execute_cmd(struct sub_device *sdev, char *cmdline) FILE *fp; /* store possible newline as well */ char output[DEVARGS_MAXLEN + 1]; + char errmsg[RTE_STRERR_BUFSIZE]; size_t len; int ret; @@ -112,7 +113,9 @@ fs_execute_cmd(struct sub_device *sdev, char *cmdline) fp = popen(sdev->cmdline, "r"); if (fp == NULL) { ret = -errno; - ERROR("popen: %s", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + ERROR("popen: %s", errmsg); return ret; } /* We only read one line */ @@ -130,8 +133,11 @@ fs_execute_cmd(struct sub_device *sdev, char *cmdline) if (ret) ERROR("Parsing device '%s' failed", output); ret_pclose: - if (pclose(fp) == -1) - ERROR("pclose: %s", strerror(errno)); + if (pclose(fp) == -1) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + ERROR("pclose: %s", errmsg); + } return ret; } diff --git a/drivers/net/failsafe/failsafe_eal.c b/drivers/net/failsafe/failsafe_eal.c index e79d3b4120..8af72c6468 100644 --- a/drivers/net/failsafe/failsafe_eal.c +++ b/drivers/net/failsafe/failsafe_eal.c @@ -33,6 +33,7 @@ fs_ethdev_portid_get(const char *name, uint16_t *port_id) static int fs_bus_init(struct rte_eth_dev *dev) { + char errmsg[RTE_STRERR_BUFSIZE]; struct sub_device *sdev; struct rte_devargs *da; uint8_t i; @@ -99,8 +100,10 @@ fs_bus_init(struct rte_eth_dev *dev) " %d named %s", i, da->name); ret = rte_eth_dev_owner_set(pid, &PRIV(dev)->my_owner); if (ret < 0) { + if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret); INFO("sub_device %d owner set failed (%s), " - "will try again later", i, strerror(-ret)); + "will try again later", i, errmsg); continue; } else if (strncmp(rte_eth_devices[pid].device->name, da->name, strlen(da->name)) != 0) { diff --git a/drivers/net/failsafe/failsafe_ops.c b/drivers/net/failsafe/failsafe_ops.c index 9c013e0419..f1734a5d36 100644 --- a/drivers/net/failsafe/failsafe_ops.c +++ b/drivers/net/failsafe/failsafe_ops.c @@ -409,6 +409,7 @@ fs_rx_queue_setup(struct rte_eth_dev *dev, const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool) { + char errmsg[RTE_STRERR_BUFSIZE]; struct sub_device *sdev; struct rxq *rxq; uint8_t i; @@ -452,7 +453,9 @@ fs_rx_queue_setup(struct rte_eth_dev *dev, #ifdef RTE_EXEC_ENV_LINUX rxq->event_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (rxq->event_fd < 0) { - ERROR("Failed to create an eventfd: %s", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + ERROR("Failed to create an eventfd: %s", errmsg); fs_unlock(dev, 0); return -errno; } diff --git a/drivers/net/failsafe/failsafe_private.h b/drivers/net/failsafe/failsafe_private.h index babea6016e..54ac39b87b 100644 --- a/drivers/net/failsafe/failsafe_private.h +++ b/drivers/net/failsafe/failsafe_private.h @@ -401,20 +401,25 @@ fs_dev(struct sub_device *sdev) { static inline int fs_lock(struct rte_eth_dev *dev, unsigned int is_alarm) { + char errmsg[RTE_STRERR_BUFSIZE]; int ret; if (is_alarm) { ret = pthread_mutex_trylock(&PRIV(dev)->hotplug_mutex); if (ret) { + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); DEBUG("Hot-plug mutex lock trying failed(%s), will try" - " again later...", strerror(ret)); + " again later...", errmsg); return ret; } PRIV(dev)->alarm_lock = 1; } else { ret = pthread_mutex_lock(&PRIV(dev)->hotplug_mutex); if (ret) { - ERROR("Cannot lock mutex(%s)", strerror(ret)); + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); + ERROR("Cannot lock mutex(%s)", errmsg); return ret; } } @@ -428,6 +433,7 @@ fs_lock(struct rte_eth_dev *dev, unsigned int is_alarm) static inline void fs_unlock(struct rte_eth_dev *dev, unsigned int is_alarm) { + char errmsg[RTE_STRERR_BUFSIZE]; int ret; if (is_alarm) { @@ -435,8 +441,11 @@ fs_unlock(struct rte_eth_dev *dev, unsigned int is_alarm) PRIV(dev)->alarm_lock = 0; } ret = pthread_mutex_unlock(&PRIV(dev)->hotplug_mutex); - if (ret) - ERROR("Cannot unlock hot-plug mutex(%s)", strerror(ret)); + if (ret) { + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); + ERROR("Cannot unlock hot-plug mutex(%s)", errmsg); + } } /* -- 2.33.0