2 * security/tomoyo/util.c
4 * Utility functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/slab.h>
12 /* Lock for protecting policy. */
13 DEFINE_MUTEX(tomoyo_policy_lock
);
15 /* Has /sbin/init started? */
16 bool tomoyo_policy_loaded
;
19 * tomoyo_permstr - Find permission keywords.
21 * @string: String representation for permissions in foo/bar/buz format.
22 * @keyword: Keyword to find from @string/
24 * Returns ture if @keyword was found in @string, false otherwise.
26 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
28 bool tomoyo_permstr(const char *string
, const char *keyword
)
30 const char *cp
= strstr(string
, keyword
);
32 return cp
== string
|| *(cp
- 1) == '/';
37 * tomoyo_read_token - Read a word from a line.
39 * @param: Pointer to "struct tomoyo_acl_param".
41 * Returns a word on success, "" otherwise.
43 * To allow the caller to skip NULL check, this function returns "" rather than
44 * NULL if there is no more words to read.
46 char *tomoyo_read_token(struct tomoyo_acl_param
*param
)
48 char *pos
= param
->data
;
49 char *del
= strchr(pos
, ' ');
53 del
= pos
+ strlen(pos
);
59 * tomoyo_parse_ulong - Parse an "unsigned long" value.
61 * @result: Pointer to "unsigned long".
62 * @str: Pointer to string to parse.
64 * Returns one of values in "enum tomoyo_value_type".
66 * The @src is updated to point the first character after the value
69 static u8
tomoyo_parse_ulong(unsigned long *result
, char **str
)
71 const char *cp
= *str
;
76 if (c
== 'x' || c
== 'X') {
79 } else if (c
>= '0' && c
<= '7') {
84 *result
= simple_strtoul(cp
, &ep
, base
);
86 return TOMOYO_VALUE_TYPE_INVALID
;
90 return TOMOYO_VALUE_TYPE_HEXADECIMAL
;
92 return TOMOYO_VALUE_TYPE_OCTAL
;
94 return TOMOYO_VALUE_TYPE_DECIMAL
;
99 * tomoyo_print_ulong - Print an "unsigned long" value.
101 * @buffer: Pointer to buffer.
102 * @buffer_len: Size of @buffer.
103 * @value: An "unsigned long" value.
104 * @type: Type of @value.
108 void tomoyo_print_ulong(char *buffer
, const int buffer_len
,
109 const unsigned long value
, const u8 type
)
111 if (type
== TOMOYO_VALUE_TYPE_DECIMAL
)
112 snprintf(buffer
, buffer_len
, "%lu", value
);
113 else if (type
== TOMOYO_VALUE_TYPE_OCTAL
)
114 snprintf(buffer
, buffer_len
, "0%lo", value
);
115 else if (type
== TOMOYO_VALUE_TYPE_HEXADECIMAL
)
116 snprintf(buffer
, buffer_len
, "0x%lX", value
);
118 snprintf(buffer
, buffer_len
, "type(%u)", type
);
122 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
124 * @param: Pointer to "struct tomoyo_acl_param".
125 * @ptr: Pointer to "struct tomoyo_name_union".
127 * Returns true on success, false otherwise.
129 bool tomoyo_parse_name_union(struct tomoyo_acl_param
*param
,
130 struct tomoyo_name_union
*ptr
)
133 if (param
->data
[0] == '@') {
135 ptr
->group
= tomoyo_get_group(param
, TOMOYO_PATH_GROUP
);
136 return ptr
->group
!= NULL
;
138 filename
= tomoyo_read_token(param
);
139 if (!tomoyo_correct_word(filename
))
141 ptr
->filename
= tomoyo_get_name(filename
);
142 return ptr
->filename
!= NULL
;
146 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
148 * @param: Pointer to "struct tomoyo_acl_param".
149 * @ptr: Pointer to "struct tomoyo_number_union".
151 * Returns true on success, false otherwise.
153 bool tomoyo_parse_number_union(struct tomoyo_acl_param
*param
,
154 struct tomoyo_number_union
*ptr
)
159 memset(ptr
, 0, sizeof(*ptr
));
160 if (param
->data
[0] == '@') {
162 ptr
->group
= tomoyo_get_group(param
, TOMOYO_NUMBER_GROUP
);
163 return ptr
->group
!= NULL
;
165 data
= tomoyo_read_token(param
);
166 type
= tomoyo_parse_ulong(&v
, &data
);
167 if (type
== TOMOYO_VALUE_TYPE_INVALID
)
170 ptr
->value_type
[0] = type
;
173 ptr
->value_type
[1] = type
;
178 type
= tomoyo_parse_ulong(&v
, &data
);
179 if (type
== TOMOYO_VALUE_TYPE_INVALID
|| *data
|| ptr
->values
[0] > v
)
182 ptr
->value_type
[1] = type
;
187 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
189 * @str: Pointer to the string.
191 * Returns true if @str is a \ooo style octal value, false otherwise.
193 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
194 * This function verifies that \ooo is in valid range.
196 static inline bool tomoyo_byte_range(const char *str
)
198 return *str
>= '0' && *str
++ <= '3' &&
199 *str
>= '0' && *str
++ <= '7' &&
200 *str
>= '0' && *str
<= '7';
204 * tomoyo_alphabet_char - Check whether the character is an alphabet.
206 * @c: The character to check.
208 * Returns true if @c is an alphabet character, false otherwise.
210 static inline bool tomoyo_alphabet_char(const char c
)
212 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
216 * tomoyo_make_byte - Make byte value from three octal characters.
218 * @c1: The first character.
219 * @c2: The second character.
220 * @c3: The third character.
222 * Returns byte value.
224 static inline u8
tomoyo_make_byte(const u8 c1
, const u8 c2
, const u8 c3
)
226 return ((c1
- '0') << 6) + ((c2
- '0') << 3) + (c3
- '0');
230 * tomoyo_valid - Check whether the character is a valid char.
232 * @c: The character to check.
234 * Returns true if @c is a valid character, false otherwise.
236 static inline bool tomoyo_valid(const unsigned char c
)
238 return c
> ' ' && c
< 127;
242 * tomoyo_invalid - Check whether the character is an invalid char.
244 * @c: The character to check.
246 * Returns true if @c is an invalid character, false otherwise.
248 static inline bool tomoyo_invalid(const unsigned char c
)
250 return c
&& (c
<= ' ' || c
>= 127);
254 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
256 * @src: Pointer to pointer to the string.
257 * @find: Pointer to the keyword.
259 * Returns true if @src starts with @find, false otherwise.
261 * The @src is updated to point the first character after the @find
262 * if @src starts with @find.
264 bool tomoyo_str_starts(char **src
, const char *find
)
266 const int len
= strlen(find
);
269 if (strncmp(tmp
, find
, len
))
277 * tomoyo_normalize_line - Format string.
279 * @buffer: The line to normalize.
281 * Leading and trailing whitespaces are removed.
282 * Multiple whitespaces are packed into single space.
286 void tomoyo_normalize_line(unsigned char *buffer
)
288 unsigned char *sp
= buffer
;
289 unsigned char *dp
= buffer
;
292 while (tomoyo_invalid(*sp
))
298 while (tomoyo_valid(*sp
))
300 while (tomoyo_invalid(*sp
))
307 * tomoyo_correct_word2 - Validate a string.
309 * @string: The string to check. May be non-'\0'-terminated.
310 * @len: Length of @string.
312 * Check whether the given string follows the naming rules.
313 * Returns true if @string follows the naming rules, false otherwise.
315 static bool tomoyo_correct_word2(const char *string
, size_t len
)
317 const char *const start
= string
;
318 bool in_repetition
= false;
331 case '\\': /* "\\" */
344 case '{': /* "/\{" */
345 if (string
- 3 < start
|| *(string
- 3) != '/')
347 in_repetition
= true;
349 case '}': /* "\}/" */
354 in_repetition
= false;
356 case '0': /* "\ooo" */
360 if (!len
-- || !len
--)
364 if (d
< '0' || d
> '7' || e
< '0' || e
> '7')
366 c
= tomoyo_make_byte(c
, d
, e
);
367 if (tomoyo_invalid(c
))
368 continue; /* pattern is not \000 */
371 } else if (in_repetition
&& c
== '/') {
373 } else if (tomoyo_invalid(c
)) {
385 * tomoyo_correct_word - Validate a string.
387 * @string: The string to check.
389 * Check whether the given string follows the naming rules.
390 * Returns true if @string follows the naming rules, false otherwise.
392 bool tomoyo_correct_word(const char *string
)
394 return tomoyo_correct_word2(string
, strlen(string
));
398 * tomoyo_correct_path - Validate a pathname.
400 * @filename: The pathname to check.
402 * Check whether the given pathname follows the naming rules.
403 * Returns true if @filename follows the naming rules, false otherwise.
405 bool tomoyo_correct_path(const char *filename
)
407 return *filename
== '/' && tomoyo_correct_word(filename
);
411 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
413 * @domainname: The domainname to check.
415 * Returns true if @domainname follows the naming rules, false otherwise.
417 bool tomoyo_correct_domain(const unsigned char *domainname
)
419 if (!domainname
|| strncmp(domainname
, TOMOYO_ROOT_NAME
,
420 TOMOYO_ROOT_NAME_LEN
))
422 domainname
+= TOMOYO_ROOT_NAME_LEN
;
425 if (*domainname
++ != ' ')
428 const unsigned char *cp
= strchr(domainname
, ' ');
431 if (*domainname
!= '/' ||
432 !tomoyo_correct_word2(domainname
, cp
- domainname
))
436 return tomoyo_correct_path(domainname
);
442 * tomoyo_domain_def - Check whether the given token can be a domainname.
444 * @buffer: The token to check.
446 * Returns true if @buffer possibly be a domainname, false otherwise.
448 bool tomoyo_domain_def(const unsigned char *buffer
)
450 return !strncmp(buffer
, TOMOYO_ROOT_NAME
, TOMOYO_ROOT_NAME_LEN
);
454 * tomoyo_find_domain - Find a domain by the given name.
456 * @domainname: The domainname to find.
458 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
460 * Caller holds tomoyo_read_lock().
462 struct tomoyo_domain_info
*tomoyo_find_domain(const char *domainname
)
464 struct tomoyo_domain_info
*domain
;
465 struct tomoyo_path_info name
;
467 name
.name
= domainname
;
468 tomoyo_fill_path_info(&name
);
469 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
470 if (!domain
->is_deleted
&&
471 !tomoyo_pathcmp(&name
, domain
->domainname
))
478 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
480 * @filename: The string to evaluate.
482 * Returns the initial length without a pattern in @filename.
484 static int tomoyo_const_part_length(const char *filename
)
491 while ((c
= *filename
++) != '\0') {
498 case '\\': /* "\\" */
501 case '0': /* "\ooo" */
506 if (c
< '0' || c
> '7')
509 if (c
< '0' || c
> '7')
520 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
522 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
524 * The caller sets "struct tomoyo_path_info"->name.
526 void tomoyo_fill_path_info(struct tomoyo_path_info
*ptr
)
528 const char *name
= ptr
->name
;
529 const int len
= strlen(name
);
531 ptr
->const_len
= tomoyo_const_part_length(name
);
532 ptr
->is_dir
= len
&& (name
[len
- 1] == '/');
533 ptr
->is_patterned
= (ptr
->const_len
< len
);
534 ptr
->hash
= full_name_hash(name
, len
);
538 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
540 * @filename: The start of string to check.
541 * @filename_end: The end of string to check.
542 * @pattern: The start of pattern to compare.
543 * @pattern_end: The end of pattern to compare.
545 * Returns true if @filename matches @pattern, false otherwise.
547 static bool tomoyo_file_matches_pattern2(const char *filename
,
548 const char *filename_end
,
550 const char *pattern_end
)
552 while (filename
< filename_end
&& pattern
< pattern_end
) {
554 if (*pattern
!= '\\') {
555 if (*filename
++ != *pattern
++)
567 } else if (c
== '\\') {
568 if (filename
[1] == '\\')
570 else if (tomoyo_byte_range(filename
+ 1))
579 if (*++filename
!= '\\')
591 if (!tomoyo_alphabet_char(c
))
598 if (c
== '\\' && tomoyo_byte_range(filename
+ 1)
599 && strncmp(filename
+ 1, pattern
, 3) == 0) {
604 return false; /* Not matched. */
607 for (i
= 0; i
<= filename_end
- filename
; i
++) {
608 if (tomoyo_file_matches_pattern2(
609 filename
+ i
, filename_end
,
610 pattern
+ 1, pattern_end
))
613 if (c
== '.' && *pattern
== '@')
617 if (filename
[i
+ 1] == '\\')
619 else if (tomoyo_byte_range(filename
+ i
+ 1))
622 break; /* Bad pattern. */
624 return false; /* Not matched. */
629 while (isdigit(filename
[j
]))
631 } else if (c
== 'X') {
632 while (isxdigit(filename
[j
]))
634 } else if (c
== 'A') {
635 while (tomoyo_alphabet_char(filename
[j
]))
638 for (i
= 1; i
<= j
; i
++) {
639 if (tomoyo_file_matches_pattern2(
640 filename
+ i
, filename_end
,
641 pattern
+ 1, pattern_end
))
644 return false; /* Not matched or bad pattern. */
649 while (*pattern
== '\\' &&
650 (*(pattern
+ 1) == '*' || *(pattern
+ 1) == '@'))
652 return filename
== filename_end
&& pattern
== pattern_end
;
656 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
658 * @filename: The start of string to check.
659 * @filename_end: The end of string to check.
660 * @pattern: The start of pattern to compare.
661 * @pattern_end: The end of pattern to compare.
663 * Returns true if @filename matches @pattern, false otherwise.
665 static bool tomoyo_file_matches_pattern(const char *filename
,
666 const char *filename_end
,
668 const char *pattern_end
)
670 const char *pattern_start
= pattern
;
674 while (pattern
< pattern_end
- 1) {
675 /* Split at "\-" pattern. */
676 if (*pattern
++ != '\\' || *pattern
++ != '-')
678 result
= tomoyo_file_matches_pattern2(filename
,
687 pattern_start
= pattern
;
689 result
= tomoyo_file_matches_pattern2(filename
, filename_end
,
690 pattern_start
, pattern_end
);
691 return first
? result
: !result
;
695 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
697 * @f: The start of string to check.
698 * @p: The start of pattern to compare.
700 * Returns true if @f matches @p, false otherwise.
702 static bool tomoyo_path_matches_pattern2(const char *f
, const char *p
)
704 const char *f_delimiter
;
705 const char *p_delimiter
;
708 f_delimiter
= strchr(f
, '/');
710 f_delimiter
= f
+ strlen(f
);
711 p_delimiter
= strchr(p
, '/');
713 p_delimiter
= p
+ strlen(p
);
714 if (*p
== '\\' && *(p
+ 1) == '{')
716 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
,
726 /* Ignore trailing "\*" and "\@" in @pattern. */
728 (*(p
+ 1) == '*' || *(p
+ 1) == '@'))
733 * The "\{" pattern is permitted only after '/' character.
734 * This guarantees that below "*(p - 1)" is safe.
735 * Also, the "\}" pattern is permitted only before '/' character
736 * so that "\{" + "\}" pair will not break the "\-" operator.
738 if (*(p
- 1) != '/' || p_delimiter
<= p
+ 3 || *p_delimiter
!= '/' ||
739 *(p_delimiter
- 1) != '}' || *(p_delimiter
- 2) != '\\')
740 return false; /* Bad pattern. */
742 /* Compare current component with pattern. */
743 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
+ 2,
746 /* Proceed to next component. */
751 /* Continue comparison. */
752 if (tomoyo_path_matches_pattern2(f
, p_delimiter
+ 1))
754 f_delimiter
= strchr(f
, '/');
755 } while (f_delimiter
);
756 return false; /* Not matched. */
760 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
762 * @filename: The filename to check.
763 * @pattern: The pattern to compare.
765 * Returns true if matches, false otherwise.
767 * The following patterns are available.
769 * \ooo Octal representation of a byte.
770 * \* Zero or more repetitions of characters other than '/'.
771 * \@ Zero or more repetitions of characters other than '/' or '.'.
772 * \? 1 byte character other than '/'.
773 * \$ One or more repetitions of decimal digits.
774 * \+ 1 decimal digit.
775 * \X One or more repetitions of hexadecimal digits.
776 * \x 1 hexadecimal digit.
777 * \A One or more repetitions of alphabet characters.
778 * \a 1 alphabet character.
780 * \- Subtraction operator.
782 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
785 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info
*filename
,
786 const struct tomoyo_path_info
*pattern
)
788 const char *f
= filename
->name
;
789 const char *p
= pattern
->name
;
790 const int len
= pattern
->const_len
;
792 /* If @pattern doesn't contain pattern, I can use strcmp(). */
793 if (!pattern
->is_patterned
)
794 return !tomoyo_pathcmp(filename
, pattern
);
795 /* Don't compare directory and non-directory. */
796 if (filename
->is_dir
!= pattern
->is_dir
)
798 /* Compare the initial length without patterns. */
799 if (strncmp(f
, p
, len
))
803 return tomoyo_path_matches_pattern2(f
, p
);
807 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
809 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
811 * This function uses kzalloc(), so the caller must call kfree()
812 * if this function didn't return NULL.
814 const char *tomoyo_get_exe(void)
816 struct mm_struct
*mm
= current
->mm
;
817 struct vm_area_struct
*vma
;
818 const char *cp
= NULL
;
822 down_read(&mm
->mmap_sem
);
823 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
824 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
) {
825 cp
= tomoyo_realpath_from_path(&vma
->vm_file
->f_path
);
829 up_read(&mm
->mmap_sem
);
834 * tomoyo_get_mode - Get MAC mode.
836 * @profile: Profile number.
837 * @index: Index number of functionality.
841 int tomoyo_get_mode(const u8 profile
, const u8 index
)
844 const u8 category
= TOMOYO_MAC_CATEGORY_FILE
;
845 if (!tomoyo_policy_loaded
)
846 return TOMOYO_CONFIG_DISABLED
;
847 mode
= tomoyo_profile(profile
)->config
[index
];
848 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
849 mode
= tomoyo_profile(profile
)->config
[category
];
850 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
851 mode
= tomoyo_profile(profile
)->default_config
;
856 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
858 * @r: Pointer to "struct tomoyo_request_info" to initialize.
859 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
860 * @index: Index number of functionality.
864 int tomoyo_init_request_info(struct tomoyo_request_info
*r
,
865 struct tomoyo_domain_info
*domain
, const u8 index
)
868 memset(r
, 0, sizeof(*r
));
870 domain
= tomoyo_domain();
872 profile
= domain
->profile
;
873 r
->profile
= profile
;
875 r
->mode
= tomoyo_get_mode(profile
, index
);
880 * tomoyo_last_word - Get last component of a line.
884 * Returns the last word of a line.
886 const char *tomoyo_last_word(const char *name
)
888 const char *cp
= strrchr(name
, ' ');
895 * tomoyo_warn_log - Print warning or error message on console.
897 * @r: Pointer to "struct tomoyo_request_info".
898 * @fmt: The printf()'s format string, followed by parameters.
900 void tomoyo_warn_log(struct tomoyo_request_info
*r
, const char *fmt
, ...)
904 const struct tomoyo_domain_info
* const domain
= r
->domain
;
905 const struct tomoyo_profile
*profile
= tomoyo_profile(domain
->profile
);
907 case TOMOYO_CONFIG_ENFORCING
:
908 if (!profile
->enforcing
->enforcing_verbose
)
911 case TOMOYO_CONFIG_PERMISSIVE
:
912 if (!profile
->permissive
->permissive_verbose
)
915 case TOMOYO_CONFIG_LEARNING
:
916 if (!profile
->learning
->learning_verbose
)
920 buffer
= kmalloc(4096, GFP_NOFS
);
924 vsnprintf(buffer
, 4095, fmt
, args
);
927 printk(KERN_WARNING
"%s: Access %s denied for %s\n",
928 r
->mode
== TOMOYO_CONFIG_ENFORCING
? "ERROR" : "WARNING", buffer
,
929 tomoyo_last_word(domain
->domainname
->name
));
934 * tomoyo_domain_quota_is_ok - Check for domain's quota.
936 * @r: Pointer to "struct tomoyo_request_info".
938 * Returns true if the domain is not exceeded quota, false otherwise.
940 * Caller holds tomoyo_read_lock().
942 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info
*r
)
944 unsigned int count
= 0;
945 struct tomoyo_domain_info
*domain
= r
->domain
;
946 struct tomoyo_acl_info
*ptr
;
948 if (r
->mode
!= TOMOYO_CONFIG_LEARNING
)
952 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
958 case TOMOYO_TYPE_PATH_ACL
:
959 perm
= container_of(ptr
, struct tomoyo_path_acl
, head
)
962 case TOMOYO_TYPE_PATH2_ACL
:
963 perm
= container_of(ptr
, struct tomoyo_path2_acl
, head
)
966 case TOMOYO_TYPE_PATH_NUMBER_ACL
:
967 perm
= container_of(ptr
, struct tomoyo_path_number_acl
,
970 case TOMOYO_TYPE_MKDEV_ACL
:
971 perm
= container_of(ptr
, struct tomoyo_mkdev_acl
,
977 for (i
= 0; i
< 16; i
++)
981 if (count
< tomoyo_profile(domain
->profile
)->learning
->
984 if (!domain
->quota_warned
) {
985 domain
->quota_warned
= true;
986 printk(KERN_WARNING
"TOMOYO-WARNING: "
987 "Domain '%s' has so many ACLs to hold. "
988 "Stopped learning mode.\n", domain
->domainname
->name
);