[v5,48/52] net/virtio: replace strerror with reentrant version

Message ID 20241104111037.3632161-49-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/virtio/virtio_user/vhost_kernel.c | 21 +++++--
 .../net/virtio/virtio_user/vhost_kernel_tap.c | 56 +++++++++++++++----
 drivers/net/virtio/virtio_user/vhost_user.c   | 52 ++++++++++++-----
 drivers/net/virtio/virtio_user/vhost_vdpa.c   | 30 ++++++++--
 .../net/virtio/virtio_user/virtio_user_dev.c  | 26 ++++++---
 drivers/net/virtio/virtio_user_ethdev.c       | 21 ++++---
 6 files changed, 155 insertions(+), 51 deletions(-)
  

Patch

diff --git a/drivers/net/virtio/virtio_user/vhost_kernel.c b/drivers/net/virtio/virtio_user/vhost_kernel.c
index e42bb35935..c3ae6dd476 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel.c
@@ -87,12 +87,15 @@  get_vhost_kernel_max_regions(void)
 static int
 vhost_kernel_ioctl(int fd, uint64_t request, void *arg)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	ret = ioctl(fd, request, arg);
 	if (ret) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Vhost-kernel ioctl %"PRIu64" failed (%s)",
-				request, strerror(errno));
+				request, errmsg);
 		return -1;
 	}
 
@@ -381,6 +384,7 @@  vhost_kernel_set_status(struct virtio_user_dev *dev __rte_unused, uint8_t status
 static int
 vhost_kernel_setup(struct virtio_user_dev *dev)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_kernel_data *data;
 	unsigned int tap_features;
 	unsigned int tap_flags;
@@ -428,7 +432,9 @@  vhost_kernel_setup(struct virtio_user_dev *dev)
 	for (i = 0; i < dev->max_queue_pairs; ++i) {
 		vhostfd = open(dev->path, O_RDWR);
 		if (vhostfd < 0) {
-			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, errmsg);
 			goto err_tapfds;
 		}
 		data->vhostfds[i] = vhostfd;
@@ -505,20 +511,23 @@  vhost_kernel_destroy(struct virtio_user_dev *dev)
 static int
 vhost_kernel_set_backend(int vhostfd, int tapfd)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_vring_file f;
 
 	f.fd = tapfd;
 	f.index = 0;
 	if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
-		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
-				strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s", errmsg);
 		return -1;
 	}
 
 	f.index = 1;
 	if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
-		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
-				strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s", errmsg);
 		return -1;
 	}
 
diff --git a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
index 611e2e25ec..4adc28df55 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
@@ -13,6 +13,7 @@ 
 #include <limits.h>
 
 #include <rte_ether.h>
+#include <rte_errno.h>
 
 #include "vhost_kernel_tap.h"
 #include "../virtio_logs.h"
@@ -22,17 +23,22 @@ 
 int
 tap_support_features(unsigned int *tap_features)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int tapfd;
 
 	tapfd = open(PATH_NET_TUN, O_RDWR);
 	if (tapfd < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "fail to open %s: %s",
-			    PATH_NET_TUN, strerror(errno));
+			    PATH_NET_TUN, errmsg);
 		return -1;
 	}
 
 	if (ioctl(tapfd, TUNGETFEATURES, tap_features) == -1) {
-		PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", errmsg);
 		close(tapfd);
 		return -1;
 	}
@@ -44,16 +50,21 @@  tap_support_features(unsigned int *tap_features)
 int
 tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct ifreq ifr;
 	int tapfd;
 
 	tapfd = open(PATH_NET_TUN, O_RDWR);
 	if (tapfd < 0) {
-		PMD_DRV_LOG(ERR, "fail to open %s: %s", PATH_NET_TUN, strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "fail to open %s: %s", PATH_NET_TUN, errmsg);
 		return -1;
 	}
 	if (fcntl(tapfd, F_SETFL, O_NONBLOCK) < 0) {
-		PMD_DRV_LOG(ERR, "fcntl tapfd failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "fcntl tapfd failed: %s", errmsg);
 		close(tapfd);
 		return -1;
 	}
@@ -66,14 +77,18 @@  tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 		ifr.ifr_flags |= IFF_MULTI_QUEUE;
 	if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
 		if (multi_queue) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			PMD_DRV_LOG(DEBUG,
 				"TUNSETIFF failed (will retry without IFF_MULTI_QUEUE): %s",
-				strerror(errno));
+				errmsg);
 			multi_queue = false;
 			goto retry_mono_q;
 		}
 
-		PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", errmsg);
 		close(tapfd);
 		tapfd = -1;
 	}
@@ -83,12 +98,15 @@  tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 int
 tap_get_name(int tapfd, char **name)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct ifreq ifr;
 	int ret;
 
 	memset(&ifr, 0, sizeof(ifr));
 	if (ioctl(tapfd, TUNGETIFF, (void *)&ifr) == -1) {
-		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", errmsg);
 		return -1;
 	}
 	ret = asprintf(name, "%s", ifr.ifr_name);
@@ -100,11 +118,14 @@  tap_get_name(int tapfd, char **name)
 int
 tap_get_flags(int tapfd, unsigned int *tap_flags)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct ifreq ifr;
 
 	memset(&ifr, 0, sizeof(ifr));
 	if (ioctl(tapfd, TUNGETIFF, (void *)&ifr) == -1) {
-		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "TUNGETIFF failed: %s", errmsg);
 		return -1;
 	}
 	*tap_flags = ifr.ifr_flags;
@@ -114,13 +135,16 @@  tap_get_flags(int tapfd, unsigned int *tap_flags)
 int
 tap_set_mac(int tapfd, uint8_t *mac)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct ifreq ifr;
 
 	memset(&ifr, 0, sizeof(ifr));
 	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
 	memcpy(ifr.ifr_hwaddr.sa_data, mac, RTE_ETHER_ADDR_LEN);
 	if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
-		PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", errmsg);
 		return -1;
 	}
 	return 0;
@@ -129,6 +153,7 @@  tap_set_mac(int tapfd, uint8_t *mac)
 static int
 vhost_kernel_tap_set_offload(int fd, uint64_t features)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	unsigned int offload = 0;
 
 	if (features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
@@ -154,8 +179,10 @@  vhost_kernel_tap_set_offload(int fd, uint64_t features)
 	if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
 		offload &= ~TUN_F_UFO;
 		if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
-				strerror(errno));
+				errmsg);
 			return -1;
 		}
 	}
@@ -166,6 +193,7 @@  vhost_kernel_tap_set_offload(int fd, uint64_t features)
 int
 vhost_kernel_tap_setup(int tapfd, int hdr_size, uint64_t features)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int sndbuf = INT_MAX;
 	int ret;
 
@@ -175,12 +203,16 @@  vhost_kernel_tap_setup(int tapfd, int hdr_size, uint64_t features)
 	 * max_mem_regions, supported in newer version linux kernel
 	 */
 	if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
-		PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", errmsg);
 		return -1;
 	}
 
 	if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
-		PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", errmsg);
 		return -1;
 	}
 
diff --git a/drivers/net/virtio/virtio_user/vhost_user.c b/drivers/net/virtio/virtio_user/vhost_user.c
index c10252506b..3f10fb95f4 100644
--- a/drivers/net/virtio/virtio_user/vhost_user.c
+++ b/drivers/net/virtio/virtio_user/vhost_user.c
@@ -111,6 +111,7 @@  vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
 	struct iovec iov;
 	size_t fd_size = fd_num * sizeof(int);
 	char control[CMSG_SPACE(fd_size)];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct cmsghdr *cmsg;
 
 	memset(&msgh, 0, sizeof(msgh));
@@ -135,8 +136,11 @@  vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
 		r = sendmsg(fd, &msgh, 0);
 	} while (r < 0 && errno == EINTR);
 
-	if (r < 0)
-		PMD_DRV_LOG(ERR, "Failed to send msg: %s", strerror(errno));
+	if (r < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "Failed to send msg: %s", errmsg);
+	}
 
 	return r;
 }
@@ -146,10 +150,13 @@  vhost_user_read(int fd, struct vhost_user_msg *msg)
 {
 	uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
 	int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	ret = recv(fd, (void *)msg, sz_hdr, 0);
 	if (ret < 0) {
-		PMD_DRV_LOG(ERR, "Failed to recv msg header: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "Failed to recv msg header: %s", errmsg);
 		return -1;
 	} else if (ret < sz_hdr) {
 		PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
@@ -175,7 +182,9 @@  vhost_user_read(int fd, struct vhost_user_msg *msg)
 	if (sz_payload) {
 		ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
 		if (ret < 0) {
-			PMD_DRV_LOG(ERR, "Failed to recv msg payload: %s", strerror(errno));
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			PMD_DRV_LOG(ERR, "Failed to recv msg payload: %s", errmsg);
 			return -1;
 		} else if (ret < sz_payload) {
 			PMD_DRV_LOG(ERR, "Failed to recv msg payload: %d instead of %u.",
@@ -745,12 +754,15 @@  vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
 	int ret;
 	int flag;
 	struct vhost_user_data *data = dev->backend_data;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int fd = data->listenfd;
 
 	ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
 	if (ret < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again",
-			    dev->path, strerror(errno));
+			    dev->path, errmsg);
 		return -1;
 	}
 	ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
@@ -760,14 +772,18 @@  vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
 	PMD_DRV_LOG(NOTICE, "(%s) waiting for client connection...", dev->path);
 	data->vhostfd = accept(fd, NULL, NULL);
 	if (data->vhostfd < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Failed to accept initial client connection (%s)",
-				strerror(errno));
+				errmsg);
 		return -1;
 	}
 
 	flag = fcntl(fd, F_GETFL);
 	if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
-		PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "fcntl failed, %s", errmsg);
 		return -1;
 	}
 
@@ -819,6 +835,7 @@  vhost_user_setup(struct virtio_user_dev *dev)
 	int flag;
 	struct sockaddr_un un;
 	struct vhost_user_data *data;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	data = malloc(sizeof(*data));
 	if (!data) {
@@ -835,15 +852,22 @@  vhost_user_setup(struct virtio_user_dev *dev)
 
 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (fd < 0) {
-		PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(ERR, "socket() error, %s", errmsg);
 		goto err_data;
 	}
 
 	flag = fcntl(fd, F_GETFD);
-	if (flag == -1)
-		PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", strerror(errno));
-	else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
-		PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", strerror(errno));
+	if (flag == -1) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", errmsg);
+	} else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", errmsg);
+	}
 
 	memset(&un, 0, sizeof(un));
 	un.sun_family = AF_UNIX;
@@ -857,7 +881,9 @@  vhost_user_setup(struct virtio_user_dev *dev)
 		}
 	} else {
 		if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
-			PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			PMD_DRV_LOG(ERR, "connect error, %s", errmsg);
 			goto err_socket;
 		}
 		data->vhostfd = fd;
diff --git a/drivers/net/virtio/virtio_user/vhost_vdpa.c b/drivers/net/virtio/virtio_user/vhost_vdpa.c
index bc3e2a9af5..ed4f4930ed 100644
--- a/drivers/net/virtio/virtio_user/vhost_vdpa.c
+++ b/drivers/net/virtio/virtio_user/vhost_vdpa.c
@@ -88,12 +88,15 @@  struct vhost_msg {
 static int
 vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	ret = ioctl(fd, request, arg);
 	if (ret) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
-				request, strerror(errno));
+				request, errmsg);
 		return -1;
 	}
 
@@ -172,6 +175,7 @@  static int
 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
 {
 	struct vhost_vdpa_data *data = dev->backend_data;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_msg msg = {};
 
 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
@@ -186,8 +190,10 @@  vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
 	msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
-				strerror(errno));
+				errmsg);
 		return -1;
 	}
 
@@ -198,6 +204,7 @@  static int
 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
 {
 	struct vhost_vdpa_data *data = dev->backend_data;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_msg msg = {};
 
 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
@@ -212,8 +219,10 @@  vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
 	msg.iotlb.type = VHOST_IOTLB_BATCH_END;
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
-				strerror(errno));
+				errmsg);
 		return -1;
 	}
 
@@ -225,6 +234,7 @@  vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
 				  uint64_t iova, size_t len)
 {
 	struct vhost_vdpa_data *data = dev->backend_data;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_msg msg = {};
 
 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
@@ -243,8 +253,10 @@  vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
 			__func__, iova, addr, len);
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
-				strerror(errno));
+				errmsg);
 		return -1;
 	}
 
@@ -256,6 +268,7 @@  vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
 				  uint64_t iova, size_t len)
 {
 	struct vhost_vdpa_data *data = dev->backend_data;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_msg msg = {};
 
 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
@@ -272,8 +285,10 @@  vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
 			__func__, iova, len);
 
 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
-				strerror(errno));
+				errmsg);
 		return -1;
 	}
 
@@ -519,6 +534,7 @@  vhost_vdpa_set_config(struct virtio_user_dev *dev, const uint8_t *data, uint32_t
 static int
 vhost_vdpa_setup(struct virtio_user_dev *dev)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vhost_vdpa_data *data;
 	uint32_t did = (uint32_t)-1;
 
@@ -530,8 +546,10 @@  vhost_vdpa_setup(struct virtio_user_dev *dev)
 
 	data->vhostfd = open(dev->path, O_RDWR);
 	if (data->vhostfd < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "Failed to open %s: %s",
-				dev->path, strerror(errno));
+				dev->path, errmsg);
 		free(data);
 		return -1;
 	}
diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c
index 2997d2bd26..a4aeaa8a0b 100644
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
@@ -53,20 +53,26 @@  virtio_user_uninit_notify_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
 static int
 virtio_user_init_notify_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
+
 	/* May use invalid flag, but some backend uses kickfd and
 	 * callfd as criteria to judge if dev is alive. so finally we
 	 * use real event_fd.
 	 */
 	dev->callfds[queue_sel] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
 	if (dev->callfds[queue_sel] < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "(%s) Failed to setup callfd for queue %u: %s",
-				dev->path, queue_sel, strerror(errno));
+				dev->path, queue_sel, errmsg);
 		return -1;
 	}
 	dev->kickfds[queue_sel] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
 	if (dev->kickfds[queue_sel] < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		PMD_DRV_LOG(ERR, "(%s) Failed to setup kickfd for queue %u: %s",
-				dev->path, queue_sel, strerror(errno));
+				dev->path, queue_sel, errmsg);
 		return -1;
 	}
 
@@ -1108,12 +1114,16 @@  static void
 virtio_user_control_queue_notify(struct virtqueue *vq, void *cookie)
 {
 	struct virtio_user_dev *dev = cookie;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint64_t notify_data = 1;
 
 	if (!dev->notify_area) {
-		if (write(dev->kickfds[vq->vq_queue_index], &notify_data, sizeof(notify_data)) < 0)
-			PMD_DRV_LOG(ERR, "failed to kick backend: %s",
-				    strerror(errno));
+		if (write(dev->kickfds[vq->vq_queue_index], &notify_data,
+		    sizeof(notify_data)) < 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			PMD_DRV_LOG(ERR, "failed to kick backend: %s", errmsg);
+		}
 		return;
 	} else if (!virtio_with_feature(&dev->hw, VIRTIO_F_NOTIFICATION_DATA)) {
 		rte_write16(vq->vq_queue_index, vq->notify_addr);
@@ -1337,6 +1347,7 @@  virtio_user_dev_server_reconnect(struct virtio_user_dev *dev)
 	int ret, old_status;
 	struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->hw.port_id];
 	struct virtio_hw *hw = &dev->hw;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (!dev->ops->server_reconnect) {
 		PMD_DRV_LOG(ERR, "(%s) Missing server reconnect callback", dev->path);
@@ -1357,8 +1368,9 @@  virtio_user_dev_server_reconnect(struct virtio_user_dev *dev)
 	virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
 
 	if (dev->ops->get_features(dev, &dev->device_features) < 0) {
-		PMD_INIT_LOG(ERR, "get_features failed: %s",
-			     strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_INIT_LOG(ERR, "get_features failed: %s", errmsg);
 		return -1;
 	}
 
diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c
index 747dddeb2e..d8611cda28 100644
--- a/drivers/net/virtio/virtio_user_ethdev.c
+++ b/drivers/net/virtio/virtio_user_ethdev.c
@@ -273,6 +273,7 @@  static void
 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
 {
 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint64_t notify_data = 1;
 
 	if (hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq)) {
@@ -283,9 +284,11 @@  virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
 
 	if (!dev->notify_area) {
 		if (write(dev->kickfds[vq->vq_queue_index], &notify_data,
-			  sizeof(notify_data)) < 0)
-			PMD_DRV_LOG(ERR, "failed to kick backend: %s",
-				    strerror(errno));
+			  sizeof(notify_data)) < 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			PMD_DRV_LOG(ERR, "failed to kick backend: %s", errmsg);
+		}
 		return;
 	} else if (!virtio_with_feature(hw, VIRTIO_F_NOTIFICATION_DATA)) {
 		rte_write16(vq->vq_queue_index, vq->notify_addr);
@@ -407,6 +410,7 @@  get_integer_arg(const char *key __rte_unused,
 static uint32_t
 vdpa_dynamic_major_num(void)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	FILE *fp;
 	char *line = NULL;
 	size_t size = 0;
@@ -416,8 +420,9 @@  vdpa_dynamic_major_num(void)
 
 	fp = fopen("/proc/devices", "r");
 	if (fp == NULL) {
-		PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
-			     strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s", errmsg);
 		return UNNAMED_MAJOR;
 	}
 
@@ -437,14 +442,16 @@  vdpa_dynamic_major_num(void)
 static enum virtio_user_backend_type
 virtio_user_backend_type(const char *path)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct stat sb;
 
 	if (stat(path, &sb) == -1) {
 		if (errno == ENOENT)
 			return VIRTIO_USER_BACKEND_VHOST_USER;
 
-		PMD_INIT_LOG(ERR, "Stat fails: %s (%s)", path,
-			     strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		PMD_INIT_LOG(ERR, "Stat fails: %s (%s)", path, errmsg);
 		return VIRTIO_USER_BACKEND_UNKNOWN;
 	}