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 /* Profile version. Currently only 20090903 is defined. */
15 static unsigned int tomoyo_profile_version;
17 /* Profile table. Memory is allocated as needed. */
18 static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
20 /* String table for functionality that takes 4 modes. */
21 static const char *tomoyo_mode[4] = {
22 "disabled", "learning", "permissive", "enforcing"
25 /* String table for /sys/kernel/security/tomoyo/profile */
26 static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
27 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
28 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
29 [TOMOYO_MAC_FILE_OPEN] = "file::open",
30 [TOMOYO_MAC_FILE_CREATE] = "file::create",
31 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
32 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
33 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
34 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
35 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
36 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
37 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
38 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
39 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
40 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
41 [TOMOYO_MAC_FILE_LINK] = "file::link",
42 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
43 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
44 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
45 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
46 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
47 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
48 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
49 [TOMOYO_MAC_FILE_UMOUNT] = "file::unmount",
50 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
51 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
54 /* String table for PREFERENCE keyword. */
55 static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
56 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
59 /* Permit policy management by non-root user? */
60 static bool tomoyo_manage_by_non_root;
62 /* Utility functions. */
65 * tomoyo_yesno - Return "yes" or "no".
70 static const char *tomoyo_yesno(const unsigned int value)
72 return value ? "yes" : "no";
77 * tomoyo_addprintf - strncat()-like-snprintf().
79 * @buffer: Buffer to write to. Must be '\0'-terminated.
80 * @len: Size of @buffer.
81 * @fmt: The printf()'s format string, followed by parameters.
85 static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
88 const int pos = strlen(buffer);
90 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
95 * tomoyo_flush - Flush queued string to userspace's buffer.
97 * @head: Pointer to "struct tomoyo_io_buffer".
99 * Returns true if all data was flushed, false otherwise.
101 static bool tomoyo_flush(struct tomoyo_io_buffer *head)
103 while (head->r.w_pos) {
104 const char *w = head->r.w[0];
107 if (len > head->read_user_buf_avail)
108 len = head->read_user_buf_avail;
111 if (copy_to_user(head->read_user_buf, w, len))
113 head->read_user_buf_avail -= len;
114 head->read_user_buf += len;
120 /* Add '\0' for query. */
122 if (!head->read_user_buf_avail ||
123 copy_to_user(head->read_user_buf, "", 1))
125 head->read_user_buf_avail--;
126 head->read_user_buf++;
129 for (len = 0; len < head->r.w_pos; len++)
130 head->r.w[len] = head->r.w[len + 1];
137 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
139 * @head: Pointer to "struct tomoyo_io_buffer".
140 * @string: String to print.
142 * Note that @string has to be kept valid until @head is kfree()d.
143 * This means that char[] allocated on stack memory cannot be passed to
144 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
146 static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
148 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
149 head->r.w[head->r.w_pos++] = string;
156 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
158 * @head: Pointer to "struct tomoyo_io_buffer".
159 * @fmt: The printf()'s format string, followed by parameters.
161 void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
165 int pos = head->r.avail;
166 int size = head->readbuf_size - pos;
170 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
172 if (pos + len >= head->readbuf_size) {
176 head->r.avail += len;
177 tomoyo_set_string(head, head->read_buf + pos);
181 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
183 * @head: Pointer to "struct tomoyo_io_buffer".
187 static void tomoyo_set_space(struct tomoyo_io_buffer *head)
189 tomoyo_set_string(head, " ");
193 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
195 * @head: Pointer to "struct tomoyo_io_buffer".
199 static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
201 tomoyo_set_string(head, "\n");
202 return !head->r.w_pos;
206 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
208 * @head: Pointer to "struct tomoyo_io_buffer".
212 static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
214 tomoyo_set_string(head, "/");
218 * tomoyo_print_name_union - Print a tomoyo_name_union.
220 * @head: Pointer to "struct tomoyo_io_buffer".
221 * @ptr: Pointer to "struct tomoyo_name_union".
223 static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
224 const struct tomoyo_name_union *ptr)
226 tomoyo_set_space(head);
228 tomoyo_set_string(head, "@");
229 tomoyo_set_string(head, ptr->group->group_name->name);
231 tomoyo_set_string(head, ptr->filename->name);
236 * tomoyo_print_number_union - Print a tomoyo_number_union.
238 * @head: Pointer to "struct tomoyo_io_buffer".
239 * @ptr: Pointer to "struct tomoyo_number_union".
241 static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
242 const struct tomoyo_number_union *ptr)
244 tomoyo_set_space(head);
246 tomoyo_set_string(head, "@");
247 tomoyo_set_string(head, ptr->group->group_name->name);
250 unsigned long min = ptr->values[0];
251 const unsigned long max = ptr->values[1];
252 u8 min_type = ptr->value_type[0];
253 const u8 max_type = ptr->value_type[1];
256 for (i = 0; i < 2; i++) {
258 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
259 tomoyo_addprintf(buffer, sizeof(buffer),
262 case TOMOYO_VALUE_TYPE_OCTAL:
263 tomoyo_addprintf(buffer, sizeof(buffer),
267 tomoyo_addprintf(buffer, sizeof(buffer),
271 if (min == max && min_type == max_type)
273 tomoyo_addprintf(buffer, sizeof(buffer), "-");
277 tomoyo_io_printf(head, "%s", buffer);
282 * tomoyo_assign_profile - Create a new profile.
284 * @profile: Profile number to create.
286 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
288 static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
290 struct tomoyo_profile *ptr;
291 struct tomoyo_profile *entry;
292 if (profile >= TOMOYO_MAX_PROFILES)
294 ptr = tomoyo_profile_ptr[profile];
297 entry = kzalloc(sizeof(*entry), GFP_NOFS);
298 if (mutex_lock_interruptible(&tomoyo_policy_lock))
300 ptr = tomoyo_profile_ptr[profile];
301 if (!ptr && tomoyo_memory_ok(entry)) {
303 ptr->default_config = TOMOYO_CONFIG_DISABLED;
304 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
305 sizeof(ptr->config));
306 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
307 mb(); /* Avoid out-of-order execution. */
308 tomoyo_profile_ptr[profile] = ptr;
311 mutex_unlock(&tomoyo_policy_lock);
318 * tomoyo_profile - Find a profile.
320 * @profile: Profile number to find.
322 * Returns pointer to "struct tomoyo_profile".
324 struct tomoyo_profile *tomoyo_profile(const u8 profile)
326 static struct tomoyo_profile tomoyo_null_profile;
327 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
329 ptr = &tomoyo_null_profile;
334 * tomoyo_find_yesno - Find values for specified keyword.
336 * @string: String to check.
337 * @find: Name of keyword.
339 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
342 static s8 tomoyo_find_yesno(const char *string, const char *find)
344 const char *cp = strstr(string, find);
347 if (!strncmp(cp, "=yes", 4))
349 else if (!strncmp(cp, "=no", 3))
357 * tomoyo_set_uint - Set value for specified preference.
359 * @i: Pointer to "unsigned int".
360 * @string: String to check.
361 * @find: Name of keyword.
365 static void tomoyo_set_uint(unsigned int *i, const char *string,
368 const char *cp = strstr(string, find);
370 sscanf(cp + strlen(find), "=%u", i);
374 * tomoyo_set_mode - Set mode for specified profile.
376 * @name: Name of functionality.
377 * @value: Mode for @name.
378 * @profile: Pointer to "struct tomoyo_profile".
380 * Returns 0 on success, negative value otherwise.
382 static int tomoyo_set_mode(char *name, const char *value,
383 struct tomoyo_profile *profile)
387 if (!strcmp(name, "CONFIG")) {
388 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
389 config = profile->default_config;
390 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
392 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
393 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
394 if (strcmp(name, tomoyo_mac_keywords[i]))
396 config = profile->config[i];
399 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
404 if (strstr(value, "use_default")) {
405 config = TOMOYO_CONFIG_USE_DEFAULT;
408 for (mode = 0; mode < 4; mode++)
409 if (strstr(value, tomoyo_mode[mode]))
411 * Update lower 3 bits in order to distinguish
412 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
414 config = (config & ~7) | mode;
416 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
417 profile->config[i] = config;
418 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
419 profile->default_config = config;
424 * tomoyo_write_profile - Write profile table.
426 * @head: Pointer to "struct tomoyo_io_buffer".
428 * Returns 0 on success, negative value otherwise.
430 static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
432 char *data = head->write_buf;
435 struct tomoyo_profile *profile;
436 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
438 i = simple_strtoul(data, &cp, 10);
442 profile = tomoyo_assign_profile(i);
445 cp = strchr(data, '=');
449 if (!strcmp(data, "COMMENT")) {
450 static DEFINE_SPINLOCK(lock);
451 const struct tomoyo_path_info *new_comment
452 = tomoyo_get_name(cp);
453 const struct tomoyo_path_info *old_comment;
457 old_comment = profile->comment;
458 profile->comment = new_comment;
460 tomoyo_put_name(old_comment);
463 if (!strcmp(data, "PREFERENCE")) {
464 for (i = 0; i < TOMOYO_MAX_PREF; i++)
465 tomoyo_set_uint(&profile->pref[i], cp,
466 tomoyo_pref_keywords[i]);
469 return tomoyo_set_mode(data, cp, profile);
472 static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
474 tomoyo_io_printf(head, "={ mode=%s }\n", tomoyo_mode[config & 3]);
478 * tomoyo_read_profile - Read profile table.
480 * @head: Pointer to "struct tomoyo_io_buffer".
482 static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
485 const struct tomoyo_profile *profile;
487 index = head->r.index;
488 profile = tomoyo_profile_ptr[index];
489 switch (head->r.step) {
491 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
495 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
497 if (tomoyo_profile_ptr[head->r.index])
499 if (head->r.index == TOMOYO_MAX_PROFILES)
506 const struct tomoyo_path_info *comment =
508 tomoyo_io_printf(head, "%u-COMMENT=", index);
509 tomoyo_set_string(head, comment ? comment->name : "");
511 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
512 for (i = 0; i < TOMOYO_MAX_PREF; i++)
513 tomoyo_io_printf(head, "%s=%u ",
514 tomoyo_pref_keywords[i],
516 tomoyo_set_string(head, "}\n");
522 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
523 tomoyo_print_config(head, profile->default_config);
529 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
530 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
531 const u8 i = head->r.bit;
532 const u8 config = profile->config[i];
533 if (config == TOMOYO_CONFIG_USE_DEFAULT)
535 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
536 tomoyo_mac_keywords[i]);
537 tomoyo_print_config(head, config);
541 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
542 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
548 if (tomoyo_flush(head))
552 static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
553 const struct tomoyo_acl_head *b)
555 return container_of(a, struct tomoyo_manager, head)->manager ==
556 container_of(b, struct tomoyo_manager, head)->manager;
560 * tomoyo_update_manager_entry - Add a manager entry.
562 * @manager: The path to manager or the domainnamme.
563 * @is_delete: True if it is a delete request.
565 * Returns 0 on success, negative value otherwise.
567 * Caller holds tomoyo_read_lock().
569 static int tomoyo_update_manager_entry(const char *manager,
570 const bool is_delete)
572 struct tomoyo_manager e = { };
573 struct tomoyo_acl_param param = {
574 .is_delete = is_delete,
575 .list = &tomoyo_policy_list[TOMOYO_ID_MANAGER],
577 int error = is_delete ? -ENOENT : -ENOMEM;
578 if (tomoyo_domain_def(manager)) {
579 if (!tomoyo_correct_domain(manager))
583 if (!tomoyo_correct_path(manager))
586 e.manager = tomoyo_get_name(manager);
588 error = tomoyo_update_policy(&e.head, sizeof(e), ¶m,
589 tomoyo_same_manager);
590 tomoyo_put_name(e.manager);
596 * tomoyo_write_manager - Write manager policy.
598 * @head: Pointer to "struct tomoyo_io_buffer".
600 * Returns 0 on success, negative value otherwise.
602 * Caller holds tomoyo_read_lock().
604 static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
606 char *data = head->write_buf;
607 bool is_delete = tomoyo_str_starts(&data, "delete ");
609 if (!strcmp(data, "manage_by_non_root")) {
610 tomoyo_manage_by_non_root = !is_delete;
613 return tomoyo_update_manager_entry(data, is_delete);
617 * tomoyo_read_manager - Read manager policy.
619 * @head: Pointer to "struct tomoyo_io_buffer".
621 * Caller holds tomoyo_read_lock().
623 static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
627 list_for_each_cookie(head->r.acl,
628 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
629 struct tomoyo_manager *ptr =
630 list_entry(head->r.acl, typeof(*ptr), head.list);
631 if (ptr->head.is_deleted)
633 if (!tomoyo_flush(head))
635 tomoyo_set_string(head, ptr->manager->name);
642 * tomoyo_manager - Check whether the current process is a policy manager.
644 * Returns true if the current process is permitted to modify policy
645 * via /sys/kernel/security/tomoyo/ interface.
647 * Caller holds tomoyo_read_lock().
649 static bool tomoyo_manager(void)
651 struct tomoyo_manager *ptr;
653 const struct task_struct *task = current;
654 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
657 if (!tomoyo_policy_loaded)
659 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
661 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
663 if (!ptr->head.is_deleted && ptr->is_domain
664 && !tomoyo_pathcmp(domainname, ptr->manager)) {
671 exe = tomoyo_get_exe();
674 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
676 if (!ptr->head.is_deleted && !ptr->is_domain
677 && !strcmp(exe, ptr->manager->name)) {
682 if (!found) { /* Reduce error messages. */
683 static pid_t last_pid;
684 const pid_t pid = current->pid;
685 if (last_pid != pid) {
686 printk(KERN_WARNING "%s ( %s ) is not permitted to "
687 "update policies.\n", domainname->name, exe);
696 * tomoyo_select_one - Parse select command.
698 * @head: Pointer to "struct tomoyo_io_buffer".
699 * @data: String to parse.
701 * Returns true on success, false otherwise.
703 * Caller holds tomoyo_read_lock().
705 static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
708 struct tomoyo_domain_info *domain = NULL;
709 bool global_pid = false;
711 if (!strcmp(data, "allow_execute")) {
712 head->r.print_execute_only = true;
715 if (sscanf(data, "pid=%u", &pid) == 1 ||
716 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
717 struct task_struct *p;
719 read_lock(&tasklist_lock);
721 p = find_task_by_pid_ns(pid, &init_pid_ns);
723 p = find_task_by_vpid(pid);
725 domain = tomoyo_real_domain(p);
726 read_unlock(&tasklist_lock);
728 } else if (!strncmp(data, "domain=", 7)) {
729 if (tomoyo_domain_def(data + 7))
730 domain = tomoyo_find_domain(data + 7);
733 head->w.domain = domain;
734 /* Accessing read_buf is safe because head->io_sem is held. */
736 return true; /* Do nothing if open(O_WRONLY). */
737 memset(&head->r, 0, sizeof(head->r));
738 head->r.print_this_domain_only = true;
740 head->r.domain = &domain->list;
743 tomoyo_io_printf(head, "# select %s\n", data);
744 if (domain && domain->is_deleted)
745 tomoyo_io_printf(head, "# This is a deleted domain.\n");
750 * tomoyo_delete_domain - Delete a domain.
752 * @domainname: The name of domain.
756 * Caller holds tomoyo_read_lock().
758 static int tomoyo_delete_domain(char *domainname)
760 struct tomoyo_domain_info *domain;
761 struct tomoyo_path_info name;
763 name.name = domainname;
764 tomoyo_fill_path_info(&name);
765 if (mutex_lock_interruptible(&tomoyo_policy_lock))
767 /* Is there an active domain? */
768 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
769 /* Never delete tomoyo_kernel_domain */
770 if (domain == &tomoyo_kernel_domain)
772 if (domain->is_deleted ||
773 tomoyo_pathcmp(domain->domainname, &name))
775 domain->is_deleted = true;
778 mutex_unlock(&tomoyo_policy_lock);
783 * tomoyo_write_domain2 - Write domain policy.
785 * @list: Pointer to "struct list_head".
786 * @data: Policy to be interpreted.
787 * @is_delete: True if it is a delete request.
789 * Returns 0 on success, negative value otherwise.
791 * Caller holds tomoyo_read_lock().
793 static int tomoyo_write_domain2(struct list_head *list, char *data,
794 const bool is_delete)
796 struct tomoyo_acl_param param = {
799 .is_delete = is_delete,
801 static const struct {
803 int (*write) (struct tomoyo_acl_param *);
804 } tomoyo_callback[1] = {
805 { "file ", tomoyo_write_file },
808 for (i = 0; i < 1; i++) {
809 if (!tomoyo_str_starts(¶m.data,
810 tomoyo_callback[i].keyword))
812 return tomoyo_callback[i].write(¶m);
818 * tomoyo_write_domain - Write domain policy.
820 * @head: Pointer to "struct tomoyo_io_buffer".
822 * Returns 0 on success, negative value otherwise.
824 * Caller holds tomoyo_read_lock().
826 static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
828 char *data = head->write_buf;
829 struct tomoyo_domain_info *domain = head->w.domain;
830 bool is_delete = false;
831 bool is_select = false;
832 unsigned int profile;
834 if (tomoyo_str_starts(&data, "delete "))
836 else if (tomoyo_str_starts(&data, "select "))
838 if (is_select && tomoyo_select_one(head, data))
840 /* Don't allow updating policies by non manager programs. */
841 if (!tomoyo_manager())
843 if (tomoyo_domain_def(data)) {
846 tomoyo_delete_domain(data);
848 domain = tomoyo_find_domain(data);
850 domain = tomoyo_assign_domain(data, 0);
851 head->w.domain = domain;
857 if (sscanf(data, "use_profile %u", &profile) == 1
858 && profile < TOMOYO_MAX_PROFILES) {
859 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
860 domain->profile = (u8) profile;
863 if (!strcmp(data, "quota_exceeded")) {
864 domain->quota_warned = !is_delete;
867 if (!strcmp(data, "transition_failed")) {
868 domain->transition_failed = !is_delete;
871 return tomoyo_write_domain2(&domain->acl_info_list, data, is_delete);
875 * tomoyo_set_group - Print category name.
877 * @head: Pointer to "struct tomoyo_io_buffer".
878 * @category: Category name.
882 static void tomoyo_set_group(struct tomoyo_io_buffer *head,
883 const char *category)
885 tomoyo_set_string(head, category);
889 * tomoyo_print_entry - Print an ACL entry.
891 * @head: Pointer to "struct tomoyo_io_buffer".
892 * @acl: Pointer to an ACL entry.
894 * Returns true on success, false otherwise.
896 static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
897 struct tomoyo_acl_info *acl)
899 const u8 acl_type = acl->type;
905 if (!tomoyo_flush(head))
907 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
908 struct tomoyo_path_acl *ptr =
909 container_of(acl, typeof(*ptr), head);
910 const u16 perm = ptr->perm;
911 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
912 if (!(perm & (1 << bit)))
914 if (head->r.print_execute_only &&
915 bit != TOMOYO_TYPE_EXECUTE)
918 tomoyo_set_group(head, "file ");
921 tomoyo_set_slash(head);
923 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
927 tomoyo_print_name_union(head, &ptr->name);
928 } else if (head->r.print_execute_only) {
930 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
931 struct tomoyo_path2_acl *ptr =
932 container_of(acl, typeof(*ptr), head);
933 const u8 perm = ptr->perm;
934 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
935 if (!(perm & (1 << bit)))
938 tomoyo_set_group(head, "file ");
941 tomoyo_set_slash(head);
943 tomoyo_set_string(head, tomoyo_mac_keywords
944 [tomoyo_pp2mac[bit]]);
948 tomoyo_print_name_union(head, &ptr->name1);
949 tomoyo_print_name_union(head, &ptr->name2);
950 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
951 struct tomoyo_path_number_acl *ptr =
952 container_of(acl, typeof(*ptr), head);
953 const u8 perm = ptr->perm;
954 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
955 if (!(perm & (1 << bit)))
958 tomoyo_set_group(head, "file ");
961 tomoyo_set_slash(head);
963 tomoyo_set_string(head, tomoyo_mac_keywords
964 [tomoyo_pn2mac[bit]]);
968 tomoyo_print_name_union(head, &ptr->name);
969 tomoyo_print_number_union(head, &ptr->number);
970 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
971 struct tomoyo_mkdev_acl *ptr =
972 container_of(acl, typeof(*ptr), head);
973 const u8 perm = ptr->perm;
974 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
975 if (!(perm & (1 << bit)))
978 tomoyo_set_group(head, "file ");
981 tomoyo_set_slash(head);
983 tomoyo_set_string(head, tomoyo_mac_keywords
984 [tomoyo_pnnn2mac[bit]]);
988 tomoyo_print_name_union(head, &ptr->name);
989 tomoyo_print_number_union(head, &ptr->mode);
990 tomoyo_print_number_union(head, &ptr->major);
991 tomoyo_print_number_union(head, &ptr->minor);
992 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
993 struct tomoyo_mount_acl *ptr =
994 container_of(acl, typeof(*ptr), head);
995 tomoyo_set_group(head, "file mount");
996 tomoyo_print_name_union(head, &ptr->dev_name);
997 tomoyo_print_name_union(head, &ptr->dir_name);
998 tomoyo_print_name_union(head, &ptr->fs_type);
999 tomoyo_print_number_union(head, &ptr->flags);
1001 tomoyo_set_lf(head);
1006 * tomoyo_read_domain2 - Read domain policy.
1008 * @head: Pointer to "struct tomoyo_io_buffer".
1009 * @domain: Pointer to "struct tomoyo_domain_info".
1011 * Caller holds tomoyo_read_lock().
1013 * Returns true on success, false otherwise.
1015 static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1016 struct tomoyo_domain_info *domain)
1018 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1019 struct tomoyo_acl_info *ptr =
1020 list_entry(head->r.acl, typeof(*ptr), list);
1021 if (!tomoyo_print_entry(head, ptr))
1029 * tomoyo_read_domain - Read domain policy.
1031 * @head: Pointer to "struct tomoyo_io_buffer".
1033 * Caller holds tomoyo_read_lock().
1035 static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
1039 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1040 struct tomoyo_domain_info *domain =
1041 list_entry(head->r.domain, typeof(*domain), list);
1042 switch (head->r.step) {
1044 if (domain->is_deleted &&
1045 !head->r.print_this_domain_only)
1047 /* Print domainname and flags. */
1048 tomoyo_set_string(head, domain->domainname->name);
1049 tomoyo_set_lf(head);
1050 tomoyo_io_printf(head, "use_profile %u\n",
1052 if (domain->quota_warned)
1053 tomoyo_set_string(head, "quota_exceeded\n");
1054 if (domain->transition_failed)
1055 tomoyo_set_string(head, "transition_failed\n");
1057 tomoyo_set_lf(head);
1060 if (!tomoyo_read_domain2(head, domain))
1063 if (!tomoyo_set_lf(head))
1068 if (head->r.print_this_domain_only)
1077 * tomoyo_write_domain_profile - Assign profile for specified domain.
1079 * @head: Pointer to "struct tomoyo_io_buffer".
1081 * Returns 0 on success, -EINVAL otherwise.
1083 * This is equivalent to doing
1085 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1086 * /usr/sbin/tomoyo-loadpolicy -d
1088 * Caller holds tomoyo_read_lock().
1090 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1092 char *data = head->write_buf;
1093 char *cp = strchr(data, ' ');
1094 struct tomoyo_domain_info *domain;
1095 unsigned long profile;
1100 domain = tomoyo_find_domain(cp + 1);
1101 if (strict_strtoul(data, 10, &profile))
1103 if (domain && profile < TOMOYO_MAX_PROFILES
1104 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1105 domain->profile = (u8) profile;
1110 * tomoyo_read_domain_profile - Read only domainname and profile.
1112 * @head: Pointer to "struct tomoyo_io_buffer".
1114 * Returns list of profile number and domainname pairs.
1116 * This is equivalent to doing
1118 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1119 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1120 * domainname = $0; } else if ( $1 == "use_profile" ) {
1121 * print $2 " " domainname; domainname = ""; } } ; '
1123 * Caller holds tomoyo_read_lock().
1125 static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1129 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1130 struct tomoyo_domain_info *domain =
1131 list_entry(head->r.domain, typeof(*domain), list);
1132 if (domain->is_deleted)
1134 if (!tomoyo_flush(head))
1136 tomoyo_io_printf(head, "%u ", domain->profile);
1137 tomoyo_set_string(head, domain->domainname->name);
1138 tomoyo_set_lf(head);
1144 * tomoyo_write_pid: Specify PID to obtain domainname.
1146 * @head: Pointer to "struct tomoyo_io_buffer".
1150 static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1152 head->r.eof = false;
1157 * tomoyo_read_pid - Get domainname of the specified PID.
1159 * @head: Pointer to "struct tomoyo_io_buffer".
1161 * Returns the domainname which the specified PID is in on success,
1162 * empty string otherwise.
1163 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1164 * using read()/write() interface rather than sysctl() interface.
1166 static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
1168 char *buf = head->write_buf;
1169 bool global_pid = false;
1171 struct task_struct *p;
1172 struct tomoyo_domain_info *domain = NULL;
1174 /* Accessing write_buf is safe because head->io_sem is held. */
1177 return; /* Do nothing if open(O_RDONLY). */
1179 if (head->r.w_pos || head->r.eof)
1182 if (tomoyo_str_starts(&buf, "global-pid "))
1184 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1186 read_lock(&tasklist_lock);
1188 p = find_task_by_pid_ns(pid, &init_pid_ns);
1190 p = find_task_by_vpid(pid);
1192 domain = tomoyo_real_domain(p);
1193 read_unlock(&tasklist_lock);
1197 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1198 tomoyo_set_string(head, domain->domainname->name);
1201 static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1202 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain",
1203 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain",
1204 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain",
1205 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain",
1208 static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1209 [TOMOYO_PATH_GROUP] = "path_group ",
1210 [TOMOYO_NUMBER_GROUP] = "number_group ",
1214 * tomoyo_write_exception - Write exception policy.
1216 * @head: Pointer to "struct tomoyo_io_buffer".
1218 * Returns 0 on success, negative value otherwise.
1220 * Caller holds tomoyo_read_lock().
1222 static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
1224 struct tomoyo_acl_param param = {
1225 .data = head->write_buf,
1228 param.is_delete = tomoyo_str_starts(¶m.data, "delete ");
1229 if (tomoyo_str_starts(¶m.data, "aggregator "))
1230 return tomoyo_write_aggregator(¶m);
1231 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1232 if (tomoyo_str_starts(¶m.data, tomoyo_transition_type[i]))
1233 return tomoyo_write_transition_control(¶m, i);
1234 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1235 if (tomoyo_str_starts(¶m.data, tomoyo_group_name[i]))
1236 return tomoyo_write_group(¶m, i);
1241 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1243 * @head: Pointer to "struct tomoyo_io_buffer".
1244 * @idx: Index number.
1246 * Returns true on success, false otherwise.
1248 * Caller holds tomoyo_read_lock().
1250 static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1252 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
1253 struct tomoyo_group *group =
1254 list_entry(head->r.group, typeof(*group), head.list);
1255 list_for_each_cookie(head->r.acl, &group->member_list) {
1256 struct tomoyo_acl_head *ptr =
1257 list_entry(head->r.acl, typeof(*ptr), list);
1258 if (ptr->is_deleted)
1260 if (!tomoyo_flush(head))
1262 tomoyo_set_string(head, tomoyo_group_name[idx]);
1263 tomoyo_set_string(head, group->group_name->name);
1264 if (idx == TOMOYO_PATH_GROUP) {
1265 tomoyo_set_space(head);
1266 tomoyo_set_string(head, container_of
1267 (ptr, struct tomoyo_path_group,
1268 head)->member_name->name);
1269 } else if (idx == TOMOYO_NUMBER_GROUP) {
1270 tomoyo_print_number_union(head, &container_of
1272 struct tomoyo_number_group,
1275 tomoyo_set_lf(head);
1279 head->r.group = NULL;
1284 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1286 * @head: Pointer to "struct tomoyo_io_buffer".
1287 * @idx: Index number.
1289 * Returns true on success, false otherwise.
1291 * Caller holds tomoyo_read_lock().
1293 static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1295 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
1296 struct tomoyo_acl_head *acl =
1297 container_of(head->r.acl, typeof(*acl), list);
1298 if (acl->is_deleted)
1300 if (!tomoyo_flush(head))
1303 case TOMOYO_ID_TRANSITION_CONTROL:
1305 struct tomoyo_transition_control *ptr =
1306 container_of(acl, typeof(*ptr), head);
1307 tomoyo_set_string(head, tomoyo_transition_type
1309 tomoyo_set_string(head, ptr->program ?
1310 ptr->program->name : "any");
1311 tomoyo_set_string(head, " from ");
1312 tomoyo_set_string(head, ptr->domainname ?
1313 ptr->domainname->name :
1317 case TOMOYO_ID_AGGREGATOR:
1319 struct tomoyo_aggregator *ptr =
1320 container_of(acl, typeof(*ptr), head);
1321 tomoyo_set_string(head, "aggregator ");
1322 tomoyo_set_string(head,
1323 ptr->original_name->name);
1324 tomoyo_set_space(head);
1325 tomoyo_set_string(head,
1326 ptr->aggregated_name->name);
1332 tomoyo_set_lf(head);
1339 * tomoyo_read_exception - Read exception policy.
1341 * @head: Pointer to "struct tomoyo_io_buffer".
1343 * Caller holds tomoyo_read_lock().
1345 static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
1349 while (head->r.step < TOMOYO_MAX_POLICY &&
1350 tomoyo_read_policy(head, head->r.step))
1352 if (head->r.step < TOMOYO_MAX_POLICY)
1354 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1355 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1357 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1363 * tomoyo_print_header - Get header line of audit log.
1365 * @r: Pointer to "struct tomoyo_request_info".
1367 * Returns string representation.
1369 * This function uses kmalloc(), so caller must kfree() if this function
1370 * didn't return NULL.
1372 static char *tomoyo_print_header(struct tomoyo_request_info *r)
1375 const pid_t gpid = task_pid_nr(current);
1376 static const int tomoyo_buffer_len = 4096;
1377 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1381 do_gettimeofday(&tv);
1383 ppid = task_tgid_vnr(current->real_parent);
1385 snprintf(buffer, tomoyo_buffer_len - 1,
1386 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1387 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1388 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1389 tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
1390 task_tgid_vnr(current), ppid,
1391 current_uid(), current_gid(), current_euid(),
1392 current_egid(), current_suid(), current_sgid(),
1393 current_fsuid(), current_fsgid());
1398 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1400 * @len: Required size.
1401 * @r: Pointer to "struct tomoyo_request_info".
1403 * Returns pointer to allocated memory.
1405 * The @len is updated to add the header lines' size on success.
1407 * This function uses kzalloc(), so caller must kfree() if this function
1408 * didn't return NULL.
1410 static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1414 const char *domainname;
1416 r->domain = tomoyo_domain();
1417 domainname = r->domain->domainname->name;
1418 header = tomoyo_print_header(r);
1421 *len += strlen(domainname) + strlen(header) + 10;
1422 buf = kzalloc(*len, GFP_NOFS);
1424 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1429 /* Wait queue for tomoyo_query_list. */
1430 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1432 /* Lock for manipulating tomoyo_query_list. */
1433 static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1435 /* Structure for query. */
1436 struct tomoyo_query {
1437 struct list_head list;
1440 unsigned int serial;
1445 /* The list for "struct tomoyo_query". */
1446 static LIST_HEAD(tomoyo_query_list);
1449 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1452 static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1455 * tomoyo_supervisor - Ask for the supervisor's decision.
1457 * @r: Pointer to "struct tomoyo_request_info".
1458 * @fmt: The printf()'s format string, followed by parameters.
1460 * Returns 0 if the supervisor decided to permit the access request which
1461 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1462 * supervisor decided to retry the access request which violated the policy in
1463 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1465 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1471 static unsigned int tomoyo_serial;
1472 struct tomoyo_query *entry = NULL;
1473 bool quota_exceeded = false;
1477 case TOMOYO_CONFIG_LEARNING:
1478 if (!tomoyo_domain_quota_is_ok(r))
1480 va_start(args, fmt);
1481 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1483 buffer = kmalloc(len, GFP_NOFS);
1486 va_start(args, fmt);
1487 vsnprintf(buffer, len - 1, fmt, args);
1489 tomoyo_normalize_line(buffer);
1490 tomoyo_write_domain2(&r->domain->acl_info_list, buffer, false);
1493 case TOMOYO_CONFIG_PERMISSIVE:
1497 r->domain = tomoyo_domain();
1498 if (!atomic_read(&tomoyo_query_observers))
1500 va_start(args, fmt);
1501 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1503 header = tomoyo_init_audit_log(&len, r);
1506 entry = kzalloc(sizeof(*entry), GFP_NOFS);
1509 entry->query = kzalloc(len, GFP_NOFS);
1512 len = ksize(entry->query);
1513 spin_lock(&tomoyo_query_list_lock);
1514 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
1515 sizeof(*entry) >= tomoyo_quota_for_query) {
1516 quota_exceeded = true;
1518 tomoyo_query_memory_size += len + sizeof(*entry);
1519 entry->serial = tomoyo_serial++;
1521 spin_unlock(&tomoyo_query_list_lock);
1524 pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1525 entry->serial, r->retry, header);
1528 va_start(args, fmt);
1529 vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1530 entry->query_len = strlen(entry->query) + 1;
1532 spin_lock(&tomoyo_query_list_lock);
1533 list_add_tail(&entry->list, &tomoyo_query_list);
1534 spin_unlock(&tomoyo_query_list_lock);
1535 /* Give 10 seconds for supervisor's opinion. */
1536 for (entry->timer = 0;
1537 atomic_read(&tomoyo_query_observers) && entry->timer < 100;
1539 wake_up(&tomoyo_query_wait);
1540 set_current_state(TASK_INTERRUPTIBLE);
1541 schedule_timeout(HZ / 10);
1545 spin_lock(&tomoyo_query_list_lock);
1546 list_del(&entry->list);
1547 tomoyo_query_memory_size -= len + sizeof(*entry);
1548 spin_unlock(&tomoyo_query_list_lock);
1549 switch (entry->answer) {
1550 case 3: /* Asked to retry by administrator. */
1551 error = TOMOYO_RETRY_REQUEST;
1555 /* Granted by administrator. */
1562 /* Rejected by administrator. */
1567 kfree(entry->query);
1574 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1576 * @file: Pointer to "struct file".
1577 * @wait: Pointer to "poll_table".
1579 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1581 * Waits for access requests which violated policy in enforcing mode.
1583 static int tomoyo_poll_query(struct file *file, poll_table *wait)
1585 struct list_head *tmp;
1588 for (i = 0; i < 2; i++) {
1589 spin_lock(&tomoyo_query_list_lock);
1590 list_for_each(tmp, &tomoyo_query_list) {
1591 struct tomoyo_query *ptr =
1592 list_entry(tmp, typeof(*ptr), list);
1598 spin_unlock(&tomoyo_query_list_lock);
1600 return POLLIN | POLLRDNORM;
1603 poll_wait(file, &tomoyo_query_wait, wait);
1609 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1611 * @head: Pointer to "struct tomoyo_io_buffer".
1613 static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1615 struct list_head *tmp;
1621 if (head->read_buf) {
1622 kfree(head->read_buf);
1623 head->read_buf = NULL;
1625 spin_lock(&tomoyo_query_list_lock);
1626 list_for_each(tmp, &tomoyo_query_list) {
1627 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1630 if (pos++ != head->r.query_index)
1632 len = ptr->query_len;
1635 spin_unlock(&tomoyo_query_list_lock);
1637 head->r.query_index = 0;
1640 buf = kzalloc(len, GFP_NOFS);
1644 spin_lock(&tomoyo_query_list_lock);
1645 list_for_each(tmp, &tomoyo_query_list) {
1646 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1649 if (pos++ != head->r.query_index)
1652 * Some query can be skipped because tomoyo_query_list
1653 * can change, but I don't care.
1655 if (len == ptr->query_len)
1656 memmove(buf, ptr->query, len);
1659 spin_unlock(&tomoyo_query_list_lock);
1661 head->read_buf = buf;
1662 head->r.w[head->r.w_pos++] = buf;
1663 head->r.query_index++;
1670 * tomoyo_write_answer - Write the supervisor's decision.
1672 * @head: Pointer to "struct tomoyo_io_buffer".
1674 * Returns 0 on success, -EINVAL otherwise.
1676 static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1678 char *data = head->write_buf;
1679 struct list_head *tmp;
1680 unsigned int serial;
1681 unsigned int answer;
1682 spin_lock(&tomoyo_query_list_lock);
1683 list_for_each(tmp, &tomoyo_query_list) {
1684 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1687 spin_unlock(&tomoyo_query_list_lock);
1688 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1690 spin_lock(&tomoyo_query_list_lock);
1691 list_for_each(tmp, &tomoyo_query_list) {
1692 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1693 if (ptr->serial != serial)
1696 ptr->answer = answer;
1699 spin_unlock(&tomoyo_query_list_lock);
1704 * tomoyo_read_version: Get version.
1706 * @head: Pointer to "struct tomoyo_io_buffer".
1708 * Returns version information.
1710 static void tomoyo_read_version(struct tomoyo_io_buffer *head)
1713 tomoyo_io_printf(head, "2.4.0");
1719 * tomoyo_read_self_domain - Get the current process's domainname.
1721 * @head: Pointer to "struct tomoyo_io_buffer".
1723 * Returns the current process's domainname.
1725 static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1729 * tomoyo_domain()->domainname != NULL
1730 * because every process belongs to a domain and
1731 * the domain's name cannot be NULL.
1733 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1739 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1741 * @type: Type of interface.
1742 * @file: Pointer to "struct file".
1744 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1746 * Caller acquires tomoyo_read_lock().
1748 int tomoyo_open_control(const u8 type, struct file *file)
1750 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
1754 mutex_init(&head->io_sem);
1757 case TOMOYO_DOMAINPOLICY:
1758 /* /sys/kernel/security/tomoyo/domain_policy */
1759 head->write = tomoyo_write_domain;
1760 head->read = tomoyo_read_domain;
1762 case TOMOYO_EXCEPTIONPOLICY:
1763 /* /sys/kernel/security/tomoyo/exception_policy */
1764 head->write = tomoyo_write_exception;
1765 head->read = tomoyo_read_exception;
1767 case TOMOYO_SELFDOMAIN:
1768 /* /sys/kernel/security/tomoyo/self_domain */
1769 head->read = tomoyo_read_self_domain;
1771 case TOMOYO_DOMAIN_STATUS:
1772 /* /sys/kernel/security/tomoyo/.domain_status */
1773 head->write = tomoyo_write_domain_profile;
1774 head->read = tomoyo_read_domain_profile;
1776 case TOMOYO_PROCESS_STATUS:
1777 /* /sys/kernel/security/tomoyo/.process_status */
1778 head->write = tomoyo_write_pid;
1779 head->read = tomoyo_read_pid;
1781 case TOMOYO_VERSION:
1782 /* /sys/kernel/security/tomoyo/version */
1783 head->read = tomoyo_read_version;
1784 head->readbuf_size = 128;
1786 case TOMOYO_MEMINFO:
1787 /* /sys/kernel/security/tomoyo/meminfo */
1788 head->write = tomoyo_write_memory_quota;
1789 head->read = tomoyo_read_memory_counter;
1790 head->readbuf_size = 512;
1792 case TOMOYO_PROFILE:
1793 /* /sys/kernel/security/tomoyo/profile */
1794 head->write = tomoyo_write_profile;
1795 head->read = tomoyo_read_profile;
1797 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1798 head->poll = tomoyo_poll_query;
1799 head->write = tomoyo_write_answer;
1800 head->read = tomoyo_read_query;
1802 case TOMOYO_MANAGER:
1803 /* /sys/kernel/security/tomoyo/manager */
1804 head->write = tomoyo_write_manager;
1805 head->read = tomoyo_read_manager;
1808 if (!(file->f_mode & FMODE_READ)) {
1810 * No need to allocate read_buf since it is not opened
1815 } else if (!head->poll) {
1816 /* Don't allocate read_buf for poll() access. */
1817 if (!head->readbuf_size)
1818 head->readbuf_size = 4096 * 2;
1819 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
1820 if (!head->read_buf) {
1825 if (!(file->f_mode & FMODE_WRITE)) {
1827 * No need to allocate write_buf since it is not opened
1831 } else if (head->write) {
1832 head->writebuf_size = 4096 * 2;
1833 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
1834 if (!head->write_buf) {
1835 kfree(head->read_buf);
1840 if (type != TOMOYO_QUERY)
1841 head->reader_idx = tomoyo_read_lock();
1842 file->private_data = head;
1844 * If the file is /sys/kernel/security/tomoyo/query , increment the
1846 * The obserber counter is used by tomoyo_supervisor() to see if
1847 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1849 if (type == TOMOYO_QUERY)
1850 atomic_inc(&tomoyo_query_observers);
1855 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1857 * @file: Pointer to "struct file".
1858 * @wait: Pointer to "poll_table".
1860 * Waits for read readiness.
1861 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1863 int tomoyo_poll_control(struct file *file, poll_table *wait)
1865 struct tomoyo_io_buffer *head = file->private_data;
1868 return head->poll(file, wait);
1872 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1874 * @head: Pointer to "struct tomoyo_io_buffer".
1875 * @buffer: Poiner to buffer to write to.
1876 * @buffer_len: Size of @buffer.
1878 * Returns bytes read on success, negative value otherwise.
1880 * Caller holds tomoyo_read_lock().
1882 int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
1883 const int buffer_len)
1889 if (mutex_lock_interruptible(&head->io_sem))
1891 head->read_user_buf = buffer;
1892 head->read_user_buf_avail = buffer_len;
1893 if (tomoyo_flush(head))
1894 /* Call the policy handler. */
1897 len = head->read_user_buf - buffer;
1898 mutex_unlock(&head->io_sem);
1903 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1905 * @head: Pointer to "struct tomoyo_io_buffer".
1906 * @buffer: Pointer to buffer to read from.
1907 * @buffer_len: Size of @buffer.
1909 * Returns @buffer_len on success, negative value otherwise.
1911 * Caller holds tomoyo_read_lock().
1913 int tomoyo_write_control(struct tomoyo_io_buffer *head,
1914 const char __user *buffer, const int buffer_len)
1916 int error = buffer_len;
1917 int avail_len = buffer_len;
1918 char *cp0 = head->write_buf;
1922 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1924 /* Don't allow updating policies by non manager programs. */
1925 if (head->write != tomoyo_write_pid &&
1926 head->write != tomoyo_write_domain && !tomoyo_manager())
1928 if (mutex_lock_interruptible(&head->io_sem))
1930 /* Read a line and dispatch it to the policy handler. */
1931 while (avail_len > 0) {
1933 if (head->w.avail >= head->writebuf_size - 1) {
1936 } else if (get_user(c, buffer)) {
1942 cp0[head->w.avail++] = c;
1945 cp0[head->w.avail - 1] = '\0';
1947 tomoyo_normalize_line(cp0);
1950 mutex_unlock(&head->io_sem);
1955 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1957 * @head: Pointer to "struct tomoyo_io_buffer".
1959 * Releases memory and returns 0.
1961 * Caller looses tomoyo_read_lock().
1963 int tomoyo_close_control(struct tomoyo_io_buffer *head)
1965 const bool is_write = !!head->write_buf;
1968 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1971 if (head->type == TOMOYO_QUERY)
1972 atomic_dec(&tomoyo_query_observers);
1974 tomoyo_read_unlock(head->reader_idx);
1975 /* Release memory used for policy I/O. */
1976 kfree(head->read_buf);
1977 head->read_buf = NULL;
1978 kfree(head->write_buf);
1979 head->write_buf = NULL;
1987 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
1989 void tomoyo_check_profile(void)
1991 struct tomoyo_domain_info *domain;
1992 const int idx = tomoyo_read_lock();
1993 tomoyo_policy_loaded = true;
1994 /* Check all profiles currently assigned to domains are defined. */
1995 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1996 const u8 profile = domain->profile;
1997 if (tomoyo_profile_ptr[profile])
1999 printk(KERN_ERR "You need to define profile %u before using it.\n",
2001 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2002 "for more information.\n");
2003 panic("Profile %u (used by '%s') not defined.\n",
2004 profile, domain->domainname->name);
2006 tomoyo_read_unlock(idx);
2007 if (tomoyo_profile_version != 20090903) {
2008 printk(KERN_ERR "You need to install userland programs for "
2009 "TOMOYO 2.3 and initialize policy configuration.\n");
2010 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2011 "for more information.\n");
2012 panic("Profile version %u is not supported.\n",
2013 tomoyo_profile_version);
2015 printk(KERN_INFO "TOMOYO: 2.3.0\n");
2016 printk(KERN_INFO "Mandatory Access Control activated.\n");