From: NeilBrown <neilb@ownmail.net>
To: linux-kernel@vger.kernel.org
Subject: [PATCH 8/9] vfs: change vfs_lookup_open() to use do_open(), not vfs_open()
Date: Thu, 3 Dec 2026 11:08:03 +1100 [thread overview]
Message-ID: <20261203000848.2986775-9-neilb@ownmail.net> (raw)
In-Reply-To: <20261203000848.2986775-1-neilb@ownmail.net>
From: NeilBrown <neil@brown.name>
The vfs_open() call misses permission checks which should happen before
an open is attempted or accepted (Some ->atomic_open functions do
separate permission checks, others complete the open unchecked and leave
the VFS to perform the checks). This can be addressed by using
do_open() instead of vfs_open().
Much of the code in do_open() will have no effect as the relevant
LOOKUP_ flags aren't set. Truncation will be done (which nfsd is not
expected to use) along with some audit logs and security hooks. The
important benefit is getting the may_open() check.
So change vfs_lookup_open() to call do_open(), having first set
op.acc_mode to the appropriate combination of MAY_READ and MAY_WRITE to
be checked by may_open(), and op.dir_mode, op.dir_vfsuid to allow
for stick-directory checks.
As do_open() will consume nd.path on success (and some errors)
replacing with a NULL path, we must get an extra reference to the mnt,
and put whatever do_open() leaves in nd.path.
do_open() must be called even if FMODE_OPENED as some ->atomic_open
don't do permission checks - it checks FMODE_OPENED before calling
vfs_open_consume(). While it does perform all the type checks that we
need, it doesn't guarantee the same error codes so we keep the existing
checks and only call do_open() on a regular file.
Fixes: 536227b814bd ("VFS: add vfs_lookup_open() for nfsd")
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index ebd340adfee0..77a03e44029a 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4729,6 +4729,9 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
goto out;
}
+static int do_open(struct nameidata *nd,
+ struct file *file, const struct open_flags *op);
+
/**
* vfs_lookup_open - open and possibly create a regular file
* @parent: directory to contain file
@@ -4777,6 +4780,9 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
nd.path = *parent;
nd.last = *last;
+ nd.inode = parent->dentry->d_inode;
+ nd.dir_mode = nd.inode->i_mode;
+ nd.dir_vfsuid = i_uid_into_vfsuid(&nop_mnt_idmap, nd.inode);
nd.flags = LOOKUP_OPEN;
if (open_flag & O_CREAT) {
nd.flags |= LOOKUP_CREATE;
@@ -4785,11 +4791,18 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
op.mode = mode;
}
op.open_flag = open_flag;
+ op.acc_mode = ACC_MODE(open_flag);;
+ if (open_flag & O_TRUNC)
+ op.acc_mode |= MAY_WRITE;
dentry = lookup_open(&nd, file, &op);
if (IS_ERR(dentry))
return ERR_CAST(dentry);
+ /* do_open() may consume nd.path */
+ mntget(nd.path.mnt);
+ nd.path.dentry = dentry;
+
if (d_is_negative_acquire(dentry)) {
error = -ENOENT;
} else if (!(file->f_mode & FMODE_CREATED) && (open_flag & O_EXCL)) {
@@ -4800,11 +4813,10 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
error = -EISDIR;
if (d_is_symlink(dentry))
error = -ELOOP;
- } else if (!(file->f_mode & FMODE_OPENED)) {
- nd.path.dentry = dentry;
- error = vfs_open(&nd.path, file);
+ } else {
+ error = do_open(&nd, file, &op);
}
- dput(dentry);
+ path_put(&nd.path);
if (error)
return ERR_PTR(error);
--
2.50.0.107.gf914562f5916.dirty
next prev parent reply other threads:[~2026-12-03 0:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 0:07 [PATCH 0/9 v3] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
2026-12-03 0:07 ` [PATCH 1/9] VFS: introduce d_is_negative_acquire() NeilBrown
2026-12-03 0:07 ` [PATCH 2/9] vfs: revise and expand documentation for atomic_open NeilBrown
2026-12-03 0:07 ` [PATCH 3/9] vfs: only set op.mode when O_CREAT in vfs_lookup_open() NeilBrown
2026-12-03 0:07 ` [PATCH 4/9] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case NeilBrown
2026-12-03 0:08 ` [PATCH 5/9] vfs: add some allowed open flags to vfs_lookup_open() NeilBrown
2026-12-03 0:08 ` [PATCH 6/9] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory delegation NeilBrown
2026-12-03 0:08 ` [PATCH 7/9] vfs: don't return -ENODEV from vfs_lookup_open() NeilBrown
2026-12-03 0:08 ` NeilBrown [this message]
2026-12-03 0:08 ` [PATCH 9/9] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE NeilBrown
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=20261203000848.2986775-9-neilb@ownmail.net \
--to=neilb@ownmail.net \
--cc=linux-kernel@vger.kernel.org \
--cc=neil@brown.name \
/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
all inboxes | Powered by JetHome®