4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/kthread.h>
9 #include <linux/slab.h>
11 /* The list for "struct tomoyo_io_buffer". */
12 static LIST_HEAD(tomoyo_io_buffer_list);
13 /* Lock for protecting tomoyo_io_buffer_list. */
14 static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock);
16 /* Size of an element. */
17 static const u8 tomoyo_element_size[TOMOYO_MAX_POLICY] = {
18 [TOMOYO_ID_GROUP] = sizeof(struct tomoyo_group),
19 [TOMOYO_ID_PATH_GROUP] = sizeof(struct tomoyo_path_group),
20 [TOMOYO_ID_NUMBER_GROUP] = sizeof(struct tomoyo_number_group),
21 [TOMOYO_ID_AGGREGATOR] = sizeof(struct tomoyo_aggregator),
22 [TOMOYO_ID_TRANSITION_CONTROL] =
23 sizeof(struct tomoyo_transition_control),
24 [TOMOYO_ID_MANAGER] = sizeof(struct tomoyo_manager),
25 /* [TOMOYO_ID_CONDITION] = "struct tomoyo_condition"->size, */
26 /* [TOMOYO_ID_NAME] = "struct tomoyo_name"->size, */
28 tomoyo_acl_size["struct tomoyo_acl_info"->type], */
29 [TOMOYO_ID_DOMAIN] = sizeof(struct tomoyo_domain_info),
32 /* Size of a domain ACL element. */
33 static const u8 tomoyo_acl_size[] = {
34 [TOMOYO_TYPE_PATH_ACL] = sizeof(struct tomoyo_path_acl),
35 [TOMOYO_TYPE_PATH2_ACL] = sizeof(struct tomoyo_path2_acl),
36 [TOMOYO_TYPE_PATH_NUMBER_ACL] = sizeof(struct tomoyo_path_number_acl),
37 [TOMOYO_TYPE_MKDEV_ACL] = sizeof(struct tomoyo_mkdev_acl),
38 [TOMOYO_TYPE_MOUNT_ACL] = sizeof(struct tomoyo_mount_acl),
39 [TOMOYO_TYPE_ENV_ACL] = sizeof(struct tomoyo_env_acl),
43 * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not.
45 * @element: Pointer to "struct list_head".
47 * Returns true if @element is used by /sys/kernel/security/tomoyo/ users,
50 static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element)
52 struct tomoyo_io_buffer *head;
55 spin_lock(&tomoyo_io_buffer_list_lock);
56 list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
58 spin_unlock(&tomoyo_io_buffer_list_lock);
59 if (mutex_lock_interruptible(&head->io_sem)) {
63 if (head->r.domain == element || head->r.group == element ||
64 head->r.acl == element || &head->w.domain->list == element)
66 mutex_unlock(&head->io_sem);
68 spin_lock(&tomoyo_io_buffer_list_lock);
73 spin_unlock(&tomoyo_io_buffer_list_lock);
78 * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not.
80 * @string: String to check.
81 * @size: Memory allocated for @string .
83 * Returns true if @string is used by /sys/kernel/security/tomoyo/ users,
86 static bool tomoyo_name_used_by_io_buffer(const char *string,
89 struct tomoyo_io_buffer *head;
92 spin_lock(&tomoyo_io_buffer_list_lock);
93 list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
96 spin_unlock(&tomoyo_io_buffer_list_lock);
97 if (mutex_lock_interruptible(&head->io_sem)) {
101 for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) {
102 const char *w = head->r.w[i];
103 if (w < string || w > string + size)
108 mutex_unlock(&head->io_sem);
110 spin_lock(&tomoyo_io_buffer_list_lock);
115 spin_unlock(&tomoyo_io_buffer_list_lock);
119 /* Structure for garbage collection. */
121 struct list_head list;
122 enum tomoyo_policy_id type;
124 struct list_head *element;
126 /* List of entries to be deleted. */
127 static LIST_HEAD(tomoyo_gc_list);
128 /* Length of tomoyo_gc_list. */
129 static int tomoyo_gc_list_len;
132 * tomoyo_add_to_gc - Add an entry to to be deleted list.
134 * @type: One of values in "enum tomoyo_policy_id".
135 * @element: Pointer to "struct list_head".
137 * Returns true on success, false otherwise.
139 * Caller holds tomoyo_policy_lock mutex.
141 * Adding an entry needs kmalloc(). Thus, if we try to add thousands of
142 * entries at once, it will take too long time. Thus, do not add more than 128
143 * entries per a scan. But to be able to handle worst case where all entries
144 * are in-use, we accept one more entry per a scan.
146 * If we use singly linked list using "struct list_head"->prev (which is
147 * LIST_POISON2), we can avoid kmalloc().
149 static bool tomoyo_add_to_gc(const int type, struct list_head *element)
151 struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
155 if (type == TOMOYO_ID_ACL)
156 entry->size = tomoyo_acl_size[
157 container_of(element,
158 typeof(struct tomoyo_acl_info),
160 else if (type == TOMOYO_ID_NAME)
161 entry->size = strlen(container_of(element,
162 typeof(struct tomoyo_name),
163 head.list)->entry.name) + 1;
164 else if (type == TOMOYO_ID_CONDITION)
166 container_of(element, typeof(struct tomoyo_condition),
169 entry->size = tomoyo_element_size[type];
170 entry->element = element;
171 list_add(&entry->list, &tomoyo_gc_list);
172 list_del_rcu(element);
173 return tomoyo_gc_list_len++ < 128;
177 * tomoyo_element_linked_by_gc - Validate next element of an entry.
179 * @element: Pointer to an element.
180 * @size: Size of @element in byte.
182 * Returns true if @element is linked by other elements in the garbage
183 * collector's queue, false otherwise.
185 static bool tomoyo_element_linked_by_gc(const u8 *element, const size_t size)
188 list_for_each_entry(p, &tomoyo_gc_list, list) {
189 const u8 *ptr = (const u8 *) p->element->next;
190 if (ptr < element || element + size < ptr)
198 * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control".
200 * @element: Pointer to "struct list_head".
204 static void tomoyo_del_transition_control(struct list_head *element)
206 struct tomoyo_transition_control *ptr =
207 container_of(element, typeof(*ptr), head.list);
208 tomoyo_put_name(ptr->domainname);
209 tomoyo_put_name(ptr->program);
213 * tomoyo_del_aggregator - Delete members in "struct tomoyo_aggregator".
215 * @element: Pointer to "struct list_head".
219 static void tomoyo_del_aggregator(struct list_head *element)
221 struct tomoyo_aggregator *ptr =
222 container_of(element, typeof(*ptr), head.list);
223 tomoyo_put_name(ptr->original_name);
224 tomoyo_put_name(ptr->aggregated_name);
228 * tomoyo_del_manager - Delete members in "struct tomoyo_manager".
230 * @element: Pointer to "struct list_head".
234 static void tomoyo_del_manager(struct list_head *element)
236 struct tomoyo_manager *ptr =
237 container_of(element, typeof(*ptr), head.list);
238 tomoyo_put_name(ptr->manager);
242 * tomoyo_del_acl - Delete members in "struct tomoyo_acl_info".
244 * @element: Pointer to "struct list_head".
248 static void tomoyo_del_acl(struct list_head *element)
250 struct tomoyo_acl_info *acl =
251 container_of(element, typeof(*acl), list);
252 tomoyo_put_condition(acl->cond);
254 case TOMOYO_TYPE_PATH_ACL:
256 struct tomoyo_path_acl *entry
257 = container_of(acl, typeof(*entry), head);
258 tomoyo_put_name_union(&entry->name);
261 case TOMOYO_TYPE_PATH2_ACL:
263 struct tomoyo_path2_acl *entry
264 = container_of(acl, typeof(*entry), head);
265 tomoyo_put_name_union(&entry->name1);
266 tomoyo_put_name_union(&entry->name2);
269 case TOMOYO_TYPE_PATH_NUMBER_ACL:
271 struct tomoyo_path_number_acl *entry
272 = container_of(acl, typeof(*entry), head);
273 tomoyo_put_name_union(&entry->name);
274 tomoyo_put_number_union(&entry->number);
277 case TOMOYO_TYPE_MKDEV_ACL:
279 struct tomoyo_mkdev_acl *entry
280 = container_of(acl, typeof(*entry), head);
281 tomoyo_put_name_union(&entry->name);
282 tomoyo_put_number_union(&entry->mode);
283 tomoyo_put_number_union(&entry->major);
284 tomoyo_put_number_union(&entry->minor);
287 case TOMOYO_TYPE_MOUNT_ACL:
289 struct tomoyo_mount_acl *entry
290 = container_of(acl, typeof(*entry), head);
291 tomoyo_put_name_union(&entry->dev_name);
292 tomoyo_put_name_union(&entry->dir_name);
293 tomoyo_put_name_union(&entry->fs_type);
294 tomoyo_put_number_union(&entry->flags);
297 case TOMOYO_TYPE_ENV_ACL:
299 struct tomoyo_env_acl *entry =
300 container_of(acl, typeof(*entry), head);
302 tomoyo_put_name(entry->env);
309 * tomoyo_del_domain - Delete members in "struct tomoyo_domain_info".
311 * @element: Pointer to "struct list_head".
313 * Returns true if deleted, false otherwise.
315 static bool tomoyo_del_domain(struct list_head *element)
317 struct tomoyo_domain_info *domain =
318 container_of(element, typeof(*domain), list);
319 struct tomoyo_acl_info *acl;
320 struct tomoyo_acl_info *tmp;
322 * Since we don't protect whole execve() operation using SRCU,
323 * we need to recheck domain->users at this point.
325 * (1) Reader starts SRCU section upon execve().
326 * (2) Reader traverses tomoyo_domain_list and finds this domain.
327 * (3) Writer marks this domain as deleted.
328 * (4) Garbage collector removes this domain from tomoyo_domain_list
329 * because this domain is marked as deleted and used by nobody.
330 * (5) Reader saves reference to this domain into
331 * "struct linux_binprm"->cred->security .
332 * (6) Reader finishes SRCU section, although execve() operation has
334 * (7) Garbage collector waits for SRCU synchronization.
335 * (8) Garbage collector kfree() this domain because this domain is
337 * (9) Reader finishes execve() operation and restores this domain from
338 * "struct linux_binprm"->cred->security.
340 * By updating domain->users at (5), we can solve this race problem
341 * by rechecking domain->users at (8).
343 if (atomic_read(&domain->users))
345 list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
346 tomoyo_del_acl(&acl->list);
347 tomoyo_memory_free(acl);
349 tomoyo_put_name(domain->domainname);
354 * tomoyo_del_condition - Delete members in "struct tomoyo_condition".
356 * @element: Pointer to "struct list_head".
360 void tomoyo_del_condition(struct list_head *element)
362 struct tomoyo_condition *cond = container_of(element, typeof(*cond),
364 const u16 condc = cond->condc;
365 const u16 numbers_count = cond->numbers_count;
366 const u16 names_count = cond->names_count;
367 const u16 argc = cond->argc;
368 const u16 envc = cond->envc;
370 const struct tomoyo_condition_element *condp
371 = (const struct tomoyo_condition_element *) (cond + 1);
372 struct tomoyo_number_union *numbers_p
373 = (struct tomoyo_number_union *) (condp + condc);
374 struct tomoyo_name_union *names_p
375 = (struct tomoyo_name_union *) (numbers_p + numbers_count);
376 const struct tomoyo_argv *argv
377 = (const struct tomoyo_argv *) (names_p + names_count);
378 const struct tomoyo_envp *envp
379 = (const struct tomoyo_envp *) (argv + argc);
380 for (i = 0; i < numbers_count; i++)
381 tomoyo_put_number_union(numbers_p++);
382 for (i = 0; i < names_count; i++)
383 tomoyo_put_name_union(names_p++);
384 for (i = 0; i < argc; argv++, i++)
385 tomoyo_put_name(argv->value);
386 for (i = 0; i < envc; envp++, i++) {
387 tomoyo_put_name(envp->name);
388 tomoyo_put_name(envp->value);
393 * tomoyo_del_name - Delete members in "struct tomoyo_name".
395 * @element: Pointer to "struct list_head".
399 static void tomoyo_del_name(struct list_head *element)
401 const struct tomoyo_name *ptr =
402 container_of(element, typeof(*ptr), head.list);
406 * tomoyo_del_path_group - Delete members in "struct tomoyo_path_group".
408 * @element: Pointer to "struct list_head".
412 static void tomoyo_del_path_group(struct list_head *element)
414 struct tomoyo_path_group *member =
415 container_of(element, typeof(*member), head.list);
416 tomoyo_put_name(member->member_name);
420 * tomoyo_del_group - Delete "struct tomoyo_group".
422 * @element: Pointer to "struct list_head".
426 static void tomoyo_del_group(struct list_head *element)
428 struct tomoyo_group *group =
429 container_of(element, typeof(*group), head.list);
430 tomoyo_put_name(group->group_name);
434 * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group".
436 * @element: Pointer to "struct list_head".
440 static void tomoyo_del_number_group(struct list_head *element)
442 struct tomoyo_number_group *member =
443 container_of(element, typeof(*member), head.list);
447 * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head".
449 * @id: One of values in "enum tomoyo_policy_id".
450 * @member_list: Pointer to "struct list_head".
452 * Returns true if some elements are deleted, false otherwise.
454 static bool tomoyo_collect_member(const enum tomoyo_policy_id id,
455 struct list_head *member_list)
457 struct tomoyo_acl_head *member;
458 list_for_each_entry(member, member_list, list) {
459 if (!member->is_deleted)
461 if (!tomoyo_add_to_gc(id, &member->list))
468 * tomoyo_collect_acl - Delete elements in "struct tomoyo_domain_info".
470 * @list: Pointer to "struct list_head".
472 * Returns true if some elements are deleted, false otherwise.
474 static bool tomoyo_collect_acl(struct list_head *list)
476 struct tomoyo_acl_info *acl;
477 list_for_each_entry(acl, list, list) {
478 if (!acl->is_deleted)
480 if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
487 * tomoyo_collect_entry - Scan lists for deleted elements.
491 static void tomoyo_collect_entry(void)
494 enum tomoyo_policy_id id;
495 struct tomoyo_policy_namespace *ns;
497 if (mutex_lock_interruptible(&tomoyo_policy_lock))
499 idx = tomoyo_read_lock();
501 struct tomoyo_domain_info *domain;
502 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
503 if (!tomoyo_collect_acl(&domain->acl_info_list))
505 if (!domain->is_deleted || atomic_read(&domain->users))
508 * Nobody is referring this domain. But somebody may
509 * refer this domain after successful execve().
510 * We recheck domain->users after SRCU synchronization.
512 if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
516 list_for_each_entry_rcu(ns, &tomoyo_namespace_list, namespace_list) {
517 for (id = 0; id < TOMOYO_MAX_POLICY; id++)
518 if (!tomoyo_collect_member(id, &ns->policy_list[id]))
520 for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++)
521 if (!tomoyo_collect_acl(&ns->acl_group[i]))
523 for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
524 struct list_head *list = &ns->group_list[i];
525 struct tomoyo_group *group;
528 id = TOMOYO_ID_PATH_GROUP;
531 id = TOMOYO_ID_NUMBER_GROUP;
534 list_for_each_entry(group, list, head.list) {
535 if (!tomoyo_collect_member
536 (id, &group->member_list))
538 if (!list_empty(&group->member_list) ||
539 atomic_read(&group->head.users))
541 if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
547 id = TOMOYO_ID_CONDITION;
548 for (i = 0; i < TOMOYO_MAX_HASH + 1; i++) {
549 struct list_head *list = !i ?
550 &tomoyo_condition_list : &tomoyo_name_list[i - 1];
551 struct tomoyo_shared_acl_head *ptr;
552 list_for_each_entry(ptr, list, list) {
553 if (atomic_read(&ptr->users))
555 if (!tomoyo_add_to_gc(id, &ptr->list))
561 tomoyo_read_unlock(idx);
562 mutex_unlock(&tomoyo_policy_lock);
566 * tomoyo_kfree_entry - Delete entries in tomoyo_gc_list.
568 * Returns true if some entries were kfree()d, false otherwise.
570 static bool tomoyo_kfree_entry(void)
573 struct tomoyo_gc *tmp;
576 list_for_each_entry_safe(p, tmp, &tomoyo_gc_list, list) {
577 struct list_head *element = p->element;
580 * list_del_rcu() in tomoyo_add_to_gc() guarantees that the
581 * list element became no longer reachable from the list which
582 * the element was originally on (e.g. tomoyo_domain_list).
583 * Also, synchronize_srcu() in tomoyo_gc_thread() guarantees
584 * that the list element became no longer referenced by syscall
587 * However, there are three users which may still be using the
588 * list element. We need to defer until all of these users
589 * forget the list element.
591 * Firstly, defer until "struct tomoyo_io_buffer"->r.{domain,
592 * group,acl} and "struct tomoyo_io_buffer"->w.domain forget
595 if (tomoyo_struct_used_by_io_buffer(element))
598 * Secondly, defer until all other elements in the
599 * tomoyo_gc_list list forget the list element.
601 if (tomoyo_element_linked_by_gc((const u8 *) element, p->size))
604 case TOMOYO_ID_TRANSITION_CONTROL:
605 tomoyo_del_transition_control(element);
607 case TOMOYO_ID_AGGREGATOR:
608 tomoyo_del_aggregator(element);
610 case TOMOYO_ID_MANAGER:
611 tomoyo_del_manager(element);
613 case TOMOYO_ID_CONDITION:
614 tomoyo_del_condition(element);
618 * Thirdly, defer until all "struct tomoyo_io_buffer"
619 * ->r.w[] forget the list element.
621 if (tomoyo_name_used_by_io_buffer(
622 container_of(element, typeof(struct tomoyo_name),
623 head.list)->entry.name, p->size))
625 tomoyo_del_name(element);
628 tomoyo_del_acl(element);
630 case TOMOYO_ID_DOMAIN:
631 if (!tomoyo_del_domain(element))
634 case TOMOYO_ID_PATH_GROUP:
635 tomoyo_del_path_group(element);
637 case TOMOYO_ID_GROUP:
638 tomoyo_del_group(element);
640 case TOMOYO_ID_NUMBER_GROUP:
641 tomoyo_del_number_group(element);
643 case TOMOYO_MAX_POLICY:
646 tomoyo_memory_free(element);
649 tomoyo_gc_list_len--;
656 * tomoyo_gc_thread - Garbage collector thread function.
660 * In case OOM-killer choose this thread for termination, we create this thread
661 * as a short live thread whenever /sys/kernel/security/tomoyo/ interface was
666 static int tomoyo_gc_thread(void *unused)
668 /* Garbage collector thread is exclusive. */
669 static DEFINE_MUTEX(tomoyo_gc_mutex);
670 if (!mutex_trylock(&tomoyo_gc_mutex))
674 tomoyo_collect_entry();
675 if (list_empty(&tomoyo_gc_list))
677 synchronize_srcu(&tomoyo_ss);
678 } while (tomoyo_kfree_entry());
680 struct tomoyo_io_buffer *head;
681 struct tomoyo_io_buffer *tmp;
683 spin_lock(&tomoyo_io_buffer_list_lock);
684 list_for_each_entry_safe(head, tmp, &tomoyo_io_buffer_list,
688 list_del(&head->list);
689 kfree(head->read_buf);
690 kfree(head->write_buf);
693 spin_unlock(&tomoyo_io_buffer_list_lock);
695 mutex_unlock(&tomoyo_gc_mutex);
697 /* This acts as do_exit(0). */
702 * tomoyo_notify_gc - Register/unregister /sys/kernel/security/tomoyo/ users.
704 * @head: Pointer to "struct tomoyo_io_buffer".
705 * @is_register: True if register, false if unregister.
709 void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register)
711 bool is_write = false;
713 spin_lock(&tomoyo_io_buffer_list_lock);
716 list_add(&head->list, &tomoyo_io_buffer_list);
718 is_write = head->write_buf != NULL;
719 if (!--head->users) {
720 list_del(&head->list);
721 kfree(head->read_buf);
722 kfree(head->write_buf);
726 spin_unlock(&tomoyo_io_buffer_list_lock);
728 struct task_struct *task = kthread_create(tomoyo_gc_thread,
732 wake_up_process(task);