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/vmbus/linux/vmbus_bus.c  | 16 ++++++--
 drivers/bus/vmbus/linux/vmbus_uio.c  | 55 +++++++++++++++++++---------
 drivers/bus/vmbus/vmbus_common.c     | 13 +++++--
 drivers/bus/vmbus/vmbus_common_uio.c |  7 +++-
 4 files changed, 65 insertions(+), 26 deletions(-)

diff --git a/drivers/bus/vmbus/linux/vmbus_bus.c 
b/drivers/bus/vmbus/linux/vmbus_bus.c
index 01d8111b85..feb80518bf 100644
--- a/drivers/bus/vmbus/linux/vmbus_bus.c
+++ b/drivers/bus/vmbus/linux/vmbus_bus.c
@@ -18,6 +18,7 @@
 #include <rte_memory.h>
 #include <rte_malloc.h>
 #include <rte_bus_vmbus.h>
+#include <rte_errno.h>
 
 #include "eal_filesystem.h"
 #include "private.h"
@@ -43,14 +44,17 @@ extern struct rte_vmbus_bus rte_vmbus_bus;
 static int
 parse_sysfs_uuid(const char *filename, rte_uuid_t uu)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        char buf[BUFSIZ];
        char *cp, *in = buf;
        FILE *f;
 
        f = fopen(filename, "r");
        if (f == NULL) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                VMBUS_LOG(ERR, "cannot open sysfs value %s: %s",
-                         filename, strerror(errno));
+                         filename, errmsg);
                return -1;
        }
 
@@ -86,13 +90,16 @@ parse_sysfs_uuid(const char *filename, rte_uuid_t uu)
 static int
 get_sysfs_string(const char *filename, char *buf, size_t buflen)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        char *cp;
        FILE *f;
 
        f = fopen(filename, "r");
        if (f == NULL) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                VMBUS_LOG(ERR, "cannot open sysfs value %s:%s",
-                         filename, strerror(errno));
+                         filename, errmsg);
                return -1;
        }
 
@@ -341,6 +348,7 @@ vmbus_scan_one(const char *name)
 int
 rte_vmbus_scan(void)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        struct dirent *e;
        DIR *dir;
 
@@ -349,8 +357,10 @@ rte_vmbus_scan(void)
                if (errno == ENOENT)
                        return 0;
 
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                VMBUS_LOG(ERR, "opendir %s failed: %s",
-                         SYSFS_VMBUS_DEVICES, strerror(errno));
+                         SYSFS_VMBUS_DEVICES, errmsg);
                return -1;
        }
 
diff --git a/drivers/bus/vmbus/linux/vmbus_uio.c 
b/drivers/bus/vmbus/linux/vmbus_uio.c
index 26edef342d..3e7bc618e4 100644
--- a/drivers/bus/vmbus/linux/vmbus_uio.c
+++ b/drivers/bus/vmbus/linux/vmbus_uio.c
@@ -18,6 +18,7 @@
 #include <rte_malloc.h>
 #include <rte_bus_vmbus.h>
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 #include "private.h"
 
@@ -29,17 +30,21 @@ static void *vmbus_map_addr;
 /* Control interrupts */
 void vmbus_uio_irq_control(struct rte_vmbus_device *dev, int32_t onoff)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        if ((rte_intr_fd_get(dev->intr_handle) < 0) ||
            write(rte_intr_fd_get(dev->intr_handle), &onoff,
                  sizeof(onoff)) < 0) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                VMBUS_LOG(ERR, "cannot write to %d:%s",
                          rte_intr_fd_get(dev->intr_handle),
-                         strerror(errno));
+                         errmsg);
        }
 }
 
 int vmbus_uio_irq_read(struct rte_vmbus_device *dev)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        int32_t count;
        int cc;
 
@@ -50,8 +55,9 @@ int vmbus_uio_irq_read(struct rte_vmbus_device *dev)
                  sizeof(count));
        if (cc < (int)sizeof(count)) {
                if (cc < 0) {
-                       VMBUS_LOG(ERR, "IRQ read failed %s",
-                                 strerror(errno));
+                       if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", errno);
+                       VMBUS_LOG(ERR, "IRQ read failed %s", errmsg);
                        return -errno;
                }
                VMBUS_LOG(ERR, "can't read IRQ count");
@@ -83,6 +89,7 @@ int
 vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
                         struct mapped_vmbus_resource **uio_res)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        char devname[PATH_MAX]; /* contains the /dev/uioX */
        int fd;
 
@@ -90,8 +97,9 @@ vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
        snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num);
        fd = open(devname, O_RDWR);
        if (fd < 0) {
-               VMBUS_LOG(ERR, "Cannot open %s: %s",
-                       devname, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "Cannot open %s: %s", devname, errmsg);
                goto error;
        }
 
@@ -150,6 +158,7 @@ vmbus_uio_map_resource_by_index(struct rte_vmbus_device 
*dev, int idx,
 {
        size_t size = dev->resource[idx].len;
        struct vmbus_map *maps = uio_res->maps;
+       char errmsg[RTE_STRERR_BUFSIZE];
        void *mapaddr;
        off_t offset;
        int fd;
@@ -157,8 +166,9 @@ vmbus_uio_map_resource_by_index(struct rte_vmbus_device 
*dev, int idx,
        /* devname for mmap  */
        fd = open(uio_res->path, O_RDWR);
        if (fd < 0) {
-               VMBUS_LOG(ERR, "Cannot open %s: %s",
-                         uio_res->path, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "Cannot open %s: %s", uio_res->path, errmsg);
                return -1;
        }
 
@@ -211,6 +221,7 @@ static int vmbus_uio_map_subchan(const struct 
rte_vmbus_device *dev,
                                 const struct vmbus_channel *chan,
                                 void **ring_buf, uint32_t *ring_size)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        char ring_path[PATH_MAX];
        size_t file_size;
        struct stat sb;
@@ -255,14 +266,16 @@ static int vmbus_uio_map_subchan(const struct 
rte_vmbus_device *dev,
 
        fd = open(ring_path, O_RDWR);
        if (fd < 0) {
-               VMBUS_LOG(ERR, "Cannot open %s: %s",
-                         ring_path, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "Cannot open %s: %s", ring_path, errmsg);
                return -errno;
        }
 
        if (fstat(fd, &sb) < 0) {
-               VMBUS_LOG(ERR, "Cannot state %s: %s",
-                         ring_path, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "Cannot state %s: %s", ring_path, errmsg);
                close(fd);
                return -errno;
        }
@@ -334,6 +347,7 @@ int vmbus_uio_map_rings(struct vmbus_channel *chan)
 static int vmbus_uio_sysfs_read(const char *dir, const char *name,
                                unsigned long *val, unsigned long max_range)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        char path[PATH_MAX];
        FILE *f;
        int ret;
@@ -341,8 +355,9 @@ static int vmbus_uio_sysfs_read(const char *dir, const char 
*name,
        snprintf(path, sizeof(path), "%s/%s", dir, name);
        f = fopen(path, "r");
        if (!f) {
-               VMBUS_LOG(ERR, "can't open %s:%s",
-                         path, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "can't open %s:%s", path, errmsg);
                return -errno;
        }
 
@@ -393,6 +408,7 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary,
 {
        const struct rte_vmbus_device *dev = primary->device;
        char chan_path[PATH_MAX], subchan_path[PATH_MAX];
+       char errmsg[RTE_STRERR_BUFSIZE];
        struct dirent *ent;
        DIR *chan_dir;
        int err;
@@ -403,8 +419,9 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary,
 
        chan_dir = opendir(chan_path);
        if (!chan_dir) {
-               VMBUS_LOG(ERR, "cannot open %s: %s",
-                         chan_path, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "cannot open %s: %s", chan_path, errmsg);
                return -errno;
        }
 
@@ -440,8 +457,10 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary,
                err = vmbus_uio_sysfs_read(subchan_path, "subchannel_id",
                                           &subid, UINT16_MAX);
                if (err) {
+                       if (strerror_r(-err, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", -err);
                        VMBUS_LOG(NOTICE, "no subchannel_id in %s:%s",
-                                 subchan_path, strerror(-err));
+                                 subchan_path, errmsg);
                        goto fail;
                }
 
@@ -451,8 +470,10 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary,
                err = vmbus_uio_sysfs_read(subchan_path, "monitor_id",
                                           &monid, UINT8_MAX);
                if (err) {
+                       if (strerror_r(-err, errmsg, sizeof(errmsg)) != 0)
+                               snprintf(errmsg, sizeof(errmsg), "Unknown error 
%d", -err);
                        VMBUS_LOG(NOTICE, "no monitor_id in %s:%s",
-                                 subchan_path, strerror(-err));
+                                 subchan_path, errmsg);
                        goto fail;
                }
 
diff --git a/drivers/bus/vmbus/vmbus_common.c b/drivers/bus/vmbus/vmbus_common.c
index 8a965d10d9..017ead03e5 100644
--- a/drivers/bus/vmbus/vmbus_common.c
+++ b/drivers/bus/vmbus/vmbus_common.c
@@ -29,16 +29,18 @@ void *
 vmbus_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
                   int flags)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
        void *mapaddr;
 
        /* Map the memory resource of device */
        mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
                       MAP_SHARED | flags, fd, offset);
        if (mapaddr == MAP_FAILED) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                VMBUS_LOG(ERR,
                          "mmap(%d, %p, %zu, %ld) failed: %s",
-                         fd, requested_addr, size, (long)offset,
-                         strerror(errno));
+                         fd, requested_addr, size, (long)offset, errmsg);
        } else {
                VMBUS_LOG(DEBUG, "  VMBUS memory mapped at %p",
                          mapaddr);
@@ -50,14 +52,17 @@ vmbus_map_resource(void *requested_addr, int fd, off_t 
offset, size_t size,
 void
 vmbus_unmap_resource(void *requested_addr, size_t size)
 {
+       char errmsg[RTE_STRERR_BUFSIZE];
+
        if (requested_addr == NULL)
                return;
 
        /* Unmap the VMBUS memory resource of device */
        if (munmap(requested_addr, size)) {
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
                VMBUS_LOG(ERR, "munmap(%p, 0x%lx) failed: %s",
-                       requested_addr, (unsigned long)size,
-                       strerror(errno));
+                       requested_addr, (unsigned long)size, errmsg);
        } else {
                VMBUS_LOG(DEBUG, "  VMBUS memory unmapped at %p",
                          requested_addr);
diff --git a/drivers/bus/vmbus/vmbus_common_uio.c 
b/drivers/bus/vmbus/vmbus_common_uio.c
index 4d4613513c..d5f187c459 100644
--- a/drivers/bus/vmbus/vmbus_common_uio.c
+++ b/drivers/bus/vmbus/vmbus_common_uio.c
@@ -14,6 +14,7 @@
 #include <rte_log.h>
 #include <rte_malloc.h>
 #include <rte_bus_vmbus.h>
+#include <rte_errno.h>
 
 #include "private.h"
 
@@ -43,6 +44,7 @@ static int
 vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
 {
        struct mapped_vmbus_resource *uio_res;
+       char errmsg[RTE_STRERR_BUFSIZE];
        struct vmbus_channel *chan;
        int fd, i;
 
@@ -55,8 +57,9 @@ vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
        /* open /dev/uioX */
        fd = open(uio_res->path, O_RDWR);
        if (fd < 0) {
-               VMBUS_LOG(ERR, "Cannot open %s: %s",
-                         uio_res->path, strerror(errno));
+               if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+                       snprintf(errmsg, sizeof(errmsg), "Unknown error %d", 
errno);
+               VMBUS_LOG(ERR, "Cannot open %s: %s", uio_res->path, errmsg);
                return -1;
        }
 
-- 
2.33.0

Reply via email to