[v5,24/52] net/af_xdp: replace strerror with reentrant version

Message ID 20241104111037.3632161-25-huangdengdui@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series replace strerror |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

huangdengdui Nov. 4, 2024, 11:10 a.m. UTC
The function strerror() is insecure in a multi-thread environment.
This patch uses strerror_r() to replace it.

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Huisong Li <lihuisong@huawei.com>
---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
  

Patch

diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
index 814398ba4b..52ad450172 100644
--- a/drivers/net/af_xdp/rte_eth_af_xdp.c
+++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
@@ -1141,6 +1141,7 @@  xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 	void *aligned_addr;
 	uint64_t umem_size;
 	struct rte_mempool_mem_range_info range;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1190,8 +1191,10 @@  xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		ret = xsk_umem__create(&umem->umem, aligned_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
 		if (ret) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			AF_XDP_LOG_LINE(ERR, "Failed to create umem [%d]: [%s]",
-				   errno, strerror(errno));
+				   errno, errmsg);
 			goto err;
 		}
 		umem->buffer = aligned_addr;
@@ -1394,6 +1397,7 @@  configure_preferred_busy_poll(struct pkt_rx_queue *rxq)
 static int
 init_uds_sock(struct sockaddr_un *server, const char *dp_path)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int sock;
 
 	sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
@@ -1407,8 +1411,10 @@  init_uds_sock(struct sockaddr_un *server, const char *dp_path)
 
 	if (connect(sock, (struct sockaddr *)server, sizeof(struct sockaddr_un)) < 0) {
 		close(sock);
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		AF_XDP_LOG_LINE(ERR, "Error connecting stream socket errno = [%d]: [%s]",
-			   errno, strerror(errno));
+			   errno, errmsg);
 		return -1;
 	}
 
@@ -1472,6 +1478,7 @@  read_msg(int sock, char *response, struct sockaddr_un *s, int *fd)
 	struct iovec iov;
 	char control[CMSG_SPACE(sizeof(*fd))];
 	struct cmsghdr *cmsg;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	/* Initialize message header structure */
 	memset(&msgh, 0, sizeof(msgh));
@@ -1492,7 +1499,9 @@  read_msg(int sock, char *response, struct sockaddr_un *s, int *fd)
 		return 0;
 
 	if (msglen < 0) {
-		AF_XDP_LOG_LINE(ERR, "recvmsg failed, %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		AF_XDP_LOG_LINE(ERR, "recvmsg failed, %s", errmsg);
 		return -1;
 	}
 
@@ -1514,6 +1523,7 @@  static int
 make_request_dp(int sock, struct sockaddr_un *server, char *request,
 		 int *req_fd, char *response, int *out_fd, const char *dp_path)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int rval;
 
 	AF_XDP_LOG_LINE(DEBUG, "Request: [%s]", request);
@@ -1527,7 +1537,9 @@  make_request_dp(int sock, struct sockaddr_un *server, char *request,
 		rval = send_msg(sock, request, req_fd, dp_path);
 
 	if (rval < 0) {
-		AF_XDP_LOG_LINE(ERR, "Write error %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		AF_XDP_LOG_LINE(ERR, "Write error %s", errmsg);
 		return -1;
 	}
 
@@ -2013,6 +2025,7 @@  static int
 parse_prog_arg(const char *key __rte_unused,
 	       const char *value, void *extra_args)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	char *path = extra_args;
 
 	if (strnlen(value, PATH_MAX) == PATH_MAX) {
@@ -2022,8 +2035,10 @@  parse_prog_arg(const char *key __rte_unused,
 	}
 
 	if (access(value, F_OK) != 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		AF_XDP_LOG_LINE(ERR, "Error accessing %s: %s",
-			   value, strerror(errno));
+			   value, errmsg);
 		return -EINVAL;
 	}