Skip to contentSkip to conversation

pci-assign: do not test path with access() before opening

PATCH Applied Paolo Bonzini 3 participants 3 messages Nov 2, 2015 – Nov 3, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci-assign: do not test path with access() before opening

Using access() is a time-of-check/time-of-use race condition. It is okay to use them to provide better error messages, but that is pretty much it.

In this case we can get the same error from fopen(), so just use strerror and errno there---which actually improves the error message most of the time.

---
 hw/i386/pci-assign-load-rom.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/i386/pci-assign-load-rom.c +1 -5
index 34a3a7e..e40b586 100644
@@ -45,14 +45,10 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, struct Object *owner,
         return NULL;
     }
 
-    if (access(rom_file, F_OK)) {
-        error_report("pci-assign: Insufficient privileges for %s", rom_file);
-        return NULL;
-    }
-
     /* Write "1" to the ROM file to enable it */
     fp = fopen(rom_file, "r+");
     if (fp == NULL) {
+        error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
         return NULL;
     }
     val = 1;
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Paolo Bonzini

Using access() is a time-of-check/time-of-use race condition. It is okay to use them to provide better error messages, but that is pretty much it.

Quoted 8 lines

In this case we can get the same error from fopen(), so just use strerror and errno there---which actually improves the error message most of the time.

---
 hw/i386/pci-assign-load-rom.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Paolo Bonzini
Quoted 7 lines · Paolo Bonzini

Using access() is a time-of-check/time-of-use race condition. It is okay to use them to provide better error messages, but that is pretty much it.

In this case we can get the same error from fopen(), so just use strerror and errno there---which actually improves the error message most of the time.

Applied to -trivial, thank you!

/mjt