Root Cause Analysis
1. Functions, Structs, and Files Involved
2. Detailed Technical Mechanism
Synchronization Architecture in SELinux Inode Handling
In the SELinux security module, struct inode_security_struct (isec) tracks the security context and security class for each inode. The struct contains a spinlock (isec->lock). By design for VFS fast-path performance, this spinlock is only acquired when creating, revalidating, or modifying an inode's label (e.g., in inode_doinit_with_dentry(), selinux_inode_post_setxattr(), selinux_inode_setsecurity(), and selinux_task_to_inode()).
Standard permission checking fast pathsβsuch as inode_has_perm(), inode_sid_has_perm(), selinux_inode_permission(), and ioctl_has_perm()βread isec->sid and isec->sclass without taking isec->lock once isec->initialized is set to LABEL_INITIALIZED. Specifically:
inode_security(inode) and backing_inode_security(dentry) perform an opportunistic lockless check:
if (data_race(likely(isec->initialized == LABEL_INITIALIZED)))
return isec;
- After retrieving
isec, ioctl_has_perm() reads isec->sid and isec->sclass directly:
isec = inode_security(inode);
rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, requested,
driver, AVC_EXT_IOCTL, xperm, &ad);
The Data Race Trigger
When userspace modifies the security.selinux extended attribute of an inode (via syscalls like setxattr or lsetxattr), selinux_inode_post_setxattr() is invoked:
isec = backing_inode_security(dentry);
spin_lock(&isec->lock);
isec->sclass = inode_mode_to_security_class(inode->i_mode);
isec->sid = newsid;
isec->initialized = LABEL_INITIALIZED;
spin_unlock(&isec->lock);
-
Unsynchronized Store vs. Lockless Reader:
While selinux_inode_post_setxattr() takes spin_lock(&isec->lock), concurrent readers such as ioctl_has_perm() do not hold isec->lock. The write isec->sclass = inode_mode_to_security_class(inode->i_mode) is a plain C store without WRITE_ONCE(), and the read in ioctl_has_perm() is a plain C load without READ_ONCE(). This forms an unsynchronized concurrent read/write under the Linux Kernel Memory Model (LKMM), which KCSAN detects and reports.
-
Redundant Store to isec->sclass:
backing_inode_security(dentry) at line 3527 has already ensured that isec was initialized (setting isec->sclass via inode_doinit_with_dentry()). Under POSIX and Linux VFS semantics, an inode's file mode type (inode->i_mode & S_IFMT) is immutable for the lifetime of the inode; updating an extended attribute cannot transform a regular file into a directory, socket, or device node. Therefore, rewriting isec->sclass in selinux_inode_post_setxattr() writes the exact value that isec->sclass already holds. Performing this plain write against live inodes unnecessarily exposes concurrent readers to a data race.