2 * security/tomoyo/common.c
4 * Common functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/uaccess.h>
10 #include <linux/slab.h>
11 #include <linux/security.h>
14 static struct tomoyo_profile tomoyo_default_profile = {
15 .learning = &tomoyo_default_profile.preference,
16 .permissive = &tomoyo_default_profile.preference,
17 .enforcing = &tomoyo_default_profile.preference,
18 .preference.enforcing_verbose = true,
19 .preference.learning_max_entry = 2048,
20 .preference.learning_verbose = false,
21 .preference.permissive_verbose = true
24 /* Profile version. Currently only 20090903 is defined. */
25 static unsigned int tomoyo_profile_version;
27 /* Profile table. Memory is allocated as needed. */
28 static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
30 /* String table for functionality that takes 4 modes. */
31 static const char *tomoyo_mode[4] = {
32 "disabled", "learning", "permissive", "enforcing"
35 /* String table for /sys/kernel/security/tomoyo/profile */
36 static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
37 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
38 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
39 [TOMOYO_MAC_FILE_OPEN] = "file::open",
40 [TOMOYO_MAC_FILE_CREATE] = "file::create",
41 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
42 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
43 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
44 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
45 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
46 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
47 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
48 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
49 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
50 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
51 [TOMOYO_MAC_FILE_LINK] = "file::link",
52 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
53 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
54 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
55 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
56 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
57 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
58 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
59 [TOMOYO_MAC_FILE_UMOUNT] = "file::unmount",
60 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
61 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
64 /* Permit policy management by non-root user? */
65 static bool tomoyo_manage_by_non_root;
67 /* Utility functions. */
70 * tomoyo_yesno - Return "yes" or "no".
74 static const char *tomoyo_yesno(const unsigned int value)
76 return value ? "yes" : "no";
79 static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
82 const int pos = strlen(buffer);
84 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
89 * tomoyo_flush - Flush queued string to userspace's buffer.
91 * @head: Pointer to "struct tomoyo_io_buffer".
93 * Returns true if all data was flushed, false otherwise.
95 static bool tomoyo_flush(struct tomoyo_io_buffer *head)
97 while (head->r.w_pos) {
98 const char *w = head->r.w[0];
101 if (len > head->read_user_buf_avail)
102 len = head->read_user_buf_avail;
105 if (copy_to_user(head->read_user_buf, w, len))
107 head->read_user_buf_avail -= len;
108 head->read_user_buf += len;
114 /* Add '\0' for query. */
116 if (!head->read_user_buf_avail ||
117 copy_to_user(head->read_user_buf, "", 1))
119 head->read_user_buf_avail--;
120 head->read_user_buf++;
123 for (len = 0; len < head->r.w_pos; len++)
124 head->r.w[len] = head->r.w[len + 1];
131 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
133 * @head: Pointer to "struct tomoyo_io_buffer".
134 * @string: String to print.
136 * Note that @string has to be kept valid until @head is kfree()d.
137 * This means that char[] allocated on stack memory cannot be passed to
138 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
140 static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
142 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
143 head->r.w[head->r.w_pos++] = string;
150 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
152 * @head: Pointer to "struct tomoyo_io_buffer".
153 * @fmt: The printf()'s format string, followed by parameters.
155 void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
159 int pos = head->r.avail;
160 int size = head->readbuf_size - pos;
164 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
166 if (pos + len >= head->readbuf_size) {
170 head->r.avail += len;
171 tomoyo_set_string(head, head->read_buf + pos);
175 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
177 * @head: Pointer to "struct tomoyo_io_buffer".
181 static void tomoyo_set_space(struct tomoyo_io_buffer *head)
183 tomoyo_set_string(head, " ");
187 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
189 * @head: Pointer to "struct tomoyo_io_buffer".
193 static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
195 tomoyo_set_string(head, "\n");
196 return !head->r.w_pos;
200 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
202 * @head: Pointer to "struct tomoyo_io_buffer".
206 static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
208 tomoyo_set_string(head, "/");
212 * tomoyo_print_name_union - Print a tomoyo_name_union.
214 * @head: Pointer to "struct tomoyo_io_buffer".
215 * @ptr: Pointer to "struct tomoyo_name_union".
217 static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
218 const struct tomoyo_name_union *ptr)
220 tomoyo_set_space(head);
222 tomoyo_set_string(head, "@");
223 tomoyo_set_string(head, ptr->group->group_name->name);
225 tomoyo_set_string(head, ptr->filename->name);
230 * tomoyo_print_number_union - Print a tomoyo_number_union.
232 * @head: Pointer to "struct tomoyo_io_buffer".
233 * @ptr: Pointer to "struct tomoyo_number_union".
235 static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
236 const struct tomoyo_number_union *ptr)
238 tomoyo_set_space(head);
240 tomoyo_set_string(head, "@");
241 tomoyo_set_string(head, ptr->group->group_name->name);
244 unsigned long min = ptr->values[0];
245 const unsigned long max = ptr->values[1];
246 u8 min_type = ptr->value_type[0];
247 const u8 max_type = ptr->value_type[1];
250 for (i = 0; i < 2; i++) {
252 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
253 tomoyo_addprintf(buffer, sizeof(buffer),
256 case TOMOYO_VALUE_TYPE_OCTAL:
257 tomoyo_addprintf(buffer, sizeof(buffer),
261 tomoyo_addprintf(buffer, sizeof(buffer),
265 if (min == max && min_type == max_type)
267 tomoyo_addprintf(buffer, sizeof(buffer), "-");
271 tomoyo_io_printf(head, "%s", buffer);
276 * tomoyo_assign_profile - Create a new profile.
278 * @profile: Profile number to create.
280 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
282 static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
284 struct tomoyo_profile *ptr;
285 struct tomoyo_profile *entry;
286 if (profile >= TOMOYO_MAX_PROFILES)
288 ptr = tomoyo_profile_ptr[profile];
291 entry = kzalloc(sizeof(*entry), GFP_NOFS);
292 if (mutex_lock_interruptible(&tomoyo_policy_lock))
294 ptr = tomoyo_profile_ptr[profile];
295 if (!ptr && tomoyo_memory_ok(entry)) {
297 ptr->learning = &tomoyo_default_profile.preference;
298 ptr->permissive = &tomoyo_default_profile.preference;
299 ptr->enforcing = &tomoyo_default_profile.preference;
300 ptr->default_config = TOMOYO_CONFIG_DISABLED;
301 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
302 sizeof(ptr->config));
303 mb(); /* Avoid out-of-order execution. */
304 tomoyo_profile_ptr[profile] = ptr;
307 mutex_unlock(&tomoyo_policy_lock);
314 * tomoyo_profile - Find a profile.
316 * @profile: Profile number to find.
318 * Returns pointer to "struct tomoyo_profile".
320 struct tomoyo_profile *tomoyo_profile(const u8 profile)
322 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
323 if (!tomoyo_policy_loaded)
324 return &tomoyo_default_profile;
329 static s8 tomoyo_find_yesno(const char *string, const char *find)
331 const char *cp = strstr(string, find);
334 if (!strncmp(cp, "=yes", 4))
336 else if (!strncmp(cp, "=no", 3))
342 static void tomoyo_set_bool(bool *b, const char *string, const char *find)
344 switch (tomoyo_find_yesno(string, find)) {
354 static void tomoyo_set_uint(unsigned int *i, const char *string,
357 const char *cp = strstr(string, find);
359 sscanf(cp + strlen(find), "=%u", i);
362 static void tomoyo_set_pref(const char *name, const char *value,
363 const bool use_default,
364 struct tomoyo_profile *profile)
366 struct tomoyo_preference **pref;
368 if (!strcmp(name, "enforcing")) {
370 pref = &profile->enforcing;
373 profile->enforcing = &profile->preference;
374 verbose = &profile->preference.enforcing_verbose;
377 if (!strcmp(name, "permissive")) {
379 pref = &profile->permissive;
382 profile->permissive = &profile->preference;
383 verbose = &profile->preference.permissive_verbose;
386 if (!strcmp(name, "learning")) {
388 pref = &profile->learning;
391 profile->learning = &profile->preference;
392 tomoyo_set_uint(&profile->preference.learning_max_entry, value,
394 verbose = &profile->preference.learning_verbose;
399 *pref = &tomoyo_default_profile.preference;
402 tomoyo_set_bool(verbose, value, "verbose");
405 static int tomoyo_set_mode(char *name, const char *value,
406 const bool use_default,
407 struct tomoyo_profile *profile)
411 if (!strcmp(name, "CONFIG")) {
412 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
413 config = profile->default_config;
414 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
416 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
417 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
418 if (strcmp(name, tomoyo_mac_keywords[i]))
420 config = profile->config[i];
423 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
429 config = TOMOYO_CONFIG_USE_DEFAULT;
432 for (mode = 0; mode < 4; mode++)
433 if (strstr(value, tomoyo_mode[mode]))
435 * Update lower 3 bits in order to distinguish
436 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
438 config = (config & ~7) | mode;
440 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
441 profile->config[i] = config;
442 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
443 profile->default_config = config;
448 * tomoyo_write_profile - Write profile table.
450 * @head: Pointer to "struct tomoyo_io_buffer".
452 * Returns 0 on success, negative value otherwise.
454 static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
456 char *data = head->write_buf;
458 bool use_default = false;
460 struct tomoyo_profile *profile;
461 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
463 i = simple_strtoul(data, &cp, 10);
465 profile = &tomoyo_default_profile;
470 profile = tomoyo_assign_profile(i);
474 cp = strchr(data, '=');
478 if (profile != &tomoyo_default_profile)
479 use_default = strstr(cp, "use_default") != NULL;
480 if (tomoyo_str_starts(&data, "PREFERENCE::")) {
481 tomoyo_set_pref(data, cp, use_default, profile);
484 if (profile == &tomoyo_default_profile)
486 if (!strcmp(data, "COMMENT")) {
487 static DEFINE_SPINLOCK(lock);
488 const struct tomoyo_path_info *new_comment
489 = tomoyo_get_name(cp);
490 const struct tomoyo_path_info *old_comment;
494 old_comment = profile->comment;
495 profile->comment = new_comment;
497 tomoyo_put_name(old_comment);
500 return tomoyo_set_mode(data, cp, use_default, profile);
503 static void tomoyo_print_preference(struct tomoyo_io_buffer *head,
506 struct tomoyo_preference *pref = &tomoyo_default_profile.preference;
507 const struct tomoyo_profile *profile = idx >= 0 ?
508 tomoyo_profile_ptr[idx] : NULL;
509 char buffer[16] = "";
511 buffer[sizeof(buffer) - 1] = '\0';
512 snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
515 pref = profile->learning;
516 if (pref == &tomoyo_default_profile.preference)
519 tomoyo_io_printf(head, "%sPREFERENCE::%s={ "
520 "verbose=%s max_entry=%u }\n",
522 tomoyo_yesno(pref->learning_verbose),
523 pref->learning_max_entry);
526 pref = profile->permissive;
527 if (pref == &tomoyo_default_profile.preference)
530 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
531 buffer, "permissive",
532 tomoyo_yesno(pref->permissive_verbose));
535 pref = profile->enforcing;
536 if (pref == &tomoyo_default_profile.preference)
539 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
541 tomoyo_yesno(pref->enforcing_verbose));
544 static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
546 tomoyo_io_printf(head, "={ mode=%s }\n", tomoyo_mode[config & 3]);
550 * tomoyo_read_profile - Read profile table.
552 * @head: Pointer to "struct tomoyo_io_buffer".
554 static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
557 const struct tomoyo_profile *profile;
559 index = head->r.index;
560 profile = tomoyo_profile_ptr[index];
561 switch (head->r.step) {
563 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
564 tomoyo_print_preference(head, -1);
568 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
570 if (tomoyo_profile_ptr[head->r.index])
572 if (head->r.index == TOMOYO_MAX_PROFILES)
578 const struct tomoyo_path_info *comment =
580 tomoyo_io_printf(head, "%u-COMMENT=", index);
581 tomoyo_set_string(head, comment ? comment->name : "");
588 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
589 tomoyo_print_config(head, profile->default_config);
595 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
596 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
597 const u8 i = head->r.bit;
598 const u8 config = profile->config[i];
599 if (config == TOMOYO_CONFIG_USE_DEFAULT)
601 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
602 tomoyo_mac_keywords[i]);
603 tomoyo_print_config(head, config);
607 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
608 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
609 tomoyo_print_preference(head, index);
615 if (tomoyo_flush(head))
619 static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
620 const struct tomoyo_acl_head *b)
622 return container_of(a, struct tomoyo_manager, head)->manager ==
623 container_of(b, struct tomoyo_manager, head)->manager;
627 * tomoyo_update_manager_entry - Add a manager entry.
629 * @manager: The path to manager or the domainnamme.
630 * @is_delete: True if it is a delete request.
632 * Returns 0 on success, negative value otherwise.
634 * Caller holds tomoyo_read_lock().
636 static int tomoyo_update_manager_entry(const char *manager,
637 const bool is_delete)
639 struct tomoyo_manager e = { };
640 struct tomoyo_acl_param param = {
641 .is_delete = is_delete,
642 .list = &tomoyo_policy_list[TOMOYO_ID_MANAGER],
644 int error = is_delete ? -ENOENT : -ENOMEM;
645 if (tomoyo_domain_def(manager)) {
646 if (!tomoyo_correct_domain(manager))
650 if (!tomoyo_correct_path(manager))
653 e.manager = tomoyo_get_name(manager);
655 error = tomoyo_update_policy(&e.head, sizeof(e), ¶m,
656 tomoyo_same_manager);
657 tomoyo_put_name(e.manager);
663 * tomoyo_write_manager - Write manager policy.
665 * @head: Pointer to "struct tomoyo_io_buffer".
667 * Returns 0 on success, negative value otherwise.
669 * Caller holds tomoyo_read_lock().
671 static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
673 char *data = head->write_buf;
674 bool is_delete = tomoyo_str_starts(&data, "delete ");
676 if (!strcmp(data, "manage_by_non_root")) {
677 tomoyo_manage_by_non_root = !is_delete;
680 return tomoyo_update_manager_entry(data, is_delete);
684 * tomoyo_read_manager - Read manager policy.
686 * @head: Pointer to "struct tomoyo_io_buffer".
688 * Caller holds tomoyo_read_lock().
690 static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
694 list_for_each_cookie(head->r.acl,
695 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
696 struct tomoyo_manager *ptr =
697 list_entry(head->r.acl, typeof(*ptr), head.list);
698 if (ptr->head.is_deleted)
700 if (!tomoyo_flush(head))
702 tomoyo_set_string(head, ptr->manager->name);
709 * tomoyo_manager - Check whether the current process is a policy manager.
711 * Returns true if the current process is permitted to modify policy
712 * via /sys/kernel/security/tomoyo/ interface.
714 * Caller holds tomoyo_read_lock().
716 static bool tomoyo_manager(void)
718 struct tomoyo_manager *ptr;
720 const struct task_struct *task = current;
721 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
724 if (!tomoyo_policy_loaded)
726 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
728 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
730 if (!ptr->head.is_deleted && ptr->is_domain
731 && !tomoyo_pathcmp(domainname, ptr->manager)) {
738 exe = tomoyo_get_exe();
741 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
743 if (!ptr->head.is_deleted && !ptr->is_domain
744 && !strcmp(exe, ptr->manager->name)) {
749 if (!found) { /* Reduce error messages. */
750 static pid_t last_pid;
751 const pid_t pid = current->pid;
752 if (last_pid != pid) {
753 printk(KERN_WARNING "%s ( %s ) is not permitted to "
754 "update policies.\n", domainname->name, exe);
763 * tomoyo_select_one - Parse select command.
765 * @head: Pointer to "struct tomoyo_io_buffer".
766 * @data: String to parse.
768 * Returns true on success, false otherwise.
770 * Caller holds tomoyo_read_lock().
772 static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
775 struct tomoyo_domain_info *domain = NULL;
776 bool global_pid = false;
778 if (!strcmp(data, "allow_execute")) {
779 head->r.print_execute_only = true;
782 if (sscanf(data, "pid=%u", &pid) == 1 ||
783 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
784 struct task_struct *p;
786 read_lock(&tasklist_lock);
788 p = find_task_by_pid_ns(pid, &init_pid_ns);
790 p = find_task_by_vpid(pid);
792 domain = tomoyo_real_domain(p);
793 read_unlock(&tasklist_lock);
795 } else if (!strncmp(data, "domain=", 7)) {
796 if (tomoyo_domain_def(data + 7))
797 domain = tomoyo_find_domain(data + 7);
800 head->w.domain = domain;
801 /* Accessing read_buf is safe because head->io_sem is held. */
803 return true; /* Do nothing if open(O_WRONLY). */
804 memset(&head->r, 0, sizeof(head->r));
805 head->r.print_this_domain_only = true;
807 head->r.domain = &domain->list;
810 tomoyo_io_printf(head, "# select %s\n", data);
811 if (domain && domain->is_deleted)
812 tomoyo_io_printf(head, "# This is a deleted domain.\n");
817 * tomoyo_delete_domain - Delete a domain.
819 * @domainname: The name of domain.
823 * Caller holds tomoyo_read_lock().
825 static int tomoyo_delete_domain(char *domainname)
827 struct tomoyo_domain_info *domain;
828 struct tomoyo_path_info name;
830 name.name = domainname;
831 tomoyo_fill_path_info(&name);
832 if (mutex_lock_interruptible(&tomoyo_policy_lock))
834 /* Is there an active domain? */
835 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
836 /* Never delete tomoyo_kernel_domain */
837 if (domain == &tomoyo_kernel_domain)
839 if (domain->is_deleted ||
840 tomoyo_pathcmp(domain->domainname, &name))
842 domain->is_deleted = true;
845 mutex_unlock(&tomoyo_policy_lock);
850 * tomoyo_write_domain2 - Write domain policy.
852 * @list: Pointer to "struct list_head".
853 * @data: Policy to be interpreted.
854 * @is_delete: True if it is a delete request.
856 * Returns 0 on success, negative value otherwise.
858 * Caller holds tomoyo_read_lock().
860 static int tomoyo_write_domain2(struct list_head *list, char *data,
861 const bool is_delete)
863 struct tomoyo_acl_param param = {
866 .is_delete = is_delete,
868 static const struct {
870 int (*write) (struct tomoyo_acl_param *);
871 } tomoyo_callback[1] = {
872 { "file ", tomoyo_write_file },
875 for (i = 0; i < 1; i++) {
876 if (!tomoyo_str_starts(¶m.data,
877 tomoyo_callback[i].keyword))
879 return tomoyo_callback[i].write(¶m);
885 * tomoyo_write_domain - Write domain policy.
887 * @head: Pointer to "struct tomoyo_io_buffer".
889 * Returns 0 on success, negative value otherwise.
891 * Caller holds tomoyo_read_lock().
893 static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
895 char *data = head->write_buf;
896 struct tomoyo_domain_info *domain = head->w.domain;
897 bool is_delete = false;
898 bool is_select = false;
899 unsigned int profile;
901 if (tomoyo_str_starts(&data, "delete "))
903 else if (tomoyo_str_starts(&data, "select "))
905 if (is_select && tomoyo_select_one(head, data))
907 /* Don't allow updating policies by non manager programs. */
908 if (!tomoyo_manager())
910 if (tomoyo_domain_def(data)) {
913 tomoyo_delete_domain(data);
915 domain = tomoyo_find_domain(data);
917 domain = tomoyo_assign_domain(data, 0);
918 head->w.domain = domain;
924 if (sscanf(data, "use_profile %u", &profile) == 1
925 && profile < TOMOYO_MAX_PROFILES) {
926 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
927 domain->profile = (u8) profile;
930 if (!strcmp(data, "quota_exceeded")) {
931 domain->quota_warned = !is_delete;
934 if (!strcmp(data, "transition_failed")) {
935 domain->transition_failed = !is_delete;
938 return tomoyo_write_domain2(&domain->acl_info_list, data, is_delete);
942 * tomoyo_set_group - Print category name.
944 * @head: Pointer to "struct tomoyo_io_buffer".
945 * @category: Category name.
949 static void tomoyo_set_group(struct tomoyo_io_buffer *head,
950 const char *category)
952 tomoyo_set_string(head, category);
956 * tomoyo_print_entry - Print an ACL entry.
958 * @head: Pointer to "struct tomoyo_io_buffer".
959 * @acl: Pointer to an ACL entry.
961 * Returns true on success, false otherwise.
963 static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
964 struct tomoyo_acl_info *acl)
966 const u8 acl_type = acl->type;
972 if (!tomoyo_flush(head))
974 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
975 struct tomoyo_path_acl *ptr =
976 container_of(acl, typeof(*ptr), head);
977 const u16 perm = ptr->perm;
978 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
979 if (!(perm & (1 << bit)))
981 if (head->r.print_execute_only &&
982 bit != TOMOYO_TYPE_EXECUTE)
985 tomoyo_set_group(head, "file ");
988 tomoyo_set_slash(head);
990 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
994 tomoyo_print_name_union(head, &ptr->name);
995 } else if (head->r.print_execute_only) {
997 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
998 struct tomoyo_path2_acl *ptr =
999 container_of(acl, typeof(*ptr), head);
1000 const u8 perm = ptr->perm;
1001 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
1002 if (!(perm & (1 << bit)))
1005 tomoyo_set_group(head, "file ");
1008 tomoyo_set_slash(head);
1010 tomoyo_set_string(head, tomoyo_mac_keywords
1011 [tomoyo_pp2mac[bit]]);
1015 tomoyo_print_name_union(head, &ptr->name1);
1016 tomoyo_print_name_union(head, &ptr->name2);
1017 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1018 struct tomoyo_path_number_acl *ptr =
1019 container_of(acl, typeof(*ptr), head);
1020 const u8 perm = ptr->perm;
1021 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
1022 if (!(perm & (1 << bit)))
1025 tomoyo_set_group(head, "file ");
1028 tomoyo_set_slash(head);
1030 tomoyo_set_string(head, tomoyo_mac_keywords
1031 [tomoyo_pn2mac[bit]]);
1035 tomoyo_print_name_union(head, &ptr->name);
1036 tomoyo_print_number_union(head, &ptr->number);
1037 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1038 struct tomoyo_mkdev_acl *ptr =
1039 container_of(acl, typeof(*ptr), head);
1040 const u8 perm = ptr->perm;
1041 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
1042 if (!(perm & (1 << bit)))
1045 tomoyo_set_group(head, "file ");
1048 tomoyo_set_slash(head);
1050 tomoyo_set_string(head, tomoyo_mac_keywords
1051 [tomoyo_pnnn2mac[bit]]);
1055 tomoyo_print_name_union(head, &ptr->name);
1056 tomoyo_print_number_union(head, &ptr->mode);
1057 tomoyo_print_number_union(head, &ptr->major);
1058 tomoyo_print_number_union(head, &ptr->minor);
1059 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1060 struct tomoyo_mount_acl *ptr =
1061 container_of(acl, typeof(*ptr), head);
1062 tomoyo_set_group(head, "file mount");
1063 tomoyo_print_name_union(head, &ptr->dev_name);
1064 tomoyo_print_name_union(head, &ptr->dir_name);
1065 tomoyo_print_name_union(head, &ptr->fs_type);
1066 tomoyo_print_number_union(head, &ptr->flags);
1068 tomoyo_set_lf(head);
1073 * tomoyo_read_domain2 - Read domain policy.
1075 * @head: Pointer to "struct tomoyo_io_buffer".
1076 * @domain: Pointer to "struct tomoyo_domain_info".
1078 * Caller holds tomoyo_read_lock().
1080 * Returns true on success, false otherwise.
1082 static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1083 struct tomoyo_domain_info *domain)
1085 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1086 struct tomoyo_acl_info *ptr =
1087 list_entry(head->r.acl, typeof(*ptr), list);
1088 if (!tomoyo_print_entry(head, ptr))
1096 * tomoyo_read_domain - Read domain policy.
1098 * @head: Pointer to "struct tomoyo_io_buffer".
1100 * Caller holds tomoyo_read_lock().
1102 static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
1106 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1107 struct tomoyo_domain_info *domain =
1108 list_entry(head->r.domain, typeof(*domain), list);
1109 switch (head->r.step) {
1111 if (domain->is_deleted &&
1112 !head->r.print_this_domain_only)
1114 /* Print domainname and flags. */
1115 tomoyo_set_string(head, domain->domainname->name);
1116 tomoyo_set_lf(head);
1117 tomoyo_io_printf(head, "use_profile %u\n",
1119 if (domain->quota_warned)
1120 tomoyo_set_string(head, "quota_exceeded\n");
1121 if (domain->transition_failed)
1122 tomoyo_set_string(head, "transition_failed\n");
1124 tomoyo_set_lf(head);
1127 if (!tomoyo_read_domain2(head, domain))
1130 if (!tomoyo_set_lf(head))
1135 if (head->r.print_this_domain_only)
1144 * tomoyo_write_domain_profile - Assign profile for specified domain.
1146 * @head: Pointer to "struct tomoyo_io_buffer".
1148 * Returns 0 on success, -EINVAL otherwise.
1150 * This is equivalent to doing
1152 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1153 * /usr/sbin/tomoyo-loadpolicy -d
1155 * Caller holds tomoyo_read_lock().
1157 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1159 char *data = head->write_buf;
1160 char *cp = strchr(data, ' ');
1161 struct tomoyo_domain_info *domain;
1162 unsigned long profile;
1167 domain = tomoyo_find_domain(cp + 1);
1168 if (strict_strtoul(data, 10, &profile))
1170 if (domain && profile < TOMOYO_MAX_PROFILES
1171 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1172 domain->profile = (u8) profile;
1177 * tomoyo_read_domain_profile - Read only domainname and profile.
1179 * @head: Pointer to "struct tomoyo_io_buffer".
1181 * Returns list of profile number and domainname pairs.
1183 * This is equivalent to doing
1185 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1186 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1187 * domainname = $0; } else if ( $1 == "use_profile" ) {
1188 * print $2 " " domainname; domainname = ""; } } ; '
1190 * Caller holds tomoyo_read_lock().
1192 static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1196 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1197 struct tomoyo_domain_info *domain =
1198 list_entry(head->r.domain, typeof(*domain), list);
1199 if (domain->is_deleted)
1201 if (!tomoyo_flush(head))
1203 tomoyo_io_printf(head, "%u ", domain->profile);
1204 tomoyo_set_string(head, domain->domainname->name);
1205 tomoyo_set_lf(head);
1211 * tomoyo_write_pid: Specify PID to obtain domainname.
1213 * @head: Pointer to "struct tomoyo_io_buffer".
1217 static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1219 head->r.eof = false;
1224 * tomoyo_read_pid - Get domainname of the specified PID.
1226 * @head: Pointer to "struct tomoyo_io_buffer".
1228 * Returns the domainname which the specified PID is in on success,
1229 * empty string otherwise.
1230 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1231 * using read()/write() interface rather than sysctl() interface.
1233 static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
1235 char *buf = head->write_buf;
1236 bool global_pid = false;
1238 struct task_struct *p;
1239 struct tomoyo_domain_info *domain = NULL;
1241 /* Accessing write_buf is safe because head->io_sem is held. */
1244 return; /* Do nothing if open(O_RDONLY). */
1246 if (head->r.w_pos || head->r.eof)
1249 if (tomoyo_str_starts(&buf, "global-pid "))
1251 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1253 read_lock(&tasklist_lock);
1255 p = find_task_by_pid_ns(pid, &init_pid_ns);
1257 p = find_task_by_vpid(pid);
1259 domain = tomoyo_real_domain(p);
1260 read_unlock(&tasklist_lock);
1264 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1265 tomoyo_set_string(head, domain->domainname->name);
1268 static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1269 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain",
1270 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain",
1271 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain",
1272 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain",
1275 static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1276 [TOMOYO_PATH_GROUP] = "path_group ",
1277 [TOMOYO_NUMBER_GROUP] = "number_group ",
1281 * tomoyo_write_exception - Write exception policy.
1283 * @head: Pointer to "struct tomoyo_io_buffer".
1285 * Returns 0 on success, negative value otherwise.
1287 * Caller holds tomoyo_read_lock().
1289 static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
1291 struct tomoyo_acl_param param = {
1292 .data = head->write_buf,
1295 param.is_delete = tomoyo_str_starts(¶m.data, "delete ");
1296 if (tomoyo_str_starts(¶m.data, "aggregator "))
1297 return tomoyo_write_aggregator(¶m);
1298 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1299 if (tomoyo_str_starts(¶m.data, tomoyo_transition_type[i]))
1300 return tomoyo_write_transition_control(¶m, i);
1301 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1302 if (tomoyo_str_starts(¶m.data, tomoyo_group_name[i]))
1303 return tomoyo_write_group(¶m, i);
1308 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1310 * @head: Pointer to "struct tomoyo_io_buffer".
1311 * @idx: Index number.
1313 * Returns true on success, false otherwise.
1315 * Caller holds tomoyo_read_lock().
1317 static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1319 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
1320 struct tomoyo_group *group =
1321 list_entry(head->r.group, typeof(*group), head.list);
1322 list_for_each_cookie(head->r.acl, &group->member_list) {
1323 struct tomoyo_acl_head *ptr =
1324 list_entry(head->r.acl, typeof(*ptr), list);
1325 if (ptr->is_deleted)
1327 if (!tomoyo_flush(head))
1329 tomoyo_set_string(head, tomoyo_group_name[idx]);
1330 tomoyo_set_string(head, group->group_name->name);
1331 if (idx == TOMOYO_PATH_GROUP) {
1332 tomoyo_set_space(head);
1333 tomoyo_set_string(head, container_of
1334 (ptr, struct tomoyo_path_group,
1335 head)->member_name->name);
1336 } else if (idx == TOMOYO_NUMBER_GROUP) {
1337 tomoyo_print_number_union(head, &container_of
1339 struct tomoyo_number_group,
1342 tomoyo_set_lf(head);
1346 head->r.group = NULL;
1351 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1353 * @head: Pointer to "struct tomoyo_io_buffer".
1354 * @idx: Index number.
1356 * Returns true on success, false otherwise.
1358 * Caller holds tomoyo_read_lock().
1360 static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1362 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
1363 struct tomoyo_acl_head *acl =
1364 container_of(head->r.acl, typeof(*acl), list);
1365 if (acl->is_deleted)
1367 if (!tomoyo_flush(head))
1370 case TOMOYO_ID_TRANSITION_CONTROL:
1372 struct tomoyo_transition_control *ptr =
1373 container_of(acl, typeof(*ptr), head);
1374 tomoyo_set_string(head, tomoyo_transition_type
1376 tomoyo_set_string(head, ptr->program ?
1377 ptr->program->name : "any");
1378 tomoyo_set_string(head, " from ");
1379 tomoyo_set_string(head, ptr->domainname ?
1380 ptr->domainname->name :
1384 case TOMOYO_ID_AGGREGATOR:
1386 struct tomoyo_aggregator *ptr =
1387 container_of(acl, typeof(*ptr), head);
1388 tomoyo_set_string(head, "aggregator ");
1389 tomoyo_set_string(head,
1390 ptr->original_name->name);
1391 tomoyo_set_space(head);
1392 tomoyo_set_string(head,
1393 ptr->aggregated_name->name);
1399 tomoyo_set_lf(head);
1406 * tomoyo_read_exception - Read exception policy.
1408 * @head: Pointer to "struct tomoyo_io_buffer".
1410 * Caller holds tomoyo_read_lock().
1412 static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
1416 while (head->r.step < TOMOYO_MAX_POLICY &&
1417 tomoyo_read_policy(head, head->r.step))
1419 if (head->r.step < TOMOYO_MAX_POLICY)
1421 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1422 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1424 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1430 * tomoyo_print_header - Get header line of audit log.
1432 * @r: Pointer to "struct tomoyo_request_info".
1434 * Returns string representation.
1436 * This function uses kmalloc(), so caller must kfree() if this function
1437 * didn't return NULL.
1439 static char *tomoyo_print_header(struct tomoyo_request_info *r)
1442 const pid_t gpid = task_pid_nr(current);
1443 static const int tomoyo_buffer_len = 4096;
1444 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1448 do_gettimeofday(&tv);
1450 ppid = task_tgid_vnr(current->real_parent);
1452 snprintf(buffer, tomoyo_buffer_len - 1,
1453 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1454 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1455 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1456 tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
1457 task_tgid_vnr(current), ppid,
1458 current_uid(), current_gid(), current_euid(),
1459 current_egid(), current_suid(), current_sgid(),
1460 current_fsuid(), current_fsgid());
1465 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1467 * @len: Required size.
1468 * @r: Pointer to "struct tomoyo_request_info".
1470 * Returns pointer to allocated memory.
1472 * The @len is updated to add the header lines' size on success.
1474 * This function uses kzalloc(), so caller must kfree() if this function
1475 * didn't return NULL.
1477 static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1481 const char *domainname;
1483 r->domain = tomoyo_domain();
1484 domainname = r->domain->domainname->name;
1485 header = tomoyo_print_header(r);
1488 *len += strlen(domainname) + strlen(header) + 10;
1489 buf = kzalloc(*len, GFP_NOFS);
1491 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1496 /* Wait queue for tomoyo_query_list. */
1497 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1499 /* Lock for manipulating tomoyo_query_list. */
1500 static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1502 /* Structure for query. */
1503 struct tomoyo_query {
1504 struct list_head list;
1507 unsigned int serial;
1512 /* The list for "struct tomoyo_query". */
1513 static LIST_HEAD(tomoyo_query_list);
1516 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1519 static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1522 * tomoyo_supervisor - Ask for the supervisor's decision.
1524 * @r: Pointer to "struct tomoyo_request_info".
1525 * @fmt: The printf()'s format string, followed by parameters.
1527 * Returns 0 if the supervisor decided to permit the access request which
1528 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1529 * supervisor decided to retry the access request which violated the policy in
1530 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1532 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1538 static unsigned int tomoyo_serial;
1539 struct tomoyo_query *entry = NULL;
1540 bool quota_exceeded = false;
1544 case TOMOYO_CONFIG_LEARNING:
1545 if (!tomoyo_domain_quota_is_ok(r))
1547 va_start(args, fmt);
1548 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1550 buffer = kmalloc(len, GFP_NOFS);
1553 va_start(args, fmt);
1554 vsnprintf(buffer, len - 1, fmt, args);
1556 tomoyo_normalize_line(buffer);
1557 tomoyo_write_domain2(&r->domain->acl_info_list, buffer, false);
1560 case TOMOYO_CONFIG_PERMISSIVE:
1564 r->domain = tomoyo_domain();
1565 if (!atomic_read(&tomoyo_query_observers))
1567 va_start(args, fmt);
1568 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1570 header = tomoyo_init_audit_log(&len, r);
1573 entry = kzalloc(sizeof(*entry), GFP_NOFS);
1576 entry->query = kzalloc(len, GFP_NOFS);
1579 len = ksize(entry->query);
1580 spin_lock(&tomoyo_query_list_lock);
1581 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
1582 sizeof(*entry) >= tomoyo_quota_for_query) {
1583 quota_exceeded = true;
1585 tomoyo_query_memory_size += len + sizeof(*entry);
1586 entry->serial = tomoyo_serial++;
1588 spin_unlock(&tomoyo_query_list_lock);
1591 pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1592 entry->serial, r->retry, header);
1595 va_start(args, fmt);
1596 vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1597 entry->query_len = strlen(entry->query) + 1;
1599 spin_lock(&tomoyo_query_list_lock);
1600 list_add_tail(&entry->list, &tomoyo_query_list);
1601 spin_unlock(&tomoyo_query_list_lock);
1602 /* Give 10 seconds for supervisor's opinion. */
1603 for (entry->timer = 0;
1604 atomic_read(&tomoyo_query_observers) && entry->timer < 100;
1606 wake_up(&tomoyo_query_wait);
1607 set_current_state(TASK_INTERRUPTIBLE);
1608 schedule_timeout(HZ / 10);
1612 spin_lock(&tomoyo_query_list_lock);
1613 list_del(&entry->list);
1614 tomoyo_query_memory_size -= len + sizeof(*entry);
1615 spin_unlock(&tomoyo_query_list_lock);
1616 switch (entry->answer) {
1617 case 3: /* Asked to retry by administrator. */
1618 error = TOMOYO_RETRY_REQUEST;
1622 /* Granted by administrator. */
1629 /* Rejected by administrator. */
1634 kfree(entry->query);
1641 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1643 * @file: Pointer to "struct file".
1644 * @wait: Pointer to "poll_table".
1646 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1648 * Waits for access requests which violated policy in enforcing mode.
1650 static int tomoyo_poll_query(struct file *file, poll_table *wait)
1652 struct list_head *tmp;
1655 for (i = 0; i < 2; i++) {
1656 spin_lock(&tomoyo_query_list_lock);
1657 list_for_each(tmp, &tomoyo_query_list) {
1658 struct tomoyo_query *ptr =
1659 list_entry(tmp, typeof(*ptr), list);
1665 spin_unlock(&tomoyo_query_list_lock);
1667 return POLLIN | POLLRDNORM;
1670 poll_wait(file, &tomoyo_query_wait, wait);
1676 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1678 * @head: Pointer to "struct tomoyo_io_buffer".
1680 static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1682 struct list_head *tmp;
1688 if (head->read_buf) {
1689 kfree(head->read_buf);
1690 head->read_buf = NULL;
1692 spin_lock(&tomoyo_query_list_lock);
1693 list_for_each(tmp, &tomoyo_query_list) {
1694 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1697 if (pos++ != head->r.query_index)
1699 len = ptr->query_len;
1702 spin_unlock(&tomoyo_query_list_lock);
1704 head->r.query_index = 0;
1707 buf = kzalloc(len, GFP_NOFS);
1711 spin_lock(&tomoyo_query_list_lock);
1712 list_for_each(tmp, &tomoyo_query_list) {
1713 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1716 if (pos++ != head->r.query_index)
1719 * Some query can be skipped because tomoyo_query_list
1720 * can change, but I don't care.
1722 if (len == ptr->query_len)
1723 memmove(buf, ptr->query, len);
1726 spin_unlock(&tomoyo_query_list_lock);
1728 head->read_buf = buf;
1729 head->r.w[head->r.w_pos++] = buf;
1730 head->r.query_index++;
1737 * tomoyo_write_answer - Write the supervisor's decision.
1739 * @head: Pointer to "struct tomoyo_io_buffer".
1741 * Returns 0 on success, -EINVAL otherwise.
1743 static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1745 char *data = head->write_buf;
1746 struct list_head *tmp;
1747 unsigned int serial;
1748 unsigned int answer;
1749 spin_lock(&tomoyo_query_list_lock);
1750 list_for_each(tmp, &tomoyo_query_list) {
1751 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1754 spin_unlock(&tomoyo_query_list_lock);
1755 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1757 spin_lock(&tomoyo_query_list_lock);
1758 list_for_each(tmp, &tomoyo_query_list) {
1759 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1760 if (ptr->serial != serial)
1763 ptr->answer = answer;
1766 spin_unlock(&tomoyo_query_list_lock);
1771 * tomoyo_read_version: Get version.
1773 * @head: Pointer to "struct tomoyo_io_buffer".
1775 * Returns version information.
1777 static void tomoyo_read_version(struct tomoyo_io_buffer *head)
1780 tomoyo_io_printf(head, "2.3.0");
1786 * tomoyo_read_self_domain - Get the current process's domainname.
1788 * @head: Pointer to "struct tomoyo_io_buffer".
1790 * Returns the current process's domainname.
1792 static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1796 * tomoyo_domain()->domainname != NULL
1797 * because every process belongs to a domain and
1798 * the domain's name cannot be NULL.
1800 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1806 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1808 * @type: Type of interface.
1809 * @file: Pointer to "struct file".
1811 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1813 * Caller acquires tomoyo_read_lock().
1815 int tomoyo_open_control(const u8 type, struct file *file)
1817 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
1821 mutex_init(&head->io_sem);
1824 case TOMOYO_DOMAINPOLICY:
1825 /* /sys/kernel/security/tomoyo/domain_policy */
1826 head->write = tomoyo_write_domain;
1827 head->read = tomoyo_read_domain;
1829 case TOMOYO_EXCEPTIONPOLICY:
1830 /* /sys/kernel/security/tomoyo/exception_policy */
1831 head->write = tomoyo_write_exception;
1832 head->read = tomoyo_read_exception;
1834 case TOMOYO_SELFDOMAIN:
1835 /* /sys/kernel/security/tomoyo/self_domain */
1836 head->read = tomoyo_read_self_domain;
1838 case TOMOYO_DOMAIN_STATUS:
1839 /* /sys/kernel/security/tomoyo/.domain_status */
1840 head->write = tomoyo_write_domain_profile;
1841 head->read = tomoyo_read_domain_profile;
1843 case TOMOYO_PROCESS_STATUS:
1844 /* /sys/kernel/security/tomoyo/.process_status */
1845 head->write = tomoyo_write_pid;
1846 head->read = tomoyo_read_pid;
1848 case TOMOYO_VERSION:
1849 /* /sys/kernel/security/tomoyo/version */
1850 head->read = tomoyo_read_version;
1851 head->readbuf_size = 128;
1853 case TOMOYO_MEMINFO:
1854 /* /sys/kernel/security/tomoyo/meminfo */
1855 head->write = tomoyo_write_memory_quota;
1856 head->read = tomoyo_read_memory_counter;
1857 head->readbuf_size = 512;
1859 case TOMOYO_PROFILE:
1860 /* /sys/kernel/security/tomoyo/profile */
1861 head->write = tomoyo_write_profile;
1862 head->read = tomoyo_read_profile;
1864 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1865 head->poll = tomoyo_poll_query;
1866 head->write = tomoyo_write_answer;
1867 head->read = tomoyo_read_query;
1869 case TOMOYO_MANAGER:
1870 /* /sys/kernel/security/tomoyo/manager */
1871 head->write = tomoyo_write_manager;
1872 head->read = tomoyo_read_manager;
1875 if (!(file->f_mode & FMODE_READ)) {
1877 * No need to allocate read_buf since it is not opened
1882 } else if (!head->poll) {
1883 /* Don't allocate read_buf for poll() access. */
1884 if (!head->readbuf_size)
1885 head->readbuf_size = 4096 * 2;
1886 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
1887 if (!head->read_buf) {
1892 if (!(file->f_mode & FMODE_WRITE)) {
1894 * No need to allocate write_buf since it is not opened
1898 } else if (head->write) {
1899 head->writebuf_size = 4096 * 2;
1900 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
1901 if (!head->write_buf) {
1902 kfree(head->read_buf);
1907 if (type != TOMOYO_QUERY)
1908 head->reader_idx = tomoyo_read_lock();
1909 file->private_data = head;
1911 * If the file is /sys/kernel/security/tomoyo/query , increment the
1913 * The obserber counter is used by tomoyo_supervisor() to see if
1914 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1916 if (type == TOMOYO_QUERY)
1917 atomic_inc(&tomoyo_query_observers);
1922 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1924 * @file: Pointer to "struct file".
1925 * @wait: Pointer to "poll_table".
1927 * Waits for read readiness.
1928 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1930 int tomoyo_poll_control(struct file *file, poll_table *wait)
1932 struct tomoyo_io_buffer *head = file->private_data;
1935 return head->poll(file, wait);
1939 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1941 * @head: Pointer to "struct tomoyo_io_buffer".
1942 * @buffer: Poiner to buffer to write to.
1943 * @buffer_len: Size of @buffer.
1945 * Returns bytes read on success, negative value otherwise.
1947 * Caller holds tomoyo_read_lock().
1949 int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
1950 const int buffer_len)
1956 if (mutex_lock_interruptible(&head->io_sem))
1958 head->read_user_buf = buffer;
1959 head->read_user_buf_avail = buffer_len;
1960 if (tomoyo_flush(head))
1961 /* Call the policy handler. */
1964 len = head->read_user_buf - buffer;
1965 mutex_unlock(&head->io_sem);
1970 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1972 * @head: Pointer to "struct tomoyo_io_buffer".
1973 * @buffer: Pointer to buffer to read from.
1974 * @buffer_len: Size of @buffer.
1976 * Returns @buffer_len on success, negative value otherwise.
1978 * Caller holds tomoyo_read_lock().
1980 int tomoyo_write_control(struct tomoyo_io_buffer *head,
1981 const char __user *buffer, const int buffer_len)
1983 int error = buffer_len;
1984 int avail_len = buffer_len;
1985 char *cp0 = head->write_buf;
1989 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1991 /* Don't allow updating policies by non manager programs. */
1992 if (head->write != tomoyo_write_pid &&
1993 head->write != tomoyo_write_domain && !tomoyo_manager())
1995 if (mutex_lock_interruptible(&head->io_sem))
1997 /* Read a line and dispatch it to the policy handler. */
1998 while (avail_len > 0) {
2000 if (head->w.avail >= head->writebuf_size - 1) {
2003 } else if (get_user(c, buffer)) {
2009 cp0[head->w.avail++] = c;
2012 cp0[head->w.avail - 1] = '\0';
2014 tomoyo_normalize_line(cp0);
2017 mutex_unlock(&head->io_sem);
2022 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2024 * @head: Pointer to "struct tomoyo_io_buffer".
2026 * Releases memory and returns 0.
2028 * Caller looses tomoyo_read_lock().
2030 int tomoyo_close_control(struct tomoyo_io_buffer *head)
2032 const bool is_write = !!head->write_buf;
2035 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2038 if (head->type == TOMOYO_QUERY)
2039 atomic_dec(&tomoyo_query_observers);
2041 tomoyo_read_unlock(head->reader_idx);
2042 /* Release memory used for policy I/O. */
2043 kfree(head->read_buf);
2044 head->read_buf = NULL;
2045 kfree(head->write_buf);
2046 head->write_buf = NULL;
2054 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
2056 void tomoyo_check_profile(void)
2058 struct tomoyo_domain_info *domain;
2059 const int idx = tomoyo_read_lock();
2060 tomoyo_policy_loaded = true;
2061 /* Check all profiles currently assigned to domains are defined. */
2062 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2063 const u8 profile = domain->profile;
2064 if (tomoyo_profile_ptr[profile])
2066 printk(KERN_ERR "You need to define profile %u before using it.\n",
2068 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2069 "for more information.\n");
2070 panic("Profile %u (used by '%s') not defined.\n",
2071 profile, domain->domainname->name);
2073 tomoyo_read_unlock(idx);
2074 if (tomoyo_profile_version != 20090903) {
2075 printk(KERN_ERR "You need to install userland programs for "
2076 "TOMOYO 2.3 and initialize policy configuration.\n");
2077 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2078 "for more information.\n");
2079 panic("Profile version %u is not supported.\n",
2080 tomoyo_profile_version);
2082 printk(KERN_INFO "TOMOYO: 2.3.0\n");
2083 printk(KERN_INFO "Mandatory Access Control activated.\n");