Technical Root Cause Analysis
1. Involved Files, Functions, Structs, and Fields
- Source Files:
fs/kernfs/inode.c: contains kernfs_iop_getattr(), kernfs_refresh_inode(), and kernfs_iop_permission().
fs/open.c: contains do_dentry_open().
- Structures and Fields:
struct inode::i_mode (umode_t, 16-bit unsigned short): the VFS file mode and permissions.
struct kernfs_node::mode (umode_t): the kernfs nodeβs persistent mode representation.
struct kernfs_root::kernfs_iattr_rwsem (struct rw_semaphore): the per-kernfs-tree read-write semaphore used for attribute protection in kernfs.
2. Bug Mechanism and Invariant Violation
In kernfs (used by sysfs, cgroupfs, etc.), the filesystem tree and file attributes exist independently of whether a VFS struct inode is currently allocated. The authoritative metadata is tracked in struct kernfs_node (kn->mode, and dynamically allocated kn->iattr for UID, GID, and timestamps).
When a VFS inode exists for a kernfs_node, kernfs lazily synchronizes attributes from kn to struct inode in several operation callbacks:
Specifically, in kernfs_iop_getattr():
int kernfs_iop_getattr(const struct mnt_idmap *idmap,
const struct path *path, struct kstat *stat,
u32 request_mask, unsigned int query_flags)
{
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
struct kernfs_root *root = kernfs_root(kn);
down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
up_read(&root->kernfs_iattr_rwsem);
return 0;
}
And in kernfs_refresh_inode() (fs/kernfs/inode.c:165-180):
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
inode->i_mode = kn->mode;
attrs = kernfs_iattrs_noalloc(kn);
...
This violates kernel concurrency expectations in two ways:
- Unsynchronized mutation of live VFS inode fields: In generic VFS,
inode->i_mode is normally modified under inode_lock(inode) (e.g., via notify_change() -> setattr_copy()). During standard read-only operations such as stat(2), VFS assumes the inode is not being actively mutated. kernfs_refresh_inode() unconditionally performs a plain store inode->i_mode = kn->mode on every stat invocation, even when kn->mode hasn't changed.
- Missing reader/writer locking contract with VFS consumers: Generic VFS operations such as
do_dentry_open() (fs/open.c:968 and fs/open.c:960) read inode->i_mode without acquiring kernfs_iattr_rwsem (a kernfs-internal rwsem unknown to the VFS core):
if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
f->f_mode |= FMODE_ATOMIC_POS;
Furthermore, kernfs_iop_getattr() only holds kernfs_iattr_rwsem as a reader (down_read), which permits multiple threads running getattr concurrently to race writing inode->i_mode at the same time.
3. Trigger Path and Failure Manifestation
When:
- Task 1 (CPU 0) performs a
fstat() / stat() syscall on a kernfs file:
vfs_fstat() -> vfs_getattr() -> vfs_getattr_nosec() -> kernfs_iop_getattr() -> kernfs_refresh_inode() performs an unmarked 16-bit store to inode->i_mode (fs/kernfs/inode.c:169).
- Task 2 (CPU 1, e.g.
udevd) concurrently opens the same file:
sys_openat() -> do_file_open() -> path_openat() -> vfs_open_consume() -> do_dentry_open() performs an unmarked 16-bit read from inode->i_mode (fs/open.c:968).
Because neither access uses atomic primitives or shared locking, KCSAN detects the simultaneous load and store to 0xffff888106dceb18 (&inode->i_mode) and reports a data race.