Skip to contentSkip to conversation

vfio/iommufd: Remove the use of stat() to check file existence

PATCH Applied Cédric Le Goater 2 participants 5 messages Dec 21, 2023 – Jan 2, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/iommufd: Remove the use of stat() to check file existence

Using stat() before opening a file or a directory can lead to a time-of-check to time-of-use (TOCTOU) filesystem race, which is reported by coverity as a Security best practices violations. The sequence could be replaced by open and fdopendir but it doesn't add much in this case. Simply use opendir to avoid the race.

Fixes CID 1531551
---
 hw/vfio/iommufd.c | 6 ------
 1 file changed, 6 deletions(-)
hw/vfio/iommufd.c +0 -6
index d4c586e842def8f04d3a914843f5eece2c75ea30..9bfddc1360895413176a9f170e29e89027384a66 100644
@@ -121,17 +121,11 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
     DIR *dir = NULL;
     struct dirent *dent;
     gchar *contents;
-    struct stat st;
     gsize length;
     int major, minor;
     dev_t vfio_devt;
 
     path = g_strdup_printf("%s/vfio-dev", sysfs_path);
-    if (stat(path, &st) < 0) {
-        error_setg_errno(errp, errno, "no such host device");
-        goto out_free_path;
-    }
-
     dir = opendir(path);
     if (!dir) {
         error_setg_errno(errp, errno, "couldn't open directory %s", path);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, December 21, 2023 4:10 PM
Subject: [PATCH] vfio/iommufd: Remove the use of stat() to check file existence

Quoted 8 lines

Using stat() before opening a file or a directory can lead to a time-of-check to time-of-use (TOCTOU) filesystem race, which is reported by coverity as a Security best practices violations. The sequence could be replaced by open and fdopendir but it doesn't add much in this case. Simply use opendir to avoid the race.

Fixes CID 1531551

Thanks for fixing, Reviewed-by: Zhenzhong Duan <[email protected]>

BRs.
Zhenzhong

Quoted 31 lines

---
hw/vfio/iommufd.c | 6 ------

1 file changed, 6 deletions(-)
hw/vfio/iommufd.c +0 -0

index d4c586e842def8f04d3a914843f5eece2c75ea30..9bfddc1360895413176a9f 170e29e89027384a66 100644

hw/vfio/iommufd.c +0 -6
@@ -121,17 +121,11 @@ static int iommufd_cdev_getfd(const char
 *sysfs_path, Error **errp)
    DIR *dir = NULL;
    struct dirent *dent;
    gchar *contents;
-    struct stat st;
    gsize length;
    int major, minor;
    dev_t vfio_devt;
 
    path = g_strdup_printf("%s/vfio-dev", sysfs_path);
-    if (stat(path, &st) < 0) {
-        error_setg_errno(errp, errno, "no such host device");
-        goto out_free_path;
-    }
-
    dir = opendir(path);
    if (!dir) {
        error_setg_errno(errp, errno, "couldn't open directory %s", path);

--
2.43.0

author Reviewed-by Zhenzhong Duan
#2 lore raw

Hello Zhenzhong

Quoted 14 lines · Zhenzhong
Quoted 14 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, December 21, 2023 4:10 PM
Subject: [PATCH] vfio/iommufd: Remove the use of stat() to check file existence

Using stat() before opening a file or a directory can lead to a time-of-check to time-of-use (TOCTOU) filesystem race, which is reported by coverity as a Security best practices violations. The sequence could be replaced by open and fdopendir but it doesn't add much in this case. Simply use opendir to avoid the race.

Quoted 4 lines
Quoted 2 lines
Fixes CID 1531551

Thanks for fixing, Reviewed-by: Zhenzhong Duan <[email protected]>

It seems that tools like b4 need the R-b tag, and probably other tags, to be at the beginning of a new line. So, just repeating :

Thanks,

C.

Quoted 36 lines

BRs.
Zhenzhong

Quoted 31 lines

---
hw/vfio/iommufd.c | 6 ------

1 file changed, 6 deletions(-)
hw/vfio/iommufd.c +0 -0

index d4c586e842def8f04d3a914843f5eece2c75ea30..9bfddc1360895413176a9f 170e29e89027384a66 100644

hw/vfio/iommufd.c +0 -6
@@ -121,17 +121,11 @@ static int iommufd_cdev_getfd(const char
 *sysfs_path, Error **errp)
     DIR *dir = NULL;
     struct dirent *dent;
     gchar *contents;
-    struct stat st;
     gsize length;
     int major, minor;
     dev_t vfio_devt;
 
     path = g_strdup_printf("%s/vfio-dev", sysfs_path);
-    if (stat(path, &st) < 0) {
-        error_setg_errno(errp, errno, "no such host device");
-        goto out_free_path;
-    }
-
     dir = opendir(path);
     if (!dir) {
         error_setg_errno(errp, errno, "couldn't open directory %s", path);

--
2.43.0

#3 lore raw
Quoted 28 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, December 21, 2023 5:16 PM
Subject: Re: [PATCH] vfio/iommufd: Remove the use of stat() to check file existence

Hello Zhenzhong

Quoted 17 lines · Zhenzhong
Quoted 16 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, December 21, 2023 4:10 PM
Subject: [PATCH] vfio/iommufd: Remove the use of stat() to check file existence

Using stat() before opening a file or a directory can lead to a time-of-check to time-of-use (TOCTOU) filesystem race, which is reported by coverity as a Security best practices violations. The sequence could be replaced by open and fdopendir but it doesn't add much in this case. Simply use opendir to avoid the race.

Fixes CID 1531551
Quoted 7 lines
Quoted 1 line

Thanks for fixing, Reviewed-by: Zhenzhong Duan

<[email protected]>

It seems that tools like b4 need the R-b tag, and probably other tags, to be at the beginning of a new line. So, just repeating :

Got it, will follow that rule in the future.

Thanks
Zhenzhong

author Applied
#4 lore raw
In reply to Cédric Le Goater
Quoted 8 lines · Cédric Le Goater

Using stat() before opening a file or a directory can lead to a time-of-check to time-of-use (TOCTOU) filesystem race, which is reported by coverity as a Security best practices violations. The sequence could be replaced by open and fdopendir but it doesn't add much in this case. Simply use opendir to avoid the race.

Fixes CID 1531551

Applied to vfio-next.

Thanks,

C.