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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1810 - (show annotations) (download) (as text)
Sun Nov 9 11:08:26 2008 UTC (15 years, 6 months ago) by kumaneko
Original Path: trunk/1.6.x/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 86380 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.5-rc 2008/11/07
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 (sscanf(data, "pid=%u", &pid) == 1) {
1480 struct task_struct *p;
1481 /***** CRITICAL SECTION START *****/
1482 read_lock(&tasklist_lock);
1483 p = find_task_by_pid(pid);
1484 if (p)
1485 domain = p->domain_info;
1486 read_unlock(&tasklist_lock);
1487 /***** CRITICAL SECTION END *****/
1488 } else if (!strncmp(data, "domain=", 7)) {
1489 if (ccs_is_domain_def(data + 7))
1490 domain = ccs_find_domain(data + 7);
1491 } else
1492 return false;
1493 head->write_var1 = domain;
1494 /* Accessing read_buf is safe because head->io_sem is held. */
1495 if (!head->read_buf)
1496 return true; /* Do nothing if open(O_WRONLY). */
1497 head->read_avail = 0;
1498 ccs_io_printf(head, "# select %s\n", data);
1499 head->read_single_domain = true;
1500 head->read_eof = !domain;
1501 if (domain) {
1502 struct domain_info *d;
1503 head->read_var1 = NULL;
1504 list1_for_each_entry(d, &domain_list, list) {
1505 if (d == domain)
1506 break;
1507 head->read_var1 = &d->list;
1508 }
1509 head->read_var2 = NULL;
1510 head->read_bit = 0;
1511 head->read_step = 0;
1512 if (domain->is_deleted)
1513 ccs_io_printf(head, "# This is a deleted domain.\n");
1514 }
1515 return true;
1516 }
1517
1518 /**
1519 * write_domain_policy - Write domain policy.
1520 *
1521 * @head: Pointer to "struct ccs_io_buffer".
1522 *
1523 * Returns 0 on success, negative value otherwise.
1524 */
1525 static int write_domain_policy(struct ccs_io_buffer *head)
1526 {
1527 char *data = head->write_buf;
1528 struct domain_info *domain = head->write_var1;
1529 bool is_delete = false;
1530 bool is_select = false;
1531 bool is_undelete = false;
1532 unsigned int profile;
1533 const struct condition_list *cond = NULL;
1534 char *cp;
1535 if (str_starts(&data, KEYWORD_DELETE))
1536 is_delete = true;
1537 else if (str_starts(&data, KEYWORD_SELECT))
1538 is_select = true;
1539 else if (str_starts(&data, KEYWORD_UNDELETE))
1540 is_undelete = true;
1541 if (is_select && is_select_one(head, data))
1542 return 0;
1543 /* Don't allow updating policies by non manager programs. */
1544 if (!is_policy_manager())
1545 return -EPERM;
1546 if (ccs_is_domain_def(data)) {
1547 domain = NULL;
1548 if (is_delete)
1549 ccs_delete_domain(data);
1550 else if (is_select)
1551 domain = ccs_find_domain(data);
1552 else if (is_undelete)
1553 domain = ccs_undelete_domain(data);
1554 else
1555 domain = ccs_find_or_assign_new_domain(data, 0);
1556 head->write_var1 = domain;
1557 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1558 return 0;
1559 }
1560 if (!domain)
1561 return -EINVAL;
1562
1563 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1564 && profile < MAX_PROFILES) {
1565 if (profile_ptr[profile] || !sbin_init_started)
1566 domain->profile = (u8) profile;
1567 return 0;
1568 }
1569 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1570 ccs_set_domain_flag(domain, is_delete,
1571 DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1572 return 0;
1573 }
1574 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1575 ccs_set_domain_flag(domain, is_delete,
1576 DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV);
1577 return 0;
1578 }
1579 cp = ccs_find_condition_part(data);
1580 if (cp) {
1581 cond = ccs_find_or_assign_new_condition(cp);
1582 if (!cond)
1583 return -EINVAL;
1584 }
1585 if (str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1586 return ccs_write_capability_policy(data, domain, cond,
1587 is_delete);
1588 else if (str_starts(&data, KEYWORD_ALLOW_NETWORK))
1589 return ccs_write_network_policy(data, domain, cond, is_delete);
1590 else if (str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1591 return ccs_write_signal_policy(data, domain, cond, is_delete);
1592 else if (str_starts(&data, KEYWORD_ALLOW_ARGV0))
1593 return ccs_write_argv0_policy(data, domain, cond, is_delete);
1594 else if (str_starts(&data, KEYWORD_ALLOW_ENV))
1595 return ccs_write_env_policy(data, domain, cond, is_delete);
1596 else
1597 return ccs_write_file_policy(data, domain, cond, is_delete);
1598 }
1599
1600 /**
1601 * print_single_path_acl - Print a single path ACL entry.
1602 *
1603 * @head: Pointer to "struct ccs_io_buffer".
1604 * @ptr: Pointer to "struct single_path_acl_record".
1605 * @cond: Pointer to "struct condition_list". May be NULL.
1606 *
1607 * Returns true on success, false otherwise.
1608 */
1609 static bool print_single_path_acl(struct ccs_io_buffer *head,
1610 struct single_path_acl_record *ptr,
1611 const struct condition_list *cond)
1612 {
1613 int pos;
1614 u8 bit;
1615 const char *atmark = "";
1616 const char *filename;
1617 const u16 perm = ptr->perm;
1618 if (ptr->u_is_group) {
1619 atmark = "@";
1620 filename = ptr->u.group->group_name->name;
1621 } else {
1622 filename = ptr->u.filename->name;
1623 }
1624 for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1625 const char *msg;
1626 if (!(perm & (1 << bit)))
1627 continue;
1628 /* Print "read/write" instead of "read" and "write". */
1629 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1630 && (perm & (1 << TYPE_READ_WRITE_ACL)))
1631 continue;
1632 msg = ccs_sp2keyword(bit);
1633 pos = head->read_avail;
1634 if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1635 atmark, filename) ||
1636 !ccs_print_condition(head, cond))
1637 goto out;
1638 }
1639 head->read_bit = 0;
1640 return true;
1641 out:
1642 head->read_bit = bit;
1643 head->read_avail = pos;
1644 return false;
1645 }
1646
1647 /**
1648 * print_double_path_acl - Print a double path ACL entry.
1649 *
1650 * @head: Pointer to "struct ccs_io_buffer".
1651 * @ptr: Pointer to "struct double_path_acl_record".
1652 * @cond: Pointer to "struct condition_list". May be NULL.
1653 *
1654 * Returns true on success, false otherwise.
1655 */
1656 static bool print_double_path_acl(struct ccs_io_buffer *head,
1657 struct double_path_acl_record *ptr,
1658 const struct condition_list *cond)
1659 {
1660 int pos;
1661 const char *atmark1 = "";
1662 const char *atmark2 = "";
1663 const char *filename1;
1664 const char *filename2;
1665 const u8 perm = ptr->perm;
1666 u8 bit;
1667 if (ptr->u1_is_group) {
1668 atmark1 = "@";
1669 filename1 = ptr->u1.group1->group_name->name;
1670 } else {
1671 filename1 = ptr->u1.filename1->name;
1672 }
1673 if (ptr->u2_is_group) {
1674 atmark2 = "@";
1675 filename2 = ptr->u2.group2->group_name->name;
1676 } else {
1677 filename2 = ptr->u2.filename2->name;
1678 }
1679 for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1680 const char *msg;
1681 if (!(perm & (1 << bit)))
1682 continue;
1683 msg = ccs_dp2keyword(bit);
1684 pos = head->read_avail;
1685 if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1686 atmark1, filename1, atmark2, filename2) ||
1687 !ccs_print_condition(head, cond))
1688 goto out;
1689 }
1690 head->read_bit = 0;
1691 return true;
1692 out:
1693 head->read_bit = bit;
1694 head->read_avail = pos;
1695 return false;
1696 }
1697
1698 /**
1699 * print_argv0_acl - Print an argv[0] ACL entry.
1700 *
1701 * @head: Pointer to "struct ccs_io_buffer".
1702 * @ptr: Pointer to "struct argv0_acl_record".
1703 * @cond: Pointer to "struct condition_list". May be NULL.
1704 *
1705 * Returns true on success, false otherwise.
1706 */
1707 static bool print_argv0_acl(struct ccs_io_buffer *head,
1708 struct argv0_acl_record *ptr,
1709 const struct condition_list *cond)
1710 {
1711 int pos = head->read_avail;
1712 if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1713 ptr->filename->name, ptr->argv0->name))
1714 goto out;
1715 if (!ccs_print_condition(head, cond))
1716 goto out;
1717 return true;
1718 out:
1719 head->read_avail = pos;
1720 return false;
1721 }
1722
1723 /**
1724 * print_env_acl - Print an evironment variable name's ACL entry.
1725 *
1726 * @head: Pointer to "struct ccs_io_buffer".
1727 * @ptr: Pointer to "struct env_acl_record".
1728 * @cond: Pointer to "struct condition_list". May be NULL.
1729 *
1730 * Returns true on success, false otherwise.
1731 */
1732 static bool print_env_acl(struct ccs_io_buffer *head,
1733 struct env_acl_record *ptr,
1734 const struct condition_list *cond)
1735 {
1736 int pos = head->read_avail;
1737 if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1738 goto out;
1739 if (!ccs_print_condition(head, cond))
1740 goto out;
1741 return true;
1742 out:
1743 head->read_avail = pos;
1744 return false;
1745 }
1746
1747 /**
1748 * print_capability_acl - Print a capability ACL entry.
1749 *
1750 * @head: Pointer to "struct ccs_io_buffer".
1751 * @ptr: Pointer to "struct capability_acl_record".
1752 * @cond: Pointer to "struct condition_list". May be NULL.
1753 *
1754 * Returns true on success, false otherwise.
1755 */
1756 static bool print_capability_acl(struct ccs_io_buffer *head,
1757 struct capability_acl_record *ptr,
1758 const struct condition_list *cond)
1759 {
1760 int pos = head->read_avail;
1761 if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1762 ccs_cap2keyword(ptr->operation)))
1763 goto out;
1764 if (!ccs_print_condition(head, cond))
1765 goto out;
1766 return true;
1767 out:
1768 head->read_avail = pos;
1769 return false;
1770 }
1771
1772 /**
1773 * print_ipv4_entry - Print IPv4 address of a network ACL entry.
1774 *
1775 * @head: Pointer to "struct ccs_io_buffer".
1776 * @ptr: Pointer to "struct ip_network_acl_record".
1777 *
1778 * Returns true on success, false otherwise.
1779 */
1780 static bool print_ipv4_entry(struct ccs_io_buffer *head,
1781 struct ip_network_acl_record *ptr)
1782 {
1783 const u32 min_address = ptr->u.ipv4.min;
1784 const u32 max_address = ptr->u.ipv4.max;
1785 if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1786 return false;
1787 if (min_address != max_address
1788 && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1789 return false;
1790 return true;
1791 }
1792
1793 /**
1794 * print_ipv6_entry - Print IPv6 address of a network ACL entry.
1795 *
1796 * @head: Pointer to "struct ccs_io_buffer".
1797 * @ptr: Pointer to "struct ip_network_acl_record".
1798 *
1799 * Returns true on success, false otherwise.
1800 */
1801 static bool print_ipv6_entry(struct ccs_io_buffer *head,
1802 struct ip_network_acl_record *ptr)
1803 {
1804 char buf[64];
1805 const struct in6_addr *min_address = ptr->u.ipv6.min;
1806 const struct in6_addr *max_address = ptr->u.ipv6.max;
1807 ccs_print_ipv6(buf, sizeof(buf), min_address);
1808 if (!ccs_io_printf(head, "%s", buf))
1809 return false;
1810 if (min_address != max_address) {
1811 ccs_print_ipv6(buf, sizeof(buf), max_address);
1812 if (!ccs_io_printf(head, "-%s", buf))
1813 return false;
1814 }
1815 return true;
1816 }
1817
1818 /**
1819 * print_port_entry - Print port number of a network ACL entry.
1820 *
1821 * @head: Pointer to "struct ccs_io_buffer".
1822 * @ptr: Pointer to "struct ip_network_acl_record".
1823 *
1824 * Returns true on success, false otherwise.
1825 */
1826 static bool print_port_entry(struct ccs_io_buffer *head,
1827 struct ip_network_acl_record *ptr)
1828 {
1829 const u16 min_port = ptr->min_port, max_port = ptr->max_port;
1830 if (!ccs_io_printf(head, " %u", min_port))
1831 return false;
1832 if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1833 return false;
1834 return true;
1835 }
1836
1837 /**
1838 * print_network_acl - Print a network ACL entry.
1839 *
1840 * @head: Pointer to "struct ccs_io_buffer".
1841 * @ptr: Pointer to "struct ip_network_acl_record".
1842 * @cond: Pointer to "struct condition_list". May be NULL.
1843 *
1844 * Returns true on success, false otherwise.
1845 */
1846 static bool print_network_acl(struct ccs_io_buffer *head,
1847 struct ip_network_acl_record *ptr,
1848 const struct condition_list *cond)
1849 {
1850 int pos = head->read_avail;
1851 if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1852 ccs_net2keyword(ptr->operation_type)))
1853 goto out;
1854 switch (ptr->record_type) {
1855 case IP_RECORD_TYPE_ADDRESS_GROUP:
1856 if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1857 goto out;
1858 break;
1859 case IP_RECORD_TYPE_IPv4:
1860 if (!print_ipv4_entry(head, ptr))
1861 goto out;
1862 break;
1863 case IP_RECORD_TYPE_IPv6:
1864 if (!print_ipv6_entry(head, ptr))
1865 goto out;
1866 break;
1867 }
1868 if (!print_port_entry(head, ptr))
1869 goto out;
1870 if (!ccs_print_condition(head, cond))
1871 goto out;
1872 return true;
1873 out:
1874 head->read_avail = pos;
1875 return false;
1876 }
1877
1878 /**
1879 * print_signal_acl - Print a signal ACL entry.
1880 *
1881 * @head: Pointer to "struct ccs_io_buffer".
1882 * @ptr: Pointer to "struct signale_acl_record".
1883 * @cond: Pointer to "struct condition_list". May be NULL.
1884 *
1885 * Returns true on success, false otherwise.
1886 */
1887 static bool print_signal_acl(struct ccs_io_buffer *head,
1888 struct signal_acl_record *ptr,
1889 const struct condition_list *cond)
1890 {
1891 int pos = head->read_avail;
1892 if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
1893 ptr->sig, ptr->domainname->name))
1894 goto out;
1895 if (!ccs_print_condition(head, cond))
1896 goto out;
1897 return true;
1898 out:
1899 head->read_avail = pos;
1900 return false;
1901 }
1902
1903 /**
1904 * print_execute_handler_record - Print an execute handler ACL entry.
1905 *
1906 * @head: Pointer to "struct ccs_io_buffer".
1907 * @keyword: Name of the keyword.
1908 * @ptr: Pointer to "struct execute_handler_record".
1909 *
1910 * Returns true on success, false otherwise.
1911 */
1912 static bool print_execute_handler_record(struct ccs_io_buffer *head,
1913 const char *keyword,
1914 struct execute_handler_record *ptr)
1915 {
1916 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1917 }
1918
1919 /**
1920 * print_entry - Print an ACL entry.
1921 *
1922 * @head: Pointer to "struct ccs_io_buffer".
1923 * @ptr: Pointer to an ACL entry.
1924 *
1925 * Returns true on success, false otherwise.
1926 */
1927 static bool print_entry(struct ccs_io_buffer *head, struct acl_info *ptr)
1928 {
1929 const struct condition_list *cond = ccs_get_condition_part(ptr);
1930 const u8 acl_type = ccs_acl_type2(ptr);
1931 if (acl_type & ACL_DELETED)
1932 return true;
1933 if (acl_type == TYPE_SINGLE_PATH_ACL) {
1934 struct single_path_acl_record *acl
1935 = container_of(ptr, struct single_path_acl_record,
1936 head);
1937 return print_single_path_acl(head, acl, cond);
1938 }
1939 if (acl_type == TYPE_DOUBLE_PATH_ACL) {
1940 struct double_path_acl_record *acl
1941 = container_of(ptr, struct double_path_acl_record,
1942 head);
1943 return print_double_path_acl(head, acl, cond);
1944 }
1945 if (acl_type == TYPE_ARGV0_ACL) {
1946 struct argv0_acl_record *acl
1947 = container_of(ptr, struct argv0_acl_record, head);
1948 return print_argv0_acl(head, acl, cond);
1949 }
1950 if (acl_type == TYPE_ENV_ACL) {
1951 struct env_acl_record *acl
1952 = container_of(ptr, struct env_acl_record, head);
1953 return print_env_acl(head, acl, cond);
1954 }
1955 if (acl_type == TYPE_CAPABILITY_ACL) {
1956 struct capability_acl_record *acl
1957 = container_of(ptr, struct capability_acl_record, head);
1958 return print_capability_acl(head, acl, cond);
1959 }
1960 if (acl_type == TYPE_IP_NETWORK_ACL) {
1961 struct ip_network_acl_record *acl
1962 = container_of(ptr, struct ip_network_acl_record, head);
1963 return print_network_acl(head, acl, cond);
1964 }
1965 if (acl_type == TYPE_SIGNAL_ACL) {
1966 struct signal_acl_record *acl
1967 = container_of(ptr, struct signal_acl_record, head);
1968 return print_signal_acl(head, acl, cond);
1969 }
1970 if (acl_type == TYPE_EXECUTE_HANDLER) {
1971 struct execute_handler_record *acl
1972 = container_of(ptr, struct execute_handler_record,
1973 head);
1974 const char *keyword = KEYWORD_EXECUTE_HANDLER;
1975 return print_execute_handler_record(head, keyword, acl);
1976 }
1977 if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {
1978 struct execute_handler_record *acl
1979 = container_of(ptr, struct execute_handler_record,
1980 head);
1981 const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;
1982 return print_execute_handler_record(head, keyword, acl);
1983 }
1984 /* Workaround for gcc 3.2.2's inline bug. */
1985 if (acl_type & ACL_DELETED)
1986 return true;
1987 BUG(); /* This must not happen. */
1988 return false;
1989 }
1990
1991 /**
1992 * read_domain_policy - Read domain policy.
1993 *
1994 * @head: Pointer to "struct ccs_io_buffer".
1995 *
1996 * Returns 0.
1997 */
1998 static int read_domain_policy(struct ccs_io_buffer *head)
1999 {
2000 struct list1_head *dpos;
2001 struct list1_head *apos;
2002 if (head->read_eof)
2003 return 0;
2004 if (head->read_step == 0)
2005 head->read_step = 1;
2006 list1_for_each_cookie(dpos, head->read_var1, &domain_list) {
2007 struct domain_info *domain;
2008 const char *quota_exceeded = "";
2009 const char *transition_failed = "";
2010 const char *ignore_global_allow_read = "";
2011 const char *ignore_global_allow_env = "";
2012 domain = list1_entry(dpos, struct domain_info, list);
2013 if (head->read_step != 1)
2014 goto acl_loop;
2015 if (domain->is_deleted && !head->read_single_domain)
2016 continue;
2017 /* Print domainname and flags. */
2018 if (domain->quota_warned)
2019 quota_exceeded = "quota_exceeded\n";
2020 if (domain->flags & DOMAIN_FLAGS_TRANSITION_FAILED)
2021 transition_failed = "transition_failed\n";
2022 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
2023 ignore_global_allow_read
2024 = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
2025 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV)
2026 ignore_global_allow_env
2027 = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
2028 if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
2029 "%s%s%s%s\n", domain->domainname->name,
2030 domain->profile, quota_exceeded,
2031 transition_failed,
2032 ignore_global_allow_read,
2033 ignore_global_allow_env))
2034 return 0;
2035 head->read_step = 2;
2036 acl_loop:
2037 if (head->read_step == 3)
2038 goto tail_mark;
2039 /* Print ACL entries in the domain. */
2040 list1_for_each_cookie(apos, head->read_var2,
2041 &domain->acl_info_list) {
2042 struct acl_info *ptr
2043 = list1_entry(apos, struct acl_info, list);
2044 if (!print_entry(head, ptr))
2045 return 0;
2046 }
2047 head->read_step = 3;
2048 tail_mark:
2049 if (!ccs_io_printf(head, "\n"))
2050 return 0;
2051 head->read_step = 1;
2052 if (head->read_single_domain)
2053 break;
2054 }
2055 head->read_eof = true;
2056 return 0;
2057 }
2058
2059 #endif
2060
2061 /**
2062 * write_domain_profile - Assign profile for specified domain.
2063 *
2064 * @head: Pointer to "struct ccs_io_buffer".
2065 *
2066 * Returns 0 on success, -EINVAL otherwise.
2067 *
2068 * This is equivalent to doing
2069 *
2070 * ( echo "select " $domainname; echo "use_profile " $profile ) |
2071 * /usr/lib/ccs/loadpolicy -d
2072 */
2073 static int write_domain_profile(struct ccs_io_buffer *head)
2074 {
2075 char *data = head->write_buf;
2076 char *cp = strchr(data, ' ');
2077 struct domain_info *domain;
2078 unsigned int profile;
2079 if (!cp)
2080 return -EINVAL;
2081 *cp = '\0';
2082 domain = ccs_find_domain(cp + 1);
2083 profile = simple_strtoul(data, NULL, 10);
2084 if (domain && profile < MAX_PROFILES
2085 && (profile_ptr[profile] || !sbin_init_started))
2086 domain->profile = (u8) profile;
2087 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
2088 return 0;
2089 }
2090
2091 /**
2092 * read_domain_profile - Read only domainname and profile.
2093 *
2094 * @head: Pointer to "struct ccs_io_buffer".
2095 *
2096 * Returns list of profile number and domainname pairs.
2097 *
2098 * This is equivalent to doing
2099 *
2100 * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
2101 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
2102 * domainname = $0; } else if ( $1 == "use_profile" ) {
2103 * print $2 " " domainname; domainname = ""; } } ; '
2104 */
2105 static int read_domain_profile(struct ccs_io_buffer *head)
2106 {
2107 struct list1_head *pos;
2108 if (head->read_eof)
2109 return 0;
2110 list1_for_each_cookie(pos, head->read_var1, &domain_list) {
2111 struct domain_info *domain;
2112 domain = list1_entry(pos, struct domain_info, list);
2113 if (domain->is_deleted)
2114 continue;
2115 if (!ccs_io_printf(head, "%u %s\n", domain->profile,
2116 domain->domainname->name))
2117 return 0;
2118 }
2119 head->read_eof = true;
2120 return 0;
2121 }
2122
2123 /**
2124 * write_pid: Specify PID to obtain domainname.
2125 *
2126 * @head: Pointer to "struct ccs_io_buffer".
2127 *
2128 * Returns 0.
2129 */
2130 static int write_pid(struct ccs_io_buffer *head)
2131 {
2132 head->read_eof = false;
2133 return 0;
2134 }
2135
2136 /**
2137 * read_pid - Read information of a process.
2138 *
2139 * @head: Pointer to "struct ccs_io_buffer".
2140 *
2141 * Returns the domainname which the specified PID is in or
2142 * process information of the specified PID on success,
2143 * empty string otherwise.
2144 */
2145 static int read_pid(struct ccs_io_buffer *head)
2146 {
2147 char *buf = head->write_buf;
2148 bool task_info = false;
2149 unsigned int pid;
2150 struct task_struct *p;
2151 struct domain_info *domain = NULL;
2152 u32 tomoyo_flags = 0;
2153 /* Accessing write_buf is safe because head->io_sem is held. */
2154 if (!buf)
2155 goto done; /* Do nothing if open(O_RDONLY). */
2156 if (head->read_avail || head->read_eof)
2157 goto done;
2158 head->read_eof = true;
2159 if (str_starts(&buf, "info "))
2160 task_info = true;
2161 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
2162 /***** CRITICAL SECTION START *****/
2163 read_lock(&tasklist_lock);
2164 p = find_task_by_pid(pid);
2165 if (p) {
2166 domain = p->domain_info;
2167 tomoyo_flags = p->tomoyo_flags;
2168 }
2169 read_unlock(&tasklist_lock);
2170 /***** CRITICAL SECTION END *****/
2171 if (!domain)
2172 goto done;
2173 if (!task_info)
2174 ccs_io_printf(head, "%u %u %s", pid, domain->profile,
2175 domain->domainname->name);
2176 else
2177 ccs_io_printf(head, "%u manager=%s execute_handler=%s "
2178 "state[0]=%u state[1]=%u state[2]=%u", pid,
2179 tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER ?
2180 "yes" : "no",
2181 tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER ?
2182 "yes" : "no",
2183 (u8) (tomoyo_flags >> 24),
2184 (u8) (tomoyo_flags >> 16),
2185 (u8) (tomoyo_flags >> 8));
2186 done:
2187 return 0;
2188 }
2189
2190 #ifdef CONFIG_TOMOYO
2191
2192 /**
2193 * write_exception_policy - Write exception policy.
2194 *
2195 * @head: Pointer to "struct ccs_io_buffer".
2196 *
2197 * Returns 0 on success, negative value otherwise.
2198 */
2199 static int write_exception_policy(struct ccs_io_buffer *head)
2200 {
2201 char *data = head->write_buf;
2202 bool is_delete = str_starts(&data, KEYWORD_DELETE);
2203 if (str_starts(&data, KEYWORD_KEEP_DOMAIN))
2204 return ccs_write_domain_keeper_policy(data, false, is_delete);
2205 if (str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))
2206 return ccs_write_domain_keeper_policy(data, true, is_delete);
2207 if (str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))
2208 return ccs_write_domain_initializer_policy(data, false,
2209 is_delete);
2210 if (str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))
2211 return ccs_write_domain_initializer_policy(data, true,
2212 is_delete);
2213 if (str_starts(&data, KEYWORD_ALIAS))
2214 return ccs_write_alias_policy(data, is_delete);
2215 if (str_starts(&data, KEYWORD_AGGREGATOR))
2216 return ccs_write_aggregator_policy(data, is_delete);
2217 if (str_starts(&data, KEYWORD_ALLOW_READ))
2218 return ccs_write_globally_readable_policy(data, is_delete);
2219 if (str_starts(&data, KEYWORD_ALLOW_ENV))
2220 return ccs_write_globally_usable_env_policy(data, is_delete);
2221 if (str_starts(&data, KEYWORD_FILE_PATTERN))
2222 return ccs_write_pattern_policy(data, is_delete);
2223 if (str_starts(&data, KEYWORD_PATH_GROUP))
2224 return ccs_write_path_group_policy(data, is_delete);
2225 if (str_starts(&data, KEYWORD_DENY_REWRITE))
2226 return ccs_write_no_rewrite_policy(data, is_delete);
2227 if (str_starts(&data, KEYWORD_ADDRESS_GROUP))
2228 return ccs_write_address_group_policy(data, is_delete);
2229 return -EINVAL;
2230 }
2231
2232 /**
2233 * read_exception_policy - Read exception policy.
2234 *
2235 * @head: Pointer to "struct ccs_io_buffer".
2236 *
2237 * Returns 0 on success, -EINVAL otherwise.
2238 */
2239 static int read_exception_policy(struct ccs_io_buffer *head)
2240 {
2241 if (!head->read_eof) {
2242 switch (head->read_step) {
2243 case 0:
2244 head->read_var2 = NULL;
2245 head->read_step = 1;
2246 case 1:
2247 if (!ccs_read_domain_keeper_policy(head))
2248 break;
2249 head->read_var2 = NULL;
2250 head->read_step = 2;
2251 case 2:
2252 if (!ccs_read_globally_readable_policy(head))
2253 break;
2254 head->read_var2 = NULL;
2255 head->read_step = 3;
2256 case 3:
2257 if (!ccs_read_globally_usable_env_policy(head))
2258 break;
2259 head->read_var2 = NULL;
2260 head->read_step = 4;
2261 case 4:
2262 if (!ccs_read_domain_initializer_policy(head))
2263 break;
2264 head->read_var2 = NULL;
2265 head->read_step = 5;
2266 case 5:
2267 if (!ccs_read_alias_policy(head))
2268 break;
2269 head->read_var2 = NULL;
2270 head->read_step = 6;
2271 case 6:
2272 if (!ccs_read_aggregator_policy(head))
2273 break;
2274 head->read_var2 = NULL;
2275 head->read_step = 7;
2276 case 7:
2277 if (!ccs_read_file_pattern(head))
2278 break;
2279 head->read_var2 = NULL;
2280 head->read_step = 8;
2281 case 8:
2282 if (!ccs_read_no_rewrite_policy(head))
2283 break;
2284 head->read_var2 = NULL;
2285 head->read_step = 9;
2286 case 9:
2287 if (!ccs_read_path_group_policy(head))
2288 break;
2289 head->read_var1 = NULL;
2290 head->read_var2 = NULL;
2291 head->read_step = 10;
2292 case 10:
2293 if (!ccs_read_address_group_policy(head))
2294 break;
2295 head->read_eof = true;
2296 break;
2297 default:
2298 return -EINVAL;
2299 }
2300 }
2301 return 0;
2302 }
2303
2304 #endif
2305
2306 #ifdef CONFIG_SAKURA
2307
2308 /**
2309 * write_system_policy - Write system policy.
2310 *
2311 * @head: Pointer to "struct ccs_io_buffer".
2312 *
2313 * Returns 0 on success, negative value otherwise.
2314 */
2315 static int write_system_policy(struct ccs_io_buffer *head)
2316 {
2317 char *data = head->write_buf;
2318 bool is_delete = false;
2319 if (str_starts(&data, KEYWORD_DELETE))
2320 is_delete = true;
2321 if (str_starts(&data, KEYWORD_ALLOW_MOUNT))
2322 return ccs_write_mount_policy(data, is_delete);
2323 if (str_starts(&data, KEYWORD_DENY_UNMOUNT))
2324 return ccs_write_no_umount_policy(data, is_delete);
2325 if (str_starts(&data, KEYWORD_ALLOW_CHROOT))
2326 return ccs_write_chroot_policy(data, is_delete);
2327 if (str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))
2328 return ccs_write_pivot_root_policy(data, is_delete);
2329 if (str_starts(&data, KEYWORD_DENY_AUTOBIND))
2330 return ccs_write_reserved_port_policy(data, is_delete);
2331 return -EINVAL;
2332 }
2333
2334 /**
2335 * read_system_policy - Read system policy.
2336 *
2337 * @head: Pointer to "struct ccs_io_buffer".
2338 *
2339 * Returns 0 on success, -EINVAL otherwise.
2340 */
2341 static int read_system_policy(struct ccs_io_buffer *head)
2342 {
2343 if (!head->read_eof) {
2344 switch (head->read_step) {
2345 case 0:
2346 head->read_var2 = NULL;
2347 head->read_step = 1;
2348 case 1:
2349 if (!ccs_read_mount_policy(head))
2350 break;
2351 head->read_var2 = NULL;
2352 head->read_step = 2;
2353 case 2:
2354 if (!ccs_read_no_umount_policy(head))
2355 break;
2356 head->read_var2 = NULL;
2357 head->read_step = 3;
2358 case 3:
2359 if (!ccs_read_chroot_policy(head))
2360 break;
2361 head->read_var2 = NULL;
2362 head->read_step = 4;
2363 case 4:
2364 if (!ccs_read_pivot_root_policy(head))
2365 break;
2366 head->read_var2 = NULL;
2367 head->read_step = 5;
2368 case 5:
2369 if (!ccs_read_reserved_port_policy(head))
2370 break;
2371 head->read_eof = true;
2372 break;
2373 default:
2374 return -EINVAL;
2375 }
2376 }
2377 return 0;
2378 }
2379
2380 #endif
2381
2382 /* Path to the policy loader. The default is /sbin/ccs-init. */
2383 static const char *ccs_loader;
2384
2385 /**
2386 * loader_setup - Specify the policy loader to use.
2387 *
2388 * @str: Path to the policy loader.
2389 *
2390 * Returns 0.
2391 */
2392 static int __init loader_setup(char *str)
2393 {
2394 ccs_loader = str;
2395 return 0;
2396 }
2397
2398 __setup("CCS_loader=", loader_setup);
2399
2400 /**
2401 * policy_loader_exists - Check whether /sbin/ccs-init exists.
2402 *
2403 * Returns true if /sbin/ccs-init exists, false otherwise.
2404 */
2405 static bool policy_loader_exists(void)
2406 {
2407 /*
2408 * Don't activate MAC if the path given by 'CCS_loader=' option doesn't
2409 * exist. If the initrd includes /sbin/init but real-root-dev has not
2410 * mounted on / yet, activating MAC will block the system since
2411 * policies are not loaded yet.
2412 * Thus, let do_execve() call this function everytime.
2413 */
2414 struct nameidata nd;
2415 if (!ccs_loader)
2416 ccs_loader = "/sbin/ccs-init";
2417 if (path_lookup(ccs_loader, lookup_flags, &nd)) {
2418 printk(KERN_INFO "Not activating Mandatory Access Control now "
2419 "since %s doesn't exist.\n", ccs_loader);
2420 return false;
2421 }
2422 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
2423 path_put(&nd.path);
2424 #else
2425 path_release(&nd);
2426 #endif
2427 return true;
2428 }
2429
2430 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
2431 /**
2432 * run_ccs_loader - Start /sbin/ccs-init .
2433 *
2434 * @unused: Not used.
2435 *
2436 * Returns PID of /sbin/ccs-init on success, negative value otherwise.
2437 */
2438 static int run_ccs_loader(void *unused)
2439 {
2440 char *argv[2];
2441 char *envp[3];
2442 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2443 ccs_loader);
2444 argv[0] = (char *) ccs_loader;
2445 argv[1] = NULL;
2446 envp[0] = "HOME=/";
2447 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2448 envp[2] = NULL;
2449 return exec_usermodehelper(argv[0], argv, envp);
2450 }
2451 #endif
2452
2453 /**
2454 * ccs_load_policy - Run external policy loader to load policy.
2455 *
2456 * @filename: The program about to start.
2457 *
2458 * This function checks whether @filename is /sbin/init , and if so
2459 * invoke /sbin/ccs-init and wait for the termination of /sbin/ccs-init
2460 * and then continues invocation of /sbin/init.
2461 * /sbin/ccs-init reads policy files in /etc/ccs/ directory and
2462 * writes to /proc/ccs/ interfaces.
2463 *
2464 * Returns nothing.
2465 */
2466 void ccs_load_policy(const char *filename)
2467 {
2468 if (sbin_init_started)
2469 return;
2470 /*
2471 * Check filename is /sbin/init or /sbin/ccs-start.
2472 * /sbin/ccs-start is a dummy filename in case where /sbin/init can't
2473 * be passed.
2474 * You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start".
2475 */
2476 if (strcmp(filename, "/sbin/init") &&
2477 strcmp(filename, "/sbin/ccs-start"))
2478 return;
2479 if (!policy_loader_exists())
2480 return;
2481 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
2482 {
2483 char *argv[2];
2484 char *envp[3];
2485 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2486 ccs_loader);
2487 argv[0] = (char *) ccs_loader;
2488 argv[1] = NULL;
2489 envp[0] = "HOME=/";
2490 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2491 envp[2] = NULL;
2492 call_usermodehelper(argv[0], argv, envp, 1);
2493 }
2494 #elif defined(TASK_DEAD)
2495 {
2496 /* Copied from kernel/kmod.c */
2497 struct task_struct *task = current;
2498 pid_t pid = kernel_thread(run_ccs_loader, NULL, 0);
2499 sigset_t tmpsig;
2500 spin_lock_irq(&task->sighand->siglock);
2501 tmpsig = task->blocked;
2502 siginitsetinv(&task->blocked,
2503 sigmask(SIGKILL) | sigmask(SIGSTOP));
2504 recalc_sigpending();
2505 spin_unlock_irq(&current->sighand->siglock);
2506 if (pid >= 0)
2507 waitpid(pid, NULL, __WCLONE);
2508 spin_lock_irq(&task->sighand->siglock);
2509 task->blocked = tmpsig;
2510 recalc_sigpending();
2511 spin_unlock_irq(&task->sighand->siglock);
2512 }
2513 #else
2514 {
2515 /* Copied from kernel/kmod.c */
2516 struct task_struct *task = current;
2517 pid_t pid = kernel_thread(run_ccs_loader, NULL, 0);
2518 sigset_t tmpsig;
2519 spin_lock_irq(&task->sigmask_lock);
2520 tmpsig = task->blocked;
2521 siginitsetinv(&task->blocked,
2522 sigmask(SIGKILL) | sigmask(SIGSTOP));
2523 recalc_sigpending(task);
2524 spin_unlock_irq(&task->sigmask_lock);
2525 if (pid >= 0)
2526 waitpid(pid, NULL, __WCLONE);
2527 spin_lock_irq(&task->sigmask_lock);
2528 task->blocked = tmpsig;
2529 recalc_sigpending(task);
2530 spin_unlock_irq(&task->sigmask_lock);
2531 }
2532 #endif
2533 #ifdef CONFIG_SAKURA
2534 printk(KERN_INFO "SAKURA: 1.6.5-rc 2008/11/07\n");
2535 #endif
2536 #ifdef CONFIG_TOMOYO
2537 printk(KERN_INFO "TOMOYO: 1.6.5-rc 2008/11/09\n");
2538 #endif
2539 printk(KERN_INFO "Mandatory Access Control activated.\n");
2540 sbin_init_started = true;
2541 ccs_log_level = KERN_WARNING;
2542 { /* Check all profiles currently assigned to domains are defined. */
2543 struct domain_info *domain;
2544 list1_for_each_entry(domain, &domain_list, list) {
2545 const u8 profile = domain->profile;
2546 if (profile_ptr[profile])
2547 continue;
2548 panic("Profile %u (used by '%s') not defined.\n",
2549 profile, domain->domainname->name);
2550 }
2551 }
2552 }
2553
2554 /* Wait queue for query_list. */
2555 static DECLARE_WAIT_QUEUE_HEAD(query_wait);
2556
2557 /* Lock for manipulating query_list. */
2558 static DEFINE_SPINLOCK(query_list_lock);
2559
2560 /* Structure for query. */
2561 struct query_entry {
2562 struct list_head list;
2563 char *query;
2564 int query_len;
2565 unsigned int serial;
2566 int timer;
2567 int answer;
2568 };
2569
2570 /* The list for "struct query_entry". */
2571 static LIST_HEAD(query_list);
2572
2573 /* Number of "struct file" referring /proc/ccs/query interface. */
2574 static atomic_t queryd_watcher = ATOMIC_INIT(0);
2575
2576 /**
2577 * ccs_check_supervisor - Ask for the supervisor's decision.
2578 *
2579 * @r: Pointer to "struct ccs_request_info".
2580 * @fmt: The printf()'s format string, followed by parameters.
2581 *
2582 * Returns 0 if the supervisor decided to permit the access request which
2583 * violated the policy in enforcing mode, 1 if the supervisor decided to
2584 * retry the access request which violated the policy in enforcing mode,
2585 * -EPERM otherwise.
2586 */
2587 int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2588 {
2589 va_list args;
2590 int error = -EPERM;
2591 int pos;
2592 int len;
2593 static unsigned int serial;
2594 struct query_entry *query_entry = NULL;
2595 char *header;
2596 if (!r->domain)
2597 r->domain = current->domain_info;
2598 if (!atomic_read(&queryd_watcher)) {
2599 int i;
2600 if (current->tomoyo_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2601 return -EPERM;
2602 for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);
2603 i++) {
2604 set_current_state(TASK_INTERRUPTIBLE);
2605 schedule_timeout(HZ / 10);
2606 }
2607 return -EPERM;
2608 }
2609 va_start(args, fmt);
2610 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2611 va_end(args);
2612 #ifdef CONFIG_TOMOYO
2613 header = ccs_init_audit_log(&len, r);
2614 #else
2615 header = ccs_alloc(1);
2616 #endif
2617 if (!header)
2618 goto out;
2619 query_entry = ccs_alloc(sizeof(*query_entry));
2620 if (!query_entry)
2621 goto out;
2622 query_entry->query = ccs_alloc(len);
2623 if (!query_entry->query)
2624 goto out;
2625 INIT_LIST_HEAD(&query_entry->list);
2626 /***** CRITICAL SECTION START *****/
2627 spin_lock(&query_list_lock);
2628 query_entry->serial = serial++;
2629 spin_unlock(&query_list_lock);
2630 /***** CRITICAL SECTION END *****/
2631 pos = snprintf(query_entry->query, len - 1, "Q%u-%hu\n%s",
2632 query_entry->serial, r->retry, header);
2633 ccs_free(header);
2634 header = NULL;
2635 va_start(args, fmt);
2636 vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args);
2637 query_entry->query_len = strlen(query_entry->query) + 1;
2638 va_end(args);
2639 /***** CRITICAL SECTION START *****/
2640 spin_lock(&query_list_lock);
2641 list_add_tail(&query_entry->list, &query_list);
2642 spin_unlock(&query_list_lock);
2643 /***** CRITICAL SECTION END *****/
2644 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2645 /* Give 10 seconds for supervisor's opinion. */
2646 for (query_entry->timer = 0; atomic_read(&queryd_watcher)
2647 && query_entry->timer < 100; query_entry->timer++) {
2648 wake_up(&query_wait);
2649 set_current_state(TASK_INTERRUPTIBLE);
2650 schedule_timeout(HZ / 10);
2651 if (query_entry->answer)
2652 break;
2653 }
2654 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2655 /***** CRITICAL SECTION START *****/
2656 spin_lock(&query_list_lock);
2657 list_del(&query_entry->list);
2658 spin_unlock(&query_list_lock);
2659 /***** CRITICAL SECTION END *****/
2660 switch (query_entry->answer) {
2661 case 3: /* Asked to retry by administrator. */
2662 error = 1;
2663 r->retry++;
2664 break;
2665 case 1:
2666 /* Granted by administrator. */
2667 error = 0;
2668 break;
2669 case 0:
2670 /* Timed out. */
2671 break;
2672 default:
2673 /* Rejected by administrator. */
2674 break;
2675 }
2676 out:
2677 if (query_entry)
2678 ccs_free(query_entry->query);
2679 ccs_free(query_entry);
2680 ccs_free(header);
2681 return error;
2682 }
2683
2684 /**
2685 * poll_query - poll() for /proc/ccs/query.
2686 *
2687 * @file: Pointer to "struct file".
2688 * @wait: Pointer to "poll_table".
2689 *
2690 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2691 *
2692 * Waits for access requests which violated policy in enforcing mode.
2693 */
2694 static int poll_query(struct file *file, poll_table *wait)
2695 {
2696 struct list_head *tmp;
2697 bool found = false;
2698 u8 i;
2699 for (i = 0; i < 2; i++) {
2700 /***** CRITICAL SECTION START *****/
2701 spin_lock(&query_list_lock);
2702 list_for_each(tmp, &query_list) {
2703 struct query_entry *ptr
2704 = list_entry(tmp, struct query_entry, list);
2705 if (ptr->answer)
2706 continue;
2707 found = true;
2708 break;
2709 }
2710 spin_unlock(&query_list_lock);
2711 /***** CRITICAL SECTION END *****/
2712 if (found)
2713 return POLLIN | POLLRDNORM;
2714 if (i)
2715 break;
2716 poll_wait(file, &query_wait, wait);
2717 }
2718 return 0;
2719 }
2720
2721 /**
2722 * read_query - Read access requests which violated policy in enforcing mode.
2723 *
2724 * @head: Pointer to "struct ccs_io_buffer".
2725 *
2726 * Returns 0.
2727 */
2728 static int read_query(struct ccs_io_buffer *head)
2729 {
2730 struct list_head *tmp;
2731 int pos = 0;
2732 int len = 0;
2733 char *buf;
2734 if (head->read_avail)
2735 return 0;
2736 if (head->read_buf) {
2737 ccs_free(head->read_buf);
2738 head->read_buf = NULL;
2739 head->readbuf_size = 0;
2740 }
2741 /***** CRITICAL SECTION START *****/
2742 spin_lock(&query_list_lock);
2743 list_for_each(tmp, &query_list) {
2744 struct query_entry *ptr
2745 = list_entry(tmp, struct query_entry, list);
2746 if (ptr->answer)
2747 continue;
2748 if (pos++ != head->read_step)
2749 continue;
2750 len = ptr->query_len;
2751 break;
2752 }
2753 spin_unlock(&query_list_lock);
2754 /***** CRITICAL SECTION END *****/
2755 if (!len) {
2756 head->read_step = 0;
2757 return 0;
2758 }
2759 buf = ccs_alloc(len);
2760 if (!buf)
2761 return 0;
2762 pos = 0;
2763 /***** CRITICAL SECTION START *****/
2764 spin_lock(&query_list_lock);
2765 list_for_each(tmp, &query_list) {
2766 struct query_entry *ptr
2767 = list_entry(tmp, struct query_entry, list);
2768 if (ptr->answer)
2769 continue;
2770 if (pos++ != head->read_step)
2771 continue;
2772 /*
2773 * Some query can be skipped because query_list
2774 * can change, but I don't care.
2775 */
2776 if (len == ptr->query_len)
2777 memmove(buf, ptr->query, len);
2778 break;
2779 }
2780 spin_unlock(&query_list_lock);
2781 /***** CRITICAL SECTION END *****/
2782 if (buf[0]) {
2783 head->read_avail = len;
2784 head->readbuf_size = head->read_avail;
2785 head->read_buf = buf;
2786 head->read_step++;
2787 } else {
2788 ccs_free(buf);
2789 }
2790 return 0;
2791 }
2792
2793 /**
2794 * write_answer - Write the supervisor's decision.
2795 *
2796 * @head: Pointer to "struct ccs_io_buffer".
2797 *
2798 * Returns 0 on success, -EINVAL otherwise.
2799 */
2800 static int write_answer(struct ccs_io_buffer *head)
2801 {
2802 char *data = head->write_buf;
2803 struct list_head *tmp;
2804 unsigned int serial;
2805 unsigned int answer;
2806 /***** CRITICAL SECTION START *****/
2807 spin_lock(&query_list_lock);
2808 list_for_each(tmp, &query_list) {
2809 struct query_entry *ptr
2810 = list_entry(tmp, struct query_entry, list);
2811 ptr->timer = 0;
2812 }
2813 spin_unlock(&query_list_lock);
2814 /***** CRITICAL SECTION END *****/
2815 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2816 return -EINVAL;
2817 /***** CRITICAL SECTION START *****/
2818 spin_lock(&query_list_lock);
2819 list_for_each(tmp, &query_list) {
2820 struct query_entry *ptr
2821 = list_entry(tmp, struct query_entry, list);
2822 if (ptr->serial != serial)
2823 continue;
2824 if (!ptr->answer)
2825 ptr->answer = answer;
2826 break;
2827 }
2828 spin_unlock(&query_list_lock);
2829 /***** CRITICAL SECTION END *****/
2830 return 0;
2831 }
2832
2833 /* Policy updates counter. */
2834 static unsigned int updates_counter[MAX_CCS_UPDATES_COUNTER];
2835
2836 /* Policy updates counter lock. */
2837 static DEFINE_SPINLOCK(updates_counter_lock);
2838
2839 /**
2840 * ccs_update_counter - Increment policy change counter.
2841 *
2842 * @index: Type of policy.
2843 *
2844 * Returns nothing.
2845 */
2846 void ccs_update_counter(const unsigned char index)
2847 {
2848 /***** CRITICAL SECTION START *****/
2849 spin_lock(&updates_counter_lock);
2850 if (index < MAX_CCS_UPDATES_COUNTER)
2851 updates_counter[index]++;
2852 spin_unlock(&updates_counter_lock);
2853 /***** CRITICAL SECTION END *****/
2854 }
2855
2856 /**
2857 * read_updates_counter - Check for policy change counter.
2858 *
2859 * @head: Pointer to "struct ccs_io_buffer".
2860 *
2861 * Returns how many times policy has changed since the previous check.
2862 */
2863 static int read_updates_counter(struct ccs_io_buffer *head)
2864 {
2865 if (!head->read_eof) {
2866 unsigned int counter[MAX_CCS_UPDATES_COUNTER];
2867 /***** CRITICAL SECTION START *****/
2868 spin_lock(&updates_counter_lock);
2869 memmove(counter, updates_counter, sizeof(updates_counter));
2870 memset(updates_counter, 0, sizeof(updates_counter));
2871 spin_unlock(&updates_counter_lock);
2872 /***** CRITICAL SECTION END *****/
2873 ccs_io_printf(head,
2874 "/proc/ccs/system_policy: %10u\n"
2875 "/proc/ccs/domain_policy: %10u\n"
2876 "/proc/ccs/exception_policy: %10u\n"
2877 "/proc/ccs/profile: %10u\n"
2878 "/proc/ccs/query: %10u\n"
2879 "/proc/ccs/manager: %10u\n"
2880 "/proc/ccs/grant_log: %10u\n"
2881 "/proc/ccs/reject_log: %10u\n",
2882 counter[CCS_UPDATES_COUNTER_SYSTEM_POLICY],
2883 counter[CCS_UPDATES_COUNTER_DOMAIN_POLICY],
2884 counter[CCS_UPDATES_COUNTER_EXCEPTION_POLICY],
2885 counter[CCS_UPDATES_COUNTER_PROFILE],
2886 counter[CCS_UPDATES_COUNTER_QUERY],
2887 counter[CCS_UPDATES_COUNTER_MANAGER],
2888 counter[CCS_UPDATES_COUNTER_GRANT_LOG],
2889 counter[CCS_UPDATES_COUNTER_REJECT_LOG]);
2890 head->read_eof = true;
2891 }
2892 return 0;
2893 }
2894
2895 /**
2896 * read_version: Get version.
2897 *
2898 * @head: Pointer to "struct ccs_io_buffer".
2899 *
2900 * Returns version information.
2901 */
2902 static int read_version(struct ccs_io_buffer *head)
2903 {
2904 if (!head->read_eof) {
2905 ccs_io_printf(head, "1.6.5-rc");
2906 head->read_eof = true;
2907 }
2908 return 0;
2909 }
2910
2911 /**
2912 * read_self_domain - Get the current process's domainname.
2913 *
2914 * @head: Pointer to "struct ccs_io_buffer".
2915 *
2916 * Returns the current process's domainname.
2917 */
2918 static int read_self_domain(struct ccs_io_buffer *head)
2919 {
2920 if (!head->read_eof) {
2921 /*
2922 * current->domain_info->domainname != NULL
2923 * because every process belongs to a domain and
2924 * the domain's name cannot be NULL.
2925 */
2926 ccs_io_printf(head, "%s",
2927 current->domain_info->domainname->name);
2928 head->read_eof = true;
2929 }
2930 return 0;
2931 }
2932
2933 /**
2934 * ccs_open_control - open() for /proc/ccs/ interface.
2935 *
2936 * @type: Type of interface.
2937 * @file: Pointer to "struct file".
2938 *
2939 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2940 */
2941 int ccs_open_control(const u8 type, struct file *file)
2942 {
2943 struct ccs_io_buffer *head = ccs_alloc(sizeof(*head));
2944 if (!head)
2945 return -ENOMEM;
2946 mutex_init(&head->io_sem);
2947 switch (type) {
2948 #ifdef CONFIG_SAKURA
2949 case CCS_SYSTEMPOLICY: /* /proc/ccs/system_policy */
2950 head->write = write_system_policy;
2951 head->read = read_system_policy;
2952 break;
2953 #endif
2954 #ifdef CONFIG_TOMOYO
2955 case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2956 head->write = write_domain_policy;
2957 head->read = read_domain_policy;
2958 break;
2959 case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2960 head->write = write_exception_policy;
2961 head->read = read_exception_policy;
2962 break;
2963 case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2964 head->poll = ccs_poll_grant_log;
2965 head->read = ccs_read_grant_log;
2966 break;
2967 case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2968 head->poll = ccs_poll_reject_log;
2969 head->read = ccs_read_reject_log;
2970 break;
2971 #endif
2972 case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
2973 head->read = read_self_domain;
2974 break;
2975 case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2976 head->write = write_domain_profile;
2977 head->read = read_domain_profile;
2978 break;
2979 case CCS_EXECUTE_HANDLER: /* /proc/ccs/.execute_handler */
2980 /* Allow execute_handler to read process's status. */
2981 if (!(current->tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER)) {
2982 ccs_free(head);
2983 return -EPERM;
2984 }
2985 /* fall through */
2986 case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2987 head->write = write_pid;
2988 head->read = read_pid;
2989 break;
2990 case CCS_VERSION: /* /proc/ccs/version */
2991 head->read = read_version;
2992 head->readbuf_size = 128;
2993 break;
2994 case CCS_MEMINFO: /* /proc/ccs/meminfo */
2995 head->write = ccs_write_memory_quota;
2996 head->read = ccs_read_memory_counter;
2997 head->readbuf_size = 512;
2998 break;
2999 case CCS_PROFILE: /* /proc/ccs/profile */
3000 head->write = write_profile;
3001 head->read = read_profile;
3002 break;
3003 case CCS_QUERY: /* /proc/ccs/query */
3004 head->poll = poll_query;
3005 head->write = write_answer;
3006 head->read = read_query;
3007 break;
3008 case CCS_MANAGER: /* /proc/ccs/manager */
3009 head->write = write_manager_policy;
3010 head->read = read_manager_policy;
3011 break;
3012 case CCS_UPDATESCOUNTER: /* /proc/ccs/.updates_counter */
3013 head->read = read_updates_counter;
3014 break;
3015 }
3016 if (!(file->f_mode & FMODE_READ)) {
3017 /*
3018 * No need to allocate read_buf since it is not opened
3019 * for reading.
3020 */
3021 head->read = NULL;
3022 head->poll = NULL;
3023 } else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG
3024 && type != CCS_QUERY) {
3025 /*
3026 * Don't allocate buffer for reading if the file is one of
3027 * /proc/ccs/grant_log , /proc/ccs/reject_log , /proc/ccs/query.
3028 */
3029 if (!head->readbuf_size)
3030 head->readbuf_size = 4096 * 2;
3031 head->read_buf = ccs_alloc(head->readbuf_size);
3032 if (!head->read_buf) {
3033 ccs_free(head);
3034 return -ENOMEM;
3035 }
3036 }
3037 if (!(file->f_mode & FMODE_WRITE)) {
3038 /*
3039 * No need to allocate write_buf since it is not opened
3040 * for writing.
3041 */
3042 head->write = NULL;
3043 } else if (head->write) {
3044 head->writebuf_size = 4096 * 2;
3045 head->write_buf = ccs_alloc(head->writebuf_size);
3046 if (!head->write_buf) {
3047 ccs_free(head->read_buf);
3048 ccs_free(head);
3049 return -ENOMEM;
3050 }
3051 }
3052 file->private_data = head;
3053 /*
3054 * Call the handler now if the file is /proc/ccs/self_domain
3055 * so that the user can use "cat < /proc/ccs/self_domain" to
3056 * know the current process's domainname.
3057 */
3058 if (type == CCS_SELFDOMAIN)
3059 ccs_read_control(file, NULL, 0);
3060 /*
3061 * If the file is /proc/ccs/query , increment the monitor count.
3062 * The monitor count is used by ccs_check_supervisor() to see if
3063 * there is some process monitoring /proc/ccs/query.
3064 */
3065 else if (head->write == write_answer || head->read == read_query)
3066 atomic_inc(&queryd_watcher);
3067 return 0;
3068 }
3069
3070 /**
3071 * ccs_poll_control - poll() for /proc/ccs/ interface.
3072 *
3073 * @file: Pointer to "struct file".
3074 * @wait: Pointer to "poll_table".
3075 *
3076 * Waits for read readiness.
3077 * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and
3078 * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
3079 * /usr/lib/ccs/ccs-auditd.
3080 */
3081 int ccs_poll_control(struct file *file, poll_table *wait)
3082 {
3083 struct ccs_io_buffer *head = file->private_data;
3084 if (!head->poll)
3085 return -ENOSYS;
3086 return head->poll(file, wait);
3087 }
3088
3089 /**
3090 * ccs_read_control - read() for /proc/ccs/ interface.
3091 *
3092 * @file: Pointer to "struct file".
3093 * @buffer: Poiner to buffer to write to.
3094 * @buffer_len: Size of @buffer.
3095 *
3096 * Returns bytes read on success, negative value otherwise.
3097 */
3098 int ccs_read_control(struct file *file, char __user *buffer,
3099 const int buffer_len)
3100 {
3101 int len = 0;
3102 struct ccs_io_buffer *head = file->private_data;
3103 char *cp;
3104 if (!head->read)
3105 return -ENOSYS;
3106 if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
3107 return -EFAULT;
3108 if (mutex_lock_interruptible(&head->io_sem))
3109 return -EINTR;
3110 /* Call the policy handler. */
3111 len = head->read(head);
3112 if (len < 0)
3113 goto out;
3114 /* Write to buffer. */
3115 len = head->read_avail;
3116 if (len > buffer_len)
3117 len = buffer_len;
3118 if (!len)
3119 goto out;
3120 /* head->read_buf changes by some functions. */
3121 cp = head->read_buf;
3122 if (copy_to_user(buffer, cp, len)) {
3123 len = -EFAULT;
3124 goto out;
3125 }
3126 head->read_avail -= len;
3127 memmove(cp, cp + len, head->read_avail);
3128 out:
3129 mutex_unlock(&head->io_sem);
3130 return len;
3131 }
3132
3133 /**
3134 * ccs_write_control - write() for /proc/ccs/ interface.
3135 *
3136 * @file: Pointer to "struct file".
3137 * @buffer: Pointer to buffer to read from.
3138 * @buffer_len: Size of @buffer.
3139 *
3140 * Returns @buffer_len on success, negative value otherwise.
3141 */
3142 int ccs_write_control(struct file *file, const char __user *buffer,
3143 const int buffer_len)
3144 {
3145 struct ccs_io_buffer *head = file->private_data;
3146 int error = buffer_len;
3147 int avail_len = buffer_len;
3148 char *cp0 = head->write_buf;
3149 if (!head->write)
3150 return -ENOSYS;
3151 if (!access_ok(VERIFY_READ, buffer, buffer_len))
3152 return -EFAULT;
3153 /* Don't allow updating policies by non manager programs. */
3154 if (head->write != write_pid &&
3155 #ifdef CONFIG_TOMOYO
3156 head->write != write_domain_policy &&
3157 #endif
3158 !is_policy_manager())
3159 return -EPERM;
3160 if (mutex_lock_interruptible(&head->io_sem))
3161 return -EINTR;
3162 /* Read a line and dispatch it to the policy handler. */
3163 while (avail_len > 0) {
3164 char c;
3165 if (head->write_avail >= head->writebuf_size - 1) {
3166 error = -ENOMEM;
3167 break;
3168 } else if (get_user(c, buffer)) {
3169 error = -EFAULT;
3170 break;
3171 }
3172 buffer++;
3173 avail_len--;
3174 cp0[head->write_avail++] = c;
3175 if (c != '\n')
3176 continue;
3177 cp0[head->write_avail - 1] = '\0';
3178 head->write_avail = 0;
3179 normalize_line(cp0);
3180 head->write(head);
3181 }
3182 mutex_unlock(&head->io_sem);
3183 return error;
3184 }
3185
3186 /**
3187 * ccs_close_control - close() for /proc/ccs/ interface.
3188 *
3189 * @file: Pointer to "struct file".
3190 *
3191 * Releases memory and returns 0.
3192 */
3193 int ccs_close_control(struct file *file)
3194 {
3195 struct ccs_io_buffer *head = file->private_data;
3196 /*
3197 * If the file is /proc/ccs/query , decrement the monitor count.
3198 */
3199 if (head->write == write_answer || head->read == read_query)
3200 atomic_dec(&queryd_watcher);
3201 /* Release memory used for policy I/O. */
3202 ccs_free(head->read_buf);
3203 head->read_buf = NULL;
3204 ccs_free(head->write_buf);
3205 head->write_buf = NULL;
3206 ccs_free(head);
3207 head = NULL;
3208 file->private_data = NULL;
3209 return 0;
3210 }
3211
3212 /**
3213 * ccs_alloc_acl_element - Allocate permanent memory for ACL entry.
3214 *
3215 * @acl_type: Type of ACL entry.
3216 * @condition: Pointer to condition part of the ACL entry. May be NULL.
3217 *
3218 * Returns pointer to the ACL entry on success, NULL otherwise.
3219 */
3220 void *ccs_alloc_acl_element(const u8 acl_type,
3221 const struct condition_list *condition)
3222 {
3223 int len;
3224 struct acl_info *ptr;
3225 switch (acl_type) {
3226 case TYPE_SINGLE_PATH_ACL:
3227 len = sizeof(struct single_path_acl_record);
3228 break;
3229 case TYPE_DOUBLE_PATH_ACL:
3230 len = sizeof(struct double_path_acl_record);
3231 break;
3232 case TYPE_ARGV0_ACL:
3233 len = sizeof(struct argv0_acl_record);
3234 break;
3235 case TYPE_ENV_ACL:
3236 len = sizeof(struct env_acl_record);
3237 break;
3238 case TYPE_CAPABILITY_ACL:
3239 len = sizeof(struct capability_acl_record);
3240 break;
3241 case TYPE_IP_NETWORK_ACL:
3242 len = sizeof(struct ip_network_acl_record);
3243 break;
3244 case TYPE_SIGNAL_ACL:
3245 len = sizeof(struct signal_acl_record);
3246 break;
3247 case TYPE_EXECUTE_HANDLER:
3248 case TYPE_DENIED_EXECUTE_HANDLER:
3249 len = sizeof(struct execute_handler_record);
3250 break;
3251 default:
3252 return NULL;
3253 }
3254 /*
3255 * If the ACL doesn't have condition part, reduce memory usage
3256 * by eliminating sizeof(struct condition_list *).
3257 */
3258 if (!condition)
3259 len -= sizeof(ptr->access_me_via_ccs_get_condition_part);
3260 ptr = ccs_alloc_element(len);
3261 if (!ptr)
3262 return NULL;
3263 if (condition) {
3264 ptr->access_me_via_ccs_get_condition_part = condition;
3265 ptr->type = acl_type | ACL_WITH_CONDITION;
3266 return ptr;
3267 }
3268 /*
3269 * Substract sizeof(struct condition_list *) because I eliminated
3270 * sizeof(struct condition_list *) from "struct acl_info"
3271 * but I must return the start address of "struct acl_info".
3272 */
3273 ptr = (void *) (((u8 *) ptr)
3274 - sizeof(ptr->access_me_via_ccs_get_condition_part));
3275 ptr->type = acl_type;
3276 return ptr;
3277 }

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