From: Eric Sandeen <sandeen@redhat.com>
To: linux-xfs <linux-xfs@vger.kernel.org>
Subject: [PATCH 2/3] xfs_io: fix TOCTOU in openfile()
Date: Tue, 19 Feb 2019 17:23:38 -0600 [thread overview]
Message-ID: <acf1deea-a637-571b-e501-c7cbcde95c38@redhat.com> (raw)
In-Reply-To: <c4b7eff6-8395-3d01-eaaf-736e32e6e1dc@redhat.com>
openfile() stats a path to determine whether it is a pipe, and then
opens it with flags based on the type it saw during the stat. It's
possible that the file at that path changes in between, and Coverity
points this out.
Instead, always open O_NONBLOCK, stat the fd we got, and turn the
flag back off via fcntl() if it's not a pipe.
Addresses-Coverity-ID: 1442788 ("Time of check time of use")
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
diff --git a/io/open.c b/io/open.c
index f5fbd2c..856018b 100644
--- a/io/open.c
+++ b/io/open.c
@@ -62,6 +62,12 @@ openfile(
int oflags;
oflags = flags & IO_READONLY ? O_RDONLY : O_RDWR;
+ /*
+ * In case we've been passed a pipe to open, don't block waiting for a
+ * reader or writer to appear. We want to either succeed or error out
+ * immediately. We'll clear O_NONBLOCK if it's not a pipe.
+ */
+ oflags |= O_NONBLOCK;
if (flags & IO_APPEND)
oflags |= O_APPEND;
if (flags & IO_CREAT)
@@ -81,18 +87,6 @@ openfile(
if (flags & IO_NOFOLLOW)
oflags |= O_NOFOLLOW;
- /*
- * if we've been passed a pipe to open, don't block waiting for a
- * reader or writer to appear. We want to either succeed or error out
- * immediately.
- */
- if (stat(path, &st) < 0 && errno != ENOENT) {
- perror("stat");
- return -1;
- }
- if (S_ISFIFO(st.st_mode))
- oflags |= O_NONBLOCK;
-
fd = open(path, oflags, mode);
if (fd < 0) {
if (errno == EISDIR &&
@@ -112,6 +106,22 @@ openfile(
}
}
+ if (fstat(fd, &st) < 0) {
+ perror("stat");
+ close(fd);
+ return -1;
+ }
+
+ /* We only want to keep nonblocking behavior for pipes */
+ if (!S_ISFIFO(st.st_mode)) {
+ oflags &= ~O_NONBLOCK;
+ if (fcntl(fd, F_SETFL, oflags) < 0) {
+ perror("fcntl");
+ close(fd);
+ return -1;
+ }
+ }
+
if (!geom || !platform_test_xfs_fd(fd))
return fd;
next prev parent reply other threads:[~2019-02-19 23:23 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-19 23:12 [PATCH 0/3] xfsprogs: minor 4.20 fixups Eric Sandeen
2019-02-19 23:17 ` [PATCH 1/3] xfs_io: don't pass negative len to copy_file_range_cmd Eric Sandeen
2019-02-19 23:24 ` Darrick J. Wong
2019-02-19 23:50 ` Eric Sandeen
2019-02-20 17:16 ` [PATCH v2] xfs_io: actually check copy file range helper return values Darrick J. Wong
2019-02-22 20:01 ` Anna Schumaker
2019-02-19 23:23 ` Eric Sandeen [this message]
2019-02-20 1:50 ` [PATCH 2/3] xfs_io: fix TOCTOU in openfile() Dave Chinner
2019-02-20 4:41 ` Eric Sandeen
2019-02-20 17:23 ` Darrick J. Wong
2019-02-20 20:25 ` Eric Sandeen
2019-02-19 23:33 ` [PATCH 3/3] libhandle: zero terminate fspath string Eric Sandeen
2019-02-20 17:26 ` Darrick J. Wong
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=acf1deea-a637-571b-e501-c7cbcde95c38@redhat.com \
--to=sandeen@redhat.com \
--cc=linux-xfs@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