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

Subversion リポジトリの参照

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

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

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