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

Subversion リポジトリの参照

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

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

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