2 * security/tomoyo/domain.c
4 * Domain transition functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
10 #include <linux/binfmts.h>
11 #include <linux/slab.h>
13 /* Variables definitions.*/
15 /* The initial domain. */
16 struct tomoyo_domain_info tomoyo_kernel_domain;
19 * tomoyo_update_policy - Update an entry for exception policy.
21 * @new_entry: Pointer to "struct tomoyo_acl_info".
22 * @size: Size of @new_entry in bytes.
23 * @is_delete: True if it is a delete request.
24 * @list: Pointer to "struct list_head".
25 * @check_duplicate: Callback function to find duplicated entry.
27 * Returns 0 on success, negative value otherwise.
29 * Caller holds tomoyo_read_lock().
31 int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
32 bool is_delete, struct list_head *list,
33 bool (*check_duplicate) (const struct tomoyo_acl_head
35 const struct tomoyo_acl_head
38 int error = is_delete ? -ENOENT : -ENOMEM;
39 struct tomoyo_acl_head *entry;
41 if (mutex_lock_interruptible(&tomoyo_policy_lock))
43 list_for_each_entry_rcu(entry, list, list) {
44 if (!check_duplicate(entry, new_entry))
46 entry->is_deleted = is_delete;
50 if (error && !is_delete) {
51 entry = tomoyo_commit_ok(new_entry, size);
53 list_add_tail_rcu(&entry->list, list);
57 mutex_unlock(&tomoyo_policy_lock);
62 * tomoyo_update_domain - Update an entry for domain policy.
64 * @new_entry: Pointer to "struct tomoyo_acl_info".
65 * @size: Size of @new_entry in bytes.
66 * @is_delete: True if it is a delete request.
67 * @domain: Pointer to "struct tomoyo_domain_info".
68 * @check_duplicate: Callback function to find duplicated entry.
69 * @merge_duplicate: Callback function to merge duplicated entry.
71 * Returns 0 on success, negative value otherwise.
73 * Caller holds tomoyo_read_lock().
75 int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
76 bool is_delete, struct tomoyo_domain_info *domain,
77 bool (*check_duplicate) (const struct tomoyo_acl_info
79 const struct tomoyo_acl_info
81 bool (*merge_duplicate) (struct tomoyo_acl_info *,
82 struct tomoyo_acl_info *,
85 int error = is_delete ? -ENOENT : -ENOMEM;
86 struct tomoyo_acl_info *entry;
88 if (mutex_lock_interruptible(&tomoyo_policy_lock))
90 list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
91 if (!check_duplicate(entry, new_entry))
94 entry->is_deleted = merge_duplicate(entry, new_entry,
97 entry->is_deleted = is_delete;
101 if (error && !is_delete) {
102 entry = tomoyo_commit_ok(new_entry, size);
104 list_add_tail_rcu(&entry->list, &domain->acl_info_list);
108 mutex_unlock(&tomoyo_policy_lock);
112 void tomoyo_check_acl(struct tomoyo_request_info *r,
113 bool (*check_entry) (const struct tomoyo_request_info *,
114 const struct tomoyo_acl_info *))
116 const struct tomoyo_domain_info *domain = r->domain;
117 struct tomoyo_acl_info *ptr;
119 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
120 if (ptr->is_deleted || ptr->type != r->param_type)
122 if (check_entry(r, ptr)) {
130 /* The list for "struct tomoyo_domain_info". */
131 LIST_HEAD(tomoyo_domain_list);
133 struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
134 struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
137 * tomoyo_get_last_name - Get last component of a domainname.
139 * @domain: Pointer to "struct tomoyo_domain_info".
141 * Returns the last component of the domainname.
143 const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
145 const char *cp0 = domain->domainname->name;
146 const char *cp1 = strrchr(cp0, ' ');
153 static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
155 const struct tomoyo_acl_head *
158 const struct tomoyo_domain_initializer_entry *p1 =
159 container_of(a, typeof(*p1), head);
160 const struct tomoyo_domain_initializer_entry *p2 =
161 container_of(b, typeof(*p2), head);
162 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
163 && p1->domainname == p2->domainname
164 && p1->program == p2->program;
168 * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
170 * @domainname: The name of domain. May be NULL.
171 * @program: The name of program.
172 * @is_not: True if it is "no_initialize_domain" entry.
173 * @is_delete: True if it is a delete request.
175 * Returns 0 on success, negative value otherwise.
177 * Caller holds tomoyo_read_lock().
179 static int tomoyo_update_domain_initializer_entry(const char *domainname,
182 const bool is_delete)
184 struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
185 int error = is_delete ? -ENOENT : -ENOMEM;
187 if (!tomoyo_correct_path(program))
190 if (!tomoyo_domain_def(domainname) &&
191 tomoyo_correct_path(domainname))
192 e.is_last_name = true;
193 else if (!tomoyo_correct_domain(domainname))
195 e.domainname = tomoyo_get_name(domainname);
199 e.program = tomoyo_get_name(program);
202 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
204 [TOMOYO_ID_DOMAIN_INITIALIZER],
205 tomoyo_same_domain_initializer_entry);
207 tomoyo_put_name(e.domainname);
208 tomoyo_put_name(e.program);
213 * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
215 * @data: String to parse.
216 * @is_not: True if it is "no_initialize_domain" entry.
217 * @is_delete: True if it is a delete request.
219 * Returns 0 on success, negative value otherwise.
221 * Caller holds tomoyo_read_lock().
223 int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
224 const bool is_delete)
226 char *cp = strstr(data, " from ");
230 return tomoyo_update_domain_initializer_entry(cp + 6, data,
234 return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
239 * tomoyo_domain_initializer - Check whether the given program causes domainname reinitialization.
241 * @domainname: The name of domain.
242 * @program: The name of program.
243 * @last_name: The last component of @domainname.
245 * Returns true if executing @program reinitializes domain transition,
248 * Caller holds tomoyo_read_lock().
250 static bool tomoyo_domain_initializer(const struct tomoyo_path_info *
252 const struct tomoyo_path_info *program,
253 const struct tomoyo_path_info *
256 struct tomoyo_domain_initializer_entry *ptr;
259 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
260 [TOMOYO_ID_DOMAIN_INITIALIZER], head.list) {
261 if (ptr->head.is_deleted)
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))
283 static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
284 const struct tomoyo_acl_head *b)
286 const struct tomoyo_domain_keeper_entry *p1 =
287 container_of(a, typeof(*p1), head);
288 const struct tomoyo_domain_keeper_entry *p2 =
289 container_of(b, typeof(*p2), head);
290 return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
291 && p1->domainname == p2->domainname
292 && p1->program == p2->program;
296 * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
298 * @domainname: The name of domain.
299 * @program: The name of program. May be NULL.
300 * @is_not: True if it is "no_keep_domain" entry.
301 * @is_delete: True if it is a delete request.
303 * Returns 0 on success, negative value otherwise.
305 * Caller holds tomoyo_read_lock().
307 static int tomoyo_update_domain_keeper_entry(const char *domainname,
310 const bool is_delete)
312 struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
313 int error = is_delete ? -ENOENT : -ENOMEM;
315 if (!tomoyo_domain_def(domainname) &&
316 tomoyo_correct_path(domainname))
317 e.is_last_name = true;
318 else if (!tomoyo_correct_domain(domainname))
321 if (!tomoyo_correct_path(program))
323 e.program = tomoyo_get_name(program);
327 e.domainname = tomoyo_get_name(domainname);
330 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
332 [TOMOYO_ID_DOMAIN_KEEPER],
333 tomoyo_same_domain_keeper_entry);
335 tomoyo_put_name(e.domainname);
336 tomoyo_put_name(e.program);
341 * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
343 * @data: String to parse.
344 * @is_not: True if it is "no_keep_domain" entry.
345 * @is_delete: True if it is a delete request.
347 * Caller holds tomoyo_read_lock().
349 int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
350 const bool is_delete)
352 char *cp = strstr(data, " from ");
356 return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
359 return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
363 * tomoyo_domain_keeper - Check whether the given program causes domain transition suppression.
365 * @domainname: The name of domain.
366 * @program: The name of program.
367 * @last_name: The last component of @domainname.
369 * Returns true if executing @program supresses domain transition,
372 * Caller holds tomoyo_read_lock().
374 static bool tomoyo_domain_keeper(const struct tomoyo_path_info *domainname,
375 const struct tomoyo_path_info *program,
376 const struct tomoyo_path_info *last_name)
378 struct tomoyo_domain_keeper_entry *ptr;
381 list_for_each_entry_rcu(ptr,
382 &tomoyo_policy_list[TOMOYO_ID_DOMAIN_KEEPER],
384 if (ptr->head.is_deleted)
386 if (!ptr->is_last_name) {
387 if (ptr->domainname != domainname)
390 if (tomoyo_pathcmp(ptr->domainname, last_name))
393 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
404 static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
405 const struct tomoyo_acl_head *b)
407 const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
409 const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
411 return p1->original_name == p2->original_name &&
412 p1->aggregated_name == p2->aggregated_name;
416 * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
418 * @original_name: The original program's name.
419 * @aggregated_name: The program name to use.
420 * @is_delete: True if it is a delete request.
422 * Returns 0 on success, negative value otherwise.
424 * Caller holds tomoyo_read_lock().
426 static int tomoyo_update_aggregator_entry(const char *original_name,
427 const char *aggregated_name,
428 const bool is_delete)
430 struct tomoyo_aggregator_entry e = { };
431 int error = is_delete ? -ENOENT : -ENOMEM;
433 if (!tomoyo_correct_path(original_name) ||
434 !tomoyo_correct_path(aggregated_name))
436 e.original_name = tomoyo_get_name(original_name);
437 e.aggregated_name = tomoyo_get_name(aggregated_name);
438 if (!e.original_name || !e.aggregated_name ||
439 e.aggregated_name->is_patterned) /* No patterns allowed. */
441 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
442 &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
443 tomoyo_same_aggregator_entry);
445 tomoyo_put_name(e.original_name);
446 tomoyo_put_name(e.aggregated_name);
451 * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
453 * @data: String to parse.
454 * @is_delete: True if it is a delete request.
456 * Returns 0 on success, negative value otherwise.
458 * Caller holds tomoyo_read_lock().
460 int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
462 char *cp = strchr(data, ' ');
467 return tomoyo_update_aggregator_entry(data, cp, is_delete);
470 static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
471 const struct tomoyo_acl_head *b)
473 const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
475 const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
477 return p1->original_name == p2->original_name &&
478 p1->aliased_name == p2->aliased_name;
482 * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
484 * @original_name: The original program's real name.
485 * @aliased_name: The symbolic program's symbolic link's name.
486 * @is_delete: True if it is a delete request.
488 * Returns 0 on success, negative value otherwise.
490 * Caller holds tomoyo_read_lock().
492 static int tomoyo_update_alias_entry(const char *original_name,
493 const char *aliased_name,
494 const bool is_delete)
496 struct tomoyo_alias_entry e = { };
497 int error = is_delete ? -ENOENT : -ENOMEM;
499 if (!tomoyo_correct_path(original_name) ||
500 !tomoyo_correct_path(aliased_name))
502 e.original_name = tomoyo_get_name(original_name);
503 e.aliased_name = tomoyo_get_name(aliased_name);
504 if (!e.original_name || !e.aliased_name ||
505 e.original_name->is_patterned || e.aliased_name->is_patterned)
506 goto out; /* No patterns allowed. */
507 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
508 &tomoyo_policy_list[TOMOYO_ID_ALIAS],
509 tomoyo_same_alias_entry);
511 tomoyo_put_name(e.original_name);
512 tomoyo_put_name(e.aliased_name);
517 * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
519 * @data: String to parse.
520 * @is_delete: True if it is a delete request.
522 * Returns 0 on success, negative value otherwise.
524 * Caller holds tomoyo_read_lock().
526 int tomoyo_write_alias_policy(char *data, const bool is_delete)
528 char *cp = strchr(data, ' ');
533 return tomoyo_update_alias_entry(data, cp, is_delete);
537 * tomoyo_find_or_assign_new_domain - Create a domain.
539 * @domainname: The name of domain.
540 * @profile: Profile number to assign if the domain was newly created.
542 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
544 * Caller holds tomoyo_read_lock().
546 struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
550 struct tomoyo_domain_info *entry;
551 struct tomoyo_domain_info *domain = NULL;
552 const struct tomoyo_path_info *saved_domainname;
555 if (!tomoyo_correct_domain(domainname))
557 saved_domainname = tomoyo_get_name(domainname);
558 if (!saved_domainname)
560 entry = kzalloc(sizeof(*entry), GFP_NOFS);
561 if (mutex_lock_interruptible(&tomoyo_policy_lock))
563 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
564 if (domain->is_deleted ||
565 tomoyo_pathcmp(saved_domainname, domain->domainname))
570 if (!found && tomoyo_memory_ok(entry)) {
571 INIT_LIST_HEAD(&entry->acl_info_list);
572 entry->domainname = saved_domainname;
573 saved_domainname = NULL;
574 entry->profile = profile;
575 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
580 mutex_unlock(&tomoyo_policy_lock);
582 tomoyo_put_name(saved_domainname);
584 return found ? domain : NULL;
588 * tomoyo_find_next_domain - Find a domain.
590 * @bprm: Pointer to "struct linux_binprm".
592 * Returns 0 on success, negative value otherwise.
594 * Caller holds tomoyo_read_lock().
596 int tomoyo_find_next_domain(struct linux_binprm *bprm)
598 struct tomoyo_request_info r;
599 char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
600 struct tomoyo_domain_info *old_domain = tomoyo_domain();
601 struct tomoyo_domain_info *domain = NULL;
602 const char *old_domain_name = old_domain->domainname->name;
603 const char *original_name = bprm->filename;
606 int retval = -ENOMEM;
607 bool need_kfree = false;
608 struct tomoyo_path_info rn = { }; /* real name */
609 struct tomoyo_path_info sn = { }; /* symlink name */
610 struct tomoyo_path_info ln; /* last name */
612 ln.name = tomoyo_get_last_name(old_domain);
613 tomoyo_fill_path_info(&ln);
614 mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
615 is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
624 /* Get tomoyo_realpath of program. */
626 rn.name = tomoyo_realpath(original_name);
629 tomoyo_fill_path_info(&rn);
632 /* Get tomoyo_realpath of symbolic link. */
633 sn.name = tomoyo_realpath_nofollow(original_name);
636 tomoyo_fill_path_info(&sn);
638 /* Check 'alias' directive. */
639 if (tomoyo_pathcmp(&rn, &sn)) {
640 struct tomoyo_alias_entry *ptr;
641 /* Is this program allowed to be called via symbolic links? */
642 list_for_each_entry_rcu(ptr,
643 &tomoyo_policy_list[TOMOYO_ID_ALIAS],
645 if (ptr->head.is_deleted ||
646 tomoyo_pathcmp(&rn, ptr->original_name) ||
647 tomoyo_pathcmp(&sn, ptr->aliased_name))
651 /* This is OK because it is read only. */
652 rn = *ptr->aliased_name;
657 /* Check 'aggregator' directive. */
659 struct tomoyo_aggregator_entry *ptr;
660 list_for_each_entry_rcu(ptr, &tomoyo_policy_list
661 [TOMOYO_ID_AGGREGATOR], head.list) {
662 if (ptr->head.is_deleted ||
663 !tomoyo_path_matches_pattern(&rn,
669 /* This is OK because it is read only. */
670 rn = *ptr->aggregated_name;
675 /* Check execute permission. */
676 retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
677 if (retval == TOMOYO_RETRY_REQUEST)
682 if (tomoyo_domain_initializer(old_domain->domainname, &rn, &ln)) {
683 /* Transit to the child of tomoyo_kernel_domain domain. */
684 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
685 TOMOYO_ROOT_NAME " " "%s", rn.name);
686 } else if (old_domain == &tomoyo_kernel_domain &&
687 !tomoyo_policy_loaded) {
689 * Needn't to transit from kernel domain before starting
690 * /sbin/init. But transit from kernel domain if executing
691 * initializers because they might start before /sbin/init.
694 } else if (tomoyo_domain_keeper(old_domain->domainname, &rn, &ln)) {
695 /* Keep current domain. */
698 /* Normal domain transition. */
699 snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
700 "%s %s", old_domain_name, rn.name);
702 if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
704 domain = tomoyo_find_domain(tmp);
708 int error = tomoyo_supervisor(&r, "# wants to create domain\n"
710 if (error == TOMOYO_RETRY_REQUEST)
715 domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
719 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
723 old_domain->transition_failed = true;
727 /* Update reference count on "struct tomoyo_domain_info". */
728 atomic_inc(&domain->users);
729 bprm->cred->security = domain;