1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/domain.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
10 #include <linux/binfmts.h>
11 #include <linux/slab.h>
12 #include <linux/rculist.h>
14 /* Variables definitions.*/
16 /* The initial domain. */
17 struct tomoyo_domain_info tomoyo_kernel_domain
;
20 * tomoyo_update_policy - Update an entry for exception policy.
22 * @new_entry: Pointer to "struct tomoyo_acl_info".
23 * @size: Size of @new_entry in bytes.
24 * @param: Pointer to "struct tomoyo_acl_param".
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 struct tomoyo_acl_param
*param
,
33 bool (*check_duplicate
)(const struct tomoyo_acl_head
35 const struct tomoyo_acl_head
38 int error
= param
->is_delete
? -ENOENT
: -ENOMEM
;
39 struct tomoyo_acl_head
*entry
;
40 struct list_head
*list
= param
->list
;
42 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
44 list_for_each_entry_rcu(entry
, list
, list
,
45 srcu_read_lock_held(&tomoyo_ss
)) {
46 if (entry
->is_deleted
== TOMOYO_GC_IN_PROGRESS
)
48 if (!check_duplicate(entry
, new_entry
))
50 entry
->is_deleted
= param
->is_delete
;
54 if (error
&& !param
->is_delete
) {
55 entry
= tomoyo_commit_ok(new_entry
, size
);
57 list_add_tail_rcu(&entry
->list
, list
);
61 mutex_unlock(&tomoyo_policy_lock
);
66 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
68 * @a: Pointer to "struct tomoyo_acl_info".
69 * @b: Pointer to "struct tomoyo_acl_info".
71 * Returns true if @a == @b, false otherwise.
73 static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info
*a
,
74 const struct tomoyo_acl_info
*b
)
76 return a
->type
== b
->type
&& a
->cond
== b
->cond
;
80 * tomoyo_update_domain - Update an entry for domain policy.
82 * @new_entry: Pointer to "struct tomoyo_acl_info".
83 * @size: Size of @new_entry in bytes.
84 * @param: Pointer to "struct tomoyo_acl_param".
85 * @check_duplicate: Callback function to find duplicated entry.
86 * @merge_duplicate: Callback function to merge duplicated entry.
88 * Returns 0 on success, negative value otherwise.
90 * Caller holds tomoyo_read_lock().
92 int tomoyo_update_domain(struct tomoyo_acl_info
*new_entry
, const int size
,
93 struct tomoyo_acl_param
*param
,
94 bool (*check_duplicate
)(const struct tomoyo_acl_info
96 const struct tomoyo_acl_info
98 bool (*merge_duplicate
)(struct tomoyo_acl_info
*,
99 struct tomoyo_acl_info
*,
102 const bool is_delete
= param
->is_delete
;
103 int error
= is_delete
? -ENOENT
: -ENOMEM
;
104 struct tomoyo_acl_info
*entry
;
105 struct list_head
* const list
= param
->list
;
107 if (param
->data
[0]) {
108 new_entry
->cond
= tomoyo_get_condition(param
);
109 if (!new_entry
->cond
)
112 * Domain transition preference is allowed for only
113 * "file execute" entries.
115 if (new_entry
->cond
->transit
&&
116 !(new_entry
->type
== TOMOYO_TYPE_PATH_ACL
&&
117 container_of(new_entry
, struct tomoyo_path_acl
, head
)
118 ->perm
== 1 << TOMOYO_TYPE_EXECUTE
))
121 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
123 list_for_each_entry_rcu(entry
, list
, list
,
124 srcu_read_lock_held(&tomoyo_ss
)) {
125 if (entry
->is_deleted
== TOMOYO_GC_IN_PROGRESS
)
127 if (!tomoyo_same_acl_head(entry
, new_entry
) ||
128 !check_duplicate(entry
, new_entry
))
131 entry
->is_deleted
= merge_duplicate(entry
, new_entry
,
134 entry
->is_deleted
= is_delete
;
138 if (error
&& !is_delete
) {
139 entry
= tomoyo_commit_ok(new_entry
, size
);
141 list_add_tail_rcu(&entry
->list
, list
);
145 mutex_unlock(&tomoyo_policy_lock
);
147 tomoyo_put_condition(new_entry
->cond
);
152 * tomoyo_check_acl - Do permission check.
154 * @r: Pointer to "struct tomoyo_request_info".
155 * @check_entry: Callback function to check type specific parameters.
157 * Returns 0 on success, negative value otherwise.
159 * Caller holds tomoyo_read_lock().
161 void tomoyo_check_acl(struct tomoyo_request_info
*r
,
162 bool (*check_entry
)(struct tomoyo_request_info
*,
163 const struct tomoyo_acl_info
*))
165 const struct tomoyo_domain_info
*domain
= r
->domain
;
166 struct tomoyo_acl_info
*ptr
;
167 const struct list_head
*list
= &domain
->acl_info_list
;
171 list_for_each_entry_rcu(ptr
, list
, list
,
172 srcu_read_lock_held(&tomoyo_ss
)) {
173 if (ptr
->is_deleted
|| ptr
->type
!= r
->param_type
)
175 if (!check_entry(r
, ptr
))
177 if (!tomoyo_condition(r
, ptr
->cond
))
179 r
->matched_acl
= ptr
;
183 for (; i
< TOMOYO_MAX_ACL_GROUPS
; i
++) {
184 if (!test_bit(i
, domain
->group
))
186 list
= &domain
->ns
->acl_group
[i
++];
192 /* The list for "struct tomoyo_domain_info". */
193 LIST_HEAD(tomoyo_domain_list
);
196 * tomoyo_last_word - Get last component of a domainname.
198 * @name: Domainname to check.
200 * Returns the last word of @domainname.
202 static const char *tomoyo_last_word(const char *name
)
204 const char *cp
= strrchr(name
, ' ');
212 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
214 * @a: Pointer to "struct tomoyo_acl_head".
215 * @b: Pointer to "struct tomoyo_acl_head".
217 * Returns true if @a == @b, false otherwise.
219 static bool tomoyo_same_transition_control(const struct tomoyo_acl_head
*a
,
220 const struct tomoyo_acl_head
*b
)
222 const struct tomoyo_transition_control
*p1
= container_of(a
,
225 const struct tomoyo_transition_control
*p2
= container_of(b
,
229 return p1
->type
== p2
->type
&& p1
->is_last_name
== p2
->is_last_name
230 && p1
->domainname
== p2
->domainname
231 && p1
->program
== p2
->program
;
235 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
237 * @param: Pointer to "struct tomoyo_acl_param".
238 * @type: Type of this entry.
240 * Returns 0 on success, negative value otherwise.
242 int tomoyo_write_transition_control(struct tomoyo_acl_param
*param
,
245 struct tomoyo_transition_control e
= { .type
= type
};
246 int error
= param
->is_delete
? -ENOENT
: -ENOMEM
;
247 char *program
= param
->data
;
248 char *domainname
= strstr(program
, " from ");
253 } else if (type
== TOMOYO_TRANSITION_CONTROL_NO_KEEP
||
254 type
== TOMOYO_TRANSITION_CONTROL_KEEP
) {
255 domainname
= program
;
258 if (program
&& strcmp(program
, "any")) {
259 if (!tomoyo_correct_path(program
))
261 e
.program
= tomoyo_get_name(program
);
265 if (domainname
&& strcmp(domainname
, "any")) {
266 if (!tomoyo_correct_domain(domainname
)) {
267 if (!tomoyo_correct_path(domainname
))
269 e
.is_last_name
= true;
271 e
.domainname
= tomoyo_get_name(domainname
);
275 param
->list
= ¶m
->ns
->policy_list
[TOMOYO_ID_TRANSITION_CONTROL
];
276 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
277 tomoyo_same_transition_control
);
279 tomoyo_put_name(e
.domainname
);
280 tomoyo_put_name(e
.program
);
285 * tomoyo_scan_transition - Try to find specific domain transition type.
287 * @list: Pointer to "struct list_head".
288 * @domainname: The name of current domain.
289 * @program: The name of requested program.
290 * @last_name: The last component of @domainname.
291 * @type: One of values in "enum tomoyo_transition_type".
293 * Returns true if found one, false otherwise.
295 * Caller holds tomoyo_read_lock().
297 static inline bool tomoyo_scan_transition
298 (const struct list_head
*list
, const struct tomoyo_path_info
*domainname
,
299 const struct tomoyo_path_info
*program
, const char *last_name
,
300 const enum tomoyo_transition_type type
)
302 const struct tomoyo_transition_control
*ptr
;
304 list_for_each_entry_rcu(ptr
, list
, head
.list
,
305 srcu_read_lock_held(&tomoyo_ss
)) {
306 if (ptr
->head
.is_deleted
|| ptr
->type
!= type
)
308 if (ptr
->domainname
) {
309 if (!ptr
->is_last_name
) {
310 if (ptr
->domainname
!= domainname
)
314 * Use direct strcmp() since this is
317 if (strcmp(ptr
->domainname
->name
, last_name
))
321 if (ptr
->program
&& tomoyo_pathcmp(ptr
->program
, program
))
329 * tomoyo_transition_type - Get domain transition type.
331 * @ns: Pointer to "struct tomoyo_policy_namespace".
332 * @domainname: The name of current domain.
333 * @program: The name of requested program.
335 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
336 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
337 * executing @program reinitializes domain transition within that namespace,
338 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
341 * Caller holds tomoyo_read_lock().
343 static enum tomoyo_transition_type tomoyo_transition_type
344 (const struct tomoyo_policy_namespace
*ns
,
345 const struct tomoyo_path_info
*domainname
,
346 const struct tomoyo_path_info
*program
)
348 const char *last_name
= tomoyo_last_word(domainname
->name
);
349 enum tomoyo_transition_type type
= TOMOYO_TRANSITION_CONTROL_NO_RESET
;
351 while (type
< TOMOYO_MAX_TRANSITION_TYPE
) {
352 const struct list_head
* const list
=
353 &ns
->policy_list
[TOMOYO_ID_TRANSITION_CONTROL
];
355 if (!tomoyo_scan_transition(list
, domainname
, program
,
360 if (type
!= TOMOYO_TRANSITION_CONTROL_NO_RESET
&&
361 type
!= TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE
)
364 * Do not check for reset_domain if no_reset_domain matched.
365 * Do not check for initialize_domain if no_initialize_domain
375 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
377 * @a: Pointer to "struct tomoyo_acl_head".
378 * @b: Pointer to "struct tomoyo_acl_head".
380 * Returns true if @a == @b, false otherwise.
382 static bool tomoyo_same_aggregator(const struct tomoyo_acl_head
*a
,
383 const struct tomoyo_acl_head
*b
)
385 const struct tomoyo_aggregator
*p1
= container_of(a
, typeof(*p1
),
387 const struct tomoyo_aggregator
*p2
= container_of(b
, typeof(*p2
),
390 return p1
->original_name
== p2
->original_name
&&
391 p1
->aggregated_name
== p2
->aggregated_name
;
395 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
397 * @param: Pointer to "struct tomoyo_acl_param".
399 * Returns 0 on success, negative value otherwise.
401 * Caller holds tomoyo_read_lock().
403 int tomoyo_write_aggregator(struct tomoyo_acl_param
*param
)
405 struct tomoyo_aggregator e
= { };
406 int error
= param
->is_delete
? -ENOENT
: -ENOMEM
;
407 const char *original_name
= tomoyo_read_token(param
);
408 const char *aggregated_name
= tomoyo_read_token(param
);
410 if (!tomoyo_correct_word(original_name
) ||
411 !tomoyo_correct_path(aggregated_name
))
413 e
.original_name
= tomoyo_get_name(original_name
);
414 e
.aggregated_name
= tomoyo_get_name(aggregated_name
);
415 if (!e
.original_name
|| !e
.aggregated_name
||
416 e
.aggregated_name
->is_patterned
) /* No patterns allowed. */
418 param
->list
= ¶m
->ns
->policy_list
[TOMOYO_ID_AGGREGATOR
];
419 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
420 tomoyo_same_aggregator
);
422 tomoyo_put_name(e
.original_name
);
423 tomoyo_put_name(e
.aggregated_name
);
428 * tomoyo_find_namespace - Find specified namespace.
430 * @name: Name of namespace to find.
431 * @len: Length of @name.
433 * Returns pointer to "struct tomoyo_policy_namespace" if found,
436 * Caller holds tomoyo_read_lock().
438 static struct tomoyo_policy_namespace
*tomoyo_find_namespace
439 (const char *name
, const unsigned int len
)
441 struct tomoyo_policy_namespace
*ns
;
443 list_for_each_entry(ns
, &tomoyo_namespace_list
, namespace_list
) {
444 if (strncmp(name
, ns
->name
, len
) ||
445 (name
[len
] && name
[len
] != ' '))
453 * tomoyo_assign_namespace - Create a new namespace.
455 * @domainname: Name of namespace to create.
457 * Returns pointer to "struct tomoyo_policy_namespace" on success,
460 * Caller holds tomoyo_read_lock().
462 struct tomoyo_policy_namespace
*tomoyo_assign_namespace(const char *domainname
)
464 struct tomoyo_policy_namespace
*ptr
;
465 struct tomoyo_policy_namespace
*entry
;
466 const char *cp
= domainname
;
467 unsigned int len
= 0;
469 while (*cp
&& *cp
++ != ' ')
471 ptr
= tomoyo_find_namespace(domainname
, len
);
474 if (len
>= TOMOYO_EXEC_TMPSIZE
- 10 || !tomoyo_domain_def(domainname
))
476 entry
= kzalloc(sizeof(*entry
) + len
+ 1, GFP_NOFS
| __GFP_NOWARN
);
477 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
479 ptr
= tomoyo_find_namespace(domainname
, len
);
480 if (!ptr
&& tomoyo_memory_ok(entry
)) {
481 char *name
= (char *) (entry
+ 1);
484 memmove(name
, domainname
, len
);
487 tomoyo_init_policy_namespace(entry
);
490 mutex_unlock(&tomoyo_policy_lock
);
497 * tomoyo_namespace_jump - Check for namespace jump.
499 * @domainname: Name of domain.
501 * Returns true if namespace differs, false otherwise.
503 static bool tomoyo_namespace_jump(const char *domainname
)
505 const char *namespace = tomoyo_current_namespace()->name
;
506 const int len
= strlen(namespace);
508 return strncmp(domainname
, namespace, len
) ||
509 (domainname
[len
] && domainname
[len
] != ' ');
513 * tomoyo_assign_domain - Create a domain or a namespace.
515 * @domainname: The name of domain.
516 * @transit: True if transit to domain found or created.
518 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
520 * Caller holds tomoyo_read_lock().
522 struct tomoyo_domain_info
*tomoyo_assign_domain(const char *domainname
,
525 struct tomoyo_domain_info e
= { };
526 struct tomoyo_domain_info
*entry
= tomoyo_find_domain(domainname
);
527 bool created
= false;
532 * Since namespace is created at runtime, profiles may
533 * not be created by the moment the process transits to
534 * that domain. Do not perform domain transition if
535 * profile for that domain is not yet created.
537 if (tomoyo_policy_loaded
&&
538 !entry
->ns
->profile_ptr
[entry
->profile
])
543 /* Requested domain does not exist. */
544 /* Don't create requested domain if domainname is invalid. */
545 if (strlen(domainname
) >= TOMOYO_EXEC_TMPSIZE
- 10 ||
546 !tomoyo_correct_domain(domainname
))
549 * Since definition of profiles and acl_groups may differ across
550 * namespaces, do not inherit "use_profile" and "use_group" settings
551 * by automatically creating requested domain upon domain transition.
553 if (transit
&& tomoyo_namespace_jump(domainname
))
555 e
.ns
= tomoyo_assign_namespace(domainname
);
559 * "use_profile" and "use_group" settings for automatically created
560 * domains are inherited from current domain. These are 0 for manually
564 const struct tomoyo_domain_info
*domain
= tomoyo_domain();
566 e
.profile
= domain
->profile
;
567 memcpy(e
.group
, domain
->group
, sizeof(e
.group
));
569 e
.domainname
= tomoyo_get_name(domainname
);
572 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
574 entry
= tomoyo_find_domain(domainname
);
576 entry
= tomoyo_commit_ok(&e
, sizeof(e
));
578 INIT_LIST_HEAD(&entry
->acl_info_list
);
579 list_add_tail_rcu(&entry
->list
, &tomoyo_domain_list
);
583 mutex_unlock(&tomoyo_policy_lock
);
585 tomoyo_put_name(e
.domainname
);
586 if (entry
&& transit
) {
588 struct tomoyo_request_info r
;
591 tomoyo_init_request_info(&r
, entry
,
592 TOMOYO_MAC_FILE_EXECUTE
);
594 tomoyo_write_log(&r
, "use_profile %u\n",
596 for (i
= 0; i
< TOMOYO_MAX_ACL_GROUPS
; i
++)
597 if (test_bit(i
, entry
->group
))
598 tomoyo_write_log(&r
, "use_group %u\n",
600 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES
);
607 * tomoyo_environ - Check permission for environment variable names.
609 * @ee: Pointer to "struct tomoyo_execve".
611 * Returns 0 on success, negative value otherwise.
613 static int tomoyo_environ(struct tomoyo_execve
*ee
)
615 struct tomoyo_request_info
*r
= &ee
->r
;
616 struct linux_binprm
*bprm
= ee
->bprm
;
617 /* env_page.data is allocated by tomoyo_dump_page(). */
618 struct tomoyo_page_dump env_page
= { };
619 char *arg_ptr
; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
621 unsigned long pos
= bprm
->p
;
622 int offset
= pos
% PAGE_SIZE
;
623 int argv_count
= bprm
->argc
;
624 int envp_count
= bprm
->envc
;
627 ee
->r
.type
= TOMOYO_MAC_ENVIRON
;
628 ee
->r
.profile
= r
->domain
->profile
;
629 ee
->r
.mode
= tomoyo_get_mode(r
->domain
->ns
, ee
->r
.profile
,
631 if (!r
->mode
|| !envp_count
)
633 arg_ptr
= kzalloc(TOMOYO_EXEC_TMPSIZE
, GFP_NOFS
);
636 while (error
== -ENOMEM
) {
637 if (!tomoyo_dump_page(bprm
, pos
, &env_page
))
639 pos
+= PAGE_SIZE
- offset
;
641 while (argv_count
&& offset
< PAGE_SIZE
) {
642 if (!env_page
.data
[offset
++])
649 while (offset
< PAGE_SIZE
) {
650 const unsigned char c
= env_page
.data
[offset
++];
652 if (c
&& arg_len
< TOMOYO_EXEC_TMPSIZE
- 10) {
654 arg_ptr
[arg_len
++] = '\0';
655 } else if (c
== '\\') {
656 arg_ptr
[arg_len
++] = '\\';
657 arg_ptr
[arg_len
++] = '\\';
658 } else if (c
> ' ' && c
< 127) {
659 arg_ptr
[arg_len
++] = c
;
661 arg_ptr
[arg_len
++] = '\\';
662 arg_ptr
[arg_len
++] = (c
>> 6) + '0';
664 = ((c
>> 3) & 7) + '0';
665 arg_ptr
[arg_len
++] = (c
& 7) + '0';
668 arg_ptr
[arg_len
] = '\0';
672 if (tomoyo_env_perm(r
, arg_ptr
)) {
685 if (r
->mode
!= TOMOYO_CONFIG_ENFORCING
)
687 kfree(env_page
.data
);
693 * tomoyo_find_next_domain - Find a domain.
695 * @bprm: Pointer to "struct linux_binprm".
697 * Returns 0 on success, negative value otherwise.
699 * Caller holds tomoyo_read_lock().
701 int tomoyo_find_next_domain(struct linux_binprm
*bprm
)
703 struct tomoyo_domain_info
*old_domain
= tomoyo_domain();
704 struct tomoyo_domain_info
*domain
= NULL
;
705 const char *original_name
= bprm
->filename
;
706 int retval
= -ENOMEM
;
707 bool reject_on_transition_failure
= false;
708 const struct tomoyo_path_info
*candidate
;
709 struct tomoyo_path_info exename
;
710 struct tomoyo_execve
*ee
= kzalloc(sizeof(*ee
), GFP_NOFS
);
714 ee
->tmp
= kzalloc(TOMOYO_EXEC_TMPSIZE
, GFP_NOFS
);
719 /* ee->dump->data is allocated by tomoyo_dump_page(). */
720 tomoyo_init_request_info(&ee
->r
, NULL
, TOMOYO_MAC_FILE_EXECUTE
);
723 ee
->r
.obj
= &ee
->obj
;
724 ee
->obj
.path1
= bprm
->file
->f_path
;
725 /* Get symlink's pathname of program. */
727 exename
.name
= tomoyo_realpath_nofollow(original_name
);
730 tomoyo_fill_path_info(&exename
);
732 /* Check 'aggregator' directive. */
734 struct tomoyo_aggregator
*ptr
;
735 struct list_head
*list
=
736 &old_domain
->ns
->policy_list
[TOMOYO_ID_AGGREGATOR
];
738 /* Check 'aggregator' directive. */
739 candidate
= &exename
;
740 list_for_each_entry_rcu(ptr
, list
, head
.list
,
741 srcu_read_lock_held(&tomoyo_ss
)) {
742 if (ptr
->head
.is_deleted
||
743 !tomoyo_path_matches_pattern(&exename
,
746 candidate
= ptr
->aggregated_name
;
751 /* Check execute permission. */
752 retval
= tomoyo_execute_permission(&ee
->r
, candidate
);
753 if (retval
== TOMOYO_RETRY_REQUEST
)
758 * To be able to specify domainnames with wildcards, use the
759 * pathname specified in the policy (which may contain
760 * wildcard) rather than the pathname passed to execve()
761 * (which never contains wildcard).
763 if (ee
->r
.param
.path
.matched_path
)
764 candidate
= ee
->r
.param
.path
.matched_path
;
767 * Check for domain transition preference if "file execute" matched.
768 * If preference is given, make execve() fail if domain transition
769 * has failed, for domain transition preference should be used with
770 * destination domain defined.
772 if (ee
->transition
) {
773 const char *domainname
= ee
->transition
->name
;
775 reject_on_transition_failure
= true;
776 if (!strcmp(domainname
, "keep"))
777 goto force_keep_domain
;
778 if (!strcmp(domainname
, "child"))
779 goto force_child_domain
;
780 if (!strcmp(domainname
, "reset"))
781 goto force_reset_domain
;
782 if (!strcmp(domainname
, "initialize"))
783 goto force_initialize_domain
;
784 if (!strcmp(domainname
, "parent")) {
787 strncpy(ee
->tmp
, old_domain
->domainname
->name
,
788 TOMOYO_EXEC_TMPSIZE
- 1);
789 cp
= strrchr(ee
->tmp
, ' ');
792 } else if (*domainname
== '<')
793 strncpy(ee
->tmp
, domainname
, TOMOYO_EXEC_TMPSIZE
- 1);
795 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "%s %s",
796 old_domain
->domainname
->name
, domainname
);
797 goto force_jump_domain
;
800 * No domain transition preference specified.
801 * Calculate domain to transit to.
803 switch (tomoyo_transition_type(old_domain
->ns
, old_domain
->domainname
,
805 case TOMOYO_TRANSITION_CONTROL_RESET
:
807 /* Transit to the root of specified namespace. */
808 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "<%s>",
811 * Make execve() fail if domain transition across namespaces
814 reject_on_transition_failure
= true;
816 case TOMOYO_TRANSITION_CONTROL_INITIALIZE
:
817 force_initialize_domain
:
818 /* Transit to the child of current namespace's root. */
819 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "%s %s",
820 old_domain
->ns
->name
, candidate
->name
);
822 case TOMOYO_TRANSITION_CONTROL_KEEP
:
824 /* Keep current domain. */
828 if (old_domain
== &tomoyo_kernel_domain
&&
829 !tomoyo_policy_loaded
) {
831 * Needn't to transit from kernel domain before
832 * starting /sbin/init. But transit from kernel domain
833 * if executing initializers because they might start
840 /* Normal domain transition. */
841 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "%s %s",
842 old_domain
->domainname
->name
, candidate
->name
);
847 domain
= tomoyo_assign_domain(ee
->tmp
, true);
850 else if (reject_on_transition_failure
) {
851 pr_warn("ERROR: Domain '%s' not ready.\n", ee
->tmp
);
853 } else if (ee
->r
.mode
== TOMOYO_CONFIG_ENFORCING
)
857 if (!old_domain
->flags
[TOMOYO_DIF_TRANSITION_FAILED
]) {
858 old_domain
->flags
[TOMOYO_DIF_TRANSITION_FAILED
] = true;
859 ee
->r
.granted
= false;
860 tomoyo_write_log(&ee
->r
, "%s", tomoyo_dif
861 [TOMOYO_DIF_TRANSITION_FAILED
]);
862 pr_warn("ERROR: Domain '%s' not defined.\n", ee
->tmp
);
868 /* Update reference count on "struct tomoyo_domain_info". */
870 struct tomoyo_task
*s
= tomoyo_task(current
);
872 s
->old_domain_info
= s
->domain_info
;
873 s
->domain_info
= domain
;
874 atomic_inc(&domain
->users
);
878 ee
->r
.domain
= domain
;
879 retval
= tomoyo_environ(ee
);
882 kfree(ee
->dump
.data
);
888 * tomoyo_dump_page - Dump a page to buffer.
890 * @bprm: Pointer to "struct linux_binprm".
891 * @pos: Location to dump.
892 * @dump: Pointer to "struct tomoyo_page_dump".
894 * Returns true on success, false otherwise.
896 bool tomoyo_dump_page(struct linux_binprm
*bprm
, unsigned long pos
,
897 struct tomoyo_page_dump
*dump
)
901 /* dump->data is released by tomoyo_find_next_domain(). */
903 dump
->data
= kzalloc(PAGE_SIZE
, GFP_NOFS
);
907 /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
910 * This is called at execve() time in order to dig around
911 * in the argv/environment of the new proceess
912 * (represented by bprm). 'current' is the process doing
915 if (get_user_pages_remote(bprm
->mm
, pos
, 1,
916 FOLL_FORCE
, &page
, NULL
, NULL
) <= 0)
919 page
= bprm
->page
[pos
/ PAGE_SIZE
];
921 if (page
!= dump
->page
) {
922 const unsigned int offset
= pos
% PAGE_SIZE
;
924 * Maybe kmap()/kunmap() should be used here.
925 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
928 char *kaddr
= kmap_atomic(page
);
931 memcpy(dump
->data
+ offset
, kaddr
+ offset
,
933 kunmap_atomic(kaddr
);
935 /* Same with put_arg_page(page) in fs/exec.c */