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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2577 - (show annotations) (download) (as text)
Thu May 21 08:25:02 2009 UTC (15 years ago) by kumaneko
Original Path: branches/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 91097 byte(s)


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