Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/gc.c |
| 3 | * |
| 4 | * Implementation of the Domain-Based Mandatory Access Control. |
| 5 | * |
| 6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include "common.h" |
| 11 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 13 | |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 14 | enum tomoyo_policy_id { |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 15 | TOMOYO_ID_GROUP, |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 16 | TOMOYO_ID_PATH_GROUP, |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 17 | TOMOYO_ID_NUMBER_GROUP, |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 18 | TOMOYO_ID_DOMAIN_INITIALIZER, |
| 19 | TOMOYO_ID_DOMAIN_KEEPER, |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 20 | TOMOYO_ID_AGGREGATOR, |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 21 | TOMOYO_ID_ALIAS, |
| 22 | TOMOYO_ID_GLOBALLY_READABLE, |
| 23 | TOMOYO_ID_PATTERN, |
| 24 | TOMOYO_ID_NO_REWRITE, |
| 25 | TOMOYO_ID_MANAGER, |
| 26 | TOMOYO_ID_NAME, |
| 27 | TOMOYO_ID_ACL, |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 28 | TOMOYO_ID_DOMAIN, |
| 29 | TOMOYO_MAX_POLICY |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct tomoyo_gc_entry { |
| 33 | struct list_head list; |
| 34 | int type; |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 35 | struct list_head *element; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 36 | }; |
| 37 | static LIST_HEAD(tomoyo_gc_queue); |
| 38 | static DEFINE_MUTEX(tomoyo_gc_mutex); |
| 39 | |
| 40 | /* Caller holds tomoyo_policy_lock mutex. */ |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 41 | static bool tomoyo_add_to_gc(const int type, struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 42 | { |
| 43 | struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
| 44 | if (!entry) |
| 45 | return false; |
| 46 | entry->type = type; |
| 47 | entry->element = element; |
| 48 | list_add(&entry->list, &tomoyo_gc_queue); |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 49 | list_del_rcu(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 53 | static void tomoyo_del_allow_read(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 54 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 55 | struct tomoyo_globally_readable_file_entry *ptr = |
| 56 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 57 | tomoyo_put_name(ptr->filename); |
| 58 | } |
| 59 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 60 | static void tomoyo_del_file_pattern(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 61 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 62 | struct tomoyo_pattern_entry *ptr = |
| 63 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 64 | tomoyo_put_name(ptr->pattern); |
| 65 | } |
| 66 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 67 | static void tomoyo_del_no_rewrite(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 68 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 69 | struct tomoyo_no_rewrite_entry *ptr = |
| 70 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 71 | tomoyo_put_name(ptr->pattern); |
| 72 | } |
| 73 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 74 | static void tomoyo_del_domain_initializer(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 75 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 76 | struct tomoyo_domain_initializer_entry *ptr = |
| 77 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 78 | tomoyo_put_name(ptr->domainname); |
| 79 | tomoyo_put_name(ptr->program); |
| 80 | } |
| 81 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 82 | static void tomoyo_del_domain_keeper(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 83 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 84 | struct tomoyo_domain_keeper_entry *ptr = |
| 85 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 86 | tomoyo_put_name(ptr->domainname); |
| 87 | tomoyo_put_name(ptr->program); |
| 88 | } |
| 89 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 90 | static void tomoyo_del_aggregator(struct list_head *element) |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 91 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 92 | struct tomoyo_aggregator_entry *ptr = |
| 93 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 94 | tomoyo_put_name(ptr->original_name); |
| 95 | tomoyo_put_name(ptr->aggregated_name); |
| 96 | } |
| 97 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 98 | static void tomoyo_del_alias(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 99 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 100 | struct tomoyo_alias_entry *ptr = |
| 101 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 102 | tomoyo_put_name(ptr->original_name); |
| 103 | tomoyo_put_name(ptr->aliased_name); |
| 104 | } |
| 105 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 106 | static void tomoyo_del_manager(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 107 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 108 | struct tomoyo_policy_manager_entry *ptr = |
| 109 | container_of(element, typeof(*ptr), head.list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 110 | tomoyo_put_name(ptr->manager); |
| 111 | } |
| 112 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 113 | static void tomoyo_del_acl(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 114 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 115 | struct tomoyo_acl_info *acl = |
| 116 | container_of(element, typeof(*acl), list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 117 | switch (acl->type) { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 118 | case TOMOYO_TYPE_PATH_ACL: |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 119 | { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 120 | struct tomoyo_path_acl *entry |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 121 | = container_of(acl, typeof(*entry), head); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 122 | tomoyo_put_name_union(&entry->name); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 123 | } |
| 124 | break; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 125 | case TOMOYO_TYPE_PATH2_ACL: |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 126 | { |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 127 | struct tomoyo_path2_acl *entry |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 128 | = container_of(acl, typeof(*entry), head); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 129 | tomoyo_put_name_union(&entry->name1); |
| 130 | tomoyo_put_name_union(&entry->name2); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 131 | } |
| 132 | break; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 133 | case TOMOYO_TYPE_PATH_NUMBER_ACL: |
| 134 | { |
| 135 | struct tomoyo_path_number_acl *entry |
| 136 | = container_of(acl, typeof(*entry), head); |
| 137 | tomoyo_put_name_union(&entry->name); |
| 138 | tomoyo_put_number_union(&entry->number); |
| 139 | } |
| 140 | break; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 141 | case TOMOYO_TYPE_MKDEV_ACL: |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 142 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 143 | struct tomoyo_mkdev_acl *entry |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 144 | = container_of(acl, typeof(*entry), head); |
| 145 | tomoyo_put_name_union(&entry->name); |
| 146 | tomoyo_put_number_union(&entry->mode); |
| 147 | tomoyo_put_number_union(&entry->major); |
| 148 | tomoyo_put_number_union(&entry->minor); |
| 149 | } |
| 150 | break; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 151 | case TOMOYO_TYPE_MOUNT_ACL: |
| 152 | { |
| 153 | struct tomoyo_mount_acl *entry |
| 154 | = container_of(acl, typeof(*entry), head); |
| 155 | tomoyo_put_name_union(&entry->dev_name); |
| 156 | tomoyo_put_name_union(&entry->dir_name); |
| 157 | tomoyo_put_name_union(&entry->fs_type); |
| 158 | tomoyo_put_number_union(&entry->flags); |
| 159 | } |
| 160 | break; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 164 | static bool tomoyo_del_domain(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 165 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 166 | struct tomoyo_domain_info *domain = |
| 167 | container_of(element, typeof(*domain), list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 168 | struct tomoyo_acl_info *acl; |
| 169 | struct tomoyo_acl_info *tmp; |
| 170 | /* |
| 171 | * Since we don't protect whole execve() operation using SRCU, |
| 172 | * we need to recheck domain->users at this point. |
| 173 | * |
| 174 | * (1) Reader starts SRCU section upon execve(). |
| 175 | * (2) Reader traverses tomoyo_domain_list and finds this domain. |
| 176 | * (3) Writer marks this domain as deleted. |
| 177 | * (4) Garbage collector removes this domain from tomoyo_domain_list |
| 178 | * because this domain is marked as deleted and used by nobody. |
| 179 | * (5) Reader saves reference to this domain into |
| 180 | * "struct linux_binprm"->cred->security . |
| 181 | * (6) Reader finishes SRCU section, although execve() operation has |
| 182 | * not finished yet. |
| 183 | * (7) Garbage collector waits for SRCU synchronization. |
| 184 | * (8) Garbage collector kfree() this domain because this domain is |
| 185 | * used by nobody. |
| 186 | * (9) Reader finishes execve() operation and restores this domain from |
| 187 | * "struct linux_binprm"->cred->security. |
| 188 | * |
| 189 | * By updating domain->users at (5), we can solve this race problem |
| 190 | * by rechecking domain->users at (8). |
| 191 | */ |
| 192 | if (atomic_read(&domain->users)) |
| 193 | return false; |
| 194 | list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 195 | tomoyo_del_acl(&acl->list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 196 | tomoyo_memory_free(acl); |
| 197 | } |
| 198 | tomoyo_put_name(domain->domainname); |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 203 | static void tomoyo_del_name(struct list_head *element) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 204 | { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 205 | const struct tomoyo_name_entry *ptr = |
| 206 | container_of(element, typeof(*ptr), list); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 209 | static void tomoyo_del_path_group(struct list_head *element) |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 210 | { |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 211 | struct tomoyo_path_group *member = |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 212 | container_of(element, typeof(*member), head.list); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 213 | tomoyo_put_name(member->member_name); |
| 214 | } |
| 215 | |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 216 | static void tomoyo_del_group(struct list_head *element) |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 217 | { |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 218 | struct tomoyo_group *group = |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 219 | container_of(element, typeof(*group), list); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 220 | tomoyo_put_name(group->group_name); |
| 221 | } |
| 222 | |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 223 | static void tomoyo_del_number_group(struct list_head *element) |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 224 | { |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 225 | struct tomoyo_number_group *member = |
| 226 | container_of(element, typeof(*member), head.list); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 229 | static struct list_head *tomoyo_policy_list[TOMOYO_MAX_POLICY] = { |
| 230 | [TOMOYO_ID_GLOBALLY_READABLE] = &tomoyo_globally_readable_list, |
| 231 | [TOMOYO_ID_PATTERN] = &tomoyo_pattern_list, |
| 232 | [TOMOYO_ID_NO_REWRITE] = &tomoyo_no_rewrite_list, |
| 233 | [TOMOYO_ID_DOMAIN_INITIALIZER] = &tomoyo_domain_initializer_list, |
| 234 | [TOMOYO_ID_DOMAIN_KEEPER] = &tomoyo_domain_keeper_list, |
| 235 | [TOMOYO_ID_AGGREGATOR] = &tomoyo_aggregator_list, |
| 236 | [TOMOYO_ID_ALIAS] = &tomoyo_alias_list, |
| 237 | [TOMOYO_ID_MANAGER] = &tomoyo_policy_manager_list, |
| 238 | }; |
| 239 | |
| 240 | static bool tomoyo_collect_member(struct list_head *member_list, int id) |
| 241 | { |
| 242 | struct tomoyo_acl_head *member; |
| 243 | list_for_each_entry(member, member_list, list) { |
| 244 | if (!member->is_deleted) |
| 245 | continue; |
| 246 | if (!tomoyo_add_to_gc(id, &member->list)) |
| 247 | return false; |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain) |
| 253 | { |
| 254 | struct tomoyo_acl_info *acl; |
| 255 | list_for_each_entry(acl, &domain->acl_info_list, list) { |
| 256 | if (!acl->is_deleted) |
| 257 | continue; |
| 258 | if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list)) |
| 259 | return false; |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 264 | static void tomoyo_collect_entry(void) |
| 265 | { |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 266 | int i; |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 267 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 268 | return; |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 269 | for (i = 0; i < TOMOYO_MAX_POLICY; i++) { |
| 270 | if (tomoyo_policy_list[i]) |
| 271 | if (!tomoyo_collect_member(tomoyo_policy_list[i], i)) |
| 272 | goto unlock; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 273 | } |
| 274 | { |
| 275 | struct tomoyo_domain_info *domain; |
| 276 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 277 | if (!tomoyo_collect_acl(domain)) |
| 278 | goto unlock; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 279 | if (!domain->is_deleted || atomic_read(&domain->users)) |
| 280 | continue; |
| 281 | /* |
| 282 | * Nobody is referring this domain. But somebody may |
| 283 | * refer this domain after successful execve(). |
| 284 | * We recheck domain->users after SRCU synchronization. |
| 285 | */ |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 286 | if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list)) |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 287 | goto unlock; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 288 | } |
| 289 | } |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 290 | for (i = 0; i < TOMOYO_MAX_HASH; i++) { |
| 291 | struct tomoyo_name_entry *ptr; |
| 292 | list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) { |
| 293 | if (atomic_read(&ptr->users)) |
| 294 | continue; |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 295 | if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->list)) |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 296 | goto unlock; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 297 | } |
| 298 | } |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 299 | { |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 300 | struct tomoyo_group *group; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 301 | list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) { |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 302 | tomoyo_collect_member(&group->member_list, |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 303 | TOMOYO_ID_PATH_GROUP); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 304 | if (!list_empty(&group->member_list) || |
| 305 | atomic_read(&group->users)) |
| 306 | continue; |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 307 | if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 308 | &group->list)) |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 309 | goto unlock; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 310 | } |
| 311 | } |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 312 | { |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 313 | struct tomoyo_group *group; |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 314 | list_for_each_entry_rcu(group, &tomoyo_number_group_list, |
| 315 | list) { |
| 316 | tomoyo_collect_member(&group->member_list, |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 317 | TOMOYO_ID_NUMBER_GROUP); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 318 | if (!list_empty(&group->member_list) || |
| 319 | atomic_read(&group->users)) |
| 320 | continue; |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 321 | if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 322 | &group->list)) |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 323 | goto unlock; |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 324 | } |
| 325 | } |
Tetsuo Handa | d2f8b23 | 2010-06-15 10:10:37 +0900 | [diff] [blame] | 326 | unlock: |
Tetsuo Handa | 2928238 | 2010-05-06 00:18:15 +0900 | [diff] [blame] | 327 | mutex_unlock(&tomoyo_policy_lock); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void tomoyo_kfree_entry(void) |
| 331 | { |
| 332 | struct tomoyo_gc_entry *p; |
| 333 | struct tomoyo_gc_entry *tmp; |
| 334 | |
| 335 | list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) { |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 336 | struct list_head *element = p->element; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 337 | switch (p->type) { |
| 338 | case TOMOYO_ID_DOMAIN_INITIALIZER: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 339 | tomoyo_del_domain_initializer(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 340 | break; |
| 341 | case TOMOYO_ID_DOMAIN_KEEPER: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 342 | tomoyo_del_domain_keeper(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 343 | break; |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 344 | case TOMOYO_ID_AGGREGATOR: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 345 | tomoyo_del_aggregator(element); |
Tetsuo Handa | 1084307 | 2010-06-03 20:38:03 +0900 | [diff] [blame] | 346 | break; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 347 | case TOMOYO_ID_ALIAS: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 348 | tomoyo_del_alias(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 349 | break; |
| 350 | case TOMOYO_ID_GLOBALLY_READABLE: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 351 | tomoyo_del_allow_read(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 352 | break; |
| 353 | case TOMOYO_ID_PATTERN: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 354 | tomoyo_del_file_pattern(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 355 | break; |
| 356 | case TOMOYO_ID_NO_REWRITE: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 357 | tomoyo_del_no_rewrite(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 358 | break; |
| 359 | case TOMOYO_ID_MANAGER: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 360 | tomoyo_del_manager(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 361 | break; |
| 362 | case TOMOYO_ID_NAME: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 363 | tomoyo_del_name(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 364 | break; |
| 365 | case TOMOYO_ID_ACL: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 366 | tomoyo_del_acl(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 367 | break; |
| 368 | case TOMOYO_ID_DOMAIN: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 369 | if (!tomoyo_del_domain(element)) |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 370 | continue; |
| 371 | break; |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 372 | case TOMOYO_ID_PATH_GROUP: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 373 | tomoyo_del_path_group(element); |
Tetsuo Handa | 7762fbf | 2010-05-10 17:30:26 +0900 | [diff] [blame] | 374 | break; |
Tetsuo Handa | a98aa4d | 2010-06-17 16:52:29 +0900 | [diff] [blame^] | 375 | case TOMOYO_ID_GROUP: |
| 376 | tomoyo_del_group(element); |
Tetsuo Handa | 4c3e9e2 | 2010-05-17 10:06:58 +0900 | [diff] [blame] | 377 | break; |
| 378 | case TOMOYO_ID_NUMBER_GROUP: |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 379 | tomoyo_del_number_group(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 380 | break; |
| 381 | } |
Tetsuo Handa | e79acf0 | 2010-06-16 16:31:50 +0900 | [diff] [blame] | 382 | tomoyo_memory_free(element); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 383 | list_del(&p->list); |
| 384 | kfree(p); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | static int tomoyo_gc_thread(void *unused) |
| 389 | { |
| 390 | daemonize("GC for TOMOYO"); |
| 391 | if (mutex_trylock(&tomoyo_gc_mutex)) { |
| 392 | int i; |
| 393 | for (i = 0; i < 10; i++) { |
| 394 | tomoyo_collect_entry(); |
| 395 | if (list_empty(&tomoyo_gc_queue)) |
| 396 | break; |
| 397 | synchronize_srcu(&tomoyo_ss); |
| 398 | tomoyo_kfree_entry(); |
| 399 | } |
| 400 | mutex_unlock(&tomoyo_gc_mutex); |
| 401 | } |
| 402 | do_exit(0); |
| 403 | } |
| 404 | |
| 405 | void tomoyo_run_gc(void) |
| 406 | { |
| 407 | struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL, |
| 408 | "GC for TOMOYO"); |
| 409 | if (!IS_ERR(task)) |
| 410 | wake_up_process(task); |
| 411 | } |