From: Al Viro <viro@ZenIV.linux.org.uk>
To: Valdis.Kletnieks@vt.edu
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC] a corner case of open(2)
Date: Tue, 26 Apr 2016 20:02:48 +0100 [thread overview]
Message-ID: <20160426190248.GQ25498@ZenIV.linux.org.uk> (raw)
In-Reply-To: <89187.1461696097@turing-police.cc.vt.edu>
On Tue, Apr 26, 2016 at 02:41:37PM -0400, Valdis.Kletnieks@vt.edu wrote:
> On Tue, 26 Apr 2016 18:55:38 +0100, Al Viro said:
>
> > It is a change of user-visible behaviour, but I would be very
> > surprised if anything broke from that change. And it would help to simplify
> > the awful mess we have in there.
>
> I have to admit that over the past 3 decades of working with Unix-y systems,
> there's been a number of times I've had to resort to 'od -cx /your/dir/here'
> to debug issues (/bin/ls -fi is *almost* equivalent, but doesn't show holes
> in the directory)
>
> The biggest danger I can see is some shell script doing something like:
>
> foobar > $dir/$targetfile
>
> and $targetfile is unset. If we allow a program to get an open fd that refers
> to a directory, what are the semantics of various operations on that fd?
Huh? We certainly do allow to get an open fd that refers to a directory -
how else could ls(1) possibly work? See getdents(2) - it does use an
open file descriptor to specify the directory we operate upon.
We also do not allow opening directories for *write*, and in that case EISDIR
is the right error (and we do return it). The corner case in question is
different:
* O_CREAT present
* O_EXCL absent
* O_RDWR absent
* O_WRONLY absent
* pathname refers to existing directory
That's where POSIX says "just open it for read, as if O_CREAT hadn't been
there" and we fail with EISDIR. With both O_CREAT and O_EXCL POSIX says
"fail with EEXIST" and we either do that or fail with EISDIR, depending on the
pathname details. With either of O_RDWR and O_WRONLY POSIX says "fail with
EISDIR, O_CREAT or no O_CREAT" and that's what we do (and would certainly keep
doing so).
If you look at the code you'll see
case S_IFDIR:
if (acc_mode & MAY_WRITE)
return -EISDIR;
in may_open() and
error = -EISDIR;
if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
goto out;
in do_last(). The former is "can't open them rw or w", the latter - "can't
have O_CREAT on those". With O_CREAT|O_RDWR as in your example either one
would trigger (in reality the latter will trigger first and the call of
may_open() several lines below won't be reached at all).
next prev parent reply other threads:[~2016-04-26 19:02 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-26 17:55 [RFC] a corner case of open(2) Al Viro
2016-04-26 18:05 ` Cedric Blancher
2016-04-26 18:15 ` Al Viro
2016-04-26 18:41 ` Valdis.Kletnieks
2016-04-26 19:02 ` Al Viro [this message]
2016-04-26 19:25 ` Valdis.Kletnieks
2016-04-26 20:17 ` Al Viro
2016-04-26 20:17 ` Al Viro
2016-04-27 5:34 ` Al Viro
2016-04-27 9:33 ` Miklos Szeredi
2016-04-27 19:29 ` another patch in #for-linus (was Re: [RFC] a corner case of open(2)) Al Viro
2016-05-02 21:48 ` Pavel Machek
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=20160426190248.GQ25498@ZenIV.linux.org.uk \
--to=viro@zeniv.linux.org.uk \
--cc=Valdis.Kletnieks@vt.edu \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.