[PATCH 8/8] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE
From: NeilBrown
Date: Wed Dec 02 2026 - 05:19:12 EST
From: NeilBrown <neil@xxxxxxxxxx>
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 <neil@xxxxxxxxxx>
---
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 30e2809265ce..6550be0464d6 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