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

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

revision 3731 by kumaneko, Fri Jun 4 01:42:54 2010 UTC revision 3780 by kumaneko, Thu Jun 24 08:50:05 2010 UTC
# 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 218  static const char *ccs_yesno(const unsig Line 221  static const char *ccs_yesno(const unsig
221          return value ? "yes" : "no";          return value ? "yes" : "no";
222  }  }
223    
224    static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
225    {
226            va_list args;
227            const int pos = strlen(buffer);
228            va_start(args, fmt);
229            vsnprintf(buffer + pos, len - pos - 1, fmt, args);
230            va_end(args);
231    }
232    
233  /**  /**
234   * ccs_io_printf - Transactional printf() to "struct ccs_io_buffer" structure.   * ccs_flush - Flush queued string to userspace's buffer.
235   *   *
236   * @head: Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
  * @fmt:  The printf()'s format string, followed by parameters.  
237   *   *
238   * Returns true on success, false otherwise.   * Returns true if all data was flushed, false otherwise.
239     */
240    static bool ccs_flush(struct ccs_io_buffer *head)
241    {
242            while (head->r.w_pos) {
243                    const char *w = head->r.w[0];
244                    int len = strlen(w);
245                    if (len) {
246                            if (len > head->read_user_buf_avail)
247                                    len = head->read_user_buf_avail;
248                            if (!len)
249                                    return false;
250                            if (copy_to_user(head->read_user_buf, w, len))
251                                    return false;
252                            head->read_user_buf_avail -= len;
253                            head->read_user_buf += len;
254                            w += len;
255                    }
256                    if (*w) {
257                            head->r.w[0] = w;
258                            return false;
259                    }
260                    /* Add '\0' for audit logs and query. */
261                    if (head->poll) {
262                            if (!head->read_user_buf_avail ||
263                                copy_to_user(head->read_user_buf, "", 1))
264                                    return false;
265                            head->read_user_buf_avail--;
266                            head->read_user_buf++;
267                    }
268                    head->r.w_pos--;
269                    for (len = 0; len < head->r.w_pos; len++)
270                            head->r.w[len] = head->r.w[len + 1];
271            }
272            head->r.avail = 0;
273            return true;
274    }
275    
276    /**
277     * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
278     *
279     * @head:   Pointer to "struct ccs_io_buffer".
280     * @string: String to print.
281     *
282     * Note that @string has to be kept valid until @head is kfree()d.
283     * This means that char[] allocated on stack memory cannot be passed to
284     * this function. Use ccs_io_printf() for char[] allocated on stack memory.
285     */
286    static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
287    {
288            if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
289                    head->r.w[head->r.w_pos++] = string;
290                    ccs_flush(head);
291            } else
292                    WARN_ON(1);
293    }
294    
295    /**
296     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
297   *   *
298   * The snprintf() will truncate, but ccs_io_printf() won't.   * @head: Pointer to "struct ccs_io_buffer".
299     * @fmt:  The printf()'s format string, followed by parameters.
300   */   */
301  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
302  {  {
303          va_list args;          va_list args;
304          int len;          int len;
305          int pos = head->read_avail;          int pos = head->r.avail;
306          int size = head->readbuf_size - pos;          int size = head->readbuf_size - pos;
307          if (size <= 0)          if (size <= 0)
308                  return false;                  return;
309          va_start(args, fmt);          va_start(args, fmt);
310          len = vsnprintf(head->read_buf + pos, size, fmt, args);          len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
311          va_end(args);          va_end(args);
312          if (pos + len >= head->readbuf_size)          if (pos + len >= head->readbuf_size) {
313                  return false;                  WARN_ON(1);
314          head->read_avail += len;                  return;
315          return true;          }
316            head->r.avail += len;
317            ccs_set_string(head, head->read_buf + pos);
318    }
319    
320    static void ccs_set_space(struct ccs_io_buffer *head)
321    {
322            ccs_set_string(head, " ");
323    }
324    
325    static bool ccs_set_lf(struct ccs_io_buffer *head)
326    {
327            ccs_set_string(head, "\n");
328            return !head->r.w_pos;
329  }  }
330    
331  /**  /**
# Line 324  struct ccs_profile *ccs_profile(const u8 Line 407  struct ccs_profile *ccs_profile(const u8
407          return ptr;          return ptr;
408  }  }
409    
410  /**  static s8 ccs_find_yesno(const char *string, const char *find)
  * ccs_write_profile - Write profile table.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_write_profile(struct ccs_io_buffer *head)  
411  {  {
412          char *data = head->write_buf;          const char *cp = strstr(string, find);
413          unsigned int i;          if (cp) {
414          int value;                  cp += strlen(find);
415          int mode;                  if (!strncmp(cp, "=yes", 4))
416          u8 config;                          return 1;
417          bool use_default = false;                  else if (!strncmp(cp, "=no", 3))
418          char *cp;                          return 0;
         struct ccs_profile *profile;  
         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_assign_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
419          }          }
420          cp = strchr(data, '=');          return -1;
421          if (!cp)  }
422                  return -EINVAL;  
423          *cp++ = '\0';  static void ccs_set_bool(bool *b, const char *string, const char *find)
424          if (profile != &ccs_default_profile)  {
425                  use_default = strstr(cp, "use_default") != NULL;          switch (ccs_find_yesno(string, find)) {
426          if (strstr(cp, "verbose=yes"))          case 1:
427                  value = 1;                  *b = true;
428          else if (strstr(cp, "verbose=no"))                  break;
429                  value = 0;          case 0:
430          else                  *b = false;
431                  value = -1;                  break;
432          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_AUDIT)) {          }
433  #ifdef CONFIG_CCSECURITY_AUDIT  }
434                  char *cp2;  
435  #endif  static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
436    {
437            const char *cp = strstr(string, find);
438            if (cp)
439                    sscanf(cp + strlen(find), "=%u", i);
440    }
441    
442    static void ccs_set_pref(const char *name, const char *value,
443                             const bool use_default, struct ccs_profile *profile)
444    {
445            struct ccs_preference **pref;
446            bool *verbose;
447            if (!strcmp(name, "audit")) {
448                  if (use_default) {                  if (use_default) {
449                          profile->audit = &ccs_default_profile.preference;                          pref = &profile->audit;
450                          return 0;                          goto set_default;
451                  }                  }
452                  profile->audit = &profile->preference;                  profile->audit = &profile->preference;
453  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
454                  cp2 = strstr(cp, "max_grant_log=");                  ccs_set_uint(&profile->preference.audit_max_grant_log, value,
455                  if (cp2)                               "max_grant_log");
456                          sscanf(cp2 + 14, "%u",                  ccs_set_uint(&profile->preference.audit_max_reject_log, value,
457                                 &profile->preference.audit_max_grant_log);                               "max_reject_log");
                 cp2 = strstr(cp, "max_reject_log=");  
                 if (cp2)  
                         sscanf(cp2 + 15, "%u",  
                                &profile->preference.audit_max_reject_log);  
458  #endif  #endif
459                  if (strstr(cp, "task_info=yes"))                  ccs_set_bool(&profile->preference.audit_task_info, value,
460                          profile->preference.audit_task_info = true;                               "task_info");
461                  else if (strstr(cp, "task_info=no"))                  ccs_set_bool(&profile->preference.audit_path_info, value,
462                          profile->preference.audit_task_info = false;                               "path_info");
463                  if (strstr(cp, "path_info=yes"))                  return;
                         profile->preference.audit_path_info = true;  
                 else if (strstr(cp, "path_info=no"))  
                         profile->preference.audit_path_info = false;  
                 return 0;  
464          }          }
465          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_ENFORCING)) {          if (!strcmp(name, "enforcing")) {
                 char *cp2;  
466                  if (use_default) {                  if (use_default) {
467                          profile->enforcing = &ccs_default_profile.preference;                          pref = &profile->enforcing;
468                          return 0;                          goto set_default;
469                  }                  }
470                  profile->enforcing = &profile->preference;                  profile->enforcing = &profile->preference;
471                  if (value >= 0)                  ccs_set_uint(&profile->preference.enforcing_penalty, value,
472                          profile->preference.enforcing_verbose = value;                               "penalty");
473                  cp2 = strstr(cp, "penalty=");                  verbose = &profile->preference.enforcing_verbose;
474                  if (cp2)                  goto set_verbose;
                         sscanf(cp2 + 8, "%u",  
                                &profile->preference.enforcing_penalty);  
                 return 0;  
475          }          }
476          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_PERMISSIVE)) {          if (!strcmp(name, "permissive")) {
477                  if (use_default) {                  if (use_default) {
478                          profile->permissive = &ccs_default_profile.preference;                          pref = &profile->permissive;
479                          return 0;                          goto set_default;
480                  }                  }
481                  profile->permissive = &profile->preference;                  profile->permissive = &profile->preference;
482                  if (value >= 0)                  verbose = &profile->preference.permissive_verbose;
483                          profile->preference.permissive_verbose = value;                  goto set_verbose;
                 return 0;  
484          }          }
485          if (!strcmp(data, CCS_KEYWORD_PREFERENCE_LEARNING)) {          if (!strcmp(name, "learning")) {
                 char *cp2;  
486                  if (use_default) {                  if (use_default) {
487                          profile->learning = &ccs_default_profile.preference;                          pref = &profile->learning;
488                          return 0;                          goto set_default;
489                  }                  }
490                  profile->learning = &profile->preference;                  profile->learning = &profile->preference;
491                  if (value >= 0)                  ccs_set_uint(&profile->preference.learning_max_entry, value,
492                          profile->preference.learning_verbose = value;                               "max_entry");
493                  cp2 = strstr(cp, "max_entry=");                  ccs_set_bool(&profile->preference.learning_exec_realpath,
494                  if (cp2)                               value, "exec.realpath");
495                          sscanf(cp2 + 10, "%u",                  ccs_set_bool(&profile->preference.learning_exec_argv0, value,
496                                 &profile->preference.learning_max_entry);                               "exec.argv0");
497                  if (strstr(cp, "exec.realpath=yes"))                  ccs_set_bool(&profile->preference.learning_symlink_target,
498                          profile->preference.learning_exec_realpath = true;                               value, "symlink.target");
499                  else if (strstr(cp, "exec.realpath=no"))                  verbose = &profile->preference.learning_verbose;
500                          profile->preference.learning_exec_realpath = false;                  goto set_verbose;
501                  if (strstr(cp, "exec.argv0=yes"))          }
502                          profile->preference.learning_exec_argv0 = true;          return;
503                  else if (strstr(cp, "exec.argv0=no"))   set_default:
504                          profile->preference.learning_exec_argv0 = false;          *pref = &ccs_default_profile.preference;
505                  if (strstr(cp, "symlink.target=yes"))          return;
506                          profile->preference.learning_symlink_target = true;   set_verbose:
507                  else if (strstr(cp, "symlink.target=no"))          ccs_set_bool(verbose, value, "verbose");
508                          profile->preference.learning_symlink_target = false;  }
509                  return 0;  
510          }  static int ccs_set_mode(char *name, const char *value, const bool use_default,
511          if (profile == &ccs_default_profile)                          struct ccs_profile *profile)
512                  return -EINVAL;  {
513          if (!strcmp(data, "COMMENT")) {          u8 i;
514                  const struct ccs_path_info *old_comment = profile->comment;          u8 config;
515                  profile->comment = ccs_get_name(cp);          if (!strcmp(name, "CONFIG")) {
                 ccs_put_name(old_comment);  
                 return 0;  
         }  
         if (!strcmp(data, "CONFIG")) {  
516                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  i = CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
517                          + CCS_MAX_MAC_CATEGORY_INDEX;                          + CCS_MAX_MAC_CATEGORY_INDEX;
518                  config = profile->default_config;                  config = profile->default_config;
519          } else if (ccs_str_starts(&data, "CONFIG::")) {          } else if (ccs_str_starts(&name, "CONFIG::")) {
520                  config = 0;                  config = 0;
521                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX
522                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {
523                          if (strcmp(data, ccs_mac_keywords[i]))                          if (strcmp(name, ccs_mac_keywords[i]))
524                                  continue;                                  continue;
525                          config = profile->config[i];                          config = profile->config[i];
526                          break;                          break;
# Line 477  static int ccs_write_profile(struct ccs_ Line 534  static int ccs_write_profile(struct ccs_
534          if (use_default) {          if (use_default) {
535                  config = CCS_CONFIG_USE_DEFAULT;                  config = CCS_CONFIG_USE_DEFAULT;
536          } else {          } else {
537                  for (mode = 3; mode >= 0; mode--)                  u8 mode;
538                          if (strstr(cp, ccs_mode_4[mode]))                  for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
539                            if (strstr(value, ccs_mode[mode]))
540                                  /*                                  /*
541                                   * Update lower 3 bits in order to distinguish                                   * Update lower 3 bits in order to distinguish
542                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.                                   * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
# Line 486  static int ccs_write_profile(struct ccs_ Line 544  static int ccs_write_profile(struct ccs_
544                                  config = (config & ~7) | mode;                                  config = (config & ~7) | mode;
545  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
546                  if (config != CCS_CONFIG_USE_DEFAULT) {                  if (config != CCS_CONFIG_USE_DEFAULT) {
547                          if (strstr(cp, "grant_log=yes"))                          switch (ccs_find_yesno(value, "grant_log")) {
548                            case 1:
549                                  config |= CCS_CONFIG_WANT_GRANT_LOG;                                  config |= CCS_CONFIG_WANT_GRANT_LOG;
550                          else if (strstr(cp, "grant_log=no"))                                  break;
551                            case 0:
552                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;                                  config &= ~CCS_CONFIG_WANT_GRANT_LOG;
553                          if (strstr(cp, "reject_log=yes"))                                  break;
554                            }
555                            switch (ccs_find_yesno(value, "reject_log")) {
556                            case 1:
557                                  config |= CCS_CONFIG_WANT_REJECT_LOG;                                  config |= CCS_CONFIG_WANT_REJECT_LOG;
558                          else if (strstr(cp, "reject_log=no"))                                  break;
559                            case 0:
560                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;                                  config &= ~CCS_CONFIG_WANT_REJECT_LOG;
561                                    break;
562                            }
563                  }                  }
564  #endif  #endif
565          }          }
# Line 505  static int ccs_write_profile(struct ccs_ Line 571  static int ccs_write_profile(struct ccs_
571          return 0;          return 0;
572  }  }
573    
574  static bool ccs_print_preference(struct ccs_io_buffer *head, const int idx)  /**
575     * ccs_write_profile - Write profile table.
576     *
577     * @head: Pointer to "struct ccs_io_buffer".
578     *
579     * Returns 0 on success, negative value otherwise.
580     */
581    static int ccs_write_profile(struct ccs_io_buffer *head)
582    {
583            char *data = head->write_buf;
584            bool use_default = false;
585            char *cp;
586            int i;
587            struct ccs_profile *profile;
588            if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)
589                    return 0;
590            i = simple_strtoul(data, &cp, 10);
591            if (data == cp) {
592                    profile = &ccs_default_profile;
593            } else {
594                    if (*cp != '-')
595                            return -EINVAL;
596                    data = cp + 1;
597                    profile = ccs_assign_profile(i);
598                    if (!profile)
599                            return -EINVAL;
600            }
601            cp = strchr(data, '=');
602            if (!cp)
603                    return -EINVAL;
604            *cp++ = '\0';
605            if (profile != &ccs_default_profile)
606                    use_default = strstr(cp, "use_default") != NULL;
607            if (ccs_str_starts(&data, "PREFERENCE::")) {
608                    ccs_set_pref(data, cp, use_default, profile);
609                    return 0;
610            }
611            if (profile == &ccs_default_profile)
612                    return -EINVAL;
613            if (!strcmp(data, "COMMENT")) {
614                    const struct ccs_path_info *old_comment = profile->comment;
615                    profile->comment = ccs_get_name(cp);
616                    ccs_put_name(old_comment);
617                    return 0;
618            }
619            return ccs_set_mode(data, cp, use_default, profile);
620    }
621    
622    static void ccs_print_preference(struct ccs_io_buffer *head, const int idx)
623  {  {
624          struct ccs_preference *pref = &ccs_default_profile.preference;          struct ccs_preference *pref = &ccs_default_profile.preference;
625          const struct ccs_profile *profile = idx >= 0 ?          const struct ccs_profile *profile = idx >= 0 ?
# Line 518  static bool ccs_print_preference(struct Line 632  static bool ccs_print_preference(struct
632          if (profile) {          if (profile) {
633                  pref = profile->audit;                  pref = profile->audit;
634                  if (pref == &ccs_default_profile.preference)                  if (pref == &ccs_default_profile.preference)
635                          pref = NULL;                          goto skip0;
636          }          }
637          if (pref && !ccs_io_printf(head, "%s%s={ "          ccs_io_printf(head, "%sPREFERENCE::%s={ "
638  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
639                                     "max_grant_log=%u max_reject_log=%u "                        "max_grant_log=%u max_reject_log=%u "
640  #endif  #endif
641                                     "task_info=%s path_info=%s }\n", buffer,                        "task_info=%s path_info=%s }\n", buffer,
642                                     CCS_KEYWORD_PREFERENCE_AUDIT,                        "audit",
643  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
644                                     pref->audit_max_grant_log,                        pref->audit_max_grant_log,
645                                     pref->audit_max_reject_log,                        pref->audit_max_reject_log,
646  #endif  #endif
647                                     ccs_yesno(pref->audit_task_info),                        ccs_yesno(pref->audit_task_info),
648                                     ccs_yesno(pref->audit_path_info)))                        ccs_yesno(pref->audit_path_info));
649                  return false;   skip0:
650          if (profile) {          if (profile) {
651                  pref = profile->learning;                  pref = profile->learning;
652                  if (pref == &ccs_default_profile.preference)                  if (pref == &ccs_default_profile.preference)
653                          pref = NULL;                          goto skip1;
654          }          }
655          if (pref && !ccs_io_printf(head, "%s%s={ "          ccs_io_printf(head, "%sPREFERENCE::%s={ "
656                                     "verbose=%s max_entry=%u exec.realpath=%s "                        "verbose=%s max_entry=%u exec.realpath=%s "
657                                     "exec.argv0=%s symlink.target=%s }\n",                        "exec.argv0=%s symlink.target=%s }\n",
658                                     buffer, CCS_KEYWORD_PREFERENCE_LEARNING,                        buffer, "learning",
659                                     ccs_yesno(pref->learning_verbose),                        ccs_yesno(pref->learning_verbose),
660                                     pref->learning_max_entry,                        pref->learning_max_entry,
661                                     ccs_yesno(pref->learning_exec_realpath),                        ccs_yesno(pref->learning_exec_realpath),
662                                     ccs_yesno(pref->learning_exec_argv0),                        ccs_yesno(pref->learning_exec_argv0),
663                                     ccs_yesno(pref->learning_symlink_target)))                        ccs_yesno(pref->learning_symlink_target));
664                  return false;   skip1:
665          if (profile) {          if (profile) {
666                  pref = profile->permissive;                  pref = profile->permissive;
667                  if (pref == &ccs_default_profile.preference)                  if (pref == &ccs_default_profile.preference)
668                          pref = NULL;                          goto skip2;
669          }          }
670          if (pref && !ccs_io_printf(head, "%s%s={ verbose=%s }\n", buffer,          ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
671                                     CCS_KEYWORD_PREFERENCE_PERMISSIVE,                        buffer, "permissive",
672                                     ccs_yesno(pref->permissive_verbose)))                        ccs_yesno(pref->permissive_verbose));
673                  return false;   skip2:
674          if (profile) {          if (profile) {
675                  pref = profile->enforcing;                  pref = profile->enforcing;
676                  if (pref == &ccs_default_profile.preference)                  if (pref == &ccs_default_profile.preference)
677                          pref = NULL;                          return;
678          }          }
679          return !pref || ccs_io_printf(head, "%s%s={ verbose=%s penalty=%u }\n",          ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
680                                        buffer, CCS_KEYWORD_PREFERENCE_ENFORCING,                        "penalty=%u }\n", buffer, "enforcing",
681                                        ccs_yesno(pref->enforcing_verbose),                        ccs_yesno(pref->enforcing_verbose),
682                                        pref->enforcing_penalty);                        pref->enforcing_penalty);
683    }
684    
685    static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
686    {
687            ccs_io_printf(head, "={ mode=%s", ccs_mode[config & 3]);
688    #ifdef CONFIG_CCSECURITY_AUDIT
689            ccs_io_printf(head, " grant_log=%s reject_log=%s",
690                          ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
691                          ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
692    #endif
693            ccs_set_string(head, " }\n");
694  }  }
695    
696  /**  /**
# Line 575  static bool ccs_print_preference(struct Line 700  static bool ccs_print_preference(struct
700   */   */
701  static void ccs_read_profile(struct ccs_io_buffer *head)  static void ccs_read_profile(struct ccs_io_buffer *head)
702  {  {
703          int index;          u8 index;
704          if (head->read_eof)          const struct ccs_profile *profile;
705                  return;   next:
706          if (head->read_bit)          index = head->r.index;
707                  goto body;          profile = ccs_profile_ptr[index];
708          ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");          switch (head->r.step) {
709          ccs_print_preference(head, -1);          case 0:
710          head->read_bit = 1;                  ccs_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
711   body:                  ccs_print_preference(head, -1);
712          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {                  head->r.step++;
713                  bool done;                  break;
714                  u8 config;          case 1:
715                  int i;                  for ( ; head->r.index < CCS_MAX_PROFILES;
716                  int pos;                        head->r.index++)
717                  const struct ccs_profile *profile = ccs_profile_ptr[index];                          if (ccs_profile_ptr[head->r.index])
718                  const struct ccs_path_info *comment;                                  break;
719                  head->read_step = index;                  if (head->r.index == CCS_MAX_PROFILES)
720                  if (!profile)                          return;
721                          continue;                  head->r.step++;
722                  pos = head->read_avail;                  break;
723                  comment = profile->comment;          case 2:
724                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,                  {
725                                       comment ? comment->name : "");                          const struct ccs_path_info *comment = profile->comment;
726                  if (!done)                          ccs_io_printf(head, "%u-COMMENT=", index);
727                          goto out;                          ccs_set_string(head, comment ? comment->name : "");
728                  config = profile->default_config;                          ccs_set_lf(head);
729  #ifdef CONFIG_CCSECURITY_AUDIT                          head->r.step++;
730                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                  }
731                                     "grant_log=%s reject_log=%s }\n", index,                  break;
732                                     "CONFIG", "", ccs_mode_4[config & 3],          case 3:
733                                     ccs_yesno(config &                  {
734                                               CCS_CONFIG_WANT_GRANT_LOG),                          ccs_io_printf(head, "%u-%s", index, "CONFIG");
735                                     ccs_yesno(config &                          ccs_print_config(head, profile->default_config);
736                                               CCS_CONFIG_WANT_REJECT_LOG)))                          head->r.bit = 0;
737                          goto out;                          head->r.step++;
738  #else                  }
739                  if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n", index,                  break;
740                                     "CONFIG", "", ccs_mode_4[config & 3]))          case 4:
741                          goto out;                  for ( ; head->r.bit < CCS_MAX_MAC_INDEX
742  #endif                                + CCS_MAX_CAPABILITY_INDEX
743                  for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX                                + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
744                               + CCS_MAX_MAC_CATEGORY_INDEX; i++) {                          const u8 i = head->r.bit;
745  #ifdef CONFIG_CCSECURITY_AUDIT                          const u8 config = profile->config[i];
                         const char *g;  
                         const char *r;  
 #endif  
                         config = profile->config[i];  
746                          if (config == CCS_CONFIG_USE_DEFAULT)                          if (config == CCS_CONFIG_USE_DEFAULT)
747                                  continue;                                  continue;
748  #ifdef CONFIG_CCSECURITY_AUDIT                          ccs_io_printf(head, "%u-%s%s", index, "CONFIG::",
749                          g = ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG);                                        ccs_mac_keywords[i]);
750                          r = ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG);                          ccs_print_config(head, config);
751                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s "                          head->r.bit++;
752                                             "grant_log=%s reject_log=%s }\n",                          break;
753                                             index, "CONFIG::",                  }
754                                             ccs_mac_keywords[i],                  if (head->r.bit == CCS_MAX_MAC_INDEX
755                                             ccs_mode_4[config & 3], g, r))                      + CCS_MAX_CAPABILITY_INDEX
756                                  goto out;                      + CCS_MAX_MAC_CATEGORY_INDEX) {
757  #else                          ccs_print_preference(head, index);
758                          if (!ccs_io_printf(head, "%u-%s%s={ mode=%s }\n",                          head->r.index++;
759                                             index, "CONFIG::",                          head->r.step = 1;
                                            ccs_mac_keywords[i],  
                                            ccs_mode_4[config & 3]))  
                                 goto out;  
 #endif  
760                  }                  }
                 if (!ccs_print_preference(head, index))  
                         goto out;  
                 continue;  
  out:  
                 head->read_avail = pos;  
761                  break;                  break;
762          }          }
763          if (index == CCS_MAX_PROFILES)          if (ccs_flush(head))
764                  head->read_eof = true;                  goto next;
765  }  }
766    
767  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,  static bool ccs_same_manager_entry(const struct ccs_acl_head *a,
# Line 683  static int ccs_update_manager_entry(cons Line 795  static int ccs_update_manager_entry(cons
795          if (!e.manager)          if (!e.manager)
796                  return error;                  return error;
797          error = ccs_update_policy(&e.head, sizeof(e), is_delete,          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
798                                    CCS_ID_MANAGER, ccs_same_manager_entry);                                    &ccs_policy_list[CCS_ID_MANAGER],
799                                      ccs_same_manager_entry);
800          ccs_put_name(e.manager);          ccs_put_name(e.manager);
801          return error;          return error;
802  }  }
# Line 715  static int ccs_write_manager(struct ccs_ Line 828  static int ccs_write_manager(struct ccs_
828   */   */
829  static void ccs_read_manager(struct ccs_io_buffer *head)  static void ccs_read_manager(struct ccs_io_buffer *head)
830  {  {
831          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
832                  return;                  return;
833          list_for_each_cookie(pos, head->read_var2,          list_for_each_cookie(head->r.acl, &ccs_policy_list[CCS_ID_MANAGER]) {
834                               &ccs_policy_list[CCS_ID_MANAGER]) {                  struct ccs_manager *ptr =
835                  struct ccs_manager *ptr                          list_entry(head->r.acl, typeof(*ptr), head.list);
                         = list_entry(pos, typeof(*ptr), head.list);  
836                  if (ptr->head.is_deleted)                  if (ptr->head.is_deleted)
837                          continue;                          continue;
838                  if (!ccs_io_printf(head, "%s\n", ptr->manager->name))                  if (!ccs_flush(head))
839                          return;                          return;
840                    ccs_set_string(head, ptr->manager->name);
841                    ccs_set_lf(head);
842          }          }
843          head->read_eof = true;          head->r.eof = true;
844  }  }
845    
846  /**  /**
# Line 816  static bool ccs_select_one(struct ccs_io Line 929  static bool ccs_select_one(struct ccs_io
929          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
930          bool global_pid = false;          bool global_pid = false;
931          if (!strcmp(data, "allow_execute")) {          if (!strcmp(data, "allow_execute")) {
932                  head->read_execute_only = true;                  head->r.print_execute_only = true;
933                  return true;                  return true;
934          }          }
935          if (sscanf(data, "pid=%u", &pid) == 1 ||          if (sscanf(data, "pid=%u", &pid) == 1 ||
# Line 840  static bool ccs_select_one(struct ccs_io Line 953  static bool ccs_select_one(struct ccs_io
953                          domain = ccs_find_domain(data + 7);                          domain = ccs_find_domain(data + 7);
954          } else          } else
955                  return false;                  return false;
956          head->write_var1 = domain;          head->w.domain = domain;
957          /* Accessing read_buf is safe because head->io_sem is held. */          /* Accessing read_buf is safe because head->io_sem is held. */
958          if (!head->read_buf)          if (!head->read_buf)
959                  return true; /* Do nothing if open(O_WRONLY). */                  return true; /* Do nothing if open(O_WRONLY). */
960          head->read_avail = 0;          memset(&head->r, 0, sizeof(head->r));
961            head->r.print_this_domain_only = true;
962            head->r.eof = !domain;
963            head->r.domain = &domain->list;
964          ccs_io_printf(head, "# select %s\n", data);          ccs_io_printf(head, "# select %s\n", data);
965          head->read_single_domain = true;          if (domain && domain->is_deleted)
966          head->read_eof = !domain;                  ccs_set_string(head, "# This is a deleted domain.\n");
         if (domain) {  
                 struct ccs_domain_info *d;  
                 head->read_var1 = NULL;  
                 list_for_each_entry_rcu(d, &ccs_domain_list, list) {  
                         if (d == domain)  
                                 break;  
                         head->read_var1 = &d->list;  
                 }  
                 head->read_var2 = NULL;  
                 head->read_bit = 0;  
                 head->read_step = 0;  
                 if (domain->is_deleted)  
                         ccs_io_printf(head, "# This is a deleted domain.\n");  
         }  
967          return true;          return true;
968  }  }
969    
970  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,  static int ccs_write_domain2(char *data, struct ccs_domain_info *domain,
971                               struct ccs_condition *cond, const bool is_delete)                               const bool is_delete)
972  {  {
         u8 i;  
973          static const struct {          static const struct {
974                  const char *keyword;                  const char *keyword;
975                  int (*write) (char *, struct ccs_domain_info *,                  int (*write) (char *, struct ccs_domain_info *,
# Line 882  static int ccs_write_domain2(char *data, Line 983  static int ccs_write_domain2(char *data,
983          };          };
984          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,          int (*write) (char *, struct ccs_domain_info *, struct ccs_condition *,
985                        const bool) = ccs_write_file;                        const bool) = ccs_write_file;
986            int error;
987            u8 i;
988            struct ccs_condition *cond = NULL;
989            char *cp = ccs_find_condition_part(data);
990            if (cp) {
991                    cond = ccs_get_condition(cp);
992                    if (!cond)
993                            return -EINVAL;
994            }
995          for (i = 0; i < 5; i++) {          for (i = 0; i < 5; i++) {
996                  if (!ccs_str_starts(&data, ccs_callback[i].keyword))                  if (!ccs_str_starts(&data, ccs_callback[i].keyword))
997                          continue;                          continue;
998                  write = ccs_callback[i].write;                  write = ccs_callback[i].write;
999                  break;                  break;
1000          }          }
1001          return write(data, domain, cond, is_delete);          error = write(data, domain, cond, is_delete);
1002            if (cond)
1003                    ccs_put_condition(cond);
1004            return error;
1005  }  }
1006    
1007    static const char *ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
1008            [CCS_DIF_QUOTA_WARNED] = CCS_KEYWORD_QUOTA_EXCEEDED "\n",
1009            [CCS_DIF_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
1010            [CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]
1011            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n",
1012            [CCS_DIF_IGNORE_GLOBAL_ALLOW_ENV]
1013            = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n",
1014            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
1015    };
1016            
1017  /**  /**
1018   * ccs_write_domain - Write domain policy.   * ccs_write_domain - Write domain policy.
1019   *   *
# Line 901  static int ccs_write_domain2(char *data, Line 1024  static int ccs_write_domain2(char *data,
1024  static int ccs_write_domain(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1025  {  {
1026          char *data = head->write_buf;          char *data = head->write_buf;
1027          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1028          bool is_delete = false;          bool is_delete = false;
1029          bool is_select = false;          bool is_select = false;
1030          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
1031          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
1032                  is_delete = true;                  is_delete = true;
1033          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
# Line 925  static int ccs_write_domain(struct ccs_i Line 1045  static int ccs_write_domain(struct ccs_i
1045                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1046                  else                  else
1047                          domain = ccs_assign_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
1048                  head->write_var1 = domain;                  head->w.domain = domain;
1049                  return 0;                  return 0;
1050          }          }
1051          if (!domain)          if (!domain)
# Line 937  static int ccs_write_domain(struct ccs_i Line 1057  static int ccs_write_domain(struct ccs_i
1057                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1058                  return 0;                  return 0;
1059          }          }
1060          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1061                  domain->ignore_global = !is_delete;                  const char *cp = ccs_dif[profile];
1062                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
1063          }                          continue;
1064          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {                  domain->flags[profile] = !is_delete;
                 domain->ignore_global_allow_read = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {  
                 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;  
1065                  return 0;                  return 0;
1066          }          }
1067          cp = ccs_find_condition_part(data);          return ccs_write_domain2(data, domain, is_delete);
         if (cp) {  
                 cond = ccs_get_condition(cp);  
                 if (!cond)  
                         return -EINVAL;  
         }  
         error = ccs_write_domain2(data, domain, cond, is_delete);  
         if (cond)  
                 ccs_put_condition(cond);  
         return error;  
1068  }  }
1069    
1070  /**  /**
# Line 974  static int ccs_write_domain(struct ccs_i Line 1072  static int ccs_write_domain(struct ccs_i
1072   *   *
1073   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1074   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1075   */   */
1076  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1077                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1078  {  {
1079          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1080          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1081                  head->read_avail--;                  ccs_set_space(head);
1082          if (ptr->is_group)          if (ptr->is_group) {
1083                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1084                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1085          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1086  }                  if (cond)
1087                            ccs_set_string(head, "\"");
1088  /**                  ccs_set_string(head, ptr->filename->name);
1089   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1090   *                          ccs_set_string(head, "\"");
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_name_union".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_name_union_quoted(struct ccs_io_buffer *head,  
                                         const struct ccs_name_union *ptr)  
 {  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         return ccs_io_printf(head, "\"%s\"", ptr->filename->name);  
 }  
   
 static void ccs_print_number(char *buffer, int buffer_len,  
                              const struct ccs_number_union *ptr)  
 {  
         int i;  
         unsigned long min = ptr->values[0];  
         const unsigned long max = ptr->values[1];  
         u8 min_type = ptr->value_type[0];  
         const u8 max_type = ptr->value_type[1];  
         memset(buffer, 0, buffer_len);  
         buffer_len -= 2;  
         for (i = 0; i < 2; i++) {  
                 int len;  
                 switch (min_type) {  
                 case CCS_VALUE_TYPE_HEXADECIMAL:  
                         snprintf(buffer, buffer_len, "0x%lX", min);  
                         break;  
                 case CCS_VALUE_TYPE_OCTAL:  
                         snprintf(buffer, buffer_len, "0%lo", min);  
                         break;  
                 default:  
                         snprintf(buffer, buffer_len, "%lu", min);  
                         break;  
                 }  
                 if (min == max && min_type == max_type)  
                         break;  
                 len = strlen(buffer);  
                 buffer[len++] = '-';  
                 buffer += len;  
                 buffer_len -= len;  
                 min_type = max_type;  
                 min = max;  
1091          }          }
1092  }  }
1093    
1094  /**  /**
  * ccs_print_number_union_common - Print a ccs_number_union.  
  *  
  * @head:       Pointer to "struct ccs_io_buffer".  
  * @ptr:        Pointer to "struct ccs_number_union".  
  * @need_space: True if a space character is needed.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_number_union_common(struct ccs_io_buffer *head,  
                                           const struct ccs_number_union *ptr,  
                                           const bool need_space)  
 {  
         char buffer[128];  
         if (need_space && !ccs_io_printf(head, " "))  
                 return false;  
         if (ptr->is_group)  
                 return ccs_io_printf(head, "@%s",  
                                      ptr->group->group_name->name);  
         ccs_print_number(buffer, sizeof(buffer), ptr);  
         return ccs_io_printf(head, "%s", buffer);  
 }  
   
 /**  
1095   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1096   *   *
1097   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1098   * @ptr:        Pointer to "struct ccs_number_union".   * @ptr:  Pointer to "struct ccs_number_union".
  *  
  * Returns true on success, false otherwise.  
1099   */   */
1100  static bool ccs_print_number_union(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1101                                     const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1102  {  {
1103          return ccs_print_number_union_common(head, ptr, true);          if (!head->r.print_cond_part)
1104  }                  ccs_set_space(head);
1105            if (ptr->is_group) {
1106  /**                  ccs_set_string(head, "@");
1107   * ccs_print_number_union_nospace - Print a ccs_number_union without a space character.                  ccs_set_string(head, ptr->group->group_name->name);
1108   *          } else {
1109   * @head:       Pointer to "struct ccs_io_buffer".                  int i;
1110   * @ptr:        Pointer to "struct ccs_number_union".                  unsigned long min = ptr->values[0];
1111   *                  const unsigned long max = ptr->values[1];
1112   * Returns true on success, false otherwise.                  u8 min_type = ptr->value_type[0];
1113   */                  const u8 max_type = ptr->value_type[1];
1114  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,                  char buffer[128];
1115                                             const struct ccs_number_union *ptr)                  buffer[0] = '\0';
1116  {                  for (i = 0; i < 2; i++) {
1117          return ccs_print_number_union_common(head, ptr, false);                          switch (min_type) {
1118                            case CCS_VALUE_TYPE_HEXADECIMAL:
1119                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1120                                                  min);
1121                                    break;
1122                            case CCS_VALUE_TYPE_OCTAL:
1123                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1124                                                  min);
1125                                    break;
1126                            default:
1127                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1128                                                  min);
1129                                    break;
1130                            }
1131                            if (min == max && min_type == max_type)
1132                                    break;
1133                            ccs_addprintf(buffer, sizeof(buffer), "-");
1134                            min_type = max_type;
1135                            min = max;
1136                    }
1137                    ccs_io_printf(head, "%s", buffer);
1138            }
1139  }  }
1140    
1141  /**  /**
1142   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1143   *   *
1144   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1145   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1146   *   *
1147   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1148   */   */
1149  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1150                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1151  {  {
1152          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1153          const struct ccs_number_union *numbers_p;          case 0:
1154          const struct ccs_name_union *names_p;                  {
1155          const struct ccs_argv *argv;                          if (cond->condc)
1156          const struct ccs_envp *envp;                                  ccs_set_string(head, " if");
1157          u16 condc;                          head->r.cond_index = 0;
1158          u16 i;                          head->r.cond_step++;
1159          u16 j;                  }
1160          char buffer[32];                  /* fall through */
1161          if (!cond)          case 1:
1162                  goto no_condition;                  {
1163          condc = cond->condc;                          const u16 condc = cond->condc;
1164          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_condition_element *condp =
1165          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(condp)) (cond + 1);
1166          names_p = (const struct ccs_name_union *)                          const struct ccs_number_union *numbers_p =
1167                  (numbers_p + cond->numbers_count);                                  (typeof(numbers_p)) (condp + condc);
1168          argv = (const struct ccs_argv *) (names_p + cond->names_count);                          const struct ccs_name_union *names_p =
1169          envp = (const struct ccs_envp *) (argv + cond->argc);                                  (typeof(names_p))
1170          memset(buffer, 0, sizeof(buffer));                                  (numbers_p + cond->numbers_count);
1171          if (condc && !ccs_io_printf(head, "%s", " if"))                          const struct ccs_argv *argv =
1172                  goto out;                                  (typeof(argv)) (names_p + cond->names_count);
1173          for (i = 0; i < condc; i++) {                          const struct ccs_envp *envp =
1174                  const u8 match = condp->equals;                                  (typeof(envp)) (argv + cond->argc);
1175                  const u8 left = condp->left;                          u16 skip;
1176                  const u8 right = condp->right;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1177                  condp++;                                  const u8 left = condp->left;
1178                  switch (left) {                                  const u8 right = condp->right;
1179                  case CCS_ARGV_ENTRY:                                  condp++;
1180                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  switch (left) {
1181                                             argv->index, argv->is_not ?                                  case CCS_ARGV_ENTRY:
1182                                             "!=" : "=", argv->value->name))                                          argv++;
1183                                  goto out;                                          continue;
1184                          argv++;                                  case CCS_ENVP_ENTRY:
1185                          continue;                                          envp++;
1186                  case CCS_ENVP_ENTRY:                                          continue;
1187                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  case CCS_NUMBER_UNION:
1188                                             envp->name->name, envp->is_not ?                                          numbers_p++;
1189                                             "!=" : "="))                                          break;
1190                                  goto out;                                  }
1191                          if (envp->value) {                                  switch (right) {
1192                                  if (!ccs_io_printf(head, "\"%s\"",                                  case CCS_NAME_UNION:
1193                                                     envp->value->name))                                          names_p++;
1194                                          goto out;                                          break;
1195                          } else {                                  case CCS_NUMBER_UNION:
1196                                  if (!ccs_io_printf(head, "NULL"))                                          numbers_p++;
1197                                          goto out;                                          break;
1198                                    }
1199                            }
1200                            while (head->r.cond_index < condc) {
1201                                    const u8 match = condp->equals;
1202                                    const u8 left = condp->left;
1203                                    const u8 right = condp->right;
1204                                    if (!ccs_flush(head))
1205                                            return false;
1206                                    condp++;
1207                                    head->r.cond_index++;
1208                                    ccs_set_space(head);
1209                                    switch (left) {
1210                                    case CCS_ARGV_ENTRY:
1211                                            ccs_io_printf(head,
1212                                                          "exec.argv[%u]%s\"%s\"",
1213                                                          argv->index,
1214                                                          argv->is_not ?
1215                                                          "!=" : "=",
1216                                                          argv->value->name);
1217                                            argv++;
1218                                            continue;
1219                                    case CCS_ENVP_ENTRY:
1220                                            ccs_io_printf(head,
1221                                                          "exec.envp[\"%s\"]%s",
1222                                                          envp->name->name,
1223                                                          envp->is_not ?
1224                                                          "!=" : "=");
1225                                            if (envp->value) {
1226                                                    ccs_set_string(head, "\"");
1227                                                    ccs_set_string(head, envp->
1228                                                                   value->name);
1229                                                    ccs_set_string(head, "\"");
1230                                            } else {
1231                                                    ccs_set_string(head, "NULL");
1232                                            }
1233                                            envp++;
1234                                            continue;
1235                                    case CCS_NUMBER_UNION:
1236                                            ccs_print_number_union(head,
1237                                                                   numbers_p++);
1238                                            break;
1239                                    default:
1240                                            ccs_set_string(head,
1241                                                   ccs_condition_keyword[left]);
1242                                            break;
1243                                    }
1244                                    ccs_set_string(head, match ? "=" : "!=");
1245                                    switch (right) {
1246                                    case CCS_NAME_UNION:
1247                                            ccs_print_name_union(head, names_p++);
1248                                            break;
1249                                    case CCS_NUMBER_UNION:
1250                                            ccs_print_number_union(head,
1251                                                                   numbers_p++);
1252                                            break;
1253                                    default:
1254                                            ccs_set_string(head,
1255                                                   ccs_condition_keyword[right]);
1256                                            break;
1257                                    }
1258                          }                          }
                         envp++;  
                         continue;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (left >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, " %s",  
                                            ccs_condition_keyword[left]))  
                                 goto out;  
                         break;  
1259                  }                  }
1260                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1261                          goto out;                  /* fall through */
1262                  switch (right) {          case 2:
1263                  case CCS_NAME_UNION:                  if (!ccs_flush(head))
                         if (!ccs_print_name_union_quoted(head, names_p++))  
                                 goto out;  
                         break;  
                 case CCS_NUMBER_UNION:  
                         if (!ccs_print_number_union_nospace(head, numbers_p++))  
                                 goto out;  
                         break;  
                 default:  
                         if (right >= CCS_MAX_CONDITION_KEYWORD)  
                                 goto out;  
                         if (!ccs_io_printf(head, "%s",  
                                            ccs_condition_keyword[right]))  
                                 goto out;  
1264                          break;                          break;
1265                    head->r.cond_step++;
1266                    /* fall through */
1267            case 3:
1268                    {
1269                            u8 j;
1270                            const u8 i = cond->post_state[3];
1271                            if (i)
1272                                    ccs_set_string(head, " ; set");
1273                            for (j = 0; j < 3; j++)
1274                                    if ((i & (1 << j)))
1275                                            ccs_io_printf(head,
1276                                                          " task.state[%u]=%u", j,
1277                                                          cond->post_state[j]);
1278                            if (i & (1 << 4))
1279                                    ccs_io_printf(head, " audit=%s",
1280                                                  ccs_yesno(cond->post_state[4]));
1281                  }                  }
1282          }                  ccs_set_lf(head);
         i = cond->post_state[3];  
         if (!i)  
                 goto no_condition;  
         if (!ccs_io_printf(head, " ; set"))  
                 goto out;  
         for (j = 0; j < 3; j++) {  
                 if (!(i & (1 << j)))  
                         continue;  
                 if (!ccs_io_printf(head, " task.state[%u]=%u", j,  
                                    cond->post_state[j]))  
                         goto out;  
         }  
         if (i & (1 << 4)) {  
                 if (!ccs_io_printf(head, " audit=%s",  
                                    ccs_yesno(cond->post_state[4])))  
                         goto out;  
         }  
  no_condition:  
         if (ccs_io_printf(head, "\n"))  
1283                  return true;                  return true;
  out:  
         return false;  
 }  
   
 /**  
  * ccs_print_path_acl - Print a path ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_acl(struct ccs_io_buffer *head,  
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE  
                     && bit != CCS_TYPE_TRANSIT)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)  
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
 }  
   
 /**  
  * ccs_print_path_number3_acl - Print a path_number3 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_number3_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  
                                        struct ccs_path_number3_acl *ptr,  
                                        const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number32keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->mode) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1284          }          }
1285          head->read_bit = 0;          return false;
         return true;  
1286  }  }
1287    
1288  /**  /**
1289   * ccs_print_path2_acl - Print a path2 ACL entry.   * ccs_fns - Find next set bit.
1290   *   *
1291   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1292   * @ptr:  Pointer to "struct ccs_path2_acl".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1293   *   *
1294   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1295   */   */
1296  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
1297  {  {
1298          int pos;          for ( ; bit < 8; bit++)
1299          u8 bit;                  if (perm & (1 << bit))
1300          const u8 perm = ptr->perm;                          break;
1301          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {          return bit;
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name1) ||  
                     !ccs_print_name_union(head, &ptr->name2) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1302  }  }
1303    
1304  /**  /**
1305   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1306   *   *
1307   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1308   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1309   *   *
1310   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1311   */   */
1312  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1313                                        struct ccs_path_number_acl *ptr,                              const struct ccs_acl_info *acl)
                                       const struct ccs_condition *cond)  
1314  {  {
1315          int pos;          const u8 acl_type = acl->type;
1316          u8 bit;          u8 bit;
1317          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1318          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1319               bit++) {          if (acl->is_deleted)
1320                  if (!(perm & (1 << bit)))                  return true;
1321                          continue;   next:
1322                  pos = head->read_avail;          bit = head->r.bit;
1323                  if (!ccs_io_printf(head, "allow_%s",          if (!ccs_flush(head))
                                    ccs_path_number2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->number) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
 }  
   
 /**  
  * ccs_print_env_acl - Print an evironment variable name's ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_env_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_env_acl(struct ccs_io_buffer *head,  
                               struct ccs_env_acl *ptr,  
                               const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_ENV "%s", ptr->env->name) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_capability_acl - Print a capability ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_capability_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_capability_acl(struct ccs_io_buffer *head,  
                                      struct ccs_capability_acl *ptr,  
                                      const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_CAPABILITY "%s",  
                            ccs_cap2keyword(ptr->operation)) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
1324                  return false;                  return false;
1325          }          else if (acl_type == CCS_TYPE_PATH_ACL) {
1326          return true;                  struct ccs_path_acl *ptr
1327  }                          = container_of(acl, typeof(*ptr), head);
1328                    const u16 perm = ptr->perm;
1329  /**                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1330   * ccs_print_network_acl - Print a network ACL entry.                          if (!(perm & (1 << bit)))
1331   *                                  continue;
1332   * @head: Pointer to "struct ccs_io_buffer".                          if (head->r.print_execute_only &&
1333   * @ptr:  Pointer to "struct ccs_ip_network_acl".                              bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1334   * @cond: Pointer to "struct ccs_condition". May be NULL.                                  continue;
1335   *                          /* Print "read/write" instead of "read" and "write". */
1336   * Returns true on success, false otherwise.                          if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)
1337   */                              && (perm & (1 << CCS_TYPE_READ_WRITE)))
1338  static bool ccs_print_network_acl(struct ccs_io_buffer *head,                                  continue;
1339                                    struct ccs_ip_network_acl *ptr,                          break;
1340                                    const struct ccs_condition *cond)                  }
1341  {                  if (bit >= CCS_MAX_PATH_OPERATION)
1342          int pos;                          goto done;
1343          u8 bit;                  ccs_io_printf(head, "allow_%s", ccs_path_keyword[bit]);
1344          const u8 perm = ptr->perm;                  ccs_print_name_union(head, &ptr->name);
1345          char buf[128];          } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1346          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1347                  const char *w[2] = { "", "" };                  struct ccs_execute_handler *ptr
1348                  if (!(perm & (1 << bit)))                          = container_of(acl, typeof(*ptr), head);
1349                          continue;                  ccs_io_printf(head, "%s ",
1350                  pos = head->read_avail;                                acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1351                                  CCS_KEYWORD_EXECUTE_HANDLER :
1352                                  CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1353                    ccs_set_string(head, ptr->handler->name);
1354            } else if (head->r.print_execute_only) {
1355                    return true;
1356            } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1357                    struct ccs_mkdev_acl *ptr =
1358                            container_of(acl, typeof(*ptr), head);
1359                    bit = ccs_fns(ptr->perm, bit);
1360                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1361                            goto done;
1362                    ccs_io_printf(head, "allow_%s", ccs_mkdev_keyword[bit]);
1363                    ccs_print_name_union(head, &ptr->name);
1364                    ccs_print_number_union(head, &ptr->mode);
1365                    ccs_print_number_union(head, &ptr->major);
1366                    ccs_print_number_union(head, &ptr->minor);
1367            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1368                    struct ccs_path2_acl *ptr =
1369                            container_of(acl, typeof(*ptr), head);
1370                    bit = ccs_fns(ptr->perm, bit);
1371                    if (bit >= CCS_MAX_PATH2_OPERATION)
1372                            goto done;
1373                    ccs_io_printf(head, "allow_%s", ccs_path2_keyword[bit]);
1374                    ccs_print_name_union(head, &ptr->name1);
1375                    ccs_print_name_union(head, &ptr->name2);
1376            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1377                    struct ccs_path_number_acl *ptr =
1378                            container_of(acl, typeof(*ptr), head);
1379                    bit = ccs_fns(ptr->perm, bit);
1380                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1381                            goto done;
1382                    ccs_io_printf(head, "allow_%s",
1383                                  ccs_path_number_keyword[bit]);
1384                    ccs_print_name_union(head, &ptr->name);
1385                    ccs_print_number_union(head, &ptr->number);
1386            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1387                    struct ccs_env_acl *ptr =
1388                            container_of(acl, typeof(*ptr), head);
1389                    ccs_set_string(head, CCS_KEYWORD_ALLOW_ENV);
1390                    ccs_set_string(head, ptr->env->name);
1391            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1392                    struct ccs_capability_acl *ptr =
1393                            container_of(acl, typeof(*ptr), head);
1394                    ccs_set_string(head, CCS_KEYWORD_ALLOW_CAPABILITY);
1395                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1396            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1397                    struct ccs_ip_network_acl *ptr =
1398                            container_of(acl, typeof(*ptr), head);
1399                    bit = ccs_fns(ptr->perm, bit);
1400                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1401                            goto done;
1402                    ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",
1403                                  ccs_net_keyword[bit]);
1404                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1405                            char buf[128];
1406                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1407                          w[0] = "@";                          ccs_set_string(head, "@");
1408                          w[1] = ptr->address.group->group_name->name;                          ccs_set_string(head,
1409                                           ptr->address.group->group_name->name);
1410                          break;                          break;
1411                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1412                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1413                                         ptr->address.ipv4.max);                                         ptr->address.ipv4.max);
1414                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1415                          break;                          break;
1416                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1417                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1418                                         ptr->address.ipv6.max);                                         ptr->address.ipv6.max);
1419                          w[0] = buf;                          ccs_io_printf(head, "%s", buf);
1420                          break;                          break;
1421                  }                  }
1422                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s %s%s",                  ccs_print_number_union(head, &ptr->port);
1423                                     ccs_net2keyword(bit), w[0], w[1]) ||          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1424                      !ccs_print_number_union(head, &ptr->port) ||                  struct ccs_signal_acl *ptr =
1425                      !ccs_print_condition(head, cond))                          container_of(acl, typeof(*ptr), head);
1426                          goto out;                  ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u ", ptr->sig);
1427                    ccs_set_string(head, ptr->domainname->name);
1428            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1429                    struct ccs_mount_acl *ptr =
1430                            container_of(acl, typeof(*ptr), head);
1431                    ccs_io_printf(head, "allow_mount");
1432                    ccs_print_name_union(head, &ptr->dev_name);
1433                    ccs_print_name_union(head, &ptr->dir_name);
1434                    ccs_print_name_union(head, &ptr->fs_type);
1435                    ccs_print_number_union(head, &ptr->flags);
1436            }
1437            head->r.bit = bit + 1;
1438            if (acl->cond) {
1439                    head->r.print_cond_part = true;
1440                    head->r.cond_step = 0;
1441                    if (!ccs_flush(head))
1442                            return false;
1443     print_cond_part:
1444                    if (!ccs_print_condition(head, acl->cond))
1445                            return false;
1446                    head->r.print_cond_part = false;
1447            } else {
1448                    ccs_set_lf(head);
1449          }          }
1450          head->read_bit = 0;          switch (acl_type) {
1451          return true;          case CCS_TYPE_PATH_ACL:
1452   out:          case CCS_TYPE_MKDEV_ACL:
1453          head->read_bit = bit;          case CCS_TYPE_PATH2_ACL:
1454          head->read_avail = pos;          case CCS_TYPE_PATH_NUMBER_ACL:
1455          return false;          case CCS_TYPE_IP_NETWORK_ACL:
1456  }                  goto next;
   
 /**  
  * ccs_print_signal_acl - Print a signal ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct signale_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_signal_acl(struct ccs_io_buffer *head,  
                                  struct ccs_signal_acl *ptr,  
                                  const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_SIGNAL "%u %s",  
                            ptr->sig, ptr->domainname->name) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
1457          }          }
1458     done:
1459            head->r.bit = 0;
1460          return true;          return true;
1461  }  }
1462    
1463  /**  /**
  * ccs_print_execute_handler - Print an execute handler ACL entry.  
  *  
  * @head:    Pointer to "struct ccs_io_buffer".  
  * @keyword: Name of the keyword.  
  * @ptr:     Pointer to "struct ccs_execute_handler".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_execute_handler(struct ccs_io_buffer *head,  
                                       const char *keyword,  
                                       struct ccs_execute_handler *ptr)  
 {  
         return ccs_io_printf(head, "%s %s\n", keyword, ptr->handler->name);  
 }  
   
 /**  
  * ccs_print_mount_acl - Print a mount ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  
                                 struct ccs_mount_acl *ptr,  
                                 const struct ccs_condition *cond)  
 {  
         const int pos = head->read_avail;  
         if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||  
             !ccs_print_name_union(head, &ptr->dev_name) ||  
             !ccs_print_name_union(head, &ptr->dir_name) ||  
             !ccs_print_name_union(head, &ptr->fs_type) ||  
             !ccs_print_number_union(head, &ptr->flags) ||  
             !ccs_print_condition(head, cond)) {  
                 head->read_avail = pos;  
                 return false;  
         }  
         return true;  
 }  
   
 /**  
  * ccs_print_entry - Print an ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to an ACL entry.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_entry(struct ccs_io_buffer *head,  
                             struct ccs_acl_info *ptr)  
 {  
         const struct ccs_condition *cond = ptr->cond;  
         const u8 acl_type = ptr->type;  
         if (ptr->is_deleted)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_ACL) {  
                 struct ccs_path_acl *acl  
                         = container_of(ptr, struct ccs_path_acl, head);  
                 return ccs_print_path_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||  
             acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {  
                 struct ccs_execute_handler *acl  
                         = container_of(ptr, struct ccs_execute_handler, head);  
                 const char *keyword = acl_type == CCS_TYPE_EXECUTE_HANDLER ?  
                         CCS_KEYWORD_EXECUTE_HANDLER :  
                         CCS_KEYWORD_DENIED_EXECUTE_HANDLER;  
                 return ccs_print_execute_handler(head, keyword, acl);  
         }  
         if (head->read_execute_only)  
                 return true;  
         if (acl_type == CCS_TYPE_PATH_NUMBER3_ACL) {  
                 struct ccs_path_number3_acl *acl  
                         = container_of(ptr, struct ccs_path_number3_acl, head);  
                 return ccs_print_path_number3_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH2_ACL) {  
                 struct ccs_path2_acl *acl  
                         = container_of(ptr, struct ccs_path2_acl, head);  
                 return ccs_print_path2_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {  
                 struct ccs_path_number_acl *acl  
                         = container_of(ptr, struct ccs_path_number_acl, head);  
                 return ccs_print_path_number_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_ENV_ACL) {  
                 struct ccs_env_acl *acl  
                         = container_of(ptr, struct ccs_env_acl, head);  
                 return ccs_print_env_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_CAPABILITY_ACL) {  
                 struct ccs_capability_acl *acl  
                         = container_of(ptr, struct ccs_capability_acl, head);  
                 return ccs_print_capability_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {  
                 struct ccs_ip_network_acl *acl  
                         = container_of(ptr, struct ccs_ip_network_acl, head);  
                 return ccs_print_network_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_SIGNAL_ACL) {  
                 struct ccs_signal_acl *acl  
                         = container_of(ptr, struct ccs_signal_acl, head);  
                 return ccs_print_signal_acl(head, acl, cond);  
         }  
         if (acl_type == CCS_TYPE_MOUNT_ACL) {  
                 struct ccs_mount_acl *acl  
                         = container_of(ptr, struct ccs_mount_acl, head);  
                 return ccs_print_mount_acl(head, acl, cond);  
         }  
         BUG(); /* This must not happen. */  
         return false;  
 }  
   
 /**  
1464   * ccs_read_domain2 - Read domain policy.   * ccs_read_domain2 - Read domain policy.
1465   *   *
1466   * @head:   Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
# Line 1602  static bool ccs_print_entry(struct ccs_i Line 1473  static bool ccs_print_entry(struct ccs_i
1473  static bool ccs_read_domain2(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1474                               struct ccs_domain_info *domain)                               struct ccs_domain_info *domain)
1475  {  {
1476          struct list_head *pos;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1477          /* Print ACL entries in the domain. */                  struct ccs_acl_info *ptr =
1478          list_for_each_cookie(pos, head->read_var2, &domain->acl_info_list) {                          list_entry(head->r.acl, typeof(*ptr), list);
                 struct ccs_acl_info *ptr  
                         = list_entry(pos, struct ccs_acl_info, list);  
1479                  if (!ccs_print_entry(head, ptr))                  if (!ccs_print_entry(head, ptr))
1480                          return false;                          return false;
1481          }          }
1482            head->r.acl = NULL;
1483          return true;          return true;
1484  }  }
1485    
# Line 1622  static bool ccs_read_domain2(struct ccs_ Line 1492  static bool ccs_read_domain2(struct ccs_
1492   */   */
1493  static void ccs_read_domain(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1494  {  {
1495          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1496                  return;                  return;
1497          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1498                  struct ccs_domain_info *domain =                  struct ccs_domain_info *domain =
1499                          list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1500                  const char *w[5] = { "", "", "", "", "" };                  switch (head->r.step) {
1501                  switch (head->read_step) {                          u8 i;
1502                  case 0:                  case 0:
1503                          if (domain->is_deleted && !head->read_single_domain)                          if (domain->is_deleted &&
1504                                !head->r.print_this_domain_only)
1505                                  continue;                                  continue;
1506                          /* Print domainname and flags. */                          /* Print domainname and flags. */
1507                          if (domain->quota_warned)                          ccs_set_string(head, domain->domainname->name);
1508                                  w[0] = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                          ccs_set_lf(head);
1509                          if (domain->domain_transition_failed)                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1510                                  w[1] = CCS_KEYWORD_TRANSITION_FAILED "\n";                                        domain->profile);
1511                          if (domain->ignore_global)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1512                                  w[2] = CCS_KEYWORD_IGNORE_GLOBAL "\n";                                  if (domain->flags[i])
1513                          if (domain->ignore_global_allow_read)                                          ccs_set_string(head, ccs_dif[i]);
1514                                  w[3] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ                          head->r.step++;
1515                                          "\n";                          ccs_set_lf(head);
                         if (domain->ignore_global_allow_env)  
                                 w[4] = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV  
                                         "\n";  
                         if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE  
                                            "%u\n%s%s%s%s%s\n",  
                                            domain->domainname->name,  
                                            domain->profile, w[0], w[1], w[2],  
                                            w[3], w[4]))  
                                 return;  
                         head->read_step++;  
1516                          /* fall through */                          /* fall through */
1517                  case 1:                  case 1:
1518                          if (!ccs_read_domain2(head, domain))                          if (!ccs_read_domain2(head, domain))
1519                                  return;                                  return;
1520                          head->read_step++;                          head->r.step++;
1521                            if (!ccs_set_lf(head))
1522                                    return;
1523                          /* fall through */                          /* fall through */
1524                  case 2:                  case 2:
1525                          if (!ccs_io_printf(head, "\n"))                          head->r.step = 0;
1526                                  return;                          if (head->r.print_this_domain_only)
1527                          head->read_step = 0;                                  goto done;
                         if (head->read_single_domain)  
                                 goto out;  
1528                  }                  }
1529          }          }
1530   out:   done:
1531          head->read_eof = true;          head->r.eof = true;
1532  }  }
1533    
1534  /**  /**
# Line 1719  static int ccs_write_domain_profile(stru Line 1579  static int ccs_write_domain_profile(stru
1579   */   */
1580  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1581  {  {
1582          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1583                  return;                  return;
1584          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1585                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1586                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1587                  if (domain->is_deleted)                  if (domain->is_deleted)
1588                          continue;                          continue;
1589                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1590                          return;                          return;
1591                    ccs_io_printf(head, "%u ", domain->profile);
1592                    ccs_set_string(head, domain->domainname->name);
1593                    ccs_set_lf(head);
1594          }          }
1595          head->read_eof = true;          head->r.eof = true;
1596  }  }
1597    
1598  /**  /**
# Line 1743  static void ccs_read_domain_profile(stru Line 1604  static void ccs_read_domain_profile(stru
1604   */   */
1605  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1606  {  {
1607          head->read_eof = false;          head->r.eof = false;
1608          return 0;          return 0;
1609  }  }
1610    
# Line 1769  static void ccs_read_pid(struct ccs_io_b Line 1630  static void ccs_read_pid(struct ccs_io_b
1630          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1631          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1632          if (!buf) {          if (!buf) {
1633                  head->read_eof = true;                  head->r.eof = true;
1634                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1635          }          }
1636          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1637                  return;                  return;
1638          head->read_eof = true;          head->r.eof = true;
1639          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1640                  task_info = true;                  task_info = true;
1641          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1796  static void ccs_read_pid(struct ccs_io_b Line 1657  static void ccs_read_pid(struct ccs_io_b
1657          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1658          if (!domain)          if (!domain)
1659                  return;                  return;
1660          if (!task_info)          if (!task_info) {
1661                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1662                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1663          else          } else {
1664                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1665                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1666                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
# Line 1809  static void ccs_read_pid(struct ccs_io_b Line 1670  static void ccs_read_pid(struct ccs_io_b
1670                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1671                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1672                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1673            }
1674  }  }
1675    
1676    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1677            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1678            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1679            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1680            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1681            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1682    };
1683    
1684    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1685            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1686            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1687            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1688    };
1689    
1690  /**  /**
1691   * ccs_write_exception - Write exception policy.   * ccs_write_exception - Write exception policy.
1692   *   *
# Line 1825  static int ccs_write_exception(struct cc Line 1701  static int ccs_write_exception(struct cc
1701          u8 i;          u8 i;
1702          static const struct {          static const struct {
1703                  const char *keyword;                  const char *keyword;
1704                  int (*write) (char *, const bool, const u8);                  int (*write) (char *, const bool);
1705          } ccs_callback[8] = {          } ccs_callback[4] = {
                 { CCS_KEYWORD_NO_KEEP_DOMAIN, ccs_write_domain_keeper },  
                 { CCS_KEYWORD_NO_INITIALIZE_DOMAIN,  
                   ccs_write_domain_initializer },  
                 { CCS_KEYWORD_KEEP_DOMAIN, ccs_write_domain_keeper },  
                 { CCS_KEYWORD_INITIALIZE_DOMAIN,  
                   ccs_write_domain_initializer },  
1706                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1707                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1708                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },                  { CCS_KEYWORD_DENY_REWRITE, ccs_write_no_rewrite },
1709                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1710          };          };
1711          static const char *ccs_name[CCS_MAX_GROUP] = {          for (i = 0; i < 4; i++) {
                 [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,  
                 [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,  
                 [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP  
         };  
         for (i = 0; i < 8; i++) {  
1712                  if (ccs_str_starts(&data, ccs_callback[i].keyword))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1713                          return ccs_callback[i].write(data, is_delete, i < 2);                          return ccs_callback[i].write(data, is_delete);
1714            }
1715            for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) {
1716                    if (ccs_str_starts(&data, ccs_transition_type[i]))
1717                            return ccs_write_transition_control(data, is_delete,
1718                                                                i);
1719          }          }
1720          for (i = 0; i < CCS_MAX_GROUP; i++) {          for (i = 0; i < CCS_MAX_GROUP; i++) {
1721                  if (ccs_str_starts(&data, ccs_name[i]))                  if (ccs_str_starts(&data, ccs_group_name[i]))
1722                          return ccs_write_group(data, is_delete, i);                          return ccs_write_group(data, is_delete, i);
1723          }          }
1724          {          return ccs_write_domain2(data, &ccs_global_domain, is_delete);
                 int error;  
                 struct ccs_condition *cond = NULL;  
                 char *cp = ccs_find_condition_part(data);  
                 if (cp) {  
                         cond = ccs_get_condition(cp);  
                         if (!cond)  
                                 return -EINVAL;  
                 }  
                 error = ccs_write_domain2(data, &ccs_global_domain, cond,  
                                           is_delete);  
                 if (cond)  
                         ccs_put_condition(cond);  
                 return error;  
         }  
1725  }  }
1726    
1727  /**  /**
# Line 1880  static int ccs_write_exception(struct cc Line 1736  static int ccs_write_exception(struct cc
1736   */   */
1737  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1738  {  {
1739          struct list_head *gpos;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
         struct list_head *mpos;  
         const char *w[3] = { "", "", "" };  
         if (idx == CCS_PATH_GROUP)  
                 w[0] = CCS_KEYWORD_PATH_GROUP;  
         else if (idx == CCS_NUMBER_GROUP)  
                 w[0] = CCS_KEYWORD_NUMBER_GROUP;  
         else if (idx == CCS_ADDRESS_GROUP)  
                 w[0] = CCS_KEYWORD_ADDRESS_GROUP;  
         list_for_each_cookie(gpos, head->read_var1, &ccs_group_list[idx]) {  
1740                  struct ccs_group *group =                  struct ccs_group *group =
1741                          list_entry(gpos, struct ccs_group, head.list);                          list_entry(head->r.group, typeof(*group), head.list);
1742                  w[1] = group->group_name->name;                  list_for_each_cookie(head->r.acl, &group->member_list) {
                 list_for_each_cookie(mpos, head->read_var2,  
                                      &group->member_list) {  
                         char buffer[128];  
1743                          struct ccs_acl_head *ptr =                          struct ccs_acl_head *ptr =
1744                                  list_entry(mpos, struct ccs_acl_head, list);                                  list_entry(head->r.acl, typeof(*ptr), list);
1745                          if (ptr->is_deleted)                          if (ptr->is_deleted)
1746                                  continue;                                  continue;
1747                            if (!ccs_flush(head))
1748                                    return false;
1749                            ccs_set_string(head, ccs_group_name[idx]);
1750                            ccs_set_string(head, group->group_name->name);
1751                          if (idx == CCS_PATH_GROUP) {                          if (idx == CCS_PATH_GROUP) {
1752                                  w[2] = container_of(ptr, struct ccs_path_group,                                  ccs_set_space(head);
1753                                                      head)->member_name->name;                                  ccs_set_string(head, container_of
1754                                                   (ptr, struct ccs_path_group,
1755                                                    head)->member_name->name);
1756                          } else if (idx == CCS_NUMBER_GROUP) {                          } else if (idx == CCS_NUMBER_GROUP) {
1757                                  w[2] = buffer;                                  ccs_print_number_union(head, &container_of
1758                                  ccs_print_number(buffer, sizeof(buffer),                                                         (ptr, struct ccs_number_group,
1759                                                   &container_of                                                          head)->number);
                                                  (ptr, struct ccs_number_group,  
                                                   head)->number);  
1760                          } else if (idx == CCS_ADDRESS_GROUP) {                          } else if (idx == CCS_ADDRESS_GROUP) {
1761                                    char buffer[128];
1762                                  struct ccs_address_group *member =                                  struct ccs_address_group *member =
1763                                          container_of(ptr, typeof(*member),                                          container_of(ptr, typeof(*member),
1764                                                       head);                                                       head);
                                 w[2] = buffer;  
1765                                  if (member->is_ipv6)                                  if (member->is_ipv6)
1766                                          ccs_print_ipv6(buffer, sizeof(buffer),                                          ccs_print_ipv6(buffer, sizeof(buffer),
1767                                                         member->min.ipv6,                                                         member->min.ipv6,
# Line 1922  static bool ccs_read_group(struct ccs_io Line 1770  static bool ccs_read_group(struct ccs_io
1770                                          ccs_print_ipv4(buffer, sizeof(buffer),                                          ccs_print_ipv4(buffer, sizeof(buffer),
1771                                                         member->min.ipv4,                                                         member->min.ipv4,
1772                                                         member->max.ipv4);                                                         member->max.ipv4);
1773                                    ccs_io_printf(head, " %s", buffer);
1774                          }                          }
1775                          if (!ccs_io_printf(head, "%s%s %s\n", w[0], w[1],                          ccs_set_lf(head);
                                            w[2]))  
                                 return false;  
1776                  }                  }
1777                    head->r.acl = NULL;
1778          }          }
1779            head->r.group = NULL;
1780          return true;          return true;
1781  }  }
1782    
# Line 1943  static bool ccs_read_group(struct ccs_io Line 1792  static bool ccs_read_group(struct ccs_io
1792   */   */
1793  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)  static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
1794  {  {
1795          struct list_head *pos;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1796          list_for_each_cookie(pos, head->read_var2, &ccs_policy_list[idx]) {                  struct ccs_acl_head *acl =
1797                  const char *w[4] = { "", "", "", "" };                          container_of(head->r.acl, typeof(*acl), list);
                 char buffer[16];  
                 struct ccs_acl_head *acl = container_of(pos, typeof(*acl),  
                                                         list);  
1798                  if (acl->is_deleted)                  if (acl->is_deleted)
1799                          continue;                          continue;
1800                    if (!ccs_flush(head))
1801                            return false;
1802                  switch (idx) {                  switch (idx) {
1803                  case CCS_ID_DOMAIN_KEEPER:                  case CCS_ID_TRANSITION_CONTROL:
1804                          {                          {
1805                                  struct ccs_domain_keeper *ptr =                                  struct ccs_transition_control *ptr =
1806                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1807                                  w[0] = ptr->is_not ?                                  ccs_set_string(head,
1808                                          CCS_KEYWORD_NO_KEEP_DOMAIN :                                                 ccs_transition_type[ptr->type]);
1809                                          CCS_KEYWORD_KEEP_DOMAIN;                                  ccs_set_string(head, ptr->program ?
1810                                  if (ptr->program) {                                                 ptr->program->name : "any");
1811                                          w[1] = ptr->program->name;                                  ccs_set_string(head, " from ");
1812                                          w[2] = " from ";                                  ccs_set_string(head, ptr->domainname ?
1813                                  }                                                 ptr->domainname->name : "any");
                                 w[3] = ptr->domainname->name;  
                         }  
                         break;  
                 case CCS_ID_DOMAIN_INITIALIZER:  
                         {  
                                 struct ccs_domain_initializer *ptr =  
                                         container_of(acl, typeof(*ptr), head);  
                                 w[0] = ptr->is_not ?  
                                         CCS_KEYWORD_NO_INITIALIZE_DOMAIN :  
                                         CCS_KEYWORD_INITIALIZE_DOMAIN;  
                                 w[1] = ptr->program->name;  
                                 if (ptr->domainname) {  
                                         w[2] = " from ";  
                                         w[3] = ptr->domainname->name;  
                                 }  
1814                          }                          }
1815                          break;                          break;
1816                  case CCS_ID_AGGREGATOR:                  case CCS_ID_AGGREGATOR:
1817                          {                          {
1818                                  struct ccs_aggregator *ptr =                                  struct ccs_aggregator *ptr =
1819                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1820                                  w[0] = CCS_KEYWORD_AGGREGATOR;                                  ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1821                                  w[1] = ptr->original_name->name;                                  ccs_set_string(head, ptr->original_name->name);
1822                                  w[2] = " ";                                  ccs_set_space(head);
1823                                  w[3] = ptr->aggregated_name->name;                                  ccs_set_string(head,
1824                                                   ptr->aggregated_name->name);
1825                          }                          }
1826                          break;                          break;
1827                  case CCS_ID_PATTERN:                  case CCS_ID_PATTERN:
1828                          {                          {
1829                                  struct ccs_pattern *ptr =                                  struct ccs_pattern *ptr =
1830                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1831                                  w[0] = CCS_KEYWORD_FILE_PATTERN;                                  ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1832                                  w[1] = ptr->pattern->name;                                  ccs_set_string(head, ptr->pattern->name);
1833                          }                          }
1834                          break;                          break;
1835                  case CCS_ID_NO_REWRITE:                  case CCS_ID_NO_REWRITE:
1836                          {                          {
1837                                  struct ccs_no_rewrite *ptr =                                  struct ccs_no_rewrite *ptr =
1838                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1839                                  w[0] = CCS_KEYWORD_DENY_REWRITE;                                  ccs_set_string(head, CCS_KEYWORD_DENY_REWRITE);
1840                                  w[1] = ptr->pattern->name;                                  ccs_set_string(head, ptr->pattern->name);
1841                          }                          }
1842                          break;                          break;
1843                  case CCS_ID_RESERVEDPORT:                  case CCS_ID_RESERVEDPORT:
# Line 2012  static bool ccs_read_policy(struct ccs_i Line 1846  static bool ccs_read_policy(struct ccs_i
1846                                          container_of(acl, typeof(*ptr), head);                                          container_of(acl, typeof(*ptr), head);
1847                                  const u16 min_port = ptr->min_port;                                  const u16 min_port = ptr->min_port;
1848                                  const u16 max_port = ptr->max_port;                                  const u16 max_port = ptr->max_port;
1849                                  w[0] = CCS_KEYWORD_DENY_AUTOBIND;                                  ccs_set_string(head,
1850                                  snprintf(buffer, sizeof(buffer) - 1, "%u%c%u",                                                 CCS_KEYWORD_DENY_AUTOBIND);
1851                                           min_port, min_port != max_port ?                                  ccs_io_printf(head, "%u", min_port);
1852                                           '-' : '\0', max_port);                                  if (min_port != max_port)
1853                                  buffer[sizeof(buffer) - 1] = '\0';                                          ccs_io_printf(head, "-%u", max_port);
                                 w[1] = buffer;  
1854                          }                          }
1855                          break;                          break;
1856                  default:                  default:
1857                          continue;                          continue;
1858                  }                  }
1859                  if (!ccs_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2], w[3]))                  ccs_set_lf(head);
                         return false;  
1860          }          }
1861            head->r.acl = NULL;
1862          return true;          return true;
1863  }  }
1864    
 static void ccs_read_global_domain(struct ccs_io_buffer *head)  
 {  
         if (!head->read_eof)  
                 head->read_eof = ccs_read_domain2(head, &ccs_global_domain);  
 }  
   
1865  /**  /**
1866   * ccs_read_exception - Read exception policy.   * ccs_read_exception - Read exception policy.
1867   *   *
# Line 2044  static void ccs_read_global_domain(struc Line 1871  static void ccs_read_global_domain(struc
1871   */   */
1872  static void ccs_read_exception(struct ccs_io_buffer *head)  static void ccs_read_exception(struct ccs_io_buffer *head)
1873  {  {
1874          if (head->read_eof)          if (head->r.eof)
1875                  return;                  return;
1876          while (head->read_step < CCS_MAX_POLICY &&          while (head->r.step < CCS_MAX_POLICY &&
1877                 ccs_read_policy(head, head->read_step))                 ccs_read_policy(head, head->r.step))
1878                  head->read_step++;                  head->r.step++;
1879          if (head->read_step < CCS_MAX_POLICY)          if (head->r.step < CCS_MAX_POLICY)
1880                  return;                  return;
1881          while (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP &&          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1882                 ccs_read_group(head, head->read_step - CCS_MAX_POLICY))                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1883                  head->read_step++;                  head->r.step++;
1884          if (head->read_step < CCS_MAX_POLICY + CCS_MAX_GROUP)          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1885                  return;                  return;
1886          head->read = ccs_read_global_domain;          head->r.eof = ccs_read_domain2(head, &ccs_global_domain);
 }  
   
 /**  
  * ccs_get_argv0 - Get argv[0].  
  *  
  * @ee: Pointer to "struct ccs_execve".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_get_argv0(struct ccs_execve *ee)  
 {  
         struct linux_binprm *bprm = ee->bprm;  
         char *arg_ptr = ee->tmp;  
         int arg_len = 0;  
         unsigned long pos = bprm->p;  
         int offset = pos % PAGE_SIZE;  
         bool done = false;  
         if (!bprm->argc)  
                 goto out;  
         while (1) {  
                 if (!ccs_dump_page(bprm, pos, &ee->dump))  
                         goto out;  
                 pos += PAGE_SIZE - offset;  
                 /* Read. */  
                 while (offset < PAGE_SIZE) {  
                         const char *kaddr = ee->dump.data;  
                         const unsigned char c = kaddr[offset++];  
                         if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {  
                                 if (c == '\\') {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = '\\';  
                                 } else if (c > ' ' && c < 127) {  
                                         arg_ptr[arg_len++] = c;  
                                 } else {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = (c >> 6) + '0';  
                                         arg_ptr[arg_len++]  
                                                 = ((c >> 3) & 7) + '0';  
                                         arg_ptr[arg_len++] = (c & 7) + '0';  
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
                         }  
                 }  
                 offset = 0;  
                 if (done)  
                         break;  
         }  
         return true;  
  out:  
         return false;  
 }  
   
 /**  
  * ccs_get_execute_condition - Get condition part for execute requests.  
  *  
  * @ee: Pointer to "struct ccs_execve".  
  *  
  * Returns pointer to "struct ccs_condition" on success, NULL otherwise.  
  */  
 static struct ccs_condition *ccs_get_execute_condition(struct ccs_execve *ee)  
 {  
         struct ccs_condition *cond;  
         char *buf;  
         int len = 256;  
         char *realpath = NULL;  
         char *argv0 = NULL;  
         const struct ccs_profile *profile = ccs_profile(ccs_current_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;  
 }  
   
 /**  
  * ccs_get_symlink_condition - Get condition part for symlink requests.  
  *  
  * @r: Pointer to "struct ccs_request_info".  
  *  
  * Returns pointer to "struct ccs_condition" on success, NULL otherwise.  
  */  
 static struct ccs_condition *ccs_get_symlink_condition  
 (const struct ccs_request_info *r)  
 {  
         struct ccs_condition *cond;  
         char *buf;  
         int len = 256;  
         const char *symlink = NULL;  
         const struct ccs_profile *profile = ccs_profile(r->profile);  
         if (profile->learning->learning_symlink_target) {  
                 symlink = r->obj->symlink_target->name;  
                 len += strlen(symlink) + 18;  
         }  
         buf = kmalloc(len, CCS_GFP_FLAGS);  
         if (!buf)  
                 return NULL;  
         snprintf(buf, len - 1, "if");  
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (symlink) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",  
                          symlink);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
1887  }  }
1888    
1889  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2232  static LIST_HEAD(ccs_query_list); Line 1908  static LIST_HEAD(ccs_query_list);
1908  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1909  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1910    
1911    static void ccs_truncate(char *str)
1912    {
1913            while (* (unsigned char *) str > (unsigned char) ' ')
1914                    str++;
1915            *str = '\0';
1916    }
1917    
1918  /**  /**
1919   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1920   *   *
# Line 2254  int ccs_supervisor(struct ccs_request_in Line 1937  int ccs_supervisor(struct ccs_request_in
1937          bool quota_exceeded = false;          bool quota_exceeded = false;
1938          char *header;          char *header;
1939          struct ccs_domain_info * const domain = ccs_current_domain();          struct ccs_domain_info * const domain = ccs_current_domain();
1940          switch (r->mode) {          va_start(args, fmt);
1941            len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1942            va_end(args);
1943            if (r->mode == CCS_CONFIG_LEARNING) {
1944                  char *buffer;                  char *buffer;
1945                  struct ccs_condition *cond;                  char *realpath = NULL;
1946          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1947                    char *symlink = NULL;
1948                    char *handler = NULL;
1949                    const struct ccs_preference *pref;
1950                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1951                          return 0;                          return 0;
1952                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1953                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (!buffer)  
1954                          return 0;                          return 0;
1955                  va_start(args, fmt);                  pref = ccs_profile(r->profile)->learning;
1956                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
                 va_end(args);  
                 ccs_normalize_line(buffer);  
1957                  if (r->param_type == CCS_TYPE_PATH_ACL &&                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1958                      r->param.path.operation == CCS_TYPE_EXECUTE)                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1959                          cond = ccs_get_execute_condition(r->ee);                          if (pref->learning_exec_argv0) {
1960                  else if (r->param_type == CCS_TYPE_PATH_ACL &&                                  argv0 = strstr(header, " argv[]={ \"");
1961                           r->param.path.operation == CCS_TYPE_SYMLINK)                                  if (argv0) {
1962                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1963                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1964                          char str[] = "if task.type=execute_handler";                                  }
1965                          cond = ccs_get_condition(str);                          }
1966                  } else                          if (pref->learning_exec_realpath) {
1967                          cond = NULL;                                  realpath = strstr(header,
1968                  ccs_write_domain2(buffer, domain, cond, false);                                                    " exec={ realpath=\"");
1969                  ccs_put_condition(cond);                                  if (realpath) {
1970                  kfree(buffer);                                          realpath += 8;
1971                  /* fall through */                                          ccs_truncate(realpath);
1972          case CCS_CONFIG_PERMISSIVE:                                  }
1973                            }
1974                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1975                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1976                               pref->learning_symlink_target) {
1977                            symlink = strstr(header, " symlink.target=\"");
1978                            if (symlink)
1979                                    ccs_truncate(symlink + 1);
1980                    }
1981                    handler = strstr(header, "type=execute_handler");
1982                    if (handler)
1983                            ccs_truncate(handler);
1984                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1985                    if (buffer) {
1986                            va_start(args, fmt);
1987                            vsnprintf(buffer, len - 1, fmt, args);
1988                            va_end(args);
1989                            if (handler || realpath || argv0 || symlink) {
1990                                    ccs_addprintf(buffer, len, " if");
1991                                    if (handler)
1992                                            ccs_addprintf(buffer, len, " task.%s",
1993                                                          handler);
1994                                    if (realpath)
1995                                            ccs_addprintf(buffer, len, " exec.%s",
1996                                                          realpath);
1997                                    if (argv0)
1998                                            ccs_addprintf(buffer, len,
1999                                                          " exec.argv[0]=%s",
2000                                                          argv0);
2001                                    if (symlink)
2002                                            ccs_addprintf(buffer, len, "%s",
2003                                                          symlink);
2004                            }
2005                            ccs_normalize_line(buffer);
2006                            ccs_write_domain2(buffer, domain, false);
2007                            kfree(buffer);
2008                    }
2009                    kfree(header);
2010                  return 0;                  return 0;
2011          }          }
2012            if (r->mode != CCS_CONFIG_ENFORCING)
2013                    return 0;
2014          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
2015                  int i;                  int i;
2016                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
# Line 2299  int ccs_supervisor(struct ccs_request_in Line 2022  int ccs_supervisor(struct ccs_request_in
2022                  }                  }
2023                  return -EPERM;                  return -EPERM;
2024          }          }
         va_start(args, fmt);  
         len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;  
         va_end(args);  
2025          header = ccs_init_log(&len, r);          header = ccs_init_log(&len, r);
2026          if (!header)          if (!header)
2027                  goto out;                  goto out;
# Line 2419  static void ccs_read_query(struct ccs_io Line 2139  static void ccs_read_query(struct ccs_io
2139          int pos = 0;          int pos = 0;
2140          int len = 0;          int len = 0;
2141          char *buf;          char *buf;
2142          if (head->read_avail)          if (head->r.w_pos)
2143                  return;                  return;
2144          if (head->read_buf) {          if (head->read_buf) {
2145                  kfree(head->read_buf);                  kfree(head->read_buf);
2146                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2147          }          }
2148          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2149          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
# Line 2432  static void ccs_read_query(struct ccs_io Line 2151  static void ccs_read_query(struct ccs_io
2151                          = list_entry(tmp, struct ccs_query_entry, list);                          = list_entry(tmp, struct ccs_query_entry, list);
2152                  if (ptr->answer)                  if (ptr->answer)
2153                          continue;                          continue;
2154                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2155                          continue;                          continue;
2156                  len = ptr->query_len;                  len = ptr->query_len;
2157                  break;                  break;
2158          }          }
2159          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2160          if (!len) {          if (!len) {
2161                  head->read_step = 0;                  head->r.query_index = 0;
2162                  return;                  return;
2163          }          }
2164          buf = kzalloc(len, CCS_GFP_FLAGS);          buf = kzalloc(len, CCS_GFP_FLAGS);
# Line 2452  static void ccs_read_query(struct ccs_io Line 2171  static void ccs_read_query(struct ccs_io
2171                          = list_entry(tmp, struct ccs_query_entry, list);                          = list_entry(tmp, struct ccs_query_entry, list);
2172                  if (ptr->answer)                  if (ptr->answer)
2173                          continue;                          continue;
2174                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2175                          continue;                          continue;
2176                  /*                  /*
2177                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2464  static void ccs_read_query(struct ccs_io Line 2183  static void ccs_read_query(struct ccs_io
2183          }          }
2184          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2185          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2186                  head->read_buf = buf;                  head->read_buf = buf;
2187                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2188                    head->r.query_index++;
2189          } else {          } else {
2190                  kfree(buf);                  kfree(buf);
2191          }          }
# Line 2516  static int ccs_write_answer(struct ccs_i Line 2234  static int ccs_write_answer(struct ccs_i
2234   */   */
2235  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2236  {  {
2237          if (head->read_eof)          if (head->r.eof)
2238                  return;                  return;
2239          ccs_io_printf(head, "1.7.2");          ccs_set_string(head, "1.7.2");
2240          head->read_eof = true;          head->r.eof = true;
2241  }  }
2242    
2243  /**  /**
# Line 2529  static void ccs_read_version(struct ccs_ Line 2247  static void ccs_read_version(struct ccs_
2247   */   */
2248  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2249  {  {
2250          if (head->read_eof)          if (head->r.eof)
2251                  return;                  return;
2252          /*          /*
2253           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2254           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2255           */           */
2256          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2257          head->read_eof = true;          head->r.eof = true;
2258  }  }
2259    
2260  /**  /**
# Line 2695  int ccs_poll_control(struct file *file, Line 2413  int ccs_poll_control(struct file *file,
2413  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2414                       const int buffer_len)                       const int buffer_len)
2415  {  {
2416          int len = 0;          int len;
2417          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
         char *cp;  
2418          int idx;          int idx;
2419          if (!head->read)          if (!head->read)
2420                  return -ENOSYS;                  return -ENOSYS;
# Line 2705  int ccs_read_control(struct file *file, Line 2422  int ccs_read_control(struct file *file,
2422                  return -EFAULT;                  return -EFAULT;
2423          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2424                  return -EINTR;                  return -EINTR;
2425            head->read_user_buf = buffer;
2426            head->read_user_buf_avail = buffer_len;
2427          idx = ccs_read_lock();          idx = ccs_read_lock();
2428          while (1) {          if (ccs_flush(head))
2429                  /* Call the policy handler. */                  /* Call the policy handler. */
2430                  head->read(head);                  head->read(head);
2431                  /* Write to buffer. */          ccs_flush(head);
                 len = head->read_avail;  
                 if (len || head->poll || head->read_eof)  
                         break;  
                 len = head->readbuf_size * 2;  
                 cp = kzalloc(len, CCS_GFP_FLAGS);  
                 if (!cp) {  
                         len = -ENOMEM;  
                         goto out;  
                 }  
                 kfree(head->read_buf);  
                 head->read_buf = cp;  
                 head->readbuf_size = len;  
         }  
         if (len > buffer_len)  
                 len = buffer_len;  
         if (!len)  
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2432          ccs_read_unlock(idx);          ccs_read_unlock(idx);
2433            len = head->read_user_buf - buffer;
2434          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2435          return len;          return len;
2436  }  }
# Line 2775  int ccs_write_control(struct file *file, Line 2469  int ccs_write_control(struct file *file,
2469          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2470          while (avail_len > 0) {          while (avail_len > 0) {
2471                  char c;                  char c;
2472                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2473                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2474                          char *cp = kzalloc(len, CCS_GFP_FLAGS);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2475                          if (!cp) {                          if (!cp) {
2476                                  error = -ENOMEM;                                  error = -ENOMEM;
2477                                  break;                                  break;
2478                          }                          }
2479                          memmove(cp, cp0, head->write_avail);                          memmove(cp, cp0, head->w.avail);
2480                          kfree(cp0);                          kfree(cp0);
2481                          head->write_buf = cp;                          head->write_buf = cp;
2482                          cp0 = cp;                          cp0 = cp;
# Line 2794  int ccs_write_control(struct file *file, Line 2488  int ccs_write_control(struct file *file,
2488                  }                  }
2489                  buffer++;                  buffer++;
2490                  avail_len--;                  avail_len--;
2491                  cp0[head->write_avail++] = c;                  cp0[head->w.avail++] = c;
2492                  if (c != '\n')                  if (c != '\n')
2493                          continue;                          continue;
2494                  cp0[head->write_avail - 1] = '\0';                  cp0[head->w.avail - 1] = '\0';
2495                  head->write_avail = 0;                  head->w.avail = 0;
2496                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2497                  head->write(head);                  head->write(head);
2498          }          }

Legend:
Removed from v.3731  
changed lines
  Added in v.3780

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