kernfs: three standalone fixes
Subsystems (4)
v2 cover kernfs: three standalone fixes
Three independent kernfs fixes, plus selftest coverage for the paths they touch. The fixes do not depend on each other.
Patch 1 adds tests for decoding a file handle and for a rename that keeps the same parent. Patches 2-4:
- kernfs_rename_ns() takes kernfs_rename_lock only when the parent changes, so two same-parent renames inside one kernfs_path_from_node() walk can build a path that never existed. sysfs_warn_dup() is the only caller that can see it.
- __kernfs_fh_to_dentry() creates inodes with no lock, so a decode racing rmdir() can hash an inode after the removal's ilookup() pass. The inode keeps i_nlink 1, so no IN_DELETE_SELF is sent.
- kernfs_create_link() reads the target's uid and gid unlocked, so a chown in between gives the link an owner the target never had.
Patch 1 applies on top of the patch [1] ("selftests: cover kernfs dentry revalidation") with vfs-7.4.kernfs branch in vfs tree as base.
[1] http://lore.kernel.org/[email protected]
Changes since v1:
http://lore.kernel.org/[email protected]
- Added cover letter
- Used approppriate tags
- Updated commit messages to be more concise
- Replaced data_race() with READ_ONCE() (TJ)
Shakeel Butt (4):
selftests: cover kernfs file handles and same-parent rename
kernfs: take kernfs_rename_lock for same-parent renames too
kernfs: don't lose IN_DELETE_SELF when decoding a file handle
kernfs: fix up the unlocked attribute reads on the creation paths
fs/kernfs/dir.c | 40 ++- fs/kernfs/kernfs-internal.h | 9 +- fs/kernfs/mount.c | 32 +- fs/kernfs/symlink.c | 17 +- tools/testing/selftests/filesystems/config | 1 + .../selftests/filesystems/kernfs_test.c | 296 +++++++++++++++++- 6 files changed, 362 insertions(+), 33 deletions(-)
base-commit: 47fc64fb3b433abb9f2242a85ba808ac6f87df22
Signature · 1 line
2.53.0-Meta
PATCH v2 1/4 selftests: cover kernfs file handles and same-parent rename
The kernfs tests only reach kernfs through lookup and readdir. Two paths are not covered: file handles, which find a node without a lookup through its parent, and rename of a node that keeps its parent.
Add three tests:
- decode a file handle, live and after the node is gone;
- decode while the node is being removed;
- look up a name while an interface is renamed, which renames its /sys/class/net entry with the parent unchanged.
Two small fixes while here. ns_tag_isolates_class_net now counts only symlinks, because bonding adds a bonding_masters attribute that is not a device. A failing mkdtemp() now skips instead of aborting.
--- tools/testing/selftests/filesystems/config | 1 + .../selftests/filesystems/kernfs_test.c | 296 +++++++++++++++++- 2 files changed, 295 insertions(+), 2 deletions(-)
tools/testing/selftests/filesystems/config +1 -0
@@ -1,5 +1,6 @@ CONFIG_CGROUPS=y CONFIG_CGROUP_PIDS=y +CONFIG_FHANDLE=y CONFIG_NAMESPACES=y CONFIG_NET=y CONFIG_NET_NS=y
tools/testing/selftests/filesystems/kernfs_test.c +294 -2
324 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v2 2/4 kernfs: take kernfs_rename_lock for same-parent renames too
In reply to Shakeel Buttkernfs_rename_ns() takes kernfs_rename_lock only when the rename moves the node to a new parent. A rename that keeps the parent, like renaming a network interface, changes kernfs_node::name under kernfs_rwsem alone. So the lock covers ->__parent but not ->name, and a reader that wants a stable name has to take kernfs_rwsem, the lock every lookup needs.
It is also a real bug. kernfs_path_from_node() holds the lock for reading and reads each ancestor's name once. One rename only moves the answer from the old path to the new one, but two renames inside one walk build a path that never existed:
CPU0 CPU1
kernfs_path_from_node() on /a/b/c
reads the name of a, gets "a"
renames a to a2 renames b to b2
reads the name of b, gets "b2"
returns "/a/b2/c"
Only sysfs can hit this: sysfs_warn_dup() is the one caller on a root without KERNFS_ROOT_INVARIANT_PARENT. The rest are cgroup, which sets the flag, so it skips the lock and reads names under RCU alone. That case needs something else and is left alone here.
So take the lock for both kinds of rename, and let kernfs_rcu_name() accept it, like kernfs_parent() already does for ->__parent. Renames are rare, the lock is per filesystem, and the locked section is at most three stores. It also gives a future rename counter one place to sit.
--- fs/kernfs/dir.c | 28 +++++++++++++++------------- fs/kernfs/kernfs-internal.h | 9 ++++++++- 2 files changed, 23 insertions(+), 14 deletions(-)
fs/kernfs/dir.c +15 -13
@@ -1808,6 +1808,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,struct kernfs_node *old_parent;struct kernfs_root *root;const char *old_name; +bool reparent;int error;/* can't move or rename root */
@@ -1857,25 +1858,26 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,*/ kernfs_unlink_sibling (kn); -/* rename_lock protects ->parent accessors */ -if (old_parent != new_parent) { + reparent = old_parent != new_parent; +if (reparent)kernfs_get (new_parent); -write_lock_irq (&root->kernfs_rename_lock); +/* +* kernfs_rename_lock protects ->__parent, ->ns and ->name, so take it +* even when the parent does not change. +*/ +write_lock_irq (&root->kernfs_rename_lock); + +if (reparent)rcu_assign_pointer (kn->__parent, new_parent); +WRITE_ONCE (kn->ns, new_ns); +if (new_name) +rcu_assign_pointer (kn->name, new_name); -WRITE_ONCE (kn->ns, new_ns); -if (new_name) -rcu_assign_pointer (kn->name, new_name); +write_unlock_irq (&root->kernfs_rename_lock); -write_unlock_irq (&root->kernfs_rename_lock); +if (reparent)kernfs_put (old_parent); - }else { -/* name assignment is RCU protected, parent is the same */ -WRITE_ONCE (kn->ns, new_ns); -if (new_name) -rcu_assign_pointer (kn->name, new_name); - } kn->hash =kernfs_name_hash (new_name ?: old_name, kn->ns);kernfs_link_sibling (kn);
fs/kernfs/kernfs-internal.h +8 -1
@@ -117,7 +117,14 @@ static inline bool kernfs_rename_is_locked(const struct kernfs_node *kn)static inline const char *kernfs_rcu_name (const struct kernfs_node *kn) { -return rcu_dereference_check (kn->name,kernfs_root_is_locked (kn)); +/* +* Like kernfs_node::__parent below, the name is only replaced under +* both kernfs_root::kernfs_rwsem and kernfs_root::kernfs_rename_lock, +* so either one keeps it, and the string it points at, stable. +*/ +return rcu_dereference_check (kn->name, +kernfs_root_is_locked (kn) || +kernfs_rename_is_locked (kn)); }static inline struct kernfs_node *kernfs_parent (const struct kernfs_node *kn)
Signature · 1 line
2.53.0-Meta
PATCH v2 3/4 kernfs: don't lose IN_DELETE_SELF when decoding a file handle
In reply to Shakeel Butt__kernfs_remove() clears i_nlink on a node's inodes and finds them with ilookup(). A decode that has pinned the node but not yet hashed its inode is invisible to that pass:
CPU0 CPU1
open_by_handle_at()
kernfs_find_and_get_node_by_id()
pins the node, still active
rmdir()
marks the subtree removing ilookup() finds no inode
kernfs_get_inode()
hashes an inode with i_nlink 1
Nothing fixes it later: kernfs_refresh_inode() never touches i_nlink for a file and skips it for a directory being removed. The inode keeps the 1 it was born with, and dentry_unlink_inode() sends IN_DELETE_SELF only at 0, so a watcher never learns the node went away.
The other callers of kernfs_get_inode() are safe: those in fs/kernfs hold kernfs_rwsem, and cgroup_may_write() is covered by cgroup_mutex, which cgroup_destroy_locked() holds across kernfs_remove(). __kernfs_fh_to_dentry() has held nothing since exportfs support was added.
Take kernfs_rwsem for reading, as ->get_parent already does, and cover the lookup as well as kernfs_get_inode(). __kernfs_remove() deactivates the whole subtree under the write lock, so under the read lock either the lookup refuses the node, or the inode is hashed before the ilookup() pass runs. The same holds for ->fh_to_parent, since a node cannot be active while an ancestor is being removed.
Reproduced with a 300ms delay between the lookup and kernfs_get_inode(), decoding a handle for a file in a cgroup directory while another task rmdir()s it: st_nlink is 1 without this patch and 0 with it.
->get_parent still has a window of its own. It takes the same lock but has no active check, so reconnect_path() can build an inode for an ancestor that is already gone. That needs the active test rather than a lock, and changes what ->get_parent returns, so it is left to the series that reworks these paths.
--- fs/kernfs/mount.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)
fs/kernfs/mount.c +21 -11
@@ -124,22 +124,32 @@ static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,return NULL ; } - kn =kernfs_find_and_get_node_by_id (info->root, id); -if (!kn) -return ERR_PTR (-ESTALE ); +/* +* Hold kernfs_rwsem across the lookup as well as kernfs_get_inode(). +* __kernfs_remove() deactivates the subtree and clears i_nlink on its +* inodes under the write lock, so under the read lock either +* kernfs_find_and_get_node_by_id() refuses the node, or the inode is +* in the inode hash before the ilookup() pass goes looking for it. +*/ + scoped_guard(rwsem_read, &info->root->kernfs_rwsem) { + kn =kernfs_find_and_get_node_by_id (info->root, id); +if (!kn) +return ERR_PTR (-ESTALE ); -if (get_parent) { -struct kernfs_node *parent; +if (get_parent) { +struct kernfs_node *parent; - parent =kernfs_get_parent (kn); + parent =kernfs_get_parent (kn); +kernfs_put (kn); + kn = parent; +if (!kn) +return ERR_PTR (-ESTALE ); + } + + inode =kernfs_get_inode (sb, kn);kernfs_put (kn); - kn = parent; -if (!kn) -return ERR_PTR (-ESTALE ); } - inode =kernfs_get_inode (sb, kn); -kernfs_put (kn);return d_obtain_alias (inode); }
Signature · 1 line
2.53.0-Meta
PATCH v2 4/4 kernfs: fix up the unlocked attribute reads on the creation paths
In reply to Shakeel ButtTwo creation paths read a live node's attributes without holding kernfs_iattr_rwsem, which kernfs_iop_setattr() takes for writing. They need different fixes.
kernfs_create_link() copies the target's ia_uid and then its ia_gid into the new link. A chown of the target between the two reads leaves the link with the old uid and the new gid, an owner the target never had. Read both under the rwsem.
kernfs_new_node() reads the parent's mode and ia_gid for S_ISGID inheritance. Either value is fine there: the node does not exist yet, so nothing orders a racing chmod or chown against the creation. Taking the rwsem would only pick between two answers that are both right. Mark the reads with READ_ONCE() instead.
The pointer that leads to them is already fine: __kernfs_iattrs() publishes kernfs_node::iattr with try_cmpxchg(), and both sides read it with READ_ONCE(), like the rest of fs/kernfs.
The Fixes tag is for the symlink half. kernfs_create_link() has read the pair unlocked since it started copying the target's owner; only the name of the lock its writer takes has changed. The READ_ONCE() markings are not a fix.
--- fs/kernfs/dir.c | 12 +++++++++--- fs/kernfs/symlink.c | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-)
fs/kernfs/dir.c +9 -3
@@ -736,13 +736,19 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, {struct kernfs_node *kn; -if (parent->mode &S_ISGID ) { +/* +* The mode and the gid below are read unlocked on purpose: they feed +* a node that does not exist yet, so nothing orders a racing chmod or +* chown against this creation. +*/ +if (READ_ONCE (parent->mode) &S_ISGID ) {/* this code block imitates inode_init_owner() for * kernfs */ +struct kernfs_iattrs *attrs =READ_ONCE (parent->iattr); -if (parent->iattr) - gid = parent->iattr->ia_gid; +if (attrs) + gid =READ_ONCE (attrs->ia_gid);if (flags &KERNFS_DIR ) mode |=S_ISGID ;
fs/kernfs/symlink.c +14 -3
@@ -31,9 +31,20 @@ struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,kuid_t uid =GLOBAL_ROOT_UID ;kgid_t gid =GLOBAL_ROOT_GID ; -if (target->iattr) { - uid = target->iattr->ia_uid; - gid = target->iattr->ia_gid; +/* +* A symlink takes its owner from its target, so both fields have to +* come from the same moment: read them under kernfs_iattr_rwsem, or +* a chown of the target racing this could leave the link with the +* old uid and the new gid. The section ends before kernfs_add_one() +* takes kernfs_rwsem. +*/ + scoped_guard(rwsem_read, &kernfs_root(target)->kernfs_iattr_rwsem) { +struct kernfs_iattrs *attrs =READ_ONCE (target->iattr); + +if (attrs) { + uid = attrs->ia_uid; + gid = attrs->ia_gid; + } } kn =kernfs_new_node (parent, name,S_IFLNK |0777 , uid, gid,KERNFS_LINK );
Signature · 1 line
2.53.0-Meta
Christian Brauner
AppliedQuoted 9 lines · Shakeel Butt
kernfs: three standalone fixes
Three independent kernfs fixes, plus selftest coverage for the paths they touch. The fixes do not depend on each other.
Patch 1 adds tests for decoding a file handle and for a rename that keeps the same parent. Patches 2-4:
[...]
Applied to the vfs-7.4.kernfs branch of the vfs/vfs.git tree. Patches in the vfs-7.4.kernfs branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.4.kernfs
[1/4] selftests: cover kernfs file handles and same-parent rename
https://git.kernel.org/vfs/vfs/c/60da22a38b01 [2/4] kernfs: take kernfs_rename_lock for same-parent renames too https://git.kernel.org/vfs/vfs/c/6034974e3aec [3/4] kernfs: don't lose IN_DELETE_SELF when decoding a file handle https://git.kernel.org/vfs/vfs/c/dc1a0cc95737 [4/4] kernfs: fix up the unlocked attribute reads on the creation paths https://git.kernel.org/vfs/vfs/c/81bdc85b4c08