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 void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp)
95 time64_to_tm(time64, 0, &tm);
96 stamp->sec = tm.tm_sec;
97 stamp->min = tm.tm_min;
98 stamp->hour = tm.tm_hour;
99 stamp->day = tm.tm_mday;
100 stamp->month = tm.tm_mon + 1;
101 stamp->year = tm.tm_year + 1900;
105 * tomoyo_permstr - Find permission keywords.
107 * @string: String representation for permissions in foo/bar/buz format.
108 * @keyword: Keyword to find from @string/
110 * Returns true if @keyword was found in @string, false otherwise.
112 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
114 bool tomoyo_permstr(const char *string, const char *keyword)
116 const char *cp = strstr(string, keyword);
119 return cp == string || *(cp - 1) == '/';
124 * tomoyo_read_token - Read a word from a line.
126 * @param: Pointer to "struct tomoyo_acl_param".
128 * Returns a word on success, "" otherwise.
130 * To allow the caller to skip NULL check, this function returns "" rather than
131 * NULL if there is no more words to read.
133 char *tomoyo_read_token(struct tomoyo_acl_param *param)
135 char *pos = param->data;
136 char *del = strchr(pos, ' ');
141 del = pos + strlen(pos);
147 * tomoyo_get_domainname - Read a domainname from a line.
149 * @param: Pointer to "struct tomoyo_acl_param".
151 * Returns a domainname on success, NULL otherwise.
153 const struct tomoyo_path_info *tomoyo_get_domainname
154 (struct tomoyo_acl_param *param)
156 char *start = param->data;
160 if (*pos++ != ' ' || *pos++ == '/')
167 if (tomoyo_correct_domain(start))
168 return tomoyo_get_name(start);
173 * tomoyo_parse_ulong - Parse an "unsigned long" value.
175 * @result: Pointer to "unsigned long".
176 * @str: Pointer to string to parse.
178 * Returns one of values in "enum tomoyo_value_type".
180 * The @src is updated to point the first character after the value
183 u8 tomoyo_parse_ulong(unsigned long *result, char **str)
185 const char *cp = *str;
192 if (c == 'x' || c == 'X') {
195 } else if (c >= '0' && c <= '7') {
200 *result = simple_strtoul(cp, &ep, base);
202 return TOMOYO_VALUE_TYPE_INVALID;
206 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
208 return TOMOYO_VALUE_TYPE_OCTAL;
210 return TOMOYO_VALUE_TYPE_DECIMAL;
215 * tomoyo_print_ulong - Print an "unsigned long" value.
217 * @buffer: Pointer to buffer.
218 * @buffer_len: Size of @buffer.
219 * @value: An "unsigned long" value.
220 * @type: Type of @value.
224 void tomoyo_print_ulong(char *buffer, const int buffer_len,
225 const unsigned long value, const u8 type)
227 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
228 snprintf(buffer, buffer_len, "%lu", value);
229 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
230 snprintf(buffer, buffer_len, "0%lo", value);
231 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
232 snprintf(buffer, buffer_len, "0x%lX", value);
234 snprintf(buffer, buffer_len, "type(%u)", type);
238 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
240 * @param: Pointer to "struct tomoyo_acl_param".
241 * @ptr: Pointer to "struct tomoyo_name_union".
243 * Returns true on success, false otherwise.
245 bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
246 struct tomoyo_name_union *ptr)
250 if (param->data[0] == '@') {
252 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
253 return ptr->group != NULL;
255 filename = tomoyo_read_token(param);
256 if (!tomoyo_correct_word(filename))
258 ptr->filename = tomoyo_get_name(filename);
259 return ptr->filename != NULL;
263 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
265 * @param: Pointer to "struct tomoyo_acl_param".
266 * @ptr: Pointer to "struct tomoyo_number_union".
268 * Returns true on success, false otherwise.
270 bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
271 struct tomoyo_number_union *ptr)
277 memset(ptr, 0, sizeof(*ptr));
278 if (param->data[0] == '@') {
280 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
281 return ptr->group != NULL;
283 data = tomoyo_read_token(param);
284 type = tomoyo_parse_ulong(&v, &data);
285 if (type == TOMOYO_VALUE_TYPE_INVALID)
288 ptr->value_type[0] = type;
291 ptr->value_type[1] = type;
296 type = tomoyo_parse_ulong(&v, &data);
297 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
300 ptr->value_type[1] = type;
305 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
307 * @str: Pointer to the string.
309 * Returns true if @str is a \ooo style octal value, false otherwise.
311 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
312 * This function verifies that \ooo is in valid range.
314 static inline bool tomoyo_byte_range(const char *str)
316 return *str >= '0' && *str++ <= '3' &&
317 *str >= '0' && *str++ <= '7' &&
318 *str >= '0' && *str <= '7';
322 * tomoyo_alphabet_char - Check whether the character is an alphabet.
324 * @c: The character to check.
326 * Returns true if @c is an alphabet character, false otherwise.
328 static inline bool tomoyo_alphabet_char(const char c)
330 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
334 * tomoyo_make_byte - Make byte value from three octal characters.
336 * @c1: The first character.
337 * @c2: The second character.
338 * @c3: The third character.
340 * Returns byte value.
342 static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
344 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
348 * tomoyo_valid - Check whether the character is a valid char.
350 * @c: The character to check.
352 * Returns true if @c is a valid character, false otherwise.
354 static inline bool tomoyo_valid(const unsigned char c)
356 return c > ' ' && c < 127;
360 * tomoyo_invalid - Check whether the character is an invalid char.
362 * @c: The character to check.
364 * Returns true if @c is an invalid character, false otherwise.
366 static inline bool tomoyo_invalid(const unsigned char c)
368 return c && (c <= ' ' || c >= 127);
372 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
374 * @src: Pointer to pointer to the string.
375 * @find: Pointer to the keyword.
377 * Returns true if @src starts with @find, false otherwise.
379 * The @src is updated to point the first character after the @find
380 * if @src starts with @find.
382 bool tomoyo_str_starts(char **src, const char *find)
384 const int len = strlen(find);
387 if (strncmp(tmp, find, len))
395 * tomoyo_normalize_line - Format string.
397 * @buffer: The line to normalize.
399 * Leading and trailing whitespaces are removed.
400 * Multiple whitespaces are packed into single space.
404 void tomoyo_normalize_line(unsigned char *buffer)
406 unsigned char *sp = buffer;
407 unsigned char *dp = buffer;
410 while (tomoyo_invalid(*sp))
416 while (tomoyo_valid(*sp))
418 while (tomoyo_invalid(*sp))
425 * tomoyo_correct_word2 - Validate a string.
427 * @string: The string to check. Maybe non-'\0'-terminated.
428 * @len: Length of @string.
430 * Check whether the given string follows the naming rules.
431 * Returns true if @string follows the naming rules, false otherwise.
433 static bool tomoyo_correct_word2(const char *string, size_t len)
435 const char *const start = string;
436 bool in_repetition = false;
450 case '\\': /* "\\" */
463 case '{': /* "/\{" */
464 if (string - 3 < start || *(string - 3) != '/')
466 in_repetition = true;
468 case '}': /* "\}/" */
473 in_repetition = false;
475 case '0': /* "\ooo" */
479 if (!len-- || !len--)
483 if (d < '0' || d > '7' || e < '0' || e > '7')
485 c = tomoyo_make_byte(c, d, e);
486 if (c <= ' ' || c >= 127)
490 } else if (in_repetition && c == '/') {
492 } else if (c <= ' ' || c >= 127) {
504 * tomoyo_correct_word - Validate a string.
506 * @string: The string to check.
508 * Check whether the given string follows the naming rules.
509 * Returns true if @string follows the naming rules, false otherwise.
511 bool tomoyo_correct_word(const char *string)
513 return tomoyo_correct_word2(string, strlen(string));
517 * tomoyo_correct_path - Validate a pathname.
519 * @filename: The pathname to check.
521 * Check whether the given pathname follows the naming rules.
522 * Returns true if @filename follows the naming rules, false otherwise.
524 bool tomoyo_correct_path(const char *filename)
526 return *filename == '/' && tomoyo_correct_word(filename);
530 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
532 * @domainname: The domainname to check.
534 * Returns true if @domainname follows the naming rules, false otherwise.
536 bool tomoyo_correct_domain(const unsigned char *domainname)
538 if (!domainname || !tomoyo_domain_def(domainname))
540 domainname = strchr(domainname, ' ');
544 const unsigned char *cp = strchr(domainname, ' ');
548 if (*domainname != '/' ||
549 !tomoyo_correct_word2(domainname, cp - domainname))
553 return tomoyo_correct_path(domainname);
557 * tomoyo_domain_def - Check whether the given token can be a domainname.
559 * @buffer: The token to check.
561 * Returns true if @buffer possibly be a domainname, false otherwise.
563 bool tomoyo_domain_def(const unsigned char *buffer)
565 const unsigned char *cp;
570 cp = strchr(buffer, ' ');
572 len = strlen(buffer);
575 if (buffer[len - 1] != '>' ||
576 !tomoyo_correct_word2(buffer + 1, len - 2))
582 * tomoyo_find_domain - Find a domain by the given name.
584 * @domainname: The domainname to find.
586 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
588 * Caller holds tomoyo_read_lock().
590 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
592 struct tomoyo_domain_info *domain;
593 struct tomoyo_path_info name;
595 name.name = domainname;
596 tomoyo_fill_path_info(&name);
597 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list,
598 srcu_read_lock_held(&tomoyo_ss)) {
599 if (!domain->is_deleted &&
600 !tomoyo_pathcmp(&name, domain->domainname))
607 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
609 * @filename: The string to evaluate.
611 * Returns the initial length without a pattern in @filename.
613 static int tomoyo_const_part_length(const char *filename)
620 while ((c = *filename++) != '\0') {
627 case '\\': /* "\\" */
630 case '0': /* "\ooo" */
635 if (c < '0' || c > '7')
638 if (c < '0' || c > '7')
649 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
651 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
653 * The caller sets "struct tomoyo_path_info"->name.
655 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
657 const char *name = ptr->name;
658 const int len = strlen(name);
660 ptr->const_len = tomoyo_const_part_length(name);
661 ptr->is_dir = len && (name[len - 1] == '/');
662 ptr->is_patterned = (ptr->const_len < len);
663 ptr->hash = full_name_hash(NULL, name, len);
667 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
669 * @filename: The start of string to check.
670 * @filename_end: The end of string to check.
671 * @pattern: The start of pattern to compare.
672 * @pattern_end: The end of pattern to compare.
674 * Returns true if @filename matches @pattern, false otherwise.
676 static bool tomoyo_file_matches_pattern2(const char *filename,
677 const char *filename_end,
679 const char *pattern_end)
681 while (filename < filename_end && pattern < pattern_end) {
686 if (*pattern != '\\') {
687 if (*filename++ != *pattern++)
697 } else if (c == '\\') {
698 if (filename[1] == '\\')
700 else if (tomoyo_byte_range(filename + 1))
709 if (*++filename != '\\')
721 if (!tomoyo_alphabet_char(c))
728 if (c == '\\' && tomoyo_byte_range(filename + 1)
729 && strncmp(filename + 1, pattern, 3) == 0) {
734 return false; /* Not matched. */
737 for (i = 0; i <= filename_end - filename; i++) {
738 if (tomoyo_file_matches_pattern2(
739 filename + i, filename_end,
740 pattern + 1, pattern_end))
743 if (c == '.' && *pattern == '@')
747 if (filename[i + 1] == '\\')
749 else if (tomoyo_byte_range(filename + i + 1))
752 break; /* Bad pattern. */
754 return false; /* Not matched. */
759 while (isdigit(filename[j]))
761 } else if (c == 'X') {
762 while (isxdigit(filename[j]))
764 } else if (c == 'A') {
765 while (tomoyo_alphabet_char(filename[j]))
768 for (i = 1; i <= j; i++) {
769 if (tomoyo_file_matches_pattern2(
770 filename + i, filename_end,
771 pattern + 1, pattern_end))
774 return false; /* Not matched or bad pattern. */
779 while (*pattern == '\\' &&
780 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
782 return filename == filename_end && pattern == pattern_end;
786 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
788 * @filename: The start of string to check.
789 * @filename_end: The end of string to check.
790 * @pattern: The start of pattern to compare.
791 * @pattern_end: The end of pattern to compare.
793 * Returns true if @filename matches @pattern, false otherwise.
795 static bool tomoyo_file_matches_pattern(const char *filename,
796 const char *filename_end,
798 const char *pattern_end)
800 const char *pattern_start = pattern;
804 while (pattern < pattern_end - 1) {
805 /* Split at "\-" pattern. */
806 if (*pattern++ != '\\' || *pattern++ != '-')
808 result = tomoyo_file_matches_pattern2(filename,
817 pattern_start = pattern;
819 result = tomoyo_file_matches_pattern2(filename, filename_end,
820 pattern_start, pattern_end);
821 return first ? result : !result;
825 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
827 * @f: The start of string to check.
828 * @p: The start of pattern to compare.
830 * Returns true if @f matches @p, false otherwise.
832 static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
834 const char *f_delimiter;
835 const char *p_delimiter;
838 f_delimiter = strchr(f, '/');
840 f_delimiter = f + strlen(f);
841 p_delimiter = strchr(p, '/');
843 p_delimiter = p + strlen(p);
844 if (*p == '\\' && *(p + 1) == '{')
846 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
856 /* Ignore trailing "\*" and "\@" in @pattern. */
858 (*(p + 1) == '*' || *(p + 1) == '@'))
863 * The "\{" pattern is permitted only after '/' character.
864 * This guarantees that below "*(p - 1)" is safe.
865 * Also, the "\}" pattern is permitted only before '/' character
866 * so that "\{" + "\}" pair will not break the "\-" operator.
868 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
869 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
870 return false; /* Bad pattern. */
872 /* Compare current component with pattern. */
873 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
876 /* Proceed to next component. */
881 /* Continue comparison. */
882 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
884 f_delimiter = strchr(f, '/');
885 } while (f_delimiter);
886 return false; /* Not matched. */
890 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
892 * @filename: The filename to check.
893 * @pattern: The pattern to compare.
895 * Returns true if matches, false otherwise.
897 * The following patterns are available.
899 * \ooo Octal representation of a byte.
900 * \* Zero or more repetitions of characters other than '/'.
901 * \@ Zero or more repetitions of characters other than '/' or '.'.
902 * \? 1 byte character other than '/'.
903 * \$ One or more repetitions of decimal digits.
904 * \+ 1 decimal digit.
905 * \X One or more repetitions of hexadecimal digits.
906 * \x 1 hexadecimal digit.
907 * \A One or more repetitions of alphabet characters.
908 * \a 1 alphabet character.
910 * \- Subtraction operator.
912 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
915 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
916 const struct tomoyo_path_info *pattern)
918 const char *f = filename->name;
919 const char *p = pattern->name;
920 const int len = pattern->const_len;
922 /* If @pattern doesn't contain pattern, I can use strcmp(). */
923 if (!pattern->is_patterned)
924 return !tomoyo_pathcmp(filename, pattern);
925 /* Don't compare directory and non-directory. */
926 if (filename->is_dir != pattern->is_dir)
928 /* Compare the initial length without patterns. */
929 if (strncmp(f, p, len))
933 return tomoyo_path_matches_pattern2(f, p);
937 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
939 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
941 * This function uses kzalloc(), so the caller must call kfree()
942 * if this function didn't return NULL.
944 const char *tomoyo_get_exe(void)
946 struct file *exe_file;
948 struct mm_struct *mm = current->mm;
952 exe_file = get_mm_exe_file(mm);
956 cp = tomoyo_realpath_from_path(&exe_file->f_path);
962 * tomoyo_get_mode - Get MAC mode.
964 * @ns: Pointer to "struct tomoyo_policy_namespace".
965 * @profile: Profile number.
966 * @index: Index number of functionality.
970 int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
974 struct tomoyo_profile *p;
976 if (!tomoyo_policy_loaded)
977 return TOMOYO_CONFIG_DISABLED;
978 p = tomoyo_profile(ns, profile);
979 mode = p->config[index];
980 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
981 mode = p->config[tomoyo_index2category[index]
982 + TOMOYO_MAX_MAC_INDEX];
983 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
984 mode = p->default_config;
989 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
991 * @r: Pointer to "struct tomoyo_request_info" to initialize.
992 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
993 * @index: Index number of functionality.
997 int tomoyo_init_request_info(struct tomoyo_request_info *r,
998 struct tomoyo_domain_info *domain, const u8 index)
1002 memset(r, 0, sizeof(*r));
1004 domain = tomoyo_domain();
1006 profile = domain->profile;
1007 r->profile = profile;
1009 r->mode = tomoyo_get_mode(domain->ns, profile, index);
1014 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1016 * @r: Pointer to "struct tomoyo_request_info".
1018 * Returns true if the domain is not exceeded quota, false otherwise.
1020 * Caller holds tomoyo_read_lock().
1022 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1024 unsigned int count = 0;
1025 struct tomoyo_domain_info *domain = r->domain;
1026 struct tomoyo_acl_info *ptr;
1028 if (r->mode != TOMOYO_CONFIG_LEARNING)
1032 if (READ_ONCE(domain->flags[TOMOYO_DIF_QUOTA_WARNED]))
1034 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list,
1035 srcu_read_lock_held(&tomoyo_ss)) {
1039 if (ptr->is_deleted)
1041 switch (ptr->type) {
1042 case TOMOYO_TYPE_PATH_ACL:
1043 perm = container_of(ptr, struct tomoyo_path_acl, head)
1046 case TOMOYO_TYPE_PATH2_ACL:
1047 perm = container_of(ptr, struct tomoyo_path2_acl, head)
1050 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1051 perm = container_of(ptr, struct tomoyo_path_number_acl,
1054 case TOMOYO_TYPE_MKDEV_ACL:
1055 perm = container_of(ptr, struct tomoyo_mkdev_acl,
1058 case TOMOYO_TYPE_INET_ACL:
1059 perm = container_of(ptr, struct tomoyo_inet_acl,
1062 case TOMOYO_TYPE_UNIX_ACL:
1063 perm = container_of(ptr, struct tomoyo_unix_acl,
1066 case TOMOYO_TYPE_MANUAL_TASK_ACL:
1072 for (i = 0; i < 16; i++)
1073 if (perm & (1 << i))
1076 if (count < tomoyo_profile(domain->ns, domain->profile)->
1077 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
1079 WRITE_ONCE(domain->flags[TOMOYO_DIF_QUOTA_WARNED], true);
1080 /* r->granted = false; */
1081 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
1082 #ifndef CONFIG_SECURITY_TOMOYO_INSECURE_BUILTIN_SETTING
1083 pr_warn("WARNING: Domain '%s' has too many ACLs to hold. Stopped learning mode.\n",
1084 domain->domainname->name);