Skip to content

Commit 8c6cb98

Browse files
Tetsuo HandaJames Morris
authored andcommitted
tomoyo: Swicth from cred->security to task_struct->security.
TOMOYO security module is designed to use "struct task_struct"->security in order to allow per "struct task_struct" tracking without being disturbed by unable to update "struct cred"->security due to override mechanism. Now that infrastructure-managed security blob is ready, this patch updates TOMOYO to use "struct task_struct"->security. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <james.morris@microsoft.com>
1 parent 23711df commit 8c6cb98

5 files changed

Lines changed: 108 additions & 122 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
986986
else
987987
p = find_task_by_vpid(pid);
988988
if (p)
989-
domain = tomoyo_real_domain(p);
989+
domain = tomoyo_task(p)->domain_info;
990990
rcu_read_unlock();
991991
} else if (!strncmp(data, "domain=", 7)) {
992992
if (tomoyo_domain_def(data + 7))
@@ -1668,7 +1668,7 @@ static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
16681668
else
16691669
p = find_task_by_vpid(pid);
16701670
if (p)
1671-
domain = tomoyo_real_domain(p);
1671+
domain = tomoyo_task(p)->domain_info;
16721672
rcu_read_unlock();
16731673
if (!domain)
16741674
return;

‎security/tomoyo/common.h‎

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ struct tomoyo_domain_info {
686686
u8 group; /* Group number to use. */
687687
bool is_deleted; /* Delete flag. */
688688
bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
689-
atomic_t users; /* Number of referring credentials. */
689+
atomic_t users; /* Number of referring tasks. */
690690
};
691691

692692
/*
@@ -913,6 +913,12 @@ struct tomoyo_policy_namespace {
913913
const char *name;
914914
};
915915

916+
/* Structure for "struct task_struct"->security. */
917+
struct tomoyo_task {
918+
struct tomoyo_domain_info *domain_info;
919+
struct tomoyo_domain_info *old_domain_info;
920+
};
921+
916922
/********** Function prototypes. **********/
917923

918924
bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
@@ -1021,6 +1027,7 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
10211027
struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
10221028
struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
10231029
const bool transit);
1030+
struct tomoyo_domain_info *tomoyo_domain(void);
10241031
struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
10251032
struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
10261033
const u8 idx);
@@ -1200,41 +1207,15 @@ static inline void tomoyo_put_group(struct tomoyo_group *group)
12001207
}
12011208

12021209
/**
1203-
* tomoyo_cred - Get a pointer to the tomoyo cred security blob
1204-
* @cred - the relevant cred
1205-
*
1206-
* Returns pointer to the tomoyo cred blob.
1207-
*/
1208-
static inline struct tomoyo_domain_info **tomoyo_cred(const struct cred *cred)
1209-
{
1210-
return cred->security + tomoyo_blob_sizes.lbs_cred;
1211-
}
1212-
1213-
/**
1214-
* tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
1210+
* tomoyo_task - Get "struct tomoyo_task" for specified thread.
12151211
*
1216-
* Returns pointer to "struct tomoyo_domain_info" for current thread.
1217-
*/
1218-
static inline struct tomoyo_domain_info *tomoyo_domain(void)
1219-
{
1220-
struct tomoyo_domain_info **blob = tomoyo_cred(current_cred());
1221-
1222-
return *blob;
1223-
}
1224-
1225-
/**
1226-
* tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
1212+
* @task - Pointer to "struct task_struct".
12271213
*
1228-
* @task: Pointer to "struct task_struct".
1229-
*
1230-
* Returns pointer to "struct tomoyo_security" for specified thread.
1214+
* Returns pointer to "struct tomoyo_task" for specified thread.
12311215
*/
1232-
static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
1233-
*task)
1216+
static inline struct tomoyo_task *tomoyo_task(struct task_struct *task)
12341217
{
1235-
struct tomoyo_domain_info **blob = tomoyo_cred(get_task_cred(task));
1236-
1237-
return *blob;
1218+
return task->security + tomoyo_blob_sizes.lbs_task;
12381219
}
12391220

12401221
/**

‎security/tomoyo/domain.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ 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;
682681
struct tomoyo_domain_info *old_domain = tomoyo_domain();
683682
struct tomoyo_domain_info *domain = NULL;
684683
const char *original_name = bprm->filename;
@@ -843,9 +842,13 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
843842
if (!domain)
844843
domain = old_domain;
845844
/* Update reference count on "struct tomoyo_domain_info". */
846-
atomic_inc(&domain->users);
847-
blob = tomoyo_cred(bprm->cred);
848-
*blob = domain;
845+
{
846+
struct tomoyo_task *s = tomoyo_task(current);
847+
848+
s->old_domain_info = s->domain_info;
849+
s->domain_info = domain;
850+
atomic_inc(&domain->users);
851+
}
849852
kfree(exename.name);
850853
if (!retval) {
851854
ee->r.domain = domain;

‎security/tomoyo/securityfs_if.c‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,14 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
6767
if (!new_domain) {
6868
error = -ENOENT;
6969
} else {
70-
struct cred *cred = prepare_creds();
71-
if (!cred) {
72-
error = -ENOMEM;
73-
} else {
74-
struct tomoyo_domain_info **blob;
75-
struct tomoyo_domain_info *old_domain;
70+
struct tomoyo_task *s = tomoyo_task(current);
71+
struct tomoyo_domain_info *old_domain =
72+
s->domain_info;
7673

77-
blob = tomoyo_cred(cred);
78-
old_domain = *blob;
79-
*blob = new_domain;
80-
atomic_inc(&new_domain->users);
81-
atomic_dec(&old_domain->users);
82-
commit_creds(cred);
83-
error = 0;
84-
}
74+
s->domain_info = new_domain;
75+
atomic_inc(&new_domain->users);
76+
atomic_dec(&old_domain->users);
77+
error = 0;
8578
}
8679
}
8780
tomoyo_read_unlock(idx);

‎security/tomoyo/tomoyo.c‎

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
#include "common.h"
1010

1111
/**
12-
* tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
12+
* tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
1313
*
14-
* @new: Pointer to "struct cred".
15-
* @gfp: Memory allocation flags.
16-
*
17-
* Returns 0.
14+
* Returns pointer to "struct tomoyo_domain_info" for current thread.
1815
*/
19-
static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
16+
struct tomoyo_domain_info *tomoyo_domain(void)
2017
{
21-
struct tomoyo_domain_info **blob = tomoyo_cred(new);
18+
struct tomoyo_task *s = tomoyo_task(current);
2219

23-
*blob = NULL;
24-
return 0;
20+
if (s->old_domain_info && !current->in_execve) {
21+
atomic_dec(&s->old_domain_info->users);
22+
s->old_domain_info = NULL;
23+
}
24+
return s->domain_info;
2525
}
2626

2727
/**
@@ -36,85 +36,56 @@ static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
3636
static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
3737
gfp_t gfp)
3838
{
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;
39+
/* Restore old_domain_info saved by previous execve() request. */
40+
struct tomoyo_task *s = tomoyo_task(current);
4541

46-
if (domain)
47-
atomic_inc(&domain->users);
42+
if (s->old_domain_info && !current->in_execve) {
43+
atomic_dec(&s->domain_info->users);
44+
s->domain_info = s->old_domain_info;
45+
s->old_domain_info = NULL;
46+
}
4847
return 0;
4948
}
5049

5150
/**
52-
* tomoyo_cred_transfer - Target for security_transfer_creds().
51+
* tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds().
5352
*
54-
* @new: Pointer to "struct cred".
55-
* @old: Pointer to "struct cred".
56-
*/
57-
static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
58-
{
59-
tomoyo_cred_prepare(new, old, 0);
60-
}
61-
62-
/**
63-
* tomoyo_cred_free - Target for security_cred_free().
64-
*
65-
* @cred: Pointer to "struct cred".
53+
* @bprm: Pointer to "struct linux_binprm".
6654
*/
67-
static void tomoyo_cred_free(struct cred *cred)
55+
static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm)
6856
{
69-
struct tomoyo_domain_info **blob = tomoyo_cred(cred);
70-
struct tomoyo_domain_info *domain = *blob;
57+
/* Clear old_domain_info saved by execve() request. */
58+
struct tomoyo_task *s = tomoyo_task(current);
7159

72-
if (domain)
73-
atomic_dec(&domain->users);
60+
atomic_dec(&s->old_domain_info->users);
61+
s->old_domain_info = NULL;
7462
}
7563

64+
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
7665
/**
7766
* tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
7867
*
7968
* @bprm: Pointer to "struct linux_binprm".
8069
*
81-
* Returns 0 on success, negative value otherwise.
70+
* Returns 0.
8271
*/
8372
static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
8473
{
85-
struct tomoyo_domain_info **blob;
86-
struct tomoyo_domain_info *domain;
87-
8874
/*
8975
* Do only if this function is called for the first time of an execve
9076
* operation.
9177
*/
9278
if (bprm->called_set_creds)
9379
return 0;
94-
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
9580
/*
9681
* Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
9782
* for the first time.
9883
*/
9984
if (!tomoyo_policy_loaded)
10085
tomoyo_load_policy(bprm->filename);
101-
#endif
102-
/*
103-
* Release reference to "struct tomoyo_domain_info" stored inside
104-
* "bprm->cred->security". New reference to "struct tomoyo_domain_info"
105-
* stored inside "bprm->cred->security" will be acquired later inside
106-
* tomoyo_find_next_domain().
107-
*/
108-
blob = tomoyo_cred(bprm->cred);
109-
domain = *blob;
110-
atomic_dec(&domain->users);
111-
/*
112-
* Tell tomoyo_bprm_check_security() is called for the first time of an
113-
* execve operation.
114-
*/
115-
*blob = NULL;
11686
return 0;
11787
}
88+
#endif
11889

11990
/**
12091
* tomoyo_bprm_check_security - Target for security_bprm_check().
@@ -125,16 +96,13 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
12596
*/
12697
static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
12798
{
128-
struct tomoyo_domain_info **blob;
129-
struct tomoyo_domain_info *domain;
99+
struct tomoyo_task *s = tomoyo_task(current);
130100

131-
blob = tomoyo_cred(bprm->cred);
132-
domain = *blob;
133101
/*
134102
* Execute permission is checked against pathname passed to do_execve()
135103
* using current domain.
136104
*/
137-
if (!domain) {
105+
if (!s->old_domain_info) {
138106
const int idx = tomoyo_read_lock();
139107
const int err = tomoyo_find_next_domain(bprm);
140108
tomoyo_read_unlock(idx);
@@ -143,8 +111,8 @@ static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
143111
/*
144112
* Read permission is checked against interpreters using next domain.
145113
*/
146-
return tomoyo_check_open_permission(domain, &bprm->file->f_path,
147-
O_RDONLY);
114+
return tomoyo_check_open_permission(s->domain_info,
115+
&bprm->file->f_path, O_RDONLY);
148116
}
149117

150118
/**
@@ -510,19 +478,60 @@ static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
510478
}
511479

512480
struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = {
513-
.lbs_cred = sizeof(struct tomoyo_domain_info *),
481+
.lbs_task = sizeof(struct tomoyo_task),
514482
};
515483

484+
/**
485+
* tomoyo_task_alloc - Target for security_task_alloc().
486+
*
487+
* @task: Pointer to "struct task_struct".
488+
* @flags: clone() flags.
489+
*
490+
* Returns 0.
491+
*/
492+
static int tomoyo_task_alloc(struct task_struct *task,
493+
unsigned long clone_flags)
494+
{
495+
struct tomoyo_task *old = tomoyo_task(current);
496+
struct tomoyo_task *new = tomoyo_task(task);
497+
498+
new->domain_info = old->domain_info;
499+
atomic_inc(&new->domain_info->users);
500+
new->old_domain_info = NULL;
501+
return 0;
502+
}
503+
504+
/**
505+
* tomoyo_task_free - Target for security_task_free().
506+
*
507+
* @task: Pointer to "struct task_struct".
508+
*/
509+
static void tomoyo_task_free(struct task_struct *task)
510+
{
511+
struct tomoyo_task *s = tomoyo_task(task);
512+
513+
if (s->domain_info) {
514+
atomic_dec(&s->domain_info->users);
515+
s->domain_info = NULL;
516+
}
517+
if (s->old_domain_info) {
518+
atomic_dec(&s->old_domain_info->users);
519+
s->old_domain_info = NULL;
520+
}
521+
}
522+
516523
/*
517524
* tomoyo_security_ops is a "struct security_operations" which is used for
518525
* registering TOMOYO.
519526
*/
520527
static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
521-
LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank),
522528
LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
523-
LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer),
524-
LSM_HOOK_INIT(cred_free, tomoyo_cred_free),
529+
LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
530+
LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
531+
LSM_HOOK_INIT(task_free, tomoyo_task_free),
532+
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
525533
LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds),
534+
#endif
526535
LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
527536
LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
528537
LSM_HOOK_INIT(file_open, tomoyo_file_open),
@@ -560,14 +569,14 @@ int tomoyo_enabled __lsm_ro_after_init = 1;
560569
*/
561570
static int __init tomoyo_init(void)
562571
{
563-
struct cred *cred = (struct cred *) current_cred();
564-
struct tomoyo_domain_info **blob;
572+
struct tomoyo_task *s = tomoyo_task(current);
565573

566574
/* register ourselves with the security framework */
567575
security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
568576
printk(KERN_INFO "TOMOYO Linux initialized\n");
569-
blob = tomoyo_cred(cred);
570-
*blob = &tomoyo_kernel_domain;
577+
s->domain_info = &tomoyo_kernel_domain;
578+
atomic_inc(&tomoyo_kernel_domain.users);
579+
s->old_domain_info = NULL;
571580
tomoyo_mm_init();
572581

573582
return 0;

0 commit comments

Comments
 (0)