blob: 5fe3679137aeb76297305e3e73d7defe3ed4c004 [file] [log] [blame]
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001/*
2 * security/tomoyo/util.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handac3ef1502010-05-17 10:12:46 +09005 */
6
7#include <linux/slab.h>
8#include "common.h"
9
10/* Lock for protecting policy. */
11DEFINE_MUTEX(tomoyo_policy_lock);
12
13/* Has /sbin/init started? */
14bool tomoyo_policy_loaded;
15
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090016/*
17 * Mapping table from "enum tomoyo_mac_index" to
18 * "enum tomoyo_mac_category_index".
19 */
20const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = {
21 /* CONFIG::file group */
22 [TOMOYO_MAC_FILE_EXECUTE] = TOMOYO_MAC_CATEGORY_FILE,
23 [TOMOYO_MAC_FILE_OPEN] = TOMOYO_MAC_CATEGORY_FILE,
24 [TOMOYO_MAC_FILE_CREATE] = TOMOYO_MAC_CATEGORY_FILE,
25 [TOMOYO_MAC_FILE_UNLINK] = TOMOYO_MAC_CATEGORY_FILE,
26 [TOMOYO_MAC_FILE_GETATTR] = TOMOYO_MAC_CATEGORY_FILE,
27 [TOMOYO_MAC_FILE_MKDIR] = TOMOYO_MAC_CATEGORY_FILE,
28 [TOMOYO_MAC_FILE_RMDIR] = TOMOYO_MAC_CATEGORY_FILE,
29 [TOMOYO_MAC_FILE_MKFIFO] = TOMOYO_MAC_CATEGORY_FILE,
30 [TOMOYO_MAC_FILE_MKSOCK] = TOMOYO_MAC_CATEGORY_FILE,
31 [TOMOYO_MAC_FILE_TRUNCATE] = TOMOYO_MAC_CATEGORY_FILE,
32 [TOMOYO_MAC_FILE_SYMLINK] = TOMOYO_MAC_CATEGORY_FILE,
33 [TOMOYO_MAC_FILE_MKBLOCK] = TOMOYO_MAC_CATEGORY_FILE,
34 [TOMOYO_MAC_FILE_MKCHAR] = TOMOYO_MAC_CATEGORY_FILE,
35 [TOMOYO_MAC_FILE_LINK] = TOMOYO_MAC_CATEGORY_FILE,
36 [TOMOYO_MAC_FILE_RENAME] = TOMOYO_MAC_CATEGORY_FILE,
37 [TOMOYO_MAC_FILE_CHMOD] = TOMOYO_MAC_CATEGORY_FILE,
38 [TOMOYO_MAC_FILE_CHOWN] = TOMOYO_MAC_CATEGORY_FILE,
39 [TOMOYO_MAC_FILE_CHGRP] = TOMOYO_MAC_CATEGORY_FILE,
40 [TOMOYO_MAC_FILE_IOCTL] = TOMOYO_MAC_CATEGORY_FILE,
41 [TOMOYO_MAC_FILE_CHROOT] = TOMOYO_MAC_CATEGORY_FILE,
42 [TOMOYO_MAC_FILE_MOUNT] = TOMOYO_MAC_CATEGORY_FILE,
43 [TOMOYO_MAC_FILE_UMOUNT] = TOMOYO_MAC_CATEGORY_FILE,
44 [TOMOYO_MAC_FILE_PIVOT_ROOT] = TOMOYO_MAC_CATEGORY_FILE,
Tetsuo Handa059d84d2011-09-10 15:23:54 +090045 /* CONFIG::network group */
46 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND] =
47 TOMOYO_MAC_CATEGORY_NETWORK,
48 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN] =
49 TOMOYO_MAC_CATEGORY_NETWORK,
50 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT] =
51 TOMOYO_MAC_CATEGORY_NETWORK,
52 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND] =
53 TOMOYO_MAC_CATEGORY_NETWORK,
54 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND] =
55 TOMOYO_MAC_CATEGORY_NETWORK,
56 [TOMOYO_MAC_NETWORK_INET_RAW_BIND] =
57 TOMOYO_MAC_CATEGORY_NETWORK,
58 [TOMOYO_MAC_NETWORK_INET_RAW_SEND] =
59 TOMOYO_MAC_CATEGORY_NETWORK,
60 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND] =
61 TOMOYO_MAC_CATEGORY_NETWORK,
62 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN] =
63 TOMOYO_MAC_CATEGORY_NETWORK,
64 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT] =
65 TOMOYO_MAC_CATEGORY_NETWORK,
66 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND] =
67 TOMOYO_MAC_CATEGORY_NETWORK,
68 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND] =
69 TOMOYO_MAC_CATEGORY_NETWORK,
70 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND] =
71 TOMOYO_MAC_CATEGORY_NETWORK,
72 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] =
73 TOMOYO_MAC_CATEGORY_NETWORK,
74 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] =
75 TOMOYO_MAC_CATEGORY_NETWORK,
Tetsuo Handad58e0da2011-09-10 15:22:48 +090076 /* CONFIG::misc group */
77 [TOMOYO_MAC_ENVIRON] = TOMOYO_MAC_CATEGORY_MISC,
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090078};
79
Tetsuo Handac3ef1502010-05-17 10:12:46 +090080/**
Tetsuo Handab22b8b92011-06-26 23:21:50 +090081 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
82 *
83 * @time: Seconds since 1970/01/01 00:00:00.
84 * @stamp: Pointer to "struct tomoyo_time".
85 *
86 * Returns nothing.
87 *
88 * This function does not handle Y2038 problem.
89 */
90void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp)
91{
92 static const u16 tomoyo_eom[2][12] = {
93 { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
94 { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
95 };
96 u16 y;
97 u8 m;
98 bool r;
99 stamp->sec = time % 60;
100 time /= 60;
101 stamp->min = time % 60;
102 time /= 60;
103 stamp->hour = time % 24;
104 time /= 24;
105 for (y = 1970; ; y++) {
106 const unsigned short days = (y & 3) ? 365 : 366;
107 if (time < days)
108 break;
109 time -= days;
110 }
111 r = (y & 3) == 0;
112 for (m = 0; m < 11 && time >= tomoyo_eom[r][m]; m++)
113 ;
114 if (m)
115 time -= tomoyo_eom[r][m - 1];
116 stamp->year = y;
117 stamp->month = ++m;
118 stamp->day = ++time;
119}
120
121/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900122 * tomoyo_permstr - Find permission keywords.
123 *
124 * @string: String representation for permissions in foo/bar/buz format.
125 * @keyword: Keyword to find from @string/
126 *
127 * Returns ture if @keyword was found in @string, false otherwise.
128 *
129 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
130 */
131bool tomoyo_permstr(const char *string, const char *keyword)
132{
133 const char *cp = strstr(string, keyword);
134 if (cp)
135 return cp == string || *(cp - 1) == '/';
136 return false;
137}
138
139/**
140 * tomoyo_read_token - Read a word from a line.
141 *
142 * @param: Pointer to "struct tomoyo_acl_param".
143 *
144 * Returns a word on success, "" otherwise.
145 *
146 * To allow the caller to skip NULL check, this function returns "" rather than
147 * NULL if there is no more words to read.
148 */
149char *tomoyo_read_token(struct tomoyo_acl_param *param)
150{
151 char *pos = param->data;
152 char *del = strchr(pos, ' ');
153 if (del)
154 *del++ = '\0';
155 else
156 del = pos + strlen(pos);
157 param->data = del;
158 return pos;
159}
160
161/**
Tetsuo Handa731d37a2011-09-10 15:25:58 +0900162 * tomoyo_get_domainname - Read a domainname from a line.
163 *
164 * @param: Pointer to "struct tomoyo_acl_param".
165 *
166 * Returns a domainname on success, NULL otherwise.
167 */
168const struct tomoyo_path_info *tomoyo_get_domainname
169(struct tomoyo_acl_param *param)
170{
171 char *start = param->data;
172 char *pos = start;
173 while (*pos) {
174 if (*pos++ != ' ' || *pos++ == '/')
175 continue;
176 pos -= 2;
177 *pos++ = '\0';
178 break;
179 }
180 param->data = pos;
181 if (tomoyo_correct_domain(start))
182 return tomoyo_get_name(start);
183 return NULL;
184}
185
186/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900187 * tomoyo_parse_ulong - Parse an "unsigned long" value.
188 *
189 * @result: Pointer to "unsigned long".
190 * @str: Pointer to string to parse.
191 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900192 * Returns one of values in "enum tomoyo_value_type".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900193 *
194 * The @src is updated to point the first character after the value
195 * on success.
196 */
Tetsuo Handa2066a362011-07-08 13:21:37 +0900197u8 tomoyo_parse_ulong(unsigned long *result, char **str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900198{
199 const char *cp = *str;
200 char *ep;
201 int base = 10;
202 if (*cp == '0') {
203 char c = *(cp + 1);
204 if (c == 'x' || c == 'X') {
205 base = 16;
206 cp += 2;
207 } else if (c >= '0' && c <= '7') {
208 base = 8;
209 cp++;
210 }
211 }
212 *result = simple_strtoul(cp, &ep, base);
213 if (cp == ep)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900214 return TOMOYO_VALUE_TYPE_INVALID;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900215 *str = ep;
216 switch (base) {
217 case 16:
218 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
219 case 8:
220 return TOMOYO_VALUE_TYPE_OCTAL;
221 default:
222 return TOMOYO_VALUE_TYPE_DECIMAL;
223 }
224}
225
226/**
227 * tomoyo_print_ulong - Print an "unsigned long" value.
228 *
229 * @buffer: Pointer to buffer.
230 * @buffer_len: Size of @buffer.
231 * @value: An "unsigned long" value.
232 * @type: Type of @value.
233 *
234 * Returns nothing.
235 */
236void tomoyo_print_ulong(char *buffer, const int buffer_len,
237 const unsigned long value, const u8 type)
238{
239 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
240 snprintf(buffer, buffer_len, "%lu", value);
241 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
242 snprintf(buffer, buffer_len, "0%lo", value);
243 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
244 snprintf(buffer, buffer_len, "0x%lX", value);
245 else
246 snprintf(buffer, buffer_len, "type(%u)", type);
247}
248
249/**
250 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
251 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900252 * @param: Pointer to "struct tomoyo_acl_param".
253 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900254 *
255 * Returns true on success, false otherwise.
256 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900257bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900258 struct tomoyo_name_union *ptr)
259{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900260 char *filename;
261 if (param->data[0] == '@') {
262 param->data++;
263 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900264 return ptr->group != NULL;
265 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900266 filename = tomoyo_read_token(param);
267 if (!tomoyo_correct_word(filename))
268 return false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900269 ptr->filename = tomoyo_get_name(filename);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900270 return ptr->filename != NULL;
271}
272
273/**
274 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
275 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900276 * @param: Pointer to "struct tomoyo_acl_param".
277 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900278 *
279 * Returns true on success, false otherwise.
280 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900281bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
282 struct tomoyo_number_union *ptr)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900283{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900284 char *data;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900285 u8 type;
286 unsigned long v;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900287 memset(ptr, 0, sizeof(*ptr));
288 if (param->data[0] == '@') {
289 param->data++;
290 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
291 return ptr->group != NULL;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900292 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900293 data = tomoyo_read_token(param);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900294 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900295 if (type == TOMOYO_VALUE_TYPE_INVALID)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900296 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900297 ptr->values[0] = v;
298 ptr->value_type[0] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900299 if (!*data) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900300 ptr->values[1] = v;
301 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900302 return true;
303 }
304 if (*data++ != '-')
305 return false;
306 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900307 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900308 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900309 ptr->values[1] = v;
310 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900311 return true;
312}
313
314/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900315 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900316 *
317 * @str: Pointer to the string.
318 *
319 * Returns true if @str is a \ooo style octal value, false otherwise.
320 *
321 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
322 * This function verifies that \ooo is in valid range.
323 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900324static inline bool tomoyo_byte_range(const char *str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900325{
326 return *str >= '0' && *str++ <= '3' &&
327 *str >= '0' && *str++ <= '7' &&
328 *str >= '0' && *str <= '7';
329}
330
331/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900332 * tomoyo_alphabet_char - Check whether the character is an alphabet.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900333 *
334 * @c: The character to check.
335 *
336 * Returns true if @c is an alphabet character, false otherwise.
337 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900338static inline bool tomoyo_alphabet_char(const char c)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900339{
340 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
341}
342
343/**
344 * tomoyo_make_byte - Make byte value from three octal characters.
345 *
346 * @c1: The first character.
347 * @c2: The second character.
348 * @c3: The third character.
349 *
350 * Returns byte value.
351 */
352static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
353{
354 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
355}
356
357/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900358 * tomoyo_valid - Check whether the character is a valid char.
359 *
360 * @c: The character to check.
361 *
362 * Returns true if @c is a valid character, false otherwise.
363 */
364static inline bool tomoyo_valid(const unsigned char c)
365{
366 return c > ' ' && c < 127;
367}
368
369/**
370 * tomoyo_invalid - Check whether the character is an invalid char.
371 *
372 * @c: The character to check.
373 *
374 * Returns true if @c is an invalid character, false otherwise.
375 */
376static inline bool tomoyo_invalid(const unsigned char c)
377{
378 return c && (c <= ' ' || c >= 127);
379}
380
381/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900382 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
383 *
384 * @src: Pointer to pointer to the string.
385 * @find: Pointer to the keyword.
386 *
387 * Returns true if @src starts with @find, false otherwise.
388 *
389 * The @src is updated to point the first character after the @find
390 * if @src starts with @find.
391 */
392bool tomoyo_str_starts(char **src, const char *find)
393{
394 const int len = strlen(find);
395 char *tmp = *src;
396
397 if (strncmp(tmp, find, len))
398 return false;
399 tmp += len;
400 *src = tmp;
401 return true;
402}
403
404/**
405 * tomoyo_normalize_line - Format string.
406 *
407 * @buffer: The line to normalize.
408 *
409 * Leading and trailing whitespaces are removed.
410 * Multiple whitespaces are packed into single space.
411 *
412 * Returns nothing.
413 */
414void tomoyo_normalize_line(unsigned char *buffer)
415{
416 unsigned char *sp = buffer;
417 unsigned char *dp = buffer;
418 bool first = true;
419
Tetsuo Handa75093152010-06-16 16:23:55 +0900420 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900421 sp++;
422 while (*sp) {
423 if (!first)
424 *dp++ = ' ';
425 first = false;
Tetsuo Handa75093152010-06-16 16:23:55 +0900426 while (tomoyo_valid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900427 *dp++ = *sp++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900428 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900429 sp++;
430 }
431 *dp = '\0';
432}
433
434/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900435 * tomoyo_correct_word2 - Validate a string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900436 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900437 * @string: The string to check. Maybe non-'\0'-terminated.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900438 * @len: Length of @string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900439 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900440 * Check whether the given string follows the naming rules.
441 * Returns true if @string follows the naming rules, false otherwise.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900442 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900443static bool tomoyo_correct_word2(const char *string, size_t len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900444{
Tetsuo Handa3f629632010-06-03 20:37:26 +0900445 const char *const start = string;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900446 bool in_repetition = false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900447 unsigned char c;
448 unsigned char d;
449 unsigned char e;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900450 if (!len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900451 goto out;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900452 while (len--) {
453 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900454 if (c == '\\') {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900455 if (!len--)
456 goto out;
457 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900458 switch (c) {
459 case '\\': /* "\\" */
460 continue;
461 case '$': /* "\$" */
462 case '+': /* "\+" */
463 case '?': /* "\?" */
464 case '*': /* "\*" */
465 case '@': /* "\@" */
466 case 'x': /* "\x" */
467 case 'X': /* "\X" */
468 case 'a': /* "\a" */
469 case 'A': /* "\A" */
470 case '-': /* "\-" */
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900471 continue;
472 case '{': /* "/\{" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900473 if (string - 3 < start || *(string - 3) != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900474 break;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900475 in_repetition = true;
476 continue;
477 case '}': /* "\}/" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900478 if (*string != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900479 break;
480 if (!in_repetition)
481 break;
482 in_repetition = false;
483 continue;
484 case '0': /* "\ooo" */
485 case '1':
486 case '2':
487 case '3':
Tetsuo Handa3f629632010-06-03 20:37:26 +0900488 if (!len-- || !len--)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900489 break;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900490 d = *string++;
491 e = *string++;
492 if (d < '0' || d > '7' || e < '0' || e > '7')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900493 break;
494 c = tomoyo_make_byte(c, d, e);
Tetsuo Handa25add8c2012-01-15 11:05:59 +0900495 if (c <= ' ' || c >= 127)
496 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900497 }
498 goto out;
499 } else if (in_repetition && c == '/') {
500 goto out;
Tetsuo Handa25add8c2012-01-15 11:05:59 +0900501 } else if (c <= ' ' || c >= 127) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900502 goto out;
503 }
504 }
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900505 if (in_repetition)
506 goto out;
507 return true;
508 out:
509 return false;
510}
511
512/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900513 * tomoyo_correct_word - Validate a string.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900514 *
515 * @string: The string to check.
516 *
517 * Check whether the given string follows the naming rules.
518 * Returns true if @string follows the naming rules, false otherwise.
519 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900520bool tomoyo_correct_word(const char *string)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900521{
Tetsuo Handa75093152010-06-16 16:23:55 +0900522 return tomoyo_correct_word2(string, strlen(string));
Tetsuo Handa3f629632010-06-03 20:37:26 +0900523}
524
525/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900526 * tomoyo_correct_path - Validate a pathname.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900527 *
528 * @filename: The pathname to check.
529 *
530 * Check whether the given pathname follows the naming rules.
531 * Returns true if @filename follows the naming rules, false otherwise.
532 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900533bool tomoyo_correct_path(const char *filename)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900534{
Tetsuo Handa75093152010-06-16 16:23:55 +0900535 return *filename == '/' && tomoyo_correct_word(filename);
Tetsuo Handa3f629632010-06-03 20:37:26 +0900536}
537
538/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900539 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900540 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900541 * @domainname: The domainname to check.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900542 *
543 * Returns true if @domainname follows the naming rules, false otherwise.
544 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900545bool tomoyo_correct_domain(const unsigned char *domainname)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900546{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900547 if (!domainname || !tomoyo_domain_def(domainname))
548 return false;
549 domainname = strchr(domainname, ' ');
550 if (!domainname++)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900551 return true;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900552 while (1) {
553 const unsigned char *cp = strchr(domainname, ' ');
554 if (!cp)
555 break;
556 if (*domainname != '/' ||
Tetsuo Handae77dc342011-05-12 06:40:51 +0900557 !tomoyo_correct_word2(domainname, cp - domainname))
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900558 return false;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900559 domainname = cp + 1;
560 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900561 return tomoyo_correct_path(domainname);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900562}
563
564/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900565 * tomoyo_domain_def - Check whether the given token can be a domainname.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900566 *
567 * @buffer: The token to check.
568 *
569 * Returns true if @buffer possibly be a domainname, false otherwise.
570 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900571bool tomoyo_domain_def(const unsigned char *buffer)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900572{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900573 const unsigned char *cp;
574 int len;
575 if (*buffer != '<')
576 return false;
577 cp = strchr(buffer, ' ');
578 if (!cp)
579 len = strlen(buffer);
580 else
581 len = cp - buffer;
582 if (buffer[len - 1] != '>' ||
583 !tomoyo_correct_word2(buffer + 1, len - 2))
584 return false;
585 return true;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900586}
587
588/**
589 * tomoyo_find_domain - Find a domain by the given name.
590 *
591 * @domainname: The domainname to find.
592 *
593 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
594 *
595 * Caller holds tomoyo_read_lock().
596 */
597struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
598{
599 struct tomoyo_domain_info *domain;
600 struct tomoyo_path_info name;
601
602 name.name = domainname;
603 tomoyo_fill_path_info(&name);
604 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
605 if (!domain->is_deleted &&
606 !tomoyo_pathcmp(&name, domain->domainname))
607 return domain;
608 }
609 return NULL;
610}
611
612/**
613 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
614 *
615 * @filename: The string to evaluate.
616 *
617 * Returns the initial length without a pattern in @filename.
618 */
619static int tomoyo_const_part_length(const char *filename)
620{
621 char c;
622 int len = 0;
623
624 if (!filename)
625 return 0;
626 while ((c = *filename++) != '\0') {
627 if (c != '\\') {
628 len++;
629 continue;
630 }
631 c = *filename++;
632 switch (c) {
633 case '\\': /* "\\" */
634 len += 2;
635 continue;
636 case '0': /* "\ooo" */
637 case '1':
638 case '2':
639 case '3':
640 c = *filename++;
641 if (c < '0' || c > '7')
642 break;
643 c = *filename++;
644 if (c < '0' || c > '7')
645 break;
646 len += 4;
647 continue;
648 }
649 break;
650 }
651 return len;
652}
653
654/**
655 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
656 *
657 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
658 *
659 * The caller sets "struct tomoyo_path_info"->name.
660 */
661void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
662{
663 const char *name = ptr->name;
664 const int len = strlen(name);
665
666 ptr->const_len = tomoyo_const_part_length(name);
667 ptr->is_dir = len && (name[len - 1] == '/');
668 ptr->is_patterned = (ptr->const_len < len);
Linus Torvalds8387ff22016-06-10 07:51:30 -0700669 ptr->hash = full_name_hash(NULL, name, len);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900670}
671
672/**
673 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
674 *
675 * @filename: The start of string to check.
676 * @filename_end: The end of string to check.
677 * @pattern: The start of pattern to compare.
678 * @pattern_end: The end of pattern to compare.
679 *
680 * Returns true if @filename matches @pattern, false otherwise.
681 */
682static bool tomoyo_file_matches_pattern2(const char *filename,
683 const char *filename_end,
684 const char *pattern,
685 const char *pattern_end)
686{
687 while (filename < filename_end && pattern < pattern_end) {
688 char c;
689 if (*pattern != '\\') {
690 if (*filename++ != *pattern++)
691 return false;
692 continue;
693 }
694 c = *filename;
695 pattern++;
696 switch (*pattern) {
697 int i;
698 int j;
699 case '?':
700 if (c == '/') {
701 return false;
702 } else if (c == '\\') {
703 if (filename[1] == '\\')
704 filename++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900705 else if (tomoyo_byte_range(filename + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900706 filename += 3;
707 else
708 return false;
709 }
710 break;
711 case '\\':
712 if (c != '\\')
713 return false;
714 if (*++filename != '\\')
715 return false;
716 break;
717 case '+':
718 if (!isdigit(c))
719 return false;
720 break;
721 case 'x':
722 if (!isxdigit(c))
723 return false;
724 break;
725 case 'a':
Tetsuo Handa75093152010-06-16 16:23:55 +0900726 if (!tomoyo_alphabet_char(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900727 return false;
728 break;
729 case '0':
730 case '1':
731 case '2':
732 case '3':
Tetsuo Handa75093152010-06-16 16:23:55 +0900733 if (c == '\\' && tomoyo_byte_range(filename + 1)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900734 && strncmp(filename + 1, pattern, 3) == 0) {
735 filename += 3;
736 pattern += 2;
737 break;
738 }
739 return false; /* Not matched. */
740 case '*':
741 case '@':
742 for (i = 0; i <= filename_end - filename; i++) {
743 if (tomoyo_file_matches_pattern2(
744 filename + i, filename_end,
745 pattern + 1, pattern_end))
746 return true;
747 c = filename[i];
748 if (c == '.' && *pattern == '@')
749 break;
750 if (c != '\\')
751 continue;
752 if (filename[i + 1] == '\\')
753 i++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900754 else if (tomoyo_byte_range(filename + i + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900755 i += 3;
756 else
757 break; /* Bad pattern. */
758 }
759 return false; /* Not matched. */
760 default:
761 j = 0;
762 c = *pattern;
763 if (c == '$') {
764 while (isdigit(filename[j]))
765 j++;
766 } else if (c == 'X') {
767 while (isxdigit(filename[j]))
768 j++;
769 } else if (c == 'A') {
Tetsuo Handa75093152010-06-16 16:23:55 +0900770 while (tomoyo_alphabet_char(filename[j]))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900771 j++;
772 }
773 for (i = 1; i <= j; i++) {
774 if (tomoyo_file_matches_pattern2(
775 filename + i, filename_end,
776 pattern + 1, pattern_end))
777 return true;
778 }
779 return false; /* Not matched or bad pattern. */
780 }
781 filename++;
782 pattern++;
783 }
784 while (*pattern == '\\' &&
785 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
786 pattern += 2;
787 return filename == filename_end && pattern == pattern_end;
788}
789
790/**
791 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
792 *
793 * @filename: The start of string to check.
794 * @filename_end: The end of string to check.
795 * @pattern: The start of pattern to compare.
796 * @pattern_end: The end of pattern to compare.
797 *
798 * Returns true if @filename matches @pattern, false otherwise.
799 */
800static bool tomoyo_file_matches_pattern(const char *filename,
801 const char *filename_end,
802 const char *pattern,
803 const char *pattern_end)
804{
805 const char *pattern_start = pattern;
806 bool first = true;
807 bool result;
808
809 while (pattern < pattern_end - 1) {
810 /* Split at "\-" pattern. */
811 if (*pattern++ != '\\' || *pattern++ != '-')
812 continue;
813 result = tomoyo_file_matches_pattern2(filename,
814 filename_end,
815 pattern_start,
816 pattern - 2);
817 if (first)
818 result = !result;
819 if (result)
820 return false;
821 first = false;
822 pattern_start = pattern;
823 }
824 result = tomoyo_file_matches_pattern2(filename, filename_end,
825 pattern_start, pattern_end);
826 return first ? result : !result;
827}
828
829/**
830 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
831 *
832 * @f: The start of string to check.
833 * @p: The start of pattern to compare.
834 *
835 * Returns true if @f matches @p, false otherwise.
836 */
837static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
838{
839 const char *f_delimiter;
840 const char *p_delimiter;
841
842 while (*f && *p) {
843 f_delimiter = strchr(f, '/');
844 if (!f_delimiter)
845 f_delimiter = f + strlen(f);
846 p_delimiter = strchr(p, '/');
847 if (!p_delimiter)
848 p_delimiter = p + strlen(p);
849 if (*p == '\\' && *(p + 1) == '{')
850 goto recursive;
851 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
852 p_delimiter))
853 return false;
854 f = f_delimiter;
855 if (*f)
856 f++;
857 p = p_delimiter;
858 if (*p)
859 p++;
860 }
861 /* Ignore trailing "\*" and "\@" in @pattern. */
862 while (*p == '\\' &&
863 (*(p + 1) == '*' || *(p + 1) == '@'))
864 p += 2;
865 return !*f && !*p;
866 recursive:
867 /*
868 * The "\{" pattern is permitted only after '/' character.
869 * This guarantees that below "*(p - 1)" is safe.
870 * Also, the "\}" pattern is permitted only before '/' character
871 * so that "\{" + "\}" pair will not break the "\-" operator.
872 */
873 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
874 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
875 return false; /* Bad pattern. */
876 do {
877 /* Compare current component with pattern. */
878 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
879 p_delimiter - 2))
880 break;
881 /* Proceed to next component. */
882 f = f_delimiter;
883 if (!*f)
884 break;
885 f++;
886 /* Continue comparison. */
887 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
888 return true;
889 f_delimiter = strchr(f, '/');
890 } while (f_delimiter);
891 return false; /* Not matched. */
892}
893
894/**
895 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
896 *
897 * @filename: The filename to check.
898 * @pattern: The pattern to compare.
899 *
900 * Returns true if matches, false otherwise.
901 *
902 * The following patterns are available.
903 * \\ \ itself.
904 * \ooo Octal representation of a byte.
905 * \* Zero or more repetitions of characters other than '/'.
906 * \@ Zero or more repetitions of characters other than '/' or '.'.
907 * \? 1 byte character other than '/'.
908 * \$ One or more repetitions of decimal digits.
909 * \+ 1 decimal digit.
910 * \X One or more repetitions of hexadecimal digits.
911 * \x 1 hexadecimal digit.
912 * \A One or more repetitions of alphabet characters.
913 * \a 1 alphabet character.
914 *
915 * \- Subtraction operator.
916 *
917 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
918 * /dir/dir/dir/ ).
919 */
920bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
921 const struct tomoyo_path_info *pattern)
922{
923 const char *f = filename->name;
924 const char *p = pattern->name;
925 const int len = pattern->const_len;
926
927 /* If @pattern doesn't contain pattern, I can use strcmp(). */
928 if (!pattern->is_patterned)
929 return !tomoyo_pathcmp(filename, pattern);
930 /* Don't compare directory and non-directory. */
931 if (filename->is_dir != pattern->is_dir)
932 return false;
933 /* Compare the initial length without patterns. */
934 if (strncmp(f, p, len))
935 return false;
936 f += len;
937 p += len;
938 return tomoyo_path_matches_pattern2(f, p);
939}
940
941/**
942 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
943 *
944 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
945 *
946 * This function uses kzalloc(), so the caller must call kfree()
947 * if this function didn't return NULL.
948 */
949const char *tomoyo_get_exe(void)
950{
Davidlohr Buesod4144ea2015-04-16 12:49:18 -0700951 struct file *exe_file;
952 const char *cp;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900953 struct mm_struct *mm = current->mm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900954
955 if (!mm)
956 return NULL;
Davidlohr Buesod4144ea2015-04-16 12:49:18 -0700957 exe_file = get_mm_exe_file(mm);
958 if (!exe_file)
959 return NULL;
960
961 cp = tomoyo_realpath_from_path(&exe_file->f_path);
962 fput(exe_file);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900963 return cp;
964}
965
966/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900967 * tomoyo_get_mode - Get MAC mode.
968 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900969 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900970 * @profile: Profile number.
971 * @index: Index number of functionality.
972 *
973 * Returns mode.
974 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900975int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
976 const u8 index)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900977{
978 u8 mode;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900979 struct tomoyo_profile *p;
980
Tetsuo Handa57c25902010-06-03 20:38:44 +0900981 if (!tomoyo_policy_loaded)
982 return TOMOYO_CONFIG_DISABLED;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900983 p = tomoyo_profile(ns, profile);
984 mode = p->config[index];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900985 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900986 mode = p->config[tomoyo_index2category[index]
987 + TOMOYO_MAX_MAC_INDEX];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900988 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900989 mode = p->default_config;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900990 return mode & 3;
991}
992
993/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900994 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
995 *
996 * @r: Pointer to "struct tomoyo_request_info" to initialize.
997 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
Tetsuo Handa57c25902010-06-03 20:38:44 +0900998 * @index: Index number of functionality.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900999 *
1000 * Returns mode.
1001 */
1002int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +09001003 struct tomoyo_domain_info *domain, const u8 index)
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001004{
Tetsuo Handa57c25902010-06-03 20:38:44 +09001005 u8 profile;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001006 memset(r, 0, sizeof(*r));
1007 if (!domain)
1008 domain = tomoyo_domain();
1009 r->domain = domain;
Tetsuo Handa57c25902010-06-03 20:38:44 +09001010 profile = domain->profile;
1011 r->profile = profile;
1012 r->type = index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001013 r->mode = tomoyo_get_mode(domain->ns, profile, index);
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001014 return r->mode;
1015}
1016
1017/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001018 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1019 *
1020 * @r: Pointer to "struct tomoyo_request_info".
1021 *
1022 * Returns true if the domain is not exceeded quota, false otherwise.
1023 *
1024 * Caller holds tomoyo_read_lock().
1025 */
1026bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1027{
1028 unsigned int count = 0;
1029 struct tomoyo_domain_info *domain = r->domain;
1030 struct tomoyo_acl_info *ptr;
1031
1032 if (r->mode != TOMOYO_CONFIG_LEARNING)
1033 return false;
1034 if (!domain)
1035 return true;
1036 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7c759642011-06-26 23:15:31 +09001037 u16 perm;
1038 u8 i;
Tetsuo Handa7e3d1992010-07-27 10:08:29 +09001039 if (ptr->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +09001040 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001041 switch (ptr->type) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001042 case TOMOYO_TYPE_PATH_ACL:
1043 perm = container_of(ptr, struct tomoyo_path_acl, head)
1044 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001045 break;
1046 case TOMOYO_TYPE_PATH2_ACL:
1047 perm = container_of(ptr, struct tomoyo_path2_acl, head)
1048 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001049 break;
1050 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1051 perm = container_of(ptr, struct tomoyo_path_number_acl,
1052 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001053 break;
Tetsuo Handa75093152010-06-16 16:23:55 +09001054 case TOMOYO_TYPE_MKDEV_ACL:
1055 perm = container_of(ptr, struct tomoyo_mkdev_acl,
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001056 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001057 break;
Tetsuo Handa545a7262011-10-11 14:06:41 +09001058 case TOMOYO_TYPE_INET_ACL:
1059 perm = container_of(ptr, struct tomoyo_inet_acl,
1060 head)->perm;
1061 break;
1062 case TOMOYO_TYPE_UNIX_ACL:
1063 perm = container_of(ptr, struct tomoyo_unix_acl,
1064 head)->perm;
1065 break;
1066 case TOMOYO_TYPE_MANUAL_TASK_ACL:
1067 perm = 0;
1068 break;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001069 default:
Tetsuo Handa7c759642011-06-26 23:15:31 +09001070 perm = 1;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001071 }
Tetsuo Handa7c759642011-06-26 23:15:31 +09001072 for (i = 0; i < 16; i++)
1073 if (perm & (1 << i))
1074 count++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001075 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001076 if (count < tomoyo_profile(domain->ns, domain->profile)->
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001077 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001078 return true;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001079 if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) {
1080 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
1081 /* r->granted = false; */
1082 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
1083 printk(KERN_WARNING "WARNING: "
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001084 "Domain '%s' has too many ACLs to hold. "
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001085 "Stopped learning mode.\n", domain->domainname->name);
1086 }
1087 return false;
1088}