]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
NFSv4: Handle case where the lookup of a directory fails
authorTrond Myklebust <trond.myklebust@hammerspace.com>
Thu, 6 Jan 2022 23:24:02 +0000 (18:24 -0500)
committerPaolo Pisati <paolo.pisati@canonical.com>
Thu, 3 Feb 2022 09:28:50 +0000 (10:28 +0100)
BugLink: https://bugs.launchpad.net/bugs/1959879
commit ac795161c93699d600db16c1a8cc23a65a1eceaf upstream.

If the application sets the O_DIRECTORY flag, and tries to open a
regular file, nfs_atomic_open() will punt to doing a regular lookup.
If the server then returns a regular file, we will happily return a
file descriptor with uninitialised open state.

The fix is to return the expected ENOTDIR error in these cases.

Reported-by: Lyu Tao <tao.lyu@epfl.ch>
Fixes: 0dd2b474d0b6 ("nfs: implement i_op->atomic_open()")
Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
fs/nfs/dir.c

index 5b68c44848caf5f2294f6dfcb8af57b98b06df1b..a78162238988ee46b158c4b962e29b7c93c5bbb7 100644 (file)
@@ -1982,6 +1982,19 @@ out:
 
 no_open:
        res = nfs_lookup(dir, dentry, lookup_flags);
+       if (!res) {
+               inode = d_inode(dentry);
+               if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
+                   !S_ISDIR(inode->i_mode))
+                       res = ERR_PTR(-ENOTDIR);
+       } else if (!IS_ERR(res)) {
+               inode = d_inode(res);
+               if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
+                   !S_ISDIR(inode->i_mode)) {
+                       dput(res);
+                       res = ERR_PTR(-ENOTDIR);
+               }
+       }
        if (switched) {
                d_lookup_done(dentry);
                if (!res)