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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1054 - (hide annotations) (download) (as text)
Mon Mar 24 09:38:11 2008 UTC (16 years, 1 month ago) by kumaneko
Original Path: trunk/1.6.x/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 80131 byte(s)


1 kumaneko 111 /*
2     * fs/ccs_common.c
3     *
4     * Common functions for SAKURA and TOMOYO.
5     *
6 kumaneko 849 * Copyright (C) 2005-2008 NTT DATA CORPORATION
7 kumaneko 111 *
8 kumaneko 1052 * Version: 1.6.0-pre 2008/03/24
9 kumaneko 111 *
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/string.h>
16     #include <linux/mm.h>
17     #include <linux/utime.h>
18     #include <linux/file.h>
19     #include <linux/module.h>
20     #include <linux/slab.h>
21     #include <asm/uaccess.h>
22     #include <stdarg.h>
23     #include <linux/version.h>
24 kumaneko 1052 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
25 kumaneko 111 #include <linux/namei.h>
26     #include <linux/mount.h>
27     static const int lookup_flags = LOOKUP_FOLLOW;
28     #else
29     static const int lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;
30     #endif
31     #include <linux/realpath.h>
32     #include <linux/ccs_common.h>
33     #include <linux/ccs_proc.h>
34     #include <linux/tomoyo.h>
35 kumaneko 1052
36     /* To support PID namespace. */
37     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
38 kumaneko 964 #define find_task_by_pid find_task_by_vpid
39     #endif
40 kumaneko 111
41 kumaneko 1052 /* Set default specified by the kernel config. */
42 kumaneko 120 #define MAX_ACCEPT_ENTRY (CONFIG_TOMOYO_MAX_ACCEPT_ENTRY)
43 kumaneko 1052 #define MAX_GRANT_LOG (CONFIG_TOMOYO_MAX_GRANT_LOG)
44     #define MAX_REJECT_LOG (CONFIG_TOMOYO_MAX_REJECT_LOG)
45 kumaneko 111
46 kumaneko 1052 /* Has /sbin/init started? */
47 kumaneko 1006 bool sbin_init_started = false;
48 kumaneko 111
49 kumaneko 1052 /* Log level for SAKURA's printk(). */
50 kumaneko 111 const char *ccs_log_level = KERN_DEBUG;
51    
52 kumaneko 1052 /* String table for functionality that takes 4 modes. */
53     static const char *mode_4[4] = {
54     "disabled", "learning", "permissive", "enforcing"
55     };
56     /* String table for functionality that takes 2 modes. */
57     static const char *mode_2[4] = {
58     "disabled", "enabled", "enabled", "enabled"
59     };
60 kumaneko 1014
61 kumaneko 1052 /* Table for profile. */
62 kumaneko 111 static struct {
63     const char *keyword;
64     unsigned int current_value;
65     const unsigned int max_value;
66     } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {
67     [CCS_TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
68     [CCS_TOMOYO_MAC_FOR_ARGV0] = { "MAC_FOR_ARGV0", 0, 3 },
69 kumaneko 581 [CCS_TOMOYO_MAC_FOR_ENV] = { "MAC_FOR_ENV", 0, 3 },
70 kumaneko 111 [CCS_TOMOYO_MAC_FOR_NETWORK] = { "MAC_FOR_NETWORK", 0, 3 },
71     [CCS_TOMOYO_MAC_FOR_SIGNAL] = { "MAC_FOR_SIGNAL", 0, 3 },
72     [CCS_SAKURA_DENY_CONCEAL_MOUNT] = { "DENY_CONCEAL_MOUNT", 0, 3 },
73     [CCS_SAKURA_RESTRICT_CHROOT] = { "RESTRICT_CHROOT", 0, 3 },
74     [CCS_SAKURA_RESTRICT_MOUNT] = { "RESTRICT_MOUNT", 0, 3 },
75     [CCS_SAKURA_RESTRICT_UNMOUNT] = { "RESTRICT_UNMOUNT", 0, 3 },
76 kumaneko 141 [CCS_SAKURA_RESTRICT_PIVOT_ROOT] = { "RESTRICT_PIVOT_ROOT", 0, 3 },
77 kumaneko 111 [CCS_SAKURA_RESTRICT_AUTOBIND] = { "RESTRICT_AUTOBIND", 0, 1 },
78 kumaneko 1052 [CCS_TOMOYO_MAX_ACCEPT_ENTRY]
79     = { "MAX_ACCEPT_ENTRY", MAX_ACCEPT_ENTRY, INT_MAX },
80     [CCS_TOMOYO_MAX_GRANT_LOG]
81     = { "MAX_GRANT_LOG", MAX_GRANT_LOG, INT_MAX },
82     [CCS_TOMOYO_MAX_REJECT_LOG]
83     = { "MAX_REJECT_LOG", MAX_REJECT_LOG, INT_MAX },
84 kumaneko 111 [CCS_TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
85     [CCS_ALLOW_ENFORCE_GRACE] = { "ALLOW_ENFORCE_GRACE", 0, 1 },
86 kumaneko 1052 [CCS_SLEEP_PERIOD]
87     = { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */
88 kumaneko 111 };
89    
90 kumaneko 1015 #ifdef CONFIG_TOMOYO
91 kumaneko 1052 /* Capability name used by domain policy. */
92 kumaneko 1015 const char *capability_control_keyword[TOMOYO_MAX_CAPABILITY_INDEX] = {
93     [TOMOYO_INET_STREAM_SOCKET_CREATE] = "inet_tcp_create",
94     [TOMOYO_INET_STREAM_SOCKET_LISTEN] = "inet_tcp_listen",
95     [TOMOYO_INET_STREAM_SOCKET_CONNECT] = "inet_tcp_connect",
96     [TOMOYO_USE_INET_DGRAM_SOCKET] = "use_inet_udp",
97     [TOMOYO_USE_INET_RAW_SOCKET] = "use_inet_ip",
98     [TOMOYO_USE_ROUTE_SOCKET] = "use_route",
99     [TOMOYO_USE_PACKET_SOCKET] = "use_packet",
100     [TOMOYO_SYS_MOUNT] = "SYS_MOUNT",
101     [TOMOYO_SYS_UMOUNT] = "SYS_UMOUNT",
102     [TOMOYO_SYS_REBOOT] = "SYS_REBOOT",
103     [TOMOYO_SYS_CHROOT] = "SYS_CHROOT",
104     [TOMOYO_SYS_KILL] = "SYS_KILL",
105     [TOMOYO_SYS_VHANGUP] = "SYS_VHANGUP",
106     [TOMOYO_SYS_SETTIME] = "SYS_TIME",
107     [TOMOYO_SYS_NICE] = "SYS_NICE",
108     [TOMOYO_SYS_SETHOSTNAME] = "SYS_SETHOSTNAME",
109     [TOMOYO_USE_KERNEL_MODULE] = "use_kernel_module",
110     [TOMOYO_CREATE_FIFO] = "create_fifo",
111     [TOMOYO_CREATE_BLOCK_DEV] = "create_block_dev",
112     [TOMOYO_CREATE_CHAR_DEV] = "create_char_dev",
113     [TOMOYO_CREATE_UNIX_SOCKET] = "create_unix_socket",
114     [TOMOYO_SYS_LINK] = "SYS_LINK",
115     [TOMOYO_SYS_SYMLINK] = "SYS_SYMLINK",
116     [TOMOYO_SYS_RENAME] = "SYS_RENAME",
117     [TOMOYO_SYS_UNLINK] = "SYS_UNLINK",
118     [TOMOYO_SYS_CHMOD] = "SYS_CHMOD",
119     [TOMOYO_SYS_CHOWN] = "SYS_CHOWN",
120     [TOMOYO_SYS_IOCTL] = "SYS_IOCTL",
121     [TOMOYO_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD",
122     [TOMOYO_SYS_PIVOT_ROOT] = "SYS_PIVOT_ROOT",
123     [TOMOYO_SYS_PTRACE] = "SYS_PTRACE",
124     };
125     #endif
126    
127 kumaneko 1052 /* Profile table. Memory is allocated as needed. */
128     static struct profile {
129 kumaneko 111 unsigned int value[CCS_MAX_CONTROL_INDEX];
130     const struct path_info *comment;
131 kumaneko 1015 #ifdef CONFIG_TOMOYO
132     unsigned char capability_value[TOMOYO_MAX_CAPABILITY_INDEX];
133     #endif
134 kumaneko 1052 } *profile_ptr[MAX_PROFILES];
135 kumaneko 111
136 kumaneko 1052 /* Permit policy management by non-root user? */
137 kumaneko 1006 static bool manage_by_non_root = false;
138 kumaneko 111
139 kumaneko 1052 /* Utility functions. */
140 kumaneko 111
141     #ifdef CONFIG_TOMOYO
142 kumaneko 1052 /**
143     * tomoyo_quiet_setup - Set TOMOYO_VERBOSE=0 by default.
144     *
145     * @str: Unused.
146     *
147     * Returns 0.
148     */
149     static int __init tomoyo_quiet_setup(char *str)
150 kumaneko 111 {
151     ccs_control_array[CCS_TOMOYO_VERBOSE].current_value = 0;
152     return 0;
153     }
154    
155 kumaneko 1052 __setup("TOMOYO_QUIET", tomoyo_quiet_setup);
156 kumaneko 111 #endif
157    
158 kumaneko 1052 /**
159     * is_byte_range - Check whether the string isa \ooo style octal value.
160     *
161     * @str: Pointer to the string.
162     *
163     * Returns true if @str is a \ooo style octal value, false otherwise.
164     */
165     static bool is_byte_range(const char *str)
166     {
167     return *str >= '0' && *str++ <= '3' &&
168     *str >= '0' && *str++ <= '7' &&
169     *str >= '0' && *str <= '7';
170     }
171    
172     /**
173     * is_decimal - Check whether the character is a decimal character.
174     *
175     * @c: The character to check.
176     *
177     * Returns true if @c is a decimal character, false otherwise.
178     */
179     static bool is_decimal(const char c)
180     {
181     return (c >= '0' && c <= '9');
182     }
183    
184     /**
185     * is_hexadecimal - Check whether the character is a hexadecimal character.
186     *
187     * @c: The character to check.
188     *
189     * Returns true if @c is a hexadecimal character, false otherwise.
190     */
191     static bool is_hexadecimal(const char c)
192     {
193     return ((c >= '0' && c <= '9') ||
194     (c >= 'A' && c <= 'F') ||
195     (c >= 'a' && c <= 'f'));
196     }
197    
198     /**
199     * is_alphabet_char - Check whether the character is an alphabet.
200     *
201     * @c: The character to check.
202     *
203     * Returns true if @c is an alphabet character, false otherwise.
204     */
205     static bool is_alphabet_char(const char c)
206     {
207     return ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'));
208     }
209    
210     /**
211     * str_starts - Check whether the given string starts with the given keyword.
212     *
213     * @src: Pointer to pointer to the string.
214     * @find: Pointer to the keyword.
215     *
216     * Returns true if @src starts with @find, false otherwise.
217     *
218     * The @src is updated to point the first character after the @find
219     * if @src starts with @find .
220     */
221     static bool str_starts(char **src, const char *find)
222     {
223     const int len = strlen(find);
224     char *tmp = *src;
225     if (strncmp(tmp, find, len))
226     return false;
227     tmp += len;
228     *src = tmp;
229     return true;
230     }
231    
232     /**
233     * normalize_line - Format string.
234     *
235     * @buffer: The line to normalize.
236     *
237 kumaneko 111 * Leading and trailing whitespaces are removed.
238     * Multiple whitespaces are packed into single space.
239 kumaneko 1052 *
240     * Returns nothing.
241 kumaneko 111 */
242 kumaneko 1052 static void normalize_line(unsigned char *buffer)
243 kumaneko 111 {
244     unsigned char *sp = buffer, *dp = buffer;
245 kumaneko 1006 bool first = true;
246 kumaneko 1052 while (*sp && (*sp <= ' ' || *sp >= 127))
247     sp++;
248 kumaneko 111 while (*sp) {
249 kumaneko 1052 if (!first)
250     *dp++ = ' ';
251 kumaneko 1006 first = false;
252 kumaneko 1052 while (*sp > ' ' && *sp < 127)
253     *dp++ = *sp++;
254     while (*sp && (*sp <= ' ' || *sp >= 127))
255     sp++;
256 kumaneko 111 }
257     *dp = '\0';
258     }
259    
260 kumaneko 1052 /**
261 kumaneko 1054 * ccs_is_correct_path - Validate a pathname.
262     * @filename: The pathname to check.
263     * @start_type: Should the pathname start with '/'?
264     * 1 = must / -1 = must not / 0 = don't care
265     * @pattern_type: Can the pathname contain a wildcard?
266     * 1 = must / -1 = must not / 0 = don't care
267     * @end_type: Should the pathname end with '/'?
268     * 1 = must / -1 = must not / 0 = don't care
269     * @function: The name of function calling me.
270 kumaneko 1052 *
271 kumaneko 1054 * Check whether the given filename follows the naming rules.
272 kumaneko 1052 * Returns true if @filename follows the naming rules, false otherwise.
273 kumaneko 111 */
274 kumaneko 1052 bool ccs_is_correct_path(const char *filename, const s8 start_type,
275     const s8 pattern_type, const s8 end_type,
276     const char *function)
277 kumaneko 111 {
278 kumaneko 1006 bool contains_pattern = false;
279 kumaneko 1052 unsigned char c, d, e;
280 kumaneko 111 const char *original_filename = filename;
281 kumaneko 1052 if (!filename)
282     goto out;
283 kumaneko 111 c = *filename;
284     if (start_type == 1) { /* Must start with '/' */
285 kumaneko 1052 if (c != '/')
286     goto out;
287 kumaneko 111 } else if (start_type == -1) { /* Must not start with '/' */
288 kumaneko 1052 if (c == '/')
289     goto out;
290 kumaneko 111 }
291 kumaneko 1052 if (c)
292     c = *(strchr(filename, '\0') - 1);
293 kumaneko 111 if (end_type == 1) { /* Must end with '/' */
294 kumaneko 1052 if (c != '/')
295     goto out;
296 kumaneko 111 } else if (end_type == -1) { /* Must not end with '/' */
297 kumaneko 1052 if (c == '/')
298     goto out;
299 kumaneko 111 }
300     while ((c = *filename++) != '\0') {
301     if (c == '\\') {
302     switch ((c = *filename++)) {
303     case '\\': /* "\\" */
304     continue;
305     case '$': /* "\$" */
306     case '+': /* "\+" */
307     case '?': /* "\?" */
308     case '*': /* "\*" */
309     case '@': /* "\@" */
310     case 'x': /* "\x" */
311     case 'X': /* "\X" */
312     case 'a': /* "\a" */
313     case 'A': /* "\A" */
314 kumaneko 206 case '-': /* "\-" */
315 kumaneko 1052 if (pattern_type == -1)
316     break; /* Must not contain pattern */
317 kumaneko 1006 contains_pattern = true;
318 kumaneko 111 continue;
319     case '0': /* "\ooo" */
320     case '1':
321     case '2':
322     case '3':
323 kumaneko 1052 d = *filename++;
324     if (d < '0' || d > '7')
325     break;
326     e = *filename++;
327     if (e < '0' || e > '7')
328     break;
329     c = (((u8) (c - '0')) << 6)
330     + (((u8) (d - '0')) << 3)
331     + (((u8) (e - '0')));
332     if (c && (c <= ' ' || c >= 127))
333     continue; /* pattern is not \000 */
334 kumaneko 111 }
335     goto out;
336     } else if (c <= ' ' || c >= 127) {
337     goto out;
338     }
339     }
340     if (pattern_type == 1) { /* Must contain pattern */
341 kumaneko 1052 if (!contains_pattern)
342     goto out;
343 kumaneko 111 }
344 kumaneko 1006 return true;
345 kumaneko 111 out:
346 kumaneko 1052 printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function,
347     original_filename);
348 kumaneko 1006 return false;
349 kumaneko 111 }
350    
351 kumaneko 1052 /**
352 kumaneko 1054 * ccs_is_correct_domain - Check whether the given domainname follows the naming rules.
353     * @domainname: The domainname to check.
354     * @function: The name of function calling me.
355 kumaneko 1052 *
356     * Returns true if @domainname follows the naming rules, false otherwise.
357 kumaneko 111 */
358 kumaneko 1052 bool ccs_is_correct_domain(const unsigned char *domainname,
359     const char *function)
360 kumaneko 111 {
361     unsigned char c, d, e;
362     const char *org_domainname = domainname;
363 kumaneko 1052 if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN))
364     goto out;
365 kumaneko 111 domainname += ROOT_NAME_LEN;
366 kumaneko 1052 if (!*domainname)
367     return true;
368 kumaneko 111 do {
369 kumaneko 1052 if (*domainname++ != ' ')
370     goto out;
371     if (*domainname++ != '/')
372     goto out;
373 kumaneko 111 while ((c = *domainname) != '\0' && c != ' ') {
374     domainname++;
375     if (c == '\\') {
376 kumaneko 1052 c = *domainname++;
377     switch ((c)) {
378 kumaneko 111 case '\\': /* "\\" */
379     continue;
380     case '0': /* "\ooo" */
381     case '1':
382     case '2':
383     case '3':
384 kumaneko 1052 d = *domainname++;
385     if (d < '0' || d > '7')
386     break;
387     e = *domainname++;
388     if (e < '0' || e > '7')
389     break;
390     c = (((u8) (c - '0')) << 6)
391     + (((u8) (d - '0')) << 3)
392     + (((u8) (e - '0')));
393     if (c && (c <= ' ' || c >= 127))
394     /* pattern is not \000 */
395     continue;
396 kumaneko 111 }
397     goto out;
398     } else if (c < ' ' || c >= 127) {
399     goto out;
400     }
401     }
402     } while (*domainname);
403 kumaneko 1006 return true;
404 kumaneko 111 out:
405 kumaneko 1052 printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function,
406     org_domainname);
407 kumaneko 1006 return false;
408 kumaneko 111 }
409    
410 kumaneko 1052 /**
411     * ccs_is_domain_def - Check whether the given token can be a domainname.
412     *
413     * @buffer: The token to check.
414     *
415     * Returns true if @buffer possibly be a domainname, false otherwise.
416     */
417     bool ccs_is_domain_def(const unsigned char *buffer)
418 kumaneko 461 {
419 kumaneko 1052 return !strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN);
420 kumaneko 461 }
421    
422 kumaneko 1052 /**
423     * ccs_find_domain - Find a domain by the given name.
424     *
425 kumaneko 1054 * @domainname: The domainname to find.
426 kumaneko 1052 *
427     * Returns pointer to "struct domain_info" if found, NULL otherwise.
428     */
429     struct domain_info *ccs_find_domain(const char *domainname)
430 kumaneko 461 {
431     struct domain_info *domain;
432 kumaneko 1052 struct path_info name;
433     name.name = domainname;
434     ccs_fill_path_info(&name);
435 kumaneko 722 list1_for_each_entry(domain, &domain_list, list) {
436 kumaneko 1052 if (!domain->is_deleted &&
437     !ccs_pathcmp(&name, domain->domainname))
438     return domain;
439 kumaneko 461 }
440     return NULL;
441     }
442    
443 kumaneko 1052 /**
444     * path_depth - Evaluate the number of '/' in a string.
445     *
446     * @pathname: The string to evaluate.
447     *
448     * Returns path depth of the string.
449     *
450     * I score 2 for each of the '/' in the @pathname
451     * and score 1 if the @pathname ends with '/'.
452     */
453     static int path_depth(const char *pathname)
454 kumaneko 111 {
455     int i = 0;
456     if (pathname) {
457     char *ep = strchr(pathname, '\0');
458     if (pathname < ep--) {
459 kumaneko 1052 if (*ep != '/')
460     i++;
461     while (pathname <= ep)
462     if (*ep-- == '/')
463     i += 2;
464 kumaneko 111 }
465     }
466     return i;
467     }
468    
469 kumaneko 1052 /**
470 kumaneko 1054 * const_part_length - Evaluate the initial length without a pattern in a token.
471 kumaneko 1052 *
472     * @filename: The string to evaluate.
473     *
474     * Returns the initial length without a pattern in @filename .
475     */
476 kumaneko 111 static int const_part_length(const char *filename)
477     {
478 kumaneko 1052 char c;
479 kumaneko 111 int len = 0;
480 kumaneko 1052 if (!filename)
481     return 0;
482     while ((c = *filename++) != '\0') {
483     if (c != '\\') {
484     len++;
485     continue;
486 kumaneko 111 }
487 kumaneko 1052 c = *filename++;
488     switch (c) {
489     case '\\': /* "\\" */
490     len += 2;
491     continue;
492     case '0': /* "\ooo" */
493     case '1':
494     case '2':
495     case '3':
496     c = *filename++;
497     if (c < '0' || c > '7')
498     break;
499     c = *filename++;
500     if (c < '0' || c > '7')
501     break;
502     len += 4;
503     continue;
504     }
505     break;
506 kumaneko 111 }
507     return len;
508     }
509    
510 kumaneko 1052 /**
511     * ccs_fill_path_info - Fill in "struct path_info" members.
512     *
513     * @ptr: Pointer to "struct path_info" to fill in.
514     *
515     * The caller sets "struct path_info"->name.
516     */
517     void ccs_fill_path_info(struct path_info *ptr)
518 kumaneko 111 {
519     const char *name = ptr->name;
520     const int len = strlen(name);
521     ptr->total_len = len;
522     ptr->const_len = const_part_length(name);
523     ptr->is_dir = len && (name[len - 1] == '/');
524     ptr->is_patterned = (ptr->const_len < len);
525     ptr->hash = full_name_hash(name, len);
526 kumaneko 1052 ptr->depth = path_depth(name);
527 kumaneko 111 }
528    
529 kumaneko 1052 /**
530     * file_matches_to_pattern2 - Pattern matching without '/' character
531     * and "\-" pattern.
532     *
533     * @filename: The start of string to check.
534     * @filename_end: The end of string to check.
535     * @pattern: The start of pattern to compare.
536     * @pattern_end: The end of pattern to compare.
537     *
538     * Returns true if @filename matches @pattern, false otherwise.
539     */
540     static bool file_matches_to_pattern2(const char *filename,
541     const char *filename_end,
542     const char *pattern,
543     const char *pattern_end)
544 kumaneko 111 {
545     while (filename < filename_end && pattern < pattern_end) {
546 kumaneko 1052 char c;
547 kumaneko 111 if (*pattern != '\\') {
548 kumaneko 1052 if (*filename++ != *pattern++)
549     return false;
550     continue;
551     }
552     c = *filename;
553     pattern++;
554     switch (*pattern) {
555     int i, j;
556     case '?':
557     if (c == '/') {
558     return false;
559     } else if (c == '\\') {
560     if (filename[1] == '\\')
561     filename++;
562     else if (is_byte_range(filename + 1))
563     filename += 3;
564     else
565 kumaneko 1006 return false;
566 kumaneko 1052 }
567     break;
568     case '\\':
569     if (c != '\\')
570     return false;
571     if (*++filename != '\\')
572     return false;
573     break;
574     case '+':
575     if (!is_decimal(c))
576     return false;
577     break;
578     case 'x':
579     if (!is_hexadecimal(c))
580     return false;
581     break;
582     case 'a':
583     if (!is_alphabet_char(c))
584     return false;
585     break;
586     case '0':
587     case '1':
588     case '2':
589     case '3':
590     if (c == '\\' && is_byte_range(filename + 1)
591     && strncmp(filename + 1, pattern, 3) == 0) {
592     filename += 3;
593     pattern += 2;
594 kumaneko 111 break;
595 kumaneko 1052 }
596     return false; /* Not matched. */
597     case '*':
598     case '@':
599     for (i = 0; i <= filename_end - filename; i++) {
600     if (file_matches_to_pattern2(filename + i,
601     filename_end,
602     pattern + 1,
603     pattern_end))
604     return true;
605     c = filename[i];
606     if (c == '.' && *pattern == '@')
607 kumaneko 111 break;
608 kumaneko 1052 if (c != '\\')
609     continue;
610     if (filename[i + 1] == '\\')
611     i++;
612     else if (is_byte_range(filename + i + 1))
613     i += 3;
614     else
615     break; /* Bad pattern. */
616 kumaneko 111 }
617 kumaneko 1052 return false; /* Not matched. */
618     default:
619     j = 0;
620     c = *pattern;
621     if (c == '$') {
622     while (is_decimal(filename[j]))
623     j++;
624     } else if (c == 'X') {
625     while (is_hexadecimal(filename[j]))
626     j++;
627     } else if (c == 'A') {
628     while (is_alphabet_char(filename[j]))
629     j++;
630     }
631     for (i = 1; i <= j; i++) {
632     if (file_matches_to_pattern2(filename + i,
633     filename_end,
634     pattern + 1,
635     pattern_end))
636     return true;
637     }
638     return false; /* Not matched or bad pattern. */
639 kumaneko 111 }
640 kumaneko 1052 filename++;
641     pattern++;
642 kumaneko 111 }
643 kumaneko 1052 while (*pattern == '\\' &&
644     (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
645     pattern += 2;
646 kumaneko 111 return (filename == filename_end && pattern == pattern_end);
647     }
648    
649 kumaneko 1052 /**
650     * file_matches_to_pattern - Pattern matching without without '/' character.
651     *
652     * @filename: The start of string to check.
653     * @filename_end: The end of string to check.
654     * @pattern: The start of pattern to compare.
655     * @pattern_end: The end of pattern to compare.
656     *
657     * Returns true if @filename matches @pattern, false otherwise.
658     */
659     static bool file_matches_to_pattern(const char *filename,
660     const char *filename_end,
661     const char *pattern,
662     const char *pattern_end)
663 kumaneko 206 {
664     const char *pattern_start = pattern;
665 kumaneko 1006 bool first = true;
666     bool result;
667 kumaneko 206 while (pattern < pattern_end - 1) {
668 kumaneko 1052 /* Split at "\-" pattern. */
669     if (*pattern++ != '\\' || *pattern++ != '-')
670     continue;
671     result = file_matches_to_pattern2(filename, filename_end,
672     pattern_start, pattern - 2);
673     if (first)
674     result = !result;
675     if (result)
676     return false;
677 kumaneko 1006 first = false;
678 kumaneko 206 pattern_start = pattern;
679     }
680 kumaneko 1052 result = file_matches_to_pattern2(filename, filename_end,
681     pattern_start, pattern_end);
682 kumaneko 206 return first ? result : !result;
683     }
684    
685 kumaneko 1052 /**
686 kumaneko 1054 * ccs_path_matches_pattern - Check whether the given filename matches the given pattern.
687 kumaneko 1052 * @filename: The filename to check.
688     * @pattern: The pattern to compare.
689 kumaneko 111 *
690 kumaneko 1052 * Returns true if matches, false otherwise.
691     *
692     * The following patterns are available.
693     * \\ \ itself.
694     * \ooo Octal representation of a byte.
695     * \* More than or equals to 0 character other than '/'.
696     * \@ More than or equals to 0 character other than '/' or '.'.
697     * \? 1 byte character other than '/'.
698     * \$ More than or equals to 1 decimal digit.
699     * \+ 1 decimal digit.
700     * \X More than or equals to 1 hexadecimal digit.
701     * \x 1 hexadecimal digit.
702     * \A More than or equals to 1 alphabet character.
703     * \a 1 alphabet character.
704     * \- Subtraction operator.
705 kumaneko 111 */
706 kumaneko 1052 bool ccs_path_matches_pattern(const struct path_info *filename,
707     const struct path_info *pattern)
708 kumaneko 111 {
709 kumaneko 1052 /*
710     if (!filename || !pattern)
711     return false;
712     */
713     const char *f = filename->name;
714     const char *p = pattern->name;
715     const int len = pattern->const_len;
716     /* If @pattern doesn't contain pattern, I can use strcmp(). */
717     if (!pattern->is_patterned)
718     return !ccs_pathcmp(filename, pattern);
719     /* Dont compare if the number of '/' differs. */
720     if (filename->depth != pattern->depth)
721     return false;
722     /* Compare the initial length without patterns. */
723     if (strncmp(f, p, len))
724     return false;
725     f += len;
726     p += len;
727     /* Main loop. Compare each directory component. */
728     while (*f && *p) {
729     const char *f_delimiter = strchr(f, '/');
730     const char *p_delimiter = strchr(p, '/');
731     if (!f_delimiter)
732     f_delimiter = strchr(f, '\0');
733     if (!p_delimiter)
734     p_delimiter = strchr(p, '\0');
735     if (!file_matches_to_pattern(f, f_delimiter, p, p_delimiter))
736     return false;
737     f = f_delimiter;
738     if (*f)
739     f++;
740     p = p_delimiter;
741     if (*p)
742     p++;
743 kumaneko 111 }
744 kumaneko 1052 /* Ignore trailing "\*" and "\@" in @pattern. */
745     while (*p == '\\' &&
746     (*(p + 1) == '*' || *(p + 1) == '@'))
747     p += 2;
748     return (!*f && !*p);
749 kumaneko 111 }
750    
751 kumaneko 1052 /**
752     * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
753     *
754     * @head: Pointer to "struct ccs_io_buffer".
755     * @fmt: The printf()'s format string, followed by parameters.
756     *
757     * Returns true on success, false otherwise.
758     *
759     * The snprintf() will truncate, but ccs_io_printf() won't.
760 kumaneko 111 */
761 kumaneko 1052 bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
762 kumaneko 111 {
763     va_list args;
764     int len, pos = head->read_avail, size = head->readbuf_size - pos;
765 kumaneko 1052 if (size <= 0)
766     return false;
767 kumaneko 111 va_start(args, fmt);
768     len = vsnprintf(head->read_buf + pos, size, fmt, args);
769     va_end(args);
770 kumaneko 1052 if (pos + len >= head->readbuf_size)
771     return false;
772 kumaneko 111 head->read_avail += len;
773 kumaneko 1052 return true;
774 kumaneko 111 }
775    
776 kumaneko 1052 /**
777     * ccs_get_exe - Get ccs_realpath() of current process.
778     *
779     * Returns the ccs_realpath() of current process on success, NULL otherwise.
780     *
781     * This function uses ccs_alloc(), so the caller must ccs_free()
782     * if this function didn't return NULL.
783 kumaneko 111 */
784 kumaneko 1052 const char *ccs_get_exe(void)
785 kumaneko 111 {
786 kumaneko 737 struct mm_struct *mm = current->mm;
787     struct vm_area_struct *vma;
788     const char *cp = NULL;
789 kumaneko 1052 if (!mm)
790     return NULL;
791 kumaneko 737 down_read(&mm->mmap_sem);
792     for (vma = mm->mmap; vma; vma = vma->vm_next) {
793     if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
794 kumaneko 1052 cp = ccs_realpath_from_dentry(vma->vm_file->f_dentry,
795     vma->vm_file->f_vfsmnt);
796 kumaneko 737 break;
797 kumaneko 111 }
798     }
799 kumaneko 737 up_read(&mm->mmap_sem);
800     return cp;
801 kumaneko 111 }
802    
803 kumaneko 1052 /**
804     * ccs_get_msg - Get warning message.
805     *
806     * @is_enforce: Is it enforcing mode?
807     *
808     * Returns "ERROR" or "WARNING".
809     */
810     const char *ccs_get_msg(const bool is_enforce)
811 kumaneko 111 {
812 kumaneko 1052 if (is_enforce)
813     return "ERROR";
814     else
815     return "WARNING";
816 kumaneko 111 }
817    
818 kumaneko 1052 /**
819     * ccs_check_flags_no_sleep_check - Check mode for specified functionality.
820     *
821     * @index: The functionality to check mode.
822     *
823     * Returns the mode of specified functionality.
824     */
825     unsigned int ccs_check_flags_no_sleep_check(const u8 index)
826 kumaneko 111 {
827     const u8 profile = current->domain_info->profile;
828 kumaneko 121 return sbin_init_started && index < CCS_MAX_CONTROL_INDEX
829     #if MAX_PROFILES != 256
830     && profile < MAX_PROFILES
831     #endif
832 kumaneko 1052 && profile_ptr[profile] ?
833     profile_ptr[profile]->value[index] : 0;
834 kumaneko 111 }
835    
836 kumaneko 1052 /**
837     * sleep_check - Check whether it is permitted to do operations that may sleep.
838     *
839     * Returns true if it is permitted to do operations that may sleep,
840     * false otherwise.
841     *
842     * TOMOYO Linux supports interactive enforcement that lets processes
843     * wait for the administrator's decision.
844     * All hooks but the one for ccs_may_autobind() are inserted where
845     * it is permitted to do operations that may sleep.
846     * Thus, this warning should not happen.
847     */
848 kumaneko 1015 static bool sleep_check(void)
849 kumaneko 899 {
850 kumaneko 1052 static u8 count = 20;
851     #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
852     if (likely(!in_interrupt()))
853     return true;
854 kumaneko 899 #else
855 kumaneko 1052 if (likely(!in_atomic()))
856     return true;
857 kumaneko 899 #endif
858 kumaneko 1052 if (count) {
859     count--;
860     printk(KERN_ERR "BUG: sleeping function called "
861     "from invalid context.\n");
862     dump_stack();
863 kumaneko 899 }
864 kumaneko 1052 return false;
865 kumaneko 899 }
866    
867 kumaneko 1052 /**
868     * ccs_check_flags - Check mode for specified functionality.
869     *
870     * @index: The functionality to check mode.
871     *
872     * Returns the mode of specified functionality.
873     */
874     unsigned int ccs_check_flags(const u8 index)
875 kumaneko 1015 {
876 kumaneko 1052 return sleep_check() ? ccs_check_flags_no_sleep_check(index) : 0;
877 kumaneko 1015 }
878    
879     #ifdef CONFIG_TOMOYO
880 kumaneko 1052 /**
881     * ccs_check_capability_flags - Check mode for specified capability.
882     *
883     * @index: The capability to check mode.
884     *
885     * Returns the mode of specified capability.
886     */
887     u8 ccs_check_capability_flags(const u8 index)
888 kumaneko 1015 {
889     const u8 profile = current->domain_info->profile;
890     return sbin_init_started && index < TOMOYO_MAX_CAPABILITY_INDEX
891     #if MAX_PROFILES != 256
892     && profile < MAX_PROFILES
893     #endif
894     && sleep_check()
895 kumaneko 1052 && profile_ptr[profile] ?
896     profile_ptr[profile]->capability_value[index] : 0;
897 kumaneko 1015 }
898    
899 kumaneko 1052 /**
900     * ccs_cap2keyword - Convert capability operation to capability name.
901     *
902     * @operation: The capability index.
903     *
904     * Returns the name of the specified capability's name.
905     */
906     const char *ccs_cap2keyword(const u8 operation)
907 kumaneko 1015 {
908 kumaneko 1052 return operation < TOMOYO_MAX_CAPABILITY_INDEX
909     ? capability_control_keyword[operation] : NULL;
910 kumaneko 1015 }
911    
912     #endif
913    
914 kumaneko 1052 /**
915     * ccs_verbose_mode - Check whether TOMOYO is verbose mode.
916     *
917     * Returns true if domain policy violation warning should be printed to
918     * console.
919     */
920     bool ccs_verbose_mode(void)
921 kumaneko 111 {
922 kumaneko 1052 return ccs_check_flags(CCS_TOMOYO_VERBOSE) != 0;
923 kumaneko 111 }
924    
925 kumaneko 1052 /**
926     * ccs_check_domain_quota - Check for domain's quota.
927     *
928     * @domain: Pointer to "struct domain_info".
929     *
930     * Returns true if the domain is not exceeded quota, false otherwise.
931     */
932     bool ccs_check_domain_quota(struct domain_info * const domain)
933 kumaneko 512 {
934     unsigned int count = 0;
935     struct acl_info *ptr;
936 kumaneko 1052 if (!domain)
937     return true;
938 kumaneko 722 list1_for_each_entry(ptr, &domain->acl_info_list, list) {
939 kumaneko 1052 if (!(ptr->type & ACL_DELETED))
940     count++;
941 kumaneko 512 }
942 kumaneko 1052 if (count < ccs_check_flags(CCS_TOMOYO_MAX_ACCEPT_ENTRY))
943     return true;
944 kumaneko 512 if (!domain->quota_warned) {
945 kumaneko 1006 domain->quota_warned = true;
946 kumaneko 1052 printk(KERN_WARNING "TOMOYO-WARNING: "
947     "Domain '%s' has so many ACLs to hold. "
948     "Stopped learning mode.\n", domain->domainname->name);
949 kumaneko 512 }
950 kumaneko 1006 return false;
951 kumaneko 512 }
952    
953 kumaneko 1052 /**
954     * ccs_find_or_assign_new_profile - Create a new profile.
955     *
956     * @profile: Profile number to create.
957     *
958     * Returns pointer to "struct profile" on success, NULL otherwise.
959     */
960     static struct profile *ccs_find_or_assign_new_profile(const unsigned int
961     profile)
962 kumaneko 111 {
963 kumaneko 652 static DEFINE_MUTEX(profile_lock);
964 kumaneko 214 struct profile *ptr = NULL;
965 kumaneko 652 mutex_lock(&profile_lock);
966 kumaneko 1052 if (profile < MAX_PROFILES) {
967     ptr = profile_ptr[profile];
968     if (ptr)
969     goto ok;
970     ptr = ccs_alloc_element(sizeof(*ptr));
971     if (ptr) {
972 kumaneko 111 int i;
973 kumaneko 1052 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)
974     ptr->value[i]
975     = ccs_control_array[i].current_value;
976     /*
977     * Needn't to initialize "ptr->capability_value"
978     * because they are always 0.
979     */
980 kumaneko 708 mb(); /* Avoid out-of-order execution. */
981 kumaneko 111 profile_ptr[profile] = ptr;
982     }
983     }
984 kumaneko 1052 ok:
985 kumaneko 652 mutex_unlock(&profile_lock);
986 kumaneko 111 return ptr;
987     }
988    
989 kumaneko 1052 /**
990     * write_profile - Write profile table.
991     *
992     * @head: Pointer to "struct ccs_io_buffer"
993     *
994     * Returns 0 on success, negative value otherwise.
995     */
996     static int write_profile(struct ccs_io_buffer *head)
997 kumaneko 111 {
998     char *data = head->write_buf;
999     unsigned int i, value;
1000     char *cp;
1001 kumaneko 214 struct profile *profile;
1002 kumaneko 111 i = simple_strtoul(data, &cp, 10);
1003     if (data != cp) {
1004 kumaneko 1052 if (*cp != '-')
1005     return -EINVAL;
1006     data = cp + 1;
1007 kumaneko 111 }
1008 kumaneko 1052 profile = ccs_find_or_assign_new_profile(i);
1009     if (!profile)
1010     return -EINVAL;
1011 kumaneko 111 cp = strchr(data, '=');
1012 kumaneko 1052 if (!cp)
1013     return -EINVAL;
1014 kumaneko 111 *cp = '\0';
1015 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_PROFILE);
1016     if (!strcmp(data, "COMMENT")) {
1017     profile->comment = ccs_save_name(cp + 1);
1018 kumaneko 111 return 0;
1019     }
1020 kumaneko 708 #ifdef CONFIG_TOMOYO
1021 kumaneko 1052 if (str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {
1022 kumaneko 1014 if (sscanf(cp + 1, "%u", &value) != 1) {
1023 kumaneko 1015 for (i = 0; i < 4; i++) {
1024 kumaneko 1052 if (strcmp(cp + 1, mode_4[i]))
1025     continue;
1026 kumaneko 1015 value = i;
1027 kumaneko 1014 break;
1028     }
1029 kumaneko 1052 if (i == 4)
1030     return -EINVAL;
1031 kumaneko 1014 }
1032 kumaneko 1052 if (value > 3)
1033     value = 3;
1034 kumaneko 1015 for (i = 0; i < TOMOYO_MAX_CAPABILITY_INDEX; i++) {
1035 kumaneko 1052 if (strcmp(data, capability_control_keyword[i]))
1036     continue;
1037 kumaneko 1015 profile->capability_value[i] = value;
1038     return 0;
1039     }
1040     return -EINVAL;
1041 kumaneko 111 }
1042 kumaneko 461 #endif
1043 kumaneko 111 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
1044 kumaneko 1052 if (strcmp(data, ccs_control_array[i].keyword))
1045     continue;
1046 kumaneko 1014 if (sscanf(cp + 1, "%u", &value) != 1) {
1047     int j;
1048     const char **modes;
1049     switch (i) {
1050     case CCS_SAKURA_RESTRICT_AUTOBIND:
1051     case CCS_TOMOYO_VERBOSE:
1052     case CCS_ALLOW_ENFORCE_GRACE:
1053     modes = mode_2;
1054     break;
1055     default:
1056     modes = mode_4;
1057     break;
1058     }
1059     for (j = 0; j < 4; j++) {
1060 kumaneko 1052 if (strcmp(cp + 1, modes[j]))
1061     continue;
1062 kumaneko 1014 value = j;
1063     break;
1064     }
1065 kumaneko 1052 if (j == 4)
1066     return -EINVAL;
1067 kumaneko 1014 } else if (value > ccs_control_array[i].max_value) {
1068     value = ccs_control_array[i].max_value;
1069     }
1070     switch (i) {
1071     case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1072     case CCS_SAKURA_RESTRICT_UNMOUNT:
1073 kumaneko 1052 if (value == 1)
1074     value = 2; /* learning mode is not supported. */
1075 kumaneko 1014 }
1076 kumaneko 111 profile->value[i] = value;
1077     return 0;
1078     }
1079     return -EINVAL;
1080     }
1081    
1082 kumaneko 1052 /**
1083     * read_profile - Read profile table.
1084     *
1085     * @head: Pointer to "struct ccs_io_buffer"
1086     *
1087     * Returns 0.
1088     */
1089     static int read_profile(struct ccs_io_buffer *head)
1090 kumaneko 111 {
1091 kumaneko 1052 static const int total
1092     = CCS_MAX_CONTROL_INDEX + TOMOYO_MAX_CAPABILITY_INDEX + 1;
1093 kumaneko 1006 int step;
1094 kumaneko 1052 if (head->read_eof)
1095     return 0;
1096     for (step = head->read_step; step < MAX_PROFILES * total; step++) {
1097     const u8 index = step / total;
1098     u8 type = step % total;
1099     const struct profile *profile = profile_ptr[index];
1100 kumaneko 1006 head->read_step = step;
1101 kumaneko 1052 if (!profile)
1102     continue;
1103     #if !defined(CONFIG_SAKURA) || !defined(CONFIG_TOMOYO)
1104     switch (type) {
1105 kumaneko 461 #ifndef CONFIG_SAKURA
1106 kumaneko 1006 case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1107     case CCS_SAKURA_RESTRICT_CHROOT:
1108     case CCS_SAKURA_RESTRICT_MOUNT:
1109     case CCS_SAKURA_RESTRICT_UNMOUNT:
1110     case CCS_SAKURA_RESTRICT_PIVOT_ROOT:
1111     case CCS_SAKURA_RESTRICT_AUTOBIND:
1112 kumaneko 461 #endif
1113 kumaneko 111 #ifndef CONFIG_TOMOYO
1114 kumaneko 1006 case CCS_TOMOYO_MAC_FOR_FILE:
1115     case CCS_TOMOYO_MAC_FOR_ARGV0:
1116     case CCS_TOMOYO_MAC_FOR_ENV:
1117     case CCS_TOMOYO_MAC_FOR_NETWORK:
1118     case CCS_TOMOYO_MAC_FOR_SIGNAL:
1119     case CCS_TOMOYO_MAX_ACCEPT_ENTRY:
1120     case CCS_TOMOYO_MAX_GRANT_LOG:
1121     case CCS_TOMOYO_MAX_REJECT_LOG:
1122     case CCS_TOMOYO_VERBOSE:
1123 kumaneko 111 #endif
1124 kumaneko 1006 continue;
1125 kumaneko 111 }
1126 kumaneko 1052 #endif
1127     if (!type) { /* Print profile' comment tag. */
1128     if (!ccs_io_printf(head, "%u-COMMENT=%s\n",
1129     index, profile->comment ?
1130     profile->comment->name : ""))
1131     break;
1132     continue;
1133     }
1134     type--;
1135     if (type >= CCS_MAX_CONTROL_INDEX) {
1136 kumaneko 1015 #ifdef CONFIG_TOMOYO
1137 kumaneko 1052 const int i = type - CCS_MAX_CONTROL_INDEX;
1138     const u8 value = profile->capability_value[i];
1139     if (!ccs_io_printf(head,
1140     "%u-" KEYWORD_MAC_FOR_CAPABILITY
1141     "%s=%s\n", index,
1142     capability_control_keyword[i],
1143     mode_4[value]))
1144     break;
1145 kumaneko 1015 #endif
1146 kumaneko 1006 } else {
1147 kumaneko 1052 const unsigned int value = profile->value[type];
1148 kumaneko 1014 const char **modes = NULL;
1149 kumaneko 1052 const char *keyword = ccs_control_array[type].keyword;
1150     switch (ccs_control_array[type].max_value) {
1151 kumaneko 1014 case 3:
1152     modes = mode_4;
1153     break;
1154     case 1:
1155     modes = mode_2;
1156     break;
1157     }
1158     if (modes) {
1159 kumaneko 1052 if (!ccs_io_printf(head, "%u-%s=%s\n", index,
1160     keyword, modes[value]))
1161     break;
1162 kumaneko 1014 } else {
1163 kumaneko 1052 if (!ccs_io_printf(head, "%u-%s=%u\n", index,
1164     keyword, value))
1165     break;
1166 kumaneko 1014 }
1167 kumaneko 1006 }
1168     }
1169 kumaneko 1052 if (step == MAX_PROFILES * total)
1170     head->read_eof = true;
1171 kumaneko 111 return 0;
1172     }
1173    
1174 kumaneko 1052 /* Structure for policy manager. */
1175 kumaneko 214 struct policy_manager_entry {
1176 kumaneko 722 struct list1_head list;
1177 kumaneko 1052 /* A path to program or a domainname. */
1178 kumaneko 111 const struct path_info *manager;
1179 kumaneko 1052 bool is_domain; /* True if manager is a domainname. */
1180     bool is_deleted; /* True if this entry is deleted. */
1181 kumaneko 214 };
1182 kumaneko 111
1183 kumaneko 1052 /* The list for "struct policy_manager_entry". */
1184 kumaneko 722 static LIST1_HEAD(policy_manager_list);
1185 kumaneko 111
1186 kumaneko 1052 /**
1187     * update_manager_entry - Add a manager entry.
1188     *
1189     * @manager: The path to manager or the domainnamme.
1190     * @is_delete: True if it is a delete request.
1191     *
1192     * Returns 0 on success, negative value otherwise.
1193     */
1194     static int update_manager_entry(const char *manager, const bool is_delete)
1195 kumaneko 111 {
1196 kumaneko 214 struct policy_manager_entry *new_entry, *ptr;
1197 kumaneko 652 static DEFINE_MUTEX(lock);
1198 kumaneko 111 const struct path_info *saved_manager;
1199     int error = -ENOMEM;
1200 kumaneko 1006 bool is_domain = false;
1201 kumaneko 1052 if (ccs_is_domain_def(manager)) {
1202     if (!ccs_is_correct_domain(manager, __func__))
1203     return -EINVAL;
1204 kumaneko 1006 is_domain = true;
1205 kumaneko 111 } else {
1206 kumaneko 1052 if (!ccs_is_correct_path(manager, 1, -1, -1, __func__))
1207     return -EINVAL;
1208 kumaneko 111 }
1209 kumaneko 1052 saved_manager = ccs_save_name(manager);
1210     if (!saved_manager)
1211     return -ENOMEM;
1212 kumaneko 652 mutex_lock(&lock);
1213 kumaneko 722 list1_for_each_entry(ptr, &policy_manager_list, list) {
1214 kumaneko 111 if (ptr->manager == saved_manager) {
1215     ptr->is_deleted = is_delete;
1216     error = 0;
1217     goto out;
1218     }
1219     }
1220     if (is_delete) {
1221     error = -ENOENT;
1222     goto out;
1223     }
1224 kumaneko 1052 new_entry = ccs_alloc_element(sizeof(*new_entry));
1225     if (!new_entry)
1226     goto out;
1227 kumaneko 111 new_entry->manager = saved_manager;
1228     new_entry->is_domain = is_domain;
1229 kumaneko 722 list1_add_tail_mb(&new_entry->list, &policy_manager_list);
1230 kumaneko 111 error = 0;
1231     out:
1232 kumaneko 652 mutex_unlock(&lock);
1233 kumaneko 1052 if (!error)
1234     ccs_update_counter(CCS_UPDATES_COUNTER_MANAGER);
1235 kumaneko 111 return error;
1236     }
1237    
1238 kumaneko 1052 /**
1239     * write_manager_policy - Write manager policy.
1240     *
1241     * @head: Pointer to "struct ccs_io_buffer"
1242     *
1243     * Returns 0 on success, negative value otherwise.
1244     */
1245     static int write_manager_policy(struct ccs_io_buffer *head)
1246 kumaneko 111 {
1247 kumaneko 1052 char *data = head->write_buf;
1248     bool is_delete = str_starts(&data, KEYWORD_DELETE);
1249 kumaneko 1006 if (strcmp(data, "manage_by_non_root") == 0) {
1250 kumaneko 1029 manage_by_non_root = !is_delete;
1251 kumaneko 1006 return 0;
1252     }
1253 kumaneko 1052 return update_manager_entry(data, is_delete);
1254 kumaneko 111 }
1255    
1256 kumaneko 1052 /**
1257     * read_manager_policy - Read manager policy.
1258     *
1259     * @head: Pointer to "struct ccs_io_buffer"
1260     *
1261     * Returns 0.
1262     */
1263     static int read_manager_policy(struct ccs_io_buffer *head)
1264 kumaneko 111 {
1265 kumaneko 722 struct list1_head *pos;
1266 kumaneko 1052 if (head->read_eof)
1267     return 0;
1268 kumaneko 722 list1_for_each_cookie(pos, head->read_var2, &policy_manager_list) {
1269 kumaneko 708 struct policy_manager_entry *ptr;
1270 kumaneko 722 ptr = list1_entry(pos, struct policy_manager_entry, list);
1271 kumaneko 1052 if (ptr->is_deleted)
1272     continue;
1273     if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
1274     return 0;
1275 kumaneko 111 }
1276 kumaneko 1006 head->read_eof = true;
1277 kumaneko 111 return 0;
1278     }
1279    
1280 kumaneko 1052 /**
1281     * is_policy_manager - Check whether the current process is a policy manager.
1282     *
1283     * Returns true if the current process is permitted to modify policy
1284     * via /proc/ccs/ interface.
1285     */
1286     static bool is_policy_manager(void)
1287 kumaneko 111 {
1288 kumaneko 214 struct policy_manager_entry *ptr;
1289 kumaneko 111 const char *exe;
1290 kumaneko 1006 const struct task_struct *task = current;
1291     const struct path_info *domainname = task->domain_info->domainname;
1292     bool found = false;
1293 kumaneko 1052 if (!sbin_init_started)
1294     return true;
1295     if (!manage_by_non_root && (task->uid || task->euid))
1296     return false;
1297 kumaneko 722 list1_for_each_entry(ptr, &policy_manager_list, list) {
1298 kumaneko 1052 if (!ptr->is_deleted && ptr->is_domain
1299     && !ccs_pathcmp(domainname, ptr->manager))
1300     return true;
1301 kumaneko 111 }
1302 kumaneko 1052 exe = ccs_get_exe();
1303     if (!exe)
1304     return false;
1305 kumaneko 722 list1_for_each_entry(ptr, &policy_manager_list, list) {
1306 kumaneko 1052 if (!ptr->is_deleted && !ptr->is_domain
1307     && !strcmp(exe, ptr->manager->name)) {
1308 kumaneko 1006 found = true;
1309 kumaneko 708 break;
1310     }
1311 kumaneko 111 }
1312 kumaneko 708 if (!found) { /* Reduce error messages. */
1313 kumaneko 1052 static pid_t last_pid;
1314 kumaneko 111 const pid_t pid = current->pid;
1315     if (last_pid != pid) {
1316 kumaneko 1052 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1317     "update policies.\n", domainname->name, exe);
1318 kumaneko 111 last_pid = pid;
1319     }
1320     }
1321     ccs_free(exe);
1322 kumaneko 708 return found;
1323 kumaneko 111 }
1324    
1325     #ifdef CONFIG_TOMOYO
1326    
1327 kumaneko 1052 /**
1328     * ccs_find_condition_part - Find condition part from the statement.
1329     *
1330     * @data: String to parse.
1331     *
1332     * Returns pointer to the condition part if it was found in the statement,
1333     * NULL otherwise.
1334     */
1335     static char *ccs_find_condition_part(char *data)
1336 kumaneko 581 {
1337     char *cp = strstr(data, " if "), *cp2;
1338     if (cp) {
1339     while ((cp2 = strstr(cp + 3, " if ")) != NULL) cp = cp2;
1340     *cp++ = '\0';
1341 kumaneko 1052 } else {
1342     cp = strstr(data, " ; set ");
1343     if (cp)
1344     *cp++ = '\0';
1345 kumaneko 581 }
1346     return cp;
1347     }
1348    
1349 kumaneko 1052 /**
1350     * write_domain_policy - Write domain policy.
1351     *
1352     * @head: Pointer to "struct ccs_io_buffer".
1353     *
1354     * Returns 0 on success, negative value otherwise.
1355     */
1356     static int write_domain_policy(struct ccs_io_buffer *head)
1357 kumaneko 111 {
1358     char *data = head->write_buf;
1359     struct domain_info *domain = head->write_var1;
1360 kumaneko 1006 bool is_delete = false, is_select = false, is_undelete = false;
1361 kumaneko 111 unsigned int profile;
1362 kumaneko 581 const struct condition_list *cond = NULL;
1363 kumaneko 906 char *cp;
1364 kumaneko 1052 if (str_starts(&data, KEYWORD_DELETE))
1365 kumaneko 1006 is_delete = true;
1366 kumaneko 1052 else if (str_starts(&data, KEYWORD_SELECT))
1367 kumaneko 1006 is_select = true;
1368 kumaneko 1052 else if (str_starts(&data, KEYWORD_UNDELETE))
1369 kumaneko 1006 is_undelete = true;
1370 kumaneko 1052 if (ccs_is_domain_def(data)) {
1371 kumaneko 111 if (is_delete) {
1372 kumaneko 1052 ccs_delete_domain(data);
1373 kumaneko 111 domain = NULL;
1374     } else if (is_select) {
1375 kumaneko 1052 domain = ccs_find_domain(data);
1376 kumaneko 111 } else if (is_undelete) {
1377 kumaneko 1052 domain = ccs_undelete_domain(data);
1378 kumaneko 111 } else {
1379 kumaneko 1052 domain = ccs_find_or_assign_new_domain(data, 0);
1380 kumaneko 111 }
1381     head->write_var1 = domain;
1382 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1383 kumaneko 111 return 0;
1384     }
1385 kumaneko 1052 if (!domain)
1386     return -EINVAL;
1387 kumaneko 581
1388 kumaneko 1052 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1389     && profile < MAX_PROFILES) {
1390     if (profile_ptr[profile] || !sbin_init_started)
1391     domain->profile = (u8) profile;
1392 kumaneko 581 return 0;
1393     }
1394 kumaneko 1052 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1395     ccs_set_domain_flag(domain, is_delete,
1396     DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1397 kumaneko 1007 return 0;
1398     }
1399 kumaneko 1052 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1400     ccs_set_domain_flag(domain, is_delete,
1401     DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV);
1402 kumaneko 1007 return 0;
1403     }
1404 kumaneko 1052 cp = ccs_find_condition_part(data);
1405     if (cp) {
1406     cond = ccs_find_or_assign_new_condition(cp);
1407     if (!cond)
1408     return -EINVAL;
1409 kumaneko 111 }
1410 kumaneko 1052 if (str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1411     return ccs_write_capability_policy(data, domain, cond,
1412     is_delete);
1413     else if (str_starts(&data, KEYWORD_ALLOW_NETWORK))
1414     return ccs_write_network_policy(data, domain, cond, is_delete);
1415     else if (str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1416     return ccs_write_signal_policy(data, domain, cond, is_delete);
1417     else if (str_starts(&data, KEYWORD_ALLOW_ARGV0))
1418     return ccs_write_argv0_policy(data, domain, cond, is_delete);
1419     else if (str_starts(&data, KEYWORD_ALLOW_ENV))
1420     return ccs_write_env_policy(data, domain, cond, is_delete);
1421     else
1422     return ccs_write_file_policy(data, domain, cond, is_delete);
1423 kumaneko 111 }
1424    
1425 kumaneko 1052 /**
1426 kumaneko 1054 * print_single_path_acl - Print a single path ACL entry.
1427 kumaneko 1052 *
1428     * @head: Pointer to "struct ccs_io_buffer".
1429     * @ptr: Pointer to "struct single_path_acl_record".
1430     * @cond: Pointer to "struct condition_list *". May be NULL.
1431     *
1432     * Returns true on success, false otherwise.
1433     */
1434     static bool print_single_path_acl(struct ccs_io_buffer *head,
1435     struct single_path_acl_record *ptr,
1436     const struct condition_list *cond)
1437 kumaneko 856 {
1438     int pos;
1439     u8 bit;
1440 kumaneko 1052 const char *atmark = "";
1441     const char *filename;
1442 kumaneko 856 const u16 perm = ptr->perm;
1443 kumaneko 1052 if (ptr->u_is_group) {
1444     atmark = "@";
1445     filename = ptr->u.group->group_name->name;
1446     } else {
1447     filename = ptr->u.filename->name;
1448     }
1449 kumaneko 856 for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1450     const char *msg;
1451 kumaneko 1052 if (!(perm & (1 << bit)))
1452     continue;
1453 kumaneko 856 /* Print "read/write" instead of "read" and "write". */
1454 kumaneko 1052 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1455     && (perm & (1 << TYPE_READ_WRITE_ACL)))
1456     continue;
1457     msg = ccs_sp2keyword(bit);
1458 kumaneko 856 pos = head->read_avail;
1459 kumaneko 1052 if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1460     atmark, filename) ||
1461 kumaneko 1054 !ccs_print_condition(head, cond))
1462 kumaneko 1052 goto out;
1463 kumaneko 856 }
1464     head->read_bit = 0;
1465 kumaneko 1006 return true;
1466 kumaneko 856 out:
1467     head->read_bit = bit;
1468     head->read_avail = pos;
1469 kumaneko 1006 return false;
1470 kumaneko 856 }
1471    
1472 kumaneko 1052 /**
1473 kumaneko 1054 * print_double_path_acl - Print a double path ACL entry.
1474 kumaneko 1052 *
1475     * @head: Pointer to "struct ccs_io_buffer".
1476     * @ptr: Pointer to "struct double_path_acl_record".
1477     * @cond: Pointer to "struct condition_list *". May be NULL.
1478     *
1479     * Returns true on success, false otherwise.
1480     */
1481     static bool print_double_path_acl(struct ccs_io_buffer *head,
1482     struct double_path_acl_record *ptr,
1483     const struct condition_list *cond)
1484 kumaneko 856 {
1485     int pos;
1486 kumaneko 1052 const char *atmark1 = "";
1487     const char *atmark2 = "";
1488     const char *filename1;
1489     const char *filename2;
1490 kumaneko 856 const u8 perm = ptr->perm;
1491     u8 bit;
1492 kumaneko 1052 if (ptr->u1_is_group) {
1493     atmark1 = "@";
1494     filename1 = ptr->u1.group1->group_name->name;
1495     } else {
1496     filename1 = ptr->u1.filename1->name;
1497     }
1498     if (ptr->u2_is_group) {
1499     atmark2 = "@";
1500     filename2 = ptr->u2.group2->group_name->name;
1501     } else {
1502     filename2 = ptr->u2.filename2->name;
1503     }
1504 kumaneko 856 for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1505     const char *msg;
1506 kumaneko 1052 if (!(perm & (1 << bit)))
1507     continue;
1508     msg = ccs_dp2keyword(bit);
1509 kumaneko 856 pos = head->read_avail;
1510 kumaneko 1052 if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1511     atmark1, filename1,
1512     atmark2, filename2) ||
1513 kumaneko 1054 !ccs_print_condition(head, cond))
1514 kumaneko 1052 goto out;
1515 kumaneko 856 }
1516 kumaneko 1032 head->read_bit = 0;
1517 kumaneko 1006 return true;
1518 kumaneko 856 out:
1519     head->read_bit = bit;
1520     head->read_avail = pos;
1521 kumaneko 1006 return false;
1522 kumaneko 856 }
1523    
1524 kumaneko 1052 /**
1525 kumaneko 1054 * print_argv0_acl - Print an argv[0] ACL entry.
1526 kumaneko 1052 *
1527     * @head: Pointer to "struct ccs_io_buffer".
1528     * @ptr: Pointer to "struct argv0_acl_record".
1529     * @cond: Pointer to "struct condition_list *". May be NULL.
1530     *
1531     * Returns true on success, false otherwise.
1532     */
1533     static bool print_argv0_acl(struct ccs_io_buffer *head,
1534     struct argv0_acl_record *ptr,
1535     const struct condition_list *cond)
1536 kumaneko 856 {
1537     int pos = head->read_avail;
1538 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1539     ptr->filename->name, ptr->argv0->name))
1540     goto out;
1541 kumaneko 1054 if (!ccs_print_condition(head, cond))
1542 kumaneko 1052 goto out;
1543 kumaneko 1006 return true;
1544 kumaneko 856 out:
1545     head->read_avail = pos;
1546 kumaneko 1006 return false;
1547 kumaneko 856 }
1548    
1549 kumaneko 1052 /**
1550 kumaneko 1054 * print_env_acl - Print an evironment variable name's ACL entry.
1551 kumaneko 1052 *
1552     * @head: Pointer to "struct ccs_io_buffer".
1553     * @ptr: Pointer to "struct env_acl_record".
1554     * @cond: Pointer to "struct condition_list *". May be NULL.
1555     *
1556     * Returns true on success, false otherwise.
1557     */
1558     static bool print_env_acl(struct ccs_io_buffer *head,
1559     struct env_acl_record *ptr,
1560     const struct condition_list *cond)
1561 kumaneko 856 {
1562     int pos = head->read_avail;
1563 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1564     goto out;
1565 kumaneko 1054 if (!ccs_print_condition(head, cond))
1566 kumaneko 1052 goto out;
1567 kumaneko 1006 return true;
1568 kumaneko 856 out:
1569     head->read_avail = pos;
1570 kumaneko 1006 return false;
1571 kumaneko 856 }
1572    
1573 kumaneko 1052 /**
1574 kumaneko 1054 * print_capability_acl - Print a capability ACL entry.
1575 kumaneko 1052 *
1576     * @head: Pointer to "struct ccs_io_buffer".
1577     * @ptr: Pointer to "struct capability_acl_record".
1578     * @cond: Pointer to "struct condition_list *". May be NULL.
1579     *
1580     * Returns true on success, false otherwise.
1581     */
1582     static bool print_capability_acl(struct ccs_io_buffer *head,
1583     struct capability_acl_record *ptr,
1584     const struct condition_list *cond)
1585 kumaneko 856 {
1586 kumaneko 860 int pos = head->read_avail;
1587 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1588     ccs_cap2keyword(ptr->operation)))
1589     goto out;
1590 kumaneko 1054 if (!ccs_print_condition(head, cond))
1591 kumaneko 1052 goto out;
1592 kumaneko 1006 return true;
1593 kumaneko 856 out:
1594     head->read_avail = pos;
1595 kumaneko 1006 return false;
1596 kumaneko 856 }
1597    
1598 kumaneko 1052 /**
1599 kumaneko 1054 * print_ipv4_entry - Print IPv4 address of a network ACL entry.
1600 kumaneko 1052 *
1601     * @head: Pointer to "struct ccs_io_buffer".
1602     * @ptr: Pointer to "struct ip_network_acl_record".
1603     *
1604     * Returns true on success, false otherwise.
1605     */
1606     static bool print_ipv4_entry(struct ccs_io_buffer *head,
1607     struct ip_network_acl_record *ptr)
1608 kumaneko 856 {
1609 kumaneko 1052 const u32 min_address = ptr->u.ipv4.min;
1610     const u32 max_address = ptr->u.ipv4.max;
1611     if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1612     return false;
1613     if (min_address != max_address
1614     && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1615     return false;
1616     return true;
1617     }
1618    
1619     /**
1620 kumaneko 1054 * print_ipv6_entry - Print IPv6 address of a network ACL entry.
1621 kumaneko 1052 *
1622     * @head: Pointer to "struct ccs_io_buffer".
1623     * @ptr: Pointer to "struct ip_network_acl_record".
1624     *
1625     * Returns true on success, false otherwise.
1626     */
1627     static bool print_ipv6_entry(struct ccs_io_buffer *head,
1628     struct ip_network_acl_record *ptr)
1629     {
1630     char buf[64];
1631     const struct in6_addr *min_address = ptr->u.ipv6.min;
1632     const struct in6_addr *max_address = ptr->u.ipv6.max;
1633     ccs_print_ipv6(buf, sizeof(buf), min_address);
1634     if (!ccs_io_printf(head, "%s", buf))
1635     return false;
1636     if (min_address != max_address) {
1637     ccs_print_ipv6(buf, sizeof(buf), max_address);
1638     if (!ccs_io_printf(head, "-%s", buf))
1639     return false;
1640     }
1641     return true;
1642     }
1643    
1644     /**
1645 kumaneko 1054 * print_port_entry - Print port number of a network ACL entry.
1646 kumaneko 1052 *
1647     * @head: Pointer to "struct ccs_io_buffer".
1648     * @ptr: Pointer to "struct ip_network_acl_record".
1649     *
1650     * Returns true on success, false otherwise.
1651     */
1652     static bool print_port_entry(struct ccs_io_buffer *head,
1653     struct ip_network_acl_record *ptr)
1654     {
1655     const u16 min_port = ptr->min_port, max_port = ptr->max_port;
1656     if (!ccs_io_printf(head, " %u", min_port))
1657     return false;
1658     if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1659     return false;
1660     return true;
1661     }
1662    
1663     /**
1664 kumaneko 1054 * print_network_acl - Print a network ACL entry.
1665 kumaneko 1052 *
1666     * @head: Pointer to "struct ccs_io_buffer".
1667     * @ptr: Pointer to "struct ip_network_acl_record".
1668     * @cond: Pointer to "struct condition_list *". May be NULL.
1669     *
1670     * Returns true on success, false otherwise.
1671     */
1672     static bool print_network_acl(struct ccs_io_buffer *head,
1673     struct ip_network_acl_record *ptr,
1674     const struct condition_list *cond)
1675     {
1676 kumaneko 856 int pos = head->read_avail;
1677 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1678     ccs_net2keyword(ptr->operation_type)))
1679     goto out;
1680 kumaneko 856 switch (ptr->record_type) {
1681     case IP_RECORD_TYPE_ADDRESS_GROUP:
1682 kumaneko 1052 if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1683     goto out;
1684 kumaneko 856 break;
1685     case IP_RECORD_TYPE_IPv4:
1686 kumaneko 1052 if (!print_ipv4_entry(head, ptr))
1687     goto out;
1688 kumaneko 856 break;
1689     case IP_RECORD_TYPE_IPv6:
1690 kumaneko 1052 if (!print_ipv6_entry(head, ptr))
1691     goto out;
1692 kumaneko 856 break;
1693     }
1694 kumaneko 1052 if (!print_port_entry(head, ptr))
1695     goto out;
1696 kumaneko 1054 if (!ccs_print_condition(head, cond))
1697 kumaneko 1052 goto out;
1698 kumaneko 1006 return true;
1699 kumaneko 856 out:
1700     head->read_avail = pos;
1701 kumaneko 1006 return false;
1702 kumaneko 856 }
1703    
1704 kumaneko 1052 /**
1705 kumaneko 1054 * print_signal_acl - Print a signal ACL entry.
1706 kumaneko 1052 *
1707     * @head: Pointer to "struct ccs_io_buffer".
1708     * @ptr: Pointer to "struct signale_acl_record".
1709     * @cond: Pointer to "struct condition_list *". May be NULL.
1710     *
1711     * Returns true on success, false otherwise.
1712     */
1713     static bool print_signal_acl(struct ccs_io_buffer *head,
1714     struct signal_acl_record *ptr,
1715     const struct condition_list *cond)
1716 kumaneko 856 {
1717     int pos = head->read_avail;
1718 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
1719     ptr->sig, ptr->domainname->name))
1720     goto out;
1721 kumaneko 1054 if (!ccs_print_condition(head, cond))
1722 kumaneko 1052 goto out;
1723 kumaneko 1006 return true;
1724 kumaneko 856 out:
1725     head->read_avail = pos;
1726 kumaneko 1006 return false;
1727 kumaneko 856 }
1728    
1729 kumaneko 1052 /**
1730 kumaneko 1054 * print_execute_handler_record - Print an execute handler ACL entry.
1731 kumaneko 1052 *
1732     * @head: Pointer to "struct ccs_io_buffer".
1733     * @keyword: Name of the keyword.
1734     * @ptr: Pointer to "struct execute_handler_record".
1735     *
1736     * Returns true on success, false otherwise.
1737     */
1738     static bool print_execute_handler_record(struct ccs_io_buffer *head,
1739     const char *keyword,
1740     struct execute_handler_record *ptr)
1741 kumaneko 1029 {
1742 kumaneko 1052 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1743 kumaneko 1029 }
1744    
1745 kumaneko 1052 /**
1746 kumaneko 1054 * print_entry - Print an ACL entry.
1747 kumaneko 1052 *
1748     * @head: Pointer to "struct ccs_io_buffer".
1749     * @ptr: Pointer to an ACL entry.
1750     *
1751     * Returns true on success, false otherwise.
1752     */
1753     static bool print_entry(struct ccs_io_buffer *head, struct acl_info *ptr)
1754 kumaneko 111 {
1755 kumaneko 1052 const struct condition_list *cond = ccs_get_condition_part(ptr);
1756     const u8 acl_type = ptr->type & ~ACL_WITH_CONDITION;
1757     if (acl_type & ACL_DELETED)
1758     return true;
1759     if (acl_type == TYPE_SINGLE_PATH_ACL) {
1760     struct single_path_acl_record *acl
1761     = container_of(ptr, struct single_path_acl_record,
1762     head);
1763     return print_single_path_acl(head, acl, cond);
1764     }
1765     if (acl_type == TYPE_DOUBLE_PATH_ACL) {
1766     struct double_path_acl_record *acl
1767     = container_of(ptr, struct double_path_acl_record,
1768     head);
1769     return print_double_path_acl(head, acl, cond);
1770     }
1771     if (acl_type == TYPE_ARGV0_ACL) {
1772     struct argv0_acl_record *acl
1773     = container_of(ptr, struct argv0_acl_record, head);
1774     return print_argv0_acl(head, acl, cond);
1775     }
1776     if (acl_type == TYPE_ENV_ACL) {
1777     struct env_acl_record *acl
1778     = container_of(ptr, struct env_acl_record, head);
1779     return print_env_acl(head, acl, cond);
1780     }
1781     if (acl_type == TYPE_CAPABILITY_ACL) {
1782     struct capability_acl_record *acl
1783     = container_of(ptr, struct capability_acl_record, head);
1784     return print_capability_acl(head, acl, cond);
1785     }
1786     if (acl_type == TYPE_IP_NETWORK_ACL) {
1787     struct ip_network_acl_record *acl
1788     = container_of(ptr, struct ip_network_acl_record, head);
1789     return print_network_acl(head, acl, cond);
1790     }
1791     if (acl_type == TYPE_SIGNAL_ACL) {
1792     struct signal_acl_record *acl
1793     = container_of(ptr, struct signal_acl_record, head);
1794     return print_signal_acl(head, acl, cond);
1795     }
1796     if (acl_type == TYPE_PREFERRED_EXECUTE_HANDLER) {
1797     struct execute_handler_record *acl
1798     = container_of(ptr, struct execute_handler_record,
1799     head);
1800     const char *keyword = KEYWORD_PREFERRED_EXECUTE_HANDLER;
1801     return print_execute_handler_record(head, keyword, acl);
1802     }
1803     if (acl_type == TYPE_DEFAULT_EXECUTE_HANDLER) {
1804     struct execute_handler_record *acl
1805     = container_of(ptr, struct execute_handler_record,
1806     head);
1807     const char *keyword = KEYWORD_DEFAULT_EXECUTE_HANDLER;
1808     return print_execute_handler_record(head, keyword, acl);
1809     }
1810     BUG(); /* This must not happen. */
1811     return false;
1812     }
1813    
1814     /**
1815     * read_domain_policy - Read domain policy.
1816     *
1817     * @head: Pointer to "struct ccs_io_buffer".
1818     *
1819     * Returns 0.
1820     */
1821     static int read_domain_policy(struct ccs_io_buffer *head)
1822     {
1823 kumaneko 722 struct list1_head *dpos;
1824     struct list1_head *apos;
1825 kumaneko 1052 if (head->read_eof)
1826     return 0;
1827     if (head->read_step == 0)
1828     head->read_step = 1;
1829 kumaneko 722 list1_for_each_cookie(dpos, head->read_var1, &domain_list) {
1830 kumaneko 708 struct domain_info *domain;
1831 kumaneko 1052 const char *quota_exceeded = "";
1832     const char *ignore_global_allow_read = "";
1833     const char *ignore_global_allow_env = "";
1834 kumaneko 722 domain = list1_entry(dpos, struct domain_info, list);
1835 kumaneko 1052 if (head->read_step != 1)
1836     goto acl_loop;
1837     if (domain->is_deleted)
1838     continue;
1839 kumaneko 1054 /* Print domainname and flags. */
1840 kumaneko 1052 if (domain->quota_warned)
1841     quota_exceeded = "quota_exceeded\n";
1842     if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
1843     ignore_global_allow_read
1844     = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
1845     if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV)
1846     ignore_global_allow_env
1847     = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
1848     if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
1849     "%s%s%s\n", domain->domainname->name,
1850     domain->profile, quota_exceeded,
1851     ignore_global_allow_read,
1852     ignore_global_allow_env))
1853     return 0;
1854 kumaneko 708 head->read_step = 2;
1855 kumaneko 1052 acl_loop:
1856     if (head->read_step == 3)
1857     goto tail_mark;
1858 kumaneko 1054 /* Print ACL entries in the domain. */
1859 kumaneko 1052 list1_for_each_cookie(apos, head->read_var2,
1860     &domain->acl_info_list) {
1861     struct acl_info *ptr
1862     = list1_entry(apos, struct acl_info, list);
1863     if (!print_entry(head, ptr))
1864     return 0;
1865 kumaneko 111 }
1866 kumaneko 708 head->read_step = 3;
1867 kumaneko 1052 tail_mark:
1868     if (!ccs_io_printf(head, "\n"))
1869     return 0;
1870 kumaneko 708 head->read_step = 1;
1871 kumaneko 111 }
1872 kumaneko 1006 head->read_eof = true;
1873 kumaneko 111 return 0;
1874     }
1875    
1876 kumaneko 461 #endif
1877    
1878 kumaneko 1052 /**
1879     * write_domain_profile - Assign profile for specified domain.
1880     *
1881     * @head: Pointer to "struct ccs_io_buffer".
1882     *
1883     * Returns 0 on success, -EINVAL otherwise.
1884     *
1885     * This is equivalent to doing
1886 kumaneko 1054 *
1887     * ( echo "select " $domainname; echo "use_profile " $profile ) |
1888 kumaneko 1052 * /usr/lib/ccs/ccs-loadpolicy -d
1889     */
1890     static int write_domain_profile(struct ccs_io_buffer *head)
1891 kumaneko 461 {
1892     char *data = head->write_buf;
1893     char *cp = strchr(data, ' ');
1894     struct domain_info *domain;
1895     unsigned int profile;
1896 kumaneko 1052 if (!cp)
1897     return -EINVAL;
1898 kumaneko 461 *cp = '\0';
1899 kumaneko 1052 domain = ccs_find_domain(cp + 1);
1900 kumaneko 461 profile = simple_strtoul(data, NULL, 10);
1901 kumaneko 1052 if (domain && profile < MAX_PROFILES
1902     && (profile_ptr[profile] || !sbin_init_started))
1903     domain->profile = (u8) profile;
1904     ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1905 kumaneko 461 return 0;
1906     }
1907    
1908 kumaneko 1052 /**
1909     * read_domain_profile - Read only domainname and profile.
1910     *
1911     * @head: Pointer to "struct ccs_io_buffer".
1912     *
1913     * Returns list of profile number and domainname pairs.
1914     *
1915     * This is equivalent to doing
1916 kumaneko 1054 *
1917     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
1918     * awk ' BEGIN { if ( domainname == "") domainname = $0;
1919     * else if ( $1 = "use_profile" ) {
1920     * print $2 " " domainname; domainname = ""; }; '
1921 kumaneko 1052 */
1922     static int read_domain_profile(struct ccs_io_buffer *head)
1923 kumaneko 111 {
1924 kumaneko 722 struct list1_head *pos;
1925 kumaneko 1052 if (head->read_eof)
1926     return 0;
1927 kumaneko 722 list1_for_each_cookie(pos, head->read_var1, &domain_list) {
1928 kumaneko 111 struct domain_info *domain;
1929 kumaneko 722 domain = list1_entry(pos, struct domain_info, list);
1930 kumaneko 1052 if (domain->is_deleted)
1931     continue;
1932     if (!ccs_io_printf(head, "%u %s\n", domain->profile,
1933     domain->domainname->name))
1934     return 0;
1935 kumaneko 111 }
1936 kumaneko 1006 head->read_eof = true;
1937 kumaneko 111 return 0;
1938     }
1939    
1940 kumaneko 1052 /**
1941     * write_pid: Specify PID to obtain domainname.
1942     *
1943     * @head: Pointer to "struct ccs_io_buffer".
1944     *
1945     * Returns 0.
1946     */
1947     static int write_pid(struct ccs_io_buffer *head)
1948 kumaneko 111 {
1949     head->read_step = (int) simple_strtoul(head->write_buf, NULL, 10);
1950 kumaneko 1006 head->read_eof = false;
1951 kumaneko 111 return 0;
1952     }
1953    
1954 kumaneko 1052 /**
1955     * read_pid - Get domainname of the specified PID.
1956     *
1957     * @head: Pointer to "struct ccs_io_buffer".
1958     *
1959     * Returns the domainname which the specified PID is in on success,
1960     * empty string otherwise.
1961     * The PID is specified by write_pid() so that the user can obtain
1962     * using read()/write() interface rather than sysctl() interface.
1963     */
1964     static int read_pid(struct ccs_io_buffer *head)
1965 kumaneko 111 {
1966     if (head->read_avail == 0 && !head->read_eof) {
1967     const int pid = head->read_step;
1968     struct task_struct *p;
1969     struct domain_info *domain = NULL;
1970     /***** CRITICAL SECTION START *****/
1971     read_lock(&tasklist_lock);
1972     p = find_task_by_pid(pid);
1973 kumaneko 1052 if (p)
1974     domain = p->domain_info;
1975 kumaneko 111 read_unlock(&tasklist_lock);
1976     /***** CRITICAL SECTION END *****/
1977 kumaneko 1052 if (domain)
1978     ccs_io_printf(head, "%d %u %s", pid, domain->profile,
1979     domain->domainname->name);
1980 kumaneko 1006 head->read_eof = true;
1981 kumaneko 111 }
1982     return 0;
1983     }
1984    
1985     #ifdef CONFIG_TOMOYO
1986    
1987 kumaneko 1052 /**
1988     * write_exception_policy - Write exception policy.
1989     *
1990     * @head: Pointer to "struct ccs_io_buffer".
1991     *
1992     * Returns 0 on success, negative value otherwise.
1993     */
1994     static int write_exception_policy(struct ccs_io_buffer *head)
1995 kumaneko 111 {
1996     char *data = head->write_buf;
1997 kumaneko 1052 bool is_delete = str_starts(&data, KEYWORD_DELETE);
1998     ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);
1999     if (str_starts(&data, KEYWORD_KEEP_DOMAIN))
2000     return ccs_write_domain_keeper_policy(data, false, is_delete);
2001     if (str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))
2002     return ccs_write_domain_keeper_policy(data, true, is_delete);
2003     if (str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))
2004     return ccs_write_domain_initializer_policy(data, false,
2005     is_delete);
2006     if (str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))
2007     return ccs_write_domain_initializer_policy(data, true,
2008     is_delete);
2009     if (str_starts(&data, KEYWORD_ALIAS))
2010     return ccs_write_alias_policy(data, is_delete);
2011     if (str_starts(&data, KEYWORD_AGGREGATOR))
2012     return ccs_write_aggregator_policy(data, is_delete);
2013     if (str_starts(&data, KEYWORD_ALLOW_READ))
2014     return ccs_write_globally_readable_policy(data, is_delete);
2015     if (str_starts(&data, KEYWORD_ALLOW_ENV))
2016     return ccs_write_globally_usable_env_policy(data, is_delete);
2017     if (str_starts(&data, KEYWORD_FILE_PATTERN))
2018     return ccs_write_pattern_policy(data, is_delete);
2019     if (str_starts(&data, KEYWORD_PATH_GROUP))
2020     return ccs_write_path_group_policy(data, is_delete);
2021     if (str_starts(&data, KEYWORD_DENY_REWRITE))
2022     return ccs_write_no_rewrite_policy(data, is_delete);
2023     if (str_starts(&data, KEYWORD_ADDRESS_GROUP))
2024     return ccs_write_address_group_policy(data, is_delete);
2025 kumaneko 111 return -EINVAL;
2026     }
2027    
2028 kumaneko 1052 /**
2029     * read_exception_policy - Read exception policy.
2030     *
2031     * @head: Pointer to "struct ccs_io_buffer".
2032     *
2033     * Returns 0 on success, -EINVAL otherwise.
2034     */
2035     static int read_exception_policy(struct ccs_io_buffer *head)
2036 kumaneko 111 {
2037     if (!head->read_eof) {
2038     switch (head->read_step) {
2039     case 0:
2040 kumaneko 1052 head->read_var2 = NULL;
2041     head->read_step = 1;
2042 kumaneko 111 case 1:
2043 kumaneko 1052 if (!ccs_read_domain_keeper_policy(head))
2044     break;
2045     head->read_var2 = NULL;
2046     head->read_step = 2;
2047 kumaneko 111 case 2:
2048 kumaneko 1052 if (!ccs_read_globally_readable_policy(head))
2049     break;
2050     head->read_var2 = NULL;
2051     head->read_step = 3;
2052 kumaneko 111 case 3:
2053 kumaneko 1052 if (!ccs_read_globally_usable_env_policy(head))
2054     break;
2055     head->read_var2 = NULL;
2056     head->read_step = 4;
2057 kumaneko 111 case 4:
2058 kumaneko 1052 if (!ccs_read_domain_initializer_policy(head))
2059     break;
2060     head->read_var2 = NULL;
2061     head->read_step = 5;
2062 kumaneko 111 case 5:
2063 kumaneko 1052 if (!ccs_read_alias_policy(head))
2064     break;
2065     head->read_var2 = NULL;
2066     head->read_step = 6;
2067 kumaneko 111 case 6:
2068 kumaneko 1052 if (!ccs_read_aggregator_policy(head))
2069     break;
2070     head->read_var2 = NULL;
2071     head->read_step = 7;
2072 kumaneko 111 case 7:
2073 kumaneko 1052 if (!ccs_read_file_pattern(head))
2074     break;
2075     head->read_var2 = NULL;
2076     head->read_step = 8;
2077 kumaneko 111 case 8:
2078 kumaneko 1052 if (!ccs_read_no_rewrite_policy(head))
2079     break;
2080     head->read_var2 = NULL;
2081     head->read_step = 9;
2082 kumaneko 581 case 9:
2083 kumaneko 1052 if (!ccs_read_path_group_policy(head))
2084     break;
2085     head->read_var1 = NULL;
2086     head->read_var2 = NULL;
2087     head->read_step = 10;
2088 kumaneko 581 case 10:
2089 kumaneko 1052 if (!ccs_read_address_group_policy(head))
2090     break;
2091 kumaneko 1006 head->read_eof = true;
2092 kumaneko 111 break;
2093     default:
2094     return -EINVAL;
2095     }
2096     }
2097     return 0;
2098     }
2099    
2100     #endif
2101    
2102     #ifdef CONFIG_SAKURA
2103    
2104 kumaneko 1052 /**
2105     * write_system_policy - Write system policy.
2106     *
2107     * @head: Pointer to "struct ccs_io_buffer".
2108     *
2109     * Returns 0 on success, negative value otherwise.
2110     */
2111     static int write_system_policy(struct ccs_io_buffer *head)
2112 kumaneko 111 {
2113     char *data = head->write_buf;
2114 kumaneko 1006 bool is_delete = false;
2115 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_SYSTEM_POLICY);
2116     if (str_starts(&data, KEYWORD_DELETE))
2117 kumaneko 1006 is_delete = true;
2118 kumaneko 1052 if (str_starts(&data, KEYWORD_ALLOW_MOUNT))
2119     return ccs_write_mount_policy(data, is_delete);
2120     if (str_starts(&data, KEYWORD_DENY_UNMOUNT))
2121     return ccs_write_no_umount_policy(data, is_delete);
2122     if (str_starts(&data, KEYWORD_ALLOW_CHROOT))
2123     return ccs_write_chroot_policy(data, is_delete);
2124     if (str_starts(&data, KEYWORD_ALLOW_PIVOT_ROOT))
2125     return ccs_write_pivot_root_policy(data, is_delete);
2126     if (str_starts(&data, KEYWORD_DENY_AUTOBIND))
2127     return ccs_write_reserved_port_policy(data, is_delete);
2128 kumaneko 111 return -EINVAL;
2129     }
2130    
2131 kumaneko 1052 /**
2132     * read_system_policy - Read system policy.
2133     *
2134     * @head: Pointer to "struct ccs_io_buffer".
2135     *
2136     * Returns 0 on success, -EINVAL otherwise.
2137     */
2138     static int read_system_policy(struct ccs_io_buffer *head)
2139 kumaneko 111 {
2140     if (!head->read_eof) {
2141     switch (head->read_step) {
2142     case 0:
2143 kumaneko 1052 head->read_var2 = NULL;
2144     head->read_step = 1;
2145 kumaneko 111 case 1:
2146 kumaneko 1052 if (!ccs_read_mount_policy(head))
2147     break;
2148     head->read_var2 = NULL;
2149     head->read_step = 2;
2150 kumaneko 111 case 2:
2151 kumaneko 1052 if (!ccs_read_no_umount_policy(head))
2152     break;
2153     head->read_var2 = NULL;
2154     head->read_step = 3;
2155 kumaneko 111 case 3:
2156 kumaneko 1052 if (!ccs_read_chroot_policy(head))
2157     break;
2158     head->read_var2 = NULL;
2159     head->read_step = 4;
2160 kumaneko 111 case 4:
2161 kumaneko 1052 if (!ccs_read_pivot_root_policy(head))
2162     break;
2163     head->read_var2 = NULL;
2164     head->read_step = 5;
2165 kumaneko 141 case 5:
2166 kumaneko 1052 if (!ccs_read_reserved_port_policy(head))
2167     break;
2168 kumaneko 1006 head->read_eof = true;
2169 kumaneko 111 break;
2170     default:
2171     return -EINVAL;
2172     }
2173     }
2174     return 0;
2175     }
2176    
2177     #endif
2178    
2179 kumaneko 1052 /* Profile loaded by policy loader? */
2180 kumaneko 1006 static bool profile_loaded = false;
2181 kumaneko 325
2182 kumaneko 1052 /* Path to the policy loader. The default is /sbin/ccs-init . */
2183     static const char *ccs_loader;
2184 kumaneko 111
2185 kumaneko 1052 /**
2186     * loader_setup - Specify the policy loader to use.
2187     *
2188     * @str: Path to the policy loader.
2189     *
2190     * Returns 0.
2191     */
2192     static int __init loader_setup(char *str)
2193 kumaneko 111 {
2194     ccs_loader = str;
2195     return 0;
2196     }
2197    
2198 kumaneko 1052 __setup("CCS_loader=", loader_setup);
2199 kumaneko 111
2200 kumaneko 1052 /**
2201     * policy_loader_exists - Check whether /sbin/ccs-init exists.
2202     *
2203     * Returns true if /sbin/ccs-init exists, false otherwise.
2204     */
2205     static bool policy_loader_exists(void)
2206 kumaneko 111 {
2207     /*
2208 kumaneko 1052 * Don't activate MAC if the path given by 'CCS_loader=' option doesn't
2209     * exist. If the initrd includes /sbin/init but real-root-dev has not
2210     * mounted on / yet, activating MAC will block the system since
2211     * policies are not loaded yet.
2212     * Thus, let do_execve() call this function everytime.
2213 kumaneko 111 */
2214 kumaneko 1052 struct nameidata nd;
2215     if (!ccs_loader)
2216     ccs_loader = "/sbin/ccs-init";
2217     if (path_lookup(ccs_loader, lookup_flags, &nd)) {
2218     printk(KERN_INFO "Not activating Mandatory Access Control now "
2219     "since %s doesn't exist.\n", ccs_loader);
2220     return false;
2221     }
2222     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
2223     path_put(&nd.path);
2224     #else
2225     path_release(&nd);
2226     #endif
2227     return true;
2228     }
2229    
2230     /**
2231     * ccs_load_policy - Run external policy loader to load policy.
2232     *
2233     * @filename: The program about to start.
2234     *
2235     * This function checks whether @filename is /sbin/init , and if so
2236     * invoke /sbin/ccs-init and wait for the termination of /sbin/ccs-init
2237     * and then continues invocation of /sbin/init .
2238     * /sbin/ccs-init reads policy files in /etc/ccs/ directory and
2239     * writes to /proc/ccs/ interfaces.
2240     *
2241     * Returns nothing.
2242     */
2243     void ccs_load_policy(const char *filename)
2244     {
2245     if (sbin_init_started)
2246     return;
2247 kumaneko 111 /*
2248 kumaneko 1052 * Check filename is /sbin/init or /sbin/ccs-start .
2249     * /sbin/ccs-start is a dummy filename in case where /sbin/init can't
2250     * be passed.
2251     * You can create /sbin/ccs-start by "ln -s /bin/true /sbin/ccs-start".
2252 kumaneko 111 */
2253 kumaneko 1052 if (strcmp(filename, "/sbin/init") &&
2254     strcmp(filename, "/sbin/ccs-start"))
2255     return;
2256     if (!policy_loader_exists())
2257     return;
2258 kumaneko 325 if (!profile_loaded) {
2259     char *argv[2], *envp[3];
2260 kumaneko 1052 printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
2261     ccs_loader);
2262 kumaneko 325 argv[0] = (char *) ccs_loader;
2263     argv[1] = NULL;
2264     envp[0] = "HOME=/";
2265     envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2266     envp[2] = NULL;
2267 kumaneko 1052 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
2268 kumaneko 325 call_usermodehelper(argv[0], argv, envp, 1);
2269     #else
2270     call_usermodehelper(argv[0], argv, envp);
2271     #endif
2272     while (!profile_loaded) {
2273     set_current_state(TASK_INTERRUPTIBLE);
2274     schedule_timeout(HZ / 10);
2275     }
2276     }
2277 kumaneko 473 #ifdef CONFIG_SAKURA
2278 kumaneko 1052 printk(KERN_INFO "SAKURA: 1.6.0-pre 2008/03/24\n");
2279 kumaneko 473 #endif
2280     #ifdef CONFIG_TOMOYO
2281 kumaneko 1052 printk(KERN_INFO "TOMOYO: 1.6.0-pre 2008/03/24\n");
2282 kumaneko 473 #endif
2283 kumaneko 1052 printk(KERN_INFO "Mandatory Access Control activated.\n");
2284 kumaneko 1006 sbin_init_started = true;
2285 kumaneko 111 ccs_log_level = KERN_WARNING;
2286     { /* Check all profiles currently assigned to domains are defined. */
2287     struct domain_info *domain;
2288 kumaneko 722 list1_for_each_entry(domain, &domain_list, list) {
2289 kumaneko 111 const u8 profile = domain->profile;
2290 kumaneko 1052 if (profile_ptr[profile])
2291     continue;
2292     panic("Profile %u (used by '%s') not defined.\n",
2293     profile, domain->domainname->name);
2294 kumaneko 111 }
2295     }
2296     }
2297    
2298 kumaneko 1052 /* Wait queue for query_list. */
2299 kumaneko 111 static DECLARE_WAIT_QUEUE_HEAD(query_wait);
2300    
2301 kumaneko 1052 /* Lock for manipurating query_list. */
2302     static DEFINE_SPINLOCK(query_lock);
2303 kumaneko 111
2304 kumaneko 1052 /* Structure for query. */
2305 kumaneko 214 struct query_entry {
2306 kumaneko 111 struct list_head list;
2307     char *query;
2308     int query_len;
2309     unsigned int serial;
2310     int timer;
2311     int answer;
2312 kumaneko 214 };
2313 kumaneko 111
2314 kumaneko 1052 /* The list for "struct query_entry". */
2315 kumaneko 111 static LIST_HEAD(query_list);
2316 kumaneko 1052
2317     /* Number of "struct file" referring /proc/ccs/query interface. */
2318 kumaneko 111 static atomic_t queryd_watcher = ATOMIC_INIT(0);
2319    
2320 kumaneko 1052 /**
2321     * ccs_check_supervisor - Ask for the supervisor's decision.
2322     *
2323     * @fmt: The printf()'s format string, followed by parameters.
2324     *
2325     * Returns 0 if the supervisor decided to permit the access request which
2326     * violated the policy in enforcing mode, -EPERM otherwise.
2327     */
2328     int ccs_check_supervisor(const char *fmt, ...)
2329 kumaneko 111 {
2330     va_list args;
2331     int error = -EPERM;
2332     int pos, len;
2333 kumaneko 1052 static unsigned int serial;
2334 kumaneko 214 struct query_entry *query_entry;
2335 kumaneko 1052 if (!ccs_check_flags(CCS_ALLOW_ENFORCE_GRACE)
2336     || !atomic_read(&queryd_watcher)) {
2337     int i;
2338     if (current->tomoyo_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2339     return -EPERM;
2340     for (i = 0; i < ccs_check_flags(CCS_SLEEP_PERIOD); i++) {
2341     set_current_state(TASK_INTERRUPTIBLE);
2342     schedule_timeout(HZ / 10);
2343 kumaneko 708 }
2344     return -EPERM;
2345     }
2346 kumaneko 111 va_start(args, fmt);
2347     len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2348     va_end(args);
2349 kumaneko 1052 query_entry = ccs_alloc(sizeof(*query_entry));
2350     if (!query_entry)
2351     goto out;
2352     query_entry->query = ccs_alloc(len);
2353     if (!query_entry->query)
2354     goto out;
2355 kumaneko 111 INIT_LIST_HEAD(&query_entry->list);
2356     /***** CRITICAL SECTION START *****/
2357     spin_lock(&query_lock);
2358     query_entry->serial = serial++;
2359     spin_unlock(&query_lock);
2360     /***** CRITICAL SECTION END *****/
2361 kumaneko 1052 pos = snprintf(query_entry->query, len - 1, "Q%u\n",
2362     query_entry->serial);
2363 kumaneko 111 va_start(args, fmt);
2364     vsnprintf(query_entry->query + pos, len - 1 - pos, fmt, args);
2365     query_entry->query_len = strlen(query_entry->query) + 1;
2366     va_end(args);
2367     /***** CRITICAL SECTION START *****/
2368     spin_lock(&query_lock);
2369     list_add_tail(&query_entry->list, &query_list);
2370     spin_unlock(&query_lock);
2371     /***** CRITICAL SECTION END *****/
2372 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2373 kumaneko 111 /* Give 10 seconds for supervisor's opinion. */
2374 kumaneko 1052 for (query_entry->timer = 0; atomic_read(&queryd_watcher)
2375     && ccs_check_flags(CCS_ALLOW_ENFORCE_GRACE)
2376     && query_entry->timer < 100; query_entry->timer++) {
2377 kumaneko 111 wake_up(&query_wait);
2378     set_current_state(TASK_INTERRUPTIBLE);
2379     schedule_timeout(HZ / 10);
2380 kumaneko 1052 if (query_entry->answer)
2381     break;
2382 kumaneko 111 }
2383 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_QUERY);
2384 kumaneko 111 /***** CRITICAL SECTION START *****/
2385     spin_lock(&query_lock);
2386     list_del(&query_entry->list);
2387     spin_unlock(&query_lock);
2388     /***** CRITICAL SECTION END *****/
2389     switch (query_entry->answer) {
2390     case 1:
2391     /* Granted by administrator. */
2392     error = 0;
2393     break;
2394     case 0:
2395     /* Timed out. */
2396     break;
2397     default:
2398     /* Rejected by administrator. */
2399     break;
2400     }
2401 kumaneko 1052 out:
2402     if (query_entry)
2403     ccs_free(query_entry->query);
2404 kumaneko 111 ccs_free(query_entry);
2405     return error;
2406     }
2407    
2408 kumaneko 1052 /**
2409     * poll_query - poll() for /proc/ccs/query .
2410     *
2411     * @file: Pointer to "struct file".
2412     * @wait: Pointer to "poll_table".
2413     *
2414     * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
2415     *
2416     * Waits for access requests which violated policy in enforcing mode.
2417     */<