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)
3636static 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 */
8372static 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 */
12697static 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
512480struct 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 */
520527static 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 */
561570static 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