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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2030 - (hide annotations) (download) (as text)
Thu Jan 1 01:40:06 2009 UTC (15 years, 4 months ago) by kumaneko
Original Path: trunk/1.6.x/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 88484 byte(s)


1 kumaneko 111 /*
2     * fs/ccs_common.c
3     *
4     * Common functions for SAKURA and TOMOYO.
5     *
6 kumaneko 2030 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 kumaneko 111 *
8 kumaneko 2002 * Version: 1.6.6-pre 2008/12/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 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 kumaneko 2002 static const int ccs_lookup_flags = LOOKUP_FOLLOW;
31 kumaneko 111 #else
32 kumaneko 2002 static const int ccs_lookup_flags = LOOKUP_FOLLOW | LOOKUP_POSITIVE;
33 kumaneko 111 #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 2002 bool ccs_sbin_init_started;
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 kumaneko 2002 static const char *ccs_mode_4[4] = {
66 kumaneko 1052 "disabled", "learning", "permissive", "enforcing"
67     };
68     /* String table for functionality that takes 2 modes. */
69 kumaneko 2002 static const char *ccs_mode_2[4] = {
70 kumaneko 1052 "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 kumaneko 1052 [CCS_SLEEP_PERIOD]
98     = { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */
99 kumaneko 111 };
100    
101 kumaneko 1015 #ifdef CONFIG_TOMOYO
102 kumaneko 1052 /* Capability name used by domain policy. */
103 kumaneko 2002 static const char *ccs_capability_control_keyword[TOMOYO_MAX_CAPABILITY_INDEX]
104     = {
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 kumaneko 2002 static struct ccs_profile {
141 kumaneko 111 unsigned int value[CCS_MAX_CONTROL_INDEX];
142 kumaneko 2002 const struct ccs_path_info *comment;
143 kumaneko 1015 #ifdef CONFIG_TOMOYO
144     unsigned char capability_value[TOMOYO_MAX_CAPABILITY_INDEX];
145     #endif
146 kumaneko 2002 } *ccs_profile_ptr[MAX_PROFILES];
147 kumaneko 111
148 kumaneko 1052 /* Permit policy management by non-root user? */
149 kumaneko 2002 static bool ccs_manage_by_non_root;
150 kumaneko 111
151 kumaneko 1052 /* Utility functions. */
152 kumaneko 111
153     #ifdef CONFIG_TOMOYO
154 kumaneko 1052 /**
155 kumaneko 2002 * ccs_quiet_setup - Set TOMOYO_VERBOSE=0 by default.
156 kumaneko 1052 *
157     * @str: Unused.
158     *
159     * Returns 0.
160     */
161 kumaneko 2002 static int __init ccs_quiet_setup(char *str)
162 kumaneko 111 {
163     ccs_control_array[CCS_TOMOYO_VERBOSE].current_value = 0;
164     return 0;
165     }
166    
167 kumaneko 2002 __setup("TOMOYO_QUIET", ccs_quiet_setup);
168 kumaneko 111 #endif
169    
170 kumaneko 1052 /**
171 kumaneko 2002 * ccs_is_byte_range - Check whether the string isa \ooo style octal value.
172 kumaneko 1052 *
173     * @str: Pointer to the string.
174     *
175     * Returns true if @str is a \ooo style octal value, false otherwise.
176     */
177 kumaneko 2002 static inline bool ccs_is_byte_range(const char *str)
178 kumaneko 1052 {
179     return *str >= '0' && *str++ <= '3' &&
180     *str >= '0' && *str++ <= '7' &&
181     *str >= '0' && *str <= '7';
182     }
183    
184     /**
185 kumaneko 2002 * ccs_is_decimal - Check whether the character is a decimal character.
186 kumaneko 1052 *
187     * @c: The character to check.
188     *
189     * Returns true if @c is a decimal character, false otherwise.
190     */
191 kumaneko 2002 static inline bool ccs_is_decimal(const char c)
192 kumaneko 1052 {
193 kumaneko 1468 return c >= '0' && c <= '9';
194 kumaneko 1052 }
195    
196     /**
197 kumaneko 2002 * ccs_is_hexadecimal - Check whether the character is a hexadecimal character.
198 kumaneko 1052 *
199     * @c: The character to check.
200     *
201     * Returns true if @c is a hexadecimal character, false otherwise.
202     */
203 kumaneko 2002 static inline bool ccs_is_hexadecimal(const char c)
204 kumaneko 1052 {
205 kumaneko 1468 return (c >= '0' && c <= '9') ||
206 kumaneko 1052 (c >= 'A' && c <= 'F') ||
207 kumaneko 1468 (c >= 'a' && c <= 'f');
208 kumaneko 1052 }
209    
210     /**
211 kumaneko 2002 * ccs_is_alphabet_char - Check whether the character is an alphabet.
212 kumaneko 1052 *
213     * @c: The character to check.
214     *
215     * Returns true if @c is an alphabet character, false otherwise.
216     */
217 kumaneko 2002 static inline bool ccs_is_alphabet_char(const char c)
218 kumaneko 1052 {
219 kumaneko 1794 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
220 kumaneko 1052 }
221    
222     /**
223 kumaneko 2002 * ccs_make_byte - Make byte value from three octal characters.
224 kumaneko 1064 *
225     * @c1: The first character.
226     * @c2: The second character.
227     * @c3: The third character.
228     *
229     * Returns byte value.
230     */
231 kumaneko 2002 static inline u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3)
232 kumaneko 1064 {
233     return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
234     }
235    
236     /**
237 kumaneko 2002 * ccs_str_starts - Check whether the given string starts with the given keyword.
238 kumaneko 1052 *
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 kumaneko 2002 static bool ccs_str_starts(char **src, const char *find)
248 kumaneko 1052 {
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 kumaneko 2002 * ccs_normalize_line - Format string.
260 kumaneko 1052 *
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 2002 static void ccs_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 kumaneko 1795 c = *(filename + strlen(filename) - 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 kumaneko 1747 while (1) {
330     c = *filename++;
331     if (!c)
332     break;
333 kumaneko 111 if (c == '\\') {
334 kumaneko 1747 c = *filename++;
335     switch (c) {
336 kumaneko 111 case '\\': /* "\\" */
337     continue;
338     case '$': /* "\$" */
339     case '+': /* "\+" */
340     case '?': /* "\?" */
341     case '*': /* "\*" */
342     case '@': /* "\@" */
343     case 'x': /* "\x" */
344     case 'X': /* "\X" */
345     case 'a': /* "\a" */
346     case 'A': /* "\A" */
347 kumaneko 206 case '-': /* "\-" */
348 kumaneko 1052 if (pattern_type == -1)
349     break; /* Must not contain pattern */
350 kumaneko 1006 contains_pattern = true;
351 kumaneko 111 continue;
352     case '0': /* "\ooo" */
353     case '1':
354     case '2':
355     case '3':
356 kumaneko 1052 d = *filename++;
357     if (d < '0' || d > '7')
358     break;
359     e = *filename++;
360     if (e < '0' || e > '7')
361     break;
362 kumaneko 2002 c = ccs_make_byte(c, d, e);
363 kumaneko 1052 if (c && (c <= ' ' || c >= 127))
364     continue; /* pattern is not \000 */
365 kumaneko 111 }
366     goto out;
367     } else if (c <= ' ' || c >= 127) {
368     goto out;
369     }
370     }
371     if (pattern_type == 1) { /* Must contain pattern */
372 kumaneko 1052 if (!contains_pattern)
373     goto out;
374 kumaneko 111 }
375 kumaneko 1006 return true;
376 kumaneko 111 out:
377 kumaneko 1052 printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function,
378     original_filename);
379 kumaneko 1006 return false;
380 kumaneko 111 }
381    
382 kumaneko 1052 /**
383 kumaneko 1054 * ccs_is_correct_domain - Check whether the given domainname follows the naming rules.
384     * @domainname: The domainname to check.
385     * @function: The name of function calling me.
386 kumaneko 1052 *
387     * Returns true if @domainname follows the naming rules, false otherwise.
388 kumaneko 111 */
389 kumaneko 1052 bool ccs_is_correct_domain(const unsigned char *domainname,
390     const char *function)
391 kumaneko 111 {
392 kumaneko 1064 unsigned char c;
393     unsigned char d;
394     unsigned char e;
395 kumaneko 111 const char *org_domainname = domainname;
396 kumaneko 1052 if (!domainname || strncmp(domainname, ROOT_NAME, ROOT_NAME_LEN))
397     goto out;
398 kumaneko 111 domainname += ROOT_NAME_LEN;
399 kumaneko 1052 if (!*domainname)
400     return true;
401 kumaneko 111 do {
402 kumaneko 1052 if (*domainname++ != ' ')
403     goto out;
404     if (*domainname++ != '/')
405     goto out;
406 kumaneko 1747 while (1) {
407     c = *domainname;
408     if (!c || c == ' ')
409     break;
410 kumaneko 111 domainname++;
411     if (c == '\\') {
412 kumaneko 1052 c = *domainname++;
413     switch ((c)) {
414 kumaneko 111 case '\\': /* "\\" */
415     continue;
416     case '0': /* "\ooo" */
417     case '1':
418     case '2':
419     case '3':
420 kumaneko 1052 d = *domainname++;
421     if (d < '0' || d > '7')
422     break;
423     e = *domainname++;
424     if (e < '0' || e > '7')
425     break;
426 kumaneko 2002 c = ccs_make_byte(c, d, e);
427 kumaneko 1052 if (c && (c <= ' ' || c >= 127))
428     /* pattern is not \000 */
429     continue;
430 kumaneko 111 }
431     goto out;
432     } else if (c < ' ' || c >= 127) {
433     goto out;
434     }
435     }
436     } while (*domainname);
437 kumaneko 1006 return true;
438 kumaneko 111 out:
439 kumaneko 1052 printk(KERN_DEBUG "%s: Invalid domainname '%s'\n", function,
440     org_domainname);
441 kumaneko 1006 return false;
442 kumaneko 111 }
443    
444 kumaneko 1052 /**
445     * ccs_is_domain_def - Check whether the given token can be a domainname.
446     *
447     * @buffer: The token to check.
448     *
449     * Returns true if @buffer possibly be a domainname, false otherwise.
450     */
451     bool ccs_is_domain_def(const unsigned char *buffer)
452 kumaneko 461 {
453 kumaneko 1052 return !strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN);
454 kumaneko 461 }
455    
456 kumaneko 1052 /**
457     * ccs_find_domain - Find a domain by the given name.
458     *
459 kumaneko 1054 * @domainname: The domainname to find.
460 kumaneko 1052 *
461     * Returns pointer to "struct domain_info" if found, NULL otherwise.
462     */
463     struct domain_info *ccs_find_domain(const char *domainname)
464 kumaneko 461 {
465     struct domain_info *domain;
466 kumaneko 2002 struct ccs_path_info name;
467 kumaneko 1052 name.name = domainname;
468     ccs_fill_path_info(&name);
469 kumaneko 2002 list1_for_each_entry(domain, &ccs_domain_list, list) {
470 kumaneko 1052 if (!domain->is_deleted &&
471     !ccs_pathcmp(&name, domain->domainname))
472     return domain;
473 kumaneko 461 }
474     return NULL;
475     }
476    
477 kumaneko 1052 /**
478 kumaneko 2002 * ccs_path_depth - Evaluate the number of '/' in a string.
479 kumaneko 1052 *
480     * @pathname: The string to evaluate.
481     *
482     * Returns path depth of the string.
483     *
484     * I score 2 for each of the '/' in the @pathname
485     * and score 1 if the @pathname ends with '/'.
486     */
487 kumaneko 2002 static int ccs_path_depth(const char *pathname)
488 kumaneko 111 {
489     int i = 0;
490     if (pathname) {
491 kumaneko 1795 const char *ep = pathname + strlen(pathname);
492 kumaneko 111 if (pathname < ep--) {
493 kumaneko 1052 if (*ep != '/')
494     i++;
495     while (pathname <= ep)
496     if (*ep-- == '/')
497     i += 2;
498 kumaneko 111 }
499     }
500     return i;
501     }
502    
503 kumaneko 1052 /**
504 kumaneko 2002 * ccs_const_part_length - Evaluate the initial length without a pattern in a token.
505 kumaneko 1052 *
506     * @filename: The string to evaluate.
507     *
508 kumaneko 1064 * Returns the initial length without a pattern in @filename.
509 kumaneko 1052 */
510 kumaneko 2002 static int ccs_const_part_length(const char *filename)
511 kumaneko 111 {
512 kumaneko 1052 char c;
513 kumaneko 111 int len = 0;
514 kumaneko 1052 if (!filename)
515     return 0;
516 kumaneko 1747 while (1) {
517     c = *filename++;
518     if (!c)
519     break;
520 kumaneko 1052 if (c != '\\') {
521     len++;
522     continue;
523 kumaneko 111 }
524 kumaneko 1052 c = *filename++;
525     switch (c) {
526     case '\\': /* "\\" */
527     len += 2;
528     continue;
529     case '0': /* "\ooo" */
530     case '1':
531     case '2':
532     case '3':
533     c = *filename++;
534     if (c < '0' || c > '7')
535     break;
536     c = *filename++;
537     if (c < '0' || c > '7')
538     break;
539     len += 4;
540     continue;
541     }
542     break;
543 kumaneko 111 }
544     return len;
545     }
546    
547 kumaneko 1052 /**
548 kumaneko 2002 * ccs_fill_path_info - Fill in "struct ccs_path_info" members.
549 kumaneko 1052 *
550 kumaneko 2002 * @ptr: Pointer to "struct ccs_path_info" to fill in.
551 kumaneko 1052 *
552 kumaneko 2002 * The caller sets "struct ccs_path_info"->name.
553 kumaneko 1052 */
554 kumaneko 2002 void ccs_fill_path_info(struct ccs_path_info *ptr)
555 kumaneko 111 {
556     const char *name = ptr->name;
557     const int len = strlen(name);
558     ptr->total_len = len;
559 kumaneko 2002 ptr->const_len = ccs_const_part_length(name);
560 kumaneko 111 ptr->is_dir = len && (name[len - 1] == '/');
561     ptr->is_patterned = (ptr->const_len < len);
562     ptr->hash = full_name_hash(name, len);
563 kumaneko 2002 ptr->depth = ccs_path_depth(name);
564 kumaneko 111 }
565    
566 kumaneko 1052 /**
567 kumaneko 2002 * ccs_file_matches_to_pattern2 - Pattern matching without '/' character
568 kumaneko 1052 * and "\-" pattern.
569     *
570     * @filename: The start of string to check.
571     * @filename_end: The end of string to check.
572     * @pattern: The start of pattern to compare.
573     * @pattern_end: The end of pattern to compare.
574     *
575     * Returns true if @filename matches @pattern, false otherwise.
576     */
577 kumaneko 2002 static bool ccs_file_matches_to_pattern2(const char *filename,
578     const char *filename_end,
579     const char *pattern,
580     const char *pattern_end)
581 kumaneko 111 {
582     while (filename < filename_end && pattern < pattern_end) {
583 kumaneko 1052 char c;
584 kumaneko 111 if (*pattern != '\\') {
585 kumaneko 1052 if (*filename++ != *pattern++)
586     return false;
587     continue;
588     }
589     c = *filename;
590     pattern++;
591     switch (*pattern) {
592 kumaneko 1064 int i;
593     int j;
594 kumaneko 1052 case '?':
595     if (c == '/') {
596     return false;
597     } else if (c == '\\') {
598     if (filename[1] == '\\')
599     filename++;
600 kumaneko 2002 else if (ccs_is_byte_range(filename + 1))
601 kumaneko 1052 filename += 3;
602     else
603 kumaneko 1006 return false;
604 kumaneko 1052 }
605     break;
606     case '\\':
607     if (c != '\\')
608     return false;
609     if (*++filename != '\\')
610     return false;
611     break;
612     case '+':
613 kumaneko 2002 if (!ccs_is_decimal(c))
614 kumaneko 1052 return false;
615     break;
616     case 'x':
617 kumaneko 2002 if (!ccs_is_hexadecimal(c))
618 kumaneko 1052 return false;
619     break;
620     case 'a':
621 kumaneko 2002 if (!ccs_is_alphabet_char(c))
622 kumaneko 1052 return false;
623     break;
624     case '0':
625     case '1':
626     case '2':
627     case '3':
628 kumaneko 2002 if (c == '\\' && ccs_is_byte_range(filename + 1)
629 kumaneko 1052 && strncmp(filename + 1, pattern, 3) == 0) {
630     filename += 3;
631     pattern += 2;
632 kumaneko 111 break;
633 kumaneko 1052 }
634     return false; /* Not matched. */
635     case '*':
636     case '@':
637     for (i = 0; i <= filename_end - filename; i++) {
638 kumaneko 2002 if (ccs_file_matches_to_pattern2(filename + i,
639     filename_end,
640     pattern + 1,
641     pattern_end))
642 kumaneko 1052 return true;
643     c = filename[i];
644     if (c == '.' && *pattern == '@')
645 kumaneko 111 break;
646 kumaneko 1052 if (c != '\\')
647     continue;
648     if (filename[i + 1] == '\\')
649     i++;
650 kumaneko 2002 else if (ccs_is_byte_range(filename + i + 1))
651 kumaneko 1052 i += 3;
652     else
653     break; /* Bad pattern. */
654 kumaneko 111 }
655 kumaneko 1052 return false; /* Not matched. */
656     default:
657     j = 0;
658     c = *pattern;
659     if (c == '$') {
660 kumaneko 2002 while (ccs_is_decimal(filename[j]))
661 kumaneko 1052 j++;
662     } else if (c == 'X') {
663 kumaneko 2002 while (ccs_is_hexadecimal(filename[j]))
664 kumaneko 1052 j++;
665     } else if (c == 'A') {
666 kumaneko 2002 while (ccs_is_alphabet_char(filename[j]))
667 kumaneko 1052 j++;
668     }
669     for (i = 1; i <= j; i++) {
670 kumaneko 2002 if (ccs_file_matches_to_pattern2(filename + i,
671     filename_end,
672     pattern + 1,
673     pattern_end))
674 kumaneko 1052 return true;
675     }
676     return false; /* Not matched or bad pattern. */
677 kumaneko 111 }
678 kumaneko 1052 filename++;
679     pattern++;
680 kumaneko 111 }
681 kumaneko 1052 while (*pattern == '\\' &&
682     (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
683     pattern += 2;
684 kumaneko 1468 return filename == filename_end && pattern == pattern_end;
685 kumaneko 111 }
686    
687 kumaneko 1052 /**
688 kumaneko 2002 * ccs_file_matches_to_pattern - Pattern matching without without '/' character.
689 kumaneko 1052 *
690     * @filename: The start of string to check.
691     * @filename_end: The end of string to check.
692     * @pattern: The start of pattern to compare.
693     * @pattern_end: The end of pattern to compare.
694     *
695     * Returns true if @filename matches @pattern, false otherwise.
696     */
697 kumaneko 2002 static bool ccs_file_matches_to_pattern(const char *filename,
698     const char *filename_end,
699     const char *pattern,
700     const char *pattern_end)
701 kumaneko 206 {
702     const char *pattern_start = pattern;
703 kumaneko 1006 bool first = true;
704     bool result;
705 kumaneko 206 while (pattern < pattern_end - 1) {
706 kumaneko 1052 /* Split at "\-" pattern. */
707     if (*pattern++ != '\\' || *pattern++ != '-')
708     continue;
709 kumaneko 2002 result = ccs_file_matches_to_pattern2(filename, filename_end,
710     pattern_start,
711     pattern - 2);
712 kumaneko 1052 if (first)
713     result = !result;
714     if (result)
715     return false;
716 kumaneko 1006 first = false;
717 kumaneko 206 pattern_start = pattern;
718     }
719 kumaneko 2002 result = ccs_file_matches_to_pattern2(filename, filename_end,
720     pattern_start, pattern_end);
721 kumaneko 206 return first ? result : !result;
722     }
723    
724 kumaneko 1052 /**
725 kumaneko 1054 * ccs_path_matches_pattern - Check whether the given filename matches the given pattern.
726 kumaneko 1052 * @filename: The filename to check.
727     * @pattern: The pattern to compare.
728 kumaneko 111 *
729 kumaneko 1052 * Returns true if matches, false otherwise.
730     *
731     * The following patterns are available.
732     * \\ \ itself.
733     * \ooo Octal representation of a byte.
734     * \* More than or equals to 0 character other than '/'.
735     * \@ More than or equals to 0 character other than '/' or '.'.
736     * \? 1 byte character other than '/'.
737     * \$ More than or equals to 1 decimal digit.
738     * \+ 1 decimal digit.
739     * \X More than or equals to 1 hexadecimal digit.
740     * \x 1 hexadecimal digit.
741     * \A More than or equals to 1 alphabet character.
742     * \a 1 alphabet character.
743     * \- Subtraction operator.
744 kumaneko 111 */
745 kumaneko 2002 bool ccs_path_matches_pattern(const struct ccs_path_info *filename,
746     const struct ccs_path_info *pattern)
747 kumaneko 111 {
748 kumaneko 1052 /*
749     if (!filename || !pattern)
750     return false;
751     */
752     const char *f = filename->name;
753     const char *p = pattern->name;
754     const int len = pattern->const_len;
755     /* If @pattern doesn't contain pattern, I can use strcmp(). */
756     if (!pattern->is_patterned)
757     return !ccs_pathcmp(filename, pattern);
758     /* Dont compare if the number of '/' differs. */
759     if (filename->depth != pattern->depth)
760     return false;
761     /* Compare the initial length without patterns. */
762     if (strncmp(f, p, len))
763     return false;
764     f += len;
765     p += len;
766     /* Main loop. Compare each directory component. */
767     while (*f && *p) {
768     const char *f_delimiter = strchr(f, '/');
769     const char *p_delimiter = strchr(p, '/');
770     if (!f_delimiter)
771 kumaneko 1795 f_delimiter = f + strlen(f);
772 kumaneko 1052 if (!p_delimiter)
773 kumaneko 1795 p_delimiter = p + strlen(p);
774 kumaneko 2002 if (!ccs_file_matches_to_pattern(f, f_delimiter,
775     p, p_delimiter))
776 kumaneko 1052 return false;
777     f = f_delimiter;
778     if (*f)
779     f++;
780     p = p_delimiter;
781     if (*p)
782     p++;
783 kumaneko 111 }
784 kumaneko 1052 /* Ignore trailing "\*" and "\@" in @pattern. */
785     while (*p == '\\' &&
786     (*(p + 1) == '*' || *(p + 1) == '@'))
787     p += 2;
788 kumaneko 1468 return !*f && !*p;
789 kumaneko 111 }
790    
791 kumaneko 1052 /**
792     * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.
793     *
794     * @head: Pointer to "struct ccs_io_buffer".
795     * @fmt: The printf()'s format string, followed by parameters.
796     *
797     * Returns true on success, false otherwise.
798     *
799     * The snprintf() will truncate, but ccs_io_printf() won't.
800 kumaneko 111 */
801 kumaneko 1052 bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
802 kumaneko 111 {
803     va_list args;
804 kumaneko 1064 int len;
805     int pos = head->read_avail;
806     int size = head->readbuf_size - pos;
807 kumaneko 1052 if (size <= 0)
808     return false;
809 kumaneko 111 va_start(args, fmt);
810     len = vsnprintf(head->read_buf + pos, size, fmt, args);
811     va_end(args);
812 kumaneko 1052 if (pos + len >= head->readbuf_size)
813     return false;
814 kumaneko 111 head->read_avail += len;
815 kumaneko 1052 return true;
816 kumaneko 111 }
817    
818 kumaneko 1052 /**
819     * ccs_get_exe - Get ccs_realpath() of current process.
820     *
821     * Returns the ccs_realpath() of current process on success, NULL otherwise.
822     *
823     * This function uses ccs_alloc(), so the caller must ccs_free()
824     * if this function didn't return NULL.
825 kumaneko 111 */
826 kumaneko 1052 const char *ccs_get_exe(void)
827 kumaneko 111 {
828 kumaneko 737 struct mm_struct *mm = current->mm;
829     struct vm_area_struct *vma;
830     const char *cp = NULL;
831 kumaneko 1052 if (!mm)
832     return NULL;
833 kumaneko 737 down_read(&mm->mmap_sem);
834     for (vma = mm->mmap; vma; vma = vma->vm_next) {
835     if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
836 kumaneko 1052 cp = ccs_realpath_from_dentry(vma->vm_file->f_dentry,
837     vma->vm_file->f_vfsmnt);
838 kumaneko 737 break;
839 kumaneko 111 }
840     }
841 kumaneko 737 up_read(&mm->mmap_sem);
842     return cp;
843 kumaneko 111 }
844    
845 kumaneko 1052 /**
846     * ccs_get_msg - Get warning message.
847     *
848     * @is_enforce: Is it enforcing mode?
849     *
850     * Returns "ERROR" or "WARNING".
851     */
852     const char *ccs_get_msg(const bool is_enforce)
853 kumaneko 111 {
854 kumaneko 1052 if (is_enforce)
855     return "ERROR";
856     else
857     return "WARNING";
858 kumaneko 111 }
859    
860 kumaneko 1052 /**
861 kumaneko 1657 * ccs_can_sleep - Check whether it is permitted to do operations that may sleep.
862 kumaneko 1052 *
863     * Returns true if it is permitted to do operations that may sleep,
864     * false otherwise.
865     *
866     * TOMOYO Linux supports interactive enforcement that lets processes
867     * wait for the administrator's decision.
868     * All hooks but the one for ccs_may_autobind() are inserted where
869     * it is permitted to do operations that may sleep.
870     * Thus, this warning should not happen.
871     */
872 kumaneko 1657 bool ccs_can_sleep(void)
873 kumaneko 899 {
874 kumaneko 1052 static u8 count = 20;
875     if (likely(!in_interrupt()))
876     return true;
877     if (count) {
878     count--;
879     printk(KERN_ERR "BUG: sleeping function called "
880     "from invalid context.\n");
881     dump_stack();
882 kumaneko 899 }
883 kumaneko 1052 return false;
884 kumaneko 899 }
885    
886 kumaneko 1052 /**
887     * ccs_check_flags - Check mode for specified functionality.
888     *
889 kumaneko 1657 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
890     * @index: The functionality to check mode.
891 kumaneko 1052 *
892     * Returns the mode of specified functionality.
893     */
894 kumaneko 1657 unsigned int ccs_check_flags(const struct domain_info *domain, const u8 index)
895 kumaneko 1015 {
896 kumaneko 1657 u8 profile;
897     if (!domain)
898     domain = current->domain_info;
899     profile = domain->profile;
900 kumaneko 2002 return ccs_sbin_init_started && index < CCS_MAX_CONTROL_INDEX
901 kumaneko 1657 #if MAX_PROFILES != 256
902     && profile < MAX_PROFILES
903     #endif
904 kumaneko 2002 && ccs_profile_ptr[profile] ?
905     ccs_profile_ptr[profile]->value[index] : 0;
906 kumaneko 1015 }
907    
908     #ifdef CONFIG_TOMOYO
909 kumaneko 1052 /**
910     * ccs_check_capability_flags - Check mode for specified capability.
911     *
912 kumaneko 1657 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
913     * @index: The capability to check mode.
914 kumaneko 1052 *
915     * Returns the mode of specified capability.
916     */
917 kumaneko 1657 static u8 ccs_check_capability_flags(const struct domain_info *domain,
918     const u8 index)
919 kumaneko 1015 {
920 kumaneko 1657 const u8 profile = domain ? domain->profile :
921     current->domain_info->profile;
922 kumaneko 2002 return ccs_sbin_init_started && index < TOMOYO_MAX_CAPABILITY_INDEX
923 kumaneko 1015 #if MAX_PROFILES != 256
924     && profile < MAX_PROFILES
925     #endif
926 kumaneko 2002 && ccs_profile_ptr[profile] ?
927     ccs_profile_ptr[profile]->capability_value[index] : 0;
928 kumaneko 1015 }
929    
930 kumaneko 1052 /**
931     * ccs_cap2keyword - Convert capability operation to capability name.
932     *
933     * @operation: The capability index.
934     *
935     * Returns the name of the specified capability's name.
936     */
937     const char *ccs_cap2keyword(const u8 operation)
938 kumaneko 1015 {
939 kumaneko 1052 return operation < TOMOYO_MAX_CAPABILITY_INDEX
940 kumaneko 2002 ? ccs_capability_control_keyword[operation] : NULL;
941 kumaneko 1015 }
942    
943     #endif
944    
945 kumaneko 1052 /**
946 kumaneko 1657 * ccs_init_request_info - Initialize "struct ccs_request_info" members.
947     *
948     * @r: Pointer to "struct ccs_request_info" to initialize.
949     * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
950     * @index: Index number of functionality.
951     */
952     void ccs_init_request_info(struct ccs_request_info *r,
953     struct domain_info *domain, const u8 index)
954     {
955     memset(r, 0, sizeof(*r));
956     if (!domain)
957     domain = current->domain_info;
958     r->domain = domain;
959     r->profile = domain->profile;
960     if (index < CCS_MAX_CONTROL_INDEX)
961     r->mode = ccs_check_flags(domain, index);
962     #ifdef CONFIG_TOMOYO
963     else
964     r->mode = ccs_check_capability_flags(domain, index
965     - CCS_MAX_CONTROL_INDEX);
966     #endif
967     }
968    
969     /**
970 kumaneko 1052 * ccs_verbose_mode - Check whether TOMOYO is verbose mode.
971     *
972 kumaneko 1657 * @domain: Pointer to "struct domain_info". NULL for current->domain_info.
973     *
974 kumaneko 1052 * Returns true if domain policy violation warning should be printed to
975     * console.
976     */
977 kumaneko 1657 bool ccs_verbose_mode(const struct domain_info *domain)
978 kumaneko 111 {
979 kumaneko 1657 return ccs_check_flags(domain, CCS_TOMOYO_VERBOSE) != 0;
980 kumaneko 111 }
981    
982 kumaneko 1052 /**
983     * ccs_check_domain_quota - Check for domain's quota.
984     *
985     * @domain: Pointer to "struct domain_info".
986     *
987     * Returns true if the domain is not exceeded quota, false otherwise.
988     */
989     bool ccs_check_domain_quota(struct domain_info * const domain)
990 kumaneko 512 {
991     unsigned int count = 0;
992 kumaneko 2002 struct ccs_acl_info *ptr;
993 kumaneko 1052 if (!domain)
994     return true;
995 kumaneko 722 list1_for_each_entry(ptr, &domain->acl_info_list, list) {
996 kumaneko 1064 if (ptr->type & ACL_DELETED)
997     continue;
998     switch (ccs_acl_type2(ptr)) {
999 kumaneko 2002 struct ccs_single_path_acl_record *acl1;
1000     struct ccs_double_path_acl_record *acl2;
1001 kumaneko 1064 u16 perm;
1002     case TYPE_SINGLE_PATH_ACL:
1003 kumaneko 2002 acl1 = container_of(ptr,
1004     struct ccs_single_path_acl_record,
1005 kumaneko 1064 head);
1006     perm = acl1->perm;
1007     if (perm & (1 << TYPE_EXECUTE_ACL))
1008     count++;
1009     if (perm &
1010     ((1 << TYPE_READ_ACL) | (1 << TYPE_WRITE_ACL)))
1011     count++;
1012     if (perm & (1 << TYPE_CREATE_ACL))
1013     count++;
1014     if (perm & (1 << TYPE_UNLINK_ACL))
1015     count++;
1016     if (perm & (1 << TYPE_MKDIR_ACL))
1017     count++;
1018     if (perm & (1 << TYPE_RMDIR_ACL))
1019     count++;
1020     if (perm & (1 << TYPE_MKFIFO_ACL))
1021     count++;
1022     if (perm & (1 << TYPE_MKSOCK_ACL))
1023     count++;
1024     if (perm & (1 << TYPE_MKBLOCK_ACL))
1025     count++;
1026     if (perm & (1 << TYPE_MKCHAR_ACL))
1027     count++;
1028     if (perm & (1 << TYPE_TRUNCATE_ACL))
1029     count++;
1030     if (perm & (1 << TYPE_SYMLINK_ACL))
1031     count++;
1032     if (perm & (1 << TYPE_REWRITE_ACL))
1033     count++;
1034     break;
1035     case TYPE_DOUBLE_PATH_ACL:
1036 kumaneko 2002 acl2 = container_of(ptr,
1037     struct ccs_double_path_acl_record,
1038 kumaneko 1064 head);
1039     perm = acl2->perm;
1040     if (perm & (1 << TYPE_LINK_ACL))
1041     count++;
1042     if (perm & (1 << TYPE_RENAME_ACL))
1043     count++;
1044     break;
1045     case TYPE_EXECUTE_HANDLER:
1046     case TYPE_DENIED_EXECUTE_HANDLER:
1047     break;
1048     default:
1049 kumaneko 1052 count++;
1050 kumaneko 1064 }
1051 kumaneko 512 }
1052 kumaneko 1657 if (count < ccs_check_flags(domain, CCS_TOMOYO_MAX_ACCEPT_ENTRY))
1053 kumaneko 1052 return true;
1054 kumaneko 512 if (!domain->quota_warned) {
1055 kumaneko 1006 domain->quota_warned = true;
1056 kumaneko 1052 printk(KERN_WARNING "TOMOYO-WARNING: "
1057     "Domain '%s' has so many ACLs to hold. "
1058     "Stopped learning mode.\n", domain->domainname->name);
1059 kumaneko 512 }
1060 kumaneko 1006 return false;
1061 kumaneko 512 }
1062    
1063 kumaneko 1052 /**
1064     * ccs_find_or_assign_new_profile - Create a new profile.
1065     *
1066     * @profile: Profile number to create.
1067     *
1068 kumaneko 2002 * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
1069 kumaneko 1052 */
1070 kumaneko 2002 static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int
1071     profile)
1072 kumaneko 111 {
1073 kumaneko 1697 static DEFINE_MUTEX(lock);
1074 kumaneko 2002 struct ccs_profile *ptr = NULL;
1075 kumaneko 1697 mutex_lock(&lock);
1076 kumaneko 1052 if (profile < MAX_PROFILES) {
1077 kumaneko 2002 ptr = ccs_profile_ptr[profile];
1078 kumaneko 1052 if (ptr)
1079     goto ok;
1080     ptr = ccs_alloc_element(sizeof(*ptr));
1081     if (ptr) {
1082 kumaneko 111 int i;
1083 kumaneko 1052 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)
1084     ptr->value[i]
1085     = ccs_control_array[i].current_value;
1086     /*
1087     * Needn't to initialize "ptr->capability_value"
1088     * because they are always 0.
1089     */
1090 kumaneko 708 mb(); /* Avoid out-of-order execution. */
1091 kumaneko 2002 ccs_profile_ptr[profile] = ptr;
1092 kumaneko 111 }
1093     }
1094 kumaneko 1052 ok:
1095 kumaneko 1697 mutex_unlock(&lock);
1096 kumaneko 111 return ptr;
1097     }
1098    
1099 kumaneko 1052 /**
1100 kumaneko 2002 * ccs_write_profile - Write profile table.
1101 kumaneko 1052 *
1102 kumaneko 1657 * @head: Pointer to "struct ccs_io_buffer".
1103 kumaneko 1052 *
1104     * Returns 0 on success, negative value otherwise.
1105     */
1106 kumaneko 2002 static int ccs_write_profile(struct ccs_io_buffer *head)
1107 kumaneko 111 {
1108     char *data = head->write_buf;
1109 kumaneko 1064 unsigned int i;
1110     unsigned int value;
1111 kumaneko 111 char *cp;
1112 kumaneko 2002 struct ccs_profile *ccs_profile;
1113 kumaneko 111 i = simple_strtoul(data, &cp, 10);
1114     if (data != cp) {
1115 kumaneko 1052 if (*cp != '-')
1116     return -EINVAL;
1117     data = cp + 1;
1118 kumaneko 111 }
1119 kumaneko 2002 ccs_profile = ccs_find_or_assign_new_profile(i);
1120     if (!ccs_profile)
1121 kumaneko 1052 return -EINVAL;
1122 kumaneko 111 cp = strchr(data, '=');
1123 kumaneko 1052 if (!cp)
1124     return -EINVAL;
1125 kumaneko 111 *cp = '\0';
1126 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_PROFILE);
1127     if (!strcmp(data, "COMMENT")) {
1128 kumaneko 2002 ccs_profile->comment = ccs_save_name(cp + 1);
1129 kumaneko 111 return 0;
1130     }
1131 kumaneko 708 #ifdef CONFIG_TOMOYO
1132 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_MAC_FOR_CAPABILITY)) {
1133 kumaneko 1014 if (sscanf(cp + 1, "%u", &value) != 1) {
1134 kumaneko 1015 for (i = 0; i < 4; i++) {
1135 kumaneko 2002 if (strcmp(cp + 1, ccs_mode_4[i]))
1136 kumaneko 1052 continue;
1137 kumaneko 1015 value = i;
1138 kumaneko 1014 break;
1139     }
1140 kumaneko 1052 if (i == 4)
1141     return -EINVAL;
1142 kumaneko 1014 }
1143 kumaneko 1052 if (value > 3)
1144     value = 3;
1145 kumaneko 1015 for (i = 0; i < TOMOYO_MAX_CAPABILITY_INDEX; i++) {
1146 kumaneko 2002 if (strcmp(data, ccs_capability_control_keyword[i]))
1147 kumaneko 1052 continue;
1148 kumaneko 2002 ccs_profile->capability_value[i] = value;
1149 kumaneko 1015 return 0;
1150     }
1151     return -EINVAL;
1152 kumaneko 111 }
1153 kumaneko 461 #endif
1154 kumaneko 111 for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {
1155 kumaneko 1052 if (strcmp(data, ccs_control_array[i].keyword))
1156     continue;
1157 kumaneko 1014 if (sscanf(cp + 1, "%u", &value) != 1) {
1158     int j;
1159     const char **modes;
1160     switch (i) {
1161     case CCS_SAKURA_RESTRICT_AUTOBIND:
1162     case CCS_TOMOYO_VERBOSE:
1163 kumaneko 2002 modes = ccs_mode_2;
1164 kumaneko 1014 break;
1165     default:
1166 kumaneko 2002 modes = ccs_mode_4;
1167 kumaneko 1014 break;
1168     }
1169     for (j = 0; j < 4; j++) {
1170 kumaneko 1052 if (strcmp(cp + 1, modes[j]))
1171     continue;
1172 kumaneko 1014 value = j;
1173     break;
1174     }
1175 kumaneko 1052 if (j == 4)
1176     return -EINVAL;
1177 kumaneko 1014 } else if (value > ccs_control_array[i].max_value) {
1178     value = ccs_control_array[i].max_value;
1179     }
1180     switch (i) {
1181     case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1182     case CCS_SAKURA_RESTRICT_UNMOUNT:
1183 kumaneko 1052 if (value == 1)
1184     value = 2; /* learning mode is not supported. */
1185 kumaneko 1014 }
1186 kumaneko 2002 ccs_profile->value[i] = value;
1187 kumaneko 111 return 0;
1188     }
1189     return -EINVAL;
1190     }
1191    
1192 kumaneko 1052 /**
1193 kumaneko 2002 * ccs_read_profile - Read profile table.
1194 kumaneko 1052 *
1195 kumaneko 1657 * @head: Pointer to "struct ccs_io_buffer".
1196 kumaneko 1052 *
1197     * Returns 0.
1198     */
1199 kumaneko 2002 static int ccs_read_profile(struct ccs_io_buffer *head)
1200 kumaneko 111 {
1201 kumaneko 2002 static const int ccs_total
1202 kumaneko 1052 = CCS_MAX_CONTROL_INDEX + TOMOYO_MAX_CAPABILITY_INDEX + 1;
1203 kumaneko 1006 int step;
1204 kumaneko 1052 if (head->read_eof)
1205     return 0;
1206 kumaneko 2002 for (step = head->read_step; step < MAX_PROFILES * ccs_total; step++) {
1207     const u8 index = step / ccs_total;
1208     u8 type = step % ccs_total;
1209     const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];
1210 kumaneko 1006 head->read_step = step;
1211 kumaneko 2002 if (!ccs_profile)
1212 kumaneko 1052 continue;
1213     #if !defined(CONFIG_SAKURA) || !defined(CONFIG_TOMOYO)
1214     switch (type) {
1215 kumaneko 461 #ifndef CONFIG_SAKURA
1216 kumaneko 1006 case CCS_SAKURA_DENY_CONCEAL_MOUNT:
1217     case CCS_SAKURA_RESTRICT_CHROOT:
1218     case CCS_SAKURA_RESTRICT_MOUNT:
1219     case CCS_SAKURA_RESTRICT_UNMOUNT:
1220     case CCS_SAKURA_RESTRICT_PIVOT_ROOT:
1221     case CCS_SAKURA_RESTRICT_AUTOBIND:
1222 kumaneko 461 #endif
1223 kumaneko 111 #ifndef CONFIG_TOMOYO
1224 kumaneko 1006 case CCS_TOMOYO_MAC_FOR_FILE:
1225     case CCS_TOMOYO_MAC_FOR_ARGV0:
1226     case CCS_TOMOYO_MAC_FOR_ENV:
1227     case CCS_TOMOYO_MAC_FOR_NETWORK:
1228     case CCS_TOMOYO_MAC_FOR_SIGNAL:
1229     case CCS_TOMOYO_MAX_ACCEPT_ENTRY:
1230     case CCS_TOMOYO_MAX_GRANT_LOG:
1231     case CCS_TOMOYO_MAX_REJECT_LOG:
1232     case CCS_TOMOYO_VERBOSE:
1233 kumaneko 111 #endif
1234 kumaneko 1006 continue;
1235 kumaneko 111 }
1236 kumaneko 1052 #endif
1237     if (!type) { /* Print profile' comment tag. */
1238     if (!ccs_io_printf(head, "%u-COMMENT=%s\n",
1239 kumaneko 2002 index, ccs_profile->comment ?
1240     ccs_profile->comment->name : ""))
1241 kumaneko 1052 break;
1242     continue;
1243     }
1244     type--;
1245     if (type >= CCS_MAX_CONTROL_INDEX) {
1246 kumaneko 1015 #ifdef CONFIG_TOMOYO
1247 kumaneko 1052 const int i = type - CCS_MAX_CONTROL_INDEX;
1248 kumaneko 2002 const u8 value = ccs_profile->capability_value[i];
1249 kumaneko 1052 if (!ccs_io_printf(head,
1250     "%u-" KEYWORD_MAC_FOR_CAPABILITY
1251     "%s=%s\n", index,
1252 kumaneko 2002 ccs_capability_control_keyword[i],
1253     ccs_mode_4[value]))
1254 kumaneko 1052 break;
1255 kumaneko 1015 #endif
1256 kumaneko 1006 } else {
1257 kumaneko 2002 const unsigned int value = ccs_profile->value[type];
1258 kumaneko 1014 const char **modes = NULL;
1259 kumaneko 1052 const char *keyword = ccs_control_array[type].keyword;
1260     switch (ccs_control_array[type].max_value) {
1261 kumaneko 1014 case 3:
1262 kumaneko 2002 modes = ccs_mode_4;
1263 kumaneko 1014 break;
1264     case 1:
1265 kumaneko 2002 modes = ccs_mode_2;
1266 kumaneko 1014 break;
1267     }
1268     if (modes) {
1269 kumaneko 1052 if (!ccs_io_printf(head, "%u-%s=%s\n", index,
1270     keyword, modes[value]))
1271     break;
1272 kumaneko 1014 } else {
1273 kumaneko 1052 if (!ccs_io_printf(head, "%u-%s=%u\n", index,
1274     keyword, value))
1275     break;
1276 kumaneko 1014 }
1277 kumaneko 1006 }
1278     }
1279 kumaneko 2002 if (step == MAX_PROFILES * ccs_total)
1280 kumaneko 1052 head->read_eof = true;
1281 kumaneko 111 return 0;
1282     }
1283    
1284 kumaneko 1052 /* Structure for policy manager. */
1285 kumaneko 2002 struct ccs_policy_manager_entry {
1286 kumaneko 722 struct list1_head list;
1287 kumaneko 1052 /* A path to program or a domainname. */
1288 kumaneko 2002 const struct ccs_path_info *manager;
1289 kumaneko 1052 bool is_domain; /* True if manager is a domainname. */
1290     bool is_deleted; /* True if this entry is deleted. */
1291 kumaneko 214 };
1292 kumaneko 111
1293 kumaneko 2002 /* The list for "struct ccs_policy_manager_entry". */
1294     static LIST1_HEAD(ccs_policy_manager_list);
1295 kumaneko 111
1296 kumaneko 1052 /**
1297 kumaneko 2002 * ccs_update_manager_entry - Add a manager entry.
1298 kumaneko 1052 *
1299     * @manager: The path to manager or the domainnamme.
1300     * @is_delete: True if it is a delete request.
1301     *
1302     * Returns 0 on success, negative value otherwise.
1303     */
1304 kumaneko 2002 static int ccs_update_manager_entry(const char *manager, const bool is_delete)
1305 kumaneko 111 {
1306 kumaneko 2002 struct ccs_policy_manager_entry *new_entry;
1307     struct ccs_policy_manager_entry *ptr;
1308 kumaneko 652 static DEFINE_MUTEX(lock);
1309 kumaneko 2002 const struct ccs_path_info *saved_manager;
1310 kumaneko 111 int error = -ENOMEM;
1311 kumaneko 1006 bool is_domain = false;
1312 kumaneko 1052 if (ccs_is_domain_def(manager)) {
1313     if (!ccs_is_correct_domain(manager, __func__))
1314     return -EINVAL;
1315 kumaneko 1006 is_domain = true;
1316 kumaneko 111 } else {
1317 kumaneko 1052 if (!ccs_is_correct_path(manager, 1, -1, -1, __func__))
1318     return -EINVAL;
1319 kumaneko 111 }
1320 kumaneko 1052 saved_manager = ccs_save_name(manager);
1321     if (!saved_manager)
1322     return -ENOMEM;
1323 kumaneko 652 mutex_lock(&lock);
1324 kumaneko 2002 list1_for_each_entry(ptr, &ccs_policy_manager_list, list) {
1325 kumaneko 1064 if (ptr->manager != saved_manager)
1326     continue;
1327     ptr->is_deleted = is_delete;
1328     error = 0;
1329     goto out;
1330 kumaneko 111 }
1331     if (is_delete) {
1332     error = -ENOENT;
1333     goto out;
1334     }
1335 kumaneko 1052 new_entry = ccs_alloc_element(sizeof(*new_entry));
1336     if (!new_entry)
1337     goto out;
1338 kumaneko 111 new_entry->manager = saved_manager;
1339     new_entry->is_domain = is_domain;
1340 kumaneko 2002 list1_add_tail_mb(&new_entry->list, &ccs_policy_manager_list);
1341 kumaneko 111 error = 0;
1342     out:
1343 kumaneko 652 mutex_unlock(&lock);
1344 kumaneko 1052 if (!error)
1345     ccs_update_counter(CCS_UPDATES_COUNTER_MANAGER);
1346 kumaneko 111 return error;
1347     }
1348    
1349 kumaneko 1052 /**
1350 kumaneko 2002 * ccs_write_manager_policy - Write manager policy.
1351 kumaneko 1052 *
1352 kumaneko 1657 * @head: Pointer to "struct ccs_io_buffer".
1353 kumaneko 1052 *
1354     * Returns 0 on success, negative value otherwise.
1355     */
1356 kumaneko 2002 static int ccs_write_manager_policy(struct ccs_io_buffer *head)
1357 kumaneko 111 {
1358 kumaneko 1052 char *data = head->write_buf;
1359 kumaneko 2002 bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);
1360 kumaneko 1064 if (!strcmp(data, "manage_by_non_root")) {
1361 kumaneko 2002 ccs_manage_by_non_root = !is_delete;
1362 kumaneko 1006 return 0;
1363     }
1364 kumaneko 2002 return ccs_update_manager_entry(data, is_delete);
1365 kumaneko 111 }
1366    
1367 kumaneko 1052 /**
1368 kumaneko 2002 * ccs_read_manager_policy - Read manager policy.
1369 kumaneko 1052 *
1370 kumaneko 1657 * @head: Pointer to "struct ccs_io_buffer".
1371 kumaneko 1052 *
1372     * Returns 0.
1373     */
1374 kumaneko 2002 static int ccs_read_manager_policy(struct ccs_io_buffer *head)
1375 kumaneko 111 {
1376 kumaneko 722 struct list1_head *pos;
1377 kumaneko 1052 if (head->read_eof)
1378     return 0;
1379 kumaneko 2002 list1_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {
1380     struct ccs_policy_manager_entry *ptr;
1381     ptr = list1_entry(pos, struct ccs_policy_manager_entry, list);
1382 kumaneko 1052 if (ptr->is_deleted)
1383     continue;
1384     if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
1385     return 0;
1386 kumaneko 111 }
1387 kumaneko 1006 head->read_eof = true;
1388 kumaneko 111 return 0;
1389     }
1390    
1391 kumaneko 1052 /**
1392 kumaneko 2002 * ccs_is_policy_manager - Check whether the current process is a policy manager.
1393 kumaneko 1052 *
1394     * Returns true if the current process is permitted to modify policy
1395     * via /proc/ccs/ interface.
1396     */
1397 kumaneko 2002 static bool ccs_is_policy_manager(void)
1398 kumaneko 111 {
1399 kumaneko 2002 struct ccs_policy_manager_entry *ptr;
1400 kumaneko 111 const char *exe;
1401 kumaneko 1578 struct task_struct *task = current;
1402 kumaneko 2002 const struct ccs_path_info *domainname = task->domain_info->domainname;
1403 kumaneko 1006 bool found = false;
1404 kumaneko 2002 if (!ccs_sbin_init_started)
1405 kumaneko 1052 return true;
1406 kumaneko 1578 if (task->tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER)
1407     return true;
1408 kumaneko 2016 if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
1409 kumaneko 1052 return false;
1410 kumaneko 2002 list1_for_each_entry(ptr, &ccs_policy_manager_list, list) {
1411 kumaneko 1052 if (!ptr->is_deleted && ptr->is_domain
1412 kumaneko 1578 && !ccs_pathcmp(domainname, ptr->manager)) {
1413     /* Set manager flag. */
1414     task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER;
1415 kumaneko 1052 return true;
1416 kumaneko 1578 }
1417 kumaneko 111 }
1418 kumaneko 1052 exe = ccs_get_exe();
1419     if (!exe)
1420     return false;
1421 kumaneko 2002 list1_for_each_entry(ptr, &ccs_policy_manager_list, list) {
1422 kumaneko 1052 if (!ptr->is_deleted && !ptr->is_domain
1423     && !strcmp(exe, ptr->manager->name)) {
1424 kumaneko 1006 found = true;
1425 kumaneko 1578 /* Set manager flag. */
1426     task->tomoyo_flags |= CCS_TASK_IS_POLICY_MANAGER;
1427 kumaneko 708 break;
1428     }
1429 kumaneko 111 }
1430 kumaneko 708 if (!found) { /* Reduce error messages. */
1431 kumaneko 2002 static pid_t ccs_last_pid;
1432 kumaneko 111 const pid_t pid = current->pid;
1433 kumaneko 2002 if (ccs_last_pid != pid) {
1434 kumaneko 1052 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1435     "update policies.\n", domainname->name, exe);
1436 kumaneko 2002 ccs_last_pid = pid;
1437 kumaneko 111 }
1438     }
1439     ccs_free(exe);
1440 kumaneko 708 return found;
1441 kumaneko 111 }
1442    
1443     #ifdef CONFIG_TOMOYO
1444    
1445 kumaneko 1052 /**
1446     * ccs_find_condition_part - Find condition part from the statement.
1447     *
1448     * @data: String to parse.
1449     *
1450     * Returns pointer to the condition part if it was found in the statement,
1451     * NULL otherwise.
1452     */
1453     static char *ccs_find_condition_part(char *data)
1454 kumaneko 581 {
1455 kumaneko 1064 char *cp = strstr(data, " if ");
1456 kumaneko 581 if (cp) {
1457 kumaneko 1747 while (1) {
1458     char *cp2 = strstr(cp + 3, " if ");
1459     if (!cp2)
1460     break;
1461 kumaneko 1064 cp = cp2;
1462 kumaneko 1747 }
1463 kumaneko 581 *cp++ = '\0';
1464 kumaneko 1052 } else {
1465     cp = strstr(data, " ; set ");
1466     if (cp)
1467     *cp++ = '\0';
1468 kumaneko 581 }
1469     return cp;
1470     }
1471    
1472 kumaneko 1052 /**
1473 kumaneko 2002 * ccs_is_select_one - Parse select command.
1474 kumaneko 1609 *
1475     * @head: Pointer to "struct ccs_io_buffer".
1476     * @data: String to parse.
1477     *
1478     * Returns true on success, false otherwise.
1479     */
1480 kumaneko 2002 static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)
1481 kumaneko 1609 {
1482     unsigned int pid;
1483     struct domain_info *domain = NULL;
1484 kumaneko 1926 if (!strcmp(data, "allow_execute")) {
1485     head->read_execute_only = true;
1486     return true;
1487     }
1488 kumaneko 1609 if (sscanf(data, "pid=%u", &pid) == 1) {
1489     struct task_struct *p;
1490     /***** CRITICAL SECTION START *****/
1491     read_lock(&tasklist_lock);
1492     p = find_task_by_pid(pid);
1493     if (p)
1494     domain = p->domain_info;
1495     read_unlock(&tasklist_lock);
1496     /***** CRITICAL SECTION END *****/
1497     } else if (!strncmp(data, "domain=", 7)) {
1498     if (ccs_is_domain_def(data + 7))
1499     domain = ccs_find_domain(data + 7);
1500     } else
1501     return false;
1502 kumaneko 1809 head->write_var1 = domain;
1503 kumaneko 1810 /* Accessing read_buf is safe because head->io_sem is held. */
1504 kumaneko 1809 if (!head->read_buf)
1505     return true; /* Do nothing if open(O_WRONLY). */
1506 kumaneko 1609 head->read_avail = 0;
1507     ccs_io_printf(head, "# select %s\n", data);
1508     head->read_single_domain = true;
1509     head->read_eof = !domain;
1510     if (domain) {
1511     struct domain_info *d;
1512     head->read_var1 = NULL;
1513 kumaneko 2002 list1_for_each_entry(d, &ccs_domain_list, list) {
1514 kumaneko 1609 if (d == domain)
1515     break;
1516     head->read_var1 = &d->list;
1517     }
1518     head->read_var2 = NULL;
1519     head->read_bit = 0;
1520     head->read_step = 0;
1521     if (domain->is_deleted)
1522     ccs_io_printf(head, "# This is a deleted domain.\n");
1523     }
1524     return true;
1525     }
1526    
1527     /**
1528 kumaneko 2002 * ccs_write_domain_policy - Write domain policy.
1529 kumaneko 1052 *
1530     * @head: Pointer to "struct ccs_io_buffer".
1531     *
1532     * Returns 0 on success, negative value otherwise.
1533     */
1534 kumaneko 2002 static int ccs_write_domain_policy(struct ccs_io_buffer *head)
1535 kumaneko 111 {
1536     char *data = head->write_buf;
1537     struct domain_info *domain = head->write_var1;
1538 kumaneko 1064 bool is_delete = false;
1539     bool is_select = false;
1540     bool is_undelete = false;
1541 kumaneko 111 unsigned int profile;
1542 kumaneko 2002 const struct ccs_condition_list *cond = NULL;
1543 kumaneko 906 char *cp;
1544 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_DELETE))
1545 kumaneko 1006 is_delete = true;
1546 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_SELECT))
1547 kumaneko 1006 is_select = true;
1548 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_UNDELETE))
1549 kumaneko 1006 is_undelete = true;
1550 kumaneko 2002 if (is_select && ccs_is_select_one(head, data))
1551 kumaneko 1609 return 0;
1552 kumaneko 1606 /* Don't allow updating policies by non manager programs. */
1553 kumaneko 2002 if (!ccs_is_policy_manager())
1554 kumaneko 1606 return -EPERM;
1555 kumaneko 1052 if (ccs_is_domain_def(data)) {
1556 kumaneko 1064 domain = NULL;
1557     if (is_delete)
1558 kumaneko 1052 ccs_delete_domain(data);
1559 kumaneko 1064 else if (is_select)
1560 kumaneko 1052 domain = ccs_find_domain(data);
1561 kumaneko 1064 else if (is_undelete)
1562 kumaneko 1052 domain = ccs_undelete_domain(data);
1563 kumaneko 1064 else
1564 kumaneko 1052 domain = ccs_find_or_assign_new_domain(data, 0);
1565 kumaneko 111 head->write_var1 = domain;
1566 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1567 kumaneko 111 return 0;
1568     }
1569 kumaneko 1052 if (!domain)
1570     return -EINVAL;
1571 kumaneko 581
1572 kumaneko 1052 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1573     && profile < MAX_PROFILES) {
1574 kumaneko 2002 if (ccs_profile_ptr[profile] || !ccs_sbin_init_started)
1575 kumaneko 1052 domain->profile = (u8) profile;
1576 kumaneko 581 return 0;
1577     }
1578 kumaneko 1052 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1579     ccs_set_domain_flag(domain, is_delete,
1580     DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1581 kumaneko 1007 return 0;
1582     }
1583 kumaneko 1052 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1584     ccs_set_domain_flag(domain, is_delete,
1585     DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV);
1586 kumaneko 1007 return 0;
1587     }
1588 kumaneko 1052 cp = ccs_find_condition_part(data);
1589     if (cp) {
1590     cond = ccs_find_or_assign_new_condition(cp);
1591     if (!cond)
1592     return -EINVAL;
1593 kumaneko 111 }
1594 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1595 kumaneko 1052 return ccs_write_capability_policy(data, domain, cond,
1596     is_delete);
1597 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_NETWORK))
1598 kumaneko 1052 return ccs_write_network_policy(data, domain, cond, is_delete);
1599 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1600 kumaneko 1052 return ccs_write_signal_policy(data, domain, cond, is_delete);
1601 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_ARGV0))
1602 kumaneko 1052 return ccs_write_argv0_policy(data, domain, cond, is_delete);
1603 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))
1604 kumaneko 1052 return ccs_write_env_policy(data, domain, cond, is_delete);
1605     else
1606     return ccs_write_file_policy(data, domain, cond, is_delete);
1607 kumaneko 111 }
1608    
1609 kumaneko 1052 /**
1610 kumaneko 2002 * ccs_print_single_path_acl - Print a single path ACL entry.
1611 kumaneko 1052 *
1612     * @head: Pointer to "struct ccs_io_buffer".
1613 kumaneko 2002 * @ptr: Pointer to "struct ccs_single_path_acl_record".
1614     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1615 kumaneko 1052 *
1616     * Returns true on success, false otherwise.
1617     */
1618 kumaneko 2002 static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,
1619     struct ccs_single_path_acl_record *ptr,
1620     const struct ccs_condition_list *cond)
1621 kumaneko 856 {
1622     int pos;
1623     u8 bit;
1624 kumaneko 1052 const char *atmark = "";
1625     const char *filename;
1626 kumaneko 856 const u16 perm = ptr->perm;
1627 kumaneko 1052 if (ptr->u_is_group) {
1628     atmark = "@";
1629     filename = ptr->u.group->group_name->name;
1630     } else {
1631     filename = ptr->u.filename->name;
1632     }
1633 kumaneko 856 for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1634     const char *msg;
1635 kumaneko 1052 if (!(perm & (1 << bit)))
1636     continue;
1637 kumaneko 1926 if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)
1638     continue;
1639 kumaneko 856 /* Print "read/write" instead of "read" and "write". */
1640 kumaneko 1052 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1641     && (perm & (1 << TYPE_READ_WRITE_ACL)))
1642     continue;
1643     msg = ccs_sp2keyword(bit);
1644 kumaneko 856 pos = head->read_avail;
1645 kumaneko 1052 if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1646     atmark, filename) ||
1647 kumaneko 1054 !ccs_print_condition(head, cond))
1648 kumaneko 1052 goto out;
1649 kumaneko 856 }
1650     head->read_bit = 0;
1651 kumaneko 1006 return true;
1652 kumaneko 856 out:
1653     head->read_bit = bit;
1654     head->read_avail = pos;
1655 kumaneko 1006 return false;
1656 kumaneko 856 }
1657    
1658 kumaneko 1052 /**
1659 kumaneko 2002 * ccs_print_double_path_acl - Print a double path ACL entry.
1660 kumaneko 1052 *
1661     * @head: Pointer to "struct ccs_io_buffer".
1662 kumaneko 2002 * @ptr: Pointer to "struct ccs_double_path_acl_record".
1663     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1664 kumaneko 1052 *
1665     * Returns true on success, false otherwise.
1666     */
1667 kumaneko 2002 static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,
1668     struct ccs_double_path_acl_record *ptr,
1669     const struct ccs_condition_list *cond)
1670 kumaneko 856 {
1671     int pos;
1672 kumaneko 1052 const char *atmark1 = "";
1673     const char *atmark2 = "";
1674     const char *filename1;
1675     const char *filename2;
1676 kumaneko 856 const u8 perm = ptr->perm;
1677     u8 bit;
1678 kumaneko 1052 if (ptr->u1_is_group) {
1679     atmark1 = "@";
1680     filename1 = ptr->u1.group1->group_name->name;
1681     } else {
1682     filename1 = ptr->u1.filename1->name;
1683     }
1684     if (ptr->u2_is_group) {
1685     atmark2 = "@";
1686     filename2 = ptr->u2.group2->group_name->name;
1687     } else {
1688     filename2 = ptr->u2.filename2->name;
1689     }
1690 kumaneko 856 for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1691     const char *msg;
1692 kumaneko 1052 if (!(perm & (1 << bit)))
1693     continue;
1694     msg = ccs_dp2keyword(bit);
1695 kumaneko 856 pos = head->read_avail;
1696 kumaneko 1052 if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1697 kumaneko 1064 atmark1, filename1, atmark2, filename2) ||
1698 kumaneko 1054 !ccs_print_condition(head, cond))
1699 kumaneko 1052 goto out;
1700 kumaneko 856 }
1701 kumaneko 1032 head->read_bit = 0;
1702 kumaneko 1006 return true;
1703 kumaneko 856 out:
1704     head->read_bit = bit;
1705     head->read_avail = pos;
1706 kumaneko 1006 return false;
1707 kumaneko 856 }
1708    
1709 kumaneko 1052 /**
1710 kumaneko 2002 * ccs_print_argv0_acl - Print an argv[0] ACL entry.
1711 kumaneko 1052 *
1712     * @head: Pointer to "struct ccs_io_buffer".
1713 kumaneko 2002 * @ptr: Pointer to "struct ccs_argv0_acl_record".
1714     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1715 kumaneko 1052 *
1716     * Returns true on success, false otherwise.
1717     */
1718 kumaneko 2002 static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,
1719     struct ccs_argv0_acl_record *ptr,
1720     const struct ccs_condition_list *cond)
1721 kumaneko 856 {
1722     int pos = head->read_avail;
1723 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1724     ptr->filename->name, ptr->argv0->name))
1725     goto out;
1726 kumaneko 1054 if (!ccs_print_condition(head, cond))
1727 kumaneko 1052 goto out;
1728 kumaneko 1006 return true;
1729 kumaneko 856 out:
1730     head->read_avail = pos;
1731 kumaneko 1006 return false;
1732 kumaneko 856 }
1733    
1734 kumaneko 1052 /**
1735 kumaneko 2002 * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1736 kumaneko 1052 *
1737     * @head: Pointer to "struct ccs_io_buffer".
1738 kumaneko 2002 * @ptr: Pointer to "struct ccs_env_acl_record".
1739     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1740 kumaneko 1052 *
1741     * Returns true on success, false otherwise.
1742     */
1743 kumaneko 2002 static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1744     struct ccs_env_acl_record *ptr,
1745     const struct ccs_condition_list *cond)
1746 kumaneko 856 {
1747     int pos = head->read_avail;
1748 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1749     goto out;
1750 kumaneko 1054 if (!ccs_print_condition(head, cond))
1751 kumaneko 1052 goto out;
1752 kumaneko 1006 return true;
1753 kumaneko 856 out:
1754     head->read_avail = pos;
1755 kumaneko 1006 return false;
1756 kumaneko 856 }
1757    
1758 kumaneko 1052 /**
1759 kumaneko 2002 * ccs_print_capability_acl - Print a capability ACL entry.
1760 kumaneko 1052 *
1761     * @head: Pointer to "struct ccs_io_buffer".
1762 kumaneko 2002 * @ptr: Pointer to "struct ccs_capability_acl_record".
1763     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1764 kumaneko 1052 *
1765     * Returns true on success, false otherwise.
1766     */
1767 kumaneko 2002 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1768     struct ccs_capability_acl_record *ptr,
1769     const struct ccs_condition_list *cond)
1770 kumaneko 856 {
1771 kumaneko 860 int pos = head->read_avail;
1772 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1773     ccs_cap2keyword(ptr->operation)))
1774     goto out;
1775 kumaneko 1054 if (!ccs_print_condition(head, cond))
1776 kumaneko 1052 goto out;
1777 kumaneko 1006 return true;
1778 kumaneko 856 out:
1779     head->read_avail = pos;
1780 kumaneko 1006 return false;
1781 kumaneko 856 }
1782    
1783 kumaneko 1052 /**
1784 kumaneko 2002 * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.
1785 kumaneko 1052 *
1786     * @head: Pointer to "struct ccs_io_buffer".
1787 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1788 kumaneko 1052 *
1789     * Returns true on success, false otherwise.
1790     */
1791 kumaneko 2002 static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,
1792     struct ccs_ip_network_acl_record *ptr)
1793 kumaneko 856 {
1794 kumaneko 1052 const u32 min_address = ptr->u.ipv4.min;
1795     const u32 max_address = ptr->u.ipv4.max;
1796     if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1797     return false;
1798     if (min_address != max_address
1799     && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1800     return false;
1801     return true;
1802     }
1803    
1804     /**
1805 kumaneko 2002 * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.
1806 kumaneko 1052 *
1807     * @head: Pointer to "struct ccs_io_buffer".
1808 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1809 kumaneko 1052 *
1810     * Returns true on success, false otherwise.
1811     */
1812 kumaneko 2002 static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,
1813     struct ccs_ip_network_acl_record *ptr)
1814 kumaneko 1052 {
1815     char buf[64];
1816     const struct in6_addr *min_address = ptr->u.ipv6.min;
1817     const struct in6_addr *max_address = ptr->u.ipv6.max;
1818     ccs_print_ipv6(buf, sizeof(buf), min_address);
1819     if (!ccs_io_printf(head, "%s", buf))
1820     return false;
1821     if (min_address != max_address) {
1822     ccs_print_ipv6(buf, sizeof(buf), max_address);
1823     if (!ccs_io_printf(head, "-%s", buf))
1824     return false;
1825     }
1826     return true;
1827     }
1828    
1829     /**
1830 kumaneko 2002 * ccs_print_port_entry - Print port number of a network ACL entry.
1831 kumaneko 1052 *
1832     * @head: Pointer to "struct ccs_io_buffer".
1833 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1834 kumaneko 1052 *
1835     * Returns true on success, false otherwise.
1836     */
1837 kumaneko 2002 static bool ccs_print_port_entry(struct ccs_io_buffer *head,
1838     struct ccs_ip_network_acl_record *ptr)
1839 kumaneko 1052 {
1840     const u16 min_port = ptr->min_port, max_port = ptr->max_port;
1841     if (!ccs_io_printf(head, " %u", min_port))
1842     return false;
1843     if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1844     return false;
1845     return true;
1846     }
1847    
1848     /**
1849 kumaneko 2002 * ccs_print_network_acl - Print a network ACL entry.
1850 kumaneko 1052 *
1851     * @head: Pointer to "struct ccs_io_buffer".
1852 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1853     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1854 kumaneko 1052 *
1855     * Returns true on success, false otherwise.
1856     */
1857 kumaneko 2002 static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1858     struct ccs_ip_network_acl_record *ptr,
1859     const struct ccs_condition_list *cond)
1860 kumaneko 1052 {
1861 kumaneko 856 int pos = head->read_avail;
1862 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1863     ccs_net2keyword(ptr->operation_type)))
1864     goto out;
1865 kumaneko 856 switch (ptr->record_type) {
1866     case IP_RECORD_TYPE_ADDRESS_GROUP:
1867 kumaneko 1052 if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1868     goto out;
1869 kumaneko 856 break;
1870     case IP_RECORD_TYPE_IPv4:
1871 kumaneko 2002 if (!ccs_print_ipv4_entry(head, ptr))
1872 kumaneko 1052 goto out;
1873 kumaneko 856 break;
1874     case IP_RECORD_TYPE_IPv6:
1875 kumaneko 2002 if (!ccs_print_ipv6_entry(head, ptr))
1876 kumaneko 1052 goto out;
1877 kumaneko 856 break;
1878     }
1879 kumaneko 2002 if (!ccs_print_port_entry(head, ptr))
1880 kumaneko 1052 goto out;
1881 kumaneko 1054 if (!ccs_print_condition(head, cond))
1882 kumaneko 1052 goto out;
1883 kumaneko 1006 return true;
1884 kumaneko 856 out:
1885     head->read_avail = pos;
1886 kumaneko 1006 return false;
1887 kumaneko 856 }
1888    
1889 kumaneko 1052 /**
1890 kumaneko 2002 * ccs_print_signal_acl - Print a signal ACL entry.
1891 kumaneko 1052 *
1892     * @head: Pointer to "struct ccs_io_buffer".
1893     * @ptr: Pointer to "struct signale_acl_record".
1894 kumaneko 2002 * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1895 kumaneko 1052 *
1896     * Returns true on success, false otherwise.
1897     */
1898 kumaneko 2002 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1899     struct ccs_signal_acl_record *ptr,
1900     const struct ccs_condition_list *cond)
1901 kumaneko 856 {
1902     int pos = head->read_avail;
1903 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
1904     ptr->sig, ptr->domainname->name))
1905     goto out;
1906 kumaneko 1054 if (!ccs_print_condition(head, cond))
1907 kumaneko 1052 goto out;
1908 kumaneko 1006 return true;
1909 kumaneko 856 out:
1910     head->read_avail = pos;
1911 kumaneko 1006 return false;
1912 kumaneko 856 }
1913    
1914 kumaneko 1052 /**
1915 kumaneko 2002 * ccs_print_execute_handler_record - Print an execute handler ACL entry.
1916 kumaneko 1052 *
1917     * @head: Pointer to "struct ccs_io_buffer".
1918     * @keyword: Name of the keyword.
1919 kumaneko 2002 * @ptr: Pointer to "struct ccs_execute_handler_record".
1920 kumaneko 1052 *
1921     * Returns true on success, false otherwise.
1922     */
1923 kumaneko 2002 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,
1924     const char *keyword,
1925     struct ccs_execute_handler_record *
1926     ptr)
1927 kumaneko 1029 {
1928 kumaneko 1052 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1929 kumaneko 1029 }
1930    
1931 kumaneko 1052 /**
1932 kumaneko 2002 * ccs_print_entry - Print an ACL entry.
1933 kumaneko 1052 *
1934     * @head: Pointer to "struct ccs_io_buffer".
1935     * @ptr: Pointer to an ACL entry.
1936     *
1937     * Returns true on success, false otherwise.
1938     */
1939 kumaneko 2002 static bool ccs_print_entry(struct ccs_io_buffer *head,
1940     struct ccs_acl_info *ptr)
1941 kumaneko 111 {
1942 kumaneko 2002 const struct ccs_condition_list *cond = ccs_get_condition_part(ptr);
1943 kumaneko 1064 const u8 acl_type = ccs_acl_type2(ptr);
1944 kumaneko 1052 if (acl_type & ACL_DELETED)
1945     return true;
1946     if (acl_type == TYPE_SINGLE_PATH_ACL) {
1947 kumaneko 2002 struct ccs_single_path_acl_record *acl
1948     = container_of(ptr, struct ccs_single_path_acl_record,
1949 kumaneko 1052 head);
1950 kumaneko 2002 return ccs_print_single_path_acl(head, acl, cond);
1951 kumaneko 1052 }
1952 kumaneko 1926 if (acl_type == TYPE_EXECUTE_HANDLER) {
1953 kumaneko 2002 struct ccs_execute_handler_record *acl
1954     = container_of(ptr, struct ccs_execute_handler_record,
1955 kumaneko 1926 head);
1956     const char *keyword = KEYWORD_EXECUTE_HANDLER;
1957 kumaneko 2002 return ccs_print_execute_handler_record(head, keyword, acl);
1958 kumaneko 1926 }
1959     if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {
1960 kumaneko 2002 struct ccs_execute_handler_record *acl
1961     = container_of(ptr, struct ccs_execute_handler_record,
1962 kumaneko 1926 head);
1963     const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;
1964 kumaneko 2002 return ccs_print_execute_handler_record(head, keyword, acl);
1965 kumaneko 1926 }
1966     if (head->read_execute_only)
1967     return true;
1968 kumaneko 1052 if (acl_type == TYPE_DOUBLE_PATH_ACL) {
1969 kumaneko 2002 struct ccs_double_path_acl_record *acl
1970     = container_of(ptr, struct ccs_double_path_acl_record,
1971 kumaneko 1052 head);
1972 kumaneko 2002 return ccs_print_double_path_acl(head, acl, cond);
1973 kumaneko 1052 }
1974     if (acl_type == TYPE_ARGV0_ACL) {
1975 kumaneko 2002 struct ccs_argv0_acl_record *acl
1976     = container_of(ptr, struct ccs_argv0_acl_record, head);
1977     return ccs_print_argv0_acl(head, acl, cond);
1978 kumaneko 1052 }
1979     if (acl_type == TYPE_ENV_ACL) {
1980 kumaneko 2002 struct ccs_env_acl_record *acl
1981     = container_of(ptr, struct ccs_env_acl_record, head);
1982     return ccs_print_env_acl(head, acl, cond);
1983 kumaneko 1052 }
1984     if (acl_type == TYPE_CAPABILITY_ACL) {
1985 kumaneko 2002 struct ccs_capability_acl_record *acl
1986     = container_of(ptr, struct ccs_capability_acl_record,
1987     head);
1988     return ccs_print_capability_acl(head, acl, cond);
1989 kumaneko 1052 }
1990     if (acl_type == TYPE_IP_NETWORK_ACL) {
1991 kumaneko 2002 struct ccs_ip_network_acl_record *acl
1992     = container_of(ptr, struct ccs_ip_network_acl_record,
1993     head);
1994     return ccs_print_network_acl(head, acl, cond);
1995 kumaneko 1052 }
1996     if (acl_type == TYPE_SIGNAL_ACL) {
1997 kumaneko 2002 struct ccs_signal_acl_record *acl
1998     = container_of(ptr, struct ccs_signal_acl_record, head);
1999     return ccs_print_signal_acl(head, acl, cond);
2000 kumaneko 1052 }
2001 kumaneko 1120 /* Workaround for gcc 3.2.2's inline bug. */
2002     if (acl_type & ACL_DELETED)
2003     return true;
2004 kumaneko 1052 BUG(); /* This must not happen. */
2005     return false;
2006     }
2007    
2008     /**
2009 kumaneko 2002 * ccs_read_domain_policy - Read domain policy.
2010 kumaneko 1052 *
2011     * @head: Pointer to "struct ccs_io_buffer".
2012     *
2013     * Returns 0.
2014     */
2015 kumaneko 2002 static int ccs_read_domain_policy(struct ccs_io_buffer *head)
2016 kumaneko 1052 {
2017 kumaneko 722 struct list1_head *dpos;
2018     struct list1_head *apos;
2019 kumaneko 1052 if (head->read_eof)
2020     return 0;
2021     if (head->read_step == 0)
2022     head->read_step = 1;
2023 kumaneko 2002 list1_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
2024 kumaneko 708 struct domain_info *domain;
2025 kumaneko 1052 const char *quota_exceeded = "";
2026 kumaneko 1180 const char *transition_failed = "";
2027 kumaneko 1052 const char *ignore_global_allow_read = "";
2028     const char *ignore_global_allow_env = "";
2029 kumaneko 722 domain = list1_entry(dpos, struct domain_info, list);
2030 kumaneko 1052 if (head->read_step != 1)
2031     goto acl_loop;
2032 kumaneko 1609 if (domain->is_deleted && !head->read_single_domain)
2033     continue;
2034 kumaneko 1054 /* Print domainname and flags. */
2035 kumaneko 1052 if (domain->quota_warned)
2036     quota_exceeded = "quota_exceeded\n";
2037 kumaneko 1180 if (domain->flags & DOMAIN_FLAGS_TRANSITION_FAILED)
2038     transition_failed = "transition_failed\n";
2039 kumaneko 1052 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
2040     ignore_global_allow_read
2041     = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
2042     if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV)
2043     ignore_global_allow_env
2044     = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
2045 kumaneko 1609 if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
2046     "%s%s%s%s\n", domain->domainname->name,
2047     domain->profile, quota_exceeded,
2048     transition_failed,
2049 kumaneko 1052 ignore_global_allow_read,
2050     ignore_global_allow_env))
2051     return 0;
2052 kumaneko 708 head->read_step = 2;
2053 kumaneko 2002 acl_loop:
2054 kumaneko 1052 if (head->read_step == 3)
2055     goto tail_mark;
2056 kumaneko 1054 /* Print ACL entries in the domain. */
2057 kumaneko 1052 list1_for_each_cookie(apos, head->read_var2,
2058     &domain->acl_info_list) {
2059 kumaneko 2002 struct ccs_acl_info *ptr
2060     = list1_entry(apos, struct ccs_acl_info, list);
2061     if (!ccs_print_entry(head, ptr))
2062 kumaneko 1052 return 0;
2063 kumaneko 111 }
2064 kumaneko 708 head->read_step = 3;
2065 kumaneko 2002 tail_mark:
2066 kumaneko 1052 if (!ccs_io_printf(head, "\n"))
2067     return 0;
2068 kumaneko 708 head->read_step = 1;
2069 kumaneko 1606 if (head->read_single_domain)
2070     break;
2071 kumaneko 111 }
2072 kumaneko 1006 head->read_eof = true;
2073 kumaneko 111 return 0;
2074     }
2075    
2076 kumaneko 461 #endif
2077    
2078 kumaneko 1052 /**
2079 kumaneko 2002 * ccs_write_domain_profile - Assign profile for specified domain.
2080 kumaneko 1052 *
2081     * @head: Pointer to "struct ccs_io_buffer".
2082     *
2083     * Returns 0 on success, -EINVAL otherwise.
2084     *
2085     * This is equivalent to doing
2086 kumaneko 1054 *
2087     * ( echo "select " $domainname; echo "use_profile " $profile ) |
2088 kumaneko 1057 * /usr/lib/ccs/loadpolicy -d
2089 kumaneko 1052 */
2090 kumaneko 2002 static int ccs_write_domain_profile(struct ccs_io_buffer *head)
2091 kumaneko 461 {
2092     char *data = head->write_buf;
2093     char *cp = strchr(data, ' ');
2094     struct domain_info *domain;
2095     unsigned int profile;
2096 kumaneko 1052 if (!cp)
2097     return -EINVAL;
2098 kumaneko 461 *cp = '\0';
2099 kumaneko 1052 domain = ccs_find_domain(cp + 1);
2100 kumaneko 461 profile = simple_strtoul(data, NULL, 10);
2101 kumaneko 1052 if (domain && profile < MAX_PROFILES
2102 kumaneko 2002 && (ccs_profile_ptr[profile] || !ccs_sbin_init_started))
2103 kumaneko 1052 domain->profile = (u8) profile;
2104     ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
2105 kumaneko 461 return 0;
2106     }
2107    
2108 kumaneko 1052 /**
2109 kumaneko 2002 * ccs_read_domain_profile - Read only domainname and profile.
2110 kumaneko 1052 *
2111     * @head: Pointer to "struct ccs_io_buffer".
2112     *
2113     * Returns list of profile number and domainname pairs.
2114     *
2115     * This is equivalent to doing
2116 kumaneko 1054 *
2117     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
2118 kumaneko 1057 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
2119     * domainname = $0; } else if ( $1 == "use_profile" ) {
2120     * print $2 " " domainname; domainname = ""; } } ; '
2121 kumaneko 1052 */
2122 kumaneko 2002 static int ccs_read_domain_profile(struct ccs_io_buffer *head)
2123 kumaneko 111 {
2124 kumaneko 722 struct list1_head *pos;
2125 kumaneko 1052 if (head->read_eof)
2126     return 0;
2127 kumaneko 2002 list1_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
2128 kumaneko 111 struct domain_info *domain;
2129 kumaneko 722 domain = list1_entry(pos, struct domain_info, list);
2130 kumaneko 1052 if (domain->is_deleted)
2131     continue;
2132     if (!ccs_io_printf(head, "%u %s\n", domain->profile,
2133     domain->domainname->name))
2134     return 0;
2135 kumaneko 111 }
2136 kumaneko 1006 head->read_eof = true;
2137 kumaneko 111 return 0;
2138     }
2139    
2140 kumaneko 1052 /**
2141 kumaneko 2002 * ccs_write_pid: Specify PID to obtain domainname.
2142 kumaneko 1052 *
2143     * @head: Pointer to "struct ccs_io_buffer".
2144     *
2145     * Returns 0.
2146     */
2147 kumaneko 2002 static int ccs_write_pid(struct ccs_io_buffer *head)
2148 kumaneko 111 {
2149 kumaneko 1006 head->read_eof = false;
2150 kumaneko 111 return 0;
2151     }
2152    
2153 kumaneko 1052 /**
2154 kumaneko 2002 * ccs_read_pid - Read information of a process.
2155 kumaneko 1052 *
2156     * @head: Pointer to "struct ccs_io_buffer".
2157     *
2158 kumaneko 1705 * Returns the domainname which the specified PID is in or
2159     * process information of the specified PID on success,
2160 kumaneko 1052 * empty string otherwise.
2161     */
2162 kumaneko 2002 static int ccs_read_pid(struct ccs_io_buffer *head)
2163 kumaneko 111 {
2164 kumaneko 1705 char *buf = head->write_buf;
2165     bool task_info = false;
2166     unsigned int pid;
2167     struct task_struct *p;
2168     struct domain_info *domain = NULL;
2169     u32 tomoyo_flags = 0;
2170 kumaneko 1810 /* Accessing write_buf is safe because head->io_sem is held. */
2171 kumaneko 1809 if (!buf)
2172     goto done; /* Do nothing if open(O_RDONLY). */
2173 kumaneko 1705 if (head->read_avail || head->read_eof)
2174     goto done;
2175     head->read_eof = true;
2176 kumaneko 2002 if (ccs_str_starts(&buf, "info "))
2177 kumaneko 1705 task_info = true;
2178     pid = (unsigned int) simple_strtoul(buf, NULL, 10);
2179     /***** CRITICAL SECTION START *****/
2180     read_lock(&tasklist_lock);
2181     p = find_task_by_pid(pid);
2182     if (p) {
2183     domain = p->domain_info;
2184     tomoyo_flags = p->tomoyo_flags;
2185 kumaneko 111 }
2186 kumaneko 1705 read_unlock(&tasklist_lock);
2187     /***** CRITICAL SECTION END *****/
2188     if (!domain)
2189     goto done;
2190     if (!task_info)
2191     ccs_io_printf(head, "%u %u %s", pid, domain->profile,
2192     domain->domainname->name);
2193     else
2194     ccs_io_printf(head, "%u manager=%s execute_handler=%s "
2195     "state[0]=%u state[1]=%u state[2]=%u", pid,
2196     tomoyo_flags & CCS_TASK_IS_POLICY_MANAGER ?
2197     "yes" : "no",
2198     tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER ?
2199     "yes" : "no",
2200     (u8) (tomoyo_flags >> 24),
2201     (u8) (tomoyo_flags >> 16),
2202     (u8) (tomoyo_flags >> 8));
2203     done:
2204 kumaneko 111 return 0;
2205     }
2206    
2207     #ifdef CONFIG_TOMOYO
2208    
2209 kumaneko 1052 /**
2210 kumaneko 2002 * ccs_write_exception_policy - Write exception policy.
2211 kumaneko 1052 *
2212     * @head: Pointer to "struct ccs_io_buffer".
2213     *
2214     * Returns 0 on success, negative value otherwise.
2215     */
2216 kumaneko 2002 static int ccs_write_exception_policy(struct ccs_io_buffer *head)
2217 kumaneko 111 {
2218     char *data = head->write_buf;
2219 kumaneko 2002 bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);
2220     if (ccs_str_starts(&data, KEYWORD_KEEP_DOMAIN))
2221 kumaneko 1052 return ccs_write_domain_keeper_policy(data, false, is_delete);
2222 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_NO_KEEP_DOMAIN))
2223 kumaneko 1052 return ccs_write_domain_keeper_policy(data, true, is_delete);
2224 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_INITIALIZE_DOMAIN))
2225 kumaneko 1052 return ccs_write_domain_initializer_policy(data, false,
2226     is_delete);
2227 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_NO_INITIALIZE_DOMAIN))
2228 kumaneko 1052 return ccs_write_domain_initializer_policy(data, true,
2229     is_delete);
2230 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_ALIAS))
2231 kumaneko 1052 return ccs_write_alias_policy(data, is_delete);
2232 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_AGGREGATOR))
2233 kumaneko 1052 return ccs_write_aggregator_policy(data, is_delete);
2234 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_ALLOW_READ))
2235 kumaneko 1052 return ccs_write_globally_readable_policy(data, is_delete);
2236 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))
2237 kumaneko 1052 return ccs_write_globally_usable_env_policy(data, is_delete);
2238 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_FILE_PATTERN))
2239 kumaneko 1052 return ccs_write_pattern_policy(data, is_delete);
2240 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_PATH_GROUP))
2241 kumaneko 1052 return ccs_write_path_group_policy(data, is_delete);
2242 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_DENY_REWRITE))
2243 kumaneko 1052 return ccs_write_no_rewrite_policy(data, is_delete);
2244 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_ADDRESS_GROUP))
2245 kumaneko 1052 return ccs_write_address_group_policy(data, is_delete);
2246 kumaneko 111 return -EINVAL;
2247     }
2248    
2249 kumaneko 1052 /**
2250 kumaneko 2002 * ccs_read_exception_policy - Read exception policy.
2251 kumaneko 1052 *
2252     * @head: Pointer to "struct ccs_io_buffer".
2253     *
2254     * Returns 0 on success, -EINVAL otherwise.
2255     */
2256 kumaneko 2002 static int ccs_read_exception_policy(struct ccs_io_buffer *head)
2257 kumaneko 111 {
2258     if (!head->read_eof) {
2259     switch (head->read_step) {
2260     case 0:
2261 kumaneko 1052 head->read_var2 = NULL;
2262     head->read_step = 1;
2263 kumaneko 111 case 1:
2264 kumaneko 1052 if (!ccs_read_domain_keeper_policy(head))
2265     break;
2266     head->read_var2 = NULL;
2267     head->read_step = 2;
2268 kumaneko 111 case 2:
2269 kumaneko 1052 if (!ccs_read_globally_readable_policy(head))
2270     break;
2271     head->read_var2 = NULL;
2272     head->read_step = 3;
2273 kumaneko 111 case 3:
2274 kumaneko 1052 if (!ccs_read_globally_usable_env_policy(head))
2275     break;
2276     head->read_var2 = NULL;
2277     head->read_step = 4;
2278 kumaneko 111 case 4:
2279 kumaneko 1052 if (!ccs_read_domain_initializer_policy(head))
2280     break;
2281