From: Sami Kerola Date: Wed, 16 Jul 2014 20:54:58 +0000 (+0100) Subject: fstrim: avoid TOCTOU race X-Git-Tag: v2.25~46 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e612ead96a0ba6599c27a920493687db74edf900;p=thirdparty%2Futil-linux.git fstrim: avoid TOCTOU race Signed-off-by: Sami Kerola --- diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c index d52d83194c..5a238c7735 100644 --- a/sys-utils/fstrim.c +++ b/sys-utils/fstrim.c @@ -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;