]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fstrim: avoid TOCTOU race
authorSami Kerola <kerolasa@iki.fi>
Wed, 16 Jul 2014 20:54:58 +0000 (21:54 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 17 Jul 2014 12:01:02 +0000 (14:01 +0200)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/fstrim.c

index d52d83194c12f476b4273ece03a80554f1d807cd..5a238c7735cbcc1f8495307de54896b6997bb7a2 100644 (file)
@@ -67,7 +67,12 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
        /* kernel modifies the range */
        memcpy(&range, rangetpl, sizeof(range));
 
-       if (stat(path, &sb) == -1) {
+       fd = open(path, O_RDONLY);
+       if (fd < 0) {
+               warn(_("cannot open %s"), path);
+               return -1;
+       }
+       if (fstat(fd, &sb) == -1) {
                warn(_("stat failed %s"), path);
                return -1;
        }
@@ -75,12 +80,6 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
                warnx(_("%s: not a directory"), path);
                return -1;
        }
-
-       fd = open(path, O_RDONLY);
-       if (fd < 0) {
-               warn(_("cannot open %s"), path);
-               return -1;
-       }
        errno = 0;
        if (ioctl(fd, FITRIM, &range)) {
                int rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -1;