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

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

trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c revision 3275 by kumaneko, Sun Dec 20 07:07:04 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 4769 by kumaneko, Tue Mar 29 06:47:03 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
  *  
  * Version: 1.7.1+   2009/12/20  
  *  
  * This file is applicable to both 2.4.30 and 2.6.11 and later.  
  * See README.ccs for ChangeLog.  
5   *   *
6     * Version: 1.8.1-rc   2011/03/20
7   */   */
8    
9  #include "internal.h"  #include "internal.h"
10    
11  static struct ccs_profile ccs_default_profile = {  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
12          .learning = &ccs_default_profile.preference,  
13          .permissive = &ccs_default_profile.preference,  /**
14          .enforcing = &ccs_default_profile.preference,   * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
15          .audit = &ccs_default_profile.preference,   *
16  #ifdef CONFIG_CCSECURITY_AUDIT   * @wq:        The waitqueue to wait on.
17          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,   * @condition: A C expression for the event to wait for.
18          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,   * @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  #endif
         .preference.audit_task_info = true,  
         .preference.audit_path_info = true,  
         .preference.enforcing_penalty = 0,  
         .preference.enforcing_verbose = true,  
         .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,  
         .preference.learning_verbose = false,  
         .preference.learning_exec_realpath = true,  
         .preference.learning_exec_argv0 = true,  
         .preference.learning_symlink_target = true,  
         .preference.permissive_verbose = true  
 };  
71    
72  /* Profile version. Currently only 20090903 is defined. */  /**
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    
82    
83    /* Profile version. Currently only 20100903 is defined. */
84  static unsigned int ccs_profile_version;  static unsigned int ccs_profile_version;
85    
86  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
87  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
88    
89  /* String table for functionality that takes 4 modes. */  /* String table for operation mode. */
90  static const char *ccs_mode_4[4] = {  const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
91          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED]   = "disabled",
92            [CCS_CONFIG_LEARNING]   = "learning",
93            [CCS_CONFIG_PERMISSIVE] = "permissive",
94            [CCS_CONFIG_ENFORCING]  = "enforcing"
95    };
96    
97    /* String table for /proc/ccs/profile interface. */
98    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  /* String table for /proc/ccs/profile */  /* String table for path operation. */
170  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
171                                      CCS_MAX_CAPABILITY_INDEX +          [CCS_TYPE_EXECUTE]    = "execute",
172                                      CCS_MAX_MAC_CATEGORY_INDEX] = {          [CCS_TYPE_READ]       = "read",
173          [CCS_MAC_FILE_EXECUTE]          [CCS_TYPE_WRITE]      = "write",
174          = "file::execute",          [CCS_TYPE_APPEND]     = "append",
175          [CCS_MAC_FILE_OPEN]          [CCS_TYPE_UNLINK]     = "unlink",
176          = "file::open",          [CCS_TYPE_GETATTR]    = "getattr",
177          [CCS_MAC_FILE_CREATE]          [CCS_TYPE_RMDIR]      = "rmdir",
178          = "file::create",          [CCS_TYPE_TRUNCATE]   = "truncate",
179          [CCS_MAC_FILE_UNLINK]          [CCS_TYPE_SYMLINK]    = "symlink",
180          = "file::unlink",          [CCS_TYPE_CHROOT]     = "chroot",
181          [CCS_MAC_FILE_MKDIR]          [CCS_TYPE_UMOUNT]     = "unmount",
182          = "file::mkdir",  };
183          [CCS_MAC_FILE_RMDIR]  
184          = "file::rmdir",  /* String table for categories. */
185          [CCS_MAC_FILE_MKFIFO]  static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
186          = "file::mkfifo",          [CCS_MAC_CATEGORY_FILE]       = "file",
187          [CCS_MAC_FILE_MKSOCK]          [CCS_MAC_CATEGORY_NETWORK]    = "network",
188          = "file::mksock",          [CCS_MAC_CATEGORY_MISC]       = "misc",
189          [CCS_MAC_FILE_TRUNCATE]          [CCS_MAC_CATEGORY_IPC]        = "ipc",
190          = "file::truncate",          [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
191          [CCS_MAC_FILE_SYMLINK]  };
192          = "file::symlink",  
193          [CCS_MAC_FILE_REWRITE]  /* String table for conditions. */
194          = "file::rewrite",  const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
195          [CCS_MAC_FILE_MKBLOCK]          [CCS_TASK_UID]             = "task.uid",
196          = "file::mkblock",          [CCS_TASK_EUID]            = "task.euid",
197          [CCS_MAC_FILE_MKCHAR]          [CCS_TASK_SUID]            = "task.suid",
198          = "file::mkchar",          [CCS_TASK_FSUID]           = "task.fsuid",
199          [CCS_MAC_FILE_LINK]          [CCS_TASK_GID]             = "task.gid",
200          = "file::link",          [CCS_TASK_EGID]            = "task.egid",
201          [CCS_MAC_FILE_RENAME]          [CCS_TASK_SGID]            = "task.sgid",
202          = "file::rename",          [CCS_TASK_FSGID]           = "task.fsgid",
203          [CCS_MAC_FILE_CHMOD]          [CCS_TASK_PID]             = "task.pid",
204          = "file::chmod",          [CCS_TASK_PPID]            = "task.ppid",
205          [CCS_MAC_FILE_CHOWN]          [CCS_EXEC_ARGC]            = "exec.argc",
206          = "file::chown",          [CCS_EXEC_ENVC]            = "exec.envc",
207          [CCS_MAC_FILE_CHGRP]          [CCS_TYPE_IS_SOCKET]       = "socket",
208          = "file::chgrp",          [CCS_TYPE_IS_SYMLINK]      = "symlink",
209          [CCS_MAC_FILE_IOCTL]          [CCS_TYPE_IS_FILE]         = "file",
210          = "file::ioctl",          [CCS_TYPE_IS_BLOCK_DEV]    = "block",
211          [CCS_MAC_FILE_CHROOT]          [CCS_TYPE_IS_DIRECTORY]    = "directory",
212          = "file::chroot",          [CCS_TYPE_IS_CHAR_DEV]     = "char",
213          [CCS_MAC_FILE_MOUNT]          [CCS_TYPE_IS_FIFO]         = "fifo",
214          = "file::mount",          [CCS_MODE_SETUID]          = "setuid",
215          [CCS_MAC_FILE_UMOUNT]          [CCS_MODE_SETGID]          = "setgid",
216          = "file::umount",          [CCS_MODE_STICKY]          = "sticky",
217          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MODE_OWNER_READ]      = "owner_read",
218          = "file::pivot_root",          [CCS_MODE_OWNER_WRITE]     = "owner_write",
219          [CCS_MAC_ENVIRON]          [CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
220          = "misc::env",          [CCS_MODE_GROUP_READ]      = "group_read",
221          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MODE_GROUP_WRITE]     = "group_write",
222          = "network::inet_udp_bind",          [CCS_MODE_GROUP_EXECUTE]   = "group_execute",
223          [CCS_MAC_NETWORK_UDP_CONNECT]          [CCS_MODE_OTHERS_READ]     = "others_read",
224          = "network::inet_udp_connect",          [CCS_MODE_OTHERS_WRITE]    = "others_write",
225          [CCS_MAC_NETWORK_TCP_BIND]          [CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
226          = "network::inet_tcp_bind",          [CCS_TASK_TYPE]            = "task.type",
227          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
228          = "network::inet_tcp_listen",          [CCS_EXEC_REALPATH]        = "exec.realpath",
229          [CCS_MAC_NETWORK_TCP_CONNECT]          [CCS_SYMLINK_TARGET]       = "symlink.target",
230          = "network::inet_tcp_connect",          [CCS_PATH1_UID]            = "path1.uid",
231          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_PATH1_GID]            = "path1.gid",
232          = "network::inet_tcp_accept",          [CCS_PATH1_INO]            = "path1.ino",
233          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_PATH1_MAJOR]          = "path1.major",
234          = "network::inet_raw_bind",          [CCS_PATH1_MINOR]          = "path1.minor",
235          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_PATH1_PERM]           = "path1.perm",
236          = "network::inet_raw_connect",          [CCS_PATH1_TYPE]           = "path1.type",
237          [CCS_MAC_SIGNAL]          [CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
238          = "ipc::signal",          [CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
239          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]          [CCS_PATH2_UID]            = "path2.uid",
240          = "capability::inet_tcp_create",          [CCS_PATH2_GID]            = "path2.gid",
241          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]          [CCS_PATH2_INO]            = "path2.ino",
242          = "capability::inet_tcp_listen",          [CCS_PATH2_MAJOR]          = "path2.major",
243          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]          [CCS_PATH2_MINOR]          = "path2.minor",
244          = "capability::inet_tcp_connect",          [CCS_PATH2_PERM]           = "path2.perm",
245          [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]          [CCS_PATH2_TYPE]           = "path2.type",
246          = "capability::use_inet_udp",          [CCS_PATH2_DEV_MAJOR]      = "path2.dev_major",
247          [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]          [CCS_PATH2_DEV_MINOR]      = "path2.dev_minor",
248          = "capability::use_inet_ip",          [CCS_PATH1_PARENT_UID]     = "path1.parent.uid",
249          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]          [CCS_PATH1_PARENT_GID]     = "path1.parent.gid",
250          = "capability::use_route",          [CCS_PATH1_PARENT_INO]     = "path1.parent.ino",
251          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_PATH1_PARENT_PERM]    = "path1.parent.perm",
252          = "capability::use_packet",          [CCS_PATH2_PARENT_UID]     = "path2.parent.uid",
253          [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]          [CCS_PATH2_PARENT_GID]     = "path2.parent.gid",
254          = "capability::SYS_MOUNT",          [CCS_PATH2_PARENT_INO]     = "path2.parent.ino",
255          [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]          [CCS_PATH2_PARENT_PERM]    = "path2.parent.perm",
256          = "capability::SYS_UMOUNT",  };
257          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]  
258          = "capability::SYS_REBOOT",  /* String table for PREFERENCE keyword. */
259          [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]  static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
260          = "capability::SYS_CHROOT",          [CCS_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
261          [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]          [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
262          = "capability::SYS_KILL",          [CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
         [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]  
         = "capability::SYS_VHANGUP",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]  
         = "capability::SYS_TIME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]  
         = "capability::SYS_NICE",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]  
         = "capability::SYS_SETHOSTNAME",  
         [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]  
         = "capability::use_kernel_module",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]  
         = "capability::create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]  
         = "capability::create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]  
         = "capability::create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]  
         = "capability::create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]  
         = "capability::SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]  
         = "capability::SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]  
         = "capability::SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]  
         = "capability::SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]  
         = "capability::SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]  
         = "capability::SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]  
         = "capability::SYS_IOCTL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]  
         = "capability::SYS_KEXEC_LOAD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]  
         = "capability::SYS_PTRACE",  
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_FILE] = "file",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_NETWORK] = "network",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_MISC] = "misc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_IPC] = "ipc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_CAPABILITY] = "capability",  
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] + 12 : 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_yesno - Return "yes" or "no".   * ccs_addprintf - strncat()-like-snprintf().
286   *   *
287   * @value: Bool value.   * @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 nothing.
292   */   */
293  static const char *ccs_yesno(const unsigned int value)  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
294  {  {
295          return value ? "yes" : "no";          va_list args;
296            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_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
304   *   *
305   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
306   *   *
307   * Returns true on success, false otherwise.   * 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    /**
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 - printf() to "struct ccs_io_buffer" structure.
371     *
372     * @head: Pointer to "struct ccs_io_buffer".
373     * @fmt:  The printf()'s format string, followed by parameters.
374   *   *
375   * The snprintf() will truncate, but ccs_io_printf() won't.   * Returns nothing.
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;
# Line 260  static struct ccs_profile *ccs_find_or_a Line 446  static struct ccs_profile *ccs_find_or_a
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;
                 ptr->audit = &ccs_default_profile.preference;  
                 ptr->learning = &ccs_default_profile.preference;  
                 ptr->permissive = &ccs_default_profile.preference;  
                 ptr->enforcing = &ccs_default_profile.preference;  
455                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
456                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
457                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
458                         sizeof(ptr->config));                         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.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
475     *
476     * Returns nothing.
477   */   */
478  void ccs_check_profile(void)  static void ccs_check_profile(void)
479  {  {
480          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
481            const int idx = ccs_read_lock();
482          ccs_policy_loaded = true;          ccs_policy_loaded = true;
483          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
484                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
485                  if (ccs_profile_ptr[profile])                  if (ccs_profile_ptr[profile])
486                          continue;                          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",                  panic("Profile %u (used by '%s') not defined.\n",
492                        profile, domain->domainname->name);                        profile, domain->domainname->name);
493          }          }
494          if (ccs_profile_version != 20090903)          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",                  panic("Profile version %u is not supported.\n",
501                        ccs_profile_version);                        ccs_profile_version);
502            }
503            printk(KERN_INFO "CCSecurity: 1.8.1-rc   2011/03/20\n");
504            printk(KERN_INFO "Mandatory Access Control activated.\n");
505  }  }
506    
507  /**  /**
# Line 310  void ccs_check_profile(void) Line 513  void ccs_check_profile(void)
513   */   */
514  struct ccs_profile *ccs_profile(const u8 profile)  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];          struct ccs_profile *ptr = ccs_profile_ptr[profile];
518          if (!ccs_policy_loaded)          if (!ptr)
519                  return &ccs_default_profile;                  ptr = &ccs_null_profile;
         BUG_ON(!ptr);  
520          return ptr;          return ptr;
521  }  }
522    
523  /**  /**
524   * ccs_write_profile - Write profile table.   * ccs_find_yesno - Find values for specified keyword.
525   *   *
526   * @head: Pointer to "struct ccs_io_buffer".   * @string: String to check.
527     * @find:   Name of keyword.
528   *   *
529   * Returns 0 on success, negative value otherwise.   * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
530   */   */
531  static int ccs_write_profile(struct ccs_io_buffer *head)  static s8 ccs_find_yesno(const char *string, const char *find)
532  {  {
533          char *data = head->write_buf;          const char *cp = strstr(string, find);
534          unsigned int i;          if (cp) {
535          int value;                  cp += strlen(find);
536          int mode;                  if (!strncmp(cp, "=yes", 4))
537          u8 config;                          return 1;
538          bool use_default = false;                  else if (!strncmp(cp, "=no", 3))
         char *cp;  
         struct ccs_profile *profile;  
         if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)  
                 return 0;  
         i = simple_strtoul(data, &cp, 10);  
         if (data == cp) {  
                 profile = &ccs_default_profile;  
         } else {  
                 if (*cp != '-')  
                         return -EINVAL;  
                 data = cp + 1;  
                 profile = ccs_find_or_assign_new_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
         }  
         cp = strchr(data, '=');  
         if (!cp)  
                 return -EINVAL;  
         *cp++ = '\0';  
         if (profile != &ccs_default_profile)  
                 use_default = strstr(cp, "use_default") != NULL;  
         if (strstr(cp, "verbose=yes"))  
                 value = 1;  
         else if (strstr(cp, "verbose=no"))  
                 value = 0;  
         else  
                 value = -1;  
         if (!strcmp(data, "PREFERENCE::audit")) {  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 char *cp2;  
 #endif  
                 if (use_default) {  
                         profile->audit = &ccs_default_profile.preference;  
                         return 0;  
                 }  
                 profile->audit = &profile->preference;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 cp2 = strstr(cp, "max_grant_log=");  
                 if (cp2)  
                         sscanf(cp2 + 14, "%u",  
                                &profile->preference.audit_max_grant_log);  
                 cp2 = strstr(cp, "max_reject_log=");  
                 if (cp2)  
                         sscanf(cp2 + 15, "%u",  
                                &profile->preference.audit_max_reject_log);  
 #endif  
                 if (strstr(cp, "task_info=yes"))  
                         profile->preference.audit_task_info = true;  
                 else if (strstr(cp, "task_info=no"))  
                         profile->preference.audit_task_info = false;  
                 if (strstr(cp, "path_info=yes"))  
                         profile->preference.audit_path_info = true;  
                 else if (strstr(cp, "path_info=no"))  
                         profile->preference.audit_path_info = false;  
                 return 0;  
         }  
         if (!strcmp(data, "PREFERENCE::enforcing")) {  
                 char *cp2;  
                 if (use_default) {  
                         profile->enforcing = &ccs_default_profile.preference;  
                         return 0;  
                 }  
                 profile->enforcing = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.enforcing_verbose = value;  
                 cp2 = strstr(cp, "penalty=");  
                 if (cp2)  
                         sscanf(cp2 + 8, "%u",  
                                &profile->preference.enforcing_penalty);  
                 return 0;  
         }  
         if (!strcmp(data, "PREFERENCE::permissive")) {  
                 if (use_default) {  
                         profile->permissive = &ccs_default_profile.preference;  
539                          return 0;                          return 0;
                 }  
                 profile->permissive = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.permissive_verbose = value;  
                 return 0;  
540          }          }
541          if (!strcmp(data, "PREFERENCE::learning")) {          return -1;
542                  char *cp2;  }
543                  if (use_default) {  
544                          profile->learning = &ccs_default_profile.preference;  /**
545                          return 0;   * ccs_set_uint - Set value for specified preference.
546                  }   *
547                  profile->learning = &profile->preference;   * @i:      Pointer to "unsigned int".
548                  if (value >= 0)   * @string: String to check.
549                          profile->preference.learning_verbose = value;   * @find:   Name of keyword.
550                  cp2 = strstr(cp, "max_entry=");   *
551                  if (cp2)   * Returns nothing.
552                          sscanf(cp2 + 10, "%u",   */
553                                 &profile->preference.learning_max_entry);  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
554                  if (strstr(cp, "exec.realpath=yes"))  {
555                          profile->preference.learning_exec_realpath = true;          const char *cp = strstr(string, find);
556                  else if (strstr(cp, "exec.realpath=no"))          if (cp)
557                          profile->preference.learning_exec_realpath = false;                  sscanf(cp + strlen(find), "=%u", i);
558                  if (strstr(cp, "exec.argv0=yes"))  }
559                          profile->preference.learning_exec_argv0 = true;  
560                  else if (strstr(cp, "exec.argv0=no"))  /**
561                          profile->preference.learning_exec_argv0 = false;   * ccs_set_mode - Set mode for specified profile.
562                  if (strstr(cp, "symlink.target=yes"))   *
563                          profile->preference.learning_symlink_target = true;   * @name:    Name of functionality.
564                  else if (strstr(cp, "symlink.target=no"))   * @value:   Mode for @name.
565                          profile->preference.learning_symlink_target = false;   * @profile: Pointer to "struct ccs_profile".
566                  return 0;   *
567          }   * Returns 0 on success, negative value otherwise.
568          if (profile == &ccs_default_profile)   */
569                  return -EINVAL;  static int ccs_set_mode(char *name, const char *value,
570          if (!strcmp(data, "COMMENT")) {                          struct ccs_profile *profile)
571                  const struct ccs_path_info *old_comment = profile->comment;  {
572                  profile->comment = ccs_get_name(cp);          u8 i;
573                  ccs_put_name(old_comment);          u8 config;
574                  return 0;          if (!strcmp(name, "CONFIG")) {
575          }                  i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
         if (!strcmp(data, "CONFIG")) {  
                 i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
                         + CCS_MAX_MAC_CATEGORY_INDEX;  
576                  config = profile->default_config;                  config = profile->default_config;
577          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
578                  config = 0;                  config = 0;
579                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
580                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                       i++) {
581                          if (strcmp(data, ccs_mac_keywords[i]))                          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;                                  continue;
593                          config = profile->config[i];                          config = profile->config[i];
594                          break;                          break;
595                  }                  }
596                  if (i == CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
                     + CCS_MAX_MAC_CATEGORY_INDEX)  
597                          return -EINVAL;                          return -EINVAL;
598          } else {          } else {
599                  return -EINVAL;                  return -EINVAL;
600          }          }
601          if (use_default) {          if (strstr(value, "use_default")) {
602                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
603          } else {          } else {
604                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
605                          if (strstr(cp, ccs_mode_4[mode]))                  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                                   * Update lower 3 bits in order to distinguish
609                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
610                                   */                                   */
611                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
612                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
613                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "grant_log")) {
614                            case 1:
615                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
616                          else if (strstr(cp, "grant_log=no"))                                  break;
617                            case 0:
618                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
619                          if (strstr(cp, "reject_log=yes"))                                  break;
620                            }
621                            switch (ccs_find_yesno(value, "reject_log")) {
622                            case 1:
623                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
624                          else if (strstr(cp, "reject_log=no"))                                  break;
625                            case 0:
626                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
627                                    break;
628                            }
629                  }                  }
 #endif  
630          }          }
631          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
             + CCS_MAX_MAC_CATEGORY_INDEX)  
632                  profile->config[i] = config;                  profile->config[i] = config;
633          else if (config != CCS_CONFIG_USE_DEFAULT)          else if (config != CCS_CONFIG_USE_DEFAULT)
634                  profile->default_config = config;                  profile->default_config = config;
# Line 499  static int ccs_write_profile(struct ccs_ Line 636  static int ccs_write_profile(struct ccs_
636  }  }
637    
638  /**  /**
639     * ccs_write_profile - Write profile table.
640     *
641     * @head: Pointer to "struct ccs_io_buffer".
642     *
643     * Returns 0 on success, negative value otherwise.
644     */
645    static int ccs_write_profile(struct ccs_io_buffer *head)
646    {
647            char *data = head->write_buf;
648            unsigned int i;
649            char *cp;
650            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);
654            if (*cp != '-')
655                    return -EINVAL;
656            data = cp + 1;
657            profile = ccs_assign_profile(i);
658            if (!profile)
659                    return -EINVAL;
660            cp = strchr(data, '=');
661            if (!cp)
662                    return -EINVAL;
663            *cp++ = '\0';
664            if (!strcmp(data, "COMMENT")) {
665                    const struct ccs_path_info *old_comment = profile->comment;
666                    profile->comment = ccs_get_name(cp);
667                    ccs_put_name(old_comment);
668                    return 0;
669            }
670            if (!strcmp(data, "PREFERENCE")) {
671                    for (i = 0; i < CCS_MAX_PREF; i++)
672                            ccs_set_uint(&profile->pref[i], cp,
673                                         ccs_pref_keywords[i]);
674                    return 0;
675            }
676            return ccs_set_mode(data, cp, profile);
677    }
678    
679    /**
680     * ccs_print_config - Print mode for specified functionality.
681     *
682     * @head:   Pointer to "struct ccs_io_buffer".
683     * @config: Mode for that functionality.
684     *
685     * Returns nothing.
686     *
687     * Caller prints functionality's name.
688     */
689    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
690    {
691            ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
692                          ccs_mode[config & 3],
693                          ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
694                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
695    }
696    
697    /**
698   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
699   *   *
700   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
701     *
702     * Returns nothing.
703   */   */
704  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
705  {  {
706          int index;          u8 index;
707          if (head->read_eof)          const struct ccs_profile *profile;
708                  return;  next:
709          if (head->read_bit)          index = head->r.index;
710                  goto body;          profile = ccs_profile_ptr[index];
711          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          switch (head->r.step) {
712          ccs_io_printf(head, "PREFERENCE::audit={ "          case 0:
713  #ifdef CONFIG_CCSECURITY_AUDIT                  ccs_io_printf(head, "PROFILE_VERSION=%u\n", 20100903);
714                        "max_grant_log=%u max_reject_log=%u "                  head->r.step++;
715  #endif                  break;
716                        "task_info=%s path_info=%s }\n",          case 1:
717  #ifdef CONFIG_CCSECURITY_AUDIT                  for ( ; head->r.index < CCS_MAX_PROFILES;
718                        ccs_default_profile.preference.audit_max_grant_log,                        head->r.index++)
719                        ccs_default_profile.preference.audit_max_reject_log,                          if (ccs_profile_ptr[head->r.index])
720  #endif                                  break;
721                        ccs_yesno(ccs_default_profile.preference.                  if (head->r.index == CCS_MAX_PROFILES)
722                                  audit_task_info),                          return;
723                        ccs_yesno(ccs_default_profile.preference.                  head->r.step++;
724                                  audit_path_info));                  break;
725          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "          case 2:
726                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",                  {
727                        ccs_yesno(ccs_default_profile.preference.                          u8 i;
728                                  learning_verbose),                          const struct ccs_path_info *comment = profile->comment;
729                        ccs_default_profile.preference.learning_max_entry,                          ccs_io_printf(head, "%u-COMMENT=", index);
730                        ccs_yesno(ccs_default_profile.preference.                          ccs_set_string(head, comment ? comment->name : "");
731                                  learning_exec_realpath),                          ccs_set_lf(head);
732                        ccs_yesno(ccs_default_profile.preference.                          ccs_io_printf(head, "%u-PREFERENCE={ ", index);
733                                  learning_exec_argv0),                          for (i = 0; i < CCS_MAX_PREF; i++)
734                        ccs_yesno(ccs_default_profile.preference.                                  ccs_io_printf(head, "%s=%u ",
735                                  learning_symlink_target));                                                ccs_pref_keywords[i],
736          ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",                                                profile->pref[i]);
737                        ccs_yesno(ccs_default_profile.preference.                          ccs_set_string(head, " }\n");
738                                  permissive_verbose));                          head->r.step++;
739          ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "                  }
740                        "}\n",                  break;
741                        ccs_yesno(ccs_default_profile.preference.          case 3:
742                                  enforcing_verbose),                  {
743                        ccs_default_profile.preference.enforcing_penalty);                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
744          head->read_bit = 1;                          ccs_print_config(head, profile->default_config);
745   body:                          head->r.bit = 0;
746          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {                          head->r.step++;
747                  bool done;                  }
748                  u8 config;                  break;
749                  int i;          case 4:
750                  int pos;                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
751                  const struct ccs_profile *profile = ccs_profile_ptr[index];                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
752                  const struct ccs_path_info *comment;                          const u8 i = head->r.bit;
753                  head->read_step = index;                          const u8 config = profile->config[i];
                 if (!profile)  
                         continue;  
                 pos = head->read_avail;  
                 comment = profile->comment;  
                 done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,  
                                      comment ? comment->name : "");  
                 if (!done)  
                         goto out;  
                 config = profile->default_config;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "  
                                    "reject_log=%s }\n", index,  
                                    ccs_mode_4[config & 3],  
                                    ccs_yesno(config &  
                                              CCS_CONFIG_WANT_GRANT_LOG),  
                                    ccs_yesno(config &  
                                              CCS_CONFIG_WANT_REJECT_LOG)))  
                         goto out;  
 #else  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,  
                                    ccs_mode_4[config & 3]))  
                         goto out;  
 #endif  
                 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
                              + CCS_MAX_MAC_CATEGORY_INDEX; i++) {  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                         const char *g;  
                         const char *r;  
 #endif  
                         config = profile->config[i];  
754                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
755                                  continue;                                  continue;
756  #ifdef CONFIG_CCSECURITY_AUDIT                          if (i < CCS_MAX_MAC_INDEX)
757                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                  ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
758                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                                                ccs_category_keywords
759                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                                                [ccs_index2category[i]],
760                                             "grant_log=%s reject_log=%s }\n",                                                ccs_mac_keywords[i]);
761                                             index, ccs_mac_keywords[i],                          else
762                                             ccs_mode_4[config & 3], g, r))                                  ccs_io_printf(head, "%u-CONFIG::%s", index,
763                                  goto out;                                                ccs_mac_keywords[i]);
764  #else                          ccs_print_config(head, config);
765                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          head->r.bit++;
766                                             index, ccs_mac_keywords[i],                          break;
767                                             ccs_mode_4[config & 3]))                  }
768                                  goto out;                  if (head->r.bit == CCS_MAX_MAC_INDEX
769  #endif                      + CCS_MAX_MAC_CATEGORY_INDEX) {
770                            head->r.index++;
771                            head->r.step = 1;
772                  }                  }
                 if (profile->audit != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::audit={ "  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    "max_grant_log=%u max_reject_log=%u "  
 #endif  
                                    "task_info=%s path_info=%s }\n", index,  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    profile->preference.audit_max_grant_log,  
                                    profile->preference.audit_max_reject_log,  
 #endif  
                                    ccs_yesno(profile->preference.  
                                              audit_task_info),  
                                    ccs_yesno(profile->preference.  
                                              audit_path_info)))  
                         goto out;  
                 if (profile->learning != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::learning={ "  
                                    "verbose=%s max_entry=%u exec.realpath=%s "  
                                    "exec.argv0=%s symlink.target=%s }\n",  
                                    index,  
                                    ccs_yesno(profile->preference.  
                                              learning_verbose),  
                                    profile->preference.learning_max_entry,  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_realpath),  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_argv0),  
                                    ccs_yesno(profile->preference.  
                                              learning_symlink_target)))  
                         goto out;  
                 if (profile->permissive != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::permissive={ "  
                                    "verbose=%s }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              permissive_verbose)))  
                         goto out;  
                 if (profile->enforcing != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::enforcing={ "  
                                    "verbose=%s penalty=%u }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              enforcing_verbose),  
                                    profile->preference.enforcing_penalty))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
773                  break;                  break;
774          }          }
775          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
776                  head->read_eof = true;                  goto next;
777  }  }
778    
779  /* The list for "struct ccs_policy_manager_entry". */  /**
780  LIST_HEAD(ccs_policy_manager_list);   * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
781     *
782     * @a: Pointer to "struct ccs_acl_head".
783     * @b: Pointer to "struct ccs_acl_head".
784     *
785     * Returns true if @a == @b, false otherwise.
786     */
787    static bool ccs_same_manager(const struct ccs_acl_head *a,
788                                 const struct ccs_acl_head *b)
789    {
790            return container_of(a, struct ccs_manager, head)->manager
791                    == container_of(b, struct ccs_manager, head)->manager;
792    }
793    
794  /**  /**
795   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 665  LIST_HEAD(ccs_policy_manager_list); Line 801  LIST_HEAD(ccs_policy_manager_list);
801   */   */
802  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)
803  {  {
804          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
805          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
806          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
807                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
808                          return -EINVAL;                          return -EINVAL;
809                  e.is_domain = true;                  e.is_domain = true;
810          } else {          } else {
811                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
812                          return -EINVAL;                          return -EINVAL;
813          }          }
814          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
815          if (!e.manager)          if (!e.manager)
816                  return -ENOMEM;                  return error;
817          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
818                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
819          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);  
820          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
821          return error;          return error;
822  }  }
823    
824  /**  /**
825   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
826   *   *
827   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
828   *   *
829   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
830   */   */
831  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
832  {  {
833          char *data = head->write_buf;          char *data = head->write_buf;
834          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, "delete ");
835          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
836                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
837                  return 0;                  return 0;
# Line 720  static int ccs_write_manager_policy(stru Line 840  static int ccs_write_manager_policy(stru
840  }  }
841    
842  /**  /**
843   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
844   *   *
845   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
846   *   *
847     * Returns nothing.
848     *
849   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
850   */   */
851  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
852  {  {
853          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
854                  return;                  return;
855          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
856                  struct ccs_policy_manager_entry *ptr;                  struct ccs_manager *ptr =
857                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                          list_entry(head->r.acl, typeof(*ptr), head.list);
858                  if (ptr->is_deleted)                  if (ptr->head.is_deleted)
859                          continue;                          continue;
860                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
861                          return;                          return;
862                    ccs_set_string(head, ptr->manager->name);
863                    ccs_set_lf(head);
864          }          }
865          head->read_eof = true;          head->r.eof = true;
866  }  }
867    
868  /**  /**
869   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
870   *   *
871   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
872   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
873   *   *
874   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
875   */   */
876  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
877  {  {
878          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
879          const char *exe;          const char *exe;
880          struct task_struct *task = current;          struct ccs_security *task = ccs_current_security();
881          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
882                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
883          bool found = false;          bool found = false;
884          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
885                  return true;                  return true;
886          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
887                  return true;                  return true;
888          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
889                  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;  
                 }  
         }  
890          exe = ccs_get_exe();          exe = ccs_get_exe();
891          if (!exe)          list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
892                  return false;                                   head.list, &ccs_ss) {
893          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
894                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
895                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
896                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
897                          /* Set manager flag. */                                  continue;
898                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
899                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
900                                    continue;
901                  }                  }
902                    /* Set manager flag. */
903                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
904                    found = true;
905                    break;
906          }          }
907          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
908                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 798  static bool ccs_is_policy_manager(void) Line 918  static bool ccs_is_policy_manager(void)
918  }  }
919    
920  /**  /**
921   * 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.  
922   *   *
923   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
924   * @data: String to parse.   * @data: String to parse.
# Line 834  static char *ccs_find_condition_part(cha Line 927  static char *ccs_find_condition_part(cha
927   *   *
928   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
929   */   */
930  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)
931  {  {
932          unsigned int pid;          unsigned int pid;
933          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
934          bool global_pid = false;          bool global_pid = false;
935          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "transition_only")) {
936                  head->read_execute_only = true;                  head->r.print_transition_related_only = true;
937                  return true;                  return true;
938          }          }
939          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
# Line 849  static bool ccs_is_select_one(struct ccs Line 942  static bool ccs_is_select_one(struct ccs
942                  ccs_tasklist_lock();                  ccs_tasklist_lock();
943  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
944                  if (global_pid)                  if (global_pid)
945                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
946                                                                   &init_pid_ns);
947                  else                  else
948                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
949  #else  #else
950                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
951  #endif  #endif
# Line 859  static bool ccs_is_select_one(struct ccs Line 953  static bool ccs_is_select_one(struct ccs
953                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
954                  ccs_tasklist_unlock();                  ccs_tasklist_unlock();
955          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
956                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
957                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
958          } else          } else
959                  return false;                  return false;
960          head->write_var1 = domain;          head->w.domain = domain;
961          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
962          if (!head->read_buf)          if (!head->read_buf)
963                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
964          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
965            head->r.print_this_domain_only = true;
966            if (domain)
967                    head->r.domain = &domain->list;
968            else
969                    head->r.eof = true;
970          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
971          head->read_single_domain = true;          if (domain && domain->is_deleted)
972          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");  
         }  
973          return true;          return true;
974  }  }
975    
976  static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,  /**
977                                      struct ccs_condition *cond,   * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
978                                      const bool is_delete)   *
979  {   * @a: Pointer to "struct ccs_acl_info".
980          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))   * @b: Pointer to "struct ccs_acl_info".
981                  return ccs_write_capability_policy(data, domain, cond,   *
982                                                     is_delete);   * Returns true if @a == @b, false otherwise.
983          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))   */
984                  return ccs_write_network_policy(data, domain, cond, is_delete);  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
985          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                                   const struct ccs_acl_info *b)
986                  return ccs_write_signal_policy(data, domain, cond, is_delete);  {
987          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
988                  return ccs_write_env_policy(data, domain, cond, is_delete);          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
989          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          return p1->handler == p2->handler;
                 return ccs_write_mount_policy(data, domain, cond, is_delete);  
         return ccs_write_file_policy(data, domain, cond, is_delete);  
990  }  }
991    
992  /**  /**
993   * ccs_write_domain_policy - Write domain policy.   * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
994     *
995     * @a: Pointer to "struct ccs_acl_info".
996     * @b: Pointer to "struct ccs_acl_info".
997     *
998     * Returns true if @a == @b, false otherwise.
999     */
1000    static bool ccs_same_task_acl(const struct ccs_acl_info *a,
1001                                  const struct ccs_acl_info *b)
1002    {
1003            const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
1004            const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
1005            return p1->domainname == p2->domainname;
1006    }
1007    
1008    /**
1009     * ccs_write_task - Update task related list.
1010     *
1011     * @param: Pointer to "struct ccs_acl_param".
1012     *
1013     * Returns 0 on success, negative value otherwise.
1014     */
1015    static int ccs_write_task(struct ccs_acl_param *param)
1016    {
1017            int error;
1018            const bool is_auto = ccs_str_starts(&param->data,
1019                                                "auto_domain_transition ");
1020            if (!is_auto && !ccs_str_starts(&param->data,
1021                                            "manual_domain_transition ")) {
1022                    struct ccs_handler_acl e = { };
1023                    char *handler;
1024                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
1025                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
1026                    else if (ccs_str_starts(&param->data,
1027                                            "denied_execute_handler "))
1028                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
1029                    else
1030                            return -EINVAL;
1031                    handler = ccs_read_token(param);
1032                    if (!ccs_correct_path(handler))
1033                            return -EINVAL;
1034                    e.handler = ccs_get_name(handler);
1035                    if (!e.handler)
1036                            return -ENOMEM;
1037                    if (e.handler->is_patterned)
1038                            error = -EINVAL; /* No patterns allowed. */
1039                    else
1040                            error = ccs_update_domain(&e.head, sizeof(e), param,
1041                                                      ccs_same_handler_acl, NULL);
1042                    ccs_put_name(e.handler);
1043            } else {
1044                    struct ccs_task_acl e = {
1045                            .head.type = is_auto ?
1046                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
1047                            .domainname = ccs_get_domainname(param),
1048                    };
1049                    if (!e.domainname)
1050                            error = -EINVAL;
1051                    else
1052                            error = ccs_update_domain(&e.head, sizeof(e), param,
1053                                                      ccs_same_task_acl, NULL);
1054                    ccs_put_name(e.domainname);
1055            }
1056            return error;
1057    }
1058    
1059    /**
1060     * ccs_write_domain2 - Write domain policy.
1061     *
1062     * @data:      Policy to be interpreted.
1063     * @domain:    Pointer to "struct ccs_domain_info".
1064     * @is_delete: True if it is a delete request.
1065     *
1066     * Returns 0 on success, negative value otherwise.
1067     */
1068    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
1069                                 const bool is_delete)
1070    {
1071            struct ccs_acl_param param = {
1072                    .data = data,
1073                    .domain = domain,
1074                    .is_delete = is_delete,
1075            };
1076            static const struct {
1077                    const char *keyword;
1078                    int (*write) (struct ccs_acl_param *);
1079            } ccs_callback[7] = {
1080                    { "file ", ccs_write_file },
1081                    { "network inet ", ccs_write_inet_network },
1082                    { "network unix ", ccs_write_unix_network },
1083                    { "misc ", ccs_write_misc },
1084                    { "capability ", ccs_write_capability },
1085                    { "ipc ", ccs_write_ipc },
1086                    { "task ", ccs_write_task },
1087            };
1088            u8 i;
1089            for (i = 0; i < 7; i++) {
1090                    if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
1091                            continue;
1092                    return ccs_callback[i].write(&param);
1093            }
1094            return -EINVAL;
1095    }
1096    
1097    /* String table for domain flags. */
1098    const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1099            [CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
1100            [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
1101    };
1102    
1103    /**
1104     * ccs_write_domain - Write domain policy.
1105   *   *
1106   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1107   *   *
1108   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1109   */   */
1110  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1111  {  {
1112          char *data = head->write_buf;          char *data = head->write_buf;
1113          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1114          bool is_delete = false;          bool is_delete = false;
1115          bool is_select = false;          bool is_select = false;
1116          unsigned int profile;          unsigned int profile;
1117          struct ccs_condition *cond = NULL;          if (ccs_str_starts(&data, "delete "))
         char *cp;  
         int error;  
         if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))  
1118                  is_delete = true;                  is_delete = true;
1119          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, "select "))
1120                  is_select = true;                  is_select = true;
1121          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1122                  return 0;                  return -EAGAIN;
1123          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1124          if (!ccs_is_policy_manager())          if (!ccs_manager())
1125                  return -EPERM;                  return -EPERM;
1126          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1127                  domain = NULL;                  domain = NULL;
1128                  if (is_delete)                  if (is_delete)
1129                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1130                  else if (is_select)                  else if (is_select)
1131                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1132                  else                  else
1133                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
1134                  head->write_var1 = domain;                  head->w.domain = domain;
1135                  return 0;                  return 0;
1136          }          }
1137          if (!domain)          if (!domain)
1138                  return -EINVAL;                  return -EINVAL;
1139    
1140          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, "use_profile %u\n", &profile) == 1
1141              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1142                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1143                          domain->profile = (u8) profile;                          if (!is_delete)
1144                                    domain->profile = (u8) profile;
1145                  return 0;                  return 0;
1146          }          }
1147          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, "use_group %u\n", &profile) == 1
1148                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
1149                    if (!is_delete)
1150                            domain->group = (u8) profile;
1151                  return 0;                  return 0;
1152          }          }
1153          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1154                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
1155                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
1156          }                          continue;
1157          if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {                  domain->flags[profile] = !is_delete;
                 domain->quota_warned = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {  
                 domain->domain_transition_failed = !is_delete;  
1158                  return 0;                  return 0;
1159          }          }
1160          cp = ccs_find_condition_part(data);          return ccs_write_domain2(data, domain, is_delete);
         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;  
1161  }  }
1162    
1163  /**  /**
# Line 986  static int ccs_write_domain_policy(struc Line 1166  static int ccs_write_domain_policy(struc
1166   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1167   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
1168   *   *
1169   * Returns true on success, false otherwise.   * Returns nothing.
1170   */   */
1171  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1172                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1173  {  {
1174          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1175          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1176                  head->read_avail--;                  ccs_set_space(head);
1177          if (ptr->is_group)          if (ptr->is_group) {
1178                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1179                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1180          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1181  }                  if (cond)
1182                            ccs_set_string(head, "\"");
1183  /**                  ccs_set_string(head, ptr->filename->name);
1184   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1185   *                          ccs_set_string(head, "\"");
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_name_union".  
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
 }  
   
 /**  
  * ccs_print_number_union_common - Print a ccs_number_union.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  * @need_space: True if a space character is needed.  
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
1186          }          }
1187  }  }
1188    
1189  /**  /**
1190   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1191   *   *
1192   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1193   * @ptr:        Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
 }  
   
 /**  
  * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
1194   *   *
1195   * Returns true on success, false otherwise.   * Returns nothing.
1196   */   */
1197  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1198                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1199  {  {
1200          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
1201                    ccs_set_space(head);
1202            if (ptr->is_group) {
1203                    ccs_set_string(head, "@");
1204                    ccs_set_string(head, ptr->group->group_name->name);
1205            } else {
1206                    int i;
1207                    unsigned long min = ptr->values[0];
1208                    const unsigned long max = ptr->values[1];
1209                    u8 min_type = ptr->value_type[0];
1210                    const u8 max_type = ptr->value_type[1];
1211                    char buffer[128];
1212                    buffer[0] = '\0';
1213                    for (i = 0; i < 2; i++) {
1214                            switch (min_type) {
1215                            case CCS_VALUE_TYPE_HEXADECIMAL:
1216                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1217                                                  min);
1218                                    break;
1219                            case CCS_VALUE_TYPE_OCTAL:
1220                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1221                                                  min);
1222                                    break;
1223                            default:
1224                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1225                                                  min);
1226                                    break;
1227                            }
1228                            if (min == max && min_type == max_type)
1229                                    break;
1230                            ccs_addprintf(buffer, sizeof(buffer), "-");
1231                            min_type = max_type;
1232                            min = max;
1233                    }
1234                    ccs_io_printf(head, "%s", buffer);
1235            }
1236  }  }
1237    
1238  /**  /**
1239   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1240   *   *
1241   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1242   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1243   *   *
1244   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1245   */   */
1246  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1247                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1248  {  {
1249          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1250          const struct ccs_number_union *numbers_p;          case 0:
1251          const struct ccs_name_union *names_p;                  head->r.cond_index = 0;
1252          const struct ccs_argv_entry *argv;                  head->r.cond_step++;
1253          const struct ccs_envp_entry *envp;                  /* fall through */
1254          u16 condc;          case 1:
1255          u16 i;                  {
1256          u16 j;                          const u16 condc = cond->condc;
1257          char buffer[32];                          const struct ccs_condition_element *condp =
1258          if (!cond)                                  (typeof(condp)) (cond + 1);
1259                  goto no_condition;                          const struct ccs_number_union *numbers_p =
1260          condc = cond->condc;                                  (typeof(numbers_p)) (condp + condc);
1261          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_name_union *names_p =
1262          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(names_p))
1263          names_p = (const struct ccs_name_union *)                                  (numbers_p + cond->numbers_count);
1264                  (numbers_p + cond->numbers_count);                          const struct ccs_argv *argv =
1265          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                                  (typeof(argv)) (names_p + cond->names_count);
1266          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                          const struct ccs_envp *envp =
1267          memset(buffer, 0, sizeof(buffer));                                  (typeof(envp)) (argv + cond->argc);
1268          if (condc && !ccs_io_printf(head, "%s", " if"))                          u16 skip;
1269                  goto out;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1270          for (i = 0; i < condc; i++) {                                  const u8 left = condp->left;
1271                  const u8 match = condp->equals;                                  const u8 right = condp->right;
1272                  const u8 left = condp->left;                                  condp++;
1273                  const u8 right = condp->right;                                  switch (left) {
1274                  condp++;                                  case CCS_ARGV_ENTRY:
1275                  switch (left) {                                          argv++;
1276                  case CCS_ARGV_ENTRY:                                          continue;
1277                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  case CCS_ENVP_ENTRY:
1278                                             argv->index, argv->is_not ?                                          envp++;
1279                                             "!=" : "=", argv->value->name))                                          continue;
1280                                  goto out;                                  case CCS_NUMBER_UNION:
1281                          argv++;                                          numbers_p++;
1282                          continue;                                          break;
1283                  case CCS_ENVP_ENTRY:                                  }
1284                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  switch (right) {
1285                                             envp->name->name, envp->is_not ?                                  case CCS_NAME_UNION:
1286                                             "!=" : "="))                                          names_p++;
1287                                  goto out;                                          break;
1288                          if (envp->value) {                                  case CCS_NUMBER_UNION:
1289                                  if (!ccs_io_printf(head, "\"%s\"",                                          numbers_p++;
1290                                                     envp->value->name))                                          break;
1291                                          goto out;                                  }
1292                          } else {                          }
1293                                  if (!ccs_io_printf(head, "NULL"))                          while (head->r.cond_index < condc) {
1294                                          goto out;                                  const u8 match = condp->equals;
1295                                    const u8 left = condp->left;
1296                                    const u8 right = condp->right;
1297                                    if (!ccs_flush(head))
1298                                            return false;
1299                                    condp++;
1300                                    head->r.cond_index++;
1301                                    ccs_set_space(head);
1302                                    switch (left) {
1303                                    case CCS_ARGV_ENTRY:
1304                                            ccs_io_printf(head,
1305                                                          "exec.argv[%u]%s\"%s\"",
1306                                                          argv->index,
1307                                                          argv->is_not ?
1308                                                          "!=" : "=",
1309                                                          argv->value->name);
1310                                            argv++;
1311                                            continue;
1312                                    case CCS_ENVP_ENTRY:
1313                                            ccs_io_printf(head,
1314                                                          "exec.envp[\"%s\"]%s",
1315                                                          envp->name->name,
1316                                                          envp->is_not ?
1317                                                          "!=" : "=");
1318                                            if (envp->value) {
1319                                                    ccs_set_string(head, "\"");
1320                                                    ccs_set_string(head, envp->
1321                                                                   value->name);
1322                                                    ccs_set_string(head, "\"");
1323                                            } else {
1324                                                    ccs_set_string(head, "NULL");
1325                                            }
1326                                            envp++;
1327                                            continue;
1328                                    case CCS_NUMBER_UNION:
1329                                            ccs_print_number_union(head,
1330                                                                   numbers_p++);
1331                                            break;
1332                                    default:
1333                                            ccs_set_string(head,
1334                                                   ccs_condition_keyword[left]);
1335                                            break;
1336                                    }
1337                                    ccs_set_string(head, match ? "=" : "!=");
1338                                    switch (right) {
1339                                    case CCS_NAME_UNION:
1340                                            ccs_print_name_union(head, names_p++);
1341                                            break;
1342                                    case CCS_NUMBER_UNION:
1343                                            ccs_print_number_union(head,
1344                                                                   numbers_p++);
1345                                            break;
1346                                    default:
1347                                            ccs_set_string(head,
1348                                                   ccs_condition_keyword[right]);
1349                                            break;
1350                                    }
1351                          }                          }
                         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;  
1352                  }                  }
1353                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1354                          goto out;                  /* fall through */
1355                  switch (right) {          case 2:
1356                  case CCS_NAME_UNION:                  if (!ccs_flush(head))
                         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;  
1357                          break;                          break;
1358                    head->r.cond_step++;
1359                    /* fall through */
1360            case 3:
1361                    if (cond->grant_log != CCS_GRANTLOG_AUTO)
1362                            ccs_io_printf(head, " grant_log=%s",
1363                                          ccs_yesno(cond->grant_log ==
1364                                                    CCS_GRANTLOG_YES));
1365                    if (cond->transit) {
1366                            ccs_set_string(head, " auto_domain_transition=\"");
1367                            ccs_set_string(head, cond->transit->name);
1368                            ccs_set_string(head, "\"");
1369                  }                  }
1370          }                  ccs_set_lf(head);
         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"))  
1371                  return true;                  return true;
1372   out:          }
1373          return false;          return false;
1374  }  }
1375    
1376  /**  /**
1377   * ccs_print_path_acl - Print a path ACL entry.   * ccs_set_group - Print "acl_group " header keyword and category name.
1378   *   *
1379   * @head: Pointer to "struct ccs_io_buffer".   * @head:     Pointer to "struct ccs_io_buffer".
1380   * @ptr:  Pointer to "struct ccs_path_acl".   * @category: Category name.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1381   *   *
1382   * Returns true on success, false otherwise.   * Returns nothing.
1383   */   */
1384  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static void ccs_set_group(struct ccs_io_buffer *head, const char *category)
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
1385  {  {
1386          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1387          u8 bit;                  ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index);
1388          const u16 perm = ptr->perm;          ccs_set_string(head, category);
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)  
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1389  }  }
1390    
1391  /**  /**
1392   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_print_entry - Print an ACL entry.
1393   *   *
1394   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1395   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1396   *   *
1397   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1398   */   */
1399  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1400                                         struct ccs_path_number3_acl *ptr,                              const struct ccs_acl_info *acl)
                                        const struct ccs_condition *cond)  
1401  {  {
1402          int pos;          const u8 acl_type = acl->type;
1403            const bool may_trigger_transition = acl->cond && acl->cond->transit;
1404            bool first = true;
1405          u8 bit;          u8 bit;
1406          const u16 perm = ptr->perm;          if (head->r.print_cond_part)
1407          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;                  goto print_cond_part;
1408               bit++) {          if (acl->is_deleted)
1409                  if (!(perm & (1 << bit)))                  return true;
1410                          continue;          if (!ccs_flush(head))
1411                  pos = head->read_avail;                  return false;
1412                  if (!ccs_io_printf(head, "allow_%s",          else if (acl_type == CCS_TYPE_PATH_ACL) {
1413                                     ccs_path_number32keyword(bit)) ||                  struct ccs_path_acl *ptr
1414                      !ccs_print_name_union(head, &ptr->name) ||                          = container_of(acl, typeof(*ptr), head);
1415                      !ccs_print_number_union(head, &ptr->mode) ||                  const u16 perm = ptr->perm;
1416                      !ccs_print_number_union(head, &ptr->major) ||                  for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) {
1417                      !ccs_print_number_union(head, &ptr->minor) ||                          if (!(perm & (1 << bit)))
1418                      !ccs_print_condition(head, cond)) {                                  continue;
1419                          head->read_bit = bit;                          if (head->r.print_transition_related_only &&
1420                          head->read_avail = pos;                              bit != CCS_TYPE_EXECUTE && !may_trigger_transition)
1421                          return false;                                  continue;
1422                            if (first) {
1423                                    ccs_set_group(head, "file ");
1424                                    first = false;
1425                            } else {
1426                                    ccs_set_slash(head);
1427                            }
1428                            ccs_set_string(head, ccs_path_keyword[bit]);
1429                  }                  }
1430          }                  if (first)
1431          head->read_bit = 0;                          return true;
1432          return true;                  ccs_print_name_union(head, &ptr->name);
1433  }          } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1434                       acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1435  /**                  struct ccs_handler_acl *ptr
1436   * ccs_print_path2_acl - Print a path2 ACL entry.                          = container_of(acl, typeof(*ptr), head);
1437   *                  ccs_set_group(head, "task ");
1438   * @head: Pointer to "struct ccs_io_buffer".                  ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1439   * @ptr:  Pointer to "struct ccs_path2_acl".                                 ? "auto_execute_handler " :
1440   * @cond: Pointer to "struct ccs_condition". May be NULL.                                 "denied_execute_handler ");
1441   *                  ccs_set_string(head, ptr->handler->name);
1442   * Returns true on success, false otherwise.          } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1443   */                     acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1444  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,                  struct ccs_task_acl *ptr =
1445                                  struct ccs_path2_acl *ptr,                          container_of(acl, typeof(*ptr), head);
1446                                  const struct ccs_condition *cond)                  ccs_set_group(head, "task ");
1447  {                  ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1448          int pos;                                 "auto_domain_transition " :
1449          u8 bit;                                 "manual_domain_transition ");
1450          const u8 perm = ptr->perm;                  ccs_set_string(head, ptr->domainname->name);
1451          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {          } else if (head->r.print_transition_related_only &&
1452                  if (!(perm & (1 << bit)))                     !may_trigger_transition) {
1453                          continue;                  return true;
1454                  pos = head->read_avail;          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1455                  if (!ccs_io_printf(head, "allow_%s",                  struct ccs_mkdev_acl *ptr =
1456                                     ccs_path22keyword(bit)) ||                          container_of(acl, typeof(*ptr), head);
1457                      !ccs_print_name_union(head, &ptr->name1) ||                  const u8 perm = ptr->perm;
1458                      !ccs_print_name_union(head, &ptr->name2) ||                  for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
1459                      !ccs_print_condition(head, cond)) {                          if (!(perm & (1 << bit)))
1460                          head->read_bit = bit;                                  continue;
1461                          head->read_avail = pos;                          if (first) {
1462                          return false;                                  ccs_set_group(head, "file ");
1463                                    first = false;
1464                            } else {
1465                                    ccs_set_slash(head);
1466                            }
1467                            ccs_set_string(head, ccs_mac_keywords
1468                                           [ccs_pnnn2mac[bit]]);
1469                  }                  }
1470          }                  if (first)
1471          head->read_bit = 0;                          return true;
1472          return true;                  ccs_print_name_union(head, &ptr->name);
1473  }                  ccs_print_number_union(head, &ptr->mode);
1474                    ccs_print_number_union(head, &ptr->major);
1475  /**                  ccs_print_number_union(head, &ptr->minor);
1476   * ccs_print_path_number_acl - Print a path_number ACL entry.          } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1477   *                  struct ccs_path2_acl *ptr =
1478   * @head: Pointer to "struct ccs_io_buffer".                          container_of(acl, typeof(*ptr), head);
1479   * @ptr:  Pointer to "struct ccs_path_number_acl".                  const u8 perm = ptr->perm;
1480   * @cond: Pointer to "struct ccs_condition". May be NULL.                  for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1481   *                          if (!(perm & (1 << bit)))
1482   * Returns true on success, false otherwise.                                  continue;
1483   */                          if (first) {
1484  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,                                  ccs_set_group(head, "file ");
1485                                        struct ccs_path_number_acl *ptr,                                  first = false;
1486                                        const struct ccs_condition *cond)                          } else {
1487  {                                  ccs_set_slash(head);
1488          int pos;                          }
1489          u8 bit;                          ccs_set_string(head, ccs_mac_keywords
1490          const u8 perm = ptr->perm;                                         [ccs_pp2mac[bit]]);
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    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;  
1491                  }                  }
1492          }                  if (first)
1493          head->read_bit = 0;                          return true;
1494          return true;                  ccs_print_name_union(head, &ptr->name1);
1495  }                  ccs_print_name_union(head, &ptr->name2);
1496            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1497  /**                  struct ccs_path_number_acl *ptr =
1498   * ccs_print_env_acl - Print an evironment variable name's ACL entry.                          container_of(acl, typeof(*ptr), head);
1499   *                  const u8 perm = ptr->perm;
1500   * @head: Pointer to "struct ccs_io_buffer".                  for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
1501   * @ptr:  Pointer to "struct ccs_env_acl".                          if (!(perm & (1 << bit)))
1502   * @cond: Pointer to "struct ccs_condition". May be NULL.                                  continue;
1503   *                          if (first) {
1504   * Returns true on success, false otherwise.                                  ccs_set_group(head, "file ");
1505   */                                  first = false;
1506  static bool ccs_print_env_acl(struct ccs_io_buffer *head,                          } else {
1507                                struct ccs_env_acl *ptr,                                  ccs_set_slash(head);
1508                                const struct ccs_condition *cond)                          }
1509  {                          ccs_set_string(head, ccs_mac_keywords
1510          const int pos = head->read_avail;                                         [ccs_pn2mac[bit]]);
1511          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||                  }
1512              !ccs_print_condition(head, cond)) {                  if (first)
1513                  head->read_avail = pos;                          return true;
1514                  return false;                  ccs_print_name_union(head, &ptr->name);
1515          }                  ccs_print_number_union(head, &ptr->number);
1516          return true;          } else if (acl_type == CCS_TYPE_ENV_ACL) {
1517  }                  struct ccs_env_acl *ptr =
1518                            container_of(acl, typeof(*ptr), head);
1519  /**                  ccs_set_group(head, "misc env ");
1520   * ccs_print_capability_acl - Print a capability ACL entry.                  ccs_set_string(head, ptr->env->name);
1521   *          } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1522   * @head: Pointer to "struct ccs_io_buffer".                  struct ccs_capability_acl *ptr =
1523   * @ptr:  Pointer to "struct ccs_capability_acl".                          container_of(acl, typeof(*ptr), head);
1524   * @cond: Pointer to "struct ccs_condition". May be NULL.                  ccs_set_group(head, "capability ");
1525   *                  ccs_set_string(head, ccs_mac_keywords
1526   * Returns true on success, false otherwise.                                 [ccs_c2mac[ptr->operation]]);
1527   */          } else if (acl_type == CCS_TYPE_INET_ACL) {
1528  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,                  struct ccs_inet_acl *ptr =
1529                                       struct ccs_capability_acl *ptr,                          container_of(acl, typeof(*ptr), head);
1530                                       const struct ccs_condition *cond)                  const u8 perm = ptr->perm;
1531  {                  for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1532          const int pos = head->read_avail;                          if (!(perm & (1 << bit)))
1533          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",                                  continue;
1534                             ccs_cap2keyword(ptr->operation)) ||                          if (first) {
1535              !ccs_print_condition(head, cond)) {                                  ccs_set_group(head, "network inet ");
1536                  head->read_avail = pos;                                  ccs_set_string(head, ccs_proto_keyword
1537                  return false;                                                 [ptr->protocol]);
1538          }                                  ccs_set_space(head);
1539          return true;                                  first = false;
1540  }                          } else {
1541                                    ccs_set_slash(head);
1542  /**                          }
1543   * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.                          ccs_set_string(head, ccs_socket_keyword[bit]);
1544   *                  }
1545   * @head: Pointer to "struct ccs_io_buffer".                  if (first)
1546   * @ptr:  Pointer to "struct ccs_ip_network_acl".                          return true;
1547   *                  ccs_set_space(head);
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl *ptr)  
 {  
         const u32 min_address = ptr->address.ipv4.min;  
         const u32 max_address = ptr->address.ipv4.max;  
         if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))  
                 return false;  
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
                 return false;  
         return true;  
 }  
   
 /**  
  * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl *ptr)  
 {  
         char buf[64];  
         const struct in6_addr *min_address = ptr->address.ipv6.min;  
         const struct in6_addr *max_address = ptr->address.ipv6.max;  
         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;  
 }  
   
 /**  
  * ccs_print_network_acl - Print a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_network_acl(struct ccs_io_buffer *head,  
                                   struct ccs_ip_network_acl *ptr,  
                                   const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",  
                                    ccs_net2keyword(bit)))  
                         goto out;  
1548                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1549                            char buf[128];
1550                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1551                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1552                                             group_name->name))                          ccs_set_string(head,
1553                                  goto out;                                         ptr->address.group->group_name->name);
1554                          break;                          break;
1555                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1556                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1557                                  goto out;                                         ptr->address.ipv4.max);
1558                            ccs_io_printf(head, "%s", buf);
1559                          break;                          break;
1560                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1561                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1562                                  goto out;                                         ptr->address.ipv6.max);
1563                            ccs_io_printf(head, "%s", buf);
1564                          break;                          break;
1565                  }                  }
1566                  if (!ccs_print_number_union(head, &ptr->port) ||                  ccs_print_number_union(head, &ptr->port);
1567                      !ccs_print_condition(head, cond))          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1568                          goto out;                  struct ccs_unix_acl *ptr =
1569          }                          container_of(acl, typeof(*ptr), head);
1570          head->read_bit = 0;                  const u8 perm = ptr->perm;
1571          return true;                  for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1572   out:                          if (!(perm & (1 << bit)))
1573          head->read_bit = bit;                                  continue;
1574          head->read_avail = pos;                          if (first) {
1575          return false;                                  ccs_set_group(head, "network unix ");
1576  }                                  ccs_set_string(head, ccs_proto_keyword
1577                                                   [ptr->protocol]);
1578  /**                                  ccs_set_space(head);
1579   * ccs_print_signal_acl - Print a signal ACL entry.                                  first = false;
1580   *                          } else {
1581   * @head: Pointer to "struct ccs_io_buffer".                                  ccs_set_slash(head);
1582   * @ptr:  Pointer to "struct signale_acl".                          }
1583   * @cond: Pointer to "struct ccs_condition". May be NULL.                          ccs_set_string(head, ccs_socket_keyword[bit]);
1584   *                  }
1585   * Returns true on success, false otherwise.                  if (first)
1586   */                          return true;
1587  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                  ccs_print_name_union(head, &ptr->name);
1588                                   struct ccs_signal_acl *ptr,          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1589                                   const struct ccs_condition *cond)                  struct ccs_signal_acl *ptr =
1590  {                          container_of(acl, typeof(*ptr), head);
1591          const int pos = head->read_avail;                  ccs_set_group(head, "ipc signal ");
1592          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                  ccs_io_printf(head, "%u ", ptr->sig);
1593                             ptr->sig, ptr->domainname->name) ||                  ccs_set_string(head, ptr->domainname->name);
1594              !ccs_print_condition(head, cond)) {          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1595                  head->read_avail = pos;                  struct ccs_mount_acl *ptr =
1596                  return false;                          container_of(acl, typeof(*ptr), head);
1597                    ccs_set_group(head, "file mount");
1598                    ccs_print_name_union(head, &ptr->dev_name);
1599                    ccs_print_name_union(head, &ptr->dir_name);
1600                    ccs_print_name_union(head, &ptr->fs_type);
1601                    ccs_print_number_union(head, &ptr->flags);
1602            }
1603            if (acl->cond) {
1604                    head->r.print_cond_part = true;
1605                    head->r.cond_step = 0;
1606                    if (!ccs_flush(head))
1607                            return false;
1608    print_cond_part:
1609                    if (!ccs_print_condition(head, acl->cond))
1610                            return false;
1611                    head->r.print_cond_part = false;
1612            } else {
1613                    ccs_set_lf(head);
1614          }          }
1615          return true;          return true;
1616  }  }
1617    
1618  /**  /**
1619   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_read_domain2 - Read domain policy.
1620   *   *
1621   * @head:    Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1622   * @keyword: Name of the keyword.   * @domain: Pointer to "struct ccs_domain_info".
1623   * @ptr:     Pointer to "struct ccs_execute_handler_record".   * @index:  Index number.
1624   *   *
1625   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
  */  
 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  
                                              const char *keyword,  
                                              struct ccs_execute_handler_record *  
                                              ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
1626   *   *
1627   * @head: Pointer to "struct ccs_io_buffer".   * Caller holds ccs_read_lock().
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
1628   */   */
1629  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1630                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain,
1631                                  const struct ccs_condition *cond)                               const u8 index)
1632  {  {
1633          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1634          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||                  struct ccs_acl_info *ptr =
1635              !ccs_print_name_union(head, &ptr->dev_name) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1636              !ccs_print_name_union(head, &ptr->dir_name) ||                  if (!ccs_print_entry(head, ptr))
1637              !ccs_print_name_union(head, &ptr->fs_type) ||                          return false;
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1638          }          }
1639            head->r.acl = NULL;
1640          return true;          return true;
1641  }  }
1642    
1643  /**  /**
1644   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain - Read domain policy.
1645   *   *
1646   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 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);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
1647   *   *
1648   * @head: Pointer to "struct ccs_io_buffer".   * Returns nothing.
1649   *   *
1650   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1651   */   */
1652  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1653  {  {
1654          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1655                  return;                  return;
1656          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1657                  head->read_step = 1;                  struct ccs_domain_info *domain =
1658          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1659                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1660                  const char *quota_exceeded = "";                          u8 i;
1661                  const char *transition_failed = "";                  case 0:
1662                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1663                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1664                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1665                  if (head->read_step != 1)                          /* Print domainname and flags. */
1666                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1667                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1668                          continue;                          ccs_io_printf(head, "use_profile %u\n",
1669                  /* Print domainname and flags. */                                        domain->profile);
1670                  if (domain->quota_warned)                          ccs_io_printf(head, "use_group %u\n", domain->group);
1671                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1672                  if (domain->domain_transition_failed)                                  if (domain->flags[i])
1673                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";                                          ccs_set_string(head, ccs_dif[i]);
1674                  if (domain->ignore_global_allow_read)                          head->r.step++;
1675                          ignore_global_allow_read                          ccs_set_lf(head);
1676                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          /* fall through */
1677                  if (domain->ignore_global_allow_env)                  case 1:
1678                          ignore_global_allow_env                          if (!ccs_read_domain2(head, domain, 0))
1679                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                                  return;
1680                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                          head->r.step++;
1681                                     "%s%s%s%s\n", domain->domainname->name,                          /* fall through */
1682                                     domain->profile, quota_exceeded,                  case 2:
1683                                     transition_failed,                          if (!ccs_read_domain2(head, domain, 1))
                                    ignore_global_allow_read,  
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         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))  
1684                                  return;                                  return;
1685                            head->r.step++;
1686                            if (!ccs_set_lf(head))
1687                                    return;
1688                            /* fall through */
1689                    case 3:
1690                            head->r.step = 0;
1691                            if (head->r.print_this_domain_only)
1692                                    goto done;
1693                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1694          }          }
1695          head->read_eof = true;  done:
1696            head->r.eof = true;
1697  }  }
1698    
1699  /**  /**
# Line 1743  static int ccs_write_domain_profile(stru Line 1733  static int ccs_write_domain_profile(stru
1733   *   *
1734   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1735   *   *
1736     * Returns nothing.
1737     *
1738   * This is equivalent to doing   * This is equivalent to doing
1739   *   *
1740   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |   *     grep -A 1 '^<kernel>' /proc/ccs/domain_policy |
# Line 1754  static int ccs_write_domain_profile(stru Line 1746  static int ccs_write_domain_profile(stru
1746   */   */
1747  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1748  {  {
1749          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1750                  return;                  return;
1751          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1752                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1753                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1754                  if (domain->is_deleted)                  if (domain->is_deleted)
1755                          continue;                          continue;
1756                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1757                          return;                          return;
1758                    ccs_io_printf(head, "%u ", domain->profile);
1759                    ccs_set_string(head, domain->domainname->name);
1760                    ccs_set_lf(head);
1761          }          }
1762          head->read_eof = true;          head->r.eof = true;
1763  }  }
1764    
1765  /**  /**
1766   * ccs_write_pid: Specify PID to obtain domainname.   * ccs_write_pid - Specify PID to obtain domainname.
1767   *   *
1768   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1769   *   *
# Line 1778  static void ccs_read_domain_profile(stru Line 1771  static void ccs_read_domain_profile(stru
1771   */   */
1772  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1773  {  {
1774          head->read_eof = false;          head->r.eof = false;
1775          return 0;          return 0;
1776  }  }
1777    
# Line 1804  static void ccs_read_pid(struct ccs_io_b Line 1797  static void ccs_read_pid(struct ccs_io_b
1797          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1798          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1799          if (!buf) {          if (!buf) {
1800                  head->read_eof = true;                  head->r.eof = true;
1801                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1802          }          }
1803          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1804                  return;                  return;
1805          head->read_eof = true;          head->r.eof = true;
1806          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1807                  task_info = true;                  task_info = true;
1808          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1818  static void ccs_read_pid(struct ccs_io_b Line 1811  static void ccs_read_pid(struct ccs_io_b
1811          ccs_tasklist_lock();          ccs_tasklist_lock();
1812  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1813          if (global_pid)          if (global_pid)
1814                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1815          else          else
1816                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1817  #else  #else
1818          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1819  #endif  #endif
1820          if (p) {          if (p) {
1821                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1822                  ccs_flags = p->ccs_flags;                  ccs_flags = ccs_task_flags(p);
1823          }          }
1824          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1825          if (!domain)          if (!domain)
1826                  return;                  return;
1827          if (!task_info)          if (!task_info) {
1828                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1829                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1830          else          } else {
1831                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
                               "state[0]=%u state[1]=%u state[2]=%u", pid,  
1832                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1833                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1834                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1835                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1836                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1837  }  }
1838    
1839    /* String table for domain transition control keywords. */
1840    static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1841            [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1842            [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1843            [CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1844            [CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
1845    };
1846    
1847    /* String table for grouping keywords. */
1848    static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1849            [CCS_PATH_GROUP]    = "path_group ",
1850            [CCS_NUMBER_GROUP]  = "number_group ",
1851            [CCS_ADDRESS_GROUP] = "address_group ",
1852    };
1853    
1854  /**  /**
1855   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1856   *   *
1857   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1858   *   *
1859   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1860   */   */
1861  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1862  {  {
1863          char *data = head->write_buf;          char *data = head->write_buf;
1864          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, "delete ");
1865          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1866                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1867          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1868                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1869          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[2] = {
1870                  return ccs_write_domain_initializer_policy(data, false,                  { "aggregator ",    ccs_write_aggregator },
1871                                                             is_delete);                  { "deny_autobind ", ccs_write_reserved_port },
1872          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))          };
1873                  return ccs_write_domain_initializer_policy(data, true,          if (!is_delete && ccs_str_starts(&data, "select ") &&
1874                                                             is_delete);              !strcmp(data, "transition_only")) {
1875          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  head->r.print_transition_related_only = true;
1876                  return ccs_write_aggregator_policy(data, is_delete);                  return -EAGAIN;
1877          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          }
1878                  return ccs_write_globally_readable_policy(data, is_delete);          /* Don't allow updating policies by non manager programs. */
1879          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          if (!ccs_manager())
1880                  return ccs_write_globally_usable_env_policy(data, is_delete);                  return -EPERM;
1881          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < 2; i++)
1882                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1883          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_callback[i].write(data, is_delete);
1884                  return ccs_write_path_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1885          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1886                  return ccs_write_number_group_policy(data, is_delete);                          return ccs_write_transition_control(data, is_delete,
1887          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                                                              i);
1888                  return ccs_write_no_rewrite_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++)
1889          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1890                  return ccs_write_address_group_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1891          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))          if (ccs_str_starts(&data, "acl_group ")) {
1892                  return ccs_write_reserved_port_policy(data, is_delete);                  unsigned int group;
1893                    if (sscanf(data, "%u", &group) == 1 &&
1894                        group < CCS_MAX_ACL_GROUPS) {
1895                            data = strchr(data, ' ');
1896                            if (data)
1897                                    return ccs_write_domain2(data + 1,
1898                                                             &ccs_acl_group[group],
1899                                                             is_delete);
1900                    }
1901            }
1902          return -EINVAL;          return -EINVAL;
1903  }  }
1904    
1905  /**  /**
1906   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1907   *   *
1908   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1909     * @idx:  Index number.
1910     *
1911     * Returns true on success, false otherwise.
1912   *   *
1913   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1914   */   */
1915  static void ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1916  {  {
1917          if (head->read_eof)          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1918                  return;                  struct ccs_group *group =
1919          switch (head->read_step) {                          list_entry(head->r.group, typeof(*group), head.list);
1920          case 0:                  list_for_each_cookie(head->r.acl, &group->member_list) {
1921                  head->read_var2 = NULL;                          struct ccs_acl_head *ptr =
1922                  head->read_step = 1;                                  list_entry(head->r.acl, typeof(*ptr), list);
1923          case 1:                          if (ptr->is_deleted)
1924                  if (!ccs_read_domain_keeper_policy(head))                                  continue;
1925                          break;                          if (!ccs_flush(head))
1926                  head->read_var2 = NULL;                                  return false;
1927                  head->read_step = 2;                          ccs_set_string(head, ccs_group_name[idx]);
1928          case 2:                          ccs_set_string(head, group->group_name->name);
1929                  if (!ccs_read_globally_readable_policy(head))                          if (idx == CCS_PATH_GROUP) {
1930                          break;                                  ccs_set_space(head);
1931                  head->read_var2 = NULL;                                  ccs_set_string(head, container_of
1932                  head->read_step = 3;                                                 (ptr, struct ccs_path_group,
1933          case 3:                                                  head)->member_name->name);
1934                  if (!ccs_read_globally_usable_env_policy(head))                          } else if (idx == CCS_NUMBER_GROUP) {
1935                          break;                                  ccs_print_number_union(head, &container_of
1936                  head->read_var2 = NULL;                                                 (ptr, struct ccs_number_group,
1937                  head->read_step = 4;                                                  head)->number);
1938          case 4:                          } else if (idx == CCS_ADDRESS_GROUP) {
1939                  if (!ccs_read_domain_initializer_policy(head))                                  char buffer[128];
1940                          break;                                  struct ccs_address_group *member =
1941                  head->read_var2 = NULL;                                          container_of(ptr, typeof(*member),
1942                  head->read_step = 6;                                                       head);
1943          case 6:                                  if (member->is_ipv6)
1944                  if (!ccs_read_aggregator_policy(head))                                          ccs_print_ipv6(buffer, sizeof(buffer),
1945                          break;                                                         member->min.ipv6,
1946                  head->read_var2 = NULL;                                                         member->max.ipv6);
1947                  head->read_step = 7;                                  else
1948          case 7:                                          ccs_print_ipv4(buffer, sizeof(buffer),
1949                  if (!ccs_read_file_pattern(head))                                                         member->min.ipv4,
1950                          break;                                                         member->max.ipv4);
1951                  head->read_var2 = NULL;                                  ccs_io_printf(head, " %s", buffer);
1952                  head->read_step = 8;                          }
1953          case 8:                          ccs_set_lf(head);
1954                  if (!ccs_read_no_rewrite_policy(head))                  }
1955                          break;                  head->r.acl = NULL;
                 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;  
1956          }          }
1957            head->r.group = NULL;
1958            return true;
1959  }  }
1960    
1961  /**  /**
1962   * ccs_get_argv0 - Get argv[0].   * ccs_read_policy - Read "struct ccs_..._entry" list.
1963   *   *
1964   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1965     * @idx:  Index number.
1966   *   *
1967   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1968     *
1969     * Caller holds ccs_read_lock().
1970   */   */
1971  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1972  {  {
1973          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1974          char *arg_ptr = ee->tmp;                  struct ccs_acl_head *acl =
1975          int arg_len = 0;                          container_of(head->r.acl, typeof(*acl), list);
1976          unsigned long pos = bprm->p;                  if (acl->is_deleted)
1977          int offset = pos % PAGE_SIZE;                          continue;
1978          bool done = false;                  if (head->r.print_transition_related_only &&
1979          if (!bprm->argc)                      idx != CCS_ID_TRANSITION_CONTROL)
1980                  goto out;                          continue;
1981          while (1) {                  if (!ccs_flush(head))
1982                  if (!ccs_dump_page(bprm, pos, &ee->dump))                          return false;
1983                          goto out;                  switch (idx) {
1984                  pos += PAGE_SIZE - offset;                  case CCS_ID_TRANSITION_CONTROL:
1985                  /* Read. */                          {
1986                  while (offset < PAGE_SIZE) {                                  struct ccs_transition_control *ptr =
1987                          const char *kaddr = ee->dump.data;                                          container_of(acl, typeof(*ptr), head);
1988                          const unsigned char c = kaddr[offset++];                                  ccs_set_string(head,
1989                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                 ccs_transition_type[ptr->type]);
1990                                  if (c == '\\') {                                  ccs_set_string(head, ptr->program ?
1991                                          arg_ptr[arg_len++] = '\\';                                                 ptr->program->name : "any");
1992                                          arg_ptr[arg_len++] = '\\';                                  ccs_set_string(head, " from ");
1993                                  } else if (c > ' ' && c < 127) {                                  ccs_set_string(head, ptr->domainname ?
1994                                          arg_ptr[arg_len++] = c;                                                 ptr->domainname->name : "any");
                                 } 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;  
1995                          }                          }
                 }  
                 offset = 0;  
                 if (done)  
1996                          break;                          break;
1997                    case CCS_ID_AGGREGATOR:
1998                            {
1999                                    struct ccs_aggregator *ptr =
2000                                            container_of(acl, typeof(*ptr), head);
2001                                    ccs_set_string(head, "aggregator ");
2002                                    ccs_set_string(head, ptr->original_name->name);
2003                                    ccs_set_space(head);
2004                                    ccs_set_string(head,
2005                                                   ptr->aggregated_name->name);
2006                            }
2007                            break;
2008                    case CCS_ID_RESERVEDPORT:
2009                            {
2010                                    struct ccs_reserved *ptr =
2011                                            container_of(acl, typeof(*ptr), head);
2012                                    const u16 min_port = ptr->min_port;
2013                                    const u16 max_port = ptr->max_port;
2014                                    ccs_set_string(head, "deny_autobind ");
2015                                    ccs_io_printf(head, "%u", min_port);
2016                                    if (min_port != max_port)
2017                                            ccs_io_printf(head, "-%u", max_port);
2018                            }
2019                            break;
2020                    default:
2021                            continue;
2022                    }
2023                    ccs_set_lf(head);
2024          }          }
2025            head->r.acl = NULL;
2026          return true;          return true;
  out:  
         return false;  
2027  }  }
2028    
2029  /**  /**
2030   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_exception - Read exception policy.
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
2031   *   *
2032   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * @head: Pointer to "struct ccs_io_buffer".
  */  
 static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry  
                                                        *ee)  
 {  
         struct ccs_condition *cond;  
         char *buf;  
         int len = 256;  
         char *realpath = NULL;  
         char *argv0 = NULL;  
         const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);  
         if (profile->learning->learning_exec_realpath) {  
                 struct file *file = ee->bprm->file;  
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)  
                 struct path path = { file->f_vfsmnt, file->f_dentry };  
                 realpath = ccs_realpath_from_path(&path);  
 #else  
                 realpath = ccs_realpath_from_path(&file->f_path);  
 #endif  
                 if (realpath)  
                         len += strlen(realpath) + 17;  
         }  
         if (profile->learning->learning_exec_argv0) {  
                 if (ccs_get_argv0(ee)) {  
                         argv0 = ee->tmp;  
                         len += strlen(argv0) + 16;  
                 }  
         }  
         buf = kmalloc(len, GFP_KERNEL);  
         if (!buf) {  
                 kfree(realpath);  
                 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;  
 }  
   
 /**  
  * ccs_get_symlink_condition - Get condition part for symlink requests.  
2033   *   *
2034   * @r: Pointer to "struct ccs_request_info".   * Returns nothing.
2035   *   *
2036   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
2037   */   */
2038  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
2039  {  {
2040          struct ccs_condition *cond;          if (head->r.eof)
2041          char *buf;                  return;
2042          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
2043          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
2044          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
2045          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
2046                  symlink = r->obj->symlink_target->name;                  return;
2047                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2048                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
2049                    head->r.step++;
2050            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2051                    return;
2052            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
2053                   + CCS_MAX_ACL_GROUPS * 2) {
2054                    head->r.acl_group_index = (head->r.step - CCS_MAX_POLICY
2055                                               - CCS_MAX_GROUP) / 2;
2056                    if (!ccs_read_domain2(head,
2057                                          &ccs_acl_group[head->r.acl_group_index],
2058                                          head->r.step & 1))
2059                            return;
2060                    head->r.step++;
2061          }          }
2062          buf = kmalloc(len, GFP_KERNEL);          head->r.eof = true;
         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 (symlink) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",  
                          symlink);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
2063  }  }
2064    
2065  /* Wait queue for ccs_query_list. */  /* Wait queue for kernel -> userspace notification. */
2066  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
2067    /* Wait queue for userspace -> kernel notification. */
2068  /* Lock for manipulating ccs_query_list. */  static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
 static DEFINE_SPINLOCK(ccs_query_list_lock);  
2069    
2070  /* Structure for query. */  /* Structure for query. */
2071  struct ccs_query_entry {  struct ccs_query {
2072          struct list_head list;          struct list_head list;
2073          char *query;          char *query;
2074          int query_len;          size_t query_len;
2075          unsigned int serial;          unsigned int serial;
2076          int timer;          u8 timer;
2077          int answer;          u8 answer;
2078            u8 retry;
2079  };  };
2080    
2081  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
2082  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
2083    
2084    /* Lock for manipulating ccs_query_list. */
2085    static DEFINE_SPINLOCK(ccs_query_list_lock);
2086    
2087  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
2088  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2089    
2090  /**  /**
2091     * ccs_truncate - Truncate a line.
2092     *
2093     * @str: String to truncate.
2094     *
2095     * Returns length of truncated @str.
2096     */
2097    static int ccs_truncate(char *str)
2098    {
2099            char *start = str;
2100            while (*(unsigned char *) str > (unsigned char) ' ')
2101                    str++;
2102            *str = '\0';
2103            return strlen(start) + 1;
2104    }
2105    
2106    /**
2107     * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode.
2108     *
2109     * @header: Lines containing ACL.
2110     *
2111     * Returns nothing.
2112     */
2113    static void ccs_add_entry(char *header)
2114    {
2115            char *buffer;
2116            char *realpath = NULL;
2117            char *argv0 = NULL;
2118            char *symlink = NULL;
2119            char *handler;
2120            char *cp = strchr(header, '\n');
2121            int len;
2122            if (!cp)
2123                    return;
2124            cp = strchr(cp + 1, '\n');
2125            if (!cp)
2126                    return;
2127            *cp++ = '\0';
2128            len = strlen(cp) + 1;
2129            /* strstr() will return NULL if ordering is wrong. */
2130            if (*cp == 'f') {
2131                    argv0 = strstr(header, " argv[]={ \"");
2132                    if (argv0) {
2133                            argv0 += 10;
2134                            len += ccs_truncate(argv0) + 14;
2135                    }
2136                    realpath = strstr(header, " exec={ realpath=\"");
2137                    if (realpath) {