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 * Mapping table from "enum tomoyo_mac_index" to
20 * "enum tomoyo_mac_category_index".
22 const u8 tomoyo_index2category
[TOMOYO_MAX_MAC_INDEX
] = {
23 /* CONFIG::file group */
24 [TOMOYO_MAC_FILE_EXECUTE
] = TOMOYO_MAC_CATEGORY_FILE
,
25 [TOMOYO_MAC_FILE_OPEN
] = TOMOYO_MAC_CATEGORY_FILE
,
26 [TOMOYO_MAC_FILE_CREATE
] = TOMOYO_MAC_CATEGORY_FILE
,
27 [TOMOYO_MAC_FILE_UNLINK
] = TOMOYO_MAC_CATEGORY_FILE
,
28 [TOMOYO_MAC_FILE_GETATTR
] = TOMOYO_MAC_CATEGORY_FILE
,
29 [TOMOYO_MAC_FILE_MKDIR
] = TOMOYO_MAC_CATEGORY_FILE
,
30 [TOMOYO_MAC_FILE_RMDIR
] = TOMOYO_MAC_CATEGORY_FILE
,
31 [TOMOYO_MAC_FILE_MKFIFO
] = TOMOYO_MAC_CATEGORY_FILE
,
32 [TOMOYO_MAC_FILE_MKSOCK
] = TOMOYO_MAC_CATEGORY_FILE
,
33 [TOMOYO_MAC_FILE_TRUNCATE
] = TOMOYO_MAC_CATEGORY_FILE
,
34 [TOMOYO_MAC_FILE_SYMLINK
] = TOMOYO_MAC_CATEGORY_FILE
,
35 [TOMOYO_MAC_FILE_MKBLOCK
] = TOMOYO_MAC_CATEGORY_FILE
,
36 [TOMOYO_MAC_FILE_MKCHAR
] = TOMOYO_MAC_CATEGORY_FILE
,
37 [TOMOYO_MAC_FILE_LINK
] = TOMOYO_MAC_CATEGORY_FILE
,
38 [TOMOYO_MAC_FILE_RENAME
] = TOMOYO_MAC_CATEGORY_FILE
,
39 [TOMOYO_MAC_FILE_CHMOD
] = TOMOYO_MAC_CATEGORY_FILE
,
40 [TOMOYO_MAC_FILE_CHOWN
] = TOMOYO_MAC_CATEGORY_FILE
,
41 [TOMOYO_MAC_FILE_CHGRP
] = TOMOYO_MAC_CATEGORY_FILE
,
42 [TOMOYO_MAC_FILE_IOCTL
] = TOMOYO_MAC_CATEGORY_FILE
,
43 [TOMOYO_MAC_FILE_CHROOT
] = TOMOYO_MAC_CATEGORY_FILE
,
44 [TOMOYO_MAC_FILE_MOUNT
] = TOMOYO_MAC_CATEGORY_FILE
,
45 [TOMOYO_MAC_FILE_UMOUNT
] = TOMOYO_MAC_CATEGORY_FILE
,
46 [TOMOYO_MAC_FILE_PIVOT_ROOT
] = TOMOYO_MAC_CATEGORY_FILE
,
50 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
52 * @time: Seconds since 1970/01/01 00:00:00.
53 * @stamp: Pointer to "struct tomoyo_time".
57 * This function does not handle Y2038 problem.
59 void tomoyo_convert_time(time_t time
, struct tomoyo_time
*stamp
)
61 static const u16 tomoyo_eom
[2][12] = {
62 { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
63 { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
68 stamp
->sec
= time
% 60;
70 stamp
->min
= time
% 60;
72 stamp
->hour
= time
% 24;
74 for (y
= 1970; ; y
++) {
75 const unsigned short days
= (y
& 3) ? 365 : 366;
81 for (m
= 0; m
< 11 && time
>= tomoyo_eom
[r
][m
]; m
++)
84 time
-= tomoyo_eom
[r
][m
- 1];
91 * tomoyo_permstr - Find permission keywords.
93 * @string: String representation for permissions in foo/bar/buz format.
94 * @keyword: Keyword to find from @string/
96 * Returns ture if @keyword was found in @string, false otherwise.
98 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
100 bool tomoyo_permstr(const char *string
, const char *keyword
)
102 const char *cp
= strstr(string
, keyword
);
104 return cp
== string
|| *(cp
- 1) == '/';
109 * tomoyo_read_token - Read a word from a line.
111 * @param: Pointer to "struct tomoyo_acl_param".
113 * Returns a word on success, "" otherwise.
115 * To allow the caller to skip NULL check, this function returns "" rather than
116 * NULL if there is no more words to read.
118 char *tomoyo_read_token(struct tomoyo_acl_param
*param
)
120 char *pos
= param
->data
;
121 char *del
= strchr(pos
, ' ');
125 del
= pos
+ strlen(pos
);
131 * tomoyo_parse_ulong - Parse an "unsigned long" value.
133 * @result: Pointer to "unsigned long".
134 * @str: Pointer to string to parse.
136 * Returns one of values in "enum tomoyo_value_type".
138 * The @src is updated to point the first character after the value
141 u8
tomoyo_parse_ulong(unsigned long *result
, char **str
)
143 const char *cp
= *str
;
148 if (c
== 'x' || c
== 'X') {
151 } else if (c
>= '0' && c
<= '7') {
156 *result
= simple_strtoul(cp
, &ep
, base
);
158 return TOMOYO_VALUE_TYPE_INVALID
;
162 return TOMOYO_VALUE_TYPE_HEXADECIMAL
;
164 return TOMOYO_VALUE_TYPE_OCTAL
;
166 return TOMOYO_VALUE_TYPE_DECIMAL
;
171 * tomoyo_print_ulong - Print an "unsigned long" value.
173 * @buffer: Pointer to buffer.
174 * @buffer_len: Size of @buffer.
175 * @value: An "unsigned long" value.
176 * @type: Type of @value.
180 void tomoyo_print_ulong(char *buffer
, const int buffer_len
,
181 const unsigned long value
, const u8 type
)
183 if (type
== TOMOYO_VALUE_TYPE_DECIMAL
)
184 snprintf(buffer
, buffer_len
, "%lu", value
);
185 else if (type
== TOMOYO_VALUE_TYPE_OCTAL
)
186 snprintf(buffer
, buffer_len
, "0%lo", value
);
187 else if (type
== TOMOYO_VALUE_TYPE_HEXADECIMAL
)
188 snprintf(buffer
, buffer_len
, "0x%lX", value
);
190 snprintf(buffer
, buffer_len
, "type(%u)", type
);
194 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
196 * @param: Pointer to "struct tomoyo_acl_param".
197 * @ptr: Pointer to "struct tomoyo_name_union".
199 * Returns true on success, false otherwise.
201 bool tomoyo_parse_name_union(struct tomoyo_acl_param
*param
,
202 struct tomoyo_name_union
*ptr
)
205 if (param
->data
[0] == '@') {
207 ptr
->group
= tomoyo_get_group(param
, TOMOYO_PATH_GROUP
);
208 return ptr
->group
!= NULL
;
210 filename
= tomoyo_read_token(param
);
211 if (!tomoyo_correct_word(filename
))
213 ptr
->filename
= tomoyo_get_name(filename
);
214 return ptr
->filename
!= NULL
;
218 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
220 * @param: Pointer to "struct tomoyo_acl_param".
221 * @ptr: Pointer to "struct tomoyo_number_union".
223 * Returns true on success, false otherwise.
225 bool tomoyo_parse_number_union(struct tomoyo_acl_param
*param
,
226 struct tomoyo_number_union
*ptr
)
231 memset(ptr
, 0, sizeof(*ptr
));
232 if (param
->data
[0] == '@') {
234 ptr
->group
= tomoyo_get_group(param
, TOMOYO_NUMBER_GROUP
);
235 return ptr
->group
!= NULL
;
237 data
= tomoyo_read_token(param
);
238 type
= tomoyo_parse_ulong(&v
, &data
);
239 if (type
== TOMOYO_VALUE_TYPE_INVALID
)
242 ptr
->value_type
[0] = type
;
245 ptr
->value_type
[1] = type
;
250 type
= tomoyo_parse_ulong(&v
, &data
);
251 if (type
== TOMOYO_VALUE_TYPE_INVALID
|| *data
|| ptr
->values
[0] > v
)
254 ptr
->value_type
[1] = type
;
259 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
261 * @str: Pointer to the string.
263 * Returns true if @str is a \ooo style octal value, false otherwise.
265 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
266 * This function verifies that \ooo is in valid range.
268 static inline bool tomoyo_byte_range(const char *str
)
270 return *str
>= '0' && *str
++ <= '3' &&
271 *str
>= '0' && *str
++ <= '7' &&
272 *str
>= '0' && *str
<= '7';
276 * tomoyo_alphabet_char - Check whether the character is an alphabet.
278 * @c: The character to check.
280 * Returns true if @c is an alphabet character, false otherwise.
282 static inline bool tomoyo_alphabet_char(const char c
)
284 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
288 * tomoyo_make_byte - Make byte value from three octal characters.
290 * @c1: The first character.
291 * @c2: The second character.
292 * @c3: The third character.
294 * Returns byte value.
296 static inline u8
tomoyo_make_byte(const u8 c1
, const u8 c2
, const u8 c3
)
298 return ((c1
- '0') << 6) + ((c2
- '0') << 3) + (c3
- '0');
302 * tomoyo_valid - Check whether the character is a valid char.
304 * @c: The character to check.
306 * Returns true if @c is a valid character, false otherwise.
308 static inline bool tomoyo_valid(const unsigned char c
)
310 return c
> ' ' && c
< 127;
314 * tomoyo_invalid - Check whether the character is an invalid char.
316 * @c: The character to check.
318 * Returns true if @c is an invalid character, false otherwise.
320 static inline bool tomoyo_invalid(const unsigned char c
)
322 return c
&& (c
<= ' ' || c
>= 127);
326 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
328 * @src: Pointer to pointer to the string.
329 * @find: Pointer to the keyword.
331 * Returns true if @src starts with @find, false otherwise.
333 * The @src is updated to point the first character after the @find
334 * if @src starts with @find.
336 bool tomoyo_str_starts(char **src
, const char *find
)
338 const int len
= strlen(find
);
341 if (strncmp(tmp
, find
, len
))
349 * tomoyo_normalize_line - Format string.
351 * @buffer: The line to normalize.
353 * Leading and trailing whitespaces are removed.
354 * Multiple whitespaces are packed into single space.
358 void tomoyo_normalize_line(unsigned char *buffer
)
360 unsigned char *sp
= buffer
;
361 unsigned char *dp
= buffer
;
364 while (tomoyo_invalid(*sp
))
370 while (tomoyo_valid(*sp
))
372 while (tomoyo_invalid(*sp
))
379 * tomoyo_correct_word2 - Validate a string.
381 * @string: The string to check. May be non-'\0'-terminated.
382 * @len: Length of @string.
384 * Check whether the given string follows the naming rules.
385 * Returns true if @string follows the naming rules, false otherwise.
387 static bool tomoyo_correct_word2(const char *string
, size_t len
)
389 const char *const start
= string
;
390 bool in_repetition
= false;
403 case '\\': /* "\\" */
416 case '{': /* "/\{" */
417 if (string
- 3 < start
|| *(string
- 3) != '/')
419 in_repetition
= true;
421 case '}': /* "\}/" */
426 in_repetition
= false;
428 case '0': /* "\ooo" */
432 if (!len
-- || !len
--)
436 if (d
< '0' || d
> '7' || e
< '0' || e
> '7')
438 c
= tomoyo_make_byte(c
, d
, e
);
439 if (tomoyo_invalid(c
))
440 continue; /* pattern is not \000 */
443 } else if (in_repetition
&& c
== '/') {
445 } else if (tomoyo_invalid(c
)) {
457 * tomoyo_correct_word - Validate a string.
459 * @string: The string to check.
461 * Check whether the given string follows the naming rules.
462 * Returns true if @string follows the naming rules, false otherwise.
464 bool tomoyo_correct_word(const char *string
)
466 return tomoyo_correct_word2(string
, strlen(string
));
470 * tomoyo_correct_path - Validate a pathname.
472 * @filename: The pathname to check.
474 * Check whether the given pathname follows the naming rules.
475 * Returns true if @filename follows the naming rules, false otherwise.
477 bool tomoyo_correct_path(const char *filename
)
479 return *filename
== '/' && tomoyo_correct_word(filename
);
483 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
485 * @domainname: The domainname to check.
487 * Returns true if @domainname follows the naming rules, false otherwise.
489 bool tomoyo_correct_domain(const unsigned char *domainname
)
491 if (!domainname
|| !tomoyo_domain_def(domainname
))
493 domainname
= strchr(domainname
, ' ');
497 const unsigned char *cp
= strchr(domainname
, ' ');
500 if (*domainname
!= '/' ||
501 !tomoyo_correct_word2(domainname
, cp
- domainname
))
505 return tomoyo_correct_path(domainname
);
509 * tomoyo_domain_def - Check whether the given token can be a domainname.
511 * @buffer: The token to check.
513 * Returns true if @buffer possibly be a domainname, false otherwise.
515 bool tomoyo_domain_def(const unsigned char *buffer
)
517 const unsigned char *cp
;
521 cp
= strchr(buffer
, ' ');
523 len
= strlen(buffer
);
526 if (buffer
[len
- 1] != '>' ||
527 !tomoyo_correct_word2(buffer
+ 1, len
- 2))
533 * tomoyo_find_domain - Find a domain by the given name.
535 * @domainname: The domainname to find.
537 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
539 * Caller holds tomoyo_read_lock().
541 struct tomoyo_domain_info
*tomoyo_find_domain(const char *domainname
)
543 struct tomoyo_domain_info
*domain
;
544 struct tomoyo_path_info name
;
546 name
.name
= domainname
;
547 tomoyo_fill_path_info(&name
);
548 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
549 if (!domain
->is_deleted
&&
550 !tomoyo_pathcmp(&name
, domain
->domainname
))
557 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
559 * @filename: The string to evaluate.
561 * Returns the initial length without a pattern in @filename.
563 static int tomoyo_const_part_length(const char *filename
)
570 while ((c
= *filename
++) != '\0') {
577 case '\\': /* "\\" */
580 case '0': /* "\ooo" */
585 if (c
< '0' || c
> '7')
588 if (c
< '0' || c
> '7')
599 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
601 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
603 * The caller sets "struct tomoyo_path_info"->name.
605 void tomoyo_fill_path_info(struct tomoyo_path_info
*ptr
)
607 const char *name
= ptr
->name
;
608 const int len
= strlen(name
);
610 ptr
->const_len
= tomoyo_const_part_length(name
);
611 ptr
->is_dir
= len
&& (name
[len
- 1] == '/');
612 ptr
->is_patterned
= (ptr
->const_len
< len
);
613 ptr
->hash
= full_name_hash(name
, len
);
617 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
619 * @filename: The start of string to check.
620 * @filename_end: The end of string to check.
621 * @pattern: The start of pattern to compare.
622 * @pattern_end: The end of pattern to compare.
624 * Returns true if @filename matches @pattern, false otherwise.
626 static bool tomoyo_file_matches_pattern2(const char *filename
,
627 const char *filename_end
,
629 const char *pattern_end
)
631 while (filename
< filename_end
&& pattern
< pattern_end
) {
633 if (*pattern
!= '\\') {
634 if (*filename
++ != *pattern
++)
646 } else if (c
== '\\') {
647 if (filename
[1] == '\\')
649 else if (tomoyo_byte_range(filename
+ 1))
658 if (*++filename
!= '\\')
670 if (!tomoyo_alphabet_char(c
))
677 if (c
== '\\' && tomoyo_byte_range(filename
+ 1)
678 && strncmp(filename
+ 1, pattern
, 3) == 0) {
683 return false; /* Not matched. */
686 for (i
= 0; i
<= filename_end
- filename
; i
++) {
687 if (tomoyo_file_matches_pattern2(
688 filename
+ i
, filename_end
,
689 pattern
+ 1, pattern_end
))
692 if (c
== '.' && *pattern
== '@')
696 if (filename
[i
+ 1] == '\\')
698 else if (tomoyo_byte_range(filename
+ i
+ 1))
701 break; /* Bad pattern. */
703 return false; /* Not matched. */
708 while (isdigit(filename
[j
]))
710 } else if (c
== 'X') {
711 while (isxdigit(filename
[j
]))
713 } else if (c
== 'A') {
714 while (tomoyo_alphabet_char(filename
[j
]))
717 for (i
= 1; i
<= j
; i
++) {
718 if (tomoyo_file_matches_pattern2(
719 filename
+ i
, filename_end
,
720 pattern
+ 1, pattern_end
))
723 return false; /* Not matched or bad pattern. */
728 while (*pattern
== '\\' &&
729 (*(pattern
+ 1) == '*' || *(pattern
+ 1) == '@'))
731 return filename
== filename_end
&& pattern
== pattern_end
;
735 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
737 * @filename: The start of string to check.
738 * @filename_end: The end of string to check.
739 * @pattern: The start of pattern to compare.
740 * @pattern_end: The end of pattern to compare.
742 * Returns true if @filename matches @pattern, false otherwise.
744 static bool tomoyo_file_matches_pattern(const char *filename
,
745 const char *filename_end
,
747 const char *pattern_end
)
749 const char *pattern_start
= pattern
;
753 while (pattern
< pattern_end
- 1) {
754 /* Split at "\-" pattern. */
755 if (*pattern
++ != '\\' || *pattern
++ != '-')
757 result
= tomoyo_file_matches_pattern2(filename
,
766 pattern_start
= pattern
;
768 result
= tomoyo_file_matches_pattern2(filename
, filename_end
,
769 pattern_start
, pattern_end
);
770 return first
? result
: !result
;
774 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
776 * @f: The start of string to check.
777 * @p: The start of pattern to compare.
779 * Returns true if @f matches @p, false otherwise.
781 static bool tomoyo_path_matches_pattern2(const char *f
, const char *p
)
783 const char *f_delimiter
;
784 const char *p_delimiter
;
787 f_delimiter
= strchr(f
, '/');
789 f_delimiter
= f
+ strlen(f
);
790 p_delimiter
= strchr(p
, '/');
792 p_delimiter
= p
+ strlen(p
);
793 if (*p
== '\\' && *(p
+ 1) == '{')
795 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
,
805 /* Ignore trailing "\*" and "\@" in @pattern. */
807 (*(p
+ 1) == '*' || *(p
+ 1) == '@'))
812 * The "\{" pattern is permitted only after '/' character.
813 * This guarantees that below "*(p - 1)" is safe.
814 * Also, the "\}" pattern is permitted only before '/' character
815 * so that "\{" + "\}" pair will not break the "\-" operator.
817 if (*(p
- 1) != '/' || p_delimiter
<= p
+ 3 || *p_delimiter
!= '/' ||
818 *(p_delimiter
- 1) != '}' || *(p_delimiter
- 2) != '\\')
819 return false; /* Bad pattern. */
821 /* Compare current component with pattern. */
822 if (!tomoyo_file_matches_pattern(f
, f_delimiter
, p
+ 2,
825 /* Proceed to next component. */
830 /* Continue comparison. */
831 if (tomoyo_path_matches_pattern2(f
, p_delimiter
+ 1))
833 f_delimiter
= strchr(f
, '/');
834 } while (f_delimiter
);
835 return false; /* Not matched. */
839 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
841 * @filename: The filename to check.
842 * @pattern: The pattern to compare.
844 * Returns true if matches, false otherwise.
846 * The following patterns are available.
848 * \ooo Octal representation of a byte.
849 * \* Zero or more repetitions of characters other than '/'.
850 * \@ Zero or more repetitions of characters other than '/' or '.'.
851 * \? 1 byte character other than '/'.
852 * \$ One or more repetitions of decimal digits.
853 * \+ 1 decimal digit.
854 * \X One or more repetitions of hexadecimal digits.
855 * \x 1 hexadecimal digit.
856 * \A One or more repetitions of alphabet characters.
857 * \a 1 alphabet character.
859 * \- Subtraction operator.
861 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
864 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info
*filename
,
865 const struct tomoyo_path_info
*pattern
)
867 const char *f
= filename
->name
;
868 const char *p
= pattern
->name
;
869 const int len
= pattern
->const_len
;
871 /* If @pattern doesn't contain pattern, I can use strcmp(). */
872 if (!pattern
->is_patterned
)
873 return !tomoyo_pathcmp(filename
, pattern
);
874 /* Don't compare directory and non-directory. */
875 if (filename
->is_dir
!= pattern
->is_dir
)
877 /* Compare the initial length without patterns. */
878 if (strncmp(f
, p
, len
))
882 return tomoyo_path_matches_pattern2(f
, p
);
886 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
888 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
890 * This function uses kzalloc(), so the caller must call kfree()
891 * if this function didn't return NULL.
893 const char *tomoyo_get_exe(void)
895 struct mm_struct
*mm
= current
->mm
;
896 struct vm_area_struct
*vma
;
897 const char *cp
= NULL
;
901 down_read(&mm
->mmap_sem
);
902 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
903 if ((vma
->vm_flags
& VM_EXECUTABLE
) && vma
->vm_file
) {
904 cp
= tomoyo_realpath_from_path(&vma
->vm_file
->f_path
);
908 up_read(&mm
->mmap_sem
);
913 * tomoyo_get_mode - Get MAC mode.
915 * @ns: Pointer to "struct tomoyo_policy_namespace".
916 * @profile: Profile number.
917 * @index: Index number of functionality.
921 int tomoyo_get_mode(const struct tomoyo_policy_namespace
*ns
, const u8 profile
,
925 const u8 category
= TOMOYO_MAC_CATEGORY_FILE
;
926 if (!tomoyo_policy_loaded
)
927 return TOMOYO_CONFIG_DISABLED
;
928 mode
= tomoyo_profile(ns
, profile
)->config
[index
];
929 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
930 mode
= tomoyo_profile(ns
, profile
)->config
[category
];
931 if (mode
== TOMOYO_CONFIG_USE_DEFAULT
)
932 mode
= tomoyo_profile(ns
, profile
)->default_config
;
937 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
939 * @r: Pointer to "struct tomoyo_request_info" to initialize.
940 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
941 * @index: Index number of functionality.
945 int tomoyo_init_request_info(struct tomoyo_request_info
*r
,
946 struct tomoyo_domain_info
*domain
, const u8 index
)
949 memset(r
, 0, sizeof(*r
));
951 domain
= tomoyo_domain();
953 profile
= domain
->profile
;
954 r
->profile
= profile
;
956 r
->mode
= tomoyo_get_mode(domain
->ns
, profile
, index
);
961 * tomoyo_domain_quota_is_ok - Check for domain's quota.
963 * @r: Pointer to "struct tomoyo_request_info".
965 * Returns true if the domain is not exceeded quota, false otherwise.
967 * Caller holds tomoyo_read_lock().
969 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info
*r
)
971 unsigned int count
= 0;
972 struct tomoyo_domain_info
*domain
= r
->domain
;
973 struct tomoyo_acl_info
*ptr
;
975 if (r
->mode
!= TOMOYO_CONFIG_LEARNING
)
979 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
985 case TOMOYO_TYPE_PATH_ACL
:
986 perm
= container_of(ptr
, struct tomoyo_path_acl
, head
)
989 case TOMOYO_TYPE_PATH2_ACL
:
990 perm
= container_of(ptr
, struct tomoyo_path2_acl
, head
)
993 case TOMOYO_TYPE_PATH_NUMBER_ACL
:
994 perm
= container_of(ptr
, struct tomoyo_path_number_acl
,
997 case TOMOYO_TYPE_MKDEV_ACL
:
998 perm
= container_of(ptr
, struct tomoyo_mkdev_acl
,
1004 for (i
= 0; i
< 16; i
++)
1005 if (perm
& (1 << i
))
1008 if (count
< tomoyo_profile(domain
->ns
, domain
->profile
)->
1009 pref
[TOMOYO_PREF_MAX_LEARNING_ENTRY
])
1011 if (!domain
->flags
[TOMOYO_DIF_QUOTA_WARNED
]) {
1012 domain
->flags
[TOMOYO_DIF_QUOTA_WARNED
] = true;
1013 /* r->granted = false; */
1014 tomoyo_write_log(r
, "%s", tomoyo_dif
[TOMOYO_DIF_QUOTA_WARNED
]);
1015 printk(KERN_WARNING
"WARNING: "
1016 "Domain '%s' has too many ACLs to hold. "
1017 "Stopped learning mode.\n", domain
->domainname
->name
);