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>
15 TOMOYO_ID_DOMAIN_INITIALIZER,
16 TOMOYO_ID_DOMAIN_KEEPER,
18 TOMOYO_ID_GLOBALLY_READABLE,
27 struct tomoyo_gc_entry {
28 struct list_head list;
32 static LIST_HEAD(tomoyo_gc_queue);
33 static DEFINE_MUTEX(tomoyo_gc_mutex);
35 /* Caller holds tomoyo_policy_lock mutex. */
36 static bool tomoyo_add_to_gc(const int type, void *element)
38 struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
42 entry->element = element;
43 list_add(&entry->list, &tomoyo_gc_queue);
47 static void tomoyo_del_allow_read
48 (struct tomoyo_globally_readable_file_entry *ptr)
50 tomoyo_put_name(ptr->filename);
53 static void tomoyo_del_file_pattern(struct tomoyo_pattern_entry *ptr)
55 tomoyo_put_name(ptr->pattern);
58 static void tomoyo_del_no_rewrite(struct tomoyo_no_rewrite_entry *ptr)
60 tomoyo_put_name(ptr->pattern);
63 static void tomoyo_del_domain_initializer
64 (struct tomoyo_domain_initializer_entry *ptr)
66 tomoyo_put_name(ptr->domainname);
67 tomoyo_put_name(ptr->program);
70 static void tomoyo_del_domain_keeper(struct tomoyo_domain_keeper_entry *ptr)
72 tomoyo_put_name(ptr->domainname);
73 tomoyo_put_name(ptr->program);
76 static void tomoyo_del_alias(struct tomoyo_alias_entry *ptr)
78 tomoyo_put_name(ptr->original_name);
79 tomoyo_put_name(ptr->aliased_name);
82 static void tomoyo_del_manager(struct tomoyo_policy_manager_entry *ptr)
84 tomoyo_put_name(ptr->manager);
87 static void tomoyo_del_acl(struct tomoyo_acl_info *acl)
90 case TOMOYO_TYPE_PATH_ACL:
92 struct tomoyo_path_acl *entry
93 = container_of(acl, typeof(*entry), head);
94 tomoyo_put_name(entry->filename);
97 case TOMOYO_TYPE_PATH2_ACL:
99 struct tomoyo_path2_acl *entry
100 = container_of(acl, typeof(*entry), head);
101 tomoyo_put_name(entry->filename1);
102 tomoyo_put_name(entry->filename2);
106 printk(KERN_WARNING "Unknown type\n");
111 static bool tomoyo_del_domain(struct tomoyo_domain_info *domain)
113 struct tomoyo_acl_info *acl;
114 struct tomoyo_acl_info *tmp;
116 * Since we don't protect whole execve() operation using SRCU,
117 * we need to recheck domain->users at this point.
119 * (1) Reader starts SRCU section upon execve().
120 * (2) Reader traverses tomoyo_domain_list and finds this domain.
121 * (3) Writer marks this domain as deleted.
122 * (4) Garbage collector removes this domain from tomoyo_domain_list
123 * because this domain is marked as deleted and used by nobody.
124 * (5) Reader saves reference to this domain into
125 * "struct linux_binprm"->cred->security .
126 * (6) Reader finishes SRCU section, although execve() operation has
128 * (7) Garbage collector waits for SRCU synchronization.
129 * (8) Garbage collector kfree() this domain because this domain is
131 * (9) Reader finishes execve() operation and restores this domain from
132 * "struct linux_binprm"->cred->security.
134 * By updating domain->users at (5), we can solve this race problem
135 * by rechecking domain->users at (8).
137 if (atomic_read(&domain->users))
139 list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
141 tomoyo_memory_free(acl);
143 tomoyo_put_name(domain->domainname);
148 static void tomoyo_del_name(const struct tomoyo_name_entry *ptr)
152 static void tomoyo_collect_entry(void)
154 mutex_lock(&tomoyo_policy_lock);
156 struct tomoyo_globally_readable_file_entry *ptr;
157 list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
159 if (!ptr->is_deleted)
161 if (tomoyo_add_to_gc(TOMOYO_ID_GLOBALLY_READABLE, ptr))
162 list_del_rcu(&ptr->list);
168 struct tomoyo_pattern_entry *ptr;
169 list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
170 if (!ptr->is_deleted)
172 if (tomoyo_add_to_gc(TOMOYO_ID_PATTERN, ptr))
173 list_del_rcu(&ptr->list);
179 struct tomoyo_no_rewrite_entry *ptr;
180 list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
181 if (!ptr->is_deleted)
183 if (tomoyo_add_to_gc(TOMOYO_ID_NO_REWRITE, ptr))
184 list_del_rcu(&ptr->list);
190 struct tomoyo_domain_initializer_entry *ptr;
191 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
193 if (!ptr->is_deleted)
195 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN_INITIALIZER, ptr))
196 list_del_rcu(&ptr->list);
202 struct tomoyo_domain_keeper_entry *ptr;
203 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
204 if (!ptr->is_deleted)
206 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN_KEEPER, ptr))
207 list_del_rcu(&ptr->list);
213 struct tomoyo_alias_entry *ptr;
214 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
215 if (!ptr->is_deleted)
217 if (tomoyo_add_to_gc(TOMOYO_ID_ALIAS, ptr))
218 list_del_rcu(&ptr->list);
224 struct tomoyo_policy_manager_entry *ptr;
225 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list,
227 if (!ptr->is_deleted)
229 if (tomoyo_add_to_gc(TOMOYO_ID_MANAGER, ptr))
230 list_del_rcu(&ptr->list);
236 struct tomoyo_domain_info *domain;
237 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
238 struct tomoyo_acl_info *acl;
239 list_for_each_entry_rcu(acl, &domain->acl_info_list,
242 case TOMOYO_TYPE_PATH_ACL:
243 if (container_of(acl,
244 struct tomoyo_path_acl,
247 struct tomoyo_path_acl,
251 case TOMOYO_TYPE_PATH2_ACL:
252 if (container_of(acl,
253 struct tomoyo_path2_acl,
260 if (tomoyo_add_to_gc(TOMOYO_ID_ACL, acl))
261 list_del_rcu(&acl->list);
265 if (!domain->is_deleted || atomic_read(&domain->users))
268 * Nobody is referring this domain. But somebody may
269 * refer this domain after successful execve().
270 * We recheck domain->users after SRCU synchronization.
272 if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, domain))
273 list_del_rcu(&domain->list);
278 mutex_unlock(&tomoyo_policy_lock);
279 mutex_lock(&tomoyo_name_list_lock);
282 for (i = 0; i < TOMOYO_MAX_HASH; i++) {
283 struct tomoyo_name_entry *ptr;
284 list_for_each_entry_rcu(ptr, &tomoyo_name_list[i],
286 if (atomic_read(&ptr->users))
288 if (tomoyo_add_to_gc(TOMOYO_ID_NAME, ptr))
289 list_del_rcu(&ptr->list);
297 mutex_unlock(&tomoyo_name_list_lock);
300 static void tomoyo_kfree_entry(void)
302 struct tomoyo_gc_entry *p;
303 struct tomoyo_gc_entry *tmp;
305 list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
307 case TOMOYO_ID_DOMAIN_INITIALIZER:
308 tomoyo_del_domain_initializer(p->element);
310 case TOMOYO_ID_DOMAIN_KEEPER:
311 tomoyo_del_domain_keeper(p->element);
313 case TOMOYO_ID_ALIAS:
314 tomoyo_del_alias(p->element);
316 case TOMOYO_ID_GLOBALLY_READABLE:
317 tomoyo_del_allow_read(p->element);
319 case TOMOYO_ID_PATTERN:
320 tomoyo_del_file_pattern(p->element);
322 case TOMOYO_ID_NO_REWRITE:
323 tomoyo_del_no_rewrite(p->element);
325 case TOMOYO_ID_MANAGER:
326 tomoyo_del_manager(p->element);
329 tomoyo_del_name(p->element);
332 tomoyo_del_acl(p->element);
334 case TOMOYO_ID_DOMAIN:
335 if (!tomoyo_del_domain(p->element))
339 printk(KERN_WARNING "Unknown type\n");
342 tomoyo_memory_free(p->element);
348 static int tomoyo_gc_thread(void *unused)
350 daemonize("GC for TOMOYO");
351 if (mutex_trylock(&tomoyo_gc_mutex)) {
353 for (i = 0; i < 10; i++) {
354 tomoyo_collect_entry();
355 if (list_empty(&tomoyo_gc_queue))
357 synchronize_srcu(&tomoyo_ss);
358 tomoyo_kfree_entry();
360 mutex_unlock(&tomoyo_gc_mutex);
365 void tomoyo_run_gc(void)
367 struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
370 wake_up_process(task);