1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/util.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/slab.h>
9 #include <linux/rculist.h>
13 /* Lock for protecting policy. */
14 DEFINE_MUTEX(tomoyo_policy_lock
);
16 /* Has /sbin/init started? */
17 bool tomoyo_policy_loaded
;
20 * Mapping table from "enum tomoyo_mac_index" to
21 * "enum tomoyo_mac_category_index".
23 const u8 tomoyo_index2category
[TOMOYO_MAX_MAC_INDEX
] = {
24 /* CONFIG::file group */
25 [TOMOYO_MAC_FILE_EXECUTE
] = TOMOYO_MAC_CATEGORY_FILE
,
26 [TOMOYO_MAC_FILE_OPEN
] = TOMOYO_MAC_CATEGORY_FILE
,
27 [TOMOYO_MAC_FILE_CREATE
] = TOMOYO_MAC_CATEGORY_FILE
,
28 [TOMOYO_MAC_FILE_UNLINK
] = TOMOYO_MAC_CATEGORY_FILE
,
29 [TOMOYO_MAC_FILE_GETATTR
] = TOMOYO_MAC_CATEGORY_FILE
,
30 [TOMOYO_MAC_FILE_MKDIR
] = TOMOYO_MAC_CATEGORY_FILE
,
31 [TOMOYO_MAC_FILE_RMDIR
] = TOMOYO_MAC_CATEGORY_FILE
,
32 [TOMOYO_MAC_FILE_MKFIFO
] = TOMOYO_MAC_CATEGORY_FILE
,
33 [TOMOYO_MAC_FILE_MKSOCK
] = TOMOYO_MAC_CATEGORY_FILE
,
34 [TOMOYO_MAC_FILE_TRUNCATE
] = TOMOYO_MAC_CATEGORY_FILE
,
35 [TOMOYO_MAC_FILE_SYMLINK
] = TOMOYO_MAC_CATEGORY_FILE
,
36 [TOMOYO_MAC_FILE_MKBLOCK
] = TOMOYO_MAC_CATEGORY_FILE
,
37 [TOMOYO_MAC_FILE_MKCHAR
] = TOMOYO_MAC_CATEGORY_FILE
,
38 [TOMOYO_MAC_FILE_LINK
] = TOMOYO_MAC_CATEGORY_FILE
,
39 [TOMOYO_MAC_FILE_RENAME
] = TOMOYO_MAC_CATEGORY_FILE
,
40 [TOMOYO_MAC_FILE_CHMOD
] = TOMOYO_MAC_CATEGORY_FILE
,
41 [TOMOYO_MAC_FILE_CHOWN
] = TOMOYO_MAC_CATEGORY_FILE
,
42 [TOMOYO_MAC_FILE_CHGRP
] = TOMOYO_MAC_CATEGORY_FILE
,
43 [TOMOYO_MAC_FILE_IOCTL
] = TOMOYO_MAC_CATEGORY_FILE
,
44 [TOMOYO_MAC_FILE_CHROOT
] = TOMOYO_MAC_CATEGORY_FILE
,
45 [TOMOYO_MAC_FILE_MOUNT
] = TOMOYO_MAC_CATEGORY_FILE
,
46 [TOMOYO_MAC_FILE_UMOUNT
] = TOMOYO_MAC_CATEGORY_FILE
,
47 [TOMOYO_MAC_FILE_PIVOT_ROOT
] = TOMOYO_MAC_CATEGORY_FILE
,
48 /* CONFIG::network group */
49 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND
] =
50 TOMOYO_MAC_CATEGORY_NETWORK
,
51 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN
] =
52 TOMOYO_MAC_CATEGORY_NETWORK
,
53 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT
] =
54 TOMOYO_MAC_CATEGORY_NETWORK
,
55 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND
] =
56 TOMOYO_MAC_CATEGORY_NETWORK
,
57 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND
] =
58 TOMOYO_MAC_CATEGORY_NETWORK
,
59 [TOMOYO_MAC_NETWORK_INET_RAW_BIND
] =
60 TOMOYO_MAC_CATEGORY_NETWORK
,
61 [TOMOYO_MAC_NETWORK_INET_RAW_SEND
] =
62 TOMOYO_MAC_CATEGORY_NETWORK
,
63 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND
] =
64 TOMOYO_MAC_CATEGORY_NETWORK
,
65 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN
] =
66 TOMOYO_MAC_CATEGORY_NETWORK
,
67 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT
] =
68 TOMOYO_MAC_CATEGORY_NETWORK
,
69 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND
] =
70 TOMOYO_MAC_CATEGORY_NETWORK
,
71 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND
] =
72 TOMOYO_MAC_CATEGORY_NETWORK
,
73 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND
] =
74 TOMOYO_MAC_CATEGORY_NETWORK
,
75 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN
] =
76 TOMOYO_MAC_CATEGORY_NETWORK
,
77 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT
] =
78 TOMOYO_MAC_CATEGORY_NETWORK
,
79 /* CONFIG::misc group */
80 [TOMOYO_MAC_ENVIRON
] = TOMOYO_MAC_CATEGORY_MISC
,
84 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
86 * @time: Seconds since 1970/01/01 00:00:00.
87 * @stamp: Pointer to "struct tomoyo_time".
91 * This function does not handle Y2038 problem.
93 void tomoyo_convert_time(time_t time
, struct tomoyo_time
*stamp
)
95 static const u16 tomoyo_eom
[2][12] = {
96 { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
97 { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
102 stamp
->sec
= time
% 60;
104 stamp
->min
= time
% 60;
106 stamp
->hour
= time
% 24;
108 for (y
= 1970; ; y
++) {
109 const unsigned short days
= (y
& 3) ? 365 : 366;
115 for (m
= 0; m
< 11 && time
>= tomoyo_eom
[r
][m
]; m
++)
118 time
-= tomoyo_eom
[r
][m
- 1];
125 * tomoyo_permstr - Find permission keywords.
127 * @string: String representation for permissions in foo/bar/buz format.
128 * @keyword: Keyword to find from @string/
130 * Returns ture if @keyword was found in @string, false otherwise.
132 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
134 bool tomoyo_permstr(const char *string
, const char *keyword
)
136 const char *cp
= strstr(string
, keyword
);
138 return cp
== string
|| *(cp
- 1) == '/';
143 * tomoyo_read_token - Read a word from a line.
145 * @param: Pointer to "struct tomoyo_acl_param".
147 * Returns a word on success, "" otherwise.
149 * To allow the caller to skip NULL check, this function returns "" rather than
150 * NULL if there is no more words to read.
152 char *tomoyo_read_token(struct tomoyo_acl_param
*param
)
154 char *pos
= param
->data
;
155 char *del
= strchr(pos
, ' ');
159 del
= pos
+ strlen(pos
);
165 * tomoyo_get_domainname - Read a domainname from a line.
167 * @param: Pointer to "struct tomoyo_acl_param".
169 * Returns a domainname on success, NULL otherwise.
171 const struct tomoyo_path_info
*tomoyo_get_domainname
172 (struct tomoyo_acl_param
*param
)
174 char *start
= param
->data
;
177 if (*pos
++ != ' ' || *pos
++ == '/')
184 if (tomoyo_correct_domain(start
))
185 return tomoyo_get_name(start
);
190 * tomoyo_parse_ulong - Parse an "unsigned long" value.
192 * @result: Pointer to "unsigned long".
193 * @str: Pointer to string to parse.
195 * Returns one of values in "enum tomoyo_value_type".
197 * The @src is updated to point the first character after the value
200 u8
tomoyo_parse_ulong(unsigned long *result
, char **str
)
202 const char *cp
= *str
;
207 if (c
== 'x' || c
== 'X') {
210 } else if (c
>= '0' && c
<= '7') {
215 *result
= simple_strtoul(cp
, &ep
, base
);
217 return TOMOYO_VALUE_TYPE_INVALID
;
221 return TOMOYO_VALUE_TYPE_HEXADECIMAL
;
223 return TOMOYO_VALUE_TYPE_OCTAL
;
225 return TOMOYO_VALUE_TYPE_DECIMAL
;
230 * tomoyo_print_ulong - Print an "unsigned long" value.
232 * @buffer: Pointer to buffer.
233 * @buffer_len: Size of @buffer.
234 * @value: An "unsigned long" value.
235 * @type: Type of @value.
239 void tomoyo_print_ulong(char *buffer
, const int buffer_len
,
240 const unsigned long value
, const u8 type
)
242 if (type
== TOMOYO_VALUE_TYPE_DECIMAL
)
243 snprintf(buffer
, buffer_len
, "%lu", value
);
244 else if (type
== TOMOYO_VALUE_TYPE_OCTAL
)
245 snprintf(buffer
, buffer_len
, "0%lo", value
);
246 else if (type
== TOMOYO_VALUE_TYPE_HEXADECIMAL
)
247 snprintf(buffer
, buffer_len
, "0x%lX", value
);
249 snprintf(buffer
, buffer_len
, "type(%u)", type
);
253 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
255 * @param: Pointer to "struct tomoyo_acl_param".
256 * @ptr: Pointer to "struct tomoyo_name_union".
258 * Returns true on success, false otherwise.
260 bool tomoyo_parse_name_union(struct tomoyo_acl_param
*param
,
261 struct tomoyo_name_union
*ptr
)
264 if (param
->data
[0] == '@') {
266 ptr
->group
= tomoyo_get_group(param
, TOMOYO_PATH_GROUP
);
267 return ptr
->group
!= NULL
;
269 filename
= tomoyo_read_token(param
);
270 if (!tomoyo_correct_word(filename
))
272 ptr
->filename
= tomoyo_get_name(filename
);
273 return ptr
->filename
!= NULL
;
277 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
279 * @param: Pointer to "struct tomoyo_acl_param".
280 * @ptr: Pointer to "struct tomoyo_number_union".
282 * Returns true on success, false otherwise.
284 bool tomoyo_parse_number_union(struct tomoyo_acl_param
*param
,
285 struct tomoyo_number_union
*ptr
)
290 memset(ptr
, 0, sizeof(*ptr
));
291 if (param
->data
[0] == '@') {
293 ptr
->group
= tomoyo_get_group(param
, TOMOYO_NUMBER_GROUP
);
294 return ptr
->group
!= NULL
;
296 data
= tomoyo_read_token(param
);
297 type
= tomoyo_parse_ulong(&v
, &data
);
298 if (type
== TOMOYO_VALUE_TYPE_INVALID
)
301 ptr
->value_type
[0] = type
;
304 ptr
->value_type
[1] = type
;
309 type
= tomoyo_parse_ulong(&v
, &data
);
310 if (type
== TOMOYO_VALUE_TYPE_INVALID
|| *data
|| ptr
->values
[0] > v
)
313 ptr
->value_type
[1] = type
;
318 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
320 * @str: Pointer to the string.
322 * Returns true if @str is a \ooo style octal value, false otherwise.
324 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
325 * This function verifies that \ooo is in valid range.
327 static inline bool tomoyo_byte_range(const char *str
)
329 return *str
>= '0' && *str
++ <= '3' &&
330 *str
>= '0' && *str
++ <= '7' &&
331 *str
>= '0' && *str
<= '7';
335 * tomoyo_alphabet_char - Check whether the character is an alphabet.
337 * @c: The character to check.
339 * Returns true if @c is an alphabet character, false otherwise.
341 static inline bool tomoyo_alphabet_char(const char c
)
343 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
347 * tomoyo_make_byte - Make byte value from three octal characters.
349 * @c1: The first character.
350 * @c2: The second character.
351 * @c3: The third character.
353 * Returns byte value.
355 static inline u8
tomoyo_make_byte(const u8 c1
, const u8 c2
, const u8 c3
)
357 return ((c1
- '0') << 6) + ((c2
- '0') << 3) + (c3
- '0');
361 * tomoyo_valid - Check whether the character is a valid char.
363 * @c: The character to check.
365 * Returns true if @c is a valid character, false otherwise.
367 static inline bool tomoyo_valid(const unsigned char c
)
369 return c
> ' ' && c
< 127;
373 * tomoyo_invalid - Check whether the character is an invalid char.
375 * @c: The character to check.
377 * Returns true if @c is an invalid character, false otherwise.
379 static inline bool tomoyo_invalid(const unsigned char c
)
381 return c
&& (c
<= ' ' || c
>= 127);
385 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
387 * @src: Pointer to pointer to the string.
388 * @find: Pointer to the keyword.
390 * Returns true if @src starts with @find, false otherwise.
392 * The @src is updated to point the first character after the @find
393 * if @src starts with @find.
395 bool tomoyo_str_starts(char **src
, const char *find
)
397 const int len
= strlen(find
);
400 if (strncmp(tmp
, find
, len
))
408 * tomoyo_normalize_line - Format string.
410 * @buffer: The line to normalize.
412 * Leading and trailing whitespaces are removed.
413 * Multiple whitespaces are packed into single space.
417 void tomoyo_normalize_line(unsigned char *buffer
)
419 unsigned char *sp
= buffer
;
420 unsigned char *dp
= buffer
;
423 while (tomoyo_invalid(*sp
))
429 while (tomoyo_valid(*sp
))
431 while (tomoyo_invalid(*sp
))
438 * tomoyo_correct_word2 - Validate a string.
440 * @string: The string to check. Maybe non-'\0'-terminated.
441 * @len: Length of @string.
443 * Check whether the given string follows the naming rules.
444 * Returns true if @string follows the naming rules, false otherwise.
446 static bool tomoyo_correct_word2(const char *string
, size_t len
)
448 const char *const start
= string
;
449 bool in_repetition
= false;
462 case '\\': /* "\\" */
475 case '{': /* "/\{" */
476 if (string
- 3 < start
|| *(string
- 3) != '/')
478 in_repetition
= true;
480 case '}': /* "\}/" */
485 in_repetition
= false;
487 case '0': /* "\ooo" */
491 if (!len
-- || !len
--)
495 if (d
< '0' || d
> '7' || e
< '0' || e
> '7')
497 c
= tomoyo_make_byte(c
, d
, e
);
498 if (c
<= ' ' || c
>= 127)
502 } else if (in_repetition
&& c
== '/') {
504 } else if (c
<= ' ' || c
>= 127) {
516 * tomoyo_correct_word - Validate a string.
518 * @string: The string to check.
520 * Check whether the given string follows the naming rules.
521 * Returns true if @string follows the naming rules, false otherwise.
523 bool tomoyo_correct_word(const char *string
)
525 return tomoyo_correct_word2(string
, strlen(string
));
529 * tomoyo_correct_path - Validate a pathname.
531 * @filename: The pathname to check.
533 * Check whether the given pathname follows the naming rules.
534 * Returns true if @filename follows the naming rules, false otherwise.
536 bool tomoyo_correct_path(const char *filename
)
538 return *filename
== '/' && tomoyo_correct_word(filename
);
542 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
544 * @domainname: The domainname to check.
546 * Returns true if @domainname follows the naming rules, false otherwise.
548 bool tomoyo_correct_domain(const unsigned char *domainname
)
550 if (!domainname
|| !tomoyo_domain_def(domainname
))
552 domainname
= strchr(domainname
, ' ');
556 const unsigned char *cp
= strchr(domainname
, ' ');
559 if (*domainname
!= '/' ||
560 !tomoyo_correct_word2(domainname
, cp
- domainname
))
564 return tomoyo_correct_path(domainname
);
568 * tomoyo_domain_def - Check whether the given token can be a domainname.
570 * @buffer: The token to check.
572 * Returns true if @buffer possibly be a domainname, false otherwise.
574 bool tomoyo_domain_def(const unsigned char *buffer
)
576 const unsigned char *cp
;
580 cp
= strchr(buffer
, ' ');
582 len
= strlen(buffer
);
585 if (buffer
[len
- 1] != '>' ||
586 !tomoyo_correct_word2(buffer
+ 1, len
- 2))
592 * tomoyo_find_domain - Find a domain by the given name.
594 * @domainname: The domainname to find.
596 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
598 * Caller holds tomoyo_read_lock().
600 struct tomoyo_domain_info
*tomoyo_find_domain(const char *domainname
)
602 struct tomoyo_domain_info
*domain
;
603 struct tomoyo_path_info name
;
605 name
.name
= domainname
;
606 tomoyo_fill_path_info(&name
);
607 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
608 if (!domain
->is_deleted
&&
609 !tomoyo_pathcmp(&name
, domain
->domainname
))
616 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
618 * @filename: The string to evaluate.
620 * Returns the initial length without a pattern in @filename.
622 static int tomoyo_const_part_length(const char *filename
)
629 while ((c
= *filename
++) != '\0') {
636 case '\\': /* "\\" */
639 case '0': /* "\ooo" */
644 if (c
< '0' || c
> '7')
647 if (c
< '0' || c
> '7')
658 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
660 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
662 * The caller sets "struct tomoyo_path_info"->name.
664 void tomoyo_fill_path_info(struct tomoyo_path_info
*ptr
)
666 const char *name
= ptr
->name
;
667 const int len
= strlen(name
);
669 ptr
->const_len
= tomoyo_const_part_length(name
);
670 ptr
->is_dir
= len
&& (name
[len
- 1] == '/');
671 ptr
->is_patterned
= (ptr
->const_len
< len
);
672 ptr
->hash
= full_name_hash(NULL
, name
, len
);
676 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
678 * @filename: The start of string to check.
679 * @filename_end: The end of string to check.
680 * @pattern: The start of pattern to compare.
681 * @pattern_end: The end of pattern to compare.
683 * Returns true if @filename matches @pattern, false otherwise.
685 static bool tomoyo_file_matches_pattern2(const char *filename
,
686 const char *filename_end
,
688 const char *pattern_end
)
690 while (filename
< filename_end
&& pattern
< pattern_end
) {
692 if (*pattern
!= '\\') {
693 if (*filename
++ != *pattern
++)
705 } else if (c
== '\\') {
706 if (filename
[1] == '\\')
708 else if (tomoyo_byte_range(filename
+ 1))
717 if (*++filename
!= '\\')
729 if (!tomoyo_alphabet_char(c
))
736 if (c
== '\\' && tomoyo_byte_range(filename
+ 1)
737 && strncmp(filename
+ 1, pattern
, 3) == 0) {
742 return false; /* Not matched. */
745 for (i
= 0; i
<= filename_end
- filename
; i
++) {
746 if (tomoyo_file_matches_pattern2(
747 filename
+ i
, filename_end
,
748 pattern
+ 1, pattern_end
))
751 if (c
== '.' && *pattern
== '@')
755 if (filename
[i
+ 1] == '\\')
757 else if (tomoyo_byte_range(filename
+ i
+ 1))
760 break; /* Bad pattern. */
762 return false; /* Not matched. */
767 while (isdigit(filename
[j
]))
769 } else if (c
== 'X') {
770 while (isxdigit(filename
[j
]))
772 } else if (c
== 'A') {
773 while (tomoyo_alphabet_char(filename
[j
]))
776 for (i
= 1; i
<= j
; i
++) {
777 if (tomoyo_file_matches_pattern2(
778 filename
+ i
, filename_end
,
779 pattern
+ 1, pattern_end
))
782 return false; /* Not matched or bad pattern. */
787 while (*pattern
== '\\' &&
788 (*(pattern
+ 1) == '*' || *(pattern
+ 1) == '@'))
790 return filename
== filename_end
&& pattern
== pattern_end
;
794 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
796 * @filename: The start of string to check.
797 * @filename_end: The end of string to check.
798 * @pattern: The start of pattern to compare.
799 * @pattern_end: The end of pattern to compare.
801 * Returns true if @filename matches @pattern, false otherwise.
803 static bool tomoyo_file_matches_pattern(const char *filename
,
804 const char *filename_end
,
806 const char *pattern_end
)
808 const char *pattern_start
= pattern
;
812 while (pattern
< pattern_end
- 1) {
813 /* Split at "\-" pattern. */
814 if (*pattern
++ != '\\' || *pattern
++ != '-')
816 result
= tomoyo_file_matches_pattern2(filename
,
825 pattern_start
= pattern
;
827 result
= tomoyo_file_matches_pattern2(filename
, filename_end
,
828 pattern_start
, pattern_end
);
829 return first
? result
: !result
;
833 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
835 * @f: The start of string to check.
836 * @p: The start of pattern to compare.
838 * Returns true if @f matches @p, false otherwise.
840 static bool tomoyo_path_matches_pattern2(const char *f
, const char *p
)
842 const char *f_delimiter
;
843 const char *p_delimiter
;
846 f_delimiter
= strchr(f
, '/');
848 f_delimiter
= f
+ strlen(f
);
849 p_delimiter
= strchr(p
, '/');
851 p_delimiter
= p
+ strlen(p
);
852 if (*p
== '\\' && *(p
+ 1) == '{')
854 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
,
864 /* Ignore trailing "\*" and "\@" in @pattern. */
866 (*(p
+ 1) == '*' || *(p
+ 1) == '@'))
871 * The "\{" pattern is permitted only after '/' character.
872 * This guarantees that below "*(p - 1)" is safe.
873 * Also, the "\}" pattern is permitted only before '/' character
874 * so that "\{" + "\}" pair will not break the "\-" operator.
876 if (*(p
- 1) != '/' || p_delimiter
<= p
+ 3 || *p_delimiter
!= '/' ||
877 *(p_delimiter
- 1) != '}' || *(p_delimiter
- 2) != '\\')
878 return false; /* Bad pattern. */
880 /* Compare current component with pattern. */
881 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
+ 2,
884 /* Proceed to next component. */
889 /* Continue comparison. */
890 if (tomoyo_path_matches_pattern2(f
, p_delimiter
+ 1))
892 f_delimiter
= strchr(f
, '/');
893 } while (f_delimiter
);
894 return false; /* Not matched. */
898 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
900 * @filename: The filename to check.
901 * @pattern: The pattern to compare.
903 * Returns true if matches, false otherwise.
905 * The following patterns are available.
907 * \ooo Octal representation of a byte.
908 * \* Zero or more repetitions of characters other than '/'.
909 * \@ Zero or more repetitions of characters other than '/' or '.'.
910 * \? 1 byte character other than '/'.
911 * \$ One or more repetitions of decimal digits.
912 * \+ 1 decimal digit.
913 * \X One or more repetitions of hexadecimal digits.
914 * \x 1 hexadecimal digit.
915 * \A One or more repetitions of alphabet characters.
916 * \a 1 alphabet character.
918 * \- Subtraction operator.
920 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
923 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info
*filename
,
924 const struct tomoyo_path_info
*pattern
)
926 const char *f
= filename
->name
;
927 const char *p
= pattern
->name
;
928 const int len
= pattern
->const_len
;
930 /* If @pattern doesn't contain pattern, I can use strcmp(). */
931 if (!pattern
->is_patterned
)
932 return !tomoyo_pathcmp(filename
, pattern
);
933 /* Don't compare directory and non-directory. */
934 if (filename
->is_dir
!= pattern
->is_dir
)
936 /* Compare the initial length without patterns. */
937 if (strncmp(f
, p
, len
))
941 return tomoyo_path_matches_pattern2(f
, p
);
945 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
947 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
949 * This function uses kzalloc(), so the caller must call kfree()
950 * if this function didn't return NULL.
952 const char *tomoyo_get_exe(void)
954 struct file
*exe_file
;
956 struct mm_struct
*mm
= current
->mm
;
960 exe_file
= get_mm_exe_file(mm
);
964 cp
= tomoyo_realpath_from_path(&exe_file
->f_path
);
970 * tomoyo_get_mode - Get MAC mode.
972 * @ns: Pointer to "struct tomoyo_policy_namespace".
973 * @profile: Profile number.
974 * @index: Index number of functionality.
978 int tomoyo_get_mode(const struct tomoyo_policy_namespace
*ns
, const u8 profile
,
982 struct tomoyo_profile
*p
;
984 if (!tomoyo_policy_loaded
)
985 return TOMOYO_CONFIG_DISABLED
;
986 p
= tomoyo_profile(ns
, profile
);
987 mode
= p
->config
[index
];
988 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
989 mode
= p
->config
[tomoyo_index2category
[index
]
990 + TOMOYO_MAX_MAC_INDEX
];
991 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
992 mode
= p
->default_config
;
997 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
999 * @r: Pointer to "struct tomoyo_request_info" to initialize.
1000 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
1001 * @index: Index number of functionality.
1005 int tomoyo_init_request_info(struct tomoyo_request_info
*r
,
1006 struct tomoyo_domain_info
*domain
, const u8 index
)
1009 memset(r
, 0, sizeof(*r
));
1011 domain
= tomoyo_domain();
1013 profile
= domain
->profile
;
1014 r
->profile
= profile
;
1016 r
->mode
= tomoyo_get_mode(domain
->ns
, profile
, index
);
1021 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1023 * @r: Pointer to "struct tomoyo_request_info".
1025 * Returns true if the domain is not exceeded quota, false otherwise.
1027 * Caller holds tomoyo_read_lock().
1029 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info
*r
)
1031 unsigned int count
= 0;
1032 struct tomoyo_domain_info
*domain
= r
->domain
;
1033 struct tomoyo_acl_info
*ptr
;
1035 if (r
->mode
!= TOMOYO_CONFIG_LEARNING
)
1039 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
1042 if (ptr
->is_deleted
)
1044 switch (ptr
->type
) {
1045 case TOMOYO_TYPE_PATH_ACL
:
1046 perm
= container_of(ptr
, struct tomoyo_path_acl
, head
)
1049 case TOMOYO_TYPE_PATH2_ACL
:
1050 perm
= container_of(ptr
, struct tomoyo_path2_acl
, head
)
1053 case TOMOYO_TYPE_PATH_NUMBER_ACL
:
1054 perm
= container_of(ptr
, struct tomoyo_path_number_acl
,
1057 case TOMOYO_TYPE_MKDEV_ACL
:
1058 perm
= container_of(ptr
, struct tomoyo_mkdev_acl
,
1061 case TOMOYO_TYPE_INET_ACL
:
1062 perm
= container_of(ptr
, struct tomoyo_inet_acl
,
1065 case TOMOYO_TYPE_UNIX_ACL
:
1066 perm
= container_of(ptr
, struct tomoyo_unix_acl
,
1069 case TOMOYO_TYPE_MANUAL_TASK_ACL
:
1075 for (i
= 0; i
< 16; i
++)
1076 if (perm
& (1 << i
))
1079 if (count
< tomoyo_profile(domain
->ns
, domain
->profile
)->
1080 pref
[TOMOYO_PREF_MAX_LEARNING_ENTRY
])
1082 if (!domain
->flags
[TOMOYO_DIF_QUOTA_WARNED
]) {
1083 domain
->flags
[TOMOYO_DIF_QUOTA_WARNED
] = true;
1084 /* r->granted = false; */
1085 tomoyo_write_log(r
, "%s", tomoyo_dif
[TOMOYO_DIF_QUOTA_WARNED
]);
1086 printk(KERN_WARNING
"WARNING: "
1087 "Domain '%s' has too many ACLs to hold. "
1088 "Stopped learning mode.\n", domain
->domainname
->name
);