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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


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