blob: 72cd2b97cae86f1bfb792c16432de34e59ff5445 [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
18/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +090019 * tomoyo_permstr - Find permission keywords.
20 *
21 * @string: String representation for permissions in foo/bar/buz format.
22 * @keyword: Keyword to find from @string/
23 *
24 * Returns ture if @keyword was found in @string, false otherwise.
25 *
26 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
27 */
28bool tomoyo_permstr(const char *string, const char *keyword)
29{
30 const char *cp = strstr(string, keyword);
31 if (cp)
32 return cp == string || *(cp - 1) == '/';
33 return false;
34}
35
36/**
37 * tomoyo_read_token - Read a word from a line.
38 *
39 * @param: Pointer to "struct tomoyo_acl_param".
40 *
41 * Returns a word on success, "" otherwise.
42 *
43 * To allow the caller to skip NULL check, this function returns "" rather than
44 * NULL if there is no more words to read.
45 */
46char *tomoyo_read_token(struct tomoyo_acl_param *param)
47{
48 char *pos = param->data;
49 char *del = strchr(pos, ' ');
50 if (del)
51 *del++ = '\0';
52 else
53 del = pos + strlen(pos);
54 param->data = del;
55 return pos;
56}
57
58/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +090059 * tomoyo_parse_ulong - Parse an "unsigned long" value.
60 *
61 * @result: Pointer to "unsigned long".
62 * @str: Pointer to string to parse.
63 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090064 * Returns one of values in "enum tomoyo_value_type".
Tetsuo Handac3ef1502010-05-17 10:12:46 +090065 *
66 * The @src is updated to point the first character after the value
67 * on success.
68 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +090069static u8 tomoyo_parse_ulong(unsigned long *result, char **str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +090070{
71 const char *cp = *str;
72 char *ep;
73 int base = 10;
74 if (*cp == '0') {
75 char c = *(cp + 1);
76 if (c == 'x' || c == 'X') {
77 base = 16;
78 cp += 2;
79 } else if (c >= '0' && c <= '7') {
80 base = 8;
81 cp++;
82 }
83 }
84 *result = simple_strtoul(cp, &ep, base);
85 if (cp == ep)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090086 return TOMOYO_VALUE_TYPE_INVALID;
Tetsuo Handac3ef1502010-05-17 10:12:46 +090087 *str = ep;
88 switch (base) {
89 case 16:
90 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
91 case 8:
92 return TOMOYO_VALUE_TYPE_OCTAL;
93 default:
94 return TOMOYO_VALUE_TYPE_DECIMAL;
95 }
96}
97
98/**
99 * tomoyo_print_ulong - Print an "unsigned long" value.
100 *
101 * @buffer: Pointer to buffer.
102 * @buffer_len: Size of @buffer.
103 * @value: An "unsigned long" value.
104 * @type: Type of @value.
105 *
106 * Returns nothing.
107 */
108void tomoyo_print_ulong(char *buffer, const int buffer_len,
109 const unsigned long value, const u8 type)
110{
111 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
112 snprintf(buffer, buffer_len, "%lu", value);
113 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
114 snprintf(buffer, buffer_len, "0%lo", value);
115 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
116 snprintf(buffer, buffer_len, "0x%lX", value);
117 else
118 snprintf(buffer, buffer_len, "type(%u)", type);
119}
120
121/**
122 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
123 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900124 * @param: Pointer to "struct tomoyo_acl_param".
125 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900126 *
127 * Returns true on success, false otherwise.
128 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900129bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900130 struct tomoyo_name_union *ptr)
131{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900132 char *filename;
133 if (param->data[0] == '@') {
134 param->data++;
135 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900136 return ptr->group != NULL;
137 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900138 filename = tomoyo_read_token(param);
139 if (!tomoyo_correct_word(filename))
140 return false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900141 ptr->filename = tomoyo_get_name(filename);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900142 return ptr->filename != NULL;
143}
144
145/**
146 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
147 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900148 * @param: Pointer to "struct tomoyo_acl_param".
149 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900150 *
151 * Returns true on success, false otherwise.
152 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900153bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
154 struct tomoyo_number_union *ptr)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900155{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900156 char *data;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900157 u8 type;
158 unsigned long v;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900159 memset(ptr, 0, sizeof(*ptr));
160 if (param->data[0] == '@') {
161 param->data++;
162 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
163 return ptr->group != NULL;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900164 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900165 data = tomoyo_read_token(param);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900166 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900167 if (type == TOMOYO_VALUE_TYPE_INVALID)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900168 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900169 ptr->values[0] = v;
170 ptr->value_type[0] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900171 if (!*data) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900172 ptr->values[1] = v;
173 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900174 return true;
175 }
176 if (*data++ != '-')
177 return false;
178 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900179 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900180 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900181 ptr->values[1] = v;
182 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900183 return true;
184}
185
186/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900187 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900188 *
189 * @str: Pointer to the string.
190 *
191 * Returns true if @str is a \ooo style octal value, false otherwise.
192 *
193 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
194 * This function verifies that \ooo is in valid range.
195 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900196static inline bool tomoyo_byte_range(const char *str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900197{
198 return *str >= '0' && *str++ <= '3' &&
199 *str >= '0' && *str++ <= '7' &&
200 *str >= '0' && *str <= '7';
201}
202
203/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900204 * tomoyo_alphabet_char - Check whether the character is an alphabet.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900205 *
206 * @c: The character to check.
207 *
208 * Returns true if @c is an alphabet character, false otherwise.
209 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900210static inline bool tomoyo_alphabet_char(const char c)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900211{
212 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
213}
214
215/**
216 * tomoyo_make_byte - Make byte value from three octal characters.
217 *
218 * @c1: The first character.
219 * @c2: The second character.
220 * @c3: The third character.
221 *
222 * Returns byte value.
223 */
224static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
225{
226 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
227}
228
229/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900230 * tomoyo_valid - Check whether the character is a valid char.
231 *
232 * @c: The character to check.
233 *
234 * Returns true if @c is a valid character, false otherwise.
235 */
236static inline bool tomoyo_valid(const unsigned char c)
237{
238 return c > ' ' && c < 127;
239}
240
241/**
242 * tomoyo_invalid - Check whether the character is an invalid char.
243 *
244 * @c: The character to check.
245 *
246 * Returns true if @c is an invalid character, false otherwise.
247 */
248static inline bool tomoyo_invalid(const unsigned char c)
249{
250 return c && (c <= ' ' || c >= 127);
251}
252
253/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900254 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
255 *
256 * @src: Pointer to pointer to the string.
257 * @find: Pointer to the keyword.
258 *
259 * Returns true if @src starts with @find, false otherwise.
260 *
261 * The @src is updated to point the first character after the @find
262 * if @src starts with @find.
263 */
264bool tomoyo_str_starts(char **src, const char *find)
265{
266 const int len = strlen(find);
267 char *tmp = *src;
268
269 if (strncmp(tmp, find, len))
270 return false;
271 tmp += len;
272 *src = tmp;
273 return true;
274}
275
276/**
277 * tomoyo_normalize_line - Format string.
278 *
279 * @buffer: The line to normalize.
280 *
281 * Leading and trailing whitespaces are removed.
282 * Multiple whitespaces are packed into single space.
283 *
284 * Returns nothing.
285 */
286void tomoyo_normalize_line(unsigned char *buffer)
287{
288 unsigned char *sp = buffer;
289 unsigned char *dp = buffer;
290 bool first = true;
291
Tetsuo Handa75093152010-06-16 16:23:55 +0900292 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900293 sp++;
294 while (*sp) {
295 if (!first)
296 *dp++ = ' ';
297 first = false;
Tetsuo Handa75093152010-06-16 16:23:55 +0900298 while (tomoyo_valid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900299 *dp++ = *sp++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900300 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900301 sp++;
302 }
303 *dp = '\0';
304}
305
306/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900307 * tomoyo_correct_word2 - Validate a string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900308 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900309 * @string: The string to check. May be non-'\0'-terminated.
310 * @len: Length of @string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900311 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900312 * Check whether the given string follows the naming rules.
313 * Returns true if @string follows the naming rules, false otherwise.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900314 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900315static bool tomoyo_correct_word2(const char *string, size_t len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900316{
Tetsuo Handa3f629632010-06-03 20:37:26 +0900317 const char *const start = string;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900318 bool in_repetition = false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900319 unsigned char c;
320 unsigned char d;
321 unsigned char e;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900322 if (!len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900323 goto out;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900324 while (len--) {
325 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900326 if (c == '\\') {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900327 if (!len--)
328 goto out;
329 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900330 switch (c) {
331 case '\\': /* "\\" */
332 continue;
333 case '$': /* "\$" */
334 case '+': /* "\+" */
335 case '?': /* "\?" */
336 case '*': /* "\*" */
337 case '@': /* "\@" */
338 case 'x': /* "\x" */
339 case 'X': /* "\X" */
340 case 'a': /* "\a" */
341 case 'A': /* "\A" */
342 case '-': /* "\-" */
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900343 continue;
344 case '{': /* "/\{" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900345 if (string - 3 < start || *(string - 3) != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900346 break;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900347 in_repetition = true;
348 continue;
349 case '}': /* "\}/" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900350 if (*string != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900351 break;
352 if (!in_repetition)
353 break;
354 in_repetition = false;
355 continue;
356 case '0': /* "\ooo" */
357 case '1':
358 case '2':
359 case '3':
Tetsuo Handa3f629632010-06-03 20:37:26 +0900360 if (!len-- || !len--)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900361 break;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900362 d = *string++;
363 e = *string++;
364 if (d < '0' || d > '7' || e < '0' || e > '7')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900365 break;
366 c = tomoyo_make_byte(c, d, e);
Tetsuo Handa75093152010-06-16 16:23:55 +0900367 if (tomoyo_invalid(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900368 continue; /* pattern is not \000 */
369 }
370 goto out;
371 } else if (in_repetition && c == '/') {
372 goto out;
Tetsuo Handa75093152010-06-16 16:23:55 +0900373 } else if (tomoyo_invalid(c)) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900374 goto out;
375 }
376 }
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900377 if (in_repetition)
378 goto out;
379 return true;
380 out:
381 return false;
382}
383
384/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900385 * tomoyo_correct_word - Validate a string.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900386 *
387 * @string: The string to check.
388 *
389 * Check whether the given string follows the naming rules.
390 * Returns true if @string follows the naming rules, false otherwise.
391 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900392bool tomoyo_correct_word(const char *string)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900393{
Tetsuo Handa75093152010-06-16 16:23:55 +0900394 return tomoyo_correct_word2(string, strlen(string));
Tetsuo Handa3f629632010-06-03 20:37:26 +0900395}
396
397/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900398 * tomoyo_correct_path - Validate a pathname.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900399 *
400 * @filename: The pathname to check.
401 *
402 * Check whether the given pathname follows the naming rules.
403 * Returns true if @filename follows the naming rules, false otherwise.
404 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900405bool tomoyo_correct_path(const char *filename)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900406{
Tetsuo Handa75093152010-06-16 16:23:55 +0900407 return *filename == '/' && tomoyo_correct_word(filename);
Tetsuo Handa3f629632010-06-03 20:37:26 +0900408}
409
410/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900411 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900412 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900413 * @domainname: The domainname to check.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900414 *
415 * Returns true if @domainname follows the naming rules, false otherwise.
416 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900417bool tomoyo_correct_domain(const unsigned char *domainname)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900418{
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900419 if (!domainname || strncmp(domainname, TOMOYO_ROOT_NAME,
420 TOMOYO_ROOT_NAME_LEN))
421 goto out;
422 domainname += TOMOYO_ROOT_NAME_LEN;
423 if (!*domainname)
424 return true;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900425 if (*domainname++ != ' ')
426 goto out;
427 while (1) {
428 const unsigned char *cp = strchr(domainname, ' ');
429 if (!cp)
430 break;
431 if (*domainname != '/' ||
Tetsuo Handae77dc342011-05-12 06:40:51 +0900432 !tomoyo_correct_word2(domainname, cp - domainname))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900433 goto out;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900434 domainname = cp + 1;
435 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900436 return tomoyo_correct_path(domainname);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900437 out:
438 return false;
439}
440
441/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900442 * tomoyo_domain_def - Check whether the given token can be a domainname.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900443 *
444 * @buffer: The token to check.
445 *
446 * Returns true if @buffer possibly be a domainname, false otherwise.
447 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900448bool tomoyo_domain_def(const unsigned char *buffer)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900449{
450 return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
451}
452
453/**
454 * tomoyo_find_domain - Find a domain by the given name.
455 *
456 * @domainname: The domainname to find.
457 *
458 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
459 *
460 * Caller holds tomoyo_read_lock().
461 */
462struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
463{
464 struct tomoyo_domain_info *domain;
465 struct tomoyo_path_info name;
466
467 name.name = domainname;
468 tomoyo_fill_path_info(&name);
469 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
470 if (!domain->is_deleted &&
471 !tomoyo_pathcmp(&name, domain->domainname))
472 return domain;
473 }
474 return NULL;
475}
476
477/**
478 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
479 *
480 * @filename: The string to evaluate.
481 *
482 * Returns the initial length without a pattern in @filename.
483 */
484static int tomoyo_const_part_length(const char *filename)
485{
486 char c;
487 int len = 0;
488
489 if (!filename)
490 return 0;
491 while ((c = *filename++) != '\0') {
492 if (c != '\\') {
493 len++;
494 continue;
495 }
496 c = *filename++;
497 switch (c) {
498 case '\\': /* "\\" */
499 len += 2;
500 continue;
501 case '0': /* "\ooo" */
502 case '1':
503 case '2':
504 case '3':
505 c = *filename++;
506 if (c < '0' || c > '7')
507 break;
508 c = *filename++;
509 if (c < '0' || c > '7')
510 break;
511 len += 4;
512 continue;
513 }
514 break;
515 }
516 return len;
517}
518
519/**
520 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
521 *
522 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
523 *
524 * The caller sets "struct tomoyo_path_info"->name.
525 */
526void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
527{
528 const char *name = ptr->name;
529 const int len = strlen(name);
530
531 ptr->const_len = tomoyo_const_part_length(name);
532 ptr->is_dir = len && (name[len - 1] == '/');
533 ptr->is_patterned = (ptr->const_len < len);
534 ptr->hash = full_name_hash(name, len);
535}
536
537/**
538 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
539 *
540 * @filename: The start of string to check.
541 * @filename_end: The end of string to check.
542 * @pattern: The start of pattern to compare.
543 * @pattern_end: The end of pattern to compare.
544 *
545 * Returns true if @filename matches @pattern, false otherwise.
546 */
547static bool tomoyo_file_matches_pattern2(const char *filename,
548 const char *filename_end,
549 const char *pattern,
550 const char *pattern_end)
551{
552 while (filename < filename_end && pattern < pattern_end) {
553 char c;
554 if (*pattern != '\\') {
555 if (*filename++ != *pattern++)
556 return false;
557 continue;
558 }
559 c = *filename;
560 pattern++;
561 switch (*pattern) {
562 int i;
563 int j;
564 case '?':
565 if (c == '/') {
566 return false;
567 } else if (c == '\\') {
568 if (filename[1] == '\\')
569 filename++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900570 else if (tomoyo_byte_range(filename + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900571 filename += 3;
572 else
573 return false;
574 }
575 break;
576 case '\\':
577 if (c != '\\')
578 return false;
579 if (*++filename != '\\')
580 return false;
581 break;
582 case '+':
583 if (!isdigit(c))
584 return false;
585 break;
586 case 'x':
587 if (!isxdigit(c))
588 return false;
589 break;
590 case 'a':
Tetsuo Handa75093152010-06-16 16:23:55 +0900591 if (!tomoyo_alphabet_char(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900592 return false;
593 break;
594 case '0':
595 case '1':
596 case '2':
597 case '3':
Tetsuo Handa75093152010-06-16 16:23:55 +0900598 if (c == '\\' && tomoyo_byte_range(filename + 1)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900599 && strncmp(filename + 1, pattern, 3) == 0) {
600 filename += 3;
601 pattern += 2;
602 break;
603 }
604 return false; /* Not matched. */
605 case '*':
606 case '@':
607 for (i = 0; i <= filename_end - filename; i++) {
608 if (tomoyo_file_matches_pattern2(
609 filename + i, filename_end,
610 pattern + 1, pattern_end))
611 return true;
612 c = filename[i];
613 if (c == '.' && *pattern == '@')
614 break;
615 if (c != '\\')
616 continue;
617 if (filename[i + 1] == '\\')
618 i++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900619 else if (tomoyo_byte_range(filename + i + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900620 i += 3;
621 else
622 break; /* Bad pattern. */
623 }
624 return false; /* Not matched. */
625 default:
626 j = 0;
627 c = *pattern;
628 if (c == '$') {
629 while (isdigit(filename[j]))
630 j++;
631 } else if (c == 'X') {
632 while (isxdigit(filename[j]))
633 j++;
634 } else if (c == 'A') {
Tetsuo Handa75093152010-06-16 16:23:55 +0900635 while (tomoyo_alphabet_char(filename[j]))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900636 j++;
637 }
638 for (i = 1; i <= j; i++) {
639 if (tomoyo_file_matches_pattern2(
640 filename + i, filename_end,
641 pattern + 1, pattern_end))
642 return true;
643 }
644 return false; /* Not matched or bad pattern. */
645 }
646 filename++;
647 pattern++;
648 }
649 while (*pattern == '\\' &&
650 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
651 pattern += 2;
652 return filename == filename_end && pattern == pattern_end;
653}
654
655/**
656 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
657 *
658 * @filename: The start of string to check.
659 * @filename_end: The end of string to check.
660 * @pattern: The start of pattern to compare.
661 * @pattern_end: The end of pattern to compare.
662 *
663 * Returns true if @filename matches @pattern, false otherwise.
664 */
665static bool tomoyo_file_matches_pattern(const char *filename,
666 const char *filename_end,
667 const char *pattern,
668 const char *pattern_end)
669{
670 const char *pattern_start = pattern;
671 bool first = true;
672 bool result;
673
674 while (pattern < pattern_end - 1) {
675 /* Split at "\-" pattern. */
676 if (*pattern++ != '\\' || *pattern++ != '-')
677 continue;
678 result = tomoyo_file_matches_pattern2(filename,
679 filename_end,
680 pattern_start,
681 pattern - 2);
682 if (first)
683 result = !result;
684 if (result)
685 return false;
686 first = false;
687 pattern_start = pattern;
688 }
689 result = tomoyo_file_matches_pattern2(filename, filename_end,
690 pattern_start, pattern_end);
691 return first ? result : !result;
692}
693
694/**
695 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
696 *
697 * @f: The start of string to check.
698 * @p: The start of pattern to compare.
699 *
700 * Returns true if @f matches @p, false otherwise.
701 */
702static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
703{
704 const char *f_delimiter;
705 const char *p_delimiter;
706
707 while (*f && *p) {
708 f_delimiter = strchr(f, '/');
709 if (!f_delimiter)
710 f_delimiter = f + strlen(f);
711 p_delimiter = strchr(p, '/');
712 if (!p_delimiter)
713 p_delimiter = p + strlen(p);
714 if (*p == '\\' && *(p + 1) == '{')
715 goto recursive;
716 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
717 p_delimiter))
718 return false;
719 f = f_delimiter;
720 if (*f)
721 f++;
722 p = p_delimiter;
723 if (*p)
724 p++;
725 }
726 /* Ignore trailing "\*" and "\@" in @pattern. */
727 while (*p == '\\' &&
728 (*(p + 1) == '*' || *(p + 1) == '@'))
729 p += 2;
730 return !*f && !*p;
731 recursive:
732 /*
733 * The "\{" pattern is permitted only after '/' character.
734 * This guarantees that below "*(p - 1)" is safe.
735 * Also, the "\}" pattern is permitted only before '/' character
736 * so that "\{" + "\}" pair will not break the "\-" operator.
737 */
738 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
739 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
740 return false; /* Bad pattern. */
741 do {
742 /* Compare current component with pattern. */
743 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
744 p_delimiter - 2))
745 break;
746 /* Proceed to next component. */
747 f = f_delimiter;
748 if (!*f)
749 break;
750 f++;
751 /* Continue comparison. */
752 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
753 return true;
754 f_delimiter = strchr(f, '/');
755 } while (f_delimiter);
756 return false; /* Not matched. */
757}
758
759/**
760 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
761 *
762 * @filename: The filename to check.
763 * @pattern: The pattern to compare.
764 *
765 * Returns true if matches, false otherwise.
766 *
767 * The following patterns are available.
768 * \\ \ itself.
769 * \ooo Octal representation of a byte.
770 * \* Zero or more repetitions of characters other than '/'.
771 * \@ Zero or more repetitions of characters other than '/' or '.'.
772 * \? 1 byte character other than '/'.
773 * \$ One or more repetitions of decimal digits.
774 * \+ 1 decimal digit.
775 * \X One or more repetitions of hexadecimal digits.
776 * \x 1 hexadecimal digit.
777 * \A One or more repetitions of alphabet characters.
778 * \a 1 alphabet character.
779 *
780 * \- Subtraction operator.
781 *
782 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
783 * /dir/dir/dir/ ).
784 */
785bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
786 const struct tomoyo_path_info *pattern)
787{
788 const char *f = filename->name;
789 const char *p = pattern->name;
790 const int len = pattern->const_len;
791
792 /* If @pattern doesn't contain pattern, I can use strcmp(). */
793 if (!pattern->is_patterned)
794 return !tomoyo_pathcmp(filename, pattern);
795 /* Don't compare directory and non-directory. */
796 if (filename->is_dir != pattern->is_dir)
797 return false;
798 /* Compare the initial length without patterns. */
799 if (strncmp(f, p, len))
800 return false;
801 f += len;
802 p += len;
803 return tomoyo_path_matches_pattern2(f, p);
804}
805
806/**
807 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
808 *
809 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
810 *
811 * This function uses kzalloc(), so the caller must call kfree()
812 * if this function didn't return NULL.
813 */
814const char *tomoyo_get_exe(void)
815{
816 struct mm_struct *mm = current->mm;
817 struct vm_area_struct *vma;
818 const char *cp = NULL;
819
820 if (!mm)
821 return NULL;
822 down_read(&mm->mmap_sem);
823 for (vma = mm->mmap; vma; vma = vma->vm_next) {
824 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
825 cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
826 break;
827 }
828 }
829 up_read(&mm->mmap_sem);
830 return cp;
831}
832
833/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900834 * tomoyo_get_mode - Get MAC mode.
835 *
836 * @profile: Profile number.
837 * @index: Index number of functionality.
838 *
839 * Returns mode.
840 */
841int tomoyo_get_mode(const u8 profile, const u8 index)
842{
843 u8 mode;
844 const u8 category = TOMOYO_MAC_CATEGORY_FILE;
845 if (!tomoyo_policy_loaded)
846 return TOMOYO_CONFIG_DISABLED;
847 mode = tomoyo_profile(profile)->config[index];
848 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
849 mode = tomoyo_profile(profile)->config[category];
850 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
851 mode = tomoyo_profile(profile)->default_config;
852 return mode & 3;
853}
854
855/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900856 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
857 *
858 * @r: Pointer to "struct tomoyo_request_info" to initialize.
859 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
Tetsuo Handa57c25902010-06-03 20:38:44 +0900860 * @index: Index number of functionality.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900861 *
862 * Returns mode.
863 */
864int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900865 struct tomoyo_domain_info *domain, const u8 index)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900866{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900867 u8 profile;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900868 memset(r, 0, sizeof(*r));
869 if (!domain)
870 domain = tomoyo_domain();
871 r->domain = domain;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900872 profile = domain->profile;
873 r->profile = profile;
874 r->type = index;
875 r->mode = tomoyo_get_mode(profile, index);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900876 return r->mode;
877}
878
879/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900880 * tomoyo_last_word - Get last component of a line.
881 *
882 * @line: A line.
883 *
884 * Returns the last word of a line.
885 */
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900886const char *tomoyo_last_word(const char *name)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900887{
888 const char *cp = strrchr(name, ' ');
889 if (cp)
890 return cp + 1;
891 return name;
892}
893
894/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900895 * tomoyo_warn_log - Print warning or error message on console.
896 *
897 * @r: Pointer to "struct tomoyo_request_info".
898 * @fmt: The printf()'s format string, followed by parameters.
899 */
900void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
901{
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900902 va_list args;
903 char *buffer;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900904 const struct tomoyo_domain_info * const domain = r->domain;
905 const struct tomoyo_profile *profile = tomoyo_profile(domain->profile);
906 switch (r->mode) {
907 case TOMOYO_CONFIG_ENFORCING:
908 if (!profile->enforcing->enforcing_verbose)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900909 return;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900910 break;
911 case TOMOYO_CONFIG_PERMISSIVE:
912 if (!profile->permissive->permissive_verbose)
913 return;
914 break;
915 case TOMOYO_CONFIG_LEARNING:
916 if (!profile->learning->learning_verbose)
917 return;
918 break;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900919 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900920 buffer = kmalloc(4096, GFP_NOFS);
921 if (!buffer)
922 return;
923 va_start(args, fmt);
924 vsnprintf(buffer, 4095, fmt, args);
925 va_end(args);
926 buffer[4095] = '\0';
927 printk(KERN_WARNING "%s: Access %s denied for %s\n",
928 r->mode == TOMOYO_CONFIG_ENFORCING ? "ERROR" : "WARNING", buffer,
929 tomoyo_last_word(domain->domainname->name));
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900930 kfree(buffer);
931}
932
933/**
934 * tomoyo_domain_quota_is_ok - Check for domain's quota.
935 *
936 * @r: Pointer to "struct tomoyo_request_info".
937 *
938 * Returns true if the domain is not exceeded quota, false otherwise.
939 *
940 * Caller holds tomoyo_read_lock().
941 */
942bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
943{
944 unsigned int count = 0;
945 struct tomoyo_domain_info *domain = r->domain;
946 struct tomoyo_acl_info *ptr;
947
948 if (r->mode != TOMOYO_CONFIG_LEARNING)
949 return false;
950 if (!domain)
951 return true;
952 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7c759642011-06-26 23:15:31 +0900953 u16 perm;
954 u8 i;
Tetsuo Handa7e3d1992010-07-27 10:08:29 +0900955 if (ptr->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900956 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900957 switch (ptr->type) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900958 case TOMOYO_TYPE_PATH_ACL:
959 perm = container_of(ptr, struct tomoyo_path_acl, head)
960 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900961 break;
962 case TOMOYO_TYPE_PATH2_ACL:
963 perm = container_of(ptr, struct tomoyo_path2_acl, head)
964 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900965 break;
966 case TOMOYO_TYPE_PATH_NUMBER_ACL:
967 perm = container_of(ptr, struct tomoyo_path_number_acl,
968 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900969 break;
Tetsuo Handa75093152010-06-16 16:23:55 +0900970 case TOMOYO_TYPE_MKDEV_ACL:
971 perm = container_of(ptr, struct tomoyo_mkdev_acl,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900972 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900973 break;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900974 default:
Tetsuo Handa7c759642011-06-26 23:15:31 +0900975 perm = 1;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900976 }
Tetsuo Handa7c759642011-06-26 23:15:31 +0900977 for (i = 0; i < 16; i++)
978 if (perm & (1 << i))
979 count++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900980 }
Tetsuo Handa57c25902010-06-03 20:38:44 +0900981 if (count < tomoyo_profile(domain->profile)->learning->
982 learning_max_entry)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900983 return true;
984 if (!domain->quota_warned) {
985 domain->quota_warned = true;
986 printk(KERN_WARNING "TOMOYO-WARNING: "
987 "Domain '%s' has so many ACLs to hold. "
988 "Stopped learning mode.\n", domain->domainname->name);
989 }
990 return false;
991}