[PATCH RFC v3 52/90] kernfs: let a filesystem refuse a change to a node

From: Josef Bacik

Date: Thu Nov 05 2026 - 10:28:56 EST


Whether a node of a kernfs based filesystem may be changed is decided by
its mode and owner, in the VFS and in kernfs. The filesystem on top of
kernfs sees some of the changes, such as a write to a file or a mkdir. It
never sees others: chmod, chown, a timestamp update, an extended
attribute. If it has a rule that is based on something other than mode and
owner, it can only enforce that rule for the first kind.

Add a may_change() callback to kernfs_syscall_ops. It is called in
addition to the mode check, everywhere a change can come from:

- when write permission is checked, which covers opening a file for
writing and creating, removing or renaming a directory entry
- on every write to a file, because the writer may not be the task that
opened it
- before an attribute or an extended attribute is set or removed

The callback is told whether the change is to the content of the node or
to its attributes. A filesystem may want to let a task create entries in a
directory without letting it change the mode of that directory.

Truncating a file counts as a change to its content. That is what an open
with O_TRUNC does, which every shell redirection uses, and kernfs ignores
the size anyway.

No filesystem implements the callback yet.

Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
---
fs/kernfs/file.c | 5 +++++
fs/kernfs/inode.c | 25 +++++++++++++++++++++++++
fs/kernfs/kernfs-internal.h | 11 +++++++++++
include/linux/kernfs.h | 15 +++++++++++++++
4 files changed, 56 insertions(+)

diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index cca9f83fc9b5..27e234c4aea0 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -307,6 +307,11 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
ssize_t len = iov_iter_count(iter);
const struct kernfs_ops *ops;
char *buf;
+ int ret;
+
+ ret = kernfs_may_change(of->kn, KERNFS_CHANGE_CONTENT);
+ if (ret)
+ return ret;

if (of->atomic_write_len) {
if (len > of->atomic_write_len)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 6630b29d7c07..efefd40161d6 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -112,12 +112,21 @@ int kernfs_iop_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
{
struct inode *inode = d_inode(dentry);
struct kernfs_node *kn = inode->i_private;
+ enum kernfs_change what = KERNFS_CHANGE_ATTRS;
struct kernfs_root *root;
int error;

if (!kn)
return -EINVAL;

+ /* Truncating a file is writing to it, which is what O_TRUNC is for. */
+ if ((iattr->ia_valid & ATTR_SIZE) &&
+ !(iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
+ what = KERNFS_CHANGE_CONTENT;
+ error = kernfs_may_change(kn, what);
+ if (error)
+ return error;
+
root = kernfs_root(kn);
down_write(&root->kernfs_iattr_rwsem);
error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
@@ -283,6 +292,12 @@ int kernfs_iop_permission(const struct mnt_idmap *idmap,
kn = inode->i_private;
root = kernfs_root(kn);

+ if (mask & MAY_WRITE) {
+ ret = kernfs_may_change(kn, KERNFS_CHANGE_CONTENT);
+ if (ret)
+ return ret;
+ }
+
down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
ret = generic_permission(&nop_mnt_idmap, inode, mask);
@@ -349,6 +364,11 @@ static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
{
const char *name = xattr_full_name(handler, suffix);
struct kernfs_node *kn = inode->i_private;
+ int ret;
+
+ ret = kernfs_may_change(kn, KERNFS_CHANGE_ATTRS);
+ if (ret)
+ return ret;

return kernfs_xattr_set(kn, name, value, size, flags);
}
@@ -362,10 +382,15 @@ static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
const char *full_name = xattr_full_name(handler, suffix);
struct kernfs_node *kn = inode->i_private;
struct kernfs_iattrs *attrs;
+ int ret;

if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
return -EOPNOTSUPP;

+ ret = kernfs_may_change(kn, KERNFS_CHANGE_ATTRS);
+ if (ret)
+ return ret;
+
attrs = kernfs_iattrs(kn);
if (!attrs)
return -ENOMEM;
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index f1e93b09e27d..5d1d4e282556 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -127,6 +127,17 @@ static inline const char *kernfs_rcu_name(const struct kernfs_node *kn)
kernfs_rename_is_locked(kn));
}

+/* See kernfs_syscall_ops::may_change. */
+static inline int kernfs_may_change(struct kernfs_node *kn,
+ enum kernfs_change what)
+{
+ struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
+
+ if (scops && scops->may_change)
+ return scops->may_change(kn, what);
+ return 0;
+}
+
static inline struct kernfs_node *kernfs_parent(const struct kernfs_node *kn)
{
/*
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 73786b567ccd..62bdb77961e3 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -243,6 +243,12 @@ struct kernfs_node {
* the exact operation requested. An active reference is held for each
* kernfs_node parameter.
*/
+/* What kernfs_syscall_ops::may_change is asked about. */
+enum kernfs_change {
+ KERNFS_CHANGE_CONTENT, /* what a file holds, size included, the entries of a directory */
+ KERNFS_CHANGE_ATTRS, /* mode, owner, times, extended attributes */
+};
+
struct kernfs_syscall_ops {
int (*show_options)(struct seq_file *sf, struct kernfs_root *root);

@@ -253,6 +259,15 @@ struct kernfs_syscall_ops {
const char *new_name);
int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
struct kernfs_root *root);
+ /*
+ * May the caller change @kn? Asked on top of what the mode says, and
+ * before it: when permission to write is asked for, which is what
+ * opening a file for writing and making, removing or renaming an entry
+ * of a directory take, for every write to a file, whoever opened it, and
+ * before an attribute or an extended attribute is set or removed. No
+ * active reference is held on @kn. Returns 0 or a negative error.
+ */
+ int (*may_change)(struct kernfs_node *kn, enum kernfs_change what);
};

struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root);

--
2.55.0