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 operation mode. */
21 const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
22 [TOMOYO_CONFIG_DISABLED] = "disabled",
23 [TOMOYO_CONFIG_LEARNING] = "learning",
24 [TOMOYO_CONFIG_PERMISSIVE] = "permissive",
25 [TOMOYO_CONFIG_ENFORCING] = "enforcing"
28 /* String table for /sys/kernel/security/tomoyo/profile */
29 static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
30 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
31 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
32 [TOMOYO_MAC_FILE_OPEN] = "file::open",
33 [TOMOYO_MAC_FILE_CREATE] = "file::create",
34 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
35 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
36 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
37 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
38 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
39 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
40 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
41 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
42 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
43 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
44 [TOMOYO_MAC_FILE_LINK] = "file::link",
45 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
46 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
47 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
48 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
49 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
50 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
51 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
52 [TOMOYO_MAC_FILE_UMOUNT] = "file::unmount",
53 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
54 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
57 /* String table for PREFERENCE keyword. */
58 static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
59 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
60 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
63 /* Permit policy management by non-root user? */
64 static bool tomoyo_manage_by_non_root;
66 /* Utility functions. */
69 * tomoyo_yesno - Return "yes" or "no".
73 const char *tomoyo_yesno(const unsigned int value)
75 return value ? "yes" : "no";
79 * tomoyo_addprintf - strncat()-like-snprintf().
81 * @buffer: Buffer to write to. Must be '\0'-terminated.
82 * @len: Size of @buffer.
83 * @fmt: The printf()'s format string, followed by parameters.
87 static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
90 const int pos = strlen(buffer);
92 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
97 * tomoyo_flush - Flush queued string to userspace's buffer.
99 * @head: Pointer to "struct tomoyo_io_buffer".
101 * Returns true if all data was flushed, false otherwise.
103 static bool tomoyo_flush(struct tomoyo_io_buffer *head)
105 while (head->r.w_pos) {
106 const char *w = head->r.w[0];
109 if (len > head->read_user_buf_avail)
110 len = head->read_user_buf_avail;
113 if (copy_to_user(head->read_user_buf, w, len))
115 head->read_user_buf_avail -= len;
116 head->read_user_buf += len;
122 /* Add '\0' for audit logs and query. */
124 if (!head->read_user_buf_avail ||
125 copy_to_user(head->read_user_buf, "", 1))
127 head->read_user_buf_avail--;
128 head->read_user_buf++;
131 for (len = 0; len < head->r.w_pos; len++)
132 head->r.w[len] = head->r.w[len + 1];
139 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
141 * @head: Pointer to "struct tomoyo_io_buffer".
142 * @string: String to print.
144 * Note that @string has to be kept valid until @head is kfree()d.
145 * This means that char[] allocated on stack memory cannot be passed to
146 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
148 static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
150 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
151 head->r.w[head->r.w_pos++] = string;
158 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
160 * @head: Pointer to "struct tomoyo_io_buffer".
161 * @fmt: The printf()'s format string, followed by parameters.
163 void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
167 int pos = head->r.avail;
168 int size = head->readbuf_size - pos;
172 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
174 if (pos + len >= head->readbuf_size) {
178 head->r.avail += len;
179 tomoyo_set_string(head, head->read_buf + pos);
183 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
185 * @head: Pointer to "struct tomoyo_io_buffer".
189 static void tomoyo_set_space(struct tomoyo_io_buffer *head)
191 tomoyo_set_string(head, " ");
195 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
197 * @head: Pointer to "struct tomoyo_io_buffer".
201 static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
203 tomoyo_set_string(head, "\n");
204 return !head->r.w_pos;
208 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
210 * @head: Pointer to "struct tomoyo_io_buffer".
214 static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
216 tomoyo_set_string(head, "/");
220 * tomoyo_print_name_union - Print a tomoyo_name_union.
222 * @head: Pointer to "struct tomoyo_io_buffer".
223 * @ptr: Pointer to "struct tomoyo_name_union".
225 static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
226 const struct tomoyo_name_union *ptr)
228 tomoyo_set_space(head);
230 tomoyo_set_string(head, "@");
231 tomoyo_set_string(head, ptr->group->group_name->name);
233 tomoyo_set_string(head, ptr->filename->name);
238 * tomoyo_print_number_union - Print a tomoyo_number_union.
240 * @head: Pointer to "struct tomoyo_io_buffer".
241 * @ptr: Pointer to "struct tomoyo_number_union".
243 static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
244 const struct tomoyo_number_union *ptr)
246 tomoyo_set_space(head);
248 tomoyo_set_string(head, "@");
249 tomoyo_set_string(head, ptr->group->group_name->name);
252 unsigned long min = ptr->values[0];
253 const unsigned long max = ptr->values[1];
254 u8 min_type = ptr->value_type[0];
255 const u8 max_type = ptr->value_type[1];
258 for (i = 0; i < 2; i++) {
260 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
261 tomoyo_addprintf(buffer, sizeof(buffer),
264 case TOMOYO_VALUE_TYPE_OCTAL:
265 tomoyo_addprintf(buffer, sizeof(buffer),
269 tomoyo_addprintf(buffer, sizeof(buffer),
273 if (min == max && min_type == max_type)
275 tomoyo_addprintf(buffer, sizeof(buffer), "-");
279 tomoyo_io_printf(head, "%s", buffer);
284 * tomoyo_assign_profile - Create a new profile.
286 * @profile: Profile number to create.
288 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
290 static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
292 struct tomoyo_profile *ptr;
293 struct tomoyo_profile *entry;
294 if (profile >= TOMOYO_MAX_PROFILES)
296 ptr = tomoyo_profile_ptr[profile];
299 entry = kzalloc(sizeof(*entry), GFP_NOFS);
300 if (mutex_lock_interruptible(&tomoyo_policy_lock))
302 ptr = tomoyo_profile_ptr[profile];
303 if (!ptr && tomoyo_memory_ok(entry)) {
305 ptr->default_config = TOMOYO_CONFIG_DISABLED |
306 TOMOYO_CONFIG_WANT_GRANT_LOG |
307 TOMOYO_CONFIG_WANT_REJECT_LOG;
308 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
309 sizeof(ptr->config));
310 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
311 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
312 mb(); /* Avoid out-of-order execution. */
313 tomoyo_profile_ptr[profile] = ptr;
316 mutex_unlock(&tomoyo_policy_lock);
323 * tomoyo_profile - Find a profile.
325 * @profile: Profile number to find.
327 * Returns pointer to "struct tomoyo_profile".
329 struct tomoyo_profile *tomoyo_profile(const u8 profile)
331 static struct tomoyo_profile tomoyo_null_profile;
332 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
334 ptr = &tomoyo_null_profile;
339 * tomoyo_find_yesno - Find values for specified keyword.
341 * @string: String to check.
342 * @find: Name of keyword.
344 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
346 static s8 tomoyo_find_yesno(const char *string, const char *find)
348 const char *cp = strstr(string, find);
351 if (!strncmp(cp, "=yes", 4))
353 else if (!strncmp(cp, "=no", 3))
360 * tomoyo_set_uint - Set value for specified preference.
362 * @i: Pointer to "unsigned int".
363 * @string: String to check.
364 * @find: Name of keyword.
368 static void tomoyo_set_uint(unsigned int *i, const char *string,
371 const char *cp = strstr(string, find);
373 sscanf(cp + strlen(find), "=%u", i);
377 * tomoyo_set_mode - Set mode for specified profile.
379 * @name: Name of functionality.
380 * @value: Mode for @name.
381 * @profile: Pointer to "struct tomoyo_profile".
383 * Returns 0 on success, negative value otherwise.
385 static int tomoyo_set_mode(char *name, const char *value,
386 struct tomoyo_profile *profile)
390 if (!strcmp(name, "CONFIG")) {
391 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
392 config = profile->default_config;
393 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
395 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
396 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
397 if (strcmp(name, tomoyo_mac_keywords[i]))
399 config = profile->config[i];
402 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
407 if (strstr(value, "use_default")) {
408 config = TOMOYO_CONFIG_USE_DEFAULT;
411 for (mode = 0; mode < 4; mode++)
412 if (strstr(value, tomoyo_mode[mode]))
414 * Update lower 3 bits in order to distinguish
415 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
417 config = (config & ~7) | mode;
418 if (config != TOMOYO_CONFIG_USE_DEFAULT) {
419 switch (tomoyo_find_yesno(value, "grant_log")) {
421 config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
424 config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
427 switch (tomoyo_find_yesno(value, "reject_log")) {
429 config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
432 config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
437 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
438 profile->config[i] = config;
439 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
440 profile->default_config = config;
445 * tomoyo_write_profile - Write profile table.
447 * @head: Pointer to "struct tomoyo_io_buffer".
449 * Returns 0 on success, negative value otherwise.
451 static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
453 char *data = head->write_buf;
456 struct tomoyo_profile *profile;
457 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
459 i = simple_strtoul(data, &cp, 10);
463 profile = tomoyo_assign_profile(i);
466 cp = strchr(data, '=');
470 if (!strcmp(data, "COMMENT")) {
471 static DEFINE_SPINLOCK(lock);
472 const struct tomoyo_path_info *new_comment
473 = tomoyo_get_name(cp);
474 const struct tomoyo_path_info *old_comment;
478 old_comment = profile->comment;
479 profile->comment = new_comment;
481 tomoyo_put_name(old_comment);
484 if (!strcmp(data, "PREFERENCE")) {
485 for (i = 0; i < TOMOYO_MAX_PREF; i++)
486 tomoyo_set_uint(&profile->pref[i], cp,
487 tomoyo_pref_keywords[i]);
490 return tomoyo_set_mode(data, cp, profile);
494 * tomoyo_print_config - Print mode for specified functionality.
496 * @head: Pointer to "struct tomoyo_io_buffer".
497 * @config: Mode for that functionality.
501 * Caller prints functionality's name.
503 static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
505 tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
506 tomoyo_mode[config & 3],
507 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
508 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
512 * tomoyo_read_profile - Read profile table.
514 * @head: Pointer to "struct tomoyo_io_buffer".
518 static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
521 const struct tomoyo_profile *profile;
523 index = head->r.index;
524 profile = tomoyo_profile_ptr[index];
525 switch (head->r.step) {
527 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n", 20090903);
531 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
533 if (tomoyo_profile_ptr[head->r.index])
535 if (head->r.index == TOMOYO_MAX_PROFILES)
542 const struct tomoyo_path_info *comment =
544 tomoyo_io_printf(head, "%u-COMMENT=", index);
545 tomoyo_set_string(head, comment ? comment->name : "");
547 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
548 for (i = 0; i < TOMOYO_MAX_PREF; i++)
549 tomoyo_io_printf(head, "%s=%u ",
550 tomoyo_pref_keywords[i],
552 tomoyo_set_string(head, "}\n");
558 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
559 tomoyo_print_config(head, profile->default_config);
565 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
566 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
567 const u8 i = head->r.bit;
568 const u8 config = profile->config[i];
569 if (config == TOMOYO_CONFIG_USE_DEFAULT)
571 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
572 tomoyo_mac_keywords[i]);
573 tomoyo_print_config(head, config);
577 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
578 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
584 if (tomoyo_flush(head))
588 static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
589 const struct tomoyo_acl_head *b)
591 return container_of(a, struct tomoyo_manager, head)->manager ==
592 container_of(b, struct tomoyo_manager, head)->manager;
596 * tomoyo_update_manager_entry - Add a manager entry.
598 * @manager: The path to manager or the domainnamme.
599 * @is_delete: True if it is a delete request.
601 * Returns 0 on success, negative value otherwise.
603 * Caller holds tomoyo_read_lock().
605 static int tomoyo_update_manager_entry(const char *manager,
606 const bool is_delete)
608 struct tomoyo_manager e = { };
609 struct tomoyo_acl_param param = {
610 .is_delete = is_delete,
611 .list = &tomoyo_policy_list[TOMOYO_ID_MANAGER],
613 int error = is_delete ? -ENOENT : -ENOMEM;
614 if (tomoyo_domain_def(manager)) {
615 if (!tomoyo_correct_domain(manager))
619 if (!tomoyo_correct_path(manager))
622 e.manager = tomoyo_get_name(manager);
624 error = tomoyo_update_policy(&e.head, sizeof(e), ¶m,
625 tomoyo_same_manager);
626 tomoyo_put_name(e.manager);
632 * tomoyo_write_manager - Write manager policy.
634 * @head: Pointer to "struct tomoyo_io_buffer".
636 * Returns 0 on success, negative value otherwise.
638 * Caller holds tomoyo_read_lock().
640 static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
642 char *data = head->write_buf;
643 bool is_delete = tomoyo_str_starts(&data, "delete ");
645 if (!strcmp(data, "manage_by_non_root")) {
646 tomoyo_manage_by_non_root = !is_delete;
649 return tomoyo_update_manager_entry(data, is_delete);
653 * tomoyo_read_manager - Read manager policy.
655 * @head: Pointer to "struct tomoyo_io_buffer".
657 * Caller holds tomoyo_read_lock().
659 static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
663 list_for_each_cookie(head->r.acl,
664 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
665 struct tomoyo_manager *ptr =
666 list_entry(head->r.acl, typeof(*ptr), head.list);
667 if (ptr->head.is_deleted)
669 if (!tomoyo_flush(head))
671 tomoyo_set_string(head, ptr->manager->name);
678 * tomoyo_manager - Check whether the current process is a policy manager.
680 * Returns true if the current process is permitted to modify policy
681 * via /sys/kernel/security/tomoyo/ interface.
683 * Caller holds tomoyo_read_lock().
685 static bool tomoyo_manager(void)
687 struct tomoyo_manager *ptr;
689 const struct task_struct *task = current;
690 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
693 if (!tomoyo_policy_loaded)
695 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
697 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
699 if (!ptr->head.is_deleted && ptr->is_domain
700 && !tomoyo_pathcmp(domainname, ptr->manager)) {
707 exe = tomoyo_get_exe();
710 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
712 if (!ptr->head.is_deleted && !ptr->is_domain
713 && !strcmp(exe, ptr->manager->name)) {
718 if (!found) { /* Reduce error messages. */
719 static pid_t last_pid;
720 const pid_t pid = current->pid;
721 if (last_pid != pid) {
722 printk(KERN_WARNING "%s ( %s ) is not permitted to "
723 "update policies.\n", domainname->name, exe);
732 * tomoyo_select_one - Parse select command.
734 * @head: Pointer to "struct tomoyo_io_buffer".
735 * @data: String to parse.
737 * Returns true on success, false otherwise.
739 * Caller holds tomoyo_read_lock().
741 static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
744 struct tomoyo_domain_info *domain = NULL;
745 bool global_pid = false;
747 if (!strcmp(data, "allow_execute")) {
748 head->r.print_execute_only = true;
751 if (sscanf(data, "pid=%u", &pid) == 1 ||
752 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
753 struct task_struct *p;
755 read_lock(&tasklist_lock);
757 p = find_task_by_pid_ns(pid, &init_pid_ns);
759 p = find_task_by_vpid(pid);
761 domain = tomoyo_real_domain(p);
762 read_unlock(&tasklist_lock);
764 } else if (!strncmp(data, "domain=", 7)) {
765 if (tomoyo_domain_def(data + 7))
766 domain = tomoyo_find_domain(data + 7);
769 head->w.domain = domain;
770 /* Accessing read_buf is safe because head->io_sem is held. */
772 return true; /* Do nothing if open(O_WRONLY). */
773 memset(&head->r, 0, sizeof(head->r));
774 head->r.print_this_domain_only = true;
776 head->r.domain = &domain->list;
779 tomoyo_io_printf(head, "# select %s\n", data);
780 if (domain && domain->is_deleted)
781 tomoyo_io_printf(head, "# This is a deleted domain.\n");
786 * tomoyo_delete_domain - Delete a domain.
788 * @domainname: The name of domain.
792 * Caller holds tomoyo_read_lock().
794 static int tomoyo_delete_domain(char *domainname)
796 struct tomoyo_domain_info *domain;
797 struct tomoyo_path_info name;
799 name.name = domainname;
800 tomoyo_fill_path_info(&name);
801 if (mutex_lock_interruptible(&tomoyo_policy_lock))
803 /* Is there an active domain? */
804 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
805 /* Never delete tomoyo_kernel_domain */
806 if (domain == &tomoyo_kernel_domain)
808 if (domain->is_deleted ||
809 tomoyo_pathcmp(domain->domainname, &name))
811 domain->is_deleted = true;
814 mutex_unlock(&tomoyo_policy_lock);
819 * tomoyo_write_domain2 - Write domain policy.
821 * @list: Pointer to "struct list_head".
822 * @data: Policy to be interpreted.
823 * @is_delete: True if it is a delete request.
825 * Returns 0 on success, negative value otherwise.
827 * Caller holds tomoyo_read_lock().
829 static int tomoyo_write_domain2(struct list_head *list, char *data,
830 const bool is_delete)
832 struct tomoyo_acl_param param = {
835 .is_delete = is_delete,
837 static const struct {
839 int (*write) (struct tomoyo_acl_param *);
840 } tomoyo_callback[1] = {
841 { "file ", tomoyo_write_file },
844 for (i = 0; i < 1; i++) {
845 if (!tomoyo_str_starts(¶m.data,
846 tomoyo_callback[i].keyword))
848 return tomoyo_callback[i].write(¶m);
854 * tomoyo_write_domain - Write domain policy.
856 * @head: Pointer to "struct tomoyo_io_buffer".
858 * Returns 0 on success, negative value otherwise.
860 * Caller holds tomoyo_read_lock().
862 static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
864 char *data = head->write_buf;
865 struct tomoyo_domain_info *domain = head->w.domain;
866 bool is_delete = false;
867 bool is_select = false;
868 unsigned int profile;
870 if (tomoyo_str_starts(&data, "delete "))
872 else if (tomoyo_str_starts(&data, "select "))
874 if (is_select && tomoyo_select_one(head, data))
876 /* Don't allow updating policies by non manager programs. */
877 if (!tomoyo_manager())
879 if (tomoyo_domain_def(data)) {
882 tomoyo_delete_domain(data);
884 domain = tomoyo_find_domain(data);
886 domain = tomoyo_assign_domain(data, 0);
887 head->w.domain = domain;
893 if (sscanf(data, "use_profile %u", &profile) == 1
894 && profile < TOMOYO_MAX_PROFILES) {
895 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
896 domain->profile = (u8) profile;
899 if (!strcmp(data, "quota_exceeded")) {
900 domain->quota_warned = !is_delete;
903 if (!strcmp(data, "transition_failed")) {
904 domain->transition_failed = !is_delete;
907 return tomoyo_write_domain2(&domain->acl_info_list, data, is_delete);
911 * tomoyo_set_group - Print category name.
913 * @head: Pointer to "struct tomoyo_io_buffer".
914 * @category: Category name.
918 static void tomoyo_set_group(struct tomoyo_io_buffer *head,
919 const char *category)
921 tomoyo_set_string(head, category);
925 * tomoyo_print_entry - Print an ACL entry.
927 * @head: Pointer to "struct tomoyo_io_buffer".
928 * @acl: Pointer to an ACL entry.
930 * Returns true on success, false otherwise.
932 static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
933 struct tomoyo_acl_info *acl)
935 const u8 acl_type = acl->type;
941 if (!tomoyo_flush(head))
943 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
944 struct tomoyo_path_acl *ptr =
945 container_of(acl, typeof(*ptr), head);
946 const u16 perm = ptr->perm;
947 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
948 if (!(perm & (1 << bit)))
950 if (head->r.print_execute_only &&
951 bit != TOMOYO_TYPE_EXECUTE)
954 tomoyo_set_group(head, "file ");
957 tomoyo_set_slash(head);
959 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
963 tomoyo_print_name_union(head, &ptr->name);
964 } else if (head->r.print_execute_only) {
966 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
967 struct tomoyo_path2_acl *ptr =
968 container_of(acl, typeof(*ptr), head);
969 const u8 perm = ptr->perm;
970 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
971 if (!(perm & (1 << bit)))
974 tomoyo_set_group(head, "file ");
977 tomoyo_set_slash(head);
979 tomoyo_set_string(head, tomoyo_mac_keywords
980 [tomoyo_pp2mac[bit]]);
984 tomoyo_print_name_union(head, &ptr->name1);
985 tomoyo_print_name_union(head, &ptr->name2);
986 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
987 struct tomoyo_path_number_acl *ptr =
988 container_of(acl, typeof(*ptr), head);
989 const u8 perm = ptr->perm;
990 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
991 if (!(perm & (1 << bit)))
994 tomoyo_set_group(head, "file ");
997 tomoyo_set_slash(head);
999 tomoyo_set_string(head, tomoyo_mac_keywords
1000 [tomoyo_pn2mac[bit]]);
1004 tomoyo_print_name_union(head, &ptr->name);
1005 tomoyo_print_number_union(head, &ptr->number);
1006 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1007 struct tomoyo_mkdev_acl *ptr =
1008 container_of(acl, typeof(*ptr), head);
1009 const u8 perm = ptr->perm;
1010 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
1011 if (!(perm & (1 << bit)))
1014 tomoyo_set_group(head, "file ");
1017 tomoyo_set_slash(head);
1019 tomoyo_set_string(head, tomoyo_mac_keywords
1020 [tomoyo_pnnn2mac[bit]]);
1024 tomoyo_print_name_union(head, &ptr->name);
1025 tomoyo_print_number_union(head, &ptr->mode);
1026 tomoyo_print_number_union(head, &ptr->major);
1027 tomoyo_print_number_union(head, &ptr->minor);
1028 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1029 struct tomoyo_mount_acl *ptr =
1030 container_of(acl, typeof(*ptr), head);
1031 tomoyo_set_group(head, "file mount");
1032 tomoyo_print_name_union(head, &ptr->dev_name);
1033 tomoyo_print_name_union(head, &ptr->dir_name);
1034 tomoyo_print_name_union(head, &ptr->fs_type);
1035 tomoyo_print_number_union(head, &ptr->flags);
1037 tomoyo_set_lf(head);
1042 * tomoyo_read_domain2 - Read domain policy.
1044 * @head: Pointer to "struct tomoyo_io_buffer".
1045 * @domain: Pointer to "struct tomoyo_domain_info".
1047 * Caller holds tomoyo_read_lock().
1049 * Returns true on success, false otherwise.
1051 static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1052 struct tomoyo_domain_info *domain)
1054 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1055 struct tomoyo_acl_info *ptr =
1056 list_entry(head->r.acl, typeof(*ptr), list);
1057 if (!tomoyo_print_entry(head, ptr))
1065 * tomoyo_read_domain - Read domain policy.
1067 * @head: Pointer to "struct tomoyo_io_buffer".
1069 * Caller holds tomoyo_read_lock().
1071 static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
1075 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1076 struct tomoyo_domain_info *domain =
1077 list_entry(head->r.domain, typeof(*domain), list);
1078 switch (head->r.step) {
1080 if (domain->is_deleted &&
1081 !head->r.print_this_domain_only)
1083 /* Print domainname and flags. */
1084 tomoyo_set_string(head, domain->domainname->name);
1085 tomoyo_set_lf(head);
1086 tomoyo_io_printf(head, "use_profile %u\n",
1088 if (domain->quota_warned)
1089 tomoyo_set_string(head, "quota_exceeded\n");
1090 if (domain->transition_failed)
1091 tomoyo_set_string(head, "transition_failed\n");
1093 tomoyo_set_lf(head);
1096 if (!tomoyo_read_domain2(head, domain))
1099 if (!tomoyo_set_lf(head))
1104 if (head->r.print_this_domain_only)
1113 * tomoyo_write_domain_profile - Assign profile for specified domain.
1115 * @head: Pointer to "struct tomoyo_io_buffer".
1117 * Returns 0 on success, -EINVAL otherwise.
1119 * This is equivalent to doing
1121 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1122 * /usr/sbin/tomoyo-loadpolicy -d
1124 * Caller holds tomoyo_read_lock().
1126 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1128 char *data = head->write_buf;
1129 char *cp = strchr(data, ' ');
1130 struct tomoyo_domain_info *domain;
1131 unsigned long profile;
1136 domain = tomoyo_find_domain(cp + 1);
1137 if (strict_strtoul(data, 10, &profile))
1139 if (domain && profile < TOMOYO_MAX_PROFILES
1140 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1141 domain->profile = (u8) profile;
1146 * tomoyo_read_domain_profile - Read only domainname and profile.
1148 * @head: Pointer to "struct tomoyo_io_buffer".
1150 * Returns list of profile number and domainname pairs.
1152 * This is equivalent to doing
1154 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1155 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1156 * domainname = $0; } else if ( $1 == "use_profile" ) {
1157 * print $2 " " domainname; domainname = ""; } } ; '
1159 * Caller holds tomoyo_read_lock().
1161 static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1165 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1166 struct tomoyo_domain_info *domain =
1167 list_entry(head->r.domain, typeof(*domain), list);
1168 if (domain->is_deleted)
1170 if (!tomoyo_flush(head))
1172 tomoyo_io_printf(head, "%u ", domain->profile);
1173 tomoyo_set_string(head, domain->domainname->name);
1174 tomoyo_set_lf(head);
1180 * tomoyo_write_pid: Specify PID to obtain domainname.
1182 * @head: Pointer to "struct tomoyo_io_buffer".
1186 static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1188 head->r.eof = false;
1193 * tomoyo_read_pid - Get domainname of the specified PID.
1195 * @head: Pointer to "struct tomoyo_io_buffer".
1197 * Returns the domainname which the specified PID is in on success,
1198 * empty string otherwise.
1199 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1200 * using read()/write() interface rather than sysctl() interface.
1202 static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
1204 char *buf = head->write_buf;
1205 bool global_pid = false;
1207 struct task_struct *p;
1208 struct tomoyo_domain_info *domain = NULL;
1210 /* Accessing write_buf is safe because head->io_sem is held. */
1213 return; /* Do nothing if open(O_RDONLY). */
1215 if (head->r.w_pos || head->r.eof)
1218 if (tomoyo_str_starts(&buf, "global-pid "))
1220 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1222 read_lock(&tasklist_lock);
1224 p = find_task_by_pid_ns(pid, &init_pid_ns);
1226 p = find_task_by_vpid(pid);
1228 domain = tomoyo_real_domain(p);
1229 read_unlock(&tasklist_lock);
1233 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1234 tomoyo_set_string(head, domain->domainname->name);
1237 static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1238 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain",
1239 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain",
1240 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain",
1241 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain",
1244 static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1245 [TOMOYO_PATH_GROUP] = "path_group ",
1246 [TOMOYO_NUMBER_GROUP] = "number_group ",
1250 * tomoyo_write_exception - Write exception policy.
1252 * @head: Pointer to "struct tomoyo_io_buffer".
1254 * Returns 0 on success, negative value otherwise.
1256 * Caller holds tomoyo_read_lock().
1258 static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
1260 struct tomoyo_acl_param param = {
1261 .data = head->write_buf,
1264 param.is_delete = tomoyo_str_starts(¶m.data, "delete ");
1265 if (tomoyo_str_starts(¶m.data, "aggregator "))
1266 return tomoyo_write_aggregator(¶m);
1267 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1268 if (tomoyo_str_starts(¶m.data, tomoyo_transition_type[i]))
1269 return tomoyo_write_transition_control(¶m, i);
1270 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1271 if (tomoyo_str_starts(¶m.data, tomoyo_group_name[i]))
1272 return tomoyo_write_group(¶m, i);
1277 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1279 * @head: Pointer to "struct tomoyo_io_buffer".
1280 * @idx: Index number.
1282 * Returns true on success, false otherwise.
1284 * Caller holds tomoyo_read_lock().
1286 static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1288 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
1289 struct tomoyo_group *group =
1290 list_entry(head->r.group, typeof(*group), head.list);
1291 list_for_each_cookie(head->r.acl, &group->member_list) {
1292 struct tomoyo_acl_head *ptr =
1293 list_entry(head->r.acl, typeof(*ptr), list);
1294 if (ptr->is_deleted)
1296 if (!tomoyo_flush(head))
1298 tomoyo_set_string(head, tomoyo_group_name[idx]);
1299 tomoyo_set_string(head, group->group_name->name);
1300 if (idx == TOMOYO_PATH_GROUP) {
1301 tomoyo_set_space(head);
1302 tomoyo_set_string(head, container_of
1303 (ptr, struct tomoyo_path_group,
1304 head)->member_name->name);
1305 } else if (idx == TOMOYO_NUMBER_GROUP) {
1306 tomoyo_print_number_union(head, &container_of
1308 struct tomoyo_number_group,
1311 tomoyo_set_lf(head);
1315 head->r.group = NULL;
1320 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1322 * @head: Pointer to "struct tomoyo_io_buffer".
1323 * @idx: Index number.
1325 * Returns true on success, false otherwise.
1327 * Caller holds tomoyo_read_lock().
1329 static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1331 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
1332 struct tomoyo_acl_head *acl =
1333 container_of(head->r.acl, typeof(*acl), list);
1334 if (acl->is_deleted)
1336 if (!tomoyo_flush(head))
1339 case TOMOYO_ID_TRANSITION_CONTROL:
1341 struct tomoyo_transition_control *ptr =
1342 container_of(acl, typeof(*ptr), head);
1343 tomoyo_set_string(head, tomoyo_transition_type
1345 tomoyo_set_string(head, ptr->program ?
1346 ptr->program->name : "any");
1347 tomoyo_set_string(head, " from ");
1348 tomoyo_set_string(head, ptr->domainname ?
1349 ptr->domainname->name :
1353 case TOMOYO_ID_AGGREGATOR:
1355 struct tomoyo_aggregator *ptr =
1356 container_of(acl, typeof(*ptr), head);
1357 tomoyo_set_string(head, "aggregator ");
1358 tomoyo_set_string(head,
1359 ptr->original_name->name);
1360 tomoyo_set_space(head);
1361 tomoyo_set_string(head,
1362 ptr->aggregated_name->name);
1368 tomoyo_set_lf(head);
1375 * tomoyo_read_exception - Read exception policy.
1377 * @head: Pointer to "struct tomoyo_io_buffer".
1379 * Caller holds tomoyo_read_lock().
1381 static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
1385 while (head->r.step < TOMOYO_MAX_POLICY &&
1386 tomoyo_read_policy(head, head->r.step))
1388 if (head->r.step < TOMOYO_MAX_POLICY)
1390 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1391 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1393 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1398 /* Wait queue for kernel -> userspace notification. */
1399 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1400 /* Wait queue for userspace -> kernel notification. */
1401 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
1403 /* Structure for query. */
1404 struct tomoyo_query {
1405 struct list_head list;
1408 unsigned int serial;
1414 /* The list for "struct tomoyo_query". */
1415 static LIST_HEAD(tomoyo_query_list);
1417 /* Lock for manipulating tomoyo_query_list. */
1418 static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1421 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1424 static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1427 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1429 * @domain: Pointer to "struct tomoyo_domain_info".
1430 * @header: Lines containing ACL.
1434 static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1437 char *cp = strchr(header, '\n');
1441 cp = strchr(cp + 1, '\n');
1445 len = strlen(cp) + 1;
1446 buffer = kmalloc(len, GFP_NOFS);
1449 snprintf(buffer, len - 1, "%s", cp);
1450 tomoyo_normalize_line(buffer);
1451 tomoyo_write_domain2(&domain->acl_info_list, buffer, false);
1456 * tomoyo_supervisor - Ask for the supervisor's decision.
1458 * @r: Pointer to "struct tomoyo_request_info".
1459 * @fmt: The printf()'s format string, followed by parameters.
1461 * Returns 0 if the supervisor decided to permit the access request which
1462 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1463 * supervisor decided to retry the access request which violated the policy in
1464 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1466 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1471 static unsigned int tomoyo_serial;
1472 struct tomoyo_query entry = { };
1473 bool quota_exceeded = false;
1474 va_start(args, fmt);
1475 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
1477 /* Write /sys/kernel/security/tomoyo/audit. */
1478 va_start(args, fmt);
1479 tomoyo_write_log2(r, len, fmt, args);
1481 /* Nothing more to do if granted. */
1485 case TOMOYO_CONFIG_ENFORCING:
1487 if (atomic_read(&tomoyo_query_observers))
1490 case TOMOYO_CONFIG_LEARNING:
1492 /* Check max_learning_entry parameter. */
1493 if (tomoyo_domain_quota_is_ok(r))
1500 va_start(args, fmt);
1501 entry.query = tomoyo_init_log(r, len, fmt, args);
1505 entry.query_len = strlen(entry.query) + 1;
1507 tomoyo_add_entry(r->domain, entry.query);
1510 len = tomoyo_round2(entry.query_len);
1511 spin_lock(&tomoyo_query_list_lock);
1512 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
1513 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
1514 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
1515 quota_exceeded = true;
1517 entry.serial = tomoyo_serial++;
1518 entry.retry = r->retry;
1519 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
1520 list_add_tail(&entry.list, &tomoyo_query_list);
1522 spin_unlock(&tomoyo_query_list_lock);
1525 /* Give 10 seconds for supervisor's opinion. */
1526 while (entry.timer < 10) {
1527 wake_up_all(&tomoyo_query_wait);
1528 if (wait_event_interruptible_timeout
1529 (tomoyo_answer_wait, entry.answer ||
1530 !atomic_read(&tomoyo_query_observers), HZ))
1535 spin_lock(&tomoyo_query_list_lock);
1536 list_del(&entry.list);
1537 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
1538 spin_unlock(&tomoyo_query_list_lock);
1539 switch (entry.answer) {
1540 case 3: /* Asked to retry by administrator. */
1541 error = TOMOYO_RETRY_REQUEST;
1545 /* Granted by administrator. */
1549 /* Timed out or rejected by administrator. */
1558 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1560 * @file: Pointer to "struct file".
1561 * @wait: Pointer to "poll_table".
1563 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1565 * Waits for access requests which violated policy in enforcing mode.
1567 static int tomoyo_poll_query(struct file *file, poll_table *wait)
1569 struct list_head *tmp;
1572 for (i = 0; i < 2; i++) {
1573 spin_lock(&tomoyo_query_list_lock);
1574 list_for_each(tmp, &tomoyo_query_list) {
1575 struct tomoyo_query *ptr =
1576 list_entry(tmp, typeof(*ptr), list);
1582 spin_unlock(&tomoyo_query_list_lock);
1584 return POLLIN | POLLRDNORM;
1587 poll_wait(file, &tomoyo_query_wait, wait);
1593 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1595 * @head: Pointer to "struct tomoyo_io_buffer".
1597 static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1599 struct list_head *tmp;
1605 if (head->read_buf) {
1606 kfree(head->read_buf);
1607 head->read_buf = NULL;
1609 spin_lock(&tomoyo_query_list_lock);
1610 list_for_each(tmp, &tomoyo_query_list) {
1611 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1614 if (pos++ != head->r.query_index)
1616 len = ptr->query_len;
1619 spin_unlock(&tomoyo_query_list_lock);
1621 head->r.query_index = 0;
1624 buf = kzalloc(len + 32, GFP_NOFS);
1628 spin_lock(&tomoyo_query_list_lock);
1629 list_for_each(tmp, &tomoyo_query_list) {
1630 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1633 if (pos++ != head->r.query_index)
1636 * Some query can be skipped because tomoyo_query_list
1637 * can change, but I don't care.
1639 if (len == ptr->query_len)
1640 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
1641 ptr->retry, ptr->query);
1644 spin_unlock(&tomoyo_query_list_lock);
1646 head->read_buf = buf;
1647 head->r.w[head->r.w_pos++] = buf;
1648 head->r.query_index++;
1655 * tomoyo_write_answer - Write the supervisor's decision.
1657 * @head: Pointer to "struct tomoyo_io_buffer".
1659 * Returns 0 on success, -EINVAL otherwise.
1661 static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1663 char *data = head->write_buf;
1664 struct list_head *tmp;
1665 unsigned int serial;
1666 unsigned int answer;
1667 spin_lock(&tomoyo_query_list_lock);
1668 list_for_each(tmp, &tomoyo_query_list) {
1669 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1672 spin_unlock(&tomoyo_query_list_lock);
1673 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1675 spin_lock(&tomoyo_query_list_lock);
1676 list_for_each(tmp, &tomoyo_query_list) {
1677 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
1678 if (ptr->serial != serial)
1681 ptr->answer = answer;
1684 spin_unlock(&tomoyo_query_list_lock);
1689 * tomoyo_read_version: Get version.
1691 * @head: Pointer to "struct tomoyo_io_buffer".
1693 * Returns version information.
1695 static void tomoyo_read_version(struct tomoyo_io_buffer *head)
1698 tomoyo_io_printf(head, "2.4.0");
1704 * tomoyo_read_self_domain - Get the current process's domainname.
1706 * @head: Pointer to "struct tomoyo_io_buffer".
1708 * Returns the current process's domainname.
1710 static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1714 * tomoyo_domain()->domainname != NULL
1715 * because every process belongs to a domain and
1716 * the domain's name cannot be NULL.
1718 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1724 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1726 * @type: Type of interface.
1727 * @file: Pointer to "struct file".
1729 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1731 * Caller acquires tomoyo_read_lock().
1733 int tomoyo_open_control(const u8 type, struct file *file)
1735 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
1739 mutex_init(&head->io_sem);
1742 case TOMOYO_DOMAINPOLICY:
1743 /* /sys/kernel/security/tomoyo/domain_policy */
1744 head->write = tomoyo_write_domain;
1745 head->read = tomoyo_read_domain;
1747 case TOMOYO_EXCEPTIONPOLICY:
1748 /* /sys/kernel/security/tomoyo/exception_policy */
1749 head->write = tomoyo_write_exception;
1750 head->read = tomoyo_read_exception;
1753 /* /sys/kernel/security/tomoyo/audit */
1754 head->poll = tomoyo_poll_log;
1755 head->read = tomoyo_read_log;
1757 case TOMOYO_SELFDOMAIN:
1758 /* /sys/kernel/security/tomoyo/self_domain */
1759 head->read = tomoyo_read_self_domain;
1761 case TOMOYO_DOMAIN_STATUS:
1762 /* /sys/kernel/security/tomoyo/.domain_status */
1763 head->write = tomoyo_write_domain_profile;
1764 head->read = tomoyo_read_domain_profile;
1766 case TOMOYO_PROCESS_STATUS:
1767 /* /sys/kernel/security/tomoyo/.process_status */
1768 head->write = tomoyo_write_pid;
1769 head->read = tomoyo_read_pid;
1771 case TOMOYO_VERSION:
1772 /* /sys/kernel/security/tomoyo/version */
1773 head->read = tomoyo_read_version;
1774 head->readbuf_size = 128;
1776 case TOMOYO_MEMINFO:
1777 /* /sys/kernel/security/tomoyo/meminfo */
1778 head->write = tomoyo_write_memory_quota;
1779 head->read = tomoyo_read_memory_counter;
1780 head->readbuf_size = 512;
1782 case TOMOYO_PROFILE:
1783 /* /sys/kernel/security/tomoyo/profile */
1784 head->write = tomoyo_write_profile;
1785 head->read = tomoyo_read_profile;
1787 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1788 head->poll = tomoyo_poll_query;
1789 head->write = tomoyo_write_answer;
1790 head->read = tomoyo_read_query;
1792 case TOMOYO_MANAGER:
1793 /* /sys/kernel/security/tomoyo/manager */
1794 head->write = tomoyo_write_manager;
1795 head->read = tomoyo_read_manager;
1798 if (!(file->f_mode & FMODE_READ)) {
1800 * No need to allocate read_buf since it is not opened
1805 } else if (!head->poll) {
1806 /* Don't allocate read_buf for poll() access. */
1807 if (!head->readbuf_size)
1808 head->readbuf_size = 4096 * 2;
1809 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
1810 if (!head->read_buf) {
1815 if (!(file->f_mode & FMODE_WRITE)) {
1817 * No need to allocate write_buf since it is not opened
1821 } else if (head->write) {
1822 head->writebuf_size = 4096 * 2;
1823 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
1824 if (!head->write_buf) {
1825 kfree(head->read_buf);
1830 if (type != TOMOYO_QUERY && type != TOMOYO_AUDIT)
1831 head->reader_idx = tomoyo_read_lock();
1832 file->private_data = head;
1834 * If the file is /sys/kernel/security/tomoyo/query , increment the
1836 * The obserber counter is used by tomoyo_supervisor() to see if
1837 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1839 if (type == TOMOYO_QUERY)
1840 atomic_inc(&tomoyo_query_observers);
1845 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1847 * @file: Pointer to "struct file".
1848 * @wait: Pointer to "poll_table".
1850 * Waits for read readiness.
1851 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
1852 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
1854 int tomoyo_poll_control(struct file *file, poll_table *wait)
1856 struct tomoyo_io_buffer *head = file->private_data;
1859 return head->poll(file, wait);
1863 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1865 * @head: Pointer to "struct tomoyo_io_buffer".
1866 * @buffer: Poiner to buffer to write to.
1867 * @buffer_len: Size of @buffer.
1869 * Returns bytes read on success, negative value otherwise.
1871 * Caller holds tomoyo_read_lock().
1873 int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
1874 const int buffer_len)
1880 if (mutex_lock_interruptible(&head->io_sem))
1882 head->read_user_buf = buffer;
1883 head->read_user_buf_avail = buffer_len;
1884 if (tomoyo_flush(head))
1885 /* Call the policy handler. */
1888 len = head->read_user_buf - buffer;
1889 mutex_unlock(&head->io_sem);
1894 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1896 * @head: Pointer to "struct tomoyo_io_buffer".
1897 * @buffer: Pointer to buffer to read from.
1898 * @buffer_len: Size of @buffer.
1900 * Returns @buffer_len on success, negative value otherwise.
1902 * Caller holds tomoyo_read_lock().
1904 int tomoyo_write_control(struct tomoyo_io_buffer *head,
1905 const char __user *buffer, const int buffer_len)
1907 int error = buffer_len;
1908 int avail_len = buffer_len;
1909 char *cp0 = head->write_buf;
1913 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1915 /* Don't allow updating policies by non manager programs. */
1916 if (head->write != tomoyo_write_pid &&
1917 head->write != tomoyo_write_domain && !tomoyo_manager())
1919 if (mutex_lock_interruptible(&head->io_sem))
1921 /* Read a line and dispatch it to the policy handler. */
1922 while (avail_len > 0) {
1924 if (head->w.avail >= head->writebuf_size - 1) {
1927 } else if (get_user(c, buffer)) {
1933 cp0[head->w.avail++] = c;
1936 cp0[head->w.avail - 1] = '\0';
1938 tomoyo_normalize_line(cp0);
1941 mutex_unlock(&head->io_sem);
1946 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1948 * @head: Pointer to "struct tomoyo_io_buffer".
1950 * Releases memory and returns 0.
1952 * Caller looses tomoyo_read_lock().
1954 int tomoyo_close_control(struct tomoyo_io_buffer *head)
1956 const bool is_write = !!head->write_buf;
1959 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1962 if (head->type == TOMOYO_QUERY)
1963 atomic_dec(&tomoyo_query_observers);
1964 else if (head->type != TOMOYO_AUDIT)
1965 tomoyo_read_unlock(head->reader_idx);
1966 /* Release memory used for policy I/O. */
1967 kfree(head->read_buf);
1968 head->read_buf = NULL;
1969 kfree(head->write_buf);
1970 head->write_buf = NULL;
1978 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
1980 void tomoyo_check_profile(void)
1982 struct tomoyo_domain_info *domain;
1983 const int idx = tomoyo_read_lock();
1984 tomoyo_policy_loaded = true;
1985 /* Check all profiles currently assigned to domains are defined. */
1986 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1987 const u8 profile = domain->profile;
1988 if (tomoyo_profile_ptr[profile])
1990 printk(KERN_ERR "You need to define profile %u before using it.\n",
1992 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
1993 "for more information.\n");
1994 panic("Profile %u (used by '%s') not defined.\n",
1995 profile, domain->domainname->name);
1997 tomoyo_read_unlock(idx);
1998 if (tomoyo_profile_version != 20090903) {
1999 printk(KERN_ERR "You need to install userland programs for "
2000 "TOMOYO 2.3 and initialize policy configuration.\n");
2001 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2002 "for more information.\n");
2003 panic("Profile version %u is not supported.\n",
2004 tomoyo_profile_version);
2006 printk(KERN_INFO "TOMOYO: 2.3.0\n");
2007 printk(KERN_INFO "Mandatory Access Control activated.\n");