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