Skip to content

Commit ec8e6a4

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Add refcounter on domain structure.
Add refcounter to "struct tomoyo_domain_info" since garbage collector needs to determine whether this struct is referred by "struct cred"->security or not. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 76bb089 commit ec8e6a4

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

‎security/tomoyo/common.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ struct tomoyo_acl_info {
234234
* name of the domain to be created was too long or it could not allocate
235235
* memory. If set to true, more than one process continued execve()
236236
* without domain transition.
237+
* (9) "users" is an atomic_t that holds how many "struct cred"->security
238+
* are referring this "struct tomoyo_domain_info". If is_deleted == true
239+
* and users == 0, this struct will be kfree()d upon next garbage
240+
* collection.
237241
*
238242
* A domain's lifecycle is an analogy of files on / directory.
239243
* Multiple domains with the same domainname cannot be created (as with
@@ -252,6 +256,7 @@ struct tomoyo_domain_info {
252256
bool quota_warned; /* Quota warnning flag. */
253257
bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
254258
bool transition_failed; /* Domain transition failed flag. */
259+
atomic_t users; /* Number of referring credentials. */
255260
};
256261

257262
/*

‎security/tomoyo/domain.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
817817
out:
818818
if (!domain)
819819
domain = old_domain;
820+
/* Update reference count on "struct tomoyo_domain_info". */
821+
atomic_inc(&domain->users);
820822
bprm->cred->security = domain;
821823
kfree(real_program_name);
822824
kfree(symlink_program_name);

‎security/tomoyo/tomoyo.c‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,23 @@ static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
2121
static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
2222
gfp_t gfp)
2323
{
24-
/*
25-
* Since "struct tomoyo_domain_info *" is a sharable pointer,
26-
* we don't need to duplicate.
27-
*/
28-
new->security = old->security;
24+
struct tomoyo_domain_info *domain = old->security;
25+
new->security = domain;
26+
if (domain)
27+
atomic_inc(&domain->users);
2928
return 0;
3029
}
3130

3231
static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
3332
{
34-
/*
35-
* Since "struct tomoyo_domain_info *" is a sharable pointer,
36-
* we don't need to duplicate.
37-
*/
38-
new->security = old->security;
33+
tomoyo_cred_prepare(new, old, 0);
34+
}
35+
36+
static void tomoyo_cred_free(struct cred *cred)
37+
{
38+
struct tomoyo_domain_info *domain = cred->security;
39+
if (domain)
40+
atomic_dec(&domain->users);
3941
}
4042

4143
static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
@@ -58,6 +60,14 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
5860
*/
5961
if (!tomoyo_policy_loaded)
6062
tomoyo_load_policy(bprm->filename);
63+
/*
64+
* Release reference to "struct tomoyo_domain_info" stored inside
65+
* "bprm->cred->security". New reference to "struct tomoyo_domain_info"
66+
* stored inside "bprm->cred->security" will be acquired later inside
67+
* tomoyo_find_next_domain().
68+
*/
69+
atomic_dec(&((struct tomoyo_domain_info *)
70+
bprm->cred->security)->users);
6171
/*
6272
* Tell tomoyo_bprm_check_security() is called for the first time of an
6373
* execve operation.
@@ -75,12 +85,6 @@ static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
7585
* using current domain.
7686
*/
7787
if (!domain) {
78-
/*
79-
* We will need to protect whole execve() operation when GC
80-
* starts kfree()ing "struct tomoyo_domain_info" because
81-
* bprm->cred->security points to "struct tomoyo_domain_info"
82-
* but "struct tomoyo_domain_info" does not have a refcounter.
83-
*/
8488
const int idx = tomoyo_read_lock();
8589
const int err = tomoyo_find_next_domain(bprm);
8690
tomoyo_read_unlock(idx);
@@ -265,6 +269,7 @@ static struct security_operations tomoyo_security_ops = {
265269
.cred_alloc_blank = tomoyo_cred_alloc_blank,
266270
.cred_prepare = tomoyo_cred_prepare,
267271
.cred_transfer = tomoyo_cred_transfer,
272+
.cred_free = tomoyo_cred_free,
268273
.bprm_set_creds = tomoyo_bprm_set_creds,
269274
.bprm_check_security = tomoyo_bprm_check_security,
270275
.file_fcntl = tomoyo_file_fcntl,

0 commit comments

Comments
 (0)