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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1781 - (show annotations) (download) (as text)
Tue Nov 4 07:19:35 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: 86342 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-pre 2008/11/04
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 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 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 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 bool is_alphabet_char(const char c)
217 {
218 return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
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 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 = *(strchr(filename, '\0') - 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 char *ep = strchr(pathname, '\0');
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 = strchr(f, '\0');
770 if (!p_delimiter)
771 p_delimiter = strchr(p, '\0');
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 r->tomoyo_flags = current->tomoyo_flags;
965 }
966
967 /**
968 * ccs_verbose_mode - Check whether TOMOYO is verbose mode.
969 *
970 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
971 *
972 * Returns true if domain policy violation warning should be printed to
973 * console.
974 */
975 bool ccs_verbose_mode(const struct domain_info *domain)
976 {
977 return ccs_check_flags(domain, CCS_TOMOYO_VERBOSE) != 0;
978 }
979
980 /**
981 * ccs_check_domain_quota - Check for domain's quota.
982 *
983 * @domain: Pointer to "struct domain_info".
984 *
985 * Returns true if the domain is not exceeded quota, false otherwise.
986 */
987 bool ccs_check_domain_quota(struct domain_info * const domain)
988 {
989 unsigned int count = 0;
990 struct acl_info *ptr;
991 if (!domain)
992 return true;
993 list1_for_each_entry(ptr, &domain->acl_info_list, list) {
994 if (ptr->type & ACL_DELETED)
995 continue;
996 switch (ccs_acl_type2(ptr)) {
997 struct single_path_acl_record *acl1;
998 struct double_path_acl_record *acl2;
999 u16 perm;
1000 case TYPE_SINGLE_PATH_ACL:
1001 acl1 = container_of(ptr, struct single_path_acl_record,
1002 head);
1003 perm = acl1->perm;
1004 if (perm & (1 << TYPE_EXECUTE_ACL))
1005 count++;
1006 if (perm &
1007 ((1 << TYPE_READ_ACL) | (1 << TYPE_WRITE_ACL)))
1008 count++;
1009 if (perm & (1 << TYPE_CREATE_ACL))
1010 count++;
1011 if (perm & (1 << TYPE_UNLINK_ACL))
1012 count++;
1013 if (perm & (1 << TYPE_MKDIR_ACL))
1014 count++;
1015 if (perm & (1 << TYPE_RMDIR_ACL))
1016 count++;
1017 if (perm & (1 << TYPE_MKFIFO_ACL))
1018 count++;
1019 if (perm & (1 << TYPE_MKSOCK_ACL))
1020 count++;
1021 if (perm & (1 << TYPE_MKBLOCK_ACL))
1022 count++;
1023 if (perm & (1 << TYPE_MKCHAR_ACL))
1024 count++;
1025 if (perm & (1 << TYPE_TRUNCATE_ACL))
1026 count++;
1027 if (perm & (1 << TYPE_SYMLINK_ACL))
1028 count++;
1029 if (perm & (1 << TYPE_REWRITE_ACL))
1030 count++;
1031 break;
1032 case TYPE_DOUBLE_PATH_ACL:
1033 acl2 = container_of(ptr, struct double_path_acl_record,
1034 head);
1035 perm = acl2->perm;
1036 if (perm & (1 << TYPE_LINK_ACL))
1037 count++;
1038 if (perm & (1 << TYPE_RENAME_ACL))
1039 count++;
1040 break;
1041 case TYPE_EXECUTE_HANDLER:
1042 case TYPE_DENIED_EXECUTE_HANDLER:
1043 break;
1044 default:
1045 count++;
1046 }
1047 }
1048 if (count < ccs_check_flags(domain, CCS_TOMOYO_MAX_ACCEPT_ENTRY))
1049 return true;
1050 if (!domain->quota_warned) {
1051 domain->quota_warned = true;
1052 printk(KERN_WARNING "TOMOYO-WARNING: "
1053 "Domain '%s' has so many ACLs to hold. "
1054 "Stopped learning mode.\n", domain->domainname->name);
1055 }
1056 return false;
1057 }
1058
1059 /**
1060 * ccs_find_or_assign_new_profile - Create a new profile.
1061 *
1062 * @profile: Profile number to create.
1063 *
1064 * Returns pointer to "struct profile" on success, NULL otherwise.
1065 */
1066 static struct profile *ccs_find_or_assign_new_profile(const unsigned int
1067 profile)
1068 {
1069 static DEFINE_MUTEX(lock);
1070 struct profile *ptr = NULL;
1071 mutex_lock(&lock);
1072 if (profile < MAX_PROFILES) {
1073 ptr = profile_ptr[profile];
1074 if (ptr)
1075 goto ok;
1076 ptr = ccs_alloc_element(sizeof(*ptr));
1077 if (ptr) {
1078 int i;
1079 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)
1080 ptr->value[i]
1081 = ccs_control_array[i].current_value;
1082 /*
1083 * Needn't to initialize "ptr->capability_value"
1084 * because they are always 0.
1085 */
1086 mb(); /* Avoid out-of-order execution. */
1087 profile_ptr[profile] = ptr;
1088 }
1089 }
1090 ok:
1091 mutex_unlock(&lock);
1092 return ptr;
1093 }
1094
1095 /**
1096 * write_profile - Write profile table.
1097 *
1098 * @head: Pointer to "struct ccs_io_buffer".
1099 *
1100 * Returns 0 on success, negative value otherwise.
1101 */
1102 static int write_profile(struct ccs_io_buffer *head)
1103 {
1104 char *data = head->write_buf;
1105 unsigned int i;
1106 unsigned int value;
1107 char *cp;
1108 struct profile *profile;
1109 i = simple_strtoul(data, &cp, 10);
1110 if (data != cp) {
1111 if (*cp != '-')
1112 return -EINVAL;
1113 data = cp + 1;
1114 }
1115 profile = ccs_find_or_assign_new_profile(i);
1116 if (!profile)
1117 return -EINVAL;
1118 cp = strchr(data, '=');
1119 if (!cp)
1120 return -EINVAL;
1121 *cp = '\0';
1122 ccs_update_counter(CCS_UPDATES_COUNTER_PROFILE);
1123 if (!strcmp(data, "COMMENT")) {
1124 profile->comment = ccs_save_name(cp + 1);
1125 return 0;
1126 }
1127 #ifdef CONFIG_TOMOYO
1128 if (str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {
1129 if (sscanf(cp + 1, "%u", &value) != 1) {
1130 for (i = 0; i < 4; i++) {
1131 if (strcmp(cp + 1, mode_4[i]))
1132 continue;
1133 value = i;
1134 break;
1135 }
1136 if (i == 4)
1137 return -EINVAL;
1138 }
1139 if (value > 3)
1140 value = 3;
1141 for (i = 0; i < TOMOYO_MAX_CAPABILITY_INDEX; i++) {
1142 if (strcmp(data, capability_control_keyword[i]))
1143 continue;
1144 profile->capability_value[i] = value;
1145 return 0;
1146 }
1147 return -EINVAL;
1148 }
1149 #endif
1150 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
1151 if (strcmp(data, ccs_control_array[i].keyword))
1152 continue;
1153 if (sscanf(cp + 1, "%u", &value) != 1) {
1154 int j;
1155 const char **modes;
1156 switch (i) {
1157 case CCS_SAKURA_RESTRICT_AUTOBIND:
1158 case CCS_TOMOYO_VERBOSE:
1159 modes = mode_2;
1160 break;
1161 default:
1162 modes = mode_4;
1163 break;
1164 }
1165 for (j = 0; j < 4; j++) {
1166 if (strcmp(cp + 1, modes[j]))
1167 continue;
1168 value = j;
1169 break;
1170 }
1171 if (j == 4)
1172 return -EINVAL;
1173 } else if (value > ccs_control_array[i].max_value) {
1174 value = ccs_control_array[i].max_value;
1175 }
1176 switch (i) {
1177 case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1178 case CCS_SAKURA_RESTRICT_UNMOUNT:
1179 if (value == 1)
1180 value = 2; /* learning mode is not supported. */
1181 }
1182 profile->value[i] = value;
1183 return 0;
1184 }
1185 return -EINVAL;
1186 }
1187
1188 /**
1189 * read_profile - Read profile table.
1190 *
1191 * @head: Pointer to "struct ccs_io_buffer".
1192 *
1193 * Returns 0.
1194 */
1195 static int read_profile(struct ccs_io_buffer *head)
1196 {
1197 static const int total
1198 = CCS_MAX_CONTROL_INDEX + TOMOYO_MAX_CAPABILITY_INDEX + 1;
1199 int step;
1200 if (head->read_eof)
1201 return 0;
1202 for (step = head->read_step; step < MAX_PROFILES * total; step++) {
1203 const u8 index = step / total;
1204 u8 type = step % total;
1205 const struct profile *profile = profile_ptr[index];
1206 head->read_step = step;
1207 if (!profile)
1208 continue;
1209 #if !defined(CONFIG_SAKURA) || !defined(CONFIG_TOMOYO)
1210 switch (type) {
1211 #ifndef CONFIG_SAKURA
1212 case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1213 case CCS_SAKURA_RESTRICT_CHROOT:
1214 case CCS_SAKURA_RESTRICT_MOUNT:
1215 case CCS_SAKURA_RESTRICT_UNMOUNT:
1216 case CCS_SAKURA_RESTRICT_PIVOT_ROOT:
1217 case CCS_SAKURA_RESTRICT_AUTOBIND:
1218 #endif
1219 #ifndef CONFIG_TOMOYO
1220 case CCS_TOMOYO_MAC_FOR_FILE:
1221 case CCS_TOMOYO_MAC_FOR_ARGV0:
1222 case CCS_TOMOYO_MAC_FOR_ENV:
1223 case CCS_TOMOYO_MAC_FOR_NETWORK:
1224 case CCS_TOMOYO_MAC_FOR_SIGNAL:
1225 case CCS_TOMOYO_MAX_ACCEPT_ENTRY:
1226 case CCS_TOMOYO_MAX_GRANT_LOG:
1227 case CCS_TOMOYO_MAX_REJECT_LOG:
1228 case CCS_TOMOYO_VERBOSE:
1229 #endif
1230 continue;
1231 }
1232 #endif
1233 if (!type) { /* Print profile' comment tag. */
1234 if (!ccs_io_printf(head, "%u-COMMENT=%s\n",
1235 index, profile->comment ?
1236 profile->comment->name : ""))
1237 break;
1238 continue;
1239 }
1240 type--;
1241 if (type >= CCS_MAX_CONTROL_INDEX) {
1242 #ifdef CONFIG_TOMOYO
1243 const int i = type - CCS_MAX_CONTROL_INDEX;
1244 const u8 value = profile->capability_value[i];
1245 if (!ccs_io_printf(head,
1246 "%u-" KEYWORD_MAC_FOR_CAPABILITY
1247 "%s=%s\n", index,
1248 capability_control_keyword[i],
1249 mode_4[value]))
1250 break;
1251 #endif
1252 } else {
1253 const unsigned int value = profile->value[type];
1254 const char **modes = NULL;
1255 const char *keyword = ccs_control_array[type].keyword;
1256 switch (ccs_control_array[type].max_value) {
1257 case 3:
1258 modes = mode_4;
1259 break;
1260 case 1:
1261 modes = mode_2;
1262 break;
1263 }
1264 if (modes) {
1265 if (!ccs_io_printf(head, "%u-%s=%s\n", index,
1266 keyword, modes[value]))
1267 break;
1268 } else {
1269 if (!ccs_io_printf(head, "%u-%s=%u\n", index,
1270 keyword, value))
1271 break;
1272 }
1273 }
1274 }
1275 if (step == MAX_PROFILES * total)
1276 head->read_eof = true;
1277 return 0;
1278 }
1279
1280 /* Structure for policy manager. */
1281 struct policy_manager_entry {
1282 struct list1_head list;
1283 /* A path to program or a domainname. */
1284 const struct path_info *manager;
1285 bool is_domain; /* True if manager is a domainname. */
1286 bool is_deleted; /* True if this entry is deleted. */
1287 };
1288
1289 /* The list for "struct policy_manager_entry". */
1290 static LIST1_HEAD(policy_manager_list);
1291
1292 /**
1293 * update_manager_entry - Add a manager entry.
1294 *
1295 * @manager: The path to manager or the domainnamme.
1296 * @is_delete: True if it is a delete request.
1297 *
1298 * Returns 0 on success, negative value otherwise.
1299 */
1300 static int update_manager_entry(const char *manager, const bool is_delete)
1301 {
1302 struct policy_manager_entry *new_entry;
1303 struct policy_manager_entry *ptr;
1304 static DEFINE_MUTEX(lock);
1305 const struct path_info *saved_manager;
1306 int error = -ENOMEM;
1307 bool is_domain = false;
1308 if (ccs_is_domain_def(manager)) {
1309 if (!ccs_is_correct_domain(manager, __func__))
1310 return -EINVAL;
1311 is_domain = true;
1312 } else {
1313 if (!ccs_is_correct_path(manager, 1, -1, -1, __func__))
1314 return -EINVAL;
1315 }
1316 saved_manager = ccs_save_name(manager);
1317 if (!saved_manager)
1318 return -ENOMEM;
1319 mutex_lock(&lock);
1320 list1_for_each_entry(ptr, &policy_manager_list, list) {
1321 if (ptr->manager != saved_manager)
1322 continue;
1323 ptr->is_deleted = is_delete;
1324 error = 0;
1325 goto out;
1326 }
1327 if (is_delete) {
1328 error = -ENOENT;
1329 goto out;
1330 }
1331 new_entry = ccs_alloc_element(sizeof(*new_entry));
1332 if (!new_entry)
1333 goto out;
1334 new_entry->manager = saved_manager;
1335 new_entry->is_domain = is_domain;
1336 list1_add_tail_mb(&new_entry->list, &policy_manager_list);
1337 error = 0;
1338 out:
1339 mutex_unlock(&lock);
1340 if (!error)
1341 ccs_update_counter(CCS_UPDATES_COUNTER_MANAGER);
1342 return error;
1343 }
1344
1345 /**
1346 * write_manager_policy - Write manager policy.
1347 *
1348 * @head: Pointer to "struct ccs_io_buffer".
1349 *
1350 * Returns 0 on success, negative value otherwise.
1351 */
1352 static int write_manager_policy(struct ccs_io_buffer *head)
1353 {
1354 char *data = head->write_buf;
1355 bool is_delete = str_starts(&data, KEYWORD_DELETE);
1356 if (!strcmp(data, "manage_by_non_root")) {
1357 manage_by_non_root = !is_delete;
1358 return 0;
1359 }
1360 return update_manager_entry(data, is_delete);
1361 }
1362
1363 /**
1364 * read_manager_policy - Read manager policy.
1365 *
1366 * @head: Pointer to "struct ccs_io_buffer".
1367 *
1368 * Returns 0.
1369 */
1370 static int read_manager_policy(struct ccs_io_buffer *head)
1371 {
1372 struct list1_head *pos;
1373 if (head->read_eof)
1374 return 0;
1375 list1_for_each_cookie(pos, head->read_var2, &policy_manager_list) {
1376 struct policy_manager_entry *ptr;
1377 ptr = list1_entry(pos, struct policy_manager_entry, list);
1378 if (ptr->is_deleted)
1379 continue;
1380 if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
1381 return 0;
1382 }
1383 head->read_eof = true;
1384 return 0;
1385 }
1386
1387 /**
1388 * is_policy_manager - Check whether the current process is a policy manager.
1389 *
1390 * Returns true if the current process is permitted to modify policy
1391 * via /proc/ccs/ interface.
1392 */
1393 static bool is_policy_manager(void)
1394 {
1395 struct policy_manager_entry *ptr;
1396 const char *exe;
1397 struct task_struct *task = current;
1398 const struct path_info *domainname = task->domain_info->domainname;
1399 bool found = false;
1400 if (!sbin_init_started)
1401 return true;
1402 if (task->tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER)
1403 return true;
1404 if (!manage_by_non_root && (task->uid || task->euid))
1405 return false;
1406 list1_for_each_entry(ptr, &policy_manager_list, list) {
1407 if (!ptr->is_deleted && ptr->is_domain
1408 && !ccs_pathcmp(domainname, ptr->manager)) {
1409 /* Set manager flag. */
1410 task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER;
1411 return true;
1412 }
1413 }
1414 exe = ccs_get_exe();
1415 if (!exe)
1416 return false;
1417 list1_for_each_entry(ptr, &policy_manager_list, list) {
1418 if (!ptr->is_deleted && !ptr->is_domain
1419 && !strcmp(exe, ptr->manager->name)) {
1420 found = true;
1421 /* Set manager flag. */
1422 task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER;
1423 break;
1424 }
1425 }
1426 if (!found) { /* Reduce error messages. */
1427 static pid_t last_pid;
1428 const pid_t pid = current->pid;
1429 if (last_pid != pid) {
1430 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1431 "update policies.\n", domainname->name, exe);
1432 last_pid = pid;
1433 }
1434 }
1435 ccs_free(exe);
1436 return found;
1437 }
1438
1439 #ifdef CONFIG_TOMOYO
1440
1441 /**
1442 * ccs_find_condition_part - Find condition part from the statement.
1443 *
1444 * @data: String to parse.
1445 *
1446 * Returns pointer to the condition part if it was found in the statement,
1447 * NULL otherwise.
1448 */
1449 static char *ccs_find_condition_part(char *data)
1450 {
1451 char *cp = strstr(data, " if ");
1452 if (cp) {
1453 while (1) {
1454 char *cp2 = strstr(cp + 3, " if ");
1455 if (!cp2)
1456 break;
1457 cp = cp2;
1458 }
1459 *cp++ = '\0';
1460 } else {
1461 cp = strstr(data, " ; set ");
1462 if (cp)
1463 *cp++ = '\0';
1464 }
1465 return cp;
1466 }
1467
1468 /**
1469 * is_select_one - Parse select command.
1470 *
1471 * @head: Pointer to "struct ccs_io_buffer".
1472 * @data: String to parse.
1473 *
1474 * Returns true on success, false otherwise.
1475 */
1476 static bool is_select_one(struct ccs_io_buffer *head, const char *data)
1477 {
1478 unsigned int pid;
1479 struct domain_info *domain = NULL;
1480 if (sscanf(data, "pid=%u", &pid) == 1) {
1481 struct task_struct *p;
1482 /***** CRITICAL SECTION START *****/
1483 read_lock(&tasklist_lock);
1484 p = find_task_by_pid(pid);
1485 if (p)
1486 domain = p->domain_info;
1487 read_unlock(&tasklist_lock);
1488 /***** CRITICAL SECTION END *****/
1489 } else if (!strncmp(data, "domain=", 7)) {
1490 if (ccs_is_domain_def(data + 7))
1491 domain = ccs_find_domain(data + 7);
1492 } else
1493 return false;
1494 head->read_avail = 0;
1495 ccs_io_printf(head, "# select %s\n", data);
1496 head->read_single_domain = true;
1497 head->read_eof = !domain;
1498 if (domain) {
1499 struct domain_info *d;
1500 head->read_var1 = NULL;
1501 list1_for_each_entry(d, &domain_list, list) {
1502 if (d == domain)
1503 break;
1504 head->read_var1 = &d->list;
1505 }
1506 head->read_var2 = NULL;
1507 head->read_bit = 0;
1508 head->read_step = 0;
1509 if (domain->is_deleted)
1510 ccs_io_printf(head, "# This is a deleted domain.\n");
1511 }
1512 head->write_var1 = domain;
1513 return true;
1514 }
1515
1516 /**
1517 * write_domain_policy - Write domain policy.
1518 *
1519 * @head: Pointer to "struct ccs_io_buffer".
1520 *
1521 * Returns 0 on success, negative value otherwise.
1522 */
1523 static int write_domain_policy(struct ccs_io_buffer *head)
1524 {
1525 char *data = head->write_buf;
1526 struct domain_info *domain = head->write_var1;
1527 bool is_delete = false;
1528 bool is_select = false;
1529 bool is_undelete = false;
1530 unsigned int profile;
1531 const struct condition_list *cond = NULL;
1532 char *cp;
1533 if (str_starts(&data, KEYWORD_DELETE))
1534 is_delete = true;
1535 else if (str_starts(&data, KEYWORD_SELECT))
1536 is_select = true;
1537 else if (str_starts(&data, KEYWORD_UNDELETE))
1538 is_undelete = true;
1539 if (is_select && is_select_one(head, data))
1540 return 0;
1541 /* Don't allow updating policies by non manager programs. */
1542 if (!is_policy_manager())
1543 return -EPERM;
1544 if (ccs_is_domain_def(data)) {
1545 domain = NULL;
1546 if (is_delete)
1547 ccs_delete_domain(data);
1548 else if (is_select)
1549 domain = ccs_find_domain(data);
1550 else if (is_undelete)
1551 domain = ccs_undelete_domain(data);
1552 else
1553 domain = ccs_find_or_assign_new_domain(data, 0);
1554 head->write_var1 = domain;
1555 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1556 return 0;
1557 }
1558 if (!domain)
1559 return -EINVAL;
1560
1561 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1562 && profile < MAX_PROFILES) {
1563 if (profile_ptr[profile] || !sbin_init_started)
1564 domain->profile = (u8) profile;
1565 return 0;
1566 }
1567 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1568 ccs_set_domain_flag(domain, is_delete,
1569 DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1570 return 0;
1571 }
1572 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1573 ccs_set_domain_flag(domain, is_delete,
1574 DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV);
1575 return 0;
1576 }
1577 cp = ccs_find_condition_part(data);
1578 if (cp) {
1579 cond = ccs_find_or_assign_new_condition(cp);
1580 if (!cond)
1581 return -EINVAL;
1582 }
1583 if (str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1584 return ccs_write_capability_policy(data, domain, cond,
1585 is_delete);
1586 else if (str_starts(&data, KEYWORD_ALLOW_NETWORK))
1587 return ccs_write_network_policy(data, domain, cond, is_delete);
1588 else if (str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1589 return ccs_write_signal_policy(data, domain, cond, is_delete);
1590 else if (str_starts(&data, KEYWORD_ALLOW_ARGV0))
1591 return ccs_write_argv0_policy(data, domain, cond, is_delete);
1592 else if (str_starts(&data, KEYWORD_ALLOW_ENV))
1593 return ccs_write_env_policy(data, domain, cond, is_delete);
1594 else
1595 return ccs_write_file_policy(data, domain, cond, is_delete);
1596 }
1597
1598 /**
1599 * print_single_path_acl - Print a single path ACL entry.
1600 *
1601 * @head: Pointer to "struct ccs_io_buffer".
1602 * @ptr: Pointer to "struct single_path_acl_record".
1603 * @cond: Pointer to "struct condition_list". May be NULL.
1604 *
1605 * Returns true on success, false otherwise.
1606 */
1607 static bool print_single_path_acl(struct ccs_io_buffer *head,
1608 struct single_path_acl_record *ptr,
1609 const struct condition_list *cond)
1610 {
1611 int pos;
1612 u8 bit;
1613 const char *atmark = "";
1614 const char *filename;
1615 const u16 perm = ptr->perm;
1616 if (ptr->u_is_group) {
1617 atmark = "@";
1618 filename = ptr->u.group->group_name->name;
1619 } else {
1620 filename = ptr->u.filename->name;
1621 }
1622 for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1623 const char *msg;
1624 if (!(perm & (1 << bit)))
1625 continue;
1626 /* Print "read/write" instead of "read" and "write". */
1627 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1628 && (perm & (1 << TYPE_READ_WRITE_ACL)))
1629 continue;
1630 msg = ccs_sp2keyword(bit);
1631 pos = head->read_avail;
1632 if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1633 atmark, filename) ||
1634 !ccs_print_condition(head, cond))
1635 goto out;
1636 }
1637 head->read_bit = 0;
1638 return true;
1639 out:
1640 head->read_bit = bit;
1641 head->read_avail = pos;
1642 return false;
1643 }
1644
1645 /**
1646 * print_double_path_acl - Print a double path ACL entry.
1647 *
1648 * @head: Pointer to "struct ccs_io_buffer".
1649 * @ptr: Pointer to "struct double_path_acl_record".
1650 * @cond: Pointer to "struct condition_list". May be NULL.
1651 *
1652 * Returns true on success, false otherwise.
1653 */
1654 static bool print_double_path_acl(struct ccs_io_buffer *head,
1655 struct double_path_acl_record *ptr,
1656 const struct condition_list *cond)
1657 {
1658 int pos;
1659 const char *atmark1 = "";
1660 const char *atmark2 = "";
1661 const char *filename1;
1662 const char *filename2;
1663 const u8 perm = ptr->perm;
1664 u8 bit;
1665 if (ptr->u1_is_group) {
1666 atmark1 = "@";
1667 filename1 = ptr->u1.group1->group_name->name;
1668 } else {
1669 filename1 = ptr->u1.filename1->name;
1670 }
1671 if (ptr->u2_is_group) {
1672 atmark2 = "@";
1673 filename2 = ptr->u2.group2->group_name->name;
1674 } else {
1675 filename2 = ptr->u2.filename2->name;
1676 }
1677 for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1678 const char *msg;
1679 if (!(perm & (1 << bit)))
1680 continue;
1681 msg = ccs_dp2keyword(bit);
1682 pos = head->read_avail;
1683 if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1684 atmark1, filename1, atmark2, filename2) ||
1685 !ccs_print_condition(head, cond))
1686 goto out;
1687 }
1688 head->read_bit = 0;
1689 return true;
1690 out:
1691 head->read_bit = bit;
1692 head->read_avail = pos;
1693 return false;
1694 }
1695
1696 /**
1697 * print_argv0_acl - Print an argv[0] ACL entry.
1698 *
1699 * @head: Pointer to "struct ccs_io_buffer".
1700 * @ptr: Pointer to "struct argv0_acl_record".
1701 * @cond: Pointer to "struct condition_list". May be NULL.
1702 *
1703 * Returns true on success, false otherwise.
1704 */
1705 static bool print_argv0_acl(struct ccs_io_buffer *head,
1706 struct argv0_acl_record *ptr,
1707 const struct condition_list *cond)
1708 {
1709 int pos = head->read_avail;
1710 if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1711 ptr->filename->name, ptr->argv0->name))
1712 goto out;
1713 if (!ccs_print_condition(head, cond))
1714 goto out;
1715 return true;
1716 out:
1717 head->read_avail = pos;
1718 return false;
1719 }
1720
1721 /**
1722 * print_env_acl - Print an evironment variable name's ACL entry.
1723 *
1724 * @head: Pointer to "struct ccs_io_buffer".
1725 * @ptr: Pointer to "struct env_acl_record".
1726 * @cond: Pointer to "struct condition_list". May be NULL.
1727 *
1728 * Returns true on success, false otherwise.
1729 */
1730 static bool print_env_acl(struct ccs_io_buffer *head,
1731 struct env_acl_record *ptr,
1732 const struct condition_list *cond)
1733 {
1734 int pos = head->read_avail;
1735 if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1736 goto out;
1737 if (!ccs_print_condition(head, cond))
1738 goto out;
1739 return true;
1740 out:
1741 head->read_avail = pos;
1742 return false;
1743 }
1744
1745 /**
1746 * print_capability_acl - Print a capability ACL entry.
1747 *
1748 * @head: Pointer to "struct ccs_io_buffer".
1749 * @ptr: Pointer to "struct capability_acl_record".
1750 * @cond: Pointer to "struct condition_list". May be NULL.
1751 *
1752 * Returns true on success, false otherwise.
1753 */
1754 static bool print_capability_acl(struct ccs_io_buffer *head,
1755 struct capability_acl_record *ptr,
1756 const struct condition_list *cond)
1757 {
1758 int pos = head->read_avail;
1759 if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1760 ccs_cap2keyword(ptr->operation)))
1761 goto out;
1762 if (!ccs_print_condition(head, cond))
1763 goto out;
1764 return true;
1765 out:
1766 head->read_avail = pos;
1767 return false;
1768 }
1769
1770 /**
1771 * print_ipv4_entry - Print IPv4 address of a network ACL entry.
1772 *
1773 * @head: Pointer to "struct ccs_io_buffer".
1774 * @ptr: Pointer to "struct ip_network_acl_record".
1775 *
1776 * Returns true on success, false otherwise.
1777 */
1778 static bool print_ipv4_entry(struct ccs_io_buffer *head,
1779 struct ip_network_acl_record *ptr)
1780 {
1781 const u32 min_address = ptr->u.ipv4.min;
1782 const u32 max_address = ptr->u.ipv4.max;
1783 if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1784 return false;
1785 if (min_address != max_address
1786 && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1787 return false;
1788 return true;
1789 }
1790
1791 /**
1792 * print_ipv6_entry - Print IPv6 address of a network ACL entry.
1793 *
1794 * @head: Pointer to "struct ccs_io_buffer".
1795 * @ptr: Pointer to "struct ip_network_acl_record".
1796 *
1797 * Returns true on success, false otherwise.
1798 */
1799 static bool print_ipv6_entry(struct ccs_io_buffer *head,
1800 struct ip_network_acl_record *ptr)
1801 {
1802 char buf[64];
1803 const struct in6_addr *min_address = ptr->u.ipv6.min;
1804 const struct in6_addr *max_address = ptr->u.ipv6.max;
1805 ccs_print_ipv6(buf, sizeof(buf), min_address);
1806 if (!ccs_io_printf(head, "%s", buf))
1807 return false;
1808 if (min_address != max_address) {
1809 ccs_print_ipv6(buf, sizeof(buf), max_address);
1810 if (!ccs_io_printf(head, "-%s", buf))
1811 return false;
1812 }
1813 return true;
1814 }
1815
1816 /**
1817 * print_port_entry - Print port number of a network ACL entry.
1818 *
1819 * @head: Pointer to "struct ccs_io_buffer".
1820 * @ptr: Pointer to "struct ip_network_acl_record".
1821 *
1822 * Returns true on success, false otherwise.
1823 */
1824 static bool print_port_entry(struct ccs_io_buffer *head,
1825 struct ip_network_acl_record *ptr)
1826 {
1827 const u16 min_port = ptr->min_port, max_port = ptr->max_port;
1828 if (!ccs_io_printf(head, " %u", min_port))
1829 return false;
1830 if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1831 return false;
1832 return true;
1833 }
1834
1835 /**
1836 * print_network_acl - Print a network ACL entry.
1837 *
1838 * @head: Pointer to "struct ccs_io_buffer".
1839 * @ptr: Pointer to "struct ip_network_acl_record".
1840 * @cond: Pointer to "struct condition_list". May be NULL.
1841 *
1842 * Returns true on success, false otherwise.
1843 */
1844 static bool print_network_acl(struct ccs_io_buffer *head,
1845 struct ip_network_acl_record *ptr,
1846 const struct condition_list *cond)
1847 {
1848 int pos = head->read_avail;
1849 if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1850 ccs_net2keyword(ptr->operation_type)))
1851 goto out;
1852 switch (ptr->record_type) {
1853 case IP_RECORD_TYPE_ADDRESS_GROUP:
1854 if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1855 goto out;
1856 break;
1857 case IP_RECORD_TYPE_IPv4:
1858 if (!print_ipv4_entry(head, ptr))
1859 goto out;
1860 break;
1861 case IP_RECORD_TYPE_IPv6:
1862 if (!print_ipv6_entry(head, ptr))
1863 goto out;
1864 break;
1865 }
1866 if (!print_port_entry(head, ptr))
1867 goto out;
1868 if (!ccs_print_condition(head, cond))
1869 goto out;
1870 return true;
1871 out:
1872 head->read_avail = pos;
1873 return false;
1874 }
1875
1876 /**
1877 * print_signal_acl - Print a signal ACL entry.
1878 *
1879 * @head: Pointer to "struct ccs_io_buffer".
1880 * @ptr: Pointer to "struct signale_acl_record".
1881 * @cond: Pointer to "struct condition_list". May be NULL.
1882 *
1883 * Returns true on success, false otherwise.
1884 */
1885 static bool print_signal_acl(struct ccs_io_buffer *head,
1886 struct signal_acl_record *ptr,
1887 const struct condition_list *cond)
1888 {
1889 int pos = head->read_avail;
1890 if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
1891 ptr->sig, ptr->domainname->name))
1892 goto out;
1893 if (!ccs_print_condition(head, cond))
1894 goto out;
1895 return true;
1896 out:
1897 head->read_avail = pos;
1898 return false;
1899 }
1900
1901 /**
1902 * print_execute_handler_record - Print an execute handler ACL entry.
1903 *
1904 * @head: Pointer to "struct ccs_io_buffer".
1905 * @keyword: Name of the keyword.
1906 * @ptr: Pointer to "struct execute_handler_record".
1907 *
1908 * Returns true on success, false otherwise.
1909 */
1910 static bool print_execute_handler_record(struct ccs_io_buffer *head,
1911 const char *keyword,
1912 struct execute_handler_record *ptr)
1913 {
1914 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1915 }
1916
1917 /**
1918 * print_entry - Print an ACL entry.
1919 *
1920 * @head: Pointer to "struct ccs_io_buffer".
1921 * @ptr: Pointer to an ACL entry.
1922 *
1923 * Returns true on success, false otherwise.
1924 */
1925 static bool print_entry(struct ccs_io_buffer *head, struct acl_info *ptr)
1926 {
1927 const struct condition_list *cond = ccs_get_condition_part(ptr);
1928 const u8 acl_type = ccs_acl_type2(ptr);
1929 if (acl_type & ACL_DELETED)
1930 return true;
1931 if (acl_type == TYPE_SINGLE_PATH_ACL) {
1932 struct single_path_acl_record *acl
1933 = container_of(ptr, struct single_path_acl_record,
1934 head);
1935 return print_single_path_acl(head, acl, cond);
1936 }
1937 if (acl_type == TYPE_DOUBLE_PATH_ACL) {
1938 struct double_path_acl_record *acl
1939 = container_of(ptr, struct double_path_acl_record,
1940 head);
1941 return print_double_path_acl(head, acl, cond);
1942 }
1943 if (acl_type == TYPE_ARGV0_ACL) {
1944 struct argv0_acl_record *acl
1945 = container_of(ptr, struct argv0_acl_record, head);
1946 return print_argv0_acl(head, acl, cond);
1947 }
1948 if (acl_type == TYPE_ENV_ACL) {
1949 struct env_acl_record *acl
1950 = container_of(ptr, struct env_acl_record, head);
1951 return print_env_acl(head, acl, cond);
1952 }
1953 if (acl_type == TYPE_CAPABILITY_ACL) {
1954 struct capability_acl_record *acl
1955 = container_of(ptr, struct capability_acl_record, head);
1956 return print_capability_acl(head, acl, cond);
1957 }
1958 if (acl_type == TYPE_IP_NETWORK_ACL) {
1959 struct ip_network_acl_record *acl
1960 = container_of(ptr, struct ip_network_acl_record, head);
1961 return print_network_acl(head, acl, cond);
1962 }
1963 if (acl_type == TYPE_SIGNAL_ACL) {
1964 struct signal_acl_record *acl
1965 = container_of(ptr, struct signal_acl_record, head);
1966 return print_signal_acl(head, acl, cond);
1967 }
1968 if (acl_type == TYPE_EXECUTE_HANDLER) {
1969 struct execute_handler_record *acl
1970 = container_of(ptr, struct execute_handler_record,
1971 head);
1972 const char *keyword = KEYWORD_EXECUTE_HANDLER;
1973 return print_execute_handler_record(head, keyword, acl);
1974 }
1975 if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {
1976 struct execute_handler_record *acl
1977 = container_of(ptr, struct execute_handler_record,
1978 head);
1979 const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;
1980 return print_execute_handler_record(head, keyword, acl);
1981 }
1982 /* Workaround for gcc 3.2.2's inline bug. */
1983 if (acl_type & ACL_DELETED)
1984 return true;
1985 BUG(); /* This must not happen. */
1986 return false;
1987 }
1988
1989 /**
1990 * read_domain_policy - Read domain policy.
1991 *
1992 * @head: Pointer to "struct ccs_io_buffer".
1993 *
1994 * Returns 0.
1995 */
1996 static int read_domain_policy(struct ccs_io_buffer *head)
1997 {
1998 struct list1_head *dpos;
1999 struct list1_head *apos;
2000 if (head->read_eof)
2001 return 0;
2002 if (head->read_step == 0)
2003 head->read_step = 1;
2004 list1_for_each_cookie(dpos, head->read_var1, &domain_list) {
2005 struct domain_info *domain;
2006 const char *quota_exceeded = "";
2007 const char *transition_failed = "";
2008 const char *ignore_global_allow_read = "";
2009 const char *ignore_global_allow_env = "";
2010 domain = list1_entry(dpos, struct domain_info, list);
2011 if (head->read_step != 1)
2012 goto acl_loop;
2013 if (domain->is_deleted && !head->read_single_domain)
2014 continue;
2015 /* Print domainname and flags. */
2016 if (domain->quota_warned)
2017 quota_exceeded = "quota_exceeded\n";
2018 if (domain->flags & DOMAIN_FLAGS_TRANSITION_FAILED)
2019 transition_failed = "transition_failed\n";
2020 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
2021 ignore_global_allow_read
2022 = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
2023 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV)
2024 ignore_global_allow_env
2025 = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
2026 if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
2027 "%s%s%s%s\n", domain->domainname->name,
2028 domain->profile, quota_exceeded,
2029 transition_failed,
2030 ignore_global_allow_read,
2031 ignore_global_allow_env))
2032 return 0;
2033 head->read_step = 2;
2034 acl_loop:
2035 if (head->read_step == 3)
2036 goto tail_mark;
2037 /* Print ACL entries in the domain. */
2038 list1_for_each_cookie(apos, head->read_var2,
2039 &domain->acl_info_list) {
2040 struct acl_info *ptr
2041 = list1_entry(apos, struct acl_info, list);
2042 if (!print_entry(head, ptr))
2043 return 0;
2044 }
2045 head->read_step = 3;
2046 tail_mark:
2047 if (!ccs_io_printf(head, "\n"))
2048 return 0;
2049 head->read_step = 1;
2050 if (head->read_single_domain)
2051 break;
2052 }
2053 head->read_eof = true;
2054 return 0;
2055 }
2056
2057 #endif
2058
2059 /**
2060 * write_domain_profile - Assign profile for specified domain.
2061 *
2062 * @head: Pointer to "struct ccs_io_buffer".
2063 *
2064 * Returns 0 on success, -EINVAL otherwise.
2065 *
2066 * This is equivalent to doing
2067 *
2068 * ( echo "select " $domainname; echo "use_profile " $profile ) |
2069 * /usr/lib/ccs/loadpolicy -d
2070 */
2071 static int write_domain_profile(struct ccs_io_buffer *head)
2072 {
2073 char *data = head->write_buf;
2074 char *cp = strchr(data, ' ');
2075 struct domain_info *domain;
2076 unsigned int profile;
2077 if (!cp)
2078 return -EINVAL;
2079 *cp = '\0';
2080 domain = ccs_find_domain(cp + 1);
2081 profile = simple_strtoul(data, NULL, 10);
2082 if (domain && profile < MAX_PROFILES
2083 && (profile_ptr[profile] || !sbin_init_started))
2084 domain->profile = (u8) profile;
2085 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
2086 return 0;
2087 }
2088
2089 /**
2090 * read_domain_profile - Read only domainname and profile.
2091 *
2092 * @head: Pointer to "struct ccs_io_buffer".
2093 *
2094 * Returns list of profile number and domainname pairs.
2095 *
2096 * This is equivalent to doing
2097 *
2098 * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
2099 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
2100 * domainname = $0; } else if ( $1 == "use_profile" ) {
2101 * print $2 " " domainname; domainname = ""; } } ; '
2102 */
2103 static int read_domain_profile(struct ccs_io_buffer *head)
2104 {
2105 struct list1_head *pos;
2106 if (head->read_eof)
2107 return 0;
2108 list1_for_each_cookie(pos, head->read_var1, &domain_list) {
2109 struct domain_info *domain;
2110 domain = list1_entry(pos, struct domain_info, list);
2111 if (domain->is_deleted)
2112 continue;
2113 if (!ccs_io_printf(head, "%u %s\n", domain->profile,
2114 domain->domainname->name))
2115 return 0;
2116 }
2117 head->read_eof = true;
2118 return 0;
2119 }
2120
2121 /**
2122 * write_pid: Specify PID to obtain domainname.
2123 *
2124 * @head: Pointer to "struct ccs_io_buffer".
2125 *
2126 * Returns 0.
2127 */
2128 static int write_pid(struct ccs_io_buffer *head)
2129 {
2130 head->read_eof = false;
2131 return 0;
2132 }
2133
2134 /**
2135 * read_pid - Read information of a process.
2136 *
2137 * @head: Pointer to "struct ccs_io_buffer".
2138 *
2139 * Returns the domainname which the specified PID is in or
2140 * process information of the specified PID on success,
2141 * empty string otherwise.
2142 */
2143 static int read_pid(struct ccs_io_buffer *head)
2144 {
2145 /* Accessing write_buf is safe because head->io_sem is held. */
2146 char *buf = head->write_buf;
2147 bool task_info = false;
2148 unsigned int pid;
2149 struct task_struct *p;
2150 struct domain_info *domain = NULL;
2151 u32 tomoyo_flags = 0;
2152 if (head->read_avail || head->read_eof)
2153 goto done;
2154 head->read_eof = true;
2155 if (str_starts(&buf, "info "))
2156 task_info = true;
2157 pid = (unsigned int) simple_strtoul(buf, NULL, 10);
2158 /***** CRITICAL SECTION START *****/
2159 read_lock(&tasklist_lock);
2160 p = find_task_by_pid(pid);
2161 if (p) {
2162 domain = p->domain_info;
2163 tomoyo_flags = p->tomoyo_flags;
2164 }
2165 read_unlock(&tasklist_lock);
2166 /***** CRITICAL SECTION END *****/
2167 if (!domain)
2168 goto done;
2169 if (!task_info)
2170 ccs_io_printf(head, "%u %u %s", pid, domain->profile,
2171 domain->domainname->name);
2172 else
2173 ccs_io_printf(head, "%u manager=%s execute_handler=%s "
2174 "state[0]=%u state[1]=%u state[2]=%u", pid,
2175 tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER ?
2176 "yes" : "no",
2177 tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER ?
2178 "yes" : "no",
2179 (u8) (tomoyo_flags >> 24),
2180 (u8) (tomoyo_flags >> 16),
2181 (u8) (tomoyo_flags >> 8));
2182 done:
2183 return 0;
2184 }
2185
2186 #ifdef CONFIG_TOMOYO
2187
2188 /**
2189 * write_exception_policy - Write exception policy.
2190 *
2191 * @head: Pointer to "struct ccs_io_buffer".
2192 *
2193 * Returns 0 on success, negative value otherwise.
2194 */
2195 static int write_exception_policy(struct ccs_io_buffer *head)
2196 {
2197 char *data = head->write_buf;
2198 bool is_delete = str_starts(&data, KEYWORD_DELETE);
2199 if (str_starts(&data, KEYWORD_KEEP_DOMAIN))
2200 return ccs_write_domain_keeper_policy(data, false, is_delete);
2201 if (str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))
2202 return ccs_write_domain_keeper_policy(data, true, is_delete);
2203 if (str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))
2204 return ccs_write_domain_initializer_policy(data, false,
2205 is_delete);
2206 if (str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))
2207 return ccs_write_domain_initializer_policy(data, true,
2208 is_delete);
2209 if (str_starts(&data, KEYWORD_ALIAS))
2210 return ccs_write_alias_policy(data, is_delete);
2211 if (str_starts(&data, KEYWORD_AGGREGATOR))
2212 return ccs_write_aggregator_policy(data, is_delete);
2213 if (str_starts(&data, KEYWORD_ALLOW_READ))
2214 return ccs_write_globally_readable_policy(data, is_delete);
2215 if (str_starts(&data, KEYWORD_ALLOW_ENV))
2216 return ccs_write_globally_usable_env_policy(data, is_delete);
2217 if (str_starts(&data, KEYWORD_FILE_PATTERN))
2218 return ccs_write_pattern_policy(data, is_delete);
2219 if (str_starts(&data, KEYWORD_PATH_GROUP))
2220 return ccs_write_path_group_policy(data, is_delete);
2221 if (str_starts(&data, KEYWORD_DENY_REWRITE))
2222 return ccs_write_no_rewrite_policy(data, is_delete);
2223 if (str_starts(&data, KEYWORD_ADDRESS_GROUP))
2224 return ccs_write_address_group_policy(data, is_delete);
2225 return -EINVAL;
2226 }
2227
2228 /**
2229 * read_exception_policy - Read exception policy.
2230 *
2231 * @head: Pointer to "struct ccs_io_buffer".
2232 *
2233 * Returns 0 on success, -EINVAL otherwise.
2234 */
2235 static int read_exception_policy(struct ccs_io_buffer *head)
2236 {
2237 if (!head->read_eof) {
2238 switch (head->read_step) {
2239 case 0:
2240 head->read_var2 = NULL;
2241 head->read_step = 1;
2242 case 1:
2243 if (!ccs_read_domain_keeper_policy(head))
2244 break;
2245 head->read_var2 = NULL;
2246 head->read_step = 2;
2247 case 2:
2248 if (!ccs_read_globally_readable_policy(head))
2249 break;
2250 head->read_var2 = NULL;
2251 head->read_step = 3;
2252 case 3:
2253 if (!ccs_read_globally_usable_env_policy(head))
2254 break;
2255 head->read_var2 = NULL;
2256 head->read_step = 4;
2257 case 4:
2258 if (!ccs_read_domain_initializer_policy(head))
2259 break;
2260 head->read_var2 = NULL;
2261 head->read_step = 5;
2262 case 5:
2263 if (!ccs_read_alias_policy(head))
2264 break;
2265 head->read_var2 = NULL;
2266 head->read_step = 6;
2267 case 6:
2268 if (!ccs_read_aggregator_policy(head))
2269 break;
2270 head->read_var2 = NULL;
2271 head->read_step = 7;
2272 case 7:
2273 if (!ccs_read_file_pattern(head))
2274 break;
2275 head->read_var2 = NULL;
2276 head->read_step = 8;
2277 case 8:
2278 if (!ccs_read_no_rewrite_policy(head))
2279 break;
2280 head->read_var2 = NULL;
2281 head->read_step = 9;
2282 case 9:
2283 if (!ccs_read_path_group_policy(head))
2284 break;
2285 head->read_var1 = NULL;
2286 head->read_var2 = NULL;
2287 head->read_step = 10;
2288 case 10:
2289 if (!ccs_read_address_group_policy(head))
2290 break;
2291 head->read_eof = true;
2292 break;
2293 default:
2294 return -EINVAL;
2295 }
2296 }
2297 return 0;
2298 }
2299
2300 #endif
2301
2302 #ifdef CONFIG_SAKURA
2303
2304 /**
2305 * write_system_policy - Write system policy.
2306 *
2307 * @head: Pointer to "struct ccs_io_buffer".
2308 *
2309 * Returns 0 on success, negative value otherwise.
2310 */
2311 static int write_system_policy(struct ccs_io_buffer *head)
2312 {
2313 char *data = head->write_buf;
2314 bool is_delete = false;
2315 if (str_starts(&data, KEYWORD_DELETE))
2316 is_delete = true;
2317 if (str_starts(&data, KEYWORD_ALLOW_MOUNT))
2318 return ccs_write_mount_policy(data, is_delete);
2319 if (str_starts(&data, KEYWORD_DENY_UNMOUNT))
2320 return ccs_write_no_umount_policy(data, is_delete);
2321 if (str_starts(&data, KEYWORD_ALLOW_CHROOT))
2322 return ccs_write_chroot_policy(data, is_delete);
2323 if (str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))
2324 return ccs_write_pivot_root_policy(data, is_delete);
2325 if (str_starts(&data, KEYWORD_DENY_AUTOBIND))
2326 return ccs_write_reserved_port_policy(data, is_delete);
2327 return -EINVAL;
2328 }
2329
2330 /**
2331 * read_system_policy - Read system policy.
2332 *
2333 * @head: Pointer to "struct ccs_io_buffer".
2334 *
2335 * Returns 0 on success, -EINVAL otherwise.
2336 */
2337 static int read_system_policy(struct ccs_io_buffer *head)
2338 {
2339 if (!head->read_eof) {
2340 switch (head->read_step) {
2341 case 0:
2342 head->read_var2 = NULL;
2343 head->read_step = 1;
2344 case 1:
2345 if (!ccs_read_mount_policy(head))
2346 break;
2347 head->read_var2 = NULL;
2348 head->read_step = 2;
2349 case 2:
2350 if (!ccs_read_no_umount_policy(head))
2351 break;
2352 head->read_var2 = NULL;
2353 head->read_step = 3;
2354 case 3:
2355 if (!ccs_read_chroot_policy(head))
2356 break;
2357 head->read_var2 = NULL;
2358 head->read_step = 4;
2359 case 4:
2360 if (!ccs_read_pivot_root_policy(head))
2361 break;
2362 head->read_var2 = NULL;
2363 head->read_step = 5;
2364 case 5:
2365 if (!ccs_read_reserved_port_policy(head))
2366 break;
2367 head->read_eof = true;
2368 break;
2369 default:
2370 return -EINVAL;
2371 }
2372 }
2373 return 0;
2374 }
2375
2376 #endif
2377
2378 /* Path to the policy loader. The default is /sbin/ccs-init. */
2379 static const char *ccs_loader;
2380
2381 /**
2382 * loader_setup - Specify the policy loader to use.
2383 *
2384 * @str: Path to the policy loader.
2385 *
2386 * Returns 0.
2387 */
2388 static int __init loader_setup(char *str)
2389 {
2390 ccs_loader = str;
2391 return 0;
2392 }
2393
2394 __setup("CCS_loader=", loader_setup);
2395
2396 /**
2397 * policy_loader_exists - Check whether /sbin/ccs-init exists.
2398 *
2399 * Returns true if /sbin/ccs-init exists, false otherwise.
2400 */
2401 static bool policy_loader_exists(void)
2402 {
2403 /*
2404 * Don't activate MAC if the path given by 'CCS_loader=' option doesn't
2405 * exist. If the initrd includes /sbin/init but real-root-dev has not
2406 * mounted on / yet, activating MAC will block the system since
2407 * policies are not loaded yet.
2408 * Thus, let do_execve() call this function everytime.
2409 */
2410 struct nameidata nd;
2411 if (!ccs_loader)
2412 ccs_loader = "/sbin/ccs-init";
2413 if (path_lookup(ccs_loader, lookup_flags, &nd)) {
2414 printk(KERN_INFO "Not activating Mandatory Access Control now "
2415 "since %s doesn't exist.\n", ccs_loader);
2416 return false;
2417 }
2418 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
2419 path_put(&nd.path);
2420 #else
2421 path_release(&nd);
2422 #endif
2423 return true;
2424 }
2425
2426 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
2427 /**
2428 * run_ccs_loader - Start /sbin/ccs-init .
2429 *
2430 * @unused: Not used.
2431 *
2432 * Returns PID of /sbin/ccs-init on success, negative value otherwise.
2433 */
2434 static int run_ccs_loader(void *unused)
2435 {
2436 char *argv[2];
2437 char *envp[3];
2438 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2439 ccs_loader);
2440 argv[0] = (char *) ccs_loader;
2441 argv[1] = NULL;
2442 envp[0] = "HOME=/";
2443 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2444 envp[2] = NULL;
2445 return exec_usermodehelper(argv[0], argv, envp);
2446 }
2447 #endif
2448
2449 /**
2450 * ccs_load_policy - Run external policy loader to load policy.
2451 *
2452 * @filename: The program about to start.
2453 *
2454 * This function checks whether @filename is /sbin/init , and if so
2455 * invoke /sbin/ccs-init and wait for the termination of /sbin/ccs-init
2456 * and then continues invocation of /sbin/init.
2457 * /sbin/ccs-init reads policy files in /etc/ccs/ directory and
2458 * writes to /proc/ccs/ interfaces.
2459 *
2460 * Returns nothing.
2461 */
2462 void ccs_load_policy(const char *filename)
2463 {
2464 if (sbin_init_started)
2465 return;
2466 /*
2467 * Check filename is /sbin/init or /sbin/ccs-start.
2468 * /sbin/ccs-start is a dummy filename in case where /sbin/init can't
2469 * be passed.
2470 * You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start".
2471 */
2472 if (strcmp(filename, "/sbin/init") &&
2473 strcmp(filename, "/sbin/ccs-start"))
2474 return;
2475 if (!policy_loader_exists())
2476 return;
2477 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
2478 {
2479 char *argv[2];
2480 char *envp[3];
2481 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2482 ccs_loader);
2483 argv[0] = (char *) ccs_loader;
2484 argv[1] = NULL;
2485 envp[0] = "HOME=/";
2486 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2487 envp[2] = NULL;
2488 call_usermodehelper(argv[0], argv, envp, 1);
2489 }
2490 #elif defined(TASK_DEAD)
2491 {
2492 /* Copied from kernel/kmod.c */
2493 struct task_struct *task = current;
2494 pid_t pid = kernel_thread(run_ccs_loader, NULL, 0);
2495 sigset_t tmpsig;
2496 spin_lock_irq(&task->sighand->siglock);
2497 tmpsig = task->blocked;
2498 siginitsetinv(&task->blocked,
2499 sigmask(SIGKILL) | sigmask(SIGSTOP));
2500 recalc_sigpending();
2501 spin_unlock_irq(&current->sighand->siglock);
2502 if (pid >= 0)
2503 waitpid(pid, NULL, __WCLONE);
2504 spin_lock_irq(&task->sighand->siglock);
2505 task->blocked = tmpsig;
2506 recalc_sigpending();
2507 spin_unlock_irq(&task->sighand->siglock);
2508 }
2509 #else
2510 {
2511 /* Copied from kernel/kmod.c */
2512 struct task_struct *task = current;
2513 pid_t pid = kernel_thread(run_ccs_loader, NULL, 0);
2514 sigset_t tmpsig;
2515 spin_lock_irq(&task->sigmask_lock);
2516 tmpsig = task->blocked;
2517 siginitsetinv(&task->blocked,
2518 sigmask(SIGKILL) | sigmask(SIGSTOP));
2519 recalc_sigpending(task);
2520 spin_unlock_irq(&task->sigmask_lock);
2521 if (pid >= 0)
2522 waitpid(pid, NULL, __WCLONE);
2523 spin_lock_irq(&task->sigmask_lock);
2524 task->blocked = tmpsig;
2525 recalc_sigpending(task);
2526 spin_unlock_irq(&task->sigmask_lock);
2527 }
2528 #endif
2529 #ifdef CONFIG_SAKURA
2530 printk(KERN_INFO "SAKURA: 1.6.5-pre 2008/11/04\n");
2531 #endif
2532 #ifdef CONFIG_TOMOYO
2533 printk(KERN_INFO "TOMOYO: 1.6.5-pre 2008/11/04\n");
2534 #endif
2535 printk(KERN_INFO "Mandatory Access Control activated.\n");
2536 sbin_init_started = true;
2537 ccs_log_level = KERN_WARNING;
2538 { /* Check all profiles currently assigned to domains are defined. */
2539 struct domain_info *domain;
2540 list1_for_each_entry(domain, &domain_list, list) {
2541 const u8 profile = domain->profile;
2542 if (profile_ptr[profile])
2543 continue;
2544 panic("Profile %u (used by '%s') not defined.\n",
2545 profile, domain->domainname->name);
2546 }
2547 }
2548 }
2549
2550 /* Wait queue for query_list. */
2551 static DECLARE_WAIT_QUEUE_HEAD(query_wait);
2552
2553 /* Lock for manipulating query_list. */
2554 static DEFINE_SPINLOCK(query_list_lock);
2555
2556 /* Structure for query. */
2557 struct query_entry {
2558 struct list_head list;
2559 char *query;
2560 int query_len;
2561 unsigned int serial;
2562 int timer;
2563 int answer;
2564 };
2565
2566 /* The list for "struct query_entry". */
2567 static LIST_HEAD(query_list);
2568
2569 /* Number of "struct file" referring /proc/ccs/query interface. */
2570 static atomic_t queryd_watcher = ATOMIC_INIT(0);
2571
2572 /**
2573 * ccs_check_supervisor - Ask for the supervisor's decision.
2574 *
2575 * @r: Pointer to "struct ccs_request_info".
2576 * @fmt: The printf()'s format string, followed by parameters.
2577 *
2578 * Returns 0 if the supervisor decided to permit the access request which
2579 * violated the policy in enforcing mode, 1 if the supervisor decided to
2580 * retry the access request which violated the policy in enforcing mode,
2581 * -EPERM otherwise.
2582 */
2583 int ccs_check_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2584 {
2585 va_list args;
2586 int error = -EPERM;
2587 int pos;
2588 int len;
2589 static unsigned int serial;
2590 struct query_entry *query_entry = NULL;
2591 char *header;
2592 if (!r->domain)
2593 r->domain = current->domain_info;
2594 if (!atomic_read(&queryd_watcher)) {
2595 int i;
2596 if (current->tomoyo_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2597 return -EPERM;
2598 for (i = 0; i < ccs_check_flags(r->domain, CCS_SLEEP_PERIOD);
2599 i++) {
2600 set_current_state(TASK_INTERRUPTIBLE);
2601 schedule_timeout(HZ / 10);
2602 }
2603 return -EPERM;
2604 }
2605 va_start(args, fmt);
2606 len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2607 va_end(args);
2608 #ifdef CONFIG_TOMOYO
2609 header = ccs_init_audit_log(&len, r);
2610 #else
2611 header = ccs_alloc(1);
2612 #endif
2613 if (!header)
2614 goto out;
2615 query_entry = ccs_alloc(sizeof(*query_entry));
2616 if (!query_entry)
2617 goto out;
2618 query_entry->query = ccs_alloc(len);
2619 if (!query_entry->query)
2620 goto out;
2621 INIT_LIST_HEAD(&query_entry->list);
2622 /***** CRITICAL SECTION START *****/
2623 spin_lock(&query_list_lock);
2624 query_entry->serial = serial++;
2625 spin_unlock(&query_list_lock);
2626 /***** CRITICAL SECTION END *****/
2627 pos = snprintf(query_entry->query, len - 1, "Q%u-%hu\n%s",
2628 query_entry->serial, r->retry, header);
2629 ccs_free(header);
2630 header = NULL;
2631 va_start(args, fmt);
2632 vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args);
2633 query_entry->query_len = strlen(query_entry->query) + 1;
2634 va_end(args);
2635 /***** CRITICAL SECTION START *****/
2636 spin_lock(&query_list_lock);
2637 list_add_tail(&query_entry->list, &query_list);
2638 spin_unlock(&query_list_lock);
2639 /***** CRITICAL SECTION END *****/
2640 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2641 /* Give 10 seconds for supervisor's opinion. */
2642 for (query_entry->timer = 0; atomic_read(&queryd_watcher)
2643 && query_entry->timer < 100; query_entry->timer++) {
2644 wake_up(&query_wait);
2645 set_current_state(TASK_INTERRUPTIBLE);
2646 schedule_timeout(HZ / 10);
2647 if (query_entry->answer)
2648 break;
2649 }
2650 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2651 /***** CRITICAL SECTION START *****/
2652 spin_lock(&query_list_lock);
2653 list_del(&query_entry->list);
2654 spin_unlock(&query_list_lock);
2655 /***** CRITICAL SECTION END *****/
2656 switch (query_entry->answer) {
2657 case 3: /* Asked to retry by administrator. */
2658 error = 1;
2659 r->retry++;
2660 break;
2661 case 1:
2662 /* Granted by administrator. */
2663 error = 0;
2664 break;
2665 case 0:
2666 /* Timed out. */
2667 break;
2668 default:
2669 /* Rejected by administrator. */
2670 break;
2671 }
2672 out:
2673 if (query_entry)
2674 ccs_free(query_entry->query);
2675 ccs_free(query_entry);
2676 ccs_free(header);
2677 return error;
2678 }
2679
2680 /**
2681 * poll_query - poll() for /proc/ccs/query.
2682 *
2683 * @file: Pointer to "struct file".
2684 * @wait: Pointer to "poll_table".
2685 *
2686 * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2687 *
2688 * Waits for access requests which violated policy in enforcing mode.
2689 */
2690 static int poll_query(struct file *file, poll_table *wait)
2691 {
2692 struct list_head *tmp;
2693 bool found = false;
2694 u8 i;
2695 for (i = 0; i < 2; i++) {
2696 /***** CRITICAL SECTION START *****/
2697 spin_lock(&query_list_lock);
2698 list_for_each(tmp, &query_list) {
2699 struct query_entry *ptr
2700 = list_entry(tmp, struct query_entry, list);
2701 if (ptr->answer)
2702 continue;
2703 found = true;
2704 break;
2705 }
2706 spin_unlock(&query_list_lock);
2707 /***** CRITICAL SECTION END *****/
2708 if (found)
2709 return POLLIN | POLLRDNORM;
2710 if (i)
2711 break;
2712 poll_wait(file, &query_wait, wait);
2713 }
2714 return 0;
2715 }
2716
2717 /**
2718 * read_query - Read access requests which violated policy in enforcing mode.
2719 *
2720 * @head: Pointer to "struct ccs_io_buffer".
2721 *
2722 * Returns 0.
2723 */
2724 static int read_query(struct ccs_io_buffer *head)
2725 {
2726 struct list_head *tmp;
2727 int pos = 0;
2728 int len = 0;
2729 char *buf;
2730 if (head->read_avail)
2731 return 0;
2732 if (head->read_buf) {
2733 ccs_free(head->read_buf);
2734 head->read_buf = NULL;
2735 head->readbuf_size = 0;
2736 }
2737 /***** CRITICAL SECTION START *****/
2738 spin_lock(&query_list_lock);
2739 list_for_each(tmp, &query_list) {
2740 struct query_entry *ptr
2741 = list_entry(tmp, struct query_entry, list);
2742 if (ptr->answer)
2743 continue;
2744 if (pos++ != head->read_step)
2745 continue;
2746 len = ptr->query_len;
2747 break;
2748 }
2749 spin_unlock(&query_list_lock);
2750 /***** CRITICAL SECTION END *****/
2751 if (!len) {
2752 head->read_step = 0;
2753 return 0;
2754 }
2755 buf = ccs_alloc(len);
2756 if (!buf)
2757 return 0;
2758 pos = 0;
2759 /***** CRITICAL SECTION START *****/
2760 spin_lock(&query_list_lock);
2761 list_for_each(tmp, &query_list) {
2762 struct query_entry *ptr
2763 = list_entry(tmp, struct query_entry, list);
2764 if (ptr->answer)
2765 continue;
2766 if (pos++ != head->read_step)
2767 continue;
2768 /*
2769 * Some query can be skipped because query_list
2770 * can change, but I don't care.
2771 */
2772 if (len == ptr->query_len)
2773 memmove(buf, ptr->query, len);
2774 break;
2775 }
2776 spin_unlock(&query_list_lock);
2777 /***** CRITICAL SECTION END *****/
2778 if (buf[0]) {
2779 head->read_avail = len;
2780 head->readbuf_size = head->read_avail;
2781 head->read_buf = buf;
2782 head->read_step++;
2783 } else {
2784 ccs_free(buf);
2785 }
2786 return 0;
2787 }
2788
2789 /**
2790 * write_answer - Write the supervisor's decision.
2791 *
2792 * @head: Pointer to "struct ccs_io_buffer".
2793 *
2794 * Returns 0 on success, -EINVAL otherwise.
2795 */
2796 static int write_answer(struct ccs_io_buffer *head)
2797 {
2798 char *data = head->write_buf;
2799 struct list_head *tmp;
2800 unsigned int serial;
2801 unsigned int answer;
2802 /***** CRITICAL SECTION START *****/
2803 spin_lock(&query_list_lock);
2804 list_for_each(tmp, &query_list) {
2805 struct query_entry *ptr
2806 = list_entry(tmp, struct query_entry, list);
2807 ptr->timer = 0;
2808 }
2809 spin_unlock(&query_list_lock);
2810 /***** CRITICAL SECTION END *****/
2811 if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
2812 return -EINVAL;
2813 /***** CRITICAL SECTION START *****/
2814 spin_lock(&query_list_lock);
2815 list_for_each(tmp, &query_list) {
2816 struct query_entry *ptr
2817 = list_entry(tmp, struct query_entry, list);
2818 if (ptr->serial != serial)
2819 continue;
2820 if (!ptr->answer)
2821 ptr->answer = answer;
2822 break;
2823 }
2824 spin_unlock(&query_list_lock);
2825 /***** CRITICAL SECTION END *****/
2826 return 0;
2827 }
2828
2829 /* Policy updates counter. */
2830 static unsigned int updates_counter[MAX_CCS_UPDATES_COUNTER];
2831
2832 /* Policy updates counter lock. */
2833 static DEFINE_SPINLOCK(updates_counter_lock);
2834
2835 /**
2836 * ccs_update_counter - Increment policy change counter.
2837 *
2838 * @index: Type of policy.
2839 *
2840 * Returns nothing.
2841 */
2842 void ccs_update_counter(const unsigned char index)
2843 {
2844 /***** CRITICAL SECTION START *****/
2845 spin_lock(&updates_counter_lock);
2846 if (index < MAX_CCS_UPDATES_COUNTER)
2847 updates_counter[index]++;
2848 spin_unlock(&updates_counter_lock);
2849 /***** CRITICAL SECTION END *****/
2850 }
2851
2852 /**
2853 * read_updates_counter - Check for policy change counter.
2854 *
2855 * @head: Pointer to "struct ccs_io_buffer".
2856 *
2857 * Returns how many times policy has changed since the previous check.
2858 */
2859 static int read_updates_counter(struct ccs_io_buffer *head)
2860 {
2861 if (!head->read_eof) {
2862 unsigned int counter[MAX_CCS_UPDATES_COUNTER];
2863 /***** CRITICAL SECTION START *****/
2864 spin_lock(&updates_counter_lock);
2865 memmove(counter, updates_counter, sizeof(updates_counter));
2866 memset(updates_counter, 0, sizeof(updates_counter));
2867 spin_unlock(&updates_counter_lock);
2868 /***** CRITICAL SECTION END *****/
2869 ccs_io_printf(head,
2870 "/proc/ccs/system_policy: %10u\n"
2871 "/proc/ccs/domain_policy: %10u\n"
2872 "/proc/ccs/exception_policy: %10u\n"
2873 "/proc/ccs/profile: %10u\n"
2874 "/proc/ccs/query: %10u\n"
2875 "/proc/ccs/manager: %10u\n"
2876 "/proc/ccs/grant_log: %10u\n"
2877 "/proc/ccs/reject_log: %10u\n",
2878 counter[CCS_UPDATES_COUNTER_SYSTEM_POLICY],
2879 counter[CCS_UPDATES_COUNTER_DOMAIN_POLICY],
2880 counter[CCS_UPDATES_COUNTER_EXCEPTION_POLICY],
2881 counter[CCS_UPDATES_COUNTER_PROFILE],
2882 counter[CCS_UPDATES_COUNTER_QUERY],
2883 counter[CCS_UPDATES_COUNTER_MANAGER],
2884 counter[CCS_UPDATES_COUNTER_GRANT_LOG],
2885 counter[CCS_UPDATES_COUNTER_REJECT_LOG]);
2886 head->read_eof = true;
2887 }
2888 return 0;
2889 }
2890
2891 /**
2892 * read_version: Get version.
2893 *
2894 * @head: Pointer to "struct ccs_io_buffer".
2895 *
2896 * Returns version information.
2897 */
2898 static int read_version(struct ccs_io_buffer *head)
2899 {
2900 if (!head->read_eof) {
2901 ccs_io_printf(head, "1.6.5-pre");
2902 head->read_eof = true;
2903 }
2904 return 0;
2905 }
2906
2907 /**
2908 * read_self_domain - Get the current process's domainname.
2909 *
2910 * @head: Pointer to "struct ccs_io_buffer".
2911 *
2912 * Returns the current process's domainname.
2913 */
2914 static int read_self_domain(struct ccs_io_buffer *head)
2915 {
2916 if (!head->read_eof) {
2917 /*
2918 * current->domain_info->domainname != NULL
2919 * because every process belongs to a domain and
2920 * the domain's name cannot be NULL.
2921 */
2922 ccs_io_printf(head, "%s",
2923 current->domain_info->domainname->name);
2924 head->read_eof = true;
2925 }
2926 return 0;
2927 }
2928
2929 /**
2930 * ccs_open_control - open() for /proc/ccs/ interface.
2931 *
2932 * @type: Type of interface.
2933 * @file: Pointer to "struct file".
2934 *
2935 * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
2936 */
2937 int ccs_open_control(const u8 type, struct file *file)
2938 {
2939 struct ccs_io_buffer *head = ccs_alloc(sizeof(*head));
2940 if (!head)
2941 return -ENOMEM;
2942 mutex_init(&head->io_sem);
2943 switch (type) {
2944 #ifdef CONFIG_SAKURA
2945 case CCS_SYSTEMPOLICY: /* /proc/ccs/system_policy */
2946 head->write = write_system_policy;
2947 head->read = read_system_policy;
2948 break;
2949 #endif
2950 #ifdef CONFIG_TOMOYO
2951 case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2952 head->write = write_domain_policy;
2953 head->read = read_domain_policy;
2954 break;
2955 case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2956 head->write = write_exception_policy;
2957 head->read = read_exception_policy;
2958 break;
2959 case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2960 head->poll = ccs_poll_grant_log;
2961 head->read = ccs_read_grant_log;
2962 break;
2963 case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2964 head->poll = ccs_poll_reject_log;
2965 head->read = ccs_read_reject_log;
2966 break;
2967 #endif
2968 case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
2969 head->read = read_self_domain;
2970 break;
2971 case CCS_DOMAIN_STATUS: /* /proc/ccs/.domain_status */
2972 head->write = write_domain_profile;
2973 head->read = read_domain_profile;
2974 break;
2975 case CCS_PROCESS_STATUS: /* /proc/ccs/.process_status */
2976 head->write = write_pid;
2977 head->read = read_pid;
2978 break;
2979 case CCS_VERSION: /* /proc/ccs/version */
2980 head->read = read_version;
2981 head->readbuf_size = 128;
2982 break;
2983 case CCS_MEMINFO: /* /proc/ccs/meminfo */
2984 head->write = ccs_write_memory_quota;
2985 head->read = ccs_read_memory_counter;
2986 head->readbuf_size = 512;
2987 break;
2988 case CCS_PROFILE: /* /proc/ccs/profile */
2989 head->write = write_profile;
2990 head->read = read_profile;
2991 break;
2992 case CCS_QUERY: /* /proc/ccs/query */
2993 head->poll = poll_query;
2994 head->write = write_answer;
2995 head->read = read_query;
2996 break;
2997 case CCS_MANAGER: /* /proc/ccs/manager */
2998 head->write = write_manager_policy;
2999 head->read = read_manager_policy;
3000 break;
3001 case CCS_UPDATESCOUNTER: /* /proc/ccs/.updates_counter */
3002 head->read = read_updates_counter;
3003 break;
3004 }
3005 if (!(file->f_mode & FMODE_READ)) {
3006 /*
3007 * No need to allocate read_buf since it is not opened
3008 * for reading.
3009 */
3010 head->read = NULL;
3011 head->poll = NULL;
3012 } else if (type != CCS_GRANTLOG && type != CCS_REJECTLOG
3013 && type != CCS_QUERY) {
3014 /*
3015 * Don't allocate buffer for reading if the file is one of
3016 * /proc/ccs/grant_log , /proc/ccs/reject_log , /proc/ccs/query.
3017 */
3018 if (!head->readbuf_size)
3019 head->readbuf_size = 4096 * 2;
3020 head->read_buf = ccs_alloc(head->readbuf_size);
3021 if (!head->read_buf) {
3022 ccs_free(head);
3023 return -ENOMEM;
3024 }
3025 }
3026 if (!(file->f_mode & FMODE_WRITE)) {
3027 /*
3028 * No need to allocate write_buf since it is not opened
3029 * for writing.
3030 */
3031 head->write = NULL;
3032 } else if (head->write) {
3033 head->writebuf_size = 4096 * 2;
3034 head->write_buf = ccs_alloc(head->writebuf_size);
3035 if (!head->write_buf) {
3036 ccs_free(head->read_buf);
3037 ccs_free(head);
3038 return -ENOMEM;
3039 }
3040 }
3041 file->private_data = head;
3042 /*
3043 * Call the handler now if the file is /proc/ccs/self_domain
3044 * so that the user can use "cat < /proc/ccs/self_domain" to
3045 * know the current process's domainname.
3046 */
3047 if (type == CCS_SELFDOMAIN)
3048 ccs_read_control(file, NULL, 0);
3049 /*
3050 * If the file is /proc/ccs/query , increment the monitor count.
3051 * The monitor count is used by ccs_check_supervisor() to see if
3052 * there is some process monitoring /proc/ccs/query.
3053 */
3054 else if (head->write == write_answer || head->read == read_query)
3055 atomic_inc(&queryd_watcher);
3056 return 0;
3057 }
3058
3059 /**
3060 * ccs_poll_control - poll() for /proc/ccs/ interface.
3061 *
3062 * @file: Pointer to "struct file".
3063 * @wait: Pointer to "poll_table".
3064 *
3065 * Waits for read readiness.
3066 * /proc/ccs/query is handled by /usr/lib/ccs/ccs-queryd and
3067 * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
3068 * /usr/lib/ccs/ccs-auditd.
3069 */
3070 int ccs_poll_control(struct file *file, poll_table *wait)
3071 {
3072 struct ccs_io_buffer *head = file->private_data;
3073 if (!head->poll)
3074 return -ENOSYS;
3075 return head->poll(file, wait);
3076 }
3077
3078 /**
3079 * ccs_read_control - read() for /proc/ccs/ interface.
3080 *
3081 * @file: Pointer to "struct file".
3082 * @buffer: Poiner to buffer to write to.
3083 * @buffer_len: Size of @buffer.
3084 *
3085 * Returns bytes read on success, negative value otherwise.
3086 */
3087 int ccs_read_control(struct file *file, char __user *buffer,
3088 const int buffer_len)
3089 {
3090 int len = 0;
3091 struct ccs_io_buffer *head = file->private_data;
3092 char *cp;
3093 if (!head->read)
3094 return -ENOSYS;
3095 if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
3096 return -EFAULT;
3097 if (mutex_lock_interruptible(&head->io_sem))
3098 return -EINTR;
3099 /* Call the policy handler. */
3100 len = head->read(head);
3101 if (len < 0)
3102 goto out;
3103 /* Write to buffer. */
3104 len = head->read_avail;
3105 if (len > buffer_len)
3106 len = buffer_len;
3107 if (!len)
3108 goto out;
3109 /* head->read_buf changes by some functions. */
3110 cp = head->read_buf;
3111 if (copy_to_user(buffer, cp, len)) {
3112 len = -EFAULT;
3113 goto out;
3114 }
3115 head->read_avail -= len;
3116 memmove(cp, cp + len, head->read_avail);
3117 out:
3118 mutex_unlock(&head->io_sem);
3119 return len;
3120 }
3121
3122 /**
3123 * ccs_write_control - write() for /proc/ccs/ interface.
3124 *
3125 * @file: Pointer to "struct file".
3126 * @buffer: Pointer to buffer to read from.
3127 * @buffer_len: Size of @buffer.
3128 *
3129 * Returns @buffer_len on success, negative value otherwise.
3130 */
3131 int ccs_write_control(struct file *file, const char __user *buffer,
3132 const int buffer_len)
3133 {
3134 struct ccs_io_buffer *head = file->private_data;
3135 int error = buffer_len;
3136 int avail_len = buffer_len;
3137 char *cp0 = head->write_buf;
3138 if (!head->write)
3139 return -ENOSYS;
3140 if (!access_ok(VERIFY_READ, buffer, buffer_len))
3141 return -EFAULT;
3142 /* Don't allow updating policies by non manager programs. */
3143 if (head->write != write_pid &&
3144 #ifdef CONFIG_TOMOYO
3145 head->write != write_domain_policy &&
3146 #endif
3147 !is_policy_manager())
3148 return -EPERM;
3149 if (mutex_lock_interruptible(&head->io_sem))
3150 return -EINTR;
3151 /* Read a line and dispatch it to the policy handler. */
3152 while (avail_len > 0) {
3153 char c;
3154 if (head->write_avail >= head->writebuf_size - 1) {
3155 error = -ENOMEM;
3156 break;
3157 } else if (get_user(c, buffer)) {
3158 error = -EFAULT;
3159 break;
3160 }
3161 buffer++;
3162 avail_len--;
3163 cp0[head->write_avail++] = c;
3164 if (c != '\n')
3165 continue;
3166 cp0[head->write_avail - 1] = '\0';
3167 head->write_avail = 0;
3168 normalize_line(cp0);
3169 head->write(head);
3170 }
3171 mutex_unlock(&head->io_sem);
3172 return error;
3173 }
3174
3175 /**
3176 * ccs_close_control - close() for /proc/ccs/ interface.
3177 *
3178 * @file: Pointer to "struct file".
3179 *
3180 * Releases memory and returns 0.
3181 */
3182 int ccs_close_control(struct file *file)
3183 {
3184 struct ccs_io_buffer *head = file->private_data;
3185 /*
3186 * If the file is /proc/ccs/query , decrement the monitor count.
3187 */
3188 if (head->write == write_answer || head->read == read_query)
3189 atomic_dec(&queryd_watcher);
3190 /* Release memory used for policy I/O. */
3191 ccs_free(head->read_buf);
3192 head->read_buf = NULL;
3193 ccs_free(head->write_buf);
3194 head->write_buf = NULL;
3195 ccs_free(head);
3196 head = NULL;
3197 file->private_data = NULL;
3198 return 0;
3199 }
3200
3201 /**
3202 * ccs_alloc_acl_element - Allocate permanent memory for ACL entry.
3203 *
3204 * @acl_type: Type of ACL entry.
3205 * @condition: Pointer to condition part of the ACL entry. May be NULL.
3206 *
3207 * Returns pointer to the ACL entry on success, NULL otherwise.
3208 */
3209 void *ccs_alloc_acl_element(const u8 acl_type,
3210 const struct condition_list *condition)
3211 {
3212 int len;
3213 struct acl_info *ptr;
3214 switch (acl_type) {
3215 case TYPE_SINGLE_PATH_ACL:
3216 len = sizeof(struct single_path_acl_record);
3217 break;
3218 case TYPE_DOUBLE_PATH_ACL:
3219 len = sizeof(struct double_path_acl_record);
3220 break;
3221 case TYPE_ARGV0_ACL:
3222 len = sizeof(struct argv0_acl_record);
3223 break;
3224 case TYPE_ENV_ACL:
3225 len = sizeof(struct env_acl_record);
3226 break;
3227 case TYPE_CAPABILITY_ACL:
3228 len = sizeof(struct capability_acl_record);
3229 break;
3230 case TYPE_IP_NETWORK_ACL:
3231 len = sizeof(struct ip_network_acl_record);
3232 break;
3233 case TYPE_SIGNAL_ACL:
3234 len = sizeof(struct signal_acl_record);
3235 break;
3236 case TYPE_EXECUTE_HANDLER:
3237 case TYPE_DENIED_EXECUTE_HANDLER:
3238 len = sizeof(struct execute_handler_record);
3239 break;
3240 default:
3241 return NULL;
3242 }
3243 /*
3244 * If the ACL doesn't have condition part, reduce memory usage
3245 * by eliminating sizeof(struct condition_list *).
3246 */
3247 if (!condition)
3248 len -= sizeof(ptr->access_me_via_ccs_get_condition_part);
3249 ptr = ccs_alloc_element(len);
3250 if (!ptr)
3251 return NULL;
3252 if (condition) {
3253 ptr->access_me_via_ccs_get_condition_part = condition;
3254 ptr->type = acl_type | ACL_WITH_CONDITION;
3255 return ptr;
3256 }
3257 /*
3258 * Substract sizeof(struct condition_list *) because I eliminated
3259 * sizeof(struct condition_list *) from "struct acl_info"
3260 * but I must return the start address of "struct acl_info".
3261 */
3262 ptr = (void *) (((u8 *) ptr)
3263 - sizeof(ptr->access_me_via_ccs_get_condition_part));
3264 ptr->type = acl_type;
3265 return ptr;
3266 }
3267
3268 /**
3269 * ccs_get_condition_part - Get condition part of the given ACL entry.
3270 *
3271 * @acl: Pointer to "struct acl_info". Pointer to an ACL entry.
3272 *
3273 * Returns pointer to the condition part if the ACL has it, NULL otherwise.
3274 */
3275 const struct condition_list *ccs_get_condition_part(const struct acl_info *acl)
3276 {
3277 return (acl->type & ACL_WITH_CONDITION) ?
3278 acl->access_me_via_ccs_get_condition_part : NULL;
3279 }

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