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_write_domain_policy - Write domain policy.
1290 * @head: Pointer to "struct tomoyo_io_buffer".
1292 * Returns 0 on success, negative value otherwise.
1294 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer
*head
)
1296 char *data
= head
->write_buf
;
1297 struct tomoyo_domain_info
*domain
= head
->write_var1
;
1298 bool is_delete
= false;
1299 bool is_select
= false;
1300 unsigned int profile
;
1302 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
))
1304 else if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_SELECT
))
1306 if (is_select
&& tomoyo_is_select_one(head
, data
))
1308 /* Don't allow updating policies by non manager programs. */
1309 if (!tomoyo_is_policy_manager())
1311 if (tomoyo_is_domain_def(data
)) {
1314 tomoyo_delete_domain(data
);
1315 else if (is_select
) {
1316 down_read(&tomoyo_domain_list_lock
);
1317 domain
= tomoyo_find_domain(data
);
1318 up_read(&tomoyo_domain_list_lock
);
1320 domain
= tomoyo_find_or_assign_new_domain(data
, 0);
1321 head
->write_var1
= domain
;
1327 if (sscanf(data
, TOMOYO_KEYWORD_USE_PROFILE
"%u", &profile
) == 1
1328 && profile
< TOMOYO_MAX_PROFILES
) {
1329 if (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
)
1330 domain
->profile
= (u8
) profile
;
1333 if (!strcmp(data
, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
)) {
1334 tomoyo_set_domain_flag(domain
, is_delete
,
1335 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
);
1338 return tomoyo_write_file_policy(data
, domain
, is_delete
);
1342 * tomoyo_print_single_path_acl - Print a single path ACL entry.
1344 * @head: Pointer to "struct tomoyo_io_buffer".
1345 * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
1347 * Returns true on success, false otherwise.
1349 static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer
*head
,
1350 struct tomoyo_single_path_acl_record
*
1355 const char *atmark
= "";
1356 const char *filename
;
1357 const u16 perm
= ptr
->perm
;
1359 filename
= ptr
->filename
->name
;
1360 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_SINGLE_PATH_OPERATION
;
1363 if (!(perm
& (1 << bit
)))
1365 /* Print "read/write" instead of "read" and "write". */
1366 if ((bit
== TOMOYO_TYPE_READ_ACL
||
1367 bit
== TOMOYO_TYPE_WRITE_ACL
)
1368 && (perm
& (1 << TOMOYO_TYPE_READ_WRITE_ACL
)))
1370 msg
= tomoyo_sp2keyword(bit
);
1371 pos
= head
->read_avail
;
1372 if (!tomoyo_io_printf(head
, "allow_%s %s%s\n", msg
,
1379 head
->read_bit
= bit
;
1380 head
->read_avail
= pos
;
1385 * tomoyo_print_double_path_acl - Print a double path ACL entry.
1387 * @head: Pointer to "struct tomoyo_io_buffer".
1388 * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
1390 * Returns true on success, false otherwise.
1392 static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer
*head
,
1393 struct tomoyo_double_path_acl_record
*
1397 const char *atmark1
= "";
1398 const char *atmark2
= "";
1399 const char *filename1
;
1400 const char *filename2
;
1401 const u8 perm
= ptr
->perm
;
1404 filename1
= ptr
->filename1
->name
;
1405 filename2
= ptr
->filename2
->name
;
1406 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_DOUBLE_PATH_OPERATION
;
1409 if (!(perm
& (1 << bit
)))
1411 msg
= tomoyo_dp2keyword(bit
);
1412 pos
= head
->read_avail
;
1413 if (!tomoyo_io_printf(head
, "allow_%s %s%s %s%s\n", msg
,
1414 atmark1
, filename1
, atmark2
, filename2
))
1420 head
->read_bit
= bit
;
1421 head
->read_avail
= pos
;
1426 * tomoyo_print_entry - Print an ACL entry.
1428 * @head: Pointer to "struct tomoyo_io_buffer".
1429 * @ptr: Pointer to an ACL entry.
1431 * Returns true on success, false otherwise.
1433 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
1434 struct tomoyo_acl_info
*ptr
)
1436 const u8 acl_type
= tomoyo_acl_type2(ptr
);
1438 if (acl_type
& TOMOYO_ACL_DELETED
)
1440 if (acl_type
== TOMOYO_TYPE_SINGLE_PATH_ACL
) {
1441 struct tomoyo_single_path_acl_record
*acl
1443 struct tomoyo_single_path_acl_record
,
1445 return tomoyo_print_single_path_acl(head
, acl
);
1447 if (acl_type
== TOMOYO_TYPE_DOUBLE_PATH_ACL
) {
1448 struct tomoyo_double_path_acl_record
*acl
1450 struct tomoyo_double_path_acl_record
,
1452 return tomoyo_print_double_path_acl(head
, acl
);
1454 BUG(); /* This must not happen. */
1459 * tomoyo_read_domain_policy - Read domain policy.
1461 * @head: Pointer to "struct tomoyo_io_buffer".
1465 static int tomoyo_read_domain_policy(struct tomoyo_io_buffer
*head
)
1467 struct list_head
*dpos
;
1468 struct list_head
*apos
;
1473 if (head
->read_step
== 0)
1474 head
->read_step
= 1;
1475 down_read(&tomoyo_domain_list_lock
);
1476 list_for_each_cookie(dpos
, head
->read_var1
, &tomoyo_domain_list
) {
1477 struct tomoyo_domain_info
*domain
;
1478 const char *quota_exceeded
= "";
1479 const char *transition_failed
= "";
1480 const char *ignore_global_allow_read
= "";
1481 domain
= list_entry(dpos
, struct tomoyo_domain_info
, list
);
1482 if (head
->read_step
!= 1)
1484 if (domain
->is_deleted
&& !head
->read_single_domain
)
1486 /* Print domainname and flags. */
1487 if (domain
->quota_warned
)
1488 quota_exceeded
= "quota_exceeded\n";
1489 if (domain
->flags
& TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED
)
1490 transition_failed
= "transition_failed\n";
1492 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
)
1493 ignore_global_allow_read
1494 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
"\n";
1495 done
= tomoyo_io_printf(head
, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1497 domain
->domainname
->name
,
1498 domain
->profile
, quota_exceeded
,
1500 ignore_global_allow_read
);
1503 head
->read_step
= 2;
1505 if (head
->read_step
== 3)
1507 /* Print ACL entries in the domain. */
1508 down_read(&tomoyo_domain_acl_info_list_lock
);
1509 list_for_each_cookie(apos
, head
->read_var2
,
1510 &domain
->acl_info_list
) {
1511 struct tomoyo_acl_info
*ptr
1512 = list_entry(apos
, struct tomoyo_acl_info
,
1514 done
= tomoyo_print_entry(head
, ptr
);
1518 up_read(&tomoyo_domain_acl_info_list_lock
);
1521 head
->read_step
= 3;
1523 done
= tomoyo_io_printf(head
, "\n");
1526 head
->read_step
= 1;
1527 if (head
->read_single_domain
)
1530 up_read(&tomoyo_domain_list_lock
);
1531 head
->read_eof
= done
;
1536 * tomoyo_write_domain_profile - Assign profile for specified domain.
1538 * @head: Pointer to "struct tomoyo_io_buffer".
1540 * Returns 0 on success, -EINVAL otherwise.
1542 * This is equivalent to doing
1544 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1545 * /usr/lib/ccs/loadpolicy -d
1547 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer
*head
)
1549 char *data
= head
->write_buf
;
1550 char *cp
= strchr(data
, ' ');
1551 struct tomoyo_domain_info
*domain
;
1552 unsigned long profile
;
1557 down_read(&tomoyo_domain_list_lock
);
1558 domain
= tomoyo_find_domain(cp
+ 1);
1559 up_read(&tomoyo_domain_list_lock
);
1560 if (strict_strtoul(data
, 10, &profile
))
1562 if (domain
&& profile
< TOMOYO_MAX_PROFILES
1563 && (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
))
1564 domain
->profile
= (u8
) profile
;
1569 * tomoyo_read_domain_profile - Read only domainname and profile.
1571 * @head: Pointer to "struct tomoyo_io_buffer".
1573 * Returns list of profile number and domainname pairs.
1575 * This is equivalent to doing
1577 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1578 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1579 * domainname = $0; } else if ( $1 == "use_profile" ) {
1580 * print $2 " " domainname; domainname = ""; } } ; '
1582 static int tomoyo_read_domain_profile(struct tomoyo_io_buffer
*head
)
1584 struct list_head
*pos
;
1589 down_read(&tomoyo_domain_list_lock
);
1590 list_for_each_cookie(pos
, head
->read_var1
, &tomoyo_domain_list
) {
1591 struct tomoyo_domain_info
*domain
;
1592 domain
= list_entry(pos
, struct tomoyo_domain_info
, list
);
1593 if (domain
->is_deleted
)
1595 done
= tomoyo_io_printf(head
, "%u %s\n", domain
->profile
,
1596 domain
->domainname
->name
);
1600 up_read(&tomoyo_domain_list_lock
);
1601 head
->read_eof
= done
;
1606 * tomoyo_write_pid: Specify PID to obtain domainname.
1608 * @head: Pointer to "struct tomoyo_io_buffer".
1612 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1615 /* No error check. */
1616 strict_strtoul(head
->write_buf
, 10, &pid
);
1617 head
->read_step
= (int) pid
;
1618 head
->read_eof
= false;
1623 * tomoyo_read_pid - Get domainname of the specified PID.
1625 * @head: Pointer to "struct tomoyo_io_buffer".
1627 * Returns the domainname which the specified PID is in on success,
1628 * empty string otherwise.
1629 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1630 * using read()/write() interface rather than sysctl() interface.
1632 static int tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1634 if (head
->read_avail
== 0 && !head
->read_eof
) {
1635 const int pid
= head
->read_step
;
1636 struct task_struct
*p
;
1637 struct tomoyo_domain_info
*domain
= NULL
;
1638 read_lock(&tasklist_lock
);
1639 p
= find_task_by_vpid(pid
);
1641 domain
= tomoyo_real_domain(p
);
1642 read_unlock(&tasklist_lock
);
1644 tomoyo_io_printf(head
, "%d %u %s", pid
, domain
->profile
,
1645 domain
->domainname
->name
);
1646 head
->read_eof
= true;
1652 * tomoyo_write_exception_policy - Write exception policy.
1654 * @head: Pointer to "struct tomoyo_io_buffer".
1656 * Returns 0 on success, negative value otherwise.
1658 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer
*head
)
1660 char *data
= head
->write_buf
;
1661 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1663 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_KEEP_DOMAIN
))
1664 return tomoyo_write_domain_keeper_policy(data
, false,
1666 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_KEEP_DOMAIN
))
1667 return tomoyo_write_domain_keeper_policy(data
, true, is_delete
);
1668 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_INITIALIZE_DOMAIN
))
1669 return tomoyo_write_domain_initializer_policy(data
, false,
1671 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN
))
1672 return tomoyo_write_domain_initializer_policy(data
, true,
1674 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALIAS
))
1675 return tomoyo_write_alias_policy(data
, is_delete
);
1676 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_READ
))
1677 return tomoyo_write_globally_readable_policy(data
, is_delete
);
1678 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_FILE_PATTERN
))
1679 return tomoyo_write_pattern_policy(data
, is_delete
);
1680 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DENY_REWRITE
))
1681 return tomoyo_write_no_rewrite_policy(data
, is_delete
);
1686 * tomoyo_read_exception_policy - Read exception policy.
1688 * @head: Pointer to "struct tomoyo_io_buffer".
1690 * Returns 0 on success, -EINVAL otherwise.
1692 static int tomoyo_read_exception_policy(struct tomoyo_io_buffer
*head
)
1694 if (!head
->read_eof
) {
1695 switch (head
->read_step
) {
1697 head
->read_var2
= NULL
;
1698 head
->read_step
= 1;
1700 if (!tomoyo_read_domain_keeper_policy(head
))
1702 head
->read_var2
= NULL
;
1703 head
->read_step
= 2;
1705 if (!tomoyo_read_globally_readable_policy(head
))
1707 head
->read_var2
= NULL
;
1708 head
->read_step
= 3;
1710 head
->read_var2
= NULL
;
1711 head
->read_step
= 4;
1713 if (!tomoyo_read_domain_initializer_policy(head
))
1715 head
->read_var2
= NULL
;
1716 head
->read_step
= 5;
1718 if (!tomoyo_read_alias_policy(head
))
1720 head
->read_var2
= NULL
;
1721 head
->read_step
= 6;
1723 head
->read_var2
= NULL
;
1724 head
->read_step
= 7;
1726 if (!tomoyo_read_file_pattern(head
))
1728 head
->read_var2
= NULL
;
1729 head
->read_step
= 8;
1731 if (!tomoyo_read_no_rewrite_policy(head
))
1733 head
->read_var2
= NULL
;
1734 head
->read_step
= 9;
1736 head
->read_eof
= true;
1745 /* path to policy loader */
1746 static const char *tomoyo_loader
= "/sbin/tomoyo-init";
1749 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
1751 * Returns true if /sbin/tomoyo-init exists, false otherwise.
1753 static bool tomoyo_policy_loader_exists(void)
1756 * Don't activate MAC if the policy loader doesn't exist.
1757 * If the initrd includes /sbin/init but real-root-dev has not
1758 * mounted on / yet, activating MAC will block the system since
1759 * policies are not loaded yet.
1760 * Thus, let do_execve() call this function everytime.
1764 if (kern_path(tomoyo_loader
, LOOKUP_FOLLOW
, &path
)) {
1765 printk(KERN_INFO
"Not activating Mandatory Access Control now "
1766 "since %s doesn't exist.\n", tomoyo_loader
);
1774 * tomoyo_load_policy - Run external policy loader to load policy.
1776 * @filename: The program about to start.
1778 * This function checks whether @filename is /sbin/init , and if so
1779 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
1780 * and then continues invocation of /sbin/init.
1781 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
1782 * writes to /sys/kernel/security/tomoyo/ interfaces.
1786 void tomoyo_load_policy(const char *filename
)
1791 if (tomoyo_policy_loaded
)
1794 * Check filename is /sbin/init or /sbin/tomoyo-start.
1795 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
1797 * You can create /sbin/tomoyo-start by
1798 * "ln -s /bin/true /sbin/tomoyo-start".
1800 if (strcmp(filename
, "/sbin/init") &&
1801 strcmp(filename
, "/sbin/tomoyo-start"))
1803 if (!tomoyo_policy_loader_exists())
1806 printk(KERN_INFO
"Calling %s to load policy. Please wait.\n",
1808 argv
[0] = (char *) tomoyo_loader
;
1811 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1813 call_usermodehelper(argv
[0], argv
, envp
, 1);
1815 printk(KERN_INFO
"TOMOYO: 2.2.0 2009/04/01\n");
1816 printk(KERN_INFO
"Mandatory Access Control activated.\n");
1817 tomoyo_policy_loaded
= true;
1818 { /* Check all profiles currently assigned to domains are defined. */
1819 struct tomoyo_domain_info
*domain
;
1820 down_read(&tomoyo_domain_list_lock
);
1821 list_for_each_entry(domain
, &tomoyo_domain_list
, list
) {
1822 const u8 profile
= domain
->profile
;
1823 if (tomoyo_profile_ptr
[profile
])
1825 panic("Profile %u (used by '%s') not defined.\n",
1826 profile
, domain
->domainname
->name
);
1828 up_read(&tomoyo_domain_list_lock
);
1833 * tomoyo_read_version: Get version.
1835 * @head: Pointer to "struct tomoyo_io_buffer".
1837 * Returns version information.
1839 static int tomoyo_read_version(struct tomoyo_io_buffer
*head
)
1841 if (!head
->read_eof
) {
1842 tomoyo_io_printf(head
, "2.2.0");
1843 head
->read_eof
= true;
1849 * tomoyo_read_self_domain - Get the current process's domainname.
1851 * @head: Pointer to "struct tomoyo_io_buffer".
1853 * Returns the current process's domainname.
1855 static int tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
1857 if (!head
->read_eof
) {
1859 * tomoyo_domain()->domainname != NULL
1860 * because every process belongs to a domain and
1861 * the domain's name cannot be NULL.
1863 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
1864 head
->read_eof
= true;
1870 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1872 * @type: Type of interface.
1873 * @file: Pointer to "struct file".
1875 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1877 static int tomoyo_open_control(const u8 type
, struct file
*file
)
1879 struct tomoyo_io_buffer
*head
= tomoyo_alloc(sizeof(*head
));
1883 mutex_init(&head
->io_sem
);
1885 case TOMOYO_DOMAINPOLICY
:
1886 /* /sys/kernel/security/tomoyo/domain_policy */
1887 head
->write
= tomoyo_write_domain_policy
;
1888 head
->read
= tomoyo_read_domain_policy
;
1890 case TOMOYO_EXCEPTIONPOLICY
:
1891 /* /sys/kernel/security/tomoyo/exception_policy */
1892 head
->write
= tomoyo_write_exception_policy
;
1893 head
->read
= tomoyo_read_exception_policy
;
1895 case TOMOYO_SELFDOMAIN
:
1896 /* /sys/kernel/security/tomoyo/self_domain */
1897 head
->read
= tomoyo_read_self_domain
;
1899 case TOMOYO_DOMAIN_STATUS
:
1900 /* /sys/kernel/security/tomoyo/.domain_status */
1901 head
->write
= tomoyo_write_domain_profile
;
1902 head
->read
= tomoyo_read_domain_profile
;
1904 case TOMOYO_PROCESS_STATUS
:
1905 /* /sys/kernel/security/tomoyo/.process_status */
1906 head
->write
= tomoyo_write_pid
;
1907 head
->read
= tomoyo_read_pid
;
1909 case TOMOYO_VERSION
:
1910 /* /sys/kernel/security/tomoyo/version */
1911 head
->read
= tomoyo_read_version
;
1912 head
->readbuf_size
= 128;
1914 case TOMOYO_MEMINFO
:
1915 /* /sys/kernel/security/tomoyo/meminfo */
1916 head
->write
= tomoyo_write_memory_quota
;
1917 head
->read
= tomoyo_read_memory_counter
;
1918 head
->readbuf_size
= 512;
1920 case TOMOYO_PROFILE
:
1921 /* /sys/kernel/security/tomoyo/profile */
1922 head
->write
= tomoyo_write_profile
;
1923 head
->read
= tomoyo_read_profile
;
1925 case TOMOYO_MANAGER
:
1926 /* /sys/kernel/security/tomoyo/manager */
1927 head
->write
= tomoyo_write_manager_policy
;
1928 head
->read
= tomoyo_read_manager_policy
;
1931 if (!(file
->f_mode
& FMODE_READ
)) {
1933 * No need to allocate read_buf since it is not opened
1938 if (!head
->readbuf_size
)
1939 head
->readbuf_size
= 4096 * 2;
1940 head
->read_buf
= tomoyo_alloc(head
->readbuf_size
);
1941 if (!head
->read_buf
) {
1946 if (!(file
->f_mode
& FMODE_WRITE
)) {
1948 * No need to allocate write_buf since it is not opened
1952 } else if (head
->write
) {
1953 head
->writebuf_size
= 4096 * 2;
1954 head
->write_buf
= tomoyo_alloc(head
->writebuf_size
);
1955 if (!head
->write_buf
) {
1956 tomoyo_free(head
->read_buf
);
1961 file
->private_data
= head
;
1963 * Call the handler now if the file is
1964 * /sys/kernel/security/tomoyo/self_domain
1965 * so that the user can use
1966 * cat < /sys/kernel/security/tomoyo/self_domain"
1967 * to know the current process's domainname.
1969 if (type
== TOMOYO_SELFDOMAIN
)
1970 tomoyo_read_control(file
, NULL
, 0);
1975 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1977 * @file: Pointer to "struct file".
1978 * @buffer: Poiner to buffer to write to.
1979 * @buffer_len: Size of @buffer.
1981 * Returns bytes read on success, negative value otherwise.
1983 static int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
1984 const int buffer_len
)
1987 struct tomoyo_io_buffer
*head
= file
->private_data
;
1992 if (mutex_lock_interruptible(&head
->io_sem
))
1994 /* Call the policy handler. */
1995 len
= head
->read(head
);
1998 /* Write to buffer. */
1999 len
= head
->read_avail
;
2000 if (len
> buffer_len
)
2004 /* head->read_buf changes by some functions. */
2005 cp
= head
->read_buf
;
2006 if (copy_to_user(buffer
, cp
, len
)) {
2010 head
->read_avail
-= len
;
2011 memmove(cp
, cp
+ len
, head
->read_avail
);
2013 mutex_unlock(&head
->io_sem
);
2018 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2020 * @file: Pointer to "struct file".
2021 * @buffer: Pointer to buffer to read from.
2022 * @buffer_len: Size of @buffer.
2024 * Returns @buffer_len on success, negative value otherwise.
2026 static int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
2027 const int buffer_len
)
2029 struct tomoyo_io_buffer
*head
= file
->private_data
;
2030 int error
= buffer_len
;
2031 int avail_len
= buffer_len
;
2032 char *cp0
= head
->write_buf
;
2036 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
2038 /* Don't allow updating policies by non manager programs. */
2039 if (head
->write
!= tomoyo_write_pid
&&
2040 head
->write
!= tomoyo_write_domain_policy
&&
2041 !tomoyo_is_policy_manager())
2043 if (mutex_lock_interruptible(&head
->io_sem
))
2045 /* Read a line and dispatch it to the policy handler. */
2046 while (avail_len
> 0) {
2048 if (head
->write_avail
>= head
->writebuf_size
- 1) {
2051 } else if (get_user(c
, buffer
)) {
2057 cp0
[head
->write_avail
++] = c
;
2060 cp0
[head
->write_avail
- 1] = '\0';
2061 head
->write_avail
= 0;
2062 tomoyo_normalize_line(cp0
);
2065 mutex_unlock(&head
->io_sem
);
2070 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2072 * @file: Pointer to "struct file".
2074 * Releases memory and returns 0.
2076 static int tomoyo_close_control(struct file
*file
)
2078 struct tomoyo_io_buffer
*head
= file
->private_data
;
2080 /* Release memory used for policy I/O. */
2081 tomoyo_free(head
->read_buf
);
2082 head
->read_buf
= NULL
;
2083 tomoyo_free(head
->write_buf
);
2084 head
->write_buf
= NULL
;
2087 file
->private_data
= NULL
;
2092 * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
2094 * @acl_type: Type of ACL entry.
2096 * Returns pointer to the ACL entry on success, NULL otherwise.
2098 void *tomoyo_alloc_acl_element(const u8 acl_type
)
2101 struct tomoyo_acl_info
*ptr
;
2104 case TOMOYO_TYPE_SINGLE_PATH_ACL
:
2105 len
= sizeof(struct tomoyo_single_path_acl_record
);
2107 case TOMOYO_TYPE_DOUBLE_PATH_ACL
:
2108 len
= sizeof(struct tomoyo_double_path_acl_record
);
2113 ptr
= tomoyo_alloc_element(len
);
2116 ptr
->type
= acl_type
;
2121 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2123 * @inode: Pointer to "struct inode".
2124 * @file: Pointer to "struct file".
2126 * Returns 0 on success, negative value otherwise.
2128 static int tomoyo_open(struct inode
*inode
, struct file
*file
)
2130 const int key
= ((u8
*) file
->f_path
.dentry
->d_inode
->i_private
)
2132 return tomoyo_open_control(key
, file
);
2136 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2138 * @inode: Pointer to "struct inode".
2139 * @file: Pointer to "struct file".
2141 * Returns 0 on success, negative value otherwise.
2143 static int tomoyo_release(struct inode
*inode
, struct file
*file
)
2145 return tomoyo_close_control(file
);
2149 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2151 * @file: Pointer to "struct file".
2152 * @buf: Pointer to buffer.
2153 * @count: Size of @buf.
2156 * Returns bytes read on success, negative value otherwise.
2158 static ssize_t
tomoyo_read(struct file
*file
, char __user
*buf
, size_t count
,
2161 return tomoyo_read_control(file
, buf
, count
);
2165 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2167 * @file: Pointer to "struct file".
2168 * @buf: Pointer to buffer.
2169 * @count: Size of @buf.
2172 * Returns @count on success, negative value otherwise.
2174 static ssize_t
tomoyo_write(struct file
*file
, const char __user
*buf
,
2175 size_t count
, loff_t
*ppos
)
2177 return tomoyo_write_control(file
, buf
, count
);
2181 * tomoyo_operations is a "struct file_operations" which is used for handling
2182 * /sys/kernel/security/tomoyo/ interface.
2184 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
2185 * See tomoyo_io_buffer for internals.
2187 static const struct file_operations tomoyo_operations
= {
2188 .open
= tomoyo_open
,
2189 .release
= tomoyo_release
,
2190 .read
= tomoyo_read
,
2191 .write
= tomoyo_write
,
2195 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2197 * @name: The name of the interface file.
2198 * @mode: The permission of the interface file.
2199 * @parent: The parent directory.
2200 * @key: Type of interface.
2204 static void __init
tomoyo_create_entry(const char *name
, const mode_t mode
,
2205 struct dentry
*parent
, const u8 key
)
2207 securityfs_create_file(name
, mode
, parent
, ((u8
*) NULL
) + key
,
2208 &tomoyo_operations
);
2212 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2216 static int __init
tomoyo_initerface_init(void)
2218 struct dentry
*tomoyo_dir
;
2220 /* Don't create securityfs entries unless registered. */
2221 if (current_cred()->security
!= &tomoyo_kernel_domain
)
2224 tomoyo_dir
= securityfs_create_dir("tomoyo", NULL
);
2225 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir
,
2226 TOMOYO_DOMAINPOLICY
);
2227 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir
,
2228 TOMOYO_EXCEPTIONPOLICY
);
2229 tomoyo_create_entry("self_domain", 0400, tomoyo_dir
,
2231 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir
,
2232 TOMOYO_DOMAIN_STATUS
);
2233 tomoyo_create_entry(".process_status", 0600, tomoyo_dir
,
2234 TOMOYO_PROCESS_STATUS
);
2235 tomoyo_create_entry("meminfo", 0600, tomoyo_dir
,
2237 tomoyo_create_entry("profile", 0600, tomoyo_dir
,
2239 tomoyo_create_entry("manager", 0600, tomoyo_dir
,
2241 tomoyo_create_entry("version", 0400, tomoyo_dir
,
2246 fs_initcall(tomoyo_initerface_init
);