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 D1BEC45C76; Mon, 4 Nov 2024 12:13:30 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 91FE04114B; Mon, 4 Nov 2024 12:11:12 +0100 (CET) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id 6079440E3E for ; Mon, 4 Nov 2024 12:10:45 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Xhpbr5rmLzfdh6; Mon, 4 Nov 2024 19:08:08 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id C036B180105; Mon, 4 Nov 2024 19:10:44 +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:44 +0800 From: Dengdui Huang To: CC: , , , , , , , , , , Subject: [PATCH v5 37/52] net/memif: replace strerror with reentrant version Date: Mon, 4 Nov 2024 19:10:22 +0800 Message-ID: <20241104111037.3632161-38-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/memif/memif_socket.c | 10 +++++-- drivers/net/memif/rte_eth_memif.c | 43 ++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/drivers/net/memif/memif_socket.c b/drivers/net/memif/memif_socket.c index 649f8d0e61..0bd4209920 100644 --- a/drivers/net/memif/memif_socket.c +++ b/drivers/net/memif/memif_socket.c @@ -57,6 +57,7 @@ memif_msg_send(int fd, memif_msg_t *msg, int afd) static int memif_msg_send_from_queue(struct memif_control_channel *cc) { + char errmsg[RTE_STRERR_BUFSIZE]; ssize_t size; int ret = 0; struct memif_msg_queue_elt *e; @@ -71,7 +72,9 @@ memif_msg_send_from_queue(struct memif_control_channel *cc) size = memif_msg_send(rte_intr_fd_get(cc->intr_handle), &e->msg, e->fd); if (size != sizeof(memif_msg_t)) { - MIF_LOG(ERR, "sendmsg fail: %s.", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + MIF_LOG(ERR, "sendmsg fail: %s.", errmsg); ret = -1; } else { MIF_LOG(DEBUG, "Sent msg type %u.", e->msg.type); @@ -891,6 +894,7 @@ memif_listener_handler(void *arg) static struct memif_socket * memif_socket_create(char *key, uint8_t listener, bool is_abstract, uid_t owner_uid, gid_t owner_gid) { + char errmsg[RTE_STRERR_BUFSIZE]; struct memif_socket *sock; struct sockaddr_un un = { 0 }; uint32_t sunlen; @@ -975,7 +979,9 @@ memif_socket_create(char *key, uint8_t listener, bool is_abstract, uid_t owner_u return sock; error: - MIF_LOG(ERR, "Failed to setup socket %s: %s", key, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + MIF_LOG(ERR, "Failed to setup socket %s: %s", key, errmsg); if (sock != NULL) { rte_intr_instance_free(sock->intr_handle); rte_free(sock); diff --git a/drivers/net/memif/rte_eth_memif.c b/drivers/net/memif/rte_eth_memif.c index cd722f254f..cdc1e0ced7 100644 --- a/drivers/net/memif/rte_eth_memif.c +++ b/drivers/net/memif/rte_eth_memif.c @@ -648,6 +648,7 @@ eth_memif_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) uint64_t a; ssize_t size; struct rte_eth_link link; + char errmsg[RTE_STRERR_BUFSIZE]; if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0)) return 0; @@ -806,8 +807,10 @@ eth_memif_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) size = write(rte_intr_fd_get(mq->intr_handle), &a, sizeof(a)); if (unlikely(size < 0)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); MIF_LOG(WARNING, - "Failed to send interrupt. %s", strerror(errno)); + "Failed to send interrupt. %s", errmsg); } } @@ -863,6 +866,7 @@ eth_memif_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) rte_eth_devices[mq->in_port].process_private; memif_ring_t *ring = memif_get_ring_from_queue(proc_private, mq); uint16_t slot, n_free, ring_size, mask, n_tx_pkts = 0; + char errmsg[RTE_STRERR_BUFSIZE]; struct rte_eth_link link; if (unlikely((pmd->flags & ETH_MEMIF_FLAG_CONNECTED) == 0)) @@ -957,8 +961,10 @@ eth_memif_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) ssize_t size = write(rte_intr_fd_get(mq->intr_handle), &a, sizeof(a)); if (unlikely(size < 0)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); MIF_LOG(WARNING, - "Failed to send interrupt. %s", strerror(errno)); + "Failed to send interrupt. %s", errmsg); } } @@ -1047,6 +1053,7 @@ memif_region_init_shm(struct rte_eth_dev *dev, uint8_t has_buffers) struct pmd_internals *pmd = dev->data->dev_private; struct pmd_process_private *proc_private = dev->process_private; char shm_name[ETH_MEMIF_SHM_NAME_SIZE]; + char errmsg[RTE_STRERR_BUFSIZE]; int ret = 0; struct memif_region *r; @@ -1080,27 +1087,35 @@ memif_region_init_shm(struct rte_eth_dev *dev, uint8_t has_buffers) r->fd = memfd_create(shm_name, MFD_ALLOW_SEALING); if (r->fd < 0) { - MIF_LOG(ERR, "Failed to create shm file: %s.", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + MIF_LOG(ERR, "Failed to create shm file: %s.", errmsg); ret = -1; goto error; } ret = fcntl(r->fd, F_ADD_SEALS, F_SEAL_SHRINK); if (ret < 0) { - MIF_LOG(ERR, "Failed to add seals to shm file: %s.", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + MIF_LOG(ERR, "Failed to add seals to shm file: %s.", errmsg); goto error; } ret = ftruncate(r->fd, r->region_size); if (ret < 0) { - MIF_LOG(ERR, "Failed to truncate shm file: %s.", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + MIF_LOG(ERR, "Failed to truncate shm file: %s.", errmsg); goto error; } r->addr = mmap(NULL, r->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, r->fd, 0); if (r->addr == MAP_FAILED) { - MIF_LOG(ERR, "Failed to mmap shm region: %s.", strerror(ret)); + if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret); + MIF_LOG(ERR, "Failed to mmap shm region: %s.", errmsg); ret = -1; goto error; } @@ -1204,6 +1219,7 @@ static int memif_init_queues(struct rte_eth_dev *dev) { struct pmd_internals *pmd = dev->data->dev_private; + char errmsg[RTE_STRERR_BUFSIZE]; struct memif_queue *mq; int i; @@ -1219,9 +1235,11 @@ memif_init_queues(struct rte_eth_dev *dev) return -rte_errno; if (rte_intr_fd_get(mq->intr_handle) < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); MIF_LOG(WARNING, "Failed to create eventfd for tx queue %d: %s.", i, - strerror(errno)); + errmsg); } mq->buffers = NULL; if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) { @@ -1243,9 +1261,11 @@ memif_init_queues(struct rte_eth_dev *dev) if (rte_intr_fd_set(mq->intr_handle, eventfd(0, EFD_NONBLOCK))) return -rte_errno; if (rte_intr_fd_get(mq->intr_handle) < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); MIF_LOG(WARNING, "Failed to create eventfd for rx queue %d: %s.", i, - strerror(errno)); + errmsg); } mq->buffers = NULL; if (pmd->flags & ETH_MEMIF_FLAG_ZERO_COPY) { @@ -1285,6 +1305,7 @@ memif_connect(struct rte_eth_dev *dev) { struct pmd_internals *pmd = dev->data->dev_private; struct pmd_process_private *proc_private = dev->process_private; + char errmsg[RTE_STRERR_BUFSIZE]; struct memif_region *mr; struct memif_queue *mq; memif_ring_t *ring; @@ -1300,8 +1321,10 @@ memif_connect(struct rte_eth_dev *dev) PROT_READ | PROT_WRITE, MAP_SHARED, mr->fd, 0); if (mr->addr == MAP_FAILED) { - MIF_LOG(ERR, "mmap failed: %s", - strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), + "Unknown error %d", errno); + MIF_LOG(ERR, "mmap failed: %s", errmsg); return -1; } } -- 2.33.0