bus/pci: fix TOCTOU for sysfs access
[dpdk.git] / drivers / bus / pci / linux / pci_uio.c
index a7c1442..f240fe4 100644 (file)
@@ -16,6 +16,7 @@
 #include <sys/io.h>
 #endif
 
+#include <rte_string_fns.h>
 #include <rte_log.h>
 #include <rte_pci.h>
 #include <rte_bus_pci.h>
@@ -268,7 +269,7 @@ pci_uio_alloc_resource(struct rte_pci_device *dev,
                goto error;
        }
 
-       snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
+       strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
        memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
 
        return 0;
@@ -296,7 +297,7 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
        maps = uio_res->maps;
 
        /* allocate memory to keep path */
-       maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
+       maps[map_idx].path = rte_malloc(NULL, sizeof(devname), 0);
        if (maps[map_idx].path == NULL) {
                RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
                                strerror(errno));
@@ -314,12 +315,11 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
                        loc->domain, loc->bus, loc->devid,
                        loc->function, res_idx);
 
-               if (access(devname, R_OK|W_OK) != -1) {
-                       fd = open(devname, O_RDWR);
-                       if (fd < 0)
-                               RTE_LOG(INFO, EAL, "%s cannot be mapped. "
-                                       "Fall-back to non prefetchable mode.\n",
-                                       devname);
+               fd = open(devname, O_RDWR);
+               if (fd < 0 && errno != ENOENT) {
+                       RTE_LOG(INFO, EAL, "%s cannot be mapped. "
+                               "Fall-back to non prefetchable mode.\n",
+                               devname);
                }
        }