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 * @time64: 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);
146 static bool tomoyo_correct_path2(const char *filename, const size_t len);
149 * tomoyo_get_domainname - Read a domainname from a line.
151 * @param: Pointer to "struct tomoyo_acl_param".
153 * Returns a domainname on success, NULL otherwise.
155 const struct tomoyo_path_info *tomoyo_get_domainname
156 (struct tomoyo_acl_param *param)
158 char *start = param->data;
163 tomoyo_correct_path2(pos, strchrnul(pos, ' ') - pos))
169 if (tomoyo_correct_domain(start))
170 return tomoyo_get_name(start);
175 * tomoyo_parse_ulong - Parse an "unsigned long" value.
177 * @result: Pointer to "unsigned long".
178 * @str: Pointer to string to parse.
180 * Returns one of values in "enum tomoyo_value_type".
182 * The @src is updated to point the first character after the value
185 u8 tomoyo_parse_ulong(unsigned long *result, char **str)
187 const char *cp = *str;
194 if (c == 'x' || c == 'X') {
197 } else if (c >= '0' && c <= '7') {
202 *result = simple_strtoul(cp, &ep, base);
204 return TOMOYO_VALUE_TYPE_INVALID;
208 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
210 return TOMOYO_VALUE_TYPE_OCTAL;
212 return TOMOYO_VALUE_TYPE_DECIMAL;
217 * tomoyo_print_ulong - Print an "unsigned long" value.
219 * @buffer: Pointer to buffer.
220 * @buffer_len: Size of @buffer.
221 * @value: An "unsigned long" value.
222 * @type: Type of @value.
226 void tomoyo_print_ulong(char *buffer, const int buffer_len,
227 const unsigned long value, const u8 type)
229 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
230 snprintf(buffer, buffer_len, "%lu", value);
231 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
232 snprintf(buffer, buffer_len, "0%lo", value);
233 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
234 snprintf(buffer, buffer_len, "0x%lX", value);
236 snprintf(buffer, buffer_len, "type(%u)", type);
240 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
242 * @param: Pointer to "struct tomoyo_acl_param".
243 * @ptr: Pointer to "struct tomoyo_name_union".
245 * Returns true on success, false otherwise.
247 bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
248 struct tomoyo_name_union *ptr)
252 if (param->data[0] == '@') {
254 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
255 return ptr->group != NULL;
257 filename = tomoyo_read_token(param);
258 if (!tomoyo_correct_word(filename))
260 ptr->filename = tomoyo_get_name(filename);
261 return ptr->filename != NULL;
265 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
267 * @param: Pointer to "struct tomoyo_acl_param".
268 * @ptr: Pointer to "struct tomoyo_number_union".
270 * Returns true on success, false otherwise.
272 bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
273 struct tomoyo_number_union *ptr)
279 memset(ptr, 0, sizeof(*ptr));
280 if (param->data[0] == '@') {
282 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
283 return ptr->group != NULL;
285 data = tomoyo_read_token(param);
286 type = tomoyo_parse_ulong(&v, &data);
287 if (type == TOMOYO_VALUE_TYPE_INVALID)
290 ptr->value_type[0] = type;
293 ptr->value_type[1] = type;
298 type = tomoyo_parse_ulong(&v, &data);
299 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
302 ptr->value_type[1] = type;
307 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
309 * @str: Pointer to the string.
311 * Returns true if @str is a \ooo style octal value, false otherwise.
313 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
314 * This function verifies that \ooo is in valid range.
316 static inline bool tomoyo_byte_range(const char *str)
318 return *str >= '0' && *str++ <= '3' &&
319 *str >= '0' && *str++ <= '7' &&
320 *str >= '0' && *str <= '7';
324 * tomoyo_alphabet_char - Check whether the character is an alphabet.
326 * @c: The character to check.
328 * Returns true if @c is an alphabet character, false otherwise.
330 static inline bool tomoyo_alphabet_char(const char c)
332 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
336 * tomoyo_make_byte - Make byte value from three octal characters.
338 * @c1: The first character.
339 * @c2: The second character.
340 * @c3: The third character.
342 * Returns byte value.
344 static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
346 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
350 * tomoyo_valid - Check whether the character is a valid char.
352 * @c: The character to check.
354 * Returns true if @c is a valid character, false otherwise.
356 static inline bool tomoyo_valid(const unsigned char c)
358 return c > ' ' && c < 127;
362 * tomoyo_invalid - Check whether the character is an invalid char.
364 * @c: The character to check.
366 * Returns true if @c is an invalid character, false otherwise.
368 static inline bool tomoyo_invalid(const unsigned char c)
370 return c && (c <= ' ' || c >= 127);
374 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
376 * @src: Pointer to pointer to the string.
377 * @find: Pointer to the keyword.
379 * Returns true if @src starts with @find, false otherwise.
381 * The @src is updated to point the first character after the @find
382 * if @src starts with @find.
384 bool tomoyo_str_starts(char **src, const char *find)
386 const int len = strlen(find);
389 if (strncmp(tmp, find, len))
397 * tomoyo_normalize_line - Format string.
399 * @buffer: The line to normalize.
401 * Leading and trailing whitespaces are removed.
402 * Multiple whitespaces are packed into single space.
406 void tomoyo_normalize_line(unsigned char *buffer)
408 unsigned char *sp = buffer;
409 unsigned char *dp = buffer;
412 while (tomoyo_invalid(*sp))
418 while (tomoyo_valid(*sp))
420 while (tomoyo_invalid(*sp))
427 * tomoyo_correct_word2 - Validate a string.
429 * @string: The string to check. Maybe non-'\0'-terminated.
430 * @len: Length of @string.
432 * Check whether the given string follows the naming rules.
433 * Returns true if @string follows the naming rules, false otherwise.
435 static bool tomoyo_correct_word2(const char *string, size_t len)
438 const char *const start = string;
439 bool in_repetition = false;
444 unsigned char c = *string++;
450 if (c >= '0' && c <= '3') {
454 if (!len-- || !len--)
458 if (d < '0' || d > '7' || e < '0' || e > '7')
460 c = tomoyo_make_byte(c, d, e);
461 if (c <= ' ' || c >= 127)
466 case '\\': /* "\\" */
483 case '{': /* "/\{" */
484 if (string - 3 < start || *(string - 3) != '/')
486 in_repetition = true;
488 case '}': /* "\}/" */
493 in_repetition = false;
497 } else if (in_repetition && c == '/') {
499 } else if (c <= ' ' || c >= 127) {
511 * tomoyo_correct_word - Validate a string.
513 * @string: The string to check.
515 * Check whether the given string follows the naming rules.
516 * Returns true if @string follows the naming rules, false otherwise.
518 bool tomoyo_correct_word(const char *string)
520 return tomoyo_correct_word2(string, strlen(string));
524 * tomoyo_correct_path2 - Check whether the given pathname follows the naming rules.
526 * @filename: The pathname to check.
527 * @len: Length of @filename.
529 * Returns true if @filename follows the naming rules, false otherwise.
531 static bool tomoyo_correct_path2(const char *filename, const size_t len)
533 const char *cp1 = memchr(filename, '/', len);
534 const char *cp2 = memchr(filename, '.', len);
536 return cp1 && (!cp2 || (cp1 < cp2)) && tomoyo_correct_word2(filename, len);
540 * tomoyo_correct_path - Validate a pathname.
542 * @filename: The pathname to check.
544 * Check whether the given pathname follows the naming rules.
545 * Returns true if @filename follows the naming rules, false otherwise.
547 bool tomoyo_correct_path(const char *filename)
549 return tomoyo_correct_path2(filename, strlen(filename));
553 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
555 * @domainname: The domainname to check.
557 * Returns true if @domainname follows the naming rules, false otherwise.
559 bool tomoyo_correct_domain(const unsigned char *domainname)
561 if (!domainname || !tomoyo_domain_def(domainname))
563 domainname = strchr(domainname, ' ');
567 const unsigned char *cp = strchr(domainname, ' ');
571 if (!tomoyo_correct_path2(domainname, cp - domainname))
575 return tomoyo_correct_path(domainname);
579 * tomoyo_domain_def - Check whether the given token can be a domainname.
581 * @buffer: The token to check.
583 * Returns true if @buffer possibly be a domainname, false otherwise.
585 bool tomoyo_domain_def(const unsigned char *buffer)
587 const unsigned char *cp;
592 cp = strchr(buffer, ' ');
594 len = strlen(buffer);
597 if (buffer[len - 1] != '>' ||
598 !tomoyo_correct_word2(buffer + 1, len - 2))
604 * tomoyo_find_domain - Find a domain by the given name.
606 * @domainname: The domainname to find.
608 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
610 * Caller holds tomoyo_read_lock().
612 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
614 struct tomoyo_domain_info *domain;
615 struct tomoyo_path_info name;
617 name.name = domainname;
618 tomoyo_fill_path_info(&name);
619 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list,
620 srcu_read_lock_held(&tomoyo_ss)) {
621 if (!domain->is_deleted &&
622 !tomoyo_pathcmp(&name, domain->domainname))
629 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
631 * @filename: The string to evaluate.
633 * Returns the initial length without a pattern in @filename.
635 static int tomoyo_const_part_length(const char *filename)
642 while ((c = *filename++) != '\0') {
649 case '\\': /* "\\" */
652 case '0': /* "\ooo" */
657 if (c < '0' || c > '7')
660 if (c < '0' || c > '7')
671 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
673 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
675 * The caller sets "struct tomoyo_path_info"->name.
677 void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
679 const char *name = ptr->name;
680 const int len = strlen(name);
682 ptr->const_len = tomoyo_const_part_length(name);
683 ptr->is_dir = len && (name[len - 1] == '/');
684 ptr->is_patterned = (ptr->const_len < len);
685 ptr->hash = full_name_hash(NULL, name, len);
689 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
691 * @filename: The start of string to check.
692 * @filename_end: The end of string to check.
693 * @pattern: The start of pattern to compare.
694 * @pattern_end: The end of pattern to compare.
696 * Returns true if @filename matches @pattern, false otherwise.
698 static bool tomoyo_file_matches_pattern2(const char *filename,
699 const char *filename_end,
701 const char *pattern_end)
703 while (filename < filename_end && pattern < pattern_end) {
708 if (*pattern != '\\') {
709 if (*filename++ != *pattern++)
719 } else if (c == '\\') {
720 if (filename[1] == '\\')
722 else if (tomoyo_byte_range(filename + 1))
731 if (*++filename != '\\')
743 if (!tomoyo_alphabet_char(c))
750 if (c == '\\' && tomoyo_byte_range(filename + 1)
751 && strncmp(filename + 1, pattern, 3) == 0) {
756 return false; /* Not matched. */
759 for (i = 0; i <= filename_end - filename; i++) {
760 if (tomoyo_file_matches_pattern2(
761 filename + i, filename_end,
762 pattern + 1, pattern_end))
765 if (c == '.' && *pattern == '@')
769 if (filename[i + 1] == '\\')
771 else if (tomoyo_byte_range(filename + i + 1))
774 break; /* Bad pattern. */
776 return false; /* Not matched. */
781 while (isdigit(filename[j]))
783 } else if (c == 'X') {
784 while (isxdigit(filename[j]))
786 } else if (c == 'A') {
787 while (tomoyo_alphabet_char(filename[j]))
790 for (i = 1; i <= j; i++) {
791 if (tomoyo_file_matches_pattern2(
792 filename + i, filename_end,
793 pattern + 1, pattern_end))
796 return false; /* Not matched or bad pattern. */
801 while (*pattern == '\\' &&
802 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
804 return filename == filename_end && pattern == pattern_end;
808 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
810 * @filename: The start of string to check.
811 * @filename_end: The end of string to check.
812 * @pattern: The start of pattern to compare.
813 * @pattern_end: The end of pattern to compare.
815 * Returns true if @filename matches @pattern, false otherwise.
817 static bool tomoyo_file_matches_pattern(const char *filename,
818 const char *filename_end,
820 const char *pattern_end)
822 const char *pattern_start = pattern;
826 while (pattern < pattern_end - 1) {
827 /* Split at "\-" pattern. */
828 if (*pattern++ != '\\' || *pattern++ != '-')
830 result = tomoyo_file_matches_pattern2(filename,
839 pattern_start = pattern;
841 result = tomoyo_file_matches_pattern2(filename, filename_end,
842 pattern_start, pattern_end);
843 return first ? result : !result;
847 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
849 * @f: The start of string to check.
850 * @p: The start of pattern to compare.
852 * Returns true if @f matches @p, false otherwise.
854 static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
856 const char *f_delimiter;
857 const char *p_delimiter;
860 f_delimiter = strchr(f, '/');
862 f_delimiter = f + strlen(f);
863 p_delimiter = strchr(p, '/');
865 p_delimiter = p + strlen(p);
866 if (*p == '\\' && *(p + 1) == '{')
868 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
878 /* Ignore trailing "\*" and "\@" in @pattern. */
880 (*(p + 1) == '*' || *(p + 1) == '@'))
885 * The "\{" pattern is permitted only after '/' character.
886 * This guarantees that below "*(p - 1)" is safe.
887 * Also, the "\}" pattern is permitted only before '/' character
888 * so that "\{" + "\}" pair will not break the "\-" operator.
890 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
891 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
892 return false; /* Bad pattern. */
894 /* Compare current component with pattern. */
895 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
898 /* Proceed to next component. */
903 /* Continue comparison. */
904 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
906 f_delimiter = strchr(f, '/');
907 } while (f_delimiter);
908 return false; /* Not matched. */
912 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
914 * @filename: The filename to check.
915 * @pattern: The pattern to compare.
917 * Returns true if matches, false otherwise.
919 * The following patterns are available.
921 * \ooo Octal representation of a byte.
922 * \* Zero or more repetitions of characters other than '/'.
923 * \@ Zero or more repetitions of characters other than '/' or '.'.
924 * \? 1 byte character other than '/'.
925 * \$ One or more repetitions of decimal digits.
926 * \+ 1 decimal digit.
927 * \X One or more repetitions of hexadecimal digits.
928 * \x 1 hexadecimal digit.
929 * \A One or more repetitions of alphabet characters.
930 * \a 1 alphabet character.
932 * \- Subtraction operator.
934 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
937 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
938 const struct tomoyo_path_info *pattern)
940 const char *f = filename->name;
941 const char *p = pattern->name;
942 const int len = pattern->const_len;
944 /* If @pattern doesn't contain pattern, I can use strcmp(). */
945 if (!pattern->is_patterned)
946 return !tomoyo_pathcmp(filename, pattern);
947 /* Don't compare directory and non-directory. */
948 if (filename->is_dir != pattern->is_dir)
950 /* Compare the initial length without patterns. */
951 if (strncmp(f, p, len))
955 return tomoyo_path_matches_pattern2(f, p);
959 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
961 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
963 * This function uses kzalloc(), so the caller must call kfree()
964 * if this function didn't return NULL.
966 const char *tomoyo_get_exe(void)
968 struct file *exe_file;
970 struct mm_struct *mm = current->mm;
974 exe_file = get_mm_exe_file(mm);
978 cp = tomoyo_realpath_from_path(&exe_file->f_path);
984 * tomoyo_get_mode - Get MAC mode.
986 * @ns: Pointer to "struct tomoyo_policy_namespace".
987 * @profile: Profile number.
988 * @index: Index number of functionality.
992 int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
996 struct tomoyo_profile *p;
998 if (!tomoyo_policy_loaded)
999 return TOMOYO_CONFIG_DISABLED;
1000 p = tomoyo_profile(ns, profile);
1001 mode = p->config[index];
1002 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
1003 mode = p->config[tomoyo_index2category[index]
1004 + TOMOYO_MAX_MAC_INDEX];
1005 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
1006 mode = p->default_config;
1011 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
1013 * @r: Pointer to "struct tomoyo_request_info" to initialize.
1014 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
1015 * @index: Index number of functionality.
1019 int tomoyo_init_request_info(struct tomoyo_request_info *r,
1020 struct tomoyo_domain_info *domain, const u8 index)
1024 memset(r, 0, sizeof(*r));
1026 domain = tomoyo_domain();
1028 profile = domain->profile;
1029 r->profile = profile;
1031 r->mode = tomoyo_get_mode(domain->ns, profile, index);
1036 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1038 * @r: Pointer to "struct tomoyo_request_info".
1040 * Returns true if the domain is not exceeded quota, false otherwise.
1042 * Caller holds tomoyo_read_lock().
1044 bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1046 unsigned int count = 0;
1047 struct tomoyo_domain_info *domain = r->domain;
1048 struct tomoyo_acl_info *ptr;
1050 if (r->mode != TOMOYO_CONFIG_LEARNING)
1054 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list,
1055 srcu_read_lock_held(&tomoyo_ss)) {
1059 if (ptr->is_deleted)
1062 * Reading perm bitmap might race with tomoyo_merge_*() because
1063 * caller does not hold tomoyo_policy_lock mutex. But exceeding
1064 * max_learning_entry parameter by a few entries does not harm.
1066 switch (ptr->type) {
1067 case TOMOYO_TYPE_PATH_ACL:
1068 data_race(perm = container_of(ptr, struct tomoyo_path_acl, head)->perm);
1070 case TOMOYO_TYPE_PATH2_ACL:
1071 data_race(perm = container_of(ptr, struct tomoyo_path2_acl, head)->perm);
1073 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1074 data_race(perm = container_of(ptr, struct tomoyo_path_number_acl, head)
1077 case TOMOYO_TYPE_MKDEV_ACL:
1078 data_race(perm = container_of(ptr, struct tomoyo_mkdev_acl, head)->perm);
1080 case TOMOYO_TYPE_INET_ACL:
1081 data_race(perm = container_of(ptr, struct tomoyo_inet_acl, head)->perm);
1083 case TOMOYO_TYPE_UNIX_ACL:
1084 data_race(perm = container_of(ptr, struct tomoyo_unix_acl, head)->perm);
1086 case TOMOYO_TYPE_MANUAL_TASK_ACL:
1092 for (i = 0; i < 16; i++)
1093 if (perm & (1 << i))
1096 if (count < tomoyo_profile(domain->ns, domain->profile)->
1097 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
1099 if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) {
1100 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
1101 /* r->granted = false; */
1102 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
1103 #ifndef CONFIG_SECURITY_TOMOYO_INSECURE_BUILTIN_SETTING
1104 pr_warn("WARNING: Domain '%s' has too many ACLs to hold. Stopped learning mode.\n",
1105 domain->domainname->name);