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

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

Legend:
Removed from v.3069  
changed lines
  Added in v.3781

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26