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

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

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Dengdui Huang Nov. 14, 2023, 12:35 p.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>
---
 lib/vhost/socket.c     | 13 +++++++------
 lib/vhost/vduse.c      | 31 ++++++++++++++++---------------
 lib/vhost/vhost_user.c | 11 ++++++-----
 3 files changed, 29 insertions(+), 26 deletions(-)
  

Patch

diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index 5882e44176..0b030222d6 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"
@@ -129,7 +130,7 @@  read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int m
 	if (ret <= 0) {
 		if (ret)
 			VHOST_LOG_CONFIG(ifname, ERR, "recvmsg failed on fd %d (%s)\n",
-				sockfd, strerror(errno));
+				sockfd, rte_strerror(errno));
 		return ret;
 	}
 
@@ -200,7 +201,7 @@  send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int f
 
 	if (ret < 0) {
 		VHOST_LOG_CONFIG(ifname, ERR, "sendmsg error on fd %d (%s)\n",
-			sockfd, strerror(errno));
+			sockfd, rte_strerror(errno));
 		return ret;
 	}
 
@@ -358,7 +359,7 @@  create_unix_socket(struct vhost_user_socket *vsocket)
 	if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
 		VHOST_LOG_CONFIG(vsocket->path, ERR,
 			"vhost-user: can't set nonblocking mode for socket, fd: %d (%s)\n",
-			fd, strerror(errno));
+			fd, rte_strerror(errno));
 		close(fd);
 		return -1;
 	}
@@ -392,7 +393,7 @@  vhost_user_start_server(struct vhost_user_socket *vsocket)
 	ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
 	if (ret < 0) {
 		VHOST_LOG_CONFIG(path, ERR, "failed to bind: %s; remove it and try again\n",
-			strerror(errno));
+			rte_strerror(errno));
 		goto err;
 	}
 	VHOST_LOG_CONFIG(path, INFO, "binding succeeded\n");
@@ -445,7 +446,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_LOG_CONFIG(path, ERR, "can't get flags for connfd %d (%s)\n",
-			fd, strerror(errno));
+			fd, rte_strerror(errno));
 		return -2;
 	}
 	if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
@@ -539,7 +540,7 @@  vhost_user_start_client(struct vhost_user_socket *vsocket)
 		return 0;
 	}
 
-	VHOST_LOG_CONFIG(path, WARNING, "failed to connect: %s\n", strerror(errno));
+	VHOST_LOG_CONFIG(path, WARNING, "failed to connect: %s\n", rte_strerror(errno));
 
 	if (ret == -2 || !vsocket->reconnect) {
 		close(fd);
diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index 080b58f7de..e2e056754c 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"
@@ -135,7 +136,7 @@  vduse_control_queue_event(int fd, void *arg, int *remove __rte_unused)
 	ret = read(fd, &buf, sizeof(buf));
 	if (ret < 0) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to read control queue event: %s\n",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	}
 
@@ -157,7 +158,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_GET_INFO, &vq_info);
 	if (ret) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to get VQ %u info: %s\n",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 		return;
 	}
 
@@ -183,7 +184,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index)
 	vq->kickfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
 	if (vq->kickfd < 0) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to init kickfd for VQ %u: %s\n",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 		vq->kickfd = VIRTIO_INVALID_EVENTFD;
 		return;
 	}
@@ -213,7 +214,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd);
 	if (ret) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to setup kickfd for VQ %u: %s\n",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 		close(vq->kickfd);
 		vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 		return;
@@ -224,7 +225,7 @@  vduse_vring_setup(struct virtio_net *dev, unsigned int index)
 		if (ret) {
 			VHOST_LOG_CONFIG(dev->ifname, ERR,
 					"Failed to setup kickfd handler for VQ %u: %s\n",
-					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);
@@ -254,7 +255,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_LOG_CONFIG(dev->ifname, ERR, "Failed to cleanup kickfd for VQ %u: %s\n",
-				index, strerror(errno));
+				index, rte_strerror(errno));
 
 	close(vq->kickfd);
 	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
@@ -291,7 +292,7 @@  vduse_device_start(struct virtio_net *dev)
 	ret = ioctl(dev->vduse_dev_fd, VDUSE_DEV_GET_FEATURES, &dev->features);
 	if (ret) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to get features: %s\n",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	}
 
@@ -358,7 +359,7 @@  vduse_events_handler(int fd, void *arg, int *remove __rte_unused)
 	ret = read(fd, &req, sizeof(req));
 	if (ret < 0) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Failed to read request: %s\n",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	} else if (ret < (int)sizeof(req)) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR, "Incomplete to read request %d\n", ret);
@@ -400,7 +401,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_LOG_CONFIG(dev->ifname, ERR, "Failed to write response %s\n",
-				strerror(errno));
+				rte_strerror(errno));
 		return;
 	}
 
@@ -453,13 +454,13 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 	control_fd = open(VDUSE_CTRL_PATH, O_RDWR);
 	if (control_fd < 0) {
 		VHOST_LOG_CONFIG(name, ERR, "Failed to open %s: %s\n",
-				VDUSE_CTRL_PATH, strerror(errno));
+				VDUSE_CTRL_PATH, rte_strerror(errno));
 		return -1;
 	}
 
 	if (ioctl(control_fd, VDUSE_SET_API_VERSION, &ver)) {
 		VHOST_LOG_CONFIG(name, ERR, "Failed to set API version: %" PRIu64 ": %s\n",
-				ver, strerror(errno));
+				ver, rte_strerror(errno));
 		ret = -1;
 		goto out_ctrl_close;
 	}
@@ -507,14 +508,14 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 	ret = ioctl(control_fd, VDUSE_CREATE_DEV, dev_config);
 	if (ret < 0) {
 		VHOST_LOG_CONFIG(name, ERR, "Failed to create VDUSE device: %s\n",
-				strerror(errno));
+				rte_strerror(errno));
 		goto out_free;
 	}
 
 	dev_fd = open(path, O_RDWR);
 	if (dev_fd < 0) {
 		VHOST_LOG_CONFIG(name, ERR, "Failed to open device %s: %s\n",
-				path, strerror(errno));
+				path, rte_strerror(errno));
 		ret = -1;
 		goto out_dev_close;
 	}
@@ -522,7 +523,7 @@  vduse_device_create(const char *path, bool compliant_ol_flags)
 	ret = fcntl(dev_fd, F_SETFL, O_NONBLOCK);
 	if (ret < 0) {
 		VHOST_LOG_CONFIG(name, ERR, "Failed to set chardev as non-blocking: %s\n",
-				strerror(errno));
+				rte_strerror(errno));
 		goto out_dev_close;
 	}
 
@@ -625,7 +626,7 @@  vduse_device_destroy(const char *path)
 		ret = ioctl(dev->vduse_ctrl_fd, VDUSE_DESTROY_DEV, name);
 		if (ret)
 			VHOST_LOG_CONFIG(name, ERR, "Failed to destroy VDUSE device: %s\n",
-					strerror(errno));
+					rte_strerror(errno));
 		close(dev->vduse_ctrl_fd);
 		dev->vduse_ctrl_fd = -1;
 	}
diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index e36312181a..ebf5afe45d 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -772,7 +772,8 @@  mem_set_dump(void *ptr, size_t size, bool enable, uint64_t pagesz)
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		rte_log(RTE_LOG_INFO, vhost_config_log_level,
-			"VHOST_CONFIG: could not set coredump preference (%s).\n", strerror(errno));
+			"VHOST_CONFIG: could not set coredump preference (%s).\n",
+			rte_strerror(errno));
 	}
 #endif
 }
@@ -1138,7 +1139,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;
 	}
 
@@ -1267,7 +1268,7 @@  vhost_user_mmap_region(struct virtio_net *dev,
 			MAP_SHARED | populate, region->fd, 0);
 
 	if (mmap_addr == MAP_FAILED) {
-		VHOST_LOG_CONFIG(dev->ifname, ERR, "mmap failed (%s).\n", strerror(errno));
+		VHOST_LOG_CONFIG(dev->ifname, ERR, "mmap failed (%s).\n", rte_strerror(errno));
 		return -1;
 	}
 
@@ -2698,7 +2699,7 @@  vhost_user_set_postcopy_advise(struct virtio_net **pdev,
 	if (dev->postcopy_ufd == -1) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR,
 			"userfaultfd not available: %s\n",
-			strerror(errno));
+			rte_strerror(errno));
 		return RTE_VHOST_MSG_RESULT_ERR;
 	}
 	api_struct.api = UFFD_API;
@@ -2706,7 +2707,7 @@  vhost_user_set_postcopy_advise(struct virtio_net **pdev,
 	if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
 		VHOST_LOG_CONFIG(dev->ifname, ERR,
 			"UFFDIO_API ioctl failure: %s\n",
-			strerror(errno));
+			rte_strerror(errno));
 		close(dev->postcopy_ufd);
 		dev->postcopy_ufd = -1;
 		return RTE_VHOST_MSG_RESULT_ERR;