blob: 084018351b4f0b9979b2f2f8eddeccfa2ca410a1 [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 Handa059d84d2011-09-10 15:23:54 +090047 /* CONFIG::network group */
48 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND] = "inet_stream_bind",
49 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN] = "inet_stream_listen",
50 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT] = "inet_stream_connect",
51 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND] = "inet_dgram_bind",
52 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND] = "inet_dgram_send",
53 [TOMOYO_MAC_NETWORK_INET_RAW_BIND] = "inet_raw_bind",
54 [TOMOYO_MAC_NETWORK_INET_RAW_SEND] = "inet_raw_send",
55 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND] = "unix_stream_bind",
56 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN] = "unix_stream_listen",
57 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT] = "unix_stream_connect",
58 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND] = "unix_dgram_bind",
59 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND] = "unix_dgram_send",
60 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND] = "unix_seqpacket_bind",
61 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = "unix_seqpacket_listen",
62 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
Tetsuo Handad58e0da2011-09-10 15:22:48 +090063 /* CONFIG::misc group */
64 [TOMOYO_MAC_ENVIRON] = "env",
65 /* CONFIG group */
Tetsuo Handa57c25902010-06-03 20:38:44 +090066 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Tetsuo Handa059d84d2011-09-10 15:23:54 +090067 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_NETWORK] = "network",
Tetsuo Handad58e0da2011-09-10 15:22:48 +090068 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_MISC] = "misc",
Kentaro Takeda95908372009-02-05 17:18:13 +090069};
70
Tetsuo Handa2066a362011-07-08 13:21:37 +090071/* String table for conditions. */
72const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = {
73 [TOMOYO_TASK_UID] = "task.uid",
74 [TOMOYO_TASK_EUID] = "task.euid",
75 [TOMOYO_TASK_SUID] = "task.suid",
76 [TOMOYO_TASK_FSUID] = "task.fsuid",
77 [TOMOYO_TASK_GID] = "task.gid",
78 [TOMOYO_TASK_EGID] = "task.egid",
79 [TOMOYO_TASK_SGID] = "task.sgid",
80 [TOMOYO_TASK_FSGID] = "task.fsgid",
81 [TOMOYO_TASK_PID] = "task.pid",
82 [TOMOYO_TASK_PPID] = "task.ppid",
Tetsuo Handa5b636852011-07-08 13:24:54 +090083 [TOMOYO_EXEC_ARGC] = "exec.argc",
84 [TOMOYO_EXEC_ENVC] = "exec.envc",
Tetsuo Handa8761afd2011-07-08 13:22:41 +090085 [TOMOYO_TYPE_IS_SOCKET] = "socket",
86 [TOMOYO_TYPE_IS_SYMLINK] = "symlink",
87 [TOMOYO_TYPE_IS_FILE] = "file",
88 [TOMOYO_TYPE_IS_BLOCK_DEV] = "block",
89 [TOMOYO_TYPE_IS_DIRECTORY] = "directory",
90 [TOMOYO_TYPE_IS_CHAR_DEV] = "char",
91 [TOMOYO_TYPE_IS_FIFO] = "fifo",
92 [TOMOYO_MODE_SETUID] = "setuid",
93 [TOMOYO_MODE_SETGID] = "setgid",
94 [TOMOYO_MODE_STICKY] = "sticky",
95 [TOMOYO_MODE_OWNER_READ] = "owner_read",
96 [TOMOYO_MODE_OWNER_WRITE] = "owner_write",
97 [TOMOYO_MODE_OWNER_EXECUTE] = "owner_execute",
98 [TOMOYO_MODE_GROUP_READ] = "group_read",
99 [TOMOYO_MODE_GROUP_WRITE] = "group_write",
100 [TOMOYO_MODE_GROUP_EXECUTE] = "group_execute",
101 [TOMOYO_MODE_OTHERS_READ] = "others_read",
102 [TOMOYO_MODE_OTHERS_WRITE] = "others_write",
103 [TOMOYO_MODE_OTHERS_EXECUTE] = "others_execute",
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +0900104 [TOMOYO_EXEC_REALPATH] = "exec.realpath",
105 [TOMOYO_SYMLINK_TARGET] = "symlink.target",
Tetsuo Handa8761afd2011-07-08 13:22:41 +0900106 [TOMOYO_PATH1_UID] = "path1.uid",
107 [TOMOYO_PATH1_GID] = "path1.gid",
108 [TOMOYO_PATH1_INO] = "path1.ino",
109 [TOMOYO_PATH1_MAJOR] = "path1.major",
110 [TOMOYO_PATH1_MINOR] = "path1.minor",
111 [TOMOYO_PATH1_PERM] = "path1.perm",
112 [TOMOYO_PATH1_TYPE] = "path1.type",
113 [TOMOYO_PATH1_DEV_MAJOR] = "path1.dev_major",
114 [TOMOYO_PATH1_DEV_MINOR] = "path1.dev_minor",
115 [TOMOYO_PATH2_UID] = "path2.uid",
116 [TOMOYO_PATH2_GID] = "path2.gid",
117 [TOMOYO_PATH2_INO] = "path2.ino",
118 [TOMOYO_PATH2_MAJOR] = "path2.major",
119 [TOMOYO_PATH2_MINOR] = "path2.minor",
120 [TOMOYO_PATH2_PERM] = "path2.perm",
121 [TOMOYO_PATH2_TYPE] = "path2.type",
122 [TOMOYO_PATH2_DEV_MAJOR] = "path2.dev_major",
123 [TOMOYO_PATH2_DEV_MINOR] = "path2.dev_minor",
124 [TOMOYO_PATH1_PARENT_UID] = "path1.parent.uid",
125 [TOMOYO_PATH1_PARENT_GID] = "path1.parent.gid",
126 [TOMOYO_PATH1_PARENT_INO] = "path1.parent.ino",
127 [TOMOYO_PATH1_PARENT_PERM] = "path1.parent.perm",
128 [TOMOYO_PATH2_PARENT_UID] = "path2.parent.uid",
129 [TOMOYO_PATH2_PARENT_GID] = "path2.parent.gid",
130 [TOMOYO_PATH2_PARENT_INO] = "path2.parent.ino",
131 [TOMOYO_PATH2_PARENT_PERM] = "path2.parent.perm",
Tetsuo Handa2066a362011-07-08 13:21:37 +0900132};
133
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900134/* String table for PREFERENCE keyword. */
135static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900136 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900137 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
138};
139
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900140/* String table for path operation. */
141const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
142 [TOMOYO_TYPE_EXECUTE] = "execute",
143 [TOMOYO_TYPE_READ] = "read",
144 [TOMOYO_TYPE_WRITE] = "write",
145 [TOMOYO_TYPE_APPEND] = "append",
146 [TOMOYO_TYPE_UNLINK] = "unlink",
147 [TOMOYO_TYPE_GETATTR] = "getattr",
148 [TOMOYO_TYPE_RMDIR] = "rmdir",
149 [TOMOYO_TYPE_TRUNCATE] = "truncate",
150 [TOMOYO_TYPE_SYMLINK] = "symlink",
151 [TOMOYO_TYPE_CHROOT] = "chroot",
152 [TOMOYO_TYPE_UMOUNT] = "unmount",
153};
154
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900155/* String table for socket's operation. */
156const char * const tomoyo_socket_keyword[TOMOYO_MAX_NETWORK_OPERATION] = {
157 [TOMOYO_NETWORK_BIND] = "bind",
158 [TOMOYO_NETWORK_LISTEN] = "listen",
159 [TOMOYO_NETWORK_CONNECT] = "connect",
160 [TOMOYO_NETWORK_SEND] = "send",
161};
162
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900163/* String table for categories. */
164static const char * const tomoyo_category_keywords
165[TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900166 [TOMOYO_MAC_CATEGORY_FILE] = "file",
167 [TOMOYO_MAC_CATEGORY_NETWORK] = "network",
168 [TOMOYO_MAC_CATEGORY_MISC] = "misc",
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900169};
170
Kentaro Takeda95908372009-02-05 17:18:13 +0900171/* Permit policy management by non-root user? */
172static bool tomoyo_manage_by_non_root;
173
174/* Utility functions. */
175
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900176/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900177 * tomoyo_yesno - Return "yes" or "no".
178 *
179 * @value: Bool value.
180 */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900181const char *tomoyo_yesno(const unsigned int value)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900182{
183 return value ? "yes" : "no";
184}
185
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900186/**
187 * tomoyo_addprintf - strncat()-like-snprintf().
188 *
189 * @buffer: Buffer to write to. Must be '\0'-terminated.
190 * @len: Size of @buffer.
191 * @fmt: The printf()'s format string, followed by parameters.
192 *
193 * Returns nothing.
194 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900195static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
196{
197 va_list args;
198 const int pos = strlen(buffer);
199 va_start(args, fmt);
200 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
201 va_end(args);
202}
203
204/**
205 * tomoyo_flush - Flush queued string to userspace's buffer.
206 *
207 * @head: Pointer to "struct tomoyo_io_buffer".
208 *
209 * Returns true if all data was flushed, false otherwise.
210 */
211static bool tomoyo_flush(struct tomoyo_io_buffer *head)
212{
213 while (head->r.w_pos) {
214 const char *w = head->r.w[0];
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900215 size_t len = strlen(w);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900216 if (len) {
217 if (len > head->read_user_buf_avail)
218 len = head->read_user_buf_avail;
219 if (!len)
220 return false;
221 if (copy_to_user(head->read_user_buf, w, len))
222 return false;
223 head->read_user_buf_avail -= len;
224 head->read_user_buf += len;
225 w += len;
226 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900227 head->r.w[0] = w;
228 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900229 return false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900230 /* Add '\0' for audit logs and query. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900231 if (head->poll) {
232 if (!head->read_user_buf_avail ||
233 copy_to_user(head->read_user_buf, "", 1))
234 return false;
235 head->read_user_buf_avail--;
236 head->read_user_buf++;
237 }
238 head->r.w_pos--;
239 for (len = 0; len < head->r.w_pos; len++)
240 head->r.w[len] = head->r.w[len + 1];
241 }
242 head->r.avail = 0;
243 return true;
244}
245
246/**
247 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
248 *
249 * @head: Pointer to "struct tomoyo_io_buffer".
250 * @string: String to print.
251 *
252 * Note that @string has to be kept valid until @head is kfree()d.
253 * This means that char[] allocated on stack memory cannot be passed to
254 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
255 */
256static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
257{
258 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
259 head->r.w[head->r.w_pos++] = string;
260 tomoyo_flush(head);
261 } else
262 WARN_ON(1);
263}
264
265/**
266 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
267 *
268 * @head: Pointer to "struct tomoyo_io_buffer".
269 * @fmt: The printf()'s format string, followed by parameters.
270 */
271void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
272{
273 va_list args;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900274 size_t len;
275 size_t pos = head->r.avail;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900276 int size = head->readbuf_size - pos;
277 if (size <= 0)
278 return;
279 va_start(args, fmt);
280 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
281 va_end(args);
282 if (pos + len >= head->readbuf_size) {
283 WARN_ON(1);
284 return;
285 }
286 head->r.avail += len;
287 tomoyo_set_string(head, head->read_buf + pos);
288}
289
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900290/**
291 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
292 *
293 * @head: Pointer to "struct tomoyo_io_buffer".
294 *
295 * Returns nothing.
296 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900297static void tomoyo_set_space(struct tomoyo_io_buffer *head)
298{
299 tomoyo_set_string(head, " ");
300}
301
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900302/**
303 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
304 *
305 * @head: Pointer to "struct tomoyo_io_buffer".
306 *
307 * Returns nothing.
308 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900309static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
310{
311 tomoyo_set_string(head, "\n");
312 return !head->r.w_pos;
313}
314
Tetsuo Handa57c25902010-06-03 20:38:44 +0900315/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900316 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
317 *
318 * @head: Pointer to "struct tomoyo_io_buffer".
319 *
320 * Returns nothing.
321 */
322static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
323{
324 tomoyo_set_string(head, "/");
325}
326
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900327/* List of namespaces. */
328LIST_HEAD(tomoyo_namespace_list);
329/* True if namespace other than tomoyo_kernel_namespace is defined. */
330static bool tomoyo_namespace_enabled;
331
332/**
333 * tomoyo_init_policy_namespace - Initialize namespace.
334 *
335 * @ns: Pointer to "struct tomoyo_policy_namespace".
336 *
337 * Returns nothing.
338 */
339void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
340{
341 unsigned int idx;
342 for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
343 INIT_LIST_HEAD(&ns->acl_group[idx]);
344 for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
345 INIT_LIST_HEAD(&ns->group_list[idx]);
346 for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
347 INIT_LIST_HEAD(&ns->policy_list[idx]);
Tetsuo Handa843d1832011-09-14 17:03:19 +0900348 ns->profile_version = 20110903;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900349 tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
350 list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
351}
352
353/**
354 * tomoyo_print_namespace - Print namespace header.
355 *
356 * @head: Pointer to "struct tomoyo_io_buffer".
357 *
358 * Returns nothing.
359 */
360static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
361{
362 if (!tomoyo_namespace_enabled)
363 return;
364 tomoyo_set_string(head,
365 container_of(head->r.ns,
366 struct tomoyo_policy_namespace,
367 namespace_list)->name);
368 tomoyo_set_space(head);
369}
370
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900371/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900372 * tomoyo_print_name_union - Print a tomoyo_name_union.
373 *
374 * @head: Pointer to "struct tomoyo_io_buffer".
375 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900376 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900377static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
378 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900379{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900380 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900381 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900382 tomoyo_set_string(head, "@");
383 tomoyo_set_string(head, ptr->group->group_name->name);
384 } else {
385 tomoyo_set_string(head, ptr->filename->name);
386 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900387}
388
389/**
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +0900390 * tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
391 *
392 * @head: Pointer to "struct tomoyo_io_buffer".
393 * @ptr: Pointer to "struct tomoyo_name_union".
394 *
395 * Returns nothing.
396 */
397static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
398 const struct tomoyo_name_union *ptr)
399{
400 if (ptr->group) {
401 tomoyo_set_string(head, "@");
402 tomoyo_set_string(head, ptr->group->group_name->name);
403 } else {
404 tomoyo_set_string(head, "\"");
405 tomoyo_set_string(head, ptr->filename->name);
406 tomoyo_set_string(head, "\"");
407 }
408}
409
410/**
Tetsuo Handa2066a362011-07-08 13:21:37 +0900411 * tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900412 *
Tetsuo Handa2066a362011-07-08 13:21:37 +0900413 * @head: Pointer to "struct tomoyo_io_buffer".
414 * @ptr: Pointer to "struct tomoyo_number_union".
415 *
416 * Returns nothing.
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900417 */
Tetsuo Handa2066a362011-07-08 13:21:37 +0900418static void tomoyo_print_number_union_nospace
419(struct tomoyo_io_buffer *head, const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900420{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900421 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900422 tomoyo_set_string(head, "@");
423 tomoyo_set_string(head, ptr->group->group_name->name);
424 } else {
425 int i;
426 unsigned long min = ptr->values[0];
427 const unsigned long max = ptr->values[1];
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900428 u8 min_type = ptr->value_type[0];
429 const u8 max_type = ptr->value_type[1];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900430 char buffer[128];
431 buffer[0] = '\0';
432 for (i = 0; i < 2; i++) {
433 switch (min_type) {
434 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
435 tomoyo_addprintf(buffer, sizeof(buffer),
436 "0x%lX", min);
437 break;
438 case TOMOYO_VALUE_TYPE_OCTAL:
439 tomoyo_addprintf(buffer, sizeof(buffer),
440 "0%lo", min);
441 break;
442 default:
Tetsuo Handa2066a362011-07-08 13:21:37 +0900443 tomoyo_addprintf(buffer, sizeof(buffer), "%lu",
444 min);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900445 break;
446 }
447 if (min == max && min_type == max_type)
448 break;
449 tomoyo_addprintf(buffer, sizeof(buffer), "-");
450 min_type = max_type;
451 min = max;
452 }
453 tomoyo_io_printf(head, "%s", buffer);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900454 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900455}
456
457/**
Tetsuo Handa2066a362011-07-08 13:21:37 +0900458 * tomoyo_print_number_union - Print a tomoyo_number_union.
459 *
460 * @head: Pointer to "struct tomoyo_io_buffer".
461 * @ptr: Pointer to "struct tomoyo_number_union".
462 *
463 * Returns nothing.
464 */
465static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
466 const struct tomoyo_number_union *ptr)
467{
468 tomoyo_set_space(head);
469 tomoyo_print_number_union_nospace(head, ptr);
470}
471
472/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900473 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900474 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900475 * @ns: Pointer to "struct tomoyo_policy_namespace".
Kentaro Takeda95908372009-02-05 17:18:13 +0900476 * @profile: Profile number to create.
477 *
478 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
479 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900480static struct tomoyo_profile *tomoyo_assign_profile
481(struct tomoyo_policy_namespace *ns, const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900482{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900483 struct tomoyo_profile *ptr;
484 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900485 if (profile >= TOMOYO_MAX_PROFILES)
486 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900487 ptr = ns->profile_ptr[profile];
Kentaro Takeda95908372009-02-05 17:18:13 +0900488 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900489 return ptr;
490 entry = kzalloc(sizeof(*entry), GFP_NOFS);
491 if (mutex_lock_interruptible(&tomoyo_policy_lock))
492 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900493 ptr = ns->profile_ptr[profile];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900494 if (!ptr && tomoyo_memory_ok(entry)) {
495 ptr = entry;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900496 ptr->default_config = TOMOYO_CONFIG_DISABLED |
497 TOMOYO_CONFIG_WANT_GRANT_LOG |
498 TOMOYO_CONFIG_WANT_REJECT_LOG;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900499 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
500 sizeof(ptr->config));
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900501 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900502 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900503 mb(); /* Avoid out-of-order execution. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900504 ns->profile_ptr[profile] = ptr;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900505 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900506 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900507 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900508 out:
509 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900510 return ptr;
511}
512
513/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900514 * tomoyo_profile - Find a profile.
515 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900516 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900517 * @profile: Profile number to find.
518 *
519 * Returns pointer to "struct tomoyo_profile".
520 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900521struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
522 const u8 profile)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900523{
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900524 static struct tomoyo_profile tomoyo_null_profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900525 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900526 if (!ptr)
527 ptr = &tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900528 return ptr;
529}
530
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900531/**
532 * tomoyo_find_yesno - Find values for specified keyword.
533 *
534 * @string: String to check.
535 * @find: Name of keyword.
536 *
537 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
538 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900539static s8 tomoyo_find_yesno(const char *string, const char *find)
540{
541 const char *cp = strstr(string, find);
542 if (cp) {
543 cp += strlen(find);
544 if (!strncmp(cp, "=yes", 4))
545 return 1;
546 else if (!strncmp(cp, "=no", 3))
547 return 0;
548 }
549 return -1;
550}
551
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900552/**
553 * tomoyo_set_uint - Set value for specified preference.
554 *
555 * @i: Pointer to "unsigned int".
556 * @string: String to check.
557 * @find: Name of keyword.
558 *
559 * Returns nothing.
560 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900561static void tomoyo_set_uint(unsigned int *i, const char *string,
562 const char *find)
563{
564 const char *cp = strstr(string, find);
565 if (cp)
566 sscanf(cp + strlen(find), "=%u", i);
567}
568
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900569/**
570 * tomoyo_set_mode - Set mode for specified profile.
571 *
572 * @name: Name of functionality.
573 * @value: Mode for @name.
574 * @profile: Pointer to "struct tomoyo_profile".
575 *
576 * Returns 0 on success, negative value otherwise.
577 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900578static int tomoyo_set_mode(char *name, const char *value,
Tetsuo Handa8e568682010-06-25 09:30:09 +0900579 struct tomoyo_profile *profile)
580{
581 u8 i;
582 u8 config;
583 if (!strcmp(name, "CONFIG")) {
584 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
585 config = profile->default_config;
586 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
587 config = 0;
588 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
589 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900590 int len = 0;
591 if (i < TOMOYO_MAX_MAC_INDEX) {
592 const u8 c = tomoyo_index2category[i];
593 const char *category =
594 tomoyo_category_keywords[c];
595 len = strlen(category);
596 if (strncmp(name, category, len) ||
597 name[len++] != ':' || name[len++] != ':')
598 continue;
599 }
600 if (strcmp(name + len, tomoyo_mac_keywords[i]))
Tetsuo Handa8e568682010-06-25 09:30:09 +0900601 continue;
602 config = profile->config[i];
603 break;
604 }
605 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
606 return -EINVAL;
607 } else {
608 return -EINVAL;
609 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900610 if (strstr(value, "use_default")) {
Tetsuo Handa8e568682010-06-25 09:30:09 +0900611 config = TOMOYO_CONFIG_USE_DEFAULT;
612 } else {
613 u8 mode;
614 for (mode = 0; mode < 4; mode++)
615 if (strstr(value, tomoyo_mode[mode]))
616 /*
617 * Update lower 3 bits in order to distinguish
618 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
619 */
620 config = (config & ~7) | mode;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900621 if (config != TOMOYO_CONFIG_USE_DEFAULT) {
622 switch (tomoyo_find_yesno(value, "grant_log")) {
623 case 1:
624 config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
625 break;
626 case 0:
627 config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
628 break;
629 }
630 switch (tomoyo_find_yesno(value, "reject_log")) {
631 case 1:
632 config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
633 break;
634 case 0:
635 config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
636 break;
637 }
638 }
Tetsuo Handa8e568682010-06-25 09:30:09 +0900639 }
640 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
641 profile->config[i] = config;
642 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
643 profile->default_config = config;
644 return 0;
645}
646
Tetsuo Handa57c25902010-06-03 20:38:44 +0900647/**
648 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900649 *
650 * @head: Pointer to "struct tomoyo_io_buffer".
651 *
652 * Returns 0 on success, negative value otherwise.
653 */
654static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
655{
656 char *data = head->write_buf;
657 unsigned int i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900658 char *cp;
659 struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900660 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
661 == 1)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900662 return 0;
663 i = simple_strtoul(data, &cp, 10);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900664 if (*cp != '-')
665 return -EINVAL;
666 data = cp + 1;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900667 profile = tomoyo_assign_profile(head->w.ns, i);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900668 if (!profile)
669 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900670 cp = strchr(data, '=');
671 if (!cp)
672 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900673 *cp++ = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +0900674 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900675 static DEFINE_SPINLOCK(lock);
676 const struct tomoyo_path_info *new_comment
677 = tomoyo_get_name(cp);
678 const struct tomoyo_path_info *old_comment;
679 if (!new_comment)
680 return -ENOMEM;
681 spin_lock(&lock);
682 old_comment = profile->comment;
683 profile->comment = new_comment;
684 spin_unlock(&lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900685 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900686 return 0;
687 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900688 if (!strcmp(data, "PREFERENCE")) {
689 for (i = 0; i < TOMOYO_MAX_PREF; i++)
690 tomoyo_set_uint(&profile->pref[i], cp,
691 tomoyo_pref_keywords[i]);
692 return 0;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900693 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900694 return tomoyo_set_mode(data, cp, profile);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900695}
696
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900697/**
698 * tomoyo_print_config - Print mode for specified functionality.
699 *
700 * @head: Pointer to "struct tomoyo_io_buffer".
701 * @config: Mode for that functionality.
702 *
703 * Returns nothing.
704 *
705 * Caller prints functionality's name.
706 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900707static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
708{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900709 tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
710 tomoyo_mode[config & 3],
711 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
712 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900713}
714
Kentaro Takeda95908372009-02-05 17:18:13 +0900715/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900716 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900717 *
718 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900719 *
720 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900721 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900722static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900723{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900724 u8 index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900725 struct tomoyo_policy_namespace *ns =
726 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900727 const struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900728 if (head->r.eof)
729 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900730 next:
731 index = head->r.index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900732 profile = ns->profile_ptr[index];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900733 switch (head->r.step) {
734 case 0:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900735 tomoyo_print_namespace(head);
736 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
737 ns->profile_version);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900738 head->r.step++;
739 break;
740 case 1:
741 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
742 head->r.index++)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900743 if (ns->profile_ptr[head->r.index])
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900744 break;
745 if (head->r.index == TOMOYO_MAX_PROFILES)
746 return;
747 head->r.step++;
748 break;
749 case 2:
750 {
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900751 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900752 const struct tomoyo_path_info *comment =
753 profile->comment;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900754 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900755 tomoyo_io_printf(head, "%u-COMMENT=", index);
756 tomoyo_set_string(head, comment ? comment->name : "");
757 tomoyo_set_lf(head);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900758 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
759 for (i = 0; i < TOMOYO_MAX_PREF; i++)
760 tomoyo_io_printf(head, "%s=%u ",
761 tomoyo_pref_keywords[i],
762 profile->pref[i]);
763 tomoyo_set_string(head, "}\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900764 head->r.step++;
765 }
766 break;
767 case 3:
768 {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900769 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900770 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
771 tomoyo_print_config(head, profile->default_config);
772 head->r.bit = 0;
773 head->r.step++;
774 }
775 break;
776 case 4:
777 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
778 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
779 const u8 i = head->r.bit;
780 const u8 config = profile->config[i];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900781 if (config == TOMOYO_CONFIG_USE_DEFAULT)
782 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900783 tomoyo_print_namespace(head);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900784 if (i < TOMOYO_MAX_MAC_INDEX)
785 tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
786 index,
787 tomoyo_category_keywords
788 [tomoyo_index2category[i]],
789 tomoyo_mac_keywords[i]);
790 else
791 tomoyo_io_printf(head, "%u-CONFIG::%s", index,
792 tomoyo_mac_keywords[i]);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900793 tomoyo_print_config(head, config);
794 head->r.bit++;
795 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900796 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900797 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
798 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900799 head->r.index++;
800 head->r.step = 1;
801 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900802 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900803 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900804 if (tomoyo_flush(head))
805 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900806}
807
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900808/**
809 * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
810 *
811 * @a: Pointer to "struct tomoyo_acl_head".
812 * @b: Pointer to "struct tomoyo_acl_head".
813 *
814 * Returns true if @a == @b, false otherwise.
815 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900816static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
817 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900818{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900819 return container_of(a, struct tomoyo_manager, head)->manager ==
820 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900821}
822
Kentaro Takeda95908372009-02-05 17:18:13 +0900823/**
824 * tomoyo_update_manager_entry - Add a manager entry.
825 *
826 * @manager: The path to manager or the domainnamme.
827 * @is_delete: True if it is a delete request.
828 *
829 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900830 *
831 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900832 */
833static int tomoyo_update_manager_entry(const char *manager,
834 const bool is_delete)
835{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900836 struct tomoyo_manager e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900837 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900838 /* .ns = &tomoyo_kernel_namespace, */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900839 .is_delete = is_delete,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900840 .list = &tomoyo_kernel_namespace.
841 policy_list[TOMOYO_ID_MANAGER],
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900842 };
843 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900844 if (tomoyo_domain_def(manager)) {
845 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900846 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900847 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900848 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900849 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900850 return -EINVAL;
851 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900852 e.manager = tomoyo_get_name(manager);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900853 if (e.manager) {
854 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
855 tomoyo_same_manager);
856 tomoyo_put_name(e.manager);
857 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900858 return error;
859}
860
861/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900862 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900863 *
864 * @head: Pointer to "struct tomoyo_io_buffer".
865 *
866 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900867 *
868 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900869 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900870static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900871{
872 char *data = head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900873
874 if (!strcmp(data, "manage_by_non_root")) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900875 tomoyo_manage_by_non_root = !head->w.is_delete;
Kentaro Takeda95908372009-02-05 17:18:13 +0900876 return 0;
877 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900878 return tomoyo_update_manager_entry(data, head->w.is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900879}
880
881/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900882 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900883 *
884 * @head: Pointer to "struct tomoyo_io_buffer".
885 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900886 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900887 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900888static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900889{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900890 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900891 return;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900892 list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
893 policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900894 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900895 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900896 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900897 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900898 if (!tomoyo_flush(head))
899 return;
900 tomoyo_set_string(head, ptr->manager->name);
901 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900902 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900903 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900904}
905
906/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900907 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900908 *
909 * Returns true if the current process is permitted to modify policy
910 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900911 *
912 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900913 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900914static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900915{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900916 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900917 const char *exe;
918 const struct task_struct *task = current;
919 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
920 bool found = false;
921
922 if (!tomoyo_policy_loaded)
923 return true;
924 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
925 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900926 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
927 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900928 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900929 && !tomoyo_pathcmp(domainname, ptr->manager)) {
930 found = true;
931 break;
932 }
933 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900934 if (found)
935 return true;
936 exe = tomoyo_get_exe();
937 if (!exe)
938 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900939 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
940 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900941 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900942 && !strcmp(exe, ptr->manager->name)) {
943 found = true;
944 break;
945 }
946 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900947 if (!found) { /* Reduce error messages. */
948 static pid_t last_pid;
949 const pid_t pid = current->pid;
950 if (last_pid != pid) {
951 printk(KERN_WARNING "%s ( %s ) is not permitted to "
952 "update policies.\n", domainname->name, exe);
953 last_pid = pid;
954 }
955 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900956 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900957 return found;
958}
959
960/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900961 * tomoyo_select_domain - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900962 *
963 * @head: Pointer to "struct tomoyo_io_buffer".
964 * @data: String to parse.
965 *
966 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900967 *
968 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900969 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900970static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
971 const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900972{
973 unsigned int pid;
974 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900975 bool global_pid = false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900976 if (strncmp(data, "select ", 7))
977 return false;
978 data += 7;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900979 if (sscanf(data, "pid=%u", &pid) == 1 ||
980 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900981 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900982 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900983 read_lock(&tasklist_lock);
Tetsuo Handa9b244372010-06-03 20:35:53 +0900984 if (global_pid)
985 p = find_task_by_pid_ns(pid, &init_pid_ns);
986 else
987 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900988 if (p)
989 domain = tomoyo_real_domain(p);
990 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900991 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900992 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900993 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900994 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900995 } else
996 return false;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900997 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900998 /* Accessing read_buf is safe because head->io_sem is held. */
999 if (!head->read_buf)
1000 return true; /* Do nothing if open(O_WRONLY). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001001 memset(&head->r, 0, sizeof(head->r));
1002 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +02001003 if (domain)
1004 head->r.domain = &domain->list;
1005 else
1006 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +09001007 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001008 if (domain && domain->is_deleted)
1009 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09001010 return true;
1011}
1012
1013/**
Tetsuo Handa731d37a2011-09-10 15:25:58 +09001014 * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1015 *
1016 * @a: Pointer to "struct tomoyo_acl_info".
1017 * @b: Pointer to "struct tomoyo_acl_info".
1018 *
1019 * Returns true if @a == @b, false otherwise.
1020 */
1021static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1022 const struct tomoyo_acl_info *b)
1023{
1024 const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1025 const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1026 return p1->domainname == p2->domainname;
1027}
1028
1029/**
1030 * tomoyo_write_task - Update task related list.
1031 *
1032 * @param: Pointer to "struct tomoyo_acl_param".
1033 *
1034 * Returns 0 on success, negative value otherwise.
1035 *
1036 * Caller holds tomoyo_read_lock().
1037 */
1038static int tomoyo_write_task(struct tomoyo_acl_param *param)
1039{
1040 int error = -EINVAL;
1041 if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1042 struct tomoyo_task_acl e = {
1043 .head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1044 .domainname = tomoyo_get_domainname(param),
1045 };
1046 if (e.domainname)
1047 error = tomoyo_update_domain(&e.head, sizeof(e), param,
1048 tomoyo_same_task_acl,
1049 NULL);
1050 tomoyo_put_name(e.domainname);
1051 }
1052 return error;
1053}
1054
1055/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001056 * tomoyo_delete_domain - Delete a domain.
1057 *
1058 * @domainname: The name of domain.
1059 *
1060 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001061 *
1062 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001063 */
1064static int tomoyo_delete_domain(char *domainname)
1065{
1066 struct tomoyo_domain_info *domain;
1067 struct tomoyo_path_info name;
1068
1069 name.name = domainname;
1070 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +09001071 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1072 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001073 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001074 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001075 /* Never delete tomoyo_kernel_domain */
1076 if (domain == &tomoyo_kernel_domain)
1077 continue;
1078 if (domain->is_deleted ||
1079 tomoyo_pathcmp(domain->domainname, &name))
1080 continue;
1081 domain->is_deleted = true;
1082 break;
1083 }
Tetsuo Handaf737d952010-01-03 21:16:32 +09001084 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001085 return 0;
1086}
1087
1088/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001089 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001090 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001091 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001092 * @list: Pointer to "struct list_head".
1093 * @data: Policy to be interpreted.
1094 * @is_delete: True if it is a delete request.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001095 *
1096 * Returns 0 on success, negative value otherwise.
1097 *
1098 * Caller holds tomoyo_read_lock().
1099 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001100static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
1101 struct list_head *list, char *data,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001102 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001103{
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001104 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001105 .ns = ns,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001106 .list = list,
1107 .data = data,
1108 .is_delete = is_delete,
1109 };
1110 static const struct {
1111 const char *keyword;
1112 int (*write) (struct tomoyo_acl_param *);
Tetsuo Handa731d37a2011-09-10 15:25:58 +09001113 } tomoyo_callback[5] = {
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001114 { "file ", tomoyo_write_file },
Tetsuo Handa059d84d2011-09-10 15:23:54 +09001115 { "network inet ", tomoyo_write_inet_network },
1116 { "network unix ", tomoyo_write_unix_network },
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001117 { "misc ", tomoyo_write_misc },
Tetsuo Handa731d37a2011-09-10 15:25:58 +09001118 { "task ", tomoyo_write_task },
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001119 };
1120 u8 i;
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001121
1122 for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001123 if (!tomoyo_str_starts(&param.data,
1124 tomoyo_callback[i].keyword))
1125 continue;
1126 return tomoyo_callback[i].write(&param);
1127 }
1128 return -EINVAL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001129}
1130
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001131/* String table for domain flags. */
1132const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
1133 [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n",
1134 [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
1135};
1136
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001137/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001138 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001139 *
1140 * @head: Pointer to "struct tomoyo_io_buffer".
1141 *
1142 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001143 *
1144 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001145 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001146static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001147{
1148 char *data = head->write_buf;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001149 struct tomoyo_policy_namespace *ns;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001150 struct tomoyo_domain_info *domain = head->w.domain;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001151 const bool is_delete = head->w.is_delete;
1152 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
Kentaro Takeda95908372009-02-05 17:18:13 +09001153 unsigned int profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001154 if (*data == '<') {
Kentaro Takeda95908372009-02-05 17:18:13 +09001155 domain = NULL;
1156 if (is_delete)
1157 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001158 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +09001159 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001160 else
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001161 domain = tomoyo_assign_domain(data, false);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001162 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001163 return 0;
1164 }
1165 if (!domain)
1166 return -EINVAL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001167 ns = domain->ns;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001168 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +09001169 && profile < TOMOYO_MAX_PROFILES) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001170 if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
Kentaro Takeda95908372009-02-05 17:18:13 +09001171 domain->profile = (u8) profile;
1172 return 0;
1173 }
Tetsuo Handa32997142011-06-26 23:19:28 +09001174 if (sscanf(data, "use_group %u\n", &profile) == 1
1175 && profile < TOMOYO_MAX_ACL_GROUPS) {
1176 if (!is_delete)
1177 domain->group = (u8) profile;
1178 return 0;
1179 }
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001180 for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
1181 const char *cp = tomoyo_dif[profile];
1182 if (strncmp(data, cp, strlen(cp) - 1))
1183 continue;
1184 domain->flags[profile] = !is_delete;
Tetsuo Handa9b244372010-06-03 20:35:53 +09001185 return 0;
1186 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001187 return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
1188 is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +09001189}
1190
1191/**
Tetsuo Handa2066a362011-07-08 13:21:37 +09001192 * tomoyo_print_condition - Print condition part.
1193 *
1194 * @head: Pointer to "struct tomoyo_io_buffer".
1195 * @cond: Pointer to "struct tomoyo_condition".
1196 *
1197 * Returns true on success, false otherwise.
1198 */
1199static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
1200 const struct tomoyo_condition *cond)
1201{
1202 switch (head->r.cond_step) {
1203 case 0:
1204 head->r.cond_index = 0;
1205 head->r.cond_step++;
1206 /* fall through */
1207 case 1:
1208 {
1209 const u16 condc = cond->condc;
1210 const struct tomoyo_condition_element *condp =
1211 (typeof(condp)) (cond + 1);
1212 const struct tomoyo_number_union *numbers_p =
1213 (typeof(numbers_p)) (condp + condc);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001214 const struct tomoyo_name_union *names_p =
1215 (typeof(names_p))
1216 (numbers_p + cond->numbers_count);
Tetsuo Handa5b636852011-07-08 13:24:54 +09001217 const struct tomoyo_argv *argv =
1218 (typeof(argv)) (names_p + cond->names_count);
1219 const struct tomoyo_envp *envp =
1220 (typeof(envp)) (argv + cond->argc);
Tetsuo Handa2066a362011-07-08 13:21:37 +09001221 u16 skip;
1222 for (skip = 0; skip < head->r.cond_index; skip++) {
1223 const u8 left = condp->left;
1224 const u8 right = condp->right;
1225 condp++;
1226 switch (left) {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001227 case TOMOYO_ARGV_ENTRY:
1228 argv++;
1229 continue;
1230 case TOMOYO_ENVP_ENTRY:
1231 envp++;
1232 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001233 case TOMOYO_NUMBER_UNION:
1234 numbers_p++;
1235 break;
1236 }
1237 switch (right) {
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001238 case TOMOYO_NAME_UNION:
1239 names_p++;
1240 break;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001241 case TOMOYO_NUMBER_UNION:
1242 numbers_p++;
1243 break;
1244 }
1245 }
1246 while (head->r.cond_index < condc) {
1247 const u8 match = condp->equals;
1248 const u8 left = condp->left;
1249 const u8 right = condp->right;
1250 if (!tomoyo_flush(head))
1251 return false;
1252 condp++;
1253 head->r.cond_index++;
1254 tomoyo_set_space(head);
1255 switch (left) {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001256 case TOMOYO_ARGV_ENTRY:
1257 tomoyo_io_printf(head,
1258 "exec.argv[%lu]%s=\"",
1259 argv->index, argv->
1260 is_not ? "!" : "");
1261 tomoyo_set_string(head,
1262 argv->value->name);
1263 tomoyo_set_string(head, "\"");
1264 argv++;
1265 continue;
1266 case TOMOYO_ENVP_ENTRY:
1267 tomoyo_set_string(head,
1268 "exec.envp[\"");
1269 tomoyo_set_string(head,
1270 envp->name->name);
1271 tomoyo_io_printf(head, "\"]%s=", envp->
1272 is_not ? "!" : "");
1273 if (envp->value) {
1274 tomoyo_set_string(head, "\"");
1275 tomoyo_set_string(head, envp->
1276 value->name);
1277 tomoyo_set_string(head, "\"");
1278 } else {
1279 tomoyo_set_string(head,
1280 "NULL");
1281 }
1282 envp++;
1283 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001284 case TOMOYO_NUMBER_UNION:
1285 tomoyo_print_number_union_nospace
1286 (head, numbers_p++);
1287 break;
1288 default:
1289 tomoyo_set_string(head,
1290 tomoyo_condition_keyword[left]);
1291 break;
1292 }
1293 tomoyo_set_string(head, match ? "=" : "!=");
1294 switch (right) {
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001295 case TOMOYO_NAME_UNION:
1296 tomoyo_print_name_union_quoted
1297 (head, names_p++);
1298 break;
Tetsuo Handa2066a362011-07-08 13:21:37 +09001299 case TOMOYO_NUMBER_UNION:
1300 tomoyo_print_number_union_nospace
1301 (head, numbers_p++);
1302 break;
1303 default:
1304 tomoyo_set_string(head,
1305 tomoyo_condition_keyword[right]);
1306 break;
1307 }
1308 }
1309 }
1310 head->r.cond_step++;
1311 /* fall through */
1312 case 2:
1313 if (!tomoyo_flush(head))
1314 break;
1315 head->r.cond_step++;
1316 /* fall through */
1317 case 3:
Tetsuo Handa1f067a62011-09-10 15:24:56 +09001318 if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
1319 tomoyo_io_printf(head, " grant_log=%s",
1320 tomoyo_yesno(cond->grant_log ==
1321 TOMOYO_GRANTLOG_YES));
Tetsuo Handa2066a362011-07-08 13:21:37 +09001322 tomoyo_set_lf(head);
1323 return true;
1324 }
1325 return false;
1326}
1327
1328/**
Tetsuo Handa32997142011-06-26 23:19:28 +09001329 * tomoyo_set_group - Print "acl_group " header keyword and category name.
Kentaro Takeda95908372009-02-05 17:18:13 +09001330 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001331 * @head: Pointer to "struct tomoyo_io_buffer".
1332 * @category: Category name.
Kentaro Takeda95908372009-02-05 17:18:13 +09001333 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001334 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +09001335 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001336static void tomoyo_set_group(struct tomoyo_io_buffer *head,
1337 const char *category)
Kentaro Takeda95908372009-02-05 17:18:13 +09001338{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001339 if (head->type == TOMOYO_EXCEPTIONPOLICY) {
1340 tomoyo_print_namespace(head);
Tetsuo Handa32997142011-06-26 23:19:28 +09001341 tomoyo_io_printf(head, "acl_group %u ",
1342 head->r.acl_group_index);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001343 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001344 tomoyo_set_string(head, category);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001345}
1346
1347/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001348 * tomoyo_print_entry - Print an ACL entry.
1349 *
1350 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001351 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +09001352 *
1353 * Returns true on success, false otherwise.
1354 */
1355static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001356 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +09001357{
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001358 const u8 acl_type = acl->type;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001359 bool first = true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001360 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +09001361
Tetsuo Handa2066a362011-07-08 13:21:37 +09001362 if (head->r.print_cond_part)
1363 goto print_cond_part;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001364 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +09001365 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001366 if (!tomoyo_flush(head))
1367 return false;
1368 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001369 struct tomoyo_path_acl *ptr =
1370 container_of(acl, typeof(*ptr), head);
1371 const u16 perm = ptr->perm;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001372 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001373 if (!(perm & (1 << bit)))
1374 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001375 if (head->r.print_transition_related_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001376 bit != TOMOYO_TYPE_EXECUTE)
1377 continue;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001378 if (first) {
1379 tomoyo_set_group(head, "file ");
1380 first = false;
1381 } else {
1382 tomoyo_set_slash(head);
1383 }
1384 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001385 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001386 if (first)
1387 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001388 tomoyo_print_name_union(head, &ptr->name);
Tetsuo Handa731d37a2011-09-10 15:25:58 +09001389 } else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1390 struct tomoyo_task_acl *ptr =
1391 container_of(acl, typeof(*ptr), head);
1392 tomoyo_set_group(head, "task ");
1393 tomoyo_set_string(head, "manual_domain_transition ");
1394 tomoyo_set_string(head, ptr->domainname->name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001395 } else if (head->r.print_transition_related_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +09001396 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001397 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
1398 struct tomoyo_path2_acl *ptr =
1399 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001400 const u8 perm = ptr->perm;
1401 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
1402 if (!(perm & (1 << bit)))
1403 continue;
1404 if (first) {
1405 tomoyo_set_group(head, "file ");
1406 first = false;
1407 } else {
1408 tomoyo_set_slash(head);
1409 }
1410 tomoyo_set_string(head, tomoyo_mac_keywords
1411 [tomoyo_pp2mac[bit]]);
1412 }
1413 if (first)
1414 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001415 tomoyo_print_name_union(head, &ptr->name1);
1416 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001417 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1418 struct tomoyo_path_number_acl *ptr =
1419 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001420 const u8 perm = ptr->perm;
1421 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
1422 if (!(perm & (1 << bit)))
1423 continue;
1424 if (first) {
1425 tomoyo_set_group(head, "file ");
1426 first = false;
1427 } else {
1428 tomoyo_set_slash(head);
1429 }
1430 tomoyo_set_string(head, tomoyo_mac_keywords
1431 [tomoyo_pn2mac[bit]]);
1432 }
1433 if (first)
1434 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001435 tomoyo_print_name_union(head, &ptr->name);
1436 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001437 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1438 struct tomoyo_mkdev_acl *ptr =
1439 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001440 const u8 perm = ptr->perm;
1441 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
1442 if (!(perm & (1 << bit)))
1443 continue;
1444 if (first) {
1445 tomoyo_set_group(head, "file ");
1446 first = false;
1447 } else {
1448 tomoyo_set_slash(head);
1449 }
1450 tomoyo_set_string(head, tomoyo_mac_keywords
1451 [tomoyo_pnnn2mac[bit]]);
1452 }
1453 if (first)
1454 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001455 tomoyo_print_name_union(head, &ptr->name);
1456 tomoyo_print_number_union(head, &ptr->mode);
1457 tomoyo_print_number_union(head, &ptr->major);
1458 tomoyo_print_number_union(head, &ptr->minor);
Tetsuo Handa059d84d2011-09-10 15:23:54 +09001459 } else if (acl_type == TOMOYO_TYPE_INET_ACL) {
1460 struct tomoyo_inet_acl *ptr =
1461 container_of(acl, typeof(*ptr), head);
1462 const u8 perm = ptr->perm;
1463
1464 for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1465 if (!(perm & (1 << bit)))
1466 continue;
1467 if (first) {
1468 tomoyo_set_group(head, "network inet ");
1469 tomoyo_set_string(head, tomoyo_proto_keyword
1470 [ptr->protocol]);
1471 tomoyo_set_space(head);
1472 first = false;
1473 } else {
1474 tomoyo_set_slash(head);
1475 }
1476 tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1477 }
1478 if (first)
1479 return true;
1480 tomoyo_set_space(head);
1481 if (ptr->address.group) {
1482 tomoyo_set_string(head, "@");
1483 tomoyo_set_string(head, ptr->address.group->group_name
1484 ->name);
1485 } else {
1486 char buf[128];
1487 tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
1488 tomoyo_io_printf(head, "%s", buf);
1489 }
1490 tomoyo_print_number_union(head, &ptr->port);
1491 } else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
1492 struct tomoyo_unix_acl *ptr =
1493 container_of(acl, typeof(*ptr), head);
1494 const u8 perm = ptr->perm;
1495
1496 for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
1497 if (!(perm & (1 << bit)))
1498 continue;
1499 if (first) {
1500 tomoyo_set_group(head, "network unix ");
1501 tomoyo_set_string(head, tomoyo_proto_keyword
1502 [ptr->protocol]);
1503 tomoyo_set_space(head);
1504 first = false;
1505 } else {
1506 tomoyo_set_slash(head);
1507 }
1508 tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
1509 }
1510 if (first)
1511 return true;
1512 tomoyo_print_name_union(head, &ptr->name);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001513 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1514 struct tomoyo_mount_acl *ptr =
1515 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001516 tomoyo_set_group(head, "file mount");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001517 tomoyo_print_name_union(head, &ptr->dev_name);
1518 tomoyo_print_name_union(head, &ptr->dir_name);
1519 tomoyo_print_name_union(head, &ptr->fs_type);
1520 tomoyo_print_number_union(head, &ptr->flags);
Tetsuo Handad58e0da2011-09-10 15:22:48 +09001521 } else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
1522 struct tomoyo_env_acl *ptr =
1523 container_of(acl, typeof(*ptr), head);
1524
1525 tomoyo_set_group(head, "misc env ");
1526 tomoyo_set_string(head, ptr->env->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001527 }
Tetsuo Handa2066a362011-07-08 13:21:37 +09001528 if (acl->cond) {
1529 head->r.print_cond_part = true;
1530 head->r.cond_step = 0;
1531 if (!tomoyo_flush(head))
1532 return false;
1533print_cond_part:
1534 if (!tomoyo_print_condition(head, acl->cond))
1535 return false;
1536 head->r.print_cond_part = false;
1537 } else {
1538 tomoyo_set_lf(head);
1539 }
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001540 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001541}
1542
1543/**
1544 * tomoyo_read_domain2 - Read domain policy.
1545 *
Tetsuo Handa32997142011-06-26 23:19:28 +09001546 * @head: Pointer to "struct tomoyo_io_buffer".
1547 * @list: Pointer to "struct list_head".
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001548 *
1549 * Caller holds tomoyo_read_lock().
1550 *
1551 * Returns true on success, false otherwise.
1552 */
1553static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
Tetsuo Handa32997142011-06-26 23:19:28 +09001554 struct list_head *list)
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001555{
Tetsuo Handa32997142011-06-26 23:19:28 +09001556 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001557 struct tomoyo_acl_info *ptr =
1558 list_entry(head->r.acl, typeof(*ptr), list);
1559 if (!tomoyo_print_entry(head, ptr))
1560 return false;
1561 }
1562 head->r.acl = NULL;
1563 return true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001564}
1565
1566/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001567 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001568 *
1569 * @head: Pointer to "struct tomoyo_io_buffer".
1570 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001571 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001572 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001573static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001574{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001575 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001576 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001577 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001578 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001579 list_entry(head->r.domain, typeof(*domain), list);
1580 switch (head->r.step) {
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001581 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001582 case 0:
1583 if (domain->is_deleted &&
1584 !head->r.print_this_domain_only)
1585 continue;
1586 /* Print domainname and flags. */
1587 tomoyo_set_string(head, domain->domainname->name);
1588 tomoyo_set_lf(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001589 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001590 domain->profile);
Tetsuo Handa32997142011-06-26 23:19:28 +09001591 tomoyo_io_printf(head, "use_group %u\n",
1592 domain->group);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001593 for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
1594 if (domain->flags[i])
1595 tomoyo_set_string(head, tomoyo_dif[i]);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001596 head->r.step++;
1597 tomoyo_set_lf(head);
1598 /* fall through */
1599 case 1:
Tetsuo Handa32997142011-06-26 23:19:28 +09001600 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001601 return;
1602 head->r.step++;
1603 if (!tomoyo_set_lf(head))
1604 return;
1605 /* fall through */
1606 case 2:
1607 head->r.step = 0;
1608 if (head->r.print_this_domain_only)
1609 goto done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001610 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001611 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001612 done:
1613 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001614}
1615
1616/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001617 * tomoyo_write_pid: Specify PID to obtain domainname.
1618 *
1619 * @head: Pointer to "struct tomoyo_io_buffer".
1620 *
1621 * Returns 0.
1622 */
1623static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1624{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001625 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001626 return 0;
1627}
1628
1629/**
1630 * tomoyo_read_pid - Get domainname of the specified PID.
1631 *
1632 * @head: Pointer to "struct tomoyo_io_buffer".
1633 *
1634 * Returns the domainname which the specified PID is in on success,
1635 * empty string otherwise.
1636 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1637 * using read()/write() interface rather than sysctl() interface.
1638 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001639static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001640{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001641 char *buf = head->write_buf;
1642 bool global_pid = false;
1643 unsigned int pid;
1644 struct task_struct *p;
1645 struct tomoyo_domain_info *domain = NULL;
1646
1647 /* Accessing write_buf is safe because head->io_sem is held. */
1648 if (!buf) {
1649 head->r.eof = true;
1650 return; /* Do nothing if open(O_RDONLY). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001651 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001652 if (head->r.w_pos || head->r.eof)
1653 return;
1654 head->r.eof = true;
1655 if (tomoyo_str_starts(&buf, "global-pid "))
1656 global_pid = true;
1657 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1658 rcu_read_lock();
1659 read_lock(&tasklist_lock);
1660 if (global_pid)
1661 p = find_task_by_pid_ns(pid, &init_pid_ns);
1662 else
1663 p = find_task_by_vpid(pid);
1664 if (p)
1665 domain = tomoyo_real_domain(p);
1666 read_unlock(&tasklist_lock);
1667 rcu_read_unlock();
1668 if (!domain)
1669 return;
1670 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1671 tomoyo_set_string(head, domain->domainname->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001672}
1673
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09001674/* String table for domain transition control keywords. */
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001675static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001676 [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
1677 [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
1678 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1679 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
1680 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
1681 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001682};
1683
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09001684/* String table for grouping keywords. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001685static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handa059d84d2011-09-10 15:23:54 +09001686 [TOMOYO_PATH_GROUP] = "path_group ",
1687 [TOMOYO_NUMBER_GROUP] = "number_group ",
1688 [TOMOYO_ADDRESS_GROUP] = "address_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001689};
1690
Kentaro Takeda95908372009-02-05 17:18:13 +09001691/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001692 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001693 *
1694 * @head: Pointer to "struct tomoyo_io_buffer".
1695 *
1696 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001697 *
1698 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001699 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001700static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001701{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001702 const bool is_delete = head->w.is_delete;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001703 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001704 .ns = head->w.ns,
1705 .is_delete = is_delete,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001706 .data = head->write_buf,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001707 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001708 u8 i;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001709 if (tomoyo_str_starts(&param.data, "aggregator "))
1710 return tomoyo_write_aggregator(&param);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001711 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001712 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1713 return tomoyo_write_transition_control(&param, i);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001714 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001715 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1716 return tomoyo_write_group(&param, i);
Tetsuo Handa32997142011-06-26 23:19:28 +09001717 if (tomoyo_str_starts(&param.data, "acl_group ")) {
1718 unsigned int group;
1719 char *data;
1720 group = simple_strtoul(param.data, &data, 10);
1721 if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001722 return tomoyo_write_domain2
1723 (head->w.ns, &head->w.ns->acl_group[group],
1724 data, is_delete);
Tetsuo Handa32997142011-06-26 23:19:28 +09001725 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001726 return -EINVAL;
1727}
1728
Tetsuo Handa31845e82010-06-17 16:54:33 +09001729/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +09001730 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
Tetsuo Handa31845e82010-06-17 16:54:33 +09001731 *
1732 * @head: Pointer to "struct tomoyo_io_buffer".
1733 * @idx: Index number.
1734 *
1735 * Returns true on success, false otherwise.
1736 *
1737 * Caller holds tomoyo_read_lock().
1738 */
1739static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1740{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001741 struct tomoyo_policy_namespace *ns =
1742 container_of(head->r.ns, typeof(*ns), namespace_list);
1743 struct list_head *list = &ns->group_list[idx];
1744 list_for_each_cookie(head->r.group, list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001745 struct tomoyo_group *group =
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001746 list_entry(head->r.group, typeof(*group), head.list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001747 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001748 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001749 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001750 if (ptr->is_deleted)
1751 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001752 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001753 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001754 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001755 tomoyo_set_string(head, tomoyo_group_name[idx]);
1756 tomoyo_set_string(head, group->group_name->name);
1757 if (idx == TOMOYO_PATH_GROUP) {
1758 tomoyo_set_space(head);
1759 tomoyo_set_string(head, container_of
1760 (ptr, struct tomoyo_path_group,
1761 head)->member_name->name);
1762 } else if (idx == TOMOYO_NUMBER_GROUP) {
1763 tomoyo_print_number_union(head, &container_of
1764 (ptr,
1765 struct tomoyo_number_group,
1766 head)->number);
Tetsuo Handa059d84d2011-09-10 15:23:54 +09001767 } else if (idx == TOMOYO_ADDRESS_GROUP) {
1768 char buffer[128];
1769
1770 struct tomoyo_address_group *member =
1771 container_of(ptr, typeof(*member),
1772 head);
1773 tomoyo_print_ip(buffer, sizeof(buffer),
1774 &member->address);
1775 tomoyo_io_printf(head, " %s", buffer);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001776 }
1777 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001778 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001779 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001780 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001781 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001782 return true;
1783}
1784
1785/**
1786 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1787 *
1788 * @head: Pointer to "struct tomoyo_io_buffer".
1789 * @idx: Index number.
1790 *
1791 * Returns true on success, false otherwise.
1792 *
1793 * Caller holds tomoyo_read_lock().
1794 */
1795static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1796{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001797 struct tomoyo_policy_namespace *ns =
1798 container_of(head->r.ns, typeof(*ns), namespace_list);
1799 struct list_head *list = &ns->policy_list[idx];
1800 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001801 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001802 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001803 if (acl->is_deleted)
1804 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001805 if (!tomoyo_flush(head))
1806 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001807 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001808 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001809 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001810 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001811 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001812 tomoyo_print_namespace(head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001813 tomoyo_set_string(head, tomoyo_transition_type
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001814 [ptr->type]);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001815 tomoyo_set_string(head, ptr->program ?
1816 ptr->program->name : "any");
1817 tomoyo_set_string(head, " from ");
1818 tomoyo_set_string(head, ptr->domainname ?
1819 ptr->domainname->name :
1820 "any");
Tetsuo Handa31845e82010-06-17 16:54:33 +09001821 }
1822 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001823 case TOMOYO_ID_AGGREGATOR:
1824 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001825 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001826 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001827 tomoyo_print_namespace(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001828 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001829 tomoyo_set_string(head,
1830 ptr->original_name->name);
1831 tomoyo_set_space(head);
1832 tomoyo_set_string(head,
1833 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001834 }
1835 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001836 default:
1837 continue;
1838 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001839 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001840 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001841 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001842 return true;
1843}
1844
Kentaro Takeda95908372009-02-05 17:18:13 +09001845/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001846 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001847 *
1848 * @head: Pointer to "struct tomoyo_io_buffer".
1849 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001850 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001851 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001852static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001853{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001854 struct tomoyo_policy_namespace *ns =
1855 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001856 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001857 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001858 while (head->r.step < TOMOYO_MAX_POLICY &&
1859 tomoyo_read_policy(head, head->r.step))
1860 head->r.step++;
1861 if (head->r.step < TOMOYO_MAX_POLICY)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001862 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001863 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1864 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1865 head->r.step++;
1866 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001867 return;
Tetsuo Handa32997142011-06-26 23:19:28 +09001868 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
1869 + TOMOYO_MAX_ACL_GROUPS) {
1870 head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
1871 - TOMOYO_MAX_GROUP;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001872 if (!tomoyo_read_domain2(head, &ns->acl_group
Tetsuo Handa32997142011-06-26 23:19:28 +09001873 [head->r.acl_group_index]))
1874 return;
1875 head->r.step++;
1876 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001877 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001878}
1879
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001880/* Wait queue for kernel -> userspace notification. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001881static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001882/* Wait queue for userspace -> kernel notification. */
1883static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001884
1885/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001886struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001887 struct list_head list;
1888 char *query;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001889 size_t query_len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001890 unsigned int serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001891 u8 timer;
1892 u8 answer;
1893 u8 retry;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001894};
1895
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001896/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001897static LIST_HEAD(tomoyo_query_list);
1898
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001899/* Lock for manipulating tomoyo_query_list. */
1900static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1901
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001902/*
1903 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1904 * interface.
1905 */
1906static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1907
1908/**
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001909 * tomoyo_truncate - Truncate a line.
1910 *
1911 * @str: String to truncate.
1912 *
1913 * Returns length of truncated @str.
1914 */
1915static int tomoyo_truncate(char *str)
1916{
1917 char *start = str;
1918 while (*(unsigned char *) str > (unsigned char) ' ')
1919 str++;
1920 *str = '\0';
1921 return strlen(start) + 1;
1922}
1923
1924/**
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001925 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1926 *
1927 * @domain: Pointer to "struct tomoyo_domain_info".
1928 * @header: Lines containing ACL.
1929 *
1930 * Returns nothing.
1931 */
1932static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1933{
1934 char *buffer;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001935 char *realpath = NULL;
Tetsuo Handa5b636852011-07-08 13:24:54 +09001936 char *argv0 = NULL;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001937 char *symlink = NULL;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001938 char *cp = strchr(header, '\n');
1939 int len;
1940 if (!cp)
1941 return;
1942 cp = strchr(cp + 1, '\n');
1943 if (!cp)
1944 return;
1945 *cp++ = '\0';
1946 len = strlen(cp) + 1;
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001947 /* strstr() will return NULL if ordering is wrong. */
1948 if (*cp == 'f') {
Tetsuo Handa5b636852011-07-08 13:24:54 +09001949 argv0 = strstr(header, " argv[]={ \"");
1950 if (argv0) {
1951 argv0 += 10;
1952 len += tomoyo_truncate(argv0) + 14;
1953 }
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001954 realpath = strstr(header, " exec={ realpath=\"");
1955 if (realpath) {
1956 realpath += 8;
1957 len += tomoyo_truncate(realpath) + 6;
1958 }
1959 symlink = strstr(header, " symlink.target=\"");
1960 if (symlink)
1961 len += tomoyo_truncate(symlink + 1) + 1;
1962 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001963 buffer = kmalloc(len, GFP_NOFS);
1964 if (!buffer)
1965 return;
1966 snprintf(buffer, len - 1, "%s", cp);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001967 if (realpath)
1968 tomoyo_addprintf(buffer, len, " exec.%s", realpath);
Tetsuo Handa5b636852011-07-08 13:24:54 +09001969 if (argv0)
1970 tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
Tetsuo Handa2ca9bf42011-07-08 13:23:44 +09001971 if (symlink)
1972 tomoyo_addprintf(buffer, len, "%s", symlink);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001973 tomoyo_normalize_line(buffer);
Tetsuo Handab22b8b92011-06-26 23:21:50 +09001974 if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1975 false))
1976 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001977 kfree(buffer);
1978}
1979
1980/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001981 * tomoyo_supervisor - Ask for the supervisor's decision.
1982 *
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001983 * @r: Pointer to "struct tomoyo_request_info".
1984 * @fmt: The printf()'s format string, followed by parameters.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001985 *
1986 * Returns 0 if the supervisor decided to permit the access request which
1987 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1988 * supervisor decided to retry the access request which violated the policy in
1989 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1990 */
1991int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1992{
1993 va_list args;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001994 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001995 int len;
1996 static unsigned int tomoyo_serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001997 struct tomoyo_query entry = { };
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001998 bool quota_exceeded = false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001999 va_start(args, fmt);
2000 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2001 va_end(args);
2002 /* Write /sys/kernel/security/tomoyo/audit. */
2003 va_start(args, fmt);
2004 tomoyo_write_log2(r, len, fmt, args);
2005 va_end(args);
2006 /* Nothing more to do if granted. */
2007 if (r->granted)
2008 return 0;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002009 if (r->mode)
2010 tomoyo_update_stat(r->mode);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002011 switch (r->mode) {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002012 case TOMOYO_CONFIG_ENFORCING:
2013 error = -EPERM;
2014 if (atomic_read(&tomoyo_query_observers))
2015 break;
2016 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002017 case TOMOYO_CONFIG_LEARNING:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002018 error = 0;
2019 /* Check max_learning_entry parameter. */
2020 if (tomoyo_domain_quota_is_ok(r))
2021 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002022 /* fall through */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002023 default:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002024 return 0;
2025 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002026 /* Get message. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002027 va_start(args, fmt);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002028 entry.query = tomoyo_init_log(r, len, fmt, args);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002029 va_end(args);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002030 if (!entry.query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002031 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002032 entry.query_len = strlen(entry.query) + 1;
2033 if (!error) {
2034 tomoyo_add_entry(r->domain, entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002035 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002036 }
2037 len = tomoyo_round2(entry.query_len);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002038 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002039 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
2040 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
2041 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002042 quota_exceeded = true;
2043 } else {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002044 entry.serial = tomoyo_serial++;
2045 entry.retry = r->retry;
2046 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
2047 list_add_tail(&entry.list, &tomoyo_query_list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002048 }
2049 spin_unlock(&tomoyo_query_list_lock);
2050 if (quota_exceeded)
2051 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002052 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002053 while (entry.timer < 10) {
2054 wake_up_all(&tomoyo_query_wait);
2055 if (wait_event_interruptible_timeout
2056 (tomoyo_answer_wait, entry.answer ||
2057 !atomic_read(&tomoyo_query_observers), HZ))
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002058 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002059 else
2060 entry.timer++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002061 }
2062 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002063 list_del(&entry.list);
2064 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002065 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002066 switch (entry.answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002067 case 3: /* Asked to retry by administrator. */
2068 error = TOMOYO_RETRY_REQUEST;
2069 r->retry++;
2070 break;
2071 case 1:
2072 /* Granted by administrator. */
2073 error = 0;
2074 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002075 default:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002076 /* Timed out or rejected by administrator. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002077 break;
2078 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002079out:
2080 kfree(entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002081 return error;
2082}
2083
2084/**
2085 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
2086 *
2087 * @file: Pointer to "struct file".
2088 * @wait: Pointer to "poll_table".
2089 *
2090 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2091 *
2092 * Waits for access requests which violated policy in enforcing mode.
2093 */
2094static int tomoyo_poll_query(struct file *file, poll_table *wait)
2095{
2096 struct list_head *tmp;
2097 bool found = false;
2098 u8 i;
2099 for (i = 0; i < 2; i++) {
2100 spin_lock(&tomoyo_query_list_lock);
2101 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002102 struct tomoyo_query *ptr =
2103 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002104 if (ptr->answer)
2105 continue;
2106 found = true;
2107 break;
2108 }
2109 spin_unlock(&tomoyo_query_list_lock);
2110 if (found)
2111 return POLLIN | POLLRDNORM;
2112 if (i)
2113 break;
2114 poll_wait(file, &tomoyo_query_wait, wait);
2115 }
2116 return 0;
2117}
2118
2119/**
2120 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
2121 *
2122 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002123 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002124static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002125{
2126 struct list_head *tmp;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002127 unsigned int pos = 0;
2128 size_t len = 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002129 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002130 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002131 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002132 if (head->read_buf) {
2133 kfree(head->read_buf);
2134 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002135 }
2136 spin_lock(&tomoyo_query_list_lock);
2137 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002138 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002139 if (ptr->answer)
2140 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002141 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002142 continue;
2143 len = ptr->query_len;
2144 break;
2145 }
2146 spin_unlock(&tomoyo_query_list_lock);
2147 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002148 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002149 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002150 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002151 buf = kzalloc(len + 32, GFP_NOFS);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002152 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002153 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002154 pos = 0;
2155 spin_lock(&tomoyo_query_list_lock);
2156 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002157 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002158 if (ptr->answer)
2159 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002160 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002161 continue;
2162 /*
2163 * Some query can be skipped because tomoyo_query_list
2164 * can change, but I don't care.
2165 */
2166 if (len == ptr->query_len)
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002167 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
2168 ptr->retry, ptr->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002169 break;
2170 }
2171 spin_unlock(&tomoyo_query_list_lock);
2172 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002173 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002174 head->r.w[head->r.w_pos++] = buf;
2175 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002176 } else {
2177 kfree(buf);
2178 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002179}
2180
2181/**
2182 * tomoyo_write_answer - Write the supervisor's decision.
2183 *
2184 * @head: Pointer to "struct tomoyo_io_buffer".
2185 *
2186 * Returns 0 on success, -EINVAL otherwise.
2187 */
2188static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
2189{
2190 char *data = head->write_buf;
2191 struct list_head *tmp;
2192 unsigned int serial;
2193 unsigned int answer;
2194 spin_lock(&tomoyo_query_list_lock);
2195 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002196 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002197 ptr->timer = 0;
2198 }
2199 spin_unlock(&tomoyo_query_list_lock);
2200 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2201 return -EINVAL;
2202 spin_lock(&tomoyo_query_list_lock);
2203 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002204 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002205 if (ptr->serial != serial)
2206 continue;
2207 if (!ptr->answer)
2208 ptr->answer = answer;
2209 break;
2210 }
2211 spin_unlock(&tomoyo_query_list_lock);
2212 return 0;
2213}
2214
2215/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002216 * tomoyo_read_version: Get version.
2217 *
2218 * @head: Pointer to "struct tomoyo_io_buffer".
2219 *
2220 * Returns version information.
2221 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09002222static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002223{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002224 if (!head->r.eof) {
Tetsuo Handa843d1832011-09-14 17:03:19 +09002225 tomoyo_io_printf(head, "2.5.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002226 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09002227 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002228}
2229
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002230/* String table for /sys/kernel/security/tomoyo/stat interface. */
2231static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
2232 [TOMOYO_STAT_POLICY_UPDATES] = "update:",
2233 [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:",
2234 [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2235 [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
2236};
2237
2238/* String table for /sys/kernel/security/tomoyo/stat interface. */
2239static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
2240 [TOMOYO_MEMORY_POLICY] = "policy:",
2241 [TOMOYO_MEMORY_AUDIT] = "audit log:",
2242 [TOMOYO_MEMORY_QUERY] = "query message:",
2243};
2244
2245/* Timestamp counter for last updated. */
2246static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
2247/* Counter for number of updates. */
2248static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
2249
2250/**
2251 * tomoyo_update_stat - Update statistic counters.
2252 *
2253 * @index: Index for policy type.
2254 *
2255 * Returns nothing.
2256 */
2257void tomoyo_update_stat(const u8 index)
2258{
2259 struct timeval tv;
2260 do_gettimeofday(&tv);
2261 /*
2262 * I don't use atomic operations because race condition is not fatal.
2263 */
2264 tomoyo_stat_updated[index]++;
2265 tomoyo_stat_modified[index] = tv.tv_sec;
2266}
2267
2268/**
2269 * tomoyo_read_stat - Read statistic data.
2270 *
2271 * @head: Pointer to "struct tomoyo_io_buffer".
2272 *
2273 * Returns nothing.
2274 */
2275static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
2276{
2277 u8 i;
2278 unsigned int total = 0;
2279 if (head->r.eof)
2280 return;
2281 for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
2282 tomoyo_io_printf(head, "Policy %-30s %10u",
2283 tomoyo_policy_headers[i],
2284 tomoyo_stat_updated[i]);
2285 if (tomoyo_stat_modified[i]) {
2286 struct tomoyo_time stamp;
2287 tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
2288 tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
2289 "%02u:%02u:%02u)",
2290 stamp.year, stamp.month, stamp.day,
2291 stamp.hour, stamp.min, stamp.sec);
2292 }
2293 tomoyo_set_lf(head);
2294 }
2295 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
2296 unsigned int used = tomoyo_memory_used[i];
2297 total += used;
2298 tomoyo_io_printf(head, "Memory used by %-22s %10u",
2299 tomoyo_memory_headers[i], used);
2300 used = tomoyo_memory_quota[i];
2301 if (used)
2302 tomoyo_io_printf(head, " (Quota: %10u)", used);
2303 tomoyo_set_lf(head);
2304 }
2305 tomoyo_io_printf(head, "Total memory used: %10u\n",
2306 total);
2307 head->r.eof = true;
2308}
2309
2310/**
2311 * tomoyo_write_stat - Set memory quota.
2312 *
2313 * @head: Pointer to "struct tomoyo_io_buffer".
2314 *
2315 * Returns 0.
2316 */
2317static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
2318{
2319 char *data = head->write_buf;
2320 u8 i;
2321 if (tomoyo_str_starts(&data, "Memory used by "))
2322 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
2323 if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
2324 sscanf(data, "%u", &tomoyo_memory_quota[i]);
2325 return 0;
2326}
2327
Kentaro Takeda95908372009-02-05 17:18:13 +09002328/**
2329 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
2330 *
2331 * @type: Type of interface.
2332 * @file: Pointer to "struct file".
2333 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002334 * Returns 0 on success, negative value otherwise.
Kentaro Takeda95908372009-02-05 17:18:13 +09002335 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002336int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09002337{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002338 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002339
2340 if (!head)
2341 return -ENOMEM;
2342 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002343 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09002344 switch (type) {
2345 case TOMOYO_DOMAINPOLICY:
2346 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002347 head->write = tomoyo_write_domain;
2348 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09002349 break;
2350 case TOMOYO_EXCEPTIONPOLICY:
2351 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002352 head->write = tomoyo_write_exception;
2353 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09002354 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002355 case TOMOYO_AUDIT:
2356 /* /sys/kernel/security/tomoyo/audit */
2357 head->poll = tomoyo_poll_log;
2358 head->read = tomoyo_read_log;
2359 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002360 case TOMOYO_PROCESS_STATUS:
2361 /* /sys/kernel/security/tomoyo/.process_status */
2362 head->write = tomoyo_write_pid;
2363 head->read = tomoyo_read_pid;
2364 break;
2365 case TOMOYO_VERSION:
2366 /* /sys/kernel/security/tomoyo/version */
2367 head->read = tomoyo_read_version;
2368 head->readbuf_size = 128;
2369 break;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002370 case TOMOYO_STAT:
2371 /* /sys/kernel/security/tomoyo/stat */
2372 head->write = tomoyo_write_stat;
2373 head->read = tomoyo_read_stat;
2374 head->readbuf_size = 1024;
Kentaro Takeda95908372009-02-05 17:18:13 +09002375 break;
2376 case TOMOYO_PROFILE:
2377 /* /sys/kernel/security/tomoyo/profile */
2378 head->write = tomoyo_write_profile;
2379 head->read = tomoyo_read_profile;
2380 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002381 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
2382 head->poll = tomoyo_poll_query;
2383 head->write = tomoyo_write_answer;
2384 head->read = tomoyo_read_query;
2385 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09002386 case TOMOYO_MANAGER:
2387 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09002388 head->write = tomoyo_write_manager;
2389 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09002390 break;
2391 }
2392 if (!(file->f_mode & FMODE_READ)) {
2393 /*
2394 * No need to allocate read_buf since it is not opened
2395 * for reading.
2396 */
2397 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002398 head->poll = NULL;
2399 } else if (!head->poll) {
2400 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09002401 if (!head->readbuf_size)
2402 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002403 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002404 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002405 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002406 return -ENOMEM;
2407 }
2408 }
2409 if (!(file->f_mode & FMODE_WRITE)) {
2410 /*
2411 * No need to allocate write_buf since it is not opened
2412 * for writing.
2413 */
2414 head->write = NULL;
2415 } else if (head->write) {
2416 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09002417 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09002418 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002419 kfree(head->read_buf);
2420 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002421 return -ENOMEM;
2422 }
2423 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002424 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002425 * If the file is /sys/kernel/security/tomoyo/query , increment the
2426 * observer counter.
2427 * The obserber counter is used by tomoyo_supervisor() to see if
2428 * there is some process monitoring /sys/kernel/security/tomoyo/query.
2429 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09002430 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002431 atomic_inc(&tomoyo_query_observers);
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002432 file->private_data = head;
2433 tomoyo_notify_gc(head, true);
Kentaro Takeda95908372009-02-05 17:18:13 +09002434 return 0;
2435}
2436
2437/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09002438 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
2439 *
2440 * @file: Pointer to "struct file".
2441 * @wait: Pointer to "poll_table".
2442 *
2443 * Waits for read readiness.
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09002444 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
2445 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09002446 */
2447int tomoyo_poll_control(struct file *file, poll_table *wait)
2448{
2449 struct tomoyo_io_buffer *head = file->private_data;
2450 if (!head->poll)
2451 return -ENOSYS;
2452 return head->poll(file, wait);
2453}
2454
2455/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002456 * tomoyo_set_namespace_cursor - Set namespace to read.
2457 *
2458 * @head: Pointer to "struct tomoyo_io_buffer".
2459 *
2460 * Returns nothing.
2461 */
2462static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
2463{
2464 struct list_head *ns;
2465 if (head->type != TOMOYO_EXCEPTIONPOLICY &&
2466 head->type != TOMOYO_PROFILE)
2467 return;
2468 /*
2469 * If this is the first read, or reading previous namespace finished
2470 * and has more namespaces to read, update the namespace cursor.
2471 */
2472 ns = head->r.ns;
2473 if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
2474 /* Clearing is OK because tomoyo_flush() returned true. */
2475 memset(&head->r, 0, sizeof(head->r));
2476 head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
2477 }
2478}
2479
2480/**
2481 * tomoyo_has_more_namespace - Check for unread namespaces.
2482 *
2483 * @head: Pointer to "struct tomoyo_io_buffer".
2484 *
2485 * Returns true if we have more entries to print, false otherwise.
2486 */
2487static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
2488{
2489 return (head->type == TOMOYO_EXCEPTIONPOLICY ||
2490 head->type == TOMOYO_PROFILE) && head->r.eof &&
2491 head->r.ns->next != &tomoyo_namespace_list;
2492}
2493
2494/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002495 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2496 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002497 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002498 * @buffer: Poiner to buffer to write to.
2499 * @buffer_len: Size of @buffer.
2500 *
2501 * Returns bytes read on success, negative value otherwise.
2502 */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002503ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
2504 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002505{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002506 int len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002507 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002508
2509 if (!head->read)
2510 return -ENOSYS;
2511 if (mutex_lock_interruptible(&head->io_sem))
2512 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002513 head->read_user_buf = buffer;
2514 head->read_user_buf_avail = buffer_len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002515 idx = tomoyo_read_lock();
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002516 if (tomoyo_flush(head))
2517 /* Call the policy handler. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002518 do {
2519 tomoyo_set_namespace_cursor(head);
2520 head->read(head);
2521 } while (tomoyo_flush(head) &&
2522 tomoyo_has_more_namespace(head));
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002523 tomoyo_read_unlock(idx);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002524 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09002525 mutex_unlock(&head->io_sem);
2526 return len;
2527}
2528
2529/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002530 * tomoyo_parse_policy - Parse a policy line.
2531 *
2532 * @head: Poiter to "struct tomoyo_io_buffer".
2533 * @line: Line to parse.
2534 *
2535 * Returns 0 on success, negative value otherwise.
2536 *
2537 * Caller holds tomoyo_read_lock().
2538 */
2539static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2540{
2541 /* Delete request? */
2542 head->w.is_delete = !strncmp(line, "delete ", 7);
2543 if (head->w.is_delete)
2544 memmove(line, line + 7, strlen(line + 7) + 1);
2545 /* Selecting namespace to update. */
2546 if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2547 head->type == TOMOYO_PROFILE) {
2548 if (*line == '<') {
2549 char *cp = strchr(line, ' ');
2550 if (cp) {
2551 *cp++ = '\0';
2552 head->w.ns = tomoyo_assign_namespace(line);
2553 memmove(line, cp, strlen(cp) + 1);
2554 } else
2555 head->w.ns = NULL;
2556 } else
2557 head->w.ns = &tomoyo_kernel_namespace;
2558 /* Don't allow updating if namespace is invalid. */
2559 if (!head->w.ns)
2560 return -ENOENT;
2561 }
2562 /* Do the update. */
2563 return head->write(head);
2564}
2565
2566/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002567 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2568 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002569 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002570 * @buffer: Pointer to buffer to read from.
2571 * @buffer_len: Size of @buffer.
2572 *
2573 * Returns @buffer_len on success, negative value otherwise.
2574 */
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09002575ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
2576 const char __user *buffer, const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002577{
Kentaro Takeda95908372009-02-05 17:18:13 +09002578 int error = buffer_len;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002579 size_t avail_len = buffer_len;
Kentaro Takeda95908372009-02-05 17:18:13 +09002580 char *cp0 = head->write_buf;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002581 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002582 if (!head->write)
2583 return -ENOSYS;
2584 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2585 return -EFAULT;
Kentaro Takeda95908372009-02-05 17:18:13 +09002586 if (mutex_lock_interruptible(&head->io_sem))
2587 return -EINTR;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002588 idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09002589 /* Read a line and dispatch it to the policy handler. */
2590 while (avail_len > 0) {
2591 char c;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002592 if (head->w.avail >= head->writebuf_size - 1) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002593 const int len = head->writebuf_size * 2;
2594 char *cp = kzalloc(len, GFP_NOFS);
2595 if (!cp) {
2596 error = -ENOMEM;
2597 break;
2598 }
2599 memmove(cp, cp0, head->w.avail);
2600 kfree(cp0);
2601 head->write_buf = cp;
2602 cp0 = cp;
2603 head->writebuf_size = len;
2604 }
2605 if (get_user(c, buffer)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09002606 error = -EFAULT;
2607 break;
2608 }
2609 buffer++;
2610 avail_len--;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002611 cp0[head->w.avail++] = c;
Kentaro Takeda95908372009-02-05 17:18:13 +09002612 if (c != '\n')
2613 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002614 cp0[head->w.avail - 1] = '\0';
2615 head->w.avail = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09002616 tomoyo_normalize_line(cp0);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002617 if (!strcmp(cp0, "reset")) {
2618 head->w.ns = &tomoyo_kernel_namespace;
2619 head->w.domain = NULL;
2620 memset(&head->r, 0, sizeof(head->r));
2621 continue;
2622 }
2623 /* Don't allow updating policies by non manager programs. */
2624 switch (head->type) {
2625 case TOMOYO_PROCESS_STATUS:
2626 /* This does not write anything. */
2627 break;
2628 case TOMOYO_DOMAINPOLICY:
2629 if (tomoyo_select_domain(head, cp0))
2630 continue;
2631 /* fall through */
2632 case TOMOYO_EXCEPTIONPOLICY:
2633 if (!strcmp(cp0, "select transition_only")) {
2634 head->r.print_transition_related_only = true;
2635 continue;
2636 }
2637 /* fall through */
2638 default:
2639 if (!tomoyo_manager()) {
2640 error = -EPERM;
2641 goto out;
2642 }
2643 }
2644 switch (tomoyo_parse_policy(head, cp0)) {
2645 case -EPERM:
2646 error = -EPERM;
2647 goto out;
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002648 case 0:
2649 switch (head->type) {
2650 case TOMOYO_DOMAINPOLICY:
2651 case TOMOYO_EXCEPTIONPOLICY:
Tetsuo Handab22b8b92011-06-26 23:21:50 +09002652 case TOMOYO_STAT:
2653 case TOMOYO_PROFILE:
2654 case TOMOYO_MANAGER:
2655 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
2656 break;
2657 default:
2658 break;
2659 }
2660 break;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002661 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002662 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002663out:
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002664 tomoyo_read_unlock(idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002665 mutex_unlock(&head->io_sem);
2666 return error;
2667}
2668
2669/**
2670 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2671 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002672 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002673 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002674 * Returns 0.
Kentaro Takeda95908372009-02-05 17:18:13 +09002675 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002676int tomoyo_close_control(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002677{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002678 /*
2679 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2680 * observer counter.
2681 */
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002682 if (head->type == TOMOYO_QUERY &&
2683 atomic_dec_and_test(&tomoyo_query_observers))
2684 wake_up_all(&tomoyo_answer_wait);
2685 tomoyo_notify_gc(head, false);
Kentaro Takeda95908372009-02-05 17:18:13 +09002686 return 0;
2687}
2688
2689/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002690 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09002691 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002692void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09002693{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002694 struct tomoyo_domain_info *domain;
2695 const int idx = tomoyo_read_lock();
2696 tomoyo_policy_loaded = true;
Tetsuo Handa843d1832011-09-14 17:03:19 +09002697 printk(KERN_INFO "TOMOYO: 2.5.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002698 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2699 const u8 profile = domain->profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002700 const struct tomoyo_policy_namespace *ns = domain->ns;
Tetsuo Handa843d1832011-09-14 17:03:19 +09002701 if (ns->profile_version != 20110903)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002702 printk(KERN_ERR
2703 "Profile version %u is not supported.\n",
2704 ns->profile_version);
2705 else if (!ns->profile_ptr[profile])
2706 printk(KERN_ERR
2707 "Profile %u (used by '%s') is not defined.\n",
2708 profile, domain->domainname->name);
2709 else
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002710 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002711 printk(KERN_ERR
Tetsuo Handa843d1832011-09-14 17:03:19 +09002712 "Userland tools for TOMOYO 2.5 must be installed and "
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002713 "policy must be initialized.\n");
Tetsuo Handa843d1832011-09-14 17:03:19 +09002714 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002715 "for more information.\n");
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002716 panic("STOP!");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002717 }
2718 tomoyo_read_unlock(idx);
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002719 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002720}
Tetsuo Handaefe836a2011-06-26 23:22:18 +09002721
2722/**
2723 * tomoyo_load_builtin_policy - Load built-in policy.
2724 *
2725 * Returns nothing.
2726 */
2727void __init tomoyo_load_builtin_policy(void)
2728{
2729 /*
2730 * This include file is manually created and contains built-in policy
2731 * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
2732 * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
2733 * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
2734 */
2735#include "builtin-policy.h"
2736 u8 i;
2737 const int idx = tomoyo_read_lock();
2738 for (i = 0; i < 5; i++) {
2739 struct tomoyo_io_buffer head = { };
2740 char *start = "";
2741 switch (i) {
2742 case 0:
2743 start = tomoyo_builtin_profile;
2744 head.type = TOMOYO_PROFILE;
2745 head.write = tomoyo_write_profile;
2746 break;
2747 case 1:
2748 start = tomoyo_builtin_exception_policy;
2749 head.type = TOMOYO_EXCEPTIONPOLICY;
2750 head.write = tomoyo_write_exception;
2751 break;
2752 case 2:
2753 start = tomoyo_builtin_domain_policy;
2754 head.type = TOMOYO_DOMAINPOLICY;
2755 head.write = tomoyo_write_domain;
2756 break;
2757 case 3:
2758 start = tomoyo_builtin_manager;
2759 head.type = TOMOYO_MANAGER;
2760 head.write = tomoyo_write_manager;
2761 break;
2762 case 4:
2763 start = tomoyo_builtin_stat;
2764 head.type = TOMOYO_STAT;
2765 head.write = tomoyo_write_stat;
2766 break;
2767 }
2768 while (1) {
2769 char *end = strchr(start, '\n');
2770 if (!end)
2771 break;
2772 *end = '\0';
2773 tomoyo_normalize_line(start);
2774 head.write_buf = start;
2775 tomoyo_parse_policy(&head, start);
2776 start = end + 1;
2777 }
2778 }
2779 tomoyo_read_unlock(idx);
Tetsuo Handa0e4ae0e2011-06-26 23:22:59 +09002780#ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
2781 tomoyo_check_profile();
2782#endif
Tetsuo Handaefe836a2011-06-26 23:22:18 +09002783}