blob: a44f655b3913cc2e9c0938a9e0e44399c6181564 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.c
3 *
4 * Common functions for TOMOYO.
5 *
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 *
Tetsuo Handa39826a12009-04-08 22:31:28 +09008 * Version: 2.2.0 2009/04/01
Kentaro Takeda95908372009-02-05 17:18:13 +09009 *
10 */
11
12#include <linux/uaccess.h>
13#include <linux/security.h>
14#include <linux/hardirq.h>
15#include "realpath.h"
16#include "common.h"
17#include "tomoyo.h"
18
19/* Has loading policy done? */
20bool tomoyo_policy_loaded;
21
22/* String table for functionality that takes 4 modes. */
23static const char *tomoyo_mode_4[4] = {
24 "disabled", "learning", "permissive", "enforcing"
25};
26/* String table for functionality that takes 2 modes. */
27static const char *tomoyo_mode_2[4] = {
28 "disabled", "enabled", "enabled", "enabled"
29};
30
31/* Table for profile. */
32static struct {
33 const char *keyword;
34 unsigned int current_value;
35 const unsigned int max_value;
36} tomoyo_control_array[TOMOYO_MAX_CONTROL_INDEX] = {
37 [TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
38 [TOMOYO_MAX_ACCEPT_ENTRY] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX },
39 [TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
40};
41
42/* Profile table. Memory is allocated as needed. */
43static struct tomoyo_profile {
44 unsigned int value[TOMOYO_MAX_CONTROL_INDEX];
45 const struct tomoyo_path_info *comment;
46} *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
47
48/* Permit policy management by non-root user? */
49static bool tomoyo_manage_by_non_root;
50
51/* Utility functions. */
52
53/* Open operation for /sys/kernel/security/tomoyo/ interface. */
54static int tomoyo_open_control(const u8 type, struct file *file);
55/* Close /sys/kernel/security/tomoyo/ interface. */
56static int tomoyo_close_control(struct file *file);
57/* Read operation for /sys/kernel/security/tomoyo/ interface. */
58static int tomoyo_read_control(struct file *file, char __user *buffer,
59 const int buffer_len);
60/* Write operation for /sys/kernel/security/tomoyo/ interface. */
61static int tomoyo_write_control(struct file *file, const char __user *buffer,
62 const int buffer_len);
63
64/**
65 * tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
66 *
67 * @str: Pointer to the string.
68 *
69 * Returns true if @str is a \ooo style octal value, false otherwise.
70 *
71 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
72 * This function verifies that \ooo is in valid range.
73 */
74static inline bool tomoyo_is_byte_range(const char *str)
75{
76 return *str >= '0' && *str++ <= '3' &&
77 *str >= '0' && *str++ <= '7' &&
78 *str >= '0' && *str <= '7';
79}
80
81/**
82 * tomoyo_is_alphabet_char - Check whether the character is an alphabet.
83 *
84 * @c: The character to check.
85 *
86 * Returns true if @c is an alphabet character, false otherwise.
87 */
88static inline bool tomoyo_is_alphabet_char(const char c)
89{
90 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
91}
92
93/**
94 * tomoyo_make_byte - Make byte value from three octal characters.
95 *
96 * @c1: The first character.
97 * @c2: The second character.
98 * @c3: The third character.
99 *
100 * Returns byte value.
101 */
102static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
103{
104 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
105}
106
107/**
108 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
109 *
110 * @src: Pointer to pointer to the string.
111 * @find: Pointer to the keyword.
112 *
113 * Returns true if @src starts with @find, false otherwise.
114 *
115 * The @src is updated to point the first character after the @find
116 * if @src starts with @find.
117 */
118static bool tomoyo_str_starts(char **src, const char *find)
119{
120 const int len = strlen(find);
121 char *tmp = *src;
122
123 if (strncmp(tmp, find, len))
124 return false;
125 tmp += len;
126 *src = tmp;
127 return true;
128}
129
130/**
131 * tomoyo_normalize_line - Format string.
132 *
133 * @buffer: The line to normalize.
134 *
135 * Leading and trailing whitespaces are removed.
136 * Multiple whitespaces are packed into single space.
137 *
138 * Returns nothing.
139 */
140static void tomoyo_normalize_line(unsigned char *buffer)
141{
142 unsigned char *sp = buffer;
143 unsigned char *dp = buffer;
144 bool first = true;
145
146 while (tomoyo_is_invalid(*sp))
147 sp++;
148 while (*sp) {
149 if (!first)
150 *dp++ = ' ';
151 first = false;
152 while (tomoyo_is_valid(*sp))
153 *dp++ = *sp++;
154 while (tomoyo_is_invalid(*sp))
155 sp++;
156 }
157 *dp = '\0';
158}
159
160/**
161 * tomoyo_is_correct_path - Validate a pathname.
162 * @filename: The pathname to check.
163 * @start_type: Should the pathname start with '/'?
164 * 1 = must / -1 = must not / 0 = don't care
165 * @pattern_type: Can the pathname contain a wildcard?
166 * 1 = must / -1 = must not / 0 = don't care
167 * @end_type: Should the pathname end with '/'?
168 * 1 = must / -1 = must not / 0 = don't care
169 * @function: The name of function calling me.
170 *
171 * Check whether the given filename follows the naming rules.
172 * Returns true if @filename follows the naming rules, false otherwise.
173 */
174bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
175 const s8 pattern_type, const s8 end_type,
176 const char *function)
177{
178 bool contains_pattern = false;
179 unsigned char c;
180 unsigned char d;
181 unsigned char e;
182 const char *original_filename = filename;
183
184 if (!filename)
185 goto out;
186 c = *filename;
187 if (start_type == 1) { /* Must start with '/' */
188 if (c != '/')
189 goto out;
190 } else if (start_type == -1) { /* Must not start with '/' */
191 if (c == '/')
192 goto out;
193 }
194 if (c)
195 c = *(filename + strlen(filename) - 1);
196 if (end_type == 1) { /* Must end with '/' */
197 if (c != '/')
198 goto out;
199 } else if (end_type == -1) { /* Must not end with '/' */
200 if (c == '/')
201 goto out;
202 }
203 while ((c = *filename++) != '\0') {
204 if (c == '\\') {
205 switch ((c = *filename++)) {
206 case '\\': /* "\\" */
207 continue;
208 case '$': /* "\$" */
209 case '+': /* "\+" */
210 case '?': /* "\?" */
211 case '*': /* "\*" */
212 case '@': /* "\@" */
213 case 'x': /* "\x" */
214 case 'X': /* "\X" */
215 case 'a': /* "\a" */
216 case 'A': /* "\A" */
217 case '-': /* "\-" */
218 if (pattern_type == -1)
219 break; /* Must not contain pattern */
220 contains_pattern = true;
221 continue;
222 case '0': /* "\ooo" */
223 case '1':
224 case '2':
225 case '3':
226 d = *filename++;
227 if (d < '0' || d > '7')
228 break;
229 e = *filename++;
230 if (e < '0' || e > '7')
231 break;
232 c = tomoyo_make_byte(c, d, e);
233 if (tomoyo_is_invalid(c))
234 continue; /* pattern is not \000 */
235 }
236 goto out;
237 } else if (tomoyo_is_invalid(c)) {
238 goto out;
239 }
240 }
241 if (pattern_type == 1) { /* Must contain pattern */
242 if (!contains_pattern)
243 goto out;
244 }
245 return true;
246 out:
247 printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function,
248 original_filename);
249 return false;
250}
251
252/**
253 * tomoyo_is_correct_domain - Check whether the given domainname follows the naming rules.
254 * @domainname: The domainname to check.
255 * @function: The name of function calling me.
256 *
257 * Returns true if @domainname follows the naming rules, false otherwise.
258 */
259bool tomoyo_is_correct_domain(const unsigned char *domainname,
260 const char *function)
261{
262 unsigned char c;
263 unsigned char d;
264 unsigned char e;
265 const char *org_domainname = domainname;
266
267 if (!domainname || strncmp(domainname, TOMOYO_ROOT_NAME,
268 TOMOYO_ROOT_NAME_LEN))
269 goto out;
270 domainname += TOMOYO_ROOT_NAME_LEN;
271 if (!*domainname)
272 return true;
273 do {
274 if (*domainname++ != ' ')
275 goto out;
276 if (*domainname++ != '/')
277 goto out;
278 while ((c = *domainname) != '\0' && c != ' ') {
279 domainname++;
280 if (c == '\\') {
281 c = *domainname++;
282 switch ((c)) {
283 case '\\': /* "\\" */
284 continue;
285 case '0': /* "\ooo" */
286 case '1':
287 case '2':
288 case '3':
289 d = *domainname++;
290 if (d < '0' || d > '7')
291 break;
292 e = *domainname++;
293 if (e < '0' || e > '7')
294 break;
295 c = tomoyo_make_byte(c, d, e);
296 if (tomoyo_is_invalid(c))
297 /* pattern is not \000 */
298 continue;
299 }
300 goto out;
301 } else if (tomoyo_is_invalid(c)) {
302 goto out;
303 }
304 }
305 } while (*domainname);
306 return true;
307 out:
308 printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function,
309 org_domainname);
310 return false;
311}
312
313/**
314 * tomoyo_is_domain_def - Check whether the given token can be a domainname.
315 *
316 * @buffer: The token to check.
317 *
318 * Returns true if @buffer possibly be a domainname, false otherwise.
319 */
320bool tomoyo_is_domain_def(const unsigned char *buffer)
321{
322 return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
323}
324
325/**
326 * tomoyo_find_domain - Find a domain by the given name.
327 *
328 * @domainname: The domainname to find.
329 *
330 * Caller must call down_read(&tomoyo_domain_list_lock); or
331 * down_write(&tomoyo_domain_list_lock); .
332 *
333 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
334 */
335struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
336{
337 struct tomoyo_domain_info *domain;
338 struct tomoyo_path_info name;
339
340 name.name = domainname;
341 tomoyo_fill_path_info(&name);
342 list_for_each_entry(domain, &tomoyo_domain_list, list) {
343 if (!domain->is_deleted &&
344 !tomoyo_pathcmp(&name, domain->domainname))
345 return domain;
346 }
347 return NULL;
348}
349
350/**
351 * tomoyo_path_depth - Evaluate the number of '/' in a string.
352 *
353 * @pathname: The string to evaluate.
354 *
355 * Returns path depth of the string.
356 *
357 * I score 2 for each of the '/' in the @pathname
358 * and score 1 if the @pathname ends with '/'.
359 */
360static int tomoyo_path_depth(const char *pathname)
361{
362 int i = 0;
363
364 if (pathname) {
365 const char *ep = pathname + strlen(pathname);
366 if (pathname < ep--) {
367 if (*ep != '/')
368 i++;
369 while (pathname <= ep)
370 if (*ep-- == '/')
371 i += 2;
372 }
373 }
374 return i;
375}
376
377/**
378 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
379 *
380 * @filename: The string to evaluate.
381 *
382 * Returns the initial length without a pattern in @filename.
383 */
384static int tomoyo_const_part_length(const char *filename)
385{
386 char c;
387 int len = 0;
388
389 if (!filename)
390 return 0;
391 while ((c = *filename++) != '\0') {
392 if (c != '\\') {
393 len++;
394 continue;
395 }
396 c = *filename++;
397 switch (c) {
398 case '\\': /* "\\" */
399 len += 2;
400 continue;
401 case '0': /* "\ooo" */
402 case '1':
403 case '2':
404 case '3':
405 c = *filename++;
406 if (c < '0' || c > '7')
407 break;
408 c = *filename++;
409 if (c < '0' || c > '7')
410 break;
411 len += 4;
412 continue;
413 }
414 break;
415 }
416 return len;
417}
418
419/**
420 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
421 *
422 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
423 *
424 * The caller sets "struct tomoyo_path_info"->name.
425 */
426void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
427{
428 const char *name = ptr->name;
429 const int len = strlen(name);
430
Kentaro Takeda95908372009-02-05 17:18:13 +0900431 ptr->const_len = tomoyo_const_part_length(name);
432 ptr->is_dir = len && (name[len - 1] == '/');
433 ptr->is_patterned = (ptr->const_len < len);
434 ptr->hash = full_name_hash(name, len);
435 ptr->depth = tomoyo_path_depth(name);
436}
437
438/**
439 * tomoyo_file_matches_to_pattern2 - Pattern matching without '/' character
440 * and "\-" pattern.
441 *
442 * @filename: The start of string to check.
443 * @filename_end: The end of string to check.
444 * @pattern: The start of pattern to compare.
445 * @pattern_end: The end of pattern to compare.
446 *
447 * Returns true if @filename matches @pattern, false otherwise.
448 */
449static bool tomoyo_file_matches_to_pattern2(const char *filename,
450 const char *filename_end,
451 const char *pattern,
452 const char *pattern_end)
453{
454 while (filename < filename_end && pattern < pattern_end) {
455 char c;
456 if (*pattern != '\\') {
457 if (*filename++ != *pattern++)
458 return false;
459 continue;
460 }
461 c = *filename;
462 pattern++;
463 switch (*pattern) {
464 int i;
465 int j;
466 case '?':
467 if (c == '/') {
468 return false;
469 } else if (c == '\\') {
470 if (filename[1] == '\\')
471 filename++;
472 else if (tomoyo_is_byte_range(filename + 1))
473 filename += 3;
474 else
475 return false;
476 }
477 break;
478 case '\\':
479 if (c != '\\')
480 return false;
481 if (*++filename != '\\')
482 return false;
483 break;
484 case '+':
485 if (!isdigit(c))
486 return false;
487 break;
488 case 'x':
489 if (!isxdigit(c))
490 return false;
491 break;
492 case 'a':
493 if (!tomoyo_is_alphabet_char(c))
494 return false;
495 break;
496 case '0':
497 case '1':
498 case '2':
499 case '3':
500 if (c == '\\' && tomoyo_is_byte_range(filename + 1)
501 && strncmp(filename + 1, pattern, 3) == 0) {
502 filename += 3;
503 pattern += 2;
504 break;
505 }
506 return false; /* Not matched. */
507 case '*':
508 case '@':
509 for (i = 0; i <= filename_end - filename; i++) {
510 if (tomoyo_file_matches_to_pattern2(
511 filename + i, filename_end,
512 pattern + 1, pattern_end))
513 return true;
514 c = filename[i];
515 if (c == '.' && *pattern == '@')
516 break;
517 if (c != '\\')
518 continue;
519 if (filename[i + 1] == '\\')
520 i++;
521 else if (tomoyo_is_byte_range(filename + i + 1))
522 i += 3;
523 else
524 break; /* Bad pattern. */
525 }
526 return false; /* Not matched. */
527 default:
528 j = 0;
529 c = *pattern;
530 if (c == '$') {
531 while (isdigit(filename[j]))
532 j++;
533 } else if (c == 'X') {
534 while (isxdigit(filename[j]))
535 j++;
536 } else if (c == 'A') {
537 while (tomoyo_is_alphabet_char(filename[j]))
538 j++;
539 }
540 for (i = 1; i <= j; i++) {
541 if (tomoyo_file_matches_to_pattern2(
542 filename + i, filename_end,
543 pattern + 1, pattern_end))
544 return true;
545 }
546 return false; /* Not matched or bad pattern. */
547 }
548 filename++;
549 pattern++;
550 }
551 while (*pattern == '\\' &&
552 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
553 pattern += 2;
554 return filename == filename_end && pattern == pattern_end;
555}
556
557/**
558 * tomoyo_file_matches_to_pattern - Pattern matching without without '/' character.
559 *
560 * @filename: The start of string to check.
561 * @filename_end: The end of string to check.
562 * @pattern: The start of pattern to compare.
563 * @pattern_end: The end of pattern to compare.
564 *
565 * Returns true if @filename matches @pattern, false otherwise.
566 */
567static bool tomoyo_file_matches_to_pattern(const char *filename,
568 const char *filename_end,
569 const char *pattern,
570 const char *pattern_end)
571{
572 const char *pattern_start = pattern;
573 bool first = true;
574 bool result;
575
576 while (pattern < pattern_end - 1) {
577 /* Split at "\-" pattern. */
578 if (*pattern++ != '\\' || *pattern++ != '-')
579 continue;
580 result = tomoyo_file_matches_to_pattern2(filename,
581 filename_end,
582 pattern_start,
583 pattern - 2);
584 if (first)
585 result = !result;
586 if (result)
587 return false;
588 first = false;
589 pattern_start = pattern;
590 }
591 result = tomoyo_file_matches_to_pattern2(filename, filename_end,
592 pattern_start, pattern_end);
593 return first ? result : !result;
594}
595
596/**
597 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
598 * @filename: The filename to check.
599 * @pattern: The pattern to compare.
600 *
601 * Returns true if matches, false otherwise.
602 *
603 * The following patterns are available.
604 * \\ \ itself.
605 * \ooo Octal representation of a byte.
606 * \* More than or equals to 0 character other than '/'.
607 * \@ More than or equals to 0 character other than '/' or '.'.
608 * \? 1 byte character other than '/'.
609 * \$ More than or equals to 1 decimal digit.
610 * \+ 1 decimal digit.
611 * \X More than or equals to 1 hexadecimal digit.
612 * \x 1 hexadecimal digit.
613 * \A More than or equals to 1 alphabet character.
614 * \a 1 alphabet character.
615 * \- Subtraction operator.
616 */
617bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
618 const struct tomoyo_path_info *pattern)
619{
620 /*
621 if (!filename || !pattern)
622 return false;
623 */
624 const char *f = filename->name;
625 const char *p = pattern->name;
626 const int len = pattern->const_len;
627
628 /* If @pattern doesn't contain pattern, I can use strcmp(). */
629 if (!pattern->is_patterned)
630 return !tomoyo_pathcmp(filename, pattern);
631 /* Dont compare if the number of '/' differs. */
632 if (filename->depth != pattern->depth)
633 return false;
634 /* Compare the initial length without patterns. */
635 if (strncmp(f, p, len))
636 return false;
637 f += len;
638 p += len;
639 /* Main loop. Compare each directory component. */
640 while (*f && *p) {
641 const char *f_delimiter = strchr(f, '/');
642 const char *p_delimiter = strchr(p, '/');
643 if (!f_delimiter)
644 f_delimiter = f + strlen(f);
645 if (!p_delimiter)
646 p_delimiter = p + strlen(p);
647 if (!tomoyo_file_matches_to_pattern(f, f_delimiter,
648 p, p_delimiter))
649 return false;
650 f = f_delimiter;
651 if (*f)
652 f++;
653 p = p_delimiter;
654 if (*p)
655 p++;
656 }
657 /* Ignore trailing "\*" and "\@" in @pattern. */
658 while (*p == '\\' &&
659 (*(p + 1) == '*' || *(p + 1) == '@'))
660 p += 2;
661 return !*f && !*p;
662}
663
664/**
665 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
666 *
667 * @head: Pointer to "struct tomoyo_io_buffer".
668 * @fmt: The printf()'s format string, followed by parameters.
669 *
670 * Returns true if output was written, false otherwise.
671 *
672 * The snprintf() will truncate, but tomoyo_io_printf() won't.
673 */
674bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
675{
676 va_list args;
677 int len;
678 int pos = head->read_avail;
679 int size = head->readbuf_size - pos;
680
681 if (size <= 0)
682 return false;
683 va_start(args, fmt);
684 len = vsnprintf(head->read_buf + pos, size, fmt, args);
685 va_end(args);
686 if (pos + len >= head->readbuf_size)
687 return false;
688 head->read_avail += len;
689 return true;
690}
691
692/**
693 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
694 *
695 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
696 *
697 * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
698 * if this function didn't return NULL.
699 */
700static const char *tomoyo_get_exe(void)
701{
702 struct mm_struct *mm = current->mm;
703 struct vm_area_struct *vma;
704 const char *cp = NULL;
705
706 if (!mm)
707 return NULL;
708 down_read(&mm->mmap_sem);
709 for (vma = mm->mmap; vma; vma = vma->vm_next) {
710 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
711 cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
712 break;
713 }
714 }
715 up_read(&mm->mmap_sem);
716 return cp;
717}
718
719/**
720 * tomoyo_get_msg - Get warning message.
721 *
722 * @is_enforce: Is it enforcing mode?
723 *
724 * Returns "ERROR" or "WARNING".
725 */
726const char *tomoyo_get_msg(const bool is_enforce)
727{
728 if (is_enforce)
729 return "ERROR";
730 else
731 return "WARNING";
732}
733
734/**
735 * tomoyo_check_flags - Check mode for specified functionality.
736 *
737 * @domain: Pointer to "struct tomoyo_domain_info".
738 * @index: The functionality to check mode.
739 *
740 * TOMOYO checks only process context.
741 * This code disables TOMOYO's enforcement in case the function is called from
742 * interrupt context.
743 */
744unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
745 const u8 index)
746{
747 const u8 profile = domain->profile;
748
749 if (WARN_ON(in_interrupt()))
750 return 0;
751 return tomoyo_policy_loaded && index < TOMOYO_MAX_CONTROL_INDEX
752#if TOMOYO_MAX_PROFILES != 256
753 && profile < TOMOYO_MAX_PROFILES
754#endif
755 && tomoyo_profile_ptr[profile] ?
756 tomoyo_profile_ptr[profile]->value[index] : 0;
757}
758
759/**
760 * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
761 *
762 * @domain: Pointer to "struct tomoyo_domain_info".
763 *
764 * Returns true if domain policy violation warning should be printed to
765 * console.
766 */
767bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain)
768{
769 return tomoyo_check_flags(domain, TOMOYO_VERBOSE) != 0;
770}
771
772/**
773 * tomoyo_domain_quota_is_ok - Check for domain's quota.
774 *
775 * @domain: Pointer to "struct tomoyo_domain_info".
776 *
777 * Returns true if the domain is not exceeded quota, false otherwise.
778 */
779bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain)
780{
781 unsigned int count = 0;
782 struct tomoyo_acl_info *ptr;
783
784 if (!domain)
785 return true;
786 down_read(&tomoyo_domain_acl_info_list_lock);
787 list_for_each_entry(ptr, &domain->acl_info_list, list) {
788 if (ptr->type & TOMOYO_ACL_DELETED)
789 continue;
790 switch (tomoyo_acl_type2(ptr)) {
791 struct tomoyo_single_path_acl_record *acl1;
792 struct tomoyo_double_path_acl_record *acl2;
793 u16 perm;
794 case TOMOYO_TYPE_SINGLE_PATH_ACL:
795 acl1 = container_of(ptr,
796 struct tomoyo_single_path_acl_record,
797 head);
798 perm = acl1->perm;
799 if (perm & (1 << TOMOYO_TYPE_EXECUTE_ACL))
800 count++;
801 if (perm &
802 ((1 << TOMOYO_TYPE_READ_ACL) |
803 (1 << TOMOYO_TYPE_WRITE_ACL)))
804 count++;
805 if (perm & (1 << TOMOYO_TYPE_CREATE_ACL))
806 count++;
807 if (perm & (1 << TOMOYO_TYPE_UNLINK_ACL))
808 count++;
809 if (perm & (1 << TOMOYO_TYPE_MKDIR_ACL))
810 count++;
811 if (perm & (1 << TOMOYO_TYPE_RMDIR_ACL))
812 count++;
813 if (perm & (1 << TOMOYO_TYPE_MKFIFO_ACL))
814 count++;
815 if (perm & (1 << TOMOYO_TYPE_MKSOCK_ACL))
816 count++;
817 if (perm & (1 << TOMOYO_TYPE_MKBLOCK_ACL))
818 count++;
819 if (perm & (1 << TOMOYO_TYPE_MKCHAR_ACL))
820 count++;
821 if (perm & (1 << TOMOYO_TYPE_TRUNCATE_ACL))
822 count++;
823 if (perm & (1 << TOMOYO_TYPE_SYMLINK_ACL))
824 count++;
825 if (perm & (1 << TOMOYO_TYPE_REWRITE_ACL))
826 count++;
827 break;
828 case TOMOYO_TYPE_DOUBLE_PATH_ACL:
829 acl2 = container_of(ptr,
830 struct tomoyo_double_path_acl_record,
831 head);
832 perm = acl2->perm;
833 if (perm & (1 << TOMOYO_TYPE_LINK_ACL))
834 count++;
835 if (perm & (1 << TOMOYO_TYPE_RENAME_ACL))
836 count++;
837 break;
838 }
839 }
840 up_read(&tomoyo_domain_acl_info_list_lock);
841 if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
842 return true;
843 if (!domain->quota_warned) {
844 domain->quota_warned = true;
845 printk(KERN_WARNING "TOMOYO-WARNING: "
846 "Domain '%s' has so many ACLs to hold. "
847 "Stopped learning mode.\n", domain->domainname->name);
848 }
849 return false;
850}
851
852/**
853 * tomoyo_find_or_assign_new_profile - Create a new profile.
854 *
855 * @profile: Profile number to create.
856 *
857 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
858 */
859static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
860 int profile)
861{
862 static DEFINE_MUTEX(lock);
863 struct tomoyo_profile *ptr = NULL;
864 int i;
865
866 if (profile >= TOMOYO_MAX_PROFILES)
867 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900868 mutex_lock(&lock);
869 ptr = tomoyo_profile_ptr[profile];
870 if (ptr)
871 goto ok;
872 ptr = tomoyo_alloc_element(sizeof(*ptr));
873 if (!ptr)
874 goto ok;
875 for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
876 ptr->value[i] = tomoyo_control_array[i].current_value;
877 mb(); /* Avoid out-of-order execution. */
878 tomoyo_profile_ptr[profile] = ptr;
879 ok:
880 mutex_unlock(&lock);
Kentaro Takeda95908372009-02-05 17:18:13 +0900881 return ptr;
882}
883
884/**
885 * tomoyo_write_profile - Write to profile table.
886 *
887 * @head: Pointer to "struct tomoyo_io_buffer".
888 *
889 * Returns 0 on success, negative value otherwise.
890 */
891static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
892{
893 char *data = head->write_buf;
894 unsigned int i;
895 unsigned int value;
896 char *cp;
897 struct tomoyo_profile *profile;
898 unsigned long num;
899
900 cp = strchr(data, '-');
901 if (cp)
902 *cp = '\0';
903 if (strict_strtoul(data, 10, &num))
904 return -EINVAL;
905 if (cp)
906 data = cp + 1;
907 profile = tomoyo_find_or_assign_new_profile(num);
908 if (!profile)
909 return -EINVAL;
910 cp = strchr(data, '=');
911 if (!cp)
912 return -EINVAL;
913 *cp = '\0';
914 if (!strcmp(data, "COMMENT")) {
915 profile->comment = tomoyo_save_name(cp + 1);
916 return 0;
917 }
918 for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
919 if (strcmp(data, tomoyo_control_array[i].keyword))
920 continue;
921 if (sscanf(cp + 1, "%u", &value) != 1) {
922 int j;
923 const char **modes;
924 switch (i) {
925 case TOMOYO_VERBOSE:
926 modes = tomoyo_mode_2;
927 break;
928 default:
929 modes = tomoyo_mode_4;
930 break;
931 }
932 for (j = 0; j < 4; j++) {
933 if (strcmp(cp + 1, modes[j]))
934 continue;
935 value = j;
936 break;
937 }
938 if (j == 4)
939 return -EINVAL;
940 } else if (value > tomoyo_control_array[i].max_value) {
941 value = tomoyo_control_array[i].max_value;
942 }
943 profile->value[i] = value;
944 return 0;
945 }
946 return -EINVAL;
947}
948
949/**
950 * tomoyo_read_profile - Read from profile table.
951 *
952 * @head: Pointer to "struct tomoyo_io_buffer".
953 *
954 * Returns 0.
955 */
956static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
957{
958 static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
959 int step;
960
961 if (head->read_eof)
962 return 0;
963 for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
964 step++) {
965 const u8 index = step / total;
966 u8 type = step % total;
967 const struct tomoyo_profile *profile
968 = tomoyo_profile_ptr[index];
969 head->read_step = step;
970 if (!profile)
971 continue;
972 if (!type) { /* Print profile' comment tag. */
973 if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
974 index, profile->comment ?
975 profile->comment->name : ""))
976 break;
977 continue;
978 }
979 type--;
980 if (type < TOMOYO_MAX_CONTROL_INDEX) {
981 const unsigned int value = profile->value[type];
982 const char **modes = NULL;
983 const char *keyword
984 = tomoyo_control_array[type].keyword;
985 switch (tomoyo_control_array[type].max_value) {
986 case 3:
987 modes = tomoyo_mode_4;
988 break;
989 case 1:
990 modes = tomoyo_mode_2;
991 break;
992 }
993 if (modes) {
994 if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
995 keyword, modes[value]))
996 break;
997 } else {
998 if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
999 keyword, value))
1000 break;
1001 }
1002 }
1003 }
1004 if (step == TOMOYO_MAX_PROFILES * total)
1005 head->read_eof = true;
1006 return 0;
1007}
1008
1009/* Structure for policy manager. */
1010struct tomoyo_policy_manager_entry {
1011 struct list_head list;
1012 /* A path to program or a domainname. */
1013 const struct tomoyo_path_info *manager;
1014 bool is_domain; /* True if manager is a domainname. */
1015 bool is_deleted; /* True if this entry is deleted. */
1016};
1017
1018/* The list for "struct tomoyo_policy_manager_entry". */
1019static LIST_HEAD(tomoyo_policy_manager_list);
1020static DECLARE_RWSEM(tomoyo_policy_manager_list_lock);
1021
1022/**
1023 * tomoyo_update_manager_entry - Add a manager entry.
1024 *
1025 * @manager: The path to manager or the domainnamme.
1026 * @is_delete: True if it is a delete request.
1027 *
1028 * Returns 0 on success, negative value otherwise.
1029 */
1030static int tomoyo_update_manager_entry(const char *manager,
1031 const bool is_delete)
1032{
1033 struct tomoyo_policy_manager_entry *new_entry;
1034 struct tomoyo_policy_manager_entry *ptr;
1035 const struct tomoyo_path_info *saved_manager;
1036 int error = -ENOMEM;
1037 bool is_domain = false;
1038
1039 if (tomoyo_is_domain_def(manager)) {
1040 if (!tomoyo_is_correct_domain(manager, __func__))
1041 return -EINVAL;
1042 is_domain = true;
1043 } else {
1044 if (!tomoyo_is_correct_path(manager, 1, -1, -1, __func__))
1045 return -EINVAL;
1046 }
1047 saved_manager = tomoyo_save_name(manager);
1048 if (!saved_manager)
1049 return -ENOMEM;
Kentaro Takeda95908372009-02-05 17:18:13 +09001050 down_write(&tomoyo_policy_manager_list_lock);
1051 list_for_each_entry(ptr, &tomoyo_policy_manager_list, list) {
1052 if (ptr->manager != saved_manager)
1053 continue;
1054 ptr->is_deleted = is_delete;
1055 error = 0;
1056 goto out;
1057 }
1058 if (is_delete) {
1059 error = -ENOENT;
1060 goto out;
1061 }
1062 new_entry = tomoyo_alloc_element(sizeof(*new_entry));
1063 if (!new_entry)
1064 goto out;
1065 new_entry->manager = saved_manager;
1066 new_entry->is_domain = is_domain;
1067 list_add_tail(&new_entry->list, &tomoyo_policy_manager_list);
1068 error = 0;
1069 out:
1070 up_write(&tomoyo_policy_manager_list_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001071 return error;
1072}
1073
1074/**
1075 * tomoyo_write_manager_policy - Write manager policy.
1076 *
1077 * @head: Pointer to "struct tomoyo_io_buffer".
1078 *
1079 * Returns 0 on success, negative value otherwise.
1080 */
1081static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
1082{
1083 char *data = head->write_buf;
1084 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1085
1086 if (!strcmp(data, "manage_by_non_root")) {
1087 tomoyo_manage_by_non_root = !is_delete;
1088 return 0;
1089 }
1090 return tomoyo_update_manager_entry(data, is_delete);
1091}
1092
1093/**
1094 * tomoyo_read_manager_policy - Read manager policy.
1095 *
1096 * @head: Pointer to "struct tomoyo_io_buffer".
1097 *
1098 * Returns 0.
1099 */
1100static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
1101{
1102 struct list_head *pos;
1103 bool done = true;
1104
1105 if (head->read_eof)
1106 return 0;
1107 down_read(&tomoyo_policy_manager_list_lock);
1108 list_for_each_cookie(pos, head->read_var2,
1109 &tomoyo_policy_manager_list) {
1110 struct tomoyo_policy_manager_entry *ptr;
1111 ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
1112 list);
1113 if (ptr->is_deleted)
1114 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001115 done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
1116 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001117 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001118 }
1119 up_read(&tomoyo_policy_manager_list_lock);
1120 head->read_eof = done;
1121 return 0;
1122}
1123
1124/**
1125 * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
1126 *
1127 * Returns true if the current process is permitted to modify policy
1128 * via /sys/kernel/security/tomoyo/ interface.
1129 */
1130static bool tomoyo_is_policy_manager(void)
1131{
1132 struct tomoyo_policy_manager_entry *ptr;
1133 const char *exe;
1134 const struct task_struct *task = current;
1135 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
1136 bool found = false;
1137
1138 if (!tomoyo_policy_loaded)
1139 return true;
1140 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
1141 return false;
1142 down_read(&tomoyo_policy_manager_list_lock);
1143 list_for_each_entry(ptr, &tomoyo_policy_manager_list, list) {
1144 if (!ptr->is_deleted && ptr->is_domain
1145 && !tomoyo_pathcmp(domainname, ptr->manager)) {
1146 found = true;
1147 break;
1148 }
1149 }
1150 up_read(&tomoyo_policy_manager_list_lock);
1151 if (found)
1152 return true;
1153 exe = tomoyo_get_exe();
1154 if (!exe)
1155 return false;
1156 down_read(&tomoyo_policy_manager_list_lock);
1157 list_for_each_entry(ptr, &tomoyo_policy_manager_list, list) {
1158 if (!ptr->is_deleted && !ptr->is_domain
1159 && !strcmp(exe, ptr->manager->name)) {
1160 found = true;
1161 break;
1162 }
1163 }
1164 up_read(&tomoyo_policy_manager_list_lock);
1165 if (!found) { /* Reduce error messages. */
1166 static pid_t last_pid;
1167 const pid_t pid = current->pid;
1168 if (last_pid != pid) {
1169 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1170 "update policies.\n", domainname->name, exe);
1171 last_pid = pid;
1172 }
1173 }
1174 tomoyo_free(exe);
1175 return found;
1176}
1177
1178/**
1179 * tomoyo_is_select_one - Parse select command.
1180 *
1181 * @head: Pointer to "struct tomoyo_io_buffer".
1182 * @data: String to parse.
1183 *
1184 * Returns true on success, false otherwise.
1185 */
1186static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
1187 const char *data)
1188{
1189 unsigned int pid;
1190 struct tomoyo_domain_info *domain = NULL;
1191
1192 if (sscanf(data, "pid=%u", &pid) == 1) {
1193 struct task_struct *p;
Kentaro Takeda95908372009-02-05 17:18:13 +09001194 read_lock(&tasklist_lock);
1195 p = find_task_by_vpid(pid);
1196 if (p)
1197 domain = tomoyo_real_domain(p);
1198 read_unlock(&tasklist_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001199 } else if (!strncmp(data, "domain=", 7)) {
1200 if (tomoyo_is_domain_def(data + 7)) {
1201 down_read(&tomoyo_domain_list_lock);
1202 domain = tomoyo_find_domain(data + 7);
1203 up_read(&tomoyo_domain_list_lock);
1204 }
1205 } else
1206 return false;
1207 head->write_var1 = domain;
1208 /* Accessing read_buf is safe because head->io_sem is held. */
1209 if (!head->read_buf)
1210 return true; /* Do nothing if open(O_WRONLY). */
1211 head->read_avail = 0;
1212 tomoyo_io_printf(head, "# select %s\n", data);
1213 head->read_single_domain = true;
1214 head->read_eof = !domain;
1215 if (domain) {
1216 struct tomoyo_domain_info *d;
1217 head->read_var1 = NULL;
1218 down_read(&tomoyo_domain_list_lock);
1219 list_for_each_entry(d, &tomoyo_domain_list, list) {
1220 if (d == domain)
1221 break;
1222 head->read_var1 = &d->list;
1223 }
1224 up_read(&tomoyo_domain_list_lock);
1225 head->read_var2 = NULL;
1226 head->read_bit = 0;
1227 head->read_step = 0;
1228 if (domain->is_deleted)
1229 tomoyo_io_printf(head, "# This is a deleted domain.\n");
1230 }
1231 return true;
1232}
1233
1234/**
1235 * tomoyo_write_domain_policy - Write domain policy.
1236 *
1237 * @head: Pointer to "struct tomoyo_io_buffer".
1238 *
1239 * Returns 0 on success, negative value otherwise.
1240 */
1241static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
1242{
1243 char *data = head->write_buf;
1244 struct tomoyo_domain_info *domain = head->write_var1;
1245 bool is_delete = false;
1246 bool is_select = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001247 unsigned int profile;
1248
1249 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
1250 is_delete = true;
1251 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
1252 is_select = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001253 if (is_select && tomoyo_is_select_one(head, data))
1254 return 0;
1255 /* Don't allow updating policies by non manager programs. */
1256 if (!tomoyo_is_policy_manager())
1257 return -EPERM;
1258 if (tomoyo_is_domain_def(data)) {
1259 domain = NULL;
1260 if (is_delete)
1261 tomoyo_delete_domain(data);
1262 else if (is_select) {
1263 down_read(&tomoyo_domain_list_lock);
1264 domain = tomoyo_find_domain(data);
1265 up_read(&tomoyo_domain_list_lock);
Tetsuo Handaa0558fc2009-04-06 20:49:14 +09001266 } else
Kentaro Takeda95908372009-02-05 17:18:13 +09001267 domain = tomoyo_find_or_assign_new_domain(data, 0);
1268 head->write_var1 = domain;
1269 return 0;
1270 }
1271 if (!domain)
1272 return -EINVAL;
1273
1274 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
1275 && profile < TOMOYO_MAX_PROFILES) {
1276 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
1277 domain->profile = (u8) profile;
1278 return 0;
1279 }
1280 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1281 tomoyo_set_domain_flag(domain, is_delete,
1282 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1283 return 0;
1284 }
1285 return tomoyo_write_file_policy(data, domain, is_delete);
1286}
1287
1288/**
1289 * tomoyo_print_single_path_acl - Print a single path ACL entry.
1290 *
1291 * @head: Pointer to "struct tomoyo_io_buffer".
1292 * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
1293 *
1294 * Returns true on success, false otherwise.
1295 */
1296static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer *head,
1297 struct tomoyo_single_path_acl_record *
1298 ptr)
1299{
1300 int pos;
1301 u8 bit;
1302 const char *atmark = "";
1303 const char *filename;
1304 const u16 perm = ptr->perm;
1305
1306 filename = ptr->filename->name;
1307 for (bit = head->read_bit; bit < TOMOYO_MAX_SINGLE_PATH_OPERATION;
1308 bit++) {
1309 const char *msg;
1310 if (!(perm & (1 << bit)))
1311 continue;
1312 /* Print "read/write" instead of "read" and "write". */
1313 if ((bit == TOMOYO_TYPE_READ_ACL ||
1314 bit == TOMOYO_TYPE_WRITE_ACL)
1315 && (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
1316 continue;
1317 msg = tomoyo_sp2keyword(bit);
1318 pos = head->read_avail;
1319 if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
1320 atmark, filename))
1321 goto out;
1322 }
1323 head->read_bit = 0;
1324 return true;
1325 out:
1326 head->read_bit = bit;
1327 head->read_avail = pos;
1328 return false;
1329}
1330
1331/**
1332 * tomoyo_print_double_path_acl - Print a double path ACL entry.
1333 *
1334 * @head: Pointer to "struct tomoyo_io_buffer".
1335 * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
1336 *
1337 * Returns true on success, false otherwise.
1338 */
1339static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer *head,
1340 struct tomoyo_double_path_acl_record *
1341 ptr)
1342{
1343 int pos;
1344 const char *atmark1 = "";
1345 const char *atmark2 = "";
1346 const char *filename1;
1347 const char *filename2;
1348 const u8 perm = ptr->perm;
1349 u8 bit;
1350
1351 filename1 = ptr->filename1->name;
1352 filename2 = ptr->filename2->name;
1353 for (bit = head->read_bit; bit < TOMOYO_MAX_DOUBLE_PATH_OPERATION;
1354 bit++) {
1355 const char *msg;
1356 if (!(perm & (1 << bit)))
1357 continue;
1358 msg = tomoyo_dp2keyword(bit);
1359 pos = head->read_avail;
1360 if (!tomoyo_io_printf(head, "allow_%s %s%s %s%s\n", msg,
1361 atmark1, filename1, atmark2, filename2))
1362 goto out;
1363 }
1364 head->read_bit = 0;
1365 return true;
1366 out:
1367 head->read_bit = bit;
1368 head->read_avail = pos;
1369 return false;
1370}
1371
1372/**
1373 * tomoyo_print_entry - Print an ACL entry.
1374 *
1375 * @head: Pointer to "struct tomoyo_io_buffer".
1376 * @ptr: Pointer to an ACL entry.
1377 *
1378 * Returns true on success, false otherwise.
1379 */
1380static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
1381 struct tomoyo_acl_info *ptr)
1382{
1383 const u8 acl_type = tomoyo_acl_type2(ptr);
1384
1385 if (acl_type & TOMOYO_ACL_DELETED)
1386 return true;
1387 if (acl_type == TOMOYO_TYPE_SINGLE_PATH_ACL) {
1388 struct tomoyo_single_path_acl_record *acl
1389 = container_of(ptr,
1390 struct tomoyo_single_path_acl_record,
1391 head);
1392 return tomoyo_print_single_path_acl(head, acl);
1393 }
1394 if (acl_type == TOMOYO_TYPE_DOUBLE_PATH_ACL) {
1395 struct tomoyo_double_path_acl_record *acl
1396 = container_of(ptr,
1397 struct tomoyo_double_path_acl_record,
1398 head);
1399 return tomoyo_print_double_path_acl(head, acl);
1400 }
1401 BUG(); /* This must not happen. */
1402 return false;
1403}
1404
1405/**
1406 * tomoyo_read_domain_policy - Read domain policy.
1407 *
1408 * @head: Pointer to "struct tomoyo_io_buffer".
1409 *
1410 * Returns 0.
1411 */
1412static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
1413{
1414 struct list_head *dpos;
1415 struct list_head *apos;
1416 bool done = true;
1417
1418 if (head->read_eof)
1419 return 0;
1420 if (head->read_step == 0)
1421 head->read_step = 1;
1422 down_read(&tomoyo_domain_list_lock);
1423 list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
1424 struct tomoyo_domain_info *domain;
1425 const char *quota_exceeded = "";
1426 const char *transition_failed = "";
1427 const char *ignore_global_allow_read = "";
1428 domain = list_entry(dpos, struct tomoyo_domain_info, list);
1429 if (head->read_step != 1)
1430 goto acl_loop;
1431 if (domain->is_deleted && !head->read_single_domain)
1432 continue;
1433 /* Print domainname and flags. */
1434 if (domain->quota_warned)
1435 quota_exceeded = "quota_exceeded\n";
1436 if (domain->flags & TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED)
1437 transition_failed = "transition_failed\n";
1438 if (domain->flags &
1439 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
1440 ignore_global_allow_read
1441 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001442 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1443 "%u\n%s%s%s\n",
1444 domain->domainname->name,
1445 domain->profile, quota_exceeded,
1446 transition_failed,
1447 ignore_global_allow_read);
1448 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001449 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001450 head->read_step = 2;
1451acl_loop:
1452 if (head->read_step == 3)
1453 goto tail_mark;
1454 /* Print ACL entries in the domain. */
1455 down_read(&tomoyo_domain_acl_info_list_lock);
1456 list_for_each_cookie(apos, head->read_var2,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001457 &domain->acl_info_list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001458 struct tomoyo_acl_info *ptr
1459 = list_entry(apos, struct tomoyo_acl_info,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001460 list);
1461 done = tomoyo_print_entry(head, ptr);
1462 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001463 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001464 }
1465 up_read(&tomoyo_domain_acl_info_list_lock);
1466 if (!done)
1467 break;
1468 head->read_step = 3;
1469tail_mark:
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001470 done = tomoyo_io_printf(head, "\n");
1471 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001472 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001473 head->read_step = 1;
1474 if (head->read_single_domain)
1475 break;
1476 }
1477 up_read(&tomoyo_domain_list_lock);
1478 head->read_eof = done;
1479 return 0;
1480}
1481
1482/**
1483 * tomoyo_write_domain_profile - Assign profile for specified domain.
1484 *
1485 * @head: Pointer to "struct tomoyo_io_buffer".
1486 *
1487 * Returns 0 on success, -EINVAL otherwise.
1488 *
1489 * This is equivalent to doing
1490 *
1491 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1492 * /usr/lib/ccs/loadpolicy -d
1493 */
1494static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1495{
1496 char *data = head->write_buf;
1497 char *cp = strchr(data, ' ');
1498 struct tomoyo_domain_info *domain;
1499 unsigned long profile;
1500
1501 if (!cp)
1502 return -EINVAL;
1503 *cp = '\0';
1504 down_read(&tomoyo_domain_list_lock);
1505 domain = tomoyo_find_domain(cp + 1);
1506 up_read(&tomoyo_domain_list_lock);
1507 if (strict_strtoul(data, 10, &profile))
1508 return -EINVAL;
1509 if (domain && profile < TOMOYO_MAX_PROFILES
1510 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1511 domain->profile = (u8) profile;
1512 return 0;
1513}
1514
1515/**
1516 * tomoyo_read_domain_profile - Read only domainname and profile.
1517 *
1518 * @head: Pointer to "struct tomoyo_io_buffer".
1519 *
1520 * Returns list of profile number and domainname pairs.
1521 *
1522 * This is equivalent to doing
1523 *
1524 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1525 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1526 * domainname = $0; } else if ( $1 == "use_profile" ) {
1527 * print $2 " " domainname; domainname = ""; } } ; '
1528 */
1529static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1530{
1531 struct list_head *pos;
1532 bool done = true;
1533
1534 if (head->read_eof)
1535 return 0;
1536 down_read(&tomoyo_domain_list_lock);
1537 list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
1538 struct tomoyo_domain_info *domain;
1539 domain = list_entry(pos, struct tomoyo_domain_info, list);
1540 if (domain->is_deleted)
1541 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001542 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1543 domain->domainname->name);
1544 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001545 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001546 }
1547 up_read(&tomoyo_domain_list_lock);
1548 head->read_eof = done;
1549 return 0;
1550}
1551
1552/**
1553 * tomoyo_write_pid: Specify PID to obtain domainname.
1554 *
1555 * @head: Pointer to "struct tomoyo_io_buffer".
1556 *
1557 * Returns 0.
1558 */
1559static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1560{
1561 unsigned long pid;
1562 /* No error check. */
1563 strict_strtoul(head->write_buf, 10, &pid);
1564 head->read_step = (int) pid;
1565 head->read_eof = false;
1566 return 0;
1567}
1568
1569/**
1570 * tomoyo_read_pid - Get domainname of the specified PID.
1571 *
1572 * @head: Pointer to "struct tomoyo_io_buffer".
1573 *
1574 * Returns the domainname which the specified PID is in on success,
1575 * empty string otherwise.
1576 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1577 * using read()/write() interface rather than sysctl() interface.
1578 */
1579static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
1580{
1581 if (head->read_avail == 0 && !head->read_eof) {
1582 const int pid = head->read_step;
1583 struct task_struct *p;
1584 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +09001585 read_lock(&tasklist_lock);
1586 p = find_task_by_vpid(pid);
1587 if (p)
1588 domain = tomoyo_real_domain(p);
1589 read_unlock(&tasklist_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001590 if (domain)
1591 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
1592 domain->domainname->name);
1593 head->read_eof = true;
1594 }
1595 return 0;
1596}
1597
1598/**
1599 * tomoyo_write_exception_policy - Write exception policy.
1600 *
1601 * @head: Pointer to "struct tomoyo_io_buffer".
1602 *
1603 * Returns 0 on success, negative value otherwise.
1604 */
1605static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1606{
1607 char *data = head->write_buf;
1608 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1609
1610 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
1611 return tomoyo_write_domain_keeper_policy(data, false,
1612 is_delete);
1613 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
1614 return tomoyo_write_domain_keeper_policy(data, true, is_delete);
1615 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
1616 return tomoyo_write_domain_initializer_policy(data, false,
1617 is_delete);
1618 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
1619 return tomoyo_write_domain_initializer_policy(data, true,
1620 is_delete);
1621 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
1622 return tomoyo_write_alias_policy(data, is_delete);
1623 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1624 return tomoyo_write_globally_readable_policy(data, is_delete);
1625 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1626 return tomoyo_write_pattern_policy(data, is_delete);
1627 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1628 return tomoyo_write_no_rewrite_policy(data, is_delete);
1629 return -EINVAL;
1630}
1631
1632/**
1633 * tomoyo_read_exception_policy - Read exception policy.
1634 *
1635 * @head: Pointer to "struct tomoyo_io_buffer".
1636 *
1637 * Returns 0 on success, -EINVAL otherwise.
1638 */
1639static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
1640{
1641 if (!head->read_eof) {
1642 switch (head->read_step) {
1643 case 0:
1644 head->read_var2 = NULL;
1645 head->read_step = 1;
1646 case 1:
1647 if (!tomoyo_read_domain_keeper_policy(head))
1648 break;
1649 head->read_var2 = NULL;
1650 head->read_step = 2;
1651 case 2:
1652 if (!tomoyo_read_globally_readable_policy(head))
1653 break;
1654 head->read_var2 = NULL;
1655 head->read_step = 3;
1656 case 3:
1657 head->read_var2 = NULL;
1658 head->read_step = 4;
1659 case 4:
1660 if (!tomoyo_read_domain_initializer_policy(head))
1661 break;
1662 head->read_var2 = NULL;
1663 head->read_step = 5;
1664 case 5:
1665 if (!tomoyo_read_alias_policy(head))
1666 break;
1667 head->read_var2 = NULL;
1668 head->read_step = 6;
1669 case 6:
1670 head->read_var2 = NULL;
1671 head->read_step = 7;
1672 case 7:
1673 if (!tomoyo_read_file_pattern(head))
1674 break;
1675 head->read_var2 = NULL;
1676 head->read_step = 8;
1677 case 8:
1678 if (!tomoyo_read_no_rewrite_policy(head))
1679 break;
1680 head->read_var2 = NULL;
1681 head->read_step = 9;
1682 case 9:
1683 head->read_eof = true;
1684 break;
1685 default:
1686 return -EINVAL;
1687 }
1688 }
1689 return 0;
1690}
1691
1692/* path to policy loader */
1693static const char *tomoyo_loader = "/sbin/tomoyo-init";
1694
1695/**
1696 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
1697 *
1698 * Returns true if /sbin/tomoyo-init exists, false otherwise.
1699 */
1700static bool tomoyo_policy_loader_exists(void)
1701{
1702 /*
1703 * Don't activate MAC if the policy loader doesn't exist.
1704 * If the initrd includes /sbin/init but real-root-dev has not
1705 * mounted on / yet, activating MAC will block the system since
1706 * policies are not loaded yet.
1707 * Thus, let do_execve() call this function everytime.
1708 */
Al Viroe24977d2009-04-02 21:17:03 -04001709 struct path path;
Kentaro Takeda95908372009-02-05 17:18:13 +09001710
Al Viroe24977d2009-04-02 21:17:03 -04001711 if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001712 printk(KERN_INFO "Not activating Mandatory Access Control now "
1713 "since %s doesn't exist.\n", tomoyo_loader);
1714 return false;
1715 }
Al Viroe24977d2009-04-02 21:17:03 -04001716 path_put(&path);
Kentaro Takeda95908372009-02-05 17:18:13 +09001717 return true;
1718}
1719
1720/**
1721 * tomoyo_load_policy - Run external policy loader to load policy.
1722 *
1723 * @filename: The program about to start.
1724 *
1725 * This function checks whether @filename is /sbin/init , and if so
1726 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
1727 * and then continues invocation of /sbin/init.
1728 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
1729 * writes to /sys/kernel/security/tomoyo/ interfaces.
1730 *
1731 * Returns nothing.
1732 */
1733void tomoyo_load_policy(const char *filename)
1734{
1735 char *argv[2];
1736 char *envp[3];
1737
1738 if (tomoyo_policy_loaded)
1739 return;
1740 /*
1741 * Check filename is /sbin/init or /sbin/tomoyo-start.
1742 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
1743 * be passed.
1744 * You can create /sbin/tomoyo-start by
1745 * "ln -s /bin/true /sbin/tomoyo-start".
1746 */
1747 if (strcmp(filename, "/sbin/init") &&
1748 strcmp(filename, "/sbin/tomoyo-start"))
1749 return;
1750 if (!tomoyo_policy_loader_exists())
1751 return;
1752
1753 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
1754 tomoyo_loader);
1755 argv[0] = (char *) tomoyo_loader;
1756 argv[1] = NULL;
1757 envp[0] = "HOME=/";
1758 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1759 envp[2] = NULL;
1760 call_usermodehelper(argv[0], argv, envp, 1);
1761
Tetsuo Handa39826a12009-04-08 22:31:28 +09001762 printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09001763 printk(KERN_INFO "Mandatory Access Control activated.\n");
1764 tomoyo_policy_loaded = true;
1765 { /* Check all profiles currently assigned to domains are defined. */
1766 struct tomoyo_domain_info *domain;
1767 down_read(&tomoyo_domain_list_lock);
1768 list_for_each_entry(domain, &tomoyo_domain_list, list) {
1769 const u8 profile = domain->profile;
1770 if (tomoyo_profile_ptr[profile])
1771 continue;
1772 panic("Profile %u (used by '%s') not defined.\n",
1773 profile, domain->domainname->name);
1774 }
1775 up_read(&tomoyo_domain_list_lock);
1776 }
1777}
1778
1779/**
1780 * tomoyo_read_version: Get version.
1781 *
1782 * @head: Pointer to "struct tomoyo_io_buffer".
1783 *
1784 * Returns version information.
1785 */
1786static int tomoyo_read_version(struct tomoyo_io_buffer *head)
1787{
1788 if (!head->read_eof) {
Tetsuo Handa39826a12009-04-08 22:31:28 +09001789 tomoyo_io_printf(head, "2.2.0");
Kentaro Takeda95908372009-02-05 17:18:13 +09001790 head->read_eof = true;
1791 }
1792 return 0;
1793}
1794
1795/**
1796 * tomoyo_read_self_domain - Get the current process's domainname.
1797 *
1798 * @head: Pointer to "struct tomoyo_io_buffer".
1799 *
1800 * Returns the current process's domainname.
1801 */
1802static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1803{
1804 if (!head->read_eof) {
1805 /*
1806 * tomoyo_domain()->domainname != NULL
1807 * because every process belongs to a domain and
1808 * the domain's name cannot be NULL.
1809 */
1810 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1811 head->read_eof = true;
1812 }
1813 return 0;
1814}
1815
1816/**
1817 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1818 *
1819 * @type: Type of interface.
1820 * @file: Pointer to "struct file".
1821 *
1822 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1823 */
1824static int tomoyo_open_control(const u8 type, struct file *file)
1825{
1826 struct tomoyo_io_buffer *head = tomoyo_alloc(sizeof(*head));
1827
1828 if (!head)
1829 return -ENOMEM;
1830 mutex_init(&head->io_sem);
1831 switch (type) {
1832 case TOMOYO_DOMAINPOLICY:
1833 /* /sys/kernel/security/tomoyo/domain_policy */
1834 head->write = tomoyo_write_domain_policy;
1835 head->read = tomoyo_read_domain_policy;
1836 break;
1837 case TOMOYO_EXCEPTIONPOLICY:
1838 /* /sys/kernel/security/tomoyo/exception_policy */
1839 head->write = tomoyo_write_exception_policy;
1840 head->read = tomoyo_read_exception_policy;
1841 break;
1842 case TOMOYO_SELFDOMAIN:
1843 /* /sys/kernel/security/tomoyo/self_domain */
1844 head->read = tomoyo_read_self_domain;
1845 break;
1846 case TOMOYO_DOMAIN_STATUS:
1847 /* /sys/kernel/security/tomoyo/.domain_status */
1848 head->write = tomoyo_write_domain_profile;
1849 head->read = tomoyo_read_domain_profile;
1850 break;
1851 case TOMOYO_PROCESS_STATUS:
1852 /* /sys/kernel/security/tomoyo/.process_status */
1853 head->write = tomoyo_write_pid;
1854 head->read = tomoyo_read_pid;
1855 break;
1856 case TOMOYO_VERSION:
1857 /* /sys/kernel/security/tomoyo/version */
1858 head->read = tomoyo_read_version;
1859 head->readbuf_size = 128;
1860 break;
1861 case TOMOYO_MEMINFO:
1862 /* /sys/kernel/security/tomoyo/meminfo */
1863 head->write = tomoyo_write_memory_quota;
1864 head->read = tomoyo_read_memory_counter;
1865 head->readbuf_size = 512;
1866 break;
1867 case TOMOYO_PROFILE:
1868 /* /sys/kernel/security/tomoyo/profile */
1869 head->write = tomoyo_write_profile;
1870 head->read = tomoyo_read_profile;
1871 break;
1872 case TOMOYO_MANAGER:
1873 /* /sys/kernel/security/tomoyo/manager */
1874 head->write = tomoyo_write_manager_policy;
1875 head->read = tomoyo_read_manager_policy;
1876 break;
1877 }
1878 if (!(file->f_mode & FMODE_READ)) {
1879 /*
1880 * No need to allocate read_buf since it is not opened
1881 * for reading.
1882 */
1883 head->read = NULL;
1884 } else {
1885 if (!head->readbuf_size)
1886 head->readbuf_size = 4096 * 2;
1887 head->read_buf = tomoyo_alloc(head->readbuf_size);
1888 if (!head->read_buf) {
1889 tomoyo_free(head);
1890 return -ENOMEM;
1891 }
1892 }
1893 if (!(file->f_mode & FMODE_WRITE)) {
1894 /*
1895 * No need to allocate write_buf since it is not opened
1896 * for writing.
1897 */
1898 head->write = NULL;
1899 } else if (head->write) {
1900 head->writebuf_size = 4096 * 2;
1901 head->write_buf = tomoyo_alloc(head->writebuf_size);
1902 if (!head->write_buf) {
1903 tomoyo_free(head->read_buf);
1904 tomoyo_free(head);
1905 return -ENOMEM;
1906 }
1907 }
1908 file->private_data = head;
1909 /*
1910 * Call the handler now if the file is
1911 * /sys/kernel/security/tomoyo/self_domain
1912 * so that the user can use
1913 * cat < /sys/kernel/security/tomoyo/self_domain"
1914 * to know the current process's domainname.
1915 */
1916 if (type == TOMOYO_SELFDOMAIN)
1917 tomoyo_read_control(file, NULL, 0);
1918 return 0;
1919}
1920
1921/**
1922 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1923 *
1924 * @file: Pointer to "struct file".
1925 * @buffer: Poiner to buffer to write to.
1926 * @buffer_len: Size of @buffer.
1927 *
1928 * Returns bytes read on success, negative value otherwise.
1929 */
1930static int tomoyo_read_control(struct file *file, char __user *buffer,
1931 const int buffer_len)
1932{
1933 int len = 0;
1934 struct tomoyo_io_buffer *head = file->private_data;
1935 char *cp;
1936
1937 if (!head->read)
1938 return -ENOSYS;
1939 if (mutex_lock_interruptible(&head->io_sem))
1940 return -EINTR;
1941 /* Call the policy handler. */
1942 len = head->read(head);
1943 if (len < 0)
1944 goto out;
1945 /* Write to buffer. */
1946 len = head->read_avail;
1947 if (len > buffer_len)
1948 len = buffer_len;
1949 if (!len)
1950 goto out;
1951 /* head->read_buf changes by some functions. */
1952 cp = head->read_buf;
1953 if (copy_to_user(buffer, cp, len)) {
1954 len = -EFAULT;
1955 goto out;
1956 }
1957 head->read_avail -= len;
1958 memmove(cp, cp + len, head->read_avail);
1959 out:
1960 mutex_unlock(&head->io_sem);
1961 return len;
1962}
1963
1964/**
1965 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1966 *
1967 * @file: Pointer to "struct file".
1968 * @buffer: Pointer to buffer to read from.
1969 * @buffer_len: Size of @buffer.
1970 *
1971 * Returns @buffer_len on success, negative value otherwise.
1972 */
1973static int tomoyo_write_control(struct file *file, const char __user *buffer,
1974 const int buffer_len)
1975{
1976 struct tomoyo_io_buffer *head = file->private_data;
1977 int error = buffer_len;
1978 int avail_len = buffer_len;
1979 char *cp0 = head->write_buf;
1980
1981 if (!head->write)
1982 return -ENOSYS;
1983 if (!access_ok(VERIFY_READ, buffer, buffer_len))
1984 return -EFAULT;
1985 /* Don't allow updating policies by non manager programs. */
1986 if (head->write != tomoyo_write_pid &&
1987 head->write != tomoyo_write_domain_policy &&
1988 !tomoyo_is_policy_manager())
1989 return -EPERM;
1990 if (mutex_lock_interruptible(&head->io_sem))
1991 return -EINTR;
1992 /* Read a line and dispatch it to the policy handler. */
1993 while (avail_len > 0) {
1994 char c;
1995 if (head->write_avail >= head->writebuf_size - 1) {
1996 error = -ENOMEM;
1997 break;
1998 } else if (get_user(c, buffer)) {
1999 error = -EFAULT;
2000 break;
2001 }
2002 buffer++;
2003 avail_len--;
2004 cp0[head->write_avail++] = c;
2005 if (c != '\n')
2006 continue;
2007 cp0[head->write_avail - 1] = '\0';
2008 head->write_avail = 0;
2009 tomoyo_normalize_line(cp0);
2010 head->write(head);
2011 }
2012 mutex_unlock(&head->io_sem);
2013 return error;
2014}
2015
2016/**
2017 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2018 *
2019 * @file: Pointer to "struct file".
2020 *
2021 * Releases memory and returns 0.
2022 */
2023static int tomoyo_close_control(struct file *file)
2024{
2025 struct tomoyo_io_buffer *head = file->private_data;
2026
2027 /* Release memory used for policy I/O. */
2028 tomoyo_free(head->read_buf);
2029 head->read_buf = NULL;
2030 tomoyo_free(head->write_buf);
2031 head->write_buf = NULL;
2032 tomoyo_free(head);
2033 head = NULL;
2034 file->private_data = NULL;
2035 return 0;
2036}
2037
2038/**
2039 * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
2040 *
2041 * @acl_type: Type of ACL entry.
2042 *
2043 * Returns pointer to the ACL entry on success, NULL otherwise.
2044 */
2045void *tomoyo_alloc_acl_element(const u8 acl_type)
2046{
2047 int len;
2048 struct tomoyo_acl_info *ptr;
2049
2050 switch (acl_type) {
2051 case TOMOYO_TYPE_SINGLE_PATH_ACL:
2052 len = sizeof(struct tomoyo_single_path_acl_record);
2053 break;
2054 case TOMOYO_TYPE_DOUBLE_PATH_ACL:
2055 len = sizeof(struct tomoyo_double_path_acl_record);
2056 break;
2057 default:
2058 return NULL;
2059 }
2060 ptr = tomoyo_alloc_element(len);
2061 if (!ptr)
2062 return NULL;
2063 ptr->type = acl_type;
2064 return ptr;
2065}
2066
2067/**
2068 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2069 *
2070 * @inode: Pointer to "struct inode".
2071 * @file: Pointer to "struct file".
2072 *
2073 * Returns 0 on success, negative value otherwise.
2074 */
2075static int tomoyo_open(struct inode *inode, struct file *file)
2076{
2077 const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
2078 - ((u8 *) NULL);
2079 return tomoyo_open_control(key, file);
2080}
2081
2082/**
2083 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2084 *
2085 * @inode: Pointer to "struct inode".
2086 * @file: Pointer to "struct file".
2087 *
2088 * Returns 0 on success, negative value otherwise.
2089 */
2090static int tomoyo_release(struct inode *inode, struct file *file)
2091{
2092 return tomoyo_close_control(file);
2093}
2094
2095/**
2096 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2097 *
2098 * @file: Pointer to "struct file".
2099 * @buf: Pointer to buffer.
2100 * @count: Size of @buf.
2101 * @ppos: Unused.
2102 *
2103 * Returns bytes read on success, negative value otherwise.
2104 */
2105static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
2106 loff_t *ppos)
2107{
2108 return tomoyo_read_control(file, buf, count);
2109}
2110
2111/**
2112 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2113 *
2114 * @file: Pointer to "struct file".
2115 * @buf: Pointer to buffer.
2116 * @count: Size of @buf.
2117 * @ppos: Unused.
2118 *
2119 * Returns @count on success, negative value otherwise.
2120 */
2121static ssize_t tomoyo_write(struct file *file, const char __user *buf,
2122 size_t count, loff_t *ppos)
2123{
2124 return tomoyo_write_control(file, buf, count);
2125}
2126
2127/* Operations for /sys/kernel/security/tomoyo/ interface. */
2128static const struct file_operations tomoyo_operations = {
2129 .open = tomoyo_open,
2130 .release = tomoyo_release,
2131 .read = tomoyo_read,
2132 .write = tomoyo_write,
2133};
2134
2135/**
2136 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2137 *
2138 * @name: The name of the interface file.
2139 * @mode: The permission of the interface file.
2140 * @parent: The parent directory.
2141 * @key: Type of interface.
2142 *
2143 * Returns nothing.
2144 */
2145static void __init tomoyo_create_entry(const char *name, const mode_t mode,
2146 struct dentry *parent, const u8 key)
2147{
2148 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
2149 &tomoyo_operations);
2150}
2151
2152/**
2153 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2154 *
2155 * Returns 0.
2156 */
2157static int __init tomoyo_initerface_init(void)
2158{
2159 struct dentry *tomoyo_dir;
2160
Tetsuo Handae5a3b952009-02-14 11:46:56 +09002161 /* Don't create securityfs entries unless registered. */
2162 if (current_cred()->security != &tomoyo_kernel_domain)
2163 return 0;
2164
Kentaro Takeda95908372009-02-05 17:18:13 +09002165 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
2166 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
2167 TOMOYO_DOMAINPOLICY);
2168 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
2169 TOMOYO_EXCEPTIONPOLICY);
2170 tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
2171 TOMOYO_SELFDOMAIN);
2172 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
2173 TOMOYO_DOMAIN_STATUS);
2174 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
2175 TOMOYO_PROCESS_STATUS);
2176 tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
2177 TOMOYO_MEMINFO);
2178 tomoyo_create_entry("profile", 0600, tomoyo_dir,
2179 TOMOYO_PROFILE);
2180 tomoyo_create_entry("manager", 0600, tomoyo_dir,
2181 TOMOYO_MANAGER);
2182 tomoyo_create_entry("version", 0400, tomoyo_dir,
2183 TOMOYO_VERSION);
2184 return 0;
2185}
2186
2187fs_initcall(tomoyo_initerface_init);