[v4,08/42] vhost: use rte strerror

Message ID 20241023082852.2780488-9-huangdengdui@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers
Series replace strerror |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

huangdengdui Oct. 23, 2024, 8:28 a.m. UTC
The function strerror() is insecure in a multi-thread environment.
This patch uses rte_strerror() to replace it.

Cc: stable@dpdk.org

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 lib/vhost/fd_man.c     |  7 ++++---
 lib/vhost/socket.c     | 13 ++++++------
 lib/vhost/vduse.c      | 45 +++++++++++++++++++++---------------------
 lib/vhost/vhost_user.c | 10 +++++-----
 4 files changed, 39 insertions(+), 36 deletions(-)
  

Patch

diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c
index 9bc7e50b93..e9367fabee 100644
--- a/lib/vhost/fd_man.c
+++ b/lib/vhost/fd_man.c
@@ -13,6 +13,7 @@ 
 #include <rte_malloc.h>
 #include <rte_string_fns.h>
 #include <rte_thread.h>
+#include <rte_errno.h>
 
 #include "fd_man.h"
 
@@ -238,7 +239,7 @@  fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
 	ret = epoll_ctl(pfdset->epfd, EPOLL_CTL_ADD, fd, &ev);
 	if (ret < 0) {
 		VHOST_FDMAN_LOG(ERR, "could not add %d fd to %d epfd: %s",
-			fd, pfdset->epfd, strerror(errno));
+			fd, pfdset->epfd, rte_strerror(errno));
 		goto out_remove;
 	}
 
@@ -259,10 +260,10 @@  fdset_del_locked(struct fdset *pfdset, struct fdentry *pfdentry)
 	if (epoll_ctl(pfdset->epfd, EPOLL_CTL_DEL, pfdentry->fd, NULL) == -1) {
 		if (errno == EBADF) /* File might have already been closed. */
 			VHOST_FDMAN_LOG(DEBUG, "could not remove %d fd from %d epfd: %s",
-				pfdentry->fd, pfdset->epfd, strerror(errno));
+				pfdentry->fd, pfdset->epfd, rte_strerror(errno));
 		else
 			VHOST_FDMAN_LOG(ERR, "could not remove %d fd from %d epfd: %s",
-				pfdentry->fd, pfdset->epfd, strerror(errno));
+				pfdentry->fd, pfdset->epfd, rte_strerror(errno));
 	}
 
 	fdset_remove_entry(pfdset, pfdentry);
diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index d29d15494c..07ae2167ea 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -16,6 +16,7 @@ 
 
 #include <rte_thread.h>
 #include <rte_log.h>
+#include <rte_errno.h>
 
 #include "fd_man.h"
 #include "vduse.h"
@@ -124,7 +125,7 @@  read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int m
 	if (ret <= 0) {
 		if (ret)
 			VHOST_CONFIG_LOG(ifname, ERR, "recvmsg failed on fd %d (%s)",
-				sockfd, strerror(errno));
+				sockfd, rte_strerror(errno));
 		return ret;
 	}
 
@@ -195,7 +196,7 @@  send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int f
 
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(ifname, ERR, "sendmsg error on fd %d (%s)",
-			sockfd, strerror(errno));
+			sockfd, rte_strerror(errno));
 		return ret;
 	}
 
@@ -352,7 +353,7 @@  create_unix_socket(struct vhost_user_socket *vsocket)
 	if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
 		VHOST_CONFIG_LOG(vsocket->path, ERR,
 			"vhost-user: can't set nonblocking mode for socket, fd: %d (%s)",
-			fd, strerror(errno));
+			fd, rte_strerror(errno));
 		close(fd);
 		return -1;
 	}
@@ -386,7 +387,7 @@  vhost_user_start_server(struct vhost_user_socket *vsocket)
 	ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(path, ERR, "failed to bind: %s; remove it and try again",
-			strerror(errno));
+			rte_strerror(errno));
 		goto err;
 	}
 	VHOST_CONFIG_LOG(path, INFO, "binding succeeded");
@@ -439,7 +440,7 @@  vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
 	flags = fcntl(fd, F_GETFL, 0);
 	if (flags < 0) {
 		VHOST_CONFIG_LOG(path, ERR, "can't get flags for connfd %d (%s)",
-			fd, strerror(errno));
+			fd, rte_strerror(errno));
 		return -2;
 	}
 	if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
@@ -534,7 +535,7 @@  vhost_user_start_client(struct vhost_user_socket *vsocket)
 			return 0;
 		}
 
-		VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", strerror(errno));
+		VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", rte_strerror(errno));
 
 		if (ret == -2 || !vsocket->reconnect) {
 			close(fd);
diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index f9ac317438..9b241cc311 100644
--- a/lib/vhost/vduse.c
+++ b/lib/vhost/vduse.c
@@ -17,6 +17,7 @@ 
 
 #include <rte_common.h>
 #include <rte_thread.h>
+#include <rte_errno.h>
 
 #include "fd_man.h"
 #include "iotlb.h"
@@ -126,7 +127,7 @@  vduse_control_queue_event(int fd, void *arg, int *remove __rte_unused)
 	ret = read(fd, &buf, sizeof(buf));
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to read control queue event: %s",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	}
 
@@ -148,7 +149,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_GET_INFO, &vq_info);
 	if (ret) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get VQ %u info: %s",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 		return;
 	}
 
@@ -179,7 +180,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect)
 	vq->kickfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
 	if (vq->kickfd < 0) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to init kickfd for VQ %u: %s",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 		vq->kickfd = VIRTIO_INVALID_EVENTFD;
 		return;
 	}
@@ -211,7 +212,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
 	if (ret) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to setup kickfd for VQ %u: %s",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 		close(vq->kickfd);
 		vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 		return;
@@ -222,7 +223,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect)
 		if (ret) {
 			VHOST_CONFIG_LOG(dev->ifname, ERR,
 					"Failed to setup kickfd handler for VQ %u: %s",
-					index, strerror(errno));
+					index, rte_strerror(errno));
 			vq_efd.fd = VDUSE_EVENTFD_DEASSIGN;
 			ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
 			close(vq->kickfd);
@@ -249,7 +250,7 @@  vduse_vring_cleanup(struct virtio_net *dev, unsigned int index)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
 	if (ret)
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to cleanup kickfd for VQ %u: %s",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 
 	close(vq->kickfd);
 	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
@@ -288,7 +289,7 @@  vduse_device_start(struct virtio_net *dev, bool reconnect)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_DEV_GET_FEATURES, &dev->features);
 	if (ret) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get features: %s",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	}
 
@@ -364,7 +365,7 @@  vduse_events_handler(int fd, void *arg, int *remove __rte_unused)
 	ret = read(fd, &req, sizeof(req));
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to read request: %s",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	} else if (ret < (int)sizeof(req)) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Incomplete to read request %d", ret);
@@ -407,7 +408,7 @@  vduse_events_handler(int fd, void *arg, int *remove __rte_unused)
 	ret = write(dev->vduse_dev_fd, &resp, sizeof(resp));
 	if (ret != sizeof(resp)) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to write response %s",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	}
 
@@ -455,7 +456,7 @@  vduse_reconnect_path_init(void)
 	ret = mkdir(vduse_reconnect_dir, 0700);
 	if (ret < 0 && errno != EEXIST) {
 		VHOST_CONFIG_LOG("vduse", ERR, "Error creating '%s': %s",
-				vduse_reconnect_dir, strerror(errno));
+				vduse_reconnect_dir, rte_strerror(errno));
 		return -1;
 	}
 
@@ -516,13 +517,13 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 	control_fd = open(VDUSE_CTRL_PATH, O_RDWR);
 	if (control_fd < 0) {
 		VHOST_CONFIG_LOG(name, ERR, "Failed to open %s: %s",
-				VDUSE_CTRL_PATH, strerror(errno));
+				VDUSE_CTRL_PATH, rte_strerror(errno));
 		return -1;
 	}
 
 	if (ioctl(control_fd, VDUSE_SET_API_VERSION, &ver)) {
 		VHOST_CONFIG_LOG(name, ERR, "Failed to set API version: %" PRIu64 ": %s",
-				ver, strerror(errno));
+				ver, rte_strerror(errno));
 		ret = -1;
 		goto out_ctrl_close;
 	}
@@ -558,7 +559,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 						reconnect_file);
 			else
 				VHOST_CONFIG_LOG(name, ERR, "Failed to open reconnect file %s (%s)",
-						reconnect_file, strerror(errno));
+						reconnect_file, rte_strerror(errno));
 			ret = -1;
 			goto out_ctrl_close;
 		}
@@ -568,7 +569,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 		close(reco_fd);
 		if (reconnect_log == MAP_FAILED) {
 			VHOST_CONFIG_LOG(name, ERR, "Failed to mmap reconnect file %s (%s)",
-					reconnect_file, strerror(errno));
+					reconnect_file, rte_strerror(errno));
 			ret = -1;
 			goto out_ctrl_close;
 		}
@@ -602,7 +603,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 						reconnect_file);
 			} else {
 				VHOST_CONFIG_LOG(name, ERR, "Failed to open reconnect file %s (%s)",
-						reconnect_file, strerror(errno));
+						reconnect_file, rte_strerror(errno));
 			}
 			ret = -1;
 			goto out_ctrl_close;
@@ -611,7 +612,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 		ret = ftruncate(reco_fd, sizeof(*reconnect_log));
 		if (ret < 0) {
 			VHOST_CONFIG_LOG(name, ERR, "Failed to truncate reconnect file %s (%s)",
-					reconnect_file, strerror(errno));
+					reconnect_file, rte_strerror(errno));
 			close(reco_fd);
 			goto out_ctrl_close;
 		}
@@ -621,7 +622,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 		close(reco_fd);
 		if (reconnect_log == MAP_FAILED) {
 			VHOST_CONFIG_LOG(name, ERR, "Failed to mmap reconnect file %s (%s)",
-					reconnect_file, strerror(errno));
+					reconnect_file, rte_strerror(errno));
 			ret = -1;
 			goto out_ctrl_close;
 		}
@@ -651,7 +652,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 		ret = ioctl(control_fd, VDUSE_CREATE_DEV, dev_config);
 		if (ret < 0) {
 			VHOST_CONFIG_LOG(name, ERR, "Failed to create VDUSE device: %s",
-					strerror(errno));
+					rte_strerror(errno));
 			goto out_free;
 		}
 
@@ -664,7 +665,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 	dev_fd = open(path, O_RDWR);
 	if (dev_fd < 0) {
 		VHOST_CONFIG_LOG(name, ERR, "Failed to open device %s: %s",
-				path, strerror(errno));
+				path, rte_strerror(errno));
 		ret = -1;
 		goto out_dev_close;
 	}
@@ -672,7 +673,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 	ret = fcntl(dev_fd, F_SETFL, O_NONBLOCK);
 	if (ret < 0) {
 		VHOST_CONFIG_LOG(name, ERR, "Failed to set chardev as non-blocking: %s",
-				strerror(errno));
+				rte_strerror(errno));
 		goto out_dev_close;
 	}
 
@@ -741,7 +742,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 		reco_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
 		if (reco_fd < 0) {
 			VHOST_CONFIG_LOG(name, ERR, "Failed to create reco_fd: %s",
-					strerror(errno));
+					rte_strerror(errno));
 			ret = -1;
 			goto out_dev_destroy;
 		}
@@ -814,7 +815,7 @@  vduse_device_destroy(const char *path)
 		ret = ioctl(dev->vduse_ctrl_fd, VDUSE_DESTROY_DEV, name);
 		if (ret) {
 			VHOST_CONFIG_LOG(name, ERR, "Failed to destroy VDUSE device: %s",
-					strerror(errno));
+					rte_strerror(errno));
 		} else {
 			/*
 			 * VDUSE device was no more attached to the vDPA bus,
diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 6d92ad904e..32b5f46749 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -832,7 +832,7 @@  mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
-			"could not set coredump preference (%s).", strerror(errno));
+			"could not set coredump preference (%s).", rte_strerror(errno));
 	}
 #endif
 }
@@ -1210,7 +1210,7 @@  vhost_user_postcopy_region_register(struct virtio_net *dev,
 			(uint64_t)reg_struct.range.start +
 			(uint64_t)reg_struct.range.len - 1,
 			dev->postcopy_ufd,
-			strerror(errno));
+			rte_strerror(errno));
 		return -1;
 	}
 
@@ -1339,7 +1339,7 @@  vhost_user_mmap_region(struct virtio_net *dev,
 			MAP_SHARED | populate, region->fd, 0);
 
 	if (mmap_addr == MAP_FAILED) {
-		VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno));
+		VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", rte_strerror(errno));
 		return -1;
 	}
 
@@ -2776,7 +2776,7 @@  vhost_user_set_postcopy_advise(struct virtio_net **pdev,
 	if (dev->postcopy_ufd == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR,
 			"userfaultfd not available: %s",
-			strerror(errno));
+			rte_strerror(errno));
 		return RTE_VHOST_MSG_RESULT_ERR;
 	}
 	api_struct.api = UFFD_API;
@@ -2784,7 +2784,7 @@  vhost_user_set_postcopy_advise(struct virtio_net **pdev,
 	if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
 		VHOST_CONFIG_LOG(dev->ifname, ERR,
 			"UFFDIO_API ioctl failure: %s",
-			strerror(errno));
+			rte_strerror(errno));
 		close(dev->postcopy_ufd);
 		dev->postcopy_ufd = -1;
 		return RTE_VHOST_MSG_RESULT_ERR;