From: NeilBrown As discussed in Commit: 2fa6b1e01a9b ("fs/namei.c: fix missing barriers when checking positivity") d_is_negative() is only safe when the dentry is locked against change, either by d_lock or parent's i_rwsem. When not locked an acquire barrier is needed. A recent change introduced a new use of d_is_negative() is a non-locked situation. To make it easier to get this sort of thing right, introduce d_is_negative_acquire(), document when it should be used and when d_is_negative() should be avoided, and use d_is_negative_acquire() where appropriate. Fixes: 98b6cf102b35 ("namei: test for a negative dentry the way traverse_mounts() does") Signed-off-by: NeilBrown --- fs/dcache.c | 1 + fs/namei.c | 8 ++++---- fs/overlayfs/namei.c | 2 +- include/linux/dcache.h | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index a438a7de43f0..af67582a0a7d 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -427,6 +427,7 @@ static inline void __d_set_inode_and_type(struct dentry *dentry, flags = READ_ONCE(dentry->d_flags); flags &= ~DCACHE_ENTRY_TYPE; flags |= type_flags; + /* release matches acquire in d_is_negative_acquire() */ smp_store_release(&dentry->d_flags, flags); } diff --git a/fs/namei.c b/fs/namei.c index c79eecffe4ac..3dc2cf170aa2 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3329,7 +3329,7 @@ struct dentry *lookup_one_positive_killable(const struct mnt_idmap *idmap, ret = lookup_dcache(name, base, 0); if (!ret) ret = lookup_slow_killable(name, base, 0); - if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { + if (!IS_ERR(ret) && d_is_negative_acquire(ret)) { dput(ret); ret = ERR_PTR(-ENOENT); } @@ -3364,7 +3364,7 @@ struct dentry *lookup_one_positive_unlocked(const struct mnt_idmap *idmap, { struct dentry *ret = lookup_one_unlocked(idmap, name, base); - if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { + if (!IS_ERR(ret) && d_is_negative_acquire(ret)) { dput(ret); ret = ERR_PTR(-ENOENT); } @@ -3424,7 +3424,7 @@ struct dentry *lookup_noperm_positive_unlocked(struct qstr *name, struct dentry *ret; ret = lookup_noperm_unlocked(name, base); - if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { + if (!IS_ERR(ret) && d_is_negative_acquire(ret)) { dput(ret); ret = ERR_PTR(-ENOENT); } @@ -4830,7 +4830,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag) return dentry; if (open_flag & O_CREAT) { - if (d_is_negative(dentry)) { + if (d_is_negative_acquire(dentry)) { if (!(nd->flags & LOOKUP_RCU)) dput(dentry); dentry = NULL; diff --git a/fs/overlayfs/namei.c b/fs/overlayfs/namei.c index ddf60254888a..8fa8c168dd50 100644 --- a/fs/overlayfs/namei.c +++ b/fs/overlayfs/namei.c @@ -216,7 +216,7 @@ static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d, struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->layer->mnt), &QSTR_LEN(name, len), base); - if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { + if (!IS_ERR(ret) && d_is_negative_acquire(ret)) { if (drop_negative && ret->d_lockref.count == 1) { spin_lock(&ret->d_lock); /* Recheck condition under lock */ diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 1d03b3c6e081..7c03a862f675 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -498,6 +498,16 @@ static inline bool d_is_file(const struct dentry *dentry) return d_is_reg(dentry) || d_is_special(dentry); } +/** + * d_is_negative - test if a dentry is negative + * @dentry: the dentry to test + * + * This should only be used if the dentry is locked against + * change, either by ->d_lock or by parent's ->i_rwsem. + * Outside this context use d_really_is_negative() if the sole + * purpose is to determine if it is safe to dereference the inode, + * or use d_is_negative_acquire() if the context is more general. + */ static inline bool d_is_negative(const struct dentry *dentry) { // TODO: check d_is_whiteout(dentry) also. @@ -509,6 +519,30 @@ static inline bool d_flags_negative(unsigned flags) return (flags & DCACHE_ENTRY_TYPE) == DCACHE_MISS_TYPE; } +/** + * d_is_negative_acquire - test if a dentry is negative, with barrier + * @dentry: the dentry to test + * + * This should be used if the dentry is NOT locked against + * change, either by ->d_lock or by parent's ->i_rwsem. + * In a locked context it is preferred to use d_is_negative(). + */ +static inline bool d_is_negative_acquire(const struct dentry *dentry) +{ + /* acquire matches release in __d_set_inode_and_type() */ + return d_flags_negative(smp_load_acquire(&dentry->d_flags)); +} + +/** + * d_is_positive - test if a dentry is positive + * @dentry: the dentry to test + * + * This should only be used if the dentry is locked against + * change, either by ->d_lock or by parent's ->i_rwsem. + * Outside this context use d_really_is_positive() if the sole + * purpose is to determine if it is safe to dereference the inode, + * or use !d_is_negative_acquire() if the context is more general. + */ static inline bool d_is_positive(const struct dentry *dentry) { return !d_is_negative(dentry); base-commit: 84fb6f8a1ba91eee3ffe6aa519686fce851a0e42 -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown atomic_open is a complex operation which different filesystems implement quite differently. The available documentation doesn't give clear guidance on how it should be implemented. nfsd has a particular need to open only regular files, but to get precise information about what was found if it wasn't a regular file. This is slightly different to the syscall calling needs. In particular it suggests that __O_REGULAR shouldn't always result in -EFTYPE. In any case that does involve creating open state, using finish_no_open() is simplest as it reduces the need to check __O_REGULAR, O_DIRECTORY, O_NOFOLLOW. So refresh the documentation to give guidance on the choice between finish_no_open, finish_open, and an error. Efficiency always wins, but when that isn't an issue, prefer finish_no_open(). Also clarify the required behaviour when __O_REGULAR is given. This should return -EISDIR if a directory is found as nfsd needs this. If a symlink is found then __O_REGULAR does NOT apply: O_NOFOLLOW must be used to decided if it is safe to not return the looked-up dentry. Assorted improvements thanks to Andreas Gruenbacher Reviewed-by: Jeff Layton Signed-off-by: NeilBrown --- Documentation/filesystems/vfs.rst | 68 +++++++++++++++++++++++++------ fs/namei.c | 6 ++- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index 9ab182dbbd73..c1f3ea7aad67 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -600,18 +600,62 @@ otherwise noted. inode. ``atomic_open`` - called on the last component of an open. Using this optional - method the filesystem can look up, possibly create and open the - file in one atomic operation. If it wants to leave actual - opening to the caller (e.g. if the file turned out to be a - symlink, device, or just something filesystem won't do atomic - open for), it may signal this by returning finish_no_open(file, - dentry). This method is only called if the last component is - negative or needs lookup. Cached positive dentries are still - handled by f_op->open(). If the file was created, FMODE_CREATED - flag should be set in file->f_mode. In case of O_EXCL the - method must only succeed if the file didn't exist and hence - FMODE_CREATED shall always be set on success. + called on the last component of an open. This optional method + can look up, create, truncate, and open the file in one atomic + operation. This is needed if the filesystem content can change + asynchronously and specifically, if a negative dentry is not a + guarantee that the object doesn't exist. It is also useful if + combinations of revalidate, lookup, create, open, and truncate + can be performed more efficiently than with a sequence of + individual operations. + + If the object found is not a file or directory, or if + lookup/create succeeded without establishing any "open" state, + then finish_no_open() should be called to indicate that the + dentry is ready to be handled by normal VFS processing. + FMODE_CREATED should be set in file->f_mode if the object was + created; this will prevent further access permission checks and + handling of O_TRUNC and O_EXCL. + + If the lookup/create operation established some open state for a + file or directory, the operation should be completed by calling + finish_open(). Passing NULL as the "open" function to + finish_open() is unlikely to be useful as that assumes that no + open state has been established. + + atomic_open() may generate errors related to O_DIRECTORY, + __O_REGULAR, O_EXCL, O_NOFOLLOW, but is not required to as the + caller will check those against the resulting dentry and + generate any error needed, possibly closing the file if it was + opened by finish_open(). atomic_open() is encouraged to handle + these flags only when doing so is more efficient than not. + + If __O_REGULAR is handled, it should generate -EISDIR if the + name is known to be a directory, and -EFTYPE if it is some other + non-regular file other than a symbolic link. Handling of + symbolic links should be guided by O_NOFOLLOW, not __O_REGULAR: + -ELOOP can be returned if O_NOFOLLOW is set; otherwise the symlink + should be returned through finish_no_open(). + + The focus for atomic_open() is to provide the correct dentry and + to set FMODE_CREATED as accurately as possible. If O_EXCL is + set, FMODE_CREATED should only be set if this operation + certainly created the object. If O_EXCL is not set, + FMODE_CREATED should be set if it is possible that this operation + created the object. + + This method is only called if the last component is negative or + needs lookup. Cached positive dentries are still handled by + f_op->open(). + + If the dentry provided is negative (not in-lookup) and O_CREAT + isn't set, then there is no guarantee of exclusive access to the + dentry - another thread might call ->atomic_open() on the same + dentry at the same time. If needed, a filesystem can ensure this + doesn't happen by returning 0 from ->d_revalidate when that is + called with LOOKUP_OPEN on a negative dentry. This will ensure + that ->atomic_open() only receives an in-lookup dentry, which + always ensures exclusive access. ``tmpfile`` called in the end of O_TMPFILE open(). Optional, equivalent to diff --git a/fs/namei.c b/fs/namei.c index 3dc2cf170aa2..b65cf552ec3a 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4932,7 +4932,8 @@ static int do_open(struct nameidata *nd, return error; } - if ((open_flag & __O_REGULAR) && !d_is_reg(nd->path.dentry)) + if ((open_flag & __O_REGULAR) && !d_is_reg(nd->path.dentry) + && !d_is_symlink(nd->path.dentry)) return -EFTYPE; if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) @@ -5133,6 +5134,9 @@ static struct file *path_openat(struct nameidata *nd, error = -EINVAL; } fput_close(file); + if (error == -EISDIR && + (op->open_flag & __O_REGULAR)) + error = -EFTYPE; if (error == -EOPENSTALE) { if (flags & LOOKUP_RCU) error = -ECHILD; -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown build_open_flags() only sets op.mode when O_CREAT (or __O_TMPFILE). Change vfs_lookup_open() to follow the same pattern for consistency. Signed-off-by: NeilBrown --- fs/namei.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/namei.c b/fs/namei.c index b65cf552ec3a..55ade0626f2f 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4767,9 +4767,9 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last, nd.flags |= LOOKUP_CREATE; if (open_flag & O_EXCL) nd.flags |= LOOKUP_EXCL; + op.mode = mode; } op.open_flag = open_flag; - op.mode = mode; dentry = lookup_open(&nd, file, &op); if (IS_ERR(dentry)) -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown If lookup_fast_for_open() finds a hashed-negative dentry and O_CREAT wasn't requested, it will be returned to open_last_lookups() which will bypass lookup_open() and no call to nfs_atomic_open() will be attempted. However if it doesn't find a hashed dentry, but lookup_open() does find a hashed-negative, then that dentry *will* be passed to ->atomic_open. This is inconsistent and somewhat problematic. When O_CREAT was requested (even if it isn't passed to ->atomic_create due to permission issues), ->atomic_open will have exclusive access to the dentry. When the dentry is d_in_lookup(), ->atomic_open again has exclusive access. The dentry cannot be positive (that case is handled in lookup_open()) but if it is negative but O_CREAT wasn't requested then ->atomic_open will not have exclusive access. NFS currently has code to handle this which should not be necessary. To provide a more consistent context for ->atomic_open handle the hashed-negative-without-O_CREAT case in lookup_open(). Signed-off-by: NeilBrown --- Documentation/filesystems/vfs.rst | 13 +++++-------- fs/namei.c | 4 ++++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index c1f3ea7aad67..d1d32540e85f 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -648,14 +648,11 @@ otherwise noted. needs lookup. Cached positive dentries are still handled by f_op->open(). - If the dentry provided is negative (not in-lookup) and O_CREAT - isn't set, then there is no guarantee of exclusive access to the - dentry - another thread might call ->atomic_open() on the same - dentry at the same time. If needed, a filesystem can ensure this - doesn't happen by returning 0 from ->d_revalidate when that is - called with LOOKUP_OPEN on a negative dentry. This will ensure - that ->atomic_open() only receives an in-lookup dentry, which - always ensures exclusive access. + It is guaranteed that this method has exclusive access to the + dentry. If it is not in-lookup, then O_CREAT was requested + and the parent is locked exclusively. This will be the case + even if O_CREAT isn't still set in open_flag (it may be cleared + if permissions checks show that creation is not permitted). ``tmpfile`` called in the end of O_TMPFILE open(). Optional, equivalent to diff --git a/fs/namei.c b/fs/namei.c index 55ade0626f2f..f2c7a69f4fdd 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4608,6 +4608,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file, if (dentry->d_inode) { /* Cached positive dentry: will open in do_open(). */ goto out; + } else if (!d_in_lookup(dentry) && !(open_flag & O_CREAT)) { + /* Cached negative dentry without O_CREAT */ + error = -ENOENT; + goto out_dput; } if (open_flag & O_CREAT) -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown vfs_lookup_open() must allow: O_LARGEFILE so that large files can be opened. O_NONBLOCK so that break_lease() can be asked to return -EWOULDBLOCK. Also allow O_NOFOLLOW as we don't/can't handle symlinks. In fact we should enforce O_NOFOLLOW for the same reason we enforce __O_REGULAR. Reviewed-by: Jeff Layton Signed-off-by: NeilBrown --- fs/namei.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index f2c7a69f4fdd..d46f157e0cfc 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4750,11 +4750,12 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last, int error = 0; WARN_ONCE(mode & ~S_IALLUGO, "mode must only have permission bits"); - WARN_ONCE(open_flag & ~(O_ACCMODE|O_CREAT|O_EXCL|O_TRUNC|__O_REGULAR), + WARN_ONCE(open_flag & ~(O_ACCMODE|O_CREAT|O_EXCL|O_TRUNC|__O_REGULAR| + O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW), "open_flag has unsupported flags"); mode |= S_IFREG; - open_flag |= __O_REGULAR; + open_flag |= __O_REGULAR | O_NOFOLLOW; error = lookup_noperm_common(last, parent->dentry); if (error) -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown An open() of a regular file can block if there is an active lease on that file, but this can be prevented by opening with O_NONBLOCK. An open(O_CREAT) of a non-existent file can block if there is an active delegation on the parent directory but this CANNOT be prevented with O_NONBLOCK. If nfsd is to use common VFS code for open/create it needs to be able to prevent this blocking. It is conceivable that a user-space application might need this too. So extend O_NONBLOCK protection to not block on a directory delegation when creating a file. Fixes: 134796f43a5e ("vfs: break parent dir delegations in open(..., O_CREAT) codepath") Reviewed-by: Jeff Layton Signed-off-by: NeilBrown --- fs/namei.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index d46f157e0cfc..11e3e1bee05a 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4671,8 +4671,14 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file, } if (create_dir) { - struct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode, - &delegated_inode); + struct dentry *res; + + if (op->open_flag & O_NONBLOCK) + res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode, + NULL); + else + res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode, + &delegated_inode); error = PTR_ERR_OR_ZERO(res); if (!error) { dentry = res; @@ -4684,8 +4690,14 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file, error = -EIO; } } else { - error = vfs_create_no_perm(idmap, dir_inode, dentry, mode, - &delegated_inode); + if (op->open_flag & O_NONBLOCK) + error = vfs_create_no_perm(idmap, dir_inode, dentry, + mode, NULL); + else + error = vfs_create_no_perm(idmap, dir_inode, dentry, + mode, &delegated_inode); + if (error) + goto out_dput; } if (error) goto out_dput; -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown -ENODEV is not generally an error meaning "the object is a device" and nfsd - the only intended caller of vfs_lookup_open() - does not benefit from knowing it was a device file. So return -EFTYPE in that case. Also switch to testing the dentry type rather than dereferencing the inode to get the type. This requires d_is_negative_acquire() as no lock is held. -EISDIR is widely used to mean "the object is a directory which is not what is wanted". -ELOOP is sometimes used elsewhere to mean "a symlink was found but cannot be handled". Reviewed-by: Jeff Layton Signed-off-by: NeilBrown --- fs/namei.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 11e3e1bee05a..45292c152c75 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4746,9 +4746,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file, * determine the type of file found from the error. * -EISDIR : a directory was found * -ELOOP : a symlink was found - * -ENODEV : a block or character device special file was found - * -EFTYPE : any other non-regular file was found, such as FIFO or SOCK. - * or ->atomic_open responded to __O_REGULAR. + * -EFTYPE : any other non-regular file was found, device-special, FIFO or SOCK * * Returns: the opened struct file, or an error. */ @@ -4792,28 +4790,16 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last, if (IS_ERR(dentry)) return ERR_CAST(dentry); - if (d_really_is_negative(dentry)) { + if (d_is_negative_acquire(dentry)) { error = -ENOENT; } else if (!(file->f_mode & FMODE_CREATED) && (open_flag & O_EXCL)) { error = -EEXIST; - } else if ((dentry->d_inode->i_mode & S_IFMT) != S_IFREG) { - switch (dentry->d_inode->i_mode & S_IFMT) { - case S_IFDIR: + } else if (!d_is_reg(dentry)) { + error = -EFTYPE; + if (d_is_dir(dentry)) error = -EISDIR; - break; - case S_IFLNK: + if (d_is_symlink(dentry)) error = -ELOOP; - break; - case S_IFBLK: - case S_IFCHR: - error = -ENODEV; - break; - case S_IFIFO: - case S_IFSOCK: - default: - error = -EFTYPE; - break; - } } else if (!(file->f_mode & FMODE_OPENED)) { nd.path.dentry = dentry; error = vfs_open(&nd.path, file); -- 2.50.0.107.gf914562f5916.dirty From: NeilBrown 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 --- fs/namei.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 45292c152c75..b7fc8af15076 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(mnt_idmap(nd.path.mnt), 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 From: NeilBrown Most access permissions have security implications, but some are for convenience. In particular: r/w/x access for the owner have no security implication as the owner can always change these flags. The different between 'x' and 'r' permission has no security implications. A user with 'r' could find a way to map the file and execute the code, a user with 'x' could execute the file under a debugger and extract the contents. When serving files over the network, the client can provide the convenience of imposing these permissions while the server cannot in general impose them. A server must allow a client to read the content of an execute-only file so that it can be loaded into client memory and executed. A server must allow a client to write to a file that was created without write-permission - at least as long as it remains open after the O_CREAT open which gave it that permission. In this case the creating user will be the owner. nfsd already manages these permission requirements in most cases. However the need to use ->atomic_open interferes with the ability of nfsd to allow extra permissions. To provide complete correctness we need to instruct ->atomic_open to bypass the convenience permissions. Also if core VFS code understands these needs we can achieve more code sharing between nfsd and VFS. So we introduce MAY_SERVE_FILE which instructs permission() functions, and generic_permission() in particular, to allow read-access to execute-only files and to allow all access to an owner. We also add FMODE_SERVE_FILE which is passed to ->atomic_open so these functions can handle when needed. No ->atomic_open functions are changed to make use of it yet. Setting these in vfs_lookup_open() will mean that nfsd gets the permission checking that it requires when it is changed to use vfs_lookup_open(). Signed-off-by: NeilBrown --- Documentation/filesystems/vfs.rst | 6 ++++++ fs/namei.c | 27 ++++++++++++++++++++++++--- include/linux/fs.h | 6 ++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index d1d32540e85f..700d176936ae 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -644,6 +644,12 @@ otherwise noted. FMODE_CREATED should be set if it is possible that this operation created the object. + If atomic_open() performs permission checks, it should + check if FMODE_SERVE_FILE is set in file->f_mode. If it + is then it should assume MAY_SERVE_FILE and not deny access + to an owner, or deny read access to a file for which eXecute + access is available. + This method is only called if the last component is negative or needs lookup. Cached positive dentries are still handled by f_op->open(). diff --git a/fs/namei.c b/fs/namei.c index b7fc8af15076..bab5a979ebc7 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -441,6 +441,11 @@ static int acl_permission_check(const struct mnt_idmap *idmap, unsigned int mode = inode->i_mode; vfsuid_t vfsuid; + if (mask & MAY_SERVE_FILE) { + /* 'x' permission grants 'r' permission */ + mode |= (mode & 0111) << 2; + } + /* * Common cheap case: everybody has the requested * rights, and there are no ACLs to check. No need @@ -466,6 +471,9 @@ static int acl_permission_check(const struct mnt_idmap *idmap, /* Are we the owner? If so, ACL's don't matter */ vfsuid = i_uid_into_vfsuid(idmap, inode); if (likely(vfsuid_eq_kuid(vfsuid, current_fsuid()))) { + if (mask & MAY_SERVE_FILE) + /* Owner always gets access */ + return 0; mask &= 7; mode >>= 6; return (mask & ~mode) ? -EACCES : 0; @@ -474,8 +482,20 @@ static int acl_permission_check(const struct mnt_idmap *idmap, /* Do we have ACL's? */ if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { int error = check_acl(idmap, inode, mask); - if (error != -EAGAIN) - return error; + if (error != -EAGAIN) { + if (likely(error != -EACCES || + !(mask & MAY_SERVE_FILE))) + return error; + if (mask & MAY_READ) + /* + * eXec permission allows us to Read for + * file serving. + */ + error = check_acl(idmap, inode, + (mask & ~MAY_READ) | MAY_EXEC); + if (error != -EAGAIN) + return error; + } } /* Only RWX matters for group/other mode bits */ @@ -4777,6 +4797,7 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last, file = alloc_empty_file(open_flag, current_cred()); if (IS_ERR(file)) return file; + file->f_mode |= FMODE_SERVE_FILE; nd.path = *parent; nd.last = *last; @@ -4791,7 +4812,7 @@ 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);; + op.acc_mode = ACC_MODE(open_flag) | MAY_SERVE_FILE; if (open_flag & O_TRUNC) op.acc_mode |= MAY_WRITE; dentry = lookup_open(&nd, file, &op); diff --git a/include/linux/fs.h b/include/linux/fs.h index 79c376d4bc57..4337a77d051f 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -99,6 +99,8 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, #define MAY_CHDIR 0x00000040 /* called from RCU mode, don't block */ #define MAY_NOT_BLOCK 0x00000080 +/* Passed by nfsd - owner may have any access, X access gives R */ +#define MAY_SERVE_FILE 0x00000100 /* * flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond @@ -121,8 +123,8 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, #define FMODE_WRITE_RESTRICTED ((__force fmode_t)(1 << 6)) /* File supports atomic writes */ #define FMODE_CAN_ATOMIC_WRITE ((__force fmode_t)(1 << 7)) - -/* FMODE_* bit 8 */ +/* File used to serve file to a client, so MAY_SERVE_FILE applies */ +#define FMODE_SERVE_FILE ((__force fmode_t)(1 << 8)) /* 32bit hashes as llseek() offset (for directories) */ #define FMODE_32BITHASH ((__force fmode_t)(1 << 9)) -- 2.50.0.107.gf914562f5916.dirty