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

Subversion リポジトリの参照

Diff of /branches/ccs-patch/security/ccsecurity/policy_io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

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

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