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

Subversion リポジトリの参照

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

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

branches/ccs-patch/fs/tomoyo_domain.c revision 2738 by kumaneko, Tue Jul 7 01:28:46 2009 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/domain.c revision 2985 by kumaneko, Thu Sep 3 08:27:32 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * fs/tomoyo_domain.c   * security/ccsecurity/domain.c
  *  
  * Implementation of the Domain-Based Mandatory Access Control.  
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2009  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-pre   2009/07/03   * Version: 1.7.0   2009/09/03
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.
10   *   *
11   */   */
12    
13  #include <linux/ccs_common.h>  #include <linux/slab.h>
 #include <linux/tomoyo.h>  
 #include <linux/realpath.h>  
14  #include <linux/highmem.h>  #include <linux/highmem.h>
15    #include <linux/version.h>
16  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
17  #include <linux/namei.h>  #include <linux/namei.h>
18  #include <linux/mount.h>  #include <linux/mount.h>
# Line 23  Line 20 
20  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
21  #include <linux/fs_struct.h>  #include <linux/fs_struct.h>
22  #endif  #endif
23    #include "internal.h"
24    
25  /* For compatibility with older kernels. */  /* For compatibility with older kernels. */
26  #ifndef for_each_process  #ifndef for_each_process
# Line 38  struct ccs_domain_info ccs_kernel_domain Line 36  struct ccs_domain_info ccs_kernel_domain
36  LIST_HEAD(ccs_domain_list);  LIST_HEAD(ccs_domain_list);
37    
38  /**  /**
  * ccs_get_last_name - Get last component of a domainname.  
  *  
  * @domain: Pointer to "struct ccs_domain_info".  
  *  
  * Returns the last component of the domainname.  
  */  
 const char *ccs_get_last_name(const struct ccs_domain_info *domain)  
 {  
         const char *cp0 = domain->domainname->name;  
         const char *cp1 = strrchr(cp0, ' ');  
         if (cp1)  
                 return cp1 + 1;  
         return cp0;  
 }  
   
 /**  
  * ccs_add_domain_acl - Add the given ACL to the given domain.  
  *  
  * @domain: Pointer to "struct ccs_domain_info". May be NULL.  
  * @acl:    Pointer to "struct ccs_acl_info".  
  *  
  * Returns 0.  
  */  
 int ccs_add_domain_acl(struct ccs_domain_info *domain, struct ccs_acl_info *acl)  
 {  
         if (domain) {  
                 if (acl->cond)  
                         atomic_inc(&acl->cond->users);  
                 list_add_tail_rcu(&acl->list, &domain->acl_info_list);  
         } else {  
                 acl->type &= ~ACL_DELETED;  
         }  
         return 0;  
 }  
   
 /**  
  * ccs_del_domain_acl - Delete the given ACL from the domain.  
  *  
  * @acl: Pointer to "struct ccs_acl_info". May be NULL.  
  *  
  * Returns 0.  
  */  
 int ccs_del_domain_acl(struct ccs_acl_info *acl)  
 {  
         if (acl)  
                 acl->type |= ACL_DELETED;  
         return 0;  
 }  
   
 /**  
39   * ccs_audit_execute_handler_log - Audit execute_handler log.   * ccs_audit_execute_handler_log - Audit execute_handler log.
40   *   *
41   * @ee:         Pointer to "struct ccs_execve_entry".   * @ee:         Pointer to "struct ccs_execve_entry".
# Line 100  static int ccs_audit_execute_handler_log Line 48  static int ccs_audit_execute_handler_log
48  {  {
49          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
50          const char *handler = ee->handler->name;          const char *handler = ee->handler->name;
51          r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_FILE);          r->mode = ccs_get_mode(r->profile, CCS_MAC_FILE_EXECUTE);
52          return ccs_write_audit_log(true, r, "%s %s\n",          return ccs_write_audit_log(true, r, "%s %s\n",
53                                     is_default ? KEYWORD_EXECUTE_HANDLER :                                     is_default ? CCS_KEYWORD_EXECUTE_HANDLER :
54                                     KEYWORD_DENIED_EXECUTE_HANDLER, handler);                                     CCS_KEYWORD_DENIED_EXECUTE_HANDLER, handler);
55  }  }
56    
57  /**  /**
# Line 117  static int ccs_audit_domain_creation_log Line 65  static int ccs_audit_domain_creation_log
65  {  {
66          int error;          int error;
67          struct ccs_request_info r;          struct ccs_request_info r;
68          ccs_init_request_info(&r, domain, CCS_MAC_FOR_FILE);          ccs_init_request_info(&r, domain, CCS_MAC_FILE_EXECUTE);
69          error = ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile);          error = ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile);
70          return error;          return error;
71  }  }
# Line 142  static int ccs_update_domain_initializer Line 90  static int ccs_update_domain_initializer
90  {  {
91          struct ccs_domain_initializer_entry *entry = NULL;          struct ccs_domain_initializer_entry *entry = NULL;
92          struct ccs_domain_initializer_entry *ptr;          struct ccs_domain_initializer_entry *ptr;
93          const struct ccs_path_info *saved_program;          struct ccs_domain_initializer_entry e = { .is_not = is_not };
         const struct ccs_path_info *saved_domainname = NULL;  
94          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
         bool is_last_name = false;  
95          if (!ccs_is_correct_path(program, 1, -1, -1))          if (!ccs_is_correct_path(program, 1, -1, -1))
96                  return -EINVAL; /* No patterns allowed. */                  return -EINVAL; /* No patterns allowed. */
97          if (domainname) {          if (domainname) {
98                  if (!ccs_is_domain_def(domainname) &&                  if (!ccs_is_domain_def(domainname) &&
99                      ccs_is_correct_path(domainname, 1, -1, -1))                      ccs_is_correct_path(domainname, 1, -1, -1))
100                          is_last_name = true;                          e.is_last_name = true;
101                  else if (!ccs_is_correct_domain(domainname))                  else if (!ccs_is_correct_domain(domainname))
102                          return -EINVAL;                          return -EINVAL;
103                  saved_domainname = ccs_get_name(domainname);                  e.domainname = ccs_get_name(domainname);
104                  if (!saved_domainname)                  if (!e.domainname)
105                          return -ENOMEM;                          goto out;
         }  
         saved_program = ccs_get_name(program);  
         if (!saved_program) {  
                 ccs_put_name(saved_domainname);  
                 return -ENOMEM;  
106          }          }
107            e.program = ccs_get_name(program);
108            if (!e.program)
109                    goto out;
110          if (!is_delete)          if (!is_delete)
111                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
112          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
113          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {
114                  if (ptr->is_not != is_not ||                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not),
115                      ptr->domainname != saved_domainname ||                                 sizeof(e)))
                     ptr->program != saved_program)  
116                          continue;                          continue;
117                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
118                  error = 0;                  error = 0;
119                  break;                  break;
120          }          }
121          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
                 entry->domainname = saved_domainname;  
                 saved_domainname = NULL;  
                 entry->program = saved_program;  
                 saved_program = NULL;  
                 entry->is_not = is_not;  
                 entry->is_last_name = is_last_name;  
122                  list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);                  list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);
123                  entry = NULL;                  entry = NULL;
124                  error = 0;                  error = 0;
125          }          }
126          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
127          ccs_put_name(saved_domainname);   out:
128          ccs_put_name(saved_program);          ccs_put_name(e.domainname);
129            ccs_put_name(e.program);
130          kfree(entry);          kfree(entry);
131          return error;          return error;
132  }  }
# Line 200  static int ccs_update_domain_initializer Line 138  static int ccs_update_domain_initializer
138   *   *
139   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
140   *   *
141   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
142   */   */
143  bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head)  bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head)
144  {  {
# Line 213  bool ccs_read_domain_initializer_policy( Line 151  bool ccs_read_domain_initializer_policy(
151                  const char *domain = "";                  const char *domain = "";
152                  struct ccs_domain_initializer_entry *ptr;                  struct ccs_domain_initializer_entry *ptr;
153                  ptr = list_entry(pos, struct ccs_domain_initializer_entry,                  ptr = list_entry(pos, struct ccs_domain_initializer_entry,
154                                    list);                                   list);
155                  if (ptr->is_deleted)                  if (ptr->is_deleted)
156                          continue;                          continue;
157                  no = ptr->is_not ? "no_" : "";                  no = ptr->is_not ? "no_" : "";
# Line 221  bool ccs_read_domain_initializer_policy( Line 159  bool ccs_read_domain_initializer_policy(
159                          from = " from ";                          from = " from ";
160                          domain = ptr->domainname->name;                          domain = ptr->domainname->name;
161                  }                  }
162                  done = ccs_io_printf(head,                  done = ccs_io_printf(head, "%s" CCS_KEYWORD_INITIALIZE_DOMAIN
163                                       "%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n",                                       "%s%s%s\n", no, ptr->program->name, from,
164                                       no, ptr->program->name, from, domain);                                       domain);
165                  if (!done)                  if (!done)
166                          break;                          break;
167          }          }
# Line 262  int ccs_write_domain_initializer_policy( Line 200  int ccs_write_domain_initializer_policy(
200   * Returns true if executing @program reinitializes domain transition,   * Returns true if executing @program reinitializes domain transition,
201   * false otherwise.   * false otherwise.
202   *   *
203   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
204   */   */
205  static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname,  static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname,
206                                        const struct ccs_path_info *program,                                        const struct ccs_path_info *program,
# Line 313  static int ccs_update_domain_keeper_entr Line 251  static int ccs_update_domain_keeper_entr
251  {  {
252          struct ccs_domain_keeper_entry *entry = NULL;          struct ccs_domain_keeper_entry *entry = NULL;
253          struct ccs_domain_keeper_entry *ptr;          struct ccs_domain_keeper_entry *ptr;
254          const struct ccs_path_info *saved_domainname;          struct ccs_domain_keeper_entry e = { .is_not = is_not };
         const struct ccs_path_info *saved_program = NULL;  
255          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
         bool is_last_name = false;  
256          if (!ccs_is_domain_def(domainname) &&          if (!ccs_is_domain_def(domainname) &&
257              ccs_is_correct_path(domainname, 1, -1, -1))              ccs_is_correct_path(domainname, 1, -1, -1))
258                  is_last_name = true;                  e.is_last_name = true;
259          else if (!ccs_is_correct_domain(domainname))          else if (!ccs_is_correct_domain(domainname))
260                  return -EINVAL;                  return -EINVAL;
261          if (program) {          if (program) {
262                  if (!ccs_is_correct_path(program, 1, -1, -1))                  if (!ccs_is_correct_path(program, 1, -1, -1))
263                          return -EINVAL;                          return -EINVAL;
264                  saved_program = ccs_get_name(program);                  e.program = ccs_get_name(program);
265                  if (!saved_program)                  if (!e.program)
266                          return -ENOMEM;                          goto out;
         }  
         saved_domainname = ccs_get_name(domainname);  
         if (!saved_domainname) {  
                 ccs_put_name(saved_program);  
                 return -ENOMEM;  
267          }          }
268            e.domainname = ccs_get_name(domainname);
269            if (!e.domainname)
270                    goto out;
271          if (!is_delete)          if (!is_delete)
272                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
273          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
274          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {
275                  if (ptr->is_not != is_not ||                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not),
276                      ptr->domainname != saved_domainname ||                                 sizeof(e)))
                     ptr->program != saved_program)  
277                          continue;                          continue;
278                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
279                  error = 0;                  error = 0;
280                  break;                  break;
281          }          }
282          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
                 entry->domainname = saved_domainname;  
                 saved_domainname = NULL;  
                 entry->program = saved_program;  
                 saved_program = NULL;  
                 entry->is_not = is_not;  
                 entry->is_last_name = is_last_name;  
283                  list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list);                  list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list);
284                  entry = NULL;                  entry = NULL;
285                  error = 0;                  error = 0;
286          }          }
287          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
288          ccs_put_name(saved_domainname);   out:
289          ccs_put_name(saved_program);          ccs_put_name(e.domainname);
290            ccs_put_name(e.program);
291          kfree(entry);          kfree(entry);
292          return error;          return error;
293  }  }
# Line 391  int ccs_write_domain_keeper_policy(char Line 319  int ccs_write_domain_keeper_policy(char
319   *   *
320   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
321   *   *
322   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
323   */   */
324  bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head)  bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head)
325  {  {
# Line 411  bool ccs_read_domain_keeper_policy(struc Line 339  bool ccs_read_domain_keeper_policy(struc
339                          from = " from ";                          from = " from ";
340                          program = ptr->program->name;                          program = ptr->program->name;
341                  }                  }
342                  done = ccs_io_printf(head,                  done = ccs_io_printf(head, "%s" CCS_KEYWORD_KEEP_DOMAIN
343                                       "%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no,                                       "%s%s%s\n", no, program, from,
344                                       program, from, ptr->domainname->name);                                       ptr->domainname->name);
345                  if (!done)                  if (!done)
346                          break;                          break;
347          }          }
# Line 430  bool ccs_read_domain_keeper_policy(struc Line 358  bool ccs_read_domain_keeper_policy(struc
358   * Returns true if executing @program supresses domain transition,   * Returns true if executing @program supresses domain transition,
359   * false otherwise.   * false otherwise.
360   *   *
361   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
362   */   */
363  static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname,  static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname,
364                                   const struct ccs_path_info *program,                                   const struct ccs_path_info *program,
# Line 477  static int ccs_update_aggregator_entry(c Line 405  static int ccs_update_aggregator_entry(c
405  {  {
406          struct ccs_aggregator_entry *entry = NULL;          struct ccs_aggregator_entry *entry = NULL;
407          struct ccs_aggregator_entry *ptr;          struct ccs_aggregator_entry *ptr;
408          const struct ccs_path_info *saved_original_name;          struct ccs_aggregator_entry e = { };
         const struct ccs_path_info *saved_aggregated_name;  
409          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
410          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||
411              !ccs_is_correct_path(aggregated_name, 1, -1, -1))              !ccs_is_correct_path(aggregated_name, 1, -1, -1))
412                  return -EINVAL;                  return -EINVAL;
413          saved_original_name = ccs_get_name(original_name);          e.original_name = ccs_get_name(original_name);
414          saved_aggregated_name = ccs_get_name(aggregated_name);          e.aggregated_name = ccs_get_name(aggregated_name);
415          if (!saved_original_name || !saved_aggregated_name) {          if (!e.original_name || !e.aggregated_name)
416                  ccs_put_name(saved_original_name);                  goto out;
                 ccs_put_name(saved_aggregated_name);  
                 return -ENOMEM;  
         }  
417          if (!is_delete)          if (!is_delete)
418                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
419          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
420          list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {          list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {
421                  if (ptr->original_name != saved_original_name ||                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), original_name),
422                      ptr->aggregated_name != saved_aggregated_name)                                 sizeof(e)))
423                          continue;                          continue;
424                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
425                  error = 0;                  error = 0;
426                  break;                  break;
427          }          }
428          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
                 entry->original_name = saved_original_name;  
                 saved_original_name = NULL;  
                 entry->aggregated_name = saved_aggregated_name;  
                 saved_aggregated_name = NULL;  
429                  list_add_tail_rcu(&entry->list, &ccs_aggregator_list);                  list_add_tail_rcu(&entry->list, &ccs_aggregator_list);
430                  entry = NULL;                  entry = NULL;
431                  error = 0;                  error = 0;
432          }          }
433          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
434          ccs_put_name(saved_original_name);   out:
435          ccs_put_name(saved_aggregated_name);          ccs_put_name(e.original_name);
436            ccs_put_name(e.aggregated_name);
437          kfree(entry);          kfree(entry);
438          return error;          return error;
439  }  }
# Line 524  static int ccs_update_aggregator_entry(c Line 445  static int ccs_update_aggregator_entry(c
445   *   *
446   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
447   *   *
448   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
449   */   */
450  bool ccs_read_aggregator_policy(struct ccs_io_buffer *head)  bool ccs_read_aggregator_policy(struct ccs_io_buffer *head)
451  {  {
# Line 535  bool ccs_read_aggregator_policy(struct c Line 456  bool ccs_read_aggregator_policy(struct c
456                  ptr = list_entry(pos, struct ccs_aggregator_entry, list);                  ptr = list_entry(pos, struct ccs_aggregator_entry, list);
457                  if (ptr->is_deleted)                  if (ptr->is_deleted)
458                          continue;                          continue;
459                  done = ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n",                  done = ccs_io_printf(head, CCS_KEYWORD_AGGREGATOR "%s %s\n",
460                                       ptr->original_name->name,                                       ptr->original_name->name,
461                                       ptr->aggregated_name->name);                                       ptr->aggregated_name->name);
462                  if (!done)                  if (!done)
# Line 554  bool ccs_read_aggregator_policy(struct c Line 475  bool ccs_read_aggregator_policy(struct c
475   */   */
476  int ccs_write_aggregator_policy(char *data, const bool is_delete)  int ccs_write_aggregator_policy(char *data, const bool is_delete)
477  {  {
478          char *cp = strchr(data, ' ');          char *w[2];
479          if (!cp)          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
480                  return -EINVAL;                  return -EINVAL;
481          *cp++ = '\0';          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
         return ccs_update_aggregator_entry(data, cp, is_delete);  
482  }  }
483    
484  /* Domain create/delete handler. */  /* Domain create/delete handler. */
# Line 607  struct ccs_domain_info *ccs_find_or_assi Line 527  struct ccs_domain_info *ccs_find_or_assi
527          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
528          const struct ccs_path_info *saved_domainname;          const struct ccs_path_info *saved_domainname;
529          bool found = false;          bool found = false;
530    
531          if (!ccs_is_correct_domain(domainname))          if (!ccs_is_correct_domain(domainname))
532                  return NULL;                  return NULL;
533          saved_domainname = ccs_get_name(domainname);          saved_domainname = ccs_get_name(domainname);
# Line 639  struct ccs_domain_info *ccs_find_or_assi Line 559  struct ccs_domain_info *ccs_find_or_assi
559  }  }
560    
561  /**  /**
  * ccs_get_argv0 - Get argv[0].  
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_get_argv0(struct ccs_execve_entry *ee)  
 {  
         struct linux_binprm *bprm = ee->bprm;  
         char *arg_ptr = ee->tmp;  
         int arg_len = 0;  
         unsigned long pos = bprm->p;  
         int offset = pos % PAGE_SIZE;  
         bool done = false;  
         if (!bprm->argc)  
                 goto out;  
         while (1) {  
                 if (!ccs_dump_page(bprm, pos, &ee->dump))  
                         goto out;  
                 pos += PAGE_SIZE - offset;  
                 /* Read. */  
                 while (offset < PAGE_SIZE) {  
                         const char *kaddr = ee->dump.data;  
                         const unsigned char c = kaddr[offset++];  
                         if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {  
                                 if (c == '\\') {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = '\\';  
                                 } else if (c == '/') {  
                                         arg_len = 0;  
                                 } else if (c > ' ' && c < 127) {  
                                         arg_ptr[arg_len++] = c;  
                                 } else {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = (c >> 6) + '0';  
                                         arg_ptr[arg_len++]  
                                                 = ((c >> 3) & 7) + '0';  
                                         arg_ptr[arg_len++] = (c & 7) + '0';  
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
                         }  
                 }  
                 offset = 0;  
                 if (done)  
                         break;  
         }  
         return true;  
  out:  
         return false;  
 }  
   
 /**  
562   * ccs_find_next_domain - Find a domain.   * ccs_find_next_domain - Find a domain.
563   *   *
564   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve_entry".
565   *   *
566   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
567   *   *
568   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
569   */   */
570  static int ccs_find_next_domain(struct ccs_execve_entry *ee)  static int ccs_find_next_domain(struct ccs_execve_entry *ee)
571  {  {
# Line 709  static int ccs_find_next_domain(struct c Line 574  static int ccs_find_next_domain(struct c
574          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
575          const char *old_domain_name = r->domain->domainname->name;          const char *old_domain_name = r->domain->domainname->name;
576          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
         const u8 mode = r->mode;  
         const bool is_enforce = (mode == 3);  
577          const u32 ccs_flags = current->ccs_flags;          const u32 ccs_flags = current->ccs_flags;
578          char *new_domain_name = NULL;          struct ccs_path_info rn = { }; /* real name */
         struct ccs_path_info rn; /* real name */  
579          struct ccs_path_info ln; /* last name */          struct ccs_path_info ln; /* last name */
580          int retval;          int retval;
581            bool need_kfree = false;
582            ln.name = ccs_last_word(old_domain_name);
583            ccs_fill_path_info(&ln);
584   retry:   retry:
585          current->ccs_flags = ccs_flags;          current->ccs_flags = ccs_flags;
586          r->cond = NULL;          r->cond = NULL;
587            if (need_kfree) {
588                    kfree(rn.name);
589                    need_kfree = false;
590            }
591    
592          /* Get symlink's pathname of program. */          /* Get symlink's pathname of program. */
593          retval = ccs_symlink_path(bprm->filename, ee);          retval = ccs_symlink_path(bprm->filename, &rn);
594          if (retval < 0)          if (retval < 0)
595                  goto out;                  goto out;
596            need_kfree = true;
         rn.name = ee->program_path;  
         ccs_fill_path_info(&rn);  
         ln.name = ccs_get_last_name(r->domain);  
         ccs_fill_path_info(&ln);  
597    
598          if (handler) {          if (handler) {
599                  if (ccs_pathcmp(&rn, handler)) {                  if (ccs_pathcmp(&rn, handler)) {
# Line 740  static int ccs_find_next_domain(struct c Line 606  static int ccs_find_next_domain(struct c
606                          }                          }
607                          goto out;                          goto out;
608                  }                  }
609                  goto calculate_domain;          } else {
         }  
   
         /* Compare basename of program_path and argv[0] */  
         r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_ARGV0);  
         if (bprm->argc > 0 && r->mode) {  
                 char *base_argv0 = ee->tmp;  
                 const char *base_filename;  
                 retval = -ENOMEM;  
                 if (!ccs_get_argv0(ee))  
                         goto out;  
                 base_filename = strrchr(ee->program_path, '/');  
                 if (!base_filename)  
                         base_filename = ee->program_path;  
                 else  
                         base_filename++;  
                 if (strcmp(base_argv0, base_filename)) {  
                         retval = ccs_check_argv0_perm(r, &rn, base_argv0);  
                         if (retval == 1)  
                                 goto retry;  
                         if (retval < 0)  
                                 goto out;  
                 }  
         }  
   
         /* Check 'aggregator' directive. */  
         {  
610                  struct ccs_aggregator_entry *ptr;                  struct ccs_aggregator_entry *ptr;
611                  /* Is this program allowed to be aggregated? */                  /* Check 'aggregator' directive. */
612                  list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {                  list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {
613                          if (ptr->is_deleted ||                          if (ptr->is_deleted ||
614                              !ccs_path_matches_pattern(&rn, ptr->original_name))                              !ccs_path_matches_pattern(&rn, ptr->original_name))
615                                  continue;                                  continue;
616                          strncpy(ee->program_path, ptr->aggregated_name->name,                          kfree(rn.name);
617                                  CCS_MAX_PATHNAME_LEN - 1);                          need_kfree = false;
618                          ccs_fill_path_info(&rn);                          /* This is OK because it is read only. */
619                            rn = *ptr->aggregated_name;
620                          break;                          break;
621                  }                  }
         }  
622    
623          /* Check execute permission. */                  /* Check execute permission. */
624          r->mode = mode;                  retval = ccs_exec_perm(r, &rn);
625          retval = ccs_check_exec_perm(r, &rn);                  if (retval == 1)
626          if (retval == 1)                          goto retry;
627                  goto retry;                  if (retval < 0)
628          if (retval < 0)                          goto out;
629                  goto out;          }
630    
631   calculate_domain:          /* Calculate domain to transit to. */
         new_domain_name = ee->tmp;  
632          if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) {          if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) {
633                  /* Transit to the child of ccs_kernel_domain domain. */                  /* Transit to the child of ccs_kernel_domain domain. */
634                  snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, ROOT_NAME " " "%s",
635                           ROOT_NAME " " "%s", ee->program_path);                           rn.name);
636          } else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) {          } else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) {
637                  /*                  /*
638                   * Needn't to transit from kernel domain before starting                   * Needn't to transit from kernel domain before starting
# Line 806  static int ccs_find_next_domain(struct c Line 645  static int ccs_find_next_domain(struct c
645                  domain = r->domain;                  domain = r->domain;
646          } else {          } else {
647                  /* Normal domain transition. */                  /* Normal domain transition. */
648                  snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
649                           "%s %s", old_domain_name, ee->program_path);                           old_domain_name, rn.name);
650          }          }
651          if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN)          if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10)
652                  goto done;                  goto done;
653          domain = ccs_find_domain(new_domain_name);          domain = ccs_find_domain(ee->tmp);
654          if (domain)          if (domain)
655                  goto done;                  goto done;
656          if (is_enforce) {          if (r->mode == CCS_CONFIG_ENFORCING) {
657                  int error = ccs_check_supervisor(r,                  int error = ccs_supervisor(r, "# wants to create domain\n"
658                                                   "# wants to create domain\n"                                             "%s\n", ee->tmp);
                                                  "%s\n", new_domain_name);  
659                  if (error == 1)                  if (error == 1)
660                          goto retry;                          goto retry;
661                  if (error < 0)                  if (error < 0)
662                          goto done;                          goto done;
663          }          }
664          domain = ccs_find_or_assign_new_domain(new_domain_name, r->profile);          domain = ccs_find_or_assign_new_domain(ee->tmp, r->profile);
665          if (domain)          if (domain)
666                  ccs_audit_domain_creation_log(r->domain);                  ccs_audit_domain_creation_log(r->domain);
667   done:   done:
668          if (!domain) {          if (!domain) {
669                  printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",                  printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n",
670                         new_domain_name);                         ee->tmp);
671                  if (is_enforce)                  if (r->mode == CCS_CONFIG_ENFORCING)
672                          retval = -EPERM;                          retval = -EPERM;
673                  else {                  else {
674                          retval = 0;                          retval = 0;
# Line 841  static int ccs_find_next_domain(struct c Line 679  static int ccs_find_next_domain(struct c
679          }          }
680   out:   out:
681          if (domain)          if (domain)
682                  r->domain = domain;                  r->domain = domain;
683            if (need_kfree)
684                    kfree(rn.name);
685          return retval;          return retval;
686  }  }
687    
688  /**  /**
689   * ccs_check_environ - Check permission for environment variable names.   * ccs_environ - Check permission for environment variable names.
690   *   *
691   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve_entry".
692   *   *
693   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
694   */   */
695  static int ccs_check_environ(struct ccs_execve_entry *ee)  static int ccs_environ(struct ccs_execve_entry *ee)
696  {  {
697          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
698          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
# Line 883  static int ccs_check_environ(struct ccs_ Line 723  static int ccs_check_environ(struct ccs_
723                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
724                          const char *kaddr = ee->dump.data;                          const char *kaddr = ee->dump.data;
725                          const unsigned char c = kaddr[offset++];                          const unsigned char c = kaddr[offset++];
726                          if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
727                                  if (c == '=') {                                  if (c == '=') {
728                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
729                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 903  static int ccs_check_environ(struct ccs_ Line 743  static int ccs_check_environ(struct ccs_
743                          }                          }
744                          if (c)                          if (c)
745                                  continue;                                  continue;
746                          if (ccs_check_env_perm(r, arg_ptr)) {                          if (ccs_env_perm(r, arg_ptr)) {
747                                  error = -EPERM;                                  error = -EPERM;
748                                  break;                                  break;
749                          }                          }
# Line 971  static void ccs_unescape(unsigned char * Line 811  static void ccs_unescape(unsigned char *
811  static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)  static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)
812  {  {
813          int depth = 0;          int depth = 0;
         /***** CRITICAL SECTION START *****/  
814          ccs_realpath_lock();          ccs_realpath_lock();
815          for (;;) {          for (;;) {
816                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
# Line 986  static inline int ccs_root_depth(struct Line 825  static inline int ccs_root_depth(struct
825                  depth++;                  depth++;
826          }          }
827          ccs_realpath_unlock();          ccs_realpath_unlock();
         /***** CRITICAL SECTION END *****/  
828          return depth;          return depth;
829  }  }
830    
# Line 1003  static int ccs_get_root_depth(void) Line 841  static int ccs_get_root_depth(void)
841  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
842          struct path root;          struct path root;
843  #endif  #endif
         /***** CRITICAL SECTION START *****/  
844          read_lock(&current->fs->lock);          read_lock(&current->fs->lock);
845  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
846          root = current->fs->root;          root = current->fs->root;
# Line 1015  static int ccs_get_root_depth(void) Line 852  static int ccs_get_root_depth(void)
852          vfsmnt = mntget(current->fs->rootmnt);          vfsmnt = mntget(current->fs->rootmnt);
853  #endif  #endif
854          read_unlock(&current->fs->lock);          read_unlock(&current->fs->lock);
         /***** CRITICAL SECTION END *****/  
855          depth = ccs_root_depth(dentry, vfsmnt);          depth = ccs_root_depth(dentry, vfsmnt);
856  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
857          path_put(&root);          path_put(&root);
# Line 1027  static int ccs_get_root_depth(void) Line 863  static int ccs_get_root_depth(void)
863  }  }
864    
865  static LIST_HEAD(ccs_execve_list);  static LIST_HEAD(ccs_execve_list);
866  static DEFINE_SPINLOCK(ccs_execve_list_lock);  DEFINE_SPINLOCK(ccs_execve_list_lock);
867    unsigned int ccs_in_execve_counter;
868    
869  /**  /**
870   * ccs_allocate_execve_entry - Allocate memory for execve().   * ccs_allocate_execve_entry - Allocate memory for execve().
871   *   *
872   * Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise.   * Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise.
873   */   */
874  static struct ccs_execve_entry *ccs_allocate_execve_entry(void)  static inline struct ccs_execve_entry *ccs_allocate_execve_entry(void)
875  {  {
876          struct ccs_execve_entry *ee = kzalloc(sizeof(*ee), GFP_KERNEL);          struct ccs_execve_entry *ee = kzalloc(sizeof(*ee), GFP_KERNEL);
877          if (!ee)          if (!ee)
878                  return NULL;                  return NULL;
         ee->program_path = kzalloc(CCS_MAX_PATHNAME_LEN, GFP_KERNEL);  
879          ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);          ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
880          if (!ee->program_path || !ee->tmp) {          if (!ee->tmp) {
                 kfree(ee->program_path);  
                 kfree(ee->tmp);  
881                  kfree(ee);                  kfree(ee);
882                  return NULL;                  return NULL;
883          }          }
884          ee->srcu_idx = srcu_read_lock(&ccs_ss);          ee->reader_idx = ccs_read_lock();
885          /* ee->dump->data is allocated by ccs_dump_page(). */          /* ee->dump->data is allocated by ccs_dump_page(). */
886          ee->task = current;          ee->task = current;
887          /***** CRITICAL SECTION START *****/          ee->previous_domain = ee->task->ccs_domain_info;
888          spin_lock(&ccs_execve_list_lock);          spin_lock(&ccs_execve_list_lock);
889            ccs_in_execve_counter++;
890          list_add(&ee->list, &ccs_execve_list);          list_add(&ee->list, &ccs_execve_list);
891          spin_unlock(&ccs_execve_list_lock);          spin_unlock(&ccs_execve_list_lock);
         /***** CRITICAL SECTION END *****/  
         return ee;  
 }  
   
 /**  
  * ccs_find_execve_entry - Find ccs_execve_entry of current process.  
  *  
  * Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise.  
  */  
 static struct ccs_execve_entry *ccs_find_execve_entry(void)  
 {  
         struct task_struct *task = current;  
         struct ccs_execve_entry *ee = NULL;  
         struct ccs_execve_entry *p;  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&ccs_execve_list_lock);  
         list_for_each_entry(p, &ccs_execve_list, list) {  
                 if (p->task != task)  
                         continue;  
                 ee = p;  
                 break;  
         }  
         spin_unlock(&ccs_execve_list_lock);  
         /***** CRITICAL SECTION END *****/  
892          return ee;          return ee;
893  }  }
894    
# Line 1086  static struct ccs_execve_entry *ccs_find Line 897  static struct ccs_execve_entry *ccs_find
897   *   *
898   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve_entry".
899   */   */
900  static void ccs_free_execve_entry(struct ccs_execve_entry *ee)  static inline void ccs_free_execve_entry(struct ccs_execve_entry *ee)
901  {  {
902          if (!ee)          if (!ee)
903                  return;                  return;
         /***** CRITICAL SECTION START *****/  
904          spin_lock(&ccs_execve_list_lock);          spin_lock(&ccs_execve_list_lock);
905          list_del(&ee->list);          list_del(&ee->list);
906            ccs_in_execve_counter--;
907          spin_unlock(&ccs_execve_list_lock);          spin_unlock(&ccs_execve_list_lock);
908          /***** CRITICAL SECTION END *****/          kfree(ee->handler_path);
         kfree(ee->program_path);  
909          kfree(ee->tmp);          kfree(ee->tmp);
910          kfree(ee->dump.data);          kfree(ee->dump.data);
911          srcu_read_unlock(&ccs_ss, ee->srcu_idx);          ccs_read_unlock(ee->reader_idx);
912          kfree(ee);          kfree(ee);
913  }  }
914    
# Line 1221  static int ccs_try_alt_exec(struct ccs_e Line 1031  static int ccs_try_alt_exec(struct ccs_e
1031                          kfree(exe);                          kfree(exe);
1032                  } else {                  } else {
1033                          exe = ee->tmp;                          exe = ee->tmp;
1034                          strncpy(ee->tmp, "<unknown>", CCS_EXEC_TMPSIZE - 1);                          snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
1035                          retval = copy_strings_kernel(1, &exe, bprm);                          retval = copy_strings_kernel(1, &exe, bprm);
1036                  }                  }
1037                  if (retval < 0)                  if (retval < 0)
# Line 1232  static int ccs_try_alt_exec(struct ccs_e Line 1042  static int ccs_try_alt_exec(struct ccs_e
1042          /* Set argv[1] */          /* Set argv[1] */
1043          {          {
1044                  char *cp = ee->tmp;                  char *cp = ee->tmp;
1045                  strncpy(ee->tmp, ccs_current_domain()->domainname->name,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
1046                          CCS_EXEC_TMPSIZE - 1);                           ccs_current_domain()->domainname->name);
1047                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1048                  if (retval < 0)                  if (retval < 0)
1049                          goto out;                          goto out;
# Line 1243  static int ccs_try_alt_exec(struct ccs_e Line 1053  static int ccs_try_alt_exec(struct ccs_e
1053          /* Set argv[0] */          /* Set argv[0] */
1054          {          {
1055                  int depth = ccs_get_root_depth();                  int depth = ccs_get_root_depth();
1056                  char *cp = ee->program_path;                  int len = ee->handler->total_len + 1;
1057                  strncpy(cp, ee->handler->name, CCS_MAX_PATHNAME_LEN - 1);                  char *cp = kmalloc(len, GFP_KERNEL);
1058                    if (!cp) {
1059                            retval = ENOMEM;
1060                            goto out;
1061                    }
1062                    ee->handler_path = cp;
1063                    memmove(cp, ee->handler->name, len);
1064                  ccs_unescape(cp);                  ccs_unescape(cp);
1065                  retval = -ENOENT;                  retval = -ENOENT;
1066                  if (!*cp || *cp != '/')                  if (!*cp || *cp != '/')
# Line 1256  static int ccs_try_alt_exec(struct ccs_e Line 1072  static int ccs_try_alt_exec(struct ccs_e
1072                                  goto out;                                  goto out;
1073                          depth--;                          depth--;
1074                  }                  }
1075                  memmove(ee->program_path, cp, strlen(cp) + 1);                  memmove(ee->handler_path, cp, strlen(cp) + 1);
1076                  cp = ee->program_path;                  cp = ee->handler_path;
1077                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1078                  if (retval < 0)                  if (retval < 0)
1079                          goto out;                          goto out;
# Line 1270  static int ccs_try_alt_exec(struct ccs_e Line 1086  static int ccs_try_alt_exec(struct ccs_e
1086  #endif  #endif
1087    
1088          /* OK, now restart the process with execute handler program's dentry. */          /* OK, now restart the process with execute handler program's dentry. */
1089          filp = open_exec(ee->program_path);          filp = open_exec(ee->handler_path);
1090          if (IS_ERR(filp)) {          if (IS_ERR(filp)) {
1091                  retval = PTR_ERR(filp);                  retval = PTR_ERR(filp);
1092                  goto out;                  goto out;
1093          }          }
1094          bprm->file = filp;          bprm->file = filp;
1095          bprm->filename = ee->program_path;          bprm->filename = ee->handler_path;
1096  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1097          bprm->interp = bprm->filename;          bprm->interp = bprm->filename;
1098  #endif  #endif
1099          retval = prepare_binprm(bprm);          retval = prepare_binprm(bprm);
1100          if (retval < 0)          if (retval < 0)
1101                  goto out;                  goto out;
1102          {          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1103                  /*          retval = ccs_find_next_domain(ee);
1104                   * Backup ee->program_path because ccs_find_next_domain() will          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
                  * overwrite ee->program_path and ee->tmp.  
                  */  
                 const int len = strlen(ee->program_path) + 1;  
                 char *cp = kzalloc(len, GFP_KERNEL);  
                 if (!cp) {  
                         retval = -ENOMEM;  
                         goto out;  
                 }  
                 memmove(cp, ee->program_path, len);  
                 bprm->filename = cp;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  
                 bprm->interp = bprm->filename;  
 #endif  
                 task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
                 retval = ccs_find_next_domain(ee);  
                 task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
                 /* Restore ee->program_path for search_binary_handler(). */  
                 memmove(ee->program_path, cp, len);  
                 bprm->filename = ee->program_path;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  
                 bprm->interp = bprm->filename;  
 #endif  
                 kfree(cp);  
         }  
1105   out:   out:
1106          return retval;          return retval;
1107  }  }
# Line 1322  static int ccs_try_alt_exec(struct ccs_e Line 1114  static int ccs_try_alt_exec(struct ccs_e
1114   *   *
1115   * Returns true if found, false otherwise.   * Returns true if found, false otherwise.
1116   *   *
1117   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
1118   */   */
1119  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,
1120                                       const u8 type)                                       const u8 type)
# Line 1401  bool ccs_dump_page(struct linux_binprm * Line 1193  bool ccs_dump_page(struct linux_binprm *
1193  }  }
1194    
1195  /**  /**
  * ccs_fetch_next_domain - Fetch next_domain from the list.  
  *  
  * Returns pointer to "struct ccs_domain_info" which will be used if execve()  
  * succeeds. This function does not return NULL.  
  */  
 struct ccs_domain_info *ccs_fetch_next_domain(void)  
 {  
         struct ccs_execve_entry *ee = ccs_find_execve_entry();  
         struct ccs_domain_info *next_domain = NULL;  
         if (ee)  
                 next_domain = ee->r.domain;  
         if (!next_domain)  
                 next_domain = ccs_current_domain();  
         return next_domain;  
 }  
   
 /**  
1196   * ccs_start_execve - Prepare for execve() operation.   * ccs_start_execve - Prepare for execve() operation.
1197   *   *
1198   * @bprm: Pointer to "struct linux_binprm".   * @bprm: Pointer to "struct linux_binprm".
# Line 1433  int ccs_start_execve(struct linux_binprm Line 1208  int ccs_start_execve(struct linux_binprm
1208                  ccs_load_policy(bprm->filename);                  ccs_load_policy(bprm->filename);
1209          if (!ee)          if (!ee)
1210                  return -ENOMEM;                  return -ENOMEM;
1211          ccs_init_request_info(&ee->r, NULL, CCS_MAC_FOR_FILE);          ccs_init_request_info(&ee->r, NULL, CCS_MAC_FILE_EXECUTE);
1212          ee->r.ee = ee;          ee->r.ee = ee;
1213          ee->bprm = bprm;          ee->bprm = bprm;
1214          ee->r.obj = &ee->obj;          ee->r.obj = &ee->obj;
1215          ee->obj.path1_dentry = bprm->file->f_dentry;          ee->obj.path1.dentry = bprm->file->f_dentry;
1216          ee->obj.path1_vfsmnt = bprm->file->f_vfsmnt;          ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1217          /* Clear manager flag. */          /* Clear manager flag. */
1218          task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;          task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;
1219          if (ccs_find_execute_handler(ee, TYPE_EXECUTE_HANDLER)) {          if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER)) {
1220                  retval = ccs_try_alt_exec(ee);                  retval = ccs_try_alt_exec(ee);
1221                  if (!retval)                  if (!retval)
1222                          ccs_audit_execute_handler_log(ee, true);                          ccs_audit_execute_handler_log(ee, true);
# Line 1450  int ccs_start_execve(struct linux_binprm Line 1225  int ccs_start_execve(struct linux_binprm
1225          retval = ccs_find_next_domain(ee);          retval = ccs_find_next_domain(ee);
1226          if (retval != -EPERM)          if (retval != -EPERM)
1227                  goto ok;                  goto ok;
1228          if (ccs_find_execute_handler(ee, TYPE_DENIED_EXECUTE_HANDLER)) {          if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) {
1229                  retval = ccs_try_alt_exec(ee);                  retval = ccs_try_alt_exec(ee);
1230                  if (!retval)                  if (!retval)
1231                          ccs_audit_execute_handler_log(ee, false);                          ccs_audit_execute_handler_log(ee, false);
# Line 1458  int ccs_start_execve(struct linux_binprm Line 1233  int ccs_start_execve(struct linux_binprm
1233   ok:   ok:
1234          if (retval < 0)          if (retval < 0)
1235                  goto out;                  goto out;
1236          ee->r.mode = ccs_check_flags(ee->r.domain, CCS_MAC_FOR_ENV);          /*
1237          retval = ccs_check_environ(ee);           * Proceed to the next domain in order to allow reaching via PID.
1238             * It will be reverted if execve() failed. Reverting is not good.
1239             * But it is better than being unable to reach via PID in interactive
1240             * enforcing mode.
1241             */
1242            task->ccs_domain_info = ee->r.domain;
1243            ee->r.mode = ccs_get_mode(ee->r.domain->profile, CCS_MAC_ENVIRON);
1244            retval = ccs_environ(ee);
1245          if (retval < 0)          if (retval < 0)
1246                  goto out;                  goto out;
1247          task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC;          task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC;
# Line 1475  int ccs_start_execve(struct linux_binprm Line 1257  int ccs_start_execve(struct linux_binprm
1257   *   *
1258   * @retval: Return code of an execve() operation.   * @retval: Return code of an execve() operation.
1259   *   *
1260   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
1261   */   */
1262  void ccs_finish_execve(int retval)  void ccs_finish_execve(int retval)
1263  {  {
1264          struct task_struct *task = current;          struct task_struct *task = current;
1265          struct ccs_execve_entry *ee = ccs_find_execve_entry();          struct ccs_execve_entry *ee = NULL;
1266            struct ccs_execve_entry *p;
1267          task->ccs_flags &= ~CCS_CHECK_READ_FOR_OPEN_EXEC;          task->ccs_flags &= ~CCS_CHECK_READ_FOR_OPEN_EXEC;
1268            spin_lock(&ccs_execve_list_lock);
1269            list_for_each_entry(p, &ccs_execve_list, list) {
1270                    if (p->task != task)
1271                            continue;
1272                    ee = p;
1273                    break;
1274            }
1275            spin_unlock(&ccs_execve_list_lock);
1276          if (!ee)          if (!ee)
1277                  return;                  return;
1278          if (retval < 0)          if (retval < 0) {
1279                    task->ccs_domain_info = ee->previous_domain;
1280                  goto out;                  goto out;
1281          /* Proceed to next domain if execution suceeded. */          }
         task->ccs_domain_info = ee->r.domain;  
1282          /* Mark the current process as execute handler. */          /* Mark the current process as execute handler. */
1283          if (ee->handler)          if (ee->handler)
1284                  task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;                  task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;

Legend:
Removed from v.2738  
changed lines
  Added in v.2985

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