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

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 3079 by kumaneko, Fri Oct 2 06:23:24 2009 UTC branches/ccs-patch/security/ccsecurity/policy_io.c revision 3697 by kumaneko, Tue May 25 08:24:59 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/10/01   * Version: 1.7.2   2010/04/01
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 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] = {  static const char *ccs_mode_4[4] = {
44          "disabled", "learning", "permissive", "enforcing"          "disabled", "learning", "permissive", "enforcing"
# Line 94  static const char *ccs_mac_keywords[CCS_ Line 94  static const char *ccs_mac_keywords[CCS_
94          = "file::umount",          = "file::umount",
95          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
96          = "file::pivot_root",          = "file::pivot_root",
97            [CCS_MAC_FILE_TRANSIT]
98            = "file::transit",
99          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
100          = "misc::env",          = "misc::env",
101          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MAC_NETWORK_UDP_BIND]
# Line 244  bool ccs_io_printf(struct ccs_io_buffer Line 246  bool ccs_io_printf(struct ccs_io_buffer
246  }  }
247    
248  /**  /**
249   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
250   *   *
251   * @profile: Profile number to create.   * @profile: Profile number to create.
252   *   *
253   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
254   */   */
255  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)  
256  {  {
257          struct ccs_profile *ptr;          struct ccs_profile *ptr;
258          struct ccs_profile *entry;          struct ccs_profile *entry;
# Line 260  static struct ccs_profile *ccs_find_or_a Line 261  static struct ccs_profile *ccs_find_or_a
261          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
262          if (ptr)          if (ptr)
263                  return ptr;                  return ptr;
264          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
265          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
266                    goto out;
267          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
268          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
269                  ptr = entry;                  ptr = entry;
# Line 278  static struct ccs_profile *ccs_find_or_a Line 280  static struct ccs_profile *ccs_find_or_a
280                  entry = NULL;                  entry = NULL;
281          }          }
282          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
283     out:
284          kfree(entry);          kfree(entry);
285          return ptr;          return ptr;
286  }  }
# Line 285  static struct ccs_profile *ccs_find_or_a Line 288  static struct ccs_profile *ccs_find_or_a
288  /**  /**
289   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
290   */   */
291  void ccs_check_profile(void)  static void ccs_check_profile(void)
292  {  {
293          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
294            const int idx = ccs_read_lock();
295          ccs_policy_loaded = true;          ccs_policy_loaded = true;
296          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
297                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
# Line 296  void ccs_check_profile(void) Line 300  void ccs_check_profile(void)
300                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
301                        profile, domain->domainname->name);                        profile, domain->domainname->name);
302          }          }
303            ccs_read_unlock(idx);
304            if (ccs_profile_version != 20090903)
305                    panic("Profile version %u is not supported.\n",
306                          ccs_profile_version);
307            printk(KERN_INFO "CCSecurity: 1.7.2   2010/04/01\n");
308            printk(KERN_INFO "Mandatory Access Control activated.\n");
309  }  }
310    
311  /**  /**
# Line 331  static int ccs_write_profile(struct ccs_ Line 341  static int ccs_write_profile(struct ccs_
341          bool use_default = false;          bool use_default = false;
342          char *cp;          char *cp;
343          struct ccs_profile *profile;          struct ccs_profile *profile;
344            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
345                    return 0;
346          i = simple_strtoul(data, &cp, 10);          i = simple_strtoul(data, &cp, 10);
347          if (data == cp) {          if (data == cp) {
348                  profile = &ccs_default_profile;                  profile = &ccs_default_profile;
# Line 338  static int ccs_write_profile(struct ccs_ Line 350  static int ccs_write_profile(struct ccs_
350                  if (*cp != '-')                  if (*cp != '-')
351                          return -EINVAL;                          return -EINVAL;
352                  data = cp + 1;                  data = cp + 1;
353                  profile = ccs_find_or_assign_new_profile(i);                  profile = ccs_assign_profile(i);
354                  if (!profile)                  if (!profile)
355                          return -EINVAL;                          return -EINVAL;
356          }          }
# Line 354  static int ccs_write_profile(struct ccs_ Line 366  static int ccs_write_profile(struct ccs_
366                  value = 0;                  value = 0;
367          else          else
368                  value = -1;                  value = -1;
369          if (!strcmp(data, "PREFERENCE::audit")) {          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_AUDIT)) {
370  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
371                  char *cp2;                  char *cp2;
372  #endif  #endif
# Line 383  static int ccs_write_profile(struct ccs_ Line 395  static int ccs_write_profile(struct ccs_
395                          profile->preference.audit_path_info = false;                          profile->preference.audit_path_info = false;
396                  return 0;                  return 0;
397          }          }
398          if (!strcmp(data, "PREFERENCE::enforcing")) {          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_ENFORCING)) {
399                  char *cp2;                  char *cp2;
400                  if (use_default) {                  if (use_default) {
401                          profile->enforcing = &ccs_default_profile.preference;                          profile->enforcing = &ccs_default_profile.preference;
# Line 398  static int ccs_write_profile(struct ccs_ Line 410  static int ccs_write_profile(struct ccs_
410                                 &profile->preference.enforcing_penalty);                                 &profile->preference.enforcing_penalty);
411                  return 0;                  return 0;
412          }          }
413          if (!strcmp(data, "PREFERENCE::permissive")) {          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_PERMISSIVE)) {
414                  if (use_default) {                  if (use_default) {
415                          profile->permissive = &ccs_default_profile.preference;                          profile->permissive = &ccs_default_profile.preference;
416                          return 0;                          return 0;
# Line 408  static int ccs_write_profile(struct ccs_ Line 420  static int ccs_write_profile(struct ccs_
420                          profile->preference.permissive_verbose = value;                          profile->preference.permissive_verbose = value;
421                  return 0;                  return 0;
422          }          }
423          if (!strcmp(data, "PREFERENCE::learning")) {          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_LEARNING)) {
424                  char *cp2;                  char *cp2;
425                  if (use_default) {                  if (use_default) {
426                          profile->learning = &ccs_default_profile.preference;                          profile->learning = &ccs_default_profile.preference;
# Line 438  static int ccs_write_profile(struct ccs_ Line 450  static int ccs_write_profile(struct ccs_
450          if (profile == &ccs_default_profile)          if (profile == &ccs_default_profile)
451                  return -EINVAL;                  return -EINVAL;
452          if (!strcmp(data, "COMMENT")) {          if (!strcmp(data, "COMMENT")) {
453                  const struct ccs_path_info *new_comment = ccs_get_name(cp);                  const struct ccs_path_info *old_comment = profile->comment;
454                  const struct ccs_path_info *old_comment;                  profile->comment = ccs_get_name(cp);
                 /* 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);  
455                  ccs_put_name(old_comment);                  ccs_put_name(old_comment);
456                  return 0;                  return 0;
457          }          }
# Line 498  static int ccs_write_profile(struct ccs_ Line 505  static int ccs_write_profile(struct ccs_
505          return 0;          return 0;
506  }  }
507    
508    static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)
509    {
510            struct ccs_preference *pref = &ccs_default_profile.preference;
511            const struct ccs_profile *profile = idx >= 0 ?
512                    ccs_profile_ptr[idx] : NULL;
513            char buffer[16] = "";
514            if (profile) {
515                    buffer[sizeof(buffer) - 1] = '\0';
516                    snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
517            }
518            if (profile) {
519                    pref = profile->audit;
520                    if (pref == &ccs_default_profile.preference)
521                            pref = NULL;
522            }
523            if (pref && !ccs_io_printf(head, "%s%s={ "
524    #ifdef CONFIG_CCSECURITY_AUDIT
525                                       "max_grant_log=%u max_reject_log=%u "
526    #endif
527                                       "task_info=%s path_info=%s }\n", buffer,
528                                       CCS_KEYWORD_PREFERENCE_AUDIT,
529    #ifdef CONFIG_CCSECURITY_AUDIT
530                                       pref->audit_max_grant_log,
531                                       pref->audit_max_reject_log,
532    #endif
533                                       ccs_yesno(pref->audit_task_info),
534                                       ccs_yesno(pref->audit_path_info)))
535                    return false;
536            if (profile) {
537                    pref = profile->learning;
538                    if (pref == &ccs_default_profile.preference)
539                            pref = NULL;
540            }
541            if (pref && !ccs_io_printf(head, "%s%s={ "
542                                       "verbose=%s max_entry=%u exec.realpath=%s "
543                                       "exec.argv0=%s symlink.target=%s }\n",
544                                       buffer, CCS_KEYWORD_PREFERENCE_LEARNING,
545                                       ccs_yesno(pref->learning_verbose),
546                                       pref->learning_max_entry,
547                                       ccs_yesno(pref->learning_exec_realpath),
548                                       ccs_yesno(pref->learning_exec_argv0),
549                                       ccs_yesno(pref->learning_symlink_target)))
550                    return false;
551            if (profile) {
552                    pref = profile->permissive;
553                    if (pref == &ccs_default_profile.preference)
554                            pref = NULL;
555            }
556            if (pref && !ccs_io_printf(head, "%s%s={ verbose=%s }\n", buffer,
557                                       CCS_KEYWORD_PREFERENCE_PERMISSIVE,
558                                       ccs_yesno(pref->permissive_verbose)))
559                    return false;
560            if (profile) {
561                    pref = profile->enforcing;
562                    if (pref == &ccs_default_profile.preference)
563                            pref = NULL;
564            }
565            return !pref || ccs_io_printf(head, "%s%s={ verbose=%s penalty=%u }\n",
566                                          buffer, CCS_KEYWORD_PREFERENCE_ENFORCING,
567                                          ccs_yesno(pref->enforcing_verbose),
568                                          pref->enforcing_penalty);
569    }
570    
571  /**  /**
572   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
573   *   *
# Line 511  static void ccs_read_profile(struct ccs_ Line 581  static void ccs_read_profile(struct ccs_
581          if (head->read_bit)          if (head->read_bit)
582                  goto body;                  goto body;
583          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
584          ccs_io_printf(head, "PREFERENCE::audit={ "          ccs_print_preference(head, -1);
 #ifdef CONFIG_CCSECURITY_AUDIT  
                       "max_grant_log=%u max_reject_log=%u "  
 #endif  
                       "task_info=%s path_info=%s }\n",  
 #ifdef CONFIG_CCSECURITY_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);  
585          head->read_bit = 1;          head->read_bit = 1;
586   body:   body:
587          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {
# Line 551  static void ccs_read_profile(struct ccs_ Line 590  static void ccs_read_profile(struct ccs_
590                  int i;                  int i;
591                  int pos;                  int pos;
592                  const struct ccs_profile *profile = ccs_profile_ptr[index];                  const struct ccs_profile *profile = ccs_profile_ptr[index];
593                    const struct ccs_path_info *comment;
594                  head->read_step = index;                  head->read_step = index;
595                  if (!profile)                  if (!profile)
596                          continue;                          continue;
597                  pos = head->read_avail;                  pos = head->read_avail;
598                  spin_lock(&ccs_profile_comment_lock);                  comment = profile->comment;
599                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
600                                       profile->comment ? profile->comment->name                                       comment ? comment->name : "");
                                      : "");  
                 spin_unlock(&ccs_profile_comment_lock);  
601                  if (!done)                  if (!done)
602                          goto out;                          goto out;
603                  config = profile->default_config;                  config = profile->default_config;
604  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
605                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
606                                     "reject_log=%s }\n", index,                                     "grant_log=%s reject_log=%s }\n", index,
607                                     ccs_mode_4[config & 3],                                     "CONFIG", "", ccs_mode_4[config & 3],
608                                     ccs_yesno(config &                                     ccs_yesno(config &
609                                               CCS_CONFIG_WANT_GRANT_LOG),                                               CCS_CONFIG_WANT_GRANT_LOG),
610                                     ccs_yesno(config &                                     ccs_yesno(config &
611                                               CCS_CONFIG_WANT_REJECT_LOG)))                                               CCS_CONFIG_WANT_REJECT_LOG)))
612                          goto out;                          goto out;
613  #else  #else
614                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,
615                                     ccs_mode_4[config & 3]))                                     "CONFIG", "", ccs_mode_4[config & 3]))
616                          goto out;                          goto out;
617  #endif  #endif
618                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
# Line 589  static void ccs_read_profile(struct ccs_ Line 627  static void ccs_read_profile(struct ccs_
627  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
628                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
629                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
630                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
631                                             "grant_log=%s reject_log=%s }\n",                                             "grant_log=%s reject_log=%s }\n",
632                                             index, ccs_mac_keywords[i],                                             index, "CONFIG::",
633                                               ccs_mac_keywords[i],
634                                             ccs_mode_4[config & 3], g, r))                                             ccs_mode_4[config & 3], g, r))
635                                  goto out;                                  goto out;
636  #else  #else
637                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",
638                                             index, ccs_mac_keywords[i],                                             index, "CONFIG::",
639                                               ccs_mac_keywords[i],
640                                             ccs_mode_4[config & 3]))                                             ccs_mode_4[config & 3]))
641                                  goto out;                                  goto out;
642  #endif  #endif
643                  }                  }
644                  if (profile->audit != &ccs_default_profile.preference &&                  if (!ccs_print_preference(head, index))
                     !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))  
645                          goto out;                          goto out;
646                  continue;                  continue;
647   out:   out:
# Line 653  static void ccs_read_profile(struct ccs_ Line 652  static void ccs_read_profile(struct ccs_
652                  head->read_eof = true;                  head->read_eof = true;
653  }  }
654    
655  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
656  LIST_HEAD(ccs_policy_manager_list);                                     const struct ccs_acl_head *b)
657    {
658            return container_of(a, struct ccs_manager, head)->manager
659                    == container_of(b, struct ccs_manager, head)->manager;
660    }
661    
662  /**  /**
663   * 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 669  LIST_HEAD(ccs_policy_manager_list);
669   */   */
670  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)
671  {  {
672          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
673          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
674          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
675                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
676                          return -EINVAL;                          return -EINVAL;
677                  e.is_domain = true;                  e.is_domain = true;
678          } else {          } else {
679                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager, 1, -1, -1))
680                          return -EINVAL;                          return -EINVAL;
681          }          }
682          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
683          if (!e.manager)          if (!e.manager)
684                  return -ENOMEM;                  return error;
685          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
686                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    CCS_ID_MANAGER, ccs_same_manager_entry);
         mutex_lock(&ccs_policy_lock);  
         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);  
687          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
688          return error;          return error;
689  }  }
690    
691  /**  /**
692   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
693   *   *
694   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
695   *   *
696   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
697   */   */
698  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
699  {  {
700          char *data = head->write_buf;          char *data = head->write_buf;
701          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 707  static int ccs_write_manager_policy(stru
707  }  }
708    
709  /**  /**
710   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
711   *   *
712   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
713   *   *
714   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
715   */   */
716  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
717  {  {
718          struct list_head *pos;          struct list_head *pos;
719          if (head->read_eof)          if (head->read_eof)
720                  return;                  return;
721          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(pos, head->read_var2,
722                  struct ccs_policy_manager_entry *ptr;                               &ccs_policy_list[CCS_ID_MANAGER]) {
723                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  struct ccs_manager *ptr
724                  if (ptr->is_deleted)                          = list_entry(pos, typeof(*ptr), head.list);
725                    if (ptr->head.is_deleted)
726                          continue;                          continue;
727                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
728                          return;                          return;
# Line 744  static void ccs_read_manager_policy(stru Line 731  static void ccs_read_manager_policy(stru
731  }  }
732    
733  /**  /**
734   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
735   *   *
736   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
737   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
738   *   *
739   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
740   */   */
741  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
742  {  {
743          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
744          const char *exe;          const char *exe;
745          struct task_struct *task = current;          struct task_struct *task = current;
746          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
# Line 761  static bool ccs_is_policy_manager(void) Line 748  static bool ccs_is_policy_manager(void)
748          bool found = false;          bool found = false;
749          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
750                  return true;                  return true;
751          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
752                  return true;                  return true;
753          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
754                  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;  
                 }  
         }  
755          exe = ccs_get_exe();          exe = ccs_get_exe();
756          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
757                  return false;                                  head.list) {
758          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
759                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
760                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
761                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
762                          /* Set manager flag. */                                  continue;
763                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
764                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
765                                    continue;
766                  }                  }
767                    /* Set manager flag. */
768                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
769                    found = true;
770                    break;
771          }          }
772          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
773                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 809  static bool ccs_is_policy_manager(void) Line 793  static bool ccs_is_policy_manager(void)
793  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
794  {  {
795          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
796          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
797                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
798                  if (cp)          if (cp)
799                          *cp++ = '\0';                  *cp++ = '\0';
         }  
800          return cp;          return cp;
801  }  }
802    
803  /**  /**
804   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
805   *   *
806   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
807   * @data: String to parse.   * @data: String to parse.
# Line 835  static char *ccs_find_condition_part(cha Line 810  static char *ccs_find_condition_part(cha
810   *   *
811   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
812   */   */
813  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)
814  {  {
815          unsigned int pid;          unsigned int pid;
816          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
# Line 847  static bool ccs_is_select_one(struct ccs Line 822  static bool ccs_is_select_one(struct ccs
822          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
823              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
824                  struct task_struct *p;                  struct task_struct *p;
825                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
826  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
827                  if (global_pid)                  if (global_pid)
828                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
829                                                                   &init_pid_ns);
830                  else                  else
831                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
832  #else  #else
833                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
834  #endif  #endif
835                  if (p)                  if (p)
836                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
837                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
838          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
839                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
840                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
841          } else          } else
842                  return false;                  return false;
# Line 889  static bool ccs_is_select_one(struct ccs Line 865  static bool ccs_is_select_one(struct ccs
865          return true;          return true;
866  }  }
867    
868  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,
869                                      struct ccs_condition *cond,                               struct ccs_condition *cond, const bool is_delete)
870                                      const bool is_delete)  {
871  {          u8 i;
872          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))          static const struct {
873                  return ccs_write_capability_policy(data, domain, cond,                  const char *keyword;
874                                                     is_delete);                  int (*write) (char *, struct ccs_domain_info *,
875          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))                                struct ccs_condition *, const bool);
876                  return ccs_write_network_policy(data, domain, cond, is_delete);          } ccs_callback[5] = {
877          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                  { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
878                  return ccs_write_signal_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
879          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
880                  return ccs_write_env_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
881          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))                  { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
882                  return ccs_write_mount_policy(data, domain, cond, is_delete);          };
883          return ccs_write_file_policy(data, domain, cond, is_delete);          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
884                          const bool) = ccs_write_file;
885            for (i = 0; i < 5; i++) {
886                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
887                            continue;
888                    write = ccs_callback[i].write;
889                    break;
890            }
891            return write(data, domain, cond, is_delete);
892  }  }
893    
894  /**  /**
895   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
896   *   *
897   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
898   *   *
899   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
900   */   */
901  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
902  {  {
903          char *data = head->write_buf;          char *data = head->write_buf;
904          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->write_var1;
# Line 928  static int ccs_write_domain_policy(struc Line 912  static int ccs_write_domain_policy(struc
912                  is_delete = true;                  is_delete = true;
913          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
914                  is_select = true;                  is_select = true;
915          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
916                  return 0;                  return 0;
917          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
918          if (!ccs_is_policy_manager())          if (!ccs_manager())
919                  return -EPERM;                  return -EPERM;
920          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
921                  domain = NULL;                  domain = NULL;
922                  if (is_delete)                  if (is_delete)
923                          ccs_delete_domain(data);                          ccs_delete_domain(data);
924                  else if (is_select)                  else if (is_select)
925                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
926                  else                  else
927                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
928                  head->write_var1 = domain;                  head->write_var1 = domain;
929                  return 0;                  return 0;
930          }          }
# Line 961  static int ccs_write_domain_policy(struc Line 945  static int ccs_write_domain_policy(struc
945                  domain->ignore_global_allow_env = !is_delete;                  domain->ignore_global_allow_env = !is_delete;
946                  return 0;                  return 0;
947          }          }
948            if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {
949                    domain->quota_warned = !is_delete;
950                    return 0;
951            }
952            if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {
953                    domain->domain_transition_failed = !is_delete;
954                    return 0;
955            }
956          cp = ccs_find_condition_part(data);          cp = ccs_find_condition_part(data);
957          if (cp) {          if (cp) {
958                  cond = ccs_get_condition(cp);                  cond = ccs_get_condition(cp);
959                  if (!cond)                  if (!cond)
960                          return -EINVAL;                          return -EINVAL;
961          }          }
962          error = ccs_write_domain_policy2(data, domain, cond, is_delete);          error = ccs_write_domain2(data, domain, cond, is_delete);
963          if (cond)          if (cond)
964                  ccs_put_condition(cond);                  ccs_put_condition(cond);
965          return error;          return error;
# Line 1010  static bool ccs_print_name_union_quoted( Line 1002  static bool ccs_print_name_union_quoted(
1002          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1003  }  }
1004    
1005    static void ccs_print_number(char *buffer, int buffer_len,
1006                                 const struct ccs_number_union *ptr)
1007    {
1008            int i;
1009            unsigned long min = ptr->values[0];
1010            const unsigned long max = ptr->values[1];
1011            u8 min_type = ptr->value_type[0];
1012            const u8 max_type = ptr->value_type[1];
1013            memset(buffer, 0, buffer_len);
1014            buffer_len -= 2;
1015            for (i = 0; i < 2; i++) {
1016                    int len;
1017                    switch (min_type) {
1018                    case CCS_VALUE_TYPE_HEXADECIMAL:
1019                            snprintf(buffer, buffer_len, "0x%lX", min);
1020                            break;
1021                    case CCS_VALUE_TYPE_OCTAL:
1022                            snprintf(buffer, buffer_len, "0%lo", min);
1023                            break;
1024                    default:
1025                            snprintf(buffer, buffer_len, "%lu", min);
1026                            break;
1027                    }
1028                    if (min == max && min_type == max_type)
1029                            break;
1030                    len = strlen(buffer);
1031                    buffer[len++] = '-';
1032                    buffer += len;
1033                    buffer_len -= len;
1034                    min_type = max_type;
1035                    min = max;
1036            }
1037    }
1038    
1039  /**  /**
1040   * ccs_print_number_union_common - Print a ccs_number_union.   * ccs_print_number_union_common - Print a ccs_number_union.
1041   *   *
# Line 1023  static bool ccs_print_number_union_commo Line 1049  static bool ccs_print_number_union_commo
1049                                            const struct ccs_number_union *ptr,                                            const struct ccs_number_union *ptr,
1050                                            const bool need_space)                                            const bool need_space)
1051  {  {
1052          unsigned long min;          char buffer[128];
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
1053          if (need_space && !ccs_io_printf(head, " "))          if (need_space && !ccs_io_printf(head, " "))
1054                  return false;                  return false;
1055          if (ptr->is_group)          if (ptr->is_group)
1056                  return ccs_io_printf(head, "@%s",                  return ccs_io_printf(head, "@%s",
1057                                       ptr->group->group_name->name);                                       ptr->group->group_name->name);
1058          min_type = ptr->min_type;          ccs_print_number(buffer, sizeof(buffer), ptr);
1059          max_type = ptr->max_type;          return ccs_io_printf(head, "%s", buffer);
         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);  
         }  
1060  }  }
1061    
1062  /**  /**
# Line 1070  static bool ccs_print_number_union_commo Line 1067  static bool ccs_print_number_union_commo
1067   *   *
1068   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1069   */   */
1070  bool ccs_print_number_union(struct ccs_io_buffer *head,  static bool ccs_print_number_union(struct ccs_io_buffer *head,
1071                              const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1072  {  {
1073          return ccs_print_number_union_common(head, ptr, true);          return ccs_print_number_union_common(head, ptr, true);
1074  }  }
# Line 1104  static bool ccs_print_condition(struct c Line 1101  static bool ccs_print_condition(struct c
1101          const struct ccs_condition_element *condp;          const struct ccs_condition_element *condp;
1102          const struct ccs_number_union *numbers_p;          const struct ccs_number_union *numbers_p;
1103          const struct ccs_name_union *names_p;          const struct ccs_name_union *names_p;
1104          const struct ccs_argv_entry *argv;          const struct ccs_argv *argv;
1105          const struct ccs_envp_entry *envp;          const struct ccs_envp *envp;
1106          u16 condc;          u16 condc;
1107          u16 i;          u16 i;
1108          u16 j;          u16 j;
# Line 1117  static bool ccs_print_condition(struct c Line 1114  static bool ccs_print_condition(struct c
1114          numbers_p = (const struct ccs_number_union *) (condp + condc);          numbers_p = (const struct ccs_number_union *) (condp + condc);
1115          names_p = (const struct ccs_name_union *)          names_p = (const struct ccs_name_union *)
1116                  (numbers_p + cond->numbers_count);                  (numbers_p + cond->numbers_count);
1117          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);          argv = (const struct ccs_argv *) (names_p + cond->names_count);
1118          envp = (const struct ccs_envp_entry *) (argv + cond->argc);          envp = (const struct ccs_envp *) (argv + cond->argc);
1119          memset(buffer, 0, sizeof(buffer));          memset(buffer, 0, sizeof(buffer));
1120          if (condc && !ccs_io_printf(head, "%s", " if"))          if (condc && !ccs_io_printf(head, "%s", " if"))
1121                  goto out;                  goto out;
# Line 1194  static bool ccs_print_condition(struct c Line 1191  static bool ccs_print_condition(struct c
1191                                     cond->post_state[j]))                                     cond->post_state[j]))
1192                          goto out;                          goto out;
1193          }          }
1194            if (i & (1 << 4)) {
1195                    if (!ccs_io_printf(head, " audit=%s",
1196                                       ccs_yesno(cond->post_state[4])))
1197                            goto out;
1198            }
1199   no_condition:   no_condition:
1200          if (ccs_io_printf(head, "\n"))          if (ccs_io_printf(head, "\n"))
1201                  return true;                  return true;
# Line 1202  static bool ccs_print_condition(struct c Line 1204  static bool ccs_print_condition(struct c
1204  }  }
1205    
1206  /**  /**
1207   * ccs_print_path_acl - Print a single path ACL entry.   * ccs_print_path_acl - Print a path ACL entry.
1208   *   *
1209   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1210   * @ptr:  Pointer to "struct ccs_path_acl".   * @ptr:  Pointer to "struct ccs_path_acl".
# Line 1220  static bool ccs_print_path_acl(struct cc Line 1222  static bool ccs_print_path_acl(struct cc
1222          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {
1223                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1224                          continue;                          continue;
1225                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1226                        && bit != CCS_TYPE_TRANSIT)
1227                          continue;                          continue;
1228                  /* Print "read/write" instead of "read" and "write". */                  /* Print "read/write" instead of "read" and "write". */
1229                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
# Line 1391  static bool ccs_print_capability_acl(str Line 1394  static bool ccs_print_capability_acl(str
1394  }  }
1395    
1396  /**  /**
  * 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))  
                 return false;  
         if (min_address != max_address) {  
                 ccs_print_ipv6(buf, sizeof(buf), max_address);  
                 if (!ccs_io_printf(head, "-%s", buf))  
                         return false;  
         }  
         return true;  
 }  
   
 /**  
1397   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_network_acl - Print a network ACL entry.
1398   *   *
1399   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 1451  static bool ccs_print_network_acl(struct Line 1408  static bool ccs_print_network_acl(struct
1408  {  {
1409          int pos;          int pos;
1410          u8 bit;          u8 bit;
1411          const u16 perm = ptr->perm;          const u8 perm = ptr->perm;
1412            char buf[128];
1413          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1414                    const char *w[2] = { "", "" };
1415                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1416                          continue;                          continue;
1417                  pos = head->read_avail;                  pos = head->read_avail;
                 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",  
                                    ccs_net2keyword(bit)))  
                         goto out;  
1418                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1419                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1420                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          w[0] = "@";
1421                                             group_name->name))                          w[1] = ptr->address.group->group_name->name;
                                 goto out;  
1422                          break;                          break;
1423                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1424                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1425                                  goto out;                                         ptr->address.ipv4.max);
1426                            w[0] = buf;
1427                          break;                          break;
1428                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1429                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1430                                  goto out;                                         ptr->address.ipv6.max);
1431                            w[0] = buf;
1432                          break;                          break;
1433                  }                  }
1434                  if (!ccs_print_number_union(head, &ptr->port) ||                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1435                                       ccs_net2keyword(bit), w[0], w[1]) ||
1436                        !ccs_print_number_union(head, &ptr->port) ||
1437                      !ccs_print_condition(head, cond))                      !ccs_print_condition(head, cond))
1438                          goto out;                          goto out;
1439          }          }
# Line 1510  static bool ccs_print_signal_acl(struct Line 1469  static bool ccs_print_signal_acl(struct
1469  }  }
1470    
1471  /**  /**
1472   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_print_execute_handler - Print an execute handler ACL entry.
1473   *   *
1474   * @head:    Pointer to "struct ccs_io_buffer".   * @head:    Pointer to "struct ccs_io_buffer".
1475   * @keyword: Name of the keyword.   * @keyword: Name of the keyword.
1476   * @ptr:     Pointer to "struct ccs_execute_handler_record".   * @ptr:     Pointer to "struct ccs_execute_handler".
1477   *   *
1478   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1479   */   */
1480  static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  static bool ccs_print_execute_handler(struct ccs_io_buffer *head,
1481                                               const char *keyword,                                        const char *keyword,
1482                                               struct ccs_execute_handler_record *                                        struct ccs_execute_handler *ptr)
                                              ptr)  
1483  {  {
1484          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);
1485  }  }
# Line 1572  static bool ccs_print_entry(struct ccs_i Line 1530  static bool ccs_print_entry(struct ccs_i
1530                          = container_of(ptr, struct ccs_path_acl, head);                          = container_of(ptr, struct ccs_path_acl, head);
1531                  return ccs_print_path_acl(head, acl, cond);                  return ccs_print_path_acl(head, acl, cond);
1532          }          }
1533          if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {          if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1534                  struct ccs_execute_handler_record *acl              acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1535                          = container_of(ptr, struct ccs_execute_handler_record,                  struct ccs_execute_handler *acl
1536                                         head);                          = container_of(ptr, struct ccs_execute_handler, head);
1537                  const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;                  const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1538                  return ccs_print_execute_handler_record(head, keyword, acl);                          CCS_KEYWORD_EXECUTE_HANDLER :
1539          }                          CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1540          if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {                  return ccs_print_execute_handler(head, keyword, acl);
                 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);  
1541          }          }
1542          if (head->read_execute_only)          if (head->read_execute_only)
1543                  return true;                  return true;
# Line 1595  static bool ccs_print_entry(struct ccs_i Line 1548  static bool ccs_print_entry(struct ccs_i
1548          }          }
1549          if (acl_type == CCS_TYPE_PATH2_ACL) {          if (acl_type == CCS_TYPE_PATH2_ACL) {
1550                  struct ccs_path2_acl *acl                  struct ccs_path2_acl *acl
1551                          = container_of(ptr, struct ccs_path2_acl,                          = container_of(ptr, struct ccs_path2_acl, head);
                                        head);  
1552                  return ccs_print_path2_acl(head, acl, cond);                  return ccs_print_path2_acl(head, acl, cond);
1553          }          }
1554          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1555                  struct ccs_path_number_acl *acl                  struct ccs_path_number_acl *acl
1556                          = container_of(ptr, struct ccs_path_number_acl,                          = container_of(ptr, struct ccs_path_number_acl, head);
                                        head);  
1557                  return ccs_print_path_number_acl(head, acl, cond);                  return ccs_print_path_number_acl(head, acl, cond);
1558          }          }
1559          if (acl_type == CCS_TYPE_ENV_ACL) {          if (acl_type == CCS_TYPE_ENV_ACL) {
# Line 1612  static bool ccs_print_entry(struct ccs_i Line 1563  static bool ccs_print_entry(struct ccs_i
1563          }          }
1564          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1565                  struct ccs_capability_acl *acl                  struct ccs_capability_acl *acl
1566                          = container_of(ptr, struct ccs_capability_acl,                          = container_of(ptr, struct ccs_capability_acl, head);
                                        head);  
1567                  return ccs_print_capability_acl(head, acl, cond);                  return ccs_print_capability_acl(head, acl, cond);
1568          }          }
1569          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1570                  struct ccs_ip_network_acl *acl                  struct ccs_ip_network_acl *acl
1571                          = container_of(ptr, struct ccs_ip_network_acl,                          = container_of(ptr, struct ccs_ip_network_acl, head);
                                        head);  
1572                  return ccs_print_network_acl(head, acl, cond);                  return ccs_print_network_acl(head, acl, cond);
1573          }          }
1574          if (acl_type == CCS_TYPE_SIGNAL_ACL) {          if (acl_type == CCS_TYPE_SIGNAL_ACL) {
# Line 1637  static bool ccs_print_entry(struct ccs_i Line 1586  static bool ccs_print_entry(struct ccs_i
1586  }  }
1587    
1588  /**  /**
1589   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1590     *
1591     * @head:   Pointer to "struct ccs_io_buffer".
1592     * @domain: Pointer to "struct ccs_domain_info".
1593     *
1594     * Caller holds ccs_read_lock().
1595     *
1596     * Returns true on success, false otherwise.
1597     */
1598    static bool ccs_read_domain2(struct ccs_io_buffer *head,
1599                                 struct ccs_domain_info *domain)
1600    {
1601            struct list_head *pos;
1602            /* Print ACL entries in the domain. */
1603            list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {
1604                    struct ccs_acl_info *ptr
1605                            = list_entry(pos, struct ccs_acl_info, list);
1606                    if (!ccs_print_entry(head, ptr))
1607                            return false;
1608            }
1609            return true;
1610    }
1611    
1612    /**
1613     * ccs_read_domain - Read domain policy.
1614   *   *
1615   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1616   *   *
1617   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1618   */   */
1619  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1620  {  {
1621          struct list_head *dpos;          struct list_head *pos;
         struct list_head *apos;  
1622          if (head->read_eof)          if (head->read_eof)
1623                  return;                  return;
1624          if (head->read_step == 0)          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1625                  head->read_step = 1;                  struct ccs_domain_info *domain =
1626          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(pos, struct ccs_domain_info, list);
1627                  struct ccs_domain_info *domain;                  const char *w[4] = { "", "", "", "" };
1628                  const char *quota_exceeded = "";                  switch (head->read_step) {
1629                  const char *transition_failed = "";                  case 0:
1630                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted && !head->read_single_domain)
1631                  const char *ignore_global_allow_env = "";                                  continue;
1632                  domain = list_entry(dpos, struct ccs_domain_info, list);                          /* Print domainname and flags. */
1633                  if (head->read_step != 1)                          if (domain->quota_warned)
1634                          goto acl_loop;                                  w[0] = CCS_KEYWORD_QUOTA_EXCEEDED "\n";
1635                  if (domain->is_deleted && !head->read_single_domain)                          if (domain->domain_transition_failed)
1636                          continue;                                  w[1] = CCS_KEYWORD_TRANSITION_FAILED "\n";
1637                  /* Print domainname and flags. */                          if (domain->ignore_global_allow_read)
1638                  if (domain->quota_warned)                                  w[2] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
1639                          quota_exceeded = "quota_exceeded\n";                                          "\n";
1640                  if (domain->domain_transition_failed)                          if (domain->ignore_global_allow_env)
1641                          transition_failed = "transition_failed\n";                                  w[3] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV
1642                  if (domain->ignore_global_allow_read)                                          "\n";
1643                          ignore_global_allow_read                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE
1644                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                             "%u\n%s%s%s%s\n",
1645                  if (domain->ignore_global_allow_env)                                             domain->domainname->name,
1646                          ignore_global_allow_env                                             domain->profile, w[0], w[1], w[2],
1647                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                                             w[3]))
1648                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                                  return;
1649                                     "%s%s%s%s\n", domain->domainname->name,                          head->read_step++;
1650                                     domain->profile, quota_exceeded,                          /* fall through */
1651                                     transition_failed,                  case 1:
1652                                     ignore_global_allow_read,                          if (!ccs_read_domain2(head, domain))
1653                                     ignore_global_allow_env))                                  return;
1654                          return;                          head->read_step++;
1655                  head->read_step = 2;                          /* fall through */
1656   acl_loop:                  case 2:
1657                  if (head->read_step == 3)                          if (!ccs_io_printf(head, "\n"))
                         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))  
1658                                  return;                                  return;
1659                            head->read_step = 0;
1660                            if (head->read_single_domain)
1661                                    goto out;
1662                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1663          }          }
1664     out:
1665          head->read_eof = true;          head->read_eof = true;
1666  }  }
1667    
# Line 1800  static void ccs_read_pid(struct ccs_io_b Line 1762  static void ccs_read_pid(struct ccs_io_b
1762          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1763          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1764          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1765          if (!buf)          if (!buf) {
1766                    head->read_eof = true;
1767                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1768            }
1769          if (head->read_avail || head->read_eof)          if (head->read_avail || head->read_eof)
1770                  return;                  return;
1771          head->read_eof = true;          head->read_eof = true;
# Line 1810  static void ccs_read_pid(struct ccs_io_b Line 1774  static void ccs_read_pid(struct ccs_io_b
1774          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
1775                  global_pid = true;                  global_pid = true;
1776          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1777          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1778  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1779          if (global_pid)          if (global_pid)
1780                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1781          else          else
1782                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1783  #else  #else
1784          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1785  #endif  #endif
# Line 1823  static void ccs_read_pid(struct ccs_io_b Line 1787  static void ccs_read_pid(struct ccs_io_b
1787                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1788                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1789          }          }
1790          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1791          if (!domain)          if (!domain)
1792                  return;                  return;
1793          if (!task_info)          if (!task_info)
# Line 1833  static void ccs_read_pid(struct ccs_io_b Line 1797  static void ccs_read_pid(struct ccs_io_b
1797                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1798                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1799                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1800                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1801                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1802                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1803                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
# Line 1842  static void ccs_read_pid(struct ccs_io_b Line 1806  static void ccs_read_pid(struct ccs_io_b
1806  }  }
1807    
1808  /**  /**
1809   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1810   *   *
1811   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1812   *   *
1813   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1814   */   */
1815  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1816  {  {
1817          char *data = head->write_buf;          char *data = head->write_buf;
1818          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1819          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1820                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1821          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1822                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool, const u8);
1823          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[8] = {
1824                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },
1825                                                             is_delete);                  { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1826          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                    ccs_write_domain_initializer },
1827                  return ccs_write_domain_initializer_policy(data, true,                  { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper },
1828                                                             is_delete);                  { CCS_KEYWORD_INITIALIZE_DOMAIN,
1829          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                    ccs_write_domain_initializer },
1830                  return ccs_write_aggregator_policy(data, is_delete);                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1831          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1832                  return ccs_write_globally_readable_policy(data, is_delete);                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1833          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1834                  return ccs_write_globally_usable_env_policy(data, is_delete);          };
1835          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          static const char *ccs_name[CCS_MAX_GROUP] = {
1836                  return ccs_write_pattern_policy(data, is_delete);                  [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1837          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                  [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1838                  return ccs_write_path_group_policy(data, is_delete);                  [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1839          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))          };
1840                  return ccs_write_number_group_policy(data, is_delete);          for (i = 0; i < 10; i++) {
1841          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1842                  return ccs_write_no_rewrite_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete, i < 2);
1843          if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))          }
1844                  return ccs_write_address_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++) {
1845          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))                  if (ccs_str_starts(&data, ccs_name[i]))
1846                  return ccs_write_reserved_port_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1847          return -EINVAL;          }
1848            {
1849                    int error;
1850                    struct ccs_condition *cond = NULL;
1851                    char *cp = ccs_find_condition_part(data);
1852                    if (cp) {
1853                            cond = ccs_get_condition(cp);
1854                            if (!cond)
1855                                    return -EINVAL;
1856                    }
1857                    error = ccs_write_domain2(data, &ccs_global_domain, cond,
1858                                              is_delete);
1859                    if (cond)
1860                            ccs_put_condition(cond);
1861                    return error;
1862            }
1863  }  }
1864    
1865  /**  /**
1866   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1867   *   *
1868   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1869     * @idx:  Index number.
1870     *
1871     * Returns true on success, false otherwise.
1872   *   *
1873   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1874   */   */
1875  static void ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1876  {  {
1877          if (head->read_eof)          struct list_head *gpos;
1878                  return;          struct list_head *mpos;
1879          switch (head->read_step) {          const char *w[3] = { "", "", "" };
1880          case 0:          if (idx == CCS_PATH_GROUP)
1881                  head->read_var2 = NULL;                  w[0] = CCS_KEYWORD_PATH_GROUP;
1882                  head->read_step = 1;          else if (idx == CCS_NUMBER_GROUP)
1883          case 1:                  w[0] = CCS_KEYWORD_NUMBER_GROUP;
1884                  if (!ccs_read_domain_keeper_policy(head))          else if (idx == CCS_ADDRESS_GROUP)
1885                          break;                  w[0] = CCS_KEYWORD_ADDRESS_GROUP;
1886                  head->read_var2 = NULL;          list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1887                  head->read_step = 2;                  struct ccs_group *group =
1888          case 2:                          list_entry(gpos, struct ccs_group, head.list);
1889                  if (!ccs_read_globally_readable_policy(head))                  w[1] = group->group_name->name;
1890                          break;                  list_for_each_cookie(mpos, head->read_var2,
1891                  head->read_var2 = NULL;                                       &group->member_list) {
1892                  head->read_step = 3;                          char buffer[128];
1893          case 3:                          struct ccs_acl_head *ptr =
1894                  if (!ccs_read_globally_usable_env_policy(head))                                  list_entry(mpos, struct ccs_acl_head, list);
1895                          break;                          if (ptr->is_deleted)
1896                  head->read_var2 = NULL;                                  continue;
1897                  head->read_step = 4;                          if (idx == CCS_PATH_GROUP) {
1898          case 4:                                  w[2] = container_of(ptr, struct ccs_path_group,
1899                  if (!ccs_read_domain_initializer_policy(head))                                                      head)->member_name->name;
1900                          break;                          } else if (idx == CCS_NUMBER_GROUP) {
1901                  head->read_var2 = NULL;                                  w[2] = buffer;
1902                  head->read_step = 6;                                  ccs_print_number(buffer, sizeof(buffer),
1903          case 6:                                                   &container_of
1904                  if (!ccs_read_aggregator_policy(head))                                                   (ptr, struct ccs_number_group,
1905                          break;                                                    head)->number);
1906                  head->read_var2 = NULL;                          } else if (idx == CCS_ADDRESS_GROUP) {
1907                  head->read_step = 7;                                  struct ccs_address_group *member =
1908          case 7:                                          container_of(ptr, typeof(*member),
1909                  if (!ccs_read_file_pattern(head))                                                       head);
1910                                    w[2] = buffer;
1911                                    if (member->is_ipv6)
1912                                            ccs_print_ipv6(buffer, sizeof(buffer),
1913                                                           member->min.ipv6,
1914                                                           member->max.ipv6);
1915                                    else
1916                                            ccs_print_ipv4(buffer, sizeof(buffer),
1917                                                           member->min.ipv4,
1918                                                           member->max.ipv4);
1919                            }
1920                            if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1921                                               w[2]))
1922                                    return false;
1923                    }
1924            }
1925            return true;
1926    }
1927    
1928    /**
1929     * ccs_read_policy - Read "struct ccs_..._entry" list.
1930     *
1931     * @head: Pointer to "struct ccs_io_buffer".
1932     * @idx:  Index number.
1933     *
1934     * Returns true on success, false otherwise.
1935     *
1936     * Caller holds ccs_read_lock().
1937     */
1938    static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1939    {
1940            struct list_head *pos;
1941            list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1942                    const char *w[4] = { "", "", "", "" };
1943                    char buffer[16];
1944                    struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1945                                                            list);
1946                    if (acl->is_deleted)
1947                            continue;
1948                    switch (idx) {
1949                    case CCS_ID_DOMAIN_KEEPER:
1950                            {
1951                                    struct ccs_domain_keeper *ptr =
1952                                            container_of(acl, typeof(*ptr), head);
1953                                    w[0] = ptr->is_not ?
1954                                            CCS_KEYWORD_NO_KEEP_DOMAIN :
1955                                            CCS_KEYWORD_KEEP_DOMAIN;
1956                                    if (ptr->program) {
1957                                            w[1] = ptr->program->name;
1958                                            w[2] = " from ";
1959                                    }
1960                                    w[3] = ptr->domainname->name;
1961                            }
1962                          break;                          break;
1963                  head->read_var2 = NULL;                  case CCS_ID_DOMAIN_INITIALIZER:
1964                  head->read_step = 8;                          {
1965          case 8:                                  struct ccs_domain_initializer *ptr =
1966                  if (!ccs_read_no_rewrite_policy(head))                                          container_of(acl, typeof(*ptr), head);
1967                                    w[0] = ptr->is_not ?
1968                                            CCS_KEYWORD_NO_INITIALIZE_DOMAIN :
1969                                            CCS_KEYWORD_INITIALIZE_DOMAIN;
1970                                    w[1] = ptr->program->name;
1971                                    if (ptr->domainname) {
1972                                            w[2] = " from ";
1973                                            w[3] = ptr->domainname->name;
1974                                    }
1975                            }
1976                          break;                          break;
1977                  head->read_var2 = NULL;                  case CCS_ID_AGGREGATOR:
1978                  head->read_step = 9;                          {
1979          case 9:                                  struct ccs_aggregator *ptr =
1980                  if (!ccs_read_path_group_policy(head))                                          container_of(acl, typeof(*ptr), head);
1981                                    w[0] = CCS_KEYWORD_AGGREGATOR;
1982                                    w[1] = ptr->original_name->name;
1983                                    w[2] = " ";
1984                                    w[3] = ptr->aggregated_name->name;
1985                            }
1986                          break;                          break;
1987                  head->read_var1 = NULL;                  case CCS_ID_PATTERN:
1988                  head->read_var2 = NULL;                          {
1989                  head->read_step = 10;                                  struct ccs_pattern *ptr =
1990          case 10:                                          container_of(acl, typeof(*ptr), head);
1991                  if (!ccs_read_number_group_policy(head))                                  w[0] = CCS_KEYWORD_FILE_PATTERN;
1992                                    w[1] = ptr->pattern->name;
1993                            }
1994                          break;                          break;
1995                  head->read_var1 = NULL;                  case CCS_ID_NO_REWRITE:
1996                  head->read_var2 = NULL;                          {
1997                  head->read_step = 11;                                  struct ccs_no_rewrite *ptr =
1998          case 11:                                          container_of(acl, typeof(*ptr), head);
1999                  if (!ccs_read_address_group_policy(head))                                  w[0] = CCS_KEYWORD_DENY_REWRITE;
2000                                    w[1] = ptr->pattern->name;
2001                            }
2002                          break;                          break;
2003                  head->read_var2 = NULL;                  case CCS_ID_RESERVEDPORT:
2004                  head->read_step = 12;                          {
2005          case 12:                                  struct ccs_reserved *ptr =
2006                  if (!ccs_read_reserved_port_policy(head))                                          container_of(acl, typeof(*ptr), head);
2007                                    const u16 min_port = ptr->min_port;
2008                                    const u16 max_port = ptr->max_port;
2009                                    w[0] = CCS_KEYWORD_DENY_AUTOBIND;
2010                                    snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
2011                                             min_port, min_port != max_port ?
2012                                             '-' : '\0', max_port);
2013                                    buffer[sizeof(buffer) - 1] = '\0';
2014                                    w[1] = buffer;
2015                            }
2016                          break;                          break;
2017                  head->read_eof = true;                  default:
2018                            continue;
2019                    }
2020                    if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
2021                            return false;
2022          }          }
2023            return true;
2024    }
2025    
2026    static void ccs_read_global_domain(struct ccs_io_buffer *head)
2027    {
2028            if (!head->read_eof)
2029                    head->read_eof = ccs_read_domain2(head, &ccs_global_domain);
2030    }
2031    
2032    /**
2033     * ccs_read_exception - Read exception policy.
2034     *
2035     * @head: Pointer to "struct ccs_io_buffer".
2036     *
2037     * Caller holds ccs_read_lock().
2038     */
2039    static void ccs_read_exception(struct ccs_io_buffer *head)
2040    {
2041            if (head->read_eof)
2042                    return;
2043            while (head->read_step < CCS_MAX_POLICY &&
2044                   ccs_read_policy(head, head->read_step))
2045                    head->read_step++;
2046            if (head->read_step < CCS_MAX_POLICY)
2047                    return;
2048            while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2049                   ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2050                    head->read_step++;
2051            if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2052                    return;
2053            head->read = ccs_read_global_domain;
2054  }  }
2055    
2056  /**  /**
2057   * ccs_get_argv0 - Get argv[0].   * ccs_get_argv0 - Get argv[0].
2058   *   *
2059   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve".
2060   *   *
2061   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
2062   */   */
2063  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_get_argv0(struct ccs_execve *ee)
2064  {  {
2065          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
2066          char *arg_ptr = ee->tmp;          char *arg_ptr = ee->tmp;
# Line 2013  static bool ccs_get_argv0(struct ccs_exe Line 2109  static bool ccs_get_argv0(struct ccs_exe
2109  /**  /**
2110   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_get_execute_condition - Get condition part for execute requests.
2111   *   *
2112   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve".
2113   *   *
2114   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
2115   */   */
2116  static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry  static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve
2117                                                         *ee)                                                         *ee)
2118  {  {
2119          struct ccs_condition *cond;          struct ccs_condition *cond;
# Line 2025  static struct ccs_condition *ccs_get_exe Line 2121  static struct ccs_condition *ccs_get_exe
2121          int len = 256;          int len = 256;
2122          char *realpath = NULL;          char *realpath = NULL;
2123          char *argv0 = NULL;          char *argv0 = NULL;
2124          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);          const struct ccs_profile *profile = ccs_profile(ccs_current_domain()->
2125                                                            profile);
2126          if (profile->learning->learning_exec_realpath) {          if (profile->learning->learning_exec_realpath) {
2127                  struct file *file = ee->bprm->file;                  struct file *file = ee->bprm->file;
2128  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
# Line 2043  static struct ccs_condition *ccs_get_exe Line 2140  static struct ccs_condition *ccs_get_exe
2140                          len += strlen(argv0) + 16;                          len += strlen(argv0) + 16;
2141                  }                  }
2142          }          }
2143          buf = kmalloc(len, GFP_KERNEL);          buf = kmalloc(len, CCS_GFP_FLAGS);
2144          if (!buf) {          if (!buf) {
2145                  kfree(realpath);                  kfree(realpath);
2146                  return NULL;                  return NULL;
# Line 2089  static struct ccs_condition *ccs_get_sym Line 2186  static struct ccs_condition *ccs_get_sym
2186                  symlink = r->obj->symlink_target->name;                  symlink = r->obj->symlink_target->name;
2187                  len += strlen(symlink) + 18;                  len += strlen(symlink) + 18;
2188          }          }
2189          buf = kmalloc(len, GFP_KERNEL);          buf = kmalloc(len, CCS_GFP_FLAGS);
2190          if (!buf)          if (!buf)
2191                  return NULL;                  return NULL;
2192          snprintf(buf, len - 1, "if");          snprintf(buf, len - 1, "if");
# Line 2137  static atomic_t ccs_query_observers = AT Line 2234  static atomic_t ccs_query_observers = AT
2234   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt:     The printf()'s format string, followed by parameters.
2235   *   *
2236   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
2237   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
2238   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
2239   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
2240   */   */
2241  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
2242  {  {
# Line 2151  int ccs_supervisor(struct ccs_request_in Line 2248  int ccs_supervisor(struct ccs_request_in
2248          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query_entry *ccs_query_entry = NULL;
2249          bool quota_exceeded = false;          bool quota_exceeded = false;
2250          char *header;          char *header;
2251          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
                 r->domain = ccs_current_domain();  
2252          switch (r->mode) {          switch (r->mode) {
2253                  char *buffer;                  char *buffer;
2254                  struct ccs_condition *cond;                  struct ccs_condition *cond;
# Line 2162  int ccs_supervisor(struct ccs_request_in Line 2258  int ccs_supervisor(struct ccs_request_in
2258                  va_start(args, fmt);                  va_start(args, fmt);
2259                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
2260                  va_end(args);                  va_end(args);
2261                  buffer = kmalloc(len, GFP_KERNEL);                  buffer = kmalloc(len, CCS_GFP_FLAGS);
2262                  if (!buffer)                  if (!buffer)
2263                          return 0;                          return 0;
2264                  va_start(args, fmt);                  va_start(args, fmt);
# Line 2178  int ccs_supervisor(struct ccs_request_in Line 2274  int ccs_supervisor(struct ccs_request_in
2274                          cond = ccs_get_condition(str);                          cond = ccs_get_condition(str);
2275                  } else                  } else
2276                          cond = NULL;                          cond = NULL;
2277                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                  ccs_write_domain2(buffer, domain, cond, false);
2278                  ccs_put_condition(cond);                  ccs_put_condition(cond);
2279                  kfree(buffer);                  kfree(buffer);
2280                  /* fall through */                  /* fall through */
# Line 2189  int ccs_supervisor(struct ccs_request_in Line 2285  int ccs_supervisor(struct ccs_request_in
2285                  int i;                  int i;
2286                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2287                          return -EPERM;                          return -EPERM;
2288                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2289                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2290                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2291                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
# Line 2199  int ccs_supervisor(struct ccs_request_in Line 2295  int ccs_supervisor(struct ccs_request_in
2295          va_start(args, fmt);          va_start(args, fmt);
2296          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
2297          va_end(args);          va_end(args);
2298          header = ccs_init_audit_log(&len, r);          header = ccs_init_log(&len, r);
2299          if (!header)          if (!header)
2300                  goto out;                  goto out;
2301          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2302          if (!ccs_query_entry)          if (!ccs_query_entry)
2303                  goto out;                  goto out;
2304          len = ccs_round2(len);          len = ccs_round2(len);
2305          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2306          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2307                  goto out;                  goto out;
2308          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
# Line 2248  int ccs_supervisor(struct ccs_request_in Line 2344  int ccs_supervisor(struct ccs_request_in
2344          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2345          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2346          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2347                  error = 1;                  error = CCS_RETRY_REQUEST;
2348                  r->retry++;                  r->retry++;
2349                  break;                  break;
2350          case 1:          case 1:
# Line 2288  static int ccs_poll_query(struct file *f Line 2384  static int ccs_poll_query(struct file *f
2384          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2385                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2386                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2387                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr =
2388                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, struct ccs_query_entry, list);
2389                          if (ptr->answer)                          if (ptr->answer)
2390                                  continue;                                  continue;
2391                          found = true;                          found = true;
# Line 2339  static void ccs_read_query(struct ccs_io Line 2435  static void ccs_read_query(struct ccs_io
2435                  head->read_step = 0;                  head->read_step = 0;
2436                  return;                  return;
2437          }          }
2438          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2439          if (!buf)          if (!buf)
2440                  return;                  return;
2441          pos = 0;          pos = 0;
# Line 2415  static void ccs_read_version(struct ccs_ Line 2511  static void ccs_read_version(struct ccs_
2511  {  {
2512          if (head->read_eof)          if (head->read_eof)
2513                  return;                  return;
2514          ccs_io_printf(head, "1.7.0");          ccs_io_printf(head, "1.7.2");
2515          head->read_eof = true;          head->read_eof = true;
2516  }  }
2517    
# Line 2446  static void ccs_read_self_domain(struct Line 2542  static void ccs_read_self_domain(struct
2542   */   */
2543  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2544  {  {
2545          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2546          if (!head)          if (!head)
2547                  return -ENOMEM;                  return -ENOMEM;
2548          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2549          head->type = type;          head->type = type;
2550          switch (type) {          switch (type) {
2551          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2552                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2553                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2554                  break;                  break;
2555          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2556                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2557                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2558                  break;                  break;
2559  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2560          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;  
2561          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2562                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2563                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2564                  break;                  break;
2565  #endif  #endif
2566          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2507  int ccs_open_control(const u8 type, stru Line 2600  int ccs_open_control(const u8 type, stru
2600                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2601                  break;                  break;
2602          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2603                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2604                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2605                  break;                  break;
2606          }          }
2607          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2522  int ccs_open_control(const u8 type, stru Line 2615  int ccs_open_control(const u8 type, stru
2615                  /* Don't allocate read_buf for poll() access. */                  /* Don't allocate read_buf for poll() access. */
2616                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2617                          head->readbuf_size = 4096;                          head->readbuf_size = 4096;
2618                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2619                  if (!head->read_buf) {                  if (!head->read_buf) {
2620                          kfree(head);                          kfree(head);
2621                          return -ENOMEM;                          return -ENOMEM;
# Line 2536  int ccs_open_control(const u8 type, stru Line 2629  int ccs_open_control(const u8 type, stru
2629                  head->write = NULL;                  head->write = NULL;
2630          } else if (head->write) {          } else if (head->write) {
2631                  head->writebuf_size = 4096;                  head->writebuf_size = 4096;
2632                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2633                  if (!head->write_buf) {                  if (!head->write_buf) {
2634                          kfree(head->read_buf);                          kfree(head->read_buf);
2635                          kfree(head);                          kfree(head);
# Line 2545  int ccs_open_control(const u8 type, stru Line 2638  int ccs_open_control(const u8 type, stru
2638          }          }
2639          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2640              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2641                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2642          file->private_data = head;          file->private_data = head;
2643          /*          /*
2644           * 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 2598  int ccs_read_control(struct file *file, Line 2691  int ccs_read_control(struct file *file,
2691          int len = 0;          int len = 0;
2692          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2693          char *cp;          char *cp;
2694            int idx;
2695          if (!head->read)          if (!head->read)
2696                  return -ENOSYS;                  return -ENOSYS;
2697          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2698                  return -EFAULT;                  return -EFAULT;
2699          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2700                  return -EINTR;                  return -EINTR;
2701            idx = ccs_read_lock();
2702          while (1) {          while (1) {
2703                  /* Call the policy handler. */                  /* Call the policy handler. */
2704                  head->read(head);                  head->read(head);
# Line 2612  int ccs_read_control(struct file *file, Line 2707  int ccs_read_control(struct file *file,
2707                  if (len || head->poll || head->read_eof)                  if (len || head->poll || head->read_eof)
2708                          break;                          break;
2709                  len = head->readbuf_size * 2;                  len = head->readbuf_size * 2;
2710                  cp = kzalloc(len, GFP_KERNEL);                  cp = kzalloc(len, CCS_GFP_FLAGS);
2711                  if (!cp) {                  if (!cp) {
2712                          len = -ENOMEM;                          len = -ENOMEM;
2713                          goto out;                          goto out;
# Line 2634  int ccs_read_control(struct file *file, Line 2729  int ccs_read_control(struct file *file,
2729          head->read_avail -= len;          head->read_avail -= len;
2730          memmove(cp, cp + len, head->read_avail);          memmove(cp, cp + len, head->read_avail);
2731   out:   out:
2732            ccs_read_unlock(idx);
2733          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2734          return len;          return len;
2735  }  }
# Line 2654  int ccs_write_control(struct file *file, Line 2750  int ccs_write_control(struct file *file,
2750          int error = buffer_len;          int error = buffer_len;
2751          int avail_len = buffer_len;          int avail_len = buffer_len;
2752          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2753            int idx;
2754          if (!head->write)          if (!head->write)
2755                  return -ENOSYS;                  return -ENOSYS;
2756          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2757                  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;  
2758          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2759                  return -EINTR;                  return -EINTR;
2760            idx = ccs_read_lock();
2761            /* Don't allow updating policies by non manager programs. */
2762            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2763                !ccs_manager()) {
2764                    ccs_read_unlock(idx);
2765                    mutex_unlock(&head->io_sem);
2766                    return -EPERM;
2767            }
2768          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2769          while (avail_len > 0) {          while (avail_len > 0) {
2770                  char c;                  char c;
2771                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->write_avail >= head->writebuf_size - 1) {
2772                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2773                          char *cp = kzalloc(len, GFP_KERNEL);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2774                          if (!cp) {                          if (!cp) {
2775                                  error = -ENOMEM;                                  error = -ENOMEM;
2776                                  break;                                  break;
# Line 2695  int ccs_write_control(struct file *file, Line 2795  int ccs_write_control(struct file *file,
2795                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2796                  head->write(head);                  head->write(head);
2797          }          }
2798            ccs_read_unlock(idx);
2799          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2800          return error;          return error;
2801  }  }
# Line 2718  int ccs_close_control(struct file *file) Line 2819  int ccs_close_control(struct file *file)
2819                  atomic_dec(&ccs_query_observers);                  atomic_dec(&ccs_query_observers);
2820          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2821              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2822                  ccs_read_unlock(head->reader_idx);                  ccs_unlock(head->reader_idx);
2823          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2824          kfree(head->read_buf);          kfree(head->read_buf);
2825          head->read_buf = NULL;          head->read_buf = NULL;
# Line 2731  int ccs_close_control(struct file *file) Line 2832  int ccs_close_control(struct file *file)
2832                  ccs_run_gc();                  ccs_run_gc();
2833          return 0;          return 0;
2834  }  }
2835    
2836    void __init ccs_policy_io_init(void)
2837    {
2838            ccsecurity_ops.check_profile = ccs_check_profile;
2839    }

Legend:
Removed from v.3079  
changed lines
  Added in v.3697

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