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>
12 #include <linux/hardirq.h>
15 /* String table for functionality that takes 4 modes. */
16 static const char *tomoyo_mode_4
[4] = {
17 "disabled", "learning", "permissive", "enforcing"
19 /* String table for functionality that takes 2 modes. */
20 static const char *tomoyo_mode_2
[4] = {
21 "disabled", "enabled", "enabled", "enabled"
25 * tomoyo_control_array is a static data which contains
27 * (1) functionality name used by /sys/kernel/security/tomoyo/profile .
28 * (2) initial values for "struct tomoyo_profile".
29 * (3) max values for "struct tomoyo_profile".
33 unsigned int current_value
;
34 const unsigned int max_value
;
35 } tomoyo_control_array
[TOMOYO_MAX_CONTROL_INDEX
] = {
36 [TOMOYO_MAC_FOR_FILE
] = { "MAC_FOR_FILE", 0, 3 },
37 [TOMOYO_MAX_ACCEPT_ENTRY
] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX
},
38 [TOMOYO_VERBOSE
] = { "TOMOYO_VERBOSE", 1, 1 },
42 * tomoyo_profile is a structure which is used for holding the mode of access
43 * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
44 * An administrator can define up to 256 profiles.
45 * The ->profile of "struct tomoyo_domain_info" is used for remembering
46 * the profile's number (0 - 255) assigned to that domain.
48 static struct tomoyo_profile
{
49 unsigned int value
[TOMOYO_MAX_CONTROL_INDEX
];
50 const struct tomoyo_path_info
*comment
;
51 } *tomoyo_profile_ptr
[TOMOYO_MAX_PROFILES
];
53 /* Permit policy management by non-root user? */
54 static bool tomoyo_manage_by_non_root
;
56 /* Utility functions. */
59 * tomoyo_print_name_union - Print a tomoyo_name_union.
61 * @head: Pointer to "struct tomoyo_io_buffer".
62 * @ptr: Pointer to "struct tomoyo_name_union".
64 * Returns true on success, false otherwise.
66 static bool tomoyo_print_name_union(struct tomoyo_io_buffer
*head
,
67 const struct tomoyo_name_union
*ptr
)
69 int pos
= head
->read_avail
;
70 if (pos
&& head
->read_buf
[pos
- 1] == ' ')
73 return tomoyo_io_printf(head
, " @%s",
74 ptr
->group
->group_name
->name
);
75 return tomoyo_io_printf(head
, " %s", ptr
->filename
->name
);
79 * tomoyo_print_number_union - Print a tomoyo_number_union.
81 * @head: Pointer to "struct tomoyo_io_buffer".
82 * @ptr: Pointer to "struct tomoyo_number_union".
84 * Returns true on success, false otherwise.
86 bool tomoyo_print_number_union(struct tomoyo_io_buffer
*head
,
87 const struct tomoyo_number_union
*ptr
)
93 if (!tomoyo_io_printf(head
, " "))
96 return tomoyo_io_printf(head
, "@%s",
97 ptr
->group
->group_name
->name
);
98 min_type
= ptr
->min_type
;
99 max_type
= ptr
->max_type
;
100 min
= ptr
->values
[0];
101 max
= ptr
->values
[1];
103 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
104 if (!tomoyo_io_printf(head
, "0x%lX", min
))
107 case TOMOYO_VALUE_TYPE_OCTAL
:
108 if (!tomoyo_io_printf(head
, "0%lo", min
))
112 if (!tomoyo_io_printf(head
, "%lu", min
))
116 if (min
== max
&& min_type
== max_type
)
119 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
120 return tomoyo_io_printf(head
, "-0x%lX", max
);
121 case TOMOYO_VALUE_TYPE_OCTAL
:
122 return tomoyo_io_printf(head
, "-0%lo", max
);
124 return tomoyo_io_printf(head
, "-%lu", max
);
129 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
131 * @head: Pointer to "struct tomoyo_io_buffer".
132 * @fmt: The printf()'s format string, followed by parameters.
134 * Returns true if output was written, false otherwise.
136 * The snprintf() will truncate, but tomoyo_io_printf() won't.
138 bool tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
142 int pos
= head
->read_avail
;
143 int size
= head
->readbuf_size
- pos
;
148 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
);
150 if (pos
+ len
>= head
->readbuf_size
)
152 head
->read_avail
+= len
;
157 * tomoyo_check_flags - Check mode for specified functionality.
159 * @domain: Pointer to "struct tomoyo_domain_info".
160 * @index: The functionality to check mode.
162 * TOMOYO checks only process context.
163 * This code disables TOMOYO's enforcement in case the function is called from
166 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info
*domain
,
169 const u8 profile
= domain
->profile
;
171 if (WARN_ON(in_interrupt()))
173 return tomoyo_policy_loaded
&& index
< TOMOYO_MAX_CONTROL_INDEX
174 #if TOMOYO_MAX_PROFILES != 256
175 && profile
< TOMOYO_MAX_PROFILES
177 && tomoyo_profile_ptr
[profile
] ?
178 tomoyo_profile_ptr
[profile
]->value
[index
] : 0;
182 * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
184 * @domain: Pointer to "struct tomoyo_domain_info".
186 * Returns true if domain policy violation warning should be printed to
189 bool tomoyo_verbose_mode(const struct tomoyo_domain_info
*domain
)
191 return tomoyo_check_flags(domain
, TOMOYO_VERBOSE
) != 0;
195 * tomoyo_find_or_assign_new_profile - Create a new profile.
197 * @profile: Profile number to create.
199 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
201 static struct tomoyo_profile
*tomoyo_find_or_assign_new_profile(const unsigned
204 struct tomoyo_profile
*ptr
= NULL
;
207 if (profile
>= TOMOYO_MAX_PROFILES
)
209 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
211 ptr
= tomoyo_profile_ptr
[profile
];
214 ptr
= kmalloc(sizeof(*ptr
), GFP_NOFS
);
215 if (!tomoyo_memory_ok(ptr
)) {
220 for (i
= 0; i
< TOMOYO_MAX_CONTROL_INDEX
; i
++)
221 ptr
->value
[i
] = tomoyo_control_array
[i
].current_value
;
222 mb(); /* Avoid out-of-order execution. */
223 tomoyo_profile_ptr
[profile
] = ptr
;
225 mutex_unlock(&tomoyo_policy_lock
);
230 * tomoyo_write_profile - Write to profile table.
232 * @head: Pointer to "struct tomoyo_io_buffer".
234 * Returns 0 on success, negative value otherwise.
236 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
238 char *data
= head
->write_buf
;
242 struct tomoyo_profile
*profile
;
245 cp
= strchr(data
, '-');
248 if (strict_strtoul(data
, 10, &num
))
252 profile
= tomoyo_find_or_assign_new_profile(num
);
255 cp
= strchr(data
, '=');
259 if (!strcmp(data
, "COMMENT")) {
260 const struct tomoyo_path_info
*old_comment
= profile
->comment
;
261 profile
->comment
= tomoyo_get_name(cp
+ 1);
262 tomoyo_put_name(old_comment
);
265 for (i
= 0; i
< TOMOYO_MAX_CONTROL_INDEX
; i
++) {
266 if (strcmp(data
, tomoyo_control_array
[i
].keyword
))
268 if (sscanf(cp
+ 1, "%u", &value
) != 1) {
273 modes
= tomoyo_mode_2
;
276 modes
= tomoyo_mode_4
;
279 for (j
= 0; j
< 4; j
++) {
280 if (strcmp(cp
+ 1, modes
[j
]))
287 } else if (value
> tomoyo_control_array
[i
].max_value
) {
288 value
= tomoyo_control_array
[i
].max_value
;
290 profile
->value
[i
] = value
;
297 * tomoyo_read_profile - Read from profile table.
299 * @head: Pointer to "struct tomoyo_io_buffer".
303 static int tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
305 static const int total
= TOMOYO_MAX_CONTROL_INDEX
+ 1;
310 for (step
= head
->read_step
; step
< TOMOYO_MAX_PROFILES
* total
;
312 const u8 index
= step
/ total
;
313 u8 type
= step
% total
;
314 const struct tomoyo_profile
*profile
315 = tomoyo_profile_ptr
[index
];
316 head
->read_step
= step
;
319 if (!type
) { /* Print profile' comment tag. */
320 if (!tomoyo_io_printf(head
, "%u-COMMENT=%s\n",
321 index
, profile
->comment
?
322 profile
->comment
->name
: ""))
327 if (type
< TOMOYO_MAX_CONTROL_INDEX
) {
328 const unsigned int value
= profile
->value
[type
];
329 const char **modes
= NULL
;
331 = tomoyo_control_array
[type
].keyword
;
332 switch (tomoyo_control_array
[type
].max_value
) {
334 modes
= tomoyo_mode_4
;
337 modes
= tomoyo_mode_2
;
341 if (!tomoyo_io_printf(head
, "%u-%s=%s\n", index
,
342 keyword
, modes
[value
]))
345 if (!tomoyo_io_printf(head
, "%u-%s=%u\n", index
,
351 if (step
== TOMOYO_MAX_PROFILES
* total
)
352 head
->read_eof
= true;
357 * tomoyo_policy_manager_list is used for holding list of domainnames or
358 * programs which are permitted to modify configuration via
359 * /sys/kernel/security/tomoyo/ interface.
361 * An entry is added by
363 * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
364 * /sys/kernel/security/tomoyo/manager
365 * (if you want to specify by a domainname)
369 * # echo '/usr/sbin/tomoyo-editpolicy' > /sys/kernel/security/tomoyo/manager
370 * (if you want to specify by a program's location)
374 * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
375 * /sys/kernel/security/tomoyo/manager
379 * # echo 'delete /usr/sbin/tomoyo-editpolicy' > \
380 * /sys/kernel/security/tomoyo/manager
382 * and all entries are retrieved by
384 * # cat /sys/kernel/security/tomoyo/manager
386 LIST_HEAD(tomoyo_policy_manager_list
);
389 * tomoyo_update_manager_entry - Add a manager entry.
391 * @manager: The path to manager or the domainnamme.
392 * @is_delete: True if it is a delete request.
394 * Returns 0 on success, negative value otherwise.
396 * Caller holds tomoyo_read_lock().
398 static int tomoyo_update_manager_entry(const char *manager
,
399 const bool is_delete
)
401 struct tomoyo_policy_manager_entry
*ptr
;
402 struct tomoyo_policy_manager_entry e
= { };
403 int error
= is_delete
? -ENOENT
: -ENOMEM
;
405 if (tomoyo_is_domain_def(manager
)) {
406 if (!tomoyo_is_correct_domain(manager
))
410 if (!tomoyo_is_correct_path(manager
, 1, -1, -1))
413 e
.manager
= tomoyo_get_name(manager
);
416 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
418 list_for_each_entry_rcu(ptr
, &tomoyo_policy_manager_list
, list
) {
419 if (ptr
->manager
!= e
.manager
)
421 ptr
->is_deleted
= is_delete
;
425 if (!is_delete
&& error
) {
426 struct tomoyo_policy_manager_entry
*entry
=
427 tomoyo_commit_ok(&e
, sizeof(e
));
429 list_add_tail_rcu(&entry
->list
,
430 &tomoyo_policy_manager_list
);
434 mutex_unlock(&tomoyo_policy_lock
);
436 tomoyo_put_name(e
.manager
);
441 * tomoyo_write_manager_policy - Write manager policy.
443 * @head: Pointer to "struct tomoyo_io_buffer".
445 * Returns 0 on success, negative value otherwise.
447 * Caller holds tomoyo_read_lock().
449 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer
*head
)
451 char *data
= head
->write_buf
;
452 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
454 if (!strcmp(data
, "manage_by_non_root")) {
455 tomoyo_manage_by_non_root
= !is_delete
;
458 return tomoyo_update_manager_entry(data
, is_delete
);
462 * tomoyo_read_manager_policy - Read manager policy.
464 * @head: Pointer to "struct tomoyo_io_buffer".
468 * Caller holds tomoyo_read_lock().
470 static int tomoyo_read_manager_policy(struct tomoyo_io_buffer
*head
)
472 struct list_head
*pos
;
477 list_for_each_cookie(pos
, head
->read_var2
,
478 &tomoyo_policy_manager_list
) {
479 struct tomoyo_policy_manager_entry
*ptr
;
480 ptr
= list_entry(pos
, struct tomoyo_policy_manager_entry
,
484 done
= tomoyo_io_printf(head
, "%s\n", ptr
->manager
->name
);
488 head
->read_eof
= done
;
493 * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
495 * Returns true if the current process is permitted to modify policy
496 * via /sys/kernel/security/tomoyo/ interface.
498 * Caller holds tomoyo_read_lock().
500 static bool tomoyo_is_policy_manager(void)
502 struct tomoyo_policy_manager_entry
*ptr
;
504 const struct task_struct
*task
= current
;
505 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
508 if (!tomoyo_policy_loaded
)
510 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
512 list_for_each_entry_rcu(ptr
, &tomoyo_policy_manager_list
, list
) {
513 if (!ptr
->is_deleted
&& ptr
->is_domain
514 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
521 exe
= tomoyo_get_exe();
524 list_for_each_entry_rcu(ptr
, &tomoyo_policy_manager_list
, list
) {
525 if (!ptr
->is_deleted
&& !ptr
->is_domain
526 && !strcmp(exe
, ptr
->manager
->name
)) {
531 if (!found
) { /* Reduce error messages. */
532 static pid_t last_pid
;
533 const pid_t pid
= current
->pid
;
534 if (last_pid
!= pid
) {
535 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
536 "update policies.\n", domainname
->name
, exe
);
545 * tomoyo_is_select_one - Parse select command.
547 * @head: Pointer to "struct tomoyo_io_buffer".
548 * @data: String to parse.
550 * Returns true on success, false otherwise.
552 * Caller holds tomoyo_read_lock().
554 static bool tomoyo_is_select_one(struct tomoyo_io_buffer
*head
,
558 struct tomoyo_domain_info
*domain
= NULL
;
559 bool global_pid
= false;
561 if (sscanf(data
, "pid=%u", &pid
) == 1 ||
562 (global_pid
= true, sscanf(data
, "global-pid=%u", &pid
) == 1)) {
563 struct task_struct
*p
;
565 read_lock(&tasklist_lock
);
567 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
569 p
= find_task_by_vpid(pid
);
571 domain
= tomoyo_real_domain(p
);
572 read_unlock(&tasklist_lock
);
574 } else if (!strncmp(data
, "domain=", 7)) {
575 if (tomoyo_is_domain_def(data
+ 7))
576 domain
= tomoyo_find_domain(data
+ 7);
579 head
->write_var1
= domain
;
580 /* Accessing read_buf is safe because head->io_sem is held. */
582 return true; /* Do nothing if open(O_WRONLY). */
583 head
->read_avail
= 0;
584 tomoyo_io_printf(head
, "# select %s\n", data
);
585 head
->read_single_domain
= true;
586 head
->read_eof
= !domain
;
588 struct tomoyo_domain_info
*d
;
589 head
->read_var1
= NULL
;
590 list_for_each_entry_rcu(d
, &tomoyo_domain_list
, list
) {
593 head
->read_var1
= &d
->list
;
595 head
->read_var2
= NULL
;
598 if (domain
->is_deleted
)
599 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
605 * tomoyo_delete_domain - Delete a domain.
607 * @domainname: The name of domain.
611 * Caller holds tomoyo_read_lock().
613 static int tomoyo_delete_domain(char *domainname
)
615 struct tomoyo_domain_info
*domain
;
616 struct tomoyo_path_info name
;
618 name
.name
= domainname
;
619 tomoyo_fill_path_info(&name
);
620 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
622 /* Is there an active domain? */
623 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
624 /* Never delete tomoyo_kernel_domain */
625 if (domain
== &tomoyo_kernel_domain
)
627 if (domain
->is_deleted
||
628 tomoyo_pathcmp(domain
->domainname
, &name
))
630 domain
->is_deleted
= true;
633 mutex_unlock(&tomoyo_policy_lock
);
638 * tomoyo_write_domain_policy2 - Write domain policy.
640 * @head: Pointer to "struct tomoyo_io_buffer".
642 * Returns 0 on success, negative value otherwise.
644 * Caller holds tomoyo_read_lock().
646 static int tomoyo_write_domain_policy2(char *data
,
647 struct tomoyo_domain_info
*domain
,
648 const bool is_delete
)
650 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_MOUNT
))
651 return tomoyo_write_mount_policy(data
, domain
, is_delete
);
652 return tomoyo_write_file_policy(data
, domain
, is_delete
);
656 * tomoyo_write_domain_policy - Write domain policy.
658 * @head: Pointer to "struct tomoyo_io_buffer".
660 * Returns 0 on success, negative value otherwise.
662 * Caller holds tomoyo_read_lock().
664 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer
*head
)
666 char *data
= head
->write_buf
;
667 struct tomoyo_domain_info
*domain
= head
->write_var1
;
668 bool is_delete
= false;
669 bool is_select
= false;
670 unsigned int profile
;
672 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
))
674 else if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_SELECT
))
676 if (is_select
&& tomoyo_is_select_one(head
, data
))
678 /* Don't allow updating policies by non manager programs. */
679 if (!tomoyo_is_policy_manager())
681 if (tomoyo_is_domain_def(data
)) {
684 tomoyo_delete_domain(data
);
686 domain
= tomoyo_find_domain(data
);
688 domain
= tomoyo_find_or_assign_new_domain(data
, 0);
689 head
->write_var1
= domain
;
695 if (sscanf(data
, TOMOYO_KEYWORD_USE_PROFILE
"%u", &profile
) == 1
696 && profile
< TOMOYO_MAX_PROFILES
) {
697 if (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
)
698 domain
->profile
= (u8
) profile
;
701 if (!strcmp(data
, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
)) {
702 domain
->ignore_global_allow_read
= !is_delete
;
705 if (!strcmp(data
, TOMOYO_KEYWORD_QUOTA_EXCEEDED
)) {
706 domain
->quota_warned
= !is_delete
;
709 if (!strcmp(data
, TOMOYO_KEYWORD_TRANSITION_FAILED
)) {
710 domain
->transition_failed
= !is_delete
;
713 return tomoyo_write_domain_policy2(data
, domain
, is_delete
);
717 * tomoyo_print_path_acl - Print a single path ACL entry.
719 * @head: Pointer to "struct tomoyo_io_buffer".
720 * @ptr: Pointer to "struct tomoyo_path_acl".
722 * Returns true on success, false otherwise.
724 static bool tomoyo_print_path_acl(struct tomoyo_io_buffer
*head
,
725 struct tomoyo_path_acl
*ptr
)
729 const u16 perm
= ptr
->perm
;
731 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH_OPERATION
; bit
++) {
732 if (!(perm
& (1 << bit
)))
734 /* Print "read/write" instead of "read" and "write". */
735 if ((bit
== TOMOYO_TYPE_READ
|| bit
== TOMOYO_TYPE_WRITE
)
736 && (perm
& (1 << TOMOYO_TYPE_READ_WRITE
)))
738 pos
= head
->read_avail
;
739 if (!tomoyo_io_printf(head
, "allow_%s ",
740 tomoyo_path2keyword(bit
)) ||
741 !tomoyo_print_name_union(head
, &ptr
->name
) ||
742 !tomoyo_io_printf(head
, "\n"))
748 head
->read_bit
= bit
;
749 head
->read_avail
= pos
;
754 * tomoyo_print_path2_acl - Print a double path ACL entry.
756 * @head: Pointer to "struct tomoyo_io_buffer".
757 * @ptr: Pointer to "struct tomoyo_path2_acl".
759 * Returns true on success, false otherwise.
761 static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer
*head
,
762 struct tomoyo_path2_acl
*ptr
)
765 const u8 perm
= ptr
->perm
;
768 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH2_OPERATION
; bit
++) {
769 if (!(perm
& (1 << bit
)))
771 pos
= head
->read_avail
;
772 if (!tomoyo_io_printf(head
, "allow_%s ",
773 tomoyo_path22keyword(bit
)) ||
774 !tomoyo_print_name_union(head
, &ptr
->name1
) ||
775 !tomoyo_print_name_union(head
, &ptr
->name2
) ||
776 !tomoyo_io_printf(head
, "\n"))
782 head
->read_bit
= bit
;
783 head
->read_avail
= pos
;
788 * tomoyo_print_path_number_acl - Print a path_number ACL entry.
790 * @head: Pointer to "struct tomoyo_io_buffer".
791 * @ptr: Pointer to "struct tomoyo_path_number_acl".
793 * Returns true on success, false otherwise.
795 static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer
*head
,
796 struct tomoyo_path_number_acl
*ptr
)
800 const u8 perm
= ptr
->perm
;
801 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH_NUMBER_OPERATION
;
803 if (!(perm
& (1 << bit
)))
805 pos
= head
->read_avail
;
806 if (!tomoyo_io_printf(head
, "allow_%s",
807 tomoyo_path_number2keyword(bit
)) ||
808 !tomoyo_print_name_union(head
, &ptr
->name
) ||
809 !tomoyo_print_number_union(head
, &ptr
->number
) ||
810 !tomoyo_io_printf(head
, "\n"))
816 head
->read_bit
= bit
;
817 head
->read_avail
= pos
;
822 * tomoyo_print_path_number3_acl - Print a path_number3 ACL entry.
824 * @head: Pointer to "struct tomoyo_io_buffer".
825 * @ptr: Pointer to "struct tomoyo_path_number3_acl".
827 * Returns true on success, false otherwise.
829 static bool tomoyo_print_path_number3_acl(struct tomoyo_io_buffer
*head
,
830 struct tomoyo_path_number3_acl
*ptr
)
834 const u16 perm
= ptr
->perm
;
835 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_PATH_NUMBER3_OPERATION
;
837 if (!(perm
& (1 << bit
)))
839 pos
= head
->read_avail
;
840 if (!tomoyo_io_printf(head
, "allow_%s",
841 tomoyo_path_number32keyword(bit
)) ||
842 !tomoyo_print_name_union(head
, &ptr
->name
) ||
843 !tomoyo_print_number_union(head
, &ptr
->mode
) ||
844 !tomoyo_print_number_union(head
, &ptr
->major
) ||
845 !tomoyo_print_number_union(head
, &ptr
->minor
) ||
846 !tomoyo_io_printf(head
, "\n"))
852 head
->read_bit
= bit
;
853 head
->read_avail
= pos
;
858 * tomoyo_print_mount_acl - Print a mount ACL entry.
860 * @head: Pointer to "struct tomoyo_io_buffer".
861 * @ptr: Pointer to "struct tomoyo_mount_acl".
863 * Returns true on success, false otherwise.
865 static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer
*head
,
866 struct tomoyo_mount_acl
*ptr
)
868 const int pos
= head
->read_avail
;
871 if (!tomoyo_io_printf(head
, TOMOYO_KEYWORD_ALLOW_MOUNT
) ||
872 !tomoyo_print_name_union(head
, &ptr
->dev_name
) ||
873 !tomoyo_print_name_union(head
, &ptr
->dir_name
) ||
874 !tomoyo_print_name_union(head
, &ptr
->fs_type
) ||
875 !tomoyo_print_number_union(head
, &ptr
->flags
) ||
876 !tomoyo_io_printf(head
, "\n")) {
877 head
->read_avail
= pos
;
884 * tomoyo_print_entry - Print an ACL entry.
886 * @head: Pointer to "struct tomoyo_io_buffer".
887 * @ptr: Pointer to an ACL entry.
889 * Returns true on success, false otherwise.
891 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
892 struct tomoyo_acl_info
*ptr
)
894 const u8 acl_type
= ptr
->type
;
896 if (acl_type
== TOMOYO_TYPE_PATH_ACL
) {
897 struct tomoyo_path_acl
*acl
898 = container_of(ptr
, struct tomoyo_path_acl
, head
);
899 return tomoyo_print_path_acl(head
, acl
);
901 if (acl_type
== TOMOYO_TYPE_PATH2_ACL
) {
902 struct tomoyo_path2_acl
*acl
903 = container_of(ptr
, struct tomoyo_path2_acl
, head
);
904 return tomoyo_print_path2_acl(head
, acl
);
906 if (acl_type
== TOMOYO_TYPE_PATH_NUMBER_ACL
) {
907 struct tomoyo_path_number_acl
*acl
908 = container_of(ptr
, struct tomoyo_path_number_acl
,
910 return tomoyo_print_path_number_acl(head
, acl
);
912 if (acl_type
== TOMOYO_TYPE_PATH_NUMBER3_ACL
) {
913 struct tomoyo_path_number3_acl
*acl
914 = container_of(ptr
, struct tomoyo_path_number3_acl
,
916 return tomoyo_print_path_number3_acl(head
, acl
);
918 if (acl_type
== TOMOYO_TYPE_MOUNT_ACL
) {
919 struct tomoyo_mount_acl
*acl
920 = container_of(ptr
, struct tomoyo_mount_acl
, head
);
921 return tomoyo_print_mount_acl(head
, acl
);
923 BUG(); /* This must not happen. */
928 * tomoyo_read_domain_policy - Read domain policy.
930 * @head: Pointer to "struct tomoyo_io_buffer".
934 * Caller holds tomoyo_read_lock().
936 static int tomoyo_read_domain_policy(struct tomoyo_io_buffer
*head
)
938 struct list_head
*dpos
;
939 struct list_head
*apos
;
944 if (head
->read_step
== 0)
946 list_for_each_cookie(dpos
, head
->read_var1
, &tomoyo_domain_list
) {
947 struct tomoyo_domain_info
*domain
;
948 const char *quota_exceeded
= "";
949 const char *transition_failed
= "";
950 const char *ignore_global_allow_read
= "";
951 domain
= list_entry(dpos
, struct tomoyo_domain_info
, list
);
952 if (head
->read_step
!= 1)
954 if (domain
->is_deleted
&& !head
->read_single_domain
)
956 /* Print domainname and flags. */
957 if (domain
->quota_warned
)
958 quota_exceeded
= "quota_exceeded\n";
959 if (domain
->transition_failed
)
960 transition_failed
= "transition_failed\n";
961 if (domain
->ignore_global_allow_read
)
962 ignore_global_allow_read
963 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
"\n";
964 done
= tomoyo_io_printf(head
, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
966 domain
->domainname
->name
,
967 domain
->profile
, quota_exceeded
,
969 ignore_global_allow_read
);
974 if (head
->read_step
== 3)
976 /* Print ACL entries in the domain. */
977 list_for_each_cookie(apos
, head
->read_var2
,
978 &domain
->acl_info_list
) {
979 struct tomoyo_acl_info
*ptr
980 = list_entry(apos
, struct tomoyo_acl_info
,
982 done
= tomoyo_print_entry(head
, ptr
);
990 done
= tomoyo_io_printf(head
, "\n");
994 if (head
->read_single_domain
)
997 head
->read_eof
= done
;
1002 * tomoyo_write_domain_profile - Assign profile for specified domain.
1004 * @head: Pointer to "struct tomoyo_io_buffer".
1006 * Returns 0 on success, -EINVAL otherwise.
1008 * This is equivalent to doing
1010 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1011 * /usr/sbin/tomoyo-loadpolicy -d
1013 * Caller holds tomoyo_read_lock().
1015 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer
*head
)
1017 char *data
= head
->write_buf
;
1018 char *cp
= strchr(data
, ' ');
1019 struct tomoyo_domain_info
*domain
;
1020 unsigned long profile
;
1025 domain
= tomoyo_find_domain(cp
+ 1);
1026 if (strict_strtoul(data
, 10, &profile
))
1028 if (domain
&& profile
< TOMOYO_MAX_PROFILES
1029 && (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
))
1030 domain
->profile
= (u8
) profile
;
1035 * tomoyo_read_domain_profile - Read only domainname and profile.
1037 * @head: Pointer to "struct tomoyo_io_buffer".
1039 * Returns list of profile number and domainname pairs.
1041 * This is equivalent to doing
1043 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1044 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1045 * domainname = $0; } else if ( $1 == "use_profile" ) {
1046 * print $2 " " domainname; domainname = ""; } } ; '
1048 * Caller holds tomoyo_read_lock().
1050 static int tomoyo_read_domain_profile(struct tomoyo_io_buffer
*head
)
1052 struct list_head
*pos
;
1057 list_for_each_cookie(pos
, head
->read_var1
, &tomoyo_domain_list
) {
1058 struct tomoyo_domain_info
*domain
;
1059 domain
= list_entry(pos
, struct tomoyo_domain_info
, list
);
1060 if (domain
->is_deleted
)
1062 done
= tomoyo_io_printf(head
, "%u %s\n", domain
->profile
,
1063 domain
->domainname
->name
);
1067 head
->read_eof
= done
;
1072 * tomoyo_write_pid: Specify PID to obtain domainname.
1074 * @head: Pointer to "struct tomoyo_io_buffer".
1078 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1081 /* No error check. */
1082 strict_strtoul(head
->write_buf
, 10, &pid
);
1083 head
->read_step
= (int) pid
;
1084 head
->read_eof
= false;
1089 * tomoyo_read_pid - Get domainname of the specified PID.
1091 * @head: Pointer to "struct tomoyo_io_buffer".
1093 * Returns the domainname which the specified PID is in on success,
1094 * empty string otherwise.
1095 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1096 * using read()/write() interface rather than sysctl() interface.
1098 static int tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1100 if (head
->read_avail
== 0 && !head
->read_eof
) {
1101 const int pid
= head
->read_step
;
1102 struct task_struct
*p
;
1103 struct tomoyo_domain_info
*domain
= NULL
;
1105 read_lock(&tasklist_lock
);
1106 p
= find_task_by_vpid(pid
);
1108 domain
= tomoyo_real_domain(p
);
1109 read_unlock(&tasklist_lock
);
1112 tomoyo_io_printf(head
, "%d %u %s", pid
, domain
->profile
,
1113 domain
->domainname
->name
);
1114 head
->read_eof
= true;
1120 * tomoyo_write_exception_policy - Write exception policy.
1122 * @head: Pointer to "struct tomoyo_io_buffer".
1124 * Returns 0 on success, negative value otherwise.
1126 * Caller holds tomoyo_read_lock().
1128 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer
*head
)
1130 char *data
= head
->write_buf
;
1131 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1133 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_KEEP_DOMAIN
))
1134 return tomoyo_write_domain_keeper_policy(data
, false,
1136 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_KEEP_DOMAIN
))
1137 return tomoyo_write_domain_keeper_policy(data
, true, is_delete
);
1138 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_INITIALIZE_DOMAIN
))
1139 return tomoyo_write_domain_initializer_policy(data
, false,
1141 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN
))
1142 return tomoyo_write_domain_initializer_policy(data
, true,
1144 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALIAS
))
1145 return tomoyo_write_alias_policy(data
, is_delete
);
1146 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_READ
))
1147 return tomoyo_write_globally_readable_policy(data
, is_delete
);
1148 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_FILE_PATTERN
))
1149 return tomoyo_write_pattern_policy(data
, is_delete
);
1150 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DENY_REWRITE
))
1151 return tomoyo_write_no_rewrite_policy(data
, is_delete
);
1152 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_PATH_GROUP
))
1153 return tomoyo_write_path_group_policy(data
, is_delete
);
1154 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NUMBER_GROUP
))
1155 return tomoyo_write_number_group_policy(data
, is_delete
);
1160 * tomoyo_read_exception_policy - Read exception policy.
1162 * @head: Pointer to "struct tomoyo_io_buffer".
1164 * Returns 0 on success, -EINVAL otherwise.
1166 * Caller holds tomoyo_read_lock().
1168 static int tomoyo_read_exception_policy(struct tomoyo_io_buffer
*head
)
1170 if (!head
->read_eof
) {
1171 switch (head
->read_step
) {
1173 head
->read_var2
= NULL
;
1174 head
->read_step
= 1;
1176 if (!tomoyo_read_domain_keeper_policy(head
))
1178 head
->read_var2
= NULL
;
1179 head
->read_step
= 2;
1181 if (!tomoyo_read_globally_readable_policy(head
))
1183 head
->read_var2
= NULL
;
1184 head
->read_step
= 3;
1186 head
->read_var2
= NULL
;
1187 head
->read_step
= 4;
1189 if (!tomoyo_read_domain_initializer_policy(head
))
1191 head
->read_var2
= NULL
;
1192 head
->read_step
= 5;
1194 if (!tomoyo_read_alias_policy(head
))
1196 head
->read_var2
= NULL
;
1197 head
->read_step
= 6;
1199 head
->read_var2
= NULL
;
1200 head
->read_step
= 7;
1202 if (!tomoyo_read_file_pattern(head
))
1204 head
->read_var2
= NULL
;
1205 head
->read_step
= 8;
1207 if (!tomoyo_read_no_rewrite_policy(head
))
1209 head
->read_var2
= NULL
;
1210 head
->read_step
= 9;
1212 if (!tomoyo_read_path_group_policy(head
))
1214 head
->read_var1
= NULL
;
1215 head
->read_var2
= NULL
;
1216 head
->read_step
= 10;
1218 if (!tomoyo_read_number_group_policy(head
))
1220 head
->read_var1
= NULL
;
1221 head
->read_var2
= NULL
;
1222 head
->read_step
= 11;
1224 head
->read_eof
= true;
1234 * tomoyo_print_header - Get header line of audit log.
1236 * @r: Pointer to "struct tomoyo_request_info".
1238 * Returns string representation.
1240 * This function uses kmalloc(), so caller must kfree() if this function
1241 * didn't return NULL.
1243 static char *tomoyo_print_header(struct tomoyo_request_info
*r
)
1245 static const char *tomoyo_mode_4
[4] = {
1246 "disabled", "learning", "permissive", "enforcing"
1249 const pid_t gpid
= task_pid_nr(current
);
1250 static const int tomoyo_buffer_len
= 4096;
1251 char *buffer
= kmalloc(tomoyo_buffer_len
, GFP_NOFS
);
1254 do_gettimeofday(&tv
);
1255 snprintf(buffer
, tomoyo_buffer_len
- 1,
1256 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1257 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1258 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1259 tv
.tv_sec
, r
->profile
, tomoyo_mode_4
[r
->mode
], gpid
,
1260 (pid_t
) sys_getpid(), (pid_t
) sys_getppid(),
1261 current_uid(), current_gid(), current_euid(),
1262 current_egid(), current_suid(), current_sgid(),
1263 current_fsuid(), current_fsgid());
1268 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1270 * @len: Required size.
1271 * @r: Pointer to "struct tomoyo_request_info".
1273 * Returns pointer to allocated memory.
1275 * The @len is updated to add the header lines' size on success.
1277 * This function uses kzalloc(), so caller must kfree() if this function
1278 * didn't return NULL.
1280 static char *tomoyo_init_audit_log(int *len
, struct tomoyo_request_info
*r
)
1284 const char *domainname
;
1286 r
->domain
= tomoyo_domain();
1287 domainname
= r
->domain
->domainname
->name
;
1288 header
= tomoyo_print_header(r
);
1291 *len
+= strlen(domainname
) + strlen(header
) + 10;
1292 buf
= kzalloc(*len
, GFP_NOFS
);
1294 snprintf(buf
, (*len
) - 1, "%s\n%s\n", header
, domainname
);
1299 /* Wait queue for tomoyo_query_list. */
1300 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait
);
1302 /* Lock for manipulating tomoyo_query_list. */
1303 static DEFINE_SPINLOCK(tomoyo_query_list_lock
);
1305 /* Structure for query. */
1306 struct tomoyo_query_entry
{
1307 struct list_head list
;
1310 unsigned int serial
;
1315 /* The list for "struct tomoyo_query_entry". */
1316 static LIST_HEAD(tomoyo_query_list
);
1319 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1322 static atomic_t tomoyo_query_observers
= ATOMIC_INIT(0);
1325 * tomoyo_supervisor - Ask for the supervisor's decision.
1327 * @r: Pointer to "struct tomoyo_request_info".
1328 * @fmt: The printf()'s format string, followed by parameters.
1330 * Returns 0 if the supervisor decided to permit the access request which
1331 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1332 * supervisor decided to retry the access request which violated the policy in
1333 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1335 int tomoyo_supervisor(struct tomoyo_request_info
*r
, const char *fmt
, ...)
1341 static unsigned int tomoyo_serial
;
1342 struct tomoyo_query_entry
*tomoyo_query_entry
= NULL
;
1343 bool quota_exceeded
= false;
1347 case TOMOYO_CONFIG_LEARNING
:
1348 if (!tomoyo_domain_quota_is_ok(r
))
1350 va_start(args
, fmt
);
1351 len
= vsnprintf((char *) &pos
, sizeof(pos
) - 1, fmt
, args
) + 4;
1353 buffer
= kmalloc(len
, GFP_NOFS
);
1356 va_start(args
, fmt
);
1357 vsnprintf(buffer
, len
- 1, fmt
, args
);
1359 tomoyo_normalize_line(buffer
);
1360 tomoyo_write_domain_policy2(buffer
, r
->domain
, false);
1363 case TOMOYO_CONFIG_PERMISSIVE
:
1367 r
->domain
= tomoyo_domain();
1368 if (!atomic_read(&tomoyo_query_observers
))
1370 va_start(args
, fmt
);
1371 len
= vsnprintf((char *) &pos
, sizeof(pos
) - 1, fmt
, args
) + 32;
1373 header
= tomoyo_init_audit_log(&len
, r
);
1376 tomoyo_query_entry
= kzalloc(sizeof(*tomoyo_query_entry
), GFP_NOFS
);
1377 if (!tomoyo_query_entry
)
1379 tomoyo_query_entry
->query
= kzalloc(len
, GFP_NOFS
);
1380 if (!tomoyo_query_entry
->query
)
1382 len
= ksize(tomoyo_query_entry
->query
);
1383 INIT_LIST_HEAD(&tomoyo_query_entry
->list
);
1384 spin_lock(&tomoyo_query_list_lock
);
1385 if (tomoyo_quota_for_query
&& tomoyo_query_memory_size
+ len
+
1386 sizeof(*tomoyo_query_entry
) >= tomoyo_quota_for_query
) {
1387 quota_exceeded
= true;
1389 tomoyo_query_memory_size
+= len
+ sizeof(*tomoyo_query_entry
);
1390 tomoyo_query_entry
->serial
= tomoyo_serial
++;
1392 spin_unlock(&tomoyo_query_list_lock
);
1395 pos
= snprintf(tomoyo_query_entry
->query
, len
- 1, "Q%u-%hu\n%s",
1396 tomoyo_query_entry
->serial
, r
->retry
, header
);
1399 va_start(args
, fmt
);
1400 vsnprintf(tomoyo_query_entry
->query
+ pos
, len
- 1 - pos
, fmt
, args
);
1401 tomoyo_query_entry
->query_len
= strlen(tomoyo_query_entry
->query
) + 1;
1403 spin_lock(&tomoyo_query_list_lock
);
1404 list_add_tail(&tomoyo_query_entry
->list
, &tomoyo_query_list
);
1405 spin_unlock(&tomoyo_query_list_lock
);
1406 /* Give 10 seconds for supervisor's opinion. */
1407 for (tomoyo_query_entry
->timer
= 0;
1408 atomic_read(&tomoyo_query_observers
) && tomoyo_query_entry
->timer
< 100;
1409 tomoyo_query_entry
->timer
++) {
1410 wake_up(&tomoyo_query_wait
);
1411 set_current_state(TASK_INTERRUPTIBLE
);
1412 schedule_timeout(HZ
/ 10);
1413 if (tomoyo_query_entry
->answer
)
1416 spin_lock(&tomoyo_query_list_lock
);
1417 list_del(&tomoyo_query_entry
->list
);
1418 tomoyo_query_memory_size
-= len
+ sizeof(*tomoyo_query_entry
);
1419 spin_unlock(&tomoyo_query_list_lock
);
1420 switch (tomoyo_query_entry
->answer
) {
1421 case 3: /* Asked to retry by administrator. */
1422 error
= TOMOYO_RETRY_REQUEST
;
1426 /* Granted by administrator. */
1433 /* Rejected by administrator. */
1437 if (tomoyo_query_entry
)
1438 kfree(tomoyo_query_entry
->query
);
1439 kfree(tomoyo_query_entry
);
1445 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1447 * @file: Pointer to "struct file".
1448 * @wait: Pointer to "poll_table".
1450 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1452 * Waits for access requests which violated policy in enforcing mode.
1454 static int tomoyo_poll_query(struct file
*file
, poll_table
*wait
)
1456 struct list_head
*tmp
;
1459 for (i
= 0; i
< 2; i
++) {
1460 spin_lock(&tomoyo_query_list_lock
);
1461 list_for_each(tmp
, &tomoyo_query_list
) {
1462 struct tomoyo_query_entry
*ptr
1463 = list_entry(tmp
, struct tomoyo_query_entry
,
1470 spin_unlock(&tomoyo_query_list_lock
);
1472 return POLLIN
| POLLRDNORM
;
1475 poll_wait(file
, &tomoyo_query_wait
, wait
);
1481 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1483 * @head: Pointer to "struct tomoyo_io_buffer".
1487 static int tomoyo_read_query(struct tomoyo_io_buffer
*head
)
1489 struct list_head
*tmp
;
1493 if (head
->read_avail
)
1495 if (head
->read_buf
) {
1496 kfree(head
->read_buf
);
1497 head
->read_buf
= NULL
;
1498 head
->readbuf_size
= 0;
1500 spin_lock(&tomoyo_query_list_lock
);
1501 list_for_each(tmp
, &tomoyo_query_list
) {
1502 struct tomoyo_query_entry
*ptr
1503 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1506 if (pos
++ != head
->read_step
)
1508 len
= ptr
->query_len
;
1511 spin_unlock(&tomoyo_query_list_lock
);
1513 head
->read_step
= 0;
1516 buf
= kzalloc(len
, GFP_NOFS
);
1520 spin_lock(&tomoyo_query_list_lock
);
1521 list_for_each(tmp
, &tomoyo_query_list
) {
1522 struct tomoyo_query_entry
*ptr
1523 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1526 if (pos
++ != head
->read_step
)
1529 * Some query can be skipped because tomoyo_query_list
1530 * can change, but I don't care.
1532 if (len
== ptr
->query_len
)
1533 memmove(buf
, ptr
->query
, len
);
1536 spin_unlock(&tomoyo_query_list_lock
);
1538 head
->read_avail
= len
;
1539 head
->readbuf_size
= head
->read_avail
;
1540 head
->read_buf
= buf
;
1549 * tomoyo_write_answer - Write the supervisor's decision.
1551 * @head: Pointer to "struct tomoyo_io_buffer".
1553 * Returns 0 on success, -EINVAL otherwise.
1555 static int tomoyo_write_answer(struct tomoyo_io_buffer
*head
)
1557 char *data
= head
->write_buf
;
1558 struct list_head
*tmp
;
1559 unsigned int serial
;
1560 unsigned int answer
;
1561 spin_lock(&tomoyo_query_list_lock
);
1562 list_for_each(tmp
, &tomoyo_query_list
) {
1563 struct tomoyo_query_entry
*ptr
1564 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1567 spin_unlock(&tomoyo_query_list_lock
);
1568 if (sscanf(data
, "A%u=%u", &serial
, &answer
) != 2)
1570 spin_lock(&tomoyo_query_list_lock
);
1571 list_for_each(tmp
, &tomoyo_query_list
) {
1572 struct tomoyo_query_entry
*ptr
1573 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1574 if (ptr
->serial
!= serial
)
1577 ptr
->answer
= answer
;
1580 spin_unlock(&tomoyo_query_list_lock
);
1585 * tomoyo_read_version: Get version.
1587 * @head: Pointer to "struct tomoyo_io_buffer".
1589 * Returns version information.
1591 static int tomoyo_read_version(struct tomoyo_io_buffer
*head
)
1593 if (!head
->read_eof
) {
1594 tomoyo_io_printf(head
, "2.2.0");
1595 head
->read_eof
= true;
1601 * tomoyo_read_self_domain - Get the current process's domainname.
1603 * @head: Pointer to "struct tomoyo_io_buffer".
1605 * Returns the current process's domainname.
1607 static int tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
1609 if (!head
->read_eof
) {
1611 * tomoyo_domain()->domainname != NULL
1612 * because every process belongs to a domain and
1613 * the domain's name cannot be NULL.
1615 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
1616 head
->read_eof
= true;
1622 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1624 * @type: Type of interface.
1625 * @file: Pointer to "struct file".
1627 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1629 * Caller acquires tomoyo_read_lock().
1631 int tomoyo_open_control(const u8 type
, struct file
*file
)
1633 struct tomoyo_io_buffer
*head
= kzalloc(sizeof(*head
), GFP_NOFS
);
1637 mutex_init(&head
->io_sem
);
1640 case TOMOYO_DOMAINPOLICY
:
1641 /* /sys/kernel/security/tomoyo/domain_policy */
1642 head
->write
= tomoyo_write_domain_policy
;
1643 head
->read
= tomoyo_read_domain_policy
;
1645 case TOMOYO_EXCEPTIONPOLICY
:
1646 /* /sys/kernel/security/tomoyo/exception_policy */
1647 head
->write
= tomoyo_write_exception_policy
;
1648 head
->read
= tomoyo_read_exception_policy
;
1650 case TOMOYO_SELFDOMAIN
:
1651 /* /sys/kernel/security/tomoyo/self_domain */
1652 head
->read
= tomoyo_read_self_domain
;
1654 case TOMOYO_DOMAIN_STATUS
:
1655 /* /sys/kernel/security/tomoyo/.domain_status */
1656 head
->write
= tomoyo_write_domain_profile
;
1657 head
->read
= tomoyo_read_domain_profile
;
1659 case TOMOYO_PROCESS_STATUS
:
1660 /* /sys/kernel/security/tomoyo/.process_status */
1661 head
->write
= tomoyo_write_pid
;
1662 head
->read
= tomoyo_read_pid
;
1664 case TOMOYO_VERSION
:
1665 /* /sys/kernel/security/tomoyo/version */
1666 head
->read
= tomoyo_read_version
;
1667 head
->readbuf_size
= 128;
1669 case TOMOYO_MEMINFO
:
1670 /* /sys/kernel/security/tomoyo/meminfo */
1671 head
->write
= tomoyo_write_memory_quota
;
1672 head
->read
= tomoyo_read_memory_counter
;
1673 head
->readbuf_size
= 512;
1675 case TOMOYO_PROFILE
:
1676 /* /sys/kernel/security/tomoyo/profile */
1677 head
->write
= tomoyo_write_profile
;
1678 head
->read
= tomoyo_read_profile
;
1680 case TOMOYO_QUERY
: /* /sys/kernel/security/tomoyo/query */
1681 head
->poll
= tomoyo_poll_query
;
1682 head
->write
= tomoyo_write_answer
;
1683 head
->read
= tomoyo_read_query
;
1685 case TOMOYO_MANAGER
:
1686 /* /sys/kernel/security/tomoyo/manager */
1687 head
->write
= tomoyo_write_manager_policy
;
1688 head
->read
= tomoyo_read_manager_policy
;
1691 if (!(file
->f_mode
& FMODE_READ
)) {
1693 * No need to allocate read_buf since it is not opened
1698 } else if (!head
->poll
) {
1699 /* Don't allocate read_buf for poll() access. */
1700 if (!head
->readbuf_size
)
1701 head
->readbuf_size
= 4096 * 2;
1702 head
->read_buf
= kzalloc(head
->readbuf_size
, GFP_NOFS
);
1703 if (!head
->read_buf
) {
1708 if (!(file
->f_mode
& FMODE_WRITE
)) {
1710 * No need to allocate write_buf since it is not opened
1714 } else if (head
->write
) {
1715 head
->writebuf_size
= 4096 * 2;
1716 head
->write_buf
= kzalloc(head
->writebuf_size
, GFP_NOFS
);
1717 if (!head
->write_buf
) {
1718 kfree(head
->read_buf
);
1723 if (type
!= TOMOYO_QUERY
)
1724 head
->reader_idx
= tomoyo_read_lock();
1725 file
->private_data
= head
;
1727 * Call the handler now if the file is
1728 * /sys/kernel/security/tomoyo/self_domain
1729 * so that the user can use
1730 * cat < /sys/kernel/security/tomoyo/self_domain"
1731 * to know the current process's domainname.
1733 if (type
== TOMOYO_SELFDOMAIN
)
1734 tomoyo_read_control(file
, NULL
, 0);
1736 * If the file is /sys/kernel/security/tomoyo/query , increment the
1738 * The obserber counter is used by tomoyo_supervisor() to see if
1739 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1741 else if (type
== TOMOYO_QUERY
)
1742 atomic_inc(&tomoyo_query_observers
);
1747 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1749 * @file: Pointer to "struct file".
1750 * @wait: Pointer to "poll_table".
1752 * Waits for read readiness.
1753 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1755 int tomoyo_poll_control(struct file
*file
, poll_table
*wait
)
1757 struct tomoyo_io_buffer
*head
= file
->private_data
;
1760 return head
->poll(file
, wait
);
1764 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1766 * @file: Pointer to "struct file".
1767 * @buffer: Poiner to buffer to write to.
1768 * @buffer_len: Size of @buffer.
1770 * Returns bytes read on success, negative value otherwise.
1772 * Caller holds tomoyo_read_lock().
1774 int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
1775 const int buffer_len
)
1778 struct tomoyo_io_buffer
*head
= file
->private_data
;
1783 if (mutex_lock_interruptible(&head
->io_sem
))
1785 /* Call the policy handler. */
1786 len
= head
->read(head
);
1789 /* Write to buffer. */
1790 len
= head
->read_avail
;
1791 if (len
> buffer_len
)
1795 /* head->read_buf changes by some functions. */
1796 cp
= head
->read_buf
;
1797 if (copy_to_user(buffer
, cp
, len
)) {
1801 head
->read_avail
-= len
;
1802 memmove(cp
, cp
+ len
, head
->read_avail
);
1804 mutex_unlock(&head
->io_sem
);
1809 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1811 * @file: Pointer to "struct file".
1812 * @buffer: Pointer to buffer to read from.
1813 * @buffer_len: Size of @buffer.
1815 * Returns @buffer_len on success, negative value otherwise.
1817 * Caller holds tomoyo_read_lock().
1819 int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
1820 const int buffer_len
)
1822 struct tomoyo_io_buffer
*head
= file
->private_data
;
1823 int error
= buffer_len
;
1824 int avail_len
= buffer_len
;
1825 char *cp0
= head
->write_buf
;
1829 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
1831 /* Don't allow updating policies by non manager programs. */
1832 if (head
->write
!= tomoyo_write_pid
&&
1833 head
->write
!= tomoyo_write_domain_policy
&&
1834 !tomoyo_is_policy_manager())
1836 if (mutex_lock_interruptible(&head
->io_sem
))
1838 /* Read a line and dispatch it to the policy handler. */
1839 while (avail_len
> 0) {
1841 if (head
->write_avail
>= head
->writebuf_size
- 1) {
1844 } else if (get_user(c
, buffer
)) {
1850 cp0
[head
->write_avail
++] = c
;
1853 cp0
[head
->write_avail
- 1] = '\0';
1854 head
->write_avail
= 0;
1855 tomoyo_normalize_line(cp0
);
1858 mutex_unlock(&head
->io_sem
);
1863 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1865 * @file: Pointer to "struct file".
1867 * Releases memory and returns 0.
1869 * Caller looses tomoyo_read_lock().
1871 int tomoyo_close_control(struct file
*file
)
1873 struct tomoyo_io_buffer
*head
= file
->private_data
;
1874 const bool is_write
= !!head
->write_buf
;
1877 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1880 if (head
->type
== TOMOYO_QUERY
)
1881 atomic_dec(&tomoyo_query_observers
);
1883 tomoyo_read_unlock(head
->reader_idx
);
1884 /* Release memory used for policy I/O. */
1885 kfree(head
->read_buf
);
1886 head
->read_buf
= NULL
;
1887 kfree(head
->write_buf
);
1888 head
->write_buf
= NULL
;
1891 file
->private_data
= NULL
;
1898 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
1900 void tomoyo_check_profile(void)
1902 struct tomoyo_domain_info
*domain
;
1903 const int idx
= tomoyo_read_lock();
1904 tomoyo_policy_loaded
= true;
1905 /* Check all profiles currently assigned to domains are defined. */
1906 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
1907 const u8 profile
= domain
->profile
;
1908 if (tomoyo_profile_ptr
[profile
])
1910 panic("Profile %u (used by '%s') not defined.\n",
1911 profile
, domain
->domainname
->name
);
1913 tomoyo_read_unlock(idx
);
1914 printk(KERN_INFO
"TOMOYO: 2.2.0 2009/04/01\n");
1915 printk(KERN_INFO
"Mandatory Access Control activated.\n");