Skip to content

Commit dde3a5d

Browse files
stephensmalleypcmoore
authored andcommitted
selinux: move avdcache to per-task security struct
The avdcache is meant to be per-task; move it to a new task_security_struct that is duplicated per-task. Cc: stable@vger.kernel.org Fixes: 5d7ddc5 ("selinux: reduce path walk overhead") Signed-off-by: Stephen Smalley <stephen.smalley.work@gmail.com> [PM: line length fixes] Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 75f72fe commit dde3a5d

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

security/selinux/hooks.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static void cred_init_security(void)
215215
/* NOTE: the lsm framework zeros out the buffer on allocation */
216216

217217
tsec = selinux_cred(unrcu_pointer(current->real_cred));
218-
tsec->osid = tsec->sid = tsec->avdcache.sid = SECINITSID_KERNEL;
218+
tsec->osid = tsec->sid = SECINITSID_KERNEL;
219219
}
220220

221221
/*
@@ -3110,10 +3110,10 @@ static noinline int audit_inode_permission(struct inode *inode,
31103110
* Clear the task's AVD cache in @tsec and reset it to the current policy's
31113111
* and task's info.
31123112
*/
3113-
static inline void task_avdcache_reset(struct cred_security_struct *tsec)
3113+
static inline void task_avdcache_reset(struct task_security_struct *tsec)
31143114
{
31153115
memset(&tsec->avdcache.dir, 0, sizeof(tsec->avdcache.dir));
3116-
tsec->avdcache.sid = tsec->sid;
3116+
tsec->avdcache.sid = current_sid();
31173117
tsec->avdcache.seqno = avc_policy_seqno();
31183118
tsec->avdcache.dir_spot = TSEC_AVDC_DIR_SIZE - 1;
31193119
}
@@ -3127,7 +3127,7 @@ static inline void task_avdcache_reset(struct cred_security_struct *tsec)
31273127
* Search @tsec for a AVD cache entry that matches @isec and return it to the
31283128
* caller via @avdc. Returns 0 if a match is found, negative values otherwise.
31293129
*/
3130-
static inline int task_avdcache_search(struct cred_security_struct *tsec,
3130+
static inline int task_avdcache_search(struct task_security_struct *tsec,
31313131
struct inode_security_struct *isec,
31323132
struct avdc_entry **avdc)
31333133
{
@@ -3137,7 +3137,7 @@ static inline int task_avdcache_search(struct cred_security_struct *tsec,
31373137
if (isec->sclass != SECCLASS_DIR)
31383138
return -ENOENT;
31393139

3140-
if (unlikely(tsec->sid != tsec->avdcache.sid ||
3140+
if (unlikely(current_sid() != tsec->avdcache.sid ||
31413141
tsec->avdcache.seqno != avc_policy_seqno())) {
31423142
task_avdcache_reset(tsec);
31433143
return -ENOENT;
@@ -3167,7 +3167,7 @@ static inline int task_avdcache_search(struct cred_security_struct *tsec,
31673167
* Update the AVD cache in @tsec with the @avdc and @audited info associated
31683168
* with @isec.
31693169
*/
3170-
static inline void task_avdcache_update(struct cred_security_struct *tsec,
3170+
static inline void task_avdcache_update(struct task_security_struct *tsec,
31713171
struct inode_security_struct *isec,
31723172
struct av_decision *avd,
31733173
u32 audited)
@@ -3201,7 +3201,8 @@ static int selinux_inode_permission(struct inode *inode, int requested)
32013201
{
32023202
int mask;
32033203
u32 perms;
3204-
struct cred_security_struct *tsec;
3204+
u32 sid = current_sid();
3205+
struct task_security_struct *tsec;
32053206
struct inode_security_struct *isec;
32063207
struct avdc_entry *avdc;
32073208
int rc, rc2;
@@ -3213,8 +3214,8 @@ static int selinux_inode_permission(struct inode *inode, int requested)
32133214
if (!mask)
32143215
return 0;
32153216

3216-
tsec = selinux_cred(current_cred());
3217-
if (task_avdcache_permnoaudit(tsec))
3217+
tsec = selinux_task(current);
3218+
if (task_avdcache_permnoaudit(tsec, sid))
32183219
return 0;
32193220

32203221
isec = inode_security_rcu(inode, requested & MAY_NOT_BLOCK);
@@ -3234,7 +3235,7 @@ static int selinux_inode_permission(struct inode *inode, int requested)
32343235
struct av_decision avd;
32353236

32363237
/* Cache miss. */
3237-
rc = avc_has_perm_noaudit(tsec->sid, isec->sid, isec->sclass,
3238+
rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass,
32383239
perms, 0, &avd);
32393240
audited = avc_audit_required(perms, &avd, rc,
32403241
(requested & MAY_ACCESS) ? FILE__AUDIT_ACCESS : 0,
@@ -3283,11 +3284,11 @@ static int selinux_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
32833284

32843285
static int selinux_inode_getattr(const struct path *path)
32853286
{
3286-
struct cred_security_struct *tsec;
3287+
struct task_security_struct *tsec;
32873288

3288-
tsec = selinux_cred(current_cred());
3289+
tsec = selinux_task(current);
32893290

3290-
if (task_avdcache_permnoaudit(tsec))
3291+
if (task_avdcache_permnoaudit(tsec, current_sid()))
32913292
return 0;
32923293

32933294
return path_has_perm(current_cred(), path, FILE__GETATTR);
@@ -4151,7 +4152,10 @@ static int selinux_task_alloc(struct task_struct *task,
41514152
u64 clone_flags)
41524153
{
41534154
u32 sid = current_sid();
4155+
struct task_security_struct *old_tsec = selinux_task(current);
4156+
struct task_security_struct *new_tsec = selinux_task(task);
41544157

4158+
*new_tsec = *old_tsec;
41554159
return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
41564160
}
41574161

@@ -7138,6 +7142,7 @@ static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *att
71387142

71397143
struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
71407144
.lbs_cred = sizeof(struct cred_security_struct),
7145+
.lbs_task = sizeof(struct task_security_struct),
71417146
.lbs_file = sizeof(struct file_security_struct),
71427147
.lbs_inode = sizeof(struct inode_security_struct),
71437148
.lbs_ipc = sizeof(struct ipc_security_struct),

security/selinux/include/objsec.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct cred_security_struct {
4444
u32 create_sid; /* fscreate SID */
4545
u32 keycreate_sid; /* keycreate SID */
4646
u32 sockcreate_sid; /* fscreate SID */
47+
} __randomize_layout;
48+
49+
struct task_security_struct {
4750
#define TSEC_AVDC_DIR_SIZE (1 << 2)
4851
struct {
4952
u32 sid; /* current SID for cached entries */
@@ -54,10 +57,11 @@ struct cred_security_struct {
5457
} avdcache;
5558
} __randomize_layout;
5659

57-
static inline bool task_avdcache_permnoaudit(struct cred_security_struct *tsec)
60+
static inline bool task_avdcache_permnoaudit(struct task_security_struct *tsec,
61+
u32 sid)
5862
{
5963
return (tsec->avdcache.permissive_neveraudit &&
60-
tsec->sid == tsec->avdcache.sid &&
64+
sid == tsec->avdcache.sid &&
6165
tsec->avdcache.seqno == avc_policy_seqno());
6266
}
6367

@@ -177,6 +181,12 @@ static inline struct cred_security_struct *selinux_cred(const struct cred *cred)
177181
return cred->security + selinux_blob_sizes.lbs_cred;
178182
}
179183

184+
static inline struct task_security_struct *
185+
selinux_task(const struct task_struct *task)
186+
{
187+
return task->security + selinux_blob_sizes.lbs_task;
188+
}
189+
180190
static inline struct file_security_struct *selinux_file(const struct file *file)
181191
{
182192
return file->f_security + selinux_blob_sizes.lbs_file;

0 commit comments

Comments
 (0)