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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2539 - (hide annotations) (download) (as text)
Thu May 14 00:07:19 2009 UTC (15 years ago) by kumaneko
Original Path: branches/ccs-patch/fs/ccs_common.c
File MIME type: text/x-csrc
File size: 91940 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 2519 * Version: 1.6.8-pre 2009/05/08
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 2308 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 kumaneko 2303 case CCS_MAC_FOR_IOCTL:
1239 kumaneko 2282 case CCS_MAC_FOR_ARGV0:
1240     case CCS_MAC_FOR_ENV:
1241     case CCS_MAC_FOR_NETWORK:
1242     case CCS_MAC_FOR_SIGNAL:
1243     case CCS_MAX_ACCEPT_ENTRY:
1244     case CCS_VERBOSE:
1245 kumaneko 111 #endif
1246 kumaneko 1006 continue;
1247 kumaneko 111 }
1248 kumaneko 1052 #endif
1249 kumaneko 2254 if (!ccs_profile_entry_used[type])
1250     continue;
1251 kumaneko 1052 if (!type) { /* Print profile' comment tag. */
1252     if (!ccs_io_printf(head, "%u-COMMENT=%s\n",
1253 kumaneko 2002 index, ccs_profile->comment ?
1254     ccs_profile->comment->name : ""))
1255 kumaneko 1052 break;
1256     continue;
1257     }
1258     type--;
1259     if (type >= CCS_MAX_CONTROL_INDEX) {
1260 kumaneko 1015 #ifdef CONFIG_TOMOYO
1261 kumaneko 1052 const int i = type - CCS_MAX_CONTROL_INDEX;
1262 kumaneko 2002 const u8 value = ccs_profile->capability_value[i];
1263 kumaneko 1052 if (!ccs_io_printf(head,
1264     "%u-" KEYWORD_MAC_FOR_CAPABILITY
1265     "%s=%s\n", index,
1266 kumaneko 2002 ccs_capability_control_keyword[i],
1267     ccs_mode_4[value]))
1268 kumaneko 1052 break;
1269 kumaneko 1015 #endif
1270 kumaneko 1006 } else {
1271 kumaneko 2002 const unsigned int value = ccs_profile->value[type];
1272 kumaneko 1014 const char **modes = NULL;
1273 kumaneko 1052 const char *keyword = ccs_control_array[type].keyword;
1274     switch (ccs_control_array[type].max_value) {
1275 kumaneko 1014 case 3:
1276 kumaneko 2002 modes = ccs_mode_4;
1277 kumaneko 1014 break;
1278     case 1:
1279 kumaneko 2002 modes = ccs_mode_2;
1280 kumaneko 1014 break;
1281     }
1282     if (modes) {
1283 kumaneko 1052 if (!ccs_io_printf(head, "%u-%s=%s\n", index,
1284     keyword, modes[value]))
1285     break;
1286 kumaneko 1014 } else {
1287 kumaneko 1052 if (!ccs_io_printf(head, "%u-%s=%u\n", index,
1288     keyword, value))
1289     break;
1290 kumaneko 1014 }
1291 kumaneko 1006 }
1292     }
1293 kumaneko 2002 if (step == MAX_PROFILES * ccs_total)
1294 kumaneko 1052 head->read_eof = true;
1295 kumaneko 111 return 0;
1296     }
1297    
1298 kumaneko 1052 /* Structure for policy manager. */
1299 kumaneko 2002 struct ccs_policy_manager_entry {
1300 kumaneko 722 struct list1_head list;
1301 kumaneko 1052 /* A path to program or a domainname. */
1302 kumaneko 2002 const struct ccs_path_info *manager;
1303 kumaneko 1052 bool is_domain; /* True if manager is a domainname. */
1304     bool is_deleted; /* True if this entry is deleted. */
1305 kumaneko 214 };
1306 kumaneko 111
1307 kumaneko 2002 /* The list for "struct ccs_policy_manager_entry". */
1308     static LIST1_HEAD(ccs_policy_manager_list);
1309 kumaneko 111
1310 kumaneko 1052 /**
1311 kumaneko 2002 * ccs_update_manager_entry - Add a manager entry.
1312 kumaneko 1052 *
1313     * @manager: The path to manager or the domainnamme.
1314     * @is_delete: True if it is a delete request.
1315     *
1316     * Returns 0 on success, negative value otherwise.
1317     */
1318 kumaneko 2002 static int ccs_update_manager_entry(const char *manager, const bool is_delete)
1319 kumaneko 111 {
1320 kumaneko 2002 struct ccs_policy_manager_entry *new_entry;
1321     struct ccs_policy_manager_entry *ptr;
1322 kumaneko 652 static DEFINE_MUTEX(lock);
1323 kumaneko 2002 const struct ccs_path_info *saved_manager;
1324 kumaneko 111 int error = -ENOMEM;
1325 kumaneko 1006 bool is_domain = false;
1326 kumaneko 1052 if (ccs_is_domain_def(manager)) {
1327     if (!ccs_is_correct_domain(manager, __func__))
1328     return -EINVAL;
1329 kumaneko 1006 is_domain = true;
1330 kumaneko 111 } else {
1331 kumaneko 1052 if (!ccs_is_correct_path(manager, 1, -1, -1, __func__))
1332     return -EINVAL;
1333 kumaneko 111 }
1334 kumaneko 1052 saved_manager = ccs_save_name(manager);
1335     if (!saved_manager)
1336     return -ENOMEM;
1337 kumaneko 652 mutex_lock(&lock);
1338 kumaneko 2002 list1_for_each_entry(ptr, &ccs_policy_manager_list, list) {
1339 kumaneko 1064 if (ptr->manager != saved_manager)
1340     continue;
1341     ptr->is_deleted = is_delete;
1342     error = 0;
1343     goto out;
1344 kumaneko 111 }
1345     if (is_delete) {
1346     error = -ENOENT;
1347     goto out;
1348     }
1349 kumaneko 1052 new_entry = ccs_alloc_element(sizeof(*new_entry));
1350     if (!new_entry)
1351     goto out;
1352 kumaneko 111 new_entry->manager = saved_manager;
1353     new_entry->is_domain = is_domain;
1354 kumaneko 2002 list1_add_tail_mb(&new_entry->list, &ccs_policy_manager_list);
1355 kumaneko 111 error = 0;
1356     out:
1357 kumaneko 652 mutex_unlock(&lock);
1358 kumaneko 1052 if (!error)
1359     ccs_update_counter(CCS_UPDATES_COUNTER_MANAGER);
1360 kumaneko 111 return error;
1361     }
1362    
1363 kumaneko 1052 /**
1364 kumaneko 2002 * ccs_write_manager_policy - Write manager policy.
1365 kumaneko 1052 *
1366 kumaneko 1657 * @head: Pointer to "struct ccs_io_buffer".
1367 kumaneko 1052 *
1368     * Returns 0 on success, negative value otherwise.
1369     */
1370 kumaneko 2002 static int ccs_write_manager_policy(struct ccs_io_buffer *head)
1371 kumaneko 111 {
1372 kumaneko 1052 char *data = head->write_buf;
1373 kumaneko 2002 bool is_delete = ccs_str_starts(&data, KEYWORD_DELETE);
1374 kumaneko 1064 if (!strcmp(data, "manage_by_non_root")) {
1375 kumaneko 2002 ccs_manage_by_non_root = !is_delete;
1376 kumaneko 1006 return 0;
1377     }
1378 kumaneko 2002 return ccs_update_manager_entry(data, is_delete);
1379 kumaneko 111 }
1380    
1381 kumaneko 1052 /**
1382 kumaneko 2002 * ccs_read_manager_policy - Read manager policy.
1383 kumaneko 1052 *
1384 kumaneko 1657 * @head: Pointer to "struct ccs_io_buffer".
1385 kumaneko 1052 *
1386     * Returns 0.
1387     */
1388 kumaneko 2002 static int ccs_read_manager_policy(struct ccs_io_buffer *head)
1389 kumaneko 111 {
1390 kumaneko 722 struct list1_head *pos;
1391 kumaneko 1052 if (head->read_eof)
1392     return 0;
1393 kumaneko 2002 list1_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {
1394     struct ccs_policy_manager_entry *ptr;
1395     ptr = list1_entry(pos, struct ccs_policy_manager_entry, list);
1396 kumaneko 1052 if (ptr->is_deleted)
1397     continue;
1398     if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
1399     return 0;
1400 kumaneko 111 }
1401 kumaneko 1006 head->read_eof = true;
1402 kumaneko 111 return 0;
1403     }
1404    
1405 kumaneko 1052 /**
1406 kumaneko 2002 * ccs_is_policy_manager - Check whether the current process is a policy manager.
1407 kumaneko 1052 *
1408     * Returns true if the current process is permitted to modify policy
1409     * via /proc/ccs/ interface.
1410     */
1411 kumaneko 2002 static bool ccs_is_policy_manager(void)
1412 kumaneko 111 {
1413 kumaneko 2002 struct ccs_policy_manager_entry *ptr;
1414 kumaneko 111 const char *exe;
1415 kumaneko 1578 struct task_struct *task = current;
1416 kumaneko 2282 const struct ccs_path_info *domainname
1417     = ccs_current_domain()->domainname;
1418 kumaneko 1006 bool found = false;
1419 kumaneko 2040 if (!ccs_policy_loaded)
1420 kumaneko 1052 return true;
1421 kumaneko 2282 if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)
1422 kumaneko 1578 return true;
1423 kumaneko 2016 if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
1424 kumaneko 1052 return false;
1425 kumaneko 2002 list1_for_each_entry(ptr, &ccs_policy_manager_list, list) {
1426 kumaneko 1052 if (!ptr->is_deleted && ptr->is_domain
1427 kumaneko 1578 && !ccs_pathcmp(domainname, ptr->manager)) {
1428     /* Set manager flag. */
1429 kumaneko 2282 task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;
1430 kumaneko 1052 return true;
1431 kumaneko 1578 }
1432 kumaneko 111 }
1433 kumaneko 1052 exe = ccs_get_exe();
1434     if (!exe)
1435     return false;
1436 kumaneko 2002 list1_for_each_entry(ptr, &ccs_policy_manager_list, list) {
1437 kumaneko 1052 if (!ptr->is_deleted && !ptr->is_domain
1438     && !strcmp(exe, ptr->manager->name)) {
1439 kumaneko 1006 found = true;
1440 kumaneko 1578 /* Set manager flag. */
1441 kumaneko 2282 task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;
1442 kumaneko 708 break;
1443     }
1444 kumaneko 111 }
1445 kumaneko 708 if (!found) { /* Reduce error messages. */
1446 kumaneko 2002 static pid_t ccs_last_pid;
1447 kumaneko 111 const pid_t pid = current->pid;
1448 kumaneko 2002 if (ccs_last_pid != pid) {
1449 kumaneko 1052 printk(KERN_WARNING "%s ( %s ) is not permitted to "
1450     "update policies.\n", domainname->name, exe);
1451 kumaneko 2002 ccs_last_pid = pid;
1452 kumaneko 111 }
1453     }
1454     ccs_free(exe);
1455 kumaneko 708 return found;
1456 kumaneko 111 }
1457    
1458     #ifdef CONFIG_TOMOYO
1459    
1460 kumaneko 1052 /**
1461     * ccs_find_condition_part - Find condition part from the statement.
1462     *
1463     * @data: String to parse.
1464     *
1465     * Returns pointer to the condition part if it was found in the statement,
1466     * NULL otherwise.
1467     */
1468     static char *ccs_find_condition_part(char *data)
1469 kumaneko 581 {
1470 kumaneko 1064 char *cp = strstr(data, " if ");
1471 kumaneko 581 if (cp) {
1472 kumaneko 1747 while (1) {
1473     char *cp2 = strstr(cp + 3, " if ");
1474     if (!cp2)
1475     break;
1476 kumaneko 1064 cp = cp2;
1477 kumaneko 1747 }
1478 kumaneko 581 *cp++ = '\0';
1479 kumaneko 1052 } else {
1480     cp = strstr(data, " ; set ");
1481     if (cp)
1482     *cp++ = '\0';
1483 kumaneko 581 }
1484     return cp;
1485     }
1486    
1487 kumaneko 1052 /**
1488 kumaneko 2002 * ccs_is_select_one - Parse select command.
1489 kumaneko 1609 *
1490     * @head: Pointer to "struct ccs_io_buffer".
1491     * @data: String to parse.
1492     *
1493     * Returns true on success, false otherwise.
1494     */
1495 kumaneko 2002 static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)
1496 kumaneko 1609 {
1497     unsigned int pid;
1498 kumaneko 2282 struct ccs_domain_info *domain = NULL;
1499 kumaneko 1926 if (!strcmp(data, "allow_execute")) {
1500     head->read_execute_only = true;
1501     return true;
1502     }
1503 kumaneko 1609 if (sscanf(data, "pid=%u", &pid) == 1) {
1504     struct task_struct *p;
1505     /***** CRITICAL SECTION START *****/
1506     read_lock(&tasklist_lock);
1507     p = find_task_by_pid(pid);
1508     if (p)
1509 kumaneko 2282 domain = ccs_task_domain(p);
1510 kumaneko 1609 read_unlock(&tasklist_lock);
1511     /***** CRITICAL SECTION END *****/
1512     } else if (!strncmp(data, "domain=", 7)) {
1513     if (ccs_is_domain_def(data + 7))
1514     domain = ccs_find_domain(data + 7);
1515     } else
1516     return false;
1517 kumaneko 1809 head->write_var1 = domain;
1518 kumaneko 1810 /* Accessing read_buf is safe because head->io_sem is held. */
1519 kumaneko 1809 if (!head->read_buf)
1520     return true; /* Do nothing if open(O_WRONLY). */
1521 kumaneko 1609 head->read_avail = 0;
1522     ccs_io_printf(head, "# select %s\n", data);
1523     head->read_single_domain = true;
1524     head->read_eof = !domain;
1525     if (domain) {
1526 kumaneko 2282 struct ccs_domain_info *d;
1527 kumaneko 1609 head->read_var1 = NULL;
1528 kumaneko 2002 list1_for_each_entry(d, &ccs_domain_list, list) {
1529 kumaneko 1609 if (d == domain)
1530     break;
1531     head->read_var1 = &d->list;
1532     }
1533     head->read_var2 = NULL;
1534     head->read_bit = 0;
1535     head->read_step = 0;
1536     if (domain->is_deleted)
1537     ccs_io_printf(head, "# This is a deleted domain.\n");
1538     }
1539     return true;
1540     }
1541    
1542     /**
1543 kumaneko 2002 * ccs_write_domain_policy - Write domain policy.
1544 kumaneko 1052 *
1545     * @head: Pointer to "struct ccs_io_buffer".
1546     *
1547     * Returns 0 on success, negative value otherwise.
1548     */
1549 kumaneko 2002 static int ccs_write_domain_policy(struct ccs_io_buffer *head)
1550 kumaneko 111 {
1551     char *data = head->write_buf;
1552 kumaneko 2282 struct ccs_domain_info *domain = head->write_var1;
1553 kumaneko 1064 bool is_delete = false;
1554     bool is_select = 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 if (is_select && ccs_is_select_one(head, data))
1563 kumaneko 1609 return 0;
1564 kumaneko 1606 /* Don't allow updating policies by non manager programs. */
1565 kumaneko 2002 if (!ccs_is_policy_manager())
1566 kumaneko 1606 return -EPERM;
1567 kumaneko 1052 if (ccs_is_domain_def(data)) {
1568 kumaneko 1064 domain = NULL;
1569     if (is_delete)
1570 kumaneko 1052 ccs_delete_domain(data);
1571 kumaneko 1064 else if (is_select)
1572 kumaneko 1052 domain = ccs_find_domain(data);
1573 kumaneko 1064 else
1574 kumaneko 1052 domain = ccs_find_or_assign_new_domain(data, 0);
1575 kumaneko 111 head->write_var1 = domain;
1576 kumaneko 1052 ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
1577 kumaneko 111 return 0;
1578     }
1579 kumaneko 1052 if (!domain)
1580     return -EINVAL;
1581 kumaneko 581
1582 kumaneko 1052 if (sscanf(data, KEYWORD_USE_PROFILE "%u", &profile) == 1
1583     && profile < MAX_PROFILES) {
1584 kumaneko 2040 if (ccs_profile_ptr[profile] || !ccs_policy_loaded)
1585 kumaneko 1052 domain->profile = (u8) profile;
1586 kumaneko 581 return 0;
1587     }
1588 kumaneko 1052 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
1589     ccs_set_domain_flag(domain, is_delete,
1590     DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
1591 kumaneko 1007 return 0;
1592     }
1593 kumaneko 1052 if (!strcmp(data, KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {
1594     ccs_set_domain_flag(domain, is_delete,
1595     DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV);
1596 kumaneko 1007 return 0;
1597     }
1598 kumaneko 1052 cp = ccs_find_condition_part(data);
1599     if (cp) {
1600     cond = ccs_find_or_assign_new_condition(cp);
1601     if (!cond)
1602     return -EINVAL;
1603 kumaneko 111 }
1604 kumaneko 2002 if (ccs_str_starts(&data, KEYWORD_ALLOW_CAPABILITY))
1605 kumaneko 1052 return ccs_write_capability_policy(data, domain, cond,
1606     is_delete);
1607 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_NETWORK))
1608 kumaneko 1052 return ccs_write_network_policy(data, domain, cond, is_delete);
1609 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_SIGNAL))
1610 kumaneko 1052 return ccs_write_signal_policy(data, domain, cond, is_delete);
1611 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_ARGV0))
1612 kumaneko 1052 return ccs_write_argv0_policy(data, domain, cond, is_delete);
1613 kumaneko 2002 else if (ccs_str_starts(&data, KEYWORD_ALLOW_ENV))
1614 kumaneko 1052 return ccs_write_env_policy(data, domain, cond, is_delete);
1615 kumaneko 2271 else if (ccs_str_starts(&data, KEYWORD_ALLOW_IOCTL))
1616     return ccs_write_ioctl_policy(data, domain, cond, is_delete);
1617 kumaneko 1052 else
1618     return ccs_write_file_policy(data, domain, cond, is_delete);
1619 kumaneko 111 }
1620    
1621 kumaneko 1052 /**
1622 kumaneko 2002 * ccs_print_single_path_acl - Print a single path ACL entry.
1623 kumaneko 1052 *
1624     * @head: Pointer to "struct ccs_io_buffer".
1625 kumaneko 2002 * @ptr: Pointer to "struct ccs_single_path_acl_record".
1626     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1627 kumaneko 1052 *
1628     * Returns true on success, false otherwise.
1629     */
1630 kumaneko 2002 static bool ccs_print_single_path_acl(struct ccs_io_buffer *head,
1631     struct ccs_single_path_acl_record *ptr,
1632     const struct ccs_condition_list *cond)
1633 kumaneko 856 {
1634     int pos;
1635     u8 bit;
1636 kumaneko 1052 const char *atmark = "";
1637     const char *filename;
1638 kumaneko 856 const u16 perm = ptr->perm;
1639 kumaneko 1052 if (ptr->u_is_group) {
1640     atmark = "@";
1641     filename = ptr->u.group->group_name->name;
1642     } else {
1643     filename = ptr->u.filename->name;
1644     }
1645 kumaneko 856 for (bit = head->read_bit; bit < MAX_SINGLE_PATH_OPERATION; bit++) {
1646     const char *msg;
1647 kumaneko 1052 if (!(perm & (1 << bit)))
1648     continue;
1649 kumaneko 1926 if (head->read_execute_only && bit != TYPE_EXECUTE_ACL)
1650     continue;
1651 kumaneko 856 /* Print "read/write" instead of "read" and "write". */
1652 kumaneko 1052 if ((bit == TYPE_READ_ACL || bit == TYPE_WRITE_ACL)
1653     && (perm & (1 << TYPE_READ_WRITE_ACL)))
1654     continue;
1655     msg = ccs_sp2keyword(bit);
1656 kumaneko 856 pos = head->read_avail;
1657 kumaneko 1052 if (!ccs_io_printf(head, "allow_%s %s%s", msg,
1658     atmark, filename) ||
1659 kumaneko 1054 !ccs_print_condition(head, cond))
1660 kumaneko 1052 goto out;
1661 kumaneko 856 }
1662     head->read_bit = 0;
1663 kumaneko 1006 return true;
1664 kumaneko 856 out:
1665     head->read_bit = bit;
1666     head->read_avail = pos;
1667 kumaneko 1006 return false;
1668 kumaneko 856 }
1669    
1670 kumaneko 1052 /**
1671 kumaneko 2002 * ccs_print_double_path_acl - Print a double path ACL entry.
1672 kumaneko 1052 *
1673     * @head: Pointer to "struct ccs_io_buffer".
1674 kumaneko 2002 * @ptr: Pointer to "struct ccs_double_path_acl_record".
1675     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1676 kumaneko 1052 *
1677     * Returns true on success, false otherwise.
1678     */
1679 kumaneko 2002 static bool ccs_print_double_path_acl(struct ccs_io_buffer *head,
1680     struct ccs_double_path_acl_record *ptr,
1681     const struct ccs_condition_list *cond)
1682 kumaneko 856 {
1683     int pos;
1684 kumaneko 1052 const char *atmark1 = "";
1685     const char *atmark2 = "";
1686     const char *filename1;
1687     const char *filename2;
1688 kumaneko 856 const u8 perm = ptr->perm;
1689     u8 bit;
1690 kumaneko 1052 if (ptr->u1_is_group) {
1691     atmark1 = "@";
1692     filename1 = ptr->u1.group1->group_name->name;
1693     } else {
1694     filename1 = ptr->u1.filename1->name;
1695     }
1696     if (ptr->u2_is_group) {
1697     atmark2 = "@";
1698     filename2 = ptr->u2.group2->group_name->name;
1699     } else {
1700     filename2 = ptr->u2.filename2->name;
1701     }
1702 kumaneko 856 for (bit = head->read_bit; bit < MAX_DOUBLE_PATH_OPERATION; bit++) {
1703     const char *msg;
1704 kumaneko 1052 if (!(perm & (1 << bit)))
1705     continue;
1706     msg = ccs_dp2keyword(bit);
1707 kumaneko 856 pos = head->read_avail;
1708 kumaneko 1052 if (!ccs_io_printf(head, "allow_%s %s%s %s%s", msg,
1709 kumaneko 1064 atmark1, filename1, atmark2, filename2) ||
1710 kumaneko 1054 !ccs_print_condition(head, cond))
1711 kumaneko 1052 goto out;
1712 kumaneko 856 }
1713 kumaneko 1032 head->read_bit = 0;
1714 kumaneko 1006 return true;
1715 kumaneko 856 out:
1716     head->read_bit = bit;
1717     head->read_avail = pos;
1718 kumaneko 1006 return false;
1719 kumaneko 856 }
1720    
1721 kumaneko 1052 /**
1722 kumaneko 2271 * ccs_print_ioctl_acl - Print an ioctl ACL entry.
1723     *
1724     * @head: Pointer to "struct ccs_io_buffer".
1725     * @ptr: Pointer to "struct ccs_ioctl_acl_record".
1726     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1727     *
1728     * Returns true on success, false otherwise.
1729     */
1730     static bool ccs_print_ioctl_acl(struct ccs_io_buffer *head,
1731     struct ccs_ioctl_acl_record *ptr,
1732     const struct ccs_condition_list *cond)
1733     {
1734     int pos = head->read_avail;
1735     const char *atmark = "";
1736     const char *filename;
1737     const unsigned int cmd_min = ptr->cmd_min;
1738     const unsigned int cmd_max = ptr->cmd_max;
1739     if (ptr->u_is_group) {
1740     atmark = "@";
1741     filename = ptr->u.group->group_name->name;
1742     } else {
1743     filename = ptr->u.filename->name;
1744     }
1745     if (!ccs_io_printf(head, KEYWORD_ALLOW_IOCTL "%s%s ", atmark, filename))
1746     goto out;
1747     if (!ccs_io_printf(head, "%u", cmd_min))
1748     goto out;
1749     if (cmd_min != cmd_max && !ccs_io_printf(head, "-%u", cmd_max))
1750     goto out;
1751     if (!ccs_print_condition(head, cond))
1752     goto out;
1753     return true;
1754     out:
1755     head->read_avail = pos;
1756     return false;
1757     }
1758    
1759     /**
1760 kumaneko 2002 * ccs_print_argv0_acl - Print an argv[0] ACL entry.
1761 kumaneko 1052 *
1762     * @head: Pointer to "struct ccs_io_buffer".
1763 kumaneko 2002 * @ptr: Pointer to "struct ccs_argv0_acl_record".
1764     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1765 kumaneko 1052 *
1766     * Returns true on success, false otherwise.
1767     */
1768 kumaneko 2002 static bool ccs_print_argv0_acl(struct ccs_io_buffer *head,
1769     struct ccs_argv0_acl_record *ptr,
1770     const struct ccs_condition_list *cond)
1771 kumaneko 856 {
1772     int pos = head->read_avail;
1773 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_ARGV0 "%s %s",
1774     ptr->filename->name, ptr->argv0->name))
1775     goto out;
1776 kumaneko 1054 if (!ccs_print_condition(head, cond))
1777 kumaneko 1052 goto out;
1778 kumaneko 1006 return true;
1779 kumaneko 856 out:
1780     head->read_avail = pos;
1781 kumaneko 1006 return false;
1782 kumaneko 856 }
1783    
1784 kumaneko 1052 /**
1785 kumaneko 2002 * ccs_print_env_acl - Print an evironment variable name's ACL entry.
1786 kumaneko 1052 *
1787     * @head: Pointer to "struct ccs_io_buffer".
1788 kumaneko 2002 * @ptr: Pointer to "struct ccs_env_acl_record".
1789     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1790 kumaneko 1052 *
1791     * Returns true on success, false otherwise.
1792     */
1793 kumaneko 2002 static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1794     struct ccs_env_acl_record *ptr,
1795     const struct ccs_condition_list *cond)
1796 kumaneko 856 {
1797     int pos = head->read_avail;
1798 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_ENV "%s", ptr->env->name))
1799     goto out;
1800 kumaneko 1054 if (!ccs_print_condition(head, cond))
1801 kumaneko 1052 goto out;
1802 kumaneko 1006 return true;
1803 kumaneko 856 out:
1804     head->read_avail = pos;
1805 kumaneko 1006 return false;
1806 kumaneko 856 }
1807    
1808 kumaneko 1052 /**
1809 kumaneko 2002 * ccs_print_capability_acl - Print a capability ACL entry.
1810 kumaneko 1052 *
1811     * @head: Pointer to "struct ccs_io_buffer".
1812 kumaneko 2002 * @ptr: Pointer to "struct ccs_capability_acl_record".
1813     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1814 kumaneko 1052 *
1815     * Returns true on success, false otherwise.
1816     */
1817 kumaneko 2002 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1818     struct ccs_capability_acl_record *ptr,
1819     const struct ccs_condition_list *cond)
1820 kumaneko 856 {
1821 kumaneko 860 int pos = head->read_avail;
1822 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_CAPABILITY "%s",
1823     ccs_cap2keyword(ptr->operation)))
1824     goto out;
1825 kumaneko 1054 if (!ccs_print_condition(head, cond))
1826 kumaneko 1052 goto out;
1827 kumaneko 1006 return true;
1828 kumaneko 856 out:
1829     head->read_avail = pos;
1830 kumaneko 1006 return false;
1831 kumaneko 856 }
1832    
1833 kumaneko 1052 /**
1834 kumaneko 2002 * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.
1835 kumaneko 1052 *
1836     * @head: Pointer to "struct ccs_io_buffer".
1837 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1838 kumaneko 1052 *
1839     * Returns true on success, false otherwise.
1840     */
1841 kumaneko 2002 static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,
1842     struct ccs_ip_network_acl_record *ptr)
1843 kumaneko 856 {
1844 kumaneko 1052 const u32 min_address = ptr->u.ipv4.min;
1845     const u32 max_address = ptr->u.ipv4.max;
1846     if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))
1847     return false;
1848     if (min_address != max_address
1849     && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))
1850     return false;
1851     return true;
1852     }
1853    
1854     /**
1855 kumaneko 2002 * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.
1856 kumaneko 1052 *
1857     * @head: Pointer to "struct ccs_io_buffer".
1858 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1859 kumaneko 1052 *
1860     * Returns true on success, false otherwise.
1861     */
1862 kumaneko 2002 static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,
1863     struct ccs_ip_network_acl_record *ptr)
1864 kumaneko 1052 {
1865     char buf[64];
1866     const struct in6_addr *min_address = ptr->u.ipv6.min;
1867     const struct in6_addr *max_address = ptr->u.ipv6.max;
1868     ccs_print_ipv6(buf, sizeof(buf), min_address);
1869     if (!ccs_io_printf(head, "%s", buf))
1870     return false;
1871     if (min_address != max_address) {
1872     ccs_print_ipv6(buf, sizeof(buf), max_address);
1873     if (!ccs_io_printf(head, "-%s", buf))
1874     return false;
1875     }
1876     return true;
1877     }
1878    
1879     /**
1880 kumaneko 2002 * ccs_print_port_entry - Print port number of a network ACL entry.
1881 kumaneko 1052 *
1882     * @head: Pointer to "struct ccs_io_buffer".
1883 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1884 kumaneko 1052 *
1885     * Returns true on success, false otherwise.
1886     */
1887 kumaneko 2002 static bool ccs_print_port_entry(struct ccs_io_buffer *head,
1888     struct ccs_ip_network_acl_record *ptr)
1889 kumaneko 1052 {
1890 kumaneko 2046 const u16 min_port = ptr->min_port;
1891     const u16 max_port = ptr->max_port;
1892 kumaneko 1052 if (!ccs_io_printf(head, " %u", min_port))
1893     return false;
1894     if (min_port != max_port && !ccs_io_printf(head, "-%u", max_port))
1895     return false;
1896     return true;
1897     }
1898    
1899     /**
1900 kumaneko 2002 * ccs_print_network_acl - Print a network ACL entry.
1901 kumaneko 1052 *
1902     * @head: Pointer to "struct ccs_io_buffer".
1903 kumaneko 2002 * @ptr: Pointer to "struct ccs_ip_network_acl_record".
1904     * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1905 kumaneko 1052 *
1906     * Returns true on success, false otherwise.
1907     */
1908 kumaneko 2002 static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1909     struct ccs_ip_network_acl_record *ptr,
1910     const struct ccs_condition_list *cond)
1911 kumaneko 1052 {
1912 kumaneko 856 int pos = head->read_avail;
1913 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_NETWORK "%s ",
1914     ccs_net2keyword(ptr->operation_type)))
1915     goto out;
1916 kumaneko 856 switch (ptr->record_type) {
1917     case IP_RECORD_TYPE_ADDRESS_GROUP:
1918 kumaneko 1052 if (!ccs_io_printf(head, "@%s", ptr->u.group->group_name->name))
1919     goto out;
1920 kumaneko 856 break;
1921     case IP_RECORD_TYPE_IPv4:
1922 kumaneko 2002 if (!ccs_print_ipv4_entry(head, ptr))
1923 kumaneko 1052 goto out;
1924 kumaneko 856 break;
1925     case IP_RECORD_TYPE_IPv6:
1926 kumaneko 2002 if (!ccs_print_ipv6_entry(head, ptr))
1927 kumaneko 1052 goto out;
1928 kumaneko 856 break;
1929     }
1930 kumaneko 2002 if (!ccs_print_port_entry(head, ptr))
1931 kumaneko 1052 goto out;
1932 kumaneko 1054 if (!ccs_print_condition(head, cond))
1933 kumaneko 1052 goto out;
1934 kumaneko 1006 return true;
1935 kumaneko 856 out:
1936     head->read_avail = pos;
1937 kumaneko 1006 return false;
1938 kumaneko 856 }
1939    
1940 kumaneko 1052 /**
1941 kumaneko 2002 * ccs_print_signal_acl - Print a signal ACL entry.
1942 kumaneko 1052 *
1943     * @head: Pointer to "struct ccs_io_buffer".
1944     * @ptr: Pointer to "struct signale_acl_record".
1945 kumaneko 2002 * @cond: Pointer to "struct ccs_condition_list". May be NULL.
1946 kumaneko 1052 *
1947     * Returns true on success, false otherwise.
1948     */
1949 kumaneko 2002 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1950     struct ccs_signal_acl_record *ptr,
1951     const struct ccs_condition_list *cond)
1952 kumaneko 856 {
1953     int pos = head->read_avail;
1954 kumaneko 1052 if (!ccs_io_printf(head, KEYWORD_ALLOW_SIGNAL "%u %s",
1955     ptr->sig, ptr->domainname->name))
1956     goto out;
1957 kumaneko 1054 if (!ccs_print_condition(head, cond))
1958 kumaneko 1052 goto out;
1959 kumaneko 1006 return true;
1960 kumaneko 856 out:
1961     head->read_avail = pos;
1962 kumaneko 1006 return false;
1963 kumaneko 856 }
1964    
1965 kumaneko 1052 /**
1966 kumaneko 2002 * ccs_print_execute_handler_record - Print an execute handler ACL entry.
1967 kumaneko 1052 *
1968     * @head: Pointer to "struct ccs_io_buffer".
1969     * @keyword: Name of the keyword.
1970 kumaneko 2002 * @ptr: Pointer to "struct ccs_execute_handler_record".
1971 kumaneko 1052 *
1972     * Returns true on success, false otherwise.
1973     */
1974 kumaneko 2002 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,
1975     const char *keyword,
1976     struct ccs_execute_handler_record *
1977     ptr)
1978 kumaneko 1029 {
1979 kumaneko 1052 return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1980 kumaneko 1029 }
1981    
1982 kumaneko 1052 /**
1983 kumaneko 2002 * ccs_print_entry - Print an ACL entry.
1984 kumaneko 1052 *
1985     * @head: Pointer to "struct ccs_io_buffer".
1986     * @ptr: Pointer to an ACL entry.
1987     *
1988     * Returns true on success, false otherwise.
1989     */
1990 kumaneko 2002 static bool ccs_print_entry(struct ccs_io_buffer *head,
1991     struct ccs_acl_info *ptr)
1992 kumaneko 111 {
1993 kumaneko 2002 const struct ccs_condition_list *cond = ccs_get_condition_part(ptr);
1994 kumaneko 1064 const u8 acl_type = ccs_acl_type2(ptr);
1995 kumaneko 1052 if (acl_type & ACL_DELETED)
1996     return true;
1997     if (acl_type == TYPE_SINGLE_PATH_ACL) {
1998 kumaneko 2002 struct ccs_single_path_acl_record *acl
1999     = container_of(ptr, struct ccs_single_path_acl_record,
2000 kumaneko 1052 head);
2001 kumaneko 2002 return ccs_print_single_path_acl(head, acl, cond);
2002 kumaneko 1052 }
2003 kumaneko 1926 if (acl_type == TYPE_EXECUTE_HANDLER) {
2004 kumaneko 2002 struct ccs_execute_handler_record *acl
2005     = container_of(ptr, struct ccs_execute_handler_record,
2006 kumaneko 1926 head);
2007     const char *keyword = KEYWORD_EXECUTE_HANDLER;
2008 kumaneko 2002 return ccs_print_execute_handler_record(head, keyword, acl);
2009 kumaneko 1926 }
2010     if (acl_type == TYPE_DENIED_EXECUTE_HANDLER) {
2011 kumaneko 2002 struct ccs_execute_handler_record *acl
2012     = container_of(ptr, struct ccs_execute_handler_record,
2013 kumaneko 1926 head);
2014     const char *keyword = KEYWORD_DENIED_EXECUTE_HANDLER;
2015 kumaneko 2002 return ccs_print_execute_handler_record(head, keyword, acl);
2016 kumaneko 1926 }
2017     if (head->read_execute_only)
2018     return true;
2019 kumaneko 1052 if (acl_type == TYPE_DOUBLE_PATH_ACL) {
2020 kumaneko 2002 struct ccs_double_path_acl_record *acl
2021     = container_of(ptr, struct ccs_double_path_acl_record,
2022 kumaneko 1052 head);
2023 kumaneko 2002 return ccs_print_double_path_acl(head, acl, cond);
2024 kumaneko 1052 }
2025 kumaneko 2271 if (acl_type == TYPE_IOCTL_ACL) {
2026     struct ccs_ioctl_acl_record *acl
2027     = container_of(ptr, struct ccs_ioctl_acl_record, head);
2028     return ccs_print_ioctl_acl(head, acl, cond);
2029     }
2030 kumaneko 1052 if (acl_type == TYPE_ARGV0_ACL) {
2031 kumaneko 2002 struct ccs_argv0_acl_record *acl
2032     = container_of(ptr, struct ccs_argv0_acl_record, head);
2033     return ccs_print_argv0_acl(head, acl, cond);
2034 kumaneko 1052 }
2035     if (acl_type == TYPE_ENV_ACL) {
2036 kumaneko 2002 struct ccs_env_acl_record *acl
2037     = container_of(ptr, struct ccs_env_acl_record, head);
2038     return ccs_print_env_acl(head, acl, cond);
2039 kumaneko 1052 }
2040     if (acl_type == TYPE_CAPABILITY_ACL) {
2041 kumaneko 2002 struct ccs_capability_acl_record *acl
2042     = container_of(ptr, struct ccs_capability_acl_record,
2043     head);
2044     return ccs_print_capability_acl(head, acl, cond);
2045 kumaneko 1052 }
2046     if (acl_type == TYPE_IP_NETWORK_ACL) {
2047 kumaneko 2002 struct ccs_ip_network_acl_record *acl
2048     = container_of(ptr, struct ccs_ip_network_acl_record,
2049     head);
2050     return ccs_print_network_acl(head, acl, cond);
2051 kumaneko 1052 }
2052     if (acl_type == TYPE_SIGNAL_ACL) {
2053 kumaneko 2002 struct ccs_signal_acl_record *acl
2054     = container_of(ptr, struct ccs_signal_acl_record, head);
2055     return ccs_print_signal_acl(head, acl, cond);
2056 kumaneko 1052 }
2057 kumaneko 1120 /* Workaround for gcc 3.2.2's inline bug. */
2058     if (acl_type & ACL_DELETED)
2059     return true;
2060 kumaneko 1052 BUG(); /* This must not happen. */
2061     return false;
2062     }
2063    
2064     /**
2065 kumaneko 2002 * ccs_read_domain_policy - Read domain policy.
2066 kumaneko 1052 *
2067     * @head: Pointer to "struct ccs_io_buffer".
2068     *
2069     * Returns 0.
2070     */
2071 kumaneko 2002 static int ccs_read_domain_policy(struct ccs_io_buffer *head)
2072 kumaneko 1052 {
2073 kumaneko 722 struct list1_head *dpos;
2074     struct list1_head *apos;
2075 kumaneko 1052 if (head->read_eof)
2076     return 0;
2077     if (head->read_step == 0)
2078     head->read_step = 1;
2079 kumaneko 2002 list1_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {
2080 kumaneko 2282 struct ccs_domain_info *domain;
2081 kumaneko 1052 const char *quota_exceeded = "";
2082 kumaneko 1180 const char *transition_failed = "";
2083 kumaneko 1052 const char *ignore_global_allow_read = "";
2084     const char *ignore_global_allow_env = "";
2085 kumaneko 2282 domain = list1_entry(dpos, struct ccs_domain_info, list);
2086 kumaneko 1052 if (head->read_step != 1)
2087     goto acl_loop;
2088 kumaneko 1609 if (domain->is_deleted && !head->read_single_domain)
2089     continue;
2090 kumaneko 1054 /* Print domainname and flags. */
2091 kumaneko 1052 if (domain->quota_warned)
2092     quota_exceeded = "quota_exceeded\n";
2093 kumaneko 1180 if (domain->flags & DOMAIN_FLAGS_TRANSITION_FAILED)
2094     transition_failed = "transition_failed\n";
2095 kumaneko 1052 if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
2096     ignore_global_allow_read
2097     = KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
2098     if (domain->flags & DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_ENV)
2099     ignore_global_allow_env
2100     = KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";
2101 kumaneko 1609 if (!ccs_io_printf(head, "%s\n" KEYWORD_USE_PROFILE "%u\n"
2102     "%s%s%s%s\n", domain->domainname->name,
2103     domain->profile, quota_exceeded,
2104     transition_failed,
2105 kumaneko 1052 ignore_global_allow_read,
2106     ignore_global_allow_env))
2107     return 0;
2108 kumaneko 708 head->read_step = 2;
2109 kumaneko 2041 acl_loop:
2110 kumaneko 1052 if (head->read_step == 3)
2111     goto tail_mark;
2112 kumaneko 1054 /* Print ACL entries in the domain. */
2113 kumaneko 1052 list1_for_each_cookie(apos, head->read_var2,
2114     &domain->acl_info_list) {
2115 kumaneko 2002 struct ccs_acl_info *ptr
2116     = list1_entry(apos, struct ccs_acl_info, list);
2117     if (!ccs_print_entry(head, ptr))
2118 kumaneko 1052 return 0;
2119 kumaneko 111 }
2120 kumaneko 708 head->read_step = 3;
2121 kumaneko 2041 tail_mark:
2122 kumaneko 1052 if (!ccs_io_printf(head, "\n"))
2123     return 0;
2124 kumaneko 708 head->read_step = 1;
2125 kumaneko 1606 if (head->read_single_domain)
2126     break;
2127 kumaneko 111 }
2128 kumaneko 1006 head->read_eof = true;
2129 kumaneko 111 return 0;
2130     }
2131    
2132 kumaneko 461 #endif
2133    
2134 kumaneko 1052 /**
2135 kumaneko 2002 * ccs_write_domain_profile - Assign profile for specified domain.
2136 kumaneko 1052 *
2137     * @head: Pointer to "struct ccs_io_buffer".
2138     *
2139     * Returns 0 on success, -EINVAL otherwise.
2140     *
2141     * This is equivalent to doing
2142 kumaneko 1054 *
2143     * ( echo "select " $domainname; echo "use_profile " $profile ) |
2144 kumaneko 1057 * /usr/lib/ccs/loadpolicy -d
2145 kumaneko 1052 */
2146 kumaneko 2002 static int ccs_write_domain_profile(struct ccs_io_buffer *head)
2147 kumaneko 461 {
2148     char *data = head->write_buf;
2149     char *cp = strchr(data, ' ');
2150 kumaneko 2282 struct ccs_domain_info *domain;
2151 kumaneko 461 unsigned int profile;
2152 kumaneko 1052 if (!cp)
2153     return -EINVAL;
2154 kumaneko 461 *cp = '\0';
2155 kumaneko 1052 domain = ccs_find_domain(cp + 1);
2156 kumaneko 461 profile = simple_strtoul(data, NULL, 10);
2157 kumaneko 1052 if (domain && profile < MAX_PROFILES
2158 kumaneko 2040 && (ccs_profile_ptr[profile] || !ccs_policy_loaded))
2159 kumaneko 1052 domain->profile = (u8) profile;
2160     ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);
2161 kumaneko 461 return 0;
2162     }
2163    
2164 kumaneko 1052 /**
2165 kumaneko 2002 * ccs_read_domain_profile - Read only domainname and profile.
2166 kumaneko 1052 *
2167     * @head: Pointer to "struct ccs_io_buffer".
2168     *
2169     * Returns list of profile number and domainname pairs.
2170     *
2171     * This is equivalent to doing
2172 kumaneko 1054 *
2173     * grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
2174 kumaneko 1057 * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
2175     * domainname = $0; } else if ( $1 == "use_profile" ) {
2176     * print $2 " " domainname; domainname = ""; } } ; '
2177 kumaneko 1052 */
2178 kumaneko 2002 static int ccs_read_domain_profile(struct ccs_io_buffer *head)
2179 kumaneko 111 {
2180 kumaneko 722 struct list1_head *pos;
2181 kumaneko 1052 if (head->read_eof)
2182     return 0;
2183 kumaneko 2002 list1_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
2184 kumaneko 2282 struct ccs_domain_info *domain;
2185     domain = list1_entry(pos, struct ccs_domain_info, list);
2186 kumaneko 1052 if (domain->is_deleted)
2187     continue;
2188     if (!ccs_io_printf(head, "%u %s\n", domain->profile,
2189     domain->domainname->name))
2190     return 0;
2191 kumaneko 111 }
2192 kumaneko 1006 head->read_eof = true;
2193 kumaneko 111 return 0;
2194     }
2195    
2196 kumaneko 1052 /**
2197 kumaneko 2002 * ccs_write_pid: Specify PID to obtain domainname.
2198 kumaneko 1052 *
2199     * @head: Pointer to "struct ccs_io_buffer".
2200     *
2201     * Returns 0.
2202     */
2203 kumaneko 2002 static int ccs_write_pid(struct ccs_io_buffer *head)
2204 kumaneko 111 {
2205 kumaneko 1006 head->read_eof = false;
2206 kumaneko 111 return 0;
2207     }
2208    
2209 kumaneko 1052 /**
2210 kumaneko 2002 * ccs_read_pid - Read information of a process.
2211 kumaneko 1052 *
2212     * @head: Pointer to "struct ccs_io_buffer".
2213     *
2214 kumaneko 1705 * Returns the domainname which the specified PID is in or
2215     * process information of the specified PID on success,
2216 kumaneko 1052 * empty string otherwise.
2217     */
2218 kumaneko 2002 static int ccs_read_pid(struct ccs_io_buffer *head)
2219 kumaneko 111 {
2220 kumaneko 1705 char *buf = head->write_buf;
2221     bool task_info = false;
2222     unsigned int pid;
2223     struct task_struct *p;
2224 kumaneko 2282 struct ccs_domain_info *domain = NULL;
2225     u32 ccs_flags = 0;
2226 kumaneko 1810 /* Accessing write_buf is safe because head->io_sem is held. */
2227 kumaneko 1809 if (!buf)
2228     goto done; /* Do nothing if open(O_RDONLY). */
2229 kumaneko 1705 if (head->read_avail || head->read_eof)
2230     goto done;
2231     head->read_eof = true;
2232 kumaneko 2002 if (ccs_str_starts(&buf, "info "))
2233 kumaneko 1705 task_info = true;
2234     pid = (unsigned int) simple_strtoul(buf, NULL, 10);
2235     /***** CRITICAL SECTION START *****/
2236     read_lock(&tasklist_lock);
2237     p = find_task_by_pid(pid);
2238     if (p) {
2239 kumaneko 2282 domain = ccs_task_domain(p);
2240     ccs_flags = p->ccs_flags;
2241 kumaneko 111 }
2242 kumaneko 1705 read_unlock(&tasklist_lock);
2243     /***** CRITICAL SECTION END *****/
2244     if (!domain)
2245     goto done;
2246     if (!task_info)
2247     ccs_io_printf(head, "%u %u %s", pid, domain->profile,
2248     domain->domainname->name);
2249     else
2250     ccs_io_printf(head, "%u manager=%s execute_handler=%s "
2251     "state[0]=%u state[1]=%u state[2]=%u", pid,
2252 kumaneko 2282 ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?
2253 kumaneko 1705 "yes" : "no",
2254 kumaneko 2282 ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?
2255 kumaneko 1705 "yes" : "no",
2256 kumaneko 2282 (u8) (ccs_flags >> 24),
2257     (u8) (ccs_flags >> 16),
2258     (u8) (ccs_flags >> 8));
2259 kumaneko 1705 done:
2260 kumaneko 111 return 0;
2261     }
2262    
2263     #ifdef CONFIG_TOMOYO
2264    
2265 kumaneko 1052 /**
2266 kumaneko 2002 * ccs_write_exception_policy - Write exception policy.
2267 kumaneko 1052 *
2268     * @head: Pointer to "struct ccs_io_buffer".
2269     *
2270     * Returns 0 on success, negative value otherwise.
2271     */
2272 kumaneko 2002 static int ccs_write_exception_policy(struct ccs_io_buffer *head)
2273 kumaneko 111 {
2274     char *data = head->write_buf;
2275 kumaneko