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