blob: 4d4ba84f8749f0373dbce66e7d97c802ac53f691 [file] [log] [blame]
Tetsuo Handa847b1732010-02-11 09:43:54 +09001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Tetsuo Handa847b1732010-02-11 09:43:54 +090013
Tetsuo Handa847b1732010-02-11 09:43:54 +090014struct tomoyo_gc_entry {
15 struct list_head list;
16 int type;
Tetsuo Handae79acf02010-06-16 16:31:50 +090017 struct list_head *element;
Tetsuo Handa847b1732010-02-11 09:43:54 +090018};
19static LIST_HEAD(tomoyo_gc_queue);
20static DEFINE_MUTEX(tomoyo_gc_mutex);
21
22/* Caller holds tomoyo_policy_lock mutex. */
Tetsuo Handae79acf02010-06-16 16:31:50 +090023static bool tomoyo_add_to_gc(const int type, struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090024{
25 struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
26 if (!entry)
27 return false;
28 entry->type = type;
29 entry->element = element;
30 list_add(&entry->list, &tomoyo_gc_queue);
Tetsuo Handae79acf02010-06-16 16:31:50 +090031 list_del_rcu(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +090032 return true;
33}
34
Tetsuo Handae79acf02010-06-16 16:31:50 +090035static void tomoyo_del_allow_read(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090036{
Tetsuo Handae79acf02010-06-16 16:31:50 +090037 struct tomoyo_globally_readable_file_entry *ptr =
38 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090039 tomoyo_put_name(ptr->filename);
40}
41
Tetsuo Handae79acf02010-06-16 16:31:50 +090042static void tomoyo_del_file_pattern(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090043{
Tetsuo Handae79acf02010-06-16 16:31:50 +090044 struct tomoyo_pattern_entry *ptr =
45 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090046 tomoyo_put_name(ptr->pattern);
47}
48
Tetsuo Handae79acf02010-06-16 16:31:50 +090049static void tomoyo_del_no_rewrite(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090050{
Tetsuo Handae79acf02010-06-16 16:31:50 +090051 struct tomoyo_no_rewrite_entry *ptr =
52 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090053 tomoyo_put_name(ptr->pattern);
54}
55
Tetsuo Handae79acf02010-06-16 16:31:50 +090056static void tomoyo_del_domain_initializer(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090057{
Tetsuo Handae79acf02010-06-16 16:31:50 +090058 struct tomoyo_domain_initializer_entry *ptr =
59 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090060 tomoyo_put_name(ptr->domainname);
61 tomoyo_put_name(ptr->program);
62}
63
Tetsuo Handae79acf02010-06-16 16:31:50 +090064static void tomoyo_del_domain_keeper(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090065{
Tetsuo Handae79acf02010-06-16 16:31:50 +090066 struct tomoyo_domain_keeper_entry *ptr =
67 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090068 tomoyo_put_name(ptr->domainname);
69 tomoyo_put_name(ptr->program);
70}
71
Tetsuo Handae79acf02010-06-16 16:31:50 +090072static void tomoyo_del_aggregator(struct list_head *element)
Tetsuo Handa10843072010-06-03 20:38:03 +090073{
Tetsuo Handae79acf02010-06-16 16:31:50 +090074 struct tomoyo_aggregator_entry *ptr =
75 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa10843072010-06-03 20:38:03 +090076 tomoyo_put_name(ptr->original_name);
77 tomoyo_put_name(ptr->aggregated_name);
78}
79
Tetsuo Handae79acf02010-06-16 16:31:50 +090080static void tomoyo_del_manager(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090081{
Tetsuo Handae79acf02010-06-16 16:31:50 +090082 struct tomoyo_policy_manager_entry *ptr =
83 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090084 tomoyo_put_name(ptr->manager);
85}
86
Tetsuo Handae79acf02010-06-16 16:31:50 +090087static void tomoyo_del_acl(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090088{
Tetsuo Handae79acf02010-06-16 16:31:50 +090089 struct tomoyo_acl_info *acl =
90 container_of(element, typeof(*acl), list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090091 switch (acl->type) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090092 case TOMOYO_TYPE_PATH_ACL:
Tetsuo Handa847b1732010-02-11 09:43:54 +090093 {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090094 struct tomoyo_path_acl *entry
Tetsuo Handa847b1732010-02-11 09:43:54 +090095 = container_of(acl, typeof(*entry), head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090096 tomoyo_put_name_union(&entry->name);
Tetsuo Handa847b1732010-02-11 09:43:54 +090097 }
98 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +090099 case TOMOYO_TYPE_PATH2_ACL:
Tetsuo Handa847b1732010-02-11 09:43:54 +0900100 {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900101 struct tomoyo_path2_acl *entry
Tetsuo Handa847b1732010-02-11 09:43:54 +0900102 = container_of(acl, typeof(*entry), head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900103 tomoyo_put_name_union(&entry->name1);
104 tomoyo_put_name_union(&entry->name2);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900105 }
106 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900107 case TOMOYO_TYPE_PATH_NUMBER_ACL:
108 {
109 struct tomoyo_path_number_acl *entry
110 = container_of(acl, typeof(*entry), head);
111 tomoyo_put_name_union(&entry->name);
112 tomoyo_put_number_union(&entry->number);
113 }
114 break;
Tetsuo Handa75093152010-06-16 16:23:55 +0900115 case TOMOYO_TYPE_MKDEV_ACL:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900116 {
Tetsuo Handa75093152010-06-16 16:23:55 +0900117 struct tomoyo_mkdev_acl *entry
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900118 = container_of(acl, typeof(*entry), head);
119 tomoyo_put_name_union(&entry->name);
120 tomoyo_put_number_union(&entry->mode);
121 tomoyo_put_number_union(&entry->major);
122 tomoyo_put_number_union(&entry->minor);
123 }
124 break;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900125 case TOMOYO_TYPE_MOUNT_ACL:
126 {
127 struct tomoyo_mount_acl *entry
128 = container_of(acl, typeof(*entry), head);
129 tomoyo_put_name_union(&entry->dev_name);
130 tomoyo_put_name_union(&entry->dir_name);
131 tomoyo_put_name_union(&entry->fs_type);
132 tomoyo_put_number_union(&entry->flags);
133 }
134 break;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900135 }
136}
137
Tetsuo Handae79acf02010-06-16 16:31:50 +0900138static bool tomoyo_del_domain(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +0900139{
Tetsuo Handae79acf02010-06-16 16:31:50 +0900140 struct tomoyo_domain_info *domain =
141 container_of(element, typeof(*domain), list);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900142 struct tomoyo_acl_info *acl;
143 struct tomoyo_acl_info *tmp;
144 /*
145 * Since we don't protect whole execve() operation using SRCU,
146 * we need to recheck domain->users at this point.
147 *
148 * (1) Reader starts SRCU section upon execve().
149 * (2) Reader traverses tomoyo_domain_list and finds this domain.
150 * (3) Writer marks this domain as deleted.
151 * (4) Garbage collector removes this domain from tomoyo_domain_list
152 * because this domain is marked as deleted and used by nobody.
153 * (5) Reader saves reference to this domain into
154 * "struct linux_binprm"->cred->security .
155 * (6) Reader finishes SRCU section, although execve() operation has
156 * not finished yet.
157 * (7) Garbage collector waits for SRCU synchronization.
158 * (8) Garbage collector kfree() this domain because this domain is
159 * used by nobody.
160 * (9) Reader finishes execve() operation and restores this domain from
161 * "struct linux_binprm"->cred->security.
162 *
163 * By updating domain->users at (5), we can solve this race problem
164 * by rechecking domain->users at (8).
165 */
166 if (atomic_read(&domain->users))
167 return false;
168 list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
Tetsuo Handae79acf02010-06-16 16:31:50 +0900169 tomoyo_del_acl(&acl->list);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900170 tomoyo_memory_free(acl);
171 }
172 tomoyo_put_name(domain->domainname);
173 return true;
174}
175
176
Tetsuo Handae79acf02010-06-16 16:31:50 +0900177static void tomoyo_del_name(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +0900178{
Tetsuo Handae79acf02010-06-16 16:31:50 +0900179 const struct tomoyo_name_entry *ptr =
180 container_of(element, typeof(*ptr), list);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900181}
182
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900183static void tomoyo_del_path_group(struct list_head *element)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900184{
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900185 struct tomoyo_path_group *member =
Tetsuo Handae79acf02010-06-16 16:31:50 +0900186 container_of(element, typeof(*member), head.list);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900187 tomoyo_put_name(member->member_name);
188}
189
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900190static void tomoyo_del_group(struct list_head *element)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900191{
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900192 struct tomoyo_group *group =
Tetsuo Handae79acf02010-06-16 16:31:50 +0900193 container_of(element, typeof(*group), list);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900194 tomoyo_put_name(group->group_name);
195}
196
Tetsuo Handae79acf02010-06-16 16:31:50 +0900197static void tomoyo_del_number_group(struct list_head *element)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900198{
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900199 struct tomoyo_number_group *member =
200 container_of(element, typeof(*member), head.list);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900201}
202
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900203static bool tomoyo_collect_member(struct list_head *member_list, int id)
204{
205 struct tomoyo_acl_head *member;
206 list_for_each_entry(member, member_list, list) {
207 if (!member->is_deleted)
208 continue;
209 if (!tomoyo_add_to_gc(id, &member->list))
210 return false;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900211 }
212 return true;
213}
214
215static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain)
216{
217 struct tomoyo_acl_info *acl;
218 list_for_each_entry(acl, &domain->acl_info_list, list) {
219 if (!acl->is_deleted)
220 continue;
221 if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
222 return false;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900223 }
224 return true;
225}
226
Tetsuo Handa847b1732010-02-11 09:43:54 +0900227static void tomoyo_collect_entry(void)
228{
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900229 int i;
Tetsuo Handa29282382010-05-06 00:18:15 +0900230 if (mutex_lock_interruptible(&tomoyo_policy_lock))
231 return;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900232 for (i = 0; i < TOMOYO_MAX_POLICY; i++) {
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900233 if (!tomoyo_collect_member(&tomoyo_policy_list[i], i))
234 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900235 }
236 {
237 struct tomoyo_domain_info *domain;
238 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900239 if (!tomoyo_collect_acl(domain))
240 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900241 if (!domain->is_deleted || atomic_read(&domain->users))
242 continue;
243 /*
244 * Nobody is referring this domain. But somebody may
245 * refer this domain after successful execve().
246 * We recheck domain->users after SRCU synchronization.
247 */
Tetsuo Handae79acf02010-06-16 16:31:50 +0900248 if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900249 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900250 }
251 }
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900252 for (i = 0; i < TOMOYO_MAX_HASH; i++) {
253 struct tomoyo_name_entry *ptr;
254 list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) {
255 if (atomic_read(&ptr->users))
256 continue;
Tetsuo Handae79acf02010-06-16 16:31:50 +0900257 if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->list))
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900258 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900259 }
260 }
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900261 for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
262 struct list_head *list = &tomoyo_group_list[i];
263 int id;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900264 struct tomoyo_group *group;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900265 switch (i) {
266 case 0:
267 id = TOMOYO_ID_PATH_GROUP;
268 break;
269 default:
270 id = TOMOYO_ID_NUMBER_GROUP;
271 break;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900272 }
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900273 list_for_each_entry(group, list, list) {
274 if (!tomoyo_collect_member(&group->member_list, id))
275 goto unlock;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900276 if (!list_empty(&group->member_list) ||
277 atomic_read(&group->users))
278 continue;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900279 if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, &group->list))
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900280 goto unlock;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900281 }
282 }
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900283 unlock:
Tetsuo Handa29282382010-05-06 00:18:15 +0900284 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900285}
286
287static void tomoyo_kfree_entry(void)
288{
289 struct tomoyo_gc_entry *p;
290 struct tomoyo_gc_entry *tmp;
291
292 list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
Tetsuo Handae79acf02010-06-16 16:31:50 +0900293 struct list_head *element = p->element;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900294 switch (p->type) {
295 case TOMOYO_ID_DOMAIN_INITIALIZER:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900296 tomoyo_del_domain_initializer(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900297 break;
298 case TOMOYO_ID_DOMAIN_KEEPER:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900299 tomoyo_del_domain_keeper(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900300 break;
Tetsuo Handa10843072010-06-03 20:38:03 +0900301 case TOMOYO_ID_AGGREGATOR:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900302 tomoyo_del_aggregator(element);
Tetsuo Handa10843072010-06-03 20:38:03 +0900303 break;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900304 case TOMOYO_ID_GLOBALLY_READABLE:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900305 tomoyo_del_allow_read(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900306 break;
307 case TOMOYO_ID_PATTERN:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900308 tomoyo_del_file_pattern(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900309 break;
310 case TOMOYO_ID_NO_REWRITE:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900311 tomoyo_del_no_rewrite(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900312 break;
313 case TOMOYO_ID_MANAGER:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900314 tomoyo_del_manager(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900315 break;
316 case TOMOYO_ID_NAME:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900317 tomoyo_del_name(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900318 break;
319 case TOMOYO_ID_ACL:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900320 tomoyo_del_acl(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900321 break;
322 case TOMOYO_ID_DOMAIN:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900323 if (!tomoyo_del_domain(element))
Tetsuo Handa847b1732010-02-11 09:43:54 +0900324 continue;
325 break;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900326 case TOMOYO_ID_PATH_GROUP:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900327 tomoyo_del_path_group(element);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900328 break;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900329 case TOMOYO_ID_GROUP:
330 tomoyo_del_group(element);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900331 break;
332 case TOMOYO_ID_NUMBER_GROUP:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900333 tomoyo_del_number_group(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900334 break;
335 }
Tetsuo Handae79acf02010-06-16 16:31:50 +0900336 tomoyo_memory_free(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900337 list_del(&p->list);
338 kfree(p);
339 }
340}
341
342static int tomoyo_gc_thread(void *unused)
343{
344 daemonize("GC for TOMOYO");
345 if (mutex_trylock(&tomoyo_gc_mutex)) {
346 int i;
347 for (i = 0; i < 10; i++) {
348 tomoyo_collect_entry();
349 if (list_empty(&tomoyo_gc_queue))
350 break;
351 synchronize_srcu(&tomoyo_ss);
352 tomoyo_kfree_entry();
353 }
354 mutex_unlock(&tomoyo_gc_mutex);
355 }
356 do_exit(0);
357}
358
359void tomoyo_run_gc(void)
360{
361 struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
362 "GC for TOMOYO");
363 if (!IS_ERR(task))
364 wake_up_process(task);
365}