Skip to content

Commit 43fc460

Browse files
cschauflerkees
authored andcommitted
TOMOYO: Abstract use of cred security blob
Don't use the cred->security pointer directly. Provide helper functions that provide the security blob pointer. 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 69b5a44 commit 43fc460

4 files changed

Lines changed: 64 additions & 16 deletions

File tree

‎security/tomoyo/common.h‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/in.h>
3030
#include <linux/in6.h>
3131
#include <linux/un.h>
32+
#include <linux/lsm_hooks.h>
3233
#include <net/sock.h>
3334
#include <net/af_unix.h>
3435
#include <net/ip.h>
@@ -1062,6 +1063,7 @@ void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
10621063
/********** External variable definitions. **********/
10631064

10641065
extern bool tomoyo_policy_loaded;
1066+
extern int tomoyo_enabled;
10651067
extern const char * const tomoyo_condition_keyword
10661068
[TOMOYO_MAX_CONDITION_KEYWORD];
10671069
extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
@@ -1196,14 +1198,27 @@ static inline void tomoyo_put_group(struct tomoyo_group *group)
11961198
atomic_dec(&group->head.users);
11971199
}
11981200

1201+
/**
1202+
* tomoyo_cred - Get a pointer to the tomoyo cred security blob
1203+
* @cred - the relevant cred
1204+
*
1205+
* Returns pointer to the tomoyo cred blob.
1206+
*/
1207+
static inline struct tomoyo_domain_info **tomoyo_cred(const struct cred *cred)
1208+
{
1209+
return (struct tomoyo_domain_info **)&cred->security;
1210+
}
1211+
11991212
/**
12001213
* tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
12011214
*
12021215
* Returns pointer to "struct tomoyo_domain_info" for current thread.
12031216
*/
12041217
static inline struct tomoyo_domain_info *tomoyo_domain(void)
12051218
{
1206-
return current_cred()->security;
1219+
struct tomoyo_domain_info **blob = tomoyo_cred(current_cred());
1220+
1221+
return *blob;
12071222
}
12081223

12091224
/**
@@ -1216,7 +1231,9 @@ static inline struct tomoyo_domain_info *tomoyo_domain(void)
12161231
static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
12171232
*task)
12181233
{
1219-
return task_cred_xxx(task, security);
1234+
struct tomoyo_domain_info **blob = tomoyo_cred(get_task_cred(task));
1235+
1236+
return *blob;
12201237
}
12211238

12221239
/**

‎security/tomoyo/domain.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ static int tomoyo_environ(struct tomoyo_execve *ee)
678678
*/
679679
int tomoyo_find_next_domain(struct linux_binprm *bprm)
680680
{
681+
struct tomoyo_domain_info **blob;
681682
struct tomoyo_domain_info *old_domain = tomoyo_domain();
682683
struct tomoyo_domain_info *domain = NULL;
683684
const char *original_name = bprm->filename;
@@ -843,7 +844,8 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
843844
domain = old_domain;
844845
/* Update reference count on "struct tomoyo_domain_info". */
845846
atomic_inc(&domain->users);
846-
bprm->cred->security = domain;
847+
blob = tomoyo_cred(bprm->cred);
848+
*blob = domain;
847849
kfree(exename.name);
848850
if (!retval) {
849851
ee->r.domain = domain;

‎security/tomoyo/securityfs_if.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
7171
if (!cred) {
7272
error = -ENOMEM;
7373
} else {
74-
struct tomoyo_domain_info *old_domain =
75-
cred->security;
76-
cred->security = new_domain;
74+
struct tomoyo_domain_info **blob;
75+
struct tomoyo_domain_info *old_domain;
76+
77+
blob = tomoyo_cred(cred);
78+
old_domain = *blob;
79+
*blob = new_domain;
7780
atomic_inc(&new_domain->users);
7881
atomic_dec(&old_domain->users);
7982
commit_creds(cred);
@@ -234,10 +237,14 @@ static void __init tomoyo_create_entry(const char *name, const umode_t mode,
234237
*/
235238
static int __init tomoyo_initerface_init(void)
236239
{
240+
struct tomoyo_domain_info *domain;
237241
struct dentry *tomoyo_dir;
238242

243+
if (!tomoyo_enabled)
244+
return 0;
245+
domain = tomoyo_domain();
239246
/* Don't create securityfs entries unless registered. */
240-
if (current_cred()->security != &tomoyo_kernel_domain)
247+
if (domain != &tomoyo_kernel_domain)
241248
return 0;
242249

243250
tomoyo_dir = securityfs_create_dir("tomoyo", NULL);

‎security/tomoyo/tomoyo.c‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919
static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
2020
{
21-
new->security = NULL;
21+
struct tomoyo_domain_info **blob = tomoyo_cred(new);
22+
23+
*blob = NULL;
2224
return 0;
2325
}
2426

@@ -34,8 +36,13 @@ static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
3436
static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
3537
gfp_t gfp)
3638
{
37-
struct tomoyo_domain_info *domain = old->security;
38-
new->security = domain;
39+
struct tomoyo_domain_info **old_blob = tomoyo_cred(old);
40+
struct tomoyo_domain_info **new_blob = tomoyo_cred(new);
41+
struct tomoyo_domain_info *domain;
42+
43+
domain = *old_blob;
44+
*new_blob = domain;
45+
3946
if (domain)
4047
atomic_inc(&domain->users);
4148
return 0;
@@ -59,7 +66,9 @@ static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
5966
*/
6067
static void tomoyo_cred_free(struct cred *cred)
6168
{
62-
struct tomoyo_domain_info *domain = cred->security;
69+
struct tomoyo_domain_info **blob = tomoyo_cred(cred);
70+
struct tomoyo_domain_info *domain = *blob;
71+
6372
if (domain)
6473
atomic_dec(&domain->users);
6574
}
@@ -73,6 +82,9 @@ static void tomoyo_cred_free(struct cred *cred)
7382
*/
7483
static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
7584
{
85+
struct tomoyo_domain_info **blob;
86+
struct tomoyo_domain_info *domain;
87+
7688
/*
7789
* Do only if this function is called for the first time of an execve
7890
* operation.
@@ -93,13 +105,14 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
93105
* stored inside "bprm->cred->security" will be acquired later inside
94106
* tomoyo_find_next_domain().
95107
*/
96-
atomic_dec(&((struct tomoyo_domain_info *)
97-
bprm->cred->security)->users);
108+
blob = tomoyo_cred(bprm->cred);
109+
domain = *blob;
110+
atomic_dec(&domain->users);
98111
/*
99112
* Tell tomoyo_bprm_check_security() is called for the first time of an
100113
* execve operation.
101114
*/
102-
bprm->cred->security = NULL;
115+
*blob = NULL;
103116
return 0;
104117
}
105118

@@ -112,8 +125,11 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
112125
*/
113126
static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
114127
{
115-
struct tomoyo_domain_info *domain = bprm->cred->security;
128+
struct tomoyo_domain_info **blob;
129+
struct tomoyo_domain_info *domain;
116130

131+
blob = tomoyo_cred(bprm->cred);
132+
domain = *blob;
117133
/*
118134
* Execute permission is checked against pathname passed to do_execve()
119135
* using current domain.
@@ -531,6 +547,8 @@ static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
531547
/* Lock for GC. */
532548
DEFINE_SRCU(tomoyo_ss);
533549

550+
int tomoyo_enabled __lsm_ro_after_init = 1;
551+
534552
/**
535553
* tomoyo_init - Register TOMOYO Linux as a LSM module.
536554
*
@@ -539,17 +557,21 @@ DEFINE_SRCU(tomoyo_ss);
539557
static int __init tomoyo_init(void)
540558
{
541559
struct cred *cred = (struct cred *) current_cred();
560+
struct tomoyo_domain_info **blob;
542561

543562
/* register ourselves with the security framework */
544563
security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
545564
printk(KERN_INFO "TOMOYO Linux initialized\n");
546-
cred->security = &tomoyo_kernel_domain;
565+
blob = tomoyo_cred(cred);
566+
*blob = &tomoyo_kernel_domain;
547567
tomoyo_mm_init();
568+
548569
return 0;
549570
}
550571

551572
DEFINE_LSM(tomoyo) = {
552573
.name = "tomoyo",
574+
.enabled = &tomoyo_enabled,
553575
.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
554576
.init = tomoyo_init,
555577
};

0 commit comments

Comments
 (0)