blob: d129317ca48192dd7a86cfb05287920be95dc8d6 [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 Handad2f8b232010-06-15 10:10:37 +090014enum tomoyo_policy_id {
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090015 TOMOYO_ID_PATH_GROUP,
16 TOMOYO_ID_PATH_GROUP_MEMBER,
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +090017 TOMOYO_ID_NUMBER_GROUP,
18 TOMOYO_ID_NUMBER_GROUP_MEMBER,
Tetsuo Handa847b1732010-02-11 09:43:54 +090019 TOMOYO_ID_DOMAIN_INITIALIZER,
20 TOMOYO_ID_DOMAIN_KEEPER,
Tetsuo Handa10843072010-06-03 20:38:03 +090021 TOMOYO_ID_AGGREGATOR,
Tetsuo Handa847b1732010-02-11 09:43:54 +090022 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 Handad2f8b232010-06-15 10:10:37 +090029 TOMOYO_ID_DOMAIN,
30 TOMOYO_MAX_POLICY
Tetsuo Handa847b1732010-02-11 09:43:54 +090031};
32
33struct tomoyo_gc_entry {
34 struct list_head list;
35 int type;
36 void *element;
37};
38static LIST_HEAD(tomoyo_gc_queue);
39static DEFINE_MUTEX(tomoyo_gc_mutex);
40
41/* Caller holds tomoyo_policy_lock mutex. */
42static bool tomoyo_add_to_gc(const int type, void *element)
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);
50 return true;
51}
52
53static void tomoyo_del_allow_read
54(struct tomoyo_globally_readable_file_entry *ptr)
55{
56 tomoyo_put_name(ptr->filename);
57}
58
59static void tomoyo_del_file_pattern(struct tomoyo_pattern_entry *ptr)
60{
61 tomoyo_put_name(ptr->pattern);
62}
63
64static void tomoyo_del_no_rewrite(struct tomoyo_no_rewrite_entry *ptr)
65{
66 tomoyo_put_name(ptr->pattern);
67}
68
69static void tomoyo_del_domain_initializer
70(struct tomoyo_domain_initializer_entry *ptr)
71{
72 tomoyo_put_name(ptr->domainname);
73 tomoyo_put_name(ptr->program);
74}
75
76static void tomoyo_del_domain_keeper(struct tomoyo_domain_keeper_entry *ptr)
77{
78 tomoyo_put_name(ptr->domainname);
79 tomoyo_put_name(ptr->program);
80}
81
Tetsuo Handa10843072010-06-03 20:38:03 +090082static void tomoyo_del_aggregator(struct tomoyo_aggregator_entry *ptr)
83{
84 tomoyo_put_name(ptr->original_name);
85 tomoyo_put_name(ptr->aggregated_name);
86}
87
Tetsuo Handa847b1732010-02-11 09:43:54 +090088static void tomoyo_del_alias(struct tomoyo_alias_entry *ptr)
89{
90 tomoyo_put_name(ptr->original_name);
91 tomoyo_put_name(ptr->aliased_name);
92}
93
94static void tomoyo_del_manager(struct tomoyo_policy_manager_entry *ptr)
95{
96 tomoyo_put_name(ptr->manager);
97}
98
99static void tomoyo_del_acl(struct tomoyo_acl_info *acl)
100{
101 switch (acl->type) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900102 case TOMOYO_TYPE_PATH_ACL:
Tetsuo Handa847b1732010-02-11 09:43:54 +0900103 {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900104 struct tomoyo_path_acl *entry
Tetsuo Handa847b1732010-02-11 09:43:54 +0900105 = container_of(acl, typeof(*entry), head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900106 tomoyo_put_name_union(&entry->name);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900107 }
108 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900109 case TOMOYO_TYPE_PATH2_ACL:
Tetsuo Handa847b1732010-02-11 09:43:54 +0900110 {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900111 struct tomoyo_path2_acl *entry
Tetsuo Handa847b1732010-02-11 09:43:54 +0900112 = container_of(acl, typeof(*entry), head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900113 tomoyo_put_name_union(&entry->name1);
114 tomoyo_put_name_union(&entry->name2);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900115 }
116 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900117 case TOMOYO_TYPE_PATH_NUMBER_ACL:
118 {
119 struct tomoyo_path_number_acl *entry
120 = container_of(acl, typeof(*entry), head);
121 tomoyo_put_name_union(&entry->name);
122 tomoyo_put_number_union(&entry->number);
123 }
124 break;
Tetsuo Handa75093152010-06-16 16:23:55 +0900125 case TOMOYO_TYPE_MKDEV_ACL:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900126 {
Tetsuo Handa75093152010-06-16 16:23:55 +0900127 struct tomoyo_mkdev_acl *entry
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900128 = container_of(acl, typeof(*entry), head);
129 tomoyo_put_name_union(&entry->name);
130 tomoyo_put_number_union(&entry->mode);
131 tomoyo_put_number_union(&entry->major);
132 tomoyo_put_number_union(&entry->minor);
133 }
134 break;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900135 case TOMOYO_TYPE_MOUNT_ACL:
136 {
137 struct tomoyo_mount_acl *entry
138 = container_of(acl, typeof(*entry), head);
139 tomoyo_put_name_union(&entry->dev_name);
140 tomoyo_put_name_union(&entry->dir_name);
141 tomoyo_put_name_union(&entry->fs_type);
142 tomoyo_put_number_union(&entry->flags);
143 }
144 break;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900145 default:
146 printk(KERN_WARNING "Unknown type\n");
147 break;
148 }
149}
150
151static bool tomoyo_del_domain(struct tomoyo_domain_info *domain)
152{
153 struct tomoyo_acl_info *acl;
154 struct tomoyo_acl_info *tmp;
155 /*
156 * Since we don't protect whole execve() operation using SRCU,
157 * we need to recheck domain->users at this point.
158 *
159 * (1) Reader starts SRCU section upon execve().
160 * (2) Reader traverses tomoyo_domain_list and finds this domain.
161 * (3) Writer marks this domain as deleted.
162 * (4) Garbage collector removes this domain from tomoyo_domain_list
163 * because this domain is marked as deleted and used by nobody.
164 * (5) Reader saves reference to this domain into
165 * "struct linux_binprm"->cred->security .
166 * (6) Reader finishes SRCU section, although execve() operation has
167 * not finished yet.
168 * (7) Garbage collector waits for SRCU synchronization.
169 * (8) Garbage collector kfree() this domain because this domain is
170 * used by nobody.
171 * (9) Reader finishes execve() operation and restores this domain from
172 * "struct linux_binprm"->cred->security.
173 *
174 * By updating domain->users at (5), we can solve this race problem
175 * by rechecking domain->users at (8).
176 */
177 if (atomic_read(&domain->users))
178 return false;
179 list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
180 tomoyo_del_acl(acl);
181 tomoyo_memory_free(acl);
182 }
183 tomoyo_put_name(domain->domainname);
184 return true;
185}
186
187
188static void tomoyo_del_name(const struct tomoyo_name_entry *ptr)
189{
190}
191
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900192static void tomoyo_del_path_group_member(struct tomoyo_path_group_member
193 *member)
194{
195 tomoyo_put_name(member->member_name);
196}
197
198static void tomoyo_del_path_group(struct tomoyo_path_group *group)
199{
200 tomoyo_put_name(group->group_name);
201}
202
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900203static void tomoyo_del_number_group_member(struct tomoyo_number_group_member
204 *member)
205{
206}
207
208static void tomoyo_del_number_group(struct tomoyo_number_group *group)
209{
210 tomoyo_put_name(group->group_name);
211}
212
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900213static struct list_head *tomoyo_policy_list[TOMOYO_MAX_POLICY] = {
214 [TOMOYO_ID_GLOBALLY_READABLE] = &tomoyo_globally_readable_list,
215 [TOMOYO_ID_PATTERN] = &tomoyo_pattern_list,
216 [TOMOYO_ID_NO_REWRITE] = &tomoyo_no_rewrite_list,
217 [TOMOYO_ID_DOMAIN_INITIALIZER] = &tomoyo_domain_initializer_list,
218 [TOMOYO_ID_DOMAIN_KEEPER] = &tomoyo_domain_keeper_list,
219 [TOMOYO_ID_AGGREGATOR] = &tomoyo_aggregator_list,
220 [TOMOYO_ID_ALIAS] = &tomoyo_alias_list,
221 [TOMOYO_ID_MANAGER] = &tomoyo_policy_manager_list,
222};
223
224static bool tomoyo_collect_member(struct list_head *member_list, int id)
225{
226 struct tomoyo_acl_head *member;
227 list_for_each_entry(member, member_list, list) {
228 if (!member->is_deleted)
229 continue;
230 if (!tomoyo_add_to_gc(id, &member->list))
231 return false;
232 list_del_rcu(&member->list);
233 }
234 return true;
235}
236
237static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain)
238{
239 struct tomoyo_acl_info *acl;
240 list_for_each_entry(acl, &domain->acl_info_list, list) {
241 if (!acl->is_deleted)
242 continue;
243 if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
244 return false;
245 list_del_rcu(&acl->list);
246 }
247 return true;
248}
249
Tetsuo Handa847b1732010-02-11 09:43:54 +0900250static void tomoyo_collect_entry(void)
251{
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900252 int i;
Tetsuo Handa29282382010-05-06 00:18:15 +0900253 if (mutex_lock_interruptible(&tomoyo_policy_lock))
254 return;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900255 for (i = 0; i < TOMOYO_MAX_POLICY; i++) {
256 if (tomoyo_policy_list[i])
257 if (!tomoyo_collect_member(tomoyo_policy_list[i], i))
258 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900259 }
260 {
261 struct tomoyo_domain_info *domain;
262 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900263 if (!tomoyo_collect_acl(domain))
264 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900265 if (!domain->is_deleted || atomic_read(&domain->users))
266 continue;
267 /*
268 * Nobody is referring this domain. But somebody may
269 * refer this domain after successful execve().
270 * We recheck domain->users after SRCU synchronization.
271 */
272 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, domain))
273 list_del_rcu(&domain->list);
274 else
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900275 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900276 }
277 }
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900278 for (i = 0; i < TOMOYO_MAX_HASH; i++) {
279 struct tomoyo_name_entry *ptr;
280 list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) {
281 if (atomic_read(&ptr->users))
282 continue;
283 if (tomoyo_add_to_gc(TOMOYO_ID_NAME, ptr))
284 list_del_rcu(&ptr->list);
285 else
286 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900287 }
288 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900289 {
290 struct tomoyo_path_group *group;
291 list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) {
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900292 tomoyo_collect_member(&group->member_list,
293 TOMOYO_ID_PATH_GROUP_MEMBER);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900294 if (!list_empty(&group->member_list) ||
295 atomic_read(&group->users))
296 continue;
297 if (tomoyo_add_to_gc(TOMOYO_ID_PATH_GROUP, group))
298 list_del_rcu(&group->list);
299 else
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900300 goto unlock;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900301 }
302 }
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900303 {
304 struct tomoyo_number_group *group;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900305 list_for_each_entry_rcu(group, &tomoyo_number_group_list,
306 list) {
307 tomoyo_collect_member(&group->member_list,
308 TOMOYO_ID_NUMBER_GROUP_MEMBER);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900309 if (!list_empty(&group->member_list) ||
310 atomic_read(&group->users))
311 continue;
312 if (tomoyo_add_to_gc(TOMOYO_ID_NUMBER_GROUP, group))
313 list_del_rcu(&group->list);
314 else
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900315 goto unlock;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900316 }
317 }
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900318 unlock:
Tetsuo Handa29282382010-05-06 00:18:15 +0900319 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900320}
321
322static void tomoyo_kfree_entry(void)
323{
324 struct tomoyo_gc_entry *p;
325 struct tomoyo_gc_entry *tmp;
326
327 list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
328 switch (p->type) {
329 case TOMOYO_ID_DOMAIN_INITIALIZER:
330 tomoyo_del_domain_initializer(p->element);
331 break;
332 case TOMOYO_ID_DOMAIN_KEEPER:
333 tomoyo_del_domain_keeper(p->element);
334 break;
Tetsuo Handa10843072010-06-03 20:38:03 +0900335 case TOMOYO_ID_AGGREGATOR:
336 tomoyo_del_aggregator(p->element);
337 break;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900338 case TOMOYO_ID_ALIAS:
339 tomoyo_del_alias(p->element);
340 break;
341 case TOMOYO_ID_GLOBALLY_READABLE:
342 tomoyo_del_allow_read(p->element);
343 break;
344 case TOMOYO_ID_PATTERN:
345 tomoyo_del_file_pattern(p->element);
346 break;
347 case TOMOYO_ID_NO_REWRITE:
348 tomoyo_del_no_rewrite(p->element);
349 break;
350 case TOMOYO_ID_MANAGER:
351 tomoyo_del_manager(p->element);
352 break;
353 case TOMOYO_ID_NAME:
354 tomoyo_del_name(p->element);
355 break;
356 case TOMOYO_ID_ACL:
357 tomoyo_del_acl(p->element);
358 break;
359 case TOMOYO_ID_DOMAIN:
360 if (!tomoyo_del_domain(p->element))
361 continue;
362 break;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900363 case TOMOYO_ID_PATH_GROUP_MEMBER:
364 tomoyo_del_path_group_member(p->element);
365 break;
366 case TOMOYO_ID_PATH_GROUP:
367 tomoyo_del_path_group(p->element);
368 break;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900369 case TOMOYO_ID_NUMBER_GROUP_MEMBER:
370 tomoyo_del_number_group_member(p->element);
371 break;
372 case TOMOYO_ID_NUMBER_GROUP:
373 tomoyo_del_number_group(p->element);
374 break;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900375 default:
376 printk(KERN_WARNING "Unknown type\n");
377 break;
378 }
379 tomoyo_memory_free(p->element);
380 list_del(&p->list);
381 kfree(p);
382 }
383}
384
385static int tomoyo_gc_thread(void *unused)
386{
387 daemonize("GC for TOMOYO");
388 if (mutex_trylock(&tomoyo_gc_mutex)) {
389 int i;
390 for (i = 0; i < 10; i++) {
391 tomoyo_collect_entry();
392 if (list_empty(&tomoyo_gc_queue))
393 break;
394 synchronize_srcu(&tomoyo_ss);
395 tomoyo_kfree_entry();
396 }
397 mutex_unlock(&tomoyo_gc_mutex);
398 }
399 do_exit(0);
400}
401
402void tomoyo_run_gc(void)
403{
404 struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
405 "GC for TOMOYO");
406 if (!IS_ERR(task))
407 wake_up_process(task);
408}