IB/mlx4: Fix incorrectly releasing steerable UD QPs when have only ETH ports
[linux-2.6-block.git] / security / tomoyo / util.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
c3ef1500
TH
2/*
3 * security/tomoyo/util.c
4 *
0f2a55d5 5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
c3ef1500
TH
6 */
7
8#include <linux/slab.h>
b2d09103
IM
9#include <linux/rculist.h>
10
c3ef1500
TH
11#include "common.h"
12
13/* Lock for protecting policy. */
14DEFINE_MUTEX(tomoyo_policy_lock);
15
16/* Has /sbin/init started? */
17bool tomoyo_policy_loaded;
18
2c47ab93
TH
19/*
20 * Mapping table from "enum tomoyo_mac_index" to
21 * "enum tomoyo_mac_category_index".
22 */
23const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = {
24 /* CONFIG::file group */
25 [TOMOYO_MAC_FILE_EXECUTE] = TOMOYO_MAC_CATEGORY_FILE,
26 [TOMOYO_MAC_FILE_OPEN] = TOMOYO_MAC_CATEGORY_FILE,
27 [TOMOYO_MAC_FILE_CREATE] = TOMOYO_MAC_CATEGORY_FILE,
28 [TOMOYO_MAC_FILE_UNLINK] = TOMOYO_MAC_CATEGORY_FILE,
29 [TOMOYO_MAC_FILE_GETATTR] = TOMOYO_MAC_CATEGORY_FILE,
30 [TOMOYO_MAC_FILE_MKDIR] = TOMOYO_MAC_CATEGORY_FILE,
31 [TOMOYO_MAC_FILE_RMDIR] = TOMOYO_MAC_CATEGORY_FILE,
32 [TOMOYO_MAC_FILE_MKFIFO] = TOMOYO_MAC_CATEGORY_FILE,
33 [TOMOYO_MAC_FILE_MKSOCK] = TOMOYO_MAC_CATEGORY_FILE,
34 [TOMOYO_MAC_FILE_TRUNCATE] = TOMOYO_MAC_CATEGORY_FILE,
35 [TOMOYO_MAC_FILE_SYMLINK] = TOMOYO_MAC_CATEGORY_FILE,
36 [TOMOYO_MAC_FILE_MKBLOCK] = TOMOYO_MAC_CATEGORY_FILE,
37 [TOMOYO_MAC_FILE_MKCHAR] = TOMOYO_MAC_CATEGORY_FILE,
38 [TOMOYO_MAC_FILE_LINK] = TOMOYO_MAC_CATEGORY_FILE,
39 [TOMOYO_MAC_FILE_RENAME] = TOMOYO_MAC_CATEGORY_FILE,
40 [TOMOYO_MAC_FILE_CHMOD] = TOMOYO_MAC_CATEGORY_FILE,
41 [TOMOYO_MAC_FILE_CHOWN] = TOMOYO_MAC_CATEGORY_FILE,
42 [TOMOYO_MAC_FILE_CHGRP] = TOMOYO_MAC_CATEGORY_FILE,
43 [TOMOYO_MAC_FILE_IOCTL] = TOMOYO_MAC_CATEGORY_FILE,
44 [TOMOYO_MAC_FILE_CHROOT] = TOMOYO_MAC_CATEGORY_FILE,
45 [TOMOYO_MAC_FILE_MOUNT] = TOMOYO_MAC_CATEGORY_FILE,
46 [TOMOYO_MAC_FILE_UMOUNT] = TOMOYO_MAC_CATEGORY_FILE,
47 [TOMOYO_MAC_FILE_PIVOT_ROOT] = TOMOYO_MAC_CATEGORY_FILE,
059d84db
TH
48 /* CONFIG::network group */
49 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND] =
50 TOMOYO_MAC_CATEGORY_NETWORK,
51 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN] =
52 TOMOYO_MAC_CATEGORY_NETWORK,
53 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT] =
54 TOMOYO_MAC_CATEGORY_NETWORK,
55 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND] =
56 TOMOYO_MAC_CATEGORY_NETWORK,
57 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND] =
58 TOMOYO_MAC_CATEGORY_NETWORK,
59 [TOMOYO_MAC_NETWORK_INET_RAW_BIND] =
60 TOMOYO_MAC_CATEGORY_NETWORK,
61 [TOMOYO_MAC_NETWORK_INET_RAW_SEND] =
62 TOMOYO_MAC_CATEGORY_NETWORK,
63 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND] =
64 TOMOYO_MAC_CATEGORY_NETWORK,
65 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN] =
66 TOMOYO_MAC_CATEGORY_NETWORK,
67 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT] =
68 TOMOYO_MAC_CATEGORY_NETWORK,
69 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND] =
70 TOMOYO_MAC_CATEGORY_NETWORK,
71 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND] =
72 TOMOYO_MAC_CATEGORY_NETWORK,
73 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND] =
74 TOMOYO_MAC_CATEGORY_NETWORK,
75 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] =
76 TOMOYO_MAC_CATEGORY_NETWORK,
77 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] =
78 TOMOYO_MAC_CATEGORY_NETWORK,
d58e0da8
TH
79 /* CONFIG::misc group */
80 [TOMOYO_MAC_ENVIRON] = TOMOYO_MAC_CATEGORY_MISC,
2c47ab93
TH
81};
82
b22b8b9f
TH
83/**
84 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
85 *
86 * @time: Seconds since 1970/01/01 00:00:00.
87 * @stamp: Pointer to "struct tomoyo_time".
88 *
89 * Returns nothing.
b22b8b9f 90 */
92734092 91void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp)
b22b8b9f 92{
92734092
AB
93 struct tm tm;
94 time64_to_tm(time64, 0, &tm);
95 stamp->sec = tm.tm_sec;
96 stamp->min = tm.tm_min;
97 stamp->hour = tm.tm_hour;
98 stamp->day = tm.tm_mday;
99 stamp->month = tm.tm_mon + 1;
100 stamp->year = tm.tm_year + 1900;
b22b8b9f
TH
101}
102
a238cf5b
TH
103/**
104 * tomoyo_permstr - Find permission keywords.
105 *
106 * @string: String representation for permissions in foo/bar/buz format.
107 * @keyword: Keyword to find from @string/
108 *
109 * Returns ture if @keyword was found in @string, false otherwise.
110 *
111 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
112 */
113bool tomoyo_permstr(const char *string, const char *keyword)
114{
115 const char *cp = strstr(string, keyword);
116 if (cp)
117 return cp == string || *(cp - 1) == '/';
118 return false;
119}
120
121/**
122 * tomoyo_read_token - Read a word from a line.
123 *
124 * @param: Pointer to "struct tomoyo_acl_param".
125 *
126 * Returns a word on success, "" otherwise.
127 *
128 * To allow the caller to skip NULL check, this function returns "" rather than
129 * NULL if there is no more words to read.
130 */
131char *tomoyo_read_token(struct tomoyo_acl_param *param)
132{
133 char *pos = param->data;
134 char *del = strchr(pos, ' ');
135 if (del)
136 *del++ = '\0';
137 else
138 del = pos + strlen(pos);
139 param->data = del;
140 return pos;
141}
142
731d37aa
TH
143/**
144 * tomoyo_get_domainname - Read a domainname from a line.
145 *
146 * @param: Pointer to "struct tomoyo_acl_param".
147 *
148 * Returns a domainname on success, NULL otherwise.
149 */
150const struct tomoyo_path_info *tomoyo_get_domainname
151(struct tomoyo_acl_param *param)
152{
153 char *start = param->data;
154 char *pos = start;
155 while (*pos) {
156 if (*pos++ != ' ' || *pos++ == '/')
157 continue;
158 pos -= 2;
159 *pos++ = '\0';
160 break;
161 }
162 param->data = pos;
163 if (tomoyo_correct_domain(start))
164 return tomoyo_get_name(start);
165 return NULL;
166}
167
c3ef1500
TH
168/**
169 * tomoyo_parse_ulong - Parse an "unsigned long" value.
170 *
171 * @result: Pointer to "unsigned long".
172 * @str: Pointer to string to parse.
173 *
0df7e8b8 174 * Returns one of values in "enum tomoyo_value_type".
c3ef1500
TH
175 *
176 * The @src is updated to point the first character after the value
177 * on success.
178 */
2066a361 179u8 tomoyo_parse_ulong(unsigned long *result, char **str)
c3ef1500
TH
180{
181 const char *cp = *str;
182 char *ep;
183 int base = 10;
184 if (*cp == '0') {
185 char c = *(cp + 1);
186 if (c == 'x' || c == 'X') {
187 base = 16;
188 cp += 2;
189 } else if (c >= '0' && c <= '7') {
190 base = 8;
191 cp++;
192 }
193 }
194 *result = simple_strtoul(cp, &ep, base);
195 if (cp == ep)
0df7e8b8 196 return TOMOYO_VALUE_TYPE_INVALID;
c3ef1500
TH
197 *str = ep;
198 switch (base) {
199 case 16:
200 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
201 case 8:
202 return TOMOYO_VALUE_TYPE_OCTAL;
203 default:
204 return TOMOYO_VALUE_TYPE_DECIMAL;
205 }
206}
207
208/**
209 * tomoyo_print_ulong - Print an "unsigned long" value.
210 *
211 * @buffer: Pointer to buffer.
212 * @buffer_len: Size of @buffer.
213 * @value: An "unsigned long" value.
214 * @type: Type of @value.
215 *
216 * Returns nothing.
217 */
218void tomoyo_print_ulong(char *buffer, const int buffer_len,
219 const unsigned long value, const u8 type)
220{
221 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
222 snprintf(buffer, buffer_len, "%lu", value);
223 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
224 snprintf(buffer, buffer_len, "0%lo", value);
225 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
226 snprintf(buffer, buffer_len, "0x%lX", value);
227 else
228 snprintf(buffer, buffer_len, "type(%u)", type);
229}
230
231/**
232 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
233 *
a238cf5b
TH
234 * @param: Pointer to "struct tomoyo_acl_param".
235 * @ptr: Pointer to "struct tomoyo_name_union".
c3ef1500
TH
236 *
237 * Returns true on success, false otherwise.
238 */
a238cf5b 239bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
c3ef1500
TH
240 struct tomoyo_name_union *ptr)
241{
a238cf5b
TH
242 char *filename;
243 if (param->data[0] == '@') {
244 param->data++;
245 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
c3ef1500
TH
246 return ptr->group != NULL;
247 }
a238cf5b
TH
248 filename = tomoyo_read_token(param);
249 if (!tomoyo_correct_word(filename))
250 return false;
c3ef1500 251 ptr->filename = tomoyo_get_name(filename);
c3ef1500
TH
252 return ptr->filename != NULL;
253}
254
255/**
256 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
257 *
a238cf5b
TH
258 * @param: Pointer to "struct tomoyo_acl_param".
259 * @ptr: Pointer to "struct tomoyo_number_union".
c3ef1500
TH
260 *
261 * Returns true on success, false otherwise.
262 */
a238cf5b
TH
263bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
264 struct tomoyo_number_union *ptr)
c3ef1500 265{
a238cf5b 266 char *data;
c3ef1500
TH
267 u8 type;
268 unsigned long v;
a238cf5b
TH
269 memset(ptr, 0, sizeof(*ptr));
270 if (param->data[0] == '@') {
271 param->data++;
272 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
273 return ptr->group != NULL;
c3ef1500 274 }
a238cf5b 275 data = tomoyo_read_token(param);
c3ef1500 276 type = tomoyo_parse_ulong(&v, &data);
a238cf5b 277 if (type == TOMOYO_VALUE_TYPE_INVALID)
c3ef1500 278 return false;
a238cf5b
TH
279 ptr->values[0] = v;
280 ptr->value_type[0] = type;
c3ef1500 281 if (!*data) {
a238cf5b
TH
282 ptr->values[1] = v;
283 ptr->value_type[1] = type;
c3ef1500
TH
284 return true;
285 }
286 if (*data++ != '-')
287 return false;
288 type = tomoyo_parse_ulong(&v, &data);
a238cf5b 289 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
c3ef1500 290 return false;
a238cf5b
TH
291 ptr->values[1] = v;
292 ptr->value_type[1] = type;
c3ef1500
TH
293 return true;
294}
295
296/**
75093152 297 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
c3ef1500
TH
298 *
299 * @str: Pointer to the string.
300 *
301 * Returns true if @str is a \ooo style octal value, false otherwise.
302 *
303 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
304 * This function verifies that \ooo is in valid range.
305 */
75093152 306static inline bool tomoyo_byte_range(const char *str)
c3ef1500
TH
307{
308 return *str >= '0' && *str++ <= '3' &&
309 *str >= '0' && *str++ <= '7' &&
310 *str >= '0' && *str <= '7';
311}
312
313/**
75093152 314 * tomoyo_alphabet_char - Check whether the character is an alphabet.
c3ef1500
TH
315 *
316 * @c: The character to check.
317 *
318 * Returns true if @c is an alphabet character, false otherwise.
319 */
75093152 320static inline bool tomoyo_alphabet_char(const char c)
c3ef1500
TH
321{
322 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
323}
324
325/**
326 * tomoyo_make_byte - Make byte value from three octal characters.
327 *
328 * @c1: The first character.
329 * @c2: The second character.
330 * @c3: The third character.
331 *
332 * Returns byte value.
333 */
334static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
335{
336 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
337}
338
0df7e8b8
TH
339/**
340 * tomoyo_valid - Check whether the character is a valid char.
341 *
342 * @c: The character to check.
343 *
344 * Returns true if @c is a valid character, false otherwise.
345 */
346static inline bool tomoyo_valid(const unsigned char c)
347{
348 return c > ' ' && c < 127;
349}
350
351/**
352 * tomoyo_invalid - Check whether the character is an invalid char.
353 *
354 * @c: The character to check.
355 *
356 * Returns true if @c is an invalid character, false otherwise.
357 */
358static inline bool tomoyo_invalid(const unsigned char c)
359{
360 return c && (c <= ' ' || c >= 127);
361}
362
c3ef1500
TH
363/**
364 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
365 *
366 * @src: Pointer to pointer to the string.
367 * @find: Pointer to the keyword.
368 *
369 * Returns true if @src starts with @find, false otherwise.
370 *
371 * The @src is updated to point the first character after the @find
372 * if @src starts with @find.
373 */
374bool tomoyo_str_starts(char **src, const char *find)
375{
376 const int len = strlen(find);
377 char *tmp = *src;
378
379 if (strncmp(tmp, find, len))
380 return false;
381 tmp += len;
382 *src = tmp;
383 return true;
384}
385
386/**
387 * tomoyo_normalize_line - Format string.
388 *
389 * @buffer: The line to normalize.
390 *
391 * Leading and trailing whitespaces are removed.
392 * Multiple whitespaces are packed into single space.
393 *
394 * Returns nothing.
395 */
396void tomoyo_normalize_line(unsigned char *buffer)
397{
398 unsigned char *sp = buffer;
399 unsigned char *dp = buffer;
400 bool first = true;
401
75093152 402 while (tomoyo_invalid(*sp))
c3ef1500
TH
403 sp++;
404 while (*sp) {
405 if (!first)
406 *dp++ = ' ';
407 first = false;
75093152 408 while (tomoyo_valid(*sp))
c3ef1500 409 *dp++ = *sp++;
75093152 410 while (tomoyo_invalid(*sp))
c3ef1500
TH
411 sp++;
412 }
413 *dp = '\0';
414}
415
c3ef1500 416/**
75093152 417 * tomoyo_correct_word2 - Validate a string.
c3ef1500 418 *
0f2a55d5 419 * @string: The string to check. Maybe non-'\0'-terminated.
3f629636 420 * @len: Length of @string.
c3ef1500 421 *
3f629636
TH
422 * Check whether the given string follows the naming rules.
423 * Returns true if @string follows the naming rules, false otherwise.
c3ef1500 424 */
75093152 425static bool tomoyo_correct_word2(const char *string, size_t len)
c3ef1500 426{
3f629636 427 const char *const start = string;
c3ef1500 428 bool in_repetition = false;
c3ef1500
TH
429 unsigned char c;
430 unsigned char d;
431 unsigned char e;
3f629636 432 if (!len)
c3ef1500 433 goto out;
3f629636
TH
434 while (len--) {
435 c = *string++;
c3ef1500 436 if (c == '\\') {
3f629636
TH
437 if (!len--)
438 goto out;
439 c = *string++;
c3ef1500
TH
440 switch (c) {
441 case '\\': /* "\\" */
442 continue;
443 case '$': /* "\$" */
444 case '+': /* "\+" */
445 case '?': /* "\?" */
446 case '*': /* "\*" */
447 case '@': /* "\@" */
448 case 'x': /* "\x" */
449 case 'X': /* "\X" */
450 case 'a': /* "\a" */
451 case 'A': /* "\A" */
452 case '-': /* "\-" */
c3ef1500
TH
453 continue;
454 case '{': /* "/\{" */
3f629636 455 if (string - 3 < start || *(string - 3) != '/')
c3ef1500 456 break;
c3ef1500
TH
457 in_repetition = true;
458 continue;
459 case '}': /* "\}/" */
3f629636 460 if (*string != '/')
c3ef1500
TH
461 break;
462 if (!in_repetition)
463 break;
464 in_repetition = false;
465 continue;
466 case '0': /* "\ooo" */
467 case '1':
468 case '2':
469 case '3':
3f629636 470 if (!len-- || !len--)
c3ef1500 471 break;
3f629636
TH
472 d = *string++;
473 e = *string++;
474 if (d < '0' || d > '7' || e < '0' || e > '7')
c3ef1500
TH
475 break;
476 c = tomoyo_make_byte(c, d, e);
25add8cf
TH
477 if (c <= ' ' || c >= 127)
478 continue;
c3ef1500
TH
479 }
480 goto out;
481 } else if (in_repetition && c == '/') {
482 goto out;
25add8cf 483 } else if (c <= ' ' || c >= 127) {
c3ef1500
TH
484 goto out;
485 }
486 }
c3ef1500
TH
487 if (in_repetition)
488 goto out;
489 return true;
490 out:
491 return false;
492}
493
3f629636 494/**
75093152 495 * tomoyo_correct_word - Validate a string.
3f629636
TH
496 *
497 * @string: The string to check.
498 *
499 * Check whether the given string follows the naming rules.
500 * Returns true if @string follows the naming rules, false otherwise.
501 */
75093152 502bool tomoyo_correct_word(const char *string)
3f629636 503{
75093152 504 return tomoyo_correct_word2(string, strlen(string));
3f629636
TH
505}
506
507/**
75093152 508 * tomoyo_correct_path - Validate a pathname.
3f629636
TH
509 *
510 * @filename: The pathname to check.
511 *
512 * Check whether the given pathname follows the naming rules.
513 * Returns true if @filename follows the naming rules, false otherwise.
514 */
75093152 515bool tomoyo_correct_path(const char *filename)
3f629636 516{
75093152 517 return *filename == '/' && tomoyo_correct_word(filename);
3f629636
TH
518}
519
c3ef1500 520/**
75093152 521 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
c3ef1500 522 *
3f629636 523 * @domainname: The domainname to check.
c3ef1500
TH
524 *
525 * Returns true if @domainname follows the naming rules, false otherwise.
526 */
75093152 527bool tomoyo_correct_domain(const unsigned char *domainname)
c3ef1500 528{
bd03a3e4
TH
529 if (!domainname || !tomoyo_domain_def(domainname))
530 return false;
531 domainname = strchr(domainname, ' ');
532 if (!domainname++)
c3ef1500 533 return true;
3f629636
TH
534 while (1) {
535 const unsigned char *cp = strchr(domainname, ' ');
536 if (!cp)
537 break;
538 if (*domainname != '/' ||
e77dc346 539 !tomoyo_correct_word2(domainname, cp - domainname))
bd03a3e4 540 return false;
3f629636
TH
541 domainname = cp + 1;
542 }
75093152 543 return tomoyo_correct_path(domainname);
c3ef1500
TH
544}
545
546/**
75093152 547 * tomoyo_domain_def - Check whether the given token can be a domainname.
c3ef1500
TH
548 *
549 * @buffer: The token to check.
550 *
551 * Returns true if @buffer possibly be a domainname, false otherwise.
552 */
75093152 553bool tomoyo_domain_def(const unsigned char *buffer)
c3ef1500 554{
bd03a3e4
TH
555 const unsigned char *cp;
556 int len;
557 if (*buffer != '<')
558 return false;
559 cp = strchr(buffer, ' ');
560 if (!cp)
561 len = strlen(buffer);
562 else
563 len = cp - buffer;
564 if (buffer[len - 1] != '>' ||
565 !tomoyo_correct_word2(buffer + 1, len - 2))
566 return false;
567 return true;
c3ef1500
TH
568}
569
570/**
571 * tomoyo_find_domain - Find a domain by the given name.
572 *
573 * @domainname: The domainname to find.
574 *
575 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
576 *
577 * Caller holds tomoyo_read_lock().
578 */
579struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
580{
581 struct tomoyo_domain_info *domain;
582 struct tomoyo_path_info name;
583
584 name.name = domainname;
585 tomoyo_fill_path_info(&name);
586 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
587 if (!domain->is_deleted &&
588 !tomoyo_pathcmp(&name, domain->domainname))
589 return domain;
590 }
591 return NULL;
592}
593
594/**
595 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
596 *
597 * @filename: The string to evaluate.
598 *
599 * Returns the initial length without a pattern in @filename.
600 */
601static int tomoyo_const_part_length(const char *filename)
602{
603 char c;
604 int len = 0;
605
606 if (!filename)
607 return 0;
608 while ((c = *filename++) != '\0') {
609 if (c != '\\') {
610 len++;
611 continue;
612 }
613 c = *filename++;
614 switch (c) {
615 case '\\': /* "\\" */
616 len += 2;
617 continue;
618 case '0': /* "\ooo" */
619 case '1':
620 case '2':
621 case '3':
622 c = *filename++;
623 if (c < '0' || c > '7')
624 break;
625 c = *filename++;
626 if (c < '0' || c > '7')
627 break;
628 len += 4;
629 continue;
630 }
631 break;
632 }
633 return len;
634}
635
636/**
637 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
638 *
639 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
640 *
641 * The caller sets "struct tomoyo_path_info"->name.
642 */
643void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
644{
645 const char *name = ptr->name;
646 const int len = strlen(name);
647
648 ptr->const_len = tomoyo_const_part_length(name);
649 ptr->is_dir = len && (name[len - 1] == '/');
650 ptr->is_patterned = (ptr->const_len < len);
8387ff25 651 ptr->hash = full_name_hash(NULL, name, len);
c3ef1500
TH
652}
653
654/**
655 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
656 *
657 * @filename: The start of string to check.
658 * @filename_end: The end of string to check.
659 * @pattern: The start of pattern to compare.
660 * @pattern_end: The end of pattern to compare.
661 *
662 * Returns true if @filename matches @pattern, false otherwise.
663 */
664static bool tomoyo_file_matches_pattern2(const char *filename,
665 const char *filename_end,
666 const char *pattern,
667 const char *pattern_end)
668{
669 while (filename < filename_end && pattern < pattern_end) {
670 char c;
671 if (*pattern != '\\') {
672 if (*filename++ != *pattern++)
673 return false;
674 continue;
675 }
676 c = *filename;
677 pattern++;
678 switch (*pattern) {
679 int i;
680 int j;
681 case '?':
682 if (c == '/') {
683 return false;
684 } else if (c == '\\') {
685 if (filename[1] == '\\')
686 filename++;
75093152 687 else if (tomoyo_byte_range(filename + 1))
c3ef1500
TH
688 filename += 3;
689 else
690 return false;
691 }
692 break;
693 case '\\':
694 if (c != '\\')
695 return false;
696 if (*++filename != '\\')
697 return false;
698 break;
699 case '+':
700 if (!isdigit(c))
701 return false;
702 break;
703 case 'x':
704 if (!isxdigit(c))
705 return false;
706 break;
707 case 'a':
75093152 708 if (!tomoyo_alphabet_char(c))
c3ef1500
TH
709 return false;
710 break;
711 case '0':
712 case '1':
713 case '2':
714 case '3':
75093152 715 if (c == '\\' && tomoyo_byte_range(filename + 1)
c3ef1500
TH
716 && strncmp(filename + 1, pattern, 3) == 0) {
717 filename += 3;
718 pattern += 2;
719 break;
720 }
721 return false; /* Not matched. */
722 case '*':
723 case '@':
724 for (i = 0; i <= filename_end - filename; i++) {
725 if (tomoyo_file_matches_pattern2(
726 filename + i, filename_end,
727 pattern + 1, pattern_end))
728 return true;
729 c = filename[i];
730 if (c == '.' && *pattern == '@')
731 break;
732 if (c != '\\')
733 continue;
734 if (filename[i + 1] == '\\')
735 i++;
75093152 736 else if (tomoyo_byte_range(filename + i + 1))
c3ef1500
TH
737 i += 3;
738 else
739 break; /* Bad pattern. */
740 }
741 return false; /* Not matched. */
742 default:
743 j = 0;
744 c = *pattern;
745 if (c == '$') {
746 while (isdigit(filename[j]))
747 j++;
748 } else if (c == 'X') {
749 while (isxdigit(filename[j]))
750 j++;
751 } else if (c == 'A') {
75093152 752 while (tomoyo_alphabet_char(filename[j]))
c3ef1500
TH
753 j++;
754 }
755 for (i = 1; i <= j; i++) {
756 if (tomoyo_file_matches_pattern2(
757 filename + i, filename_end,
758 pattern + 1, pattern_end))
759 return true;
760 }
761 return false; /* Not matched or bad pattern. */
762 }
763 filename++;
764 pattern++;
765 }
766 while (*pattern == '\\' &&
767 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
768 pattern += 2;
769 return filename == filename_end && pattern == pattern_end;
770}
771
772/**
773 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
774 *
775 * @filename: The start of string to check.
776 * @filename_end: The end of string to check.
777 * @pattern: The start of pattern to compare.
778 * @pattern_end: The end of pattern to compare.
779 *
780 * Returns true if @filename matches @pattern, false otherwise.
781 */
782static bool tomoyo_file_matches_pattern(const char *filename,
783 const char *filename_end,
784 const char *pattern,
785 const char *pattern_end)
786{
787 const char *pattern_start = pattern;
788 bool first = true;
789 bool result;
790
791 while (pattern < pattern_end - 1) {
792 /* Split at "\-" pattern. */
793 if (*pattern++ != '\\' || *pattern++ != '-')
794 continue;
795 result = tomoyo_file_matches_pattern2(filename,
796 filename_end,
797 pattern_start,
798 pattern - 2);
799 if (first)
800 result = !result;
801 if (result)
802 return false;
803 first = false;
804 pattern_start = pattern;
805 }
806 result = tomoyo_file_matches_pattern2(filename, filename_end,
807 pattern_start, pattern_end);
808 return first ? result : !result;
809}
810
811/**
812 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
813 *
814 * @f: The start of string to check.
815 * @p: The start of pattern to compare.
816 *
817 * Returns true if @f matches @p, false otherwise.
818 */
819static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
820{
821 const char *f_delimiter;
822 const char *p_delimiter;
823
824 while (*f && *p) {
825 f_delimiter = strchr(f, '/');
826 if (!f_delimiter)
827 f_delimiter = f + strlen(f);
828 p_delimiter = strchr(p, '/');
829 if (!p_delimiter)
830 p_delimiter = p + strlen(p);
831 if (*p == '\\' && *(p + 1) == '{')
832 goto recursive;
833 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
834 p_delimiter))
835 return false;
836 f = f_delimiter;
837 if (*f)
838 f++;
839 p = p_delimiter;
840 if (*p)
841 p++;
842 }
843 /* Ignore trailing "\*" and "\@" in @pattern. */
844 while (*p == '\\' &&
845 (*(p + 1) == '*' || *(p + 1) == '@'))
846 p += 2;
847 return !*f && !*p;
848 recursive:
849 /*
850 * The "\{" pattern is permitted only after '/' character.
851 * This guarantees that below "*(p - 1)" is safe.
852 * Also, the "\}" pattern is permitted only before '/' character
853 * so that "\{" + "\}" pair will not break the "\-" operator.
854 */
855 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
856 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
857 return false; /* Bad pattern. */
858 do {
859 /* Compare current component with pattern. */
860 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
861 p_delimiter - 2))
862 break;
863 /* Proceed to next component. */
864 f = f_delimiter;
865 if (!*f)
866 break;
867 f++;
868 /* Continue comparison. */
869 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
870 return true;
871 f_delimiter = strchr(f, '/');
872 } while (f_delimiter);
873 return false; /* Not matched. */
874}
875
876/**
877 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
878 *
879 * @filename: The filename to check.
880 * @pattern: The pattern to compare.
881 *
882 * Returns true if matches, false otherwise.
883 *
884 * The following patterns are available.
885 * \\ \ itself.
886 * \ooo Octal representation of a byte.
887 * \* Zero or more repetitions of characters other than '/'.
888 * \@ Zero or more repetitions of characters other than '/' or '.'.
889 * \? 1 byte character other than '/'.
890 * \$ One or more repetitions of decimal digits.
891 * \+ 1 decimal digit.
892 * \X One or more repetitions of hexadecimal digits.
893 * \x 1 hexadecimal digit.
894 * \A One or more repetitions of alphabet characters.
895 * \a 1 alphabet character.
896 *
897 * \- Subtraction operator.
898 *
899 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
900 * /dir/dir/dir/ ).
901 */
902bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
903 const struct tomoyo_path_info *pattern)
904{
905 const char *f = filename->name;
906 const char *p = pattern->name;
907 const int len = pattern->const_len;
908
909 /* If @pattern doesn't contain pattern, I can use strcmp(). */
910 if (!pattern->is_patterned)
911 return !tomoyo_pathcmp(filename, pattern);
912 /* Don't compare directory and non-directory. */
913 if (filename->is_dir != pattern->is_dir)
914 return false;
915 /* Compare the initial length without patterns. */
916 if (strncmp(f, p, len))
917 return false;
918 f += len;
919 p += len;
920 return tomoyo_path_matches_pattern2(f, p);
921}
922
923/**
924 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
925 *
926 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
927 *
928 * This function uses kzalloc(), so the caller must call kfree()
929 * if this function didn't return NULL.
930 */
931const char *tomoyo_get_exe(void)
932{
d4144ea6
DB
933 struct file *exe_file;
934 const char *cp;
c3ef1500 935 struct mm_struct *mm = current->mm;
c3ef1500
TH
936
937 if (!mm)
938 return NULL;
d4144ea6
DB
939 exe_file = get_mm_exe_file(mm);
940 if (!exe_file)
941 return NULL;
942
943 cp = tomoyo_realpath_from_path(&exe_file->f_path);
944 fput(exe_file);
c3ef1500
TH
945 return cp;
946}
947
57c2590f
TH
948/**
949 * tomoyo_get_mode - Get MAC mode.
950 *
bd03a3e4 951 * @ns: Pointer to "struct tomoyo_policy_namespace".
57c2590f
TH
952 * @profile: Profile number.
953 * @index: Index number of functionality.
954 *
955 * Returns mode.
956 */
bd03a3e4
TH
957int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
958 const u8 index)
57c2590f
TH
959{
960 u8 mode;
d58e0da8
TH
961 struct tomoyo_profile *p;
962
57c2590f
TH
963 if (!tomoyo_policy_loaded)
964 return TOMOYO_CONFIG_DISABLED;
d58e0da8
TH
965 p = tomoyo_profile(ns, profile);
966 mode = p->config[index];
57c2590f 967 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
d58e0da8
TH
968 mode = p->config[tomoyo_index2category[index]
969 + TOMOYO_MAX_MAC_INDEX];
57c2590f 970 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
d58e0da8 971 mode = p->default_config;
57c2590f
TH
972 return mode & 3;
973}
974
c3ef1500
TH
975/**
976 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
977 *
978 * @r: Pointer to "struct tomoyo_request_info" to initialize.
979 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
57c2590f 980 * @index: Index number of functionality.
c3ef1500
TH
981 *
982 * Returns mode.
983 */
984int tomoyo_init_request_info(struct tomoyo_request_info *r,
57c2590f 985 struct tomoyo_domain_info *domain, const u8 index)
c3ef1500 986{
57c2590f 987 u8 profile;
c3ef1500
TH
988 memset(r, 0, sizeof(*r));
989 if (!domain)
990 domain = tomoyo_domain();
991 r->domain = domain;
57c2590f
TH
992 profile = domain->profile;
993 r->profile = profile;
994 r->type = index;
bd03a3e4 995 r->mode = tomoyo_get_mode(domain->ns, profile, index);
c3ef1500
TH
996 return r->mode;
997}
998
c3ef1500
TH
999/**
1000 * tomoyo_domain_quota_is_ok - Check for domain's quota.
1001 *
1002 * @r: Pointer to "struct tomoyo_request_info".
1003 *
1004 * Returns true if the domain is not exceeded quota, false otherwise.
1005 *
1006 * Caller holds tomoyo_read_lock().
1007 */
1008bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1009{
1010 unsigned int count = 0;
1011 struct tomoyo_domain_info *domain = r->domain;
1012 struct tomoyo_acl_info *ptr;
1013
1014 if (r->mode != TOMOYO_CONFIG_LEARNING)
1015 return false;
1016 if (!domain)
1017 return true;
1018 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
7c75964f
TH
1019 u16 perm;
1020 u8 i;
7e3d199a 1021 if (ptr->is_deleted)
237ab459 1022 continue;
c3ef1500 1023 switch (ptr->type) {
c3ef1500
TH
1024 case TOMOYO_TYPE_PATH_ACL:
1025 perm = container_of(ptr, struct tomoyo_path_acl, head)
1026 ->perm;
c3ef1500
TH
1027 break;
1028 case TOMOYO_TYPE_PATH2_ACL:
1029 perm = container_of(ptr, struct tomoyo_path2_acl, head)
1030 ->perm;
c3ef1500
TH
1031 break;
1032 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1033 perm = container_of(ptr, struct tomoyo_path_number_acl,
1034 head)->perm;
c3ef1500 1035 break;
75093152
TH
1036 case TOMOYO_TYPE_MKDEV_ACL:
1037 perm = container_of(ptr, struct tomoyo_mkdev_acl,
c3ef1500 1038 head)->perm;
c3ef1500 1039 break;
545a7260
TH
1040 case TOMOYO_TYPE_INET_ACL:
1041 perm = container_of(ptr, struct tomoyo_inet_acl,
1042 head)->perm;
1043 break;
1044 case TOMOYO_TYPE_UNIX_ACL:
1045 perm = container_of(ptr, struct tomoyo_unix_acl,
1046 head)->perm;
1047 break;
1048 case TOMOYO_TYPE_MANUAL_TASK_ACL:
1049 perm = 0;
1050 break;
237ab459 1051 default:
7c75964f 1052 perm = 1;
c3ef1500 1053 }
7c75964f
TH
1054 for (i = 0; i < 16; i++)
1055 if (perm & (1 << i))
1056 count++;
c3ef1500 1057 }
bd03a3e4 1058 if (count < tomoyo_profile(domain->ns, domain->profile)->
d5ca1725 1059 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
c3ef1500 1060 return true;
2c47ab93
TH
1061 if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) {
1062 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
1063 /* r->granted = false; */
1064 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
1065 printk(KERN_WARNING "WARNING: "
d5ca1725 1066 "Domain '%s' has too many ACLs to hold. "
c3ef1500
TH
1067 "Stopped learning mode.\n", domain->domainname->name);
1068 }
1069 return false;
1070}