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 if (entry
->is_deleted
== TOMOYO_GC_IN_PROGRESS
)
47 if (!check_duplicate(entry
, new_entry
))
49 entry
->is_deleted
= param
->is_delete
;
53 if (error
&& !param
->is_delete
) {
54 entry
= tomoyo_commit_ok(new_entry
, size
);
56 list_add_tail_rcu(&entry
->list
, list
);
60 mutex_unlock(&tomoyo_policy_lock
);
65 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
67 * @a: Pointer to "struct tomoyo_acl_info".
68 * @b: Pointer to "struct tomoyo_acl_info".
70 * Returns true if @a == @b, false otherwise.
72 static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info
*a
,
73 const struct tomoyo_acl_info
*b
)
75 return a
->type
== b
->type
&& a
->cond
== b
->cond
;
79 * tomoyo_update_domain - Update an entry for domain policy.
81 * @new_entry: Pointer to "struct tomoyo_acl_info".
82 * @size: Size of @new_entry in bytes.
83 * @param: Pointer to "struct tomoyo_acl_param".
84 * @check_duplicate: Callback function to find duplicated entry.
85 * @merge_duplicate: Callback function to merge duplicated entry.
87 * Returns 0 on success, negative value otherwise.
89 * Caller holds tomoyo_read_lock().
91 int tomoyo_update_domain(struct tomoyo_acl_info
*new_entry
, const int size
,
92 struct tomoyo_acl_param
*param
,
93 bool (*check_duplicate
)(const struct tomoyo_acl_info
95 const struct tomoyo_acl_info
97 bool (*merge_duplicate
)(struct tomoyo_acl_info
*,
98 struct tomoyo_acl_info
*,
101 const bool is_delete
= param
->is_delete
;
102 int error
= is_delete
? -ENOENT
: -ENOMEM
;
103 struct tomoyo_acl_info
*entry
;
104 struct list_head
* const list
= param
->list
;
106 if (param
->data
[0]) {
107 new_entry
->cond
= tomoyo_get_condition(param
);
108 if (!new_entry
->cond
)
111 * Domain transition preference is allowed for only
112 * "file execute" entries.
114 if (new_entry
->cond
->transit
&&
115 !(new_entry
->type
== TOMOYO_TYPE_PATH_ACL
&&
116 container_of(new_entry
, struct tomoyo_path_acl
, head
)
117 ->perm
== 1 << TOMOYO_TYPE_EXECUTE
))
120 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
122 list_for_each_entry_rcu(entry
, list
, list
) {
123 if (entry
->is_deleted
== TOMOYO_GC_IN_PROGRESS
)
125 if (!tomoyo_same_acl_head(entry
, new_entry
) ||
126 !check_duplicate(entry
, new_entry
))
129 entry
->is_deleted
= merge_duplicate(entry
, new_entry
,
132 entry
->is_deleted
= is_delete
;
136 if (error
&& !is_delete
) {
137 entry
= tomoyo_commit_ok(new_entry
, size
);
139 list_add_tail_rcu(&entry
->list
, list
);
143 mutex_unlock(&tomoyo_policy_lock
);
145 tomoyo_put_condition(new_entry
->cond
);
150 * tomoyo_check_acl - Do permission check.
152 * @r: Pointer to "struct tomoyo_request_info".
153 * @check_entry: Callback function to check type specific parameters.
155 * Returns 0 on success, negative value otherwise.
157 * Caller holds tomoyo_read_lock().
159 void tomoyo_check_acl(struct tomoyo_request_info
*r
,
160 bool (*check_entry
)(struct tomoyo_request_info
*,
161 const struct tomoyo_acl_info
*))
163 const struct tomoyo_domain_info
*domain
= r
->domain
;
164 struct tomoyo_acl_info
*ptr
;
165 const struct list_head
*list
= &domain
->acl_info_list
;
169 list_for_each_entry_rcu(ptr
, list
, list
) {
170 if (ptr
->is_deleted
|| ptr
->type
!= r
->param_type
)
172 if (!check_entry(r
, ptr
))
174 if (!tomoyo_condition(r
, ptr
->cond
))
176 r
->matched_acl
= ptr
;
180 for (; i
< TOMOYO_MAX_ACL_GROUPS
; i
++) {
181 if (!test_bit(i
, domain
->group
))
183 list
= &domain
->ns
->acl_group
[i
++];
189 /* The list for "struct tomoyo_domain_info". */
190 LIST_HEAD(tomoyo_domain_list
);
193 * tomoyo_last_word - Get last component of a domainname.
195 * @name: Domainname to check.
197 * Returns the last word of @domainname.
199 static const char *tomoyo_last_word(const char *name
)
201 const char *cp
= strrchr(name
, ' ');
209 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
211 * @a: Pointer to "struct tomoyo_acl_head".
212 * @b: Pointer to "struct tomoyo_acl_head".
214 * Returns true if @a == @b, false otherwise.
216 static bool tomoyo_same_transition_control(const struct tomoyo_acl_head
*a
,
217 const struct tomoyo_acl_head
*b
)
219 const struct tomoyo_transition_control
*p1
= container_of(a
,
222 const struct tomoyo_transition_control
*p2
= container_of(b
,
226 return p1
->type
== p2
->type
&& p1
->is_last_name
== p2
->is_last_name
227 && p1
->domainname
== p2
->domainname
228 && p1
->program
== p2
->program
;
232 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
234 * @param: Pointer to "struct tomoyo_acl_param".
235 * @type: Type of this entry.
237 * Returns 0 on success, negative value otherwise.
239 int tomoyo_write_transition_control(struct tomoyo_acl_param
*param
,
242 struct tomoyo_transition_control e
= { .type
= type
};
243 int error
= param
->is_delete
? -ENOENT
: -ENOMEM
;
244 char *program
= param
->data
;
245 char *domainname
= strstr(program
, " from ");
250 } else if (type
== TOMOYO_TRANSITION_CONTROL_NO_KEEP
||
251 type
== TOMOYO_TRANSITION_CONTROL_KEEP
) {
252 domainname
= program
;
255 if (program
&& strcmp(program
, "any")) {
256 if (!tomoyo_correct_path(program
))
258 e
.program
= tomoyo_get_name(program
);
262 if (domainname
&& strcmp(domainname
, "any")) {
263 if (!tomoyo_correct_domain(domainname
)) {
264 if (!tomoyo_correct_path(domainname
))
266 e
.is_last_name
= true;
268 e
.domainname
= tomoyo_get_name(domainname
);
272 param
->list
= ¶m
->ns
->policy_list
[TOMOYO_ID_TRANSITION_CONTROL
];
273 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
274 tomoyo_same_transition_control
);
276 tomoyo_put_name(e
.domainname
);
277 tomoyo_put_name(e
.program
);
282 * tomoyo_scan_transition - Try to find specific domain transition type.
284 * @list: Pointer to "struct list_head".
285 * @domainname: The name of current domain.
286 * @program: The name of requested program.
287 * @last_name: The last component of @domainname.
288 * @type: One of values in "enum tomoyo_transition_type".
290 * Returns true if found one, false otherwise.
292 * Caller holds tomoyo_read_lock().
294 static inline bool tomoyo_scan_transition
295 (const struct list_head
*list
, const struct tomoyo_path_info
*domainname
,
296 const struct tomoyo_path_info
*program
, const char *last_name
,
297 const enum tomoyo_transition_type type
)
299 const struct tomoyo_transition_control
*ptr
;
301 list_for_each_entry_rcu(ptr
, list
, head
.list
) {
302 if (ptr
->head
.is_deleted
|| ptr
->type
!= type
)
304 if (ptr
->domainname
) {
305 if (!ptr
->is_last_name
) {
306 if (ptr
->domainname
!= domainname
)
310 * Use direct strcmp() since this is
313 if (strcmp(ptr
->domainname
->name
, last_name
))
317 if (ptr
->program
&& tomoyo_pathcmp(ptr
->program
, program
))
325 * tomoyo_transition_type - Get domain transition type.
327 * @ns: Pointer to "struct tomoyo_policy_namespace".
328 * @domainname: The name of current domain.
329 * @program: The name of requested program.
331 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
332 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
333 * executing @program reinitializes domain transition within that namespace,
334 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
337 * Caller holds tomoyo_read_lock().
339 static enum tomoyo_transition_type tomoyo_transition_type
340 (const struct tomoyo_policy_namespace
*ns
,
341 const struct tomoyo_path_info
*domainname
,
342 const struct tomoyo_path_info
*program
)
344 const char *last_name
= tomoyo_last_word(domainname
->name
);
345 enum tomoyo_transition_type type
= TOMOYO_TRANSITION_CONTROL_NO_RESET
;
347 while (type
< TOMOYO_MAX_TRANSITION_TYPE
) {
348 const struct list_head
* const list
=
349 &ns
->policy_list
[TOMOYO_ID_TRANSITION_CONTROL
];
351 if (!tomoyo_scan_transition(list
, domainname
, program
,
356 if (type
!= TOMOYO_TRANSITION_CONTROL_NO_RESET
&&
357 type
!= TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE
)
360 * Do not check for reset_domain if no_reset_domain matched.
361 * Do not check for initialize_domain if no_initialize_domain
371 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
373 * @a: Pointer to "struct tomoyo_acl_head".
374 * @b: Pointer to "struct tomoyo_acl_head".
376 * Returns true if @a == @b, false otherwise.
378 static bool tomoyo_same_aggregator(const struct tomoyo_acl_head
*a
,
379 const struct tomoyo_acl_head
*b
)
381 const struct tomoyo_aggregator
*p1
= container_of(a
, typeof(*p1
),
383 const struct tomoyo_aggregator
*p2
= container_of(b
, typeof(*p2
),
386 return p1
->original_name
== p2
->original_name
&&
387 p1
->aggregated_name
== p2
->aggregated_name
;
391 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
393 * @param: Pointer to "struct tomoyo_acl_param".
395 * Returns 0 on success, negative value otherwise.
397 * Caller holds tomoyo_read_lock().
399 int tomoyo_write_aggregator(struct tomoyo_acl_param
*param
)
401 struct tomoyo_aggregator e
= { };
402 int error
= param
->is_delete
? -ENOENT
: -ENOMEM
;
403 const char *original_name
= tomoyo_read_token(param
);
404 const char *aggregated_name
= tomoyo_read_token(param
);
406 if (!tomoyo_correct_word(original_name
) ||
407 !tomoyo_correct_path(aggregated_name
))
409 e
.original_name
= tomoyo_get_name(original_name
);
410 e
.aggregated_name
= tomoyo_get_name(aggregated_name
);
411 if (!e
.original_name
|| !e
.aggregated_name
||
412 e
.aggregated_name
->is_patterned
) /* No patterns allowed. */
414 param
->list
= ¶m
->ns
->policy_list
[TOMOYO_ID_AGGREGATOR
];
415 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
416 tomoyo_same_aggregator
);
418 tomoyo_put_name(e
.original_name
);
419 tomoyo_put_name(e
.aggregated_name
);
424 * tomoyo_find_namespace - Find specified namespace.
426 * @name: Name of namespace to find.
427 * @len: Length of @name.
429 * Returns pointer to "struct tomoyo_policy_namespace" if found,
432 * Caller holds tomoyo_read_lock().
434 static struct tomoyo_policy_namespace
*tomoyo_find_namespace
435 (const char *name
, const unsigned int len
)
437 struct tomoyo_policy_namespace
*ns
;
439 list_for_each_entry(ns
, &tomoyo_namespace_list
, namespace_list
) {
440 if (strncmp(name
, ns
->name
, len
) ||
441 (name
[len
] && name
[len
] != ' '))
449 * tomoyo_assign_namespace - Create a new namespace.
451 * @domainname: Name of namespace to create.
453 * Returns pointer to "struct tomoyo_policy_namespace" on success,
456 * Caller holds tomoyo_read_lock().
458 struct tomoyo_policy_namespace
*tomoyo_assign_namespace(const char *domainname
)
460 struct tomoyo_policy_namespace
*ptr
;
461 struct tomoyo_policy_namespace
*entry
;
462 const char *cp
= domainname
;
463 unsigned int len
= 0;
465 while (*cp
&& *cp
++ != ' ')
467 ptr
= tomoyo_find_namespace(domainname
, len
);
470 if (len
>= TOMOYO_EXEC_TMPSIZE
- 10 || !tomoyo_domain_def(domainname
))
472 entry
= kzalloc(sizeof(*entry
) + len
+ 1, GFP_NOFS
);
475 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
477 ptr
= tomoyo_find_namespace(domainname
, len
);
478 if (!ptr
&& tomoyo_memory_ok(entry
)) {
479 char *name
= (char *) (entry
+ 1);
482 memmove(name
, domainname
, len
);
485 tomoyo_init_policy_namespace(entry
);
488 mutex_unlock(&tomoyo_policy_lock
);
495 * tomoyo_namespace_jump - Check for namespace jump.
497 * @domainname: Name of domain.
499 * Returns true if namespace differs, false otherwise.
501 static bool tomoyo_namespace_jump(const char *domainname
)
503 const char *namespace = tomoyo_current_namespace()->name
;
504 const int len
= strlen(namespace);
506 return strncmp(domainname
, namespace, len
) ||
507 (domainname
[len
] && domainname
[len
] != ' ');
511 * tomoyo_assign_domain - Create a domain or a namespace.
513 * @domainname: The name of domain.
514 * @transit: True if transit to domain found or created.
516 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
518 * Caller holds tomoyo_read_lock().
520 struct tomoyo_domain_info
*tomoyo_assign_domain(const char *domainname
,
523 struct tomoyo_domain_info e
= { };
524 struct tomoyo_domain_info
*entry
= tomoyo_find_domain(domainname
);
525 bool created
= false;
530 * Since namespace is created at runtime, profiles may
531 * not be created by the moment the process transits to
532 * that domain. Do not perform domain transition if
533 * profile for that domain is not yet created.
535 if (tomoyo_policy_loaded
&&
536 !entry
->ns
->profile_ptr
[entry
->profile
])
541 /* Requested domain does not exist. */
542 /* Don't create requested domain if domainname is invalid. */
543 if (strlen(domainname
) >= TOMOYO_EXEC_TMPSIZE
- 10 ||
544 !tomoyo_correct_domain(domainname
))
547 * Since definition of profiles and acl_groups may differ across
548 * namespaces, do not inherit "use_profile" and "use_group" settings
549 * by automatically creating requested domain upon domain transition.
551 if (transit
&& tomoyo_namespace_jump(domainname
))
553 e
.ns
= tomoyo_assign_namespace(domainname
);
557 * "use_profile" and "use_group" settings for automatically created
558 * domains are inherited from current domain. These are 0 for manually
562 const struct tomoyo_domain_info
*domain
= tomoyo_domain();
564 e
.profile
= domain
->profile
;
565 memcpy(e
.group
, domain
->group
, sizeof(e
.group
));
567 e
.domainname
= tomoyo_get_name(domainname
);
570 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
572 entry
= tomoyo_find_domain(domainname
);
574 entry
= tomoyo_commit_ok(&e
, sizeof(e
));
576 INIT_LIST_HEAD(&entry
->acl_info_list
);
577 list_add_tail_rcu(&entry
->list
, &tomoyo_domain_list
);
581 mutex_unlock(&tomoyo_policy_lock
);
583 tomoyo_put_name(e
.domainname
);
584 if (entry
&& transit
) {
586 struct tomoyo_request_info r
;
589 tomoyo_init_request_info(&r
, entry
,
590 TOMOYO_MAC_FILE_EXECUTE
);
592 tomoyo_write_log(&r
, "use_profile %u\n",
594 for (i
= 0; i
< TOMOYO_MAX_ACL_GROUPS
; i
++)
595 if (test_bit(i
, entry
->group
))
596 tomoyo_write_log(&r
, "use_group %u\n",
598 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES
);
605 * tomoyo_environ - Check permission for environment variable names.
607 * @ee: Pointer to "struct tomoyo_execve".
609 * Returns 0 on success, negative value otherwise.
611 static int tomoyo_environ(struct tomoyo_execve
*ee
)
613 struct tomoyo_request_info
*r
= &ee
->r
;
614 struct linux_binprm
*bprm
= ee
->bprm
;
615 /* env_page.data is allocated by tomoyo_dump_page(). */
616 struct tomoyo_page_dump env_page
= { };
617 char *arg_ptr
; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
619 unsigned long pos
= bprm
->p
;
620 int offset
= pos
% PAGE_SIZE
;
621 int argv_count
= bprm
->argc
;
622 int envp_count
= bprm
->envc
;
625 ee
->r
.type
= TOMOYO_MAC_ENVIRON
;
626 ee
->r
.profile
= r
->domain
->profile
;
627 ee
->r
.mode
= tomoyo_get_mode(r
->domain
->ns
, ee
->r
.profile
,
629 if (!r
->mode
|| !envp_count
)
631 arg_ptr
= kzalloc(TOMOYO_EXEC_TMPSIZE
, GFP_NOFS
);
634 while (error
== -ENOMEM
) {
635 if (!tomoyo_dump_page(bprm
, pos
, &env_page
))
637 pos
+= PAGE_SIZE
- offset
;
639 while (argv_count
&& offset
< PAGE_SIZE
) {
640 if (!env_page
.data
[offset
++])
647 while (offset
< PAGE_SIZE
) {
648 const unsigned char c
= env_page
.data
[offset
++];
650 if (c
&& arg_len
< TOMOYO_EXEC_TMPSIZE
- 10) {
652 arg_ptr
[arg_len
++] = '\0';
653 } else if (c
== '\\') {
654 arg_ptr
[arg_len
++] = '\\';
655 arg_ptr
[arg_len
++] = '\\';
656 } else if (c
> ' ' && c
< 127) {
657 arg_ptr
[arg_len
++] = c
;
659 arg_ptr
[arg_len
++] = '\\';
660 arg_ptr
[arg_len
++] = (c
>> 6) + '0';
662 = ((c
>> 3) & 7) + '0';
663 arg_ptr
[arg_len
++] = (c
& 7) + '0';
666 arg_ptr
[arg_len
] = '\0';
670 if (tomoyo_env_perm(r
, arg_ptr
)) {
683 if (r
->mode
!= TOMOYO_CONFIG_ENFORCING
)
685 kfree(env_page
.data
);
691 * tomoyo_find_next_domain - Find a domain.
693 * @bprm: Pointer to "struct linux_binprm".
695 * Returns 0 on success, negative value otherwise.
697 * Caller holds tomoyo_read_lock().
699 int tomoyo_find_next_domain(struct linux_binprm
*bprm
)
701 struct tomoyo_domain_info
*old_domain
= tomoyo_domain();
702 struct tomoyo_domain_info
*domain
= NULL
;
703 const char *original_name
= bprm
->filename
;
704 int retval
= -ENOMEM
;
705 bool reject_on_transition_failure
= false;
706 const struct tomoyo_path_info
*candidate
;
707 struct tomoyo_path_info exename
;
708 struct tomoyo_execve
*ee
= kzalloc(sizeof(*ee
), GFP_NOFS
);
712 ee
->tmp
= kzalloc(TOMOYO_EXEC_TMPSIZE
, GFP_NOFS
);
717 /* ee->dump->data is allocated by tomoyo_dump_page(). */
718 tomoyo_init_request_info(&ee
->r
, NULL
, TOMOYO_MAC_FILE_EXECUTE
);
721 ee
->r
.obj
= &ee
->obj
;
722 ee
->obj
.path1
= bprm
->file
->f_path
;
723 /* Get symlink's pathname of program. */
725 exename
.name
= tomoyo_realpath_nofollow(original_name
);
728 tomoyo_fill_path_info(&exename
);
730 /* Check 'aggregator' directive. */
732 struct tomoyo_aggregator
*ptr
;
733 struct list_head
*list
=
734 &old_domain
->ns
->policy_list
[TOMOYO_ID_AGGREGATOR
];
736 /* Check 'aggregator' directive. */
737 candidate
= &exename
;
738 list_for_each_entry_rcu(ptr
, list
, head
.list
) {
739 if (ptr
->head
.is_deleted
||
740 !tomoyo_path_matches_pattern(&exename
,
743 candidate
= ptr
->aggregated_name
;
748 /* Check execute permission. */
749 retval
= tomoyo_execute_permission(&ee
->r
, candidate
);
750 if (retval
== TOMOYO_RETRY_REQUEST
)
755 * To be able to specify domainnames with wildcards, use the
756 * pathname specified in the policy (which may contain
757 * wildcard) rather than the pathname passed to execve()
758 * (which never contains wildcard).
760 if (ee
->r
.param
.path
.matched_path
)
761 candidate
= ee
->r
.param
.path
.matched_path
;
764 * Check for domain transition preference if "file execute" matched.
765 * If preference is given, make do_execve() fail if domain transition
766 * has failed, for domain transition preference should be used with
767 * destination domain defined.
769 if (ee
->transition
) {
770 const char *domainname
= ee
->transition
->name
;
772 reject_on_transition_failure
= true;
773 if (!strcmp(domainname
, "keep"))
774 goto force_keep_domain
;
775 if (!strcmp(domainname
, "child"))
776 goto force_child_domain
;
777 if (!strcmp(domainname
, "reset"))
778 goto force_reset_domain
;
779 if (!strcmp(domainname
, "initialize"))
780 goto force_initialize_domain
;
781 if (!strcmp(domainname
, "parent")) {
784 strncpy(ee
->tmp
, old_domain
->domainname
->name
,
785 TOMOYO_EXEC_TMPSIZE
- 1);
786 cp
= strrchr(ee
->tmp
, ' ');
789 } else if (*domainname
== '<')
790 strncpy(ee
->tmp
, domainname
, TOMOYO_EXEC_TMPSIZE
- 1);
792 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "%s %s",
793 old_domain
->domainname
->name
, domainname
);
794 goto force_jump_domain
;
797 * No domain transition preference specified.
798 * Calculate domain to transit to.
800 switch (tomoyo_transition_type(old_domain
->ns
, old_domain
->domainname
,
802 case TOMOYO_TRANSITION_CONTROL_RESET
:
804 /* Transit to the root of specified namespace. */
805 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "<%s>",
808 * Make do_execve() fail if domain transition across namespaces
811 reject_on_transition_failure
= true;
813 case TOMOYO_TRANSITION_CONTROL_INITIALIZE
:
814 force_initialize_domain
:
815 /* Transit to the child of current namespace's root. */
816 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "%s %s",
817 old_domain
->ns
->name
, candidate
->name
);
819 case TOMOYO_TRANSITION_CONTROL_KEEP
:
821 /* Keep current domain. */
825 if (old_domain
== &tomoyo_kernel_domain
&&
826 !tomoyo_policy_loaded
) {
828 * Needn't to transit from kernel domain before
829 * starting /sbin/init. But transit from kernel domain
830 * if executing initializers because they might start
837 /* Normal domain transition. */
838 snprintf(ee
->tmp
, TOMOYO_EXEC_TMPSIZE
- 1, "%s %s",
839 old_domain
->domainname
->name
, candidate
->name
);
844 domain
= tomoyo_assign_domain(ee
->tmp
, true);
847 else if (reject_on_transition_failure
) {
848 pr_warn("ERROR: Domain '%s' not ready.\n", ee
->tmp
);
850 } else if (ee
->r
.mode
== TOMOYO_CONFIG_ENFORCING
)
854 if (!old_domain
->flags
[TOMOYO_DIF_TRANSITION_FAILED
]) {
855 old_domain
->flags
[TOMOYO_DIF_TRANSITION_FAILED
] = true;
856 ee
->r
.granted
= false;
857 tomoyo_write_log(&ee
->r
, "%s", tomoyo_dif
858 [TOMOYO_DIF_TRANSITION_FAILED
]);
859 pr_warn("ERROR: Domain '%s' not defined.\n", ee
->tmp
);
865 /* Update reference count on "struct tomoyo_domain_info". */
867 struct tomoyo_task
*s
= tomoyo_task(current
);
869 s
->old_domain_info
= s
->domain_info
;
870 s
->domain_info
= domain
;
871 atomic_inc(&domain
->users
);
875 ee
->r
.domain
= domain
;
876 retval
= tomoyo_environ(ee
);
879 kfree(ee
->dump
.data
);
885 * tomoyo_dump_page - Dump a page to buffer.
887 * @bprm: Pointer to "struct linux_binprm".
888 * @pos: Location to dump.
889 * @dump: Poiner to "struct tomoyo_page_dump".
891 * Returns true on success, false otherwise.
893 bool tomoyo_dump_page(struct linux_binprm
*bprm
, unsigned long pos
,
894 struct tomoyo_page_dump
*dump
)
898 /* dump->data is released by tomoyo_find_next_domain(). */
900 dump
->data
= kzalloc(PAGE_SIZE
, GFP_NOFS
);
904 /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
907 * This is called at execve() time in order to dig around
908 * in the argv/environment of the new proceess
909 * (represented by bprm). 'current' is the process doing
912 if (get_user_pages_remote(current
, bprm
->mm
, pos
, 1,
913 FOLL_FORCE
, &page
, NULL
, NULL
) <= 0)
916 page
= bprm
->page
[pos
/ PAGE_SIZE
];
918 if (page
!= dump
->page
) {
919 const unsigned int offset
= pos
% PAGE_SIZE
;
921 * Maybe kmap()/kunmap() should be used here.
922 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
925 char *kaddr
= kmap_atomic(page
);
928 memcpy(dump
->data
+ offset
, kaddr
+ offset
,
930 kunmap_atomic(kaddr
);
932 /* Same with put_arg_page(page) in fs/exec.c */