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

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 3109 by kumaneko, Fri Oct 16 05:02:23 2009 UTC branches/ccs-patch/security/ccsecurity/policy_io.c revision 3853 by kumaneko, Mon Jul 26 03:42:20 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.1-pre   2009/10/16   * Version: 1.7.2+   2010/07/21
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 33  static struct ccs_profile ccs_default_pr Line 33  static struct ccs_profile ccs_default_pr
33          .preference.permissive_verbose = true          .preference.permissive_verbose = true
34  };  };
35    
36    /* Profile version. Currently only 20090903 is defined. */
37    static unsigned int ccs_profile_version;
38    
39  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
40  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
 /* Lock for protecting "struct ccs_profile"->comment  */  
 static DEFINE_SPINLOCK(ccs_profile_comment_lock);  
   
42  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
43  static const char *ccs_mode_4[4] = {  const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
44          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED] = "disabled",
45            [CCS_CONFIG_LEARNING] = "learning",
46            [CCS_CONFIG_PERMISSIVE] = "permissive",
47            [CCS_CONFIG_ENFORCING] = "enforcing"
48  };  };
49    
50  /* String table for /proc/ccs/profile */  /* String table for /proc/ccs/profile */
# Line 68  static const char *ccs_mac_keywords[CCS_ Line 71  static const char *ccs_mac_keywords[CCS_
71          = "file::truncate",          = "file::truncate",
72          [CCS_MAC_FILE_SYMLINK]          [CCS_MAC_FILE_SYMLINK]
73          = "file::symlink",          = "file::symlink",
         [CCS_MAC_FILE_REWRITE]  
         = "file::rewrite",  
74          [CCS_MAC_FILE_MKBLOCK]          [CCS_MAC_FILE_MKBLOCK]
75          = "file::mkblock",          = "file::mkblock",
76          [CCS_MAC_FILE_MKCHAR]          [CCS_MAC_FILE_MKCHAR]
# Line 94  static const char *ccs_mac_keywords[CCS_ Line 95  static const char *ccs_mac_keywords[CCS_
95          = "file::umount",          = "file::umount",
96          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
97          = "file::pivot_root",          = "file::pivot_root",
98            [CCS_MAC_FILE_TRANSIT]
99            = "file::transit",
100          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
101          = "misc::env",          = "misc::env",
         [CCS_MAC_NETWORK_UDP_BIND]  
         = "network::inet_udp_bind",  
         [CCS_MAC_NETWORK_UDP_CONNECT]  
         = "network::inet_udp_connect",  
102          [CCS_MAC_NETWORK_TCP_BIND]          [CCS_MAC_NETWORK_TCP_BIND]
103          = "network::inet_tcp_bind",          = "network::inet_tcp_bind",
104          [CCS_MAC_NETWORK_TCP_LISTEN]          [CCS_MAC_NETWORK_TCP_LISTEN]
# Line 108  static const char *ccs_mac_keywords[CCS_ Line 107  static const char *ccs_mac_keywords[CCS_
107          = "network::inet_tcp_connect",          = "network::inet_tcp_connect",
108          [CCS_MAC_NETWORK_TCP_ACCEPT]          [CCS_MAC_NETWORK_TCP_ACCEPT]
109          = "network::inet_tcp_accept",          = "network::inet_tcp_accept",
110            [CCS_MAC_NETWORK_UDP_BIND]
111            = "network::inet_udp_bind",
112            [CCS_MAC_NETWORK_UDP_SEND]
113            = "network::inet_udp_send",
114            [CCS_MAC_NETWORK_UDP_RECV]
115            = "network::inet_udp_recv",
116          [CCS_MAC_NETWORK_RAW_BIND]          [CCS_MAC_NETWORK_RAW_BIND]
117          = "network::inet_raw_bind",          = "network::inet_raw_bind",
118          [CCS_MAC_NETWORK_RAW_CONNECT]          [CCS_MAC_NETWORK_RAW_SEND]
119          = "network::inet_raw_connect",          = "network::inet_raw_send",
120            [CCS_MAC_NETWORK_RAW_RECV]
121            = "network::inet_raw_recv",
122          [CCS_MAC_SIGNAL]          [CCS_MAC_SIGNAL]
123          = "ipc::signal",          = "ipc::signal",
124          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]          [CCS_MAX_MAC_INDEX + CCS_INET_STREAM_SOCKET_CREATE]
# Line 128  static const char *ccs_mac_keywords[CCS_ Line 135  static const char *ccs_mac_keywords[CCS_
135          = "capability::use_route",          = "capability::use_route",
136          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_USE_PACKET_SOCKET]
137          = "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",  
138          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]          [CCS_MAX_MAC_INDEX + CCS_SYS_REBOOT]
139          = "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",  
140          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]          [CCS_MAX_MAC_INDEX + CCS_SYS_VHANGUP]
141          = "capability::SYS_VHANGUP",          = "capability::SYS_VHANGUP",
142          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]          [CCS_MAX_MAC_INDEX + CCS_SYS_SETTIME]
# Line 156  static const char *ccs_mac_keywords[CCS_ Line 155  static const char *ccs_mac_keywords[CCS_
155          = "capability::create_char_dev",          = "capability::create_char_dev",
156          [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]          [CCS_MAX_MAC_INDEX + CCS_CREATE_UNIX_SOCKET]
157          = "capability::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",  
158          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]          [CCS_MAX_MAC_INDEX + CCS_SYS_KEXEC_LOAD]
159          = "capability::SYS_KEXEC_LOAD",          = "capability::SYS_KEXEC_LOAD",
         [CCS_MAX_MAC_INDEX + CCS_SYS_PIVOT_ROOT]  
         = "capability::SYS_PIVOT_ROOT",  
160          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]          [CCS_MAX_MAC_INDEX + CCS_SYS_PTRACE]
161          = "capability::SYS_PTRACE",          = "capability::SYS_PTRACE",
         [CCS_MAX_MAC_INDEX + CCS_CONCEAL_MOUNT]  
         = "capability::conceal_mount",  
162          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
163           + CCS_MAC_CATEGORY_FILE] = "file",           + CCS_MAC_CATEGORY_FILE] = "file",
164          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX          [CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
# Line 216  static const char *ccs_yesno(const unsig Line 197  static const char *ccs_yesno(const unsig
197          return value ? "yes" : "no";          return value ? "yes" : "no";
198  }  }
199    
200    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
201    {
202            va_list args;
203            const int pos = strlen(buffer);
204            va_start(args, fmt);
205            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
206            va_end(args);
207    }
208    
209  /**  /**
210   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
211   *   *
212   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
213   *   *
214   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
215     */
216    static bool ccs_flush(struct ccs_io_buffer *head)
217    {
218            while (head->r.w_pos) {
219                    const char *w = head->r.w[0];
220                    int len = strlen(w);
221                    if (len) {
222                            if (len > head->read_user_buf_avail)
223                                    len = head->read_user_buf_avail;
224                            if (!len)
225                                    return false;
226                            if (copy_to_user(head->read_user_buf, w, len))
227                                    return false;
228                            head->read_user_buf_avail -= len;
229                            head->read_user_buf += len;
230                            w += len;
231                    }
232                    if (*w) {
233                            head->r.w[0] = w;
234                            return false;
235                    }
236                    /* Add '\0' for audit logs and query. */
237                    if (head->poll) {
238                            if (!head->read_user_buf_avail ||
239                                copy_to_user(head->read_user_buf, "", 1))
240                                    return false;
241                            head->read_user_buf_avail--;
242                            head->read_user_buf++;
243                    }
244                    head->r.w_pos--;
245                    for (len = 0; len < head->r.w_pos; len++)
246                            head->r.w[len] = head->r.w[len + 1];
247            }
248            head->r.avail = 0;
249            return true;
250    }
251    
252    /**
253     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
254     *
255     * @head:   Pointer to "struct ccs_io_buffer".
256     * @string: String to print.
257     *
258     * Note that @string has to be kept valid until @head is kfree()d.
259     * This means that char[] allocated on stack memory cannot be passed to
260     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
261     */
262    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
263    {
264            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
265                    head->r.w[head->r.w_pos++] = string;
266                    ccs_flush(head);
267            } else
268                    printk(KERN_WARNING "Too many words in a line.\n");
269    }
270    
271    /**
272     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
273   *   *
274   * The snprintf() will truncate, but ccs_io_printf() won't.   * @head: Pointer to "struct ccs_io_buffer".
275     * @fmt:  The printf()'s format string, followed by parameters.
276   */   */
277  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
278  {  {
279          va_list args;          va_list args;
280          int len;          int len;
281          int pos = head->read_avail;          int pos = head->r.avail;
282          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
283          if (size <= 0)          if (size <= 0)
284                  return false;                  return;
285          va_start(args, fmt);          va_start(args, fmt);
286          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
287          va_end(args);          va_end(args);
288          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
289                  return false;                  printk(KERN_WARNING "Too many words in a line.\n");
290          head->read_avail += len;                  return;
291          return true;          }
292            head->r.avail += len;
293            ccs_set_string(head, head->read_buf + pos);
294    }
295    
296    static void ccs_set_space(struct ccs_io_buffer *head)
297    {
298            ccs_set_string(head, " ");
299    }
300    
301    static bool ccs_set_lf(struct ccs_io_buffer *head)
302    {
303            ccs_set_string(head, "\n");
304            return !head->r.w_pos;
305  }  }
306    
307  /**  /**
308   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
309   *   *
310   * @profile: Profile number to create.   * @profile: Profile number to create.
311   *   *
312   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
313   */   */
314  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)  
315  {  {
316          struct ccs_profile *ptr;          struct ccs_profile *ptr;
317          struct ccs_profile *entry;          struct ccs_profile *entry;
# Line 260  static struct ccs_profile *ccs_find_or_a Line 320  static struct ccs_profile *ccs_find_or_a
320          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
321          if (ptr)          if (ptr)
322                  return ptr;                  return ptr;
323          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
324          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
325                    goto out;
326          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
327          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
328                  ptr = entry;                  ptr = entry;
# Line 278  static struct ccs_profile *ccs_find_or_a Line 339  static struct ccs_profile *ccs_find_or_a
339                  entry = NULL;                  entry = NULL;
340          }          }
341          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
342     out:
343          kfree(entry);          kfree(entry);
344          return ptr;          return ptr;
345  }  }
# Line 285  static struct ccs_profile *ccs_find_or_a Line 347  static struct ccs_profile *ccs_find_or_a
347  /**  /**
348   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
349   */   */
350  void ccs_check_profile(void)  static void ccs_check_profile(void)
351  {  {
352          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
353            const int idx = ccs_read_lock();
354          ccs_policy_loaded = true;          ccs_policy_loaded = true;
355          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
356                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
# Line 296  void ccs_check_profile(void) Line 359  void ccs_check_profile(void)
359                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
360                        profile, domain->domainname->name);                        profile, domain->domainname->name);
361          }          }
362            ccs_read_unlock(idx);
363            if (ccs_profile_version != 20090903)
364                    panic("Profile version %u is not supported.\n",
365                          ccs_profile_version);
366            printk(KERN_INFO "CCSecurity: 1.7.2+   2010/07/21\n");
367            printk(KERN_INFO "Mandatory Access Control activated.\n");
368  }  }
369    
370  /**  /**
# Line 314  struct ccs_profile *ccs_profile(const u8 Line 383  struct ccs_profile *ccs_profile(const u8
383          return ptr;          return ptr;
384  }  }
385    
386  /**  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)  
387  {  {
388          char *data = head->write_buf;          const char *cp = strstr(string, find);
389          unsigned int i;          if (cp) {
390          int value;                  cp += strlen(find);
391          int mode;                  if (!strncmp(cp, "=yes", 4))
392          u8 config;                          return 1;
393          bool use_default = false;                  else if (!strncmp(cp, "=no", 3))
394          char *cp;                          return 0;
         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;  
395          }          }
396          cp = strchr(data, '=');          return -1;
397          if (!cp)  }
398                  return -EINVAL;  
399          *cp++ = '\0';  static void ccs_set_bool(bool *b, const char *string, const char *find)
400          if (profile != &ccs_default_profile)  {
401                  use_default = strstr(cp, "use_default") != NULL;          switch (ccs_find_yesno(string, find)) {
402          if (strstr(cp, "verbose=yes"))          case 1:
403                  value = 1;                  *b = true;
404          else if (strstr(cp, "verbose=no"))                  break;
405                  value = 0;          case 0:
406          else                  *b = false;
407                  value = -1;                  break;
408          if (!strcmp(data, "PREFERENCE::audit")) {          }
409  #ifdef CONFIG_CCSECURITY_AUDIT  }
410                  char *cp2;  
411  #endif  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
412    {
413            const char *cp = strstr(string, find);
414            if (cp)
415                    sscanf(cp + strlen(find), "=%u", i);
416    }
417    
418    static void ccs_set_pref(const char *name, const char *value,
419                             const bool use_default, struct ccs_profile *profile)
420    {
421            struct ccs_preference **pref;
422            bool *verbose;
423            if (!strcmp(name, "audit")) {
424                  if (use_default) {                  if (use_default) {
425                          profile->audit = &ccs_default_profile.preference;                          pref = &profile->audit;
426                          return 0;                          goto set_default;
427                  }                  }
428                  profile->audit = &profile->preference;                  profile->audit = &profile->preference;
429  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
430                  cp2 = strstr(cp, "max_grant_log=");                  ccs_set_uint(&profile->preference.audit_max_grant_log, value,
431                  if (cp2)                               "max_grant_log");
432                          sscanf(cp2 + 14, "%u",                  ccs_set_uint(&profile->preference.audit_max_reject_log, value,
433                                 &profile->preference.audit_max_grant_log);                               "max_reject_log");
                 cp2 = strstr(cp, "max_reject_log=");  
                 if (cp2)  
                         sscanf(cp2 + 15, "%u",  
                                &profile->preference.audit_max_reject_log);  
434  #endif  #endif
435                  if (strstr(cp, "task_info=yes"))                  ccs_set_bool(&profile->preference.audit_task_info, value,
436                          profile->preference.audit_task_info = true;                               "task_info");
437                  else if (strstr(cp, "task_info=no"))                  ccs_set_bool(&profile->preference.audit_path_info, value,
438                          profile->preference.audit_task_info = false;                               "path_info");
439                  if (strstr(cp, "path_info=yes"))                  return;
                         profile->preference.audit_path_info = true;  
                 else if (strstr(cp, "path_info=no"))  
                         profile->preference.audit_path_info = false;  
                 return 0;  
440          }          }
441          if (!strcmp(data, "PREFERENCE::enforcing")) {          if (!strcmp(name, "enforcing")) {
                 char *cp2;  
442                  if (use_default) {                  if (use_default) {
443                          profile->enforcing = &ccs_default_profile.preference;                          pref = &profile->enforcing;
444                          return 0;                          goto set_default;
445                  }                  }
446                  profile->enforcing = &profile->preference;                  profile->enforcing = &profile->preference;
447                  if (value >= 0)                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
448                          profile->preference.enforcing_verbose = value;                               "penalty");
449                  cp2 = strstr(cp, "penalty=");                  verbose = &profile->preference.enforcing_verbose;
450                  if (cp2)                  goto set_verbose;
                         sscanf(cp2 + 8, "%u",  
                                &profile->preference.enforcing_penalty);  
                 return 0;  
451          }          }
452          if (!strcmp(data, "PREFERENCE::permissive")) {          if (!strcmp(name, "permissive")) {
453                  if (use_default) {                  if (use_default) {
454                          profile->permissive = &ccs_default_profile.preference;                          pref = &profile->permissive;
455                          return 0;                          goto set_default;
456                  }                  }
457                  profile->permissive = &profile->preference;                  profile->permissive = &profile->preference;
458                  if (value >= 0)                  verbose = &profile->preference.permissive_verbose;
459                          profile->preference.permissive_verbose = value;                  goto set_verbose;
                 return 0;  
460          }          }
461          if (!strcmp(data, "PREFERENCE::learning")) {          if (!strcmp(name, "learning")) {
                 char *cp2;  
462                  if (use_default) {                  if (use_default) {
463                          profile->learning = &ccs_default_profile.preference;                          pref = &profile->learning;
464                          return 0;                          goto set_default;
465                  }                  }
466                  profile->learning = &profile->preference;                  profile->learning = &profile->preference;
467                  if (value >= 0)                  ccs_set_uint(&profile->preference.learning_max_entry, value,
468                          profile->preference.learning_verbose = value;                               "max_entry");
469                  cp2 = strstr(cp, "max_entry=");                  ccs_set_bool(&profile->preference.learning_exec_realpath,
470                  if (cp2)                               value, "exec.realpath");
471                          sscanf(cp2 + 10, "%u",                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
472                                 &profile->preference.learning_max_entry);                               "exec.argv0");
473                  if (strstr(cp, "exec.realpath=yes"))                  ccs_set_bool(&profile->preference.learning_symlink_target,
474                          profile->preference.learning_exec_realpath = true;                               value, "symlink.target");
475                  else if (strstr(cp, "exec.realpath=no"))                  verbose = &profile->preference.learning_verbose;
476                          profile->preference.learning_exec_realpath = false;                  goto set_verbose;
477                  if (strstr(cp, "exec.argv0=yes"))          }
478                          profile->preference.learning_exec_argv0 = true;          return;
479                  else if (strstr(cp, "exec.argv0=no"))   set_default:
480                          profile->preference.learning_exec_argv0 = false;          *pref = &ccs_default_profile.preference;
481                  if (strstr(cp, "symlink.target=yes"))          return;
482                          profile->preference.learning_symlink_target = true;   set_verbose:
483                  else if (strstr(cp, "symlink.target=no"))          ccs_set_bool(verbose, value, "verbose");
484                          profile->preference.learning_symlink_target = false;  }
485                  return 0;  
486          }  static int ccs_set_mode(char *name, const char *value, const bool use_default,
487          if (profile == &ccs_default_profile)                          struct ccs_profile *profile)
488                  return -EINVAL;  {
489          if (!strcmp(data, "COMMENT")) {          u8 i;
490                  const struct ccs_path_info *new_comment = ccs_get_name(cp);          u8 config;
491                  const struct ccs_path_info *old_comment;          if (!strcmp(name, "CONFIG")) {
                 /* Protect reader from ccs_put_name(). */  
                 spin_lock(&ccs_profile_comment_lock);  
                 old_comment = profile->comment;  
                 profile->comment = new_comment;  
                 spin_unlock(&ccs_profile_comment_lock);  
                 ccs_put_name(old_comment);  
                 return 0;  
         }  
         if (!strcmp(data, "CONFIG")) {  
492                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
493                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
494                  config = profile->default_config;                  config = profile->default_config;
495          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
496                  config = 0;                  config = 0;
497                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
498                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
499                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
500                                  continue;                                  continue;
501                          config = profile->config[i];                          config = profile->config[i];
502                          break;                          break;
# Line 470  static int ccs_write_profile(struct ccs_ Line 510  static int ccs_write_profile(struct ccs_
510          if (use_default) {          if (use_default) {
511                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
512          } else {          } else {
513                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
514                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
515                            if (strstr(value, ccs_mode[mode]))
516                                  /*                                  /*
517                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
518                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
# Line 479  static int ccs_write_profile(struct ccs_ Line 520  static int ccs_write_profile(struct ccs_
520                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
521  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
522                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
523                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "grant_log")) {
524                            case 1:
525                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
526                          else if (strstr(cp, "grant_log=no"))                                  break;
527                            case 0:
528                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
529                          if (strstr(cp, "reject_log=yes"))                                  break;
530                            }
531                            switch (ccs_find_yesno(value, "reject_log")) {
532                            case 1:
533                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
534                          else if (strstr(cp, "reject_log=no"))                                  break;
535                            case 0:
536                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
537                                    break;
538                            }
539                  }                  }
540  #endif  #endif
541          }          }
# Line 499  static int ccs_write_profile(struct ccs_ Line 548  static int ccs_write_profile(struct ccs_
548  }  }
549    
550  /**  /**
551   * ccs_read_profile - Read profile table.   * ccs_write_profile - Write profile table.
552   *   *
553   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
554     *
555     * Returns 0 on success, negative value otherwise.
556   */   */
557  static void ccs_read_profile(struct ccs_io_buffer *head)  static int ccs_write_profile(struct ccs_io_buffer *head)
558  {  {
559          int index;          char *data = head->write_buf;
560          if (head->read_eof)          unsigned int i;
561                  return;          bool use_default = false;
562          if (head->read_bit)          char *cp;
563                  goto body;          struct ccs_profile *profile;
564          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
565          ccs_io_printf(head, "PREFERENCE::audit={ "                  return 0;
566            i = simple_strtoul(data, &cp, 10);
567            if (data == cp) {
568                    profile = &ccs_default_profile;
569            } else {
570                    if (*cp != '-')
571                            return -EINVAL;
572                    data = cp + 1;
573                    profile = ccs_assign_profile(i);
574                    if (!profile)
575                            return -EINVAL;
576            }
577            cp = strchr(data, '=');
578            if (!cp)
579                    return -EINVAL;
580            *cp++ = '\0';
581            if (profile != &ccs_default_profile)
582                    use_default = strstr(cp, "use_default") != NULL;
583            if (ccs_str_starts(&data, "PREFERENCE::")) {
584                    ccs_set_pref(data, cp, use_default, profile);
585                    return 0;
586            }
587            if (profile == &ccs_default_profile)
588                    return -EINVAL;
589            if (!strcmp(data, "COMMENT")) {
590                    const struct ccs_path_info *old_comment = profile->comment;
591                    profile->comment = ccs_get_name(cp);
592                    ccs_put_name(old_comment);
593                    return 0;
594            }
595            return ccs_set_mode(data, cp, use_default, profile);
596    }
597    
598    static void ccs_print_preference(struct ccs_io_buffer *head, const int idx)
599    {
600            struct ccs_preference *pref = &ccs_default_profile.preference;
601            const struct ccs_profile *profile = idx >= 0 ?
602                    ccs_profile_ptr[idx] : NULL;
603            char buffer[16] = "";
604            if (profile) {
605                    buffer[sizeof(buffer) - 1] = '\0';
606                    snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
607            }
608            if (profile) {
609                    pref = profile->audit;
610                    if (pref == &ccs_default_profile.preference)
611                            goto skip0;
612            }
613            ccs_io_printf(head, "%sPREFERENCE::%s={ "
614  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
615                        "max_grant_log=%u max_reject_log=%u "                        "max_grant_log=%u max_reject_log=%u "
616  #endif  #endif
617                        "task_info=%s path_info=%s }\n",                        "task_info=%s path_info=%s }\n", buffer,
618  #ifdef CONFIG_CCSECURITY_AUDIT                        "audit",
                       ccs_default_profile.preference.audit_max_grant_log,  
                       ccs_default_profile.preference.audit_max_reject_log,  
 #endif  
                       ccs_yesno(ccs_default_profile.preference.  
                                 audit_task_info),  
                       ccs_yesno(ccs_default_profile.preference.  
                                 audit_path_info));  
         ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "  
                       "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",  
                       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;  
619  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
620                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "                        pref->audit_max_grant_log,
621                                     "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;  
622  #endif  #endif
623                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                        ccs_yesno(pref->audit_task_info),
624                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                        ccs_yesno(pref->audit_path_info));
625     skip0:
626            if (profile) {
627                    pref = profile->learning;
628                    if (pref == &ccs_default_profile.preference)
629                            goto skip1;
630            }
631            ccs_io_printf(head, "%sPREFERENCE::%s={ "
632                          "verbose=%s max_entry=%u exec.realpath=%s "
633                          "exec.argv0=%s symlink.target=%s }\n",
634                          buffer, "learning",
635                          ccs_yesno(pref->learning_verbose),
636                          pref->learning_max_entry,
637                          ccs_yesno(pref->learning_exec_realpath),
638                          ccs_yesno(pref->learning_exec_argv0),
639                          ccs_yesno(pref->learning_symlink_target));
640     skip1:
641            if (profile) {
642                    pref = profile->permissive;
643                    if (pref == &ccs_default_profile.preference)
644                            goto skip2;
645            }
646            ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
647                          buffer, "permissive",
648                          ccs_yesno(pref->permissive_verbose));
649     skip2:
650            if (profile) {
651                    pref = profile->enforcing;
652                    if (pref == &ccs_default_profile.preference)
653                            return;
654            }
655            ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
656                          "penalty=%u }\n", buffer, "enforcing",
657                          ccs_yesno(pref->enforcing_verbose),
658                          pref->enforcing_penalty);
659    }
660    
661    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
662    {
663            ccs_io_printf(head, "={ mode=%s", ccs_mode[config & 3]);
664  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
665                          const char *g;          ccs_io_printf(head, " grant_log=%s reject_log=%s",
666                          const char *r;                        ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
667                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
668  #endif  #endif
669                          config = profile->config[i];          ccs_set_string(head, " }\n");
670    }
671    
672    /**
673     * ccs_read_profile - Read profile table.
674     *
675     * @head: Pointer to "struct ccs_io_buffer".
676     */
677    static void ccs_read_profile(struct ccs_io_buffer *head)
678    {
679            u8 index;
680            const struct ccs_profile *profile;
681     next:
682            index = head->r.index;
683            profile = ccs_profile_ptr[index];
684            switch (head->r.step) {
685            case 0:
686                    ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
687                    ccs_print_preference(head, -1);
688                    head->r.step++;
689                    break;
690            case 1:
691                    for ( ; head->r.index < CCS_MAX_PROFILES;
692                          head->r.index++)
693                            if (ccs_profile_ptr[head->r.index])
694                                    break;
695                    if (head->r.index == CCS_MAX_PROFILES)
696                            return;
697                    head->r.step++;
698                    break;
699            case 2:
700                    {
701                            const struct ccs_path_info *comment = profile->comment;
702                            ccs_io_printf(head, "%u-COMMENT=", index);
703                            ccs_set_string(head, comment ? comment->name : "");
704                            ccs_set_lf(head);
705                            head->r.step++;
706                    }
707                    break;
708            case 3:
709                    {
710                            ccs_io_printf(head, "%u-%s", index, "CONFIG");
711                            ccs_print_config(head, profile->default_config);
712                            head->r.bit = 0;
713                            head->r.step++;
714                    }
715                    break;
716            case 4:
717                    for ( ; head->r.bit < CCS_MAX_MAC_INDEX
718                                  + CCS_MAX_CAPABILITY_INDEX
719                                  + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
720                            const u8 i = head->r.bit;
721                            const u8 config = profile->config[i];
722                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
723                                  continue;                                  continue;
724  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
725                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                        ccs_mac_keywords[i]);
726                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          ccs_print_config(head, config);
727                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                          head->r.bit++;
728                                             "grant_log=%s reject_log=%s }\n",                          break;
729                                             index, ccs_mac_keywords[i],                  }
730                                             ccs_mode_4[config & 3], g, r))                  if (head->r.bit == CCS_MAX_MAC_INDEX
731                                  goto out;                      + CCS_MAX_CAPABILITY_INDEX
732  #else                      + CCS_MAX_MAC_CATEGORY_INDEX) {
733                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          ccs_print_preference(head, index);
734                                             index, ccs_mac_keywords[i],                          head->r.index++;
735                                             ccs_mode_4[config & 3]))                          head->r.step = 1;
                                 goto out;  
 #endif  
736                  }                  }
                 if (profile->audit != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::audit={ "  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    "max_grant_log=%u max_reject_log=%u "  
 #endif  
                                    "task_info=%s path_info=%s }\n", index,  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                                    profile->preference.audit_max_grant_log,  
                                    profile->preference.audit_max_reject_log,  
 #endif  
                                    ccs_yesno(profile->preference.  
                                              audit_task_info),  
                                    ccs_yesno(profile->preference.  
                                              audit_path_info)))  
                         goto out;  
                 if (profile->learning != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::learning={ "  
                                    "verbose=%s max_entry=%u exec.realpath=%s "  
                                    "exec.argv0=%s symlink.target=%s }\n",  
                                    index,  
                                    ccs_yesno(profile->preference.  
                                              learning_verbose),  
                                    profile->preference.learning_max_entry,  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_realpath),  
                                    ccs_yesno(profile->preference.  
                                              learning_exec_argv0),  
                                    ccs_yesno(profile->preference.  
                                              learning_symlink_target)))  
                         goto out;  
                 if (profile->permissive != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::permissive={ "  
                                    "verbose=%s }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              permissive_verbose)))  
                         goto out;  
                 if (profile->enforcing != &ccs_default_profile.preference &&  
                     !ccs_io_printf(head, "%u-PREFERENCE::enforcing={ "  
                                    "verbose=%s penalty=%u }\n", index,  
                                    ccs_yesno(profile->preference.  
                                              enforcing_verbose),  
                                    profile->preference.enforcing_penalty))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
737                  break;                  break;
738          }          }
739          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
740                  head->read_eof = true;                  goto next;
741  }  }
742    
743  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager(const struct ccs_acl_head *a,
744  LIST_HEAD(ccs_policy_manager_list);                               const struct ccs_acl_head *b)
745    {
746            return container_of(a, struct ccs_manager, head)->manager
747                    == container_of(b, struct ccs_manager, head)->manager;
748    }
749    
750  /**  /**
751   * ccs_update_manager_entry - Add a manager entry.   * ccs_update_manager_entry - Add a manager entry.
# Line 666  LIST_HEAD(ccs_policy_manager_list); Line 757  LIST_HEAD(ccs_policy_manager_list);
757   */   */
758  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)
759  {  {
760          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
761          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
762          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
763                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
764                          return -EINVAL;                          return -EINVAL;
765                  e.is_domain = true;                  e.is_domain = true;
766          } else {          } else {
767                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
768                          return -EINVAL;                          return -EINVAL;
769          }          }
770          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
771          if (!e.manager)          if (!e.manager)
772                  return -ENOMEM;                  return error;
773          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
774                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
775          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);  
776          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
777          return error;          return error;
778  }  }
779    
780  /**  /**
781   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
782   *   *
783   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
784   *   *
785   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
786   */   */
787  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
788  {  {
789          char *data = head->write_buf;          char *data = head->write_buf;
790          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
# Line 721  static int ccs_write_manager_policy(stru Line 796  static int ccs_write_manager_policy(stru
796  }  }
797    
798  /**  /**
799   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
800   *   *
801   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
802   *   *
803   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
804   */   */
805  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
806  {  {
807          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
808                  return;                  return;
809          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]) {
810                  struct ccs_policy_manager_entry *ptr;                  struct ccs_manager *ptr =
811                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                          list_entry(head->r.acl, typeof(*ptr), head.list);
812                  if (ptr->is_deleted)                  if (ptr->head.is_deleted)
813                          continue;                          continue;
814                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
815                          return;                          return;
816                    ccs_set_string(head, ptr->manager->name);
817                    ccs_set_lf(head);
818          }          }
819          head->read_eof = true;          head->r.eof = true;
820  }  }
821    
822  /**  /**
823   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
824   *   *
825   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
826   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
827   *   *
828   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
829   */   */
830  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
831  {  {
832          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
833          const char *exe;          const char *exe;
834          struct task_struct *task = current;          struct task_struct *task = current;
835          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
# Line 761  static bool ccs_is_policy_manager(void) Line 837  static bool ccs_is_policy_manager(void)
837          bool found = false;          bool found = false;
838          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
839                  return true;                  return true;
840          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
841                  return true;                  return true;
842          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
843                  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;  
                 }  
         }  
844          exe = ccs_get_exe();          exe = ccs_get_exe();
845          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
846                  return false;                                  head.list) {
847          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
848                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
849                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
850                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
851                          /* Set manager flag. */                                  continue;
852                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
853                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
854                                    continue;
855                  }                  }
856                    /* Set manager flag. */
857                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
858                    found = true;
859                    break;
860          }          }
861          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
862                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 809  static bool ccs_is_policy_manager(void) Line 882  static bool ccs_is_policy_manager(void)
882  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
883  {  {
884          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
885          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
886                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
887                  if (cp)          if (cp)
888                          *cp++ = '\0';                  *cp++ = '\0';
         }  
889          return cp;          return cp;
890  }  }
891    
892  /**  /**
893   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
894   *   *
895   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
896   * @data: String to parse.   * @data: String to parse.
# Line 835  static char *ccs_find_condition_part(cha Line 899  static char *ccs_find_condition_part(cha
899   *   *
900   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
901   */   */
902  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)
903  {  {
904          unsigned int pid;          unsigned int pid;
905          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
906          bool global_pid = false;          bool global_pid = false;
907          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "execute")) {
908                  head->read_execute_only = true;                  head->r.print_execute_only = true;
909                  return true;                  return true;
910          }          }
911          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
912              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
913                  struct task_struct *p;                  struct task_struct *p;
914                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
915  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
916                  if (global_pid)                  if (global_pid)
917                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
918                                                                   &init_pid_ns);
919                  else                  else
920                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
921  #else  #else
922                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
923  #endif  #endif
924                  if (p)                  if (p)
925                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
926                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
927          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
928                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
929                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
930          } else          } else
931                  return false;                  return false;
932          head->write_var1 = domain;          head->w.domain = domain;
933          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
934          if (!head->read_buf)          if (!head->read_buf)
935                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
936          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
937            head->r.print_this_domain_only = true;
938            head->r.eof = !domain;
939            head->r.domain = &domain->list;
940          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
941          head->read_single_domain = true;          if (domain && domain->is_deleted)
942          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");  
         }  
943          return true;          return true;
944  }  }
945    
946  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,
947                                      struct ccs_condition *cond,                               const bool is_delete)
948                                      const bool is_delete)  {
949  {          static const struct {
950          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))                  const char *keyword;
951                  return ccs_write_capability_policy(data, domain, cond,                  int (*write) (char *, struct ccs_domain_info *,
952                                                     is_delete);                                struct ccs_condition *, const bool);
953          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))          } ccs_callback[4] = {
954                  return ccs_write_network_policy(data, domain, cond, is_delete);                  { "network ", ccs_write_network },
955          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                  { "misc ", ccs_write_misc },
956                  return ccs_write_signal_policy(data, domain, cond, is_delete);                  { "capability ", ccs_write_capability },
957          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { "ipc ", ccs_write_ipc },
958                  return ccs_write_env_policy(data, domain, cond, is_delete);          };
959          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
960                  return ccs_write_mount_policy(data, domain, cond, is_delete);                        const bool) = ccs_write_file;
961          return ccs_write_file_policy(data, domain, cond, is_delete);          int error;
962            u8 i;
963            struct ccs_condition *cond = NULL;
964            char *cp = ccs_find_condition_part(data);
965            if (cp) {
966                    cond = ccs_get_condition(cp);
967                    if (!cond)
968                            return -EINVAL;
969            }
970            for (i = 0; i < 4; i++) {
971                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
972                            continue;
973                    write = ccs_callback[i].write;
974                    break;
975            }
976            error = write(data, domain, cond, is_delete);
977            if (cond)
978                    ccs_put_condition(cond);
979            return error;
980  }  }
981    
982    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
983            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
984            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
985    };
986            
987  /**  /**
988   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
989   *   *
990   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
991   *   *
992   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
993   */   */
994  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
995  {  {
996          char *data = head->write_buf;          char *data = head->write_buf;
997          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
998          bool is_delete = false;          bool is_delete = false;
999          bool is_select = false;          bool is_select = false;
1000          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
1001          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
1002                  is_delete = true;                  is_delete = true;
1003          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1004                  is_select = true;                  is_select = true;
1005          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1006                  return 0;                  return 0;
1007          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1008          if (!ccs_is_policy_manager())          if (!ccs_manager())
1009                  return -EPERM;                  return -EPERM;
1010          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1011                  domain = NULL;                  domain = NULL;
1012                  if (is_delete)                  if (is_delete)
1013                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1014                  else if (is_select)                  else if (is_select)
1015                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1016                  else                  else
1017                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0, 0);
1018                  head->write_var1 = domain;                  head->w.domain = domain;
1019                  return 0;                  return 0;
1020          }          }
1021          if (!domain)          if (!domain)
# Line 953  static int ccs_write_domain_policy(struc Line 1027  static int ccs_write_domain_policy(struc
1027                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1028                  return 0;                  return 0;
1029          }          }
1030          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          if (sscanf(data, CCS_KEYWORD_USE_GROUP "%u", &profile) == 1
1031                  domain->ignore_global_allow_read = !is_delete;              && profile < CCS_MAX_ACL_GROUPS) {
1032                    domain->group = (u8) profile;
1033                  return 0;                  return 0;
1034          }          }
1035          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1036                  domain->ignore_global_allow_env = !is_delete;                  const char *cp = ccs_dif[profile];
1037                    if (strncmp(data, cp, strlen(cp) - 1))
1038                            continue;
1039                    domain->flags[profile] = !is_delete;
1040                  return 0;                  return 0;
1041          }          }
1042          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;  
1043  }  }
1044    
1045  /**  /**
# Line 978  static int ccs_write_domain_policy(struc Line 1047  static int ccs_write_domain_policy(struc
1047   *   *
1048   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1049   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1050   */   */
1051  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1052                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1053  {  {
1054          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1055          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1056                  head->read_avail--;                  ccs_set_space(head);
1057          if (ptr->is_group)          if (ptr->is_group) {
1058                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1059                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1060          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1061  }                  if (cond)
1062                            ccs_set_string(head, "\"");
1063  /**                  ccs_set_string(head, ptr->filename->name);
1064   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1065   *                          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);  
1066          }          }
1067  }  }
1068    
1069  /**  /**
1070   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1071   *   *
1072   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1073   * @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.  
1074   */   */
1075  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1076                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1077  {  {
1078          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
1079                    ccs_set_space(head);
1080            if (ptr->is_group) {
1081                    ccs_set_string(head, "@");
1082                    ccs_set_string(head, ptr->group->group_name->name);
1083            } else {
1084                    int i;
1085                    unsigned long min = ptr->values[0];
1086                    const unsigned long max = ptr->values[1];
1087                    u8 min_type = ptr->value_type[0];
1088                    const u8 max_type = ptr->value_type[1];
1089                    char buffer[128];
1090                    buffer[0] = '\0';
1091                    for (i = 0; i < 2; i++) {
1092                            switch (min_type) {
1093                            case CCS_VALUE_TYPE_HEXADECIMAL:
1094                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1095                                                  min);
1096                                    break;
1097                            case CCS_VALUE_TYPE_OCTAL:
1098                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1099                                                  min);
1100                                    break;
1101                            default:
1102                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1103                                                  min);
1104                                    break;
1105                            }
1106                            if (min == max && min_type == max_type)
1107                                    break;
1108                            ccs_addprintf(buffer, sizeof(buffer), "-");
1109                            min_type = max_type;
1110                            min = max;
1111                    }
1112                    ccs_io_printf(head, "%s", buffer);
1113            }
1114  }  }
1115    
1116  /**  /**
1117   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1118   *   *
1119   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1120   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1121   *   *
1122   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1123   */   */
1124  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1125                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1126  {  {
1127          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1128          const struct ccs_number_union *numbers_p;          case 0:
1129          const struct ccs_name_union *names_p;                  {
1130          const struct ccs_argv_entry *argv;                          if (cond->condc)
1131          const struct ccs_envp_entry *envp;                                  ccs_set_string(head, " if");
1132          u16 condc;                          head->r.cond_index = 0;
1133          u16 i;                          head->r.cond_step++;
1134          u16 j;                  }
1135          char buffer[32];                  /* fall through */
1136          if (!cond)          case 1:
1137                  goto no_condition;                  {
1138          condc = cond->condc;                          const u16 condc = cond->condc;
1139          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_condition_element *condp =
1140          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(condp)) (cond + 1);
1141          names_p = (const struct ccs_name_union *)                          const struct ccs_number_union *numbers_p =
1142                  (numbers_p + cond->numbers_count);                                  (typeof(numbers_p)) (condp + condc);
1143          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                          const struct ccs_name_union *names_p =
1144          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                                  (typeof(names_p))
1145          memset(buffer, 0, sizeof(buffer));                                  (numbers_p + cond->numbers_count);
1146          if (condc && !ccs_io_printf(head, "%s", " if"))                          const struct ccs_argv *argv =
1147                  goto out;                                  (typeof(argv)) (names_p + cond->names_count);
1148          for (i = 0; i < condc; i++) {                          const struct ccs_envp *envp =
1149                  const u8 match = condp->equals;                                  (typeof(envp)) (argv + cond->argc);
1150                  const u8 left = condp->left;                          u16 skip;
1151                  const u8 right = condp->right;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1152                  condp++;                                  const u8 left = condp->left;
1153                  switch (left) {                                  const u8 right = condp->right;
1154                  case CCS_ARGV_ENTRY:                                  condp++;
1155                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  switch (left) {
1156                                             argv->index, argv->is_not ?                                  case CCS_ARGV_ENTRY:
1157                                             "!=" : "=", argv->value->name))                                          argv++;
1158                                  goto out;                                          continue;
1159                          argv++;                                  case CCS_ENVP_ENTRY:
1160                          continue;                                          envp++;
1161                  case CCS_ENVP_ENTRY:                                          continue;
1162                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  case CCS_NUMBER_UNION:
1163                                             envp->name->name, envp->is_not ?                                          numbers_p++;
1164                                             "!=" : "="))                                          break;
1165                                  goto out;                                  }
1166                          if (envp->value) {                                  switch (right) {
1167                                  if (!ccs_io_printf(head, "\"%s\"",                                  case CCS_NAME_UNION:
1168                                                     envp->value->name))                                          names_p++;
1169                                          goto out;                                          break;
1170                          } else {                                  case CCS_NUMBER_UNION:
1171                                  if (!ccs_io_printf(head, "NULL"))                                          numbers_p++;
1172                                          goto out;                                          break;
1173                                    }
1174                            }
1175                            while (head->r.cond_index < condc) {
1176                                    const u8 match = condp->equals;
1177                                    const u8 left = condp->left;
1178                                    const u8 right = condp->right;
1179                                    if (!ccs_flush(head))
1180                                            return false;
1181                                    condp++;
1182                                    head->r.cond_index++;
1183                                    ccs_set_space(head);
1184                                    switch (left) {
1185                                    case CCS_ARGV_ENTRY:
1186                                            ccs_io_printf(head,
1187                                                          "exec.argv[%u]%s\"%s\"",
1188                                                          argv->index,
1189                                                          argv->is_not ?
1190                                                          "!=" : "=",
1191                                                          argv->value->name);
1192                                            argv++;
1193                                            continue;
1194                                    case CCS_ENVP_ENTRY:
1195                                            ccs_io_printf(head,
1196                                                          "exec.envp[\"%s\"]%s",
1197                                                          envp->name->name,
1198                                                          envp->is_not ?
1199                                                          "!=" : "=");
1200                                            if (envp->value) {
1201                                                    ccs_set_string(head, "\"");
1202                                                    ccs_set_string(head, envp->
1203                                                                   value->name);
1204                                                    ccs_set_string(head, "\"");
1205                                            } else {
1206                                                    ccs_set_string(head, "NULL");
1207                                            }
1208                                            envp++;
1209                                            continue;
1210                                    case CCS_NUMBER_UNION:
1211                                            ccs_print_number_union(head,
1212                                                                   numbers_p++);
1213                                            break;
1214                                    default:
1215                                            ccs_set_string(head,
1216                                                   ccs_condition_keyword[left]);
1217                                            break;
1218                                    }
1219                                    ccs_set_string(head, match ? "=" : "!=");
1220                                    switch (right) {
1221                                    case CCS_NAME_UNION:
1222                                            ccs_print_name_union(head, names_p++);
1223                                            break;
1224                                    case CCS_NUMBER_UNION:
1225                                            ccs_print_number_union(head,
1226                                                                   numbers_p++);
1227                                            break;
1228                                    default:
1229                                            ccs_set_string(head,
1230                                                   ccs_condition_keyword[right]);
1231                                            break;
1232                                    }
1233                          }                          }
                         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;  
1234                  }                  }
1235                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1236                          goto out;                  /* fall through */
1237                  switch (right) {          case 2:
1238                  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;  
1239                          break;                          break;
1240                    head->r.cond_step++;
1241                    /* fall through */
1242            case 3:
1243                    {
1244                            u8 j;
1245                            const u8 i = cond->post_state[3];
1246                            if (i)
1247                                    ccs_set_string(head, " ; set");
1248                            for (j = 0; j < 3; j++)
1249                                    if ((i & (1 << j)))
1250                                            ccs_io_printf(head,
1251                                                          " task.state[%u]=%u", j,
1252                                                          cond->post_state[j]);
1253                            if (i & (1 << 4))
1254                                    ccs_io_printf(head, " audit=%s",
1255                                                  ccs_yesno(cond->post_state[4]));
1256                  }                  }
1257          }                  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"))  
1258                  return true;                  return true;
  out:  
         return false;  
 }  
   
 /**  
  * ccs_print_path_acl - Print a path ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_acl(struct ccs_io_buffer *head,  
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)  
                         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;  
                 }  
1259          }          }
1260          head->read_bit = 0;          return false;
         return true;  
1261  }  }
1262    
1263  /**  /**
1264   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_fns - Find next set bit.
1265   *   *
1266   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1267   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1268   *   *
1269   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1270   */   */
1271  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                        struct ccs_path_number3_acl *ptr,  
                                        const struct ccs_condition *cond)  
1272  {  {
1273          int pos;          for ( ; bit < 8; bit++)
1274          u8 bit;                  if (perm & (1 << bit))
1275          const u16 perm = ptr->perm;                          break;
1276          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;          return bit;
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number32keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->mode) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1277  }  }
1278    
1279  /**  static void ccs_set_group(struct ccs_io_buffer *head)
  * ccs_print_path2_acl - Print a path2 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
1280  {  {
1281          int pos;          if (head->type == CCS_EXCEPTIONPOLICY)
1282          u8 bit;                  ccs_io_printf(head, "acl_group %u ", head->r.group_index);
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name1) ||  
                     !ccs_print_name_union(head, &ptr->name2) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1283  }  }
1284    
1285  /**  /**
1286   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1287   *   *
1288   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1289   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1290   *   *
1291   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1292   */   */
1293  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1294                                        struct ccs_path_number_acl *ptr,                              const struct ccs_acl_info *acl)
                                       const struct ccs_condition *cond)  
1295  {  {
1296          int pos;          const u8 acl_type = acl->type;
1297          u8 bit;          u8 bit;
1298          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1299          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1300               bit++) {          if (acl->is_deleted)
1301                  if (!(perm & (1 << bit)))                  return true;
1302                          continue;   next:
1303                  pos = head->read_avail;          bit = head->r.bit;
1304                  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))  
1305                  return false;                  return false;
1306          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1307                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1308                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1309                          return false;                  const u16 perm = ptr->perm;
1310          }                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1311          return true;                          if (!(perm & (1 << bit)))
1312  }                                  continue;
1313                            if (head->r.print_execute_only &&
1314  /**                              bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1315   * ccs_print_network_acl - Print a network ACL entry.                                  continue;
1316   *                          break;
1317   * @head: Pointer to "struct ccs_io_buffer".                  }
1318   * @ptr:  Pointer to "struct ccs_ip_network_acl".                  if (bit >= CCS_MAX_PATH_OPERATION)
1319   * @cond: Pointer to "struct ccs_condition". May be NULL.                          goto done;
1320   *                  ccs_set_group(head);
1321   * Returns true on success, false otherwise.                  ccs_set_string(head, "file ");
1322   */                  ccs_set_string(head, ccs_path_keyword[bit]);
1323  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                  ccs_print_name_union(head, &ptr->name);
1324                                    struct ccs_ip_network_acl *ptr,          } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1325                                    const struct ccs_condition *cond)                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1326  {                  struct ccs_execute_handler *ptr
1327          int pos;                          = container_of(acl, typeof(*ptr), head);
1328          u8 bit;                  ccs_set_group(head);
1329          const u16 perm = ptr->perm;                  ccs_io_printf(head, "%s ",
1330          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                                acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1331                  if (!(perm & (1 << bit)))                                CCS_KEYWORD_EXECUTE_HANDLER :
1332                          continue;                                CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1333                  pos = head->read_avail;                  ccs_set_string(head, ptr->handler->name);
1334                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",          } else if (head->r.print_execute_only) {
1335                                     ccs_net2keyword(bit)))                  return true;
1336                          goto out;          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1337                    struct ccs_mkdev_acl *ptr =
1338                            container_of(acl, typeof(*ptr), head);
1339                    bit = ccs_fns(ptr->perm, bit);
1340                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1341                            goto done;
1342                    ccs_set_group(head);
1343                    ccs_set_string(head, "file ");
1344                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1345                    ccs_print_name_union(head, &ptr->name);
1346                    ccs_print_number_union(head, &ptr->mode);
1347                    ccs_print_number_union(head, &ptr->major);
1348                    ccs_print_number_union(head, &ptr->minor);
1349            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1350                    struct ccs_path2_acl *ptr =
1351                            container_of(acl, typeof(*ptr), head);
1352                    bit = ccs_fns(ptr->perm, bit);
1353                    if (bit >= CCS_MAX_PATH2_OPERATION)
1354                            goto done;
1355                    ccs_set_group(head);
1356                    ccs_set_string(head, "file ");
1357                    ccs_set_string(head, ccs_path2_keyword[bit]);
1358                    ccs_print_name_union(head, &ptr->name1);
1359                    ccs_print_name_union(head, &ptr->name2);
1360            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1361                    struct ccs_path_number_acl *ptr =
1362                            container_of(acl, typeof(*ptr), head);
1363                    bit = ccs_fns(ptr->perm, bit);
1364                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1365                            goto done;
1366                    ccs_set_group(head);
1367                    ccs_set_string(head, "file ");
1368                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1369                    ccs_print_name_union(head, &ptr->name);
1370                    ccs_print_number_union(head, &ptr->number);
1371            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1372                    struct ccs_env_acl *ptr =
1373                            container_of(acl, typeof(*ptr), head);
1374                    ccs_set_group(head);
1375                    ccs_set_string(head, "misc env ");
1376                    ccs_set_string(head, ptr->env->name);
1377            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1378                    struct ccs_capability_acl *ptr =
1379                            container_of(acl, typeof(*ptr), head);
1380                    ccs_set_group(head);
1381                    ccs_set_string(head, "capability ");
1382                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1383            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1384                    struct ccs_ip_network_acl *ptr =
1385                            container_of(acl, typeof(*ptr), head);
1386                    bit = ccs_fns(ptr->perm, bit);
1387                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1388                            goto done;
1389                    ccs_set_group(head);
1390                    ccs_set_string(head, "network ");
1391                    ccs_set_string(head, ccs_net_protocol_keyword[ptr->protocol]);
1392                    ccs_set_space(head);
1393                    ccs_set_string(head, ccs_net_keyword[bit]);
1394                    ccs_set_space(head);
1395                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1396                            char buf[128];
1397                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1398                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1399                                             group_name->name))                          ccs_set_string(head,
1400                                  goto out;                                         ptr->address.group->group_name->name);
1401                          break;                          break;
1402                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1403                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1404                                  goto out;                                         ptr->address.ipv4.max);
1405                            ccs_io_printf(head, "%s", buf);
1406                          break;                          break;
1407                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1408                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1409                                  goto out;                                         ptr->address.ipv6.max);
1410                          break;                          ccs_io_printf(head, "%s", buf);
1411                  }                          break;
1412                  if (!ccs_print_number_union(head, &ptr->port) ||                  }
1413                      !ccs_print_condition(head, cond))                  ccs_print_number_union(head, &ptr->port);
1414                          goto out;          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1415                    struct ccs_signal_acl *ptr =
1416                            container_of(acl, typeof(*ptr), head);
1417                    ccs_set_group(head);
1418                    ccs_set_string(head, "ipc signal ");
1419                    ccs_io_printf(head, "%u ", ptr->sig);
1420                    ccs_set_string(head, ptr->domainname->name);
1421            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1422                    struct ccs_mount_acl *ptr =
1423                            container_of(acl, typeof(*ptr), head);
1424                    ccs_set_group(head);
1425                    ccs_io_printf(head, "file mount");
1426                    ccs_print_name_union(head, &ptr->dev_name);
1427                    ccs_print_name_union(head, &ptr->dir_name);
1428                    ccs_print_name_union(head, &ptr->fs_type);
1429                    ccs_print_number_union(head, &ptr->flags);
1430            }
1431            head->r.bit = bit + 1;
1432            if (acl->cond) {
1433                    head->r.print_cond_part = true;
1434                    head->r.cond_step = 0;
1435                    if (!ccs_flush(head))
1436                            return false;
1437     print_cond_part:
1438                    if (!ccs_print_condition(head, acl->cond))
1439                            return false;
1440                    head->r.print_cond_part = false;
1441            } else {
1442                    ccs_set_lf(head);
1443          }          }
1444          head->read_bit = 0;          switch (acl_type) {
1445          return true;          case CCS_TYPE_PATH_ACL:
1446   out:          case CCS_TYPE_MKDEV_ACL:
1447          head->read_bit = bit;          case CCS_TYPE_PATH2_ACL:
1448          head->read_avail = pos;          case CCS_TYPE_PATH_NUMBER_ACL:
1449          return false;          case CCS_TYPE_IP_NETWORK_ACL:
1450  }                  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;  
1451          }          }
1452     done:
1453            head->r.bit = 0;
1454          return true;          return true;
1455  }  }
1456    
1457  /**  /**
1458   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_read_domain2 - Read domain policy.
  *  
  * @head:    Pointer to "struct ccs_io_buffer".  
  * @keyword: Name of the keyword.  
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1459   *   *
1460   * Returns true on success, false otherwise.   * @head:   Pointer to "struct ccs_io_buffer".
1461   */   * @domain: Pointer to "struct ccs_domain_info".
 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.  
1462   *   *
1463   * @head: Pointer to "struct ccs_io_buffer".   * Caller holds ccs_read_lock().
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1464   *   *
1465   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1466   */   */
1467  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1468                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain)
                                 const struct ccs_condition *cond)  
1469  {  {
1470          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1471          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||                  struct ccs_acl_info *ptr =
1472              !ccs_print_name_union(head, &ptr->dev_name) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1473              !ccs_print_name_union(head, &ptr->dir_name) ||                  if (!ccs_print_entry(head, ptr))
1474              !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;  
1475          }          }
1476            head->r.acl = NULL;
1477          return true;          return true;
1478  }  }
1479    
1480  /**  /**
1481   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain - Read domain policy.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const struct ccs_condition *cond = ptr->cond;  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl, head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, struct ccs_path_number3_acl, head);  
                 return ccs_print_path_number3_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, struct ccs_path2_acl, head);  
                 return ccs_print_path2_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, struct ccs_path_number_acl, head);  
                 return ccs_print_path_number_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, struct ccs_env_acl, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, struct ccs_capability_acl, head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, struct ccs_ip_network_acl, head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, struct ccs_signal_acl, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, struct ccs_mount_acl, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
1482   *   *
1483   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1484   *   *
1485   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1486   */   */
1487  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1488  {  {
1489          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1490                  return;                  return;
1491          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1492                  head->read_step = 1;                  struct ccs_domain_info *domain =
1493          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1494                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1495                  const char *quota_exceeded = "";                          u8 i;
1496                  const char *transition_failed = "";                  case 0:
1497                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1498                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1499                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1500                  if (head->read_step != 1)                          /* Print domainname and flags. */
1501                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1502                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1503                          continue;                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1504                  /* Print domainname and flags. */                                        domain->profile);
1505                  if (domain->quota_warned)                          ccs_io_printf(head, CCS_KEYWORD_USE_GROUP "%u\n",
1506                          quota_exceeded = "quota_exceeded\n";                                        domain->group);
1507                  if (domain->domain_transition_failed)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1508                          transition_failed = "transition_failed\n";                                  if (domain->flags[i])
1509                  if (domain->ignore_global_allow_read)                                          ccs_set_string(head, ccs_dif[i]);
1510                          ignore_global_allow_read                          head->r.step++;
1511                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                          ccs_set_lf(head);
1512                  if (domain->ignore_global_allow_env)                          /* fall through */
1513                          ignore_global_allow_env                  case 1:
1514                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          if (!ccs_read_domain2(head, domain))
                 if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"  
                                    "%s%s%s%s\n", domain->domainname->name,  
                                    domain->profile, quota_exceeded,  
                                    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))  
1515                                  return;                                  return;
1516                            head->r.step++;
1517                            if (!ccs_set_lf(head))
1518                                    return;
1519                            /* fall through */
1520                    case 2:
1521                            head->r.step = 0;
1522                            if (head->r.print_this_domain_only)
1523                                    goto done;
1524                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1525          }          }
1526          head->read_eof = true;   done:
1527            head->r.eof = true;
1528  }  }
1529    
1530  /**  /**
# Line 1747  static int ccs_write_domain_profile(stru Line 1575  static int ccs_write_domain_profile(stru
1575   */   */
1576  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1577  {  {
1578          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1579                  return;                  return;
1580          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1581                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1582                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1583                  if (domain->is_deleted)                  if (domain->is_deleted)
1584                          continue;                          continue;
1585                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1586                          return;                          return;
1587                    ccs_io_printf(head, "%u ", domain->profile);
1588                    ccs_set_string(head, domain->domainname->name);
1589                    ccs_set_lf(head);
1590          }          }
1591          head->read_eof = true;          head->r.eof = true;
1592  }  }
1593    
1594  /**  /**
# Line 1771  static void ccs_read_domain_profile(stru Line 1600  static void ccs_read_domain_profile(stru
1600   */   */
1601  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1602  {  {
1603          head->read_eof = false;          head->r.eof = false;
1604          return 0;          return 0;
1605  }  }
1606    
# Line 1796  static void ccs_read_pid(struct ccs_io_b Line 1625  static void ccs_read_pid(struct ccs_io_b
1625          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1626          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1627          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1628          if (!buf)          if (!buf) {
1629                    head->r.eof = true;
1630                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1631          if (head->read_avail || head->read_eof)          }
1632            if (head->r.w_pos || head->r.eof)
1633                  return;                  return;
1634          head->read_eof = true;          head->r.eof = true;
1635          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1636                  task_info = true;                  task_info = true;
1637          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
1638                  global_pid = true;                  global_pid = true;
1639          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1640          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1641  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1642          if (global_pid)          if (global_pid)
1643                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1644          else          else
1645                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1646  #else  #else
1647          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1648  #endif  #endif
# Line 1819  static void ccs_read_pid(struct ccs_io_b Line 1650  static void ccs_read_pid(struct ccs_io_b
1650                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1651                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1652          }          }
1653          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1654          if (!domain)          if (!domain)
1655                  return;                  return;
1656          if (!task_info)          if (!task_info) {
1657                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1658                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1659          else          } else {
1660                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1661                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1662                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1663                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1664                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1665                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1666                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1667                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1668                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1669            }
1670  }  }
1671    
1672    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1673            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1674            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1675            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1676            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1677            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1678    };
1679    
1680    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1681            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1682            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1683            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1684    };
1685    
1686  /**  /**
1687   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1688   *   *
1689   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1690   *   *
1691   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1692   */   */
1693  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1694  {  {
1695          char *data = head->write_buf;          char *data = head->write_buf;
1696          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1697          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1698                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1699          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1700                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1701          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1702                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1703                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1704          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1705                  return ccs_write_domain_initializer_policy(data, true,          };
1706                                                             is_delete);          for (i = 0; i < 3; i++)
1707          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1708                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1709          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1710                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1711          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1712                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1713          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1714                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1715          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1716                  return ccs_write_path_group_policy(data, is_delete);          if (ccs_str_starts(&data, "acl_group ")) {
1717          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  unsigned int group;
1718                  return ccs_write_number_group_policy(data, is_delete);                  if (sscanf(data, "%u", &group) == 1 &&
1719          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                      group < CCS_MAX_ACL_GROUPS) {
1720                  return ccs_write_no_rewrite_policy(data, is_delete);                          data = strchr(data, ' ');
1721          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))                          if (data)
1722                  return ccs_write_address_group_policy(data, is_delete);                                  return ccs_write_domain2(data + 1,
1723          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                                                           &ccs_acl_group[group],
1724                  return ccs_write_reserved_port_policy(data, is_delete);                                                           is_delete);
1725                    }
1726            }
1727          return -EINVAL;          return -EINVAL;
1728  }  }
1729    
1730  /**  /**
1731   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1732   *   *
1733   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1734   *   * @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".  
1735   *   *
1736   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1737     *
1738     * Caller holds ccs_read_lock().
1739   */   */
1740  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1741  {  {
1742          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1743          char *arg_ptr = ee->tmp;                  struct ccs_group *group =
1744          int arg_len = 0;                          list_entry(head->r.group, typeof(*group), head.list);
1745          unsigned long pos = bprm->p;                  list_for_each_cookie(head->r.acl, &group->member_list) {
1746          int offset = pos % PAGE_SIZE;                          struct ccs_acl_head *ptr =
1747          bool done = false;                                  list_entry(head->r.acl, typeof(*ptr), list);
1748          if (!bprm->argc)                          if (ptr->is_deleted)
1749                  goto out;                                  continue;
1750          while (1) {                          if (!ccs_flush(head))
1751                  if (!ccs_dump_page(bprm, pos, &ee->dump))                                  return false;
1752                          goto out;                          ccs_set_string(head, ccs_group_name[idx]);
1753                  pos += PAGE_SIZE - offset;                          ccs_set_string(head, group->group_name->name);
1754                  /* Read. */                          if (idx == CCS_PATH_GROUP) {
1755                  while (offset < PAGE_SIZE) {                                  ccs_set_space(head);
1756                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, container_of
1757                          const unsigned char c = kaddr[offset++];                                                 (ptr, struct ccs_path_group,
1758                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                  head)->member_name->name);
1759                                  if (c == '\\') {                          } else if (idx == CCS_NUMBER_GROUP) {
1760                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_number_union(head, &container_of
1761                                          arg_ptr[arg_len++] = '\\';                                                         (ptr, struct ccs_number_group,
1762                                  } else if (c > ' ' && c < 127) {                                                          head)->number);
1763                                          arg_ptr[arg_len++] = c;                          } else if (idx == CCS_ADDRESS_GROUP) {
1764                                  } else {                                  char buffer[128];
1765                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_address_group *member =
1766                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(ptr, typeof(*member),
1767                                          arg_ptr[arg_len++]                                                       head);
1768                                                  = ((c >> 3) & 7) + '0';                                  if (member->is_ipv6)
1769                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          ccs_print_ipv6(buffer, sizeof(buffer),
1770                                  }                                                         member->min.ipv6,
1771                          } else {                                                         member->max.ipv6);
1772                                  arg_ptr[arg_len] = '\0';                                  else
1773                                  done = true;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1774                                  break;                                                         member->min.ipv4,
1775                                                           member->max.ipv4);
1776                                    ccs_io_printf(head, " %s", buffer);
1777                          }                          }
1778                            ccs_set_lf(head);
1779                  }                  }
1780                  offset = 0;                  head->r.acl = NULL;
                 if (done)  
                         break;  
1781          }          }
1782            head->r.group = NULL;
1783          return true;          return true;
  out:  
         return false;  
1784  }  }
1785    
1786  /**  /**
1787   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_policy - Read "struct ccs_..._entry" list.
1788   *   *
1789   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1790     * @idx:  Index number.
1791     *
1792     * Returns true on success, false otherwise.
1793   *   *
1794   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1795   */   */
1796  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)  
1797  {  {
1798          struct ccs_condition *cond;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1799          char *buf;                  struct ccs_acl_head *acl =
1800          int len = 256;                          container_of(head->r.acl, typeof(*acl), list);
1801          char *realpath = NULL;                  if (acl->is_deleted)
1802          char *argv0 = NULL;                          continue;
1803          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);                  if (!ccs_flush(head))
1804          if (profile->learning->learning_exec_realpath) {                          return false;
1805                  struct file *file = ee->bprm->file;                  switch (idx) {
1806  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)                  case CCS_ID_TRANSITION_CONTROL:
1807                  struct path path = { file->f_vfsmnt, file->f_dentry };                          {
1808                  realpath = ccs_realpath_from_path(&path);                                  struct ccs_transition_control *ptr =
1809  #else                                          container_of(acl, typeof(*ptr), head);
1810                  realpath = ccs_realpath_from_path(&file->f_path);                                  ccs_set_string(head,
1811  #endif                                                 ccs_transition_type[ptr->type]);
1812                  if (realpath)                                  ccs_set_string(head, ptr->program ?
1813                          len += strlen(realpath) + 17;                                                 ptr->program->name : "any");
1814          }                                  ccs_set_string(head, " from ");
1815          if (profile->learning->learning_exec_argv0) {                                  ccs_set_string(head, ptr->domainname ?
1816                  if (ccs_get_argv0(ee)) {                                                 ptr->domainname->name : "any");
1817                          argv0 = ee->tmp;                          }
1818                          len += strlen(argv0) + 16;                          break;
1819                    case CCS_ID_AGGREGATOR:
1820                            {
1821                                    struct ccs_aggregator *ptr =
1822                                            container_of(acl, typeof(*ptr), head);
1823                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1824                                    ccs_set_string(head, ptr->original_name->name);
1825                                    ccs_set_space(head);
1826                                    ccs_set_string(head,
1827                                                   ptr->aggregated_name->name);
1828                            }
1829                            break;
1830                    case CCS_ID_PATTERN:
1831                            {
1832                                    struct ccs_pattern *ptr =
1833                                            container_of(acl, typeof(*ptr), head);
1834                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1835                                    ccs_set_string(head, ptr->pattern->name);
1836                            }
1837                            break;
1838                    case CCS_ID_RESERVEDPORT:
1839                            {
1840                                    struct ccs_reserved *ptr =
1841                                            container_of(acl, typeof(*ptr), head);
1842                                    const u16 min_port = ptr->min_port;
1843                                    const u16 max_port = ptr->max_port;
1844                                    ccs_set_string(head,
1845                                                   CCS_KEYWORD_DENY_AUTOBIND);
1846                                    ccs_io_printf(head, "%u", min_port);
1847                                    if (min_port != max_port)
1848                                            ccs_io_printf(head, "-%u", max_port);
1849                            }
1850                            break;
1851                    default:
1852                            continue;
1853                  }                  }
1854                    ccs_set_lf(head);
1855          }          }
1856          buf = kmalloc(len, GFP_KERNEL);          head->r.acl = NULL;
1857          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;  
1858  }  }
1859    
1860  /**  /**
1861   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1862   *   *
1863   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1864   *   *
1865   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1866   */   */
1867  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1868  {  {
1869          struct ccs_condition *cond;          if (head->r.eof)
1870          char *buf;                  return;
1871          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1872          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
1873          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
1874          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
1875                  symlink = r->obj->symlink_target->name;                  return;
1876                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1877                   ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1878                    head->r.step++;
1879            if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1880                    return;
1881            while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
1882                   + CCS_MAX_ACL_GROUPS) {
1883                    head->r.group_index = head->r.step - CCS_MAX_POLICY
1884                            - CCS_MAX_GROUP;
1885                    if (!ccs_read_domain2(head,
1886                                          &ccs_acl_group[head->r.group_index]))
1887                            return;
1888                    head->r.step++;
1889          }          }
1890          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;  
1891  }  }
1892    
1893  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2111  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1897  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1897  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1898    
1899  /* Structure for query. */  /* Structure for query. */
1900  struct ccs_query_entry {  struct ccs_query {
1901          struct list_head list;          struct list_head list;
1902          char *query;          char *query;
1903          int query_len;          int query_len;
# Line 2120  struct ccs_query_entry { Line 1906  struct ccs_query_entry {
1906          int answer;          int answer;
1907  };  };
1908    
1909  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1910  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1911    
1912  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1913  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1914    
1915    static void ccs_truncate(char *str)
1916    {
1917            while (* (unsigned char *) str > (unsigned char) ' ')
1918                    str++;
1919            *str = '\0';
1920    }
1921    
1922  /**  /**
1923   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1924   *   *
1925   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1926   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1927   *   *
1928   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1929   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1930   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1931   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1932   */   */
1933  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1934  {  {
# Line 2144  int ccs_supervisor(struct ccs_request_in Line 1937  int ccs_supervisor(struct ccs_request_in
1937          int pos;          int pos;
1938          int len;          int len;
1939          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1940          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1941          bool quota_exceeded = false;          bool quota_exceeded = false;
1942          char *header;          char *header;
1943          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1944                  r->domain = ccs_current_domain();          va_start(args, fmt);
1945          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1946            va_end(args);
1947            if (r->mode == CCS_CONFIG_LEARNING) {
1948                  char *buffer;                  char *buffer;
1949                  struct ccs_condition *cond;                  char *realpath = NULL;
1950          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1951                    char *symlink = NULL;
1952                    char *handler = NULL;
1953                    const struct ccs_preference *pref;
1954                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1955                          return 0;                          return 0;
1956                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1957                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, GFP_KERNEL);  
                 if (!buffer)  
1958                          return 0;                          return 0;
1959                  va_start(args, fmt);                  pref = ccs_profile(r->profile)->learning;
1960                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1961                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1962                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1963                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1964                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1965                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1966                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1967                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1968                          char str[] = "if task.type=execute_handler";                                  }
1969                          cond = ccs_get_condition(str);                          }
1970                  } else                          if (pref->learning_exec_realpath) {
1971                          cond = NULL;                                  realpath = strstr(header,
1972                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1973                  ccs_put_condition(cond);                                  if (realpath) {
1974                  kfree(buffer);                                          realpath += 8;
1975                  /* fall through */                                          ccs_truncate(realpath);
1976          case CCS_CONFIG_PERMISSIVE:                                  }
1977                            }
1978                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1979                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1980                               pref->learning_symlink_target) {
1981                            symlink = strstr(header, " symlink.target=\"");
1982                            if (symlink)
1983                                    ccs_truncate(symlink + 1);
1984                    }
1985                    handler = strstr(header, "type=execute_handler");
1986                    if (handler)
1987                            ccs_truncate(handler);
1988                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1989                    if (buffer) {
1990                            va_start(args, fmt);
1991                            vsnprintf(buffer, len - 1, fmt, args);
1992                            va_end(args);
1993                            if (handler || realpath || argv0 || symlink) {
1994                                    ccs_addprintf(buffer, len, " if");
1995                                    if (handler)
1996                                            ccs_addprintf(buffer, len, " task.%s",
1997                                                          handler);
1998                                    if (realpath)
1999                                            ccs_addprintf(buffer, len, " exec.%s",
2000                                                          realpath);
2001                                    if (argv0)
2002                                            ccs_addprintf(buffer, len,
2003                                                          " exec.argv[0]=%s",
2004                                                          argv0);
2005                                    if (symlink)
2006                                            ccs_addprintf(buffer, len, "%s",
2007                                                          symlink);
2008                            }
2009                            ccs_normalize_line(buffer);
2010                            ccs_write_domain2(buffer, domain, false);
2011                            kfree(buffer);
2012                    }
2013                    kfree(header);
2014                  return 0;                  return 0;
2015          }          }
2016            if (r->mode != CCS_CONFIG_ENFORCING)
2017                    return 0;
2018          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2019                  int i;                  int i;
2020                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2021                          return -EPERM;                          return -EPERM;
2022                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2023                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2024                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2025                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2026                  }                  }
2027                  return -EPERM;                  return -EPERM;
2028          }          }
2029          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);  
2030          if (!header)          if (!header)
2031                  goto out;                  goto out;
2032          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2033          if (!ccs_query_entry)          if (!entry)
2034                  goto out;                  goto out;
2035          len = ccs_round2(len);          len = ccs_round2(len);
2036          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2037          if (!ccs_query_entry->query)          if (!entry->query)
2038                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
2039          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2040          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2041              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2042                  quota_exceeded = true;                  quota_exceeded = true;
2043          } else {          } else {
2044                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2045                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2046          }          }
2047          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2048          if (quota_exceeded)          if (quota_exceeded)
2049                  goto out;                  goto out;
2050          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2051                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2052          kfree(header);          kfree(header);
2053          header = NULL;          header = NULL;
2054          va_start(args, fmt);          va_start(args, fmt);
2055          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2056          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
2057          va_end(args);          va_end(args);
2058          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2059          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2060          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2061          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2062          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2063               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2064               ccs_query_entry->timer++) {               entry->timer++) {
2065                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2066                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2067                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2068                  if (ccs_query_entry->answer)                  if (entry->answer)
2069                          break;                          break;
2070          }          }
2071          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2072          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2073          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2074          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2075          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2076          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2077                  error = 1;                  error = CCS_RETRY_REQUEST;
2078                  r->retry++;                  r->retry++;
2079                  break;                  break;
2080          case 1:          case 1:
# Line 2259  int ccs_supervisor(struct ccs_request_in Line 2089  int ccs_supervisor(struct ccs_request_in
2089                  break;                  break;
2090          }          }
2091   out:   out:
2092          if (ccs_query_entry)          if (entry)
2093                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2094          kfree(ccs_query_entry);          kfree(entry);
2095          kfree(header);          kfree(header);
2096          return error;          return error;
2097  }  }
# Line 2284  static int ccs_poll_query(struct file *f Line 2114  static int ccs_poll_query(struct file *f
2114          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2115                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2116                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2117                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2118                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2119                          if (ptr->answer)                          if (ptr->answer)
2120                                  continue;                                  continue;
2121                          found = true;                          found = true;
# Line 2312  static void ccs_read_query(struct ccs_io Line 2142  static void ccs_read_query(struct ccs_io
2142          int pos = 0;          int pos = 0;
2143          int len = 0;          int len = 0;
2144          char *buf;          char *buf;
2145          if (head->read_avail)          if (head->r.w_pos)
2146                  return;                  return;
2147          if (head->read_buf) {          if (head->read_buf) {
2148                  kfree(head->read_buf);                  kfree(head->read_buf);
2149                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2150          }          }
2151          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2152          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2153                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2154                  if (ptr->answer)                  if (ptr->answer)
2155                          continue;                          continue;
2156                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2157                          continue;                          continue;
2158                  len = ptr->query_len;                  len = ptr->query_len;
2159                  break;                  break;
2160          }          }
2161          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2162          if (!len) {          if (!len) {
2163                  head->read_step = 0;                  head->r.query_index = 0;
2164                  return;                  return;
2165          }          }
2166          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2167          if (!buf)          if (!buf)
2168                  return;                  return;
2169          pos = 0;          pos = 0;
2170          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2171          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2172                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2173                  if (ptr->answer)                  if (ptr->answer)
2174                          continue;                          continue;
2175                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2176                          continue;                          continue;
2177                  /*                  /*
2178                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2357  static void ccs_read_query(struct ccs_io Line 2184  static void ccs_read_query(struct ccs_io
2184          }          }
2185          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2186          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2187                  head->read_buf = buf;                  head->read_buf = buf;
2188                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2189                    head->r.query_index++;
2190          } else {          } else {
2191                  kfree(buf);                  kfree(buf);
2192          }          }
# Line 2381  static int ccs_write_answer(struct ccs_i Line 2207  static int ccs_write_answer(struct ccs_i
2207          unsigned int answer;          unsigned int answer;
2208          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2209          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2210                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2211                  ptr->timer = 0;                  ptr->timer = 0;
2212          }          }
2213          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2390  static int ccs_write_answer(struct ccs_i Line 2215  static int ccs_write_answer(struct ccs_i
2215                  return -EINVAL;                  return -EINVAL;
2216          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2217          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2218                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2219                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2220                          continue;                          continue;
2221                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2409  static int ccs_write_answer(struct ccs_i Line 2233  static int ccs_write_answer(struct ccs_i
2233   */   */
2234  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2235  {  {
2236          if (head->read_eof)          if (head->r.eof)
2237                  return;                  return;
2238          ccs_io_printf(head, "1.7.1-pre");          ccs_set_string(head, "1.7.2");
2239          head->read_eof = true;          head->r.eof = true;
2240  }  }
2241    
2242  /**  /**
# Line 2422  static void ccs_read_version(struct ccs_ Line 2246  static void ccs_read_version(struct ccs_
2246   */   */
2247  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2248  {  {
2249          if (head->read_eof)          if (head->r.eof)
2250                  return;                  return;
2251          /*          /*
2252           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2253           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2254           */           */
2255          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2256          head->read_eof = true;          head->r.eof = true;
2257  }  }
2258    
2259  /**  /**
# Line 2442  static void ccs_read_self_domain(struct Line 2266  static void ccs_read_self_domain(struct
2266   */   */
2267  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2268  {  {
2269          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2270          if (!head)          if (!head)
2271                  return -ENOMEM;                  return -ENOMEM;
2272          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2273          head->type = type;          head->type = type;
2274          switch (type) {          switch (type) {
2275          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2276                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2277                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2278                  break;                  break;
2279          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2280                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2281                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2282                  break;                  break;
2283  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2284          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;  
2285          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2286                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2287                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2288                  break;                  break;
2289  #endif  #endif
2290          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2503  int ccs_open_control(const u8 type, stru Line 2324  int ccs_open_control(const u8 type, stru
2324                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2325                  break;                  break;
2326          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2327                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2328                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2329                  break;                  break;
2330          }          }
2331          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2518  int ccs_open_control(const u8 type, stru Line 2339  int ccs_open_control(const u8 type, stru
2339                  /* Don't allocate read_buf for poll() access. */                  /* Don't allocate read_buf for poll() access. */
2340                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2341                          head->readbuf_size = 4096;                          head->readbuf_size = 4096;
2342                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2343                  if (!head->read_buf) {                  if (!head->read_buf) {
2344                          kfree(head);                          kfree(head);
2345                          return -ENOMEM;                          return -ENOMEM;
# Line 2532  int ccs_open_control(const u8 type, stru Line 2353  int ccs_open_control(const u8 type, stru
2353                  head->write = NULL;                  head->write = NULL;
2354          } else if (head->write) {          } else if (head->write) {
2355                  head->writebuf_size = 4096;                  head->writebuf_size = 4096;
2356                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2357                  if (!head->write_buf) {                  if (!head->write_buf) {
2358                          kfree(head->read_buf);                          kfree(head->read_buf);
2359                          kfree(head);                          kfree(head);
# Line 2541  int ccs_open_control(const u8 type, stru Line 2362  int ccs_open_control(const u8 type, stru
2362          }          }
2363          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2364              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2365                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2366          file->private_data = head;          file->private_data = head;
2367          /*          /*
2368           * 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 2591  int ccs_poll_control(struct file *file, Line 2412  int ccs_poll_control(struct file *file,
2412  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2413                       const int buffer_len)                       const int buffer_len)
2414  {  {
2415          int len = 0;          int len;
2416          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2417          char *cp;          int idx;
2418          if (!head->read)          if (!head->read)
2419                  return -ENOSYS;                  return -ENOSYS;
2420          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2421                  return -EFAULT;                  return -EFAULT;
2422          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2423                  return -EINTR;                  return -EINTR;
2424          while (1) {          head->read_user_buf = buffer;
2425            head->read_user_buf_avail = buffer_len;
2426            idx = ccs_read_lock();
2427            if (ccs_flush(head))
2428                  /* Call the policy handler. */                  /* Call the policy handler. */
2429                  head->read(head);                  head->read(head);
2430                  /* Write to buffer. */          ccs_flush(head);
2431                  len = head->read_avail;          ccs_read_unlock(idx);
2432                  if (len || head->poll || head->read_eof)          len = head->read_user_buf - buffer;
                         break;  
                 len = head->readbuf_size * 2;  
                 cp = kzalloc(len, GFP_KERNEL);  
                 if (!cp) {  
                         len = -ENOMEM;  
                         goto out;  
                 }  
                 kfree(head->read_buf);  
                 head->read_buf = cp;  
                 head->readbuf_size = len;  
         }  
         if (len > buffer_len)  
                 len = buffer_len;  
         if (!len)  
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2433          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2434          return len;          return len;
2435  }  }
# Line 2650  int ccs_write_control(struct file *file, Line 2450  int ccs_write_control(struct file *file,
2450          int error = buffer_len;          int error = buffer_len;
2451          int avail_len = buffer_len;          int avail_len = buffer_len;
2452          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2453            int idx;
2454          if (!head->write)          if (!head->write)
2455                  return -ENOSYS;                  return -ENOSYS;
2456          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2457                  return -EFAULT;                  return -EFAULT;
         /* Don't allow updating policies by non manager programs. */  
         if (head->write != ccs_write_pid &&  
             head->write != ccs_write_domain_policy &&  
             !ccs_is_policy_manager())  
                 return -EPERM;  
2458          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2459                  return -EINTR;                  return -EINTR;
2460            idx = ccs_read_lock();
2461            /* Don't allow updating policies by non manager programs. */
2462            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2463                !ccs_manager()) {
2464                    ccs_read_unlock(idx);
2465                    mutex_unlock(&head->io_sem);
2466                    return -EPERM;
2467            }
2468          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2469          while (avail_len > 0) {          while (avail_len > 0) {
2470                  char c;                  char c;
2471                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2472                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2473                          char *cp = kzalloc(len, GFP_KERNEL);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2474                          if (!cp) {                          if (!cp) {
2475                                  error = -ENOMEM;                                  error = -ENOMEM;
2476                                  break;                                  break;
2477                          }                          }
2478                          memmove(cp, cp0, head->write_avail);                          memmove(cp, cp0, head->w.avail);
2479                          kfree(cp0);                          kfree(cp0);
2480                          head->write_buf = cp;                          head->write_buf = cp;
2481                          cp0 = cp;                          cp0 = cp;
# Line 2683  int ccs_write_control(struct file *file, Line 2487  int ccs_write_control(struct file *file,
2487                  }                  }
2488                  buffer++;                  buffer++;
2489                  avail_len--;                  avail_len--;
2490                  cp0[head->write_avail++] = c;                  cp0[head->w.avail++] = c;
2491                  if (c != '\n')                  if (c != '\n')
2492                          continue;                          continue;
2493                  cp0[head->write_avail - 1] = '\0';                  cp0[head->w.avail - 1] = '\0';
2494                  head->write_avail = 0;                  head->w.avail = 0;
2495                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2496                  head->write(head);                  head->write(head);
2497          }          }
2498            ccs_read_unlock(idx);
2499          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2500          return error;          return error;
2501  }  }
# Line 2714  int ccs_close_control(struct file *file) Line 2519  int ccs_close_control(struct file *file)
2519                  atomic_dec(&ccs_query_observers);                  atomic_dec(&ccs_query_observers);
2520          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2521              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2522                  ccs_read_unlock(head->reader_idx);                  ccs_unlock(head->reader_idx);
2523          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2524          kfree(head->read_buf);          kfree(head->read_buf);