blob: a1c3d9ccebfa4572a05f3a1a3952f8d1a8af7113 [file] [log] [blame]
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001/*
2 * security/tomoyo/util.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handac3ef1502010-05-17 10:12:46 +09005 */
6
7#include <linux/slab.h>
8#include "common.h"
9
10/* Lock for protecting policy. */
11DEFINE_MUTEX(tomoyo_policy_lock);
12
13/* Has /sbin/init started? */
14bool tomoyo_policy_loaded;
15
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090016/*
17 * Mapping table from "enum tomoyo_mac_index" to
18 * "enum tomoyo_mac_category_index".
19 */
20const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = {
21 /* CONFIG::file group */
22 [TOMOYO_MAC_FILE_EXECUTE] = TOMOYO_MAC_CATEGORY_FILE,
23 [TOMOYO_MAC_FILE_OPEN] = TOMOYO_MAC_CATEGORY_FILE,
24 [TOMOYO_MAC_FILE_CREATE] = TOMOYO_MAC_CATEGORY_FILE,
25 [TOMOYO_MAC_FILE_UNLINK] = TOMOYO_MAC_CATEGORY_FILE,
26 [TOMOYO_MAC_FILE_GETATTR] = TOMOYO_MAC_CATEGORY_FILE,
27 [TOMOYO_MAC_FILE_MKDIR] = TOMOYO_MAC_CATEGORY_FILE,
28 [TOMOYO_MAC_FILE_RMDIR] = TOMOYO_MAC_CATEGORY_FILE,
29 [TOMOYO_MAC_FILE_MKFIFO] = TOMOYO_MAC_CATEGORY_FILE,
30 [TOMOYO_MAC_FILE_MKSOCK] = TOMOYO_MAC_CATEGORY_FILE,
31 [TOMOYO_MAC_FILE_TRUNCATE] = TOMOYO_MAC_CATEGORY_FILE,
32 [TOMOYO_MAC_FILE_SYMLINK] = TOMOYO_MAC_CATEGORY_FILE,
33 [TOMOYO_MAC_FILE_MKBLOCK] = TOMOYO_MAC_CATEGORY_FILE,
34 [TOMOYO_MAC_FILE_MKCHAR] = TOMOYO_MAC_CATEGORY_FILE,
35 [TOMOYO_MAC_FILE_LINK] = TOMOYO_MAC_CATEGORY_FILE,
36 [TOMOYO_MAC_FILE_RENAME] = TOMOYO_MAC_CATEGORY_FILE,
37 [TOMOYO_MAC_FILE_CHMOD] = TOMOYO_MAC_CATEGORY_FILE,
38 [TOMOYO_MAC_FILE_CHOWN] = TOMOYO_MAC_CATEGORY_FILE,
39 [TOMOYO_MAC_FILE_CHGRP] = TOMOYO_MAC_CATEGORY_FILE,
40 [TOMOYO_MAC_FILE_IOCTL] = TOMOYO_MAC_CATEGORY_FILE,
41 [TOMOYO_MAC_FILE_CHROOT] = TOMOYO_MAC_CATEGORY_FILE,
42 [TOMOYO_MAC_FILE_MOUNT] = TOMOYO_MAC_CATEGORY_FILE,
43 [TOMOYO_MAC_FILE_UMOUNT] = TOMOYO_MAC_CATEGORY_FILE,
44 [TOMOYO_MAC_FILE_PIVOT_ROOT] = TOMOYO_MAC_CATEGORY_FILE,
Tetsuo Handa059d84d2011-09-10 15:23:54 +090045 /* CONFIG::network group */
46 [TOMOYO_MAC_NETWORK_INET_STREAM_BIND] =
47 TOMOYO_MAC_CATEGORY_NETWORK,
48 [TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN] =
49 TOMOYO_MAC_CATEGORY_NETWORK,
50 [TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT] =
51 TOMOYO_MAC_CATEGORY_NETWORK,
52 [TOMOYO_MAC_NETWORK_INET_DGRAM_BIND] =
53 TOMOYO_MAC_CATEGORY_NETWORK,
54 [TOMOYO_MAC_NETWORK_INET_DGRAM_SEND] =
55 TOMOYO_MAC_CATEGORY_NETWORK,
56 [TOMOYO_MAC_NETWORK_INET_RAW_BIND] =
57 TOMOYO_MAC_CATEGORY_NETWORK,
58 [TOMOYO_MAC_NETWORK_INET_RAW_SEND] =
59 TOMOYO_MAC_CATEGORY_NETWORK,
60 [TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND] =
61 TOMOYO_MAC_CATEGORY_NETWORK,
62 [TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN] =
63 TOMOYO_MAC_CATEGORY_NETWORK,
64 [TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT] =
65 TOMOYO_MAC_CATEGORY_NETWORK,
66 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND] =
67 TOMOYO_MAC_CATEGORY_NETWORK,
68 [TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND] =
69 TOMOYO_MAC_CATEGORY_NETWORK,
70 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND] =
71 TOMOYO_MAC_CATEGORY_NETWORK,
72 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] =
73 TOMOYO_MAC_CATEGORY_NETWORK,
74 [TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] =
75 TOMOYO_MAC_CATEGORY_NETWORK,
Tetsuo Handad58e0da2011-09-10 15:22:48 +090076 /* CONFIG::misc group */
77 [TOMOYO_MAC_ENVIRON] = TOMOYO_MAC_CATEGORY_MISC,
Tetsuo Handa2c47ab92011-06-26 23:21:19 +090078};
79
Tetsuo Handac3ef1502010-05-17 10:12:46 +090080/**
Tetsuo Handab22b8b92011-06-26 23:21:50 +090081 * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
82 *
83 * @time: Seconds since 1970/01/01 00:00:00.
84 * @stamp: Pointer to "struct tomoyo_time".
85 *
86 * Returns nothing.
87 *
88 * This function does not handle Y2038 problem.
89 */
90void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp)
91{
92 static const u16 tomoyo_eom[2][12] = {
93 { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
94 { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
95 };
96 u16 y;
97 u8 m;
98 bool r;
99 stamp->sec = time % 60;
100 time /= 60;
101 stamp->min = time % 60;
102 time /= 60;
103 stamp->hour = time % 24;
104 time /= 24;
105 for (y = 1970; ; y++) {
106 const unsigned short days = (y & 3) ? 365 : 366;
107 if (time < days)
108 break;
109 time -= days;
110 }
111 r = (y & 3) == 0;
112 for (m = 0; m < 11 && time >= tomoyo_eom[r][m]; m++)
113 ;
114 if (m)
115 time -= tomoyo_eom[r][m - 1];
116 stamp->year = y;
117 stamp->month = ++m;
118 stamp->day = ++time;
119}
120
121/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900122 * tomoyo_permstr - Find permission keywords.
123 *
124 * @string: String representation for permissions in foo/bar/buz format.
125 * @keyword: Keyword to find from @string/
126 *
127 * Returns ture if @keyword was found in @string, false otherwise.
128 *
129 * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
130 */
131bool tomoyo_permstr(const char *string, const char *keyword)
132{
133 const char *cp = strstr(string, keyword);
134 if (cp)
135 return cp == string || *(cp - 1) == '/';
136 return false;
137}
138
139/**
140 * tomoyo_read_token - Read a word from a line.
141 *
142 * @param: Pointer to "struct tomoyo_acl_param".
143 *
144 * Returns a word on success, "" otherwise.
145 *
146 * To allow the caller to skip NULL check, this function returns "" rather than
147 * NULL if there is no more words to read.
148 */
149char *tomoyo_read_token(struct tomoyo_acl_param *param)
150{
151 char *pos = param->data;
152 char *del = strchr(pos, ' ');
153 if (del)
154 *del++ = '\0';
155 else
156 del = pos + strlen(pos);
157 param->data = del;
158 return pos;
159}
160
161/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900162 * tomoyo_parse_ulong - Parse an "unsigned long" value.
163 *
164 * @result: Pointer to "unsigned long".
165 * @str: Pointer to string to parse.
166 *
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900167 * Returns one of values in "enum tomoyo_value_type".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900168 *
169 * The @src is updated to point the first character after the value
170 * on success.
171 */
Tetsuo Handa2066a362011-07-08 13:21:37 +0900172u8 tomoyo_parse_ulong(unsigned long *result, char **str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900173{
174 const char *cp = *str;
175 char *ep;
176 int base = 10;
177 if (*cp == '0') {
178 char c = *(cp + 1);
179 if (c == 'x' || c == 'X') {
180 base = 16;
181 cp += 2;
182 } else if (c >= '0' && c <= '7') {
183 base = 8;
184 cp++;
185 }
186 }
187 *result = simple_strtoul(cp, &ep, base);
188 if (cp == ep)
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900189 return TOMOYO_VALUE_TYPE_INVALID;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900190 *str = ep;
191 switch (base) {
192 case 16:
193 return TOMOYO_VALUE_TYPE_HEXADECIMAL;
194 case 8:
195 return TOMOYO_VALUE_TYPE_OCTAL;
196 default:
197 return TOMOYO_VALUE_TYPE_DECIMAL;
198 }
199}
200
201/**
202 * tomoyo_print_ulong - Print an "unsigned long" value.
203 *
204 * @buffer: Pointer to buffer.
205 * @buffer_len: Size of @buffer.
206 * @value: An "unsigned long" value.
207 * @type: Type of @value.
208 *
209 * Returns nothing.
210 */
211void tomoyo_print_ulong(char *buffer, const int buffer_len,
212 const unsigned long value, const u8 type)
213{
214 if (type == TOMOYO_VALUE_TYPE_DECIMAL)
215 snprintf(buffer, buffer_len, "%lu", value);
216 else if (type == TOMOYO_VALUE_TYPE_OCTAL)
217 snprintf(buffer, buffer_len, "0%lo", value);
218 else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
219 snprintf(buffer, buffer_len, "0x%lX", value);
220 else
221 snprintf(buffer, buffer_len, "type(%u)", type);
222}
223
224/**
225 * tomoyo_parse_name_union - Parse a tomoyo_name_union.
226 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900227 * @param: Pointer to "struct tomoyo_acl_param".
228 * @ptr: Pointer to "struct tomoyo_name_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900229 *
230 * Returns true on success, false otherwise.
231 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900232bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900233 struct tomoyo_name_union *ptr)
234{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900235 char *filename;
236 if (param->data[0] == '@') {
237 param->data++;
238 ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900239 return ptr->group != NULL;
240 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900241 filename = tomoyo_read_token(param);
242 if (!tomoyo_correct_word(filename))
243 return false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900244 ptr->filename = tomoyo_get_name(filename);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900245 return ptr->filename != NULL;
246}
247
248/**
249 * tomoyo_parse_number_union - Parse a tomoyo_number_union.
250 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900251 * @param: Pointer to "struct tomoyo_acl_param".
252 * @ptr: Pointer to "struct tomoyo_number_union".
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900253 *
254 * Returns true on success, false otherwise.
255 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900256bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
257 struct tomoyo_number_union *ptr)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900258{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900259 char *data;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900260 u8 type;
261 unsigned long v;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900262 memset(ptr, 0, sizeof(*ptr));
263 if (param->data[0] == '@') {
264 param->data++;
265 ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP);
266 return ptr->group != NULL;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900267 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900268 data = tomoyo_read_token(param);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900269 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900270 if (type == TOMOYO_VALUE_TYPE_INVALID)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900271 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900272 ptr->values[0] = v;
273 ptr->value_type[0] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900274 if (!*data) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900275 ptr->values[1] = v;
276 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900277 return true;
278 }
279 if (*data++ != '-')
280 return false;
281 type = tomoyo_parse_ulong(&v, &data);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900282 if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900283 return false;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900284 ptr->values[1] = v;
285 ptr->value_type[1] = type;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900286 return true;
287}
288
289/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900290 * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900291 *
292 * @str: Pointer to the string.
293 *
294 * Returns true if @str is a \ooo style octal value, false otherwise.
295 *
296 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
297 * This function verifies that \ooo is in valid range.
298 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900299static inline bool tomoyo_byte_range(const char *str)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900300{
301 return *str >= '0' && *str++ <= '3' &&
302 *str >= '0' && *str++ <= '7' &&
303 *str >= '0' && *str <= '7';
304}
305
306/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900307 * tomoyo_alphabet_char - Check whether the character is an alphabet.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900308 *
309 * @c: The character to check.
310 *
311 * Returns true if @c is an alphabet character, false otherwise.
312 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900313static inline bool tomoyo_alphabet_char(const char c)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900314{
315 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
316}
317
318/**
319 * tomoyo_make_byte - Make byte value from three octal characters.
320 *
321 * @c1: The first character.
322 * @c2: The second character.
323 * @c3: The third character.
324 *
325 * Returns byte value.
326 */
327static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
328{
329 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
330}
331
332/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900333 * tomoyo_valid - Check whether the character is a valid char.
334 *
335 * @c: The character to check.
336 *
337 * Returns true if @c is a valid character, false otherwise.
338 */
339static inline bool tomoyo_valid(const unsigned char c)
340{
341 return c > ' ' && c < 127;
342}
343
344/**
345 * tomoyo_invalid - Check whether the character is an invalid char.
346 *
347 * @c: The character to check.
348 *
349 * Returns true if @c is an invalid character, false otherwise.
350 */
351static inline bool tomoyo_invalid(const unsigned char c)
352{
353 return c && (c <= ' ' || c >= 127);
354}
355
356/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900357 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
358 *
359 * @src: Pointer to pointer to the string.
360 * @find: Pointer to the keyword.
361 *
362 * Returns true if @src starts with @find, false otherwise.
363 *
364 * The @src is updated to point the first character after the @find
365 * if @src starts with @find.
366 */
367bool tomoyo_str_starts(char **src, const char *find)
368{
369 const int len = strlen(find);
370 char *tmp = *src;
371
372 if (strncmp(tmp, find, len))
373 return false;
374 tmp += len;
375 *src = tmp;
376 return true;
377}
378
379/**
380 * tomoyo_normalize_line - Format string.
381 *
382 * @buffer: The line to normalize.
383 *
384 * Leading and trailing whitespaces are removed.
385 * Multiple whitespaces are packed into single space.
386 *
387 * Returns nothing.
388 */
389void tomoyo_normalize_line(unsigned char *buffer)
390{
391 unsigned char *sp = buffer;
392 unsigned char *dp = buffer;
393 bool first = true;
394
Tetsuo Handa75093152010-06-16 16:23:55 +0900395 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900396 sp++;
397 while (*sp) {
398 if (!first)
399 *dp++ = ' ';
400 first = false;
Tetsuo Handa75093152010-06-16 16:23:55 +0900401 while (tomoyo_valid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900402 *dp++ = *sp++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900403 while (tomoyo_invalid(*sp))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900404 sp++;
405 }
406 *dp = '\0';
407}
408
409/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900410 * tomoyo_correct_word2 - Validate a string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900411 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900412 * @string: The string to check. Maybe non-'\0'-terminated.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900413 * @len: Length of @string.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900414 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900415 * Check whether the given string follows the naming rules.
416 * Returns true if @string follows the naming rules, false otherwise.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900417 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900418static bool tomoyo_correct_word2(const char *string, size_t len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900419{
Tetsuo Handa3f629632010-06-03 20:37:26 +0900420 const char *const start = string;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900421 bool in_repetition = false;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900422 unsigned char c;
423 unsigned char d;
424 unsigned char e;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900425 if (!len)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900426 goto out;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900427 while (len--) {
428 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900429 if (c == '\\') {
Tetsuo Handa3f629632010-06-03 20:37:26 +0900430 if (!len--)
431 goto out;
432 c = *string++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900433 switch (c) {
434 case '\\': /* "\\" */
435 continue;
436 case '$': /* "\$" */
437 case '+': /* "\+" */
438 case '?': /* "\?" */
439 case '*': /* "\*" */
440 case '@': /* "\@" */
441 case 'x': /* "\x" */
442 case 'X': /* "\X" */
443 case 'a': /* "\a" */
444 case 'A': /* "\A" */
445 case '-': /* "\-" */
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900446 continue;
447 case '{': /* "/\{" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900448 if (string - 3 < start || *(string - 3) != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900449 break;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900450 in_repetition = true;
451 continue;
452 case '}': /* "\}/" */
Tetsuo Handa3f629632010-06-03 20:37:26 +0900453 if (*string != '/')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900454 break;
455 if (!in_repetition)
456 break;
457 in_repetition = false;
458 continue;
459 case '0': /* "\ooo" */
460 case '1':
461 case '2':
462 case '3':
Tetsuo Handa3f629632010-06-03 20:37:26 +0900463 if (!len-- || !len--)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900464 break;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900465 d = *string++;
466 e = *string++;
467 if (d < '0' || d > '7' || e < '0' || e > '7')
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900468 break;
469 c = tomoyo_make_byte(c, d, e);
Tetsuo Handa75093152010-06-16 16:23:55 +0900470 if (tomoyo_invalid(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900471 continue; /* pattern is not \000 */
472 }
473 goto out;
474 } else if (in_repetition && c == '/') {
475 goto out;
Tetsuo Handa75093152010-06-16 16:23:55 +0900476 } else if (tomoyo_invalid(c)) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900477 goto out;
478 }
479 }
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900480 if (in_repetition)
481 goto out;
482 return true;
483 out:
484 return false;
485}
486
487/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900488 * tomoyo_correct_word - Validate a string.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900489 *
490 * @string: The string to check.
491 *
492 * Check whether the given string follows the naming rules.
493 * Returns true if @string follows the naming rules, false otherwise.
494 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900495bool tomoyo_correct_word(const char *string)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900496{
Tetsuo Handa75093152010-06-16 16:23:55 +0900497 return tomoyo_correct_word2(string, strlen(string));
Tetsuo Handa3f629632010-06-03 20:37:26 +0900498}
499
500/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900501 * tomoyo_correct_path - Validate a pathname.
Tetsuo Handa3f629632010-06-03 20:37:26 +0900502 *
503 * @filename: The pathname to check.
504 *
505 * Check whether the given pathname follows the naming rules.
506 * Returns true if @filename follows the naming rules, false otherwise.
507 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900508bool tomoyo_correct_path(const char *filename)
Tetsuo Handa3f629632010-06-03 20:37:26 +0900509{
Tetsuo Handa75093152010-06-16 16:23:55 +0900510 return *filename == '/' && tomoyo_correct_word(filename);
Tetsuo Handa3f629632010-06-03 20:37:26 +0900511}
512
513/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900514 * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900515 *
Tetsuo Handa3f629632010-06-03 20:37:26 +0900516 * @domainname: The domainname to check.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900517 *
518 * Returns true if @domainname follows the naming rules, false otherwise.
519 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900520bool tomoyo_correct_domain(const unsigned char *domainname)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900521{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900522 if (!domainname || !tomoyo_domain_def(domainname))
523 return false;
524 domainname = strchr(domainname, ' ');
525 if (!domainname++)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900526 return true;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900527 while (1) {
528 const unsigned char *cp = strchr(domainname, ' ');
529 if (!cp)
530 break;
531 if (*domainname != '/' ||
Tetsuo Handae77dc342011-05-12 06:40:51 +0900532 !tomoyo_correct_word2(domainname, cp - domainname))
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900533 return false;
Tetsuo Handa3f629632010-06-03 20:37:26 +0900534 domainname = cp + 1;
535 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900536 return tomoyo_correct_path(domainname);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900537}
538
539/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900540 * tomoyo_domain_def - Check whether the given token can be a domainname.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900541 *
542 * @buffer: The token to check.
543 *
544 * Returns true if @buffer possibly be a domainname, false otherwise.
545 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900546bool tomoyo_domain_def(const unsigned char *buffer)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900547{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900548 const unsigned char *cp;
549 int len;
550 if (*buffer != '<')
551 return false;
552 cp = strchr(buffer, ' ');
553 if (!cp)
554 len = strlen(buffer);
555 else
556 len = cp - buffer;
557 if (buffer[len - 1] != '>' ||
558 !tomoyo_correct_word2(buffer + 1, len - 2))
559 return false;
560 return true;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900561}
562
563/**
564 * tomoyo_find_domain - Find a domain by the given name.
565 *
566 * @domainname: The domainname to find.
567 *
568 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
569 *
570 * Caller holds tomoyo_read_lock().
571 */
572struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
573{
574 struct tomoyo_domain_info *domain;
575 struct tomoyo_path_info name;
576
577 name.name = domainname;
578 tomoyo_fill_path_info(&name);
579 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
580 if (!domain->is_deleted &&
581 !tomoyo_pathcmp(&name, domain->domainname))
582 return domain;
583 }
584 return NULL;
585}
586
587/**
588 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
589 *
590 * @filename: The string to evaluate.
591 *
592 * Returns the initial length without a pattern in @filename.
593 */
594static int tomoyo_const_part_length(const char *filename)
595{
596 char c;
597 int len = 0;
598
599 if (!filename)
600 return 0;
601 while ((c = *filename++) != '\0') {
602 if (c != '\\') {
603 len++;
604 continue;
605 }
606 c = *filename++;
607 switch (c) {
608 case '\\': /* "\\" */
609 len += 2;
610 continue;
611 case '0': /* "\ooo" */
612 case '1':
613 case '2':
614 case '3':
615 c = *filename++;
616 if (c < '0' || c > '7')
617 break;
618 c = *filename++;
619 if (c < '0' || c > '7')
620 break;
621 len += 4;
622 continue;
623 }
624 break;
625 }
626 return len;
627}
628
629/**
630 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
631 *
632 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
633 *
634 * The caller sets "struct tomoyo_path_info"->name.
635 */
636void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
637{
638 const char *name = ptr->name;
639 const int len = strlen(name);
640
641 ptr->const_len = tomoyo_const_part_length(name);
642 ptr->is_dir = len && (name[len - 1] == '/');
643 ptr->is_patterned = (ptr->const_len < len);
644 ptr->hash = full_name_hash(name, len);
645}
646
647/**
648 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
649 *
650 * @filename: The start of string to check.
651 * @filename_end: The end of string to check.
652 * @pattern: The start of pattern to compare.
653 * @pattern_end: The end of pattern to compare.
654 *
655 * Returns true if @filename matches @pattern, false otherwise.
656 */
657static bool tomoyo_file_matches_pattern2(const char *filename,
658 const char *filename_end,
659 const char *pattern,
660 const char *pattern_end)
661{
662 while (filename < filename_end && pattern < pattern_end) {
663 char c;
664 if (*pattern != '\\') {
665 if (*filename++ != *pattern++)
666 return false;
667 continue;
668 }
669 c = *filename;
670 pattern++;
671 switch (*pattern) {
672 int i;
673 int j;
674 case '?':
675 if (c == '/') {
676 return false;
677 } else if (c == '\\') {
678 if (filename[1] == '\\')
679 filename++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900680 else if (tomoyo_byte_range(filename + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900681 filename += 3;
682 else
683 return false;
684 }
685 break;
686 case '\\':
687 if (c != '\\')
688 return false;
689 if (*++filename != '\\')
690 return false;
691 break;
692 case '+':
693 if (!isdigit(c))
694 return false;
695 break;
696 case 'x':
697 if (!isxdigit(c))
698 return false;
699 break;
700 case 'a':
Tetsuo Handa75093152010-06-16 16:23:55 +0900701 if (!tomoyo_alphabet_char(c))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900702 return false;
703 break;
704 case '0':
705 case '1':
706 case '2':
707 case '3':
Tetsuo Handa75093152010-06-16 16:23:55 +0900708 if (c == '\\' && tomoyo_byte_range(filename + 1)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900709 && strncmp(filename + 1, pattern, 3) == 0) {
710 filename += 3;
711 pattern += 2;
712 break;
713 }
714 return false; /* Not matched. */
715 case '*':
716 case '@':
717 for (i = 0; i <= filename_end - filename; i++) {
718 if (tomoyo_file_matches_pattern2(
719 filename + i, filename_end,
720 pattern + 1, pattern_end))
721 return true;
722 c = filename[i];
723 if (c == '.' && *pattern == '@')
724 break;
725 if (c != '\\')
726 continue;
727 if (filename[i + 1] == '\\')
728 i++;
Tetsuo Handa75093152010-06-16 16:23:55 +0900729 else if (tomoyo_byte_range(filename + i + 1))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900730 i += 3;
731 else
732 break; /* Bad pattern. */
733 }
734 return false; /* Not matched. */
735 default:
736 j = 0;
737 c = *pattern;
738 if (c == '$') {
739 while (isdigit(filename[j]))
740 j++;
741 } else if (c == 'X') {
742 while (isxdigit(filename[j]))
743 j++;
744 } else if (c == 'A') {
Tetsuo Handa75093152010-06-16 16:23:55 +0900745 while (tomoyo_alphabet_char(filename[j]))
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900746 j++;
747 }
748 for (i = 1; i <= j; i++) {
749 if (tomoyo_file_matches_pattern2(
750 filename + i, filename_end,
751 pattern + 1, pattern_end))
752 return true;
753 }
754 return false; /* Not matched or bad pattern. */
755 }
756 filename++;
757 pattern++;
758 }
759 while (*pattern == '\\' &&
760 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
761 pattern += 2;
762 return filename == filename_end && pattern == pattern_end;
763}
764
765/**
766 * tomoyo_file_matches_pattern - Pattern matching without '/' character.
767 *
768 * @filename: The start of string to check.
769 * @filename_end: The end of string to check.
770 * @pattern: The start of pattern to compare.
771 * @pattern_end: The end of pattern to compare.
772 *
773 * Returns true if @filename matches @pattern, false otherwise.
774 */
775static bool tomoyo_file_matches_pattern(const char *filename,
776 const char *filename_end,
777 const char *pattern,
778 const char *pattern_end)
779{
780 const char *pattern_start = pattern;
781 bool first = true;
782 bool result;
783
784 while (pattern < pattern_end - 1) {
785 /* Split at "\-" pattern. */
786 if (*pattern++ != '\\' || *pattern++ != '-')
787 continue;
788 result = tomoyo_file_matches_pattern2(filename,
789 filename_end,
790 pattern_start,
791 pattern - 2);
792 if (first)
793 result = !result;
794 if (result)
795 return false;
796 first = false;
797 pattern_start = pattern;
798 }
799 result = tomoyo_file_matches_pattern2(filename, filename_end,
800 pattern_start, pattern_end);
801 return first ? result : !result;
802}
803
804/**
805 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
806 *
807 * @f: The start of string to check.
808 * @p: The start of pattern to compare.
809 *
810 * Returns true if @f matches @p, false otherwise.
811 */
812static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
813{
814 const char *f_delimiter;
815 const char *p_delimiter;
816
817 while (*f && *p) {
818 f_delimiter = strchr(f, '/');
819 if (!f_delimiter)
820 f_delimiter = f + strlen(f);
821 p_delimiter = strchr(p, '/');
822 if (!p_delimiter)
823 p_delimiter = p + strlen(p);
824 if (*p == '\\' && *(p + 1) == '{')
825 goto recursive;
826 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
827 p_delimiter))
828 return false;
829 f = f_delimiter;
830 if (*f)
831 f++;
832 p = p_delimiter;
833 if (*p)
834 p++;
835 }
836 /* Ignore trailing "\*" and "\@" in @pattern. */
837 while (*p == '\\' &&
838 (*(p + 1) == '*' || *(p + 1) == '@'))
839 p += 2;
840 return !*f && !*p;
841 recursive:
842 /*
843 * The "\{" pattern is permitted only after '/' character.
844 * This guarantees that below "*(p - 1)" is safe.
845 * Also, the "\}" pattern is permitted only before '/' character
846 * so that "\{" + "\}" pair will not break the "\-" operator.
847 */
848 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
849 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
850 return false; /* Bad pattern. */
851 do {
852 /* Compare current component with pattern. */
853 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
854 p_delimiter - 2))
855 break;
856 /* Proceed to next component. */
857 f = f_delimiter;
858 if (!*f)
859 break;
860 f++;
861 /* Continue comparison. */
862 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
863 return true;
864 f_delimiter = strchr(f, '/');
865 } while (f_delimiter);
866 return false; /* Not matched. */
867}
868
869/**
870 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
871 *
872 * @filename: The filename to check.
873 * @pattern: The pattern to compare.
874 *
875 * Returns true if matches, false otherwise.
876 *
877 * The following patterns are available.
878 * \\ \ itself.
879 * \ooo Octal representation of a byte.
880 * \* Zero or more repetitions of characters other than '/'.
881 * \@ Zero or more repetitions of characters other than '/' or '.'.
882 * \? 1 byte character other than '/'.
883 * \$ One or more repetitions of decimal digits.
884 * \+ 1 decimal digit.
885 * \X One or more repetitions of hexadecimal digits.
886 * \x 1 hexadecimal digit.
887 * \A One or more repetitions of alphabet characters.
888 * \a 1 alphabet character.
889 *
890 * \- Subtraction operator.
891 *
892 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
893 * /dir/dir/dir/ ).
894 */
895bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
896 const struct tomoyo_path_info *pattern)
897{
898 const char *f = filename->name;
899 const char *p = pattern->name;
900 const int len = pattern->const_len;
901
902 /* If @pattern doesn't contain pattern, I can use strcmp(). */
903 if (!pattern->is_patterned)
904 return !tomoyo_pathcmp(filename, pattern);
905 /* Don't compare directory and non-directory. */
906 if (filename->is_dir != pattern->is_dir)
907 return false;
908 /* Compare the initial length without patterns. */
909 if (strncmp(f, p, len))
910 return false;
911 f += len;
912 p += len;
913 return tomoyo_path_matches_pattern2(f, p);
914}
915
916/**
917 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
918 *
919 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
920 *
921 * This function uses kzalloc(), so the caller must call kfree()
922 * if this function didn't return NULL.
923 */
924const char *tomoyo_get_exe(void)
925{
926 struct mm_struct *mm = current->mm;
927 struct vm_area_struct *vma;
928 const char *cp = NULL;
929
930 if (!mm)
931 return NULL;
932 down_read(&mm->mmap_sem);
933 for (vma = mm->mmap; vma; vma = vma->vm_next) {
934 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
935 cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
936 break;
937 }
938 }
939 up_read(&mm->mmap_sem);
940 return cp;
941}
942
943/**
Tetsuo Handa57c25902010-06-03 20:38:44 +0900944 * tomoyo_get_mode - Get MAC mode.
945 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900946 * @ns: Pointer to "struct tomoyo_policy_namespace".
Tetsuo Handa57c25902010-06-03 20:38:44 +0900947 * @profile: Profile number.
948 * @index: Index number of functionality.
949 *
950 * Returns mode.
951 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900952int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
953 const u8 index)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900954{
955 u8 mode;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900956 struct tomoyo_profile *p;
957
Tetsuo Handa57c25902010-06-03 20:38:44 +0900958 if (!tomoyo_policy_loaded)
959 return TOMOYO_CONFIG_DISABLED;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900960 p = tomoyo_profile(ns, profile);
961 mode = p->config[index];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900962 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900963 mode = p->config[tomoyo_index2category[index]
964 + TOMOYO_MAX_MAC_INDEX];
Tetsuo Handa57c25902010-06-03 20:38:44 +0900965 if (mode == TOMOYO_CONFIG_USE_DEFAULT)
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900966 mode = p->default_config;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900967 return mode & 3;
968}
969
970/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900971 * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
972 *
973 * @r: Pointer to "struct tomoyo_request_info" to initialize.
974 * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
Tetsuo Handa57c25902010-06-03 20:38:44 +0900975 * @index: Index number of functionality.
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900976 *
977 * Returns mode.
978 */
979int tomoyo_init_request_info(struct tomoyo_request_info *r,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900980 struct tomoyo_domain_info *domain, const u8 index)
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900981{
Tetsuo Handa57c25902010-06-03 20:38:44 +0900982 u8 profile;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900983 memset(r, 0, sizeof(*r));
984 if (!domain)
985 domain = tomoyo_domain();
986 r->domain = domain;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900987 profile = domain->profile;
988 r->profile = profile;
989 r->type = index;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900990 r->mode = tomoyo_get_mode(domain->ns, profile, index);
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900991 return r->mode;
992}
993
994/**
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900995 * tomoyo_domain_quota_is_ok - Check for domain's quota.
996 *
997 * @r: Pointer to "struct tomoyo_request_info".
998 *
999 * Returns true if the domain is not exceeded quota, false otherwise.
1000 *
1001 * Caller holds tomoyo_read_lock().
1002 */
1003bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
1004{
1005 unsigned int count = 0;
1006 struct tomoyo_domain_info *domain = r->domain;
1007 struct tomoyo_acl_info *ptr;
1008
1009 if (r->mode != TOMOYO_CONFIG_LEARNING)
1010 return false;
1011 if (!domain)
1012 return true;
1013 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Tetsuo Handa7c759642011-06-26 23:15:31 +09001014 u16 perm;
1015 u8 i;
Tetsuo Handa7e3d1992010-07-27 10:08:29 +09001016 if (ptr->is_deleted)
Tetsuo Handa237ab452010-06-12 20:46:22 +09001017 continue;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001018 switch (ptr->type) {
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001019 case TOMOYO_TYPE_PATH_ACL:
1020 perm = container_of(ptr, struct tomoyo_path_acl, head)
1021 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001022 break;
1023 case TOMOYO_TYPE_PATH2_ACL:
1024 perm = container_of(ptr, struct tomoyo_path2_acl, head)
1025 ->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001026 break;
1027 case TOMOYO_TYPE_PATH_NUMBER_ACL:
1028 perm = container_of(ptr, struct tomoyo_path_number_acl,
1029 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001030 break;
Tetsuo Handa75093152010-06-16 16:23:55 +09001031 case TOMOYO_TYPE_MKDEV_ACL:
1032 perm = container_of(ptr, struct tomoyo_mkdev_acl,
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001033 head)->perm;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001034 break;
Tetsuo Handa237ab452010-06-12 20:46:22 +09001035 default:
Tetsuo Handa7c759642011-06-26 23:15:31 +09001036 perm = 1;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001037 }
Tetsuo Handa7c759642011-06-26 23:15:31 +09001038 for (i = 0; i < 16; i++)
1039 if (perm & (1 << i))
1040 count++;
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001041 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +09001042 if (count < tomoyo_profile(domain->ns, domain->profile)->
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001043 pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001044 return true;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +09001045 if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) {
1046 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
1047 /* r->granted = false; */
1048 tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
1049 printk(KERN_WARNING "WARNING: "
Tetsuo Handad5ca1722011-06-26 23:18:21 +09001050 "Domain '%s' has too many ACLs to hold. "
Tetsuo Handac3ef1502010-05-17 10:12:46 +09001051 "Stopped learning mode.\n", domain->domainname->name);
1052 }
1053 return false;
1054}