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_domain - Update an entry for domain 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 * @domain: Pointer to "struct tomoyo_domain_info".
25 * @check_duplicate: Callback function to find duplicated entry.
26 * @merge_duplicate: Callback function to merge duplicated entry.
28 * Returns 0 on success, negative value otherwise.
30 * Caller holds tomoyo_read_lock().
32 int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
33 bool is_delete, struct tomoyo_domain_info *domain,
34 bool (*check_duplicate) (const struct tomoyo_acl_info
36 const struct tomoyo_acl_info
38 bool (*merge_duplicate) (struct tomoyo_acl_info *,
39 struct tomoyo_acl_info *,
42 int error = is_delete ? -ENOENT : -ENOMEM;
43 struct tomoyo_acl_info *entry;
45 if (mutex_lock_interruptible(&tomoyo_policy_lock))
47 list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
48 if (!check_duplicate(entry, new_entry))
51 entry->is_deleted = merge_duplicate(entry, new_entry,
54 entry->is_deleted = is_delete;
58 if (error && !is_delete) {
59 entry = tomoyo_commit_ok(new_entry, size);
61 list_add_tail_rcu(&entry->list, &domain->acl_info_list);
65 mutex_unlock(&tomoyo_policy_lock);
70 * tomoyo_domain_list is used for holding list of domains.
71 * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
72 * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
74 * An entry is added by
76 * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \
77 * /sys/kernel/security/tomoyo/domain_policy
81 * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \
82 * /sys/kernel/security/tomoyo/domain_policy
84 * and all entries are retrieved by
86 * # cat /sys/kernel/security/tomoyo/domain_policy
88 * A domain is added by
90 * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy
94 * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy
96 * and all domains are retrieved by
98 * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy
100 * Normally, a domainname is monotonically getting longer because a domainname
101 * which the process will belong to if an execve() operation succeeds is
102 * defined as a concatenation of "current domainname" + "pathname passed to
104 * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for
107 LIST_HEAD(tomoyo_domain_list);
110 * tomoyo_get_last_name - Get last component of a domainname.
112 * @domain: Pointer to "struct tomoyo_domain_info".
114 * Returns the last component of the domainname.
116 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
118 const char *cp0 = domain->domainname->name;
119 const char *cp1 = strrchr(cp0, ' ');
127 * tomoyo_domain_initializer_list is used for holding list of programs which
128 * triggers reinitialization of domainname. Normally, a domainname is
129 * monotonically getting longer. But sometimes, we restart daemon programs.
130 * It would be convenient for us that "a daemon started upon system boot" and
131 * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO
132 * provides a way to shorten domainnames.
134 * An entry is added by
136 * # echo 'initialize_domain /usr/sbin/httpd' > \
137 * /sys/kernel/security/tomoyo/exception_policy
141 * # echo 'delete initialize_domain /usr/sbin/httpd' > \
142 * /sys/kernel/security/tomoyo/exception_policy
144 * and all entries are retrieved by
146 * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy
148 * In the example above, /usr/sbin/httpd will belong to
149 * "<kernel> /usr/sbin/httpd" domain.
151 * You may specify a domainname using "from" keyword.
152 * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
153 * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd"
154 * domain to belong to "<kernel> /usr/sbin/httpd" domain.
156 * You may add "no_" prefix to "initialize_domain".
157 * "initialize_domain /usr/sbin/httpd" and
158 * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
159 * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain
160 * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain.
162 LIST_HEAD(tomoyo_domain_initializer_list);
165 * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
167 * @domainname: The name of domain. May be NULL.
168 * @program: The name of program.
169 * @is_not: True if it is "no_initialize_domain" entry.
170 * @is_delete: True if it is a delete request.
172 * Returns 0 on success, negative value otherwise.
174 * Caller holds tomoyo_read_lock().
176 static int tomoyo_update_domain_initializer_entry(const char *domainname,
179 const bool is_delete)
181 struct tomoyo_domain_initializer_entry *ptr;
182 struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
183 int error = is_delete ? -ENOENT : -ENOMEM;
185 if (!tomoyo_is_correct_path(program))
188 if (!tomoyo_is_domain_def(domainname) &&
189 tomoyo_is_correct_path(domainname))
190 e.is_last_name = true;
191 else if (!tomoyo_is_correct_domain(domainname))
193 e.domainname = tomoyo_get_name(domainname);
197 e.program = tomoyo_get_name(program);
200 if (mutex_lock_interruptible(&tomoyo_policy_lock))
202 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
204 if (!tomoyo_is_same_domain_initializer_entry(ptr, &e))
206 ptr->head.is_deleted = is_delete;
210 if (!is_delete && error) {
211 struct tomoyo_domain_initializer_entry *entry =
212 tomoyo_commit_ok(&e, sizeof(e));
214 list_add_tail_rcu(&entry->head.list,
215 &tomoyo_domain_initializer_list);
219 mutex_unlock(&tomoyo_policy_lock);
221 tomoyo_put_name(e.domainname);
222 tomoyo_put_name(e.program);
227 * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
229 * @head: Pointer to "struct tomoyo_io_buffer".
231 * Returns true on success, false otherwise.
233 * Caller holds tomoyo_read_lock().
235 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
237 struct list_head *pos;
240 list_for_each_cookie(pos, head->read_var2,
241 &tomoyo_domain_initializer_list) {
243 const char *from = "";
244 const char *domain = "";
245 struct tomoyo_domain_initializer_entry *ptr;
246 ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
248 if (ptr->head.is_deleted)
250 no = ptr->is_not ? "no_" : "";
251 if (ptr->domainname) {
253 domain = ptr->domainname->name;
255 done = tomoyo_io_printf(head,
256 "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
257 "%s%s%s\n", no, ptr->program->name,
266 * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
268 * @data: String to parse.
269 * @is_not: True if it is "no_initialize_domain" entry.
270 * @is_delete: True if it is a delete request.
272 * Returns 0 on success, negative value otherwise.
274 * Caller holds tomoyo_read_lock().
276 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
277 const bool is_delete)
279 char *cp = strstr(data, " from ");
283 return tomoyo_update_domain_initializer_entry(cp + 6, data,
287 return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
292 * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
294 * @domainname: The name of domain.
295 * @program: The name of program.
296 * @last_name: The last component of @domainname.
298 * Returns true if executing @program reinitializes domain transition,
301 * Caller holds tomoyo_read_lock().
303 static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
305 const struct tomoyo_path_info *program,
306 const struct tomoyo_path_info *
309 struct tomoyo_domain_initializer_entry *ptr;
312 list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
314 if (ptr->head.is_deleted)
316 if (ptr->domainname) {
317 if (!ptr->is_last_name) {
318 if (ptr->domainname != domainname)
321 if (tomoyo_pathcmp(ptr->domainname, last_name))
325 if (tomoyo_pathcmp(ptr->program, program))
337 * tomoyo_domain_keeper_list is used for holding list of domainnames which
338 * suppresses domain transition. Normally, a domainname is monotonically
339 * getting longer. But sometimes, we want to suppress domain transition.
340 * It would be convenient for us that programs executed from a login session
341 * belong to the same domain. Thus, TOMOYO provides a way to suppress domain
344 * An entry is added by
346 * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
347 * /sys/kernel/security/tomoyo/exception_policy
351 * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
352 * /sys/kernel/security/tomoyo/exception_policy
354 * and all entries are retrieved by
356 * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy
358 * In the example above, any process which belongs to
359 * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain,
360 * unless explicitly specified by "initialize_domain" or "no_keep_domain".
362 * You may specify a program using "from" keyword.
363 * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash"
364 * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash"
365 * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain.
367 * You may add "no_" prefix to "keep_domain".
368 * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and
369 * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will
370 * cause "/usr/bin/passwd" to belong to
371 * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless
372 * explicitly specified by "initialize_domain".
374 LIST_HEAD(tomoyo_domain_keeper_list);
377 * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
379 * @domainname: The name of domain.
380 * @program: The name of program. May be NULL.
381 * @is_not: True if it is "no_keep_domain" entry.
382 * @is_delete: True if it is a delete request.
384 * Returns 0 on success, negative value otherwise.
386 * Caller holds tomoyo_read_lock().
388 static int tomoyo_update_domain_keeper_entry(const char *domainname,
391 const bool is_delete)
393 struct tomoyo_domain_keeper_entry *ptr;
394 struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
395 int error = is_delete ? -ENOENT : -ENOMEM;
397 if (!tomoyo_is_domain_def(domainname) &&
398 tomoyo_is_correct_path(domainname))
399 e.is_last_name = true;
400 else if (!tomoyo_is_correct_domain(domainname))
403 if (!tomoyo_is_correct_path(program))
405 e.program = tomoyo_get_name(program);
409 e.domainname = tomoyo_get_name(domainname);
412 if (mutex_lock_interruptible(&tomoyo_policy_lock))
414 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, head.list) {
415 if (!tomoyo_is_same_domain_keeper_entry(ptr, &e))
417 ptr->head.is_deleted = is_delete;
421 if (!is_delete && error) {
422 struct tomoyo_domain_keeper_entry *entry =
423 tomoyo_commit_ok(&e, sizeof(e));
425 list_add_tail_rcu(&entry->head.list,
426 &tomoyo_domain_keeper_list);
430 mutex_unlock(&tomoyo_policy_lock);
432 tomoyo_put_name(e.domainname);
433 tomoyo_put_name(e.program);
438 * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
440 * @data: String to parse.
441 * @is_not: True if it is "no_keep_domain" entry.
442 * @is_delete: True if it is a delete request.
444 * Caller holds tomoyo_read_lock().
446 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
447 const bool is_delete)
449 char *cp = strstr(data, " from ");
453 return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
456 return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
460 * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
462 * @head: Pointer to "struct tomoyo_io_buffer".
464 * Returns true on success, false otherwise.
466 * Caller holds tomoyo_read_lock().
468 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
470 struct list_head *pos;
473 list_for_each_cookie(pos, head->read_var2,
474 &tomoyo_domain_keeper_list) {
475 struct tomoyo_domain_keeper_entry *ptr;
477 const char *from = "";
478 const char *program = "";
480 ptr = list_entry(pos, struct tomoyo_domain_keeper_entry,
482 if (ptr->head.is_deleted)
484 no = ptr->is_not ? "no_" : "";
487 program = ptr->program->name;
489 done = tomoyo_io_printf(head,
490 "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
491 "%s%s%s\n", no, program, from,
492 ptr->domainname->name);
500 * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
502 * @domainname: The name of domain.
503 * @program: The name of program.
504 * @last_name: The last component of @domainname.
506 * Returns true if executing @program supresses domain transition,
509 * Caller holds tomoyo_read_lock().
511 static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
512 const struct tomoyo_path_info *program,
513 const struct tomoyo_path_info *last_name)
515 struct tomoyo_domain_keeper_entry *ptr;
518 list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, head.list) {
519 if (ptr->head.is_deleted)
521 if (!ptr->is_last_name) {
522 if (ptr->domainname != domainname)
525 if (tomoyo_pathcmp(ptr->domainname, last_name))
528 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
540 * tomoyo_aggregator_list is used for holding list of rewrite table for
541 * execve() request. Some programs provides similar functionality. This keyword
542 * allows users to aggregate such programs.
544 * Entries are added by
546 * # echo 'aggregator /usr/bin/vi /./editor' > \
547 * /sys/kernel/security/tomoyo/exception_policy
548 * # echo 'aggregator /usr/bin/emacs /./editor' > \
549 * /sys/kernel/security/tomoyo/exception_policy
553 * # echo 'delete aggregator /usr/bin/vi /./editor' > \
554 * /sys/kernel/security/tomoyo/exception_policy
555 * # echo 'delete aggregator /usr/bin/emacs /./editor' > \
556 * /sys/kernel/security/tomoyo/exception_policy
558 * and all entries are retrieved by
560 * # grep ^aggregator /sys/kernel/security/tomoyo/exception_policy
562 * In the example above, if /usr/bin/vi or /usr/bin/emacs are executed,
563 * permission is checked for /./editor and domainname which the current process
564 * will belong to after execve() succeeds is calculated using /./editor .
566 LIST_HEAD(tomoyo_aggregator_list);
569 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
571 * @original_name: The original program's name.
572 * @aggregated_name: The program name to use.
573 * @is_delete: True if it is a delete request.
575 * Returns 0 on success, negative value otherwise.
577 * Caller holds tomoyo_read_lock().
579 static int tomoyo_update_aggregator_entry(const char *original_name,
580 const char *aggregated_name,
581 const bool is_delete)
583 struct tomoyo_aggregator_entry *ptr;
584 struct tomoyo_aggregator_entry e = { };
585 int error = is_delete ? -ENOENT : -ENOMEM;
587 if (!tomoyo_is_correct_path(original_name) ||
588 !tomoyo_is_correct_path(aggregated_name))
590 e.original_name = tomoyo_get_name(original_name);
591 e.aggregated_name = tomoyo_get_name(aggregated_name);
592 if (!e.original_name || !e.aggregated_name ||
593 e.aggregated_name->is_patterned) /* No patterns allowed. */
595 if (mutex_lock_interruptible(&tomoyo_policy_lock))
597 list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list, head.list) {
598 if (!tomoyo_is_same_aggregator_entry(ptr, &e))
600 ptr->head.is_deleted = is_delete;
604 if (!is_delete && error) {
605 struct tomoyo_aggregator_entry *entry =
606 tomoyo_commit_ok(&e, sizeof(e));
608 list_add_tail_rcu(&entry->head.list,
609 &tomoyo_aggregator_list);
613 mutex_unlock(&tomoyo_policy_lock);
615 tomoyo_put_name(e.original_name);
616 tomoyo_put_name(e.aggregated_name);
621 * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
623 * @head: Pointer to "struct tomoyo_io_buffer".
625 * Returns true on success, false otherwise.
627 * Caller holds tomoyo_read_lock().
629 bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head)
631 struct list_head *pos;
634 list_for_each_cookie(pos, head->read_var2, &tomoyo_aggregator_list) {
635 struct tomoyo_aggregator_entry *ptr;
637 ptr = list_entry(pos, struct tomoyo_aggregator_entry,
639 if (ptr->head.is_deleted)
641 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR
642 "%s %s\n", ptr->original_name->name,
643 ptr->aggregated_name->name);
651 * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
653 * @data: String to parse.
654 * @is_delete: True if it is a delete request.
656 * Returns 0 on success, negative value otherwise.
658 * Caller holds tomoyo_read_lock().
660 int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
662 char *cp = strchr(data, ' ');
667 return tomoyo_update_aggregator_entry(data, cp, is_delete);
671 * tomoyo_alias_list is used for holding list of symlink's pathnames which are
672 * allowed to be passed to an execve() request. Normally, the domainname which
673 * the current process will belong to after execve() succeeds is calculated
674 * using dereferenced pathnames. But some programs behave differently depending
675 * on the name passed to argv[0]. For busybox, calculating domainname using
676 * dereferenced pathnames will cause all programs in the busybox to belong to
677 * the same domain. Thus, TOMOYO provides a way to allow use of symlink's
678 * pathname for checking execve()'s permission and calculating domainname which
679 * the current process will belong to after execve() succeeds.
681 * An entry is added by
683 * # echo 'alias /bin/busybox /bin/cat' > \
684 * /sys/kernel/security/tomoyo/exception_policy
688 * # echo 'delete alias /bin/busybox /bin/cat' > \
689 * /sys/kernel/security/tomoyo/exception_policy
691 * and all entries are retrieved by
693 * # grep ^alias /sys/kernel/security/tomoyo/exception_policy
695 * In the example above, if /bin/cat is a symlink to /bin/busybox and execution
696 * of /bin/cat is requested, permission is checked for /bin/cat rather than
697 * /bin/busybox and domainname which the current process will belong to after
698 * execve() succeeds is calculated using /bin/cat rather than /bin/busybox .
700 LIST_HEAD(tomoyo_alias_list);
703 * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
705 * @original_name: The original program's real name.
706 * @aliased_name: The symbolic program's symbolic link's name.
707 * @is_delete: True if it is a delete request.
709 * Returns 0 on success, negative value otherwise.
711 * Caller holds tomoyo_read_lock().
713 static int tomoyo_update_alias_entry(const char *original_name,
714 const char *aliased_name,
715 const bool is_delete)
717 struct tomoyo_alias_entry *ptr;
718 struct tomoyo_alias_entry e = { };
719 int error = is_delete ? -ENOENT : -ENOMEM;
721 if (!tomoyo_is_correct_path(original_name) ||
722 !tomoyo_is_correct_path(aliased_name))
724 e.original_name = tomoyo_get_name(original_name);
725 e.aliased_name = tomoyo_get_name(aliased_name);
726 if (!e.original_name || !e.aliased_name ||
727 e.original_name->is_patterned || e.aliased_name->is_patterned)
728 goto out; /* No patterns allowed. */
729 if (mutex_lock_interruptible(&tomoyo_policy_lock))
731 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, head.list) {
732 if (!tomoyo_is_same_alias_entry(ptr, &e))
734 ptr->head.is_deleted = is_delete;
738 if (!is_delete && error) {
739 struct tomoyo_alias_entry *entry =
740 tomoyo_commit_ok(&e, sizeof(e));
742 list_add_tail_rcu(&entry->head.list,
747 mutex_unlock(&tomoyo_policy_lock);
749 tomoyo_put_name(e.original_name);
750 tomoyo_put_name(e.aliased_name);
755 * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
757 * @head: Pointer to "struct tomoyo_io_buffer".
759 * Returns true on success, false otherwise.
761 * Caller holds tomoyo_read_lock().
763 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
765 struct list_head *pos;
768 list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
769 struct tomoyo_alias_entry *ptr;
771 ptr = list_entry(pos, struct tomoyo_alias_entry, head.list);
772 if (ptr->head.is_deleted)
774 done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
775 ptr->original_name->name,
776 ptr->aliased_name->name);
784 * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
786 * @data: String to parse.
787 * @is_delete: True if it is a delete request.
789 * Returns 0 on success, negative value otherwise.
791 * Caller holds tomoyo_read_lock().
793 int tomoyo_write_alias_policy(char *data, const bool is_delete)
795 char *cp = strchr(data, ' ');
800 return tomoyo_update_alias_entry(data, cp, is_delete);
804 * tomoyo_find_or_assign_new_domain - Create a domain.
806 * @domainname: The name of domain.
807 * @profile: Profile number to assign if the domain was newly created.
809 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
811 * Caller holds tomoyo_read_lock().
813 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
817 struct tomoyo_domain_info *entry;
818 struct tomoyo_domain_info *domain = NULL;
819 const struct tomoyo_path_info *saved_domainname;
822 if (!tomoyo_is_correct_domain(domainname))
824 saved_domainname = tomoyo_get_name(domainname);
825 if (!saved_domainname)
827 entry = kzalloc(sizeof(*entry), GFP_NOFS);
828 if (mutex_lock_interruptible(&tomoyo_policy_lock))
830 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
831 if (domain->is_deleted ||
832 tomoyo_pathcmp(saved_domainname, domain->domainname))
837 if (!found && tomoyo_memory_ok(entry)) {
838 INIT_LIST_HEAD(&entry->acl_info_list);
839 entry->domainname = saved_domainname;
840 saved_domainname = NULL;
841 entry->profile = profile;
842 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
847 mutex_unlock(&tomoyo_policy_lock);
849 tomoyo_put_name(saved_domainname);
851 return found ? domain : NULL;
855 * tomoyo_find_next_domain - Find a domain.
857 * @bprm: Pointer to "struct linux_binprm".
859 * Returns 0 on success, negative value otherwise.
861 * Caller holds tomoyo_read_lock().
863 int tomoyo_find_next_domain(struct linux_binprm *bprm)
865 struct tomoyo_request_info r;
866 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
867 struct tomoyo_domain_info *old_domain = tomoyo_domain();
868 struct tomoyo_domain_info *domain = NULL;
869 const char *old_domain_name = old_domain->domainname->name;
870 const char *original_name = bprm->filename;
873 int retval = -ENOMEM;
874 bool need_kfree = false;
875 struct tomoyo_path_info rn = { }; /* real name */
876 struct tomoyo_path_info sn = { }; /* symlink name */
877 struct tomoyo_path_info ln; /* last name */
879 ln.name = tomoyo_get_last_name(old_domain);
880 tomoyo_fill_path_info(&ln);
881 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
882 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
891 /* Get tomoyo_realpath of program. */
893 rn.name = tomoyo_realpath(original_name);
896 tomoyo_fill_path_info(&rn);
899 /* Get tomoyo_realpath of symbolic link. */
900 sn.name = tomoyo_realpath_nofollow(original_name);
903 tomoyo_fill_path_info(&sn);
905 /* Check 'alias' directive. */
906 if (tomoyo_pathcmp(&rn, &sn)) {
907 struct tomoyo_alias_entry *ptr;
908 /* Is this program allowed to be called via symbolic links? */
909 list_for_each_entry_rcu(ptr, &tomoyo_alias_list, head.list) {
910 if (ptr->head.is_deleted ||
911 tomoyo_pathcmp(&rn, ptr->original_name) ||
912 tomoyo_pathcmp(&sn, ptr->aliased_name))
916 /* This is OK because it is read only. */
917 rn = *ptr->aliased_name;
922 /* Check 'aggregator' directive. */
924 struct tomoyo_aggregator_entry *ptr;
925 list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list,
927 if (ptr->head.is_deleted ||
928 !tomoyo_path_matches_pattern(&rn,
934 /* This is OK because it is read only. */
935 rn = *ptr->aggregated_name;
940 /* Check execute permission. */
941 retval = tomoyo_check_exec_perm(&r, &rn);
942 if (retval == TOMOYO_RETRY_REQUEST)
947 if (tomoyo_is_domain_initializer(old_domain->domainname, &rn, &ln)) {
948 /* Transit to the child of tomoyo_kernel_domain domain. */
949 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
950 TOMOYO_ROOT_NAME " " "%s", rn.name);
951 } else if (old_domain == &tomoyo_kernel_domain &&
952 !tomoyo_policy_loaded) {
954 * Needn't to transit from kernel domain before starting
955 * /sbin/init. But transit from kernel domain if executing
956 * initializers because they might start before /sbin/init.
959 } else if (tomoyo_is_domain_keeper(old_domain->domainname, &rn, &ln)) {
960 /* Keep current domain. */
963 /* Normal domain transition. */
964 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
965 "%s %s", old_domain_name, rn.name);
967 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
969 domain = tomoyo_find_domain(tmp);
973 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
975 if (error == TOMOYO_RETRY_REQUEST)
980 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
984 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
988 old_domain->transition_failed = true;
992 /* Update reference count on "struct tomoyo_domain_info". */
993 atomic_inc(&domain->users);
994 bprm->cred->security = domain;