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_domain_list is used for holding list of domains.
20 * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
21 * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
23 * An entry is added by
25 * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \
26 * /sys/kernel/security/tomoyo/domain_policy
30 * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \
31 * /sys/kernel/security/tomoyo/domain_policy
33 * and all entries are retrieved by
35 * # cat /sys/kernel/security/tomoyo/domain_policy
37 * A domain is added by
39 * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy
43 * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy
45 * and all domains are retrieved by
47 * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy
49 * Normally, a domainname is monotonically getting longer because a domainname
50 * which the process will belong to if an execve() operation succeeds is
51 * defined as a concatenation of "current domainname" + "pathname passed to
53 * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for
56 LIST_HEAD(tomoyo_domain_list
);
59 * tomoyo_get_last_name - Get last component of a domainname.
61 * @domain: Pointer to "struct tomoyo_domain_info".
63 * Returns the last component of the domainname.
65 const char *tomoyo_get_last_name(const struct tomoyo_domain_info
*domain
)
67 const char *cp0
= domain
->domainname
->name
;
68 const char *cp1
= strrchr(cp0
, ' ');
76 * tomoyo_domain_initializer_list is used for holding list of programs which
77 * triggers reinitialization of domainname. Normally, a domainname is
78 * monotonically getting longer. But sometimes, we restart daemon programs.
79 * It would be convenient for us that "a daemon started upon system boot" and
80 * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO
81 * provides a way to shorten domainnames.
83 * An entry is added by
85 * # echo 'initialize_domain /usr/sbin/httpd' > \
86 * /sys/kernel/security/tomoyo/exception_policy
90 * # echo 'delete initialize_domain /usr/sbin/httpd' > \
91 * /sys/kernel/security/tomoyo/exception_policy
93 * and all entries are retrieved by
95 * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy
97 * In the example above, /usr/sbin/httpd will belong to
98 * "<kernel> /usr/sbin/httpd" domain.
100 * You may specify a domainname using "from" keyword.
101 * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
102 * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd"
103 * domain to belong to "<kernel> /usr/sbin/httpd" domain.
105 * You may add "no_" prefix to "initialize_domain".
106 * "initialize_domain /usr/sbin/httpd" and
107 * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
108 * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain
109 * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain.
111 LIST_HEAD(tomoyo_domain_initializer_list
);
114 * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
116 * @domainname: The name of domain. May be NULL.
117 * @program: The name of program.
118 * @is_not: True if it is "no_initialize_domain" entry.
119 * @is_delete: True if it is a delete request.
121 * Returns 0 on success, negative value otherwise.
123 * Caller holds tomoyo_read_lock().
125 static int tomoyo_update_domain_initializer_entry(const char *domainname
,
128 const bool is_delete
)
130 struct tomoyo_domain_initializer_entry
*ptr
;
131 struct tomoyo_domain_initializer_entry e
= { .is_not
= is_not
};
132 int error
= is_delete
? -ENOENT
: -ENOMEM
;
134 if (!tomoyo_is_correct_path(program
))
137 if (!tomoyo_is_domain_def(domainname
) &&
138 tomoyo_is_correct_path(domainname
))
139 e
.is_last_name
= true;
140 else if (!tomoyo_is_correct_domain(domainname
))
142 e
.domainname
= tomoyo_get_name(domainname
);
146 e
.program
= tomoyo_get_name(program
);
149 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
151 list_for_each_entry_rcu(ptr
, &tomoyo_domain_initializer_list
, list
) {
152 if (!tomoyo_is_same_domain_initializer_entry(ptr
, &e
))
154 ptr
->is_deleted
= is_delete
;
158 if (!is_delete
&& error
) {
159 struct tomoyo_domain_initializer_entry
*entry
=
160 tomoyo_commit_ok(&e
, sizeof(e
));
162 list_add_tail_rcu(&entry
->list
,
163 &tomoyo_domain_initializer_list
);
167 mutex_unlock(&tomoyo_policy_lock
);
169 tomoyo_put_name(e
.domainname
);
170 tomoyo_put_name(e
.program
);
175 * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
177 * @head: Pointer to "struct tomoyo_io_buffer".
179 * Returns true on success, false otherwise.
181 * Caller holds tomoyo_read_lock().
183 bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer
*head
)
185 struct list_head
*pos
;
188 list_for_each_cookie(pos
, head
->read_var2
,
189 &tomoyo_domain_initializer_list
) {
191 const char *from
= "";
192 const char *domain
= "";
193 struct tomoyo_domain_initializer_entry
*ptr
;
194 ptr
= list_entry(pos
, struct tomoyo_domain_initializer_entry
,
198 no
= ptr
->is_not
? "no_" : "";
199 if (ptr
->domainname
) {
201 domain
= ptr
->domainname
->name
;
203 done
= tomoyo_io_printf(head
,
204 "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
205 "%s%s%s\n", no
, ptr
->program
->name
,
214 * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
216 * @data: String to parse.
217 * @is_not: True if it is "no_initialize_domain" entry.
218 * @is_delete: True if it is a delete request.
220 * Returns 0 on success, negative value otherwise.
222 * Caller holds tomoyo_read_lock().
224 int tomoyo_write_domain_initializer_policy(char *data
, const bool is_not
,
225 const bool is_delete
)
227 char *cp
= strstr(data
, " from ");
231 return tomoyo_update_domain_initializer_entry(cp
+ 6, data
,
235 return tomoyo_update_domain_initializer_entry(NULL
, data
, is_not
,
240 * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
242 * @domainname: The name of domain.
243 * @program: The name of program.
244 * @last_name: The last component of @domainname.
246 * Returns true if executing @program reinitializes domain transition,
249 * Caller holds tomoyo_read_lock().
251 static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info
*
253 const struct tomoyo_path_info
*program
,
254 const struct tomoyo_path_info
*
257 struct tomoyo_domain_initializer_entry
*ptr
;
260 list_for_each_entry_rcu(ptr
, &tomoyo_domain_initializer_list
, list
) {
263 if (ptr
->domainname
) {
264 if (!ptr
->is_last_name
) {
265 if (ptr
->domainname
!= domainname
)
268 if (tomoyo_pathcmp(ptr
->domainname
, last_name
))
272 if (tomoyo_pathcmp(ptr
->program
, program
))
284 * tomoyo_domain_keeper_list is used for holding list of domainnames which
285 * suppresses domain transition. Normally, a domainname is monotonically
286 * getting longer. But sometimes, we want to suppress domain transition.
287 * It would be convenient for us that programs executed from a login session
288 * belong to the same domain. Thus, TOMOYO provides a way to suppress domain
291 * An entry is added by
293 * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
294 * /sys/kernel/security/tomoyo/exception_policy
298 * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
299 * /sys/kernel/security/tomoyo/exception_policy
301 * and all entries are retrieved by
303 * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy
305 * In the example above, any process which belongs to
306 * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain,
307 * unless explicitly specified by "initialize_domain" or "no_keep_domain".
309 * You may specify a program using "from" keyword.
310 * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash"
311 * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash"
312 * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain.
314 * You may add "no_" prefix to "keep_domain".
315 * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and
316 * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will
317 * cause "/usr/bin/passwd" to belong to
318 * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless
319 * explicitly specified by "initialize_domain".
321 LIST_HEAD(tomoyo_domain_keeper_list
);
324 * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
326 * @domainname: The name of domain.
327 * @program: The name of program. May be NULL.
328 * @is_not: True if it is "no_keep_domain" entry.
329 * @is_delete: True if it is a delete request.
331 * Returns 0 on success, negative value otherwise.
333 * Caller holds tomoyo_read_lock().
335 static int tomoyo_update_domain_keeper_entry(const char *domainname
,
338 const bool is_delete
)
340 struct tomoyo_domain_keeper_entry
*ptr
;
341 struct tomoyo_domain_keeper_entry e
= { .is_not
= is_not
};
342 int error
= is_delete
? -ENOENT
: -ENOMEM
;
344 if (!tomoyo_is_domain_def(domainname
) &&
345 tomoyo_is_correct_path(domainname
))
346 e
.is_last_name
= true;
347 else if (!tomoyo_is_correct_domain(domainname
))
350 if (!tomoyo_is_correct_path(program
))
352 e
.program
= tomoyo_get_name(program
);
356 e
.domainname
= tomoyo_get_name(domainname
);
359 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
361 list_for_each_entry_rcu(ptr
, &tomoyo_domain_keeper_list
, list
) {
362 if (!tomoyo_is_same_domain_keeper_entry(ptr
, &e
))
364 ptr
->is_deleted
= is_delete
;
368 if (!is_delete
&& error
) {
369 struct tomoyo_domain_keeper_entry
*entry
=
370 tomoyo_commit_ok(&e
, sizeof(e
));
372 list_add_tail_rcu(&entry
->list
,
373 &tomoyo_domain_keeper_list
);
377 mutex_unlock(&tomoyo_policy_lock
);
379 tomoyo_put_name(e
.domainname
);
380 tomoyo_put_name(e
.program
);
385 * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
387 * @data: String to parse.
388 * @is_not: True if it is "no_keep_domain" entry.
389 * @is_delete: True if it is a delete request.
391 * Caller holds tomoyo_read_lock().
393 int tomoyo_write_domain_keeper_policy(char *data
, const bool is_not
,
394 const bool is_delete
)
396 char *cp
= strstr(data
, " from ");
400 return tomoyo_update_domain_keeper_entry(cp
+ 6, data
, is_not
,
403 return tomoyo_update_domain_keeper_entry(data
, NULL
, is_not
, is_delete
);
407 * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
409 * @head: Pointer to "struct tomoyo_io_buffer".
411 * Returns true on success, false otherwise.
413 * Caller holds tomoyo_read_lock().
415 bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer
*head
)
417 struct list_head
*pos
;
420 list_for_each_cookie(pos
, head
->read_var2
,
421 &tomoyo_domain_keeper_list
) {
422 struct tomoyo_domain_keeper_entry
*ptr
;
424 const char *from
= "";
425 const char *program
= "";
427 ptr
= list_entry(pos
, struct tomoyo_domain_keeper_entry
, list
);
430 no
= ptr
->is_not
? "no_" : "";
433 program
= ptr
->program
->name
;
435 done
= tomoyo_io_printf(head
,
436 "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
437 "%s%s%s\n", no
, program
, from
,
438 ptr
->domainname
->name
);
446 * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
448 * @domainname: The name of domain.
449 * @program: The name of program.
450 * @last_name: The last component of @domainname.
452 * Returns true if executing @program supresses domain transition,
455 * Caller holds tomoyo_read_lock().
457 static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info
*domainname
,
458 const struct tomoyo_path_info
*program
,
459 const struct tomoyo_path_info
*last_name
)
461 struct tomoyo_domain_keeper_entry
*ptr
;
464 list_for_each_entry_rcu(ptr
, &tomoyo_domain_keeper_list
, list
) {
467 if (!ptr
->is_last_name
) {
468 if (ptr
->domainname
!= domainname
)
471 if (tomoyo_pathcmp(ptr
->domainname
, last_name
))
474 if (ptr
->program
&& tomoyo_pathcmp(ptr
->program
, program
))
486 * tomoyo_aggregator_list is used for holding list of rewrite table for
487 * execve() request. Some programs provides similar functionality. This keyword
488 * allows users to aggregate such programs.
490 * Entries are added by
492 * # echo 'aggregator /usr/bin/vi /./editor' > \
493 * /sys/kernel/security/tomoyo/exception_policy
494 * # echo 'aggregator /usr/bin/emacs /./editor' > \
495 * /sys/kernel/security/tomoyo/exception_policy
499 * # echo 'delete aggregator /usr/bin/vi /./editor' > \
500 * /sys/kernel/security/tomoyo/exception_policy
501 * # echo 'delete aggregator /usr/bin/emacs /./editor' > \
502 * /sys/kernel/security/tomoyo/exception_policy
504 * and all entries are retrieved by
506 * # grep ^aggregator /sys/kernel/security/tomoyo/exception_policy
508 * In the example above, if /usr/bin/vi or /usr/bin/emacs are executed,
509 * permission is checked for /./editor and domainname which the current process
510 * will belong to after execve() succeeds is calculated using /./editor .
512 LIST_HEAD(tomoyo_aggregator_list
);
515 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
517 * @original_name: The original program's name.
518 * @aggregated_name: The program name to use.
519 * @is_delete: True if it is a delete request.
521 * Returns 0 on success, negative value otherwise.
523 * Caller holds tomoyo_read_lock().
525 static int tomoyo_update_aggregator_entry(const char *original_name
,
526 const char *aggregated_name
,
527 const bool is_delete
)
529 struct tomoyo_aggregator_entry
*ptr
;
530 struct tomoyo_aggregator_entry e
= { };
531 int error
= is_delete
? -ENOENT
: -ENOMEM
;
533 if (!tomoyo_is_correct_path(original_name
) ||
534 !tomoyo_is_correct_path(aggregated_name
))
536 e
.original_name
= tomoyo_get_name(original_name
);
537 e
.aggregated_name
= tomoyo_get_name(aggregated_name
);
538 if (!e
.original_name
|| !e
.aggregated_name
||
539 e
.aggregated_name
->is_patterned
) /* No patterns allowed. */
541 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
543 list_for_each_entry_rcu(ptr
, &tomoyo_aggregator_list
, list
) {
544 if (!tomoyo_is_same_aggregator_entry(ptr
, &e
))
546 ptr
->is_deleted
= is_delete
;
550 if (!is_delete
&& error
) {
551 struct tomoyo_aggregator_entry
*entry
=
552 tomoyo_commit_ok(&e
, sizeof(e
));
554 list_add_tail_rcu(&entry
->list
,
555 &tomoyo_aggregator_list
);
559 mutex_unlock(&tomoyo_policy_lock
);
561 tomoyo_put_name(e
.original_name
);
562 tomoyo_put_name(e
.aggregated_name
);
567 * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
569 * @head: Pointer to "struct tomoyo_io_buffer".
571 * Returns true on success, false otherwise.
573 * Caller holds tomoyo_read_lock().
575 bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer
*head
)
577 struct list_head
*pos
;
580 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_aggregator_list
) {
581 struct tomoyo_aggregator_entry
*ptr
;
583 ptr
= list_entry(pos
, struct tomoyo_aggregator_entry
, list
);
586 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_AGGREGATOR
587 "%s %s\n", ptr
->original_name
->name
,
588 ptr
->aggregated_name
->name
);
596 * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
598 * @data: String to parse.
599 * @is_delete: True if it is a delete request.
601 * Returns 0 on success, negative value otherwise.
603 * Caller holds tomoyo_read_lock().
605 int tomoyo_write_aggregator_policy(char *data
, const bool is_delete
)
607 char *cp
= strchr(data
, ' ');
612 return tomoyo_update_aggregator_entry(data
, cp
, is_delete
);
616 * tomoyo_alias_list is used for holding list of symlink's pathnames which are
617 * allowed to be passed to an execve() request. Normally, the domainname which
618 * the current process will belong to after execve() succeeds is calculated
619 * using dereferenced pathnames. But some programs behave differently depending
620 * on the name passed to argv[0]. For busybox, calculating domainname using
621 * dereferenced pathnames will cause all programs in the busybox to belong to
622 * the same domain. Thus, TOMOYO provides a way to allow use of symlink's
623 * pathname for checking execve()'s permission and calculating domainname which
624 * the current process will belong to after execve() succeeds.
626 * An entry is added by
628 * # echo 'alias /bin/busybox /bin/cat' > \
629 * /sys/kernel/security/tomoyo/exception_policy
633 * # echo 'delete alias /bin/busybox /bin/cat' > \
634 * /sys/kernel/security/tomoyo/exception_policy
636 * and all entries are retrieved by
638 * # grep ^alias /sys/kernel/security/tomoyo/exception_policy
640 * In the example above, if /bin/cat is a symlink to /bin/busybox and execution
641 * of /bin/cat is requested, permission is checked for /bin/cat rather than
642 * /bin/busybox and domainname which the current process will belong to after
643 * execve() succeeds is calculated using /bin/cat rather than /bin/busybox .
645 LIST_HEAD(tomoyo_alias_list
);
648 * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
650 * @original_name: The original program's real name.
651 * @aliased_name: The symbolic program's symbolic link's name.
652 * @is_delete: True if it is a delete request.
654 * Returns 0 on success, negative value otherwise.
656 * Caller holds tomoyo_read_lock().
658 static int tomoyo_update_alias_entry(const char *original_name
,
659 const char *aliased_name
,
660 const bool is_delete
)
662 struct tomoyo_alias_entry
*ptr
;
663 struct tomoyo_alias_entry e
= { };
664 int error
= is_delete
? -ENOENT
: -ENOMEM
;
666 if (!tomoyo_is_correct_path(original_name
) ||
667 !tomoyo_is_correct_path(aliased_name
))
669 e
.original_name
= tomoyo_get_name(original_name
);
670 e
.aliased_name
= tomoyo_get_name(aliased_name
);
671 if (!e
.original_name
|| !e
.aliased_name
||
672 e
.original_name
->is_patterned
|| e
.aliased_name
->is_patterned
)
673 goto out
; /* No patterns allowed. */
674 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
676 list_for_each_entry_rcu(ptr
, &tomoyo_alias_list
, list
) {
677 if (!tomoyo_is_same_alias_entry(ptr
, &e
))
679 ptr
->is_deleted
= is_delete
;
683 if (!is_delete
&& error
) {
684 struct tomoyo_alias_entry
*entry
=
685 tomoyo_commit_ok(&e
, sizeof(e
));
687 list_add_tail_rcu(&entry
->list
, &tomoyo_alias_list
);
691 mutex_unlock(&tomoyo_policy_lock
);
693 tomoyo_put_name(e
.original_name
);
694 tomoyo_put_name(e
.aliased_name
);
699 * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
701 * @head: Pointer to "struct tomoyo_io_buffer".
703 * Returns true on success, false otherwise.
705 * Caller holds tomoyo_read_lock().
707 bool tomoyo_read_alias_policy(struct tomoyo_io_buffer
*head
)
709 struct list_head
*pos
;
712 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_alias_list
) {
713 struct tomoyo_alias_entry
*ptr
;
715 ptr
= list_entry(pos
, struct tomoyo_alias_entry
, list
);
718 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_ALIAS
"%s %s\n",
719 ptr
->original_name
->name
,
720 ptr
->aliased_name
->name
);
728 * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
730 * @data: String to parse.
731 * @is_delete: True if it is a delete request.
733 * Returns 0 on success, negative value otherwise.
735 * Caller holds tomoyo_read_lock().
737 int tomoyo_write_alias_policy(char *data
, const bool is_delete
)
739 char *cp
= strchr(data
, ' ');
744 return tomoyo_update_alias_entry(data
, cp
, is_delete
);
748 * tomoyo_find_or_assign_new_domain - Create a domain.
750 * @domainname: The name of domain.
751 * @profile: Profile number to assign if the domain was newly created.
753 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
755 * Caller holds tomoyo_read_lock().
757 struct tomoyo_domain_info
*tomoyo_find_or_assign_new_domain(const char *
761 struct tomoyo_domain_info
*entry
;
762 struct tomoyo_domain_info
*domain
= NULL
;
763 const struct tomoyo_path_info
*saved_domainname
;
766 if (!tomoyo_is_correct_domain(domainname
))
768 saved_domainname
= tomoyo_get_name(domainname
);
769 if (!saved_domainname
)
771 entry
= kzalloc(sizeof(*entry
), GFP_NOFS
);
772 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
774 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
775 if (domain
->is_deleted
||
776 tomoyo_pathcmp(saved_domainname
, domain
->domainname
))
781 if (!found
&& tomoyo_memory_ok(entry
)) {
782 INIT_LIST_HEAD(&entry
->acl_info_list
);
783 entry
->domainname
= saved_domainname
;
784 saved_domainname
= NULL
;
785 entry
->profile
= profile
;
786 list_add_tail_rcu(&entry
->list
, &tomoyo_domain_list
);
791 mutex_unlock(&tomoyo_policy_lock
);
793 tomoyo_put_name(saved_domainname
);
795 return found
? domain
: NULL
;
799 * tomoyo_find_next_domain - Find a domain.
801 * @bprm: Pointer to "struct linux_binprm".
803 * Returns 0 on success, negative value otherwise.
805 * Caller holds tomoyo_read_lock().
807 int tomoyo_find_next_domain(struct linux_binprm
*bprm
)
809 struct tomoyo_request_info r
;
810 char *tmp
= kzalloc(TOMOYO_EXEC_TMPSIZE
, GFP_NOFS
);
811 struct tomoyo_domain_info
*old_domain
= tomoyo_domain();
812 struct tomoyo_domain_info
*domain
= NULL
;
813 const char *old_domain_name
= old_domain
->domainname
->name
;
814 const char *original_name
= bprm
->filename
;
815 const u8 mode
= tomoyo_check_flags(old_domain
, TOMOYO_MAC_FOR_FILE
);
816 const bool is_enforce
= (mode
== TOMOYO_CONFIG_ENFORCING
);
817 int retval
= -ENOMEM
;
818 bool need_kfree
= false;
819 struct tomoyo_path_info rn
= { }; /* real name */
820 struct tomoyo_path_info sn
= { }; /* symlink name */
821 struct tomoyo_path_info ln
; /* last name */
823 ln
.name
= tomoyo_get_last_name(old_domain
);
824 tomoyo_fill_path_info(&ln
);
825 tomoyo_init_request_info(&r
, NULL
);
834 /* Get tomoyo_realpath of program. */
836 rn
.name
= tomoyo_realpath(original_name
);
839 tomoyo_fill_path_info(&rn
);
842 /* Get tomoyo_realpath of symbolic link. */
843 sn
.name
= tomoyo_realpath_nofollow(original_name
);
846 tomoyo_fill_path_info(&sn
);
848 /* Check 'alias' directive. */
849 if (tomoyo_pathcmp(&rn
, &sn
)) {
850 struct tomoyo_alias_entry
*ptr
;
851 /* Is this program allowed to be called via symbolic links? */
852 list_for_each_entry_rcu(ptr
, &tomoyo_alias_list
, list
) {
853 if (ptr
->is_deleted
||
854 tomoyo_pathcmp(&rn
, ptr
->original_name
) ||
855 tomoyo_pathcmp(&sn
, ptr
->aliased_name
))
859 /* This is OK because it is read only. */
860 rn
= *ptr
->aliased_name
;
865 /* Check 'aggregator' directive. */
867 struct tomoyo_aggregator_entry
*ptr
;
868 list_for_each_entry_rcu(ptr
, &tomoyo_aggregator_list
, list
) {
869 if (ptr
->is_deleted
||
870 !tomoyo_path_matches_pattern(&rn
,
876 /* This is OK because it is read only. */
877 rn
= *ptr
->aggregated_name
;
882 /* Check execute permission. */
883 retval
= tomoyo_check_exec_perm(old_domain
, &rn
);
884 if (retval
== TOMOYO_RETRY_REQUEST
)
889 if (tomoyo_is_domain_initializer(old_domain
->domainname
, &rn
, &ln
)) {
890 /* Transit to the child of tomoyo_kernel_domain domain. */
891 snprintf(tmp
, TOMOYO_EXEC_TMPSIZE
- 1,
892 TOMOYO_ROOT_NAME
" " "%s", rn
.name
);
893 } else if (old_domain
== &tomoyo_kernel_domain
&&
894 !tomoyo_policy_loaded
) {
896 * Needn't to transit from kernel domain before starting
897 * /sbin/init. But transit from kernel domain if executing
898 * initializers because they might start before /sbin/init.
901 } else if (tomoyo_is_domain_keeper(old_domain
->domainname
, &rn
, &ln
)) {
902 /* Keep current domain. */
905 /* Normal domain transition. */
906 snprintf(tmp
, TOMOYO_EXEC_TMPSIZE
- 1,
907 "%s %s", old_domain_name
, rn
.name
);
909 if (domain
|| strlen(tmp
) >= TOMOYO_EXEC_TMPSIZE
- 10)
911 domain
= tomoyo_find_domain(tmp
);
915 int error
= tomoyo_supervisor(&r
, "# wants to create domain\n"
917 if (error
== TOMOYO_RETRY_REQUEST
)
922 domain
= tomoyo_find_or_assign_new_domain(tmp
, old_domain
->profile
);
926 printk(KERN_WARNING
"TOMOYO-ERROR: Domain '%s' not defined.\n", tmp
);
930 old_domain
->transition_failed
= true;
934 /* Update reference count on "struct tomoyo_domain_info". */
935 atomic_inc(&domain
->users
);
936 bprm
->cred
->security
= domain
;