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