nfs: fix nfs_access_calc_mask() corner cases
Subsystems (2)
PATCH 1/1 nfs: fix nfs_access_calc_mask() corner cases
The special case for special files (neither regular file nor directory) is inconsistent with the perms requested in nfs_do_access() and the MAY_WRITE condition seems incorrect (checks just access_result & NFS_MAY_WRITE instead of (access_result & NFS_MAY_WRITE) == NFS_MAY_WRITE as is the pattern for the other cases).
Since nfs_access_calc_mask() requests the same access bits for regular and special files, we end up with nfs_access_calc_mask() that treats both the same as well.
Notably, this fixes a corner case inconsistency between NFS and classic filesystems when calling access("...", X_OK) on special files (i.e. fifo, block device, and character device; symlink is special here). On classic filesystems the result of that call depends on the UNIX permissions of the file, while on NFS it would always fail. See a test script below that demonstrates this difference:
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
systemctl start nfs-server
exportfs -o rw,no_root_squash,fsid=0 "localhost:$tmpdir" mount -t nfs localhost:/ /mnt
mkfifo "$tmpdir/fifo"
mknod "$tmpdir/chrdev" c 1 3
mknod "$tmpdir/blkdev" b 7 0
for f in fifo chrdev blkdev; do
python3 -c "import os; print(os.access('$tmpdir/$f', os.X_OK))"
done
for f in fifo chrdev blkdev; do
python3 -c "import os; print(os.access('/mnt/$f', os.X_OK))"
done
umount /mnt
chmod u+x "$tmpdir/"*
mount -t nfs localhost:/ /mnt
for f in fifo chrdev blkdev; do
python3 -c "import os; print(os.access('$tmpdir/$f', os.X_OK))"
done
for f in fifo chrdev blkdev; do
python3 -c "import os; print(os.access('/mnt/$f', os.X_OK))"
done
umount /mnt
exportfs -u "localhost:$tmpdir"
Note that on some Fedora/RHEL systems you may need to also do setenforce 0 before running the script, because the SELinux policy could block the execute permission for the special files on the server side - this bug will be fixed independently in the policy.
--- fs/nfs/dir.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
fs/nfs/dir.c +5 -7
@@ -3250,12 +3250,11 @@ void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,EXPORT_SYMBOL_GPL (nfs_access_add_cache);#define NFS_MAY_READ (NFS_ACCESS_READ) -#define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ - NFS_ACCESS_EXTEND | \ - NFS_ACCESS_DELETE)#define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ NFS_ACCESS_EXTEND) -#define NFS_DIR_MAY_WRITE NFS_MAY_WRITE +#define NFS_DIR_MAY_WRITE (NFS_ACCESS_MODIFY | \ + NFS_ACCESS_EXTEND | \ + NFS_ACCESS_DELETE)#define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)#define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) static int
@@ -3270,13 +3269,12 @@ nfs_access_calc_mask(u32 access_result, umode_t umode) mask |=MAY_WRITE ;if ((access_result &NFS_MAY_LOOKUP ) ==NFS_MAY_LOOKUP ) mask |=MAY_EXEC ; - }else if (S_ISREG (umode)) { + }else {if ((access_result &NFS_FILE_MAY_WRITE ) ==NFS_FILE_MAY_WRITE ) mask |=MAY_WRITE ;if ((access_result &NFS_MAY_EXECUTE ) ==NFS_MAY_EXECUTE ) mask |=MAY_EXEC ; - }else if (access_result &NFS_MAY_WRITE ) - mask |=MAY_WRITE ; + }return mask; }
Signature · 1 line
2.54.0
Quoted 52 lines · Ondrej Mosnacek
The special case for special files (neither regular file nor directory) is inconsistent with the perms requested in
nfs_do_access()and theMAY_WRITEcondition seems incorrect (checks justaccess_result & NFS_MAY_WRITEinstead of(access_result & NFS_MAY_WRITE) == NFS_MAY_WRITEas is the pattern for the other cases).Since
nfs_access_calc_mask()requests the same access bits for regular and special files, we end up withnfs_access_calc_mask()that treats both the same as well.Notably, this fixes a corner case inconsistency between NFS and classic filesystems when calling
access("...", X_OK)on special files (i.e. fifo, block device, and character device; symlink is special here). On classic filesystems the result of that call depends on the UNIX permissions of the file, while on NFS it would always fail. See a test script below that demonstrates this difference:tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXITsystemctl start nfs-server
exportfs -o rw,no_root_squash,fsid=0 "localhost:$tmpdir" mount -t nfs localhost:/ /mntmkfifo "$tmpdir/fifo"
mknod "$tmpdir/chrdev" c 1 3
mknod "$tmpdir/blkdev" b 7 0for f in fifo chrdev blkdev; do
python3 -c "import os;print(os.access('$tmpdir/$f', os.X_OK))"
donefor f in fifo chrdev blkdev; do
python3 -c "import os;print(os.access('/mnt/$f', os.X_OK))"
doneumount /mnt
chmod u+x "$tmpdir/"*
mount -t nfs localhost:/ /mntfor f in fifo chrdev blkdev; do
python3 -c "import os;print(os.access('$tmpdir/$f', os.X_OK))"
donefor f in fifo chrdev blkdev; do
python3 -c "import os;print(os.access('/mnt/$f', os.X_OK))"
doneumount /mnt
exportfs -u "localhost:$tmpdir"
Quoted 7 lines
Note that on some Fedora/RHEL systems you may need to also do
setenforce 0before running the script, because the SELinux policy could block the execute permission for the special files on the server side - this bug will be fixed independently in the policy.Fixes ecbb903c5674 ("NFS: Be more careful about mapping file permissions")Signed-off-by Ondrej Mosnacek <[email protected]>
I can see that the patch has been marked as "Not Applicable" in Patchwork [1], but I can't see any reply explaining why. Did it happen by mistake? Or otherwise what's wrong with the patch?
[1] https://patchwork.kernel.org/project/linux-nfs/patch/[email protected]/
Signature · 3 lines
Ondrej Mosnacek Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.