public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 3/3] fstrim: avoid TOCTOU race
Date: Wed, 16 Jul 2014 21:54:58 +0100	[thread overview]
Message-ID: <1405544098-1740-3-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1405544098-1740-1-git-send-email-kerolasa@iki.fi>

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 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


  parent reply	other threads:[~2014-07-16 20:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-16 20:54 [PATCH 1/3] lib/colors: remove memory leak Sami Kerola
2014-07-16 20:54 ` [PATCH 2/3] fallocate: avoid unnecessary computation Sami Kerola
2014-07-16 20:54 ` Sami Kerola [this message]
2014-07-17 12:02 ` [PATCH 1/3] lib/colors: remove memory leak Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1405544098-1740-3-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox