4 * Implementation of the Domain-Based Mandatory Access Control.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
11 #include <linux/kthread.h>
12 #include <linux/slab.h>
16 TOMOYO_ID_PATH_GROUP_MEMBER,
17 TOMOYO_ID_NUMBER_GROUP,
18 TOMOYO_ID_NUMBER_GROUP_MEMBER,
19 TOMOYO_ID_DOMAIN_INITIALIZER,
20 TOMOYO_ID_DOMAIN_KEEPER,
23 TOMOYO_ID_GLOBALLY_READABLE,
32 struct tomoyo_gc_entry {
33 struct list_head list;
37 static LIST_HEAD(tomoyo_gc_queue);
38 static DEFINE_MUTEX(tomoyo_gc_mutex);
40 /* Caller holds tomoyo_policy_lock mutex. */
41 static bool tomoyo_add_to_gc(const int type, void *element)
43 struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
47 entry->element = element;
48 list_add(&entry->list, &tomoyo_gc_queue);
52 static void tomoyo_del_allow_read
53 (struct tomoyo_globally_readable_file_entry *ptr)
55 tomoyo_put_name(ptr->filename);
58 static void tomoyo_del_file_pattern(struct tomoyo_pattern_entry *ptr)
60 tomoyo_put_name(ptr->pattern);
63 static void tomoyo_del_no_rewrite(struct tomoyo_no_rewrite_entry *ptr)
65 tomoyo_put_name(ptr->pattern);
68 static void tomoyo_del_domain_initializer
69 (struct tomoyo_domain_initializer_entry *ptr)
71 tomoyo_put_name(ptr->domainname);
72 tomoyo_put_name(ptr->program);
75 static void tomoyo_del_domain_keeper(struct tomoyo_domain_keeper_entry *ptr)
77 tomoyo_put_name(ptr->domainname);
78 tomoyo_put_name(ptr->program);
81 static void tomoyo_del_aggregator(struct tomoyo_aggregator_entry *ptr)
83 tomoyo_put_name(ptr->original_name);
84 tomoyo_put_name(ptr->aggregated_name);
87 static void tomoyo_del_alias(struct tomoyo_alias_entry *ptr)
89 tomoyo_put_name(ptr->original_name);
90 tomoyo_put_name(ptr->aliased_name);
93 static void tomoyo_del_manager(struct tomoyo_policy_manager_entry *ptr)
95 tomoyo_put_name(ptr->manager);
98 static void tomoyo_del_acl(struct tomoyo_acl_info *acl)
101 case TOMOYO_TYPE_PATH_ACL:
103 struct tomoyo_path_acl *entry
104 = container_of(acl, typeof(*entry), head);
105 tomoyo_put_name_union(&entry->name);
108 case TOMOYO_TYPE_PATH2_ACL:
110 struct tomoyo_path2_acl *entry
111 = container_of(acl, typeof(*entry), head);
112 tomoyo_put_name_union(&entry->name1);
113 tomoyo_put_name_union(&entry->name2);
116 case TOMOYO_TYPE_PATH_NUMBER_ACL:
118 struct tomoyo_path_number_acl *entry
119 = container_of(acl, typeof(*entry), head);
120 tomoyo_put_name_union(&entry->name);
121 tomoyo_put_number_union(&entry->number);
124 case TOMOYO_TYPE_PATH_NUMBER3_ACL:
126 struct tomoyo_path_number3_acl *entry
127 = container_of(acl, typeof(*entry), head);
128 tomoyo_put_name_union(&entry->name);
129 tomoyo_put_number_union(&entry->mode);
130 tomoyo_put_number_union(&entry->major);
131 tomoyo_put_number_union(&entry->minor);
134 case TOMOYO_TYPE_MOUNT_ACL:
136 struct tomoyo_mount_acl *entry
137 = container_of(acl, typeof(*entry), head);
138 tomoyo_put_name_union(&entry->dev_name);
139 tomoyo_put_name_union(&entry->dir_name);
140 tomoyo_put_name_union(&entry->fs_type);
141 tomoyo_put_number_union(&entry->flags);
145 printk(KERN_WARNING "Unknown type\n");
150 static bool tomoyo_del_domain(struct tomoyo_domain_info *domain)
152 struct tomoyo_acl_info *acl;
153 struct tomoyo_acl_info *tmp;
155 * Since we don't protect whole execve() operation using SRCU,
156 * we need to recheck domain->users at this point.
158 * (1) Reader starts SRCU section upon execve().
159 * (2) Reader traverses tomoyo_domain_list and finds this domain.
160 * (3) Writer marks this domain as deleted.
161 * (4) Garbage collector removes this domain from tomoyo_domain_list
162 * because this domain is marked as deleted and used by nobody.
163 * (5) Reader saves reference to this domain into
164 * "struct linux_binprm"->cred->security .
165 * (6) Reader finishes SRCU section, although execve() operation has
167 * (7) Garbage collector waits for SRCU synchronization.
168 * (8) Garbage collector kfree() this domain because this domain is
170 * (9) Reader finishes execve() operation and restores this domain from
171 * "struct linux_binprm"->cred->security.
173 * By updating domain->users at (5), we can solve this race problem
174 * by rechecking domain->users at (8).
176 if (atomic_read(&domain->users))
178 list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
180 tomoyo_memory_free(acl);
182 tomoyo_put_name(domain->domainname);
187 static void tomoyo_del_name(const struct tomoyo_name_entry *ptr)
191 static void tomoyo_del_path_group_member(struct tomoyo_path_group_member
194 tomoyo_put_name(member->member_name);
197 static void tomoyo_del_path_group(struct tomoyo_path_group *group)
199 tomoyo_put_name(group->group_name);
202 static void tomoyo_del_number_group_member(struct tomoyo_number_group_member
207 static void tomoyo_del_number_group(struct tomoyo_number_group *group)
209 tomoyo_put_name(group->group_name);
212 static void tomoyo_collect_entry(void)
214 if (mutex_lock_interruptible(&tomoyo_policy_lock))
217 struct tomoyo_globally_readable_file_entry *ptr;
218 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
220 if (!ptr->is_deleted)
222 if (tomoyo_add_to_gc(TOMOYO_ID_GLOBALLY_READABLE, ptr))
223 list_del_rcu(&ptr->list);
229 struct tomoyo_pattern_entry *ptr;
230 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
231 if (!ptr->is_deleted)
233 if (tomoyo_add_to_gc(TOMOYO_ID_PATTERN, ptr))
234 list_del_rcu(&ptr->list);
240 struct tomoyo_no_rewrite_entry *ptr;
241 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
242 if (!ptr->is_deleted)
244 if (tomoyo_add_to_gc(TOMOYO_ID_NO_REWRITE, ptr))
245 list_del_rcu(&ptr->list);
251 struct tomoyo_domain_initializer_entry *ptr;
252 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
254 if (!ptr->is_deleted)
256 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN_INITIALIZER, ptr))
257 list_del_rcu(&ptr->list);
263 struct tomoyo_domain_keeper_entry *ptr;
264 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
265 if (!ptr->is_deleted)
267 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN_KEEPER, ptr))
268 list_del_rcu(&ptr->list);
274 struct tomoyo_aggregator_entry *ptr;
275 list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list, list) {
276 if (!ptr->is_deleted)
278 if (tomoyo_add_to_gc(TOMOYO_ID_AGGREGATOR, ptr))
279 list_del_rcu(&ptr->list);
285 struct tomoyo_alias_entry *ptr;
286 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
287 if (!ptr->is_deleted)
289 if (tomoyo_add_to_gc(TOMOYO_ID_ALIAS, ptr))
290 list_del_rcu(&ptr->list);
296 struct tomoyo_policy_manager_entry *ptr;
297 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list,
299 if (!ptr->is_deleted)
301 if (tomoyo_add_to_gc(TOMOYO_ID_MANAGER, ptr))
302 list_del_rcu(&ptr->list);
308 struct tomoyo_domain_info *domain;
309 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
310 struct tomoyo_acl_info *acl;
311 list_for_each_entry_rcu(acl, &domain->acl_info_list,
314 case TOMOYO_TYPE_PATH_ACL:
315 if (container_of(acl,
316 struct tomoyo_path_acl,
320 case TOMOYO_TYPE_PATH2_ACL:
321 if (container_of(acl,
322 struct tomoyo_path2_acl,
326 case TOMOYO_TYPE_PATH_NUMBER_ACL:
327 if (container_of(acl,
328 struct tomoyo_path_number_acl,
332 case TOMOYO_TYPE_PATH_NUMBER3_ACL:
333 if (container_of(acl,
334 struct tomoyo_path_number3_acl,
341 if (tomoyo_add_to_gc(TOMOYO_ID_ACL, acl))
342 list_del_rcu(&acl->list);
346 if (!domain->is_deleted || atomic_read(&domain->users))
349 * Nobody is referring this domain. But somebody may
350 * refer this domain after successful execve().
351 * We recheck domain->users after SRCU synchronization.
353 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, domain))
354 list_del_rcu(&domain->list);
361 for (i = 0; i < TOMOYO_MAX_HASH; i++) {
362 struct tomoyo_name_entry *ptr;
363 list_for_each_entry_rcu(ptr, &tomoyo_name_list[i],
365 if (atomic_read(&ptr->users))
367 if (tomoyo_add_to_gc(TOMOYO_ID_NAME, ptr))
368 list_del_rcu(&ptr->list);
377 struct tomoyo_path_group *group;
378 list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) {
379 struct tomoyo_path_group_member *member;
380 list_for_each_entry_rcu(member, &group->member_list,
382 if (!member->is_deleted)
384 if (tomoyo_add_to_gc(TOMOYO_ID_PATH_GROUP_MEMBER,
386 list_del_rcu(&member->list);
390 if (!list_empty(&group->member_list) ||
391 atomic_read(&group->users))
393 if (tomoyo_add_to_gc(TOMOYO_ID_PATH_GROUP, group))
394 list_del_rcu(&group->list);
400 struct tomoyo_number_group *group;
401 list_for_each_entry_rcu(group, &tomoyo_number_group_list, list) {
402 struct tomoyo_number_group_member *member;
403 list_for_each_entry_rcu(member, &group->member_list,
405 if (!member->is_deleted)
407 if (tomoyo_add_to_gc(TOMOYO_ID_NUMBER_GROUP_MEMBER,
409 list_del_rcu(&member->list);
413 if (!list_empty(&group->member_list) ||
414 atomic_read(&group->users))
416 if (tomoyo_add_to_gc(TOMOYO_ID_NUMBER_GROUP, group))
417 list_del_rcu(&group->list);
422 mutex_unlock(&tomoyo_policy_lock);
425 static void tomoyo_kfree_entry(void)
427 struct tomoyo_gc_entry *p;
428 struct tomoyo_gc_entry *tmp;
430 list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
432 case TOMOYO_ID_DOMAIN_INITIALIZER:
433 tomoyo_del_domain_initializer(p->element);
435 case TOMOYO_ID_DOMAIN_KEEPER:
436 tomoyo_del_domain_keeper(p->element);
438 case TOMOYO_ID_AGGREGATOR:
439 tomoyo_del_aggregator(p->element);
441 case TOMOYO_ID_ALIAS:
442 tomoyo_del_alias(p->element);
444 case TOMOYO_ID_GLOBALLY_READABLE:
445 tomoyo_del_allow_read(p->element);
447 case TOMOYO_ID_PATTERN:
448 tomoyo_del_file_pattern(p->element);
450 case TOMOYO_ID_NO_REWRITE:
451 tomoyo_del_no_rewrite(p->element);
453 case TOMOYO_ID_MANAGER:
454 tomoyo_del_manager(p->element);
457 tomoyo_del_name(p->element);
460 tomoyo_del_acl(p->element);
462 case TOMOYO_ID_DOMAIN:
463 if (!tomoyo_del_domain(p->element))
466 case TOMOYO_ID_PATH_GROUP_MEMBER:
467 tomoyo_del_path_group_member(p->element);
469 case TOMOYO_ID_PATH_GROUP:
470 tomoyo_del_path_group(p->element);
472 case TOMOYO_ID_NUMBER_GROUP_MEMBER:
473 tomoyo_del_number_group_member(p->element);
475 case TOMOYO_ID_NUMBER_GROUP:
476 tomoyo_del_number_group(p->element);
479 printk(KERN_WARNING "Unknown type\n");
482 tomoyo_memory_free(p->element);
488 static int tomoyo_gc_thread(void *unused)
490 daemonize("GC for TOMOYO");
491 if (mutex_trylock(&tomoyo_gc_mutex)) {
493 for (i = 0; i < 10; i++) {
494 tomoyo_collect_entry();
495 if (list_empty(&tomoyo_gc_queue))
497 synchronize_srcu(&tomoyo_ss);
498 tomoyo_kfree_entry();
500 mutex_unlock(&tomoyo_gc_mutex);
505 void tomoyo_run_gc(void)
507 struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
510 wake_up_process(task);