]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/common.c
TOMOYO: Remove alias keyword.
[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
KT
509{
510 struct list_head *pos;
511 bool done = true;
512
513 if (head->read_eof)
8fbe71f0 514 return;
9590837b 515 list_for_each_cookie(pos, head->read_var2,
a230f9e7 516 &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
9590837b
KT
517 struct tomoyo_policy_manager_entry *ptr;
518 ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
82e0f001
TH
519 head.list);
520 if (ptr->head.is_deleted)
9590837b 521 continue;
7d2948b1
TH
522 done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
523 if (!done)
9590837b 524 break;
9590837b 525 }
9590837b 526 head->read_eof = done;
9590837b
KT
527}
528
529/**
75093152 530 * tomoyo_policy_manager - Check whether the current process is a policy manager.
9590837b
KT
531 *
532 * Returns true if the current process is permitted to modify policy
533 * via /sys/kernel/security/tomoyo/ interface.
fdb8ebb7
TH
534 *
535 * Caller holds tomoyo_read_lock().
9590837b 536 */
75093152 537static bool tomoyo_policy_manager(void)
9590837b
KT
538{
539 struct tomoyo_policy_manager_entry *ptr;
540 const char *exe;
541 const struct task_struct *task = current;
542 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
543 bool found = false;
544
545 if (!tomoyo_policy_loaded)
546 return true;
547 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
548 return false;
a230f9e7
TH
549 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
550 head.list) {
82e0f001 551 if (!ptr->head.is_deleted && ptr->is_domain
9590837b
KT
552 && !tomoyo_pathcmp(domainname, ptr->manager)) {
553 found = true;
554 break;
555 }
556 }
9590837b
KT
557 if (found)
558 return true;
559 exe = tomoyo_get_exe();
560 if (!exe)
561 return false;
a230f9e7
TH
562 list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
563 head.list) {
82e0f001 564 if (!ptr->head.is_deleted && !ptr->is_domain
9590837b
KT
565 && !strcmp(exe, ptr->manager->name)) {
566 found = true;
567 break;
568 }
569 }
9590837b
KT
570 if (!found) { /* Reduce error messages. */
571 static pid_t last_pid;
572 const pid_t pid = current->pid;
573 if (last_pid != pid) {
574 printk(KERN_WARNING "%s ( %s ) is not permitted to "
575 "update policies.\n", domainname->name, exe);
576 last_pid = pid;
577 }
578 }
8e2d39a1 579 kfree(exe);
9590837b
KT
580 return found;
581}
582
583/**
75093152 584 * tomoyo_select_one - Parse select command.
9590837b
KT
585 *
586 * @head: Pointer to "struct tomoyo_io_buffer".
587 * @data: String to parse.
588 *
589 * Returns true on success, false otherwise.
fdb8ebb7
TH
590 *
591 * Caller holds tomoyo_read_lock().
9590837b 592 */
75093152 593static bool tomoyo_select_one(struct tomoyo_io_buffer *head,
9590837b
KT
594 const char *data)
595{
596 unsigned int pid;
597 struct tomoyo_domain_info *domain = NULL;
9b244373 598 bool global_pid = false;
9590837b 599
9b244373
TH
600 if (sscanf(data, "pid=%u", &pid) == 1 ||
601 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9590837b 602 struct task_struct *p;
1fcdc7c5 603 rcu_read_lock();
9590837b 604 read_lock(&tasklist_lock);
9b244373
TH
605 if (global_pid)
606 p = find_task_by_pid_ns(pid, &init_pid_ns);
607 else
608 p = find_task_by_vpid(pid);
9590837b
KT
609 if (p)
610 domain = tomoyo_real_domain(p);
611 read_unlock(&tasklist_lock);
1fcdc7c5 612 rcu_read_unlock();
9590837b 613 } else if (!strncmp(data, "domain=", 7)) {
75093152 614 if (tomoyo_domain_def(data + 7))
9590837b 615 domain = tomoyo_find_domain(data + 7);
9590837b
KT
616 } else
617 return false;
618 head->write_var1 = domain;
619 /* Accessing read_buf is safe because head->io_sem is held. */
620 if (!head->read_buf)
621 return true; /* Do nothing if open(O_WRONLY). */
622 head->read_avail = 0;
623 tomoyo_io_printf(head, "# select %s\n", data);
624 head->read_single_domain = true;
625 head->read_eof = !domain;
626 if (domain) {
627 struct tomoyo_domain_info *d;
628 head->read_var1 = NULL;
fdb8ebb7 629 list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
9590837b
KT
630 if (d == domain)
631 break;
632 head->read_var1 = &d->list;
633 }
9590837b
KT
634 head->read_var2 = NULL;
635 head->read_bit = 0;
636 head->read_step = 0;
637 if (domain->is_deleted)
638 tomoyo_io_printf(head, "# This is a deleted domain.\n");
639 }
640 return true;
641}
642
ccf135f5
TH
643/**
644 * tomoyo_delete_domain - Delete a domain.
645 *
646 * @domainname: The name of domain.
647 *
648 * Returns 0.
fdb8ebb7
TH
649 *
650 * Caller holds tomoyo_read_lock().
ccf135f5
TH
651 */
652static int tomoyo_delete_domain(char *domainname)
653{
654 struct tomoyo_domain_info *domain;
655 struct tomoyo_path_info name;
656
657 name.name = domainname;
658 tomoyo_fill_path_info(&name);
29282381
TH
659 if (mutex_lock_interruptible(&tomoyo_policy_lock))
660 return 0;
ccf135f5 661 /* Is there an active domain? */
fdb8ebb7 662 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
ccf135f5
TH
663 /* Never delete tomoyo_kernel_domain */
664 if (domain == &tomoyo_kernel_domain)
665 continue;
666 if (domain->is_deleted ||
667 tomoyo_pathcmp(domain->domainname, &name))
668 continue;
669 domain->is_deleted = true;
670 break;
671 }
f737d95d 672 mutex_unlock(&tomoyo_policy_lock);
ccf135f5
TH
673 return 0;
674}
675
17fcfbd9
TH
676/**
677 * tomoyo_write_domain_policy2 - Write domain policy.
678 *
679 * @head: Pointer to "struct tomoyo_io_buffer".
680 *
681 * Returns 0 on success, negative value otherwise.
682 *
683 * Caller holds tomoyo_read_lock().
684 */
685static int tomoyo_write_domain_policy2(char *data,
686 struct tomoyo_domain_info *domain,
687 const bool is_delete)
688{
689 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
690 return tomoyo_write_mount_policy(data, domain, is_delete);
691 return tomoyo_write_file_policy(data, domain, is_delete);
692}
693
9590837b
KT
694/**
695 * tomoyo_write_domain_policy - Write domain policy.
696 *
697 * @head: Pointer to "struct tomoyo_io_buffer".
698 *
699 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
700 *
701 * Caller holds tomoyo_read_lock().
9590837b
KT
702 */
703static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
704{
705 char *data = head->write_buf;
706 struct tomoyo_domain_info *domain = head->write_var1;
707 bool is_delete = false;
708 bool is_select = false;
9590837b
KT
709 unsigned int profile;
710
711 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
712 is_delete = true;
713 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
714 is_select = true;
75093152 715 if (is_select && tomoyo_select_one(head, data))
9590837b
KT
716 return 0;
717 /* Don't allow updating policies by non manager programs. */
75093152 718 if (!tomoyo_policy_manager())
9590837b 719 return -EPERM;
75093152 720 if (tomoyo_domain_def(data)) {
9590837b
KT
721 domain = NULL;
722 if (is_delete)
723 tomoyo_delete_domain(data);
fdb8ebb7 724 else if (is_select)
9590837b 725 domain = tomoyo_find_domain(data);
fdb8ebb7 726 else
9590837b
KT
727 domain = tomoyo_find_or_assign_new_domain(data, 0);
728 head->write_var1 = domain;
729 return 0;
730 }
731 if (!domain)
732 return -EINVAL;
733
734 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
735 && profile < TOMOYO_MAX_PROFILES) {
736 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
737 domain->profile = (u8) profile;
738 return 0;
739 }
740 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
ea13ddba 741 domain->ignore_global_allow_read = !is_delete;
9590837b
KT
742 return 0;
743 }
9b244373
TH
744 if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
745 domain->quota_warned = !is_delete;
746 return 0;
747 }
748 if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
749 domain->transition_failed = !is_delete;
750 return 0;
751 }
17fcfbd9 752 return tomoyo_write_domain_policy2(data, domain, is_delete);
9590837b
KT
753}
754
755/**
7ef61233 756 * tomoyo_print_path_acl - Print a single path ACL entry.
9590837b
KT
757 *
758 * @head: Pointer to "struct tomoyo_io_buffer".
7ef61233 759 * @ptr: Pointer to "struct tomoyo_path_acl".
9590837b
KT
760 *
761 * Returns true on success, false otherwise.
762 */
7ef61233
TH
763static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
764 struct tomoyo_path_acl *ptr)
9590837b
KT
765{
766 int pos;
767 u8 bit;
a1f9bb6a 768 const u16 perm = ptr->perm;
9590837b 769
7ef61233 770 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
9590837b
KT
771 if (!(perm & (1 << bit)))
772 continue;
773 /* Print "read/write" instead of "read" and "write". */
7ef61233
TH
774 if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
775 && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
9590837b 776 continue;
9590837b 777 pos = head->read_avail;
7762fbff 778 if (!tomoyo_io_printf(head, "allow_%s ",
71c28236 779 tomoyo_path_keyword[bit]) ||
7762fbff
TH
780 !tomoyo_print_name_union(head, &ptr->name) ||
781 !tomoyo_io_printf(head, "\n"))
9590837b
KT
782 goto out;
783 }
784 head->read_bit = 0;
785 return true;
786 out:
787 head->read_bit = bit;
788 head->read_avail = pos;
789 return false;
790}
791
792/**
7ef61233 793 * tomoyo_print_path2_acl - Print a double path ACL entry.
9590837b
KT
794 *
795 * @head: Pointer to "struct tomoyo_io_buffer".
7ef61233 796 * @ptr: Pointer to "struct tomoyo_path2_acl".
9590837b
KT
797 *
798 * Returns true on success, false otherwise.
799 */
7ef61233
TH
800static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
801 struct tomoyo_path2_acl *ptr)
9590837b
KT
802{
803 int pos;
9590837b
KT
804 const u8 perm = ptr->perm;
805 u8 bit;
806
7ef61233 807 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
9590837b
KT
808 if (!(perm & (1 << bit)))
809 continue;
9590837b 810 pos = head->read_avail;
7762fbff 811 if (!tomoyo_io_printf(head, "allow_%s ",
71c28236 812 tomoyo_path2_keyword[bit]) ||
7762fbff
TH
813 !tomoyo_print_name_union(head, &ptr->name1) ||
814 !tomoyo_print_name_union(head, &ptr->name2) ||
815 !tomoyo_io_printf(head, "\n"))
9590837b
KT
816 goto out;
817 }
818 head->read_bit = 0;
819 return true;
820 out:
821 head->read_bit = bit;
822 head->read_avail = pos;
823 return false;
824}
825
a1f9bb6a
TH
826/**
827 * tomoyo_print_path_number_acl - Print a path_number ACL entry.
828 *
829 * @head: Pointer to "struct tomoyo_io_buffer".
830 * @ptr: Pointer to "struct tomoyo_path_number_acl".
831 *
832 * Returns true on success, false otherwise.
833 */
834static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
835 struct tomoyo_path_number_acl *ptr)
836{
837 int pos;
838 u8 bit;
839 const u8 perm = ptr->perm;
840 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
841 bit++) {
842 if (!(perm & (1 << bit)))
843 continue;
844 pos = head->read_avail;
845 if (!tomoyo_io_printf(head, "allow_%s",
71c28236 846 tomoyo_path_number_keyword[bit]) ||
a1f9bb6a
TH
847 !tomoyo_print_name_union(head, &ptr->name) ||
848 !tomoyo_print_number_union(head, &ptr->number) ||
849 !tomoyo_io_printf(head, "\n"))
850 goto out;
851 }
852 head->read_bit = 0;
853 return true;
854 out:
855 head->read_bit = bit;
856 head->read_avail = pos;
857 return false;
858}
859
860/**
75093152 861 * tomoyo_print_mkdev_acl - Print a mkdev ACL entry.
a1f9bb6a
TH
862 *
863 * @head: Pointer to "struct tomoyo_io_buffer".
75093152 864 * @ptr: Pointer to "struct tomoyo_mkdev_acl".
a1f9bb6a
TH
865 *
866 * Returns true on success, false otherwise.
867 */
75093152
TH
868static bool tomoyo_print_mkdev_acl(struct tomoyo_io_buffer *head,
869 struct tomoyo_mkdev_acl *ptr)
a1f9bb6a
TH
870{
871 int pos;
872 u8 bit;
873 const u16 perm = ptr->perm;
75093152 874 for (bit = head->read_bit; bit < TOMOYO_MAX_MKDEV_OPERATION;
a1f9bb6a
TH
875 bit++) {
876 if (!(perm & (1 << bit)))
877 continue;
878 pos = head->read_avail;
879 if (!tomoyo_io_printf(head, "allow_%s",
71c28236 880 tomoyo_mkdev_keyword[bit]) ||
a1f9bb6a
TH
881 !tomoyo_print_name_union(head, &ptr->name) ||
882 !tomoyo_print_number_union(head, &ptr->mode) ||
883 !tomoyo_print_number_union(head, &ptr->major) ||
884 !tomoyo_print_number_union(head, &ptr->minor) ||
885 !tomoyo_io_printf(head, "\n"))
886 goto out;
887 }
888 head->read_bit = 0;
889 return true;
890 out:
891 head->read_bit = bit;
892 head->read_avail = pos;
893 return false;
894}
895
2106ccd9
TH
896/**
897 * tomoyo_print_mount_acl - Print a mount ACL entry.
898 *
899 * @head: Pointer to "struct tomoyo_io_buffer".
900 * @ptr: Pointer to "struct tomoyo_mount_acl".
901 *
902 * Returns true on success, false otherwise.
903 */
904static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
905 struct tomoyo_mount_acl *ptr)
906{
907 const int pos = head->read_avail;
908 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
909 !tomoyo_print_name_union(head, &ptr->dev_name) ||
910 !tomoyo_print_name_union(head, &ptr->dir_name) ||
911 !tomoyo_print_name_union(head, &ptr->fs_type) ||
912 !tomoyo_print_number_union(head, &ptr->flags) ||
913 !tomoyo_io_printf(head, "\n")) {
914 head->read_avail = pos;
915 return false;
916 }
917 return true;
918}
919
9590837b
KT
920/**
921 * tomoyo_print_entry - Print an ACL entry.
922 *
923 * @head: Pointer to "struct tomoyo_io_buffer".
924 * @ptr: Pointer to an ACL entry.
925 *
926 * Returns true on success, false otherwise.
927 */
928static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
929 struct tomoyo_acl_info *ptr)
930{
ea13ddba 931 const u8 acl_type = ptr->type;
9590837b 932
237ab459
TH
933 if (ptr->is_deleted)
934 return true;
7ef61233
TH
935 if (acl_type == TOMOYO_TYPE_PATH_ACL) {
936 struct tomoyo_path_acl *acl
937 = container_of(ptr, struct tomoyo_path_acl, head);
938 return tomoyo_print_path_acl(head, acl);
9590837b 939 }
7ef61233
TH
940 if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
941 struct tomoyo_path2_acl *acl
942 = container_of(ptr, struct tomoyo_path2_acl, head);
943 return tomoyo_print_path2_acl(head, acl);
9590837b 944 }
a1f9bb6a
TH
945 if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
946 struct tomoyo_path_number_acl *acl
947 = container_of(ptr, struct tomoyo_path_number_acl,
948 head);
949 return tomoyo_print_path_number_acl(head, acl);
950 }
75093152
TH
951 if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
952 struct tomoyo_mkdev_acl *acl
953 = container_of(ptr, struct tomoyo_mkdev_acl,
a1f9bb6a 954 head);
75093152 955 return tomoyo_print_mkdev_acl(head, acl);
a1f9bb6a 956 }
2106ccd9
TH
957 if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
958 struct tomoyo_mount_acl *acl
959 = container_of(ptr, struct tomoyo_mount_acl, head);
960 return tomoyo_print_mount_acl(head, acl);
961 }
9590837b
KT
962 BUG(); /* This must not happen. */
963 return false;
964}
965
966/**
967 * tomoyo_read_domain_policy - Read domain policy.
968 *
969 * @head: Pointer to "struct tomoyo_io_buffer".
970 *
fdb8ebb7 971 * Caller holds tomoyo_read_lock().
9590837b 972 */
8fbe71f0 973static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
9590837b
KT
974{
975 struct list_head *dpos;
976 struct list_head *apos;
977 bool done = true;
978
979 if (head->read_eof)
8fbe71f0 980 return;
9590837b
KT
981 if (head->read_step == 0)
982 head->read_step = 1;
9590837b
KT
983 list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
984 struct tomoyo_domain_info *domain;
985 const char *quota_exceeded = "";
986 const char *transition_failed = "";
987 const char *ignore_global_allow_read = "";
988 domain = list_entry(dpos, struct tomoyo_domain_info, list);
989 if (head->read_step != 1)
990 goto acl_loop;
991 if (domain->is_deleted && !head->read_single_domain)
992 continue;
993 /* Print domainname and flags. */
994 if (domain->quota_warned)
995 quota_exceeded = "quota_exceeded\n";
ea13ddba 996 if (domain->transition_failed)
9590837b 997 transition_failed = "transition_failed\n";
ea13ddba 998 if (domain->ignore_global_allow_read)
9590837b
KT
999 ignore_global_allow_read
1000 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
7d2948b1
TH
1001 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1002 "%u\n%s%s%s\n",
1003 domain->domainname->name,
1004 domain->profile, quota_exceeded,
1005 transition_failed,
1006 ignore_global_allow_read);
1007 if (!done)
9590837b 1008 break;
9590837b
KT
1009 head->read_step = 2;
1010acl_loop:
1011 if (head->read_step == 3)
1012 goto tail_mark;
1013 /* Print ACL entries in the domain. */
9590837b 1014 list_for_each_cookie(apos, head->read_var2,
7d2948b1 1015 &domain->acl_info_list) {
9590837b
KT
1016 struct tomoyo_acl_info *ptr
1017 = list_entry(apos, struct tomoyo_acl_info,
7d2948b1
TH
1018 list);
1019 done = tomoyo_print_entry(head, ptr);
1020 if (!done)
9590837b 1021 break;
9590837b 1022 }
9590837b
KT
1023 if (!done)
1024 break;
1025 head->read_step = 3;
1026tail_mark:
7d2948b1
TH
1027 done = tomoyo_io_printf(head, "\n");
1028 if (!done)
9590837b 1029 break;
9590837b
KT
1030 head->read_step = 1;
1031 if (head->read_single_domain)
1032 break;
1033 }
9590837b 1034 head->read_eof = done;
9590837b
KT
1035}
1036
1037/**
1038 * tomoyo_write_domain_profile - Assign profile for specified domain.
1039 *
1040 * @head: Pointer to "struct tomoyo_io_buffer".
1041 *
1042 * Returns 0 on success, -EINVAL otherwise.
1043 *
1044 * This is equivalent to doing
1045 *
1046 * ( echo "select " $domainname; echo "use_profile " $profile ) |
9b244373 1047 * /usr/sbin/tomoyo-loadpolicy -d
fdb8ebb7
TH
1048 *
1049 * Caller holds tomoyo_read_lock().
9590837b
KT
1050 */
1051static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1052{
1053 char *data = head->write_buf;
1054 char *cp = strchr(data, ' ');
1055 struct tomoyo_domain_info *domain;
1056 unsigned long profile;
1057
1058 if (!cp)
1059 return -EINVAL;
1060 *cp = '\0';
9590837b 1061 domain = tomoyo_find_domain(cp + 1);
9590837b
KT
1062 if (strict_strtoul(data, 10, &profile))
1063 return -EINVAL;
1064 if (domain && profile < TOMOYO_MAX_PROFILES
1065 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1066 domain->profile = (u8) profile;
1067 return 0;
1068}
1069
1070/**
1071 * tomoyo_read_domain_profile - Read only domainname and profile.
1072 *
1073 * @head: Pointer to "struct tomoyo_io_buffer".
1074 *
1075 * Returns list of profile number and domainname pairs.
1076 *
1077 * This is equivalent to doing
1078 *
1079 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1080 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1081 * domainname = $0; } else if ( $1 == "use_profile" ) {
1082 * print $2 " " domainname; domainname = ""; } } ; '
fdb8ebb7
TH
1083 *
1084 * Caller holds tomoyo_read_lock().
9590837b 1085 */
8fbe71f0 1086static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
9590837b
KT
1087{
1088 struct list_head *pos;
1089 bool done = true;
1090
1091 if (head->read_eof)
8fbe71f0 1092 return;
9590837b
KT
1093 list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
1094 struct tomoyo_domain_info *domain;
1095 domain = list_entry(pos, struct tomoyo_domain_info, list);
1096 if (domain->is_deleted)
1097 continue;
7d2948b1
TH
1098 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1099 domain->domainname->name);
1100 if (!done)
9590837b 1101 break;
9590837b 1102 }
9590837b 1103 head->read_eof = done;
9590837b
KT
1104}
1105
1106/**
1107 * tomoyo_write_pid: Specify PID to obtain domainname.
1108 *
1109 * @head: Pointer to "struct tomoyo_io_buffer".
1110 *
1111 * Returns 0.
1112 */
1113static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1114{
1115 unsigned long pid;
1116 /* No error check. */
1117 strict_strtoul(head->write_buf, 10, &pid);
1118 head->read_step = (int) pid;
1119 head->read_eof = false;
1120 return 0;
1121}
1122
1123/**
1124 * tomoyo_read_pid - Get domainname of the specified PID.
1125 *
1126 * @head: Pointer to "struct tomoyo_io_buffer".
1127 *
1128 * Returns the domainname which the specified PID is in on success,
1129 * empty string otherwise.
1130 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1131 * using read()/write() interface rather than sysctl() interface.
1132 */
8fbe71f0 1133static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
9590837b
KT
1134{
1135 if (head->read_avail == 0 && !head->read_eof) {
1136 const int pid = head->read_step;
1137 struct task_struct *p;
1138 struct tomoyo_domain_info *domain = NULL;
1fcdc7c5 1139 rcu_read_lock();
9590837b
KT
1140 read_lock(&tasklist_lock);
1141 p = find_task_by_vpid(pid);
1142 if (p)
1143 domain = tomoyo_real_domain(p);
1144 read_unlock(&tasklist_lock);
1fcdc7c5 1145 rcu_read_unlock();
9590837b
KT
1146 if (domain)
1147 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
1148 domain->domainname->name);
1149 head->read_eof = true;
1150 }
9590837b
KT
1151}
1152
1153/**
1154 * tomoyo_write_exception_policy - Write exception policy.
1155 *
1156 * @head: Pointer to "struct tomoyo_io_buffer".
1157 *
1158 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
1159 *
1160 * Caller holds tomoyo_read_lock().
9590837b
KT
1161 */
1162static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1163{
1164 char *data = head->write_buf;
1165 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1166
1167 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
1168 return tomoyo_write_domain_keeper_policy(data, false,
1169 is_delete);
1170 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
1171 return tomoyo_write_domain_keeper_policy(data, true, is_delete);
1172 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
1173 return tomoyo_write_domain_initializer_policy(data, false,
1174 is_delete);
1175 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
1176 return tomoyo_write_domain_initializer_policy(data, true,
1177 is_delete);
1084307c
TH
1178 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR))
1179 return tomoyo_write_aggregator_policy(data, is_delete);
9590837b
KT
1180 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1181 return tomoyo_write_globally_readable_policy(data, is_delete);
1182 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1183 return tomoyo_write_pattern_policy(data, is_delete);
1184 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1185 return tomoyo_write_no_rewrite_policy(data, is_delete);
7762fbff 1186 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
7c2ea22e 1187 return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
4c3e9e2d 1188 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
7c2ea22e 1189 return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
9590837b
KT
1190 return -EINVAL;
1191}
1192
31845e8c
TH
1193static void tomoyo_print_number(char *buffer, int buffer_len,
1194 const struct tomoyo_number_union *ptr)
1195{
1196 int i;
1197 unsigned long min = ptr->values[0];
1198 const unsigned long max = ptr->values[1];
1199 u8 min_type = ptr->min_type;
1200 const u8 max_type = ptr->max_type;
1201 memset(buffer, 0, buffer_len);
1202 buffer_len -= 2;
1203 for (i = 0; i < 2; i++) {
1204 int len;
1205 switch (min_type) {
1206 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
1207 snprintf(buffer, buffer_len, "0x%lX", min);
1208 break;
1209 case TOMOYO_VALUE_TYPE_OCTAL:
1210 snprintf(buffer, buffer_len, "0%lo", min);
1211 break;
1212 default:
1213 snprintf(buffer, buffer_len, "%lu", min);
1214 break;
1215 }
1216 if (min == max && min_type == max_type)
1217 break;
1218 len = strlen(buffer);
1219 buffer[len++] = '-';
1220 buffer += len;
1221 buffer_len -= len;
1222 min_type = max_type;
1223 min = max;
1224 }
1225}
1226
1227static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
1228 [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
1229 [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
1230};
1231
9590837b 1232/**
31845e8c 1233 * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
9590837b
KT
1234 *
1235 * @head: Pointer to "struct tomoyo_io_buffer".
31845e8c
TH
1236 * @idx: Index number.
1237 *
1238 * Returns true on success, false otherwise.
9590837b 1239 *
fdb8ebb7 1240 * Caller holds tomoyo_read_lock().
9590837b 1241 */
31845e8c 1242static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
9590837b 1243{
31845e8c
TH
1244 struct list_head *gpos;
1245 struct list_head *mpos;
1246 const char *w[3] = { "", "", "" };
1247 w[0] = tomoyo_group_name[idx];
1248 list_for_each_cookie(gpos, head->read_var1, &tomoyo_group_list[idx]) {
1249 struct tomoyo_group *group =
1250 list_entry(gpos, struct tomoyo_group, list);
1251 w[1] = group->group_name->name;
1252 list_for_each_cookie(mpos, head->read_var2,
1253 &group->member_list) {
1254 char buffer[128];
1255 struct tomoyo_acl_head *ptr =
1256 list_entry(mpos, struct tomoyo_acl_head, list);
1257 if (ptr->is_deleted)
1258 continue;
1259 if (idx == TOMOYO_PATH_GROUP) {
1260 w[2] = container_of(ptr,
1261 struct tomoyo_path_group,
1262 head)->member_name->name;
1263 } else if (idx == TOMOYO_NUMBER_GROUP) {
1264 tomoyo_print_number(buffer, sizeof(buffer),
1265 &container_of
1266 (ptr, struct
1267 tomoyo_number_group,
1268 head)->number);
1269 w[2] = buffer;
1270 }
1271 if (!tomoyo_io_printf(head, "%s%s %s\n", w[0], w[1],
1272 w[2]))
1273 return false;
1274 }
1275 }
1276 return true;
1277}
1278
1279/**
1280 * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
1281 *
1282 * @head: Pointer to "struct tomoyo_io_buffer".
1283 * @idx: Index number.
1284 *
1285 * Returns true on success, false otherwise.
1286 *
1287 * Caller holds tomoyo_read_lock().
1288 */
1289static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
1290{
1291 struct list_head *pos;
1292 list_for_each_cookie(pos, head->read_var2, &tomoyo_policy_list[idx]) {
1293 const char *w[4] = { "", "", "", "" };
1294 struct tomoyo_acl_head *acl = container_of(pos, typeof(*acl),
1295 list);
1296 if (acl->is_deleted)
1297 continue;
1298 switch (idx) {
1299 case TOMOYO_ID_DOMAIN_KEEPER:
1300 {
1301 struct tomoyo_domain_keeper_entry *ptr =
1302 container_of(acl, typeof(*ptr), head);
1303 w[0] = ptr->is_not ?
1304 TOMOYO_KEYWORD_NO_KEEP_DOMAIN :
1305 TOMOYO_KEYWORD_KEEP_DOMAIN;
1306 if (ptr->program) {
1307 w[1] = ptr->program->name;
1308 w[2] = " from ";
1309 }
1310 w[3] = ptr->domainname->name;
1311 }
9590837b 1312 break;
31845e8c
TH
1313 case TOMOYO_ID_DOMAIN_INITIALIZER:
1314 {
1315 struct tomoyo_domain_initializer_entry *ptr =
1316 container_of(acl, typeof(*ptr), head);
1317 w[0] = ptr->is_not ?
1318 TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN :
1319 TOMOYO_KEYWORD_INITIALIZE_DOMAIN;
1320 w[1] = ptr->program->name;
1321 if (ptr->domainname) {
1322 w[2] = " from ";
1323 w[3] = ptr->domainname->name;
1324 }
1325 }
1326 break;
1327 case TOMOYO_ID_GLOBALLY_READABLE:
1328 {
1329 struct tomoyo_globally_readable_file_entry *ptr
1330 = container_of(acl, typeof(*ptr), head);
1331 w[0] = TOMOYO_KEYWORD_ALLOW_READ;
1332 w[1] = ptr->filename->name;
1333 }
1334 break;
31845e8c
TH
1335 case TOMOYO_ID_AGGREGATOR:
1336 {
1337 struct tomoyo_aggregator_entry *ptr =
1338 container_of(acl, typeof(*ptr), head);
1339 w[0] = TOMOYO_KEYWORD_AGGREGATOR;
1340 w[1] = ptr->original_name->name;
1341 w[2] = " ";
1342 w[3] = ptr->aggregated_name->name;
1343 }
1344 break;
1345 case TOMOYO_ID_PATTERN:
1346 {
1347 struct tomoyo_pattern_entry *ptr =
1348 container_of(acl, typeof(*ptr), head);
1349 w[0] = TOMOYO_KEYWORD_FILE_PATTERN;
1350 w[1] = ptr->pattern->name;
1351 }
1352 break;
1353 case TOMOYO_ID_NO_REWRITE:
1354 {
1355 struct tomoyo_no_rewrite_entry *ptr =
1356 container_of(acl, typeof(*ptr), head);
1357 w[0] = TOMOYO_KEYWORD_DENY_REWRITE;
1358 w[1] = ptr->pattern->name;
1359 }
1360 break;
1361 default:
1362 continue;
9590837b 1363 }
31845e8c
TH
1364 if (!tomoyo_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2],
1365 w[3]))
1366 return false;
9590837b 1367 }
31845e8c
TH
1368 return true;
1369}
1370
1371/**
1372 * tomoyo_read_exception_policy - Read exception policy.
1373 *
1374 * @head: Pointer to "struct tomoyo_io_buffer".
1375 *
1376 * Caller holds tomoyo_read_lock().
1377 */
1378static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
1379{
1380 if (head->read_eof)
1381 return;
1382 while (head->read_step < TOMOYO_MAX_POLICY &&
1383 tomoyo_read_policy(head, head->read_step))
1384 head->read_step++;
1385 if (head->read_step < TOMOYO_MAX_POLICY)
1386 return;
1387 while (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
1388 tomoyo_read_group(head, head->read_step - TOMOYO_MAX_POLICY))
1389 head->read_step++;
1390 if (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
1391 return;
1392 head->read_eof = true;
9590837b
KT
1393}
1394
17fcfbd9
TH
1395/**
1396 * tomoyo_print_header - Get header line of audit log.
1397 *
1398 * @r: Pointer to "struct tomoyo_request_info".
1399 *
1400 * Returns string representation.
1401 *
1402 * This function uses kmalloc(), so caller must kfree() if this function
1403 * didn't return NULL.
1404 */
1405static char *tomoyo_print_header(struct tomoyo_request_info *r)
1406{
1407 static const char *tomoyo_mode_4[4] = {
1408 "disabled", "learning", "permissive", "enforcing"
1409 };
1410 struct timeval tv;
1411 const pid_t gpid = task_pid_nr(current);
1412 static const int tomoyo_buffer_len = 4096;
1413 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
1414 if (!buffer)
1415 return NULL;
1416 do_gettimeofday(&tv);
1417 snprintf(buffer, tomoyo_buffer_len - 1,
1418 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
1419 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
1420 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
1421 tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
1422 (pid_t) sys_getpid(), (pid_t) sys_getppid(),
1423 current_uid(), current_gid(), current_euid(),
1424 current_egid(), current_suid(), current_sgid(),
1425 current_fsuid(), current_fsgid());
1426 return buffer;
1427}
1428
1429/**
1430 * tomoyo_init_audit_log - Allocate buffer for audit logs.
1431 *
1432 * @len: Required size.
1433 * @r: Pointer to "struct tomoyo_request_info".
1434 *
1435 * Returns pointer to allocated memory.
1436 *
1437 * The @len is updated to add the header lines' size on success.
1438 *
1439 * This function uses kzalloc(), so caller must kfree() if this function
1440 * didn't return NULL.
1441 */
1442static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
1443{
1444 char *buf = NULL;
1445 const char *header;
1446 const char *domainname;
1447 if (!r->domain)
1448 r->domain = tomoyo_domain();
1449 domainname = r->domain->domainname->name;
1450 header = tomoyo_print_header(r);
1451 if (!header)
1452 return NULL;
1453 *len += strlen(domainname) + strlen(header) + 10;
1454 buf = kzalloc(*len, GFP_NOFS);
1455 if (buf)
1456 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
1457 kfree(header);
1458 return buf;
1459}
1460
1461/* Wait queue for tomoyo_query_list. */
1462static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
1463
1464/* Lock for manipulating tomoyo_query_list. */
1465static DEFINE_SPINLOCK(tomoyo_query_list_lock);
1466
1467/* Structure for query. */
1468struct tomoyo_query_entry {
1469 struct list_head list;
1470 char *query;
1471 int query_len;
1472 unsigned int serial;
1473 int timer;
1474 int answer;
1475};
1476
1477/* The list for "struct tomoyo_query_entry". */
1478static LIST_HEAD(tomoyo_query_list);
1479
1480/*
1481 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
1482 * interface.
1483 */
1484static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
1485
1486/**
1487 * tomoyo_supervisor - Ask for the supervisor's decision.
1488 *
1489 * @r: Pointer to "struct tomoyo_request_info".
1490 * @fmt: The printf()'s format string, followed by parameters.
1491 *
1492 * Returns 0 if the supervisor decided to permit the access request which
1493 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
1494 * supervisor decided to retry the access request which violated the policy in
1495 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1496 */
1497int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
1498{
1499 va_list args;
1500 int error = -EPERM;
1501 int pos;
1502 int len;
1503 static unsigned int tomoyo_serial;
1504 struct tomoyo_query_entry *tomoyo_query_entry = NULL;
1505 bool quota_exceeded = false;
1506 char *header;
1507 switch (r->mode) {
1508 char *buffer;
1509 case TOMOYO_CONFIG_LEARNING:
1510 if (!tomoyo_domain_quota_is_ok(r))
1511 return 0;
1512 va_start(args, fmt);
1513 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
1514 va_end(args);
1515 buffer = kmalloc(len, GFP_NOFS);
1516 if (!buffer)
1517 return 0;
1518 va_start(args, fmt);
1519 vsnprintf(buffer, len - 1, fmt, args);
1520 va_end(args);
1521 tomoyo_normalize_line(buffer);
1522 tomoyo_write_domain_policy2(buffer, r->domain, false);
1523 kfree(buffer);
1524 /* fall through */
1525 case TOMOYO_CONFIG_PERMISSIVE:
1526 return 0;
1527 }
1528 if (!r->domain)
1529 r->domain = tomoyo_domain();
1530 if (!atomic_read(&tomoyo_query_observers))
1531 return -EPERM;
1532 va_start(args, fmt);
1533 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
1534 va_end(args);
1535 header = tomoyo_init_audit_log(&len, r);
1536 if (!header)
1537 goto out;
1538 tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
1539 if (!tomoyo_query_entry)
1540 goto out;
1541 tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
1542 if (!tomoyo_query_entry->query)
1543 goto out;
1544 len = ksize(tomoyo_query_entry->query);
1545 INIT_LIST_HEAD(&tomoyo_query_entry->list);
1546 spin_lock(&tomoyo_query_list_lock);
1547 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
1548 sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
1549 quota_exceeded = true;
1550 } else {
1551 tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
1552 tomoyo_query_entry->serial = tomoyo_serial++;
1553 }
1554 spin_unlock(&tomoyo_query_list_lock);
1555 if (quota_exceeded)
1556 goto out;
1557 pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
1558 tomoyo_query_entry->serial, r->retry, header);
1559 kfree(header);
1560 header = NULL;
1561 va_start(args, fmt);
1562 vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
1563 tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
1564 va_end(args);
1565 spin_lock(&tomoyo_query_list_lock);
1566 list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
1567 spin_unlock(&tomoyo_query_list_lock);
1568 /* Give 10 seconds for supervisor's opinion. */
1569 for (tomoyo_query_entry->timer = 0;
1570 atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
1571 tomoyo_query_entry->timer++) {
1572 wake_up(&tomoyo_query_wait);
1573 set_current_state(TASK_INTERRUPTIBLE);
1574 schedule_timeout(HZ / 10);
1575 if (tomoyo_query_entry->answer)
1576 break;
1577 }
1578 spin_lock(&tomoyo_query_list_lock);
1579 list_del(&tomoyo_query_entry->list);
1580 tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
1581 spin_unlock(&tomoyo_query_list_lock);
1582 switch (tomoyo_query_entry->answer) {
1583 case 3: /* Asked to retry by administrator. */
1584 error = TOMOYO_RETRY_REQUEST;
1585 r->retry++;
1586 break;
1587 case 1:
1588 /* Granted by administrator. */
1589 error = 0;
1590 break;
1591 case 0:
1592 /* Timed out. */
1593 break;
1594 default:
1595 /* Rejected by administrator. */
1596 break;
1597 }
1598 out:
1599 if (tomoyo_query_entry)
1600 kfree(tomoyo_query_entry->query);
1601 kfree(tomoyo_query_entry);
1602 kfree(header);
1603 return error;
1604}
1605
1606/**
1607 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
1608 *
1609 * @file: Pointer to "struct file".
1610 * @wait: Pointer to "poll_table".
1611 *
1612 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
1613 *
1614 * Waits for access requests which violated policy in enforcing mode.
1615 */
1616static int tomoyo_poll_query(struct file *file, poll_table *wait)
1617{
1618 struct list_head *tmp;
1619 bool found = false;
1620 u8 i;
1621 for (i = 0; i < 2; i++) {
1622 spin_lock(&tomoyo_query_list_lock);
1623 list_for_each(tmp, &tomoyo_query_list) {
1624 struct tomoyo_query_entry *ptr
1625 = list_entry(tmp, struct tomoyo_query_entry,
1626 list);
1627 if (ptr->answer)
1628 continue;
1629 found = true;
1630 break;
1631 }
1632 spin_unlock(&tomoyo_query_list_lock);
1633 if (found)
1634 return POLLIN | POLLRDNORM;
1635 if (i)
1636 break;
1637 poll_wait(file, &tomoyo_query_wait, wait);
1638 }
1639 return 0;
1640}
1641
1642/**
1643 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
1644 *
1645 * @head: Pointer to "struct tomoyo_io_buffer".
17fcfbd9 1646 */
8fbe71f0 1647static void tomoyo_read_query(struct tomoyo_io_buffer *head)
17fcfbd9
TH
1648{
1649 struct list_head *tmp;
1650 int pos = 0;
1651 int len = 0;
1652 char *buf;
1653 if (head->read_avail)
8fbe71f0 1654 return;
17fcfbd9
TH
1655 if (head->read_buf) {
1656 kfree(head->read_buf);
1657 head->read_buf = NULL;
1658 head->readbuf_size = 0;
1659 }
1660 spin_lock(&tomoyo_query_list_lock);
1661 list_for_each(tmp, &tomoyo_query_list) {
1662 struct tomoyo_query_entry *ptr
1663 = list_entry(tmp, struct tomoyo_query_entry, list);
1664 if (ptr->answer)
1665 continue;
1666 if (pos++ != head->read_step)
1667 continue;
1668 len = ptr->query_len;
1669 break;
1670 }
1671 spin_unlock(&tomoyo_query_list_lock);
1672 if (!len) {
1673 head->read_step = 0;
8fbe71f0 1674 return;
17fcfbd9
TH
1675 }
1676 buf = kzalloc(len, GFP_NOFS);
1677 if (!buf)
8fbe71f0 1678 return;
17fcfbd9
TH
1679 pos = 0;
1680 spin_lock(&tomoyo_query_list_lock);
1681 list_for_each(tmp, &tomoyo_query_list) {
1682 struct tomoyo_query_entry *ptr
1683 = list_entry(tmp, struct tomoyo_query_entry, list);
1684 if (ptr->answer)
1685 continue;
1686 if (pos++ != head->read_step)
1687 continue;
1688 /*
1689 * Some query can be skipped because tomoyo_query_list
1690 * can change, but I don't care.
1691 */
1692 if (len == ptr->query_len)
1693 memmove(buf, ptr->query, len);
1694 break;
1695 }
1696 spin_unlock(&tomoyo_query_list_lock);
1697 if (buf[0]) {
1698 head->read_avail = len;
1699 head->readbuf_size = head->read_avail;
1700 head->read_buf = buf;
1701 head->read_step++;
1702 } else {
1703 kfree(buf);
1704 }
17fcfbd9
TH
1705}
1706
1707/**
1708 * tomoyo_write_answer - Write the supervisor's decision.
1709 *
1710 * @head: Pointer to "struct tomoyo_io_buffer".
1711 *
1712 * Returns 0 on success, -EINVAL otherwise.
1713 */
1714static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
1715{
1716 char *data = head->write_buf;
1717 struct list_head *tmp;
1718 unsigned int serial;
1719 unsigned int answer;
1720 spin_lock(&tomoyo_query_list_lock);
1721 list_for_each(tmp, &tomoyo_query_list) {
1722 struct tomoyo_query_entry *ptr
1723 = list_entry(tmp, struct tomoyo_query_entry, list);
1724 ptr->timer = 0;
1725 }
1726 spin_unlock(&tomoyo_query_list_lock);
1727 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
1728 return -EINVAL;
1729 spin_lock(&tomoyo_query_list_lock);
1730 list_for_each(tmp, &tomoyo_query_list) {
1731 struct tomoyo_query_entry *ptr
1732 = list_entry(tmp, struct tomoyo_query_entry, list);
1733 if (ptr->serial != serial)
1734 continue;
1735 if (!ptr->answer)
1736 ptr->answer = answer;
1737 break;
1738 }
1739 spin_unlock(&tomoyo_query_list_lock);
1740 return 0;
1741}
1742
9590837b
KT
1743/**
1744 * tomoyo_read_version: Get version.
1745 *
1746 * @head: Pointer to "struct tomoyo_io_buffer".
1747 *
1748 * Returns version information.
1749 */
8fbe71f0 1750static void tomoyo_read_version(struct tomoyo_io_buffer *head)
9590837b
KT
1751{
1752 if (!head->read_eof) {
57c2590f 1753 tomoyo_io_printf(head, "2.3.0-pre");
9590837b
KT
1754 head->read_eof = true;
1755 }
9590837b
KT
1756}
1757
1758/**
1759 * tomoyo_read_self_domain - Get the current process's domainname.
1760 *
1761 * @head: Pointer to "struct tomoyo_io_buffer".
1762 *
1763 * Returns the current process's domainname.
1764 */
8fbe71f0 1765static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
9590837b
KT
1766{
1767 if (!head->read_eof) {
1768 /*
1769 * tomoyo_domain()->domainname != NULL
1770 * because every process belongs to a domain and
1771 * the domain's name cannot be NULL.
1772 */
1773 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1774 head->read_eof = true;
1775 }
9590837b
KT
1776}
1777
1778/**
1779 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1780 *
1781 * @type: Type of interface.
1782 * @file: Pointer to "struct file".
1783 *
1784 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
fdb8ebb7
TH
1785 *
1786 * Caller acquires tomoyo_read_lock().
9590837b 1787 */
c3ef1500 1788int tomoyo_open_control(const u8 type, struct file *file)
9590837b 1789{
4e5d6f7e 1790 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
9590837b
KT
1791
1792 if (!head)
1793 return -ENOMEM;
1794 mutex_init(&head->io_sem);
17fcfbd9 1795 head->type = type;
9590837b
KT
1796 switch (type) {
1797 case TOMOYO_DOMAINPOLICY:
1798 /* /sys/kernel/security/tomoyo/domain_policy */
1799 head->write = tomoyo_write_domain_policy;
1800 head->read = tomoyo_read_domain_policy;
1801 break;
1802 case TOMOYO_EXCEPTIONPOLICY:
1803 /* /sys/kernel/security/tomoyo/exception_policy */
1804 head->write = tomoyo_write_exception_policy;
1805 head->read = tomoyo_read_exception_policy;
1806 break;
1807 case TOMOYO_SELFDOMAIN:
1808 /* /sys/kernel/security/tomoyo/self_domain */
1809 head->read = tomoyo_read_self_domain;
1810 break;
1811 case TOMOYO_DOMAIN_STATUS:
1812 /* /sys/kernel/security/tomoyo/.domain_status */
1813 head->write = tomoyo_write_domain_profile;
1814 head->read = tomoyo_read_domain_profile;
1815 break;
1816 case TOMOYO_PROCESS_STATUS:
1817 /* /sys/kernel/security/tomoyo/.process_status */
1818 head->write = tomoyo_write_pid;
1819 head->read = tomoyo_read_pid;
1820 break;
1821 case TOMOYO_VERSION:
1822 /* /sys/kernel/security/tomoyo/version */
1823 head->read = tomoyo_read_version;
1824 head->readbuf_size = 128;
1825 break;
1826 case TOMOYO_MEMINFO:
1827 /* /sys/kernel/security/tomoyo/meminfo */
1828 head->write = tomoyo_write_memory_quota;
1829 head->read = tomoyo_read_memory_counter;
1830 head->readbuf_size = 512;
1831 break;
1832 case TOMOYO_PROFILE:
1833 /* /sys/kernel/security/tomoyo/profile */
1834 head->write = tomoyo_write_profile;
1835 head->read = tomoyo_read_profile;
1836 break;
17fcfbd9
TH
1837 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
1838 head->poll = tomoyo_poll_query;
1839 head->write = tomoyo_write_answer;
1840 head->read = tomoyo_read_query;
1841 break;
9590837b
KT
1842 case TOMOYO_MANAGER:
1843 /* /sys/kernel/security/tomoyo/manager */
1844 head->write = tomoyo_write_manager_policy;
1845 head->read = tomoyo_read_manager_policy;
1846 break;
1847 }
1848 if (!(file->f_mode & FMODE_READ)) {
1849 /*
1850 * No need to allocate read_buf since it is not opened
1851 * for reading.
1852 */
1853 head->read = NULL;
17fcfbd9
TH
1854 head->poll = NULL;
1855 } else if (!head->poll) {
1856 /* Don't allocate read_buf for poll() access. */
9590837b
KT
1857 if (!head->readbuf_size)
1858 head->readbuf_size = 4096 * 2;
4e5d6f7e 1859 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
9590837b 1860 if (!head->read_buf) {
8e2d39a1 1861 kfree(head);
9590837b
KT
1862 return -ENOMEM;
1863 }
1864 }
1865 if (!(file->f_mode & FMODE_WRITE)) {
1866 /*
1867 * No need to allocate write_buf since it is not opened
1868 * for writing.
1869 */
1870 head->write = NULL;
1871 } else if (head->write) {
1872 head->writebuf_size = 4096 * 2;
4e5d6f7e 1873 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
9590837b 1874 if (!head->write_buf) {
8e2d39a1
TH
1875 kfree(head->read_buf);
1876 kfree(head);
9590837b
KT
1877 return -ENOMEM;
1878 }
1879 }
17fcfbd9
TH
1880 if (type != TOMOYO_QUERY)
1881 head->reader_idx = tomoyo_read_lock();
9590837b
KT
1882 file->private_data = head;
1883 /*
1884 * Call the handler now if the file is
1885 * /sys/kernel/security/tomoyo/self_domain
1886 * so that the user can use
1887 * cat < /sys/kernel/security/tomoyo/self_domain"
1888 * to know the current process's domainname.
1889 */
1890 if (type == TOMOYO_SELFDOMAIN)
1891 tomoyo_read_control(file, NULL, 0);
17fcfbd9
TH
1892 /*
1893 * If the file is /sys/kernel/security/tomoyo/query , increment the
1894 * observer counter.
1895 * The obserber counter is used by tomoyo_supervisor() to see if
1896 * there is some process monitoring /sys/kernel/security/tomoyo/query.
1897 */
1898 else if (type == TOMOYO_QUERY)
1899 atomic_inc(&tomoyo_query_observers);
9590837b
KT
1900 return 0;
1901}
1902
17fcfbd9
TH
1903/**
1904 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
1905 *
1906 * @file: Pointer to "struct file".
1907 * @wait: Pointer to "poll_table".
1908 *
1909 * Waits for read readiness.
1910 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
1911 */
1912int tomoyo_poll_control(struct file *file, poll_table *wait)
1913{
1914 struct tomoyo_io_buffer *head = file->private_data;
1915 if (!head->poll)
1916 return -ENOSYS;
1917 return head->poll(file, wait);
1918}
1919
9590837b
KT
1920/**
1921 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1922 *
1923 * @file: Pointer to "struct file".
1924 * @buffer: Poiner to buffer to write to.
1925 * @buffer_len: Size of @buffer.
1926 *
1927 * Returns bytes read on success, negative value otherwise.
fdb8ebb7
TH
1928 *
1929 * Caller holds tomoyo_read_lock().
9590837b 1930 */
c3ef1500
TH
1931int tomoyo_read_control(struct file *file, char __user *buffer,
1932 const int buffer_len)
9590837b
KT
1933{
1934 int len = 0;
1935 struct tomoyo_io_buffer *head = file->private_data;
1936 char *cp;
1937
1938 if (!head->read)
1939 return -ENOSYS;
1940 if (mutex_lock_interruptible(&head->io_sem))
1941 return -EINTR;
1942 /* Call the policy handler. */
8fbe71f0 1943 head->read(head);
9590837b
KT
1944 if (len < 0)
1945 goto out;
1946 /* Write to buffer. */
1947 len = head->read_avail;
1948 if (len > buffer_len)
1949 len = buffer_len;
1950 if (!len)
1951 goto out;
1952 /* head->read_buf changes by some functions. */
1953 cp = head->read_buf;
1954 if (copy_to_user(buffer, cp, len)) {
1955 len = -EFAULT;
1956 goto out;
1957 }
1958 head->read_avail -= len;
1959 memmove(cp, cp + len, head->read_avail);
1960 out:
1961 mutex_unlock(&head->io_sem);
1962 return len;
1963}
1964
1965/**
1966 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1967 *
1968 * @file: Pointer to "struct file".
1969 * @buffer: Pointer to buffer to read from.
1970 * @buffer_len: Size of @buffer.
1971 *
1972 * Returns @buffer_len on success, negative value otherwise.
fdb8ebb7
TH
1973 *
1974 * Caller holds tomoyo_read_lock().
9590837b 1975 */
c3ef1500
TH
1976int tomoyo_write_control(struct file *file, const char __user *buffer,
1977 const int buffer_len)
9590837b
KT
1978{
1979 struct tomoyo_io_buffer *head = file->private_data;
1980 int error = buffer_len;
1981 int avail_len = buffer_len;
1982 char *cp0 = head->write_buf;
1983
1984 if (!head->write)
1985 return -ENOSYS;
1986 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1987 return -EFAULT;
1988 /* Don't allow updating policies by non manager programs. */
1989 if (head->write != tomoyo_write_pid &&
1990 head->write != tomoyo_write_domain_policy &&
75093152 1991 !tomoyo_policy_manager())
9590837b
KT
1992 return -EPERM;
1993 if (mutex_lock_interruptible(&head->io_sem))
1994 return -EINTR;
1995 /* Read a line and dispatch it to the policy handler. */
1996 while (avail_len > 0) {
1997 char c;
1998 if (head->write_avail >= head->writebuf_size - 1) {
1999 error = -ENOMEM;
2000 break;
2001 } else if (get_user(c, buffer)) {
2002 error = -EFAULT;
2003 break;
2004 }
2005 buffer++;
2006 avail_len--;
2007 cp0[head->write_avail++] = c;
2008 if (c != '\n')
2009 continue;
2010 cp0[head->write_avail - 1] = '\0';
2011 head->write_avail = 0;
2012 tomoyo_normalize_line(cp0);
2013 head->write(head);
2014 }
2015 mutex_unlock(&head->io_sem);
2016 return error;
2017}
2018
2019/**
2020 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2021 *
2022 * @file: Pointer to "struct file".
2023 *
2024 * Releases memory and returns 0.
fdb8ebb7
TH
2025 *
2026 * Caller looses tomoyo_read_lock().
9590837b 2027 */
c3ef1500 2028int tomoyo_close_control(struct file *file)
9590837b
KT
2029{
2030 struct tomoyo_io_buffer *head = file->private_data;
847b173e 2031 const bool is_write = !!head->write_buf;
9590837b 2032
17fcfbd9
TH
2033 /*
2034 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2035 * observer counter.
2036 */
2037 if (head->type == TOMOYO_QUERY)
2038 atomic_dec(&tomoyo_query_observers);
2039 else
2040 tomoyo_read_unlock(head->reader_idx);
9590837b 2041 /* Release memory used for policy I/O. */
8e2d39a1 2042 kfree(head->read_buf);
9590837b 2043 head->read_buf = NULL;
8e2d39a1 2044 kfree(head->write_buf);
9590837b 2045 head->write_buf = NULL;
8e2d39a1 2046 kfree(head);
9590837b
KT
2047 head = NULL;
2048 file->private_data = NULL;
847b173e
TH
2049 if (is_write)
2050 tomoyo_run_gc();
9590837b
KT
2051 return 0;
2052}
2053
9590837b 2054/**
c3ef1500 2055 * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
9590837b 2056 */
c3ef1500 2057void tomoyo_check_profile(void)
9590837b 2058{
c3ef1500
TH
2059 struct tomoyo_domain_info *domain;
2060 const int idx = tomoyo_read_lock();
2061 tomoyo_policy_loaded = true;
2062 /* Check all profiles currently assigned to domains are defined. */
2063 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2064 const u8 profile = domain->profile;
2065 if (tomoyo_profile_ptr[profile])
2066 continue;
2067 panic("Profile %u (used by '%s') not defined.\n",
2068 profile, domain->domainname->name);
2069 }
2070 tomoyo_read_unlock(idx);
57c2590f
TH
2071 if (tomoyo_profile_version != 20090903)
2072 panic("Profile version %u is not supported.\n",
2073 tomoyo_profile_version);
2074 printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n");
c3ef1500 2075 printk(KERN_INFO "Mandatory Access Control activated.\n");
9590837b 2076}