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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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