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

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