blob: a53ee059da4818f5841bf35e62ceb18d2b6f5de1 [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 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900753 * This function uses kzalloc(), so the caller must call kfree()
Kentaro Takeda95908372009-02-05 17:18:13 +0900754 * 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) {
Tetsuo Handaea13ddb2010-02-03 06:43:06 +0900845 switch (ptr->type) {
Tetsuo Handa937bf612009-12-02 21:09:48 +0900846 struct tomoyo_single_path_acl_record *acl;
847 u32 perm;
848 u8 i;
Kentaro Takeda95908372009-02-05 17:18:13 +0900849 case TOMOYO_TYPE_SINGLE_PATH_ACL:
Tetsuo Handa937bf612009-12-02 21:09:48 +0900850 acl = container_of(ptr,
851 struct tomoyo_single_path_acl_record,
852 head);
853 perm = acl->perm | (((u32) acl->perm_high) << 16);
854 for (i = 0; i < TOMOYO_MAX_SINGLE_PATH_OPERATION; i++)
855 if (perm & (1 << i))
856 count++;
857 if (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL))
858 count -= 2;
Kentaro Takeda95908372009-02-05 17:18:13 +0900859 break;
860 case TOMOYO_TYPE_DOUBLE_PATH_ACL:
Tetsuo Handa937bf612009-12-02 21:09:48 +0900861 perm = container_of(ptr,
Kentaro Takeda95908372009-02-05 17:18:13 +0900862 struct tomoyo_double_path_acl_record,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900863 head)->perm;
864 for (i = 0; i < TOMOYO_MAX_DOUBLE_PATH_OPERATION; i++)
865 if (perm & (1 << i))
866 count++;
Kentaro Takeda95908372009-02-05 17:18:13 +0900867 break;
868 }
869 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900870 if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
871 return true;
872 if (!domain->quota_warned) {
873 domain->quota_warned = true;
874 printk(KERN_WARNING "TOMOYO-WARNING: "
875 "Domain '%s' has so many ACLs to hold. "
876 "Stopped learning mode.\n", domain->domainname->name);
877 }
878 return false;
879}
880
881/**
882 * tomoyo_find_or_assign_new_profile - Create a new profile.
883 *
884 * @profile: Profile number to create.
885 *
886 * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
887 */
888static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
889 int profile)
890{
891 static DEFINE_MUTEX(lock);
892 struct tomoyo_profile *ptr = NULL;
893 int i;
894
895 if (profile >= TOMOYO_MAX_PROFILES)
896 return NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +0900897 mutex_lock(&lock);
898 ptr = tomoyo_profile_ptr[profile];
899 if (ptr)
900 goto ok;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900901 ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
902 if (!tomoyo_memory_ok(ptr)) {
903 kfree(ptr);
Kentaro Takeda95908372009-02-05 17:18:13 +0900904 goto ok;
Tetsuo Handacd7bec62010-01-05 06:39:37 +0900905 }
Kentaro Takeda95908372009-02-05 17:18:13 +0900906 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{
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001106 struct tomoyo_policy_manager_entry *entry = NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +09001107 struct tomoyo_policy_manager_entry *ptr;
1108 const struct tomoyo_path_info *saved_manager;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001109 int error = is_delete ? -ENOENT : -ENOMEM;
Kentaro Takeda95908372009-02-05 17:18:13 +09001110 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 Handaca0b7df2010-02-07 20:23:59 +09001123 if (!is_delete)
1124 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
Tetsuo Handaf737d952010-01-03 21:16:32 +09001125 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001126 list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001127 if (ptr->manager != saved_manager)
1128 continue;
1129 ptr->is_deleted = is_delete;
1130 error = 0;
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001131 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001132 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001133 if (!is_delete && error && tomoyo_memory_ok(entry)) {
1134 entry->manager = saved_manager;
1135 entry->is_domain = is_domain;
1136 list_add_tail_rcu(&entry->list, &tomoyo_policy_manager_list);
1137 entry = NULL;
1138 error = 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09001139 }
Tetsuo Handaf737d952010-01-03 21:16:32 +09001140 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaca0b7df2010-02-07 20:23:59 +09001141 kfree(entry);
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 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001245 kfree(exe);
Kentaro Takeda95908372009-02-05 17:18:13 +09001246 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);
Tetsuo Handaf737d952010-01-03 21:16:32 +09001318 mutex_lock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001319 /* 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 }
Tetsuo Handaf737d952010-01-03 21:16:32 +09001330 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handaccf135f2009-06-19 10:29:34 +09001331 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)) {
Tetsuo Handaea13ddb2010-02-03 06:43:06 +09001381 domain->ignore_global_allow_read = !is_delete;
Kentaro Takeda95908372009-02-05 17:18:13 +09001382 return 0;
1383 }
1384 return tomoyo_write_file_policy(data, domain, is_delete);
1385}
1386
1387/**
1388 * tomoyo_print_single_path_acl - Print a single path ACL entry.
1389 *
1390 * @head: Pointer to "struct tomoyo_io_buffer".
1391 * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
1392 *
1393 * Returns true on success, false otherwise.
1394 */
1395static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer *head,
1396 struct tomoyo_single_path_acl_record *
1397 ptr)
1398{
1399 int pos;
1400 u8 bit;
1401 const char *atmark = "";
1402 const char *filename;
Tetsuo Handa937bf612009-12-02 21:09:48 +09001403 const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
Kentaro Takeda95908372009-02-05 17:18:13 +09001404
1405 filename = ptr->filename->name;
1406 for (bit = head->read_bit; bit < TOMOYO_MAX_SINGLE_PATH_OPERATION;
1407 bit++) {
1408 const char *msg;
1409 if (!(perm & (1 << bit)))
1410 continue;
1411 /* Print "read/write" instead of "read" and "write". */
1412 if ((bit == TOMOYO_TYPE_READ_ACL ||
1413 bit == TOMOYO_TYPE_WRITE_ACL)
1414 && (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
1415 continue;
1416 msg = tomoyo_sp2keyword(bit);
1417 pos = head->read_avail;
1418 if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
1419 atmark, filename))
1420 goto out;
1421 }
1422 head->read_bit = 0;
1423 return true;
1424 out:
1425 head->read_bit = bit;
1426 head->read_avail = pos;
1427 return false;
1428}
1429
1430/**
1431 * tomoyo_print_double_path_acl - Print a double path ACL entry.
1432 *
1433 * @head: Pointer to "struct tomoyo_io_buffer".
1434 * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
1435 *
1436 * Returns true on success, false otherwise.
1437 */
1438static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer *head,
1439 struct tomoyo_double_path_acl_record *
1440 ptr)
1441{
1442 int pos;
1443 const char *atmark1 = "";
1444 const char *atmark2 = "";
1445 const char *filename1;
1446 const char *filename2;
1447 const u8 perm = ptr->perm;
1448 u8 bit;
1449
1450 filename1 = ptr->filename1->name;
1451 filename2 = ptr->filename2->name;
1452 for (bit = head->read_bit; bit < TOMOYO_MAX_DOUBLE_PATH_OPERATION;
1453 bit++) {
1454 const char *msg;
1455 if (!(perm & (1 << bit)))
1456 continue;
1457 msg = tomoyo_dp2keyword(bit);
1458 pos = head->read_avail;
1459 if (!tomoyo_io_printf(head, "allow_%s %s%s %s%s\n", msg,
1460 atmark1, filename1, atmark2, filename2))
1461 goto out;
1462 }
1463 head->read_bit = 0;
1464 return true;
1465 out:
1466 head->read_bit = bit;
1467 head->read_avail = pos;
1468 return false;
1469}
1470
1471/**
1472 * tomoyo_print_entry - Print an ACL entry.
1473 *
1474 * @head: Pointer to "struct tomoyo_io_buffer".
1475 * @ptr: Pointer to an ACL entry.
1476 *
1477 * Returns true on success, false otherwise.
1478 */
1479static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
1480 struct tomoyo_acl_info *ptr)
1481{
Tetsuo Handaea13ddb2010-02-03 06:43:06 +09001482 const u8 acl_type = ptr->type;
Kentaro Takeda95908372009-02-05 17:18:13 +09001483
Kentaro Takeda95908372009-02-05 17:18:13 +09001484 if (acl_type == TOMOYO_TYPE_SINGLE_PATH_ACL) {
1485 struct tomoyo_single_path_acl_record *acl
1486 = container_of(ptr,
1487 struct tomoyo_single_path_acl_record,
1488 head);
1489 return tomoyo_print_single_path_acl(head, acl);
1490 }
1491 if (acl_type == TOMOYO_TYPE_DOUBLE_PATH_ACL) {
1492 struct tomoyo_double_path_acl_record *acl
1493 = container_of(ptr,
1494 struct tomoyo_double_path_acl_record,
1495 head);
1496 return tomoyo_print_double_path_acl(head, acl);
1497 }
1498 BUG(); /* This must not happen. */
1499 return false;
1500}
1501
1502/**
1503 * tomoyo_read_domain_policy - Read domain policy.
1504 *
1505 * @head: Pointer to "struct tomoyo_io_buffer".
1506 *
1507 * Returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001508 *
1509 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001510 */
1511static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
1512{
1513 struct list_head *dpos;
1514 struct list_head *apos;
1515 bool done = true;
1516
1517 if (head->read_eof)
1518 return 0;
1519 if (head->read_step == 0)
1520 head->read_step = 1;
Kentaro Takeda95908372009-02-05 17:18:13 +09001521 list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
1522 struct tomoyo_domain_info *domain;
1523 const char *quota_exceeded = "";
1524 const char *transition_failed = "";
1525 const char *ignore_global_allow_read = "";
1526 domain = list_entry(dpos, struct tomoyo_domain_info, list);
1527 if (head->read_step != 1)
1528 goto acl_loop;
1529 if (domain->is_deleted && !head->read_single_domain)
1530 continue;
1531 /* Print domainname and flags. */
1532 if (domain->quota_warned)
1533 quota_exceeded = "quota_exceeded\n";
Tetsuo Handaea13ddb2010-02-03 06:43:06 +09001534 if (domain->transition_failed)
Kentaro Takeda95908372009-02-05 17:18:13 +09001535 transition_failed = "transition_failed\n";
Tetsuo Handaea13ddb2010-02-03 06:43:06 +09001536 if (domain->ignore_global_allow_read)
Kentaro Takeda95908372009-02-05 17:18:13 +09001537 ignore_global_allow_read
1538 = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001539 done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
1540 "%u\n%s%s%s\n",
1541 domain->domainname->name,
1542 domain->profile, quota_exceeded,
1543 transition_failed,
1544 ignore_global_allow_read);
1545 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001546 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001547 head->read_step = 2;
1548acl_loop:
1549 if (head->read_step == 3)
1550 goto tail_mark;
1551 /* Print ACL entries in the domain. */
Kentaro Takeda95908372009-02-05 17:18:13 +09001552 list_for_each_cookie(apos, head->read_var2,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001553 &domain->acl_info_list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001554 struct tomoyo_acl_info *ptr
1555 = list_entry(apos, struct tomoyo_acl_info,
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001556 list);
1557 done = tomoyo_print_entry(head, ptr);
1558 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001559 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001560 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001561 if (!done)
1562 break;
1563 head->read_step = 3;
1564tail_mark:
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001565 done = tomoyo_io_printf(head, "\n");
1566 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001567 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001568 head->read_step = 1;
1569 if (head->read_single_domain)
1570 break;
1571 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001572 head->read_eof = done;
1573 return 0;
1574}
1575
1576/**
1577 * tomoyo_write_domain_profile - Assign profile for specified domain.
1578 *
1579 * @head: Pointer to "struct tomoyo_io_buffer".
1580 *
1581 * Returns 0 on success, -EINVAL otherwise.
1582 *
1583 * This is equivalent to doing
1584 *
1585 * ( echo "select " $domainname; echo "use_profile " $profile ) |
1586 * /usr/lib/ccs/loadpolicy -d
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001587 *
1588 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001589 */
1590static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
1591{
1592 char *data = head->write_buf;
1593 char *cp = strchr(data, ' ');
1594 struct tomoyo_domain_info *domain;
1595 unsigned long profile;
1596
1597 if (!cp)
1598 return -EINVAL;
1599 *cp = '\0';
Kentaro Takeda95908372009-02-05 17:18:13 +09001600 domain = tomoyo_find_domain(cp + 1);
Kentaro Takeda95908372009-02-05 17:18:13 +09001601 if (strict_strtoul(data, 10, &profile))
1602 return -EINVAL;
1603 if (domain && profile < TOMOYO_MAX_PROFILES
1604 && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
1605 domain->profile = (u8) profile;
1606 return 0;
1607}
1608
1609/**
1610 * tomoyo_read_domain_profile - Read only domainname and profile.
1611 *
1612 * @head: Pointer to "struct tomoyo_io_buffer".
1613 *
1614 * Returns list of profile number and domainname pairs.
1615 *
1616 * This is equivalent to doing
1617 *
1618 * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
1619 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
1620 * domainname = $0; } else if ( $1 == "use_profile" ) {
1621 * print $2 " " domainname; domainname = ""; } } ; '
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001622 *
1623 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001624 */
1625static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
1626{
1627 struct list_head *pos;
1628 bool done = true;
1629
1630 if (head->read_eof)
1631 return 0;
Kentaro Takeda95908372009-02-05 17:18:13 +09001632 list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
1633 struct tomoyo_domain_info *domain;
1634 domain = list_entry(pos, struct tomoyo_domain_info, list);
1635 if (domain->is_deleted)
1636 continue;
Tetsuo Handa7d2948b2009-06-02 20:42:24 +09001637 done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
1638 domain->domainname->name);
1639 if (!done)
Kentaro Takeda95908372009-02-05 17:18:13 +09001640 break;
Kentaro Takeda95908372009-02-05 17:18:13 +09001641 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001642 head->read_eof = done;
1643 return 0;
1644}
1645
1646/**
1647 * tomoyo_write_pid: Specify PID to obtain domainname.
1648 *
1649 * @head: Pointer to "struct tomoyo_io_buffer".
1650 *
1651 * Returns 0.
1652 */
1653static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
1654{
1655 unsigned long pid;
1656 /* No error check. */
1657 strict_strtoul(head->write_buf, 10, &pid);
1658 head->read_step = (int) pid;
1659 head->read_eof = false;
1660 return 0;
1661}
1662
1663/**
1664 * tomoyo_read_pid - Get domainname of the specified PID.
1665 *
1666 * @head: Pointer to "struct tomoyo_io_buffer".
1667 *
1668 * Returns the domainname which the specified PID is in on success,
1669 * empty string otherwise.
1670 * The PID is specified by tomoyo_write_pid() so that the user can obtain
1671 * using read()/write() interface rather than sysctl() interface.
1672 */
1673static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
1674{
1675 if (head->read_avail == 0 && !head->read_eof) {
1676 const int pid = head->read_step;
1677 struct task_struct *p;
1678 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda95908372009-02-05 17:18:13 +09001679 read_lock(&tasklist_lock);
1680 p = find_task_by_vpid(pid);
1681 if (p)
1682 domain = tomoyo_real_domain(p);
1683 read_unlock(&tasklist_lock);
Kentaro Takeda95908372009-02-05 17:18:13 +09001684 if (domain)
1685 tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
1686 domain->domainname->name);
1687 head->read_eof = true;
1688 }
1689 return 0;
1690}
1691
1692/**
1693 * tomoyo_write_exception_policy - Write exception policy.
1694 *
1695 * @head: Pointer to "struct tomoyo_io_buffer".
1696 *
1697 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001698 *
1699 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001700 */
1701static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
1702{
1703 char *data = head->write_buf;
1704 bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
1705
1706 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
1707 return tomoyo_write_domain_keeper_policy(data, false,
1708 is_delete);
1709 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
1710 return tomoyo_write_domain_keeper_policy(data, true, is_delete);
1711 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
1712 return tomoyo_write_domain_initializer_policy(data, false,
1713 is_delete);
1714 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
1715 return tomoyo_write_domain_initializer_policy(data, true,
1716 is_delete);
1717 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
1718 return tomoyo_write_alias_policy(data, is_delete);
1719 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
1720 return tomoyo_write_globally_readable_policy(data, is_delete);
1721 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
1722 return tomoyo_write_pattern_policy(data, is_delete);
1723 if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
1724 return tomoyo_write_no_rewrite_policy(data, is_delete);
1725 return -EINVAL;
1726}
1727
1728/**
1729 * tomoyo_read_exception_policy - Read exception policy.
1730 *
1731 * @head: Pointer to "struct tomoyo_io_buffer".
1732 *
1733 * Returns 0 on success, -EINVAL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001734 *
1735 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001736 */
1737static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
1738{
1739 if (!head->read_eof) {
1740 switch (head->read_step) {
1741 case 0:
1742 head->read_var2 = NULL;
1743 head->read_step = 1;
1744 case 1:
1745 if (!tomoyo_read_domain_keeper_policy(head))
1746 break;
1747 head->read_var2 = NULL;
1748 head->read_step = 2;
1749 case 2:
1750 if (!tomoyo_read_globally_readable_policy(head))
1751 break;
1752 head->read_var2 = NULL;
1753 head->read_step = 3;
1754 case 3:
1755 head->read_var2 = NULL;
1756 head->read_step = 4;
1757 case 4:
1758 if (!tomoyo_read_domain_initializer_policy(head))
1759 break;
1760 head->read_var2 = NULL;
1761 head->read_step = 5;
1762 case 5:
1763 if (!tomoyo_read_alias_policy(head))
1764 break;
1765 head->read_var2 = NULL;
1766 head->read_step = 6;
1767 case 6:
1768 head->read_var2 = NULL;
1769 head->read_step = 7;
1770 case 7:
1771 if (!tomoyo_read_file_pattern(head))
1772 break;
1773 head->read_var2 = NULL;
1774 head->read_step = 8;
1775 case 8:
1776 if (!tomoyo_read_no_rewrite_policy(head))
1777 break;
1778 head->read_var2 = NULL;
1779 head->read_step = 9;
1780 case 9:
1781 head->read_eof = true;
1782 break;
1783 default:
1784 return -EINVAL;
1785 }
1786 }
1787 return 0;
1788}
1789
1790/* path to policy loader */
1791static const char *tomoyo_loader = "/sbin/tomoyo-init";
1792
1793/**
1794 * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
1795 *
1796 * Returns true if /sbin/tomoyo-init exists, false otherwise.
1797 */
1798static bool tomoyo_policy_loader_exists(void)
1799{
1800 /*
1801 * Don't activate MAC if the policy loader doesn't exist.
1802 * If the initrd includes /sbin/init but real-root-dev has not
1803 * mounted on / yet, activating MAC will block the system since
1804 * policies are not loaded yet.
1805 * Thus, let do_execve() call this function everytime.
1806 */
Al Viroe24977d2009-04-02 21:17:03 -04001807 struct path path;
Kentaro Takeda95908372009-02-05 17:18:13 +09001808
Al Viroe24977d2009-04-02 21:17:03 -04001809 if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001810 printk(KERN_INFO "Not activating Mandatory Access Control now "
1811 "since %s doesn't exist.\n", tomoyo_loader);
1812 return false;
1813 }
Al Viroe24977d2009-04-02 21:17:03 -04001814 path_put(&path);
Kentaro Takeda95908372009-02-05 17:18:13 +09001815 return true;
1816}
1817
1818/**
1819 * tomoyo_load_policy - Run external policy loader to load policy.
1820 *
1821 * @filename: The program about to start.
1822 *
1823 * This function checks whether @filename is /sbin/init , and if so
1824 * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
1825 * and then continues invocation of /sbin/init.
1826 * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
1827 * writes to /sys/kernel/security/tomoyo/ interfaces.
1828 *
1829 * Returns nothing.
1830 */
1831void tomoyo_load_policy(const char *filename)
1832{
1833 char *argv[2];
1834 char *envp[3];
1835
1836 if (tomoyo_policy_loaded)
1837 return;
1838 /*
1839 * Check filename is /sbin/init or /sbin/tomoyo-start.
1840 * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
1841 * be passed.
1842 * You can create /sbin/tomoyo-start by
1843 * "ln -s /bin/true /sbin/tomoyo-start".
1844 */
1845 if (strcmp(filename, "/sbin/init") &&
1846 strcmp(filename, "/sbin/tomoyo-start"))
1847 return;
1848 if (!tomoyo_policy_loader_exists())
1849 return;
1850
1851 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
1852 tomoyo_loader);
1853 argv[0] = (char *) tomoyo_loader;
1854 argv[1] = NULL;
1855 envp[0] = "HOME=/";
1856 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1857 envp[2] = NULL;
1858 call_usermodehelper(argv[0], argv, envp, 1);
1859
Tetsuo Handa39826a12009-04-08 22:31:28 +09001860 printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
Kentaro Takeda95908372009-02-05 17:18:13 +09001861 printk(KERN_INFO "Mandatory Access Control activated.\n");
1862 tomoyo_policy_loaded = true;
1863 { /* Check all profiles currently assigned to domains are defined. */
1864 struct tomoyo_domain_info *domain;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001865 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Kentaro Takeda95908372009-02-05 17:18:13 +09001866 const u8 profile = domain->profile;
1867 if (tomoyo_profile_ptr[profile])
1868 continue;
1869 panic("Profile %u (used by '%s') not defined.\n",
1870 profile, domain->domainname->name);
1871 }
Kentaro Takeda95908372009-02-05 17:18:13 +09001872 }
1873}
1874
1875/**
1876 * tomoyo_read_version: Get version.
1877 *
1878 * @head: Pointer to "struct tomoyo_io_buffer".
1879 *
1880 * Returns version information.
1881 */
1882static int tomoyo_read_version(struct tomoyo_io_buffer *head)
1883{
1884 if (!head->read_eof) {
Tetsuo Handa39826a12009-04-08 22:31:28 +09001885 tomoyo_io_printf(head, "2.2.0");
Kentaro Takeda95908372009-02-05 17:18:13 +09001886 head->read_eof = true;
1887 }
1888 return 0;
1889}
1890
1891/**
1892 * tomoyo_read_self_domain - Get the current process's domainname.
1893 *
1894 * @head: Pointer to "struct tomoyo_io_buffer".
1895 *
1896 * Returns the current process's domainname.
1897 */
1898static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
1899{
1900 if (!head->read_eof) {
1901 /*
1902 * tomoyo_domain()->domainname != NULL
1903 * because every process belongs to a domain and
1904 * the domain's name cannot be NULL.
1905 */
1906 tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
1907 head->read_eof = true;
1908 }
1909 return 0;
1910}
1911
1912/**
1913 * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
1914 *
1915 * @type: Type of interface.
1916 * @file: Pointer to "struct file".
1917 *
1918 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09001919 *
1920 * Caller acquires tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09001921 */
1922static int tomoyo_open_control(const u8 type, struct file *file)
1923{
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001924 struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);
Kentaro Takeda95908372009-02-05 17:18:13 +09001925
1926 if (!head)
1927 return -ENOMEM;
1928 mutex_init(&head->io_sem);
1929 switch (type) {
1930 case TOMOYO_DOMAINPOLICY:
1931 /* /sys/kernel/security/tomoyo/domain_policy */
1932 head->write = tomoyo_write_domain_policy;
1933 head->read = tomoyo_read_domain_policy;
1934 break;
1935 case TOMOYO_EXCEPTIONPOLICY:
1936 /* /sys/kernel/security/tomoyo/exception_policy */
1937 head->write = tomoyo_write_exception_policy;
1938 head->read = tomoyo_read_exception_policy;
1939 break;
1940 case TOMOYO_SELFDOMAIN:
1941 /* /sys/kernel/security/tomoyo/self_domain */
1942 head->read = tomoyo_read_self_domain;
1943 break;
1944 case TOMOYO_DOMAIN_STATUS:
1945 /* /sys/kernel/security/tomoyo/.domain_status */
1946 head->write = tomoyo_write_domain_profile;
1947 head->read = tomoyo_read_domain_profile;
1948 break;
1949 case TOMOYO_PROCESS_STATUS:
1950 /* /sys/kernel/security/tomoyo/.process_status */
1951 head->write = tomoyo_write_pid;
1952 head->read = tomoyo_read_pid;
1953 break;
1954 case TOMOYO_VERSION:
1955 /* /sys/kernel/security/tomoyo/version */
1956 head->read = tomoyo_read_version;
1957 head->readbuf_size = 128;
1958 break;
1959 case TOMOYO_MEMINFO:
1960 /* /sys/kernel/security/tomoyo/meminfo */
1961 head->write = tomoyo_write_memory_quota;
1962 head->read = tomoyo_read_memory_counter;
1963 head->readbuf_size = 512;
1964 break;
1965 case TOMOYO_PROFILE:
1966 /* /sys/kernel/security/tomoyo/profile */
1967 head->write = tomoyo_write_profile;
1968 head->read = tomoyo_read_profile;
1969 break;
1970 case TOMOYO_MANAGER:
1971 /* /sys/kernel/security/tomoyo/manager */
1972 head->write = tomoyo_write_manager_policy;
1973 head->read = tomoyo_read_manager_policy;
1974 break;
1975 }
1976 if (!(file->f_mode & FMODE_READ)) {
1977 /*
1978 * No need to allocate read_buf since it is not opened
1979 * for reading.
1980 */
1981 head->read = NULL;
1982 } else {
1983 if (!head->readbuf_size)
1984 head->readbuf_size = 4096 * 2;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001985 head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);
Kentaro Takeda95908372009-02-05 17:18:13 +09001986 if (!head->read_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001987 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09001988 return -ENOMEM;
1989 }
1990 }
1991 if (!(file->f_mode & FMODE_WRITE)) {
1992 /*
1993 * No need to allocate write_buf since it is not opened
1994 * for writing.
1995 */
1996 head->write = NULL;
1997 } else if (head->write) {
1998 head->writebuf_size = 4096 * 2;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09001999 head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);
Kentaro Takeda95908372009-02-05 17:18:13 +09002000 if (!head->write_buf) {
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002001 kfree(head->read_buf);
2002 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002003 return -ENOMEM;
2004 }
2005 }
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002006 head->reader_idx = tomoyo_read_lock();
Kentaro Takeda95908372009-02-05 17:18:13 +09002007 file->private_data = head;
2008 /*
2009 * Call the handler now if the file is
2010 * /sys/kernel/security/tomoyo/self_domain
2011 * so that the user can use
2012 * cat < /sys/kernel/security/tomoyo/self_domain"
2013 * to know the current process's domainname.
2014 */
2015 if (type == TOMOYO_SELFDOMAIN)
2016 tomoyo_read_control(file, NULL, 0);
2017 return 0;
2018}
2019
2020/**
2021 * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
2022 *
2023 * @file: Pointer to "struct file".
2024 * @buffer: Poiner to buffer to write to.
2025 * @buffer_len: Size of @buffer.
2026 *
2027 * Returns bytes read on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002028 *
2029 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002030 */
2031static int tomoyo_read_control(struct file *file, char __user *buffer,
2032 const int buffer_len)
2033{
2034 int len = 0;
2035 struct tomoyo_io_buffer *head = file->private_data;
2036 char *cp;
2037
2038 if (!head->read)
2039 return -ENOSYS;
2040 if (mutex_lock_interruptible(&head->io_sem))
2041 return -EINTR;
2042 /* Call the policy handler. */
2043 len = head->read(head);
2044 if (len < 0)
2045 goto out;
2046 /* Write to buffer. */
2047 len = head->read_avail;
2048 if (len > buffer_len)
2049 len = buffer_len;
2050 if (!len)
2051 goto out;
2052 /* head->read_buf changes by some functions. */
2053 cp = head->read_buf;
2054 if (copy_to_user(buffer, cp, len)) {
2055 len = -EFAULT;
2056 goto out;
2057 }
2058 head->read_avail -= len;
2059 memmove(cp, cp + len, head->read_avail);
2060 out:
2061 mutex_unlock(&head->io_sem);
2062 return len;
2063}
2064
2065/**
2066 * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
2067 *
2068 * @file: Pointer to "struct file".
2069 * @buffer: Pointer to buffer to read from.
2070 * @buffer_len: Size of @buffer.
2071 *
2072 * Returns @buffer_len on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002073 *
2074 * Caller holds tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002075 */
2076static int tomoyo_write_control(struct file *file, const char __user *buffer,
2077 const int buffer_len)
2078{
2079 struct tomoyo_io_buffer *head = file->private_data;
2080 int error = buffer_len;
2081 int avail_len = buffer_len;
2082 char *cp0 = head->write_buf;
2083
2084 if (!head->write)
2085 return -ENOSYS;
2086 if (!access_ok(VERIFY_READ, buffer, buffer_len))
2087 return -EFAULT;
2088 /* Don't allow updating policies by non manager programs. */
2089 if (head->write != tomoyo_write_pid &&
2090 head->write != tomoyo_write_domain_policy &&
2091 !tomoyo_is_policy_manager())
2092 return -EPERM;
2093 if (mutex_lock_interruptible(&head->io_sem))
2094 return -EINTR;
2095 /* Read a line and dispatch it to the policy handler. */
2096 while (avail_len > 0) {
2097 char c;
2098 if (head->write_avail >= head->writebuf_size - 1) {
2099 error = -ENOMEM;
2100 break;
2101 } else if (get_user(c, buffer)) {
2102 error = -EFAULT;
2103 break;
2104 }
2105 buffer++;
2106 avail_len--;
2107 cp0[head->write_avail++] = c;
2108 if (c != '\n')
2109 continue;
2110 cp0[head->write_avail - 1] = '\0';
2111 head->write_avail = 0;
2112 tomoyo_normalize_line(cp0);
2113 head->write(head);
2114 }
2115 mutex_unlock(&head->io_sem);
2116 return error;
2117}
2118
2119/**
2120 * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
2121 *
2122 * @file: Pointer to "struct file".
2123 *
2124 * Releases memory and returns 0.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002125 *
2126 * Caller looses tomoyo_read_lock().
Kentaro Takeda95908372009-02-05 17:18:13 +09002127 */
2128static int tomoyo_close_control(struct file *file)
2129{
2130 struct tomoyo_io_buffer *head = file->private_data;
2131
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +09002132 tomoyo_read_unlock(head->reader_idx);
Kentaro Takeda95908372009-02-05 17:18:13 +09002133 /* Release memory used for policy I/O. */
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002134 kfree(head->read_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09002135 head->read_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002136 kfree(head->write_buf);
Kentaro Takeda95908372009-02-05 17:18:13 +09002137 head->write_buf = NULL;
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +09002138 kfree(head);
Kentaro Takeda95908372009-02-05 17:18:13 +09002139 head = NULL;
2140 file->private_data = NULL;
2141 return 0;
2142}
2143
2144/**
Kentaro Takeda95908372009-02-05 17:18:13 +09002145 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
2146 *
2147 * @inode: Pointer to "struct inode".
2148 * @file: Pointer to "struct file".
2149 *
2150 * Returns 0 on success, negative value otherwise.
2151 */
2152static int tomoyo_open(struct inode *inode, struct file *file)
2153{
2154 const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
2155 - ((u8 *) NULL);
2156 return tomoyo_open_control(key, file);
2157}
2158
2159/**
2160 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
2161 *
2162 * @inode: Pointer to "struct inode".
2163 * @file: Pointer to "struct file".
2164 *
2165 * Returns 0 on success, negative value otherwise.
2166 */
2167static int tomoyo_release(struct inode *inode, struct file *file)
2168{
2169 return tomoyo_close_control(file);
2170}
2171
2172/**
2173 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
2174 *
2175 * @file: Pointer to "struct file".
2176 * @buf: Pointer to buffer.
2177 * @count: Size of @buf.
2178 * @ppos: Unused.
2179 *
2180 * Returns bytes read on success, negative value otherwise.
2181 */
2182static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
2183 loff_t *ppos)
2184{
2185 return tomoyo_read_control(file, buf, count);
2186}
2187
2188/**
2189 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
2190 *
2191 * @file: Pointer to "struct file".
2192 * @buf: Pointer to buffer.
2193 * @count: Size of @buf.
2194 * @ppos: Unused.
2195 *
2196 * Returns @count on success, negative value otherwise.
2197 */
2198static ssize_t tomoyo_write(struct file *file, const char __user *buf,
2199 size_t count, loff_t *ppos)
2200{
2201 return tomoyo_write_control(file, buf, count);
2202}
2203
Tetsuo Handac3fa1092009-06-08 12:37:39 +09002204/*
2205 * tomoyo_operations is a "struct file_operations" which is used for handling
2206 * /sys/kernel/security/tomoyo/ interface.
2207 *
2208 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
2209 * See tomoyo_io_buffer for internals.
2210 */
Kentaro Takeda95908372009-02-05 17:18:13 +09002211static const struct file_operations tomoyo_operations = {
2212 .open = tomoyo_open,
2213 .release = tomoyo_release,
2214 .read = tomoyo_read,
2215 .write = tomoyo_write,
2216};
2217
2218/**
2219 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
2220 *
2221 * @name: The name of the interface file.
2222 * @mode: The permission of the interface file.
2223 * @parent: The parent directory.
2224 * @key: Type of interface.
2225 *
2226 * Returns nothing.
2227 */
2228static void __init tomoyo_create_entry(const char *name, const mode_t mode,
2229 struct dentry *parent, const u8 key)
2230{
2231 securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
2232 &tomoyo_operations);
2233}
2234
2235/**
2236 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
2237 *
2238 * Returns 0.
2239 */
2240static int __init tomoyo_initerface_init(void)
2241{
2242 struct dentry *tomoyo_dir;
2243
Tetsuo Handae5a3b952009-02-14 11:46:56 +09002244 /* Don't create securityfs entries unless registered. */
2245 if (current_cred()->security != &tomoyo_kernel_domain)
2246 return 0;
2247
Kentaro Takeda95908372009-02-05 17:18:13 +09002248 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
2249 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
2250 TOMOYO_DOMAINPOLICY);
2251 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
2252 TOMOYO_EXCEPTIONPOLICY);
2253 tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
2254 TOMOYO_SELFDOMAIN);
2255 tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
2256 TOMOYO_DOMAIN_STATUS);
2257 tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
2258 TOMOYO_PROCESS_STATUS);
2259 tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
2260 TOMOYO_MEMINFO);
2261 tomoyo_create_entry("profile", 0600, tomoyo_dir,
2262 TOMOYO_PROFILE);
2263 tomoyo_create_entry("manager", 0600, tomoyo_dir,
2264 TOMOYO_MANAGER);
2265 tomoyo_create_entry("version", 0400, tomoyo_dir,
2266 TOMOYO_VERSION);
2267 return 0;
2268}
2269
2270fs_initcall(tomoyo_initerface_init);