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

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 3747 by kumaneko, Wed Jun 9 13:58:57 2010 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 4080 by kumaneko, Fri Oct 22 03:32:47 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.2+   2010/06/04   * Version: 1.8.0-pre   2010/10/22
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 12  Line 12 
12    
13  #include "internal.h"  #include "internal.h"
14    
15  static struct ccs_profile ccs_default_profile = {  /* Profile version. Currently only 20100903 is defined. */
         .learning = &ccs_default_profile.preference,  
         .permissive = &ccs_default_profile.preference,  
         .enforcing = &ccs_default_profile.preference,  
         .audit = &ccs_default_profile.preference,  
 #ifdef CONFIG_CCSECURITY_AUDIT  
         .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,  
         .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,  
 #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  
 };  
   
 /* Profile version. Currently only 20090903 is defined. */  
16  static unsigned int ccs_profile_version;  static unsigned int ccs_profile_version;
17    
18  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
19  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
20    
21  /* String table for functionality that takes 4 modes. */  /* String table for operation mode. */
22  static const char *ccs_mode_4[4] = {  const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
23          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED]   = "disabled",
24            [CCS_CONFIG_LEARNING]   = "learning",
25            [CCS_CONFIG_PERMISSIVE] = "permissive",
26            [CCS_CONFIG_ENFORCING]  = "enforcing"
27    };
28    
29    /* String table for /proc/ccs/profile interface. */
30    const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
31                                        + CCS_MAX_MAC_CATEGORY_INDEX] = {
32            /* CONFIG::file group */
33            [CCS_MAC_FILE_EXECUTE]    = "execute",
34            [CCS_MAC_FILE_OPEN]       = "open",
35            [CCS_MAC_FILE_CREATE]     = "create",
36            [CCS_MAC_FILE_UNLINK]     = "unlink",
37            [CCS_MAC_FILE_GETATTR]    = "getattr",
38            [CCS_MAC_FILE_MKDIR]      = "mkdir",
39            [CCS_MAC_FILE_RMDIR]      = "rmdir",
40            [CCS_MAC_FILE_MKFIFO]     = "mkfifo",
41            [CCS_MAC_FILE_MKSOCK]     = "mksock",
42            [CCS_MAC_FILE_TRUNCATE]   = "truncate",
43            [CCS_MAC_FILE_SYMLINK]    = "symlink",
44            [CCS_MAC_FILE_MKBLOCK]    = "mkblock",
45            [CCS_MAC_FILE_MKCHAR]     = "mkchar",
46            [CCS_MAC_FILE_LINK]       = "link",
47            [CCS_MAC_FILE_RENAME]     = "rename",
48            [CCS_MAC_FILE_CHMOD]      = "chmod",
49            [CCS_MAC_FILE_CHOWN]      = "chown",
50            [CCS_MAC_FILE_CHGRP]      = "chgrp",
51            [CCS_MAC_FILE_IOCTL]      = "ioctl",
52            [CCS_MAC_FILE_CHROOT]     = "chroot",
53            [CCS_MAC_FILE_MOUNT]      = "mount",
54            [CCS_MAC_FILE_UMOUNT]     = "unmount",
55            [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
56            /* CONFIG::misc group */
57            [CCS_MAC_ENVIRON] = "env",
58            /* CONFIG::network group */
59            [CCS_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
60            [CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
61            [CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
62            [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = "inet_stream_accept",
63            [CCS_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
64            [CCS_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
65            [CCS_MAC_NETWORK_INET_DGRAM_RECV]        = "inet_dgram_recv",
66            [CCS_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
67            [CCS_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
68            [CCS_MAC_NETWORK_INET_RAW_RECV]          = "inet_raw_recv",
69            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
70            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
71            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
72            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = "unix_stream_accept",
73            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
74            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
75            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = "unix_dgram_recv",
76            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
77            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
78            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
79            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = "unix_seqpacket_accept",
80            /* CONFIG::ipc group */
81            [CCS_MAC_SIGNAL] = "signal",
82            /* CONFIG::capability group */
83            [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = "use_route",
84            [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
85            [CCS_MAC_CAPABILITY_SYS_REBOOT]        = "SYS_REBOOT",
86            [CCS_MAC_CAPABILITY_SYS_VHANGUP]       = "SYS_VHANGUP",
87            [CCS_MAC_CAPABILITY_SYS_SETTIME]       = "SYS_TIME",
88            [CCS_MAC_CAPABILITY_SYS_NICE]          = "SYS_NICE",
89            [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = "SYS_SETHOSTNAME",
90            [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
91            [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = "SYS_KEXEC_LOAD",
92            [CCS_MAC_CAPABILITY_SYS_PTRACE]        = "SYS_PTRACE",
93            /* CONFIG group */
94            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE]       = "file",
95            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK]    = "network",
96            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC]       = "misc",
97            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC]        = "ipc",
98            [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
99    };
100    
101    /* String table for path operation. */
102    const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
103            [CCS_TYPE_EXECUTE]    = "execute",
104            [CCS_TYPE_READ]       = "read",
105            [CCS_TYPE_WRITE]      = "write",
106            [CCS_TYPE_APPEND]     = "append",
107            [CCS_TYPE_UNLINK]     = "unlink",
108            [CCS_TYPE_GETATTR]    = "getattr",
109            [CCS_TYPE_RMDIR]      = "rmdir",
110            [CCS_TYPE_TRUNCATE]   = "truncate",
111            [CCS_TYPE_SYMLINK]    = "symlink",
112            [CCS_TYPE_CHROOT]     = "chroot",
113            [CCS_TYPE_UMOUNT]     = "unmount",
114    };
115    
116    /* String table for categories. */
117    static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
118            [CCS_MAC_CATEGORY_FILE]       = "file",
119            [CCS_MAC_CATEGORY_NETWORK]    = "network",
120            [CCS_MAC_CATEGORY_MISC]       = "misc",
121            [CCS_MAC_CATEGORY_IPC]        = "ipc",
122            [CCS_MAC_CATEGORY_CAPABILITY] = "capability",
123    };
124    
125    /* String table for conditions. */
126    const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
127            [CCS_TASK_UID]             = "task.uid",
128            [CCS_TASK_EUID]            = "task.euid",
129            [CCS_TASK_SUID]            = "task.suid",
130            [CCS_TASK_FSUID]           = "task.fsuid",
131            [CCS_TASK_GID]             = "task.gid",
132            [CCS_TASK_EGID]            = "task.egid",
133            [CCS_TASK_SGID]            = "task.sgid",
134            [CCS_TASK_FSGID]           = "task.fsgid",
135            [CCS_TASK_PID]             = "task.pid",
136            [CCS_TASK_PPID]            = "task.ppid",
137            [CCS_EXEC_ARGC]            = "exec.argc",
138            [CCS_EXEC_ENVC]            = "exec.envc",
139            [CCS_TYPE_IS_SOCKET]       = "socket",
140            [CCS_TYPE_IS_SYMLINK]      = "symlink",
141            [CCS_TYPE_IS_FILE]         = "file",
142            [CCS_TYPE_IS_BLOCK_DEV]    = "block",
143            [CCS_TYPE_IS_DIRECTORY]    = "directory",
144            [CCS_TYPE_IS_CHAR_DEV]     = "char",
145            [CCS_TYPE_IS_FIFO]         = "fifo",
146            [CCS_MODE_SETUID]          = "setuid",
147            [CCS_MODE_SETGID]          = "setgid",
148            [CCS_MODE_STICKY]          = "sticky",
149            [CCS_MODE_OWNER_READ]      = "owner_read",
150            [CCS_MODE_OWNER_WRITE]     = "owner_write",
151            [CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
152            [CCS_MODE_GROUP_READ]      = "group_read",
153            [CCS_MODE_GROUP_WRITE]     = "group_write",
154            [CCS_MODE_GROUP_EXECUTE]   = "group_execute",
155            [CCS_MODE_OTHERS_READ]     = "others_read",
156            [CCS_MODE_OTHERS_WRITE]    = "others_write",
157            [CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
158            [CCS_TASK_TYPE]            = "task.type",
159            [CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
160            [CCS_EXEC_REALPATH]        = "exec.realpath",
161            [CCS_SYMLINK_TARGET]       = "symlink.target",
162            [CCS_PATH1_UID]            = "path1.uid",
163            [CCS_PATH1_GID]            = "path1.gid",
164            [CCS_PATH1_INO]            = "path1.ino",
165            [CCS_PATH1_MAJOR]          = "path1.major",
166            [CCS_PATH1_MINOR]          = "path1.minor",
167            [CCS_PATH1_PERM]           = "path1.perm",
168            [CCS_PATH1_TYPE]           = "path1.type",
169            [CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
170            [CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
171            [CCS_PATH2_UID]            = "path2.uid",
172            [CCS_PATH2_GID]            = "path2.gid",
173            [CCS_PATH2_INO]            = "path2.ino",
174            [CCS_PATH2_MAJOR]          = "path2.major",
175            [CCS_PATH2_MINOR]          = "path2.minor",
176            [CCS_PATH2_PERM]           = "path2.perm",
177            [CCS_PATH2_TYPE]           = "path2.type",
178            [CCS_PATH2_DEV_MAJOR]      = "path2.dev_major",
179            [CCS_PATH2_DEV_MINOR]      = "path2.dev_minor",
180            [CCS_PATH1_PARENT_UID]     = "path1.parent.uid",
181            [CCS_PATH1_PARENT_GID]     = "path1.parent.gid",
182            [CCS_PATH1_PARENT_INO]     = "path1.parent.ino",
183            [CCS_PATH1_PARENT_PERM]    = "path1.parent.perm",
184            [CCS_PATH2_PARENT_UID]     = "path2.parent.uid",
185            [CCS_PATH2_PARENT_GID]     = "path2.parent.gid",
186            [CCS_PATH2_PARENT_INO]     = "path2.parent.ino",
187            [CCS_PATH2_PARENT_PERM]    = "path2.parent.perm",
188  };  };
189    
190  /* String table for /proc/ccs/profile */  /* String table for PREFERENCE keyword. */
191  static const char *ccs_mac_keywords[CCS_MAX_MAC_INDEX +  static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
192                                      CCS_MAX_CAPABILITY_INDEX +          [CCS_PREF_MAX_GRANT_LOG]      = "max_grant_log",
193                                      CCS_MAX_MAC_CATEGORY_INDEX] = {          [CCS_PREF_MAX_REJECT_LOG]     = "max_reject_log",
194          [CCS_MAC_FILE_EXECUTE]          [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
195          = "file::execute",          [CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
         [CCS_MAC_FILE_OPEN]  
         = "file::open",  
         [CCS_MAC_FILE_CREATE]  
         = "file::create",  
         [CCS_MAC_FILE_UNLINK]  
         = "file::unlink",  
         [CCS_MAC_FILE_MKDIR]  
         = "file::mkdir",  
         [CCS_MAC_FILE_RMDIR]  
         = "file::rmdir",  
         [CCS_MAC_FILE_MKFIFO]  
         = "file::mkfifo",  
         [CCS_MAC_FILE_MKSOCK]  
         = "file::mksock",  
         [CCS_MAC_FILE_TRUNCATE]  
         = "file::truncate",  
         [CCS_MAC_FILE_SYMLINK]  
         = "file::symlink",  
         [CCS_MAC_FILE_REWRITE]  
         = "file::rewrite",  
         [CCS_MAC_FILE_MKBLOCK]  
         = "file::mkblock",  
         [CCS_MAC_FILE_MKCHAR]  
         = "file::mkchar",  
         [CCS_MAC_FILE_LINK]  
         = "file::link",  
         [CCS_MAC_FILE_RENAME]  
         = "file::rename",  
         [CCS_MAC_FILE_CHMOD]  
         = "file::chmod",  
         [CCS_MAC_FILE_CHOWN]  
         = "file::chown",  
         [CCS_MAC_FILE_CHGRP]  
         = "file::chgrp",  
         [CCS_MAC_FILE_IOCTL]  
         = "file::ioctl",  
         [CCS_MAC_FILE_CHROOT]  
         = "file::chroot",  
         [CCS_MAC_FILE_MOUNT]  
         = "file::mount",  
         [CCS_MAC_FILE_UMOUNT]  
         = "file::umount",  
         [CCS_MAC_FILE_PIVOT_ROOT]  
         = "file::pivot_root",  
         [CCS_MAC_FILE_TRANSIT]  
         = "file::transit",  
         [CCS_MAC_ENVIRON]  
         = "misc::env",  
         [CCS_MAC_NETWORK_UDP_BIND]  
         = "network::inet_udp_bind",  
         [CCS_MAC_NETWORK_UDP_CONNECT]  
         = "network::inet_udp_connect",  
         [CCS_MAC_NETWORK_TCP_BIND]  
         = "network::inet_tcp_bind",  
         [CCS_MAC_NETWORK_TCP_LISTEN]  
         = "network::inet_tcp_listen",  
         [CCS_MAC_NETWORK_TCP_CONNECT]  
         = "network::inet_tcp_connect",  
         [CCS_MAC_NETWORK_TCP_ACCEPT]  
         = "network::inet_tcp_accept",  
         [CCS_MAC_NETWORK_RAW_BIND]  
         = "network::inet_raw_bind",  
         [CCS_MAC_NETWORK_RAW_CONNECT]  
         = "network::inet_raw_connect",  
         [CCS_MAC_SIGNAL]  
         = "ipc::signal",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "capability::inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "capability::inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "capability::inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]  
         = "capability::use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]  
         = "capability::use_inet_ip",  
         [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]  
         = "capability::use_route",  
         [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]  
         = "capability::use_packet",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]  
         = "capability::SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]  
         = "capability::SYS_UMOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]  
         = "capability::SYS_REBOOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHROOT]  
         = "capability::SYS_CHROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KILL]  
         = "capability::SYS_KILL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]  
         = "capability::SYS_VHANGUP",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]  
         = "capability::SYS_TIME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_NICE]  
         = "capability::SYS_NICE",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SETHOSTNAME]  
         = "capability::SYS_SETHOSTNAME",  
         [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]  
         = "capability::use_kernel_module",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_FIFO]  
         = "capability::create_fifo",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_BLOCK_DEV]  
         = "capability::create_block_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_CHAR_DEV]  
         = "capability::create_char_dev",  
         [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]  
         = "capability::create_unix_socket",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_LINK]  
         = "capability::SYS_LINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_SYMLINK]  
         = "capability::SYS_SYMLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_RENAME]  
         = "capability::SYS_RENAME",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UNLINK]  
         = "capability::SYS_UNLINK",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHMOD]  
         = "capability::SYS_CHMOD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_CHOWN]  
         = "capability::SYS_CHOWN",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_IOCTL]  
         = "capability::SYS_IOCTL",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]  
         = "capability::SYS_KEXEC_LOAD",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]  
         = "capability::SYS_PTRACE",  
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_FILE] = "file",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_NETWORK] = "network",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_MISC] = "misc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_IPC] = "ipc",  
         [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
          + CCS_MAC_CATEGORY_CAPABILITY] = "capability",  
196  };  };
197    
198  /* Permit policy management by non-root user? */  /* Permit policy management by non-root user? */
199  static bool ccs_manage_by_non_root;  static bool ccs_manage_by_non_root;
200    
201  /**  /**
202   * ccs_cap2keyword - Convert capability operation to capability name.   * ccs_yesno - Return "yes" or "no".
203   *   *
204   * @operation: The capability index.   * @value: Bool value.
205   *   *
206   * Returns the name of the specified capability's name.   * Returns "yes" if @value is not 0, "no" otherwise.
207   */   */
208  const char *ccs_cap2keyword(const u8 operation)  static const char *ccs_yesno(const unsigned int value)
209  {  {
210          return operation < CCS_MAX_CAPABILITY_INDEX          return value ? "yes" : "no";
                 ? ccs_mac_keywords[CCS_MAX_MAC_INDEX + operation] + 12 : NULL;  
211  }  }
212    
213    /* Prototype fpr ccs_addprintf(). */
214    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
215            __attribute__ ((format(printf, 3, 4)));
216    
217  /**  /**
218   * ccs_yesno - Return "yes" or "no".   * ccs_addprintf - snprint()-like-strncat().
219   *   *
220   * @value: Bool value.   * @buffer: Buffer to write to. Must be '\0'-terminated.
221     * @len:    Size of @buffer.
222     * @fmt:    The printf()'s format string, followed by parameters.
223     *
224     * Returns nothing.
225   */   */
226  static const char *ccs_yesno(const unsigned int value)  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
227  {  {
228          return value ? "yes" : "no";          va_list args;
229            const int pos = strlen(buffer);
230            va_start(args, fmt);
231            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
232            va_end(args);
233  }  }
234    
235  /**  /**
236   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
237   *   *
238   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
239   *   *
240   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
241     */
242    static bool ccs_flush(struct ccs_io_buffer *head)
243    {
244            while (head->r.w_pos) {
245                    const char *w = head->r.w[0];
246                    int len = strlen(w);
247                    if (len) {
248                            if (len > head->read_user_buf_avail)
249                                    len = head->read_user_buf_avail;
250                            if (!len)
251                                    return false;
252                            if (copy_to_user(head->read_user_buf, w, len))
253                                    return false;
254                            head->read_user_buf_avail -= len;
255                            head->read_user_buf += len;
256                            w += len;
257                    }
258                    if (*w) {
259                            head->r.w[0] = w;
260                            return false;
261                    }
262                    /* Add '\0' for audit logs and query. */
263                    if (head->poll) {
264                            if (!head->read_user_buf_avail ||
265                                copy_to_user(head->read_user_buf, "", 1))
266                                    return false;
267                            head->read_user_buf_avail--;
268                            head->read_user_buf++;
269                    }
270                    head->r.w_pos--;
271                    for (len = 0; len < head->r.w_pos; len++)
272                            head->r.w[len] = head->r.w[len + 1];
273            }
274            head->r.avail = 0;
275            return true;
276    }
277    
278    /**
279     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
280     *
281     * @head:   Pointer to "struct ccs_io_buffer".
282     * @string: String to print.
283     *
284     * Note that @string has to be kept valid until @head is kfree()d.
285     * This means that char[] allocated on stack memory cannot be passed to
286     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
287     *
288     * Returns nothing.
289     */
290    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
291    {
292            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
293                    head->r.w[head->r.w_pos++] = string;
294                    ccs_flush(head);
295            } else
296                    printk(KERN_WARNING "Too many words in a line.\n");
297    }
298    
299    /**
300     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
301     *
302     * @head: Pointer to "struct ccs_io_buffer".
303     * @fmt:  The printf()'s format string, followed by parameters.
304   *   *
305   * The snprintf() will truncate, but ccs_io_printf() won't.   * Returns nothing.
306   */   */
307  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
308  {  {
309          va_list args;          va_list args;
310          int len;          int len;
311          int pos = head->read_avail;          int pos = head->r.avail;
312          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
313          if (size <= 0)          if (size <= 0)
314                  return false;                  return;
315          va_start(args, fmt);          va_start(args, fmt);
316          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
317          va_end(args);          va_end(args);
318          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
319                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
320          head->read_avail += len;                  return;
321          return true;          }
322            head->r.avail += len;
323            ccs_set_string(head, head->read_buf + pos);
324    }
325    
326    /**
327     * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
328     *
329     * @head: Pointer to "struct ccs_io_buffer".
330     *
331     * Returns nothing.
332     */
333    static void ccs_set_space(struct ccs_io_buffer *head)
334    {
335            ccs_set_string(head, " ");
336    }
337    
338    /**
339     * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
340     *
341     * @head: Pointer to "struct ccs_io_buffer".
342     *
343     * Returns nothing.
344     */
345    static bool ccs_set_lf(struct ccs_io_buffer *head)
346    {
347            ccs_set_string(head, "\n");
348            return !head->r.w_pos;
349  }  }
350    
351  /**  /**
# Line 267  static struct ccs_profile *ccs_assign_pr Line 370  static struct ccs_profile *ccs_assign_pr
370          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
371          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
372                  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;  
373                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
374                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
375                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
376                         sizeof(ptr->config));                         sizeof(ptr->config));
377                    ptr->pref[CCS_PREF_MAX_GRANT_LOG] =
378                            CONFIG_CCSECURITY_MAX_GRANT_LOG;
379                    ptr->pref[CCS_PREF_MAX_REJECT_LOG] =
380                            CONFIG_CCSECURITY_MAX_REJECT_LOG;
381                    ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
382                            CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
383                  mb(); /* Avoid out-of-order execution. */                  mb(); /* Avoid out-of-order execution. */
384                  ccs_profile_ptr[profile] = ptr;                  ccs_profile_ptr[profile] = ptr;
385                  entry = NULL;                  entry = NULL;
386          }          }
387          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
388   out:  out:
389          kfree(entry);          kfree(entry);
390          return ptr;          return ptr;
391  }  }
392    
393  /**  /**
394   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
395     *
396     * Returns nothing.
397   */   */
398  static void ccs_check_profile(void)  static void ccs_check_profile(void)
399  {  {
400          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
401          const int idx = ccs_read_lock();          const int idx = ccs_read_lock();
402          ccs_policy_loaded = true;          ccs_policy_loaded = true;
403          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
404                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
405                  if (ccs_profile_ptr[profile])                  if (ccs_profile_ptr[profile])
406                          continue;                          continue;
407                    printk(KERN_ERR "You need to define profile %u before using it.\n",
408                           profile);
409                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
410                           "for more information.\n");
411                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
412                        profile, domain->domainname->name);                        profile, domain->domainname->name);
413          }          }
414          ccs_read_unlock(idx);          ccs_read_unlock(idx);
415          if (ccs_profile_version != 20090903)          if (ccs_profile_version != 20100903) {
416                    printk(KERN_ERR "You need to install userland programs for "
417                           "TOMOYO 1.8 and initialize policy configuration.\n");
418                    printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/1.8/ "
419                           "for more information.\n");
420                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
421                        ccs_profile_version);                        ccs_profile_version);
422          printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");          }
423            printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/10/22\n");
424          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
425  }  }
426    
# Line 317  static void ccs_check_profile(void) Line 433  static void ccs_check_profile(void)
433   */   */
434  struct ccs_profile *ccs_profile(const u8 profile)  struct ccs_profile *ccs_profile(const u8 profile)
435  {  {
436            static struct ccs_profile ccs_null_profile;
437          struct ccs_profile *ptr = ccs_profile_ptr[profile];          struct ccs_profile *ptr = ccs_profile_ptr[profile];
438          if (!ccs_policy_loaded)          if (!ptr)
439                  return &ccs_default_profile;                  ptr = &ccs_null_profile;
         BUG_ON(!ptr);  
440          return ptr;          return ptr;
441  }  }
442    
443    /**
444     * ccs_find_yesno - Find values for specified keyword.
445     *
446     * @string: String to check.
447     * @find:   Name of keyword.
448     *
449     * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
450     */
451  static s8 ccs_find_yesno(const char *string, const char *find)  static s8 ccs_find_yesno(const char *string, const char *find)
452  {  {
453          const char *cp = strstr(string, find);          const char *cp = strstr(string, find);
454          if (cp) {          if (cp) {
455                  cp += strlen(find);                  cp += strlen(find);
456                  if (strncmp(cp, "=yes", 4))                  if (!strncmp(cp, "=yes", 4))
457                          return 1;                          return 1;
458                  else if (strncmp(cp, "=no", 3))                  else if (!strncmp(cp, "=no", 3))
459                          return 0;                          return 0;
460          }          }
461          return -1;          return -1;
462  }  }
463    
464  static void ccs_set_bool(bool *b, const char *string, const char *find)  /**
465  {   * ccs_set_uint - Set value for specified preference.
466          switch (ccs_find_yesno(string, find)) {   *
467          case 1:   * @i:      Pointer to "unsigned int".
468                  *b = true;   * @string: String to check.
469                  break;   * @find:   Name of keyword.
470          case 0:   *
471                  *b = false;   * Returns nothing.
472                  break;   */
         }  
 }  
   
473  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
474  {  {
475          const char *cp = strstr(string, find);          const char *cp = strstr(string, find);
# Line 356  static void ccs_set_uint(unsigned int *i Line 477  static void ccs_set_uint(unsigned int *i
477                  sscanf(cp + strlen(find), "=%u", i);                  sscanf(cp + strlen(find), "=%u", i);
478  }  }
479    
480  static void ccs_set_pref(const char *cp, const char *data,  /**
481                           struct ccs_profile *profile, const bool use_default)   * ccs_set_mode - Set mode for specified profile.
482  {   *
483          struct ccs_preference **pref;   * @name:    Name of functionality.
484          bool *verbose;   * @value:   Mode for @name.
485          if (!strcmp(data, "audit")) {   * @profile: Pointer to "struct ccs_profile".
486                  if (use_default) {   *
487                          pref = &profile->audit;   * Returns 0 on success, negative value otherwise.
488                          goto set_default;   */
489                  }  static int ccs_set_mode(char *name, const char *value,
490                  profile->audit = &profile->preference;                          struct ccs_profile *profile)
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 ccs_set_uint(&profile->preference.audit_max_grant_log, cp,  
                              "max_grant_log");  
                 ccs_set_uint(&profile->preference.audit_max_reject_log, cp,  
                              "max_reject_log");  
 #endif  
                 ccs_set_bool(&profile->preference.audit_task_info, cp,  
                              "task_info");  
                 ccs_set_bool(&profile->preference.audit_path_info, cp,  
                              "path_info");  
                 return;  
         }  
         if (!strcmp(data, "enforcing")) {  
                 if (use_default) {  
                         pref = &profile->enforcing;  
                         goto set_default;  
                 }  
                 profile->enforcing = &profile->preference;  
                 ccs_set_uint(&profile->preference.enforcing_penalty, cp,  
                              "penalty");  
                 verbose = &profile->preference.enforcing_verbose;  
                 goto set_verbose;  
         }  
         if (!strcmp(data, "permissive")) {  
                 if (use_default) {  
                         pref = &profile->permissive;  
                         goto set_default;  
                 }  
                 profile->permissive = &profile->preference;  
                 verbose = &profile->preference.permissive_verbose;  
                 goto set_verbose;  
         }  
         if (!strcmp(data, "learning")) {  
                 if (use_default) {  
                         pref = &profile->learning;  
                         goto set_default;  
                 }  
                 profile->learning = &profile->preference;  
                 ccs_set_uint(&profile->preference.learning_max_entry, cp,  
                              "max_entry");  
                 ccs_set_bool(&profile->preference.learning_exec_realpath, cp,  
                              "exec.realpath");  
                 ccs_set_bool(&profile->preference.learning_exec_argv0, cp,  
                              "exec.argv0");  
                 ccs_set_bool(&profile->preference.learning_symlink_target, cp,  
                              "symlink.target");  
                 verbose = &profile->preference.learning_verbose;  
                 goto set_verbose;  
         }  
         return;  
  set_default:  
         *pref = &ccs_default_profile.preference;  
         return;  
  set_verbose:  
         ccs_set_bool(verbose, cp, "verbose");  
 }  
   
 static int ccs_set_mode(const char *cp, char *data,  
                         struct ccs_profile *profile, const bool use_default)  
491  {  {
492          u8 i;          u8 i;
493          u8 config;          u8 config;
494          if (!strcmp(data, "CONFIG")) {          if (!strcmp(name, "CONFIG")) {
495                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
                         + CCS_MAX_MAC_CATEGORY_INDEX;  
496                  config = profile->default_config;                  config = profile->default_config;
497          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
498                  config = 0;                  config = 0;
499                  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;
500                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                       i++) {
501                          if (strcmp(data, ccs_mac_keywords[i]))                          int len = 0;
502                            if (i < CCS_MAX_MAC_INDEX) {
503                                    const u8 c = ccs_index2category[i];
504                                    const char *category =
505                                            ccs_category_keywords[c];
506                                    len = strlen(category);
507                                    if (strncmp(name, category, len) ||
508                                        name[len++] != ':' || name[len++] != ':')
509                                            continue;
510                            }
511                            if (strcmp(name + len, ccs_mac_keywords[i]))
512                                  continue;                                  continue;
513                          config = profile->config[i];                          config = profile->config[i];
514                          break;                          break;
515                  }                  }
516                  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)  
517                          return -EINVAL;                          return -EINVAL;
518          } else {          } else {
519                  return -EINVAL;                  return -EINVAL;
520          }          }
521          if (use_default) {          if (strstr(value, "use_default")) {
522                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
523          } else {          } else {
524                  u8 mode;                  u8 mode;
525                  for (mode = 0; mode <= 3; mode--)                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
526                          if (strstr(cp, ccs_mode_4[mode]))                          if (strstr(value, ccs_mode[mode]))
527                                  /*                                  /*
528                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
529                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
530                                   */                                   */
531                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
532                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
533                          switch (ccs_find_yesno(cp, "grant_log")) {                          switch (ccs_find_yesno(value, "grant_log")) {
534                          case 1:                          case 1:
535                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
536                                  break;                                  break;
# Line 469  static int ccs_set_mode(const char *cp, Line 538  static int ccs_set_mode(const char *cp,
538                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
539                                  break;                                  break;
540                          }                          }
541                          switch (ccs_find_yesno(cp, "reject_log")) {                          switch (ccs_find_yesno(value, "reject_log")) {
542                          case 1:                          case 1:
543                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
544                                  break;                                  break;
# Line 478  static int ccs_set_mode(const char *cp, Line 547  static int ccs_set_mode(const char *cp,
547                                  break;                                  break;
548                          }                          }
549                  }                  }
 #endif  
550          }          }
551          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)  
552                  profile->config[i] = config;                  profile->config[i] = config;
553          else if (config != CCS_CONFIG_USE_DEFAULT)          else if (config != CCS_CONFIG_USE_DEFAULT)
554                  profile->default_config = config;                  profile->default_config = config;
# Line 498  static int ccs_set_mode(const char *cp, Line 565  static int ccs_set_mode(const char *cp,
565  static int ccs_write_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
566  {  {
567          char *data = head->write_buf;          char *data = head->write_buf;
568          bool use_default = false;          unsigned int i;
569          char *cp;          char *cp;
         int i;  
570          struct ccs_profile *profile;          struct ccs_profile *profile;
571          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
572                  return 0;                  return 0;
573          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
574          if (data == cp) {          if (*cp != '-')
575                  profile = &ccs_default_profile;                  return -EINVAL;
576          } else {          data = cp + 1;
577                  if (*cp != '-')          profile = ccs_assign_profile(i);
578                          return -EINVAL;          if (!profile)
579                  data = cp + 1;                  return -EINVAL;
                 profile = ccs_assign_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
         }  
580          cp = strchr(data, '=');          cp = strchr(data, '=');
581          if (!cp)          if (!cp)
582                  return -EINVAL;                  return -EINVAL;
583          *cp++ = '\0';          *cp++ = '\0';
         if (profile != &ccs_default_profile)  
                 use_default = strstr(cp, "use_default") != NULL;  
         if (ccs_str_starts(&data, "PREFERENCE::")) {  
                 ccs_set_pref(cp, data, profile, use_default);  
                 return 0;  
         }  
         if (profile == &ccs_default_profile)  
                 return -EINVAL;  
584          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
585                  const struct ccs_path_info *old_comment = profile->comment;                  const struct ccs_path_info *old_comment = profile->comment;
586                  profile->comment = ccs_get_name(cp);                  profile->comment = ccs_get_name(cp);
587                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
588                  return 0;                  return 0;
589          }          }
590          return ccs_set_mode(cp, data, profile, use_default);          if (!strcmp(data, "PREFERENCE")) {
591                    for (i = 0; i < CCS_MAX_PREF; i++)
592                            ccs_set_uint(&profile->pref[i], cp,
593                                         ccs_pref_keywords[i]);
594                    return 0;
595            }
596            return ccs_set_mode(data, cp, profile);
597  }  }
598    
599  static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)  /**
600     * ccs_print_config - Print mode for specified functionality.
601     *
602     * @head:   Pointer to "struct ccs_io_buffer".
603     * @config: Mode for that functionality.
604     *
605     * Returns nothing.
606     *
607     * Caller prints functionality's name.
608     */
609    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
610  {  {
611          struct ccs_preference *pref = &ccs_default_profile.preference;          ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
612          const struct ccs_profile *profile = idx >= 0 ?                        ccs_mode[config & 3],
613                  ccs_profile_ptr[idx] : NULL;                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
614          char buffer[16] = "";                        ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
         if (profile) {  
                 buffer[sizeof(buffer) - 1] = '\0';  
                 snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);  
         }  
         if (profile) {  
                 pref = profile->audit;  
                 if (pref == &ccs_default_profile.preference)  
                         pref = NULL;  
         }  
         if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    "max_grant_log=%u max_reject_log=%u "  
 #endif  
                                    "task_info=%s path_info=%s }\n", buffer,  
                                    "audit",  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    pref->audit_max_grant_log,  
                                    pref->audit_max_reject_log,  
 #endif  
                                    ccs_yesno(pref->audit_task_info),  
                                    ccs_yesno(pref->audit_path_info)))  
                 return false;  
         if (profile) {  
                 pref = profile->learning;  
                 if (pref == &ccs_default_profile.preference)  
                         pref = NULL;  
         }  
         if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "  
                                    "verbose=%s max_entry=%u exec.realpath=%s "  
                                    "exec.argv0=%s symlink.target=%s }\n",  
                                    buffer, "learning",  
                                    ccs_yesno(pref->learning_verbose),  
                                    pref->learning_max_entry,  
                                    ccs_yesno(pref->learning_exec_realpath),  
                                    ccs_yesno(pref->learning_exec_argv0),  
                                    ccs_yesno(pref->learning_symlink_target)))  
                 return false;  
         if (profile) {  
                 pref = profile->permissive;  
                 if (pref == &ccs_default_profile.preference)  
                         pref = NULL;  
         }  
         if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",  
                                    buffer, "permissive",  
                                    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, "%sPREFERENCE::%s={ verbose=%s "  
                                       "penalty=%u }\n", buffer, "enforcing",  
                                       ccs_yesno(pref->enforcing_verbose),  
                                       pref->enforcing_penalty);  
615  }  }
616    
617  /**  /**
# Line 606  static bool ccs_print_preference(struct Line 621  static bool ccs_print_preference(struct
621   */   */
622  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
623  {  {
624          int index;          u8 index;
625          if (head->read_eof)          const struct ccs_profile *profile;
626                  return;  next:
627          if (head->read_bit)          index = head->r.index;
628                  goto body;          profile = ccs_profile_ptr[index];
629          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          switch (head->r.step) {
630          ccs_print_preference(head, -1);          case 0:
631          head->read_bit = 1;                  ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20100903");
632   body:                  head->r.step++;
633          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {                  break;
634                  bool done;          case 1:
635                  u8 config;                  for ( ; head->r.index < CCS_MAX_PROFILES;
636                  int i;                        head->r.index++)
637                  int pos;                          if (ccs_profile_ptr[head->r.index])
638                  const struct ccs_profile *profile = ccs_profile_ptr[index];                                  break;
639                  const struct ccs_path_info *comment;                  if (head->r.index == CCS_MAX_PROFILES)
640                  head->read_step = index;                          return;
641                  if (!profile)                  head->r.step++;
642                          continue;                  break;
643                  pos = head->read_avail;          case 2:
644                  comment = profile->comment;                  {
645                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                          u8 i;
646                                       comment ? comment->name : "");                          const struct ccs_path_info *comment = profile->comment;
647                  if (!done)                          ccs_io_printf(head, "%u-COMMENT=", index);
648                          goto out;                          ccs_set_string(head, comment ? comment->name : "");
649                  config = profile->default_config;                          ccs_set_lf(head);
650  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-PREFERENCE={ ", index);
651                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                          for (i = 0; i < CCS_MAX_PREF; i++)
652                                     "grant_log=%s reject_log=%s }\n", index,                                  ccs_io_printf(head, "%s=%u ",
653                                     "CONFIG", "", ccs_mode_4[config & 3],                                                ccs_pref_keywords[i],
654                                     ccs_yesno(config &                                                profile->pref[i]);
655                                               CCS_CONFIG_WANT_GRANT_LOG),                          ccs_set_string(head, " }\n");
656                                     ccs_yesno(config &                          head->r.step++;
657                                               CCS_CONFIG_WANT_REJECT_LOG)))                  }
658                          goto out;                  break;
659  #else          case 3:
660                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,                  {
661                                     "CONFIG", "", ccs_mode_4[config & 3]))                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
662                          goto out;                          ccs_print_config(head, profile->default_config);
663  #endif                          head->r.bit = 0;
664                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                          head->r.step++;
665                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                  }
666  #ifdef CONFIG_CCSECURITY_AUDIT                  break;
667                          const char *g;          case 4:
668                          const char *r;                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
669  #endif                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
670                          config = profile->config[i];                          const u8 i = head->r.bit;
671                            const u8 config = profile->config[i];
672                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
673                                  continue;                                  continue;
674  #ifdef CONFIG_CCSECURITY_AUDIT                          if (i < CCS_MAX_MAC_INDEX)
675                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                  ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
676                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                                                ccs_category_keywords
677                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                                                [ccs_index2category[i]],
678                                             "grant_log=%s reject_log=%s }\n",                                                ccs_mac_keywords[i]);
679                                             index, "CONFIG::",                          else
680                                             ccs_mac_keywords[i],                                  ccs_io_printf(head, "%u-CONFIG::%s", index,
681                                             ccs_mode_4[config & 3], g, r))                                                ccs_mac_keywords[i]);
682                                  goto out;                          ccs_print_config(head, config);
683  #else                          head->r.bit++;
684                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",                          break;
685                                             index, "CONFIG::",                  }
686                                             ccs_mac_keywords[i],                  if (head->r.bit == CCS_MAX_MAC_INDEX
687                                             ccs_mode_4[config & 3]))                      + CCS_MAX_MAC_CATEGORY_INDEX) {
688                                  goto out;                          head->r.index++;
689  #endif                          head->r.step = 1;
690                  }                  }
                 if (!ccs_print_preference(head, index))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
691                  break;                  break;
692          }          }
693          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
694                  head->read_eof = true;                  goto next;
695  }  }
696    
697  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,  /**
698                                     const struct ccs_acl_head *b)   * ccs_same_manager - Check for duplicated "struct ccs_manager" entry.
699     *
700     * @a: Pointer to "struct ccs_acl_head".
701     * @b: Pointer to "struct ccs_acl_head".
702     *
703     * Returns true if @a and @b are duplicated, false otherwise.
704     */
705    static bool ccs_same_manager(const struct ccs_acl_head *a,
706                                 const struct ccs_acl_head *b)
707  {  {
708          return container_of(a, struct ccs_manager, head)->manager          return container_of(a, struct ccs_manager, head)->manager
709                  == container_of(b, struct ccs_manager, head)->manager;                  == container_of(b, struct ccs_manager, head)->manager;
# Line 714  static int ccs_update_manager_entry(cons Line 733  static int ccs_update_manager_entry(cons
733          if (!e.manager)          if (!e.manager)
734                  return error;                  return error;
735          error = ccs_update_policy(&e.head, sizeof(e), is_delete,          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
736                                    CCS_ID_MANAGER, ccs_same_manager_entry);                                    &ccs_policy_list[CCS_ID_MANAGER],
737                                      ccs_same_manager);
738          ccs_put_name(e.manager);          ccs_put_name(e.manager);
739          return error;          return error;
740  }  }
# Line 729  static int ccs_update_manager_entry(cons Line 749  static int ccs_update_manager_entry(cons
749  static int ccs_write_manager(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
750  {  {
751          char *data = head->write_buf;          char *data = head->write_buf;
752          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, "delete ");
753          if (!strcmp(data, "manage_by_non_root")) {          if (!strcmp(data, "manage_by_non_root")) {
754                  ccs_manage_by_non_root = !is_delete;                  ccs_manage_by_non_root = !is_delete;
755                  return 0;                  return 0;
# Line 746  static int ccs_write_manager(struct ccs_ Line 766  static int ccs_write_manager(struct ccs_
766   */   */
767  static void ccs_read_manager(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
768  {  {
769          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
770                  return;                  return;
771          list_for_each_cookie(pos, head->read_var2,          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
772                               &ccs_policy_list[CCS_ID_MANAGER]) {                  struct ccs_manager *ptr =
773                  struct ccs_manager *ptr                          list_entry(head->r.acl, typeof(*ptr), head.list);
                         = list_entry(pos, typeof(*ptr), head.list);  
774                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
775                          continue;                          continue;
776                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
777                          return;                          return;
778                    ccs_set_string(head, ptr->manager->name);
779                    ccs_set_lf(head);
780          }          }
781          head->read_eof = true;          head->r.eof = true;
782  }  }
783    
784  /**  /**
# Line 773  static bool ccs_manager(void) Line 793  static bool ccs_manager(void)
793  {  {
794          struct ccs_manager *ptr;          struct ccs_manager *ptr;
795          const char *exe;          const char *exe;
796          struct task_struct *task = current;          struct ccs_security *task = ccs_current_security();
797          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
798                  = ccs_current_domain()->domainname;                  = ccs_current_domain()->domainname;
799          bool found = false;          bool found = false;
# Line 784  static bool ccs_manager(void) Line 804  static bool ccs_manager(void)
804          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
805                  return false;                  return false;
806          exe = ccs_get_exe();          exe = ccs_get_exe();
807          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],          list_for_each_entry_srcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
808                                  head.list) {                                   head.list, &ccs_ss) {
809                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
810                          continue;                          continue;
811                  if (ptr->is_domain) {                  if (ptr->is_domain) {
# Line 814  static bool ccs_manager(void) Line 834  static bool ccs_manager(void)
834  }  }
835    
836  /**  /**
  * 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.  
  */  
 static char *ccs_find_condition_part(char *data)  
 {  
         char *cp = strstr(data, " if ");  
         if (!cp)  
                 cp = strstr(data, " ; set ");  
         if (cp)  
                 *cp++ = '\0';  
         return cp;  
 }  
   
 /**  
837   * ccs_select_one - Parse select command.   * ccs_select_one - Parse select command.
838   *   *
839   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 846  static bool ccs_select_one(struct ccs_io Line 848  static bool ccs_select_one(struct ccs_io
848          unsigned int pid;          unsigned int pid;
849          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
850          bool global_pid = false;          bool global_pid = false;
851          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
852                  head->read_execute_only = true;                  head->r.print_execute_only = true;
853                  return true;                  return true;
854          }          }
855          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
# Line 871  static bool ccs_select_one(struct ccs_io Line 873  static bool ccs_select_one(struct ccs_io
873                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
874          } else          } else
875                  return false;                  return false;
876          head->write_var1 = domain;          head->w.domain = domain;
877          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
878          if (!head->read_buf)          if (!head->read_buf)
879                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
880          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
881            head->r.print_this_domain_only = true;
882            if (domain)
883                    head->r.domain = &domain->list;
884            else
885                    head->r.eof = true;
886          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
887          head->read_single_domain = true;          if (domain && domain->is_deleted)
888          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");  
         }  
889          return true;          return true;
890  }  }
891    
892    /**
893     * ccs_same_handler_acl - Check for duplicated "struct ccs_handler_acl" entry.
894     *
895     * @a: Pointer to "struct ccs_acl_info".
896     * @b: Pointer to "struct ccs_acl_info".
897     *
898     * Returns true if @a and @b are duplicated, false otherwise.
899     */
900    static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
901                                     const struct ccs_acl_info *b)
902    {
903            const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
904            const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
905            return p1->handler == p2->handler;
906    }
907    
908    /**
909     * ccs_same_task_acl - Check for duplicated "struct ccs_task_acl" entry.
910     *
911     * @a: Pointer to "struct ccs_acl_info".
912     * @b: Pointer to "struct ccs_acl_info".
913     *
914     * Returns true if @a and @b are duplicated, false otherwise.
915     */
916    static bool ccs_same_task_acl(const struct ccs_acl_info *a,
917                                  const struct ccs_acl_info *b)
918    {
919            const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
920            const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
921            return p1->domainname == p2->domainname;
922    }
923    
924    /**
925     * ccs_write_task - Update task related list.
926     *
927     * @param: Pointer to "struct ccs_acl_param".
928     *
929     * Returns 0 on success, negative value otherwise.
930     */
931    static int ccs_write_task(struct ccs_acl_param *param)
932    {
933            int error;
934            const bool is_auto = ccs_str_starts(&param->data,
935                                                "auto_domain_transition ");
936            if (!is_auto && !ccs_str_starts(&param->data,
937                                            "manual_domain_transition ")) {
938                    struct ccs_handler_acl e = { };
939                    char *handler;
940                    if (ccs_str_starts(&param->data, "auto_execute_handler "))
941                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
942                    else if (ccs_str_starts(&param->data,
943                                            "denied_execute_handler "))
944                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
945                    else
946                            return -EINVAL;
947                    handler = ccs_read_token(param);
948                    if (!ccs_correct_path(handler))
949                            return -EINVAL;
950                    e.handler = ccs_get_name(handler);
951                    if (!e.handler)
952                            return -ENOMEM;
953                    if (e.handler->is_patterned)
954                            error = -EINVAL; /* No patterns allowed. */
955                    else
956                            error = ccs_update_domain(&e.head, sizeof(e), param,
957                                                      ccs_same_handler_acl, NULL);
958                    ccs_put_name(e.handler);
959            } else {
960                    struct ccs_task_acl e = {
961                            .head.type = is_auto ?
962                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
963                            .domainname = ccs_get_domainname(param),
964                    };
965                    if (!e.domainname)
966                            error = -EINVAL;
967                    else
968                            error = ccs_update_domain(&e.head, sizeof(e), param,
969                                                      ccs_same_task_acl, NULL);
970                    ccs_put_name(e.domainname);
971            }
972            return error;
973    }
974    
975    /**
976     * ccs_write_domain2 - Write domain policy.
977     *
978     * @data:      Policy to be interpreted.
979     * @domain:    Pointer to "struct ccs_domain_info".
980     * @is_delete: True if it is a delete request.
981     *
982     * Returns 0 on success, negative value otherwise.
983     */
984  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
985                               const bool is_delete)                               const bool is_delete)
986  {  {
987            struct ccs_acl_param param = {
988                    .data = data,
989                    .domain = domain,
990                    .is_delete = is_delete,
991            };
992          static const struct {          static const struct {
993                  const char *keyword;                  const char *keyword;
994                  int (*write) (char *, struct ccs_domain_info *,                  int (*write) (struct ccs_acl_param *);
995                                struct ccs_condition *, const bool);          } ccs_callback[7] = {
996          } ccs_callback[5] = {                  { "file ", ccs_write_file },
997                  { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },                  { "network inet ", ccs_write_inet_network },
998                  { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },                  { "network unix ", ccs_write_unix_network },
999                  { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },                  { "misc ", ccs_write_misc },
1000                  { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },                  { "capability ", ccs_write_capability },
1001                  { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }                  { "ipc ", ccs_write_ipc },
1002                    { "task ", ccs_write_task },
1003          };          };
         int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,  
                       const bool) = ccs_write_file;  
         int error;  
1004          u8 i;          u8 i;
1005          struct ccs_condition *cond = NULL;          for (i = 0; i < 7; i++) {
1006          char *cp = ccs_find_condition_part(data);                  if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         for (i = 0; i < 5; i++) {  
                 if (!ccs_str_starts(&data, ccs_callback[i].keyword))  
1007                          continue;                          continue;
1008                  write = ccs_callback[i].write;                  return ccs_callback[i].write(&param);
                 break;  
1009          }          }
1010          error = write(data, domain, cond, is_delete);          return -EINVAL;
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
1011  }  }
1012    
1013  static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {  /* String table for domain flags. */
1014          [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",  const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1015          [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",          [CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
1016          [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]          [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
         = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",  
         [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]  
         = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",  
         [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"  
1017  };  };
1018            
1019  /**  /**
1020   * ccs_write_domain - Write domain policy.   * ccs_write_domain - Write domain policy.
1021   *   *
# Line 953  static const char *ccs_dif[CCS_MAX_DOMAI Line 1026  static const char *ccs_dif[CCS_MAX_DOMAI
1026  static int ccs_write_domain(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1027  {  {
1028          char *data = head->write_buf;          char *data = head->write_buf;
1029          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1030          bool is_delete = false;          bool is_delete = false;
1031          bool is_select = false;          bool is_select = false;
1032          unsigned int profile;          unsigned int profile;
1033          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, "delete "))
1034                  is_delete = true;                  is_delete = true;
1035          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, "select "))
1036                  is_select = true;                  is_select = true;
1037          if (is_select && ccs_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1038                  return 0;                  return 0;
# Line 973  static int ccs_write_domain(struct ccs_i Line 1046  static int ccs_write_domain(struct ccs_i
1046                  else if (is_select)                  else if (is_select)
1047                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1048                  else                  else
1049                          domain = ccs_assign_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
1050                  head->write_var1 = domain;                  head->w.domain = domain;
1051                  return 0;                  return 0;
1052          }          }
1053          if (!domain)          if (!domain)
1054                  return -EINVAL;                  return -EINVAL;
1055    
1056          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, "use_profile %u\n", &profile) == 1
1057              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1058                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1059                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1060                  return 0;                  return 0;
1061          }          }
1062            if (sscanf(data, "use_group %u\n", &profile) == 1
1063                && profile < CCS_MAX_ACL_GROUPS) {
1064                    domain->group = (u8) profile;
1065                    return 0;
1066            }
1067          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1068                  const char *cp = ccs_dif[profile];                  const char *cp = ccs_dif[profile];
1069                  if (strncmp(data, cp, strlen(cp) - 1))                  if (strncmp(data, cp, strlen(cp) - 1))
# Line 1002  static int ccs_write_domain(struct ccs_i Line 1080  static int ccs_write_domain(struct ccs_i
1080   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1081   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
1082   *   *
1083   * Returns true on success, false otherwise.   * Returns nothing.
1084   */   */
1085  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1086                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1087  {  {
1088          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1089          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1090                  head->read_avail--;                  ccs_set_space(head);
1091          if (ptr->is_group)          if (ptr->is_group) {
1092                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1093                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1094          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1095  }                  if (cond)
1096                            ccs_set_string(head, "\"");
1097  /**                  ccs_set_string(head, ptr->filename->name);
1098   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1099   *                          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;  
1100          }          }
1101  }  }
1102    
1103  /**  /**
  * 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);  
 }  
   
 /**  
1104   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1105   *   *
1106   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1107   * @ptr:        Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
1108   *   *
1109   * Returns true on success, false otherwise.   * Returns nothing.
1110   */   */
1111  static bool ccs_print_number_union(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1112                                     const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1113  {  {
1114          return ccs_print_number_union_common(head, ptr, true);          if (!head->r.print_cond_part)
1115  }                  ccs_set_space(head);
1116            if (ptr->is_group) {
1117  /**                  ccs_set_string(head, "@");
1118   * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.                  ccs_set_string(head, ptr->group->group_name->name);
1119   *          } else {
1120   * @head:       Pointer to "struct ccs_io_buffer".                  int i;
1121   * @ptr:        Pointer to "struct ccs_number_union".                  unsigned long min = ptr->values[0];
1122   *                  const unsigned long max = ptr->values[1];
1123   * Returns true on success, false otherwise.                  u8 min_type = ptr->value_type[0];
1124   */                  const u8 max_type = ptr->value_type[1];
1125  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,                  char buffer[128];
1126                                             const struct ccs_number_union *ptr)                  buffer[0] = '\0';
1127  {                  for (i = 0; i < 2; i++) {
1128          return ccs_print_number_union_common(head, ptr, false);                          switch (min_type) {
1129                            case CCS_VALUE_TYPE_HEXADECIMAL:
1130                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1131                                                  min);
1132                                    break;
1133                            case CCS_VALUE_TYPE_OCTAL:
1134                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1135                                                  min);
1136                                    break;
1137                            default:
1138                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1139                                                  min);
1140                                    break;
1141                            }
1142                            if (min == max && min_type == max_type)
1143                                    break;
1144                            ccs_addprintf(buffer, sizeof(buffer), "-");
1145                            min_type = max_type;
1146                            min = max;
1147                    }
1148                    ccs_io_printf(head, "%s", buffer);
1149            }
1150  }  }
1151    
1152  /**  /**
1153   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1154   *   *
1155   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1156   * @cond: Pointer to "struct ccs_condition". Maybe NULL.   * @cond: Pointer to "struct ccs_condition".
1157   *   *
1158   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1159   */   */
1160  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1161                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1162  {  {
1163          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1164          const struct ccs_number_union *numbers_p;          case 0:
1165          const struct ccs_name_union *names_p;                  head->r.cond_index = 0;
1166          const struct ccs_argv *argv;                  head->r.cond_step++;
1167          const struct ccs_envp *envp;                  /* fall through */
1168          u16 condc;          case 1:
1169          u16 i;                  {
1170          u16 j;                          const u16 condc = cond->condc;
1171          char buffer[32];                          const struct ccs_condition_element *condp =
1172          if (!cond)                                  (typeof(condp)) (cond + 1);
1173                  goto no_condition;                          const struct ccs_number_union *numbers_p =
1174          condc = cond->condc;                                  (typeof(numbers_p)) (condp + condc);
1175          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_name_union *names_p =
1176          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(names_p))
1177          names_p = (const struct ccs_name_union *)                                  (numbers_p + cond->numbers_count);
1178                  (numbers_p + cond->numbers_count);                          const struct ccs_argv *argv =
1179          argv = (const struct ccs_argv *) (names_p + cond->names_count);                                  (typeof(argv)) (names_p + cond->names_count);
1180          envp = (const struct ccs_envp *) (argv + cond->argc);                          const struct ccs_envp *envp =
1181          memset(buffer, 0, sizeof(buffer));                                  (typeof(envp)) (argv + cond->argc);
1182          if (condc && !ccs_io_printf(head, "%s", " if"))                          u16 skip;
1183                  goto out;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1184          for (i = 0; i < condc; i++) {                                  const u8 left = condp->left;
1185                  const u8 match = condp->equals;                                  const u8 right = condp->right;
1186                  const u8 left = condp->left;                                  condp++;
1187                  const u8 right = condp->right;                                  switch (left) {
1188                  condp++;                                  case CCS_ARGV_ENTRY:
1189                  switch (left) {                                          argv++;
1190                  case CCS_ARGV_ENTRY:                                          continue;
1191                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  case CCS_ENVP_ENTRY:
1192                                             argv->index, argv->is_not ?                                          envp++;
1193                                             "!=" : "=", argv->value->name))                                          continue;
1194                                  goto out;                                  case CCS_NUMBER_UNION:
1195                          argv++;                                          numbers_p++;
1196                          continue;                                          break;
1197                  case CCS_ENVP_ENTRY:                                  }
1198                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  switch (right) {
1199                                             envp->name->name, envp->is_not ?                                  case CCS_NAME_UNION:
1200                                             "!=" : "="))                                          names_p++;
1201                                  goto out;                                          break;
1202                          if (envp->value) {                                  case CCS_NUMBER_UNION:
1203                                  if (!ccs_io_printf(head, "\"%s\"",                                          numbers_p++;
1204                                                     envp->value->name))                                          break;
1205                                          goto out;                                  }
1206                          } else {                          }
1207                                  if (!ccs_io_printf(head, "NULL"))                          while (head->r.cond_index < condc) {
1208                                          goto out;                                  const u8 match = condp->equals;
1209                                    const u8 left = condp->left;
1210                                    const u8 right = condp->right;
1211                                    if (!ccs_flush(head))
1212                                            return false;
1213                                    condp++;
1214                                    head->r.cond_index++;
1215                                    ccs_set_space(head);
1216                                    switch (left) {
1217                                    case CCS_ARGV_ENTRY:
1218                                            ccs_io_printf(head,
1219                                                          "exec.argv[%u]%s\"%s\"",
1220                                                          argv->index,
1221                                                          argv->is_not ?
1222                                                          "!=" : "=",
1223                                                          argv->value->name);
1224                                            argv++;
1225                                            continue;
1226                                    case CCS_ENVP_ENTRY:
1227                                            ccs_io_printf(head,
1228                                                          "exec.envp[\"%s\"]%s",
1229                                                          envp->name->name,
1230                                                          envp->is_not ?
1231                                                          "!=" : "=");
1232                                            if (envp->value) {
1233                                                    ccs_set_string(head, "\"");
1234                                                    ccs_set_string(head, envp->
1235                                                                   value->name);
1236                                                    ccs_set_string(head, "\"");
1237                                            } else {
1238                                                    ccs_set_string(head, "NULL");
1239                                            }
1240                                            envp++;
1241                                            continue;
1242                                    case CCS_NUMBER_UNION:
1243                                            ccs_print_number_union(head,
1244                                                                   numbers_p++);
1245                                            break;
1246                                    default:
1247                                            ccs_set_string(head,
1248                                                   ccs_condition_keyword[left]);
1249                                            break;
1250                                    }
1251                                    ccs_set_string(head, match ? "=" : "!=");
1252                                    switch (right) {
1253                                    case CCS_NAME_UNION:
1254                                            ccs_print_name_union(head, names_p++);
1255                                            break;
1256                                    case CCS_NUMBER_UNION:
1257                                            ccs_print_number_union(head,
1258                                                                   numbers_p++);
1259                                            break;
1260                                    default:
1261                                            ccs_set_string(head,
1262                                                   ccs_condition_keyword[right]);
1263                                            break;
1264                                    }
1265                          }                          }
                         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;  
1266                  }                  }
1267                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1268                          goto out;                  /* fall through */
1269                  switch (right) {          case 2:
1270                  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;  
1271                          break;                          break;
1272                    head->r.cond_step++;
1273                    /* fall through */
1274            case 3:
1275                    if (cond->grant_log != CCS_GRANTLOG_AUTO)
1276                            ccs_io_printf(head, " grant_log=%s",
1277                                          ccs_yesno(cond->grant_log ==
1278                                                    CCS_GRANTLOG_YES));
1279                    if (cond->transit) {
1280                            ccs_set_string(head, " auto_domain_transitition=\"");
1281                            ccs_set_string(head, cond->transit->name);
1282                            ccs_set_string(head, "\"");
1283                  }                  }
1284          }                  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"))  
1285                  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".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_acl(struct ccs_io_buffer *head,  
                                struct ccs_path_acl *ptr)  
 {  
         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, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1286          }          }
1287          head->read_bit = 0;          return false;
         return true;  
1288  }  }
1289    
1290  /**  /**
1291   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_fns - Find next set bit.
1292   *   *
1293   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1294   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @bit:  First bit to find.
1295   *   *
1296   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1297   */   */
1298  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)  
1299  {  {
1300          int pos;          for ( ; bit < 8; bit++)
1301          u8 bit;                  if (perm & (1 << bit))
1302          const u16 perm = ptr->perm;                          break;
1303          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, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1304  }  }
1305    
1306  /**  /**
1307   * ccs_print_path2_acl - Print a path2 ACL entry.   * ccs_set_group - Print "acl_group " header keyword.
1308   *   *
1309   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
  * @ptr:  Pointer to "struct ccs_path2_acl".  
1310   *   *
1311   * Returns true on success, false otherwise.   * Returns nothing.
1312   */   */
1313  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)  
1314  {  {
1315          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1316          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, ptr->head.cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1317  }  }
1318    
1319  /**  /**
1320   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1321   *   *
1322   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1323   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
1324   *   *
1325   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1326   */   */
1327  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1328                                        struct ccs_path_number_acl *ptr)                              const struct ccs_acl_info *acl)
1329  {  {
1330          int pos;          const u8 acl_type = acl->type;
1331          u8 bit;          u8 bit;
1332          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1333          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1334               bit++) {          if (acl->is_deleted)
1335                  if (!(perm & (1 << bit)))                  return true;
1336                          continue;  next:
1337                  pos = head->read_avail;          bit = head->r.bit;
1338                  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, ptr->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".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_env_acl(struct ccs_io_buffer *head,  
                               struct ccs_env_acl *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||  
             !ccs_print_condition(head, ptr->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".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  
                                      struct ccs_capability_acl *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",  
                            ccs_cap2keyword(ptr->operation)) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
1339                  return false;                  return false;
1340          }          else if (acl_type == CCS_TYPE_PATH_ACL) {
1341          return true;                  struct ccs_path_acl *ptr
1342  }                          = container_of(acl, typeof(*ptr), head);
1343                    const u16 perm = ptr->perm;
1344  /**                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1345   * ccs_print_network_acl - Print a network ACL entry.                          if (!(perm & (1 << bit)))
1346   *                                  continue;
1347   * @head: Pointer to "struct ccs_io_buffer".                          if (head->r.print_execute_only &&
1348   * @ptr:  Pointer to "struct ccs_ip_network_acl".                              bit != CCS_TYPE_EXECUTE
1349   *                              /* && bit != CCS_TYPE_TRANSIT */)
1350   * Returns true on success, false otherwise.                                  continue;
1351   */                          break;
1352  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                  }
1353                                    struct ccs_ip_network_acl *ptr)                  if (bit >= CCS_MAX_PATH_OPERATION)
1354  {                          goto done;
1355          int pos;                  ccs_set_group(head);
1356          u8 bit;                  ccs_set_string(head, "file ");
1357          const u8 perm = ptr->perm;                  ccs_set_string(head, ccs_path_keyword[bit]);
1358          char buf[128];                  ccs_print_name_union(head, &ptr->name);
1359          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {          } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1360                  const char *w[2] = { "", "" };                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1361                  if (!(perm & (1 << bit)))                  struct ccs_handler_acl *ptr
1362                          continue;                          = container_of(acl, typeof(*ptr), head);
1363                  pos = head->read_avail;                  ccs_set_group(head);
1364                    ccs_set_string(head, "task ");
1365                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1366                                   ? "auto_execute_handler " :
1367                                   "denied_execute_handler ");
1368                    ccs_set_string(head, ptr->handler->name);
1369            } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1370                       acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1371                    struct ccs_task_acl *ptr =
1372                            container_of(acl, typeof(*ptr), head);
1373                    ccs_set_group(head);
1374                    ccs_set_string(head, "task ");
1375                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1376                                   "auto_domain_transition " :
1377                                   "manual_domain_transition ");
1378                    ccs_set_string(head, ptr->domainname->name);
1379            } else if (head->r.print_execute_only) {
1380                    return true;
1381            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1382                    struct ccs_mkdev_acl *ptr =
1383                            container_of(acl, typeof(*ptr), head);
1384                    bit = ccs_fns(ptr->perm, bit);
1385                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1386                            goto done;
1387                    ccs_set_group(head);
1388                    ccs_set_string(head, "file ");
1389                    ccs_set_string(head, ccs_mac_keywords[ccs_pnnn2mac[bit]]);
1390                    ccs_print_name_union(head, &ptr->name);
1391                    ccs_print_number_union(head, &ptr->mode);
1392                    ccs_print_number_union(head, &ptr->major);
1393                    ccs_print_number_union(head, &ptr->minor);
1394            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1395                    struct ccs_path2_acl *ptr =
1396                            container_of(acl, typeof(*ptr), head);
1397                    bit = ccs_fns(ptr->perm, bit);
1398                    if (bit >= CCS_MAX_PATH2_OPERATION)
1399                            goto done;
1400                    ccs_set_group(head);
1401                    ccs_set_string(head, "file ");
1402                    ccs_set_string(head, ccs_mac_keywords[ccs_pp2mac[bit]]);
1403                    ccs_print_name_union(head, &ptr->name1);
1404                    ccs_print_name_union(head, &ptr->name2);
1405            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1406                    struct ccs_path_number_acl *ptr =
1407                            container_of(acl, typeof(*ptr), head);
1408                    bit = ccs_fns(ptr->perm, bit);
1409                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1410                            goto done;
1411                    ccs_set_group(head);
1412                    ccs_set_string(head, "file ");
1413                    ccs_set_string(head, ccs_mac_keywords[ccs_pn2mac[bit]]);
1414                    ccs_print_name_union(head, &ptr->name);
1415                    ccs_print_number_union(head, &ptr->number);
1416            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1417                    struct ccs_env_acl *ptr =
1418                            container_of(acl, typeof(*ptr), head);
1419                    ccs_set_group(head);
1420                    ccs_set_string(head, "misc env ");
1421                    ccs_set_string(head, ptr->env->name);
1422            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1423                    struct ccs_capability_acl *ptr =
1424                            container_of(acl, typeof(*ptr), head);
1425                    ccs_set_group(head);
1426                    ccs_set_string(head, "capability ");
1427                    ccs_set_string(head,
1428                                   ccs_mac_keywords[ccs_c2mac[ptr->operation]]);
1429            } else if (acl_type == CCS_TYPE_INET_ACL) {
1430                    struct ccs_inet_acl *ptr =
1431                            container_of(acl, typeof(*ptr), head);
1432                    bit = ccs_fns(ptr->perm, bit);
1433                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1434                            goto done;
1435                    ccs_set_group(head);
1436                    ccs_set_string(head, "network inet ");
1437                    ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1438                    ccs_set_space(head);
1439                    ccs_set_string(head, ccs_socket_keyword[bit]);
1440                    ccs_set_space(head);
1441                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1442                            char buf[128];
1443                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1444                          w[0] = "@";                          ccs_set_string(head, "@");
1445                          w[1] = ptr->address.group->group_name->name;                          ccs_set_string(head,
1446                                           ptr->address.group->group_name->name);
1447                          break;                          break;
1448                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1449                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1450                                         ptr->address.ipv4.max);                                         ptr->address.ipv4.max);
1451                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1452                          break;                          break;
1453                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1454                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1455                                         ptr->address.ipv6.max);                                         ptr->address.ipv6.max);
1456                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1457                          break;                          break;
1458                  }                  }
1459                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",                  ccs_print_number_union(head, &ptr->port);
1460                                     ccs_net2keyword(bit), w[0], w[1]) ||          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1461                      !ccs_print_number_union(head, &ptr->port) ||                  struct ccs_unix_acl *ptr =
1462                      !ccs_print_condition(head, ptr->head.cond))                          container_of(acl, typeof(*ptr), head);
1463                          goto out;                  bit = ccs_fns(ptr->perm, bit);
1464          }                  if (bit >= CCS_MAX_NETWORK_OPERATION)
1465          head->read_bit = 0;                          goto done;
1466          return true;                  ccs_set_group(head);
1467   out:                  ccs_set_string(head, "network unix ");
1468          head->read_bit = bit;                  ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1469          head->read_avail = pos;                  ccs_set_space(head);
1470          return false;                  ccs_set_string(head, ccs_socket_keyword[bit]);
1471  }                  ccs_print_name_union(head, &ptr->name);
1472            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1473  /**                  struct ccs_signal_acl *ptr =
1474   * ccs_print_signal_acl - Print a signal ACL entry.                          container_of(acl, typeof(*ptr), head);
1475   *                  ccs_set_group(head);
1476   * @head: Pointer to "struct ccs_io_buffer".                  ccs_set_string(head, "ipc signal ");
1477   * @ptr:  Pointer to "struct signale_acl".                  ccs_io_printf(head, "%u ", ptr->sig);
1478   *                  ccs_set_string(head, ptr->domainname->name);
1479   * Returns true on success, false otherwise.          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1480   */                  struct ccs_mount_acl *ptr =
1481  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,                          container_of(acl, typeof(*ptr), head);
1482                                   struct ccs_signal_acl *ptr)                  ccs_set_group(head);
1483  {                  ccs_io_printf(head, "file mount");
1484          const int pos = head->read_avail;                  ccs_print_name_union(head, &ptr->dev_name);
1485          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                  ccs_print_name_union(head, &ptr->dir_name);
1486                             ptr->sig, ptr->domainname->name) ||                  ccs_print_name_union(head, &ptr->fs_type);
1487              !ccs_print_condition(head, ptr->head.cond)) {                  ccs_print_number_union(head, &ptr->flags);
1488                  head->read_avail = pos;          }
1489                  return false;          head->r.bit = bit + 1;
1490          }          if (acl->cond) {
1491          return true;                  head->r.print_cond_part = true;
1492  }                  head->r.cond_step = 0;
1493                    if (!ccs_flush(head))
1494  /**                          return false;
1495   * ccs_print_execute_handler - Print an execute handler ACL entry.  print_cond_part:
1496   *                  if (!ccs_print_condition(head, acl->cond))
1497   * @head:    Pointer to "struct ccs_io_buffer".                          return false;
1498   * @keyword: Name of the keyword.                  head->r.print_cond_part = false;
1499   * @ptr:     Pointer to "struct ccs_execute_handler".          } else {
1500   *                  ccs_set_lf(head);
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_execute_handler(struct ccs_io_buffer *head,  
                                       const char *keyword,  
                                       struct ccs_execute_handler *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, "%s %s", keyword, ptr->handler->name) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
                 return false;  
1501          }          }
1502          return true;          switch (acl_type) {
1503  }          case CCS_TYPE_PATH_ACL:
1504            case CCS_TYPE_MKDEV_ACL:
1505  /**          case CCS_TYPE_PATH2_ACL:
1506   * ccs_print_mount_acl - Print a mount ACL entry.          case CCS_TYPE_PATH_NUMBER_ACL:
1507   *          case CCS_TYPE_INET_ACL:
1508   * @head: Pointer to "struct ccs_io_buffer".          case CCS_TYPE_UNIX_ACL:
1509   * @ptr:  Pointer to "struct ccs_mount_acl".                  goto next;
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl *ptr)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, ptr->head.cond)) {  
                 head->read_avail = pos;  
                 return false;  
1510          }          }
1511    done:
1512            head->r.bit = 0;
1513          return true;          return true;
1514  }  }
1515    
1516  /**  /**
  * 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 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, typeof(*acl), head);  
                 return ccs_print_path_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||  
             acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?  
                         CCS_KEYWORD_EXECUTE_HANDLER :  
                         CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler(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, typeof(*acl), head);  
                 return ccs_print_path_number3_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_path2_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_path_number_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_env_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_capability_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_network_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_signal_acl(head, acl);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, typeof(*acl), head);  
                 return ccs_print_mount_acl(head, acl);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
1517   * ccs_read_domain2 - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1518   *   *
1519   * @head:   Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1520   * @domain: Pointer to "struct ccs_domain_info".   * @domain: Pointer to "struct ccs_domain_info".
1521     * @index:  Index number.
1522   *   *
1523   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1524   *   *
1525   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1526   */   */
1527  static bool ccs_read_domain2(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1528                               struct ccs_domain_info *domain)                               struct ccs_domain_info *domain,
1529                                 const u8 index)
1530  {  {
1531          struct list_head *pos;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1532          /* Print ACL entries in the domain. */                  struct ccs_acl_info *ptr =
1533          list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {                          list_entry(head->r.acl, typeof(*ptr), list);
                 struct ccs_acl_info *ptr  
                         = list_entry(pos, struct ccs_acl_info, list);  
1534                  if (!ccs_print_entry(head, ptr))                  if (!ccs_print_entry(head, ptr))
1535                          return false;                          return false;
1536          }          }
1537            head->r.acl = NULL;
1538          return true;          return true;
1539  }  }
1540    
# Line 1636  static bool ccs_read_domain2(struct ccs_ Line 1547  static bool ccs_read_domain2(struct ccs_
1547   */   */
1548  static void ccs_read_domain(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1549  {  {
1550          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1551                  return;                  return;
1552          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1553                  struct ccs_domain_info *domain =                  struct ccs_domain_info *domain =
1554                          list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1555                  switch (head->read_step) {                  switch (head->r.step) {
1556                          u8 i;                          u8 i;
                         const char *w[CCS_MAX_DOMAIN_INFO_FLAGS];  
1557                  case 0:                  case 0:
1558                          if (domain->is_deleted && !head->read_single_domain)                          if (domain->is_deleted &&
1559                                !head->r.print_this_domain_only)
1560                                  continue;                                  continue;
1561                          /* Print domainname and flags. */                          /* Print domainname and flags. */
1562                            ccs_set_string(head, domain->domainname->name);
1563                            ccs_set_lf(head);
1564                            ccs_io_printf(head, "use_profile %u\n",
1565                                          domain->profile);
1566                            ccs_io_printf(head, "use_group %u\n", domain->group);
1567                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1568                                  w[i] = domain->flags[i] ? ccs_dif[i] : "";                                  if (domain->flags[i])
1569                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE                                          ccs_set_string(head, ccs_dif[i]);
1570                                             "%u\n%s%s%s%s%s\n",                          head->r.step++;
1571                                             domain->domainname->name,                          ccs_set_lf(head);
                                            domain->profile,  
                                            w[CCS_DIF_QUOTA_WARNED],  
                                            w[CCS_DIF_IGNORE_GLOBAL],  
                                            w[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ],  
                                            w[CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV],  
                                            w[CCS_DIF_TRANSITION_FAILED]))  
                                 return;  
                         head->read_step++;  
1572                          /* fall through */                          /* fall through */
1573                  case 1:                  case 1:
1574                          if (!ccs_read_domain2(head, domain))                          if (!ccs_read_domain2(head, domain, 0))
1575                                  return;                                  return;
1576                          head->read_step++;                          head->r.step++;
1577                          /* fall through */                          /* fall through */
1578                  case 2:                  case 2:
1579                          if (!ccs_io_printf(head, "\n"))                          if (!ccs_read_domain2(head, domain, 1))
1580                                    return;
1581                            head->r.step++;
1582                            if (!ccs_set_lf(head))
1583                                  return;                                  return;
1584                          head->read_step = 0;                          /* fall through */
1585                          if (head->read_single_domain)                  case 3:
1586                                  goto out;                          head->r.step = 0;
1587                            if (head->r.print_this_domain_only)
1588                                    goto done;
1589                  }                  }
1590          }          }
1591   out:  done:
1592          head->read_eof = true;          head->r.eof = true;
1593  }  }
1594    
1595  /**  /**
# Line 1728  static int ccs_write_domain_profile(stru Line 1640  static int ccs_write_domain_profile(stru
1640   */   */
1641  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1642  {  {
1643          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1644                  return;                  return;
1645          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1646                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1647                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1648                  if (domain->is_deleted)                  if (domain->is_deleted)
1649                          continue;                          continue;
1650                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1651                          return;                          return;
1652                    ccs_io_printf(head, "%u ", domain->profile);
1653                    ccs_set_string(head, domain->domainname->name);
1654                    ccs_set_lf(head);
1655          }          }
1656          head->read_eof = true;          head->r.eof = true;
1657  }  }
1658    
1659  /**  /**
# Line 1752  static void ccs_read_domain_profile(stru Line 1665  static void ccs_read_domain_profile(stru
1665   */   */
1666  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1667  {  {
1668          head->read_eof = false;          head->r.eof = false;
1669          return 0;          return 0;
1670  }  }
1671    
# Line 1778  static void ccs_read_pid(struct ccs_io_b Line 1691  static void ccs_read_pid(struct ccs_io_b
1691          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1692          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1693          if (!buf) {          if (!buf) {
1694                  head->read_eof = true;                  head->r.eof = true;
1695                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1696          }          }
1697          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1698                  return;                  return;
1699          head->read_eof = true;          head->r.eof = true;
1700          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1701                  task_info = true;                  task_info = true;
1702          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1800  static void ccs_read_pid(struct ccs_io_b Line 1713  static void ccs_read_pid(struct ccs_io_b
1713  #endif  #endif
1714          if (p) {          if (p) {
1715                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1716                  ccs_flags = p->ccs_flags;                  ccs_flags = ccs_task_flags(p);
1717          }          }
1718          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1719          if (!domain)          if (!domain)
1720                  return;                  return;
1721          if (!task_info)          if (!task_info) {
1722                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1723                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1724          else          } else {
1725                  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,  
1726                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1727                                          CCS_TASK_IS_MANAGER),                                          CCS_TASK_IS_MANAGER),
1728                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1729                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1730                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1731  }  }
1732    
1733    /* String table for domain transition control keywords. */
1734    static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1735            [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
1736            [CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
1737            [CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
1738            [CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
1739    };
1740    
1741    /* String table for grouping keywords. */
1742    static const char * const ccs_group_name[CCS_MAX_GROUP] = {
1743            [CCS_PATH_GROUP]    = "path_group ",
1744            [CCS_NUMBER_GROUP]  = "number_group ",
1745            [CCS_ADDRESS_GROUP] = "address_group ",
1746    };
1747    
1748  /**  /**
1749   * ccs_write_exception - Write exception policy.   * ccs_write_exception - Write exception policy.
1750   *   *
# Line 1830  static void ccs_read_pid(struct ccs_io_b Line 1755  static void ccs_read_pid(struct ccs_io_b
1755  static int ccs_write_exception(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1756  {  {
1757          char *data = head->write_buf;          char *data = head->write_buf;
1758          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, "delete ");
1759          u8 i;          u8 i;
1760          static const struct {          static const struct {
1761                  const char *keyword;                  const char *keyword;
1762                  int (*write) (char *, const bool, const u8);                  int (*write) (char *, const bool);
1763          } ccs_callback[8] = {          } ccs_callback[2] = {
1764                  { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },                  { "aggregator ",    ccs_write_aggregator },
1765                  { 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_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  
1766          };          };
1767          for (i = 0; i < 8; i++) {          for (i = 0; i < 2; i++)
1768                  if (ccs_str_starts(&data, ccs_callback[i].keyword))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1769                          return ccs_callback[i].write(data, is_delete, i < 2);                          return ccs_callback[i].write(data, is_delete);
1770          }          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1771          for (i = 0; i < CCS_MAX_GROUP; i++) {                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1772                  if (ccs_str_starts(&data, ccs_name[i]))                          return ccs_write_transition_control(data, is_delete,
1773                                                                i);
1774            for (i = 0; i < CCS_MAX_GROUP; i++)
1775                    if (ccs_str_starts(&data, ccs_group_name[i]))
1776                          return ccs_write_group(data, is_delete, i);                          return ccs_write_group(data, is_delete, i);
1777            if (ccs_str_starts(&data, "acl_group ")) {
1778                    unsigned int group;
1779                    if (sscanf(data, "%u", &group) == 1 &&
1780                        group < CCS_MAX_ACL_GROUPS) {
1781                            data = strchr(data, ' ');
1782                            if (data)
1783                                    return ccs_write_domain2(data + 1,
1784                                                             &ccs_acl_group[group],
1785                                                             is_delete);
1786                    }
1787          }          }
1788          return ccs_write_domain2(data, &ccs_global_domain, is_delete);          return -EINVAL;
1789  }  }
1790    
1791  /**  /**
# Line 1875  static int ccs_write_exception(struct cc Line 1800  static int ccs_write_exception(struct cc
1800   */   */
1801  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)
1802  {  {
1803          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]) {  
1804                  struct ccs_group *group =                  struct ccs_group *group =
1805                          list_entry(gpos, struct ccs_group, head.list);                          list_entry(head->r.group, typeof(*group), head.list);
1806                  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];  
1807                          struct ccs_acl_head *ptr =                          struct ccs_acl_head *ptr =
1808                                  list_entry(mpos, struct ccs_acl_head, list);                                  list_entry(head->r.acl, typeof(*ptr), list);
1809                          if (ptr->is_deleted)                          if (ptr->is_deleted)
1810                                  continue;                                  continue;
1811                            if (!ccs_flush(head))
1812                                    return false;
1813                            ccs_set_string(head, ccs_group_name[idx]);
1814                            ccs_set_string(head, group->group_name->name);
1815                          if (idx == CCS_PATH_GROUP) {                          if (idx == CCS_PATH_GROUP) {
1816                                  w[2] = container_of(ptr, struct ccs_path_group,                                  ccs_set_space(head);
1817                                                      head)->member_name->name;                                  ccs_set_string(head, container_of
1818                                                   (ptr, struct ccs_path_group,
1819                                                    head)->member_name->name);
1820                          } else if (idx == CCS_NUMBER_GROUP) {                          } else if (idx == CCS_NUMBER_GROUP) {
1821                                  w[2] = buffer;                                  ccs_print_number_union(head, &container_of
1822                                  ccs_print_number(buffer, sizeof(buffer),                                                 (ptr, struct ccs_number_group,
1823                                                   &container_of                                                  head)->number);
                                                  (ptr, struct ccs_number_group,  
                                                   head)->number);  
1824                          } else if (idx == CCS_ADDRESS_GROUP) {                          } else if (idx == CCS_ADDRESS_GROUP) {
1825                                    char buffer[128];
1826                                  struct ccs_address_group *member =                                  struct ccs_address_group *member =
1827                                          container_of(ptr, typeof(*member),                                          container_of(ptr, typeof(*member),
1828                                                       head);                                                       head);
                                 w[2] = buffer;  
1829                                  if (member->is_ipv6)                                  if (member->is_ipv6)
1830                                          ccs_print_ipv6(buffer, sizeof(buffer),                                          ccs_print_ipv6(buffer, sizeof(buffer),
1831                                                         member->min.ipv6,                                                         member->min.ipv6,
# Line 1917  static bool ccs_read_group(struct ccs_io Line 1834  static bool ccs_read_group(struct ccs_io
1834                                          ccs_print_ipv4(buffer, sizeof(buffer),                                          ccs_print_ipv4(buffer, sizeof(buffer),
1835                                                         member->min.ipv4,                                                         member->min.ipv4,
1836                                                         member->max.ipv4);                                                         member->max.ipv4);
1837                                    ccs_io_printf(head, " %s", buffer);
1838                          }                          }
1839                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],                          ccs_set_lf(head);
                                            w[2]))  
                                 return false;  
1840                  }                  }
1841                    head->r.acl = NULL;
1842          }          }
1843            head->r.group = NULL;
1844          return true;          return true;
1845  }  }
1846    
# Line 1938  static bool ccs_read_group(struct ccs_io Line 1856  static bool ccs_read_group(struct ccs_io
1856   */   */
1857  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)
1858  {  {
1859          struct list_head *pos;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1860          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {                  struct ccs_acl_head *acl =
1861                  const char *w[4] = { "", "", "", "" };                          container_of(head->r.acl, typeof(*acl), list);
                 char buffer[16];  
                 struct ccs_acl_head *acl = container_of(pos, typeof(*acl),  
                                                         list);  
1862                  if (acl->is_deleted)                  if (acl->is_deleted)
1863                          continue;                          continue;
1864                    if (!ccs_flush(head))
1865                            return false;
1866                  switch (idx) {                  switch (idx) {
1867                  case CCS_ID_DOMAIN_KEEPER:                  case CCS_ID_TRANSITION_CONTROL:
1868                          {                          {
1869                                  struct ccs_domain_keeper *ptr =                                  struct ccs_transition_control *ptr =
1870                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1871                                  w[0] = ptr->is_not ?                                  ccs_set_string(head,
1872                                          CCS_KEYWORD_NO_KEEP_DOMAIN :                                                 ccs_transition_type[ptr->type]);
1873                                          CCS_KEYWORD_KEEP_DOMAIN;                                  ccs_set_string(head, ptr->program ?
1874                                  if (ptr->program) {                                                 ptr->program->name : "any");
1875                                          w[1] = ptr->program->name;                                  ccs_set_string(head, " from ");
1876                                          w[2] = " from ";                                  ccs_set_string(head, ptr->domainname ?
1877                                  }                                                 ptr->domainname->name : "any");
                                 w[3] = ptr->domainname->name;  
                         }  
                         break;  
                 case CCS_ID_DOMAIN_INITIALIZER:  
                         {  
                                 struct ccs_domain_initializer *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = ptr->is_not ?  
                                         CCS_KEYWORD_NO_INITIALIZE_DOMAIN :  
                                         CCS_KEYWORD_INITIALIZE_DOMAIN;  
                                 w[1] = ptr->program->name;  
                                 if (ptr->domainname) {  
                                         w[2] = " from ";  
                                         w[3] = ptr->domainname->name;  
                                 }  
1878                          }                          }
1879                          break;                          break;
1880                  case CCS_ID_AGGREGATOR:                  case CCS_ID_AGGREGATOR:
1881                          {                          {
1882                                  struct ccs_aggregator *ptr =                                  struct ccs_aggregator *ptr =
1883                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1884                                  w[0] = CCS_KEYWORD_AGGREGATOR;                                  ccs_set_string(head, "aggregator ");
1885                                  w[1] = ptr->original_name->name;                                  ccs_set_string(head, ptr->original_name->name);
1886                                  w[2] = " ";                                  ccs_set_space(head);
1887                                  w[3] = ptr->aggregated_name->name;                                  ccs_set_string(head,
1888                          }                                                 ptr->aggregated_name->name);
                         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;  
1889                          }                          }
1890                          break;                          break;
1891                  case CCS_ID_RESERVEDPORT:                  case CCS_ID_RESERVEDPORT:
# Line 2007  static bool ccs_read_policy(struct ccs_i Line 1894  static bool ccs_read_policy(struct ccs_i
1894                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1895                                  const u16 min_port = ptr->min_port;                                  const u16 min_port = ptr->min_port;
1896                                  const u16 max_port = ptr->max_port;                                  const u16 max_port = ptr->max_port;
1897                                  w[0] = CCS_KEYWORD_DENY_AUTOBIND;                                  ccs_set_string(head, "deny_autobind ");
1898                                  snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",                                  ccs_io_printf(head, "%u", min_port);
1899                                           min_port, min_port != max_port ?                                  if (min_port != max_port)
1900                                           '-' : '\0', max_port);                                          ccs_io_printf(head, "-%u", max_port);
                                 buffer[sizeof(buffer) - 1] = '\0';  
                                 w[1] = buffer;  
1901                          }                          }
1902                          break;                          break;
1903                  default:                  default:
1904                          continue;                          continue;
1905                  }                  }
1906                  if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))                  ccs_set_lf(head);
                         return false;  
1907          }          }
1908            head->r.acl = NULL;
1909          return true;          return true;
1910  }  }
1911    
 static void ccs_read_global_domain(struct ccs_io_buffer *head)  
 {  
         if (!head->read_eof)  
                 head->read_eof = ccs_read_domain2(head, &ccs_global_domain);  
 }  
   
1912  /**  /**
1913   * ccs_read_exception - Read exception policy.   * ccs_read_exception - Read exception policy.
1914   *   *
# Line 2039  static void ccs_read_global_domain(struc Line 1918  static void ccs_read_global_domain(struc
1918   */   */
1919  static void ccs_read_exception(struct ccs_io_buffer *head)  static void ccs_read_exception(struct ccs_io_buffer *head)
1920  {  {
1921          if (head->read_eof)          if (head->r.eof)
1922                  return;                  return;
1923          while (head->read_step < CCS_MAX_POLICY &&          while (head->r.step < CCS_MAX_POLICY &&
1924                 ccs_read_policy(head, head->read_step))                 ccs_read_policy(head, head->r.step))
1925                  head->read_step++;                  head->r.step++;
1926          if (head->read_step < CCS_MAX_POLICY)          if (head->r.step < CCS_MAX_POLICY)
1927                  return;                  return;
1928          while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1929                 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1930                  head->read_step++;                  head->r.step++;
1931          if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1932                  return;                  return;
1933          head->read = ccs_read_global_domain;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1934  }                 + CCS_MAX_ACL_GROUPS * 2) {
1935                    head->r.group_index = (head->r.step - CCS_MAX_POLICY
1936  /**                                         - CCS_MAX_GROUP) / 2;
1937   * ccs_get_argv0 - Get argv[0].                  if (!ccs_read_domain2(head,
1938   *                                        &ccs_acl_group[head->r.group_index],
1939   * @ee: Pointer to "struct ccs_execve".                                        head->r.step & 1))
1940   *                          return;
1941   * Returns true on success, false otherwise.                  head->r.step++;
  */  
 static bool ccs_get_argv0(struct ccs_execve *ee)  
 {  
         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;  
1942          }          }
1943          return true;          head->r.eof = true;
  out:  
         return false;  
1944  }  }
1945    
1946  /* Wait queue for ccs_query_list. */  /* Wait queue for kernel -> userspace notification. */
1947  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);  static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
1948    /* Wait queue for userspace -> kernel notification. */
1949    static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
1950    
1951  /* Lock for manipulating ccs_query_list. */  /* Lock for manipulating ccs_query_list. */
1952  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1953    
1954  /* Structure for query. */  /* Structure for query. */
1955  struct ccs_query_entry {  struct ccs_query {
1956          struct list_head list;          struct list_head list;
1957          char *query;          char *query;
1958          int query_len;          int query_len;
1959          unsigned int serial;          unsigned int serial;
1960          int timer;          int timer;
1961          int answer;          int answer;
1962            u8 retry;
1963  };  };
1964    
1965  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1966  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1967    
1968  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1969  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1970    
1971  static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)  /**
1972     * ccs_truncate - Truncate a line.
1973     *
1974     * @str: String to truncate.
1975     *
1976     * Returns length of truncated @str.
1977     */
1978    static int ccs_truncate(char *str)
1979  {  {
1980          va_list args;          char *start = str;
1981          const int pos = strlen(buffer);          while (*(unsigned char *) str > (unsigned char) ' ')
1982          va_start(args, fmt);                  str++;
1983          vsnprintf(buffer + pos, len - pos - 1, fmt, args);          *str = '\0';
1984          va_end(args);          return strlen(start) + 1;
1985    }
1986    
1987    /**
1988     * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode.
1989     *
1990     * @header: Lines containing ACL.
1991     *
1992     * Returns nothing.
1993     */
1994    static void ccs_add_entry(char *header)
1995    {
1996            char *buffer;
1997            char *realpath = NULL;
1998            char *argv0 = NULL;
1999            char *symlink = NULL;
2000            char *handler;
2001            char *cp = strchr(header, '\n');
2002            int len;
2003            if (!cp)
2004                    return;
2005            cp = strchr(cp + 1, '\n');
2006            if (!cp)
2007                    return;
2008            *cp++ = '\0';
2009            len = strlen(cp) + 1;
2010            /* strstr() will return NULL if ordering is wrong. */
2011            if (*cp == 'f') {
2012                    argv0 = strstr(header, " argv[]={ \"");
2013                    if (argv0) {
2014                            argv0 += 10;
2015                            len += ccs_truncate(argv0) + 14;
2016                    }
2017                    realpath = strstr(header, " exec={ realpath=\"");
2018                    if (realpath) {
2019                            realpath += 8;
2020                            len += ccs_truncate(realpath) + 6;
2021                    }
2022                    symlink = strstr(header, " symlink.target=\"");
2023                    if (symlink)
2024                            len += ccs_truncate(symlink + 1) + 1;
2025            }
2026            handler = strstr(header, "type=execute_handler");
2027            if (handler)
2028                    len += ccs_truncate(handler) + 6;
2029            buffer = kmalloc(len, CCS_GFP_FLAGS);
2030            if (!buffer)
2031                    return;
2032            snprintf(buffer, len - 1, "%s", cp);
2033            if (handler)
2034                    ccs_addprintf(buffer, len, " task.%s", handler);
2035            if (realpath)
2036                    ccs_addprintf(buffer, len, " exec.%s", realpath);
2037            if (argv0)
2038                    ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
2039            if (symlink)
2040                    ccs_addprintf(buffer, len, "%s", symlink);
2041            ccs_normalize_line(buffer);
2042            if (!ccs_write_domain2(buffer, ccs_current_domain(), false))
2043                    ccs_update_stat(CCS_STAT_POLICY_UPDATES);
2044            kfree(buffer);
2045  }  }
2046                            
2047  /**  /**
2048   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2049   *   *
# Line 2152  static void ccs_addprintf(char *buffer, Line 2058  static void ccs_addprintf(char *buffer,
2058  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2059  {  {
2060          va_list args;          va_list args;
2061          int error = -EPERM;          int error;
         int pos;  
2062          int len;          int len;
2063          static unsigned int ccs_serial;          static unsigned int ccs_serial;
2064          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query entry = { };
2065          bool quota_exceeded = false;          bool quota_exceeded = false;
         char *header;  
         struct ccs_domain_info * const domain = ccs_current_domain();  
2066          va_start(args, fmt);          va_start(args, fmt);
2067          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;          len = vsnprintf((char *) &len, 1, fmt, args) + 1;
2068          va_end(args);          va_end(args);
2069          if (r->mode == CCS_CONFIG_LEARNING) {          /* Write /proc/ccs/grant_log or /proc/ccs/reject_log . */
2070                  char *buffer;          va_start(args, fmt);
2071                  char *realpath = NULL;          ccs_write_log2(r, len, fmt, args);
2072                  char *argv0 = NULL;          va_end(args);
2073                  const char *symlink = NULL;          /* Nothing more to do if granted. */
2074                  const struct ccs_preference *pref;          if (r->granted)
                 if (!ccs_domain_quota_ok(r))  
                         return 0;  
                 pref = ccs_profile(r->profile)->learning;  
                 if (r->param_type == CCS_TYPE_PATH_ACL &&  
                     r->param.path.operation == CCS_TYPE_EXECUTE) {  
                         if (pref->learning_exec_realpath) {  
                                 struct file *file = r->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);  
                         }  
                         if (pref->learning_exec_argv0) {  
                                 if (ccs_get_argv0(r->ee)) {  
                                         argv0 = r->ee->tmp;  
                                         len += strlen(argv0);  
                                 }  
                         }  
                 }  
                 if (r->param_type == CCS_TYPE_PATH_ACL &&  
                     r->param.path.operation == CCS_TYPE_SYMLINK &&  
                     pref->learning_symlink_target) {  
                         symlink = r->obj->symlink_target->name;  
                         len += strlen(symlink);  
                 }  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (buffer) {  
                         const bool handler = (current->ccs_flags &  
                                               CCS_TASK_IS_EXECUTE_HANDLER)  
                                 != 0;  
                         va_start(args, fmt);  
                         vsnprintf(buffer, len - 1, fmt, args);  
                         va_end(args);  
                         if (handler || realpath || argv0 || symlink) {  
                                 ccs_addprintf(buffer, len, " if");  
                                 if (handler)  
                                         ccs_addprintf(buffer, len, " task.type"  
                                                       "=execute_handler");  
                                 if (realpath)  
                                         ccs_addprintf(buffer, len,  
                                                       " exec.realpath=\"%s\"",  
                                                       realpath);  
                                 if (argv0)  
                                         ccs_addprintf(buffer, len,  
                                                       " exec.argv[0]=\"%s\"",  
                                                       argv0);  
                                 if (symlink)  
                                         ccs_addprintf(buffer, len,  
                                                       " symlink.target=\"%s\"",  
                                                       symlink);  
                         }  
                         ccs_normalize_line(buffer);  
                         ccs_write_domain2(buffer, domain, false);  
                         kfree(buffer);  
                 }  
                 kfree(realpath);  
                 return 0;  
         }  
         if (r->mode != CCS_CONFIG_ENFORCING)  
2075                  return 0;                  return 0;
2076          if (!atomic_read(&ccs_query_observers)) {          if (r->mode)
2077                    ccs_update_stat(r->mode);
2078            switch (r->mode) {
2079                  int i;                  int i;
2080                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  struct ccs_profile *p;
2081                          return -EPERM;          case CCS_CONFIG_ENFORCING:
2082                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->                  error = -EPERM;
2083                               enforcing_penalty; i++) {                  if (atomic_read(&ccs_query_observers))
2084                            break;
2085                    if (ccs_current_flags() & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2086                            goto out;
2087                    p = ccs_profile(r->profile);
2088                    /* Check enforcing_penalty parameter. */
2089                    for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) {
2090                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2091                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2092                  }                  }
                 return -EPERM;  
         }  
         header = ccs_init_log(&len, r);  
         if (!header)  
2093                  goto out;                  goto out;
2094          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);          case CCS_CONFIG_LEARNING:
2095          if (!ccs_query_entry)                  error = 0;
2096                    /* Check mac_learning_entry parameter. */
2097                    if (ccs_domain_quota_ok(r))
2098                            break;
2099                    /* fall through */
2100            default:
2101                    return 0;
2102            }
2103            /* Get message. */
2104            va_start(args, fmt);
2105            entry.query = ccs_init_log(r, len, fmt, args);
2106            va_end(args);
2107            if (!entry.query)
2108                  goto out;                  goto out;
2109          len = ccs_round2(len);          entry.query_len = strlen(entry.query) + 1;
2110          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);          if (!error) {
2111          if (!ccs_query_entry->query)                  ccs_add_entry(entry.query);
2112                  goto out;                  goto out;
2113          INIT_LIST_HEAD(&ccs_query_entry->list);          }
2114            len = ccs_round2(entry.query_len);
2115          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2116          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_memory_quota[CCS_MEMORY_QUERY] &&
2117              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              ccs_memory_used[CCS_MEMORY_QUERY] + len
2118                >= ccs_memory_quota[CCS_MEMORY_QUERY]) {
2119                  quota_exceeded = true;                  quota_exceeded = true;
2120          } else {          } else {
2121                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  entry.serial = ccs_serial++;
2122                  ccs_query_entry->serial = ccs_serial++;                  entry.retry = r->retry;
2123                    ccs_memory_used[CCS_MEMORY_QUERY] += len;
2124                    list_add_tail(&entry.list, &ccs_query_list);
2125          }          }
2126          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2127          if (quota_exceeded)          if (quota_exceeded)
2128                  goto out;                  goto out;
         snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",  
                  ccs_query_entry->serial, r->retry, header);  
         kfree(header);  
         header = NULL;  
         ccs_addprintf(ccs_query_entry->query, len, fmt, args);  
         ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;  
         spin_lock(&ccs_query_list_lock);  
         list_add_tail(&ccs_query_entry->list, &ccs_query_list);  
         spin_unlock(&ccs_query_list_lock);  
2129          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2130          for (ccs_query_entry->timer = 0;          while (entry.timer < 10) {
2131               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;                  wake_up_all(&ccs_query_wait);
2132               ccs_query_entry->timer++) {                  if (wait_event_interruptible_timeout
2133                  wake_up(&ccs_query_wait);                      (ccs_answer_wait, entry.answer ||
2134                  set_current_state(TASK_INTERRUPTIBLE);                       !atomic_read(&ccs_query_observers), HZ))
                 schedule_timeout(HZ / 10);  
                 if (ccs_query_entry->answer)  
2135                          break;                          break;
2136                    else
2137                            entry.timer++;
2138          }          }
2139          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2140          list_del(&ccs_query_entry->list);          list_del(&entry.list);
2141          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_memory_used[CCS_MEMORY_QUERY] -= len;
2142          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2143          switch (ccs_query_entry->answer) {          switch (entry.answer) {
2144          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2145                  error = CCS_RETRY_REQUEST;                  error = CCS_RETRY_REQUEST;
2146                  r->retry++;                  r->retry++;
# Line 2300  int ccs_supervisor(struct ccs_request_in Line 2149  int ccs_supervisor(struct ccs_request_in
2149                  /* Granted by administrator. */                  /* Granted by administrator. */
2150                  error = 0;                  error = 0;
2151                  break;                  break;
         case 0:  
                 /* Timed out. */  
                 break;  
2152          default:          default:
2153                  /* Rejected by administrator. */                  /* Timed out or rejected by administrator. */
2154                  break;                  break;
2155          }          }
2156   out:  out:
2157          if (ccs_query_entry)          kfree(entry.query);
                 kfree(ccs_query_entry->query);  
         kfree(ccs_query_entry);  
         kfree(header);  
2158          return error;          return error;
2159  }  }
2160    
# Line 2333  static int ccs_poll_query(struct file *f Line 2176  static int ccs_poll_query(struct file *f
2176          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2177                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2178                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2179                          struct ccs_query_entry *ptr =                          struct ccs_query *ptr =
2180                                  list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2181                          if (ptr->answer)                          if (ptr->answer)
2182                                  continue;                                  continue;
2183                          found = true;                          found = true;
# Line 2354  static int ccs_poll_query(struct file *f Line 2197  static int ccs_poll_query(struct file *f
2197   * ccs_read_query - Read access requests which violated policy in enforcing mode.   * ccs_read_query - Read access requests which violated policy in enforcing mode.
2198   *   *
2199   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2200     *
2201     * Returns nothing.
2202   */   */
2203  static void ccs_read_query(struct ccs_io_buffer *head)  static void ccs_read_query(struct ccs_io_buffer *head)
2204  {  {
# Line 2361  static void ccs_read_query(struct ccs_io Line 2206  static void ccs_read_query(struct ccs_io
2206          int pos = 0;          int pos = 0;
2207          int len = 0;          int len = 0;
2208          char *buf;          char *buf;
2209          if (head->read_avail)          if (head->r.w_pos)
2210                  return;                  return;
2211          if (head->read_buf) {          if (head->read_buf) {
2212                  kfree(head->read_buf);                  kfree(head->read_buf);
2213                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2214          }          }
2215          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2216          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2217                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2218                  if (ptr->answer)                  if (ptr->answer)
2219                          continue;                          continue;
2220                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2221                          continue;                          continue;
2222                  len = ptr->query_len;                  len = ptr->query_len;
2223                  break;                  break;
2224          }          }
2225          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2226          if (!len) {          if (!len) {
2227                  head->read_step = 0;                  head->r.query_index = 0;
2228                  return;                  return;
2229          }          }
2230          buf = kzalloc(len, CCS_GFP_FLAGS);          buf = kzalloc(len + 32, CCS_GFP_FLAGS);
2231          if (!buf)          if (!buf)
2232                  return;                  return;
2233          pos = 0;          pos = 0;
2234          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2235          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2236                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2237                  if (ptr->answer)                  if (ptr->answer)
2238                          continue;                          continue;
2239                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2240                          continue;                          continue;
2241                  /*                  /*
2242                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
2243                   * can change, but I don't care.                   * can change, but I don't care.
2244                   */                   */
2245                  if (len == ptr->query_len)                  if (len == ptr->query_len)
2246                          memmove(buf, ptr->query, len);                          snprintf(buf, len + 32, "Q%u-%hu\n%s", ptr->serial,
2247                                     ptr->retry, ptr->query);
2248                  break;                  break;
2249          }          }
2250          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2251          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2252                  head->read_buf = buf;                  head->read_buf = buf;
2253                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2254                    head->r.query_index++;
2255          } else {          } else {
2256                  kfree(buf);                  kfree(buf);
2257          }          }
# Line 2430  static int ccs_write_answer(struct ccs_i Line 2272  static int ccs_write_answer(struct ccs_i
2272          unsigned int answer;          unsigned int answer;
2273          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2274          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2275                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2276                  ptr->timer = 0;                  ptr->timer = 0;
2277          }          }
2278          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2439  static int ccs_write_answer(struct ccs_i Line 2280  static int ccs_write_answer(struct ccs_i
2280                  return -EINVAL;                  return -EINVAL;
2281          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2282          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2283                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2284                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2285                          continue;                          continue;
2286                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2448  static int ccs_write_answer(struct ccs_i Line 2288  static int ccs_write_answer(struct ccs_i
2288                  break;                  break;
2289          }          }
2290          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2291            wake_up_all(&ccs_answer_wait);
2292          return 0;          return 0;
2293  }  }
2294    
# Line 2455  static int ccs_write_answer(struct ccs_i Line 2296  static int ccs_write_answer(struct ccs_i
2296   * ccs_read_version: Get version.   * ccs_read_version: Get version.
2297   *   *
2298   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
2299     *
2300     * Returns nothing.
2301   */   */
2302  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2303  {  {
2304          if (head->read_eof)          if (head->r.eof)
2305                    return;
2306            ccs_set_string(head, "1.8.0-pre");
2307            head->r.eof = true;
2308    }
2309    
2310    /* String table for /proc/ccs/meminfo interface. */
2311    static const char * const ccs_policy_headers[CCS_MAX_POLICY_STAT] = {
2312            [CCS_STAT_POLICY_UPDATES]    = "update:",
2313            [CCS_STAT_POLICY_LEARNING]   = "violation in learning mode:",
2314            [CCS_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
2315            [CCS_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
2316    };
2317    
2318    /* String table for /proc/ccs/meminfo interface. */
2319    static const char * const ccs_memory_headers[CCS_MAX_MEMORY_STAT] = {
2320            [CCS_MEMORY_POLICY] = "policy:",
2321            [CCS_MEMORY_AUDIT]  = "audit log:",
2322            [CCS_MEMORY_QUERY]  = "query message:",
2323    };
2324    
2325    /* Timestamp counter for last updated. */
2326    static unsigned int ccs_stat_updated[CCS_MAX_POLICY_STAT];
2327    /* Counter for number of updates. */
2328    static unsigned int ccs_stat_modified[CCS_MAX_POLICY_STAT];
2329    
2330    /**
2331     * ccs_update_stat - Update statistic counters.
2332     *
2333     * @index: Index for policy type.
2334     *
2335     * Returns nothing.
2336     */
2337    void ccs_update_stat(const u8 index)
2338    {
2339            struct timeval tv;
2340            do_gettimeofday(&tv);
2341            /*
2342             * I don't use atomic operations because race condition is not fatal.
2343             */
2344            ccs_stat_updated[index]++;
2345            ccs_stat_modified[index] = tv.tv_sec;
2346    }
2347    
2348    /**
2349     * ccs_read_stat - Read statistic data.
2350     *
2351     * @head: Pointer to "struct ccs_io_buffer".
2352     *
2353     * Returns nothing.
2354     */
2355    static void ccs_read_stat(struct ccs_io_buffer *head)
2356    {
2357            u8 i;
2358            unsigned int total = 0;
2359            if (head->r.eof)
2360                  return;                  return;
2361          ccs_io_printf(head, "1.7.2");          for (i = 0; i < CCS_MAX_POLICY_STAT; i++) {
2362          head->read_eof = true;                  ccs_io_printf(head, "Policy %-30s %10u", ccs_policy_headers[i],
2363                                  ccs_stat_updated[i]);
2364                    if (ccs_stat_modified[i]) {