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