]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/common.c
TOMOYO: Change list iterator.
[net-next-2.6.git] / security / tomoyo / common.c
CommitLineData
9590837b
KT
1/*
2 * security/tomoyo/common.c
3 *
4 * Common functions for TOMOYO.
5 *
c3ef1500 6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9590837b
KT
7 */
8
9#include <linux/uaccess.h>
5a0e3ad6 10#include <linux/slab.h>
9590837b 11#include <linux/security.h>
9590837b 12#include "common.h"
9590837b 13
57c2590f
TH
14static 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
9590837b
KT
30/* String table for functionality that takes 4 modes. */
31static const char *tomoyo_mode_4[4] = {
32 "disabled", "learning", "permissive", "enforcing"
33};
9590837b 34
57c2590f
TH
35/* 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",
42 [TOMOYO_MAC_FILE_MKDIR] = "file::mkdir",
43 [TOMOYO_MAC_FILE_RMDIR] = "file::rmdir",
44 [TOMOYO_MAC_FILE_MKFIFO] = "file::mkfifo",
45 [TOMOYO_MAC_FILE_MKSOCK] = "file::mksock",
46 [TOMOYO_MAC_FILE_TRUNCATE] = "file::truncate",
47 [TOMOYO_MAC_FILE_SYMLINK] = "file::symlink",
48 [TOMOYO_MAC_FILE_REWRITE] = "file::rewrite",
49 [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",
9590837b
KT
62};
63
9590837b
KT
64/* Permit policy management by non-root user? */
65static bool tomoyo_manage_by_non_root;
66
67/* Utility functions. */
68
57c2590f
TH
69/**
70 * 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
7762fbff
TH
79/**
80 * tomoyo_print_name_union - Print a tomoyo_name_union.
81 *
82 * @head: Pointer to "struct tomoyo_io_buffer".
83 * @ptr: Pointer to "struct tomoyo_name_union".
84 *
85 * Returns true on success, false otherwise.
86 */
87static bool tomoyo_print_name_union(struct tomoyo_io_buffer *head,
88 const struct tomoyo_name_union *ptr)
89{
90 int pos = head->read_avail;
91 if (pos && head->read_buf[pos - 1] == ' ')
92 head->read_avail--;
93 if (ptr->is_group)
94 return tomoyo_io_printf(head, " @%s",
95 ptr->group->group_name->name);
96 return tomoyo_io_printf(head, " %s", ptr->filename->name);
97}
98
4c3e9e2d
TH
99/**
100 * tomoyo_print_number_union - Print a tomoyo_number_union.
101 *
102 * @head: Pointer to "struct tomoyo_io_buffer".
103 * @ptr: Pointer to "struct tomoyo_number_union".
104 *
105 * Returns true on success, false otherwise.
106 */
107bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
108 const struct tomoyo_number_union *ptr)
109{
110 unsigned long min;
111 unsigned long max;
112 u8 min_type;
113 u8 max_type;
114 if (!tomoyo_io_printf(head, " "))
115 return false;
116 if (ptr->is_group)
117 return tomoyo_io_printf(head, "@%s",
118 ptr->group->group_name->name);
119 min_type = ptr->min_type;
120 max_type = ptr->max_type;
121 min = ptr->values[0];
122 max = ptr->values[1];
123 switch (min_type) {
124 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
125 if (!tomoyo_io_printf(head, "0x%lX", min))
126 return false;
127 break;
128 case TOMOYO_VALUE_TYPE_OCTAL:
129 if (!tomoyo_io_printf(head, "0%lo", min))
130 return false;
131 break;
132 default:
133 if (!tomoyo_io_printf(head, "%lu", min))
134 return false;
135 break;
136 }
137 if (min == max && min_type == max_type)
138 return true;
139 switch (max_type) {
140 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
141 return tomoyo_io_printf(head, "-0x%lX", max);
142 case TOMOYO_VALUE_TYPE_OCTAL:
143 return tomoyo_io_printf(head, "-0%lo", max);
144 default:
145 return tomoyo_io_printf(head, "-%lu", max);
146 }
147}
148
9590837b
KT
149/**
150 * tomoyo_io_printf - Transactional 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 *
155 * Returns true if output was written, false otherwise.
156 *
157 * The snprintf() will truncate, but tomoyo_io_printf() won't.
158 */
159bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
160{
161 va_list args;
162 int len;
163 int pos = head->read_avail;
164 int size = head->readbuf_size - pos;
165
166 if (size <= 0)
167 return false;
168 va_start(args, fmt);
169 len = vsnprintf(head->read_buf + pos, size, fmt, args);
170 va_end(args);
171 if (pos + len >= head->readbuf_size)
172 return false;
173 head->read_avail += len;
174 return true;
175}
176
9590837b
KT
177/**
178 * tomoyo_find_or_assign_new_profile - Create a new profile.
179 *
180 * @profile: Profile number to create.
181 *
182 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
183 */
57c2590f
TH
184static struct tomoyo_profile *tomoyo_find_or_assign_new_profile
185(const unsigned int profile)
9590837b 186{
57c2590f
TH
187 struct tomoyo_profile *ptr;
188 struct tomoyo_profile *entry;
9590837b
KT
189 if (profile >= TOMOYO_MAX_PROFILES)
190 return NULL;
9590837b
KT
191 ptr = tomoyo_profile_ptr[profile];
192 if (ptr)
57c2590f
TH
193 return ptr;
194 entry = kzalloc(sizeof(*entry), GFP_NOFS);
195 if (mutex_lock_interruptible(&tomoyo_policy_lock))
196 goto out;
197 ptr = tomoyo_profile_ptr[profile];
198 if (!ptr && tomoyo_memory_ok(entry)) {
199 ptr = entry;
200 ptr->learning = &tomoyo_default_profile.preference;
201 ptr->permissive = &tomoyo_default_profile.preference;
202 ptr->enforcing = &tomoyo_default_profile.preference;
203 ptr->default_config = TOMOYO_CONFIG_DISABLED;
204 memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
205 sizeof(ptr->config));
206 mb(); /* Avoid out-of-order execution. */
207 tomoyo_profile_ptr[profile] = ptr;
208 entry = NULL;
cd7bec6a 209 }
29282381 210 mutex_unlock(&tomoyo_policy_lock);
57c2590f
TH
211 out:
212 kfree(entry);
9590837b
KT
213 return ptr;
214}
215
216/**
57c2590f
TH
217 * tomoyo_profile - Find a profile.
218 *
219 * @profile: Profile number to find.
220 *
221 * Returns pointer to "struct tomoyo_profile".
222 */
223struct tomoyo_profile *tomoyo_profile(const u8 profile)
224{
225 struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
226 if (!tomoyo_policy_loaded)
227 return &tomoyo_default_profile;
228 BUG_ON(!ptr);
229 return ptr;
230}
231
232/**
233 * tomoyo_write_profile - Write profile table.
9590837b
KT
234 *
235 * @head: Pointer to "struct tomoyo_io_buffer".
236 *
237 * Returns 0 on success, negative value otherwise.
238 */
239static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
240{
241 char *data = head->write_buf;
242 unsigned int i;
57c2590f
TH
243 int value;
244 int mode;
245 u8 config;
246 bool use_default = false;
9590837b
KT
247 char *cp;
248 struct tomoyo_profile *profile;
57c2590f
TH
249 if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
250 return 0;
251 i = simple_strtoul(data, &cp, 10);
252 if (data == cp) {
253 profile = &tomoyo_default_profile;
254 } else {
255 if (*cp != '-')
256 return -EINVAL;
9590837b 257 data = cp + 1;
57c2590f
TH
258 profile = tomoyo_find_or_assign_new_profile(i);
259 if (!profile)
260 return -EINVAL;
261 }
9590837b
KT
262 cp = strchr(data, '=');
263 if (!cp)
264 return -EINVAL;
57c2590f
TH
265 *cp++ = '\0';
266 if (profile != &tomoyo_default_profile)
267 use_default = strstr(cp, "use_default") != NULL;
268 if (strstr(cp, "verbose=yes"))
269 value = 1;
270 else if (strstr(cp, "verbose=no"))
271 value = 0;
272 else
273 value = -1;
274 if (!strcmp(data, "PREFERENCE::enforcing")) {
275 if (use_default) {
276 profile->enforcing = &tomoyo_default_profile.preference;
277 return 0;
278 }
279 profile->enforcing = &profile->preference;
280 if (value >= 0)
281 profile->preference.enforcing_verbose = value;
282 return 0;
283 }
284 if (!strcmp(data, "PREFERENCE::permissive")) {
285 if (use_default) {
286 profile->permissive = &tomoyo_default_profile.preference;
287 return 0;
288 }
289 profile->permissive = &profile->preference;
290 if (value >= 0)
291 profile->preference.permissive_verbose = value;
292 return 0;
293 }
294 if (!strcmp(data, "PREFERENCE::learning")) {
295 char *cp2;
296 if (use_default) {
297 profile->learning = &tomoyo_default_profile.preference;
298 return 0;
299 }
300 profile->learning = &profile->preference;
301 if (value >= 0)
302 profile->preference.learning_verbose = value;
303 cp2 = strstr(cp, "max_entry=");
304 if (cp2)
305 sscanf(cp2 + 10, "%u",
306 &profile->preference.learning_max_entry);
307 return 0;
308 }
309 if (profile == &tomoyo_default_profile)
310 return -EINVAL;
9590837b 311 if (!strcmp(data, "COMMENT")) {
bf24fb01 312 const struct tomoyo_path_info *old_comment = profile->comment;
57c2590f 313 profile->comment = tomoyo_get_name(cp);
bf24fb01 314 tomoyo_put_name(old_comment);
9590837b
KT
315 return 0;
316 }
57c2590f
TH
317 if (!strcmp(data, "CONFIG")) {
318 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
319 config = profile->default_config;
320 } else if (tomoyo_str_starts(&data, "CONFIG::")) {
321 config = 0;
322 for (i = 0; i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
323 if (strcmp(data, tomoyo_mac_keywords[i]))
324 continue;
325 config = profile->config[i];
326 break;
9590837b 327 }
57c2590f
TH
328 if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
329 return -EINVAL;
330 } else {
331 return -EINVAL;
9590837b 332 }
57c2590f
TH
333 if (use_default) {
334 config = TOMOYO_CONFIG_USE_DEFAULT;
335 } else {
336 for (mode = 3; mode >= 0; mode--)
337 if (strstr(cp, tomoyo_mode_4[mode]))
338 /*
339 * Update lower 3 bits in order to distinguish
340 * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
341 */
342 config = (config & ~7) | mode;
343 }
344 if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
345 profile->config[i] = config;
346 else if (config != TOMOYO_CONFIG_USE_DEFAULT)
347 profile->default_config = config;
348 return 0;
9590837b
KT
349}
350
351/**
57c2590f 352 * tomoyo_read_profile - Read profile table.
9590837b
KT
353 *
354 * @head: Pointer to "struct tomoyo_io_buffer".
9590837b 355 */
8fbe71f0 356static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
9590837b 357{
57c2590f 358 int index;
9590837b 359 if (head->read_eof)
8fbe71f0 360 return;
57c2590f
TH
361 if (head->read_bit)
362 goto body;
363 tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
364 tomoyo_io_printf(head, "PREFERENCE::learning={ verbose=%s "
365 "max_entry=%u }\n",
366 tomoyo_yesno(tomoyo_default_profile.preference.
367 learning_verbose),
368 tomoyo_default_profile.preference.learning_max_entry);
369 tomoyo_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",
370 tomoyo_yesno(tomoyo_default_profile.preference.
371 permissive_verbose));
372 tomoyo_io_printf(head, "PREFERENCE::enforcing={ verbose=%s }\n",
373 tomoyo_yesno(tomoyo_default_profile.preference.
374 enforcing_verbose));
375 head->read_bit = 1;
376 body:
377 for (index = head->read_step; index < TOMOYO_MAX_PROFILES; index++) {
378 bool done;
379 u8 config;
380 int i;
381 int pos;
9590837b
KT
382 const struct tomoyo_profile *profile
383 = tomoyo_profile_ptr[index];
57c2590f
TH
384 const struct tomoyo_path_info *comment;
385 head->read_step = index;
9590837b
KT
386 if (!profile)
387 continue;
57c2590f
TH
388 pos = head->read_avail;
389 comment = profile->comment;
390 done = tomoyo_io_printf(head, "%u-COMMENT=%s\n", index,
391 comment ? comment->name : "");
392 if (!done)
393 goto out;
394 config = profile->default_config;
395 if (!tomoyo_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,
396 tomoyo_mode_4[config & 3]))
397 goto out;
398 for (i = 0; i < TOMOYO_MAX_MAC_INDEX +
399 TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
400 config = profile->config[i];
401 if (config == TOMOYO_CONFIG_USE_DEFAULT)
402 continue;
403 if (!tomoyo_io_printf(head,
404 "%u-CONFIG::%s={ mode=%s }\n",
405 index, tomoyo_mac_keywords[i],
406 tomoyo_mode_4[config & 3]))
407 goto out;
9590837b 408 }
57c2590f
TH
409 if (profile->learning != &tomoyo_default_profile.preference &&
410 !tomoyo_io_printf(head, "%u-PREFERENCE::learning={ "
411 "verbose=%s max_entry=%u }\n", index,
412 tomoyo_yesno(profile->preference.
413 learning_verbose),
414 profile->preference.learning_max_entry))
415 goto out;
416 if (profile->permissive != &tomoyo_default_profile.preference
417 && !tomoyo_io_printf(head, "%u-PREFERENCE::permissive={ "
418 "verbose=%s }\n", index,
419 tomoyo_yesno(profile->preference.
420 permissive_verbose)))
421 goto out;
422 if (profile->enforcing != &tomoyo_default_profile.preference &&
423 !tomoyo_io_printf(head, "%u-PREFERENCE::enforcing={ "
424 "verbose=%s }\n", index,
425 tomoyo_yesno(profile->preference.
426 enforcing_verbose)))
427 goto out;
428 continue;
429 out:
430 head->read_avail = pos;
431 break;
9590837b 432 }
57c2590f 433 if (index == TOMOYO_MAX_PROFILES)
9590837b 434 head->read_eof = true;
9590837b
KT
435}
436
36f5e1ff
TH
437static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head *a,
438 const struct tomoyo_acl_head *b)
439{
440 return container_of(a, struct tomoyo_policy_manager_entry, head)
441 ->manager ==
442 container_of(b, struct tomoyo_policy_manager_entry, head)
443 ->manager;
444}
445
9590837b
KT
446/**
447 * tomoyo_update_manager_entry - Add a manager entry.
448 *
449 * @manager: The path to manager or the domainnamme.
450 * @is_delete: True if it is a delete request.
451 *
452 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
453 *
454 * Caller holds tomoyo_read_lock().
9590837b
KT
455 */
456static int tomoyo_update_manager_entry(const char *manager,
457 const bool is_delete)
458{
9e4b50e9 459 struct tomoyo_policy_manager_entry e = { };
36f5e1ff 460 int error;
9590837b 461
75093152
TH
462 if (tomoyo_domain_def(manager)) {
463 if (!tomoyo_correct_domain(manager))
9590837b 464 return -EINVAL;
9e4b50e9 465 e.is_domain = true;
9590837b 466 } else {
75093152 467 if (!tomoyo_correct_path(manager))
9590837b
KT
468 return -EINVAL;
469 }
9e4b50e9
TH
470 e.manager = tomoyo_get_name(manager);
471 if (!e.manager)
9590837b 472 return -ENOMEM;
36f5e1ff 473 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
a230f9e7 474 &tomoyo_policy_list[TOMOYO_ID_MANAGER],
36f5e1ff 475 tomoyo_same_manager_entry);
9e4b50e9 476 tomoyo_put_name(e.manager);
9590837b
KT
477 return error;
478}
479
480/**
481 * tomoyo_write_manager_policy - Write manager policy.
482 *
483 * @head: Pointer to "struct tomoyo_io_buffer".
484 *
485 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
486 *
487 * Caller holds tomoyo_read_lock().
9590837b
KT
488 */
489static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
490{
491 char *data = head->write_buf;
492 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
493
494 if (!strcmp(data, "manage_by_non_root")) {
495 tomoyo_manage_by_non_root = !is_delete;
496 return 0;
497 }
498 return tomoyo_update_manager_entry(data, is_delete);
499}
500
501/**
502 * tomoyo_read_manager_policy - Read manager policy.
503 *
504 * @head: Pointer to "struct tomoyo_io_buffer".
505 *
fdb8ebb7 506 * Caller holds tomoyo_read_lock().
9590837b 507 */
8fbe71f0 508static void tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
9590837b 509{
9590837b
KT
510 bool done = true;
511
512 if (head->read_eof)
8fbe71f0 513 return;
475e6fa3 514 list_for_each_cookie(head->read_var2,
a230f9e7 515 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
475e6fa3
TH
516 struct tomoyo_policy_manager_entry *ptr =
517 list_entry(head->read_var2, typeof(*ptr), head.list);
82e0f001 518 if (ptr->head.is_deleted)
9590837b 519 continue;
7d2948b1
TH
520 done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
521 if (!done)
9590837b 522 break;
9590837b 523 }
9590837b 524 head->read_eof = done;
9590837b
KT
525}
526
527/**
75093152 528 * tomoyo_policy_manager - Check whether the current process is a policy manager.
9590837b
KT
529 *
530 * Returns true if the current process is permitted to modify policy
531 * via /sys/kernel/security/tomoyo/ interface.
fdb8ebb7
TH
532 *
533 * Caller holds tomoyo_read_lock().
9590837b 534 */
75093152 535static bool tomoyo_policy_manager(void)
9590837b
KT
536{
537 struct tomoyo_policy_manager_entry *ptr;
538 const char *exe;
539 const struct task_struct *task = current;
540 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
541 bool found = false;
542
543 if (!tomoyo_policy_loaded)
544 return true;
545 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
546 return false;
a230f9e7
TH
547 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
548 head.list) {
82e0f001 549 if (!ptr->head.is_deleted && ptr->is_domain
9590837b
KT
550 && !tomoyo_pathcmp(domainname, ptr->manager)) {
551 found = true;
552 break;
553 }
554 }
9590837b
KT
555 if (found)
556 return true;
557 exe = tomoyo_get_exe();
558 if (!exe)
559 return false;
a230f9e7
TH
560 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
561 head.list) {
82e0f001 562 if (!ptr->head.is_deleted && !ptr->is_domain
9590837b
KT
563 && !strcmp(exe, ptr->manager->name)) {
564 found = true;
565 break;
566 }
567 }
9590837b
KT
568 if (!found) { /* Reduce error messages. */
569 static pid_t last_pid;
570 const pid_t pid = current->pid;
571 if (last_pid != pid) {
572 printk(KERN_WARNING "%s ( %s ) is not permitted to "
573 "update policies.\n", domainname->name, exe);
574 last_pid = pid;
575 }
576 }
8e2d39a1 577 kfree(exe);
9590837b
KT
578 return found;
579}
580
581/**
75093152 582 * tomoyo_select_one - Parse select command.
9590837b
KT
583 *
584 * @head: Pointer to "struct tomoyo_io_buffer".
585 * @data: String to parse.
586 *
587 * Returns true on success, false otherwise.
fdb8ebb7
TH
588 *
589 * Caller holds tomoyo_read_lock().
9590837b 590 */
475e6fa3 591static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
9590837b
KT
592{
593 unsigned int pid;
594 struct tomoyo_domain_info *domain = NULL;
9b244373 595 bool global_pid = false;
9590837b 596
9b244373
TH
597 if (sscanf(data, "pid=%u", &pid) == 1 ||
598 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9590837b 599 struct task_struct *p;
1fcdc7c5 600 rcu_read_lock();
9590837b 601 read_lock(&tasklist_lock);
9b244373
TH
602 if (global_pid)
603 p = find_task_by_pid_ns(pid, &init_pid_ns);
604 else
605 p = find_task_by_vpid(pid);
9590837b
KT
606 if (p)
607 domain = tomoyo_real_domain(p);
608 read_unlock(&tasklist_lock);
1fcdc7c5 609 rcu_read_unlock();
9590837b 610 } else if (!strncmp(data, "domain=", 7)) {
75093152 611 if (tomoyo_domain_def(data + 7))
9590837b 612 domain = tomoyo_find_domain(data + 7);
9590837b
KT
613 } else
614 return false;
615 head->write_var1 = domain;
616 /* Accessing read_buf is safe because head->io_sem is held. */
617 if (!head->read_buf)
618 return true; /* Do nothing if open(O_WRONLY). */
619 head->read_avail = 0;
620 tomoyo_io_printf(head, "# select %s\n", data);
621 head->read_single_domain = true;
622 head->read_eof = !domain;
475e6fa3
TH
623 head->read_var1 = &domain->list;
624 head->read_var2 = NULL;
625 head->read_bit = 0;
626 head->read_step = 0;
627 if (domain && domain->is_deleted)
628 tomoyo_io_printf(head, "# This is a deleted domain.\n");
9590837b
KT
629 return true;
630}
631
ccf135f5
TH
632/**
633 * tomoyo_delete_domain - Delete a domain.
634 *
635 * @domainname: The name of domain.
636 *
637 * Returns 0.
fdb8ebb7
TH
638 *
639 * Caller holds tomoyo_read_lock().
ccf135f5
TH
640 */
641static int tomoyo_delete_domain(char *domainname)
642{
643 struct tomoyo_domain_info *domain;
644 struct tomoyo_path_info name;
645
646 name.name = domainname;
647 tomoyo_fill_path_info(&name);
29282381
TH
648 if (mutex_lock_interruptible(&tomoyo_policy_lock))
649 return 0;
ccf135f5 650 /* Is there an active domain? */
fdb8ebb7 651 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
ccf135f5
TH
652 /* Never delete tomoyo_kernel_domain */
653 if (domain == &tomoyo_kernel_domain)
654 continue;
655 if (domain->is_deleted ||
656 tomoyo_pathcmp(domain->domainname, &name))
657 continue;
658 domain->is_deleted = true;
659 break;
660 }
f737d95d 661 mutex_unlock(&tomoyo_policy_lock);
ccf135f5
TH
662 return 0;
663}
664
17fcfbd9
TH
665/**
666 * tomoyo_write_domain_policy2 - Write domain policy.
667 *
668 * @head: Pointer to "struct tomoyo_io_buffer".
669 *
670 * Returns 0 on success, negative value otherwise.
671 *
672 * Caller holds tomoyo_read_lock().
673 */
674static int tomoyo_write_domain_policy2(char *data,
675 struct tomoyo_domain_info *domain,
676 const bool is_delete)
677{
678 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
679 return tomoyo_write_mount_policy(data, domain, is_delete);
680 return tomoyo_write_file_policy(data, domain, is_delete);
681}
682
9590837b
KT
683/**
684 * tomoyo_write_domain_policy - Write domain policy.
685 *
686 * @head: Pointer to "struct tomoyo_io_buffer".
687 *
688 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
689 *
690 * Caller holds tomoyo_read_lock().
9590837b
KT
691 */
692static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
693{
694 char *data = head->write_buf;
695 struct tomoyo_domain_info *domain = head->write_var1;
696 bool is_delete = false;
697 bool is_select = false;
9590837b
KT
698 unsigned int profile;
699
700 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
701 is_delete = true;
702 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
703 is_select = true;
75093152 704 if (is_select && tomoyo_select_one(head, data))
9590837b
KT
705 return 0;
706 /* Don't allow updating policies by non manager programs. */
75093152 707 if (!tomoyo_policy_manager())
9590837b 708 return -EPERM;
75093152 709 if (tomoyo_domain_def(data)) {
9590837b
KT
710 domain = NULL;
711 if (is_delete)
712 tomoyo_delete_domain(data);
fdb8ebb7 713 else if (is_select)
9590837b 714 domain = tomoyo_find_domain(data);
fdb8ebb7 715 else
9590837b
KT
716 domain = tomoyo_find_or_assign_new_domain(data, 0);
717 head->write_var1 = domain;
718 return 0;
719 }
720 if (!domain)
721 return -EINVAL;
722
723 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
724 && profile < TOMOYO_MAX_PROFILES) {
725 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
726 domain->profile = (u8) profile;
727 return 0;
728 }
729 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
ea13ddba 730 domain->ignore_global_allow_read = !is_delete;
9590837b
KT
731 return 0;
732 }
9b244373
TH
733 if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
734 domain->quota_warned = !is_delete;
735 return 0;
736 }
737 if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
738 domain->transition_failed = !is_delete;
739 return 0;
740 }
17fcfbd9 741 return tomoyo_write_domain_policy2(data, domain, is_delete);
9590837b
KT
742}
743
744/**
7ef61233 745 * tomoyo_print_path_acl - Print a single path ACL entry.
9590837b
KT
746 *
747 * @head: Pointer to "struct tomoyo_io_buffer".
7ef61233 748 * @ptr: Pointer to "struct tomoyo_path_acl".
9590837b
KT
749 *
750 * Returns true on success, false otherwise.
751 */
7ef61233
TH
752static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
753 struct tomoyo_path_acl *ptr)
9590837b
KT
754{
755 int pos;
756 u8 bit;
a1f9bb6a 757 const u16 perm = ptr->perm;
9590837b 758
7ef61233 759 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
9590837b
KT
760 if (!(perm & (1 << bit)))
761 continue;
762 /* Print "read/write" instead of "read" and "write". */
7ef61233
TH
763 if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
764 && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
9590837b 765 continue;
9590837b 766 pos = head->read_avail;
7762fbff 767 if (!tomoyo_io_printf(head, "allow_%s ",
71c28236 768 tomoyo_path_keyword[bit]) ||
7762fbff
TH
769 !tomoyo_print_name_union(head, &ptr->name) ||
770 !tomoyo_io_printf(head, "\n"))
9590837b
KT
771 goto out;
772 }
773 head->read_bit = 0;
774 return true;
775 out:
776 head->read_bit = bit;
777 head->read_avail = pos;
778 return false;
779}
780
781/**
7ef61233 782 * tomoyo_print_path2_acl - Print a double path ACL entry.
9590837b
KT
783 *
784 * @head: Pointer to "struct tomoyo_io_buffer".
7ef61233 785 * @ptr: Pointer to "struct tomoyo_path2_acl".
9590837b
KT
786 *
787 * Returns true on success, false otherwise.
788 */
7ef61233
TH
789static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
790 struct tomoyo_path2_acl *ptr)
9590837b
KT
791{
792 int pos;
9590837b
KT
793 const u8 perm = ptr->perm;
794 u8 bit;
795
7ef61233 796 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
9590837b
KT
797 if (!(perm & (1 << bit)))
798 continue;
9590837b 799 pos = head->read_avail;
7762fbff 800 if (!tomoyo_io_printf(head, "allow_%s ",
71c28236 801 tomoyo_path2_keyword[bit]) ||
7762fbff
TH
802 !tomoyo_print_name_union(head, &ptr->name1) ||
803 !tomoyo_print_name_union(head, &ptr->name2) ||
804 !tomoyo_io_printf(head, "\n"))
9590837b
KT
805 goto out;
806 }
807 head->read_bit = 0;
808 return true;
809 out:
810 head->read_bit = bit;
811 head->read_avail = pos;
812 return false;
813}
814
a1f9bb6a
TH
815/**
816 * tomoyo_print_path_number_acl - Print a path_number ACL entry.
817 *
818 * @head: Pointer to "struct tomoyo_io_buffer".
819 * @ptr: Pointer to "struct tomoyo_path_number_acl".
820 *
821 * Returns true on success, false otherwise.
822 */
823static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
824 struct tomoyo_path_number_acl *ptr)
825{
826 int pos;
827 u8 bit;
828 const u8 perm = ptr->perm;
829 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
830 bit++) {
831 if (!(perm & (1 << bit)))
832 continue;
833 pos = head->read_avail;
834 if (!tomoyo_io_printf(head, "allow_%s",
71c28236 835 tomoyo_path_number_keyword[bit]) ||
a1f9bb6a
TH
836 !tomoyo_print_name_union(head, &ptr->name) ||
837 !tomoyo_print_number_union(head, &ptr->number) ||
838 !tomoyo_io_printf(head, "\n"))
839 goto out;
840 }
841 head->read_bit = 0;
842 return true;
843 out:
844 head->read_bit = bit;
845 head->read_avail = pos;
846 return false;
847}
848
849/**
75093152 850 * tomoyo_print_mkdev_acl - Print a mkdev ACL entry.
a1f9bb6a
TH
851 *
852 * @head: Pointer to "struct tomoyo_io_buffer".
75093152 853 * @ptr: Pointer to "struct tomoyo_mkdev_acl".
a1f9bb6a
TH
854 *
855 * Returns true on success, false otherwise.
856 */
75093152
TH
857static bool tomoyo_print_mkdev_acl(struct tomoyo_io_buffer *head,
858 struct tomoyo_mkdev_acl *ptr)
a1f9bb6a
TH
859{
860 int pos;
861 u8 bit;
862 const u16 perm = ptr->perm;
75093152 863 for (bit = head->read_bit; bit < TOMOYO_MAX_MKDEV_OPERATION;
a1f9bb6a
TH
864 bit++) {
865 if (!(perm & (1 << bit)))
866 continue;
867 pos = head->read_avail;
868 if (!tomoyo_io_printf(head, "allow_%s",
71c28236 869 tomoyo_mkdev_keyword[bit]) ||
a1f9bb6a
TH
870 !tomoyo_print_name_union(head, &ptr->name) ||
871 !tomoyo_print_number_union(head, &ptr->mode) ||
872 !tomoyo_print_number_union(head, &ptr->major) ||
873 !tomoyo_print_number_union(head, &ptr->minor) ||
874 !tomoyo_io_printf(head, "\n"))
875 goto out;
876 }
877 head->read_bit = 0;
878 return true;
879 out:
880 head->read_bit = bit;
881 head->read_avail = pos;
882 return false;
883}
884
2106ccd9
TH
885/**
886 * tomoyo_print_mount_acl - Print a mount ACL entry.
887 *
888 * @head: Pointer to "struct tomoyo_io_buffer".
889 * @ptr: Pointer to "struct tomoyo_mount_acl".
890 *
891 * Returns true on success, false otherwise.
892 */
893static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
894 struct tomoyo_mount_acl *ptr)
895{
896 const int pos = head->read_avail;
897 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
898 !tomoyo_print_name_union(head, &ptr->dev_name) ||
899 !tomoyo_print_name_union(head, &ptr->dir_name) ||
900 !tomoyo_print_name_union(head, &ptr->fs_type) ||
901 !tomoyo_print_number_union(head, &ptr->flags) ||
902 !tomoyo_io_printf(head, "\n")) {
903 head->read_avail = pos;
904 return false;
905 }
906 return true;
907}
908
9590837b
KT
909/**
910 * tomoyo_print_entry - Print an ACL entry.
911 *
912 * @head: Pointer to "struct tomoyo_io_buffer".
913 * @ptr: Pointer to an ACL entry.
914 *
915 * Returns true on success, false otherwise.
916 */
917static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
918 struct tomoyo_acl_info *ptr)
919{
ea13ddba 920 const u8 acl_type = ptr->type;
9590837b 921
237ab459
TH
922 if (ptr->is_deleted)
923 return true;
7ef61233
TH
924 if (acl_type == TOMOYO_TYPE_PATH_ACL) {
925 struct tomoyo_path_acl *acl
926 = container_of(ptr, struct tomoyo_path_acl, head);
927 return tomoyo_print_path_acl(head, acl);
9590837b 928 }
7ef61233
TH
929 if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
930 struct tomoyo_path2_acl *acl
931 = container_of(ptr, struct tomoyo_path2_acl, head);
932 return tomoyo_print_path2_acl(head, acl);
9590837b 933 }
a1f9bb6a
TH
934 if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
935 struct tomoyo_path_number_acl *acl
936 = container_of(ptr, struct tomoyo_path_number_acl,
937 head);
938 return tomoyo_print_path_number_acl(head, acl);
939 }
75093152
TH
940 if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
941 struct tomoyo_mkdev_acl *acl
942 = container_of(ptr, struct tomoyo_mkdev_acl,
a1f9bb6a 943 head);
75093152 944 return tomoyo_print_mkdev_acl(head, acl);
a1f9bb6a 945 }
2106ccd9
TH
946 if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
947 struct tomoyo_mount_acl *acl
948 = container_of(ptr, struct tomoyo_mount_acl, head);
949 return tomoyo_print_mount_acl(head, acl);
950 }
9590837b
KT
951 BUG(); /* This must not happen. */
952 return false;
953}
954
955/**
956 * tomoyo_read_domain_policy - Read domain policy.
957 *
958 * @head: Pointer to "struct tomoyo_io_buffer".
959 *
fdb8ebb7 960 * Caller holds tomoyo_read_lock().
9590837b 961 */
8fbe71f0 962static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
9590837b 963{
9590837b
KT
964 bool done = true;
965
966 if (head->read_eof)
8fbe71f0 967 return;
9590837b
KT
968 if (head->read_step == 0)
969 head->read_step = 1;
475e6fa3
TH
970 list_for_each_cookie(head->read_var1, &tomoyo_domain_list) {
971 struct tomoyo_domain_info *domain =
972 list_entry(head->read_var1, typeof(*domain), list);
9590837b
KT
973 const char *quota_exceeded = "";
974 const char *transition_failed = "";
975 const char *ignore_global_allow_read = "";
9590837b
KT
976 if (head->read_step != 1)
977 goto acl_loop;
978 if (domain->is_deleted && !head->read_single_domain)
979 continue;
980 /* Print domainname and flags. */
981 if (domain->quota_warned)
982 quota_exceeded = "quota_exceeded\n";
ea13ddba 983 if (domain->transition_failed)
9590837b 984 transition_failed = "transition_failed\n";
ea13ddba 985 if (domain->ignore_global_allow_read)
9590837b
KT
986 ignore_global_allow_read
987 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
7d2948b1
TH
988 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
989 "%u\n%s%s%s\n",
990 domain->domainname->name,
991 domain->profile, quota_exceeded,
992 transition_failed,
993 ignore_global_allow_read);
994 if (!done)
9590837b 995 break;
9590837b
KT
996 head->read_step = 2;
997acl_loop:
998 if (head->read_step == 3)
999 goto tail_mark;
1000 /* Print ACL entries in the domain. */
475e6fa3 1001 list_for_each_cookie(head->read_var2,
7d2948b1 1002 &domain->acl_info_list) {
475e6fa3
TH
1003 struct tomoyo_acl_info *ptr =
1004 list_entry(head->read_var2, typeof(*ptr), list);
7d2948b1
TH
1005 done = tomoyo_print_entry(head, ptr);
1006 if (!done)
9590837b 1007 break;
9590837b 1008 }
9590837b
KT
1009 if (!done)
1010 break;
475e6fa3 1011 head->read_var2 = NULL;
9590837b
KT
1012 head->read_step = 3;
1013tail_mark:
7d2948b1
TH
1014 done = tomoyo_io_printf(head, "\n");
1015 if (!done)
9590837b 1016 break;
9590837b
KT
1017 head->read_step = 1;
1018 if (head->read_single_domain)
1019 break;
1020 }
9590837b 1021 head->read_eof = done;
9590837b
KT
1022}
1023
1024/**
1025 * tomoyo_write_domain_profile - Assign profile for specified domain.
1026 *
1027 * @head: Pointer to "struct tomoyo_io_buffer".
1028 *
1029 * Returns 0 on success, -EINVAL otherwise.
1030 *
1031 * This is equivalent to doing
1032 *
1033 * ( echo "select " $domainname; echo "use_profile " $profile ) |
9b244373 1034 * /usr/sbin/tomoyo-loadpolicy -d
fdb8ebb7
TH
1035 *
1036 * Caller holds tomoyo_read_lock().
9590837b
KT
1037 */
1038static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1039{
1040 char *data = head->write_buf;
1041 char *cp = strchr(data, ' ');
1042 struct tomoyo_domain_info *domain;
1043 unsigned long profile;
1044
1045 if (!cp)
1046 return -EINVAL;
1047 *cp = '\0';
9590837b 1048 domain = tomoyo_find_domain(cp + 1);
9590837b
KT
1049 if (strict_strtoul(data, 10, &profile))
1050 return -EINVAL;
1051 if (domain && profile < TOMOYO_MAX_PROFILES
1052 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1053 domain->profile = (u8) profile;
1054 return 0;
1055}
1056
1057/**
1058 * tomoyo_read_domain_profile - Read only domainname and profile.
1059 *
1060 * @head: Pointer to "struct tomoyo_io_buffer".
1061 *
1062 * Returns list of profile number and domainname pairs.
1063 *
1064 * This is equivalent to doing
1065 *
1066 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1067 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1068 * domainname = $0; } else if ( $1 == "use_profile" ) {
1069 * print $2 " " domainname; domainname = ""; } } ; '
fdb8ebb7
TH
1070 *
1071 * Caller holds tomoyo_read_lock().
9590837b 1072 */
8fbe71f0 1073static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
9590837b 1074{
9590837b
KT
1075 bool done = true;
1076
1077 if (head->read_eof)
8fbe71f0 1078 return;
475e6fa3
TH
1079 list_for_each_cookie(head->read_var1, &tomoyo_domain_list) {
1080 struct tomoyo_domain_info *domain =
1081 list_entry(head->read_var1, typeof(*domain), list);
9590837b
KT
1082 if (domain->is_deleted)
1083 continue;
7d2948b1
TH
1084 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1085 domain->domainname->name);
1086 if (!done)
9590837b 1087 break;
9590837b 1088 }
9590837b 1089 head->read_eof = done;
9590837b
KT
1090}
1091
1092/**
1093 * tomoyo_write_pid: Specify PID to obtain domainname.
1094 *
1095 * @head: Pointer to "struct tomoyo_io_buffer".
1096 *
1097 * Returns 0.
1098 */
1099static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1100{
1101 unsigned long pid;
1102 /* No error check. */
1103 strict_strtoul(head->write_buf, 10, &pid);
1104 head->read_step = (int) pid;
1105 head->read_eof = false;
1106 return 0;
1107}
1108
1109/**
1110 * tomoyo_read_pid - Get domainname of the specified PID.
1111 *
1112 * @head: Pointer to "struct tomoyo_io_buffer".
1113 *
1114 * Returns the domainname which the specified PID is in on success,
1115 * empty string otherwise.
1116 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1117 * using read()/write() interface rather than sysctl() interface.
1118 */
8fbe71f0 1119static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
9590837b
KT
1120{
1121 if (head->read_avail == 0 && !head->read_eof) {
1122 const int pid = head->read_step;
1123 struct task_struct *p;
1124 struct tomoyo_domain_info *domain = NULL;
1fcdc7c5 1125 rcu_read_lock();
9590837b
KT
1126 read_lock(&tasklist_lock);
1127 p = find_task_by_vpid(pid);
1128 if (p)
1129 domain = tomoyo_real_domain(p);
1130 read_unlock(&tasklist_lock);
1fcdc7c5 1131 rcu_read_unlock();
9590837b
KT
1132 if (domain)
1133 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
1134 domain->domainname->name);
1135 head->read_eof = true;
1136 }
9590837b
KT
1137}
1138
5448ec4f
TH
1139static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
1140 [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE]
1141 = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN,
1142 [TOMOYO_TRANSITION_CONTROL_INITIALIZE]
1143 = TOMOYO_KEYWORD_INITIALIZE_DOMAIN,
1144 [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN,
1145 [TOMOYO_TRANSITION_CONTROL_KEEP] = TOMOYO_KEYWORD_KEEP_DOMAIN
1146};
1147
9590837b
KT
1148/**
1149 * tomoyo_write_exception_policy - Write exception policy.
1150 *
1151 * @head: Pointer to "struct tomoyo_io_buffer".
1152 *
1153 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
1154 *
1155 * Caller holds tomoyo_read_lock().
9590837b
KT
1156 */
1157static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1158{
1159 char *data = head->write_buf;
1160 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
5448ec4f 1161 u8 i;
9590837b 1162
5448ec4f
TH
1163 for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) {
1164 if (tomoyo_str_starts(&data, tomoyo_transition_type[i]))
1165 return tomoyo_write_transition_control(data, is_delete,
1166 i);
1167 }
1084307c
TH
1168 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR))
1169 return tomoyo_write_aggregator_policy(data, is_delete);
9590837b
KT
1170 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1171 return tomoyo_write_globally_readable_policy(data, is_delete);
1172 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1173 return tomoyo_write_pattern_policy(data, is_delete);
1174 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1175 return tomoyo_write_no_rewrite_policy(data, is_delete);
7762fbff 1176 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
7c2ea22e 1177 return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
4c3e9e2d 1178 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
7c2ea22e 1179 return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
9590837b
KT
1180 return -EINVAL;
1181}
1182
31845e8c
TH
1183static void tomoyo_print_number(char *buffer, int buffer_len,
1184 const struct tomoyo_number_union *ptr)
1185{
1186 int i;
1187 unsigned long min = ptr->values[0];
1188 const unsigned long max = ptr->values[1];
1189 u8 min_type = ptr->min_type;
1190 const u8 max_type = ptr->max_type;
1191 memset(buffer, 0, buffer_len);
1192 buffer_len -= 2;
1193 for (i = 0; i < 2; i++) {
1194 int len;
1195 switch (min_type) {
1196 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
1197 snprintf(buffer, buffer_len, "0x%lX", min);
1198 break;
1199 case TOMOYO_VALUE_TYPE_OCTAL:
1200 snprintf(buffer, buffer_len, "0%lo", min);
1201 break;
1202 default:
1203 snprintf(buffer, buffer_len, "%lu", min);
1204 break;
1205 }
1206 if (min == max && min_type == max_type)
1207 break;
1208 len = strlen(buffer);
1209 buffer[len++] = '-';
1210 buffer += len;
1211 buffer_len -= len;
1212 min_type = max_type;
1213 min = max;
1214 }
1215}
1216
1217static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1218 [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
1219 [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
1220};
1221
9590837b 1222/**
31845e8c 1223 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
9590837b
KT
1224 *
1225 * @head: Pointer to "struct tomoyo_io_buffer".
31845e8c
TH
1226 * @idx: Index number.
1227 *
1228 * Returns true on success, false otherwise.
9590837b 1229 *
fdb8ebb7 1230 * Caller holds tomoyo_read_lock().
9590837b 1231 */
31845e8c 1232static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
9590837b 1233{
31845e8c
TH
1234 const char *w[3] = { "", "", "" };
1235 w[0] = tomoyo_group_name[idx];
475e6fa3 1236 list_for_each_cookie(head->read_var1, &tomoyo_group_list[idx]) {
31845e8c 1237 struct tomoyo_group *group =
475e6fa3 1238 list_entry(head->read_var1, typeof(*group), list);
31845e8c 1239 w[1] = group->group_name->name;
475e6fa3 1240 list_for_each_cookie(head->read_var2, &group->member_list) {
31845e8c
TH
1241 char buffer[128];
1242 struct tomoyo_acl_head *ptr =
475e6fa3 1243 list_entry(head->read_var2, typeof(*ptr), list);
31845e8c
TH
1244 if (ptr->is_deleted)
1245 continue;
1246 if (idx == TOMOYO_PATH_GROUP) {
1247 w[2] = container_of(ptr,
1248 struct tomoyo_path_group,
1249 head)->member_name->name;
1250 } else if (idx == TOMOYO_NUMBER_GROUP) {
1251 tomoyo_print_number(buffer, sizeof(buffer),
1252 &container_of
1253 (ptr, struct
1254 tomoyo_number_group,
1255 head)->number);
1256 w[2] = buffer;
1257 }
1258 if (!tomoyo_io_printf(head, "%s%s %s\n", w[0], w[1],
1259 w[2]))
1260 return false;
1261 }
475e6fa3 1262 head->read_var2 = NULL;
31845e8c 1263 }
475e6fa3 1264 head->read_var1 = NULL;
31845e8c
TH
1265 return true;
1266}
1267
1268/**
1269 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1270 *
1271 * @head: Pointer to "struct tomoyo_io_buffer".
1272 * @idx: Index number.
1273 *
1274 * Returns true on success, false otherwise.
1275 *
1276 * Caller holds tomoyo_read_lock().
1277 */
1278static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1279{
475e6fa3 1280 list_for_each_cookie(head->read_var2, &tomoyo_policy_list[idx]) {
31845e8c 1281 const char *w[4] = { "", "", "", "" };
475e6fa3
TH
1282 struct tomoyo_acl_head *acl =
1283 container_of(head->read_var2, typeof(*acl), list);
31845e8c
TH
1284 if (acl->is_deleted)
1285 continue;
1286 switch (idx) {
5448ec4f 1287 case TOMOYO_ID_TRANSITION_CONTROL:
31845e8c 1288 {
5448ec4f 1289 struct tomoyo_transition_control *ptr =
31845e8c 1290 container_of(acl, typeof(*ptr), head);
5448ec4f
TH
1291 w[0] = tomoyo_transition_type[ptr->type];
1292 if (ptr->program)
31845e8c 1293 w[1] = ptr->program->name;
5448ec4f 1294 if (ptr->domainname)
31845e8c 1295 w[3] = ptr->domainname->name;
5448ec4f
TH
1296 if (w[1][0] && w[3][0])
1297 w[2] = " from ";
31845e8c
TH
1298 }
1299 break;
1300 case TOMOYO_ID_GLOBALLY_READABLE:
1301 {
1302 struct tomoyo_globally_readable_file_entry *ptr
1303 = container_of(acl, typeof(*ptr), head);
1304 w[0] = TOMOYO_KEYWORD_ALLOW_READ;
1305 w[1] = ptr->filename->name;
1306 }
1307 break;
31845e8c
TH
1308 case TOMOYO_ID_AGGREGATOR:
1309 {
1310 struct tomoyo_aggregator_entry *ptr =
1311 container_of(acl, typeof(*ptr), head);
1312 w[0] = TOMOYO_KEYWORD_AGGREGATOR;
1313 w[1] = ptr->original_name->name;
1314 w[2] = " ";
1315 w[3] = ptr->aggregated_name->name;
1316 }
1317 break;
1318 case TOMOYO_ID_PATTERN:
1319 {
1320 struct tomoyo_pattern_entry *ptr =
1321 container_of(acl, typeof(*ptr), head);
1322 w[0] = TOMOYO_KEYWORD_FILE_PATTERN;
1323 w[1] = ptr->pattern->name;
1324 }
1325 break;
1326 case TOMOYO_ID_NO_REWRITE:
1327 {
1328 struct tomoyo_no_rewrite_entry *ptr =
1329 container_of(acl, typeof(*ptr), head);
1330 w[0] = TOMOYO_KEYWORD_DENY_REWRITE;
1331 w[1] = ptr->pattern->name;
1332 }
1333 break;
1334 default:
1335 continue;
9590837b 1336 }
31845e8c
TH
1337 if (!tomoyo_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2],
1338 w[3]))
1339 return false;
9590837b 1340 }
475e6fa3 1341 head->read_var2 = NULL;
31845e8c
TH
1342 return true;
1343}
1344
1345/**
1346 * tomoyo_read_exception_policy - Read exception policy.
1347 *
1348 * @head: Pointer to "struct tomoyo_io_buffer".
1349 *
1350 * Caller holds tomoyo_read_lock().
1351 */
1352static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
1353{
1354 if (head->read_eof)
1355 return;
1356 while (head->read_step < TOMOYO_MAX_POLICY &&
1357 tomoyo_read_policy(head, head->read_step))
1358 head->read_step++;
1359 if (head->read_step < TOMOYO_MAX_POLICY)
1360 return;
1361 while (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1362 tomoyo_read_group(head, head->read_step - TOMOYO_MAX_POLICY))
1363 head->read_step++;
1364 if (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1365 return;
1366 head->read_eof = true;
9590837b
KT
1367}
1368
17fcfbd9
TH
1369/**
1370 * tomoyo_print_header - Get header line of audit log.
1371 *
1372 * @r: Pointer to "struct tomoyo_request_info".
1373 *
1374 * Returns string representation.
1375 *
1376 * This function uses kmalloc(), so caller must kfree() if this function
1377 * didn't return NULL.
1378 */
1379static char *tomoyo_print_header(struct tomoyo_request_info *r)
1380{
1381 static const char *tomoyo_mode_4[4] = {
1382 "disabled", "learning", "permissive", "enforcing"
1383 };
1384 struct timeval tv;
1385 const pid_t gpid = task_pid_nr(current);
1386 static const int tomoyo_buffer_len = 4096;
1387 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1388 if (!buffer)
1389 return NULL;
1390 do_gettimeofday(&tv);
1391 snprintf(buffer, tomoyo_buffer_len - 1,
1392 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1393 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1394 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1395 tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
1396 (pid_t) sys_getpid(), (pid_t) sys_getppid(),
1397 current_uid(), current_gid(), current_euid(),
1398 current_egid(), current_suid(), current_sgid(),
1399 current_fsuid(), current_fsgid());
1400 return buffer;
1401}
1402
1403/**
1404 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1405 *
1406 * @len: Required size.
1407 * @r: Pointer to "struct tomoyo_request_info".
1408 *
1409 * Returns pointer to allocated memory.
1410 *
1411 * The @len is updated to add the header lines' size on success.
1412 *
1413 * This function uses kzalloc(), so caller must kfree() if this function
1414 * didn't return NULL.
1415 */
1416static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1417{
1418 char *buf = NULL;
1419 const char *header;
1420 const char *domainname;
1421 if (!r->domain)
1422 r->domain = tomoyo_domain();
1423 domainname = r->domain->domainname->name;
1424 header = tomoyo_print_header(r);
1425 if (!header)
1426 return NULL;
1427 *len += strlen(domainname) + strlen(header) + 10;
1428 buf = kzalloc(*len, GFP_NOFS);
1429 if (buf)
1430 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1431 kfree(header);
1432 return buf;
1433}
1434
1435/* Wait queue for tomoyo_query_list. */
1436static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1437
1438/* Lock for manipulating tomoyo_query_list. */
1439static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1440
1441/* Structure for query. */
1442struct tomoyo_query_entry {
1443 struct list_head list;
1444 char *query;
1445 int query_len;
1446 unsigned int serial;
1447 int timer;
1448 int answer;
1449};
1450
1451/* The list for "struct tomoyo_query_entry". */
1452static LIST_HEAD(tomoyo_query_list);
1453
1454/*
1455 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1456 * interface.
1457 */
1458static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1459
1460/**
1461 * tomoyo_supervisor - Ask for the supervisor's decision.
1462 *
1463 * @r: Pointer to "struct tomoyo_request_info".
1464 * @fmt: The printf()'s format string, followed by parameters.
1465 *
1466 * Returns 0 if the supervisor decided to permit the access request which
1467 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1468 * supervisor decided to retry the access request which violated the policy in
1469 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1470 */
1471int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1472{
1473 va_list args;
1474 int error = -EPERM;
1475 int pos;
1476 int len;
1477 static unsigned int tomoyo_serial;
1478 struct tomoyo_query_entry *tomoyo_query_entry = NULL;
1479 bool quota_exceeded = false;
1480 char *header;
1481 switch (r->mode) {
1482 char *buffer;
1483 case TOMOYO_CONFIG_LEARNING:
1484 if (!tomoyo_domain_quota_is_ok(r))
1485 return 0;
1486 va_start(args, fmt);
1487 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1488 va_end(args);
1489 buffer = kmalloc(len, GFP_NOFS);
1490 if (!buffer)
1491 return 0;
1492 va_start(args, fmt);
1493 vsnprintf(buffer, len - 1, fmt, args);
1494 va_end(args);
1495 tomoyo_normalize_line(buffer);
1496 tomoyo_write_domain_policy2(buffer, r->domain, false);
1497 kfree(buffer);
1498 /* fall through */
1499 case TOMOYO_CONFIG_PERMISSIVE:
1500 return 0;
1501 }
1502 if (!r->domain)
1503 r->domain = tomoyo_domain();
1504 if (!atomic_read(&tomoyo_query_observers))
1505 return -EPERM;
1506 va_start(args, fmt);
1507 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1508 va_end(args);
1509 header = tomoyo_init_audit_log(&len, r);
1510 if (!header)
1511 goto out;
1512 tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
1513 if (!tomoyo_query_entry)
1514 goto out;
1515 tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
1516 if (!tomoyo_query_entry->query)
1517 goto out;
1518 len = ksize(tomoyo_query_entry->query);
1519 INIT_LIST_HEAD(&tomoyo_query_entry->list);
1520 spin_lock(&tomoyo_query_list_lock);
1521 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
1522 sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
1523 quota_exceeded = true;
1524 } else {
1525 tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
1526 tomoyo_query_entry->serial = tomoyo_serial++;
1527 }
1528 spin_unlock(&tomoyo_query_list_lock);
1529 if (quota_exceeded)
1530 goto out;
1531 pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
1532 tomoyo_query_entry->serial, r->retry, header);
1533 kfree(header);
1534 header = NULL;
1535 va_start(args, fmt);
1536 vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
1537 tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
1538 va_end(args);
1539 spin_lock(&tomoyo_query_list_lock);
1540 list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
1541 spin_unlock(&tomoyo_query_list_lock);
1542 /* Give 10 seconds for supervisor's opinion. */
1543 for (tomoyo_query_entry->timer = 0;
1544 atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
1545 tomoyo_query_entry->timer++) {
1546 wake_up(&tomoyo_query_wait);
1547 set_current_state(TASK_INTERRUPTIBLE);
1548 schedule_timeout(HZ / 10);
1549 if (tomoyo_query_entry->answer)
1550 break;
1551 }
1552 spin_lock(&tomoyo_query_list_lock);
1553 list_del(&tomoyo_query_entry->list);
1554 tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
1555 spin_unlock(&tomoyo_query_list_lock);
1556 switch (tomoyo_query_entry->answer) {
1557 case 3: /* Asked to retry by administrator. */
1558 error = TOMOYO_RETRY_REQUEST;
1559 r->retry++;
1560 break;
1561 case 1:
1562 /* Granted by administrator. */
1563 error = 0;
1564 break;
1565 case 0:
1566 /* Timed out. */
1567 break;
1568 default:
1569 /* Rejected by administrator. */
1570 break;
1571 }
1572 out:
1573 if (tomoyo_query_entry)
1574 kfree(tomoyo_query_entry->query);
1575 kfree(tomoyo_query_entry);
1576 kfree(header);
1577 return error;
1578}
1579
1580/**
1581 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1582 *
1583 * @file: Pointer to "struct file".
1584 * @wait: Pointer to "poll_table".
1585 *
1586 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1587 *
1588 * Waits for access requests which violated policy in enforcing mode.
1589 */
1590static int tomoyo_poll_query(struct file *file, poll_table *wait)
1591{
1592 struct list_head *tmp;
1593 bool found = false;
1594 u8 i;
1595 for (i = 0; i < 2; i++) {
1596 spin_lock(&tomoyo_query_list_lock);
1597 list_for_each(tmp, &tomoyo_query_list) {
1598 struct tomoyo_query_entry *ptr
1599 = list_entry(tmp, struct tomoyo_query_entry,
1600 list);
1601 if (ptr->answer)
1602 continue;
1603 found = true;
1604 break;
1605 }
1606 spin_unlock(&tomoyo_query_list_lock);
1607 if (found)
1608 return POLLIN | POLLRDNORM;
1609 if (i)
1610 break;
1611 poll_wait(file, &tomoyo_query_wait, wait);
1612 }
1613 return 0;
1614}
1615
1616/**
1617 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1618 *
1619 * @head: Pointer to "struct tomoyo_io_buffer".
17fcfbd9 1620 */
8fbe71f0 1621static void tomoyo_read_query(struct tomoyo_io_buffer *head)
17fcfbd9
TH
1622{
1623 struct list_head *tmp;
1624 int pos = 0;
1625 int len = 0;
1626 char *buf;
1627 if (head->read_avail)
8fbe71f0 1628 return;
17fcfbd9
TH
1629 if (head->read_buf) {
1630 kfree(head->read_buf);
1631 head->read_buf = NULL;
1632 head->readbuf_size = 0;
1633 }
1634 spin_lock(&tomoyo_query_list_lock);
1635 list_for_each(tmp, &tomoyo_query_list) {
1636 struct tomoyo_query_entry *ptr
1637 = list_entry(tmp, struct tomoyo_query_entry, list);
1638 if (ptr->answer)
1639 continue;
1640 if (pos++ != head->read_step)
1641 continue;
1642 len = ptr->query_len;
1643 break;
1644 }
1645 spin_unlock(&tomoyo_query_list_lock);
1646 if (!len) {
1647 head->read_step = 0;
8fbe71f0 1648 return;
17fcfbd9
TH
1649 }
1650 buf = kzalloc(len, GFP_NOFS);
1651 if (!buf)
8fbe71f0 1652 return;
17fcfbd9
TH
1653 pos = 0;
1654 spin_lock(&tomoyo_query_list_lock);
1655 list_for_each(tmp, &tomoyo_query_list) {
1656 struct tomoyo_query_entry *ptr
1657 = list_entry(tmp, struct tomoyo_query_entry, list);
1658 if (ptr->answer)
1659 continue;
1660 if (pos++ != head->read_step)
1661 continue;
1662 /*
1663 * Some query can be skipped because tomoyo_query_list
1664 * can change, but I don't care.
1665 */
1666 if (len == ptr->query_len)
1667 memmove(buf, ptr->query, len);
1668 break;
1669 }
1670 spin_unlock(&tomoyo_query_list_lock);
1671 if (buf[0]) {
1672 head->read_avail = len;
1673 head->readbuf_size = head->read_avail;
1674 head->read_buf = buf;
1675 head->read_step++;
1676 } else {
1677 kfree(buf);
1678 }
17fcfbd9
TH
1679}
1680
1681/**
1682 * tomoyo_write_answer - Write the supervisor's decision.
1683 *
1684 * @head: Pointer to "struct tomoyo_io_buffer".
1685 *
1686 * Returns 0 on success, -EINVAL otherwise.
1687 */
1688static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1689{
1690 char *data = head->write_buf;
1691 struct list_head *tmp;
1692 unsigned int serial;
1693 unsigned int answer;
1694 spin_lock(&tomoyo_query_list_lock);
1695 list_for_each(tmp, &tomoyo_query_list) {
1696 struct tomoyo_query_entry *ptr
1697 = list_entry(tmp, struct tomoyo_query_entry, list);
1698 ptr->timer = 0;
1699 }
1700 spin_unlock(&tomoyo_query_list_lock);
1701 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1702 return -EINVAL;
1703 spin_lock(&tomoyo_query_list_lock);
1704 list_for_each(tmp, &tomoyo_query_list) {
1705 struct tomoyo_query_entry *ptr
1706 = list_entry(tmp, struct tomoyo_query_entry, list);
1707 if (ptr->serial != serial)
1708 continue;
1709 if (!ptr->answer)
1710 ptr->answer = answer;
1711 break;
1712 }
1713 spin_unlock(&tomoyo_query_list_lock);
1714 return 0;
1715}
1716
9590837b
KT
1717/**
1718 * tomoyo_read_version: Get version.
1719 *
1720 * @head: Pointer to "struct tomoyo_io_buffer".
1721 *
1722 * Returns version information.
1723 */
8fbe71f0 1724static void tomoyo_read_version(struct tomoyo_io_buffer *head)
9590837b
KT
1725{
1726 if (!head->read_eof) {
57c2590f 1727 tomoyo_io_printf(head, "2.3.0-pre");
9590837b
KT
1728 head->read_eof = true;
1729 }
9590837b
KT
1730}
1731
1732/**
1733 * tomoyo_read_self_domain - Get the current process's domainname.
1734 *
1735 * @head: Pointer to "struct tomoyo_io_buffer".
1736 *
1737 * Returns the current process's domainname.
1738 */
8fbe71f0 1739static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
9590837b
KT
1740{
1741 if (!head->read_eof) {
1742 /*
1743 * tomoyo_domain()->domainname != NULL
1744 * because every process belongs to a domain and
1745 * the domain's name cannot be NULL.
1746 */
1747 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1748 head->read_eof = true;
1749 }
9590837b
KT
1750}
1751
1752/**
1753 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1754 *
1755 * @type: Type of interface.
1756 * @file: Pointer to "struct file".
1757 *
1758 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
fdb8ebb7
TH
1759 *
1760 * Caller acquires tomoyo_read_lock().
9590837b 1761 */
c3ef1500 1762int tomoyo_open_control(const u8 type, struct file *file)
9590837b 1763{
4e5d6f7e 1764 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
9590837b
KT
1765
1766 if (!head)
1767 return -ENOMEM;
1768 mutex_init(&head->io_sem);
17fcfbd9 1769 head->type = type;
9590837b
KT
1770 switch (type) {
1771 case TOMOYO_DOMAINPOLICY:
1772 /* /sys/kernel/security/tomoyo/domain_policy */
1773 head->write = tomoyo_write_domain_policy;
1774 head->read = tomoyo_read_domain_policy;
1775 break;
1776 case TOMOYO_EXCEPTIONPOLICY:
1777 /* /sys/kernel/security/tomoyo/exception_policy */
1778 head->write = tomoyo_write_exception_policy;
1779 head->read = tomoyo_read_exception_policy;
1780 break;
1781 case TOMOYO_SELFDOMAIN:
1782 /* /sys/kernel/security/tomoyo/self_domain */
1783 head->read = tomoyo_read_self_domain;
1784 break;
1785 case TOMOYO_DOMAIN_STATUS:
1786 /* /sys/kernel/security/tomoyo/.domain_status */
1787 head->write = tomoyo_write_domain_profile;
1788 head->read = tomoyo_read_domain_profile;
1789 break;
1790 case TOMOYO_PROCESS_STATUS:
1791 /* /sys/kernel/security/tomoyo/.process_status */
1792 head->write = tomoyo_write_pid;
1793 head->read = tomoyo_read_pid;
1794 break;
1795 case TOMOYO_VERSION:
1796 /* /sys/kernel/security/tomoyo/version */
1797 head->read = tomoyo_read_version;
1798 head->readbuf_size = 128;
1799 break;
1800 case TOMOYO_MEMINFO:
1801 /* /sys/kernel/security/tomoyo/meminfo */
1802 head->write = tomoyo_write_memory_quota;
1803 head->read = tomoyo_read_memory_counter;
1804 head->readbuf_size = 512;
1805 break;
1806 case TOMOYO_PROFILE:
1807 /* /sys/kernel/security/tomoyo/profile */
1808 head->write = tomoyo_write_profile;
1809 head->read = tomoyo_read_profile;
1810 break;
17fcfbd9
TH
1811 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1812 head->poll = tomoyo_poll_query;
1813 head->write = tomoyo_write_answer;
1814 head->read = tomoyo_read_query;
1815 break;
9590837b
KT
1816 case TOMOYO_MANAGER:
1817 /* /sys/kernel/security/tomoyo/manager */
1818 head->write = tomoyo_write_manager_policy;
1819 head->read = tomoyo_read_manager_policy;
1820 break;
1821 }
1822 if (!(file->f_mode & FMODE_READ)) {
1823 /*
1824 * No need to allocate read_buf since it is not opened
1825 * for reading.
1826 */
1827 head->read = NULL;
17fcfbd9
TH
1828 head->poll = NULL;
1829 } else if (!head->poll) {
1830 /* Don't allocate read_buf for poll() access. */
9590837b
KT
1831 if (!head->readbuf_size)
1832 head->readbuf_size = 4096 * 2;
4e5d6f7e 1833 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
9590837b 1834 if (!head->read_buf) {
8e2d39a1 1835 kfree(head);
9590837b
KT
1836 return -ENOMEM;
1837 }
1838 }
1839 if (!(file->f_mode & FMODE_WRITE)) {
1840 /*
1841 * No need to allocate write_buf since it is not opened
1842 * for writing.
1843 */
1844 head->write = NULL;
1845 } else if (head->write) {
1846 head->writebuf_size = 4096 * 2;
4e5d6f7e 1847 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
9590837b 1848 if (!head->write_buf) {
8e2d39a1
TH
1849 kfree(head->read_buf);
1850 kfree(head);
9590837b
KT
1851 return -ENOMEM;
1852 }
1853 }
17fcfbd9
TH
1854 if (type != TOMOYO_QUERY)
1855 head->reader_idx = tomoyo_read_lock();
9590837b
KT
1856 file->private_data = head;
1857 /*
1858 * Call the handler now if the file is
1859 * /sys/kernel/security/tomoyo/self_domain
1860 * so that the user can use
1861 * cat < /sys/kernel/security/tomoyo/self_domain"
1862 * to know the current process's domainname.
1863 */
1864 if (type == TOMOYO_SELFDOMAIN)
1865 tomoyo_read_control(file, NULL, 0);
17fcfbd9
TH
1866 /*
1867 * If the file is /sys/kernel/security/tomoyo/query , increment the
1868 * observer counter.
1869 * The obserber counter is used by tomoyo_supervisor() to see if
1870 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1871 */
1872 else if (type == TOMOYO_QUERY)
1873 atomic_inc(&tomoyo_query_observers);
9590837b
KT
1874 return 0;
1875}
1876
17fcfbd9
TH
1877/**
1878 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1879 *
1880 * @file: Pointer to "struct file".
1881 * @wait: Pointer to "poll_table".
1882 *
1883 * Waits for read readiness.
1884 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1885 */
1886int tomoyo_poll_control(struct file *file, poll_table *wait)
1887{
1888 struct tomoyo_io_buffer *head = file->private_data;
1889 if (!head->poll)
1890 return -ENOSYS;
1891 return head->poll(file, wait);
1892}
1893
9590837b
KT
1894/**
1895 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1896 *
1897 * @file: Pointer to "struct file".
1898 * @buffer: Poiner to buffer to write to.
1899 * @buffer_len: Size of @buffer.
1900 *
1901 * Returns bytes read on success, negative value otherwise.
fdb8ebb7
TH
1902 *
1903 * Caller holds tomoyo_read_lock().
9590837b 1904 */
c3ef1500
TH
1905int tomoyo_read_control(struct file *file, char __user *buffer,
1906 const int buffer_len)
9590837b
KT
1907{
1908 int len = 0;
1909 struct tomoyo_io_buffer *head = file->private_data;
1910 char *cp;
1911
1912 if (!head->read)
1913 return -ENOSYS;
1914 if (mutex_lock_interruptible(&head->io_sem))
1915 return -EINTR;
1916 /* Call the policy handler. */
8fbe71f0 1917 head->read(head);
9590837b
KT
1918 if (len < 0)
1919 goto out;
1920 /* Write to buffer. */
1921 len = head->read_avail;
1922 if (len > buffer_len)
1923 len = buffer_len;
1924 if (!len)
1925 goto out;
1926 /* head->read_buf changes by some functions. */
1927 cp = head->read_buf;
1928 if (copy_to_user(buffer, cp, len)) {
1929 len = -EFAULT;
1930 goto out;
1931 }
1932 head->read_avail -= len;
1933 memmove(cp, cp + len, head->read_avail);
1934 out:
1935 mutex_unlock(&head->io_sem);
1936 return len;
1937}
1938
1939/**
1940 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1941 *
1942 * @file: Pointer to "struct file".
1943 * @buffer: Pointer to buffer to read from.
1944 * @buffer_len: Size of @buffer.
1945 *
1946 * Returns @buffer_len on success, negative value otherwise.
fdb8ebb7
TH
1947 *
1948 * Caller holds tomoyo_read_lock().
9590837b 1949 */
c3ef1500
TH
1950int tomoyo_write_control(struct file *file, const char __user *buffer,
1951 const int buffer_len)
9590837b
KT
1952{
1953 struct tomoyo_io_buffer *head = file->private_data;
1954 int error = buffer_len;
1955 int avail_len = buffer_len;
1956 char *cp0 = head->write_buf;
1957
1958 if (!head->write)
1959 return -ENOSYS;
1960 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1961 return -EFAULT;
1962 /* Don't allow updating policies by non manager programs. */
1963 if (head->write != tomoyo_write_pid &&
1964 head->write != tomoyo_write_domain_policy &&
75093152 1965 !tomoyo_policy_manager())
9590837b
KT
1966 return -EPERM;
1967 if (mutex_lock_interruptible(&head->io_sem))
1968 return -EINTR;
1969 /* Read a line and dispatch it to the policy handler. */
1970 while (avail_len > 0) {
1971 char c;
1972 if (head->write_avail >= head->writebuf_size - 1) {
1973 error = -ENOMEM;
1974 break;
1975 } else if (get_user(c, buffer)) {
1976 error = -EFAULT;
1977 break;
1978 }
1979 buffer++;
1980 avail_len--;
1981 cp0[head->write_avail++] = c;
1982 if (c != '\n')
1983 continue;
1984 cp0[head->write_avail - 1] = '\0';
1985 head->write_avail = 0;
1986 tomoyo_normalize_line(cp0);
1987 head->write(head);
1988 }
1989 mutex_unlock(&head->io_sem);
1990 return error;
1991}
1992
1993/**
1994 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
1995 *
1996 * @file: Pointer to "struct file".
1997 *
1998 * Releases memory and returns 0.
fdb8ebb7
TH
1999 *
2000 * Caller looses tomoyo_read_lock().
9590837b 2001 */
c3ef1500 2002int tomoyo_close_control(struct file *file)
9590837b
KT
2003{
2004 struct tomoyo_io_buffer *head = file->private_data;
847b173e 2005 const bool is_write = !!head->write_buf;
9590837b 2006
17fcfbd9
TH
2007 /*
2008 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2009 * observer counter.
2010 */
2011 if (head->type == TOMOYO_QUERY)
2012 atomic_dec(&tomoyo_query_observers);
2013 else
2014 tomoyo_read_unlock(head->reader_idx);
9590837b 2015 /* Release memory used for policy I/O. */
8e2d39a1 2016 kfree(head->read_buf);
9590837b 2017 head->read_buf = NULL;
8e2d39a1 2018 kfree(head->write_buf);
9590837b 2019 head->write_buf = NULL;
8e2d39a1 2020 kfree(head);
9590837b
KT
2021 head = NULL;
2022 file->private_data = NULL;
847b173e
TH
2023 if (is_write)
2024 tomoyo_run_gc();
9590837b
KT
2025 return 0;
2026}
2027
9590837b 2028/**
c3ef1500 2029 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
9590837b 2030 */
c3ef1500 2031void tomoyo_check_profile(void)
9590837b 2032{
c3ef1500
TH
2033 struct tomoyo_domain_info *domain;
2034 const int idx = tomoyo_read_lock();
2035 tomoyo_policy_loaded = true;
2036 /* Check all profiles currently assigned to domains are defined. */
2037 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2038 const u8 profile = domain->profile;
2039 if (tomoyo_profile_ptr[profile])
2040 continue;
2041 panic("Profile %u (used by '%s') not defined.\n",
2042 profile, domain->domainname->name);
2043 }
2044 tomoyo_read_unlock(idx);
57c2590f
TH
2045 if (tomoyo_profile_version != 20090903)
2046 panic("Profile version %u is not supported.\n",
2047 tomoyo_profile_version);
2048 printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n");
c3ef1500 2049 printk(KERN_INFO "Mandatory Access Control activated.\n");
9590837b 2050}