Skip to contentSkip to conversation

bus/pci: fix TOCTOU issue

PATCH Applied Stephen Hemminger 3 participants 3 messages Apr 2, 2019 – Jun 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bus/pci: fix TOCTOU issue

Using access followed by open causes a static analysis warning about Time of check versus Time of use. Also, access() and open() have different UID permission checks.

This is not a serious problem; but easy to fix by using errno instead.

Coverity issue: 300870

Fixes 4a928ef9f611 ("bus/pci: enable write combining during mapping")

---
v2 - add more CC to original mail, and rebase

 drivers/bus/pci/linux/pci_uio.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/bus/pci/linux/pci_uio.c +5 -6
index 09ecbb7aad25..0d1b9aa347ba 100644
@@ -314,12 +314,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);
 		}
 	}
 
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 38 lines · Stephen Hemminger

Using access followed by open causes a static analysis warning about Time of check versus Time of use. Also, access() and open() have different UID permission checks.

This is not a serious problem; but easy to fix by using errno instead.

Coverity issue: 300870

Fixes 4a928ef9f611 ("bus/pci: enable write combining during mapping")

---
v2 - add more CC to original mail, and rebase

 drivers/bus/pci/linux/pci_uio.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/bus/pci/linux/pci_uio.c +5 -6
index 09ecbb7aad25..0d1b9aa347ba 100644
@@ -314,12 +314,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);
                }
Quoted 3 lines
        }
Signature · 1 line
David Marchand
#2 lore raw

14/06/2019 16:16, David Marchand:

Quoted 11 lines
Quoted 8 lines · Stephen Hemminger

Using access followed by open causes a static analysis warning about Time of check versus Time of use. Also, access() and open() have different UID permission checks.

This is not a serious problem; but easy to fix by using errno instead.

Coverity issue: 300870

Fixes 4a928ef9f611 ("bus/pci: enable write combining during mapping")
Quoted 3 lines
Quoted 2 lines

Applied, thanks