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

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 3772 by kumaneko, Mon Jun 21 06:05:52 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/06/04
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 33  static struct ccs_profile ccs_default_pr Line 33  static struct ccs_profile ccs_default_pr
33          .preference.permissive_verbose = true          .preference.permissive_verbose = true
34  };  };
35    
36    /* Profile version. Currently only 20090903 is defined. */
37    static unsigned int ccs_profile_version;
38    
39  /* Profile table. Memory is allocated as needed. */  /* Profile table. Memory is allocated as needed. */
40  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
 /* Lock for protecting "struct ccs_profile"->comment  */  
 static DEFINE_SPINLOCK(ccs_profile_comment_lock);  
   
42  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
43  static const char *ccs_mode_4[4] = {  static const char *ccs_mode[CCS_CONFIG_MAX_MODE] = {
44          "disabled", "learning", "permissive", "enforcing"          [CCS_CONFIG_DISABLED] = "disabled",
45            [CCS_CONFIG_LEARNING] = "learning",
46            [CCS_CONFIG_PERMISSIVE] = "permissive",
47            [CCS_CONFIG_ENFORCING] = "enforcing"
48  };  };
49    
50  /* String table for /proc/ccs/profile */  /* String table for /proc/ccs/profile */
# Line 94  static const char *ccs_mac_keywords[CCS_ Line 97  static const char *ccs_mac_keywords[CCS_
97          = "file::umount",          = "file::umount",
98          [CCS_MAC_FILE_PIVOT_ROOT]          [CCS_MAC_FILE_PIVOT_ROOT]
99          = "file::pivot_root",          = "file::pivot_root",
100            [CCS_MAC_FILE_TRANSIT]
101            = "file::transit",
102          [CCS_MAC_ENVIRON]          [CCS_MAC_ENVIRON]
103          = "misc::env",          = "misc::env",
104          [CCS_MAC_NETWORK_UDP_BIND]          [CCS_MAC_NETWORK_UDP_BIND]
# Line 244  bool ccs_io_printf(struct ccs_io_buffer Line 249  bool ccs_io_printf(struct ccs_io_buffer
249  }  }
250    
251  /**  /**
252   * ccs_find_or_assign_new_profile - Create a new profile.   * ccs_assign_profile - Create a new profile.
253   *   *
254   * @profile: Profile number to create.   * @profile: Profile number to create.
255   *   *
256   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.   * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
257   */   */
258  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)  
259  {  {
260          struct ccs_profile *ptr;          struct ccs_profile *ptr;
261          struct ccs_profile *entry;          struct ccs_profile *entry;
# Line 260  static struct ccs_profile *ccs_find_or_a Line 264  static struct ccs_profile *ccs_find_or_a
264          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
265          if (ptr)          if (ptr)
266                  return ptr;                  return ptr;
267          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
268          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
269                    goto out;
270          ptr = ccs_profile_ptr[profile];          ptr = ccs_profile_ptr[profile];
271          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {          if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
272                  ptr = entry;                  ptr = entry;
# Line 278  static struct ccs_profile *ccs_find_or_a Line 283  static struct ccs_profile *ccs_find_or_a
283                  entry = NULL;                  entry = NULL;
284          }          }
285          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
286     out:
287          kfree(entry);          kfree(entry);
288          return ptr;          return ptr;
289  }  }
# Line 285  static struct ccs_profile *ccs_find_or_a Line 291  static struct ccs_profile *ccs_find_or_a
291  /**  /**
292   * ccs_check_profile - Check all profiles currently assigned to domains are defined.   * ccs_check_profile - Check all profiles currently assigned to domains are defined.
293   */   */
294  void ccs_check_profile(void)  static void ccs_check_profile(void)
295  {  {
296          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
297            const int idx = ccs_read_lock();
298          ccs_policy_loaded = true;          ccs_policy_loaded = true;
299          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
300                  const u8 profile = domain->profile;                  const u8 profile = domain->profile;
# Line 296  void ccs_check_profile(void) Line 303  void ccs_check_profile(void)
303                  panic("Profile %u (used by '%s') not defined.\n",                  panic("Profile %u (used by '%s') not defined.\n",
304                        profile, domain->domainname->name);                        profile, domain->domainname->name);
305          }          }
306            ccs_read_unlock(idx);
307            if (ccs_profile_version != 20090903)
308                    panic("Profile version %u is not supported.\n",
309                          ccs_profile_version);
310            printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");
311            printk(KERN_INFO "Mandatory Access Control activated.\n");
312  }  }
313    
314  /**  /**
# Line 314  struct ccs_profile *ccs_profile(const u8 Line 327  struct ccs_profile *ccs_profile(const u8
327          return ptr;          return ptr;
328  }  }
329    
330  /**  static s8 ccs_find_yesno(const char *string, const char *find)
  * ccs_write_profile - Write profile table.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_write_profile(struct ccs_io_buffer *head)  
331  {  {
332          char *data = head->write_buf;          const char *cp = strstr(string, find);
333          unsigned int i;          if (cp) {
334          int value;                  cp += strlen(find);
335          int mode;                  if (!strncmp(cp, "=yes", 4))
336          u8 config;                          return 1;
337          bool use_default = false;                  else if (!strncmp(cp, "=no", 3))
338          char *cp;                          return 0;
         struct ccs_profile *profile;  
         i = simple_strtoul(data, &cp, 10);  
         if (data == cp) {  
                 profile = &ccs_default_profile;  
         } else {  
                 if (*cp != '-')  
                         return -EINVAL;  
                 data = cp + 1;  
                 profile = ccs_find_or_assign_new_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
339          }          }
340          cp = strchr(data, '=');          return -1;
341          if (!cp)  }
342                  return -EINVAL;  
343          *cp++ = '\0';  static void ccs_set_bool(bool *b, const char *string, const char *find)
344          if (profile != &ccs_default_profile)  {
345                  use_default = strstr(cp, "use_default") != NULL;          switch (ccs_find_yesno(string, find)) {
346          if (strstr(cp, "verbose=yes"))          case 1:
347                  value = 1;                  *b = true;
348          else if (strstr(cp, "verbose=no"))                  break;
349                  value = 0;          case 0:
350          else                  *b = false;
351                  value = -1;                  break;
352          if (!strcmp(data, "PREFERENCE::audit")) {          }
353  #ifdef CONFIG_CCSECURITY_AUDIT  }
354                  char *cp2;  
355  #endif  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
356    {
357            const char *cp = strstr(string, find);
358            if (cp)
359                    sscanf(cp + strlen(find), "=%u", i);
360    }
361    
362    static void ccs_set_pref(const char *name, const char *value,
363                             const bool use_default, struct ccs_profile *profile)
364    {
365            struct ccs_preference **pref;
366            bool *verbose;
367            if (!strcmp(name, "audit")) {
368                  if (use_default) {                  if (use_default) {
369                          profile->audit = &ccs_default_profile.preference;                          pref = &profile->audit;
370                          return 0;                          goto set_default;
371                  }                  }
372                  profile->audit = &profile->preference;                  profile->audit = &profile->preference;
373  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
374                  cp2 = strstr(cp, "max_grant_log=");                  ccs_set_uint(&profile->preference.audit_max_grant_log, value,
375                  if (cp2)                               "max_grant_log");
376                          sscanf(cp2 + 14, "%u",                  ccs_set_uint(&profile->preference.audit_max_reject_log, value,
377                                 &profile->preference.audit_max_grant_log);                               "max_reject_log");
                 cp2 = strstr(cp, "max_reject_log=");  
                 if (cp2)  
                         sscanf(cp2 + 15, "%u",  
                                &profile->preference.audit_max_reject_log);  
378  #endif  #endif
379                  if (strstr(cp, "task_info=yes"))                  ccs_set_bool(&profile->preference.audit_task_info, value,
380                          profile->preference.audit_task_info = true;                               "task_info");
381                  else if (strstr(cp, "task_info=no"))                  ccs_set_bool(&profile->preference.audit_path_info, value,
382                          profile->preference.audit_task_info = false;                               "path_info");
383                  if (strstr(cp, "path_info=yes"))                  return;
                         profile->preference.audit_path_info = true;  
                 else if (strstr(cp, "path_info=no"))  
                         profile->preference.audit_path_info = false;  
                 return 0;  
384          }          }
385          if (!strcmp(data, "PREFERENCE::enforcing")) {          if (!strcmp(name, "enforcing")) {
                 char *cp2;  
386                  if (use_default) {                  if (use_default) {
387                          profile->enforcing = &ccs_default_profile.preference;                          pref = &profile->enforcing;
388                          return 0;                          goto set_default;
389                  }                  }
390                  profile->enforcing = &profile->preference;                  profile->enforcing = &profile->preference;
391                  if (value >= 0)                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
392                          profile->preference.enforcing_verbose = value;                               "penalty");
393                  cp2 = strstr(cp, "penalty=");                  verbose = &profile->preference.enforcing_verbose;
394                  if (cp2)                  goto set_verbose;
                         sscanf(cp2 + 8, "%u",  
                                &profile->preference.enforcing_penalty);  
                 return 0;  
395          }          }
396          if (!strcmp(data, "PREFERENCE::permissive")) {          if (!strcmp(name, "permissive")) {
397                  if (use_default) {                  if (use_default) {
398                          profile->permissive = &ccs_default_profile.preference;                          pref = &profile->permissive;
399                          return 0;                          goto set_default;
400                  }                  }
401                  profile->permissive = &profile->preference;                  profile->permissive = &profile->preference;
402                  if (value >= 0)                  verbose = &profile->preference.permissive_verbose;
403                          profile->preference.permissive_verbose = value;                  goto set_verbose;
                 return 0;  
404          }          }
405          if (!strcmp(data, "PREFERENCE::learning")) {          if (!strcmp(name, "learning")) {
                 char *cp2;  
406                  if (use_default) {                  if (use_default) {
407                          profile->learning = &ccs_default_profile.preference;                          pref = &profile->learning;
408                          return 0;                          goto set_default;
409                  }                  }
410                  profile->learning = &profile->preference;                  profile->learning = &profile->preference;
411                  if (value >= 0)                  ccs_set_uint(&profile->preference.learning_max_entry, value,
412                          profile->preference.learning_verbose = value;                               "max_entry");
413                  cp2 = strstr(cp, "max_entry=");                  ccs_set_bool(&profile->preference.learning_exec_realpath,
414                  if (cp2)                               value, "exec.realpath");
415                          sscanf(cp2 + 10, "%u",                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
416                                 &profile->preference.learning_max_entry);                               "exec.argv0");
417                  if (strstr(cp, "exec.realpath=yes"))                  ccs_set_bool(&profile->preference.learning_symlink_target,
418                          profile->preference.learning_exec_realpath = true;                               value, "symlink.target");
419                  else if (strstr(cp, "exec.realpath=no"))                  verbose = &profile->preference.learning_verbose;
420                          profile->preference.learning_exec_realpath = false;                  goto set_verbose;
421                  if (strstr(cp, "exec.argv0=yes"))          }
422                          profile->preference.learning_exec_argv0 = true;          return;
423                  else if (strstr(cp, "exec.argv0=no"))   set_default:
424                          profile->preference.learning_exec_argv0 = false;          *pref = &ccs_default_profile.preference;
425                  if (strstr(cp, "symlink.target=yes"))          return;
426                          profile->preference.learning_symlink_target = true;   set_verbose:
427                  else if (strstr(cp, "symlink.target=no"))          ccs_set_bool(verbose, value, "verbose");
428                          profile->preference.learning_symlink_target = false;  }
429                  return 0;  
430          }  static int ccs_set_mode(char *name, const char *value, const bool use_default,
431          if (profile == &ccs_default_profile)                          struct ccs_profile *profile)
432                  return -EINVAL;  {
433          if (!strcmp(data, "COMMENT")) {          u8 i;
434                  const struct ccs_path_info *new_comment = ccs_get_name(cp);          u8 config;
435                  const struct ccs_path_info *old_comment;          if (!strcmp(name, "CONFIG")) {
                 /* Protect reader from ccs_put_name(). */  
                 spin_lock(&ccs_profile_comment_lock);  
                 old_comment = profile->comment;  
                 profile->comment = new_comment;  
                 spin_unlock(&ccs_profile_comment_lock);  
                 ccs_put_name(old_comment);  
                 return 0;  
         }  
         if (!strcmp(data, "CONFIG")) {  
436                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
437                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
438                  config = profile->default_config;                  config = profile->default_config;
439          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
440                  config = 0;                  config = 0;
441                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
442                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
443                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
444                                  continue;                                  continue;
445                          config = profile->config[i];                          config = profile->config[i];
446                          break;                          break;
# Line 470  static int ccs_write_profile(struct ccs_ Line 454  static int ccs_write_profile(struct ccs_
454          if (use_default) {          if (use_default) {
455                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
456          } else {          } else {
457                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
458                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
459                            if (strstr(value, ccs_mode[mode]))
460                                  /*                                  /*
461                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
462                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
# Line 479  static int ccs_write_profile(struct ccs_ Line 464  static int ccs_write_profile(struct ccs_
464                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
465  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
466                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
467                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "grant_log")) {
468                            case 1:
469                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
470                          else if (strstr(cp, "grant_log=no"))                                  break;
471                            case 0:
472                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
473                          if (strstr(cp, "reject_log=yes"))                                  break;
474                            }
475                            switch (ccs_find_yesno(value, "reject_log")) {
476                            case 1:
477                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
478                          else if (strstr(cp, "reject_log=no"))                                  break;
479                            case 0:
480                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
481                                    break;
482                            }
483                  }                  }
484  #endif  #endif
485          }          }
# Line 499  static int ccs_write_profile(struct ccs_ Line 492  static int ccs_write_profile(struct ccs_
492  }  }
493    
494  /**  /**
495     * ccs_write_profile - Write profile table.
496     *
497     * @head: Pointer to "struct ccs_io_buffer".
498     *
499     * Returns 0 on success, negative value otherwise.
500     */
501    static int ccs_write_profile(struct ccs_io_buffer *head)
502    {
503            char *data = head->write_buf;
504            bool use_default = false;
505            char *cp;
506            int i;
507            struct ccs_profile *profile;
508            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
509                    return 0;
510            i = simple_strtoul(data, &cp, 10);
511            if (data == cp) {
512                    profile = &ccs_default_profile;
513            } else {
514                    if (*cp != '-')
515                            return -EINVAL;
516                    data = cp + 1;
517                    profile = ccs_assign_profile(i);
518                    if (!profile)
519                            return -EINVAL;
520            }
521            cp = strchr(data, '=');
522            if (!cp)
523                    return -EINVAL;
524            *cp++ = '\0';
525            if (profile != &ccs_default_profile)
526                    use_default = strstr(cp, "use_default") != NULL;
527            if (ccs_str_starts(&data, "PREFERENCE::")) {
528                    ccs_set_pref(data, cp, use_default, profile);
529                    return 0;
530            }
531            if (profile == &ccs_default_profile)
532                    return -EINVAL;
533            if (!strcmp(data, "COMMENT")) {
534                    const struct ccs_path_info *old_comment = profile->comment;
535                    profile->comment = ccs_get_name(cp);
536                    ccs_put_name(old_comment);
537                    return 0;
538            }
539            return ccs_set_mode(data, cp, use_default, profile);
540    }
541    
542    static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)
543    {
544            struct ccs_preference *pref = &ccs_default_profile.preference;
545            const struct ccs_profile *profile = idx >= 0 ?
546                    ccs_profile_ptr[idx] : NULL;
547            char buffer[16] = "";
548            if (profile) {
549                    buffer[sizeof(buffer) - 1] = '\0';
550                    snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
551            }
552            if (profile) {
553                    pref = profile->audit;
554                    if (pref == &ccs_default_profile.preference)
555                            pref = NULL;
556            }
557            if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "
558    #ifdef CONFIG_CCSECURITY_AUDIT
559                                       "max_grant_log=%u max_reject_log=%u "
560    #endif
561                                       "task_info=%s path_info=%s }\n", buffer,
562                                       "audit",
563    #ifdef CONFIG_CCSECURITY_AUDIT
564                                       pref->audit_max_grant_log,
565                                       pref->audit_max_reject_log,
566    #endif
567                                       ccs_yesno(pref->audit_task_info),
568                                       ccs_yesno(pref->audit_path_info)))
569                    return false;
570            if (profile) {
571                    pref = profile->learning;
572                    if (pref == &ccs_default_profile.preference)
573                            pref = NULL;
574            }
575            if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ "
576                                       "verbose=%s max_entry=%u exec.realpath=%s "
577                                       "exec.argv0=%s symlink.target=%s }\n",
578                                       buffer, "learning",
579                                       ccs_yesno(pref->learning_verbose),
580                                       pref->learning_max_entry,
581                                       ccs_yesno(pref->learning_exec_realpath),
582                                       ccs_yesno(pref->learning_exec_argv0),
583                                       ccs_yesno(pref->learning_symlink_target)))
584                    return false;
585            if (profile) {
586                    pref = profile->permissive;
587                    if (pref == &ccs_default_profile.preference)
588                            pref = NULL;
589            }
590            if (pref && !ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
591                                       buffer, "permissive",
592                                       ccs_yesno(pref->permissive_verbose)))
593                    return false;
594            if (profile) {
595                    pref = profile->enforcing;
596                    if (pref == &ccs_default_profile.preference)
597                            pref = NULL;
598            }
599            return !pref || ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
600                                          "penalty=%u }\n", buffer, "enforcing",
601                                          ccs_yesno(pref->enforcing_verbose),
602                                          pref->enforcing_penalty);
603    }
604    
605    /**
606   * ccs_read_profile - Read profile table.   * ccs_read_profile - Read profile table.
607   *   *
608   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
# Line 511  static void ccs_read_profile(struct ccs_ Line 615  static void ccs_read_profile(struct ccs_
615          if (head->read_bit)          if (head->read_bit)
616                  goto body;                  goto body;
617          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
618          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);  
619          head->read_bit = 1;          head->read_bit = 1;
620   body:   body:
621          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 624  static void ccs_read_profile(struct ccs_
624                  int i;                  int i;
625                  int pos;                  int pos;
626                  const struct ccs_profile *profile = ccs_profile_ptr[index];                  const struct ccs_profile *profile = ccs_profile_ptr[index];
627                    const struct ccs_path_info *comment;
628                  head->read_step = index;                  head->read_step = index;
629                  if (!profile)                  if (!profile)
630                          continue;                          continue;
631                  pos = head->read_avail;                  pos = head->read_avail;
632                  spin_lock(&ccs_profile_comment_lock);                  comment = profile->comment;
633                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,
634                                       profile->comment ? profile->comment->name                                       comment ? comment->name : "");
                                      : "");  
                 spin_unlock(&ccs_profile_comment_lock);  
635                  if (!done)                  if (!done)
636                          goto out;                          goto out;
637                  config = profile->default_config;                  config = profile->default_config;
638  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
639                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
640                                     "reject_log=%s }\n", index,                                     "grant_log=%s reject_log=%s }\n", index,
641                                     ccs_mode_4[config & 3],                                     "CONFIG", "", ccs_mode[config & 3],
642                                     ccs_yesno(config &                                     ccs_yesno(config &
643                                               CCS_CONFIG_WANT_GRANT_LOG),                                               CCS_CONFIG_WANT_GRANT_LOG),
644                                     ccs_yesno(config &                                     ccs_yesno(config &
645                                               CCS_CONFIG_WANT_REJECT_LOG)))                                               CCS_CONFIG_WANT_REJECT_LOG)))
646                          goto out;                          goto out;
647  #else  #else
648                  if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s }\n", index,                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,
649                                     ccs_mode_4[config & 3]))                                     "CONFIG", "", ccs_mode[config & 3]))
650                          goto out;                          goto out;
651  #endif  #endif
652                  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 661  static void ccs_read_profile(struct ccs_
661  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
662                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);
663                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);
664                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s "                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "
665                                             "grant_log=%s reject_log=%s }\n",                                             "grant_log=%s reject_log=%s }\n",
666                                             index, ccs_mac_keywords[i],                                             index, "CONFIG::",
667                                             ccs_mode_4[config & 3], g, r))                                             ccs_mac_keywords[i],
668                                               ccs_mode[config & 3], g, r))
669                                  goto out;                                  goto out;
670  #else  #else
671                          if (!ccs_io_printf(head, "%u-CONFIG::%s={ mode=%s }\n",                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",
672                                             index, ccs_mac_keywords[i],                                             index, "CONFIG::",
673                                             ccs_mode_4[config & 3]))                                             ccs_mac_keywords[i],
674                                               ccs_mode[config & 3]))
675                                  goto out;                                  goto out;
676  #endif  #endif
677                  }                  }
678                  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))  
679                          goto out;                          goto out;
680                  continue;                  continue;
681   out:   out:
# Line 653  static void ccs_read_profile(struct ccs_ Line 686  static void ccs_read_profile(struct ccs_
686                  head->read_eof = true;                  head->read_eof = true;
687  }  }
688    
689  /* The list for "struct ccs_policy_manager_entry". */  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
690  LIST_HEAD(ccs_policy_manager_list);                                     const struct ccs_acl_head *b)
691    {
692            return container_of(a, struct ccs_manager, head)->manager
693                    == container_of(b, struct ccs_manager, head)->manager;
694    }
695    
696  /**  /**
697   * 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 703  LIST_HEAD(ccs_policy_manager_list);
703   */   */
704  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)
705  {  {
706          struct ccs_policy_manager_entry *entry = NULL;          struct ccs_manager e = { };
         struct ccs_policy_manager_entry *ptr;  
         struct ccs_policy_manager_entry e = { };  
707          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
708          if (ccs_is_domain_def(manager)) {          if (ccs_domain_def(manager)) {
709                  if (!ccs_is_correct_domain(manager))                  if (!ccs_correct_domain(manager))
710                          return -EINVAL;                          return -EINVAL;
711                  e.is_domain = true;                  e.is_domain = true;
712          } else {          } else {
713                  if (!ccs_is_correct_path(manager, 1, -1, -1))                  if (!ccs_correct_path(manager))
714                          return -EINVAL;                          return -EINVAL;
715          }          }
716          e.manager = ccs_get_name(manager);          e.manager = ccs_get_name(manager);
717          if (!e.manager)          if (!e.manager)
718                  return -ENOMEM;                  return error;
719          if (!is_delete)          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
720                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    &ccs_policy_list[CCS_ID_MANAGER],
721          mutex_lock(&ccs_policy_lock);                                    ccs_same_manager_entry);
         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);  
722          ccs_put_name(e.manager);          ccs_put_name(e.manager);
         kfree(entry);  
723          return error;          return error;
724  }  }
725    
726  /**  /**
727   * ccs_write_manager_policy - Write manager policy.   * ccs_write_manager - Write manager policy.
728   *   *
729   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
730   *   *
731   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
732   */   */
733  static int ccs_write_manager_policy(struct ccs_io_buffer *head)  static int ccs_write_manager(struct ccs_io_buffer *head)
734  {  {
735          char *data = head->write_buf;          char *data = head->write_buf;
736          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 742  static int ccs_write_manager_policy(stru
742  }  }
743    
744  /**  /**
745   * ccs_read_manager_policy - Read manager policy.   * ccs_read_manager - Read manager policy.
746   *   *
747   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
748   *   *
749   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
750   */   */
751  static void ccs_read_manager_policy(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
752  {  {
753          struct list_head *pos;          struct list_head *pos;
754          if (head->read_eof)          if (head->read_eof)
755                  return;                  return;
756          list_for_each_cookie(pos, head->read_var2, &ccs_policy_manager_list) {          list_for_each_cookie(pos, head->read_var2,
757                  struct ccs_policy_manager_entry *ptr;                               &ccs_policy_list[CCS_ID_MANAGER]) {
758                  ptr = list_entry(pos, struct ccs_policy_manager_entry, list);                  struct ccs_manager *ptr
759                  if (ptr->is_deleted)                          = list_entry(pos, typeof(*ptr), head.list);
760                    if (ptr->head.is_deleted)
761                          continue;                          continue;
762                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))
763                          return;                          return;
# Line 744  static void ccs_read_manager_policy(stru Line 766  static void ccs_read_manager_policy(stru
766  }  }
767    
768  /**  /**
769   * ccs_is_policy_manager - Check whether the current process is a policy manager.   * ccs_manager - Check whether the current process is a policy manager.
770   *   *
771   * Returns true if the current process is permitted to modify policy   * Returns true if the current process is permitted to modify policy
772   * via /proc/ccs/ interface.   * via /proc/ccs/ interface.
773   *   *
774   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
775   */   */
776  static bool ccs_is_policy_manager(void)  static bool ccs_manager(void)
777  {  {
778          struct ccs_policy_manager_entry *ptr;          struct ccs_manager *ptr;
779          const char *exe;          const char *exe;
780          struct task_struct *task = current;          struct task_struct *task = current;
781          const struct ccs_path_info *domainname          const struct ccs_path_info *domainname
# Line 761  static bool ccs_is_policy_manager(void) Line 783  static bool ccs_is_policy_manager(void)
783          bool found = false;          bool found = false;
784          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
785                  return true;                  return true;
786          if (task->ccs_flags & CCS_TASK_IS_POLICY_MANAGER)          if (task->ccs_flags & CCS_TASK_IS_MANAGER)
787                  return true;                  return true;
788          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))          if (!ccs_manage_by_non_root && (current_uid() || current_euid()))
789                  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;  
                 }  
         }  
790          exe = ccs_get_exe();          exe = ccs_get_exe();
791          if (!exe)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_MANAGER],
792                  return false;                                  head.list) {
793          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                  if (ptr->head.is_deleted)
794                  if (!ptr->is_deleted && !ptr->is_domain                          continue;
795                      && !strcmp(exe, ptr->manager->name)) {                  if (ptr->is_domain) {
796                          found = true;                          if (ccs_pathcmp(domainname, ptr->manager))
797                          /* Set manager flag. */                                  continue;
798                          task->ccs_flags |= CCS_TASK_IS_POLICY_MANAGER;                  } else {
799                          break;                          if (!exe || strcmp(exe, ptr->manager->name))
800                                    continue;
801                  }                  }
802                    /* Set manager flag. */
803                    task->ccs_flags |= CCS_TASK_IS_MANAGER;
804                    found = true;
805                    break;
806          }          }
807          if (!found) { /* Reduce error messages. */          if (!found) { /* Reduce error messages. */
808                  static pid_t ccs_last_pid;                  static pid_t ccs_last_pid;
# Line 809  static bool ccs_is_policy_manager(void) Line 828  static bool ccs_is_policy_manager(void)
828  static char *ccs_find_condition_part(char *data)  static char *ccs_find_condition_part(char *data)
829  {  {
830          char *cp = strstr(data, " if ");          char *cp = strstr(data, " if ");
831          if (cp) {          if (!cp)
                 while (1) {  
                         char *cp2 = strstr(cp + 3, " if ");  
                         if (!cp2)  
                                 break;  
                         cp = cp2;  
                 }  
                 *cp++ = '\0';  
         } else {  
832                  cp = strstr(data, " ; set ");                  cp = strstr(data, " ; set ");
833                  if (cp)          if (cp)
834                          *cp++ = '\0';                  *cp++ = '\0';
         }  
835          return cp;          return cp;
836  }  }
837    
838  /**  /**
839   * ccs_is_select_one - Parse select command.   * ccs_select_one - Parse select command.
840   *   *
841   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
842   * @data: String to parse.   * @data: String to parse.
# Line 835  static char *ccs_find_condition_part(cha Line 845  static char *ccs_find_condition_part(cha
845   *   *
846   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
847   */   */
848  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)
849  {  {
850          unsigned int pid;          unsigned int pid;
851          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
# Line 847  static bool ccs_is_select_one(struct ccs Line 857  static bool ccs_is_select_one(struct ccs
857          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
858              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {              (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
859                  struct task_struct *p;                  struct task_struct *p;
860                  read_lock(&tasklist_lock);                  ccs_tasklist_lock();
861  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
862                  if (global_pid)                  if (global_pid)
863                          p = find_task_by_pid_ns(pid, &init_pid_ns);                          p = ccsecurity_exports.find_task_by_pid_ns(pid,
864                                                                   &init_pid_ns);
865                  else                  else
866                          p = find_task_by_vpid(pid);                          p = ccsecurity_exports.find_task_by_vpid(pid);
867  #else  #else
868                  p = find_task_by_pid(pid);                  p = find_task_by_pid(pid);
869  #endif  #endif
870                  if (p)                  if (p)
871                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
872                  read_unlock(&tasklist_lock);                  ccs_tasklist_unlock();
873          } else if (!strncmp(data, "domain=", 7)) {          } else if (!strncmp(data, "domain=", 7)) {
874                  if (ccs_is_domain_def(data + 7))                  if (ccs_domain_def(data + 7))
875                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
876          } else          } else
877                  return false;                  return false;
# Line 869  static bool ccs_is_select_one(struct ccs Line 880  static bool ccs_is_select_one(struct ccs
880          if (!head->read_buf)          if (!head->read_buf)
881                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
882          head->read_avail = 0;          head->read_avail = 0;
883            head->read_cond = false;
884          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
885          head->read_single_domain = true;          head->read_single_domain = true;
886          head->read_eof = !domain;          head->read_eof = !domain;
887          if (domain) {          head->read_var1 = domain;
888                  struct ccs_domain_info *d;          head->read_var2 = NULL;
889                  head->read_var1 = NULL;          head->read_bit = 0;
890                  list_for_each_entry_rcu(d, &ccs_domain_list, list) {          head->read_step = 0;
891                          if (d == domain)          if (domain && domain->is_deleted)
892                                  break;                  ccs_io_printf(head, "# This is a deleted domain.\n");
                         head->read_var1 = &d->list;  
                 }  
                 head->read_var2 = NULL;  
                 head->read_bit = 0;  
                 head->read_step = 0;  
                 if (domain->is_deleted)  
                         ccs_io_printf(head, "# This is a deleted domain.\n");  
         }  
893          return true;          return true;
894  }  }
895    
896  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,
897                                      struct ccs_condition *cond,                               const bool is_delete)
898                                      const bool is_delete)  {
899  {          static const struct {
900          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))                  const char *keyword;
901                  return ccs_write_capability_policy(data, domain, cond,                  int (*write) (char *, struct ccs_domain_info *,
902                                                     is_delete);                                struct ccs_condition *, const bool);
903          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))          } ccs_callback[5] = {
904                  return ccs_write_network_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
905          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                  { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
906                  return ccs_write_signal_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
907          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
908                  return ccs_write_env_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
909          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          };
910                  return ccs_write_mount_policy(data, domain, cond, is_delete);          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
911          return ccs_write_file_policy(data, domain, cond, is_delete);                        const bool) = ccs_write_file;
912            int error;
913            u8 i;
914            struct ccs_condition *cond = NULL;
915            char *cp = ccs_find_condition_part(data);
916            if (cp) {
917                    cond = ccs_get_condition(cp);
918                    if (!cond)
919                            return -EINVAL;
920            }
921            for (i = 0; i < 5; i++) {
922                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
923                            continue;
924                    write = ccs_callback[i].write;
925                    break;
926            }
927            error = write(data, domain, cond, is_delete);
928            if (cond)
929                    ccs_put_condition(cond);
930            return error;
931  }  }
932    
933    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
934            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
935            [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
936            [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]
937            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",
938            [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]
939            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",
940            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
941    };
942            
943  /**  /**
944   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
945   *   *
946   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
947   *   *
948   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
949   */   */
950  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
951  {  {
952          char *data = head->write_buf;          char *data = head->write_buf;
953          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->write_var1;
954          bool is_delete = false;          bool is_delete = false;
955          bool is_select = false;          bool is_select = false;
956          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
957          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
958                  is_delete = true;                  is_delete = true;
959          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
960                  is_select = true;                  is_select = true;
961          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
962                  return 0;                  return 0;
963          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
964          if (!ccs_is_policy_manager())          if (!ccs_manager())
965                  return -EPERM;                  return -EPERM;
966          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
967                  domain = NULL;                  domain = NULL;
968                  if (is_delete)                  if (is_delete)
969                          ccs_delete_domain(data);                          ccs_delete_domain(data);
970                  else if (is_select)                  else if (is_select)
971                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
972                  else                  else
973                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
974                  head->write_var1 = domain;                  head->write_var1 = domain;
975                  return 0;                  return 0;
976          }          }
# Line 953  static int ccs_write_domain_policy(struc Line 983  static int ccs_write_domain_policy(struc
983                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
984                  return 0;                  return 0;
985          }          }
986          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
987                  domain->ignore_global_allow_read = !is_delete;                  const char *cp = ccs_dif[profile];
988                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
989          }                          continue;
990          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {                  domain->flags[profile] = !is_delete;
                 domain->ignore_global_allow_env = !is_delete;  
991                  return 0;                  return 0;
992          }          }
993          cp = ccs_find_condition_part(data);          return ccs_write_domain2(data, domain, is_delete);
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         error = ccs_write_domain_policy2(data, domain, cond, is_delete);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
994  }  }
995    
996  /**  /**
# Line 1010  static bool ccs_print_name_union_quoted( Line 1030  static bool ccs_print_name_union_quoted(
1030          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1031  }  }
1032    
1033    static void ccs_print_number(char *buffer, int buffer_len,
1034                                 const struct ccs_number_union *ptr)
1035    {
1036            int i;
1037            unsigned long min = ptr->values[0];
1038            const unsigned long max = ptr->values[1];
1039            u8 min_type = ptr->value_type[0];
1040            const u8 max_type = ptr->value_type[1];
1041            memset(buffer, 0, buffer_len);
1042            buffer_len -= 2;
1043            for (i = 0; i < 2; i++) {
1044                    int len;
1045                    switch (min_type) {
1046                    case CCS_VALUE_TYPE_HEXADECIMAL:
1047                            snprintf(buffer, buffer_len, "0x%lX", min);
1048                            break;
1049                    case CCS_VALUE_TYPE_OCTAL:
1050                            snprintf(buffer, buffer_len, "0%lo", min);
1051                            break;
1052                    default:
1053                            snprintf(buffer, buffer_len, "%lu", min);
1054                            break;
1055                    }
1056                    if (min == max && min_type == max_type)
1057                            break;
1058                    len = strlen(buffer);
1059                    buffer[len++] = '-';
1060                    buffer += len;
1061                    buffer_len -= len;
1062                    min_type = max_type;
1063                    min = max;
1064            }
1065    }
1066    
1067  /**  /**
1068   * ccs_print_number_union_common - Print a ccs_number_union.   * ccs_print_number_union_common - Print a ccs_number_union.
1069   *   *
# Line 1023  static bool ccs_print_number_union_commo Line 1077  static bool ccs_print_number_union_commo
1077                                            const struct ccs_number_union *ptr,                                            const struct ccs_number_union *ptr,
1078                                            const bool need_space)                                            const bool need_space)
1079  {  {
1080          unsigned long min;          char buffer[128];
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
1081          if (need_space && !ccs_io_printf(head, " "))          if (need_space && !ccs_io_printf(head, " "))
1082                  return false;                  return false;
1083          if (ptr->is_group)          if (ptr->is_group)
1084                  return ccs_io_printf(head, "@%s",                  return ccs_io_printf(head, "@%s",
1085                                       ptr->group->group_name->name);                                       ptr->group->group_name->name);
1086          min_type = ptr->min_type;          ccs_print_number(buffer, sizeof(buffer), ptr);
1087          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);  
         }  
1088  }  }
1089    
1090  /**  /**
# Line 1070  static bool ccs_print_number_union_commo Line 1095  static bool ccs_print_number_union_commo
1095   *   *
1096   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1097   */   */
1098  bool ccs_print_number_union(struct ccs_io_buffer *head,  static bool ccs_print_number_union(struct ccs_io_buffer *head,
1099                              const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1100  {  {
1101          return ccs_print_number_union_common(head, ptr, true);          return ccs_print_number_union_common(head, ptr, true);
1102  }  }
# Line 1094  static bool ccs_print_number_union_nospa Line 1119  static bool ccs_print_number_union_nospa
1119   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1120   *   *
1121   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1122   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". Maybe NULL.
1123   *   *
1124   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1125   */   */
# Line 1104  static bool ccs_print_condition(struct c Line 1129  static bool ccs_print_condition(struct c
1129          const struct ccs_condition_element *condp;          const struct ccs_condition_element *condp;
1130          const struct ccs_number_union *numbers_p;          const struct ccs_number_union *numbers_p;
1131          const struct ccs_name_union *names_p;          const struct ccs_name_union *names_p;
1132          const struct ccs_argv_entry *argv;          const struct ccs_argv *argv;
1133          const struct ccs_envp_entry *envp;          const struct ccs_envp *envp;
1134          u16 condc;          u16 condc;
1135          u16 i;          u16 i;
1136          u16 j;          u16 j;
# Line 1117  static bool ccs_print_condition(struct c Line 1142  static bool ccs_print_condition(struct c
1142          numbers_p = (const struct ccs_number_union *) (condp + condc);          numbers_p = (const struct ccs_number_union *) (condp + condc);
1143          names_p = (const struct ccs_name_union *)          names_p = (const struct ccs_name_union *)
1144                  (numbers_p + cond->numbers_count);                  (numbers_p + cond->numbers_count);
1145          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);          argv = (const struct ccs_argv *) (names_p + cond->names_count);
1146          envp = (const struct ccs_envp_entry *) (argv + cond->argc);          envp = (const struct ccs_envp *) (argv + cond->argc);
1147          memset(buffer, 0, sizeof(buffer));          memset(buffer, 0, sizeof(buffer));
1148          if (condc && !ccs_io_printf(head, "%s", " if"))          if (condc && !ccs_io_printf(head, "%s", " if"))
1149                  goto out;                  goto out;
# Line 1194  static bool ccs_print_condition(struct c Line 1219  static bool ccs_print_condition(struct c
1219                                     cond->post_state[j]))                                     cond->post_state[j]))
1220                          goto out;                          goto out;
1221          }          }
1222            if (i & (1 << 4)) {
1223                    if (!ccs_io_printf(head, " audit=%s",
1224                                       ccs_yesno(cond->post_state[4])))
1225                            goto out;
1226            }
1227   no_condition:   no_condition:
1228          if (ccs_io_printf(head, "\n"))          if (ccs_io_printf(head, "\n"))
1229                  return true;                  return true;
# Line 1202  static bool ccs_print_condition(struct c Line 1232  static bool ccs_print_condition(struct c
1232  }  }
1233    
1234  /**  /**
1235   * ccs_print_path_acl - Print a single path ACL entry.   * ccs_print_entry - Print an ACL entry.
1236   *   *
1237   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1238   * @ptr:  Pointer to "struct ccs_path_acl".   * @ptr:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1239   *   *
1240   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1241   */   */
1242  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1243                                 struct ccs_path_acl *ptr,                              const struct ccs_acl_info *acl)
                                const struct ccs_condition *cond)  
1244  {  {
1245          int pos;          int pos;
1246          u8 bit;          const u8 acl_type = acl->type;
1247          const u16 perm = ptr->perm;          u8 bit = head->read_bit;
1248          for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {          if (head->read_cond)
1249                  if (!(perm & (1 << bit)))                  goto print_cond;
1250                          continue;          if (acl->is_deleted)
1251                  if (head->read_execute_only && bit != CCS_TYPE_EXECUTE)                  return true;
1252                          continue;   next:
1253                  /* Print "read/write" instead of "read" and "write". */          pos = head->read_avail;
1254                  if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)          if (acl_type == CCS_TYPE_PATH_ACL) {
1255                      && (perm & (1 << CCS_TYPE_READ_WRITE)))                  struct ccs_path_acl *ptr
1256                          continue;                          = container_of(acl, typeof(*ptr), head);
1257                  pos = head->read_avail;                  const u16 perm = ptr->perm;
1258                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1259                      !ccs_print_name_union(head, &ptr->name) ||                          if (!(perm & (1 << bit)))
1260                      !ccs_print_condition(head, cond)) {                                  continue;
1261                          head->read_bit = bit;                          if (head->read_execute_only && bit != CCS_TYPE_EXECUTE
1262                          head->read_avail = pos;                              && bit != CCS_TYPE_TRANSIT)
1263                          return false;                                  continue;
1264                            /* Print "read/write" instead of "read" and "write". */
1265                            if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1266                                && (perm & (1 << CCS_TYPE_READ_WRITE)))
1267                                    continue;
1268                            break;
1269                  }                  }
1270          }                  if (bit == CCS_MAX_PATH_OPERATION)
1271          head->read_bit = 0;                          goto done;
1272          return true;                  if (!ccs_io_printf(head, "allow_%s", ccs_path_keyword[bit]) ||
1273  }                      !ccs_print_name_union(head, &ptr->name))
1274                            goto fail;
1275  /**          } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1276   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1277   *                  struct ccs_execute_handler *ptr
1278   * @head: Pointer to "struct ccs_io_buffer".                          = container_of(acl, typeof(*ptr), head);
1279   * @ptr:  Pointer to "struct ccs_path_number3_acl".                  if (!ccs_io_printf(head, "%s %s",
1280   * @cond: Pointer to "struct ccs_condition". May be NULL.                                     acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1281   *                                     CCS_KEYWORD_EXECUTE_HANDLER :
1282   * Returns true on success, false otherwise.                                     CCS_KEYWORD_DENIED_EXECUTE_HANDLER,
1283   */                                     ptr->handler->name))
1284  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,                          goto fail;
1285                                         struct ccs_path_number3_acl *ptr,          } if (head->read_execute_only) {
1286                                         const struct ccs_condition *cond)                  return true;
1287  {          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1288          int pos;                  struct ccs_mkdev_acl *ptr
1289          u8 bit;                          = container_of(acl, typeof(*ptr), head);
1290          const u16 perm = ptr->perm;                  const u8 perm = ptr->perm;
1291          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;                  for ( ; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
1292               bit++) {                          if (!(perm & (1 << bit)))
1293                  if (!(perm & (1 << bit)))                                  continue;
1294                          continue;                          break;
1295                  pos = head->read_avail;                  }
1296                  if (!ccs_io_printf(head, "allow_%s",                  if (bit == CCS_MAX_MKDEV_OPERATION)
1297                                     ccs_path_number32keyword(bit)) ||                          goto done;
1298                    if (!ccs_io_printf(head, "allow_%s", ccs_mkdev_keyword[bit]) ||
1299                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1300                      !ccs_print_number_union(head, &ptr->mode) ||                      !ccs_print_number_union(head, &ptr->mode) ||
1301                      !ccs_print_number_union(head, &ptr->major) ||                      !ccs_print_number_union(head, &ptr->major) ||
1302                      !ccs_print_number_union(head, &ptr->minor) ||                      !ccs_print_number_union(head, &ptr->minor))
1303                      !ccs_print_condition(head, cond)) {                          goto fail;
1304                          head->read_bit = bit;          } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1305                          head->read_avail = pos;                  struct ccs_path2_acl *ptr
1306                          return false;                          = container_of(acl, typeof(*ptr), head);
1307                    const u8 perm = ptr->perm;
1308                    for ( ; bit < CCS_MAX_PATH2_OPERATION; bit++) {
1309                            if (!(perm & (1 << bit)))
1310                                    continue;
1311                            break;
1312                  }                  }
1313          }                  if (bit == CCS_MAX_PATH2_OPERATION)
1314          head->read_bit = 0;                          goto done;
1315          return true;                  if (!ccs_io_printf(head, "allow_%s", ccs_path2_keyword[bit]) ||
 }  
   
 /**  
  * ccs_print_path2_acl - Print a path2 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path2_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
1316                      !ccs_print_name_union(head, &ptr->name1) ||                      !ccs_print_name_union(head, &ptr->name1) ||
1317                      !ccs_print_name_union(head, &ptr->name2) ||                      !ccs_print_name_union(head, &ptr->name2))
1318                      !ccs_print_condition(head, cond)) {                          goto fail;
1319                          head->read_bit = bit;          } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1320                          head->read_avail = pos;                  struct ccs_path_number_acl *ptr
1321                          return false;                          = container_of(acl, typeof(*ptr), head);
1322                    const u8 perm = ptr->perm;
1323                    for ( ; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
1324                            if (!(perm & (1 << bit)))
1325                                    continue;
1326                            break;
1327                  }                  }
1328          }                  if (bit == CCS_MAX_PATH_NUMBER_OPERATION)
1329          head->read_bit = 0;                          goto done;
         return true;  
 }  
   
 /**  
  * ccs_print_path_number_acl - Print a path_number ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_number_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  
                                       struct ccs_path_number_acl *ptr,  
                                       const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u8 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
1330                  if (!ccs_io_printf(head, "allow_%s",                  if (!ccs_io_printf(head, "allow_%s",
1331                                     ccs_path_number2keyword(bit)) ||                                     ccs_path_number_keyword[bit]) ||
1332                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1333                      !ccs_print_number_union(head, &ptr->number) ||                      !ccs_print_number_union(head, &ptr->number))
1334                      !ccs_print_condition(head, cond)) {                          goto fail;
1335                          head->read_bit = bit;          } else if (acl_type == CCS_TYPE_ENV_ACL) {
1336                          head->read_avail = pos;                  struct ccs_env_acl *ptr
1337                          return false;                          = container_of(acl, typeof(*ptr), head);
1338                    if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s",
1339                                       ptr->env->name))
1340                            goto fail;
1341            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1342                    struct ccs_capability_acl *ptr
1343                            = container_of(acl, typeof(*ptr), head);
1344                    if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1345                                       ccs_cap2keyword(ptr->operation)))
1346                            goto fail;
1347            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1348                    struct ccs_ip_network_acl *ptr
1349                            = container_of(acl, typeof(*ptr), head);
1350                    char buf[128];
1351                    const char *w[2] = { buf, "" };
1352                    const u8 perm = ptr->perm;
1353                    for ( ; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1354                            if (!(perm & (1 << bit)))
1355                                    continue;
1356                            break;
1357                  }                  }
1358          }                  if (bit == CCS_MAX_NETWORK_OPERATION)
1359          head->read_bit = 0;                          goto done;
         return true;  
 }  
   
 /**  
  * ccs_print_env_acl - Print an evironment variable name's ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_env_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_env_acl(struct ccs_io_buffer *head,  
                               struct ccs_env_acl *ptr,  
                               const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_capability_acl - Print a capability ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_capability_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  
                                      struct ccs_capability_acl *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",  
                            ccs_cap2keyword(ptr->operation)) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_ipv4_entry - Print IPv4 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv4_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl *ptr)  
 {  
         const u32 min_address = ptr->address.ipv4.min;  
         const u32 max_address = ptr->address.ipv4.max;  
         if (!ccs_io_printf(head, "%u.%u.%u.%u", HIPQUAD(min_address)))  
                 return false;  
         if (min_address != max_address  
             && !ccs_io_printf(head, "-%u.%u.%u.%u", HIPQUAD(max_address)))  
                 return false;  
         return true;  
 }  
   
 /**  
  * ccs_print_ipv6_entry - Print IPv6 address of a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_ipv6_entry(struct ccs_io_buffer *head,  
                                  struct ccs_ip_network_acl *ptr)  
 {  
         char buf[64];  
         const struct in6_addr *min_address = ptr->address.ipv6.min;  
         const struct in6_addr *max_address = ptr->address.ipv6.max;  
         ccs_print_ipv6(buf, sizeof(buf), min_address);  
         if (!ccs_io_printf(head, "%s", buf))  
                 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;  
 }  
   
 /**  
  * ccs_print_network_acl - Print a network ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_ip_network_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_network_acl(struct ccs_io_buffer *head,  
                                   struct ccs_ip_network_acl *ptr,  
                                   const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",  
                                    ccs_net2keyword(bit)))  
                         goto out;  
1360                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1361                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1362                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          w[0] = "@";
1363                                             group_name->name))                          w[1] = ptr->address.group->group_name->name;
                                 goto out;  
1364                          break;                          break;
1365                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1366                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1367                                  goto out;                                         ptr->address.ipv4.max);
1368                          break;                          break;
1369                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1370                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1371                                  goto out;                                         ptr->address.ipv6.max);
1372                          break;                          break;
1373                  }                  }
1374                  if (!ccs_print_number_union(head, &ptr->port) ||                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1375                      !ccs_print_condition(head, cond))                                     ccs_net_keyword[bit], w[0], w[1]) ||
1376                          goto out;                      !ccs_print_number_union(head, &ptr->port))
1377                            goto fail;
1378            } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1379                    struct ccs_signal_acl *ptr
1380                            = container_of(acl, typeof(*ptr), head);
1381                    if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1382                                       ptr->sig, ptr->domainname->name))
1383                            goto fail;
1384            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1385                    struct ccs_mount_acl *ptr
1386                            = container_of(acl, typeof(*ptr), head);
1387                    if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
1388                        !ccs_print_name_union(head, &ptr->dev_name) ||
1389                        !ccs_print_name_union(head, &ptr->dir_name) ||
1390                        !ccs_print_name_union(head, &ptr->fs_type) ||
1391                        !ccs_print_number_union(head, &ptr->flags))
1392                            goto fail;
1393          }          }
         head->read_bit = 0;  
         return true;  
  out:  
1394          head->read_bit = bit;          head->read_bit = bit;
1395          head->read_avail = pos;          head->read_cond = true;
1396          return false;   print_cond:
1397  }          pos = head->read_avail;
1398            if (!ccs_print_condition(head, acl->cond)) {
 /**  
  * ccs_print_signal_acl - Print a signal ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct signale_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  
                                  struct ccs_signal_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",  
                            ptr->sig, ptr->domainname->name) ||  
             !ccs_print_condition(head, cond)) {  
1399                  head->read_avail = pos;                  head->read_avail = pos;
1400                  return false;                  return false;
1401          }          }
1402            head->read_cond = false;
1403            switch (acl_type) {
1404            case CCS_TYPE_PATH_ACL:
1405            case CCS_TYPE_MKDEV_ACL:
1406            case CCS_TYPE_PATH2_ACL:
1407            case CCS_TYPE_PATH_NUMBER_ACL:
1408            case CCS_TYPE_IP_NETWORK_ACL:
1409                    bit++;
1410                    goto next;
1411            }
1412     done:
1413            head->read_bit = 0;
1414          return true;          return true;
1415     fail:
1416            head->read_bit = bit;
1417            head->read_avail = pos;
1418            return false;
1419  }  }
1420    
1421  /**  /**
1422   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_read_domain2 - Read domain policy.
  *  
  * @head:    Pointer to "struct ccs_io_buffer".  
  * @keyword: Name of the keyword.  
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1423   *   *
1424   * Returns true on success, false otherwise.   * @head:   Pointer to "struct ccs_io_buffer".
1425   */   * @domain: Pointer to "struct ccs_domain_info".
 static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  
                                              const char *keyword,  
                                              struct ccs_execute_handler_record *  
                                              ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
1426   *   *
1427   * @head: Pointer to "struct ccs_io_buffer".   * Caller holds ccs_read_lock().
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1428   *   *
1429   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1430   */   */
1431  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1432                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain)
                                 const struct ccs_condition *cond)  
1433  {  {
1434          const int pos = head->read_avail;          struct list_head *pos;
1435          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||          /* Print ACL entries in the domain. */
1436              !ccs_print_name_union(head, &ptr->dev_name) ||          list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {
1437              !ccs_print_name_union(head, &ptr->dir_name) ||                  struct ccs_acl_info *ptr
1438              !ccs_print_name_union(head, &ptr->fs_type) ||                          = list_entry(pos, struct ccs_acl_info, list);
1439              !ccs_print_number_union(head, &ptr->flags) ||                  if (!ccs_print_entry(head, ptr))
1440              !ccs_print_condition(head, cond)) {                          return false;
                 head->read_avail = pos;  
                 return false;  
1441          }          }
1442          return true;          return true;
1443  }  }
1444    
1445  /**  /**
1446   * ccs_print_entry - Print an ACL entry.   * ccs_read_domain - Read domain policy.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const struct ccs_condition *cond = ptr->cond;  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl, head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, struct ccs_path_number3_acl, head);  
                 return ccs_print_path_number3_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, struct ccs_path2_acl,  
                                        head);  
                 return ccs_print_path2_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, struct ccs_path_number_acl,  
                                        head);  
                 return ccs_print_path_number_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, struct ccs_env_acl, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, struct ccs_capability_acl,  
                                        head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, struct ccs_ip_network_acl,  
                                        head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, struct ccs_signal_acl, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, struct ccs_mount_acl, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
  * ccs_read_domain_policy - Read domain policy.  
1447   *   *
1448   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1449   *   *
1450   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1451   */   */
1452  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1453  {  {
1454          struct list_head *dpos;          struct list_head *pos;
         struct list_head *apos;  
1455          if (head->read_eof)          if (head->read_eof)
1456                  return;                  return;
1457          if (head->read_step == 0)          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1458                  head->read_step = 1;                  struct ccs_domain_info *domain =
1459          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(pos, struct ccs_domain_info, list);
1460                  struct ccs_domain_info *domain;                  switch (head->read_step) {
1461                  const char *quota_exceeded = "";                          u8 i;
1462                  const char *transition_failed = "";                          const char *w[CCS_MAX_DOMAIN_INFO_FLAGS];
1463                  const char *ignore_global_allow_read = "";                  case 0:
1464                  const char *ignore_global_allow_env = "";                          if (domain->is_deleted && !head->read_single_domain)
1465                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1466                  if (head->read_step != 1)                          /* Print domainname and flags. */
1467                          goto acl_loop;                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1468                  if (domain->is_deleted && !head->read_single_domain)                                  w[i] = domain->flags[i] ? ccs_dif[i] : "";
1469                          continue;                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE
1470                  /* Print domainname and flags. */                                             "%u\n%s%s%s%s%s\n",
1471                  if (domain->quota_warned)                                             domain->domainname->name,
1472                          quota_exceeded = "quota_exceeded\n";                                             domain->profile,
1473                  if (domain->domain_transition_failed)                                             w[CCS_DIF_QUOTA_WARNED],
1474                          transition_failed = "transition_failed\n";                                             w[CCS_DIF_IGNORE_GLOBAL],
1475                  if (domain->ignore_global_allow_read)                                             w[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ],
1476                          ignore_global_allow_read                                             w[CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV],
1477                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                             w[CCS_DIF_TRANSITION_FAILED]))
1478                  if (domain->ignore_global_allow_env)                                  return;
1479                          ignore_global_allow_env                          head->read_step++;
1480                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          /* fall through */
1481                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                  case 1:
1482                                     "%s%s%s%s\n", domain->domainname->name,                          if (!ccs_read_domain2(head, domain))
                                    domain->profile, quota_exceeded,  
                                    transition_failed,  
                                    ignore_global_allow_read,  
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
1483                                  return;                                  return;
1484                            head->read_step++;
1485                            /* fall through */
1486                    case 2:
1487                            if (!ccs_io_printf(head, "\n"))
1488                                    return;
1489                            head->read_step = 0;
1490                            if (head->read_single_domain)
1491                                    goto out;
1492                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1493          }          }
1494     out:
1495          head->read_eof = true;          head->read_eof = true;
1496  }  }
1497    
# Line 1800  static void ccs_read_pid(struct ccs_io_b Line 1592  static void ccs_read_pid(struct ccs_io_b
1592          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
1593          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1594          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1595          if (!buf)          if (!buf) {
1596                    head->read_eof = true;
1597                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1598            }
1599          if (head->read_avail || head->read_eof)          if (head->read_avail || head->read_eof)
1600                  return;                  return;
1601          head->read_eof = true;          head->read_eof = true;
# Line 1810  static void ccs_read_pid(struct ccs_io_b Line 1604  static void ccs_read_pid(struct ccs_io_b
1604          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
1605                  global_pid = true;                  global_pid = true;
1606          pid = (unsigned int) simple_strtoul(buf, NULL, 10);          pid = (unsigned int) simple_strtoul(buf, NULL, 10);
1607          read_lock(&tasklist_lock);          ccs_tasklist_lock();
1608  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1609          if (global_pid)          if (global_pid)
1610                  p = find_task_by_pid_ns(pid, &init_pid_ns);                  p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
1611          else          else
1612                  p = find_task_by_vpid(pid);                  p = ccsecurity_exports.find_task_by_vpid(pid);
1613  #else  #else
1614          p = find_task_by_pid(pid);          p = find_task_by_pid(pid);
1615  #endif  #endif
# Line 1823  static void ccs_read_pid(struct ccs_io_b Line 1617  static void ccs_read_pid(struct ccs_io_b
1617                  domain = ccs_task_domain(p);                  domain = ccs_task_domain(p);
1618                  ccs_flags = p->ccs_flags;                  ccs_flags = p->ccs_flags;
1619          }          }
1620          read_unlock(&tasklist_lock);          ccs_tasklist_unlock();
1621          if (!domain)          if (!domain)
1622                  return;                  return;
1623          if (!task_info)          if (!task_info)
# Line 1833  static void ccs_read_pid(struct ccs_io_b Line 1627  static void ccs_read_pid(struct ccs_io_b
1627                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1628                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1629                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1630                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1631                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1632                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1633                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
# Line 1841  static void ccs_read_pid(struct ccs_io_b Line 1635  static void ccs_read_pid(struct ccs_io_b
1635                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1636  }  }
1637    
1638    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1639            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1640            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1641            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1642            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1643            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1644    };
1645    
1646    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1647            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1648            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1649            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1650    };
1651    
1652  /**  /**
1653   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1654   *   *
1655   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1656   *   *
1657   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1658   */   */
1659  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1660  {  {
1661          char *data = head->write_buf;          char *data = head->write_buf;
1662          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1663          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1664                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1665          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1666                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1667          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[4] = {
1668                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1669                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1670          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1671                  return ccs_write_domain_initializer_policy(data, true,                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1672                                                             is_delete);          };
1673          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))          for (i = 0; i < 4; i++) {
1674                  return ccs_write_aggregator_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1675          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))                          return ccs_callback[i].write(data, is_delete);
1676                  return ccs_write_globally_readable_policy(data, is_delete);          }
1677          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) {
1678                  return ccs_write_globally_usable_env_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1679          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))                          return ccs_write_transition_control(data, is_delete,
1680                  return ccs_write_pattern_policy(data, is_delete);                                                              i);
1681          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))          }
1682                  return ccs_write_path_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++) {
1683          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1684                  return ccs_write_number_group_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1685          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))          }
1686                  return ccs_write_no_rewrite_policy(data, is_delete);          return ccs_write_domain2(data, &ccs_global_domain, is_delete);
         if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))  
                 return ccs_write_address_group_policy(data, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))  
                 return ccs_write_reserved_port_policy(data, is_delete);  
         return -EINVAL;  
1687  }  }
1688    
1689  /**  /**
1690   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1691   *   *
1692   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1693     * @idx:  Index number.
1694     *
1695     * Returns true on success, false otherwise.
1696   *   *
1697   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1698   */   */
1699  static void ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1700  {  {
1701          if (head->read_eof)          struct list_head *gpos;
1702                  return;          struct list_head *mpos;
1703          switch (head->read_step) {          const char *w[3] = { "", "", "" };
1704          case 0:          w[0] = ccs_group_name[idx];
1705                  head->read_var2 = NULL;          list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1706                  head->read_step = 1;                  struct ccs_group *group =
1707          case 1:                          list_entry(gpos, struct ccs_group, head.list);
1708                  if (!ccs_read_domain_keeper_policy(head))                  w[1] = group->group_name->name;
1709                          break;                  list_for_each_cookie(mpos, head->read_var2,
1710                  head->read_var2 = NULL;                                       &group->member_list) {
1711                  head->read_step = 2;                          char buffer[128];
1712          case 2:                          struct ccs_acl_head *ptr =
1713                  if (!ccs_read_globally_readable_policy(head))                                  list_entry(mpos, struct ccs_acl_head, list);
1714                          break;                          if (ptr->is_deleted)
1715                  head->read_var2 = NULL;                                  continue;
1716                  head->read_step = 3;                          w[2] = buffer;
1717          case 3:                          if (idx == CCS_PATH_GROUP) {
1718                  if (!ccs_read_globally_usable_env_policy(head))                                  w[2] = container_of(ptr, struct ccs_path_group,
1719                          break;                                                      head)->member_name->name;
1720                  head->read_var2 = NULL;                          } else if (idx == CCS_NUMBER_GROUP) {
1721                  head->read_step = 4;                                  ccs_print_number(buffer, sizeof(buffer),
1722          case 4:                                                   &container_of
1723                  if (!ccs_read_domain_initializer_policy(head))                                                   (ptr, struct ccs_number_group,
1724                          break;                                                    head)->number);
1725                  head->read_var2 = NULL;                          } else if (idx == CCS_ADDRESS_GROUP) {
1726                  head->read_step = 6;                                  struct ccs_address_group *member =
1727          case 6:                                          container_of(ptr, typeof(*member),
1728                  if (!ccs_read_aggregator_policy(head))                                                       head);
1729                          break;                                  if (member->is_ipv6)
1730                  head->read_var2 = NULL;                                          ccs_print_ipv6(buffer, sizeof(buffer),
1731                  head->read_step = 7;                                                         member->min.ipv6,
1732          case 7:                                                         member->max.ipv6);
1733                  if (!ccs_read_file_pattern(head))                                  else
1734                          break;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1735                  head->read_var2 = NULL;                                                         member->min.ipv4,
1736                  head->read_step = 8;                                                         member->max.ipv4);
1737          case 8:                          }
1738                  if (!ccs_read_no_rewrite_policy(head))                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1739                          break;                                             w[2]))
1740                  head->read_var2 = NULL;                                  return false;
1741                  head->read_step = 9;                  }
         case 9:  
                 if (!ccs_read_path_group_policy(head))  
                         break;  
                 head->read_var1 = NULL;  
                 head->read_var2 = NULL;  
                 head->read_step = 10;  
         case 10:  
                 if (!ccs_read_number_group_policy(head))  
                         break;  
                 head->read_var1 = NULL;  
                 head->read_var2 = NULL;  
                 head->read_step = 11;  
         case 11:  
                 if (!ccs_read_address_group_policy(head))  
                         break;  
                 head->read_var2 = NULL;  
                 head->read_step = 12;  
         case 12:  
                 if (!ccs_read_reserved_port_policy(head))  
                         break;  
                 head->read_eof = true;  
1742          }          }
1743            return true;
1744  }  }
1745    
1746  /**  /**
1747   * ccs_get_argv0 - Get argv[0].   * ccs_read_policy - Read "struct ccs_..._entry" list.
1748   *   *
1749   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1750     * @idx:  Index number.
1751   *   *
1752   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1753     *
1754     * Caller holds ccs_read_lock().
1755   */   */
1756  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1757  {  {
1758          struct linux_binprm *bprm = ee->bprm;          struct list_head *pos;
1759          char *arg_ptr = ee->tmp;          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1760          int arg_len = 0;                  const char *w[4] = { "", "", "", "" };
1761          unsigned long pos = bprm->p;                  char buffer[16];
1762          int offset = pos % PAGE_SIZE;                  struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1763          bool done = false;                                                          list);
1764          if (!bprm->argc)                  if (acl->is_deleted)
1765                  goto out;                          continue;
1766          while (1) {                  switch (idx) {
1767                  if (!ccs_dump_page(bprm, pos, &ee->dump))                  case CCS_ID_TRANSITION_CONTROL:
1768                          goto out;                          {
1769                  pos += PAGE_SIZE - offset;                                  struct ccs_transition_control *ptr =
1770                  /* Read. */                                          container_of(acl, typeof(*ptr), head);
1771                  while (offset < PAGE_SIZE) {                                  w[0] = ccs_transition_type[ptr->type];
1772                          const char *kaddr = ee->dump.data;                                  w[1] = ptr->program ? ptr->program->name
1773                          const unsigned char c = kaddr[offset++];                                          : "any";
1774                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                  w[2] = " from ";
1775                                  if (c == '\\') {                                  w[3] = ptr->domainname ? ptr->domainname->name
1776                                          arg_ptr[arg_len++] = '\\';                                          : "any";
1777                                          arg_ptr[arg_len++] = '\\';                          }
1778                                  } else if (c > ' ' && c < 127) {                          break;
1779                                          arg_ptr[arg_len++] = c;                  case CCS_ID_AGGREGATOR:
1780                                  } else {                          {
1781                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_aggregator *ptr =
1782                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(acl, typeof(*ptr), head);
1783                                          arg_ptr[arg_len++]                                  w[0] = CCS_KEYWORD_AGGREGATOR;
1784                                                  = ((c >> 3) & 7) + '0';                                  w[1] = ptr->original_name->name;
1785                                          arg_ptr[arg_len++] = (c & 7) + '0';                                  w[2] = " ";
1786                                  }                                  w[3] = ptr->aggregated_name->name;
1787                          } else {                          }
1788                                  arg_ptr[arg_len] = '\0';                          break;
1789                                  done = true;                  case CCS_ID_PATTERN:
1790                                  break;                          {
1791                                    struct ccs_pattern *ptr =
1792                                            container_of(acl, typeof(*ptr), head);
1793                                    w[0] = CCS_KEYWORD_FILE_PATTERN;
1794                                    w[1] = ptr->pattern->name;
1795                            }
1796                            break;
1797                    case CCS_ID_NO_REWRITE:
1798                            {
1799                                    struct ccs_no_rewrite *ptr =
1800                                            container_of(acl, typeof(*ptr), head);
1801                                    w[0] = CCS_KEYWORD_DENY_REWRITE;
1802                                    w[1] = ptr->pattern->name;
1803                          }                          }
                 }  
                 offset = 0;  
                 if (done)  
1804                          break;                          break;
1805                    case CCS_ID_RESERVEDPORT:
1806                            {
1807                                    struct ccs_reserved *ptr =
1808                                            container_of(acl, typeof(*ptr), head);
1809                                    const u16 min_port = ptr->min_port;
1810                                    const u16 max_port = ptr->max_port;
1811                                    w[0] = CCS_KEYWORD_DENY_AUTOBIND;
1812                                    snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
1813                                             min_port, min_port != max_port ?
1814                                             '-' : '\0', max_port);
1815                                    buffer[sizeof(buffer) - 1] = '\0';
1816                                    w[1] = buffer;
1817                            }
1818                            break;
1819                    default:
1820                            continue;
1821                    }
1822                    if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
1823                            return false;
1824          }          }
1825          return true;          return true;
  out:  
         return false;  
1826  }  }
1827    
1828  /**  static void ccs_read_global_domain(struct ccs_io_buffer *head)
  * ccs_get_execute_condition - Get condition part for execute requests.  
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
  *  
  * Returns pointer to "struct ccs_condition" on success, NULL otherwise.  
  */  
 static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve_entry  
                                                        *ee)  
1829  {  {
1830          struct ccs_condition *cond;          if (!head->read_eof)
1831          char *buf;                  head->read_eof = ccs_read_domain2(head, &ccs_global_domain);
         int len = 256;  
         char *realpath = NULL;  
         char *argv0 = NULL;  
         const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);  
         if (profile->learning->learning_exec_realpath) {  
                 struct file *file = ee->bprm->file;  
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)  
                 struct path path = { file->f_vfsmnt, file->f_dentry };  
                 realpath = ccs_realpath_from_path(&path);  
 #else  
                 realpath = ccs_realpath_from_path(&file->f_path);  
 #endif  
                 if (realpath)  
                         len += strlen(realpath) + 17;  
         }  
         if (profile->learning->learning_exec_argv0) {  
                 if (ccs_get_argv0(ee)) {  
                         argv0 = ee->tmp;  
                         len += strlen(argv0) + 16;  
                 }  
         }  
         buf = kmalloc(len, GFP_KERNEL);  
         if (!buf) {  
                 kfree(realpath);  
                 return NULL;  
         }  
         snprintf(buf, len - 1, "if");  
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (realpath) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " exec.realpath=\"%s\"",  
                          realpath);  
                 kfree(realpath);  
         }  
         if (argv0) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " exec.argv[0]=\"%s\"",  
                          argv0);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
1832  }  }
1833    
1834  /**  /**
1835   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1836   *   *
1837   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1838   *   *
1839   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1840   */   */
1841  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1842  {  {
1843          struct ccs_condition *cond;          if (head->read_eof)
1844          char *buf;                  return;
1845          int len = 256;          while (head->read_step < CCS_MAX_POLICY &&
1846          const char *symlink = NULL;                 ccs_read_policy(head, head->read_step))
1847          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->read_step++;
1848          if (profile->learning->learning_symlink_target) {          if (head->read_step < CCS_MAX_POLICY)
1849                  symlink = r->obj->symlink_target->name;                  return;
1850                  len += strlen(symlink) + 18;          while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1851          }                 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
1852          buf = kmalloc(len, GFP_KERNEL);                  head->read_step++;
1853          if (!buf)          if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1854                  return NULL;                  return;
1855          snprintf(buf, len - 1, "if");          head->read = ccs_read_global_domain;
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (symlink) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",  
                          symlink);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
1856  }  }
1857    
1858  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2130  static LIST_HEAD(ccs_query_list); Line 1877  static LIST_HEAD(ccs_query_list);
1877  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1878  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1879    
1880    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
1881    {
1882            va_list args;
1883            const int pos = strlen(buffer);
1884            va_start(args, fmt);
1885            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
1886            va_end(args);
1887    }
1888    
1889    static void ccs_truncate(char *str)
1890    {
1891            while (* (unsigned char *) str > (unsigned char) ' ')
1892                    str++;
1893            *str = '\0';
1894    }
1895    
1896  /**  /**
1897   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1898   *   *
1899   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1900   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1901   *   *
1902   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1903   * violated the policy in enforcing mode, 1 if the supervisor decided to   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
1904   * retry the access request which violated the policy in enforcing mode,   * decided to retry the access request which violated the policy in enforcing
1905   * 0 if it is not in enforcing mode, -EPERM otherwise.   * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
1906   */   */
1907  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)  int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
1908  {  {
# Line 2151  int ccs_supervisor(struct ccs_request_in Line 1914  int ccs_supervisor(struct ccs_request_in
1914          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query_entry *ccs_query_entry = NULL;
1915          bool quota_exceeded = false;          bool quota_exceeded = false;
1916          char *header;          char *header;
1917          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1918                  r->domain = ccs_current_domain();          va_start(args, fmt);
1919          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1920            va_end(args);
1921            if (r->mode == CCS_CONFIG_LEARNING) {
1922                  char *buffer;                  char *buffer;
1923                  struct ccs_condition *cond;                  char *realpath = NULL;
1924          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1925                    char *symlink = NULL;
1926                    char *handler = NULL;
1927                    const struct ccs_preference *pref;
1928                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1929                          return 0;                          return 0;
1930                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1931                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, GFP_KERNEL);  
                 if (!buffer)  
1932                          return 0;                          return 0;
1933                  va_start(args, fmt);                  pref = ccs_profile(r->profile)->learning;
1934                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1935                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1936                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1937                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1938                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1939                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1940                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1941                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1942                          char str[] = "if task.type=execute_handler";                                  }
1943                          cond = ccs_get_condition(str);                          }
1944                  } else                          if (pref->learning_exec_realpath) {
1945                          cond = NULL;                                  realpath = strstr(header,
1946                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1947                  ccs_put_condition(cond);                                  if (realpath) {
1948                  kfree(buffer);                                          realpath += 8;
1949                  /* fall through */                                          ccs_truncate(realpath);
1950          case CCS_CONFIG_PERMISSIVE:                                  }
1951                            }
1952                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1953                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1954                               pref->learning_symlink_target) {
1955                            symlink = strstr(header, " symlink.target=\"");
1956                            if (symlink)
1957                                    ccs_truncate(symlink + 1);
1958                    }
1959                    handler = strstr(header, "type=execute_handler");
1960                    if (handler)
1961                            ccs_truncate(handler);
1962                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1963                    if (buffer) {
1964                            va_start(args, fmt);
1965                            vsnprintf(buffer, len - 1, fmt, args);
1966                            va_end(args);
1967                            if (handler || realpath || argv0 || symlink) {
1968                                    ccs_addprintf(buffer, len, " if");
1969                                    if (handler)
1970                                            ccs_addprintf(buffer, len, " task.%s",
1971                                                          handler);
1972                                    if (realpath)
1973                                            ccs_addprintf(buffer, len, " exec.%s",
1974                                                          realpath);
1975                                    if (argv0)
1976                                            ccs_addprintf(buffer, len,
1977                                                          " exec.argv[0]=%s",
1978                                                          argv0);
1979                                    if (symlink)
1980                                            ccs_addprintf(buffer, len, "%s",
1981                                                          symlink);
1982                            }
1983                            ccs_normalize_line(buffer);
1984                            ccs_write_domain2(buffer, domain, false);
1985                            kfree(buffer);
1986                    }
1987                    kfree(header);
1988                  return 0;                  return 0;
1989          }          }
1990            if (r->mode != CCS_CONFIG_ENFORCING)
1991                    return 0;
1992          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
1993                  int i;                  int i;
1994                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1995                          return -EPERM;                          return -EPERM;
1996                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
1997                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
1998                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
1999                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2000                  }                  }
2001                  return -EPERM;                  return -EPERM;
2002          }          }
2003          va_start(args, fmt);          header = ccs_init_log(&len, r);
         len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;  
         va_end(args);  
         header = ccs_init_audit_log(&len, r);  
2004          if (!header)          if (!header)
2005                  goto out;                  goto out;
2006          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), GFP_KERNEL);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
2007          if (!ccs_query_entry)          if (!ccs_query_entry)
2008                  goto out;                  goto out;
2009          len = ccs_round2(len);          len = ccs_round2(len);
2010          ccs_query_entry->query = kzalloc(len, GFP_KERNEL);          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);
2011          if (!ccs_query_entry->query)          if (!ccs_query_entry->query)
2012                  goto out;                  goto out;
2013          INIT_LIST_HEAD(&ccs_query_entry->list);          INIT_LIST_HEAD(&ccs_query_entry->list);
# Line 2221  int ccs_supervisor(struct ccs_request_in Line 2022  int ccs_supervisor(struct ccs_request_in
2022          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2023          if (quota_exceeded)          if (quota_exceeded)
2024                  goto out;                  goto out;
2025          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",
2026                         ccs_query_entry->serial, r->retry, header);                   ccs_query_entry->serial, r->retry, header);
2027          kfree(header);          kfree(header);
2028          header = NULL;          header = NULL;
2029          va_start(args, fmt);          ccs_addprintf(ccs_query_entry->query, len, fmt, args);
         vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);  
2030          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;
         va_end(args);  
2031          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2032          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2033          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2248  int ccs_supervisor(struct ccs_request_in Line 2047  int ccs_supervisor(struct ccs_request_in
2047          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2048          switch (ccs_query_entry->answer) {          switch (ccs_query_entry->answer) {
2049          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2050                  error = 1;                  error = CCS_RETRY_REQUEST;
2051                  r->retry++;                  r->retry++;
2052                  break;                  break;
2053          case 1:          case 1:
# Line 2288  static int ccs_poll_query(struct file *f Line 2087  static int ccs_poll_query(struct file *f
2087          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2088                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2089                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2090                          struct ccs_query_entry *ptr                          struct ccs_query_entry *ptr =
2091                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, struct ccs_query_entry, list);
2092                          if (ptr->answer)                          if (ptr->answer)
2093                                  continue;                                  continue;
2094                          found = true;                          found = true;
# Line 2339  static void ccs_read_query(struct ccs_io Line 2138  static void ccs_read_query(struct ccs_io
2138                  head->read_step = 0;                  head->read_step = 0;
2139                  return;                  return;
2140          }          }
2141          buf = kzalloc(len, GFP_KERNEL);          buf = kzalloc(len, CCS_GFP_FLAGS);
2142          if (!buf)          if (!buf)
2143                  return;                  return;
2144          pos = 0;          pos = 0;
# Line 2415  static void ccs_read_version(struct ccs_ Line 2214  static void ccs_read_version(struct ccs_
2214  {  {
2215          if (head->read_eof)          if (head->read_eof)
2216                  return;                  return;
2217          ccs_io_printf(head, "1.7.0");          ccs_io_printf(head, "1.7.2");
2218          head->read_eof = true;          head->read_eof = true;
2219  }  }
2220    
# Line 2446  static void ccs_read_self_domain(struct Line 2245  static void ccs_read_self_domain(struct
2245   */   */
2246  int ccs_open_control(const u8 type, struct file *file)  int ccs_open_control(const u8 type, struct file *file)
2247  {  {
2248          struct ccs_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);          struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
2249          if (!head)          if (!head)
2250                  return -ENOMEM;                  return -ENOMEM;
2251          mutex_init(&head->io_sem);          mutex_init(&head->io_sem);
2252          head->type = type;          head->type = type;
2253          switch (type) {          switch (type) {
2254          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2255                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2256                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2257                  break;                  break;
2258          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2259                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2260                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2261                  break;                  break;
2262  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2263          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;  
2264          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2265                  head->poll = ccs_poll_reject_log;                  head->poll = ccs_poll_log;
2266                  head->read = ccs_read_reject_log;                  head->read = ccs_read_log;
2267                  break;                  break;
2268  #endif  #endif
2269          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 2303  int ccs_open_control(const u8 type, stru
2303                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2304                  break;                  break;
2305          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2306                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2307                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2308                  break;                  break;
2309          }          }
2310          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2522  int ccs_open_control(const u8 type, stru Line 2318  int ccs_open_control(const u8 type, stru
2318                  /* Don't allocate read_buf for poll() access. */                  /* Don't allocate read_buf for poll() access. */
2319                  if (!head->readbuf_size)                  if (!head->readbuf_size)
2320                          head->readbuf_size = 4096;                          head->readbuf_size = 4096;
2321                  head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);                  head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
2322                  if (!head->read_buf) {                  if (!head->read_buf) {
2323                          kfree(head);                          kfree(head);
2324                          return -ENOMEM;                          return -ENOMEM;
# Line 2536  int ccs_open_control(const u8 type, stru Line 2332  int ccs_open_control(const u8 type, stru
2332                  head->write = NULL;                  head->write = NULL;
2333          } else if (head->write) {          } else if (head->write) {
2334                  head->writebuf_size = 4096;                  head->writebuf_size = 4096;
2335                  head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);                  head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
2336                  if (!head->write_buf) {                  if (!head->write_buf) {
2337                          kfree(head->read_buf);                          kfree(head->read_buf);
2338                          kfree(head);                          kfree(head);
# Line 2545  int ccs_open_control(const u8 type, stru Line 2341  int ccs_open_control(const u8 type, stru
2341          }          }
2342          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2343              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2344                  head->reader_idx = ccs_read_lock();                  head->reader_idx = ccs_lock();
2345          file->private_data = head;          file->private_data = head;
2346          /*          /*
2347           * 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 2394  int ccs_read_control(struct file *file,
2394          int len = 0;          int len = 0;
2395          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
2396          char *cp;          char *cp;
2397            int idx;
2398          if (!head->read)          if (!head->read)
2399                  return -ENOSYS;                  return -ENOSYS;
2400          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))          if (!access_ok(VERIFY_WRITE, buffer, buffer_len))
2401                  return -EFAULT;                  return -EFAULT;
2402          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2403                  return -EINTR;                  return -EINTR;
2404            idx = ccs_read_lock();
2405          while (1) {          while (1) {
2406                  /* Call the policy handler. */                  /* Call the policy handler. */
2407                  head->read(head);                  head->read(head);
# Line 2612  int ccs_read_control(struct file *file, Line 2410  int ccs_read_control(struct file *file,
2410                  if (len || head->poll || head->read_eof)                  if (len || head->poll || head->read_eof)
2411                          break;                          break;
2412                  len = head->readbuf_size * 2;                  len = head->readbuf_size * 2;
2413                  cp = kzalloc(len, GFP_KERNEL);                  cp = kzalloc(len, CCS_GFP_FLAGS);
2414                  if (!cp) {                  if (!cp) {
2415                          len = -ENOMEM;                          len = -ENOMEM;
2416                          goto out;                          goto out;
# Line 2634  int ccs_read_control(struct file *file, Line 2432  int ccs_read_control(struct file *file,
2432          head->read_avail -= len;          head->read_avail -= len;
2433          memmove(cp, cp + len, head->read_avail);          memmove(cp, cp + len, head->read_avail);
2434   out:   out:
2435            ccs_read_unlock(idx);
2436          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2437          return len;          return len;
2438  }  }
# Line 2654  int ccs_write_control(struct file *file, Line 2453  int ccs_write_control(struct file *file,
2453          int error = buffer_len;          int error = buffer_len;
2454          int avail_len = buffer_len;          int avail_len = buffer_len;
2455          char *cp0 = head->write_buf;          char *cp0 = head->write_buf;
2456            int idx;
2457          if (!head->write)          if (!head->write)
2458                  return -ENOSYS;                  return -ENOSYS;
2459          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2460                  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;  
2461          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2462                  return -EINTR;                  return -EINTR;
2463            idx = ccs_read_lock();
2464            /* Don't allow updating policies by non manager programs. */
2465            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2466                !ccs_manager()) {
2467                    ccs_read_unlock(idx);
2468                    mutex_unlock(&head->io_sem);
2469                    return -EPERM;
2470            }
2471          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2472          while (avail_len > 0) {          while (avail_len > 0) {
2473                  char c;                  char c;
2474                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->write_avail >= head->writebuf_size - 1) {
2475                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2476                          char *cp = kzalloc(len, GFP_KERNEL);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2477                          if (!cp) {                          if (!cp) {
2478                                  error = -ENOMEM;                                  error = -ENOMEM;
2479                                  break;                                  break;
# Line 2695  int ccs_write_control(struct file *file, Line 2498  int ccs_write_control(struct file *file,
2498                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2499                  head->write(head);                  head->write(head);
2500          }          }
2501            ccs_read_unlock(idx);
2502          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2503          return error;          return error;
2504  }  }
# Line 2718  int ccs_close_control(struct file *file) Line 2522  int ccs_close_control(struct file *file)
2522                  atomic_dec(&ccs_query_observers);                  atomic_dec(&ccs_query_observers);
2523          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2524              type != CCS_GRANTLOG && type != CCS_REJECTLOG)              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2525                  ccs_read_unlock(head->reader_idx);                  ccs_unlock(head->reader_idx);
2526          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2527          kfree(head->read_buf);          kfree(head->read_buf);
2528          head->read_buf = NULL;          head->read_buf = NULL;
# Line 2731  int ccs_close_control(struct file *file) Line 2535  int ccs_close_control(struct file *file)
2535                  ccs_run_gc();                  ccs_run_gc();
2536          return 0;          return 0;
2537  }  }
2538    
2539    void __init ccs_policy_io_init(void)
2540    {
2541            ccsecurity_ops.check_profile = ccs_check_profile;
2542    }

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

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