2 * security/tomoyo/common.c
4 * Common functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/uaccess.h>
10 #include <linux/slab.h>
11 #include <linux/security.h>
14 static struct tomoyo_profile tomoyo_default_profile
= {
15 .learning
= &tomoyo_default_profile
.preference
,
16 .permissive
= &tomoyo_default_profile
.preference
,
17 .enforcing
= &tomoyo_default_profile
.preference
,
18 .preference
.enforcing_verbose
= true,
19 .preference
.learning_max_entry
= 2048,
20 .preference
.learning_verbose
= false,
21 .preference
.permissive_verbose
= true
24 /* Profile version. Currently only 20090903 is defined. */
25 static unsigned int tomoyo_profile_version
;
27 /* Profile table. Memory is allocated as needed. */
28 static struct tomoyo_profile
*tomoyo_profile_ptr
[TOMOYO_MAX_PROFILES
];
30 /* String table for functionality that takes 4 modes. */
31 static const char *tomoyo_mode
[4] = {
32 "disabled", "learning", "permissive", "enforcing"
35 /* String table for /sys/kernel/security/tomoyo/profile */
36 static const char *tomoyo_mac_keywords
[TOMOYO_MAX_MAC_INDEX
37 + TOMOYO_MAX_MAC_CATEGORY_INDEX
] = {
38 [TOMOYO_MAC_FILE_EXECUTE
] = "file::execute",
39 [TOMOYO_MAC_FILE_OPEN
] = "file::open",
40 [TOMOYO_MAC_FILE_CREATE
] = "file::create",
41 [TOMOYO_MAC_FILE_UNLINK
] = "file::unlink",
42 [TOMOYO_MAC_FILE_MKDIR
] = "file::mkdir",
43 [TOMOYO_MAC_FILE_RMDIR
] = "file::rmdir",
44 [TOMOYO_MAC_FILE_MKFIFO
] = "file::mkfifo",
45 [TOMOYO_MAC_FILE_MKSOCK
] = "file::mksock",
46 [TOMOYO_MAC_FILE_TRUNCATE
] = "file::truncate",
47 [TOMOYO_MAC_FILE_SYMLINK
] = "file::symlink",
48 [TOMOYO_MAC_FILE_REWRITE
] = "file::rewrite",
49 [TOMOYO_MAC_FILE_MKBLOCK
] = "file::mkblock",
50 [TOMOYO_MAC_FILE_MKCHAR
] = "file::mkchar",
51 [TOMOYO_MAC_FILE_LINK
] = "file::link",
52 [TOMOYO_MAC_FILE_RENAME
] = "file::rename",
53 [TOMOYO_MAC_FILE_CHMOD
] = "file::chmod",
54 [TOMOYO_MAC_FILE_CHOWN
] = "file::chown",
55 [TOMOYO_MAC_FILE_CHGRP
] = "file::chgrp",
56 [TOMOYO_MAC_FILE_IOCTL
] = "file::ioctl",
57 [TOMOYO_MAC_FILE_CHROOT
] = "file::chroot",
58 [TOMOYO_MAC_FILE_MOUNT
] = "file::mount",
59 [TOMOYO_MAC_FILE_UMOUNT
] = "file::umount",
60 [TOMOYO_MAC_FILE_PIVOT_ROOT
] = "file::pivot_root",
61 [TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAC_CATEGORY_FILE
] = "file",
64 /* Permit policy management by non-root user? */
65 static bool tomoyo_manage_by_non_root
;
67 /* Utility functions. */
70 * tomoyo_yesno - Return "yes" or "no".
74 static const char *tomoyo_yesno(const unsigned int value
)
76 return value
? "yes" : "no";
79 static void tomoyo_addprintf(char *buffer
, int len
, const char *fmt
, ...)
82 const int pos
= strlen(buffer
);
84 vsnprintf(buffer
+ pos
, len
- pos
- 1, fmt
, args
);
89 * tomoyo_flush - Flush queued string to userspace's buffer.
91 * @head: Pointer to "struct tomoyo_io_buffer".
93 * Returns true if all data was flushed, false otherwise.
95 static bool tomoyo_flush(struct tomoyo_io_buffer
*head
)
97 while (head
->r
.w_pos
) {
98 const char *w
= head
->r
.w
[0];
101 if (len
> head
->read_user_buf_avail
)
102 len
= head
->read_user_buf_avail
;
105 if (copy_to_user(head
->read_user_buf
, w
, len
))
107 head
->read_user_buf_avail
-= len
;
108 head
->read_user_buf
+= len
;
115 /* Add '\0' for query. */
117 if (!head
->read_user_buf_avail
||
118 copy_to_user(head
->read_user_buf
, "", 1))
120 head
->read_user_buf_avail
--;
121 head
->read_user_buf
++;
124 for (len
= 0; len
< head
->r
.w_pos
; len
++)
125 head
->r
.w
[len
] = head
->r
.w
[len
+ 1];
132 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
134 * @head: Pointer to "struct tomoyo_io_buffer".
135 * @string: String to print.
137 * Note that @string has to be kept valid until @head is kfree()d.
138 * This means that char[] allocated on stack memory cannot be passed to
139 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
141 static void tomoyo_set_string(struct tomoyo_io_buffer
*head
, const char *string
)
143 if (head
->r
.w_pos
< TOMOYO_MAX_IO_READ_QUEUE
) {
144 head
->r
.w
[head
->r
.w_pos
++] = string
;
151 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
153 * @head: Pointer to "struct tomoyo_io_buffer".
154 * @fmt: The printf()'s format string, followed by parameters.
156 void tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
160 int pos
= head
->r
.avail
;
161 int size
= head
->readbuf_size
- pos
;
165 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
) + 1;
167 if (pos
+ len
>= head
->readbuf_size
) {
171 head
->r
.avail
+= len
;
172 tomoyo_set_string(head
, head
->read_buf
+ pos
);
175 static void tomoyo_set_space(struct tomoyo_io_buffer
*head
)
177 tomoyo_set_string(head
, " ");
180 static bool tomoyo_set_lf(struct tomoyo_io_buffer
*head
)
182 tomoyo_set_string(head
, "\n");
183 return !head
->r
.w_pos
;
187 * tomoyo_print_name_union - Print a tomoyo_name_union.
189 * @head: Pointer to "struct tomoyo_io_buffer".
190 * @ptr: Pointer to "struct tomoyo_name_union".
192 static void tomoyo_print_name_union(struct tomoyo_io_buffer
*head
,
193 const struct tomoyo_name_union
*ptr
)
195 tomoyo_set_space(head
);
197 tomoyo_set_string(head
, "@");
198 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
200 tomoyo_set_string(head
, ptr
->filename
->name
);
205 * tomoyo_print_number_union - Print a tomoyo_number_union.
207 * @head: Pointer to "struct tomoyo_io_buffer".
208 * @ptr: Pointer to "struct tomoyo_number_union".
210 static void tomoyo_print_number_union(struct tomoyo_io_buffer
*head
,
211 const struct tomoyo_number_union
*ptr
)
213 tomoyo_set_space(head
);
215 tomoyo_set_string(head
, "@");
216 tomoyo_set_string(head
, ptr
->group
->group_name
->name
);
219 unsigned long min
= ptr
->values
[0];
220 const unsigned long max
= ptr
->values
[1];
221 u8 min_type
= ptr
->min_type
;
222 const u8 max_type
= ptr
->max_type
;
225 for (i
= 0; i
< 2; i
++) {
227 case TOMOYO_VALUE_TYPE_HEXADECIMAL
:
228 tomoyo_addprintf(buffer
, sizeof(buffer
),
231 case TOMOYO_VALUE_TYPE_OCTAL
:
232 tomoyo_addprintf(buffer
, sizeof(buffer
),
236 tomoyo_addprintf(buffer
, sizeof(buffer
),
240 if (min
== max
&& min_type
== max_type
)
242 tomoyo_addprintf(buffer
, sizeof(buffer
), "-");
246 tomoyo_io_printf(head
, "%s", buffer
);
251 * tomoyo_find_or_assign_new_profile - Create a new profile.
253 * @profile: Profile number to create.
255 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
257 static struct tomoyo_profile
*tomoyo_find_or_assign_new_profile
258 (const unsigned int profile
)
260 struct tomoyo_profile
*ptr
;
261 struct tomoyo_profile
*entry
;
262 if (profile
>= TOMOYO_MAX_PROFILES
)
264 ptr
= tomoyo_profile_ptr
[profile
];
267 entry
= kzalloc(sizeof(*entry
), GFP_NOFS
);
268 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
270 ptr
= tomoyo_profile_ptr
[profile
];
271 if (!ptr
&& tomoyo_memory_ok(entry
)) {
273 ptr
->learning
= &tomoyo_default_profile
.preference
;
274 ptr
->permissive
= &tomoyo_default_profile
.preference
;
275 ptr
->enforcing
= &tomoyo_default_profile
.preference
;
276 ptr
->default_config
= TOMOYO_CONFIG_DISABLED
;
277 memset(ptr
->config
, TOMOYO_CONFIG_USE_DEFAULT
,
278 sizeof(ptr
->config
));
279 mb(); /* Avoid out-of-order execution. */
280 tomoyo_profile_ptr
[profile
] = ptr
;
283 mutex_unlock(&tomoyo_policy_lock
);
290 * tomoyo_profile - Find a profile.
292 * @profile: Profile number to find.
294 * Returns pointer to "struct tomoyo_profile".
296 struct tomoyo_profile
*tomoyo_profile(const u8 profile
)
298 struct tomoyo_profile
*ptr
= tomoyo_profile_ptr
[profile
];
299 if (!tomoyo_policy_loaded
)
300 return &tomoyo_default_profile
;
305 static s8
tomoyo_find_yesno(const char *string
, const char *find
)
307 const char *cp
= strstr(string
, find
);
310 if (!strncmp(cp
, "=yes", 4))
312 else if (!strncmp(cp
, "=no", 3))
318 static void tomoyo_set_bool(bool *b
, const char *string
, const char *find
)
320 switch (tomoyo_find_yesno(string
, find
)) {
330 static void tomoyo_set_uint(unsigned int *i
, const char *string
,
333 const char *cp
= strstr(string
, find
);
335 sscanf(cp
+ strlen(find
), "=%u", i
);
338 static void tomoyo_set_pref(const char *name
, const char *value
,
339 const bool use_default
,
340 struct tomoyo_profile
*profile
)
342 struct tomoyo_preference
**pref
;
344 if (!strcmp(name
, "enforcing")) {
346 pref
= &profile
->enforcing
;
349 profile
->enforcing
= &profile
->preference
;
350 verbose
= &profile
->preference
.enforcing_verbose
;
353 if (!strcmp(name
, "permissive")) {
355 pref
= &profile
->permissive
;
358 profile
->permissive
= &profile
->preference
;
359 verbose
= &profile
->preference
.permissive_verbose
;
362 if (!strcmp(name
, "learning")) {
364 pref
= &profile
->learning
;
367 profile
->learning
= &profile
->preference
;
368 tomoyo_set_uint(&profile
->preference
.learning_max_entry
, value
,
370 verbose
= &profile
->preference
.learning_verbose
;
375 *pref
= &tomoyo_default_profile
.preference
;
378 tomoyo_set_bool(verbose
, value
, "verbose");
381 static int tomoyo_set_mode(char *name
, const char *value
,
382 const bool use_default
,
383 struct tomoyo_profile
*profile
)
387 if (!strcmp(name
, "CONFIG")) {
388 i
= TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
;
389 config
= profile
->default_config
;
390 } else if (tomoyo_str_starts(&name
, "CONFIG::")) {
392 for (i
= 0; i
< TOMOYO_MAX_MAC_INDEX
393 + TOMOYO_MAX_MAC_CATEGORY_INDEX
; i
++) {
394 if (strcmp(name
, tomoyo_mac_keywords
[i
]))
396 config
= profile
->config
[i
];
399 if (i
== TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
405 config
= TOMOYO_CONFIG_USE_DEFAULT
;
408 for (mode
= 0; mode
< 4; mode
++)
409 if (strstr(value
, tomoyo_mode
[mode
]))
411 * Update lower 3 bits in order to distinguish
412 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
414 config
= (config
& ~7) | mode
;
416 if (i
< TOMOYO_MAX_MAC_INDEX
+ TOMOYO_MAX_MAC_CATEGORY_INDEX
)
417 profile
->config
[i
] = config
;
418 else if (config
!= TOMOYO_CONFIG_USE_DEFAULT
)
419 profile
->default_config
= config
;
424 * tomoyo_write_profile - Write profile table.
426 * @head: Pointer to "struct tomoyo_io_buffer".
428 * Returns 0 on success, negative value otherwise.
430 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
432 char *data
= head
->write_buf
;
434 bool use_default
= false;
436 struct tomoyo_profile
*profile
;
437 if (sscanf(data
, "PROFILE_VERSION=%u", &tomoyo_profile_version
) == 1)
439 i
= simple_strtoul(data
, &cp
, 10);
441 profile
= &tomoyo_default_profile
;
446 profile
= tomoyo_find_or_assign_new_profile(i
);
450 cp
= strchr(data
, '=');
454 if (profile
!= &tomoyo_default_profile
)
455 use_default
= strstr(cp
, "use_default") != NULL
;
456 if (tomoyo_str_starts(&data
, "PREFERENCE::")) {
457 tomoyo_set_pref(data
, cp
, use_default
, profile
);
460 if (profile
== &tomoyo_default_profile
)
462 if (!strcmp(data
, "COMMENT")) {
463 const struct tomoyo_path_info
*old_comment
= profile
->comment
;
464 profile
->comment
= tomoyo_get_name(cp
);
465 tomoyo_put_name(old_comment
);
468 return tomoyo_set_mode(data
, cp
, use_default
, profile
);
471 static void tomoyo_print_preference(struct tomoyo_io_buffer
*head
,
474 struct tomoyo_preference
*pref
= &tomoyo_default_profile
.preference
;
475 const struct tomoyo_profile
*profile
= idx
>= 0 ?
476 tomoyo_profile_ptr
[idx
] : NULL
;
477 char buffer
[16] = "";
479 buffer
[sizeof(buffer
) - 1] = '\0';
480 snprintf(buffer
, sizeof(buffer
) - 1, "%u-", idx
);
483 pref
= profile
->learning
;
484 if (pref
== &tomoyo_default_profile
.preference
)
487 tomoyo_io_printf(head
, "%sPREFERENCE::%s={ "
488 "verbose=%s max_entry=%u }\n",
490 tomoyo_yesno(pref
->learning_verbose
),
491 pref
->learning_max_entry
);
494 pref
= profile
->permissive
;
495 if (pref
== &tomoyo_default_profile
.preference
)
498 tomoyo_io_printf(head
, "%sPREFERENCE::%s={ verbose=%s }\n",
499 buffer
, "permissive",
500 tomoyo_yesno(pref
->permissive_verbose
));
503 pref
= profile
->enforcing
;
504 if (pref
== &tomoyo_default_profile
.preference
)
507 tomoyo_io_printf(head
, "%sPREFERENCE::%s={ verbose=%s }\n",
509 tomoyo_yesno(pref
->enforcing_verbose
));
512 static void tomoyo_print_config(struct tomoyo_io_buffer
*head
, const u8 config
)
514 tomoyo_io_printf(head
, "={ mode=%s }\n", tomoyo_mode
[config
& 3]);
518 * tomoyo_read_profile - Read profile table.
520 * @head: Pointer to "struct tomoyo_io_buffer".
522 static void tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
525 const struct tomoyo_profile
*profile
;
527 index
= head
->r
.index
;
528 profile
= tomoyo_profile_ptr
[index
];
529 switch (head
->r
.step
) {
531 tomoyo_io_printf(head
, "PROFILE_VERSION=%s\n", "20090903");
532 tomoyo_print_preference(head
, -1);
536 for ( ; head
->r
.index
< TOMOYO_MAX_PROFILES
;
538 if (tomoyo_profile_ptr
[head
->r
.index
])
540 if (head
->r
.index
== TOMOYO_MAX_PROFILES
)
546 const struct tomoyo_path_info
*comment
=
548 tomoyo_io_printf(head
, "%u-COMMENT=", index
);
549 tomoyo_set_string(head
, comment
? comment
->name
: "");
556 tomoyo_io_printf(head
, "%u-%s", index
, "CONFIG");
557 tomoyo_print_config(head
, profile
->default_config
);
563 for ( ; head
->r
.bit
< TOMOYO_MAX_MAC_INDEX
564 + TOMOYO_MAX_MAC_CATEGORY_INDEX
; head
->r
.bit
++) {
565 const u8 i
= head
->r
.bit
;
566 const u8 config
= profile
->config
[i
];
567 if (config
== TOMOYO_CONFIG_USE_DEFAULT
)
569 tomoyo_io_printf(head
, "%u-%s%s", index
, "CONFIG::",
570 tomoyo_mac_keywords
[i
]);
571 tomoyo_print_config(head
, config
);
575 if (head
->r
.bit
== TOMOYO_MAX_MAC_INDEX
576 + TOMOYO_MAX_MAC_CATEGORY_INDEX
) {
577 tomoyo_print_preference(head
, index
);
583 if (tomoyo_flush(head
))
587 static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head
*a
,
588 const struct tomoyo_acl_head
*b
)
590 return container_of(a
, struct tomoyo_policy_manager_entry
, head
)
592 container_of(b
, struct tomoyo_policy_manager_entry
, head
)
597 * tomoyo_update_manager_entry - Add a manager entry.
599 * @manager: The path to manager or the domainnamme.
600 * @is_delete: True if it is a delete request.
602 * Returns 0 on success, negative value otherwise.
604 * Caller holds tomoyo_read_lock().
606 static int tomoyo_update_manager_entry(const char *manager
,
607 const bool is_delete
)
609 struct tomoyo_policy_manager_entry e
= { };
612 if (tomoyo_domain_def(manager
)) {
613 if (!tomoyo_correct_domain(manager
))
617 if (!tomoyo_correct_path(manager
))
620 e
.manager
= tomoyo_get_name(manager
);
623 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), is_delete
,
624 &tomoyo_policy_list
[TOMOYO_ID_MANAGER
],
625 tomoyo_same_manager_entry
);
626 tomoyo_put_name(e
.manager
);
631 * tomoyo_write_manager_policy - Write manager policy.
633 * @head: Pointer to "struct tomoyo_io_buffer".
635 * Returns 0 on success, negative value otherwise.
637 * Caller holds tomoyo_read_lock().
639 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer
*head
)
641 char *data
= head
->write_buf
;
642 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
644 if (!strcmp(data
, "manage_by_non_root")) {
645 tomoyo_manage_by_non_root
= !is_delete
;
648 return tomoyo_update_manager_entry(data
, is_delete
);
652 * tomoyo_read_manager_policy - Read manager policy.
654 * @head: Pointer to "struct tomoyo_io_buffer".
656 * Caller holds tomoyo_read_lock().
658 static void tomoyo_read_manager_policy(struct tomoyo_io_buffer
*head
)
662 list_for_each_cookie(head
->r
.acl
,
663 &tomoyo_policy_list
[TOMOYO_ID_MANAGER
]) {
664 struct tomoyo_policy_manager_entry
*ptr
=
665 list_entry(head
->r
.acl
, typeof(*ptr
), head
.list
);
666 if (ptr
->head
.is_deleted
)
668 if (!tomoyo_flush(head
))
670 tomoyo_set_string(head
, ptr
->manager
->name
);
677 * tomoyo_policy_manager - Check whether the current process is a policy manager.
679 * Returns true if the current process is permitted to modify policy
680 * via /sys/kernel/security/tomoyo/ interface.
682 * Caller holds tomoyo_read_lock().
684 static bool tomoyo_policy_manager(void)
686 struct tomoyo_policy_manager_entry
*ptr
;
688 const struct task_struct
*task
= current
;
689 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
692 if (!tomoyo_policy_loaded
)
694 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
696 list_for_each_entry_rcu(ptr
, &tomoyo_policy_list
[TOMOYO_ID_MANAGER
],
698 if (!ptr
->head
.is_deleted
&& ptr
->is_domain
699 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
706 exe
= tomoyo_get_exe();
709 list_for_each_entry_rcu(ptr
, &tomoyo_policy_list
[TOMOYO_ID_MANAGER
],
711 if (!ptr
->head
.is_deleted
&& !ptr
->is_domain
712 && !strcmp(exe
, ptr
->manager
->name
)) {
717 if (!found
) { /* Reduce error messages. */
718 static pid_t last_pid
;
719 const pid_t pid
= current
->pid
;
720 if (last_pid
!= pid
) {
721 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
722 "update policies.\n", domainname
->name
, exe
);
731 * tomoyo_select_one - Parse select command.
733 * @head: Pointer to "struct tomoyo_io_buffer".
734 * @data: String to parse.
736 * Returns true on success, false otherwise.
738 * Caller holds tomoyo_read_lock().
740 static bool tomoyo_select_one(struct tomoyo_io_buffer
*head
, const char *data
)
743 struct tomoyo_domain_info
*domain
= NULL
;
744 bool global_pid
= false;
746 if (!strcmp(data
, "allow_execute")) {
747 head
->r
.print_execute_only
= true;
750 if (sscanf(data
, "pid=%u", &pid
) == 1 ||
751 (global_pid
= true, sscanf(data
, "global-pid=%u", &pid
) == 1)) {
752 struct task_struct
*p
;
754 read_lock(&tasklist_lock
);
756 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
758 p
= find_task_by_vpid(pid
);
760 domain
= tomoyo_real_domain(p
);
761 read_unlock(&tasklist_lock
);
763 } else if (!strncmp(data
, "domain=", 7)) {
764 if (tomoyo_domain_def(data
+ 7))
765 domain
= tomoyo_find_domain(data
+ 7);
768 head
->write_var1
= domain
;
769 /* Accessing read_buf is safe because head->io_sem is held. */
771 return true; /* Do nothing if open(O_WRONLY). */
772 memset(&head
->r
, 0, sizeof(head
->r
));
773 head
->r
.print_this_domain_only
= true;
774 head
->r
.eof
= !domain
;
775 head
->r
.domain
= &domain
->list
;
776 tomoyo_io_printf(head
, "# select %s\n", data
);
777 if (domain
&& domain
->is_deleted
)
778 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
783 * tomoyo_delete_domain - Delete a domain.
785 * @domainname: The name of domain.
789 * Caller holds tomoyo_read_lock().
791 static int tomoyo_delete_domain(char *domainname
)
793 struct tomoyo_domain_info
*domain
;
794 struct tomoyo_path_info name
;
796 name
.name
= domainname
;
797 tomoyo_fill_path_info(&name
);
798 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
800 /* Is there an active domain? */
801 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
802 /* Never delete tomoyo_kernel_domain */
803 if (domain
== &tomoyo_kernel_domain
)
805 if (domain
->is_deleted
||
806 tomoyo_pathcmp(domain
->domainname
, &name
))
808 domain
->is_deleted
= true;
811 mutex_unlock(&tomoyo_policy_lock
);
816 * tomoyo_write_domain_policy2 - Write domain policy.
818 * @head: Pointer to "struct tomoyo_io_buffer".
820 * Returns 0 on success, negative value otherwise.
822 * Caller holds tomoyo_read_lock().
824 static int tomoyo_write_domain_policy2(char *data
,
825 struct tomoyo_domain_info
*domain
,
826 const bool is_delete
)
828 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_MOUNT
))
829 return tomoyo_write_mount_policy(data
, domain
, is_delete
);
830 return tomoyo_write_file_policy(data
, domain
, is_delete
);
834 * tomoyo_write_domain_policy - Write domain policy.
836 * @head: Pointer to "struct tomoyo_io_buffer".
838 * Returns 0 on success, negative value otherwise.
840 * Caller holds tomoyo_read_lock().
842 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer
*head
)
844 char *data
= head
->write_buf
;
845 struct tomoyo_domain_info
*domain
= head
->write_var1
;
846 bool is_delete
= false;
847 bool is_select
= false;
848 unsigned int profile
;
850 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
))
852 else if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_SELECT
))
854 if (is_select
&& tomoyo_select_one(head
, data
))
856 /* Don't allow updating policies by non manager programs. */
857 if (!tomoyo_policy_manager())
859 if (tomoyo_domain_def(data
)) {
862 tomoyo_delete_domain(data
);
864 domain
= tomoyo_find_domain(data
);
866 domain
= tomoyo_find_or_assign_new_domain(data
, 0);
867 head
->write_var1
= domain
;
873 if (sscanf(data
, TOMOYO_KEYWORD_USE_PROFILE
"%u", &profile
) == 1
874 && profile
< TOMOYO_MAX_PROFILES
) {
875 if (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
)
876 domain
->profile
= (u8
) profile
;
879 if (!strcmp(data
, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
)) {
880 domain
->ignore_global_allow_read
= !is_delete
;
883 if (!strcmp(data
, TOMOYO_KEYWORD_QUOTA_EXCEEDED
)) {
884 domain
->quota_warned
= !is_delete
;
887 if (!strcmp(data
, TOMOYO_KEYWORD_TRANSITION_FAILED
)) {
888 domain
->transition_failed
= !is_delete
;
891 return tomoyo_write_domain_policy2(data
, domain
, is_delete
);
895 * tomoyo_fns - Find next set bit.
897 * @perm: 8 bits value.
898 * @bit: First bit to find.
900 * Returns next on-bit on success, 8 otherwise.
902 static u8
tomoyo_fns(const u8 perm
, u8 bit
)
904 for ( ; bit
< 8; bit
++)
905 if (perm
& (1 << bit
))
911 * tomoyo_print_entry - Print an ACL entry.
913 * @head: Pointer to "struct tomoyo_io_buffer".
914 * @acl: Pointer to an ACL entry.
916 * Returns true on success, false otherwise.
918 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
919 struct tomoyo_acl_info
*acl
)
921 const u8 acl_type
= acl
->type
;
928 if (!tomoyo_flush(head
))
930 else if (acl_type
== TOMOYO_TYPE_PATH_ACL
) {
931 struct tomoyo_path_acl
*ptr
=
932 container_of(acl
, typeof(*ptr
), head
);
933 const u16 perm
= ptr
->perm
;
934 for ( ; bit
< TOMOYO_MAX_PATH_OPERATION
; bit
++) {
935 if (!(perm
& (1 << bit
)))
937 if (head
->r
.print_execute_only
&&
938 bit
!= TOMOYO_TYPE_EXECUTE
)
940 /* Print "read/write" instead of "read" and "write". */
941 if ((bit
== TOMOYO_TYPE_READ
||
942 bit
== TOMOYO_TYPE_WRITE
)
943 && (perm
& (1 << TOMOYO_TYPE_READ_WRITE
)))
947 if (bit
>= TOMOYO_MAX_PATH_OPERATION
)
949 tomoyo_io_printf(head
, "allow_%s", tomoyo_path_keyword
[bit
]);
950 tomoyo_print_name_union(head
, &ptr
->name
);
951 } else if (head
->r
.print_execute_only
) {
953 } else if (acl_type
== TOMOYO_TYPE_PATH2_ACL
) {
954 struct tomoyo_path2_acl
*ptr
=
955 container_of(acl
, typeof(*ptr
), head
);
956 bit
= tomoyo_fns(ptr
->perm
, bit
);
957 if (bit
>= TOMOYO_MAX_PATH2_OPERATION
)
959 tomoyo_io_printf(head
, "allow_%s", tomoyo_path2_keyword
[bit
]);
960 tomoyo_print_name_union(head
, &ptr
->name1
);
961 tomoyo_print_name_union(head
, &ptr
->name2
);
962 } else if (acl_type
== TOMOYO_TYPE_PATH_NUMBER_ACL
) {
963 struct tomoyo_path_number_acl
*ptr
=
964 container_of(acl
, typeof(*ptr
), head
);
965 bit
= tomoyo_fns(ptr
->perm
, bit
);
966 if (bit
>= TOMOYO_MAX_PATH_NUMBER_OPERATION
)
968 tomoyo_io_printf(head
, "allow_%s",
969 tomoyo_path_number_keyword
[bit
]);
970 tomoyo_print_name_union(head
, &ptr
->name
);
971 tomoyo_print_number_union(head
, &ptr
->number
);
972 } else if (acl_type
== TOMOYO_TYPE_MKDEV_ACL
) {
973 struct tomoyo_mkdev_acl
*ptr
=
974 container_of(acl
, typeof(*ptr
), head
);
975 bit
= tomoyo_fns(ptr
->perm
, bit
);
976 if (bit
>= TOMOYO_MAX_MKDEV_OPERATION
)
978 tomoyo_io_printf(head
, "allow_%s", tomoyo_mkdev_keyword
[bit
]);
979 tomoyo_print_name_union(head
, &ptr
->name
);
980 tomoyo_print_number_union(head
, &ptr
->mode
);
981 tomoyo_print_number_union(head
, &ptr
->major
);
982 tomoyo_print_number_union(head
, &ptr
->minor
);
983 } else if (acl_type
== TOMOYO_TYPE_MOUNT_ACL
) {
984 struct tomoyo_mount_acl
*ptr
=
985 container_of(acl
, typeof(*ptr
), head
);
986 tomoyo_io_printf(head
, "allow_mount");
987 tomoyo_print_name_union(head
, &ptr
->dev_name
);
988 tomoyo_print_name_union(head
, &ptr
->dir_name
);
989 tomoyo_print_name_union(head
, &ptr
->fs_type
);
990 tomoyo_print_number_union(head
, &ptr
->flags
);
992 head
->r
.bit
= bit
+ 1;
993 tomoyo_io_printf(head
, "\n");
994 if (acl_type
!= TOMOYO_TYPE_MOUNT_ACL
)
1002 * tomoyo_read_domain2 - Read domain policy.
1004 * @head: Pointer to "struct tomoyo_io_buffer".
1005 * @domain: Pointer to "struct tomoyo_domain_info".
1007 * Caller holds tomoyo_read_lock().
1009 * Returns true on success, false otherwise.
1011 static bool tomoyo_read_domain2(struct tomoyo_io_buffer
*head
,
1012 struct tomoyo_domain_info
*domain
)
1014 list_for_each_cookie(head
->r
.acl
, &domain
->acl_info_list
) {
1015 struct tomoyo_acl_info
*ptr
=
1016 list_entry(head
->r
.acl
, typeof(*ptr
), list
);
1017 if (!tomoyo_print_entry(head
, ptr
))
1025 * tomoyo_read_domain_policy - Read domain policy.
1027 * @head: Pointer to "struct tomoyo_io_buffer".
1029 * Caller holds tomoyo_read_lock().
1031 static void tomoyo_read_domain_policy(struct tomoyo_io_buffer
*head
)
1035 list_for_each_cookie(head
->r
.domain
, &tomoyo_domain_list
) {
1036 struct tomoyo_domain_info
*domain
=
1037 list_entry(head
->r
.domain
, typeof(*domain
), list
);
1038 switch (head
->r
.step
) {
1040 if (domain
->is_deleted
&&
1041 !head
->r
.print_this_domain_only
)
1043 /* Print domainname and flags. */
1044 tomoyo_set_string(head
, domain
->domainname
->name
);
1045 tomoyo_set_lf(head
);
1046 tomoyo_io_printf(head
,
1047 TOMOYO_KEYWORD_USE_PROFILE
"%u\n",
1049 if (domain
->quota_warned
)
1050 tomoyo_set_string(head
, "quota_exceeded\n");
1051 if (domain
->transition_failed
)
1052 tomoyo_set_string(head
, "transition_failed\n");
1053 if (domain
->ignore_global_allow_read
)
1054 tomoyo_set_string(head
,
1055 TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
1058 tomoyo_set_lf(head
);
1061 if (!tomoyo_read_domain2(head
, domain
))
1064 if (!tomoyo_set_lf(head
))
1069 if (head
->r
.print_this_domain_only
)
1078 * tomoyo_write_domain_profile - Assign profile for specified domain.
1080 * @head: Pointer to "struct tomoyo_io_buffer".
1082 * Returns 0 on success, -EINVAL otherwise.
1084 * This is equivalent to doing
1086 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1087 * /usr/sbin/tomoyo-loadpolicy -d
1089 * Caller holds tomoyo_read_lock().
1091 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer
*head
)
1093 char *data
= head
->write_buf
;
1094 char *cp
= strchr(data
, ' ');
1095 struct tomoyo_domain_info
*domain
;
1096 unsigned long profile
;
1101 domain
= tomoyo_find_domain(cp
+ 1);
1102 if (strict_strtoul(data
, 10, &profile
))
1104 if (domain
&& profile
< TOMOYO_MAX_PROFILES
1105 && (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
))
1106 domain
->profile
= (u8
) profile
;
1111 * tomoyo_read_domain_profile - Read only domainname and profile.
1113 * @head: Pointer to "struct tomoyo_io_buffer".
1115 * Returns list of profile number and domainname pairs.
1117 * This is equivalent to doing
1119 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1120 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1121 * domainname = $0; } else if ( $1 == "use_profile" ) {
1122 * print $2 " " domainname; domainname = ""; } } ; '
1124 * Caller holds tomoyo_read_lock().
1126 static void tomoyo_read_domain_profile(struct tomoyo_io_buffer
*head
)
1130 list_for_each_cookie(head
->r
.domain
, &tomoyo_domain_list
) {
1131 struct tomoyo_domain_info
*domain
=
1132 list_entry(head
->r
.domain
, typeof(*domain
), list
);
1133 if (domain
->is_deleted
)
1135 if (!tomoyo_flush(head
))
1137 tomoyo_io_printf(head
, "%u ", domain
->profile
);
1138 tomoyo_set_string(head
, domain
->domainname
->name
);
1139 tomoyo_set_lf(head
);
1145 * tomoyo_write_pid: Specify PID to obtain domainname.
1147 * @head: Pointer to "struct tomoyo_io_buffer".
1151 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1153 head
->r
.eof
= false;
1158 * tomoyo_read_pid - Get domainname of the specified PID.
1160 * @head: Pointer to "struct tomoyo_io_buffer".
1162 * Returns the domainname which the specified PID is in on success,
1163 * empty string otherwise.
1164 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1165 * using read()/write() interface rather than sysctl() interface.
1167 static void tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1169 char *buf
= head
->write_buf
;
1170 bool global_pid
= false;
1172 struct task_struct
*p
;
1173 struct tomoyo_domain_info
*domain
= NULL
;
1175 /* Accessing write_buf is safe because head->io_sem is held. */
1178 return; /* Do nothing if open(O_RDONLY). */
1180 if (head
->r
.w_pos
|| head
->r
.eof
)
1183 if (tomoyo_str_starts(&buf
, "global-pid "))
1185 pid
= (unsigned int) simple_strtoul(buf
, NULL
, 10);
1187 read_lock(&tasklist_lock
);
1189 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
1191 p
= find_task_by_vpid(pid
);
1193 domain
= tomoyo_real_domain(p
);
1194 read_unlock(&tasklist_lock
);
1198 tomoyo_io_printf(head
, "%u %u ", pid
, domain
->profile
);
1199 tomoyo_set_string(head
, domain
->domainname
->name
);
1202 static const char *tomoyo_transition_type
[TOMOYO_MAX_TRANSITION_TYPE
] = {
1203 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE
]
1204 = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN
,
1205 [TOMOYO_TRANSITION_CONTROL_INITIALIZE
]
1206 = TOMOYO_KEYWORD_INITIALIZE_DOMAIN
,
1207 [TOMOYO_TRANSITION_CONTROL_NO_KEEP
] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN
,
1208 [TOMOYO_TRANSITION_CONTROL_KEEP
] = TOMOYO_KEYWORD_KEEP_DOMAIN
1212 * tomoyo_write_exception_policy - Write exception policy.
1214 * @head: Pointer to "struct tomoyo_io_buffer".
1216 * Returns 0 on success, negative value otherwise.
1218 * Caller holds tomoyo_read_lock().
1220 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer
*head
)
1222 char *data
= head
->write_buf
;
1223 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1226 for (i
= 0; i
< TOMOYO_MAX_TRANSITION_TYPE
; i
++) {
1227 if (tomoyo_str_starts(&data
, tomoyo_transition_type
[i
]))
1228 return tomoyo_write_transition_control(data
, is_delete
,
1231 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_AGGREGATOR
))
1232 return tomoyo_write_aggregator_policy(data
, is_delete
);
1233 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_READ
))
1234 return tomoyo_write_globally_readable_policy(data
, is_delete
);
1235 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_FILE_PATTERN
))
1236 return tomoyo_write_pattern_policy(data
, is_delete
);
1237 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DENY_REWRITE
))
1238 return tomoyo_write_no_rewrite_policy(data
, is_delete
);
1239 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_PATH_GROUP
))
1240 return tomoyo_write_group(data
, is_delete
, TOMOYO_PATH_GROUP
);
1241 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NUMBER_GROUP
))
1242 return tomoyo_write_group(data
, is_delete
, TOMOYO_NUMBER_GROUP
);
1246 static const char *tomoyo_group_name
[TOMOYO_MAX_GROUP
] = {
1247 [TOMOYO_PATH_GROUP
] = TOMOYO_KEYWORD_PATH_GROUP
,
1248 [TOMOYO_NUMBER_GROUP
] = TOMOYO_KEYWORD_NUMBER_GROUP
1252 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1254 * @head: Pointer to "struct tomoyo_io_buffer".
1255 * @idx: Index number.
1257 * Returns true on success, false otherwise.
1259 * Caller holds tomoyo_read_lock().
1261 static bool tomoyo_read_group(struct tomoyo_io_buffer
*head
, const int idx
)
1263 list_for_each_cookie(head
->r
.group
, &tomoyo_group_list
[idx
]) {
1264 struct tomoyo_group
*group
=
1265 list_entry(head
->r
.group
, typeof(*group
), list
);
1266 list_for_each_cookie(head
->r
.acl
, &group
->member_list
) {
1267 struct tomoyo_acl_head
*ptr
=
1268 list_entry(head
->r
.acl
, typeof(*ptr
), list
);
1269 if (ptr
->is_deleted
)
1271 if (!tomoyo_flush(head
))
1273 tomoyo_set_string(head
, tomoyo_group_name
[idx
]);
1274 tomoyo_set_string(head
, group
->group_name
->name
);
1275 if (idx
== TOMOYO_PATH_GROUP
) {
1276 tomoyo_set_space(head
);
1277 tomoyo_set_string(head
, container_of
1278 (ptr
, struct tomoyo_path_group
,
1279 head
)->member_name
->name
);
1280 } else if (idx
== TOMOYO_NUMBER_GROUP
) {
1281 tomoyo_print_number_union(head
, &container_of
1283 struct tomoyo_number_group
,
1286 tomoyo_set_lf(head
);
1290 head
->r
.group
= NULL
;
1295 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1297 * @head: Pointer to "struct tomoyo_io_buffer".
1298 * @idx: Index number.
1300 * Returns true on success, false otherwise.
1302 * Caller holds tomoyo_read_lock().
1304 static bool tomoyo_read_policy(struct tomoyo_io_buffer
*head
, const int idx
)
1306 list_for_each_cookie(head
->r
.acl
, &tomoyo_policy_list
[idx
]) {
1307 struct tomoyo_acl_head
*acl
=
1308 container_of(head
->r
.acl
, typeof(*acl
), list
);
1309 if (acl
->is_deleted
)
1311 if (!tomoyo_flush(head
))
1314 case TOMOYO_ID_TRANSITION_CONTROL
:
1316 struct tomoyo_transition_control
*ptr
=
1317 container_of(acl
, typeof(*ptr
), head
);
1318 tomoyo_set_string(head
,
1319 tomoyo_transition_type
1322 tomoyo_set_string(head
,
1323 ptr
->program
->name
);
1324 if (ptr
->program
&& ptr
->domainname
)
1325 tomoyo_set_string(head
, " from ");
1326 if (ptr
->domainname
)
1327 tomoyo_set_string(head
,
1332 case TOMOYO_ID_GLOBALLY_READABLE
:
1334 struct tomoyo_globally_readable_file_entry
*ptr
1335 = container_of(acl
, typeof(*ptr
), head
);
1336 tomoyo_set_string(head
,
1337 TOMOYO_KEYWORD_ALLOW_READ
);
1338 tomoyo_set_string(head
, ptr
->filename
->name
);
1341 case TOMOYO_ID_AGGREGATOR
:
1343 struct tomoyo_aggregator_entry
*ptr
=
1344 container_of(acl
, typeof(*ptr
), head
);
1345 tomoyo_set_string(head
,
1346 TOMOYO_KEYWORD_AGGREGATOR
);
1347 tomoyo_set_string(head
,
1348 ptr
->original_name
->name
);
1349 tomoyo_set_space(head
);
1350 tomoyo_set_string(head
,
1351 ptr
->aggregated_name
->name
);
1354 case TOMOYO_ID_PATTERN
:
1356 struct tomoyo_pattern_entry
*ptr
=
1357 container_of(acl
, typeof(*ptr
), head
);
1358 tomoyo_set_string(head
,
1359 TOMOYO_KEYWORD_FILE_PATTERN
);
1360 tomoyo_set_string(head
, ptr
->pattern
->name
);
1363 case TOMOYO_ID_NO_REWRITE
:
1365 struct tomoyo_no_rewrite_entry
*ptr
=
1366 container_of(acl
, typeof(*ptr
), head
);
1367 tomoyo_set_string(head
,
1368 TOMOYO_KEYWORD_DENY_REWRITE
);
1369 tomoyo_set_string(head
, ptr
->pattern
->name
);
1375 tomoyo_set_lf(head
);
1382 * tomoyo_read_exception_policy - Read exception policy.
1384 * @head: Pointer to "struct tomoyo_io_buffer".
1386 * Caller holds tomoyo_read_lock().
1388 static void tomoyo_read_exception_policy(struct tomoyo_io_buffer
*head
)
1392 while (head
->r
.step
< TOMOYO_MAX_POLICY
&&
1393 tomoyo_read_policy(head
, head
->r
.step
))
1395 if (head
->r
.step
< TOMOYO_MAX_POLICY
)
1397 while (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
&&
1398 tomoyo_read_group(head
, head
->r
.step
- TOMOYO_MAX_POLICY
))
1400 if (head
->r
.step
< TOMOYO_MAX_POLICY
+ TOMOYO_MAX_GROUP
)
1406 * tomoyo_print_header - Get header line of audit log.
1408 * @r: Pointer to "struct tomoyo_request_info".
1410 * Returns string representation.
1412 * This function uses kmalloc(), so caller must kfree() if this function
1413 * didn't return NULL.
1415 static char *tomoyo_print_header(struct tomoyo_request_info
*r
)
1418 const pid_t gpid
= task_pid_nr(current
);
1419 static const int tomoyo_buffer_len
= 4096;
1420 char *buffer
= kmalloc(tomoyo_buffer_len
, GFP_NOFS
);
1423 do_gettimeofday(&tv
);
1424 snprintf(buffer
, tomoyo_buffer_len
- 1,
1425 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1426 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1427 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1428 tv
.tv_sec
, r
->profile
, tomoyo_mode
[r
->mode
], gpid
,
1429 (pid_t
) sys_getpid(), (pid_t
) sys_getppid(),
1430 current_uid(), current_gid(), current_euid(),
1431 current_egid(), current_suid(), current_sgid(),
1432 current_fsuid(), current_fsgid());
1437 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1439 * @len: Required size.
1440 * @r: Pointer to "struct tomoyo_request_info".
1442 * Returns pointer to allocated memory.
1444 * The @len is updated to add the header lines' size on success.
1446 * This function uses kzalloc(), so caller must kfree() if this function
1447 * didn't return NULL.
1449 static char *tomoyo_init_audit_log(int *len
, struct tomoyo_request_info
*r
)
1453 const char *domainname
;
1455 r
->domain
= tomoyo_domain();
1456 domainname
= r
->domain
->domainname
->name
;
1457 header
= tomoyo_print_header(r
);
1460 *len
+= strlen(domainname
) + strlen(header
) + 10;
1461 buf
= kzalloc(*len
, GFP_NOFS
);
1463 snprintf(buf
, (*len
) - 1, "%s\n%s\n", header
, domainname
);
1468 /* Wait queue for tomoyo_query_list. */
1469 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait
);
1471 /* Lock for manipulating tomoyo_query_list. */
1472 static DEFINE_SPINLOCK(tomoyo_query_list_lock
);
1474 /* Structure for query. */
1475 struct tomoyo_query_entry
{
1476 struct list_head list
;
1479 unsigned int serial
;
1484 /* The list for "struct tomoyo_query_entry". */
1485 static LIST_HEAD(tomoyo_query_list
);
1488 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1491 static atomic_t tomoyo_query_observers
= ATOMIC_INIT(0);
1494 * tomoyo_supervisor - Ask for the supervisor's decision.
1496 * @r: Pointer to "struct tomoyo_request_info".
1497 * @fmt: The printf()'s format string, followed by parameters.
1499 * Returns 0 if the supervisor decided to permit the access request which
1500 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1501 * supervisor decided to retry the access request which violated the policy in
1502 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1504 int tomoyo_supervisor(struct tomoyo_request_info
*r
, const char *fmt
, ...)
1510 static unsigned int tomoyo_serial
;
1511 struct tomoyo_query_entry
*tomoyo_query_entry
= NULL
;
1512 bool quota_exceeded
= false;
1516 case TOMOYO_CONFIG_LEARNING
:
1517 if (!tomoyo_domain_quota_is_ok(r
))
1519 va_start(args
, fmt
);
1520 len
= vsnprintf((char *) &pos
, sizeof(pos
) - 1, fmt
, args
) + 4;
1522 buffer
= kmalloc(len
, GFP_NOFS
);
1525 va_start(args
, fmt
);
1526 vsnprintf(buffer
, len
- 1, fmt
, args
);
1528 tomoyo_normalize_line(buffer
);
1529 tomoyo_write_domain_policy2(buffer
, r
->domain
, false);
1532 case TOMOYO_CONFIG_PERMISSIVE
:
1536 r
->domain
= tomoyo_domain();
1537 if (!atomic_read(&tomoyo_query_observers
))
1539 va_start(args
, fmt
);
1540 len
= vsnprintf((char *) &pos
, sizeof(pos
) - 1, fmt
, args
) + 32;
1542 header
= tomoyo_init_audit_log(&len
, r
);
1545 tomoyo_query_entry
= kzalloc(sizeof(*tomoyo_query_entry
), GFP_NOFS
);
1546 if (!tomoyo_query_entry
)
1548 tomoyo_query_entry
->query
= kzalloc(len
, GFP_NOFS
);
1549 if (!tomoyo_query_entry
->query
)
1551 len
= ksize(tomoyo_query_entry
->query
);
1552 INIT_LIST_HEAD(&tomoyo_query_entry
->list
);
1553 spin_lock(&tomoyo_query_list_lock
);
1554 if (tomoyo_quota_for_query
&& tomoyo_query_memory_size
+ len
+
1555 sizeof(*tomoyo_query_entry
) >= tomoyo_quota_for_query
) {
1556 quota_exceeded
= true;
1558 tomoyo_query_memory_size
+= len
+ sizeof(*tomoyo_query_entry
);
1559 tomoyo_query_entry
->serial
= tomoyo_serial
++;
1561 spin_unlock(&tomoyo_query_list_lock
);
1564 pos
= snprintf(tomoyo_query_entry
->query
, len
- 1, "Q%u-%hu\n%s",
1565 tomoyo_query_entry
->serial
, r
->retry
, header
);
1568 va_start(args
, fmt
);
1569 vsnprintf(tomoyo_query_entry
->query
+ pos
, len
- 1 - pos
, fmt
, args
);
1570 tomoyo_query_entry
->query_len
= strlen(tomoyo_query_entry
->query
) + 1;
1572 spin_lock(&tomoyo_query_list_lock
);
1573 list_add_tail(&tomoyo_query_entry
->list
, &tomoyo_query_list
);
1574 spin_unlock(&tomoyo_query_list_lock
);
1575 /* Give 10 seconds for supervisor's opinion. */
1576 for (tomoyo_query_entry
->timer
= 0;
1577 atomic_read(&tomoyo_query_observers
) && tomoyo_query_entry
->timer
< 100;
1578 tomoyo_query_entry
->timer
++) {
1579 wake_up(&tomoyo_query_wait
);
1580 set_current_state(TASK_INTERRUPTIBLE
);
1581 schedule_timeout(HZ
/ 10);
1582 if (tomoyo_query_entry
->answer
)
1585 spin_lock(&tomoyo_query_list_lock
);
1586 list_del(&tomoyo_query_entry
->list
);
1587 tomoyo_query_memory_size
-= len
+ sizeof(*tomoyo_query_entry
);
1588 spin_unlock(&tomoyo_query_list_lock
);
1589 switch (tomoyo_query_entry
->answer
) {
1590 case 3: /* Asked to retry by administrator. */
1591 error
= TOMOYO_RETRY_REQUEST
;
1595 /* Granted by administrator. */
1602 /* Rejected by administrator. */
1606 if (tomoyo_query_entry
)
1607 kfree(tomoyo_query_entry
->query
);
1608 kfree(tomoyo_query_entry
);
1614 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1616 * @file: Pointer to "struct file".
1617 * @wait: Pointer to "poll_table".
1619 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1621 * Waits for access requests which violated policy in enforcing mode.
1623 static int tomoyo_poll_query(struct file
*file
, poll_table
*wait
)
1625 struct list_head
*tmp
;
1628 for (i
= 0; i
< 2; i
++) {
1629 spin_lock(&tomoyo_query_list_lock
);
1630 list_for_each(tmp
, &tomoyo_query_list
) {
1631 struct tomoyo_query_entry
*ptr
1632 = list_entry(tmp
, struct tomoyo_query_entry
,
1639 spin_unlock(&tomoyo_query_list_lock
);
1641 return POLLIN
| POLLRDNORM
;
1644 poll_wait(file
, &tomoyo_query_wait
, wait
);
1650 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1652 * @head: Pointer to "struct tomoyo_io_buffer".
1654 static void tomoyo_read_query(struct tomoyo_io_buffer
*head
)
1656 struct list_head
*tmp
;
1662 if (head
->read_buf
) {
1663 kfree(head
->read_buf
);
1664 head
->read_buf
= NULL
;
1666 spin_lock(&tomoyo_query_list_lock
);
1667 list_for_each(tmp
, &tomoyo_query_list
) {
1668 struct tomoyo_query_entry
*ptr
=
1669 list_entry(tmp
, typeof(*ptr
), list
);
1672 if (pos
++ != head
->r
.query_index
)
1674 len
= ptr
->query_len
;
1677 spin_unlock(&tomoyo_query_list_lock
);
1679 head
->r
.query_index
= 0;
1682 buf
= kzalloc(len
, GFP_NOFS
);
1686 spin_lock(&tomoyo_query_list_lock
);
1687 list_for_each(tmp
, &tomoyo_query_list
) {
1688 struct tomoyo_query_entry
*ptr
=
1689 list_entry(tmp
, typeof(*ptr
), list
);
1692 if (pos
++ != head
->r
.query_index
)
1695 * Some query can be skipped because tomoyo_query_list
1696 * can change, but I don't care.
1698 if (len
== ptr
->query_len
)
1699 memmove(buf
, ptr
->query
, len
);
1702 spin_unlock(&tomoyo_query_list_lock
);
1704 head
->read_buf
= buf
;
1705 head
->r
.w
[head
->r
.w_pos
++] = buf
;
1706 head
->r
.query_index
++;
1713 * tomoyo_write_answer - Write the supervisor's decision.
1715 * @head: Pointer to "struct tomoyo_io_buffer".
1717 * Returns 0 on success, -EINVAL otherwise.
1719 static int tomoyo_write_answer(struct tomoyo_io_buffer
*head
)
1721 char *data
= head
->write_buf
;
1722 struct list_head
*tmp
;
1723 unsigned int serial
;
1724 unsigned int answer
;
1725 spin_lock(&tomoyo_query_list_lock
);
1726 list_for_each(tmp
, &tomoyo_query_list
) {
1727 struct tomoyo_query_entry
*ptr
1728 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1731 spin_unlock(&tomoyo_query_list_lock
);
1732 if (sscanf(data
, "A%u=%u", &serial
, &answer
) != 2)
1734 spin_lock(&tomoyo_query_list_lock
);
1735 list_for_each(tmp
, &tomoyo_query_list
) {
1736 struct tomoyo_query_entry
*ptr
1737 = list_entry(tmp
, struct tomoyo_query_entry
, list
);
1738 if (ptr
->serial
!= serial
)
1741 ptr
->answer
= answer
;
1744 spin_unlock(&tomoyo_query_list_lock
);
1749 * tomoyo_read_version: Get version.
1751 * @head: Pointer to "struct tomoyo_io_buffer".
1753 * Returns version information.
1755 static void tomoyo_read_version(struct tomoyo_io_buffer
*head
)
1758 tomoyo_io_printf(head
, "2.3.0-pre");
1764 * tomoyo_read_self_domain - Get the current process's domainname.
1766 * @head: Pointer to "struct tomoyo_io_buffer".
1768 * Returns the current process's domainname.
1770 static void tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
1774 * tomoyo_domain()->domainname != NULL
1775 * because every process belongs to a domain and
1776 * the domain's name cannot be NULL.
1778 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
1784 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1786 * @type: Type of interface.
1787 * @file: Pointer to "struct file".
1789 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1791 * Caller acquires tomoyo_read_lock().
1793 int tomoyo_open_control(const u8 type
, struct file
*file
)
1795 struct tomoyo_io_buffer
*head
= kzalloc(sizeof(*head
), GFP_NOFS
);
1799 mutex_init(&head
->io_sem
);
1802 case TOMOYO_DOMAINPOLICY
:
1803 /* /sys/kernel/security/tomoyo/domain_policy */
1804 head
->write
= tomoyo_write_domain_policy
;
1805 head
->read
= tomoyo_read_domain_policy
;
1807 case TOMOYO_EXCEPTIONPOLICY
:
1808 /* /sys/kernel/security/tomoyo/exception_policy */
1809 head
->write
= tomoyo_write_exception_policy
;
1810 head
->read
= tomoyo_read_exception_policy
;
1812 case TOMOYO_SELFDOMAIN
:
1813 /* /sys/kernel/security/tomoyo/self_domain */
1814 head
->read
= tomoyo_read_self_domain
;
1816 case TOMOYO_DOMAIN_STATUS
:
1817 /* /sys/kernel/security/tomoyo/.domain_status */
1818 head
->write
= tomoyo_write_domain_profile
;
1819 head
->read
= tomoyo_read_domain_profile
;
1821 case TOMOYO_PROCESS_STATUS
:
1822 /* /sys/kernel/security/tomoyo/.process_status */
1823 head
->write
= tomoyo_write_pid
;
1824 head
->read
= tomoyo_read_pid
;
1826 case TOMOYO_VERSION
:
1827 /* /sys/kernel/security/tomoyo/version */
1828 head
->read
= tomoyo_read_version
;
1829 head
->readbuf_size
= 128;
1831 case TOMOYO_MEMINFO
:
1832 /* /sys/kernel/security/tomoyo/meminfo */
1833 head
->write
= tomoyo_write_memory_quota
;
1834 head
->read
= tomoyo_read_memory_counter
;
1835 head
->readbuf_size
= 512;
1837 case TOMOYO_PROFILE
:
1838 /* /sys/kernel/security/tomoyo/profile */
1839 head
->write
= tomoyo_write_profile
;
1840 head
->read
= tomoyo_read_profile
;
1842 case TOMOYO_QUERY
: /* /sys/kernel/security/tomoyo/query */
1843 head
->poll
= tomoyo_poll_query
;
1844 head
->write
= tomoyo_write_answer
;
1845 head
->read
= tomoyo_read_query
;
1847 case TOMOYO_MANAGER
:
1848 /* /sys/kernel/security/tomoyo/manager */
1849 head
->write
= tomoyo_write_manager_policy
;
1850 head
->read
= tomoyo_read_manager_policy
;
1853 if (!(file
->f_mode
& FMODE_READ
)) {
1855 * No need to allocate read_buf since it is not opened
1860 } else if (!head
->poll
) {
1861 /* Don't allocate read_buf for poll() access. */
1862 if (!head
->readbuf_size
)
1863 head
->readbuf_size
= 4096 * 2;
1864 head
->read_buf
= kzalloc(head
->readbuf_size
, GFP_NOFS
);
1865 if (!head
->read_buf
) {
1870 if (!(file
->f_mode
& FMODE_WRITE
)) {
1872 * No need to allocate write_buf since it is not opened
1876 } else if (head
->write
) {
1877 head
->writebuf_size
= 4096 * 2;
1878 head
->write_buf
= kzalloc(head
->writebuf_size
, GFP_NOFS
);
1879 if (!head
->write_buf
) {
1880 kfree(head
->read_buf
);
1885 if (type
!= TOMOYO_QUERY
)
1886 head
->reader_idx
= tomoyo_read_lock();
1887 file
->private_data
= head
;
1889 * Call the handler now if the file is
1890 * /sys/kernel/security/tomoyo/self_domain
1891 * so that the user can use
1892 * cat < /sys/kernel/security/tomoyo/self_domain"
1893 * to know the current process's domainname.
1895 if (type
== TOMOYO_SELFDOMAIN
)
1896 tomoyo_read_control(file
, NULL
, 0);
1898 * If the file is /sys/kernel/security/tomoyo/query , increment the
1900 * The obserber counter is used by tomoyo_supervisor() to see if
1901 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1903 else if (type
== TOMOYO_QUERY
)
1904 atomic_inc(&tomoyo_query_observers
);
1909 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1911 * @file: Pointer to "struct file".
1912 * @wait: Pointer to "poll_table".
1914 * Waits for read readiness.
1915 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1917 int tomoyo_poll_control(struct file
*file
, poll_table
*wait
)
1919 struct tomoyo_io_buffer
*head
= file
->private_data
;
1922 return head
->poll(file
, wait
);
1926 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1928 * @file: Pointer to "struct file".
1929 * @buffer: Poiner to buffer to write to.
1930 * @buffer_len: Size of @buffer.
1932 * Returns bytes read on success, negative value otherwise.
1934 * Caller holds tomoyo_read_lock().
1936 int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
1937 const int buffer_len
)
1940 struct tomoyo_io_buffer
*head
= file
->private_data
;
1944 if (mutex_lock_interruptible(&head
->io_sem
))
1946 head
->read_user_buf
= buffer
;
1947 head
->read_user_buf_avail
= buffer_len
;
1948 if (tomoyo_flush(head
))
1949 /* Call the policy handler. */
1952 len
= head
->read_user_buf
- buffer
;
1953 mutex_unlock(&head
->io_sem
);
1958 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1960 * @file: Pointer to "struct file".
1961 * @buffer: Pointer to buffer to read from.
1962 * @buffer_len: Size of @buffer.
1964 * Returns @buffer_len on success, negative value otherwise.
1966 * Caller holds tomoyo_read_lock().
1968 int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
1969 const int buffer_len
)
1971 struct tomoyo_io_buffer
*head
= file
->private_data
;
1972 int error
= buffer_len
;
1973 int avail_len
= buffer_len
;
1974 char *cp0
= head
->write_buf
;
1978 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
1980 /* Don't allow updating policies by non manager programs. */
1981 if (head
->write
!= tomoyo_write_pid
&&
1982 head
->write
!= tomoyo_write_domain_policy
&&
1983 !tomoyo_policy_manager())
1985 if (mutex_lock_interruptible(&head
->io_sem
))
1987 /* Read a line and dispatch it to the policy handler. */
1988 while (avail_len
> 0) {
1990 if (head
->write_avail
>= head
->writebuf_size
- 1) {
1993 } else if (get_user(c
, buffer
)) {
1999 cp0
[head
->write_avail
++] = c
;
2002 cp0
[head
->write_avail
- 1] = '\0';
2003 head
->write_avail
= 0;
2004 tomoyo_normalize_line(cp0
);
2007 mutex_unlock(&head
->io_sem
);
2012 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2014 * @file: Pointer to "struct file".
2016 * Releases memory and returns 0.
2018 * Caller looses tomoyo_read_lock().
2020 int tomoyo_close_control(struct file
*file
)
2022 struct tomoyo_io_buffer
*head
= file
->private_data
;
2023 const bool is_write
= !!head
->write_buf
;
2026 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2029 if (head
->type
== TOMOYO_QUERY
)
2030 atomic_dec(&tomoyo_query_observers
);
2032 tomoyo_read_unlock(head
->reader_idx
);
2033 /* Release memory used for policy I/O. */
2034 kfree(head
->read_buf
);
2035 head
->read_buf
= NULL
;
2036 kfree(head
->write_buf
);
2037 head
->write_buf
= NULL
;
2040 file
->private_data
= NULL
;
2047 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
2049 void tomoyo_check_profile(void)
2051 struct tomoyo_domain_info
*domain
;
2052 const int idx
= tomoyo_read_lock();
2053 tomoyo_policy_loaded
= true;
2054 /* Check all profiles currently assigned to domains are defined. */
2055 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
2056 const u8 profile
= domain
->profile
;
2057 if (tomoyo_profile_ptr
[profile
])
2059 panic("Profile %u (used by '%s') not defined.\n",
2060 profile
, domain
->domainname
->name
);
2062 tomoyo_read_unlock(idx
);
2063 if (tomoyo_profile_version
!= 20090903)
2064 panic("Profile version %u is not supported.\n",
2065 tomoyo_profile_version
);
2066 printk(KERN_INFO
"TOMOYO: 2.3.0-pre 2010/06/03\n");
2067 printk(KERN_INFO
"Mandatory Access Control activated.\n");