blob: 1c340217a06a043ed57fe1060ef2bd14453d1d43 [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 +090014static struct tomoyo_profile tomoyo_default_profile = {
15 .learning = &tomoyo_default_profile.preference,
16 .permissive = &tomoyo_default_profile.preference,
17 .enforcing = &tomoyo_default_profile.preference,
18 .preference.enforcing_verbose = true,
19 .preference.learning_max_entry = 2048,
20 .preference.learning_verbose = false,
21 .preference.permissive_verbose = true
22};
23
24/* Profile version. Currently only 20090903 is defined. */
25static unsigned int tomoyo_profile_version;
26
27/* Profile table. Memory is allocated as needed. */
28static struct tomoyo_profile *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
29
Kentaro Takeda95908372009-02-05 17:18:13 +090030/* String table for functionality that takes 4 modes. */
Tetsuo Handaf23571e2010-06-24 14:57:16 +090031static const char *tomoyo_mode[4] = {
Kentaro Takeda95908372009-02-05 17:18:13 +090032 "disabled", "learning", "permissive", "enforcing"
33};
Kentaro Takeda95908372009-02-05 17:18:13 +090034
Tetsuo Handa57c25902010-06-03 20:38:44 +090035/* String table for /sys/kernel/security/tomoyo/profile */
36static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
37 + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
38 [TOMOYO_MAC_FILE_EXECUTE] = "file::execute",
39 [TOMOYO_MAC_FILE_OPEN] = "file::open",
40 [TOMOYO_MAC_FILE_CREATE] = "file::create",
41 [TOMOYO_MAC_FILE_UNLINK] = "file::unlink",
Tetsuo Handa7c759642011-06-26 23:15:31 +090042 [TOMOYO_MAC_FILE_GETATTR] = "file::getattr",
Tetsuo Handa57c25902010-06-03 20:38:44 +090043 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
44 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
45 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
46 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
47 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
48 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
Tetsuo Handa57c25902010-06-03 20:38:44 +090049 [TOMOYO_MAC_FILE_MKBLOCK] = "file::mkblock",
50 [TOMOYO_MAC_FILE_MKCHAR] = "file::mkchar",
51 [TOMOYO_MAC_FILE_LINK] = "file::link",
52 [TOMOYO_MAC_FILE_RENAME] = "file::rename",
53 [TOMOYO_MAC_FILE_CHMOD] = "file::chmod",
54 [TOMOYO_MAC_FILE_CHOWN] = "file::chown",
55 [TOMOYO_MAC_FILE_CHGRP] = "file::chgrp",
56 [TOMOYO_MAC_FILE_IOCTL] = "file::ioctl",
57 [TOMOYO_MAC_FILE_CHROOT] = "file::chroot",
58 [TOMOYO_MAC_FILE_MOUNT] = "file::mount",
59 [TOMOYO_MAC_FILE_UMOUNT] = "file::umount",
60 [TOMOYO_MAC_FILE_PIVOT_ROOT] = "file::pivot_root",
61 [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
Kentaro Takeda95908372009-02-05 17:18:13 +090062};
63
Kentaro Takeda95908372009-02-05 17:18:13 +090064/* Permit policy management by non-root user? */
65static bool tomoyo_manage_by_non_root;
66
67/* Utility functions. */
68
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090069/**
Tetsuo Handa57c25902010-06-03 20:38:44 +090070 * tomoyo_yesno - Return "yes" or "no".
71 *
72 * @value: Bool value.
73 */
74static const char *tomoyo_yesno(const unsigned int value)
75{
76 return value ? "yes" : "no";
77}
78
Tetsuo Handaf23571e2010-06-24 14:57:16 +090079static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
80{
81 va_list args;
82 const int pos = strlen(buffer);
83 va_start(args, fmt);
84 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
85 va_end(args);
86}
87
88/**
89 * tomoyo_flush - Flush queued string to userspace's buffer.
90 *
91 * @head: Pointer to "struct tomoyo_io_buffer".
92 *
93 * Returns true if all data was flushed, false otherwise.
94 */
95static bool tomoyo_flush(struct tomoyo_io_buffer *head)
96{
97 while (head->r.w_pos) {
98 const char *w = head->r.w[0];
99 int len = strlen(w);
100 if (len) {
101 if (len > head->read_user_buf_avail)
102 len = head->read_user_buf_avail;
103 if (!len)
104 return false;
105 if (copy_to_user(head->read_user_buf, w, len))
106 return false;
107 head->read_user_buf_avail -= len;
108 head->read_user_buf += len;
109 w += len;
110 }
Tetsuo Handac0fa7972011-04-03 00:12:54 +0900111 head->r.w[0] = w;
112 if (*w)
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900113 return false;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900114 /* Add '\0' for query. */
115 if (head->poll) {
116 if (!head->read_user_buf_avail ||
117 copy_to_user(head->read_user_buf, "", 1))
118 return false;
119 head->read_user_buf_avail--;
120 head->read_user_buf++;
121 }
122 head->r.w_pos--;
123 for (len = 0; len < head->r.w_pos; len++)
124 head->r.w[len] = head->r.w[len + 1];
125 }
126 head->r.avail = 0;
127 return true;
128}
129
130/**
131 * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
132 *
133 * @head: Pointer to "struct tomoyo_io_buffer".
134 * @string: String to print.
135 *
136 * Note that @string has to be kept valid until @head is kfree()d.
137 * This means that char[] allocated on stack memory cannot be passed to
138 * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
139 */
140static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
141{
142 if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
143 head->r.w[head->r.w_pos++] = string;
144 tomoyo_flush(head);
145 } else
146 WARN_ON(1);
147}
148
149/**
150 * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
151 *
152 * @head: Pointer to "struct tomoyo_io_buffer".
153 * @fmt: The printf()'s format string, followed by parameters.
154 */
155void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
156{
157 va_list args;
158 int len;
159 int pos = head->r.avail;
160 int size = head->readbuf_size - pos;
161 if (size <= 0)
162 return;
163 va_start(args, fmt);
164 len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
165 va_end(args);
166 if (pos + len >= head->readbuf_size) {
167 WARN_ON(1);
168 return;
169 }
170 head->r.avail += len;
171 tomoyo_set_string(head, head->read_buf + pos);
172}
173
174static void tomoyo_set_space(struct tomoyo_io_buffer *head)
175{
176 tomoyo_set_string(head, " ");
177}
178
179static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
180{
181 tomoyo_set_string(head, "\n");
182 return !head->r.w_pos;
183}
184
Tetsuo Handa57c25902010-06-03 20:38:44 +0900185/**
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900186 * tomoyo_print_name_union - Print a tomoyo_name_union.
187 *
188 * @head: Pointer to "struct tomoyo_io_buffer".
189 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900190 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900191static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
192 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900193{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900194 tomoyo_set_space(head);
195 if (ptr->is_group) {
196 tomoyo_set_string(head, "@");
197 tomoyo_set_string(head, ptr->group->group_name->name);
198 } else {
199 tomoyo_set_string(head, ptr->filename->name);
200 }
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900201}
202
203/**
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900204 * tomoyo_print_number_union - Print a tomoyo_number_union.
205 *
206 * @head: Pointer to "struct tomoyo_io_buffer".
207 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900208 */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900209static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
210 const struct tomoyo_number_union *ptr)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900211{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900212 tomoyo_set_space(head);
213 if (ptr->is_group) {
214 tomoyo_set_string(head, "@");
215 tomoyo_set_string(head, ptr->group->group_name->name);
216 } else {
217 int i;
218 unsigned long min = ptr->values[0];
219 const unsigned long max = ptr->values[1];
220 u8 min_type = ptr->min_type;
221 const u8 max_type = ptr->max_type;
222 char buffer[128];
223 buffer[0] = '\0';
224 for (i = 0; i < 2; i++) {
225 switch (min_type) {
226 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
227 tomoyo_addprintf(buffer, sizeof(buffer),
228 "0x%lX", min);
229 break;
230 case TOMOYO_VALUE_TYPE_OCTAL:
231 tomoyo_addprintf(buffer, sizeof(buffer),
232 "0%lo", min);
233 break;
234 default:
235 tomoyo_addprintf(buffer, sizeof(buffer),
236 "%lu", min);
237 break;
238 }
239 if (min == max && min_type == max_type)
240 break;
241 tomoyo_addprintf(buffer, sizeof(buffer), "-");
242 min_type = max_type;
243 min = max;
244 }
245 tomoyo_io_printf(head, "%s", buffer);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900246 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900247}
248
249/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900250 * tomoyo_assign_profile - Create a new profile.
Kentaro Takeda95908372009-02-05 17:18:13 +0900251 *
252 * @profile: Profile number to create.
253 *
254 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
255 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900256static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
Kentaro Takeda95908372009-02-05 17:18:13 +0900257{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900258 struct tomoyo_profile *ptr;
259 struct tomoyo_profile *entry;
Kentaro Takeda95908372009-02-05 17:18:13 +0900260 if (profile >= TOMOYO_MAX_PROFILES)
261 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900262 ptr = tomoyo_profile_ptr[profile];
263 if (ptr)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900264 return ptr;
265 entry = kzalloc(sizeof(*entry), GFP_NOFS);
266 if (mutex_lock_interruptible(&tomoyo_policy_lock))
267 goto out;
268 ptr = tomoyo_profile_ptr[profile];
269 if (!ptr && tomoyo_memory_ok(entry)) {
270 ptr = entry;
271 ptr->learning = &tomoyo_default_profile.preference;
272 ptr->permissive = &tomoyo_default_profile.preference;
273 ptr->enforcing = &tomoyo_default_profile.preference;
274 ptr->default_config = TOMOYO_CONFIG_DISABLED;
275 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
276 sizeof(ptr->config));
277 mb(); /* Avoid out-of-order execution. */
278 tomoyo_profile_ptr[profile] = ptr;
279 entry = NULL;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900280 }
Tetsuo Handa29282382010-05-06 00:18:15 +0900281 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900282 out:
283 kfree(entry);
Kentaro Takeda95908372009-02-05 17:18:13 +0900284 return ptr;
285}
286
287/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900288 * tomoyo_profile - Find a profile.
289 *
290 * @profile: Profile number to find.
291 *
292 * Returns pointer to "struct tomoyo_profile".
293 */
294struct tomoyo_profile *tomoyo_profile(const u8 profile)
295{
296 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
297 if (!tomoyo_policy_loaded)
298 return &tomoyo_default_profile;
299 BUG_ON(!ptr);
300 return ptr;
301}
302
Tetsuo Handa8e568682010-06-25 09:30:09 +0900303static s8 tomoyo_find_yesno(const char *string, const char *find)
304{
305 const char *cp = strstr(string, find);
306 if (cp) {
307 cp += strlen(find);
308 if (!strncmp(cp, "=yes", 4))
309 return 1;
310 else if (!strncmp(cp, "=no", 3))
311 return 0;
312 }
313 return -1;
314}
315
316static void tomoyo_set_bool(bool *b, const char *string, const char *find)
317{
318 switch (tomoyo_find_yesno(string, find)) {
319 case 1:
320 *b = true;
321 break;
322 case 0:
323 *b = false;
324 break;
325 }
326}
327
328static void tomoyo_set_uint(unsigned int *i, const char *string,
329 const char *find)
330{
331 const char *cp = strstr(string, find);
332 if (cp)
333 sscanf(cp + strlen(find), "=%u", i);
334}
335
336static void tomoyo_set_pref(const char *name, const char *value,
337 const bool use_default,
338 struct tomoyo_profile *profile)
339{
340 struct tomoyo_preference **pref;
341 bool *verbose;
342 if (!strcmp(name, "enforcing")) {
343 if (use_default) {
344 pref = &profile->enforcing;
345 goto set_default;
346 }
347 profile->enforcing = &profile->preference;
348 verbose = &profile->preference.enforcing_verbose;
349 goto set_verbose;
350 }
351 if (!strcmp(name, "permissive")) {
352 if (use_default) {
353 pref = &profile->permissive;
354 goto set_default;
355 }
356 profile->permissive = &profile->preference;
357 verbose = &profile->preference.permissive_verbose;
358 goto set_verbose;
359 }
360 if (!strcmp(name, "learning")) {
361 if (use_default) {
362 pref = &profile->learning;
363 goto set_default;
364 }
365 profile->learning = &profile->preference;
366 tomoyo_set_uint(&profile->preference.learning_max_entry, value,
367 "max_entry");
368 verbose = &profile->preference.learning_verbose;
369 goto set_verbose;
370 }
371 return;
372 set_default:
373 *pref = &tomoyo_default_profile.preference;
374 return;
375 set_verbose:
376 tomoyo_set_bool(verbose, value, "verbose");
377}
378
379static int tomoyo_set_mode(char *name, const char *value,
380 const bool use_default,
381 struct tomoyo_profile *profile)
382{
383 u8 i;
384 u8 config;
385 if (!strcmp(name, "CONFIG")) {
386 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
387 config = profile->default_config;
388 } else if (tomoyo_str_starts(&name, "CONFIG::")) {
389 config = 0;
390 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
391 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
392 if (strcmp(name, tomoyo_mac_keywords[i]))
393 continue;
394 config = profile->config[i];
395 break;
396 }
397 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
398 return -EINVAL;
399 } else {
400 return -EINVAL;
401 }
402 if (use_default) {
403 config = TOMOYO_CONFIG_USE_DEFAULT;
404 } else {
405 u8 mode;
406 for (mode = 0; mode < 4; mode++)
407 if (strstr(value, tomoyo_mode[mode]))
408 /*
409 * Update lower 3 bits in order to distinguish
410 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
411 */
412 config = (config & ~7) | mode;
413 }
414 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
415 profile->config[i] = config;
416 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
417 profile->default_config = config;
418 return 0;
419}
420
Tetsuo Handa57c25902010-06-03 20:38:44 +0900421/**
422 * tomoyo_write_profile - Write profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900423 *
424 * @head: Pointer to "struct tomoyo_io_buffer".
425 *
426 * Returns 0 on success, negative value otherwise.
427 */
428static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
429{
430 char *data = head->write_buf;
431 unsigned int i;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900432 bool use_default = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900433 char *cp;
434 struct tomoyo_profile *profile;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900435 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
436 return 0;
437 i = simple_strtoul(data, &cp, 10);
438 if (data == cp) {
439 profile = &tomoyo_default_profile;
440 } else {
441 if (*cp != '-')
442 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900443 data = cp + 1;
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900444 profile = tomoyo_assign_profile(i);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900445 if (!profile)
446 return -EINVAL;
447 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900448 cp = strchr(data, '=');
449 if (!cp)
450 return -EINVAL;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900451 *cp++ = '\0';
452 if (profile != &tomoyo_default_profile)
453 use_default = strstr(cp, "use_default") != NULL;
Tetsuo Handa8e568682010-06-25 09:30:09 +0900454 if (tomoyo_str_starts(&data, "PREFERENCE::")) {
455 tomoyo_set_pref(data, cp, use_default, profile);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900456 return 0;
457 }
458 if (profile == &tomoyo_default_profile)
459 return -EINVAL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900460 if (!strcmp(data, "COMMENT")) {
Tetsuo Handa2a086e52011-04-03 00:09:26 +0900461 static DEFINE_SPINLOCK(lock);
462 const struct tomoyo_path_info *new_comment
463 = tomoyo_get_name(cp);
464 const struct tomoyo_path_info *old_comment;
465 if (!new_comment)
466 return -ENOMEM;
467 spin_lock(&lock);
468 old_comment = profile->comment;
469 profile->comment = new_comment;
470 spin_unlock(&lock);
Tetsuo Handabf24fb02010-02-11 09:41:58 +0900471 tomoyo_put_name(old_comment);
Kentaro Takeda95908372009-02-05 17:18:13 +0900472 return 0;
473 }
Tetsuo Handa8e568682010-06-25 09:30:09 +0900474 return tomoyo_set_mode(data, cp, use_default, profile);
Kentaro Takeda95908372009-02-05 17:18:13 +0900475}
476
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900477static void tomoyo_print_preference(struct tomoyo_io_buffer *head,
478 const int idx)
479{
480 struct tomoyo_preference *pref = &tomoyo_default_profile.preference;
481 const struct tomoyo_profile *profile = idx >= 0 ?
482 tomoyo_profile_ptr[idx] : NULL;
483 char buffer[16] = "";
484 if (profile) {
485 buffer[sizeof(buffer) - 1] = '\0';
486 snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
487 }
488 if (profile) {
489 pref = profile->learning;
490 if (pref == &tomoyo_default_profile.preference)
491 goto skip1;
492 }
493 tomoyo_io_printf(head, "%sPREFERENCE::%s={ "
494 "verbose=%s max_entry=%u }\n",
495 buffer, "learning",
496 tomoyo_yesno(pref->learning_verbose),
497 pref->learning_max_entry);
498 skip1:
499 if (profile) {
500 pref = profile->permissive;
501 if (pref == &tomoyo_default_profile.preference)
502 goto skip2;
503 }
504 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
505 buffer, "permissive",
506 tomoyo_yesno(pref->permissive_verbose));
507 skip2:
508 if (profile) {
509 pref = profile->enforcing;
510 if (pref == &tomoyo_default_profile.preference)
511 return;
512 }
513 tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
514 buffer, "enforcing",
515 tomoyo_yesno(pref->enforcing_verbose));
516}
517
518static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
519{
520 tomoyo_io_printf(head, "={ mode=%s }\n", tomoyo_mode[config & 3]);
521}
522
Kentaro Takeda95908372009-02-05 17:18:13 +0900523/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900524 * tomoyo_read_profile - Read profile table.
Kentaro Takeda95908372009-02-05 17:18:13 +0900525 *
526 * @head: Pointer to "struct tomoyo_io_buffer".
Kentaro Takeda95908372009-02-05 17:18:13 +0900527 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900528static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900529{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900530 u8 index;
531 const struct tomoyo_profile *profile;
532 next:
533 index = head->r.index;
534 profile = tomoyo_profile_ptr[index];
535 switch (head->r.step) {
536 case 0:
537 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
538 tomoyo_print_preference(head, -1);
539 head->r.step++;
540 break;
541 case 1:
542 for ( ; head->r.index < TOMOYO_MAX_PROFILES;
543 head->r.index++)
544 if (tomoyo_profile_ptr[head->r.index])
545 break;
546 if (head->r.index == TOMOYO_MAX_PROFILES)
547 return;
548 head->r.step++;
549 break;
550 case 2:
551 {
552 const struct tomoyo_path_info *comment =
553 profile->comment;
554 tomoyo_io_printf(head, "%u-COMMENT=", index);
555 tomoyo_set_string(head, comment ? comment->name : "");
556 tomoyo_set_lf(head);
557 head->r.step++;
558 }
559 break;
560 case 3:
561 {
562 tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
563 tomoyo_print_config(head, profile->default_config);
564 head->r.bit = 0;
565 head->r.step++;
566 }
567 break;
568 case 4:
569 for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
570 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
571 const u8 i = head->r.bit;
572 const u8 config = profile->config[i];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900573 if (config == TOMOYO_CONFIG_USE_DEFAULT)
574 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900575 tomoyo_io_printf(head, "%u-%s%s", index, "CONFIG::",
576 tomoyo_mac_keywords[i]);
577 tomoyo_print_config(head, config);
578 head->r.bit++;
579 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900580 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900581 if (head->r.bit == TOMOYO_MAX_MAC_INDEX
582 + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
583 tomoyo_print_preference(head, index);
584 head->r.index++;
585 head->r.step = 1;
586 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900587 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900588 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900589 if (tomoyo_flush(head))
590 goto next;
Kentaro Takeda95908372009-02-05 17:18:13 +0900591}
592
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900593static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
594 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900595{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900596 return container_of(a, struct tomoyo_manager, head)->manager ==
597 container_of(b, struct tomoyo_manager, head)->manager;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900598}
599
Kentaro Takeda95908372009-02-05 17:18:13 +0900600/**
601 * tomoyo_update_manager_entry - Add a manager entry.
602 *
603 * @manager: The path to manager or the domainnamme.
604 * @is_delete: True if it is a delete request.
605 *
606 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900607 *
608 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900609 */
610static int tomoyo_update_manager_entry(const char *manager,
611 const bool is_delete)
612{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900613 struct tomoyo_manager e = { };
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900614 int error;
Kentaro Takeda95908372009-02-05 17:18:13 +0900615
Tetsuo Handa75093152010-06-16 16:23:55 +0900616 if (tomoyo_domain_def(manager)) {
617 if (!tomoyo_correct_domain(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900618 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900619 e.is_domain = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900620 } else {
Tetsuo Handa75093152010-06-16 16:23:55 +0900621 if (!tomoyo_correct_path(manager))
Kentaro Takeda95908372009-02-05 17:18:13 +0900622 return -EINVAL;
623 }
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900624 e.manager = tomoyo_get_name(manager);
625 if (!e.manager)
Kentaro Takeda95908372009-02-05 17:18:13 +0900626 return -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900627 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900628 &tomoyo_policy_list[TOMOYO_ID_MANAGER],
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900629 tomoyo_same_manager);
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900630 tomoyo_put_name(e.manager);
Kentaro Takeda95908372009-02-05 17:18:13 +0900631 return error;
632}
633
634/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900635 * tomoyo_write_manager - Write manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900636 *
637 * @head: Pointer to "struct tomoyo_io_buffer".
638 *
639 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900640 *
641 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900642 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900643static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900644{
645 char *data = head->write_buf;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900646 bool is_delete = tomoyo_str_starts(&data, "delete ");
Kentaro Takeda95908372009-02-05 17:18:13 +0900647
648 if (!strcmp(data, "manage_by_non_root")) {
649 tomoyo_manage_by_non_root = !is_delete;
650 return 0;
651 }
652 return tomoyo_update_manager_entry(data, is_delete);
653}
654
655/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900656 * tomoyo_read_manager - Read manager policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900657 *
658 * @head: Pointer to "struct tomoyo_io_buffer".
659 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900660 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900661 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900662static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900663{
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900664 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +0900665 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900666 list_for_each_cookie(head->r.acl,
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900667 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900668 struct tomoyo_manager *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900669 list_entry(head->r.acl, typeof(*ptr), head.list);
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900670 if (ptr->head.is_deleted)
Kentaro Takeda95908372009-02-05 17:18:13 +0900671 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900672 if (!tomoyo_flush(head))
673 return;
674 tomoyo_set_string(head, ptr->manager->name);
675 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +0900676 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900677 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +0900678}
679
680/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900681 * tomoyo_manager - Check whether the current process is a policy manager.
Kentaro Takeda95908372009-02-05 17:18:13 +0900682 *
683 * Returns true if the current process is permitted to modify policy
684 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900685 *
686 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900687 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900688static bool tomoyo_manager(void)
Kentaro Takeda95908372009-02-05 17:18:13 +0900689{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900690 struct tomoyo_manager *ptr;
Kentaro Takeda95908372009-02-05 17:18:13 +0900691 const char *exe;
692 const struct task_struct *task = current;
693 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
694 bool found = false;
695
696 if (!tomoyo_policy_loaded)
697 return true;
698 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
699 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900700 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
701 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900702 if (!ptr->head.is_deleted && ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900703 && !tomoyo_pathcmp(domainname, ptr->manager)) {
704 found = true;
705 break;
706 }
707 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900708 if (found)
709 return true;
710 exe = tomoyo_get_exe();
711 if (!exe)
712 return false;
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900713 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
714 head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900715 if (!ptr->head.is_deleted && !ptr->is_domain
Kentaro Takeda95908372009-02-05 17:18:13 +0900716 && !strcmp(exe, ptr->manager->name)) {
717 found = true;
718 break;
719 }
720 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900721 if (!found) { /* Reduce error messages. */
722 static pid_t last_pid;
723 const pid_t pid = current->pid;
724 if (last_pid != pid) {
725 printk(KERN_WARNING "%s ( %s ) is not permitted to "
726 "update policies.\n", domainname->name, exe);
727 last_pid = pid;
728 }
729 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900730 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +0900731 return found;
732}
733
734/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900735 * tomoyo_select_one - Parse select command.
Kentaro Takeda95908372009-02-05 17:18:13 +0900736 *
737 * @head: Pointer to "struct tomoyo_io_buffer".
738 * @data: String to parse.
739 *
740 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900741 *
742 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900743 */
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900744static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
Kentaro Takeda95908372009-02-05 17:18:13 +0900745{
746 unsigned int pid;
747 struct tomoyo_domain_info *domain = NULL;
Tetsuo Handa9b244372010-06-03 20:35:53 +0900748 bool global_pid = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900749
Tetsuo Handa063821c2010-06-24 12:00:25 +0900750 if (!strcmp(data, "allow_execute")) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900751 head->r.print_execute_only = true;
Tetsuo Handa063821c2010-06-24 12:00:25 +0900752 return true;
753 }
Tetsuo Handa9b244372010-06-03 20:35:53 +0900754 if (sscanf(data, "pid=%u", &pid) == 1 ||
755 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900756 struct task_struct *p;
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900757 rcu_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900758 read_lock(&tasklist_lock);
Tetsuo Handa9b244372010-06-03 20:35:53 +0900759 if (global_pid)
760 p = find_task_by_pid_ns(pid, &init_pid_ns);
761 else
762 p = find_task_by_vpid(pid);
Kentaro Takeda95908372009-02-05 17:18:13 +0900763 if (p)
764 domain = tomoyo_real_domain(p);
765 read_unlock(&tasklist_lock);
Tetsuo Handa1fcdc7c2010-02-25 17:19:25 +0900766 rcu_read_unlock();
Kentaro Takeda95908372009-02-05 17:18:13 +0900767 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900768 if (tomoyo_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +0900769 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +0900770 } else
771 return false;
772 head->write_var1 = domain;
773 /* Accessing read_buf is safe because head->io_sem is held. */
774 if (!head->read_buf)
775 return true; /* Do nothing if open(O_WRONLY). */
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900776 memset(&head->r, 0, sizeof(head->r));
777 head->r.print_this_domain_only = true;
Dan Carpenter68eda8f2010-08-08 00:17:51 +0200778 if (domain)
779 head->r.domain = &domain->list;
780 else
781 head->r.eof = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +0900782 tomoyo_io_printf(head, "# select %s\n", data);
Tetsuo Handa475e6fa2010-06-24 11:28:14 +0900783 if (domain && domain->is_deleted)
784 tomoyo_io_printf(head, "# This is a deleted domain.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +0900785 return true;
786}
787
788/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900789 * tomoyo_delete_domain - Delete a domain.
790 *
791 * @domainname: The name of domain.
792 *
793 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900794 *
795 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900796 */
797static int tomoyo_delete_domain(char *domainname)
798{
799 struct tomoyo_domain_info *domain;
800 struct tomoyo_path_info name;
801
802 name.name = domainname;
803 tomoyo_fill_path_info(&name);
Tetsuo Handa29282382010-05-06 00:18:15 +0900804 if (mutex_lock_interruptible(&tomoyo_policy_lock))
805 return 0;
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900806 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900807 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900808 /* Never delete tomoyo_kernel_domain */
809 if (domain == &tomoyo_kernel_domain)
810 continue;
811 if (domain->is_deleted ||
812 tomoyo_pathcmp(domain->domainname, &name))
813 continue;
814 domain->is_deleted = true;
815 break;
816 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900817 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +0900818 return 0;
819}
820
821/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900822 * tomoyo_write_domain2 - Write domain policy.
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900823 *
824 * @head: Pointer to "struct tomoyo_io_buffer".
825 *
826 * Returns 0 on success, negative value otherwise.
827 *
828 * Caller holds tomoyo_read_lock().
829 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900830static int tomoyo_write_domain2(char *data, struct tomoyo_domain_info *domain,
831 const bool is_delete)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900832{
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900833 if (tomoyo_str_starts(&data, "allow_mount "))
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900834 return tomoyo_write_mount(data, domain, is_delete);
835 return tomoyo_write_file(data, domain, is_delete);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900836}
837
838/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900839 * tomoyo_write_domain - Write domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +0900840 *
841 * @head: Pointer to "struct tomoyo_io_buffer".
842 *
843 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900844 *
845 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900846 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900847static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +0900848{
849 char *data = head->write_buf;
850 struct tomoyo_domain_info *domain = head->write_var1;
851 bool is_delete = false;
852 bool is_select = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900853 unsigned int profile;
854
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900855 if (tomoyo_str_starts(&data, "delete "))
Kentaro Takeda95908372009-02-05 17:18:13 +0900856 is_delete = true;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900857 else if (tomoyo_str_starts(&data, "select "))
Kentaro Takeda95908372009-02-05 17:18:13 +0900858 is_select = true;
Tetsuo Handa75093152010-06-16 16:23:55 +0900859 if (is_select && tomoyo_select_one(head, data))
Kentaro Takeda95908372009-02-05 17:18:13 +0900860 return 0;
861 /* Don't allow updating policies by non manager programs. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900862 if (!tomoyo_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +0900863 return -EPERM;
Tetsuo Handa75093152010-06-16 16:23:55 +0900864 if (tomoyo_domain_def(data)) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900865 domain = NULL;
866 if (is_delete)
867 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900868 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +0900869 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900870 else
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900871 domain = tomoyo_assign_domain(data, 0);
Kentaro Takeda95908372009-02-05 17:18:13 +0900872 head->write_var1 = domain;
873 return 0;
874 }
875 if (!domain)
876 return -EINVAL;
877
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900878 if (sscanf(data, "use_profile %u", &profile) == 1
Kentaro Takeda95908372009-02-05 17:18:13 +0900879 && profile < TOMOYO_MAX_PROFILES) {
880 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
881 domain->profile = (u8) profile;
882 return 0;
883 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900884 if (!strcmp(data, "quota_exceeded")) {
Tetsuo Handa9b244372010-06-03 20:35:53 +0900885 domain->quota_warned = !is_delete;
886 return 0;
887 }
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900888 if (!strcmp(data, "transition_failed")) {
Tetsuo Handa9b244372010-06-03 20:35:53 +0900889 domain->transition_failed = !is_delete;
890 return 0;
891 }
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900892 return tomoyo_write_domain2(data, domain, is_delete);
Kentaro Takeda95908372009-02-05 17:18:13 +0900893}
894
895/**
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900896 * tomoyo_fns - Find next set bit.
Kentaro Takeda95908372009-02-05 17:18:13 +0900897 *
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900898 * @perm: 8 bits value.
899 * @bit: First bit to find.
Kentaro Takeda95908372009-02-05 17:18:13 +0900900 *
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900901 * Returns next on-bit on success, 8 otherwise.
Kentaro Takeda95908372009-02-05 17:18:13 +0900902 */
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900903static u8 tomoyo_fns(const u8 perm, u8 bit)
Kentaro Takeda95908372009-02-05 17:18:13 +0900904{
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900905 for ( ; bit < 8; bit++)
906 if (perm & (1 << bit))
907 break;
908 return bit;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900909}
910
911/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900912 * tomoyo_print_entry - Print an ACL entry.
913 *
914 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900915 * @acl: Pointer to an ACL entry.
Kentaro Takeda95908372009-02-05 17:18:13 +0900916 *
917 * Returns true on success, false otherwise.
918 */
919static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900920 struct tomoyo_acl_info *acl)
Kentaro Takeda95908372009-02-05 17:18:13 +0900921{
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900922 const u8 acl_type = acl->type;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900923 u8 bit;
Kentaro Takeda95908372009-02-05 17:18:13 +0900924
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900925 if (acl->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900926 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900927 next:
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900928 bit = head->r.bit;
929 if (!tomoyo_flush(head))
930 return false;
931 else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900932 struct tomoyo_path_acl *ptr =
933 container_of(acl, typeof(*ptr), head);
934 const u16 perm = ptr->perm;
935 for ( ; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
936 if (!(perm & (1 << bit)))
937 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900938 if (head->r.print_execute_only &&
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900939 bit != TOMOYO_TYPE_EXECUTE)
940 continue;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900941 break;
942 }
943 if (bit >= TOMOYO_MAX_PATH_OPERATION)
944 goto done;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900945 tomoyo_io_printf(head, "allow_%s", tomoyo_path_keyword[bit]);
946 tomoyo_print_name_union(head, &ptr->name);
947 } else if (head->r.print_execute_only) {
Tetsuo Handa063821c2010-06-24 12:00:25 +0900948 return true;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900949 } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
950 struct tomoyo_path2_acl *ptr =
951 container_of(acl, typeof(*ptr), head);
952 bit = tomoyo_fns(ptr->perm, bit);
953 if (bit >= TOMOYO_MAX_PATH2_OPERATION)
954 goto done;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900955 tomoyo_io_printf(head, "allow_%s", tomoyo_path2_keyword[bit]);
956 tomoyo_print_name_union(head, &ptr->name1);
957 tomoyo_print_name_union(head, &ptr->name2);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900958 } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
959 struct tomoyo_path_number_acl *ptr =
960 container_of(acl, typeof(*ptr), head);
961 bit = tomoyo_fns(ptr->perm, bit);
962 if (bit >= TOMOYO_MAX_PATH_NUMBER_OPERATION)
963 goto done;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900964 tomoyo_io_printf(head, "allow_%s",
965 tomoyo_path_number_keyword[bit]);
966 tomoyo_print_name_union(head, &ptr->name);
967 tomoyo_print_number_union(head, &ptr->number);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900968 } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
969 struct tomoyo_mkdev_acl *ptr =
970 container_of(acl, typeof(*ptr), head);
971 bit = tomoyo_fns(ptr->perm, bit);
972 if (bit >= TOMOYO_MAX_MKDEV_OPERATION)
973 goto done;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900974 tomoyo_io_printf(head, "allow_%s", tomoyo_mkdev_keyword[bit]);
975 tomoyo_print_name_union(head, &ptr->name);
976 tomoyo_print_number_union(head, &ptr->mode);
977 tomoyo_print_number_union(head, &ptr->major);
978 tomoyo_print_number_union(head, &ptr->minor);
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900979 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
980 struct tomoyo_mount_acl *ptr =
981 container_of(acl, typeof(*ptr), head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900982 tomoyo_io_printf(head, "allow_mount");
983 tomoyo_print_name_union(head, &ptr->dev_name);
984 tomoyo_print_name_union(head, &ptr->dir_name);
985 tomoyo_print_name_union(head, &ptr->fs_type);
986 tomoyo_print_number_union(head, &ptr->flags);
Kentaro Takeda95908372009-02-05 17:18:13 +0900987 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900988 head->r.bit = bit + 1;
989 tomoyo_io_printf(head, "\n");
990 if (acl_type != TOMOYO_TYPE_MOUNT_ACL)
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900991 goto next;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900992 done:
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900993 head->r.bit = 0;
Tetsuo Handa5db5a392010-06-24 12:24:19 +0900994 return true;
Tetsuo Handaf23571e2010-06-24 14:57:16 +0900995}
996
997/**
998 * tomoyo_read_domain2 - Read domain policy.
999 *
1000 * @head: Pointer to "struct tomoyo_io_buffer".
1001 * @domain: Pointer to "struct tomoyo_domain_info".
1002 *
1003 * Caller holds tomoyo_read_lock().
1004 *
1005 * Returns true on success, false otherwise.
1006 */
1007static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
1008 struct tomoyo_domain_info *domain)
1009{
1010 list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1011 struct tomoyo_acl_info *ptr =
1012 list_entry(head->r.acl, typeof(*ptr), list);
1013 if (!tomoyo_print_entry(head, ptr))
1014 return false;
1015 }
1016 head->r.acl = NULL;
1017 return true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001018}
1019
1020/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001021 * tomoyo_read_domain - Read domain policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001022 *
1023 * @head: Pointer to "struct tomoyo_io_buffer".
1024 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001025 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001026 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001027static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001028{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001029 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001030 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001031 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001032 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001033 list_entry(head->r.domain, typeof(*domain), list);
1034 switch (head->r.step) {
1035 case 0:
1036 if (domain->is_deleted &&
1037 !head->r.print_this_domain_only)
1038 continue;
1039 /* Print domainname and flags. */
1040 tomoyo_set_string(head, domain->domainname->name);
1041 tomoyo_set_lf(head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001042 tomoyo_io_printf(head, "use_profile %u\n",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001043 domain->profile);
1044 if (domain->quota_warned)
1045 tomoyo_set_string(head, "quota_exceeded\n");
1046 if (domain->transition_failed)
1047 tomoyo_set_string(head, "transition_failed\n");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001048 head->r.step++;
1049 tomoyo_set_lf(head);
1050 /* fall through */
1051 case 1:
1052 if (!tomoyo_read_domain2(head, domain))
1053 return;
1054 head->r.step++;
1055 if (!tomoyo_set_lf(head))
1056 return;
1057 /* fall through */
1058 case 2:
1059 head->r.step = 0;
1060 if (head->r.print_this_domain_only)
1061 goto done;
Kentaro Takeda95908372009-02-05 17:18:13 +09001062 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001063 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001064 done:
1065 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001066}
1067
1068/**
1069 * tomoyo_write_domain_profile - Assign profile for specified domain.
1070 *
1071 * @head: Pointer to "struct tomoyo_io_buffer".
1072 *
1073 * Returns 0 on success, -EINVAL otherwise.
1074 *
1075 * This is equivalent to doing
1076 *
1077 * ( echo "select " $domainname; echo "use_profile " $profile ) |
Tetsuo Handa9b244372010-06-03 20:35:53 +09001078 * /usr/sbin/tomoyo-loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001079 *
1080 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001081 */
1082static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1083{
1084 char *data = head->write_buf;
1085 char *cp = strchr(data, ' ');
1086 struct tomoyo_domain_info *domain;
1087 unsigned long profile;
1088
1089 if (!cp)
1090 return -EINVAL;
1091 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001092 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001093 if (strict_strtoul(data, 10, &profile))
1094 return -EINVAL;
1095 if (domain && profile < TOMOYO_MAX_PROFILES
1096 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1097 domain->profile = (u8) profile;
1098 return 0;
1099}
1100
1101/**
1102 * tomoyo_read_domain_profile - Read only domainname and profile.
1103 *
1104 * @head: Pointer to "struct tomoyo_io_buffer".
1105 *
1106 * Returns list of profile number and domainname pairs.
1107 *
1108 * This is equivalent to doing
1109 *
1110 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1111 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1112 * domainname = $0; } else if ( $1 == "use_profile" ) {
1113 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001114 *
1115 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001116 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001117static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001118{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001119 if (head->r.eof)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001120 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001121 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001122 struct tomoyo_domain_info *domain =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001123 list_entry(head->r.domain, typeof(*domain), list);
Kentaro Takeda95908372009-02-05 17:18:13 +09001124 if (domain->is_deleted)
1125 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001126 if (!tomoyo_flush(head))
1127 return;
1128 tomoyo_io_printf(head, "%u ", domain->profile);
1129 tomoyo_set_string(head, domain->domainname->name);
1130 tomoyo_set_lf(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001131 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001132 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001133}
1134
1135/**
1136 * tomoyo_write_pid: Specify PID to obtain domainname.
1137 *
1138 * @head: Pointer to "struct tomoyo_io_buffer".
1139 *
1140 * Returns 0.
1141 */
1142static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1143{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001144 head->r.eof = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001145 return 0;
1146}
1147
1148/**
1149 * tomoyo_read_pid - Get domainname of the specified PID.
1150 *
1151 * @head: Pointer to "struct tomoyo_io_buffer".
1152 *
1153 * Returns the domainname which the specified PID is in on success,
1154 * empty string otherwise.
1155 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1156 * using read()/write() interface rather than sysctl() interface.
1157 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001158static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001159{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001160 char *buf = head->write_buf;
1161 bool global_pid = false;
1162 unsigned int pid;
1163 struct task_struct *p;
1164 struct tomoyo_domain_info *domain = NULL;
1165
1166 /* Accessing write_buf is safe because head->io_sem is held. */
1167 if (!buf) {
1168 head->r.eof = true;
1169 return; /* Do nothing if open(O_RDONLY). */
Kentaro Takeda95908372009-02-05 17:18:13 +09001170 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001171 if (head->r.w_pos || head->r.eof)
1172 return;
1173 head->r.eof = true;
1174 if (tomoyo_str_starts(&buf, "global-pid "))
1175 global_pid = true;
1176 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1177 rcu_read_lock();
1178 read_lock(&tasklist_lock);
1179 if (global_pid)
1180 p = find_task_by_pid_ns(pid, &init_pid_ns);
1181 else
1182 p = find_task_by_vpid(pid);
1183 if (p)
1184 domain = tomoyo_real_domain(p);
1185 read_unlock(&tasklist_lock);
1186 rcu_read_unlock();
1187 if (!domain)
1188 return;
1189 tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
1190 tomoyo_set_string(head, domain->domainname->name);
Kentaro Takeda95908372009-02-05 17:18:13 +09001191}
1192
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001193static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001194 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain",
1195 [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain",
1196 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain",
1197 [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain",
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001198};
1199
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001200static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001201 [TOMOYO_PATH_GROUP] = "path_group ",
1202 [TOMOYO_NUMBER_GROUP] = "number_group ",
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001203};
1204
Kentaro Takeda95908372009-02-05 17:18:13 +09001205/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001206 * tomoyo_write_exception - Write exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001207 *
1208 * @head: Pointer to "struct tomoyo_io_buffer".
1209 *
1210 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001211 *
1212 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001213 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001214static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001215{
1216 char *data = head->write_buf;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001217 bool is_delete = tomoyo_str_starts(&data, "delete ");
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001218 u8 i;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001219 static const struct {
1220 const char *keyword;
1221 int (*write) (char *, const bool);
Tetsuo Handa7c759642011-06-26 23:15:31 +09001222 } tomoyo_callback[1] = {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001223 { "aggregator ", tomoyo_write_aggregator },
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001224 };
Kentaro Takeda95908372009-02-05 17:18:13 +09001225
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001226 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001227 if (tomoyo_str_starts(&data, tomoyo_transition_type[i]))
1228 return tomoyo_write_transition_control(data, is_delete,
1229 i);
Tetsuo Handa7c759642011-06-26 23:15:31 +09001230 for (i = 0; i < 1; i++)
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001231 if (tomoyo_str_starts(&data, tomoyo_callback[i].keyword))
1232 return tomoyo_callback[i].write(data, is_delete);
1233 for (i = 0; i < TOMOYO_MAX_GROUP; i++)
1234 if (tomoyo_str_starts(&data, tomoyo_group_name[i]))
1235 return tomoyo_write_group(data, is_delete, i);
Kentaro Takeda95908372009-02-05 17:18:13 +09001236 return -EINVAL;
1237}
1238
Tetsuo Handa31845e82010-06-17 16:54:33 +09001239/**
1240 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
1241 *
1242 * @head: Pointer to "struct tomoyo_io_buffer".
1243 * @idx: Index number.
1244 *
1245 * Returns true on success, false otherwise.
1246 *
1247 * Caller holds tomoyo_read_lock().
1248 */
1249static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
1250{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001251 list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001252 struct tomoyo_group *group =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001253 list_entry(head->r.group, typeof(*group), list);
1254 list_for_each_cookie(head->r.acl, &group->member_list) {
Tetsuo Handa31845e82010-06-17 16:54:33 +09001255 struct tomoyo_acl_head *ptr =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001256 list_entry(head->r.acl, typeof(*ptr), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001257 if (ptr->is_deleted)
1258 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001259 if (!tomoyo_flush(head))
Tetsuo Handa31845e82010-06-17 16:54:33 +09001260 return false;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001261 tomoyo_set_string(head, tomoyo_group_name[idx]);
1262 tomoyo_set_string(head, group->group_name->name);
1263 if (idx == TOMOYO_PATH_GROUP) {
1264 tomoyo_set_space(head);
1265 tomoyo_set_string(head, container_of
1266 (ptr, struct tomoyo_path_group,
1267 head)->member_name->name);
1268 } else if (idx == TOMOYO_NUMBER_GROUP) {
1269 tomoyo_print_number_union(head, &container_of
1270 (ptr,
1271 struct tomoyo_number_group,
1272 head)->number);
1273 }
1274 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001275 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001276 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001277 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001278 head->r.group = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001279 return true;
1280}
1281
1282/**
1283 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1284 *
1285 * @head: Pointer to "struct tomoyo_io_buffer".
1286 * @idx: Index number.
1287 *
1288 * Returns true on success, false otherwise.
1289 *
1290 * Caller holds tomoyo_read_lock().
1291 */
1292static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1293{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001294 list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
Tetsuo Handa475e6fa2010-06-24 11:28:14 +09001295 struct tomoyo_acl_head *acl =
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001296 container_of(head->r.acl, typeof(*acl), list);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001297 if (acl->is_deleted)
1298 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001299 if (!tomoyo_flush(head))
1300 return false;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001301 switch (idx) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001302 case TOMOYO_ID_TRANSITION_CONTROL:
Tetsuo Handa31845e82010-06-17 16:54:33 +09001303 {
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001304 struct tomoyo_transition_control *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001305 container_of(acl, typeof(*ptr), head);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001306 tomoyo_set_string(head,
1307 tomoyo_transition_type
1308 [ptr->type]);
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001309 if (ptr->program)
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001310 tomoyo_set_string(head,
1311 ptr->program->name);
1312 if (ptr->program && ptr->domainname)
1313 tomoyo_set_string(head, " from ");
Tetsuo Handa5448ec42010-06-21 11:14:39 +09001314 if (ptr->domainname)
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001315 tomoyo_set_string(head,
1316 ptr->domainname->
1317 name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001318 }
1319 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001320 case TOMOYO_ID_AGGREGATOR:
1321 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001322 struct tomoyo_aggregator *ptr =
Tetsuo Handa31845e82010-06-17 16:54:33 +09001323 container_of(acl, typeof(*ptr), head);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +09001324 tomoyo_set_string(head, "aggregator ");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001325 tomoyo_set_string(head,
1326 ptr->original_name->name);
1327 tomoyo_set_space(head);
1328 tomoyo_set_string(head,
1329 ptr->aggregated_name->name);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001330 }
1331 break;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001332 default:
1333 continue;
1334 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001335 tomoyo_set_lf(head);
Tetsuo Handa31845e82010-06-17 16:54:33 +09001336 }
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001337 head->r.acl = NULL;
Tetsuo Handa31845e82010-06-17 16:54:33 +09001338 return true;
1339}
1340
Kentaro Takeda95908372009-02-05 17:18:13 +09001341/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001342 * tomoyo_read_exception - Read exception policy.
Kentaro Takeda95908372009-02-05 17:18:13 +09001343 *
1344 * @head: Pointer to "struct tomoyo_io_buffer".
1345 *
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001346 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001347 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001348static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001349{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001350 if (head->r.eof)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001351 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001352 while (head->r.step < TOMOYO_MAX_POLICY &&
1353 tomoyo_read_policy(head, head->r.step))
1354 head->r.step++;
1355 if (head->r.step < TOMOYO_MAX_POLICY)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001356 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001357 while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1358 tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
1359 head->r.step++;
1360 if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
Tetsuo Handa31845e82010-06-17 16:54:33 +09001361 return;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001362 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001363}
1364
Kentaro Takeda95908372009-02-05 17:18:13 +09001365/**
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001366 * tomoyo_print_header - Get header line of audit log.
1367 *
1368 * @r: Pointer to "struct tomoyo_request_info".
1369 *
1370 * Returns string representation.
1371 *
1372 * This function uses kmalloc(), so caller must kfree() if this function
1373 * didn't return NULL.
1374 */
1375static char *tomoyo_print_header(struct tomoyo_request_info *r)
1376{
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001377 struct timeval tv;
1378 const pid_t gpid = task_pid_nr(current);
1379 static const int tomoyo_buffer_len = 4096;
1380 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
Ben Hutchingsc8da96e2010-09-26 05:55:13 +01001381 pid_t ppid;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001382 if (!buffer)
1383 return NULL;
1384 do_gettimeofday(&tv);
Ben Hutchingsc8da96e2010-09-26 05:55:13 +01001385 rcu_read_lock();
1386 ppid = task_tgid_vnr(current->real_parent);
1387 rcu_read_unlock();
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001388 snprintf(buffer, tomoyo_buffer_len - 1,
1389 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1390 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1391 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001392 tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
Ben Hutchingsc8da96e2010-09-26 05:55:13 +01001393 task_tgid_vnr(current), ppid,
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001394 current_uid(), current_gid(), current_euid(),
1395 current_egid(), current_suid(), current_sgid(),
1396 current_fsuid(), current_fsgid());
1397 return buffer;
1398}
1399
1400/**
1401 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1402 *
1403 * @len: Required size.
1404 * @r: Pointer to "struct tomoyo_request_info".
1405 *
1406 * Returns pointer to allocated memory.
1407 *
1408 * The @len is updated to add the header lines' size on success.
1409 *
1410 * This function uses kzalloc(), so caller must kfree() if this function
1411 * didn't return NULL.
1412 */
1413static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1414{
1415 char *buf = NULL;
1416 const char *header;
1417 const char *domainname;
1418 if (!r->domain)
1419 r->domain = tomoyo_domain();
1420 domainname = r->domain->domainname->name;
1421 header = tomoyo_print_header(r);
1422 if (!header)
1423 return NULL;
1424 *len += strlen(domainname) + strlen(header) + 10;
1425 buf = kzalloc(*len, GFP_NOFS);
1426 if (buf)
1427 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1428 kfree(header);
1429 return buf;
1430}
1431
1432/* Wait queue for tomoyo_query_list. */
1433static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1434
1435/* Lock for manipulating tomoyo_query_list. */
1436static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1437
1438/* Structure for query. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001439struct tomoyo_query {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001440 struct list_head list;
1441 char *query;
1442 int query_len;
1443 unsigned int serial;
1444 int timer;
1445 int answer;
1446};
1447
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001448/* The list for "struct tomoyo_query". */
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001449static LIST_HEAD(tomoyo_query_list);
1450
1451/*
1452 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1453 * interface.
1454 */
1455static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1456
1457/**
1458 * tomoyo_supervisor - Ask for the supervisor's decision.
1459 *
1460 * @r: Pointer to "struct tomoyo_request_info".
1461 * @fmt: The printf()'s format string, followed by parameters.
1462 *
1463 * Returns 0 if the supervisor decided to permit the access request which
1464 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1465 * supervisor decided to retry the access request which violated the policy in
1466 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1467 */
1468int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1469{
1470 va_list args;
1471 int error = -EPERM;
1472 int pos;
1473 int len;
1474 static unsigned int tomoyo_serial;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001475 struct tomoyo_query *entry = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001476 bool quota_exceeded = false;
1477 char *header;
1478 switch (r->mode) {
1479 char *buffer;
1480 case TOMOYO_CONFIG_LEARNING:
1481 if (!tomoyo_domain_quota_is_ok(r))
1482 return 0;
1483 va_start(args, fmt);
1484 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1485 va_end(args);
1486 buffer = kmalloc(len, GFP_NOFS);
1487 if (!buffer)
1488 return 0;
1489 va_start(args, fmt);
1490 vsnprintf(buffer, len - 1, fmt, args);
1491 va_end(args);
1492 tomoyo_normalize_line(buffer);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001493 tomoyo_write_domain2(buffer, r->domain, false);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001494 kfree(buffer);
1495 /* fall through */
1496 case TOMOYO_CONFIG_PERMISSIVE:
1497 return 0;
1498 }
1499 if (!r->domain)
1500 r->domain = tomoyo_domain();
1501 if (!atomic_read(&tomoyo_query_observers))
1502 return -EPERM;
1503 va_start(args, fmt);
1504 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1505 va_end(args);
1506 header = tomoyo_init_audit_log(&len, r);
1507 if (!header)
1508 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001509 entry = kzalloc(sizeof(*entry), GFP_NOFS);
1510 if (!entry)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001511 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001512 entry->query = kzalloc(len, GFP_NOFS);
1513 if (!entry->query)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001514 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001515 len = ksize(entry->query);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001516 spin_lock(&tomoyo_query_list_lock);
1517 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001518 sizeof(*entry) >= tomoyo_quota_for_query) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001519 quota_exceeded = true;
1520 } else {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001521 tomoyo_query_memory_size += len + sizeof(*entry);
1522 entry->serial = tomoyo_serial++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001523 }
1524 spin_unlock(&tomoyo_query_list_lock);
1525 if (quota_exceeded)
1526 goto out;
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001527 pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1528 entry->serial, r->retry, header);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001529 kfree(header);
1530 header = NULL;
1531 va_start(args, fmt);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001532 vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1533 entry->query_len = strlen(entry->query) + 1;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001534 va_end(args);
1535 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001536 list_add_tail(&entry->list, &tomoyo_query_list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001537 spin_unlock(&tomoyo_query_list_lock);
1538 /* Give 10 seconds for supervisor's opinion. */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001539 for (entry->timer = 0;
1540 atomic_read(&tomoyo_query_observers) && entry->timer < 100;
1541 entry->timer++) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001542 wake_up(&tomoyo_query_wait);
1543 set_current_state(TASK_INTERRUPTIBLE);
1544 schedule_timeout(HZ / 10);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001545 if (entry->answer)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001546 break;
1547 }
1548 spin_lock(&tomoyo_query_list_lock);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001549 list_del(&entry->list);
1550 tomoyo_query_memory_size -= len + sizeof(*entry);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001551 spin_unlock(&tomoyo_query_list_lock);
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001552 switch (entry->answer) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001553 case 3: /* Asked to retry by administrator. */
1554 error = TOMOYO_RETRY_REQUEST;
1555 r->retry++;
1556 break;
1557 case 1:
1558 /* Granted by administrator. */
1559 error = 0;
1560 break;
1561 case 0:
1562 /* Timed out. */
1563 break;
1564 default:
1565 /* Rejected by administrator. */
1566 break;
1567 }
1568 out:
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001569 if (entry)
1570 kfree(entry->query);
1571 kfree(entry);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001572 kfree(header);
1573 return error;
1574}
1575
1576/**
1577 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1578 *
1579 * @file: Pointer to "struct file".
1580 * @wait: Pointer to "poll_table".
1581 *
1582 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1583 *
1584 * Waits for access requests which violated policy in enforcing mode.
1585 */
1586static int tomoyo_poll_query(struct file *file, poll_table *wait)
1587{
1588 struct list_head *tmp;
1589 bool found = false;
1590 u8 i;
1591 for (i = 0; i < 2; i++) {
1592 spin_lock(&tomoyo_query_list_lock);
1593 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001594 struct tomoyo_query *ptr =
1595 list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001596 if (ptr->answer)
1597 continue;
1598 found = true;
1599 break;
1600 }
1601 spin_unlock(&tomoyo_query_list_lock);
1602 if (found)
1603 return POLLIN | POLLRDNORM;
1604 if (i)
1605 break;
1606 poll_wait(file, &tomoyo_query_wait, wait);
1607 }
1608 return 0;
1609}
1610
1611/**
1612 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1613 *
1614 * @head: Pointer to "struct tomoyo_io_buffer".
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001615 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001616static void tomoyo_read_query(struct tomoyo_io_buffer *head)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001617{
1618 struct list_head *tmp;
1619 int pos = 0;
1620 int len = 0;
1621 char *buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001622 if (head->r.w_pos)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001623 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001624 if (head->read_buf) {
1625 kfree(head->read_buf);
1626 head->read_buf = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001627 }
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 len = ptr->query_len;
1636 break;
1637 }
1638 spin_unlock(&tomoyo_query_list_lock);
1639 if (!len) {
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001640 head->r.query_index = 0;
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001641 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001642 }
1643 buf = kzalloc(len, GFP_NOFS);
1644 if (!buf)
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001645 return;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001646 pos = 0;
1647 spin_lock(&tomoyo_query_list_lock);
1648 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001649 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001650 if (ptr->answer)
1651 continue;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001652 if (pos++ != head->r.query_index)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001653 continue;
1654 /*
1655 * Some query can be skipped because tomoyo_query_list
1656 * can change, but I don't care.
1657 */
1658 if (len == ptr->query_len)
1659 memmove(buf, ptr->query, len);
1660 break;
1661 }
1662 spin_unlock(&tomoyo_query_list_lock);
1663 if (buf[0]) {
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001664 head->read_buf = buf;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001665 head->r.w[head->r.w_pos++] = buf;
1666 head->r.query_index++;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001667 } else {
1668 kfree(buf);
1669 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001670}
1671
1672/**
1673 * tomoyo_write_answer - Write the supervisor's decision.
1674 *
1675 * @head: Pointer to "struct tomoyo_io_buffer".
1676 *
1677 * Returns 0 on success, -EINVAL otherwise.
1678 */
1679static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1680{
1681 char *data = head->write_buf;
1682 struct list_head *tmp;
1683 unsigned int serial;
1684 unsigned int answer;
1685 spin_lock(&tomoyo_query_list_lock);
1686 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001687 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001688 ptr->timer = 0;
1689 }
1690 spin_unlock(&tomoyo_query_list_lock);
1691 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1692 return -EINVAL;
1693 spin_lock(&tomoyo_query_list_lock);
1694 list_for_each(tmp, &tomoyo_query_list) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001695 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001696 if (ptr->serial != serial)
1697 continue;
1698 if (!ptr->answer)
1699 ptr->answer = answer;
1700 break;
1701 }
1702 spin_unlock(&tomoyo_query_list_lock);
1703 return 0;
1704}
1705
1706/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001707 * tomoyo_read_version: Get version.
1708 *
1709 * @head: Pointer to "struct tomoyo_io_buffer".
1710 *
1711 * Returns version information.
1712 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001713static void tomoyo_read_version(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001714{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001715 if (!head->r.eof) {
Tetsuo Handae6f6a4c2010-07-27 17:17:06 +09001716 tomoyo_io_printf(head, "2.3.0");
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001717 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001718 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001719}
1720
1721/**
1722 * tomoyo_read_self_domain - Get the current process's domainname.
1723 *
1724 * @head: Pointer to "struct tomoyo_io_buffer".
1725 *
1726 * Returns the current process's domainname.
1727 */
Tetsuo Handa8fbe71f2010-06-16 16:29:59 +09001728static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
Kentaro Takeda95908372009-02-05 17:18:13 +09001729{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001730 if (!head->r.eof) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001731 /*
1732 * tomoyo_domain()->domainname != NULL
1733 * because every process belongs to a domain and
1734 * the domain's name cannot be NULL.
1735 */
1736 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001737 head->r.eof = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001738 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001739}
1740
1741/**
1742 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1743 *
1744 * @type: Type of interface.
1745 * @file: Pointer to "struct file".
1746 *
1747 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001748 *
1749 * Caller acquires tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001750 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001751int tomoyo_open_control(const u8 type, struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09001752{
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001753 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001754
1755 if (!head)
1756 return -ENOMEM;
1757 mutex_init(&head->io_sem);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001758 head->type = type;
Kentaro Takeda95908372009-02-05 17:18:13 +09001759 switch (type) {
1760 case TOMOYO_DOMAINPOLICY:
1761 /* /sys/kernel/security/tomoyo/domain_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001762 head->write = tomoyo_write_domain;
1763 head->read = tomoyo_read_domain;
Kentaro Takeda95908372009-02-05 17:18:13 +09001764 break;
1765 case TOMOYO_EXCEPTIONPOLICY:
1766 /* /sys/kernel/security/tomoyo/exception_policy */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001767 head->write = tomoyo_write_exception;
1768 head->read = tomoyo_read_exception;
Kentaro Takeda95908372009-02-05 17:18:13 +09001769 break;
1770 case TOMOYO_SELFDOMAIN:
1771 /* /sys/kernel/security/tomoyo/self_domain */
1772 head->read = tomoyo_read_self_domain;
1773 break;
1774 case TOMOYO_DOMAIN_STATUS:
1775 /* /sys/kernel/security/tomoyo/.domain_status */
1776 head->write = tomoyo_write_domain_profile;
1777 head->read = tomoyo_read_domain_profile;
1778 break;
1779 case TOMOYO_PROCESS_STATUS:
1780 /* /sys/kernel/security/tomoyo/.process_status */
1781 head->write = tomoyo_write_pid;
1782 head->read = tomoyo_read_pid;
1783 break;
1784 case TOMOYO_VERSION:
1785 /* /sys/kernel/security/tomoyo/version */
1786 head->read = tomoyo_read_version;
1787 head->readbuf_size = 128;
1788 break;
1789 case TOMOYO_MEMINFO:
1790 /* /sys/kernel/security/tomoyo/meminfo */
1791 head->write = tomoyo_write_memory_quota;
1792 head->read = tomoyo_read_memory_counter;
1793 head->readbuf_size = 512;
1794 break;
1795 case TOMOYO_PROFILE:
1796 /* /sys/kernel/security/tomoyo/profile */
1797 head->write = tomoyo_write_profile;
1798 head->read = tomoyo_read_profile;
1799 break;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001800 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1801 head->poll = tomoyo_poll_query;
1802 head->write = tomoyo_write_answer;
1803 head->read = tomoyo_read_query;
1804 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001805 case TOMOYO_MANAGER:
1806 /* /sys/kernel/security/tomoyo/manager */
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001807 head->write = tomoyo_write_manager;
1808 head->read = tomoyo_read_manager;
Kentaro Takeda95908372009-02-05 17:18:13 +09001809 break;
1810 }
1811 if (!(file->f_mode & FMODE_READ)) {
1812 /*
1813 * No need to allocate read_buf since it is not opened
1814 * for reading.
1815 */
1816 head->read = NULL;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001817 head->poll = NULL;
1818 } else if (!head->poll) {
1819 /* Don't allocate read_buf for poll() access. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001820 if (!head->readbuf_size)
1821 head->readbuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001822 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001823 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001824 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001825 return -ENOMEM;
1826 }
1827 }
1828 if (!(file->f_mode & FMODE_WRITE)) {
1829 /*
1830 * No need to allocate write_buf since it is not opened
1831 * for writing.
1832 */
1833 head->write = NULL;
1834 } else if (head->write) {
1835 head->writebuf_size = 4096 * 2;
Tetsuo Handa4e5d6f72010-04-28 14:17:42 +09001836 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
Kentaro Takeda95908372009-02-05 17:18:13 +09001837 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001838 kfree(head->read_buf);
1839 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001840 return -ENOMEM;
1841 }
1842 }
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001843 if (type != TOMOYO_QUERY)
1844 head->reader_idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09001845 file->private_data = head;
1846 /*
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001847 * If the file is /sys/kernel/security/tomoyo/query , increment the
1848 * observer counter.
1849 * The obserber counter is used by tomoyo_supervisor() to see if
1850 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1851 */
Tetsuo Handa7c759642011-06-26 23:15:31 +09001852 if (type == TOMOYO_QUERY)
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001853 atomic_inc(&tomoyo_query_observers);
Kentaro Takeda95908372009-02-05 17:18:13 +09001854 return 0;
1855}
1856
1857/**
Tetsuo Handa0849e3b2010-06-25 12:22:09 +09001858 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1859 *
1860 * @file: Pointer to "struct file".
1861 * @wait: Pointer to "poll_table".
1862 *
1863 * Waits for read readiness.
1864 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1865 */
1866int tomoyo_poll_control(struct file *file, poll_table *wait)
1867{
1868 struct tomoyo_io_buffer *head = file->private_data;
1869 if (!head->poll)
1870 return -ENOSYS;
1871 return head->poll(file, wait);
1872}
1873
1874/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001875 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1876 *
1877 * @file: Pointer to "struct file".
1878 * @buffer: Poiner to buffer to write to.
1879 * @buffer_len: Size of @buffer.
1880 *
1881 * Returns bytes read on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001882 *
1883 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001884 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001885int tomoyo_read_control(struct file *file, char __user *buffer,
1886 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001887{
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001888 int len;
Kentaro Takeda95908372009-02-05 17:18:13 +09001889 struct tomoyo_io_buffer *head = file->private_data;
Kentaro Takeda95908372009-02-05 17:18:13 +09001890
1891 if (!head->read)
1892 return -ENOSYS;
1893 if (mutex_lock_interruptible(&head->io_sem))
1894 return -EINTR;
Tetsuo Handaf23571e2010-06-24 14:57:16 +09001895 head->read_user_buf = buffer;
1896 head->read_user_buf_avail = buffer_len;
1897 if (tomoyo_flush(head))
1898 /* Call the policy handler. */
1899 head->read(head);
1900 tomoyo_flush(head);
1901 len = head->read_user_buf - buffer;
Kentaro Takeda95908372009-02-05 17:18:13 +09001902 mutex_unlock(&head->io_sem);
1903 return len;
1904}
1905
1906/**
1907 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1908 *
1909 * @file: Pointer to "struct file".
1910 * @buffer: Pointer to buffer to read from.
1911 * @buffer_len: Size of @buffer.
1912 *
1913 * Returns @buffer_len on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001914 *
1915 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001916 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001917int tomoyo_write_control(struct file *file, const char __user *buffer,
1918 const int buffer_len)
Kentaro Takeda95908372009-02-05 17:18:13 +09001919{
1920 struct tomoyo_io_buffer *head = file->private_data;
1921 int error = buffer_len;
1922 int avail_len = buffer_len;
1923 char *cp0 = head->write_buf;
1924
1925 if (!head->write)
1926 return -ENOSYS;
1927 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1928 return -EFAULT;
1929 /* Don't allow updating policies by non manager programs. */
1930 if (head->write != tomoyo_write_pid &&
Tetsuo Handae2bf6902010-06-25 11:16:00 +09001931 head->write != tomoyo_write_domain && !tomoyo_manager())
Kentaro Takeda95908372009-02-05 17:18:13 +09001932 return -EPERM;
1933 if (mutex_lock_interruptible(&head->io_sem))
1934 return -EINTR;
1935 /* Read a line and dispatch it to the policy handler. */
1936 while (avail_len > 0) {
1937 char c;
1938 if (head->write_avail >= head->writebuf_size - 1) {
1939 error = -ENOMEM;
1940 break;
1941 } else if (get_user(c, buffer)) {
1942 error = -EFAULT;
1943 break;
1944 }
1945 buffer++;
1946 avail_len--;
1947 cp0[head->write_avail++] = c;
1948 if (c != '\n')
1949 continue;
1950 cp0[head->write_avail - 1] = '\0';
1951 head->write_avail = 0;
1952 tomoyo_normalize_line(cp0);
1953 head->write(head);
1954 }
1955 mutex_unlock(&head->io_sem);
1956 return error;
1957}
1958
1959/**
1960 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1961 *
1962 * @file: Pointer to "struct file".
1963 *
1964 * Releases memory and returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001965 *
1966 * Caller looses tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001967 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001968int tomoyo_close_control(struct file *file)
Kentaro Takeda95908372009-02-05 17:18:13 +09001969{
1970 struct tomoyo_io_buffer *head = file->private_data;
Tetsuo Handa847b1732010-02-11 09:43:54 +09001971 const bool is_write = !!head->write_buf;
Kentaro Takeda95908372009-02-05 17:18:13 +09001972
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +09001973 /*
1974 * If the file is /sys/kernel/security/tomoyo/query , decrement the
1975 * observer counter.
1976 */
1977 if (head->type == TOMOYO_QUERY)
1978 atomic_dec(&tomoyo_query_observers);
1979 else
1980 tomoyo_read_unlock(head->reader_idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09001981 /* Release memory used for policy I/O. */
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001982 kfree(head->read_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09001983 head->read_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001984 kfree(head->write_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09001985 head->write_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001986 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001987 head = NULL;
1988 file->private_data = NULL;
Tetsuo Handa847b1732010-02-11 09:43:54 +09001989 if (is_write)
1990 tomoyo_run_gc();
Kentaro Takeda95908372009-02-05 17:18:13 +09001991 return 0;
1992}
1993
1994/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001995 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
Kentaro Takeda95908372009-02-05 17:18:13 +09001996 */
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001997void tomoyo_check_profile(void)
Kentaro Takeda95908372009-02-05 17:18:13 +09001998{
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001999 struct tomoyo_domain_info *domain;
2000 const int idx = tomoyo_read_lock();
2001 tomoyo_policy_loaded = true;
2002 /* Check all profiles currently assigned to domains are defined. */
2003 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2004 const u8 profile = domain->profile;
2005 if (tomoyo_profile_ptr[profile])
2006 continue;
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002007 printk(KERN_ERR "You need to define profile %u before using it.\n",
2008 profile);
2009 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2010 "for more information.\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002011 panic("Profile %u (used by '%s') not defined.\n",
2012 profile, domain->domainname->name);
2013 }
2014 tomoyo_read_unlock(idx);
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002015 if (tomoyo_profile_version != 20090903) {
2016 printk(KERN_ERR "You need to install userland programs for "
2017 "TOMOYO 2.3 and initialize policy configuration.\n");
2018 printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.3/ "
2019 "for more information.\n");
Tetsuo Handa57c25902010-06-03 20:38:44 +09002020 panic("Profile version %u is not supported.\n",
2021 tomoyo_profile_version);
Tetsuo Handa9f1c1d42010-10-08 14:43:22 +09002022 }
Tetsuo Handae6f6a4c2010-07-27 17:17:06 +09002023 printk(KERN_INFO "TOMOYO: 2.3.0\n");
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002024 printk(KERN_INFO "Mandatory Access Control activated.\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09002025}