2 * security/tomoyo/domain.c
4 * Domain transition functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
10 #include <linux/binfmts.h>
11 #include <linux/slab.h>
13 /* Variables definitions.*/
15 /* The initial domain. */
16 struct tomoyo_domain_info tomoyo_kernel_domain;
19 * tomoyo_update_policy - Update an entry for exception policy.
21 * @new_entry: Pointer to "struct tomoyo_acl_info".
22 * @size: Size of @new_entry in bytes.
23 * @is_delete: True if it is a delete request.
24 * @list: Pointer to "struct list_head".
25 * @check_duplicate: Callback function to find duplicated entry.
27 * Returns 0 on success, negative value otherwise.
29 * Caller holds tomoyo_read_lock().
31 int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
32 bool is_delete, struct list_head *list,
33 bool (*check_duplicate) (const struct tomoyo_acl_head
35 const struct tomoyo_acl_head
38 int error = is_delete ? -ENOENT : -ENOMEM;
39 struct tomoyo_acl_head *entry;
41 if (mutex_lock_interruptible(&tomoyo_policy_lock))
43 list_for_each_entry_rcu(entry, list, list) {
44 if (!check_duplicate(entry, new_entry))
46 entry->is_deleted = is_delete;
50 if (error && !is_delete) {
51 entry = tomoyo_commit_ok(new_entry, size);
53 list_add_tail_rcu(&entry->list, list);
57 mutex_unlock(&tomoyo_policy_lock);
62 * tomoyo_update_domain - Update an entry for domain policy.
64 * @new_entry: Pointer to "struct tomoyo_acl_info".
65 * @size: Size of @new_entry in bytes.
66 * @is_delete: True if it is a delete request.
67 * @domain: Pointer to "struct tomoyo_domain_info".
68 * @check_duplicate: Callback function to find duplicated entry.
69 * @merge_duplicate: Callback function to merge duplicated entry.
71 * Returns 0 on success, negative value otherwise.
73 * Caller holds tomoyo_read_lock().
75 int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
76 bool is_delete, struct tomoyo_domain_info *domain,
77 bool (*check_duplicate) (const struct tomoyo_acl_info
79 const struct tomoyo_acl_info
81 bool (*merge_duplicate) (struct tomoyo_acl_info *,
82 struct tomoyo_acl_info *,
85 int error = is_delete ? -ENOENT : -ENOMEM;
86 struct tomoyo_acl_info *entry;
88 if (mutex_lock_interruptible(&tomoyo_policy_lock))
90 list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
91 if (!check_duplicate(entry, new_entry))
94 entry->is_deleted = merge_duplicate(entry, new_entry,
97 entry->is_deleted = is_delete;
101 if (error && !is_delete) {
102 entry = tomoyo_commit_ok(new_entry, size);
104 list_add_tail_rcu(&entry->list, &domain->acl_info_list);
108 mutex_unlock(&tomoyo_policy_lock);
112 void tomoyo_check_acl(struct tomoyo_request_info *r,
113 bool (*check_entry) (const struct tomoyo_request_info *,
114 const struct tomoyo_acl_info *))
116 const struct tomoyo_domain_info *domain = r->domain;
117 struct tomoyo_acl_info *ptr;
119 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
120 if (ptr->is_deleted || ptr->type != r->param_type)
122 if (check_entry(r, ptr)) {
130 /* The list for "struct tomoyo_domain_info". */
131 LIST_HEAD(tomoyo_domain_list);
133 struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
134 struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
137 * tomoyo_get_last_name - Get last component of a domainname.
139 * @domain: Pointer to "struct tomoyo_domain_info".
141 * Returns the last component of the domainname.
143 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
145 const char *cp0 = domain->domainname->name;
146 const char *cp1 = strrchr(cp0, ' ');
153 static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
155 const struct tomoyo_acl_head *
158 const struct tomoyo_domain_initializer_entry *p1 =
159 container_of(a, typeof(*p1), head);
160 const struct tomoyo_domain_initializer_entry *p2 =
161 container_of(b, typeof(*p2), head);
162 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
163 && p1->domainname == p2->domainname
164 && p1->program == p2->program;
168 * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
170 * @domainname: The name of domain. May be NULL.
171 * @program: The name of program.
172 * @is_not: True if it is "no_initialize_domain" entry.
173 * @is_delete: True if it is a delete request.
175 * Returns 0 on success, negative value otherwise.
177 * Caller holds tomoyo_read_lock().
179 static int tomoyo_update_domain_initializer_entry(const char *domainname,
182 const bool is_delete)
184 struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
185 int error = is_delete ? -ENOENT : -ENOMEM;
187 if (!tomoyo_correct_path(program))
190 if (!tomoyo_domain_def(domainname) &&
191 tomoyo_correct_path(domainname))
192 e.is_last_name = true;
193 else if (!tomoyo_correct_domain(domainname))
195 e.domainname = tomoyo_get_name(domainname);
199 e.program = tomoyo_get_name(program);
202 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
204 [TOMOYO_ID_DOMAIN_INITIALIZER],
205 tomoyo_same_domain_initializer_entry);
207 tomoyo_put_name(e.domainname);
208 tomoyo_put_name(e.program);
213 * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
215 * @head: Pointer to "struct tomoyo_io_buffer".
217 * Returns true on success, false otherwise.
219 * Caller holds tomoyo_read_lock().
221 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
223 struct list_head *pos;
226 list_for_each_cookie(pos, head->read_var2, &tomoyo_policy_list
227 [TOMOYO_ID_DOMAIN_INITIALIZER]) {
229 const char *from = "";
230 const char *domain = "";
231 struct tomoyo_domain_initializer_entry *ptr;
232 ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
234 if (ptr->head.is_deleted)
236 no = ptr->is_not ? "no_" : "";
237 if (ptr->domainname) {
239 domain = ptr->domainname->name;
241 done = tomoyo_io_printf(head,
242 "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
243 "%s%s%s\n", no, ptr->program->name,
252 * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
254 * @data: String to parse.
255 * @is_not: True if it is "no_initialize_domain" entry.
256 * @is_delete: True if it is a delete request.
258 * Returns 0 on success, negative value otherwise.
260 * Caller holds tomoyo_read_lock().
262 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
263 const bool is_delete)
265 char *cp = strstr(data, " from ");
269 return tomoyo_update_domain_initializer_entry(cp + 6, data,
273 return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
278 * tomoyo_domain_initializer - Check whether the given program causes domainname reinitialization.
280 * @domainname: The name of domain.
281 * @program: The name of program.
282 * @last_name: The last component of @domainname.
284 * Returns true if executing @program reinitializes domain transition,
287 * Caller holds tomoyo_read_lock().
289 static bool tomoyo_domain_initializer(const struct tomoyo_path_info *
291 const struct tomoyo_path_info *program,
292 const struct tomoyo_path_info *
295 struct tomoyo_domain_initializer_entry *ptr;
298 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
299 [TOMOYO_ID_DOMAIN_INITIALIZER], head.list) {
300 if (ptr->head.is_deleted)
302 if (ptr->domainname) {
303 if (!ptr->is_last_name) {
304 if (ptr->domainname != domainname)
307 if (tomoyo_pathcmp(ptr->domainname, last_name))
311 if (tomoyo_pathcmp(ptr->program, program))
322 static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
323 const struct tomoyo_acl_head *b)
325 const struct tomoyo_domain_keeper_entry *p1 =
326 container_of(a, typeof(*p1), head);
327 const struct tomoyo_domain_keeper_entry *p2 =
328 container_of(b, typeof(*p2), head);
329 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
330 && p1->domainname == p2->domainname
331 && p1->program == p2->program;
335 * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
337 * @domainname: The name of domain.
338 * @program: The name of program. May be NULL.
339 * @is_not: True if it is "no_keep_domain" entry.
340 * @is_delete: True if it is a delete request.
342 * Returns 0 on success, negative value otherwise.
344 * Caller holds tomoyo_read_lock().
346 static int tomoyo_update_domain_keeper_entry(const char *domainname,
349 const bool is_delete)
351 struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
352 int error = is_delete ? -ENOENT : -ENOMEM;
354 if (!tomoyo_domain_def(domainname) &&
355 tomoyo_correct_path(domainname))
356 e.is_last_name = true;
357 else if (!tomoyo_correct_domain(domainname))
360 if (!tomoyo_correct_path(program))
362 e.program = tomoyo_get_name(program);
366 e.domainname = tomoyo_get_name(domainname);
369 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
371 [TOMOYO_ID_DOMAIN_KEEPER],
372 tomoyo_same_domain_keeper_entry);
374 tomoyo_put_name(e.domainname);
375 tomoyo_put_name(e.program);
380 * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
382 * @data: String to parse.
383 * @is_not: True if it is "no_keep_domain" entry.
384 * @is_delete: True if it is a delete request.
386 * Caller holds tomoyo_read_lock().
388 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
389 const bool is_delete)
391 char *cp = strstr(data, " from ");
395 return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
398 return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
402 * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
404 * @head: Pointer to "struct tomoyo_io_buffer".
406 * Returns true on success, false otherwise.
408 * Caller holds tomoyo_read_lock().
410 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
412 struct list_head *pos;
415 list_for_each_cookie(pos, head->read_var2,
416 &tomoyo_policy_list[TOMOYO_ID_DOMAIN_KEEPER]) {
417 struct tomoyo_domain_keeper_entry *ptr;
419 const char *from = "";
420 const char *program = "";
422 ptr = list_entry(pos, struct tomoyo_domain_keeper_entry,
424 if (ptr->head.is_deleted)
426 no = ptr->is_not ? "no_" : "";
429 program = ptr->program->name;
431 done = tomoyo_io_printf(head,
432 "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
433 "%s%s%s\n", no, program, from,
434 ptr->domainname->name);
442 * tomoyo_domain_keeper - Check whether the given program causes domain transition suppression.
444 * @domainname: The name of domain.
445 * @program: The name of program.
446 * @last_name: The last component of @domainname.
448 * Returns true if executing @program supresses domain transition,
451 * Caller holds tomoyo_read_lock().
453 static bool tomoyo_domain_keeper(const struct tomoyo_path_info *domainname,
454 const struct tomoyo_path_info *program,
455 const struct tomoyo_path_info *last_name)
457 struct tomoyo_domain_keeper_entry *ptr;
460 list_for_each_entry_rcu(ptr,
461 &tomoyo_policy_list[TOMOYO_ID_DOMAIN_KEEPER],
463 if (ptr->head.is_deleted)
465 if (!ptr->is_last_name) {
466 if (ptr->domainname != domainname)
469 if (tomoyo_pathcmp(ptr->domainname, last_name))
472 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
483 static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
484 const struct tomoyo_acl_head *b)
486 const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
488 const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
490 return p1->original_name == p2->original_name &&
491 p1->aggregated_name == p2->aggregated_name;
495 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
497 * @original_name: The original program's name.
498 * @aggregated_name: The program name to use.
499 * @is_delete: True if it is a delete request.
501 * Returns 0 on success, negative value otherwise.
503 * Caller holds tomoyo_read_lock().
505 static int tomoyo_update_aggregator_entry(const char *original_name,
506 const char *aggregated_name,
507 const bool is_delete)
509 struct tomoyo_aggregator_entry e = { };
510 int error = is_delete ? -ENOENT : -ENOMEM;
512 if (!tomoyo_correct_path(original_name) ||
513 !tomoyo_correct_path(aggregated_name))
515 e.original_name = tomoyo_get_name(original_name);
516 e.aggregated_name = tomoyo_get_name(aggregated_name);
517 if (!e.original_name || !e.aggregated_name ||
518 e.aggregated_name->is_patterned) /* No patterns allowed. */
520 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
521 &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
522 tomoyo_same_aggregator_entry);
524 tomoyo_put_name(e.original_name);
525 tomoyo_put_name(e.aggregated_name);
530 * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
532 * @head: Pointer to "struct tomoyo_io_buffer".
534 * Returns true on success, false otherwise.
536 * Caller holds tomoyo_read_lock().
538 bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head)
540 struct list_head *pos;
543 list_for_each_cookie(pos, head->read_var2,
544 &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR]) {
545 struct tomoyo_aggregator_entry *ptr;
547 ptr = list_entry(pos, struct tomoyo_aggregator_entry,
549 if (ptr->head.is_deleted)
551 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR
552 "%s %s\n", ptr->original_name->name,
553 ptr->aggregated_name->name);
561 * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
563 * @data: String to parse.
564 * @is_delete: True if it is a delete request.
566 * Returns 0 on success, negative value otherwise.
568 * Caller holds tomoyo_read_lock().
570 int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
572 char *cp = strchr(data, ' ');
577 return tomoyo_update_aggregator_entry(data, cp, is_delete);
580 static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
581 const struct tomoyo_acl_head *b)
583 const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
585 const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
587 return p1->original_name == p2->original_name &&
588 p1->aliased_name == p2->aliased_name;
592 * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
594 * @original_name: The original program's real name.
595 * @aliased_name: The symbolic program's symbolic link's name.
596 * @is_delete: True if it is a delete request.
598 * Returns 0 on success, negative value otherwise.
600 * Caller holds tomoyo_read_lock().
602 static int tomoyo_update_alias_entry(const char *original_name,
603 const char *aliased_name,
604 const bool is_delete)
606 struct tomoyo_alias_entry e = { };
607 int error = is_delete ? -ENOENT : -ENOMEM;
609 if (!tomoyo_correct_path(original_name) ||
610 !tomoyo_correct_path(aliased_name))
612 e.original_name = tomoyo_get_name(original_name);
613 e.aliased_name = tomoyo_get_name(aliased_name);
614 if (!e.original_name || !e.aliased_name ||
615 e.original_name->is_patterned || e.aliased_name->is_patterned)
616 goto out; /* No patterns allowed. */
617 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
618 &tomoyo_policy_list[TOMOYO_ID_ALIAS],
619 tomoyo_same_alias_entry);
621 tomoyo_put_name(e.original_name);
622 tomoyo_put_name(e.aliased_name);
627 * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
629 * @head: Pointer to "struct tomoyo_io_buffer".
631 * Returns true on success, false otherwise.
633 * Caller holds tomoyo_read_lock().
635 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
637 struct list_head *pos;
640 list_for_each_cookie(pos, head->read_var2,
641 &tomoyo_policy_list[TOMOYO_ID_ALIAS]) {
642 struct tomoyo_alias_entry *ptr;
644 ptr = list_entry(pos, struct tomoyo_alias_entry, head.list);
645 if (ptr->head.is_deleted)
647 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
648 ptr->original_name->name,
649 ptr->aliased_name->name);
657 * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
659 * @data: String to parse.
660 * @is_delete: True if it is a delete request.
662 * Returns 0 on success, negative value otherwise.
664 * Caller holds tomoyo_read_lock().
666 int tomoyo_write_alias_policy(char *data, const bool is_delete)
668 char *cp = strchr(data, ' ');
673 return tomoyo_update_alias_entry(data, cp, is_delete);
677 * tomoyo_find_or_assign_new_domain - Create a domain.
679 * @domainname: The name of domain.
680 * @profile: Profile number to assign if the domain was newly created.
682 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
684 * Caller holds tomoyo_read_lock().
686 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
690 struct tomoyo_domain_info *entry;
691 struct tomoyo_domain_info *domain = NULL;
692 const struct tomoyo_path_info *saved_domainname;
695 if (!tomoyo_correct_domain(domainname))
697 saved_domainname = tomoyo_get_name(domainname);
698 if (!saved_domainname)
700 entry = kzalloc(sizeof(*entry), GFP_NOFS);
701 if (mutex_lock_interruptible(&tomoyo_policy_lock))
703 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
704 if (domain->is_deleted ||
705 tomoyo_pathcmp(saved_domainname, domain->domainname))
710 if (!found && tomoyo_memory_ok(entry)) {
711 INIT_LIST_HEAD(&entry->acl_info_list);
712 entry->domainname = saved_domainname;
713 saved_domainname = NULL;
714 entry->profile = profile;
715 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
720 mutex_unlock(&tomoyo_policy_lock);
722 tomoyo_put_name(saved_domainname);
724 return found ? domain : NULL;
728 * tomoyo_find_next_domain - Find a domain.
730 * @bprm: Pointer to "struct linux_binprm".
732 * Returns 0 on success, negative value otherwise.
734 * Caller holds tomoyo_read_lock().
736 int tomoyo_find_next_domain(struct linux_binprm *bprm)
738 struct tomoyo_request_info r;
739 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
740 struct tomoyo_domain_info *old_domain = tomoyo_domain();
741 struct tomoyo_domain_info *domain = NULL;
742 const char *old_domain_name = old_domain->domainname->name;
743 const char *original_name = bprm->filename;
746 int retval = -ENOMEM;
747 bool need_kfree = false;
748 struct tomoyo_path_info rn = { }; /* real name */
749 struct tomoyo_path_info sn = { }; /* symlink name */
750 struct tomoyo_path_info ln; /* last name */
752 ln.name = tomoyo_get_last_name(old_domain);
753 tomoyo_fill_path_info(&ln);
754 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
755 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
764 /* Get tomoyo_realpath of program. */
766 rn.name = tomoyo_realpath(original_name);
769 tomoyo_fill_path_info(&rn);
772 /* Get tomoyo_realpath of symbolic link. */
773 sn.name = tomoyo_realpath_nofollow(original_name);
776 tomoyo_fill_path_info(&sn);
778 /* Check 'alias' directive. */
779 if (tomoyo_pathcmp(&rn, &sn)) {
780 struct tomoyo_alias_entry *ptr;
781 /* Is this program allowed to be called via symbolic links? */
782 list_for_each_entry_rcu(ptr,
783 &tomoyo_policy_list[TOMOYO_ID_ALIAS],
785 if (ptr->head.is_deleted ||
786 tomoyo_pathcmp(&rn, ptr->original_name) ||
787 tomoyo_pathcmp(&sn, ptr->aliased_name))
791 /* This is OK because it is read only. */
792 rn = *ptr->aliased_name;
797 /* Check 'aggregator' directive. */
799 struct tomoyo_aggregator_entry *ptr;
800 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
801 [TOMOYO_ID_AGGREGATOR], head.list) {
802 if (ptr->head.is_deleted ||
803 !tomoyo_path_matches_pattern(&rn,
809 /* This is OK because it is read only. */
810 rn = *ptr->aggregated_name;
815 /* Check execute permission. */
816 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
817 if (retval == TOMOYO_RETRY_REQUEST)
822 if (tomoyo_domain_initializer(old_domain->domainname, &rn, &ln)) {
823 /* Transit to the child of tomoyo_kernel_domain domain. */
824 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
825 TOMOYO_ROOT_NAME " " "%s", rn.name);
826 } else if (old_domain == &tomoyo_kernel_domain &&
827 !tomoyo_policy_loaded) {
829 * Needn't to transit from kernel domain before starting
830 * /sbin/init. But transit from kernel domain if executing
831 * initializers because they might start before /sbin/init.
834 } else if (tomoyo_domain_keeper(old_domain->domainname, &rn, &ln)) {
835 /* Keep current domain. */
838 /* Normal domain transition. */
839 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
840 "%s %s", old_domain_name, rn.name);
842 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
844 domain = tomoyo_find_domain(tmp);
848 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
850 if (error == TOMOYO_RETRY_REQUEST)
855 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
859 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
863 old_domain->transition_failed = true;
867 /* Update reference count on "struct tomoyo_domain_info". */
868 atomic_inc(&domain->users);
869 bprm->cred->security = domain;