Skip to content

Commit 33bf60c

Browse files
cschauflerkees
authored andcommitted
LSM: Infrastructure management of the file security
Move management of the file->f_security blob out of the individual security modules and into the infrastructure. The modules no longer allocate or free the data, instead they tell the infrastructure how much space they require. Signed-off-by: Casey Schaufler <casey@schaufler-ca.com> Reviewed-by: Kees Cook <keescook@chromium.org> [kees: adjusted for ordered init series] Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent f28952a commit 33bf60c

8 files changed

Lines changed: 72 additions & 51 deletions

File tree

‎include/linux/lsm_hooks.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,7 @@ struct security_hook_list {
20322032
*/
20332033
struct lsm_blob_sizes {
20342034
int lbs_cred;
2035+
int lbs_file;
20352036
};
20362037

20372038
/*

‎security/apparmor/include/file.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ struct path;
3232
AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
3333
AA_EXEC_MMAP | AA_MAY_LINK)
3434

35-
#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security)
35+
static inline struct aa_file_ctx *file_ctx(struct file *file)
36+
{
37+
return file->f_security + apparmor_blob_sizes.lbs_file;
38+
}
3639

3740
/* struct aa_file_ctx - the AppArmor context the file was opened in
3841
* @lock: lock to update the ctx

‎security/apparmor/lsm.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,21 +434,21 @@ static int apparmor_file_open(struct file *file)
434434

435435
static int apparmor_file_alloc_security(struct file *file)
436436
{
437-
int error = 0;
438-
439-
/* freed by apparmor_file_free_security */
437+
struct aa_file_ctx *ctx = file_ctx(file);
440438
struct aa_label *label = begin_current_label_crit_section();
441-
file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
442-
if (!file_ctx(file))
443-
error = -ENOMEM;
444-
end_current_label_crit_section(label);
445439

446-
return error;
440+
spin_lock_init(&ctx->lock);
441+
rcu_assign_pointer(ctx->label, aa_get_label(label));
442+
end_current_label_crit_section(label);
443+
return 0;
447444
}
448445

449446
static void apparmor_file_free_security(struct file *file)
450447
{
451-
aa_free_file_ctx(file_ctx(file));
448+
struct aa_file_ctx *ctx = file_ctx(file);
449+
450+
if (ctx)
451+
aa_put_label(rcu_access_pointer(ctx->label));
452452
}
453453

454454
static int common_file_perm(const char *op, struct file *file, u32 mask)
@@ -1156,6 +1156,7 @@ static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb,
11561156
*/
11571157
struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = {
11581158
.lbs_cred = sizeof(struct aa_task_ctx *),
1159+
.lbs_file = sizeof(struct aa_file_ctx),
11591160
};
11601161

11611162
static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {

‎security/security.c‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
struct security_hook_heads security_hook_heads __lsm_ro_after_init;
4141
static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
4242

43+
static struct kmem_cache *lsm_file_cache;
44+
4345
char *lsm_names;
4446
static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init;
4547

@@ -158,6 +160,7 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
158160
return;
159161

160162
lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
163+
lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
161164
}
162165

163166
/* Prepare LSM for initialization. */
@@ -279,6 +282,15 @@ static void __init ordered_lsm_init(void)
279282
prepare_lsm(*lsm);
280283

281284
init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
285+
init_debug("file blob size = %d\n", blob_sizes.lbs_file);
286+
287+
/*
288+
* Create any kmem_caches needed for blobs
289+
*/
290+
if (blob_sizes.lbs_file)
291+
lsm_file_cache = kmem_cache_create("lsm_file_cache",
292+
blob_sizes.lbs_file, 0,
293+
SLAB_PANIC, NULL);
282294

283295
for (lsm = ordered_lsms; *lsm; lsm++)
284296
initialize_lsm(*lsm);
@@ -448,6 +460,27 @@ void __init lsm_early_cred(struct cred *cred)
448460
panic("%s: Early cred alloc failed.\n", __func__);
449461
}
450462

463+
/**
464+
* lsm_file_alloc - allocate a composite file blob
465+
* @file: the file that needs a blob
466+
*
467+
* Allocate the file blob for all the modules
468+
*
469+
* Returns 0, or -ENOMEM if memory can't be allocated.
470+
*/
471+
static int lsm_file_alloc(struct file *file)
472+
{
473+
if (!lsm_file_cache) {
474+
file->f_security = NULL;
475+
return 0;
476+
}
477+
478+
file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
479+
if (file->f_security == NULL)
480+
return -ENOMEM;
481+
return 0;
482+
}
483+
451484
/*
452485
* Hook list operation macros.
453486
*
@@ -1144,12 +1177,27 @@ int security_file_permission(struct file *file, int mask)
11441177

11451178
int security_file_alloc(struct file *file)
11461179
{
1147-
return call_int_hook(file_alloc_security, 0, file);
1180+
int rc = lsm_file_alloc(file);
1181+
1182+
if (rc)
1183+
return rc;
1184+
rc = call_int_hook(file_alloc_security, 0, file);
1185+
if (unlikely(rc))
1186+
security_file_free(file);
1187+
return rc;
11481188
}
11491189

11501190
void security_file_free(struct file *file)
11511191
{
1192+
void *blob;
1193+
11521194
call_void_hook(file_free_security, file);
1195+
1196+
blob = file->f_security;
1197+
if (blob) {
1198+
file->f_security = NULL;
1199+
kmem_cache_free(lsm_file_cache, blob);
1200+
}
11531201
}
11541202

11551203
int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -1267,7 +1315,7 @@ int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
12671315
return rc;
12681316

12691317
rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
1270-
if (rc)
1318+
if (unlikely(rc))
12711319
security_cred_free(cred);
12721320
return rc;
12731321
}
@@ -1288,7 +1336,7 @@ int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
12881336
return rc;
12891337

12901338
rc = call_int_hook(cred_prepare, 0, new, old, gfp);
1291-
if (rc)
1339+
if (unlikely(rc))
12921340
security_cred_free(new);
12931341
return rc;
12941342
}

‎security/selinux/hooks.c‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ static int __init checkreqprot_setup(char *str)
146146
__setup("checkreqprot=", checkreqprot_setup);
147147

148148
static struct kmem_cache *sel_inode_cache;
149-
static struct kmem_cache *file_security_cache;
150149

151150
/**
152151
* selinux_secmark_enabled - Check to see if SECMARK is currently enabled
@@ -378,27 +377,15 @@ static void inode_free_security(struct inode *inode)
378377

379378
static int file_alloc_security(struct file *file)
380379
{
381-
struct file_security_struct *fsec;
380+
struct file_security_struct *fsec = selinux_file(file);
382381
u32 sid = current_sid();
383382

384-
fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
385-
if (!fsec)
386-
return -ENOMEM;
387-
388383
fsec->sid = sid;
389384
fsec->fown_sid = sid;
390-
file->f_security = fsec;
391385

392386
return 0;
393387
}
394388

395-
static void file_free_security(struct file *file)
396-
{
397-
struct file_security_struct *fsec = selinux_file(file);
398-
file->f_security = NULL;
399-
kmem_cache_free(file_security_cache, fsec);
400-
}
401-
402389
static int superblock_alloc_security(struct super_block *sb)
403390
{
404391
struct superblock_security_struct *sbsec;
@@ -3345,11 +3332,6 @@ static int selinux_file_alloc_security(struct file *file)
33453332
return file_alloc_security(file);
33463333
}
33473334

3348-
static void selinux_file_free_security(struct file *file)
3349-
{
3350-
file_free_security(file);
3351-
}
3352-
33533335
/*
33543336
* Check whether a task has the ioctl permission and cmd
33553337
* operation to an inode.
@@ -6646,6 +6628,7 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
66466628

66476629
struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
66486630
.lbs_cred = sizeof(struct task_security_struct),
6631+
.lbs_file = sizeof(struct file_security_struct),
66496632
};
66506633

66516634
static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
@@ -6717,7 +6700,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
67176700

67186701
LSM_HOOK_INIT(file_permission, selinux_file_permission),
67196702
LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6720-
LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
67216703
LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
67226704
LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
67236705
LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
@@ -6902,9 +6884,6 @@ static __init int selinux_init(void)
69026884
sel_inode_cache = kmem_cache_create("selinux_inode_security",
69036885
sizeof(struct inode_security_struct),
69046886
0, SLAB_PANIC, NULL);
6905-
file_security_cache = kmem_cache_create("selinux_file_security",
6906-
sizeof(struct file_security_struct),
6907-
0, SLAB_PANIC, NULL);
69086887
avc_init();
69096888

69106889
avtab_cache_init();

‎security/selinux/include/objsec.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static inline struct task_security_struct *selinux_cred(const struct cred *cred)
167167

168168
static inline struct file_security_struct *selinux_file(const struct file *file)
169169
{
170-
return file->f_security;
170+
return file->f_security + selinux_blob_sizes.lbs_file;
171171
}
172172

173173
#endif /* _SELINUX_OBJSEC_H_ */

‎security/smack/smack.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ static inline struct task_smack *smack_cred(const struct cred *cred)
364364

365365
static inline struct smack_known **smack_file(const struct file *file)
366366
{
367-
return (struct smack_known **)&file->f_security;
367+
return (struct smack_known **)(file->f_security +
368+
smack_blob_sizes.lbs_file);
368369
}
369370

370371
/*

‎security/smack/smack_lsm.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,18 +1495,6 @@ static int smack_file_alloc_security(struct file *file)
14951495
return 0;
14961496
}
14971497

1498-
/**
1499-
* smack_file_free_security - clear a file security blob
1500-
* @file: the object
1501-
*
1502-
* The security blob for a file is a pointer to the master
1503-
* label list, so no memory is freed.
1504-
*/
1505-
static void smack_file_free_security(struct file *file)
1506-
{
1507-
file->f_security = NULL;
1508-
}
1509-
15101498
/**
15111499
* smack_file_ioctl - Smack check on ioctls
15121500
* @file: the object
@@ -4559,6 +4547,7 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
45594547

45604548
struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
45614549
.lbs_cred = sizeof(struct task_smack),
4550+
.lbs_file = sizeof(struct smack_known *),
45624551
};
45634552

45644553
static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
@@ -4595,7 +4584,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
45954584
LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
45964585

45974586
LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4598-
LSM_HOOK_INIT(file_free_security, smack_file_free_security),
45994587
LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
46004588
LSM_HOOK_INIT(file_lock, smack_file_lock),
46014589
LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),

0 commit comments

Comments
 (0)