Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Tetsuo Handa and committed by
James Morris
8c6cb983 23711df7

+110 -124
+2 -2
security/tomoyo/common.c
··· 986 986 else 987 987 p = find_task_by_vpid(pid); 988 988 if (p) 989 - domain = tomoyo_real_domain(p); 989 + domain = tomoyo_task(p)->domain_info; 990 990 rcu_read_unlock(); 991 991 } else if (!strncmp(data, "domain=", 7)) { 992 992 if (tomoyo_domain_def(data + 7)) ··· 1668 1668 else 1669 1669 p = find_task_by_vpid(pid); 1670 1670 if (p) 1671 - domain = tomoyo_real_domain(p); 1671 + domain = tomoyo_task(p)->domain_info; 1672 1672 rcu_read_unlock(); 1673 1673 if (!domain) 1674 1674 return;
+14 -33
security/tomoyo/common.h
··· 686 686 u8 group; /* Group number to use. */ 687 687 bool is_deleted; /* Delete flag. */ 688 688 bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS]; 689 - atomic_t users; /* Number of referring credentials. */ 689 + atomic_t users; /* Number of referring tasks. */ 690 690 }; 691 691 692 692 /* ··· 913 913 const char *name; 914 914 }; 915 915 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 + 916 922 /********** Function prototypes. **********/ 917 923 918 924 bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address, ··· 1027 1021 struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param); 1028 1022 struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, 1029 1023 const bool transit); 1024 + struct tomoyo_domain_info *tomoyo_domain(void); 1030 1025 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname); 1031 1026 struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, 1032 1027 const u8 idx); ··· 1207 1200 } 1208 1201 1209 1202 /** 1210 - * tomoyo_cred - Get a pointer to the tomoyo cred security blob 1211 - * @cred - the relevant cred 1203 + * tomoyo_task - Get "struct tomoyo_task" for specified thread. 1212 1204 * 1213 - * Returns pointer to the tomoyo cred blob. 1205 + * @task - Pointer to "struct task_struct". 1206 + * 1207 + * Returns pointer to "struct tomoyo_task" for specified thread. 1214 1208 */ 1215 - static inline struct tomoyo_domain_info **tomoyo_cred(const struct cred *cred) 1209 + static inline struct tomoyo_task *tomoyo_task(struct task_struct *task) 1216 1210 { 1217 - return cred->security + tomoyo_blob_sizes.lbs_cred; 1218 - } 1219 - 1220 - /** 1221 - * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread. 1222 - * 1223 - * Returns pointer to "struct tomoyo_domain_info" for current thread. 1224 - */ 1225 - static inline struct tomoyo_domain_info *tomoyo_domain(void) 1226 - { 1227 - struct tomoyo_domain_info **blob = tomoyo_cred(current_cred()); 1228 - 1229 - return *blob; 1230 - } 1231 - 1232 - /** 1233 - * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread. 1234 - * 1235 - * @task: Pointer to "struct task_struct". 1236 - * 1237 - * Returns pointer to "struct tomoyo_security" for specified thread. 1238 - */ 1239 - static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct 1240 - *task) 1241 - { 1242 - struct tomoyo_domain_info **blob = tomoyo_cred(get_task_cred(task)); 1243 - 1244 - return *blob; 1211 + return task->security + tomoyo_blob_sizes.lbs_task; 1245 1212 } 1246 1213 1247 1214 /**
+7 -4
security/tomoyo/domain.c
··· 678 678 */ 679 679 int tomoyo_find_next_domain(struct linux_binprm *bprm) 680 680 { 681 - struct tomoyo_domain_info **blob; 682 681 struct tomoyo_domain_info *old_domain = tomoyo_domain(); 683 682 struct tomoyo_domain_info *domain = NULL; 684 683 const char *original_name = bprm->filename; ··· 842 843 if (!domain) 843 844 domain = old_domain; 844 845 /* Update reference count on "struct tomoyo_domain_info". */ 845 - atomic_inc(&domain->users); 846 - blob = tomoyo_cred(bprm->cred); 847 - *blob = domain; 846 + { 847 + struct tomoyo_task *s = tomoyo_task(current); 848 + 849 + s->old_domain_info = s->domain_info; 850 + s->domain_info = domain; 851 + atomic_inc(&domain->users); 852 + } 848 853 kfree(exename.name); 849 854 if (!retval) { 850 855 ee->r.domain = domain;
+7 -14
security/tomoyo/securityfs_if.c
··· 67 67 if (!new_domain) { 68 68 error = -ENOENT; 69 69 } 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; 76 73 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; 85 78 } 86 79 } 87 80 tomoyo_read_unlock(idx);
+80 -71
security/tomoyo/tomoyo.c
··· 9 9 #include "common.h" 10 10 11 11 /** 12 - * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank(). 12 + * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread. 13 13 * 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. 18 15 */ 19 - static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) 16 + struct tomoyo_domain_info *tomoyo_domain(void) 20 17 { 21 - struct tomoyo_domain_info **blob = tomoyo_cred(new); 18 + struct tomoyo_task *s = tomoyo_task(current); 22 19 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; 25 25 } 26 26 27 27 /** ··· 36 36 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old, 37 37 gfp_t gfp) 38 38 { 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; 39 + /* Restore old_domain_info saved by previous execve() request. */ 40 + struct tomoyo_task *s = tomoyo_task(current); 42 41 43 - domain = *old_blob; 44 - *new_blob = domain; 45 - 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 + } 48 47 return 0; 49 48 } 50 49 51 50 /** 52 - * tomoyo_cred_transfer - Target for security_transfer_creds(). 51 + * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds(). 53 52 * 54 - * @new: Pointer to "struct cred". 55 - * @old: Pointer to "struct cred". 53 + * @bprm: Pointer to "struct linux_binprm". 56 54 */ 57 - static void tomoyo_cred_transfer(struct cred *new, const struct cred *old) 55 + static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm) 58 56 { 59 - tomoyo_cred_prepare(new, old, 0); 57 + /* Clear old_domain_info saved by execve() request. */ 58 + struct tomoyo_task *s = tomoyo_task(current); 59 + 60 + atomic_dec(&s->old_domain_info->users); 61 + s->old_domain_info = NULL; 60 62 } 61 63 62 - /** 63 - * tomoyo_cred_free - Target for security_cred_free(). 64 - * 65 - * @cred: Pointer to "struct cred". 66 - */ 67 - static void tomoyo_cred_free(struct cred *cred) 68 - { 69 - struct tomoyo_domain_info **blob = tomoyo_cred(cred); 70 - struct tomoyo_domain_info *domain = *blob; 71 - 72 - if (domain) 73 - atomic_dec(&domain->users); 74 - } 75 - 64 + #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER 76 65 /** 77 66 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds(). 78 67 * 79 68 * @bprm: Pointer to "struct linux_binprm". 80 69 * 81 - * Returns 0 on success, negative value otherwise. 70 + * Returns 0. 82 71 */ 83 72 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) 84 73 { 85 - struct tomoyo_domain_info **blob; 86 - struct tomoyo_domain_info *domain; 87 - 88 74 /* 89 75 * Do only if this function is called for the first time of an execve 90 76 * operation. 91 77 */ 92 78 if (bprm->called_set_creds) 93 79 return 0; 94 - #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER 95 80 /* 96 81 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested 97 82 * for the first time. 98 83 */ 99 84 if (!tomoyo_policy_loaded) 100 85 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; 116 86 return 0; 117 87 } 88 + #endif 118 89 119 90 /** 120 91 * tomoyo_bprm_check_security - Target for security_bprm_check(). ··· 96 125 */ 97 126 static int tomoyo_bprm_check_security(struct linux_binprm *bprm) 98 127 { 99 - struct tomoyo_domain_info **blob; 100 - struct tomoyo_domain_info *domain; 128 + struct tomoyo_task *s = tomoyo_task(current); 101 129 102 - blob = tomoyo_cred(bprm->cred); 103 - domain = *blob; 104 130 /* 105 131 * Execute permission is checked against pathname passed to do_execve() 106 132 * using current domain. 107 133 */ 108 - if (!domain) { 134 + if (!s->old_domain_info) { 109 135 const int idx = tomoyo_read_lock(); 110 136 const int err = tomoyo_find_next_domain(bprm); 111 137 tomoyo_read_unlock(idx); ··· 111 143 /* 112 144 * Read permission is checked against interpreters using next domain. 113 145 */ 114 - return tomoyo_check_open_permission(domain, &bprm->file->f_path, 115 - O_RDONLY); 146 + return tomoyo_check_open_permission(s->domain_info, 147 + &bprm->file->f_path, O_RDONLY); 116 148 } 117 149 118 150 /** ··· 478 510 } 479 511 480 512 struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = { 481 - .lbs_cred = sizeof(struct tomoyo_domain_info *), 513 + .lbs_task = sizeof(struct tomoyo_task), 482 514 }; 515 + 516 + /** 517 + * tomoyo_task_alloc - Target for security_task_alloc(). 518 + * 519 + * @task: Pointer to "struct task_struct". 520 + * @flags: clone() flags. 521 + * 522 + * Returns 0. 523 + */ 524 + static int tomoyo_task_alloc(struct task_struct *task, 525 + unsigned long clone_flags) 526 + { 527 + struct tomoyo_task *old = tomoyo_task(current); 528 + struct tomoyo_task *new = tomoyo_task(task); 529 + 530 + new->domain_info = old->domain_info; 531 + atomic_inc(&new->domain_info->users); 532 + new->old_domain_info = NULL; 533 + return 0; 534 + } 535 + 536 + /** 537 + * tomoyo_task_free - Target for security_task_free(). 538 + * 539 + * @task: Pointer to "struct task_struct". 540 + */ 541 + static void tomoyo_task_free(struct task_struct *task) 542 + { 543 + struct tomoyo_task *s = tomoyo_task(task); 544 + 545 + if (s->domain_info) { 546 + atomic_dec(&s->domain_info->users); 547 + s->domain_info = NULL; 548 + } 549 + if (s->old_domain_info) { 550 + atomic_dec(&s->old_domain_info->users); 551 + s->old_domain_info = NULL; 552 + } 553 + } 483 554 484 555 /* 485 556 * tomoyo_security_ops is a "struct security_operations" which is used for 486 557 * registering TOMOYO. 487 558 */ 488 559 static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = { 489 - LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank), 490 560 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare), 491 - LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer), 492 - LSM_HOOK_INIT(cred_free, tomoyo_cred_free), 561 + LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds), 562 + LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc), 563 + LSM_HOOK_INIT(task_free, tomoyo_task_free), 564 + #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER 493 565 LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds), 566 + #endif 494 567 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security), 495 568 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl), 496 569 LSM_HOOK_INIT(file_open, tomoyo_file_open), ··· 569 560 */ 570 561 static int __init tomoyo_init(void) 571 562 { 572 - struct cred *cred = (struct cred *) current_cred(); 573 - struct tomoyo_domain_info **blob; 563 + struct tomoyo_task *s = tomoyo_task(current); 574 564 575 565 /* register ourselves with the security framework */ 576 566 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo"); 577 567 printk(KERN_INFO "TOMOYO Linux initialized\n"); 578 - blob = tomoyo_cred(cred); 579 - *blob = &tomoyo_kernel_domain; 568 + s->domain_info = &tomoyo_kernel_domain; 569 + atomic_inc(&tomoyo_kernel_domain.users); 570 + s->old_domain_info = NULL; 580 571 tomoyo_mm_init(); 581 572 582 573 return 0;