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