aboutsummaryrefslogtreecommitdiffstats
path: root/security/tomoyo/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/tomoyo/common.c')
-rw-r--r--security/tomoyo/common.c1964
1 files changed, 1267 insertions, 697 deletions
diff --git a/security/tomoyo/common.c b/security/tomoyo/common.c
index a0d09e56874..2e43aec1c36 100644
--- a/security/tomoyo/common.c
+++ b/security/tomoyo/common.c
@@ -1,9 +1,7 @@
1/* 1/*
2 * security/tomoyo/common.c 2 * security/tomoyo/common.c
3 * 3 *
4 * Common functions for TOMOYO. 4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
5 *
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
7 */ 5 */
8 6
9#include <linux/uaccess.h> 7#include <linux/uaccess.h>
@@ -11,54 +9,131 @@
11#include <linux/security.h> 9#include <linux/security.h>
12#include "common.h" 10#include "common.h"
13 11
14static struct tomoyo_profile tomoyo_default_profile = { 12/* String table for operation mode. */
15 .learning = &tomoyo_default_profile.preference, 13const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
16 .permissive = &tomoyo_default_profile.preference, 14 [TOMOYO_CONFIG_DISABLED] = "disabled",
17 .enforcing = &tomoyo_default_profile.preference, 15 [TOMOYO_CONFIG_LEARNING] = "learning",
18 .preference.enforcing_verbose = true, 16 [TOMOYO_CONFIG_PERMISSIVE] = "permissive",
19 .preference.learning_max_entry = 2048, 17 [TOMOYO_CONFIG_ENFORCING] = "enforcing"
20 .preference.learning_verbose = false,
21 .preference.permissive_verbose = true
22}; 18};
23 19
24/* Profile version. Currently only 20090903 is defined. */ 20/* String table for /sys/kernel/security/tomoyo/profile */
25static unsigned int tomoyo_profile_version; 21const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
22 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
23 [TOMOYO_MAC_FILE_EXECUTE] = "execute",
24 [TOMOYO_MAC_FILE_OPEN] = "open",
25 [TOMOYO_MAC_FILE_CREATE] = "create",
26 [TOMOYO_MAC_FILE_UNLINK] = "unlink",
27 [TOMOYO_MAC_FILE_GETATTR] = "getattr",
28 [TOMOYO_MAC_FILE_MKDIR] = "mkdir",
29 [TOMOYO_MAC_FILE_RMDIR] = "rmdir",
30 [TOMOYO_MAC_FILE_MKFIFO] = "mkfifo",
31 [TOMOYO_MAC_FILE_MKSOCK] = "mksock",
32 [TOMOYO_MAC_FILE_TRUNCATE] = "truncate",
33 [TOMOYO_MAC_FILE_SYMLINK] = "symlink",
34 [TOMOYO_MAC_FILE_MKBLOCK] = "mkblock",
35 [TOMOYO_MAC_FILE_MKCHAR] = "mkchar",
36 [TOMOYO_MAC_FILE_LINK] = "link",
37 [TOMOYO_MAC_FILE_RENAME] = "rename",
38 [TOMOYO_MAC_FILE_CHMOD] = "chmod",
39 [TOMOYO_MAC_FILE_CHOWN] = "chown",
40 [TOMOYO_MAC_FILE_CHGRP] = "chgrp",
41 [TOMOYO_MAC_FILE_IOCTL] = "ioctl",
42 [TOMOYO_MAC_FILE_CHROOT] = "chroot",
43 [TOMOYO_MAC_FILE_MOUNT] = "mount",
44 [TOMOYO_MAC_FILE_UMOUNT] = "unmount",
45 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root",
46 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
47};
26 48
27/* Profile table. Memory is allocated as needed. */ 49/* String table for conditions. */
28static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES]; 50const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = {
51 [TOMOYO_TASK_UID] = "task.uid",
52 [TOMOYO_TASK_EUID] = "task.euid",
53 [TOMOYO_TASK_SUID] = "task.suid",
54 [TOMOYO_TASK_FSUID] = "task.fsuid",
55 [TOMOYO_TASK_GID] = "task.gid",
56 [TOMOYO_TASK_EGID] = "task.egid",
57 [TOMOYO_TASK_SGID] = "task.sgid",
58 [TOMOYO_TASK_FSGID] = "task.fsgid",
59 [TOMOYO_TASK_PID] = "task.pid",
60 [TOMOYO_TASK_PPID] = "task.ppid",
61 [TOMOYO_EXEC_ARGC] = "exec.argc",
62 [TOMOYO_EXEC_ENVC] = "exec.envc",
63 [TOMOYO_TYPE_IS_SOCKET] = "socket",
64 [TOMOYO_TYPE_IS_SYMLINK] = "symlink",
65 [TOMOYO_TYPE_IS_FILE] = "file",
66 [TOMOYO_TYPE_IS_BLOCK_DEV] = "block",
67 [TOMOYO_TYPE_IS_DIRECTORY] = "directory",
68 [TOMOYO_TYPE_IS_CHAR_DEV] = "char",
69 [TOMOYO_TYPE_IS_FIFO] = "fifo",
70 [TOMOYO_MODE_SETUID] = "setuid",
71 [TOMOYO_MODE_SETGID] = "setgid",
72 [TOMOYO_MODE_STICKY] = "sticky",
73 [TOMOYO_MODE_OWNER_READ] = "owner_read",
74 [TOMOYO_MODE_OWNER_WRITE] = "owner_write",
75 [TOMOYO_MODE_OWNER_EXECUTE] = "owner_execute",
76 [TOMOYO_MODE_GROUP_READ] = "group_read",
77 [TOMOYO_MODE_GROUP_WRITE] = "group_write",
78 [TOMOYO_MODE_GROUP_EXECUTE] = "group_execute",
79 [TOMOYO_MODE_OTHERS_READ] = "others_read",
80 [TOMOYO_MODE_OTHERS_WRITE] = "others_write",
81 [TOMOYO_MODE_OTHERS_EXECUTE] = "others_execute",
82 [TOMOYO_EXEC_REALPATH] = "exec.realpath",
83 [TOMOYO_SYMLINK_TARGET] = "symlink.target",
84 [TOMOYO_PATH1_UID] = "path1.uid",
85 [TOMOYO_PATH1_GID] = "path1.gid",
86 [TOMOYO_PATH1_INO] = "path1.ino",
87 [TOMOYO_PATH1_MAJOR] = "path1.major",
88 [TOMOYO_PATH1_MINOR] = "path1.minor",
89 [TOMOYO_PATH1_PERM] = "path1.perm",
90 [TOMOYO_PATH1_TYPE] = "path1.type",
91 [TOMOYO_PATH1_DEV_MAJOR] = "path1.dev_major",
92 [TOMOYO_PATH1_DEV_MINOR] = "path1.dev_minor",
93 [TOMOYO_PATH2_UID] = "path2.uid",
94 [TOMOYO_PATH2_GID] = "path2.gid",
95 [TOMOYO_PATH2_INO] = "path2.ino",
96 [TOMOYO_PATH2_MAJOR] = "path2.major",
97 [TOMOYO_PATH2_MINOR] = "path2.minor",
98 [TOMOYO_PATH2_PERM] = "path2.perm",
99 [TOMOYO_PATH2_TYPE] = "path2.type",
100 [TOMOYO_PATH2_DEV_MAJOR] = "path2.dev_major",
101 [TOMOYO_PATH2_DEV_MINOR] = "path2.dev_minor",
102 [TOMOYO_PATH1_PARENT_UID] = "path1.parent.uid",
103 [TOMOYO_PATH1_PARENT_GID] = "path1.parent.gid",
104 [TOMOYO_PATH1_PARENT_INO] = "path1.parent.ino",
105 [TOMOYO_PATH1_PARENT_PERM] = "path1.parent.perm",
106 [TOMOYO_PATH2_PARENT_UID] = "path2.parent.uid",
107 [TOMOYO_PATH2_PARENT_GID] = "path2.parent.gid",
108 [TOMOYO_PATH2_PARENT_INO] = "path2.parent.ino",
109 [TOMOYO_PATH2_PARENT_PERM] = "path2.parent.perm",
110};
29 111
30/* String table for functionality that takes 4 modes. */ 112/* String table for PREFERENCE keyword. */
31static const char *tomoyo_mode[4] = { 113static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
32 "disabled", "learning", "permissive", "enforcing" 114 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
115 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
33}; 116};
34 117
35/* String table for /sys/kernel/security/tomoyo/profile */ 118/* String table for path operation. */
36static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX 119const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
37 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = { 120 [TOMOYO_TYPE_EXECUTE] = "execute",
38 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute", 121 [TOMOYO_TYPE_READ] = "read",
39 [TOMOYO_MAC_FILE_OPEN] = "file::open", 122 [TOMOYO_TYPE_WRITE] = "write",
40 [TOMOYO_MAC_FILE_CREATE] = "file::create", 123 [TOMOYO_TYPE_APPEND] = "append",
41 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink", 124 [TOMOYO_TYPE_UNLINK] = "unlink",
42 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir", 125 [TOMOYO_TYPE_GETATTR] = "getattr",
43 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir", 126 [TOMOYO_TYPE_RMDIR] = "rmdir",
44 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo", 127 [TOMOYO_TYPE_TRUNCATE] = "truncate",
45 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock", 128 [TOMOYO_TYPE_SYMLINK] = "symlink",
46 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate", 129 [TOMOYO_TYPE_CHROOT] = "chroot",
47 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink", 130 [TOMOYO_TYPE_UMOUNT] = "unmount",
48 [TOMOYO_MAC_FILE_REWRITE] = "file::rewrite", 131};
49 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock", 132
50 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar", 133/* String table for categories. */
51 [TOMOYO_MAC_FILE_LINK] = "file::link", 134static const char * const tomoyo_category_keywords
52 [TOMOYO_MAC_FILE_RENAME] = "file::rename", 135[TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
53 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod", 136 [TOMOYO_MAC_CATEGORY_FILE] = "file",
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",
62}; 137};
63 138
64/* Permit policy management by non-root user? */ 139/* Permit policy management by non-root user? */
@@ -71,11 +146,20 @@ static bool tomoyo_manage_by_non_root;
71 * 146 *
72 * @value: Bool value. 147 * @value: Bool value.
73 */ 148 */
74static const char *tomoyo_yesno(const unsigned int value) 149const char *tomoyo_yesno(const unsigned int value)
75{ 150{
76 return value ? "yes" : "no"; 151 return value ? "yes" : "no";
77} 152}
78 153
154/**
155 * tomoyo_addprintf - strncat()-like-snprintf().
156 *
157 * @buffer: Buffer to write to. Must be '\0'-terminated.
158 * @len: Size of @buffer.
159 * @fmt: The printf()'s format string, followed by parameters.
160 *
161 * Returns nothing.
162 */
79static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...) 163static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
80{ 164{
81 va_list args; 165 va_list args;
@@ -96,7 +180,7 @@ static bool tomoyo_flush(struct tomoyo_io_buffer *head)
96{ 180{
97 while (head->r.w_pos) { 181 while (head->r.w_pos) {
98 const char *w = head->r.w[0]; 182 const char *w = head->r.w[0];
99 int len = strlen(w); 183 size_t len = strlen(w);
100 if (len) { 184 if (len) {
101 if (len > head->read_user_buf_avail) 185 if (len > head->read_user_buf_avail)
102 len = head->read_user_buf_avail; 186 len = head->read_user_buf_avail;
@@ -111,7 +195,7 @@ static bool tomoyo_flush(struct tomoyo_io_buffer *head)
111 head->r.w[0] = w; 195 head->r.w[0] = w;
112 if (*w) 196 if (*w)
113 return false; 197 return false;
114 /* Add '\0' for query. */ 198 /* Add '\0' for audit logs and query. */
115 if (head->poll) { 199 if (head->poll) {
116 if (!head->read_user_buf_avail || 200 if (!head->read_user_buf_avail ||
117 copy_to_user(head->read_user_buf, "", 1)) 201 copy_to_user(head->read_user_buf, "", 1))
@@ -155,8 +239,8 @@ static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
155void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...) 239void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
156{ 240{
157 va_list args; 241 va_list args;
158 int len; 242 size_t len;
159 int pos = head->r.avail; 243 size_t pos = head->r.avail;
160 int size = head->readbuf_size - pos; 244 int size = head->readbuf_size - pos;
161 if (size <= 0) 245 if (size <= 0)
162 return; 246 return;
@@ -171,11 +255,25 @@ void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
171 tomoyo_set_string(head, head->read_buf + pos); 255 tomoyo_set_string(head, head->read_buf + pos);
172} 256}
173 257
258/**
259 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
260 *
261 * @head: Pointer to "struct tomoyo_io_buffer".
262 *
263 * Returns nothing.
264 */
174static void tomoyo_set_space(struct tomoyo_io_buffer *head) 265static void tomoyo_set_space(struct tomoyo_io_buffer *head)
175{ 266{
176 tomoyo_set_string(head, " "); 267 tomoyo_set_string(head, " ");
177} 268}
178 269
270/**
271 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
272 *
273 * @head: Pointer to "struct tomoyo_io_buffer".
274 *
275 * Returns nothing.
276 */
179static bool tomoyo_set_lf(struct tomoyo_io_buffer *head) 277static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
180{ 278{
181 tomoyo_set_string(head, "\n"); 279 tomoyo_set_string(head, "\n");
@@ -183,6 +281,62 @@ static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
183} 281}
184 282
185/** 283/**
284 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
285 *
286 * @head: Pointer to "struct tomoyo_io_buffer".
287 *
288 * Returns nothing.
289 */
290static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
291{
292 tomoyo_set_string(head, "/");
293}
294
295/* List of namespaces. */
296LIST_HEAD(tomoyo_namespace_list);
297/* True if namespace other than tomoyo_kernel_namespace is defined. */
298static bool tomoyo_namespace_enabled;
299
300/**
301 * tomoyo_init_policy_namespace - Initialize namespace.
302 *
303 * @ns: Pointer to "struct tomoyo_policy_namespace".
304 *
305 * Returns nothing.
306 */
307void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
308{
309 unsigned int idx;
310 for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
311 INIT_LIST_HEAD(&ns->acl_group[idx]);
312 for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
313 INIT_LIST_HEAD(&ns->group_list[idx]);
314 for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
315 INIT_LIST_HEAD(&ns->policy_list[idx]);
316 ns->profile_version = 20100903;
317 tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
318 list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
319}
320
321/**
322 * tomoyo_print_namespace - Print namespace header.
323 *
324 * @head: Pointer to "struct tomoyo_io_buffer".
325 *
326 * Returns nothing.
327 */
328static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
329{
330 if (!tomoyo_namespace_enabled)
331 return;
332 tomoyo_set_string(head,
333 container_of(head->r.ns,
334 struct tomoyo_policy_namespace,
335 namespace_list)->name);
336 tomoyo_set_space(head);
337}
338
339/**
186 * tomoyo_print_name_union - Print a tomoyo_name_union. 340 * tomoyo_print_name_union - Print a tomoyo_name_union.
187 * 341 *
188 * @head: Pointer to "struct tomoyo_io_buffer". 342 * @head: Pointer to "struct tomoyo_io_buffer".
@@ -192,7 +346,7 @@ static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
192 const struct tomoyo_name_union *ptr) 346 const struct tomoyo_name_union *ptr)
193{ 347{
194 tomoyo_set_space(head); 348 tomoyo_set_space(head);
195 if (ptr->is_group) { 349 if (ptr->group) {
196 tomoyo_set_string(head, "@"); 350 tomoyo_set_string(head, "@");
197 tomoyo_set_string(head, ptr->group->group_name->name); 351 tomoyo_set_string(head, ptr->group->group_name->name);
198 } else { 352 } else {
@@ -201,24 +355,46 @@ static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
201} 355}
202 356
203/** 357/**
204 * tomoyo_print_number_union - Print a tomoyo_number_union. 358 * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
205 * 359 *
206 * @head: Pointer to "struct tomoyo_io_buffer". 360 * @head: Pointer to "struct tomoyo_io_buffer".
207 * @ptr: Pointer to "struct tomoyo_number_union". 361 * @ptr: Pointer to "struct tomoyo_name_union".
362 *
363 * Returns nothing.
208 */ 364 */
209static void tomoyo_print_number_union(struct tomoyo_io_buffer *head, 365static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
210 const struct tomoyo_number_union *ptr) 366 const struct tomoyo_name_union *ptr)
211{ 367{
212 tomoyo_set_space(head); 368 if (ptr->group) {
213 if (ptr->is_group) { 369 tomoyo_set_string(head, "@");
370 tomoyo_set_string(head, ptr->group->group_name->name);
371 } else {
372 tomoyo_set_string(head, "\"");
373 tomoyo_set_string(head, ptr->filename->name);
374 tomoyo_set_string(head, "\"");
375 }
376}
377
378/**
379 * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
380 *
381 * @head: Pointer to "struct tomoyo_io_buffer".
382 * @ptr: Pointer to "struct tomoyo_number_union".
383 *
384 * Returns nothing.
385 */
386static void tomoyo_print_number_union_nospace
387(struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
388{
389 if (ptr->group) {
214 tomoyo_set_string(head, "@"); 390 tomoyo_set_string(head, "@");
215 tomoyo_set_string(head, ptr->group->group_name->name); 391 tomoyo_set_string(head, ptr->group->group_name->name);
216 } else { 392 } else {
217 int i; 393 int i;
218 unsigned long min = ptr->values[0]; 394 unsigned long min = ptr->values[0];
219 const unsigned long max = ptr->values[1]; 395 const unsigned long max = ptr->values[1];
220 u8 min_type = ptr->min_type; 396 u8 min_type = ptr->value_type[0];
221 const u8 max_type = ptr->max_type; 397 const u8 max_type = ptr->value_type[1];
222 char buffer[128]; 398 char buffer[128];
223 buffer[0] = '\0'; 399 buffer[0] = '\0';
224 for (i = 0; i < 2; i++) { 400 for (i = 0; i < 2; i++) {
@@ -232,8 +408,8 @@ static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
232 "0%lo", min); 408 "0%lo", min);
233 break; 409 break;
234 default: 410 default:
235 tomoyo_addprintf(buffer, sizeof(buffer), 411 tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
236 "%lu", min); 412 min);
237 break; 413 break;
238 } 414 }
239 if (min == max && min_type == max_type) 415 if (min == max && min_type == max_type)
@@ -247,35 +423,53 @@ static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
247} 423}
248 424
249/** 425/**
426 * tomoyo_print_number_union - Print a tomoyo_number_union.
427 *
428 * @head: Pointer to "struct tomoyo_io_buffer".
429 * @ptr: Pointer to "struct tomoyo_number_union".
430 *
431 * Returns nothing.
432 */
433static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
434 const struct tomoyo_number_union *ptr)
435{
436 tomoyo_set_space(head);
437 tomoyo_print_number_union_nospace(head, ptr);
438}
439
440/**
250 * tomoyo_assign_profile - Create a new profile. 441 * tomoyo_assign_profile - Create a new profile.
251 * 442 *
443 * @ns: Pointer to "struct tomoyo_policy_namespace".
252 * @profile: Profile number to create. 444 * @profile: Profile number to create.
253 * 445 *
254 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise. 446 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
255 */ 447 */
256static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile) 448static struct tomoyo_profile *tomoyo_assign_profile
449(struct tomoyo_policy_namespace *ns, const unsigned int profile)
257{ 450{
258 struct tomoyo_profile *ptr; 451 struct tomoyo_profile *ptr;
259 struct tomoyo_profile *entry; 452 struct tomoyo_profile *entry;
260 if (profile >= TOMOYO_MAX_PROFILES) 453 if (profile >= TOMOYO_MAX_PROFILES)
261 return NULL; 454 return NULL;
262 ptr = tomoyo_profile_ptr[profile]; 455 ptr = ns->profile_ptr[profile];
263 if (ptr) 456 if (ptr)
264 return ptr; 457 return ptr;
265 entry = kzalloc(sizeof(*entry), GFP_NOFS); 458 entry = kzalloc(sizeof(*entry), GFP_NOFS);
266 if (mutex_lock_interruptible(&tomoyo_policy_lock)) 459 if (mutex_lock_interruptible(&tomoyo_policy_lock))
267 goto out; 460 goto out;
268 ptr = tomoyo_profile_ptr[profile]; 461 ptr = ns->profile_ptr[profile];
269 if (!ptr && tomoyo_memory_ok(entry)) { 462 if (!ptr && tomoyo_memory_ok(entry)) {
270 ptr = entry; 463 ptr = entry;
271 ptr->learning = &tomoyo_default_profile.preference; 464 ptr->default_config = TOMOYO_CONFIG_DISABLED |
272 ptr->permissive = &tomoyo_default_profile.preference; 465 TOMOYO_CONFIG_WANT_GRANT_LOG |
273 ptr->enforcing = &tomoyo_default_profile.preference; 466 TOMOYO_CONFIG_WANT_REJECT_LOG;
274 ptr->default_config = TOMOYO_CONFIG_DISABLED;
275 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT, 467 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
276 sizeof(ptr->config)); 468 sizeof(ptr->config));
469 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
470 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
277 mb(); /* Avoid out-of-order execution. */ 471 mb(); /* Avoid out-of-order execution. */
278 tomoyo_profile_ptr[profile] = ptr; 472 ns->profile_ptr[profile] = ptr;
279 entry = NULL; 473 entry = NULL;
280 } 474 }
281 mutex_unlock(&tomoyo_policy_lock); 475 mutex_unlock(&tomoyo_policy_lock);
@@ -287,19 +481,29 @@ static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
287/** 481/**
288 * tomoyo_profile - Find a profile. 482 * tomoyo_profile - Find a profile.
289 * 483 *
484 * @ns: Pointer to "struct tomoyo_policy_namespace".
290 * @profile: Profile number to find. 485 * @profile: Profile number to find.
291 * 486 *
292 * Returns pointer to "struct tomoyo_profile". 487 * Returns pointer to "struct tomoyo_profile".
293 */ 488 */
294struct tomoyo_profile *tomoyo_profile(const u8 profile) 489struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
490 const u8 profile)
295{ 491{
296 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile]; 492 static struct tomoyo_profile tomoyo_null_profile;
297 if (!tomoyo_policy_loaded) 493 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
298 return &tomoyo_default_profile; 494 if (!ptr)
299 BUG_ON(!ptr); 495 ptr = &tomoyo_null_profile;
300 return ptr; 496 return ptr;
301} 497}
302 498
499/**
500 * tomoyo_find_yesno - Find values for specified keyword.
501 *
502 * @string: String to check.
503 * @find: Name of keyword.
504 *
505 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
506 */
303static s8 tomoyo_find_yesno(const char *string, const char *find) 507static s8 tomoyo_find_yesno(const char *string, const char *find)
304{ 508{
305 const char *cp = strstr(string, find); 509 const char *cp = strstr(string, find);
@@ -313,18 +517,15 @@ static s8 tomoyo_find_yesno(const char *string, const char *find)
313 return -1; 517 return -1;
314} 518}
315 519
316static void tomoyo_set_bool(bool *b, const char *string, const char *find) 520/**
317{ 521 * tomoyo_set_uint - Set value for specified preference.
318 switch (tomoyo_find_yesno(string, find)) { 522 *
319 case 1: 523 * @i: Pointer to "unsigned int".
320 *b = true; 524 * @string: String to check.
321 break; 525 * @find: Name of keyword.
322 case 0: 526 *
323 *b = false; 527 * Returns nothing.
324 break; 528 */
325 }
326}
327
328static void tomoyo_set_uint(unsigned int *i, const char *string, 529static void tomoyo_set_uint(unsigned int *i, const char *string,
329 const char *find) 530 const char *find)
330{ 531{
@@ -333,51 +534,16 @@ static void tomoyo_set_uint(unsigned int *i, const char *string,
333 sscanf(cp + strlen(find), "=%u", i); 534 sscanf(cp + strlen(find), "=%u", i);
334} 535}
335 536
336static void tomoyo_set_pref(const char *name, const char *value, 537/**
337 const bool use_default, 538 * tomoyo_set_mode - Set mode for specified profile.
338 struct tomoyo_profile *profile) 539 *
339{ 540 * @name: Name of functionality.
340 struct tomoyo_preference **pref; 541 * @value: Mode for @name.
341 bool *verbose; 542 * @profile: Pointer to "struct tomoyo_profile".
342 if (!strcmp(name, "enforcing")) { 543 *
343 if (use_default) { 544 * Returns 0 on success, negative value otherwise.
344 pref = &profile->enforcing; 545 */
345 goto set_default;
346 }
347 profile->enforcing = &profile->preference;
348 verbose = &profile->preference.enforcing_verbose;
349 goto set_verbose;
350 }
351 if (!strcmp(name, "permissive")) {
352 if (use_default) {
353 pref = &profile->permissive;
354 goto set_default;
355 }
356 profile->permissive = &profile->preference;
357 verbose = &profile->preference.permissive_verbose;
358 goto set_verbose;
359 }
360 if (!strcmp(name, "learning")) {
361 if (use_default) {
362 pref = &profile->learning;
363 goto set_default;
364 }
365 profile->learning = &profile->preference;
366 tomoyo_set_uint(&profile->preference.learning_max_entry, value,
367 "max_entry");
368 verbose = &profile->preference.learning_verbose;
369 goto set_verbose;
370 }
371 return;
372 set_default:
373 *pref = &tomoyo_default_profile.preference;
374 return;
375 set_verbose:
376 tomoyo_set_bool(verbose, value, "verbose");
377}
378
379static int tomoyo_set_mode(char *name, const char *value, 546static int tomoyo_set_mode(char *name, const char *value,
380 const bool use_default,
381 struct tomoyo_profile *profile) 547 struct tomoyo_profile *profile)
382{ 548{
383 u8 i; 549 u8 i;
@@ -389,7 +555,17 @@ static int tomoyo_set_mode(char *name, const char *value,
389 config = 0; 555 config = 0;
390 for (i = 0; i < TOMOYO_MAX_MAC_INDEX 556 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
391 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) { 557 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
392 if (strcmp(name, tomoyo_mac_keywords[i])) 558 int len = 0;
559 if (i < TOMOYO_MAX_MAC_INDEX) {
560 const u8 c = tomoyo_index2category[i];
561 const char *category =
562 tomoyo_category_keywords[c];
563 len = strlen(category);
564 if (strncmp(name, category, len) ||
565 name[len++] != ':' || name[len++] != ':')
566 continue;
567 }
568 if (strcmp(name + len, tomoyo_mac_keywords[i]))
393 continue; 569 continue;
394 config = profile->config[i]; 570 config = profile->config[i];
395 break; 571 break;
@@ -399,7 +575,7 @@ static int tomoyo_set_mode(char *name, const char *value,
399 } else { 575 } else {
400 return -EINVAL; 576 return -EINVAL;
401 } 577 }
402 if (use_default) { 578 if (strstr(value, "use_default")) {
403 config = TOMOYO_CONFIG_USE_DEFAULT; 579 config = TOMOYO_CONFIG_USE_DEFAULT;
404 } else { 580 } else {
405 u8 mode; 581 u8 mode;
@@ -410,6 +586,24 @@ static int tomoyo_set_mode(char *name, const char *value,
410 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'. 586 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
411 */ 587 */
412 config = (config & ~7) | mode; 588 config = (config & ~7) | mode;
589 if (config != TOMOYO_CONFIG_USE_DEFAULT) {
590 switch (tomoyo_find_yesno(value, "grant_log")) {
591 case 1:
592 config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
593 break;
594 case 0:
595 config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
596 break;
597 }
598 switch (tomoyo_find_yesno(value, "reject_log")) {
599 case 1:
600 config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
601 break;
602 case 0:
603 config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
604 break;
605 }
606 }
413 } 607 }
414 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX) 608 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
415 profile->config[i] = config; 609 profile->config[i] = config;
@@ -429,34 +623,22 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
429{ 623{
430 char *data = head->write_buf; 624 char *data = head->write_buf;
431 unsigned int i; 625 unsigned int i;
432 bool use_default = false;
433 char *cp; 626 char *cp;
434 struct tomoyo_profile *profile; 627 struct tomoyo_profile *profile;
435 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1) 628 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
629 == 1)
436 return 0; 630 return 0;
437 i = simple_strtoul(data, &cp, 10); 631 i = simple_strtoul(data, &cp, 10);
438 if (data == cp) { 632 if (*cp != '-')
439 profile = &tomoyo_default_profile; 633 return -EINVAL;
440 } else { 634 data = cp + 1;
441 if (*cp != '-') 635 profile = tomoyo_assign_profile(head->w.ns, i);
442 return -EINVAL; 636 if (!profile)
443 data = cp + 1; 637 return -EINVAL;
444 profile = tomoyo_assign_profile(i);
445 if (!profile)
446 return -EINVAL;
447 }
448 cp = strchr(data, '='); 638 cp = strchr(data, '=');
449 if (!cp) 639 if (!cp)
450 return -EINVAL; 640 return -EINVAL;
451 *cp++ = '\0'; 641 *cp++ = '\0';
452 if (profile != &tomoyo_default_profile)
453 use_default = strstr(cp, "use_default") != NULL;
454 if (tomoyo_str_starts(&data, "PREFERENCE::")) {
455 tomoyo_set_pref(data, cp, use_default, profile);
456 return 0;
457 }
458 if (profile == &tomoyo_default_profile)
459 return -EINVAL;
460 if (!strcmp(data, "COMMENT")) { 642 if (!strcmp(data, "COMMENT")) {
461 static DEFINE_SPINLOCK(lock); 643 static DEFINE_SPINLOCK(lock);
462 const struct tomoyo_path_info *new_comment 644 const struct tomoyo_path_info *new_comment
@@ -471,94 +653,91 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
471 tomoyo_put_name(old_comment); 653 tomoyo_put_name(old_comment);
472 return 0; 654 return 0;
473 } 655 }
474 return tomoyo_set_mode(data, cp, use_default, profile); 656 if (!strcmp(data, "PREFERENCE")) {
475} 657 for (i = 0; i < TOMOYO_MAX_PREF; i++)
476 658 tomoyo_set_uint(&profile->pref[i], cp,
477static void tomoyo_print_preference(struct tomoyo_io_buffer *head, 659 tomoyo_pref_keywords[i]);
478 const int idx) 660 return 0;
479{
480 struct tomoyo_preference *pref = &tomoyo_default_profile.preference;
481 const struct tomoyo_profile *profile = idx >= 0 ?
482 tomoyo_profile_ptr[idx] : NULL;
483 char buffer[16] = "";
484 if (profile) {
485 buffer[sizeof(buffer) - 1] = '\0';
486 snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
487 }
488 if (profile) {
489 pref = profile->learning;
490 if (pref == &tomoyo_default_profile.preference)
491 goto skip1;
492 }
493 tomoyo_io_printf(head, "%sPREFERENCE::%s={ "
494 "verbose=%s max_entry=%u }\n",
495 buffer, "learning",
496 tomoyo_yesno(pref->learning_verbose),
497 pref->learning_max_entry);
498 skip1:
499 if (profile) {
500 pref = profile->permissive;
501 if (pref == &tomoyo_default_profile.preference)
502 goto skip2;
503 }
504 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
505 buffer, "permissive",
506 tomoyo_yesno(pref->permissive_verbose));
507 skip2:
508 if (profile) {
509 pref = profile->enforcing;
510 if (pref == &tomoyo_default_profile.preference)
511 return;
512 } 661 }
513 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n", 662 return tomoyo_set_mode(data, cp, profile);
514 buffer, "enforcing",
515 tomoyo_yesno(pref->enforcing_verbose));
516} 663}
517 664
665/**
666 * tomoyo_print_config - Print mode for specified functionality.
667 *
668 * @head: Pointer to "struct tomoyo_io_buffer".
669 * @config: Mode for that functionality.
670 *
671 * Returns nothing.
672 *
673 * Caller prints functionality's name.
674 */
518static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config) 675static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
519{ 676{
520 tomoyo_io_printf(head, "={ mode=%s }\n", tomoyo_mode[config & 3]); 677 tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
678 tomoyo_mode[config & 3],
679 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
680 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
521} 681}
522 682
523/** 683/**
524 * tomoyo_read_profile - Read profile table. 684 * tomoyo_read_profile - Read profile table.
525 * 685 *
526 * @head: Pointer to "struct tomoyo_io_buffer". 686 * @head: Pointer to "struct tomoyo_io_buffer".
687 *
688 * Returns nothing.
527 */ 689 */
528static void tomoyo_read_profile(struct tomoyo_io_buffer *head) 690static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
529{ 691{
530 u8 index; 692 u8 index;
693 struct tomoyo_policy_namespace *ns =
694 container_of(head->r.ns, typeof(*ns), namespace_list);
531 const struct tomoyo_profile *profile; 695 const struct tomoyo_profile *profile;
696 if (head->r.eof)
697 return;
532 next: 698 next:
533 index = head->r.index; 699 index = head->r.index;
534 profile = tomoyo_profile_ptr[index]; 700 profile = ns->profile_ptr[index];
535 switch (head->r.step) { 701 switch (head->r.step) {
536 case 0: 702 case 0:
537 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903"); 703 tomoyo_print_namespace(head);
538 tomoyo_print_preference(head, -1); 704 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
705 ns->profile_version);
539 head->r.step++; 706 head->r.step++;
540 break; 707 break;
541 case 1: 708 case 1:
542 for ( ; head->r.index < TOMOYO_MAX_PROFILES; 709 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
543 head->r.index++) 710 head->r.index++)
544 if (tomoyo_profile_ptr[head->r.index]) 711 if (ns->profile_ptr[head->r.index])
545 break; 712 break;
546 if (head->r.index == TOMOYO_MAX_PROFILES) 713 if (head->r.index == TOMOYO_MAX_PROFILES) {
714 head->r.eof = true;
547 return; 715 return;
716 }
548 head->r.step++; 717 head->r.step++;
549 break; 718 break;
550 case 2: 719 case 2:
551 { 720 {
721 u8 i;
552 const struct tomoyo_path_info *comment = 722 const struct tomoyo_path_info *comment =
553 profile->comment; 723 profile->comment;
724 tomoyo_print_namespace(head);
554 tomoyo_io_printf(head, "%u-COMMENT=", index); 725 tomoyo_io_printf(head, "%u-COMMENT=", index);
555 tomoyo_set_string(head, comment ? comment->name : ""); 726 tomoyo_set_string(head, comment ? comment->name : "");
556 tomoyo_set_lf(head); 727 tomoyo_set_lf(head);
728 tomoyo_print_namespace(head);
729 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
730 for (i = 0; i < TOMOYO_MAX_PREF; i++)
731 tomoyo_io_printf(head, "%s=%u ",
732 tomoyo_pref_keywords[i],
733 profile->pref[i]);
734 tomoyo_set_string(head, "}\n");
557 head->r.step++; 735 head->r.step++;
558 } 736 }
559 break; 737 break;
560 case 3: 738 case 3:
561 { 739 {
740 tomoyo_print_namespace(head);
562 tomoyo_io_printf(head, "%u-%s", index, "CONFIG"); 741 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
563 tomoyo_print_config(head, profile->default_config); 742 tomoyo_print_config(head, profile->default_config);
564 head->r.bit = 0; 743 head->r.bit = 0;
@@ -572,15 +751,22 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
572 const u8 config = profile->config[i]; 751 const u8 config = profile->config[i];
573 if (config == TOMOYO_CONFIG_USE_DEFAULT) 752 if (config == TOMOYO_CONFIG_USE_DEFAULT)
574 continue; 753 continue;
575 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::", 754 tomoyo_print_namespace(head);
576 tomoyo_mac_keywords[i]); 755 if (i < TOMOYO_MAX_MAC_INDEX)
756 tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
757 index,
758 tomoyo_category_keywords
759 [tomoyo_index2category[i]],
760 tomoyo_mac_keywords[i]);
761 else
762 tomoyo_io_printf(head, "%u-CONFIG::%s", index,
763 tomoyo_mac_keywords[i]);
577 tomoyo_print_config(head, config); 764 tomoyo_print_config(head, config);
578 head->r.bit++; 765 head->r.bit++;
579 break; 766 break;
580 } 767 }
581 if (head->r.bit == TOMOYO_MAX_MAC_INDEX 768 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
582 + TOMOYO_MAX_MAC_CATEGORY_INDEX) { 769 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
583 tomoyo_print_preference(head, index);
584 head->r.index++; 770 head->r.index++;
585 head->r.step = 1; 771 head->r.step = 1;
586 } 772 }
@@ -590,6 +776,14 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
590 goto next; 776 goto next;
591} 777}
592 778
779/**
780 * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
781 *
782 * @a: Pointer to "struct tomoyo_acl_head".
783 * @b: Pointer to "struct tomoyo_acl_head".
784 *
785 * Returns true if @a == @b, false otherwise.
786 */
593static bool tomoyo_same_manager(const struct tomoyo_acl_head *a, 787static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
594 const struct tomoyo_acl_head *b) 788 const struct tomoyo_acl_head *b)
595{ 789{
@@ -611,8 +805,13 @@ static int tomoyo_update_manager_entry(const char *manager,
611 const bool is_delete) 805 const bool is_delete)
612{ 806{
613 struct tomoyo_manager e = { }; 807 struct tomoyo_manager e = { };
614 int error; 808 struct tomoyo_acl_param param = {
615 809 /* .ns = &tomoyo_kernel_namespace, */
810 .is_delete = is_delete,
811 .list = &tomoyo_kernel_namespace.
812 policy_list[TOMOYO_ID_MANAGER],
813 };
814 int error = is_delete ? -ENOENT : -ENOMEM;
616 if (tomoyo_domain_def(manager)) { 815 if (tomoyo_domain_def(manager)) {
617 if (!tomoyo_correct_domain(manager)) 816 if (!tomoyo_correct_domain(manager))
618 return -EINVAL; 817 return -EINVAL;
@@ -622,12 +821,11 @@ static int tomoyo_update_manager_entry(const char *manager,
622 return -EINVAL; 821 return -EINVAL;
623 } 822 }
624 e.manager = tomoyo_get_name(manager); 823 e.manager = tomoyo_get_name(manager);
625 if (!e.manager) 824 if (e.manager) {
626 return -ENOMEM; 825 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
627 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete, 826 tomoyo_same_manager);
628 &tomoyo_policy_list[TOMOYO_ID_MANAGER], 827 tomoyo_put_name(e.manager);
629 tomoyo_same_manager); 828 }
630 tomoyo_put_name(e.manager);
631 return error; 829 return error;
632} 830}
633 831
@@ -643,13 +841,12 @@ static int tomoyo_update_manager_entry(const char *manager,
643static int tomoyo_write_manager(struct tomoyo_io_buffer *head) 841static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
644{ 842{
645 char *data = head->write_buf; 843 char *data = head->write_buf;
646 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
647 844
648 if (!strcmp(data, "manage_by_non_root")) { 845 if (!strcmp(data, "manage_by_non_root")) {
649 tomoyo_manage_by_non_root = !is_delete; 846 tomoyo_manage_by_non_root = !head->w.is_delete;
650 return 0; 847 return 0;
651 } 848 }
652 return tomoyo_update_manager_entry(data, is_delete); 849 return tomoyo_update_manager_entry(data, head->w.is_delete);
653} 850}
654 851
655/** 852/**
@@ -663,8 +860,8 @@ static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
663{ 860{
664 if (head->r.eof) 861 if (head->r.eof)
665 return; 862 return;
666 list_for_each_cookie(head->r.acl, 863 list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
667 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) { 864 policy_list[TOMOYO_ID_MANAGER]) {
668 struct tomoyo_manager *ptr = 865 struct tomoyo_manager *ptr =
669 list_entry(head->r.acl, typeof(*ptr), head.list); 866 list_entry(head->r.acl, typeof(*ptr), head.list);
670 if (ptr->head.is_deleted) 867 if (ptr->head.is_deleted)
@@ -697,8 +894,8 @@ static bool tomoyo_manager(void)
697 return true; 894 return true;
698 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid)) 895 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
699 return false; 896 return false;
700 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER], 897 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
701 head.list) { 898 policy_list[TOMOYO_ID_MANAGER], head.list) {
702 if (!ptr->head.is_deleted && ptr->is_domain 899 if (!ptr->head.is_deleted && ptr->is_domain
703 && !tomoyo_pathcmp(domainname, ptr->manager)) { 900 && !tomoyo_pathcmp(domainname, ptr->manager)) {
704 found = true; 901 found = true;
@@ -710,8 +907,8 @@ static bool tomoyo_manager(void)
710 exe = tomoyo_get_exe(); 907 exe = tomoyo_get_exe();
711 if (!exe) 908 if (!exe)
712 return false; 909 return false;
713 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER], 910 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
714 head.list) { 911 policy_list[TOMOYO_ID_MANAGER], head.list) {
715 if (!ptr->head.is_deleted && !ptr->is_domain 912 if (!ptr->head.is_deleted && !ptr->is_domain
716 && !strcmp(exe, ptr->manager->name)) { 913 && !strcmp(exe, ptr->manager->name)) {
717 found = true; 914 found = true;
@@ -732,7 +929,7 @@ static bool tomoyo_manager(void)
732} 929}
733 930
734/** 931/**
735 * tomoyo_select_one - Parse select command. 932 * tomoyo_select_domain - Parse select command.
736 * 933 *
737 * @head: Pointer to "struct tomoyo_io_buffer". 934 * @head: Pointer to "struct tomoyo_io_buffer".
738 * @data: String to parse. 935 * @data: String to parse.
@@ -741,16 +938,15 @@ static bool tomoyo_manager(void)
741 * 938 *
742 * Caller holds tomoyo_read_lock(). 939 * Caller holds tomoyo_read_lock().
743 */ 940 */
744static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data) 941static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
942 const char *data)
745{ 943{
746 unsigned int pid; 944 unsigned int pid;
747 struct tomoyo_domain_info *domain = NULL; 945 struct tomoyo_domain_info *domain = NULL;
748 bool global_pid = false; 946 bool global_pid = false;
749 947 if (strncmp(data, "select ", 7))
750 if (!strcmp(data, "allow_execute")) { 948 return false;
751 head->r.print_execute_only = true; 949 data += 7;
752 return true;
753 }
754 if (sscanf(data, "pid=%u", &pid) == 1 || 950 if (sscanf(data, "pid=%u", &pid) == 1 ||
755 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { 951 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
756 struct task_struct *p; 952 struct task_struct *p;
@@ -769,7 +965,7 @@ static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
769 domain = tomoyo_find_domain(data + 7); 965 domain = tomoyo_find_domain(data + 7);
770 } else 966 } else
771 return false; 967 return false;
772 head->write_var1 = domain; 968 head->w.domain = domain;
773 /* Accessing read_buf is safe because head->io_sem is held. */ 969 /* Accessing read_buf is safe because head->io_sem is held. */
774 if (!head->read_buf) 970 if (!head->read_buf)
775 return true; /* Do nothing if open(O_WRONLY). */ 971 return true; /* Do nothing if open(O_WRONLY). */
@@ -821,20 +1017,47 @@ static int tomoyo_delete_domain(char *domainname)
821/** 1017/**
822 * tomoyo_write_domain2 - Write domain policy. 1018 * tomoyo_write_domain2 - Write domain policy.
823 * 1019 *
824 * @head: Pointer to "struct tomoyo_io_buffer". 1020 * @ns: Pointer to "struct tomoyo_policy_namespace".
1021 * @list: Pointer to "struct list_head".
1022 * @data: Policy to be interpreted.
1023 * @is_delete: True if it is a delete request.
825 * 1024 *
826 * Returns 0 on success, negative value otherwise. 1025 * Returns 0 on success, negative value otherwise.
827 * 1026 *
828 * Caller holds tomoyo_read_lock(). 1027 * Caller holds tomoyo_read_lock().
829 */ 1028 */
830static int tomoyo_write_domain2(char *data, struct tomoyo_domain_info *domain, 1029static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1030 struct list_head *list, char *data,
831 const bool is_delete) 1031 const bool is_delete)
832{ 1032{
833 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT)) 1033 struct tomoyo_acl_param param = {
834 return tomoyo_write_mount(data, domain, is_delete); 1034 .ns = ns,
835 return tomoyo_write_file(data, domain, is_delete); 1035 .list = list,
1036 .data = data,
1037 .is_delete = is_delete,
1038 };
1039 static const struct {
1040 const char *keyword;
1041 int (*write) (struct tomoyo_acl_param *);
1042 } tomoyo_callback[1] = {
1043 { "file ", tomoyo_write_file },
1044 };
1045 u8 i;
1046 for (i = 0; i < 1; i++) {
1047 if (!tomoyo_str_starts(&param.data,
1048 tomoyo_callback[i].keyword))
1049 continue;
1050 return tomoyo_callback[i].write(&param);
1051 }
1052 return -EINVAL;
836} 1053}
837 1054
1055/* String table for domain flags. */
1056const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
1057 [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n",
1058 [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
1059};
1060
838/** 1061/**
839 * tomoyo_write_domain - Write domain policy. 1062 * tomoyo_write_domain - Write domain policy.
840 * 1063 *
@@ -847,69 +1070,198 @@ static int tomoyo_write_domain2(char *data, struct tomoyo_domain_info *domain,
847static int tomoyo_write_domain(struct tomoyo_io_buffer *head) 1070static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
848{ 1071{
849 char *data = head->write_buf; 1072 char *data = head->write_buf;
850 struct tomoyo_domain_info *domain = head->write_var1; 1073 struct tomoyo_policy_namespace *ns;
851 bool is_delete = false; 1074 struct tomoyo_domain_info *domain = head->w.domain;
852 bool is_select = false; 1075 const bool is_delete = head->w.is_delete;
1076 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
853 unsigned int profile; 1077 unsigned int profile;
854 1078 if (*data == '<') {
855 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
856 is_delete = true;
857 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
858 is_select = true;
859 if (is_select && tomoyo_select_one(head, data))
860 return 0;
861 /* Don't allow updating policies by non manager programs. */
862 if (!tomoyo_manager())
863 return -EPERM;
864 if (tomoyo_domain_def(data)) {
865 domain = NULL; 1079 domain = NULL;
866 if (is_delete) 1080 if (is_delete)
867 tomoyo_delete_domain(data); 1081 tomoyo_delete_domain(data);
868 else if (is_select) 1082 else if (is_select)
869 domain = tomoyo_find_domain(data); 1083 domain = tomoyo_find_domain(data);
870 else 1084 else
871 domain = tomoyo_assign_domain(data, 0); 1085 domain = tomoyo_assign_domain(data, false);
872 head->write_var1 = domain; 1086 head->w.domain = domain;
873 return 0; 1087 return 0;
874 } 1088 }
875 if (!domain) 1089 if (!domain)
876 return -EINVAL; 1090 return -EINVAL;
877 1091 ns = domain->ns;
878 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1 1092 if (sscanf(data, "use_profile %u", &profile) == 1
879 && profile < TOMOYO_MAX_PROFILES) { 1093 && profile < TOMOYO_MAX_PROFILES) {
880 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded) 1094 if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
881 domain->profile = (u8) profile; 1095 domain->profile = (u8) profile;
882 return 0; 1096 return 0;
883 } 1097 }
884 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) { 1098 if (sscanf(data, "use_group %u\n", &profile) == 1
885 domain->ignore_global_allow_read = !is_delete; 1099 && profile < TOMOYO_MAX_ACL_GROUPS) {
1100 if (!is_delete)
1101 domain->group = (u8) profile;
886 return 0; 1102 return 0;
887 } 1103 }
888 if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) { 1104 for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
889 domain->quota_warned = !is_delete; 1105 const char *cp = tomoyo_dif[profile];
890 return 0; 1106 if (strncmp(data, cp, strlen(cp) - 1))
891 } 1107 continue;
892 if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) { 1108 domain->flags[profile] = !is_delete;
893 domain->transition_failed = !is_delete;
894 return 0; 1109 return 0;
895 } 1110 }
896 return tomoyo_write_domain2(data, domain, is_delete); 1111 return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1112 is_delete);
897} 1113}
898 1114
899/** 1115/**
900 * tomoyo_fns - Find next set bit. 1116 * tomoyo_print_condition - Print condition part.
901 * 1117 *
902 * @perm: 8 bits value. 1118 * @head: Pointer to "struct tomoyo_io_buffer".
903 * @bit: First bit to find. 1119 * @cond: Pointer to "struct tomoyo_condition".
904 * 1120 *
905 * Returns next on-bit on success, 8 otherwise. 1121 * Returns true on success, false otherwise.
906 */ 1122 */
907static u8 tomoyo_fns(const u8 perm, u8 bit) 1123static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
1124 const struct tomoyo_condition *cond)
908{ 1125{
909 for ( ; bit < 8; bit++) 1126 switch (head->r.cond_step) {
910 if (perm & (1 << bit)) 1127 case 0:
1128 head->r.cond_index = 0;
1129 head->r.cond_step++;
1130 /* fall through */
1131 case 1:
1132 {
1133 const u16 condc = cond->condc;
1134 const struct tomoyo_condition_element *condp =
1135 (typeof(condp)) (cond + 1);
1136 const struct tomoyo_number_union *numbers_p =
1137 (typeof(numbers_p)) (condp + condc);
1138 const struct tomoyo_name_union *names_p =
1139 (typeof(names_p))
1140 (numbers_p + cond->numbers_count);
1141 const struct tomoyo_argv *argv =
1142 (typeof(argv)) (names_p + cond->names_count);
1143 const struct tomoyo_envp *envp =
1144 (typeof(envp)) (argv + cond->argc);
1145 u16 skip;
1146 for (skip = 0; skip < head->r.cond_index; skip++) {
1147 const u8 left = condp->left;
1148 const u8 right = condp->right;
1149 condp++;
1150 switch (left) {
1151 case TOMOYO_ARGV_ENTRY:
1152 argv++;
1153 continue;
1154 case TOMOYO_ENVP_ENTRY:
1155 envp++;
1156 continue;
1157 case TOMOYO_NUMBER_UNION:
1158 numbers_p++;
1159 break;
1160 }
1161 switch (right) {
1162 case TOMOYO_NAME_UNION:
1163 names_p++;
1164 break;
1165 case TOMOYO_NUMBER_UNION:
1166 numbers_p++;
1167 break;
1168 }
1169 }
1170 while (head->r.cond_index < condc) {
1171 const u8 match = condp->equals;
1172 const u8 left = condp->left;
1173 const u8 right = condp->right;
1174 if (!tomoyo_flush(head))
1175 return false;
1176 condp++;
1177 head->r.cond_index++;
1178 tomoyo_set_space(head);
1179 switch (left) {
1180 case TOMOYO_ARGV_ENTRY:
1181 tomoyo_io_printf(head,
1182 "exec.argv[%lu]%s=\"",
1183 argv->index, argv->
1184 is_not ? "!" : "");
1185 tomoyo_set_string(head,
1186 argv->value->name);
1187 tomoyo_set_string(head, "\"");
1188 argv++;
1189 continue;
1190 case TOMOYO_ENVP_ENTRY:
1191 tomoyo_set_string(head,
1192 "exec.envp[\"");
1193 tomoyo_set_string(head,
1194 envp->name->name);
1195 tomoyo_io_printf(head, "\"]%s=", envp->
1196 is_not ? "!" : "");
1197 if (envp->value) {
1198 tomoyo_set_string(head, "\"");
1199 tomoyo_set_string(head, envp->
1200 value->name);
1201 tomoyo_set_string(head, "\"");
1202 } else {
1203 tomoyo_set_string(head,
1204 "NULL");
1205 }
1206 envp++;
1207 continue;
1208 case TOMOYO_NUMBER_UNION:
1209 tomoyo_print_number_union_nospace
1210 (head, numbers_p++);
1211 break;
1212 default:
1213 tomoyo_set_string(head,
1214 tomoyo_condition_keyword[left]);
1215 break;
1216 }
1217 tomoyo_set_string(head, match ? "=" : "!=");
1218 switch (right) {
1219 case TOMOYO_NAME_UNION:
1220 tomoyo_print_name_union_quoted
1221 (head, names_p++);
1222 break;
1223 case TOMOYO_NUMBER_UNION:
1224 tomoyo_print_number_union_nospace
1225 (head, numbers_p++);
1226 break;
1227 default:
1228 tomoyo_set_string(head,
1229 tomoyo_condition_keyword[right]);
1230 break;
1231 }
1232 }
1233 }
1234 head->r.cond_step++;
1235 /* fall through */
1236 case 2:
1237 if (!tomoyo_flush(head))
911 break; 1238 break;
912 return bit; 1239 head->r.cond_step++;
1240 /* fall through */
1241 case 3:
1242 tomoyo_set_lf(head);
1243 return true;
1244 }
1245 return false;
1246}
1247
1248/**
1249 * tomoyo_set_group - Print "acl_group " header keyword and category name.
1250 *
1251 * @head: Pointer to "struct tomoyo_io_buffer".
1252 * @category: Category name.
1253 *
1254 * Returns nothing.
1255 */
1256static void tomoyo_set_group(struct tomoyo_io_buffer *head,
1257 const char *category)
1258{
1259 if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1260 tomoyo_print_namespace(head);
1261 tomoyo_io_printf(head, "acl_group %u ",
1262 head->r.acl_group_index);
1263 }
1264 tomoyo_set_string(head, category);
913} 1265}
914 1266
915/** 1267/**
@@ -924,63 +1276,96 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
924 struct tomoyo_acl_info *acl) 1276 struct tomoyo_acl_info *acl)
925{ 1277{
926 const u8 acl_type = acl->type; 1278 const u8 acl_type = acl->type;
1279 bool first = true;
927 u8 bit; 1280 u8 bit;
928 1281
1282 if (head->r.print_cond_part)
1283 goto print_cond_part;
929 if (acl->is_deleted) 1284 if (acl->is_deleted)
930 return true; 1285 return true;
931 next:
932 bit = head->r.bit;
933 if (!tomoyo_flush(head)) 1286 if (!tomoyo_flush(head))
934 return false; 1287 return false;
935 else if (acl_type == TOMOYO_TYPE_PATH_ACL) { 1288 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
936 struct tomoyo_path_acl *ptr = 1289 struct tomoyo_path_acl *ptr =
937 container_of(acl, typeof(*ptr), head); 1290 container_of(acl, typeof(*ptr), head);
938 const u16 perm = ptr->perm; 1291 const u16 perm = ptr->perm;
939 for ( ; bit < TOMOYO_MAX_PATH_OPERATION; bit++) { 1292 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
940 if (!(perm & (1 << bit))) 1293 if (!(perm & (1 << bit)))
941 continue; 1294 continue;
942 if (head->r.print_execute_only && 1295 if (head->r.print_transition_related_only &&
943 bit != TOMOYO_TYPE_EXECUTE) 1296 bit != TOMOYO_TYPE_EXECUTE)
944 continue; 1297 continue;
945 /* Print "read/write" instead of "read" and "write". */ 1298 if (first) {
946 if ((bit == TOMOYO_TYPE_READ || 1299 tomoyo_set_group(head, "file ");
947 bit == TOMOYO_TYPE_WRITE) 1300 first = false;
948 && (perm & (1 << TOMOYO_TYPE_READ_WRITE))) 1301 } else {
949 continue; 1302 tomoyo_set_slash(head);
950 break; 1303 }
1304 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
951 } 1305 }
952 if (bit >= TOMOYO_MAX_PATH_OPERATION) 1306 if (first)
953 goto done; 1307 return true;
954 tomoyo_io_printf(head, "allow_%s", tomoyo_path_keyword[bit]);
955 tomoyo_print_name_union(head, &ptr->name); 1308 tomoyo_print_name_union(head, &ptr->name);
956 } else if (head->r.print_execute_only) { 1309 } else if (head->r.print_transition_related_only) {
957 return true; 1310 return true;
958 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) { 1311 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
959 struct tomoyo_path2_acl *ptr = 1312 struct tomoyo_path2_acl *ptr =
960 container_of(acl, typeof(*ptr), head); 1313 container_of(acl, typeof(*ptr), head);
961 bit = tomoyo_fns(ptr->perm, bit); 1314 const u8 perm = ptr->perm;
962 if (bit >= TOMOYO_MAX_PATH2_OPERATION) 1315 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
963 goto done; 1316 if (!(perm & (1 << bit)))
964 tomoyo_io_printf(head, "allow_%s", tomoyo_path2_keyword[bit]); 1317 continue;
1318 if (first) {
1319 tomoyo_set_group(head, "file ");
1320 first = false;
1321 } else {
1322 tomoyo_set_slash(head);
1323 }
1324 tomoyo_set_string(head, tomoyo_mac_keywords
1325 [tomoyo_pp2mac[bit]]);
1326 }
1327 if (first)
1328 return true;
965 tomoyo_print_name_union(head, &ptr->name1); 1329 tomoyo_print_name_union(head, &ptr->name1);
966 tomoyo_print_name_union(head, &ptr->name2); 1330 tomoyo_print_name_union(head, &ptr->name2);
967 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) { 1331 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
968 struct tomoyo_path_number_acl *ptr = 1332 struct tomoyo_path_number_acl *ptr =
969 container_of(acl, typeof(*ptr), head); 1333 container_of(acl, typeof(*ptr), head);
970 bit = tomoyo_fns(ptr->perm, bit); 1334 const u8 perm = ptr->perm;
971 if (bit >= TOMOYO_MAX_PATH_NUMBER_OPERATION) 1335 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
972 goto done; 1336 if (!(perm & (1 << bit)))
973 tomoyo_io_printf(head, "allow_%s", 1337 continue;
974 tomoyo_path_number_keyword[bit]); 1338 if (first) {
1339 tomoyo_set_group(head, "file ");
1340 first = false;
1341 } else {
1342 tomoyo_set_slash(head);
1343 }
1344 tomoyo_set_string(head, tomoyo_mac_keywords
1345 [tomoyo_pn2mac[bit]]);
1346 }
1347 if (first)
1348 return true;
975 tomoyo_print_name_union(head, &ptr->name); 1349 tomoyo_print_name_union(head, &ptr->name);
976 tomoyo_print_number_union(head, &ptr->number); 1350 tomoyo_print_number_union(head, &ptr->number);
977 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) { 1351 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
978 struct tomoyo_mkdev_acl *ptr = 1352 struct tomoyo_mkdev_acl *ptr =
979 container_of(acl, typeof(*ptr), head); 1353 container_of(acl, typeof(*ptr), head);
980 bit = tomoyo_fns(ptr->perm, bit); 1354 const u8 perm = ptr->perm;
981 if (bit >= TOMOYO_MAX_MKDEV_OPERATION) 1355 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
982 goto done; 1356 if (!(perm & (1 << bit)))
983 tomoyo_io_printf(head, "allow_%s", tomoyo_mkdev_keyword[bit]); 1357 continue;
1358 if (first) {
1359 tomoyo_set_group(head, "file ");
1360 first = false;
1361 } else {
1362 tomoyo_set_slash(head);
1363 }
1364 tomoyo_set_string(head, tomoyo_mac_keywords
1365 [tomoyo_pnnn2mac[bit]]);
1366 }
1367 if (first)
1368 return true;
984 tomoyo_print_name_union(head, &ptr->name); 1369 tomoyo_print_name_union(head, &ptr->name);
985 tomoyo_print_number_union(head, &ptr->mode); 1370 tomoyo_print_number_union(head, &ptr->mode);
986 tomoyo_print_number_union(head, &ptr->major); 1371 tomoyo_print_number_union(head, &ptr->major);
@@ -988,35 +1373,41 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
988 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) { 1373 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
989 struct tomoyo_mount_acl *ptr = 1374 struct tomoyo_mount_acl *ptr =
990 container_of(acl, typeof(*ptr), head); 1375 container_of(acl, typeof(*ptr), head);
991 tomoyo_io_printf(head, "allow_mount"); 1376 tomoyo_set_group(head, "file mount");
992 tomoyo_print_name_union(head, &ptr->dev_name); 1377 tomoyo_print_name_union(head, &ptr->dev_name);
993 tomoyo_print_name_union(head, &ptr->dir_name); 1378 tomoyo_print_name_union(head, &ptr->dir_name);
994 tomoyo_print_name_union(head, &ptr->fs_type); 1379 tomoyo_print_name_union(head, &ptr->fs_type);
995 tomoyo_print_number_union(head, &ptr->flags); 1380 tomoyo_print_number_union(head, &ptr->flags);
996 } 1381 }
997 head->r.bit = bit + 1; 1382 if (acl->cond) {
998 tomoyo_io_printf(head, "\n"); 1383 head->r.print_cond_part = true;
999 if (acl_type != TOMOYO_TYPE_MOUNT_ACL) 1384 head->r.cond_step = 0;
1000 goto next; 1385 if (!tomoyo_flush(head))
1001 done: 1386 return false;
1002 head->r.bit = 0; 1387print_cond_part:
1388 if (!tomoyo_print_condition(head, acl->cond))
1389 return false;
1390 head->r.print_cond_part = false;
1391 } else {
1392 tomoyo_set_lf(head);
1393 }
1003 return true; 1394 return true;
1004} 1395}
1005 1396
1006/** 1397/**
1007 * tomoyo_read_domain2 - Read domain policy. 1398 * tomoyo_read_domain2 - Read domain policy.
1008 * 1399 *
1009 * @head: Pointer to "struct tomoyo_io_buffer". 1400 * @head: Pointer to "struct tomoyo_io_buffer".
1010 * @domain: Pointer to "struct tomoyo_domain_info". 1401 * @list: Pointer to "struct list_head".
1011 * 1402 *
1012 * Caller holds tomoyo_read_lock(). 1403 * Caller holds tomoyo_read_lock().
1013 * 1404 *
1014 * Returns true on success, false otherwise. 1405 * Returns true on success, false otherwise.
1015 */ 1406 */
1016static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head, 1407static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1017 struct tomoyo_domain_info *domain) 1408 struct list_head *list)
1018{ 1409{
1019 list_for_each_cookie(head->r.acl, &domain->acl_info_list) { 1410 list_for_each_cookie(head->r.acl, list) {
1020 struct tomoyo_acl_info *ptr = 1411 struct tomoyo_acl_info *ptr =
1021 list_entry(head->r.acl, typeof(*ptr), list); 1412 list_entry(head->r.acl, typeof(*ptr), list);
1022 if (!tomoyo_print_entry(head, ptr)) 1413 if (!tomoyo_print_entry(head, ptr))
@@ -1041,6 +1432,7 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
1041 struct tomoyo_domain_info *domain = 1432 struct tomoyo_domain_info *domain =
1042 list_entry(head->r.domain, typeof(*domain), list); 1433 list_entry(head->r.domain, typeof(*domain), list);
1043 switch (head->r.step) { 1434 switch (head->r.step) {
1435 u8 i;
1044 case 0: 1436 case 0:
1045 if (domain->is_deleted && 1437 if (domain->is_deleted &&
1046 !head->r.print_this_domain_only) 1438 !head->r.print_this_domain_only)
@@ -1048,22 +1440,18 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
1048 /* Print domainname and flags. */ 1440 /* Print domainname and flags. */
1049 tomoyo_set_string(head, domain->domainname->name); 1441 tomoyo_set_string(head, domain->domainname->name);
1050 tomoyo_set_lf(head); 1442 tomoyo_set_lf(head);
1051 tomoyo_io_printf(head, 1443 tomoyo_io_printf(head, "use_profile %u\n",
1052 TOMOYO_KEYWORD_USE_PROFILE "%u\n",
1053 domain->profile); 1444 domain->profile);
1054 if (domain->quota_warned) 1445 tomoyo_io_printf(head, "use_group %u\n",
1055 tomoyo_set_string(head, "quota_exceeded\n"); 1446 domain->group);
1056 if (domain->transition_failed) 1447 for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
1057 tomoyo_set_string(head, "transition_failed\n"); 1448 if (domain->flags[i])
1058 if (domain->ignore_global_allow_read) 1449 tomoyo_set_string(head, tomoyo_dif[i]);
1059 tomoyo_set_string(head,
1060 TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
1061 "\n");
1062 head->r.step++; 1450 head->r.step++;
1063 tomoyo_set_lf(head); 1451 tomoyo_set_lf(head);
1064 /* fall through */ 1452 /* fall through */
1065 case 1: 1453 case 1:
1066 if (!tomoyo_read_domain2(head, domain)) 1454 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
1067 return; 1455 return;
1068 head->r.step++; 1456 head->r.step++;
1069 if (!tomoyo_set_lf(head)) 1457 if (!tomoyo_set_lf(head))
@@ -1080,73 +1468,6 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
1080} 1468}
1081 1469
1082/** 1470/**
1083 * tomoyo_write_domain_profile - Assign profile for specified domain.
1084 *
1085 * @head: Pointer to "struct tomoyo_io_buffer".
1086 *
1087 * Returns 0 on success, -EINVAL otherwise.
1088 *
1089 * This is equivalent to doing
1090 *
1091 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1092 * /usr/sbin/tomoyo-loadpolicy -d
1093 *
1094 * Caller holds tomoyo_read_lock().
1095 */
1096static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1097{
1098 char *data = head->write_buf;
1099 char *cp = strchr(data, ' ');
1100 struct tomoyo_domain_info *domain;
1101 unsigned long profile;
1102
1103 if (!cp)
1104 return -EINVAL;
1105 *cp = '\0';
1106 domain = tomoyo_find_domain(cp + 1);
1107 if (strict_strtoul(data, 10, &profile))
1108 return -EINVAL;
1109 if (domain && profile < TOMOYO_MAX_PROFILES
1110 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1111 domain->profile = (u8) profile;
1112 return 0;
1113}
1114
1115/**
1116 * tomoyo_read_domain_profile - Read only domainname and profile.
1117 *
1118 * @head: Pointer to "struct tomoyo_io_buffer".
1119 *
1120 * Returns list of profile number and domainname pairs.
1121 *
1122 * This is equivalent to doing
1123 *
1124 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1125 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1126 * domainname = $0; } else if ( $1 == "use_profile" ) {
1127 * print $2 " " domainname; domainname = ""; } } ; '
1128 *
1129 * Caller holds tomoyo_read_lock().
1130 */
1131static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1132{
1133 if (head->r.eof)
1134 return;
1135 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
1136 struct tomoyo_domain_info *domain =
1137 list_entry(head->r.domain, typeof(*domain), list);
1138 if (domain->is_deleted)
1139 continue;
1140 if (!tomoyo_flush(head))
1141 return;
1142 tomoyo_io_printf(head, "%u ", domain->profile);
1143 tomoyo_set_string(head, domain->domainname->name);
1144 tomoyo_set_lf(head);
1145 }
1146 head->r.eof = true;
1147}
1148
1149/**
1150 * tomoyo_write_pid: Specify PID to obtain domainname. 1471 * tomoyo_write_pid: Specify PID to obtain domainname.
1151 * 1472 *
1152 * @head: Pointer to "struct tomoyo_io_buffer". 1473 * @head: Pointer to "struct tomoyo_io_buffer".
@@ -1204,18 +1525,20 @@ static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
1204 tomoyo_set_string(head, domain->domainname->name); 1525 tomoyo_set_string(head, domain->domainname->name);
1205} 1526}
1206 1527
1528/* String table for domain transition control keywords. */
1207static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = { 1529static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1208 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] 1530 [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
1209 = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN, 1531 [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
1210 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] 1532 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1211 = TOMOYO_KEYWORD_INITIALIZE_DOMAIN, 1533 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
1212 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN, 1534 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
1213 [TOMOYO_TRANSITION_CONTROL_KEEP] = TOMOYO_KEYWORD_KEEP_DOMAIN 1535 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
1214}; 1536};
1215 1537
1538/* String table for grouping keywords. */
1216static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = { 1539static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1217 [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP, 1540 [TOMOYO_PATH_GROUP] = "path_group ",
1218 [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP 1541 [TOMOYO_NUMBER_GROUP] = "number_group ",
1219}; 1542};
1220 1543
1221/** 1544/**
@@ -1229,29 +1552,30 @@ static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1229 */ 1552 */
1230static int tomoyo_write_exception(struct tomoyo_io_buffer *head) 1553static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
1231{ 1554{
1232 char *data = head->write_buf; 1555 const bool is_delete = head->w.is_delete;
1233 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE); 1556 struct tomoyo_acl_param param = {
1234 u8 i; 1557 .ns = head->w.ns,
1235 static const struct { 1558 .is_delete = is_delete,
1236 const char *keyword; 1559 .data = head->write_buf,
1237 int (*write) (char *, const bool);
1238 } tomoyo_callback[4] = {
1239 { TOMOYO_KEYWORD_AGGREGATOR, tomoyo_write_aggregator },
1240 { TOMOYO_KEYWORD_FILE_PATTERN, tomoyo_write_pattern },
1241 { TOMOYO_KEYWORD_DENY_REWRITE, tomoyo_write_no_rewrite },
1242 { TOMOYO_KEYWORD_ALLOW_READ, tomoyo_write_globally_readable },
1243 }; 1560 };
1244 1561 u8 i;
1562 if (tomoyo_str_starts(&param.data, "aggregator "))
1563 return tomoyo_write_aggregator(&param);
1245 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) 1564 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
1246 if (tomoyo_str_starts(&data, tomoyo_transition_type[i])) 1565 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1247 return tomoyo_write_transition_control(data, is_delete, 1566 return tomoyo_write_transition_control(&param, i);
1248 i);
1249 for (i = 0; i < 4; i++)
1250 if (tomoyo_str_starts(&data, tomoyo_callback[i].keyword))
1251 return tomoyo_callback[i].write(data, is_delete);
1252 for (i = 0; i < TOMOYO_MAX_GROUP; i++) 1567 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1253 if (tomoyo_str_starts(&data, tomoyo_group_name[i])) 1568 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1254 return tomoyo_write_group(data, is_delete, i); 1569 return tomoyo_write_group(&param, i);
1570 if (tomoyo_str_starts(&param.data, "acl_group ")) {
1571 unsigned int group;
1572 char *data;
1573 group = simple_strtoul(param.data, &data, 10);
1574 if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
1575 return tomoyo_write_domain2
1576 (head->w.ns, &head->w.ns->acl_group[group],
1577 data, is_delete);
1578 }
1255 return -EINVAL; 1579 return -EINVAL;
1256} 1580}
1257 1581
@@ -1267,9 +1591,12 @@ static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
1267 */ 1591 */
1268static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) 1592static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1269{ 1593{
1270 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) { 1594 struct tomoyo_policy_namespace *ns =
1595 container_of(head->r.ns, typeof(*ns), namespace_list);
1596 struct list_head *list = &ns->group_list[idx];
1597 list_for_each_cookie(head->r.group, list) {
1271 struct tomoyo_group *group = 1598 struct tomoyo_group *group =
1272 list_entry(head->r.group, typeof(*group), list); 1599 list_entry(head->r.group, typeof(*group), head.list);
1273 list_for_each_cookie(head->r.acl, &group->member_list) { 1600 list_for_each_cookie(head->r.acl, &group->member_list) {
1274 struct tomoyo_acl_head *ptr = 1601 struct tomoyo_acl_head *ptr =
1275 list_entry(head->r.acl, typeof(*ptr), list); 1602 list_entry(head->r.acl, typeof(*ptr), list);
@@ -1277,6 +1604,7 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1277 continue; 1604 continue;
1278 if (!tomoyo_flush(head)) 1605 if (!tomoyo_flush(head))
1279 return false; 1606 return false;
1607 tomoyo_print_namespace(head);
1280 tomoyo_set_string(head, tomoyo_group_name[idx]); 1608 tomoyo_set_string(head, tomoyo_group_name[idx]);
1281 tomoyo_set_string(head, group->group_name->name); 1609 tomoyo_set_string(head, group->group_name->name);
1282 if (idx == TOMOYO_PATH_GROUP) { 1610 if (idx == TOMOYO_PATH_GROUP) {
@@ -1310,7 +1638,10 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1310 */ 1638 */
1311static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) 1639static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1312{ 1640{
1313 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) { 1641 struct tomoyo_policy_namespace *ns =
1642 container_of(head->r.ns, typeof(*ns), namespace_list);
1643 struct list_head *list = &ns->policy_list[idx];
1644 list_for_each_cookie(head->r.acl, list) {
1314 struct tomoyo_acl_head *acl = 1645 struct tomoyo_acl_head *acl =
1315 container_of(head->r.acl, typeof(*acl), list); 1646 container_of(head->r.acl, typeof(*acl), list);
1316 if (acl->is_deleted) 1647 if (acl->is_deleted)
@@ -1322,35 +1653,23 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1322 { 1653 {
1323 struct tomoyo_transition_control *ptr = 1654 struct tomoyo_transition_control *ptr =
1324 container_of(acl, typeof(*ptr), head); 1655 container_of(acl, typeof(*ptr), head);
1325 tomoyo_set_string(head, 1656 tomoyo_print_namespace(head);
1326 tomoyo_transition_type 1657 tomoyo_set_string(head, tomoyo_transition_type
1327 [ptr->type]); 1658 [ptr->type]);
1328 if (ptr->program) 1659 tomoyo_set_string(head, ptr->program ?
1329 tomoyo_set_string(head, 1660 ptr->program->name : "any");
1330 ptr->program->name); 1661 tomoyo_set_string(head, " from ");
1331 if (ptr->program && ptr->domainname) 1662 tomoyo_set_string(head, ptr->domainname ?
1332 tomoyo_set_string(head, " from "); 1663 ptr->domainname->name :
1333 if (ptr->domainname) 1664 "any");
1334 tomoyo_set_string(head,
1335 ptr->domainname->
1336 name);
1337 }
1338 break;
1339 case TOMOYO_ID_GLOBALLY_READABLE:
1340 {
1341 struct tomoyo_readable_file *ptr =
1342 container_of(acl, typeof(*ptr), head);
1343 tomoyo_set_string(head,
1344 TOMOYO_KEYWORD_ALLOW_READ);
1345 tomoyo_set_string(head, ptr->filename->name);
1346 } 1665 }
1347 break; 1666 break;
1348 case TOMOYO_ID_AGGREGATOR: 1667 case TOMOYO_ID_AGGREGATOR:
1349 { 1668 {
1350 struct tomoyo_aggregator *ptr = 1669 struct tomoyo_aggregator *ptr =
1351 container_of(acl, typeof(*ptr), head); 1670 container_of(acl, typeof(*ptr), head);
1352 tomoyo_set_string(head, 1671 tomoyo_print_namespace(head);
1353 TOMOYO_KEYWORD_AGGREGATOR); 1672 tomoyo_set_string(head, "aggregator ");
1354 tomoyo_set_string(head, 1673 tomoyo_set_string(head,
1355 ptr->original_name->name); 1674 ptr->original_name->name);
1356 tomoyo_set_space(head); 1675 tomoyo_set_space(head);
@@ -1358,24 +1677,6 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1358 ptr->aggregated_name->name); 1677 ptr->aggregated_name->name);
1359 } 1678 }
1360 break; 1679 break;
1361 case TOMOYO_ID_PATTERN:
1362 {
1363 struct tomoyo_no_pattern *ptr =
1364 container_of(acl, typeof(*ptr), head);
1365 tomoyo_set_string(head,
1366 TOMOYO_KEYWORD_FILE_PATTERN);
1367 tomoyo_set_string(head, ptr->pattern->name);
1368 }
1369 break;
1370 case TOMOYO_ID_NO_REWRITE:
1371 {
1372 struct tomoyo_no_rewrite *ptr =
1373 container_of(acl, typeof(*ptr), head);
1374 tomoyo_set_string(head,
1375 TOMOYO_KEYWORD_DENY_REWRITE);
1376 tomoyo_set_string(head, ptr->pattern->name);
1377 }
1378 break;
1379 default: 1680 default:
1380 continue; 1681 continue;
1381 } 1682 }
@@ -1394,6 +1695,8 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1394 */ 1695 */
1395static void tomoyo_read_exception(struct tomoyo_io_buffer *head) 1696static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
1396{ 1697{
1698 struct tomoyo_policy_namespace *ns =
1699 container_of(head->r.ns, typeof(*ns), namespace_list);
1397 if (head->r.eof) 1700 if (head->r.eof)
1398 return; 1701 return;
1399 while (head->r.step < TOMOYO_MAX_POLICY && 1702 while (head->r.step < TOMOYO_MAX_POLICY &&
@@ -1406,95 +1709,40 @@ static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
1406 head->r.step++; 1709 head->r.step++;
1407 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP) 1710 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1408 return; 1711 return;
1712 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
1713 + TOMOYO_MAX_ACL_GROUPS) {
1714 head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
1715 - TOMOYO_MAX_GROUP;
1716 if (!tomoyo_read_domain2(head, &ns->acl_group
1717 [head->r.acl_group_index]))
1718 return;
1719 head->r.step++;
1720 }
1409 head->r.eof = true; 1721 head->r.eof = true;
1410} 1722}
1411 1723
1412/** 1724/* Wait queue for kernel -> userspace notification. */
1413 * tomoyo_print_header - Get header line of audit log.
1414 *
1415 * @r: Pointer to "struct tomoyo_request_info".
1416 *
1417 * Returns string representation.
1418 *
1419 * This function uses kmalloc(), so caller must kfree() if this function
1420 * didn't return NULL.
1421 */
1422static char *tomoyo_print_header(struct tomoyo_request_info *r)
1423{
1424 struct timeval tv;
1425 const pid_t gpid = task_pid_nr(current);
1426 static const int tomoyo_buffer_len = 4096;
1427 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1428 pid_t ppid;
1429 if (!buffer)
1430 return NULL;
1431 do_gettimeofday(&tv);
1432 rcu_read_lock();
1433 ppid = task_tgid_vnr(current->real_parent);
1434 rcu_read_unlock();
1435 snprintf(buffer, tomoyo_buffer_len - 1,
1436 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1437 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1438 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1439 tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
1440 task_tgid_vnr(current), ppid,
1441 current_uid(), current_gid(), current_euid(),
1442 current_egid(), current_suid(), current_sgid(),
1443 current_fsuid(), current_fsgid());
1444 return buffer;
1445}
1446
1447/**
1448 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1449 *
1450 * @len: Required size.
1451 * @r: Pointer to "struct tomoyo_request_info".
1452 *
1453 * Returns pointer to allocated memory.
1454 *
1455 * The @len is updated to add the header lines' size on success.
1456 *
1457 * This function uses kzalloc(), so caller must kfree() if this function
1458 * didn't return NULL.
1459 */
1460static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1461{
1462 char *buf = NULL;
1463 const char *header;
1464 const char *domainname;
1465 if (!r->domain)
1466 r->domain = tomoyo_domain();
1467 domainname = r->domain->domainname->name;
1468 header = tomoyo_print_header(r);
1469 if (!header)
1470 return NULL;
1471 *len += strlen(domainname) + strlen(header) + 10;
1472 buf = kzalloc(*len, GFP_NOFS);
1473 if (buf)
1474 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1475 kfree(header);
1476 return buf;
1477}
1478
1479/* Wait queue for tomoyo_query_list. */
1480static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait); 1725static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1481 1726/* Wait queue for userspace -> kernel notification. */
1482/* Lock for manipulating tomoyo_query_list. */ 1727static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
1483static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1484 1728
1485/* Structure for query. */ 1729/* Structure for query. */
1486struct tomoyo_query { 1730struct tomoyo_query {
1487 struct list_head list; 1731 struct list_head list;
1488 char *query; 1732 char *query;
1489 int query_len; 1733 size_t query_len;
1490 unsigned int serial; 1734 unsigned int serial;
1491 int timer; 1735 u8 timer;
1492 int answer; 1736 u8 answer;
1737 u8 retry;
1493}; 1738};
1494 1739
1495/* The list for "struct tomoyo_query". */ 1740/* The list for "struct tomoyo_query". */
1496static LIST_HEAD(tomoyo_query_list); 1741static LIST_HEAD(tomoyo_query_list);
1497 1742
1743/* Lock for manipulating tomoyo_query_list. */
1744static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1745
1498/* 1746/*
1499 * Number of "struct file" referring /sys/kernel/security/tomoyo/query 1747 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1500 * interface. 1748 * interface.
@@ -1502,10 +1750,82 @@ static LIST_HEAD(tomoyo_query_list);
1502static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); 1750static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1503 1751
1504/** 1752/**
1753 * tomoyo_truncate - Truncate a line.
1754 *
1755 * @str: String to truncate.
1756 *
1757 * Returns length of truncated @str.
1758 */
1759static int tomoyo_truncate(char *str)
1760{
1761 char *start = str;
1762 while (*(unsigned char *) str > (unsigned char) ' ')
1763 str++;
1764 *str = '\0';
1765 return strlen(start) + 1;
1766}
1767
1768/**
1769 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1770 *
1771 * @domain: Pointer to "struct tomoyo_domain_info".
1772 * @header: Lines containing ACL.
1773 *
1774 * Returns nothing.
1775 */
1776static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1777{
1778 char *buffer;
1779 char *realpath = NULL;
1780 char *argv0 = NULL;
1781 char *symlink = NULL;
1782 char *cp = strchr(header, '\n');
1783 int len;
1784 if (!cp)
1785 return;
1786 cp = strchr(cp + 1, '\n');
1787 if (!cp)
1788 return;
1789 *cp++ = '\0';
1790 len = strlen(cp) + 1;
1791 /* strstr() will return NULL if ordering is wrong. */
1792 if (*cp == 'f') {
1793 argv0 = strstr(header, " argv[]={ \"");
1794 if (argv0) {
1795 argv0 += 10;
1796 len += tomoyo_truncate(argv0) + 14;
1797 }
1798 realpath = strstr(header, " exec={ realpath=\"");
1799 if (realpath) {
1800 realpath += 8;
1801 len += tomoyo_truncate(realpath) + 6;
1802 }
1803 symlink = strstr(header, " symlink.target=\"");
1804 if (symlink)
1805 len += tomoyo_truncate(symlink + 1) + 1;
1806 }
1807 buffer = kmalloc(len, GFP_NOFS);
1808 if (!buffer)
1809 return;
1810 snprintf(buffer, len - 1, "%s", cp);
1811 if (realpath)
1812 tomoyo_addprintf(buffer, len, " exec.%s", realpath);
1813 if (argv0)
1814 tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
1815 if (symlink)
1816 tomoyo_addprintf(buffer, len, "%s", symlink);
1817 tomoyo_normalize_line(buffer);
1818 if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1819 false))
1820 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
1821 kfree(buffer);
1822}
1823
1824/**
1505 * tomoyo_supervisor - Ask for the supervisor's decision. 1825 * tomoyo_supervisor - Ask for the supervisor's decision.
1506 * 1826 *
1507 * @r: Pointer to "struct tomoyo_request_info". 1827 * @r: Pointer to "struct tomoyo_request_info".
1508 * @fmt: The printf()'s format string, followed by parameters. 1828 * @fmt: The printf()'s format string, followed by parameters.
1509 * 1829 *
1510 * Returns 0 if the supervisor decided to permit the access request which 1830 * Returns 0 if the supervisor decided to permit the access request which
1511 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the 1831 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
@@ -1515,88 +1835,79 @@ static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1515int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) 1835int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1516{ 1836{
1517 va_list args; 1837 va_list args;
1518 int error = -EPERM; 1838 int error;
1519 int pos;
1520 int len; 1839 int len;
1521 static unsigned int tomoyo_serial; 1840 static unsigned int tomoyo_serial;
1522 struct tomoyo_query *entry = NULL; 1841 struct tomoyo_query entry = { };
1523 bool quota_exceeded = false; 1842 bool quota_exceeded = false;
1524 char *header; 1843 va_start(args, fmt);
1844 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
1845 va_end(args);
1846 /* Write /sys/kernel/security/tomoyo/audit. */
1847 va_start(args, fmt);
1848 tomoyo_write_log2(r, len, fmt, args);
1849 va_end(args);
1850 /* Nothing more to do if granted. */
1851 if (r->granted)
1852 return 0;
1853 if (r->mode)
1854 tomoyo_update_stat(r->mode);
1525 switch (r->mode) { 1855 switch (r->mode) {
1526 char *buffer; 1856 case TOMOYO_CONFIG_ENFORCING:
1857 error = -EPERM;
1858 if (atomic_read(&tomoyo_query_observers))
1859 break;
1860 goto out;
1527 case TOMOYO_CONFIG_LEARNING: 1861 case TOMOYO_CONFIG_LEARNING:
1528 if (!tomoyo_domain_quota_is_ok(r)) 1862 error = 0;
1529 return 0; 1863 /* Check max_learning_entry parameter. */
1530 va_start(args, fmt); 1864 if (tomoyo_domain_quota_is_ok(r))
1531 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4; 1865 break;
1532 va_end(args);
1533 buffer = kmalloc(len, GFP_NOFS);
1534 if (!buffer)
1535 return 0;
1536 va_start(args, fmt);
1537 vsnprintf(buffer, len - 1, fmt, args);
1538 va_end(args);
1539 tomoyo_normalize_line(buffer);
1540 tomoyo_write_domain2(buffer, r->domain, false);
1541 kfree(buffer);
1542 /* fall through */ 1866 /* fall through */
1543 case TOMOYO_CONFIG_PERMISSIVE: 1867 default:
1544 return 0; 1868 return 0;
1545 } 1869 }
1546 if (!r->domain) 1870 /* Get message. */
1547 r->domain = tomoyo_domain();
1548 if (!atomic_read(&tomoyo_query_observers))
1549 return -EPERM;
1550 va_start(args, fmt); 1871 va_start(args, fmt);
1551 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32; 1872 entry.query = tomoyo_init_log(r, len, fmt, args);
1552 va_end(args); 1873 va_end(args);
1553 header = tomoyo_init_audit_log(&len, r); 1874 if (!entry.query)
1554 if (!header)
1555 goto out; 1875 goto out;
1556 entry = kzalloc(sizeof(*entry), GFP_NOFS); 1876 entry.query_len = strlen(entry.query) + 1;
1557 if (!entry) 1877 if (!error) {
1878 tomoyo_add_entry(r->domain, entry.query);
1558 goto out; 1879 goto out;
1559 entry->query = kzalloc(len, GFP_NOFS); 1880 }
1560 if (!entry->query) 1881 len = tomoyo_round2(entry.query_len);
1561 goto out;
1562 len = ksize(entry->query);
1563 spin_lock(&tomoyo_query_list_lock); 1882 spin_lock(&tomoyo_query_list_lock);
1564 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len + 1883 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
1565 sizeof(*entry) >= tomoyo_quota_for_query) { 1884 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
1885 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
1566 quota_exceeded = true; 1886 quota_exceeded = true;
1567 } else { 1887 } else {
1568 tomoyo_query_memory_size += len + sizeof(*entry); 1888 entry.serial = tomoyo_serial++;
1569 entry->serial = tomoyo_serial++; 1889 entry.retry = r->retry;
1890 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
1891 list_add_tail(&entry.list, &tomoyo_query_list);
1570 } 1892 }
1571 spin_unlock(&tomoyo_query_list_lock); 1893 spin_unlock(&tomoyo_query_list_lock);
1572 if (quota_exceeded) 1894 if (quota_exceeded)
1573 goto out; 1895 goto out;
1574 pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1575 entry->serial, r->retry, header);
1576 kfree(header);
1577 header = NULL;
1578 va_start(args, fmt);
1579 vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1580 entry->query_len = strlen(entry->query) + 1;
1581 va_end(args);
1582 spin_lock(&tomoyo_query_list_lock);
1583 list_add_tail(&entry->list, &tomoyo_query_list);
1584 spin_unlock(&tomoyo_query_list_lock);
1585 /* Give 10 seconds for supervisor's opinion. */ 1896 /* Give 10 seconds for supervisor's opinion. */
1586 for (entry->timer = 0; 1897 while (entry.timer < 10) {
1587 atomic_read(&tomoyo_query_observers) && entry->timer < 100; 1898 wake_up_all(&tomoyo_query_wait);
1588 entry->timer++) { 1899 if (wait_event_interruptible_timeout
1589 wake_up(&tomoyo_query_wait); 1900 (tomoyo_answer_wait, entry.answer ||
1590 set_current_state(TASK_INTERRUPTIBLE); 1901 !atomic_read(&tomoyo_query_observers), HZ))
1591 schedule_timeout(HZ / 10);
1592 if (entry->answer)
1593 break; 1902 break;
1903 else
1904 entry.timer++;
1594 } 1905 }
1595 spin_lock(&tomoyo_query_list_lock); 1906 spin_lock(&tomoyo_query_list_lock);
1596 list_del(&entry->list); 1907 list_del(&entry.list);
1597 tomoyo_query_memory_size -= len + sizeof(*entry); 1908 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
1598 spin_unlock(&tomoyo_query_list_lock); 1909 spin_unlock(&tomoyo_query_list_lock);
1599 switch (entry->answer) { 1910 switch (entry.answer) {
1600 case 3: /* Asked to retry by administrator. */ 1911 case 3: /* Asked to retry by administrator. */
1601 error = TOMOYO_RETRY_REQUEST; 1912 error = TOMOYO_RETRY_REQUEST;
1602 r->retry++; 1913 r->retry++;
@@ -1605,18 +1916,12 @@ int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1605 /* Granted by administrator. */ 1916 /* Granted by administrator. */
1606 error = 0; 1917 error = 0;
1607 break; 1918 break;
1608 case 0:
1609 /* Timed out. */
1610 break;
1611 default: 1919 default:
1612 /* Rejected by administrator. */ 1920 /* Timed out or rejected by administrator. */
1613 break; 1921 break;
1614 } 1922 }
1615 out: 1923out:
1616 if (entry) 1924 kfree(entry.query);
1617 kfree(entry->query);
1618 kfree(entry);
1619 kfree(header);
1620 return error; 1925 return error;
1621} 1926}
1622 1927
@@ -1663,8 +1968,8 @@ static int tomoyo_poll_query(struct file *file, poll_table *wait)
1663static void tomoyo_read_query(struct tomoyo_io_buffer *head) 1968static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1664{ 1969{
1665 struct list_head *tmp; 1970 struct list_head *tmp;
1666 int pos = 0; 1971 unsigned int pos = 0;
1667 int len = 0; 1972 size_t len = 0;
1668 char *buf; 1973 char *buf;
1669 if (head->r.w_pos) 1974 if (head->r.w_pos)
1670 return; 1975 return;
@@ -1687,7 +1992,7 @@ static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1687 head->r.query_index = 0; 1992 head->r.query_index = 0;
1688 return; 1993 return;
1689 } 1994 }
1690 buf = kzalloc(len, GFP_NOFS); 1995 buf = kzalloc(len + 32, GFP_NOFS);
1691 if (!buf) 1996 if (!buf)
1692 return; 1997 return;
1693 pos = 0; 1998 pos = 0;
@@ -1703,7 +2008,8 @@ static void tomoyo_read_query(struct tomoyo_io_buffer *head)
1703 * can change, but I don't care. 2008 * can change, but I don't care.
1704 */ 2009 */
1705 if (len == ptr->query_len) 2010 if (len == ptr->query_len)
1706 memmove(buf, ptr->query, len); 2011 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2012 ptr->retry, ptr->query);
1707 break; 2013 break;
1708 } 2014 }
1709 spin_unlock(&tomoyo_query_list_lock); 2015 spin_unlock(&tomoyo_query_list_lock);
@@ -1760,7 +2066,7 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1760static void tomoyo_read_version(struct tomoyo_io_buffer *head) 2066static void tomoyo_read_version(struct tomoyo_io_buffer *head)
1761{ 2067{
1762 if (!head->r.eof) { 2068 if (!head->r.eof) {
1763 tomoyo_io_printf(head, "2.3.0"); 2069 tomoyo_io_printf(head, "2.4.0");
1764 head->r.eof = true; 2070 head->r.eof = true;
1765 } 2071 }
1766} 2072}
@@ -1785,15 +2091,111 @@ static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1785 } 2091 }
1786} 2092}
1787 2093
2094/* String table for /sys/kernel/security/tomoyo/stat interface. */
2095static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2096 [TOMOYO_STAT_POLICY_UPDATES] = "update:",
2097 [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:",
2098 [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2099 [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
2100};
2101
2102/* String table for /sys/kernel/security/tomoyo/stat interface. */
2103static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2104 [TOMOYO_MEMORY_POLICY] = "policy:",
2105 [TOMOYO_MEMORY_AUDIT] = "audit log:",
2106 [TOMOYO_MEMORY_QUERY] = "query message:",
2107};
2108
2109/* Timestamp counter for last updated. */
2110static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2111/* Counter for number of updates. */
2112static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2113
2114/**
2115 * tomoyo_update_stat - Update statistic counters.
2116 *
2117 * @index: Index for policy type.
2118 *
2119 * Returns nothing.
2120 */
2121void tomoyo_update_stat(const u8 index)
2122{
2123 struct timeval tv;
2124 do_gettimeofday(&tv);
2125 /*
2126 * I don't use atomic operations because race condition is not fatal.
2127 */
2128 tomoyo_stat_updated[index]++;
2129 tomoyo_stat_modified[index] = tv.tv_sec;
2130}
2131
2132/**
2133 * tomoyo_read_stat - Read statistic data.
2134 *
2135 * @head: Pointer to "struct tomoyo_io_buffer".
2136 *
2137 * Returns nothing.
2138 */
2139static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2140{
2141 u8 i;
2142 unsigned int total = 0;
2143 if (head->r.eof)
2144 return;
2145 for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2146 tomoyo_io_printf(head, "Policy %-30s %10u",
2147 tomoyo_policy_headers[i],
2148 tomoyo_stat_updated[i]);
2149 if (tomoyo_stat_modified[i]) {
2150 struct tomoyo_time stamp;
2151 tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2152 tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2153 "%02u:%02u:%02u)",
2154 stamp.year, stamp.month, stamp.day,
2155 stamp.hour, stamp.min, stamp.sec);
2156 }
2157 tomoyo_set_lf(head);
2158 }
2159 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2160 unsigned int used = tomoyo_memory_used[i];
2161 total += used;
2162 tomoyo_io_printf(head, "Memory used by %-22s %10u",
2163 tomoyo_memory_headers[i], used);
2164 used = tomoyo_memory_quota[i];
2165 if (used)
2166 tomoyo_io_printf(head, " (Quota: %10u)", used);
2167 tomoyo_set_lf(head);
2168 }
2169 tomoyo_io_printf(head, "Total memory used: %10u\n",
2170 total);
2171 head->r.eof = true;
2172}
2173
2174/**
2175 * tomoyo_write_stat - Set memory quota.
2176 *
2177 * @head: Pointer to "struct tomoyo_io_buffer".
2178 *
2179 * Returns 0.
2180 */
2181static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2182{
2183 char *data = head->write_buf;
2184 u8 i;
2185 if (tomoyo_str_starts(&data, "Memory used by "))
2186 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2187 if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2188 sscanf(data, "%u", &tomoyo_memory_quota[i]);
2189 return 0;
2190}
2191
1788/** 2192/**
1789 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface. 2193 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1790 * 2194 *
1791 * @type: Type of interface. 2195 * @type: Type of interface.
1792 * @file: Pointer to "struct file". 2196 * @file: Pointer to "struct file".
1793 * 2197 *
1794 * Associates policy handler and returns 0 on success, -ENOMEM otherwise. 2198 * Returns 0 on success, negative value otherwise.
1795 *
1796 * Caller acquires tomoyo_read_lock().
1797 */ 2199 */
1798int tomoyo_open_control(const u8 type, struct file *file) 2200int tomoyo_open_control(const u8 type, struct file *file)
1799{ 2201{
@@ -1814,15 +2216,15 @@ int tomoyo_open_control(const u8 type, struct file *file)
1814 head->write = tomoyo_write_exception; 2216 head->write = tomoyo_write_exception;
1815 head->read = tomoyo_read_exception; 2217 head->read = tomoyo_read_exception;
1816 break; 2218 break;
2219 case TOMOYO_AUDIT:
2220 /* /sys/kernel/security/tomoyo/audit */
2221 head->poll = tomoyo_poll_log;
2222 head->read = tomoyo_read_log;
2223 break;
1817 case TOMOYO_SELFDOMAIN: 2224 case TOMOYO_SELFDOMAIN:
1818 /* /sys/kernel/security/tomoyo/self_domain */ 2225 /* /sys/kernel/security/tomoyo/self_domain */
1819 head->read = tomoyo_read_self_domain; 2226 head->read = tomoyo_read_self_domain;
1820 break; 2227 break;
1821 case TOMOYO_DOMAIN_STATUS:
1822 /* /sys/kernel/security/tomoyo/.domain_status */
1823 head->write = tomoyo_write_domain_profile;
1824 head->read = tomoyo_read_domain_profile;
1825 break;
1826 case TOMOYO_PROCESS_STATUS: 2228 case TOMOYO_PROCESS_STATUS:
1827 /* /sys/kernel/security/tomoyo/.process_status */ 2229 /* /sys/kernel/security/tomoyo/.process_status */
1828 head->write = tomoyo_write_pid; 2230 head->write = tomoyo_write_pid;
@@ -1833,11 +2235,11 @@ int tomoyo_open_control(const u8 type, struct file *file)
1833 head->read = tomoyo_read_version; 2235 head->read = tomoyo_read_version;
1834 head->readbuf_size = 128; 2236 head->readbuf_size = 128;
1835 break; 2237 break;
1836 case TOMOYO_MEMINFO: 2238 case TOMOYO_STAT:
1837 /* /sys/kernel/security/tomoyo/meminfo */ 2239 /* /sys/kernel/security/tomoyo/stat */
1838 head->write = tomoyo_write_memory_quota; 2240 head->write = tomoyo_write_stat;
1839 head->read = tomoyo_read_memory_counter; 2241 head->read = tomoyo_read_stat;
1840 head->readbuf_size = 512; 2242 head->readbuf_size = 1024;
1841 break; 2243 break;
1842 case TOMOYO_PROFILE: 2244 case TOMOYO_PROFILE:
1843 /* /sys/kernel/security/tomoyo/profile */ 2245 /* /sys/kernel/security/tomoyo/profile */
@@ -1887,26 +2289,16 @@ int tomoyo_open_control(const u8 type, struct file *file)
1887 return -ENOMEM; 2289 return -ENOMEM;
1888 } 2290 }
1889 } 2291 }
1890 if (type != TOMOYO_QUERY)
1891 head->reader_idx = tomoyo_read_lock();
1892 file->private_data = head;
1893 /*
1894 * Call the handler now if the file is
1895 * /sys/kernel/security/tomoyo/self_domain
1896 * so that the user can use
1897 * cat < /sys/kernel/security/tomoyo/self_domain"
1898 * to know the current process's domainname.
1899 */
1900 if (type == TOMOYO_SELFDOMAIN)
1901 tomoyo_read_control(file, NULL, 0);
1902 /* 2292 /*
1903 * If the file is /sys/kernel/security/tomoyo/query , increment the 2293 * If the file is /sys/kernel/security/tomoyo/query , increment the
1904 * observer counter. 2294 * observer counter.
1905 * The obserber counter is used by tomoyo_supervisor() to see if 2295 * The obserber counter is used by tomoyo_supervisor() to see if
1906 * there is some process monitoring /sys/kernel/security/tomoyo/query. 2296 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1907 */ 2297 */
1908 else if (type == TOMOYO_QUERY) 2298 if (type == TOMOYO_QUERY)
1909 atomic_inc(&tomoyo_query_observers); 2299 atomic_inc(&tomoyo_query_observers);
2300 file->private_data = head;
2301 tomoyo_notify_gc(head, true);
1910 return 0; 2302 return 0;
1911} 2303}
1912 2304
@@ -1917,7 +2309,8 @@ int tomoyo_open_control(const u8 type, struct file *file)
1917 * @wait: Pointer to "poll_table". 2309 * @wait: Pointer to "poll_table".
1918 * 2310 *
1919 * Waits for read readiness. 2311 * Waits for read readiness.
1920 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd . 2312 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2313 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
1921 */ 2314 */
1922int tomoyo_poll_control(struct file *file, poll_table *wait) 2315int tomoyo_poll_control(struct file *file, poll_table *wait)
1923{ 2316{
@@ -1928,21 +2321,58 @@ int tomoyo_poll_control(struct file *file, poll_table *wait)
1928} 2321}
1929 2322
1930/** 2323/**
2324 * tomoyo_set_namespace_cursor - Set namespace to read.
2325 *
2326 * @head: Pointer to "struct tomoyo_io_buffer".
2327 *
2328 * Returns nothing.
2329 */
2330static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2331{
2332 struct list_head *ns;
2333 if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2334 head->type != TOMOYO_PROFILE)
2335 return;
2336 /*
2337 * If this is the first read, or reading previous namespace finished
2338 * and has more namespaces to read, update the namespace cursor.
2339 */
2340 ns = head->r.ns;
2341 if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2342 /* Clearing is OK because tomoyo_flush() returned true. */
2343 memset(&head->r, 0, sizeof(head->r));
2344 head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2345 }
2346}
2347
2348/**
2349 * tomoyo_has_more_namespace - Check for unread namespaces.
2350 *
2351 * @head: Pointer to "struct tomoyo_io_buffer".
2352 *
2353 * Returns true if we have more entries to print, false otherwise.
2354 */
2355static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2356{
2357 return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2358 head->type == TOMOYO_PROFILE) && head->r.eof &&
2359 head->r.ns->next != &tomoyo_namespace_list;
2360}
2361
2362/**
1931 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface. 2363 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1932 * 2364 *
1933 * @file: Pointer to "struct file". 2365 * @head: Pointer to "struct tomoyo_io_buffer".
1934 * @buffer: Poiner to buffer to write to. 2366 * @buffer: Poiner to buffer to write to.
1935 * @buffer_len: Size of @buffer. 2367 * @buffer_len: Size of @buffer.
1936 * 2368 *
1937 * Returns bytes read on success, negative value otherwise. 2369 * Returns bytes read on success, negative value otherwise.
1938 *
1939 * Caller holds tomoyo_read_lock().
1940 */ 2370 */
1941int tomoyo_read_control(struct file *file, char __user *buffer, 2371ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
1942 const int buffer_len) 2372 const int buffer_len)
1943{ 2373{
1944 int len; 2374 int len;
1945 struct tomoyo_io_buffer *head = file->private_data; 2375 int idx;
1946 2376
1947 if (!head->read) 2377 if (!head->read)
1948 return -ENOSYS; 2378 return -ENOSYS;
@@ -1950,64 +2380,156 @@ int tomoyo_read_control(struct file *file, char __user *buffer,
1950 return -EINTR; 2380 return -EINTR;
1951 head->read_user_buf = buffer; 2381 head->read_user_buf = buffer;
1952 head->read_user_buf_avail = buffer_len; 2382 head->read_user_buf_avail = buffer_len;
2383 idx = tomoyo_read_lock();
1953 if (tomoyo_flush(head)) 2384 if (tomoyo_flush(head))
1954 /* Call the policy handler. */ 2385 /* Call the policy handler. */
1955 head->read(head); 2386 do {
1956 tomoyo_flush(head); 2387 tomoyo_set_namespace_cursor(head);
2388 head->read(head);
2389 } while (tomoyo_flush(head) &&
2390 tomoyo_has_more_namespace(head));
2391 tomoyo_read_unlock(idx);
1957 len = head->read_user_buf - buffer; 2392 len = head->read_user_buf - buffer;
1958 mutex_unlock(&head->io_sem); 2393 mutex_unlock(&head->io_sem);
1959 return len; 2394 return len;
1960} 2395}
1961 2396
1962/** 2397/**
2398 * tomoyo_parse_policy - Parse a policy line.
2399 *
2400 * @head: Poiter to "struct tomoyo_io_buffer".
2401 * @line: Line to parse.
2402 *
2403 * Returns 0 on success, negative value otherwise.
2404 *
2405 * Caller holds tomoyo_read_lock().
2406 */
2407static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2408{
2409 /* Delete request? */
2410 head->w.is_delete = !strncmp(line, "delete ", 7);
2411 if (head->w.is_delete)
2412 memmove(line, line + 7, strlen(line + 7) + 1);
2413 /* Selecting namespace to update. */
2414 if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2415 head->type == TOMOYO_PROFILE) {
2416 if (*line == '<') {
2417 char *cp = strchr(line, ' ');
2418 if (cp) {
2419 *cp++ = '\0';
2420 head->w.ns = tomoyo_assign_namespace(line);
2421 memmove(line, cp, strlen(cp) + 1);
2422 } else
2423 head->w.ns = NULL;
2424 } else
2425 head->w.ns = &tomoyo_kernel_namespace;
2426 /* Don't allow updating if namespace is invalid. */
2427 if (!head->w.ns)
2428 return -ENOENT;
2429 }
2430 /* Do the update. */
2431 return head->write(head);
2432}
2433
2434/**
1963 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface. 2435 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1964 * 2436 *
1965 * @file: Pointer to "struct file". 2437 * @head: Pointer to "struct tomoyo_io_buffer".
1966 * @buffer: Pointer to buffer to read from. 2438 * @buffer: Pointer to buffer to read from.
1967 * @buffer_len: Size of @buffer. 2439 * @buffer_len: Size of @buffer.
1968 * 2440 *
1969 * Returns @buffer_len on success, negative value otherwise. 2441 * Returns @buffer_len on success, negative value otherwise.
1970 *
1971 * Caller holds tomoyo_read_lock().
1972 */ 2442 */
1973int tomoyo_write_control(struct file *file, const char __user *buffer, 2443ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
1974 const int buffer_len) 2444 const char __user *buffer, const int buffer_len)
1975{ 2445{
1976 struct tomoyo_io_buffer *head = file->private_data;
1977 int error = buffer_len; 2446 int error = buffer_len;
1978 int avail_len = buffer_len; 2447 size_t avail_len = buffer_len;
1979 char *cp0 = head->write_buf; 2448 char *cp0 = head->write_buf;
1980 2449 int idx;
1981 if (!head->write) 2450 if (!head->write)
1982 return -ENOSYS; 2451 return -ENOSYS;
1983 if (!access_ok(VERIFY_READ, buffer, buffer_len)) 2452 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1984 return -EFAULT; 2453 return -EFAULT;
1985 /* Don't allow updating policies by non manager programs. */
1986 if (head->write != tomoyo_write_pid &&
1987 head->write != tomoyo_write_domain && !tomoyo_manager())
1988 return -EPERM;
1989 if (mutex_lock_interruptible(&head->io_sem)) 2454 if (mutex_lock_interruptible(&head->io_sem))
1990 return -EINTR; 2455 return -EINTR;
2456 idx = tomoyo_read_lock();
1991 /* Read a line and dispatch it to the policy handler. */ 2457 /* Read a line and dispatch it to the policy handler. */
1992 while (avail_len > 0) { 2458 while (avail_len > 0) {
1993 char c; 2459 char c;
1994 if (head->write_avail >= head->writebuf_size - 1) { 2460 if (head->w.avail >= head->writebuf_size - 1) {
1995 error = -ENOMEM; 2461 const int len = head->writebuf_size * 2;
1996 break; 2462 char *cp = kzalloc(len, GFP_NOFS);
1997 } else if (get_user(c, buffer)) { 2463 if (!cp) {
2464 error = -ENOMEM;
2465 break;
2466 }
2467 memmove(cp, cp0, head->w.avail);
2468 kfree(cp0);
2469 head->write_buf = cp;
2470 cp0 = cp;
2471 head->writebuf_size = len;
2472 }
2473 if (get_user(c, buffer)) {
1998 error = -EFAULT; 2474 error = -EFAULT;
1999 break; 2475 break;
2000 } 2476 }
2001 buffer++; 2477 buffer++;
2002 avail_len--; 2478 avail_len--;
2003 cp0[head->write_avail++] = c; 2479 cp0[head->w.avail++] = c;
2004 if (c != '\n') 2480 if (c != '\n')
2005 continue; 2481 continue;
2006 cp0[head->write_avail - 1] = '\0'; 2482 cp0[head->w.avail - 1] = '\0';
2007 head->write_avail = 0; 2483 head->w.avail = 0;
2008 tomoyo_normalize_line(cp0); 2484 tomoyo_normalize_line(cp0);
2009 head->write(head); 2485 if (!strcmp(cp0, "reset")) {
2486 head->w.ns = &tomoyo_kernel_namespace;
2487 head->w.domain = NULL;
2488 memset(&head->r, 0, sizeof(head->r));
2489 continue;
2490 }
2491 /* Don't allow updating policies by non manager programs. */
2492 switch (head->type) {
2493 case TOMOYO_PROCESS_STATUS:
2494 /* This does not write anything. */
2495 break;
2496 case TOMOYO_DOMAINPOLICY:
2497 if (tomoyo_select_domain(head, cp0))
2498 continue;
2499 /* fall through */
2500 case TOMOYO_EXCEPTIONPOLICY:
2501 if (!strcmp(cp0, "select transition_only")) {
2502 head->r.print_transition_related_only = true;
2503 continue;
2504 }
2505 /* fall through */
2506 default:
2507 if (!tomoyo_manager()) {
2508 error = -EPERM;
2509 goto out;
2510 }
2511 }
2512 switch (tomoyo_parse_policy(head, cp0)) {
2513 case -EPERM:
2514 error = -EPERM;
2515 goto out;
2516 case 0:
2517 switch (head->type) {
2518 case TOMOYO_DOMAINPOLICY:
2519 case TOMOYO_EXCEPTIONPOLICY:
2520 case TOMOYO_STAT:
2521 case TOMOYO_PROFILE:
2522 case TOMOYO_MANAGER:
2523 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2524 break;
2525 default:
2526 break;
2527 }
2528 break;
2529 }
2010 } 2530 }
2531out:
2532 tomoyo_read_unlock(idx);
2011 mutex_unlock(&head->io_sem); 2533 mutex_unlock(&head->io_sem);
2012 return error; 2534 return error;
2013} 2535}
@@ -2015,35 +2537,20 @@ int tomoyo_write_control(struct file *file, const char __user *buffer,
2015/** 2537/**
2016 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface. 2538 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2017 * 2539 *
2018 * @file: Pointer to "struct file". 2540 * @head: Pointer to "struct tomoyo_io_buffer".
2019 *
2020 * Releases memory and returns 0.
2021 * 2541 *
2022 * Caller looses tomoyo_read_lock(). 2542 * Returns 0.
2023 */ 2543 */
2024int tomoyo_close_control(struct file *file) 2544int tomoyo_close_control(struct tomoyo_io_buffer *head)
2025{ 2545{
2026 struct tomoyo_io_buffer *head = file->private_data;
2027 const bool is_write = !!head->write_buf;
2028
2029 /* 2546 /*
2030 * If the file is /sys/kernel/security/tomoyo/query , decrement the 2547 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2031 * observer counter. 2548 * observer counter.
2032 */ 2549 */
2033 if (head->type == TOMOYO_QUERY) 2550 if (head->type == TOMOYO_QUERY &&
2034 atomic_dec(&tomoyo_query_observers); 2551 atomic_dec_and_test(&tomoyo_query_observers))
2035 else 2552 wake_up_all(&tomoyo_answer_wait);
2036 tomoyo_read_unlock(head->reader_idx); 2553 tomoyo_notify_gc(head, false);
2037 /* Release memory used for policy I/O. */
2038 kfree(head->read_buf);
2039 head->read_buf = NULL;
2040 kfree(head->write_buf);
2041 head->write_buf = NULL;
2042 kfree(head);
2043 head = NULL;
2044 file->private_data = NULL;
2045 if (is_write)
2046 tomoyo_run_gc();
2047 return 0; 2554 return 0;
2048} 2555}
2049 2556
@@ -2055,27 +2562,90 @@ void tomoyo_check_profile(void)
2055 struct tomoyo_domain_info *domain; 2562 struct tomoyo_domain_info *domain;
2056 const int idx = tomoyo_read_lock(); 2563 const int idx = tomoyo_read_lock();
2057 tomoyo_policy_loaded = true; 2564 tomoyo_policy_loaded = true;
2058 /* Check all profiles currently assigned to domains are defined. */ 2565 printk(KERN_INFO "TOMOYO: 2.4.0\n");
2059 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { 2566 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2060 const u8 profile = domain->profile; 2567 const u8 profile = domain->profile;
2061 if (tomoyo_profile_ptr[profile]) 2568 const struct tomoyo_policy_namespace *ns = domain->ns;
2569 if (ns->profile_version != 20100903)
2570 printk(KERN_ERR
2571 "Profile version %u is not supported.\n",
2572 ns->profile_version);
2573 else if (!ns->profile_ptr[profile])
2574 printk(KERN_ERR
2575 "Profile %u (used by '%s') is not defined.\n",
2576 profile, domain->domainname->name);
2577 else
2062 continue; 2578 continue;
2063 printk(KERN_ERR "You need to define profile %u before using it.\n", 2579 printk(KERN_ERR
2064 profile); 2580 "Userland tools for TOMOYO 2.4 must be installed and "
2065 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ " 2581 "policy must be initialized.\n");
2582 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ "
2066 "for more information.\n"); 2583 "for more information.\n");
2067 panic("Profile %u (used by '%s') not defined.\n", 2584 panic("STOP!");
2068 profile, domain->domainname->name);
2069 } 2585 }
2070 tomoyo_read_unlock(idx); 2586 tomoyo_read_unlock(idx);
2071 if (tomoyo_profile_version != 20090903) {
2072 printk(KERN_ERR "You need to install userland programs for "
2073 "TOMOYO 2.3 and initialize policy configuration.\n");
2074 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2075 "for more information.\n");
2076 panic("Profile version %u is not supported.\n",
2077 tomoyo_profile_version);
2078 }
2079 printk(KERN_INFO "TOMOYO: 2.3.0\n");
2080 printk(KERN_INFO "Mandatory Access Control activated.\n"); 2587 printk(KERN_INFO "Mandatory Access Control activated.\n");
2081} 2588}
2589
2590/**
2591 * tomoyo_load_builtin_policy - Load built-in policy.
2592 *
2593 * Returns nothing.
2594 */
2595void __init tomoyo_load_builtin_policy(void)
2596{
2597 /*
2598 * This include file is manually created and contains built-in policy
2599 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2600 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2601 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2602 */
2603#include "builtin-policy.h"
2604 u8 i;
2605 const int idx = tomoyo_read_lock();
2606 for (i = 0; i < 5; i++) {
2607 struct tomoyo_io_buffer head = { };
2608 char *start = "";
2609 switch (i) {
2610 case 0:
2611 start = tomoyo_builtin_profile;
2612 head.type = TOMOYO_PROFILE;
2613 head.write = tomoyo_write_profile;
2614 break;
2615 case 1:
2616 start = tomoyo_builtin_exception_policy;
2617 head.type = TOMOYO_EXCEPTIONPOLICY;
2618 head.write = tomoyo_write_exception;
2619 break;
2620 case 2:
2621 start = tomoyo_builtin_domain_policy;
2622 head.type = TOMOYO_DOMAINPOLICY;
2623 head.write = tomoyo_write_domain;
2624 break;
2625 case 3:
2626 start = tomoyo_builtin_manager;
2627 head.type = TOMOYO_MANAGER;
2628 head.write = tomoyo_write_manager;
2629 break;
2630 case 4:
2631 start = tomoyo_builtin_stat;
2632 head.type = TOMOYO_STAT;
2633 head.write = tomoyo_write_stat;
2634 break;
2635 }
2636 while (1) {
2637 char *end = strchr(start, '\n');
2638 if (!end)
2639 break;
2640 *end = '\0';
2641 tomoyo_normalize_line(start);
2642 head.write_buf = start;
2643 tomoyo_parse_policy(&head, start);
2644 start = end + 1;
2645 }
2646 }
2647 tomoyo_read_unlock(idx);
2648#ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
2649 tomoyo_check_profile();
2650#endif
2651}