[PATCH 3/3] fstrim: avoid TOCTOU race

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Sami Kerola <kerolasa@xxxxxx>
---
 sys-utils/fstrim.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index d52d831..5a238c7 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;
-- 
2.0.1

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux