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

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 3949 by kumaneko, Sun Sep 5 11:50:03 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/09/01
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 13  Line 13 
13  #include "internal.h"  #include "internal.h"
14    
15  static struct ccs_profile ccs_default_profile = {  static struct ccs_profile ccs_default_profile = {
         .learning = &ccs_default_profile.preference,  
         .permissive = &ccs_default_profile.preference,  
         .enforcing = &ccs_default_profile.preference,  
         .audit = &ccs_default_profile.preference,  
16  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
17          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,          .preference.audit_max_grant_log = CONFIG_CCSECURITY_MAX_GRANT_LOG,
18          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,          .preference.audit_max_reject_log = CONFIG_CCSECURITY_MAX_REJECT_LOG,
# Line 24  static struct ccs_profile ccs_default_pr Line 20  static struct ccs_profile ccs_default_pr
20          .preference.audit_task_info = true,          .preference.audit_task_info = true,
21          .preference.audit_path_info = true,          .preference.audit_path_info = true,
22          .preference.enforcing_penalty = 0,          .preference.enforcing_penalty = 0,
         .preference.enforcing_verbose = true,  
23          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,          .preference.learning_max_entry = CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY,
         .preference.learning_verbose = false,  
24          .preference.learning_exec_realpath = true,          .preference.learning_exec_realpath = true,
25          .preference.learning_exec_argv0 = true,          .preference.learning_exec_argv0 = true,
26          .preference.learning_symlink_target = true,          .preference.learning_symlink_target = true,
         .preference.permissive_verbose = true  
27  };  };
28    
29  /* Profile version. Currently only 20090903 is defined. */  /* Profile version. Currently only 20100903 is defined. */
30  static unsigned int ccs_profile_version;  static unsigned int ccs_profile_version;
31    
32  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
33  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
34    
35  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
36  static const char *ccs_mode_4[4] = {  const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
37          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED] = "disabled",
38            [CCS_CONFIG_LEARNING] = "learning",
39            [CCS_CONFIG_PERMISSIVE] = "permissive",
40            [CCS_CONFIG_ENFORCING] = "enforcing"
41  };  };
42    
43  /* String table for /proc/ccs/profile */  /* String table for /proc/ccs/profile */
# Line 68  static const char *ccs_mac_keywords[CCS_ Line 64  static const char *ccs_mac_keywords[CCS_
64          = "file::truncate",          = "file::truncate",
65          [CCS_MAC_FILE_SYMLINK]          [CCS_MAC_FILE_SYMLINK]
66          = "file::symlink",          = "file::symlink",
         [CCS_MAC_FILE_REWRITE]  
         = "file::rewrite",  
67          [CCS_MAC_FILE_MKBLOCK]          [CCS_MAC_FILE_MKBLOCK]
68          = "file::mkblock",          = "file::mkblock",
69          [CCS_MAC_FILE_MKCHAR]          [CCS_MAC_FILE_MKCHAR]
# Line 94  static const char *ccs_mac_keywords[CCS_ Line 88  static const char *ccs_mac_keywords[CCS_
88          = "file::umount",          = "file::umount",
89          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
90          = "file::pivot_root",          = "file::pivot_root",
         [CCS_MAC_FILE_TRANSIT]  
         = "file::transit",  
91          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
92          = "misc::env",          = "misc::env",
93          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MAC_NETWORK_INET_STREAM_BIND]
94          = "network::inet_udp_bind",          = "network::inet_stream_bind",
95          [CCS_MAC_NETWORK_UDP_CONNECT]          [CCS_MAC_NETWORK_INET_STREAM_LISTEN]
96          = "network::inet_udp_connect",          = "network::inet_stream_listen",
97          [CCS_MAC_NETWORK_TCP_BIND]          [CCS_MAC_NETWORK_INET_STREAM_CONNECT]
98          = "network::inet_tcp_bind",          = "network::inet_stream_connect",
99          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_NETWORK_INET_STREAM_ACCEPT]
100          = "network::inet_tcp_listen",          = "network::inet_stream_accept",
101          [CCS_MAC_NETWORK_TCP_CONNECT]          [CCS_MAC_NETWORK_INET_DGRAM_BIND]
102          = "network::inet_tcp_connect",          = "network::inet_dgram_bind",
103          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_MAC_NETWORK_INET_DGRAM_SEND]
104          = "network::inet_tcp_accept",          = "network::inet_dgram_send",
105          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAC_NETWORK_INET_DGRAM_RECV]
106            = "network::inet_dgram_recv",
107            [CCS_MAC_NETWORK_INET_RAW_BIND]
108          = "network::inet_raw_bind",          = "network::inet_raw_bind",
109          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_MAC_NETWORK_INET_RAW_SEND]
110          = "network::inet_raw_connect",          = "network::inet_raw_send",
111            [CCS_MAC_NETWORK_INET_RAW_RECV]
112            = "network::inet_raw_recv",
113            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]
114            = "network::unix_stream_bind",
115            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]
116            = "network::unix_stream_listen",
117            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]
118            = "network::unix_stream_connect",
119            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]
120            = "network::unix_stream_accept",
121            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]
122            = "network::unix_dgram_bind",
123            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]
124            = "network::unix_dgram_send",
125            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]
126            = "network::unix_dgram_recv",
127            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]
128            = "network::unix_seqpacket_bind",
129            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]
130            = "network::unix_seqpacket_listen",
131            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT]
132            = "network::unix_seqpacket_connect",
133            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]
134            = "network::unix_seqpacket_accept",
135          [CCS_MAC_SIGNAL]          [CCS_MAC_SIGNAL]
136          = "ipc::signal",          = "ipc::signal",
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]  
         = "capability::inet_tcp_create",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_LISTEN]  
         = "capability::inet_tcp_listen",  
         [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CONNECT]  
         = "capability::inet_tcp_connect",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_DGRAM_SOCKET]  
         = "capability::use_inet_udp",  
         [CCS_MAX_MAC_INDEX + CCS_USE_INET_RAW_SOCKET]  
         = "capability::use_inet_ip",  
137          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
138          = "capability::use_route",          = "capability::use_route",
139          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
140          = "capability::use_packet",          = "capability::use_packet",
         [CCS_MAX_MAC_INDEX + CCS_SYS_MOUNT]  
         = "capability::SYS_MOUNT",  
         [CCS_MAX_MAC_INDEX + CCS_SYS_UMOUNT]  
         = "capability::SYS_UMOUNT",  
141          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
142          = "capability::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",  
143          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
144          = "capability::SYS_VHANGUP",          = "capability::SYS_VHANGUP",
145          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
# Line 150  static const char *ccs_mac_keywords[CCS_ Line 150  static const char *ccs_mac_keywords[CCS_
150          = "capability::SYS_SETHOSTNAME",          = "capability::SYS_SETHOSTNAME",
151          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
152          = "capability::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",  
153          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
154          = "capability::SYS_KEXEC_LOAD",          = "capability::SYS_KEXEC_LOAD",
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
155          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
156          = "capability::SYS_PTRACE",          = "capability::SYS_PTRACE",
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
157          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
158           + CCS_MAC_CATEGORY_FILE] = "file",           + CCS_MAC_CATEGORY_FILE] = "file",
159          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
# Line 218  static const char *ccs_yesno(const unsig Line 192  static const char *ccs_yesno(const unsig
192          return value ? "yes" : "no";          return value ? "yes" : "no";
193  }  }
194    
195    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
196    {
197            va_list args;
198            const int pos = strlen(buffer);
199            va_start(args, fmt);
200            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
201            va_end(args);
202    }
203    
204  /**  /**
205   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
206   *   *
207   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
208   *   *
209   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
210     */
211    static bool ccs_flush(struct ccs_io_buffer *head)
212    {
213            while (head->r.w_pos) {
214                    const char *w = head->r.w[0];
215                    int len = strlen(w);
216                    if (len) {
217                            if (len > head->read_user_buf_avail)
218                                    len = head->read_user_buf_avail;
219                            if (!len)
220                                    return false;
221                            if (copy_to_user(head->read_user_buf, w, len))
222                                    return false;
223                            head->read_user_buf_avail -= len;
224                            head->read_user_buf += len;
225                            w += len;
226                    }
227                    if (*w) {
228                            head->r.w[0] = w;
229                            return false;
230                    }
231                    /* Add '\0' for audit logs and query. */
232                    if (head->poll) {
233                            if (!head->read_user_buf_avail ||
234                                copy_to_user(head->read_user_buf, "", 1))
235                                    return false;
236                            head->read_user_buf_avail--;
237                            head->read_user_buf++;
238                    }
239                    head->r.w_pos--;
240                    for (len = 0; len < head->r.w_pos; len++)
241                            head->r.w[len] = head->r.w[len + 1];
242            }
243            head->r.avail = 0;
244            return true;
245    }
246    
247    /**
248     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
249   *   *
250   * The snprintf() will truncate, but ccs_io_printf() won't.   * @head:   Pointer to "struct ccs_io_buffer".
251     * @string: String to print.
252     *
253     * Note that @string has to be kept valid until @head is kfree()d.
254     * This means that char[] allocated on stack memory cannot be passed to
255     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
256   */   */
257  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
258    {
259            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
260                    head->r.w[head->r.w_pos++] = string;
261                    ccs_flush(head);
262            } else
263                    printk(KERN_WARNING "Too many words in a line.\n");
264    }
265    
266    /**
267     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
268     *
269     * @head: Pointer to "struct ccs_io_buffer".
270     * @fmt:  The printf()'s format string, followed by parameters.
271     */
272    void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
273  {  {
274          va_list args;          va_list args;
275          int len;          int len;
276          int pos = head->read_avail;          int pos = head->r.avail;
277          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
278          if (size <= 0)          if (size <= 0)
279                  return false;                  return;
280          va_start(args, fmt);          va_start(args, fmt);
281          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
282          va_end(args);          va_end(args);
283          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
284                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
285          head->read_avail += len;                  return;
286          return true;          }
287            head->r.avail += len;
288            ccs_set_string(head, head->read_buf + pos);
289    }
290    
291    static void ccs_set_space(struct ccs_io_buffer *head)
292    {
293            ccs_set_string(head, " ");
294    }
295    
296    static bool ccs_set_lf(struct ccs_io_buffer *head)
297    {
298            ccs_set_string(head, "\n");
299            return !head->r.w_pos;
300  }  }
301    
302  /**  /**
303   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
304   *   *
305   * @profile: Profile number to create.   * @profile: Profile number to create.
306   *   *
307   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
308   */   */
309  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)  
310  {  {
311          struct ccs_profile *ptr;          struct ccs_profile *ptr;
312          struct ccs_profile *entry;          struct ccs_profile *entry;
# Line 263  static struct ccs_profile *ccs_find_or_a Line 316  static struct ccs_profile *ccs_find_or_a
316          if (ptr)          if (ptr)
317                  return ptr;                  return ptr;
318          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
319          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
320                    goto out;
321          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
322          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
323                  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;  
324                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
325                            CCS_CONFIG_VERBOSE |
326                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
327                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
328                         sizeof(ptr->config));                         sizeof(ptr->config));
# Line 280  static struct ccs_profile *ccs_find_or_a Line 331  static struct ccs_profile *ccs_find_or_a
331                  entry = NULL;                  entry = NULL;
332          }          }
333          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
334     out:
335          kfree(entry);          kfree(entry);
336          return ptr;          return ptr;
337  }  }
# Line 290  static struct ccs_profile *ccs_find_or_a Line 342  static struct ccs_profile *ccs_find_or_a
342  static void ccs_check_profile(void)  static void ccs_check_profile(void)
343  {  {
344          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
345            const int idx = ccs_read_lock();
346          ccs_policy_loaded = true;          ccs_policy_loaded = true;
347          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
348                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
# Line 298  static void ccs_check_profile(void) Line 351  static void ccs_check_profile(void)
351                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
352                        profile, domain->domainname->name);                        profile, domain->domainname->name);
353          }          }
354          if (ccs_profile_version != 20090903)          ccs_read_unlock(idx);
355            if (ccs_profile_version != 20100903)
356                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
357                        ccs_profile_version);                        ccs_profile_version);
358          printk(KERN_INFO "CCSecurity: 1.7.2-pre   2010/03/21\n");          printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/09/01\n");
359          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
360  }  }
361    
# Line 321  struct ccs_profile *ccs_profile(const u8 Line 375  struct ccs_profile *ccs_profile(const u8
375          return ptr;          return ptr;
376  }  }
377    
378  /**  static s8 ccs_find_yesno(const char *string, const char *find)
  * ccs_write_profile - Write profile table.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_write_profile(struct ccs_io_buffer *head)  
379  {  {
380          char *data = head->write_buf;          const char *cp = strstr(string, find);
381          unsigned int i;          if (cp) {
382          int value;                  cp += strlen(find);
383          int mode;                  if (!strncmp(cp, "=yes", 4))
384          u8 config;                          return 1;
385          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;  
386                          return 0;                          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;  
387          }          }
388          if (!strcmp(data, "PREFERENCE::enforcing")) {          return -1;
389                  char *cp2;  }
390                  if (use_default) {  
391                          profile->enforcing = &ccs_default_profile.preference;  static void ccs_set_bool(bool *b, const char *string, const char *find)
392                          return 0;  {
393                  }          switch (ccs_find_yesno(string, find)) {
394                  profile->enforcing = &profile->preference;          case 1:
395                  if (value >= 0)                  *b = true;
396                          profile->preference.enforcing_verbose = value;                  break;
397                  cp2 = strstr(cp, "penalty=");          case 0:
398                  if (cp2)                  *b = false;
399                          sscanf(cp2 + 8, "%u",                  break;
                                &profile->preference.enforcing_penalty);  
                 return 0;  
400          }          }
401          if (!strcmp(data, "PREFERENCE::permissive")) {  }
402                  if (use_default) {  
403                          profile->permissive = &ccs_default_profile.preference;  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
404                          return 0;  {
405                  }          const char *cp = strstr(string, find);
406                  profile->permissive = &profile->preference;          if (cp)
407                  if (value >= 0)                  sscanf(cp + strlen(find), "=%u", i);
408                          profile->preference.permissive_verbose = value;  }
409                  return 0;  
410    static void ccs_set_pref(const char *name, const char *value,
411                             struct ccs_profile *profile)
412    {
413            if (!strcmp(name, "audit")) {
414    #ifdef CONFIG_CCSECURITY_AUDIT
415                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
416                                 "max_grant_log");
417                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
418                                 "max_reject_log");
419    #endif
420                    ccs_set_bool(&profile->preference.audit_task_info, value,
421                                 "task_info");
422                    ccs_set_bool(&profile->preference.audit_path_info, value,
423                                 "path_info");
424                    return;
425          }          }
426          if (!strcmp(data, "PREFERENCE::learning")) {          if (!strcmp(name, "enforcing")) {
427                  char *cp2;                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
428                  if (use_default) {                               "penalty");
429                          profile->learning = &ccs_default_profile.preference;                  return;
                         return 0;  
                 }  
                 profile->learning = &profile->preference;  
                 if (value >= 0)  
                         profile->preference.learning_verbose = value;  
                 cp2 = strstr(cp, "max_entry=");  
                 if (cp2)  
                         sscanf(cp2 + 10, "%u",  
                                &profile->preference.learning_max_entry);  
                 if (strstr(cp, "exec.realpath=yes"))  
                         profile->preference.learning_exec_realpath = true;  
                 else if (strstr(cp, "exec.realpath=no"))  
                         profile->preference.learning_exec_realpath = false;  
                 if (strstr(cp, "exec.argv0=yes"))  
                         profile->preference.learning_exec_argv0 = true;  
                 else if (strstr(cp, "exec.argv0=no"))  
                         profile->preference.learning_exec_argv0 = false;  
                 if (strstr(cp, "symlink.target=yes"))  
                         profile->preference.learning_symlink_target = true;  
                 else if (strstr(cp, "symlink.target=no"))  
                         profile->preference.learning_symlink_target = false;  
                 return 0;  
430          }          }
431          if (profile == &ccs_default_profile)          if (!strcmp(name, "learning")) {
432                  return -EINVAL;                  ccs_set_uint(&profile->preference.learning_max_entry, value,
433          if (!strcmp(data, "COMMENT")) {                               "max_entry");
434                  const struct ccs_path_info *old_comment = profile->comment;                  ccs_set_bool(&profile->preference.learning_exec_realpath,
435                  profile->comment = ccs_get_name(cp);                               value, "exec.realpath");
436                  ccs_put_name(old_comment);                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
437                  return 0;                               "exec.argv0");
438                    ccs_set_bool(&profile->preference.learning_symlink_target,
439                                 value, "symlink.target");
440                    return;
441          }          }
442          if (!strcmp(data, "CONFIG")) {  }
443    
444    static int ccs_set_mode(char *name, const char *value,
445                            struct ccs_profile *profile)
446    {
447            u8 i;
448            u8 config;
449            if (!strcmp(name, "CONFIG")) {
450                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
451                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
452                  config = profile->default_config;                  config = profile->default_config;
453          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
454                  config = 0;                  config = 0;
455                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
456                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
457                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
458                                  continue;                                  continue;
459                          config = profile->config[i];                          config = profile->config[i];
460                          break;                          break;
# Line 471  static int ccs_write_profile(struct ccs_ Line 465  static int ccs_write_profile(struct ccs_
465          } else {          } else {
466                  return -EINVAL;                  return -EINVAL;
467          }          }
468          if (use_default) {          if (strstr(value, "use_default")) {
469                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
470          } else {          } else {
471                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
472                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
473                            if (strstr(value, ccs_mode[mode]))
474                                  /*                                  /*
475                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
476                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
477                                   */                                   */
478                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
479                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
480                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "verbose")) {
481                            case 1:
482                                    config |= CCS_CONFIG_VERBOSE;
483                                    break;
484                            case 0:
485                                    config &= ~CCS_CONFIG_VERBOSE;
486                                    break;
487                            }
488    #ifdef CONFIG_CCSECURITY_AUDIT
489                            switch (ccs_find_yesno(value, "grant_log")) {
490                            case 1:
491                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
492                          else if (strstr(cp, "grant_log=no"))                                  break;
493                            case 0:
494                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
495                          if (strstr(cp, "reject_log=yes"))                                  break;
496                            }
497                            switch (ccs_find_yesno(value, "reject_log")) {
498                            case 1:
499                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
500                          else if (strstr(cp, "reject_log=no"))                                  break;
501                            case 0:
502                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
503                  }                                  break;
504                            }
505  #endif  #endif
506                    }
507          }          }
508          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
509              + CCS_MAX_MAC_CATEGORY_INDEX)              + CCS_MAX_MAC_CATEGORY_INDEX)
# Line 503  static int ccs_write_profile(struct ccs_ Line 514  static int ccs_write_profile(struct ccs_
514  }  }
515    
516  /**  /**
517   * ccs_read_profile - Read profile table.   * ccs_write_profile - Write profile table.
518   *   *
519   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
520     *
521     * Returns 0 on success, negative value otherwise.
522   */   */
523  static void ccs_read_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
524  {  {
525          int index;          char *data = head->write_buf;
526          if (head->read_eof)          unsigned int i;
527                  return;          char *cp;
528          if (head->read_bit)          struct ccs_profile *profile;
529                  goto body;          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
530          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");                  return 0;
531          ccs_io_printf(head, "PREFERENCE::audit={ "          i = simple_strtoul(data, &cp, 10);
532            if (*cp != '-')
533                    return -EINVAL;
534            data = cp + 1;
535            profile = ccs_assign_profile(i);
536            if (!profile)
537                    return -EINVAL;
538            cp = strchr(data, '=');
539            if (!cp)
540                    return -EINVAL;
541            *cp++ = '\0';
542            if (ccs_str_starts(&data, "PREFERENCE::")) {
543                    ccs_set_pref(data, cp, profile);
544                    return 0;
545            }
546            if (!strcmp(data, "COMMENT")) {
547                    const struct ccs_path_info *old_comment = profile->comment;
548                    profile->comment = ccs_get_name(cp);
549                    ccs_put_name(old_comment);
550                    return 0;
551            }
552            return ccs_set_mode(data, cp, profile);
553    }
554    
555    static void ccs_print_preference(struct ccs_io_buffer *head, const int index)
556    {
557            struct ccs_profile *profile = ccs_profile_ptr[index];
558            struct ccs_preference *pref = &profile->preference;
559            ccs_io_printf(head, "%u-PREFERENCE::%s={ "
560  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
561                        "max_grant_log=%u max_reject_log=%u "                        "max_grant_log=%u max_reject_log=%u "
562  #endif  #endif
563                        "task_info=%s path_info=%s }\n",                        "task_info=%s path_info=%s }\n", index,
564                          "audit",
565  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
566                        ccs_default_profile.preference.audit_max_grant_log,                        pref->audit_max_grant_log,
567                        ccs_default_profile.preference.audit_max_reject_log,                        pref->audit_max_reject_log,
568  #endif  #endif
569                        ccs_yesno(ccs_default_profile.preference.                        ccs_yesno(pref->audit_task_info),
570                                  audit_task_info),                        ccs_yesno(pref->audit_path_info));
571                        ccs_yesno(ccs_default_profile.preference.          ccs_io_printf(head, "%u-PREFERENCE::%s={ "
572                                  audit_path_info));                        "max_entry=%u exec.realpath=%s "
573          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "                        "exec.argv0=%s symlink.target=%s }\n",
574                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",                        index, "learning",
575                        ccs_yesno(ccs_default_profile.preference.                        pref->learning_max_entry,
576                                  learning_verbose),                        ccs_yesno(pref->learning_exec_realpath),
577                        ccs_default_profile.preference.learning_max_entry,                        ccs_yesno(pref->learning_exec_argv0),
578                        ccs_yesno(ccs_default_profile.preference.                        ccs_yesno(pref->learning_symlink_target));
579                                  learning_exec_realpath),          ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,
580                        ccs_yesno(ccs_default_profile.preference.                        "enforcing", pref->enforcing_penalty);
581                                  learning_exec_argv0),  }
582                        ccs_yesno(ccs_default_profile.preference.  
583                                  learning_symlink_target));  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
584          ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",  {
585                        ccs_yesno(ccs_default_profile.preference.          ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],
586                                  permissive_verbose));                        ccs_yesno(config & CCS_CONFIG_VERBOSE));
         ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "  
                       "}\n",  
                       ccs_yesno(ccs_default_profile.preference.  
                                 enforcing_verbose),  
                       ccs_default_profile.preference.enforcing_penalty);  
         head->read_bit = 1;  
  body:  
         for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {  
                 bool done;  
                 u8 config;  
                 int i;  
                 int pos;  
                 const struct ccs_profile *profile = ccs_profile_ptr[index];  
                 const struct ccs_path_info *comment;  
                 head->read_step = index;  
                 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++) {  
587  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
588                          const char *g;          ccs_io_printf(head, " grant_log=%s reject_log=%s",
589                          const char *r;                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
590                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
591  #endif  #endif
592                          config = profile->config[i];          ccs_set_string(head, " }\n");
593    }
594    
595    /**
596     * ccs_read_profile - Read profile table.
597     *
598     * @head: Pointer to "struct ccs_io_buffer".
599     */
600    static void ccs_read_profile(struct ccs_io_buffer *head)
601    {
602            u8 index;
603            const struct ccs_profile *profile;
604     next:
605            index = head->r.index;
606            profile = ccs_profile_ptr[index];
607            switch (head->r.step) {
608            case 0:
609                    ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20100903");
610                    head->r.step++;
611                    break;
612            case 1:
613                    for ( ; head->r.index < CCS_MAX_PROFILES;
614                          head->r.index++)
615                            if (ccs_profile_ptr[head->r.index])
616                                    break;
617                    if (head->r.index == CCS_MAX_PROFILES)
618                            return;
619                    head->r.step++;
620                    break;
621            case 2:
622                    {
623                            const struct ccs_path_info *comment = profile->comment;
624                            ccs_io_printf(head, "%u-COMMENT=", index);
625                            ccs_set_string(head, comment ? comment->name : "");
626                            ccs_set_lf(head);
627                            head->r.step++;
628                    }
629                    break;
630            case 3:
631                    {
632                            ccs_io_printf(head, "%u-%s", index, "CONFIG");
633                            ccs_print_config(head, profile->default_config);
634                            head->r.bit = 0;
635                            head->r.step++;
636                    }
637                    break;
638            case 4:
639                    for ( ; head->r.bit < CCS_MAX_MAC_INDEX
640                                  + CCS_MAX_CAPABILITY_INDEX
641                                  + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
642                            const u8 i = head->r.bit;
643                            const u8 config = profile->config[i];
644                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
645                                  continue;                                  continue;
646  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
647                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                        ccs_mac_keywords[i]);
648                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          ccs_print_config(head, config);
649                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                          head->r.bit++;
650                                             "grant_log=%s reject_log=%s }\n",                          break;
651                                             index, ccs_mac_keywords[i],                  }
652                                             ccs_mode_4[config & 3], g, r))                  if (head->r.bit == CCS_MAX_MAC_INDEX
653                                  goto out;                      + CCS_MAX_CAPABILITY_INDEX
654  #else                      + CCS_MAX_MAC_CATEGORY_INDEX) {
655                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          ccs_print_preference(head, index);
656                                             index, ccs_mac_keywords[i],                          head->r.index++;
657                                             ccs_mode_4[config & 3]))                          head->r.step = 1;
                                 goto out;  
 #endif  
658                  }                  }
                 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;  
659                  break;                  break;
660          }          }
661          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
662                  head->read_eof = true;                  goto next;
663  }  }
664    
665  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager(const struct ccs_acl_head *a,
666  LIST_HEAD(ccs_policy_manager_list);                               const struct ccs_acl_head *b)
667    {
668            return container_of(a, struct ccs_manager, head)->manager
669                    == container_of(b, struct ccs_manager, head)->manager;
670    }
671    
672  /**  /**
673   * 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 679  LIST_HEAD(ccs_policy_manager_list);
679   */   */
680  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)
681  {  {
682          struct ccs_policy_manager_entry *ptr;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry e = { };  
683          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
684          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
685                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
686                          return -EINVAL;                          return -EINVAL;
687                  e.is_domain = true;                  e.is_domain = true;
688          } else {          } else {
689                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
690                          return -EINVAL;                          return -EINVAL;
691          }          }
692          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
693          if (!e.manager)          if (!e.manager)
694                  return -ENOMEM;                  return error;
695          mutex_lock(&ccs_policy_lock);          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
696          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                                    &ccs_policy_list[CCS_ID_MANAGER],
697                  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);  
698          ccs_put_name(e.manager);          ccs_put_name(e.manager);
699          return error;          return error;
700  }  }
701    
702  /**  /**
703   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
704   *   *
705   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
706   *   *
707   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
708   */   */
709  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
710  {  {
711          char *data = head->write_buf;          char *data = head->write_buf;
712          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
# Line 724  static int ccs_write_manager_policy(stru Line 718  static int ccs_write_manager_policy(stru
718  }  }
719    
720  /**  /**
721   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
722   *   *
723   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
724   *   *
725   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
726   */   */
727  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
728  {  {
729          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
730                  return;                  return;
731          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]) {
732                  struct ccs_policy_manager_entry *ptr;                  struct ccs_manager *ptr =
733                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                          list_entry(head->r.acl, typeof(*ptr), head.list);
734                  if (ptr->is_deleted)                  if (ptr->head.is_deleted)
735                          continue;                          continue;
736                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
737                          return;                          return;
738                    ccs_set_string(head, ptr->manager->name);
739                    ccs_set_lf(head);
740          }          }
741          head->read_eof = true;          head->r.eof = true;
742  }  }
743    
744  /**  /**
745   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
746   *   *
747   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
748   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
749   *   *
750   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
751   */   */
752  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
753  {  {
754          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
755          const char *exe;          const char *exe;
756          struct task_struct *task = current;          struct task_struct *task = current;
757          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
# Line 764  static bool ccs_is_policy_manager(void) Line 759  static bool ccs_is_policy_manager(void)
759          bool found = false;          bool found = false;
760          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
761                  return true;                  return true;
762          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
763                  return true;                  return true;
764          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
765                  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;  
                 }  
         }  
766          exe = ccs_get_exe();          exe = ccs_get_exe();
767          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
768                  return false;                                  head.list) {
769          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
770                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
771                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
772                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
773                          /* Set manager flag. */                                  continue;
774                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
775                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
776                                    continue;
777                  }                  }
778                    /* Set manager flag. */
779                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
780                    found = true;
781                    break;
782          }          }
783          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
784                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 819  static char *ccs_find_condition_part(cha Line 811  static char *ccs_find_condition_part(cha
811                                  break;                                  break;
812                          cp = cp2;                          cp = cp2;
813                  }                  }
814                  *cp++ = '\0';                  *cp = '\0';
815          } else {                  cp += 4;
                 cp = strstr(data, " ; set ");  
                 if (cp)  
                         *cp++ = '\0';  
816          }          }
817          return cp;          return cp;
818  }  }
819    
820  /**  /**
821   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
822   *   *
823   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
824   * @data: String to parse.   * @data: String to parse.
# Line 838  static char *ccs_find_condition_part(cha Line 827  static char *ccs_find_condition_part(cha
827   *   *
828   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
829   */   */
830  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)
831  {  {
832          unsigned int pid;          unsigned int pid;
833          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
834          bool global_pid = false;          bool global_pid = false;
835          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
836                  head->read_execute_only = true;                  head->r.print_execute_only = true;
837                  return true;                  return true;
838          }          }
839          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 853  static bool ccs_is_select_one(struct ccs
853                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
854                  ccs_tasklist_unlock();                  ccs_tasklist_unlock();
855          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
856                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
857                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
858          } else          } else
859                  return false;                  return false;
860          head->write_var1 = domain;          head->w.domain = domain;
861          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
862          if (!head->read_buf)          if (!head->read_buf)
863                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
864          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
865            head->r.print_this_domain_only = true;
866            if (domain)
867                    head->r.domain = &domain->list;
868            else
869                    head->r.eof = true;
870          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
871          head->read_single_domain = true;          if (domain && domain->is_deleted)
872          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");  
         }  
873          return true;          return true;
874  }  }
875    
876  static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,  static bool ccs_same_handler_acl(const struct ccs_acl_info *a,
877                                      struct ccs_condition *cond,                                   const struct ccs_acl_info *b)
878                                      const bool is_delete)  {
879  {          const struct ccs_handler_acl *p1 = container_of(a, typeof(*p1), head);
880          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))          const struct ccs_handler_acl *p2 = container_of(b, typeof(*p2), head);
881                  return ccs_write_capability_policy(data, domain, cond,          return p1->handler == p2->handler;
882                                                     is_delete);  }
883          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))  
884                  return ccs_write_network_policy(data, domain, cond, is_delete);  static bool ccs_same_task_acl(const struct ccs_acl_info *a,
885          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                                const struct ccs_acl_info *b)
886                  return ccs_write_signal_policy(data, domain, cond, is_delete);  {
887          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
888                  return ccs_write_env_policy(data, domain, cond, is_delete);          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
889          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          return p1->domainname == p2->domainname;
890                  return ccs_write_mount_policy(data, domain, cond, is_delete);  }
891          return ccs_write_file_policy(data, domain, cond, is_delete);  
892    /**
893     * ccs_write_task - Update task related list.
894     *
895     * @data:  String to parse.
896     * @param: Pointer to "struct ccs_acl_param".
897     *
898     * Returns 0 on success, negative value otherwise.
899     */
900    static int ccs_write_task(char *data, struct ccs_acl_param *param)
901    {
902            int error;
903            const bool is_auto = ccs_str_starts(&data, "auto_domain_transition ");
904            if (!is_auto && !ccs_str_starts(&data, "manual_domain_transition ")) {
905                    struct ccs_handler_acl e = { };
906                    if (ccs_str_starts(&data, "auto_execute_handler "))
907                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
908                    else if (ccs_str_starts(&data, "denied_execute_handler "))
909                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
910                    else
911                            return -EINVAL;
912                    if (!ccs_correct_path(data))
913                            return -EINVAL;
914                    e.handler = ccs_get_name(data);
915                    if (!e.handler)
916                            return -ENOMEM;
917                    if (e.handler->is_patterned)
918                            error = -EINVAL; /* No patterns allowed. */
919                    else
920                            error = ccs_update_domain(&e.head, sizeof(e), param,
921                                                      ccs_same_handler_acl, NULL);
922                    ccs_put_name(e.handler);
923            } else {
924                    struct ccs_task_acl e = {
925                            .head.type = is_auto ?
926                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL
927                    };
928                    if (!ccs_correct_domain(data))
929                            return -EINVAL;
930                    e.domainname = ccs_get_name(data);
931                    if (!e.domainname)
932                            return -ENOMEM;
933                    error = ccs_update_domain(&e.head, sizeof(e), param,
934                                              ccs_same_task_acl, NULL);
935                    ccs_put_name(e.domainname);
936            }
937            return error;
938    }
939    
940    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
941                                 const bool is_delete)
942    {
943            struct ccs_acl_param param = {
944                    .domain = domain,
945                    .is_delete = is_delete,
946            };
947            static const struct {
948                    const char *keyword;
949                    int (*write) (char *, struct ccs_acl_param *);
950            } ccs_callback[7] = {
951                    { "file ", ccs_write_file },
952                    { "network inet ", ccs_write_inet_network },
953                    { "network unix ", ccs_write_unix_network },
954                    { "misc ", ccs_write_misc },
955                    { "capability ", ccs_write_capability },
956                    { "ipc ", ccs_write_ipc },
957                    { "task ", ccs_write_task },
958            };
959            int error = -EINVAL;
960            u8 i;
961            char *cp = ccs_find_condition_part(data);
962            if (cp) {
963                    param.condition = ccs_get_condition(cp);
964                    if (!param.condition)
965                            return -EINVAL;
966            }
967            for (i = 0; i < 7; i++) {
968                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
969                            continue;
970                    error = ccs_callback[i].write(data, &param);
971                    break;
972            }
973            if (param.condition)
974                    ccs_put_condition(param.condition);
975            return error;
976  }  }
977    
978    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
979            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
980            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
981    };
982            
983  /**  /**
984   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
985   *   *
986   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
987   *   *
988   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
989   */   */
990  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
991  {  {
992          char *data = head->write_buf;          char *data = head->write_buf;
993          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
994          bool is_delete = false;          bool is_delete = false;
995          bool is_select = false;          bool is_select = false;
996          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
997          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
998                  is_delete = true;                  is_delete = true;
999          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1000                  is_select = true;                  is_select = true;
1001          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1002                  return 0;                  return 0;
1003          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1004          if (!ccs_is_policy_manager())          if (!ccs_manager())
1005                  return -EPERM;                  return -EPERM;
1006          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1007                  domain = NULL;                  domain = NULL;
1008                  if (is_delete)                  if (is_delete)
1009                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1010                  else if (is_select)                  else if (is_select)
1011                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1012                  else                  else
1013                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
1014                  head->write_var1 = domain;                  head->w.domain = domain;
1015                  return 0;                  return 0;
1016          }          }
1017          if (!domain)          if (!domain)
# Line 957  static int ccs_write_domain_policy(struc Line 1023  static int ccs_write_domain_policy(struc
1023                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1024                  return 0;                  return 0;
1025          }          }
1026          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
1027                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
1028                  return 0;                  domain->group = (u8) profile;
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {  
                 domain->ignore_global_allow_env = !is_delete;  
1029                  return 0;                  return 0;
1030          }          }
1031          if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1032                  domain->quota_warned = !is_delete;                  const char *cp = ccs_dif[profile];
1033                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
1034          }                          continue;
1035          if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {                  domain->flags[profile] = !is_delete;
                 domain->domain_transition_failed = !is_delete;  
1036                  return 0;                  return 0;
1037          }          }
1038          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;  
1039  }  }
1040    
1041  /**  /**
# Line 990  static int ccs_write_domain_policy(struc Line 1043  static int ccs_write_domain_policy(struc
1043   *   *
1044   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1045   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1046   */   */
1047  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1048                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1049  {  {
1050          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1051          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1052                  head->read_avail--;                  ccs_set_space(head);
1053          if (ptr->is_group)          if (ptr->is_group) {
1054                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1055                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1056          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1057  }                  if (cond)
1058                            ccs_set_string(head, "\"");
1059  /**                  ccs_set_string(head, ptr->filename->name);
1060   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1061   *                          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);  
1062          }          }
1063  }  }
1064    
1065  /**  /**
1066   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1067   *   *
1068   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1069   * @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".  
  *  
  * Returns true on success, false otherwise.  
1070   */   */
1071  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1072                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1073  {  {
1074          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
1075                    ccs_set_space(head);
1076            if (ptr->is_group) {
1077                    ccs_set_string(head, "@");
1078                    ccs_set_string(head, ptr->group->group_name->name);
1079            } else {
1080                    int i;
1081                    unsigned long min = ptr->values[0];
1082                    const unsigned long max = ptr->values[1];
1083                    u8 min_type = ptr->value_type[0];
1084                    const u8 max_type = ptr->value_type[1];
1085                    char buffer[128];
1086                    buffer[0] = '\0';
1087                    for (i = 0; i < 2; i++) {
1088                            switch (min_type) {
1089                            case CCS_VALUE_TYPE_HEXADECIMAL:
1090                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1091                                                  min);
1092                                    break;
1093                            case CCS_VALUE_TYPE_OCTAL:
1094                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1095                                                  min);
1096                                    break;
1097                            default:
1098                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1099                                                  min);
1100                                    break;
1101                            }
1102                            if (min == max && min_type == max_type)
1103                                    break;
1104                            ccs_addprintf(buffer, sizeof(buffer), "-");
1105                            min_type = max_type;
1106                            min = max;
1107                    }
1108                    ccs_io_printf(head, "%s", buffer);
1109            }
1110  }  }
1111    
1112  /**  /**
1113   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1114   *   *
1115   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1116   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1117   *   *
1118   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1119   */   */
1120  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1121                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1122  {  {
1123          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1124          const struct ccs_number_union *numbers_p;          case 0:
1125          const struct ccs_name_union *names_p;                  {
1126          const struct ccs_argv_entry *argv;                          ccs_set_string(head, " if");
1127          const struct ccs_envp_entry *envp;                          head->r.cond_index = 0;
1128          u16 condc;                          head->r.cond_step++;
1129          u16 i;                  }
1130          u16 j;                  /* fall through */
1131          char buffer[32];          case 1:
1132          if (!cond)                  {
1133                  goto no_condition;                          const u16 condc = cond->condc;
1134          condc = cond->condc;                          const struct ccs_condition_element *condp =
1135          condp = (const struct ccs_condition_element *) (cond + 1);                                  (typeof(condp)) (cond + 1);
1136          numbers_p = (const struct ccs_number_union *) (condp + condc);                          const struct ccs_number_union *numbers_p =
1137          names_p = (const struct ccs_name_union *)                                  (typeof(numbers_p)) (condp + condc);
1138                  (numbers_p + cond->numbers_count);                          const struct ccs_name_union *names_p =
1139          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                                  (typeof(names_p))
1140          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                                  (numbers_p + cond->numbers_count);
1141          memset(buffer, 0, sizeof(buffer));                          const struct ccs_argv *argv =
1142          if (condc && !ccs_io_printf(head, "%s", " if"))                                  (typeof(argv)) (names_p + cond->names_count);
1143                  goto out;                          const struct ccs_envp *envp =
1144          for (i = 0; i < condc; i++) {                                  (typeof(envp)) (argv + cond->argc);
1145                  const u8 match = condp->equals;                          u16 skip;
1146                  const u8 left = condp->left;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1147                  const u8 right = condp->right;                                  const u8 left = condp->left;
1148                  condp++;                                  const u8 right = condp->right;
1149                  switch (left) {                                  condp++;
1150                  case CCS_ARGV_ENTRY:                                  switch (left) {
1151                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  case CCS_ARGV_ENTRY:
1152                                             argv->index, argv->is_not ?                                          argv++;
1153                                             "!=" : "=", argv->value->name))                                          continue;
1154                                  goto out;                                  case CCS_ENVP_ENTRY:
1155                          argv++;                                          envp++;
1156                          continue;                                          continue;
1157                  case CCS_ENVP_ENTRY:                                  case CCS_NUMBER_UNION:
1158                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                          numbers_p++;
1159                                             envp->name->name, envp->is_not ?                                          break;
1160                                             "!=" : "="))                                  }
1161                                  goto out;                                  switch (right) {
1162                          if (envp->value) {                                  case CCS_NAME_UNION:
1163                                  if (!ccs_io_printf(head, "\"%s\"",                                          names_p++;
1164                                                     envp->value->name))                                          break;
1165                                          goto out;                                  case CCS_NUMBER_UNION:
1166                          } else {                                          numbers_p++;
1167                                  if (!ccs_io_printf(head, "NULL"))                                          break;
1168                                          goto out;                                  }
1169                            }
1170                            while (head->r.cond_index < condc) {
1171                                    const u8 match = condp->equals;
1172                                    const u8 left = condp->left;
1173                                    const u8 right = condp->right;
1174                                    if (!ccs_flush(head))
1175                                            return false;
1176                                    condp++;
1177                                    head->r.cond_index++;
1178                                    ccs_set_space(head);
1179                                    switch (left) {
1180                                    case CCS_ARGV_ENTRY:
1181                                            ccs_io_printf(head,
1182                                                          "exec.argv[%u]%s\"%s\"",
1183                                                          argv->index,
1184                                                          argv->is_not ?
1185                                                          "!=" : "=",
1186                                                          argv->value->name);
1187                                            argv++;
1188                                            continue;
1189                                    case CCS_ENVP_ENTRY:
1190                                            ccs_io_printf(head,
1191                                                          "exec.envp[\"%s\"]%s",
1192                                                          envp->name->name,
1193                                                          envp->is_not ?
1194                                                          "!=" : "=");
1195                                            if (envp->value) {
1196                                                    ccs_set_string(head, "\"");
1197                                                    ccs_set_string(head, envp->
1198                                                                   value->name);
1199                                                    ccs_set_string(head, "\"");
1200                                            } else {
1201                                                    ccs_set_string(head, "NULL");
1202                                            }
1203                                            envp++;
1204                                            continue;
1205                                    case CCS_NUMBER_UNION:
1206                                            ccs_print_number_union(head,
1207                                                                   numbers_p++);
1208                                            break;
1209                                    default:
1210                                            ccs_set_string(head,
1211                                                   ccs_condition_keyword[left]);
1212                                            break;
1213                                    }
1214                                    ccs_set_string(head, match ? "=" : "!=");
1215                                    switch (right) {
1216                                    case CCS_NAME_UNION:
1217                                            ccs_print_name_union(head, names_p++);
1218                                            break;
1219                                    case CCS_NUMBER_UNION:
1220                                            ccs_print_number_union(head,
1221                                                                   numbers_p++);
1222                                            break;
1223                                    default:
1224                                            ccs_set_string(head,
1225                                                   ccs_condition_keyword[right]);
1226                                            break;
1227                                    }
1228                          }                          }
                         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;  
1229                  }                  }
1230                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1231                          goto out;                  /* fall through */
1232                  switch (right) {          case 2:
1233                  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;  
1234                          break;                          break;
1235                    head->r.cond_step++;
1236                    /* fall through */
1237            case 3:
1238                    if (cond->audit)
1239                            ccs_io_printf(head, " audit=%s",
1240                                          ccs_yesno(cond->audit == 2));
1241                    if (cond->transit) {
1242                            ccs_set_string(head, " auto_domain_transitition=\"");
1243                            ccs_set_string(head, cond->transit->name);
1244                            ccs_set_string(head, "\"");
1245                  }                  }
1246          }                  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"))  
1247                  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;  
                 }  
1248          }          }
1249          head->read_bit = 0;          return false;
         return true;  
1250  }  }
1251    
1252  /**  /**
1253   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_fns - Find next set bit.
1254   *   *
1255   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1256   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1257   *   *
1258   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1259   */   */
1260  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)  
1261  {  {
1262          int pos;          for ( ; bit < 8; bit++)
1263          u8 bit;                  if (perm & (1 << bit))
1264          const u16 perm = ptr->perm;                          break;
1265          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;  
1266  }  }
1267    
1268  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_path2_acl - Print a path2 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
1269  {  {
1270          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1271          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;  
1272  }  }
1273    
1274  /**  /**
1275   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1276   *   *
1277   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1278   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1279   *   *
1280   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1281   */   */
1282  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1283                                        struct ccs_path_number_acl *ptr,                              const struct ccs_acl_info *acl)
                                       const struct ccs_condition *cond)  
1284  {  {
1285          int pos;          const u8 acl_type = acl->type;
1286          u8 bit;          u8 bit;
1287          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1288          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1289               bit++) {          if (acl->is_deleted)
1290                  if (!(perm & (1 << bit)))                  return true;
1291                          continue;   next:
1292                  pos = head->read_avail;          bit = head->r.bit;
1293                  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))  
1294                  return false;                  return false;
1295          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1296                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1297                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1298                          return false;                  const u16 perm = ptr->perm;
1299          }                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1300          return true;                          if (!(perm & (1 << bit)))
1301  }                                  continue;
1302                            if (head->r.print_execute_only &&
1303  /**                              bit != CCS_TYPE_EXECUTE
1304   * ccs_print_network_acl - Print a network ACL entry.                              /* && bit != CCS_TYPE_TRANSIT */)
1305   *                                  continue;
1306   * @head: Pointer to "struct ccs_io_buffer".                          break;
1307   * @ptr:  Pointer to "struct ccs_ip_network_acl".                  }
1308   * @cond: Pointer to "struct ccs_condition". May be NULL.                  if (bit >= CCS_MAX_PATH_OPERATION)
1309   *                          goto done;
1310   * Returns true on success, false otherwise.                  ccs_set_group(head);
1311   */                  ccs_set_string(head, "file ");
1312  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                  ccs_set_string(head, ccs_path_keyword[bit]);
1313                                    struct ccs_ip_network_acl *ptr,                  ccs_print_name_union(head, &ptr->name);
1314                                    const struct ccs_condition *cond)          } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1315  {                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1316          int pos;                  struct ccs_handler_acl *ptr
1317          u8 bit;                          = container_of(acl, typeof(*ptr), head);
1318          const u16 perm = ptr->perm;                  ccs_set_group(head);
1319          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                  ccs_set_string(head, "task ");
1320                  if (!(perm & (1 << bit)))                  ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1321                          continue;                                 ? "auto_execute_handler " :
1322                  pos = head->read_avail;                                 "denied_execute_handler ");
1323                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",                  ccs_set_string(head, ptr->handler->name);
1324                                     ccs_net2keyword(bit)))          } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1325                          goto out;                     acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1326                    struct ccs_task_acl *ptr =
1327                            container_of(acl, typeof(*ptr), head);
1328                    ccs_set_group(head);
1329                    ccs_set_string(head, "task ");
1330                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1331                                   "auto_domain_transition " :
1332                                   "manual_domain_transition ");
1333                    ccs_set_string(head, ptr->domainname->name);
1334            } else if (head->r.print_execute_only) {
1335                    return true;
1336            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1337                    struct ccs_mkdev_acl *ptr =
1338                            container_of(acl, typeof(*ptr), head);
1339                    bit = ccs_fns(ptr->perm, bit);
1340                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1341                            goto done;
1342                    ccs_set_group(head);
1343                    ccs_set_string(head, "file ");
1344                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1345                    ccs_print_name_union(head, &ptr->name);
1346                    ccs_print_number_union(head, &ptr->mode);
1347                    ccs_print_number_union(head, &ptr->major);
1348                    ccs_print_number_union(head, &ptr->minor);
1349            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1350                    struct ccs_path2_acl *ptr =
1351                            container_of(acl, typeof(*ptr), head);
1352                    bit = ccs_fns(ptr->perm, bit);
1353                    if (bit >= CCS_MAX_PATH2_OPERATION)
1354                            goto done;
1355                    ccs_set_group(head);
1356                    ccs_set_string(head, "file ");
1357                    ccs_set_string(head, ccs_path2_keyword[bit]);
1358                    ccs_print_name_union(head, &ptr->name1);
1359                    ccs_print_name_union(head, &ptr->name2);
1360            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1361                    struct ccs_path_number_acl *ptr =
1362                            container_of(acl, typeof(*ptr), head);
1363                    bit = ccs_fns(ptr->perm, bit);
1364                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1365                            goto done;
1366                    ccs_set_group(head);
1367                    ccs_set_string(head, "file ");
1368                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1369                    ccs_print_name_union(head, &ptr->name);
1370                    ccs_print_number_union(head, &ptr->number);
1371            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1372                    struct ccs_env_acl *ptr =
1373                            container_of(acl, typeof(*ptr), head);
1374                    ccs_set_group(head);
1375                    ccs_set_string(head, "misc env ");
1376                    ccs_set_string(head, ptr->env->name);
1377            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1378                    struct ccs_capability_acl *ptr =
1379                            container_of(acl, typeof(*ptr), head);
1380                    ccs_set_group(head);
1381                    ccs_set_string(head, "capability ");
1382                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1383            } else if (acl_type == CCS_TYPE_INET_ACL) {
1384                    struct ccs_inet_acl *ptr =
1385                            container_of(acl, typeof(*ptr), head);
1386                    bit = ccs_fns(ptr->perm, bit);
1387                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1388                            goto done;
1389                    ccs_set_group(head);
1390                    ccs_set_string(head, "network inet ");
1391                    ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1392                    ccs_set_space(head);
1393                    ccs_set_string(head, ccs_socket_keyword[bit]);
1394                    ccs_set_space(head);
1395                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1396                            char buf[128];
1397                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1398                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1399                                             group_name->name))                          ccs_set_string(head,
1400                                  goto out;                                         ptr->address.group->group_name->name);
1401                          break;                          break;
1402                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1403                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1404                                  goto out;                                         ptr->address.ipv4.max);
1405                            ccs_io_printf(head, "%s", buf);
1406                          break;                          break;
1407                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1408                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1409                                  goto out;                                         ptr->address.ipv6.max);
1410                          break;                          ccs_io_printf(head, "%s", buf);
1411                  }                          break;
1412                  if (!ccs_print_number_union(head, &ptr->port) ||                  }
1413                      !ccs_print_condition(head, cond))                  ccs_print_number_union(head, &ptr->port);
1414                          goto out;          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1415                    struct ccs_unix_acl *ptr =
1416                            container_of(acl, typeof(*ptr), head);
1417                    bit = ccs_fns(ptr->perm, bit);
1418                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1419                            goto done;
1420                    ccs_set_group(head);
1421                    ccs_set_string(head, "network unix ");
1422                    ccs_set_string(head, ccs_proto_keyword[ptr->protocol]);
1423                    ccs_set_space(head);
1424                    ccs_set_string(head, ccs_socket_keyword[bit]);
1425                    ccs_print_name_union(head, &ptr->name);
1426            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1427                    struct ccs_signal_acl *ptr =
1428                            container_of(acl, typeof(*ptr), head);
1429                    ccs_set_group(head);
1430                    ccs_set_string(head, "ipc signal ");
1431                    ccs_io_printf(head, "%u ", ptr->sig);
1432                    ccs_set_string(head, ptr->domainname->name);
1433            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1434                    struct ccs_mount_acl *ptr =
1435                            container_of(acl, typeof(*ptr), head);
1436                    ccs_set_group(head);
1437                    ccs_io_printf(head, "file mount");
1438                    ccs_print_name_union(head, &ptr->dev_name);
1439                    ccs_print_name_union(head, &ptr->dir_name);
1440                    ccs_print_name_union(head, &ptr->fs_type);
1441                    ccs_print_number_union(head, &ptr->flags);
1442            }
1443            head->r.bit = bit + 1;
1444            if (acl->cond) {
1445                    head->r.print_cond_part = true;
1446                    head->r.cond_step = 0;
1447                    if (!ccs_flush(head))
1448                            return false;
1449     print_cond_part:
1450                    if (!ccs_print_condition(head, acl->cond))
1451                            return false;
1452                    head->r.print_cond_part = false;
1453            } else {
1454                    ccs_set_lf(head);
1455          }          }
1456          head->read_bit = 0;          switch (acl_type) {
1457          return true;          case CCS_TYPE_PATH_ACL:
1458   out:          case CCS_TYPE_MKDEV_ACL:
1459          head->read_bit = bit;          case CCS_TYPE_PATH2_ACL:
1460          head->read_avail = pos;          case CCS_TYPE_PATH_NUMBER_ACL:
1461          return false;          case CCS_TYPE_INET_ACL:
1462  }          case CCS_TYPE_UNIX_ACL:
1463                    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;  
1464          }          }
1465     done:
1466            head->r.bit = 0;
1467          return true;          return true;
1468  }  }
1469    
1470  /**  /**
1471   * 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".  
1472   *   *
1473   * Returns true on success, false otherwise.   * @head:   Pointer to "struct ccs_io_buffer".
1474   */   * @domain: Pointer to "struct ccs_domain_info".
1475  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.  
1476   *   *
1477   * @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.  
1478   *   *
1479   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1480   */   */
1481  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1482                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain,
1483                                  const struct ccs_condition *cond)                               const u8 index)
1484  {  {
1485          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1486          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||                  struct ccs_acl_info *ptr =
1487              !ccs_print_name_union(head, &ptr->dev_name) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1488              !ccs_print_name_union(head, &ptr->dir_name) ||                  if (!ccs_print_entry(head, ptr))
1489              !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;  
1490          }          }
1491            head->r.acl = NULL;
1492          return true;          return true;
1493  }  }
1494    
1495  /**  /**
1496   * 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.  
1497   *   *
1498   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1499   *   *
1500   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1501   */   */
1502  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1503  {  {
1504          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1505                  return;                  return;
1506          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1507                  head->read_step = 1;                  struct ccs_domain_info *domain =
1508          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1509                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1510                  const char *quota_exceeded = "";                          u8 i;
1511                  const char *transition_failed = "";                  case 0:
1512                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1513                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1514                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1515                  if (head->read_step != 1)                          /* Print domainname and flags. */
1516                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1517                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1518                          continue;                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1519                  /* Print domainname and flags. */                                        domain->profile);
1520                  if (domain->quota_warned)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1521                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                                        domain->group);
1522                  if (domain->domain_transition_failed)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1523                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";                                  if (domain->flags[i])
1524                  if (domain->ignore_global_allow_read)                                          ccs_set_string(head, ccs_dif[i]);
1525                          ignore_global_allow_read                          head->r.step++;
1526                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          ccs_set_lf(head);
1527                  if (domain->ignore_global_allow_env)                          /* fall through */
1528                          ignore_global_allow_env                  case 1:
1529                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          if (!ccs_read_domain2(head, domain, 0))
1530                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                                  return;
1531                                     "%s%s%s%s\n", domain->domainname->name,                          head->r.step++;
1532                                     domain->profile, quota_exceeded,                          /* fall through */
1533                                     transition_failed,                  case 2:
1534                                     ignore_global_allow_read,                          if (!ccs_read_domain2(head, domain, 1))
1535                                     ignore_global_allow_env))                                  return;
1536                          return;                          head->r.step++;
1537                  head->read_step = 2;                          if (!ccs_set_lf(head))
  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))  
1538                                  return;                                  return;
1539                            /* fall through */
1540                    case 3:
1541                            head->r.step = 0;
1542                            if (head->r.print_this_domain_only)
1543                                    goto done;
1544                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1545          }          }
1546          head->read_eof = true;   done:
1547            head->r.eof = true;
1548  }  }
1549    
1550  /**  /**
# Line 1760  static int ccs_write_domain_profile(stru Line 1595  static int ccs_write_domain_profile(stru
1595   */   */
1596  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1597  {  {
1598          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1599                  return;                  return;
1600          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1601                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1602                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1603                  if (domain->is_deleted)                  if (domain->is_deleted)
1604                          continue;                          continue;
1605                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1606                          return;                          return;
1607                    ccs_io_printf(head, "%u ", domain->profile);
1608                    ccs_set_string(head, domain->domainname->name);
1609                    ccs_set_lf(head);
1610          }          }
1611          head->read_eof = true;          head->r.eof = true;
1612  }  }
1613    
1614  /**  /**
# Line 1784  static void ccs_read_domain_profile(stru Line 1620  static void ccs_read_domain_profile(stru
1620   */   */
1621  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1622  {  {
1623          head->read_eof = false;          head->r.eof = false;
1624          return 0;          return 0;
1625  }  }
1626    
# Line 1810  static void ccs_read_pid(struct ccs_io_b Line 1646  static void ccs_read_pid(struct ccs_io_b
1646          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1647          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1648          if (!buf) {          if (!buf) {
1649                  head->read_eof = true;                  head->r.eof = true;
1650                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1651          }          }
1652          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1653                  return;                  return;
1654          head->read_eof = true;          head->r.eof = true;
1655          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1656                  task_info = true;                  task_info = true;
1657          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1837  static void ccs_read_pid(struct ccs_io_b Line 1673  static void ccs_read_pid(struct ccs_io_b
1673          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1674          if (!domain)          if (!domain)
1675                  return;                  return;
1676          if (!task_info)          if (!task_info) {
1677                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1678                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1679          else          } else {
1680                  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,  
1681                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1682                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1683                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1684                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1685                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1686  }  }
1687    
1688    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1689            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1690            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1691            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1692            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1693            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1694    };
1695    
1696    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1697            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1698            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1699            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1700    };
1701    
1702  /**  /**
1703   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1704   *   *
1705   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1706   *   *
1707   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1708   */   */
1709  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1710  {  {
1711          char *data = head->write_buf;          char *data = head->write_buf;
1712          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1713          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1714                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1715          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1716                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1717          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1718                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1719                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1720          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1721                  return ccs_write_domain_initializer_policy(data, true,          };
1722                                                             is_delete);          for (i = 0; i < 3; i++)
1723          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1724                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1725          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1726                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1727          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1728                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1729          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1730                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1731          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1732                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1733          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  unsigned int group;
1734                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1735          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1736                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1737          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                          if (data)
1738                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1739          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1740                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1741                    }
1742            }
1743          return -EINVAL;          return -EINVAL;
1744  }  }
1745    
1746  /**  /**
1747   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1748   *   *
1749   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1750   *   * @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".  
1751   *   *
1752   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1753     *
1754     * Caller holds ccs_read_lock().
1755   */   */
1756  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1757  {  {
1758          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1759          char *arg_ptr = ee->tmp;                  struct ccs_group *group =
1760          int arg_len = 0;                          list_entry(head->r.group, typeof(*group), head.list);
1761          unsigned long pos = bprm->p;                  list_for_each_cookie(head->r.acl, &group->member_list) {
1762          int offset = pos % PAGE_SIZE;                          struct ccs_acl_head *ptr =
1763          bool done = false;                                  list_entry(head->r.acl, typeof(*ptr), list);
1764          if (!bprm->argc)                          if (ptr->is_deleted)
1765                  goto out;                                  continue;
1766          while (1) {                          if (!ccs_flush(head))
1767                  if (!ccs_dump_page(bprm, pos, &ee->dump))                                  return false;
1768                          goto out;                          ccs_set_string(head, ccs_group_name[idx]);
1769                  pos += PAGE_SIZE - offset;                          ccs_set_string(head, group->group_name->name);
1770                  /* Read. */                          if (idx == CCS_PATH_GROUP) {
1771                  while (offset < PAGE_SIZE) {                                  ccs_set_space(head);
1772                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, container_of
1773                          const unsigned char c = kaddr[offset++];                                                 (ptr, struct ccs_path_group,
1774                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                  head)->member_name->name);
1775                                  if (c == '\\') {                          } else if (idx == CCS_NUMBER_GROUP) {
1776                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_number_union(head, &container_of
1777                                          arg_ptr[arg_len++] = '\\';                                                         (ptr, struct ccs_number_group,
1778                                  } else if (c > ' ' && c < 127) {                                                          head)->number);
1779                                          arg_ptr[arg_len++] = c;                          } else if (idx == CCS_ADDRESS_GROUP) {
1780                                  } else {                                  char buffer[128];
1781                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_address_group *member =
1782                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(ptr, typeof(*member),
1783                                          arg_ptr[arg_len++]                                                       head);
1784                                                  = ((c >> 3) & 7) + '0';                                  if (member->is_ipv6)
1785                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          ccs_print_ipv6(buffer, sizeof(buffer),
1786                                  }                                                         member->min.ipv6,
1787                          } else {                                                         member->max.ipv6);
1788                                  arg_ptr[arg_len] = '\0';                                  else
1789                                  done = true;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1790                                  break;                                                         member->min.ipv4,
1791                                                           member->max.ipv4);
1792                                    ccs_io_printf(head, " %s", buffer);
1793                          }                          }
1794                            ccs_set_lf(head);
1795                  }                  }
1796                  offset = 0;                  head->r.acl = NULL;
                 if (done)  
                         break;  
1797          }          }
1798            head->r.group = NULL;
1799          return true;          return true;
  out:  
         return false;  
1800  }  }
1801    
1802  /**  /**
1803   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_policy - Read "struct ccs_..._entry" list.
1804   *   *
1805   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1806     * @idx:  Index number.
1807   *   *
1808   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Returns true on success, false otherwise.
1809     *
1810     * Caller holds ccs_read_lock().
1811   */   */
1812  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)  
1813  {  {
1814          struct ccs_condition *cond;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1815          char *buf;                  struct ccs_acl_head *acl =
1816          int len = 256;                          container_of(head->r.acl, typeof(*acl), list);
1817          char *realpath = NULL;                  if (acl->is_deleted)
1818          char *argv0 = NULL;                          continue;
1819          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);                  if (!ccs_flush(head))
1820          if (profile->learning->learning_exec_realpath) {                          return false;
1821                  struct file *file = ee->bprm->file;                  switch (idx) {
1822  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)                  case CCS_ID_TRANSITION_CONTROL:
1823                  struct path path = { file->f_vfsmnt, file->f_dentry };                          {
1824                  realpath = ccs_realpath_from_path(&path);                                  struct ccs_transition_control *ptr =
1825  #else                                          container_of(acl, typeof(*ptr), head);
1826                  realpath = ccs_realpath_from_path(&file->f_path);                                  ccs_set_string(head,
1827  #endif                                                 ccs_transition_type[ptr->type]);
1828                  if (realpath)                                  ccs_set_string(head, ptr->program ?
1829                          len += strlen(realpath) + 17;                                                 ptr->program->name : "any");
1830          }                                  ccs_set_string(head, " from ");
1831          if (profile->learning->learning_exec_argv0) {                                  ccs_set_string(head, ptr->domainname ?
1832                  if (ccs_get_argv0(ee)) {                                                 ptr->domainname->name : "any");
1833                          argv0 = ee->tmp;                          }
1834                          len += strlen(argv0) + 16;                          break;
1835                    case CCS_ID_AGGREGATOR:
1836                            {
1837                                    struct ccs_aggregator *ptr =
1838                                            container_of(acl, typeof(*ptr), head);
1839                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1840                                    ccs_set_string(head, ptr->original_name->name);
1841                                    ccs_set_space(head);
1842                                    ccs_set_string(head,
1843                                                   ptr->aggregated_name->name);
1844                            }
1845                            break;
1846                    case CCS_ID_PATTERN:
1847                            {
1848                                    struct ccs_pattern *ptr =
1849                                            container_of(acl, typeof(*ptr), head);
1850                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1851                                    ccs_set_string(head, ptr->pattern->name);
1852                            }
1853                            break;
1854                    case CCS_ID_RESERVEDPORT:
1855                            {
1856                                    struct ccs_reserved *ptr =
1857                                            container_of(acl, typeof(*ptr), head);
1858                                    const u16 min_port = ptr->min_port;
1859                                    const u16 max_port = ptr->max_port;
1860                                    ccs_set_string(head,
1861                                                   CCS_KEYWORD_DENY_AUTOBIND);
1862                                    ccs_io_printf(head, "%u", min_port);
1863                                    if (min_port != max_port)
1864                                            ccs_io_printf(head, "-%u", max_port);
1865                            }
1866                            break;
1867                    default:
1868                            continue;
1869                  }                  }
1870                    ccs_set_lf(head);
1871          }          }
1872          buf = kmalloc(len, CCS_GFP_FLAGS);          head->r.acl = NULL;
1873          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;  
1874  }  }
1875    
1876  /**  /**
1877   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1878   *   *
1879   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1880   *   *
1881   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1882   */   */
1883  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1884  {  {
1885          struct ccs_condition *cond;          if (head->r.eof)
1886          char *buf;                  return;
1887          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1888          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
1889          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
1890          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
1891                  symlink = r->obj->symlink_target->name;                  return;
1892                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1893                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1894                    head->r.step++;
1895            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1896                    return;
1897            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1898                   + CCS_MAX_ACL_GROUPS * 2) {
1899                    head->r.group_index = (head->r.step - CCS_MAX_POLICY
1900                                           - CCS_MAX_GROUP) / 2;
1901                    if (!ccs_read_domain2(head,
1902                                          &ccs_acl_group[head->r.group_index],
1903                                          head->r.step & 1))
1904                            return;
1905                    head->r.step++;
1906          }          }
1907          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;  
1908  }  }
1909    
1910  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2126  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1914  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1914  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1915    
1916  /* Structure for query. */  /* Structure for query. */
1917  struct ccs_query_entry {  struct ccs_query {
1918          struct list_head list;          struct list_head list;
1919          char *query;          char *query;
1920          int query_len;          int query_len;
# Line 2135  struct ccs_query_entry { Line 1923  struct ccs_query_entry {
1923          int answer;          int answer;
1924  };  };
1925    
1926  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1927  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1928    
1929  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1930  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1931    
1932    static void ccs_truncate(char *str)
1933    {
1934            while (* (unsigned char *) str > (unsigned char) ' ')
1935                    str++;
1936            *str = '\0';
1937    }
1938    
1939  /**  /**
1940   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1941   *   *
1942   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1943   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1944   *   *
1945   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1946   * 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 2159  int ccs_supervisor(struct ccs_request_in Line 1954  int ccs_supervisor(struct ccs_request_in
1954          int pos;          int pos;
1955          int len;          int len;
1956          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1957          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1958          bool quota_exceeded = false;          bool quota_exceeded = false;
1959          char *header;          char *header;
1960          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1961                  r->domain = ccs_current_domain();          va_start(args, fmt);
1962          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1963            va_end(args);
1964            if (r->mode == CCS_CONFIG_LEARNING) {
1965                  char *buffer;                  char *buffer;
1966                  struct ccs_condition *cond;                  char *realpath = NULL;
1967          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1968                    char *symlink = NULL;
1969                    char *handler = NULL;
1970                    const struct ccs_preference *pref;
1971                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1972                          return 0;                          return 0;
1973                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1974                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (!buffer)  
1975                          return 0;                          return 0;
1976                  va_start(args, fmt);                  pref = &ccs_profile(r->profile)->preference;
1977                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1978                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1979                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1980                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1981                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1982                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1983                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1984                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1985                          char str[] = "if task.type=execute_handler";                                  }
1986                          cond = ccs_get_condition(str);                          }
1987                  } else                          if (pref->learning_exec_realpath) {
1988                          cond = NULL;                                  realpath = strstr(header,
1989                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1990                  ccs_put_condition(cond);                                  if (realpath) {
1991                  kfree(buffer);                                          realpath += 8;
1992                  /* fall through */                                          ccs_truncate(realpath);
1993          case CCS_CONFIG_PERMISSIVE:                                  }
1994                            }
1995                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1996                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1997                               pref->learning_symlink_target) {
1998                            symlink = strstr(header, " symlink.target=\"");
1999                            if (symlink)
2000                                    ccs_truncate(symlink + 1);
2001                    }
2002                    handler = strstr(header, "type=execute_handler");
2003                    if (handler)
2004                            ccs_truncate(handler);
2005                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2006                    if (buffer) {
2007                            va_start(args, fmt);
2008                            vsnprintf(buffer, len - 1, fmt, args);
2009                            va_end(args);
2010                            if (handler || realpath || argv0 || symlink) {
2011                                    ccs_addprintf(buffer, len, " if");
2012                                    if (handler)
2013                                            ccs_addprintf(buffer, len, " task.%s",
2014                                                          handler);
2015                                    if (realpath)
2016                                            ccs_addprintf(buffer, len, " exec.%s",
2017                                                          realpath);
2018                                    if (argv0)
2019                                            ccs_addprintf(buffer, len,
2020                                                          " exec.argv[0]=%s",
2021                                                          argv0);
2022                                    if (symlink)
2023                                            ccs_addprintf(buffer, len, "%s",
2024                                                          symlink);
2025                            }
2026                            ccs_normalize_line(buffer);
2027                            ccs_write_domain2(buffer, domain, false);
2028                            kfree(buffer);
2029                    }
2030                    kfree(header);
2031                  return 0;                  return 0;
2032          }          }
2033            if (r->mode != CCS_CONFIG_ENFORCING)
2034                    return 0;
2035          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2036                  int i;                  int i;
2037                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2038                          return -EPERM;                          return -EPERM;
2039                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->preference.
2040                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2041                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2042                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2043                  }                  }
2044                  return -EPERM;                  return -EPERM;
2045          }          }
2046          va_start(args, fmt);          header = ccs_init_log(&len, r);
         len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;  
         va_end(args);  
         header = ccs_init_audit_log(&len, r);  
2047          if (!header)          if (!header)
2048                  goto out;                  goto out;
2049          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2050          if (!ccs_query_entry)          if (!entry)
2051                  goto out;                  goto out;
2052          len = ccs_round2(len);          len = ccs_round2(len);
2053          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2054          if (!ccs_query_entry->query)          if (!entry->query)
2055                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
2056          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2057          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2058              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2059                  quota_exceeded = true;                  quota_exceeded = true;
2060          } else {          } else {
2061                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2062                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2063          }          }
2064          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2065          if (quota_exceeded)          if (quota_exceeded)
2066                  goto out;                  goto out;
2067          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2068                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2069          kfree(header);          kfree(header);
2070          header = NULL;          header = NULL;
2071          va_start(args, fmt);          va_start(args, fmt);
2072          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2073          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
2074          va_end(args);          va_end(args);
2075          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2076          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2077          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2078          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2079          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2080               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2081               ccs_query_entry->timer++) {               entry->timer++) {
2082                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2083                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2084                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2085                  if (ccs_query_entry->answer)                  if (entry->answer)
2086                          break;                          break;
2087          }          }
2088          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2089          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2090          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2091          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2092          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2093          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2094                  error = CCS_RETRY_REQUEST;                  error = CCS_RETRY_REQUEST;
2095                  r->retry++;                  r->retry++;
# Line 2274  int ccs_supervisor(struct ccs_request_in Line 2106  int ccs_supervisor(struct ccs_request_in
2106                  break;                  break;
2107          }          }
2108   out:   out:
2109          if (ccs_query_entry)          if (entry)
2110                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2111          kfree(ccs_query_entry);          kfree(entry);
2112          kfree(header);          kfree(header);
2113          return error;          return error;
2114  }  }
# Line 2299  static int ccs_poll_query(struct file *f Line 2131  static int ccs_poll_query(struct file *f
2131          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2132                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2133                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2134                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2135                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2136                          if (ptr->answer)                          if (ptr->answer)
2137                                  continue;                                  continue;
2138                          found = true;                          found = true;
# Line 2327  static void ccs_read_query(struct ccs_io Line 2159  static void ccs_read_query(struct ccs_io
2159          int pos = 0;          int pos = 0;
2160          int len = 0;          int len = 0;
2161          char *buf;          char *buf;
2162          if (head->read_avail)          if (head->r.w_pos)
2163                  return;                  return;
2164          if (head->read_buf) {          if (head->read_buf) {
2165                  kfree(head->read_buf);                  kfree(head->read_buf);
2166                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2167          }          }
2168          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2169          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2170                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2171                  if (ptr->answer)                  if (ptr->answer)
2172                          continue;                          continue;
2173                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2174                          continue;                          continue;
2175                  len = ptr->query_len;                  len = ptr->query_len;
2176                  break;                  break;
2177          }          }
2178          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2179          if (!len) {          if (!len) {
2180                  head->read_step = 0;                  head->r.query_index = 0;
2181                  return;                  return;
2182          }          }
2183          buf = kzalloc(len, CCS_GFP_FLAGS);          buf = kzalloc(len, CCS_GFP_FLAGS);
# Line 2356  static void ccs_read_query(struct ccs_io Line 2186  static void ccs_read_query(struct ccs_io
2186          pos = 0;          pos = 0;
2187          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2188          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2189                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2190                  if (ptr->answer)                  if (ptr->answer)
2191                          continue;                          continue;
2192                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2193                          continue;                          continue;
2194                  /*                  /*
2195                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2372  static void ccs_read_query(struct ccs_io Line 2201  static void ccs_read_query(struct ccs_io
2201          }          }
2202          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2203          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2204                  head->read_buf = buf;                  head->read_buf = buf;
2205                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2206                    head->r.query_index++;
2207          } else {          } else {
2208                  kfree(buf);                  kfree(buf);
2209          }          }
# Line 2396  static int ccs_write_answer(struct ccs_i Line 2224  static int ccs_write_answer(struct ccs_i
2224          unsigned int answer;          unsigned int answer;
2225          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2226          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2227                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2228                  ptr->timer = 0;                  ptr->timer = 0;
2229          }          }
2230          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2405  static int ccs_write_answer(struct ccs_i Line 2232  static int ccs_write_answer(struct ccs_i
2232                  return -EINVAL;                  return -EINVAL;
2233          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2234          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2235                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2236                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2237                          continue;                          continue;
2238                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2424  static int ccs_write_answer(struct ccs_i Line 2250  static int ccs_write_answer(struct ccs_i
2250   */   */
2251  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2252  {  {
2253          if (head->read_eof)          if (head->r.eof)
2254                  return;                  return;
2255          ccs_io_printf(head, "1.7.1");          ccs_set_string(head, "1.8.0-pre");
2256          head->read_eof = true;          head->r.eof = true;
2257  }  }
2258    
2259  /**  /**
# Line 2437  static void ccs_read_version(struct ccs_ Line 2263  static void ccs_read_version(struct ccs_
2263   */   */
2264  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2265  {  {
2266          if (head->read_eof)          if (head->r.eof)
2267                  return;                  return;
2268          /*          /*
2269           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2270           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2271           */           */
2272          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2273          head->read_eof = true;          head->r.eof = true;
2274  }  }
2275    
2276  /**  /**
# Line 2464  int ccs_open_control(const u8 type, stru Line 2290  int ccs_open_control(const u8 type, stru
2290          head->type = type;          head->type = type;
2291          switch (type) {          switch (type) {
2292          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2293                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2294                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2295                  break;                  break;
2296          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2297                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2298                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2299                  break;                  break;
2300  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2301          case CCS_GRANTLOG: /* /proc/ccs/grant_log */          case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2302          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2303                  head->poll = ccs_poll_audit_log;                  head->poll = ccs_poll_log;
2304                  head->read = ccs_read_audit_log;                  head->read = ccs_read_log;
2305                  break;                  break;
2306  #endif  #endif
2307          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2515  int ccs_open_control(const u8 type, stru Line 2341  int ccs_open_control(const u8 type, stru
2341                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2342                  break;                  break;
2343          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2344                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2345                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2346                  break;                  break;
2347          }          }
2348          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2552  int ccs_open_control(const u8 type, stru Line 2378  int ccs_open_control(const u8 type, stru
2378                  }                  }
2379          }          }
2380          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2381              type != CCS_GRANTLOG && type != CCS_REJECTLOG) {              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2382                  spin_lock(&ccs_io_buffer_list_lock);                  head->reader_idx = ccs_lock();
                 list_add(&head->list, &ccs_io_buffer_list);  
                 spin_unlock(&ccs_io_buffer_list_lock);  
         }  
2383          file->private_data = head;          file->private_data = head;
2384          /*          /*
2385           * Call the handler now if the file is /proc/ccs/self_domain           * Call the handler now if the file is /proc/ccs/self_domain
# Line 2606  int ccs_poll_control(struct file *file, Line 2429  int ccs_poll_control(struct file *file,
2429  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2430                       const int buffer_len)                       const int buffer_len)
2431  {  {
2432          int len = 0;          int len;
2433          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
         char *cp;  
2434          int idx;          int idx;
2435          if (!head->read)          if (!head->read)
2436                  return -ENOSYS;                  return -ENOSYS;
# Line 2616  int ccs_read_control(struct file *file, Line 2438  int ccs_read_control(struct file *file,
2438                  return -EFAULT;                  return -EFAULT;
2439          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2440                  return -EINTR;                  return -EINTR;
2441            head->read_user_buf = buffer;
2442            head->read_user_buf_avail = buffer_len;
2443          idx = ccs_read_lock();          idx = ccs_read_lock();
2444          while (1) {          if (ccs_flush(head))
2445                  /* Call the policy handler. */                  /* Call the policy handler. */
2446                  head->read(head);                  head->read(head);
2447                  /* Write to buffer. */          ccs_flush(head);
                 len = head->read_avail;  
                 if (len || head->poll || head->read_eof)  
                         break;  
                 len = head->readbuf_size * 2;  
                 cp = kzalloc(len, CCS_GFP_FLAGS);  
                 if (!cp) {  
                         len = -ENOMEM;  
                         goto out;  
                 }  
                 kfree(head->read_buf);  
                 head->read_buf = cp;  
                 head->readbuf_size = len;  
         }  
         if (len > buffer_len)  
                 len = buffer_len;  
         if (!len)  
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2448          ccs_read_unlock(idx);          ccs_read_unlock(idx);
2449            len = head->read_user_buf - buffer;
2450          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2451          return len;          return len;
2452  }  }
# Line 2673  int ccs_write_control(struct file *file, Line 2472  int ccs_write_control(struct file *file,
2472                  return -ENOSYS;                  return -ENOSYS;
2473          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2474                  return -EFAULT;                  return -EFAULT;
         /* Don't allow updating policies by non manager programs. */  
         if (head->write != ccs_write_pid &&  
             head->write != ccs_write_domain_policy &&  
             !ccs_is_policy_manager())  
                 return -EPERM;  
2475          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2476                  return -EINTR;                  return -EINTR;
2477          idx = ccs_read_lock();          idx = ccs_read_lock();
2478            /* Don't allow updating policies by non manager programs. */
2479            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2480                !ccs_manager()) {
2481                    ccs_read_unlock(idx);
2482                    mutex_unlock(&head->io_sem);
2483                    return -EPERM;
2484            }
2485          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2486          while (avail_len > 0) {          while (avail_len > 0) {
2487                  char c;                  char c;
2488                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2489                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2490                          char *cp = kzalloc(len, CCS_GFP_FLAGS);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2491                          if (!cp) {                          if (!cp) {
2492                                  error = -ENOMEM;                                  error = -ENOMEM;
2493                                  break;                                  break;
2494                          }                          }
2495                          memmove(cp, cp0, head->write_avail);                          memmove(cp, cp0, head->w.avail);
2496                          kfree(cp0);                          kfree(cp0);
2497                          head->write_buf = cp;                          head->write_buf = cp;
2498                          cp0 = cp;                          cp0 = cp;
# Line 2703  int ccs_write_control(struct file *file, Line 2504