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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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