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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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