The function strerror() is insecure in a multi-thread environment.
This patch uses strerror_r() to replace it.

Signed-off-by: Dengdui Huang <huangdeng...@huawei.com>
Acked-by: Chengwen Feng <fengcheng...@huawei.com>
Acked-by: Morten Brørup <m...@smartsharesystems.com>
Acked-by: Huisong Li <lihuis...@huawei.com>
---
 drivers/bus/cdx/cdx.c      |  6 ++--
 drivers/bus/cdx/cdx_vfio.c | 58 +++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/drivers/bus/cdx/cdx.c b/drivers/bus/cdx/cdx.c
index 62b108e082..3e8ac81766 100644
--- a/drivers/bus/cdx/cdx.c
+++ b/drivers/bus/cdx/cdx.c
@@ -264,14 +264,16 @@ cdx_scan_one(const char *dirname, const char *dev_name)
 static int
 cdx_scan(void)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        struct dirent *e;
        DIR *dir;
        char dirname[PATH_MAX];
 
        dir = opendir(RTE_CDX_BUS_DEVICES_PATH);
        if (dir == NULL) {
-               CDX_BUS_INFO("%s(): opendir failed: %s", __func__,
-                       strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               CDX_BUS_INFO("%s(): opendir failed: %s", __func__, errmsg);
                return 0;
        }
 
diff --git a/drivers/bus/cdx/cdx_vfio.c b/drivers/bus/cdx/cdx_vfio.c
index 664f267471..a17da30701 100644
--- a/drivers/bus/cdx/cdx_vfio.c
+++ b/drivers/bus/cdx/cdx_vfio.c
@@ -20,6 +20,7 @@
 #include <rte_eal_paging.h>
 #include <rte_malloc.h>
 #include <rte_vfio.h>
+#include <rte_errno.h>
 
 #include "bus_cdx_driver.h"
 #include "cdx_logs.h"
@@ -186,6 +187,7 @@ static int
 cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int vfio_dev_fd,
                int num_irqs)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        int i, ret;
 
        if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
@@ -203,8 +205,10 @@ cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int 
vfio_dev_fd,
 
                ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
                if (ret < 0) {
+                       if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", errno);
                        CDX_BUS_ERR("Cannot get VFIO IRQ info, error %i (%s)",
-                               errno, strerror(errno));
+                               errno, errmsg);
                        return -1;
                }
 
@@ -221,8 +225,10 @@ cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int 
vfio_dev_fd,
                /* set up an eventfd for interrupts */
                fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
                if (fd < 0) {
+                       if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", errno);
                        CDX_BUS_ERR("Cannot set up eventfd, error %i (%s)",
-                               errno, strerror(errno));
+                               errno, errmsg);
                        return -1;
                }
 
@@ -244,6 +250,8 @@ static int
 cdx_vfio_setup_device(struct rte_cdx_device *dev, int vfio_dev_fd,
                int num_irqs)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
+
        if (cdx_vfio_setup_interrupts(dev, vfio_dev_fd, num_irqs) != 0) {
                CDX_BUS_ERR("Error setting up interrupts!");
                return -1;
@@ -254,8 +262,10 @@ cdx_vfio_setup_device(struct rte_cdx_device *dev, int 
vfio_dev_fd,
         * then it updates errno as EINVAL.
         */
        if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                CDX_BUS_ERR("Unable to reset device! Error: %d (%s)", errno,
-                       strerror(errno));
+                       errmsg);
                return -1;
        }
 
@@ -264,8 +274,10 @@ cdx_vfio_setup_device(struct rte_cdx_device *dev, int 
vfio_dev_fd,
         * device does not support configuring bus master.
         */
        if (rte_cdx_vfio_bm_enable(dev) && (errno != -ENOTTY)) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                CDX_BUS_ERR("Bus master enable failure! Error: %d (%s)", errno,
-                       strerror(errno));
+                       errmsg);
                return -1;
        }
 
@@ -393,6 +405,7 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev)
        const char *dev_name = dev->device.name;
        struct cdx_map *maps;
        int vfio_dev_fd, i, ret;
+       char errmsg[RTE_STRERR_BUFSIZE];
 
        if (rte_intr_fd_set(dev->intr_handle, -1))
                return -1;
@@ -422,8 +435,10 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev)
 
                ret = cdx_vfio_get_region_info(vfio_dev_fd, &reg, i);
                if (ret < 0) {
+                       if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", errno);
                        CDX_BUS_ERR("%s cannot get device region info error %i 
(%s)",
-                               dev_name, errno, strerror(errno));
+                               dev_name, errno, errmsg);
                        goto err_vfio_res;
                }
 
@@ -450,8 +465,10 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev)
 
                ret = cdx_vfio_mmap_resource(vfio_dev_fd, vfio_res, i, 0);
                if (ret < 0) {
+                       if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", errno);
                        CDX_BUS_ERR("%s mapping region %i failed: %s",
-                               cdx_addr, i, strerror(errno));
+                               cdx_addr, i, errmsg);
                        free(reg);
                        goto err_vfio_res;
                }
@@ -489,6 +506,7 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev)
        struct mapped_cdx_res_list *vfio_res_list =
                RTE_TAILQ_CAST(cdx_vfio_tailq.head, mapped_cdx_res_list);
        const char *dev_name = dev->device.name;
+       char errmsg[RTE_STRERR_BUFSIZE];
        struct cdx_map *maps;
 
        if (rte_intr_fd_set(dev->intr_handle, -1))
@@ -518,8 +536,10 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev)
        for (i = 0; i < vfio_res->nb_maps; i++) {
                ret = cdx_vfio_mmap_resource(vfio_dev_fd, vfio_res, i, 
MAP_FIXED);
                if (ret < 0) {
+                       if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", errno);
                        CDX_BUS_ERR("%s mapping MMIO region %i failed: %s",
-                               dev_name, i, strerror(errno));
+                               dev_name, i, errmsg);
                        goto err_vfio_dev_fd;
                }
 
@@ -617,6 +637,7 @@ rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev)
        struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
        struct vfio_device_feature_bus_master *vfio_bm_feature;
        struct vfio_device_feature *feature;
+       char errmsg[RTE_STRERR_BUFSIZE];
        int vfio_dev_fd, ret;
        size_t argsz;
 
@@ -638,8 +659,10 @@ rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev)
        feature->flags |= VFIO_DEVICE_FEATURE_SET;
        ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature);
        if (ret) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                CDX_BUS_ERR("Bus Master configuring not supported for device: 
%s, error: %d (%s)",
-                       dev->name, errno, strerror(errno));
+                       dev->name, errno, errmsg);
                free(feature);
                return ret;
        }
@@ -647,9 +670,12 @@ rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev)
        feature->flags = RTE_VFIO_DEVICE_FEATURE_BUS_MASTER | 
VFIO_DEVICE_FEATURE_SET;
        vfio_bm_feature->op = VFIO_DEVICE_FEATURE_SET_MASTER;
        ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature);
-       if (ret < 0)
+       if (ret < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                CDX_BUS_ERR("BM Enable Error for device: %s, Error: %d (%s)",
-                       dev->name, errno, strerror(errno));
+                       dev->name, errno, errmsg);
+       }
 
        free(feature);
        return ret;
@@ -661,6 +687,7 @@ rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev)
 {
        struct vfio_device_feature_bus_master *vfio_bm_feature;
        struct vfio_device_feature *feature;
+       char errmsg[RTE_STRERR_BUFSIZE];
        int vfio_dev_fd, ret;
        size_t argsz;
 
@@ -682,8 +709,10 @@ rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev)
        feature->flags |= VFIO_DEVICE_FEATURE_SET;
        ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature);
        if (ret) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                CDX_BUS_ERR("Bus Master configuring not supported for device: 
%s, Error: %d (%s)",
-                       dev->name, errno, strerror(errno));
+                       dev->name, errno, errmsg);
                free(feature);
                return ret;
        }
@@ -691,9 +720,12 @@ rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev)
        feature->flags = RTE_VFIO_DEVICE_FEATURE_BUS_MASTER | 
VFIO_DEVICE_FEATURE_SET;
        vfio_bm_feature->op = VFIO_DEVICE_FEATURE_CLEAR_MASTER;
        ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature);
-       if (ret < 0)
+       if (ret < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                CDX_BUS_ERR("BM Disable Error for device: %s, Error: %d (%s)",
-                       dev->name, errno, strerror(errno));
+                       dev->name, errno, errmsg);
+       }
 
        free(feature);
        return ret;
-- 
2.33.0

Reply via email to