2 * security/tomoyo/common.c
4 * Common functions for TOMOYO.
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
8 * Version: 2.2.0 2009/04/01
12 #include <linux/uaccess.h>
13 #include <linux/slab.h>
14 #include <linux/security.h>
15 #include <linux/hardirq.h>
18 /* Lock for protecting policy. */
19 DEFINE_MUTEX(tomoyo_policy_lock);
21 /* Has loading policy done? */
22 bool tomoyo_policy_loaded;
24 /* String table for functionality that takes 4 modes. */
25 static const char *tomoyo_mode_4[4] = {
26 "disabled", "learning", "permissive", "enforcing"
28 /* String table for functionality that takes 2 modes. */
29 static const char *tomoyo_mode_2[4] = {
30 "disabled", "enabled", "enabled", "enabled"
34 * tomoyo_control_array is a static data which contains
36 * (1) functionality name used by /sys/kernel/security/tomoyo/profile .
37 * (2) initial values for "struct tomoyo_profile".
38 * (3) max values for "struct tomoyo_profile".
42 unsigned int current_value;
43 const unsigned int max_value;
44 } tomoyo_control_array[TOMOYO_MAX_CONTROL_INDEX] = {
45 [TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
46 [TOMOYO_MAX_ACCEPT_ENTRY] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX },
47 [TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
51 * tomoyo_profile is a structure which is used for holding the mode of access
52 * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
53 * An administrator can define up to 256 profiles.
54 * The ->profile of "struct tomoyo_domain_info" is used for remembering
55 * the profile's number (0 - 255) assigned to that domain.
57 static struct tomoyo_profile {
58 unsigned int value[TOMOYO_MAX_CONTROL_INDEX];
59 const struct tomoyo_path_info *comment;
60 } *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
62 /* Permit policy management by non-root user? */
63 static bool tomoyo_manage_by_non_root;
65 /* Utility functions. */
67 /* Open operation for /sys/kernel/security/tomoyo/ interface. */
68 static int tomoyo_open_control(const u8 type, struct file *file);
69 /* Close /sys/kernel/security/tomoyo/ interface. */
70 static int tomoyo_close_control(struct file *file);
71 /* Read operation for /sys/kernel/security/tomoyo/ interface. */
72 static int tomoyo_read_control(struct file *file, char __user *buffer,
73 const int buffer_len);
74 /* Write operation for /sys/kernel/security/tomoyo/ interface. */
75 static int tomoyo_write_control(struct file *file, const char __user *buffer,
76 const int buffer_len);
77 /* Check whether the domain has too many ACL entries to hold. */
78 static bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
81 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
83 * @filename: Name or name group.
84 * @ptr: Pointer to "struct tomoyo_name_union".
86 * Returns true on success, false otherwise.
88 bool tomoyo_parse_name_union(const char *filename,
89 struct tomoyo_name_union *ptr)
91 if (!tomoyo_is_correct_path(filename, 0, 0, 0))
93 if (filename[0] == '@') {
94 ptr->group = tomoyo_get_path_group(filename + 1);
96 return ptr->group != NULL;
98 ptr->filename = tomoyo_get_name(filename);
99 ptr->is_group = false;
100 return ptr->filename != NULL;
104 * tomoyo_print_name_union - Print a tomoyo_name_union.
106 * @head: Pointer to "struct tomoyo_io_buffer".
107 * @ptr: Pointer to "struct tomoyo_name_union".
109 * Returns true on success, false otherwise.
111 static bool tomoyo_print_name_union(struct tomoyo_io_buffer *head,
112 const struct tomoyo_name_union *ptr)
114 int pos = head->read_avail;
115 if (pos && head->read_buf[pos - 1] == ' ')
118 return tomoyo_io_printf(head, " @%s",
119 ptr->group->group_name->name);
120 return tomoyo_io_printf(head, " %s", ptr->filename->name);
124 * tomoyo_parse_ulong - Parse an "unsigned long" value.
126 * @result: Pointer to "unsigned long".
127 * @str: Pointer to string to parse.
129 * Returns value type on success, 0 otherwise.
131 * The @src is updated to point the first character after the value
134 u8 tomoyo_parse_ulong(unsigned long *result, char **str)
136 const char *cp = *str;
141 if (c == 'x' || c == 'X') {
144 } else if (c >= '0' && c <= '7') {
149 *result = simple_strtoul(cp, &ep, base);
155 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
157 return TOMOYO_VALUE_TYPE_OCTAL;
159 return TOMOYO_VALUE_TYPE_DECIMAL;
164 * tomoyo_print_ulong - Print an "unsigned long" value.
166 * @buffer: Pointer to buffer.
167 * @buffer_len: Size of @buffer.
168 * @value: An "unsigned long" value.
169 * @type: Type of @value.
173 void tomoyo_print_ulong(char *buffer, const int buffer_len,
174 const unsigned long value, const u8 type)
176 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
177 snprintf(buffer, buffer_len, "%lu", value);
178 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
179 snprintf(buffer, buffer_len, "0%lo", value);
180 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
181 snprintf(buffer, buffer_len, "0x%lX", value);
183 snprintf(buffer, buffer_len, "type(%u)", type);
187 * tomoyo_print_number_union - Print a tomoyo_number_union.
189 * @head: Pointer to "struct tomoyo_io_buffer".
190 * @ptr: Pointer to "struct tomoyo_number_union".
192 * Returns true on success, false otherwise.
194 bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
195 const struct tomoyo_number_union *ptr)
201 if (!tomoyo_io_printf(head, " "))
204 return tomoyo_io_printf(head, "@%s",
205 ptr->group->group_name->name);
206 min_type = ptr->min_type;
207 max_type = ptr->max_type;
208 min = ptr->values[0];
209 max = ptr->values[1];
211 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
212 if (!tomoyo_io_printf(head, "0x%lX", min))
215 case TOMOYO_VALUE_TYPE_OCTAL:
216 if (!tomoyo_io_printf(head, "0%lo", min))
220 if (!tomoyo_io_printf(head, "%lu", min))
224 if (min == max && min_type == max_type)
227 case TOMOYO_VALUE_TYPE_HEXADECIMAL:
228 return tomoyo_io_printf(head, "-0x%lX", max);
229 case TOMOYO_VALUE_TYPE_OCTAL:
230 return tomoyo_io_printf(head, "-0%lo", max);
232 return tomoyo_io_printf(head, "-%lu", max);
237 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
239 * @data: Number or number range or number group.
240 * @ptr: Pointer to "struct tomoyo_number_union".
242 * Returns true on success, false otherwise.
244 bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num)
248 memset(num, 0, sizeof(*num));
249 if (data[0] == '@') {
250 if (!tomoyo_is_correct_path(data, 0, 0, 0))
252 num->group = tomoyo_get_number_group(data + 1);
253 num->is_group = true;
254 return num->group != NULL;
256 type = tomoyo_parse_ulong(&v, &data);
260 num->min_type = type;
263 num->max_type = type;
268 type = tomoyo_parse_ulong(&v, &data);
272 num->max_type = type;
277 * tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
279 * @str: Pointer to the string.
281 * Returns true if @str is a \ooo style octal value, false otherwise.
283 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
284 * This function verifies that \ooo is in valid range.
286 static inline bool tomoyo_is_byte_range(const char *str)
288 return *str >= '0' && *str++ <= '3' &&
289 *str >= '0' && *str++ <= '7' &&
290 *str >= '0' && *str <= '7';
294 * tomoyo_is_alphabet_char - Check whether the character is an alphabet.
296 * @c: The character to check.
298 * Returns true if @c is an alphabet character, false otherwise.
300 static inline bool tomoyo_is_alphabet_char(const char c)
302 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
306 * tomoyo_make_byte - Make byte value from three octal characters.
308 * @c1: The first character.
309 * @c2: The second character.
310 * @c3: The third character.
312 * Returns byte value.
314 static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
316 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
320 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
322 * @src: Pointer to pointer to the string.
323 * @find: Pointer to the keyword.
325 * Returns true if @src starts with @find, false otherwise.
327 * The @src is updated to point the first character after the @find
328 * if @src starts with @find.
330 static bool tomoyo_str_starts(char **src, const char *find)
332 const int len = strlen(find);
335 if (strncmp(tmp, find, len))
343 * tomoyo_normalize_line - Format string.
345 * @buffer: The line to normalize.
347 * Leading and trailing whitespaces are removed.
348 * Multiple whitespaces are packed into single space.
352 static void tomoyo_normalize_line(unsigned char *buffer)
354 unsigned char *sp = buffer;
355 unsigned char *dp = buffer;
358 while (tomoyo_is_invalid(*sp))
364 while (tomoyo_is_valid(*sp))
366 while (tomoyo_is_invalid(*sp))
373 * tomoyo_tokenize - Tokenize string.
375 * @buffer: The line to tokenize.
376 * @w: Pointer to "char *".
379 * Returns true on success, false otherwise.
381 bool tomoyo_tokenize(char *buffer, char *w[], size_t size)
383 int count = size / sizeof(char *);
385 for (i = 0; i < count; i++)
387 for (i = 0; i < count; i++) {
388 char *cp = strchr(buffer, ' ');
396 return i < count || !*buffer;
400 * tomoyo_is_correct_path - Validate a pathname.
401 * @filename: The pathname to check.
402 * @start_type: Should the pathname start with '/'?
403 * 1 = must / -1 = must not / 0 = don't care
404 * @pattern_type: Can the pathname contain a wildcard?
405 * 1 = must / -1 = must not / 0 = don't care
406 * @end_type: Should the pathname end with '/'?
407 * 1 = must / -1 = must not / 0 = don't care
409 * Check whether the given filename follows the naming rules.
410 * Returns true if @filename follows the naming rules, false otherwise.
412 bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
413 const s8 pattern_type, const s8 end_type)
415 const char *const start = filename;
416 bool in_repetition = false;
417 bool contains_pattern = false;
425 if (start_type == 1) { /* Must start with '/' */
428 } else if (start_type == -1) { /* Must not start with '/' */
433 c = *(filename + strlen(filename) - 1);
434 if (end_type == 1) { /* Must end with '/' */
437 } else if (end_type == -1) { /* Must not end with '/' */
448 case '\\': /* "\\" */
460 if (pattern_type == -1)
461 break; /* Must not contain pattern */
462 contains_pattern = true;
464 case '{': /* "/\{" */
465 if (filename - 3 < start ||
466 *(filename - 3) != '/')
468 if (pattern_type == -1)
469 break; /* Must not contain pattern */
470 contains_pattern = true;
471 in_repetition = true;
473 case '}': /* "\}/" */
474 if (*filename != '/')
478 in_repetition = false;
480 case '0': /* "\ooo" */
485 if (d < '0' || d > '7')
488 if (e < '0' || e > '7')
490 c = tomoyo_make_byte(c, d, e);
491 if (tomoyo_is_invalid(c))
492 continue; /* pattern is not \000 */
495 } else if (in_repetition && c == '/') {
497 } else if (tomoyo_is_invalid(c)) {
501 if (pattern_type == 1) { /* Must contain pattern */
502 if (!contains_pattern)
513 * tomoyo_is_correct_domain - Check whether the given domainname follows the naming rules.
514 * @domainname: The domainname to check.
516 * Returns true if @domainname follows the naming rules, false otherwise.
518 bool tomoyo_is_correct_domain(const unsigned char *domainname)
524 if (!domainname || strncmp(domainname, TOMOYO_ROOT_NAME,
525 TOMOYO_ROOT_NAME_LEN))
527 domainname += TOMOYO_ROOT_NAME_LEN;
531 if (*domainname++ != ' ')
533 if (*domainname++ != '/')
535 while ((c = *domainname) != '\0' && c != ' ') {
540 case '\\': /* "\\" */
542 case '0': /* "\ooo" */
547 if (d < '0' || d > '7')
550 if (e < '0' || e > '7')
552 c = tomoyo_make_byte(c, d, e);
553 if (tomoyo_is_invalid(c))
554 /* pattern is not \000 */
558 } else if (tomoyo_is_invalid(c)) {
562 } while (*domainname);
569 * tomoyo_is_domain_def - Check whether the given token can be a domainname.
571 * @buffer: The token to check.
573 * Returns true if @buffer possibly be a domainname, false otherwise.
575 bool tomoyo_is_domain_def(const unsigned char *buffer)
577 return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
581 * tomoyo_find_domain - Find a domain by the given name.
583 * @domainname: The domainname to find.
585 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
587 * Caller holds tomoyo_read_lock().
589 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
591 struct tomoyo_domain_info *domain;
592 struct tomoyo_path_info name;
594 name.name = domainname;
595 tomoyo_fill_path_info(&name);
596 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
597 if (!domain->is_deleted &&
598 !tomoyo_pathcmp(&name, domain->domainname))
605 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
607 * @filename: The string to evaluate.
609 * Returns the initial length without a pattern in @filename.
611 static int tomoyo_const_part_length(const char *filename)
618 while ((c = *filename++) != '\0') {
625 case '\\': /* "\\" */
628 case '0': /* "\ooo" */
633 if (c < '0' || c > '7')
636 if (c < '0' || c > '7')
647 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
649 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
651 * The caller sets "struct tomoyo_path_info"->name.
653 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
655 const char *name = ptr->name;
656 const int len = strlen(name);
658 ptr->const_len = tomoyo_const_part_length(name);
659 ptr->is_dir = len && (name[len - 1] == '/');
660 ptr->is_patterned = (ptr->const_len < len);
661 ptr->hash = full_name_hash(name, len);
665 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character
668 * @filename: The start of string to check.
669 * @filename_end: The end of string to check.
670 * @pattern: The start of pattern to compare.
671 * @pattern_end: The end of pattern to compare.
673 * Returns true if @filename matches @pattern, false otherwise.
675 static bool tomoyo_file_matches_pattern2(const char *filename,
676 const char *filename_end,
678 const char *pattern_end)
680 while (filename < filename_end && pattern < pattern_end) {
682 if (*pattern != '\\') {
683 if (*filename++ != *pattern++)
695 } else if (c == '\\') {
696 if (filename[1] == '\\')
698 else if (tomoyo_is_byte_range(filename + 1))
707 if (*++filename != '\\')
719 if (!tomoyo_is_alphabet_char(c))
726 if (c == '\\' && tomoyo_is_byte_range(filename + 1)
727 && strncmp(filename + 1, pattern, 3) == 0) {
732 return false; /* Not matched. */
735 for (i = 0; i <= filename_end - filename; i++) {
736 if (tomoyo_file_matches_pattern2(
737 filename + i, filename_end,
738 pattern + 1, pattern_end))
741 if (c == '.' && *pattern == '@')
745 if (filename[i + 1] == '\\')
747 else if (tomoyo_is_byte_range(filename + i + 1))
750 break; /* Bad pattern. */
752 return false; /* Not matched. */
757 while (isdigit(filename[j]))
759 } else if (c == 'X') {
760 while (isxdigit(filename[j]))
762 } else if (c == 'A') {
763 while (tomoyo_is_alphabet_char(filename[j]))
766 for (i = 1; i <= j; i++) {
767 if (tomoyo_file_matches_pattern2(
768 filename + i, filename_end,
769 pattern + 1, pattern_end))
772 return false; /* Not matched or bad pattern. */
777 while (*pattern == '\\' &&
778 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
780 return filename == filename_end && pattern == pattern_end;
784 * tomoyo_file_matches_pattern - Pattern matching without without '/' character.
786 * @filename: The start of string to check.
787 * @filename_end: The end of string to check.
788 * @pattern: The start of pattern to compare.
789 * @pattern_end: The end of pattern to compare.
791 * Returns true if @filename matches @pattern, false otherwise.
793 static bool tomoyo_file_matches_pattern(const char *filename,
794 const char *filename_end,
796 const char *pattern_end)
798 const char *pattern_start = pattern;
802 while (pattern < pattern_end - 1) {
803 /* Split at "\-" pattern. */
804 if (*pattern++ != '\\' || *pattern++ != '-')
806 result = tomoyo_file_matches_pattern2(filename,
815 pattern_start = pattern;
817 result = tomoyo_file_matches_pattern2(filename, filename_end,
818 pattern_start, pattern_end);
819 return first ? result : !result;
823 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
825 * @f: The start of string to check.
826 * @p: The start of pattern to compare.
828 * Returns true if @f matches @p, false otherwise.
830 static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
832 const char *f_delimiter;
833 const char *p_delimiter;
836 f_delimiter = strchr(f, '/');
838 f_delimiter = f + strlen(f);
839 p_delimiter = strchr(p, '/');
841 p_delimiter = p + strlen(p);
842 if (*p == '\\' && *(p + 1) == '{')
844 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
854 /* Ignore trailing "\*" and "\@" in @pattern. */
856 (*(p + 1) == '*' || *(p + 1) == '@'))
861 * The "\{" pattern is permitted only after '/' character.
862 * This guarantees that below "*(p - 1)" is safe.
863 * Also, the "\}" pattern is permitted only before '/' character
864 * so that "\{" + "\}" pair will not break the "\-" operator.
866 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
867 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
868 return false; /* Bad pattern. */
870 /* Compare current component with pattern. */
871 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
874 /* Proceed to next component. */
879 /* Continue comparison. */
880 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
882 f_delimiter = strchr(f, '/');
883 } while (f_delimiter);
884 return false; /* Not matched. */
888 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
890 * @filename: The filename to check.
891 * @pattern: The pattern to compare.
893 * Returns true if matches, false otherwise.
895 * The following patterns are available.
897 * \ooo Octal representation of a byte.
898 * \* Zero or more repetitions of characters other than '/'.
899 * \@ Zero or more repetitions of characters other than '/' or '.'.
900 * \? 1 byte character other than '/'.
901 * \$ One or more repetitions of decimal digits.
902 * \+ 1 decimal digit.
903 * \X One or more repetitions of hexadecimal digits.
904 * \x 1 hexadecimal digit.
905 * \A One or more repetitions of alphabet characters.
906 * \a 1 alphabet character.
908 * \- Subtraction operator.
910 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
913 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
914 const struct tomoyo_path_info *pattern)
916 const char *f = filename->name;
917 const char *p = pattern->name;
918 const int len = pattern->const_len;
920 /* If @pattern doesn't contain pattern, I can use strcmp(). */
921 if (!pattern->is_patterned)
922 return !tomoyo_pathcmp(filename, pattern);
923 /* Don't compare directory and non-directory. */
924 if (filename->is_dir != pattern->is_dir)
926 /* Compare the initial length without patterns. */
927 if (strncmp(f, p, len))
931 return tomoyo_path_matches_pattern2(f, p);
935 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
937 * @head: Pointer to "struct tomoyo_io_buffer".
938 * @fmt: The printf()'s format string, followed by parameters.
940 * Returns true if output was written, false otherwise.
942 * The snprintf() will truncate, but tomoyo_io_printf() won't.
944 bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
948 int pos = head->read_avail;
949 int size = head->readbuf_size - pos;
954 len = vsnprintf(head->read_buf + pos, size, fmt, args);
956 if (pos + len >= head->readbuf_size)
958 head->read_avail += len;
963 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
965 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
967 * This function uses kzalloc(), so the caller must call kfree()
968 * if this function didn't return NULL.
970 static const char *tomoyo_get_exe(void)
972 struct mm_struct *mm = current->mm;
973 struct vm_area_struct *vma;
974 const char *cp = NULL;
978 down_read(&mm->mmap_sem);
979 for (vma = mm->mmap; vma; vma = vma->vm_next) {
980 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
981 cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
985 up_read(&mm->mmap_sem);
990 * tomoyo_check_flags - Check mode for specified functionality.
992 * @domain: Pointer to "struct tomoyo_domain_info".
993 * @index: The functionality to check mode.
995 * TOMOYO checks only process context.
996 * This code disables TOMOYO's enforcement in case the function is called from
999 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
1002 const u8 profile = domain->profile;
1004 if (WARN_ON(in_interrupt()))
1006 return tomoyo_policy_loaded && index < TOMOYO_MAX_CONTROL_INDEX
1007 #if TOMOYO_MAX_PROFILES != 256
1008 && profile < TOMOYO_MAX_PROFILES
1010 && tomoyo_profile_ptr[profile] ?
1011 tomoyo_profile_ptr[profile]->value[index] : 0;
1015 * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
1017 * @domain: Pointer to "struct tomoyo_domain_info".
1019 * Returns true if domain policy violation warning should be printed to
1022 bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain)
1024 return tomoyo_check_flags(domain, TOMOYO_VERBOSE) != 0;
1028 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1030 * @r: Pointer to "struct tomoyo_request_info".
1032 * Returns true if the domain is not exceeded quota, false otherwise.
1034 * Caller holds tomoyo_read_lock().
1036 static bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1038 unsigned int count = 0;
1039 struct tomoyo_domain_info *domain = r->domain;
1040 struct tomoyo_acl_info *ptr;
1042 if (r->mode != TOMOYO_CONFIG_LEARNING)
1046 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1047 switch (ptr->type) {
1050 case TOMOYO_TYPE_PATH_ACL:
1051 perm = container_of(ptr, struct tomoyo_path_acl, head)
1053 for (i = 0; i < TOMOYO_MAX_PATH_OPERATION; i++)
1054 if (perm & (1 << i))
1056 if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
1059 case TOMOYO_TYPE_PATH2_ACL:
1060 perm = container_of(ptr, struct tomoyo_path2_acl, head)
1062 for (i = 0; i < TOMOYO_MAX_PATH2_OPERATION; i++)
1063 if (perm & (1 << i))
1066 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1067 perm = container_of(ptr, struct tomoyo_path_number_acl,
1069 for (i = 0; i < TOMOYO_MAX_PATH_NUMBER_OPERATION; i++)
1070 if (perm & (1 << i))
1073 case TOMOYO_TYPE_PATH_NUMBER3_ACL:
1074 perm = container_of(ptr, struct tomoyo_path_number3_acl,
1076 for (i = 0; i < TOMOYO_MAX_PATH_NUMBER3_OPERATION; i++)
1077 if (perm & (1 << i))
1080 case TOMOYO_TYPE_MOUNT_ACL:
1081 if (!container_of(ptr, struct tomoyo_mount_acl, head)->
1086 if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
1088 if (!domain->quota_warned) {
1089 domain->quota_warned = true;
1090 printk(KERN_WARNING "TOMOYO-WARNING: "
1091 "Domain '%s' has so many ACLs to hold. "
1092 "Stopped learning mode.\n", domain->domainname->name);
1098 * tomoyo_find_or_assign_new_profile - Create a new profile.
1100 * @profile: Profile number to create.
1102 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
1104 static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
1107 struct tomoyo_profile *ptr = NULL;
1110 if (profile >= TOMOYO_MAX_PROFILES)
1112 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1114 ptr = tomoyo_profile_ptr[profile];
1117 ptr = kmalloc(sizeof(*ptr), GFP_NOFS);
1118 if (!tomoyo_memory_ok(ptr)) {
1123 for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
1124 ptr->value[i] = tomoyo_control_array[i].current_value;
1125 mb(); /* Avoid out-of-order execution. */
1126 tomoyo_profile_ptr[profile] = ptr;
1128 mutex_unlock(&tomoyo_policy_lock);
1133 * tomoyo_write_profile - Write to profile table.
1135 * @head: Pointer to "struct tomoyo_io_buffer".
1137 * Returns 0 on success, negative value otherwise.
1139 static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
1141 char *data = head->write_buf;
1145 struct tomoyo_profile *profile;
1148 cp = strchr(data, '-');
1151 if (strict_strtoul(data, 10, &num))
1155 profile = tomoyo_find_or_assign_new_profile(num);
1158 cp = strchr(data, '=');
1162 if (!strcmp(data, "COMMENT")) {
1163 const struct tomoyo_path_info *old_comment = profile->comment;
1164 profile->comment = tomoyo_get_name(cp + 1);
1165 tomoyo_put_name(old_comment);
1168 for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
1169 if (strcmp(data, tomoyo_control_array[i].keyword))
1171 if (sscanf(cp + 1, "%u", &value) != 1) {
1175 case TOMOYO_VERBOSE:
1176 modes = tomoyo_mode_2;
1179 modes = tomoyo_mode_4;
1182 for (j = 0; j < 4; j++) {
1183 if (strcmp(cp + 1, modes[j]))
1190 } else if (value > tomoyo_control_array[i].max_value) {
1191 value = tomoyo_control_array[i].max_value;
1193 profile->value[i] = value;
1200 * tomoyo_read_profile - Read from profile table.
1202 * @head: Pointer to "struct tomoyo_io_buffer".
1206 static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
1208 static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
1213 for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
1215 const u8 index = step / total;
1216 u8 type = step % total;
1217 const struct tomoyo_profile *profile
1218 = tomoyo_profile_ptr[index];
1219 head->read_step = step;
1222 if (!type) { /* Print profile' comment tag. */
1223 if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
1224 index, profile->comment ?
1225 profile->comment->name : ""))
1230 if (type < TOMOYO_MAX_CONTROL_INDEX) {
1231 const unsigned int value = profile->value[type];
1232 const char **modes = NULL;
1234 = tomoyo_control_array[type].keyword;
1235 switch (tomoyo_control_array[type].max_value) {
1237 modes = tomoyo_mode_4;
1240 modes = tomoyo_mode_2;
1244 if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
1245 keyword, modes[value]))
1248 if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
1254 if (step == TOMOYO_MAX_PROFILES * total)
1255 head->read_eof = true;
1260 * tomoyo_policy_manager_list is used for holding list of domainnames or
1261 * programs which are permitted to modify configuration via
1262 * /sys/kernel/security/tomoyo/ interface.
1264 * An entry is added by
1266 * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
1267 * /sys/kernel/security/tomoyo/manager
1268 * (if you want to specify by a domainname)
1272 * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
1273 * (if you want to specify by a program's location)
1277 * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
1278 * /sys/kernel/security/tomoyo/manager
1282 * # echo 'delete /usr/lib/ccs/editpolicy' > \
1283 * /sys/kernel/security/tomoyo/manager
1285 * and all entries are retrieved by
1287 * # cat /sys/kernel/security/tomoyo/manager
1289 LIST_HEAD(tomoyo_policy_manager_list);
1292 * tomoyo_update_manager_entry - Add a manager entry.
1294 * @manager: The path to manager or the domainnamme.
1295 * @is_delete: True if it is a delete request.
1297 * Returns 0 on success, negative value otherwise.
1299 * Caller holds tomoyo_read_lock().
1301 static int tomoyo_update_manager_entry(const char *manager,
1302 const bool is_delete)
1304 struct tomoyo_policy_manager_entry *ptr;
1305 struct tomoyo_policy_manager_entry e = { };
1306 int error = is_delete ? -ENOENT : -ENOMEM;
1308 if (tomoyo_is_domain_def(manager)) {
1309 if (!tomoyo_is_correct_domain(manager))
1313 if (!tomoyo_is_correct_path(manager, 1, -1, -1))
1316 e.manager = tomoyo_get_name(manager);
1319 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1321 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
1322 if (ptr->manager != e.manager)
1324 ptr->is_deleted = is_delete;
1328 if (!is_delete && error) {
1329 struct tomoyo_policy_manager_entry *entry =
1330 tomoyo_commit_ok(&e, sizeof(e));
1332 list_add_tail_rcu(&entry->list,
1333 &tomoyo_policy_manager_list);
1337 mutex_unlock(&tomoyo_policy_lock);
1339 tomoyo_put_name(e.manager);
1344 * tomoyo_write_manager_policy - Write manager policy.
1346 * @head: Pointer to "struct tomoyo_io_buffer".
1348 * Returns 0 on success, negative value otherwise.
1350 * Caller holds tomoyo_read_lock().
1352 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
1354 char *data = head->write_buf;
1355 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1357 if (!strcmp(data, "manage_by_non_root")) {
1358 tomoyo_manage_by_non_root = !is_delete;
1361 return tomoyo_update_manager_entry(data, is_delete);
1365 * tomoyo_read_manager_policy - Read manager policy.
1367 * @head: Pointer to "struct tomoyo_io_buffer".
1371 * Caller holds tomoyo_read_lock().
1373 static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
1375 struct list_head *pos;
1380 list_for_each_cookie(pos, head->read_var2,
1381 &tomoyo_policy_manager_list) {
1382 struct tomoyo_policy_manager_entry *ptr;
1383 ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
1385 if (ptr->is_deleted)
1387 done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
1391 head->read_eof = done;
1396 * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
1398 * Returns true if the current process is permitted to modify policy
1399 * via /sys/kernel/security/tomoyo/ interface.
1401 * Caller holds tomoyo_read_lock().
1403 static bool tomoyo_is_policy_manager(void)
1405 struct tomoyo_policy_manager_entry *ptr;
1407 const struct task_struct *task = current;
1408 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
1411 if (!tomoyo_policy_loaded)
1413 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
1415 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
1416 if (!ptr->is_deleted && ptr->is_domain
1417 && !tomoyo_pathcmp(domainname, ptr->manager)) {
1424 exe = tomoyo_get_exe();
1427 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
1428 if (!ptr->is_deleted && !ptr->is_domain
1429 && !strcmp(exe, ptr->manager->name)) {
1434 if (!found) { /* Reduce error messages. */
1435 static pid_t last_pid;
1436 const pid_t pid = current->pid;
1437 if (last_pid != pid) {
1438 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1439 "update policies.\n", domainname->name, exe);
1448 * tomoyo_is_select_one - Parse select command.
1450 * @head: Pointer to "struct tomoyo_io_buffer".
1451 * @data: String to parse.
1453 * Returns true on success, false otherwise.
1455 * Caller holds tomoyo_read_lock().
1457 static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
1461 struct tomoyo_domain_info *domain = NULL;
1463 if (sscanf(data, "pid=%u", &pid) == 1) {
1464 struct task_struct *p;
1466 read_lock(&tasklist_lock);
1467 p = find_task_by_vpid(pid);
1469 domain = tomoyo_real_domain(p);
1470 read_unlock(&tasklist_lock);
1472 } else if (!strncmp(data, "domain=", 7)) {
1473 if (tomoyo_is_domain_def(data + 7))
1474 domain = tomoyo_find_domain(data + 7);
1477 head->write_var1 = domain;
1478 /* Accessing read_buf is safe because head->io_sem is held. */
1479 if (!head->read_buf)
1480 return true; /* Do nothing if open(O_WRONLY). */
1481 head->read_avail = 0;
1482 tomoyo_io_printf(head, "# select %s\n", data);
1483 head->read_single_domain = true;
1484 head->read_eof = !domain;
1486 struct tomoyo_domain_info *d;
1487 head->read_var1 = NULL;
1488 list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
1491 head->read_var1 = &d->list;
1493 head->read_var2 = NULL;
1495 head->read_step = 0;
1496 if (domain->is_deleted)
1497 tomoyo_io_printf(head, "# This is a deleted domain.\n");
1503 * tomoyo_delete_domain - Delete a domain.
1505 * @domainname: The name of domain.
1509 * Caller holds tomoyo_read_lock().
1511 static int tomoyo_delete_domain(char *domainname)
1513 struct tomoyo_domain_info *domain;
1514 struct tomoyo_path_info name;
1516 name.name = domainname;
1517 tomoyo_fill_path_info(&name);
1518 if (mutex_lock_interruptible(&tomoyo_policy_lock))
1520 /* Is there an active domain? */
1521 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
1522 /* Never delete tomoyo_kernel_domain */
1523 if (domain == &tomoyo_kernel_domain)
1525 if (domain->is_deleted ||
1526 tomoyo_pathcmp(domain->domainname, &name))
1528 domain->is_deleted = true;
1531 mutex_unlock(&tomoyo_policy_lock);
1536 * tomoyo_write_domain_policy2 - Write domain policy.
1538 * @head: Pointer to "struct tomoyo_io_buffer".
1540 * Returns 0 on success, negative value otherwise.
1542 * Caller holds tomoyo_read_lock().
1544 static int tomoyo_write_domain_policy2(char *data,
1545 struct tomoyo_domain_info *domain,
1546 const bool is_delete)
1548 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
1549 return tomoyo_write_mount_policy(data, domain, is_delete);
1550 return tomoyo_write_file_policy(data, domain, is_delete);
1554 * tomoyo_write_domain_policy - Write domain policy.
1556 * @head: Pointer to "struct tomoyo_io_buffer".
1558 * Returns 0 on success, negative value otherwise.
1560 * Caller holds tomoyo_read_lock().
1562 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
1564 char *data = head->write_buf;
1565 struct tomoyo_domain_info *domain = head->write_var1;
1566 bool is_delete = false;
1567 bool is_select = false;
1568 unsigned int profile;
1570 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
1572 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
1574 if (is_select && tomoyo_is_select_one(head, data))
1576 /* Don't allow updating policies by non manager programs. */
1577 if (!tomoyo_is_policy_manager())
1579 if (tomoyo_is_domain_def(data)) {
1582 tomoyo_delete_domain(data);
1584 domain = tomoyo_find_domain(data);
1586 domain = tomoyo_find_or_assign_new_domain(data, 0);
1587 head->write_var1 = domain;
1593 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
1594 && profile < TOMOYO_MAX_PROFILES) {
1595 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
1596 domain->profile = (u8) profile;
1599 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1600 domain->ignore_global_allow_read = !is_delete;
1603 return tomoyo_write_domain_policy2(data, domain, is_delete);
1607 * tomoyo_print_path_acl - Print a single path ACL entry.
1609 * @head: Pointer to "struct tomoyo_io_buffer".
1610 * @ptr: Pointer to "struct tomoyo_path_acl".
1612 * Returns true on success, false otherwise.
1614 static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
1615 struct tomoyo_path_acl *ptr)
1619 const u16 perm = ptr->perm;
1621 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
1622 if (!(perm & (1 << bit)))
1624 /* Print "read/write" instead of "read" and "write". */
1625 if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
1626 && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
1628 pos = head->read_avail;
1629 if (!tomoyo_io_printf(head, "allow_%s ",
1630 tomoyo_path2keyword(bit)) ||
1631 !tomoyo_print_name_union(head, &ptr->name) ||
1632 !tomoyo_io_printf(head, "\n"))
1638 head->read_bit = bit;
1639 head->read_avail = pos;
1644 * tomoyo_print_path2_acl - Print a double path ACL entry.
1646 * @head: Pointer to "struct tomoyo_io_buffer".
1647 * @ptr: Pointer to "struct tomoyo_path2_acl".
1649 * Returns true on success, false otherwise.
1651 static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
1652 struct tomoyo_path2_acl *ptr)
1655 const u8 perm = ptr->perm;
1658 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
1659 if (!(perm & (1 << bit)))
1661 pos = head->read_avail;
1662 if (!tomoyo_io_printf(head, "allow_%s ",
1663 tomoyo_path22keyword(bit)) ||
1664 !tomoyo_print_name_union(head, &ptr->name1) ||
1665 !tomoyo_print_name_union(head, &ptr->name2) ||
1666 !tomoyo_io_printf(head, "\n"))
1672 head->read_bit = bit;
1673 head->read_avail = pos;
1678 * tomoyo_print_path_number_acl - Print a path_number ACL entry.
1680 * @head: Pointer to "struct tomoyo_io_buffer".
1681 * @ptr: Pointer to "struct tomoyo_path_number_acl".
1683 * Returns true on success, false otherwise.
1685 static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
1686 struct tomoyo_path_number_acl *ptr)
1690 const u8 perm = ptr->perm;
1691 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
1693 if (!(perm & (1 << bit)))
1695 pos = head->read_avail;
1696 if (!tomoyo_io_printf(head, "allow_%s",
1697 tomoyo_path_number2keyword(bit)) ||
1698 !tomoyo_print_name_union(head, &ptr->name) ||
1699 !tomoyo_print_number_union(head, &ptr->number) ||
1700 !tomoyo_io_printf(head, "\n"))
1706 head->read_bit = bit;
1707 head->read_avail = pos;
1712 * tomoyo_print_path_number3_acl - Print a path_number3 ACL entry.
1714 * @head: Pointer to "struct tomoyo_io_buffer".
1715 * @ptr: Pointer to "struct tomoyo_path_number3_acl".
1717 * Returns true on success, false otherwise.
1719 static bool tomoyo_print_path_number3_acl(struct tomoyo_io_buffer *head,
1720 struct tomoyo_path_number3_acl *ptr)
1724 const u16 perm = ptr->perm;
1725 for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER3_OPERATION;
1727 if (!(perm & (1 << bit)))
1729 pos = head->read_avail;
1730 if (!tomoyo_io_printf(head, "allow_%s",
1731 tomoyo_path_number32keyword(bit)) ||
1732 !tomoyo_print_name_union(head, &ptr->name) ||
1733 !tomoyo_print_number_union(head, &ptr->mode) ||
1734 !tomoyo_print_number_union(head, &ptr->major) ||
1735 !tomoyo_print_number_union(head, &ptr->minor) ||
1736 !tomoyo_io_printf(head, "\n"))
1742 head->read_bit = bit;
1743 head->read_avail = pos;
1748 * tomoyo_print_mount_acl - Print a mount ACL entry.
1750 * @head: Pointer to "struct tomoyo_io_buffer".
1751 * @ptr: Pointer to "struct tomoyo_mount_acl".
1753 * Returns true on success, false otherwise.
1755 static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
1756 struct tomoyo_mount_acl *ptr)
1758 const int pos = head->read_avail;
1759 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
1760 !tomoyo_print_name_union(head, &ptr->dev_name) ||
1761 !tomoyo_print_name_union(head, &ptr->dir_name) ||
1762 !tomoyo_print_name_union(head, &ptr->fs_type) ||
1763 !tomoyo_print_number_union(head, &ptr->flags) ||
1764 !tomoyo_io_printf(head, "\n")) {
1765 head->read_avail = pos;
1772 * tomoyo_print_entry - Print an ACL entry.
1774 * @head: Pointer to "struct tomoyo_io_buffer".
1775 * @ptr: Pointer to an ACL entry.
1777 * Returns true on success, false otherwise.
1779 static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
1780 struct tomoyo_acl_info *ptr)
1782 const u8 acl_type = ptr->type;
1784 if (acl_type == TOMOYO_TYPE_PATH_ACL) {
1785 struct tomoyo_path_acl *acl
1786 = container_of(ptr, struct tomoyo_path_acl, head);
1787 return tomoyo_print_path_acl(head, acl);
1789 if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
1790 struct tomoyo_path2_acl *acl
1791 = container_of(ptr, struct tomoyo_path2_acl, head);
1792 return tomoyo_print_path2_acl(head, acl);
1794 if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1795 struct tomoyo_path_number_acl *acl
1796 = container_of(ptr, struct tomoyo_path_number_acl,
1798 return tomoyo_print_path_number_acl(head, acl);
1800 if (acl_type == TOMOYO_TYPE_PATH_NUMBER3_ACL) {
1801 struct tomoyo_path_number3_acl *acl
1802 = container_of(ptr, struct tomoyo_path_number3_acl,
1804 return tomoyo_print_path_number3_acl(head, acl);
1806 if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1807 struct tomoyo_mount_acl *acl
1808 = container_of(ptr, struct tomoyo_mount_acl, head);
1809 return tomoyo_print_mount_acl(head, acl);
1811 BUG(); /* This must not happen. */
1816 * tomoyo_read_domain_policy - Read domain policy.
1818 * @head: Pointer to "struct tomoyo_io_buffer".
1822 * Caller holds tomoyo_read_lock().
1824 static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
1826 struct list_head *dpos;
1827 struct list_head *apos;
1832 if (head->read_step == 0)
1833 head->read_step = 1;
1834 list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
1835 struct tomoyo_domain_info *domain;
1836 const char *quota_exceeded = "";
1837 const char *transition_failed = "";
1838 const char *ignore_global_allow_read = "";
1839 domain = list_entry(dpos, struct tomoyo_domain_info, list);
1840 if (head->read_step != 1)
1842 if (domain->is_deleted && !head->read_single_domain)
1844 /* Print domainname and flags. */
1845 if (domain->quota_warned)
1846 quota_exceeded = "quota_exceeded\n";
1847 if (domain->transition_failed)
1848 transition_failed = "transition_failed\n";
1849 if (domain->ignore_global_allow_read)
1850 ignore_global_allow_read
1851 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1852 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1854 domain->domainname->name,
1855 domain->profile, quota_exceeded,
1857 ignore_global_allow_read);
1860 head->read_step = 2;
1862 if (head->read_step == 3)
1864 /* Print ACL entries in the domain. */
1865 list_for_each_cookie(apos, head->read_var2,
1866 &domain->acl_info_list) {
1867 struct tomoyo_acl_info *ptr
1868 = list_entry(apos, struct tomoyo_acl_info,
1870 done = tomoyo_print_entry(head, ptr);
1876 head->read_step = 3;
1878 done = tomoyo_io_printf(head, "\n");
1881 head->read_step = 1;
1882 if (head->read_single_domain)
1885 head->read_eof = done;
1890 * tomoyo_write_domain_profile - Assign profile for specified domain.
1892 * @head: Pointer to "struct tomoyo_io_buffer".
1894 * Returns 0 on success, -EINVAL otherwise.
1896 * This is equivalent to doing
1898 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1899 * /usr/lib/ccs/loadpolicy -d
1901 * Caller holds tomoyo_read_lock().
1903 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1905 char *data = head->write_buf;
1906 char *cp = strchr(data, ' ');
1907 struct tomoyo_domain_info *domain;
1908 unsigned long profile;
1913 domain = tomoyo_find_domain(cp + 1);
1914 if (strict_strtoul(data, 10, &profile))
1916 if (domain && profile < TOMOYO_MAX_PROFILES
1917 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1918 domain->profile = (u8) profile;
1923 * tomoyo_read_domain_profile - Read only domainname and profile.
1925 * @head: Pointer to "struct tomoyo_io_buffer".
1927 * Returns list of profile number and domainname pairs.
1929 * This is equivalent to doing
1931 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1932 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1933 * domainname = $0; } else if ( $1 == "use_profile" ) {
1934 * print $2 " " domainname; domainname = ""; } } ; '
1936 * Caller holds tomoyo_read_lock().
1938 static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1940 struct list_head *pos;
1945 list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
1946 struct tomoyo_domain_info *domain;
1947 domain = list_entry(pos, struct tomoyo_domain_info, list);
1948 if (domain->is_deleted)
1950 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1951 domain->domainname->name);
1955 head->read_eof = done;
1960 * tomoyo_write_pid: Specify PID to obtain domainname.
1962 * @head: Pointer to "struct tomoyo_io_buffer".
1966 static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1969 /* No error check. */
1970 strict_strtoul(head->write_buf, 10, &pid);
1971 head->read_step = (int) pid;
1972 head->read_eof = false;
1977 * tomoyo_read_pid - Get domainname of the specified PID.
1979 * @head: Pointer to "struct tomoyo_io_buffer".
1981 * Returns the domainname which the specified PID is in on success,
1982 * empty string otherwise.
1983 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1984 * using read()/write() interface rather than sysctl() interface.
1986 static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
1988 if (head->read_avail == 0 && !head->read_eof) {
1989 const int pid = head->read_step;
1990 struct task_struct *p;
1991 struct tomoyo_domain_info *domain = NULL;
1993 read_lock(&tasklist_lock);
1994 p = find_task_by_vpid(pid);
1996 domain = tomoyo_real_domain(p);
1997 read_unlock(&tasklist_lock);
2000 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
2001 domain->domainname->name);
2002 head->read_eof = true;
2008 * tomoyo_write_exception_policy - Write exception policy.
2010 * @head: Pointer to "struct tomoyo_io_buffer".
2012 * Returns 0 on success, negative value otherwise.
2014 * Caller holds tomoyo_read_lock().
2016 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
2018 char *data = head->write_buf;
2019 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
2021 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
2022 return tomoyo_write_domain_keeper_policy(data, false,
2024 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
2025 return tomoyo_write_domain_keeper_policy(data, true, is_delete);
2026 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
2027 return tomoyo_write_domain_initializer_policy(data, false,
2029 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
2030 return tomoyo_write_domain_initializer_policy(data, true,
2032 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
2033 return tomoyo_write_alias_policy(data, is_delete);
2034 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
2035 return tomoyo_write_globally_readable_policy(data, is_delete);
2036 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
2037 return tomoyo_write_pattern_policy(data, is_delete);
2038 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
2039 return tomoyo_write_no_rewrite_policy(data, is_delete);
2040 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
2041 return tomoyo_write_path_group_policy(data, is_delete);
2042 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
2043 return tomoyo_write_number_group_policy(data, is_delete);
2048 * tomoyo_read_exception_policy - Read exception policy.
2050 * @head: Pointer to "struct tomoyo_io_buffer".
2052 * Returns 0 on success, -EINVAL otherwise.
2054 * Caller holds tomoyo_read_lock().
2056 static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
2058 if (!head->read_eof) {
2059 switch (head->read_step) {
2061 head->read_var2 = NULL;
2062 head->read_step = 1;
2064 if (!tomoyo_read_domain_keeper_policy(head))
2066 head->read_var2 = NULL;
2067 head->read_step = 2;
2069 if (!tomoyo_read_globally_readable_policy(head))
2071 head->read_var2 = NULL;
2072 head->read_step = 3;
2074 head->read_var2 = NULL;
2075 head->read_step = 4;
2077 if (!tomoyo_read_domain_initializer_policy(head))
2079 head->read_var2 = NULL;
2080 head->read_step = 5;
2082 if (!tomoyo_read_alias_policy(head))
2084 head->read_var2 = NULL;
2085 head->read_step = 6;
2087 head->read_var2 = NULL;
2088 head->read_step = 7;
2090 if (!tomoyo_read_file_pattern(head))
2092 head->read_var2 = NULL;
2093 head->read_step = 8;
2095 if (!tomoyo_read_no_rewrite_policy(head))
2097 head->read_var2 = NULL;
2098 head->read_step = 9;
2100 if (!tomoyo_read_path_group_policy(head))
2102 head->read_var1 = NULL;
2103 head->read_var2 = NULL;
2104 head->read_step = 10;
2106 if (!tomoyo_read_number_group_policy(head))
2108 head->read_var1 = NULL;
2109 head->read_var2 = NULL;
2110 head->read_step = 11;
2112 head->read_eof = true;
2121 /* path to policy loader */
2122 static const char *tomoyo_loader = "/sbin/tomoyo-init";
2125 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
2127 * Returns true if /sbin/tomoyo-init exists, false otherwise.
2129 static bool tomoyo_policy_loader_exists(void)
2132 * Don't activate MAC if the policy loader doesn't exist.
2133 * If the initrd includes /sbin/init but real-root-dev has not
2134 * mounted on / yet, activating MAC will block the system since
2135 * policies are not loaded yet.
2136 * Thus, let do_execve() call this function everytime.
2140 if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
2141 printk(KERN_INFO "Not activating Mandatory Access Control now "
2142 "since %s doesn't exist.\n", tomoyo_loader);
2150 * tomoyo_load_policy - Run external policy loader to load policy.
2152 * @filename: The program about to start.
2154 * This function checks whether @filename is /sbin/init , and if so
2155 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
2156 * and then continues invocation of /sbin/init.
2157 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
2158 * writes to /sys/kernel/security/tomoyo/ interfaces.
2162 void tomoyo_load_policy(const char *filename)
2167 if (tomoyo_policy_loaded)
2170 * Check filename is /sbin/init or /sbin/tomoyo-start.
2171 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
2173 * You can create /sbin/tomoyo-start by
2174 * "ln -s /bin/true /sbin/tomoyo-start".
2176 if (strcmp(filename, "/sbin/init") &&
2177 strcmp(filename, "/sbin/tomoyo-start"))
2179 if (!tomoyo_policy_loader_exists())
2182 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2184 argv[0] = (char *) tomoyo_loader;
2187 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2189 call_usermodehelper(argv[0], argv, envp, 1);
2191 printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
2192 printk(KERN_INFO "Mandatory Access Control activated.\n");
2193 tomoyo_policy_loaded = true;
2194 { /* Check all profiles currently assigned to domains are defined. */
2195 struct tomoyo_domain_info *domain;
2196 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
2197 const u8 profile = domain->profile;
2198 if (tomoyo_profile_ptr[profile])
2200 panic("Profile %u (used by '%s') not defined.\n",
2201 profile, domain->domainname->name);
2207 * tomoyo_print_header - Get header line of audit log.
2209 * @r: Pointer to "struct tomoyo_request_info".
2211 * Returns string representation.
2213 * This function uses kmalloc(), so caller must kfree() if this function
2214 * didn't return NULL.
2216 static char *tomoyo_print_header(struct tomoyo_request_info *r)
2218 static const char *tomoyo_mode_4[4] = {
2219 "disabled", "learning", "permissive", "enforcing"
2222 const pid_t gpid = task_pid_nr(current);
2223 static const int tomoyo_buffer_len = 4096;
2224 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
2227 do_gettimeofday(&tv);
2228 snprintf(buffer, tomoyo_buffer_len - 1,
2229 "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
2230 " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
2231 " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
2232 tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
2233 (pid_t) sys_getpid(), (pid_t) sys_getppid(),
2234 current_uid(), current_gid(), current_euid(),
2235 current_egid(), current_suid(), current_sgid(),
2236 current_fsuid(), current_fsgid());
2241 * tomoyo_init_audit_log - Allocate buffer for audit logs.
2243 * @len: Required size.
2244 * @r: Pointer to "struct tomoyo_request_info".
2246 * Returns pointer to allocated memory.
2248 * The @len is updated to add the header lines' size on success.
2250 * This function uses kzalloc(), so caller must kfree() if this function
2251 * didn't return NULL.
2253 static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
2257 const char *domainname;
2259 r->domain = tomoyo_domain();
2260 domainname = r->domain->domainname->name;
2261 header = tomoyo_print_header(r);
2264 *len += strlen(domainname) + strlen(header) + 10;
2265 buf = kzalloc(*len, GFP_NOFS);
2267 snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
2272 /* Wait queue for tomoyo_query_list. */
2273 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
2275 /* Lock for manipulating tomoyo_query_list. */
2276 static DEFINE_SPINLOCK(tomoyo_query_list_lock);
2278 /* Structure for query. */
2279 struct tomoyo_query_entry {
2280 struct list_head list;
2283 unsigned int serial;
2288 /* The list for "struct tomoyo_query_entry". */
2289 static LIST_HEAD(tomoyo_query_list);
2292 * Number of "struct file" referring /sys/kernel/security/tomoyo/query
2295 static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
2298 * tomoyo_supervisor - Ask for the supervisor's decision.
2300 * @r: Pointer to "struct tomoyo_request_info".
2301 * @fmt: The printf()'s format string, followed by parameters.
2303 * Returns 0 if the supervisor decided to permit the access request which
2304 * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
2305 * supervisor decided to retry the access request which violated the policy in
2306 * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2308 int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
2314 static unsigned int tomoyo_serial;
2315 struct tomoyo_query_entry *tomoyo_query_entry = NULL;
2316 bool quota_exceeded = false;
2320 case TOMOYO_CONFIG_LEARNING:
2321 if (!tomoyo_domain_quota_is_ok(r))
2323 va_start(args, fmt);
2324 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2326 buffer = kmalloc(len, GFP_NOFS);
2329 va_start(args, fmt);
2330 vsnprintf(buffer, len - 1, fmt, args);
2332 tomoyo_normalize_line(buffer);
2333 tomoyo_write_domain_policy2(buffer, r->domain, false);
2336 case TOMOYO_CONFIG_PERMISSIVE:
2340 r->domain = tomoyo_domain();
2341 if (!atomic_read(&tomoyo_query_observers))
2343 va_start(args, fmt);
2344 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2346 header = tomoyo_init_audit_log(&len, r);
2349 tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
2350 if (!tomoyo_query_entry)
2352 tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
2353 if (!tomoyo_query_entry->query)
2355 len = ksize(tomoyo_query_entry->query);
2356 INIT_LIST_HEAD(&tomoyo_query_entry->list);
2357 spin_lock(&tomoyo_query_list_lock);
2358 if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
2359 sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
2360 quota_exceeded = true;
2362 tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
2363 tomoyo_query_entry->serial = tomoyo_serial++;
2365 spin_unlock(&tomoyo_query_list_lock);
2368 pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
2369 tomoyo_query_entry->serial, r->retry, header);
2372 va_start(args, fmt);
2373 vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
2374 tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
2376 spin_lock(&tomoyo_query_list_lock);
2377 list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
2378 spin_unlock(&tomoyo_query_list_lock);
2379 /* Give 10 seconds for supervisor's opinion. */
2380 for (tomoyo_query_entry->timer = 0;
2381 atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
2382 tomoyo_query_entry->timer++) {
2383 wake_up(&tomoyo_query_wait);
2384 set_current_state(TASK_INTERRUPTIBLE);
2385 schedule_timeout(HZ / 10);
2386 if (tomoyo_query_entry->answer)
2389 spin_lock(&tomoyo_query_list_lock);
2390 list_del(&tomoyo_query_entry->list);
2391 tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
2392 spin_unlock(&tomoyo_query_list_lock);
2393 switch (tomoyo_query_entry->answer) {
2394 case 3: /* Asked to retry by administrator. */
2395 error = TOMOYO_RETRY_REQUEST;
2399 /* Granted by administrator. */
2406 /* Rejected by administrator. */
2410 if (tomoyo_query_entry)
2411 kfree(tomoyo_query_entry->query);
2412 kfree(tomoyo_query_entry);
2418 * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
2420 * @file: Pointer to "struct file".
2421 * @wait: Pointer to "poll_table".
2423 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2425 * Waits for access requests which violated policy in enforcing mode.
2427 static int tomoyo_poll_query(struct file *file, poll_table *wait)
2429 struct list_head *tmp;
2432 for (i = 0; i < 2; i++) {
2433 spin_lock(&tomoyo_query_list_lock);
2434 list_for_each(tmp, &tomoyo_query_list) {
2435 struct tomoyo_query_entry *ptr
2436 = list_entry(tmp, struct tomoyo_query_entry,
2443 spin_unlock(&tomoyo_query_list_lock);
2445 return POLLIN | POLLRDNORM;
2448 poll_wait(file, &tomoyo_query_wait, wait);
2454 * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
2456 * @head: Pointer to "struct tomoyo_io_buffer".
2460 static int tomoyo_read_query(struct tomoyo_io_buffer *head)
2462 struct list_head *tmp;
2466 if (head->read_avail)
2468 if (head->read_buf) {
2469 kfree(head->read_buf);
2470 head->read_buf = NULL;
2471 head->readbuf_size = 0;
2473 spin_lock(&tomoyo_query_list_lock);
2474 list_for_each(tmp, &tomoyo_query_list) {
2475 struct tomoyo_query_entry *ptr
2476 = list_entry(tmp, struct tomoyo_query_entry, list);
2479 if (pos++ != head->read_step)
2481 len = ptr->query_len;
2484 spin_unlock(&tomoyo_query_list_lock);
2486 head->read_step = 0;
2489 buf = kzalloc(len, GFP_NOFS);
2493 spin_lock(&tomoyo_query_list_lock);
2494 list_for_each(tmp, &tomoyo_query_list) {
2495 struct tomoyo_query_entry *ptr
2496 = list_entry(tmp, struct tomoyo_query_entry, list);
2499 if (pos++ != head->read_step)
2502 * Some query can be skipped because tomoyo_query_list
2503 * can change, but I don't care.
2505 if (len == ptr->query_len)
2506 memmove(buf, ptr->query, len);
2509 spin_unlock(&tomoyo_query_list_lock);
2511 head->read_avail = len;
2512 head->readbuf_size = head->read_avail;
2513 head->read_buf = buf;
2522 * tomoyo_write_answer - Write the supervisor's decision.
2524 * @head: Pointer to "struct tomoyo_io_buffer".
2526 * Returns 0 on success, -EINVAL otherwise.
2528 static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
2530 char *data = head->write_buf;
2531 struct list_head *tmp;
2532 unsigned int serial;
2533 unsigned int answer;
2534 spin_lock(&tomoyo_query_list_lock);
2535 list_for_each(tmp, &tomoyo_query_list) {
2536 struct tomoyo_query_entry *ptr
2537 = list_entry(tmp, struct tomoyo_query_entry, list);
2540 spin_unlock(&tomoyo_query_list_lock);
2541 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2543 spin_lock(&tomoyo_query_list_lock);
2544 list_for_each(tmp, &tomoyo_query_list) {
2545 struct tomoyo_query_entry *ptr
2546 = list_entry(tmp, struct tomoyo_query_entry, list);
2547 if (ptr->serial != serial)
2550 ptr->answer = answer;
2553 spin_unlock(&tomoyo_query_list_lock);
2558 * tomoyo_read_version: Get version.
2560 * @head: Pointer to "struct tomoyo_io_buffer".
2562 * Returns version information.
2564 static int tomoyo_read_version(struct tomoyo_io_buffer *head)
2566 if (!head->read_eof) {
2567 tomoyo_io_printf(head, "2.2.0");
2568 head->read_eof = true;
2574 * tomoyo_read_self_domain - Get the current process's domainname.
2576 * @head: Pointer to "struct tomoyo_io_buffer".
2578 * Returns the current process's domainname.
2580 static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
2582 if (!head->read_eof) {
2584 * tomoyo_domain()->domainname != NULL
2585 * because every process belongs to a domain and
2586 * the domain's name cannot be NULL.
2588 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
2589 head->read_eof = true;
2595 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
2597 * @type: Type of interface.
2598 * @file: Pointer to "struct file".
2600 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2602 * Caller acquires tomoyo_read_lock().
2604 static int tomoyo_open_control(const u8 type, struct file *file)
2606 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
2610 mutex_init(&head->io_sem);
2613 case TOMOYO_DOMAINPOLICY:
2614 /* /sys/kernel/security/tomoyo/domain_policy */
2615 head->write = tomoyo_write_domain_policy;
2616 head->read = tomoyo_read_domain_policy;
2618 case TOMOYO_EXCEPTIONPOLICY:
2619 /* /sys/kernel/security/tomoyo/exception_policy */
2620 head->write = tomoyo_write_exception_policy;
2621 head->read = tomoyo_read_exception_policy;
2623 case TOMOYO_SELFDOMAIN:
2624 /* /sys/kernel/security/tomoyo/self_domain */
2625 head->read = tomoyo_read_self_domain;
2627 case TOMOYO_DOMAIN_STATUS:
2628 /* /sys/kernel/security/tomoyo/.domain_status */
2629 head->write = tomoyo_write_domain_profile;
2630 head->read = tomoyo_read_domain_profile;
2632 case TOMOYO_PROCESS_STATUS:
2633 /* /sys/kernel/security/tomoyo/.process_status */
2634 head->write = tomoyo_write_pid;
2635 head->read = tomoyo_read_pid;
2637 case TOMOYO_VERSION:
2638 /* /sys/kernel/security/tomoyo/version */
2639 head->read = tomoyo_read_version;
2640 head->readbuf_size = 128;
2642 case TOMOYO_MEMINFO:
2643 /* /sys/kernel/security/tomoyo/meminfo */
2644 head->write = tomoyo_write_memory_quota;
2645 head->read = tomoyo_read_memory_counter;
2646 head->readbuf_size = 512;
2648 case TOMOYO_PROFILE:
2649 /* /sys/kernel/security/tomoyo/profile */
2650 head->write = tomoyo_write_profile;
2651 head->read = tomoyo_read_profile;
2653 case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
2654 head->poll = tomoyo_poll_query;
2655 head->write = tomoyo_write_answer;
2656 head->read = tomoyo_read_query;
2658 case TOMOYO_MANAGER:
2659 /* /sys/kernel/security/tomoyo/manager */
2660 head->write = tomoyo_write_manager_policy;
2661 head->read = tomoyo_read_manager_policy;
2664 if (!(file->f_mode & FMODE_READ)) {
2666 * No need to allocate read_buf since it is not opened
2671 } else if (!head->poll) {
2672 /* Don't allocate read_buf for poll() access. */
2673 if (!head->readbuf_size)
2674 head->readbuf_size = 4096 * 2;
2675 head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
2676 if (!head->read_buf) {
2681 if (!(file->f_mode & FMODE_WRITE)) {
2683 * No need to allocate write_buf since it is not opened
2687 } else if (head->write) {
2688 head->writebuf_size = 4096 * 2;
2689 head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
2690 if (!head->write_buf) {
2691 kfree(head->read_buf);
2696 if (type != TOMOYO_QUERY)
2697 head->reader_idx = tomoyo_read_lock();
2698 file->private_data = head;
2700 * Call the handler now if the file is
2701 * /sys/kernel/security/tomoyo/self_domain
2702 * so that the user can use
2703 * cat < /sys/kernel/security/tomoyo/self_domain"
2704 * to know the current process's domainname.
2706 if (type == TOMOYO_SELFDOMAIN)
2707 tomoyo_read_control(file, NULL, 0);
2709 * If the file is /sys/kernel/security/tomoyo/query , increment the
2711 * The obserber counter is used by tomoyo_supervisor() to see if
2712 * there is some process monitoring /sys/kernel/security/tomoyo/query.
2714 else if (type == TOMOYO_QUERY)
2715 atomic_inc(&tomoyo_query_observers);
2720 * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
2722 * @file: Pointer to "struct file".
2723 * @wait: Pointer to "poll_table".
2725 * Waits for read readiness.
2726 * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
2728 int tomoyo_poll_control(struct file *file, poll_table *wait)
2730 struct tomoyo_io_buffer *head = file->private_data;
2733 return head->poll(file, wait);
2737 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2739 * @file: Pointer to "struct file".
2740 * @buffer: Poiner to buffer to write to.
2741 * @buffer_len: Size of @buffer.
2743 * Returns bytes read on success, negative value otherwise.
2745 * Caller holds tomoyo_read_lock().
2747 static int tomoyo_read_control(struct file *file, char __user *buffer,
2748 const int buffer_len)
2751 struct tomoyo_io_buffer *head = file->private_data;
2756 if (mutex_lock_interruptible(&head->io_sem))
2758 /* Call the policy handler. */
2759 len = head->read(head);
2762 /* Write to buffer. */
2763 len = head->read_avail;
2764 if (len > buffer_len)
2768 /* head->read_buf changes by some functions. */
2769 cp = head->read_buf;
2770 if (copy_to_user(buffer, cp, len)) {
2774 head->read_avail -= len;
2775 memmove(cp, cp + len, head->read_avail);
2777 mutex_unlock(&head->io_sem);
2782 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2784 * @file: Pointer to "struct file".
2785 * @buffer: Pointer to buffer to read from.
2786 * @buffer_len: Size of @buffer.
2788 * Returns @buffer_len on success, negative value otherwise.
2790 * Caller holds tomoyo_read_lock().
2792 static int tomoyo_write_control(struct file *file, const char __user *buffer,
2793 const int buffer_len)
2795 struct tomoyo_io_buffer *head = file->private_data;
2796 int error = buffer_len;
2797 int avail_len = buffer_len;
2798 char *cp0 = head->write_buf;
2802 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2804 /* Don't allow updating policies by non manager programs. */
2805 if (head->write != tomoyo_write_pid &&
2806 head->write != tomoyo_write_domain_policy &&
2807 !tomoyo_is_policy_manager())
2809 if (mutex_lock_interruptible(&head->io_sem))
2811 /* Read a line and dispatch it to the policy handler. */
2812 while (avail_len > 0) {
2814 if (head->write_avail >= head->writebuf_size - 1) {
2817 } else if (get_user(c, buffer)) {
2823 cp0[head->write_avail++] = c;
2826 cp0[head->write_avail - 1] = '\0';
2827 head->write_avail = 0;
2828 tomoyo_normalize_line(cp0);
2831 mutex_unlock(&head->io_sem);
2836 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2838 * @file: Pointer to "struct file".
2840 * Releases memory and returns 0.
2842 * Caller looses tomoyo_read_lock().
2844 static int tomoyo_close_control(struct file *file)
2846 struct tomoyo_io_buffer *head = file->private_data;
2847 const bool is_write = !!head->write_buf;
2850 * If the file is /sys/kernel/security/tomoyo/query , decrement the
2853 if (head->type == TOMOYO_QUERY)
2854 atomic_dec(&tomoyo_query_observers);
2856 tomoyo_read_unlock(head->reader_idx);
2857 /* Release memory used for policy I/O. */
2858 kfree(head->read_buf);
2859 head->read_buf = NULL;
2860 kfree(head->write_buf);
2861 head->write_buf = NULL;
2864 file->private_data = NULL;
2871 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2873 * @inode: Pointer to "struct inode".
2874 * @file: Pointer to "struct file".
2876 * Returns 0 on success, negative value otherwise.
2878 static int tomoyo_open(struct inode *inode, struct file *file)
2880 const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
2882 return tomoyo_open_control(key, file);
2886 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2888 * @inode: Pointer to "struct inode".
2889 * @file: Pointer to "struct file".
2891 * Returns 0 on success, negative value otherwise.
2893 static int tomoyo_release(struct inode *inode, struct file *file)
2895 return tomoyo_close_control(file);
2899 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2901 * @file: Pointer to "struct file".
2902 * @buf: Pointer to buffer.
2903 * @count: Size of @buf.
2906 * Returns bytes read on success, negative value otherwise.
2908 static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
2911 return tomoyo_read_control(file, buf, count);
2915 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2917 * @file: Pointer to "struct file".
2918 * @buf: Pointer to buffer.
2919 * @count: Size of @buf.
2922 * Returns @count on success, negative value otherwise.
2924 static ssize_t tomoyo_write(struct file *file, const char __user *buf,
2925 size_t count, loff_t *ppos)
2927 return tomoyo_write_control(file, buf, count);
2931 * tomoyo_operations is a "struct file_operations" which is used for handling
2932 * /sys/kernel/security/tomoyo/ interface.
2934 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
2935 * See tomoyo_io_buffer for internals.
2937 static const struct file_operations tomoyo_operations = {
2938 .open = tomoyo_open,
2939 .release = tomoyo_release,
2940 .read = tomoyo_read,
2941 .write = tomoyo_write,
2945 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2947 * @name: The name of the interface file.
2948 * @mode: The permission of the interface file.
2949 * @parent: The parent directory.
2950 * @key: Type of interface.
2954 static void __init tomoyo_create_entry(const char *name, const mode_t mode,
2955 struct dentry *parent, const u8 key)
2957 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
2958 &tomoyo_operations);
2962 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2966 static int __init tomoyo_initerface_init(void)
2968 struct dentry *tomoyo_dir;
2970 /* Don't create securityfs entries unless registered. */
2971 if (current_cred()->security != &tomoyo_kernel_domain)
2974 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
2975 tomoyo_create_entry("query", 0600, tomoyo_dir,
2977 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
2978 TOMOYO_DOMAINPOLICY);
2979 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
2980 TOMOYO_EXCEPTIONPOLICY);
2981 tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
2983 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
2984 TOMOYO_DOMAIN_STATUS);
2985 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
2986 TOMOYO_PROCESS_STATUS);
2987 tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
2989 tomoyo_create_entry("profile", 0600, tomoyo_dir,
2991 tomoyo_create_entry("manager", 0600, tomoyo_dir,
2993 tomoyo_create_entry("version", 0400, tomoyo_dir,
2998 fs_initcall(tomoyo_initerface_init);