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

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 2985 by kumaneko, Thu Sep 3 08:27:32 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c revision 3912 by kumaneko, Sun Aug 22 14:15:32 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   2009/09/03   * 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 92  static const char *ccs_mac_keywords[CCS_ Line 88  static const char *ccs_mac_keywords[CCS_
88          = "file::umount",          = "file::umount",
89          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
90          = "file::pivot_root",          = "file::pivot_root",
91            [CCS_MAC_FILE_TRANSIT]
92            = "file::transit",
93          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
94          = "misc::env",          = "misc::env",
95          [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]  
96          = "network::inet_tcp_bind",          = "network::inet_tcp_bind",
97          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_NETWORK_INET_TCP_LISTEN]
98          = "network::inet_tcp_listen",          = "network::inet_tcp_listen",
99          [CCS_MAC_NETWORK_TCP_CONNECT]          [CCS_MAC_NETWORK_INET_TCP_CONNECT]
100          = "network::inet_tcp_connect",          = "network::inet_tcp_connect",
101          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_MAC_NETWORK_INET_TCP_ACCEPT]
102          = "network::inet_tcp_accept",          = "network::inet_tcp_accept",
103          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAC_NETWORK_INET_UDP_BIND]
104            = "network::inet_udp_bind",
105            [CCS_MAC_NETWORK_INET_UDP_SEND]
106            = "network::inet_udp_send",
107            [CCS_MAC_NETWORK_INET_UDP_RECV]
108            = "network::inet_udp_recv",
109            [CCS_MAC_NETWORK_INET_RAW_BIND]
110          = "network::inet_raw_bind",          = "network::inet_raw_bind",
111          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_MAC_NETWORK_INET_RAW_SEND]
112          = "network::inet_raw_connect",          = "network::inet_raw_send",
113            [CCS_MAC_NETWORK_INET_RAW_RECV]
114            = "network::inet_raw_recv",
115            [CCS_MAC_NETWORK_UNIX_STREAM_BIND]
116            = "network::unix_stream_bind",
117            [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]
118            = "network::unix_stream_listen",
119            [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]
120            = "network::unix_stream_connect",
121            [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]
122            = "network::unix_stream_accept",
123            [CCS_MAC_NETWORK_UNIX_DGRAM_BIND]
124            = "network::unix_dgram_bind",
125            [CCS_MAC_NETWORK_UNIX_DGRAM_SEND]
126            = "network::unix_dgram_send",
127            [CCS_MAC_NETWORK_UNIX_DGRAM_RECV]
128            = "network::unix_dgram_recv",
129            [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]
130            = "network::unix_seqpacket_bind",
131            [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]
132            = "network::unix_seqpacket_listen",
133            [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT]
134            = "network::unix_seqpacket_connect",
135            [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]
136            = "network::unix_seqpacket_accept",
137          [CCS_MAC_SIGNAL]          [CCS_MAC_SIGNAL]
138          = "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",  
139          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_ROUTE_SOCKET]
140          = "capability::use_route",          = "capability::use_route",
141          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
142          = "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",  
143          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
144          = "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",  
145          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
146          = "capability::SYS_VHANGUP",          = "capability::SYS_VHANGUP",
147          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
# Line 146  static const char *ccs_mac_keywords[CCS_ Line 152  static const char *ccs_mac_keywords[CCS_
152          = "capability::SYS_SETHOSTNAME",          = "capability::SYS_SETHOSTNAME",
153          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]          [CCS_MAX_MAC_INDEX + CCS_USE_KERNEL_MODULE]
154          = "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",  
155          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
156          = "capability::SYS_KEXEC_LOAD",          = "capability::SYS_KEXEC_LOAD",
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
157          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
158          = "capability::SYS_PTRACE",          = "capability::SYS_PTRACE",
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
159          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
160           + CCS_MAC_CATEGORY_FILE] = "file",           + CCS_MAC_CATEGORY_FILE] = "file",
161          [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 194  static const char *ccs_yesno(const unsig
194          return value ? "yes" : "no";          return value ? "yes" : "no";
195  }  }
196    
197    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
198    {
199            va_list args;
200            const int pos = strlen(buffer);
201            va_start(args, fmt);
202            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
203            va_end(args);
204    }
205    
206  /**  /**
207   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
208   *   *
209   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
210   *   *
211   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
212     */
213    static bool ccs_flush(struct ccs_io_buffer *head)
214    {
215            while (head->r.w_pos) {
216                    const char *w = head->r.w[0];
217                    int len = strlen(w);
218                    if (len) {
219                            if (len > head->read_user_buf_avail)
220                                    len = head->read_user_buf_avail;
221                            if (!len)
222                                    return false;
223                            if (copy_to_user(head->read_user_buf, w, len))
224                                    return false;
225                            head->read_user_buf_avail -= len;
226                            head->read_user_buf += len;
227                            w += len;
228                    }
229                    if (*w) {
230                            head->r.w[0] = w;
231                            return false;
232                    }
233                    /* Add '\0' for audit logs and query. */
234                    if (head->poll) {
235                            if (!head->read_user_buf_avail ||
236                                copy_to_user(head->read_user_buf, "", 1))
237                                    return false;
238                            head->read_user_buf_avail--;
239                            head->read_user_buf++;
240                    }
241                    head->r.w_pos--;
242                    for (len = 0; len < head->r.w_pos; len++)
243                            head->r.w[len] = head->r.w[len + 1];
244            }
245            head->r.avail = 0;
246            return true;
247    }
248    
249    /**
250     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
251     *
252     * @head:   Pointer to "struct ccs_io_buffer".
253     * @string: String to print.
254   *   *
255   * The snprintf() will truncate, but ccs_io_printf() won't.   * Note that @string has to be kept valid until @head is kfree()d.
256     * This means that char[] allocated on stack memory cannot be passed to
257     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
258   */   */
259  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
260    {
261            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
262                    head->r.w[head->r.w_pos++] = string;
263                    ccs_flush(head);
264            } else
265                    printk(KERN_WARNING "Too many words in a line.\n");
266    }
267    
268    /**
269     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
270     *
271     * @head: Pointer to "struct ccs_io_buffer".
272     * @fmt:  The printf()'s format string, followed by parameters.
273     */
274    void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
275  {  {
276          va_list args;          va_list args;
277          int len;          int len;
278          int pos = head->read_avail;          int pos = head->r.avail;
279          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
280          if (size <= 0)          if (size <= 0)
281                  return false;                  return;
282          va_start(args, fmt);          va_start(args, fmt);
283          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
284          va_end(args);          va_end(args);
285          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
286                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
287          head->read_avail += len;                  return;
288          return true;          }
289            head->r.avail += len;
290            ccs_set_string(head, head->read_buf + pos);
291    }
292    
293    static void ccs_set_space(struct ccs_io_buffer *head)
294    {
295            ccs_set_string(head, " ");
296    }
297    
298    static bool ccs_set_lf(struct ccs_io_buffer *head)
299    {
300            ccs_set_string(head, "\n");
301            return !head->r.w_pos;
302  }  }
303    
304  /**  /**
305   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
306   *   *
307   * @profile: Profile number to create.   * @profile: Profile number to create.
308   *   *
309   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
310   */   */
311  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)  
312  {  {
313          struct ccs_profile *ptr;          struct ccs_profile *ptr;
314          struct ccs_profile *entry;          struct ccs_profile *entry;
# Line 258  static struct ccs_profile *ccs_find_or_a Line 317  static struct ccs_profile *ccs_find_or_a
317          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
318          if (ptr)          if (ptr)
319                  return ptr;                  return ptr;
320          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
321          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
322                    goto out;
323          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
324          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
325                  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;  
326                  ptr->default_config = CCS_CONFIG_DISABLED |                  ptr->default_config = CCS_CONFIG_DISABLED |
327                            CCS_CONFIG_VERBOSE |
328                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;                          CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
329                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,                  memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
330                         sizeof(ptr->config));                         sizeof(ptr->config));
# Line 278  static struct ccs_profile *ccs_find_or_a Line 333  static struct ccs_profile *ccs_find_or_a
333                  entry = NULL;                  entry = NULL;
334          }          }
335          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
336     out:
337          kfree(entry);          kfree(entry);
338          return ptr;          return ptr;
339  }  }
340    
341  /**  /**
342     * ccs_check_profile - Check all profiles currently assigned to domains are defined.
343     */
344    static void ccs_check_profile(void)
345    {
346            struct ccs_domain_info *domain;
347            const int idx = ccs_read_lock();
348            ccs_policy_loaded = true;
349            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
350                    const u8 profile = domain->profile;
351                    if (ccs_profile_ptr[profile])
352                            continue;
353                    panic("Profile %u (used by '%s') not defined.\n",
354                          profile, domain->domainname->name);
355            }
356            ccs_read_unlock(idx);
357            if (ccs_profile_version != 20090903)
358                    panic("Profile version %u is not supported.\n",
359                          ccs_profile_version);
360            printk(KERN_INFO "CCSecurity: 1.8.0-pre   2010/08/01\n");
361            printk(KERN_INFO "Mandatory Access Control activated.\n");
362    }
363    
364    /**
365   * ccs_profile - Find a profile.   * ccs_profile - Find a profile.
366   *   *
367   * @profile: Profile number to find.   * @profile: Profile number to find.
368   *   *
369   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile".
370   */   */
371  struct ccs_profile *ccs_profile(const u8 profile)  struct ccs_profile *ccs_profile(const u8 profile)
372  {  {
# Line 298  struct ccs_profile *ccs_profile(const u8 Line 377  struct ccs_profile *ccs_profile(const u8
377          return ptr;          return ptr;
378  }  }
379    
380  /**  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)  
381  {  {
382          char *data = head->write_buf;          const char *cp = strstr(string, find);
383          unsigned int i;          if (cp) {
384          int value;                  cp += strlen(find);
385          int mode;                  if (!strncmp(cp, "=yes", 4))
386          u8 config;                          return 1;
387          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;  
388                          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;  
389          }          }
390  #endif          return -1;
391          if (!strcmp(data, "PREFERENCE::enforcing")) {  }
392                  char *cp2;  
393                  if (use_default) {  static void ccs_set_bool(bool *b, const char *string, const char *find)
394                          profile->enforcing = &ccs_default_profile.preference;  {
395                          return 0;          switch (ccs_find_yesno(string, find)) {
396                  }          case 1:
397                  profile->enforcing = &profile->preference;                  *b = true;
398                  if (value >= 0)                  break;
399                          profile->preference.enforcing_verbose = value;          case 0:
400                  cp2 = strstr(cp, "penalty=");                  *b = false;
401                  if (cp2)                  break;
                         sscanf(cp2 + 8, "%u",  
                                &profile->preference.enforcing_penalty);  
                 return 0;  
402          }          }
403          if (!strcmp(data, "PREFERENCE::permissive")) {  }
404                  if (use_default) {  
405                          profile->permissive = &ccs_default_profile.preference;  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
406                          return 0;  {
407                  }          const char *cp = strstr(string, find);
408                  profile->permissive = &profile->preference;          if (cp)
409                  if (value >= 0)                  sscanf(cp + strlen(find), "=%u", i);
410                          profile->preference.permissive_verbose = value;  }
411                  return 0;  
412    static void ccs_set_pref(const char *name, const char *value,
413                             struct ccs_profile *profile)
414    {
415            if (!strcmp(name, "audit")) {
416    #ifdef CONFIG_CCSECURITY_AUDIT
417                    ccs_set_uint(&profile->preference.audit_max_grant_log, value,
418                                 "max_grant_log");
419                    ccs_set_uint(&profile->preference.audit_max_reject_log, value,
420                                 "max_reject_log");
421    #endif
422                    ccs_set_bool(&profile->preference.audit_task_info, value,
423                                 "task_info");
424                    ccs_set_bool(&profile->preference.audit_path_info, value,
425                                 "path_info");
426                    return;
427          }          }
428          if (!strcmp(data, "PREFERENCE::learning")) {          if (!strcmp(name, "enforcing")) {
429                  char *cp2;                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
430                  if (use_default) {                               "penalty");
431                          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;  
432          }          }
433          if (profile == &ccs_default_profile)          if (!strcmp(name, "learning")) {
434                  return -EINVAL;                  ccs_set_uint(&profile->preference.learning_max_entry, value,
435          if (!strcmp(data, "COMMENT")) {                               "max_entry");
436                  const struct ccs_path_info *new_comment = ccs_get_name(cp);                  ccs_set_bool(&profile->preference.learning_exec_realpath,
437                  const struct ccs_path_info *old_comment;                               value, "exec.realpath");
438                  /* Protect reader from ccs_put_name(). */                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
439                  spin_lock(&ccs_profile_comment_lock);                               "exec.argv0");
440                  old_comment = profile->comment;                  ccs_set_bool(&profile->preference.learning_symlink_target,
441                  profile->comment = new_comment;                               value, "symlink.target");
442                  spin_unlock(&ccs_profile_comment_lock);                  return;
                 ccs_put_name(old_comment);  
                 return 0;  
443          }          }
444          if (!strcmp(data, "CONFIG")) {  }
445    
446    static int ccs_set_mode(char *name, const char *value,
447                            struct ccs_profile *profile)
448    {
449            u8 i;
450            u8 config;
451            if (!strcmp(name, "CONFIG")) {
452                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
453                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
454                  config = profile->default_config;                  config = profile->default_config;
455          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
456                  config = 0;                  config = 0;
457                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
458                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
459                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
460                                  continue;                                  continue;
461                          config = profile->config[i];                          config = profile->config[i];
462                          break;                          break;
# Line 441  static int ccs_write_profile(struct ccs_ Line 467  static int ccs_write_profile(struct ccs_
467          } else {          } else {
468                  return -EINVAL;                  return -EINVAL;
469          }          }
470          if (use_default) {          if (strstr(value, "use_default")) {
471                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
472          } else {          } else {
473                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
474                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
475                            if (strstr(value, ccs_mode[mode]))
476                                  /*                                  /*
477                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
478                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
479                                   */                                   */
480                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
 #ifdef CONFIG_CCSECURITY_AUDIT  
481                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
482                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "verbose")) {
483                            case 1:
484                                    config |= CCS_CONFIG_VERBOSE;
485                                    break;
486                            case 0:
487                                    config &= ~CCS_CONFIG_VERBOSE;
488                                    break;
489                            }
490    #ifdef CONFIG_CCSECURITY_AUDIT
491                            switch (ccs_find_yesno(value, "grant_log")) {
492                            case 1:
493                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
494                          else if (strstr(cp, "grant_log=no"))                                  break;
495                            case 0:
496                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
497                          if (strstr(cp, "reject_log=yes"))                                  break;
498                            }
499                            switch (ccs_find_yesno(value, "reject_log")) {
500                            case 1:
501                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
502                          else if (strstr(cp, "reject_log=no"))                                  break;
503                            case 0:
504                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
505                  }                                  break;
506                            }
507  #endif  #endif
508                    }
509          }          }
510          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          if (i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
511              + CCS_MAX_MAC_CATEGORY_INDEX)              + CCS_MAX_MAC_CATEGORY_INDEX)
# Line 473  static int ccs_write_profile(struct ccs_ Line 516  static int ccs_write_profile(struct ccs_
516  }  }
517    
518  /**  /**
519   * ccs_read_profile - Read profile table.   * ccs_write_profile - Write profile table.
520   *   *
521   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
522     *
523     * Returns 0 on success, negative value otherwise.
524   */   */
525  static void ccs_read_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
526  {  {
527          int index;          char *data = head->write_buf;
528          if (head->read_eof)          unsigned int i;
529                  return;          char *cp;
530          if (head->read_bit)          struct ccs_profile *profile;
531                  goto body;          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
532          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");                  return 0;
533            i = simple_strtoul(data, &cp, 10);
534            if (*cp != '-')
535                    return -EINVAL;
536            data = cp + 1;
537            profile = ccs_assign_profile(i);
538            if (!profile)
539                    return -EINVAL;
540            cp = strchr(data, '=');
541            if (!cp)
542                    return -EINVAL;
543            *cp++ = '\0';
544            if (ccs_str_starts(&data, "PREFERENCE::")) {
545                    ccs_set_pref(data, cp, profile);
546                    return 0;
547            }
548            if (!strcmp(data, "COMMENT")) {
549                    const struct ccs_path_info *old_comment = profile->comment;
550                    profile->comment = ccs_get_name(cp);
551                    ccs_put_name(old_comment);
552                    return 0;
553            }
554            return ccs_set_mode(data, cp, profile);
555    }
556    
557    static void ccs_print_preference(struct ccs_io_buffer *head, const int index)
558    {
559            struct ccs_profile *profile = ccs_profile_ptr[index];
560            struct ccs_preference *pref = &profile->preference;
561            ccs_io_printf(head, "%u-PREFERENCE::%s={ "
562  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
563          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);  
564  #endif  #endif
565          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "                        "task_info=%s path_info=%s }\n", index,
566                        "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;  
567  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
568                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "                        pref->audit_max_grant_log,
569                                     "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;  
570  #endif  #endif
571                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                        ccs_yesno(pref->audit_task_info),
572                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                        ccs_yesno(pref->audit_path_info));
573            ccs_io_printf(head, "%u-PREFERENCE::%s={ "
574                          "max_entry=%u exec.realpath=%s "
575                          "exec.argv0=%s symlink.target=%s }\n",
576                          index, "learning",
577                          pref->learning_max_entry,
578                          ccs_yesno(pref->learning_exec_realpath),
579                          ccs_yesno(pref->learning_exec_argv0),
580                          ccs_yesno(pref->learning_symlink_target));
581            ccs_io_printf(head, "%u-PREFERENCE::%s={ penalty=%u }\n", index,
582                          "enforcing", pref->enforcing_penalty);
583    }
584    
585    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
586    {
587            ccs_io_printf(head, "={ mode=%s verbose=%s", ccs_mode[config & 3],
588                          ccs_yesno(config & CCS_CONFIG_VERBOSE));
589  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
590                          const char *g;          ccs_io_printf(head, " grant_log=%s reject_log=%s",
591                          const char *r;                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
592                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
593  #endif  #endif
594                          config = profile->config[i];          ccs_set_string(head, " }\n");
595    }
596    
597    /**
598     * ccs_read_profile - Read profile table.
599     *
600     * @head: Pointer to "struct ccs_io_buffer".
601     */
602    static void ccs_read_profile(struct ccs_io_buffer *head)
603    {
604            u8 index;
605            const struct ccs_profile *profile;
606     next:
607            index = head->r.index;
608            profile = ccs_profile_ptr[index];
609            switch (head->r.step) {
610            case 0:
611                    ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
612                    head->r.step++;
613                    break;
614            case 1:
615                    for ( ; head->r.index < CCS_MAX_PROFILES;
616                          head->r.index++)
617                            if (ccs_profile_ptr[head->r.index])
618                                    break;
619                    if (head->r.index == CCS_MAX_PROFILES)
620                            return;
621                    head->r.step++;
622                    break;
623            case 2:
624                    {
625                            const struct ccs_path_info *comment = profile->comment;
626                            ccs_io_printf(head, "%u-COMMENT=", index);
627                            ccs_set_string(head, comment ? comment->name : "");
628                            ccs_set_lf(head);
629                            head->r.step++;
630                    }
631                    break;
632            case 3:
633                    {
634                            ccs_io_printf(head, "%u-%s", index, "CONFIG");
635                            ccs_print_config(head, profile->default_config);
636                            head->r.bit = 0;
637                            head->r.step++;
638                    }
639                    break;
640            case 4:
641                    for ( ; head->r.bit < CCS_MAX_MAC_INDEX
642                                  + CCS_MAX_CAPABILITY_INDEX
643                                  + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
644                            const u8 i = head->r.bit;
645                            const u8 config = profile->config[i];
646                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
647                                  continue;                                  continue;
648  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
649                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                        ccs_mac_keywords[i]);
650                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          ccs_print_config(head, config);
651                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                          head->r.bit++;
652                                             "grant_log=%s reject_log=%s }\n",                          break;
653                                             index, ccs_mac_keywords[i],                  }
654                                             ccs_mode_4[config & 3], g, r))                  if (head->r.bit == CCS_MAX_MAC_INDEX
655                                  goto out;                      + CCS_MAX_CAPABILITY_INDEX
656  #else                      + CCS_MAX_MAC_CATEGORY_INDEX) {
657                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          ccs_print_preference(head, index);
658                                             index, ccs_mac_keywords[i],                          head->r.index++;
659                                             ccs_mode_4[config & 3]))                          head->r.step = 1;
                                 goto out;  
 #endif  
660                  }                  }
 #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;  
661                  break;                  break;
662          }          }
663          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
664                  head->read_eof = true;                  goto next;
665  }  }
666    
667  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager(const struct ccs_acl_head *a,
668  LIST_HEAD(ccs_policy_manager_list);                               const struct ccs_acl_head *b)
669    {
670            return container_of(a, struct ccs_manager, head)->manager
671                    == container_of(b, struct ccs_manager, head)->manager;
672    }
673    
674  /**  /**
675   * 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 681  LIST_HEAD(ccs_policy_manager_list);
681   */   */
682  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)
683  {  {
684          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
685          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
686          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
687                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
688                          return -EINVAL;                          return -EINVAL;
689                  e.is_domain = true;                  e.is_domain = true;
690          } else {          } else {
691                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
692                          return -EINVAL;                          return -EINVAL;
693          }          }
694          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
695          if (!e.manager)          if (!e.manager)
696                  return -ENOMEM;                  return error;
697          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
698                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
699          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);  
700          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
701          return error;          return error;
702  }  }
703    
704  /**  /**
705   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
706   *   *
707   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
708   *   *
709   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
710   */   */
711  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
712  {  {
713          char *data = head->write_buf;          char *data = head->write_buf;
714          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 720  static int ccs_write_manager_policy(stru
720  }  }
721    
722  /**  /**
723   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
724   *   *
725   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
726   *   *
727   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
728   */   */
729  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
730  {  {
731          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
732                  return;                  return;
733          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]) {
734                  struct ccs_policy_manager_entry *ptr;                  struct ccs_manager *ptr =
735                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                          list_entry(head->r.acl, typeof(*ptr), head.list);
736                  if (ptr->is_deleted)                  if (ptr->head.is_deleted)
737                          continue;                          continue;
738                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
739                          return;                          return;
740                    ccs_set_string(head, ptr->manager->name);
741                    ccs_set_lf(head);
742          }          }
743          head->read_eof = true;          head->r.eof = true;
744  }  }
745    
746  /**  /**
747   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
748   *   *
749   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
750   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
751   *   *
752   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
753   */   */
754  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
755  {  {
756          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
757          const char *exe;          const char *exe;
758          struct task_struct *task = current;          struct task_struct *task = current;
759          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
# Line 722  static bool ccs_is_policy_manager(void) Line 761  static bool ccs_is_policy_manager(void)
761          bool found = false;          bool found = false;
762          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
763                  return true;                  return true;
764          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
765                  return true;                  return true;
766          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
767                  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;  
                 }  
         }  
768          exe = ccs_get_exe();          exe = ccs_get_exe();
769          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
770                  return false;                                  head.list) {
771          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
772                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
773                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
774                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
775                          /* Set manager flag. */                                  continue;
776                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
777                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
778                                    continue;
779                  }                  }
780                    /* Set manager flag. */
781                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
782                    found = true;
783                    break;
784          }          }
785          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
786                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 770  static bool ccs_is_policy_manager(void) Line 806  static bool ccs_is_policy_manager(void)
806  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
807  {  {
808          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
809          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
810                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
811                  if (cp)          if (cp)
812                          *cp++ = '\0';                  *cp++ = '\0';
         }  
813          return cp;          return cp;
814  }  }
815    
816  /**  /**
817   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
818   *   *
819   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
820   * @data: String to parse.   * @data: String to parse.
# Line 796  static char *ccs_find_condition_part(cha Line 823  static char *ccs_find_condition_part(cha
823   *   *
824   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
825   */   */
826  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)
827  {  {
828          unsigned int pid;          unsigned int pid;
829          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
830          bool global_pid = false;          bool global_pid = false;
831          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
832                  head->read_execute_only = true;                  head->r.print_execute_only = true;
833                  return true;                  return true;
834          }          }
835          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
836              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
837                  struct task_struct *p;                  struct task_struct *p;
838                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
839  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
840                  if (global_pid)                  if (global_pid)
841                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
842                                                                   &init_pid_ns);
843                  else                  else
844                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
845  #else  #else
846                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
847  #endif  #endif
848                  if (p)                  if (p)
849                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
850                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
851          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
852                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
853                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
854          } else          } else
855                  return false;                  return false;
856          head->write_var1 = domain;          head->w.domain = domain;
857          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
858          if (!head->read_buf)          if (!head->read_buf)
859                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
860          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
861            head->r.print_this_domain_only = true;
862            if (domain)
863                    head->r.domain = &domain->list;
864            else
865                    head->r.eof = true;
866          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
867          head->read_single_domain = true;          if (domain && domain->is_deleted)
868          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");  
         }  
869          return true;          return true;
870  }  }
871    
872  static int ccs_write_domain_policy2(char *data, struct ccs_domain_info *domain,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
873                                      struct ccs_condition *cond,                               const bool is_delete)
874                                      const bool is_delete)  {
875  {          static const struct {
876          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))                  const char *keyword;
877                  return ccs_write_capability_policy(data, domain, cond,                  int (*write) (char *, struct ccs_domain_info *,
878                                                     is_delete);                                struct ccs_condition *, const bool);
879          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))          } ccs_callback[5] = {
880                  return ccs_write_network_policy(data, domain, cond, is_delete);                  { "network inet ", ccs_write_inet_network },
881          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                  { "network unix ", ccs_write_unix_network },
882                  return ccs_write_signal_policy(data, domain, cond, is_delete);                  { "misc ", ccs_write_misc },
883          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { "capability ", ccs_write_capability },
884                  return ccs_write_env_policy(data, domain, cond, is_delete);                  { "ipc ", ccs_write_ipc },
885          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          };
886                  return ccs_write_mount_policy(data, domain, cond, is_delete);          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
887          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_UNMOUNT))                        const bool) = ccs_write_file;
888                  return ccs_write_umount_policy(data, domain, cond, is_delete);          int error;
889          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CHROOT))          u8 i;
890                  return ccs_write_chroot_policy(data, domain, cond, is_delete);          struct ccs_condition *cond = NULL;
891          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_PIVOT_ROOT))          char *cp = ccs_find_condition_part(data);
892                  return ccs_write_pivot_root_policy(data, domain, cond,          if (cp) {
893                                                     is_delete);                  cond = ccs_get_condition(cp);
894          return ccs_write_file_policy(data, domain, cond, is_delete);                  if (!cond)
895                            return -EINVAL;
896            }
897            for (i = 0; i < 5; i++) {
898                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
899                            continue;
900                    write = ccs_callback[i].write;
901                    break;
902            }
903            error = write(data, domain, cond, is_delete);
904            if (cond)
905                    ccs_put_condition(cond);
906            return error;
907  }  }
908    
909    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
910            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
911            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
912    };
913            
914  /**  /**
915   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
916   *   *
917   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
918   *   *
919   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
920   */   */
921  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
922  {  {
923          char *data = head->write_buf;          char *data = head->write_buf;
924          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
925          bool is_delete = false;          bool is_delete = false;
926          bool is_select = false;          bool is_select = false;
927          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
928          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
929                  is_delete = true;                  is_delete = true;
930          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
931                  is_select = true;                  is_select = true;
932          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
933                  return 0;                  return 0;
934          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
935          if (!ccs_is_policy_manager())          if (!ccs_manager())
936                  return -EPERM;                  return -EPERM;
937          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
938                  domain = NULL;                  domain = NULL;
939                  if (is_delete)                  if (is_delete)
940                          ccs_delete_domain(data);                          ccs_delete_domain(data);
941                  else if (is_select)                  else if (is_select)
942                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
943                  else                  else
944                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0, false);
945                  head->write_var1 = domain;                  head->w.domain = domain;
946                  return 0;                  return 0;
947          }          }
948          if (!domain)          if (!domain)
# Line 917  static int ccs_write_domain_policy(struc Line 950  static int ccs_write_domain_policy(struc
950    
951          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1          if (sscanf(data, CCS_KEYWORD_USE_PROFILE "%u", &profile) == 1
952              && profile < CCS_MAX_PROFILES) {              && profile < CCS_MAX_PROFILES) {
953                  if (ccs_profile(profile))                  if (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile])
954                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
955                  return 0;                  return 0;
956          }          }
957          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
958                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
959                    domain->group = (u8) profile;
960                  return 0;                  return 0;
961          }          }
962          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
963                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
964                    if (strncmp(data, cp, strlen(cp) - 1))
965                            continue;
966                    domain->flags[profile] = !is_delete;
967                  return 0;                  return 0;
968          }          }
969          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;  
970  }  }
971    
972  /**  /**
# Line 946  static int ccs_write_domain_policy(struc Line 974  static int ccs_write_domain_policy(struc
974   *   *
975   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
976   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
977   */   */
978  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
979                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
980  {  {
981          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
982          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
983                  head->read_avail--;                  ccs_set_space(head);
984          if (ptr->is_group)          if (ptr->is_group) {
985                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
986                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
987          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
988  }                  if (cond)
989                            ccs_set_string(head, "\"");
990  /**                  ccs_set_string(head, ptr->filename->name);
991   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
992   *                          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);  
993          }          }
994  }  }
995    
996  /**  /**
997   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
998   *   *
999   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1000   * @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.  
1001   */   */
1002  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1003                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1004  {  {
1005          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
1006                    ccs_set_space(head);
1007            if (ptr->is_group) {
1008                    ccs_set_string(head, "@");
1009                    ccs_set_string(head, ptr->group->group_name->name);
1010            } else {
1011                    int i;
1012                    unsigned long min = ptr->values[0];
1013                    const unsigned long max = ptr->values[1];
1014                    u8 min_type = ptr->value_type[0];
1015                    const u8 max_type = ptr->value_type[1];
1016                    char buffer[128];
1017                    buffer[0] = '\0';
1018                    for (i = 0; i < 2; i++) {
1019                            switch (min_type) {
1020                            case CCS_VALUE_TYPE_HEXADECIMAL:
1021                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1022                                                  min);
1023                                    break;
1024                            case CCS_VALUE_TYPE_OCTAL:
1025                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1026                                                  min);
1027                                    break;
1028                            default:
1029                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1030                                                  min);
1031                                    break;
1032                            }
1033                            if (min == max && min_type == max_type)
1034                                    break;
1035                            ccs_addprintf(buffer, sizeof(buffer), "-");
1036                            min_type = max_type;
1037                            min = max;
1038                    }
1039                    ccs_io_printf(head, "%s", buffer);
1040            }
1041  }  }
1042    
1043  /**  /**
1044   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1045   *   *
1046   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1047   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1048   *   *
1049   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1050   */   */
1051  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1052                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1053  {  {
1054          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1055          const struct ccs_number_union *numbers_p;          case 0:
1056          const struct ccs_name_union *names_p;                  {
1057          const struct ccs_argv_entry *argv;                          if (cond->condc)
1058          const struct ccs_envp_entry *envp;                                  ccs_set_string(head, " if");
1059          u16 condc;                          head->r.cond_index = 0;
1060          u16 i;                          head->r.cond_step++;
1061          u16 j;                  }
1062          char buffer[32];                  /* fall through */
1063          if (!cond)          case 1:
1064                  goto no_condition;                  {
1065          condc = cond->condc;                          const u16 condc = cond->condc;
1066          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_condition_element *condp =
1067          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(condp)) (cond + 1);
1068          names_p = (const struct ccs_name_union *)                          const struct ccs_number_union *numbers_p =
1069                  (numbers_p + cond->numbers_count);                                  (typeof(numbers_p)) (condp + condc);
1070          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                          const struct ccs_name_union *names_p =
1071          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                                  (typeof(names_p))
1072          memset(buffer, 0, sizeof(buffer));                                  (numbers_p + cond->numbers_count);
1073          if (condc && !ccs_io_printf(head, "%s", " if"))                          const struct ccs_argv *argv =
1074                  goto out;                                  (typeof(argv)) (names_p + cond->names_count);
1075          for (i = 0; i < condc; i++) {                          const struct ccs_envp *envp =
1076                  const u8 match = condp->equals;                                  (typeof(envp)) (argv + cond->argc);
1077                  const u8 left = condp->left;                          u16 skip;
1078                  const u8 right = condp->right;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1079                  condp++;                                  const u8 left = condp->left;
1080                  switch (left) {                                  const u8 right = condp->right;
1081                  case CCS_ARGV_ENTRY:                                  condp++;
1082                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  switch (left) {
1083                                             argv->index, argv->is_not ?                                  case CCS_ARGV_ENTRY:
1084                                             "!=" : "=", argv->value->name))                                          argv++;
1085                                  goto out;                                          continue;
1086                          argv++;                                  case CCS_ENVP_ENTRY:
1087                          continue;                                          envp++;
1088                  case CCS_ENVP_ENTRY:                                          continue;
1089                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  case CCS_NUMBER_UNION:
1090                                             envp->name->name, envp->is_not ?                                          numbers_p++;
1091                                             "!=" : "="))                                          break;
1092                                  goto out;                                  }
1093                          if (envp->value) {                                  switch (right) {
1094                                  if (!ccs_io_printf(head, "\"%s\"",                                  case CCS_NAME_UNION:
1095                                                     envp->value->name))                                          names_p++;
1096                                          goto out;                                          break;
1097                          } else {                                  case CCS_NUMBER_UNION:
1098                                  if (!ccs_io_printf(head, "NULL"))                                          numbers_p++;
1099                                          goto out;                                          break;
1100                                    }
1101                            }
1102                            while (head->r.cond_index < condc) {
1103                                    const u8 match = condp->equals;
1104                                    const u8 left = condp->left;
1105                                    const u8 right = condp->right;
1106                                    if (!ccs_flush(head))
1107                                            return false;
1108                                    condp++;
1109                                    head->r.cond_index++;
1110                                    ccs_set_space(head);
1111                                    switch (left) {
1112                                    case CCS_ARGV_ENTRY:
1113                                            ccs_io_printf(head,
1114                                                          "exec.argv[%u]%s\"%s\"",
1115                                                          argv->index,
1116                                                          argv->is_not ?
1117                                                          "!=" : "=",
1118                                                          argv->value->name);
1119                                            argv++;
1120                                            continue;
1121                                    case CCS_ENVP_ENTRY:
1122                                            ccs_io_printf(head,
1123                                                          "exec.envp[\"%s\"]%s",
1124                                                          envp->name->name,
1125                                                          envp->is_not ?
1126                                                          "!=" : "=");
1127                                            if (envp->value) {
1128                                                    ccs_set_string(head, "\"");
1129                                                    ccs_set_string(head, envp->
1130                                                                   value->name);
1131                                                    ccs_set_string(head, "\"");
1132                                            } else {
1133                                                    ccs_set_string(head, "NULL");
1134                                            }
1135                                            envp++;
1136                                            continue;
1137                                    case CCS_NUMBER_UNION:
1138                                            ccs_print_number_union(head,
1139                                                                   numbers_p++);
1140                                            break;
1141                                    default:
1142                                            ccs_set_string(head,
1143                                                   ccs_condition_keyword[left]);
1144                                            break;
1145                                    }
1146                                    ccs_set_string(head, match ? "=" : "!=");
1147                                    switch (right) {
1148                                    case CCS_NAME_UNION:
1149                                            ccs_print_name_union(head, names_p++);
1150                                            break;
1151                                    case CCS_NUMBER_UNION:
1152                                            ccs_print_number_union(head,
1153                                                                   numbers_p++);
1154                                            break;
1155                                    default:
1156                                            ccs_set_string(head,
1157                                                   ccs_condition_keyword[right]);
1158                                            break;
1159                                    }
1160                          }                          }
                         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;  
1161                  }                  }
1162                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1163                          goto out;                  /* fall through */
1164                  switch (right) {          case 2:
1165                  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;  
1166                          break;                          break;
1167                    head->r.cond_step++;
1168                    /* fall through */
1169            case 3:
1170                    {
1171                            u8 j;
1172                            const u8 i = cond->post_state[3];
1173                            if (i)
1174                                    ccs_set_string(head, " ; set");
1175                            for (j = 0; j < 3; j++)
1176                                    if ((i & (1 << j)))
1177                                            ccs_io_printf(head,
1178                                                          " task.state[%u]=%u", j,
1179                                                          cond->post_state[j]);
1180                            if (i & (1 << 4))
1181                                    ccs_io_printf(head, " audit=%s",
1182                                                  ccs_yesno(cond->post_state[4]));
1183                  }                  }
1184          }                  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"))  
1185                  return true;                  return true;
1186   out:          }
1187          return false;          return false;
1188  }  }
1189    
1190  /**  /**
1191   * ccs_print_path_acl - Print a single path ACL entry.   * ccs_fns - Find next set bit.
1192   *   *
1193   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1194   * @ptr:  Pointer to "struct ccs_path_acl".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1195   *   *
1196   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1197   */   */
1198  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
1199  {  {
1200          int pos;          for ( ; bit < 8; bit++)
1201          u8 bit;                  if (perm & (1 << bit))
1202          const u16 perm = ptr->perm;                          break;
1203          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {          return 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;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1204  }  }
1205    
1206  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_path_number3_acl - Print a path_number3 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_number3_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  
                                        struct ccs_path_number3_acl *ptr,  
                                        const struct ccs_condition *cond)  
1207  {  {
1208          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1209          u8 bit;                  ccs_io_printf(head, "acl_group %u ", head->r.group_index);
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;  
              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;  
1210  }  }
1211    
1212  /**  /**
1213   * ccs_print_path2_acl - Print a path2 ACL entry.   * ccs_print_entry - Print an 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)  
 {  
         int pos;  
         u8 bit;  
         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;  
 }  
   
 /**  
  * ccs_print_path_number_acl - Print a path_number ACL entry.  
1214   *   *
1215   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1216   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1217   *   *
1218   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1219   */   */
1220  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1221                                        struct ccs_path_number_acl *ptr,                              const struct ccs_acl_info *acl)
                                       const struct ccs_condition *cond)  
1222  {  {
1223          int pos;          const u8 acl_type = acl->type;
1224          u8 bit;          u8 bit;
1225          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1226          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1227               bit++) {          if (acl->is_deleted)
1228                  if (!(perm & (1 << bit)))                  return true;
1229                          continue;   next:
1230                  pos = head->read_avail;          bit = head->r.bit;
1231                  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))  
1232                  return false;                  return false;
1233          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1234                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1235                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1236                          return false;                  const u16 perm = ptr->perm;
1237          }                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1238          return true;                          if (!(perm & (1 << bit)))
1239  }                                  continue;
1240                            if (head->r.print_execute_only &&
1241  /**                              bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1242   * ccs_print_network_acl - Print a network ACL entry.                                  continue;
1243   *                          break;
1244   * @head: Pointer to "struct ccs_io_buffer".                  }
1245   * @ptr:  Pointer to "struct ccs_ip_network_acl".                  if (bit >= CCS_MAX_PATH_OPERATION)
1246   * @cond: Pointer to "struct ccs_condition". May be NULL.                          goto done;
1247   *                  ccs_set_group(head);
1248   * Returns true on success, false otherwise.                  ccs_set_string(head, "file ");
1249   */                  ccs_set_string(head, ccs_path_keyword[bit]);
1250  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                  ccs_print_name_union(head, &ptr->name);
1251                                    struct ccs_ip_network_acl *ptr,          } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1252                                    const struct ccs_condition *cond)                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1253  {                  struct ccs_execute_handler *ptr
1254          int pos;                          = container_of(acl, typeof(*ptr), head);
1255          u8 bit;                  ccs_set_group(head);
1256          const u16 perm = ptr->perm;                  ccs_io_printf(head, "%s ",
1257          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                                acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1258                  if (!(perm & (1 << bit)))                                CCS_KEYWORD_EXECUTE_HANDLER :
1259                          continue;                                CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1260                  pos = head->read_avail;                  ccs_set_string(head, ptr->handler->name);
1261                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",          } else if (head->r.print_execute_only) {
1262                                     ccs_net2keyword(bit)))                  return true;
1263                          goto out;          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1264                    struct ccs_mkdev_acl *ptr =
1265                            container_of(acl, typeof(*ptr), head);
1266                    bit = ccs_fns(ptr->perm, bit);
1267                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1268                            goto done;
1269                    ccs_set_group(head);
1270                    ccs_set_string(head, "file ");
1271                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1272                    ccs_print_name_union(head, &ptr->name);
1273                    ccs_print_number_union(head, &ptr->mode);
1274                    ccs_print_number_union(head, &ptr->major);
1275                    ccs_print_number_union(head, &ptr->minor);
1276            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1277                    struct ccs_path2_acl *ptr =
1278                            container_of(acl, typeof(*ptr), head);
1279                    bit = ccs_fns(ptr->perm, bit);
1280                    if (bit >= CCS_MAX_PATH2_OPERATION)
1281                            goto done;
1282                    ccs_set_group(head);
1283                    ccs_set_string(head, "file ");
1284                    ccs_set_string(head, ccs_path2_keyword[bit]);
1285                    ccs_print_name_union(head, &ptr->name1);
1286                    ccs_print_name_union(head, &ptr->name2);
1287            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1288                    struct ccs_path_number_acl *ptr =
1289                            container_of(acl, typeof(*ptr), head);
1290                    bit = ccs_fns(ptr->perm, bit);
1291                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1292                            goto done;
1293                    ccs_set_group(head);
1294                    ccs_set_string(head, "file ");
1295                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1296                    ccs_print_name_union(head, &ptr->name);
1297                    ccs_print_number_union(head, &ptr->number);
1298            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1299                    struct ccs_env_acl *ptr =
1300                            container_of(acl, typeof(*ptr), head);
1301                    ccs_set_group(head);
1302                    ccs_set_string(head, "misc env ");
1303                    ccs_set_string(head, ptr->env->name);
1304            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1305                    struct ccs_capability_acl *ptr =
1306                            container_of(acl, typeof(*ptr), head);
1307                    ccs_set_group(head);
1308                    ccs_set_string(head, "capability ");
1309                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1310            } else if (acl_type == CCS_TYPE_INET_ACL) {
1311                    struct ccs_inet_acl *ptr =
1312                            container_of(acl, typeof(*ptr), head);
1313                    bit = ccs_fns(ptr->perm, bit);
1314                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1315                            goto done;
1316                    ccs_set_group(head);
1317                    ccs_set_string(head, "network inet ");
1318                    ccs_set_string(head, ccs_inet_keyword[ptr->protocol]);
1319                    ccs_set_space(head);
1320                    ccs_set_string(head, ccs_net_keyword[bit]);
1321                    ccs_set_space(head);
1322                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1323                            char buf[128];
1324                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1325                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1326                                             group_name->name))                          ccs_set_string(head,
1327                                  goto out;                                         ptr->address.group->group_name->name);
1328                          break;                          break;
1329                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1330                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1331                                  goto out;                                         ptr->address.ipv4.max);
1332                            ccs_io_printf(head, "%s", buf);
1333                          break;                          break;
1334                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1335                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1336                                  goto out;                                         ptr->address.ipv6.max);
1337                          break;                          ccs_io_printf(head, "%s", buf);
1338                  }                          break;
1339                  if (!ccs_print_number_union(head, &ptr->port) ||                  }
1340                      !ccs_print_condition(head, cond))                  ccs_print_number_union(head, &ptr->port);
1341                          goto out;          } else if (acl_type == CCS_TYPE_UNIX_ACL) {
1342                    struct ccs_unix_acl *ptr =
1343                            container_of(acl, typeof(*ptr), head);
1344                    bit = ccs_fns(ptr->perm, bit);
1345                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1346                            goto done;
1347                    ccs_set_group(head);
1348                    ccs_set_string(head, "network unix ");
1349                    ccs_set_string(head, ccs_unix_keyword[ptr->protocol]);
1350                    ccs_set_space(head);
1351                    ccs_set_string(head, ccs_net_keyword[bit]);
1352                    ccs_print_name_union(head, &ptr->name);
1353            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1354                    struct ccs_signal_acl *ptr =
1355                            container_of(acl, typeof(*ptr), head);
1356                    ccs_set_group(head);
1357                    ccs_set_string(head, "ipc signal ");
1358                    ccs_io_printf(head, "%u ", ptr->sig);
1359                    ccs_set_string(head, ptr->domainname->name);
1360            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1361                    struct ccs_mount_acl *ptr =
1362                            container_of(acl, typeof(*ptr), head);
1363                    ccs_set_group(head);
1364                    ccs_io_printf(head, "file mount");
1365                    ccs_print_name_union(head, &ptr->dev_name);
1366                    ccs_print_name_union(head, &ptr->dir_name);
1367                    ccs_print_name_union(head, &ptr->fs_type);
1368                    ccs_print_number_union(head, &ptr->flags);
1369            }
1370            head->r.bit = bit + 1;
1371            if (acl->cond) {
1372                    head->r.print_cond_part = true;
1373                    head->r.cond_step = 0;
1374                    if (!ccs_flush(head))
1375                            return false;
1376     print_cond_part:
1377                    if (!ccs_print_condition(head, acl->cond))
1378                            return false;
1379                    head->r.print_cond_part = false;
1380            } else {
1381                    ccs_set_lf(head);
1382          }          }
1383          head->read_bit = 0;          switch (acl_type) {
1384          return true;          case CCS_TYPE_PATH_ACL:
1385   out:          case CCS_TYPE_MKDEV_ACL:
1386          head->read_bit = bit;          case CCS_TYPE_PATH2_ACL:
1387          head->read_avail = pos;          case CCS_TYPE_PATH_NUMBER_ACL:
1388          return false;          case CCS_TYPE_INET_ACL:
1389  }          case CCS_TYPE_UNIX_ACL:
1390                    goto next;
 /**  
  * ccs_print_signal_acl - Print a signal ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct signale_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  
                                  struct ccs_signal_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",  
                            ptr->sig, ptr->domainname->name) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1391          }          }
1392     done:
1393            head->r.bit = 0;
1394          return true;          return true;
1395  }  }
1396    
1397  /**  /**
1398   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_read_domain2 - Read domain policy.
1399   *   *
1400   * @head:    Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1401   * @keyword: Name of the keyword.   * @domain: Pointer to "struct ccs_domain_info".
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1402   *   *
1403   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
  */  
 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.  
1404   *   *
1405   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1406   */   */
1407  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1408                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain)
                                 const struct ccs_condition *cond)  
1409  {  {
1410          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1411          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||                  struct ccs_acl_info *ptr =
1412              !ccs_print_name_union(head, &ptr->dev_name) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1413              !ccs_print_name_union(head, &ptr->dir_name) ||                  if (!ccs_print_entry(head, ptr))
1414              !ccs_print_name_union(head, &ptr->fs_type) ||                          return false;
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_umount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_umount_acl".  
  * @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;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_chroot_acl - Print a chroot ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_chroot_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_chroot_acl(struct ccs_io_buffer *head,  
                                  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.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_pivot_root_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_pivot_root_acl(struct ccs_io_buffer *head,  
                                      struct ccs_pivot_root_acl *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_PIVOT_ROOT) ||  
             !ccs_print_name_union(head, &ptr->new_root) ||  
             !ccs_print_name_union(head, &ptr->old_root) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1415          }          }
1416            head->r.acl = NULL;
1417          return true;          return true;
1418  }  }
1419    
1420  /**  /**
1421   * 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.  
1422   *   *
1423   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1424   *   *
1425   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1426   */   */
1427  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1428  {  {
1429          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1430                  return;                  return;
1431          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1432                  head->read_step = 1;                  struct ccs_domain_info *domain =
1433          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1434                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1435                  const char *quota_exceeded = "";                          u8 i;
1436                  const char *transition_failed = "";                  case 0:
1437                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1438                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1439                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1440                  if (head->read_step != 1)                          /* Print domainname and flags. */
1441                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1442                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1443                          continue;                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1444                  /* Print domainname and flags. */                                        domain->profile);
1445                  if (domain->quota_warned)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1446                          quota_exceeded = "quota_exceeded\n";                                        domain->group);
1447                  if (domain->domain_transition_failed)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1448                          transition_failed = "transition_failed\n";                                  if (domain->flags[i])
1449                  if (domain->ignore_global_allow_read)                                          ccs_set_string(head, ccs_dif[i]);
1450                          ignore_global_allow_read                          head->r.step++;
1451                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          ccs_set_lf(head);
1452                  if (domain->ignore_global_allow_env)                          /* fall through */
1453                          ignore_global_allow_env                  case 1:
1454                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          if (!ccs_read_domain2(head, domain))
1455                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                                  return;
1456                                     "%s%s%s%s\n", domain->domainname->name,                          head->r.step++;
1457                                     domain->profile, quota_exceeded,                          if (!ccs_set_lf(head))
                                    transition_failed,  
                                    ignore_global_allow_read,  
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
1458                                  return;                                  return;
1459                            /* fall through */
1460                    case 2:
1461                            head->r.step = 0;
1462                            if (head->r.print_this_domain_only)
1463                                    goto done;
1464                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1465          }          }
1466          head->read_eof = true;   done:
1467            head->r.eof = true;
1468  }  }
1469    
1470  /**  /**
# Line 1785  static int ccs_write_domain_profile(stru Line 1494  static int ccs_write_domain_profile(stru
1494          if (profile >= CCS_MAX_PROFILES)          if (profile >= CCS_MAX_PROFILES)
1495                  return -EINVAL;                  return -EINVAL;
1496          domain = ccs_find_domain(cp + 1);          domain = ccs_find_domain(cp + 1);
1497          if (domain && ccs_profile(profile))          if (domain && (!ccs_policy_loaded || ccs_profile_ptr[(u8) profile]))
1498                  domain->profile = (u8) profile;                  domain->profile = (u8) profile;
1499          return 0;          return 0;
1500  }  }
# Line 1806  static int ccs_write_domain_profile(stru Line 1515  static int ccs_write_domain_profile(stru
1515   */   */
1516  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1517  {  {
1518          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1519                  return;                  return;
1520          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1521                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1522                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1523                  if (domain->is_deleted)                  if (domain->is_deleted)
1524                          continue;                          continue;
1525                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1526                          return;                          return;
1527                    ccs_io_printf(head, "%u ", domain->profile);
1528                    ccs_set_string(head, domain->domainname->name);
1529                    ccs_set_lf(head);
1530          }          }
1531          head->read_eof = true;          head->r.eof = true;
1532  }  }
1533    
1534  /**  /**
# Line 1830  static void ccs_read_domain_profile(stru Line 1540  static void ccs_read_domain_profile(stru
1540   */   */
1541  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1542  {  {
1543          head->read_eof = false;          head->r.eof = false;
1544          return 0;          return 0;
1545  }  }
1546    
# Line 1855  static void ccs_read_pid(struct ccs_io_b Line 1565  static void ccs_read_pid(struct ccs_io_b
1565          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1566          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1567          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1568          if (!buf)          if (!buf) {
1569                    head->r.eof = true;
1570                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1571          if (head->read_avail || head->read_eof)          }
1572            if (head->r.w_pos || head->r.eof)
1573                  return;                  return;
1574          head->read_eof = true;          head->r.eof = true;
1575          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1576                  task_info = true;                  task_info = true;
1577          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
1578                  global_pid = true;                  global_pid = true;
1579          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1580          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1581  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1582          if (global_pid)          if (global_pid)
1583                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1584          else          else
1585                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1586  #else  #else
1587          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1588  #endif  #endif
# Line 1878  static void ccs_read_pid(struct ccs_io_b Line 1590  static void ccs_read_pid(struct ccs_io_b
1590                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1591                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1592          }          }
1593          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1594          if (!domain)          if (!domain)
1595                  return;                  return;
1596          if (!task_info)          if (!task_info) {
1597                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1598                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1599          else          } else {
1600                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1601                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1602                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1603                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1604                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1605                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1606                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1607                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1608                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1609            }
1610  }  }
1611    
1612    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1613            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1614            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1615            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1616            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1617            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1618    };
1619    
1620    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1621            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1622            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1623            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1624    };
1625    
1626  /**  /**
1627   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1628   *   *
1629   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1630   *   *
1631   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1632   */   */
1633  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1634  {  {
1635          char *data = head->write_buf;          char *data = head->write_buf;
1636          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1637          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1638                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1639          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1640                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1641          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1642                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1643                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1644          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1645                  return ccs_write_domain_initializer_policy(data, true,          };
1646                                                             is_delete);          for (i = 0; i < 3; i++)
1647          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1648                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1649          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1650                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1651          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1652                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1653          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1654                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1655          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1656                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1657          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  unsigned int group;
1658                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1659          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1660                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1661          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                          if (data)
1662                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1663          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1664                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1665                    }
1666            }
1667          return -EINVAL;          return -EINVAL;
1668  }  }
1669    
1670  /**  /**
1671   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1672   *   *
1673   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1674   *   * @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".  
1675   *   *
1676   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1677     *
1678     * Caller holds ccs_read_lock().
1679   */   */
1680  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1681  {  {
1682          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1683          char *arg_ptr = ee->tmp;                  struct ccs_group *group =
1684          int arg_len = 0;                          list_entry(head->r.group, typeof(*group), head.list);
1685          unsigned long pos = bprm->p;                  list_for_each_cookie(head->r.acl, &group->member_list) {
1686          int offset = pos % PAGE_SIZE;                          struct ccs_acl_head *ptr =
1687          bool done = false;                                  list_entry(head->r.acl, typeof(*ptr), list);
1688          if (!bprm->argc)                          if (ptr->is_deleted)
1689                  goto out;                                  continue;
1690          while (1) {                          if (!ccs_flush(head))
1691                  if (!ccs_dump_page(bprm, pos, &ee->dump))                                  return false;
1692                          goto out;                          ccs_set_string(head, ccs_group_name[idx]);
1693                  pos += PAGE_SIZE - offset;                          ccs_set_string(head, group->group_name->name);
1694                  /* Read. */                          if (idx == CCS_PATH_GROUP) {
1695                  while (offset < PAGE_SIZE) {                                  ccs_set_space(head);
1696                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, container_of
1697                          const unsigned char c = kaddr[offset++];                                                 (ptr, struct ccs_path_group,
1698                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                  head)->member_name->name);
1699                                  if (c == '\\') {                          } else if (idx == CCS_NUMBER_GROUP) {
1700                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_number_union(head, &container_of
1701                                          arg_ptr[arg_len++] = '\\';                                                         (ptr, struct ccs_number_group,
1702                                  } else if (c > ' ' && c < 127) {                                                          head)->number);
1703                                          arg_ptr[arg_len++] = c;                          } else if (idx == CCS_ADDRESS_GROUP) {
1704                                  } else {                                  char buffer[128];
1705                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_address_group *member =
1706                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(ptr, typeof(*member),
1707                                          arg_ptr[arg_len++]                                                       head);
1708                                                  = ((c >> 3) & 7) + '0';                                  if (member->is_ipv6)
1709                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          ccs_print_ipv6(buffer, sizeof(buffer),
1710                                  }                                                         member->min.ipv6,
1711                          } else {                                                         member->max.ipv6);
1712                                  arg_ptr[arg_len] = '\0';                                  else
1713                                  done = true;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1714                                  break;                                                         member->min.ipv4,
1715                                                           member->max.ipv4);
1716                                    ccs_io_printf(head, " %s", buffer);
1717                          }                          }
1718                            ccs_set_lf(head);
1719                  }                  }
1720                  offset = 0;                  head->r.acl = NULL;
                 if (done)  
                         break;  
1721          }          }
1722            head->r.group = NULL;
1723          return true;          return true;
  out:  
         return false;  
1724  }  }
1725    
1726  /**  /**
1727   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_policy - Read "struct ccs_..._entry" list.
1728   *   *
1729   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1730     * @idx:  Index number.
1731   *   *
1732   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Returns true on success, false otherwise.
1733     *
1734     * Caller holds ccs_read_lock().
1735   */   */
1736  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)  
1737  {  {
1738          struct ccs_condition *cond;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1739          char *buf;                  struct ccs_acl_head *acl =
1740          int len = 256;                          container_of(head->r.acl, typeof(*acl), list);
1741          char *realpath = NULL;                  if (acl->is_deleted)
1742          char *argv0 = NULL;                          continue;
1743          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);                  if (!ccs_flush(head))
1744          if (profile->learning->learning_exec_realpath) {                          return false;
1745                  struct file *file = ee->bprm->file;                  switch (idx) {
1746  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)                  case CCS_ID_TRANSITION_CONTROL:
1747                  struct path path = { file->f_vfsmnt, file->f_dentry };                          {
1748                  realpath = ccs_realpath_from_path(&path);                                  struct ccs_transition_control *ptr =
1749  #else                                          container_of(acl, typeof(*ptr), head);
1750                  realpath = ccs_realpath_from_path(&file->f_path);                                  ccs_set_string(head,
1751  #endif                                                 ccs_transition_type[ptr->type]);
1752                  if (realpath)                                  ccs_set_string(head, ptr->program ?
1753                          len += strlen(realpath) + 17;                                                 ptr->program->name : "any");
1754          }                                  ccs_set_string(head, " from ");
1755          if (profile->learning->learning_exec_argv0) {                                  ccs_set_string(head, ptr->domainname ?
1756                  if (ccs_get_argv0(ee)) {                                                 ptr->domainname->name : "any");
1757                          argv0 = ee->tmp;                          }
1758                          len += strlen(argv0) + 16;                          break;
1759                    case CCS_ID_AGGREGATOR:
1760                            {
1761                                    struct ccs_aggregator *ptr =
1762                                            container_of(acl, typeof(*ptr), head);
1763                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1764                                    ccs_set_string(head, ptr->original_name->name);
1765                                    ccs_set_space(head);
1766                                    ccs_set_string(head,
1767                                                   ptr->aggregated_name->name);
1768                            }
1769                            break;
1770                    case CCS_ID_PATTERN:
1771                            {
1772                                    struct ccs_pattern *ptr =
1773                                            container_of(acl, typeof(*ptr), head);
1774                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1775                                    ccs_set_string(head, ptr->pattern->name);
1776                            }
1777                            break;
1778                    case CCS_ID_RESERVEDPORT:
1779                            {
1780                                    struct ccs_reserved *ptr =
1781                                            container_of(acl, typeof(*ptr), head);
1782                                    const u16 min_port = ptr->min_port;
1783                                    const u16 max_port = ptr->max_port;
1784                                    ccs_set_string(head,
1785                                                   CCS_KEYWORD_DENY_AUTOBIND);
1786                                    ccs_io_printf(head, "%u", min_port);
1787                                    if (min_port != max_port)
1788                                            ccs_io_printf(head, "-%u", max_port);
1789                            }
1790                            break;
1791                    default:
1792                            continue;
1793                  }                  }
1794                    ccs_set_lf(head);
1795          }          }
1796          buf = kmalloc(len, GFP_KERNEL);          head->r.acl = NULL;
1797          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;  
1798  }  }
1799    
1800  /**  /**
1801   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1802   *   *
1803   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1804   *   *
1805   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1806   */   */
1807  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1808  {  {
1809          struct ccs_condition *cond;          if (head->r.eof)
1810          char *buf;                  return;
1811          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1812          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
1813          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
1814          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
1815                  symlink = r->obj->symlink_target->name;                  return;
1816                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1817                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1818                    head->r.step++;
1819            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1820                    return;
1821            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1822                   + CCS_MAX_ACL_GROUPS) {
1823                    head->r.group_index = head->r.step - CCS_MAX_POLICY
1824                            - CCS_MAX_GROUP;
1825                    if (!ccs_read_domain2(head,
1826                                          &ccs_acl_group[head->r.group_index]))
1827                            return;
1828                    head->r.step++;
1829          }          }
1830          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;  
1831  }  }
1832    
1833  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2170  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1837  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1837  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1838    
1839  /* Structure for query. */  /* Structure for query. */
1840  struct ccs_query_entry {  struct ccs_query {
1841          struct list_head list;          struct list_head list;
1842          char *query;          char *query;
1843          int query_len;          int query_len;
# Line 2179  struct ccs_query_entry { Line 1846  struct ccs_query_entry {
1846          int answer;          int answer;
1847  };  };
1848    
1849  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1850  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1851    
1852  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1853  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1854    
1855    static void ccs_truncate(char *str)
1856    {
1857            while (* (unsigned char *) str > (unsigned char) ' ')
1858                    str++;
1859            *str = '\0';
1860    }
1861    
1862  /**  /**
1863   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1864   *   *
1865   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1866   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1867   *   *
1868   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1869   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1870   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1871   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1872   */   */
1873  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1874  {  {
# Line 2203  int ccs_supervisor(struct ccs_request_in Line 1877  int ccs_supervisor(struct ccs_request_in
1877          int pos;          int pos;
1878          int len;          int len;
1879          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1880          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1881          bool quota_exceeded = false;          bool quota_exceeded = false;
1882          char *header;          char *header;
1883          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1884                  r->domain = ccs_current_domain();          va_start(args, fmt);
1885          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1886            va_end(args);
1887            if (r->mode == CCS_CONFIG_LEARNING) {
1888                  char *buffer;                  char *buffer;
1889                  struct ccs_condition *cond;                  char *realpath = NULL;
1890          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1891                    char *symlink = NULL;
1892                    char *handler = NULL;
1893                    const struct ccs_preference *pref;
1894                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1895                          return 0;                          return 0;
1896                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1897                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, GFP_KERNEL);  
                 if (!buffer)  
1898                          return 0;                          return 0;
1899                  va_start(args, fmt);                  pref = &ccs_profile(r->profile)->preference;
1900                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1901                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1902                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1903                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1904                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1905                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1906                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1907                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1908                          char str[] = "if task.type=execute_handler";                                  }
1909                          cond = ccs_get_condition(str);                          }
1910                  } else                          if (pref->learning_exec_realpath) {
1911                          cond = NULL;                                  realpath = strstr(header,
1912                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1913                  ccs_put_condition(cond);                                  if (realpath) {
1914                  kfree(buffer);                                          realpath += 8;
1915                  /* fall through */                                          ccs_truncate(realpath);
1916          case CCS_CONFIG_PERMISSIVE:                                  }
1917                            }
1918                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1919                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1920                               pref->learning_symlink_target) {
1921                            symlink = strstr(header, " symlink.target=\"");
1922                            if (symlink)
1923                                    ccs_truncate(symlink + 1);
1924                    }
1925                    handler = strstr(header, "type=execute_handler");
1926                    if (handler)
1927                            ccs_truncate(handler);
1928                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1929                    if (buffer) {
1930                            va_start(args, fmt);
1931                            vsnprintf(buffer, len - 1, fmt, args);
1932                            va_end(args);
1933                            if (handler || realpath || argv0 || symlink) {
1934                                    ccs_addprintf(buffer, len, " if");
1935                                    if (handler)
1936                                            ccs_addprintf(buffer, len, " task.%s",
1937                                                          handler);
1938                                    if (realpath)
1939                                            ccs_addprintf(buffer, len, " exec.%s",
1940                                                          realpath);
1941                                    if (argv0)
1942                                            ccs_addprintf(buffer, len,
1943                                                          " exec.argv[0]=%s",
1944                                                          argv0);
1945                                    if (symlink)
1946                                            ccs_addprintf(buffer, len, "%s",
1947                                                          symlink);
1948                            }
1949                            ccs_normalize_line(buffer);
1950                            ccs_write_domain2(buffer, domain, false);
1951                            kfree(buffer);
1952                    }
1953                    kfree(header);
1954                  return 0;                  return 0;
1955          }          }
1956            if (r->mode != CCS_CONFIG_ENFORCING)
1957                    return 0;
1958          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
1959                  int i;                  int i;
1960                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1961                          return -EPERM;                          return -EPERM;
1962                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->preference.
1963                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
1964                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
1965                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
1966                  }                  }
1967                  return -EPERM;                  return -EPERM;
1968          }          }
1969          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);  
1970          if (!header)          if (!header)
1971                  goto out;                  goto out;
1972          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
1973          if (!ccs_query_entry)          if (!entry)
1974                  goto out;                  goto out;
1975          len = ccs_round2(len);          len = ccs_round2(len);
1976          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
1977          if (!ccs_query_entry->query)          if (!entry->query)
1978                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
1979          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
1980          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
1981              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
1982                  quota_exceeded = true;                  quota_exceeded = true;
1983          } else {          } else {
1984                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
1985                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
1986          }          }
1987          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
1988          if (quota_exceeded)          if (quota_exceeded)
1989                  goto out;                  goto out;
1990          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
1991                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
1992          kfree(header);          kfree(header);
1993          header = NULL;          header = NULL;
1994          va_start(args, fmt);          va_start(args, fmt);
1995          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
1996          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
1997          va_end(args);          va_end(args);
1998          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
1999          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2000          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2001          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2002          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2003               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2004               ccs_query_entry->timer++) {               entry->timer++) {
2005                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2006                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2007                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2008                  if (ccs_query_entry->answer)                  if (entry->answer)
2009                          break;                          break;
2010          }          }
2011          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2012          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2013          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2014          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2015          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2016          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2017                  error = 1;                  error = CCS_RETRY_REQUEST;
2018                  r->retry++;                  r->retry++;
2019                  break;                  break;
2020          case 1:          case 1:
# Line 2318  int ccs_supervisor(struct ccs_request_in Line 2029  int ccs_supervisor(struct ccs_request_in
2029                  break;                  break;
2030          }          }
2031   out:   out:
2032          if (ccs_query_entry)          if (entry)
2033                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2034          kfree(ccs_query_entry);          kfree(entry);
2035          kfree(header);          kfree(header);
2036          return error;          return error;
2037  }  }
# Line 2343  static int ccs_poll_query(struct file *f Line 2054  static int ccs_poll_query(struct file *f
2054          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2055                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2056                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2057                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2058                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2059                          if (ptr->answer)                          if (ptr->answer)
2060                                  continue;                                  continue;
2061                          found = true;                          found = true;
# Line 2371  static void ccs_read_query(struct ccs_io Line 2082  static void ccs_read_query(struct ccs_io
2082          int pos = 0;          int pos = 0;
2083          int len = 0;          int len = 0;
2084          char *buf;          char *buf;
2085          if (head->read_avail)          if (head->r.w_pos)
2086                  return;                  return;
2087          if (head->read_buf) {          if (head->read_buf) {
2088                  kfree(head->read_buf);                  kfree(head->read_buf);
2089                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2090          }          }
2091          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2092          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2093                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2094                  if (ptr->answer)                  if (ptr->answer)
2095                          continue;                          continue;
2096                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2097                          continue;                          continue;
2098                  len = ptr->query_len;                  len = ptr->query_len;
2099                  break;                  break;
2100          }          }
2101          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2102          if (!len) {          if (!len) {
2103                  head->read_step = 0;                  head->r.query_index = 0;
2104                  return;                  return;
2105          }          }
2106          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2107          if (!buf)          if (!buf)
2108                  return;                  return;
2109          pos = 0;          pos = 0;
2110          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2111          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2112                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2113                  if (ptr->answer)                  if (ptr->answer)
2114                          continue;                          continue;
2115                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2116                          continue;                          continue;
2117                  /*                  /*
2118                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2416  static void ccs_read_query(struct ccs_io Line 2124  static void ccs_read_query(struct ccs_io
2124          }          }
2125          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2126          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2127                  head->read_buf = buf;                  head->read_buf = buf;
2128                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2129                    head->r.query_index++;
2130          } else {          } else {
2131                  kfree(buf);                  kfree(buf);
2132          }          }
# Line 2440  static int ccs_write_answer(struct ccs_i Line 2147  static int ccs_write_answer(struct ccs_i
2147          unsigned int answer;          unsigned int answer;
2148          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2149          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2150                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2151                  ptr->timer = 0;                  ptr->timer = 0;
2152          }          }
2153          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2449  static int ccs_write_answer(struct ccs_i Line 2155  static int ccs_write_answer(struct ccs_i
2155                  return -EINVAL;                  return -EINVAL;
2156          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2157          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2158                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2159                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2160                          continue;                          continue;
2161                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2468  static int ccs_write_answer(struct ccs_i Line 2173  static int ccs_write_answer(struct ccs_i
2173   */   */
2174  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2175  {  {
2176          if (head->read_eof)          if (head->r.eof)
2177                  return;                  return;
2178          ccs_io_printf(head, "1.7.0");          ccs_set_string(head, "1.8.0-pre");
2179          head->read_eof = true;          head->r.eof = true;
2180  }  }
2181    
2182  /**  /**
# Line 2481  static void ccs_read_version(struct ccs_ Line 2186  static void ccs_read_version(struct ccs_
2186   */   */
2187  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2188  {  {
2189          if (head->read_eof)          if (head->r.eof)
2190                  return;                  return;
2191          /*          /*
2192           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2193           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2194           */           */
2195          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2196          head->read_eof = true;          head->r.eof = true;
2197  }  }
2198    
2199  /**  /**
# Line 2501  static void ccs_read_self_domain(struct Line 2206  static void ccs_read_self_domain(struct
2206   */   */
2207  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2208  {  {
2209          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2210          if (!head)          if (!head)
2211                  return -ENOMEM;                  return -ENOMEM;
2212          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2213          head->type = type;          head->type = type;
2214          switch (type) {          switch (type) {
2215          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2216                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2217                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2218                  break;                  break;
2219          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2220                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2221                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2222                  break;                  break;
2223  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2224          case CCS_GRANTLOG: /* /proc/ccs/grant_log */          case CCS_GRANTLOG: /* /proc/ccs/grant_log */
                 head->poll = ccs_poll_grant_log;  
                 head->read = ccs_read_grant_log;  
                 break;  
2225          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2226                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2227                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2228                  break;                  break;
2229  #endif  #endif
2230          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2562  int ccs_open_control(const u8 type, stru Line 2264  int ccs_open_control(const u8 type, stru
2264                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2265                  break;                  break;
2266          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2267                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2268                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2269                  break;                  break;
2270          }          }
2271          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2577  int ccs_open_control(const u8 type, stru Line 2279  int ccs_open_control(const u8 type, stru
2279                  /* Don't allocate read_buf for poll() access. */                  /* Don't allocate read_buf for poll() access. */
2280                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2281                          head->readbuf_size = 4096;                          head->readbuf_size = 4096;
2282                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2283                  if (!head->read_buf) {                  if (!head->read_buf) {
2284                          kfree(head);                          kfree(head);
2285                          return -ENOMEM;                          return -ENOMEM;
# Line 2591  int ccs_open_control(const u8 type, stru Line 2293  int ccs_open_control(const u8 type, stru
2293                  head->write = NULL;                  head->write = NULL;
2294          } else if (head->write) {          } else if (head->write) {
2295                  head->writebuf_size = 4096;                  head->writebuf_size = 4096;
2296                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2297                  if (!head->write_buf) {                  if (!head->write_buf) {
2298                          kfree(head->read_buf);                          kfree(head->read_buf);
2299                          kfree(head);                          kfree(head);
# Line 2600  int ccs_open_control(const u8 type, stru Line 2302  int ccs_open_control(const u8 type, stru
2302          }          }
2303          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2304              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2305                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2306          file->private_data = head;          file->private_data = head;
2307          /*          /*
2308           * Call the handler now if the file is /proc/ccs/self_domain           * Call the handler now if the file is /proc/ccs/self_domain
# Line 2628  int ccs_open_control(const u8 type, stru Line 2330  int ccs_open_control(const u8 type, stru
2330   * Waits for read readiness.   * Waits for read readiness.
2331   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and   * /proc/ccs/query is handled by /usr/sbin/ccs-queryd and
2332   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by   * /proc/ccs/grant_log and /proc/ccs/reject_log are handled by
2333   * /usr/sbin/ccs-auditd.   * /usr/sbin/ccs-auditd .
2334   */   */
2335  int ccs_poll_control(struct file *file, poll_table *wait)  int ccs_poll_control(struct file *file, poll_table *wait)
2336  {  {
# Line 2650  int ccs_poll_control(struct file *file, Line 2352  int ccs_poll_control(struct file *file,
2352  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2353                       const int buffer_len)                       const int buffer_len)
2354  {  {
2355          int len = 0;          int len;
2356          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;