オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

Contents of /trunk/1.7.x/ccs-patch/security/ccsecurity/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1992 - (show annotations) (download) (as text)
Mon Dec 22 00:57:21 2008 UTC (15 years, 5 months ago) by kumaneko
Original Path: trunk/1.6.x/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 86637 byte(s)


1 /*
2 * fs/ccs_common.c
3 *
4 * Common functions for SAKURA and TOMOYO.
5 *
6 * Copyright (C) 2005-2008 NTT DATA CORPORATION
7 *
8 * Version: 1.6.6-pre 2008/12/22
9 *
10 * This file is applicable to both 2.4.30 and 2.6.11 and later.
11 * See README.ccs for ChangeLog.
12 *
13 */
14
15 #include <linux/version.h>
16 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
17 #define __KERNEL_SYSCALLS__
18 #endif
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/utime.h>
22 #include <linux/file.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <asm/uaccess.h>
26 #include <stdarg.h>
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
28 #include <linux/namei.h>
29 #include <linux/mount.h>
30 static const int lookup_flags = LOOKUP_FOLLOW;
31 #else
32 static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;
33 #endif
34 #include <linux/realpath.h>
35 #include <linux/ccs_common.h>
36 #include <linux/ccs_proc.h>
37 #include <linux/tomoyo.h>
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
39 #include <linux/unistd.h>
40 #endif
41
42 /* To support PID namespace. */
43 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
44 #define find_task_by_pid find_task_by_vpid
45 #endif
46
47 /* Set default specified by the kernel config. */
48 #ifdef CONFIG_TOMOYO
49 #define MAX_ACCEPT_ENTRY (CONFIG_TOMOYO_MAX_ACCEPT_ENTRY)
50 #define MAX_GRANT_LOG (CONFIG_TOMOYO_MAX_GRANT_LOG)
51 #define MAX_REJECT_LOG (CONFIG_TOMOYO_MAX_REJECT_LOG)
52 #else
53 #define MAX_ACCEPT_ENTRY 0
54 #define MAX_GRANT_LOG 0
55 #define MAX_REJECT_LOG 0
56 #endif
57
58 /* Has /sbin/init started? */
59 bool sbin_init_started;
60
61 /* Log level for SAKURA's printk(). */
62 const char *ccs_log_level = KERN_DEBUG;
63
64 /* String table for functionality that takes 4 modes. */
65 static const char *mode_4[4] = {
66 "disabled", "learning", "permissive", "enforcing"
67 };
68 /* String table for functionality that takes 2 modes. */
69 static const char *mode_2[4] = {
70 "disabled", "enabled", "enabled", "enabled"
71 };
72
73 /* Table for profile. */
74 static struct {
75 const char *keyword;
76 unsigned int current_value;
77 const unsigned int max_value;
78 } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {
79 [CCS_TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
80 [CCS_TOMOYO_MAC_FOR_ARGV0] = { "MAC_FOR_ARGV0", 0, 3 },
81 [CCS_TOMOYO_MAC_FOR_ENV] = { "MAC_FOR_ENV", 0, 3 },
82 [CCS_TOMOYO_MAC_FOR_NETWORK] = { "MAC_FOR_NETWORK", 0, 3 },
83 [CCS_TOMOYO_MAC_FOR_SIGNAL] = { "MAC_FOR_SIGNAL", 0, 3 },
84 [CCS_SAKURA_DENY_CONCEAL_MOUNT] = { "DENY_CONCEAL_MOUNT", 0, 3 },
85 [CCS_SAKURA_RESTRICT_CHROOT] = { "RESTRICT_CHROOT", 0, 3 },
86 [CCS_SAKURA_RESTRICT_MOUNT] = { "RESTRICT_MOUNT", 0, 3 },
87 [CCS_SAKURA_RESTRICT_UNMOUNT] = { "RESTRICT_UNMOUNT", 0, 3 },
88 [CCS_SAKURA_RESTRICT_PIVOT_ROOT] = { "RESTRICT_PIVOT_ROOT", 0, 3 },
89 [CCS_SAKURA_RESTRICT_AUTOBIND] = { "RESTRICT_AUTOBIND", 0, 1 },
90 [CCS_TOMOYO_MAX_ACCEPT_ENTRY]
91 = { "MAX_ACCEPT_ENTRY", MAX_ACCEPT_ENTRY, INT_MAX },
92 [CCS_TOMOYO_MAX_GRANT_LOG]
93 = { "MAX_GRANT_LOG", MAX_GRANT_LOG, INT_MAX },
94 [CCS_TOMOYO_MAX_REJECT_LOG]
95 = { "MAX_REJECT_LOG", MAX_REJECT_LOG, INT_MAX },
96 [CCS_TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
97 [CCS_SLEEP_PERIOD]
98 = { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */
99 };
100
101 #ifdef CONFIG_TOMOYO
102 /* Capability name used by domain policy. */
103 static const char *capability_control_keyword[TOMOYO_MAX_CAPABILITY_INDEX] = {
104 [TOMOYO_INET_STREAM_SOCKET_CREATE] = "inet_tcp_create",
105 [TOMOYO_INET_STREAM_SOCKET_LISTEN] = "inet_tcp_listen",
106 [TOMOYO_INET_STREAM_SOCKET_CONNECT] = "inet_tcp_connect",
107 [TOMOYO_USE_INET_DGRAM_SOCKET] = "use_inet_udp",
108 [TOMOYO_USE_INET_RAW_SOCKET] = "use_inet_ip",
109 [TOMOYO_USE_ROUTE_SOCKET] = "use_route",
110 [TOMOYO_USE_PACKET_SOCKET] = "use_packet",
111 [TOMOYO_SYS_MOUNT] = "SYS_MOUNT",
112 [TOMOYO_SYS_UMOUNT] = "SYS_UMOUNT",
113 [TOMOYO_SYS_REBOOT] = "SYS_REBOOT",
114 [TOMOYO_SYS_CHROOT] = "SYS_CHROOT",
115 [TOMOYO_SYS_KILL] = "SYS_KILL",
116 [TOMOYO_SYS_VHANGUP] = "SYS_VHANGUP",
117 [TOMOYO_SYS_SETTIME] = "SYS_TIME",
118 [TOMOYO_SYS_NICE] = "SYS_NICE",
119 [TOMOYO_SYS_SETHOSTNAME] = "SYS_SETHOSTNAME",
120 [TOMOYO_USE_KERNEL_MODULE] = "use_kernel_module",
121 [TOMOYO_CREATE_FIFO] = "create_fifo",
122 [TOMOYO_CREATE_BLOCK_DEV] = "create_block_dev",
123 [TOMOYO_CREATE_CHAR_DEV] = "create_char_dev",
124 [TOMOYO_CREATE_UNIX_SOCKET] = "create_unix_socket",
125 [TOMOYO_SYS_LINK] = "SYS_LINK",
126 [TOMOYO_SYS_SYMLINK] = "SYS_SYMLINK",
127 [TOMOYO_SYS_RENAME] = "SYS_RENAME",
128 [TOMOYO_SYS_UNLINK] = "SYS_UNLINK",
129 [TOMOYO_SYS_CHMOD] = "SYS_CHMOD",
130 [TOMOYO_SYS_CHOWN] = "SYS_CHOWN",
131 [TOMOYO_SYS_IOCTL] = "SYS_IOCTL",
132 [TOMOYO_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD",
133 [TOMOYO_SYS_PIVOT_ROOT] = "SYS_PIVOT_ROOT",
134 [TOMOYO_SYS_PTRACE] = "SYS_PTRACE",
135 };
136 #endif
137
138 /* Profile table. Memory is allocated as needed. */
139 static struct profile {
140 unsigned int value[CCS_MAX_CONTROL_INDEX];
141 const struct path_info *comment;
142 #ifdef CONFIG_TOMOYO
143 unsigned char capability_value[TOMOYO_MAX_CAPABILITY_INDEX];
144 #endif
145 } *profile_ptr[MAX_PROFILES];
146
147 /* Permit policy management by non-root user? */
148 static bool manage_by_non_root;
149
150 /* Utility functions. */
151
152 #ifdef CONFIG_TOMOYO
153 /**
154 * tomoyo_quiet_setup - Set TOMOYO_VERBOSE=0 by default.
155 *
156 * @str: Unused.
157 *
158 * Returns 0.
159 */
160 static int __init tomoyo_quiet_setup(char *str)
161 {
162 ccs_control_array[CCS_TOMOYO_VERBOSE].current_value = 0;
163 return 0;
164 }
165
166 __setup("TOMOYO_QUIET", tomoyo_quiet_setup);
167 #endif
168
169 /**
170 * is_byte_range - Check whether the string isa \ooo style octal value.
171 *
172 * @str: Pointer to the string.
173 *
174 * Returns true if @str is a \ooo style octal value, false otherwise.
175 */
176 static inline bool is_byte_range(const char *str)
177 {
178 return *str >= '0' && *str++ <= '3' &&
179 *str >= '0' && *str++ <= '7' &&
180 *str >= '0' && *str <= '7';
181 }
182
183 /**
184 * is_decimal - Check whether the character is a decimal character.
185 *
186 * @c: The character to check.
187 *
188 * Returns true if @c is a decimal character, false otherwise.
189 */
190 static inline bool is_decimal(const char c)
191 {
192 return c >= '0' && c <= '9';
193 }
194
195 /**
196 * is_hexadecimal - Check whether the character is a hexadecimal character.
197 *
198 * @c: The character to check.
199 *
200 * Returns true if @c is a hexadecimal character, false otherwise.
201 */
202 static inline bool is_hexadecimal(const char c)
203 {
204 return (c >= '0' && c <= '9') ||
205 (c >= 'A' && c <= 'F') ||
206 (c >= 'a' && c <= 'f');
207 }
208
209 /**
210 * is_alphabet_char - Check whether the character is an alphabet.
211 *
212 * @c: The character to check.
213 *
214 * Returns true if @c is an alphabet character, false otherwise.
215 */
216 static inline bool is_alphabet_char(const char c)
217 {
218 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
219 }
220
221 /**
222 * make_byte - Make byte value from three octal characters.
223 *
224 * @c1: The first character.
225 * @c2: The second character.
226 * @c3: The third character.
227 *
228 * Returns byte value.
229 */
230 static inline u8 make_byte(const u8 c1, const u8 c2, const u8 c3)
231 {
232 return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
233 }
234
235 /**
236 * str_starts - Check whether the given string starts with the given keyword.
237 *
238 * @src: Pointer to pointer to the string.
239 * @find: Pointer to the keyword.
240 *
241 * Returns true if @src starts with @find, false otherwise.
242 *
243 * The @src is updated to point the first character after the @find
244 * if @src starts with @find.
245 */
246 static bool str_starts(char **src, const char *find)
247 {
248 const int len = strlen(find);
249 char *tmp = *src;
250 if (strncmp(tmp, find, len))
251 return false;
252 tmp += len;
253 *src = tmp;
254 return true;
255 }
256
257 /**
258 * normalize_line - Format string.
259 *
260 * @buffer: The line to normalize.
261 *
262 * Leading and trailing whitespaces are removed.
263 * Multiple whitespaces are packed into single space.
264 *
265 * Returns nothing.
266 */
267 static void normalize_line(unsigned char *buffer)
268 {
269 unsigned char *sp = buffer;
270 unsigned char *dp = buffer;
271 bool first = true;
272 while (*sp && (*sp <= ' ' || *sp >= 127))
273 sp++;
274 while (*sp) {
275 if (!first)
276 *dp++ = ' ';
277 first = false;
278 while (*sp > ' ' && *sp < 127)
279 *dp++ = *sp++;
280 while (*sp && (*sp <= ' ' || *sp >= 127))
281 sp++;
282 }
283 *dp = '\0';
284 }
285
286 /**
287 * ccs_is_correct_path - Validate a pathname.
288 * @filename: The pathname to check.
289 * @start_type: Should the pathname start with '/'?
290 * 1 = must / -1 = must not / 0 = don't care
291 * @pattern_type: Can the pathname contain a wildcard?
292 * 1 = must / -1 = must not / 0 = don't care
293 * @end_type: Should the pathname end with '/'?
294 * 1 = must / -1 = must not / 0 = don't care
295 * @function: The name of function calling me.
296 *
297 * Check whether the given filename follows the naming rules.
298 * Returns true if @filename follows the naming rules, false otherwise.
299 */
300 bool ccs_is_correct_path(const char *filename, const s8 start_type,
301 const s8 pattern_type, const s8 end_type,
302 const char *function)
303 {
304 bool contains_pattern = false;
305 unsigned char c;
306 unsigned char d;
307 unsigned char e;
308 const char *original_filename = filename;
309 if (!filename)
310 goto out;
311 c = *filename;
312 if (start_type == 1) { /* Must start with '/' */
313 if (c != '/')
314 goto out;
315 } else if (start_type == -1) { /* Must not start with '/' */
316 if (c == '/')
317 goto out;
318 }
319 if (c)
320 c = *(filename + strlen(filename) - 1);
321 if (end_type == 1) { /* Must end with '/' */
322 if (c != '/')
323 goto out;
324 } else if (end_type == -1) { /* Must not end with '/' */
325 if (c == '/')
326 goto out;
327 }
328 while (1) {
329 c = *filename++;
330 if (!c)
331 break;
332 if (c == '\\') {
333 c = *filename++;
334 switch (c) {
335 case '\\': /* "\\" */
336 continue;
337 case '$': /* "\$" */
338 case '+': /* "\+" */
339 case '?': /* "\?" */
340 case '*': /* "\*" */
341 case '@': /* "\@" */
342 case 'x': /* "\x" */
343 case 'X': /* "\X" */
344 case 'a': /* "\a" */
345 case 'A': /* "\A" */
346 case '-': /* "\-" */
347 if (pattern_type == -1)
348 break; /* Must not contain pattern */
349 contains_pattern = true;
350 continue;
351 case '0': /* "\ooo" */
352 case '1':
353 case '2':
354 case '3':
355 d = *filename++;
356 if (d < '0' || d > '7')
357 break;
358 e = *filename++;
359 if (e < '0' || e > '7')
360 break;
361 c = make_byte(c, d, e);
362 if (c && (c <= ' ' || c >= 127))
363 continue; /* pattern is not \000 */
364 }
365 goto out;
366 } else if (c <= ' ' || c >= 127) {
367 goto out;
368 }
369 }
370 if (pattern_type == 1) { /* Must contain pattern */
371 if (!contains_pattern)
372 goto out;
373 }
374 return true;
375 out:
376 printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function,
377 original_filename);
378 return false;
379 }
380
381 /**
382 * ccs_is_correct_domain - Check whether the given domainname follows the naming rules.
383 * @domainname: The domainname to check.
384 * @function: The name of function calling me.
385 *
386 * Returns true if @domainname follows the naming rules, false otherwise.
387 */
388 bool ccs_is_correct_domain(const unsigned char *domainname,
389 const char *function)
390 {
391 unsigned char c;
392 unsigned char d;
393 unsigned char e;
394 const char *org_domainname = domainname;
395 if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN))
396 goto out;
397 domainname += ROOT_NAME_LEN;
398 if (!*domainname)
399 return true;
400 do {
401 if (*domainname++ != ' ')
402 goto out;
403 if (*domainname++ != '/')
404 goto out;
405 while (1) {
406 c = *domainname;
407 if (!c || c == ' ')
408 break;
409 domainname++;
410 if (c == '\\') {
411 c = *domainname++;
412 switch ((c)) {
413 case '\\': /* "\\" */
414 continue;
415 case '0': /* "\ooo" */
416 case '1':
417 case '2':
418 case '3':
419 d = *domainname++;
420 if (d < '0' || d > '7')
421 break;
422 e = *domainname++;
423 if (e < '0' || e > '7')
424 break;
425 c = make_byte(c, d, e);
426 if (c && (c <= ' ' || c >= 127))
427 /* pattern is not \000 */
428 continue;
429 }
430 goto out;
431 } else if (c < ' ' || c >= 127) {
432 goto out;
433 }
434 }
435 } while (*domainname);
436 return true;
437 out:
438 printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function,
439 org_domainname);
440 return false;
441 }
442
443 /**
444 * ccs_is_domain_def - Check whether the given token can be a domainname.
445 *
446 * @buffer: The token to check.
447 *
448 * Returns true if @buffer possibly be a domainname, false otherwise.
449 */
450 bool ccs_is_domain_def(const unsigned char *buffer)
451 {
452 return !strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN);
453 }
454
455 /**
456 * ccs_find_domain - Find a domain by the given name.
457 *
458 * @domainname: The domainname to find.
459 *
460 * Returns pointer to "struct domain_info" if found, NULL otherwise.
461 */
462 struct domain_info *ccs_find_domain(const char *domainname)
463 {
464 struct domain_info *domain;
465 struct path_info name;
466 name.name = domainname;
467 ccs_fill_path_info(&name);
468 list1_for_each_entry(domain, &domain_list, list) {
469 if (!domain->is_deleted &&
470 !ccs_pathcmp(&name, domain->domainname))
471 return domain;
472 }
473 return NULL;
474 }
475
476 /**
477 * path_depth - Evaluate the number of '/' in a string.
478 *
479 * @pathname: The string to evaluate.
480 *
481 * Returns path depth of the string.
482 *
483 * I score 2 for each of the '/' in the @pathname
484 * and score 1 if the @pathname ends with '/'.
485 */
486 static int path_depth(const char *pathname)
487 {
488 int i = 0;
489 if (pathname) {
490 const char *ep = pathname + strlen(pathname);
491 if (pathname < ep--) {
492 if (*ep != '/')
493 i++;
494 while (pathname <= ep)
495 if (*ep-- == '/')
496 i += 2;
497 }
498 }
499 return i;
500 }
501
502 /**
503 * const_part_length - Evaluate the initial length without a pattern in a token.
504 *
505 * @filename: The string to evaluate.
506 *
507 * Returns the initial length without a pattern in @filename.
508 */
509 static int const_part_length(const char *filename)
510 {
511 char c;
512 int len = 0;
513 if (!filename)
514 return 0;
515 while (1) {
516 c = *filename++;
517 if (!c)
518 break;
519 if (c != '\\') {
520 len++;
521 continue;
522 }
523 c = *filename++;
524 switch (c) {
525 case '\\': /* "\\" */
526 len += 2;
527 continue;
528 case '0': /* "\ooo" */
529 case '1':
530 case '2':
531 case '3':
532 c = *filename++;
533 if (c < '0' || c > '7')
534 break;
535 c = *filename++;
536 if (c < '0' || c > '7')
537 break;
538 len += 4;
539 continue;
540 }
541 break;
542 }
543 return len;
544 }
545
546 /**
547 * ccs_fill_path_info - Fill in "struct path_info" members.
548 *
549 * @ptr: Pointer to "struct path_info" to fill in.
550 *
551 * The caller sets "struct path_info"->name.
552 */
553 void ccs_fill_path_info(struct path_info *ptr)
554 {
555 const char *name = ptr->name;
556 const int len = strlen(name);
557 ptr->total_len = len;
558 ptr->const_len = const_part_length(name);
559 ptr->is_dir = len && (name[len - 1] == '/');
560 ptr->is_patterned = (ptr->const_len < len);
561 ptr->hash = full_name_hash(name, len);
562 ptr->depth = path_depth(name);
563 }
564
565 /**
566 * file_matches_to_pattern2 - Pattern matching without '/' character
567 * and "\-" pattern.
568 *
569 * @filename: The start of string to check.
570 * @filename_end: The end of string to check.
571 * @pattern: The start of pattern to compare.
572 * @pattern_end: The end of pattern to compare.
573 *
574 * Returns true if @filename matches @pattern, false otherwise.
575 */
576 static bool file_matches_to_pattern2(const char *filename,
577 const char *filename_end,
578 const char *pattern,
579 const char *pattern_end)
580 {
581 while (filename < filename_end && pattern < pattern_end) {
582 char c;
583 if (*pattern != '\\') {
584 if (*filename++ != *pattern++)
585 return false;
586 continue;
587 }
588 c = *filename;
589 pattern++;
590 switch (*pattern) {
591 int i;
592 int j;
593 case '?':
594 if (c == '/') {
595 return false;
596 } else if (c == '\\') {
597 if (filename[1] == '\\')
598 filename++;
599 else if (is_byte_range(filename + 1))
600 filename += 3;
601 else
602 return false;
603 }
604 break;
605 case '\\':
606 if (c != '\\')
607 return false;
608 if (*++filename != '\\')
609 return false;
610 break;
611 case '+':
612 if (!is_decimal(c))
613 return false;
614 break;
615 case 'x':
616 if (!is_hexadecimal(c))
617 return false;
618 break;
619 case 'a':
620 if (!is_alphabet_char(c))
621 return false;
622 break;
623 case '0':
624 case '1':
625 case '2':
626 case '3':
627 if (c == '\\' && is_byte_range(filename + 1)
628 && strncmp(filename + 1, pattern, 3) == 0) {
629 filename += 3;
630 pattern += 2;
631 break;
632 }
633 return false; /* Not matched. */
634 case '*':
635 case '@':
636 for (i = 0; i <= filename_end - filename; i++) {
637 if (file_matches_to_pattern2(filename + i,
638 filename_end,
639 pattern + 1,
640 pattern_end))
641 return true;
642 c = filename[i];
643 if (c == '.' && *pattern == '@')
644 break;
645 if (c != '\\')
646 continue;
647 if (filename[i + 1] == '\\')
648 i++;
649 else if (is_byte_range(filename + i + 1))
650 i += 3;
651 else
652 break; /* Bad pattern. */
653 }
654 return false; /* Not matched. */
655 default:
656 j = 0;
657 c = *pattern;
658 if (c == '$') {
659 while (is_decimal(filename[j]))
660 j++;
661 } else if (c == 'X') {
662 while (is_hexadecimal(filename[j]))
663 j++;
664 } else if (c == 'A') {
665 while (is_alphabet_char(filename[j]))
666 j++;
667 }
668 for (i = 1; i <= j; i++) {
669 if (file_matches_to_pattern2(filename + i,
670 filename_end,
671 pattern + 1,
672 pattern_end))
673 return true;
674 }
675 return false; /* Not matched or bad pattern. */
676 }
677 filename++;
678 pattern++;
679 }
680 while (*pattern == '\\' &&
681 (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
682 pattern += 2;
683 return filename == filename_end && pattern == pattern_end;
684 }
685
686 /**
687 * file_matches_to_pattern - Pattern matching without without '/' character.
688 *
689 * @filename: The start of string to check.
690 * @filename_end: The end of string to check.
691 * @pattern: The start of pattern to compare.
692 * @pattern_end: The end of pattern to compare.
693 *
694 * Returns true if @filename matches @pattern, false otherwise.
695 */
696 static bool file_matches_to_pattern(const char *filename,
697 const char *filename_end,
698 const char *pattern,
699 const char *pattern_end)
700 {
701 const char *pattern_start = pattern;
702 bool first = true;
703 bool result;
704 while (pattern < pattern_end - 1) {
705 /* Split at "\-" pattern. */
706 if (*pattern++ != '\\' || *pattern++ != '-')
707 continue;
708 result = file_matches_to_pattern2(filename, filename_end,
709 pattern_start, pattern - 2);
710 if (first)
711 result = !result;
712 if (result)
713 return false;
714 first = false;
715 pattern_start = pattern;
716 }
717 result = file_matches_to_pattern2(filename, filename_end,
718 pattern_start, pattern_end);
719 return first ? result : !result;
720 }
721
722 /**
723 * ccs_path_matches_pattern - Check whether the given filename matches the given pattern.
724 * @filename: The filename to check.
725 * @pattern: The pattern to compare.
726 *
727 * Returns true if matches, false otherwise.
728 *
729 * The following patterns are available.
730 * \\ \ itself.
731 * \ooo Octal representation of a byte.
732 * \* More than or equals to 0 character other than '/'.
733 * \@ More than or equals to 0 character other than '/' or '.'.
734 * \? 1 byte character other than '/'.
735 * \$ More than or equals to 1 decimal digit.
736 * \+ 1 decimal digit.
737 * \X More than or equals to 1 hexadecimal digit.
738 * \x 1 hexadecimal digit.
739 * \A More than or equals to 1 alphabet character.
740 * \a 1 alphabet character.
741 * \- Subtraction operator.
742 */
743 bool ccs_path_matches_pattern(const struct path_info *filename,
744 const struct path_info *pattern)
745 {
746 /*
747 if (!filename || !pattern)
748 return false;
749 */
750 const char *f = filename->name;
751 const char *p = pattern->name;
752 const int len = pattern->const_len;
753 /* If @pattern doesn't contain pattern, I can use strcmp(). */
754 if (!pattern->is_patterned)
755 return !ccs_pathcmp(filename, pattern);
756 /* Dont compare if the number of '/' differs. */
757 if (filename->depth != pattern->depth)
758 return false;
759 /* Compare the initial length without patterns. */
760 if (strncmp(f, p, len))
761 return false;
762 f += len;
763 p += len;
764 /* Main loop. Compare each directory component. */
765 while (*f && *p) {
766 const char *f_delimiter = strchr(f, '/');
767 const char *p_delimiter = strchr(p, '/');
768 if (!f_delimiter)
769 f_delimiter = f + strlen(f);
770 if (!p_delimiter)
771 p_delimiter = p + strlen(p);
772 if (!file_matches_to_pattern(f, f_delimiter, p, p_delimiter))
773 return false;
774 f = f_delimiter;
775 if (*f)
776 f++;
777 p = p_delimiter;
778 if (*p)
779 p++;
780 }
781 /* Ignore trailing "\*" and "\@" in @pattern. */
782 while (*p == '\\' &&
783 (*(p + 1) == '*' || *(p + 1) == '@'))
784 p += 2;
785 return !*f && !*p;
786 }
787
788 /**
789 * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
790 *
791 * @head: Pointer to "struct ccs_io_buffer".
792 * @fmt: The printf()'s format string, followed by parameters.
793 *
794 * Returns true on success, false otherwise.
795 *
796 * The snprintf() will truncate, but ccs_io_printf() won't.
797 */
798 bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
799 {
800 va_list args;
801 int len;
802 int pos = head->read_avail;
803 int size = head->readbuf_size - pos;
804 if (size <= 0)
805 return false;
806 va_start(args, fmt);
807 len = vsnprintf(head->read_buf + pos, size, fmt, args);
808 va_end(args);
809 if (pos + len >= head->readbuf_size)
810 return false;
811 head->read_avail += len;
812 return true;
813 }
814
815 /**
816 * ccs_get_exe - Get ccs_realpath() of current process.
817 *
818 * Returns the ccs_realpath() of current process on success, NULL otherwise.
819 *
820 * This function uses ccs_alloc(), so the caller must ccs_free()
821 * if this function didn't return NULL.
822 */
823 const char *ccs_get_exe(void)
824 {
825 struct mm_struct *mm = current->mm;
826 struct vm_area_struct *vma;
827 const char *cp = NULL;
828 if (!mm)
829 return NULL;
830 down_read(&mm->mmap_sem);
831 for (vma = mm->mmap; vma; vma = vma->vm_next) {
832 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
833 cp = ccs_realpath_from_dentry(vma->vm_file->f_dentry,
834 vma->vm_file->f_vfsmnt);
835 break;
836 }
837 }
838 up_read(&mm->mmap_sem);
839 return cp;
840 }
841
842 /**
843 * ccs_get_msg - Get warning message.
844 *
845 * @is_enforce: Is it enforcing mode?
846 *
847 * Returns "ERROR" or "WARNING".
848 */
849 const char *ccs_get_msg(const bool is_enforce)
850 {
851 if (is_enforce)
852 return "ERROR";
853 else
854 return "WARNING";
855 }
856
857 /**
858 * ccs_can_sleep - Check whether it is permitted to do operations that may sleep.
859 *
860 * Returns true if it is permitted to do operations that may sleep,
861 * false otherwise.
862 *
863 * TOMOYO Linux supports interactive enforcement that lets processes
864 * wait for the administrator's decision.
865 * All hooks but the one for ccs_may_autobind() are inserted where
866 * it is permitted to do operations that may sleep.
867 * Thus, this warning should not happen.
868 */
869 bool ccs_can_sleep(void)
870 {
871 static u8 count = 20;
872 if (likely(!in_interrupt()))
873 return true;
874 if (count) {
875 count--;
876 printk(KERN_ERR "BUG: sleeping function called "
877 "from invalid context.\n");
878 dump_stack();
879 }
880 return false;
881 }
882
883 /**
884 * ccs_check_flags - Check mode for specified functionality.
885 *
886 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
887 * @index: The functionality to check mode.
888 *
889 * Returns the mode of specified functionality.
890 */
891 unsigned int ccs_check_flags(const struct domain_info *domain, const u8 index)
892 {
893 u8 profile;
894 if (!domain)
895 domain = current->domain_info;
896 profile = domain->profile;
897 return sbin_init_started && index < CCS_MAX_CONTROL_INDEX
898 #if MAX_PROFILES != 256
899 && profile < MAX_PROFILES
900 #endif
901 && profile_ptr[profile] ?
902 profile_ptr[profile]->value[index] : 0;
903 }
904
905 #ifdef CONFIG_TOMOYO
906 /**
907 * ccs_check_capability_flags - Check mode for specified capability.
908 *
909 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
910 * @index: The capability to check mode.
911 *
912 * Returns the mode of specified capability.
913 */
914 static u8 ccs_check_capability_flags(const struct domain_info *domain,
915 const u8 index)
916 {
917 const u8 profile = domain ? domain->profile :
918 current->domain_info->profile;
919 return sbin_init_started && index < TOMOYO_MAX_CAPABILITY_INDEX
920 #if MAX_PROFILES != 256
921 && profile < MAX_PROFILES
922 #endif
923 && profile_ptr[profile] ?
924 profile_ptr[profile]->capability_value[index] : 0;
925 }
926
927 /**
928 * ccs_cap2keyword - Convert capability operation to capability name.
929 *
930 * @operation: The capability index.
931 *
932 * Returns the name of the specified capability's name.
933 */
934 const char *ccs_cap2keyword(const u8 operation)
935 {
936 return operation < TOMOYO_MAX_CAPABILITY_INDEX
937 ? capability_control_keyword[operation] : NULL;
938 }
939
940 #endif
941
942 /**
943 * ccs_init_request_info - Initialize "struct ccs_request_info" members.
944 *
945 * @r: Pointer to "struct ccs_request_info" to initialize.
946 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
947 * @index: Index number of functionality.
948 */
949 void ccs_init_request_info(struct ccs_request_info *r,
950 struct domain_info *domain, const u8 index)
951 {
952 memset(r, 0, sizeof(*r));
953 if (!domain)
954 domain = current->domain_info;
955 r->domain = domain;
956 r->profile = domain->profile;
957 if (index < CCS_MAX_CONTROL_INDEX)
958 r->mode = ccs_check_flags(domain, index);
959 #ifdef CONFIG_TOMOYO
960 else
961 r->mode = ccs_check_capability_flags(domain, index
962 - CCS_MAX_CONTROL_INDEX);
963 #endif
964 }
965
966 /**
967 * ccs_verbose_mode - Check whether TOMOYO is verbose mode.
968 *
969 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
970 *
971 * Returns true if domain policy violation warning should be printed to
972 * console.
973 */
974 bool ccs_verbose_mode(const struct domain_info *domain)
975 {
976 return ccs_check_flags(domain, CCS_TOMOYO_VERBOSE) != 0;
977 }
978
979 /**
980 * ccs_check_domain_quota - Check for domain's quota.
981 *
982 * @domain: Pointer to "struct domain_info".
983 *
984 * Returns true if the domain is not exceeded quota, false otherwise.
985 */
986 bool ccs_check_domain_quota(struct domain_info * const domain)
987 {
988 unsigned int count = 0;
989 struct acl_info *ptr;
990 if (!domain)
991 return true;
992 list1_for_each_entry(ptr, &domain->acl_info_list, list) {
993 if (ptr->type & ACL_DELETED)
994 continue;
995 switch (ccs_acl_type2(ptr)) {
996 struct single_path_acl_record *acl1;
997 struct double_path_acl_record *acl2;
998 u16 perm;
999 case TYPE_SINGLE_PATH_ACL:
1000 acl1 = container_of(ptr, struct single_path_acl_record,
1001 head);
1002 perm = acl1->perm;
1003 if (perm & (1 << TYPE_EXECUTE_ACL))
1004 count++;
1005 if (perm &
1006 ((1 << TYPE_READ_ACL) | (1 << TYPE_WRITE_ACL)))
1007 count++;
1008 if (perm & (1 << TYPE_CREATE_ACL))
1009 count++;
1010 if (perm & (1 << TYPE_UNLINK_ACL))
1011 count++;
1012 if (perm & (1 << TYPE_MKDIR_ACL))
1013 count++;
1014 if (perm & (1 << TYPE_RMDIR_ACL))
1015 count++;
1016 if (perm & (1 << TYPE_MKFIFO_ACL))
1017 count++;
1018 if (perm & (1 << TYPE_MKSOCK_ACL))
1019 count++;
1020 if (perm & (1 << TYPE_MKBLOCK_ACL))
1021 count++;
1022 if (perm & (1 << TYPE_MKCHAR_ACL))
1023 count++;
1024 if (perm & (1 << TYPE_TRUNCATE_ACL))
1025 count++;
1026 if (perm & (1 << TYPE_SYMLINK_ACL))
1027 count++;
1028 if (perm & (1 << TYPE_REWRITE_ACL))
1029 count++;
1030 break;
1031 case TYPE_DOUBLE_PATH_ACL:
1032 acl2 = container_of(ptr, struct double_path_acl_record,
1033 head);
1034 perm = acl2->perm;
1035 if (perm & (1 << TYPE_LINK_ACL))
1036 count++;
1037 if (perm & (1 << TYPE_RENAME_ACL))
1038 count++;
1039 break;
1040 case TYPE_EXECUTE_HANDLER:
1041 case TYPE_DENIED_EXECUTE_HANDLER:
1042 break;
1043 default:
1044 count++;
1045 }
1046 }
1047 if (count < ccs_check_flags(domain, CCS_TOMOYO_MAX_ACCEPT_ENTRY))
1048 return true;
1049 if (!domain->quota_warned) {
1050 domain->quota_warned = true;
1051 printk(KERN_WARNING "TOMOYO-WARNING: "
1052 "Domain '%s' has so many ACLs to hold. "
1053 "Stopped learning mode.\n", domain->domainname->name);
1054 }
1055 return false;
1056 }
1057
1058 /**
1059 * ccs_find_or_assign_new_profile - Create a new profile.
1060 *
1061 * @profile: Profile number to create.
1062 *
1063 * Returns pointer to "struct profile" on success, NULL otherwise.
1064 */
1065 static struct profile *ccs_find_or_assign_new_profile(const unsigned int
1066 profile)
1067 {
1068 static DEFINE_MUTEX(lock);
1069 struct profile *ptr = NULL;
1070 mutex_lock(&lock);
1071 if (profile < MAX_PROFILES) {
1072 ptr = profile_ptr[profile];
1073 if (ptr)
1074 goto ok;
1075 ptr = ccs_alloc_element(sizeof(*ptr));
1076 if (ptr) {
1077 int i;
1078 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)
1079 ptr->value[i]
1080 = ccs_control_array[i].current_value;
1081 /*
1082 * Needn't to initialize "ptr->capability_value"
1083 * because they are always 0.
1084 */
1085 mb(); /* Avoid out-of-order execution. */
1086 profile_ptr[profile] = ptr;
1087 }
1088 }
1089 ok:
1090 mutex_unlock(&lock);
1091 return ptr;
1092 }
1093
1094 /**
1095 * write_profile - Write profile table.
1096 *
1097 * @head: Pointer to "struct ccs_io_buffer".
1098 *
1099 * Returns 0 on success, negative value otherwise.
1100 */
1101 static int write_profile(struct ccs_io_buffer *head)
1102 {
1103 char *data = head->write_buf;
1104 unsigned int i;
1105 unsigned int value;
1106 char *cp;
1107 struct profile *profile;
1108 i = simple_strtoul(data, &cp, 10);
1109 if (data != cp) {
1110 if (*cp != '-')
1111 return -EINVAL;
1112 data = cp + 1;
1113 }
1114 profile = ccs_find_or_assign_new_profile(i);
1115 if (!profile)
1116 return -EINVAL;
1117 cp = strchr(data, '=');
1118 if (!cp)
1119 return -EINVAL;
1120 *cp = '\0';
1121 ccs_update_counter(CCS_UPDATES_COUNTER_PROFILE);
1122 if (!strcmp(data, "COMMENT")) {
1123 profile->comment = ccs_save_name(cp + 1);
1124 return 0;
1125 }
1126 #ifdef CONFIG_TOMOYO
1127 if (str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {
1128 if (sscanf(cp + 1, "%u", &value) != 1) {
1129 for (i = 0; i < 4; i++) {
1130 if (strcmp(cp + 1, mode_4[i]))
1131 continue;
1132 value = i;
1133 break;
1134 }
1135 if (i == 4)
1136 return -EINVAL;
1137 }
1138 if (value > 3)
1139 value = 3;
1140 for (i = 0; i < TOMOYO_MAX_CAPABILITY_INDEX; i++) {
1141 if (strcmp(data, capability_control_keyword[i]))
1142 continue;
1143 profile->capability_value[i] = value;
1144 return 0;
1145 }
1146 return -EINVAL;
1147 }
1148 #endif
1149 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
1150 if (strcmp(data, ccs_control_array[i].keyword))
1151 continue;
1152 if (sscanf(cp + 1, "%u", &value) != 1) {
1153 int j;
1154 const char **modes;
1155 switch (i) {
1156 case CCS_SAKURA_RESTRICT_AUTOBIND:
1157 case CCS_TOMOYO_VERBOSE:
1158 modes = mode_2;
1159 break;
1160 default:
1161 modes = mode_4;
1162 break;
1163 }
1164 for (j = 0; j < 4; j++) {
1165 if (strcmp(cp + 1, modes[j]))
1166 continue;
1167 value = j;
1168 break;
1169 }
1170 if (j == 4)
1171 return -EINVAL;
1172 } else if (value > ccs_control_array[i].max_value) {
1173 value = ccs_control_array[i].max_value;
1174 }
1175 switch (i) {
1176 case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1177 case CCS_SAKURA_RESTRICT_UNMOUNT:
1178 if (value == 1)
1179 value = 2; /* learning mode is not supported. */
1180 }
1181 profile->value[i] = value;
1182 return 0;
1183 }
1184 return -EINVAL;
1185 }
1186
1187 /**
1188 * read_profile - Read profile table.
1189 *
1190 * @head: Pointer to "struct ccs_io_buffer".
1191 *
1192 * Returns 0.
1193 */
1194 static int read_profile(struct ccs_io_buffer *head)
1195 {
1196 static const int total
1197 = CCS_MAX_CONTROL_INDEX + TOMOYO_MAX_CAPABILITY_INDEX + 1;
1198 int step;
1199 if (head->read_eof)
1200 return 0;
1201 for (step = head->read_step; step < MAX_PROFILES * total; step++) {
1202 const u8 index = step / total;
1203 u8 type = step % total;
1204 const struct profile *profile = profile_ptr[index];
1205 head->read_step = step;
1206 if (!profile)
1207 continue;
1208 #if !defined(CONFIG_SAKURA) || !defined(CONFIG_TOMOYO)
1209 switch (type) {
1210 #ifndef CONFIG_SAKURA
1211 case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1212 case CCS_SAKURA_RESTRICT_CHROOT:
1213 case CCS_SAKURA_RESTRICT_MOUNT:
1214 case CCS_SAKURA_RESTRICT_UNMOUNT:
1215 case CCS_SAKURA_RESTRICT_PIVOT_ROOT:
1216 case CCS_SAKURA_RESTRICT_AUTOBIND:
1217 #endif
1218 #ifndef CONFIG_TOMOYO
1219 case CCS_TOMOYO_MAC_FOR_FILE:
1220 case CCS_TOMOYO_MAC_FOR_ARGV0:
1221 case CCS_TOMOYO_MAC_FOR_ENV:
1222 case CCS_TOMOYO_MAC_FOR_NETWORK:
1223 case CCS_TOMOYO_MAC_FOR_SIGNAL:
1224 case CCS_TOMOYO_MAX_ACCEPT_ENTRY:
1225 case CCS_TOMOYO_MAX_GRANT_LOG:
1226 case CCS_TOMOYO_MAX_REJECT_LOG:
1227 case CCS_TOMOYO_VERBOSE:
1228 #endif
1229 continue;
1230 }
1231 #endif
1232 if (!type) { /* Print profile' comment tag. */
1233 if (!ccs_io_printf(head, "%u-COMMENT=%s\n",
1234 index, profile->comment ?
1235 profile->comment->name : ""))
1236 break;
1237 continue;
1238 }
1239 type--;
1240 if (type >= CCS_MAX_CONTROL_INDEX) {
1241 #ifdef CONFIG_TOMOYO
1242 const int i = type - CCS_MAX_CONTROL_INDEX;
1243 const u8 value = profile->capability_value[i];
1244 if (!ccs_io_printf(head,
1245 "%u-" KEYWORD_MAC_FOR_CAPABILITY
1246 "%s=%s\n", index,
1247 capability_control_keyword[i],
1248 mode_4[value]))
1249 break;
1250 #endif
1251 } else {
1252 const unsigned int value = profile->value[type];
1253 const char **modes = NULL;
1254 const char *keyword = ccs_control_array[type].keyword;
1255 switch (ccs_control_array[type].max_value) {
1256 case 3:
1257 modes = mode_4;
1258 break;
1259 case 1:
1260 modes = mode_2;
1261 break;
1262 }
1263 if (modes) {
1264 if (!ccs_io_printf(head, "%u-%s=%s\n", index,
1265 keyword, modes[value]))
1266 break;
1267 } else {
1268 if (!ccs_io_printf(head, "%u-%s=%u\n", index,
1269 keyword, value))
1270 break;
1271 }
1272 }
1273 }
1274 if (step == MAX_PROFILES * total)
1275 head->read_eof = true;
1276 return 0;
1277 }
1278
1279 /* Structure for policy manager. */
1280 struct policy_manager_entry {
1281 struct list1_head list;
1282 /* A path to program or a domainname. */
1283 const struct path_info *manager;
1284 bool is_domain; /* True if manager is a domainname. */
1285 bool is_deleted; /* True if this entry is deleted. */
1286 };
1287
1288 /* The list for "struct policy_manager_entry". */
1289 static LIST1_HEAD(policy_manager_list);
1290
1291 /**
1292 * update_manager_entry - Add a manager entry.
1293 *
1294 * @manager: The path to manager or the domainnamme.
1295 * @is_delete: True if it is a delete request.
1296 *
1297 * Returns 0 on success, negative value otherwise.
1298 */
1299 static int update_manager_entry(const char *manager, const bool is_delete)
1300 {
1301 struct policy_manager_entry *new_entry;
1302 struct policy_manager_entry *ptr;
1303 static DEFINE_MUTEX(lock);
1304 const struct path_info *saved_manager;
1305 int error = -ENOMEM;
1306 bool is_domain = false;
1307 if (ccs_is_domain_def(manager)) {
1308 if (!ccs_is_correct_domain(manager, __func__))
1309 return -EINVAL;
1310 is_domain = true;
1311 } else {
1312 if (!ccs_is_correct_path(manager, 1, -1, -1, __func__))
1313 return -EINVAL;
1314 }
1315 saved_manager = ccs_save_name(manager);
1316 if (!saved_manager)
1317 return -ENOMEM;
1318 mutex_lock(&lock);
1319 list1_for_each_entry(ptr, &policy_manager_list, list) {
1320 if (ptr->manager != saved_manager)
1321 continue;
1322 ptr->is_deleted = is_delete;
1323 error = 0;
1324 goto out;
1325 }
1326 if (is_delete) {
1327 error = -ENOENT;
1328 goto out;
1329 }
1330 new_entry = ccs_alloc_element(sizeof(*new_entry));
1331 if (!new_entry)
1332 goto out;
1333 new_entry->manager = saved_manager;
1334 new_entry->is_domain = is_domain;
1335 list1_add_tail_mb(&new_entry->list, &policy_manager_list);
1336 error = 0;
1337 out:
1338 mutex_unlock(&lock);
1339 if (!error)
1340 ccs_update_counter(CCS_UPDATES_COUNTER_MANAGER);
1341 return error;
1342 }
1343
1344 /**
1345 * write_manager_policy - Write manager policy.
1346 *
1347 * @head: Pointer to "struct ccs_io_buffer".
1348 *
1349 * Returns 0 on success, negative value otherwise.
1350 */
1351 static int write_manager_policy(struct ccs_io_buffer *head)
1352 {
1353 char *data = head->write_buf;
1354 bool is_delete = str_starts(&data, KEYWORD_DELETE);
1355 if (!strcmp(data, "manage_by_non_root")) {
1356 manage_by_non_root = !is_delete;
1357 return 0;
1358 }
1359 return update_manager_entry(data, is_delete);
1360 }
1361
1362 /**
1363 * read_manager_policy - Read manager policy.
1364 *
1365 * @head: Pointer to "struct ccs_io_buffer".
1366 *
1367 * Returns 0.
1368 */
1369 static int read_manager_policy(struct ccs_io_buffer *head)
1370 {
1371 struct list1_head *pos;
1372 if (head->read_eof)
1373 return 0;
1374 list1_for_each_cookie(pos, head->read_var2, &policy_manager_list) {
1375 struct policy_manager_entry *ptr;
1376 ptr = list1_entry(pos, struct policy_manager_entry, list);
1377 if (ptr->is_deleted)
1378 continue;
1379 if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
1380 return 0;
1381 }
1382 head->read_eof = true;
1383 return 0;
1384 }
1385
1386 /**
1387 * is_policy_manager - Check whether the current process is a policy manager.
1388 *
1389 * Returns true if the current process is permitted to modify policy
1390 * via /proc/ccs/ interface.
1391 */
1392 static bool is_policy_manager(void)
1393 {
1394 struct policy_manager_entry *ptr;
1395 const char *exe;
1396 struct task_struct *task = current;
1397 const struct path_info *domainname = task->domain_info->domainname;
1398 bool found = false;
1399 if (!sbin_init_started)
1400 return true;
1401 if (task->tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER)
1402 return true;
1403 if (!manage_by_non_root && (task->uid || task->euid))
1404 return false;
1405 list1_for_each_entry(ptr, &policy_manager_list, list) {
1406 if (!ptr->is_deleted && ptr->is_domain
1407 && !ccs_pathcmp(domainname, ptr->manager)) {
1408 /* Set manager flag. */
1409 task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER;
1410 return true;
1411 }
1412 }
1413 exe = ccs_get_exe();
1414 if (!exe)
1415 return false;
1416 list1_for_each_entry(ptr, &policy_manager_list, list) {
1417 if (!ptr->is_deleted && !ptr->is_domain
1418 && !strcmp(exe, ptr->manager->name)) {
1419 found = true;
1420 /* Set manager flag. */
1421 task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER;
1422 break;
1423 }
1424 }
1425 if (!found) { /* Reduce error messages. */
1426 static pid_t last_pid;
1427 const pid_t pid = current->pid;
1428 if (last_pid != pid) {
1429 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1430 "update policies.\n", domainname->name, exe);
1431 last_pid = pid;
1432 }
1433 }
1434 ccs_free(exe);
1435 return found;
1436 }
1437
1438 #ifdef CONFIG_TOMOYO
1439
1440 /**
1441 * ccs_find_condition_part - Find condition part from the statement.
1442 *
1443 * @data: String to parse.
1444 *
1445 * Returns pointer to the condition part if it was found in the statement,
1446 * NULL otherwise.
1447 */
1448 static char *ccs_find_condition_part(char *data)
1449 {
1450 char *cp = strstr(data, " if ");
1451 if (cp) {
1452 while (1) {
1453 char *cp2 = strstr(cp + 3, " if ");
1454 if (!cp2)
1455 break;
1456 cp = cp2;
1457 }
1458 *cp++ = '\0';
1459 } else {
1460 cp = strstr(data, " ; set ");
1461 if (cp)
1462 *cp++ = '\0';
1463 }
1464 return cp;
1465 }
1466
1467 /**
1468 * is_select_one - Parse select command.
1469 *
1470 * @head: Pointer to "struct ccs_io_buffer".
1471 * @data: String to parse.
1472 *
1473 * Returns true on success, false otherwise.
1474 */
1475 static bool is_select_one(struct ccs_io_buffer *head, const char *data)
1476 {
1477 unsigned int pid;
1478 struct domain_info *domain = NULL;
1479 if (!strcmp(data, "allow_execute")) {
1480 head->read_execute_only = true;
1481 return true;
1482 }
1483 if (sscanf(data, "pid=%u", &pid) == 1) {
1484 struct task_struct *p;
1485 /***** CRITICAL SECTION START *****/
1486 read_lock(&tasklist_lock);
1487 p = find_task_by_pid(pid);
1488 if (p)
1489 domain = p->domain_info;
1490 read_unlock(&tasklist_lock);
1491 /***** CRITICAL SECTION END *****/
1492 } else if (!strncmp(data, "domain=", 7)) {
1493 if (ccs_is_domain_def(data + 7))
1494 domain = ccs_find_domain(data + 7);
1495 } else
1496 return false;
1497 head->write_var1 = domain;
1498 /* Accessing read_buf is safe because head->io_sem is held. */
1499 if (!head->read_buf)
1500 return true; /* Do nothing if open(O_WRONLY). */
1501 head->read_avail = 0;
1502 ccs_io_printf(head, "# select %s\n", data);
1503 head->read_single_domain = true;
1504 head->read_eof = !domain;
1505 if (domain) {
1506 struct domain_info *d;
1507 head->read_var1 = NULL;
1508 list1_for_each_entry(d, &domain_list, list) {
1509 if (d == domain)
1510 break;
1511 head->read_var1 = &d->list;
1512 }
1513 head->read_var2 = NULL;
1514 head->read_bit = 0;
1515 head->read_step = 0;
1516 if (domain->is_deleted)
1517 ccs_io_printf(head, "# This is a deleted domain.\n");
1518 }
1519 return true;
1520 }
1521
1522 /**
1523 * write_domain_policy - Write domain policy.
1524 *
1525 * @head: Pointer to "struct ccs_io_buffer".
1526 *
1527 * Returns 0 on success, negative value otherwise.
1528 */
1529 static int write_domain_policy(struct ccs_io_buffer *head)
1530 {
1531 char *data = head->write_buf;
1532 struct domain_info *domain = head->write_var1;
1533 bool is_delete = false;
1534 bool is_select = false;
1535 bool is_undelete = false;
1536 unsigned int profile;
1537 const struct condition_list *cond = NULL;
1538 char *cp;
1539 if (str_starts(&data, KEYWORD_DELETE))
1540 is_delete = true;
1541 else if (str_starts(&data, KEYWORD_SELECT))
1542 is_select = true;
1543 else if (str_starts(&data, KEYWORD_UNDELETE))
1544 is_undelete = true;
1545 if (is_select && is_select_one(head, data))
1546 return 0;
1547 /* Don't allow updating policies by non manager programs. */
1548 if (!is_policy_manager())
1549 return -EPERM;
1550 if (ccs_is_domain_def(data)) {
1551 domain = NULL;
1552 if (is_delete)
1553 ccs_delete_domain(data);
1554 else if (is_select)
1555 domain = ccs_find_domain(data);
1556 else if (is_undelete)
1557 domain = ccs_undelete_domain(data);
1558 else
1559 domain = ccs_find_or_assign_new_domain(data, 0);
1560 head->write_var1 = domain;
1561 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1562 return 0;
1563 }
1564 if (!domain)
1565 return -EINVAL;
1566
1567 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1568 && profile < MAX_PROFILES) {
1569 if (profile_ptr[profile] || !sbin_init_started)
1570 domain->profile = (u8) profile;
1571 return 0;
1572 }
1573 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1574 ccs_set_domain_flag(domain, is_delete,
1575 DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1576 return 0;
1577 }
1578 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1579 ccs_set_domain_flag(domain, is_delete,
1580 DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV);
1581 return 0;
1582 }
1583 cp = ccs_find_condition_part(data);
1584 if (cp) {
1585 cond = ccs_find_or_assign_new_condition(cp);
1586 if (!cond)
1587 return -EINVAL;
1588 }
1589 if (str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1590 return ccs_write_capability_policy(data, domain, cond,
1591 is_delete);
1592 else if (str_starts(&data, KEYWORD_ALLOW_NETWORK))
1593 return ccs_write_network_policy(data, domain, cond, is_delete);
1594 else if (str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1595 return ccs_write_signal_policy(data, domain, cond, is_delete);
1596 else if (str_starts(&data, KEYWORD_ALLOW_ARGV0))
1597 return ccs_write_argv0_policy(data, domain, cond, is_delete);
1598 else if (str_starts(&data, KEYWORD_ALLOW_ENV))
1599 return ccs_write_env_policy(data, domain, cond, is_delete);
1600 else
1601 return ccs_write_file_policy(data, domain, cond, is_delete);
1602 }
1603
1604 /**
1605 * print_single_path_acl - Print a single path ACL entry.
1606 *
1607 * @head: Pointer to "struct ccs_io_buffer".
1608 * @ptr: Pointer to "struct single_path_acl_record".
1609 * @cond: Pointer to "struct condition_list". May be NULL.
1610 *
1611 * Returns true on success, false otherwise.
1612 */
1613 static bool print_single_path_acl(struct ccs_io_buffer *head,
1614 struct single_path_acl_record *ptr,
1615 const struct condition_list *cond)
1616 {
1617 int pos;
1618 u8 bit;
1619 const char *atmark = "";
1620 const char *filename;
1621 const u16 perm = ptr->perm;
1622 if (ptr->u_is_group) {
1623 atmark = "@";
1624 filename = ptr->u.group->group_name->name;
1625 } else {
1626 filename = ptr->u.filename->name;
1627 }
1628 for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1629 const char *msg;
1630 if (!(perm & (1 << bit)))
1631 continue;
1632 if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)
1633 continue;
1634 /* Print "read/write" instead of "read" and "write". */
1635 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1636 && (perm & (1 << TYPE_READ_WRITE_ACL)))
1637 continue;
1638 msg = ccs_sp2keyword(bit);
1639 pos = head->read_avail;
1640 if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1641 atmark, filename) ||
1642 !ccs_print_condition(head, cond))
1643 goto out;
1644 }
1645 head->read_bit = 0;
1646 return true;
1647 out:
1648 head->read_bit = bit;
1649 head->read_avail = pos;
1650 return false;
1651 }
1652
1653 /**
1654 * print_double_path_acl - Print a double path ACL entry.
1655 *
1656 * @head: Pointer to "struct ccs_io_buffer".
1657 * @ptr: Pointer to "struct double_path_acl_record".
1658 * @cond: Pointer to "struct condition_list". May be NULL.
1659 *
1660 * Returns true on success, false otherwise.
1661 */
1662 static bool print_double_path_acl(struct ccs_io_buffer *head,
1663 struct double_path_acl_record *ptr,
1664 const struct condition_list *cond)
1665 {
1666 int pos;
1667 const char *atmark1 = "";
1668 const char *atmark2 = "";
1669 const char *filename1;
1670 const char *filename2;
1671 const u8 perm = ptr->perm;
1672 u8 bit;
1673 if (ptr->u1_is_group) {
1674 atmark1 = "@";
1675 filename1 = ptr->u1.group1->group_name->name;
1676 } else {
1677 filename1 = ptr->u1.filename1->name;
1678 }
1679 if (ptr->u2_is_group) {
1680 atmark2 = "@";
1681 filename2 = ptr->u2.group2->group_name->name;
1682 } else {
1683 filename2 = ptr->u2.filename2->name;
1684 }
1685 for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1686 const char *msg;
1687 if (!(perm & (1 << bit)))
1688 continue;
1689 msg = ccs_dp2keyword(bit);
1690 pos = head->read_avail;
1691 if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1692 atmark1, filename1, atmark2, filename2) ||
1693 !ccs_print_condition(head, cond))
1694 goto out;
1695 }
1696 head->read_bit = 0;
1697 return true;
1698 out:
1699 head->read_bit = bit;
1700 head->read_avail = pos;
1701 return false;
1702 }
1703
1704 /**
1705 * print_argv0_acl - Print an argv[0] ACL entry.
1706 *
1707 * @head: Pointer to "struct ccs_io_buffer".
1708 * @ptr: Pointer to "struct argv0_acl_record".
1709 * @cond: Pointer to "struct condition_list". May be NULL.
1710 *
1711 * Returns true on success, false otherwise.
1712 */
1713 static bool print_argv0_acl(struct ccs_io_buffer *head,
1714 struct argv0_acl_record *ptr,
1715 const struct condition_list *cond)
1716 {
1717 int pos = head->read_avail;
1718 if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1719 ptr->filename->name, ptr->argv0->name))
1720 goto out;
1721 if (!ccs_print_condition(head, cond))
1722 goto out;
1723 return true;
1724 out:
1725 head->read_avail = pos;
1726 return false;
1727 }
1728
1729 /**
1730 * print_env_acl - Print an evironment variable name's ACL entry.
1731 *
1732 * @head: Pointer to "struct ccs_io_buffer".
1733 * @ptr: Pointer to "struct env_acl_record".
1734 * @cond: Pointer to "struct condition_list". May be NULL.
1735 *
1736 * Returns true on success, false otherwise.
1737 */
1738 static bool print_env_acl(struct ccs_io_buffer *head,
1739 struct env_acl_record *ptr,
1740 const struct condition_list *cond)
1741 {
1742 int pos = head->read_avail;
1743 if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1744 goto out;
1745 if (!ccs_print_condition(head, cond))
1746 goto out;
1747 return true;
1748 out:
1749 head->read_avail = pos;
1750 return false;
1751 }
1752
1753 /**
1754 * print_capability_acl - Print a capability ACL entry.
1755 *
1756 * @head: Pointer to "struct ccs_io_buffer".
1757 * @ptr: Pointer to "struct capability_acl_record".
1758 * @cond: Pointer to "struct condition_list". May be NULL.
1759 *
1760 * Returns true on success, false otherwise.
1761 */
1762 static bool print_capability_acl(struct ccs_io_buffer *head,
1763 struct capability_acl_record *ptr,
1764 const struct condition_list *cond)
1765 {
1766 int pos = head->read_avail;
1767 if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1768 ccs_cap2keyword(ptr->operation)))
1769 goto out;
1770 if (!ccs_print_condition(head, cond))
1771 goto out;
1772 return true;
1773 out:
1774 head->read_avail = pos;
1775 return false;
1776 }
1777
1778 /**
1779 * print_ipv4_entry - Print IPv4 address of a network ACL entry.
1780 *
1781 * @head: Pointer to "struct ccs_io_buffer".
1782 * @ptr: Pointer to "struct ip_network_acl_record".
1783 *
1784 * Returns true on success, false otherwise.
1785 */
1786 static bool print_ipv4_entry(struct ccs_io_buffer *head,
1787 struct ip_network_acl_record *ptr)
1788 {
1789 const u32 min_address = ptr->u.ipv4.min;
1790 const u32 max_address = ptr->u.ipv4.max;
1791 if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1792 return false;
1793 if (min_address != max_address
1794 && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1795 return false;
1796 return true;
1797 }
1798
1799 /**
1800 * print_ipv6_entry - Print IPv6 address of a network ACL entry.
1801 *
1802 * @head: Pointer to "struct ccs_io_buffer".
1803 * @ptr: Pointer to "struct ip_network_acl_record".
1804 *
1805 * Returns true on success, false otherwise.
1806 */
1807 static bool print_ipv6_entry(struct ccs_io_buffer *head,
1808 struct ip_network_acl_record *ptr)
1809 {
1810 char buf[64];
1811 const struct in6_addr *min_address = ptr->u.ipv6.min;
1812 const struct in6_addr *max_address = ptr->u.ipv6.max;
1813 ccs_print_ipv6(buf, sizeof(buf), min_address);
1814 if (!ccs_io_printf(head, "%s", buf))
1815 return false;
1816 if (min_address != max_address) {
1817 ccs_print_ipv6(buf, sizeof(buf), max_address);
1818 if (!ccs_io_printf(head, "-%s", buf))
1819 return false;
1820 }
1821 return true;
1822 }
1823
1824 /**
1825 * print_port_entry - Print port number of a network ACL entry.
1826 *
1827 * @head: Pointer to "struct ccs_io_buffer".
1828 * @ptr: Pointer to "struct ip_network_acl_record".
1829 *
1830 * Returns true on success, false otherwise.
1831 */
1832 static bool print_port_entry(struct ccs_io_buffer *head,
1833 struct ip_network_acl_record *ptr)
1834 {
1835 const u16 min_port = ptr->min_port, max_port = ptr->max_port;
1836 if (!ccs_io_printf(head, " %u", min_port))
1837 return false;
1838 if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1839 return false;
1840 return true;
1841 }
1842
1843 /**
1844 * print_network_acl - Print a network ACL entry.
1845 *
1846 * @head: Pointer to "struct ccs_io_buffer".
1847 * @ptr: Pointer to "struct ip_network_acl_record".
1848 * @cond: Pointer to "struct condition_list". May be NULL.
1849 *
1850 * Returns true on success, false otherwise.
1851 */
1852 static bool print_network_acl(struct ccs_io_buffer *head,
1853 struct ip_network_acl_record *ptr,
1854 const struct condition_list *cond)
1855 {
1856 int pos = head->read_avail;
1857 if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1858 ccs_net2keyword(ptr->operation_type)))
1859 goto out;
1860 switch (ptr->record_type) {
1861 case IP_RECORD_TYPE_ADDRESS_GROUP:
1862 if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1863 goto out;
1864 break;
1865 case IP_RECORD_TYPE_IPv4:
1866 if (!print_ipv4_entry(head, ptr))
1867 goto out;
1868 break;
1869 case IP_RECORD_TYPE_IPv6:
1870 if (!print_ipv6_entry(head, ptr))
1871 goto out;
1872 break;
1873 }
1874 if (!print_port_entry(head, ptr))
1875 goto out;
1876 if (!ccs_print_condition(head, cond))
1877 goto out;
1878 return true;
1879 out:
1880 head->read_avail = pos;
1881 return false;
1882 }
1883
1884 /**
1885 * print_signal_acl - Print a signal ACL entry.
1886 *
1887 * @head: Pointer to "struct ccs_io_buffer".
1888 * @ptr: Pointer to "struct signale_acl_record".
1889 * @cond: Pointer to "struct condition_list". May be NULL.
1890 *
1891 * Returns true on success, false otherwise.
1892 */
1893 static bool print_signal_acl(struct ccs_io_buffer *head,
1894 struct signal_acl_record *ptr,
1895 const struct condition_list *cond)
1896 {
1897 int pos = head->read_avail;
1898 if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
1899 ptr->sig, ptr->domainname->name))
1900 goto out;
1901 if (!ccs_print_condition(head, cond))
1902 goto out;
1903 return true;
1904 out:
1905 head->read_avail = pos;
1906 return false;
1907 }
1908
1909 /**
1910 * print_execute_handler_record - Print an execute handler ACL entry.
1911 *
1912 * @head: Pointer to "struct ccs_io_buffer".
1913 * @keyword: Name of the keyword.
1914 * @ptr: Pointer to "struct execute_handler_record".
1915 *
1916 * Returns true on success, false otherwise.
1917 */
1918 static bool print_execute_handler_record(struct ccs_io_buffer *head,
1919 const char *keyword,
1920 struct execute_handler_record *ptr)
1921 {
1922 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1923 }
1924
1925 /**
1926 * print_entry - Print an ACL entry.
1927 *
1928 * @head: Pointer to "struct ccs_io_buffer".
1929 * @ptr: Pointer to an ACL entry.
1930 *
1931 * Returns true on success, false otherwise.
1932 */
1933 static bool print_entry(struct ccs_io_buffer *head, struct acl_info *ptr)
1934 {
1935 const struct condition_list *cond = ccs_get_condition_part(ptr);
1936 const u8 acl_type = ccs_acl_type2(ptr);
1937 if (acl_type & ACL_DELETED)
1938 return true;
1939 if (acl_type == TYPE_SINGLE_PATH_ACL) {
1940 struct single_path_acl_record *acl
1941 = container_of(ptr, struct single_path_acl_record,
1942 head);
1943 return print_single_path_acl(head, acl, cond);
1944 }
1945 if (acl_type == TYPE_EXECUTE_HANDLER) {
1946 struct execute_handler_record *acl
1947 = container_of(ptr, struct execute_handler_record,
1948 head);
1949 const char *keyword = KEYWORD_EXECUTE_HANDLER;
1950 return print_execute_handler_record(head, keyword, acl);
1951 }
1952 if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {
1953 struct execute_handler_record *acl
1954 = container_of(ptr, struct execute_handler_record,
1955 head);
1956 const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;
1957 return print_execute_handler_record(head, keyword, acl);
1958 }
1959 if (head->read_execute_only)
1960 return true;
1961 if (acl_type == TYPE_DOUBLE_PATH_ACL) {
1962 struct double_path_acl_record *acl
1963 = container_of(ptr, struct double_path_acl_record,
1964 head);
1965 return print_double_path_acl(head, acl, cond);
1966 }
1967 if (acl_type == TYPE_ARGV0_ACL) {
1968 struct argv0_acl_record *acl
1969 = container_of(ptr, struct argv0_acl_record, head);
1970 return print_argv0_acl(head, acl, cond);
1971 }
1972 if (acl_type == TYPE_ENV_ACL) {
1973 struct env_acl_record *acl
1974 = container_of(ptr, struct env_acl_record, head);
1975 return print_env_acl(head, acl, cond);
1976 }
1977 if (acl_type == TYPE_CAPABILITY_ACL) {
1978 struct capability_acl_record *acl
1979 = container_of(ptr, struct capability_acl_record, head);
1980 return print_capability_acl(head, acl, cond);
1981 }
1982 if (acl_type == TYPE_IP_NETWORK_ACL) {
1983 struct ip_network_acl_record *acl
1984 = container_of(ptr, struct ip_network_acl_record, head);
1985 return print_network_acl(head, acl, cond);
1986 }
1987 if (acl_type == TYPE_SIGNAL_ACL) {
1988 struct signal_acl_record *acl
1989 = container_of(ptr, struct signal_acl_record, head);
1990 return print_signal_acl(head, acl, cond);
1991 }
1992 /* Workaround for gcc 3.2.2's inline bug. */
1993 if (acl_type & ACL_DELETED)
1994 return true;
1995 BUG(); /* This must not happen. */
1996 return false;
1997 }
1998
1999 /**
2000 * read_domain_policy - Read domain policy.
2001 *
2002 * @head: Pointer to "struct ccs_io_buffer".
2003 *
2004 * Returns 0.
2005 */
2006 static int read_domain_policy(struct ccs_io_buffer *head)
2007 {
2008 struct list1_head *dpos;
2009 struct list1_head *apos;
2010 if (head->read_eof)
2011 return 0;
2012 if (head->read_step == 0)
2013 head->read_step = 1;
2014 list1_for_each_cookie(dpos, head->read_var1, &domain_list) {
2015 struct domain_info *domain;
2016 const char *quota_exceeded = "";
2017 const char *transition_failed = "";
2018 const char *ignore_global_allow_read = "";
2019 const char *ignore_global_allow_env = "";
2020 domain = list1_entry(dpos, struct domain_info, list);
2021 if (head->read_step != 1)
2022 goto acl_loop;
2023 if (domain->is_deleted && !head->read_single_domain)
2024 continue;
2025 /* Print domainname and flags. */
2026 if (domain->quota_warned)
2027 quota_exceeded = "quota_exceeded\n";
2028 if (domain->flags & DOMAIN_FLAGS_TRANSITION_FAILED)
2029 transition_failed = "transition_failed\n";
2030 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
2031 ignore_global_allow_read
2032 = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
2033 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV)
2034 ignore_global_allow_env
2035 = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
2036 if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
2037 "%s%s%s%s\n", domain->domainname->name,
2038 domain->profile, quota_exceeded,
2039 transition_failed,
2040 ignore_global_allow_read,
2041 ignore_global_allow_env))
2042 return 0;
2043 head->read_step = 2;
2044 acl_loop:
2045 if (head->read_step == 3)
2046 goto tail_mark;
2047 /* Print ACL entries in the domain. */
2048 list1_for_each_cookie(apos, head->read_var2,
2049 &domain->acl_info_list) {
2050 struct acl_info *ptr
2051 = list1_entry(apos, struct acl_info, list);
2052 if (!print_entry(head, ptr))
2053 return 0;
2054 }
2055 head->read_step = 3;
2056 tail_mark:
2057 if (!ccs_io_printf(head, "\n"))
2058 return 0;
2059 head->read_step = 1;
2060 if (head->read_single_domain)
2061 break;
2062 }
2063 head->read_eof = true;
2064 return 0;
2065 }
2066
2067 #endif
2068
2069 /**
2070 * write_domain_profile - Assign profile for specified domain.
2071 *
2072 * @head: Pointer to "struct ccs_io_buffer".
2073 *
2074 * Returns 0 on success, -EINVAL otherwise.
2075 *
2076 * This is equivalent to doing
2077 *
2078 * ( echo "select " $domainname; echo "use_profile " $profile ) |
2079 * /usr/lib/ccs/loadpolicy -d
2080 */
2081 static int write_domain_profile(struct ccs_io_buffer *head)
2082 {
2083 char *data = head->write_buf;
2084 char *cp = strchr(data, ' ');
2085 struct domain_info *domain;
2086 unsigned int profile;
2087 if (!cp)
2088 return -EINVAL;
2089 *cp = '\0';
2090 domain = ccs_find_domain(cp + 1);
2091 profile = simple_strtoul(data, NULL, 10);
2092 if (domain && profile < MAX_PROFILES
2093 && (profile_ptr[profile] || !sbin_init_started))
2094 domain->profile = (u8) profile;
2095 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
2096 return 0;
2097 }
2098
2099 /**
2100 * read_domain_profile - Read only domainname and profile.
2101 *
2102 * @head: Pointer to "struct ccs_io_buffer".
2103 *
2104 * Returns list of profile number and domainname pairs.
2105 *
2106 * This is equivalent to doing
2107 *
2108 * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
2109 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
2110 * domainname = $0; } else if ( $1 == "use_profile" ) {
2111 * print $2 " " domainname; domainname = ""; } } ; '
2112 */
2113 static int read_domain_profile(struct ccs_io_buffer *head)
2114 {
2115 struct list1_head *pos;
2116 if (head->read_eof)
2117 return 0;
2118 list1_for_each_cookie(pos, head->read_var1, &domain_list) {
2119 struct domain_info *domain;
2120 domain = list1_entry(pos, struct domain_info, list);
2121 if (domain->is_deleted)
2122 continue;
2123 if (!ccs_io_printf(head, "%u %s\n", domain->profile,
2124 domain->domainname->name))
2125 return 0;
2126 }
2127 head->read_eof = true;
2128 return 0;
2129 }
2130
2131 /**
2132 * write_pid: Specify PID to obtain domainname.
2133 *
2134 * @head: Pointer to "struct ccs_io_buffer".
2135 *
2136 * Returns 0.
2137 */
2138 static int write_pid(struct ccs_io_buffer *head)
2139 {
2140 head->read_eof = false;
2141 return 0;
2142 }
2143
2144 /**
2145 * read_pid - Read information of a process.
2146 *
2147 * @head: Pointer to "struct ccs_io_buffer".
2148 *
2149 * Returns the domainname which the specified PID is in or
2150 * process information of the specified PID on success,
2151 * empty string otherwise.
2152 */
2153 static int read_pid(struct ccs_io_buffer *head)
2154 {
2155 char *buf = head->write_buf;
2156 bool task_info = false;
2157 unsigned int pid;
2158 struct task_struct *p;
2159 struct domain_info *domain = NULL;
2160 u32 tomoyo_flags = 0;
2161 /* Accessing write_buf is safe because head->io_sem is held. */
2162 if (!buf)
2163 goto done; /* Do nothing if open(O_RDONLY). */
2164 if (head->read_avail || head->read_eof)
2165 goto done;
2166 head->read_eof = true;
2167 if (str_starts(&buf, "info "))
2168 task_info = true;
2169 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
2170 /***** CRITICAL SECTION START *****/
2171 read_lock(&tasklist_lock);
2172 p = find_task_by_pid(pid);
2173 if (p) {
2174 domain = p->domain_info;
2175 tomoyo_flags = p->tomoyo_flags;
2176 }
2177 read_unlock(&tasklist_lock);
2178 /***** CRITICAL SECTION END *****/
2179 if (!domain)
2180 goto done;
2181 if (!task_info)
2182 ccs_io_printf(head, "%u %u %s", pid, domain->profile,
2183 domain->domainname->name);
2184 else
2185 ccs_io_printf(head, "%u manager=%s execute_handler=%s "
2186 "state[0]=%u state[1]=%u state[2]=%u", pid,
2187 tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER ?
2188 "yes" : "no",
2189 tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER ?
2190 "yes" : "no",
2191 (u8) (tomoyo_flags >> 24),
2192 (u8) (tomoyo_flags >> 16),
2193 (u8) (tomoyo_flags >> 8));
2194 done:
2195 return 0;
2196 }
2197
2198 #ifdef CONFIG_TOMOYO
2199
2200 /**
2201 * write_exception_policy - Write exception policy.
2202 *
2203 * @head: Pointer to "struct ccs_io_buffer".
2204 *
2205 * Returns 0 on success, negative value otherwise.
2206 */
2207 static int write_exception_policy(struct ccs_io_buffer *head)
2208 {
2209 char *data = head->write_buf;
2210 bool is_delete = str_starts(&data, KEYWORD_DELETE);
2211 if (str_starts(&data, KEYWORD_KEEP_DOMAIN))
2212 return ccs_write_domain_keeper_policy(data, false, is_delete);
2213 if (str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))
2214 return ccs_write_domain_keeper_policy(data, true, is_delete);
2215 if (str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))
2216 return ccs_write_domain_initializer_policy(data, false,
2217 is_delete);
2218 if (str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))
2219 return ccs_write_domain_initializer_policy(data, true,
2220 is_delete);
2221 if (str_starts(&data, KEYWORD_ALIAS))
2222 return ccs_write_alias_policy(data, is_delete);
2223 if (str_starts(&data, KEYWORD_AGGREGATOR))
2224 return ccs_write_aggregator_policy(data, is_delete);
2225 if (str_starts(&data, KEYWORD_ALLOW_READ))
2226 return ccs_write_globally_readable_policy(data, is_delete);
2227 if (str_starts(&data, KEYWORD_ALLOW_ENV))
2228 return ccs_write_globally_usable_env_policy(data, is_delete);
2229 if (str_starts(&data, KEYWORD_FILE_PATTERN))
2230 return ccs_write_pattern_policy(data, is_delete);
2231 if (str_starts(&data, KEYWORD_PATH_GROUP))
2232 return ccs_write_path_group_policy(data, is_delete);
2233 if (str_starts(&data, KEYWORD_DENY_REWRITE))
2234 return ccs_write_no_rewrite_policy(data, is_delete);
2235 if (str_starts(&data, KEYWORD_ADDRESS_GROUP))
2236 return ccs_write_address_group_policy(data, is_delete);
2237 return -EINVAL;
2238 }
2239
2240 /**
2241 * read_exception_policy - Read exception policy.
2242 *
2243 * @head: Pointer to "struct ccs_io_buffer".
2244 *
2245 * Returns 0 on success, -EINVAL otherwise.
2246 */
2247 static int read_exception_policy(struct ccs_io_buffer *head)
2248 {
2249 if (!head->read_eof) {
2250 switch (head->read_step) {
2251 case 0:
2252 head->read_var2 = NULL;
2253 head->read_step = 1;
2254 case 1:
2255 if (!ccs_read_domain_keeper_policy(head))
2256 break;
2257 head->read_var2 = NULL;
2258 head->read_step = 2;
2259 case 2:
2260 if (!ccs_read_globally_readable_policy(head))
2261 break;
2262 head->read_var2 = NULL;
2263 head->read_step = 3;
2264 case 3:
2265 if (!ccs_read_globally_usable_env_policy(head))
2266 break;
2267 head->read_var2 = NULL;
2268 head->read_step = 4;
2269 case 4:
2270 if (!ccs_read_domain_initializer_policy(head))
2271 break;
2272 head->read_var2 = NULL;
2273 head->read_step = 5;
2274 case 5:
2275 if (!ccs_read_alias_policy(head))
2276 break;
2277 head->read_var2 = NULL;
2278 head->read_step = 6;
2279 case 6:
2280 if (!ccs_read_aggregator_policy(head))
2281 break;
2282 head->read_var2 = NULL;
2283 head->read_step = 7;
2284 case 7:
2285 if (!ccs_read_file_pattern(head))
2286 break;
2287 head->read_var2 = NULL;
2288 head->read_step = 8;
2289 case 8:
2290 if (!ccs_read_no_rewrite_policy(head))
2291 break;
2292 head->read_var2 = NULL;
2293 head->read_step = 9;
2294 case 9:
2295 if (!ccs_read_path_group_policy(head))
2296 break;
2297 head->read_var1 = NULL;
2298 head->read_var2 = NULL;
2299 head->read_step = 10;
2300 case 10:
2301 if (!ccs_read_address_group_policy(head))
2302 break;
2303 head->read_eof = true;
2304 break;
2305 default:
2306 return -EINVAL;
2307 }
2308 }
2309 return 0;
2310 }
2311
2312 #endif
2313
2314 #ifdef CONFIG_SAKURA
2315
2316 /**
2317 * write_system_policy - Write system policy.
2318 *
2319 * @head: Pointer to "struct ccs_io_buffer".
2320 *
2321 * Returns 0 on success, negative value otherwise.
2322 */
2323 static int write_system_policy(struct ccs_io_buffer *head)
2324 {
2325 char *data = head->write_buf;
2326 bool is_delete = false;
2327 if (str_starts(&data, KEYWORD_DELETE))
2328 is_delete = true;
2329 if (str_starts(&data, KEYWORD_ALLOW_MOUNT))
2330 return ccs_write_mount_policy(data, is_delete);
2331 if (str_starts(&data, KEYWORD_DENY_UNMOUNT))
2332 return ccs_write_no_umount_policy(data, is_delete);
2333 if (str_starts(&data, KEYWORD_ALLOW_CHROOT))
2334 return ccs_write_chroot_policy(data, is_delete);
2335 if (str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))
2336 return ccs_write_pivot_root_policy(data, is_delete);
2337 if (str_starts(&data, KEYWORD_DENY_AUTOBIND))
2338 return ccs_write_reserved_port_policy(data, is_delete);
2339 return -EINVAL;
2340 }
2341
2342 /**
2343 * read_system_policy - Read system policy.
2344 *
2345 * @head: Pointer to "struct ccs_io_buffer".
2346 *
2347 * Returns 0 on success, -EINVAL otherwise.
2348 */
2349 static int read_system_policy(struct ccs_io_buffer *head)
2350 {
2351 if (!head->read_eof) {
2352 switch (head->read_step) {
2353 case 0:
2354 head->read_var2 = NULL;
2355 head->read_step = 1;
2356 case 1:
2357 if (!ccs_read_mount_policy(head))
2358 break;
2359 head->read_var2 = NULL;
2360 head->read_step = 2;
2361 case 2:
2362 if (!ccs_read_no_umount_policy(head))
2363 break;
2364 head->read_var2 = NULL;
2365 head->read_step = 3;
2366 case 3:
2367 if (!ccs_read_chroot_policy(head))
2368 break;
2369 head->read_var2 = NULL;
2370 head->read_step = 4;
2371 case 4:
2372 if (!ccs_read_pivot_root_policy(head))
2373 break;
2374 head->read_var2 = NULL;
2375 head->read_step = 5;
2376 case 5:
2377 if (!ccs_read_reserved_port_policy(head))
2378 break;
2379 head->read_eof = true;
2380 break;
2381 default:
2382 return -EINVAL;
2383 }
2384 }
2385 return 0;
2386 }
2387
2388 #endif
2389
2390 /* Path to the policy loader. The default is /sbin/ccs-init. */
2391 static const char *ccs_loader;
2392
2393 /**
2394 * loader_setup - Specify the policy loader to use.
2395 *
2396 * @str: Path to the policy loader.
2397 *
2398 * Returns 0.
2399 */
2400 static int __init loader_setup(char *str)
2401 {
2402 ccs_loader = str;
2403 return 0;
2404 }
2405
2406 __setup("CCS_loader=", loader_setup);
2407
2408 /**
2409 * policy_loader_exists - Check whether /sbin/ccs-init exists.
2410 *
2411 * Returns true if /sbin/ccs-init exists, false otherwise.
2412 */
2413 static bool policy_loader_exists(void)
2414 {
2415 /*
2416 * Don't activate MAC if the path given by 'CCS_loader=' option doesn't
2417 * exist. If the initrd includes /sbin/init but real-root-dev has not
2418 * mounted on / yet, activating MAC will block the system since
2419 * policies are not loaded yet.
2420 * Thus, let do_execve() call this function everytime.
2421 */
2422 struct nameidata nd;
2423 if (!ccs_loader)
2424 ccs_loader = "/sbin/ccs-init";
2425 if (path_lookup(ccs_loader, lookup_flags, &nd)) {
2426 printk(KERN_INFO "Not activating Mandatory Access Control now "
2427 "since %s doesn't exist.\n", ccs_loader);
2428 return false;
2429 }
2430 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
2431 path_put(&nd.path);
2432 #else
2433 path_release(&nd);
2434 #endif
2435 return true;
2436 }
2437
2438 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
2439 /**
2440 * run_ccs_loader - Start /sbin/ccs-init .
2441 *
2442 * @unused: Not used.
2443 *
2444 * Returns PID of /sbin/ccs-init on success, negative value otherwise.
2445 */
2446 static int run_ccs_loader(void *unused)
2447 {
2448 char *argv[2];
2449 char *envp[3];
2450 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2451 ccs_loader);
2452 argv[0] = (char *) ccs_loader;
2453 argv[1] = NULL;
2454 envp[0] = "HOME=/";
2455 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2456 envp[2] = NULL;
2457 return exec_usermodehelper(argv[0], argv, envp);
2458 }
2459 #endif
2460
2461 /**
2462 * ccs_load_policy - Run external policy loader to load policy.
2463 *
2464 * @filename: The program about to start.
2465 *
2466 * This function checks whether @filename is /sbin/init , and if so
2467 * invoke /sbin/ccs-init and wait for the termination of /sbin/ccs-init
2468 * and then continues invocation of /sbin/init.
2469 * /sbin/ccs-init reads policy files in /etc/ccs/ directory and
2470 * writes to /proc/ccs/ interfaces.
2471 *
2472 * Returns nothing.
2473 */
2474 void ccs_load_policy(const char *filename)
2475 {
2476 if (sbin_init_started)
2477 return;
2478 /*
2479 * Check filename is /sbin/init or /sbin/ccs-start.
2480 * /sbin/ccs-start is a dummy filename in case where /sbin/init can't
2481 * be passed.
2482 * You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start".
2483 */
2484 if (strcmp(filename, "/sbin/init") &&
2485 strcmp(filename, "/sbin/ccs-start"))
2486 return;
2487 if (!policy_loader_exists())
2488 return;
2489 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
2490 {
2491 char *argv[2];
2492 char *envp[3];
2493 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2494 ccs_loader);
2495 argv[0] = (char *) ccs_loader;
2496 argv[1] = NULL;
2497 envp[0] = "HOME=/";
2498 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2499 envp[2] = NULL;
2500 call_usermodehelper(argv[0], argv, envp, 1);
2501 }
2502 #elif defined(TASK_DEAD)
2503 {
2504 /* Copied from kernel/kmod.c */
2505 struct task_struct *task = current;
2506 pid_t pid = kernel_thread(run_ccs_loader, NULL, 0);
2507 sigset_t tmpsig;
2508 spin_lock_irq(&task->sighand->siglock);
2509 tmpsig = task->blocked;
2510 siginitsetinv(&task->blocked,
2511 sigmask(SIGKILL) | sigmask(SIGSTOP));
2512 recalc_sigpending();
2513 spin_unlock_irq(&current->sighand->siglock);
2514 if (pid >= 0)
2515 waitpid(pid, NULL, __WCLONE);
2516 spin_lock_irq(&task->sighand->siglock);
2517 task->blocked = tmpsig;
2518 recalc_sigpending();
2519 spin_unlock_irq(&task->sighand->siglock);
2520 }
2521 #else
2522 {
2523 /* Copied from kernel/kmod.c */
2524 struct task_struct *task = current;
2525 pid_t pid = kernel_thread(run_ccs_loader, NULL, 0);
2526 sigset_t tmpsig;
2527 spin_lock_irq(&task->sigmask_lock);
2528 tmpsig = task->blocked;
2529 siginitsetinv(&task->blocked,
2530 sigmask(SIGKILL) | sigmask(SIGSTOP));
2531 recalc_sigpending(task);
2532 spin_unlock_irq(&task->sigmask_lock);
2533 if (pid >= 0)
2534 waitpid(pid, NULL, __WCLONE);
2535 spin_lock_irq(&task->sigmask_lock);
2536 task->blocked = tmpsig;
2537 recalc_sigpending(task);
2538 spin_unlock_irq(&task->sigmask_lock);
2539 }
2540 #endif
2541 #ifdef CONFIG_SAKURA
2542 printk(KERN_INFO "SAKURA: 1.6.6-pre 2008/12/01\n");
2543 #endif
2544 #ifdef CONFIG_TOMOYO
2545 printk(KERN_INFO "TOMOYO: 1.6.6-pre 2008/12/22\n");
2546 #endif
2547 printk(KERN_INFO "Mandatory Access Control activated.\n");
2548 sbin_init_started = true;
2549 ccs_log_level = KERN_WARNING;
2550 { /* Check all profiles currently assigned to domains are defined. */
2551 struct domain_info *domain;
2552 list1_for_each_entry(domain, &domain_list, list) {
2553 const u8 profile = domain->profile;
2554 if (profile_ptr[profile])
2555 continue;
2556 panic("Profile %u (used by '%s') not defined.\n",
2557 profile, domain->domainname->name);
2558 }
2559 }
2560 }
2561
2562 /* Wait queue for query_list. */
2563 static DECLARE_WAIT_QUEUE_HEAD(query_wait);
2564
2565 /* Lock for manipulating query_list. */
2566 static DEFINE_SPINLOCK(query_list_lock);
2567
2568 /* Structure for query. */
2569 struct query_entry {
2570 struct list_head list;
2571 char *query;
2572 int query_len;
2573 unsigned int serial;
2574 int timer;
2575 int answer;
2576 };
2577
2578 /* The list for "struct query_entry". */
2579 static LIST_HEAD(query_list);
2580
2581 /* Number of "struct file" referring /proc/ccs/query interface. */
2582 static atomic_t queryd_watcher = ATOMIC_INIT(0);
2583
2584 /**
2585 * ccs_check_supervisor - Ask for the supervisor's decision.
2586 *
2587 * @r: Pointer to "struct ccs_request_info".
2588 * @fmt: The printf()'s format string, followed by parameters.
2589 *
2590 * Returns 0 if the supervisor decided to permit the access request which
2591 * violated the policy in enforcing mode, 1 if the supervisor decided to
2592 * retry the access request which violated the policy in enforcing mode,
2593 * -EPERM otherwise.
2594 */
2595 int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2596 {
2597 va_list args;
2598 int error = -EPERM;
2599 int pos;
2600 int len;
2601 static unsigned int serial;
2602 struct query_entry *query_entry = NULL;
2603 char *header;
2604 if (!r->domain)
2605 r->domain = current->domain_info;
2606 if (!atomic_read(&queryd_watcher)) {
2607 int i;
2608 if (current->tomoyo_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2609 return -EPERM;
2610 for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);
2611 i++) {
2612 set_current_state(TASK_INTERRUPTIBLE);
2613 schedule_timeout(HZ / 10);
2614 }
2615 return -EPERM;
2616 }
2617 va_start(args, fmt);
2618 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2619 va_end(args);
2620 #ifdef CONFIG_TOMOYO
2621 header = ccs_init_audit_log(&len, r);
2622 #else
2623 header = ccs_alloc(1, true);
2624 #endif
2625 if (!header)
2626 goto out;
2627 query_entry = ccs_alloc(sizeof(*query_entry), true);
2628 if (!query_entry)
2629 goto out;
2630 query_entry->query = ccs_alloc(len, true);
2631 if (!query_entry->query)
2632 goto out;
2633 INIT_LIST_HEAD(&query_entry->list);
2634 /***** CRITICAL SECTION START *****/
2635 spin_lock(&query_list_lock);
2636 query_entry->serial = serial++;
2637 spin_unlock(&query_list_lock);
2638 /***** CRITICAL SECTION END *****/
2639 pos = snprintf(query_entry->query, len - 1, "Q%u-%hu\n%s",
2640 query_entry->serial, r->retry, header);
2641 ccs_free(header);
2642 header = NULL;
2643 va_start(args, fmt);
2644 vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args);
2645 query_entry->query_len = strlen(query_entry->query) + 1;
2646 va_end(args);
2647 /***** CRITICAL SECTION START *****/
2648 spin_lock(&query_list_lock);
2649 list_add_tail(&query_entry->list, &query_list);
2650 spin_unlock(&query_list_lock);
2651 /***** CRITICAL SECTION END *****/
2652 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2653 /* Give 10 seconds for supervisor's opinion. */
2654 for (query_entry->timer = 0; atomic_read(&queryd_watcher)
2655 && query_entry->timer < 100; query_entry->timer++) {
2656 wake_up(&query_wait);
2657 set_current_state(TASK_INTERRUPTIBLE);
2658 schedule_timeout(HZ / 10);
2659 if (query_entry->answer)
2660 break;
2661 }
2662 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2663 /***** CRITICAL SECTION START *****/
2664 spin_lock(&query_list_lock);
2665 list_del(&query_entry->list);
2666 spin_unlock(&query_list_lock);
2667 /***** CRITICAL SECTION END *****/
2668 switch (query_entry->answer) {
2669 case 3: /* Asked to retry by administrator. */
2670 error = 1;
2671 r->retry++;
2672 break;
2673 case 1:
2674 /* Granted by administrator. */
2675 error = 0;
2676 break;
2677 case 0:
2678 /* Timed out. */
2679 break;
2680 default:
2681 /* Rejected by administrator. */
2682 break;
2683 }
2684 out:
2685 if (query_entry)
2686 ccs_free(query_entry->query);
2687 ccs_free(query_entry);
2688 ccs_free(header);
2689 return error;
2690 }
2691
2692 /**
2693 * poll_query - poll() for /proc/ccs/query.
2694 *
2695 * @file: Pointer to "struct file".
2696 * @wait: Pointer to "poll_table".
2697 *
2698 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2699 *
2700 * Waits for access requests which violated policy in enforcing mode.
2701 */
2702 static int poll_query(struct file *file, poll_table *wait)
2703 {
2704 struct list_head *tmp;
2705 bool found = false;
2706 u8 i;
2707 for (i = 0; i < 2; i++) {
2708 /***** CRITICAL SECTION START *****/
2709 spin_lock(&query_list_lock);
2710 list_for_each(tmp, &query_list) {
2711 struct query_entry *ptr
2712 = list_entry(tmp, struct query_entry, list);
2713 if (ptr->answer)
2714 continue;
2715 found = true;
2716 break;
2717 }
2718 spin_unlock(&query_list_lock);
2719 /***** CRITICAL SECTION END *****/
2720 if (found)
2721 return POLLIN | POLLRDNORM;
2722 if (i)
2723 break;
2724 poll_wait(file, &query_wait, wait);
2725 }
2726 return 0;
2727 }
2728
2729 /**
2730 * read_query - Read access requests which violated policy in enforcing mode.
2731 *
2732 * @head: Pointer to "struct ccs_io_buffer".
2733 *
2734 * Returns 0.
2735 */
2736 static int read_query(struct ccs_io_buffer *head)
2737 {
2738 struct list_head *tmp;
2739 int pos = 0;
2740 int len = 0;
2741 char *buf;
2742 if (head->read_avail)
2743 return 0;
2744 if (head->read_buf) {
2745 ccs_free(head->read_buf);
2746 head->read_buf = NULL;
2747 head->readbuf_size = 0;
2748 }
2749 /***** CRITICAL SECTION START *****/
2750 spin_lock(&query_list_lock);
2751 list_for_each(tmp, &query_list) {
2752 struct query_entry *ptr
2753 = list_entry(tmp, struct query_entry, list);
2754 if (ptr->answer)
2755 continue;
2756 if (pos++ != head->read_step)
2757 continue;
2758 len = ptr->query_len;
2759 break;
2760 }
2761 spin_unlock(&query_list_lock);
2762 /***** CRITICAL SECTION END *****/
2763 if (!len) {
2764 head->read_step = 0;
2765 return 0;
2766 }
2767 buf = ccs_alloc(len, false);
2768 if (!buf)
2769 return 0;
2770 pos = 0;
2771 /***** CRITICAL SECTION START *****/
2772 spin_lock(&query_list_lock);
2773 list_for_each(tmp, &query_list) {
2774 struct query_entry *ptr
2775 = list_entry(tmp, struct query_entry, list);
2776 if (ptr->answer)
2777 continue;
2778 if (pos++ != head->read_step)
2779 continue;
2780 /*
2781 * Some query can be skipped because query_list
2782 * can change, but I don't care.
2783 */
2784 if (len == ptr->query_len)
2785 memmove(buf, ptr->query, len);
2786 break;
2787 }
2788 spin_unlock(&query_list_lock);
2789 /***** CRITICAL SECTION END *****/
2790 if (buf[0]) {
2791 head->read_avail = len;
2792 head->readbuf_size = head->read_avail;
2793 head->read_buf = buf;
2794 head->read_step++;
2795 } else {
2796 ccs_free(buf);
2797 }
2798 return 0;
2799 }
2800
2801 /**
2802 * write_answer - Write the supervisor's decision.
2803 *
2804 * @head: Pointer to "struct ccs_io_buffer".
2805 *
2806 * Returns 0 on success, -EINVAL otherwise.
2807 */
2808 static int write_answer(struct ccs_io_buffer *head)
2809 {
2810 char *data = head->write_buf;
2811 struct list_head *tmp;
2812 unsigned int serial;
2813 unsigned int answer;
2814 /***** CRITICAL SECTION START *****/
2815 spin_lock(&query_list_lock);
2816 list_for_each(tmp, &query_list) {
2817 struct query_entry *ptr
2818 = list_entry(tmp, struct query_entry, list);
2819 ptr->timer = 0;
2820 }
2821 spin_unlock(&query_list_lock);
2822 /***** CRITICAL SECTION END *****/
2823 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2824 return -EINVAL;
2825 /***** CRITICAL SECTION START *****/
2826 spin_lock(&query_list_lock);
2827 list_for_each(tmp, &query_list) {
2828 struct query_entry *ptr
2829 = list_entry(tmp, struct query_entry, list);
2830 if (ptr->serial != serial)
2831 continue;
2832 if (!ptr->answer)
2833 ptr->answer = answer;
2834 break;
2835 }
2836 spin_unlock(&query_list_lock);
2837 /***** CRITICAL SECTION END *****/
2838 return 0;
2839 }
2840
2841 /* Policy updates counter. */
2842 static unsigned int updates_counter[MAX_CCS_UPDATES_COUNTER];
2843
2844 /* Policy updates counter lock. */
2845 static DEFINE_SPINLOCK(updates_counter_lock);
2846
2847 /**
2848 * ccs_update_counter - Increment policy change counter.
2849 *
2850 * @index: Type of policy.
2851 *
2852 * Returns nothing.
2853 */
2854 void ccs_update_counter(const unsigned char index)
2855 {
2856 /***** CRITICAL SECTION START *****/
2857 spin_lock(&updates_counter_lock);
2858 if (index < MAX_CCS_UPDATES_COUNTER)
2859 updates_counter[index]++;
2860 spin_unlock(&updates_counter_lock);
2861 /***** CRITICAL SECTION END *****/
2862 }
2863
2864 /**
2865 * read_updates_counter - Check for policy change counter.
2866 *
2867 * @head: Pointer to "struct ccs_io_buffer".
2868 *
2869 * Returns how many times policy has changed since the previous check.
2870 */
2871 static int read_updates_counter(struct ccs_io_buffer *head)
2872 {
2873 if (!head->read_eof) {
2874 unsigned int counter[MAX_CCS_UPDATES_COUNTER];
2875 /***** CRITICAL SECTION START *****/
2876 spin_lock(&updates_counter_lock);
2877 memmove(counter, updates_counter, sizeof(updates_counter));
2878 memset(updates_counter, 0, sizeof(updates_counter));
2879 spin_unlock(&updates_counter_lock);
2880 /***** CRITICAL SECTION END *****/
2881 ccs_io_printf(head,
2882 "/proc/ccs/system_policy: %10u\n"
2883 "/proc/ccs/domain_policy: %10u\n"
2884 "/proc/ccs/exception_policy: %10u\n"
2885 "/proc/ccs/profile: %10u\n"
2886 "/proc/ccs/query: %10u\n"
2887 "/proc/ccs/manager: %10u\n"
2888 "/proc/ccs/grant_log: %10u\n"
2889 "/proc/ccs/reject_log: %10u\n",
2890 counter[CCS_UPDATES_COUNTER_SYSTEM_POLICY],
2891 counter[CCS_UPDATES_COUNTER_DOMAIN_POLICY],
2892 counter[CCS_UPDATES_COUNTER_EXCEPTION_POLICY],
2893 counter[CCS_UPDATES_COUNTER_PROFILE],
2894 counter[CCS_UPDATES_COUNTER_QUERY],
2895 counter[CCS_UPDATES_COUNTER_MANAGER],
2896 counter[CCS_UPDATES_COUNTER_GRANT_LOG],
2897 counter[CCS_UPDATES_COUNTER_REJECT_LOG]);
2898 head->read_eof = true;
2899 }
2900 return 0;
2901 }
2902
2903 /**
2904 * read_version: Get version.
2905 *
2906 * @head: Pointer to "struct ccs_io_buffer".
2907 *
2908 * Returns version information.
2909 */
2910 static int read_version(struct ccs_io_buffer *head)
2911 {
2912 if (!head->read_eof) {
2913 ccs_io_printf(head, "1.6.6-pre");
2914 head->read_eof = true;
2915 }
2916 return 0;
2917 }
2918
2919 /**
2920 * read_self_domain - Get the current process's domainname.
2921 *
2922 * @head: Pointer to "struct ccs_io_buffer".
2923 *
2924 * Returns the current process's domainname.
2925 */
2926 static int read_self_domain(struct ccs_io_buffer *head)
2927 {
2928 if (!head->read_eof) {
2929 /*
2930 * current->domain_info->domainname != NULL
2931 * because every process belongs to a domain and
2932 * the domain's name cannot be NULL.
2933 */
2934 ccs_io_printf(head, "%s",
2935 current->domain_info->domainname->name);
2936 head->read_eof = true;
2937 }
2938 return 0;
2939 }
2940
2941 /**
2942 * ccs_open_control - open() for /proc/ccs/ interface.
2943 *
2944 * @type: Type of interface.
2945 * @file: Pointer to "struct file".
2946 *
2947 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2948 */
2949 int ccs_open_control(const u8 type, struct file *file)
2950 {
2951 struct ccs_io_buffer *head = ccs_alloc(sizeof(*head), false);
2952 if (!head)
2953 return -ENOMEM;
2954 mutex_init(&head->io_sem);
2955 switch (type) {
2956 #ifdef CONFIG_SAKURA
2957 case CCS_SYSTEMPOLICY: /* /proc/ccs/system_policy */
2958 head->write = write_system_policy;
2959 head->read = read_system_policy;
2960 break;
2961 #endif
2962 #ifdef CONFIG_TOMOYO
2963 case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2964 head->write = write_domain_policy;
2965 head->read = read_domain_policy;
2966 break;
2967 case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2968 head->write = write_exception_policy;
2969 head->read = read_exception_policy;
2970 break;
2971 case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2972 head->poll = ccs_poll_grant_log;
2973 head->read = ccs_read_grant_log;
2974 break;
2975 case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2976 head->poll = ccs_poll_reject_log;
2977 head->read = ccs_read_reject_log;
2978 break;
2979 #endif
2980 case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
2981 head->read = read_self_domain;
2982 break;
2983 case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2984 head->write = write_domain_profile;
2985 head->read = read_domain_profile;
2986 break;
2987 case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2988 /* Allow execute_handler to read process's status. */
2989 if (!(current->tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER)) {
2990 ccs_free(head);
2991 return -EPERM;
2992 }
2993 /* fall through */
2994 case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2995 head->write = write_pid;
2996 head->read = read_pid;
2997 break;
2998 case CCS_VERSION: /* /proc/ccs/version */
2999 head->read = read_version;
3000 head->readbuf_size = 128;
3001 break;
3002 case CCS_MEMINFO: /* /proc/ccs/meminfo */
3003 head->write = ccs_write_memory_quota;
3004 head->read = ccs_read_memory_counter;
3005 head->readbuf_size = 512;
3006 break;
3007 case CCS_PROFILE: /* /proc/ccs/profile */
3008 head->write = write_profile;
3009 head->read = read_profile;
3010 break;
3011 case CCS_QUERY: /* /proc/ccs/query */
3012 head->poll = poll_query;
3013 head->write = write_answer;
3014 head->read = read_query;
3015 break;
3016 case CCS_MANAGER: /* /proc/ccs/manager */
3017 head->write = write_manager_policy;
3018 head->read = read_manager_policy;
3019 break;
3020 case CCS_UPDATESCOUNTER: /* /proc/ccs/.updates_counter */
3021 head->read = read_updates_counter;
3022 break;
3023 }
3024 if (!(file->f_mode & FMODE_READ)) {
3025 /*
3026 * No need to allocate read_buf since it is not opened
3027 * for reading.
3028 */
3029 head->read = NULL;
3030 head->poll = NULL;
3031 } else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG
3032 && type != CCS_QUERY) {
3033 /*
3034 * Don't allocate buffer for reading if the file is one of
3035 * /proc/ccs/grant_log , /proc/ccs/reject_log , /proc/ccs/query.
3036 */
3037 if (!head->readbuf_size)
3038 head->readbuf_size = 4096 * 2;
3039 head->read_buf = ccs_alloc(head->readbuf_size, false);
3040 if (!head->read_buf) {
3041 ccs_free(head);
3042 return -ENOMEM;
3043 }
3044 }
3045 if (!(file->f_mode & FMODE_WRITE)) {
3046 /*
3047 * No need to allocate write_buf since it is not opened
3048 * for writing.
3049 */
3050 head->write = NULL;
3051 } else if (head->write) {
3052 head->writebuf_size = 4096 * 2;
3053 head->write_buf = ccs_alloc(head->writebuf_size, false);
3054 if (!head->write_buf) {
3055 ccs_free(head->read_buf);
3056 ccs_free(head);
3057 return -ENOMEM;
3058 }
3059 }
3060 file->private_data = head;
3061 /*
3062 * Call the handler now if the file is /proc/ccs/self_domain
3063 * so that the user can use "cat < /proc/ccs/self_domain" to
3064 * know the current process's domainname.
3065 */
3066 if (type == CCS_SELFDOMAIN)
3067 ccs_read_control(file, NULL, 0);
3068 /*
3069 * If the file is /proc/ccs/query , increment the monitor count.
3070 * The monitor count is used by ccs_check_supervisor() to see if
3071 * there is some process monitoring /proc/ccs/query.
3072 */
3073 else if (head->write == write_answer || head->read == read_query)
3074 atomic_inc(&queryd_watcher);
3075 return 0;
3076 }
3077
3078 /**
3079 * ccs_poll_control - poll() for /proc/ccs/ interface.
3080 *
3081 * @file: Pointer to "struct file".
3082 * @wait: Pointer to "poll_table".
3083 *
3084 * Waits for read readiness.
3085 * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and
3086 * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
3087 * /usr/lib/ccs/ccs-auditd.
3088 */
3089 int ccs_poll_control(struct file *file, poll_table *wait)
3090 {
3091 struct ccs_io_buffer *head = file->private_data;
3092 if (!head->poll)
3093 return -ENOSYS;
3094 return head->poll(file, wait);
3095 }
3096
3097 /**
3098 * ccs_read_control - read() for /proc/ccs/ interface.
3099 *
3100 * @file: Pointer to "struct file".
3101 * @buffer: Poiner to buffer to write to.
3102 * @buffer_len: Size of @buffer.
3103 *
3104 * Returns bytes read on success, negative value otherwise.
3105 */
3106 int ccs_read_control(struct file *file, char __user *buffer,
3107 const int buffer_len)
3108 {
3109 int len = 0;
3110 struct ccs_io_buffer *head = file->private_data;
3111 char *cp;
3112 if (!head->read)
3113 return -ENOSYS;
3114 if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
3115 return -EFAULT;
3116 if (mutex_lock_interruptible(&head->io_sem))
3117 return -EINTR;
3118 /* Call the policy handler. */
3119 len = head->read(head);
3120 if (len < 0)
3121 goto out;
3122 /* Write to buffer. */
3123 len = head->read_avail;
3124 if (len > buffer_len)
3125 len = buffer_len;
3126 if (!len)
3127 goto out;
3128 /* head->read_buf changes by some functions. */
3129 cp = head->read_buf;
3130 if (copy_to_user(buffer, cp, len)) {
3131 len = -EFAULT;
3132 goto out;
3133 }
3134 head->read_avail -= len;
3135 memmove(cp, cp + len, head->read_avail);
3136 out:
3137 mutex_unlock(&head->io_sem);
3138 return len;
3139 }
3140
3141 /**
3142 * ccs_write_control - write() for /proc/ccs/ interface.
3143 *
3144 * @file: Pointer to "struct file".
3145 * @buffer: Pointer to buffer to read from.
3146 * @buffer_len: Size of @buffer.
3147 *
3148 * Returns @buffer_len on success, negative value otherwise.
3149 */
3150 int ccs_write_control(struct file *file, const char __user *buffer,
3151 const int buffer_len)
3152 {
3153 struct ccs_io_buffer *head = file->private_data;
3154 int error = buffer_len;
3155 int avail_len = buffer_len;
3156 char *cp0 = head->write_buf;
3157 if (!head->write)
3158 return -ENOSYS;
3159 if (!access_ok(VERIFY_READ, buffer, buffer_len))
3160 return -EFAULT;
3161 /* Don't allow updating policies by non manager programs. */
3162 if (head->write != write_pid &&
3163 #ifdef CONFIG_TOMOYO
3164 head->write != write_domain_policy &&
3165 #endif
3166 !is_policy_manager())
3167 return -EPERM;
3168 if (mutex_lock_interruptible(&head->io_sem))
3169 return -EINTR;
3170 /* Read a line and dispatch it to the policy handler. */
3171 while (avail_len > 0) {
3172 char c;
3173 if (head->write_avail >= head->writebuf_size - 1) {
3174 error = -ENOMEM;
3175 break;
3176 } else if (get_user(c, buffer)) {
3177 error = -EFAULT;
3178 break;
3179 }
3180 buffer++;
3181 avail_len--;
3182 cp0[head->write_avail++] = c;
3183 if (c != '\n')
3184 continue;
3185 cp0[head->write_avail - 1] = '\0';
3186 head->write_avail = 0;
3187 normalize_line(cp0);
3188 head->write(head);
3189 }
3190 mutex_unlock(&head->io_sem);
3191 return error;
3192 }
3193
3194 /**
3195 * ccs_close_control - close() for /proc/ccs/ interface.
3196 *
3197 * @file: Pointer to "struct file".
3198 *
3199 * Releases memory and returns 0.
3200 */
3201 int ccs_close_control(struct file *file)
3202 {
3203 struct ccs_io_buffer *head = file->private_data;
3204 /*
3205 * If the file is /proc/ccs/query , decrement the monitor count.
3206 */
3207 if (head->write == write_answer || head->read == read_query)
3208 atomic_dec(&queryd_watcher);
3209 /* Release memory used for policy I/O. */
3210 ccs_free(head->read_buf);
3211 head->read_buf = NULL;
3212 ccs_free(head->write_buf);
3213 head->write_buf = NULL;
3214 ccs_free(head);
3215 head = NULL;
3216 file->private_data = NULL;
3217 return 0;
3218 }
3219
3220 /**
3221 * ccs_alloc_acl_element - Allocate permanent memory for ACL entry.
3222 *
3223 * @acl_type: Type of ACL entry.
3224 * @condition: Pointer to condition part of the ACL entry. May be NULL.
3225 *
3226 * Returns pointer to the ACL entry on success, NULL otherwise.
3227 */
3228 void *ccs_alloc_acl_element(const u8 acl_type,
3229 const struct condition_list *condition)
3230 {
3231 int len;
3232 struct acl_info *ptr;
3233 switch (acl_type) {
3234 case TYPE_SINGLE_PATH_ACL:
3235 len = sizeof(struct single_path_acl_record);
3236 break;
3237 case TYPE_DOUBLE_PATH_ACL:
3238 len = sizeof(struct double_path_acl_record);
3239 break;
3240 case TYPE_ARGV0_ACL:
3241 len = sizeof(struct argv0_acl_record);
3242 break;
3243 case TYPE_ENV_ACL:
3244 len = sizeof(struct env_acl_record);
3245 break;
3246 case TYPE_CAPABILITY_ACL:
3247 len = sizeof(struct capability_acl_record);
3248 break;
3249 case TYPE_IP_NETWORK_ACL:
3250 len = sizeof(struct ip_network_acl_record);
3251 break;
3252 case TYPE_SIGNAL_ACL:
3253 len = sizeof(struct signal_acl_record);
3254 break;
3255 case TYPE_EXECUTE_HANDLER:
3256 case TYPE_DENIED_EXECUTE_HANDLER:
3257 len = sizeof(struct execute_handler_record);
3258 break;
3259 default:
3260 return NULL;
3261 }
3262 /*
3263 * If the ACL doesn't have condition part, reduce memory usage
3264 * by eliminating sizeof(struct condition_list *).
3265 */
3266 if (!condition)
3267 len -= sizeof(ptr->access_me_via_ccs_get_condition_part);
3268 ptr = ccs_alloc_element(len);
3269 if (!ptr)
3270 return NULL;
3271 if (condition) {
3272 ptr->access_me_via_ccs_get_condition_part = condition;
3273 ptr->type = acl_type | ACL_WITH_CONDITION;
3274 return ptr;
3275 }
3276 /*
3277 * Substract sizeof(struct condition_list *) because I eliminated
3278 * sizeof(struct condition_list *) from "struct acl_info"
3279 * but I must return the start address of "struct acl_info".
3280 */
3281 ptr = (void *) (((u8 *) ptr)
3282 - sizeof(ptr->access_me_via_ccs_get_condition_part));
3283 ptr->type = acl_type;
3284 return ptr;
3285 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26