blob: 6580ef35074b9f59e1127be66360522b0d5b95ac [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 Handa57c25902010-06-03 20:38:44 +090014/* Profile version. Currently only 20090903 is defined. */
15static unsigned int tomoyo_profile_version;
16
17/* Profile table. Memory is allocated as needed. */
18static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
19
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090020/* String table for operation mode. */
21const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
22 [TOMOYO_CONFIG_DISABLED] = "disabled",
23 [TOMOYO_CONFIG_LEARNING] = "learning",
24 [TOMOYO_CONFIG_PERMISSIVE] = "permissive",
25 [TOMOYO_CONFIG_ENFORCING] = "enforcing"
Kentaro Takeda95908372009-02-05 17:18:13 +090026};
Kentaro Takeda95908372009-02-05 17:18:13 +090027
Tetsuo Handa57c25902010-06-03 20:38:44 +090028/* String table for /sys/kernel/security/tomoyo/profile */
29static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
30 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
31 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
32 [TOMOYO_MAC_FILE_OPEN] = "file::open",
33 [TOMOYO_MAC_FILE_CREATE] = "file::create",
34 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
Tetsuo Handa7c759642011-06-26 23:15:31 +090035 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
Tetsuo Handa57c25902010-06-03 20:38:44 +090036 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
37 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
38 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
39 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
40 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
41 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
Tetsuo Handa57c25902010-06-03 20:38:44 +090042 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
43 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
44 [TOMOYO_MAC_FILE_LINK] = "file::link",
45 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
46 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
47 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
48 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
49 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
50 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
51 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090052 [TOMOYO_MAC_FILE_UMOUNT] = "file::unmount",
Tetsuo Handa57c25902010-06-03 20:38:44 +090053 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
54 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Kentaro Takeda95908372009-02-05 17:18:13 +090055};
56
Tetsuo Handad5ca1722011-06-26 23:18:21 +090057/* String table for PREFERENCE keyword. */
58static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090059 [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
Tetsuo Handad5ca1722011-06-26 23:18:21 +090060 [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
61};
62
Kentaro Takeda95908372009-02-05 17:18:13 +090063/* Permit policy management by non-root user? */
64static bool tomoyo_manage_by_non_root;
65
66/* Utility functions. */
67
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090068/**
Tetsuo Handa57c25902010-06-03 20:38:44 +090069 * tomoyo_yesno - Return "yes" or "no".
70 *
71 * @value: Bool value.
72 */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090073const char *tomoyo_yesno(const unsigned int value)
Tetsuo Handa57c25902010-06-03 20:38:44 +090074{
75 return value ? "yes" : "no";
76}
77
Tetsuo Handad5ca1722011-06-26 23:18:21 +090078/**
79 * tomoyo_addprintf - strncat()-like-snprintf().
80 *
81 * @buffer: Buffer to write to. Must be '\0'-terminated.
82 * @len: Size of @buffer.
83 * @fmt: The printf()'s format string, followed by parameters.
84 *
85 * Returns nothing.
86 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +090087static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
88{
89 va_list args;
90 const int pos = strlen(buffer);
91 va_start(args, fmt);
92 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
93 va_end(args);
94}
95
96/**
97 * tomoyo_flush - Flush queued string to userspace's buffer.
98 *
99 * @head: Pointer to "struct tomoyo_io_buffer".
100 *
101 * Returns true if all data was flushed, false otherwise.
102 */
103static bool tomoyo_flush(struct tomoyo_io_buffer *head)
104{
105 while (head->r.w_pos) {
106 const char *w = head->r.w[0];
107 int len = strlen(w);
108 if (len) {
109 if (len > head->read_user_buf_avail)
110 len = head->read_user_buf_avail;
111 if (!len)
112 return false;
113 if (copy_to_user(head->read_user_buf, w, len))
114 return false;
115 head->read_user_buf_avail -= len;
116 head->read_user_buf += len;
117 w += len;
118 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900119 head->r.w[0] = w;
120 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900121 return false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900122 /* Add '\0' for audit logs and query. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900123 if (head->poll) {
124 if (!head->read_user_buf_avail ||
125 copy_to_user(head->read_user_buf, "", 1))
126 return false;
127 head->read_user_buf_avail--;
128 head->read_user_buf++;
129 }
130 head->r.w_pos--;
131 for (len = 0; len < head->r.w_pos; len++)
132 head->r.w[len] = head->r.w[len + 1];
133 }
134 head->r.avail = 0;
135 return true;
136}
137
138/**
139 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
140 *
141 * @head: Pointer to "struct tomoyo_io_buffer".
142 * @string: String to print.
143 *
144 * Note that @string has to be kept valid until @head is kfree()d.
145 * This means that char[] allocated on stack memory cannot be passed to
146 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
147 */
148static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
149{
150 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
151 head->r.w[head->r.w_pos++] = string;
152 tomoyo_flush(head);
153 } else
154 WARN_ON(1);
155}
156
157/**
158 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
159 *
160 * @head: Pointer to "struct tomoyo_io_buffer".
161 * @fmt: The printf()'s format string, followed by parameters.
162 */
163void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
164{
165 va_list args;
166 int len;
167 int pos = head->r.avail;
168 int size = head->readbuf_size - pos;
169 if (size <= 0)
170 return;
171 va_start(args, fmt);
172 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
173 va_end(args);
174 if (pos + len >= head->readbuf_size) {
175 WARN_ON(1);
176 return;
177 }
178 head->r.avail += len;
179 tomoyo_set_string(head, head->read_buf + pos);
180}
181
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900182/**
183 * tomoyo_set_space - Put a space to "struct tomoyo_io_buffer" structure.
184 *
185 * @head: Pointer to "struct tomoyo_io_buffer".
186 *
187 * Returns nothing.
188 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900189static void tomoyo_set_space(struct tomoyo_io_buffer *head)
190{
191 tomoyo_set_string(head, " ");
192}
193
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900194/**
195 * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
196 *
197 * @head: Pointer to "struct tomoyo_io_buffer".
198 *
199 * Returns nothing.
200 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900201static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
202{
203 tomoyo_set_string(head, "\n");
204 return !head->r.w_pos;
205}
206
Tetsuo Handa57c25902010-06-03 20:38:44 +0900207/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900208 * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
209 *
210 * @head: Pointer to "struct tomoyo_io_buffer".
211 *
212 * Returns nothing.
213 */
214static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
215{
216 tomoyo_set_string(head, "/");
217}
218
219/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900220 * tomoyo_print_name_union - Print a tomoyo_name_union.
221 *
222 * @head: Pointer to "struct tomoyo_io_buffer".
223 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900224 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900225static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
226 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900227{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900228 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900229 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900230 tomoyo_set_string(head, "@");
231 tomoyo_set_string(head, ptr->group->group_name->name);
232 } else {
233 tomoyo_set_string(head, ptr->filename->name);
234 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900235}
236
237/**
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900238 * tomoyo_print_number_union - Print a tomoyo_number_union.
239 *
240 * @head: Pointer to "struct tomoyo_io_buffer".
241 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900242 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900243static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
244 const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900245{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900246 tomoyo_set_space(head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900247 if (ptr->group) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900248 tomoyo_set_string(head, "@");
249 tomoyo_set_string(head, ptr->group->group_name->name);
250 } else {
251 int i;
252 unsigned long min = ptr->values[0];
253 const unsigned long max = ptr->values[1];
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900254 u8 min_type = ptr->value_type[0];
255 const u8 max_type = ptr->value_type[1];
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900256 char buffer[128];
257 buffer[0] = '\0';
258 for (i = 0; i < 2; i++) {
259 switch (min_type) {
260 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
261 tomoyo_addprintf(buffer, sizeof(buffer),
262 "0x%lX", min);
263 break;
264 case TOMOYO_VALUE_TYPE_OCTAL:
265 tomoyo_addprintf(buffer, sizeof(buffer),
266 "0%lo", min);
267 break;
268 default:
269 tomoyo_addprintf(buffer, sizeof(buffer),
270 "%lu", min);
271 break;
272 }
273 if (min == max && min_type == max_type)
274 break;
275 tomoyo_addprintf(buffer, sizeof(buffer), "-");
276 min_type = max_type;
277 min = max;
278 }
279 tomoyo_io_printf(head, "%s", buffer);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900280 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900281}
282
283/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900284 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900285 *
286 * @profile: Profile number to create.
287 *
288 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
289 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900290static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900291{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900292 struct tomoyo_profile *ptr;
293 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900294 if (profile >= TOMOYO_MAX_PROFILES)
295 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900296 ptr = tomoyo_profile_ptr[profile];
297 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900298 return ptr;
299 entry = kzalloc(sizeof(*entry), GFP_NOFS);
300 if (mutex_lock_interruptible(&tomoyo_policy_lock))
301 goto out;
302 ptr = tomoyo_profile_ptr[profile];
303 if (!ptr && tomoyo_memory_ok(entry)) {
304 ptr = entry;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900305 ptr->default_config = TOMOYO_CONFIG_DISABLED |
306 TOMOYO_CONFIG_WANT_GRANT_LOG |
307 TOMOYO_CONFIG_WANT_REJECT_LOG;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900308 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
309 sizeof(ptr->config));
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900310 ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900311 ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900312 mb(); /* Avoid out-of-order execution. */
313 tomoyo_profile_ptr[profile] = ptr;
314 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900315 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900316 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900317 out:
318 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900319 return ptr;
320}
321
322/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900323 * tomoyo_profile - Find a profile.
324 *
325 * @profile: Profile number to find.
326 *
327 * Returns pointer to "struct tomoyo_profile".
328 */
329struct tomoyo_profile *tomoyo_profile(const u8 profile)
330{
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900331 static struct tomoyo_profile tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900332 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900333 if (!ptr)
334 ptr = &tomoyo_null_profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900335 return ptr;
336}
337
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900338/**
339 * tomoyo_find_yesno - Find values for specified keyword.
340 *
341 * @string: String to check.
342 * @find: Name of keyword.
343 *
344 * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
345 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900346static s8 tomoyo_find_yesno(const char *string, const char *find)
347{
348 const char *cp = strstr(string, find);
349 if (cp) {
350 cp += strlen(find);
351 if (!strncmp(cp, "=yes", 4))
352 return 1;
353 else if (!strncmp(cp, "=no", 3))
354 return 0;
355 }
356 return -1;
357}
358
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900359/**
360 * tomoyo_set_uint - Set value for specified preference.
361 *
362 * @i: Pointer to "unsigned int".
363 * @string: String to check.
364 * @find: Name of keyword.
365 *
366 * Returns nothing.
367 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900368static void tomoyo_set_uint(unsigned int *i, const char *string,
369 const char *find)
370{
371 const char *cp = strstr(string, find);
372 if (cp)
373 sscanf(cp + strlen(find), "=%u", i);
374}
375
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900376/**
377 * tomoyo_set_mode - Set mode for specified profile.
378 *
379 * @name: Name of functionality.
380 * @value: Mode for @name.
381 * @profile: Pointer to "struct tomoyo_profile".
382 *
383 * Returns 0 on success, negative value otherwise.
384 */
Tetsuo Handa8e568682010-06-25 09:30:09 +0900385static int tomoyo_set_mode(char *name, const char *value,
Tetsuo Handa8e568682010-06-25 09:30:09 +0900386 struct tomoyo_profile *profile)
387{
388 u8 i;
389 u8 config;
390 if (!strcmp(name, "CONFIG")) {
391 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
392 config = profile->default_config;
393 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
394 config = 0;
395 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
396 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
397 if (strcmp(name, tomoyo_mac_keywords[i]))
398 continue;
399 config = profile->config[i];
400 break;
401 }
402 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
403 return -EINVAL;
404 } else {
405 return -EINVAL;
406 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900407 if (strstr(value, "use_default")) {
Tetsuo Handa8e568682010-06-25 09:30:09 +0900408 config = TOMOYO_CONFIG_USE_DEFAULT;
409 } else {
410 u8 mode;
411 for (mode = 0; mode < 4; mode++)
412 if (strstr(value, tomoyo_mode[mode]))
413 /*
414 * Update lower 3 bits in order to distinguish
415 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
416 */
417 config = (config & ~7) | mode;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900418 if (config != TOMOYO_CONFIG_USE_DEFAULT) {
419 switch (tomoyo_find_yesno(value, "grant_log")) {
420 case 1:
421 config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
422 break;
423 case 0:
424 config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
425 break;
426 }
427 switch (tomoyo_find_yesno(value, "reject_log")) {
428 case 1:
429 config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
430 break;
431 case 0:
432 config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
433 break;
434 }
435 }
Tetsuo Handa8e568682010-06-25 09:30:09 +0900436 }
437 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
438 profile->config[i] = config;
439 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
440 profile->default_config = config;
441 return 0;
442}
443
Tetsuo Handa57c25902010-06-03 20:38:44 +0900444/**
445 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900446 *
447 * @head: Pointer to "struct tomoyo_io_buffer".
448 *
449 * Returns 0 on success, negative value otherwise.
450 */
451static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
452{
453 char *data = head->write_buf;
454 unsigned int i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900455 char *cp;
456 struct tomoyo_profile *profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900457 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
458 return 0;
459 i = simple_strtoul(data, &cp, 10);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900460 if (*cp != '-')
461 return -EINVAL;
462 data = cp + 1;
463 profile = tomoyo_assign_profile(i);
464 if (!profile)
465 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900466 cp = strchr(data, '=');
467 if (!cp)
468 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900469 *cp++ = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +0900470 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900471 static DEFINE_SPINLOCK(lock);
472 const struct tomoyo_path_info *new_comment
473 = tomoyo_get_name(cp);
474 const struct tomoyo_path_info *old_comment;
475 if (!new_comment)
476 return -ENOMEM;
477 spin_lock(&lock);
478 old_comment = profile->comment;
479 profile->comment = new_comment;
480 spin_unlock(&lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900481 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900482 return 0;
483 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900484 if (!strcmp(data, "PREFERENCE")) {
485 for (i = 0; i < TOMOYO_MAX_PREF; i++)
486 tomoyo_set_uint(&profile->pref[i], cp,
487 tomoyo_pref_keywords[i]);
488 return 0;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900489 }
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900490 return tomoyo_set_mode(data, cp, profile);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900491}
492
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900493/**
494 * tomoyo_print_config - Print mode for specified functionality.
495 *
496 * @head: Pointer to "struct tomoyo_io_buffer".
497 * @config: Mode for that functionality.
498 *
499 * Returns nothing.
500 *
501 * Caller prints functionality's name.
502 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900503static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
504{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900505 tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
506 tomoyo_mode[config & 3],
507 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
508 tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900509}
510
Kentaro Takeda95908372009-02-05 17:18:13 +0900511/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900512 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900513 *
514 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900515 *
516 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900517 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900518static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900519{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900520 u8 index;
521 const struct tomoyo_profile *profile;
522 next:
523 index = head->r.index;
524 profile = tomoyo_profile_ptr[index];
525 switch (head->r.step) {
526 case 0:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900527 tomoyo_io_printf(head, "PROFILE_VERSION=%u\n", 20090903);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900528 head->r.step++;
529 break;
530 case 1:
531 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
532 head->r.index++)
533 if (tomoyo_profile_ptr[head->r.index])
534 break;
535 if (head->r.index == TOMOYO_MAX_PROFILES)
536 return;
537 head->r.step++;
538 break;
539 case 2:
540 {
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900541 u8 i;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900542 const struct tomoyo_path_info *comment =
543 profile->comment;
544 tomoyo_io_printf(head, "%u-COMMENT=", index);
545 tomoyo_set_string(head, comment ? comment->name : "");
546 tomoyo_set_lf(head);
Tetsuo Handad5ca1722011-06-26 23:18:21 +0900547 tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
548 for (i = 0; i < TOMOYO_MAX_PREF; i++)
549 tomoyo_io_printf(head, "%s=%u ",
550 tomoyo_pref_keywords[i],
551 profile->pref[i]);
552 tomoyo_set_string(head, "}\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900553 head->r.step++;
554 }
555 break;
556 case 3:
557 {
558 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
559 tomoyo_print_config(head, profile->default_config);
560 head->r.bit = 0;
561 head->r.step++;
562 }
563 break;
564 case 4:
565 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
566 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
567 const u8 i = head->r.bit;
568 const u8 config = profile->config[i];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900569 if (config == TOMOYO_CONFIG_USE_DEFAULT)
570 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900571 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
572 tomoyo_mac_keywords[i]);
573 tomoyo_print_config(head, config);
574 head->r.bit++;
575 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900576 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900577 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
578 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900579 head->r.index++;
580 head->r.step = 1;
581 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900582 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900583 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900584 if (tomoyo_flush(head))
585 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900586}
587
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900588static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
589 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900590{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900591 return container_of(a, struct tomoyo_manager, head)->manager ==
592 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900593}
594
Kentaro Takeda95908372009-02-05 17:18:13 +0900595/**
596 * tomoyo_update_manager_entry - Add a manager entry.
597 *
598 * @manager: The path to manager or the domainnamme.
599 * @is_delete: True if it is a delete request.
600 *
601 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900602 *
603 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900604 */
605static int tomoyo_update_manager_entry(const char *manager,
606 const bool is_delete)
607{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900608 struct tomoyo_manager e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900609 struct tomoyo_acl_param param = {
610 .is_delete = is_delete,
611 .list = &tomoyo_policy_list[TOMOYO_ID_MANAGER],
612 };
613 int error = is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900614 if (tomoyo_domain_def(manager)) {
615 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900616 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900617 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900618 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900619 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900620 return -EINVAL;
621 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900622 e.manager = tomoyo_get_name(manager);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900623 if (e.manager) {
624 error = tomoyo_update_policy(&e.head, sizeof(e), &param,
625 tomoyo_same_manager);
626 tomoyo_put_name(e.manager);
627 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900628 return error;
629}
630
631/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900632 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900633 *
634 * @head: Pointer to "struct tomoyo_io_buffer".
635 *
636 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900637 *
638 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900639 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900640static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900641{
642 char *data = head->write_buf;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900643 bool is_delete = tomoyo_str_starts(&data, "delete ");
Kentaro Takeda95908372009-02-05 17:18:13 +0900644
645 if (!strcmp(data, "manage_by_non_root")) {
646 tomoyo_manage_by_non_root = !is_delete;
647 return 0;
648 }
649 return tomoyo_update_manager_entry(data, is_delete);
650}
651
652/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900653 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900654 *
655 * @head: Pointer to "struct tomoyo_io_buffer".
656 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900657 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900658 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900659static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900660{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900661 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900662 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900663 list_for_each_cookie(head->r.acl,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900664 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900665 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900666 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900667 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900668 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900669 if (!tomoyo_flush(head))
670 return;
671 tomoyo_set_string(head, ptr->manager->name);
672 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900673 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900674 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900675}
676
677/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900678 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900679 *
680 * Returns true if the current process is permitted to modify policy
681 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900682 *
683 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900684 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900685static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900686{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900687 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900688 const char *exe;
689 const struct task_struct *task = current;
690 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
691 bool found = false;
692
693 if (!tomoyo_policy_loaded)
694 return true;
695 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
696 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900697 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
698 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900699 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900700 && !tomoyo_pathcmp(domainname, ptr->manager)) {
701 found = true;
702 break;
703 }
704 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900705 if (found)
706 return true;
707 exe = tomoyo_get_exe();
708 if (!exe)
709 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900710 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
711 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900712 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900713 && !strcmp(exe, ptr->manager->name)) {
714 found = true;
715 break;
716 }
717 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900718 if (!found) { /* Reduce error messages. */
719 static pid_t last_pid;
720 const pid_t pid = current->pid;
721 if (last_pid != pid) {
722 printk(KERN_WARNING "%s ( %s ) is not permitted to "
723 "update policies.\n", domainname->name, exe);
724 last_pid = pid;
725 }
726 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900727 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900728 return found;
729}
730
731/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900732 * tomoyo_select_one - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900733 *
734 * @head: Pointer to "struct tomoyo_io_buffer".
735 * @data: String to parse.
736 *
737 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900738 *
739 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900740 */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900741static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900742{
743 unsigned int pid;
744 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900745 bool global_pid = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900746
Tetsuo Handa063821c2010-06-24 12:00:25 +0900747 if (!strcmp(data, "allow_execute")) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900748 head->r.print_execute_only = true;
Tetsuo Handa063821c2010-06-24 12:00:25 +0900749 return true;
750 }
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900751 if (sscanf(data, "pid=%u", &pid) == 1 ||
752 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900753 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900754 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900755 read_lock(&tasklist_lock);
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900756 if (global_pid)
757 p = find_task_by_pid_ns(pid, &init_pid_ns);
758 else
759 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900760 if (p)
761 domain = tomoyo_real_domain(p);
762 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900763 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900764 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900765 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900766 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900767 } else
768 return false;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900769 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900770 /* Accessing read_buf is safe because head->io_sem is held. */
771 if (!head->read_buf)
772 return true; /* Do nothing if open(O_WRONLY). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900773 memset(&head->r, 0, sizeof(head->r));
774 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +0200775 if (domain)
776 head->r.domain = &domain->list;
777 else
778 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +0900779 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900780 if (domain && domain->is_deleted)
781 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900782 return true;
783}
784
785/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900786 * tomoyo_delete_domain - Delete a domain.
787 *
788 * @domainname: The name of domain.
789 *
790 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900791 *
792 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900793 */
794static int tomoyo_delete_domain(char *domainname)
795{
796 struct tomoyo_domain_info *domain;
797 struct tomoyo_path_info name;
798
799 name.name = domainname;
800 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +0900801 if (mutex_lock_interruptible(&tomoyo_policy_lock))
802 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900803 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900804 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900805 /* Never delete tomoyo_kernel_domain */
806 if (domain == &tomoyo_kernel_domain)
807 continue;
808 if (domain->is_deleted ||
809 tomoyo_pathcmp(domain->domainname, &name))
810 continue;
811 domain->is_deleted = true;
812 break;
813 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900814 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900815 return 0;
816}
817
818/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900819 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900820 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900821 * @list: Pointer to "struct list_head".
822 * @data: Policy to be interpreted.
823 * @is_delete: True if it is a delete request.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900824 *
825 * Returns 0 on success, negative value otherwise.
826 *
827 * Caller holds tomoyo_read_lock().
828 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900829static int tomoyo_write_domain2(struct list_head *list, char *data,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900830 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900831{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900832 struct tomoyo_acl_param param = {
833 .list = list,
834 .data = data,
835 .is_delete = is_delete,
836 };
837 static const struct {
838 const char *keyword;
839 int (*write) (struct tomoyo_acl_param *);
840 } tomoyo_callback[1] = {
841 { "file ", tomoyo_write_file },
842 };
843 u8 i;
844 for (i = 0; i < 1; i++) {
845 if (!tomoyo_str_starts(&param.data,
846 tomoyo_callback[i].keyword))
847 continue;
848 return tomoyo_callback[i].write(&param);
849 }
850 return -EINVAL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900851}
852
853/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900854 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900855 *
856 * @head: Pointer to "struct tomoyo_io_buffer".
857 *
858 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900859 *
860 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900861 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900862static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900863{
864 char *data = head->write_buf;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900865 struct tomoyo_domain_info *domain = head->w.domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900866 bool is_delete = false;
867 bool is_select = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900868 unsigned int profile;
869
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900870 if (tomoyo_str_starts(&data, "delete "))
Kentaro Takeda95908372009-02-05 17:18:13 +0900871 is_delete = true;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900872 else if (tomoyo_str_starts(&data, "select "))
Kentaro Takeda95908372009-02-05 17:18:13 +0900873 is_select = true;
Tetsuo Handa75093152010-06-16 16:23:55 +0900874 if (is_select && tomoyo_select_one(head, data))
Kentaro Takeda95908372009-02-05 17:18:13 +0900875 return 0;
876 /* Don't allow updating policies by non manager programs. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900877 if (!tomoyo_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +0900878 return -EPERM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900879 if (tomoyo_domain_def(data)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900880 domain = NULL;
881 if (is_delete)
882 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900883 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +0900884 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900885 else
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900886 domain = tomoyo_assign_domain(data, 0);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900887 head->w.domain = domain;
Kentaro Takeda95908372009-02-05 17:18:13 +0900888 return 0;
889 }
890 if (!domain)
891 return -EINVAL;
892
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900893 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +0900894 && profile < TOMOYO_MAX_PROFILES) {
895 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
896 domain->profile = (u8) profile;
897 return 0;
898 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900899 if (!strcmp(data, "quota_exceeded")) {
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900900 domain->quota_warned = !is_delete;
901 return 0;
902 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900903 if (!strcmp(data, "transition_failed")) {
Tetsuo Handa9b2443732010-06-03 20:35:53 +0900904 domain->transition_failed = !is_delete;
905 return 0;
906 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900907 return tomoyo_write_domain2(&domain->acl_info_list, data, is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900908}
909
910/**
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900911 * tomoyo_set_group - Print category name.
Kentaro Takeda95908372009-02-05 17:18:13 +0900912 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900913 * @head: Pointer to "struct tomoyo_io_buffer".
914 * @category: Category name.
Kentaro Takeda95908372009-02-05 17:18:13 +0900915 *
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900916 * Returns nothing.
Kentaro Takeda95908372009-02-05 17:18:13 +0900917 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900918static void tomoyo_set_group(struct tomoyo_io_buffer *head,
919 const char *category)
Kentaro Takeda95908372009-02-05 17:18:13 +0900920{
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900921 tomoyo_set_string(head, category);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900922}
923
924/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900925 * tomoyo_print_entry - Print an ACL entry.
926 *
927 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900928 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900929 *
930 * Returns true on success, false otherwise.
931 */
932static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900933 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +0900934{
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900935 const u8 acl_type = acl->type;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900936 bool first = true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900937 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +0900938
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900939 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900940 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900941 if (!tomoyo_flush(head))
942 return false;
943 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900944 struct tomoyo_path_acl *ptr =
945 container_of(acl, typeof(*ptr), head);
946 const u16 perm = ptr->perm;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900947 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900948 if (!(perm & (1 << bit)))
949 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900950 if (head->r.print_execute_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900951 bit != TOMOYO_TYPE_EXECUTE)
952 continue;
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900953 if (first) {
954 tomoyo_set_group(head, "file ");
955 first = false;
956 } else {
957 tomoyo_set_slash(head);
958 }
959 tomoyo_set_string(head, tomoyo_path_keyword[bit]);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900960 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900961 if (first)
962 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900963 tomoyo_print_name_union(head, &ptr->name);
964 } else if (head->r.print_execute_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +0900965 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900966 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
967 struct tomoyo_path2_acl *ptr =
968 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900969 const u8 perm = ptr->perm;
970 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
971 if (!(perm & (1 << bit)))
972 continue;
973 if (first) {
974 tomoyo_set_group(head, "file ");
975 first = false;
976 } else {
977 tomoyo_set_slash(head);
978 }
979 tomoyo_set_string(head, tomoyo_mac_keywords
980 [tomoyo_pp2mac[bit]]);
981 }
982 if (first)
983 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900984 tomoyo_print_name_union(head, &ptr->name1);
985 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900986 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
987 struct tomoyo_path_number_acl *ptr =
988 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900989 const u8 perm = ptr->perm;
990 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
991 if (!(perm & (1 << bit)))
992 continue;
993 if (first) {
994 tomoyo_set_group(head, "file ");
995 first = false;
996 } else {
997 tomoyo_set_slash(head);
998 }
999 tomoyo_set_string(head, tomoyo_mac_keywords
1000 [tomoyo_pn2mac[bit]]);
1001 }
1002 if (first)
1003 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001004 tomoyo_print_name_union(head, &ptr->name);
1005 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001006 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
1007 struct tomoyo_mkdev_acl *ptr =
1008 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001009 const u8 perm = ptr->perm;
1010 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
1011 if (!(perm & (1 << bit)))
1012 continue;
1013 if (first) {
1014 tomoyo_set_group(head, "file ");
1015 first = false;
1016 } else {
1017 tomoyo_set_slash(head);
1018 }
1019 tomoyo_set_string(head, tomoyo_mac_keywords
1020 [tomoyo_pnnn2mac[bit]]);
1021 }
1022 if (first)
1023 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001024 tomoyo_print_name_union(head, &ptr->name);
1025 tomoyo_print_number_union(head, &ptr->mode);
1026 tomoyo_print_number_union(head, &ptr->major);
1027 tomoyo_print_number_union(head, &ptr->minor);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001028 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1029 struct tomoyo_mount_acl *ptr =
1030 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001031 tomoyo_set_group(head, "file mount");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001032 tomoyo_print_name_union(head, &ptr->dev_name);
1033 tomoyo_print_name_union(head, &ptr->dir_name);
1034 tomoyo_print_name_union(head, &ptr->fs_type);
1035 tomoyo_print_number_union(head, &ptr->flags);
Kentaro Takeda95908372009-02-05 17:18:13 +09001036 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001037 tomoyo_set_lf(head);
Tetsuo Handa5db5a392010-06-24 12:24:19 +09001038 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001039}
1040
1041/**
1042 * tomoyo_read_domain2 - Read domain policy.
1043 *
1044 * @head: Pointer to "struct tomoyo_io_buffer".
1045 * @domain: Pointer to "struct tomoyo_domain_info".
1046 *
1047 * Caller holds tomoyo_read_lock().
1048 *
1049 * Returns true on success, false otherwise.
1050 */
1051static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1052 struct tomoyo_domain_info *domain)
1053{
1054 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1055 struct tomoyo_acl_info *ptr =
1056 list_entry(head->r.acl, typeof(*ptr), list);
1057 if (!tomoyo_print_entry(head, ptr))
1058 return false;
1059 }
1060 head->r.acl = NULL;
1061 return true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001062}
1063
1064/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001065 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001066 *
1067 * @head: Pointer to "struct tomoyo_io_buffer".
1068 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001069 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001070 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001071static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001072{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001073 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001074 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001075 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001076 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001077 list_entry(head->r.domain, typeof(*domain), list);
1078 switch (head->r.step) {
1079 case 0:
1080 if (domain->is_deleted &&
1081 !head->r.print_this_domain_only)
1082 continue;
1083 /* Print domainname and flags. */
1084 tomoyo_set_string(head, domain->domainname->name);
1085 tomoyo_set_lf(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001086 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001087 domain->profile);
1088 if (domain->quota_warned)
1089 tomoyo_set_string(head, "quota_exceeded\n");
1090 if (domain->transition_failed)
1091 tomoyo_set_string(head, "transition_failed\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001092 head->r.step++;
1093 tomoyo_set_lf(head);
1094 /* fall through */
1095 case 1:
1096 if (!tomoyo_read_domain2(head, domain))
1097 return;
1098 head->r.step++;
1099 if (!tomoyo_set_lf(head))
1100 return;
1101 /* fall through */
1102 case 2:
1103 head->r.step = 0;
1104 if (head->r.print_this_domain_only)
1105 goto done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001106 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001107 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001108 done:
1109 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001110}
1111
1112/**
1113 * tomoyo_write_domain_profile - Assign profile for specified domain.
1114 *
1115 * @head: Pointer to "struct tomoyo_io_buffer".
1116 *
1117 * Returns 0 on success, -EINVAL otherwise.
1118 *
1119 * This is equivalent to doing
1120 *
1121 * ( echo "select " $domainname; echo "use_profile " $profile ) |
Tetsuo Handa9b2443732010-06-03 20:35:53 +09001122 * /usr/sbin/tomoyo-loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001123 *
1124 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001125 */
1126static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1127{
1128 char *data = head->write_buf;
1129 char *cp = strchr(data, ' ');
1130 struct tomoyo_domain_info *domain;
1131 unsigned long profile;
1132
1133 if (!cp)
1134 return -EINVAL;
1135 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001136 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001137 if (strict_strtoul(data, 10, &profile))
1138 return -EINVAL;
1139 if (domain && profile < TOMOYO_MAX_PROFILES
1140 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1141 domain->profile = (u8) profile;
1142 return 0;
1143}
1144
1145/**
1146 * tomoyo_read_domain_profile - Read only domainname and profile.
1147 *
1148 * @head: Pointer to "struct tomoyo_io_buffer".
1149 *
1150 * Returns list of profile number and domainname pairs.
1151 *
1152 * This is equivalent to doing
1153 *
1154 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1155 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1156 * domainname = $0; } else if ( $1 == "use_profile" ) {
1157 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001158 *
1159 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001160 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001161static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001162{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001163 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001164 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001165 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001166 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001167 list_entry(head->r.domain, typeof(*domain), list);
Kentaro Takeda95908372009-02-05 17:18:13 +09001168 if (domain->is_deleted)
1169 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001170 if (!tomoyo_flush(head))
1171 return;
1172 tomoyo_io_printf(head, "%u ", domain->profile);
1173 tomoyo_set_string(head, domain->domainname->name);
1174 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001175 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001176 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001177}
1178
1179/**
1180 * tomoyo_write_pid: Specify PID to obtain domainname.
1181 *
1182 * @head: Pointer to "struct tomoyo_io_buffer".
1183 *
1184 * Returns 0.
1185 */
1186static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1187{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001188 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001189 return 0;
1190}
1191
1192/**
1193 * tomoyo_read_pid - Get domainname of the specified PID.
1194 *
1195 * @head: Pointer to "struct tomoyo_io_buffer".
1196 *
1197 * Returns the domainname which the specified PID is in on success,
1198 * empty string otherwise.
1199 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1200 * using read()/write() interface rather than sysctl() interface.
1201 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001202static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001203{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001204 char *buf = head->write_buf;
1205 bool global_pid = false;
1206 unsigned int pid;
1207 struct task_struct *p;
1208 struct tomoyo_domain_info *domain = NULL;
1209
1210 /* Accessing write_buf is safe because head->io_sem is held. */
1211 if (!buf) {
1212 head->r.eof = true;
1213 return; /* Do nothing if open(O_RDONLY). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001214 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001215 if (head->r.w_pos || head->r.eof)
1216 return;
1217 head->r.eof = true;
1218 if (tomoyo_str_starts(&buf, "global-pid "))
1219 global_pid = true;
1220 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1221 rcu_read_lock();
1222 read_lock(&tasklist_lock);
1223 if (global_pid)
1224 p = find_task_by_pid_ns(pid, &init_pid_ns);
1225 else
1226 p = find_task_by_vpid(pid);
1227 if (p)
1228 domain = tomoyo_real_domain(p);
1229 read_unlock(&tasklist_lock);
1230 rcu_read_unlock();
1231 if (!domain)
1232 return;
1233 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1234 tomoyo_set_string(head, domain->domainname->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001235}
1236
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001237static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001238 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain",
1239 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain",
1240 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain",
1241 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001242};
1243
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001244static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001245 [TOMOYO_PATH_GROUP] = "path_group ",
1246 [TOMOYO_NUMBER_GROUP] = "number_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001247};
1248
Kentaro Takeda95908372009-02-05 17:18:13 +09001249/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001250 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001251 *
1252 * @head: Pointer to "struct tomoyo_io_buffer".
1253 *
1254 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001255 *
1256 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001257 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001258static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001259{
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001260 struct tomoyo_acl_param param = {
1261 .data = head->write_buf,
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001262 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001263 u8 i;
1264 param.is_delete = tomoyo_str_starts(&param.data, "delete ");
1265 if (tomoyo_str_starts(&param.data, "aggregator "))
1266 return tomoyo_write_aggregator(&param);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001267 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001268 if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
1269 return tomoyo_write_transition_control(&param, i);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001270 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
Tetsuo Handaa238cf52011-06-26 23:17:10 +09001271 if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
1272 return tomoyo_write_group(&param, i);
Kentaro Takeda95908372009-02-05 17:18:13 +09001273 return -EINVAL;
1274}
1275
Tetsuo Handa31845e82010-06-17 16:54:33 +09001276/**
1277 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1278 *
1279 * @head: Pointer to "struct tomoyo_io_buffer".
1280 * @idx: Index number.
1281 *
1282 * Returns true on success, false otherwise.
1283 *
1284 * Caller holds tomoyo_read_lock().
1285 */
1286static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1287{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001288 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001289 struct tomoyo_group *group =
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001290 list_entry(head->r.group, typeof(*group), head.list);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001291 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001292 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001293 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001294 if (ptr->is_deleted)
1295 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001296 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001297 return false;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001298 tomoyo_set_string(head, tomoyo_group_name[idx]);
1299 tomoyo_set_string(head, group->group_name->name);
1300 if (idx == TOMOYO_PATH_GROUP) {
1301 tomoyo_set_space(head);
1302 tomoyo_set_string(head, container_of
1303 (ptr, struct tomoyo_path_group,
1304 head)->member_name->name);
1305 } else if (idx == TOMOYO_NUMBER_GROUP) {
1306 tomoyo_print_number_union(head, &container_of
1307 (ptr,
1308 struct tomoyo_number_group,
1309 head)->number);
1310 }
1311 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001312 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001313 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001314 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001315 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001316 return true;
1317}
1318
1319/**
1320 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1321 *
1322 * @head: Pointer to "struct tomoyo_io_buffer".
1323 * @idx: Index number.
1324 *
1325 * Returns true on success, false otherwise.
1326 *
1327 * Caller holds tomoyo_read_lock().
1328 */
1329static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1330{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001331 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001332 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001333 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001334 if (acl->is_deleted)
1335 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001336 if (!tomoyo_flush(head))
1337 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001338 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001339 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001340 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001341 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001342 container_of(acl, typeof(*ptr), head);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001343 tomoyo_set_string(head, tomoyo_transition_type
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001344 [ptr->type]);
Tetsuo Handa0d2171d2011-06-26 23:17:46 +09001345 tomoyo_set_string(head, ptr->program ?
1346 ptr->program->name : "any");
1347 tomoyo_set_string(head, " from ");
1348 tomoyo_set_string(head, ptr->domainname ?
1349 ptr->domainname->name :
1350 "any");
Tetsuo Handa31845e82010-06-17 16:54:33 +09001351 }
1352 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001353 case TOMOYO_ID_AGGREGATOR:
1354 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001355 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001356 container_of(acl, typeof(*ptr), head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001357 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001358 tomoyo_set_string(head,
1359 ptr->original_name->name);
1360 tomoyo_set_space(head);
1361 tomoyo_set_string(head,
1362 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001363 }
1364 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001365 default:
1366 continue;
1367 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001368 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001369 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001370 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001371 return true;
1372}
1373
Kentaro Takeda95908372009-02-05 17:18:13 +09001374/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001375 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001376 *
1377 * @head: Pointer to "struct tomoyo_io_buffer".
1378 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001379 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001380 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001381static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001382{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001383 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001384 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001385 while (head->r.step < TOMOYO_MAX_POLICY &&
1386 tomoyo_read_policy(head, head->r.step))
1387 head->r.step++;
1388 if (head->r.step < TOMOYO_MAX_POLICY)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001389 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001390 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1391 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1392 head->r.step++;
1393 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001394 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001395 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001396}
1397
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001398/* Wait queue for kernel -> userspace notification. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001399static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001400/* Wait queue for userspace -> kernel notification. */
1401static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001402
1403/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001404struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001405 struct list_head list;
1406 char *query;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001407 size_t query_len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001408 unsigned int serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001409 u8 timer;
1410 u8 answer;
1411 u8 retry;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001412};
1413
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001414/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001415static LIST_HEAD(tomoyo_query_list);
1416
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001417/* Lock for manipulating tomoyo_query_list. */
1418static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1419
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001420/*
1421 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1422 * interface.
1423 */
1424static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1425
1426/**
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001427 * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1428 *
1429 * @domain: Pointer to "struct tomoyo_domain_info".
1430 * @header: Lines containing ACL.
1431 *
1432 * Returns nothing.
1433 */
1434static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
1435{
1436 char *buffer;
1437 char *cp = strchr(header, '\n');
1438 int len;
1439 if (!cp)
1440 return;
1441 cp = strchr(cp + 1, '\n');
1442 if (!cp)
1443 return;
1444 *cp++ = '\0';
1445 len = strlen(cp) + 1;
1446 buffer = kmalloc(len, GFP_NOFS);
1447 if (!buffer)
1448 return;
1449 snprintf(buffer, len - 1, "%s", cp);
1450 tomoyo_normalize_line(buffer);
1451 tomoyo_write_domain2(&domain->acl_info_list, buffer, false);
1452 kfree(buffer);
1453}
1454
1455/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001456 * tomoyo_supervisor - Ask for the supervisor's decision.
1457 *
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001458 * @r: Pointer to "struct tomoyo_request_info".
1459 * @fmt: The printf()'s format string, followed by parameters.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001460 *
1461 * Returns 0 if the supervisor decided to permit the access request which
1462 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1463 * supervisor decided to retry the access request which violated the policy in
1464 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1465 */
1466int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1467{
1468 va_list args;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001469 int error;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001470 int len;
1471 static unsigned int tomoyo_serial;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001472 struct tomoyo_query entry = { };
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001473 bool quota_exceeded = false;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001474 va_start(args, fmt);
1475 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
1476 va_end(args);
1477 /* Write /sys/kernel/security/tomoyo/audit. */
1478 va_start(args, fmt);
1479 tomoyo_write_log2(r, len, fmt, args);
1480 va_end(args);
1481 /* Nothing more to do if granted. */
1482 if (r->granted)
1483 return 0;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001484 switch (r->mode) {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001485 case TOMOYO_CONFIG_ENFORCING:
1486 error = -EPERM;
1487 if (atomic_read(&tomoyo_query_observers))
1488 break;
1489 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001490 case TOMOYO_CONFIG_LEARNING:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001491 error = 0;
1492 /* Check max_learning_entry parameter. */
1493 if (tomoyo_domain_quota_is_ok(r))
1494 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001495 /* fall through */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001496 default:
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001497 return 0;
1498 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001499 /* Get message. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001500 va_start(args, fmt);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001501 entry.query = tomoyo_init_log(r, len, fmt, args);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001502 va_end(args);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001503 if (!entry.query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001504 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001505 entry.query_len = strlen(entry.query) + 1;
1506 if (!error) {
1507 tomoyo_add_entry(r->domain, entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001508 goto out;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001509 }
1510 len = tomoyo_round2(entry.query_len);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001511 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001512 if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
1513 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
1514 >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001515 quota_exceeded = true;
1516 } else {
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001517 entry.serial = tomoyo_serial++;
1518 entry.retry = r->retry;
1519 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
1520 list_add_tail(&entry.list, &tomoyo_query_list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001521 }
1522 spin_unlock(&tomoyo_query_list_lock);
1523 if (quota_exceeded)
1524 goto out;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001525 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001526 while (entry.timer < 10) {
1527 wake_up_all(&tomoyo_query_wait);
1528 if (wait_event_interruptible_timeout
1529 (tomoyo_answer_wait, entry.answer ||
1530 !atomic_read(&tomoyo_query_observers), HZ))
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001531 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001532 else
1533 entry.timer++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001534 }
1535 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001536 list_del(&entry.list);
1537 tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001538 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001539 switch (entry.answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001540 case 3: /* Asked to retry by administrator. */
1541 error = TOMOYO_RETRY_REQUEST;
1542 r->retry++;
1543 break;
1544 case 1:
1545 /* Granted by administrator. */
1546 error = 0;
1547 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001548 default:
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001549 /* Timed out or rejected by administrator. */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001550 break;
1551 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001552out:
1553 kfree(entry.query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001554 return error;
1555}
1556
1557/**
1558 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1559 *
1560 * @file: Pointer to "struct file".
1561 * @wait: Pointer to "poll_table".
1562 *
1563 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1564 *
1565 * Waits for access requests which violated policy in enforcing mode.
1566 */
1567static int tomoyo_poll_query(struct file *file, poll_table *wait)
1568{
1569 struct list_head *tmp;
1570 bool found = false;
1571 u8 i;
1572 for (i = 0; i < 2; i++) {
1573 spin_lock(&tomoyo_query_list_lock);
1574 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001575 struct tomoyo_query *ptr =
1576 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001577 if (ptr->answer)
1578 continue;
1579 found = true;
1580 break;
1581 }
1582 spin_unlock(&tomoyo_query_list_lock);
1583 if (found)
1584 return POLLIN | POLLRDNORM;
1585 if (i)
1586 break;
1587 poll_wait(file, &tomoyo_query_wait, wait);
1588 }
1589 return 0;
1590}
1591
1592/**
1593 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1594 *
1595 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001596 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001597static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001598{
1599 struct list_head *tmp;
1600 int pos = 0;
1601 int len = 0;
1602 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001603 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001604 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001605 if (head->read_buf) {
1606 kfree(head->read_buf);
1607 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001608 }
1609 spin_lock(&tomoyo_query_list_lock);
1610 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001611 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001612 if (ptr->answer)
1613 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001614 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001615 continue;
1616 len = ptr->query_len;
1617 break;
1618 }
1619 spin_unlock(&tomoyo_query_list_lock);
1620 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001621 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001622 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001623 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001624 buf = kzalloc(len + 32, GFP_NOFS);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001625 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001626 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001627 pos = 0;
1628 spin_lock(&tomoyo_query_list_lock);
1629 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001630 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001631 if (ptr->answer)
1632 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001633 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001634 continue;
1635 /*
1636 * Some query can be skipped because tomoyo_query_list
1637 * can change, but I don't care.
1638 */
1639 if (len == ptr->query_len)
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001640 snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
1641 ptr->retry, ptr->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001642 break;
1643 }
1644 spin_unlock(&tomoyo_query_list_lock);
1645 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001646 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001647 head->r.w[head->r.w_pos++] = buf;
1648 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001649 } else {
1650 kfree(buf);
1651 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001652}
1653
1654/**
1655 * tomoyo_write_answer - Write the supervisor's decision.
1656 *
1657 * @head: Pointer to "struct tomoyo_io_buffer".
1658 *
1659 * Returns 0 on success, -EINVAL otherwise.
1660 */
1661static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1662{
1663 char *data = head->write_buf;
1664 struct list_head *tmp;
1665 unsigned int serial;
1666 unsigned int answer;
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 = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001670 ptr->timer = 0;
1671 }
1672 spin_unlock(&tomoyo_query_list_lock);
1673 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1674 return -EINVAL;
1675 spin_lock(&tomoyo_query_list_lock);
1676 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001677 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001678 if (ptr->serial != serial)
1679 continue;
1680 if (!ptr->answer)
1681 ptr->answer = answer;
1682 break;
1683 }
1684 spin_unlock(&tomoyo_query_list_lock);
1685 return 0;
1686}
1687
1688/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001689 * tomoyo_read_version: Get version.
1690 *
1691 * @head: Pointer to "struct tomoyo_io_buffer".
1692 *
1693 * Returns version information.
1694 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001695static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001696{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001697 if (!head->r.eof) {
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001698 tomoyo_io_printf(head, "2.4.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001699 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001700 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001701}
1702
1703/**
1704 * tomoyo_read_self_domain - Get the current process's domainname.
1705 *
1706 * @head: Pointer to "struct tomoyo_io_buffer".
1707 *
1708 * Returns the current process's domainname.
1709 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001710static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001711{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001712 if (!head->r.eof) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001713 /*
1714 * tomoyo_domain()->domainname != NULL
1715 * because every process belongs to a domain and
1716 * the domain's name cannot be NULL.
1717 */
1718 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001719 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001720 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001721}
1722
1723/**
1724 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1725 *
1726 * @type: Type of interface.
1727 * @file: Pointer to "struct file".
1728 *
1729 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001730 *
1731 * Caller acquires tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001732 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001733int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09001734{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001735 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001736
1737 if (!head)
1738 return -ENOMEM;
1739 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001740 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09001741 switch (type) {
1742 case TOMOYO_DOMAINPOLICY:
1743 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001744 head->write = tomoyo_write_domain;
1745 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001746 break;
1747 case TOMOYO_EXCEPTIONPOLICY:
1748 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001749 head->write = tomoyo_write_exception;
1750 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09001751 break;
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001752 case TOMOYO_AUDIT:
1753 /* /sys/kernel/security/tomoyo/audit */
1754 head->poll = tomoyo_poll_log;
1755 head->read = tomoyo_read_log;
1756 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001757 case TOMOYO_SELFDOMAIN:
1758 /* /sys/kernel/security/tomoyo/self_domain */
1759 head->read = tomoyo_read_self_domain;
1760 break;
1761 case TOMOYO_DOMAIN_STATUS:
1762 /* /sys/kernel/security/tomoyo/.domain_status */
1763 head->write = tomoyo_write_domain_profile;
1764 head->read = tomoyo_read_domain_profile;
1765 break;
1766 case TOMOYO_PROCESS_STATUS:
1767 /* /sys/kernel/security/tomoyo/.process_status */
1768 head->write = tomoyo_write_pid;
1769 head->read = tomoyo_read_pid;
1770 break;
1771 case TOMOYO_VERSION:
1772 /* /sys/kernel/security/tomoyo/version */
1773 head->read = tomoyo_read_version;
1774 head->readbuf_size = 128;
1775 break;
1776 case TOMOYO_MEMINFO:
1777 /* /sys/kernel/security/tomoyo/meminfo */
1778 head->write = tomoyo_write_memory_quota;
1779 head->read = tomoyo_read_memory_counter;
1780 head->readbuf_size = 512;
1781 break;
1782 case TOMOYO_PROFILE:
1783 /* /sys/kernel/security/tomoyo/profile */
1784 head->write = tomoyo_write_profile;
1785 head->read = tomoyo_read_profile;
1786 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001787 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1788 head->poll = tomoyo_poll_query;
1789 head->write = tomoyo_write_answer;
1790 head->read = tomoyo_read_query;
1791 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001792 case TOMOYO_MANAGER:
1793 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001794 head->write = tomoyo_write_manager;
1795 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09001796 break;
1797 }
1798 if (!(file->f_mode & FMODE_READ)) {
1799 /*
1800 * No need to allocate read_buf since it is not opened
1801 * for reading.
1802 */
1803 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001804 head->poll = NULL;
1805 } else if (!head->poll) {
1806 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001807 if (!head->readbuf_size)
1808 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001809 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001810 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001811 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001812 return -ENOMEM;
1813 }
1814 }
1815 if (!(file->f_mode & FMODE_WRITE)) {
1816 /*
1817 * No need to allocate write_buf since it is not opened
1818 * for writing.
1819 */
1820 head->write = NULL;
1821 } else if (head->write) {
1822 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001823 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001824 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001825 kfree(head->read_buf);
1826 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001827 return -ENOMEM;
1828 }
1829 }
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001830 if (type != TOMOYO_QUERY && type != TOMOYO_AUDIT)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001831 head->reader_idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09001832 file->private_data = head;
1833 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001834 * If the file is /sys/kernel/security/tomoyo/query , increment the
1835 * observer counter.
1836 * The obserber counter is used by tomoyo_supervisor() to see if
1837 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1838 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09001839 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001840 atomic_inc(&tomoyo_query_observers);
Kentaro Takeda95908372009-02-05 17:18:13 +09001841 return 0;
1842}
1843
1844/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09001845 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1846 *
1847 * @file: Pointer to "struct file".
1848 * @wait: Pointer to "poll_table".
1849 *
1850 * Waits for read readiness.
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001851 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
1852 * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09001853 */
1854int tomoyo_poll_control(struct file *file, poll_table *wait)
1855{
1856 struct tomoyo_io_buffer *head = file->private_data;
1857 if (!head->poll)
1858 return -ENOSYS;
1859 return head->poll(file, wait);
1860}
1861
1862/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001863 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1864 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001865 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001866 * @buffer: Poiner to buffer to write to.
1867 * @buffer_len: Size of @buffer.
1868 *
1869 * Returns bytes read on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001870 *
1871 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001872 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001873int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001874 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001875{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001876 int len;
Kentaro Takeda95908372009-02-05 17:18:13 +09001877
1878 if (!head->read)
1879 return -ENOSYS;
1880 if (mutex_lock_interruptible(&head->io_sem))
1881 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001882 head->read_user_buf = buffer;
1883 head->read_user_buf_avail = buffer_len;
1884 if (tomoyo_flush(head))
1885 /* Call the policy handler. */
1886 head->read(head);
1887 tomoyo_flush(head);
1888 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09001889 mutex_unlock(&head->io_sem);
1890 return len;
1891}
1892
1893/**
1894 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1895 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001896 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001897 * @buffer: Pointer to buffer to read from.
1898 * @buffer_len: Size of @buffer.
1899 *
1900 * Returns @buffer_len on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001901 *
1902 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001903 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001904int tomoyo_write_control(struct tomoyo_io_buffer *head,
1905 const char __user *buffer, const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001906{
Kentaro Takeda95908372009-02-05 17:18:13 +09001907 int error = buffer_len;
1908 int avail_len = buffer_len;
1909 char *cp0 = head->write_buf;
1910
1911 if (!head->write)
1912 return -ENOSYS;
1913 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1914 return -EFAULT;
1915 /* Don't allow updating policies by non manager programs. */
1916 if (head->write != tomoyo_write_pid &&
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001917 head->write != tomoyo_write_domain && !tomoyo_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +09001918 return -EPERM;
1919 if (mutex_lock_interruptible(&head->io_sem))
1920 return -EINTR;
1921 /* Read a line and dispatch it to the policy handler. */
1922 while (avail_len > 0) {
1923 char c;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001924 if (head->w.avail >= head->writebuf_size - 1) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001925 error = -ENOMEM;
1926 break;
1927 } else if (get_user(c, buffer)) {
1928 error = -EFAULT;
1929 break;
1930 }
1931 buffer++;
1932 avail_len--;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001933 cp0[head->w.avail++] = c;
Kentaro Takeda95908372009-02-05 17:18:13 +09001934 if (c != '\n')
1935 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001936 cp0[head->w.avail - 1] = '\0';
1937 head->w.avail = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09001938 tomoyo_normalize_line(cp0);
1939 head->write(head);
1940 }
1941 mutex_unlock(&head->io_sem);
1942 return error;
1943}
1944
1945/**
1946 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1947 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001948 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +09001949 *
1950 * Releases memory and returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001951 *
1952 * Caller looses tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001953 */
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +09001954int tomoyo_close_control(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001955{
Tetsuo Handa847b1732010-02-11 09:43:54 +09001956 const bool is_write = !!head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +09001957
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001958 /*
1959 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1960 * observer counter.
1961 */
1962 if (head->type == TOMOYO_QUERY)
1963 atomic_dec(&tomoyo_query_observers);
Tetsuo Handaeadd99c2011-06-26 23:18:58 +09001964 else if (head->type != TOMOYO_AUDIT)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001965 tomoyo_read_unlock(head->reader_idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09001966 /* Release memory used for policy I/O. */
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001967 kfree(head->read_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09001968 head->read_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001969 kfree(head->write_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09001970 head->write_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001971 kfree(head);
Tetsuo Handa847b1732010-02-11 09:43:54 +09001972 if (is_write)
1973 tomoyo_run_gc();
Kentaro Takeda95908372009-02-05 17:18:13 +09001974 return 0;
1975}
1976
1977/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001978 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09001979 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001980void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09001981{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001982 struct tomoyo_domain_info *domain;
1983 const int idx = tomoyo_read_lock();
1984 tomoyo_policy_loaded = true;
1985 /* Check all profiles currently assigned to domains are defined. */
1986 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1987 const u8 profile = domain->profile;
1988 if (tomoyo_profile_ptr[profile])
1989 continue;
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09001990 printk(KERN_ERR "You need to define profile %u before using it.\n",
1991 profile);
1992 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
1993 "for more information.\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001994 panic("Profile %u (used by '%s') not defined.\n",
1995 profile, domain->domainname->name);
1996 }
1997 tomoyo_read_unlock(idx);
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09001998 if (tomoyo_profile_version != 20090903) {
1999 printk(KERN_ERR "You need to install userland programs for "
2000 "TOMOYO 2.3 and initialize policy configuration.\n");
2001 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2002 "for more information.\n");
Tetsuo Handa57c25902010-06-03 20:38:44 +09002003 panic("Profile version %u is not supported.\n",
2004 tomoyo_profile_version);
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002005 }
Tetsuo Handae6f6a4c2010-07-27 17:17:06 +09002006 printk(KERN_INFO "TOMOYO: 2.3.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002007 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002008}