[PATCH] selinux: read the inode SID once in selinux_inode_permission()

From: Jiayi Zhang

Date: Sat Dec 05 2026 - 03:40:31 EST


selinux_inode_permission() reads isec->sid without isec->lock. On an
AVD cache miss it reads it three times: in task_avdcache_search(), for
avc_has_perm_noaudit(), and in task_avdcache_update(), which stores it
as the key of the new cache entry. If the SID changes between the
second and the third read, the new entry pairs the new SID with the
decision computed for the old SID.

The SID of an inode in use changes when selinux_task_to_inode() stores
the task's current SID in its /proc/<pid> inodes, which pid_revalidate()
does on every lookup; when a directory is relabeled through setxattr;
and when an invalidated label is reloaded. For example, task A walks
through /proc/<pid> of task B while B executes a program with a domain
transition, and a lookup of /proc/<pid> by another thread stores B's
new SID between A's two reads. A's cache then holds the decision for
B's old SID under B's new SID. /proc/<pid> is a directory, the only
class the cache holds, so A's later checks on directories with B's new
SID use that decision until newer entries replace it, the policy
sequence number changes, or A's own SID changes. Children that A forks
in the meantime inherit the entry.

KCSAN reports the unlocked accesses:

BUG: KCSAN: data-race in selinux_inode_permission / selinux_task_to_inode

write to 0xffff88810d82b7d4 of 4 bytes by task 4030 on cpu 0:
selinux_task_to_inode+0x160/0x1a0 security/selinux/hooks.c:4740
security_task_to_inode+0x39/0x50 security/security.c:3374
pid_update_inode fs/proc/base.c:2035 [inline]
pid_revalidate+0x82/0xb0 fs/proc/base.c:2057
...

read to 0xffff88810d82b7d4 of 4 bytes by task 4029 on cpu 1:
selinux_inode_permission+0x3e6/0x740 security/selinux/hooks.c
security_inode_permission+0x73/0xb0 security/security.c:1865
inode_permission+0x1e3/0x3c0 fs/namei.c:661
...

Read isec->sid and isec->sclass once, with READ_ONCE(), and pass the
values to the cache search, the AVC check, the cache update and the
audit record. Mark the stores of the SID and the class on inodes that
may be in use with WRITE_ONCE(). The two values do not need to be read
together under isec->lock: only directories are cached, and the class
of a directory is SECCLASS_DIR once its label is initialized.

Fixes: 5d7ddc59b3d8 ("selinux: reduce path walk overhead")
Reported-by: syzbot+3857e9743aa17a4cd369@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/all/6b118362.8e3793c5.1bab8b.1377.GAE@xxxxxxxxxx/
Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
---
Other lockless readers of isec->sid in hooks.c (inode_has_perm(),
selinux_inode_follow_link(), ioctl_has_perm(), ...) are not changed;
they do not put the SID into the per-task cache. KCSAN can still
report them against the stores marked here (syzbot extid
8619c7745c88e275aab7 is one); I can mark them in a follow-up.

The patch applies to selinux/dev as well (offsets only).

syzbot has no reproducer. Tested in QEMU (2 CPUs, kernel 693c2e04e3f1
with the syzbot config, built with GCC 12.2; syzbot uses clang; the
image's policy in permissive mode) with a test program in which 16
threads in 4 processes keep opening files under /proc/<pid> of one
sleeping child, so that the revalidation of /proc/<pid> in one thread
meets the permission check on it in another. The SID stored there does
not change in this test, so it shows the unmarked accesses, not a
wrong cache entry. KCSAN was switched on at run time with a report
filter for selinux_task_to_inode() and selinux_inode_permission().
Without the patch all 5 instances reported this race within a second;
with the patch none of 5 did. One of the 5 patched instances was
stopped by the test harness after about 6 minutes for lack of console
output, with no kernel message; the same happened to 1 of 5 instances
of the unpatched kernel with the same load and KCSAN reporting nothing,
so it comes from the test setup.

Build tested with GCC 8.5.0 and W=1 (security/selinux/hooks.o): no new
warnings.

AI tools assisted with the analysis, the code and the changelog.

security/selinux/hooks.c | 66 +++++++++++++++++++++++++++-------------------
1 file changed, 39 insertions(+), 27 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 5f6a818..c7b4093 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -1456,7 +1456,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
goto out_unlock;

if (isec->sclass == SECCLASS_FILE)
- isec->sclass = inode_mode_to_security_class(inode->i_mode);
+ WRITE_ONCE(isec->sclass,
+ inode_mode_to_security_class(inode->i_mode));

sbsec = selinux_superblock(inode->i_sb);
if (!(sbsec->flags & SE_SBINITIALIZED)) {
@@ -1600,7 +1601,7 @@ out:
goto out_unlock;
}
isec->initialized = LABEL_INITIALIZED;
- isec->sid = sid;
+ WRITE_ONCE(isec->sid, sid);
}

out_unlock:
@@ -1611,7 +1612,7 @@ out_invalid:
spin_lock(&isec->lock);
if (isec->initialized == LABEL_PENDING) {
isec->initialized = LABEL_INVALID;
- isec->sid = sid;
+ WRITE_ONCE(isec->sid, sid);
}
spin_unlock(&isec->lock);
return 0;
@@ -3159,16 +3160,16 @@ static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
}

static noinline int audit_inode_permission(struct inode *inode,
+ u32 isid, u16 sclass,
u32 perms, u32 audited, u32 denied,
int result)
{
struct common_audit_data ad;
- struct inode_security_struct *isec = selinux_inode(inode);

ad.type = LSM_AUDIT_DATA_INODE;
ad.u.inode = inode;

- return slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
+ return slow_avc_audit(current_sid(), isid, sclass, perms,
audited, denied, result, &ad);
}

@@ -3190,20 +3191,21 @@ static inline void task_avdcache_reset(struct task_security_struct *tsec)
/**
* task_avdcache_search - Search the task's AVD cache
* @tsec: the task's security state
- * @isec: the inode to search for in the cache
+ * @isid: the inode SID to search for in the cache
+ * @sclass: the inode security class, only SECCLASS_DIR is cached
* @avdc: matching avd cache entry returned to the caller
*
- * Search @tsec for a AVD cache entry that matches @isec and return it to the
+ * Search @tsec for an AVD cache entry that matches @isid and return it to the
* caller via @avdc. Returns 0 if a match is found, negative values otherwise.
*/
static inline int task_avdcache_search(struct task_security_struct *tsec,
- struct inode_security_struct *isec,
+ u32 isid, u16 sclass,
struct avdc_entry **avdc)
{
int orig, iter;

/* focused on path walk optimization, only cache directories */
- if (isec->sclass != SECCLASS_DIR)
+ if (sclass != SECCLASS_DIR)
return -ENOENT;

if (unlikely(current_sid() != tsec->avdcache.sid ||
@@ -3214,7 +3216,7 @@ static inline int task_avdcache_search(struct task_security_struct *tsec,

orig = iter = tsec->avdcache.dir_spot;
do {
- if (tsec->avdcache.dir[iter].isid == isec->sid) {
+ if (tsec->avdcache.dir[iter].isid == isid) {
/* cache hit */
tsec->avdcache.dir_spot = iter;
*avdc = &tsec->avdcache.dir[iter];
@@ -3229,26 +3231,27 @@ static inline int task_avdcache_search(struct task_security_struct *tsec,
/**
* task_avdcache_update - Update the task's AVD cache
* @tsec: the task's security state
- * @isec: the inode associated with the cache entry
+ * @isid: the inode SID associated with the cache entry
+ * @sclass: the inode security class
* @avd: the AVD to cache
*
* Update the AVD cache in @tsec with the @avd info associated
- * with @isec.
+ * with @isid and @sclass.
*/
static inline void task_avdcache_update(struct task_security_struct *tsec,
- struct inode_security_struct *isec,
+ u32 isid, u16 sclass,
struct av_decision *avd)
{
int spot;

/* focused on path walk optimization, only cache directories */
- if (isec->sclass != SECCLASS_DIR)
+ if (sclass != SECCLASS_DIR)
return;

/* update cache */
spot = (tsec->avdcache.dir_spot + 1) & (TSEC_AVDC_DIR_SIZE - 1);
tsec->avdcache.dir_spot = spot;
- tsec->avdcache.dir[spot].isid = isec->sid;
+ tsec->avdcache.dir[spot].isid = isid;
tsec->avdcache.dir[spot].avd = *avd;
tsec->avdcache.permissive_neveraudit =
(avd->flags == (AVD_FLAGS_PERMISSIVE|AVD_FLAGS_NEVERAUDIT));
@@ -3267,7 +3270,8 @@ static int selinux_inode_permission(const struct mnt_idmap *idmap,
struct inode *inode, int requested)
{
int mask;
- u32 perms;
+ u32 perms, isid;
+ u16 sclass;
u32 sid = current_sid();
struct task_security_struct *tsec;
struct inode_security_struct *isec;
@@ -3289,9 +3293,17 @@ static int selinux_inode_permission(const struct mnt_idmap *idmap,
isec = inode_security_rcu(inode, requested & MAY_NOT_BLOCK);
if (IS_ERR(isec))
return PTR_ERR(isec);
+
+ /*
+ * isec->sid can change under us (relabel, /proc/<pid> revalidation);
+ * read it once so the AVC check, the cache and the audit record
+ * all use the same SID.
+ */
+ isid = READ_ONCE(isec->sid);
+ sclass = READ_ONCE(isec->sclass);
perms = file_mask_to_av(inode->i_mode, mask);

- rc = task_avdcache_search(tsec, isec, &avdc);
+ rc = task_avdcache_search(tsec, isid, sclass, &avdc);
if (likely(!rc)) {
/* Cache hit. */
avdp = &avdc->avd;
@@ -3301,9 +3313,8 @@ static int selinux_inode_permission(const struct mnt_idmap *idmap,
rc = -EACCES;
} else {
/* Cache miss. */
- rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass,
- perms, 0, avdp);
- task_avdcache_update(tsec, isec, avdp);
+ rc = avc_has_perm_noaudit(sid, isid, sclass, perms, 0, avdp);
+ task_avdcache_update(tsec, isid, sclass, avdp);
}

audited = avc_audit_required(perms, avdp, rc,
@@ -3312,7 +3323,8 @@ static int selinux_inode_permission(const struct mnt_idmap *idmap,
if (likely(!audited))
return rc;

- rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
+ rc2 = audit_inode_permission(inode, isid, sclass, perms, audited,
+ denied, rc);
if (rc2)
return rc2;

@@ -3528,8 +3540,8 @@ static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,

isec = backing_inode_security(dentry);
spin_lock(&isec->lock);
- isec->sclass = inode_mode_to_security_class(inode->i_mode);
- isec->sid = newsid;
+ WRITE_ONCE(isec->sclass, inode_mode_to_security_class(inode->i_mode));
+ WRITE_ONCE(isec->sid, newsid);
isec->initialized = LABEL_INITIALIZED;
spin_unlock(&isec->lock);
}
@@ -3697,8 +3709,8 @@ static int selinux_inode_setsecurity(struct inode *inode, const char *name,
return rc;

spin_lock(&isec->lock);
- isec->sclass = inode_mode_to_security_class(inode->i_mode);
- isec->sid = newsid;
+ WRITE_ONCE(isec->sclass, inode_mode_to_security_class(inode->i_mode));
+ WRITE_ONCE(isec->sid, newsid);
isec->initialized = LABEL_INITIALIZED;
spin_unlock(&isec->lock);
return 0;
@@ -4736,8 +4748,8 @@ static void selinux_task_to_inode(struct task_struct *p,
u32 sid = task_sid_obj(p);

spin_lock(&isec->lock);
- isec->sclass = inode_mode_to_security_class(inode->i_mode);
- isec->sid = sid;
+ WRITE_ONCE(isec->sclass, inode_mode_to_security_class(inode->i_mode));
+ WRITE_ONCE(isec->sid, sid);
isec->initialized = LABEL_INITIALIZED;
spin_unlock(&isec->lock);
}
--
2.53.0