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

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 2975 by kumaneko, Tue Sep 1 05:34:46 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 3924 by kumaneko, Thu Aug 26 11:01:19 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/policy_io.c   * security/ccsecurity/policy_io.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-rc   2009/09/01   * Version: 1.8.0-pre   2010/08/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,  
16  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
         .audit = &ccs_default_profile.preference,  
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,
19  #endif  #endif
20            .preference.audit_task_info = true,
21            .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. */
30    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    
 /* Lock for protecting "struct ccs_profile"->comment  */  
 static DEFINE_SPINLOCK(ccs_profile_comment_lock);  
   
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 66  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 90  static const char *ccs_mac_keywords[CCS_
90          = "file::pivot_root",          = "file::pivot_root",
91          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
92          = "misc::env",          = "misc::env",
93          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MAC_NETWORK_INET_TCP_BIND]
         = "network::inet_udp_bind",  
         [CCS_MAC_NETWORK_UDP_CONNECT]  
         = "network::inet_udp_connect",  
         [CCS_MAC_NETWORK_TCP_BIND]  
94          = "network::inet_tcp_bind",          = "network::inet_tcp_bind",
95          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_NETWORK_INET_TCP_LISTEN]
96          = "network::inet_tcp_listen",          = "network::inet_tcp_listen",
97          [CCS_MAC_NETWORK_TCP_CONNECT]          [CCS_MAC_NETWORK_INET_TCP_CONNECT]
98          = "network::inet_tcp_connect",          = "network::inet_tcp_connect",
99          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_MAC_NETWORK_INET_TCP_ACCEPT]
100          = "network::inet_tcp_accept",          = "network::inet_tcp_accept",
101          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAC_NETWORK_INET_UDP_BIND]
102            = "network::inet_udp_bind",
103            [CCS_MAC_NETWORK_INET_UDP_SEND]
104            = "network::inet_udp_send",
105            [CCS_MAC_NETWORK_INET_UDP_RECV]
106            = "network::inet_udp_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 146  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 214  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     * @head:   Pointer to "struct ccs_io_buffer".
251     * @string: String to print.
252   *   *
253   * The snprintf() will truncate, but ccs_io_printf() won't.   * 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    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  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  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 258  static struct ccs_profile *ccs_find_or_a Line 315  static struct ccs_profile *ccs_find_or_a
315          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
316          if (ptr)          if (ptr)
317                  return ptr;                  return ptr;
318          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          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;
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 ptr->audit = &ccs_default_profile.preference;  
 #endif  
                 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 278  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  }  }
338    
339  /**  /**
340     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
341     */
342    static void ccs_check_profile(void)
343    {
344            struct ccs_domain_info *domain;
345            const int idx = ccs_read_lock();
346            ccs_policy_loaded = true;
347            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
348                    const u8 profile = domain->profile;
349                    if (ccs_profile_ptr[profile])
350                            continue;
351                    panic("Profile %u (used by '%s') not defined.\n",
352                          profile, domain->domainname->name);
353            }
354            ccs_read_unlock(idx);
355            if (ccs_profile_version != 20090903)
356                    panic("Profile version %u is not supported.\n",
357                          ccs_profile_version);
358            printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/08/01\n");
359            printk(KERN_INFO "Mandatory Access Control activated.\n");
360    }
361    
362    /**
363   * ccs_profile - Find a profile.   * ccs_profile - Find a profile.
364   *   *
365   * @profile: Profile number to find.   * @profile: Profile number to find.
366   *   *
367   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile".
368   */   */
369  struct ccs_profile *ccs_profile(const u8 profile)  struct ccs_profile *ccs_profile(const u8 profile)
370  {  {
# Line 298  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;  
         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;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
         if (!strcmp(data, "PREFERENCE::audit")) {  
                 char *cp2;  
                 if (use_default) {  
                         profile->audit = &ccs_default_profile.preference;  
386                          return 0;                          return 0;
                 }  
                 profile->audit = &profile->preference;  
                 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);  
                 return 0;  
387          }          }
388  #endif          return -1;
389          if (!strcmp(data, "PREFERENCE::enforcing")) {  }
390                  char *cp2;  
391                  if (use_default) {  static void ccs_set_bool(bool *b, const char *string, const char *find)
392                          profile->enforcing = &ccs_default_profile.preference;  {
393                          return 0;          switch (ccs_find_yesno(string, find)) {
394                  }          case 1:
395                  profile->enforcing = &profile->preference;                  *b = true;
396                  if (value >= 0)                  break;
397                          profile->preference.enforcing_verbose = value;          case 0:
398                  cp2 = strstr(cp, "penalty=");                  *b = false;
399                  if (cp2)                  break;
                         sscanf(cp2 + 8, "%u",  
                                &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 *new_comment = ccs_get_name(cp);                  ccs_set_bool(&profile->preference.learning_exec_realpath,
435                  const struct ccs_path_info *old_comment;                               value, "exec.realpath");
436                  /* Protect reader from ccs_put_name(). */                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
437                  spin_lock(&ccs_profile_comment_lock);                               "exec.argv0");
438                  old_comment = profile->comment;                  ccs_set_bool(&profile->preference.learning_symlink_target,
439                  profile->comment = new_comment;                               value, "symlink.target");
440                  spin_unlock(&ccs_profile_comment_lock);                  return;
                 ccs_put_name(old_comment);  
                 return 0;  
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 441  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 473  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", "20090827");                  return 0;
531            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          ccs_io_printf(head, "PREFERENCE::audit={ max_grant_log=%u "                        "max_grant_log=%u max_reject_log=%u "
                       "max_reject_log=%u }\n",  
                       ccs_default_profile.preference.audit_max_grant_log,  
                       ccs_default_profile.preference.audit_max_reject_log);  
562  #endif  #endif
563          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "                        "task_info=%s path_info=%s }\n", index,
564                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",                        "audit",
                       ccs_yesno(ccs_default_profile.preference.  
                                 learning_verbose),  
                       ccs_default_profile.preference.learning_max_entry,  
                       ccs_yesno(ccs_default_profile.preference.  
                                 learning_exec_realpath),  
                       ccs_yesno(ccs_default_profile.preference.  
                                 learning_exec_argv0),  
                       ccs_yesno(ccs_default_profile.preference.  
                                 learning_symlink_target));  
         ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",  
                       ccs_yesno(ccs_default_profile.preference.  
                                 permissive_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];  
                 head->read_step = index;  
                 if (!profile)  
                         continue;  
                 pos = head->read_avail;  
                 spin_lock(&ccs_profile_comment_lock);  
                 done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,  
                                      profile->comment ? profile->comment->name  
                                      : "");  
                 spin_unlock(&ccs_profile_comment_lock);  
                 if (!done)  
                         goto out;  
                 config = profile->default_config;  
565  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
566                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "                        pref->audit_max_grant_log,
567                                     "reject_log=%s }\n", index,                        pref->audit_max_reject_log,
                                    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;  
568  #endif  #endif
569                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                        ccs_yesno(pref->audit_task_info),
570                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                        ccs_yesno(pref->audit_path_info));
571            ccs_io_printf(head, "%u-PREFERENCE::%s={ "
572                          "max_entry=%u exec.realpath=%s "
573                          "exec.argv0=%s symlink.target=%s }\n",
574                          index, "learning",
575                          pref->learning_max_entry,
576                          ccs_yesno(pref->learning_exec_realpath),
577                          ccs_yesno(pref->learning_exec_argv0),
578                          ccs_yesno(pref->learning_symlink_target));
579            ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,
580                          "enforcing", pref->enforcing_penalty);
581    }
582    
583    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
584    {
585            ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],
586                          ccs_yesno(config & CCS_CONFIG_VERBOSE));
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", "20090903");
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                  }                  }
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 if (profile->audit != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::audit={ "  
                                    "max_grant_log=%u max_reject_log=%u }\n",  
                                    index,  
                                    profile->preference.audit_max_grant_log,  
                                    profile->preference.audit_max_reject_log))  
                         goto out;  
 #endif  
                 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 627  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 *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         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          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
696                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
697          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager);
         list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {  
                 if (ptr->manager != e.manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {  
                 list_add_tail_rcu(&entry->list, &ccs_policy_manager_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
698          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
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 682  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 722  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 777  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 796  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 ||
840              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
841                  struct task_struct *p;                  struct task_struct *p;
842                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
843  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
844                  if (global_pid)                  if (global_pid)
845                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
846                                                                   &init_pid_ns);
847                  else                  else
848                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
849  #else  #else
850                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
851  #endif  #endif
852                  if (p)                  if (p)
853                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
854                  read_unlock(&tasklist_lock);                  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 ccs_same_acl_head(&p1->head, &p2->head) &&
882                                                     is_delete);                  p1->handler == p2->handler;
883          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))  }
884                  return ccs_write_network_policy(data, domain, cond, is_delete);  
885          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))  static bool ccs_same_task_acl(const struct ccs_acl_info *a,
886                  return ccs_write_signal_policy(data, domain, cond, is_delete);                                const struct ccs_acl_info *b)
887          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))  {
888                  return ccs_write_env_policy(data, domain, cond, is_delete);          const struct ccs_task_acl *p1 = container_of(a, typeof(*p1), head);
889          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          const struct ccs_task_acl *p2 = container_of(b, typeof(*p2), head);
890                  return ccs_write_mount_policy(data, domain, cond, is_delete);          return ccs_same_acl_head(&p1->head, &p2->head) &&
891          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_UNMOUNT))                  p1->domainname == p2->domainname;
                 return ccs_write_umount_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CHROOT))  
                 return ccs_write_chroot_policy(data, domain, cond, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_PIVOT_ROOT))  
                 return ccs_write_pivot_root_policy(data, domain, cond,  
                                                    is_delete);  
         return ccs_write_file_policy(data, domain, cond, is_delete);  
892  }  }
893    
894  /**  /**
895   * ccs_write_domain_policy - Write domain policy.   * ccs_write_task - Update task related list.
896     *
897     * @data:      String to parse.
898     * @domain:    Pointer to "struct ccs_domain_info".
899     * @condition: Pointer to "struct ccs_condition". Maybe NULL.
900     * @is_delete: True if it is a delete request.
901     *
902     * Returns 0 on success, negative value otherwise.
903     */
904    static int ccs_write_task(char *data, struct ccs_domain_info *domain,
905                              struct ccs_condition *condition,
906                              const bool is_delete)
907    {
908            int error;
909            const bool is_auto = ccs_str_starts(&data, "auto_domain_transition ");
910            if (!is_auto && !ccs_str_starts(&data, "manual_domain_transition ")) {
911                    struct ccs_handler_acl e = {
912                            .head.cond = condition,
913                    };
914                    if (ccs_str_starts(&data, "auto_execute_handler "))
915                            e.head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
916                    else if (ccs_str_starts(&data, "denied_execute_handler "))
917                            e.head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
918                    else
919                            return -EINVAL;
920                    if (!ccs_correct_path(data))
921                            return -EINVAL;
922                    e.handler = ccs_get_name(data);
923                    if (!e.handler)
924                            return -ENOMEM;
925                    if (e.handler->is_patterned)
926                            error = -EINVAL; /* No patterns allowed. */
927                    else
928                            error = ccs_update_domain(&e.head, sizeof(e),
929                                                      is_delete, domain,
930                                                      ccs_same_handler_acl, NULL);
931                    ccs_put_name(e.handler);
932            } else {
933                    struct ccs_task_acl e = {
934                            .head.type = is_auto ?
935                            CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL,
936                            .head.cond = condition,
937                    };
938                    if (!ccs_correct_domain(data))
939                            return -EINVAL;
940                    e.domainname = ccs_get_name(data);
941                    if (!e.domainname)
942                            return -ENOMEM;
943                    error = ccs_update_domain(&e.head, sizeof(e), is_delete,
944                                              domain, ccs_same_task_acl, NULL);
945                    ccs_put_name(e.domainname);
946            }
947            return error;
948    }
949    
950    static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
951                                 const bool is_delete)
952    {
953            static const struct {
954                    const char *keyword;
955                    int (*write) (char *, struct ccs_domain_info *,
956                                  struct ccs_condition *, const bool);
957            } ccs_callback[7] = {
958                    { "file ", ccs_write_file },
959                    { "network inet ", ccs_write_inet_network },
960                    { "network unix ", ccs_write_unix_network },
961                    { "misc ", ccs_write_misc },
962                    { "capability ", ccs_write_capability },
963                    { "ipc ", ccs_write_ipc },
964                    { "task ", ccs_write_task },
965            };
966            int error = -EINVAL;
967            u8 i;
968            struct ccs_condition *cond = NULL;
969            char *cp = ccs_find_condition_part(data);
970            if (cp) {
971                    cond = ccs_get_condition(cp);
972                    if (!cond)
973                            return -EINVAL;
974            }
975            for (i = 0; i < 7; i++) {
976                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
977                            continue;
978                    error = ccs_callback[i].write(data, domain, cond, is_delete);
979                    break;
980            }
981            if (cond)
982                    ccs_put_condition(cond);
983            return error;
984    }
985    
986    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
987            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
988            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
989    };
990            
991    /**
992     * ccs_write_domain - Write domain policy.
993   *   *
994   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
995   *   *
996   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
997   */   */
998  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
999  {  {
1000          char *data = head->write_buf;          char *data = head->write_buf;
1001          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1002          bool is_delete = false;          bool is_delete = false;
1003          bool is_select = false;          bool is_select = false;
1004          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
1005          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
1006                  is_delete = true;                  is_delete = true;
1007          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1008                  is_select = true;                  is_select = true;
1009          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1010                  return 0;                  return 0;
1011          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1012          if (!ccs_is_policy_manager())          if (!ccs_manager())
1013                  return -EPERM;                  return -EPERM;
1014          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1015                  domain = NULL;                  domain = NULL;
1016                  if (is_delete)                  if (is_delete)
1017                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1018                  else if (is_select)                  else if (is_select)
1019                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1020                  else                  else
1021                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
1022                  head->write_var1 = domain;                  head->w.domain = domain;
1023                  return 0;                  return 0;
1024          }          }
1025          if (!domain)          if (!domain)
# Line 917  static int ccs_write_domain_policy(struc Line 1027  static int ccs_write_domain_policy(struc
1027    
1028          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
1029              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
1030                  if (ccs_profile(profile))                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
1031                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1032                  return 0;                  return 0;
1033          }          }
1034          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
1035                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
1036                    domain->group = (u8) profile;
1037                  return 0;                  return 0;
1038          }          }
1039          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1040                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
1041                    if (strncmp(data, cp, strlen(cp) - 1))
1042                            continue;
1043                    domain->flags[profile] = !is_delete;
1044                  return 0;                  return 0;
1045          }          }
1046          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;  
1047  }  }
1048    
1049  /**  /**
# Line 946  static int ccs_write_domain_policy(struc Line 1051  static int ccs_write_domain_policy(struc
1051   *   *
1052   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1053   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1054   */   */
1055  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1056                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1057  {  {
1058          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1059          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1060                  head->read_avail--;                  ccs_set_space(head);
1061          if (ptr->is_group)          if (ptr->is_group) {
1062                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1063                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1064          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1065  }                  if (cond)
1066                            ccs_set_string(head, "\"");
1067  /**                  ccs_set_string(head, ptr->filename->name);
1068   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1069   *                          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);  
1070          }          }
1071  }  }
1072    
1073  /**  /**
1074   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1075   *   *
1076   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1077   * @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.  
1078   */   */
1079  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1080                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1081  {  {
1082          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
1083                    ccs_set_space(head);
1084            if (ptr->is_group) {
1085                    ccs_set_string(head, "@");
1086                    ccs_set_string(head, ptr->group->group_name->name);
1087            } else {
1088                    int i;
1089                    unsigned long min = ptr->values[0];
1090                    const unsigned long max = ptr->values[1];
1091                    u8 min_type = ptr->value_type[0];
1092                    const u8 max_type = ptr->value_type[1];
1093                    char buffer[128];
1094                    buffer[0] = '\0';
1095                    for (i = 0; i < 2; i++) {
1096                            switch (min_type) {
1097                            case CCS_VALUE_TYPE_HEXADECIMAL:
1098                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1099                                                  min);
1100                                    break;
1101                            case CCS_VALUE_TYPE_OCTAL:
1102                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1103                                                  min);
1104                                    break;
1105                            default:
1106                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1107                                                  min);
1108                                    break;
1109                            }
1110                            if (min == max && min_type == max_type)
1111                                    break;
1112                            ccs_addprintf(buffer, sizeof(buffer), "-");
1113                            min_type = max_type;
1114                            min = max;
1115                    }
1116                    ccs_io_printf(head, "%s", buffer);
1117            }
1118  }  }
1119    
1120  /**  /**
1121   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1122   *   *
1123   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1124   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1125   *   *
1126   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1127   */   */
1128  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1129                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1130  {  {
1131          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1132          const struct ccs_number_union *numbers_p;          case 0:
1133          const struct ccs_name_union *names_p;                  {
1134          const struct ccs_argv_entry *argv;                          ccs_set_string(head, " if");
1135          const struct ccs_envp_entry *envp;                          head->r.cond_index = 0;
1136          u16 condc;                          head->r.cond_step++;
1137          u16 i;                  }
1138          u16 j;                  /* fall through */
1139          char buffer[32];          case 1:
1140          if (!cond)                  {
1141                  goto no_condition;                          const u16 condc = cond->condc;
1142          condc = cond->condc;                          const struct ccs_condition_element *condp =
1143          condp = (const struct ccs_condition_element *) (cond + 1);                                  (typeof(condp)) (cond + 1);
1144          numbers_p = (const struct ccs_number_union *) (condp + condc);                          const struct ccs_number_union *numbers_p =
1145          names_p = (const struct ccs_name_union *)                                  (typeof(numbers_p)) (condp + condc);
1146                  (numbers_p + cond->numbers_count);                          const struct ccs_name_union *names_p =
1147          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                                  (typeof(names_p))
1148          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                                  (numbers_p + cond->numbers_count);
1149          memset(buffer, 0, sizeof(buffer));                          const struct ccs_argv *argv =
1150          if (condc && !ccs_io_printf(head, "%s", " if"))                                  (typeof(argv)) (names_p + cond->names_count);
1151                  goto out;                          const struct ccs_envp *envp =
1152          for (i = 0; i < condc; i++) {                                  (typeof(envp)) (argv + cond->argc);
1153                  const u8 match = condp->equals;                          u16 skip;
1154                  const u8 left = condp->left;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1155                  const u8 right = condp->right;                                  const u8 left = condp->left;
1156                  condp++;                                  const u8 right = condp->right;
1157                  switch (left) {                                  condp++;
1158                  case CCS_ARGV_ENTRY:                                  switch (left) {
1159                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  case CCS_ARGV_ENTRY:
1160                                             argv->index, argv->is_not ?                                          argv++;
1161                                             "!=" : "=", argv->value->name))                                          continue;
1162                                  goto out;                                  case CCS_ENVP_ENTRY:
1163                          argv++;                                          envp++;
1164                          continue;                                          continue;
1165                  case CCS_ENVP_ENTRY:                                  case CCS_NUMBER_UNION:
1166                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                          numbers_p++;
1167                                             envp->name->name, envp->is_not ?                                          break;
1168                                             "!=" : "="))                                  }
1169                                  goto out;                                  switch (right) {
1170                          if (envp->value) {                                  case CCS_NAME_UNION:
1171                                  if (!ccs_io_printf(head, "\"%s\"",                                          names_p++;
1172                                                     envp->value->name))                                          break;
1173                                          goto out;                                  case CCS_NUMBER_UNION:
1174                          } else {                                          numbers_p++;
1175                                  if (!ccs_io_printf(head, "NULL"))                                          break;
1176                                          goto out;                                  }
1177                            }
1178                            while (head->r.cond_index < condc) {
1179                                    const u8 match = condp->equals;
1180                                    const u8 left = condp->left;
1181                                    const u8 right = condp->right;
1182                                    if (!ccs_flush(head))
1183                                            return false;
1184                                    condp++;
1185                                    head->r.cond_index++;
1186                                    ccs_set_space(head);
1187                                    switch (left) {
1188                                    case CCS_ARGV_ENTRY:
1189                                            ccs_io_printf(head,
1190                                                          "exec.argv[%u]%s\"%s\"",
1191                                                          argv->index,
1192                                                          argv->is_not ?
1193                                                          "!=" : "=",
1194                                                          argv->value->name);
1195                                            argv++;
1196                                            continue;
1197                                    case CCS_ENVP_ENTRY:
1198                                            ccs_io_printf(head,
1199                                                          "exec.envp[\"%s\"]%s",
1200                                                          envp->name->name,
1201                                                          envp->is_not ?
1202                                                          "!=" : "=");
1203                                            if (envp->value) {
1204                                                    ccs_set_string(head, "\"");
1205                                                    ccs_set_string(head, envp->
1206                                                                   value->name);
1207                                                    ccs_set_string(head, "\"");
1208                                            } else {
1209                                                    ccs_set_string(head, "NULL");
1210                                            }
1211                                            envp++;
1212                                            continue;
1213                                    case CCS_NUMBER_UNION:
1214                                            ccs_print_number_union(head,
1215                                                                   numbers_p++);
1216                                            break;
1217                                    default:
1218                                            ccs_set_string(head,
1219                                                   ccs_condition_keyword[left]);
1220                                            break;
1221                                    }
1222                                    ccs_set_string(head, match ? "=" : "!=");
1223                                    switch (right) {
1224                                    case CCS_NAME_UNION:
1225                                            ccs_print_name_union(head, names_p++);
1226                                            break;
1227                                    case CCS_NUMBER_UNION:
1228                                            ccs_print_number_union(head,
1229                                                                   numbers_p++);
1230                                            break;
1231                                    default:
1232                                            ccs_set_string(head,
1233                                                   ccs_condition_keyword[right]);
1234                                            break;
1235                                    }
1236                          }                          }
                         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;  
1237                  }                  }
1238                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1239                          goto out;                  /* fall through */
1240                  switch (right) {          case 2:
1241                  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;  
1242                          break;                          break;
1243                    head->r.cond_step++;
1244                    /* fall through */
1245            case 3:
1246                    if (cond->audit)
1247                            ccs_io_printf(head, " audit=%s",
1248                                          ccs_yesno(cond->audit == 2));
1249                    if (cond->transit) {
1250                            ccs_set_string(head, " auto_domain_transitition=\"");
1251                            ccs_set_string(head, cond->transit->name);
1252                            ccs_set_string(head, "\"");
1253                  }                  }
1254          }                  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"))  
1255                  return true;                  return true;
  out:  
         return false;  
 }  
   
 /**  
  * ccs_print_path_acl - Print a single 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)  
                         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;  
                 }  
1256          }          }
1257          head->read_bit = 0;          return false;
         return true;  
1258  }  }
1259    
1260  /**  /**
1261   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_fns - Find next set bit.
1262   *   *
1263   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1264   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1265   *   *
1266   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1267   */   */
1268  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)  
1269  {  {
1270          int pos;          for ( ; bit < 8; bit++)
1271          u8 bit;                  if (perm & (1 << bit))
1272          const u16 perm = ptr->perm;                          break;
1273          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;  
1274  }  }
1275    
1276  /**  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)  
1277  {  {
1278          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1279          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;  
1280  }  }
1281    
1282  /**  /**
1283   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1284   *   *
1285   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1286   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1287   *   *
1288   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1289   */   */
1290  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1291                                        struct ccs_path_number_acl *ptr,                              const struct ccs_acl_info *acl)
                                       const struct ccs_condition *cond)  
1292  {  {
1293          int pos;          const u8 acl_type = acl->type;
1294          u8 bit;          u8 bit;
1295          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1296          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1297               bit++) {          if (acl->is_deleted)
1298                  if (!(perm & (1 << bit)))                  return true;
1299                          continue;   next:
1300                  pos = head->read_avail;          bit = head->r.bit;
1301                  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))  
1302                  return false;                  return false;
1303          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1304                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1305                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1306                          return false;                  const u16 perm = ptr->perm;
1307          }                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1308          return true;                          if (!(perm & (1 << bit)))
1309  }                                  continue;
1310                            if (head->r.print_execute_only &&
1311  /**                              bit != CCS_TYPE_EXECUTE
1312   * ccs_print_network_acl - Print a network ACL entry.                              /* && bit != CCS_TYPE_TRANSIT */)
1313   *                                  continue;
1314   * @head: Pointer to "struct ccs_io_buffer".                          break;
1315   * @ptr:  Pointer to "struct ccs_ip_network_acl".                  }
1316   * @cond: Pointer to "struct ccs_condition". May be NULL.                  if (bit >= CCS_MAX_PATH_OPERATION)
1317   *                          goto done;
1318   * Returns true on success, false otherwise.                  ccs_set_group(head);
1319   */                  ccs_set_string(head, "file ");
1320  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                  ccs_set_string(head, ccs_path_keyword[bit]);
1321                                    struct ccs_ip_network_acl *ptr,                  ccs_print_name_union(head, &ptr->name);
1322                                    const struct ccs_condition *cond)          } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
1323  {                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1324          int pos;                  struct ccs_handler_acl *ptr
1325          u8 bit;                          = container_of(acl, typeof(*ptr), head);
1326          const u16 perm = ptr->perm;                  ccs_set_group(head);
1327          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                  ccs_set_string(head, "task ");
1328                  if (!(perm & (1 << bit)))                  ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
1329                          continue;                                 ? "auto_execute_handler " :
1330                  pos = head->read_avail;                                 "denied_execute_handler ");
1331                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",                  ccs_set_string(head, ptr->handler->name);
1332                                     ccs_net2keyword(bit)))          } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
1333                          goto out;                     acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
1334                    struct ccs_task_acl *ptr =
1335                            container_of(acl, typeof(*ptr), head);
1336                    ccs_set_group(head);
1337                    ccs_set_string(head, "task ");
1338                    ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
1339                                   "auto_domain_transition " :
1340                                   "manual_domain_transition ");
1341                    ccs_set_string(head, ptr->domainname->name);
1342            } else if (head->r.print_execute_only) {
1343                    return true;
1344            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1345                    struct ccs_mkdev_acl *ptr =
1346                            container_of(acl, typeof(*ptr), head);
1347                    bit = ccs_fns(ptr->perm, bit);
1348                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1349                            goto done;
1350                    ccs_set_group(head);
1351                    ccs_set_string(head, "file ");
1352                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1353                    ccs_print_name_union(head, &ptr->name);
1354                    ccs_print_number_union(head, &ptr->mode);
1355                    ccs_print_number_union(head, &ptr->major);
1356                    ccs_print_number_union(head, &ptr->minor);
1357            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1358                    struct ccs_path2_acl *ptr =
1359                            container_of(acl, typeof(*ptr), head);
1360                    bit = ccs_fns(ptr->perm, bit);
1361                    if (bit >= CCS_MAX_PATH2_OPERATION)
1362                            goto done;
1363                    ccs_set_group(head);
1364                    ccs_set_string(head, "file ");
1365                    ccs_set_string(head, ccs_path2_keyword[bit]);
1366                    ccs_print_name_union(head, &ptr->name1);
1367                    ccs_print_name_union(head, &ptr->name2);
1368            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1369                    struct ccs_path_number_acl *ptr =
1370                            container_of(acl, typeof(*ptr), head);
1371                    bit = ccs_fns(ptr->perm, bit);
1372                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1373                            goto done;
1374                    ccs_set_group(head);
1375                    ccs_set_string(head, "file ");
1376                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1377                    ccs_print_name_union(head, &ptr->name);
1378                    ccs_print_number_union(head, &ptr->number);
1379            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1380                    struct ccs_env_acl *ptr =
1381                            container_of(acl, typeof(*ptr), head);
1382                    ccs_set_group(head);
1383                    ccs_set_string(head, "misc env ");
1384                    ccs_set_string(head, ptr->env->name);
1385            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1386                    struct ccs_capability_acl *ptr =
1387                            container_of(acl, typeof(*ptr), head);
1388                    ccs_set_group(head);
1389                    ccs_set_string(head, "capability ");
1390                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1391            } else if (acl_type == CCS_TYPE_INET_ACL) {
1392                    struct ccs_inet_acl *ptr =
1393                            container_of(acl, typeof(*ptr), head);
1394                    bit = ccs_fns(ptr->perm, bit);
1395                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1396                            goto done;
1397                    ccs_set_group(head);
1398                    ccs_set_string(head, "network inet ");
1399                    ccs_set_string(head, ccs_inet_keyword[ptr->protocol]);
1400                    ccs_set_space(head);
1401                    ccs_set_string(head, ccs_net_keyword[bit]);
1402                    ccs_set_space(head);
1403                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1404                            char buf[128];
1405                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1406                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1407                                             group_name->name))                          ccs_set_string(head,
1408                                  goto out;                                         ptr->address.group->group_name->name);
1409                          break;                          break;
1410                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1411                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1412                                  goto out;                                         ptr->address.ipv4.max);
1413                            ccs_io_printf(head, "%s", buf);
1414                          break;                          break;
1415                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1416                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1417                                  goto out;                                         ptr->address.ipv6.max);
1418                          break;                          ccs_io_printf(head, "%s", buf);
1419                  }                          break;
1420                  if (!ccs_print_number_union(head, &ptr->port) ||                  }
1421                      !ccs_print_condition(head, cond))                  ccs_print_number_union(head, &ptr->port);
1422                          goto out;          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1423          }                  struct ccs_unix_acl *ptr =
1424          head->read_bit = 0;                          container_of(acl, typeof(*ptr), head);
1425          return true;                  bit = ccs_fns(ptr->perm, bit);
1426   out:                  if (bit >= CCS_MAX_NETWORK_OPERATION)
1427          head->read_bit = bit;                          goto done;
1428          head->read_avail = pos;                  ccs_set_group(head);
1429          return false;                  ccs_set_string(head, "network unix ");
1430  }                  ccs_set_string(head, ccs_unix_keyword[ptr->protocol]);
1431                    ccs_set_space(head);
1432  /**                  ccs_set_string(head, ccs_net_keyword[bit]);
1433   * ccs_print_signal_acl - Print a signal ACL entry.                  ccs_print_name_union(head, &ptr->name);
1434   *          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1435   * @head: Pointer to "struct ccs_io_buffer".                  struct ccs_signal_acl *ptr =
1436   * @ptr:  Pointer to "struct signale_acl".                          container_of(acl, typeof(*ptr), head);
1437   * @cond: Pointer to "struct ccs_condition". May be NULL.                  ccs_set_group(head);
1438   *                  ccs_set_string(head, "ipc signal ");
1439   * Returns true on success, false otherwise.                  ccs_io_printf(head, "%u ", ptr->sig);
1440   */                  ccs_set_string(head, ptr->domainname->name);
1441  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,          } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1442                                   struct ccs_signal_acl *ptr,                  struct ccs_mount_acl *ptr =
1443                                   const struct ccs_condition *cond)                          container_of(acl, typeof(*ptr), head);
1444  {                  ccs_set_group(head);
1445          const int pos = head->read_avail;                  ccs_io_printf(head, "file mount");
1446          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",                  ccs_print_name_union(head, &ptr->dev_name);
1447                             ptr->sig, ptr->domainname->name) ||                  ccs_print_name_union(head, &ptr->dir_name);
1448              !ccs_print_condition(head, cond)) {                  ccs_print_name_union(head, &ptr->fs_type);
1449                  head->read_avail = pos;                  ccs_print_number_union(head, &ptr->flags);
1450                  return false;          }
1451          }          head->r.bit = bit + 1;
1452          return true;          if (acl->cond) {
1453  }                  head->r.print_cond_part = true;
1454                    head->r.cond_step = 0;
1455  /**                  if (!ccs_flush(head))
1456   * ccs_print_execute_handler_record - Print an execute handler ACL entry.                          return false;
1457   *   print_cond_part:
1458   * @head:    Pointer to "struct ccs_io_buffer".                  if (!ccs_print_condition(head, acl->cond))
1459   * @keyword: Name of the keyword.                          return false;
1460   * @ptr:     Pointer to "struct ccs_execute_handler_record".                  head->r.print_cond_part = false;
1461   *          } else {
1462   * Returns true on success, false otherwise.                  ccs_set_lf(head);
  */  
 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  
                                              const char *keyword,  
                                              struct ccs_execute_handler_record *  
                                              ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1463          }          }
1464          return true;          switch (acl_type) {
1465  }          case CCS_TYPE_PATH_ACL:
1466            case CCS_TYPE_MKDEV_ACL:
1467  /**          case CCS_TYPE_PATH2_ACL:
1468   * ccs_print_umount_acl - Print a mount ACL entry.          case CCS_TYPE_PATH_NUMBER_ACL:
1469   *          case CCS_TYPE_INET_ACL:
1470   * @head: Pointer to "struct ccs_io_buffer".          case CCS_TYPE_UNIX_ACL:
1471   * @ptr:  Pointer to "struct ccs_umount_acl".                  goto next;
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_umount_acl(struct ccs_io_buffer *head,  
                                  struct ccs_umount_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_UNMOUNT) ||  
             !ccs_print_name_union(head, &ptr->dir) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1472          }          }
1473     done:
1474            head->r.bit = 0;
1475          return true;          return true;
1476  }  }
1477    
1478  /**  /**
1479   * ccs_print_chroot_acl - Print a chroot ACL entry.   * ccs_read_domain2 - Read domain policy.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_chroot_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1480   *   *
1481   * Returns true on success, false otherwise.   * @head:   Pointer to "struct ccs_io_buffer".
1482   */   * @domain: Pointer to "struct ccs_domain_info".
1483  static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,   * @index:  Index number.
                                  struct ccs_chroot_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CHROOT) ||  
             !ccs_print_name_union(head, &ptr->dir) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_pivot_root_acl - Print a pivot_root ACL entry.  
1484   *   *
1485   * @head: Pointer to "struct ccs_io_buffer".   * Caller holds ccs_read_lock().
  * @ptr:  Pointer to "struct ccs_pivot_root_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1486   *   *
1487   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1488   */   */
1489  static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1490                                       struct ccs_pivot_root_acl *ptr,                               struct ccs_domain_info *domain,
1491                                       const struct ccs_condition *cond)                               const u8 index)
1492  {  {
1493          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list[index]) {
1494          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_PIVOT_ROOT) ||                  struct ccs_acl_info *ptr =
1495              !ccs_print_name_union(head, &ptr->new_root) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1496              !ccs_print_name_union(head, &ptr->old_root) ||                  if (!ccs_print_entry(head, ptr))
1497              !ccs_print_condition(head, cond)) {                          return false;
                 head->read_avail = pos;  
                 return false;  
1498          }          }
1499            head->r.acl = NULL;
1500          return true;          return true;
1501  }  }
1502    
1503  /**  /**
1504   * 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);  
         }  
         if (acl_type == CCS_TYPE_UMOUNT_ACL) {  
                 struct ccs_umount_acl *acl  
                         = container_of(ptr, struct ccs_umount_acl, head);  
                 return ccs_print_umount_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CHROOT_ACL) {  
                 struct ccs_chroot_acl *acl  
                         = container_of(ptr, struct ccs_chroot_acl, head);  
                 return ccs_print_chroot_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PIVOT_ROOT_ACL) {  
                 struct ccs_pivot_root_acl *acl  
                         = container_of(ptr, struct ccs_pivot_root_acl,  
                                        head);  
                 return ccs_print_pivot_root_acl(head, acl, cond);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
1505   *   *
1506   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1507   *   *
1508   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1509   */   */
1510  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1511  {  {
1512          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1513                  return;                  return;
1514          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1515                  head->read_step = 1;                  struct ccs_domain_info *domain =
1516          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1517                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1518                  const char *quota_exceeded = "";                          u8 i;
1519                  const char *transition_failed = "";                  case 0:
1520                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1521                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1522                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1523                  if (head->read_step != 1)                          /* Print domainname and flags. */
1524                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1525                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1526                          continue;                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1527                  /* Print domainname and flags. */                                        domain->profile);
1528                  if (domain->quota_warned)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1529                          quota_exceeded = "quota_exceeded\n";                                        domain->group);
1530                  if (domain->domain_transition_failed)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1531                          transition_failed = "transition_failed\n";                                  if (domain->flags[i])
1532                  if (domain->ignore_global_allow_read)                                          ccs_set_string(head, ccs_dif[i]);
1533                          ignore_global_allow_read                          head->r.step++;
1534                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          ccs_set_lf(head);
1535                  if (domain->ignore_global_allow_env)                          /* fall through */
1536                          ignore_global_allow_env                  case 1:
1537                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          if (!ccs_read_domain2(head, domain, 0))
1538                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                                  return;
1539                                     "%s%s%s%s\n", domain->domainname->name,                          head->r.step++;
1540                                     domain->profile, quota_exceeded,                          /* fall through */
1541                                     transition_failed,                  case 2:
1542                                     ignore_global_allow_read,                          if (!ccs_read_domain2(head, domain, 1))
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
1543                                  return;                                  return;
1544                            head->r.step++;
1545                            if (!ccs_set_lf(head))
1546                                    return;
1547                            /* fall through */
1548                    case 3:
1549                            head->r.step = 0;
1550                            if (head->r.print_this_domain_only)
1551                                    goto done;
1552                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1553          }          }
1554          head->read_eof = true;   done:
1555            head->r.eof = true;
1556  }  }
1557    
1558  /**  /**
# Line 1785  static int ccs_write_domain_profile(stru Line 1582  static int ccs_write_domain_profile(stru
1582          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1583                  return -EINVAL;                  return -EINVAL;
1584          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1585          if (domain && ccs_profile(profile))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1586                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1587          return 0;          return 0;
1588  }  }
# Line 1806  static int ccs_write_domain_profile(stru Line 1603  static int ccs_write_domain_profile(stru
1603   */   */
1604  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1605  {  {
1606          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1607                  return;                  return;
1608          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1609                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1610                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1611                  if (domain->is_deleted)                  if (domain->is_deleted)
1612                          continue;                          continue;
1613                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1614                          return;                          return;
1615                    ccs_io_printf(head, "%u ", domain->profile);
1616                    ccs_set_string(head, domain->domainname->name);
1617                    ccs_set_lf(head);
1618          }          }
1619          head->read_eof = true;          head->r.eof = true;
1620  }  }
1621    
1622  /**  /**
# Line 1830  static void ccs_read_domain_profile(stru Line 1628  static void ccs_read_domain_profile(stru
1628   */   */
1629  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1630  {  {
1631          head->read_eof = false;          head->r.eof = false;
1632          return 0;          return 0;
1633  }  }
1634    
# Line 1855  static void ccs_read_pid(struct ccs_io_b Line 1653  static void ccs_read_pid(struct ccs_io_b
1653          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1654          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1655          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1656          if (!buf)          if (!buf) {
1657                    head->r.eof = true;
1658                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1659          if (head->read_avail || head->read_eof)          }
1660            if (head->r.w_pos || head->r.eof)
1661                  return;                  return;
1662          head->read_eof = true;          head->r.eof = true;
1663          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1664                  task_info = true;                  task_info = true;
1665          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
1666                  global_pid = true;                  global_pid = true;
1667          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1668          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1669  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1670          if (global_pid)          if (global_pid)
1671                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1672          else          else
1673                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1674  #else  #else
1675          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1676  #endif  #endif
# Line 1878  static void ccs_read_pid(struct ccs_io_b Line 1678  static void ccs_read_pid(struct ccs_io_b
1678                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1679                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1680          }          }
1681          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1682          if (!domain)          if (!domain)
1683                  return;                  return;
1684          if (!task_info)          if (!task_info) {
1685                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1686                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1687          else          } else {
1688                  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,  
1689                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1690                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1691                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1692                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER));
1693                                (u8) (ccs_flags >> 24),          }
                               (u8) (ccs_flags >> 16),  
                               (u8) (ccs_flags >> 8));  
1694  }  }
1695    
1696    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1697            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1698            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1699            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1700            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1701            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1702    };
1703    
1704    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1705            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1706            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1707            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1708    };
1709    
1710  /**  /**
1711   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1712   *   *
1713   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1714   *   *
1715   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1716   */   */
1717  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1718  {  {
1719          char *data = head->write_buf;          char *data = head->write_buf;
1720          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1721          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1722                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1723          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1724                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1725          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1726                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1727                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1728          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1729                  return ccs_write_domain_initializer_policy(data, true,          };
1730                                                             is_delete);          for (i = 0; i < 3; i++)
1731          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1732                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1733          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1734                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1735          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1736                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1737          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1738                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1739          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1740                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1741          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  unsigned int group;
1742                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1743          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1744                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1745          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                          if (data)
1746                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1747          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1748                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1749                    }
1750            }
1751          return -EINVAL;          return -EINVAL;
1752  }  }
1753    
1754  /**  /**
1755   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1756   *   *
1757   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1758   *   * @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".  
1759   *   *
1760   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1761     *
1762     * Caller holds ccs_read_lock().
1763   */   */
1764  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1765  {  {
1766          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1767          char *arg_ptr = ee->tmp;                  struct ccs_group *group =
1768          int arg_len = 0;                          list_entry(head->r.group, typeof(*group), head.list);
1769          unsigned long pos = bprm->p;                  list_for_each_cookie(head->r.acl, &group->member_list) {
1770          int offset = pos % PAGE_SIZE;                          struct ccs_acl_head *ptr =
1771          bool done = false;                                  list_entry(head->r.acl, typeof(*ptr), list);
1772          if (!bprm->argc)                          if (ptr->is_deleted)
1773                  goto out;                                  continue;
1774          while (1) {                          if (!ccs_flush(head))
1775                  if (!ccs_dump_page(bprm, pos, &ee->dump))                                  return false;
1776                          goto out;                          ccs_set_string(head, ccs_group_name[idx]);
1777                  pos += PAGE_SIZE - offset;                          ccs_set_string(head, group->group_name->name);
1778                  /* Read. */                          if (idx == CCS_PATH_GROUP) {
1779                  while (offset < PAGE_SIZE) {                                  ccs_set_space(head);
1780                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, container_of
1781                          const unsigned char c = kaddr[offset++];                                                 (ptr, struct ccs_path_group,
1782                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                  head)->member_name->name);
1783                                  if (c == '\\') {                          } else if (idx == CCS_NUMBER_GROUP) {
1784                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_number_union(head, &container_of
1785                                          arg_ptr[arg_len++] = '\\';                                                         (ptr, struct ccs_number_group,
1786                                  } else if (c > ' ' && c < 127) {                                                          head)->number);
1787                                          arg_ptr[arg_len++] = c;                          } else if (idx == CCS_ADDRESS_GROUP) {
1788                                  } else {                                  char buffer[128];
1789                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_address_group *member =
1790                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(ptr, typeof(*member),
1791                                          arg_ptr[arg_len++]                                                       head);
1792                                                  = ((c >> 3) & 7) + '0';                                  if (member->is_ipv6)
1793                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          ccs_print_ipv6(buffer, sizeof(buffer),
1794                                  }                                                         member->min.ipv6,
1795                          } else {                                                         member->max.ipv6);
1796                                  arg_ptr[arg_len] = '\0';                                  else
1797                                  done = true;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1798                                  break;                                                         member->min.ipv4,
1799                                                           member->max.ipv4);
1800                                    ccs_io_printf(head, " %s", buffer);
1801                          }                          }
1802                            ccs_set_lf(head);
1803                  }                  }
1804                  offset = 0;                  head->r.acl = NULL;
                 if (done)  
                         break;  
1805          }          }
1806            head->r.group = NULL;
1807          return true;          return true;
  out:  
         return false;  
1808  }  }
1809    
1810  /**  /**
1811   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_policy - Read "struct ccs_..._entry" list.
1812     *
1813     * @head: Pointer to "struct ccs_io_buffer".
1814     * @idx:  Index number.
1815   *   *
1816   * @ee: Pointer to "struct ccs_execve_entry".   * Returns true on success, false otherwise.
1817   *   *
1818   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1819   */   */
1820  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)  
1821  {  {
1822          struct ccs_condition *cond;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1823          char *buf;                  struct ccs_acl_head *acl =
1824          int len = 256;                          container_of(head->r.acl, typeof(*acl), list);
1825          char *realpath = NULL;                  if (acl->is_deleted)
1826          char *argv0 = NULL;                          continue;
1827          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);                  if (!ccs_flush(head))
1828          if (profile->learning->learning_exec_realpath) {                          return false;
1829                  struct file *file = ee->bprm->file;                  switch (idx) {
1830  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)                  case CCS_ID_TRANSITION_CONTROL:
1831                  struct path path = { file->f_vfsmnt, file->f_dentry };                          {
1832                  realpath = ccs_realpath_from_path(&path);                                  struct ccs_transition_control *ptr =
1833  #else                                          container_of(acl, typeof(*ptr), head);
1834                  realpath = ccs_realpath_from_path(&file->f_path);                                  ccs_set_string(head,
1835  #endif                                                 ccs_transition_type[ptr->type]);
1836                  if (realpath)                                  ccs_set_string(head, ptr->program ?
1837                          len += strlen(realpath) + 17;                                                 ptr->program->name : "any");
1838          }                                  ccs_set_string(head, " from ");
1839          if (profile->learning->learning_exec_argv0) {                                  ccs_set_string(head, ptr->domainname ?
1840                  if (ccs_get_argv0(ee)) {                                                 ptr->domainname->name : "any");
1841                          argv0 = ee->tmp;                          }
1842                          len += strlen(argv0) + 16;                          break;
1843                    case CCS_ID_AGGREGATOR:
1844                            {
1845                                    struct ccs_aggregator *ptr =
1846                                            container_of(acl, typeof(*ptr), head);
1847                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1848                                    ccs_set_string(head, ptr->original_name->name);
1849                                    ccs_set_space(head);
1850                                    ccs_set_string(head,
1851                                                   ptr->aggregated_name->name);
1852                            }
1853                            break;
1854                    case CCS_ID_PATTERN:
1855                            {
1856                                    struct ccs_pattern *ptr =
1857                                            container_of(acl, typeof(*ptr), head);
1858                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1859                                    ccs_set_string(head, ptr->pattern->name);
1860                            }
1861                            break;
1862                    case CCS_ID_RESERVEDPORT:
1863                            {
1864                                    struct ccs_reserved *ptr =
1865                                            container_of(acl, typeof(*ptr), head);
1866                                    const u16 min_port = ptr->min_port;
1867                                    const u16 max_port = ptr->max_port;
1868                                    ccs_set_string(head,
1869                                                   CCS_KEYWORD_DENY_AUTOBIND);
1870                                    ccs_io_printf(head, "%u", min_port);
1871                                    if (min_port != max_port)
1872                                            ccs_io_printf(head, "-%u", max_port);
1873                            }
1874                            break;
1875                    default:
1876                            continue;
1877                  }                  }
1878                    ccs_set_lf(head);
1879          }          }
1880          buf = kmalloc(len, GFP_KERNEL);          head->r.acl = NULL;
1881          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;  
1882  }  }
1883    
1884  /**  /**
1885   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1886   *   *
1887   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1888   *   *
1889   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1890   */   */
1891  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1892  {  {
1893          struct ccs_condition *cond;          if (head->r.eof)
1894          char *buf;                  return;
1895          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1896          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
1897          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
1898          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
1899                  symlink = r->obj->symlink_target->name;                  return;
1900                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1901                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1902                    head->r.step++;
1903            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1904                    return;
1905            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1906                   + CCS_MAX_ACL_GROUPS * 2) {
1907                    head->r.group_index = (head->r.step - CCS_MAX_POLICY
1908                                           - CCS_MAX_GROUP) / 2;
1909                    if (!ccs_read_domain2(head,
1910                                          &ccs_acl_group[head->r.group_index],
1911                                          head->r.step & 1))
1912                            return;
1913                    head->r.step++;
1914          }          }
1915          buf = kmalloc(len, GFP_KERNEL);          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;  
1916  }  }
1917    
1918  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2170  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1922  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1922  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1923    
1924  /* Structure for query. */  /* Structure for query. */
1925  struct ccs_query_entry {  struct ccs_query {
1926          struct list_head list;          struct list_head list;
1927          char *query;          char *query;
1928          int query_len;          int query_len;
# Line 2179  struct ccs_query_entry { Line 1931  struct ccs_query_entry {
1931          int answer;          int answer;
1932  };  };
1933    
1934  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1935  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1936    
1937  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1938  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1939    
1940    static void ccs_truncate(char *str)
1941    {
1942            while (* (unsigned char *) str > (unsigned char) ' ')
1943                    str++;
1944            *str = '\0';
1945    }
1946    
1947  /**  /**
1948   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1949   *   *
1950   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1951   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1952   *   *
1953   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1954   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1955   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1956   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1957   */   */
1958  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1959  {  {
# Line 2203  int ccs_supervisor(struct ccs_request_in Line 1962  int ccs_supervisor(struct ccs_request_in
1962          int pos;          int pos;
1963          int len;          int len;
1964          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1965          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1966          bool quota_exceeded = false;          bool quota_exceeded = false;
1967          char *header;          char *header;
1968          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1969                  r->domain = ccs_current_domain();          va_start(args, fmt);
1970          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1971            va_end(args);
1972            if (r->mode == CCS_CONFIG_LEARNING) {
1973                  char *buffer;                  char *buffer;
1974                  struct ccs_condition *cond;                  char *realpath = NULL;
1975          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1976                    char *symlink = NULL;
1977                    char *handler = NULL;
1978                    const struct ccs_preference *pref;
1979                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1980                          return 0;                          return 0;
1981                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1982                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, GFP_KERNEL);  
                 if (!buffer)  
1983                          return 0;                          return 0;
1984                  va_start(args, fmt);                  pref = &ccs_profile(r->profile)->preference;
1985                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1986                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1987                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1988                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1989                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1990                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1991                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1992                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1993                          char str[] = "if task.type=execute_handler";                                  }
1994                          cond = ccs_get_condition(str);                          }
1995                  } else                          if (pref->learning_exec_realpath) {
1996                          cond = NULL;                                  realpath = strstr(header,
1997                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1998                  ccs_put_condition(cond);                                  if (realpath) {
1999                  kfree(buffer);                                          realpath += 8;
2000                  /* fall through */                                          ccs_truncate(realpath);
2001          case CCS_CONFIG_PERMISSIVE:                                  }
2002                            }
2003                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
2004                               r->param.path.operation == CCS_TYPE_SYMLINK &&
2005                               pref->learning_symlink_target) {
2006                            symlink = strstr(header, " symlink.target=\"");
2007                            if (symlink)
2008                                    ccs_truncate(symlink + 1);
2009                    }
2010                    handler = strstr(header, "type=execute_handler");
2011                    if (handler)
2012                            ccs_truncate(handler);
2013                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2014                    if (buffer) {
2015                            va_start(args, fmt);
2016                            vsnprintf(buffer, len - 1, fmt, args);
2017                            va_end(args);
2018                            if (handler || realpath || argv0 || symlink) {
2019                                    ccs_addprintf(buffer, len, " if");
2020                                    if (handler)
2021                                            ccs_addprintf(buffer, len, " task.%s",
2022                                                          handler);
2023                                    if (realpath)
2024                                            ccs_addprintf(buffer, len, " exec.%s",
2025                                                          realpath);
2026                                    if (argv0)
2027                                            ccs_addprintf(buffer, len,
2028                                                          " exec.argv[0]=%s",
2029                                                          argv0);
2030                                    if (symlink)
2031                                            ccs_addprintf(buffer, len, "%s",
2032                                                          symlink);
2033                            }
2034                            ccs_normalize_line(buffer);
2035                            ccs_write_domain2(buffer, domain, false);
2036                            kfree(buffer);
2037                    }
2038                    kfree(header);
2039                  return 0;                  return 0;
2040          }          }
2041            if (r->mode != CCS_CONFIG_ENFORCING)
2042                    return 0;
2043          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2044                  int i;                  int i;
2045                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2046                          return -EPERM;                          return -EPERM;
2047                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->preference.
2048                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2049                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2050                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2051                  }                  }
2052                  return -EPERM;                  return -EPERM;
2053          }          }
2054          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);  
2055          if (!header)          if (!header)
2056                  goto out;                  goto out;
2057          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2058          if (!ccs_query_entry)          if (!entry)
2059                  goto out;                  goto out;
2060          len = ccs_round2(len);          len = ccs_round2(len);
2061          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2062          if (!ccs_query_entry->query)          if (!entry->query)
2063                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
2064          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2065          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2066              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2067                  quota_exceeded = true;                  quota_exceeded = true;
2068          } else {          } else {
2069                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2070                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2071          }          }
2072          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2073          if (quota_exceeded)          if (quota_exceeded)
2074                  goto out;                  goto out;
2075          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2076                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2077          kfree(header);          kfree(header);
2078          header = NULL;          header = NULL;
2079          va_start(args, fmt);          va_start(args, fmt);
2080          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2081          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
2082          va_end(args);          va_end(args);
2083          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2084          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2085          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2086          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2087          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2088               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2089               ccs_query_entry->timer++) {               entry->timer++) {
2090                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2091                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2092                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2093                  if (ccs_query_entry->answer)                  if (entry->answer)
2094                          break;                          break;
2095          }          }
2096          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2097          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2098          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2099          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2100          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2101          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2102                  error = 1;                  error = CCS_RETRY_REQUEST;
2103                  r->retry++;                  r->retry++;
2104                  break;                  break;
2105          case 1:          case 1:
# Line 2318  int ccs_supervisor(struct ccs_request_in Line 2114  int ccs_supervisor(struct ccs_request_in
2114                  break;                  break;
2115          }          }
2116   out:   out:
2117          if (ccs_query_entry)          if (entry)
2118                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2119          kfree(ccs_query_entry);          kfree(entry);
2120          kfree(header);          kfree(header);
2121          return error;          return error;
2122  }  }
# Line 2343  static int ccs_poll_query(struct file *f Line 2139  static int ccs_poll_query(struct file *f
2139          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2140                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2141                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2142                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2143                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2144                          if (ptr->answer)                          if (ptr->answer)
2145                                  continue;                                  continue;
2146                          found = true;                          found = true;
# Line 2371  static void ccs_read_query(struct ccs_io Line 2167  static void ccs_read_query(struct ccs_io
2167          int pos = 0;          int pos = 0;
2168          int len = 0;          int len = 0;
2169          char *buf;          char *buf;
2170          if (head->read_avail)          if (head->r.w_pos)
2171                  return;                  return;
2172          if (head->read_buf) {          if (head->read_buf) {
2173                  kfree(head->read_buf);                  kfree(head->read_buf);
2174                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2175          }          }
2176          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2177          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2178                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2179                  if (ptr->answer)                  if (ptr->answer)
2180                          continue;                          continue;
2181                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2182                          continue;                          continue;
2183                  len = ptr->query_len;                  len = ptr->query_len;
2184                  break;                  break;
2185          }          }
2186          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2187          if (!len) {          if (!len) {
2188                  head->read_step = 0;                  head->r.query_index = 0;
2189                  return;                  return;
2190          }          }
2191          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2192          if (!buf)          if (!buf)
2193                  return;                  return;
2194          pos = 0;          pos = 0;
2195          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2196          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2197                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2198                  if (ptr->answer)                  if (ptr->answer)
2199                          continue;                          continue;
2200                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2201                          continue;                          continue;
2202                  /*