2 * security/tomoyo/util.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/slab.h>
10 /* Lock for protecting policy. */
11 DEFINE_MUTEX(tomoyo_policy_lock
);
13 /* Has /sbin/init started? */
14 bool tomoyo_policy_loaded
;
17 * Mapping table from "enum tomoyo_mac_index" to
18 * "enum tomoyo_mac_category_index".
20 const u8 tomoyo_index2category
[TOMOYO_MAX_MAC_INDEX
] = {
21 /* CONFIG::file group */
22 [TOMOYO_MAC_FILE_EXECUTE
] = TOMOYO_MAC_CATEGORY_FILE
,
23 [TOMOYO_MAC_FILE_OPEN
] = TOMOYO_MAC_CATEGORY_FILE
,
24 [TOMOYO_MAC_FILE_CREATE
] = TOMOYO_MAC_CATEGORY_FILE
,
25 [TOMOYO_MAC_FILE_UNLINK
] = TOMOYO_MAC_CATEGORY_FILE
,
26 [TOMOYO_MAC_FILE_GETATTR
] = TOMOYO_MAC_CATEGORY_FILE
,
27 [TOMOYO_MAC_FILE_MKDIR
] = TOMOYO_MAC_CATEGORY_FILE
,
28 [TOMOYO_MAC_FILE_RMDIR
] = TOMOYO_MAC_CATEGORY_FILE
,
29 [TOMOYO_MAC_FILE_MKFIFO
] = TOMOYO_MAC_CATEGORY_FILE
,
30 [TOMOYO_MAC_FILE_MKSOCK
] = TOMOYO_MAC_CATEGORY_FILE
,
31 [TOMOYO_MAC_FILE_TRUNCATE
] = TOMOYO_MAC_CATEGORY_FILE
,
32 [TOMOYO_MAC_FILE_SYMLINK
] = TOMOYO_MAC_CATEGORY_FILE
,
33 [TOMOYO_MAC_FILE_MKBLOCK
] = TOMOYO_MAC_CATEGORY_FILE
,
34 [TOMOYO_MAC_FILE_MKCHAR
] = TOMOYO_MAC_CATEGORY_FILE
,
35 [TOMOYO_MAC_FILE_LINK
] = TOMOYO_MAC_CATEGORY_FILE
,
36 [TOMOYO_MAC_FILE_RENAME
] = TOMOYO_MAC_CATEGORY_FILE
,
37 [TOMOYO_MAC_FILE_CHMOD
] = TOMOYO_MAC_CATEGORY_FILE
,
38 [TOMOYO_MAC_FILE_CHOWN
] = TOMOYO_MAC_CATEGORY_FILE
,
39 [TOMOYO_MAC_FILE_CHGRP
] = TOMOYO_MAC_CATEGORY_FILE
,
40 [TOMOYO_MAC_FILE_IOCTL
] = TOMOYO_MAC_CATEGORY_FILE
,
41 [TOMOYO_MAC_FILE_CHROOT
] = TOMOYO_MAC_CATEGORY_FILE
,
42 [TOMOYO_MAC_FILE_MOUNT
] = TOMOYO_MAC_CATEGORY_FILE
,
43 [TOMOYO_MAC_FILE_UMOUNT
] = TOMOYO_MAC_CATEGORY_FILE
,
44 [TOMOYO_MAC_FILE_PIVOT_ROOT
] = TOMOYO_MAC_CATEGORY_FILE
,
45 /* CONFIG::network group */
46 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND
] =
47 TOMOYO_MAC_CATEGORY_NETWORK
,
48 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN
] =
49 TOMOYO_MAC_CATEGORY_NETWORK
,
50 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT
] =
51 TOMOYO_MAC_CATEGORY_NETWORK
,
52 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND
] =
53 TOMOYO_MAC_CATEGORY_NETWORK
,
54 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND
] =
55 TOMOYO_MAC_CATEGORY_NETWORK
,
56 [TOMOYO_MAC_NETWORK_INET_RAW_BIND
] =
57 TOMOYO_MAC_CATEGORY_NETWORK
,
58 [TOMOYO_MAC_NETWORK_INET_RAW_SEND
] =
59 TOMOYO_MAC_CATEGORY_NETWORK
,
60 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND
] =
61 TOMOYO_MAC_CATEGORY_NETWORK
,
62 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN
] =
63 TOMOYO_MAC_CATEGORY_NETWORK
,
64 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT
] =
65 TOMOYO_MAC_CATEGORY_NETWORK
,
66 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND
] =
67 TOMOYO_MAC_CATEGORY_NETWORK
,
68 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND
] =
69 TOMOYO_MAC_CATEGORY_NETWORK
,
70 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND
] =
71 TOMOYO_MAC_CATEGORY_NETWORK
,
72 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN
] =
73 TOMOYO_MAC_CATEGORY_NETWORK
,
74 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT
] =
75 TOMOYO_MAC_CATEGORY_NETWORK
,
76 /* CONFIG::misc group */
77 [TOMOYO_MAC_ENVIRON
] = TOMOYO_MAC_CATEGORY_MISC
,
81 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
83 * @time: Seconds since 1970/01/01 00:00:00.
84 * @stamp: Pointer to "struct tomoyo_time".
88 * This function does not handle Y2038 problem.
90 void tomoyo_convert_time(time_t time
, struct tomoyo_time
*stamp
)
92 static const u16 tomoyo_eom
[2][12] = {
93 { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
94 { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
99 stamp
->sec
= time
% 60;
101 stamp
->min
= time
% 60;
103 stamp
->hour
= time
% 24;
105 for (y
= 1970; ; y
++) {
106 const unsigned short days
= (y
& 3) ? 365 : 366;
112 for (m
= 0; m
< 11 && time
>= tomoyo_eom
[r
][m
]; m
++)
115 time
-= tomoyo_eom
[r
][m
- 1];
122 * tomoyo_permstr - Find permission keywords.
124 * @string: String representation for permissions in foo/bar/buz format.
125 * @keyword: Keyword to find from @string/
127 * Returns ture if @keyword was found in @string, false otherwise.
129 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
131 bool tomoyo_permstr(const char *string
, const char *keyword
)
133 const char *cp
= strstr(string
, keyword
);
135 return cp
== string
|| *(cp
- 1) == '/';
140 * tomoyo_read_token - Read a word from a line.
142 * @param: Pointer to "struct tomoyo_acl_param".
144 * Returns a word on success, "" otherwise.
146 * To allow the caller to skip NULL check, this function returns "" rather than
147 * NULL if there is no more words to read.
149 char *tomoyo_read_token(struct tomoyo_acl_param
*param
)
151 char *pos
= param
->data
;
152 char *del
= strchr(pos
, ' ');
156 del
= pos
+ strlen(pos
);
162 * tomoyo_get_domainname - Read a domainname from a line.
164 * @param: Pointer to "struct tomoyo_acl_param".
166 * Returns a domainname on success, NULL otherwise.
168 const struct tomoyo_path_info
*tomoyo_get_domainname
169 (struct tomoyo_acl_param
*param
)
171 char *start
= param
->data
;
174 if (*pos
++ != ' ' || *pos
++ == '/')
181 if (tomoyo_correct_domain(start
))
182 return tomoyo_get_name(start
);
187 * tomoyo_parse_ulong - Parse an "unsigned long" value.
189 * @result: Pointer to "unsigned long".
190 * @str: Pointer to string to parse.
192 * Returns one of values in "enum tomoyo_value_type".
194 * The @src is updated to point the first character after the value
197 u8
tomoyo_parse_ulong(unsigned long *result
, char **str
)
199 const char *cp
= *str
;
204 if (c
== 'x' || c
== 'X') {
207 } else if (c
>= '0' && c
<= '7') {
212 *result
= simple_strtoul(cp
, &ep
, base
);
214 return TOMOYO_VALUE_TYPE_INVALID
;
218 return TOMOYO_VALUE_TYPE_HEXADECIMAL
;
220 return TOMOYO_VALUE_TYPE_OCTAL
;
222 return TOMOYO_VALUE_TYPE_DECIMAL
;
227 * tomoyo_print_ulong - Print an "unsigned long" value.
229 * @buffer: Pointer to buffer.
230 * @buffer_len: Size of @buffer.
231 * @value: An "unsigned long" value.
232 * @type: Type of @value.
236 void tomoyo_print_ulong(char *buffer
, const int buffer_len
,
237 const unsigned long value
, const u8 type
)
239 if (type
== TOMOYO_VALUE_TYPE_DECIMAL
)
240 snprintf(buffer
, buffer_len
, "%lu", value
);
241 else if (type
== TOMOYO_VALUE_TYPE_OCTAL
)
242 snprintf(buffer
, buffer_len
, "0%lo", value
);
243 else if (type
== TOMOYO_VALUE_TYPE_HEXADECIMAL
)
244 snprintf(buffer
, buffer_len
, "0x%lX", value
);
246 snprintf(buffer
, buffer_len
, "type(%u)", type
);
250 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
252 * @param: Pointer to "struct tomoyo_acl_param".
253 * @ptr: Pointer to "struct tomoyo_name_union".
255 * Returns true on success, false otherwise.
257 bool tomoyo_parse_name_union(struct tomoyo_acl_param
*param
,
258 struct tomoyo_name_union
*ptr
)
261 if (param
->data
[0] == '@') {
263 ptr
->group
= tomoyo_get_group(param
, TOMOYO_PATH_GROUP
);
264 return ptr
->group
!= NULL
;
266 filename
= tomoyo_read_token(param
);
267 if (!tomoyo_correct_word(filename
))
269 ptr
->filename
= tomoyo_get_name(filename
);
270 return ptr
->filename
!= NULL
;
274 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
276 * @param: Pointer to "struct tomoyo_acl_param".
277 * @ptr: Pointer to "struct tomoyo_number_union".
279 * Returns true on success, false otherwise.
281 bool tomoyo_parse_number_union(struct tomoyo_acl_param
*param
,
282 struct tomoyo_number_union
*ptr
)
287 memset(ptr
, 0, sizeof(*ptr
));
288 if (param
->data
[0] == '@') {
290 ptr
->group
= tomoyo_get_group(param
, TOMOYO_NUMBER_GROUP
);
291 return ptr
->group
!= NULL
;
293 data
= tomoyo_read_token(param
);
294 type
= tomoyo_parse_ulong(&v
, &data
);
295 if (type
== TOMOYO_VALUE_TYPE_INVALID
)
298 ptr
->value_type
[0] = type
;
301 ptr
->value_type
[1] = type
;
306 type
= tomoyo_parse_ulong(&v
, &data
);
307 if (type
== TOMOYO_VALUE_TYPE_INVALID
|| *data
|| ptr
->values
[0] > v
)
310 ptr
->value_type
[1] = type
;
315 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
317 * @str: Pointer to the string.
319 * Returns true if @str is a \ooo style octal value, false otherwise.
321 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
322 * This function verifies that \ooo is in valid range.
324 static inline bool tomoyo_byte_range(const char *str
)
326 return *str
>= '0' && *str
++ <= '3' &&
327 *str
>= '0' && *str
++ <= '7' &&
328 *str
>= '0' && *str
<= '7';
332 * tomoyo_alphabet_char - Check whether the character is an alphabet.
334 * @c: The character to check.
336 * Returns true if @c is an alphabet character, false otherwise.
338 static inline bool tomoyo_alphabet_char(const char c
)
340 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
344 * tomoyo_make_byte - Make byte value from three octal characters.
346 * @c1: The first character.
347 * @c2: The second character.
348 * @c3: The third character.
350 * Returns byte value.
352 static inline u8
tomoyo_make_byte(const u8 c1
, const u8 c2
, const u8 c3
)
354 return ((c1
- '0') << 6) + ((c2
- '0') << 3) + (c3
- '0');
358 * tomoyo_valid - Check whether the character is a valid char.
360 * @c: The character to check.
362 * Returns true if @c is a valid character, false otherwise.
364 static inline bool tomoyo_valid(const unsigned char c
)
366 return c
> ' ' && c
< 127;
370 * tomoyo_invalid - Check whether the character is an invalid char.
372 * @c: The character to check.
374 * Returns true if @c is an invalid character, false otherwise.
376 static inline bool tomoyo_invalid(const unsigned char c
)
378 return c
&& (c
<= ' ' || c
>= 127);
382 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
384 * @src: Pointer to pointer to the string.
385 * @find: Pointer to the keyword.
387 * Returns true if @src starts with @find, false otherwise.
389 * The @src is updated to point the first character after the @find
390 * if @src starts with @find.
392 bool tomoyo_str_starts(char **src
, const char *find
)
394 const int len
= strlen(find
);
397 if (strncmp(tmp
, find
, len
))
405 * tomoyo_normalize_line - Format string.
407 * @buffer: The line to normalize.
409 * Leading and trailing whitespaces are removed.
410 * Multiple whitespaces are packed into single space.
414 void tomoyo_normalize_line(unsigned char *buffer
)
416 unsigned char *sp
= buffer
;
417 unsigned char *dp
= buffer
;
420 while (tomoyo_invalid(*sp
))
426 while (tomoyo_valid(*sp
))
428 while (tomoyo_invalid(*sp
))
435 * tomoyo_correct_word2 - Validate a string.
437 * @string: The string to check. Maybe non-'\0'-terminated.
438 * @len: Length of @string.
440 * Check whether the given string follows the naming rules.
441 * Returns true if @string follows the naming rules, false otherwise.
443 static bool tomoyo_correct_word2(const char *string
, size_t len
)
445 const char *const start
= string
;
446 bool in_repetition
= false;
459 case '\\': /* "\\" */
472 case '{': /* "/\{" */
473 if (string
- 3 < start
|| *(string
- 3) != '/')
475 in_repetition
= true;
477 case '}': /* "\}/" */
482 in_repetition
= false;
484 case '0': /* "\ooo" */
488 if (!len
-- || !len
--)
492 if (d
< '0' || d
> '7' || e
< '0' || e
> '7')
494 c
= tomoyo_make_byte(c
, d
, e
);
495 if (c
<= ' ' || c
>= 127)
499 } else if (in_repetition
&& c
== '/') {
501 } else if (c
<= ' ' || c
>= 127) {
513 * tomoyo_correct_word - Validate a string.
515 * @string: The string to check.
517 * Check whether the given string follows the naming rules.
518 * Returns true if @string follows the naming rules, false otherwise.
520 bool tomoyo_correct_word(const char *string
)
522 return tomoyo_correct_word2(string
, strlen(string
));
526 * tomoyo_correct_path - Validate a pathname.
528 * @filename: The pathname to check.
530 * Check whether the given pathname follows the naming rules.
531 * Returns true if @filename follows the naming rules, false otherwise.
533 bool tomoyo_correct_path(const char *filename
)
535 return *filename
== '/' && tomoyo_correct_word(filename
);
539 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
541 * @domainname: The domainname to check.
543 * Returns true if @domainname follows the naming rules, false otherwise.
545 bool tomoyo_correct_domain(const unsigned char *domainname
)
547 if (!domainname
|| !tomoyo_domain_def(domainname
))
549 domainname
= strchr(domainname
, ' ');
553 const unsigned char *cp
= strchr(domainname
, ' ');
556 if (*domainname
!= '/' ||
557 !tomoyo_correct_word2(domainname
, cp
- domainname
))
561 return tomoyo_correct_path(domainname
);
565 * tomoyo_domain_def - Check whether the given token can be a domainname.
567 * @buffer: The token to check.
569 * Returns true if @buffer possibly be a domainname, false otherwise.
571 bool tomoyo_domain_def(const unsigned char *buffer
)
573 const unsigned char *cp
;
577 cp
= strchr(buffer
, ' ');
579 len
= strlen(buffer
);
582 if (buffer
[len
- 1] != '>' ||
583 !tomoyo_correct_word2(buffer
+ 1, len
- 2))
589 * tomoyo_find_domain - Find a domain by the given name.
591 * @domainname: The domainname to find.
593 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
595 * Caller holds tomoyo_read_lock().
597 struct tomoyo_domain_info
*tomoyo_find_domain(const char *domainname
)
599 struct tomoyo_domain_info
*domain
;
600 struct tomoyo_path_info name
;
602 name
.name
= domainname
;
603 tomoyo_fill_path_info(&name
);
604 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
605 if (!domain
->is_deleted
&&
606 !tomoyo_pathcmp(&name
, domain
->domainname
))
613 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
615 * @filename: The string to evaluate.
617 * Returns the initial length without a pattern in @filename.
619 static int tomoyo_const_part_length(const char *filename
)
626 while ((c
= *filename
++) != '\0') {
633 case '\\': /* "\\" */
636 case '0': /* "\ooo" */
641 if (c
< '0' || c
> '7')
644 if (c
< '0' || c
> '7')
655 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
657 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
659 * The caller sets "struct tomoyo_path_info"->name.
661 void tomoyo_fill_path_info(struct tomoyo_path_info
*ptr
)
663 const char *name
= ptr
->name
;
664 const int len
= strlen(name
);
666 ptr
->const_len
= tomoyo_const_part_length(name
);
667 ptr
->is_dir
= len
&& (name
[len
- 1] == '/');
668 ptr
->is_patterned
= (ptr
->const_len
< len
);
669 ptr
->hash
= full_name_hash(name
, len
);
673 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
675 * @filename: The start of string to check.
676 * @filename_end: The end of string to check.
677 * @pattern: The start of pattern to compare.
678 * @pattern_end: The end of pattern to compare.
680 * Returns true if @filename matches @pattern, false otherwise.
682 static bool tomoyo_file_matches_pattern2(const char *filename
,
683 const char *filename_end
,
685 const char *pattern_end
)
687 while (filename
< filename_end
&& pattern
< pattern_end
) {
689 if (*pattern
!= '\\') {
690 if (*filename
++ != *pattern
++)
702 } else if (c
== '\\') {
703 if (filename
[1] == '\\')
705 else if (tomoyo_byte_range(filename
+ 1))
714 if (*++filename
!= '\\')
726 if (!tomoyo_alphabet_char(c
))
733 if (c
== '\\' && tomoyo_byte_range(filename
+ 1)
734 && strncmp(filename
+ 1, pattern
, 3) == 0) {
739 return false; /* Not matched. */
742 for (i
= 0; i
<= filename_end
- filename
; i
++) {
743 if (tomoyo_file_matches_pattern2(
744 filename
+ i
, filename_end
,
745 pattern
+ 1, pattern_end
))
748 if (c
== '.' && *pattern
== '@')
752 if (filename
[i
+ 1] == '\\')
754 else if (tomoyo_byte_range(filename
+ i
+ 1))
757 break; /* Bad pattern. */
759 return false; /* Not matched. */
764 while (isdigit(filename
[j
]))
766 } else if (c
== 'X') {
767 while (isxdigit(filename
[j
]))
769 } else if (c
== 'A') {
770 while (tomoyo_alphabet_char(filename
[j
]))
773 for (i
= 1; i
<= j
; i
++) {
774 if (tomoyo_file_matches_pattern2(
775 filename
+ i
, filename_end
,
776 pattern
+ 1, pattern_end
))
779 return false; /* Not matched or bad pattern. */
784 while (*pattern
== '\\' &&
785 (*(pattern
+ 1) == '*' || *(pattern
+ 1) == '@'))
787 return filename
== filename_end
&& pattern
== pattern_end
;
791 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
793 * @filename: The start of string to check.
794 * @filename_end: The end of string to check.
795 * @pattern: The start of pattern to compare.
796 * @pattern_end: The end of pattern to compare.
798 * Returns true if @filename matches @pattern, false otherwise.
800 static bool tomoyo_file_matches_pattern(const char *filename
,
801 const char *filename_end
,
803 const char *pattern_end
)
805 const char *pattern_start
= pattern
;
809 while (pattern
< pattern_end
- 1) {
810 /* Split at "\-" pattern. */
811 if (*pattern
++ != '\\' || *pattern
++ != '-')
813 result
= tomoyo_file_matches_pattern2(filename
,
822 pattern_start
= pattern
;
824 result
= tomoyo_file_matches_pattern2(filename
, filename_end
,
825 pattern_start
, pattern_end
);
826 return first
? result
: !result
;
830 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
832 * @f: The start of string to check.
833 * @p: The start of pattern to compare.
835 * Returns true if @f matches @p, false otherwise.
837 static bool tomoyo_path_matches_pattern2(const char *f
, const char *p
)
839 const char *f_delimiter
;
840 const char *p_delimiter
;
843 f_delimiter
= strchr(f
, '/');
845 f_delimiter
= f
+ strlen(f
);
846 p_delimiter
= strchr(p
, '/');
848 p_delimiter
= p
+ strlen(p
);
849 if (*p
== '\\' && *(p
+ 1) == '{')
851 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
,
861 /* Ignore trailing "\*" and "\@" in @pattern. */
863 (*(p
+ 1) == '*' || *(p
+ 1) == '@'))
868 * The "\{" pattern is permitted only after '/' character.
869 * This guarantees that below "*(p - 1)" is safe.
870 * Also, the "\}" pattern is permitted only before '/' character
871 * so that "\{" + "\}" pair will not break the "\-" operator.
873 if (*(p
- 1) != '/' || p_delimiter
<= p
+ 3 || *p_delimiter
!= '/' ||
874 *(p_delimiter
- 1) != '}' || *(p_delimiter
- 2) != '\\')
875 return false; /* Bad pattern. */
877 /* Compare current component with pattern. */
878 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
+ 2,
881 /* Proceed to next component. */
886 /* Continue comparison. */
887 if (tomoyo_path_matches_pattern2(f
, p_delimiter
+ 1))
889 f_delimiter
= strchr(f
, '/');
890 } while (f_delimiter
);
891 return false; /* Not matched. */
895 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
897 * @filename: The filename to check.
898 * @pattern: The pattern to compare.
900 * Returns true if matches, false otherwise.
902 * The following patterns are available.
904 * \ooo Octal representation of a byte.
905 * \* Zero or more repetitions of characters other than '/'.
906 * \@ Zero or more repetitions of characters other than '/' or '.'.
907 * \? 1 byte character other than '/'.
908 * \$ One or more repetitions of decimal digits.
909 * \+ 1 decimal digit.
910 * \X One or more repetitions of hexadecimal digits.
911 * \x 1 hexadecimal digit.
912 * \A One or more repetitions of alphabet characters.
913 * \a 1 alphabet character.
915 * \- Subtraction operator.
917 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
920 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info
*filename
,
921 const struct tomoyo_path_info
*pattern
)
923 const char *f
= filename
->name
;
924 const char *p
= pattern
->name
;
925 const int len
= pattern
->const_len
;
927 /* If @pattern doesn't contain pattern, I can use strcmp(). */
928 if (!pattern
->is_patterned
)
929 return !tomoyo_pathcmp(filename
, pattern
);
930 /* Don't compare directory and non-directory. */
931 if (filename
->is_dir
!= pattern
->is_dir
)
933 /* Compare the initial length without patterns. */
934 if (strncmp(f
, p
, len
))
938 return tomoyo_path_matches_pattern2(f
, p
);
942 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
944 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
946 * This function uses kzalloc(), so the caller must call kfree()
947 * if this function didn't return NULL.
949 const char *tomoyo_get_exe(void)
951 struct mm_struct
*mm
= current
->mm
;
952 struct vm_area_struct
*vma
;
953 const char *cp
= NULL
;
957 down_read(&mm
->mmap_sem
);
958 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
959 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
) {
960 cp
= tomoyo_realpath_from_path(&vma
->vm_file
->f_path
);
964 up_read(&mm
->mmap_sem
);
969 * tomoyo_get_mode - Get MAC mode.
971 * @ns: Pointer to "struct tomoyo_policy_namespace".
972 * @profile: Profile number.
973 * @index: Index number of functionality.
977 int tomoyo_get_mode(const struct tomoyo_policy_namespace
*ns
, const u8 profile
,
981 struct tomoyo_profile
*p
;
983 if (!tomoyo_policy_loaded
)
984 return TOMOYO_CONFIG_DISABLED
;
985 p
= tomoyo_profile(ns
, profile
);
986 mode
= p
->config
[index
];
987 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
988 mode
= p
->config
[tomoyo_index2category
[index
]
989 + TOMOYO_MAX_MAC_INDEX
];
990 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
991 mode
= p
->default_config
;
996 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
998 * @r: Pointer to "struct tomoyo_request_info" to initialize.
999 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
1000 * @index: Index number of functionality.
1004 int tomoyo_init_request_info(struct tomoyo_request_info
*r
,
1005 struct tomoyo_domain_info
*domain
, const u8 index
)
1008 memset(r
, 0, sizeof(*r
));
1010 domain
= tomoyo_domain();
1012 profile
= domain
->profile
;
1013 r
->profile
= profile
;
1015 r
->mode
= tomoyo_get_mode(domain
->ns
, profile
, index
);
1020 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1022 * @r: Pointer to "struct tomoyo_request_info".
1024 * Returns true if the domain is not exceeded quota, false otherwise.
1026 * Caller holds tomoyo_read_lock().
1028 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info
*r
)
1030 unsigned int count
= 0;
1031 struct tomoyo_domain_info
*domain
= r
->domain
;
1032 struct tomoyo_acl_info
*ptr
;
1034 if (r
->mode
!= TOMOYO_CONFIG_LEARNING
)
1038 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
1041 if (ptr
->is_deleted
)
1043 switch (ptr
->type
) {
1044 case TOMOYO_TYPE_PATH_ACL
:
1045 perm
= container_of(ptr
, struct tomoyo_path_acl
, head
)
1048 case TOMOYO_TYPE_PATH2_ACL
:
1049 perm
= container_of(ptr
, struct tomoyo_path2_acl
, head
)
1052 case TOMOYO_TYPE_PATH_NUMBER_ACL
:
1053 perm
= container_of(ptr
, struct tomoyo_path_number_acl
,
1056 case TOMOYO_TYPE_MKDEV_ACL
:
1057 perm
= container_of(ptr
, struct tomoyo_mkdev_acl
,
1060 case TOMOYO_TYPE_INET_ACL
:
1061 perm
= container_of(ptr
, struct tomoyo_inet_acl
,
1064 case TOMOYO_TYPE_UNIX_ACL
:
1065 perm
= container_of(ptr
, struct tomoyo_unix_acl
,
1068 case TOMOYO_TYPE_MANUAL_TASK_ACL
:
1074 for (i
= 0; i
< 16; i
++)
1075 if (perm
& (1 << i
))
1078 if (count
< tomoyo_profile(domain
->ns
, domain
->profile
)->
1079 pref
[TOMOYO_PREF_MAX_LEARNING_ENTRY
])
1081 if (!domain
->flags
[TOMOYO_DIF_QUOTA_WARNED
]) {
1082 domain
->flags
[TOMOYO_DIF_QUOTA_WARNED
] = true;
1083 /* r->granted = false; */
1084 tomoyo_write_log(r
, "%s", tomoyo_dif
[TOMOYO_DIF_QUOTA_WARNED
]);
1085 printk(KERN_WARNING
"WARNING: "
1086 "Domain '%s' has too many ACLs to hold. "
1087 "Stopped learning mode.\n", domain
->domainname
->name
);