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