blob: 4f9047e94bd172b8d2b3eee771b2735074f13069 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.c
3 *
4 * Common functions for TOMOYO.
5 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takeda95908372009-02-05 17:18:13 +09007 */
8
9#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090011#include <linux/security.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090012#include "common.h"
Kentaro Takeda95908372009-02-05 17:18:13 +090013
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090014/* String table for operation mode. */
15const 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"
Kentaro Takeda95908372009-02-05 17:18:13 +090020};
Kentaro Takeda95908372009-02-05 17:18:13 +090021
Tetsuo Handa57c25902010-06-03 20:38:44 +090022/* String table for /sys/kernel/security/tomoyo/profile */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090023const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
Tetsuo Handa57c25902010-06-03 20:38:44 +090024 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090025 [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",
Tetsuo Handa57c25902010-06-03 20:38:44 +090048 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Kentaro Takeda95908372009-02-05 17:18:13 +090049};
50
Tetsuo Handa2066a362011-07-08 13:21:37 +090051/* String table for conditions. */
52const 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",
Tetsuo Handa5b636852011-07-08 13:24:54 +090063 [TOMOYO_EXEC_ARGC] = "exec.argc",
64 [TOMOYO_EXEC_ENVC] = "exec.envc",
Tetsuo Handa8761afd2011-07-08 13:22:41 +090065 [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",
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +090084 [TOMOYO_EXEC_REALPATH] = "exec.realpath",
85 [TOMOYO_SYMLINK_TARGET] = "symlink.target",
Tetsuo Handa8761afd2011-07-08 13:22:41 +090086 [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",
Tetsuo Handa2066a362011-07-08 13:21:37 +0900112};
113
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900114/* String table for PREFERENCE keyword. */
115static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900116 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900117 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
118};
119
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900120/* String table for path operation. */
121const 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. */
136static const char * const tomoyo_category_keywords
137[TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
138 [TOMOYO_MAC_CATEGORY_FILE] = "file",
139};
140
Kentaro Takeda95908372009-02-05 17:18:13 +0900141/* Permit policy management by non-root user? */
142static bool tomoyo_manage_by_non_root;
143
144/* Utility functions. */
145
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900146/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900147 * tomoyo_yesno - Return "yes" or "no".
148 *
149 * @value: Bool value.
150 */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900151const char *tomoyo_yesno(const unsigned int value)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900152{
153 return value ? "yes" : "no";
154}
155
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900156/**
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 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900165static 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 */
181static bool tomoyo_flush(struct tomoyo_io_buffer *head)
182{
183 while (head->r.w_pos) {
184 const char *w = head->r.w[0];
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900185 size_t len = strlen(w);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900186 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 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900197 head->r.w[0] = w;
198 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900199 return false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900200 /* Add '\0' for audit logs and query. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900201 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 */
226static 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 */
241void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
242{
243 va_list args;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900244 size_t len;
245 size_t pos = head->r.avail;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900246 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
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900260/**
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 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900267static void tomoyo_set_space(struct tomoyo_io_buffer *head)
268{
269 tomoyo_set_string(head, " ");
270}
271
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900272/**
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 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900279static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
280{
281 tomoyo_set_string(head, "\n");
282 return !head->r.w_pos;
283}
284
Tetsuo Handa57c25902010-06-03 20:38:44 +0900285/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900286 * 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 */
292static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
293{
294 tomoyo_set_string(head, "/");
295}
296
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900297/* List of namespaces. */
298LIST_HEAD(tomoyo_namespace_list);
299/* True if namespace other than tomoyo_kernel_namespace is defined. */
300static 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 */
309void 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 */
330static 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
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900341/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900342 * 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".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900346 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900347static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
348 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900349{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900350 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900351 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900352 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 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900357}
358
359/**
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +0900360 * 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 */
367static 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/**
Tetsuo Handa2066a362011-07-08 13:21:37 +0900381 * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900382 *
Tetsuo Handa2066a362011-07-08 13:21:37 +0900383 * @head: Pointer to "struct tomoyo_io_buffer".
384 * @ptr: Pointer to "struct tomoyo_number_union".
385 *
386 * Returns nothing.
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900387 */
Tetsuo Handa2066a362011-07-08 13:21:37 +0900388static void tomoyo_print_number_union_nospace
389(struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900390{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900391 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900392 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];
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900398 u8 min_type = ptr->value_type[0];
399 const u8 max_type = ptr->value_type[1];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900400 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:
Tetsuo Handa2066a362011-07-08 13:21:37 +0900413 tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
414 min);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900415 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);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900424 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900425}
426
427/**
Tetsuo Handa2066a362011-07-08 13:21:37 +0900428 * 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 */
435static 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/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900443 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900444 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900445 * @ns: Pointer to "struct tomoyo_policy_namespace".
Kentaro Takeda95908372009-02-05 17:18:13 +0900446 * @profile: Profile number to create.
447 *
448 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
449 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900450static struct tomoyo_profile *tomoyo_assign_profile
451(struct tomoyo_policy_namespace *ns, const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900452{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900453 struct tomoyo_profile *ptr;
454 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900455 if (profile >= TOMOYO_MAX_PROFILES)
456 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900457 ptr = ns->profile_ptr[profile];
Kentaro Takeda95908372009-02-05 17:18:13 +0900458 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900459 return ptr;
460 entry = kzalloc(sizeof(*entry), GFP_NOFS);
461 if (mutex_lock_interruptible(&tomoyo_policy_lock))
462 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900463 ptr = ns->profile_ptr[profile];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900464 if (!ptr && tomoyo_memory_ok(entry)) {
465 ptr = entry;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900466 ptr->default_config = TOMOYO_CONFIG_DISABLED |
467 TOMOYO_CONFIG_WANT_GRANT_LOG |
468 TOMOYO_CONFIG_WANT_REJECT_LOG;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900469 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
470 sizeof(ptr->config));
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900471 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900472 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900473 mb(); /* Avoid out-of-order execution. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900474 ns->profile_ptr[profile] = ptr;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900475 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900476 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900477 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900478 out:
479 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900480 return ptr;
481}
482
483/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900484 * tomoyo_profile - Find a profile.
485 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900486 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900487 * @profile: Profile number to find.
488 *
489 * Returns pointer to "struct tomoyo_profile".
490 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900491struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
492 const u8 profile)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900493{
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900494 static struct tomoyo_profile tomoyo_null_profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900495 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900496 if (!ptr)
497 ptr = &tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900498 return ptr;
499}
500
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900501/**
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 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900509static 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
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900522/**
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 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900531static 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
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900539/**
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 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900548static int tomoyo_set_mode(char *name, const char *value,
Tetsuo Handa8e568682010-06-25 09:30:09 +0900549 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++) {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900560 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]))
Tetsuo Handa8e568682010-06-25 09:30:09 +0900571 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 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900580 if (strstr(value, "use_default")) {
Tetsuo Handa8e568682010-06-25 09:30:09 +0900581 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;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900591 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 }
Tetsuo Handa8e568682010-06-25 09:30:09 +0900609 }
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
Tetsuo Handa57c25902010-06-03 20:38:44 +0900617/**
618 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900619 *
620 * @head: Pointer to "struct tomoyo_io_buffer".
621 *
622 * Returns 0 on success, negative value otherwise.
623 */
624static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
625{
626 char *data = head->write_buf;
627 unsigned int i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900628 char *cp;
629 struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900630 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
631 == 1)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900632 return 0;
633 i = simple_strtoul(data, &cp, 10);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900634 if (*cp != '-')
635 return -EINVAL;
636 data = cp + 1;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900637 profile = tomoyo_assign_profile(head->w.ns, i);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900638 if (!profile)
639 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900640 cp = strchr(data, '=');
641 if (!cp)
642 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900643 *cp++ = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +0900644 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900645 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);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900655 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900656 return 0;
657 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900658 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;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900663 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900664 return tomoyo_set_mode(data, cp, profile);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900665}
666
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900667/**
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 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900677static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
678{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900679 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));
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900683}
684
Kentaro Takeda95908372009-02-05 17:18:13 +0900685/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900686 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900687 *
688 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900689 *
690 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900691 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900692static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900693{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900694 u8 index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900695 struct tomoyo_policy_namespace *ns =
696 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900697 const struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900698 if (head->r.eof)
699 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900700 next:
701 index = head->r.index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900702 profile = ns->profile_ptr[index];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900703 switch (head->r.step) {
704 case 0:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900705 tomoyo_print_namespace(head);
706 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
707 ns->profile_version);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900708 head->r.step++;
709 break;
710 case 1:
711 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
712 head->r.index++)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900713 if (ns->profile_ptr[head->r.index])
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900714 break;
715 if (head->r.index == TOMOYO_MAX_PROFILES)
716 return;
717 head->r.step++;
718 break;
719 case 2:
720 {
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900721 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900722 const struct tomoyo_path_info *comment =
723 profile->comment;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900724 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900725 tomoyo_io_printf(head, "%u-COMMENT=", index);
726 tomoyo_set_string(head, comment ? comment->name : "");
727 tomoyo_set_lf(head);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900728 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");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900734 head->r.step++;
735 }
736 break;
737 case 3:
738 {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900739 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900740 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];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900751 if (config == TOMOYO_CONFIG_USE_DEFAULT)
752 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900753 tomoyo_print_namespace(head);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900754 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]);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900763 tomoyo_print_config(head, config);
764 head->r.bit++;
765 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900766 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900767 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
768 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900769 head->r.index++;
770 head->r.step = 1;
771 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900772 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900773 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900774 if (tomoyo_flush(head))
775 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900776}
777
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900778static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
779 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900780{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900781 return container_of(a, struct tomoyo_manager, head)->manager ==
782 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900783}
784
Kentaro Takeda95908372009-02-05 17:18:13 +0900785/**
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.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900792 *
793 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900794 */
795static int tomoyo_update_manager_entry(const char *manager,
796 const bool is_delete)
797{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900798 struct tomoyo_manager e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900799 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900800 /* .ns = &tomoyo_kernel_namespace, */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900801 .is_delete = is_delete,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900802 .list = &tomoyo_kernel_namespace.
803 policy_list[TOMOYO_ID_MANAGER],
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900804 };
805 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900806 if (tomoyo_domain_def(manager)) {
807 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900808 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900809 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900810 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900811 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900812 return -EINVAL;
813 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900814 e.manager = tomoyo_get_name(manager);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900815 if (e.manager) {
816 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
817 tomoyo_same_manager);
818 tomoyo_put_name(e.manager);
819 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900820 return error;
821}
822
823/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900824 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900825 *
826 * @head: Pointer to "struct tomoyo_io_buffer".
827 *
828 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900829 *
830 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900831 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900832static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900833{
834 char *data = head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900835
836 if (!strcmp(data, "manage_by_non_root")) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900837 tomoyo_manage_by_non_root = !head->w.is_delete;
Kentaro Takeda95908372009-02-05 17:18:13 +0900838 return 0;
839 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900840 return tomoyo_update_manager_entry(data, head->w.is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900841}
842
843/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900844 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900845 *
846 * @head: Pointer to "struct tomoyo_io_buffer".
847 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900848 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900849 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900850static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900851{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900852 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900853 return;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900854 list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
855 policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900856 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900857 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900858 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900859 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900860 if (!tomoyo_flush(head))
861 return;
862 tomoyo_set_string(head, ptr->manager->name);
863 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900864 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900865 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900866}
867
868/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900869 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900870 *
871 * Returns true if the current process is permitted to modify policy
872 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900873 *
874 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900875 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900876static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900877{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900878 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900879 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;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900888 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
889 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900890 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900891 && !tomoyo_pathcmp(domainname, ptr->manager)) {
892 found = true;
893 break;
894 }
895 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900896 if (found)
897 return true;
898 exe = tomoyo_get_exe();
899 if (!exe)
900 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900901 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
902 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900903 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900904 && !strcmp(exe, ptr->manager->name)) {
905 found = true;
906 break;
907 }
908 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900909 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 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900918 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900919 return found;
920}
921
922/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900923 * tomoyo_select_domain - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900924 *
925 * @head: Pointer to "struct tomoyo_io_buffer".
926 * @data: String to parse.
927 *
928 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900929 *
930 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900931 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900932static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
933 const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900934{
935 unsigned int pid;
936 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900937 bool global_pid = false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900938 if (strncmp(data, "select ", 7))
939 return false;
940 data += 7;
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900941 if (sscanf(data, "pid=%u", &pid) == 1 ||
942 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900943 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900944 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900945 read_lock(&tasklist_lock);
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900946 if (global_pid)
947 p = find_task_by_pid_ns(pid, &init_pid_ns);
948 else
949 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900950 if (p)
951 domain = tomoyo_real_domain(p);
952 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900953 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900954 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900955 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900956 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900957 } else
958 return false;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900959 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900960 /* 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). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900963 memset(&head->r, 0, sizeof(head->r));
964 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +0200965 if (domain)
966 head->r.domain = &domain->list;
967 else
968 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +0900969 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900970 if (domain && domain->is_deleted)
971 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900972 return true;
973}
974
975/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900976 * tomoyo_delete_domain - Delete a domain.
977 *
978 * @domainname: The name of domain.
979 *
980 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900981 *
982 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900983 */
984static 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);
Tetsuo Handa29282382010-05-06 00:18:15 +0900991 if (mutex_lock_interruptible(&tomoyo_policy_lock))
992 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900993 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900994 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900995 /* 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 }
Tetsuo Handaf737d952010-01-03 21:16:32 +09001004 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001005 return 0;
1006}
1007
1008/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001009 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001010 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001011 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001012 * @list: Pointer to "struct list_head".
1013 * @data: Policy to be interpreted.
1014 * @is_delete: True if it is a delete request.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001015 *
1016 * Returns 0 on success, negative value otherwise.
1017 *
1018 * Caller holds tomoyo_read_lock().
1019 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001020static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1021 struct list_head *list, char *data,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001022 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001023{
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001024 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001025 .ns = ns,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001026 .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;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001044}
1045
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001046/* String table for domain flags. */
1047const 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
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001052/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001053 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001054 *
1055 * @head: Pointer to "struct tomoyo_io_buffer".
1056 *
1057 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001058 *
1059 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001060 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001061static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001062{
1063 char *data = head->write_buf;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001064 struct tomoyo_policy_namespace *ns;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001065 struct tomoyo_domain_info *domain = head->w.domain;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001066 const bool is_delete = head->w.is_delete;
1067 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
Kentaro Takeda95908372009-02-05 17:18:13 +09001068 unsigned int profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001069 if (*data == '<') {
Kentaro Takeda95908372009-02-05 17:18:13 +09001070 domain = NULL;
1071 if (is_delete)
1072 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001073 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +09001074 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001075 else
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001076 domain = tomoyo_assign_domain(data, false);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001077 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001078 return 0;
1079 }
1080 if (!domain)
1081 return -EINVAL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001082 ns = domain->ns;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001083 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +09001084 && profile < TOMOYO_MAX_PROFILES) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001085 if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
Kentaro Takeda95908372009-02-05 17:18:13 +09001086 domain->profile = (u8) profile;
1087 return 0;
1088 }
Tetsuo Handa32997142011-06-26 23:19:28 +09001089 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 }
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001095 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;
Tetsuo Handa9b2443732010-06-03 20:35:53 +09001100 return 0;
1101 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001102 return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1103 is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +09001104}
1105
1106/**
Tetsuo Handa2066a362011-07-08 13:21:37 +09001107 * 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 */
1114static 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);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001129 const struct tomoyo_name_union *names_p =
1130 (typeof(names_p))
1131 (numbers_p + cond->numbers_count);
Tetsuo Handa5b636852011-07-08 13:24:54 +09001132 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);
Tetsuo Handa2066a362011-07-08 13:21:37 +09001136 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) {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001142 case TOMOYO_ARGV_ENTRY:
1143 argv++;
1144 continue;
1145 case TOMOYO_ENVP_ENTRY:
1146 envp++;
1147 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001148 case TOMOYO_NUMBER_UNION:
1149 numbers_p++;
1150 break;
1151 }
1152 switch (right) {
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001153 case TOMOYO_NAME_UNION:
1154 names_p++;
1155 break;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001156 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) {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001171 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;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001199 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) {
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001210 case TOMOYO_NAME_UNION:
1211 tomoyo_print_name_union_quoted
1212 (head, names_p++);
1213 break;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001214 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/**
Tetsuo Handa32997142011-06-26 23:19:28 +09001240 * tomoyo_set_group - Print "acl_group " header keyword and category name.
Kentaro Takeda95908372009-02-05 17:18:13 +09001241 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001242 * @head: Pointer to "struct tomoyo_io_buffer".
1243 * @category: Category name.
Kentaro Takeda95908372009-02-05 17:18:13 +09001244 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001245 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +09001246 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001247static void tomoyo_set_group(struct tomoyo_io_buffer *head,
1248 const char *category)
Kentaro Takeda95908372009-02-05 17:18:13 +09001249{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001250 if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1251 tomoyo_print_namespace(head);
Tetsuo Handa32997142011-06-26 23:19:28 +09001252 tomoyo_io_printf(head, "acl_group %u ",
1253 head->r.acl_group_index);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001254 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001255 tomoyo_set_string(head, category);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001256}
1257
1258/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001259 * tomoyo_print_entry - Print an ACL entry.
1260 *
1261 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001262 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +09001263 *
1264 * Returns true on success, false otherwise.
1265 */
1266static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001267 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +09001268{
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001269 const u8 acl_type = acl->type;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001270 bool first = true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001271 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +09001272
Tetsuo Handa2066a362011-07-08 13:21:37 +09001273 if (head->r.print_cond_part)
1274 goto print_cond_part;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001275 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +09001276 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001277 if (!tomoyo_flush(head))
1278 return false;
1279 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001280 struct tomoyo_path_acl *ptr =
1281 container_of(acl, typeof(*ptr), head);
1282 const u16 perm = ptr->perm;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001283 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001284 if (!(perm & (1 << bit)))
1285 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001286 if (head->r.print_transition_related_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001287 bit != TOMOYO_TYPE_EXECUTE)
1288 continue;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001289 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]);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001296 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001297 if (first)
1298 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001299 tomoyo_print_name_union(head, &ptr->name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001300 } else if (head->r.print_transition_related_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +09001301 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001302 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
1303 struct tomoyo_path2_acl *ptr =
1304 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001305 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;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001320 tomoyo_print_name_union(head, &ptr->name1);
1321 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001322 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1323 struct tomoyo_path_number_acl *ptr =
1324 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001325 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;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001340 tomoyo_print_name_union(head, &ptr->name);
1341 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001342 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1343 struct tomoyo_mkdev_acl *ptr =
1344 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001345 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;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001360 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);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001364 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1365 struct tomoyo_mount_acl *ptr =
1366 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001367 tomoyo_set_group(head, "file mount");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001368 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);
Kentaro Takeda95908372009-02-05 17:18:13 +09001372 }
Tetsuo Handa2066a362011-07-08 13:21:37 +09001373 if (acl->cond) {
1374 head->r.print_cond_part = true;
1375 head->r.cond_step = 0;
1376 if (!tomoyo_flush(head))
1377 return false;
1378print_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 }
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001385 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001386}
1387
1388/**
1389 * tomoyo_read_domain2 - Read domain policy.
1390 *
Tetsuo Handa32997142011-06-26 23:19:28 +09001391 * @head: Pointer to "struct tomoyo_io_buffer".
1392 * @list: Pointer to "struct list_head".
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001393 *
1394 * Caller holds tomoyo_read_lock().
1395 *
1396 * Returns true on success, false otherwise.
1397 */
1398static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
Tetsuo Handa32997142011-06-26 23:19:28 +09001399 struct list_head *list)
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001400{
Tetsuo Handa32997142011-06-26 23:19:28 +09001401 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001402 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;
Kentaro Takeda95908372009-02-05 17:18:13 +09001409}
1410
1411/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001412 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001413 *
1414 * @head: Pointer to "struct tomoyo_io_buffer".
1415 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001416 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001417 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001418static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001419{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001420 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001421 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001422 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001423 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001424 list_entry(head->r.domain, typeof(*domain), list);
1425 switch (head->r.step) {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001426 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001427 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);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001434 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001435 domain->profile);
Tetsuo Handa32997142011-06-26 23:19:28 +09001436 tomoyo_io_printf(head, "use_group %u\n",
1437 domain->group);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001438 for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
1439 if (domain->flags[i])
1440 tomoyo_set_string(head, tomoyo_dif[i]);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001441 head->r.step++;
1442 tomoyo_set_lf(head);
1443 /* fall through */
1444 case 1:
Tetsuo Handa32997142011-06-26 23:19:28 +09001445 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001446 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;
Kentaro Takeda95908372009-02-05 17:18:13 +09001455 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001456 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001457 done:
1458 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001459}
1460
1461/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001462 * tomoyo_write_pid: Specify PID to obtain domainname.
1463 *
1464 * @head: Pointer to "struct tomoyo_io_buffer".
1465 *
1466 * Returns 0.
1467 */
1468static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1469{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001470 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001471 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 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001484static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001485{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001486 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). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001496 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001497 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);
Kentaro Takeda95908372009-02-05 17:18:13 +09001517}
1518
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001519static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001520 [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 ",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001526};
1527
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001528static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001529 [TOMOYO_PATH_GROUP] = "path_group ",
1530 [TOMOYO_NUMBER_GROUP] = "number_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001531};
1532
Kentaro Takeda95908372009-02-05 17:18:13 +09001533/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001534 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001535 *
1536 * @head: Pointer to "struct tomoyo_io_buffer".
1537 *
1538 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001539 *
1540 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001541 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001542static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001543{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001544 const bool is_delete = head->w.is_delete;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001545 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001546 .ns = head->w.ns,
1547 .is_delete = is_delete,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001548 .data = head->write_buf,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001549 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001550 u8 i;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001551 if (tomoyo_str_starts(&param.data, "aggregator "))
1552 return tomoyo_write_aggregator(&param);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001553 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001554 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1555 return tomoyo_write_transition_control(&param, i);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001556 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001557 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1558 return tomoyo_write_group(&param, i);
Tetsuo Handa32997142011-06-26 23:19:28 +09001559 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++ == ' ')
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001564 return tomoyo_write_domain2
1565 (head->w.ns, &head->w.ns->acl_group[group],
1566 data, is_delete);
Tetsuo Handa32997142011-06-26 23:19:28 +09001567 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001568 return -EINVAL;
1569}
1570
Tetsuo Handa31845e82010-06-17 16:54:33 +09001571/**
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 */
1581static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1582{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001583 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) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001587 struct tomoyo_group *group =
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001588 list_entry(head->r.group, typeof(*group), head.list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001589 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001590 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001591 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001592 if (ptr->is_deleted)
1593 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001594 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001595 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001596 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001597 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);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001611 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001612 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001613 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001614 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001615 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 */
1628static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1629{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001630 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) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001634 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001635 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001636 if (acl->is_deleted)
1637 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001638 if (!tomoyo_flush(head))
1639 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001640 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001641 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001642 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001643 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001644 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001645 tomoyo_print_namespace(head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001646 tomoyo_set_string(head, tomoyo_transition_type
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001647 [ptr->type]);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001648 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");
Tetsuo Handa31845e82010-06-17 16:54:33 +09001654 }
1655 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001656 case TOMOYO_ID_AGGREGATOR:
1657 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001658 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001659 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001660 tomoyo_print_namespace(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001661 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001662 tomoyo_set_string(head,
1663 ptr->original_name->name);
1664 tomoyo_set_space(head);
1665 tomoyo_set_string(head,
1666 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001667 }
1668 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001669 default:
1670 continue;
1671 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001672 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001673 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001674 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001675 return true;
1676}
1677
Kentaro Takeda95908372009-02-05 17:18:13 +09001678/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001679 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001680 *
1681 * @head: Pointer to "struct tomoyo_io_buffer".
1682 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001683 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001684 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001685static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001686{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001687 struct tomoyo_policy_namespace *ns =
1688 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001689 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001690 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001691 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)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001695 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001696 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)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001700 return;
Tetsuo Handa32997142011-06-26 23:19:28 +09001701 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;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001705 if (!tomoyo_read_domain2(head, &ns->acl_group
Tetsuo Handa32997142011-06-26 23:19:28 +09001706 [head->r.acl_group_index]))
1707 return;
1708 head->r.step++;
1709 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001710 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001711}
1712
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001713/* Wait queue for kernel -> userspace notification. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001714static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001715/* Wait queue for userspace -> kernel notification. */
1716static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001717
1718/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001719struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001720 struct list_head list;
1721 char *query;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001722 size_t query_len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001723 unsigned int serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001724 u8 timer;
1725 u8 answer;
1726 u8 retry;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001727};
1728
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001729/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001730static LIST_HEAD(tomoyo_query_list);
1731
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001732/* Lock for manipulating tomoyo_query_list. */
1733static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1734
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001735/*
1736 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1737 * interface.
1738 */
1739static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1740
1741/**
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001742 * tomoyo_truncate - Truncate a line.
1743 *
1744 * @str: String to truncate.
1745 *
1746 * Returns length of truncated @str.
1747 */
1748static 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/**
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001758 * 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 */
1765static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1766{
1767 char *buffer;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001768 char *realpath = NULL;
Tetsuo Handa5b636852011-07-08 13:24:54 +09001769 char *argv0 = NULL;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001770 char *symlink = NULL;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001771 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;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001780 /* strstr() will return NULL if ordering is wrong. */
1781 if (*cp == 'f') {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001782 argv0 = strstr(header, " argv[]={ \"");
1783 if (argv0) {
1784 argv0 += 10;
1785 len += tomoyo_truncate(argv0) + 14;
1786 }
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001787 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 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001796 buffer = kmalloc(len, GFP_NOFS);
1797 if (!buffer)
1798 return;
1799 snprintf(buffer, len - 1, "%s", cp);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001800 if (realpath)
1801 tomoyo_addprintf(buffer, len, " exec.%s", realpath);
Tetsuo Handa5b636852011-07-08 13:24:54 +09001802 if (argv0)
1803 tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001804 if (symlink)
1805 tomoyo_addprintf(buffer, len, "%s", symlink);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001806 tomoyo_normalize_line(buffer);
Tetsuo Handab22b8b92011-06-26 23:21:50 +09001807 if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1808 false))
1809 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001810 kfree(buffer);
1811}
1812
1813/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001814 * tomoyo_supervisor - Ask for the supervisor's decision.
1815 *
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001816 * @r: Pointer to "struct tomoyo_request_info".
1817 * @fmt: The printf()'s format string, followed by parameters.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001818 *
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 */
1824int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1825{
1826 va_list args;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001827 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001828 int len;
1829 static unsigned int tomoyo_serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001830 struct tomoyo_query entry = { };
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001831 bool quota_exceeded = false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001832 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;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09001842 if (r->mode)
1843 tomoyo_update_stat(r->mode);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001844 switch (r->mode) {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001845 case TOMOYO_CONFIG_ENFORCING:
1846 error = -EPERM;
1847 if (atomic_read(&tomoyo_query_observers))
1848 break;
1849 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001850 case TOMOYO_CONFIG_LEARNING:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001851 error = 0;
1852 /* Check max_learning_entry parameter. */
1853 if (tomoyo_domain_quota_is_ok(r))
1854 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001855 /* fall through */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001856 default:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001857 return 0;
1858 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001859 /* Get message. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001860 va_start(args, fmt);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001861 entry.query = tomoyo_init_log(r, len, fmt, args);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001862 va_end(args);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001863 if (!entry.query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001864 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001865 entry.query_len = strlen(entry.query) + 1;
1866 if (!error) {
1867 tomoyo_add_entry(r->domain, entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001868 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001869 }
1870 len = tomoyo_round2(entry.query_len);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001871 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001872 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
1873 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
1874 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001875 quota_exceeded = true;
1876 } else {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001877 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);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001881 }
1882 spin_unlock(&tomoyo_query_list_lock);
1883 if (quota_exceeded)
1884 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001885 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001886 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))
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001891 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001892 else
1893 entry.timer++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001894 }
1895 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001896 list_del(&entry.list);
1897 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001898 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001899 switch (entry.answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001900 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;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001908 default:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001909 /* Timed out or rejected by administrator. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001910 break;
1911 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001912out:
1913 kfree(entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001914 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 */
1927static 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) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001935 struct tomoyo_query *ptr =
1936 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001937 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".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001956 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001957static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001958{
1959 struct list_head *tmp;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001960 unsigned int pos = 0;
1961 size_t len = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001962 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001963 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001964 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001965 if (head->read_buf) {
1966 kfree(head->read_buf);
1967 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001968 }
1969 spin_lock(&tomoyo_query_list_lock);
1970 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001971 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001972 if (ptr->answer)
1973 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001974 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001975 continue;
1976 len = ptr->query_len;
1977 break;
1978 }
1979 spin_unlock(&tomoyo_query_list_lock);
1980 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001981 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001982 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001983 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001984 buf = kzalloc(len + 32, GFP_NOFS);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001985 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001986 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001987 pos = 0;
1988 spin_lock(&tomoyo_query_list_lock);
1989 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001990 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001991 if (ptr->answer)
1992 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001993 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001994 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)
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002000 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2001 ptr->retry, ptr->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002002 break;
2003 }
2004 spin_unlock(&tomoyo_query_list_lock);
2005 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002006 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002007 head->r.w[head->r.w_pos++] = buf;
2008 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002009 } else {
2010 kfree(buf);
2011 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002012}
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 */
2021static 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) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002029 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002030 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) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002037 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002038 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/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002049 * tomoyo_read_version: Get version.
2050 *
2051 * @head: Pointer to "struct tomoyo_io_buffer".
2052 *
2053 * Returns version information.
2054 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002055static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002056{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002057 if (!head->r.eof) {
Tetsuo Handad5ca1722011-06-26 23:18:21 +09002058 tomoyo_io_printf(head, "2.4.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002059 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09002060 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002061}
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 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002070static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002071{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002072 if (!head->r.eof) {
Kentaro Takeda95908372009-02-05 17:18:13 +09002073 /*
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);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002079 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09002080 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002081}
2082
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002083/* String table for /sys/kernel/security/tomoyo/stat interface. */
2084static 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. */
2092static 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. */
2099static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2100/* Counter for number of updates. */
2101static 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 */
2110void 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 */
2128static 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 */
2170static 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
Kentaro Takeda95908372009-02-05 17:18:13 +09002181/**
2182 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
2183 *
2184 * @type: Type of interface.
2185 * @file: Pointer to "struct file".
2186 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002187 * Returns 0 on success, negative value otherwise.
Kentaro Takeda95908372009-02-05 17:18:13 +09002188 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002189int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09002190{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002191 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002192
2193 if (!head)
2194 return -ENOMEM;
2195 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002196 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09002197 switch (type) {
2198 case TOMOYO_DOMAINPOLICY:
2199 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002200 head->write = tomoyo_write_domain;
2201 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09002202 break;
2203 case TOMOYO_EXCEPTIONPOLICY:
2204 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002205 head->write = tomoyo_write_exception;
2206 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09002207 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002208 case TOMOYO_AUDIT:
2209 /* /sys/kernel/security/tomoyo/audit */
2210 head->poll = tomoyo_poll_log;
2211 head->read = tomoyo_read_log;
2212 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002213 case TOMOYO_SELFDOMAIN:
2214 /* /sys/kernel/security/tomoyo/self_domain */
2215 head->read = tomoyo_read_self_domain;
2216 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002217 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;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002227 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;
Kentaro Takeda95908372009-02-05 17:18:13 +09002232 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;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002238 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;
Kentaro Takeda95908372009-02-05 17:18:13 +09002243 case TOMOYO_MANAGER:
2244 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002245 head->write = tomoyo_write_manager;
2246 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09002247 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;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002255 head->poll = NULL;
2256 } else if (!head->poll) {
2257 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09002258 if (!head->readbuf_size)
2259 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002260 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002261 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002262 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002263 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;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002274 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002275 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002276 kfree(head->read_buf);
2277 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002278 return -ENOMEM;
2279 }
2280 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002281 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002282 * 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 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09002287 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002288 atomic_inc(&tomoyo_query_observers);
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002289 file->private_data = head;
2290 tomoyo_notify_gc(head, true);
Kentaro Takeda95908372009-02-05 17:18:13 +09002291 return 0;
2292}
2293
2294/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09002295 * 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.
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002301 * /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.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09002303 */
2304int 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/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002313 * tomoyo_set_namespace_cursor - Set namespace to read.
2314 *
2315 * @head: Pointer to "struct tomoyo_io_buffer".
2316 *
2317 * Returns nothing.
2318 */
2319static 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 */
2344static 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/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002352 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2353 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002354 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002355 * @buffer: Poiner to buffer to write to.
2356 * @buffer_len: Size of @buffer.
2357 *
2358 * Returns bytes read on success, negative value otherwise.
2359 */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002360ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
2361 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002362{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002363 int len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002364 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002365
2366 if (!head->read)
2367 return -ENOSYS;
2368 if (mutex_lock_interruptible(&head->io_sem))
2369 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002370 head->read_user_buf = buffer;
2371 head->read_user_buf_avail = buffer_len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002372 idx = tomoyo_read_lock();
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002373 if (tomoyo_flush(head))
2374 /* Call the policy handler. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002375 do {
2376 tomoyo_set_namespace_cursor(head);
2377 head->read(head);
2378 } while (tomoyo_flush(head) &&
2379 tomoyo_has_more_namespace(head));
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002380 tomoyo_read_unlock(idx);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002381 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09002382 mutex_unlock(&head->io_sem);
2383 return len;
2384}
2385
2386/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002387 * 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 */
2396static 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/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002424 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2425 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002426 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002427 * @buffer: Pointer to buffer to read from.
2428 * @buffer_len: Size of @buffer.
2429 *
2430 * Returns @buffer_len on success, negative value otherwise.
2431 */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002432ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
2433 const char __user *buffer, const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002434{
Kentaro Takeda95908372009-02-05 17:18:13 +09002435 int error = buffer_len;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002436 size_t avail_len = buffer_len;
Kentaro Takeda95908372009-02-05 17:18:13 +09002437 char *cp0 = head->write_buf;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002438 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002439 if (!head->write)
2440 return -ENOSYS;
2441 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2442 return -EFAULT;
Kentaro Takeda95908372009-02-05 17:18:13 +09002443 if (mutex_lock_interruptible(&head->io_sem))
2444 return -EINTR;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002445 idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09002446 /* Read a line and dispatch it to the policy handler. */
2447 while (avail_len > 0) {
2448 char c;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002449 if (head->w.avail >= head->writebuf_size - 1) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002450 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)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09002463 error = -EFAULT;
2464 break;
2465 }
2466 buffer++;
2467 avail_len--;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002468 cp0[head->w.avail++] = c;
Kentaro Takeda95908372009-02-05 17:18:13 +09002469 if (c != '\n')
2470 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002471 cp0[head->w.avail - 1] = '\0';
2472 head->w.avail = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09002473 tomoyo_normalize_line(cp0);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002474 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;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002505 case 0:
2506 switch (head->type) {
2507 case TOMOYO_DOMAINPOLICY:
2508 case TOMOYO_EXCEPTIONPOLICY:
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002509 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;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002518 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002519 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002520out:
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002521 tomoyo_read_unlock(idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002522 mutex_unlock(&head->io_sem);
2523 return error;
2524}
2525
2526/**
2527 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2528 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002529 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002530 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002531 * Returns 0.
Kentaro Takeda95908372009-02-05 17:18:13 +09002532 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002533int tomoyo_close_control(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002534{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002535 /*
2536 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2537 * observer counter.
2538 */
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002539 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);
Kentaro Takeda95908372009-02-05 17:18:13 +09002543 return 0;
2544}
2545
2546/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002547 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09002548 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002549void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09002550{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002551 struct tomoyo_domain_info *domain;
2552 const int idx = tomoyo_read_lock();
2553 tomoyo_policy_loaded = true;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002554 printk(KERN_INFO "TOMOYO: 2.4.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002555 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2556 const u8 profile = domain->profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002557 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
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002567 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002568 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/ "
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002572 "for more information.\n");
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002573 panic("STOP!");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002574 }
2575 tomoyo_read_unlock(idx);
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002576 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002577}
Tetsuo Handaefe836a2011-06-26 23:22:18 +09002578
2579/**
2580 * tomoyo_load_builtin_policy - Load built-in policy.
2581 *
2582 * Returns nothing.
2583 */
2584void __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);
Tetsuo Handa0e4ae0e2011-06-26 23:22:59 +09002637#ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
2638 tomoyo_check_profile();
2639#endif
Tetsuo Handaefe836a2011-06-26 23:22:18 +09002640}