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

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 3625 by kumaneko, Wed May 5 01:00:40 2010 UTC branches/ccs-patch/security/ccsecurity/policy_io.c revision 3761 by kumaneko, Sun Jun 13 14:58:41 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.2   2010/04/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 40  static unsigned int ccs_profile_version; Line 40  static unsigned int ccs_profile_version;
40  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
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 246  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 305  static void ccs_check_profile(void) Line 307  static void ccs_check_profile(void)
307          if (ccs_profile_version != 20090903)          if (ccs_profile_version != 20090903)
308                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
309                        ccs_profile_version);                        ccs_profile_version);
310          printk(KERN_INFO "CCSecurity: 1.7.2   2010/04/01\n");          printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");
311          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
312  }  }
313    
# Line 325  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;  
         if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)  
                 return 0;  
         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 *old_comment = profile->comment;          u8 config;
435                  profile->comment = ccs_get_name(cp);          if (!strcmp(name, "CONFIG")) {
                 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 478  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 487  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 507  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 519  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 571  static void ccs_read_profile(struct ccs_ Line 636  static void ccs_read_profile(struct ccs_
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 596  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 660  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 673  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 *ptr;          struct ccs_manager e = { };
         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 (mutex_lock_interruptible(&ccs_policy_lock))          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
720                  goto out;                                    &ccs_policy_list[CCS_ID_MANAGER],
721          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                                    ccs_same_manager_entry);
                 if (ptr->manager != e.manager)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error) {  
                 struct ccs_policy_manager_entry *entry =  
                         ccs_commit_ok(&e, sizeof(e));  
                 if (entry) {  
                         list_add_tail_rcu(&entry->list,  
                                           &ccs_policy_manager_list);  
                         error = 0;  
                 }  
         }  
         mutex_unlock(&ccs_policy_lock);  
  out:  
722          ccs_put_name(e.manager);          ccs_put_name(e.manager);
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 730  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 753  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 770  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 818  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 844  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 870  static bool ccs_is_select_one(struct ccs Line 871  static bool ccs_is_select_one(struct ccs
871                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
872                  ccs_tasklist_unlock();                  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 899  static bool ccs_is_select_one(struct ccs Line 900  static bool ccs_is_select_one(struct ccs
900          return true;          return true;
901  }  }
902    
903  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,
904                                      struct ccs_condition *cond,                               const bool is_delete)
905                                      const bool is_delete)  {
906  {          static const struct {
907          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_CAPABILITY))                  const char *keyword;
908                  return ccs_write_capability_policy(data, domain, cond,                  int (*write) (char *, struct ccs_domain_info *,
909                                                     is_delete);                                struct ccs_condition *, const bool);
910          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_NETWORK))          } ccs_callback[5] = {
911                  return ccs_write_network_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_NETWORK, ccs_write_network },
912          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_SIGNAL))                  { CCS_KEYWORD_ALLOW_ENV, ccs_write_env },
913                  return ccs_write_signal_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_CAPABILITY, ccs_write_capability },
914          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                  { CCS_KEYWORD_ALLOW_SIGNAL, ccs_write_signal },
915                  return ccs_write_env_policy(data, domain, cond, is_delete);                  { CCS_KEYWORD_ALLOW_MOUNT, ccs_write_mount }
916          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_MOUNT))          };
917                  return ccs_write_mount_policy(data, domain, cond, is_delete);          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
918          return ccs_write_file_policy(data, domain, cond, is_delete);                        const bool) = ccs_write_file;
919            int error;
920            u8 i;
921            struct ccs_condition *cond = NULL;
922            char *cp = ccs_find_condition_part(data);
923            if (cp) {
924                    cond = ccs_get_condition(cp);
925                    if (!cond)
926                            return -EINVAL;
927            }
928            for (i = 0; i < 5; i++) {
929                    if (!ccs_str_starts(&data, ccs_callback[i].keyword))
930                            continue;
931                    write = ccs_callback[i].write;
932                    break;
933            }
934            error = write(data, domain, cond, is_delete);
935            if (cond)
936                    ccs_put_condition(cond);
937            return error;
938  }  }
939    
940    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
941            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
942            [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
943            [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]
944            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",
945            [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]
946            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",
947            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
948    };
949            
950  /**  /**
951   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
952   *   *
953   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
954   *   *
955   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
956   */   */
957  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
958  {  {
959          char *data = head->write_buf;          char *data = head->write_buf;
960          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->write_var1;
961          bool is_delete = false;          bool is_delete = false;
962          bool is_select = false;          bool is_select = false;
963          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
964          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
965                  is_delete = true;                  is_delete = true;
966          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
967                  is_select = true;                  is_select = true;
968          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
969                  return 0;                  return 0;
970          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
971          if (!ccs_is_policy_manager())          if (!ccs_manager())
972                  return -EPERM;                  return -EPERM;
973          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
974                  domain = NULL;                  domain = NULL;
975                  if (is_delete)                  if (is_delete)
976                          ccs_delete_domain(data);                          ccs_delete_domain(data);
977                  else if (is_select)                  else if (is_select)
978                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
979                  else                  else
980                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
981                  head->write_var1 = domain;                  head->write_var1 = domain;
982                  return 0;                  return 0;
983          }          }
# Line 963  static int ccs_write_domain_policy(struc Line 990  static int ccs_write_domain_policy(struc
990                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
991                  return 0;                  return 0;
992          }          }
993          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
994                  domain->ignore_global_allow_read = !is_delete;                  const char *cp = ccs_dif[profile];
995                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
996          }                          continue;
997          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {                  domain->flags[profile] = !is_delete;
                 domain->ignore_global_allow_env = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {  
                 domain->quota_warned = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {  
                 domain->domain_transition_failed = !is_delete;  
998                  return 0;                  return 0;
999          }          }
1000          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;  
1001  }  }
1002    
1003  /**  /**
# Line 1028  static bool ccs_print_name_union_quoted( Line 1037  static bool ccs_print_name_union_quoted(
1037          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);          return ccs_io_printf(head, "\"%s\"", ptr->filename->name);
1038  }  }
1039    
1040    static void ccs_print_number(char *buffer, int buffer_len,
1041                                 const struct ccs_number_union *ptr)
1042    {
1043            int i;
1044            unsigned long min = ptr->values[0];
1045            const unsigned long max = ptr->values[1];
1046            u8 min_type = ptr->value_type[0];
1047            const u8 max_type = ptr->value_type[1];
1048            memset(buffer, 0, buffer_len);
1049            buffer_len -= 2;
1050            for (i = 0; i < 2; i++) {
1051                    int len;
1052                    switch (min_type) {
1053                    case CCS_VALUE_TYPE_HEXADECIMAL:
1054                            snprintf(buffer, buffer_len, "0x%lX", min);
1055                            break;
1056                    case CCS_VALUE_TYPE_OCTAL:
1057                            snprintf(buffer, buffer_len, "0%lo", min);
1058                            break;
1059                    default:
1060                            snprintf(buffer, buffer_len, "%lu", min);
1061                            break;
1062                    }
1063                    if (min == max && min_type == max_type)
1064                            break;
1065                    len = strlen(buffer);
1066                    buffer[len++] = '-';
1067                    buffer += len;
1068                    buffer_len -= len;
1069                    min_type = max_type;
1070                    min = max;
1071            }
1072    }
1073    
1074  /**  /**
1075   * ccs_print_number_union_common - Print a ccs_number_union.   * ccs_print_number_union_common - Print a ccs_number_union.
1076   *   *
# Line 1041  static bool ccs_print_number_union_commo Line 1084  static bool ccs_print_number_union_commo
1084                                            const struct ccs_number_union *ptr,                                            const struct ccs_number_union *ptr,
1085                                            const bool need_space)                                            const bool need_space)
1086  {  {
1087          unsigned long min;          char buffer[128];
         unsigned long max;  
         u8 min_type;  
         u8 max_type;  
1088          if (need_space && !ccs_io_printf(head, " "))          if (need_space && !ccs_io_printf(head, " "))
1089                  return false;                  return false;
1090          if (ptr->is_group)          if (ptr->is_group)
1091                  return ccs_io_printf(head, "@%s",                  return ccs_io_printf(head, "@%s",
1092                                       ptr->group->group_name->name);                                       ptr->group->group_name->name);
1093          min_type = ptr->min_type;          ccs_print_number(buffer, sizeof(buffer), ptr);
1094          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);  
         }  
1095  }  }
1096    
1097  /**  /**
# Line 1088  static bool ccs_print_number_union_commo Line 1102  static bool ccs_print_number_union_commo
1102   *   *
1103   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1104   */   */
1105  bool ccs_print_number_union(struct ccs_io_buffer *head,  static bool ccs_print_number_union(struct ccs_io_buffer *head,
1106                              const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1107  {  {
1108          return ccs_print_number_union_common(head, ptr, true);          return ccs_print_number_union_common(head, ptr, true);
1109  }  }
# Line 1112  static bool ccs_print_number_union_nospa Line 1126  static bool ccs_print_number_union_nospa
1126   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1127   *   *
1128   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1129   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition". Maybe NULL.
1130   *   *
1131   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1132   */   */
# Line 1122  static bool ccs_print_condition(struct c Line 1136  static bool ccs_print_condition(struct c
1136          const struct ccs_condition_element *condp;          const struct ccs_condition_element *condp;
1137          const struct ccs_number_union *numbers_p;          const struct ccs_number_union *numbers_p;
1138          const struct ccs_name_union *names_p;          const struct ccs_name_union *names_p;
1139          const struct ccs_argv_entry *argv;          const struct ccs_argv *argv;
1140          const struct ccs_envp_entry *envp;          const struct ccs_envp *envp;
1141          u16 condc;          u16 condc;
1142          u16 i;          u16 i;
1143          u16 j;          u16 j;
# Line 1135  static bool ccs_print_condition(struct c Line 1149  static bool ccs_print_condition(struct c
1149          numbers_p = (const struct ccs_number_union *) (condp + condc);          numbers_p = (const struct ccs_number_union *) (condp + condc);
1150          names_p = (const struct ccs_name_union *)          names_p = (const struct ccs_name_union *)
1151                  (numbers_p + cond->numbers_count);                  (numbers_p + cond->numbers_count);
1152          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);          argv = (const struct ccs_argv *) (names_p + cond->names_count);
1153          envp = (const struct ccs_envp_entry *) (argv + cond->argc);          envp = (const struct ccs_envp *) (argv + cond->argc);
1154          memset(buffer, 0, sizeof(buffer));          memset(buffer, 0, sizeof(buffer));
1155          if (condc && !ccs_io_printf(head, "%s", " if"))          if (condc && !ccs_io_printf(head, "%s", " if"))
1156                  goto out;                  goto out;
# Line 1212  static bool ccs_print_condition(struct c Line 1226  static bool ccs_print_condition(struct c
1226                                     cond->post_state[j]))                                     cond->post_state[j]))
1227                          goto out;                          goto out;
1228          }          }
1229            if (i & (1 << 4)) {
1230                    if (!ccs_io_printf(head, " audit=%s",
1231                                       ccs_yesno(cond->post_state[4])))
1232                            goto out;
1233            }
1234   no_condition:   no_condition:
1235          if (ccs_io_printf(head, "\n"))          if (ccs_io_printf(head, "\n"))
1236                  return true;                  return true;
# Line 1224  static bool ccs_print_condition(struct c Line 1243  static bool ccs_print_condition(struct c
1243   *   *
1244   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1245   * @ptr:  Pointer to "struct ccs_path_acl".   * @ptr:  Pointer to "struct ccs_path_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1246   *   *
1247   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1248   */   */
1249  static bool ccs_print_path_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_acl(struct ccs_io_buffer *head,
1250                                 struct ccs_path_acl *ptr,                                 const struct ccs_path_acl *ptr)
                                const struct ccs_condition *cond)  
1251  {  {
1252          int pos;          int pos;
1253          u8 bit;          u8 bit;
# Line 1246  static bool ccs_print_path_acl(struct cc Line 1263  static bool ccs_print_path_acl(struct cc
1263                      && (perm & (1 << CCS_TYPE_READ_WRITE)))                      && (perm & (1 << CCS_TYPE_READ_WRITE)))
1264                          continue;                          continue;
1265                  pos = head->read_avail;                  pos = head->read_avail;
1266                  if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||                  if (!ccs_io_printf(head, "allow_%s", ccs_path_keyword[bit]) ||
1267                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1268                      !ccs_print_condition(head, cond)) {                      !ccs_print_condition(head, ptr->head.cond)) {
1269                          head->read_bit = bit;                          head->read_bit = bit;
1270                          head->read_avail = pos;                          head->read_avail = pos;
1271                          return false;                          return false;
# Line 1259  static bool ccs_print_path_acl(struct cc Line 1276  static bool ccs_print_path_acl(struct cc
1276  }  }
1277    
1278  /**  /**
1279   * ccs_print_path_number3_acl - Print a path_number3 ACL entry.   * ccs_print_mkdev_acl - Print a mkdev ACL entry.
1280   *   *
1281   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1282   * @ptr:  Pointer to "struct ccs_path_number3_acl".   * @ptr:  Pointer to "struct ccs_mkdev_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1283   *   *
1284   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1285   */   */
1286  static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  static bool ccs_print_mkdev_acl(struct ccs_io_buffer *head,
1287                                         struct ccs_path_number3_acl *ptr,                                  const struct ccs_mkdev_acl *ptr)
                                        const struct ccs_condition *cond)  
1288  {  {
1289          int pos;          int pos;
1290          u8 bit;          u8 bit;
1291          const u16 perm = ptr->perm;          const u16 perm = ptr->perm;
1292          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;          for (bit = head->read_bit; bit < CCS_MAX_MKDEV_OPERATION;
1293               bit++) {               bit++) {
1294                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1295                          continue;                          continue;
1296                  pos = head->read_avail;                  pos = head->read_avail;
1297                  if (!ccs_io_printf(head, "allow_%s",                  if (!ccs_io_printf(head, "allow_%s", ccs_mkdev_keyword[bit]) ||
                                    ccs_path_number32keyword(bit)) ||  
1298                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1299                      !ccs_print_number_union(head, &ptr->mode) ||                      !ccs_print_number_union(head, &ptr->mode) ||
1300                      !ccs_print_number_union(head, &ptr->major) ||                      !ccs_print_number_union(head, &ptr->major) ||
1301                      !ccs_print_number_union(head, &ptr->minor) ||                      !ccs_print_number_union(head, &ptr->minor) ||
1302                      !ccs_print_condition(head, cond)) {                      !ccs_print_condition(head, ptr->head.cond)) {
1303                          head->read_bit = bit;                          head->read_bit = bit;
1304                          head->read_avail = pos;                          head->read_avail = pos;
1305                          return false;                          return false;
# Line 1300  static bool ccs_print_path_number3_acl(s Line 1314  static bool ccs_print_path_number3_acl(s
1314   *   *
1315   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1316   * @ptr:  Pointer to "struct ccs_path2_acl".   * @ptr:  Pointer to "struct ccs_path2_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1317   *   *
1318   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1319   */   */
1320  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,
1321                                  struct ccs_path2_acl *ptr,                                  const struct ccs_path2_acl *ptr)
                                 const struct ccs_condition *cond)  
1322  {  {
1323          int pos;          int pos;
1324          u8 bit;          u8 bit;
# Line 1315  static bool ccs_print_path2_acl(struct c Line 1327  static bool ccs_print_path2_acl(struct c
1327                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1328                          continue;                          continue;
1329                  pos = head->read_avail;                  pos = head->read_avail;
1330                  if (!ccs_io_printf(head, "allow_%s",                  if (!ccs_io_printf(head, "allow_%s", ccs_path2_keyword[bit]) ||
                                    ccs_path22keyword(bit)) ||  
1331                      !ccs_print_name_union(head, &ptr->name1) ||                      !ccs_print_name_union(head, &ptr->name1) ||
1332                      !ccs_print_name_union(head, &ptr->name2) ||                      !ccs_print_name_union(head, &ptr->name2) ||
1333                      !ccs_print_condition(head, cond)) {                      !ccs_print_condition(head, ptr->head.cond)) {
1334                          head->read_bit = bit;                          head->read_bit = bit;
1335                          head->read_avail = pos;                          head->read_avail = pos;
1336                          return false;                          return false;
# Line 1334  static bool ccs_print_path2_acl(struct c Line 1345  static bool ccs_print_path2_acl(struct c
1345   *   *
1346   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1347   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @ptr:  Pointer to "struct ccs_path_number_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1348   *   *
1349   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1350   */   */
1351  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,
1352                                        struct ccs_path_number_acl *ptr,                                        const struct ccs_path_number_acl *ptr)
                                       const struct ccs_condition *cond)  
1353  {  {
1354          int pos;          int pos;
1355          u8 bit;          u8 bit;
# Line 1351  static bool ccs_print_path_number_acl(st Line 1360  static bool ccs_print_path_number_acl(st
1360                          continue;                          continue;
1361                  pos = head->read_avail;                  pos = head->read_avail;
1362                  if (!ccs_io_printf(head, "allow_%s",                  if (!ccs_io_printf(head, "allow_%s",
1363                                     ccs_path_number2keyword(bit)) ||                                     ccs_path_number_keyword[bit]) ||
1364                      !ccs_print_name_union(head, &ptr->name) ||                      !ccs_print_name_union(head, &ptr->name) ||
1365                      !ccs_print_number_union(head, &ptr->number) ||                      !ccs_print_number_union(head, &ptr->number) ||
1366                      !ccs_print_condition(head, cond)) {                      !ccs_print_condition(head, ptr->head.cond)) {
1367                          head->read_bit = bit;                          head->read_bit = bit;
1368                          head->read_avail = pos;                          head->read_avail = pos;
1369                          return false;                          return false;
# Line 1369  static bool ccs_print_path_number_acl(st Line 1378  static bool ccs_print_path_number_acl(st
1378   *   *
1379   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1380   * @ptr:  Pointer to "struct ccs_env_acl".   * @ptr:  Pointer to "struct ccs_env_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1381   *   *
1382   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1383   */   */
1384  static bool ccs_print_env_acl(struct ccs_io_buffer *head,  static bool ccs_print_env_acl(struct ccs_io_buffer *head,
1385                                struct ccs_env_acl *ptr,                                const struct ccs_env_acl *ptr)
                               const struct ccs_condition *cond)  
1386  {  {
1387          const int pos = head->read_avail;          const int pos = head->read_avail;
1388          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||
1389              !ccs_print_condition(head, cond)) {              !ccs_print_condition(head, ptr->head.cond)) {
1390                  head->read_avail = pos;                  head->read_avail = pos;
1391                  return false;                  return false;
1392          }          }
# Line 1391  static bool ccs_print_env_acl(struct ccs Line 1398  static bool ccs_print_env_acl(struct ccs
1398   *   *
1399   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1400   * @ptr:  Pointer to "struct ccs_capability_acl".   * @ptr:  Pointer to "struct ccs_capability_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1401   *   *
1402   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1403   */   */
1404  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  static bool ccs_print_capability_acl(struct ccs_io_buffer *head,
1405                                       struct ccs_capability_acl *ptr,                                       const struct ccs_capability_acl *ptr)
                                      const struct ccs_condition *cond)  
1406  {  {
1407          const int pos = head->read_avail;          const int pos = head->read_avail;
1408          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",
1409                             ccs_cap2keyword(ptr->operation)) ||                             ccs_cap2keyword(ptr->operation)) ||
1410              !ccs_print_condition(head, cond)) {              !ccs_print_condition(head, ptr->head.cond)) {
1411                  head->read_avail = pos;                  head->read_avail = pos;
1412                  return false;                  return false;
1413          }          }
# Line 1410  static bool ccs_print_capability_acl(str Line 1415  static bool ccs_print_capability_acl(str
1415  }  }
1416    
1417  /**  /**
  * 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;  
 }  
   
 /**  
1418   * ccs_print_network_acl - Print a network ACL entry.   * ccs_print_network_acl - Print a network ACL entry.
1419   *   *
1420   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1421   * @ptr:  Pointer to "struct ccs_ip_network_acl".   * @ptr:  Pointer to "struct ccs_ip_network_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1422   *   *
1423   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1424   */   */
1425  static bool ccs_print_network_acl(struct ccs_io_buffer *head,  static bool ccs_print_network_acl(struct ccs_io_buffer *head,
1426                                    struct ccs_ip_network_acl *ptr,                                    const struct ccs_ip_network_acl *ptr)
                                   const struct ccs_condition *cond)  
1427  {  {
1428          int pos;          int pos;
1429          u8 bit;          u8 bit;
1430          const u16 perm = ptr->perm;          const u8 perm = ptr->perm;
1431            char buf[128];
1432          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
1433                    const char *w[2] = { buf, "" };
1434                  if (!(perm & (1 << bit)))                  if (!(perm & (1 << bit)))
1435                          continue;                          continue;
1436                  pos = head->read_avail;                  pos = head->read_avail;
                 if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",  
                                    ccs_net2keyword(bit)))  
                         goto out;  
1437                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1438                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1439                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          w[0] = "@";
1440                                             group_name->name))                          w[1] = ptr->address.group->group_name->name;
                                 goto out;  
1441                          break;                          break;
1442                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1443                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1444                                  goto out;                                         ptr->address.ipv4.max);
1445                          break;                          break;
1446                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1447                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1448                                  goto out;                                         ptr->address.ipv6.max);
1449                          break;                          break;
1450                  }                  }
1451                  if (!ccs_print_number_union(head, &ptr->port) ||                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",
1452                      !ccs_print_condition(head, cond))                                     ccs_net_keyword[bit], w[0], w[1]) ||
1453                        !ccs_print_number_union(head, &ptr->port) ||
1454                        !ccs_print_condition(head, ptr->head.cond))
1455                          goto out;                          goto out;
1456          }          }
1457          head->read_bit = 0;          head->read_bit = 0;
# Line 1510  static bool ccs_print_network_acl(struct Line 1467  static bool ccs_print_network_acl(struct
1467   *   *
1468   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1469   * @ptr:  Pointer to "struct signale_acl".   * @ptr:  Pointer to "struct signale_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1470   *   *
1471   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1472   */   */
1473  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  static bool ccs_print_signal_acl(struct ccs_io_buffer *head,
1474                                   struct ccs_signal_acl *ptr,                                   const struct ccs_signal_acl *ptr)
                                  const struct ccs_condition *cond)  
1475  {  {
1476          const int pos = head->read_avail;          const int pos = head->read_avail;
1477          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",
1478                             ptr->sig, ptr->domainname->name) ||                             ptr->sig, ptr->domainname->name) ||
1479              !ccs_print_condition(head, cond)) {              !ccs_print_condition(head, ptr->head.cond)) {
1480                  head->read_avail = pos;                  head->read_avail = pos;
1481                  return false;                  return false;
1482          }          }
# Line 1529  static bool ccs_print_signal_acl(struct Line 1484  static bool ccs_print_signal_acl(struct
1484  }  }
1485    
1486  /**  /**
1487   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_print_execute_handler - Print an execute handler ACL entry.
1488   *   *
1489   * @head:    Pointer to "struct ccs_io_buffer".   * @head:    Pointer to "struct ccs_io_buffer".
1490   * @keyword: Name of the keyword.   * @keyword: Name of the keyword.
1491   * @ptr:     Pointer to "struct ccs_execute_handler_record".   * @ptr:     Pointer to "struct ccs_execute_handler".
1492   *   *
1493   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1494   */   */
1495  static bool ccs_print_execute_handler_record(struct ccs_io_buffer *head,  static bool ccs_print_execute_handler(struct ccs_io_buffer *head,
1496                                               const char *keyword,                                        const char *keyword,
1497                                               struct ccs_execute_handler_record                                        const struct ccs_execute_handler *ptr)
                                              *ptr)  
1498  {  {
1499          return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);          const int pos = head->read_avail;
1500            if (!ccs_io_printf(head, "%s %s", keyword, ptr->handler->name) ||
1501                !ccs_print_condition(head, ptr->head.cond)) {
1502                    head->read_avail = pos;
1503                    return false;
1504            }
1505            return true;
1506  }  }
1507    
1508  /**  /**
# Line 1550  static bool ccs_print_execute_handler_re Line 1510  static bool ccs_print_execute_handler_re
1510   *   *
1511   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1512   * @ptr:  Pointer to "struct ccs_mount_acl".   * @ptr:  Pointer to "struct ccs_mount_acl".
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1513   *   *
1514   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1515   */   */
1516  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,
1517                                  struct ccs_mount_acl *ptr,                                  const struct ccs_mount_acl *ptr)
                                 const struct ccs_condition *cond)  
1518  {  {
1519          const int pos = head->read_avail;          const int pos = head->read_avail;
1520          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||
# Line 1564  static bool ccs_print_mount_acl(struct c Line 1522  static bool ccs_print_mount_acl(struct c
1522              !ccs_print_name_union(head, &ptr->dir_name) ||              !ccs_print_name_union(head, &ptr->dir_name) ||
1523              !ccs_print_name_union(head, &ptr->fs_type) ||              !ccs_print_name_union(head, &ptr->fs_type) ||
1524              !ccs_print_number_union(head, &ptr->flags) ||              !ccs_print_number_union(head, &ptr->flags) ||
1525              !ccs_print_condition(head, cond)) {              !ccs_print_condition(head, ptr->head.cond)) {
1526                  head->read_avail = pos;                  head->read_avail = pos;
1527                  return false;                  return false;
1528          }          }
# Line 1580  static bool ccs_print_mount_acl(struct c Line 1538  static bool ccs_print_mount_acl(struct c
1538   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1539   */   */
1540  static bool ccs_print_entry(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1541                              struct ccs_acl_info *ptr)                              const struct ccs_acl_info *ptr)
1542  {  {
         const struct ccs_condition *cond = ptr->cond;  
1543          const u8 acl_type = ptr->type;          const u8 acl_type = ptr->type;
1544          if (ptr->is_deleted)          if (ptr->is_deleted)
1545                  return true;                  return true;
1546          if (acl_type == CCS_TYPE_PATH_ACL) {          if (acl_type == CCS_TYPE_PATH_ACL) {
1547                  struct ccs_path_acl *acl                  struct ccs_path_acl *acl
1548                          = container_of(ptr, struct ccs_path_acl, head);                          = container_of(ptr, typeof(*acl), head);
1549                  return ccs_print_path_acl(head, acl, cond);                  return ccs_print_path_acl(head, acl);
1550          }          }
1551          if (acl_type == CCS_TYPE_EXECUTE_HANDLER) {          if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1552                  struct ccs_execute_handler_record *acl              acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1553                          = container_of(ptr, struct ccs_execute_handler_record,                  struct ccs_execute_handler *acl
1554                                         head);                          = container_of(ptr, typeof(*acl), head);
1555                  const char *keyword = CCS_KEYWORD_EXECUTE_HANDLER;                  const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1556                  return ccs_print_execute_handler_record(head, keyword, acl);                          CCS_KEYWORD_EXECUTE_HANDLER :
1557          }                          CCS_KEYWORD_DENIED_EXECUTE_HANDLER;
1558          if (acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {                  return ccs_print_execute_handler(head, keyword, acl);
                 struct ccs_execute_handler_record *acl  
                         = container_of(ptr, struct ccs_execute_handler_record,  
                                        head);  
                 const char *keyword = CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler_record(head, keyword, acl);  
1559          }          }
1560          if (head->read_execute_only)          if (head->read_execute_only)
1561                  return true;                  return true;
1562          if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {          if (acl_type == CCS_TYPE_MKDEV_ACL) {
1563                  struct ccs_path_number3_acl *acl                  struct ccs_mkdev_acl *acl
1564                          = container_of(ptr, struct ccs_path_number3_acl, head);                          = container_of(ptr, typeof(*acl), head);
1565                  return ccs_print_path_number3_acl(head, acl, cond);                  return ccs_print_mkdev_acl(head, acl);
1566          }          }
1567          if (acl_type == CCS_TYPE_PATH2_ACL) {          if (acl_type == CCS_TYPE_PATH2_ACL) {
1568                  struct ccs_path2_acl *acl                  struct ccs_path2_acl *acl
1569                          = container_of(ptr, struct ccs_path2_acl, head);                          = container_of(ptr, typeof(*acl), head);
1570                  return ccs_print_path2_acl(head, acl, cond);                  return ccs_print_path2_acl(head, acl);
1571          }          }
1572          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {          if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1573                  struct ccs_path_number_acl *acl                  struct ccs_path_number_acl *acl
1574                          = container_of(ptr, struct ccs_path_number_acl, head);                          = container_of(ptr, typeof(*acl), head);
1575                  return ccs_print_path_number_acl(head, acl, cond);                  return ccs_print_path_number_acl(head, acl);
1576          }          }
1577          if (acl_type == CCS_TYPE_ENV_ACL) {          if (acl_type == CCS_TYPE_ENV_ACL) {
1578                  struct ccs_env_acl *acl                  struct ccs_env_acl *acl
1579                          = container_of(ptr, struct ccs_env_acl, head);                          = container_of(ptr, typeof(*acl), head);
1580                  return ccs_print_env_acl(head, acl, cond);                  return ccs_print_env_acl(head, acl);
1581          }          }
1582          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {          if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1583                  struct ccs_capability_acl *acl                  struct ccs_capability_acl *acl
1584                          = container_of(ptr, struct ccs_capability_acl, head);                          = container_of(ptr, typeof(*acl), head);
1585                  return ccs_print_capability_acl(head, acl, cond);                  return ccs_print_capability_acl(head, acl);
1586          }          }
1587          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {          if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1588                  struct ccs_ip_network_acl *acl                  struct ccs_ip_network_acl *acl
1589                          = container_of(ptr, struct ccs_ip_network_acl, head);                          = container_of(ptr, typeof(*acl), head);
1590                  return ccs_print_network_acl(head, acl, cond);                  return ccs_print_network_acl(head, acl);
1591          }          }
1592          if (acl_type == CCS_TYPE_SIGNAL_ACL) {          if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1593                  struct ccs_signal_acl *acl                  struct ccs_signal_acl *acl
1594                          = container_of(ptr, struct ccs_signal_acl, head);                          = container_of(ptr, typeof(*acl), head);
1595                  return ccs_print_signal_acl(head, acl, cond);                  return ccs_print_signal_acl(head, acl);
1596          }          }
1597          if (acl_type == CCS_TYPE_MOUNT_ACL) {          if (acl_type == CCS_TYPE_MOUNT_ACL) {
1598                  struct ccs_mount_acl *acl                  struct ccs_mount_acl *acl
1599                          = container_of(ptr, struct ccs_mount_acl, head);                          = container_of(ptr, typeof(*acl), head);
1600                  return ccs_print_mount_acl(head, acl, cond);                  return ccs_print_mount_acl(head, acl);
1601          }          }
1602          BUG(); /* This must not happen. */          BUG(); /* This must not happen. */
1603          return false;          return false;
1604  }  }
1605    
1606  /**  /**
1607   * ccs_read_domain_policy - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1608     *
1609     * @head:   Pointer to "struct ccs_io_buffer".
1610     * @domain: Pointer to "struct ccs_domain_info".
1611     *
1612     * Caller holds ccs_read_lock().
1613     *
1614     * Returns true on success, false otherwise.
1615     */
1616    static bool ccs_read_domain2(struct ccs_io_buffer *head,
1617                                 struct ccs_domain_info *domain)
1618    {
1619            struct list_head *pos;
1620            /* Print ACL entries in the domain. */
1621            list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {
1622                    struct ccs_acl_info *ptr
1623                            = list_entry(pos, struct ccs_acl_info, list);
1624                    if (!ccs_print_entry(head, ptr))
1625                            return false;
1626            }
1627            return true;
1628    }
1629    
1630    /**
1631     * ccs_read_domain - Read domain policy.
1632   *   *
1633   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1634   *   *
1635   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1636   */   */
1637  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1638  {  {
1639          struct list_head *dpos;          struct list_head *pos;
         struct list_head *apos;  
1640          if (head->read_eof)          if (head->read_eof)
1641                  return;                  return;
1642          if (head->read_step == 0)          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {
1643                  head->read_step = 1;                  struct ccs_domain_info *domain =
1644          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(pos, struct ccs_domain_info, list);
1645                  struct ccs_domain_info *domain;                  switch (head->read_step) {
1646                  const char *quota_exceeded = "";                          u8 i;
1647                  const char *transition_failed = "";                          const char *w[CCS_MAX_DOMAIN_INFO_FLAGS];
1648                  const char *ignore_global_allow_read = "";                  case 0:
1649                  const char *ignore_global_allow_env = "";                          if (domain->is_deleted && !head->read_single_domain)
1650                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1651                  if (head->read_step != 1)                          /* Print domainname and flags. */
1652                          goto acl_loop;                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1653                  if (domain->is_deleted && !head->read_single_domain)                                  w[i] = domain->flags[i] ? ccs_dif[i] : "";
1654                          continue;                          if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE
1655                  /* Print domainname and flags. */                                             "%u\n%s%s%s%s%s\n",
1656                  if (domain->quota_warned)                                             domain->domainname->name,
1657                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                                             domain->profile,
1658                  if (domain->domain_transition_failed)                                             w[CCS_DIF_QUOTA_WARNED],
1659                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";                                             w[CCS_DIF_IGNORE_GLOBAL],
1660                  if (domain->ignore_global_allow_read)                                             w[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ],
1661                          ignore_global_allow_read                                             w[CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV],
1662                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                                             w[CCS_DIF_TRANSITION_FAILED]))
                 if (domain->ignore_global_allow_env)  
                         ignore_global_allow_env  
                                 = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";  
                 if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"  
                                    "%s%s%s%s\n", domain->domainname->name,  
                                    domain->profile, quota_exceeded,  
                                    transition_failed,  
                                    ignore_global_allow_read,  
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
1663                                  return;                                  return;
1664                            head->read_step++;
1665                            /* fall through */
1666                    case 1:
1667                            if (!ccs_read_domain2(head, domain))
1668                                    return;
1669                            head->read_step++;
1670                            /* fall through */
1671                    case 2:
1672                            if (!ccs_io_printf(head, "\n"))
1673                                    return;
1674                            head->read_step = 0;
1675                            if (head->read_single_domain)
1676                                    goto out;
1677                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1678          }          }
1679     out:
1680          head->read_eof = true;          head->read_eof = true;
1681  }  }
1682    
# Line 1850  static void ccs_read_pid(struct ccs_io_b Line 1812  static void ccs_read_pid(struct ccs_io_b
1812                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1813                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1814                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1815                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1816                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1817                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1818                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
# Line 1858  static void ccs_read_pid(struct ccs_io_b Line 1820  static void ccs_read_pid(struct ccs_io_b
1820                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1821  }  }
1822    
1823    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1824            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1825            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1826            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1827            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1828            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1829    };
1830    
1831    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1832            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1833            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1834            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1835    };
1836    
1837  /**  /**
1838   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1839   *   *
1840   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1841   *   *
1842   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1843   */   */
1844  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1845  {  {
1846          char *data = head->write_buf;          char *data = head->write_buf;
1847          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1848          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1849                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1850          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1851                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1852          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[4] = {
1853                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1854                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1855          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1856                  return ccs_write_domain_initializer_policy(data, true,                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1857                                                             is_delete);          };
1858          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))          for (i = 0; i < 4; i++) {
1859                  return ccs_write_aggregator_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1860          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))                          return ccs_callback[i].write(data, is_delete);
1861                  return ccs_write_globally_readable_policy(data, is_delete);          }
1862          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) {
1863                  return ccs_write_globally_usable_env_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1864          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))                          return ccs_write_transition_control(data, is_delete,
1865                  return ccs_write_pattern_policy(data, is_delete);                                                              i);
1866          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))          }
1867                  return ccs_write_path_group_policy(data, is_delete);          for (i = 0; i < CCS_MAX_GROUP; i++) {
1868          if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1869                  return ccs_write_number_group_policy(data, is_delete);                          return ccs_write_group(data, is_delete, i);
1870          if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))          }
1871                  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;  
1872  }  }
1873    
1874  /**  /**
1875   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1876   *   *
1877   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1878     * @idx:  Index number.
1879     *
1880     * Returns true on success, false otherwise.
1881   *   *
1882   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1883   */   */
1884  static void ccs_read_exception_policy(struct ccs_io_buffer *head)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1885  {  {
1886          if (head->read_eof)          struct list_head *gpos;
1887                  return;          struct list_head *mpos;
1888          switch (head->read_step) {          const char *w[3] = { "", "", "" };
1889          case 0:          w[0] = ccs_group_name[idx];
1890                  head->read_var2 = NULL;          list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {
1891                  head->read_step = 1;                  struct ccs_group *group =
1892          case 1:                          list_entry(gpos, struct ccs_group, head.list);
1893                  if (!ccs_read_domain_keeper_policy(head))                  w[1] = group->group_name->name;
1894                          break;                  list_for_each_cookie(mpos, head->read_var2,
1895                  head->read_var2 = NULL;                                       &group->member_list) {
1896                  head->read_step = 2;                          char buffer[128];
1897          case 2:                          struct ccs_acl_head *ptr =
1898                  if (!ccs_read_globally_readable_policy(head))                                  list_entry(mpos, struct ccs_acl_head, list);
1899                          break;                          if (ptr->is_deleted)
1900                  head->read_var2 = NULL;                                  continue;
1901                  head->read_step = 3;                          w[2] = buffer;
1902          case 3:                          if (idx == CCS_PATH_GROUP) {
1903                  if (!ccs_read_globally_usable_env_policy(head))                                  w[2] = container_of(ptr, struct ccs_path_group,
1904                          break;                                                      head)->member_name->name;
1905                  head->read_var2 = NULL;                          } else if (idx == CCS_NUMBER_GROUP) {
1906                  head->read_step = 4;                                  ccs_print_number(buffer, sizeof(buffer),
1907          case 4:                                                   &container_of
1908                  if (!ccs_read_domain_initializer_policy(head))                                                   (ptr, struct ccs_number_group,
1909                          break;                                                    head)->number);
1910                  head->read_var2 = NULL;                          } else if (idx == CCS_ADDRESS_GROUP) {
1911                  head->read_step = 6;                                  struct ccs_address_group *member =
1912          case 6:                                          container_of(ptr, typeof(*member),
1913                  if (!ccs_read_aggregator_policy(head))                                                       head);
1914                          break;                                  if (member->is_ipv6)
1915                  head->read_var2 = NULL;                                          ccs_print_ipv6(buffer, sizeof(buffer),
1916                  head->read_step = 7;                                                         member->min.ipv6,
1917          case 7:                                                         member->max.ipv6);
1918                  if (!ccs_read_file_pattern(head))                                  else
1919                          break;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1920                  head->read_var2 = NULL;                                                         member->min.ipv4,
1921                  head->read_step = 8;                                                         member->max.ipv4);
1922          case 8:                          }
1923                  if (!ccs_read_no_rewrite_policy(head))                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],
1924                          break;                                             w[2]))
1925                  head->read_var2 = NULL;                                  return false;
1926                  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;  
1927          }          }
1928            return true;
1929  }  }
1930    
1931  /**  /**
1932   * ccs_get_argv0 - Get argv[0].   * ccs_read_policy - Read "struct ccs_..._entry" list.
1933   *   *
1934   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1935     * @idx:  Index number.
1936   *   *
1937   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1938     *
1939     * Caller holds ccs_read_lock().
1940   */   */
1941  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1942  {  {
1943          struct linux_binprm *bprm = ee->bprm;          struct list_head *pos;
1944          char *arg_ptr = ee->tmp;          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {
1945          int arg_len = 0;                  const char *w[4] = { "", "", "", "" };
1946          unsigned long pos = bprm->p;                  char buffer[16];
1947          int offset = pos % PAGE_SIZE;                  struct ccs_acl_head *acl = container_of(pos, typeof(*acl),
1948          bool done = false;                                                          list);
1949          if (!bprm->argc)                  if (acl->is_deleted)
1950                  goto out;                          continue;
1951          while (1) {                  switch (idx) {
1952                  if (!ccs_dump_page(bprm, pos, &ee->dump))                  case CCS_ID_TRANSITION_CONTROL:
1953                          goto out;                          {
1954                  pos += PAGE_SIZE - offset;                                  struct ccs_transition_control *ptr =
1955                  /* Read. */                                          container_of(acl, typeof(*ptr), head);
1956                  while (offset < PAGE_SIZE) {                                  w[0] = ccs_transition_type[ptr->type];
1957                          const char *kaddr = ee->dump.data;                                  w[1] = ptr->program ? ptr->program->name
1958                          const unsigned char c = kaddr[offset++];                                          : "any";
1959                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                  w[2] = " from ";
1960                                  if (c == '\\') {                                  w[3] = ptr->domainname ? ptr->domainname->name
1961                                          arg_ptr[arg_len++] = '\\';                                          : "any";
1962                                          arg_ptr[arg_len++] = '\\';                          }
1963                                  } else if (c > ' ' && c < 127) {                          break;
1964                                          arg_ptr[arg_len++] = c;                  case CCS_ID_AGGREGATOR:
1965                                  } else {                          {
1966                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_aggregator *ptr =
1967                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(acl, typeof(*ptr), head);
1968                                          arg_ptr[arg_len++]                                  w[0] = CCS_KEYWORD_AGGREGATOR;
1969                                                  = ((c >> 3) & 7) + '0';                                  w[1] = ptr->original_name->name;
1970                                          arg_ptr[arg_len++] = (c & 7) + '0';                                  w[2] = " ";
1971                                  }                                  w[3] = ptr->aggregated_name->name;
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
1972                          }                          }
                 }  
                 offset = 0;  
                 if (done)  
1973                          break;                          break;
1974                    case CCS_ID_PATTERN:
1975                            {
1976                                    struct ccs_pattern *ptr =
1977                                            container_of(acl, typeof(*ptr), head);
1978                                    w[0] = CCS_KEYWORD_FILE_PATTERN;
1979                                    w[1] = ptr->pattern->name;
1980                            }
1981                            break;
1982                    case CCS_ID_NO_REWRITE:
1983                            {
1984                                    struct ccs_no_rewrite *ptr =
1985                                            container_of(acl, typeof(*ptr), head);
1986                                    w[0] = CCS_KEYWORD_DENY_REWRITE;
1987                                    w[1] = ptr->pattern->name;
1988                            }
1989                            break;
1990                    case CCS_ID_RESERVEDPORT:
1991                            {
1992                                    struct ccs_reserved *ptr =
1993                                            container_of(acl, typeof(*ptr), head);
1994                                    const u16 min_port = ptr->min_port;
1995                                    const u16 max_port = ptr->max_port;
1996                                    w[0] = CCS_KEYWORD_DENY_AUTOBIND;
1997                                    snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",
1998                                             min_port, min_port != max_port ?
1999                                             '-' : '\0', max_port);
2000                                    buffer[sizeof(buffer) - 1] = '\0';
2001                                    w[1] = buffer;
2002                            }
2003                            break;
2004                    default:
2005                            continue;
2006                    }
2007                    if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))
2008                            return false;
2009          }          }
2010          return true;          return true;
  out:  
         return false;  
2011  }  }
2012    
2013  /**  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)  
2014  {  {
2015          struct ccs_condition *cond;          if (!head->read_eof)
2016          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, CCS_GFP_FLAGS);  
         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;  
2017  }  }
2018    
2019  /**  /**
2020   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
2021   *   *
2022   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
2023   *   *
2024   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
2025   */   */
2026  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
2027  {  {
2028          struct ccs_condition *cond;          if (head->read_eof)
2029          char *buf;                  return;
2030          int len = 256;          while (head->read_step < CCS_MAX_POLICY &&
2031          const char *symlink = NULL;                 ccs_read_policy(head, head->read_step))
2032          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->read_step++;
2033          if (profile->learning->learning_symlink_target) {          if (head->read_step < CCS_MAX_POLICY)
2034                  symlink = r->obj->symlink_target->name;                  return;
2035                  len += strlen(symlink) + 18;          while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
2036          }                 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))
2037          buf = kmalloc(len, CCS_GFP_FLAGS);                  head->read_step++;
2038          if (!buf)          if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)
2039                  return NULL;                  return;
2040          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;  
2041  }  }
2042    
2043  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2147  static LIST_HEAD(ccs_query_list); Line 2062  static LIST_HEAD(ccs_query_list);
2062  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
2063  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
2064    
2065    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
2066    {
2067            va_list args;
2068            const int pos = strlen(buffer);
2069            va_start(args, fmt);
2070            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
2071            va_end(args);
2072    }
2073    
2074    static void ccs_truncate(char *str)
2075    {
2076            while (* (unsigned char *) str > (unsigned char) ' ')
2077                    str++;
2078            *str = '\0';
2079    }
2080    
2081  /**  /**
2082   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
2083   *   *
2084   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
2085   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
2086   *   *
2087   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
2088   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
# Line 2168  int ccs_supervisor(struct ccs_request_in Line 2099  int ccs_supervisor(struct ccs_request_in
2099          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query_entry *ccs_query_entry = NULL;
2100          bool quota_exceeded = false;          bool quota_exceeded = false;
2101          char *header;          char *header;
2102          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
2103                  r->domain = ccs_current_domain();          va_start(args, fmt);
2104          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
2105            va_end(args);
2106            if (r->mode == CCS_CONFIG_LEARNING) {
2107                  char *buffer;                  char *buffer;
2108                  struct ccs_condition *cond;                  char *realpath = NULL;
2109          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
2110                    char *symlink = NULL;
2111                    char *handler = NULL;
2112                    const struct ccs_preference *pref;
2113                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
2114                          return 0;                          return 0;
2115                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
2116                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (!buffer)  
2117                          return 0;                          return 0;
2118                  va_start(args, fmt);                  pref = ccs_profile(r->profile)->learning;
2119                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
2120                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
2121                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
2122                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
2123                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
2124                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
2125                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
2126                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
2127                          char str[] = "if task.type=execute_handler";                                  }
2128                          cond = ccs_get_condition(str);                          }
2129                  } else                          if (pref->learning_exec_realpath) {
2130                          cond = NULL;                                  realpath = strstr(header,
2131                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
2132                  ccs_put_condition(cond);                                  if (realpath) {
2133                  kfree(buffer);                                          realpath += 8;
2134                  /* fall through */                                          ccs_truncate(realpath);
2135          case CCS_CONFIG_PERMISSIVE:                                  }
2136                            }
2137                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
2138                               r->param.path.operation == CCS_TYPE_SYMLINK &&
2139                               pref->learning_symlink_target) {
2140                            symlink = strstr(header, " symlink.target=\"");
2141                            if (symlink)
2142                                    ccs_truncate(symlink + 1);
2143                    }
2144                    handler = strstr(header, "type=execute_handler");
2145                    if (handler)
2146                            ccs_truncate(handler);
2147                    buffer = kmalloc(len, CCS_GFP_FLAGS);
2148                    if (buffer) {
2149                            va_start(args, fmt);
2150                            vsnprintf(buffer, len - 1, fmt, args);
2151                            va_end(args);
2152                            if (handler || realpath || argv0 || symlink) {
2153                                    ccs_addprintf(buffer, len, " if");
2154                                    if (handler)
2155                                            ccs_addprintf(buffer, len, " task.%s",
2156                                                          handler);
2157                                    if (realpath)
2158                                            ccs_addprintf(buffer, len, " exec.%s",
2159                                                          realpath);
2160                                    if (argv0)
2161                                            ccs_addprintf(buffer, len,
2162                                                          " exec.argv[0]=%s",
2163                                                          argv0);
2164                                    if (symlink)
2165                                            ccs_addprintf(buffer, len, "%s",
2166                                                          symlink);
2167                            }
2168                            ccs_normalize_line(buffer);
2169                            ccs_write_domain2(buffer, domain, false);
2170                            kfree(buffer);
2171                    }
2172                    kfree(header);
2173                  return 0;                  return 0;
2174          }          }
2175            if (r->mode != CCS_CONFIG_ENFORCING)
2176                    return 0;
2177          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2178                  int i;                  int i;
2179                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
2180                          return -EPERM;                          return -EPERM;
2181                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2182                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2183                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2184                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2185                  }                  }
2186                  return -EPERM;                  return -EPERM;
2187          }          }
2188          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);  
2189          if (!header)          if (!header)
2190                  goto out;                  goto out;
2191          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);
# Line 2238  int ccs_supervisor(struct ccs_request_in Line 2207  int ccs_supervisor(struct ccs_request_in
2207          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2208          if (quota_exceeded)          if (quota_exceeded)
2209                  goto out;                  goto out;
2210          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",
2211                         ccs_query_entry->serial, r->retry, header);                   ccs_query_entry->serial, r->retry, header);
2212          kfree(header);          kfree(header);
2213          header = NULL;          header = NULL;
2214          va_start(args, fmt);          ccs_addprintf(ccs_query_entry->query, len, fmt, args);
         vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);  
2215          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);  
2216          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2217          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&ccs_query_entry->list, &ccs_query_list);
2218          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2470  int ccs_open_control(const u8 type, stru Line 2437  int ccs_open_control(const u8 type, stru
2437          head->type = type;          head->type = type;
2438          switch (type) {          switch (type) {
2439          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2440                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2441                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2442                  break;                  break;
2443          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2444                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2445                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2446                  break;                  break;
2447  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2448          case CCS_GRANTLOG: /* /proc/ccs/grant_log */          case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2449          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2450                  head->poll = ccs_poll_audit_log;                  head->poll = ccs_poll_log;
2451                  head->read = ccs_read_audit_log;                  head->read = ccs_read_log;
2452                  break;                  break;
2453  #endif  #endif
2454          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2521  int ccs_open_control(const u8 type, stru Line 2488  int ccs_open_control(const u8 type, stru
2488                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2489                  break;                  break;
2490          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2491                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2492                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2493                  break;                  break;
2494          }          }
2495          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2680  int ccs_write_control(struct file *file, Line 2647  int ccs_write_control(struct file *file,
2647                  return -EINTR;                  return -EINTR;
2648          idx = ccs_read_lock();          idx = ccs_read_lock();
2649          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
2650          if (head->write != ccs_write_pid &&          if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2651              head->write != ccs_write_domain_policy &&              !ccs_manager()) {
             !ccs_is_policy_manager()) {  
2652                  ccs_read_unlock(idx);                  ccs_read_unlock(idx);
2653                  mutex_unlock(&head->io_sem);                  mutex_unlock(&head->io_sem);
2654                  return -EPERM;                  return -EPERM;

Legend:
Removed from v.3625  
changed lines
  Added in v.3761

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