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
[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";
80 * tomoyo_print_name_union - Print a tomoyo_name_union.
82 * @head: Pointer to "struct tomoyo_io_buffer".
83 * @ptr: Pointer to "struct tomoyo_name_union".
85 * Returns true on success, false otherwise.
87 static bool tomoyo_print_name_union(struct tomoyo_io_buffer
*head
,
88 const struct tomoyo_name_union
*ptr
)
90 int pos
= head
->read_avail
;
91 if (pos
&& head
->read_buf
[pos
- 1] == ' ')
94 return tomoyo_io_printf(head
, " @%s",
95 ptr
->group
->group_name
->name
);
96 return tomoyo_io_printf(head
, " %s", ptr
->filename
->name
);
100 * tomoyo_print_number_union - Print a tomoyo_number_union.
102 * @head: Pointer to "struct tomoyo_io_buffer".
103 * @ptr: Pointer to "struct tomoyo_number_union".
105 * Returns true on success, false otherwise.
107 bool tomoyo_print_number_union(struct tomoyo_io_buffer
*head
,
108 const struct tomoyo_number_union
*ptr
)
114 if (!tomoyo_io_printf(head
, " "))
117 return tomoyo_io_printf(head
, "@%s",
118 ptr
->group
->group_name
->name
);
119 min_type
= ptr
->min_type
;
120 max_type
= ptr
->max_type
;
121 min
= ptr
->values
[0];
122 max
= ptr
->values
[1];
124 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
125 if (!tomoyo_io_printf(head
, "0x%lX", min
))
128 case TOMOYO_VALUE_TYPE_OCTAL
:
129 if (!tomoyo_io_printf(head
, "0%lo", min
))
133 if (!tomoyo_io_printf(head
, "%lu", min
))
137 if (min
== max
&& min_type
== max_type
)
140 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
141 return tomoyo_io_printf(head
, "-0x%lX", max
);
142 case TOMOYO_VALUE_TYPE_OCTAL
:
143 return tomoyo_io_printf(head
, "-0%lo", max
);
145 return tomoyo_io_printf(head
, "-%lu", max
);
150 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
152 * @head: Pointer to "struct tomoyo_io_buffer".
153 * @fmt: The printf()'s format string, followed by parameters.
155 * Returns true if output was written, false otherwise.
157 * The snprintf() will truncate, but tomoyo_io_printf() won't.
159 bool tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
163 int pos
= head
->read_avail
;
164 int size
= head
->readbuf_size
- pos
;
169 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
);
171 if (pos
+ len
>= head
->readbuf_size
)
173 head
->read_avail
+= len
;
178 * tomoyo_find_or_assign_new_profile - Create a new profile.
180 * @profile: Profile number to create.
182 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
184 static struct tomoyo_profile
*tomoyo_find_or_assign_new_profile
185 (const unsigned int profile
)
187 struct tomoyo_profile
*ptr
;
188 struct tomoyo_profile
*entry
;
189 if (profile
>= TOMOYO_MAX_PROFILES
)
191 ptr
= tomoyo_profile_ptr
[profile
];
194 entry
= kzalloc(sizeof(*entry
), GFP_NOFS
);
195 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
197 ptr
= tomoyo_profile_ptr
[profile
];
198 if (!ptr
&& tomoyo_memory_ok(entry
)) {
200 ptr
->learning
= &tomoyo_default_profile
.preference
;
201 ptr
->permissive
= &tomoyo_default_profile
.preference
;
202 ptr
->enforcing
= &tomoyo_default_profile
.preference
;
203 ptr
->default_config
= TOMOYO_CONFIG_DISABLED
;
204 memset(ptr
->config
, TOMOYO_CONFIG_USE_DEFAULT
,
205 sizeof(ptr
->config
));
206 mb(); /* Avoid out-of-order execution. */
207 tomoyo_profile_ptr
[profile
] = ptr
;
210 mutex_unlock(&tomoyo_policy_lock
);
217 * tomoyo_profile - Find a profile.
219 * @profile: Profile number to find.
221 * Returns pointer to "struct tomoyo_profile".
223 struct tomoyo_profile
*tomoyo_profile(const u8 profile
)
225 struct tomoyo_profile
*ptr
= tomoyo_profile_ptr
[profile
];
226 if (!tomoyo_policy_loaded
)
227 return &tomoyo_default_profile
;
233 * tomoyo_write_profile - Write profile table.
235 * @head: Pointer to "struct tomoyo_io_buffer".
237 * Returns 0 on success, negative value otherwise.
239 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
241 char *data
= head
->write_buf
;
246 bool use_default
= false;
248 struct tomoyo_profile
*profile
;
249 if (sscanf(data
, "PROFILE_VERSION=%u", &tomoyo_profile_version
) == 1)
251 i
= simple_strtoul(data
, &cp
, 10);
253 profile
= &tomoyo_default_profile
;
258 profile
= tomoyo_find_or_assign_new_profile(i
);
262 cp
= strchr(data
, '=');
266 if (profile
!= &tomoyo_default_profile
)
267 use_default
= strstr(cp
, "use_default") != NULL
;
268 if (strstr(cp
, "verbose=yes"))
270 else if (strstr(cp
, "verbose=no"))
274 if (!strcmp(data
, "PREFERENCE::enforcing")) {
276 profile
->enforcing
= &tomoyo_default_profile
.preference
;
279 profile
->enforcing
= &profile
->preference
;
281 profile
->preference
.enforcing_verbose
= value
;
284 if (!strcmp(data
, "PREFERENCE::permissive")) {
286 profile
->permissive
= &tomoyo_default_profile
.preference
;
289 profile
->permissive
= &profile
->preference
;
291 profile
->preference
.permissive_verbose
= value
;
294 if (!strcmp(data
, "PREFERENCE::learning")) {
297 profile
->learning
= &tomoyo_default_profile
.preference
;
300 profile
->learning
= &profile
->preference
;
302 profile
->preference
.learning_verbose
= value
;
303 cp2
= strstr(cp
, "max_entry=");
305 sscanf(cp2
+ 10, "%u",
306 &profile
->preference
.learning_max_entry
);
309 if (profile
== &tomoyo_default_profile
)
311 if (!strcmp(data
, "COMMENT")) {
312 const struct tomoyo_path_info
*old_comment
= profile
->comment
;
313 profile
->comment
= tomoyo_get_name(cp
);
314 tomoyo_put_name(old_comment
);
317 if (!strcmp(data
, "CONFIG")) {
318 i
= TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
;
319 config
= profile
->default_config
;
320 } else if (tomoyo_str_starts(&data
, "CONFIG::")) {
322 for (i
= 0; i
< TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
; i
++) {
323 if (strcmp(data
, tomoyo_mac_keywords
[i
]))
325 config
= profile
->config
[i
];
328 if (i
== TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
334 config
= TOMOYO_CONFIG_USE_DEFAULT
;
336 for (mode
= 3; mode
>= 0; mode
--)
337 if (strstr(cp
, tomoyo_mode_4
[mode
]))
339 * Update lower 3 bits in order to distinguish
340 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
342 config
= (config
& ~7) | mode
;
344 if (i
< TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
345 profile
->config
[i
] = config
;
346 else if (config
!= TOMOYO_CONFIG_USE_DEFAULT
)
347 profile
->default_config
= config
;
352 * tomoyo_read_profile - Read profile table.
354 * @head: Pointer to "struct tomoyo_io_buffer".
356 static void tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
363 tomoyo_io_printf(head
, "PROFILE_VERSION=%s\n", "20090903");
364 tomoyo_io_printf(head
, "PREFERENCE::learning={ verbose=%s "
366 tomoyo_yesno(tomoyo_default_profile
.preference
.
368 tomoyo_default_profile
.preference
.learning_max_entry
);
369 tomoyo_io_printf(head
, "PREFERENCE::permissive={ verbose=%s }\n",
370 tomoyo_yesno(tomoyo_default_profile
.preference
.
371 permissive_verbose
));
372 tomoyo_io_printf(head
, "PREFERENCE::enforcing={ verbose=%s }\n",
373 tomoyo_yesno(tomoyo_default_profile
.preference
.
377 for (index
= head
->read_step
; index
< TOMOYO_MAX_PROFILES
; index
++) {
382 const struct tomoyo_profile
*profile
383 = tomoyo_profile_ptr
[index
];
384 const struct tomoyo_path_info
*comment
;
385 head
->read_step
= index
;
388 pos
= head
->read_avail
;
389 comment
= profile
->comment
;
390 done
= tomoyo_io_printf(head
, "%u-COMMENT=%s\n", index
,
391 comment
? comment
->name
: "");
394 config
= profile
->default_config
;
395 if (!tomoyo_io_printf(head
, "%u-CONFIG={ mode=%s }\n", index
,
396 tomoyo_mode_4
[config
& 3]))
398 for (i
= 0; i
< TOMOYO_MAX_MAC_INDEX
+
399 TOMOYO_MAX_MAC_CATEGORY_INDEX
; i
++) {
400 config
= profile
->config
[i
];
401 if (config
== TOMOYO_CONFIG_USE_DEFAULT
)
403 if (!tomoyo_io_printf(head
,
404 "%u-CONFIG::%s={ mode=%s }\n",
405 index
, tomoyo_mac_keywords
[i
],
406 tomoyo_mode_4
[config
& 3]))
409 if (profile
->learning
!= &tomoyo_default_profile
.preference
&&
410 !tomoyo_io_printf(head
, "%u-PREFERENCE::learning={ "
411 "verbose=%s max_entry=%u }\n", index
,
412 tomoyo_yesno(profile
->preference
.
414 profile
->preference
.learning_max_entry
))
416 if (profile
->permissive
!= &tomoyo_default_profile
.preference
417 && !tomoyo_io_printf(head
, "%u-PREFERENCE::permissive={ "
418 "verbose=%s }\n", index
,
419 tomoyo_yesno(profile
->preference
.
420 permissive_verbose
)))
422 if (profile
->enforcing
!= &tomoyo_default_profile
.preference
&&
423 !tomoyo_io_printf(head
, "%u-PREFERENCE::enforcing={ "
424 "verbose=%s }\n", index
,
425 tomoyo_yesno(profile
->preference
.
430 head
->read_avail
= pos
;
433 if (index
== TOMOYO_MAX_PROFILES
)
434 head
->read_eof
= true;
437 static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head
*a
,
438 const struct tomoyo_acl_head
*b
)
440 return container_of(a
, struct tomoyo_policy_manager_entry
, head
)
442 container_of(b
, struct tomoyo_policy_manager_entry
, head
)
447 * tomoyo_update_manager_entry - Add a manager entry.
449 * @manager: The path to manager or the domainnamme.
450 * @is_delete: True if it is a delete request.
452 * Returns 0 on success, negative value otherwise.
454 * Caller holds tomoyo_read_lock().
456 static int tomoyo_update_manager_entry(const char *manager
,
457 const bool is_delete
)
459 struct tomoyo_policy_manager_entry e
= { };
462 if (tomoyo_domain_def(manager
)) {
463 if (!tomoyo_correct_domain(manager
))
467 if (!tomoyo_correct_path(manager
))
470 e
.manager
= tomoyo_get_name(manager
);
473 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), is_delete
,
474 &tomoyo_policy_list
[TOMOYO_ID_MANAGER
],
475 tomoyo_same_manager_entry
);
476 tomoyo_put_name(e
.manager
);
481 * tomoyo_write_manager_policy - Write manager policy.
483 * @head: Pointer to "struct tomoyo_io_buffer".
485 * Returns 0 on success, negative value otherwise.
487 * Caller holds tomoyo_read_lock().
489 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer
*head
)
491 char *data
= head
->write_buf
;
492 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
494 if (!strcmp(data
, "manage_by_non_root")) {
495 tomoyo_manage_by_non_root
= !is_delete
;
498 return tomoyo_update_manager_entry(data
, is_delete
);
502 * tomoyo_read_manager_policy - Read manager policy.
504 * @head: Pointer to "struct tomoyo_io_buffer".
506 * Caller holds tomoyo_read_lock().
508 static void tomoyo_read_manager_policy(struct tomoyo_io_buffer
*head
)
510 struct list_head
*pos
;
515 list_for_each_cookie(pos
, head
->read_var2
,
516 &tomoyo_policy_list
[TOMOYO_ID_MANAGER
]) {
517 struct tomoyo_policy_manager_entry
*ptr
;
518 ptr
= list_entry(pos
, struct tomoyo_policy_manager_entry
,
520 if (ptr
->head
.is_deleted
)
522 done
= tomoyo_io_printf(head
, "%s\n", ptr
->manager
->name
);
526 head
->read_eof
= done
;
530 * tomoyo_policy_manager - Check whether the current process is a policy manager.
532 * Returns true if the current process is permitted to modify policy
533 * via /sys/kernel/security/tomoyo/ interface.
535 * Caller holds tomoyo_read_lock().
537 static bool tomoyo_policy_manager(void)
539 struct tomoyo_policy_manager_entry
*ptr
;
541 const struct task_struct
*task
= current
;
542 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
545 if (!tomoyo_policy_loaded
)
547 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
549 list_for_each_entry_rcu(ptr
, &tomoyo_policy_list
[TOMOYO_ID_MANAGER
],
551 if (!ptr
->head
.is_deleted
&& ptr
->is_domain
552 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
559 exe
= tomoyo_get_exe();
562 list_for_each_entry_rcu(ptr
, &tomoyo_policy_list
[TOMOYO_ID_MANAGER
],
564 if (!ptr
->head
.is_deleted
&& !ptr
->is_domain
565 && !strcmp(exe
, ptr
->manager
->name
)) {
570 if (!found
) { /* Reduce error messages. */
571 static pid_t last_pid
;
572 const pid_t pid
= current
->pid
;
573 if (last_pid
!= pid
) {
574 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
575 "update policies.\n", domainname
->name
, exe
);
584 * tomoyo_select_one - Parse select command.
586 * @head: Pointer to "struct tomoyo_io_buffer".
587 * @data: String to parse.
589 * Returns true on success, false otherwise.
591 * Caller holds tomoyo_read_lock().
593 static bool tomoyo_select_one(struct tomoyo_io_buffer
*head
,
597 struct tomoyo_domain_info
*domain
= NULL
;
598 bool global_pid
= false;
600 if (sscanf(data
, "pid=%u", &pid
) == 1 ||
601 (global_pid
= true, sscanf(data
, "global-pid=%u", &pid
) == 1)) {
602 struct task_struct
*p
;
604 read_lock(&tasklist_lock
);
606 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
608 p
= find_task_by_vpid(pid
);
610 domain
= tomoyo_real_domain(p
);
611 read_unlock(&tasklist_lock
);
613 } else if (!strncmp(data
, "domain=", 7)) {
614 if (tomoyo_domain_def(data
+ 7))
615 domain
= tomoyo_find_domain(data
+ 7);
618 head
->write_var1
= domain
;
619 /* Accessing read_buf is safe because head->io_sem is held. */
621 return true; /* Do nothing if open(O_WRONLY). */
622 head
->read_avail
= 0;
623 tomoyo_io_printf(head
, "# select %s\n", data
);
624 head
->read_single_domain
= true;
625 head
->read_eof
= !domain
;
627 struct tomoyo_domain_info
*d
;
628 head
->read_var1
= NULL
;
629 list_for_each_entry_rcu(d
, &tomoyo_domain_list
, list
) {
632 head
->read_var1
= &d
->list
;
634 head
->read_var2
= NULL
;
637 if (domain
->is_deleted
)
638 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
644 * tomoyo_delete_domain - Delete a domain.
646 * @domainname: The name of domain.
650 * Caller holds tomoyo_read_lock().
652 static int tomoyo_delete_domain(char *domainname
)
654 struct tomoyo_domain_info
*domain
;
655 struct tomoyo_path_info name
;
657 name
.name
= domainname
;
658 tomoyo_fill_path_info(&name
);
659 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
661 /* Is there an active domain? */
662 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
663 /* Never delete tomoyo_kernel_domain */
664 if (domain
== &tomoyo_kernel_domain
)
666 if (domain
->is_deleted
||
667 tomoyo_pathcmp(domain
->domainname
, &name
))
669 domain
->is_deleted
= true;
672 mutex_unlock(&tomoyo_policy_lock
);
677 * tomoyo_write_domain_policy2 - Write domain policy.
679 * @head: Pointer to "struct tomoyo_io_buffer".
681 * Returns 0 on success, negative value otherwise.
683 * Caller holds tomoyo_read_lock().
685 static int tomoyo_write_domain_policy2(char *data
,
686 struct tomoyo_domain_info
*domain
,
687 const bool is_delete
)
689 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_MOUNT
))
690 return tomoyo_write_mount_policy(data
, domain
, is_delete
);
691 return tomoyo_write_file_policy(data
, domain
, is_delete
);
695 * tomoyo_write_domain_policy - Write domain policy.
697 * @head: Pointer to "struct tomoyo_io_buffer".
699 * Returns 0 on success, negative value otherwise.
701 * Caller holds tomoyo_read_lock().
703 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer
*head
)
705 char *data
= head
->write_buf
;
706 struct tomoyo_domain_info
*domain
= head
->write_var1
;
707 bool is_delete
= false;
708 bool is_select
= false;
709 unsigned int profile
;
711 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
))
713 else if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_SELECT
))
715 if (is_select
&& tomoyo_select_one(head
, data
))
717 /* Don't allow updating policies by non manager programs. */
718 if (!tomoyo_policy_manager())
720 if (tomoyo_domain_def(data
)) {
723 tomoyo_delete_domain(data
);
725 domain
= tomoyo_find_domain(data
);
727 domain
= tomoyo_find_or_assign_new_domain(data
, 0);
728 head
->write_var1
= domain
;
734 if (sscanf(data
, TOMOYO_KEYWORD_USE_PROFILE
"%u", &profile
) == 1
735 && profile
< TOMOYO_MAX_PROFILES
) {
736 if (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
)
737 domain
->profile
= (u8
) profile
;
740 if (!strcmp(data
, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
)) {
741 domain
->ignore_global_allow_read
= !is_delete
;
744 if (!strcmp(data
, TOMOYO_KEYWORD_QUOTA_EXCEEDED
)) {
745 domain
->quota_warned
= !is_delete
;
748 if (!strcmp(data
, TOMOYO_KEYWORD_TRANSITION_FAILED
)) {
749 domain
->transition_failed
= !is_delete
;
752 return tomoyo_write_domain_policy2(data
, domain
, is_delete
);
756 * tomoyo_print_path_acl - Print a single path ACL entry.
758 * @head: Pointer to "struct tomoyo_io_buffer".
759 * @ptr: Pointer to "struct tomoyo_path_acl".
761 * Returns true on success, false otherwise.
763 static bool tomoyo_print_path_acl(struct tomoyo_io_buffer
*head
,
764 struct tomoyo_path_acl
*ptr
)
768 const u16 perm
= ptr
->perm
;
770 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH_OPERATION
; bit
++) {
771 if (!(perm
& (1 << bit
)))
773 /* Print "read/write" instead of "read" and "write". */
774 if ((bit
== TOMOYO_TYPE_READ
|| bit
== TOMOYO_TYPE_WRITE
)
775 && (perm
& (1 << TOMOYO_TYPE_READ_WRITE
)))
777 pos
= head
->read_avail
;
778 if (!tomoyo_io_printf(head
, "allow_%s ",
779 tomoyo_path_keyword
[bit
]) ||
780 !tomoyo_print_name_union(head
, &ptr
->name
) ||
781 !tomoyo_io_printf(head
, "\n"))
787 head
->read_bit
= bit
;
788 head
->read_avail
= pos
;
793 * tomoyo_print_path2_acl - Print a double path ACL entry.
795 * @head: Pointer to "struct tomoyo_io_buffer".
796 * @ptr: Pointer to "struct tomoyo_path2_acl".
798 * Returns true on success, false otherwise.
800 static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer
*head
,
801 struct tomoyo_path2_acl
*ptr
)
804 const u8 perm
= ptr
->perm
;
807 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH2_OPERATION
; bit
++) {
808 if (!(perm
& (1 << bit
)))
810 pos
= head
->read_avail
;
811 if (!tomoyo_io_printf(head
, "allow_%s ",
812 tomoyo_path2_keyword
[bit
]) ||
813 !tomoyo_print_name_union(head
, &ptr
->name1
) ||
814 !tomoyo_print_name_union(head
, &ptr
->name2
) ||
815 !tomoyo_io_printf(head
, "\n"))
821 head
->read_bit
= bit
;
822 head
->read_avail
= pos
;
827 * tomoyo_print_path_number_acl - Print a path_number ACL entry.
829 * @head: Pointer to "struct tomoyo_io_buffer".
830 * @ptr: Pointer to "struct tomoyo_path_number_acl".
832 * Returns true on success, false otherwise.
834 static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer
*head
,
835 struct tomoyo_path_number_acl
*ptr
)
839 const u8 perm
= ptr
->perm
;
840 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH_NUMBER_OPERATION
;
842 if (!(perm
& (1 << bit
)))
844 pos
= head
->read_avail
;
845 if (!tomoyo_io_printf(head
, "allow_%s",
846 tomoyo_path_number_keyword
[bit
]) ||
847 !tomoyo_print_name_union(head
, &ptr
->name
) ||
848 !tomoyo_print_number_union(head
, &ptr
->number
) ||
849 !tomoyo_io_printf(head
, "\n"))
855 head
->read_bit
= bit
;
856 head
->read_avail
= pos
;
861 * tomoyo_print_mkdev_acl - Print a mkdev ACL entry.
863 * @head: Pointer to "struct tomoyo_io_buffer".
864 * @ptr: Pointer to "struct tomoyo_mkdev_acl".
866 * Returns true on success, false otherwise.
868 static bool tomoyo_print_mkdev_acl(struct tomoyo_io_buffer
*head
,
869 struct tomoyo_mkdev_acl
*ptr
)
873 const u16 perm
= ptr
->perm
;
874 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_MKDEV_OPERATION
;
876 if (!(perm
& (1 << bit
)))
878 pos
= head
->read_avail
;
879 if (!tomoyo_io_printf(head
, "allow_%s",
880 tomoyo_mkdev_keyword
[bit
]) ||
881 !tomoyo_print_name_union(head
, &ptr
->name
) ||
882 !tomoyo_print_number_union(head
, &ptr
->mode
) ||
883 !tomoyo_print_number_union(head
, &ptr
->major
) ||
884 !tomoyo_print_number_union(head
, &ptr
->minor
) ||
885 !tomoyo_io_printf(head
, "\n"))
891 head
->read_bit
= bit
;
892 head
->read_avail
= pos
;
897 * tomoyo_print_mount_acl - Print a mount ACL entry.
899 * @head: Pointer to "struct tomoyo_io_buffer".
900 * @ptr: Pointer to "struct tomoyo_mount_acl".
902 * Returns true on success, false otherwise.
904 static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer
*head
,
905 struct tomoyo_mount_acl
*ptr
)
907 const int pos
= head
->read_avail
;
908 if (!tomoyo_io_printf(head
, TOMOYO_KEYWORD_ALLOW_MOUNT
) ||
909 !tomoyo_print_name_union(head
, &ptr
->dev_name
) ||
910 !tomoyo_print_name_union(head
, &ptr
->dir_name
) ||
911 !tomoyo_print_name_union(head
, &ptr
->fs_type
) ||
912 !tomoyo_print_number_union(head
, &ptr
->flags
) ||
913 !tomoyo_io_printf(head
, "\n")) {
914 head
->read_avail
= pos
;
921 * tomoyo_print_entry - Print an ACL entry.
923 * @head: Pointer to "struct tomoyo_io_buffer".
924 * @ptr: Pointer to an ACL entry.
926 * Returns true on success, false otherwise.
928 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
929 struct tomoyo_acl_info
*ptr
)
931 const u8 acl_type
= ptr
->type
;
935 if (acl_type
== TOMOYO_TYPE_PATH_ACL
) {
936 struct tomoyo_path_acl
*acl
937 = container_of(ptr
, struct tomoyo_path_acl
, head
);
938 return tomoyo_print_path_acl(head
, acl
);
940 if (acl_type
== TOMOYO_TYPE_PATH2_ACL
) {
941 struct tomoyo_path2_acl
*acl
942 = container_of(ptr
, struct tomoyo_path2_acl
, head
);
943 return tomoyo_print_path2_acl(head
, acl
);
945 if (acl_type
== TOMOYO_TYPE_PATH_NUMBER_ACL
) {
946 struct tomoyo_path_number_acl
*acl
947 = container_of(ptr
, struct tomoyo_path_number_acl
,
949 return tomoyo_print_path_number_acl(head
, acl
);
951 if (acl_type
== TOMOYO_TYPE_MKDEV_ACL
) {
952 struct tomoyo_mkdev_acl
*acl
953 = container_of(ptr
, struct tomoyo_mkdev_acl
,
955 return tomoyo_print_mkdev_acl(head
, acl
);
957 if (acl_type
== TOMOYO_TYPE_MOUNT_ACL
) {
958 struct tomoyo_mount_acl
*acl
959 = container_of(ptr
, struct tomoyo_mount_acl
, head
);
960 return tomoyo_print_mount_acl(head
, acl
);
962 BUG(); /* This must not happen. */
967 * tomoyo_read_domain_policy - Read domain policy.
969 * @head: Pointer to "struct tomoyo_io_buffer".
971 * Caller holds tomoyo_read_lock().
973 static void tomoyo_read_domain_policy(struct tomoyo_io_buffer
*head
)
975 struct list_head
*dpos
;
976 struct list_head
*apos
;
981 if (head
->read_step
== 0)
983 list_for_each_cookie(dpos
, head
->read_var1
, &tomoyo_domain_list
) {
984 struct tomoyo_domain_info
*domain
;
985 const char *quota_exceeded
= "";
986 const char *transition_failed
= "";
987 const char *ignore_global_allow_read
= "";
988 domain
= list_entry(dpos
, struct tomoyo_domain_info
, list
);
989 if (head
->read_step
!= 1)
991 if (domain
->is_deleted
&& !head
->read_single_domain
)
993 /* Print domainname and flags. */
994 if (domain
->quota_warned
)
995 quota_exceeded
= "quota_exceeded\n";
996 if (domain
->transition_failed
)
997 transition_failed
= "transition_failed\n";
998 if (domain
->ignore_global_allow_read
)
999 ignore_global_allow_read
1000 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
"\n";
1001 done
= tomoyo_io_printf(head
, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1003 domain
->domainname
->name
,
1004 domain
->profile
, quota_exceeded
,
1006 ignore_global_allow_read
);
1009 head
->read_step
= 2;
1011 if (head
->read_step
== 3)
1013 /* Print ACL entries in the domain. */
1014 list_for_each_cookie(apos
, head
->read_var2
,
1015 &domain
->acl_info_list
) {
1016 struct tomoyo_acl_info
*ptr
1017 = list_entry(apos
, struct tomoyo_acl_info
,
1019 done
= tomoyo_print_entry(head
, ptr
);
1025 head
->read_step
= 3;
1027 done
= tomoyo_io_printf(head
, "\n");
1030 head
->read_step
= 1;
1031 if (head
->read_single_domain
)
1034 head
->read_eof
= done
;
1038 * tomoyo_write_domain_profile - Assign profile for specified domain.
1040 * @head: Pointer to "struct tomoyo_io_buffer".
1042 * Returns 0 on success, -EINVAL otherwise.
1044 * This is equivalent to doing
1046 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1047 * /usr/sbin/tomoyo-loadpolicy -d
1049 * Caller holds tomoyo_read_lock().
1051 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer
*head
)
1053 char *data
= head
->write_buf
;
1054 char *cp
= strchr(data
, ' ');
1055 struct tomoyo_domain_info
*domain
;
1056 unsigned long profile
;
1061 domain
= tomoyo_find_domain(cp
+ 1);
1062 if (strict_strtoul(data
, 10, &profile
))
1064 if (domain
&& profile
< TOMOYO_MAX_PROFILES
1065 && (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
))
1066 domain
->profile
= (u8
) profile
;
1071 * tomoyo_read_domain_profile - Read only domainname and profile.
1073 * @head: Pointer to "struct tomoyo_io_buffer".
1075 * Returns list of profile number and domainname pairs.
1077 * This is equivalent to doing
1079 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1080 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1081 * domainname = $0; } else if ( $1 == "use_profile" ) {
1082 * print $2 " " domainname; domainname = ""; } } ; '
1084 * Caller holds tomoyo_read_lock().
1086 static void tomoyo_read_domain_profile(struct tomoyo_io_buffer
*head
)
1088 struct list_head
*pos
;
1093 list_for_each_cookie(pos
, head
->read_var1
, &tomoyo_domain_list
) {
1094 struct tomoyo_domain_info
*domain
;
1095 domain
= list_entry(pos
, struct tomoyo_domain_info
, list
);
1096 if (domain
->is_deleted
)
1098 done
= tomoyo_io_printf(head
, "%u %s\n", domain
->profile
,
1099 domain
->domainname
->name
);
1103 head
->read_eof
= done
;
1107 * tomoyo_write_pid: Specify PID to obtain domainname.
1109 * @head: Pointer to "struct tomoyo_io_buffer".
1113 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1116 /* No error check. */
1117 strict_strtoul(head
->write_buf
, 10, &pid
);
1118 head
->read_step
= (int) pid
;
1119 head
->read_eof
= false;
1124 * tomoyo_read_pid - Get domainname of the specified PID.
1126 * @head: Pointer to "struct tomoyo_io_buffer".
1128 * Returns the domainname which the specified PID is in on success,
1129 * empty string otherwise.
1130 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1131 * using read()/write() interface rather than sysctl() interface.
1133 static void tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1135 if (head
->read_avail
== 0 && !head
->read_eof
) {
1136 const int pid
= head
->read_step
;
1137 struct task_struct
*p
;
1138 struct tomoyo_domain_info
*domain
= NULL
;
1140 read_lock(&tasklist_lock
);
1141 p
= find_task_by_vpid(pid
);
1143 domain
= tomoyo_real_domain(p
);
1144 read_unlock(&tasklist_lock
);
1147 tomoyo_io_printf(head
, "%d %u %s", pid
, domain
->profile
,
1148 domain
->domainname
->name
);
1149 head
->read_eof
= true;
1153 static const char *tomoyo_transition_type
[TOMOYO_MAX_TRANSITION_TYPE
] = {
1154 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE
]
1155 = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN
,
1156 [TOMOYO_TRANSITION_CONTROL_INITIALIZE
]
1157 = TOMOYO_KEYWORD_INITIALIZE_DOMAIN
,
1158 [TOMOYO_TRANSITION_CONTROL_NO_KEEP
] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN
,
1159 [TOMOYO_TRANSITION_CONTROL_KEEP
] = TOMOYO_KEYWORD_KEEP_DOMAIN
1163 * tomoyo_write_exception_policy - Write exception policy.
1165 * @head: Pointer to "struct tomoyo_io_buffer".
1167 * Returns 0 on success, negative value otherwise.
1169 * Caller holds tomoyo_read_lock().
1171 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer
*head
)
1173 char *data
= head
->write_buf
;
1174 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1177 for (i
= 0; i
< TOMOYO_MAX_TRANSITION_TYPE
; i
++) {
1178 if (tomoyo_str_starts(&data
, tomoyo_transition_type
[i
]))
1179 return tomoyo_write_transition_control(data
, is_delete
,
1182 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_AGGREGATOR
))
1183 return tomoyo_write_aggregator_policy(data
, is_delete
);
1184 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_READ
))
1185 return tomoyo_write_globally_readable_policy(data
, is_delete
);
1186 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_FILE_PATTERN
))
1187 return tomoyo_write_pattern_policy(data
, is_delete
);
1188 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DENY_REWRITE
))
1189 return tomoyo_write_no_rewrite_policy(data
, is_delete
);
1190 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_PATH_GROUP
))
1191 return tomoyo_write_group(data
, is_delete
, TOMOYO_PATH_GROUP
);
1192 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NUMBER_GROUP
))
1193 return tomoyo_write_group(data
, is_delete
, TOMOYO_NUMBER_GROUP
);
1197 static void tomoyo_print_number(char *buffer
, int buffer_len
,
1198 const struct tomoyo_number_union
*ptr
)
1201 unsigned long min
= ptr
->values
[0];
1202 const unsigned long max
= ptr
->values
[1];
1203 u8 min_type
= ptr
->min_type
;
1204 const u8 max_type
= ptr
->max_type
;
1205 memset(buffer
, 0, buffer_len
);
1207 for (i
= 0; i
< 2; i
++) {
1210 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
1211 snprintf(buffer
, buffer_len
, "0x%lX", min
);
1213 case TOMOYO_VALUE_TYPE_OCTAL
:
1214 snprintf(buffer
, buffer_len
, "0%lo", min
);
1217 snprintf(buffer
, buffer_len
, "%lu", min
);
1220 if (min
== max
&& min_type
== max_type
)
1222 len
= strlen(buffer
);
1223 buffer
[len
++] = '-';
1226 min_type
= max_type
;
1231 static const char *tomoyo_group_name
[TOMOYO_MAX_GROUP
] = {
1232 [TOMOYO_PATH_GROUP
] = TOMOYO_KEYWORD_PATH_GROUP
,
1233 [TOMOYO_NUMBER_GROUP
] = TOMOYO_KEYWORD_NUMBER_GROUP
1237 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1239 * @head: Pointer to "struct tomoyo_io_buffer".
1240 * @idx: Index number.
1242 * Returns true on success, false otherwise.
1244 * Caller holds tomoyo_read_lock().
1246 static bool tomoyo_read_group(struct tomoyo_io_buffer
*head
, const int idx
)
1248 struct list_head
*gpos
;
1249 struct list_head
*mpos
;
1250 const char *w
[3] = { "", "", "" };
1251 w
[0] = tomoyo_group_name
[idx
];
1252 list_for_each_cookie(gpos
, head
->read_var1
, &tomoyo_group_list
[idx
]) {
1253 struct tomoyo_group
*group
=
1254 list_entry(gpos
, struct tomoyo_group
, list
);
1255 w
[1] = group
->group_name
->name
;
1256 list_for_each_cookie(mpos
, head
->read_var2
,
1257 &group
->member_list
) {
1259 struct tomoyo_acl_head
*ptr
=
1260 list_entry(mpos
, struct tomoyo_acl_head
, list
);
1261 if (ptr
->is_deleted
)
1263 if (idx
== TOMOYO_PATH_GROUP
) {
1264 w
[2] = container_of(ptr
,
1265 struct tomoyo_path_group
,
1266 head
)->member_name
->name
;
1267 } else if (idx
== TOMOYO_NUMBER_GROUP
) {
1268 tomoyo_print_number(buffer
, sizeof(buffer
),
1271 tomoyo_number_group
,
1275 if (!tomoyo_io_printf(head
, "%s%s %s\n", w
[0], w
[1],
1284 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1286 * @head: Pointer to "struct tomoyo_io_buffer".
1287 * @idx: Index number.
1289 * Returns true on success, false otherwise.
1291 * Caller holds tomoyo_read_lock().
1293 static bool tomoyo_read_policy(struct tomoyo_io_buffer
*head
, const int idx
)
1295 struct list_head
*pos
;
1296 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_policy_list
[idx
]) {
1297 const char *w
[4] = { "", "", "", "" };
1298 struct tomoyo_acl_head
*acl
= container_of(pos
, typeof(*acl
),
1300 if (acl
->is_deleted
)
1303 case TOMOYO_ID_TRANSITION_CONTROL
:
1305 struct tomoyo_transition_control
*ptr
=
1306 container_of(acl
, typeof(*ptr
), head
);
1307 w
[0] = tomoyo_transition_type
[ptr
->type
];
1309 w
[1] = ptr
->program
->name
;
1310 if (ptr
->domainname
)
1311 w
[3] = ptr
->domainname
->name
;
1312 if (w
[1][0] && w
[3][0])
1316 case TOMOYO_ID_GLOBALLY_READABLE
:
1318 struct tomoyo_globally_readable_file_entry
*ptr
1319 = container_of(acl
, typeof(*ptr
), head
);
1320 w
[0] = TOMOYO_KEYWORD_ALLOW_READ
;
1321 w
[1] = ptr
->filename
->name
;
1324 case TOMOYO_ID_AGGREGATOR
:
1326 struct tomoyo_aggregator_entry
*ptr
=
1327 container_of(acl
, typeof(*ptr
), head
);
1328 w
[0] = TOMOYO_KEYWORD_AGGREGATOR
;
1329 w
[1] = ptr
->original_name
->name
;
1331 w
[3] = ptr
->aggregated_name
->name
;
1334 case TOMOYO_ID_PATTERN
:
1336 struct tomoyo_pattern_entry
*ptr
=
1337 container_of(acl
, typeof(*ptr
), head
);
1338 w
[0] = TOMOYO_KEYWORD_FILE_PATTERN
;
1339 w
[1] = ptr
->pattern
->name
;
1342 case TOMOYO_ID_NO_REWRITE
:
1344 struct tomoyo_no_rewrite_entry
*ptr
=
1345 container_of(acl
, typeof(*ptr
), head
);
1346 w
[0] = TOMOYO_KEYWORD_DENY_REWRITE
;
1347 w
[1] = ptr
->pattern
->name
;
1353 if (!tomoyo_io_printf(head
, "%s%s%s%s\n", w
[0], w
[1], w
[2],
1361 * tomoyo_read_exception_policy - Read exception policy.
1363 * @head: Pointer to "struct tomoyo_io_buffer".
1365 * Caller holds tomoyo_read_lock().
1367 static void tomoyo_read_exception_policy(struct tomoyo_io_buffer
*head
)
1371 while (head
->read_step
< TOMOYO_MAX_POLICY
&&
1372 tomoyo_read_policy(head
, head
->read_step
))
1374 if (head
->read_step
< TOMOYO_MAX_POLICY
)
1376 while (head
->read_step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
&&
1377 tomoyo_read_group(head
, head
->read_step
- TOMOYO_MAX_POLICY
))
1379 if (head
->read_step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
)
1381 head
->read_eof
= true;
1385 * tomoyo_print_header - Get header line of audit log.
1387 * @r: Pointer to "struct tomoyo_request_info".
1389 * Returns string representation.
1391 * This function uses kmalloc(), so caller must kfree() if this function
1392 * didn't return NULL.
1394 static char *tomoyo_print_header(struct tomoyo_request_info
*r
)
1396 static const char *tomoyo_mode_4
[4] = {
1397 "disabled", "learning", "permissive", "enforcing"
1400 const pid_t gpid
= task_pid_nr(current
);
1401 static const int tomoyo_buffer_len
= 4096;
1402 char *buffer
= kmalloc(tomoyo_buffer_len
, GFP_NOFS
);
1405 do_gettimeofday(&tv
);
1406 snprintf(buffer
, tomoyo_buffer_len
- 1,
1407 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1408 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1409 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1410 tv
.tv_sec
, r
->profile
, tomoyo_mode_4
[r
->mode
], gpid
,
1411 (pid_t
) sys_getpid(), (pid_t
) sys_getppid(),
1412 current_uid(), current_gid(), current_euid(),
1413 current_egid(), current_suid(), current_sgid(),
1414 current_fsuid(), current_fsgid());
1419 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1421 * @len: Required size.
1422 * @r: Pointer to "struct tomoyo_request_info".
1424 * Returns pointer to allocated memory.
1426 * The @len is updated to add the header lines' size on success.
1428 * This function uses kzalloc(), so caller must kfree() if this function
1429 * didn't return NULL.
1431 static char *tomoyo_init_audit_log(int *len
, struct tomoyo_request_info
*r
)
1435 const char *domainname
;
1437 r
->domain
= tomoyo_domain();
1438 domainname
= r
->domain
->domainname
->name
;
1439 header
= tomoyo_print_header(r
);
1442 *len
+= strlen(domainname
) + strlen(header
) + 10;
1443 buf
= kzalloc(*len
, GFP_NOFS
);
1445 snprintf(buf
, (*len
) - 1, "%s\n%s\n", header
, domainname
);
1450 /* Wait queue for tomoyo_query_list. */
1451 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait
);
1453 /* Lock for manipulating tomoyo_query_list. */
1454 static DEFINE_SPINLOCK(tomoyo_query_list_lock
);
1456 /* Structure for query. */
1457 struct tomoyo_query_entry
{
1458 struct list_head list
;
1461 unsigned int serial
;
1466 /* The list for "struct tomoyo_query_entry". */
1467 static LIST_HEAD(tomoyo_query_list
);
1470 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1473 static atomic_t tomoyo_query_observers
= ATOMIC_INIT(0);
1476 * tomoyo_supervisor - Ask for the supervisor's decision.
1478 * @r: Pointer to "struct tomoyo_request_info".
1479 * @fmt: The printf()'s format string, followed by parameters.
1481 * Returns 0 if the supervisor decided to permit the access request which
1482 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1483 * supervisor decided to retry the access request which violated the policy in
1484 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1486 int tomoyo_supervisor(struct tomoyo_request_info
*r
, const char *fmt
, ...)
1492 static unsigned int tomoyo_serial
;
1493 struct tomoyo_query_entry
*tomoyo_query_entry
= NULL
;
1494 bool quota_exceeded
= false;
1498 case TOMOYO_CONFIG_LEARNING
:
1499 if (!tomoyo_domain_quota_is_ok(r
))
1501 va_start(args
, fmt
);
1502 len
= vsnprintf((char *) &pos
, sizeof(pos
) - 1, fmt
, args
) + 4;
1504 buffer
= kmalloc(len
, GFP_NOFS
);
1507 va_start(args
, fmt
);
1508 vsnprintf(buffer
, len
- 1, fmt
, args
);
1510 tomoyo_normalize_line(buffer
);
1511 tomoyo_write_domain_policy2(buffer
, r
->domain
, false);
1514 case TOMOYO_CONFIG_PERMISSIVE
:
1518 r
->domain
= tomoyo_domain();
1519 if (!atomic_read(&tomoyo_query_observers
))
1521 va_start(args
, fmt
);
1522 len
= vsnprintf((char *) &pos
, sizeof(pos
) - 1, fmt
, args
) + 32;
1524 header
= tomoyo_init_audit_log(&len
, r
);
1527 tomoyo_query_entry
= kzalloc(sizeof(*tomoyo_query_entry
), GFP_NOFS
);
1528 if (!tomoyo_query_entry
)
1530 tomoyo_query_entry
->query
= kzalloc(len
, GFP_NOFS
);
1531 if (!tomoyo_query_entry
->query
)
1533 len
= ksize(tomoyo_query_entry
->query
);
1534 INIT_LIST_HEAD(&tomoyo_query_entry
->list
);
1535 spin_lock(&tomoyo_query_list_lock
);
1536 if (tomoyo_quota_for_query
&& tomoyo_query_memory_size
+ len
+
1537 sizeof(*tomoyo_query_entry
) >= tomoyo_quota_for_query
) {
1538 quota_exceeded
= true;
1540 tomoyo_query_memory_size
+= len
+ sizeof(*tomoyo_query_entry
);
1541 tomoyo_query_entry
->serial
= tomoyo_serial
++;
1543 spin_unlock(&tomoyo_query_list_lock
);
1546 pos
= snprintf(tomoyo_query_entry
->query
, len
- 1, "Q%u-%hu\n%s",
1547 tomoyo_query_entry
->serial
, r
->retry
, header
);
1550 va_start(args
, fmt
);
1551 vsnprintf(tomoyo_query_entry
->query
+ pos
, len
- 1 - pos
, fmt
, args
);
1552 tomoyo_query_entry
->query_len
= strlen(tomoyo_query_entry
->query
) + 1;
1554 spin_lock(&tomoyo_query_list_lock
);
1555 list_add_tail(&tomoyo_query_entry
->list
, &tomoyo_query_list
);
1556 spin_unlock(&tomoyo_query_list_lock
);
1557 /* Give 10 seconds for supervisor's opinion. */
1558 for (tomoyo_query_entry
->timer
= 0;
1559 atomic_read(&tomoyo_query_observers
) && tomoyo_query_entry
->timer
< 100;
1560 tomoyo_query_entry
->timer
++) {
1561 wake_up(&tomoyo_query_wait
);
1562 set_current_state(TASK_INTERRUPTIBLE
);
1563 schedule_timeout(HZ
/ 10);
1564 if (tomoyo_query_entry
->answer
)
1567 spin_lock(&tomoyo_query_list_lock
);
1568 list_del(&tomoyo_query_entry
->list
);
1569 tomoyo_query_memory_size
-= len
+ sizeof(*tomoyo_query_entry
);
1570 spin_unlock(&tomoyo_query_list_lock
);
1571 switch (tomoyo_query_entry
->answer
) {
1572 case 3: /* Asked to retry by administrator. */
1573 error
= TOMOYO_RETRY_REQUEST
;
1577 /* Granted by administrator. */
1584 /* Rejected by administrator. */
1588 if (tomoyo_query_entry
)
1589 kfree(tomoyo_query_entry
->query
);
1590 kfree(tomoyo_query_entry
);
1596 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1598 * @file: Pointer to "struct file".
1599 * @wait: Pointer to "poll_table".
1601 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1603 * Waits for access requests which violated policy in enforcing mode.
1605 static int tomoyo_poll_query(struct file
*file
, poll_table
*wait
)
1607 struct list_head
*tmp
;
1610 for (i
= 0; i
< 2; i
++) {
1611 spin_lock(&tomoyo_query_list_lock
);
1612 list_for_each(tmp
, &tomoyo_query_list
) {
1613 struct tomoyo_query_entry
*ptr
1614 = list_entry(tmp
, struct tomoyo_query_entry
,
1621 spin_unlock(&tomoyo_query_list_lock
);
1623 return POLLIN
| POLLRDNORM
;
1626 poll_wait(file
, &tomoyo_query_wait
, wait
);
1632 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1634 * @head: Pointer to "struct tomoyo_io_buffer".
1636 static void tomoyo_read_query(struct tomoyo_io_buffer
*head
)
1638 struct list_head
*tmp
;
1642 if (head
->read_avail
)
1644 if (head
->read_buf
) {
1645 kfree(head
->read_buf
);
1646 head
->read_buf
= NULL
;
1647 head
->readbuf_size
= 0;
1649 spin_lock(&tomoyo_query_list_lock
);
1650 list_for_each(tmp
, &tomoyo_query_list
) {
1651 struct tomoyo_query_entry
*ptr
1652 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1655 if (pos
++ != head
->read_step
)
1657 len
= ptr
->query_len
;
1660 spin_unlock(&tomoyo_query_list_lock
);
1662 head
->read_step
= 0;
1665 buf
= kzalloc(len
, GFP_NOFS
);
1669 spin_lock(&tomoyo_query_list_lock
);
1670 list_for_each(tmp
, &tomoyo_query_list
) {
1671 struct tomoyo_query_entry
*ptr
1672 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1675 if (pos
++ != head
->read_step
)
1678 * Some query can be skipped because tomoyo_query_list
1679 * can change, but I don't care.
1681 if (len
== ptr
->query_len
)
1682 memmove(buf
, ptr
->query
, len
);
1685 spin_unlock(&tomoyo_query_list_lock
);
1687 head
->read_avail
= len
;
1688 head
->readbuf_size
= head
->read_avail
;
1689 head
->read_buf
= buf
;
1697 * tomoyo_write_answer - Write the supervisor's decision.
1699 * @head: Pointer to "struct tomoyo_io_buffer".
1701 * Returns 0 on success, -EINVAL otherwise.
1703 static int tomoyo_write_answer(struct tomoyo_io_buffer
*head
)
1705 char *data
= head
->write_buf
;
1706 struct list_head
*tmp
;
1707 unsigned int serial
;
1708 unsigned int answer
;
1709 spin_lock(&tomoyo_query_list_lock
);
1710 list_for_each(tmp
, &tomoyo_query_list
) {
1711 struct tomoyo_query_entry
*ptr
1712 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1715 spin_unlock(&tomoyo_query_list_lock
);
1716 if (sscanf(data
, "A%u=%u", &serial
, &answer
) != 2)
1718 spin_lock(&tomoyo_query_list_lock
);
1719 list_for_each(tmp
, &tomoyo_query_list
) {
1720 struct tomoyo_query_entry
*ptr
1721 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1722 if (ptr
->serial
!= serial
)
1725 ptr
->answer
= answer
;
1728 spin_unlock(&tomoyo_query_list_lock
);
1733 * tomoyo_read_version: Get version.
1735 * @head: Pointer to "struct tomoyo_io_buffer".
1737 * Returns version information.
1739 static void tomoyo_read_version(struct tomoyo_io_buffer
*head
)
1741 if (!head
->read_eof
) {
1742 tomoyo_io_printf(head
, "2.3.0-pre");
1743 head
->read_eof
= true;
1748 * tomoyo_read_self_domain - Get the current process's domainname.
1750 * @head: Pointer to "struct tomoyo_io_buffer".
1752 * Returns the current process's domainname.
1754 static void tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
1756 if (!head
->read_eof
) {
1758 * tomoyo_domain()->domainname != NULL
1759 * because every process belongs to a domain and
1760 * the domain's name cannot be NULL.
1762 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
1763 head
->read_eof
= true;
1768 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1770 * @type: Type of interface.
1771 * @file: Pointer to "struct file".
1773 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1775 * Caller acquires tomoyo_read_lock().
1777 int tomoyo_open_control(const u8 type
, struct file
*file
)
1779 struct tomoyo_io_buffer
*head
= kzalloc(sizeof(*head
), GFP_NOFS
);
1783 mutex_init(&head
->io_sem
);
1786 case TOMOYO_DOMAINPOLICY
:
1787 /* /sys/kernel/security/tomoyo/domain_policy */
1788 head
->write
= tomoyo_write_domain_policy
;
1789 head
->read
= tomoyo_read_domain_policy
;
1791 case TOMOYO_EXCEPTIONPOLICY
:
1792 /* /sys/kernel/security/tomoyo/exception_policy */
1793 head
->write
= tomoyo_write_exception_policy
;
1794 head
->read
= tomoyo_read_exception_policy
;
1796 case TOMOYO_SELFDOMAIN
:
1797 /* /sys/kernel/security/tomoyo/self_domain */
1798 head
->read
= tomoyo_read_self_domain
;
1800 case TOMOYO_DOMAIN_STATUS
:
1801 /* /sys/kernel/security/tomoyo/.domain_status */
1802 head
->write
= tomoyo_write_domain_profile
;
1803 head
->read
= tomoyo_read_domain_profile
;
1805 case TOMOYO_PROCESS_STATUS
:
1806 /* /sys/kernel/security/tomoyo/.process_status */
1807 head
->write
= tomoyo_write_pid
;
1808 head
->read
= tomoyo_read_pid
;
1810 case TOMOYO_VERSION
:
1811 /* /sys/kernel/security/tomoyo/version */
1812 head
->read
= tomoyo_read_version
;
1813 head
->readbuf_size
= 128;
1815 case TOMOYO_MEMINFO
:
1816 /* /sys/kernel/security/tomoyo/meminfo */
1817 head
->write
= tomoyo_write_memory_quota
;
1818 head
->read
= tomoyo_read_memory_counter
;
1819 head
->readbuf_size
= 512;
1821 case TOMOYO_PROFILE
:
1822 /* /sys/kernel/security/tomoyo/profile */
1823 head
->write
= tomoyo_write_profile
;
1824 head
->read
= tomoyo_read_profile
;
1826 case TOMOYO_QUERY
: /* /sys/kernel/security/tomoyo/query */
1827 head
->poll
= tomoyo_poll_query
;
1828 head
->write
= tomoyo_write_answer
;
1829 head
->read
= tomoyo_read_query
;
1831 case TOMOYO_MANAGER
:
1832 /* /sys/kernel/security/tomoyo/manager */
1833 head
->write
= tomoyo_write_manager_policy
;
1834 head
->read
= tomoyo_read_manager_policy
;
1837 if (!(file
->f_mode
& FMODE_READ
)) {
1839 * No need to allocate read_buf since it is not opened
1844 } else if (!head
->poll
) {
1845 /* Don't allocate read_buf for poll() access. */
1846 if (!head
->readbuf_size
)
1847 head
->readbuf_size
= 4096 * 2;
1848 head
->read_buf
= kzalloc(head
->readbuf_size
, GFP_NOFS
);
1849 if (!head
->read_buf
) {
1854 if (!(file
->f_mode
& FMODE_WRITE
)) {
1856 * No need to allocate write_buf since it is not opened
1860 } else if (head
->write
) {
1861 head
->writebuf_size
= 4096 * 2;
1862 head
->write_buf
= kzalloc(head
->writebuf_size
, GFP_NOFS
);
1863 if (!head
->write_buf
) {
1864 kfree(head
->read_buf
);
1869 if (type
!= TOMOYO_QUERY
)
1870 head
->reader_idx
= tomoyo_read_lock();
1871 file
->private_data
= head
;
1873 * Call the handler now if the file is
1874 * /sys/kernel/security/tomoyo/self_domain
1875 * so that the user can use
1876 * cat < /sys/kernel/security/tomoyo/self_domain"
1877 * to know the current process's domainname.
1879 if (type
== TOMOYO_SELFDOMAIN
)
1880 tomoyo_read_control(file
, NULL
, 0);
1882 * If the file is /sys/kernel/security/tomoyo/query , increment the
1884 * The obserber counter is used by tomoyo_supervisor() to see if
1885 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1887 else if (type
== TOMOYO_QUERY
)
1888 atomic_inc(&tomoyo_query_observers
);
1893 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1895 * @file: Pointer to "struct file".
1896 * @wait: Pointer to "poll_table".
1898 * Waits for read readiness.
1899 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1901 int tomoyo_poll_control(struct file
*file
, poll_table
*wait
)
1903 struct tomoyo_io_buffer
*head
= file
->private_data
;
1906 return head
->poll(file
, wait
);
1910 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1912 * @file: Pointer to "struct file".
1913 * @buffer: Poiner to buffer to write to.
1914 * @buffer_len: Size of @buffer.
1916 * Returns bytes read on success, negative value otherwise.
1918 * Caller holds tomoyo_read_lock().
1920 int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
1921 const int buffer_len
)
1924 struct tomoyo_io_buffer
*head
= file
->private_data
;
1929 if (mutex_lock_interruptible(&head
->io_sem
))
1931 /* Call the policy handler. */
1935 /* Write to buffer. */
1936 len
= head
->read_avail
;
1937 if (len
> buffer_len
)
1941 /* head->read_buf changes by some functions. */
1942 cp
= head
->read_buf
;
1943 if (copy_to_user(buffer
, cp
, len
)) {
1947 head
->read_avail
-= len
;
1948 memmove(cp
, cp
+ len
, head
->read_avail
);
1950 mutex_unlock(&head
->io_sem
);
1955 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1957 * @file: Pointer to "struct file".
1958 * @buffer: Pointer to buffer to read from.
1959 * @buffer_len: Size of @buffer.
1961 * Returns @buffer_len on success, negative value otherwise.
1963 * Caller holds tomoyo_read_lock().
1965 int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
1966 const int buffer_len
)
1968 struct tomoyo_io_buffer
*head
= file
->private_data
;
1969 int error
= buffer_len
;
1970 int avail_len
= buffer_len
;
1971 char *cp0
= head
->write_buf
;
1975 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
1977 /* Don't allow updating policies by non manager programs. */
1978 if (head
->write
!= tomoyo_write_pid
&&
1979 head
->write
!= tomoyo_write_domain_policy
&&
1980 !tomoyo_policy_manager())
1982 if (mutex_lock_interruptible(&head
->io_sem
))
1984 /* Read a line and dispatch it to the policy handler. */
1985 while (avail_len
> 0) {
1987 if (head
->write_avail
>= head
->writebuf_size
- 1) {
1990 } else if (get_user(c
, buffer
)) {
1996 cp0
[head
->write_avail
++] = c
;
1999 cp0
[head
->write_avail
- 1] = '\0';
2000 head
->write_avail
= 0;
2001 tomoyo_normalize_line(cp0
);
2004 mutex_unlock(&head
->io_sem
);
2009 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2011 * @file: Pointer to "struct file".
2013 * Releases memory and returns 0.
2015 * Caller looses tomoyo_read_lock().
2017 int tomoyo_close_control(struct file
*file
)
2019 struct tomoyo_io_buffer
*head
= file
->private_data
;
2020 const bool is_write
= !!head
->write_buf
;
2023 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2026 if (head
->type
== TOMOYO_QUERY
)
2027 atomic_dec(&tomoyo_query_observers
);
2029 tomoyo_read_unlock(head
->reader_idx
);
2030 /* Release memory used for policy I/O. */
2031 kfree(head
->read_buf
);
2032 head
->read_buf
= NULL
;
2033 kfree(head
->write_buf
);
2034 head
->write_buf
= NULL
;
2037 file
->private_data
= NULL
;
2044 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
2046 void tomoyo_check_profile(void)
2048 struct tomoyo_domain_info
*domain
;
2049 const int idx
= tomoyo_read_lock();
2050 tomoyo_policy_loaded
= true;
2051 /* Check all profiles currently assigned to domains are defined. */
2052 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
2053 const u8 profile
= domain
->profile
;
2054 if (tomoyo_profile_ptr
[profile
])
2056 panic("Profile %u (used by '%s') not defined.\n",
2057 profile
, domain
->domainname
->name
);
2059 tomoyo_read_unlock(idx
);
2060 if (tomoyo_profile_version
!= 20090903)
2061 panic("Profile version %u is not supported.\n",
2062 tomoyo_profile_version
);
2063 printk(KERN_INFO
"TOMOYO: 2.3.0-pre 2010/06/03\n");
2064 printk(KERN_INFO
"Mandatory Access Control activated.\n");