blob: eba0b3395851e0ae4a3be8be16dc37f28dccd726 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tetsuo Handac3ef1502010-05-17 10:12:46 +09002/*
3 * security/tomoyo/util.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 */
7
8#include <linux/slab.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +01009#include <linux/rculist.h>
10
Tetsuo Handac3ef1502010-05-17 10:12:46 +090011#include "common.h"
12
13/* Lock for protecting policy. */
14DEFINE_MUTEX(tomoyo_policy_lock);
15
16/* Has /sbin/init started? */
17bool tomoyo_policy_loaded;
18
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090019/*
20 * Mapping table from "enum tomoyo_mac_index" to
21 * "enum tomoyo_mac_category_index".
22 */
23const 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,
Tetsuo Handa059d84d2011-09-10 15:23:54 +090048 /* 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,
Tetsuo Handad58e0da2011-09-10 15:22:48 +090079 /* CONFIG::misc group */
80 [TOMOYO_MAC_ENVIRON] = TOMOYO_MAC_CATEGORY_MISC,
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090081};
82
Tetsuo Handac3ef1502010-05-17 10:12:46 +090083/**
Tetsuo Handab22b8b92011-06-26 23:21:50 +090084 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
85 *
86 * @time: Seconds since 1970/01/01 00:00:00.
87 * @stamp: Pointer to "struct tomoyo_time".
88 *
89 * Returns nothing.
Tetsuo Handab22b8b92011-06-26 23:21:50 +090090 */
Arnd Bergmann92734092017-10-19 14:29:04 +020091void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp)
Tetsuo Handab22b8b92011-06-26 23:21:50 +090092{
Arnd Bergmann92734092017-10-19 14:29:04 +020093 struct tm tm;
Tetsuo Handacdcf6722019-01-24 18:37:35 +090094
Arnd Bergmann92734092017-10-19 14:29:04 +020095 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;
Tetsuo Handab22b8b92011-06-26 23:21:50 +0900102}
103
104/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900105 * tomoyo_permstr - Find permission keywords.
106 *
107 * @string: String representation for permissions in foo/bar/buz format.
108 * @keyword: Keyword to find from @string/
109 *
Yangtao Lif09c2962018-11-01 11:55:08 -0400110 * Returns true if @keyword was found in @string, false otherwise.
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900111 *
112 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
113 */
114bool tomoyo_permstr(const char *string, const char *keyword)
115{
116 const char *cp = strstr(string, keyword);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900117
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900118 if (cp)
119 return cp == string || *(cp - 1) == '/';
120 return false;
121}
122
123/**
124 * tomoyo_read_token - Read a word from a line.
125 *
126 * @param: Pointer to "struct tomoyo_acl_param".
127 *
128 * Returns a word on success, "" otherwise.
129 *
130 * To allow the caller to skip NULL check, this function returns "" rather than
131 * NULL if there is no more words to read.
132 */
133char *tomoyo_read_token(struct tomoyo_acl_param *param)
134{
135 char *pos = param->data;
136 char *del = strchr(pos, ' ');
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900137
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900138 if (del)
139 *del++ = '\0';
140 else
141 del = pos + strlen(pos);
142 param->data = del;
143 return pos;
144}
145
146/**
Tetsuo Handa731d37a2011-09-10 15:25:58 +0900147 * tomoyo_get_domainname - Read a domainname from a line.
148 *
149 * @param: Pointer to "struct tomoyo_acl_param".
150 *
151 * Returns a domainname on success, NULL otherwise.
152 */
153const struct tomoyo_path_info *tomoyo_get_domainname
154(struct tomoyo_acl_param *param)
155{
156 char *start = param->data;
157 char *pos = start;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900158
Tetsuo Handa731d37a2011-09-10 15:25:58 +0900159 while (*pos) {
160 if (*pos++ != ' ' || *pos++ == '/')
161 continue;
162 pos -= 2;
163 *pos++ = '\0';
164 break;
165 }
166 param->data = pos;
167 if (tomoyo_correct_domain(start))
168 return tomoyo_get_name(start);
169 return NULL;
170}
171
172/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900173 * tomoyo_parse_ulong - Parse an "unsigned long" value.
174 *
175 * @result: Pointer to "unsigned long".
176 * @str: Pointer to string to parse.
177 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900178 * Returns one of values in "enum tomoyo_value_type".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900179 *
180 * The @src is updated to point the first character after the value
181 * on success.
182 */
Tetsuo Handa2066a362011-07-08 13:21:37 +0900183u8 tomoyo_parse_ulong(unsigned long *result, char **str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900184{
185 const char *cp = *str;
186 char *ep;
187 int base = 10;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900188
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900189 if (*cp == '0') {
190 char c = *(cp + 1);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900191
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900192 if (c == 'x' || c == 'X') {
193 base = 16;
194 cp += 2;
195 } else if (c >= '0' && c <= '7') {
196 base = 8;
197 cp++;
198 }
199 }
200 *result = simple_strtoul(cp, &ep, base);
201 if (cp == ep)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900202 return TOMOYO_VALUE_TYPE_INVALID;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900203 *str = ep;
204 switch (base) {
205 case 16:
206 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
207 case 8:
208 return TOMOYO_VALUE_TYPE_OCTAL;
209 default:
210 return TOMOYO_VALUE_TYPE_DECIMAL;
211 }
212}
213
214/**
215 * tomoyo_print_ulong - Print an "unsigned long" value.
216 *
217 * @buffer: Pointer to buffer.
218 * @buffer_len: Size of @buffer.
219 * @value: An "unsigned long" value.
220 * @type: Type of @value.
221 *
222 * Returns nothing.
223 */
224void tomoyo_print_ulong(char *buffer, const int buffer_len,
225 const unsigned long value, const u8 type)
226{
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);
233 else
234 snprintf(buffer, buffer_len, "type(%u)", type);
235}
236
237/**
238 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
239 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900240 * @param: Pointer to "struct tomoyo_acl_param".
241 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900242 *
243 * Returns true on success, false otherwise.
244 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900245bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900246 struct tomoyo_name_union *ptr)
247{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900248 char *filename;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900249
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900250 if (param->data[0] == '@') {
251 param->data++;
252 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900253 return ptr->group != NULL;
254 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900255 filename = tomoyo_read_token(param);
256 if (!tomoyo_correct_word(filename))
257 return false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900258 ptr->filename = tomoyo_get_name(filename);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900259 return ptr->filename != NULL;
260}
261
262/**
263 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
264 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900265 * @param: Pointer to "struct tomoyo_acl_param".
266 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900267 *
268 * Returns true on success, false otherwise.
269 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900270bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
271 struct tomoyo_number_union *ptr)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900272{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900273 char *data;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900274 u8 type;
275 unsigned long v;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900276
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900277 memset(ptr, 0, sizeof(*ptr));
278 if (param->data[0] == '@') {
279 param->data++;
280 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
281 return ptr->group != NULL;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900282 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900283 data = tomoyo_read_token(param);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900284 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900285 if (type == TOMOYO_VALUE_TYPE_INVALID)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900286 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900287 ptr->values[0] = v;
288 ptr->value_type[0] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900289 if (!*data) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900290 ptr->values[1] = v;
291 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900292 return true;
293 }
294 if (*data++ != '-')
295 return false;
296 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900297 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900298 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900299 ptr->values[1] = v;
300 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900301 return true;
302}
303
304/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900305 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900306 *
307 * @str: Pointer to the string.
308 *
309 * Returns true if @str is a \ooo style octal value, false otherwise.
310 *
311 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
312 * This function verifies that \ooo is in valid range.
313 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900314static inline bool tomoyo_byte_range(const char *str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900315{
316 return *str >= '0' && *str++ <= '3' &&
317 *str >= '0' && *str++ <= '7' &&
318 *str >= '0' && *str <= '7';
319}
320
321/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900322 * tomoyo_alphabet_char - Check whether the character is an alphabet.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900323 *
324 * @c: The character to check.
325 *
326 * Returns true if @c is an alphabet character, false otherwise.
327 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900328static inline bool tomoyo_alphabet_char(const char c)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900329{
330 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
331}
332
333/**
334 * tomoyo_make_byte - Make byte value from three octal characters.
335 *
336 * @c1: The first character.
337 * @c2: The second character.
338 * @c3: The third character.
339 *
340 * Returns byte value.
341 */
342static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
343{
344 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
345}
346
347/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900348 * tomoyo_valid - Check whether the character is a valid char.
349 *
350 * @c: The character to check.
351 *
352 * Returns true if @c is a valid character, false otherwise.
353 */
354static inline bool tomoyo_valid(const unsigned char c)
355{
356 return c > ' ' && c < 127;
357}
358
359/**
360 * tomoyo_invalid - Check whether the character is an invalid char.
361 *
362 * @c: The character to check.
363 *
364 * Returns true if @c is an invalid character, false otherwise.
365 */
366static inline bool tomoyo_invalid(const unsigned char c)
367{
368 return c && (c <= ' ' || c >= 127);
369}
370
371/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900372 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
373 *
374 * @src: Pointer to pointer to the string.
375 * @find: Pointer to the keyword.
376 *
377 * Returns true if @src starts with @find, false otherwise.
378 *
379 * The @src is updated to point the first character after the @find
380 * if @src starts with @find.
381 */
382bool tomoyo_str_starts(char **src, const char *find)
383{
384 const int len = strlen(find);
385 char *tmp = *src;
386
387 if (strncmp(tmp, find, len))
388 return false;
389 tmp += len;
390 *src = tmp;
391 return true;
392}
393
394/**
395 * tomoyo_normalize_line - Format string.
396 *
397 * @buffer: The line to normalize.
398 *
399 * Leading and trailing whitespaces are removed.
400 * Multiple whitespaces are packed into single space.
401 *
402 * Returns nothing.
403 */
404void tomoyo_normalize_line(unsigned char *buffer)
405{
406 unsigned char *sp = buffer;
407 unsigned char *dp = buffer;
408 bool first = true;
409
Tetsuo Handa75093152010-06-16 16:23:55 +0900410 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900411 sp++;
412 while (*sp) {
413 if (!first)
414 *dp++ = ' ';
415 first = false;
Tetsuo Handa75093152010-06-16 16:23:55 +0900416 while (tomoyo_valid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900417 *dp++ = *sp++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900418 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900419 sp++;
420 }
421 *dp = '\0';
422}
423
424/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900425 * tomoyo_correct_word2 - Validate a string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900426 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900427 * @string: The string to check. Maybe non-'\0'-terminated.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900428 * @len: Length of @string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900429 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900430 * Check whether the given string follows the naming rules.
431 * Returns true if @string follows the naming rules, false otherwise.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900432 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900433static bool tomoyo_correct_word2(const char *string, size_t len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900434{
Tetsuo Handa3f629632010-06-03 20:37:26 +0900435 const char *const start = string;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900436 bool in_repetition = false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900437 unsigned char c;
438 unsigned char d;
439 unsigned char e;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900440
Tetsuo Handa3f629632010-06-03 20:37:26 +0900441 if (!len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900442 goto out;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900443 while (len--) {
444 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900445 if (c == '\\') {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900446 if (!len--)
447 goto out;
448 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900449 switch (c) {
450 case '\\': /* "\\" */
451 continue;
452 case '$': /* "\$" */
453 case '+': /* "\+" */
454 case '?': /* "\?" */
455 case '*': /* "\*" */
456 case '@': /* "\@" */
457 case 'x': /* "\x" */
458 case 'X': /* "\X" */
459 case 'a': /* "\a" */
460 case 'A': /* "\A" */
461 case '-': /* "\-" */
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900462 continue;
463 case '{': /* "/\{" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900464 if (string - 3 < start || *(string - 3) != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900465 break;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900466 in_repetition = true;
467 continue;
468 case '}': /* "\}/" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900469 if (*string != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900470 break;
471 if (!in_repetition)
472 break;
473 in_repetition = false;
474 continue;
475 case '0': /* "\ooo" */
476 case '1':
477 case '2':
478 case '3':
Tetsuo Handa3f629632010-06-03 20:37:26 +0900479 if (!len-- || !len--)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900480 break;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900481 d = *string++;
482 e = *string++;
483 if (d < '0' || d > '7' || e < '0' || e > '7')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900484 break;
485 c = tomoyo_make_byte(c, d, e);
Tetsuo Handa25add8c2012-01-15 11:05:59 +0900486 if (c <= ' ' || c >= 127)
487 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900488 }
489 goto out;
490 } else if (in_repetition && c == '/') {
491 goto out;
Tetsuo Handa25add8c2012-01-15 11:05:59 +0900492 } else if (c <= ' ' || c >= 127) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900493 goto out;
494 }
495 }
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900496 if (in_repetition)
497 goto out;
498 return true;
499 out:
500 return false;
501}
502
503/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900504 * tomoyo_correct_word - Validate a string.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900505 *
506 * @string: The string to check.
507 *
508 * Check whether the given string follows the naming rules.
509 * Returns true if @string follows the naming rules, false otherwise.
510 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900511bool tomoyo_correct_word(const char *string)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900512{
Tetsuo Handa75093152010-06-16 16:23:55 +0900513 return tomoyo_correct_word2(string, strlen(string));
Tetsuo Handa3f629632010-06-03 20:37:26 +0900514}
515
516/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900517 * tomoyo_correct_path - Validate a pathname.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900518 *
519 * @filename: The pathname to check.
520 *
521 * Check whether the given pathname follows the naming rules.
522 * Returns true if @filename follows the naming rules, false otherwise.
523 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900524bool tomoyo_correct_path(const char *filename)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900525{
Tetsuo Handa75093152010-06-16 16:23:55 +0900526 return *filename == '/' && tomoyo_correct_word(filename);
Tetsuo Handa3f629632010-06-03 20:37:26 +0900527}
528
529/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900530 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900531 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900532 * @domainname: The domainname to check.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900533 *
534 * Returns true if @domainname follows the naming rules, false otherwise.
535 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900536bool tomoyo_correct_domain(const unsigned char *domainname)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900537{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900538 if (!domainname || !tomoyo_domain_def(domainname))
539 return false;
540 domainname = strchr(domainname, ' ');
541 if (!domainname++)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900542 return true;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900543 while (1) {
544 const unsigned char *cp = strchr(domainname, ' ');
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900545
Tetsuo Handa3f629632010-06-03 20:37:26 +0900546 if (!cp)
547 break;
548 if (*domainname != '/' ||
Tetsuo Handae77dc342011-05-12 06:40:51 +0900549 !tomoyo_correct_word2(domainname, cp - domainname))
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900550 return false;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900551 domainname = cp + 1;
552 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900553 return tomoyo_correct_path(domainname);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900554}
555
556/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900557 * tomoyo_domain_def - Check whether the given token can be a domainname.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900558 *
559 * @buffer: The token to check.
560 *
561 * Returns true if @buffer possibly be a domainname, false otherwise.
562 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900563bool tomoyo_domain_def(const unsigned char *buffer)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900564{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900565 const unsigned char *cp;
566 int len;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900567
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900568 if (*buffer != '<')
569 return false;
570 cp = strchr(buffer, ' ');
571 if (!cp)
572 len = strlen(buffer);
573 else
574 len = cp - buffer;
575 if (buffer[len - 1] != '>' ||
576 !tomoyo_correct_word2(buffer + 1, len - 2))
577 return false;
578 return true;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900579}
580
581/**
582 * tomoyo_find_domain - Find a domain by the given name.
583 *
584 * @domainname: The domainname to find.
585 *
586 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
587 *
588 * Caller holds tomoyo_read_lock().
589 */
590struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
591{
592 struct tomoyo_domain_info *domain;
593 struct tomoyo_path_info name;
594
595 name.name = domainname;
596 tomoyo_fill_path_info(&name);
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +0900597 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list,
598 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900599 if (!domain->is_deleted &&
600 !tomoyo_pathcmp(&name, domain->domainname))
601 return domain;
602 }
603 return NULL;
604}
605
606/**
607 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
608 *
609 * @filename: The string to evaluate.
610 *
611 * Returns the initial length without a pattern in @filename.
612 */
613static int tomoyo_const_part_length(const char *filename)
614{
615 char c;
616 int len = 0;
617
618 if (!filename)
619 return 0;
620 while ((c = *filename++) != '\0') {
621 if (c != '\\') {
622 len++;
623 continue;
624 }
625 c = *filename++;
626 switch (c) {
627 case '\\': /* "\\" */
628 len += 2;
629 continue;
630 case '0': /* "\ooo" */
631 case '1':
632 case '2':
633 case '3':
634 c = *filename++;
635 if (c < '0' || c > '7')
636 break;
637 c = *filename++;
638 if (c < '0' || c > '7')
639 break;
640 len += 4;
641 continue;
642 }
643 break;
644 }
645 return len;
646}
647
648/**
649 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
650 *
651 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
652 *
653 * The caller sets "struct tomoyo_path_info"->name.
654 */
655void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
656{
657 const char *name = ptr->name;
658 const int len = strlen(name);
659
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);
Linus Torvalds8387ff22016-06-10 07:51:30 -0700663 ptr->hash = full_name_hash(NULL, name, len);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900664}
665
666/**
667 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
668 *
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.
673 *
674 * Returns true if @filename matches @pattern, false otherwise.
675 */
676static bool tomoyo_file_matches_pattern2(const char *filename,
677 const char *filename_end,
678 const char *pattern,
679 const char *pattern_end)
680{
681 while (filename < filename_end && pattern < pattern_end) {
682 char c;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900683 int i;
684 int j;
685
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900686 if (*pattern != '\\') {
687 if (*filename++ != *pattern++)
688 return false;
689 continue;
690 }
691 c = *filename;
692 pattern++;
693 switch (*pattern) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900694 case '?':
695 if (c == '/') {
696 return false;
697 } else if (c == '\\') {
698 if (filename[1] == '\\')
699 filename++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900700 else if (tomoyo_byte_range(filename + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900701 filename += 3;
702 else
703 return false;
704 }
705 break;
706 case '\\':
707 if (c != '\\')
708 return false;
709 if (*++filename != '\\')
710 return false;
711 break;
712 case '+':
713 if (!isdigit(c))
714 return false;
715 break;
716 case 'x':
717 if (!isxdigit(c))
718 return false;
719 break;
720 case 'a':
Tetsuo Handa75093152010-06-16 16:23:55 +0900721 if (!tomoyo_alphabet_char(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900722 return false;
723 break;
724 case '0':
725 case '1':
726 case '2':
727 case '3':
Tetsuo Handa75093152010-06-16 16:23:55 +0900728 if (c == '\\' && tomoyo_byte_range(filename + 1)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900729 && strncmp(filename + 1, pattern, 3) == 0) {
730 filename += 3;
731 pattern += 2;
732 break;
733 }
734 return false; /* Not matched. */
735 case '*':
736 case '@':
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))
741 return true;
742 c = filename[i];
743 if (c == '.' && *pattern == '@')
744 break;
745 if (c != '\\')
746 continue;
747 if (filename[i + 1] == '\\')
748 i++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900749 else if (tomoyo_byte_range(filename + i + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900750 i += 3;
751 else
752 break; /* Bad pattern. */
753 }
754 return false; /* Not matched. */
755 default:
756 j = 0;
757 c = *pattern;
758 if (c == '$') {
759 while (isdigit(filename[j]))
760 j++;
761 } else if (c == 'X') {
762 while (isxdigit(filename[j]))
763 j++;
764 } else if (c == 'A') {
Tetsuo Handa75093152010-06-16 16:23:55 +0900765 while (tomoyo_alphabet_char(filename[j]))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900766 j++;
767 }
768 for (i = 1; i <= j; i++) {
769 if (tomoyo_file_matches_pattern2(
770 filename + i, filename_end,
771 pattern + 1, pattern_end))
772 return true;
773 }
774 return false; /* Not matched or bad pattern. */
775 }
776 filename++;
777 pattern++;
778 }
779 while (*pattern == '\\' &&
780 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
781 pattern += 2;
782 return filename == filename_end && pattern == pattern_end;
783}
784
785/**
786 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
787 *
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.
792 *
793 * Returns true if @filename matches @pattern, false otherwise.
794 */
795static bool tomoyo_file_matches_pattern(const char *filename,
796 const char *filename_end,
797 const char *pattern,
798 const char *pattern_end)
799{
800 const char *pattern_start = pattern;
801 bool first = true;
802 bool result;
803
804 while (pattern < pattern_end - 1) {
805 /* Split at "\-" pattern. */
806 if (*pattern++ != '\\' || *pattern++ != '-')
807 continue;
808 result = tomoyo_file_matches_pattern2(filename,
809 filename_end,
810 pattern_start,
811 pattern - 2);
812 if (first)
813 result = !result;
814 if (result)
815 return false;
816 first = false;
817 pattern_start = pattern;
818 }
819 result = tomoyo_file_matches_pattern2(filename, filename_end,
820 pattern_start, pattern_end);
821 return first ? result : !result;
822}
823
824/**
825 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
826 *
827 * @f: The start of string to check.
828 * @p: The start of pattern to compare.
829 *
830 * Returns true if @f matches @p, false otherwise.
831 */
832static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
833{
834 const char *f_delimiter;
835 const char *p_delimiter;
836
837 while (*f && *p) {
838 f_delimiter = strchr(f, '/');
839 if (!f_delimiter)
840 f_delimiter = f + strlen(f);
841 p_delimiter = strchr(p, '/');
842 if (!p_delimiter)
843 p_delimiter = p + strlen(p);
844 if (*p == '\\' && *(p + 1) == '{')
845 goto recursive;
846 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
847 p_delimiter))
848 return false;
849 f = f_delimiter;
850 if (*f)
851 f++;
852 p = p_delimiter;
853 if (*p)
854 p++;
855 }
856 /* Ignore trailing "\*" and "\@" in @pattern. */
857 while (*p == '\\' &&
858 (*(p + 1) == '*' || *(p + 1) == '@'))
859 p += 2;
860 return !*f && !*p;
861 recursive:
862 /*
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.
867 */
868 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
869 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
870 return false; /* Bad pattern. */
871 do {
872 /* Compare current component with pattern. */
873 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
874 p_delimiter - 2))
875 break;
876 /* Proceed to next component. */
877 f = f_delimiter;
878 if (!*f)
879 break;
880 f++;
881 /* Continue comparison. */
882 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
883 return true;
884 f_delimiter = strchr(f, '/');
885 } while (f_delimiter);
886 return false; /* Not matched. */
887}
888
889/**
890 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
891 *
892 * @filename: The filename to check.
893 * @pattern: The pattern to compare.
894 *
895 * Returns true if matches, false otherwise.
896 *
897 * The following patterns are available.
898 * \\ \ itself.
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.
909 *
910 * \- Subtraction operator.
911 *
912 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
913 * /dir/dir/dir/ ).
914 */
915bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
916 const struct tomoyo_path_info *pattern)
917{
918 const char *f = filename->name;
919 const char *p = pattern->name;
920 const int len = pattern->const_len;
921
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)
927 return false;
928 /* Compare the initial length without patterns. */
929 if (strncmp(f, p, len))
930 return false;
931 f += len;
932 p += len;
933 return tomoyo_path_matches_pattern2(f, p);
934}
935
936/**
937 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
938 *
939 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
940 *
941 * This function uses kzalloc(), so the caller must call kfree()
942 * if this function didn't return NULL.
943 */
944const char *tomoyo_get_exe(void)
945{
Davidlohr Buesod4144ea2015-04-16 12:49:18 -0700946 struct file *exe_file;
947 const char *cp;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900948 struct mm_struct *mm = current->mm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900949
950 if (!mm)
951 return NULL;
Davidlohr Buesod4144ea2015-04-16 12:49:18 -0700952 exe_file = get_mm_exe_file(mm);
953 if (!exe_file)
954 return NULL;
955
956 cp = tomoyo_realpath_from_path(&exe_file->f_path);
957 fput(exe_file);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900958 return cp;
959}
960
961/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900962 * tomoyo_get_mode - Get MAC mode.
963 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900964 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900965 * @profile: Profile number.
966 * @index: Index number of functionality.
967 *
968 * Returns mode.
969 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900970int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
971 const u8 index)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900972{
973 u8 mode;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900974 struct tomoyo_profile *p;
975
Tetsuo Handa57c25902010-06-03 20:38:44 +0900976 if (!tomoyo_policy_loaded)
977 return TOMOYO_CONFIG_DISABLED;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900978 p = tomoyo_profile(ns, profile);
979 mode = p->config[index];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900980 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900981 mode = p->config[tomoyo_index2category[index]
982 + TOMOYO_MAX_MAC_INDEX];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900983 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900984 mode = p->default_config;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900985 return mode & 3;
986}
987
988/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900989 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
990 *
991 * @r: Pointer to "struct tomoyo_request_info" to initialize.
992 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
Tetsuo Handa57c25902010-06-03 20:38:44 +0900993 * @index: Index number of functionality.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900994 *
995 * Returns mode.
996 */
997int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900998 struct tomoyo_domain_info *domain, const u8 index)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900999{
Tetsuo Handa57c25902010-06-03 20:38:44 +09001000 u8 profile;
Tetsuo Handacdcf6722019-01-24 18:37:35 +09001001
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001002 memset(r, 0, sizeof(*r));
1003 if (!domain)
1004 domain = tomoyo_domain();
1005 r->domain = domain;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001006 profile = domain->profile;
1007 r->profile = profile;
1008 r->type = index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001009 r->mode = tomoyo_get_mode(domain->ns, profile, index);
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001010 return r->mode;
1011}
1012
1013/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001014 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1015 *
1016 * @r: Pointer to "struct tomoyo_request_info".
1017 *
1018 * Returns true if the domain is not exceeded quota, false otherwise.
1019 *
1020 * Caller holds tomoyo_read_lock().
1021 */
1022bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1023{
1024 unsigned int count = 0;
1025 struct tomoyo_domain_info *domain = r->domain;
1026 struct tomoyo_acl_info *ptr;
1027
1028 if (r->mode != TOMOYO_CONFIG_LEARNING)
1029 return false;
1030 if (!domain)
1031 return true;
Tetsuo Handa6bd5ce62019-12-16 19:16:48 +09001032 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list,
1033 srcu_read_lock_held(&tomoyo_ss)) {
Tetsuo Handa7c759642011-06-26 23:15:31 +09001034 u16 perm;
1035 u8 i;
Tetsuo Handacdcf6722019-01-24 18:37:35 +09001036
Tetsuo Handa7e3d1992010-07-27 10:08:29 +09001037 if (ptr->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +09001038 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001039 switch (ptr->type) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001040 case TOMOYO_TYPE_PATH_ACL:
1041 perm = container_of(ptr, struct tomoyo_path_acl, head)
1042 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001043 break;
1044 case TOMOYO_TYPE_PATH2_ACL:
1045 perm = container_of(ptr, struct tomoyo_path2_acl, head)
1046 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001047 break;
1048 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1049 perm = container_of(ptr, struct tomoyo_path_number_acl,
1050 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001051 break;
Tetsuo Handa75093152010-06-16 16:23:55 +09001052 case TOMOYO_TYPE_MKDEV_ACL:
1053 perm = container_of(ptr, struct tomoyo_mkdev_acl,
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001054 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001055 break;
Tetsuo Handa545a7262011-10-11 14:06:41 +09001056 case TOMOYO_TYPE_INET_ACL:
1057 perm = container_of(ptr, struct tomoyo_inet_acl,
1058 head)->perm;
1059 break;
1060 case TOMOYO_TYPE_UNIX_ACL:
1061 perm = container_of(ptr, struct tomoyo_unix_acl,
1062 head)->perm;
1063 break;
1064 case TOMOYO_TYPE_MANUAL_TASK_ACL:
1065 perm = 0;
1066 break;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001067 default:
Tetsuo Handa7c759642011-06-26 23:15:31 +09001068 perm = 1;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001069 }
Tetsuo Handa7c759642011-06-26 23:15:31 +09001070 for (i = 0; i < 16; i++)
1071 if (perm & (1 << i))
1072 count++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001073 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001074 if (count < tomoyo_profile(domain->ns, domain->profile)->
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001075 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001076 return true;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001077 if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) {
1078 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
1079 /* r->granted = false; */
1080 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
Tetsuo Handa4ad98ac2019-05-07 20:34:22 +09001081#ifndef CONFIG_SECURITY_TOMOYO_INSECURE_BUILTIN_SETTING
Tetsuo Handacdcf6722019-01-24 18:37:35 +09001082 pr_warn("WARNING: Domain '%s' has too many ACLs to hold. Stopped learning mode.\n",
1083 domain->domainname->name);
Tetsuo Handa4ad98ac2019-05-07 20:34:22 +09001084#endif
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001085 }
1086 return false;
1087}