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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/policy_io.c

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

trunk/1.7.x/ccs-patch/security/ccsecurity/policy_io.c revision 3528 by kumaneko, Wed Mar 24 02:13:40 2010 UTC branches/ccs-patch/security/ccsecurity/policy_io.c revision 3808 by kumaneko, Fri Jul 9 04:40:54 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.2-pre   2010/03/21   * Version: 1.7.2+   2010/06/04
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 40  static unsigned int ccs_profile_version; Line 40  static unsigned int ccs_profile_version;
40  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];  static struct ccs_profile *ccs_profile_ptr[CCS_MAX_PROFILES];
41    
42  /* String table for functionality that takes 4 modes. */  /* String table for functionality that takes 4 modes. */
43  static const char *ccs_mode_4[4] = {  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 218  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   * The snprintf() will truncate, but ccs_io_printf() won't.   * @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  bool ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)  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                    WARN_ON(1);
291    }
292    
293    /**
294     * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
295     *
296     * @head: Pointer to "struct ccs_io_buffer".
297     * @fmt:  The printf()'s format string, followed by parameters.
298     */
299    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;                  WARN_ON(1);
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 263  static struct ccs_profile *ccs_find_or_a Line 343  static struct ccs_profile *ccs_find_or_a
343          if (ptr)          if (ptr)
344                  return ptr;                  return ptr;
345          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);          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 280  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 290  static struct ccs_profile *ccs_find_or_a Line 372  static struct ccs_profile *ccs_find_or_a
372  static 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 298  static void ccs_check_profile(void) Line 381  static 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)          if (ccs_profile_version != 20090903)
386                  panic("Profile version %u is not supported.\n",                  panic("Profile version %u is not supported.\n",
387                        ccs_profile_version);                        ccs_profile_version);
388          printk(KERN_INFO "CCSecurity: 1.7.2-pre   2010/03/21\n");          printk(KERN_INFO "CCSecurity: 1.7.2+   2010/06/04\n");
389          printk(KERN_INFO "Mandatory Access Control activated.\n");          printk(KERN_INFO "Mandatory Access Control activated.\n");
390  }  }
391    
# Line 321  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;  
         if (sscanf(data, "PROFILE_VERSION=%u", &ccs_profile_version) == 1)  
                 return 0;  
         i = simple_strtoul(data, &cp, 10);  
         if (data == cp) {  
                 profile = &ccs_default_profile;  
         } else {  
                 if (*cp != '-')  
                         return -EINVAL;  
                 data = cp + 1;  
                 profile = ccs_find_or_assign_new_profile(i);  
                 if (!profile)  
                         return -EINVAL;  
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 *old_comment = profile->comment;          u8 config;
513                  profile->comment = ccs_get_name(cp);          if (!strcmp(name, "CONFIG")) {
                 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 474  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 483  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 503  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                          "audit",
641  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
642                        ccs_default_profile.preference.audit_max_grant_log,                        pref->audit_max_grant_log,
643                        ccs_default_profile.preference.audit_max_reject_log,                        pref->audit_max_reject_log,
644  #endif  #endif
645                        ccs_yesno(ccs_default_profile.preference.                        ccs_yesno(pref->audit_task_info),
646                                  audit_task_info),                        ccs_yesno(pref->audit_path_info));
647                        ccs_yesno(ccs_default_profile.preference.   skip0:
648                                  audit_path_info));          if (profile) {
649          ccs_io_printf(head, "PREFERENCE::learning={ verbose=%s max_entry=%u "                  pref = profile->learning;
650                        "exec.realpath=%s exec.argv0=%s symlink.target=%s }\n",                  if (pref == &ccs_default_profile.preference)
651                        ccs_yesno(ccs_default_profile.preference.                          goto skip1;
652                                  learning_verbose),          }
653                        ccs_default_profile.preference.learning_max_entry,          ccs_io_printf(head, "%sPREFERENCE::%s={ "
654                        ccs_yesno(ccs_default_profile.preference.                        "verbose=%s max_entry=%u exec.realpath=%s "
655                                  learning_exec_realpath),                        "exec.argv0=%s symlink.target=%s }\n",
656                        ccs_yesno(ccs_default_profile.preference.                        buffer, "learning",
657                                  learning_exec_argv0),                        ccs_yesno(pref->learning_verbose),
658                        ccs_yesno(ccs_default_profile.preference.                        pref->learning_max_entry,
659                                  learning_symlink_target));                        ccs_yesno(pref->learning_exec_realpath),
660          ccs_io_printf(head, "PREFERENCE::permissive={ verbose=%s }\n",                        ccs_yesno(pref->learning_exec_argv0),
661                        ccs_yesno(ccs_default_profile.preference.                        ccs_yesno(pref->learning_symlink_target));
662                                  permissive_verbose));   skip1:
663          ccs_io_printf(head, "PREFERENCE::enforcing={ verbose=%s penalty=%u "          if (profile) {
664                        "}\n",                  pref = profile->permissive;
665                        ccs_yesno(ccs_default_profile.preference.                  if (pref == &ccs_default_profile.preference)
666                                  enforcing_verbose),                          goto skip2;
667                        ccs_default_profile.preference.enforcing_penalty);          }
668          head->read_bit = 1;          ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
669   body:                        buffer, "permissive",
670          for (index = head->read_step; index < CCS_MAX_PROFILES; index++) {                        ccs_yesno(pref->permissive_verbose));
671                  bool done;   skip2:
672                  u8 config;          if (profile) {
673                  int i;                  pref = profile->enforcing;
674                  int pos;                  if (pref == &ccs_default_profile.preference)
675                  const struct ccs_profile *profile = ccs_profile_ptr[index];                          return;
676                  const struct ccs_path_info *comment;          }
677                  head->read_step = index;          ccs_io_printf(head, "%sPREFERENCE::%s={ verbose=%s "
678                  if (!profile)                        "penalty=%u }\n", buffer, "enforcing",
679                          continue;                        ccs_yesno(pref->enforcing_verbose),
680                  pos = head->read_avail;                        pref->enforcing_penalty);
681                  comment = profile->comment;  }
682                  done = ccs_io_printf(head, "%u-COMMENT=%s\n", index,  
683                                       comment ? comment->name : "");  static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
684                  if (!done)  {
685                          goto out;          ccs_io_printf(head, "={ mode=%s", ccs_mode[config & 3]);
                 config = profile->default_config;  
 #ifdef CONFIG_CCSECURITY_AUDIT  
                 if (!ccs_io_printf(head, "%u-CONFIG={ mode=%s grant_log=%s "  
                                    "reject_log=%s }\n", index,  
                                    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;  
 #endif  
                 for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_CAPABILITY_INDEX  
                              + CCS_MAX_MAC_CATEGORY_INDEX; i++) {  
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 669  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 *ptr;          struct ccs_manager e = { };
         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          mutex_lock(&ccs_policy_lock);          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
796          list_for_each_entry_rcu(ptr, &ccs_policy_manager_list, list) {                                    &ccs_policy_list[CCS_ID_MANAGER],
797                  if (ptr->manager != e.manager)                                    ccs_same_manager);
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error) {  
                 struct ccs_policy_manager_entry *entry =  
                         ccs_commit_ok(&e, sizeof(e));  
                 if (entry) {  
                         list_add_tail_rcu(&entry->list,  
                                           &ccs_policy_manager_list);  
                         error = 0;  
                 }  
         }  
         mutex_unlock(&ccs_policy_lock);  
798          ccs_put_name(e.manager);          ccs_put_name(e.manager);
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 724  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 764  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 812  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 838  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 ||
# Line 864  static bool ccs_is_select_one(struct ccs Line 947  static bool ccs_is_select_one(struct ccs
947                          domain = ccs_task_domain(p);                          domain = ccs_task_domain(p);
948                  ccs_tasklist_unlock();                  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_IGNORE_GLOBAL] = CCS_KEYWORD_IGNORE_GLOBAL "\n",
1007            [CCS_DIF_TRANSITION_FAILED] = CCS_KEYWORD_TRANSITION_FAILED "\n"
1008    };
1009            
1010  /**  /**
1011   * ccs_write_domain_policy - Write domain policy.   * ccs_write_domain - Write domain policy.
1012   *   *
1013   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1014   *   *
1015   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1016   */   */
1017  static int ccs_write_domain_policy(struct ccs_io_buffer *head)  static int ccs_write_domain(struct ccs_io_buffer *head)
1018  {  {
1019          char *data = head->write_buf;          char *data = head->write_buf;
1020          struct ccs_domain_info *domain = head->write_var1;          struct ccs_domain_info *domain = head->w.domain;
1021          bool is_delete = false;          bool is_delete = false;
1022          bool is_select = false;          bool is_select = false;
1023          unsigned int profile;          unsigned int profile;
         struct ccs_condition *cond = NULL;  
         char *cp;  
         int error;  
1024          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))          if (ccs_str_starts(&data, CCS_KEYWORD_DELETE))
1025                  is_delete = true;                  is_delete = true;
1026          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))          else if (ccs_str_starts(&data, CCS_KEYWORD_SELECT))
1027                  is_select = true;                  is_select = true;
1028          if (is_select && ccs_is_select_one(head, data))          if (is_select && ccs_select_one(head, data))
1029                  return 0;                  return 0;
1030          /* Don't allow updating policies by non manager programs. */          /* Don't allow updating policies by non manager programs. */
1031          if (!ccs_is_policy_manager())          if (!ccs_manager())
1032                  return -EPERM;                  return -EPERM;
1033          if (ccs_is_domain_def(data)) {          if (ccs_domain_def(data)) {
1034                  domain = NULL;                  domain = NULL;
1035                  if (is_delete)                  if (is_delete)
1036                          ccs_delete_domain(data);                          ccs_delete_domain(data);
1037                  else if (is_select)                  else if (is_select)
1038                          domain = ccs_find_domain(data);                          domain = ccs_find_domain(data);
1039                  else                  else
1040                          domain = ccs_find_or_assign_new_domain(data, 0);                          domain = ccs_assign_domain(data, 0);
1041                  head->write_var1 = domain;                  head->w.domain = domain;
1042                  return 0;                  return 0;
1043          }          }
1044          if (!domain)          if (!domain)
# Line 957  static int ccs_write_domain_policy(struc Line 1050  static int ccs_write_domain_policy(struc
1050                          domain->profile = (u8) profile;                          domain->profile = (u8) profile;
1051                  return 0;                  return 0;
1052          }          }
1053          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {          for (profile = 0; profile < CCS_MAX_DOMAIN_INFO_FLAGS; profile++) {
1054                  domain->ignore_global_allow_read = !is_delete;                  const char *cp = ccs_dif[profile];
1055                  return 0;                  if (strncmp(data, cp, strlen(cp) - 1))
1056          }                          continue;
1057          if (!strcmp(data, CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV)) {                  domain->flags[profile] = !is_delete;
                 domain->ignore_global_allow_env = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_QUOTA_EXCEEDED)) {  
                 domain->quota_warned = !is_delete;  
                 return 0;  
         }  
         if (!strcmp(data, CCS_KEYWORD_TRANSITION_FAILED)) {  
                 domain->domain_transition_failed = !is_delete;  
1058                  return 0;                  return 0;
1059          }          }
1060          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;  
1061  }  }
1062    
1063  /**  /**
# Line 990  static int ccs_write_domain_policy(struc Line 1065  static int ccs_write_domain_policy(struc
1065   *   *
1066   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1067   * @ptr:  Pointer to "struct ccs_name_union".   * @ptr:  Pointer to "struct ccs_name_union".
  *  
  * Returns true on success, false otherwise.  
1068   */   */
1069  static bool ccs_print_name_union(struct ccs_io_buffer *head,  static void ccs_print_name_union(struct ccs_io_buffer *head,
1070                                   const struct ccs_name_union *ptr)                                   const struct ccs_name_union *ptr)
1071  {  {
1072          int pos = head->read_avail;          const bool cond = head->r.print_cond_part;
1073          if (pos && head->read_buf[pos - 1] == ' ')          if (!cond)
1074                  head->read_avail--;                  ccs_set_space(head);
1075          if (ptr->is_group)          if (ptr->is_group) {
1076                  return ccs_io_printf(head, " @%s",                  ccs_set_string(head, "@");
1077                                       ptr->group->group_name->name);                  ccs_set_string(head, ptr->group->group_name->name);
1078          return ccs_io_printf(head, " %s", ptr->filename->name);          } else {
1079  }                  if (cond)
1080                            ccs_set_string(head, "\"");
1081  /**                  ccs_set_string(head, ptr->filename->name);
1082   * ccs_print_name_union_quoted - Print a ccs_name_union with double quotes.                  if (cond)
1083   *                          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);  
1084          }          }
1085  }  }
1086    
1087  /**  /**
1088   * ccs_print_number_union - Print a ccs_number_union.   * ccs_print_number_union - Print a ccs_number_union.
1089   *   *
1090   * @head:       Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1091   * @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.  
1092   */   */
1093  static bool ccs_print_number_union_nospace(struct ccs_io_buffer *head,  static void ccs_print_number_union(struct ccs_io_buffer *head,
1094                                             const struct ccs_number_union *ptr)                                     const struct ccs_number_union *ptr)
1095  {  {
1096          return ccs_print_number_union_common(head, ptr, false);          if (!head->r.print_cond_part)
1097                    ccs_set_space(head);
1098            if (ptr->is_group) {
1099                    ccs_set_string(head, "@");
1100                    ccs_set_string(head, ptr->group->group_name->name);
1101            } else {
1102                    int i;
1103                    unsigned long min = ptr->values[0];
1104                    const unsigned long max = ptr->values[1];
1105                    u8 min_type = ptr->value_type[0];
1106                    const u8 max_type = ptr->value_type[1];
1107                    char buffer[128];
1108                    buffer[0] = '\0';
1109                    for (i = 0; i < 2; i++) {
1110                            switch (min_type) {
1111                            case CCS_VALUE_TYPE_HEXADECIMAL:
1112                                    ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
1113                                                  min);
1114                                    break;
1115                            case CCS_VALUE_TYPE_OCTAL:
1116                                    ccs_addprintf(buffer, sizeof(buffer), "0%lo",
1117                                                  min);
1118                                    break;
1119                            default:
1120                                    ccs_addprintf(buffer, sizeof(buffer), "%lu",
1121                                                  min);
1122                                    break;
1123                            }
1124                            if (min == max && min_type == max_type)
1125                                    break;
1126                            ccs_addprintf(buffer, sizeof(buffer), "-");
1127                            min_type = max_type;
1128                            min = max;
1129                    }
1130                    ccs_io_printf(head, "%s", buffer);
1131            }
1132  }  }
1133    
1134  /**  /**
1135   * ccs_print_condition - Print condition part.   * ccs_print_condition - Print condition part.
1136   *   *
1137   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1138   * @cond: Pointer to "struct ccs_condition". May be NULL.   * @cond: Pointer to "struct ccs_condition".
1139   *   *
1140   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1141   */   */
1142  static bool ccs_print_condition(struct ccs_io_buffer *head,  static bool ccs_print_condition(struct ccs_io_buffer *head,
1143                                  const struct ccs_condition *cond)                                  const struct ccs_condition *cond)
1144  {  {
1145          const struct ccs_condition_element *condp;          switch (head->r.cond_step) {
1146          const struct ccs_number_union *numbers_p;          case 0:
1147          const struct ccs_name_union *names_p;                  {
1148          const struct ccs_argv_entry *argv;                          if (cond->condc)
1149          const struct ccs_envp_entry *envp;                                  ccs_set_string(head, " if");
1150          u16 condc;                          head->r.cond_index = 0;
1151          u16 i;                          head->r.cond_step++;
1152          u16 j;                  }
1153          char buffer[32];                  /* fall through */
1154          if (!cond)          case 1:
1155                  goto no_condition;                  {
1156          condc = cond->condc;                          const u16 condc = cond->condc;
1157          condp = (const struct ccs_condition_element *) (cond + 1);                          const struct ccs_condition_element *condp =
1158          numbers_p = (const struct ccs_number_union *) (condp + condc);                                  (typeof(condp)) (cond + 1);
1159          names_p = (const struct ccs_name_union *)                          const struct ccs_number_union *numbers_p =
1160                  (numbers_p + cond->numbers_count);                                  (typeof(numbers_p)) (condp + condc);
1161          argv = (const struct ccs_argv_entry *) (names_p + cond->names_count);                          const struct ccs_name_union *names_p =
1162          envp = (const struct ccs_envp_entry *) (argv + cond->argc);                                  (typeof(names_p))
1163          memset(buffer, 0, sizeof(buffer));                                  (numbers_p + cond->numbers_count);
1164          if (condc && !ccs_io_printf(head, "%s", " if"))                          const struct ccs_argv *argv =
1165                  goto out;                                  (typeof(argv)) (names_p + cond->names_count);
1166          for (i = 0; i < condc; i++) {                          const struct ccs_envp *envp =
1167                  const u8 match = condp->equals;                                  (typeof(envp)) (argv + cond->argc);
1168                  const u8 left = condp->left;                          u16 skip;
1169                  const u8 right = condp->right;                          for (skip = 0; skip < head->r.cond_index; skip++) {
1170                  condp++;                                  const u8 left = condp->left;
1171                  switch (left) {                                  const u8 right = condp->right;
1172                  case CCS_ARGV_ENTRY:                                  condp++;
1173                          if (!ccs_io_printf(head, " exec.argv[%u]%s\"%s\"",                                  switch (left) {
1174                                             argv->index, argv->is_not ?                                  case CCS_ARGV_ENTRY:
1175                                             "!=" : "=", argv->value->name))                                          argv++;
1176                                  goto out;                                          continue;
1177                          argv++;                                  case CCS_ENVP_ENTRY:
1178                          continue;                                          envp++;
1179                  case CCS_ENVP_ENTRY:                                          continue;
1180                          if (!ccs_io_printf(head, " exec.envp[\"%s\"]%s",                                  case CCS_NUMBER_UNION:
1181                                             envp->name->name, envp->is_not ?                                          numbers_p++;
1182                                             "!=" : "="))                                          break;
1183                                  goto out;                                  }
1184                          if (envp->value) {                                  switch (right) {
1185                                  if (!ccs_io_printf(head, "\"%s\"",                                  case CCS_NAME_UNION:
1186                                                     envp->value->name))                                          names_p++;
1187                                          goto out;                                          break;
1188                          } else {                                  case CCS_NUMBER_UNION:
1189                                  if (!ccs_io_printf(head, "NULL"))                                          numbers_p++;
1190                                          goto out;                                          break;
1191                                    }
1192                            }
1193                            while (head->r.cond_index < condc) {
1194                                    const u8 match = condp->equals;
1195                                    const u8 left = condp->left;
1196                                    const u8 right = condp->right;
1197                                    if (!ccs_flush(head))
1198                                            return false;
1199                                    condp++;
1200                                    head->r.cond_index++;
1201                                    ccs_set_space(head);
1202                                    switch (left) {
1203                                    case CCS_ARGV_ENTRY:
1204                                            ccs_io_printf(head,
1205                                                          "exec.argv[%u]%s\"%s\"",
1206                                                          argv->index,
1207                                                          argv->is_not ?
1208                                                          "!=" : "=",
1209                                                          argv->value->name);
1210                                            argv++;
1211                                            continue;
1212                                    case CCS_ENVP_ENTRY:
1213                                            ccs_io_printf(head,
1214                                                          "exec.envp[\"%s\"]%s",
1215                                                          envp->name->name,
1216                                                          envp->is_not ?
1217                                                          "!=" : "=");
1218                                            if (envp->value) {
1219                                                    ccs_set_string(head, "\"");
1220                                                    ccs_set_string(head, envp->
1221                                                                   value->name);
1222                                                    ccs_set_string(head, "\"");
1223                                            } else {
1224                                                    ccs_set_string(head, "NULL");
1225                                            }
1226                                            envp++;
1227                                            continue;
1228                                    case CCS_NUMBER_UNION:
1229                                            ccs_print_number_union(head,
1230                                                                   numbers_p++);
1231                                            break;
1232                                    default:
1233                                            ccs_set_string(head,
1234                                                   ccs_condition_keyword[left]);
1235                                            break;
1236                                    }
1237                                    ccs_set_string(head, match ? "=" : "!=");
1238                                    switch (right) {
1239                                    case CCS_NAME_UNION:
1240                                            ccs_print_name_union(head, names_p++);
1241                                            break;
1242                                    case CCS_NUMBER_UNION:
1243                                            ccs_print_number_union(head,
1244                                                                   numbers_p++);
1245                                            break;
1246                                    default:
1247                                            ccs_set_string(head,
1248                                                   ccs_condition_keyword[right]);
1249                                            break;
1250                                    }
1251                          }                          }
                         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;  
1252                  }                  }
1253                  if (!ccs_io_printf(head, "%s", match ? "=" : "!="))                  head->r.cond_step++;
1254                          goto out;                  /* fall through */
1255                  switch (right) {          case 2:
1256                  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;  
1257                          break;                          break;
1258                    head->r.cond_step++;
1259                    /* fall through */
1260            case 3:
1261                    {
1262                            u8 j;
1263                            const u8 i = cond->post_state[3];
1264                            if (i)
1265                                    ccs_set_string(head, " ; set");
1266                            for (j = 0; j < 3; j++)
1267                                    if ((i & (1 << j)))
1268                                            ccs_io_printf(head,
1269                                                          " task.state[%u]=%u", j,
1270                                                          cond->post_state[j]);
1271                            if (i & (1 << 4))
1272                                    ccs_io_printf(head, " audit=%s",
1273                                                  ccs_yesno(cond->post_state[4]));
1274                  }                  }
1275          }                  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"))  
1276                  return true;                  return true;
  out:  
         return false;  
 }  
   
 /**  
  * ccs_print_path_acl - Print a path ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_acl(struct ccs_io_buffer *head,  
                                struct ccs_path_acl *ptr,  
                                const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_OPERATION; bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 if (head->read_execute_only && bit != CCS_TYPE_EXECUTE  
                     && bit != CCS_TYPE_TRANSIT)  
                         continue;  
                 /* Print "read/write" instead of "read" and "write". */  
                 if ((bit == CCS_TYPE_READ || bit == CCS_TYPE_WRITE)  
                     && (perm & (1 << CCS_TYPE_READ_WRITE)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s", ccs_path2keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
 }  
   
 /**  
  * ccs_print_path_number3_acl - Print a path_number3 ACL entry.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_path_number3_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_print_path_number3_acl(struct ccs_io_buffer *head,  
                                        struct ccs_path_number3_acl *ptr,  
                                        const struct ccs_condition *cond)  
 {  
         int pos;  
         u8 bit;  
         const u16 perm = ptr->perm;  
         for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER3_OPERATION;  
              bit++) {  
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path_number32keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name) ||  
                     !ccs_print_number_union(head, &ptr->mode) ||  
                     !ccs_print_number_union(head, &ptr->major) ||  
                     !ccs_print_number_union(head, &ptr->minor) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
1277          }          }
1278          head->read_bit = 0;          return false;
         return true;  
1279  }  }
1280    
1281  /**  /**
1282   * ccs_print_path2_acl - Print a path2 ACL entry.   * ccs_fns - Find next set bit.
1283   *   *
1284   * @head: Pointer to "struct ccs_io_buffer".   * @perm: 8 bits value.
1285   * @ptr:  Pointer to "struct ccs_path2_acl".   * @bit:  First bit to find.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1286   *   *
1287   * Returns true on success, false otherwise.   * Returns next set bit on success, 8 otherwise.
1288   */   */
1289  static bool ccs_print_path2_acl(struct ccs_io_buffer *head,  static u8 ccs_fns(const u8 perm, u8 bit)
                                 struct ccs_path2_acl *ptr,  
                                 const struct ccs_condition *cond)  
1290  {  {
1291          int pos;          for ( ; bit < 8; bit++)
1292          u8 bit;                  if (perm & (1 << bit))
1293          const u8 perm = ptr->perm;                          break;
1294          for (bit = head->read_bit; bit < CCS_MAX_PATH2_OPERATION; bit++) {          return bit;
                 if (!(perm & (1 << bit)))  
                         continue;  
                 pos = head->read_avail;  
                 if (!ccs_io_printf(head, "allow_%s",  
                                    ccs_path22keyword(bit)) ||  
                     !ccs_print_name_union(head, &ptr->name1) ||  
                     !ccs_print_name_union(head, &ptr->name2) ||  
                     !ccs_print_condition(head, cond)) {  
                         head->read_bit = bit;  
                         head->read_avail = pos;  
                         return false;  
                 }  
         }  
         head->read_bit = 0;  
         return true;  
1295  }  }
1296    
1297  /**  /**
1298   * ccs_print_path_number_acl - Print a path_number ACL entry.   * ccs_print_entry - Print an ACL entry.
1299   *   *
1300   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1301   * @ptr:  Pointer to "struct ccs_path_number_acl".   * @acl:  Pointer to an ACL entry.
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1302   *   *
1303   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1304   */   */
1305  static bool ccs_print_path_number_acl(struct ccs_io_buffer *head,  static bool ccs_print_entry(struct ccs_io_buffer *head,
1306                                        struct ccs_path_number_acl *ptr,                              const struct ccs_acl_info *acl)
                                       const struct ccs_condition *cond)  
1307  {  {
1308          int pos;          const u8 acl_type = acl->type;
1309          u8 bit;          u8 bit;
1310          const u8 perm = ptr->perm;          if (head->r.print_cond_part)
1311          for (bit = head->read_bit; bit < CCS_MAX_PATH_NUMBER_OPERATION;                  goto print_cond_part;
1312               bit++) {          if (acl->is_deleted)
1313                  if (!(perm & (1 << bit)))                  return true;
1314                          continue;   next:
1315                  pos = head->read_avail;          bit = head->r.bit;
1316                  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))  
1317                  return false;                  return false;
1318          if (min_address != max_address) {          else if (acl_type == CCS_TYPE_PATH_ACL) {
1319                  ccs_print_ipv6(buf, sizeof(buf), max_address);                  struct ccs_path_acl *ptr
1320                  if (!ccs_io_printf(head, "-%s", buf))                          = container_of(acl, typeof(*ptr), head);
1321                          return false;                  const u16 perm = ptr->perm;
1322          }                  for ( ; bit < CCS_MAX_PATH_OPERATION; bit++) {
1323          return true;                          if (!(perm & (1 << bit)))
1324  }                                  continue;
1325                            if (head->r.print_execute_only &&
1326  /**                              bit != CCS_TYPE_EXECUTE && bit != CCS_TYPE_TRANSIT)
1327   * ccs_print_network_acl - Print a network ACL entry.                                  continue;
1328   *                          break;
1329   * @head: Pointer to "struct ccs_io_buffer".                  }
1330   * @ptr:  Pointer to "struct ccs_ip_network_acl".                  if (bit >= CCS_MAX_PATH_OPERATION)
1331   * @cond: Pointer to "struct ccs_condition". May be NULL.                          goto done;
1332   *                  ccs_set_string(head, "file ");
1333   * Returns true on success, false otherwise.                  ccs_set_string(head, ccs_path_keyword[bit]);
1334   */                  ccs_print_name_union(head, &ptr->name);
1335  static bool ccs_print_network_acl(struct ccs_io_buffer *head,          } else if (acl_type == CCS_TYPE_EXECUTE_HANDLER ||
1336                                    struct ccs_ip_network_acl *ptr,                     acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
1337                                    const struct ccs_condition *cond)                  struct ccs_execute_handler *ptr
1338  {                          = container_of(acl, typeof(*ptr), head);
1339          int pos;                  ccs_io_printf(head, "%s ",
1340          u8 bit;                                acl_type == CCS_TYPE_EXECUTE_HANDLER ?
1341          const u16 perm = ptr->perm;                                CCS_KEYWORD_EXECUTE_HANDLER :
1342          for (bit = head->read_bit; bit < CCS_MAX_NETWORK_OPERATION; bit++) {                                CCS_KEYWORD_DENIED_EXECUTE_HANDLER);
1343                  if (!(perm & (1 << bit)))                  ccs_set_string(head, ptr->handler->name);
1344                          continue;          } else if (head->r.print_execute_only) {
1345                  pos = head->read_avail;                  return true;
1346                  if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_NETWORK "%s ",          } else if (acl_type == CCS_TYPE_MKDEV_ACL) {
1347                                     ccs_net2keyword(bit)))                  struct ccs_mkdev_acl *ptr =
1348                          goto out;                          container_of(acl, typeof(*ptr), head);
1349                    bit = ccs_fns(ptr->perm, bit);
1350                    if (bit >= CCS_MAX_MKDEV_OPERATION)
1351                            goto done;
1352                    ccs_set_string(head, "file ");
1353                    ccs_set_string(head, ccs_mkdev_keyword[bit]);
1354                    ccs_print_name_union(head, &ptr->name);
1355                    ccs_print_number_union(head, &ptr->mode);
1356                    ccs_print_number_union(head, &ptr->major);
1357                    ccs_print_number_union(head, &ptr->minor);
1358            } else if (acl_type == CCS_TYPE_PATH2_ACL) {
1359                    struct ccs_path2_acl *ptr =
1360                            container_of(acl, typeof(*ptr), head);
1361                    bit = ccs_fns(ptr->perm, bit);
1362                    if (bit >= CCS_MAX_PATH2_OPERATION)
1363                            goto done;
1364                    ccs_set_string(head, "file ");
1365                    ccs_set_string(head, ccs_path2_keyword[bit]);
1366                    ccs_print_name_union(head, &ptr->name1);
1367                    ccs_print_name_union(head, &ptr->name2);
1368            } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
1369                    struct ccs_path_number_acl *ptr =
1370                            container_of(acl, typeof(*ptr), head);
1371                    bit = ccs_fns(ptr->perm, bit);
1372                    if (bit >= CCS_MAX_PATH_NUMBER_OPERATION)
1373                            goto done;
1374                    ccs_set_string(head, "file ");
1375                    ccs_set_string(head, ccs_path_number_keyword[bit]);
1376                    ccs_print_name_union(head, &ptr->name);
1377                    ccs_print_number_union(head, &ptr->number);
1378            } else if (acl_type == CCS_TYPE_ENV_ACL) {
1379                    struct ccs_env_acl *ptr =
1380                            container_of(acl, typeof(*ptr), head);
1381                    ccs_set_string(head, "misc env ");
1382                    ccs_set_string(head, ptr->env->name);
1383            } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
1384                    struct ccs_capability_acl *ptr =
1385                            container_of(acl, typeof(*ptr), head);
1386                    ccs_set_string(head, "capability ");
1387                    ccs_set_string(head, ccs_cap2keyword(ptr->operation));
1388            } else if (acl_type == CCS_TYPE_IP_NETWORK_ACL) {
1389                    struct ccs_ip_network_acl *ptr =
1390                            container_of(acl, typeof(*ptr), head);
1391                    bit = ccs_fns(ptr->perm, bit);
1392                    if (bit >= CCS_MAX_NETWORK_OPERATION)
1393                            goto done;
1394                    ccs_set_string(head, "network ");
1395                    ccs_set_string(head, ccs_net_keyword[bit]);
1396                    ccs_set_space(head);
1397                  switch (ptr->address_type) {                  switch (ptr->address_type) {
1398                            char buf[128];
1399                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:                  case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
1400                          if (!ccs_io_printf(head, "@%s", ptr->address.group->                          ccs_set_string(head, "@");
1401                                             group_name->name))                          ccs_set_string(head,
1402                                  goto out;                                         ptr->address.group->group_name->name);
1403                          break;                          break;
1404                  case CCS_IP_ADDRESS_TYPE_IPv4:                  case CCS_IP_ADDRESS_TYPE_IPv4:
1405                          if (!ccs_print_ipv4_entry(head, ptr))                          ccs_print_ipv4(buf, sizeof(buf), ptr->address.ipv4.min,
1406                                  goto out;                                         ptr->address.ipv4.max);
1407                            ccs_io_printf(head, "%s", buf);
1408                          break;                          break;
1409                  case CCS_IP_ADDRESS_TYPE_IPv6:                  case CCS_IP_ADDRESS_TYPE_IPv6:
1410                          if (!ccs_print_ipv6_entry(head, ptr))                          ccs_print_ipv6(buf, sizeof(buf), ptr->address.ipv6.min,
1411                                  goto out;                                         ptr->address.ipv6.max);
1412                          break;                          ccs_io_printf(head, "%s", buf);
1413                  }                          break;
1414                  if (!ccs_print_number_union(head, &ptr->port) ||                  }
1415                      !ccs_print_condition(head, cond))                  ccs_print_number_union(head, &ptr->port);
1416                          goto out;          } else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
1417                    struct ccs_signal_acl *ptr =
1418                            container_of(acl, typeof(*ptr), head);
1419                    ccs_set_string(head, "ipc signal ");
1420                    ccs_io_printf(head, "%u ", ptr->sig);
1421                    ccs_set_string(head, ptr->domainname->name);
1422            } else if (acl_type == CCS_TYPE_MOUNT_ACL) {
1423                    struct ccs_mount_acl *ptr =
1424                            container_of(acl, typeof(*ptr), head);
1425                    ccs_io_printf(head, "file mount");
1426                    ccs_print_name_union(head, &ptr->dev_name);
1427                    ccs_print_name_union(head, &ptr->dir_name);
1428                    ccs_print_name_union(head, &ptr->fs_type);
1429                    ccs_print_number_union(head, &ptr->flags);
1430            }
1431            head->r.bit = bit + 1;
1432            if (acl->cond) {
1433                    head->r.print_cond_part = true;
1434                    head->r.cond_step = 0;
1435                    if (!ccs_flush(head))
1436                            return false;
1437     print_cond_part:
1438                    if (!ccs_print_condition(head, acl->cond))
1439                            return false;
1440                    head->r.print_cond_part = false;
1441            } else {
1442                    ccs_set_lf(head);
1443          }          }
1444          head->read_bit = 0;          switch (acl_type) {
1445          return true;          case CCS_TYPE_PATH_ACL:
1446   out:          case CCS_TYPE_MKDEV_ACL:
1447          head->read_bit = bit;          case CCS_TYPE_PATH2_ACL:
1448          head->read_avail = pos;          case CCS_TYPE_PATH_NUMBER_ACL:
1449          return false;          case CCS_TYPE_IP_NETWORK_ACL:
1450  }                  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;  
1451          }          }
1452     done:
1453            head->r.bit = 0;
1454          return true;          return true;
1455  }  }
1456    
1457  /**  /**
1458   * ccs_print_execute_handler_record - Print an execute handler ACL entry.   * ccs_read_domain2 - Read domain policy.
1459   *   *
1460   * @head:    Pointer to "struct ccs_io_buffer".   * @head:   Pointer to "struct ccs_io_buffer".
1461   * @keyword: Name of the keyword.   * @domain: Pointer to "struct ccs_domain_info".
  * @ptr:     Pointer to "struct ccs_execute_handler_record".  
1462   *   *
1463   * Returns true on success, false otherwise.   * Caller holds ccs_read_lock().
  */  
 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.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  * @ptr:  Pointer to "struct ccs_mount_acl".  
  * @cond: Pointer to "struct ccs_condition". May be NULL.  
1464   *   *
1465   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1466   */   */
1467  static bool ccs_print_mount_acl(struct ccs_io_buffer *head,  static bool ccs_read_domain2(struct ccs_io_buffer *head,
1468                                  struct ccs_mount_acl *ptr,                               struct ccs_domain_info *domain)
                                 const struct ccs_condition *cond)  
1469  {  {
1470          const int pos = head->read_avail;          list_for_each_cookie(head->r.acl, &domain->acl_info_list) {
1471          if (!ccs_io_printf(head, CCS_KEYWORD_ALLOW_MOUNT) ||                  struct ccs_acl_info *ptr =
1472              !ccs_print_name_union(head, &ptr->dev_name) ||                          list_entry(head->r.acl, typeof(*ptr), list);
1473              !ccs_print_name_union(head, &ptr->dir_name) ||                  if (!ccs_print_entry(head, ptr))
1474              !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;  
1475          }          }
1476            head->r.acl = NULL;
1477          return true;          return true;
1478  }  }
1479    
1480  /**  /**
1481   * 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.  
1482   *   *
1483   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1484   *   *
1485   * Caller holds ccs_read_lock().   * Caller holds ccs_read_lock().
1486   */   */
1487  static void ccs_read_domain_policy(struct ccs_io_buffer *head)  static void ccs_read_domain(struct ccs_io_buffer *head)
1488  {  {
1489          struct list_head *dpos;          if (head->r.eof)
         struct list_head *apos;  
         if (head->read_eof)  
1490                  return;                  return;
1491          if (head->read_step == 0)          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1492                  head->read_step = 1;                  struct ccs_domain_info *domain =
1493          list_for_each_cookie(dpos, head->read_var1, &ccs_domain_list) {                          list_entry(head->r.domain, typeof(*domain), list);
1494                  struct ccs_domain_info *domain;                  switch (head->r.step) {
1495                  const char *quota_exceeded = "";                          u8 i;
1496                  const char *transition_failed = "";                  case 0:
1497                  const char *ignore_global_allow_read = "";                          if (domain->is_deleted &&
1498                  const char *ignore_global_allow_env = "";                              !head->r.print_this_domain_only)
1499                  domain = list_entry(dpos, struct ccs_domain_info, list);                                  continue;
1500                  if (head->read_step != 1)                          /* Print domainname and flags. */
1501                          goto acl_loop;                          ccs_set_string(head, domain->domainname->name);
1502                  if (domain->is_deleted && !head->read_single_domain)                          ccs_set_lf(head);
1503                          continue;                          ccs_io_printf(head, CCS_KEYWORD_USE_PROFILE "%u\n",
1504                  /* Print domainname and flags. */                                        domain->profile);
1505                  if (domain->quota_warned)                          for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
1506                          quota_exceeded = CCS_KEYWORD_QUOTA_EXCEEDED "\n";                                  if (domain->flags[i])
1507                  if (domain->domain_transition_failed)                                          ccs_set_string(head, ccs_dif[i]);
1508                          transition_failed = CCS_KEYWORD_TRANSITION_FAILED "\n";                          head->r.step++;
1509                  if (domain->ignore_global_allow_read)                          ccs_set_lf(head);
1510                          ignore_global_allow_read                          /* fall through */
1511                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";                  case 1:
1512                  if (domain->ignore_global_allow_env)                          if (!ccs_read_domain2(head, domain))
1513                          ignore_global_allow_env                                  return;
1514                                  = CCS_KEYWORD_IGNORE_GLOBAL_ALLOW_ENV "\n";                          head->r.step++;
1515                  if (!ccs_io_printf(head, "%s\n" CCS_KEYWORD_USE_PROFILE "%u\n"                          if (!ccs_set_lf(head))
                                    "%s%s%s%s\n", domain->domainname->name,  
                                    domain->profile, quota_exceeded,  
                                    transition_failed,  
                                    ignore_global_allow_read,  
                                    ignore_global_allow_env))  
                         return;  
                 head->read_step = 2;  
  acl_loop:  
                 if (head->read_step == 3)  
                         goto tail_mark;  
                 /* Print ACL entries in the domain. */  
                 list_for_each_cookie(apos, head->read_var2,  
                                      &domain->acl_info_list) {  
                         struct ccs_acl_info *ptr  
                                 = list_entry(apos, struct ccs_acl_info, list);  
                         if (!ccs_print_entry(head, ptr))  
1516                                  return;                                  return;
1517                            /* fall through */
1518                    case 2:
1519                            head->r.step = 0;
1520                            if (head->r.print_this_domain_only)
1521                                    goto done;
1522                  }                  }
                 head->read_step = 3;  
  tail_mark:  
                 if (!ccs_io_printf(head, "\n"))  
                         return;  
                 head->read_step = 1;  
                 if (head->read_single_domain)  
                         break;  
1523          }          }
1524          head->read_eof = true;   done:
1525            head->r.eof = true;
1526  }  }
1527    
1528  /**  /**
# Line 1760  static int ccs_write_domain_profile(stru Line 1573  static int ccs_write_domain_profile(stru
1573   */   */
1574  static void ccs_read_domain_profile(struct ccs_io_buffer *head)  static void ccs_read_domain_profile(struct ccs_io_buffer *head)
1575  {  {
1576          struct list_head *pos;          if (head->r.eof)
         if (head->read_eof)  
1577                  return;                  return;
1578          list_for_each_cookie(pos, head->read_var1, &ccs_domain_list) {          list_for_each_cookie(head->r.domain, &ccs_domain_list) {
1579                  struct ccs_domain_info *domain;                  struct ccs_domain_info *domain =
1580                  domain = list_entry(pos, struct ccs_domain_info, list);                          list_entry(head->r.domain, typeof(*domain), list);
1581                  if (domain->is_deleted)                  if (domain->is_deleted)
1582                          continue;                          continue;
1583                  if (!ccs_io_printf(head, "%u %s\n", domain->profile,                  if (!ccs_flush(head))
                                    domain->domainname->name))  
1584                          return;                          return;
1585                    ccs_io_printf(head, "%u ", domain->profile);
1586                    ccs_set_string(head, domain->domainname->name);
1587                    ccs_set_lf(head);
1588          }          }
1589          head->read_eof = true;          head->r.eof = true;
1590  }  }
1591    
1592  /**  /**
# Line 1784  static void ccs_read_domain_profile(stru Line 1598  static void ccs_read_domain_profile(stru
1598   */   */
1599  static int ccs_write_pid(struct ccs_io_buffer *head)  static int ccs_write_pid(struct ccs_io_buffer *head)
1600  {  {
1601          head->read_eof = false;          head->r.eof = false;
1602          return 0;          return 0;
1603  }  }
1604    
# Line 1810  static void ccs_read_pid(struct ccs_io_b Line 1624  static void ccs_read_pid(struct ccs_io_b
1624          u32 ccs_flags = 0;          u32 ccs_flags = 0;
1625          /* Accessing write_buf is safe because head->io_sem is held. */          /* Accessing write_buf is safe because head->io_sem is held. */
1626          if (!buf) {          if (!buf) {
1627                  head->read_eof = true;                  head->r.eof = true;
1628                  return; /* Do nothing if open(O_RDONLY). */                  return; /* Do nothing if open(O_RDONLY). */
1629          }          }
1630          if (head->read_avail || head->read_eof)          if (head->r.w_pos || head->r.eof)
1631                  return;                  return;
1632          head->read_eof = true;          head->r.eof = true;
1633          if (ccs_str_starts(&buf, "info "))          if (ccs_str_starts(&buf, "info "))
1634                  task_info = true;                  task_info = true;
1635          if (ccs_str_starts(&buf, "global-pid "))          if (ccs_str_starts(&buf, "global-pid "))
# Line 1837  static void ccs_read_pid(struct ccs_io_b Line 1651  static void ccs_read_pid(struct ccs_io_b
1651          ccs_tasklist_unlock();          ccs_tasklist_unlock();
1652          if (!domain)          if (!domain)
1653                  return;                  return;
1654          if (!task_info)          if (!task_info) {
1655                  ccs_io_printf(head, "%u %u %s", pid, domain->profile,                  ccs_io_printf(head, "%u %u ", pid, domain->profile);
1656                                domain->domainname->name);                  ccs_set_string(head, domain->domainname->name);
1657          else          } else {
1658                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "                  ccs_io_printf(head, "%u manager=%s execute_handler=%s "
1659                                "state[0]=%u state[1]=%u state[2]=%u", pid,                                "state[0]=%u state[1]=%u state[2]=%u", pid,
1660                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1661                                          CCS_TASK_IS_POLICY_MANAGER),                                          CCS_TASK_IS_MANAGER),
1662                                ccs_yesno(ccs_flags &                                ccs_yesno(ccs_flags &
1663                                          CCS_TASK_IS_EXECUTE_HANDLER),                                          CCS_TASK_IS_EXECUTE_HANDLER),
1664                                (u8) (ccs_flags >> 24),                                (u8) (ccs_flags >> 24),
1665                                (u8) (ccs_flags >> 16),                                (u8) (ccs_flags >> 16),
1666                                (u8) (ccs_flags >> 8));                                (u8) (ccs_flags >> 8));
1667            }
1668  }  }
1669    
1670    static const char *ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
1671            [CCS_TRANSITION_CONTROL_NO_INITIALIZE]
1672            = CCS_KEYWORD_NO_INITIALIZE_DOMAIN,
1673            [CCS_TRANSITION_CONTROL_INITIALIZE] = CCS_KEYWORD_INITIALIZE_DOMAIN,
1674            [CCS_TRANSITION_CONTROL_NO_KEEP] = CCS_KEYWORD_NO_KEEP_DOMAIN,
1675            [CCS_TRANSITION_CONTROL_KEEP] = CCS_KEYWORD_KEEP_DOMAIN
1676    };
1677    
1678    static const char *ccs_group_name[CCS_MAX_GROUP] = {
1679            [CCS_PATH_GROUP] = CCS_KEYWORD_PATH_GROUP,
1680            [CCS_NUMBER_GROUP] = CCS_KEYWORD_NUMBER_GROUP,
1681            [CCS_ADDRESS_GROUP] = CCS_KEYWORD_ADDRESS_GROUP
1682    };
1683    
1684  /**  /**
1685   * ccs_write_exception_policy - Write exception policy.   * ccs_write_exception - Write exception policy.
1686   *   *
1687   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1688   *   *
1689   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1690   */   */
1691  static int ccs_write_exception_policy(struct ccs_io_buffer *head)  static int ccs_write_exception(struct ccs_io_buffer *head)
1692  {  {
1693          char *data = head->write_buf;          char *data = head->write_buf;
1694          bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);          const bool is_delete = ccs_str_starts(&data, CCS_KEYWORD_DELETE);
1695          if (ccs_str_starts(&data, CCS_KEYWORD_KEEP_DOMAIN))          u8 i;
1696                  return ccs_write_domain_keeper_policy(data, false, is_delete);          static const struct {
1697          if (ccs_str_starts(&data, CCS_KEYWORD_NO_KEEP_DOMAIN))                  const char *keyword;
1698                  return ccs_write_domain_keeper_policy(data, true, is_delete);                  int (*write) (char *, const bool);
1699          if (ccs_str_starts(&data, CCS_KEYWORD_INITIALIZE_DOMAIN))          } ccs_callback[3] = {
1700                  return ccs_write_domain_initializer_policy(data, false,                  { CCS_KEYWORD_AGGREGATOR, ccs_write_aggregator },
1701                                                             is_delete);                  { CCS_KEYWORD_FILE_PATTERN, ccs_write_pattern },
1702          if (ccs_str_starts(&data, CCS_KEYWORD_NO_INITIALIZE_DOMAIN))                  { CCS_KEYWORD_DENY_AUTOBIND, ccs_write_reserved_port }
1703                  return ccs_write_domain_initializer_policy(data, true,          };
1704                                                             is_delete);          for (i = 0; i < 3; i++)
1705          if (ccs_str_starts(&data, CCS_KEYWORD_AGGREGATOR))                  if (ccs_str_starts(&data, ccs_callback[i].keyword))
1706                  return ccs_write_aggregator_policy(data, is_delete);                          return ccs_callback[i].write(data, is_delete);
1707          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_READ))          for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
1708                  return ccs_write_globally_readable_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_transition_type[i]))
1709          if (ccs_str_starts(&data, CCS_KEYWORD_ALLOW_ENV))                          return ccs_write_transition_control(data, is_delete,
1710                  return ccs_write_globally_usable_env_policy(data, is_delete);                                                              i);
1711          if (ccs_str_starts(&data, CCS_KEYWORD_FILE_PATTERN))          for (i = 0; i < CCS_MAX_GROUP; i++)
1712                  return ccs_write_pattern_policy(data, is_delete);                  if (ccs_str_starts(&data, ccs_group_name[i]))
1713          if (ccs_str_starts(&data, CCS_KEYWORD_PATH_GROUP))                          return ccs_write_group(data, is_delete, i);
1714                  return ccs_write_path_group_policy(data, is_delete);          return ccs_write_domain2(data, &ccs_global_domain, is_delete);
         if (ccs_str_starts(&data, CCS_KEYWORD_NUMBER_GROUP))  
                 return ccs_write_number_group_policy(data, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_DENY_REWRITE))  
                 return ccs_write_no_rewrite_policy(data, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_ADDRESS_GROUP))  
                 return ccs_write_address_group_policy(data, is_delete);  
         if (ccs_str_starts(&data, CCS_KEYWORD_DENY_AUTOBIND))  
                 return ccs_write_reserved_port_policy(data, is_delete);  
         return -EINVAL;  
1715  }  }
1716    
1717  /**  /**
1718   * ccs_read_exception_policy - Read exception policy.   * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
1719   *   *
1720   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
1721   *   * @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".  
1722   *   *
1723   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
1724     *
1725     * Caller holds ccs_read_lock().
1726   */   */
1727  static bool ccs_get_argv0(struct ccs_execve_entry *ee)  static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
1728  {  {
1729          struct linux_binprm *bprm = ee->bprm;          list_for_each_cookie(head->r.group, &ccs_group_list[idx]) {
1730          char *arg_ptr = ee->tmp;                  struct ccs_group *group =
1731          int arg_len = 0;                          list_entry(head->r.group, typeof(*group), head.list);
1732          unsigned long pos = bprm->p;                  list_for_each_cookie(head->r.acl, &group->member_list) {
1733          int offset = pos % PAGE_SIZE;                          struct ccs_acl_head *ptr =
1734          bool done = false;                                  list_entry(head->r.acl, typeof(*ptr), list);
1735          if (!bprm->argc)                          if (ptr->is_deleted)
1736                  goto out;                                  continue;
1737          while (1) {                          if (!ccs_flush(head))
1738                  if (!ccs_dump_page(bprm, pos, &ee->dump))                                  return false;
1739                          goto out;                          ccs_set_string(head, ccs_group_name[idx]);
1740                  pos += PAGE_SIZE - offset;                          ccs_set_string(head, group->group_name->name);
1741                  /* Read. */                          if (idx == CCS_PATH_GROUP) {
1742                  while (offset < PAGE_SIZE) {                                  ccs_set_space(head);
1743                          const char *kaddr = ee->dump.data;                                  ccs_set_string(head, container_of
1744                          const unsigned char c = kaddr[offset++];                                                 (ptr, struct ccs_path_group,
1745                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {                                                  head)->member_name->name);
1746                                  if (c == '\\') {                          } else if (idx == CCS_NUMBER_GROUP) {
1747                                          arg_ptr[arg_len++] = '\\';                                  ccs_print_number_union(head, &container_of
1748                                          arg_ptr[arg_len++] = '\\';                                                         (ptr, struct ccs_number_group,
1749                                  } else if (c > ' ' && c < 127) {                                                          head)->number);
1750                                          arg_ptr[arg_len++] = c;                          } else if (idx == CCS_ADDRESS_GROUP) {
1751                                  } else {                                  char buffer[128];
1752                                          arg_ptr[arg_len++] = '\\';                                  struct ccs_address_group *member =
1753                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          container_of(ptr, typeof(*member),
1754                                          arg_ptr[arg_len++]                                                       head);
1755                                                  = ((c >> 3) & 7) + '0';                                  if (member->is_ipv6)
1756                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          ccs_print_ipv6(buffer, sizeof(buffer),
1757                                  }                                                         member->min.ipv6,
1758                          } else {                                                         member->max.ipv6);
1759                                  arg_ptr[arg_len] = '\0';                                  else
1760                                  done = true;                                          ccs_print_ipv4(buffer, sizeof(buffer),
1761                                  break;                                                         member->min.ipv4,
1762                                                           member->max.ipv4);
1763                                    ccs_io_printf(head, " %s", buffer);
1764                          }                          }
1765                            ccs_set_lf(head);
1766                  }                  }
1767                  offset = 0;                  head->r.acl = NULL;
                 if (done)  
                         break;  
1768          }          }
1769            head->r.group = NULL;
1770          return true;          return true;
  out:  
         return false;  
1771  }  }
1772    
1773  /**  /**
1774   * ccs_get_execute_condition - Get condition part for execute requests.   * ccs_read_policy - Read "struct ccs_..._entry" list.
1775   *   *
1776   * @ee: Pointer to "struct ccs_execve_entry".   * @head: Pointer to "struct ccs_io_buffer".
1777     * @idx:  Index number.
1778     *
1779     * Returns true on success, false otherwise.
1780   *   *
1781   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1782   */   */
1783  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)  
1784  {  {
1785          struct ccs_condition *cond;          list_for_each_cookie(head->r.acl, &ccs_policy_list[idx]) {
1786          char *buf;                  struct ccs_acl_head *acl =
1787          int len = 256;                          container_of(head->r.acl, typeof(*acl), list);
1788          char *realpath = NULL;                  if (acl->is_deleted)
1789          char *argv0 = NULL;                          continue;
1790          const struct ccs_profile *profile = ccs_profile(ee->r.domain->profile);                  if (!ccs_flush(head))
1791          if (profile->learning->learning_exec_realpath) {                          return false;
1792                  struct file *file = ee->bprm->file;                  switch (idx) {
1793  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)                  case CCS_ID_TRANSITION_CONTROL:
1794                  struct path path = { file->f_vfsmnt, file->f_dentry };                          {
1795                  realpath = ccs_realpath_from_path(&path);                                  struct ccs_transition_control *ptr =
1796  #else                                          container_of(acl, typeof(*ptr), head);
1797                  realpath = ccs_realpath_from_path(&file->f_path);                                  ccs_set_string(head,
1798  #endif                                                 ccs_transition_type[ptr->type]);
1799                  if (realpath)                                  ccs_set_string(head, ptr->program ?
1800                          len += strlen(realpath) + 17;                                                 ptr->program->name : "any");
1801          }                                  ccs_set_string(head, " from ");
1802          if (profile->learning->learning_exec_argv0) {                                  ccs_set_string(head, ptr->domainname ?
1803                  if (ccs_get_argv0(ee)) {                                                 ptr->domainname->name : "any");
1804                          argv0 = ee->tmp;                          }
1805                          len += strlen(argv0) + 16;                          break;
1806                    case CCS_ID_AGGREGATOR:
1807                            {
1808                                    struct ccs_aggregator *ptr =
1809                                            container_of(acl, typeof(*ptr), head);
1810                                    ccs_set_string(head, CCS_KEYWORD_AGGREGATOR);
1811                                    ccs_set_string(head, ptr->original_name->name);
1812                                    ccs_set_space(head);
1813                                    ccs_set_string(head,
1814                                                   ptr->aggregated_name->name);
1815                            }
1816                            break;
1817                    case CCS_ID_PATTERN:
1818                            {
1819                                    struct ccs_pattern *ptr =
1820                                            container_of(acl, typeof(*ptr), head);
1821                                    ccs_set_string(head, CCS_KEYWORD_FILE_PATTERN);
1822                                    ccs_set_string(head, ptr->pattern->name);
1823                            }
1824                            break;
1825                    case CCS_ID_RESERVEDPORT:
1826                            {
1827                                    struct ccs_reserved *ptr =
1828                                            container_of(acl, typeof(*ptr), head);
1829                                    const u16 min_port = ptr->min_port;
1830                                    const u16 max_port = ptr->max_port;
1831                                    ccs_set_string(head,
1832                                                   CCS_KEYWORD_DENY_AUTOBIND);
1833                                    ccs_io_printf(head, "%u", min_port);
1834                                    if (min_port != max_port)
1835                                            ccs_io_printf(head, "-%u", max_port);
1836                            }
1837                            break;
1838                    default:
1839                            continue;
1840                  }                  }
1841                    ccs_set_lf(head);
1842          }          }
1843          buf = kmalloc(len, CCS_GFP_FLAGS);          head->r.acl = NULL;
1844          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;  
1845  }  }
1846    
1847  /**  /**
1848   * ccs_get_symlink_condition - Get condition part for symlink requests.   * ccs_read_exception - Read exception policy.
1849   *   *
1850   * @r: Pointer to "struct ccs_request_info".   * @head: Pointer to "struct ccs_io_buffer".
1851   *   *
1852   * Returns pointer to "struct ccs_condition" on success, NULL otherwise.   * Caller holds ccs_read_lock().
1853   */   */
1854  static struct ccs_condition *ccs_get_symlink_condition(struct ccs_request_info  static void ccs_read_exception(struct ccs_io_buffer *head)
                                                        *r)  
1855  {  {
1856          struct ccs_condition *cond;          if (head->r.eof)
1857          char *buf;                  return;
1858          int len = 256;          while (head->r.step < CCS_MAX_POLICY &&
1859          const char *symlink = NULL;                 ccs_read_policy(head, head->r.step))
1860          const struct ccs_profile *profile = ccs_profile(r->profile);                  head->r.step++;
1861          if (profile->learning->learning_symlink_target) {          if (head->r.step < CCS_MAX_POLICY)
1862                  symlink = r->obj->symlink_target->name;                  return;
1863                  len += strlen(symlink) + 18;          while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
1864          }                 ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
1865          buf = kmalloc(len, CCS_GFP_FLAGS);                  head->r.step++;
1866          if (!buf)          if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
1867                  return NULL;                  return;
1868          snprintf(buf, len - 1, "if");          head->r.eof = ccs_read_domain2(head, &ccs_global_domain);
         if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1,  
                          " task.type=execute_handler");  
         }  
         if (symlink) {  
                 const int pos = strlen(buf);  
                 snprintf(buf + pos, len - pos - 1, " symlink.target=\"%s\"",  
                          symlink);  
         }  
         cond = ccs_get_condition(buf);  
         kfree(buf);  
         return cond;  
1869  }  }
1870    
1871  /* Wait queue for ccs_query_list. */  /* Wait queue for ccs_query_list. */
# Line 2126  static DECLARE_WAIT_QUEUE_HEAD(ccs_query Line 1875  static DECLARE_WAIT_QUEUE_HEAD(ccs_query
1875  static DEFINE_SPINLOCK(ccs_query_list_lock);  static DEFINE_SPINLOCK(ccs_query_list_lock);
1876    
1877  /* Structure for query. */  /* Structure for query. */
1878  struct ccs_query_entry {  struct ccs_query {
1879          struct list_head list;          struct list_head list;
1880          char *query;          char *query;
1881          int query_len;          int query_len;
# Line 2135  struct ccs_query_entry { Line 1884  struct ccs_query_entry {
1884          int answer;          int answer;
1885  };  };
1886    
1887  /* The list for "struct ccs_query_entry". */  /* The list for "struct ccs_query". */
1888  static LIST_HEAD(ccs_query_list);  static LIST_HEAD(ccs_query_list);
1889    
1890  /* Number of "struct file" referring /proc/ccs/query interface. */  /* Number of "struct file" referring /proc/ccs/query interface. */
1891  static atomic_t ccs_query_observers = ATOMIC_INIT(0);  static atomic_t ccs_query_observers = ATOMIC_INIT(0);
1892    
1893    static void ccs_truncate(char *str)
1894    {
1895            while (* (unsigned char *) str > (unsigned char) ' ')
1896                    str++;
1897            *str = '\0';
1898    }
1899    
1900  /**  /**
1901   * ccs_supervisor - Ask for the supervisor's decision.   * ccs_supervisor - Ask for the supervisor's decision.
1902   *   *
1903   * @r:       Pointer to "struct ccs_request_info".   * @r:   Pointer to "struct ccs_request_info".
1904   * @fmt:     The printf()'s format string, followed by parameters.   * @fmt: The printf()'s format string, followed by parameters.
1905   *   *
1906   * Returns 0 if the supervisor decided to permit the access request which   * Returns 0 if the supervisor decided to permit the access request which
1907   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor   * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
# Line 2159  int ccs_supervisor(struct ccs_request_in Line 1915  int ccs_supervisor(struct ccs_request_in
1915          int pos;          int pos;
1916          int len;          int len;
1917          static unsigned int ccs_serial;          static unsigned int ccs_serial;
1918          struct ccs_query_entry *ccs_query_entry = NULL;          struct ccs_query *entry = NULL;
1919          bool quota_exceeded = false;          bool quota_exceeded = false;
1920          char *header;          char *header;
1921          if (!r->domain)          struct ccs_domain_info * const domain = ccs_current_domain();
1922                  r->domain = ccs_current_domain();          va_start(args, fmt);
1923          switch (r->mode) {          len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 80;
1924            va_end(args);
1925            if (r->mode == CCS_CONFIG_LEARNING) {
1926                  char *buffer;                  char *buffer;
1927                  struct ccs_condition *cond;                  char *realpath = NULL;
1928          case CCS_CONFIG_LEARNING:                  char *argv0 = NULL;
1929                    char *symlink = NULL;
1930                    char *handler = NULL;
1931                    const struct ccs_preference *pref;
1932                  if (!ccs_domain_quota_ok(r))                  if (!ccs_domain_quota_ok(r))
1933                          return 0;                          return 0;
1934                  va_start(args, fmt);                  header = ccs_init_log(&len, r);
1935                  len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;                  if (!header)
                 va_end(args);  
                 buffer = kmalloc(len, CCS_GFP_FLAGS);  
                 if (!buffer)  
1936                          return 0;                          return 0;
1937                  va_start(args, fmt);                  pref = ccs_profile(r->profile)->learning;
1938                  vsnprintf(buffer, len - 1, fmt, args);                  /* strstr() will return NULL if ordering is wrong. */
1939                  va_end(args);                  if (r->param_type == CCS_TYPE_PATH_ACL &&
1940                  ccs_normalize_line(buffer);                      r->param.path.operation == CCS_TYPE_EXECUTE) {
1941                  if (r->ee && !strncmp(buffer, "allow_execute ", 14))                          if (pref->learning_exec_argv0) {
1942                          cond = ccs_get_execute_condition(r->ee);                                  argv0 = strstr(header, " argv[]={ \"");
1943                  else if (r->obj && r->obj->symlink_target)                                  if (argv0) {
1944                          cond = ccs_get_symlink_condition(r);                                          argv0 += 10;
1945                  else if ((current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)) {                                          ccs_truncate(argv0);
1946                          char str[] = "if task.type=execute_handler";                                  }
1947                          cond = ccs_get_condition(str);                          }
1948                  } else                          if (pref->learning_exec_realpath) {
1949                          cond = NULL;                                  realpath = strstr(header,
1950                  ccs_write_domain_policy2(buffer, r->domain, cond, false);                                                    " exec={ realpath=\"");
1951                  ccs_put_condition(cond);                                  if (realpath) {
1952                  kfree(buffer);                                          realpath += 8;
1953                  /* fall through */                                          ccs_truncate(realpath);
1954          case CCS_CONFIG_PERMISSIVE:                                  }
1955                            }
1956                    } else if (r->param_type == CCS_TYPE_PATH_ACL &&
1957                               r->param.path.operation == CCS_TYPE_SYMLINK &&
1958                               pref->learning_symlink_target) {
1959                            symlink = strstr(header, " symlink.target=\"");
1960                            if (symlink)
1961                                    ccs_truncate(symlink + 1);
1962                    }
1963                    handler = strstr(header, "type=execute_handler");
1964                    if (handler)
1965                            ccs_truncate(handler);
1966                    buffer = kmalloc(len, CCS_GFP_FLAGS);
1967                    if (buffer) {
1968                            va_start(args, fmt);
1969                            vsnprintf(buffer, len - 1, fmt, args);
1970                            va_end(args);
1971                            if (handler || realpath || argv0 || symlink) {
1972                                    ccs_addprintf(buffer, len, " if");
1973                                    if (handler)
1974                                            ccs_addprintf(buffer, len, " task.%s",
1975                                                          handler);
1976                                    if (realpath)
1977                                            ccs_addprintf(buffer, len, " exec.%s",
1978                                                          realpath);
1979                                    if (argv0)
1980                                            ccs_addprintf(buffer, len,
1981                                                          " exec.argv[0]=%s",
1982                                                          argv0);
1983                                    if (symlink)
1984                                            ccs_addprintf(buffer, len, "%s",
1985                                                          symlink);
1986                            }
1987                            ccs_normalize_line(buffer);
1988                            ccs_write_domain2(buffer, domain, false);
1989                            kfree(buffer);
1990                    }
1991                    kfree(header);
1992                  return 0;                  return 0;
1993          }          }
1994            if (r->mode != CCS_CONFIG_ENFORCING)
1995                    return 0;
1996          if (!atomic_read(&ccs_query_observers)) {          if (!atomic_read(&ccs_query_observers)) {
1997                  int i;                  int i;
1998                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)                  if (current->ccs_flags & CCS_DONT_SLEEP_ON_ENFORCE_ERROR)
1999                          return -EPERM;                          return -EPERM;
2000                  for (i = 0; i < ccs_profile(r->domain->profile)->enforcing->                  for (i = 0; i < ccs_profile(domain->profile)->enforcing->
2001                               enforcing_penalty; i++) {                               enforcing_penalty; i++) {
2002                          set_current_state(TASK_INTERRUPTIBLE);                          set_current_state(TASK_INTERRUPTIBLE);
2003                          schedule_timeout(HZ / 10);                          schedule_timeout(HZ / 10);
2004                  }                  }
2005                  return -EPERM;                  return -EPERM;
2006          }          }
2007          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);  
2008          if (!header)          if (!header)
2009                  goto out;                  goto out;
2010          ccs_query_entry = kzalloc(sizeof(*ccs_query_entry), CCS_GFP_FLAGS);          entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
2011          if (!ccs_query_entry)          if (!entry)
2012                  goto out;                  goto out;
2013          len = ccs_round2(len);          len = ccs_round2(len);
2014          ccs_query_entry->query = kzalloc(len, CCS_GFP_FLAGS);          entry->query = kzalloc(len, CCS_GFP_FLAGS);
2015          if (!ccs_query_entry->query)          if (!entry->query)
2016                  goto out;                  goto out;
         INIT_LIST_HEAD(&ccs_query_entry->list);  
2017          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2018          if (ccs_quota_for_query && ccs_query_memory_size + len +          if (ccs_quota_for_query && ccs_query_memory_size + len +
2019              sizeof(*ccs_query_entry) >= ccs_quota_for_query) {              sizeof(*entry) >= ccs_quota_for_query) {
2020                  quota_exceeded = true;                  quota_exceeded = true;
2021          } else {          } else {
2022                  ccs_query_memory_size += len + sizeof(*ccs_query_entry);                  ccs_query_memory_size += len + sizeof(*entry);
2023                  ccs_query_entry->serial = ccs_serial++;                  entry->serial = ccs_serial++;
2024          }          }
2025          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2026          if (quota_exceeded)          if (quota_exceeded)
2027                  goto out;                  goto out;
2028          pos = snprintf(ccs_query_entry->query, len - 1, "Q%u-%hu\n%s",          pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
2029                         ccs_query_entry->serial, r->retry, header);                         entry->serial, r->retry, header);
2030          kfree(header);          kfree(header);
2031          header = NULL;          header = NULL;
2032          va_start(args, fmt);          va_start(args, fmt);
2033          vsnprintf(ccs_query_entry->query + pos, len - 1 - pos, fmt, args);          vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
2034          ccs_query_entry->query_len = strlen(ccs_query_entry->query) + 1;          entry->query_len = strlen(entry->query) + 1;
2035          va_end(args);          va_end(args);
2036          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2037          list_add_tail(&ccs_query_entry->list, &ccs_query_list);          list_add_tail(&entry->list, &ccs_query_list);
2038          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2039          /* Give 10 seconds for supervisor's opinion. */          /* Give 10 seconds for supervisor's opinion. */
2040          for (ccs_query_entry->timer = 0;          for (entry->timer = 0;
2041               atomic_read(&ccs_query_observers) && ccs_query_entry->timer < 100;               atomic_read(&ccs_query_observers) && entry->timer < 100;
2042               ccs_query_entry->timer++) {               entry->timer++) {
2043                  wake_up(&ccs_query_wait);                  wake_up(&ccs_query_wait);
2044                  set_current_state(TASK_INTERRUPTIBLE);                  set_current_state(TASK_INTERRUPTIBLE);
2045                  schedule_timeout(HZ / 10);                  schedule_timeout(HZ / 10);
2046                  if (ccs_query_entry->answer)                  if (entry->answer)
2047                          break;                          break;
2048          }          }
2049          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2050          list_del(&ccs_query_entry->list);          list_del(&entry->list);
2051          ccs_query_memory_size -= len + sizeof(*ccs_query_entry);          ccs_query_memory_size -= len + sizeof(*entry);
2052          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2053          switch (ccs_query_entry->answer) {          switch (entry->answer) {
2054          case 3: /* Asked to retry by administrator. */          case 3: /* Asked to retry by administrator. */
2055                  error = CCS_RETRY_REQUEST;                  error = CCS_RETRY_REQUEST;
2056                  r->retry++;                  r->retry++;
# Line 2274  int ccs_supervisor(struct ccs_request_in Line 2067  int ccs_supervisor(struct ccs_request_in
2067                  break;                  break;
2068          }          }
2069   out:   out:
2070          if (ccs_query_entry)          if (entry)
2071                  kfree(ccs_query_entry->query);                  kfree(entry->query);
2072          kfree(ccs_query_entry);          kfree(entry);
2073          kfree(header);          kfree(header);
2074          return error;          return error;
2075  }  }
# Line 2299  static int ccs_poll_query(struct file *f Line 2092  static int ccs_poll_query(struct file *f
2092          for (i = 0; i < 2; i++) {          for (i = 0; i < 2; i++) {
2093                  spin_lock(&ccs_query_list_lock);                  spin_lock(&ccs_query_list_lock);
2094                  list_for_each(tmp, &ccs_query_list) {                  list_for_each(tmp, &ccs_query_list) {
2095                          struct ccs_query_entry *ptr                          struct ccs_query *ptr =
2096                                  = list_entry(tmp, struct ccs_query_entry, list);                                  list_entry(tmp, typeof(*ptr), list);
2097                          if (ptr->answer)                          if (ptr->answer)
2098                                  continue;                                  continue;
2099                          found = true;                          found = true;
# Line 2327  static void ccs_read_query(struct ccs_io Line 2120  static void ccs_read_query(struct ccs_io
2120          int pos = 0;          int pos = 0;
2121          int len = 0;          int len = 0;
2122          char *buf;          char *buf;
2123          if (head->read_avail)          if (head->r.w_pos)
2124                  return;                  return;
2125          if (head->read_buf) {          if (head->read_buf) {
2126                  kfree(head->read_buf);                  kfree(head->read_buf);
2127                  head->read_buf = NULL;                  head->read_buf = NULL;
                 head->readbuf_size = 0;  
2128          }          }
2129          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2130          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2131                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2132                  if (ptr->answer)                  if (ptr->answer)
2133                          continue;                          continue;
2134                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2135                          continue;                          continue;
2136                  len = ptr->query_len;                  len = ptr->query_len;
2137                  break;                  break;
2138          }          }
2139          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2140          if (!len) {          if (!len) {
2141                  head->read_step = 0;                  head->r.query_index = 0;
2142                  return;                  return;
2143          }          }
2144          buf = kzalloc(len, CCS_GFP_FLAGS);          buf = kzalloc(len, CCS_GFP_FLAGS);
# Line 2356  static void ccs_read_query(struct ccs_io Line 2147  static void ccs_read_query(struct ccs_io
2147          pos = 0;          pos = 0;
2148          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2149          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2150                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2151                  if (ptr->answer)                  if (ptr->answer)
2152                          continue;                          continue;
2153                  if (pos++ != head->read_step)                  if (pos++ != head->r.query_index)
2154                          continue;                          continue;
2155                  /*                  /*
2156                   * Some query can be skipped because ccs_query_list                   * Some query can be skipped because ccs_query_list
# Line 2372  static void ccs_read_query(struct ccs_io Line 2162  static void ccs_read_query(struct ccs_io
2162          }          }
2163          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
2164          if (buf[0]) {          if (buf[0]) {
                 head->read_avail = len;  
                 head->readbuf_size = head->read_avail;  
2165                  head->read_buf = buf;                  head->read_buf = buf;
2166                  head->read_step++;                  head->r.w[head->r.w_pos++] = buf;
2167                    head->r.query_index++;
2168          } else {          } else {
2169                  kfree(buf);                  kfree(buf);
2170          }          }
# Line 2396  static int ccs_write_answer(struct ccs_i Line 2185  static int ccs_write_answer(struct ccs_i
2185          unsigned int answer;          unsigned int answer;
2186          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2187          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2188                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2189                  ptr->timer = 0;                  ptr->timer = 0;
2190          }          }
2191          spin_unlock(&ccs_query_list_lock);          spin_unlock(&ccs_query_list_lock);
# Line 2405  static int ccs_write_answer(struct ccs_i Line 2193  static int ccs_write_answer(struct ccs_i
2193                  return -EINVAL;                  return -EINVAL;
2194          spin_lock(&ccs_query_list_lock);          spin_lock(&ccs_query_list_lock);
2195          list_for_each(tmp, &ccs_query_list) {          list_for_each(tmp, &ccs_query_list) {
2196                  struct ccs_query_entry *ptr                  struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
                         = list_entry(tmp, struct ccs_query_entry, list);  
2197                  if (ptr->serial != serial)                  if (ptr->serial != serial)
2198                          continue;                          continue;
2199                  if (!ptr->answer)                  if (!ptr->answer)
# Line 2424  static int ccs_write_answer(struct ccs_i Line 2211  static int ccs_write_answer(struct ccs_i
2211   */   */
2212  static void ccs_read_version(struct ccs_io_buffer *head)  static void ccs_read_version(struct ccs_io_buffer *head)
2213  {  {
2214          if (head->read_eof)          if (head->r.eof)
2215                  return;                  return;
2216          ccs_io_printf(head, "1.7.1");          ccs_set_string(head, "1.7.2");
2217          head->read_eof = true;          head->r.eof = true;
2218  }  }
2219    
2220  /**  /**
# Line 2437  static void ccs_read_version(struct ccs_ Line 2224  static void ccs_read_version(struct ccs_
2224   */   */
2225  static void ccs_read_self_domain(struct ccs_io_buffer *head)  static void ccs_read_self_domain(struct ccs_io_buffer *head)
2226  {  {
2227          if (head->read_eof)          if (head->r.eof)
2228                  return;                  return;
2229          /*          /*
2230           * ccs_current_domain()->domainname != NULL because every process           * ccs_current_domain()->domainname != NULL because every process
2231           * belongs to a domain and the domain's name cannot be NULL.           * belongs to a domain and the domain's name cannot be NULL.
2232           */           */
2233          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);          ccs_io_printf(head, "%s", ccs_current_domain()->domainname->name);
2234          head->read_eof = true;          head->r.eof = true;
2235  }  }
2236    
2237  /**  /**
# Line 2464  int ccs_open_control(const u8 type, stru Line 2251  int ccs_open_control(const u8 type, stru
2251          head->type = type;          head->type = type;
2252          switch (type) {          switch (type) {
2253          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */          case CCS_DOMAINPOLICY: /* /proc/ccs/domain_policy */
2254                  head->write = ccs_write_domain_policy;                  head->write = ccs_write_domain;
2255                  head->read = ccs_read_domain_policy;                  head->read = ccs_read_domain;
2256                  break;                  break;
2257          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */          case CCS_EXCEPTIONPOLICY: /* /proc/ccs/exception_policy */
2258                  head->write = ccs_write_exception_policy;                  head->write = ccs_write_exception;
2259                  head->read = ccs_read_exception_policy;                  head->read = ccs_read_exception;
2260                  break;                  break;
2261  #ifdef CONFIG_CCSECURITY_AUDIT  #ifdef CONFIG_CCSECURITY_AUDIT
2262          case CCS_GRANTLOG: /* /proc/ccs/grant_log */          case CCS_GRANTLOG: /* /proc/ccs/grant_log */
2263          case CCS_REJECTLOG: /* /proc/ccs/reject_log */          case CCS_REJECTLOG: /* /proc/ccs/reject_log */
2264                  head->poll = ccs_poll_audit_log;                  head->poll = ccs_poll_log;
2265                  head->read = ccs_read_audit_log;                  head->read = ccs_read_log;
2266                  break;                  break;
2267  #endif  #endif
2268          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */          case CCS_SELFDOMAIN: /* /proc/ccs/self_domain */
# Line 2515  int ccs_open_control(const u8 type, stru Line 2302  int ccs_open_control(const u8 type, stru
2302                  head->read = ccs_read_query;                  head->read = ccs_read_query;
2303                  break;                  break;
2304          case CCS_MANAGER: /* /proc/ccs/manager */          case CCS_MANAGER: /* /proc/ccs/manager */
2305                  head->write = ccs_write_manager_policy;                  head->write = ccs_write_manager;
2306                  head->read = ccs_read_manager_policy;                  head->read = ccs_read_manager;
2307                  break;                  break;
2308          }          }
2309          if (!(file->f_mode & FMODE_READ)) {          if (!(file->f_mode & FMODE_READ)) {
# Line 2552  int ccs_open_control(const u8 type, stru Line 2339  int ccs_open_control(const u8 type, stru
2339                  }                  }
2340          }          }
2341          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2342              type != CCS_GRANTLOG && type != CCS_REJECTLOG) {              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2343                  spin_lock(&ccs_io_buffer_list_lock);                  head->reader_idx = ccs_lock();
                 list_add(&head->list, &ccs_io_buffer_list);  
                 spin_unlock(&ccs_io_buffer_list_lock);  
         }  
2344          file->private_data = head;          file->private_data = head;
2345          /*          /*
2346           * 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 2606  int ccs_poll_control(struct file *file, Line 2390  int ccs_poll_control(struct file *file,
2390  int ccs_read_control(struct file *file, char __user *buffer,  int ccs_read_control(struct file *file, char __user *buffer,
2391                       const int buffer_len)                       const int buffer_len)
2392  {  {
2393          int len = 0;          int len;
2394          struct ccs_io_buffer *head = file->private_data;          struct ccs_io_buffer *head = file->private_data;
         char *cp;  
2395          int idx;          int idx;
2396          if (!head->read)          if (!head->read)
2397                  return -ENOSYS;                  return -ENOSYS;
# Line 2616  int ccs_read_control(struct file *file, Line 2399  int ccs_read_control(struct file *file,
2399                  return -EFAULT;                  return -EFAULT;
2400          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2401                  return -EINTR;                  return -EINTR;
2402            head->read_user_buf = buffer;
2403            head->read_user_buf_avail = buffer_len;
2404          idx = ccs_read_lock();          idx = ccs_read_lock();
2405          while (1) {          if (ccs_flush(head))
2406                  /* Call the policy handler. */                  /* Call the policy handler. */
2407                  head->read(head);                  head->read(head);
2408                  /* Write to buffer. */          ccs_flush(head);
                 len = head->read_avail;  
                 if (len || head->poll || head->read_eof)  
                         break;  
                 len = head->readbuf_size * 2;  
                 cp = kzalloc(len, CCS_GFP_FLAGS);  
                 if (!cp) {  
                         len = -ENOMEM;  
                         goto out;  
                 }  
                 kfree(head->read_buf);  
                 head->read_buf = cp;  
                 head->readbuf_size = len;  
         }  
         if (len > buffer_len)  
                 len = buffer_len;  
         if (!len)  
                 goto out;  
         /* head->read_buf changes by some functions. */  
         cp = head->read_buf;  
         if (copy_to_user(buffer, cp, len)) {  
                 len = -EFAULT;  
                 goto out;  
         }  
         head->read_avail -= len;  
         memmove(cp, cp + len, head->read_avail);  
  out:  
2409          ccs_read_unlock(idx);          ccs_read_unlock(idx);
2410            len = head->read_user_buf - buffer;
2411          mutex_unlock(&head->io_sem);          mutex_unlock(&head->io_sem);
2412          return len;          return len;
2413  }  }
# Line 2673  int ccs_write_control(struct file *file, Line 2433  int ccs_write_control(struct file *file,
2433                  return -ENOSYS;                  return -ENOSYS;
2434          if (!access_ok(VERIFY_READ, buffer, buffer_len))          if (!access_ok(VERIFY_READ, buffer, buffer_len))
2435                  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;  
2436          if (mutex_lock_interruptible(&head->io_sem))          if (mutex_lock_interruptible(&head->io_sem))
2437                  return -EINTR;                  return -EINTR;
2438          idx = ccs_read_lock();          idx = ccs_read_lock();
2439            /* Don't allow updating policies by non manager programs. */
2440            if (head->write != ccs_write_pid && head->write != ccs_write_domain &&
2441                !ccs_manager()) {
2442                    ccs_read_unlock(idx);
2443                    mutex_unlock(&head->io_sem);
2444                    return -EPERM;
2445            }
2446          /* Read a line and dispatch it to the policy handler. */          /* Read a line and dispatch it to the policy handler. */
2447          while (avail_len > 0) {          while (avail_len > 0) {
2448                  char c;                  char c;
2449                  if (head->write_avail >= head->writebuf_size - 1) {                  if (head->w.avail >= head->writebuf_size - 1) {
2450                          const int len = head->writebuf_size * 2;                          const int len = head->writebuf_size * 2;
2451                          char *cp = kzalloc(len, CCS_GFP_FLAGS);                          char *cp = kzalloc(len, CCS_GFP_FLAGS);
2452                          if (!cp) {                          if (!cp) {
2453                                  error = -ENOMEM;                                  error = -ENOMEM;
2454                                  break;                                  break;
2455                          }                          }
2456                          memmove(cp, cp0, head->write_avail);                          memmove(cp, cp0, head->w.avail);
2457                          kfree(cp0);                          kfree(cp0);
2458                          head->write_buf = cp;                          head->write_buf = cp;
2459                          cp0 = cp;                          cp0 = cp;
# Line 2703  int ccs_write_control(struct file *file, Line 2465  int ccs_write_control(struct file *file,
2465                  }                  }
2466                  buffer++;                  buffer++;
2467                  avail_len--;                  avail_len--;
2468                  cp0[head->write_avail++] = c;                  cp0[head->w.avail++] = c;
2469                  if (c != '\n')                  if (c != '\n')
2470                          continue;                          continue;
2471                  cp0[head->write_avail - 1] = '\0';                  cp0[head->w.avail - 1] = '\0';
2472                  head->write_avail = 0;                  head->w.avail = 0;
2473                  ccs_normalize_line(cp0);                  ccs_normalize_line(cp0);
2474                  head->write(head);                  head->write(head);
2475          }          }
# Line 2734  int ccs_close_control(struct file *file) Line 2496  int ccs_close_control(struct file *file)
2496          if (type == CCS_QUERY)          if (type == CCS_QUERY)
2497                  atomic_dec(&ccs_query_observers);                  atomic_dec(&ccs_query_observers);
2498          if (type != CCS_QUERY &&          if (type != CCS_QUERY &&
2499              type != CCS_GRANTLOG && type != CCS_REJECTLOG) {              type != CCS_GRANTLOG && type != CCS_REJECTLOG)
2500                  spin_lock(&ccs_io_buffer_list_lock);                  ccs_unlock(head->reader_idx);
                 list_del(&head->list);  
                 spin_unlock(&ccs_io_buffer_list_lock);  
         }  
2501          /* Release memory used for policy I/O. */          /* Release memory used for policy I/O. */
2502          kfree(head->read_buf);          kfree(head->read_buf);
2503          head->read_buf = NULL;          head->read_buf = NULL;
# Line 2747  int ccs_close_control(struct file *file) Line 2506  int ccs_close_control(struct file *file)
2506          kfree(head);          kfree(head);
2507          head = NULL;          head = NULL;
2508          file->private_data = NULL;          file->private_data = NULL;
2509          if (is_write) {          if (is_write)
2510                  ccs_need_gc = 1;                  ccs_run_gc();
                 wake_up(&ccs_gc_queue);  
         }  
2511          return 0;          return 0;
2512  }  }
2513    

Legend:
Removed from v.3528  
changed lines
  Added in v.3808

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