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/security.h>
14 #include <linux/hardirq.h>
19 /* Has loading policy done? */
20 bool tomoyo_policy_loaded
;
22 /* String table for functionality that takes 4 modes. */
23 static const char *tomoyo_mode_4
[4] = {
24 "disabled", "learning", "permissive", "enforcing"
26 /* String table for functionality that takes 2 modes. */
27 static const char *tomoyo_mode_2
[4] = {
28 "disabled", "enabled", "enabled", "enabled"
32 * tomoyo_control_array is a static data which contains
34 * (1) functionality name used by /sys/kernel/security/tomoyo/profile .
35 * (2) initial values for "struct tomoyo_profile".
36 * (3) max values for "struct tomoyo_profile".
40 unsigned int current_value
;
41 const unsigned int max_value
;
42 } tomoyo_control_array
[TOMOYO_MAX_CONTROL_INDEX
] = {
43 [TOMOYO_MAC_FOR_FILE
] = { "MAC_FOR_FILE", 0, 3 },
44 [TOMOYO_MAX_ACCEPT_ENTRY
] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX
},
45 [TOMOYO_VERBOSE
] = { "TOMOYO_VERBOSE", 1, 1 },
49 * tomoyo_profile is a structure which is used for holding the mode of access
50 * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
51 * An administrator can define up to 256 profiles.
52 * The ->profile of "struct tomoyo_domain_info" is used for remembering
53 * the profile's number (0 - 255) assigned to that domain.
55 static struct tomoyo_profile
{
56 unsigned int value
[TOMOYO_MAX_CONTROL_INDEX
];
57 const struct tomoyo_path_info
*comment
;
58 } *tomoyo_profile_ptr
[TOMOYO_MAX_PROFILES
];
60 /* Permit policy management by non-root user? */
61 static bool tomoyo_manage_by_non_root
;
63 /* Utility functions. */
65 /* Open operation for /sys/kernel/security/tomoyo/ interface. */
66 static int tomoyo_open_control(const u8 type
, struct file
*file
);
67 /* Close /sys/kernel/security/tomoyo/ interface. */
68 static int tomoyo_close_control(struct file
*file
);
69 /* Read operation for /sys/kernel/security/tomoyo/ interface. */
70 static int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
71 const int buffer_len
);
72 /* Write operation for /sys/kernel/security/tomoyo/ interface. */
73 static int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
74 const int buffer_len
);
77 * tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
79 * @str: Pointer to the string.
81 * Returns true if @str is a \ooo style octal value, false otherwise.
83 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
84 * This function verifies that \ooo is in valid range.
86 static inline bool tomoyo_is_byte_range(const char *str
)
88 return *str
>= '0' && *str
++ <= '3' &&
89 *str
>= '0' && *str
++ <= '7' &&
90 *str
>= '0' && *str
<= '7';
94 * tomoyo_is_alphabet_char - Check whether the character is an alphabet.
96 * @c: The character to check.
98 * Returns true if @c is an alphabet character, false otherwise.
100 static inline bool tomoyo_is_alphabet_char(const char c
)
102 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
106 * tomoyo_make_byte - Make byte value from three octal characters.
108 * @c1: The first character.
109 * @c2: The second character.
110 * @c3: The third character.
112 * Returns byte value.
114 static inline u8
tomoyo_make_byte(const u8 c1
, const u8 c2
, const u8 c3
)
116 return ((c1
- '0') << 6) + ((c2
- '0') << 3) + (c3
- '0');
120 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
122 * @src: Pointer to pointer to the string.
123 * @find: Pointer to the keyword.
125 * Returns true if @src starts with @find, false otherwise.
127 * The @src is updated to point the first character after the @find
128 * if @src starts with @find.
130 static bool tomoyo_str_starts(char **src
, const char *find
)
132 const int len
= strlen(find
);
135 if (strncmp(tmp
, find
, len
))
143 * tomoyo_normalize_line - Format string.
145 * @buffer: The line to normalize.
147 * Leading and trailing whitespaces are removed.
148 * Multiple whitespaces are packed into single space.
152 static void tomoyo_normalize_line(unsigned char *buffer
)
154 unsigned char *sp
= buffer
;
155 unsigned char *dp
= buffer
;
158 while (tomoyo_is_invalid(*sp
))
164 while (tomoyo_is_valid(*sp
))
166 while (tomoyo_is_invalid(*sp
))
173 * tomoyo_is_correct_path - Validate a pathname.
174 * @filename: The pathname to check.
175 * @start_type: Should the pathname start with '/'?
176 * 1 = must / -1 = must not / 0 = don't care
177 * @pattern_type: Can the pathname contain a wildcard?
178 * 1 = must / -1 = must not / 0 = don't care
179 * @end_type: Should the pathname end with '/'?
180 * 1 = must / -1 = must not / 0 = don't care
181 * @function: The name of function calling me.
183 * Check whether the given filename follows the naming rules.
184 * Returns true if @filename follows the naming rules, false otherwise.
186 bool tomoyo_is_correct_path(const char *filename
, const s8 start_type
,
187 const s8 pattern_type
, const s8 end_type
,
188 const char *function
)
190 bool contains_pattern
= false;
194 const char *original_filename
= filename
;
199 if (start_type
== 1) { /* Must start with '/' */
202 } else if (start_type
== -1) { /* Must not start with '/' */
207 c
= *(filename
+ strlen(filename
) - 1);
208 if (end_type
== 1) { /* Must end with '/' */
211 } else if (end_type
== -1) { /* Must not end with '/' */
215 while ((c
= *filename
++) != '\0') {
217 switch ((c
= *filename
++)) {
218 case '\\': /* "\\" */
230 if (pattern_type
== -1)
231 break; /* Must not contain pattern */
232 contains_pattern
= true;
234 case '0': /* "\ooo" */
239 if (d
< '0' || d
> '7')
242 if (e
< '0' || e
> '7')
244 c
= tomoyo_make_byte(c
, d
, e
);
245 if (tomoyo_is_invalid(c
))
246 continue; /* pattern is not \000 */
249 } else if (tomoyo_is_invalid(c
)) {
253 if (pattern_type
== 1) { /* Must contain pattern */
254 if (!contains_pattern
)
259 printk(KERN_DEBUG
"%s: Invalid pathname '%s'\n", function
,
265 * tomoyo_is_correct_domain - Check whether the given domainname follows the naming rules.
266 * @domainname: The domainname to check.
267 * @function: The name of function calling me.
269 * Returns true if @domainname follows the naming rules, false otherwise.
271 bool tomoyo_is_correct_domain(const unsigned char *domainname
,
272 const char *function
)
277 const char *org_domainname
= domainname
;
279 if (!domainname
|| strncmp(domainname
, TOMOYO_ROOT_NAME
,
280 TOMOYO_ROOT_NAME_LEN
))
282 domainname
+= TOMOYO_ROOT_NAME_LEN
;
286 if (*domainname
++ != ' ')
288 if (*domainname
++ != '/')
290 while ((c
= *domainname
) != '\0' && c
!= ' ') {
295 case '\\': /* "\\" */
297 case '0': /* "\ooo" */
302 if (d
< '0' || d
> '7')
305 if (e
< '0' || e
> '7')
307 c
= tomoyo_make_byte(c
, d
, e
);
308 if (tomoyo_is_invalid(c
))
309 /* pattern is not \000 */
313 } else if (tomoyo_is_invalid(c
)) {
317 } while (*domainname
);
320 printk(KERN_DEBUG
"%s: Invalid domainname '%s'\n", function
,
326 * tomoyo_is_domain_def - Check whether the given token can be a domainname.
328 * @buffer: The token to check.
330 * Returns true if @buffer possibly be a domainname, false otherwise.
332 bool tomoyo_is_domain_def(const unsigned char *buffer
)
334 return !strncmp(buffer
, TOMOYO_ROOT_NAME
, TOMOYO_ROOT_NAME_LEN
);
338 * tomoyo_find_domain - Find a domain by the given name.
340 * @domainname: The domainname to find.
342 * Caller must call down_read(&tomoyo_domain_list_lock); or
343 * down_write(&tomoyo_domain_list_lock); .
345 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
347 struct tomoyo_domain_info
*tomoyo_find_domain(const char *domainname
)
349 struct tomoyo_domain_info
*domain
;
350 struct tomoyo_path_info name
;
352 name
.name
= domainname
;
353 tomoyo_fill_path_info(&name
);
354 list_for_each_entry(domain
, &tomoyo_domain_list
, list
) {
355 if (!domain
->is_deleted
&&
356 !tomoyo_pathcmp(&name
, domain
->domainname
))
363 * tomoyo_path_depth - Evaluate the number of '/' in a string.
365 * @pathname: The string to evaluate.
367 * Returns path depth of the string.
369 * I score 2 for each of the '/' in the @pathname
370 * and score 1 if the @pathname ends with '/'.
372 static int tomoyo_path_depth(const char *pathname
)
377 const char *ep
= pathname
+ strlen(pathname
);
378 if (pathname
< ep
--) {
381 while (pathname
<= ep
)
390 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
392 * @filename: The string to evaluate.
394 * Returns the initial length without a pattern in @filename.
396 static int tomoyo_const_part_length(const char *filename
)
403 while ((c
= *filename
++) != '\0') {
410 case '\\': /* "\\" */
413 case '0': /* "\ooo" */
418 if (c
< '0' || c
> '7')
421 if (c
< '0' || c
> '7')
432 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
434 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
436 * The caller sets "struct tomoyo_path_info"->name.
438 void tomoyo_fill_path_info(struct tomoyo_path_info
*ptr
)
440 const char *name
= ptr
->name
;
441 const int len
= strlen(name
);
443 ptr
->const_len
= tomoyo_const_part_length(name
);
444 ptr
->is_dir
= len
&& (name
[len
- 1] == '/');
445 ptr
->is_patterned
= (ptr
->const_len
< len
);
446 ptr
->hash
= full_name_hash(name
, len
);
447 ptr
->depth
= tomoyo_path_depth(name
);
451 * tomoyo_file_matches_to_pattern2 - Pattern matching without '/' character
454 * @filename: The start of string to check.
455 * @filename_end: The end of string to check.
456 * @pattern: The start of pattern to compare.
457 * @pattern_end: The end of pattern to compare.
459 * Returns true if @filename matches @pattern, false otherwise.
461 static bool tomoyo_file_matches_to_pattern2(const char *filename
,
462 const char *filename_end
,
464 const char *pattern_end
)
466 while (filename
< filename_end
&& pattern
< pattern_end
) {
468 if (*pattern
!= '\\') {
469 if (*filename
++ != *pattern
++)
481 } else if (c
== '\\') {
482 if (filename
[1] == '\\')
484 else if (tomoyo_is_byte_range(filename
+ 1))
493 if (*++filename
!= '\\')
505 if (!tomoyo_is_alphabet_char(c
))
512 if (c
== '\\' && tomoyo_is_byte_range(filename
+ 1)
513 && strncmp(filename
+ 1, pattern
, 3) == 0) {
518 return false; /* Not matched. */
521 for (i
= 0; i
<= filename_end
- filename
; i
++) {
522 if (tomoyo_file_matches_to_pattern2(
523 filename
+ i
, filename_end
,
524 pattern
+ 1, pattern_end
))
527 if (c
== '.' && *pattern
== '@')
531 if (filename
[i
+ 1] == '\\')
533 else if (tomoyo_is_byte_range(filename
+ i
+ 1))
536 break; /* Bad pattern. */
538 return false; /* Not matched. */
543 while (isdigit(filename
[j
]))
545 } else if (c
== 'X') {
546 while (isxdigit(filename
[j
]))
548 } else if (c
== 'A') {
549 while (tomoyo_is_alphabet_char(filename
[j
]))
552 for (i
= 1; i
<= j
; i
++) {
553 if (tomoyo_file_matches_to_pattern2(
554 filename
+ i
, filename_end
,
555 pattern
+ 1, pattern_end
))
558 return false; /* Not matched or bad pattern. */
563 while (*pattern
== '\\' &&
564 (*(pattern
+ 1) == '*' || *(pattern
+ 1) == '@'))
566 return filename
== filename_end
&& pattern
== pattern_end
;
570 * tomoyo_file_matches_to_pattern - Pattern matching without without '/' character.
572 * @filename: The start of string to check.
573 * @filename_end: The end of string to check.
574 * @pattern: The start of pattern to compare.
575 * @pattern_end: The end of pattern to compare.
577 * Returns true if @filename matches @pattern, false otherwise.
579 static bool tomoyo_file_matches_to_pattern(const char *filename
,
580 const char *filename_end
,
582 const char *pattern_end
)
584 const char *pattern_start
= pattern
;
588 while (pattern
< pattern_end
- 1) {
589 /* Split at "\-" pattern. */
590 if (*pattern
++ != '\\' || *pattern
++ != '-')
592 result
= tomoyo_file_matches_to_pattern2(filename
,
601 pattern_start
= pattern
;
603 result
= tomoyo_file_matches_to_pattern2(filename
, filename_end
,
604 pattern_start
, pattern_end
);
605 return first
? result
: !result
;
609 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
610 * @filename: The filename to check.
611 * @pattern: The pattern to compare.
613 * Returns true if matches, false otherwise.
615 * The following patterns are available.
617 * \ooo Octal representation of a byte.
618 * \* More than or equals to 0 character other than '/'.
619 * \@ More than or equals to 0 character other than '/' or '.'.
620 * \? 1 byte character other than '/'.
621 * \$ More than or equals to 1 decimal digit.
622 * \+ 1 decimal digit.
623 * \X More than or equals to 1 hexadecimal digit.
624 * \x 1 hexadecimal digit.
625 * \A More than or equals to 1 alphabet character.
626 * \a 1 alphabet character.
627 * \- Subtraction operator.
629 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info
*filename
,
630 const struct tomoyo_path_info
*pattern
)
633 if (!filename || !pattern)
636 const char *f
= filename
->name
;
637 const char *p
= pattern
->name
;
638 const int len
= pattern
->const_len
;
640 /* If @pattern doesn't contain pattern, I can use strcmp(). */
641 if (!pattern
->is_patterned
)
642 return !tomoyo_pathcmp(filename
, pattern
);
643 /* Dont compare if the number of '/' differs. */
644 if (filename
->depth
!= pattern
->depth
)
646 /* Compare the initial length without patterns. */
647 if (strncmp(f
, p
, len
))
651 /* Main loop. Compare each directory component. */
653 const char *f_delimiter
= strchr(f
, '/');
654 const char *p_delimiter
= strchr(p
, '/');
656 f_delimiter
= f
+ strlen(f
);
658 p_delimiter
= p
+ strlen(p
);
659 if (!tomoyo_file_matches_to_pattern(f
, f_delimiter
,
669 /* Ignore trailing "\*" and "\@" in @pattern. */
671 (*(p
+ 1) == '*' || *(p
+ 1) == '@'))
677 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
679 * @head: Pointer to "struct tomoyo_io_buffer".
680 * @fmt: The printf()'s format string, followed by parameters.
682 * Returns true if output was written, false otherwise.
684 * The snprintf() will truncate, but tomoyo_io_printf() won't.
686 bool tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
690 int pos
= head
->read_avail
;
691 int size
= head
->readbuf_size
- pos
;
696 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
);
698 if (pos
+ len
>= head
->readbuf_size
)
700 head
->read_avail
+= len
;
705 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
707 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
709 * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
710 * if this function didn't return NULL.
712 static const char *tomoyo_get_exe(void)
714 struct mm_struct
*mm
= current
->mm
;
715 struct vm_area_struct
*vma
;
716 const char *cp
= NULL
;
720 down_read(&mm
->mmap_sem
);
721 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
722 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
) {
723 cp
= tomoyo_realpath_from_path(&vma
->vm_file
->f_path
);
727 up_read(&mm
->mmap_sem
);
732 * tomoyo_get_msg - Get warning message.
734 * @is_enforce: Is it enforcing mode?
736 * Returns "ERROR" or "WARNING".
738 const char *tomoyo_get_msg(const bool is_enforce
)
747 * tomoyo_check_flags - Check mode for specified functionality.
749 * @domain: Pointer to "struct tomoyo_domain_info".
750 * @index: The functionality to check mode.
752 * TOMOYO checks only process context.
753 * This code disables TOMOYO's enforcement in case the function is called from
756 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info
*domain
,
759 const u8 profile
= domain
->profile
;
761 if (WARN_ON(in_interrupt()))
763 return tomoyo_policy_loaded
&& index
< TOMOYO_MAX_CONTROL_INDEX
764 #if TOMOYO_MAX_PROFILES != 256
765 && profile
< TOMOYO_MAX_PROFILES
767 && tomoyo_profile_ptr
[profile
] ?
768 tomoyo_profile_ptr
[profile
]->value
[index
] : 0;
772 * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
774 * @domain: Pointer to "struct tomoyo_domain_info".
776 * Returns true if domain policy violation warning should be printed to
779 bool tomoyo_verbose_mode(const struct tomoyo_domain_info
*domain
)
781 return tomoyo_check_flags(domain
, TOMOYO_VERBOSE
) != 0;
785 * tomoyo_domain_quota_is_ok - Check for domain's quota.
787 * @domain: Pointer to "struct tomoyo_domain_info".
789 * Returns true if the domain is not exceeded quota, false otherwise.
791 bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info
* const domain
)
793 unsigned int count
= 0;
794 struct tomoyo_acl_info
*ptr
;
798 down_read(&tomoyo_domain_acl_info_list_lock
);
799 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
800 if (ptr
->type
& TOMOYO_ACL_DELETED
)
802 switch (tomoyo_acl_type2(ptr
)) {
803 struct tomoyo_single_path_acl_record
*acl1
;
804 struct tomoyo_double_path_acl_record
*acl2
;
806 case TOMOYO_TYPE_SINGLE_PATH_ACL
:
807 acl1
= container_of(ptr
,
808 struct tomoyo_single_path_acl_record
,
811 if (perm
& (1 << TOMOYO_TYPE_EXECUTE_ACL
))
814 ((1 << TOMOYO_TYPE_READ_ACL
) |
815 (1 << TOMOYO_TYPE_WRITE_ACL
)))
817 if (perm
& (1 << TOMOYO_TYPE_CREATE_ACL
))
819 if (perm
& (1 << TOMOYO_TYPE_UNLINK_ACL
))
821 if (perm
& (1 << TOMOYO_TYPE_MKDIR_ACL
))
823 if (perm
& (1 << TOMOYO_TYPE_RMDIR_ACL
))
825 if (perm
& (1 << TOMOYO_TYPE_MKFIFO_ACL
))
827 if (perm
& (1 << TOMOYO_TYPE_MKSOCK_ACL
))
829 if (perm
& (1 << TOMOYO_TYPE_MKBLOCK_ACL
))
831 if (perm
& (1 << TOMOYO_TYPE_MKCHAR_ACL
))
833 if (perm
& (1 << TOMOYO_TYPE_TRUNCATE_ACL
))
835 if (perm
& (1 << TOMOYO_TYPE_SYMLINK_ACL
))
837 if (perm
& (1 << TOMOYO_TYPE_REWRITE_ACL
))
840 case TOMOYO_TYPE_DOUBLE_PATH_ACL
:
841 acl2
= container_of(ptr
,
842 struct tomoyo_double_path_acl_record
,
845 if (perm
& (1 << TOMOYO_TYPE_LINK_ACL
))
847 if (perm
& (1 << TOMOYO_TYPE_RENAME_ACL
))
852 up_read(&tomoyo_domain_acl_info_list_lock
);
853 if (count
< tomoyo_check_flags(domain
, TOMOYO_MAX_ACCEPT_ENTRY
))
855 if (!domain
->quota_warned
) {
856 domain
->quota_warned
= true;
857 printk(KERN_WARNING
"TOMOYO-WARNING: "
858 "Domain '%s' has so many ACLs to hold. "
859 "Stopped learning mode.\n", domain
->domainname
->name
);
865 * tomoyo_find_or_assign_new_profile - Create a new profile.
867 * @profile: Profile number to create.
869 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
871 static struct tomoyo_profile
*tomoyo_find_or_assign_new_profile(const unsigned
874 static DEFINE_MUTEX(lock
);
875 struct tomoyo_profile
*ptr
= NULL
;
878 if (profile
>= TOMOYO_MAX_PROFILES
)
881 ptr
= tomoyo_profile_ptr
[profile
];
884 ptr
= tomoyo_alloc_element(sizeof(*ptr
));
887 for (i
= 0; i
< TOMOYO_MAX_CONTROL_INDEX
; i
++)
888 ptr
->value
[i
] = tomoyo_control_array
[i
].current_value
;
889 mb(); /* Avoid out-of-order execution. */
890 tomoyo_profile_ptr
[profile
] = ptr
;
897 * tomoyo_write_profile - Write to profile table.
899 * @head: Pointer to "struct tomoyo_io_buffer".
901 * Returns 0 on success, negative value otherwise.
903 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
905 char *data
= head
->write_buf
;
909 struct tomoyo_profile
*profile
;
912 cp
= strchr(data
, '-');
915 if (strict_strtoul(data
, 10, &num
))
919 profile
= tomoyo_find_or_assign_new_profile(num
);
922 cp
= strchr(data
, '=');
926 if (!strcmp(data
, "COMMENT")) {
927 profile
->comment
= tomoyo_save_name(cp
+ 1);
930 for (i
= 0; i
< TOMOYO_MAX_CONTROL_INDEX
; i
++) {
931 if (strcmp(data
, tomoyo_control_array
[i
].keyword
))
933 if (sscanf(cp
+ 1, "%u", &value
) != 1) {
938 modes
= tomoyo_mode_2
;
941 modes
= tomoyo_mode_4
;
944 for (j
= 0; j
< 4; j
++) {
945 if (strcmp(cp
+ 1, modes
[j
]))
952 } else if (value
> tomoyo_control_array
[i
].max_value
) {
953 value
= tomoyo_control_array
[i
].max_value
;
955 profile
->value
[i
] = value
;
962 * tomoyo_read_profile - Read from profile table.
964 * @head: Pointer to "struct tomoyo_io_buffer".
968 static int tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
970 static const int total
= TOMOYO_MAX_CONTROL_INDEX
+ 1;
975 for (step
= head
->read_step
; step
< TOMOYO_MAX_PROFILES
* total
;
977 const u8 index
= step
/ total
;
978 u8 type
= step
% total
;
979 const struct tomoyo_profile
*profile
980 = tomoyo_profile_ptr
[index
];
981 head
->read_step
= step
;
984 if (!type
) { /* Print profile' comment tag. */
985 if (!tomoyo_io_printf(head
, "%u-COMMENT=%s\n",
986 index
, profile
->comment
?
987 profile
->comment
->name
: ""))
992 if (type
< TOMOYO_MAX_CONTROL_INDEX
) {
993 const unsigned int value
= profile
->value
[type
];
994 const char **modes
= NULL
;
996 = tomoyo_control_array
[type
].keyword
;
997 switch (tomoyo_control_array
[type
].max_value
) {
999 modes
= tomoyo_mode_4
;
1002 modes
= tomoyo_mode_2
;
1006 if (!tomoyo_io_printf(head
, "%u-%s=%s\n", index
,
1007 keyword
, modes
[value
]))
1010 if (!tomoyo_io_printf(head
, "%u-%s=%u\n", index
,
1016 if (step
== TOMOYO_MAX_PROFILES
* total
)
1017 head
->read_eof
= true;
1022 * tomoyo_policy_manager_entry is a structure which is used for holding list of
1023 * domainnames or programs which are permitted to modify configuration via
1024 * /sys/kernel/security/tomoyo/ interface.
1025 * It has following fields.
1027 * (1) "list" which is linked to tomoyo_policy_manager_list .
1028 * (2) "manager" is a domainname or a program's pathname.
1029 * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
1031 * (4) "is_deleted" is a bool which is true if marked as deleted, false
1034 struct tomoyo_policy_manager_entry
{
1035 struct list_head list
;
1036 /* A path to program or a domainname. */
1037 const struct tomoyo_path_info
*manager
;
1038 bool is_domain
; /* True if manager is a domainname. */
1039 bool is_deleted
; /* True if this entry is deleted. */
1043 * tomoyo_policy_manager_list is used for holding list of domainnames or
1044 * programs which are permitted to modify configuration via
1045 * /sys/kernel/security/tomoyo/ interface.
1047 * An entry is added by
1049 * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
1050 * /sys/kernel/security/tomoyo/manager
1051 * (if you want to specify by a domainname)
1055 * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
1056 * (if you want to specify by a program's location)
1060 * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
1061 * /sys/kernel/security/tomoyo/manager
1065 * # echo 'delete /usr/lib/ccs/editpolicy' > \
1066 * /sys/kernel/security/tomoyo/manager
1068 * and all entries are retrieved by
1070 * # cat /sys/kernel/security/tomoyo/manager
1072 static LIST_HEAD(tomoyo_policy_manager_list
);
1073 static DECLARE_RWSEM(tomoyo_policy_manager_list_lock
);
1076 * tomoyo_update_manager_entry - Add a manager entry.
1078 * @manager: The path to manager or the domainnamme.
1079 * @is_delete: True if it is a delete request.
1081 * Returns 0 on success, negative value otherwise.
1083 static int tomoyo_update_manager_entry(const char *manager
,
1084 const bool is_delete
)
1086 struct tomoyo_policy_manager_entry
*new_entry
;
1087 struct tomoyo_policy_manager_entry
*ptr
;
1088 const struct tomoyo_path_info
*saved_manager
;
1089 int error
= -ENOMEM
;
1090 bool is_domain
= false;
1092 if (tomoyo_is_domain_def(manager
)) {
1093 if (!tomoyo_is_correct_domain(manager
, __func__
))
1097 if (!tomoyo_is_correct_path(manager
, 1, -1, -1, __func__
))
1100 saved_manager
= tomoyo_save_name(manager
);
1103 down_write(&tomoyo_policy_manager_list_lock
);
1104 list_for_each_entry(ptr
, &tomoyo_policy_manager_list
, list
) {
1105 if (ptr
->manager
!= saved_manager
)
1107 ptr
->is_deleted
= is_delete
;
1115 new_entry
= tomoyo_alloc_element(sizeof(*new_entry
));
1118 new_entry
->manager
= saved_manager
;
1119 new_entry
->is_domain
= is_domain
;
1120 list_add_tail(&new_entry
->list
, &tomoyo_policy_manager_list
);
1123 up_write(&tomoyo_policy_manager_list_lock
);
1128 * tomoyo_write_manager_policy - Write manager policy.
1130 * @head: Pointer to "struct tomoyo_io_buffer".
1132 * Returns 0 on success, negative value otherwise.
1134 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer
*head
)
1136 char *data
= head
->write_buf
;
1137 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1139 if (!strcmp(data
, "manage_by_non_root")) {
1140 tomoyo_manage_by_non_root
= !is_delete
;
1143 return tomoyo_update_manager_entry(data
, is_delete
);
1147 * tomoyo_read_manager_policy - Read manager policy.
1149 * @head: Pointer to "struct tomoyo_io_buffer".
1153 static int tomoyo_read_manager_policy(struct tomoyo_io_buffer
*head
)
1155 struct list_head
*pos
;
1160 down_read(&tomoyo_policy_manager_list_lock
);
1161 list_for_each_cookie(pos
, head
->read_var2
,
1162 &tomoyo_policy_manager_list
) {
1163 struct tomoyo_policy_manager_entry
*ptr
;
1164 ptr
= list_entry(pos
, struct tomoyo_policy_manager_entry
,
1166 if (ptr
->is_deleted
)
1168 done
= tomoyo_io_printf(head
, "%s\n", ptr
->manager
->name
);
1172 up_read(&tomoyo_policy_manager_list_lock
);
1173 head
->read_eof
= done
;
1178 * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
1180 * Returns true if the current process is permitted to modify policy
1181 * via /sys/kernel/security/tomoyo/ interface.
1183 static bool tomoyo_is_policy_manager(void)
1185 struct tomoyo_policy_manager_entry
*ptr
;
1187 const struct task_struct
*task
= current
;
1188 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
1191 if (!tomoyo_policy_loaded
)
1193 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
1195 down_read(&tomoyo_policy_manager_list_lock
);
1196 list_for_each_entry(ptr
, &tomoyo_policy_manager_list
, list
) {
1197 if (!ptr
->is_deleted
&& ptr
->is_domain
1198 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
1203 up_read(&tomoyo_policy_manager_list_lock
);
1206 exe
= tomoyo_get_exe();
1209 down_read(&tomoyo_policy_manager_list_lock
);
1210 list_for_each_entry(ptr
, &tomoyo_policy_manager_list
, list
) {
1211 if (!ptr
->is_deleted
&& !ptr
->is_domain
1212 && !strcmp(exe
, ptr
->manager
->name
)) {
1217 up_read(&tomoyo_policy_manager_list_lock
);
1218 if (!found
) { /* Reduce error messages. */
1219 static pid_t last_pid
;
1220 const pid_t pid
= current
->pid
;
1221 if (last_pid
!= pid
) {
1222 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
1223 "update policies.\n", domainname
->name
, exe
);
1232 * tomoyo_is_select_one - Parse select command.
1234 * @head: Pointer to "struct tomoyo_io_buffer".
1235 * @data: String to parse.
1237 * Returns true on success, false otherwise.
1239 static bool tomoyo_is_select_one(struct tomoyo_io_buffer
*head
,
1243 struct tomoyo_domain_info
*domain
= NULL
;
1245 if (sscanf(data
, "pid=%u", &pid
) == 1) {
1246 struct task_struct
*p
;
1247 read_lock(&tasklist_lock
);
1248 p
= find_task_by_vpid(pid
);
1250 domain
= tomoyo_real_domain(p
);
1251 read_unlock(&tasklist_lock
);
1252 } else if (!strncmp(data
, "domain=", 7)) {
1253 if (tomoyo_is_domain_def(data
+ 7)) {
1254 down_read(&tomoyo_domain_list_lock
);
1255 domain
= tomoyo_find_domain(data
+ 7);
1256 up_read(&tomoyo_domain_list_lock
);
1260 head
->write_var1
= domain
;
1261 /* Accessing read_buf is safe because head->io_sem is held. */
1262 if (!head
->read_buf
)
1263 return true; /* Do nothing if open(O_WRONLY). */
1264 head
->read_avail
= 0;
1265 tomoyo_io_printf(head
, "# select %s\n", data
);
1266 head
->read_single_domain
= true;
1267 head
->read_eof
= !domain
;
1269 struct tomoyo_domain_info
*d
;
1270 head
->read_var1
= NULL
;
1271 down_read(&tomoyo_domain_list_lock
);
1272 list_for_each_entry(d
, &tomoyo_domain_list
, list
) {
1275 head
->read_var1
= &d
->list
;
1277 up_read(&tomoyo_domain_list_lock
);
1278 head
->read_var2
= NULL
;
1280 head
->read_step
= 0;
1281 if (domain
->is_deleted
)
1282 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
1288 * tomoyo_delete_domain - Delete a domain.
1290 * @domainname: The name of domain.
1294 static int tomoyo_delete_domain(char *domainname
)
1296 struct tomoyo_domain_info
*domain
;
1297 struct tomoyo_path_info name
;
1299 name
.name
= domainname
;
1300 tomoyo_fill_path_info(&name
);
1301 down_write(&tomoyo_domain_list_lock
);
1302 /* Is there an active domain? */
1303 list_for_each_entry(domain
, &tomoyo_domain_list
, list
) {
1304 /* Never delete tomoyo_kernel_domain */
1305 if (domain
== &tomoyo_kernel_domain
)
1307 if (domain
->is_deleted
||
1308 tomoyo_pathcmp(domain
->domainname
, &name
))
1310 domain
->is_deleted
= true;
1313 up_write(&tomoyo_domain_list_lock
);
1318 * tomoyo_write_domain_policy - Write domain policy.
1320 * @head: Pointer to "struct tomoyo_io_buffer".
1322 * Returns 0 on success, negative value otherwise.
1324 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer
*head
)
1326 char *data
= head
->write_buf
;
1327 struct tomoyo_domain_info
*domain
= head
->write_var1
;
1328 bool is_delete
= false;
1329 bool is_select
= false;
1330 unsigned int profile
;
1332 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
))
1334 else if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_SELECT
))
1336 if (is_select
&& tomoyo_is_select_one(head
, data
))
1338 /* Don't allow updating policies by non manager programs. */
1339 if (!tomoyo_is_policy_manager())
1341 if (tomoyo_is_domain_def(data
)) {
1344 tomoyo_delete_domain(data
);
1345 else if (is_select
) {
1346 down_read(&tomoyo_domain_list_lock
);
1347 domain
= tomoyo_find_domain(data
);
1348 up_read(&tomoyo_domain_list_lock
);
1350 domain
= tomoyo_find_or_assign_new_domain(data
, 0);
1351 head
->write_var1
= domain
;
1357 if (sscanf(data
, TOMOYO_KEYWORD_USE_PROFILE
"%u", &profile
) == 1
1358 && profile
< TOMOYO_MAX_PROFILES
) {
1359 if (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
)
1360 domain
->profile
= (u8
) profile
;
1363 if (!strcmp(data
, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
)) {
1364 tomoyo_set_domain_flag(domain
, is_delete
,
1365 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
);
1368 return tomoyo_write_file_policy(data
, domain
, is_delete
);
1372 * tomoyo_print_single_path_acl - Print a single path ACL entry.
1374 * @head: Pointer to "struct tomoyo_io_buffer".
1375 * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
1377 * Returns true on success, false otherwise.
1379 static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer
*head
,
1380 struct tomoyo_single_path_acl_record
*
1385 const char *atmark
= "";
1386 const char *filename
;
1387 const u16 perm
= ptr
->perm
;
1389 filename
= ptr
->filename
->name
;
1390 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_SINGLE_PATH_OPERATION
;
1393 if (!(perm
& (1 << bit
)))
1395 /* Print "read/write" instead of "read" and "write". */
1396 if ((bit
== TOMOYO_TYPE_READ_ACL
||
1397 bit
== TOMOYO_TYPE_WRITE_ACL
)
1398 && (perm
& (1 << TOMOYO_TYPE_READ_WRITE_ACL
)))
1400 msg
= tomoyo_sp2keyword(bit
);
1401 pos
= head
->read_avail
;
1402 if (!tomoyo_io_printf(head
, "allow_%s %s%s\n", msg
,
1409 head
->read_bit
= bit
;
1410 head
->read_avail
= pos
;
1415 * tomoyo_print_double_path_acl - Print a double path ACL entry.
1417 * @head: Pointer to "struct tomoyo_io_buffer".
1418 * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
1420 * Returns true on success, false otherwise.
1422 static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer
*head
,
1423 struct tomoyo_double_path_acl_record
*
1427 const char *atmark1
= "";
1428 const char *atmark2
= "";
1429 const char *filename1
;
1430 const char *filename2
;
1431 const u8 perm
= ptr
->perm
;
1434 filename1
= ptr
->filename1
->name
;
1435 filename2
= ptr
->filename2
->name
;
1436 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_DOUBLE_PATH_OPERATION
;
1439 if (!(perm
& (1 << bit
)))
1441 msg
= tomoyo_dp2keyword(bit
);
1442 pos
= head
->read_avail
;
1443 if (!tomoyo_io_printf(head
, "allow_%s %s%s %s%s\n", msg
,
1444 atmark1
, filename1
, atmark2
, filename2
))
1450 head
->read_bit
= bit
;
1451 head
->read_avail
= pos
;
1456 * tomoyo_print_entry - Print an ACL entry.
1458 * @head: Pointer to "struct tomoyo_io_buffer".
1459 * @ptr: Pointer to an ACL entry.
1461 * Returns true on success, false otherwise.
1463 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
1464 struct tomoyo_acl_info
*ptr
)
1466 const u8 acl_type
= tomoyo_acl_type2(ptr
);
1468 if (acl_type
& TOMOYO_ACL_DELETED
)
1470 if (acl_type
== TOMOYO_TYPE_SINGLE_PATH_ACL
) {
1471 struct tomoyo_single_path_acl_record
*acl
1473 struct tomoyo_single_path_acl_record
,
1475 return tomoyo_print_single_path_acl(head
, acl
);
1477 if (acl_type
== TOMOYO_TYPE_DOUBLE_PATH_ACL
) {
1478 struct tomoyo_double_path_acl_record
*acl
1480 struct tomoyo_double_path_acl_record
,
1482 return tomoyo_print_double_path_acl(head
, acl
);
1484 BUG(); /* This must not happen. */
1489 * tomoyo_read_domain_policy - Read domain policy.
1491 * @head: Pointer to "struct tomoyo_io_buffer".
1495 static int tomoyo_read_domain_policy(struct tomoyo_io_buffer
*head
)
1497 struct list_head
*dpos
;
1498 struct list_head
*apos
;
1503 if (head
->read_step
== 0)
1504 head
->read_step
= 1;
1505 down_read(&tomoyo_domain_list_lock
);
1506 list_for_each_cookie(dpos
, head
->read_var1
, &tomoyo_domain_list
) {
1507 struct tomoyo_domain_info
*domain
;
1508 const char *quota_exceeded
= "";
1509 const char *transition_failed
= "";
1510 const char *ignore_global_allow_read
= "";
1511 domain
= list_entry(dpos
, struct tomoyo_domain_info
, list
);
1512 if (head
->read_step
!= 1)
1514 if (domain
->is_deleted
&& !head
->read_single_domain
)
1516 /* Print domainname and flags. */
1517 if (domain
->quota_warned
)
1518 quota_exceeded
= "quota_exceeded\n";
1519 if (domain
->flags
& TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED
)
1520 transition_failed
= "transition_failed\n";
1522 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
)
1523 ignore_global_allow_read
1524 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
"\n";
1525 done
= tomoyo_io_printf(head
, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1527 domain
->domainname
->name
,
1528 domain
->profile
, quota_exceeded
,
1530 ignore_global_allow_read
);
1533 head
->read_step
= 2;
1535 if (head
->read_step
== 3)
1537 /* Print ACL entries in the domain. */
1538 down_read(&tomoyo_domain_acl_info_list_lock
);
1539 list_for_each_cookie(apos
, head
->read_var2
,
1540 &domain
->acl_info_list
) {
1541 struct tomoyo_acl_info
*ptr
1542 = list_entry(apos
, struct tomoyo_acl_info
,
1544 done
= tomoyo_print_entry(head
, ptr
);
1548 up_read(&tomoyo_domain_acl_info_list_lock
);
1551 head
->read_step
= 3;
1553 done
= tomoyo_io_printf(head
, "\n");
1556 head
->read_step
= 1;
1557 if (head
->read_single_domain
)
1560 up_read(&tomoyo_domain_list_lock
);
1561 head
->read_eof
= done
;
1566 * tomoyo_write_domain_profile - Assign profile for specified domain.
1568 * @head: Pointer to "struct tomoyo_io_buffer".
1570 * Returns 0 on success, -EINVAL otherwise.
1572 * This is equivalent to doing
1574 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1575 * /usr/lib/ccs/loadpolicy -d
1577 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer
*head
)
1579 char *data
= head
->write_buf
;
1580 char *cp
= strchr(data
, ' ');
1581 struct tomoyo_domain_info
*domain
;
1582 unsigned long profile
;
1587 down_read(&tomoyo_domain_list_lock
);
1588 domain
= tomoyo_find_domain(cp
+ 1);
1589 up_read(&tomoyo_domain_list_lock
);
1590 if (strict_strtoul(data
, 10, &profile
))
1592 if (domain
&& profile
< TOMOYO_MAX_PROFILES
1593 && (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
))
1594 domain
->profile
= (u8
) profile
;
1599 * tomoyo_read_domain_profile - Read only domainname and profile.
1601 * @head: Pointer to "struct tomoyo_io_buffer".
1603 * Returns list of profile number and domainname pairs.
1605 * This is equivalent to doing
1607 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1608 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1609 * domainname = $0; } else if ( $1 == "use_profile" ) {
1610 * print $2 " " domainname; domainname = ""; } } ; '
1612 static int tomoyo_read_domain_profile(struct tomoyo_io_buffer
*head
)
1614 struct list_head
*pos
;
1619 down_read(&tomoyo_domain_list_lock
);
1620 list_for_each_cookie(pos
, head
->read_var1
, &tomoyo_domain_list
) {
1621 struct tomoyo_domain_info
*domain
;
1622 domain
= list_entry(pos
, struct tomoyo_domain_info
, list
);
1623 if (domain
->is_deleted
)
1625 done
= tomoyo_io_printf(head
, "%u %s\n", domain
->profile
,
1626 domain
->domainname
->name
);
1630 up_read(&tomoyo_domain_list_lock
);
1631 head
->read_eof
= done
;
1636 * tomoyo_write_pid: Specify PID to obtain domainname.
1638 * @head: Pointer to "struct tomoyo_io_buffer".
1642 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1645 /* No error check. */
1646 strict_strtoul(head
->write_buf
, 10, &pid
);
1647 head
->read_step
= (int) pid
;
1648 head
->read_eof
= false;
1653 * tomoyo_read_pid - Get domainname of the specified PID.
1655 * @head: Pointer to "struct tomoyo_io_buffer".
1657 * Returns the domainname which the specified PID is in on success,
1658 * empty string otherwise.
1659 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1660 * using read()/write() interface rather than sysctl() interface.
1662 static int tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1664 if (head
->read_avail
== 0 && !head
->read_eof
) {
1665 const int pid
= head
->read_step
;
1666 struct task_struct
*p
;
1667 struct tomoyo_domain_info
*domain
= NULL
;
1668 read_lock(&tasklist_lock
);
1669 p
= find_task_by_vpid(pid
);
1671 domain
= tomoyo_real_domain(p
);
1672 read_unlock(&tasklist_lock
);
1674 tomoyo_io_printf(head
, "%d %u %s", pid
, domain
->profile
,
1675 domain
->domainname
->name
);
1676 head
->read_eof
= true;
1682 * tomoyo_write_exception_policy - Write exception policy.
1684 * @head: Pointer to "struct tomoyo_io_buffer".
1686 * Returns 0 on success, negative value otherwise.
1688 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer
*head
)
1690 char *data
= head
->write_buf
;
1691 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1693 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_KEEP_DOMAIN
))
1694 return tomoyo_write_domain_keeper_policy(data
, false,
1696 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_KEEP_DOMAIN
))
1697 return tomoyo_write_domain_keeper_policy(data
, true, is_delete
);
1698 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_INITIALIZE_DOMAIN
))
1699 return tomoyo_write_domain_initializer_policy(data
, false,
1701 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN
))
1702 return tomoyo_write_domain_initializer_policy(data
, true,
1704 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALIAS
))
1705 return tomoyo_write_alias_policy(data
, is_delete
);
1706 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_READ
))
1707 return tomoyo_write_globally_readable_policy(data
, is_delete
);
1708 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_FILE_PATTERN
))
1709 return tomoyo_write_pattern_policy(data
, is_delete
);
1710 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DENY_REWRITE
))
1711 return tomoyo_write_no_rewrite_policy(data
, is_delete
);
1716 * tomoyo_read_exception_policy - Read exception policy.
1718 * @head: Pointer to "struct tomoyo_io_buffer".
1720 * Returns 0 on success, -EINVAL otherwise.
1722 static int tomoyo_read_exception_policy(struct tomoyo_io_buffer
*head
)
1724 if (!head
->read_eof
) {
1725 switch (head
->read_step
) {
1727 head
->read_var2
= NULL
;
1728 head
->read_step
= 1;
1730 if (!tomoyo_read_domain_keeper_policy(head
))
1732 head
->read_var2
= NULL
;
1733 head
->read_step
= 2;
1735 if (!tomoyo_read_globally_readable_policy(head
))
1737 head
->read_var2
= NULL
;
1738 head
->read_step
= 3;
1740 head
->read_var2
= NULL
;
1741 head
->read_step
= 4;
1743 if (!tomoyo_read_domain_initializer_policy(head
))
1745 head
->read_var2
= NULL
;
1746 head
->read_step
= 5;
1748 if (!tomoyo_read_alias_policy(head
))
1750 head
->read_var2
= NULL
;
1751 head
->read_step
= 6;
1753 head
->read_var2
= NULL
;
1754 head
->read_step
= 7;
1756 if (!tomoyo_read_file_pattern(head
))
1758 head
->read_var2
= NULL
;
1759 head
->read_step
= 8;
1761 if (!tomoyo_read_no_rewrite_policy(head
))
1763 head
->read_var2
= NULL
;
1764 head
->read_step
= 9;
1766 head
->read_eof
= true;
1775 /* path to policy loader */
1776 static const char *tomoyo_loader
= "/sbin/tomoyo-init";
1779 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
1781 * Returns true if /sbin/tomoyo-init exists, false otherwise.
1783 static bool tomoyo_policy_loader_exists(void)
1786 * Don't activate MAC if the policy loader doesn't exist.
1787 * If the initrd includes /sbin/init but real-root-dev has not
1788 * mounted on / yet, activating MAC will block the system since
1789 * policies are not loaded yet.
1790 * Thus, let do_execve() call this function everytime.
1794 if (kern_path(tomoyo_loader
, LOOKUP_FOLLOW
, &path
)) {
1795 printk(KERN_INFO
"Not activating Mandatory Access Control now "
1796 "since %s doesn't exist.\n", tomoyo_loader
);
1804 * tomoyo_load_policy - Run external policy loader to load policy.
1806 * @filename: The program about to start.
1808 * This function checks whether @filename is /sbin/init , and if so
1809 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
1810 * and then continues invocation of /sbin/init.
1811 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
1812 * writes to /sys/kernel/security/tomoyo/ interfaces.
1816 void tomoyo_load_policy(const char *filename
)
1821 if (tomoyo_policy_loaded
)
1824 * Check filename is /sbin/init or /sbin/tomoyo-start.
1825 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
1827 * You can create /sbin/tomoyo-start by
1828 * "ln -s /bin/true /sbin/tomoyo-start".
1830 if (strcmp(filename
, "/sbin/init") &&
1831 strcmp(filename
, "/sbin/tomoyo-start"))
1833 if (!tomoyo_policy_loader_exists())
1836 printk(KERN_INFO
"Calling %s to load policy. Please wait.\n",
1838 argv
[0] = (char *) tomoyo_loader
;
1841 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1843 call_usermodehelper(argv
[0], argv
, envp
, 1);
1845 printk(KERN_INFO
"TOMOYO: 2.2.0 2009/04/01\n");
1846 printk(KERN_INFO
"Mandatory Access Control activated.\n");
1847 tomoyo_policy_loaded
= true;
1848 { /* Check all profiles currently assigned to domains are defined. */
1849 struct tomoyo_domain_info
*domain
;
1850 down_read(&tomoyo_domain_list_lock
);
1851 list_for_each_entry(domain
, &tomoyo_domain_list
, list
) {
1852 const u8 profile
= domain
->profile
;
1853 if (tomoyo_profile_ptr
[profile
])
1855 panic("Profile %u (used by '%s') not defined.\n",
1856 profile
, domain
->domainname
->name
);
1858 up_read(&tomoyo_domain_list_lock
);
1863 * tomoyo_read_version: Get version.
1865 * @head: Pointer to "struct tomoyo_io_buffer".
1867 * Returns version information.
1869 static int tomoyo_read_version(struct tomoyo_io_buffer
*head
)
1871 if (!head
->read_eof
) {
1872 tomoyo_io_printf(head
, "2.2.0");
1873 head
->read_eof
= true;
1879 * tomoyo_read_self_domain - Get the current process's domainname.
1881 * @head: Pointer to "struct tomoyo_io_buffer".
1883 * Returns the current process's domainname.
1885 static int tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
1887 if (!head
->read_eof
) {
1889 * tomoyo_domain()->domainname != NULL
1890 * because every process belongs to a domain and
1891 * the domain's name cannot be NULL.
1893 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
1894 head
->read_eof
= true;
1900 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1902 * @type: Type of interface.
1903 * @file: Pointer to "struct file".
1905 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1907 static int tomoyo_open_control(const u8 type
, struct file
*file
)
1909 struct tomoyo_io_buffer
*head
= tomoyo_alloc(sizeof(*head
));
1913 mutex_init(&head
->io_sem
);
1915 case TOMOYO_DOMAINPOLICY
:
1916 /* /sys/kernel/security/tomoyo/domain_policy */
1917 head
->write
= tomoyo_write_domain_policy
;
1918 head
->read
= tomoyo_read_domain_policy
;
1920 case TOMOYO_EXCEPTIONPOLICY
:
1921 /* /sys/kernel/security/tomoyo/exception_policy */
1922 head
->write
= tomoyo_write_exception_policy
;
1923 head
->read
= tomoyo_read_exception_policy
;
1925 case TOMOYO_SELFDOMAIN
:
1926 /* /sys/kernel/security/tomoyo/self_domain */
1927 head
->read
= tomoyo_read_self_domain
;
1929 case TOMOYO_DOMAIN_STATUS
:
1930 /* /sys/kernel/security/tomoyo/.domain_status */
1931 head
->write
= tomoyo_write_domain_profile
;
1932 head
->read
= tomoyo_read_domain_profile
;
1934 case TOMOYO_PROCESS_STATUS
:
1935 /* /sys/kernel/security/tomoyo/.process_status */
1936 head
->write
= tomoyo_write_pid
;
1937 head
->read
= tomoyo_read_pid
;
1939 case TOMOYO_VERSION
:
1940 /* /sys/kernel/security/tomoyo/version */
1941 head
->read
= tomoyo_read_version
;
1942 head
->readbuf_size
= 128;
1944 case TOMOYO_MEMINFO
:
1945 /* /sys/kernel/security/tomoyo/meminfo */
1946 head
->write
= tomoyo_write_memory_quota
;
1947 head
->read
= tomoyo_read_memory_counter
;
1948 head
->readbuf_size
= 512;
1950 case TOMOYO_PROFILE
:
1951 /* /sys/kernel/security/tomoyo/profile */
1952 head
->write
= tomoyo_write_profile
;
1953 head
->read
= tomoyo_read_profile
;
1955 case TOMOYO_MANAGER
:
1956 /* /sys/kernel/security/tomoyo/manager */
1957 head
->write
= tomoyo_write_manager_policy
;
1958 head
->read
= tomoyo_read_manager_policy
;
1961 if (!(file
->f_mode
& FMODE_READ
)) {
1963 * No need to allocate read_buf since it is not opened
1968 if (!head
->readbuf_size
)
1969 head
->readbuf_size
= 4096 * 2;
1970 head
->read_buf
= tomoyo_alloc(head
->readbuf_size
);
1971 if (!head
->read_buf
) {
1976 if (!(file
->f_mode
& FMODE_WRITE
)) {
1978 * No need to allocate write_buf since it is not opened
1982 } else if (head
->write
) {
1983 head
->writebuf_size
= 4096 * 2;
1984 head
->write_buf
= tomoyo_alloc(head
->writebuf_size
);
1985 if (!head
->write_buf
) {
1986 tomoyo_free(head
->read_buf
);
1991 file
->private_data
= head
;
1993 * Call the handler now if the file is
1994 * /sys/kernel/security/tomoyo/self_domain
1995 * so that the user can use
1996 * cat < /sys/kernel/security/tomoyo/self_domain"
1997 * to know the current process's domainname.
1999 if (type
== TOMOYO_SELFDOMAIN
)
2000 tomoyo_read_control(file
, NULL
, 0);
2005 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2007 * @file: Pointer to "struct file".
2008 * @buffer: Poiner to buffer to write to.
2009 * @buffer_len: Size of @buffer.
2011 * Returns bytes read on success, negative value otherwise.
2013 static int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
2014 const int buffer_len
)
2017 struct tomoyo_io_buffer
*head
= file
->private_data
;
2022 if (mutex_lock_interruptible(&head
->io_sem
))
2024 /* Call the policy handler. */
2025 len
= head
->read(head
);
2028 /* Write to buffer. */
2029 len
= head
->read_avail
;
2030 if (len
> buffer_len
)
2034 /* head->read_buf changes by some functions. */
2035 cp
= head
->read_buf
;
2036 if (copy_to_user(buffer
, cp
, len
)) {
2040 head
->read_avail
-= len
;
2041 memmove(cp
, cp
+ len
, head
->read_avail
);
2043 mutex_unlock(&head
->io_sem
);
2048 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2050 * @file: Pointer to "struct file".
2051 * @buffer: Pointer to buffer to read from.
2052 * @buffer_len: Size of @buffer.
2054 * Returns @buffer_len on success, negative value otherwise.
2056 static int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
2057 const int buffer_len
)
2059 struct tomoyo_io_buffer
*head
= file
->private_data
;
2060 int error
= buffer_len
;
2061 int avail_len
= buffer_len
;
2062 char *cp0
= head
->write_buf
;
2066 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
2068 /* Don't allow updating policies by non manager programs. */
2069 if (head
->write
!= tomoyo_write_pid
&&
2070 head
->write
!= tomoyo_write_domain_policy
&&
2071 !tomoyo_is_policy_manager())
2073 if (mutex_lock_interruptible(&head
->io_sem
))
2075 /* Read a line and dispatch it to the policy handler. */
2076 while (avail_len
> 0) {
2078 if (head
->write_avail
>= head
->writebuf_size
- 1) {
2081 } else if (get_user(c
, buffer
)) {
2087 cp0
[head
->write_avail
++] = c
;
2090 cp0
[head
->write_avail
- 1] = '\0';
2091 head
->write_avail
= 0;
2092 tomoyo_normalize_line(cp0
);
2095 mutex_unlock(&head
->io_sem
);
2100 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2102 * @file: Pointer to "struct file".
2104 * Releases memory and returns 0.
2106 static int tomoyo_close_control(struct file
*file
)
2108 struct tomoyo_io_buffer
*head
= file
->private_data
;
2110 /* Release memory used for policy I/O. */
2111 tomoyo_free(head
->read_buf
);
2112 head
->read_buf
= NULL
;
2113 tomoyo_free(head
->write_buf
);
2114 head
->write_buf
= NULL
;
2117 file
->private_data
= NULL
;
2122 * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
2124 * @acl_type: Type of ACL entry.
2126 * Returns pointer to the ACL entry on success, NULL otherwise.
2128 void *tomoyo_alloc_acl_element(const u8 acl_type
)
2131 struct tomoyo_acl_info
*ptr
;
2134 case TOMOYO_TYPE_SINGLE_PATH_ACL
:
2135 len
= sizeof(struct tomoyo_single_path_acl_record
);
2137 case TOMOYO_TYPE_DOUBLE_PATH_ACL
:
2138 len
= sizeof(struct tomoyo_double_path_acl_record
);
2143 ptr
= tomoyo_alloc_element(len
);
2146 ptr
->type
= acl_type
;
2151 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2153 * @inode: Pointer to "struct inode".
2154 * @file: Pointer to "struct file".
2156 * Returns 0 on success, negative value otherwise.
2158 static int tomoyo_open(struct inode
*inode
, struct file
*file
)
2160 const int key
= ((u8
*) file
->f_path
.dentry
->d_inode
->i_private
)
2162 return tomoyo_open_control(key
, file
);
2166 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2168 * @inode: Pointer to "struct inode".
2169 * @file: Pointer to "struct file".
2171 * Returns 0 on success, negative value otherwise.
2173 static int tomoyo_release(struct inode
*inode
, struct file
*file
)
2175 return tomoyo_close_control(file
);
2179 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2181 * @file: Pointer to "struct file".
2182 * @buf: Pointer to buffer.
2183 * @count: Size of @buf.
2186 * Returns bytes read on success, negative value otherwise.
2188 static ssize_t
tomoyo_read(struct file
*file
, char __user
*buf
, size_t count
,
2191 return tomoyo_read_control(file
, buf
, count
);
2195 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2197 * @file: Pointer to "struct file".
2198 * @buf: Pointer to buffer.
2199 * @count: Size of @buf.
2202 * Returns @count on success, negative value otherwise.
2204 static ssize_t
tomoyo_write(struct file
*file
, const char __user
*buf
,
2205 size_t count
, loff_t
*ppos
)
2207 return tomoyo_write_control(file
, buf
, count
);
2211 * tomoyo_operations is a "struct file_operations" which is used for handling
2212 * /sys/kernel/security/tomoyo/ interface.
2214 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
2215 * See tomoyo_io_buffer for internals.
2217 static const struct file_operations tomoyo_operations
= {
2218 .open
= tomoyo_open
,
2219 .release
= tomoyo_release
,
2220 .read
= tomoyo_read
,
2221 .write
= tomoyo_write
,
2225 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2227 * @name: The name of the interface file.
2228 * @mode: The permission of the interface file.
2229 * @parent: The parent directory.
2230 * @key: Type of interface.
2234 static void __init
tomoyo_create_entry(const char *name
, const mode_t mode
,
2235 struct dentry
*parent
, const u8 key
)
2237 securityfs_create_file(name
, mode
, parent
, ((u8
*) NULL
) + key
,
2238 &tomoyo_operations
);
2242 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2246 static int __init
tomoyo_initerface_init(void)
2248 struct dentry
*tomoyo_dir
;
2250 /* Don't create securityfs entries unless registered. */
2251 if (current_cred()->security
!= &tomoyo_kernel_domain
)
2254 tomoyo_dir
= securityfs_create_dir("tomoyo", NULL
);
2255 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir
,
2256 TOMOYO_DOMAINPOLICY
);
2257 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir
,
2258 TOMOYO_EXCEPTIONPOLICY
);
2259 tomoyo_create_entry("self_domain", 0400, tomoyo_dir
,
2261 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir
,
2262 TOMOYO_DOMAIN_STATUS
);
2263 tomoyo_create_entry(".process_status", 0600, tomoyo_dir
,
2264 TOMOYO_PROCESS_STATUS
);
2265 tomoyo_create_entry("meminfo", 0600, tomoyo_dir
,
2267 tomoyo_create_entry("profile", 0600, tomoyo_dir
,
2269 tomoyo_create_entry("manager", 0600, tomoyo_dir
,
2271 tomoyo_create_entry("version", 0400, tomoyo_dir
,
2276 fs_initcall(tomoyo_initerface_init
);