blob: 691c34025a4a87c543bb31adb7f231615a10d3a5 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.c
3 *
4 * Common functions for TOMOYO.
5 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takeda95908372009-02-05 17:18:13 +09007 */
8
9#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090011#include <linux/security.h>
Kentaro Takeda95908372009-02-05 17:18:13 +090012#include "common.h"
Kentaro Takeda95908372009-02-05 17:18:13 +090013
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090014/* String table for operation mode. */
15const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
16 [TOMOYO_CONFIG_DISABLED] = "disabled",
17 [TOMOYO_CONFIG_LEARNING] = "learning",
18 [TOMOYO_CONFIG_PERMISSIVE] = "permissive",
19 [TOMOYO_CONFIG_ENFORCING] = "enforcing"
Kentaro Takeda95908372009-02-05 17:18:13 +090020};
Kentaro Takeda95908372009-02-05 17:18:13 +090021
Tetsuo Handa57c25902010-06-03 20:38:44 +090022/* String table for /sys/kernel/security/tomoyo/profile */
23static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
24 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
25 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
26 [TOMOYO_MAC_FILE_OPEN] = "file::open",
27 [TOMOYO_MAC_FILE_CREATE] = "file::create",
28 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
Tetsuo Handa7c759642011-06-26 23:15:31 +090029 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
Tetsuo Handa57c25902010-06-03 20:38:44 +090030 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
31 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
32 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
33 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
34 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
35 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
Tetsuo Handa57c25902010-06-03 20:38:44 +090036 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
37 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
38 [TOMOYO_MAC_FILE_LINK] = "file::link",
39 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
40 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
41 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
42 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
43 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
44 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
45 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090046 [TOMOYO_MAC_FILE_UMOUNT] = "file::unmount",
Tetsuo Handa57c25902010-06-03 20:38:44 +090047 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
48 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Kentaro Takeda95908372009-02-05 17:18:13 +090049};
50
Tetsuo Handad5ca1722011-06-26 23:18:21 +090051/* String table for PREFERENCE keyword. */
52static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090053 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
Tetsuo Handad5ca1722011-06-26 23:18:21 +090054 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
55};
56
Kentaro Takeda95908372009-02-05 17:18:13 +090057/* Permit policy management by non-root user? */
58static bool tomoyo_manage_by_non_root;
59
60/* Utility functions. */
61
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090062/**
Tetsuo Handa57c25902010-06-03 20:38:44 +090063 * tomoyo_yesno - Return "yes" or "no".
64 *
65 * @value: Bool value.
66 */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090067const char *tomoyo_yesno(const unsigned int value)
Tetsuo Handa57c25902010-06-03 20:38:44 +090068{
69 return value ? "yes" : "no";
70}
71
Tetsuo Handad5ca1722011-06-26 23:18:21 +090072/**
73 * tomoyo_addprintf - strncat()-like-snprintf().
74 *
75 * @buffer: Buffer to write to. Must be '\0'-terminated.
76 * @len: Size of @buffer.
77 * @fmt: The printf()'s format string, followed by parameters.
78 *
79 * Returns nothing.
80 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +090081static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
82{
83 va_list args;
84 const int pos = strlen(buffer);
85 va_start(args, fmt);
86 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
87 va_end(args);
88}
89
90/**
91 * tomoyo_flush - Flush queued string to userspace's buffer.
92 *
93 * @head: Pointer to "struct tomoyo_io_buffer".
94 *
95 * Returns true if all data was flushed, false otherwise.
96 */
97static bool tomoyo_flush(struct tomoyo_io_buffer *head)
98{
99 while (head->r.w_pos) {
100 const char *w = head->r.w[0];
101 int len = strlen(w);
102 if (len) {
103 if (len > head->read_user_buf_avail)
104 len = head->read_user_buf_avail;
105 if (!len)
106 return false;
107 if (copy_to_user(head->read_user_buf, w, len))
108 return false;
109 head->read_user_buf_avail -= len;
110 head->read_user_buf += len;
111 w += len;
112 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900113 head->r.w[0] = w;
114 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900115 return false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900116 /* Add '\0' for audit logs and query. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900117 if (head->poll) {
118 if (!head->read_user_buf_avail ||
119 copy_to_user(head->read_user_buf, "", 1))
120 return false;
121 head->read_user_buf_avail--;
122 head->read_user_buf++;
123 }
124 head->r.w_pos--;
125 for (len = 0; len < head->r.w_pos; len++)
126 head->r.w[len] = head->r.w[len + 1];
127 }
128 head->r.avail = 0;
129 return true;
130}
131
132/**
133 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
134 *
135 * @head: Pointer to "struct tomoyo_io_buffer".
136 * @string: String to print.
137 *
138 * Note that @string has to be kept valid until @head is kfree()d.
139 * This means that char[] allocated on stack memory cannot be passed to
140 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
141 */
142static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
143{
144 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
145 head->r.w[head->r.w_pos++] = string;
146 tomoyo_flush(head);
147 } else
148 WARN_ON(1);
149}
150
151/**
152 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
153 *
154 * @head: Pointer to "struct tomoyo_io_buffer".
155 * @fmt: The printf()'s format string, followed by parameters.
156 */
157void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
158{
159 va_list args;
160 int len;
161 int pos = head->r.avail;
162 int size = head->readbuf_size - pos;
163 if (size <= 0)
164 return;
165 va_start(args, fmt);
166 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
167 va_end(args);
168 if (pos + len >= head->readbuf_size) {
169 WARN_ON(1);
170 return;
171 }
172 head->r.avail += len;
173 tomoyo_set_string(head, head->read_buf + pos);
174}
175
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900176/**
177 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
178 *
179 * @head: Pointer to "struct tomoyo_io_buffer".
180 *
181 * Returns nothing.
182 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900183static void tomoyo_set_space(struct tomoyo_io_buffer *head)
184{
185 tomoyo_set_string(head, " ");
186}
187
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900188/**
189 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
190 *
191 * @head: Pointer to "struct tomoyo_io_buffer".
192 *
193 * Returns nothing.
194 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900195static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
196{
197 tomoyo_set_string(head, "\n");
198 return !head->r.w_pos;
199}
200
Tetsuo Handa57c25902010-06-03 20:38:44 +0900201/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900202 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
203 *
204 * @head: Pointer to "struct tomoyo_io_buffer".
205 *
206 * Returns nothing.
207 */
208static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
209{
210 tomoyo_set_string(head, "/");
211}
212
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900213/* List of namespaces. */
214LIST_HEAD(tomoyo_namespace_list);
215/* True if namespace other than tomoyo_kernel_namespace is defined. */
216static bool tomoyo_namespace_enabled;
217
218/**
219 * tomoyo_init_policy_namespace - Initialize namespace.
220 *
221 * @ns: Pointer to "struct tomoyo_policy_namespace".
222 *
223 * Returns nothing.
224 */
225void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
226{
227 unsigned int idx;
228 for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
229 INIT_LIST_HEAD(&ns->acl_group[idx]);
230 for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
231 INIT_LIST_HEAD(&ns->group_list[idx]);
232 for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
233 INIT_LIST_HEAD(&ns->policy_list[idx]);
234 ns->profile_version = 20100903;
235 tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
236 list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
237}
238
239/**
240 * tomoyo_print_namespace - Print namespace header.
241 *
242 * @head: Pointer to "struct tomoyo_io_buffer".
243 *
244 * Returns nothing.
245 */
246static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
247{
248 if (!tomoyo_namespace_enabled)
249 return;
250 tomoyo_set_string(head,
251 container_of(head->r.ns,
252 struct tomoyo_policy_namespace,
253 namespace_list)->name);
254 tomoyo_set_space(head);
255}
256
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900257/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900258 * tomoyo_print_name_union - Print a tomoyo_name_union.
259 *
260 * @head: Pointer to "struct tomoyo_io_buffer".
261 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900262 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900263static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
264 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900265{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900266 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900267 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900268 tomoyo_set_string(head, "@");
269 tomoyo_set_string(head, ptr->group->group_name->name);
270 } else {
271 tomoyo_set_string(head, ptr->filename->name);
272 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900273}
274
275/**
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900276 * tomoyo_print_number_union - Print a tomoyo_number_union.
277 *
278 * @head: Pointer to "struct tomoyo_io_buffer".
279 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900280 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900281static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
282 const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900283{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900284 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900285 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900286 tomoyo_set_string(head, "@");
287 tomoyo_set_string(head, ptr->group->group_name->name);
288 } else {
289 int i;
290 unsigned long min = ptr->values[0];
291 const unsigned long max = ptr->values[1];
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900292 u8 min_type = ptr->value_type[0];
293 const u8 max_type = ptr->value_type[1];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900294 char buffer[128];
295 buffer[0] = '\0';
296 for (i = 0; i < 2; i++) {
297 switch (min_type) {
298 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
299 tomoyo_addprintf(buffer, sizeof(buffer),
300 "0x%lX", min);
301 break;
302 case TOMOYO_VALUE_TYPE_OCTAL:
303 tomoyo_addprintf(buffer, sizeof(buffer),
304 "0%lo", min);
305 break;
306 default:
307 tomoyo_addprintf(buffer, sizeof(buffer),
308 "%lu", min);
309 break;
310 }
311 if (min == max && min_type == max_type)
312 break;
313 tomoyo_addprintf(buffer, sizeof(buffer), "-");
314 min_type = max_type;
315 min = max;
316 }
317 tomoyo_io_printf(head, "%s", buffer);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900318 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900319}
320
321/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900322 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900323 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900324 * @ns: Pointer to "struct tomoyo_policy_namespace".
Kentaro Takeda95908372009-02-05 17:18:13 +0900325 * @profile: Profile number to create.
326 *
327 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
328 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900329static struct tomoyo_profile *tomoyo_assign_profile
330(struct tomoyo_policy_namespace *ns, const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900331{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900332 struct tomoyo_profile *ptr;
333 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900334 if (profile >= TOMOYO_MAX_PROFILES)
335 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900336 ptr = ns->profile_ptr[profile];
Kentaro Takeda95908372009-02-05 17:18:13 +0900337 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900338 return ptr;
339 entry = kzalloc(sizeof(*entry), GFP_NOFS);
340 if (mutex_lock_interruptible(&tomoyo_policy_lock))
341 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900342 ptr = ns->profile_ptr[profile];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900343 if (!ptr && tomoyo_memory_ok(entry)) {
344 ptr = entry;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900345 ptr->default_config = TOMOYO_CONFIG_DISABLED |
346 TOMOYO_CONFIG_WANT_GRANT_LOG |
347 TOMOYO_CONFIG_WANT_REJECT_LOG;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900348 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
349 sizeof(ptr->config));
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900350 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900351 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900352 mb(); /* Avoid out-of-order execution. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900353 ns->profile_ptr[profile] = ptr;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900354 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900355 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900356 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900357 out:
358 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900359 return ptr;
360}
361
362/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900363 * tomoyo_profile - Find a profile.
364 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900365 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900366 * @profile: Profile number to find.
367 *
368 * Returns pointer to "struct tomoyo_profile".
369 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900370struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
371 const u8 profile)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900372{
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900373 static struct tomoyo_profile tomoyo_null_profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900374 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900375 if (!ptr)
376 ptr = &tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900377 return ptr;
378}
379
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900380/**
381 * tomoyo_find_yesno - Find values for specified keyword.
382 *
383 * @string: String to check.
384 * @find: Name of keyword.
385 *
386 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
387 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900388static s8 tomoyo_find_yesno(const char *string, const char *find)
389{
390 const char *cp = strstr(string, find);
391 if (cp) {
392 cp += strlen(find);
393 if (!strncmp(cp, "=yes", 4))
394 return 1;
395 else if (!strncmp(cp, "=no", 3))
396 return 0;
397 }
398 return -1;
399}
400
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900401/**
402 * tomoyo_set_uint - Set value for specified preference.
403 *
404 * @i: Pointer to "unsigned int".
405 * @string: String to check.
406 * @find: Name of keyword.
407 *
408 * Returns nothing.
409 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900410static void tomoyo_set_uint(unsigned int *i, const char *string,
411 const char *find)
412{
413 const char *cp = strstr(string, find);
414 if (cp)
415 sscanf(cp + strlen(find), "=%u", i);
416}
417
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900418/**
419 * tomoyo_set_mode - Set mode for specified profile.
420 *
421 * @name: Name of functionality.
422 * @value: Mode for @name.
423 * @profile: Pointer to "struct tomoyo_profile".
424 *
425 * Returns 0 on success, negative value otherwise.
426 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900427static int tomoyo_set_mode(char *name, const char *value,
Tetsuo Handa8e568682010-06-25 09:30:09 +0900428 struct tomoyo_profile *profile)
429{
430 u8 i;
431 u8 config;
432 if (!strcmp(name, "CONFIG")) {
433 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
434 config = profile->default_config;
435 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
436 config = 0;
437 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
438 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
439 if (strcmp(name, tomoyo_mac_keywords[i]))
440 continue;
441 config = profile->config[i];
442 break;
443 }
444 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
445 return -EINVAL;
446 } else {
447 return -EINVAL;
448 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900449 if (strstr(value, "use_default")) {
Tetsuo Handa8e568682010-06-25 09:30:09 +0900450 config = TOMOYO_CONFIG_USE_DEFAULT;
451 } else {
452 u8 mode;
453 for (mode = 0; mode < 4; mode++)
454 if (strstr(value, tomoyo_mode[mode]))
455 /*
456 * Update lower 3 bits in order to distinguish
457 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
458 */
459 config = (config & ~7) | mode;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900460 if (config != TOMOYO_CONFIG_USE_DEFAULT) {
461 switch (tomoyo_find_yesno(value, "grant_log")) {
462 case 1:
463 config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
464 break;
465 case 0:
466 config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
467 break;
468 }
469 switch (tomoyo_find_yesno(value, "reject_log")) {
470 case 1:
471 config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
472 break;
473 case 0:
474 config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
475 break;
476 }
477 }
Tetsuo Handa8e568682010-06-25 09:30:09 +0900478 }
479 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
480 profile->config[i] = config;
481 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
482 profile->default_config = config;
483 return 0;
484}
485
Tetsuo Handa57c25902010-06-03 20:38:44 +0900486/**
487 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900488 *
489 * @head: Pointer to "struct tomoyo_io_buffer".
490 *
491 * Returns 0 on success, negative value otherwise.
492 */
493static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
494{
495 char *data = head->write_buf;
496 unsigned int i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900497 char *cp;
498 struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900499 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
500 == 1)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900501 return 0;
502 i = simple_strtoul(data, &cp, 10);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900503 if (*cp != '-')
504 return -EINVAL;
505 data = cp + 1;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900506 profile = tomoyo_assign_profile(head->w.ns, i);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900507 if (!profile)
508 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900509 cp = strchr(data, '=');
510 if (!cp)
511 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900512 *cp++ = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +0900513 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900514 static DEFINE_SPINLOCK(lock);
515 const struct tomoyo_path_info *new_comment
516 = tomoyo_get_name(cp);
517 const struct tomoyo_path_info *old_comment;
518 if (!new_comment)
519 return -ENOMEM;
520 spin_lock(&lock);
521 old_comment = profile->comment;
522 profile->comment = new_comment;
523 spin_unlock(&lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900524 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900525 return 0;
526 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900527 if (!strcmp(data, "PREFERENCE")) {
528 for (i = 0; i < TOMOYO_MAX_PREF; i++)
529 tomoyo_set_uint(&profile->pref[i], cp,
530 tomoyo_pref_keywords[i]);
531 return 0;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900532 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900533 return tomoyo_set_mode(data, cp, profile);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900534}
535
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900536/**
537 * tomoyo_print_config - Print mode for specified functionality.
538 *
539 * @head: Pointer to "struct tomoyo_io_buffer".
540 * @config: Mode for that functionality.
541 *
542 * Returns nothing.
543 *
544 * Caller prints functionality's name.
545 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900546static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
547{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900548 tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
549 tomoyo_mode[config & 3],
550 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
551 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900552}
553
Kentaro Takeda95908372009-02-05 17:18:13 +0900554/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900555 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900556 *
557 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900558 *
559 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900560 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900561static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900562{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900563 u8 index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900564 struct tomoyo_policy_namespace *ns =
565 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900566 const struct tomoyo_profile *profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900567 if (head->r.eof)
568 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900569 next:
570 index = head->r.index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900571 profile = ns->profile_ptr[index];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900572 switch (head->r.step) {
573 case 0:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900574 tomoyo_print_namespace(head);
575 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
576 ns->profile_version);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900577 head->r.step++;
578 break;
579 case 1:
580 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
581 head->r.index++)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900582 if (ns->profile_ptr[head->r.index])
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900583 break;
584 if (head->r.index == TOMOYO_MAX_PROFILES)
585 return;
586 head->r.step++;
587 break;
588 case 2:
589 {
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900590 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900591 const struct tomoyo_path_info *comment =
592 profile->comment;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900593 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900594 tomoyo_io_printf(head, "%u-COMMENT=", index);
595 tomoyo_set_string(head, comment ? comment->name : "");
596 tomoyo_set_lf(head);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900597 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
598 for (i = 0; i < TOMOYO_MAX_PREF; i++)
599 tomoyo_io_printf(head, "%s=%u ",
600 tomoyo_pref_keywords[i],
601 profile->pref[i]);
602 tomoyo_set_string(head, "}\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900603 head->r.step++;
604 }
605 break;
606 case 3:
607 {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900608 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900609 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
610 tomoyo_print_config(head, profile->default_config);
611 head->r.bit = 0;
612 head->r.step++;
613 }
614 break;
615 case 4:
616 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
617 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
618 const u8 i = head->r.bit;
619 const u8 config = profile->config[i];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900620 if (config == TOMOYO_CONFIG_USE_DEFAULT)
621 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900622 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900623 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
624 tomoyo_mac_keywords[i]);
625 tomoyo_print_config(head, config);
626 head->r.bit++;
627 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900628 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900629 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
630 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900631 head->r.index++;
632 head->r.step = 1;
633 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900634 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900635 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900636 if (tomoyo_flush(head))
637 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900638}
639
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900640static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
641 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900642{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900643 return container_of(a, struct tomoyo_manager, head)->manager ==
644 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900645}
646
Kentaro Takeda95908372009-02-05 17:18:13 +0900647/**
648 * tomoyo_update_manager_entry - Add a manager entry.
649 *
650 * @manager: The path to manager or the domainnamme.
651 * @is_delete: True if it is a delete request.
652 *
653 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900654 *
655 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900656 */
657static int tomoyo_update_manager_entry(const char *manager,
658 const bool is_delete)
659{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900660 struct tomoyo_manager e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900661 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900662 /* .ns = &tomoyo_kernel_namespace, */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900663 .is_delete = is_delete,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900664 .list = &tomoyo_kernel_namespace.
665 policy_list[TOMOYO_ID_MANAGER],
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900666 };
667 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900668 if (tomoyo_domain_def(manager)) {
669 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900670 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900671 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900672 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900673 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900674 return -EINVAL;
675 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900676 e.manager = tomoyo_get_name(manager);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900677 if (e.manager) {
678 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
679 tomoyo_same_manager);
680 tomoyo_put_name(e.manager);
681 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900682 return error;
683}
684
685/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900686 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900687 *
688 * @head: Pointer to "struct tomoyo_io_buffer".
689 *
690 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900691 *
692 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900693 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900694static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900695{
696 char *data = head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +0900697
698 if (!strcmp(data, "manage_by_non_root")) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900699 tomoyo_manage_by_non_root = !head->w.is_delete;
Kentaro Takeda95908372009-02-05 17:18:13 +0900700 return 0;
701 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900702 return tomoyo_update_manager_entry(data, head->w.is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900703}
704
705/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900706 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900707 *
708 * @head: Pointer to "struct tomoyo_io_buffer".
709 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900710 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900711 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900712static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900713{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900714 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900715 return;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900716 list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
717 policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900718 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900719 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900720 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900721 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900722 if (!tomoyo_flush(head))
723 return;
724 tomoyo_set_string(head, ptr->manager->name);
725 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900726 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900727 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900728}
729
730/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900731 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900732 *
733 * Returns true if the current process is permitted to modify policy
734 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900735 *
736 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900737 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900738static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900739{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900740 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900741 const char *exe;
742 const struct task_struct *task = current;
743 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
744 bool found = false;
745
746 if (!tomoyo_policy_loaded)
747 return true;
748 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
749 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900750 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
751 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900752 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900753 && !tomoyo_pathcmp(domainname, ptr->manager)) {
754 found = true;
755 break;
756 }
757 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900758 if (found)
759 return true;
760 exe = tomoyo_get_exe();
761 if (!exe)
762 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900763 list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
764 policy_list[TOMOYO_ID_MANAGER], head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900765 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900766 && !strcmp(exe, ptr->manager->name)) {
767 found = true;
768 break;
769 }
770 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900771 if (!found) { /* Reduce error messages. */
772 static pid_t last_pid;
773 const pid_t pid = current->pid;
774 if (last_pid != pid) {
775 printk(KERN_WARNING "%s ( %s ) is not permitted to "
776 "update policies.\n", domainname->name, exe);
777 last_pid = pid;
778 }
779 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900780 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900781 return found;
782}
783
784/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900785 * tomoyo_select_domain - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900786 *
787 * @head: Pointer to "struct tomoyo_io_buffer".
788 * @data: String to parse.
789 *
790 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900791 *
792 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900793 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900794static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
795 const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900796{
797 unsigned int pid;
798 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900799 bool global_pid = false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900800 if (strncmp(data, "select ", 7))
801 return false;
802 data += 7;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900803 if (sscanf(data, "pid=%u", &pid) == 1 ||
804 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900805 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900806 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900807 read_lock(&tasklist_lock);
Tetsuo Handa9b244372010-06-03 20:35:53 +0900808 if (global_pid)
809 p = find_task_by_pid_ns(pid, &init_pid_ns);
810 else
811 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900812 if (p)
813 domain = tomoyo_real_domain(p);
814 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900815 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900816 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900817 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900818 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900819 } else
820 return false;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900821 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900822 /* Accessing read_buf is safe because head->io_sem is held. */
823 if (!head->read_buf)
824 return true; /* Do nothing if open(O_WRONLY). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900825 memset(&head->r, 0, sizeof(head->r));
826 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +0200827 if (domain)
828 head->r.domain = &domain->list;
829 else
830 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +0900831 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900832 if (domain && domain->is_deleted)
833 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900834 return true;
835}
836
837/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900838 * tomoyo_delete_domain - Delete a domain.
839 *
840 * @domainname: The name of domain.
841 *
842 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900843 *
844 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900845 */
846static int tomoyo_delete_domain(char *domainname)
847{
848 struct tomoyo_domain_info *domain;
849 struct tomoyo_path_info name;
850
851 name.name = domainname;
852 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +0900853 if (mutex_lock_interruptible(&tomoyo_policy_lock))
854 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900855 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900856 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900857 /* Never delete tomoyo_kernel_domain */
858 if (domain == &tomoyo_kernel_domain)
859 continue;
860 if (domain->is_deleted ||
861 tomoyo_pathcmp(domain->domainname, &name))
862 continue;
863 domain->is_deleted = true;
864 break;
865 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900866 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900867 return 0;
868}
869
870/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900871 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900872 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900873 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900874 * @list: Pointer to "struct list_head".
875 * @data: Policy to be interpreted.
876 * @is_delete: True if it is a delete request.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900877 *
878 * Returns 0 on success, negative value otherwise.
879 *
880 * Caller holds tomoyo_read_lock().
881 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900882static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
883 struct list_head *list, char *data,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900884 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900885{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900886 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900887 .ns = ns,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900888 .list = list,
889 .data = data,
890 .is_delete = is_delete,
891 };
892 static const struct {
893 const char *keyword;
894 int (*write) (struct tomoyo_acl_param *);
895 } tomoyo_callback[1] = {
896 { "file ", tomoyo_write_file },
897 };
898 u8 i;
899 for (i = 0; i < 1; i++) {
900 if (!tomoyo_str_starts(&param.data,
901 tomoyo_callback[i].keyword))
902 continue;
903 return tomoyo_callback[i].write(&param);
904 }
905 return -EINVAL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900906}
907
908/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900909 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900910 *
911 * @head: Pointer to "struct tomoyo_io_buffer".
912 *
913 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900914 *
915 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900916 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900917static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900918{
919 char *data = head->write_buf;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900920 struct tomoyo_policy_namespace *ns;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900921 struct tomoyo_domain_info *domain = head->w.domain;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900922 const bool is_delete = head->w.is_delete;
923 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
Kentaro Takeda95908372009-02-05 17:18:13 +0900924 unsigned int profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900925 if (*data == '<') {
Kentaro Takeda95908372009-02-05 17:18:13 +0900926 domain = NULL;
927 if (is_delete)
928 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900929 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +0900930 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900931 else
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900932 domain = tomoyo_assign_domain(data, false);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900933 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900934 return 0;
935 }
936 if (!domain)
937 return -EINVAL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900938 ns = domain->ns;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900939 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +0900940 && profile < TOMOYO_MAX_PROFILES) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900941 if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
Kentaro Takeda95908372009-02-05 17:18:13 +0900942 domain->profile = (u8) profile;
943 return 0;
944 }
Tetsuo Handa32997142011-06-26 23:19:28 +0900945 if (sscanf(data, "use_group %u\n", &profile) == 1
946 && profile < TOMOYO_MAX_ACL_GROUPS) {
947 if (!is_delete)
948 domain->group = (u8) profile;
949 return 0;
950 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900951 if (!strcmp(data, "quota_exceeded")) {
Tetsuo Handa9b244372010-06-03 20:35:53 +0900952 domain->quota_warned = !is_delete;
953 return 0;
954 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900955 if (!strcmp(data, "transition_failed")) {
Tetsuo Handa9b244372010-06-03 20:35:53 +0900956 domain->transition_failed = !is_delete;
957 return 0;
958 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900959 return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
960 is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900961}
962
963/**
Tetsuo Handa32997142011-06-26 23:19:28 +0900964 * tomoyo_set_group - Print "acl_group " header keyword and category name.
Kentaro Takeda95908372009-02-05 17:18:13 +0900965 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900966 * @head: Pointer to "struct tomoyo_io_buffer".
967 * @category: Category name.
Kentaro Takeda95908372009-02-05 17:18:13 +0900968 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900969 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900970 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900971static void tomoyo_set_group(struct tomoyo_io_buffer *head,
972 const char *category)
Kentaro Takeda95908372009-02-05 17:18:13 +0900973{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900974 if (head->type == TOMOYO_EXCEPTIONPOLICY) {
975 tomoyo_print_namespace(head);
Tetsuo Handa32997142011-06-26 23:19:28 +0900976 tomoyo_io_printf(head, "acl_group %u ",
977 head->r.acl_group_index);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900978 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900979 tomoyo_set_string(head, category);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900980}
981
982/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900983 * tomoyo_print_entry - Print an ACL entry.
984 *
985 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900986 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900987 *
988 * Returns true on success, false otherwise.
989 */
990static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900991 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +0900992{
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900993 const u8 acl_type = acl->type;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900994 bool first = true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900995 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +0900996
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900997 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900998 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900999 if (!tomoyo_flush(head))
1000 return false;
1001 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001002 struct tomoyo_path_acl *ptr =
1003 container_of(acl, typeof(*ptr), head);
1004 const u16 perm = ptr->perm;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001005 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001006 if (!(perm & (1 << bit)))
1007 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001008 if (head->r.print_transition_related_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001009 bit != TOMOYO_TYPE_EXECUTE)
1010 continue;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001011 if (first) {
1012 tomoyo_set_group(head, "file ");
1013 first = false;
1014 } else {
1015 tomoyo_set_slash(head);
1016 }
1017 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001018 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001019 if (first)
1020 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001021 tomoyo_print_name_union(head, &ptr->name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001022 } else if (head->r.print_transition_related_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +09001023 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001024 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
1025 struct tomoyo_path2_acl *ptr =
1026 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001027 const u8 perm = ptr->perm;
1028 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
1029 if (!(perm & (1 << bit)))
1030 continue;
1031 if (first) {
1032 tomoyo_set_group(head, "file ");
1033 first = false;
1034 } else {
1035 tomoyo_set_slash(head);
1036 }
1037 tomoyo_set_string(head, tomoyo_mac_keywords
1038 [tomoyo_pp2mac[bit]]);
1039 }
1040 if (first)
1041 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001042 tomoyo_print_name_union(head, &ptr->name1);
1043 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001044 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1045 struct tomoyo_path_number_acl *ptr =
1046 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001047 const u8 perm = ptr->perm;
1048 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
1049 if (!(perm & (1 << bit)))
1050 continue;
1051 if (first) {
1052 tomoyo_set_group(head, "file ");
1053 first = false;
1054 } else {
1055 tomoyo_set_slash(head);
1056 }
1057 tomoyo_set_string(head, tomoyo_mac_keywords
1058 [tomoyo_pn2mac[bit]]);
1059 }
1060 if (first)
1061 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001062 tomoyo_print_name_union(head, &ptr->name);
1063 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001064 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1065 struct tomoyo_mkdev_acl *ptr =
1066 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001067 const u8 perm = ptr->perm;
1068 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
1069 if (!(perm & (1 << bit)))
1070 continue;
1071 if (first) {
1072 tomoyo_set_group(head, "file ");
1073 first = false;
1074 } else {
1075 tomoyo_set_slash(head);
1076 }
1077 tomoyo_set_string(head, tomoyo_mac_keywords
1078 [tomoyo_pnnn2mac[bit]]);
1079 }
1080 if (first)
1081 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001082 tomoyo_print_name_union(head, &ptr->name);
1083 tomoyo_print_number_union(head, &ptr->mode);
1084 tomoyo_print_number_union(head, &ptr->major);
1085 tomoyo_print_number_union(head, &ptr->minor);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001086 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1087 struct tomoyo_mount_acl *ptr =
1088 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001089 tomoyo_set_group(head, "file mount");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001090 tomoyo_print_name_union(head, &ptr->dev_name);
1091 tomoyo_print_name_union(head, &ptr->dir_name);
1092 tomoyo_print_name_union(head, &ptr->fs_type);
1093 tomoyo_print_number_union(head, &ptr->flags);
Kentaro Takeda95908372009-02-05 17:18:13 +09001094 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001095 tomoyo_set_lf(head);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001096 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001097}
1098
1099/**
1100 * tomoyo_read_domain2 - Read domain policy.
1101 *
Tetsuo Handa32997142011-06-26 23:19:28 +09001102 * @head: Pointer to "struct tomoyo_io_buffer".
1103 * @list: Pointer to "struct list_head".
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001104 *
1105 * Caller holds tomoyo_read_lock().
1106 *
1107 * Returns true on success, false otherwise.
1108 */
1109static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
Tetsuo Handa32997142011-06-26 23:19:28 +09001110 struct list_head *list)
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001111{
Tetsuo Handa32997142011-06-26 23:19:28 +09001112 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001113 struct tomoyo_acl_info *ptr =
1114 list_entry(head->r.acl, typeof(*ptr), list);
1115 if (!tomoyo_print_entry(head, ptr))
1116 return false;
1117 }
1118 head->r.acl = NULL;
1119 return true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001120}
1121
1122/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001123 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001124 *
1125 * @head: Pointer to "struct tomoyo_io_buffer".
1126 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001127 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001128 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001129static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001130{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001131 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001132 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001133 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001134 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001135 list_entry(head->r.domain, typeof(*domain), list);
1136 switch (head->r.step) {
1137 case 0:
1138 if (domain->is_deleted &&
1139 !head->r.print_this_domain_only)
1140 continue;
1141 /* Print domainname and flags. */
1142 tomoyo_set_string(head, domain->domainname->name);
1143 tomoyo_set_lf(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001144 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001145 domain->profile);
Tetsuo Handa32997142011-06-26 23:19:28 +09001146 tomoyo_io_printf(head, "use_group %u\n",
1147 domain->group);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001148 if (domain->quota_warned)
1149 tomoyo_set_string(head, "quota_exceeded\n");
1150 if (domain->transition_failed)
1151 tomoyo_set_string(head, "transition_failed\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001152 head->r.step++;
1153 tomoyo_set_lf(head);
1154 /* fall through */
1155 case 1:
Tetsuo Handa32997142011-06-26 23:19:28 +09001156 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001157 return;
1158 head->r.step++;
1159 if (!tomoyo_set_lf(head))
1160 return;
1161 /* fall through */
1162 case 2:
1163 head->r.step = 0;
1164 if (head->r.print_this_domain_only)
1165 goto done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001166 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001167 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001168 done:
1169 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001170}
1171
1172/**
1173 * tomoyo_write_domain_profile - Assign profile for specified domain.
1174 *
1175 * @head: Pointer to "struct tomoyo_io_buffer".
1176 *
1177 * Returns 0 on success, -EINVAL otherwise.
1178 *
1179 * This is equivalent to doing
1180 *
1181 * ( echo "select " $domainname; echo "use_profile " $profile ) |
Tetsuo Handa9b244372010-06-03 20:35:53 +09001182 * /usr/sbin/tomoyo-loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001183 *
1184 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001185 */
1186static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1187{
1188 char *data = head->write_buf;
1189 char *cp = strchr(data, ' ');
1190 struct tomoyo_domain_info *domain;
1191 unsigned long profile;
1192
1193 if (!cp)
1194 return -EINVAL;
1195 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001196 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001197 if (strict_strtoul(data, 10, &profile))
1198 return -EINVAL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001199 if (domain && (!tomoyo_policy_loaded ||
1200 head->w.ns->profile_ptr[(u8) profile]))
Kentaro Takeda95908372009-02-05 17:18:13 +09001201 domain->profile = (u8) profile;
1202 return 0;
1203}
1204
1205/**
1206 * tomoyo_read_domain_profile - Read only domainname and profile.
1207 *
1208 * @head: Pointer to "struct tomoyo_io_buffer".
1209 *
1210 * Returns list of profile number and domainname pairs.
1211 *
1212 * This is equivalent to doing
1213 *
1214 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1215 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1216 * domainname = $0; } else if ( $1 == "use_profile" ) {
1217 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001218 *
1219 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001220 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001221static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001222{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001223 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001224 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001225 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001226 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001227 list_entry(head->r.domain, typeof(*domain), list);
Kentaro Takeda95908372009-02-05 17:18:13 +09001228 if (domain->is_deleted)
1229 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001230 if (!tomoyo_flush(head))
1231 return;
1232 tomoyo_io_printf(head, "%u ", domain->profile);
1233 tomoyo_set_string(head, domain->domainname->name);
1234 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001235 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001236 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001237}
1238
1239/**
1240 * tomoyo_write_pid: Specify PID to obtain domainname.
1241 *
1242 * @head: Pointer to "struct tomoyo_io_buffer".
1243 *
1244 * Returns 0.
1245 */
1246static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1247{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001248 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001249 return 0;
1250}
1251
1252/**
1253 * tomoyo_read_pid - Get domainname of the specified PID.
1254 *
1255 * @head: Pointer to "struct tomoyo_io_buffer".
1256 *
1257 * Returns the domainname which the specified PID is in on success,
1258 * empty string otherwise.
1259 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1260 * using read()/write() interface rather than sysctl() interface.
1261 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001262static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001263{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001264 char *buf = head->write_buf;
1265 bool global_pid = false;
1266 unsigned int pid;
1267 struct task_struct *p;
1268 struct tomoyo_domain_info *domain = NULL;
1269
1270 /* Accessing write_buf is safe because head->io_sem is held. */
1271 if (!buf) {
1272 head->r.eof = true;
1273 return; /* Do nothing if open(O_RDONLY). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001274 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001275 if (head->r.w_pos || head->r.eof)
1276 return;
1277 head->r.eof = true;
1278 if (tomoyo_str_starts(&buf, "global-pid "))
1279 global_pid = true;
1280 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1281 rcu_read_lock();
1282 read_lock(&tasklist_lock);
1283 if (global_pid)
1284 p = find_task_by_pid_ns(pid, &init_pid_ns);
1285 else
1286 p = find_task_by_vpid(pid);
1287 if (p)
1288 domain = tomoyo_real_domain(p);
1289 read_unlock(&tasklist_lock);
1290 rcu_read_unlock();
1291 if (!domain)
1292 return;
1293 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1294 tomoyo_set_string(head, domain->domainname->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001295}
1296
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001297static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001298 [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
1299 [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
1300 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1301 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
1302 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
1303 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001304};
1305
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001306static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001307 [TOMOYO_PATH_GROUP] = "path_group ",
1308 [TOMOYO_NUMBER_GROUP] = "number_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001309};
1310
Kentaro Takeda95908372009-02-05 17:18:13 +09001311/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001312 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001313 *
1314 * @head: Pointer to "struct tomoyo_io_buffer".
1315 *
1316 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001317 *
1318 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001319 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001320static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001321{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001322 const bool is_delete = head->w.is_delete;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001323 struct tomoyo_acl_param param = {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001324 .ns = head->w.ns,
1325 .is_delete = is_delete,
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001326 .data = head->write_buf,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001327 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001328 u8 i;
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001329 if (tomoyo_str_starts(&param.data, "aggregator "))
1330 return tomoyo_write_aggregator(&param);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001331 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001332 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1333 return tomoyo_write_transition_control(&param, i);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001334 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001335 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1336 return tomoyo_write_group(&param, i);
Tetsuo Handa32997142011-06-26 23:19:28 +09001337 if (tomoyo_str_starts(&param.data, "acl_group ")) {
1338 unsigned int group;
1339 char *data;
1340 group = simple_strtoul(param.data, &data, 10);
1341 if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001342 return tomoyo_write_domain2
1343 (head->w.ns, &head->w.ns->acl_group[group],
1344 data, is_delete);
Tetsuo Handa32997142011-06-26 23:19:28 +09001345 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001346 return -EINVAL;
1347}
1348
Tetsuo Handa31845e82010-06-17 16:54:33 +09001349/**
1350 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1351 *
1352 * @head: Pointer to "struct tomoyo_io_buffer".
1353 * @idx: Index number.
1354 *
1355 * Returns true on success, false otherwise.
1356 *
1357 * Caller holds tomoyo_read_lock().
1358 */
1359static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1360{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001361 struct tomoyo_policy_namespace *ns =
1362 container_of(head->r.ns, typeof(*ns), namespace_list);
1363 struct list_head *list = &ns->group_list[idx];
1364 list_for_each_cookie(head->r.group, list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001365 struct tomoyo_group *group =
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001366 list_entry(head->r.group, typeof(*group), head.list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001367 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001368 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001369 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001370 if (ptr->is_deleted)
1371 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001372 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001373 return false;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001374 tomoyo_print_namespace(head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001375 tomoyo_set_string(head, tomoyo_group_name[idx]);
1376 tomoyo_set_string(head, group->group_name->name);
1377 if (idx == TOMOYO_PATH_GROUP) {
1378 tomoyo_set_space(head);
1379 tomoyo_set_string(head, container_of
1380 (ptr, struct tomoyo_path_group,
1381 head)->member_name->name);
1382 } else if (idx == TOMOYO_NUMBER_GROUP) {
1383 tomoyo_print_number_union(head, &container_of
1384 (ptr,
1385 struct tomoyo_number_group,
1386 head)->number);
1387 }
1388 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001389 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001390 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001391 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001392 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001393 return true;
1394}
1395
1396/**
1397 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1398 *
1399 * @head: Pointer to "struct tomoyo_io_buffer".
1400 * @idx: Index number.
1401 *
1402 * Returns true on success, false otherwise.
1403 *
1404 * Caller holds tomoyo_read_lock().
1405 */
1406static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1407{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001408 struct tomoyo_policy_namespace *ns =
1409 container_of(head->r.ns, typeof(*ns), namespace_list);
1410 struct list_head *list = &ns->policy_list[idx];
1411 list_for_each_cookie(head->r.acl, list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001412 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001413 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001414 if (acl->is_deleted)
1415 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001416 if (!tomoyo_flush(head))
1417 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001418 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001419 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001420 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001421 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001422 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001423 tomoyo_print_namespace(head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001424 tomoyo_set_string(head, tomoyo_transition_type
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001425 [ptr->type]);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001426 tomoyo_set_string(head, ptr->program ?
1427 ptr->program->name : "any");
1428 tomoyo_set_string(head, " from ");
1429 tomoyo_set_string(head, ptr->domainname ?
1430 ptr->domainname->name :
1431 "any");
Tetsuo Handa31845e82010-06-17 16:54:33 +09001432 }
1433 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001434 case TOMOYO_ID_AGGREGATOR:
1435 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001436 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001437 container_of(acl, typeof(*ptr), head);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001438 tomoyo_print_namespace(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001439 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001440 tomoyo_set_string(head,
1441 ptr->original_name->name);
1442 tomoyo_set_space(head);
1443 tomoyo_set_string(head,
1444 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001445 }
1446 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001447 default:
1448 continue;
1449 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001450 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001451 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001452 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001453 return true;
1454}
1455
Kentaro Takeda95908372009-02-05 17:18:13 +09001456/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001457 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001458 *
1459 * @head: Pointer to "struct tomoyo_io_buffer".
1460 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001461 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001462 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001463static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001464{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001465 struct tomoyo_policy_namespace *ns =
1466 container_of(head->r.ns, typeof(*ns), namespace_list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001467 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001468 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001469 while (head->r.step < TOMOYO_MAX_POLICY &&
1470 tomoyo_read_policy(head, head->r.step))
1471 head->r.step++;
1472 if (head->r.step < TOMOYO_MAX_POLICY)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001473 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001474 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1475 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1476 head->r.step++;
1477 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001478 return;
Tetsuo Handa32997142011-06-26 23:19:28 +09001479 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
1480 + TOMOYO_MAX_ACL_GROUPS) {
1481 head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
1482 - TOMOYO_MAX_GROUP;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001483 if (!tomoyo_read_domain2(head, &ns->acl_group
Tetsuo Handa32997142011-06-26 23:19:28 +09001484 [head->r.acl_group_index]))
1485 return;
1486 head->r.step++;
1487 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001488 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001489}
1490
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001491/* Wait queue for kernel -> userspace notification. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001492static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001493/* Wait queue for userspace -> kernel notification. */
1494static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001495
1496/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001497struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001498 struct list_head list;
1499 char *query;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001500 size_t query_len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001501 unsigned int serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001502 u8 timer;
1503 u8 answer;
1504 u8 retry;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001505};
1506
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001507/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001508static LIST_HEAD(tomoyo_query_list);
1509
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001510/* Lock for manipulating tomoyo_query_list. */
1511static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1512
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001513/*
1514 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1515 * interface.
1516 */
1517static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1518
1519/**
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001520 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1521 *
1522 * @domain: Pointer to "struct tomoyo_domain_info".
1523 * @header: Lines containing ACL.
1524 *
1525 * Returns nothing.
1526 */
1527static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1528{
1529 char *buffer;
1530 char *cp = strchr(header, '\n');
1531 int len;
1532 if (!cp)
1533 return;
1534 cp = strchr(cp + 1, '\n');
1535 if (!cp)
1536 return;
1537 *cp++ = '\0';
1538 len = strlen(cp) + 1;
1539 buffer = kmalloc(len, GFP_NOFS);
1540 if (!buffer)
1541 return;
1542 snprintf(buffer, len - 1, "%s", cp);
1543 tomoyo_normalize_line(buffer);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001544 tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
1545 false);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001546 kfree(buffer);
1547}
1548
1549/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001550 * tomoyo_supervisor - Ask for the supervisor's decision.
1551 *
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001552 * @r: Pointer to "struct tomoyo_request_info".
1553 * @fmt: The printf()'s format string, followed by parameters.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001554 *
1555 * Returns 0 if the supervisor decided to permit the access request which
1556 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1557 * supervisor decided to retry the access request which violated the policy in
1558 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1559 */
1560int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1561{
1562 va_list args;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001563 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001564 int len;
1565 static unsigned int tomoyo_serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001566 struct tomoyo_query entry = { };
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001567 bool quota_exceeded = false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001568 va_start(args, fmt);
1569 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
1570 va_end(args);
1571 /* Write /sys/kernel/security/tomoyo/audit. */
1572 va_start(args, fmt);
1573 tomoyo_write_log2(r, len, fmt, args);
1574 va_end(args);
1575 /* Nothing more to do if granted. */
1576 if (r->granted)
1577 return 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001578 switch (r->mode) {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001579 case TOMOYO_CONFIG_ENFORCING:
1580 error = -EPERM;
1581 if (atomic_read(&tomoyo_query_observers))
1582 break;
1583 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001584 case TOMOYO_CONFIG_LEARNING:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001585 error = 0;
1586 /* Check max_learning_entry parameter. */
1587 if (tomoyo_domain_quota_is_ok(r))
1588 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001589 /* fall through */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001590 default:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001591 return 0;
1592 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001593 /* Get message. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001594 va_start(args, fmt);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001595 entry.query = tomoyo_init_log(r, len, fmt, args);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001596 va_end(args);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001597 if (!entry.query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001598 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001599 entry.query_len = strlen(entry.query) + 1;
1600 if (!error) {
1601 tomoyo_add_entry(r->domain, entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001602 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001603 }
1604 len = tomoyo_round2(entry.query_len);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001605 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001606 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
1607 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
1608 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001609 quota_exceeded = true;
1610 } else {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001611 entry.serial = tomoyo_serial++;
1612 entry.retry = r->retry;
1613 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
1614 list_add_tail(&entry.list, &tomoyo_query_list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001615 }
1616 spin_unlock(&tomoyo_query_list_lock);
1617 if (quota_exceeded)
1618 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001619 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001620 while (entry.timer < 10) {
1621 wake_up_all(&tomoyo_query_wait);
1622 if (wait_event_interruptible_timeout
1623 (tomoyo_answer_wait, entry.answer ||
1624 !atomic_read(&tomoyo_query_observers), HZ))
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001625 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001626 else
1627 entry.timer++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001628 }
1629 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001630 list_del(&entry.list);
1631 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001632 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001633 switch (entry.answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001634 case 3: /* Asked to retry by administrator. */
1635 error = TOMOYO_RETRY_REQUEST;
1636 r->retry++;
1637 break;
1638 case 1:
1639 /* Granted by administrator. */
1640 error = 0;
1641 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001642 default:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001643 /* Timed out or rejected by administrator. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001644 break;
1645 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001646out:
1647 kfree(entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001648 return error;
1649}
1650
1651/**
1652 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1653 *
1654 * @file: Pointer to "struct file".
1655 * @wait: Pointer to "poll_table".
1656 *
1657 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1658 *
1659 * Waits for access requests which violated policy in enforcing mode.
1660 */
1661static int tomoyo_poll_query(struct file *file, poll_table *wait)
1662{
1663 struct list_head *tmp;
1664 bool found = false;
1665 u8 i;
1666 for (i = 0; i < 2; i++) {
1667 spin_lock(&tomoyo_query_list_lock);
1668 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001669 struct tomoyo_query *ptr =
1670 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001671 if (ptr->answer)
1672 continue;
1673 found = true;
1674 break;
1675 }
1676 spin_unlock(&tomoyo_query_list_lock);
1677 if (found)
1678 return POLLIN | POLLRDNORM;
1679 if (i)
1680 break;
1681 poll_wait(file, &tomoyo_query_wait, wait);
1682 }
1683 return 0;
1684}
1685
1686/**
1687 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1688 *
1689 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001690 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001691static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001692{
1693 struct list_head *tmp;
1694 int pos = 0;
1695 int len = 0;
1696 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001697 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001698 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001699 if (head->read_buf) {
1700 kfree(head->read_buf);
1701 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001702 }
1703 spin_lock(&tomoyo_query_list_lock);
1704 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001705 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001706 if (ptr->answer)
1707 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001708 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001709 continue;
1710 len = ptr->query_len;
1711 break;
1712 }
1713 spin_unlock(&tomoyo_query_list_lock);
1714 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001715 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001716 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001717 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001718 buf = kzalloc(len + 32, GFP_NOFS);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001719 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001720 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001721 pos = 0;
1722 spin_lock(&tomoyo_query_list_lock);
1723 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001724 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001725 if (ptr->answer)
1726 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001727 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001728 continue;
1729 /*
1730 * Some query can be skipped because tomoyo_query_list
1731 * can change, but I don't care.
1732 */
1733 if (len == ptr->query_len)
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001734 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
1735 ptr->retry, ptr->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001736 break;
1737 }
1738 spin_unlock(&tomoyo_query_list_lock);
1739 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001740 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001741 head->r.w[head->r.w_pos++] = buf;
1742 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001743 } else {
1744 kfree(buf);
1745 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001746}
1747
1748/**
1749 * tomoyo_write_answer - Write the supervisor's decision.
1750 *
1751 * @head: Pointer to "struct tomoyo_io_buffer".
1752 *
1753 * Returns 0 on success, -EINVAL otherwise.
1754 */
1755static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1756{
1757 char *data = head->write_buf;
1758 struct list_head *tmp;
1759 unsigned int serial;
1760 unsigned int answer;
1761 spin_lock(&tomoyo_query_list_lock);
1762 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001763 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001764 ptr->timer = 0;
1765 }
1766 spin_unlock(&tomoyo_query_list_lock);
1767 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1768 return -EINVAL;
1769 spin_lock(&tomoyo_query_list_lock);
1770 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001771 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001772 if (ptr->serial != serial)
1773 continue;
1774 if (!ptr->answer)
1775 ptr->answer = answer;
1776 break;
1777 }
1778 spin_unlock(&tomoyo_query_list_lock);
1779 return 0;
1780}
1781
1782/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001783 * tomoyo_read_version: Get version.
1784 *
1785 * @head: Pointer to "struct tomoyo_io_buffer".
1786 *
1787 * Returns version information.
1788 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001789static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001790{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001791 if (!head->r.eof) {
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001792 tomoyo_io_printf(head, "2.4.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001793 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001794 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001795}
1796
1797/**
1798 * tomoyo_read_self_domain - Get the current process's domainname.
1799 *
1800 * @head: Pointer to "struct tomoyo_io_buffer".
1801 *
1802 * Returns the current process's domainname.
1803 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001804static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001805{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001806 if (!head->r.eof) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001807 /*
1808 * tomoyo_domain()->domainname != NULL
1809 * because every process belongs to a domain and
1810 * the domain's name cannot be NULL.
1811 */
1812 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001813 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001814 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001815}
1816
1817/**
1818 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1819 *
1820 * @type: Type of interface.
1821 * @file: Pointer to "struct file".
1822 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09001823 * Returns 0 on success, negative value otherwise.
Kentaro Takeda95908372009-02-05 17:18:13 +09001824 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001825int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09001826{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001827 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001828
1829 if (!head)
1830 return -ENOMEM;
1831 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001832 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09001833 switch (type) {
1834 case TOMOYO_DOMAINPOLICY:
1835 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001836 head->write = tomoyo_write_domain;
1837 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001838 break;
1839 case TOMOYO_EXCEPTIONPOLICY:
1840 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001841 head->write = tomoyo_write_exception;
1842 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09001843 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001844 case TOMOYO_AUDIT:
1845 /* /sys/kernel/security/tomoyo/audit */
1846 head->poll = tomoyo_poll_log;
1847 head->read = tomoyo_read_log;
1848 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001849 case TOMOYO_SELFDOMAIN:
1850 /* /sys/kernel/security/tomoyo/self_domain */
1851 head->read = tomoyo_read_self_domain;
1852 break;
1853 case TOMOYO_DOMAIN_STATUS:
1854 /* /sys/kernel/security/tomoyo/.domain_status */
1855 head->write = tomoyo_write_domain_profile;
1856 head->read = tomoyo_read_domain_profile;
1857 break;
1858 case TOMOYO_PROCESS_STATUS:
1859 /* /sys/kernel/security/tomoyo/.process_status */
1860 head->write = tomoyo_write_pid;
1861 head->read = tomoyo_read_pid;
1862 break;
1863 case TOMOYO_VERSION:
1864 /* /sys/kernel/security/tomoyo/version */
1865 head->read = tomoyo_read_version;
1866 head->readbuf_size = 128;
1867 break;
1868 case TOMOYO_MEMINFO:
1869 /* /sys/kernel/security/tomoyo/meminfo */
1870 head->write = tomoyo_write_memory_quota;
1871 head->read = tomoyo_read_memory_counter;
1872 head->readbuf_size = 512;
1873 break;
1874 case TOMOYO_PROFILE:
1875 /* /sys/kernel/security/tomoyo/profile */
1876 head->write = tomoyo_write_profile;
1877 head->read = tomoyo_read_profile;
1878 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001879 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1880 head->poll = tomoyo_poll_query;
1881 head->write = tomoyo_write_answer;
1882 head->read = tomoyo_read_query;
1883 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001884 case TOMOYO_MANAGER:
1885 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001886 head->write = tomoyo_write_manager;
1887 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09001888 break;
1889 }
1890 if (!(file->f_mode & FMODE_READ)) {
1891 /*
1892 * No need to allocate read_buf since it is not opened
1893 * for reading.
1894 */
1895 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001896 head->poll = NULL;
1897 } else if (!head->poll) {
1898 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001899 if (!head->readbuf_size)
1900 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001901 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001902 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001903 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001904 return -ENOMEM;
1905 }
1906 }
1907 if (!(file->f_mode & FMODE_WRITE)) {
1908 /*
1909 * No need to allocate write_buf since it is not opened
1910 * for writing.
1911 */
1912 head->write = NULL;
1913 } else if (head->write) {
1914 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001915 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001916 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001917 kfree(head->read_buf);
1918 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001919 return -ENOMEM;
1920 }
1921 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001922 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001923 * If the file is /sys/kernel/security/tomoyo/query , increment the
1924 * observer counter.
1925 * The obserber counter is used by tomoyo_supervisor() to see if
1926 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1927 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09001928 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001929 atomic_inc(&tomoyo_query_observers);
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09001930 file->private_data = head;
1931 tomoyo_notify_gc(head, true);
Kentaro Takeda95908372009-02-05 17:18:13 +09001932 return 0;
1933}
1934
1935/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09001936 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1937 *
1938 * @file: Pointer to "struct file".
1939 * @wait: Pointer to "poll_table".
1940 *
1941 * Waits for read readiness.
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001942 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
1943 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09001944 */
1945int tomoyo_poll_control(struct file *file, poll_table *wait)
1946{
1947 struct tomoyo_io_buffer *head = file->private_data;
1948 if (!head->poll)
1949 return -ENOSYS;
1950 return head->poll(file, wait);
1951}
1952
1953/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001954 * tomoyo_set_namespace_cursor - Set namespace to read.
1955 *
1956 * @head: Pointer to "struct tomoyo_io_buffer".
1957 *
1958 * Returns nothing.
1959 */
1960static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
1961{
1962 struct list_head *ns;
1963 if (head->type != TOMOYO_EXCEPTIONPOLICY &&
1964 head->type != TOMOYO_PROFILE)
1965 return;
1966 /*
1967 * If this is the first read, or reading previous namespace finished
1968 * and has more namespaces to read, update the namespace cursor.
1969 */
1970 ns = head->r.ns;
1971 if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
1972 /* Clearing is OK because tomoyo_flush() returned true. */
1973 memset(&head->r, 0, sizeof(head->r));
1974 head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
1975 }
1976}
1977
1978/**
1979 * tomoyo_has_more_namespace - Check for unread namespaces.
1980 *
1981 * @head: Pointer to "struct tomoyo_io_buffer".
1982 *
1983 * Returns true if we have more entries to print, false otherwise.
1984 */
1985static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
1986{
1987 return (head->type == TOMOYO_EXCEPTIONPOLICY ||
1988 head->type == TOMOYO_PROFILE) && head->r.eof &&
1989 head->r.ns->next != &tomoyo_namespace_list;
1990}
1991
1992/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001993 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1994 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001995 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001996 * @buffer: Poiner to buffer to write to.
1997 * @buffer_len: Size of @buffer.
1998 *
1999 * Returns bytes read on success, negative value otherwise.
2000 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002001int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002002 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002003{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002004 int len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002005 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002006
2007 if (!head->read)
2008 return -ENOSYS;
2009 if (mutex_lock_interruptible(&head->io_sem))
2010 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002011 head->read_user_buf = buffer;
2012 head->read_user_buf_avail = buffer_len;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002013 idx = tomoyo_read_lock();
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002014 if (tomoyo_flush(head))
2015 /* Call the policy handler. */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002016 do {
2017 tomoyo_set_namespace_cursor(head);
2018 head->read(head);
2019 } while (tomoyo_flush(head) &&
2020 tomoyo_has_more_namespace(head));
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002021 tomoyo_read_unlock(idx);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09002022 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09002023 mutex_unlock(&head->io_sem);
2024 return len;
2025}
2026
2027/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002028 * tomoyo_parse_policy - Parse a policy line.
2029 *
2030 * @head: Poiter to "struct tomoyo_io_buffer".
2031 * @line: Line to parse.
2032 *
2033 * Returns 0 on success, negative value otherwise.
2034 *
2035 * Caller holds tomoyo_read_lock().
2036 */
2037static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
2038{
2039 /* Delete request? */
2040 head->w.is_delete = !strncmp(line, "delete ", 7);
2041 if (head->w.is_delete)
2042 memmove(line, line + 7, strlen(line + 7) + 1);
2043 /* Selecting namespace to update. */
2044 if (head->type == TOMOYO_EXCEPTIONPOLICY ||
2045 head->type == TOMOYO_PROFILE) {
2046 if (*line == '<') {
2047 char *cp = strchr(line, ' ');
2048 if (cp) {
2049 *cp++ = '\0';
2050 head->w.ns = tomoyo_assign_namespace(line);
2051 memmove(line, cp, strlen(cp) + 1);
2052 } else
2053 head->w.ns = NULL;
2054 } else
2055 head->w.ns = &tomoyo_kernel_namespace;
2056 /* Don't allow updating if namespace is invalid. */
2057 if (!head->w.ns)
2058 return -ENOENT;
2059 }
2060 /* Do the update. */
2061 return head->write(head);
2062}
2063
2064/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002065 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2066 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002067 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002068 * @buffer: Pointer to buffer to read from.
2069 * @buffer_len: Size of @buffer.
2070 *
2071 * Returns @buffer_len on success, negative value otherwise.
2072 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002073int tomoyo_write_control(struct tomoyo_io_buffer *head,
2074 const char __user *buffer, const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09002075{
Kentaro Takeda95908372009-02-05 17:18:13 +09002076 int error = buffer_len;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002077 size_t avail_len = buffer_len;
Kentaro Takeda95908372009-02-05 17:18:13 +09002078 char *cp0 = head->write_buf;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002079 int idx;
Kentaro Takeda95908372009-02-05 17:18:13 +09002080 if (!head->write)
2081 return -ENOSYS;
2082 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2083 return -EFAULT;
Kentaro Takeda95908372009-02-05 17:18:13 +09002084 if (mutex_lock_interruptible(&head->io_sem))
2085 return -EINTR;
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002086 idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09002087 /* Read a line and dispatch it to the policy handler. */
2088 while (avail_len > 0) {
2089 char c;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002090 if (head->w.avail >= head->writebuf_size - 1) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002091 const int len = head->writebuf_size * 2;
2092 char *cp = kzalloc(len, GFP_NOFS);
2093 if (!cp) {
2094 error = -ENOMEM;
2095 break;
2096 }
2097 memmove(cp, cp0, head->w.avail);
2098 kfree(cp0);
2099 head->write_buf = cp;
2100 cp0 = cp;
2101 head->writebuf_size = len;
2102 }
2103 if (get_user(c, buffer)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09002104 error = -EFAULT;
2105 break;
2106 }
2107 buffer++;
2108 avail_len--;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002109 cp0[head->w.avail++] = c;
Kentaro Takeda95908372009-02-05 17:18:13 +09002110 if (c != '\n')
2111 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002112 cp0[head->w.avail - 1] = '\0';
2113 head->w.avail = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09002114 tomoyo_normalize_line(cp0);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002115 if (!strcmp(cp0, "reset")) {
2116 head->w.ns = &tomoyo_kernel_namespace;
2117 head->w.domain = NULL;
2118 memset(&head->r, 0, sizeof(head->r));
2119 continue;
2120 }
2121 /* Don't allow updating policies by non manager programs. */
2122 switch (head->type) {
2123 case TOMOYO_PROCESS_STATUS:
2124 /* This does not write anything. */
2125 break;
2126 case TOMOYO_DOMAINPOLICY:
2127 if (tomoyo_select_domain(head, cp0))
2128 continue;
2129 /* fall through */
2130 case TOMOYO_EXCEPTIONPOLICY:
2131 if (!strcmp(cp0, "select transition_only")) {
2132 head->r.print_transition_related_only = true;
2133 continue;
2134 }
2135 /* fall through */
2136 default:
2137 if (!tomoyo_manager()) {
2138 error = -EPERM;
2139 goto out;
2140 }
2141 }
2142 switch (tomoyo_parse_policy(head, cp0)) {
2143 case -EPERM:
2144 error = -EPERM;
2145 goto out;
2146 }
Kentaro Takeda95908372009-02-05 17:18:13 +09002147 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002148out:
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002149 tomoyo_read_unlock(idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002150 mutex_unlock(&head->io_sem);
2151 return error;
2152}
2153
2154/**
2155 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2156 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002157 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09002158 *
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002159 * Returns 0.
Kentaro Takeda95908372009-02-05 17:18:13 +09002160 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09002161int tomoyo_close_control(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09002162{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09002163 /*
2164 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2165 * observer counter.
2166 */
Tetsuo Handa2e503bb2011-06-26 23:20:55 +09002167 if (head->type == TOMOYO_QUERY &&
2168 atomic_dec_and_test(&tomoyo_query_observers))
2169 wake_up_all(&tomoyo_answer_wait);
2170 tomoyo_notify_gc(head, false);
Kentaro Takeda95908372009-02-05 17:18:13 +09002171 return 0;
2172}
2173
2174/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002175 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09002176 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002177void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09002178{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002179 struct tomoyo_domain_info *domain;
2180 const int idx = tomoyo_read_lock();
2181 tomoyo_policy_loaded = true;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002182 printk(KERN_INFO "TOMOYO: 2.4.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002183 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2184 const u8 profile = domain->profile;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002185 const struct tomoyo_policy_namespace *ns = domain->ns;
2186 if (ns->profile_version != 20100903)
2187 printk(KERN_ERR
2188 "Profile version %u is not supported.\n",
2189 ns->profile_version);
2190 else if (!ns->profile_ptr[profile])
2191 printk(KERN_ERR
2192 "Profile %u (used by '%s') is not defined.\n",
2193 profile, domain->domainname->name);
2194 else
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002195 continue;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002196 printk(KERN_ERR
2197 "Userland tools for TOMOYO 2.4 must be installed and "
2198 "policy must be initialized.\n");
2199 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ "
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002200 "for more information.\n");
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09002201 panic("STOP!");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002202 }
2203 tomoyo_read_unlock(idx);
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002204 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002205}