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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/ccs-patch/security/ccsecurity/policy_io.c revision 2932 by kumaneko, Fri Aug 21 08:26:08 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 4978 by kumaneko, Wed May 11 03:31:29 2011 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2011  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-pre   2009/08/08   * Version: 1.8.1+   2011/05/11
7     */
8    
9    #include "internal.h"
10    
11    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
12    
13    /**
14     * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
15   *   *
16   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * @wq:        The waitqueue to wait on.
17   * See README.ccs for ChangeLog.   * @condition: A C expression for the event to wait for.
18     * @ret:       Timeout, in jiffies.
19     *
20     * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
21     * signal, and the remaining jiffies otherwise if the condition evaluated to
22     * true before the timeout elapsed.
23     *
24     * This is for compatibility with older kernels.
25     */
26    #define __wait_event_interruptible_timeout(wq, condition, ret)          \
27    do {                                                                    \
28            wait_queue_t __wait;                                            \
29            init_waitqueue_entry(&__wait, current);                         \
30                                                                            \
31            add_wait_queue(&wq, &__wait);                                   \
32            for (;;) {                                                      \
33                    set_current_state(TASK_INTERRUPTIBLE);                  \
34                    if (condition)                                          \
35                            break;                                          \
36                    if (!signal_pending(current)) {                         \
37                            ret = schedule_timeout(ret);                    \
38                            if (!ret)                                       \
39                                    break;                                  \
40                            continue;                                       \
41                    }                                                       \
42                    ret = -ERESTARTSYS;                                     \
43                    break;                                                  \
44            }                                                               \
45            current->state = TASK_RUNNING;                                  \
46            remove_wait_queue(&wq, &__wait);                                \
47    } while (0)
48    
49    /**
50     * wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
51     *
52     * @wq:        The waitqueue to wait on.
53     * @condition: A C expression for the event to wait for.
54     * @timeout:   Timeout, in jiffies.
55     *
56     * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
57     * signal, and the remaining jiffies otherwise if the condition evaluated to
58     * true before the timeout elapsed.
59     *
60     * This is for compatibility with older kernels.
61     */
62    #define wait_event_interruptible_timeout(wq, condition, timeout)        \
63    ({                                                                      \
64            long __ret = timeout;                                           \
65            if (!(condition))                                               \
66                    __wait_event_interruptible_timeout(wq, condition, __ret); \
67            __ret;                                                          \
68    })
69    
70    #endif
71    
72    /**
73     * list_for_each_cookie - iterate over a list with cookie.
74   *   *
75     * @pos:  Pointer to "struct list_head".
76     * @head: Pointer to "struct list_head".
77   */   */
78    #define list_for_each_cookie(pos, head)                                 \
79            for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \
80                 pos != (head); pos = srcu_dereference(pos->next, &ccs_ss))
81    
 #include "internal.h"  
82    
83  /* Lock for protecting ccs_profile->comment  */  /* Profile version. Currently only 20100903 is defined. */
84  static DEFINE_SPINLOCK(ccs_profile_comment_lock);  static unsigned int ccs_profile_version;
85    
86  /* String table for functionality that takes 2 modes. */  /* Profile table. Memory is allocated as needed. */
87  static const char *ccs_mode_2[2] = {  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
88          "disabled", "enabled"  
89    /* String table for operation mode. */
90    const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
91            [CCS_CONFIG_DISABLED]   = "disabled",
92            [CCS_CONFIG_LEARNING]   = "learning",
93            [CCS_CONFIG_PERMISSIVE] = "permissive",
94            [CCS_CONFIG_ENFORCING]  = "enforcing"
95  };  };
96    
97  static const char *ccs_mode_4[4] = {  /* String table for /proc/ccs/profile interface. */
98          "disabled", "learning", "permissive", "enforcing"  const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
99                                        + CCS_MAX_MAC_CATEGORY_INDEX] = {
100            /* CONFIG::file group */
101            [CCS_MAC_FILE_EXECUTE]    = "execute",
102            [CCS_MAC_FILE_OPEN]       = "open",
103            [CCS_MAC_FILE_CREATE]     = "create",
104            [CCS_MAC_FILE_UNLINK]     = "unlink",
105            [CCS_MAC_FILE_GETATTR]    = "getattr",
106            [CCS_MAC_FILE_MKDIR]      = "mkdir",
107            [CCS_MAC_FILE_RMDIR]      = "rmdir",
108            [CCS_MAC_FILE_MKFIFO]     = "mkfifo",
109            [CCS_MAC_FILE_MKSOCK]     = "mksock",
110            [CCS_MAC_FILE_TRUNCATE]   = "truncate",
111            [CCS_MAC_FILE_SYMLINK]    = "symlink",
112            [CCS_MAC_FILE_MKBLOCK]    = "mkblock",
113            [CCS_MAC_FILE_MKCHAR]     = "mkchar",
114            [CCS_MAC_FILE_LINK]       = "link",
115            [CCS_MAC_FILE_RENAME]     = "rename",
116            [CCS_MAC_FILE_CHMOD]      = "chmod",
117            [CCS_MAC_FILE_CHOWN]      = "chown",
118            [CCS_MAC_FILE_CHGRP]      = "chgrp",
119            [CCS_MAC_FILE_IOCTL]      = "ioctl",
120            [CCS_MAC_FILE_CHROOT]     = "chroot",
121            [CCS_MAC_FILE_MOUNT]      = "mount",
122            [CCS_MAC_FILE_UMOUNT]     = "unmount",
123            [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
124            /* CONFIG::misc group */
125            [CCS_MAC_ENVIRON] = "env",
126            /* CONFIG::network group */
127            [CCS_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
128            [CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
129            [CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
130            [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = "inet_stream_accept",
131            [CCS_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
132            [CCS_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
133            [CCS_MAC_NETWORK_INET_DGRAM_RECV]        = "inet_dgram_recv",
134            [CCS_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
135            [CCS_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
136            [CCS_MAC_NETWORK_INET_RAW_RECV]          = "inet_raw_recv",
137            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
138            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
139            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
140            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = "unix_stream_accept",
141            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
142            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
143            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = "unix_dgram_recv",
144            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
145            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
146            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
147            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = "unix_seqpacket_accept",
148            /* CONFIG::ipc group */
149            [CCS_MAC_SIGNAL] = "signal",
150            /* CONFIG::capability group */
151            [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = "use_route",
152            [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
153            [CCS_MAC_CAPABILITY_SYS_REBOOT]        = "SYS_REBOOT",
154            [CCS_MAC_CAPABILITY_SYS_VHANGUP]       = "SYS_VHANGUP",
155            [CCS_MAC_CAPABILITY_SYS_SETTIME]       = "SYS_TIME",
156            [CCS_MAC_CAPABILITY_SYS_NICE]          = "SYS_NICE",
157            [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = "SYS_SETHOSTNAME",
158            [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
159            [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = "SYS_KEXEC_LOAD",
160            [CCS_MAC_CAPABILITY_SYS_PTRACE]        = "SYS_PTRACE",
161            /* CONFIG group */
162            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE]       = "file",
163            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK]    = "network",
164            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC]       = "misc",
165            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC]        = "ipc",
166            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
167  };  };
168    
169  static bool ccs_mac_keywords_used[CCS_MAX_MAC_INDEX +  /* String table for path operation. */
170                                    CCS_MAX_CAPABILITY_INDEX];  const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
171            [CCS_TYPE_EXECUTE]    = "execute",
172            [CCS_TYPE_READ]       = "read",
173            [CCS_TYPE_WRITE]      = "write",
174            [CCS_TYPE_APPEND]     = "append",
175            [CCS_TYPE_UNLINK]     = "unlink",
176            [CCS_TYPE_GETATTR]    = "getattr",
177            [CCS_TYPE_RMDIR]      = "rmdir",
178            [CCS_TYPE_TRUNCATE]   = "truncate",
179            [CCS_TYPE_SYMLINK]    = "symlink",
180            [CCS_TYPE_CHROOT]     = "chroot",
181            [CCS_TYPE_UMOUNT]     = "unmount",
182    };
183    
184  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  /* String table for categories. */
185                                      CCS_MAX_CAPABILITY_INDEX] = {  static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
186          [CCS_MAC_EXECUTE]    = "execute",          [CCS_MAC_CATEGORY_FILE]       = "file",
187          [CCS_MAC_OPEN]       = "open",          [CCS_MAC_CATEGORY_NETWORK]    = "network",
188          [CCS_MAC_CREATE]     = "create",          [CCS_MAC_CATEGORY_MISC]       = "misc",
189          [CCS_MAC_UNLINK]     = "unlink",          [CCS_MAC_CATEGORY_IPC]        = "ipc",
190          [CCS_MAC_MKDIR]      = "mkdir",          [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
         [CCS_MAC_RMDIR]      = "rmdir",  
         [CCS_MAC_MKFIFO]     = "mkfifo",  
         [CCS_MAC_MKSOCK]     = "mksock",  
         [CCS_MAC_TRUNCATE]   = "truncate",  
         [CCS_MAC_SYMLINK]    = "symlink",  
         [CCS_MAC_REWRITE]    = "rewrite",  
         [CCS_MAC_MKBLOCK]    = "mkblock",  
         [CCS_MAC_MKCHAR]     = "mkchar",  
         [CCS_MAC_LINK]       = "link",  
         [CCS_MAC_RENAME]     = "rename",  
         [CCS_MAC_CHMOD]      = "chmod",  
         [CCS_MAC_CHOWN]      = "chown",  
         [CCS_MAC_CHGRP]      = "chgrp",  
         [CCS_MAC_IOCTL]      = "ioctl",  
         [CCS_MAC_CHROOT]     = "chroot",  
         [CCS_MAC_MOUNT]      = "mount",  
         [CCS_MAC_UMOUNT]     = "umount",  
         [CCS_MAC_PIVOT_ROOT] = "pivot_root",  
         [CCS_MAC_ENVIRON]    = "env",  
         [CCS_MAC_NETWORK]    = "network",  
         [CCS_MAC_SIGNAL]     = "signal",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET] = "use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]   = "use_inet_ip",  
         [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]      = "use_route",  
         [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]     = "use_packet",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]             = "SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]            = "SYS_UMOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]            = "SYS_REBOOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]            = "SYS_CHROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]              = "SYS_KILL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]           = "SYS_VHANGUP",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]           = "SYS_TIME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]              = "SYS_NICE",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]       = "SYS_SETHOSTNAME",  
         [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]     = "use_kernel_module",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]           = "create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]      = "create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]       = "create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]    = "create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]              = "SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]           = "SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]            = "SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]            = "SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]             = "SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]             = "SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]             = "SYS_IOCTL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]        = "SYS_KEXEC_LOAD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]        = "SYS_PIVOT_ROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]            = "SYS_PTRACE",  
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]         = "conceal_mount"  
191  };  };
192    
193  /* Table for profile. */  /* String table for conditions. */
194  static struct {  const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
195          const char *keyword;          [CCS_TASK_UID]             = "task.uid",
196          unsigned int current_value;          [CCS_TASK_EUID]            = "task.euid",
197          const unsigned int max_value;          [CCS_TASK_SUID]            = "task.suid",
198  } ccs_control_array[CCS_MAX_CONTROL_INDEX] = {          [CCS_TASK_FSUID]           = "task.fsuid",
199          [CCS_AUTOLEARN_EXEC_REALPATH] = { "AUTOLEARN_EXEC_REALPATH", 0, 1 },          [CCS_TASK_GID]             = "task.gid",
200          [CCS_AUTOLEARN_EXEC_ARGV0] = { "AUTOLEARN_EXEC_ARGV0", 0, 1 },          [CCS_TASK_EGID]            = "task.egid",
201          [CCS_MAX_ACCEPT_ENTRY]          [CCS_TASK_SGID]            = "task.sgid",
202          = { "MAX_ACCEPT_ENTRY", CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY, INT_MAX },          [CCS_TASK_FSGID]           = "task.fsgid",
203  #ifdef CONFIG_CCSECURITY_AUDIT          [CCS_TASK_PID]             = "task.pid",
204          [CCS_MAX_GRANT_LOG]          [CCS_TASK_PPID]            = "task.ppid",
205          = { "MAX_GRANT_LOG", CONFIG_CCSECURITY_MAX_GRANT_LOG, INT_MAX },          [CCS_EXEC_ARGC]            = "exec.argc",
206          [CCS_MAX_REJECT_LOG]          [CCS_EXEC_ENVC]            = "exec.envc",
207          = { "MAX_REJECT_LOG", CONFIG_CCSECURITY_MAX_REJECT_LOG, INT_MAX },          [CCS_TYPE_IS_SOCKET]       = "socket",
208  #endif          [CCS_TYPE_IS_SYMLINK]      = "symlink",
209          [CCS_VERBOSE] = { "PRINT_VIOLATION",      1, 1 },          [CCS_TYPE_IS_FILE]         = "file",
210          [CCS_SLEEP_PERIOD] = { "SLEEP_PERIOD", 0, 3000 }, /* in 0.1 second */          [CCS_TYPE_IS_BLOCK_DEV]    = "block",
211            [CCS_TYPE_IS_DIRECTORY]    = "directory",
212            [CCS_TYPE_IS_CHAR_DEV]     = "char",
213            [CCS_TYPE_IS_FIFO]         = "fifo",
214            [CCS_MODE_SETUID]          = "setuid",
215            [CCS_MODE_SETGID]          = "setgid",
216            [CCS_MODE_STICKY]          = "sticky",
217            [CCS_MODE_OWNER_READ]      = "owner_read",
218            [CCS_MODE_OWNER_WRITE]     = "owner_write",
219            [CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
220            [CCS_MODE_GROUP_READ]      = "group_read",
221            [CCS_MODE_GROUP_WRITE]     = "group_write",
222            [CCS_MODE_GROUP_EXECUTE]   = "group_execute",
223            [CCS_MODE_OTHERS_READ]     = "others_read",
224            [CCS_MODE_OTHERS_WRITE]    = "others_write",
225            [CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
226            [CCS_TASK_TYPE]            = "task.type",
227            [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
228            [CCS_EXEC_REALPATH]        = "exec.realpath",
229            [CCS_SYMLINK_TARGET]       = "symlink.target",
230            [CCS_PATH1_UID]            = "path1.uid",
231            [CCS_PATH1_GID]            = "path1.gid",
232            [CCS_PATH1_INO]            = "path1.ino",
233            [CCS_PATH1_MAJOR]          = "path1.major",
234            [CCS_PATH1_MINOR]          = "path1.minor",
235            [CCS_PATH1_PERM]           = "path1.perm",
236            [CCS_PATH1_TYPE]           = "path1.type",
237            [CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
238            [CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
239            [CCS_PATH2_UID]            = "path2.uid",
240            [CCS_PATH2_GID]            = "path2.gid",
241            [CCS_PATH2_INO]            = "path2.ino",
242            [CCS_PATH2_MAJOR]          = "path2.major",
243            [CCS_PATH2_MINOR]          = "path2.minor",
244            [CCS_PATH2_PERM]           = "path2.perm",
245            [CCS_PATH2_TYPE]           = "path2.type",
246            [CCS_PATH2_DEV_MAJOR]      = "path2.dev_major",
247            [CCS_PATH2_DEV_MINOR]      = "path2.dev_minor",
248            [CCS_PATH1_PARENT_UID]     = "path1.parent.uid",
249            [CCS_PATH1_PARENT_GID]     = "path1.parent.gid",
250            [CCS_PATH1_PARENT_INO]     = "path1.parent.ino",
251            [CCS_PATH1_PARENT_PERM]    = "path1.parent.perm",
252            [CCS_PATH2_PARENT_UID]     = "path2.parent.uid",
253            [CCS_PATH2_PARENT_GID]     = "path2.parent.gid",
254            [CCS_PATH2_PARENT_INO]     = "path2.parent.ino",
255            [CCS_PATH2_PARENT_PERM]    = "path2.parent.perm",
256    };
257    
258    /* String table for PREFERENCE keyword. */
259    static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
260            [CCS_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
261            [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
262            [CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
263  };  };
264    
265  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
266  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
267    
268  /**  /**
269   * ccs_cap2keyword - Convert capability operation to capability name.   * ccs_yesno - Return "yes" or "no".
270   *   *
271   * @operation: The capability index.   * @value: Bool value.
272   *   *
273   * Returns the name of the specified capability's name.   * Returns "yes" if @value is not 0, "no" otherwise.
274   */   */
275  const char *ccs_cap2keyword(const u8 operation)  const char *ccs_yesno(const unsigned int value)
276  {  {
277          return operation < CCS_MAX_CAPABILITY_INDEX          return value ? "yes" : "no";
                 ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] : NULL;  
278  }  }
279    
280    /* Prototype for ccs_addprintf(). */
281    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
282            __attribute__ ((format(printf, 3, 4)));
283    
284  /**  /**
285   * ccs_quiet_setup - Set CCS_VERBOSE=0 by default.   * ccs_addprintf - strncat()-like-snprintf().
286   *   *
287   * @str: Unused.   * @buffer: Buffer to write to. Must be '\0'-terminated.
288     * @len:    Size of @buffer.
289     * @fmt:    The printf()'s format string, followed by parameters.
290   *   *
291   * Returns 0.   * Returns nothing.
292   */   */
293  static int __init ccs_quiet_setup(char *str)  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
294  {  {
295          ccs_control_array[CCS_VERBOSE].current_value = 0;          va_list args;
296          return 0;          const int pos = strlen(buffer);
297            va_start(args, fmt);
298            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
299            va_end(args);
300    }
301    
302    /**
303     * ccs_flush - Flush queued string to userspace's buffer.
304     *
305     * @head: Pointer to "struct ccs_io_buffer".
306     *
307     * Returns true if all data was flushed, false otherwise.
308     */
309    static bool ccs_flush(struct ccs_io_buffer *head)
310    {
311            while (head->r.w_pos) {
312                    const char *w = head->r.w[0];
313                    size_t len = strlen(w);
314                    if (len) {
315                            if (len > head->read_user_buf_avail)
316                                    len = head->read_user_buf_avail;
317                            if (!len)
318                                    return false;
319                            if (copy_to_user(head->read_user_buf, w, len))
320                                    return false;
321                            head->read_user_buf_avail -= len;
322                            head->read_user_buf += len;
323                            w += len;
324                    }
325                    head->r.w[0] = w;
326                    if (*w)
327                            return false;
328                    /* Add '\0' for audit logs and query. */
329                    if (head->poll) {
330                            if (!head->read_user_buf_avail ||
331                                copy_to_user(head->read_user_buf, "", 1))
332                                    return false;
333                            head->read_user_buf_avail--;
334                            head->read_user_buf++;
335                    }
336                    head->r.w_pos--;
337                    for (len = 0; len < head->r.w_pos; len++)
338                            head->r.w[len] = head->r.w[len + 1];
339            }
340            head->r.avail = 0;
341            return true;
342  }  }
343    
344  __setup("CCS_QUIET", ccs_quiet_setup);  /**
345     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
346     *
347     * @head:   Pointer to "struct ccs_io_buffer".
348     * @string: String to print.
349     *
350     * Returns nothing.
351     *
352     * Note that @string has to be kept valid until @head is kfree()d.
353     * This means that char[] allocated on stack memory cannot be passed to
354     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
355     */
356    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
357    {
358            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
359                    head->r.w[head->r.w_pos++] = string;
360                    ccs_flush(head);
361            } else
362                    printk(KERN_WARNING "Too many words in a line.\n");
363    }
364    
365    /* Prototype for ccs_io_printf(). */
366    static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
367            __attribute__ ((format(printf, 2, 3)));
368    
369  /**  /**
370   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
371   *   *
372   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
373   * @fmt:  The printf()'s format string, followed by parameters.   * @fmt:  The printf()'s format string, followed by parameters.
374   *   *
375   * Returns true on success, false otherwise.   * Returns nothing.
  *  
  * The snprintf() will truncate, but ccs_io_printf() won't.  
376   */   */
377  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
378  {  {
379          va_list args;          va_list args;
380          int len;          size_t len;
381          int pos = head->read_avail;          size_t pos = head->r.avail;
382          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
383          if (size <= 0)          if (size <= 0)
384                  return false;                  return;
385          va_start(args, fmt);          va_start(args, fmt);
386          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
387          va_end(args);          va_end(args);
388          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
389                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
390          head->read_avail += len;                  return;
391          return true;          }
392            head->r.avail += len;
393            ccs_set_string(head, head->read_buf + pos);
394  }  }
395    
396  /**  /**
397   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
398     *
399     * @head: Pointer to "struct ccs_io_buffer".
400     *
401     * Returns nothing.
402     */
403    static void ccs_set_space(struct ccs_io_buffer *head)
404    {
405            ccs_set_string(head, " ");
406    }
407    
408    /**
409     * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
410     *
411     * @head: Pointer to "struct ccs_io_buffer".
412     *
413     * Returns nothing.
414     */
415    static bool ccs_set_lf(struct ccs_io_buffer *head)
416    {
417            ccs_set_string(head, "\n");
418            return !head->r.w_pos;
419    }
420    
421    /**
422     * ccs_set_slash - Put a shash to "struct ccs_io_buffer" structure.
423     *
424     * @head: Pointer to "struct ccs_io_buffer".
425     *
426     * Returns nothing.
427     */
428    static void ccs_set_slash(struct ccs_io_buffer *head)
429    {
430            ccs_set_string(head, "/");
431    }
432    
433    /**
434     * ccs_assign_profile - Create a new profile.
435   *   *
436   * @profile: Profile number to create.   * @profile: Profile number to create.
437   *   *
438   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
439   */   */
440  static struct ccs_profile *ccs_find_or_assign_new_profile(const unsigned int  static struct ccs_profile *ccs_assign_profile(const unsigned int profile)
                                                           profile)  
441  {  {
442          struct ccs_profile *ptr;          struct ccs_profile *ptr;
443          struct ccs_profile *entry;          struct ccs_profile *entry;
         int i;  
444          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
445                  return NULL;                  return NULL;
446          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
447          if (ptr)          if (ptr)
448                  return ptr;                  return ptr;
449          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
450          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
451                    goto out;
452          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
453          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
454                  ptr = entry;                  ptr = entry;
455                  for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++)                  ptr->default_config = CCS_CONFIG_DISABLED |
456                          ptr->value[i] = ccs_control_array[i].current_value;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
457                    memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
458                           sizeof(ptr->config));
459                    ptr->pref[CCS_PREF_MAX_AUDIT_LOG] =
460                            CONFIG_CCSECURITY_MAX_AUDIT_LOG;
461                    ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
462                            CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
463                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
464                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
465                  entry = NULL;                  entry = NULL;
466          }          }
467          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
468    out:
469          kfree(entry);          kfree(entry);
470          return ptr;          return ptr;
471  }  }
472    
473  /**  /**
474     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
475     *
476     * Returns nothing.
477     */
478    static void ccs_check_profile(void)
479    {
480            struct ccs_domain_info *domain;
481            const int idx = ccs_read_lock();
482            ccs_policy_loaded = true;
483            list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
484                    const u8 profile = domain->profile;
485                    if (ccs_profile_ptr[profile])
486                            continue;
487                    printk(KERN_ERR "Profile %u must be defined before using it.\n",
488                           profile);
489                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
490                           "for more information.\n");
491                    panic("Profile %u (used by '%s') not defined.\n",
492                          profile, domain->domainname->name);
493            }
494            ccs_read_unlock(idx);
495            if (ccs_profile_version != 20100903) {
496                    printk(KERN_ERR "Userland tools must be installed for "
497                           "TOMOYO 1.8, and policy must be initialized.\n");
498                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
499                           "for more information.\n");
500                    panic("Profile version %u is not supported.\n",
501                          ccs_profile_version);
502            }
503            printk(KERN_INFO "CCSecurity: 1.8.1+   2011/05/11\n");
504            printk(KERN_INFO "Mandatory Access Control activated.\n");
505    }
506    
507    /**
508     * ccs_profile - Find a profile.
509     *
510     * @profile: Profile number to find.
511     *
512     * Returns pointer to "struct ccs_profile".
513     */
514    struct ccs_profile *ccs_profile(const u8 profile)
515    {
516            static struct ccs_profile ccs_null_profile;
517            struct ccs_profile *ptr = ccs_profile_ptr[profile];
518            if (!ptr)
519                    ptr = &ccs_null_profile;
520            return ptr;
521    }
522    
523    /**
524     * ccs_find_yesno - Find values for specified keyword.
525     *
526     * @string: String to check.
527     * @find:   Name of keyword.
528     *
529     * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
530     */
531    static s8 ccs_find_yesno(const char *string, const char *find)
532    {
533            const char *cp = strstr(string, find);
534            if (cp) {
535                    cp += strlen(find);
536                    if (!strncmp(cp, "=yes", 4))
537                            return 1;
538                    else if (!strncmp(cp, "=no", 3))
539                            return 0;
540            }
541            return -1;
542    }
543    
544    /**
545     * ccs_set_uint - Set value for specified preference.
546     *
547     * @i:      Pointer to "unsigned int".
548     * @string: String to check.
549     * @find:   Name of keyword.
550     *
551     * Returns nothing.
552     */
553    static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
554    {
555            const char *cp = strstr(string, find);
556            if (cp)
557                    sscanf(cp + strlen(find), "=%u", i);
558    }
559    
560    /**
561     * ccs_set_mode - Set mode for specified profile.
562     *
563     * @name:    Name of functionality.
564     * @value:   Mode for @name.
565     * @profile: Pointer to "struct ccs_profile".
566     *
567     * Returns 0 on success, negative value otherwise.
568     */
569    static int ccs_set_mode(char *name, const char *value,
570                            struct ccs_profile *profile)
571    {
572            u8 i;
573            u8 config;
574            if (!strcmp(name, "CONFIG")) {
575                    i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
576                    config = profile->default_config;
577            } else if (ccs_str_starts(&name, "CONFIG::")) {
578                    config = 0;
579                    for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
580                         i++) {
581                            int len = 0;
582                            if (i < CCS_MAX_MAC_INDEX) {
583                                    const u8 c = ccs_index2category[i];
584                                    const char *category =
585                                            ccs_category_keywords[c];
586                                    len = strlen(category);
587                                    if (strncmp(name, category, len) ||
588                                        name[len++] != ':' || name[len++] != ':')
589                                            continue;
590                            }
591                            if (strcmp(name + len, ccs_mac_keywords[i]))
592                                    continue;
593                            config = profile->config[i];
594                            break;
595                    }
596                    if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
597                            return -EINVAL;
598            } else {
599                    return -EINVAL;
600            }
601            if (strstr(value, "use_default")) {
602                    config = CCS_CONFIG_USE_DEFAULT;
603            } else {
604                    u8 mode;
605                    for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
606                            if (strstr(value, ccs_mode[mode]))
607                                    /*
608                                     * Update lower 3 bits in order to distinguish
609                                     * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
610                                     */
611                                    config = (config & ~7) | mode;
612                    if (config != CCS_CONFIG_USE_DEFAULT) {
613                            switch (ccs_find_yesno(value, "grant_log")) {
614                            case 1:
615                                    config |= CCS_CONFIG_WANT_GRANT_LOG;
616                                    break;
617                            case 0:
618                                    config &= ~CCS_CONFIG_WANT_GRANT_LOG;
619                                    break;
620                            }
621                            switch (ccs_find_yesno(value, "reject_log")) {
622                            case 1:
623                                    config |= CCS_CONFIG_WANT_REJECT_LOG;
624                                    break;
625                            case 0:
626                                    config &= ~CCS_CONFIG_WANT_REJECT_LOG;
627                                    break;
628                            }
629                    }
630            }
631            if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
632                    profile->config[i] = config;
633            else if (config != CCS_CONFIG_USE_DEFAULT)
634                    profile->default_config = config;
635            return 0;
636    }
637    
638    /**
639   * ccs_write_profile - Write profile table.   * ccs_write_profile - Write profile table.
640   *   *
641   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 215  static int ccs_write_profile(struct ccs_ Line 646  static int ccs_write_profile(struct ccs_
646  {  {
647          char *data = head->write_buf;          char *data = head->write_buf;
648          unsigned int i;          unsigned int i;
         unsigned int value;  
         int index = -1;  
         int mode;  
649          char *cp;          char *cp;
650          struct ccs_profile *ccs_profile;          struct ccs_profile *profile;
651            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
652                    return 0;
653          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
654          if (data != cp) {          if (*cp != '-')
655                  if (*cp != '-')                  return -EINVAL;
656                          return -EINVAL;          data = cp + 1;
657                  data = cp + 1;          profile = ccs_assign_profile(i);
658          }          if (!profile)
         ccs_profile = ccs_find_or_assign_new_profile(i);  
         if (!ccs_profile)  
659                  return -EINVAL;                  return -EINVAL;
660          cp = strchr(data, '=');          cp = strchr(data, '=');
661          if (!cp)          if (!cp)
662                  return -EINVAL;                  return -EINVAL;
663          *cp++ = '\0';          *cp++ = '\0';
664          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
665                    static DEFINE_SPINLOCK(lock);
666                  const struct ccs_path_info *new_comment = ccs_get_name(cp);                  const struct ccs_path_info *new_comment = ccs_get_name(cp);
667                  const struct ccs_path_info *old_comment;                  const struct ccs_path_info *old_comment;
668                  /* Protect reader from ccs_put_name(). */                  if (!new_comment)
669                  spin_lock(&ccs_profile_comment_lock);                          return -ENOMEM;
670                  old_comment = ccs_profile->comment;                  spin_lock(&lock);
671                  ccs_profile->comment = new_comment;                  old_comment = profile->comment;
672                  spin_unlock(&ccs_profile_comment_lock);                  profile->comment = new_comment;
673                    spin_unlock(&lock);
674                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
675                  return 0;                  return 0;
676          }          }
677          if (!ccs_str_starts(&data, "MAC::"))          if (!strcmp(data, "PREFERENCE")) {
678                  goto not_mac;                  for (i = 0; i < CCS_MAX_PREF; i++)
679          if (ccs_str_starts(&data, CCS_KEYWORD_CAPABILITY))                          ccs_set_uint(&profile->pref[i], cp,
680                  for (i = 0; i < CCS_MAX_CAPABILITY_INDEX; i++) {                                       ccs_pref_keywords[i]);
                         if (strcmp(data,  
                                    ccs_mac_keywords[CCS_MAX_MAC_INDEX + i]))  
                                 continue;  
                         index = CCS_MAX_MAC_INDEX + i;  
                         break;  
                 }  
         else  
                 for (i = 0; i < CCS_MAX_MAC_INDEX; i++) {  
                         if (strcmp(data, ccs_mac_keywords[i]))  
                                 continue;  
                         index = i;  
                         break;  
                 }  
         if (index < 0)  
                 return -EINVAL;  
         ccs_mac_keywords_used[index] = 1;  
         ccs_profile->no_grant_log[index] = !!strstr(cp, "no_grant_log");  
         ccs_profile->no_reject_log[index] = !!strstr(cp, "no_reject_log");  
         for (mode = 0; mode < 4; mode++)  
                 if (strstr(cp, ccs_mode_4[mode]))  
                         ccs_profile->mac_mode[index] = mode;  
         return 0;  
  not_mac:  
         for (i = 0; i < CCS_MAX_CONTROL_INDEX; i++) {  
                 if (strcmp(data, ccs_control_array[i].keyword))  
                         continue;  
                 if (sscanf(cp, "%u", &value) != 1) {  
                         int j;  
                         for (j = 0; j < 2; j++) {  
                                 if (strcmp(cp, ccs_mode_2[j]))  
                                         continue;  
                                 value = j;  
                                 break;  
                         }  
                         if (j == 2)  
                                 return -EINVAL;  
                 } else if (value > ccs_control_array[i].max_value) {  
                         value = ccs_control_array[i].max_value;  
                 }  
                 ccs_profile->value[i] = value;  
681                  return 0;                  return 0;
682          }          }
683          return -EINVAL;          return ccs_set_mode(data, cp, profile);
684  }  }
685    
686  static bool ccs_print_mac_mode(struct ccs_io_buffer *head, u8 index)  /**
687     * ccs_print_config - Print mode for specified functionality.
688     *
689     * @head:   Pointer to "struct ccs_io_buffer".
690     * @config: Mode for that functionality.
691     *
692     * Returns nothing.
693     *
694     * Caller prints functionality's name.
695     */
696    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
697  {  {
698          const int pos = head->read_avail;          ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
699          int i;                        ccs_mode[config & 3],
700          const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
701          for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX; i++) {                        ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
                 if (!ccs_mac_keywords_used[i])  
                         continue;  
                 if (!ccs_io_printf(head, "%u-MAC::%s%s=%s %s %s\n", index,  
                                    i >= CCS_MAX_MAC_INDEX ?  
                                    CCS_KEYWORD_CAPABILITY : "",  
                                    ccs_mac_keywords[i],  
                                    ccs_mode_4[ccs_profile->mac_mode[i]],  
                                    ccs_profile->no_grant_log[i] ?  
                                    "no_grant_log" : "",  
                                    ccs_profile->no_reject_log[i] ?  
                                    "no_reject_log" : ""))  
                         goto out;  
         }  
         return true;  
  out:  
         head->read_avail = pos;  
         return false;  
702  }  }
703    
704  /**  /**
# Line 323  static bool ccs_print_mac_mode(struct cc Line 706  static bool ccs_print_mac_mode(struct cc
706   *   *
707   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
708   *   *
709   * Returns 0.   * Returns nothing.
710   */   */
711  static int ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
712  {  {
713          static const int ccs_total = CCS_MAX_CONTROL_INDEX + 2;          u8 index;
714          int step;          const struct ccs_profile *profile;
715          if (head->read_eof)  next:
716                  return 0;          index = head->r.index;
717          for (step = head->read_step; step < CCS_MAX_PROFILES * ccs_total;          profile = ccs_profile_ptr[index];
718               step++) {          switch (head->r.step) {
719                  const u8 index = step / ccs_total;          case 0:
720                  u8 type = step % ccs_total;                  ccs_io_printf(head, "PROFILE_VERSION=%u\n", 20100903);
721                  const struct ccs_profile *ccs_profile = ccs_profile_ptr[index];                  head->r.step++;
722                  head->read_step = step;                  break;
723                  if (!ccs_profile)          case 1:
724                          continue;                  for ( ; head->r.index < CCS_MAX_PROFILES;
725                  if (!type) { /* Print profile' comment tag. */                        head->r.index++)
726                          bool done;                          if (ccs_profile_ptr[head->r.index])
                         spin_lock(&ccs_profile_comment_lock);  
                         done = ccs_io_printf(head, "%u-COMMENT=%s\n",  
                                              index, ccs_profile->comment ?  
                                              ccs_profile->comment->name : "");  
                         spin_unlock(&ccs_profile_comment_lock);  
                         if (!done)  
                                 break;  
                         continue;  
                 } else if (type == 1) {  
                         if (!ccs_print_mac_mode(head, index))  
727                                  break;                                  break;
728                          continue;                  if (head->r.index == CCS_MAX_PROFILES)
729                            return;
730                    head->r.step++;
731                    break;
732            case 2:
733                    {
734                            u8 i;
735                            const struct ccs_path_info *comment = profile->comment;
736                            ccs_io_printf(head, "%u-COMMENT=", index);
737                            ccs_set_string(head, comment ? comment->name : "");
738                            ccs_set_lf(head);
739                            ccs_io_printf(head, "%u-PREFERENCE={ ", index);
740                            for (i = 0; i < CCS_MAX_PREF; i++)
741                                    ccs_io_printf(head, "%s=%u ",
742                                                  ccs_pref_keywords[i],
743                                                  profile->pref[i]);
744                            ccs_set_string(head, "}\n");
745                            head->r.step++;
746                  }                  }
747                  type -= 2;                  break;
748            case 3:
749                  {                  {
750                          const unsigned int value = ccs_profile->value[type];                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
751                          const char *keyword = ccs_control_array[type].keyword;                          ccs_print_config(head, profile->default_config);
752                          if (ccs_control_array[type].max_value == 1) {                          head->r.bit = 0;
753                                  if (!ccs_io_printf(head, "%u-%s=%s\n", index,                          head->r.step++;
                                                    keyword, ccs_mode_2[value]))  
                                         break;  
                         } else {  
                                 if (!ccs_io_printf(head, "%u-%s=%u\n", index,  
                                                    keyword, value))  
                                         break;  
                         }  
754                  }                  }
755                    break;
756            case 4:
757                    for ( ; head->r.bit < CCS_MAX_MAC_INDEX
758                                  + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
759                            const u8 i = head->r.bit;
760                            const u8 config = profile->config[i];
761                            if (config == CCS_CONFIG_USE_DEFAULT)
762                                    continue;
763                            if (i < CCS_MAX_MAC_INDEX)
764                                    ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
765                                                  ccs_category_keywords
766                                                  [ccs_index2category[i]],
767                                                  ccs_mac_keywords[i]);
768                            else
769                                    ccs_io_printf(head, "%u-CONFIG::%s", index,
770                                                  ccs_mac_keywords[i]);
771                            ccs_print_config(head, config);
772                            head->r.bit++;
773                            break;
774                    }
775                    if (head->r.bit == CCS_MAX_MAC_INDEX
776                        + CCS_MAX_MAC_CATEGORY_INDEX) {
777                            head->r.index++;
778                            head->r.step = 1;
779                    }
780                    break;
781          }          }
782          if (step == CCS_MAX_PROFILES * ccs_total)          if (ccs_flush(head))
783                  head->read_eof = true;                  goto next;
         return 0;  
784  }  }
785    
786  /* The list for "struct ccs_policy_manager_entry". */  /**
787  LIST_HEAD(ccs_policy_manager_list);   * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
788     *
789     * @a: Pointer to "struct ccs_acl_head".
790     * @b: Pointer to "struct ccs_acl_head".
791     *
792     * Returns true if @a == @b, false otherwise.
793     */
794    static bool ccs_same_manager(const struct ccs_acl_head *a,
795                                 const struct ccs_acl_head *b)
796    {
797            return container_of(a, struct ccs_manager, head)->manager
798                    == container_of(b, struct ccs_manager, head)->manager;
799    }
800    
801  /**  /**
802   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 387  LIST_HEAD(ccs_policy_manager_list); Line 808  LIST_HEAD(ccs_policy_manager_list);
808   */   */
809  static int ccs_update_manager_entry(const char *manager, const bool is_delete)  static int ccs_update_manager_entry(const char *manager, const bool is_delete)
810  {  {
811          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
812          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
813          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
814                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
815                          return -EINVAL;                          return -EINVAL;
816                  e.is_domain = true;                  e.is_domain = true;
817          } else {          } else {
818                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
819                          return -EINVAL;                          return -EINVAL;
820          }          }
821          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
822          if (!e.manager)          if (!e.manager)
823                  return -ENOMEM;                  return error;
824          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
825                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
826          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager);
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (ptr->manager != e.manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {  
                 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
827          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
828          return error;          return error;
829  }  }
830    
831  /**  /**
832   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
833   *   *
834   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
835   *   *
836   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
837   */   */
838  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
839  {  {
840          char *data = head->write_buf;          char *data = head->write_buf;
841          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, "delete ");
842          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
843                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
844                  return 0;                  return 0;
# Line 442  static int ccs_write_manager_policy(stru Line 847  static int ccs_write_manager_policy(stru
847  }  }
848    
849  /**  /**
850   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
851   *   *
852   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
853   *   *
854   * Returns 0.   * Returns nothing.
855   *   *
856   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
857   */   */
858  static int ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
859  {  {
860          struct list_head *pos;          if (head->r.eof)
861          ccs_assert_read_lock();                  return;
862          if (head->read_eof)          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
863                  return 0;                  struct ccs_manager *ptr =
864          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {                          list_entry(head->r.acl, typeof(*ptr), head.list);
865                  struct ccs_policy_manager_entry *ptr;                  if (ptr->head.is_deleted)
                 ptr = list_entry(pos, struct ccs_policy_manager_entry, list);  
                 if (ptr->is_deleted)  
866                          continue;                          continue;
867                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
868                          return 0;                          return;
869                    ccs_set_string(head, ptr->manager->name);
870                    ccs_set_lf(head);
871          }          }
872          head->read_eof = true;          head->r.eof = true;
         return 0;  
873  }  }
874    
875  /**  /**
876   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
877   *   *
878   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
879   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
880   *   *
881   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
882   */   */
883  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
884  {  {
885          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
886          const char *exe;          const char *exe;
887          struct task_struct *task = current;          struct ccs_security *task = ccs_current_security();
888          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
889                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
890          bool found = false;          bool found = false;
         ccs_assert_read_lock();  
891          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
892                  return true;                  return true;
893          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
894                  return true;                  return true;
895          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
896                  return false;                  return false;
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (!ptr->is_deleted && ptr->is_domain  
                     && !ccs_pathcmp(domainname, ptr->manager)) {  
                         /* Set manager flag. */  
                         task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;  
                         return true;  
                 }  
         }  
897          exe = ccs_get_exe();          exe = ccs_get_exe();
898          if (!exe)          list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
899                  return false;                                   head.list, &ccs_ss) {
900          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
901                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
902                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
903                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
904                          /* Set manager flag. */                                  continue;
905                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
906                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
907                                    continue;
908                  }                  }
909                    /* Set manager flag. */
910                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
911                    found = true;
912                    break;
913          }          }
914          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
915                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 525  static bool ccs_is_policy_manager(void) Line 925  static bool ccs_is_policy_manager(void)
925  }  }
926    
927  /**  /**
928   * ccs_find_condition_part - Find condition part from the statement.   * ccs_select_one - Parse select command.
  *  
  * @data: String to parse.  
  *  
  * Returns pointer to the condition part if it was found in the statement,  
  * NULL otherwise.  
  */  
 static char *ccs_find_condition_part(char *data)  
 {  
         char *cp = strstr(data, " if ");  
         if (cp) {  
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
                 cp = strstr(data, " ; set ");  
                 if (cp)  
                         *cp++ = '\0';  
         }  
         return cp;  
 }  
   
 /**  
  * ccs_is_select_one - Parse select command.  
929   *   *
930   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
931   * @data: String to parse.   * @data: String to parse.
# Line 561  static char *ccs_find_condition_part(cha Line 934  static char *ccs_find_condition_part(cha
934   *   *
935   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
936   */   */
937  static bool ccs_is_select_one(struct ccs_io_buffer *head, const char *data)  static bool ccs_select_one(struct ccs_io_buffer *head, const char *data)
938  {  {
939          unsigned int pid;          unsigned int pid;
940          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
941          ccs_assert_read_lock();          bool global_pid = false;
942          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "transition_only")) {
943                  head->read_execute_only = true;                  head->r.print_transition_related_only = true;
944                  return true;                  return true;
945          }          }
946          if (sscanf(data, "pid=%u", &pid) == 1) {          if (sscanf(data, "pid=%u", &pid) == 1 ||
947                (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
948                  struct task_struct *p;                  struct task_struct *p;
949                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
950    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
951                    if (global_pid)
952                            p = ccsecurity_exports.find_task_by_pid_ns(pid,
953                                                                   &init_pid_ns);
954                    else
955                            p = ccsecurity_exports.find_task_by_vpid(pid);
956    #else
957                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
958    #endif
959                  if (p)                  if (p)
960                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
961                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
962          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
963                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
964                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
965          } else          } else
966                  return false;                  return false;
967          head->write_var1 = domain;          head->w.domain = domain;
968          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
969          if (!head->read_buf)          if (!head->read_buf)
970                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
971          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
972            head->r.print_this_domain_only = true;
973            if (domain)
974                    head->r.domain = &domain->list;
975            else
976                    head->r.eof = true;
977          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
978          head->read_single_domain = true;          if (domain && domain->is_deleted)
979          head->read_eof = !domain;                  ccs_set_string(head, "# This is a deleted domain.\n");
         if (domain) {  
                 struct ccs_domain_info *d;  
                 head->read_var1 = NULL;  
                 list_for_each_entry_rcu(d, &ccs_domain_list, list) {  
                         if (d == domain)  
                                 break;  
                         head->read_var1 = &d->list;  
                 }  
                 head->read_var2 = NULL;  
                 head->read_bit = 0;  
                 head->read_step = 0;  
                 if (domain->is_deleted)  
                         ccs_io_printf(head, "# This is a deleted domain.\n");  
         }  
980          return true;          return true;
981  }  }
982    
 static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,  
                                     struct ccs_condition *cond,  
                                     const bool is_delete)  
 {  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))  
                 return ccs_write_capability_policy(data, domain, cond,  
                                                    is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))  
                 return ccs_write_network_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))  
                 return ccs_write_signal_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))  
                 return ccs_write_env_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))  
                 return ccs_write_mount_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_UNMOUNT))  
                 return ccs_write_umount_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CHROOT))  
                 return ccs_write_chroot_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_PIVOT_ROOT))  
                 return ccs_write_pivot_root_policy(data, domain, cond,  
                                                    is_delete);  
         return ccs_write_file_policy(data, domain, cond, is_delete);  
 }  
   
983  /**  /**
984   * ccs_write_domain_policy - Write domain policy.   * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
985   *   *
986   * @head: Pointer to "struct ccs_io_buffer".   * @a: Pointer to "struct ccs_acl_info".
987     * @b: Pointer to "struct ccs_acl_info".
988   *   *
989   * Returns 0 on success, negative value otherwise.   * Returns true if @a == @b, false otherwise.
990   */   */
991  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
992                                     const struct ccs_acl_info *b)
993  {  {
994          char *data = head->write_buf;          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
995          struct ccs_domain_info *domain = head->write_var1;          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
996          bool is_delete = false;          return p1->handler == p2->handler;
         bool is_select = false;  
         unsigned int profile;  
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))  
                 is_delete = true;  
         else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))  
                 is_select = true;  
         if (is_select && ccs_is_select_one(head, data))  
                 return 0;  
         /* Don't allow updating policies by non manager programs. */  
         if (!ccs_is_policy_manager())  
                 return -EPERM;  
         if (ccs_is_domain_def(data)) {  
                 domain = NULL;  
                 if (is_delete)  
                         ccs_delete_domain(data);  
                 else if (is_select)  
                         domain = ccs_find_domain(data);  
                 else  
                         domain = ccs_find_or_assign_new_domain(data, 0);  
                 head->write_var1 = domain;  
                 return 0;  
         }  
         if (!domain)  
                 return -EINVAL;  
   
         if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1  
             && profile < CCS_MAX_PROFILES) {  
                 if (ccs_profile_ptr[profile] || !ccs_policy_loaded)  
                         domain->profile = (u8) profile;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {  
                 domain->ignore_global_allow_read = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {  
                 domain->ignore_global_allow_env = !is_delete;  
                 return 0;  
         }  
         cp = ccs_find_condition_part(data);  
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         error = ccs_write_domain_policy2(data, domain, cond, is_delete);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
 }  
   
 static bool ccs_print_name_union(struct ccs_io_buffer *head,  
                                  const struct ccs_name_union *ptr)  
 {  
         int pos = head->read_avail;  
         if (pos && head->read_buf[pos - 1] == ' ')  
                 head->read_avail--;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, " @%s",  
                                      ptr->group->group_name->name);  
         return ccs_io_printf(head, " %s", ptr->filename->name);  
 }  
   
 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,  
                                         const struct ccs_name_union *ptr)  
 {  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         return ccs_io_printf(head, "\"%s\"", ptr->filename->name);  
 }  
   
 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,  
                                           const struct ccs_number_union *ptr,  
                                           const bool need_space)  
 {  
         unsigned long min;  
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
         if (need_space && !ccs_io_printf(head, " "))  
                 return false;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         min_type = ptr->min_type;  
         max_type = ptr->max_type;  
         min = ptr->values[0];  
         max = ptr->values[1];  
         switch (min_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 if (!ccs_io_printf(head, "0x%lX", min))  
                         return false;  
                 break;  
         case CCS_VALUE_TYPE_OCTAL:  
                 if (!ccs_io_printf(head, "0%lo", min))  
                         return false;  
                 break;  
         default:  
                 if (!ccs_io_printf(head, "%lu", min))  
                         return false;  
                 break;  
         }  
         if (min == max && min_type == max_type)  
                 return true;  
         switch (max_type) {  
         case CCS_VALUE_TYPE_HEXADECIMAL:  
                 return ccs_io_printf(head, "-0x%lX", max);  
         case CCS_VALUE_TYPE_OCTAL:  
                 return ccs_io_printf(head, "-0%lo", max);  
         default:  
                 return ccs_io_printf(head, "-%lu", max);  
         }  
 }  
   
 bool ccs_print_number_union(struct ccs_io_buffer *head,  
                             const struct ccs_number_union *ptr)  
 {  
         return ccs_print_number_union_common(head, ptr, true);  
 }  
   
 static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  
                                            const struct ccs_number_union *ptr)  
 {  
         return ccs_print_number_union_common(head, ptr, false);  
997  }  }
998    
999  /**  /**
1000   * ccs_print_condition - Print condition part.   * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
1001   *   *
1002   * @head: Pointer to "struct ccs_io_buffer".   * @a: Pointer to "struct ccs_acl_info".
1003   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @b: Pointer to "struct ccs_acl_info".
1004   *   *
1005   * Returns true on success, false otherwise.   * Returns true if @a == @b, false otherwise.
1006   */   */
1007  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_same_task_acl(const struct ccs_acl_info *a,
1008                                  const struct ccs_condition *cond)                                const struct ccs_acl_info *b)
1009  {  {
1010          const struct ccs_condition_element *condp;          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
1011          const struct ccs_number_union *numbers_p;          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
1012          const struct ccs_name_union *names_p;          return p1->domainname == p2->domainname;
         const struct ccs_argv_entry *argv;  
         const struct ccs_envp_entry *envp;  
         u16 condc;  
         u16 i;  
         u16 j;  
         char buffer[32];  
         if (!cond)  
                 goto no_condition;  
         condc = cond->condc;  
         condp = (const struct ccs_condition_element *) (cond + 1);  
         numbers_p = (const struct ccs_number_union *) (condp + condc);  
         names_p = (const struct ccs_name_union *)  
                 (numbers_p + cond->numbers_count);  
         argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);  
         envp = (const struct ccs_envp_entry *) (argv + cond->argc);  
         memset(buffer, 0, sizeof(buffer));  
         if (condc && !ccs_io_printf(head, "%s", " if"))  
                 goto out;  
         for (i = 0; i < condc; i++) {  
                 const u8 match = condp->equals;  
                 const u8 left = condp->left;  
                 const u8 right = condp->right;  
                 condp++;  
                 switch (left) {  
                 case CCS_ARGV_ENTRY:  
                         if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",  
                                            argv->index, argv->is_not ?  
                                            "!=" : "=", argv->value->name))  
                                 goto out;  
                         argv++;  
                         continue;  
                 case CCS_ENVP_ENTRY:  
                         if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",  
                                            envp->name->name, envp->is_not ?  
                                            "!=" : "="))  
                                 goto out;  
                         if (envp->value) {  
                                 if (!ccs_io_printf(head, "\"%s\"",  
                                                    envp->value->name))  
                                         goto out;  
                         } else {  
                                 if (!ccs_io_printf(head, "NULL"))  
                                         goto out;  
                         }  
                         envp++;  
                         continue;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (left >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, " %s",  
                                            ccs_condition_keyword[left]))  
                                 goto out;  
                         break;  
                 }  
                 if (!ccs_io_printf(head, "%s", match ? "=" : "!="))  
                         goto out;  
                 switch (right) {  
                 case CCS_NAME_UNION:  
                         if (!ccs_print_name_union_quoted(head, names_p++))  
                                 goto out;  
                         break;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union_nospace(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (right >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, "%s",  
                                            ccs_condition_keyword[right]))  
                                 goto out;  
                         break;  
                 }  
         }  
         i = cond->post_state[3];  
         if (!i)  
                 goto no_condition;  
         if (!ccs_io_printf(head, " ; set"))  
                 goto out;  
         for (j = 0; j < 3; j++) {  
                 if (!(i & (1 << j)))  
                         continue;  
                 if (!ccs_io_printf(head, " task.state[%u]=%u", j,  
                                    cond->post_state[j]))  
                         goto out;  
         }  
  no_condition:  
         if (ccs_io_printf(head, "\n"))  
                 return true;  
  out:  
         return false;  
1013  }  }
1014    
1015  /**  /**
1016   * ccs_print_path_acl - Print a single path ACL entry.   * ccs_write_task - Update task related list.
1017   *   *
1018   * @head: Pointer to "struct ccs_io_buffer".   * @param: Pointer to "struct ccs_acl_param".
  * @ptr:  Pointer to "struct ccs_path_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1019   *   *
1020   * Returns true on success, false otherwise.   * Returns 0 on success, negative value otherwise.
1021     *
1022     * Caller holds ccs_read_lock().
1023   */   */
1024  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static int ccs_write_task(struct ccs_acl_param *param)
                                       struct ccs_path_acl *ptr,  
                                       const struct ccs_condition *cond)  
1025  {  {
1026          int pos;          int error;
1027          u8 bit;          const bool is_auto = ccs_str_starts(&param->data,
1028          const u16 perm = ptr->perm;                                              "auto_domain_transition ");
1029          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {          if (!is_auto && !ccs_str_starts(&param->data,
1030                  if (!(perm & (1 << bit)))                                          "manual_domain_transition ")) {
1031                          continue;                  struct ccs_handler_acl e = { };
1032                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)                  char *handler;
1033                          continue;                  if (ccs_str_starts(&param->data, "auto_execute_handler "))
1034                  /* Print "read/write" instead of "read" and "write". */                          e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
1035                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)                  else if (ccs_str_starts(&param->data,
1036                      && (perm & (1 << CCS_TYPE_READ_WRITE)))                                          "denied_execute_handler "))
1037                          continue;                          e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
1038                  pos = head->read_avail;                  else
1039                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||                          return -EINVAL;
1040                      !ccs_print_name_union(head, &ptr->name) ||                  handler = ccs_read_token(param);
1041                      !ccs_print_condition(head, cond)) {                  if (!ccs_correct_path(handler))
1042                          head->read_bit = bit;                          return -EINVAL;
1043                          head->read_avail = pos;                  e.handler = ccs_get_name(handler);
1044                          return false;                  if (!e.handler)
1045                  }                          return -ENOMEM;
1046                    if (e.handler->is_patterned)
1047                            error = -EINVAL; /* No patterns allowed. */
1048                    else
1049                            error = ccs_update_domain(&e.head, sizeof(e), param,
1050                                                      ccs_same_handler_acl, NULL);
1051                    ccs_put_name(e.handler);
1052            } else {
1053                    struct ccs_task_acl e = {
1054                            .head.type = is_auto ?
1055                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
1056                            .domainname = ccs_get_domainname(param),
1057                    };
1058                    if (!e.domainname)
1059                            error = -EINVAL;
1060                    else
1061                            error = ccs_update_domain(&e.head, sizeof(e), param,
1062                                                      ccs_same_task_acl, NULL);
1063                    ccs_put_name(e.domainname);
1064          }          }
1065          head->read_bit = 0;          return error;
         return true;  
1066  }  }
1067    
1068  /**  /**
1069   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_write_domain2 - Write domain policy.
1070   *   *
1071   * @head: Pointer to "struct ccs_io_buffer".   * @data:      Policy to be interpreted.
1072   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @domain:    Pointer to "struct ccs_domain_info".
1073   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @is_delete: True if it is a delete request.
1074   *   *
1075   * Returns true on success, false otherwise.   * Returns 0 on success, negative value otherwise.
1076     *
1077     * Caller holds ccs_read_lock().
1078   */   */
1079  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
1080                                  struct ccs_path_number3_acl *ptr,                               const bool is_delete)
                                 const struct ccs_condition *cond)  
1081  {  {
1082          int pos;          struct ccs_acl_param param = {
1083          u8 bit;                  .data = data,
1084          const u16 perm = ptr->perm;                  .domain = domain,
1085          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;                  .is_delete = is_delete,
1086               bit++) {          };
1087                  if (!(perm & (1 << bit)))          static const struct {
1088                    const char *keyword;
1089                    int (*write) (struct ccs_acl_param *);
1090            } ccs_callback[7] = {
1091                    { "file ", ccs_write_file },
1092                    { "network inet ", ccs_write_inet_network },
1093                    { "network unix ", ccs_write_unix_network },
1094                    { "misc ", ccs_write_misc },
1095                    { "capability ", ccs_write_capability },
1096                    { "ipc ", ccs_write_ipc },
1097                    { "task ", ccs_write_task },
1098            };
1099            u8 i;
1100            for (i = 0; i < 7; i++) {
1101                    if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
1102                          continue;                          continue;
1103                  pos = head->read_avail;                  return ccs_callback[i].write(&param);
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number32keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->mode) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1104          }          }
1105          head->read_bit = 0;          return -EINVAL;
         return true;  
1106  }  }
1107    
1108  /**  /**
1109   * ccs_print_path2_acl - Print a double path ACL entry.   * ccs_delete_domain - Delete a domain.
1110   *   *
1111   * @head: Pointer to "struct ccs_io_buffer".   * @domainname: The name of domain.
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1112   *   *
1113   * Returns true on success, false otherwise.   * Returns 0.
1114   */   */
1115  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  static int ccs_delete_domain(char *domainname)
                                       struct ccs_path2_acl *ptr,  
                                       const struct ccs_condition *cond)  
1116  {  {
1117          int pos;          struct ccs_domain_info *domain;
1118          u8 bit;          struct ccs_path_info name;
1119          const u8 perm = ptr->perm;          name.name = domainname;
1120          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {          ccs_fill_path_info(&name);
1121                  if (!(perm & (1 << bit)))          if (mutex_lock_interruptible(&ccs_policy_lock))
1122                    return 0;
1123            /* Is there an active domain? */
1124            list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
1125                    /* Never delete ccs_kernel_domain. */
1126                    if (domain == &ccs_kernel_domain)
1127                          continue;                          continue;
1128                  pos = head->read_avail;                  if (domain->is_deleted ||
1129                  if (!ccs_io_printf(head, "allow_%s",                      ccs_pathcmp(domain->domainname, &name))
1130                                     ccs_path22keyword(bit)) ||                          continue;
1131                      !ccs_print_name_union(head, &ptr->name1) ||                  domain->is_deleted = true;
1132                      !ccs_print_name_union(head, &ptr->name2) ||                  break;
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1133          }          }
1134          head->read_bit = 0;          mutex_unlock(&ccs_policy_lock);
1135          return true;          return 0;
1136  }  }
1137    
1138    /* String table for domain flags. */
1139    const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1140            [CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
1141            [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
1142    };
1143    
1144  /**  /**
1145   * ccs_print_path_number_acl - Print an ioctl/chmod/chown/chgrp ACL entry.   * ccs_write_domain - Write domain policy.
1146   *   *
1147   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to "struct ccs_path_number_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1148   *   *
1149   * Returns true on success, false otherwise.   * Returns 0 on success, negative value otherwise.
1150     *
1151     * Caller holds ccs_read_lock().
1152   */   */
1153  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static int ccs_write_domain(struct ccs_io_buffer *head)
                                       struct ccs_path_number_acl *ptr,  
                                       const struct ccs_condition *cond)  
1154  {  {
1155          int pos;          char *data = head->write_buf;
1156          u8 bit;          struct ccs_domain_info *domain = head->w.domain;
1157          const u8 perm = ptr->perm;          bool is_delete = false;
1158          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;          bool is_select = false;
1159               bit++) {          unsigned int profile;
1160                  if (!(perm & (1 << bit)))          if (ccs_str_starts(&data, "delete "))
1161                    is_delete = true;
1162            else if (ccs_str_starts(&data, "select "))
1163                    is_select = true;
1164            if (is_select && ccs_select_one(head, data))
1165                    return -EAGAIN;
1166            /* Don't allow updating policies by non manager programs. */
1167            if (!ccs_manager())
1168                    return -EPERM;
1169            if (ccs_domain_def(data)) {
1170                    domain = NULL;
1171                    if (is_delete)
1172                            ccs_delete_domain(data);
1173                    else if (is_select)
1174                            domain = ccs_find_domain(data);
1175                    else
1176                            domain = ccs_assign_domain(data, 0, 0, false);
1177                    head->w.domain = domain;
1178                    return 0;
1179            }
1180            if (!domain)
1181                    return -EINVAL;
1182    
1183            if (sscanf(data, "use_profile %u\n", &profile) == 1
1184                && profile < CCS_MAX_PROFILES) {
1185                    if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1186                            if (!is_delete)
1187                                    domain->profile = (u8) profile;
1188                    return 0;
1189            }
1190            if (sscanf(data, "use_group %u\n", &profile) == 1
1191                && profile < CCS_MAX_ACL_GROUPS) {
1192                    if (!is_delete)
1193                            domain->group = (u8) profile;
1194                    return 0;
1195            }
1196            for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1197                    const char *cp = ccs_dif[profile];
1198                    if (strncmp(data, cp, strlen(cp) - 1))
1199                          continue;                          continue;
1200                  pos = head->read_avail;                  domain->flags[profile] = !is_delete;
1201                  if (!ccs_io_printf(head, "allow_%s",                  return 0;
                                    ccs_path_number2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->number) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1202          }          }
1203          head->read_bit = 0;          return ccs_write_domain2(data, domain, is_delete);
         return true;  
1204  }  }
1205    
1206  /**  /**
1207   * ccs_print_env_acl - Print an evironment variable name's ACL entry.   * ccs_print_name_union - Print a ccs_name_union.
1208   *   *
1209   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1210   * @ptr:  Pointer to "struct ccs_env_acl".   * @ptr:  Pointer to "struct ccs_name_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1211   *   *
1212   * Returns true on success, false otherwise.   * Returns nothing.
1213   */   */
1214  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1215                                struct ccs_env_acl *ptr,                                   const struct ccs_name_union *ptr)
1216                                const struct ccs_condition *cond)  {
1217  {          const bool cond = head->r.print_cond_part;
1218          const int pos = head->read_avail;          if (!cond)
1219          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||                  ccs_set_space(head);
1220              !ccs_print_condition(head, cond)) {          if (ptr->is_group) {
1221                  head->read_avail = pos;                  ccs_set_string(head, "@");
1222                  return false;                  ccs_set_string(head, ptr->group->group_name->name);
1223            } else {
1224                    if (cond)
1225                            ccs_set_string(head, "\"");
1226                    ccs_set_string(head, ptr->filename->name);
1227                    if (cond)
1228                            ccs_set_string(head, "\"");
1229          }          }
         return true;  
1230  }  }
1231    
1232  /**  /**
1233   * ccs_print_capability_acl - Print a capability ACL entry.   * ccs_print_number_union - Print a ccs_number_union.
1234   *   *
1235   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1236   * @ptr:  Pointer to "struct ccs_capability_acl".   * @ptr:  Pointer to "struct ccs_number_union".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1237   *   *
1238   * Returns true on success, false otherwise.   * Returns nothing.
1239   */   */
1240  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1241                                       struct ccs_capability_acl *ptr,                                     const struct ccs_number_union *ptr)
1242                                       const struct ccs_condition *cond)  {
1243  {          if (!head->r.print_cond_part)
1244          const int pos = head->read_avail;                  ccs_set_space(head);
1245          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",          if (ptr->is_group) {
1246                             ccs_cap2keyword(ptr->operation)) ||                  ccs_set_string(head, "@");
1247              !ccs_print_condition(head, cond)) {                  ccs_set_string(head, ptr->group->group_name->name);
1248                  head->read_avail = pos;          } else {
1249                  return false;                  int i;
1250                    unsigned long min = ptr->values[0];
1251                    const unsigned long max = ptr->values[1];
1252                    u8 min_type = ptr->value_type[0];
1253                    const u8 max_type = ptr->value_type[1];
1254                    char buffer[128];
1255                    buffer[0] = '\0';
1256                    for (i = 0; i < 2; i++) {
1257                            switch (min_type) {
1258                            case CCS_VALUE_TYPE_HEXADECIMAL:
1259                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1260                                                  min);
1261                                    break;
1262                            case CCS_VALUE_TYPE_OCTAL:
1263                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1264                                                  min);
1265                                    break;
1266                            default:
1267                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1268                                                  min);
1269                                    break;
1270                            }
1271                            if (min == max && min_type == max_type)
1272                                    break;
1273                            ccs_addprintf(buffer, sizeof(buffer), "-");
1274                            min_type = max_type;
1275                            min = max;
1276                    }
1277                    ccs_io_printf(head, "%s", buffer);
1278          }          }
         return true;  
1279  }  }
1280    
1281  /**  /**
1282   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.   * ccs_print_condition - Print condition part.
1283   *   *
1284   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1285   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @cond: Pointer to "struct ccs_condition".
1286   *   *
1287   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1288   */   */
1289  static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1290                                   struct ccs_ip_network_acl *ptr)                                  const struct ccs_condition *cond)
1291  {  {
1292          const u32 min_address = ptr->address.ipv4.min;          switch (head->r.cond_step) {
1293          const u32 max_address = ptr->address.ipv4.max;          case 0:
1294          if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))                  head->r.cond_index = 0;
1295                  return false;                  head->r.cond_step++;
1296          if (min_address != max_address                  /* fall through */
1297              && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))          case 1:
1298                  return false;                  {
1299          return true;                          const u16 condc = cond->condc;
1300                            const struct ccs_condition_element *condp =
1301                                    (typeof(condp)) (cond + 1);
1302                            const struct ccs_number_union *numbers_p =
1303                                    (typeof(numbers_p)) (condp + condc);
1304                            const struct ccs_name_union *names_p =
1305                                    (typeof(names_p))
1306                                    (numbers_p + cond->numbers_count);
1307                            const struct ccs_argv *argv =
1308                                    (typeof(argv)) (names_p + cond->names_count);
1309                            const struct ccs_envp *envp =
1310                                    (typeof(envp)) (argv + cond->argc);
1311                            u16 skip;
1312                            for (skip = 0; skip < head->r.cond_index; skip++) {
1313                                    const u8 left = condp->left;
1314                                    const u8 right = condp->right;
1315                                    condp++;
1316                                    switch (left) {
1317                                    case CCS_ARGV_ENTRY:
1318                                            argv++;
1319                                            continue;
1320                                    case CCS_ENVP_ENTRY:
1321                                            envp++;
1322                                            continue;
1323                                    case CCS_NUMBER_UNION:
1324                                            numbers_p++;
1325                                            break;
1326                                    }
1327                                    switch (right) {
1328                                    case CCS_NAME_UNION:
1329                                            names_p++;
1330                                            break;
1331                                    case CCS_NUMBER_UNION:
1332                                            numbers_p++;
1333                                            break;
1334                                    }
1335                            }
1336                            while (head->r.cond_index < condc) {
1337                                    const u8 match = condp->equals;
1338                                    const u8 left = condp->left;
1339                                    const u8 right = condp->right;
1340                                    if (!ccs_flush(head))
1341                                            return false;
1342                                    condp++;
1343                                    head->r.cond_index++;
1344                                    ccs_set_space(head);
1345                                    switch (left) {
1346                                    case CCS_ARGV_ENTRY:
1347                                            ccs_io_printf(head,
1348                                                          "exec.argv[%u]%s\"%s\"",
1349                                                          argv->index,
1350                                                          argv->is_not ?
1351                                                          "!=" : "=",
1352                                                          argv->value->name);
1353                                            argv++;
1354                                            continue;
1355                                    case CCS_ENVP_ENTRY:
1356                                            ccs_io_printf(head,
1357                                                          "exec.envp[\"%s\"]%s",
1358                                                          envp->name->name,
1359                                                          envp->is_not ?
1360                                                          "!=" : "=");
1361                                            if (envp->value) {
1362                                                    ccs_set_string(head, "\"");
1363                                                    ccs_set_string(head, envp->
1364                                                                   value->name);
1365                                                    ccs_set_string(head, "\"");
1366                                            } else {
1367                                                    ccs_set_string(head, "NULL");
1368                                            }
1369                                            envp++;
1370                                            continue;
1371                                    case CCS_NUMBER_UNION:
1372                                            ccs_print_number_union(head,
1373                                                                   numbers_p++);
1374                                            break;
1375                                    default:
1376                                            ccs_set_string(head,
1377                                                   ccs_condition_keyword[left]);
1378                                            break;
1379                                    }
1380                                    ccs_set_string(head, match ? "=" : "!=");
1381                                    switch (right) {
1382                                    case CCS_NAME_UNION:
1383                                            ccs_print_name_union(head, names_p++);
1384                                            break;
1385                                    case CCS_NUMBER_UNION:
1386                                            ccs_print_number_union(head,
1387                                                                   numbers_p++);
1388                                            break;
1389                                    default:
1390                                            ccs_set_string(head,
1391                                                   ccs_condition_keyword[right]);
1392                                            break;
1393                                    }
1394                            }
1395                    }
1396                    head->r.cond_step++;
1397                    /* fall through */
1398            case 2:
1399                    if (!ccs_flush(head))
1400                            break;
1401                    head->r.cond_step++;
1402                    /* fall through */
1403            case 3:
1404                    if (cond->grant_log != CCS_GRANTLOG_AUTO)
1405                            ccs_io_printf(head, " grant_log=%s",
1406                                          ccs_yesno(cond->grant_log ==
1407                                                    CCS_GRANTLOG_YES));
1408                    if (cond->transit) {
1409                            ccs_set_string(head, " auto_domain_transition=\"");
1410                            ccs_set_string(head, cond->transit->name);
1411                            ccs_set_string(head, "\"");
1412                    }
1413                    ccs_set_lf(head);
1414                    return true;
1415            }
1416            return false;
1417  }  }
1418    
1419  /**  /**
1420   * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.   * ccs_set_group - Print "acl_group " header keyword and category name.
1421   *   *
1422   * @head: Pointer to "struct ccs_io_buffer".   * @head:     Pointer to "struct ccs_io_buffer".
1423   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @category: Category name.
1424   *   *
1425   * Returns true on success, false otherwise.   * Returns nothing.
1426   */   */
1427  static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  static void ccs_set_group(struct ccs_io_buffer *head, const char *category)
                                  struct ccs_ip_network_acl *ptr)  
1428  {  {
1429          char buf[64];          if (head->type == CCS_EXCEPTIONPOLICY)
1430          const struct in6_addr *min_address = ptr->address.ipv6.min;                  ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index);
1431          const struct in6_addr *max_address = ptr->address.ipv6.max;          ccs_set_string(head, category);
         ccs_print_ipv6(buf, sizeof(buf), min_address);  
         if (!ccs_io_printf(head, "%s", buf))  
                 return false;  
         if (min_address != max_address) {  
                 ccs_print_ipv6(buf, sizeof(buf), max_address);  
                 if (!ccs_io_printf(head, "-%s", buf))  
                         return false;  
         }  
         return true;  
1432  }  }
1433    
1434  /**  /**
1435   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_entry - Print an ACL entry.
1436   *   *
1437   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1438   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1439   *   *
1440   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1441   */   */
1442  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1443                                    struct ccs_ip_network_acl *ptr,                              const struct ccs_acl_info *acl)
                                   const struct ccs_condition *cond)  
1444  {  {
1445          int pos;          const u8 acl_type = acl->type;
1446            const bool may_trigger_transition = acl->cond && acl->cond->transit;
1447            bool first = true;
1448          u8 bit;          u8 bit;
1449          const u16 perm = ptr->perm;          if (head->r.print_cond_part)
1450          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                  goto print_cond_part;
1451                  if (!(perm & (1 << bit)))          if (acl->is_deleted)
1452                          continue;                  return true;
1453                  pos = head->read_avail;          if (!ccs_flush(head))
1454                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",                  return false;
1455                                     ccs_net2keyword(bit)))          else if (acl_type == CCS_TYPE_PATH_ACL) {
1456                          goto out;                  struct ccs_path_acl *ptr
1457                            = container_of(acl, typeof(*ptr), head);
1458                    const u16 perm = ptr->perm;
1459                    for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) {
1460                            if (!(perm & (1 << bit)))
1461                                    continue;
1462                            if (head->r.print_transition_related_only &&
1463                                bit != CCS_TYPE_EXECUTE && !may_trigger_transition)
1464                                    continue;
1465                            if (first) {
1466                                    ccs_set_group(head, "file ");
1467                                    first = false;
1468                            } else {
1469                                    ccs_set_slash(head);
1470                            }
1471                            ccs_set_string(head, ccs_path_keyword[bit]);
1472                    }
1473                    if (first)
1474                            return true;
1475                    ccs_print_name_union(head, &ptr->name);
1476            } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1477                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1478                    struct ccs_handler_acl *ptr
1479                            = container_of(acl, typeof(*ptr), head);
1480                    ccs_set_group(head, "task ");
1481                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1482                                   ? "auto_execute_handler " :
1483                                   "denied_execute_handler ");
1484                    ccs_set_string(head, ptr->handler->name);
1485            } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1486                       acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1487                    struct ccs_task_acl *ptr =
1488                            container_of(acl, typeof(*ptr), head);
1489                    ccs_set_group(head, "task ");
1490                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1491                                   "auto_domain_transition " :
1492                                   "manual_domain_transition ");
1493                    ccs_set_string(head, ptr->domainname->name);
1494            } else if (head->r.print_transition_related_only &&
1495                       !may_trigger_transition) {
1496                    return true;
1497            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1498                    struct ccs_mkdev_acl *ptr =
1499                            container_of(acl, typeof(*ptr), head);
1500                    const u8 perm = ptr->perm;
1501                    for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
1502                            if (!(perm & (1 << bit)))
1503                                    continue;
1504                            if (first) {
1505                                    ccs_set_group(head, "file ");
1506                                    first = false;
1507                            } else {
1508                                    ccs_set_slash(head);
1509                            }
1510                            ccs_set_string(head, ccs_mac_keywords
1511                                           [ccs_pnnn2mac[bit]]);
1512                    }
1513                    if (first)
1514                            return true;
1515                    ccs_print_name_union(head, &ptr->name);
1516                    ccs_print_number_union(head, &ptr->mode);
1517                    ccs_print_number_union(head, &ptr->major);
1518                    ccs_print_number_union(head, &ptr->minor);
1519            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1520                    struct ccs_path2_acl *ptr =
1521                            container_of(acl, typeof(*ptr), head);
1522                    const u8 perm = ptr->perm;
1523                    for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1524                            if (!(perm & (1 << bit)))
1525                                    continue;
1526                            if (first) {
1527                                    ccs_set_group(head, "file ");
1528                                    first = false;
1529                            } else {
1530                                    ccs_set_slash(head);
1531                            }
1532                            ccs_set_string(head, ccs_mac_keywords
1533                                           [ccs_pp2mac[bit]]);
1534                    }
1535                    if (first)
1536                            return true;
1537                    ccs_print_name_union(head, &ptr->name1);
1538                    ccs_print_name_union(head, &ptr->name2);
1539            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1540                    struct ccs_path_number_acl *ptr =
1541                            container_of(acl, typeof(*ptr), head);
1542                    const u8 perm = ptr->perm;
1543                    for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
1544                            if (!(perm & (1 << bit)))
1545                                    continue;
1546                            if (first) {
1547                                    ccs_set_group(head, "file ");
1548                                    first = false;
1549                            } else {
1550                                    ccs_set_slash(head);
1551                            }
1552                            ccs_set_string(head, ccs_mac_keywords
1553                                           [ccs_pn2mac[bit]]);
1554                    }
1555                    if (first)
1556                            return true;
1557                    ccs_print_name_union(head, &ptr->name);
1558                    ccs_print_number_union(head, &ptr->number);
1559            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1560                    struct ccs_env_acl *ptr =
1561                            container_of(acl, typeof(*ptr), head);
1562                    ccs_set_group(head, "misc env ");
1563                    ccs_set_string(head, ptr->env->name);
1564            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1565                    struct ccs_capability_acl *ptr =
1566                            container_of(acl, typeof(*ptr), head);
1567                    ccs_set_group(head, "capability ");
1568                    ccs_set_string(head, ccs_mac_keywords
1569                                   [ccs_c2mac[ptr->operation]]);
1570            } else if (acl_type == CCS_TYPE_INET_ACL) {
1571                    struct ccs_inet_acl *ptr =
1572                            container_of(acl, typeof(*ptr), head);
1573                    const u8 perm = ptr->perm;
1574                    for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1575                            if (!(perm & (1 << bit)))
1576                                    continue;
1577                            if (first) {
1578                                    ccs_set_group(head, "network inet ");
1579                                    ccs_set_string(head, ccs_proto_keyword
1580                                                   [ptr->protocol]);
1581                                    ccs_set_space(head);
1582                                    first = false;
1583                            } else {
1584                                    ccs_set_slash(head);
1585                            }
1586                            ccs_set_string(head, ccs_socket_keyword[bit]);
1587                    }
1588                    if (first)
1589                            return true;
1590                    ccs_set_space(head);
1591                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1592                            char buf[128];
1593                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1594                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1595                                             group_name->name))                          ccs_set_string(head,
1596                                  goto out;                                         ptr->address.group->group_name->name);
1597                          break;                          break;
1598                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1599                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1600                                  goto out;                                         ptr->address.ipv4.max);
1601                            ccs_io_printf(head, "%s", buf);
1602                          break;                          break;
1603                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1604                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1605                                  goto out;                                         ptr->address.ipv6.max);
1606                            ccs_io_printf(head, "%s", buf);
1607                          break;                          break;
1608                  }                  }
1609                  if (!ccs_print_number_union(head, &ptr->port) ||                  ccs_print_number_union(head, &ptr->port);
1610                      !ccs_print_condition(head, cond))          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1611                          goto out;                  struct ccs_unix_acl *ptr =
1612          }                          container_of(acl, typeof(*ptr), head);
1613          head->read_bit = 0;                  const u8 perm = ptr->perm;
1614          return true;                  for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1615   out:                          if (!(perm & (1 << bit)))
1616          head->read_bit = bit;                                  continue;
1617          head->read_avail = pos;                          if (first) {
1618          return false;                                  ccs_set_group(head, "network unix ");
1619  }                                  ccs_set_string(head, ccs_proto_keyword
1620                                                   [ptr->protocol]);
1621  /**                                  ccs_set_space(head);
1622   * ccs_print_signal_acl - Print a signal ACL entry.                                  first = false;
1623   *                          } else {
1624   * @head: Pointer to "struct ccs_io_buffer".                                  ccs_set_slash(head);
1625   * @ptr:  Pointer to "struct signale_acl".                          }
1626   * @cond: Pointer to "struct ccs_condition". May be NULL.                          ccs_set_string(head, ccs_socket_keyword[bit]);
1627   *                  }
1628   * Returns true on success, false otherwise.                  if (first)
1629   */                          return true;
1630  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                  ccs_print_name_union(head, &ptr->name);
1631                                   struct ccs_signal_acl *ptr,          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1632                                   const struct ccs_condition *cond)                  struct ccs_signal_acl *ptr =
1633  {                          container_of(acl, typeof(*ptr), head);
1634          const int pos = head->read_avail;                  ccs_set_group(head, "ipc signal ");
1635          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                  ccs_io_printf(head, "%u ", ptr->sig);
1636                             ptr->sig, ptr->domainname->name) ||                  ccs_set_string(head, ptr->domainname->name);
1637              !ccs_print_condition(head, cond)) {          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1638                  head->read_avail = pos;                  struct ccs_mount_acl *ptr =
1639                  return false;                          container_of(acl, typeof(*ptr), head);
1640          }                  ccs_set_group(head, "file mount");
1641          return true;                  ccs_print_name_union(head, &ptr->dev_name);
1642  }                  ccs_print_name_union(head, &ptr->dir_name);
1643                    ccs_print_name_union(head, &ptr->fs_type);
1644  /**                  ccs_print_number_union(head, &ptr->flags);
1645   * ccs_print_execute_handler_record - Print an execute handler ACL entry.          }
1646   *          if (acl->cond) {
1647   * @head:    Pointer to "struct ccs_io_buffer".                  head->r.print_cond_part = true;
1648   * @keyword: Name of the keyword.                  head->r.cond_step = 0;
1649   * @ptr:     Pointer to "struct ccs_execute_handler_record".                  if (!ccs_flush(head))
1650   *                          return false;
1651   * Returns true on success, false otherwise.  print_cond_part:
1652   */                  if (!ccs_print_condition(head, acl->cond))
1653  static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,                          return false;
1654                                               const char *keyword,                  head->r.print_cond_part = false;
1655                                               struct ccs_execute_handler_record *          } else {
1656                                               ptr)                  ccs_set_lf(head);
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_umount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_umount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_umount_acl(struct ccs_io_buffer *head,  
                                  struct ccs_umount_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_UNMOUNT) ||  
             !ccs_print_name_union(head, &ptr->dir) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1657          }          }
1658          return true;          return true;
1659  }  }
1660    
1661  /**  /**
1662   * ccs_print_chroot_acl - Print a chroot ACL entry.   * ccs_read_domain2 - Read domain policy.
1663   *   *
1664   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1665   * @ptr:  Pointer to "struct ccs_chroot_acl".   * @domain: Pointer to "struct ccs_domain_info".
1666   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @index:  Index number.
1667   *   *
1668   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
  */  
 static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,  
                                  struct ccs_chroot_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CHROOT) ||  
             !ccs_print_name_union(head, &ptr->dir) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_pivot_root_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1669   *   *
1670   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
1671   */   */
1672  static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1673                                       struct ccs_pivot_root_acl *ptr,                               struct ccs_domain_info *domain,
1674                                       const struct ccs_condition *cond)                               const u8 index)
1675  {  {
1676          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1677          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_PIVOT_ROOT) ||                  struct ccs_acl_info *ptr =
1678              !ccs_print_name_union(head, &ptr->new_root) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1679              !ccs_print_name_union(head, &ptr->old_root) ||                  if (!ccs_print_entry(head, ptr))
1680              !ccs_print_condition(head, cond)) {                          return false;
                 head->read_avail = pos;  
                 return false;  
1681          }          }
1682            head->r.acl = NULL;
1683          return true;          return true;
1684  }  }
1685    
1686  /**  /**
1687   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain - Read domain policy.
1688   *   *
1689   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to an ACL entry.  
1690   *   *
1691   * Returns true on success, false otherwise.   * Returns nothing.
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const struct ccs_condition *cond = ptr->cond;  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl,  
                                        head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, struct ccs_path_number3_acl, head);  
                 return ccs_print_path_number3_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, struct ccs_path2_acl,  
                                        head);  
                 return ccs_print_path2_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, struct ccs_path_number_acl,  
                                        head);  
                 return ccs_print_path_number_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, struct ccs_env_acl, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, struct ccs_capability_acl,  
                                        head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, struct ccs_ip_network_acl,  
                                        head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, struct ccs_signal_acl, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, struct ccs_mount_acl, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_UMOUNT_ACL) {  
                 struct ccs_umount_acl *acl  
                         = container_of(ptr, struct ccs_umount_acl, head);  
                 return ccs_print_umount_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CHROOT_ACL) {  
                 struct ccs_chroot_acl *acl  
                         = container_of(ptr, struct ccs_chroot_acl, head);  
                 return ccs_print_chroot_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PIVOT_ROOT_ACL) {  
                 struct ccs_pivot_root_acl *acl  
                         = container_of(ptr, struct ccs_pivot_root_acl,  
                                        head);  
                 return ccs_print_pivot_root_acl(head, acl, cond);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0.  
1692   *   *
1693   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1694   */   */
1695  static int ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1696  {  {
1697          struct list_head *dpos;          if (head->r.eof)
1698          struct list_head *apos;                  return;
1699          ccs_assert_read_lock();          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1700          if (head->read_eof)                  struct ccs_domain_info *domain =
1701                  return 0;                          list_entry(head->r.domain, typeof(*domain), list);
1702          if (head->read_step == 0)                  switch (head->r.step) {
1703                  head->read_step = 1;                          u8 i;
1704          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                  case 0:
1705                  struct ccs_domain_info *domain;                          if (domain->is_deleted &&
1706                  const char *quota_exceeded = "";                              !head->r.print_this_domain_only)
1707                  const char *transition_failed = "";                                  continue;
1708                  const char *ignore_global_allow_read = "";                          /* Print domainname and flags. */
1709                  const char *ignore_global_allow_env = "";                          ccs_set_string(head, domain->domainname->name);
1710                  domain = list_entry(dpos, struct ccs_domain_info, list);                          ccs_set_lf(head);
1711                  if (head->read_step != 1)                          ccs_io_printf(head, "use_profile %u\n",
1712                          goto acl_loop;                                        domain->profile);
1713                  if (domain->is_deleted && !head->read_single_domain)                          ccs_io_printf(head, "use_group %u\n", domain->group);
1714                          continue;                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1715                  /* Print domainname and flags. */                                  if (domain->flags[i])
1716                  if (domain->quota_warned)                                          ccs_set_string(head, ccs_dif[i]);
1717                          quota_exceeded = "quota_exceeded\n";                          head->r.step++;
1718                  if (domain->domain_transition_failed)                          ccs_set_lf(head);
1719                          transition_failed = "transition_failed\n";                          /* fall through */
1720                  if (domain->ignore_global_allow_read)                  case 1:
1721                          ignore_global_allow_read                          if (!ccs_read_domain2(head, domain, 0))
1722                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                  return;
1723                  if (domain->ignore_global_allow_env)                          head->r.step++;
1724                          ignore_global_allow_env                          /* fall through */
1725                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                  case 2:
1726                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                          if (!ccs_read_domain2(head, domain, 1))
1727                                     "%s%s%s%s\n", domain->domainname->name,                                  return;
1728                                     domain->profile, quota_exceeded,                          head->r.step++;
1729                                     transition_failed,                          if (!ccs_set_lf(head))
1730                                     ignore_global_allow_read,                                  return;
1731                                     ignore_global_allow_env))                          /* fall through */
1732                          return 0;                  case 3:
1733                  head->read_step = 2;                          head->r.step = 0;
1734   acl_loop:                          if (head->r.print_this_domain_only)
1735                  if (head->read_step == 3)                                  goto done;
1736                          goto tail_mark;                  }
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
                                 return 0;  
                 }  
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return 0;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1737          }          }
1738          head->read_eof = true;  done:
1739          return 0;          head->r.eof = true;
1740  }  }
1741    
1742  /**  /**
# Line 1488  static int ccs_read_domain_policy(struct Line 1749  static int ccs_read_domain_policy(struct
1749   * This is equivalent to doing   * This is equivalent to doing
1750   *   *
1751   *     ( echo "select " $domainname; echo "use_profile " $profile ) |   *     ( echo "select " $domainname; echo "use_profile " $profile ) |
1752   *     /usr/lib/ccs/loadpolicy -d   *     /usr/sbin/ccs-loadpolicy -d
1753   *   *
1754   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1755   */   */
# Line 1498  static int ccs_write_domain_profile(stru Line 1759  static int ccs_write_domain_profile(stru
1759          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
1760          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
1761          unsigned int profile;          unsigned int profile;
         ccs_assert_read_lock();  
1762          if (!cp)          if (!cp)
1763                  return -EINVAL;                  return -EINVAL;
1764          *cp = '\0';          *cp = '\0';
# Line 1506  static int ccs_write_domain_profile(stru Line 1766  static int ccs_write_domain_profile(stru
1766          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1767                  return -EINVAL;                  return -EINVAL;
1768          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1769          if (domain && (ccs_profile_ptr[profile] || !ccs_policy_loaded))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1770                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1771          return 0;          return 0;
1772  }  }
# Line 1516  static int ccs_write_domain_profile(stru Line 1776  static int ccs_write_domain_profile(stru
1776   *   *
1777   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1778   *   *
1779   * Returns list of profile number and domainname pairs.   * Returns nothing.
1780   *   *
1781   * This is equivalent to doing   * This is equivalent to doing
1782   *   *
# Line 1527  static int ccs_write_domain_profile(stru Line 1787  static int ccs_write_domain_profile(stru
1787   *   *
1788   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1789   */   */
1790  static int ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1791  {  {
1792          struct list_head *pos;          if (head->r.eof)
1793          ccs_assert_read_lock();                  return;
1794          if (head->read_eof)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1795                  return 0;                  struct ccs_domain_info *domain =
1796          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
                 struct ccs_domain_info *domain;  
                 domain = list_entry(pos, struct ccs_domain_info, list);  
1797                  if (domain->is_deleted)                  if (domain->is_deleted)
1798                          continue;                          continue;
1799                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
1800                                     domain->domainname->name))                          return;
1801                          return 0;                  ccs_io_printf(head, "%u ", domain->profile);
1802                    ccs_set_string(head, domain->domainname->name);
1803                    ccs_set_lf(head);
1804          }          }
1805          head->read_eof = true;          head->r.eof = true;
         return 0;  
1806  }  }
1807    
1808  /**  /**
1809   * ccs_write_pid: Specify PID to obtain domainname.   * ccs_write_pid - Specify PID to obtain domainname.
1810   *   *
1811   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1812   *   *
# Line 1555  static int ccs_read_domain_profile(struc Line 1814  static int ccs_read_domain_profile(struc
1814   */   */
1815  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1816  {  {
1817          head->read_eof = false;          head->r.eof = false;
1818          return 0;          return 0;
1819  }  }
1820    
# Line 1570  static int ccs_write_pid(struct ccs_io_b Line 1829  static int ccs_write_pid(struct ccs_io_b
1829   *   *
1830   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1831   */   */
1832  static int ccs_read_pid(struct ccs_io_buffer *head)  static void ccs_read_pid(struct ccs_io_buffer *head)
1833  {  {
1834          char *buf = head->write_buf;          char *buf = head->write_buf;
1835          bool task_info = false;          bool task_info = false;
1836            bool global_pid = false;
1837          unsigned int pid;          unsigned int pid;
1838          struct task_struct *p;          struct task_struct *p;
1839          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1840          u32 ccs_flags = 0;          u32 ccs_flags = 0;
         ccs_assert_read_lock();  
1841          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1842          if (!buf)          if (!buf) {
1843                  goto done; /* Do nothing if open(O_RDONLY). */                  head->r.eof = true;
1844          if (head->read_avail || head->read_eof)                  return; /* Do nothing if open(O_RDONLY). */
1845                  goto done;          }
1846          head->read_eof = true;          if (head->r.w_pos || head->r.eof)
1847                    return;
1848            head->r.eof = true;
1849          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1850                  task_info = true;                  task_info = true;
1851            if (ccs_str_starts(&buf, "global-pid "))
1852                    global_pid = true;
1853          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1854          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1855    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1856            if (global_pid)
1857                    p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1858            else
1859                    p = ccsecurity_exports.find_task_by_vpid(pid);
1860    #else
1861          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1862    #endif
1863          if (p) {          if (p) {
1864                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1865                  ccs_flags = p->ccs_flags;                  ccs_flags = ccs_task_flags(p);
1866          }          }
1867          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1868          if (!domain)          if (!domain)
1869                  goto done;                  return;
1870          if (!task_info)          if (!task_info) {
1871                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1872                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1873          else          } else {
1874                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
1875                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                ccs_yesno(ccs_flags &
1876                                ccs_flags & CCS_TASK_IS_POLICY_MANAGER ?                                          CCS_TASK_IS_MANAGER),
1877                                "yes" : "no",                                ccs_yesno(ccs_flags &
1878                                ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ?                                          CCS_TASK_IS_EXECUTE_HANDLER));
1879                                "yes" : "no",          }
                               (u8) (ccs_flags >> 24),  
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
  done:  
         return 0;  
1880  }  }
1881    
1882    /* String table for domain transition control keywords. */
1883    static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1884            [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1885            [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1886            [CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1887            [CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
1888    };
1889    
1890    /* String table for grouping keywords. */
1891    static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1892            [CCS_PATH_GROUP]    = "path_group ",
1893            [CCS_NUMBER_GROUP]  = "number_group ",
1894            [CCS_ADDRESS_GROUP] = "address_group ",
1895    };
1896    
1897  /**  /**
1898   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1899   *   *
1900   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1901   *   *
1902   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1903   */   */
1904  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1905  {  {
1906          char *data = head->write_buf;          char *data = head->write_buf;
1907          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, "delete ");
1908          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1909                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1910          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1911                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1912          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[2] = {
1913                  return ccs_write_domain_initializer_policy(data, false,                  { "aggregator ",    ccs_write_aggregator },
1914                                                             is_delete);                  { "deny_autobind ", ccs_write_reserved_port },
1915          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))          };
1916                  return ccs_write_domain_initializer_policy(data, true,          if (!is_delete && ccs_str_starts(&data, "select ") &&
1917                                                             is_delete);              !strcmp(data, "transition_only")) {
1918          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  head->r.print_transition_related_only = true;
1919                  return ccs_write_aggregator_policy(data, is_delete);                  return -EAGAIN;
1920          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          }
1921                  return ccs_write_globally_readable_policy(data, is_delete);          /* Don't allow updating policies by non manager programs. */
1922          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          if (!ccs_manager())
1923                  return ccs_write_globally_usable_env_policy(data, is_delete);                  return -EPERM;
1924          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < 2; i++)
1925                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1926          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_callback[i].write(data, is_delete);
1927                  return ccs_write_path_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1928          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1929                  return ccs_write_number_group_policy(data, is_delete);                          return ccs_write_transition_control(data, is_delete,
1930          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                                                              i);
1931                  return ccs_write_no_rewrite_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++)
1932          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1933                  return ccs_write_address_group_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1934          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))          if (ccs_str_starts(&data, "acl_group ")) {
1935                  return ccs_write_reserved_port_policy(data, is_delete);                  unsigned int group;
1936                    if (sscanf(data, "%u", &group) == 1 &&
1937                        group < CCS_MAX_ACL_GROUPS) {
1938                            data = strchr(data, ' ');
1939                            if (data)
1940                                    return ccs_write_domain2(data + 1,
1941                                                             &ccs_acl_group[group],
1942                                                             is_delete);
1943                    }
1944            }
1945          return -EINVAL;          return -EINVAL;
1946  }  }
1947    
1948  /**  /**
1949   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1950   *   *
1951   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1952     * @idx:  Index number.
1953   *   *
1954   * Returns 0 on success, -EINVAL otherwise.   * Returns true on success, false otherwise.
1955   *   *
1956   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1957   */   */
1958  static int ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1959  {  {
1960          ccs_assert_read_lock();          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1961          if (!head->read_eof) {                  struct ccs_group *group =
1962                  switch (head->read_step) {                          list_entry(head->r.group, typeof(*group), head.list);
1963                  case 0:                  list_for_each_cookie(head->r.acl, &group->member_list) {
1964                          head->read_var2 = NULL;                          struct ccs_acl_head *ptr =
1965                          head->read_step = 1;                                  list_entry(head->r.acl, typeof(*ptr), list);
1966                  case 1:                          if (ptr->is_deleted)
1967                          if (!ccs_read_domain_keeper_policy(head))                                  continue;
1968                                  break;                          if (!ccs_flush(head))
1969                          head->read_var2 = NULL;                                  return false;
1970                          head->read_step = 2;                          ccs_set_string(head, ccs_group_name[idx]);
1971                  case 2:                          ccs_set_string(head, group->group_name->name);
1972                          if (!ccs_read_globally_readable_policy(head))                          if (idx == CCS_PATH_GROUP) {
1973                                  break;                                  ccs_set_space(head);
1974                          head->read_var2 = NULL;                                  ccs_set_string(head, container_of
1975                          head->read_step = 3;                                                 (ptr, struct ccs_path_group,
1976                  case 3:                                                  head)->member_name->name);
1977                          if (!ccs_read_globally_usable_env_policy(head))                          } else if (idx == CCS_NUMBER_GROUP) {
1978                                  break;                                  ccs_print_number_union(head, &container_of
1979                          head->read_var2 = NULL;                                                 (ptr, struct ccs_number_group,
1980                          head->read_step = 4;                                                  head)->number);
1981                  case 4:                          } else if (idx == CCS_ADDRESS_GROUP) {
1982                          if (!ccs_read_domain_initializer_policy(head))                                  char buffer[128];
1983                                  break;                                  struct ccs_address_group *member =
1984                          head->read_var2 = NULL;                                          container_of(ptr, typeof(*member),
1985                          head->read_step = 6;                                                       head);
1986                  case 6:                                  if (member->is_ipv6)
1987                          if (!ccs_read_aggregator_policy(head))                                          ccs_print_ipv6(buffer, sizeof(buffer),
1988                                  break;                                                         member->min.ipv6,
1989                          head->read_var2 = NULL;                                                         member->max.ipv6);
1990                          head->read_step = 7;                                  else
1991                  case 7:                                          ccs_print_ipv4(buffer, sizeof(buffer),
1992                          if (!ccs_read_file_pattern(head))                                                         member->min.ipv4,
1993                                  break;                                                         member->max.ipv4);
1994                          head->read_var2 = NULL;                                  ccs_io_printf(head, " %s", buffer);
1995                          head->read_step = 8;                          }
1996                  case 8:                          ccs_set_lf(head);
                         if (!ccs_read_no_rewrite_policy(head))  
                                 break;  
                         head->read_var2 = NULL;  
                         head->read_step = 9;  
                 case 9:  
                         if (!ccs_read_path_group_policy(head))  
                                 break;  
                         head->read_var1 = NULL;  
                         head->read_var2 = NULL;  
                         head->read_step = 10;  
                 case 10:  
                         if (!ccs_read_number_group_policy(head))  
                                 break;  
                         head->read_var1 = NULL;  
                         head->read_var2 = NULL;  
                         head->read_step = 11;  
                 case 11:  
                         if (!ccs_read_address_group_policy(head))  
                                 break;  
                         head->read_var2 = NULL;  
                         head->read_step = 12;  
                 case 12:  
                         if (!ccs_read_reserved_port_policy(head))  
                                 break;  
                         head->read_eof = true;  
                         break;  
                 default:  
                         return -EINVAL;  
1997                  }                  }
1998                    head->r.acl = NULL;
1999          }          }
2000          return 0;          head->r.group = NULL;
2001            return true;
2002  }  }
2003    
2004  /**  /**
2005   * ccs_get_argv0 - Get argv[0].   * ccs_read_policy - Read "struct ccs_..._entry" list.
2006   *   *
2007   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
2008     * @idx:  Index number.
2009   *   *
2010   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
2011     *
2012     * Caller holds ccs_read_lock().
2013   */   */
2014  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
2015  {  {
2016          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
2017          char *arg_ptr = ee->tmp;                  struct ccs_acl_head *acl =
2018          int arg_len = 0;                          container_of(head->r.acl, typeof(*acl), list);
2019          unsigned long pos = bprm->p;                  if (acl->is_deleted)
2020          int offset = pos % PAGE_SIZE;                          continue;
2021          bool done = false;                  if (head->r.print_transition_related_only &&
2022          if (!bprm->argc)                      idx != CCS_ID_TRANSITION_CONTROL)
2023                  goto out;                          continue;
2024          while (1) {                  if (!ccs_flush(head))
2025                  if (!ccs_dump_page(bprm, pos, &ee->dump))                          return false;
2026                          goto out;                  switch (idx) {
2027                  pos += PAGE_SIZE - offset;                  case CCS_ID_TRANSITION_CONTROL:
2028                  /* Read. */                          {
2029                  while (offset < PAGE_SIZE) {                                  struct ccs_transition_control *ptr =
2030                          const char *kaddr = ee->dump.data;                                          container_of(acl, typeof(*ptr), head);
2031                          const unsigned char c = kaddr[offset++];                                  ccs_set_string(head,
2032                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                 ccs_transition_type[ptr->type]);
2033                                  if (c == '\\') {                                  ccs_set_string(head, ptr->program ?
2034                                          arg_ptr[arg_len++] = '\\';                                                 ptr->program->name : "any");
2035                                          arg_ptr[arg_len++] = '\\';                                  ccs_set_string(head, " from ");
2036                                  } else if (c == '/') {                                  ccs_set_string(head, ptr->domainname ?
2037                                          arg_len = 0;                                                 ptr->domainname->name : "any");
                                 } else if (c > ' ' && c < 127) {  
                                         arg_ptr[arg_len++] = c;  
                                 } else {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = (c >> 6) + '0';  
                                         arg_ptr[arg_len++]  
                                                 = ((c >> 3) & 7) + '0';  
                                         arg_ptr[arg_len++] = (c & 7) + '0';  
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
2038                          }                          }
                 }  
                 offset = 0;  
                 if (done)  
2039                          break;                          break;
2040                    case CCS_ID_AGGREGATOR:
2041                            {
2042                                    struct ccs_aggregator *ptr =
2043                                            container_of(acl, typeof(*ptr), head);
2044                                    ccs_set_string(head, "aggregator ");
2045                                    ccs_set_string(head, ptr->original_name->name);
2046                                    ccs_set_space(head);
2047                                    ccs_set_string(head,
2048                                                   ptr->aggregated_name->name);
2049                            }
2050                            break;
2051                    case CCS_ID_RESERVEDPORT:
2052                            {
2053                                    struct ccs_reserved *ptr =
2054                                            container_of(acl, typeof(*ptr), head);
2055                                    const u16 min_port = ptr->min_port;
2056                                    const u16 max_port = ptr->max_port;
2057                                    ccs_set_string(head, "deny_autobind ");
2058                                    ccs_io_printf(head, "%u", min_port);
2059                                    if (min_port != max_port)
2060                                            ccs_io_printf(head, "-%u", max_port);
2061                            }
2062                            break;
2063                    default:
2064                            continue;
2065                    }
2066                    ccs_set_lf(head);
2067          }          }
2068            head->r.acl = NULL;
2069          return true;          return true;
  out:  
         return false;  
2070  }  }
2071    
2072  static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry  /**
2073                                                         *ee)   * ccs_read_exception - Read exception policy.
2074     *
2075     * @head: Pointer to "struct ccs_io_buffer".
2076     *
2077     * Returns nothing.
2078     *
2079     * Caller holds ccs_read_lock().
2080     */
2081    static void ccs_read_exception(struct ccs_io_buffer *head)
2082  {  {
2083          struct ccs_condition *cond;          if (head->r.eof)
2084          char *buf;                  return;
2085          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
2086          char *realpath = NULL;                 ccs_read_policy(head, head->r.step))
2087          char *argv0 = NULL;                  head->r.step++;
2088          if (ccs_flags(NULL, CCS_AUTOLEARN_EXEC_REALPATH)) {          if (head->r.step < CCS_MAX_POLICY)
2089                  struct file *file = ee->bprm->file;                  return;
2090  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2091                  struct path path = { file->f_vfsmnt, file->f_dentry };                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
2092                  realpath = ccs_realpath_from_path(&path);                  head->r.step++;
2093  #else          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2094                  realpath = ccs_realpath_from_path(&file->f_path);                  return;
2095  #endif          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
2096                  if (realpath)                 + CCS_MAX_ACL_GROUPS * 2) {
2097                          len += strlen(realpath) + 17;                  head->r.acl_group_index = (head->r.step - CCS_MAX_POLICY
2098                                               - CCS_MAX_GROUP) / 2;
2099                    if (!ccs_read_domain2(head,
2100                                          &ccs_acl_group[head->r.acl_group_index],
2101                                          head->r.step & 1))
2102                            return;
2103                    head->r.step++;
2104          }          }
2105          if (ccs_flags(NULL, CCS_AUTOLEARN_EXEC_REALPATH)) {          head->r.eof = true;
                 if (ccs_get_argv0(ee)) {  
                         argv0 = ee->tmp;  
                         len += strlen(argv0) + 16;  
                 }  
         }  
         buf = kmalloc(len, GFP_KERNEL);  
         if (!buf)  
                 return NULL;  
         snprintf(buf, len - 1, "if");  
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (realpath) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",  
                          realpath);  
                 kfree(realpath);  
         }  
         if (argv0) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",  
                          argv0);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
2106  }  }
2107    
2108  /* Wait queue for ccs_query_list. */  /* Wait queue for kernel -> userspace notification. */
2109  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2110    /* Wait queue for userspace -> kernel notification. */
2111  /* Lock for manipulating ccs_query_list. */  static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
 static DEFINE_SPINLOCK(ccs_query_list_lock);  
2112    
2113  /* Structure for query. */  /* Structure for query. */
2114  struct ccs_query_entry {  struct ccs_query {
2115          struct list_head list;          struct list_head list;
2116          char *query;          char *query;
2117          int query_len;          size_t query_len;
2118          unsigned int serial;          unsigned int serial;
2119          int timer;          u8 timer;
2120          int answer;          u8 answer;
2121            u8 retry;
2122  };  };
2123    
2124  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
2125  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
2126    
2127    /* Lock for manipulating ccs_query_list. */
2128    static DEFINE_SPINLOCK(ccs_query_list_lock);
2129    
2130  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
2131  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2132