blob: f01b9364db2dd14914eb61bc568c36f054de1f48 [file] [log] [blame]
Kentaro Takeda95908372009-02-05 17:18:13 +09001/*
2 * security/tomoyo/common.c
3 *
4 * Common functions for TOMOYO.
5 *
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 *
Tetsuo Handa39826a12009-04-08 22:31:28 +09008 * Version: 2.2.0 2009/04/01
Kentaro Takeda95908372009-02-05 17:18:13 +09009 *
10 */
11
12#include <linux/uaccess.h>
13#include <linux/security.h>
14#include <linux/hardirq.h>
15#include "realpath.h"
16#include "common.h"
17#include "tomoyo.h"
18
19/* Has loading policy done? */
20bool tomoyo_policy_loaded;
21
22/* String table for functionality that takes 4 modes. */
23static const char *tomoyo_mode_4[4] = {
24 "disabled", "learning", "permissive", "enforcing"
25};
26/* String table for functionality that takes 2 modes. */
27static const char *tomoyo_mode_2[4] = {
28 "disabled", "enabled", "enabled", "enabled"
29};
30
Tetsuo Handac3fa1092009-06-08 12:37:39 +090031/*
32 * tomoyo_control_array is a static data which contains
33 *
34 * (1) functionality name used by /sys/kernel/security/tomoyo/profile .
35 * (2) initial values for "struct tomoyo_profile".
36 * (3) max values for "struct tomoyo_profile".
37 */
Kentaro Takeda95908372009-02-05 17:18:13 +090038static struct {
39 const char *keyword;
40 unsigned int current_value;
41 const unsigned int max_value;
42} tomoyo_control_array[TOMOYO_MAX_CONTROL_INDEX] = {
43 [TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
44 [TOMOYO_MAX_ACCEPT_ENTRY] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX },
45 [TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
46};
47
Tetsuo Handac3fa1092009-06-08 12:37:39 +090048/*
49 * tomoyo_profile is a structure which is used for holding the mode of access
50 * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
51 * An administrator can define up to 256 profiles.
52 * The ->profile of "struct tomoyo_domain_info" is used for remembering
53 * the profile's number (0 - 255) assigned to that domain.
54 */
Kentaro Takeda95908372009-02-05 17:18:13 +090055static struct tomoyo_profile {
56 unsigned int value[TOMOYO_MAX_CONTROL_INDEX];
57 const struct tomoyo_path_info *comment;
58} *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
59
60/* Permit policy management by non-root user? */
61static bool tomoyo_manage_by_non_root;
62
63/* Utility functions. */
64
65/* Open operation for /sys/kernel/security/tomoyo/ interface. */
66static int tomoyo_open_control(const u8 type, struct file *file);
67/* Close /sys/kernel/security/tomoyo/ interface. */
68static int tomoyo_close_control(struct file *file);
69/* Read operation for /sys/kernel/security/tomoyo/ interface. */
70static int tomoyo_read_control(struct file *file, char __user *buffer,
71 const int buffer_len);
72/* Write operation for /sys/kernel/security/tomoyo/ interface. */
73static int tomoyo_write_control(struct file *file, const char __user *buffer,
74 const int buffer_len);
75
76/**
77 * tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
78 *
79 * @str: Pointer to the string.
80 *
81 * Returns true if @str is a \ooo style octal value, false otherwise.
82 *
83 * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
84 * This function verifies that \ooo is in valid range.
85 */
86static inline bool tomoyo_is_byte_range(const char *str)
87{
88 return *str >= '0' && *str++ <= '3' &&
89 *str >= '0' && *str++ <= '7' &&
90 *str >= '0' && *str <= '7';
91}
92
93/**
94 * tomoyo_is_alphabet_char - Check whether the character is an alphabet.
95 *
96 * @c: The character to check.
97 *
98 * Returns true if @c is an alphabet character, false otherwise.
99 */
100static inline bool tomoyo_is_alphabet_char(const char c)
101{
102 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
103}
104
105/**
106 * tomoyo_make_byte - Make byte value from three octal characters.
107 *
108 * @c1: The first character.
109 * @c2: The second character.
110 * @c3: The third character.
111 *
112 * Returns byte value.
113 */
114static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
115{
116 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
117}
118
119/**
120 * tomoyo_str_starts - Check whether the given string starts with the given keyword.
121 *
122 * @src: Pointer to pointer to the string.
123 * @find: Pointer to the keyword.
124 *
125 * Returns true if @src starts with @find, false otherwise.
126 *
127 * The @src is updated to point the first character after the @find
128 * if @src starts with @find.
129 */
130static bool tomoyo_str_starts(char **src, const char *find)
131{
132 const int len = strlen(find);
133 char *tmp = *src;
134
135 if (strncmp(tmp, find, len))
136 return false;
137 tmp += len;
138 *src = tmp;
139 return true;
140}
141
142/**
143 * tomoyo_normalize_line - Format string.
144 *
145 * @buffer: The line to normalize.
146 *
147 * Leading and trailing whitespaces are removed.
148 * Multiple whitespaces are packed into single space.
149 *
150 * Returns nothing.
151 */
152static void tomoyo_normalize_line(unsigned char *buffer)
153{
154 unsigned char *sp = buffer;
155 unsigned char *dp = buffer;
156 bool first = true;
157
158 while (tomoyo_is_invalid(*sp))
159 sp++;
160 while (*sp) {
161 if (!first)
162 *dp++ = ' ';
163 first = false;
164 while (tomoyo_is_valid(*sp))
165 *dp++ = *sp++;
166 while (tomoyo_is_invalid(*sp))
167 sp++;
168 }
169 *dp = '\0';
170}
171
172/**
173 * tomoyo_is_correct_path - Validate a pathname.
174 * @filename: The pathname to check.
175 * @start_type: Should the pathname start with '/'?
176 * 1 = must / -1 = must not / 0 = don't care
177 * @pattern_type: Can the pathname contain a wildcard?
178 * 1 = must / -1 = must not / 0 = don't care
179 * @end_type: Should the pathname end with '/'?
180 * 1 = must / -1 = must not / 0 = don't care
181 * @function: The name of function calling me.
182 *
183 * Check whether the given filename follows the naming rules.
184 * Returns true if @filename follows the naming rules, false otherwise.
185 */
186bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
187 const s8 pattern_type, const s8 end_type,
188 const char *function)
189{
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900190 const char *const start = filename;
191 bool in_repetition = false;
Kentaro Takeda95908372009-02-05 17:18:13 +0900192 bool contains_pattern = false;
193 unsigned char c;
194 unsigned char d;
195 unsigned char e;
196 const char *original_filename = filename;
197
198 if (!filename)
199 goto out;
200 c = *filename;
201 if (start_type == 1) { /* Must start with '/' */
202 if (c != '/')
203 goto out;
204 } else if (start_type == -1) { /* Must not start with '/' */
205 if (c == '/')
206 goto out;
207 }
208 if (c)
209 c = *(filename + strlen(filename) - 1);
210 if (end_type == 1) { /* Must end with '/' */
211 if (c != '/')
212 goto out;
213 } else if (end_type == -1) { /* Must not end with '/' */
214 if (c == '/')
215 goto out;
216 }
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900217 while (1) {
218 c = *filename++;
219 if (!c)
220 break;
Kentaro Takeda95908372009-02-05 17:18:13 +0900221 if (c == '\\') {
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900222 c = *filename++;
223 switch (c) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900224 case '\\': /* "\\" */
225 continue;
226 case '$': /* "\$" */
227 case '+': /* "\+" */
228 case '?': /* "\?" */
229 case '*': /* "\*" */
230 case '@': /* "\@" */
231 case 'x': /* "\x" */
232 case 'X': /* "\X" */
233 case 'a': /* "\a" */
234 case 'A': /* "\A" */
235 case '-': /* "\-" */
236 if (pattern_type == -1)
237 break; /* Must not contain pattern */
238 contains_pattern = true;
239 continue;
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900240 case '{': /* "/\{" */
241 if (filename - 3 < start ||
242 *(filename - 3) != '/')
243 break;
244 if (pattern_type == -1)
245 break; /* Must not contain pattern */
246 contains_pattern = true;
247 in_repetition = true;
248 continue;
249 case '}': /* "\}/" */
250 if (*filename != '/')
251 break;
252 if (!in_repetition)
253 break;
254 in_repetition = false;
255 continue;
Kentaro Takeda95908372009-02-05 17:18:13 +0900256 case '0': /* "\ooo" */
257 case '1':
258 case '2':
259 case '3':
260 d = *filename++;
261 if (d < '0' || d > '7')
262 break;
263 e = *filename++;
264 if (e < '0' || e > '7')
265 break;
266 c = tomoyo_make_byte(c, d, e);
267 if (tomoyo_is_invalid(c))
268 continue; /* pattern is not \000 */
269 }
270 goto out;
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900271 } else if (in_repetition && c == '/') {
272 goto out;
Kentaro Takeda95908372009-02-05 17:18:13 +0900273 } else if (tomoyo_is_invalid(c)) {
274 goto out;
275 }
276 }
277 if (pattern_type == 1) { /* Must contain pattern */
278 if (!contains_pattern)
279 goto out;
280 }
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900281 if (in_repetition)
282 goto out;
Kentaro Takeda95908372009-02-05 17:18:13 +0900283 return true;
284 out:
285 printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function,
286 original_filename);
287 return false;
288}
289
290/**
291 * tomoyo_is_correct_domain - Check whether the given domainname follows the naming rules.
292 * @domainname: The domainname to check.
293 * @function: The name of function calling me.
294 *
295 * Returns true if @domainname follows the naming rules, false otherwise.
296 */
297bool tomoyo_is_correct_domain(const unsigned char *domainname,
298 const char *function)
299{
300 unsigned char c;
301 unsigned char d;
302 unsigned char e;
303 const char *org_domainname = domainname;
304
305 if (!domainname || strncmp(domainname, TOMOYO_ROOT_NAME,
306 TOMOYO_ROOT_NAME_LEN))
307 goto out;
308 domainname += TOMOYO_ROOT_NAME_LEN;
309 if (!*domainname)
310 return true;
311 do {
312 if (*domainname++ != ' ')
313 goto out;
314 if (*domainname++ != '/')
315 goto out;
316 while ((c = *domainname) != '\0' && c != ' ') {
317 domainname++;
318 if (c == '\\') {
319 c = *domainname++;
320 switch ((c)) {
321 case '\\': /* "\\" */
322 continue;
323 case '0': /* "\ooo" */
324 case '1':
325 case '2':
326 case '3':
327 d = *domainname++;
328 if (d < '0' || d > '7')
329 break;
330 e = *domainname++;
331 if (e < '0' || e > '7')
332 break;
333 c = tomoyo_make_byte(c, d, e);
334 if (tomoyo_is_invalid(c))
335 /* pattern is not \000 */
336 continue;
337 }
338 goto out;
339 } else if (tomoyo_is_invalid(c)) {
340 goto out;
341 }
342 }
343 } while (*domainname);
344 return true;
345 out:
346 printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function,
347 org_domainname);
348 return false;
349}
350
351/**
352 * tomoyo_is_domain_def - Check whether the given token can be a domainname.
353 *
354 * @buffer: The token to check.
355 *
356 * Returns true if @buffer possibly be a domainname, false otherwise.
357 */
358bool tomoyo_is_domain_def(const unsigned char *buffer)
359{
360 return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
361}
362
363/**
364 * tomoyo_find_domain - Find a domain by the given name.
365 *
366 * @domainname: The domainname to find.
367 *
Kentaro Takeda95908372009-02-05 17:18:13 +0900368 * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900369 *
370 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900371 */
372struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
373{
374 struct tomoyo_domain_info *domain;
375 struct tomoyo_path_info name;
376
377 name.name = domainname;
378 tomoyo_fill_path_info(&name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900379 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900380 if (!domain->is_deleted &&
381 !tomoyo_pathcmp(&name, domain->domainname))
382 return domain;
383 }
384 return NULL;
385}
386
387/**
Kentaro Takeda95908372009-02-05 17:18:13 +0900388 * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
389 *
390 * @filename: The string to evaluate.
391 *
392 * Returns the initial length without a pattern in @filename.
393 */
394static int tomoyo_const_part_length(const char *filename)
395{
396 char c;
397 int len = 0;
398
399 if (!filename)
400 return 0;
401 while ((c = *filename++) != '\0') {
402 if (c != '\\') {
403 len++;
404 continue;
405 }
406 c = *filename++;
407 switch (c) {
408 case '\\': /* "\\" */
409 len += 2;
410 continue;
411 case '0': /* "\ooo" */
412 case '1':
413 case '2':
414 case '3':
415 c = *filename++;
416 if (c < '0' || c > '7')
417 break;
418 c = *filename++;
419 if (c < '0' || c > '7')
420 break;
421 len += 4;
422 continue;
423 }
424 break;
425 }
426 return len;
427}
428
429/**
430 * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
431 *
432 * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
433 *
434 * The caller sets "struct tomoyo_path_info"->name.
435 */
436void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
437{
438 const char *name = ptr->name;
439 const int len = strlen(name);
440
Kentaro Takeda95908372009-02-05 17:18:13 +0900441 ptr->const_len = tomoyo_const_part_length(name);
442 ptr->is_dir = len && (name[len - 1] == '/');
443 ptr->is_patterned = (ptr->const_len < len);
444 ptr->hash = full_name_hash(name, len);
Kentaro Takeda95908372009-02-05 17:18:13 +0900445}
446
447/**
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900448 * tomoyo_file_matches_pattern2 - Pattern matching without '/' character
Kentaro Takeda95908372009-02-05 17:18:13 +0900449 * and "\-" pattern.
450 *
451 * @filename: The start of string to check.
452 * @filename_end: The end of string to check.
453 * @pattern: The start of pattern to compare.
454 * @pattern_end: The end of pattern to compare.
455 *
456 * Returns true if @filename matches @pattern, false otherwise.
457 */
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900458static bool tomoyo_file_matches_pattern2(const char *filename,
459 const char *filename_end,
460 const char *pattern,
461 const char *pattern_end)
Kentaro Takeda95908372009-02-05 17:18:13 +0900462{
463 while (filename < filename_end && pattern < pattern_end) {
464 char c;
465 if (*pattern != '\\') {
466 if (*filename++ != *pattern++)
467 return false;
468 continue;
469 }
470 c = *filename;
471 pattern++;
472 switch (*pattern) {
473 int i;
474 int j;
475 case '?':
476 if (c == '/') {
477 return false;
478 } else if (c == '\\') {
479 if (filename[1] == '\\')
480 filename++;
481 else if (tomoyo_is_byte_range(filename + 1))
482 filename += 3;
483 else
484 return false;
485 }
486 break;
487 case '\\':
488 if (c != '\\')
489 return false;
490 if (*++filename != '\\')
491 return false;
492 break;
493 case '+':
494 if (!isdigit(c))
495 return false;
496 break;
497 case 'x':
498 if (!isxdigit(c))
499 return false;
500 break;
501 case 'a':
502 if (!tomoyo_is_alphabet_char(c))
503 return false;
504 break;
505 case '0':
506 case '1':
507 case '2':
508 case '3':
509 if (c == '\\' && tomoyo_is_byte_range(filename + 1)
510 && strncmp(filename + 1, pattern, 3) == 0) {
511 filename += 3;
512 pattern += 2;
513 break;
514 }
515 return false; /* Not matched. */
516 case '*':
517 case '@':
518 for (i = 0; i <= filename_end - filename; i++) {
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900519 if (tomoyo_file_matches_pattern2(
Kentaro Takeda95908372009-02-05 17:18:13 +0900520 filename + i, filename_end,
521 pattern + 1, pattern_end))
522 return true;
523 c = filename[i];
524 if (c == '.' && *pattern == '@')
525 break;
526 if (c != '\\')
527 continue;
528 if (filename[i + 1] == '\\')
529 i++;
530 else if (tomoyo_is_byte_range(filename + i + 1))
531 i += 3;
532 else
533 break; /* Bad pattern. */
534 }
535 return false; /* Not matched. */
536 default:
537 j = 0;
538 c = *pattern;
539 if (c == '$') {
540 while (isdigit(filename[j]))
541 j++;
542 } else if (c == 'X') {
543 while (isxdigit(filename[j]))
544 j++;
545 } else if (c == 'A') {
546 while (tomoyo_is_alphabet_char(filename[j]))
547 j++;
548 }
549 for (i = 1; i <= j; i++) {
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900550 if (tomoyo_file_matches_pattern2(
Kentaro Takeda95908372009-02-05 17:18:13 +0900551 filename + i, filename_end,
552 pattern + 1, pattern_end))
553 return true;
554 }
555 return false; /* Not matched or bad pattern. */
556 }
557 filename++;
558 pattern++;
559 }
560 while (*pattern == '\\' &&
561 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
562 pattern += 2;
563 return filename == filename_end && pattern == pattern_end;
564}
565
566/**
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900567 * tomoyo_file_matches_pattern - Pattern matching without without '/' character.
Kentaro Takeda95908372009-02-05 17:18:13 +0900568 *
569 * @filename: The start of string to check.
570 * @filename_end: The end of string to check.
571 * @pattern: The start of pattern to compare.
572 * @pattern_end: The end of pattern to compare.
573 *
574 * Returns true if @filename matches @pattern, false otherwise.
575 */
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900576static bool tomoyo_file_matches_pattern(const char *filename,
Kentaro Takeda95908372009-02-05 17:18:13 +0900577 const char *filename_end,
578 const char *pattern,
579 const char *pattern_end)
580{
581 const char *pattern_start = pattern;
582 bool first = true;
583 bool result;
584
585 while (pattern < pattern_end - 1) {
586 /* Split at "\-" pattern. */
587 if (*pattern++ != '\\' || *pattern++ != '-')
588 continue;
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900589 result = tomoyo_file_matches_pattern2(filename,
590 filename_end,
591 pattern_start,
592 pattern - 2);
Kentaro Takeda95908372009-02-05 17:18:13 +0900593 if (first)
594 result = !result;
595 if (result)
596 return false;
597 first = false;
598 pattern_start = pattern;
599 }
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900600 result = tomoyo_file_matches_pattern2(filename, filename_end,
601 pattern_start, pattern_end);
Kentaro Takeda95908372009-02-05 17:18:13 +0900602 return first ? result : !result;
603}
604
605/**
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900606 * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
Kentaro Takeda95908372009-02-05 17:18:13 +0900607 *
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900608 * @f: The start of string to check.
609 * @p: The start of pattern to compare.
Kentaro Takeda95908372009-02-05 17:18:13 +0900610 *
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900611 * Returns true if @f matches @p, false otherwise.
Kentaro Takeda95908372009-02-05 17:18:13 +0900612 */
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900613static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
Kentaro Takeda95908372009-02-05 17:18:13 +0900614{
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900615 const char *f_delimiter;
616 const char *p_delimiter;
Kentaro Takeda95908372009-02-05 17:18:13 +0900617
Kentaro Takeda95908372009-02-05 17:18:13 +0900618 while (*f && *p) {
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900619 f_delimiter = strchr(f, '/');
Kentaro Takeda95908372009-02-05 17:18:13 +0900620 if (!f_delimiter)
621 f_delimiter = f + strlen(f);
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900622 p_delimiter = strchr(p, '/');
Kentaro Takeda95908372009-02-05 17:18:13 +0900623 if (!p_delimiter)
624 p_delimiter = p + strlen(p);
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900625 if (*p == '\\' && *(p + 1) == '{')
626 goto recursive;
627 if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
628 p_delimiter))
Kentaro Takeda95908372009-02-05 17:18:13 +0900629 return false;
630 f = f_delimiter;
631 if (*f)
632 f++;
633 p = p_delimiter;
634 if (*p)
635 p++;
636 }
637 /* Ignore trailing "\*" and "\@" in @pattern. */
638 while (*p == '\\' &&
639 (*(p + 1) == '*' || *(p + 1) == '@'))
640 p += 2;
641 return !*f && !*p;
Tetsuo Handa7539cf42009-11-24 22:00:05 +0900642 recursive:
643 /*
644 * The "\{" pattern is permitted only after '/' character.
645 * This guarantees that below "*(p - 1)" is safe.
646 * Also, the "\}" pattern is permitted only before '/' character
647 * so that "\{" + "\}" pair will not break the "\-" operator.
648 */
649 if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
650 *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
651 return false; /* Bad pattern. */
652 do {
653 /* Compare current component with pattern. */
654 if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
655 p_delimiter - 2))
656 break;
657 /* Proceed to next component. */
658 f = f_delimiter;
659 if (!*f)
660 break;
661 f++;
662 /* Continue comparison. */
663 if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
664 return true;
665 f_delimiter = strchr(f, '/');
666 } while (f_delimiter);
667 return false; /* Not matched. */
668}
669
670/**
671 * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
672 *
673 * @filename: The filename to check.
674 * @pattern: The pattern to compare.
675 *
676 * Returns true if matches, false otherwise.
677 *
678 * The following patterns are available.
679 * \\ \ itself.
680 * \ooo Octal representation of a byte.
681 * \* Zero or more repetitions of characters other than '/'.
682 * \@ Zero or more repetitions of characters other than '/' or '.'.
683 * \? 1 byte character other than '/'.
684 * \$ One or more repetitions of decimal digits.
685 * \+ 1 decimal digit.
686 * \X One or more repetitions of hexadecimal digits.
687 * \x 1 hexadecimal digit.
688 * \A One or more repetitions of alphabet characters.
689 * \a 1 alphabet character.
690 *
691 * \- Subtraction operator.
692 *
693 * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
694 * /dir/dir/dir/ ).
695 */
696bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
697 const struct tomoyo_path_info *pattern)
698{
699 const char *f = filename->name;
700 const char *p = pattern->name;
701 const int len = pattern->const_len;
702
703 /* If @pattern doesn't contain pattern, I can use strcmp(). */
704 if (!pattern->is_patterned)
705 return !tomoyo_pathcmp(filename, pattern);
706 /* Don't compare directory and non-directory. */
707 if (filename->is_dir != pattern->is_dir)
708 return false;
709 /* Compare the initial length without patterns. */
710 if (strncmp(f, p, len))
711 return false;
712 f += len;
713 p += len;
714 return tomoyo_path_matches_pattern2(f, p);
Kentaro Takeda95908372009-02-05 17:18:13 +0900715}
716
717/**
718 * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
719 *
720 * @head: Pointer to "struct tomoyo_io_buffer".
721 * @fmt: The printf()'s format string, followed by parameters.
722 *
723 * Returns true if output was written, false otherwise.
724 *
725 * The snprintf() will truncate, but tomoyo_io_printf() won't.
726 */
727bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
728{
729 va_list args;
730 int len;
731 int pos = head->read_avail;
732 int size = head->readbuf_size - pos;
733
734 if (size <= 0)
735 return false;
736 va_start(args, fmt);
737 len = vsnprintf(head->read_buf + pos, size, fmt, args);
738 va_end(args);
739 if (pos + len >= head->readbuf_size)
740 return false;
741 head->read_avail += len;
742 return true;
743}
744
745/**
746 * tomoyo_get_exe - Get tomoyo_realpath() of current process.
747 *
748 * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
749 *
750 * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
751 * if this function didn't return NULL.
752 */
753static const char *tomoyo_get_exe(void)
754{
755 struct mm_struct *mm = current->mm;
756 struct vm_area_struct *vma;
757 const char *cp = NULL;
758
759 if (!mm)
760 return NULL;
761 down_read(&mm->mmap_sem);
762 for (vma = mm->mmap; vma; vma = vma->vm_next) {
763 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
764 cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
765 break;
766 }
767 }
768 up_read(&mm->mmap_sem);
769 return cp;
770}
771
772/**
773 * tomoyo_get_msg - Get warning message.
774 *
775 * @is_enforce: Is it enforcing mode?
776 *
777 * Returns "ERROR" or "WARNING".
778 */
779const char *tomoyo_get_msg(const bool is_enforce)
780{
781 if (is_enforce)
782 return "ERROR";
783 else
784 return "WARNING";
785}
786
787/**
788 * tomoyo_check_flags - Check mode for specified functionality.
789 *
790 * @domain: Pointer to "struct tomoyo_domain_info".
791 * @index: The functionality to check mode.
792 *
793 * TOMOYO checks only process context.
794 * This code disables TOMOYO's enforcement in case the function is called from
795 * interrupt context.
796 */
797unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
798 const u8 index)
799{
800 const u8 profile = domain->profile;
801
802 if (WARN_ON(in_interrupt()))
803 return 0;
804 return tomoyo_policy_loaded && index < TOMOYO_MAX_CONTROL_INDEX
805#if TOMOYO_MAX_PROFILES != 256
806 && profile < TOMOYO_MAX_PROFILES
807#endif
808 && tomoyo_profile_ptr[profile] ?
809 tomoyo_profile_ptr[profile]->value[index] : 0;
810}
811
812/**
813 * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
814 *
815 * @domain: Pointer to "struct tomoyo_domain_info".
816 *
817 * Returns true if domain policy violation warning should be printed to
818 * console.
819 */
820bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain)
821{
822 return tomoyo_check_flags(domain, TOMOYO_VERBOSE) != 0;
823}
824
825/**
826 * tomoyo_domain_quota_is_ok - Check for domain's quota.
827 *
828 * @domain: Pointer to "struct tomoyo_domain_info".
829 *
830 * Returns true if the domain is not exceeded quota, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900831 *
832 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +0900833 */
834bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain)
835{
836 unsigned int count = 0;
837 struct tomoyo_acl_info *ptr;
838
839 if (!domain)
840 return true;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900841 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +0900842 if (ptr->type & TOMOYO_ACL_DELETED)
843 continue;
844 switch (tomoyo_acl_type2(ptr)) {
Tetsuo Handa937bf612009-12-02 21:09:48 +0900845 struct tomoyo_single_path_acl_record *acl;
846 u32 perm;
847 u8 i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900848 case TOMOYO_TYPE_SINGLE_PATH_ACL:
Tetsuo Handa937bf612009-12-02 21:09:48 +0900849 acl = container_of(ptr,
850 struct tomoyo_single_path_acl_record,
851 head);
852 perm = acl->perm | (((u32) acl->perm_high) << 16);
853 for (i = 0; i < TOMOYO_MAX_SINGLE_PATH_OPERATION; i++)
854 if (perm & (1 << i))
855 count++;
856 if (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL))
857 count -= 2;
Kentaro Takeda95908372009-02-05 17:18:13 +0900858 break;
859 case TOMOYO_TYPE_DOUBLE_PATH_ACL:
Tetsuo Handa937bf612009-12-02 21:09:48 +0900860 perm = container_of(ptr,
Kentaro Takeda95908372009-02-05 17:18:13 +0900861 struct tomoyo_double_path_acl_record,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900862 head)->perm;
863 for (i = 0; i < TOMOYO_MAX_DOUBLE_PATH_OPERATION; i++)
864 if (perm & (1 << i))
865 count++;
Kentaro Takeda95908372009-02-05 17:18:13 +0900866 break;
867 }
868 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900869 if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
870 return true;
871 if (!domain->quota_warned) {
872 domain->quota_warned = true;
873 printk(KERN_WARNING "TOMOYO-WARNING: "
874 "Domain '%s' has so many ACLs to hold. "
875 "Stopped learning mode.\n", domain->domainname->name);
876 }
877 return false;
878}
879
880/**
881 * tomoyo_find_or_assign_new_profile - Create a new profile.
882 *
883 * @profile: Profile number to create.
884 *
885 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
886 */
887static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
888 int profile)
889{
890 static DEFINE_MUTEX(lock);
891 struct tomoyo_profile *ptr = NULL;
892 int i;
893
894 if (profile >= TOMOYO_MAX_PROFILES)
895 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900896 mutex_lock(&lock);
897 ptr = tomoyo_profile_ptr[profile];
898 if (ptr)
899 goto ok;
900 ptr = tomoyo_alloc_element(sizeof(*ptr));
901 if (!ptr)
902 goto ok;
903 for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
904 ptr->value[i] = tomoyo_control_array[i].current_value;
905 mb(); /* Avoid out-of-order execution. */
906 tomoyo_profile_ptr[profile] = ptr;
907 ok:
908 mutex_unlock(&lock);
Kentaro Takeda95908372009-02-05 17:18:13 +0900909 return ptr;
910}
911
912/**
913 * tomoyo_write_profile - Write to profile table.
914 *
915 * @head: Pointer to "struct tomoyo_io_buffer".
916 *
917 * Returns 0 on success, negative value otherwise.
918 */
919static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
920{
921 char *data = head->write_buf;
922 unsigned int i;
923 unsigned int value;
924 char *cp;
925 struct tomoyo_profile *profile;
926 unsigned long num;
927
928 cp = strchr(data, '-');
929 if (cp)
930 *cp = '\0';
931 if (strict_strtoul(data, 10, &num))
932 return -EINVAL;
933 if (cp)
934 data = cp + 1;
935 profile = tomoyo_find_or_assign_new_profile(num);
936 if (!profile)
937 return -EINVAL;
938 cp = strchr(data, '=');
939 if (!cp)
940 return -EINVAL;
941 *cp = '\0';
942 if (!strcmp(data, "COMMENT")) {
943 profile->comment = tomoyo_save_name(cp + 1);
944 return 0;
945 }
946 for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
947 if (strcmp(data, tomoyo_control_array[i].keyword))
948 continue;
949 if (sscanf(cp + 1, "%u", &value) != 1) {
950 int j;
951 const char **modes;
952 switch (i) {
953 case TOMOYO_VERBOSE:
954 modes = tomoyo_mode_2;
955 break;
956 default:
957 modes = tomoyo_mode_4;
958 break;
959 }
960 for (j = 0; j < 4; j++) {
961 if (strcmp(cp + 1, modes[j]))
962 continue;
963 value = j;
964 break;
965 }
966 if (j == 4)
967 return -EINVAL;
968 } else if (value > tomoyo_control_array[i].max_value) {
969 value = tomoyo_control_array[i].max_value;
970 }
971 profile->value[i] = value;
972 return 0;
973 }
974 return -EINVAL;
975}
976
977/**
978 * tomoyo_read_profile - Read from profile table.
979 *
980 * @head: Pointer to "struct tomoyo_io_buffer".
981 *
982 * Returns 0.
983 */
984static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
985{
986 static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
987 int step;
988
989 if (head->read_eof)
990 return 0;
991 for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
992 step++) {
993 const u8 index = step / total;
994 u8 type = step % total;
995 const struct tomoyo_profile *profile
996 = tomoyo_profile_ptr[index];
997 head->read_step = step;
998 if (!profile)
999 continue;
1000 if (!type) { /* Print profile' comment tag. */
1001 if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
1002 index, profile->comment ?
1003 profile->comment->name : ""))
1004 break;
1005 continue;
1006 }
1007 type--;
1008 if (type < TOMOYO_MAX_CONTROL_INDEX) {
1009 const unsigned int value = profile->value[type];
1010 const char **modes = NULL;
1011 const char *keyword
1012 = tomoyo_control_array[type].keyword;
1013 switch (tomoyo_control_array[type].max_value) {
1014 case 3:
1015 modes = tomoyo_mode_4;
1016 break;
1017 case 1:
1018 modes = tomoyo_mode_2;
1019 break;
1020 }
1021 if (modes) {
1022 if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
1023 keyword, modes[value]))
1024 break;
1025 } else {
1026 if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
1027 keyword, value))
1028 break;
1029 }
1030 }
1031 }
1032 if (step == TOMOYO_MAX_PROFILES * total)
1033 head->read_eof = true;
1034 return 0;
1035}
1036
Tetsuo Handac3fa1092009-06-08 12:37:39 +09001037/*
1038 * tomoyo_policy_manager_entry is a structure which is used for holding list of
1039 * domainnames or programs which are permitted to modify configuration via
1040 * /sys/kernel/security/tomoyo/ interface.
1041 * It has following fields.
1042 *
1043 * (1) "list" which is linked to tomoyo_policy_manager_list .
1044 * (2) "manager" is a domainname or a program's pathname.
1045 * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
1046 * otherwise.
1047 * (4) "is_deleted" is a bool which is true if marked as deleted, false
1048 * otherwise.
1049 */
Kentaro Takeda95908372009-02-05 17:18:13 +09001050struct tomoyo_policy_manager_entry {
1051 struct list_head list;
1052 /* A path to program or a domainname. */
1053 const struct tomoyo_path_info *manager;
1054 bool is_domain; /* True if manager is a domainname. */
1055 bool is_deleted; /* True if this entry is deleted. */
1056};
1057
Tetsuo Handac3fa1092009-06-08 12:37:39 +09001058/*
1059 * tomoyo_policy_manager_list is used for holding list of domainnames or
1060 * programs which are permitted to modify configuration via
1061 * /sys/kernel/security/tomoyo/ interface.
1062 *
1063 * An entry is added by
1064 *
1065 * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
1066 * /sys/kernel/security/tomoyo/manager
1067 * (if you want to specify by a domainname)
1068 *
1069 * or
1070 *
1071 * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
1072 * (if you want to specify by a program's location)
1073 *
1074 * and is deleted by
1075 *
1076 * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
1077 * /sys/kernel/security/tomoyo/manager
1078 *
1079 * or
1080 *
1081 * # echo 'delete /usr/lib/ccs/editpolicy' > \
1082 * /sys/kernel/security/tomoyo/manager
1083 *
1084 * and all entries are retrieved by
1085 *
1086 * # cat /sys/kernel/security/tomoyo/manager
1087 */
Kentaro Takeda95908372009-02-05 17:18:13 +09001088static LIST_HEAD(tomoyo_policy_manager_list);
1089static DECLARE_RWSEM(tomoyo_policy_manager_list_lock);
1090
1091/**
1092 * tomoyo_update_manager_entry - Add a manager entry.
1093 *
1094 * @manager: The path to manager or the domainnamme.
1095 * @is_delete: True if it is a delete request.
1096 *
1097 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001098 *
1099 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001100 */
1101static int tomoyo_update_manager_entry(const char *manager,
1102 const bool is_delete)
1103{
1104 struct tomoyo_policy_manager_entry *new_entry;
1105 struct tomoyo_policy_manager_entry *ptr;
1106 const struct tomoyo_path_info *saved_manager;
1107 int error = -ENOMEM;
1108 bool is_domain = false;
1109
1110 if (tomoyo_is_domain_def(manager)) {
1111 if (!tomoyo_is_correct_domain(manager, __func__))
1112 return -EINVAL;
1113 is_domain = true;
1114 } else {
1115 if (!tomoyo_is_correct_path(manager, 1, -1, -1, __func__))
1116 return -EINVAL;
1117 }
1118 saved_manager = tomoyo_save_name(manager);
1119 if (!saved_manager)
1120 return -ENOMEM;
Kentaro Takeda95908372009-02-05 17:18:13 +09001121 down_write(&tomoyo_policy_manager_list_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001122 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001123 if (ptr->manager != saved_manager)
1124 continue;
1125 ptr->is_deleted = is_delete;
1126 error = 0;
1127 goto out;
1128 }
1129 if (is_delete) {
1130 error = -ENOENT;
1131 goto out;
1132 }
1133 new_entry = tomoyo_alloc_element(sizeof(*new_entry));
1134 if (!new_entry)
1135 goto out;
1136 new_entry->manager = saved_manager;
1137 new_entry->is_domain = is_domain;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001138 list_add_tail_rcu(&new_entry->list, &tomoyo_policy_manager_list);
Kentaro Takeda95908372009-02-05 17:18:13 +09001139 error = 0;
1140 out:
1141 up_write(&tomoyo_policy_manager_list_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001142 return error;
1143}
1144
1145/**
1146 * tomoyo_write_manager_policy - Write manager policy.
1147 *
1148 * @head: Pointer to "struct tomoyo_io_buffer".
1149 *
1150 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001151 *
1152 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001153 */
1154static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
1155{
1156 char *data = head->write_buf;
1157 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1158
1159 if (!strcmp(data, "manage_by_non_root")) {
1160 tomoyo_manage_by_non_root = !is_delete;
1161 return 0;
1162 }
1163 return tomoyo_update_manager_entry(data, is_delete);
1164}
1165
1166/**
1167 * tomoyo_read_manager_policy - Read manager policy.
1168 *
1169 * @head: Pointer to "struct tomoyo_io_buffer".
1170 *
1171 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001172 *
1173 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001174 */
1175static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
1176{
1177 struct list_head *pos;
1178 bool done = true;
1179
1180 if (head->read_eof)
1181 return 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09001182 list_for_each_cookie(pos, head->read_var2,
1183 &tomoyo_policy_manager_list) {
1184 struct tomoyo_policy_manager_entry *ptr;
1185 ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
1186 list);
1187 if (ptr->is_deleted)
1188 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001189 done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
1190 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001191 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001192 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001193 head->read_eof = done;
1194 return 0;
1195}
1196
1197/**
1198 * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
1199 *
1200 * Returns true if the current process is permitted to modify policy
1201 * via /sys/kernel/security/tomoyo/ interface.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001202 *
1203 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001204 */
1205static bool tomoyo_is_policy_manager(void)
1206{
1207 struct tomoyo_policy_manager_entry *ptr;
1208 const char *exe;
1209 const struct task_struct *task = current;
1210 const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
1211 bool found = false;
1212
1213 if (!tomoyo_policy_loaded)
1214 return true;
1215 if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
1216 return false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001217 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001218 if (!ptr->is_deleted && ptr->is_domain
1219 && !tomoyo_pathcmp(domainname, ptr->manager)) {
1220 found = true;
1221 break;
1222 }
1223 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001224 if (found)
1225 return true;
1226 exe = tomoyo_get_exe();
1227 if (!exe)
1228 return false;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001229 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001230 if (!ptr->is_deleted && !ptr->is_domain
1231 && !strcmp(exe, ptr->manager->name)) {
1232 found = true;
1233 break;
1234 }
1235 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001236 if (!found) { /* Reduce error messages. */
1237 static pid_t last_pid;
1238 const pid_t pid = current->pid;
1239 if (last_pid != pid) {
1240 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1241 "update policies.\n", domainname->name, exe);
1242 last_pid = pid;
1243 }
1244 }
1245 tomoyo_free(exe);
1246 return found;
1247}
1248
1249/**
1250 * tomoyo_is_select_one - Parse select command.
1251 *
1252 * @head: Pointer to "struct tomoyo_io_buffer".
1253 * @data: String to parse.
1254 *
1255 * Returns true on success, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001256 *
1257 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001258 */
1259static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
1260 const char *data)
1261{
1262 unsigned int pid;
1263 struct tomoyo_domain_info *domain = NULL;
1264
1265 if (sscanf(data, "pid=%u", &pid) == 1) {
1266 struct task_struct *p;
Kentaro Takeda95908372009-02-05 17:18:13 +09001267 read_lock(&tasklist_lock);
1268 p = find_task_by_vpid(pid);
1269 if (p)
1270 domain = tomoyo_real_domain(p);
1271 read_unlock(&tasklist_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001272 } else if (!strncmp(data, "domain=", 7)) {
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001273 if (tomoyo_is_domain_def(data + 7))
Kentaro Takeda95908372009-02-05 17:18:13 +09001274 domain = tomoyo_find_domain(data + 7);
Kentaro Takeda95908372009-02-05 17:18:13 +09001275 } else
1276 return false;
1277 head->write_var1 = domain;
1278 /* Accessing read_buf is safe because head->io_sem is held. */
1279 if (!head->read_buf)
1280 return true; /* Do nothing if open(O_WRONLY). */
1281 head->read_avail = 0;
1282 tomoyo_io_printf(head, "# select %s\n", data);
1283 head->read_single_domain = true;
1284 head->read_eof = !domain;
1285 if (domain) {
1286 struct tomoyo_domain_info *d;
1287 head->read_var1 = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001288 list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001289 if (d == domain)
1290 break;
1291 head->read_var1 = &d->list;
1292 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001293 head->read_var2 = NULL;
1294 head->read_bit = 0;
1295 head->read_step = 0;
1296 if (domain->is_deleted)
1297 tomoyo_io_printf(head, "# This is a deleted domain.\n");
1298 }
1299 return true;
1300}
1301
1302/**
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001303 * tomoyo_delete_domain - Delete a domain.
1304 *
1305 * @domainname: The name of domain.
1306 *
1307 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001308 *
1309 * Caller holds tomoyo_read_lock().
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001310 */
1311static int tomoyo_delete_domain(char *domainname)
1312{
1313 struct tomoyo_domain_info *domain;
1314 struct tomoyo_path_info name;
1315
1316 name.name = domainname;
1317 tomoyo_fill_path_info(&name);
1318 down_write(&tomoyo_domain_list_lock);
1319 /* Is there an active domain? */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001320 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001321 /* Never delete tomoyo_kernel_domain */
1322 if (domain == &tomoyo_kernel_domain)
1323 continue;
1324 if (domain->is_deleted ||
1325 tomoyo_pathcmp(domain->domainname, &name))
1326 continue;
1327 domain->is_deleted = true;
1328 break;
1329 }
1330 up_write(&tomoyo_domain_list_lock);
1331 return 0;
1332}
1333
1334/**
Kentaro Takeda95908372009-02-05 17:18:13 +09001335 * tomoyo_write_domain_policy - Write domain policy.
1336 *
1337 * @head: Pointer to "struct tomoyo_io_buffer".
1338 *
1339 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001340 *
1341 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001342 */
1343static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
1344{
1345 char *data = head->write_buf;
1346 struct tomoyo_domain_info *domain = head->write_var1;
1347 bool is_delete = false;
1348 bool is_select = false;
Kentaro Takeda95908372009-02-05 17:18:13 +09001349 unsigned int profile;
1350
1351 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
1352 is_delete = true;
1353 else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
1354 is_select = true;
Kentaro Takeda95908372009-02-05 17:18:13 +09001355 if (is_select && tomoyo_is_select_one(head, data))
1356 return 0;
1357 /* Don't allow updating policies by non manager programs. */
1358 if (!tomoyo_is_policy_manager())
1359 return -EPERM;
1360 if (tomoyo_is_domain_def(data)) {
1361 domain = NULL;
1362 if (is_delete)
1363 tomoyo_delete_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001364 else if (is_select)
Kentaro Takeda95908372009-02-05 17:18:13 +09001365 domain = tomoyo_find_domain(data);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001366 else
Kentaro Takeda95908372009-02-05 17:18:13 +09001367 domain = tomoyo_find_or_assign_new_domain(data, 0);
1368 head->write_var1 = domain;
1369 return 0;
1370 }
1371 if (!domain)
1372 return -EINVAL;
1373
1374 if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
1375 && profile < TOMOYO_MAX_PROFILES) {
1376 if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
1377 domain->profile = (u8) profile;
1378 return 0;
1379 }
1380 if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1381 tomoyo_set_domain_flag(domain, is_delete,
1382 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1383 return 0;
1384 }
1385 return tomoyo_write_file_policy(data, domain, is_delete);
1386}
1387
1388/**
1389 * tomoyo_print_single_path_acl - Print a single path ACL entry.
1390 *
1391 * @head: Pointer to "struct tomoyo_io_buffer".
1392 * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
1393 *
1394 * Returns true on success, false otherwise.
1395 */
1396static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer *head,
1397 struct tomoyo_single_path_acl_record *
1398 ptr)
1399{
1400 int pos;
1401 u8 bit;
1402 const char *atmark = "";
1403 const char *filename;
Tetsuo Handa937bf612009-12-02 21:09:48 +09001404 const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
Kentaro Takeda95908372009-02-05 17:18:13 +09001405
1406 filename = ptr->filename->name;
1407 for (bit = head->read_bit; bit < TOMOYO_MAX_SINGLE_PATH_OPERATION;
1408 bit++) {
1409 const char *msg;
1410 if (!(perm & (1 << bit)))
1411 continue;
1412 /* Print "read/write" instead of "read" and "write". */
1413 if ((bit == TOMOYO_TYPE_READ_ACL ||
1414 bit == TOMOYO_TYPE_WRITE_ACL)
1415 && (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
1416 continue;
1417 msg = tomoyo_sp2keyword(bit);
1418 pos = head->read_avail;
1419 if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
1420 atmark, filename))
1421 goto out;
1422 }
1423 head->read_bit = 0;
1424 return true;
1425 out:
1426 head->read_bit = bit;
1427 head->read_avail = pos;
1428 return false;
1429}
1430
1431/**
1432 * tomoyo_print_double_path_acl - Print a double path ACL entry.
1433 *
1434 * @head: Pointer to "struct tomoyo_io_buffer".
1435 * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
1436 *
1437 * Returns true on success, false otherwise.
1438 */
1439static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer *head,
1440 struct tomoyo_double_path_acl_record *
1441 ptr)
1442{
1443 int pos;
1444 const char *atmark1 = "";
1445 const char *atmark2 = "";
1446 const char *filename1;
1447 const char *filename2;
1448 const u8 perm = ptr->perm;
1449 u8 bit;
1450
1451 filename1 = ptr->filename1->name;
1452 filename2 = ptr->filename2->name;
1453 for (bit = head->read_bit; bit < TOMOYO_MAX_DOUBLE_PATH_OPERATION;
1454 bit++) {
1455 const char *msg;
1456 if (!(perm & (1 << bit)))
1457 continue;
1458 msg = tomoyo_dp2keyword(bit);
1459 pos = head->read_avail;
1460 if (!tomoyo_io_printf(head, "allow_%s %s%s %s%s\n", msg,
1461 atmark1, filename1, atmark2, filename2))
1462 goto out;
1463 }
1464 head->read_bit = 0;
1465 return true;
1466 out:
1467 head->read_bit = bit;
1468 head->read_avail = pos;
1469 return false;
1470}
1471
1472/**
1473 * tomoyo_print_entry - Print an ACL entry.
1474 *
1475 * @head: Pointer to "struct tomoyo_io_buffer".
1476 * @ptr: Pointer to an ACL entry.
1477 *
1478 * Returns true on success, false otherwise.
1479 */
1480static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
1481 struct tomoyo_acl_info *ptr)
1482{
1483 const u8 acl_type = tomoyo_acl_type2(ptr);
1484
1485 if (acl_type & TOMOYO_ACL_DELETED)
1486 return true;
1487 if (acl_type == TOMOYO_TYPE_SINGLE_PATH_ACL) {
1488 struct tomoyo_single_path_acl_record *acl
1489 = container_of(ptr,
1490 struct tomoyo_single_path_acl_record,
1491 head);
1492 return tomoyo_print_single_path_acl(head, acl);
1493 }
1494 if (acl_type == TOMOYO_TYPE_DOUBLE_PATH_ACL) {
1495 struct tomoyo_double_path_acl_record *acl
1496 = container_of(ptr,
1497 struct tomoyo_double_path_acl_record,
1498 head);
1499 return tomoyo_print_double_path_acl(head, acl);
1500 }
1501 BUG(); /* This must not happen. */
1502 return false;
1503}
1504
1505/**
1506 * tomoyo_read_domain_policy - Read domain policy.
1507 *
1508 * @head: Pointer to "struct tomoyo_io_buffer".
1509 *
1510 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001511 *
1512 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001513 */
1514static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
1515{
1516 struct list_head *dpos;
1517 struct list_head *apos;
1518 bool done = true;
1519
1520 if (head->read_eof)
1521 return 0;
1522 if (head->read_step == 0)
1523 head->read_step = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +09001524 list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
1525 struct tomoyo_domain_info *domain;
1526 const char *quota_exceeded = "";
1527 const char *transition_failed = "";
1528 const char *ignore_global_allow_read = "";
1529 domain = list_entry(dpos, struct tomoyo_domain_info, list);
1530 if (head->read_step != 1)
1531 goto acl_loop;
1532 if (domain->is_deleted && !head->read_single_domain)
1533 continue;
1534 /* Print domainname and flags. */
1535 if (domain->quota_warned)
1536 quota_exceeded = "quota_exceeded\n";
1537 if (domain->flags & TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED)
1538 transition_failed = "transition_failed\n";
1539 if (domain->flags &
1540 TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
1541 ignore_global_allow_read
1542 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001543 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1544 "%u\n%s%s%s\n",
1545 domain->domainname->name,
1546 domain->profile, quota_exceeded,
1547 transition_failed,
1548 ignore_global_allow_read);
1549 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001550 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001551 head->read_step = 2;
1552acl_loop:
1553 if (head->read_step == 3)
1554 goto tail_mark;
1555 /* Print ACL entries in the domain. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001556 list_for_each_cookie(apos, head->read_var2,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001557 &domain->acl_info_list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001558 struct tomoyo_acl_info *ptr
1559 = list_entry(apos, struct tomoyo_acl_info,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001560 list);
1561 done = tomoyo_print_entry(head, ptr);
1562 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001563 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001564 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001565 if (!done)
1566 break;
1567 head->read_step = 3;
1568tail_mark:
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001569 done = tomoyo_io_printf(head, "\n");
1570 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001571 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001572 head->read_step = 1;
1573 if (head->read_single_domain)
1574 break;
1575 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001576 head->read_eof = done;
1577 return 0;
1578}
1579
1580/**
1581 * tomoyo_write_domain_profile - Assign profile for specified domain.
1582 *
1583 * @head: Pointer to "struct tomoyo_io_buffer".
1584 *
1585 * Returns 0 on success, -EINVAL otherwise.
1586 *
1587 * This is equivalent to doing
1588 *
1589 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1590 * /usr/lib/ccs/loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001591 *
1592 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001593 */
1594static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1595{
1596 char *data = head->write_buf;
1597 char *cp = strchr(data, ' ');
1598 struct tomoyo_domain_info *domain;
1599 unsigned long profile;
1600
1601 if (!cp)
1602 return -EINVAL;
1603 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001604 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001605 if (strict_strtoul(data, 10, &profile))
1606 return -EINVAL;
1607 if (domain && profile < TOMOYO_MAX_PROFILES
1608 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1609 domain->profile = (u8) profile;
1610 return 0;
1611}
1612
1613/**
1614 * tomoyo_read_domain_profile - Read only domainname and profile.
1615 *
1616 * @head: Pointer to "struct tomoyo_io_buffer".
1617 *
1618 * Returns list of profile number and domainname pairs.
1619 *
1620 * This is equivalent to doing
1621 *
1622 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1623 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1624 * domainname = $0; } else if ( $1 == "use_profile" ) {
1625 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001626 *
1627 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001628 */
1629static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1630{
1631 struct list_head *pos;
1632 bool done = true;
1633
1634 if (head->read_eof)
1635 return 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09001636 list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
1637 struct tomoyo_domain_info *domain;
1638 domain = list_entry(pos, struct tomoyo_domain_info, list);
1639 if (domain->is_deleted)
1640 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001641 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1642 domain->domainname->name);
1643 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001644 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001645 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001646 head->read_eof = done;
1647 return 0;
1648}
1649
1650/**
1651 * tomoyo_write_pid: Specify PID to obtain domainname.
1652 *
1653 * @head: Pointer to "struct tomoyo_io_buffer".
1654 *
1655 * Returns 0.
1656 */
1657static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1658{
1659 unsigned long pid;
1660 /* No error check. */
1661 strict_strtoul(head->write_buf, 10, &pid);
1662 head->read_step = (int) pid;
1663 head->read_eof = false;
1664 return 0;
1665}
1666
1667/**
1668 * tomoyo_read_pid - Get domainname of the specified PID.
1669 *
1670 * @head: Pointer to "struct tomoyo_io_buffer".
1671 *
1672 * Returns the domainname which the specified PID is in on success,
1673 * empty string otherwise.
1674 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1675 * using read()/write() interface rather than sysctl() interface.
1676 */
1677static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
1678{
1679 if (head->read_avail == 0 && !head->read_eof) {
1680 const int pid = head->read_step;
1681 struct task_struct *p;
1682 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +09001683 read_lock(&tasklist_lock);
1684 p = find_task_by_vpid(pid);
1685 if (p)
1686 domain = tomoyo_real_domain(p);
1687 read_unlock(&tasklist_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001688 if (domain)
1689 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
1690 domain->domainname->name);
1691 head->read_eof = true;
1692 }
1693 return 0;
1694}
1695
1696/**
1697 * tomoyo_write_exception_policy - Write exception policy.
1698 *
1699 * @head: Pointer to "struct tomoyo_io_buffer".
1700 *
1701 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001702 *
1703 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001704 */
1705static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1706{
1707 char *data = head->write_buf;
1708 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1709
1710 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
1711 return tomoyo_write_domain_keeper_policy(data, false,
1712 is_delete);
1713 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
1714 return tomoyo_write_domain_keeper_policy(data, true, is_delete);
1715 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
1716 return tomoyo_write_domain_initializer_policy(data, false,
1717 is_delete);
1718 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
1719 return tomoyo_write_domain_initializer_policy(data, true,
1720 is_delete);
1721 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
1722 return tomoyo_write_alias_policy(data, is_delete);
1723 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1724 return tomoyo_write_globally_readable_policy(data, is_delete);
1725 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1726 return tomoyo_write_pattern_policy(data, is_delete);
1727 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1728 return tomoyo_write_no_rewrite_policy(data, is_delete);
1729 return -EINVAL;
1730}
1731
1732/**
1733 * tomoyo_read_exception_policy - Read exception policy.
1734 *
1735 * @head: Pointer to "struct tomoyo_io_buffer".
1736 *
1737 * Returns 0 on success, -EINVAL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001738 *
1739 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001740 */
1741static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
1742{
1743 if (!head->read_eof) {
1744 switch (head->read_step) {
1745 case 0:
1746 head->read_var2 = NULL;
1747 head->read_step = 1;
1748 case 1:
1749 if (!tomoyo_read_domain_keeper_policy(head))
1750 break;
1751 head->read_var2 = NULL;
1752 head->read_step = 2;
1753 case 2:
1754 if (!tomoyo_read_globally_readable_policy(head))
1755 break;
1756 head->read_var2 = NULL;
1757 head->read_step = 3;
1758 case 3:
1759 head->read_var2 = NULL;
1760 head->read_step = 4;
1761 case 4:
1762 if (!tomoyo_read_domain_initializer_policy(head))
1763 break;
1764 head->read_var2 = NULL;
1765 head->read_step = 5;
1766 case 5:
1767 if (!tomoyo_read_alias_policy(head))
1768 break;
1769 head->read_var2 = NULL;
1770 head->read_step = 6;
1771 case 6:
1772 head->read_var2 = NULL;
1773 head->read_step = 7;
1774 case 7:
1775 if (!tomoyo_read_file_pattern(head))
1776 break;
1777 head->read_var2 = NULL;
1778 head->read_step = 8;
1779 case 8:
1780 if (!tomoyo_read_no_rewrite_policy(head))
1781 break;
1782 head->read_var2 = NULL;
1783 head->read_step = 9;
1784 case 9:
1785 head->read_eof = true;
1786 break;
1787 default:
1788 return -EINVAL;
1789 }
1790 }
1791 return 0;
1792}
1793
1794/* path to policy loader */
1795static const char *tomoyo_loader = "/sbin/tomoyo-init";
1796
1797/**
1798 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
1799 *
1800 * Returns true if /sbin/tomoyo-init exists, false otherwise.
1801 */
1802static bool tomoyo_policy_loader_exists(void)
1803{
1804 /*
1805 * Don't activate MAC if the policy loader doesn't exist.
1806 * If the initrd includes /sbin/init but real-root-dev has not
1807 * mounted on / yet, activating MAC will block the system since
1808 * policies are not loaded yet.
1809 * Thus, let do_execve() call this function everytime.
1810 */
Al Viroe24977d2009-04-02 21:17:03 -04001811 struct path path;
Kentaro Takeda95908372009-02-05 17:18:13 +09001812
Al Viroe24977d2009-04-02 21:17:03 -04001813 if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001814 printk(KERN_INFO "Not activating Mandatory Access Control now "
1815 "since %s doesn't exist.\n", tomoyo_loader);
1816 return false;
1817 }
Al Viroe24977d2009-04-02 21:17:03 -04001818 path_put(&path);
Kentaro Takeda95908372009-02-05 17:18:13 +09001819 return true;
1820}
1821
1822/**
1823 * tomoyo_load_policy - Run external policy loader to load policy.
1824 *
1825 * @filename: The program about to start.
1826 *
1827 * This function checks whether @filename is /sbin/init , and if so
1828 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
1829 * and then continues invocation of /sbin/init.
1830 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
1831 * writes to /sys/kernel/security/tomoyo/ interfaces.
1832 *
1833 * Returns nothing.
1834 */
1835void tomoyo_load_policy(const char *filename)
1836{
1837 char *argv[2];
1838 char *envp[3];
1839
1840 if (tomoyo_policy_loaded)
1841 return;
1842 /*
1843 * Check filename is /sbin/init or /sbin/tomoyo-start.
1844 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
1845 * be passed.
1846 * You can create /sbin/tomoyo-start by
1847 * "ln -s /bin/true /sbin/tomoyo-start".
1848 */
1849 if (strcmp(filename, "/sbin/init") &&
1850 strcmp(filename, "/sbin/tomoyo-start"))
1851 return;
1852 if (!tomoyo_policy_loader_exists())
1853 return;
1854
1855 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
1856 tomoyo_loader);
1857 argv[0] = (char *) tomoyo_loader;
1858 argv[1] = NULL;
1859 envp[0] = "HOME=/";
1860 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1861 envp[2] = NULL;
1862 call_usermodehelper(argv[0], argv, envp, 1);
1863
Tetsuo Handa39826a12009-04-08 22:31:28 +09001864 printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09001865 printk(KERN_INFO "Mandatory Access Control activated.\n");
1866 tomoyo_policy_loaded = true;
1867 { /* Check all profiles currently assigned to domains are defined. */
1868 struct tomoyo_domain_info *domain;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001869 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001870 const u8 profile = domain->profile;
1871 if (tomoyo_profile_ptr[profile])
1872 continue;
1873 panic("Profile %u (used by '%s') not defined.\n",
1874 profile, domain->domainname->name);
1875 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001876 }
1877}
1878
1879/**
1880 * tomoyo_read_version: Get version.
1881 *
1882 * @head: Pointer to "struct tomoyo_io_buffer".
1883 *
1884 * Returns version information.
1885 */
1886static int tomoyo_read_version(struct tomoyo_io_buffer *head)
1887{
1888 if (!head->read_eof) {
Tetsuo Handa39826a12009-04-08 22:31:28 +09001889 tomoyo_io_printf(head, "2.2.0");
Kentaro Takeda95908372009-02-05 17:18:13 +09001890 head->read_eof = true;
1891 }
1892 return 0;
1893}
1894
1895/**
1896 * tomoyo_read_self_domain - Get the current process's domainname.
1897 *
1898 * @head: Pointer to "struct tomoyo_io_buffer".
1899 *
1900 * Returns the current process's domainname.
1901 */
1902static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1903{
1904 if (!head->read_eof) {
1905 /*
1906 * tomoyo_domain()->domainname != NULL
1907 * because every process belongs to a domain and
1908 * the domain's name cannot be NULL.
1909 */
1910 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1911 head->read_eof = true;
1912 }
1913 return 0;
1914}
1915
1916/**
1917 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1918 *
1919 * @type: Type of interface.
1920 * @file: Pointer to "struct file".
1921 *
1922 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001923 *
1924 * Caller acquires tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001925 */
1926static int tomoyo_open_control(const u8 type, struct file *file)
1927{
1928 struct tomoyo_io_buffer *head = tomoyo_alloc(sizeof(*head));
1929
1930 if (!head)
1931 return -ENOMEM;
1932 mutex_init(&head->io_sem);
1933 switch (type) {
1934 case TOMOYO_DOMAINPOLICY:
1935 /* /sys/kernel/security/tomoyo/domain_policy */
1936 head->write = tomoyo_write_domain_policy;
1937 head->read = tomoyo_read_domain_policy;
1938 break;
1939 case TOMOYO_EXCEPTIONPOLICY:
1940 /* /sys/kernel/security/tomoyo/exception_policy */
1941 head->write = tomoyo_write_exception_policy;
1942 head->read = tomoyo_read_exception_policy;
1943 break;
1944 case TOMOYO_SELFDOMAIN:
1945 /* /sys/kernel/security/tomoyo/self_domain */
1946 head->read = tomoyo_read_self_domain;
1947 break;
1948 case TOMOYO_DOMAIN_STATUS:
1949 /* /sys/kernel/security/tomoyo/.domain_status */
1950 head->write = tomoyo_write_domain_profile;
1951 head->read = tomoyo_read_domain_profile;
1952 break;
1953 case TOMOYO_PROCESS_STATUS:
1954 /* /sys/kernel/security/tomoyo/.process_status */
1955 head->write = tomoyo_write_pid;
1956 head->read = tomoyo_read_pid;
1957 break;
1958 case TOMOYO_VERSION:
1959 /* /sys/kernel/security/tomoyo/version */
1960 head->read = tomoyo_read_version;
1961 head->readbuf_size = 128;
1962 break;
1963 case TOMOYO_MEMINFO:
1964 /* /sys/kernel/security/tomoyo/meminfo */
1965 head->write = tomoyo_write_memory_quota;
1966 head->read = tomoyo_read_memory_counter;
1967 head->readbuf_size = 512;
1968 break;
1969 case TOMOYO_PROFILE:
1970 /* /sys/kernel/security/tomoyo/profile */
1971 head->write = tomoyo_write_profile;
1972 head->read = tomoyo_read_profile;
1973 break;
1974 case TOMOYO_MANAGER:
1975 /* /sys/kernel/security/tomoyo/manager */
1976 head->write = tomoyo_write_manager_policy;
1977 head->read = tomoyo_read_manager_policy;
1978 break;
1979 }
1980 if (!(file->f_mode & FMODE_READ)) {
1981 /*
1982 * No need to allocate read_buf since it is not opened
1983 * for reading.
1984 */
1985 head->read = NULL;
1986 } else {
1987 if (!head->readbuf_size)
1988 head->readbuf_size = 4096 * 2;
1989 head->read_buf = tomoyo_alloc(head->readbuf_size);
1990 if (!head->read_buf) {
1991 tomoyo_free(head);
1992 return -ENOMEM;
1993 }
1994 }
1995 if (!(file->f_mode & FMODE_WRITE)) {
1996 /*
1997 * No need to allocate write_buf since it is not opened
1998 * for writing.
1999 */
2000 head->write = NULL;
2001 } else if (head->write) {
2002 head->writebuf_size = 4096 * 2;
2003 head->write_buf = tomoyo_alloc(head->writebuf_size);
2004 if (!head->write_buf) {
2005 tomoyo_free(head->read_buf);
2006 tomoyo_free(head);
2007 return -ENOMEM;
2008 }
2009 }
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002010 head->reader_idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09002011 file->private_data = head;
2012 /*
2013 * Call the handler now if the file is
2014 * /sys/kernel/security/tomoyo/self_domain
2015 * so that the user can use
2016 * cat < /sys/kernel/security/tomoyo/self_domain"
2017 * to know the current process's domainname.
2018 */
2019 if (type == TOMOYO_SELFDOMAIN)
2020 tomoyo_read_control(file, NULL, 0);
2021 return 0;
2022}
2023
2024/**
2025 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2026 *
2027 * @file: Pointer to "struct file".
2028 * @buffer: Poiner to buffer to write to.
2029 * @buffer_len: Size of @buffer.
2030 *
2031 * Returns bytes read on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002032 *
2033 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002034 */
2035static int tomoyo_read_control(struct file *file, char __user *buffer,
2036 const int buffer_len)
2037{
2038 int len = 0;
2039 struct tomoyo_io_buffer *head = file->private_data;
2040 char *cp;
2041
2042 if (!head->read)
2043 return -ENOSYS;
2044 if (mutex_lock_interruptible(&head->io_sem))
2045 return -EINTR;
2046 /* Call the policy handler. */
2047 len = head->read(head);
2048 if (len < 0)
2049 goto out;
2050 /* Write to buffer. */
2051 len = head->read_avail;
2052 if (len > buffer_len)
2053 len = buffer_len;
2054 if (!len)
2055 goto out;
2056 /* head->read_buf changes by some functions. */
2057 cp = head->read_buf;
2058 if (copy_to_user(buffer, cp, len)) {
2059 len = -EFAULT;
2060 goto out;
2061 }
2062 head->read_avail -= len;
2063 memmove(cp, cp + len, head->read_avail);
2064 out:
2065 mutex_unlock(&head->io_sem);
2066 return len;
2067}
2068
2069/**
2070 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2071 *
2072 * @file: Pointer to "struct file".
2073 * @buffer: Pointer to buffer to read from.
2074 * @buffer_len: Size of @buffer.
2075 *
2076 * Returns @buffer_len on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002077 *
2078 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002079 */
2080static int tomoyo_write_control(struct file *file, const char __user *buffer,
2081 const int buffer_len)
2082{
2083 struct tomoyo_io_buffer *head = file->private_data;
2084 int error = buffer_len;
2085 int avail_len = buffer_len;
2086 char *cp0 = head->write_buf;
2087
2088 if (!head->write)
2089 return -ENOSYS;
2090 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2091 return -EFAULT;
2092 /* Don't allow updating policies by non manager programs. */
2093 if (head->write != tomoyo_write_pid &&
2094 head->write != tomoyo_write_domain_policy &&
2095 !tomoyo_is_policy_manager())
2096 return -EPERM;
2097 if (mutex_lock_interruptible(&head->io_sem))
2098 return -EINTR;
2099 /* Read a line and dispatch it to the policy handler. */
2100 while (avail_len > 0) {
2101 char c;
2102 if (head->write_avail >= head->writebuf_size - 1) {
2103 error = -ENOMEM;
2104 break;
2105 } else if (get_user(c, buffer)) {
2106 error = -EFAULT;
2107 break;
2108 }
2109 buffer++;
2110 avail_len--;
2111 cp0[head->write_avail++] = c;
2112 if (c != '\n')
2113 continue;
2114 cp0[head->write_avail - 1] = '\0';
2115 head->write_avail = 0;
2116 tomoyo_normalize_line(cp0);
2117 head->write(head);
2118 }
2119 mutex_unlock(&head->io_sem);
2120 return error;
2121}
2122
2123/**
2124 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2125 *
2126 * @file: Pointer to "struct file".
2127 *
2128 * Releases memory and returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002129 *
2130 * Caller looses tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002131 */
2132static int tomoyo_close_control(struct file *file)
2133{
2134 struct tomoyo_io_buffer *head = file->private_data;
2135
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002136 tomoyo_read_unlock(head->reader_idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002137 /* Release memory used for policy I/O. */
2138 tomoyo_free(head->read_buf);
2139 head->read_buf = NULL;
2140 tomoyo_free(head->write_buf);
2141 head->write_buf = NULL;
2142 tomoyo_free(head);
2143 head = NULL;
2144 file->private_data = NULL;
2145 return 0;
2146}
2147
2148/**
2149 * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
2150 *
2151 * @acl_type: Type of ACL entry.
2152 *
2153 * Returns pointer to the ACL entry on success, NULL otherwise.
2154 */
2155void *tomoyo_alloc_acl_element(const u8 acl_type)
2156{
2157 int len;
2158 struct tomoyo_acl_info *ptr;
2159
2160 switch (acl_type) {
2161 case TOMOYO_TYPE_SINGLE_PATH_ACL:
2162 len = sizeof(struct tomoyo_single_path_acl_record);
2163 break;
2164 case TOMOYO_TYPE_DOUBLE_PATH_ACL:
2165 len = sizeof(struct tomoyo_double_path_acl_record);
2166 break;
2167 default:
2168 return NULL;
2169 }
2170 ptr = tomoyo_alloc_element(len);
2171 if (!ptr)
2172 return NULL;
2173 ptr->type = acl_type;
2174 return ptr;
2175}
2176
2177/**
2178 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2179 *
2180 * @inode: Pointer to "struct inode".
2181 * @file: Pointer to "struct file".
2182 *
2183 * Returns 0 on success, negative value otherwise.
2184 */
2185static int tomoyo_open(struct inode *inode, struct file *file)
2186{
2187 const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
2188 - ((u8 *) NULL);
2189 return tomoyo_open_control(key, file);
2190}
2191
2192/**
2193 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2194 *
2195 * @inode: Pointer to "struct inode".
2196 * @file: Pointer to "struct file".
2197 *
2198 * Returns 0 on success, negative value otherwise.
2199 */
2200static int tomoyo_release(struct inode *inode, struct file *file)
2201{
2202 return tomoyo_close_control(file);
2203}
2204
2205/**
2206 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2207 *
2208 * @file: Pointer to "struct file".
2209 * @buf: Pointer to buffer.
2210 * @count: Size of @buf.
2211 * @ppos: Unused.
2212 *
2213 * Returns bytes read on success, negative value otherwise.
2214 */
2215static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
2216 loff_t *ppos)
2217{
2218 return tomoyo_read_control(file, buf, count);
2219}
2220
2221/**
2222 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2223 *
2224 * @file: Pointer to "struct file".
2225 * @buf: Pointer to buffer.
2226 * @count: Size of @buf.
2227 * @ppos: Unused.
2228 *
2229 * Returns @count on success, negative value otherwise.
2230 */
2231static ssize_t tomoyo_write(struct file *file, const char __user *buf,
2232 size_t count, loff_t *ppos)
2233{
2234 return tomoyo_write_control(file, buf, count);
2235}
2236
Tetsuo Handac3fa1092009-06-08 12:37:39 +09002237/*
2238 * tomoyo_operations is a "struct file_operations" which is used for handling
2239 * /sys/kernel/security/tomoyo/ interface.
2240 *
2241 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
2242 * See tomoyo_io_buffer for internals.
2243 */
Kentaro Takeda95908372009-02-05 17:18:13 +09002244static const struct file_operations tomoyo_operations = {
2245 .open = tomoyo_open,
2246 .release = tomoyo_release,
2247 .read = tomoyo_read,
2248 .write = tomoyo_write,
2249};
2250
2251/**
2252 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2253 *
2254 * @name: The name of the interface file.
2255 * @mode: The permission of the interface file.
2256 * @parent: The parent directory.
2257 * @key: Type of interface.
2258 *
2259 * Returns nothing.
2260 */
2261static void __init tomoyo_create_entry(const char *name, const mode_t mode,
2262 struct dentry *parent, const u8 key)
2263{
2264 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
2265 &tomoyo_operations);
2266}
2267
2268/**
2269 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2270 *
2271 * Returns 0.
2272 */
2273static int __init tomoyo_initerface_init(void)
2274{
2275 struct dentry *tomoyo_dir;
2276
Tetsuo Handae5a3b952009-02-14 11:46:56 +09002277 /* Don't create securityfs entries unless registered. */
2278 if (current_cred()->security != &tomoyo_kernel_domain)
2279 return 0;
2280
Kentaro Takeda95908372009-02-05 17:18:13 +09002281 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
2282 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
2283 TOMOYO_DOMAINPOLICY);
2284 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
2285 TOMOYO_EXCEPTIONPOLICY);
2286 tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
2287 TOMOYO_SELFDOMAIN);
2288 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
2289 TOMOYO_DOMAIN_STATUS);
2290 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
2291 TOMOYO_PROCESS_STATUS);
2292 tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
2293 TOMOYO_MEMINFO);
2294 tomoyo_create_entry("profile", 0600, tomoyo_dir,
2295 TOMOYO_PROFILE);
2296 tomoyo_create_entry("manager", 0600, tomoyo_dir,
2297 TOMOYO_MANAGER);
2298 tomoyo_create_entry("version", 0400, tomoyo_dir,
2299 TOMOYO_VERSION);
2300 return 0;
2301}
2302
2303fs_initcall(tomoyo_initerface_init);