[v5,49/52] raw/ifpga: replace strerror with reentrant version

Message ID 20241104111037.3632161-50-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/raw/ifpga/afu_pmd_n3000.c      | 10 ++++++++--
 drivers/raw/ifpga/base/ifpga_fme_rsu.c | 24 +++++++++++++++++++-----
 drivers/raw/ifpga/ifpga_rawdev.c       |  5 ++++-
 3 files changed, 31 insertions(+), 8 deletions(-)
  

Patch

diff --git a/drivers/raw/ifpga/afu_pmd_n3000.c b/drivers/raw/ifpga/afu_pmd_n3000.c
index 6aae1b224e..d9545a3b5f 100644
--- a/drivers/raw/ifpga/afu_pmd_n3000.c
+++ b/drivers/raw/ifpga/afu_pmd_n3000.c
@@ -21,6 +21,7 @@ 
 #include <bus_pci_driver.h>
 #include <bus_ifpga_driver.h>
 #include <rte_rawdev.h>
+#include <rte_errno.h>
 
 #include "afu_pmd_core.h"
 #include "afu_pmd_n3000.h"
@@ -680,6 +681,7 @@  static void clear_interrupt(struct dma_afu_ctx *ctx)
 
 static int poll_interrupt(struct dma_afu_ctx *ctx)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct pollfd pfd = {0};
 	uint64_t count = 0;
 	ssize_t bytes_read = 0;
@@ -693,7 +695,9 @@  static int poll_interrupt(struct dma_afu_ctx *ctx)
 	pfd.events = POLLIN;
 	poll_ret = poll(&pfd, 1, DMA_TIMEOUT_MSEC);
 	if (poll_ret < 0) {
-		IFPGA_RAWDEV_PMD_ERR("Error %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		IFPGA_RAWDEV_PMD_ERR("Error %s", errmsg);
 		ret = -EFAULT;
 		goto out;
 	} else if (poll_ret == 0) {
@@ -707,8 +711,10 @@  static int poll_interrupt(struct dma_afu_ctx *ctx)
 					poll_ret, count);
 			ret = 0;
 		} else {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			IFPGA_RAWDEV_PMD_ERR("Failed %s", bytes_read > 0 ?
-				strerror(errno) : "zero bytes read");
+				errmsg : "zero bytes read");
 			ret = -EIO;
 		}
 	}
diff --git a/drivers/raw/ifpga/base/ifpga_fme_rsu.c b/drivers/raw/ifpga/base/ifpga_fme_rsu.c
index f147aaa1e8..9f1643c5ee 100644
--- a/drivers/raw/ifpga/base/ifpga_fme_rsu.c
+++ b/drivers/raw/ifpga/base/ifpga_fme_rsu.c
@@ -7,6 +7,8 @@ 
 #include <unistd.h>
 #include "ifpga_sec_mgr.h"
 
+#include <rte_errno.h>
+
 static struct ifpga_sec_mgr *sec_mgr;
 
 static void set_rsu_control(struct ifpga_sec_mgr *smgr, uint32_t ctrl)
@@ -91,6 +93,7 @@  static int start_flash_update(struct ifpga_sec_mgr *smgr)
 static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,
 	uint32_t offset)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	void *buf = NULL;
 	int retry = 0;
 	uint32_t length = 0;
@@ -110,9 +113,11 @@  static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,
 
 	fd = open(image, O_RDONLY);
 	if (fd < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		dev_err(smgr,
 			"Failed to open \'%s\' for RD [e:%s]\n",
-			image, strerror(errno));
+			image, errmsg);
 		return -EIO;
 	}
 
@@ -129,15 +134,19 @@  static int write_flash_image(struct ifpga_sec_mgr *smgr, const char *image,
 		to_transfer = (length > IFPGA_RSU_DATA_BLK_SIZE) ?
 			IFPGA_RSU_DATA_BLK_SIZE : length;
 		if (lseek(fd, offset, SEEK_SET) < 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			dev_err(smgr, "Failed to seek in \'%s\' [e:%s]\n",
-				image, strerror(errno));
+				image, errmsg);
 			ret = -EIO;
 			goto end;
 		}
 		read_size = read(fd, buf, to_transfer);
 		if (read_size < 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			dev_err(smgr, "Failed to read from \'%s\' [e:%s]\n",
-				image, strerror(errno));
+				image, errmsg);
 			ret = -EIO;
 			goto end;
 		}
@@ -280,6 +289,7 @@  int fpga_update_flash(struct ifpga_fme_hw *fme, const char *image,
 	struct sigaction sa;
 	time_t start;
 	int ret = 0;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (!fme || !image || !status) {
 		dev_err(fme, "Input parameter of %s is invalid\n", __func__);
@@ -314,18 +324,22 @@  int fpga_update_flash(struct ifpga_fme_hw *fme, const char *image,
 
 	fd = open(image, O_RDONLY);
 	if (fd < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		dev_err(smgr,
 			"Failed to open \'%s\' for RD [e:%s]\n",
-			image, strerror(errno));
+			image, errmsg);
 		return -EIO;
 	}
 	len = lseek(fd, 0, SEEK_END);
 	close(fd);
 
 	if (len < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		dev_err(smgr,
 			"Failed to get file length of \'%s\' [e:%s]\n",
-			image, strerror(errno));
+			image, errmsg);
 		return -EIO;
 	}
 	if (len == 0) {
diff --git a/drivers/raw/ifpga/ifpga_rawdev.c b/drivers/raw/ifpga/ifpga_rawdev.c
index 5b9b596435..d6728079e1 100644
--- a/drivers/raw/ifpga/ifpga_rawdev.c
+++ b/drivers/raw/ifpga/ifpga_rawdev.c
@@ -834,6 +834,7 @@  static int
 rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 		const char *file_name)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct stat file_stat;
 	int file_fd;
 	int ret = 0;
@@ -848,7 +849,9 @@  rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
 	if (file_fd < 0) {
 		IFPGA_RAWDEV_PMD_ERR("%s: open file error: %s",
 				__func__, file_name);
-		IFPGA_RAWDEV_PMD_ERR("Message : %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		IFPGA_RAWDEV_PMD_ERR("Message : %s", errmsg);
 		return -EINVAL;
 	}
 	ret = stat(file_name, &file_stat);