[PATCH 7/8] vfs: change vfs_lookup_open() to use do_open(), not vfs_open()

From: NeilBrown

Date: Wed Dec 02 2026 - 05:19:03 EST


From: NeilBrown <neil@xxxxxxxxxx>

The vfs_open() call misses permission checks which should happen before
an open is attempted (atomic_open does separate permission 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().

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() performs all the type checks that we need, except that it
assume the dentry is positive. lookup_open() will normally return a
positive dentry or an error, but that is not guaranteed so we need
check.

Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
---
fs/namei.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 95cedf840b20..30e2809265ce 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,26 +4791,22 @@ 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);

- if (d_is_miss(dentry)) {
+ /* do_open() may consume nd.path */
+ mntget(nd.path.mnt);
+ nd.path.dentry = dentry;
+ if (d_is_miss(dentry))
error = -ENOENT;
- } else if (!(file->f_mode & FMODE_CREATED) && (open_flag & O_EXCL)) {
- error = -EEXIST;
- } else if (!d_is_reg(dentry)) {
- error = -EFTYPE;
- if (d_is_dir(dentry))
- 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);
- }
- dput(dentry);
+ else
+ error = do_open(&nd, file, &op);
+ path_put(&nd.path);

if (error)
return ERR_PTR(error);
--
2.50.0.107.gf914562f5916.dirty