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

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