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"
31 /* Table for profile. */
34 unsigned int current_value
;
35 const unsigned int max_value
;
36 } tomoyo_control_array
[TOMOYO_MAX_CONTROL_INDEX
] = {
37 [TOMOYO_MAC_FOR_FILE
] = { "MAC_FOR_FILE", 0, 3 },
38 [TOMOYO_MAX_ACCEPT_ENTRY
] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX
},
39 [TOMOYO_VERBOSE
] = { "TOMOYO_VERBOSE", 1, 1 },
42 /* Profile table. Memory is allocated as needed. */
43 static struct tomoyo_profile
{
44 unsigned int value
[TOMOYO_MAX_CONTROL_INDEX
];
45 const struct tomoyo_path_info
*comment
;
46 } *tomoyo_profile_ptr
[TOMOYO_MAX_PROFILES
];
48 /* Permit policy management by non-root user? */
49 static bool tomoyo_manage_by_non_root
;
51 /* Utility functions. */
53 /* Open operation for /sys/kernel/security/tomoyo/ interface. */
54 static int tomoyo_open_control(const u8 type
, struct file
*file
);
55 /* Close /sys/kernel/security/tomoyo/ interface. */
56 static int tomoyo_close_control(struct file
*file
);
57 /* Read operation for /sys/kernel/security/tomoyo/ interface. */
58 static int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
59 const int buffer_len
);
60 /* Write operation for /sys/kernel/security/tomoyo/ interface. */
61 static int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
62 const int buffer_len
);
65 * tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
67 * @str: Pointer to the string.
69 * Returns true if @str is a \ooo style octal value, false otherwise.
71 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
72 * This function verifies that \ooo is in valid range.
74 static inline bool tomoyo_is_byte_range(const char *str
)
76 return *str
>= '0' && *str
++ <= '3' &&
77 *str
>= '0' && *str
++ <= '7' &&
78 *str
>= '0' && *str
<= '7';
82 * tomoyo_is_alphabet_char - Check whether the character is an alphabet.
84 * @c: The character to check.
86 * Returns true if @c is an alphabet character, false otherwise.
88 static inline bool tomoyo_is_alphabet_char(const char c
)
90 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
94 * tomoyo_make_byte - Make byte value from three octal characters.
96 * @c1: The first character.
97 * @c2: The second character.
98 * @c3: The third character.
100 * Returns byte value.
102 static inline u8
tomoyo_make_byte(const u8 c1
, const u8 c2
, const u8 c3
)
104 return ((c1
- '0') << 6) + ((c2
- '0') << 3) + (c3
- '0');
108 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
110 * @src: Pointer to pointer to the string.
111 * @find: Pointer to the keyword.
113 * Returns true if @src starts with @find, false otherwise.
115 * The @src is updated to point the first character after the @find
116 * if @src starts with @find.
118 static bool tomoyo_str_starts(char **src
, const char *find
)
120 const int len
= strlen(find
);
123 if (strncmp(tmp
, find
, len
))
131 * tomoyo_normalize_line - Format string.
133 * @buffer: The line to normalize.
135 * Leading and trailing whitespaces are removed.
136 * Multiple whitespaces are packed into single space.
140 static void tomoyo_normalize_line(unsigned char *buffer
)
142 unsigned char *sp
= buffer
;
143 unsigned char *dp
= buffer
;
146 while (tomoyo_is_invalid(*sp
))
152 while (tomoyo_is_valid(*sp
))
154 while (tomoyo_is_invalid(*sp
))
161 * tomoyo_is_correct_path - Validate a pathname.
162 * @filename: The pathname to check.
163 * @start_type: Should the pathname start with '/'?
164 * 1 = must / -1 = must not / 0 = don't care
165 * @pattern_type: Can the pathname contain a wildcard?
166 * 1 = must / -1 = must not / 0 = don't care
167 * @end_type: Should the pathname end with '/'?
168 * 1 = must / -1 = must not / 0 = don't care
169 * @function: The name of function calling me.
171 * Check whether the given filename follows the naming rules.
172 * Returns true if @filename follows the naming rules, false otherwise.
174 bool tomoyo_is_correct_path(const char *filename
, const s8 start_type
,
175 const s8 pattern_type
, const s8 end_type
,
176 const char *function
)
178 bool contains_pattern
= false;
182 const char *original_filename
= filename
;
187 if (start_type
== 1) { /* Must start with '/' */
190 } else if (start_type
== -1) { /* Must not start with '/' */
195 c
= *(filename
+ strlen(filename
) - 1);
196 if (end_type
== 1) { /* Must end with '/' */
199 } else if (end_type
== -1) { /* Must not end with '/' */
203 while ((c
= *filename
++) != '\0') {
205 switch ((c
= *filename
++)) {
206 case '\\': /* "\\" */
218 if (pattern_type
== -1)
219 break; /* Must not contain pattern */
220 contains_pattern
= true;
222 case '0': /* "\ooo" */
227 if (d
< '0' || d
> '7')
230 if (e
< '0' || e
> '7')
232 c
= tomoyo_make_byte(c
, d
, e
);
233 if (tomoyo_is_invalid(c
))
234 continue; /* pattern is not \000 */
237 } else if (tomoyo_is_invalid(c
)) {
241 if (pattern_type
== 1) { /* Must contain pattern */
242 if (!contains_pattern
)
247 printk(KERN_DEBUG
"%s: Invalid pathname '%s'\n", function
,
253 * tomoyo_is_correct_domain - Check whether the given domainname follows the naming rules.
254 * @domainname: The domainname to check.
255 * @function: The name of function calling me.
257 * Returns true if @domainname follows the naming rules, false otherwise.
259 bool tomoyo_is_correct_domain(const unsigned char *domainname
,
260 const char *function
)
265 const char *org_domainname
= domainname
;
267 if (!domainname
|| strncmp(domainname
, TOMOYO_ROOT_NAME
,
268 TOMOYO_ROOT_NAME_LEN
))
270 domainname
+= TOMOYO_ROOT_NAME_LEN
;
274 if (*domainname
++ != ' ')
276 if (*domainname
++ != '/')
278 while ((c
= *domainname
) != '\0' && c
!= ' ') {
283 case '\\': /* "\\" */
285 case '0': /* "\ooo" */
290 if (d
< '0' || d
> '7')
293 if (e
< '0' || e
> '7')
295 c
= tomoyo_make_byte(c
, d
, e
);
296 if (tomoyo_is_invalid(c
))
297 /* pattern is not \000 */
301 } else if (tomoyo_is_invalid(c
)) {
305 } while (*domainname
);
308 printk(KERN_DEBUG
"%s: Invalid domainname '%s'\n", function
,
314 * tomoyo_is_domain_def - Check whether the given token can be a domainname.
316 * @buffer: The token to check.
318 * Returns true if @buffer possibly be a domainname, false otherwise.
320 bool tomoyo_is_domain_def(const unsigned char *buffer
)
322 return !strncmp(buffer
, TOMOYO_ROOT_NAME
, TOMOYO_ROOT_NAME_LEN
);
326 * tomoyo_find_domain - Find a domain by the given name.
328 * @domainname: The domainname to find.
330 * Caller must call down_read(&tomoyo_domain_list_lock); or
331 * down_write(&tomoyo_domain_list_lock); .
333 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
335 struct tomoyo_domain_info
*tomoyo_find_domain(const char *domainname
)
337 struct tomoyo_domain_info
*domain
;
338 struct tomoyo_path_info name
;
340 name
.name
= domainname
;
341 tomoyo_fill_path_info(&name
);
342 list_for_each_entry(domain
, &tomoyo_domain_list
, list
) {
343 if (!domain
->is_deleted
&&
344 !tomoyo_pathcmp(&name
, domain
->domainname
))
351 * tomoyo_path_depth - Evaluate the number of '/' in a string.
353 * @pathname: The string to evaluate.
355 * Returns path depth of the string.
357 * I score 2 for each of the '/' in the @pathname
358 * and score 1 if the @pathname ends with '/'.
360 static int tomoyo_path_depth(const char *pathname
)
365 const char *ep
= pathname
+ strlen(pathname
);
366 if (pathname
< ep
--) {
369 while (pathname
<= ep
)
378 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
380 * @filename: The string to evaluate.
382 * Returns the initial length without a pattern in @filename.
384 static int tomoyo_const_part_length(const char *filename
)
391 while ((c
= *filename
++) != '\0') {
398 case '\\': /* "\\" */
401 case '0': /* "\ooo" */
406 if (c
< '0' || c
> '7')
409 if (c
< '0' || c
> '7')
420 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
422 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
424 * The caller sets "struct tomoyo_path_info"->name.
426 void tomoyo_fill_path_info(struct tomoyo_path_info
*ptr
)
428 const char *name
= ptr
->name
;
429 const int len
= strlen(name
);
431 ptr
->total_len
= len
;
432 ptr
->const_len
= tomoyo_const_part_length(name
);
433 ptr
->is_dir
= len
&& (name
[len
- 1] == '/');
434 ptr
->is_patterned
= (ptr
->const_len
< len
);
435 ptr
->hash
= full_name_hash(name
, len
);
436 ptr
->depth
= tomoyo_path_depth(name
);
440 * tomoyo_file_matches_to_pattern2 - Pattern matching without '/' character
443 * @filename: The start of string to check.
444 * @filename_end: The end of string to check.
445 * @pattern: The start of pattern to compare.
446 * @pattern_end: The end of pattern to compare.
448 * Returns true if @filename matches @pattern, false otherwise.
450 static bool tomoyo_file_matches_to_pattern2(const char *filename
,
451 const char *filename_end
,
453 const char *pattern_end
)
455 while (filename
< filename_end
&& pattern
< pattern_end
) {
457 if (*pattern
!= '\\') {
458 if (*filename
++ != *pattern
++)
470 } else if (c
== '\\') {
471 if (filename
[1] == '\\')
473 else if (tomoyo_is_byte_range(filename
+ 1))
482 if (*++filename
!= '\\')
494 if (!tomoyo_is_alphabet_char(c
))
501 if (c
== '\\' && tomoyo_is_byte_range(filename
+ 1)
502 && strncmp(filename
+ 1, pattern
, 3) == 0) {
507 return false; /* Not matched. */
510 for (i
= 0; i
<= filename_end
- filename
; i
++) {
511 if (tomoyo_file_matches_to_pattern2(
512 filename
+ i
, filename_end
,
513 pattern
+ 1, pattern_end
))
516 if (c
== '.' && *pattern
== '@')
520 if (filename
[i
+ 1] == '\\')
522 else if (tomoyo_is_byte_range(filename
+ i
+ 1))
525 break; /* Bad pattern. */
527 return false; /* Not matched. */
532 while (isdigit(filename
[j
]))
534 } else if (c
== 'X') {
535 while (isxdigit(filename
[j
]))
537 } else if (c
== 'A') {
538 while (tomoyo_is_alphabet_char(filename
[j
]))
541 for (i
= 1; i
<= j
; i
++) {
542 if (tomoyo_file_matches_to_pattern2(
543 filename
+ i
, filename_end
,
544 pattern
+ 1, pattern_end
))
547 return false; /* Not matched or bad pattern. */
552 while (*pattern
== '\\' &&
553 (*(pattern
+ 1) == '*' || *(pattern
+ 1) == '@'))
555 return filename
== filename_end
&& pattern
== pattern_end
;
559 * tomoyo_file_matches_to_pattern - Pattern matching without without '/' character.
561 * @filename: The start of string to check.
562 * @filename_end: The end of string to check.
563 * @pattern: The start of pattern to compare.
564 * @pattern_end: The end of pattern to compare.
566 * Returns true if @filename matches @pattern, false otherwise.
568 static bool tomoyo_file_matches_to_pattern(const char *filename
,
569 const char *filename_end
,
571 const char *pattern_end
)
573 const char *pattern_start
= pattern
;
577 while (pattern
< pattern_end
- 1) {
578 /* Split at "\-" pattern. */
579 if (*pattern
++ != '\\' || *pattern
++ != '-')
581 result
= tomoyo_file_matches_to_pattern2(filename
,
590 pattern_start
= pattern
;
592 result
= tomoyo_file_matches_to_pattern2(filename
, filename_end
,
593 pattern_start
, pattern_end
);
594 return first
? result
: !result
;
598 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
599 * @filename: The filename to check.
600 * @pattern: The pattern to compare.
602 * Returns true if matches, false otherwise.
604 * The following patterns are available.
606 * \ooo Octal representation of a byte.
607 * \* More than or equals to 0 character other than '/'.
608 * \@ More than or equals to 0 character other than '/' or '.'.
609 * \? 1 byte character other than '/'.
610 * \$ More than or equals to 1 decimal digit.
611 * \+ 1 decimal digit.
612 * \X More than or equals to 1 hexadecimal digit.
613 * \x 1 hexadecimal digit.
614 * \A More than or equals to 1 alphabet character.
615 * \a 1 alphabet character.
616 * \- Subtraction operator.
618 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info
*filename
,
619 const struct tomoyo_path_info
*pattern
)
622 if (!filename || !pattern)
625 const char *f
= filename
->name
;
626 const char *p
= pattern
->name
;
627 const int len
= pattern
->const_len
;
629 /* If @pattern doesn't contain pattern, I can use strcmp(). */
630 if (!pattern
->is_patterned
)
631 return !tomoyo_pathcmp(filename
, pattern
);
632 /* Dont compare if the number of '/' differs. */
633 if (filename
->depth
!= pattern
->depth
)
635 /* Compare the initial length without patterns. */
636 if (strncmp(f
, p
, len
))
640 /* Main loop. Compare each directory component. */
642 const char *f_delimiter
= strchr(f
, '/');
643 const char *p_delimiter
= strchr(p
, '/');
645 f_delimiter
= f
+ strlen(f
);
647 p_delimiter
= p
+ strlen(p
);
648 if (!tomoyo_file_matches_to_pattern(f
, f_delimiter
,
658 /* Ignore trailing "\*" and "\@" in @pattern. */
660 (*(p
+ 1) == '*' || *(p
+ 1) == '@'))
666 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
668 * @head: Pointer to "struct tomoyo_io_buffer".
669 * @fmt: The printf()'s format string, followed by parameters.
671 * Returns true if output was written, false otherwise.
673 * The snprintf() will truncate, but tomoyo_io_printf() won't.
675 bool tomoyo_io_printf(struct tomoyo_io_buffer
*head
, const char *fmt
, ...)
679 int pos
= head
->read_avail
;
680 int size
= head
->readbuf_size
- pos
;
685 len
= vsnprintf(head
->read_buf
+ pos
, size
, fmt
, args
);
687 if (pos
+ len
>= head
->readbuf_size
)
689 head
->read_avail
+= len
;
694 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
696 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
698 * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
699 * if this function didn't return NULL.
701 static const char *tomoyo_get_exe(void)
703 struct mm_struct
*mm
= current
->mm
;
704 struct vm_area_struct
*vma
;
705 const char *cp
= NULL
;
709 down_read(&mm
->mmap_sem
);
710 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
711 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
) {
712 cp
= tomoyo_realpath_from_path(&vma
->vm_file
->f_path
);
716 up_read(&mm
->mmap_sem
);
721 * tomoyo_get_msg - Get warning message.
723 * @is_enforce: Is it enforcing mode?
725 * Returns "ERROR" or "WARNING".
727 const char *tomoyo_get_msg(const bool is_enforce
)
736 * tomoyo_check_flags - Check mode for specified functionality.
738 * @domain: Pointer to "struct tomoyo_domain_info".
739 * @index: The functionality to check mode.
741 * TOMOYO checks only process context.
742 * This code disables TOMOYO's enforcement in case the function is called from
745 unsigned int tomoyo_check_flags(const struct tomoyo_domain_info
*domain
,
748 const u8 profile
= domain
->profile
;
750 if (WARN_ON(in_interrupt()))
752 return tomoyo_policy_loaded
&& index
< TOMOYO_MAX_CONTROL_INDEX
753 #if TOMOYO_MAX_PROFILES != 256
754 && profile
< TOMOYO_MAX_PROFILES
756 && tomoyo_profile_ptr
[profile
] ?
757 tomoyo_profile_ptr
[profile
]->value
[index
] : 0;
761 * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
763 * @domain: Pointer to "struct tomoyo_domain_info".
765 * Returns true if domain policy violation warning should be printed to
768 bool tomoyo_verbose_mode(const struct tomoyo_domain_info
*domain
)
770 return tomoyo_check_flags(domain
, TOMOYO_VERBOSE
) != 0;
774 * tomoyo_domain_quota_is_ok - Check for domain's quota.
776 * @domain: Pointer to "struct tomoyo_domain_info".
778 * Returns true if the domain is not exceeded quota, false otherwise.
780 bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info
* const domain
)
782 unsigned int count
= 0;
783 struct tomoyo_acl_info
*ptr
;
787 down_read(&tomoyo_domain_acl_info_list_lock
);
788 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
789 if (ptr
->type
& TOMOYO_ACL_DELETED
)
791 switch (tomoyo_acl_type2(ptr
)) {
792 struct tomoyo_single_path_acl_record
*acl1
;
793 struct tomoyo_double_path_acl_record
*acl2
;
795 case TOMOYO_TYPE_SINGLE_PATH_ACL
:
796 acl1
= container_of(ptr
,
797 struct tomoyo_single_path_acl_record
,
800 if (perm
& (1 << TOMOYO_TYPE_EXECUTE_ACL
))
803 ((1 << TOMOYO_TYPE_READ_ACL
) |
804 (1 << TOMOYO_TYPE_WRITE_ACL
)))
806 if (perm
& (1 << TOMOYO_TYPE_CREATE_ACL
))
808 if (perm
& (1 << TOMOYO_TYPE_UNLINK_ACL
))
810 if (perm
& (1 << TOMOYO_TYPE_MKDIR_ACL
))
812 if (perm
& (1 << TOMOYO_TYPE_RMDIR_ACL
))
814 if (perm
& (1 << TOMOYO_TYPE_MKFIFO_ACL
))
816 if (perm
& (1 << TOMOYO_TYPE_MKSOCK_ACL
))
818 if (perm
& (1 << TOMOYO_TYPE_MKBLOCK_ACL
))
820 if (perm
& (1 << TOMOYO_TYPE_MKCHAR_ACL
))
822 if (perm
& (1 << TOMOYO_TYPE_TRUNCATE_ACL
))
824 if (perm
& (1 << TOMOYO_TYPE_SYMLINK_ACL
))
826 if (perm
& (1 << TOMOYO_TYPE_REWRITE_ACL
))
829 case TOMOYO_TYPE_DOUBLE_PATH_ACL
:
830 acl2
= container_of(ptr
,
831 struct tomoyo_double_path_acl_record
,
834 if (perm
& (1 << TOMOYO_TYPE_LINK_ACL
))
836 if (perm
& (1 << TOMOYO_TYPE_RENAME_ACL
))
841 up_read(&tomoyo_domain_acl_info_list_lock
);
842 if (count
< tomoyo_check_flags(domain
, TOMOYO_MAX_ACCEPT_ENTRY
))
844 if (!domain
->quota_warned
) {
845 domain
->quota_warned
= true;
846 printk(KERN_WARNING
"TOMOYO-WARNING: "
847 "Domain '%s' has so many ACLs to hold. "
848 "Stopped learning mode.\n", domain
->domainname
->name
);
854 * tomoyo_find_or_assign_new_profile - Create a new profile.
856 * @profile: Profile number to create.
858 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
860 static struct tomoyo_profile
*tomoyo_find_or_assign_new_profile(const unsigned
863 static DEFINE_MUTEX(lock
);
864 struct tomoyo_profile
*ptr
= NULL
;
867 if (profile
>= TOMOYO_MAX_PROFILES
)
869 /***** EXCLUSIVE SECTION START *****/
871 ptr
= tomoyo_profile_ptr
[profile
];
874 ptr
= tomoyo_alloc_element(sizeof(*ptr
));
877 for (i
= 0; i
< TOMOYO_MAX_CONTROL_INDEX
; i
++)
878 ptr
->value
[i
] = tomoyo_control_array
[i
].current_value
;
879 mb(); /* Avoid out-of-order execution. */
880 tomoyo_profile_ptr
[profile
] = ptr
;
883 /***** EXCLUSIVE SECTION END *****/
888 * tomoyo_write_profile - Write to profile table.
890 * @head: Pointer to "struct tomoyo_io_buffer".
892 * Returns 0 on success, negative value otherwise.
894 static int tomoyo_write_profile(struct tomoyo_io_buffer
*head
)
896 char *data
= head
->write_buf
;
900 struct tomoyo_profile
*profile
;
903 cp
= strchr(data
, '-');
906 if (strict_strtoul(data
, 10, &num
))
910 profile
= tomoyo_find_or_assign_new_profile(num
);
913 cp
= strchr(data
, '=');
917 if (!strcmp(data
, "COMMENT")) {
918 profile
->comment
= tomoyo_save_name(cp
+ 1);
921 for (i
= 0; i
< TOMOYO_MAX_CONTROL_INDEX
; i
++) {
922 if (strcmp(data
, tomoyo_control_array
[i
].keyword
))
924 if (sscanf(cp
+ 1, "%u", &value
) != 1) {
929 modes
= tomoyo_mode_2
;
932 modes
= tomoyo_mode_4
;
935 for (j
= 0; j
< 4; j
++) {
936 if (strcmp(cp
+ 1, modes
[j
]))
943 } else if (value
> tomoyo_control_array
[i
].max_value
) {
944 value
= tomoyo_control_array
[i
].max_value
;
946 profile
->value
[i
] = value
;
953 * tomoyo_read_profile - Read from profile table.
955 * @head: Pointer to "struct tomoyo_io_buffer".
959 static int tomoyo_read_profile(struct tomoyo_io_buffer
*head
)
961 static const int total
= TOMOYO_MAX_CONTROL_INDEX
+ 1;
966 for (step
= head
->read_step
; step
< TOMOYO_MAX_PROFILES
* total
;
968 const u8 index
= step
/ total
;
969 u8 type
= step
% total
;
970 const struct tomoyo_profile
*profile
971 = tomoyo_profile_ptr
[index
];
972 head
->read_step
= step
;
975 if (!type
) { /* Print profile' comment tag. */
976 if (!tomoyo_io_printf(head
, "%u-COMMENT=%s\n",
977 index
, profile
->comment
?
978 profile
->comment
->name
: ""))
983 if (type
< TOMOYO_MAX_CONTROL_INDEX
) {
984 const unsigned int value
= profile
->value
[type
];
985 const char **modes
= NULL
;
987 = tomoyo_control_array
[type
].keyword
;
988 switch (tomoyo_control_array
[type
].max_value
) {
990 modes
= tomoyo_mode_4
;
993 modes
= tomoyo_mode_2
;
997 if (!tomoyo_io_printf(head
, "%u-%s=%s\n", index
,
998 keyword
, modes
[value
]))
1001 if (!tomoyo_io_printf(head
, "%u-%s=%u\n", index
,
1007 if (step
== TOMOYO_MAX_PROFILES
* total
)
1008 head
->read_eof
= true;
1012 /* Structure for policy manager. */
1013 struct tomoyo_policy_manager_entry
{
1014 struct list_head list
;
1015 /* A path to program or a domainname. */
1016 const struct tomoyo_path_info
*manager
;
1017 bool is_domain
; /* True if manager is a domainname. */
1018 bool is_deleted
; /* True if this entry is deleted. */
1021 /* The list for "struct tomoyo_policy_manager_entry". */
1022 static LIST_HEAD(tomoyo_policy_manager_list
);
1023 static DECLARE_RWSEM(tomoyo_policy_manager_list_lock
);
1026 * tomoyo_update_manager_entry - Add a manager entry.
1028 * @manager: The path to manager or the domainnamme.
1029 * @is_delete: True if it is a delete request.
1031 * Returns 0 on success, negative value otherwise.
1033 static int tomoyo_update_manager_entry(const char *manager
,
1034 const bool is_delete
)
1036 struct tomoyo_policy_manager_entry
*new_entry
;
1037 struct tomoyo_policy_manager_entry
*ptr
;
1038 const struct tomoyo_path_info
*saved_manager
;
1039 int error
= -ENOMEM
;
1040 bool is_domain
= false;
1042 if (tomoyo_is_domain_def(manager
)) {
1043 if (!tomoyo_is_correct_domain(manager
, __func__
))
1047 if (!tomoyo_is_correct_path(manager
, 1, -1, -1, __func__
))
1050 saved_manager
= tomoyo_save_name(manager
);
1053 /***** EXCLUSIVE SECTION START *****/
1054 down_write(&tomoyo_policy_manager_list_lock
);
1055 list_for_each_entry(ptr
, &tomoyo_policy_manager_list
, list
) {
1056 if (ptr
->manager
!= saved_manager
)
1058 ptr
->is_deleted
= is_delete
;
1066 new_entry
= tomoyo_alloc_element(sizeof(*new_entry
));
1069 new_entry
->manager
= saved_manager
;
1070 new_entry
->is_domain
= is_domain
;
1071 list_add_tail(&new_entry
->list
, &tomoyo_policy_manager_list
);
1074 up_write(&tomoyo_policy_manager_list_lock
);
1075 /***** EXCLUSIVE SECTION END *****/
1080 * tomoyo_write_manager_policy - Write manager policy.
1082 * @head: Pointer to "struct tomoyo_io_buffer".
1084 * Returns 0 on success, negative value otherwise.
1086 static int tomoyo_write_manager_policy(struct tomoyo_io_buffer
*head
)
1088 char *data
= head
->write_buf
;
1089 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1091 if (!strcmp(data
, "manage_by_non_root")) {
1092 tomoyo_manage_by_non_root
= !is_delete
;
1095 return tomoyo_update_manager_entry(data
, is_delete
);
1099 * tomoyo_read_manager_policy - Read manager policy.
1101 * @head: Pointer to "struct tomoyo_io_buffer".
1105 static int tomoyo_read_manager_policy(struct tomoyo_io_buffer
*head
)
1107 struct list_head
*pos
;
1112 down_read(&tomoyo_policy_manager_list_lock
);
1113 list_for_each_cookie(pos
, head
->read_var2
,
1114 &tomoyo_policy_manager_list
) {
1115 struct tomoyo_policy_manager_entry
*ptr
;
1116 ptr
= list_entry(pos
, struct tomoyo_policy_manager_entry
,
1118 if (ptr
->is_deleted
)
1120 if (!tomoyo_io_printf(head
, "%s\n", ptr
->manager
->name
)) {
1125 up_read(&tomoyo_policy_manager_list_lock
);
1126 head
->read_eof
= done
;
1131 * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
1133 * Returns true if the current process is permitted to modify policy
1134 * via /sys/kernel/security/tomoyo/ interface.
1136 static bool tomoyo_is_policy_manager(void)
1138 struct tomoyo_policy_manager_entry
*ptr
;
1140 const struct task_struct
*task
= current
;
1141 const struct tomoyo_path_info
*domainname
= tomoyo_domain()->domainname
;
1144 if (!tomoyo_policy_loaded
)
1146 if (!tomoyo_manage_by_non_root
&& (task
->cred
->uid
|| task
->cred
->euid
))
1148 down_read(&tomoyo_policy_manager_list_lock
);
1149 list_for_each_entry(ptr
, &tomoyo_policy_manager_list
, list
) {
1150 if (!ptr
->is_deleted
&& ptr
->is_domain
1151 && !tomoyo_pathcmp(domainname
, ptr
->manager
)) {
1156 up_read(&tomoyo_policy_manager_list_lock
);
1159 exe
= tomoyo_get_exe();
1162 down_read(&tomoyo_policy_manager_list_lock
);
1163 list_for_each_entry(ptr
, &tomoyo_policy_manager_list
, list
) {
1164 if (!ptr
->is_deleted
&& !ptr
->is_domain
1165 && !strcmp(exe
, ptr
->manager
->name
)) {
1170 up_read(&tomoyo_policy_manager_list_lock
);
1171 if (!found
) { /* Reduce error messages. */
1172 static pid_t last_pid
;
1173 const pid_t pid
= current
->pid
;
1174 if (last_pid
!= pid
) {
1175 printk(KERN_WARNING
"%s ( %s ) is not permitted to "
1176 "update policies.\n", domainname
->name
, exe
);
1185 * tomoyo_is_select_one - Parse select command.
1187 * @head: Pointer to "struct tomoyo_io_buffer".
1188 * @data: String to parse.
1190 * Returns true on success, false otherwise.
1192 static bool tomoyo_is_select_one(struct tomoyo_io_buffer
*head
,
1196 struct tomoyo_domain_info
*domain
= NULL
;
1198 if (sscanf(data
, "pid=%u", &pid
) == 1) {
1199 struct task_struct
*p
;
1200 /***** CRITICAL SECTION START *****/
1201 read_lock(&tasklist_lock
);
1202 p
= find_task_by_vpid(pid
);
1204 domain
= tomoyo_real_domain(p
);
1205 read_unlock(&tasklist_lock
);
1206 /***** CRITICAL SECTION END *****/
1207 } else if (!strncmp(data
, "domain=", 7)) {
1208 if (tomoyo_is_domain_def(data
+ 7)) {
1209 down_read(&tomoyo_domain_list_lock
);
1210 domain
= tomoyo_find_domain(data
+ 7);
1211 up_read(&tomoyo_domain_list_lock
);
1215 head
->write_var1
= domain
;
1216 /* Accessing read_buf is safe because head->io_sem is held. */
1217 if (!head
->read_buf
)
1218 return true; /* Do nothing if open(O_WRONLY). */
1219 head
->read_avail
= 0;
1220 tomoyo_io_printf(head
, "# select %s\n", data
);
1221 head
->read_single_domain
= true;
1222 head
->read_eof
= !domain
;
1224 struct tomoyo_domain_info
*d
;
1225 head
->read_var1
= NULL
;
1226 down_read(&tomoyo_domain_list_lock
);
1227 list_for_each_entry(d
, &tomoyo_domain_list
, list
) {
1230 head
->read_var1
= &d
->list
;
1232 up_read(&tomoyo_domain_list_lock
);
1233 head
->read_var2
= NULL
;
1235 head
->read_step
= 0;
1236 if (domain
->is_deleted
)
1237 tomoyo_io_printf(head
, "# This is a deleted domain.\n");
1243 * tomoyo_write_domain_policy - Write domain policy.
1245 * @head: Pointer to "struct tomoyo_io_buffer".
1247 * Returns 0 on success, negative value otherwise.
1249 static int tomoyo_write_domain_policy(struct tomoyo_io_buffer
*head
)
1251 char *data
= head
->write_buf
;
1252 struct tomoyo_domain_info
*domain
= head
->write_var1
;
1253 bool is_delete
= false;
1254 bool is_select
= false;
1255 unsigned int profile
;
1257 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
))
1259 else if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_SELECT
))
1261 if (is_select
&& tomoyo_is_select_one(head
, data
))
1263 /* Don't allow updating policies by non manager programs. */
1264 if (!tomoyo_is_policy_manager())
1266 if (tomoyo_is_domain_def(data
)) {
1269 tomoyo_delete_domain(data
);
1270 else if (is_select
) {
1271 down_read(&tomoyo_domain_list_lock
);
1272 domain
= tomoyo_find_domain(data
);
1273 up_read(&tomoyo_domain_list_lock
);
1275 domain
= tomoyo_find_or_assign_new_domain(data
, 0);
1276 head
->write_var1
= domain
;
1282 if (sscanf(data
, TOMOYO_KEYWORD_USE_PROFILE
"%u", &profile
) == 1
1283 && profile
< TOMOYO_MAX_PROFILES
) {
1284 if (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
)
1285 domain
->profile
= (u8
) profile
;
1288 if (!strcmp(data
, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
)) {
1289 tomoyo_set_domain_flag(domain
, is_delete
,
1290 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
);
1293 return tomoyo_write_file_policy(data
, domain
, is_delete
);
1297 * tomoyo_print_single_path_acl - Print a single path ACL entry.
1299 * @head: Pointer to "struct tomoyo_io_buffer".
1300 * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
1302 * Returns true on success, false otherwise.
1304 static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer
*head
,
1305 struct tomoyo_single_path_acl_record
*
1310 const char *atmark
= "";
1311 const char *filename
;
1312 const u16 perm
= ptr
->perm
;
1314 filename
= ptr
->filename
->name
;
1315 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_SINGLE_PATH_OPERATION
;
1318 if (!(perm
& (1 << bit
)))
1320 /* Print "read/write" instead of "read" and "write". */
1321 if ((bit
== TOMOYO_TYPE_READ_ACL
||
1322 bit
== TOMOYO_TYPE_WRITE_ACL
)
1323 && (perm
& (1 << TOMOYO_TYPE_READ_WRITE_ACL
)))
1325 msg
= tomoyo_sp2keyword(bit
);
1326 pos
= head
->read_avail
;
1327 if (!tomoyo_io_printf(head
, "allow_%s %s%s\n", msg
,
1334 head
->read_bit
= bit
;
1335 head
->read_avail
= pos
;
1340 * tomoyo_print_double_path_acl - Print a double path ACL entry.
1342 * @head: Pointer to "struct tomoyo_io_buffer".
1343 * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
1345 * Returns true on success, false otherwise.
1347 static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer
*head
,
1348 struct tomoyo_double_path_acl_record
*
1352 const char *atmark1
= "";
1353 const char *atmark2
= "";
1354 const char *filename1
;
1355 const char *filename2
;
1356 const u8 perm
= ptr
->perm
;
1359 filename1
= ptr
->filename1
->name
;
1360 filename2
= ptr
->filename2
->name
;
1361 for (bit
= head
->read_bit
; bit
< TOMOYO_MAX_DOUBLE_PATH_OPERATION
;
1364 if (!(perm
& (1 << bit
)))
1366 msg
= tomoyo_dp2keyword(bit
);
1367 pos
= head
->read_avail
;
1368 if (!tomoyo_io_printf(head
, "allow_%s %s%s %s%s\n", msg
,
1369 atmark1
, filename1
, atmark2
, filename2
))
1375 head
->read_bit
= bit
;
1376 head
->read_avail
= pos
;
1381 * tomoyo_print_entry - Print an ACL entry.
1383 * @head: Pointer to "struct tomoyo_io_buffer".
1384 * @ptr: Pointer to an ACL entry.
1386 * Returns true on success, false otherwise.
1388 static bool tomoyo_print_entry(struct tomoyo_io_buffer
*head
,
1389 struct tomoyo_acl_info
*ptr
)
1391 const u8 acl_type
= tomoyo_acl_type2(ptr
);
1393 if (acl_type
& TOMOYO_ACL_DELETED
)
1395 if (acl_type
== TOMOYO_TYPE_SINGLE_PATH_ACL
) {
1396 struct tomoyo_single_path_acl_record
*acl
1398 struct tomoyo_single_path_acl_record
,
1400 return tomoyo_print_single_path_acl(head
, acl
);
1402 if (acl_type
== TOMOYO_TYPE_DOUBLE_PATH_ACL
) {
1403 struct tomoyo_double_path_acl_record
*acl
1405 struct tomoyo_double_path_acl_record
,
1407 return tomoyo_print_double_path_acl(head
, acl
);
1409 BUG(); /* This must not happen. */
1414 * tomoyo_read_domain_policy - Read domain policy.
1416 * @head: Pointer to "struct tomoyo_io_buffer".
1420 static int tomoyo_read_domain_policy(struct tomoyo_io_buffer
*head
)
1422 struct list_head
*dpos
;
1423 struct list_head
*apos
;
1428 if (head
->read_step
== 0)
1429 head
->read_step
= 1;
1430 down_read(&tomoyo_domain_list_lock
);
1431 list_for_each_cookie(dpos
, head
->read_var1
, &tomoyo_domain_list
) {
1432 struct tomoyo_domain_info
*domain
;
1433 const char *quota_exceeded
= "";
1434 const char *transition_failed
= "";
1435 const char *ignore_global_allow_read
= "";
1436 domain
= list_entry(dpos
, struct tomoyo_domain_info
, list
);
1437 if (head
->read_step
!= 1)
1439 if (domain
->is_deleted
&& !head
->read_single_domain
)
1441 /* Print domainname and flags. */
1442 if (domain
->quota_warned
)
1443 quota_exceeded
= "quota_exceeded\n";
1444 if (domain
->flags
& TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED
)
1445 transition_failed
= "transition_failed\n";
1447 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
)
1448 ignore_global_allow_read
1449 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
"\n";
1450 if (!tomoyo_io_printf(head
,
1451 "%s\n" TOMOYO_KEYWORD_USE_PROFILE
"%u\n"
1452 "%s%s%s\n", domain
->domainname
->name
,
1453 domain
->profile
, quota_exceeded
,
1455 ignore_global_allow_read
)) {
1459 head
->read_step
= 2;
1461 if (head
->read_step
== 3)
1463 /* Print ACL entries in the domain. */
1464 down_read(&tomoyo_domain_acl_info_list_lock
);
1465 list_for_each_cookie(apos
, head
->read_var2
,
1466 &domain
->acl_info_list
) {
1467 struct tomoyo_acl_info
*ptr
1468 = list_entry(apos
, struct tomoyo_acl_info
,
1470 if (!tomoyo_print_entry(head
, ptr
)) {
1475 up_read(&tomoyo_domain_acl_info_list_lock
);
1478 head
->read_step
= 3;
1480 if (!tomoyo_io_printf(head
, "\n")) {
1484 head
->read_step
= 1;
1485 if (head
->read_single_domain
)
1488 up_read(&tomoyo_domain_list_lock
);
1489 head
->read_eof
= done
;
1494 * tomoyo_write_domain_profile - Assign profile for specified domain.
1496 * @head: Pointer to "struct tomoyo_io_buffer".
1498 * Returns 0 on success, -EINVAL otherwise.
1500 * This is equivalent to doing
1502 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1503 * /usr/lib/ccs/loadpolicy -d
1505 static int tomoyo_write_domain_profile(struct tomoyo_io_buffer
*head
)
1507 char *data
= head
->write_buf
;
1508 char *cp
= strchr(data
, ' ');
1509 struct tomoyo_domain_info
*domain
;
1510 unsigned long profile
;
1515 down_read(&tomoyo_domain_list_lock
);
1516 domain
= tomoyo_find_domain(cp
+ 1);
1517 up_read(&tomoyo_domain_list_lock
);
1518 if (strict_strtoul(data
, 10, &profile
))
1520 if (domain
&& profile
< TOMOYO_MAX_PROFILES
1521 && (tomoyo_profile_ptr
[profile
] || !tomoyo_policy_loaded
))
1522 domain
->profile
= (u8
) profile
;
1527 * tomoyo_read_domain_profile - Read only domainname and profile.
1529 * @head: Pointer to "struct tomoyo_io_buffer".
1531 * Returns list of profile number and domainname pairs.
1533 * This is equivalent to doing
1535 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1536 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1537 * domainname = $0; } else if ( $1 == "use_profile" ) {
1538 * print $2 " " domainname; domainname = ""; } } ; '
1540 static int tomoyo_read_domain_profile(struct tomoyo_io_buffer
*head
)
1542 struct list_head
*pos
;
1547 down_read(&tomoyo_domain_list_lock
);
1548 list_for_each_cookie(pos
, head
->read_var1
, &tomoyo_domain_list
) {
1549 struct tomoyo_domain_info
*domain
;
1550 domain
= list_entry(pos
, struct tomoyo_domain_info
, list
);
1551 if (domain
->is_deleted
)
1553 if (!tomoyo_io_printf(head
, "%u %s\n", domain
->profile
,
1554 domain
->domainname
->name
)) {
1559 up_read(&tomoyo_domain_list_lock
);
1560 head
->read_eof
= done
;
1565 * tomoyo_write_pid: Specify PID to obtain domainname.
1567 * @head: Pointer to "struct tomoyo_io_buffer".
1571 static int tomoyo_write_pid(struct tomoyo_io_buffer
*head
)
1574 /* No error check. */
1575 strict_strtoul(head
->write_buf
, 10, &pid
);
1576 head
->read_step
= (int) pid
;
1577 head
->read_eof
= false;
1582 * tomoyo_read_pid - Get domainname of the specified PID.
1584 * @head: Pointer to "struct tomoyo_io_buffer".
1586 * Returns the domainname which the specified PID is in on success,
1587 * empty string otherwise.
1588 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1589 * using read()/write() interface rather than sysctl() interface.
1591 static int tomoyo_read_pid(struct tomoyo_io_buffer
*head
)
1593 if (head
->read_avail
== 0 && !head
->read_eof
) {
1594 const int pid
= head
->read_step
;
1595 struct task_struct
*p
;
1596 struct tomoyo_domain_info
*domain
= NULL
;
1597 /***** CRITICAL SECTION START *****/
1598 read_lock(&tasklist_lock
);
1599 p
= find_task_by_vpid(pid
);
1601 domain
= tomoyo_real_domain(p
);
1602 read_unlock(&tasklist_lock
);
1603 /***** CRITICAL SECTION END *****/
1605 tomoyo_io_printf(head
, "%d %u %s", pid
, domain
->profile
,
1606 domain
->domainname
->name
);
1607 head
->read_eof
= true;
1613 * tomoyo_write_exception_policy - Write exception policy.
1615 * @head: Pointer to "struct tomoyo_io_buffer".
1617 * Returns 0 on success, negative value otherwise.
1619 static int tomoyo_write_exception_policy(struct tomoyo_io_buffer
*head
)
1621 char *data
= head
->write_buf
;
1622 bool is_delete
= tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DELETE
);
1624 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_KEEP_DOMAIN
))
1625 return tomoyo_write_domain_keeper_policy(data
, false,
1627 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_KEEP_DOMAIN
))
1628 return tomoyo_write_domain_keeper_policy(data
, true, is_delete
);
1629 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_INITIALIZE_DOMAIN
))
1630 return tomoyo_write_domain_initializer_policy(data
, false,
1632 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN
))
1633 return tomoyo_write_domain_initializer_policy(data
, true,
1635 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALIAS
))
1636 return tomoyo_write_alias_policy(data
, is_delete
);
1637 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_ALLOW_READ
))
1638 return tomoyo_write_globally_readable_policy(data
, is_delete
);
1639 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_FILE_PATTERN
))
1640 return tomoyo_write_pattern_policy(data
, is_delete
);
1641 if (tomoyo_str_starts(&data
, TOMOYO_KEYWORD_DENY_REWRITE
))
1642 return tomoyo_write_no_rewrite_policy(data
, is_delete
);
1647 * tomoyo_read_exception_policy - Read exception policy.
1649 * @head: Pointer to "struct tomoyo_io_buffer".
1651 * Returns 0 on success, -EINVAL otherwise.
1653 static int tomoyo_read_exception_policy(struct tomoyo_io_buffer
*head
)
1655 if (!head
->read_eof
) {
1656 switch (head
->read_step
) {
1658 head
->read_var2
= NULL
;
1659 head
->read_step
= 1;
1661 if (!tomoyo_read_domain_keeper_policy(head
))
1663 head
->read_var2
= NULL
;
1664 head
->read_step
= 2;
1666 if (!tomoyo_read_globally_readable_policy(head
))
1668 head
->read_var2
= NULL
;
1669 head
->read_step
= 3;
1671 head
->read_var2
= NULL
;
1672 head
->read_step
= 4;
1674 if (!tomoyo_read_domain_initializer_policy(head
))
1676 head
->read_var2
= NULL
;
1677 head
->read_step
= 5;
1679 if (!tomoyo_read_alias_policy(head
))
1681 head
->read_var2
= NULL
;
1682 head
->read_step
= 6;
1684 head
->read_var2
= NULL
;
1685 head
->read_step
= 7;
1687 if (!tomoyo_read_file_pattern(head
))
1689 head
->read_var2
= NULL
;
1690 head
->read_step
= 8;
1692 if (!tomoyo_read_no_rewrite_policy(head
))
1694 head
->read_var2
= NULL
;
1695 head
->read_step
= 9;
1697 head
->read_eof
= true;
1706 /* path to policy loader */
1707 static const char *tomoyo_loader
= "/sbin/tomoyo-init";
1710 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
1712 * Returns true if /sbin/tomoyo-init exists, false otherwise.
1714 static bool tomoyo_policy_loader_exists(void)
1717 * Don't activate MAC if the policy loader doesn't exist.
1718 * If the initrd includes /sbin/init but real-root-dev has not
1719 * mounted on / yet, activating MAC will block the system since
1720 * policies are not loaded yet.
1721 * Thus, let do_execve() call this function everytime.
1723 struct nameidata nd
;
1725 if (path_lookup(tomoyo_loader
, LOOKUP_FOLLOW
, &nd
)) {
1726 printk(KERN_INFO
"Not activating Mandatory Access Control now "
1727 "since %s doesn't exist.\n", tomoyo_loader
);
1735 * tomoyo_load_policy - Run external policy loader to load policy.
1737 * @filename: The program about to start.
1739 * This function checks whether @filename is /sbin/init , and if so
1740 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
1741 * and then continues invocation of /sbin/init.
1742 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
1743 * writes to /sys/kernel/security/tomoyo/ interfaces.
1747 void tomoyo_load_policy(const char *filename
)
1752 if (tomoyo_policy_loaded
)
1755 * Check filename is /sbin/init or /sbin/tomoyo-start.
1756 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
1758 * You can create /sbin/tomoyo-start by
1759 * "ln -s /bin/true /sbin/tomoyo-start".
1761 if (strcmp(filename
, "/sbin/init") &&
1762 strcmp(filename
, "/sbin/tomoyo-start"))
1764 if (!tomoyo_policy_loader_exists())
1767 printk(KERN_INFO
"Calling %s to load policy. Please wait.\n",
1769 argv
[0] = (char *) tomoyo_loader
;
1772 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1774 call_usermodehelper(argv
[0], argv
, envp
, 1);
1776 printk(KERN_INFO
"TOMOYO: 2.2.0 2009/04/01\n");
1777 printk(KERN_INFO
"Mandatory Access Control activated.\n");
1778 tomoyo_policy_loaded
= true;
1779 { /* Check all profiles currently assigned to domains are defined. */
1780 struct tomoyo_domain_info
*domain
;
1781 down_read(&tomoyo_domain_list_lock
);
1782 list_for_each_entry(domain
, &tomoyo_domain_list
, list
) {
1783 const u8 profile
= domain
->profile
;
1784 if (tomoyo_profile_ptr
[profile
])
1786 panic("Profile %u (used by '%s') not defined.\n",
1787 profile
, domain
->domainname
->name
);
1789 up_read(&tomoyo_domain_list_lock
);
1794 * tomoyo_read_version: Get version.
1796 * @head: Pointer to "struct tomoyo_io_buffer".
1798 * Returns version information.
1800 static int tomoyo_read_version(struct tomoyo_io_buffer
*head
)
1802 if (!head
->read_eof
) {
1803 tomoyo_io_printf(head
, "2.2.0");
1804 head
->read_eof
= true;
1810 * tomoyo_read_self_domain - Get the current process's domainname.
1812 * @head: Pointer to "struct tomoyo_io_buffer".
1814 * Returns the current process's domainname.
1816 static int tomoyo_read_self_domain(struct tomoyo_io_buffer
*head
)
1818 if (!head
->read_eof
) {
1820 * tomoyo_domain()->domainname != NULL
1821 * because every process belongs to a domain and
1822 * the domain's name cannot be NULL.
1824 tomoyo_io_printf(head
, "%s", tomoyo_domain()->domainname
->name
);
1825 head
->read_eof
= true;
1831 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1833 * @type: Type of interface.
1834 * @file: Pointer to "struct file".
1836 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1838 static int tomoyo_open_control(const u8 type
, struct file
*file
)
1840 struct tomoyo_io_buffer
*head
= tomoyo_alloc(sizeof(*head
));
1844 mutex_init(&head
->io_sem
);
1846 case TOMOYO_DOMAINPOLICY
:
1847 /* /sys/kernel/security/tomoyo/domain_policy */
1848 head
->write
= tomoyo_write_domain_policy
;
1849 head
->read
= tomoyo_read_domain_policy
;
1851 case TOMOYO_EXCEPTIONPOLICY
:
1852 /* /sys/kernel/security/tomoyo/exception_policy */
1853 head
->write
= tomoyo_write_exception_policy
;
1854 head
->read
= tomoyo_read_exception_policy
;
1856 case TOMOYO_SELFDOMAIN
:
1857 /* /sys/kernel/security/tomoyo/self_domain */
1858 head
->read
= tomoyo_read_self_domain
;
1860 case TOMOYO_DOMAIN_STATUS
:
1861 /* /sys/kernel/security/tomoyo/.domain_status */
1862 head
->write
= tomoyo_write_domain_profile
;
1863 head
->read
= tomoyo_read_domain_profile
;
1865 case TOMOYO_PROCESS_STATUS
:
1866 /* /sys/kernel/security/tomoyo/.process_status */
1867 head
->write
= tomoyo_write_pid
;
1868 head
->read
= tomoyo_read_pid
;
1870 case TOMOYO_VERSION
:
1871 /* /sys/kernel/security/tomoyo/version */
1872 head
->read
= tomoyo_read_version
;
1873 head
->readbuf_size
= 128;
1875 case TOMOYO_MEMINFO
:
1876 /* /sys/kernel/security/tomoyo/meminfo */
1877 head
->write
= tomoyo_write_memory_quota
;
1878 head
->read
= tomoyo_read_memory_counter
;
1879 head
->readbuf_size
= 512;
1881 case TOMOYO_PROFILE
:
1882 /* /sys/kernel/security/tomoyo/profile */
1883 head
->write
= tomoyo_write_profile
;
1884 head
->read
= tomoyo_read_profile
;
1886 case TOMOYO_MANAGER
:
1887 /* /sys/kernel/security/tomoyo/manager */
1888 head
->write
= tomoyo_write_manager_policy
;
1889 head
->read
= tomoyo_read_manager_policy
;
1892 if (!(file
->f_mode
& FMODE_READ
)) {
1894 * No need to allocate read_buf since it is not opened
1899 if (!head
->readbuf_size
)
1900 head
->readbuf_size
= 4096 * 2;
1901 head
->read_buf
= tomoyo_alloc(head
->readbuf_size
);
1902 if (!head
->read_buf
) {
1907 if (!(file
->f_mode
& FMODE_WRITE
)) {
1909 * No need to allocate write_buf since it is not opened
1913 } else if (head
->write
) {
1914 head
->writebuf_size
= 4096 * 2;
1915 head
->write_buf
= tomoyo_alloc(head
->writebuf_size
);
1916 if (!head
->write_buf
) {
1917 tomoyo_free(head
->read_buf
);
1922 file
->private_data
= head
;
1924 * Call the handler now if the file is
1925 * /sys/kernel/security/tomoyo/self_domain
1926 * so that the user can use
1927 * cat < /sys/kernel/security/tomoyo/self_domain"
1928 * to know the current process's domainname.
1930 if (type
== TOMOYO_SELFDOMAIN
)
1931 tomoyo_read_control(file
, NULL
, 0);
1936 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
1938 * @file: Pointer to "struct file".
1939 * @buffer: Poiner to buffer to write to.
1940 * @buffer_len: Size of @buffer.
1942 * Returns bytes read on success, negative value otherwise.
1944 static int tomoyo_read_control(struct file
*file
, char __user
*buffer
,
1945 const int buffer_len
)
1948 struct tomoyo_io_buffer
*head
= file
->private_data
;
1953 if (mutex_lock_interruptible(&head
->io_sem
))
1955 /* Call the policy handler. */
1956 len
= head
->read(head
);
1959 /* Write to buffer. */
1960 len
= head
->read_avail
;
1961 if (len
> buffer_len
)
1965 /* head->read_buf changes by some functions. */
1966 cp
= head
->read_buf
;
1967 if (copy_to_user(buffer
, cp
, len
)) {
1971 head
->read_avail
-= len
;
1972 memmove(cp
, cp
+ len
, head
->read_avail
);
1974 mutex_unlock(&head
->io_sem
);
1979 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
1981 * @file: Pointer to "struct file".
1982 * @buffer: Pointer to buffer to read from.
1983 * @buffer_len: Size of @buffer.
1985 * Returns @buffer_len on success, negative value otherwise.
1987 static int tomoyo_write_control(struct file
*file
, const char __user
*buffer
,
1988 const int buffer_len
)
1990 struct tomoyo_io_buffer
*head
= file
->private_data
;
1991 int error
= buffer_len
;
1992 int avail_len
= buffer_len
;
1993 char *cp0
= head
->write_buf
;
1997 if (!access_ok(VERIFY_READ
, buffer
, buffer_len
))
1999 /* Don't allow updating policies by non manager programs. */
2000 if (head
->write
!= tomoyo_write_pid
&&
2001 head
->write
!= tomoyo_write_domain_policy
&&
2002 !tomoyo_is_policy_manager())
2004 if (mutex_lock_interruptible(&head
->io_sem
))
2006 /* Read a line and dispatch it to the policy handler. */
2007 while (avail_len
> 0) {
2009 if (head
->write_avail
>= head
->writebuf_size
- 1) {
2012 } else if (get_user(c
, buffer
)) {
2018 cp0
[head
->write_avail
++] = c
;
2021 cp0
[head
->write_avail
- 1] = '\0';
2022 head
->write_avail
= 0;
2023 tomoyo_normalize_line(cp0
);
2026 mutex_unlock(&head
->io_sem
);
2031 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2033 * @file: Pointer to "struct file".
2035 * Releases memory and returns 0.
2037 static int tomoyo_close_control(struct file
*file
)
2039 struct tomoyo_io_buffer
*head
= file
->private_data
;
2041 /* Release memory used for policy I/O. */
2042 tomoyo_free(head
->read_buf
);
2043 head
->read_buf
= NULL
;
2044 tomoyo_free(head
->write_buf
);
2045 head
->write_buf
= NULL
;
2048 file
->private_data
= NULL
;
2053 * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
2055 * @acl_type: Type of ACL entry.
2057 * Returns pointer to the ACL entry on success, NULL otherwise.
2059 void *tomoyo_alloc_acl_element(const u8 acl_type
)
2062 struct tomoyo_acl_info
*ptr
;
2065 case TOMOYO_TYPE_SINGLE_PATH_ACL
:
2066 len
= sizeof(struct tomoyo_single_path_acl_record
);
2068 case TOMOYO_TYPE_DOUBLE_PATH_ACL
:
2069 len
= sizeof(struct tomoyo_double_path_acl_record
);
2074 ptr
= tomoyo_alloc_element(len
);
2077 ptr
->type
= acl_type
;
2082 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2084 * @inode: Pointer to "struct inode".
2085 * @file: Pointer to "struct file".
2087 * Returns 0 on success, negative value otherwise.
2089 static int tomoyo_open(struct inode
*inode
, struct file
*file
)
2091 const int key
= ((u8
*) file
->f_path
.dentry
->d_inode
->i_private
)
2093 return tomoyo_open_control(key
, file
);
2097 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2099 * @inode: Pointer to "struct inode".
2100 * @file: Pointer to "struct file".
2102 * Returns 0 on success, negative value otherwise.
2104 static int tomoyo_release(struct inode
*inode
, struct file
*file
)
2106 return tomoyo_close_control(file
);
2110 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2112 * @file: Pointer to "struct file".
2113 * @buf: Pointer to buffer.
2114 * @count: Size of @buf.
2117 * Returns bytes read on success, negative value otherwise.
2119 static ssize_t
tomoyo_read(struct file
*file
, char __user
*buf
, size_t count
,
2122 return tomoyo_read_control(file
, buf
, count
);
2126 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2128 * @file: Pointer to "struct file".
2129 * @buf: Pointer to buffer.
2130 * @count: Size of @buf.
2133 * Returns @count on success, negative value otherwise.
2135 static ssize_t
tomoyo_write(struct file
*file
, const char __user
*buf
,
2136 size_t count
, loff_t
*ppos
)
2138 return tomoyo_write_control(file
, buf
, count
);
2141 /* Operations for /sys/kernel/security/tomoyo/ interface. */
2142 static const struct file_operations tomoyo_operations
= {
2143 .open
= tomoyo_open
,
2144 .release
= tomoyo_release
,
2145 .read
= tomoyo_read
,
2146 .write
= tomoyo_write
,
2150 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2152 * @name: The name of the interface file.
2153 * @mode: The permission of the interface file.
2154 * @parent: The parent directory.
2155 * @key: Type of interface.
2159 static void __init
tomoyo_create_entry(const char *name
, const mode_t mode
,
2160 struct dentry
*parent
, const u8 key
)
2162 securityfs_create_file(name
, mode
, parent
, ((u8
*) NULL
) + key
,
2163 &tomoyo_operations
);
2167 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2171 static int __init
tomoyo_initerface_init(void)
2173 struct dentry
*tomoyo_dir
;
2175 /* Don't create securityfs entries unless registered. */
2176 if (current_cred()->security
!= &tomoyo_kernel_domain
)
2179 tomoyo_dir
= securityfs_create_dir("tomoyo", NULL
);
2180 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir
,
2181 TOMOYO_DOMAINPOLICY
);
2182 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir
,
2183 TOMOYO_EXCEPTIONPOLICY
);
2184 tomoyo_create_entry("self_domain", 0400, tomoyo_dir
,
2186 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir
,
2187 TOMOYO_DOMAIN_STATUS
);
2188 tomoyo_create_entry(".process_status", 0600, tomoyo_dir
,
2189 TOMOYO_PROCESS_STATUS
);
2190 tomoyo_create_entry("meminfo", 0600, tomoyo_dir
,
2192 tomoyo_create_entry("profile", 0600, tomoyo_dir
,
2194 tomoyo_create_entry("manager", 0600, tomoyo_dir
,
2196 tomoyo_create_entry("version", 0400, tomoyo_dir
,
2201 fs_initcall(tomoyo_initerface_init
);