blob: d116e1ece3e6b2e88fdad721c1db6c9404fff917 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takeda95908372009-02-05 17:18:13 +09005 */
6
7#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Kentaro Takeda95908372009-02-05 17:18:13 +09009#include <linux/security.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090010#include "common.h"
Kentaro Takeda95908372009-02-05 17:18:13 +090011
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090012/* String table for operation mode. */
13const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
14 [TOMOYO_CONFIG_DISABLED] = "disabled",
15 [TOMOYO_CONFIG_LEARNING] = "learning",
16 [TOMOYO_CONFIG_PERMISSIVE] = "permissive",
17 [TOMOYO_CONFIG_ENFORCING] = "enforcing"
Kentaro Takeda95908372009-02-05 17:18:13 +090018};
Kentaro Takeda95908372009-02-05 17:18:13 +090019
Tetsuo Handa57c25902010-06-03 20:38:44 +090020/* String table for /sys/kernel/security/tomoyo/profile */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090021const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
Tetsuo Handa57c25902010-06-03 20:38:44 +090022 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
Tetsuo Handad58e0da2011-09-10 15:22:48 +090023 /* CONFIG::file group */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090024 [TOMOYO_MAC_FILE_EXECUTE] = "execute",
25 [TOMOYO_MAC_FILE_OPEN] = "open",
26 [TOMOYO_MAC_FILE_CREATE] = "create",
27 [TOMOYO_MAC_FILE_UNLINK] = "unlink",
28 [TOMOYO_MAC_FILE_GETATTR] = "getattr",
29 [TOMOYO_MAC_FILE_MKDIR] = "mkdir",
30 [TOMOYO_MAC_FILE_RMDIR] = "rmdir",
31 [TOMOYO_MAC_FILE_MKFIFO] = "mkfifo",
32 [TOMOYO_MAC_FILE_MKSOCK] = "mksock",
33 [TOMOYO_MAC_FILE_TRUNCATE] = "truncate",
34 [TOMOYO_MAC_FILE_SYMLINK] = "symlink",
35 [TOMOYO_MAC_FILE_MKBLOCK] = "mkblock",
36 [TOMOYO_MAC_FILE_MKCHAR] = "mkchar",
37 [TOMOYO_MAC_FILE_LINK] = "link",
38 [TOMOYO_MAC_FILE_RENAME] = "rename",
39 [TOMOYO_MAC_FILE_CHMOD] = "chmod",
40 [TOMOYO_MAC_FILE_CHOWN] = "chown",
41 [TOMOYO_MAC_FILE_CHGRP] = "chgrp",
42 [TOMOYO_MAC_FILE_IOCTL] = "ioctl",
43 [TOMOYO_MAC_FILE_CHROOT] = "chroot",
44 [TOMOYO_MAC_FILE_MOUNT] = "mount",
45 [TOMOYO_MAC_FILE_UMOUNT] = "unmount",
46 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root",
Tetsuo Handad58e0da2011-09-10 15:22:48 +090047 /* CONFIG::misc group */
48 [TOMOYO_MAC_ENVIRON] = "env",
49 /* CONFIG group */
Tetsuo Handa57c25902010-06-03 20:38:44 +090050 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Tetsuo Handad58e0da2011-09-10 15:22:48 +090051 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_MISC] = "misc",
Kentaro Takeda95908372009-02-05 17:18:13 +090052};
53
Tetsuo Handa2066a362011-07-08 13:21:37 +090054/* String table for conditions. */
55const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = {
56 [TOMOYO_TASK_UID] = "task.uid",
57 [TOMOYO_TASK_EUID] = "task.euid",
58 [TOMOYO_TASK_SUID] = "task.suid",
59 [TOMOYO_TASK_FSUID] = "task.fsuid",
60 [TOMOYO_TASK_GID] = "task.gid",
61 [TOMOYO_TASK_EGID] = "task.egid",
62 [TOMOYO_TASK_SGID] = "task.sgid",
63 [TOMOYO_TASK_FSGID] = "task.fsgid",
64 [TOMOYO_TASK_PID] = "task.pid",
65 [TOMOYO_TASK_PPID] = "task.ppid",
Tetsuo Handa5b636852011-07-08 13:24:54 +090066 [TOMOYO_EXEC_ARGC] = "exec.argc",
67 [TOMOYO_EXEC_ENVC] = "exec.envc",
Tetsuo Handa8761afd2011-07-08 13:22:41 +090068 [TOMOYO_TYPE_IS_SOCKET] = "socket",
69 [TOMOYO_TYPE_IS_SYMLINK] = "symlink",
70 [TOMOYO_TYPE_IS_FILE] = "file",
71 [TOMOYO_TYPE_IS_BLOCK_DEV] = "block",
72 [TOMOYO_TYPE_IS_DIRECTORY] = "directory",
73 [TOMOYO_TYPE_IS_CHAR_DEV] = "char",
74 [TOMOYO_TYPE_IS_FIFO] = "fifo",
75 [TOMOYO_MODE_SETUID] = "setuid",
76 [TOMOYO_MODE_SETGID] = "setgid",
77 [TOMOYO_MODE_STICKY] = "sticky",
78 [TOMOYO_MODE_OWNER_READ] = "owner_read",
79 [TOMOYO_MODE_OWNER_WRITE] = "owner_write",
80 [TOMOYO_MODE_OWNER_EXECUTE] = "owner_execute",
81 [TOMOYO_MODE_GROUP_READ] = "group_read",
82 [TOMOYO_MODE_GROUP_WRITE] = "group_write",
83 [TOMOYO_MODE_GROUP_EXECUTE] = "group_execute",
84 [TOMOYO_MODE_OTHERS_READ] = "others_read",
85 [TOMOYO_MODE_OTHERS_WRITE] = "others_write",
86 [TOMOYO_MODE_OTHERS_EXECUTE] = "others_execute",
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +090087 [TOMOYO_EXEC_REALPATH] = "exec.realpath",
88 [TOMOYO_SYMLINK_TARGET] = "symlink.target",
Tetsuo Handa8761afd2011-07-08 13:22:41 +090089 [TOMOYO_PATH1_UID] = "path1.uid",
90 [TOMOYO_PATH1_GID] = "path1.gid",
91 [TOMOYO_PATH1_INO] = "path1.ino",
92 [TOMOYO_PATH1_MAJOR] = "path1.major",
93 [TOMOYO_PATH1_MINOR] = "path1.minor",
94 [TOMOYO_PATH1_PERM] = "path1.perm",
95 [TOMOYO_PATH1_TYPE] = "path1.type",
96 [TOMOYO_PATH1_DEV_MAJOR] = "path1.dev_major",
97 [TOMOYO_PATH1_DEV_MINOR] = "path1.dev_minor",
98 [TOMOYO_PATH2_UID] = "path2.uid",
99 [TOMOYO_PATH2_GID] = "path2.gid",
100 [TOMOYO_PATH2_INO] = "path2.ino",
101 [TOMOYO_PATH2_MAJOR] = "path2.major",
102 [TOMOYO_PATH2_MINOR] = "path2.minor",
103 [TOMOYO_PATH2_PERM] = "path2.perm",
104 [TOMOYO_PATH2_TYPE] = "path2.type",
105 [TOMOYO_PATH2_DEV_MAJOR] = "path2.dev_major",
106 [TOMOYO_PATH2_DEV_MINOR] = "path2.dev_minor",
107 [TOMOYO_PATH1_PARENT_UID] = "path1.parent.uid",
108 [TOMOYO_PATH1_PARENT_GID] = "path1.parent.gid",
109 [TOMOYO_PATH1_PARENT_INO] = "path1.parent.ino",
110 [TOMOYO_PATH1_PARENT_PERM] = "path1.parent.perm",
111 [TOMOYO_PATH2_PARENT_UID] = "path2.parent.uid",
112 [TOMOYO_PATH2_PARENT_GID] = "path2.parent.gid",
113 [TOMOYO_PATH2_PARENT_INO] = "path2.parent.ino",
114 [TOMOYO_PATH2_PARENT_PERM] = "path2.parent.perm",
Tetsuo Handa2066a362011-07-08 13:21:37 +0900115};
116
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900117/* String table for PREFERENCE keyword. */
118static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900119 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900120 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
121};
122
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900123/* String table for path operation. */
124const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
125 [TOMOYO_TYPE_EXECUTE] = "execute",
126 [TOMOYO_TYPE_READ] = "read",
127 [TOMOYO_TYPE_WRITE] = "write",
128 [TOMOYO_TYPE_APPEND] = "append",
129 [TOMOYO_TYPE_UNLINK] = "unlink",
130 [TOMOYO_TYPE_GETATTR] = "getattr",
131 [TOMOYO_TYPE_RMDIR] = "rmdir",
132 [TOMOYO_TYPE_TRUNCATE] = "truncate",
133 [TOMOYO_TYPE_SYMLINK] = "symlink",
134 [TOMOYO_TYPE_CHROOT] = "chroot",
135 [TOMOYO_TYPE_UMOUNT] = "unmount",
136};
137
138/* String table for categories. */
139static const char * const tomoyo_category_keywords
140[TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900141 [TOMOYO_MAC_CATEGORY_FILE] = "file",
142 [TOMOYO_MAC_CATEGORY_MISC] = "misc",
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900143};
144
Kentaro Takeda95908372009-02-05 17:18:13 +0900145/* Permit policy management by non-root user? */
146static bool tomoyo_manage_by_non_root;
147
148/* Utility functions. */
149
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900150/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900151 * tomoyo_yesno - Return "yes" or "no".
152 *
153 * @value: Bool value.
154 */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900155const char *tomoyo_yesno(const unsigned int value)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900156{
157 return value ? "yes" : "no";
158}
159
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900160/**
161 * tomoyo_addprintf - strncat()-like-snprintf().
162 *
163 * @buffer: Buffer to write to. Must be '\0'-terminated.
164 * @len: Size of @buffer.
165 * @fmt: The printf()'s format string, followed by parameters.
166 *
167 * Returns nothing.
168 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900169static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
170{
171 va_list args;
172 const int pos = strlen(buffer);
173 va_start(args, fmt);
174 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
175 va_end(args);
176}
177
178/**
179 * tomoyo_flush - Flush queued string to userspace's buffer.
180 *
181 * @head: Pointer to "struct tomoyo_io_buffer".
182 *
183 * Returns true if all data was flushed, false otherwise.
184 */
185static bool tomoyo_flush(struct tomoyo_io_buffer *head)
186{
187 while (head->r.w_pos) {
188 const char *w = head->r.w[0];
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900189 size_t len = strlen(w);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900190 if (len) {
191 if (len > head->read_user_buf_avail)
192 len = head->read_user_buf_avail;
193 if (!len)
194 return false;
195 if (copy_to_user(head->read_user_buf, w, len))
196 return false;
197 head->read_user_buf_avail -= len;
198 head->read_user_buf += len;
199 w += len;
200 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900201 head->r.w[0] = w;
202 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900203 return false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900204 /* Add '\0' for audit logs and query. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900205 if (head->poll) {
206 if (!head->read_user_buf_avail ||
207 copy_to_user(head->read_user_buf, "", 1))
208 return false;
209 head->read_user_buf_avail--;
210 head->read_user_buf++;
211 }
212 head->r.w_pos--;
213 for (len = 0; len < head->r.w_pos; len++)
214 head->r.w[len] = head->r.w[len + 1];
215 }
216 head->r.avail = 0;
217 return true;
218}
219
220/**
221 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
222 *
223 * @head: Pointer to "struct tomoyo_io_buffer".
224 * @string: String to print.
225 *
226 * Note that @string has to be kept valid until @head is kfree()d.
227 * This means that char[] allocated on stack memory cannot be passed to
228 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
229 */
230static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
231{
232 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
233 head->r.w[head->r.w_pos++] = string;
234 tomoyo_flush(head);
235 } else
236 WARN_ON(1);
237}
238
239/**
240 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
241 *
242 * @head: Pointer to "struct tomoyo_io_buffer".
243 * @fmt: The printf()'s format string, followed by parameters.
244 */
245void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
246{
247 va_list args;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900248 size_t len;
249 size_t pos = head->r.avail;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900250 int size = head->readbuf_size - pos;
251 if (size <= 0)
252 return;
253 va_start(args, fmt);
254 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
255 va_end(args);
256 if (pos + len >= head->readbuf_size) {
257 WARN_ON(1);
258 return;
259 }
260 head->r.avail += len;
261 tomoyo_set_string(head, head->read_buf + pos);
262}
263
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900264/**
265 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
266 *
267 * @head: Pointer to "struct tomoyo_io_buffer".
268 *
269 * Returns nothing.
270 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900271static void tomoyo_set_space(struct tomoyo_io_buffer *head)
272{
273 tomoyo_set_string(head, " ");
274}
275
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900276/**
277 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
278 *
279 * @head: Pointer to "struct tomoyo_io_buffer".
280 *
281 * Returns nothing.
282 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900283static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
284{
285 tomoyo_set_string(head, "\n");
286 return !head->r.w_pos;
287}
288
Tetsuo Handa57c25902010-06-03 20:38:44 +0900289/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900290 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
291 *
292 * @head: Pointer to "struct tomoyo_io_buffer".
293 *
294 * Returns nothing.
295 */
296static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
297{
298 tomoyo_set_string(head, "/");
299}
300
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900301/* List of namespaces. */
302LIST_HEAD(tomoyo_namespace_list);
303/* True if namespace other than tomoyo_kernel_namespace is defined. */
304static bool tomoyo_namespace_enabled;
305
306/**
307 * tomoyo_init_policy_namespace - Initialize namespace.
308 *
309 * @ns: Pointer to "struct tomoyo_policy_namespace".
310 *
311 * Returns nothing.
312 */
313void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
314{
315 unsigned int idx;
316 for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
317 INIT_LIST_HEAD(&ns->acl_group[idx]);
318 for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
319 INIT_LIST_HEAD(&ns->group_list[idx]);
320 for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
321 INIT_LIST_HEAD(&ns->policy_list[idx]);
322 ns->profile_version = 20100903;
323 tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
324 list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
325}
326
327/**
328 * tomoyo_print_namespace - Print namespace header.
329 *
330 * @head: Pointer to "struct tomoyo_io_buffer".
331 *
332 * Returns nothing.
333 */
334static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
335{
336 if (!tomoyo_namespace_enabled)
337 return;
338 tomoyo_set_string(head,
339 container_of(head->r.ns,
340 struct tomoyo_policy_namespace,
341 namespace_list)->name);
342 tomoyo_set_space(head);
343}
344
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900345/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900346 * tomoyo_print_name_union - Print a tomoyo_name_union.
347 *
348 * @head: Pointer to "struct tomoyo_io_buffer".
349 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900350 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900351static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
352 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900353{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900354 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900355 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900356 tomoyo_set_string(head, "@");
357 tomoyo_set_string(head, ptr->group->group_name->name);
358 } else {
359 tomoyo_set_string(head, ptr->filename->name);
360 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900361}
362
363/**
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +0900364 * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
365 *
366 * @head: Pointer to "struct tomoyo_io_buffer".
367 * @ptr: Pointer to "struct tomoyo_name_union".
368 *
369 * Returns nothing.
370 */
371static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
372 const struct tomoyo_name_union *ptr)
373{
374 if (ptr->group) {
375 tomoyo_set_string(head, "@");
376 tomoyo_set_string(head, ptr->group->group_name->name);
377 } else {
378 tomoyo_set_string(head, "\"");
379 tomoyo_set_string(head, ptr->filename->name);
380 tomoyo_set_string(head, "\"");
381 }
382}
383
384/**
Tetsuo Handa2066a362011-07-08 13:21:37 +0900385 * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900386 *
Tetsuo Handa2066a362011-07-08 13:21:37 +0900387 * @head: Pointer to "struct tomoyo_io_buffer".
388 * @ptr: Pointer to "struct tomoyo_number_union".
389 *
390 * Returns nothing.
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900391 */
Tetsuo Handa2066a362011-07-08 13:21:37 +0900392static void tomoyo_print_number_union_nospace
393(struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900394{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900395 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900396 tomoyo_set_string(head, "@");
397 tomoyo_set_string(head, ptr->group->group_name->name);
398 } else {
399 int i;
400 unsigned long min = ptr->values[0];
401 const unsigned long max = ptr->values[1];
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900402 u8 min_type = ptr->value_type[0];
403 const u8 max_type = ptr->value_type[1];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900404 char buffer[128];
405 buffer[0] = '\0';
406 for (i = 0; i < 2; i++) {
407 switch (min_type) {
408 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
409 tomoyo_addprintf(buffer, sizeof(buffer),
410 "0x%lX", min);
411 break;
412 case TOMOYO_VALUE_TYPE_OCTAL:
413 tomoyo_addprintf(buffer, sizeof(buffer),
414 "0%lo", min);
415 break;
416 default:
Tetsuo Handa2066a362011-07-08 13:21:37 +0900417 tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
418 min);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900419 break;
420 }
421 if (min == max && min_type == max_type)
422 break;
423 tomoyo_addprintf(buffer, sizeof(buffer), "-");
424 min_type = max_type;
425 min = max;
426 }
427 tomoyo_io_printf(head, "%s", buffer);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900428 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900429}
430
431/**
Tetsuo Handa2066a362011-07-08 13:21:37 +0900432 * tomoyo_print_number_union - Print a tomoyo_number_union.
433 *
434 * @head: Pointer to "struct tomoyo_io_buffer".
435 * @ptr: Pointer to "struct tomoyo_number_union".
436 *
437 * Returns nothing.
438 */
439static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
440 const struct tomoyo_number_union *ptr)
441{
442 tomoyo_set_space(head);
443 tomoyo_print_number_union_nospace(head, ptr);
444}
445
446/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900447 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900448 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900449 * @ns: Pointer to "struct tomoyo_policy_namespace".
Kentaro Takeda95908372009-02-05 17:18:13 +0900450 * @profile: Profile number to create.
451 *
452 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
453 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900454static struct tomoyo_profile *tomoyo_assign_profile
455(struct tomoyo_policy_namespace *ns, const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900456{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900457 struct tomoyo_profile *ptr;
458 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900459 if (profile >= TOMOYO_MAX_PROFILES)
460 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900461 ptr = ns->profile_ptr[profile];
Kentaro Takeda95908372009-02-05 17:18:13 +0900462 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900463 return ptr;
464 entry = kzalloc(sizeof(*entry), GFP_NOFS);
465 if (mutex_lock_interruptible(&tomoyo_policy_lock))
466 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900467 ptr = ns->profile_ptr[profile];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900468 if (!ptr && tomoyo_memory_ok(entry)) {
469 ptr = entry;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900470 ptr->default_config = TOMOYO_CONFIG_DISABLED |
471 TOMOYO_CONFIG_WANT_GRANT_LOG |
472 TOMOYO_CONFIG_WANT_REJECT_LOG;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900473 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
474 sizeof(ptr->config));
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900475 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900476 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900477 mb(); /* Avoid out-of-order execution. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900478 ns->profile_ptr[profile] = ptr;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900479 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900480 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900481 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900482 out:
483 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900484 return ptr;
485}
486
487/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900488 * tomoyo_profile - Find a profile.
489 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900490 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900491 * @profile: Profile number to find.
492 *
493 * Returns pointer to "struct tomoyo_profile".
494 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900495struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
496 const u8 profile)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900497{
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900498 static struct tomoyo_profile tomoyo_null_profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900499 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900500 if (!ptr)
501 ptr = &tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900502 return ptr;
503}
504
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900505/**
506 * tomoyo_find_yesno - Find values for specified keyword.
507 *
508 * @string: String to check.
509 * @find: Name of keyword.
510 *
511 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
512 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900513static s8 tomoyo_find_yesno(const char *string, const char *find)
514{
515 const char *cp = strstr(string, find);
516 if (cp) {
517 cp += strlen(find);
518 if (!strncmp(cp, "=yes", 4))
519 return 1;
520 else if (!strncmp(cp, "=no", 3))
521 return 0;
522 }
523 return -1;
524}
525
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900526/**
527 * tomoyo_set_uint - Set value for specified preference.
528 *
529 * @i: Pointer to "unsigned int".
530 * @string: String to check.
531 * @find: Name of keyword.
532 *
533 * Returns nothing.
534 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900535static void tomoyo_set_uint(unsigned int *i, const char *string,
536 const char *find)
537{
538 const char *cp = strstr(string, find);
539 if (cp)
540 sscanf(cp + strlen(find), "=%u", i);
541}
542
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900543/**
544 * tomoyo_set_mode - Set mode for specified profile.
545 *
546 * @name: Name of functionality.
547 * @value: Mode for @name.
548 * @profile: Pointer to "struct tomoyo_profile".
549 *
550 * Returns 0 on success, negative value otherwise.
551 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900552static int tomoyo_set_mode(char *name, const char *value,
Tetsuo Handa8e568682010-06-25 09:30:09 +0900553 struct tomoyo_profile *profile)
554{
555 u8 i;
556 u8 config;
557 if (!strcmp(name, "CONFIG")) {
558 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
559 config = profile->default_config;
560 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
561 config = 0;
562 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
563 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900564 int len = 0;
565 if (i < TOMOYO_MAX_MAC_INDEX) {
566 const u8 c = tomoyo_index2category[i];
567 const char *category =
568 tomoyo_category_keywords[c];
569 len = strlen(category);
570 if (strncmp(name, category, len) ||
571 name[len++] != ':' || name[len++] != ':')
572 continue;
573 }
574 if (strcmp(name + len, tomoyo_mac_keywords[i]))
Tetsuo Handa8e568682010-06-25 09:30:09 +0900575 continue;
576 config = profile->config[i];
577 break;
578 }
579 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
580 return -EINVAL;
581 } else {
582 return -EINVAL;
583 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900584 if (strstr(value, "use_default")) {
Tetsuo Handa8e568682010-06-25 09:30:09 +0900585 config = TOMOYO_CONFIG_USE_DEFAULT;
586 } else {
587 u8 mode;
588 for (mode = 0; mode < 4; mode++)
589 if (strstr(value, tomoyo_mode[mode]))
590 /*
591 * Update lower 3 bits in order to distinguish
592 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
593 */
594 config = (config & ~7) | mode;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900595 if (config != TOMOYO_CONFIG_USE_DEFAULT) {
596 switch (tomoyo_find_yesno(value, "grant_log")) {
597 case 1:
598 config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
599 break;
600 case 0:
601 config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
602 break;
603 }
604 switch (tomoyo_find_yesno(value, "reject_log")) {
605 case 1:
606 config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
607 break;
608 case 0:
609 config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
610 break;
611 }
612 }
Tetsuo Handa8e568682010-06-25 09:30:09 +0900613 }
614 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
615 profile->config[i] = config;
616 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
617 profile->default_config = config;
618 return 0;
619}
620
Tetsuo Handa57c25902010-06-03 20:38:44 +0900621/**
622 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900623 *
624 * @head: Pointer to "struct tomoyo_io_buffer".
625 *
626 * Returns 0 on success, negative value otherwise.
627 */
628static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
629{
630 char *data = head->write_buf;
631 unsigned int i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900632 char *cp;
633 struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900634 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
635 == 1)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900636 return 0;
637 i = simple_strtoul(data, &cp, 10);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900638 if (*cp != '-')
639 return -EINVAL;
640 data = cp + 1;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900641 profile = tomoyo_assign_profile(head->w.ns, i);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900642 if (!profile)
643 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900644 cp = strchr(data, '=');
645 if (!cp)
646 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900647 *cp++ = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +0900648 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900649 static DEFINE_SPINLOCK(lock);
650 const struct tomoyo_path_info *new_comment
651 = tomoyo_get_name(cp);
652 const struct tomoyo_path_info *old_comment;
653 if (!new_comment)
654 return -ENOMEM;
655 spin_lock(&lock);
656 old_comment = profile->comment;
657 profile->comment = new_comment;
658 spin_unlock(&lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900659 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900660 return 0;
661 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900662 if (!strcmp(data, "PREFERENCE")) {
663 for (i = 0; i < TOMOYO_MAX_PREF; i++)
664 tomoyo_set_uint(&profile->pref[i], cp,
665 tomoyo_pref_keywords[i]);
666 return 0;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900667 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900668 return tomoyo_set_mode(data, cp, profile);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900669}
670
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900671/**
672 * tomoyo_print_config - Print mode for specified functionality.
673 *
674 * @head: Pointer to "struct tomoyo_io_buffer".
675 * @config: Mode for that functionality.
676 *
677 * Returns nothing.
678 *
679 * Caller prints functionality's name.
680 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900681static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
682{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900683 tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
684 tomoyo_mode[config & 3],
685 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
686 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900687}
688
Kentaro Takeda95908372009-02-05 17:18:13 +0900689/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900690 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900691 *
692 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900693 *
694 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900695 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900696static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900697{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900698 u8 index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900699 struct tomoyo_policy_namespace *ns =
700 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900701 const struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900702 if (head->r.eof)
703 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900704 next:
705 index = head->r.index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900706 profile = ns->profile_ptr[index];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900707 switch (head->r.step) {
708 case 0:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900709 tomoyo_print_namespace(head);
710 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
711 ns->profile_version);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900712 head->r.step++;
713 break;
714 case 1:
715 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
716 head->r.index++)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900717 if (ns->profile_ptr[head->r.index])
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900718 break;
719 if (head->r.index == TOMOYO_MAX_PROFILES)
720 return;
721 head->r.step++;
722 break;
723 case 2:
724 {
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900725 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900726 const struct tomoyo_path_info *comment =
727 profile->comment;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900728 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900729 tomoyo_io_printf(head, "%u-COMMENT=", index);
730 tomoyo_set_string(head, comment ? comment->name : "");
731 tomoyo_set_lf(head);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900732 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
733 for (i = 0; i < TOMOYO_MAX_PREF; i++)
734 tomoyo_io_printf(head, "%s=%u ",
735 tomoyo_pref_keywords[i],
736 profile->pref[i]);
737 tomoyo_set_string(head, "}\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900738 head->r.step++;
739 }
740 break;
741 case 3:
742 {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900743 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900744 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
745 tomoyo_print_config(head, profile->default_config);
746 head->r.bit = 0;
747 head->r.step++;
748 }
749 break;
750 case 4:
751 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
752 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
753 const u8 i = head->r.bit;
754 const u8 config = profile->config[i];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900755 if (config == TOMOYO_CONFIG_USE_DEFAULT)
756 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900757 tomoyo_print_namespace(head);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900758 if (i < TOMOYO_MAX_MAC_INDEX)
759 tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
760 index,
761 tomoyo_category_keywords
762 [tomoyo_index2category[i]],
763 tomoyo_mac_keywords[i]);
764 else
765 tomoyo_io_printf(head, "%u-CONFIG::%s", index,
766 tomoyo_mac_keywords[i]);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900767 tomoyo_print_config(head, config);
768 head->r.bit++;
769 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900770 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900771 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
772 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900773 head->r.index++;
774 head->r.step = 1;
775 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900776 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900777 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900778 if (tomoyo_flush(head))
779 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900780}
781
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900782/**
783 * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
784 *
785 * @a: Pointer to "struct tomoyo_acl_head".
786 * @b: Pointer to "struct tomoyo_acl_head".
787 *
788 * Returns true if @a == @b, false otherwise.
789 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900790static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
791 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900792{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900793 return container_of(a, struct tomoyo_manager, head)->manager ==
794 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900795}
796
Kentaro Takeda95908372009-02-05 17:18:13 +0900797/**
798 * tomoyo_update_manager_entry - Add a manager entry.
799 *
800 * @manager: The path to manager or the domainnamme.
801 * @is_delete: True if it is a delete request.
802 *
803 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900804 *
805 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900806 */
807static int tomoyo_update_manager_entry(const char *manager,
808 const bool is_delete)
809{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900810 struct tomoyo_manager e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900811 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900812 /* .ns = &tomoyo_kernel_namespace, */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900813 .is_delete = is_delete,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900814 .list = &tomoyo_kernel_namespace.
815 policy_list[TOMOYO_ID_MANAGER],
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900816 };
817 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900818 if (tomoyo_domain_def(manager)) {
819 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900820 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900821 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900822 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900823 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900824 return -EINVAL;
825 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900826 e.manager = tomoyo_get_name(manager);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900827 if (e.manager) {
828 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
829 tomoyo_same_manager);
830 tomoyo_put_name(e.manager);
831 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900832 return error;
833}
834
835/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900836 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900837 *
838 * @head: Pointer to "struct tomoyo_io_buffer".
839 *
840 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900841 *
842 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900843 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900844static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900845{
846 char *data = head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900847
848 if (!strcmp(data, "manage_by_non_root")) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900849 tomoyo_manage_by_non_root = !head->w.is_delete;
Kentaro Takeda95908372009-02-05 17:18:13 +0900850 return 0;
851 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900852 return tomoyo_update_manager_entry(data, head->w.is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900853}
854
855/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900856 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900857 *
858 * @head: Pointer to "struct tomoyo_io_buffer".
859 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900860 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900861 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900862static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900863{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900864 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900865 return;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900866 list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
867 policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900868 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900869 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900870 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900871 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900872 if (!tomoyo_flush(head))
873 return;
874 tomoyo_set_string(head, ptr->manager->name);
875 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900876 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900877 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900878}
879
880/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900881 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900882 *
883 * Returns true if the current process is permitted to modify policy
884 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900885 *
886 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900887 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900888static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900889{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900890 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900891 const char *exe;
892 const struct task_struct *task = current;
893 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
894 bool found = false;
895
896 if (!tomoyo_policy_loaded)
897 return true;
898 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
899 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900900 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
901 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900902 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900903 && !tomoyo_pathcmp(domainname, ptr->manager)) {
904 found = true;
905 break;
906 }
907 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900908 if (found)
909 return true;
910 exe = tomoyo_get_exe();
911 if (!exe)
912 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900913 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
914 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900915 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900916 && !strcmp(exe, ptr->manager->name)) {
917 found = true;
918 break;
919 }
920 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900921 if (!found) { /* Reduce error messages. */
922 static pid_t last_pid;
923 const pid_t pid = current->pid;
924 if (last_pid != pid) {
925 printk(KERN_WARNING "%s ( %s ) is not permitted to "
926 "update policies.\n", domainname->name, exe);
927 last_pid = pid;
928 }
929 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900930 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900931 return found;
932}
933
934/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900935 * tomoyo_select_domain - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900936 *
937 * @head: Pointer to "struct tomoyo_io_buffer".
938 * @data: String to parse.
939 *
940 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900941 *
942 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900943 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900944static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
945 const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900946{
947 unsigned int pid;
948 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900949 bool global_pid = false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900950 if (strncmp(data, "select ", 7))
951 return false;
952 data += 7;
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900953 if (sscanf(data, "pid=%u", &pid) == 1 ||
954 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900955 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900956 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900957 read_lock(&tasklist_lock);
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900958 if (global_pid)
959 p = find_task_by_pid_ns(pid, &init_pid_ns);
960 else
961 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900962 if (p)
963 domain = tomoyo_real_domain(p);
964 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900965 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900966 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900967 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900968 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900969 } else
970 return false;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900971 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900972 /* Accessing read_buf is safe because head->io_sem is held. */
973 if (!head->read_buf)
974 return true; /* Do nothing if open(O_WRONLY). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900975 memset(&head->r, 0, sizeof(head->r));
976 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +0200977 if (domain)
978 head->r.domain = &domain->list;
979 else
980 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +0900981 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900982 if (domain && domain->is_deleted)
983 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900984 return true;
985}
986
987/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900988 * tomoyo_delete_domain - Delete a domain.
989 *
990 * @domainname: The name of domain.
991 *
992 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900993 *
994 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900995 */
996static int tomoyo_delete_domain(char *domainname)
997{
998 struct tomoyo_domain_info *domain;
999 struct tomoyo_path_info name;
1000
1001 name.name = domainname;
1002 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +09001003 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1004 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001005 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001006 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001007 /* Never delete tomoyo_kernel_domain */
1008 if (domain == &tomoyo_kernel_domain)
1009 continue;
1010 if (domain->is_deleted ||
1011 tomoyo_pathcmp(domain->domainname, &name))
1012 continue;
1013 domain->is_deleted = true;
1014 break;
1015 }
Tetsuo Handaf737d952010-01-03 21:16:32 +09001016 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001017 return 0;
1018}
1019
1020/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001021 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001022 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001023 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001024 * @list: Pointer to "struct list_head".
1025 * @data: Policy to be interpreted.
1026 * @is_delete: True if it is a delete request.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001027 *
1028 * Returns 0 on success, negative value otherwise.
1029 *
1030 * Caller holds tomoyo_read_lock().
1031 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001032static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1033 struct list_head *list, char *data,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001034 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001035{
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001036 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001037 .ns = ns,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001038 .list = list,
1039 .data = data,
1040 .is_delete = is_delete,
1041 };
1042 static const struct {
1043 const char *keyword;
1044 int (*write) (struct tomoyo_acl_param *);
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001045 } tomoyo_callback[2] = {
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001046 { "file ", tomoyo_write_file },
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001047 { "misc ", tomoyo_write_misc },
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001048 };
1049 u8 i;
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001050
1051 for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001052 if (!tomoyo_str_starts(&param.data,
1053 tomoyo_callback[i].keyword))
1054 continue;
1055 return tomoyo_callback[i].write(&param);
1056 }
1057 return -EINVAL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001058}
1059
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001060/* String table for domain flags. */
1061const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
1062 [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n",
1063 [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
1064};
1065
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001066/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001067 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001068 *
1069 * @head: Pointer to "struct tomoyo_io_buffer".
1070 *
1071 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001072 *
1073 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001074 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001075static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001076{
1077 char *data = head->write_buf;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001078 struct tomoyo_policy_namespace *ns;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001079 struct tomoyo_domain_info *domain = head->w.domain;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001080 const bool is_delete = head->w.is_delete;
1081 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
Kentaro Takeda95908372009-02-05 17:18:13 +09001082 unsigned int profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001083 if (*data == '<') {
Kentaro Takeda95908372009-02-05 17:18:13 +09001084 domain = NULL;
1085 if (is_delete)
1086 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001087 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +09001088 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001089 else
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001090 domain = tomoyo_assign_domain(data, false);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001091 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001092 return 0;
1093 }
1094 if (!domain)
1095 return -EINVAL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001096 ns = domain->ns;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001097 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +09001098 && profile < TOMOYO_MAX_PROFILES) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001099 if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
Kentaro Takeda95908372009-02-05 17:18:13 +09001100 domain->profile = (u8) profile;
1101 return 0;
1102 }
Tetsuo Handa32997142011-06-26 23:19:28 +09001103 if (sscanf(data, "use_group %u\n", &profile) == 1
1104 && profile < TOMOYO_MAX_ACL_GROUPS) {
1105 if (!is_delete)
1106 domain->group = (u8) profile;
1107 return 0;
1108 }
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001109 for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
1110 const char *cp = tomoyo_dif[profile];
1111 if (strncmp(data, cp, strlen(cp) - 1))
1112 continue;
1113 domain->flags[profile] = !is_delete;
Tetsuo Handa9b2443732010-06-03 20:35:53 +09001114 return 0;
1115 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001116 return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1117 is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +09001118}
1119
1120/**
Tetsuo Handa2066a362011-07-08 13:21:37 +09001121 * tomoyo_print_condition - Print condition part.
1122 *
1123 * @head: Pointer to "struct tomoyo_io_buffer".
1124 * @cond: Pointer to "struct tomoyo_condition".
1125 *
1126 * Returns true on success, false otherwise.
1127 */
1128static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
1129 const struct tomoyo_condition *cond)
1130{
1131 switch (head->r.cond_step) {
1132 case 0:
1133 head->r.cond_index = 0;
1134 head->r.cond_step++;
1135 /* fall through */
1136 case 1:
1137 {
1138 const u16 condc = cond->condc;
1139 const struct tomoyo_condition_element *condp =
1140 (typeof(condp)) (cond + 1);
1141 const struct tomoyo_number_union *numbers_p =
1142 (typeof(numbers_p)) (condp + condc);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001143 const struct tomoyo_name_union *names_p =
1144 (typeof(names_p))
1145 (numbers_p + cond->numbers_count);
Tetsuo Handa5b636852011-07-08 13:24:54 +09001146 const struct tomoyo_argv *argv =
1147 (typeof(argv)) (names_p + cond->names_count);
1148 const struct tomoyo_envp *envp =
1149 (typeof(envp)) (argv + cond->argc);
Tetsuo Handa2066a362011-07-08 13:21:37 +09001150 u16 skip;
1151 for (skip = 0; skip < head->r.cond_index; skip++) {
1152 const u8 left = condp->left;
1153 const u8 right = condp->right;
1154 condp++;
1155 switch (left) {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001156 case TOMOYO_ARGV_ENTRY:
1157 argv++;
1158 continue;
1159 case TOMOYO_ENVP_ENTRY:
1160 envp++;
1161 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001162 case TOMOYO_NUMBER_UNION:
1163 numbers_p++;
1164 break;
1165 }
1166 switch (right) {
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001167 case TOMOYO_NAME_UNION:
1168 names_p++;
1169 break;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001170 case TOMOYO_NUMBER_UNION:
1171 numbers_p++;
1172 break;
1173 }
1174 }
1175 while (head->r.cond_index < condc) {
1176 const u8 match = condp->equals;
1177 const u8 left = condp->left;
1178 const u8 right = condp->right;
1179 if (!tomoyo_flush(head))
1180 return false;
1181 condp++;
1182 head->r.cond_index++;
1183 tomoyo_set_space(head);
1184 switch (left) {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001185 case TOMOYO_ARGV_ENTRY:
1186 tomoyo_io_printf(head,
1187 "exec.argv[%lu]%s=\"",
1188 argv->index, argv->
1189 is_not ? "!" : "");
1190 tomoyo_set_string(head,
1191 argv->value->name);
1192 tomoyo_set_string(head, "\"");
1193 argv++;
1194 continue;
1195 case TOMOYO_ENVP_ENTRY:
1196 tomoyo_set_string(head,
1197 "exec.envp[\"");
1198 tomoyo_set_string(head,
1199 envp->name->name);
1200 tomoyo_io_printf(head, "\"]%s=", envp->
1201 is_not ? "!" : "");
1202 if (envp->value) {
1203 tomoyo_set_string(head, "\"");
1204 tomoyo_set_string(head, envp->
1205 value->name);
1206 tomoyo_set_string(head, "\"");
1207 } else {
1208 tomoyo_set_string(head,
1209 "NULL");
1210 }
1211 envp++;
1212 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001213 case TOMOYO_NUMBER_UNION:
1214 tomoyo_print_number_union_nospace
1215 (head, numbers_p++);
1216 break;
1217 default:
1218 tomoyo_set_string(head,
1219 tomoyo_condition_keyword[left]);
1220 break;
1221 }
1222 tomoyo_set_string(head, match ? "=" : "!=");
1223 switch (right) {
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001224 case TOMOYO_NAME_UNION:
1225 tomoyo_print_name_union_quoted
1226 (head, names_p++);
1227 break;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001228 case TOMOYO_NUMBER_UNION:
1229 tomoyo_print_number_union_nospace
1230 (head, numbers_p++);
1231 break;
1232 default:
1233 tomoyo_set_string(head,
1234 tomoyo_condition_keyword[right]);
1235 break;
1236 }
1237 }
1238 }
1239 head->r.cond_step++;
1240 /* fall through */
1241 case 2:
1242 if (!tomoyo_flush(head))
1243 break;
1244 head->r.cond_step++;
1245 /* fall through */
1246 case 3:
1247 tomoyo_set_lf(head);
1248 return true;
1249 }
1250 return false;
1251}
1252
1253/**
Tetsuo Handa32997142011-06-26 23:19:28 +09001254 * tomoyo_set_group - Print "acl_group " header keyword and category name.
Kentaro Takeda95908372009-02-05 17:18:13 +09001255 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001256 * @head: Pointer to "struct tomoyo_io_buffer".
1257 * @category: Category name.
Kentaro Takeda95908372009-02-05 17:18:13 +09001258 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001259 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +09001260 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001261static void tomoyo_set_group(struct tomoyo_io_buffer *head,
1262 const char *category)
Kentaro Takeda95908372009-02-05 17:18:13 +09001263{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001264 if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1265 tomoyo_print_namespace(head);
Tetsuo Handa32997142011-06-26 23:19:28 +09001266 tomoyo_io_printf(head, "acl_group %u ",
1267 head->r.acl_group_index);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001268 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001269 tomoyo_set_string(head, category);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001270}
1271
1272/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001273 * tomoyo_print_entry - Print an ACL entry.
1274 *
1275 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001276 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +09001277 *
1278 * Returns true on success, false otherwise.
1279 */
1280static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001281 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +09001282{
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001283 const u8 acl_type = acl->type;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001284 bool first = true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001285 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +09001286
Tetsuo Handa2066a362011-07-08 13:21:37 +09001287 if (head->r.print_cond_part)
1288 goto print_cond_part;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001289 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +09001290 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001291 if (!tomoyo_flush(head))
1292 return false;
1293 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001294 struct tomoyo_path_acl *ptr =
1295 container_of(acl, typeof(*ptr), head);
1296 const u16 perm = ptr->perm;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001297 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001298 if (!(perm & (1 << bit)))
1299 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001300 if (head->r.print_transition_related_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001301 bit != TOMOYO_TYPE_EXECUTE)
1302 continue;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001303 if (first) {
1304 tomoyo_set_group(head, "file ");
1305 first = false;
1306 } else {
1307 tomoyo_set_slash(head);
1308 }
1309 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001310 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001311 if (first)
1312 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001313 tomoyo_print_name_union(head, &ptr->name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001314 } else if (head->r.print_transition_related_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +09001315 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001316 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
1317 struct tomoyo_path2_acl *ptr =
1318 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001319 const u8 perm = ptr->perm;
1320 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
1321 if (!(perm & (1 << bit)))
1322 continue;
1323 if (first) {
1324 tomoyo_set_group(head, "file ");
1325 first = false;
1326 } else {
1327 tomoyo_set_slash(head);
1328 }
1329 tomoyo_set_string(head, tomoyo_mac_keywords
1330 [tomoyo_pp2mac[bit]]);
1331 }
1332 if (first)
1333 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001334 tomoyo_print_name_union(head, &ptr->name1);
1335 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001336 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1337 struct tomoyo_path_number_acl *ptr =
1338 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001339 const u8 perm = ptr->perm;
1340 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
1341 if (!(perm & (1 << bit)))
1342 continue;
1343 if (first) {
1344 tomoyo_set_group(head, "file ");
1345 first = false;
1346 } else {
1347 tomoyo_set_slash(head);
1348 }
1349 tomoyo_set_string(head, tomoyo_mac_keywords
1350 [tomoyo_pn2mac[bit]]);
1351 }
1352 if (first)
1353 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001354 tomoyo_print_name_union(head, &ptr->name);
1355 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001356 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1357 struct tomoyo_mkdev_acl *ptr =
1358 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001359 const u8 perm = ptr->perm;
1360 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
1361 if (!(perm & (1 << bit)))
1362 continue;
1363 if (first) {
1364 tomoyo_set_group(head, "file ");
1365 first = false;
1366 } else {
1367 tomoyo_set_slash(head);
1368 }
1369 tomoyo_set_string(head, tomoyo_mac_keywords
1370 [tomoyo_pnnn2mac[bit]]);
1371 }
1372 if (first)
1373 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001374 tomoyo_print_name_union(head, &ptr->name);
1375 tomoyo_print_number_union(head, &ptr->mode);
1376 tomoyo_print_number_union(head, &ptr->major);
1377 tomoyo_print_number_union(head, &ptr->minor);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001378 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1379 struct tomoyo_mount_acl *ptr =
1380 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001381 tomoyo_set_group(head, "file mount");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001382 tomoyo_print_name_union(head, &ptr->dev_name);
1383 tomoyo_print_name_union(head, &ptr->dir_name);
1384 tomoyo_print_name_union(head, &ptr->fs_type);
1385 tomoyo_print_number_union(head, &ptr->flags);
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001386 } else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1387 struct tomoyo_env_acl *ptr =
1388 container_of(acl, typeof(*ptr), head);
1389
1390 tomoyo_set_group(head, "misc env ");
1391 tomoyo_set_string(head, ptr->env->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001392 }
Tetsuo Handa2066a362011-07-08 13:21:37 +09001393 if (acl->cond) {
1394 head->r.print_cond_part = true;
1395 head->r.cond_step = 0;
1396 if (!tomoyo_flush(head))
1397 return false;
1398print_cond_part:
1399 if (!tomoyo_print_condition(head, acl->cond))
1400 return false;
1401 head->r.print_cond_part = false;
1402 } else {
1403 tomoyo_set_lf(head);
1404 }
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001405 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001406}
1407
1408/**
1409 * tomoyo_read_domain2 - Read domain policy.
1410 *
Tetsuo Handa32997142011-06-26 23:19:28 +09001411 * @head: Pointer to "struct tomoyo_io_buffer".
1412 * @list: Pointer to "struct list_head".
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001413 *
1414 * Caller holds tomoyo_read_lock().
1415 *
1416 * Returns true on success, false otherwise.
1417 */
1418static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
Tetsuo Handa32997142011-06-26 23:19:28 +09001419 struct list_head *list)
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001420{
Tetsuo Handa32997142011-06-26 23:19:28 +09001421 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001422 struct tomoyo_acl_info *ptr =
1423 list_entry(head->r.acl, typeof(*ptr), list);
1424 if (!tomoyo_print_entry(head, ptr))
1425 return false;
1426 }
1427 head->r.acl = NULL;
1428 return true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001429}
1430
1431/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001432 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001433 *
1434 * @head: Pointer to "struct tomoyo_io_buffer".
1435 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001436 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001437 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001438static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001439{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001440 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001441 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001442 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001443 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001444 list_entry(head->r.domain, typeof(*domain), list);
1445 switch (head->r.step) {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001446 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001447 case 0:
1448 if (domain->is_deleted &&
1449 !head->r.print_this_domain_only)
1450 continue;
1451 /* Print domainname and flags. */
1452 tomoyo_set_string(head, domain->domainname->name);
1453 tomoyo_set_lf(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001454 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001455 domain->profile);
Tetsuo Handa32997142011-06-26 23:19:28 +09001456 tomoyo_io_printf(head, "use_group %u\n",
1457 domain->group);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001458 for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
1459 if (domain->flags[i])
1460 tomoyo_set_string(head, tomoyo_dif[i]);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001461 head->r.step++;
1462 tomoyo_set_lf(head);
1463 /* fall through */
1464 case 1:
Tetsuo Handa32997142011-06-26 23:19:28 +09001465 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001466 return;
1467 head->r.step++;
1468 if (!tomoyo_set_lf(head))
1469 return;
1470 /* fall through */
1471 case 2:
1472 head->r.step = 0;
1473 if (head->r.print_this_domain_only)
1474 goto done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001475 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001476 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001477 done:
1478 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001479}
1480
1481/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001482 * tomoyo_write_pid: Specify PID to obtain domainname.
1483 *
1484 * @head: Pointer to "struct tomoyo_io_buffer".
1485 *
1486 * Returns 0.
1487 */
1488static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1489{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001490 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001491 return 0;
1492}
1493
1494/**
1495 * tomoyo_read_pid - Get domainname of the specified PID.
1496 *
1497 * @head: Pointer to "struct tomoyo_io_buffer".
1498 *
1499 * Returns the domainname which the specified PID is in on success,
1500 * empty string otherwise.
1501 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1502 * using read()/write() interface rather than sysctl() interface.
1503 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001504static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001505{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001506 char *buf = head->write_buf;
1507 bool global_pid = false;
1508 unsigned int pid;
1509 struct task_struct *p;
1510 struct tomoyo_domain_info *domain = NULL;
1511
1512 /* Accessing write_buf is safe because head->io_sem is held. */
1513 if (!buf) {
1514 head->r.eof = true;
1515 return; /* Do nothing if open(O_RDONLY). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001516 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001517 if (head->r.w_pos || head->r.eof)
1518 return;
1519 head->r.eof = true;
1520 if (tomoyo_str_starts(&buf, "global-pid "))
1521 global_pid = true;
1522 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1523 rcu_read_lock();
1524 read_lock(&tasklist_lock);
1525 if (global_pid)
1526 p = find_task_by_pid_ns(pid, &init_pid_ns);
1527 else
1528 p = find_task_by_vpid(pid);
1529 if (p)
1530 domain = tomoyo_real_domain(p);
1531 read_unlock(&tasklist_lock);
1532 rcu_read_unlock();
1533 if (!domain)
1534 return;
1535 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1536 tomoyo_set_string(head, domain->domainname->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001537}
1538
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09001539/* String table for domain transition control keywords. */
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001540static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001541 [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
1542 [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
1543 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1544 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
1545 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
1546 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001547};
1548
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09001549/* String table for grouping keywords. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001550static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001551 [TOMOYO_PATH_GROUP] = "path_group ",
1552 [TOMOYO_NUMBER_GROUP] = "number_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001553};
1554
Kentaro Takeda95908372009-02-05 17:18:13 +09001555/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001556 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001557 *
1558 * @head: Pointer to "struct tomoyo_io_buffer".
1559 *
1560 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001561 *
1562 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001563 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001564static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001565{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001566 const bool is_delete = head->w.is_delete;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001567 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001568 .ns = head->w.ns,
1569 .is_delete = is_delete,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001570 .data = head->write_buf,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001571 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001572 u8 i;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001573 if (tomoyo_str_starts(&param.data, "aggregator "))
1574 return tomoyo_write_aggregator(&param);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001575 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001576 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1577 return tomoyo_write_transition_control(&param, i);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001578 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001579 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1580 return tomoyo_write_group(&param, i);
Tetsuo Handa32997142011-06-26 23:19:28 +09001581 if (tomoyo_str_starts(&param.data, "acl_group ")) {
1582 unsigned int group;
1583 char *data;
1584 group = simple_strtoul(param.data, &data, 10);
1585 if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001586 return tomoyo_write_domain2
1587 (head->w.ns, &head->w.ns->acl_group[group],
1588 data, is_delete);
Tetsuo Handa32997142011-06-26 23:19:28 +09001589 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001590 return -EINVAL;
1591}
1592
Tetsuo Handa31845e82010-06-17 16:54:33 +09001593/**
1594 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1595 *
1596 * @head: Pointer to "struct tomoyo_io_buffer".
1597 * @idx: Index number.
1598 *
1599 * Returns true on success, false otherwise.
1600 *
1601 * Caller holds tomoyo_read_lock().
1602 */
1603static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1604{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001605 struct tomoyo_policy_namespace *ns =
1606 container_of(head->r.ns, typeof(*ns), namespace_list);
1607 struct list_head *list = &ns->group_list[idx];
1608 list_for_each_cookie(head->r.group, list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001609 struct tomoyo_group *group =
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001610 list_entry(head->r.group, typeof(*group), head.list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001611 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001612 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001613 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001614 if (ptr->is_deleted)
1615 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001616 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001617 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001618 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001619 tomoyo_set_string(head, tomoyo_group_name[idx]);
1620 tomoyo_set_string(head, group->group_name->name);
1621 if (idx == TOMOYO_PATH_GROUP) {
1622 tomoyo_set_space(head);
1623 tomoyo_set_string(head, container_of
1624 (ptr, struct tomoyo_path_group,
1625 head)->member_name->name);
1626 } else if (idx == TOMOYO_NUMBER_GROUP) {
1627 tomoyo_print_number_union(head, &container_of
1628 (ptr,
1629 struct tomoyo_number_group,
1630 head)->number);
1631 }
1632 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001633 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001634 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001635 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001636 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001637 return true;
1638}
1639
1640/**
1641 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1642 *
1643 * @head: Pointer to "struct tomoyo_io_buffer".
1644 * @idx: Index number.
1645 *
1646 * Returns true on success, false otherwise.
1647 *
1648 * Caller holds tomoyo_read_lock().
1649 */
1650static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1651{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001652 struct tomoyo_policy_namespace *ns =
1653 container_of(head->r.ns, typeof(*ns), namespace_list);
1654 struct list_head *list = &ns->policy_list[idx];
1655 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001656 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001657 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001658 if (acl->is_deleted)
1659 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001660 if (!tomoyo_flush(head))
1661 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001662 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001663 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001664 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001665 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001666 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001667 tomoyo_print_namespace(head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001668 tomoyo_set_string(head, tomoyo_transition_type
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001669 [ptr->type]);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001670 tomoyo_set_string(head, ptr->program ?
1671 ptr->program->name : "any");
1672 tomoyo_set_string(head, " from ");
1673 tomoyo_set_string(head, ptr->domainname ?
1674 ptr->domainname->name :
1675 "any");
Tetsuo Handa31845e82010-06-17 16:54:33 +09001676 }
1677 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001678 case TOMOYO_ID_AGGREGATOR:
1679 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001680 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001681 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001682 tomoyo_print_namespace(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001683 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001684 tomoyo_set_string(head,
1685 ptr->original_name->name);
1686 tomoyo_set_space(head);
1687 tomoyo_set_string(head,
1688 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001689 }
1690 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001691 default:
1692 continue;
1693 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001694 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001695 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001696 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001697 return true;
1698}
1699
Kentaro Takeda95908372009-02-05 17:18:13 +09001700/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001701 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001702 *
1703 * @head: Pointer to "struct tomoyo_io_buffer".
1704 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001705 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001706 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001707static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001708{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001709 struct tomoyo_policy_namespace *ns =
1710 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001711 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001712 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001713 while (head->r.step < TOMOYO_MAX_POLICY &&
1714 tomoyo_read_policy(head, head->r.step))
1715 head->r.step++;
1716 if (head->r.step < TOMOYO_MAX_POLICY)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001717 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001718 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1719 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1720 head->r.step++;
1721 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001722 return;
Tetsuo Handa32997142011-06-26 23:19:28 +09001723 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
1724 + TOMOYO_MAX_ACL_GROUPS) {
1725 head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
1726 - TOMOYO_MAX_GROUP;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001727 if (!tomoyo_read_domain2(head, &ns->acl_group
Tetsuo Handa32997142011-06-26 23:19:28 +09001728 [head->r.acl_group_index]))
1729 return;
1730 head->r.step++;
1731 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001732 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001733}
1734
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001735/* Wait queue for kernel -> userspace notification. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001736static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001737/* Wait queue for userspace -> kernel notification. */
1738static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001739
1740/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001741struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001742 struct list_head list;
1743 char *query;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001744 size_t query_len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001745 unsigned int serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001746 u8 timer;
1747 u8 answer;
1748 u8 retry;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001749};
1750
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001751/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001752static LIST_HEAD(tomoyo_query_list);
1753
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001754/* Lock for manipulating tomoyo_query_list. */
1755static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1756
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001757/*
1758 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1759 * interface.
1760 */
1761static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1762
1763/**
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001764 * tomoyo_truncate - Truncate a line.
1765 *
1766 * @str: String to truncate.
1767 *
1768 * Returns length of truncated @str.
1769 */
1770static int tomoyo_truncate(char *str)
1771{
1772 char *start = str;
1773 while (*(unsigned char *) str > (unsigned char) ' ')
1774 str++;
1775 *str = '\0';
1776 return strlen(start) + 1;
1777}
1778
1779/**
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001780 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1781 *
1782 * @domain: Pointer to "struct tomoyo_domain_info".
1783 * @header: Lines containing ACL.
1784 *
1785 * Returns nothing.
1786 */
1787static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1788{
1789 char *buffer;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001790 char *realpath = NULL;
Tetsuo Handa5b636852011-07-08 13:24:54 +09001791 char *argv0 = NULL;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001792 char *symlink = NULL;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001793 char *cp = strchr(header, '\n');
1794 int len;
1795 if (!cp)
1796 return;
1797 cp = strchr(cp + 1, '\n');
1798 if (!cp)
1799 return;
1800 *cp++ = '\0';
1801 len = strlen(cp) + 1;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001802 /* strstr() will return NULL if ordering is wrong. */
1803 if (*cp == 'f') {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001804 argv0 = strstr(header, " argv[]={ \"");
1805 if (argv0) {
1806 argv0 += 10;
1807 len += tomoyo_truncate(argv0) + 14;
1808 }
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001809 realpath = strstr(header, " exec={ realpath=\"");
1810 if (realpath) {
1811 realpath += 8;
1812 len += tomoyo_truncate(realpath) + 6;
1813 }
1814 symlink = strstr(header, " symlink.target=\"");
1815 if (symlink)
1816 len += tomoyo_truncate(symlink + 1) + 1;
1817 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001818 buffer = kmalloc(len, GFP_NOFS);
1819 if (!buffer)
1820 return;
1821 snprintf(buffer, len - 1, "%s", cp);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001822 if (realpath)
1823 tomoyo_addprintf(buffer, len, " exec.%s", realpath);
Tetsuo Handa5b636852011-07-08 13:24:54 +09001824 if (argv0)
1825 tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001826 if (symlink)
1827 tomoyo_addprintf(buffer, len, "%s", symlink);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001828 tomoyo_normalize_line(buffer);
Tetsuo Handab22b8b92011-06-26 23:21:50 +09001829 if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1830 false))
1831 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001832 kfree(buffer);
1833}
1834
1835/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001836 * tomoyo_supervisor - Ask for the supervisor's decision.
1837 *
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001838 * @r: Pointer to "struct tomoyo_request_info".
1839 * @fmt: The printf()'s format string, followed by parameters.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001840 *
1841 * Returns 0 if the supervisor decided to permit the access request which
1842 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1843 * supervisor decided to retry the access request which violated the policy in
1844 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1845 */
1846int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1847{
1848 va_list args;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001849 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001850 int len;
1851 static unsigned int tomoyo_serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001852 struct tomoyo_query entry = { };
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001853 bool quota_exceeded = false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001854 va_start(args, fmt);
1855 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
1856 va_end(args);
1857 /* Write /sys/kernel/security/tomoyo/audit. */
1858 va_start(args, fmt);
1859 tomoyo_write_log2(r, len, fmt, args);
1860 va_end(args);
1861 /* Nothing more to do if granted. */
1862 if (r->granted)
1863 return 0;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09001864 if (r->mode)
1865 tomoyo_update_stat(r->mode);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001866 switch (r->mode) {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001867 case TOMOYO_CONFIG_ENFORCING:
1868 error = -EPERM;
1869 if (atomic_read(&tomoyo_query_observers))
1870 break;
1871 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001872 case TOMOYO_CONFIG_LEARNING:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001873 error = 0;
1874 /* Check max_learning_entry parameter. */
1875 if (tomoyo_domain_quota_is_ok(r))
1876 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001877 /* fall through */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001878 default:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001879 return 0;
1880 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001881 /* Get message. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001882 va_start(args, fmt);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001883 entry.query = tomoyo_init_log(r, len, fmt, args);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001884 va_end(args);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001885 if (!entry.query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001886 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001887 entry.query_len = strlen(entry.query) + 1;
1888 if (!error) {
1889 tomoyo_add_entry(r->domain, entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001890 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001891 }
1892 len = tomoyo_round2(entry.query_len);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001893 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001894 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
1895 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
1896 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001897 quota_exceeded = true;
1898 } else {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001899 entry.serial = tomoyo_serial++;
1900 entry.retry = r->retry;
1901 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
1902 list_add_tail(&entry.list, &tomoyo_query_list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001903 }
1904 spin_unlock(&tomoyo_query_list_lock);
1905 if (quota_exceeded)
1906 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001907 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001908 while (entry.timer < 10) {
1909 wake_up_all(&tomoyo_query_wait);
1910 if (wait_event_interruptible_timeout
1911 (tomoyo_answer_wait, entry.answer ||
1912 !atomic_read(&tomoyo_query_observers), HZ))
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001913 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001914 else
1915 entry.timer++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001916 }
1917 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001918 list_del(&entry.list);
1919 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001920 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001921 switch (entry.answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001922 case 3: /* Asked to retry by administrator. */
1923 error = TOMOYO_RETRY_REQUEST;
1924 r->retry++;
1925 break;
1926 case 1:
1927 /* Granted by administrator. */
1928 error = 0;
1929 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001930 default:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001931 /* Timed out or rejected by administrator. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001932 break;
1933 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001934out:
1935 kfree(entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001936 return error;
1937}
1938
1939/**
1940 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1941 *
1942 * @file: Pointer to "struct file".
1943 * @wait: Pointer to "poll_table".
1944 *
1945 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1946 *
1947 * Waits for access requests which violated policy in enforcing mode.
1948 */
1949static int tomoyo_poll_query(struct file *file, poll_table *wait)
1950{
1951 struct list_head *tmp;
1952 bool found = false;
1953 u8 i;
1954 for (i = 0; i < 2; i++) {
1955 spin_lock(&tomoyo_query_list_lock);
1956 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001957 struct tomoyo_query *ptr =
1958 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001959 if (ptr->answer)
1960 continue;
1961 found = true;
1962 break;
1963 }
1964 spin_unlock(&tomoyo_query_list_lock);
1965 if (found)
1966 return POLLIN | POLLRDNORM;
1967 if (i)
1968 break;
1969 poll_wait(file, &tomoyo_query_wait, wait);
1970 }
1971 return 0;
1972}
1973
1974/**
1975 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1976 *
1977 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001978 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001979static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001980{
1981 struct list_head *tmp;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001982 unsigned int pos = 0;
1983 size_t len = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001984 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001985 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001986 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001987 if (head->read_buf) {
1988 kfree(head->read_buf);
1989 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001990 }
1991 spin_lock(&tomoyo_query_list_lock);
1992 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001993 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001994 if (ptr->answer)
1995 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001996 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001997 continue;
1998 len = ptr->query_len;
1999 break;
2000 }
2001 spin_unlock(&tomoyo_query_list_lock);
2002 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002003 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002004 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002005 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002006 buf = kzalloc(len + 32, GFP_NOFS);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002007 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002008 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002009 pos = 0;
2010 spin_lock(&tomoyo_query_list_lock);
2011 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002012 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002013 if (ptr->answer)
2014 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002015 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002016 continue;
2017 /*
2018 * Some query can be skipped because tomoyo_query_list
2019 * can change, but I don't care.
2020 */
2021 if (len == ptr->query_len)
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002022 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2023 ptr->retry, ptr->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002024 break;
2025 }
2026 spin_unlock(&tomoyo_query_list_lock);
2027 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002028 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002029 head->r.w[head->r.w_pos++] = buf;
2030 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002031 } else {
2032 kfree(buf);
2033 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002034}
2035
2036/**
2037 * tomoyo_write_answer - Write the supervisor's decision.
2038 *
2039 * @head: Pointer to "struct tomoyo_io_buffer".
2040 *
2041 * Returns 0 on success, -EINVAL otherwise.
2042 */
2043static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
2044{
2045 char *data = head->write_buf;
2046 struct list_head *tmp;
2047 unsigned int serial;
2048 unsigned int answer;
2049 spin_lock(&tomoyo_query_list_lock);
2050 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002051 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002052 ptr->timer = 0;
2053 }
2054 spin_unlock(&tomoyo_query_list_lock);
2055 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2056 return -EINVAL;
2057 spin_lock(&tomoyo_query_list_lock);
2058 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002059 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002060 if (ptr->serial != serial)
2061 continue;
2062 if (!ptr->answer)
2063 ptr->answer = answer;
2064 break;
2065 }
2066 spin_unlock(&tomoyo_query_list_lock);
2067 return 0;
2068}
2069
2070/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002071 * tomoyo_read_version: Get version.
2072 *
2073 * @head: Pointer to "struct tomoyo_io_buffer".
2074 *
2075 * Returns version information.
2076 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002077static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002078{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002079 if (!head->r.eof) {
Tetsuo Handad5ca1722011-06-26 23:18:21 +09002080 tomoyo_io_printf(head, "2.4.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002081 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09002082 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002083}
2084
2085/**
2086 * tomoyo_read_self_domain - Get the current process's domainname.
2087 *
2088 * @head: Pointer to "struct tomoyo_io_buffer".
2089 *
2090 * Returns the current process's domainname.
2091 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002092static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002093{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002094 if (!head->r.eof) {
Kentaro Takeda95908372009-02-05 17:18:13 +09002095 /*
2096 * tomoyo_domain()->domainname != NULL
2097 * because every process belongs to a domain and
2098 * the domain's name cannot be NULL.
2099 */
2100 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002101 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09002102 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002103}
2104
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002105/* String table for /sys/kernel/security/tomoyo/stat interface. */
2106static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2107 [TOMOYO_STAT_POLICY_UPDATES] = "update:",
2108 [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:",
2109 [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2110 [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
2111};
2112
2113/* String table for /sys/kernel/security/tomoyo/stat interface. */
2114static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2115 [TOMOYO_MEMORY_POLICY] = "policy:",
2116 [TOMOYO_MEMORY_AUDIT] = "audit log:",
2117 [TOMOYO_MEMORY_QUERY] = "query message:",
2118};
2119
2120/* Timestamp counter for last updated. */
2121static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2122/* Counter for number of updates. */
2123static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2124
2125/**
2126 * tomoyo_update_stat - Update statistic counters.
2127 *
2128 * @index: Index for policy type.
2129 *
2130 * Returns nothing.
2131 */
2132void tomoyo_update_stat(const u8 index)
2133{
2134 struct timeval tv;
2135 do_gettimeofday(&tv);
2136 /*
2137 * I don't use atomic operations because race condition is not fatal.
2138 */
2139 tomoyo_stat_updated[index]++;
2140 tomoyo_stat_modified[index] = tv.tv_sec;
2141}
2142
2143/**
2144 * tomoyo_read_stat - Read statistic data.
2145 *
2146 * @head: Pointer to "struct tomoyo_io_buffer".
2147 *
2148 * Returns nothing.
2149 */
2150static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2151{
2152 u8 i;
2153 unsigned int total = 0;
2154 if (head->r.eof)
2155 return;
2156 for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2157 tomoyo_io_printf(head, "Policy %-30s %10u",
2158 tomoyo_policy_headers[i],
2159 tomoyo_stat_updated[i]);
2160 if (tomoyo_stat_modified[i]) {
2161 struct tomoyo_time stamp;
2162 tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2163 tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2164 "%02u:%02u:%02u)",
2165 stamp.year, stamp.month, stamp.day,
2166 stamp.hour, stamp.min, stamp.sec);
2167 }
2168 tomoyo_set_lf(head);
2169 }
2170 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2171 unsigned int used = tomoyo_memory_used[i];
2172 total += used;
2173 tomoyo_io_printf(head, "Memory used by %-22s %10u",
2174 tomoyo_memory_headers[i], used);
2175 used = tomoyo_memory_quota[i];
2176 if (used)
2177 tomoyo_io_printf(head, " (Quota: %10u)", used);
2178 tomoyo_set_lf(head);
2179 }
2180 tomoyo_io_printf(head, "Total memory used: %10u\n",
2181 total);
2182 head->r.eof = true;
2183}
2184
2185/**
2186 * tomoyo_write_stat - Set memory quota.
2187 *
2188 * @head: Pointer to "struct tomoyo_io_buffer".
2189 *
2190 * Returns 0.
2191 */
2192static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2193{
2194 char *data = head->write_buf;
2195 u8 i;
2196 if (tomoyo_str_starts(&data, "Memory used by "))
2197 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2198 if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2199 sscanf(data, "%u", &tomoyo_memory_quota[i]);
2200 return 0;
2201}
2202
Kentaro Takeda95908372009-02-05 17:18:13 +09002203/**
2204 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
2205 *
2206 * @type: Type of interface.
2207 * @file: Pointer to "struct file".
2208 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002209 * Returns 0 on success, negative value otherwise.
Kentaro Takeda95908372009-02-05 17:18:13 +09002210 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002211int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09002212{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002213 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002214
2215 if (!head)
2216 return -ENOMEM;
2217 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002218 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09002219 switch (type) {
2220 case TOMOYO_DOMAINPOLICY:
2221 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002222 head->write = tomoyo_write_domain;
2223 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09002224 break;
2225 case TOMOYO_EXCEPTIONPOLICY:
2226 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002227 head->write = tomoyo_write_exception;
2228 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09002229 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002230 case TOMOYO_AUDIT:
2231 /* /sys/kernel/security/tomoyo/audit */
2232 head->poll = tomoyo_poll_log;
2233 head->read = tomoyo_read_log;
2234 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002235 case TOMOYO_SELFDOMAIN:
2236 /* /sys/kernel/security/tomoyo/self_domain */
2237 head->read = tomoyo_read_self_domain;
2238 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002239 case TOMOYO_PROCESS_STATUS:
2240 /* /sys/kernel/security/tomoyo/.process_status */
2241 head->write = tomoyo_write_pid;
2242 head->read = tomoyo_read_pid;
2243 break;
2244 case TOMOYO_VERSION:
2245 /* /sys/kernel/security/tomoyo/version */
2246 head->read = tomoyo_read_version;
2247 head->readbuf_size = 128;
2248 break;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002249 case TOMOYO_STAT:
2250 /* /sys/kernel/security/tomoyo/stat */
2251 head->write = tomoyo_write_stat;
2252 head->read = tomoyo_read_stat;
2253 head->readbuf_size = 1024;
Kentaro Takeda95908372009-02-05 17:18:13 +09002254 break;
2255 case TOMOYO_PROFILE:
2256 /* /sys/kernel/security/tomoyo/profile */
2257 head->write = tomoyo_write_profile;
2258 head->read = tomoyo_read_profile;
2259 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002260 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
2261 head->poll = tomoyo_poll_query;
2262 head->write = tomoyo_write_answer;
2263 head->read = tomoyo_read_query;
2264 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002265 case TOMOYO_MANAGER:
2266 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002267 head->write = tomoyo_write_manager;
2268 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09002269 break;
2270 }
2271 if (!(file->f_mode & FMODE_READ)) {
2272 /*
2273 * No need to allocate read_buf since it is not opened
2274 * for reading.
2275 */
2276 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002277 head->poll = NULL;
2278 } else if (!head->poll) {
2279 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09002280 if (!head->readbuf_size)
2281 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002282 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002283 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002284 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002285 return -ENOMEM;
2286 }
2287 }
2288 if (!(file->f_mode & FMODE_WRITE)) {
2289 /*
2290 * No need to allocate write_buf since it is not opened
2291 * for writing.
2292 */
2293 head->write = NULL;
2294 } else if (head->write) {
2295 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002296 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002297 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002298 kfree(head->read_buf);
2299 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002300 return -ENOMEM;
2301 }
2302 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002303 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002304 * If the file is /sys/kernel/security/tomoyo/query , increment the
2305 * observer counter.
2306 * The obserber counter is used by tomoyo_supervisor() to see if
2307 * there is some process monitoring /sys/kernel/security/tomoyo/query.
2308 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09002309 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002310 atomic_inc(&tomoyo_query_observers);
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002311 file->private_data = head;
2312 tomoyo_notify_gc(head, true);
Kentaro Takeda95908372009-02-05 17:18:13 +09002313 return 0;
2314}
2315
2316/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09002317 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
2318 *
2319 * @file: Pointer to "struct file".
2320 * @wait: Pointer to "poll_table".
2321 *
2322 * Waits for read readiness.
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002323 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2324 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09002325 */
2326int tomoyo_poll_control(struct file *file, poll_table *wait)
2327{
2328 struct tomoyo_io_buffer *head = file->private_data;
2329 if (!head->poll)
2330 return -ENOSYS;
2331 return head->poll(file, wait);
2332}
2333
2334/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002335 * tomoyo_set_namespace_cursor - Set namespace to read.
2336 *
2337 * @head: Pointer to "struct tomoyo_io_buffer".
2338 *
2339 * Returns nothing.
2340 */
2341static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2342{
2343 struct list_head *ns;
2344 if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2345 head->type != TOMOYO_PROFILE)
2346 return;
2347 /*
2348 * If this is the first read, or reading previous namespace finished
2349 * and has more namespaces to read, update the namespace cursor.
2350 */
2351 ns = head->r.ns;
2352 if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2353 /* Clearing is OK because tomoyo_flush() returned true. */
2354 memset(&head->r, 0, sizeof(head->r));
2355 head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2356 }
2357}
2358
2359/**
2360 * tomoyo_has_more_namespace - Check for unread namespaces.
2361 *
2362 * @head: Pointer to "struct tomoyo_io_buffer".
2363 *
2364 * Returns true if we have more entries to print, false otherwise.
2365 */
2366static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2367{
2368 return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2369 head->type == TOMOYO_PROFILE) && head->r.eof &&
2370 head->r.ns->next != &tomoyo_namespace_list;
2371}
2372
2373/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002374 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2375 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002376 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002377 * @buffer: Poiner to buffer to write to.
2378 * @buffer_len: Size of @buffer.
2379 *
2380 * Returns bytes read on success, negative value otherwise.
2381 */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002382ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
2383 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002384{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002385 int len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002386 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002387
2388 if (!head->read)
2389 return -ENOSYS;
2390 if (mutex_lock_interruptible(&head->io_sem))
2391 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002392 head->read_user_buf = buffer;
2393 head->read_user_buf_avail = buffer_len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002394 idx = tomoyo_read_lock();
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002395 if (tomoyo_flush(head))
2396 /* Call the policy handler. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002397 do {
2398 tomoyo_set_namespace_cursor(head);
2399 head->read(head);
2400 } while (tomoyo_flush(head) &&
2401 tomoyo_has_more_namespace(head));
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002402 tomoyo_read_unlock(idx);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002403 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09002404 mutex_unlock(&head->io_sem);
2405 return len;
2406}
2407
2408/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002409 * tomoyo_parse_policy - Parse a policy line.
2410 *
2411 * @head: Poiter to "struct tomoyo_io_buffer".
2412 * @line: Line to parse.
2413 *
2414 * Returns 0 on success, negative value otherwise.
2415 *
2416 * Caller holds tomoyo_read_lock().
2417 */
2418static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2419{
2420 /* Delete request? */
2421 head->w.is_delete = !strncmp(line, "delete ", 7);
2422 if (head->w.is_delete)
2423 memmove(line, line + 7, strlen(line + 7) + 1);
2424 /* Selecting namespace to update. */
2425 if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2426 head->type == TOMOYO_PROFILE) {
2427 if (*line == '<') {
2428 char *cp = strchr(line, ' ');
2429 if (cp) {
2430 *cp++ = '\0';
2431 head->w.ns = tomoyo_assign_namespace(line);
2432 memmove(line, cp, strlen(cp) + 1);
2433 } else
2434 head->w.ns = NULL;
2435 } else
2436 head->w.ns = &tomoyo_kernel_namespace;
2437 /* Don't allow updating if namespace is invalid. */
2438 if (!head->w.ns)
2439 return -ENOENT;
2440 }
2441 /* Do the update. */
2442 return head->write(head);
2443}
2444
2445/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002446 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2447 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002448 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002449 * @buffer: Pointer to buffer to read from.
2450 * @buffer_len: Size of @buffer.
2451 *
2452 * Returns @buffer_len on success, negative value otherwise.
2453 */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002454ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
2455 const char __user *buffer, const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002456{
Kentaro Takeda95908372009-02-05 17:18:13 +09002457 int error = buffer_len;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002458 size_t avail_len = buffer_len;
Kentaro Takeda95908372009-02-05 17:18:13 +09002459 char *cp0 = head->write_buf;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002460 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002461 if (!head->write)
2462 return -ENOSYS;
2463 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2464 return -EFAULT;
Kentaro Takeda95908372009-02-05 17:18:13 +09002465 if (mutex_lock_interruptible(&head->io_sem))
2466 return -EINTR;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002467 idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09002468 /* Read a line and dispatch it to the policy handler. */
2469 while (avail_len > 0) {
2470 char c;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002471 if (head->w.avail >= head->writebuf_size - 1) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002472 const int len = head->writebuf_size * 2;
2473 char *cp = kzalloc(len, GFP_NOFS);
2474 if (!cp) {
2475 error = -ENOMEM;
2476 break;
2477 }
2478 memmove(cp, cp0, head->w.avail);
2479 kfree(cp0);
2480 head->write_buf = cp;
2481 cp0 = cp;
2482 head->writebuf_size = len;
2483 }
2484 if (get_user(c, buffer)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09002485 error = -EFAULT;
2486 break;
2487 }
2488 buffer++;
2489 avail_len--;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002490 cp0[head->w.avail++] = c;
Kentaro Takeda95908372009-02-05 17:18:13 +09002491 if (c != '\n')
2492 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002493 cp0[head->w.avail - 1] = '\0';
2494 head->w.avail = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09002495 tomoyo_normalize_line(cp0);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002496 if (!strcmp(cp0, "reset")) {
2497 head->w.ns = &tomoyo_kernel_namespace;
2498 head->w.domain = NULL;
2499 memset(&head->r, 0, sizeof(head->r));
2500 continue;
2501 }
2502 /* Don't allow updating policies by non manager programs. */
2503 switch (head->type) {
2504 case TOMOYO_PROCESS_STATUS:
2505 /* This does not write anything. */
2506 break;
2507 case TOMOYO_DOMAINPOLICY:
2508 if (tomoyo_select_domain(head, cp0))
2509 continue;
2510 /* fall through */
2511 case TOMOYO_EXCEPTIONPOLICY:
2512 if (!strcmp(cp0, "select transition_only")) {
2513 head->r.print_transition_related_only = true;
2514 continue;
2515 }
2516 /* fall through */
2517 default:
2518 if (!tomoyo_manager()) {
2519 error = -EPERM;
2520 goto out;
2521 }
2522 }
2523 switch (tomoyo_parse_policy(head, cp0)) {
2524 case -EPERM:
2525 error = -EPERM;
2526 goto out;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002527 case 0:
2528 switch (head->type) {
2529 case TOMOYO_DOMAINPOLICY:
2530 case TOMOYO_EXCEPTIONPOLICY:
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002531 case TOMOYO_STAT:
2532 case TOMOYO_PROFILE:
2533 case TOMOYO_MANAGER:
2534 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2535 break;
2536 default:
2537 break;
2538 }
2539 break;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002540 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002541 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002542out:
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002543 tomoyo_read_unlock(idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002544 mutex_unlock(&head->io_sem);
2545 return error;
2546}
2547
2548/**
2549 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2550 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002551 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002552 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002553 * Returns 0.
Kentaro Takeda95908372009-02-05 17:18:13 +09002554 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002555int tomoyo_close_control(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002556{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002557 /*
2558 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2559 * observer counter.
2560 */
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002561 if (head->type == TOMOYO_QUERY &&
2562 atomic_dec_and_test(&tomoyo_query_observers))
2563 wake_up_all(&tomoyo_answer_wait);
2564 tomoyo_notify_gc(head, false);
Kentaro Takeda95908372009-02-05 17:18:13 +09002565 return 0;
2566}
2567
2568/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002569 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09002570 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002571void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09002572{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002573 struct tomoyo_domain_info *domain;
2574 const int idx = tomoyo_read_lock();
2575 tomoyo_policy_loaded = true;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002576 printk(KERN_INFO "TOMOYO: 2.4.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002577 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2578 const u8 profile = domain->profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002579 const struct tomoyo_policy_namespace *ns = domain->ns;
2580 if (ns->profile_version != 20100903)
2581 printk(KERN_ERR
2582 "Profile version %u is not supported.\n",
2583 ns->profile_version);
2584 else if (!ns->profile_ptr[profile])
2585 printk(KERN_ERR
2586 "Profile %u (used by '%s') is not defined.\n",
2587 profile, domain->domainname->name);
2588 else
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002589 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002590 printk(KERN_ERR
2591 "Userland tools for TOMOYO 2.4 must be installed and "
2592 "policy must be initialized.\n");
2593 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ "
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002594 "for more information.\n");
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002595 panic("STOP!");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002596 }
2597 tomoyo_read_unlock(idx);
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002598 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002599}
Tetsuo Handaefe836a2011-06-26 23:22:18 +09002600
2601/**
2602 * tomoyo_load_builtin_policy - Load built-in policy.
2603 *
2604 * Returns nothing.
2605 */
2606void __init tomoyo_load_builtin_policy(void)
2607{
2608 /*
2609 * This include file is manually created and contains built-in policy
2610 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2611 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2612 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2613 */
2614#include "builtin-policy.h"
2615 u8 i;
2616 const int idx = tomoyo_read_lock();
2617 for (i = 0; i < 5; i++) {
2618 struct tomoyo_io_buffer head = { };
2619 char *start = "";
2620 switch (i) {
2621 case 0:
2622 start = tomoyo_builtin_profile;
2623 head.type = TOMOYO_PROFILE;
2624 head.write = tomoyo_write_profile;
2625 break;
2626 case 1:
2627 start = tomoyo_builtin_exception_policy;
2628 head.type = TOMOYO_EXCEPTIONPOLICY;
2629 head.write = tomoyo_write_exception;
2630 break;
2631 case 2:
2632 start = tomoyo_builtin_domain_policy;
2633 head.type = TOMOYO_DOMAINPOLICY;
2634 head.write = tomoyo_write_domain;
2635 break;
2636 case 3:
2637 start = tomoyo_builtin_manager;
2638 head.type = TOMOYO_MANAGER;
2639 head.write = tomoyo_write_manager;
2640 break;
2641 case 4:
2642 start = tomoyo_builtin_stat;
2643 head.type = TOMOYO_STAT;
2644 head.write = tomoyo_write_stat;
2645 break;
2646 }
2647 while (1) {
2648 char *end = strchr(start, '\n');
2649 if (!end)
2650 break;
2651 *end = '\0';
2652 tomoyo_normalize_line(start);
2653 head.write_buf = start;
2654 tomoyo_parse_policy(&head, start);
2655 start = end + 1;
2656 }
2657 }
2658 tomoyo_read_unlock(idx);
Tetsuo Handa0e4ae0e2011-06-26 23:22:59 +09002659#ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
2660 tomoyo_check_profile();
2661#endif
Tetsuo Handaefe836a2011-06-26 23:22:18 +09002662}