2 * security/tomoyo/common.c
4 * Common functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/uaccess.h>
10 #include <linux/slab.h>
11 #include <linux/security.h>
14 static struct tomoyo_profile tomoyo_default_profile = {
15 .learning = &tomoyo_default_profile.preference,
16 .permissive = &tomoyo_default_profile.preference,
17 .enforcing = &tomoyo_default_profile.preference,
18 .preference.enforcing_verbose = true,
19 .preference.learning_max_entry = 2048,
20 .preference.learning_verbose = false,
21 .preference.permissive_verbose = true
24 /* Profile version. Currently only 20090903 is defined. */
25 static unsigned int tomoyo_profile_version;
27 /* Profile table. Memory is allocated as needed. */
28 static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
30 /* String table for functionality that takes 4 modes. */
31 static const char *tomoyo_mode[4] = {
32 "disabled", "learning", "permissive", "enforcing"
35 /* String table for /sys/kernel/security/tomoyo/profile */
36 static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
37 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
38 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
39 [TOMOYO_MAC_FILE_OPEN] = "file::open",
40 [TOMOYO_MAC_FILE_CREATE] = "file::create",
41 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
42 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
43 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
44 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
45 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
46 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
47 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
48 [TOMOYO_MAC_FILE_REWRITE] = "file::rewrite",
49 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
50 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
51 [TOMOYO_MAC_FILE_LINK] = "file::link",
52 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
53 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
54 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
55 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
56 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
57 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
58 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
59 [TOMOYO_MAC_FILE_UMOUNT] = "file::umount",
60 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
61 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
64 /* Permit policy management by non-root user? */
65 static bool tomoyo_manage_by_non_root;
67 /* Utility functions. */
70 * tomoyo_yesno - Return "yes" or "no".
74 static const char *tomoyo_yesno(const unsigned int value)
76 return value ? "yes" : "no";
79 static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
82 const int pos = strlen(buffer);
84 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
89 * tomoyo_flush - Flush queued string to userspace's buffer.
91 * @head: Pointer to "struct tomoyo_io_buffer".
93 * Returns true if all data was flushed, false otherwise.
95 static bool tomoyo_flush(struct tomoyo_io_buffer *head)
97 while (head->r.w_pos) {
98 const char *w = head->r.w[0];
101 if (len > head->read_user_buf_avail)
102 len = head->read_user_buf_avail;
105 if (copy_to_user(head->read_user_buf, w, len))
107 head->read_user_buf_avail -= len;
108 head->read_user_buf += len;
115 /* Add '\0' for query. */
117 if (!head->read_user_buf_avail ||
118 copy_to_user(head->read_user_buf, "", 1))
120 head->read_user_buf_avail--;
121 head->read_user_buf++;
124 for (len = 0; len < head->r.w_pos; len++)
125 head->r.w[len] = head->r.w[len + 1];
132 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
134 * @head: Pointer to "struct tomoyo_io_buffer".
135 * @string: String to print.
137 * Note that @string has to be kept valid until @head is kfree()d.
138 * This means that char[] allocated on stack memory cannot be passed to
139 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
141 static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
143 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
144 head->r.w[head->r.w_pos++] = string;
151 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
153 * @head: Pointer to "struct tomoyo_io_buffer".
154 * @fmt: The printf()'s format string, followed by parameters.
156 void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
160 int pos = head->r.avail;
161 int size = head->readbuf_size - pos;
165 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
167 if (pos + len >= head->readbuf_size) {
171 head->r.avail += len;
172 tomoyo_set_string(head, head->read_buf + pos);
175 static void tomoyo_set_space(struct tomoyo_io_buffer *head)
177 tomoyo_set_string(head, " ");
180 static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
182 tomoyo_set_string(head, "\n");
183 return !head->r.w_pos;
187 * tomoyo_print_name_union - Print a tomoyo_name_union.
189 * @head: Pointer to "struct tomoyo_io_buffer".
190 * @ptr: Pointer to "struct tomoyo_name_union".
192 static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
193 const struct tomoyo_name_union *ptr)
195 tomoyo_set_space(head);
197 tomoyo_set_string(head, "@");
198 tomoyo_set_string(head, ptr->group->group_name->name);
200 tomoyo_set_string(head, ptr->filename->name);
205 * tomoyo_print_number_union - Print a tomoyo_number_union.
207 * @head: Pointer to "struct tomoyo_io_buffer".
208 * @ptr: Pointer to "struct tomoyo_number_union".
210 static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
211 const struct tomoyo_number_union *ptr)
213 tomoyo_set_space(head);
215 tomoyo_set_string(head, "@");
216 tomoyo_set_string(head, ptr->group->group_name->name);
219 unsigned long min = ptr->values[0];
220 const unsigned long max = ptr->values[1];
221 u8 min_type = ptr->min_type;
222 const u8 max_type = ptr->max_type;
225 for (i = 0; i < 2; i++) {
227 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
228 tomoyo_addprintf(buffer, sizeof(buffer),
231 case TOMOYO_VALUE_TYPE_OCTAL:
232 tomoyo_addprintf(buffer, sizeof(buffer),
236 tomoyo_addprintf(buffer, sizeof(buffer),
240 if (min == max && min_type == max_type)
242 tomoyo_addprintf(buffer, sizeof(buffer), "-");
246 tomoyo_io_printf(head, "%s", buffer);
251 * tomoyo_find_or_assign_new_profile - Create a new profile.
253 * @profile: Profile number to create.
255 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
257 static struct tomoyo_profile *tomoyo_find_or_assign_new_profile
258 (const unsigned int profile)
260 struct tomoyo_profile *ptr;
261 struct tomoyo_profile *entry;
262 if (profile >= TOMOYO_MAX_PROFILES)
264 ptr = tomoyo_profile_ptr[profile];
267 entry = kzalloc(sizeof(*entry), GFP_NOFS);
268 if (mutex_lock_interruptible(&tomoyo_policy_lock))
270 ptr = tomoyo_profile_ptr[profile];
271 if (!ptr && tomoyo_memory_ok(entry)) {
273 ptr->learning = &tomoyo_default_profile.preference;
274 ptr->permissive = &tomoyo_default_profile.preference;
275 ptr->enforcing = &tomoyo_default_profile.preference;
276 ptr->default_config = TOMOYO_CONFIG_DISABLED;
277 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
278 sizeof(ptr->config));
279 mb(); /* Avoid out-of-order execution. */
280 tomoyo_profile_ptr[profile] = ptr;
283 mutex_unlock(&tomoyo_policy_lock);
290 * tomoyo_profile - Find a profile.
292 * @profile: Profile number to find.
294 * Returns pointer to "struct tomoyo_profile".
296 struct tomoyo_profile *tomoyo_profile(const u8 profile)
298 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
299 if (!tomoyo_policy_loaded)
300 return &tomoyo_default_profile;
306 * tomoyo_write_profile - Write profile table.
308 * @head: Pointer to "struct tomoyo_io_buffer".
310 * Returns 0 on success, negative value otherwise.
312 static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
314 char *data = head->write_buf;
319 bool use_default = false;
321 struct tomoyo_profile *profile;
322 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
324 i = simple_strtoul(data, &cp, 10);
326 profile = &tomoyo_default_profile;
331 profile = tomoyo_find_or_assign_new_profile(i);
335 cp = strchr(data, '=');
339 if (profile != &tomoyo_default_profile)
340 use_default = strstr(cp, "use_default") != NULL;
341 if (strstr(cp, "verbose=yes"))
343 else if (strstr(cp, "verbose=no"))
347 if (!strcmp(data, "PREFERENCE::enforcing")) {
349 profile->enforcing = &tomoyo_default_profile.preference;
352 profile->enforcing = &profile->preference;
354 profile->preference.enforcing_verbose = value;
357 if (!strcmp(data, "PREFERENCE::permissive")) {
359 profile->permissive = &tomoyo_default_profile.preference;
362 profile->permissive = &profile->preference;
364 profile->preference.permissive_verbose = value;
367 if (!strcmp(data, "PREFERENCE::learning")) {
370 profile->learning = &tomoyo_default_profile.preference;
373 profile->learning = &profile->preference;
375 profile->preference.learning_verbose = value;
376 cp2 = strstr(cp, "max_entry=");
378 sscanf(cp2 + 10, "%u",
379 &profile->preference.learning_max_entry);
382 if (profile == &tomoyo_default_profile)
384 if (!strcmp(data, "COMMENT")) {
385 const struct tomoyo_path_info *old_comment = profile->comment;
386 profile->comment = tomoyo_get_name(cp);
387 tomoyo_put_name(old_comment);
390 if (!strcmp(data, "CONFIG")) {
391 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
392 config = profile->default_config;
393 } else if (tomoyo_str_starts(&data, "CONFIG::")) {
395 for (i = 0; i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
396 if (strcmp(data, tomoyo_mac_keywords[i]))
398 config = profile->config[i];
401 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
407 config = TOMOYO_CONFIG_USE_DEFAULT;
409 for (mode = 3; mode >= 0; mode--)
410 if (strstr(cp, tomoyo_mode[mode]))
412 * Update lower 3 bits in order to distinguish
413 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
415 config = (config & ~7) | mode;
417 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
418 profile->config[i] = config;
419 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
420 profile->default_config = config;
424 static void tomoyo_print_preference(struct tomoyo_io_buffer *head,
427 struct tomoyo_preference *pref = &tomoyo_default_profile.preference;
428 const struct tomoyo_profile *profile = idx >= 0 ?
429 tomoyo_profile_ptr[idx] : NULL;
430 char buffer[16] = "";
432 buffer[sizeof(buffer) - 1] = '\0';
433 snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
436 pref = profile->learning;
437 if (pref == &tomoyo_default_profile.preference)
440 tomoyo_io_printf(head, "%sPREFERENCE::%s={ "
441 "verbose=%s max_entry=%u }\n",
443 tomoyo_yesno(pref->learning_verbose),
444 pref->learning_max_entry);
447 pref = profile->permissive;
448 if (pref == &tomoyo_default_profile.preference)
451 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
452 buffer, "permissive",
453 tomoyo_yesno(pref->permissive_verbose));
456 pref = profile->enforcing;
457 if (pref == &tomoyo_default_profile.preference)
460 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
462 tomoyo_yesno(pref->enforcing_verbose));
465 static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
467 tomoyo_io_printf(head, "={ mode=%s }\n", tomoyo_mode[config & 3]);
471 * tomoyo_read_profile - Read profile table.
473 * @head: Pointer to "struct tomoyo_io_buffer".
475 static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
478 const struct tomoyo_profile *profile;
480 index = head->r.index;
481 profile = tomoyo_profile_ptr[index];
482 switch (head->r.step) {
484 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
485 tomoyo_print_preference(head, -1);
489 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
491 if (tomoyo_profile_ptr[head->r.index])
493 if (head->r.index == TOMOYO_MAX_PROFILES)
499 const struct tomoyo_path_info *comment =
501 tomoyo_io_printf(head, "%u-COMMENT=", index);
502 tomoyo_set_string(head, comment ? comment->name : "");
509 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
510 tomoyo_print_config(head, profile->default_config);
516 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
517 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
518 const u8 i = head->r.bit;
519 const u8 config = profile->config[i];
520 if (config == TOMOYO_CONFIG_USE_DEFAULT)
522 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
523 tomoyo_mac_keywords[i]);
524 tomoyo_print_config(head, config);
528 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
529 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
530 tomoyo_print_preference(head, index);
536 if (tomoyo_flush(head))
540 static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head *a,
541 const struct tomoyo_acl_head *b)
543 return container_of(a, struct tomoyo_policy_manager_entry, head)
545 container_of(b, struct tomoyo_policy_manager_entry, head)
550 * tomoyo_update_manager_entry - Add a manager entry.
552 * @manager: The path to manager or the domainnamme.
553 * @is_delete: True if it is a delete request.
555 * Returns 0 on success, negative value otherwise.
557 * Caller holds tomoyo_read_lock().
559 static int tomoyo_update_manager_entry(const char *manager,
560 const bool is_delete)
562 struct tomoyo_policy_manager_entry e = { };
565 if (tomoyo_domain_def(manager)) {
566 if (!tomoyo_correct_domain(manager))
570 if (!tomoyo_correct_path(manager))
573 e.manager = tomoyo_get_name(manager);
576 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
577 &tomoyo_policy_list[TOMOYO_ID_MANAGER],
578 tomoyo_same_manager_entry);
579 tomoyo_put_name(e.manager);
584 * tomoyo_write_manager_policy - Write manager policy.
586 * @head: Pointer to "struct tomoyo_io_buffer".
588 * Returns 0 on success, negative value otherwise.
590 * Caller holds tomoyo_read_lock().
592 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
594 char *data = head->write_buf;
595 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
597 if (!strcmp(data, "manage_by_non_root")) {
598 tomoyo_manage_by_non_root = !is_delete;
601 return tomoyo_update_manager_entry(data, is_delete);
605 * tomoyo_read_manager_policy - Read manager policy.
607 * @head: Pointer to "struct tomoyo_io_buffer".
609 * Caller holds tomoyo_read_lock().
611 static void tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
615 list_for_each_cookie(head->r.acl,
616 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
617 struct tomoyo_policy_manager_entry *ptr =
618 list_entry(head->r.acl, typeof(*ptr), head.list);
619 if (ptr->head.is_deleted)
621 if (!tomoyo_flush(head))
623 tomoyo_set_string(head, ptr->manager->name);
630 * tomoyo_policy_manager - Check whether the current process is a policy manager.
632 * Returns true if the current process is permitted to modify policy
633 * via /sys/kernel/security/tomoyo/ interface.
635 * Caller holds tomoyo_read_lock().
637 static bool tomoyo_policy_manager(void)
639 struct tomoyo_policy_manager_entry *ptr;
641 const struct task_struct *task = current;
642 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
645 if (!tomoyo_policy_loaded)
647 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
649 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
651 if (!ptr->head.is_deleted && ptr->is_domain
652 && !tomoyo_pathcmp(domainname, ptr->manager)) {
659 exe = tomoyo_get_exe();
662 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
664 if (!ptr->head.is_deleted && !ptr->is_domain
665 && !strcmp(exe, ptr->manager->name)) {
670 if (!found) { /* Reduce error messages. */
671 static pid_t last_pid;
672 const pid_t pid = current->pid;
673 if (last_pid != pid) {
674 printk(KERN_WARNING "%s ( %s ) is not permitted to "
675 "update policies.\n", domainname->name, exe);
684 * tomoyo_select_one - Parse select command.
686 * @head: Pointer to "struct tomoyo_io_buffer".
687 * @data: String to parse.
689 * Returns true on success, false otherwise.
691 * Caller holds tomoyo_read_lock().
693 static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
696 struct tomoyo_domain_info *domain = NULL;
697 bool global_pid = false;
699 if (!strcmp(data, "allow_execute")) {
700 head->r.print_execute_only = true;
703 if (sscanf(data, "pid=%u", &pid) == 1 ||
704 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
705 struct task_struct *p;
707 read_lock(&tasklist_lock);
709 p = find_task_by_pid_ns(pid, &init_pid_ns);
711 p = find_task_by_vpid(pid);
713 domain = tomoyo_real_domain(p);
714 read_unlock(&tasklist_lock);
716 } else if (!strncmp(data, "domain=", 7)) {
717 if (tomoyo_domain_def(data + 7))
718 domain = tomoyo_find_domain(data + 7);
721 head->write_var1 = domain;
722 /* Accessing read_buf is safe because head->io_sem is held. */
724 return true; /* Do nothing if open(O_WRONLY). */
725 memset(&head->r, 0, sizeof(head->r));
726 head->r.print_this_domain_only = true;
727 head->r.eof = !domain;
728 head->r.domain = &domain->list;
729 tomoyo_io_printf(head, "# select %s\n", data);
730 if (domain && domain->is_deleted)
731 tomoyo_io_printf(head, "# This is a deleted domain.\n");
736 * tomoyo_delete_domain - Delete a domain.
738 * @domainname: The name of domain.
742 * Caller holds tomoyo_read_lock().
744 static int tomoyo_delete_domain(char *domainname)
746 struct tomoyo_domain_info *domain;
747 struct tomoyo_path_info name;
749 name.name = domainname;
750 tomoyo_fill_path_info(&name);
751 if (mutex_lock_interruptible(&tomoyo_policy_lock))
753 /* Is there an active domain? */
754 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
755 /* Never delete tomoyo_kernel_domain */
756 if (domain == &tomoyo_kernel_domain)
758 if (domain->is_deleted ||
759 tomoyo_pathcmp(domain->domainname, &name))
761 domain->is_deleted = true;
764 mutex_unlock(&tomoyo_policy_lock);
769 * tomoyo_write_domain_policy2 - Write domain policy.
771 * @head: Pointer to "struct tomoyo_io_buffer".
773 * Returns 0 on success, negative value otherwise.
775 * Caller holds tomoyo_read_lock().
777 static int tomoyo_write_domain_policy2(char *data,
778 struct tomoyo_domain_info *domain,
779 const bool is_delete)
781 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
782 return tomoyo_write_mount_policy(data, domain, is_delete);
783 return tomoyo_write_file_policy(data, domain, is_delete);
787 * tomoyo_write_domain_policy - Write domain policy.
789 * @head: Pointer to "struct tomoyo_io_buffer".
791 * Returns 0 on success, negative value otherwise.
793 * Caller holds tomoyo_read_lock().
795 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
797 char *data = head->write_buf;
798 struct tomoyo_domain_info *domain = head->write_var1;
799 bool is_delete = false;
800 bool is_select = false;
801 unsigned int profile;
803 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
805 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
807 if (is_select && tomoyo_select_one(head, data))
809 /* Don't allow updating policies by non manager programs. */
810 if (!tomoyo_policy_manager())
812 if (tomoyo_domain_def(data)) {
815 tomoyo_delete_domain(data);
817 domain = tomoyo_find_domain(data);
819 domain = tomoyo_find_or_assign_new_domain(data, 0);
820 head->write_var1 = domain;
826 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
827 && profile < TOMOYO_MAX_PROFILES) {
828 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
829 domain->profile = (u8) profile;
832 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
833 domain->ignore_global_allow_read = !is_delete;
836 if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
837 domain->quota_warned = !is_delete;
840 if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
841 domain->transition_failed = !is_delete;
844 return tomoyo_write_domain_policy2(data, domain, is_delete);
848 * tomoyo_fns - Find next set bit.
850 * @perm: 8 bits value.
851 * @bit: First bit to find.
853 * Returns next on-bit on success, 8 otherwise.
855 static u8 tomoyo_fns(const u8 perm, u8 bit)
857 for ( ; bit < 8; bit++)
858 if (perm & (1 << bit))
864 * tomoyo_print_entry - Print an ACL entry.
866 * @head: Pointer to "struct tomoyo_io_buffer".
867 * @acl: Pointer to an ACL entry.
869 * Returns true on success, false otherwise.
871 static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
872 struct tomoyo_acl_info *acl)
874 const u8 acl_type = acl->type;
881 if (!tomoyo_flush(head))
883 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
884 struct tomoyo_path_acl *ptr =
885 container_of(acl, typeof(*ptr), head);
886 const u16 perm = ptr->perm;
887 for ( ; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
888 if (!(perm & (1 << bit)))
890 if (head->r.print_execute_only &&
891 bit != TOMOYO_TYPE_EXECUTE)
893 /* Print "read/write" instead of "read" and "write". */
894 if ((bit == TOMOYO_TYPE_READ ||
895 bit == TOMOYO_TYPE_WRITE)
896 && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
900 if (bit >= TOMOYO_MAX_PATH_OPERATION)
902 tomoyo_io_printf(head, "allow_%s", tomoyo_path_keyword[bit]);
903 tomoyo_print_name_union(head, &ptr->name);
904 } else if (head->r.print_execute_only) {
906 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
907 struct tomoyo_path2_acl *ptr =
908 container_of(acl, typeof(*ptr), head);
909 bit = tomoyo_fns(ptr->perm, bit);
910 if (bit >= TOMOYO_MAX_PATH2_OPERATION)
912 tomoyo_io_printf(head, "allow_%s", tomoyo_path2_keyword[bit]);
913 tomoyo_print_name_union(head, &ptr->name1);
914 tomoyo_print_name_union(head, &ptr->name2);
915 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
916 struct tomoyo_path_number_acl *ptr =
917 container_of(acl, typeof(*ptr), head);
918 bit = tomoyo_fns(ptr->perm, bit);
919 if (bit >= TOMOYO_MAX_PATH_NUMBER_OPERATION)
921 tomoyo_io_printf(head, "allow_%s",
922 tomoyo_path_number_keyword[bit]);
923 tomoyo_print_name_union(head, &ptr->name);
924 tomoyo_print_number_union(head, &ptr->number);
925 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
926 struct tomoyo_mkdev_acl *ptr =
927 container_of(acl, typeof(*ptr), head);
928 bit = tomoyo_fns(ptr->perm, bit);
929 if (bit >= TOMOYO_MAX_MKDEV_OPERATION)
931 tomoyo_io_printf(head, "allow_%s", tomoyo_mkdev_keyword[bit]);
932 tomoyo_print_name_union(head, &ptr->name);
933 tomoyo_print_number_union(head, &ptr->mode);
934 tomoyo_print_number_union(head, &ptr->major);
935 tomoyo_print_number_union(head, &ptr->minor);
936 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
937 struct tomoyo_mount_acl *ptr =
938 container_of(acl, typeof(*ptr), head);
939 tomoyo_io_printf(head, "allow_mount");
940 tomoyo_print_name_union(head, &ptr->dev_name);
941 tomoyo_print_name_union(head, &ptr->dir_name);
942 tomoyo_print_name_union(head, &ptr->fs_type);
943 tomoyo_print_number_union(head, &ptr->flags);
945 head->r.bit = bit + 1;
946 tomoyo_io_printf(head, "\n");
947 if (acl_type != TOMOYO_TYPE_MOUNT_ACL)
955 * tomoyo_read_domain2 - Read domain policy.
957 * @head: Pointer to "struct tomoyo_io_buffer".
958 * @domain: Pointer to "struct tomoyo_domain_info".
960 * Caller holds tomoyo_read_lock().
962 * Returns true on success, false otherwise.
964 static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
965 struct tomoyo_domain_info *domain)
967 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
968 struct tomoyo_acl_info *ptr =
969 list_entry(head->r.acl, typeof(*ptr), list);
970 if (!tomoyo_print_entry(head, ptr))
978 * tomoyo_read_domain_policy - Read domain policy.
980 * @head: Pointer to "struct tomoyo_io_buffer".
982 * Caller holds tomoyo_read_lock().
984 static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
988 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
989 struct tomoyo_domain_info *domain =
990 list_entry(head->r.domain, typeof(*domain), list);
991 switch (head->r.step) {
993 if (domain->is_deleted &&
994 !head->r.print_this_domain_only)
996 /* Print domainname and flags. */
997 tomoyo_set_string(head, domain->domainname->name);
999 tomoyo_io_printf(head,
1000 TOMOYO_KEYWORD_USE_PROFILE "%u\n",
1002 if (domain->quota_warned)
1003 tomoyo_set_string(head, "quota_exceeded\n");
1004 if (domain->transition_failed)
1005 tomoyo_set_string(head, "transition_failed\n");
1006 if (domain->ignore_global_allow_read)
1007 tomoyo_set_string(head,
1008 TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
1011 tomoyo_set_lf(head);
1014 if (!tomoyo_read_domain2(head, domain))
1017 if (!tomoyo_set_lf(head))
1022 if (head->r.print_this_domain_only)
1031 * tomoyo_write_domain_profile - Assign profile for specified domain.
1033 * @head: Pointer to "struct tomoyo_io_buffer".
1035 * Returns 0 on success, -EINVAL otherwise.
1037 * This is equivalent to doing
1039 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1040 * /usr/sbin/tomoyo-loadpolicy -d
1042 * Caller holds tomoyo_read_lock().
1044 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1046 char *data = head->write_buf;
1047 char *cp = strchr(data, ' ');
1048 struct tomoyo_domain_info *domain;
1049 unsigned long profile;
1054 domain = tomoyo_find_domain(cp + 1);
1055 if (strict_strtoul(data, 10, &profile))
1057 if (domain && profile < TOMOYO_MAX_PROFILES
1058 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1059 domain->profile = (u8) profile;
1064 * tomoyo_read_domain_profile - Read only domainname and profile.
1066 * @head: Pointer to "struct tomoyo_io_buffer".
1068 * Returns list of profile number and domainname pairs.
1070 * This is equivalent to doing
1072 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1073 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1074 * domainname = $0; } else if ( $1 == "use_profile" ) {
1075 * print $2 " " domainname; domainname = ""; } } ; '
1077 * Caller holds tomoyo_read_lock().
1079 static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1083 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1084 struct tomoyo_domain_info *domain =
1085 list_entry(head->r.domain, typeof(*domain), list);
1086 if (domain->is_deleted)
1088 if (!tomoyo_flush(head))
1090 tomoyo_io_printf(head, "%u ", domain->profile);
1091 tomoyo_set_string(head, domain->domainname->name);
1092 tomoyo_set_lf(head);
1098 * tomoyo_write_pid: Specify PID to obtain domainname.
1100 * @head: Pointer to "struct tomoyo_io_buffer".
1104 static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1106 head->r.eof = false;
1111 * tomoyo_read_pid - Get domainname of the specified PID.
1113 * @head: Pointer to "struct tomoyo_io_buffer".
1115 * Returns the domainname which the specified PID is in on success,
1116 * empty string otherwise.
1117 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1118 * using read()/write() interface rather than sysctl() interface.
1120 static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
1122 char *buf = head->write_buf;
1123 bool global_pid = false;
1125 struct task_struct *p;
1126 struct tomoyo_domain_info *domain = NULL;
1128 /* Accessing write_buf is safe because head->io_sem is held. */
1131 return; /* Do nothing if open(O_RDONLY). */
1133 if (head->r.w_pos || head->r.eof)
1136 if (tomoyo_str_starts(&buf, "global-pid "))
1138 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1140 read_lock(&tasklist_lock);
1142 p = find_task_by_pid_ns(pid, &init_pid_ns);
1144 p = find_task_by_vpid(pid);
1146 domain = tomoyo_real_domain(p);
1147 read_unlock(&tasklist_lock);
1151 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1152 tomoyo_set_string(head, domain->domainname->name);
1155 static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1156 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE]
1157 = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN,
1158 [TOMOYO_TRANSITION_CONTROL_INITIALIZE]
1159 = TOMOYO_KEYWORD_INITIALIZE_DOMAIN,
1160 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN,
1161 [TOMOYO_TRANSITION_CONTROL_KEEP] = TOMOYO_KEYWORD_KEEP_DOMAIN
1165 * tomoyo_write_exception_policy - Write exception policy.
1167 * @head: Pointer to "struct tomoyo_io_buffer".
1169 * Returns 0 on success, negative value otherwise.
1171 * Caller holds tomoyo_read_lock().
1173 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1175 char *data = head->write_buf;
1176 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1179 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) {
1180 if (tomoyo_str_starts(&data, tomoyo_transition_type[i]))
1181 return tomoyo_write_transition_control(data, is_delete,
1184 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR))
1185 return tomoyo_write_aggregator_policy(data, is_delete);
1186 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1187 return tomoyo_write_globally_readable_policy(data, is_delete);
1188 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1189 return tomoyo_write_pattern_policy(data, is_delete);
1190 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1191 return tomoyo_write_no_rewrite_policy(data, is_delete);
1192 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
1193 return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
1194 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
1195 return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
1199 static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1200 [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
1201 [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
1205 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1207 * @head: Pointer to "struct tomoyo_io_buffer".
1208 * @idx: Index number.
1210 * Returns true on success, false otherwise.
1212 * Caller holds tomoyo_read_lock().
1214 static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1216 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
1217 struct tomoyo_group *group =
1218 list_entry(head->r.group, typeof(*group), list);
1219 list_for_each_cookie(head->r.acl, &group->member_list) {
1220 struct tomoyo_acl_head *ptr =
1221 list_entry(head->r.acl, typeof(*ptr), list);
1222 if (ptr->is_deleted)
1224 if (!tomoyo_flush(head))
1226 tomoyo_set_string(head, tomoyo_group_name[idx]);
1227 tomoyo_set_string(head, group->group_name->name);
1228 if (idx == TOMOYO_PATH_GROUP) {
1229 tomoyo_set_space(head);
1230 tomoyo_set_string(head, container_of
1231 (ptr, struct tomoyo_path_group,
1232 head)->member_name->name);
1233 } else if (idx == TOMOYO_NUMBER_GROUP) {
1234 tomoyo_print_number_union(head, &container_of
1236 struct tomoyo_number_group,
1239 tomoyo_set_lf(head);
1243 head->r.group = NULL;
1248 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1250 * @head: Pointer to "struct tomoyo_io_buffer".
1251 * @idx: Index number.
1253 * Returns true on success, false otherwise.
1255 * Caller holds tomoyo_read_lock().
1257 static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1259 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
1260 struct tomoyo_acl_head *acl =
1261 container_of(head->r.acl, typeof(*acl), list);
1262 if (acl->is_deleted)
1264 if (!tomoyo_flush(head))
1267 case TOMOYO_ID_TRANSITION_CONTROL:
1269 struct tomoyo_transition_control *ptr =
1270 container_of(acl, typeof(*ptr), head);
1271 tomoyo_set_string(head,
1272 tomoyo_transition_type
1275 tomoyo_set_string(head,
1276 ptr->program->name);
1277 if (ptr->program && ptr->domainname)
1278 tomoyo_set_string(head, " from ");
1279 if (ptr->domainname)
1280 tomoyo_set_string(head,
1285 case TOMOYO_ID_GLOBALLY_READABLE:
1287 struct tomoyo_globally_readable_file_entry *ptr
1288 = container_of(acl, typeof(*ptr), head);
1289 tomoyo_set_string(head,
1290 TOMOYO_KEYWORD_ALLOW_READ);
1291 tomoyo_set_string(head, ptr->filename->name);
1294 case TOMOYO_ID_AGGREGATOR:
1296 struct tomoyo_aggregator_entry *ptr =
1297 container_of(acl, typeof(*ptr), head);
1298 tomoyo_set_string(head,
1299 TOMOYO_KEYWORD_AGGREGATOR);
1300 tomoyo_set_string(head,
1301 ptr->original_name->name);
1302 tomoyo_set_space(head);
1303 tomoyo_set_string(head,
1304 ptr->aggregated_name->name);
1307 case TOMOYO_ID_PATTERN:
1309 struct tomoyo_pattern_entry *ptr =
1310 container_of(acl, typeof(*ptr), head);
1311 tomoyo_set_string(head,
1312 TOMOYO_KEYWORD_FILE_PATTERN);
1313 tomoyo_set_string(head, ptr->pattern->name);
1316 case TOMOYO_ID_NO_REWRITE:
1318 struct tomoyo_no_rewrite_entry *ptr =
1319 container_of(acl, typeof(*ptr), head);
1320 tomoyo_set_string(head,
1321 TOMOYO_KEYWORD_DENY_REWRITE);
1322 tomoyo_set_string(head, ptr->pattern->name);
1328 tomoyo_set_lf(head);
1335 * tomoyo_read_exception_policy - Read exception policy.
1337 * @head: Pointer to "struct tomoyo_io_buffer".
1339 * Caller holds tomoyo_read_lock().
1341 static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
1345 while (head->r.step < TOMOYO_MAX_POLICY &&
1346 tomoyo_read_policy(head, head->r.step))
1348 if (head->r.step < TOMOYO_MAX_POLICY)
1350 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1351 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1353 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1359 * tomoyo_print_header - Get header line of audit log.
1361 * @r: Pointer to "struct tomoyo_request_info".
1363 * Returns string representation.
1365 * This function uses kmalloc(), so caller must kfree() if this function
1366 * didn't return NULL.
1368 static char *tomoyo_print_header(struct tomoyo_request_info *r)
1371 const pid_t gpid = task_pid_nr(current);
1372 static const int tomoyo_buffer_len = 4096;
1373 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1376 do_gettimeofday(&tv);
1377 snprintf(buffer, tomoyo_buffer_len - 1,
1378 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1379 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1380 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1381 tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
1382 (pid_t) sys_getpid(), (pid_t) sys_getppid(),
1383 current_uid(), current_gid(), current_euid(),
1384 current_egid(), current_suid(), current_sgid(),
1385 current_fsuid(), current_fsgid());
1390 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1392 * @len: Required size.
1393 * @r: Pointer to "struct tomoyo_request_info".
1395 * Returns pointer to allocated memory.
1397 * The @len is updated to add the header lines' size on success.
1399 * This function uses kzalloc(), so caller must kfree() if this function
1400 * didn't return NULL.
1402 static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1406 const char *domainname;
1408 r->domain = tomoyo_domain();
1409 domainname = r->domain->domainname->name;
1410 header = tomoyo_print_header(r);
1413 *len += strlen(domainname) + strlen(header) + 10;
1414 buf = kzalloc(*len, GFP_NOFS);
1416 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1421 /* Wait queue for tomoyo_query_list. */
1422 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1424 /* Lock for manipulating tomoyo_query_list. */
1425 static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1427 /* Structure for query. */
1428 struct tomoyo_query_entry {
1429 struct list_head list;
1432 unsigned int serial;
1437 /* The list for "struct tomoyo_query_entry". */
1438 static LIST_HEAD(tomoyo_query_list);
1441 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1444 static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1447 * tomoyo_supervisor - Ask for the supervisor's decision.
1449 * @r: Pointer to "struct tomoyo_request_info".
1450 * @fmt: The printf()'s format string, followed by parameters.
1452 * Returns 0 if the supervisor decided to permit the access request which
1453 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1454 * supervisor decided to retry the access request which violated the policy in
1455 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1457 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1463 static unsigned int tomoyo_serial;
1464 struct tomoyo_query_entry *tomoyo_query_entry = NULL;
1465 bool quota_exceeded = false;
1469 case TOMOYO_CONFIG_LEARNING:
1470 if (!tomoyo_domain_quota_is_ok(r))
1472 va_start(args, fmt);
1473 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1475 buffer = kmalloc(len, GFP_NOFS);
1478 va_start(args, fmt);
1479 vsnprintf(buffer, len - 1, fmt, args);
1481 tomoyo_normalize_line(buffer);
1482 tomoyo_write_domain_policy2(buffer, r->domain, false);
1485 case TOMOYO_CONFIG_PERMISSIVE:
1489 r->domain = tomoyo_domain();
1490 if (!atomic_read(&tomoyo_query_observers))
1492 va_start(args, fmt);
1493 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1495 header = tomoyo_init_audit_log(&len, r);
1498 tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
1499 if (!tomoyo_query_entry)
1501 tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
1502 if (!tomoyo_query_entry->query)
1504 len = ksize(tomoyo_query_entry->query);
1505 INIT_LIST_HEAD(&tomoyo_query_entry->list);
1506 spin_lock(&tomoyo_query_list_lock);
1507 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
1508 sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
1509 quota_exceeded = true;
1511 tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
1512 tomoyo_query_entry->serial = tomoyo_serial++;
1514 spin_unlock(&tomoyo_query_list_lock);
1517 pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
1518 tomoyo_query_entry->serial, r->retry, header);
1521 va_start(args, fmt);
1522 vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
1523 tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
1525 spin_lock(&tomoyo_query_list_lock);
1526 list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
1527 spin_unlock(&tomoyo_query_list_lock);
1528 /* Give 10 seconds for supervisor's opinion. */
1529 for (tomoyo_query_entry->timer = 0;
1530 atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
1531 tomoyo_query_entry->timer++) {
1532 wake_up(&tomoyo_query_wait);
1533 set_current_state(TASK_INTERRUPTIBLE);
1534 schedule_timeout(HZ / 10);
1535 if (tomoyo_query_entry->answer)
1538 spin_lock(&tomoyo_query_list_lock);
1539 list_del(&tomoyo_query_entry->list);
1540 tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
1541 spin_unlock(&tomoyo_query_list_lock);
1542 switch (tomoyo_query_entry->answer) {
1543 case 3: /* Asked to retry by administrator. */
1544 error = TOMOYO_RETRY_REQUEST;
1548 /* Granted by administrator. */
1555 /* Rejected by administrator. */
1559 if (tomoyo_query_entry)
1560 kfree(tomoyo_query_entry->query);
1561 kfree(tomoyo_query_entry);
1567 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1569 * @file: Pointer to "struct file".
1570 * @wait: Pointer to "poll_table".
1572 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1574 * Waits for access requests which violated policy in enforcing mode.
1576 static int tomoyo_poll_query(struct file *file, poll_table *wait)
1578 struct list_head *tmp;
1581 for (i = 0; i < 2; i++) {
1582 spin_lock(&tomoyo_query_list_lock);
1583 list_for_each(tmp, &tomoyo_query_list) {
1584 struct tomoyo_query_entry *ptr
1585 = list_entry(tmp, struct tomoyo_query_entry,
1592 spin_unlock(&tomoyo_query_list_lock);
1594 return POLLIN | POLLRDNORM;
1597 poll_wait(file, &tomoyo_query_wait, wait);
1603 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1605 * @head: Pointer to "struct tomoyo_io_buffer".
1607 static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1609 struct list_head *tmp;
1615 if (head->read_buf) {
1616 kfree(head->read_buf);
1617 head->read_buf = NULL;
1619 spin_lock(&tomoyo_query_list_lock);
1620 list_for_each(tmp, &tomoyo_query_list) {
1621 struct tomoyo_query_entry *ptr =
1622 list_entry(tmp, typeof(*ptr), list);
1625 if (pos++ != head->r.query_index)
1627 len = ptr->query_len;
1630 spin_unlock(&tomoyo_query_list_lock);
1632 head->r.query_index = 0;
1635 buf = kzalloc(len, GFP_NOFS);
1639 spin_lock(&tomoyo_query_list_lock);
1640 list_for_each(tmp, &tomoyo_query_list) {
1641 struct tomoyo_query_entry *ptr =
1642 list_entry(tmp, typeof(*ptr), list);
1645 if (pos++ != head->r.query_index)
1648 * Some query can be skipped because tomoyo_query_list
1649 * can change, but I don't care.
1651 if (len == ptr->query_len)
1652 memmove(buf, ptr->query, len);
1655 spin_unlock(&tomoyo_query_list_lock);
1657 head->read_buf = buf;
1658 head->r.w[head->r.w_pos++] = buf;
1659 head->r.query_index++;
1666 * tomoyo_write_answer - Write the supervisor's decision.
1668 * @head: Pointer to "struct tomoyo_io_buffer".
1670 * Returns 0 on success, -EINVAL otherwise.
1672 static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1674 char *data = head->write_buf;
1675 struct list_head *tmp;
1676 unsigned int serial;
1677 unsigned int answer;
1678 spin_lock(&tomoyo_query_list_lock);
1679 list_for_each(tmp, &tomoyo_query_list) {
1680 struct tomoyo_query_entry *ptr
1681 = list_entry(tmp, struct tomoyo_query_entry, list);
1684 spin_unlock(&tomoyo_query_list_lock);
1685 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1687 spin_lock(&tomoyo_query_list_lock);
1688 list_for_each(tmp, &tomoyo_query_list) {
1689 struct tomoyo_query_entry *ptr
1690 = list_entry(tmp, struct tomoyo_query_entry, list);
1691 if (ptr->serial != serial)
1694 ptr->answer = answer;
1697 spin_unlock(&tomoyo_query_list_lock);
1702 * tomoyo_read_version: Get version.
1704 * @head: Pointer to "struct tomoyo_io_buffer".
1706 * Returns version information.
1708 static void tomoyo_read_version(struct tomoyo_io_buffer *head)
1711 tomoyo_io_printf(head, "2.3.0-pre");
1717 * tomoyo_read_self_domain - Get the current process's domainname.
1719 * @head: Pointer to "struct tomoyo_io_buffer".
1721 * Returns the current process's domainname.
1723 static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1727 * tomoyo_domain()->domainname != NULL
1728 * because every process belongs to a domain and
1729 * the domain's name cannot be NULL.
1731 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1737 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1739 * @type: Type of interface.
1740 * @file: Pointer to "struct file".
1742 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1744 * Caller acquires tomoyo_read_lock().
1746 int tomoyo_open_control(const u8 type, struct file *file)
1748 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
1752 mutex_init(&head->io_sem);
1755 case TOMOYO_DOMAINPOLICY:
1756 /* /sys/kernel/security/tomoyo/domain_policy */
1757 head->write = tomoyo_write_domain_policy;
1758 head->read = tomoyo_read_domain_policy;
1760 case TOMOYO_EXCEPTIONPOLICY:
1761 /* /sys/kernel/security/tomoyo/exception_policy */
1762 head->write = tomoyo_write_exception_policy;
1763 head->read = tomoyo_read_exception_policy;
1765 case TOMOYO_SELFDOMAIN:
1766 /* /sys/kernel/security/tomoyo/self_domain */
1767 head->read = tomoyo_read_self_domain;
1769 case TOMOYO_DOMAIN_STATUS:
1770 /* /sys/kernel/security/tomoyo/.domain_status */
1771 head->write = tomoyo_write_domain_profile;
1772 head->read = tomoyo_read_domain_profile;
1774 case TOMOYO_PROCESS_STATUS:
1775 /* /sys/kernel/security/tomoyo/.process_status */
1776 head->write = tomoyo_write_pid;
1777 head->read = tomoyo_read_pid;
1779 case TOMOYO_VERSION:
1780 /* /sys/kernel/security/tomoyo/version */
1781 head->read = tomoyo_read_version;
1782 head->readbuf_size = 128;
1784 case TOMOYO_MEMINFO:
1785 /* /sys/kernel/security/tomoyo/meminfo */
1786 head->write = tomoyo_write_memory_quota;
1787 head->read = tomoyo_read_memory_counter;
1788 head->readbuf_size = 512;
1790 case TOMOYO_PROFILE:
1791 /* /sys/kernel/security/tomoyo/profile */
1792 head->write = tomoyo_write_profile;
1793 head->read = tomoyo_read_profile;
1795 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1796 head->poll = tomoyo_poll_query;
1797 head->write = tomoyo_write_answer;
1798 head->read = tomoyo_read_query;
1800 case TOMOYO_MANAGER:
1801 /* /sys/kernel/security/tomoyo/manager */
1802 head->write = tomoyo_write_manager_policy;
1803 head->read = tomoyo_read_manager_policy;
1806 if (!(file->f_mode & FMODE_READ)) {
1808 * No need to allocate read_buf since it is not opened
1813 } else if (!head->poll) {
1814 /* Don't allocate read_buf for poll() access. */
1815 if (!head->readbuf_size)
1816 head->readbuf_size = 4096 * 2;
1817 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
1818 if (!head->read_buf) {
1823 if (!(file->f_mode & FMODE_WRITE)) {
1825 * No need to allocate write_buf since it is not opened
1829 } else if (head->write) {
1830 head->writebuf_size = 4096 * 2;
1831 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
1832 if (!head->write_buf) {
1833 kfree(head->read_buf);
1838 if (type != TOMOYO_QUERY)
1839 head->reader_idx = tomoyo_read_lock();
1840 file->private_data = head;
1842 * Call the handler now if the file is
1843 * /sys/kernel/security/tomoyo/self_domain
1844 * so that the user can use
1845 * cat < /sys/kernel/security/tomoyo/self_domain"
1846 * to know the current process's domainname.
1848 if (type == TOMOYO_SELFDOMAIN)
1849 tomoyo_read_control(file, NULL, 0);
1851 * If the file is /sys/kernel/security/tomoyo/query , increment the
1853 * The obserber counter is used by tomoyo_supervisor() to see if
1854 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1856 else if (type == TOMOYO_QUERY)
1857 atomic_inc(&tomoyo_query_observers);
1862 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1864 * @file: Pointer to "struct file".
1865 * @wait: Pointer to "poll_table".
1867 * Waits for read readiness.
1868 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1870 int tomoyo_poll_control(struct file *file, poll_table *wait)
1872 struct tomoyo_io_buffer *head = file->private_data;
1875 return head->poll(file, wait);
1879 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1881 * @file: Pointer to "struct file".
1882 * @buffer: Poiner to buffer to write to.
1883 * @buffer_len: Size of @buffer.
1885 * Returns bytes read on success, negative value otherwise.
1887 * Caller holds tomoyo_read_lock().
1889 int tomoyo_read_control(struct file *file, char __user *buffer,
1890 const int buffer_len)
1893 struct tomoyo_io_buffer *head = file->private_data;
1897 if (mutex_lock_interruptible(&head->io_sem))
1899 head->read_user_buf = buffer;
1900 head->read_user_buf_avail = buffer_len;
1901 if (tomoyo_flush(head))
1902 /* Call the policy handler. */
1905 len = head->read_user_buf - buffer;
1906 mutex_unlock(&head->io_sem);
1911 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1913 * @file: Pointer to "struct file".
1914 * @buffer: Pointer to buffer to read from.
1915 * @buffer_len: Size of @buffer.
1917 * Returns @buffer_len on success, negative value otherwise.
1919 * Caller holds tomoyo_read_lock().
1921 int tomoyo_write_control(struct file *file, const char __user *buffer,
1922 const int buffer_len)
1924 struct tomoyo_io_buffer *head = file->private_data;
1925 int error = buffer_len;
1926 int avail_len = buffer_len;
1927 char *cp0 = head->write_buf;
1931 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1933 /* Don't allow updating policies by non manager programs. */
1934 if (head->write != tomoyo_write_pid &&
1935 head->write != tomoyo_write_domain_policy &&
1936 !tomoyo_policy_manager())
1938 if (mutex_lock_interruptible(&head->io_sem))
1940 /* Read a line and dispatch it to the policy handler. */
1941 while (avail_len > 0) {
1943 if (head->write_avail >= head->writebuf_size - 1) {
1946 } else if (get_user(c, buffer)) {
1952 cp0[head->write_avail++] = c;
1955 cp0[head->write_avail - 1] = '\0';
1956 head->write_avail = 0;
1957 tomoyo_normalize_line(cp0);
1960 mutex_unlock(&head->io_sem);
1965 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1967 * @file: Pointer to "struct file".
1969 * Releases memory and returns 0.
1971 * Caller looses tomoyo_read_lock().
1973 int tomoyo_close_control(struct file *file)
1975 struct tomoyo_io_buffer *head = file->private_data;
1976 const bool is_write = !!head->write_buf;
1979 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1982 if (head->type == TOMOYO_QUERY)
1983 atomic_dec(&tomoyo_query_observers);
1985 tomoyo_read_unlock(head->reader_idx);
1986 /* Release memory used for policy I/O. */
1987 kfree(head->read_buf);
1988 head->read_buf = NULL;
1989 kfree(head->write_buf);
1990 head->write_buf = NULL;
1993 file->private_data = NULL;
2000 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
2002 void tomoyo_check_profile(void)
2004 struct tomoyo_domain_info *domain;
2005 const int idx = tomoyo_read_lock();
2006 tomoyo_policy_loaded = true;
2007 /* Check all profiles currently assigned to domains are defined. */
2008 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2009 const u8 profile = domain->profile;
2010 if (tomoyo_profile_ptr[profile])
2012 panic("Profile %u (used by '%s') not defined.\n",
2013 profile, domain->domainname->name);
2015 tomoyo_read_unlock(idx);
2016 if (tomoyo_profile_version != 20090903)
2017 panic("Profile version %u is not supported.\n",
2018 tomoyo_profile_version);
2019 printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n");
2020 printk(KERN_INFO "Mandatory Access Control activated.\n");