blob: 7ff54c95e1f25c45440d97395723416a7be1dbb5 [file] [log] [blame]
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001/*
2 * security/tomoyo/util.c
3 *
4 * Utility functions for TOMOYO.
5 *
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
7 */
8
9#include <linux/slab.h>
10#include "common.h"
11
12/* Lock for protecting policy. */
13DEFINE_MUTEX(tomoyo_policy_lock);
14
15/* Has /sbin/init started? */
16bool tomoyo_policy_loaded;
17
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090018/*
19 * Mapping table from "enum tomoyo_mac_index" to
20 * "enum tomoyo_mac_category_index".
21 */
22const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = {
23 /* CONFIG::file group */
24 [TOMOYO_MAC_FILE_EXECUTE] = TOMOYO_MAC_CATEGORY_FILE,
25 [TOMOYO_MAC_FILE_OPEN] = TOMOYO_MAC_CATEGORY_FILE,
26 [TOMOYO_MAC_FILE_CREATE] = TOMOYO_MAC_CATEGORY_FILE,
27 [TOMOYO_MAC_FILE_UNLINK] = TOMOYO_MAC_CATEGORY_FILE,
28 [TOMOYO_MAC_FILE_GETATTR] = TOMOYO_MAC_CATEGORY_FILE,
29 [TOMOYO_MAC_FILE_MKDIR] = TOMOYO_MAC_CATEGORY_FILE,
30 [TOMOYO_MAC_FILE_RMDIR] = TOMOYO_MAC_CATEGORY_FILE,
31 [TOMOYO_MAC_FILE_MKFIFO] = TOMOYO_MAC_CATEGORY_FILE,
32 [TOMOYO_MAC_FILE_MKSOCK] = TOMOYO_MAC_CATEGORY_FILE,
33 [TOMOYO_MAC_FILE_TRUNCATE] = TOMOYO_MAC_CATEGORY_FILE,
34 [TOMOYO_MAC_FILE_SYMLINK] = TOMOYO_MAC_CATEGORY_FILE,
35 [TOMOYO_MAC_FILE_MKBLOCK] = TOMOYO_MAC_CATEGORY_FILE,
36 [TOMOYO_MAC_FILE_MKCHAR] = TOMOYO_MAC_CATEGORY_FILE,
37 [TOMOYO_MAC_FILE_LINK] = TOMOYO_MAC_CATEGORY_FILE,
38 [TOMOYO_MAC_FILE_RENAME] = TOMOYO_MAC_CATEGORY_FILE,
39 [TOMOYO_MAC_FILE_CHMOD] = TOMOYO_MAC_CATEGORY_FILE,
40 [TOMOYO_MAC_FILE_CHOWN] = TOMOYO_MAC_CATEGORY_FILE,
41 [TOMOYO_MAC_FILE_CHGRP] = TOMOYO_MAC_CATEGORY_FILE,
42 [TOMOYO_MAC_FILE_IOCTL] = TOMOYO_MAC_CATEGORY_FILE,
43 [TOMOYO_MAC_FILE_CHROOT] = TOMOYO_MAC_CATEGORY_FILE,
44 [TOMOYO_MAC_FILE_MOUNT] = TOMOYO_MAC_CATEGORY_FILE,
45 [TOMOYO_MAC_FILE_UMOUNT] = TOMOYO_MAC_CATEGORY_FILE,
46 [TOMOYO_MAC_FILE_PIVOT_ROOT] = TOMOYO_MAC_CATEGORY_FILE,
47};
48
Tetsuo Handac3ef1502010-05-17 10:12:46 +090049/**
Tetsuo Handab22b8b92011-06-26 23:21:50 +090050 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
51 *
52 * @time: Seconds since 1970/01/01 00:00:00.
53 * @stamp: Pointer to "struct tomoyo_time".
54 *
55 * Returns nothing.
56 *
57 * This function does not handle Y2038 problem.
58 */
59void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp)
60{
61 static const u16 tomoyo_eom[2][12] = {
62 { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
63 { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
64 };
65 u16 y;
66 u8 m;
67 bool r;
68 stamp->sec = time % 60;
69 time /= 60;
70 stamp->min = time % 60;
71 time /= 60;
72 stamp->hour = time % 24;
73 time /= 24;
74 for (y = 1970; ; y++) {
75 const unsigned short days = (y & 3) ? 365 : 366;
76 if (time < days)
77 break;
78 time -= days;
79 }
80 r = (y & 3) == 0;
81 for (m = 0; m < 11 && time >= tomoyo_eom[r][m]; m++)
82 ;
83 if (m)
84 time -= tomoyo_eom[r][m - 1];
85 stamp->year = y;
86 stamp->month = ++m;
87 stamp->day = ++time;
88}
89
90/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +090091 * tomoyo_permstr - Find permission keywords.
92 *
93 * @string: String representation for permissions in foo/bar/buz format.
94 * @keyword: Keyword to find from @string/
95 *
96 * Returns ture if @keyword was found in @string, false otherwise.
97 *
98 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
99 */
100bool tomoyo_permstr(const char *string, const char *keyword)
101{
102 const char *cp = strstr(string, keyword);
103 if (cp)
104 return cp == string || *(cp - 1) == '/';
105 return false;
106}
107
108/**
109 * tomoyo_read_token - Read a word from a line.
110 *
111 * @param: Pointer to "struct tomoyo_acl_param".
112 *
113 * Returns a word on success, "" otherwise.
114 *
115 * To allow the caller to skip NULL check, this function returns "" rather than
116 * NULL if there is no more words to read.
117 */
118char *tomoyo_read_token(struct tomoyo_acl_param *param)
119{
120 char *pos = param->data;
121 char *del = strchr(pos, ' ');
122 if (del)
123 *del++ = '\0';
124 else
125 del = pos + strlen(pos);
126 param->data = del;
127 return pos;
128}
129
130/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900131 * tomoyo_parse_ulong - Parse an "unsigned long" value.
132 *
133 * @result: Pointer to "unsigned long".
134 * @str: Pointer to string to parse.
135 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900136 * Returns one of values in "enum tomoyo_value_type".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900137 *
138 * The @src is updated to point the first character after the value
139 * on success.
140 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900141static u8 tomoyo_parse_ulong(unsigned long *result, char **str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900142{
143 const char *cp = *str;
144 char *ep;
145 int base = 10;
146 if (*cp == '0') {
147 char c = *(cp + 1);
148 if (c == 'x' || c == 'X') {
149 base = 16;
150 cp += 2;
151 } else if (c >= '0' && c <= '7') {
152 base = 8;
153 cp++;
154 }
155 }
156 *result = simple_strtoul(cp, &ep, base);
157 if (cp == ep)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900158 return TOMOYO_VALUE_TYPE_INVALID;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900159 *str = ep;
160 switch (base) {
161 case 16:
162 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
163 case 8:
164 return TOMOYO_VALUE_TYPE_OCTAL;
165 default:
166 return TOMOYO_VALUE_TYPE_DECIMAL;
167 }
168}
169
170/**
171 * tomoyo_print_ulong - Print an "unsigned long" value.
172 *
173 * @buffer: Pointer to buffer.
174 * @buffer_len: Size of @buffer.
175 * @value: An "unsigned long" value.
176 * @type: Type of @value.
177 *
178 * Returns nothing.
179 */
180void tomoyo_print_ulong(char *buffer, const int buffer_len,
181 const unsigned long value, const u8 type)
182{
183 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
184 snprintf(buffer, buffer_len, "%lu", value);
185 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
186 snprintf(buffer, buffer_len, "0%lo", value);
187 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
188 snprintf(buffer, buffer_len, "0x%lX", value);
189 else
190 snprintf(buffer, buffer_len, "type(%u)", type);
191}
192
193/**
194 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
195 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900196 * @param: Pointer to "struct tomoyo_acl_param".
197 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900198 *
199 * Returns true on success, false otherwise.
200 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900201bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900202 struct tomoyo_name_union *ptr)
203{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900204 char *filename;
205 if (param->data[0] == '@') {
206 param->data++;
207 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900208 return ptr->group != NULL;
209 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900210 filename = tomoyo_read_token(param);
211 if (!tomoyo_correct_word(filename))
212 return false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900213 ptr->filename = tomoyo_get_name(filename);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900214 return ptr->filename != NULL;
215}
216
217/**
218 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
219 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900220 * @param: Pointer to "struct tomoyo_acl_param".
221 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900222 *
223 * Returns true on success, false otherwise.
224 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900225bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
226 struct tomoyo_number_union *ptr)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900227{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900228 char *data;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900229 u8 type;
230 unsigned long v;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900231 memset(ptr, 0, sizeof(*ptr));
232 if (param->data[0] == '@') {
233 param->data++;
234 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
235 return ptr->group != NULL;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900236 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900237 data = tomoyo_read_token(param);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900238 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900239 if (type == TOMOYO_VALUE_TYPE_INVALID)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900240 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900241 ptr->values[0] = v;
242 ptr->value_type[0] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900243 if (!*data) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900244 ptr->values[1] = v;
245 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900246 return true;
247 }
248 if (*data++ != '-')
249 return false;
250 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900251 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900252 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900253 ptr->values[1] = v;
254 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900255 return true;
256}
257
258/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900259 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900260 *
261 * @str: Pointer to the string.
262 *
263 * Returns true if @str is a \ooo style octal value, false otherwise.
264 *
265 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
266 * This function verifies that \ooo is in valid range.
267 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900268static inline bool tomoyo_byte_range(const char *str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900269{
270 return *str >= '0' && *str++ <= '3' &&
271 *str >= '0' && *str++ <= '7' &&
272 *str >= '0' && *str <= '7';
273}
274
275/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900276 * tomoyo_alphabet_char - Check whether the character is an alphabet.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900277 *
278 * @c: The character to check.
279 *
280 * Returns true if @c is an alphabet character, false otherwise.
281 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900282static inline bool tomoyo_alphabet_char(const char c)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900283{
284 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
285}
286
287/**
288 * tomoyo_make_byte - Make byte value from three octal characters.
289 *
290 * @c1: The first character.
291 * @c2: The second character.
292 * @c3: The third character.
293 *
294 * Returns byte value.
295 */
296static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
297{
298 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
299}
300
301/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900302 * tomoyo_valid - Check whether the character is a valid char.
303 *
304 * @c: The character to check.
305 *
306 * Returns true if @c is a valid character, false otherwise.
307 */
308static inline bool tomoyo_valid(const unsigned char c)
309{
310 return c > ' ' && c < 127;
311}
312
313/**
314 * tomoyo_invalid - Check whether the character is an invalid char.
315 *
316 * @c: The character to check.
317 *
318 * Returns true if @c is an invalid character, false otherwise.
319 */
320static inline bool tomoyo_invalid(const unsigned char c)
321{
322 return c && (c <= ' ' || c >= 127);
323}
324
325/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900326 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
327 *
328 * @src: Pointer to pointer to the string.
329 * @find: Pointer to the keyword.
330 *
331 * Returns true if @src starts with @find, false otherwise.
332 *
333 * The @src is updated to point the first character after the @find
334 * if @src starts with @find.
335 */
336bool tomoyo_str_starts(char **src, const char *find)
337{
338 const int len = strlen(find);
339 char *tmp = *src;
340
341 if (strncmp(tmp, find, len))
342 return false;
343 tmp += len;
344 *src = tmp;
345 return true;
346}
347
348/**
349 * tomoyo_normalize_line - Format string.
350 *
351 * @buffer: The line to normalize.
352 *
353 * Leading and trailing whitespaces are removed.
354 * Multiple whitespaces are packed into single space.
355 *
356 * Returns nothing.
357 */
358void tomoyo_normalize_line(unsigned char *buffer)
359{
360 unsigned char *sp = buffer;
361 unsigned char *dp = buffer;
362 bool first = true;
363
Tetsuo Handa75093152010-06-16 16:23:55 +0900364 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900365 sp++;
366 while (*sp) {
367 if (!first)
368 *dp++ = ' ';
369 first = false;
Tetsuo Handa75093152010-06-16 16:23:55 +0900370 while (tomoyo_valid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900371 *dp++ = *sp++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900372 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900373 sp++;
374 }
375 *dp = '\0';
376}
377
378/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900379 * tomoyo_correct_word2 - Validate a string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900380 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900381 * @string: The string to check. May be non-'\0'-terminated.
382 * @len: Length of @string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900383 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900384 * Check whether the given string follows the naming rules.
385 * Returns true if @string follows the naming rules, false otherwise.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900386 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900387static bool tomoyo_correct_word2(const char *string, size_t len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900388{
Tetsuo Handa3f629632010-06-03 20:37:26 +0900389 const char *const start = string;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900390 bool in_repetition = false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900391 unsigned char c;
392 unsigned char d;
393 unsigned char e;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900394 if (!len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900395 goto out;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900396 while (len--) {
397 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900398 if (c == '\\') {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900399 if (!len--)
400 goto out;
401 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900402 switch (c) {
403 case '\\': /* "\\" */
404 continue;
405 case '$': /* "\$" */
406 case '+': /* "\+" */
407 case '?': /* "\?" */
408 case '*': /* "\*" */
409 case '@': /* "\@" */
410 case 'x': /* "\x" */
411 case 'X': /* "\X" */
412 case 'a': /* "\a" */
413 case 'A': /* "\A" */
414 case '-': /* "\-" */
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900415 continue;
416 case '{': /* "/\{" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900417 if (string - 3 < start || *(string - 3) != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900418 break;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900419 in_repetition = true;
420 continue;
421 case '}': /* "\}/" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900422 if (*string != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900423 break;
424 if (!in_repetition)
425 break;
426 in_repetition = false;
427 continue;
428 case '0': /* "\ooo" */
429 case '1':
430 case '2':
431 case '3':
Tetsuo Handa3f629632010-06-03 20:37:26 +0900432 if (!len-- || !len--)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900433 break;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900434 d = *string++;
435 e = *string++;
436 if (d < '0' || d > '7' || e < '0' || e > '7')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900437 break;
438 c = tomoyo_make_byte(c, d, e);
Tetsuo Handa75093152010-06-16 16:23:55 +0900439 if (tomoyo_invalid(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900440 continue; /* pattern is not \000 */
441 }
442 goto out;
443 } else if (in_repetition && c == '/') {
444 goto out;
Tetsuo Handa75093152010-06-16 16:23:55 +0900445 } else if (tomoyo_invalid(c)) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900446 goto out;
447 }
448 }
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900449 if (in_repetition)
450 goto out;
451 return true;
452 out:
453 return false;
454}
455
456/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900457 * tomoyo_correct_word - Validate a string.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900458 *
459 * @string: The string to check.
460 *
461 * Check whether the given string follows the naming rules.
462 * Returns true if @string follows the naming rules, false otherwise.
463 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900464bool tomoyo_correct_word(const char *string)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900465{
Tetsuo Handa75093152010-06-16 16:23:55 +0900466 return tomoyo_correct_word2(string, strlen(string));
Tetsuo Handa3f629632010-06-03 20:37:26 +0900467}
468
469/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900470 * tomoyo_correct_path - Validate a pathname.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900471 *
472 * @filename: The pathname to check.
473 *
474 * Check whether the given pathname follows the naming rules.
475 * Returns true if @filename follows the naming rules, false otherwise.
476 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900477bool tomoyo_correct_path(const char *filename)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900478{
Tetsuo Handa75093152010-06-16 16:23:55 +0900479 return *filename == '/' && tomoyo_correct_word(filename);
Tetsuo Handa3f629632010-06-03 20:37:26 +0900480}
481
482/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900483 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900484 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900485 * @domainname: The domainname to check.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900486 *
487 * Returns true if @domainname follows the naming rules, false otherwise.
488 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900489bool tomoyo_correct_domain(const unsigned char *domainname)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900490{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900491 if (!domainname || !tomoyo_domain_def(domainname))
492 return false;
493 domainname = strchr(domainname, ' ');
494 if (!domainname++)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900495 return true;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900496 while (1) {
497 const unsigned char *cp = strchr(domainname, ' ');
498 if (!cp)
499 break;
500 if (*domainname != '/' ||
Tetsuo Handae77dc342011-05-12 06:40:51 +0900501 !tomoyo_correct_word2(domainname, cp - domainname))
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900502 return false;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900503 domainname = cp + 1;
504 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900505 return tomoyo_correct_path(domainname);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900506}
507
508/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900509 * tomoyo_domain_def - Check whether the given token can be a domainname.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900510 *
511 * @buffer: The token to check.
512 *
513 * Returns true if @buffer possibly be a domainname, false otherwise.
514 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900515bool tomoyo_domain_def(const unsigned char *buffer)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900516{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900517 const unsigned char *cp;
518 int len;
519 if (*buffer != '<')
520 return false;
521 cp = strchr(buffer, ' ');
522 if (!cp)
523 len = strlen(buffer);
524 else
525 len = cp - buffer;
526 if (buffer[len - 1] != '>' ||
527 !tomoyo_correct_word2(buffer + 1, len - 2))
528 return false;
529 return true;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900530}
531
532/**
533 * tomoyo_find_domain - Find a domain by the given name.
534 *
535 * @domainname: The domainname to find.
536 *
537 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
538 *
539 * Caller holds tomoyo_read_lock().
540 */
541struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
542{
543 struct tomoyo_domain_info *domain;
544 struct tomoyo_path_info name;
545
546 name.name = domainname;
547 tomoyo_fill_path_info(&name);
548 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
549 if (!domain->is_deleted &&
550 !tomoyo_pathcmp(&name, domain->domainname))
551 return domain;
552 }
553 return NULL;
554}
555
556/**
557 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
558 *
559 * @filename: The string to evaluate.
560 *
561 * Returns the initial length without a pattern in @filename.
562 */
563static int tomoyo_const_part_length(const char *filename)
564{
565 char c;
566 int len = 0;
567
568 if (!filename)
569 return 0;
570 while ((c = *filename++) != '\0') {
571 if (c != '\\') {
572 len++;
573 continue;
574 }
575 c = *filename++;
576 switch (c) {
577 case '\\': /* "\\" */
578 len += 2;
579 continue;
580 case '0': /* "\ooo" */
581 case '1':
582 case '2':
583 case '3':
584 c = *filename++;
585 if (c < '0' || c > '7')
586 break;
587 c = *filename++;
588 if (c < '0' || c > '7')
589 break;
590 len += 4;
591 continue;
592 }
593 break;
594 }
595 return len;
596}
597
598/**
599 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
600 *
601 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
602 *
603 * The caller sets "struct tomoyo_path_info"->name.
604 */
605void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
606{
607 const char *name = ptr->name;
608 const int len = strlen(name);
609
610 ptr->const_len = tomoyo_const_part_length(name);
611 ptr->is_dir = len && (name[len - 1] == '/');
612 ptr->is_patterned = (ptr->const_len < len);
613 ptr->hash = full_name_hash(name, len);
614}
615
616/**
617 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
618 *
619 * @filename: The start of string to check.
620 * @filename_end: The end of string to check.
621 * @pattern: The start of pattern to compare.
622 * @pattern_end: The end of pattern to compare.
623 *
624 * Returns true if @filename matches @pattern, false otherwise.
625 */
626static bool tomoyo_file_matches_pattern2(const char *filename,
627 const char *filename_end,
628 const char *pattern,
629 const char *pattern_end)
630{
631 while (filename < filename_end && pattern < pattern_end) {
632 char c;
633 if (*pattern != '\\') {
634 if (*filename++ != *pattern++)
635 return false;
636 continue;
637 }
638 c = *filename;
639 pattern++;
640 switch (*pattern) {
641 int i;
642 int j;
643 case '?':
644 if (c == '/') {
645 return false;
646 } else if (c == '\\') {
647 if (filename[1] == '\\')
648 filename++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900649 else if (tomoyo_byte_range(filename + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900650 filename += 3;
651 else
652 return false;
653 }
654 break;
655 case '\\':
656 if (c != '\\')
657 return false;
658 if (*++filename != '\\')
659 return false;
660 break;
661 case '+':
662 if (!isdigit(c))
663 return false;
664 break;
665 case 'x':
666 if (!isxdigit(c))
667 return false;
668 break;
669 case 'a':
Tetsuo Handa75093152010-06-16 16:23:55 +0900670 if (!tomoyo_alphabet_char(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900671 return false;
672 break;
673 case '0':
674 case '1':
675 case '2':
676 case '3':
Tetsuo Handa75093152010-06-16 16:23:55 +0900677 if (c == '\\' && tomoyo_byte_range(filename + 1)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900678 && strncmp(filename + 1, pattern, 3) == 0) {
679 filename += 3;
680 pattern += 2;
681 break;
682 }
683 return false; /* Not matched. */
684 case '*':
685 case '@':
686 for (i = 0; i <= filename_end - filename; i++) {
687 if (tomoyo_file_matches_pattern2(
688 filename + i, filename_end,
689 pattern + 1, pattern_end))
690 return true;
691 c = filename[i];
692 if (c == '.' && *pattern == '@')
693 break;
694 if (c != '\\')
695 continue;
696 if (filename[i + 1] == '\\')
697 i++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900698 else if (tomoyo_byte_range(filename + i + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900699 i += 3;
700 else
701 break; /* Bad pattern. */
702 }
703 return false; /* Not matched. */
704 default:
705 j = 0;
706 c = *pattern;
707 if (c == '$') {
708 while (isdigit(filename[j]))
709 j++;
710 } else if (c == 'X') {
711 while (isxdigit(filename[j]))
712 j++;
713 } else if (c == 'A') {
Tetsuo Handa75093152010-06-16 16:23:55 +0900714 while (tomoyo_alphabet_char(filename[j]))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900715 j++;
716 }
717 for (i = 1; i <= j; i++) {
718 if (tomoyo_file_matches_pattern2(
719 filename + i, filename_end,
720 pattern + 1, pattern_end))
721 return true;
722 }
723 return false; /* Not matched or bad pattern. */
724 }
725 filename++;
726 pattern++;
727 }
728 while (*pattern == '\\' &&
729 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
730 pattern += 2;
731 return filename == filename_end && pattern == pattern_end;
732}
733
734/**
735 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
736 *
737 * @filename: The start of string to check.
738 * @filename_end: The end of string to check.
739 * @pattern: The start of pattern to compare.
740 * @pattern_end: The end of pattern to compare.
741 *
742 * Returns true if @filename matches @pattern, false otherwise.
743 */
744static bool tomoyo_file_matches_pattern(const char *filename,
745 const char *filename_end,
746 const char *pattern,
747 const char *pattern_end)
748{
749 const char *pattern_start = pattern;
750 bool first = true;
751 bool result;
752
753 while (pattern < pattern_end - 1) {
754 /* Split at "\-" pattern. */
755 if (*pattern++ != '\\' || *pattern++ != '-')
756 continue;
757 result = tomoyo_file_matches_pattern2(filename,
758 filename_end,
759 pattern_start,
760 pattern - 2);
761 if (first)
762 result = !result;
763 if (result)
764 return false;
765 first = false;
766 pattern_start = pattern;
767 }
768 result = tomoyo_file_matches_pattern2(filename, filename_end,
769 pattern_start, pattern_end);
770 return first ? result : !result;
771}
772
773/**
774 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
775 *
776 * @f: The start of string to check.
777 * @p: The start of pattern to compare.
778 *
779 * Returns true if @f matches @p, false otherwise.
780 */
781static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
782{
783 const char *f_delimiter;
784 const char *p_delimiter;
785
786 while (*f && *p) {
787 f_delimiter = strchr(f, '/');
788 if (!f_delimiter)
789 f_delimiter = f + strlen(f);
790 p_delimiter = strchr(p, '/');
791 if (!p_delimiter)
792 p_delimiter = p + strlen(p);
793 if (*p == '\\' && *(p + 1) == '{')
794 goto recursive;
795 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
796 p_delimiter))
797 return false;
798 f = f_delimiter;
799 if (*f)
800 f++;
801 p = p_delimiter;
802 if (*p)
803 p++;
804 }
805 /* Ignore trailing "\*" and "\@" in @pattern. */
806 while (*p == '\\' &&
807 (*(p + 1) == '*' || *(p + 1) == '@'))
808 p += 2;
809 return !*f && !*p;
810 recursive:
811 /*
812 * The "\{" pattern is permitted only after '/' character.
813 * This guarantees that below "*(p - 1)" is safe.
814 * Also, the "\}" pattern is permitted only before '/' character
815 * so that "\{" + "\}" pair will not break the "\-" operator.
816 */
817 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
818 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
819 return false; /* Bad pattern. */
820 do {
821 /* Compare current component with pattern. */
822 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
823 p_delimiter - 2))
824 break;
825 /* Proceed to next component. */
826 f = f_delimiter;
827 if (!*f)
828 break;
829 f++;
830 /* Continue comparison. */
831 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
832 return true;
833 f_delimiter = strchr(f, '/');
834 } while (f_delimiter);
835 return false; /* Not matched. */
836}
837
838/**
839 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
840 *
841 * @filename: The filename to check.
842 * @pattern: The pattern to compare.
843 *
844 * Returns true if matches, false otherwise.
845 *
846 * The following patterns are available.
847 * \\ \ itself.
848 * \ooo Octal representation of a byte.
849 * \* Zero or more repetitions of characters other than '/'.
850 * \@ Zero or more repetitions of characters other than '/' or '.'.
851 * \? 1 byte character other than '/'.
852 * \$ One or more repetitions of decimal digits.
853 * \+ 1 decimal digit.
854 * \X One or more repetitions of hexadecimal digits.
855 * \x 1 hexadecimal digit.
856 * \A One or more repetitions of alphabet characters.
857 * \a 1 alphabet character.
858 *
859 * \- Subtraction operator.
860 *
861 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
862 * /dir/dir/dir/ ).
863 */
864bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
865 const struct tomoyo_path_info *pattern)
866{
867 const char *f = filename->name;
868 const char *p = pattern->name;
869 const int len = pattern->const_len;
870
871 /* If @pattern doesn't contain pattern, I can use strcmp(). */
872 if (!pattern->is_patterned)
873 return !tomoyo_pathcmp(filename, pattern);
874 /* Don't compare directory and non-directory. */
875 if (filename->is_dir != pattern->is_dir)
876 return false;
877 /* Compare the initial length without patterns. */
878 if (strncmp(f, p, len))
879 return false;
880 f += len;
881 p += len;
882 return tomoyo_path_matches_pattern2(f, p);
883}
884
885/**
886 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
887 *
888 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
889 *
890 * This function uses kzalloc(), so the caller must call kfree()
891 * if this function didn't return NULL.
892 */
893const char *tomoyo_get_exe(void)
894{
895 struct mm_struct *mm = current->mm;
896 struct vm_area_struct *vma;
897 const char *cp = NULL;
898
899 if (!mm)
900 return NULL;
901 down_read(&mm->mmap_sem);
902 for (vma = mm->mmap; vma; vma = vma->vm_next) {
903 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
904 cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
905 break;
906 }
907 }
908 up_read(&mm->mmap_sem);
909 return cp;
910}
911
912/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900913 * tomoyo_get_mode - Get MAC mode.
914 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900915 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900916 * @profile: Profile number.
917 * @index: Index number of functionality.
918 *
919 * Returns mode.
920 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900921int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
922 const u8 index)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900923{
924 u8 mode;
925 const u8 category = TOMOYO_MAC_CATEGORY_FILE;
926 if (!tomoyo_policy_loaded)
927 return TOMOYO_CONFIG_DISABLED;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900928 mode = tomoyo_profile(ns, profile)->config[index];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900929 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900930 mode = tomoyo_profile(ns, profile)->config[category];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900931 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900932 mode = tomoyo_profile(ns, profile)->default_config;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900933 return mode & 3;
934}
935
936/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900937 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
938 *
939 * @r: Pointer to "struct tomoyo_request_info" to initialize.
940 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
Tetsuo Handa57c25902010-06-03 20:38:44 +0900941 * @index: Index number of functionality.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900942 *
943 * Returns mode.
944 */
945int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900946 struct tomoyo_domain_info *domain, const u8 index)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900947{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900948 u8 profile;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900949 memset(r, 0, sizeof(*r));
950 if (!domain)
951 domain = tomoyo_domain();
952 r->domain = domain;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900953 profile = domain->profile;
954 r->profile = profile;
955 r->type = index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900956 r->mode = tomoyo_get_mode(domain->ns, profile, index);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900957 return r->mode;
958}
959
960/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900961 * tomoyo_domain_quota_is_ok - Check for domain's quota.
962 *
963 * @r: Pointer to "struct tomoyo_request_info".
964 *
965 * Returns true if the domain is not exceeded quota, false otherwise.
966 *
967 * Caller holds tomoyo_read_lock().
968 */
969bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
970{
971 unsigned int count = 0;
972 struct tomoyo_domain_info *domain = r->domain;
973 struct tomoyo_acl_info *ptr;
974
975 if (r->mode != TOMOYO_CONFIG_LEARNING)
976 return false;
977 if (!domain)
978 return true;
979 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7c759642011-06-26 23:15:31 +0900980 u16 perm;
981 u8 i;
Tetsuo Handa7e3d1992010-07-27 10:08:29 +0900982 if (ptr->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900983 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900984 switch (ptr->type) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900985 case TOMOYO_TYPE_PATH_ACL:
986 perm = container_of(ptr, struct tomoyo_path_acl, head)
987 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900988 break;
989 case TOMOYO_TYPE_PATH2_ACL:
990 perm = container_of(ptr, struct tomoyo_path2_acl, head)
991 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900992 break;
993 case TOMOYO_TYPE_PATH_NUMBER_ACL:
994 perm = container_of(ptr, struct tomoyo_path_number_acl,
995 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900996 break;
Tetsuo Handa75093152010-06-16 16:23:55 +0900997 case TOMOYO_TYPE_MKDEV_ACL:
998 perm = container_of(ptr, struct tomoyo_mkdev_acl,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900999 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001000 break;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001001 default:
Tetsuo Handa7c759642011-06-26 23:15:31 +09001002 perm = 1;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001003 }
Tetsuo Handa7c759642011-06-26 23:15:31 +09001004 for (i = 0; i < 16; i++)
1005 if (perm & (1 << i))
1006 count++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001007 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001008 if (count < tomoyo_profile(domain->ns, domain->profile)->
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001009 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001010 return true;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001011 if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) {
1012 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
1013 /* r->granted = false; */
1014 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
1015 printk(KERN_WARNING "WARNING: "
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001016 "Domain '%s' has too many ACLs to hold. "
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001017 "Stopped learning mode.\n", domain->domainname->name);
1018 }
1019 return false;
1020}