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

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 2718 by kumaneko, Thu Jul 2 01:30:12 2009 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/domain.c revision 3261 by kumaneko, Thu Dec 17 04:03:23 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/05/28   * Version: 1.7.1   2009/11/11
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"
 /* For compatibility with older kernels. */  
 #ifndef for_each_process  
 #define for_each_process for_each_task  
 #endif  
24    
25  /* Variables definitions.*/  /* Variables definitions.*/
26    
# Line 38  struct ccs_domain_info ccs_kernel_domain Line 31  struct ccs_domain_info ccs_kernel_domain
31  LIST_HEAD(ccs_domain_list);  LIST_HEAD(ccs_domain_list);
32    
33  /**  /**
  * 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;  
 }  
   
 /**  
34   * ccs_audit_execute_handler_log - Audit execute_handler log.   * ccs_audit_execute_handler_log - Audit execute_handler log.
35   *   *
36   * @ee:         Pointer to "struct ccs_execve_entry".   * @ee:         Pointer to "struct ccs_execve_entry".
# Line 100  static int ccs_audit_execute_handler_log Line 43  static int ccs_audit_execute_handler_log
43  {  {
44          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
45          const char *handler = ee->handler->name;          const char *handler = ee->handler->name;
46          r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_FILE);          r->mode = ccs_get_mode(r->profile, CCS_MAC_FILE_EXECUTE);
47          return ccs_write_audit_log(true, r, "%s %s\n",          return ccs_write_audit_log(true, r, "%s %s\n",
48                                     is_default ? KEYWORD_EXECUTE_HANDLER :                                     is_default ? CCS_KEYWORD_EXECUTE_HANDLER :
49                                     KEYWORD_DENIED_EXECUTE_HANDLER, handler);                                     CCS_KEYWORD_DENIED_EXECUTE_HANDLER, handler);
50  }  }
51    
52  /**  /**
# Line 117  static int ccs_audit_domain_creation_log Line 60  static int ccs_audit_domain_creation_log
60  {  {
61          int error;          int error;
62          struct ccs_request_info r;          struct ccs_request_info r;
63          ccs_init_request_info(&r, domain, CCS_MAC_FOR_FILE);          ccs_init_request_info(&r, domain, CCS_MAC_FILE_EXECUTE);
64          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);
65          return error;          return error;
66  }  }
# Line 142  static int ccs_update_domain_initializer Line 85  static int ccs_update_domain_initializer
85  {  {
86          struct ccs_domain_initializer_entry *entry = NULL;          struct ccs_domain_initializer_entry *entry = NULL;
87          struct ccs_domain_initializer_entry *ptr;          struct ccs_domain_initializer_entry *ptr;
88          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;  
89          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
         bool is_last_name = false;  
90          if (!ccs_is_correct_path(program, 1, -1, -1))          if (!ccs_is_correct_path(program, 1, -1, -1))
91                  return -EINVAL; /* No patterns allowed. */                  return -EINVAL; /* No patterns allowed. */
92          if (domainname) {          if (domainname) {
93                  if (!ccs_is_domain_def(domainname) &&                  if (!ccs_is_domain_def(domainname) &&
94                      ccs_is_correct_path(domainname, 1, -1, -1))                      ccs_is_correct_path(domainname, 1, -1, -1))
95                          is_last_name = true;                          e.is_last_name = true;
96                  else if (!ccs_is_correct_domain(domainname))                  else if (!ccs_is_correct_domain(domainname))
97                          return -EINVAL;                          return -EINVAL;
98                  saved_domainname = ccs_get_name(domainname);                  e.domainname = ccs_get_name(domainname);
99                  if (!saved_domainname)                  if (!e.domainname)
100                          return -ENOMEM;                          goto out;
         }  
         saved_program = ccs_get_name(program);  
         if (!saved_program) {  
                 ccs_put_name(saved_domainname);  
                 return -ENOMEM;  
101          }          }
102            e.program = ccs_get_name(program);
103            if (!e.program)
104                    goto out;
105          if (!is_delete)          if (!is_delete)
106                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
107          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
108          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {
109                  if (ptr->is_not != is_not ||                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not),
110                      ptr->domainname != saved_domainname ||                                 sizeof(e)))
                     ptr->program != saved_program)  
111                          continue;                          continue;
112                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
113                  error = 0;                  error = 0;
114                  break;                  break;
115          }          }
116          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;  
117                  list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);                  list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);
118                  entry = NULL;                  entry = NULL;
119                  error = 0;                  error = 0;
120          }          }
121          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
122          ccs_put_name(saved_domainname);   out:
123          ccs_put_name(saved_program);          ccs_put_name(e.domainname);
124            ccs_put_name(e.program);
125          kfree(entry);          kfree(entry);
126          return error;          return error;
127  }  }
# Line 200  static int ccs_update_domain_initializer Line 133  static int ccs_update_domain_initializer
133   *   *
134   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
135   *   *
136   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
137   */   */
138  bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head)  bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head)
139  {  {
# Line 213  bool ccs_read_domain_initializer_policy( Line 146  bool ccs_read_domain_initializer_policy(
146                  const char *domain = "";                  const char *domain = "";
147                  struct ccs_domain_initializer_entry *ptr;                  struct ccs_domain_initializer_entry *ptr;
148                  ptr = list_entry(pos, struct ccs_domain_initializer_entry,                  ptr = list_entry(pos, struct ccs_domain_initializer_entry,
149                                    list);                                   list);
150                  if (ptr->is_deleted)                  if (ptr->is_deleted)
151                          continue;                          continue;
152                  no = ptr->is_not ? "no_" : "";                  no = ptr->is_not ? "no_" : "";
# Line 221  bool ccs_read_domain_initializer_policy( Line 154  bool ccs_read_domain_initializer_policy(
154                          from = " from ";                          from = " from ";
155                          domain = ptr->domainname->name;                          domain = ptr->domainname->name;
156                  }                  }
157                  done = ccs_io_printf(head,                  done = ccs_io_printf(head, "%s" CCS_KEYWORD_INITIALIZE_DOMAIN
158                                       "%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n",                                       "%s%s%s\n", no, ptr->program->name, from,
159                                       no, ptr->program->name, from, domain);                                       domain);
160                  if (!done)                  if (!done)
161                          break;                          break;
162          }          }
# Line 262  int ccs_write_domain_initializer_policy( Line 195  int ccs_write_domain_initializer_policy(
195   * Returns true if executing @program reinitializes domain transition,   * Returns true if executing @program reinitializes domain transition,
196   * false otherwise.   * false otherwise.
197   *   *
198   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
199   */   */
200  static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname,  static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname,
201                                        const struct ccs_path_info *program,                                        const struct ccs_path_info *program,
# Line 313  static int ccs_update_domain_keeper_entr Line 246  static int ccs_update_domain_keeper_entr
246  {  {
247          struct ccs_domain_keeper_entry *entry = NULL;          struct ccs_domain_keeper_entry *entry = NULL;
248          struct ccs_domain_keeper_entry *ptr;          struct ccs_domain_keeper_entry *ptr;
249          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;  
250          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
         bool is_last_name = false;  
251          if (!ccs_is_domain_def(domainname) &&          if (!ccs_is_domain_def(domainname) &&
252              ccs_is_correct_path(domainname, 1, -1, -1))              ccs_is_correct_path(domainname, 1, -1, -1))
253                  is_last_name = true;                  e.is_last_name = true;
254          else if (!ccs_is_correct_domain(domainname))          else if (!ccs_is_correct_domain(domainname))
255                  return -EINVAL;                  return -EINVAL;
256          if (program) {          if (program) {
257                  if (!ccs_is_correct_path(program, 1, -1, -1))                  if (!ccs_is_correct_path(program, 1, -1, -1))
258                          return -EINVAL;                          return -EINVAL;
259                  saved_program = ccs_get_name(program);                  e.program = ccs_get_name(program);
260                  if (!saved_program)                  if (!e.program)
261                          return -ENOMEM;                          goto out;
         }  
         saved_domainname = ccs_get_name(domainname);  
         if (!saved_domainname) {  
                 ccs_put_name(saved_program);  
                 return -ENOMEM;  
262          }          }
263            e.domainname = ccs_get_name(domainname);
264            if (!e.domainname)
265                    goto out;
266          if (!is_delete)          if (!is_delete)
267                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
268          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
269          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {
270                  if (ptr->is_not != is_not ||                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not),
271                      ptr->domainname != saved_domainname ||                                 sizeof(e)))
                     ptr->program != saved_program)  
272                          continue;                          continue;
273                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
274                  error = 0;                  error = 0;
275                  break;                  break;
276          }          }
277          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;  
278                  list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list);                  list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list);
279                  entry = NULL;                  entry = NULL;
280                  error = 0;                  error = 0;
281          }          }
282          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
283          ccs_put_name(saved_domainname);   out:
284          ccs_put_name(saved_program);          ccs_put_name(e.domainname);
285            ccs_put_name(e.program);
286          kfree(entry);          kfree(entry);
287          return error;          return error;
288  }  }
# Line 391  int ccs_write_domain_keeper_policy(char Line 314  int ccs_write_domain_keeper_policy(char
314   *   *
315   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
316   *   *
317   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
318   */   */
319  bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head)  bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head)
320  {  {
# Line 411  bool ccs_read_domain_keeper_policy(struc Line 334  bool ccs_read_domain_keeper_policy(struc
334                          from = " from ";                          from = " from ";
335                          program = ptr->program->name;                          program = ptr->program->name;
336                  }                  }
337                  done = ccs_io_printf(head,                  done = ccs_io_printf(head, "%s" CCS_KEYWORD_KEEP_DOMAIN
338                                       "%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no,                                       "%s%s%s\n", no, program, from,
339                                       program, from, ptr->domainname->name);                                       ptr->domainname->name);
340                  if (!done)                  if (!done)
341                          break;                          break;
342          }          }
# Line 430  bool ccs_read_domain_keeper_policy(struc Line 353  bool ccs_read_domain_keeper_policy(struc
353   * Returns true if executing @program supresses domain transition,   * Returns true if executing @program supresses domain transition,
354   * false otherwise.   * false otherwise.
355   *   *
356   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
357   */   */
358  static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname,  static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname,
359                                   const struct ccs_path_info *program,                                   const struct ccs_path_info *program,
# Line 459  static bool ccs_is_domain_keeper(const s Line 382  static bool ccs_is_domain_keeper(const s
382          return flag;          return flag;
383  }  }
384    
 /* The list for "struct ccs_alias_entry". */  
 LIST_HEAD(ccs_alias_list);  
   
 /**  
  * ccs_update_alias_entry - Update "struct ccs_alias_entry" list.  
  *  
  * @original_name: The original program's real name.  
  * @aliased_name:  The symbolic program's symbolic link's name.  
  * @is_delete:     True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_update_alias_entry(const char *original_name,  
                                   const char *aliased_name,  
                                   const bool is_delete)  
 {  
         struct ccs_alias_entry *entry = NULL;  
         struct ccs_alias_entry *ptr;  
         const struct ccs_path_info *saved_original_name;  
         const struct ccs_path_info *saved_aliased_name;  
         int error = is_delete ? -ENOENT : -ENOMEM;  
         if (!ccs_is_correct_path(original_name, 1, -1, -1) ||  
             !ccs_is_correct_path(aliased_name, 1, -1, -1))  
                 return -EINVAL; /* No patterns allowed. */  
         saved_original_name = ccs_get_name(original_name);  
         saved_aliased_name = ccs_get_name(aliased_name);  
         if (!saved_original_name || !saved_aliased_name) {  
                 ccs_put_name(saved_original_name);  
                 ccs_put_name(saved_aliased_name);  
                 return -ENOMEM;  
         }  
         if (!is_delete)  
                 entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
         mutex_lock(&ccs_policy_lock);  
         list_for_each_entry_rcu(ptr, &ccs_alias_list, list) {  
                 if (ptr->original_name != saved_original_name ||  
                     ptr->aliased_name != saved_aliased_name)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {  
                 entry->original_name = saved_original_name;  
                 saved_original_name = NULL;  
                 entry->aliased_name = saved_aliased_name;  
                 saved_aliased_name = NULL;  
                 list_add_tail_rcu(&entry->list, &ccs_alias_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
         ccs_put_name(saved_original_name);  
         ccs_put_name(saved_aliased_name);  
         kfree(entry);  
         return error;  
 }  
   
 /**  
  * ccs_read_alias_policy - Read "struct ccs_alias_entry" list.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  *  
  * Caller holds srcu_read_lock(&ccs_ss).  
  */  
 bool ccs_read_alias_policy(struct ccs_io_buffer *head)  
 {  
         struct list_head *pos;  
         bool done = true;  
         list_for_each_cookie(pos, head->read_var2, &ccs_alias_list) {  
                 struct ccs_alias_entry *ptr;  
                 ptr = list_entry(pos, struct ccs_alias_entry, list);  
                 if (ptr->is_deleted)  
                         continue;  
                 done = ccs_io_printf(head, KEYWORD_ALIAS "%s %s\n",  
                                      ptr->original_name->name,  
                                      ptr->aliased_name->name);  
                 if (!done)  
                         break;  
         }  
         return done;  
 }  
   
 /**  
  * ccs_write_alias_policy - Write "struct ccs_alias_entry" list.  
  *  
  * @data:      String to parse.  
  * @is_delete: True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 int ccs_write_alias_policy(char *data, const bool is_delete)  
 {  
         char *cp = strchr(data, ' ');  
         if (!cp)  
                 return -EINVAL;  
         *cp++ = '\0';  
         return ccs_update_alias_entry(data, cp, is_delete);  
 }  
   
385  /* The list for "struct ccs_aggregator_entry". */  /* The list for "struct ccs_aggregator_entry". */
386  LIST_HEAD(ccs_aggregator_list);  LIST_HEAD(ccs_aggregator_list);
387    
# Line 579  static int ccs_update_aggregator_entry(c Line 400  static int ccs_update_aggregator_entry(c
400  {  {
401          struct ccs_aggregator_entry *entry = NULL;          struct ccs_aggregator_entry *entry = NULL;
402          struct ccs_aggregator_entry *ptr;          struct ccs_aggregator_entry *ptr;
403          const struct ccs_path_info *saved_original_name;          struct ccs_aggregator_entry e = { };
         const struct ccs_path_info *saved_aggregated_name;  
404          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
405          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||
406              !ccs_is_correct_path(aggregated_name, 1, -1, -1))              !ccs_is_correct_path(aggregated_name, 1, -1, -1))
407                  return -EINVAL;                  return -EINVAL;
408          saved_original_name = ccs_get_name(original_name);          e.original_name = ccs_get_name(original_name);
409          saved_aggregated_name = ccs_get_name(aggregated_name);          e.aggregated_name = ccs_get_name(aggregated_name);
410          if (!saved_original_name || !saved_aggregated_name) {          if (!e.original_name || !e.aggregated_name)
411                  ccs_put_name(saved_original_name);                  goto out;
                 ccs_put_name(saved_aggregated_name);  
                 return -ENOMEM;  
         }  
412          if (!is_delete)          if (!is_delete)
413                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);                  entry = kmalloc(sizeof(e), GFP_KERNEL);
414          mutex_lock(&ccs_policy_lock);          mutex_lock(&ccs_policy_lock);
415          list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {          list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {
416                  if (ptr->original_name != saved_original_name ||                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), original_name),
417                      ptr->aggregated_name != saved_aggregated_name)                                 sizeof(e)))
418                          continue;                          continue;
419                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
420                  error = 0;                  error = 0;
421                  break;                  break;
422          }          }
423          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;  
424                  list_add_tail_rcu(&entry->list, &ccs_aggregator_list);                  list_add_tail_rcu(&entry->list, &ccs_aggregator_list);
425                  entry = NULL;                  entry = NULL;
426                  error = 0;                  error = 0;
427          }          }
428          mutex_unlock(&ccs_policy_lock);          mutex_unlock(&ccs_policy_lock);
429          ccs_put_name(saved_original_name);   out:
430          ccs_put_name(saved_aggregated_name);          ccs_put_name(e.original_name);
431            ccs_put_name(e.aggregated_name);
432          kfree(entry);          kfree(entry);
433          return error;          return error;
434  }  }
# Line 626  static int ccs_update_aggregator_entry(c Line 440  static int ccs_update_aggregator_entry(c
440   *   *
441   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
442   *   *
443   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
444   */   */
445  bool ccs_read_aggregator_policy(struct ccs_io_buffer *head)  bool ccs_read_aggregator_policy(struct ccs_io_buffer *head)
446  {  {
# Line 637  bool ccs_read_aggregator_policy(struct c Line 451  bool ccs_read_aggregator_policy(struct c
451                  ptr = list_entry(pos, struct ccs_aggregator_entry, list);                  ptr = list_entry(pos, struct ccs_aggregator_entry, list);
452                  if (ptr->is_deleted)                  if (ptr->is_deleted)
453                          continue;                          continue;
454                  done = ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n",                  done = ccs_io_printf(head, CCS_KEYWORD_AGGREGATOR "%s %s\n",
455                                       ptr->original_name->name,                                       ptr->original_name->name,
456                                       ptr->aggregated_name->name);                                       ptr->aggregated_name->name);
457                  if (!done)                  if (!done)
# Line 656  bool ccs_read_aggregator_policy(struct c Line 470  bool ccs_read_aggregator_policy(struct c
470   */   */
471  int ccs_write_aggregator_policy(char *data, const bool is_delete)  int ccs_write_aggregator_policy(char *data, const bool is_delete)
472  {  {
473          char *cp = strchr(data, ' ');          char *w[2];
474          if (!cp)          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
475                  return -EINVAL;                  return -EINVAL;
476          *cp++ = '\0';          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
         return ccs_update_aggregator_entry(data, cp, is_delete);  
477  }  }
478    
479  /* Domain create/delete handler. */  /* Domain create/delete handler. */
# Line 709  struct ccs_domain_info *ccs_find_or_assi Line 522  struct ccs_domain_info *ccs_find_or_assi
522          struct ccs_domain_info *domain;          struct ccs_domain_info *domain;
523          const struct ccs_path_info *saved_domainname;          const struct ccs_path_info *saved_domainname;
524          bool found = false;          bool found = false;
525    
526          if (!ccs_is_correct_domain(domainname))          if (!ccs_is_correct_domain(domainname))
527                  return NULL;                  return NULL;
528          saved_domainname = ccs_get_name(domainname);          saved_domainname = ccs_get_name(domainname);
# Line 741  struct ccs_domain_info *ccs_find_or_assi Line 554  struct ccs_domain_info *ccs_find_or_assi
554  }  }
555    
556  /**  /**
  * 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;  
 }  
   
 /**  
557   * ccs_find_next_domain - Find a domain.   * ccs_find_next_domain - Find a domain.
558   *   *
559   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve_entry".
560   *   *
561   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
562   *   *
563   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
564   */   */
565  static int ccs_find_next_domain(struct ccs_execve_entry *ee)  static int ccs_find_next_domain(struct ccs_execve_entry *ee)
566  {  {
# Line 811  static int ccs_find_next_domain(struct c Line 569  static int ccs_find_next_domain(struct c
569          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
570          const char *old_domain_name = r->domain->domainname->name;          const char *old_domain_name = r->domain->domainname->name;
571          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
         const u8 mode = r->mode;  
         const bool is_enforce = (mode == 3);  
572          const u32 ccs_flags = current->ccs_flags;          const u32 ccs_flags = current->ccs_flags;
573          char *new_domain_name = NULL;          struct ccs_path_info rn = { }; /* real name */
         struct ccs_path_info rn; /* real name */  
         struct ccs_path_info sn; /* symlink name */  
574          struct ccs_path_info ln; /* last name */          struct ccs_path_info ln; /* last name */
575          int retval;          int retval;
576            bool need_kfree = false;
577            ln.name = ccs_last_word(old_domain_name);
578            ccs_fill_path_info(&ln);
579   retry:   retry:
580          current->ccs_flags = ccs_flags;          current->ccs_flags = ccs_flags;
581          r->cond = NULL;          r->cond = NULL;
582          /* Get realpath of program and symbolic link. */          if (need_kfree) {
583          retval = ccs_realpath_both(bprm->filename, ee);                  kfree(rn.name);
584                    need_kfree = false;
585            }
586    
587            /* Get symlink's pathname of program. */
588            retval = ccs_symlink_path(bprm->filename, &rn);
589          if (retval < 0)          if (retval < 0)
590                  goto out;                  goto out;
591            need_kfree = true;
         rn.name = ee->program_path;  
         ccs_fill_path_info(&rn);  
         sn.name = ee->tmp;  
         ccs_fill_path_info(&sn);  
         ln.name = ccs_get_last_name(r->domain);  
         ccs_fill_path_info(&ln);  
592    
593          if (handler) {          if (handler) {
594                  if (ccs_pathcmp(&rn, handler)) {                  if (ccs_pathcmp(&rn, handler)) {
# Line 845  static int ccs_find_next_domain(struct c Line 601  static int ccs_find_next_domain(struct c
601                          }                          }
602                          goto out;                          goto out;
603                  }                  }
604                  goto calculate_domain;          } else {
         }  
   
         /* Check 'alias' directive. */  
         if (ccs_pathcmp(&rn, &sn)) {  
                 struct ccs_alias_entry *ptr;  
                 /* Is this program allowed to be called via symbolic links? */  
                 list_for_each_entry_rcu(ptr, &ccs_alias_list, list) {  
                         if (ptr->is_deleted ||  
                             ccs_pathcmp(&rn, ptr->original_name) ||  
                             ccs_pathcmp(&sn, ptr->aliased_name))  
                                 continue;  
                         strncpy(ee->program_path, ptr->aliased_name->name,  
                                 CCS_MAX_PATHNAME_LEN - 1);  
                         ccs_fill_path_info(&rn);  
                         break;  
                 }  
         }  
         /* sn will be overwritten after here. */  
   
         /* 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. */  
         {  
605                  struct ccs_aggregator_entry *ptr;                  struct ccs_aggregator_entry *ptr;
606                  /* Is this program allowed to be aggregated? */                  /* Check 'aggregator' directive. */
607                  list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {                  list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {
608                          if (ptr->is_deleted ||                          if (ptr->is_deleted ||
609                              !ccs_path_matches_pattern(&rn, ptr->original_name))                              !ccs_path_matches_pattern(&rn, ptr->original_name))
610                                  continue;                                  continue;
611                          strncpy(ee->program_path, ptr->aggregated_name->name,                          kfree(rn.name);
612                                  CCS_MAX_PATHNAME_LEN - 1);                          need_kfree = false;
613                          ccs_fill_path_info(&rn);                          /* This is OK because it is read only. */
614                            rn = *ptr->aggregated_name;
615                          break;                          break;
616                  }                  }
         }  
617    
618          /* Check execute permission. */                  /* Check execute permission. */
619          r->mode = mode;                  retval = ccs_exec_perm(r, &rn);
620          retval = ccs_check_exec_perm(r, &rn);                  if (retval == 1)
621          if (retval == 1)                          goto retry;
622                  goto retry;                  if (retval < 0)
623          if (retval < 0)                          goto out;
624                  goto out;          }
625    
626   calculate_domain:          /* Calculate domain to transit to. */
         new_domain_name = ee->tmp;  
627          if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) {          if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) {
628                  /* Transit to the child of ccs_kernel_domain domain. */                  /* Transit to the child of ccs_kernel_domain domain. */
629                  snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, ROOT_NAME " " "%s",
630                           ROOT_NAME " " "%s", ee->program_path);                           rn.name);
631          } else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) {          } else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) {
632                  /*                  /*
633                   * Needn't to transit from kernel domain before starting                   * Needn't to transit from kernel domain before starting
# Line 928  static int ccs_find_next_domain(struct c Line 640  static int ccs_find_next_domain(struct c
640                  domain = r->domain;                  domain = r->domain;
641          } else {          } else {
642                  /* Normal domain transition. */                  /* Normal domain transition. */
643                  snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
644                           "%s %s", old_domain_name, ee->program_path);                           old_domain_name, rn.name);
645          }          }
646          if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN)          if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10)
647                  goto done;                  goto done;
648          domain = ccs_find_domain(new_domain_name);          domain = ccs_find_domain(ee->tmp);
649          if (domain)          if (domain)
650                  goto done;                  goto done;
651          if (is_enforce) {          if (r->mode == CCS_CONFIG_ENFORCING) {
652                  int error = ccs_check_supervisor(r,                  int error = ccs_supervisor(r, "# wants to create domain\n"
653                                                   "# wants to create domain\n"                                             "%s\n", ee->tmp);
                                                  "%s\n", new_domain_name);  
654                  if (error == 1)                  if (error == 1)
655                          goto retry;                          goto retry;
656                  if (error < 0)                  if (error < 0)
657                          goto done;                          goto done;
658          }          }
659          domain = ccs_find_or_assign_new_domain(new_domain_name, r->profile);          domain = ccs_find_or_assign_new_domain(ee->tmp, r->profile);
660          if (domain)          if (domain)
661                  ccs_audit_domain_creation_log(r->domain);                  ccs_audit_domain_creation_log(domain);
662   done:   done:
663          if (!domain) {          if (!domain) {
664                  printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",                  retval = (r->mode == CCS_CONFIG_ENFORCING) ? -EPERM : 0;
665                         new_domain_name);                  if (!r->domain->domain_transition_failed) {
                 if (is_enforce)  
                         retval = -EPERM;  
                 else {  
                         retval = 0;  
666                          r->domain->domain_transition_failed = true;                          r->domain->domain_transition_failed = true;
667                            ccs_write_audit_log(false, r,
668                                                CCS_KEYWORD_TRANSITION_FAILED
669                                                "\n");
670                            printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n",
671                                   ee->tmp);
672                  }                  }
673          } else {          } else {
674                  retval = 0;                  retval = 0;
675          }          }
676   out:   out:
677          if (domain)          if (domain)
678                  r->domain = domain;                  r->domain = domain;
679            if (need_kfree)
680                    kfree(rn.name);
681          return retval;          return retval;
682  }  }
683    
684  /**  /**
685   * ccs_check_environ - Check permission for environment variable names.   * ccs_environ - Check permission for environment variable names.
686   *   *
687   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve_entry".
688   *   *
689   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
690   */   */
691  static int ccs_check_environ(struct ccs_execve_entry *ee)  static int ccs_environ(struct ccs_execve_entry *ee)
692  {  {
693          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
694          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
695          char *arg_ptr = ee->tmp;          /* env_page->data is allocated by ccs_dump_page(). */
696            struct ccs_page_dump env_page = { };
697            char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
698          int arg_len = 0;          int arg_len = 0;
699          unsigned long pos = bprm->p;          unsigned long pos = bprm->p;
700          int offset = pos % PAGE_SIZE;          int offset = pos % PAGE_SIZE;
# Line 988  static int ccs_check_environ(struct ccs_ Line 704  static int ccs_check_environ(struct ccs_
704          int error = -ENOMEM;          int error = -ENOMEM;
705          if (!r->mode || !envp_count)          if (!r->mode || !envp_count)
706                  return 0;                  return 0;
707            arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
708            if (!arg_ptr)
709                    goto out;
710          while (error == -ENOMEM) {          while (error == -ENOMEM) {
711                  if (!ccs_dump_page(bprm, pos, &ee->dump))                  if (!ccs_dump_page(bprm, pos, &env_page))
712                          goto out;                          goto out;
713                  pos += PAGE_SIZE - offset;                  pos += PAGE_SIZE - offset;
714                  /* Read. */                  /* Read. */
715                  while (argv_count && offset < PAGE_SIZE) {                  while (argv_count && offset < PAGE_SIZE) {
716                          const char *kaddr = ee->dump.data;                          if (!env_page.data[offset++])
                         if (!kaddr[offset++])  
717                                  argv_count--;                                  argv_count--;
718                  }                  }
719                  if (argv_count) {                  if (argv_count) {
# Line 1003  static int ccs_check_environ(struct ccs_ Line 721  static int ccs_check_environ(struct ccs_
721                          continue;                          continue;
722                  }                  }
723                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
724                          const char *kaddr = ee->dump.data;                          const unsigned char c = env_page.data[offset++];
725                          const unsigned char c = kaddr[offset++];                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
                         if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {  
726                                  if (c == '=') {                                  if (c == '=') {
727                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
728                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 1025  static int ccs_check_environ(struct ccs_ Line 742  static int ccs_check_environ(struct ccs_
742                          }                          }
743                          if (c)                          if (c)
744                                  continue;                                  continue;
745                          if (ccs_check_env_perm(r, arg_ptr)) {                          if (ccs_env_perm(r, arg_ptr)) {
746                                  error = -EPERM;                                  error = -EPERM;
747                                  break;                                  break;
748                          }                          }
# Line 1040  static int ccs_check_environ(struct ccs_ Line 757  static int ccs_check_environ(struct ccs_
757   out:   out:
758          if (r->mode != 3)          if (r->mode != 3)
759                  error = 0;                  error = 0;
760            kfree(env_page.data);
761          return error;          return error;
762  }  }
763    
# Line 1093  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 1108  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 1125  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 1137  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 1148  static int ccs_get_root_depth(void) Line 862  static int ccs_get_root_depth(void)
862          return depth;          return depth;
863  }  }
864    
 static LIST_HEAD(ccs_execve_list);  
 static DEFINE_SPINLOCK(ccs_execve_list_lock);  
   
 /**  
  * ccs_allocate_execve_entry - Allocate memory for execve().  
  *  
  * Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise.  
  */  
 static struct ccs_execve_entry *ccs_allocate_execve_entry(void)  
 {  
         struct ccs_execve_entry *ee = kzalloc(sizeof(*ee), GFP_KERNEL);  
         if (!ee)  
                 return NULL;  
         memset(ee, 0, sizeof(*ee));  
         ee->program_path = kzalloc(CCS_MAX_PATHNAME_LEN, GFP_KERNEL);  
         ee->tmp = kzalloc(CCS_MAX_PATHNAME_LEN, GFP_KERNEL);  
         if (!ee->program_path || !ee->tmp) {  
                 kfree(ee->program_path);  
                 kfree(ee->tmp);  
                 kfree(ee);  
                 return NULL;  
         }  
         ee->srcu_idx = srcu_read_lock(&ccs_ss);  
         /* ee->dump->data is allocated by ccs_dump_page(). */  
         ee->task = current;  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&ccs_execve_list_lock);  
         list_add(&ee->list, &ccs_execve_list);  
         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 *****/  
         return ee;  
 }  
   
 /**  
  * ccs_free_execve_entry - Free memory for execve().  
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
  */  
 static void ccs_free_execve_entry(struct ccs_execve_entry *ee)  
 {  
         if (!ee)  
                 return;  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&ccs_execve_list_lock);  
         list_del(&ee->list);  
         spin_unlock(&ccs_execve_list_lock);  
         /***** CRITICAL SECTION END *****/  
         kfree(ee->program_path);  
         kfree(ee->tmp);  
         kfree(ee->dump.data);  
         srcu_read_unlock(&ccs_ss, ee->srcu_idx);  
         kfree(ee);  
 }  
   
865  /**  /**
866   * ccs_try_alt_exec - Try to start execute handler.   * ccs_try_alt_exec - Try to start execute handler.
867   *   *
# Line 1278  static int ccs_try_alt_exec(struct ccs_e Line 915  static int ccs_try_alt_exec(struct ccs_e
915          struct task_struct *task = current;          struct task_struct *task = current;
916    
917          /* Close the requested program's dentry. */          /* Close the requested program's dentry. */
918            ee->obj.path1.dentry = NULL;
919            ee->obj.path1.mnt = NULL;
920            ee->obj.validate_done = false;
921          allow_write_access(bprm->file);          allow_write_access(bprm->file);
922          fput(bprm->file);          fput(bprm->file);
923          bprm->file = NULL;          bprm->file = NULL;
# Line 1344  static int ccs_try_alt_exec(struct ccs_e Line 984  static int ccs_try_alt_exec(struct ccs_e
984                          kfree(exe);                          kfree(exe);
985                  } else {                  } else {
986                          exe = ee->tmp;                          exe = ee->tmp;
987                          strncpy(ee->tmp, "<unknown>", CCS_EXEC_TMPSIZE - 1);                          snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
988                          retval = copy_strings_kernel(1, &exe, bprm);                          retval = copy_strings_kernel(1, &exe, bprm);
989                  }                  }
990                  if (retval < 0)                  if (retval < 0)
# Line 1355  static int ccs_try_alt_exec(struct ccs_e Line 995  static int ccs_try_alt_exec(struct ccs_e
995          /* Set argv[1] */          /* Set argv[1] */
996          {          {
997                  char *cp = ee->tmp;                  char *cp = ee->tmp;
998                  strncpy(ee->tmp, ccs_current_domain()->domainname->name,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
999                          CCS_EXEC_TMPSIZE - 1);                           ccs_current_domain()->domainname->name);
1000                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1001                  if (retval < 0)                  if (retval < 0)
1002                          goto out;                          goto out;
# Line 1366  static int ccs_try_alt_exec(struct ccs_e Line 1006  static int ccs_try_alt_exec(struct ccs_e
1006          /* Set argv[0] */          /* Set argv[0] */
1007          {          {
1008                  int depth = ccs_get_root_depth();                  int depth = ccs_get_root_depth();
1009                  char *cp = ee->program_path;                  int len = ee->handler->total_len + 1;
1010                  strncpy(cp, ee->handler->name, CCS_MAX_PATHNAME_LEN - 1);                  char *cp = kmalloc(len, GFP_KERNEL);
1011                    if (!cp) {
1012                            retval = -ENOMEM;
1013                            goto out;
1014                    }
1015                    ee->handler_path = cp;
1016                    memmove(cp, ee->handler->name, len);
1017                  ccs_unescape(cp);                  ccs_unescape(cp);
1018                  retval = -ENOENT;                  retval = -ENOENT;
1019                  if (!*cp || *cp != '/')                  if (!*cp || *cp != '/')
# Line 1379  static int ccs_try_alt_exec(struct ccs_e Line 1025  static int ccs_try_alt_exec(struct ccs_e
1025                                  goto out;                                  goto out;
1026                          depth--;                          depth--;
1027                  }                  }
1028                  memmove(ee->program_path, cp, strlen(cp) + 1);                  memmove(ee->handler_path, cp, strlen(cp) + 1);
1029                  cp = ee->program_path;                  cp = ee->handler_path;
1030                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1031                  if (retval < 0)                  if (retval < 0)
1032                          goto out;                          goto out;
# Line 1393  static int ccs_try_alt_exec(struct ccs_e Line 1039  static int ccs_try_alt_exec(struct ccs_e
1039  #endif  #endif
1040    
1041          /* OK, now restart the process with execute handler program's dentry. */          /* OK, now restart the process with execute handler program's dentry. */
1042          filp = open_exec(ee->program_path);          filp = open_exec(ee->handler_path);
1043          if (IS_ERR(filp)) {          if (IS_ERR(filp)) {
1044                  retval = PTR_ERR(filp);                  retval = PTR_ERR(filp);
1045                  goto out;                  goto out;
1046          }          }
1047            ee->obj.path1.dentry = filp->f_dentry;
1048            ee->obj.path1.mnt = filp->f_vfsmnt;
1049          bprm->file = filp;          bprm->file = filp;
1050          bprm->filename = ee->program_path;          bprm->filename = ee->handler_path;
1051  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1052          bprm->interp = bprm->filename;          bprm->interp = bprm->filename;
1053  #endif  #endif
1054          retval = prepare_binprm(bprm);          retval = prepare_binprm(bprm);
1055          if (retval < 0)          if (retval < 0)
1056                  goto out;                  goto out;
1057          {          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1058                  /*          retval = ccs_find_next_domain(ee);
1059                   * 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);  
         }  
1060   out:   out:
1061          return retval;          return retval;
1062  }  }
# Line 1445  static int ccs_try_alt_exec(struct ccs_e Line 1069  static int ccs_try_alt_exec(struct ccs_e
1069   *   *
1070   * Returns true if found, false otherwise.   * Returns true if found, false otherwise.
1071   *   *
1072   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
1073   */   */
1074  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,
1075                                       const u8 type)                                       const u8 type)
# Line 1486  bool ccs_dump_page(struct linux_binprm * Line 1110  bool ccs_dump_page(struct linux_binprm *
1110                     struct ccs_page_dump *dump)                     struct ccs_page_dump *dump)
1111  {  {
1112          struct page *page;          struct page *page;
1113          /* dump->data is released by ccs_free_execve_entry(). */          /* dump->data is released by ccs_finish_execve(). */
1114          if (!dump->data) {          if (!dump->data) {
1115                  dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);                  dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);
1116                  if (!dump->data)                  if (!dump->data)
# Line 1496  bool ccs_dump_page(struct linux_binprm * Line 1120  bool ccs_dump_page(struct linux_binprm *
1120  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1121          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1122                  return false;                  return false;
1123  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU)  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1124            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1125                    return false;
1126    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1127          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1128                  return false;                  return false;
1129  #else  #else
# Line 1517  bool ccs_dump_page(struct linux_binprm * Line 1144  bool ccs_dump_page(struct linux_binprm *
1144          /* Same with put_arg_page(page) in fs/exec.c */          /* Same with put_arg_page(page) in fs/exec.c */
1145  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1146          put_page(page);          put_page(page);
1147  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU)  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1148            put_page(page);
1149    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1150          put_page(page);          put_page(page);
1151  #endif  #endif
1152          return true;          return true;
1153  }  }
1154    
1155  /**  /**
  * 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;  
 }  
   
 /**  
1156   * ccs_start_execve - Prepare for execve() operation.   * ccs_start_execve - Prepare for execve() operation.
1157   *   *
1158   * @bprm: Pointer to "struct linux_binprm".   * @bprm: Pointer to "struct linux_binprm".
1159     * @eep:  Pointer to "struct ccs_execve_entry *".
1160   *   *
1161   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1162   */   */
1163  int ccs_start_execve(struct linux_binprm *bprm)  int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve_entry **eep)
1164  {  {
1165          int retval;          int retval;
1166          struct task_struct *task = current;          struct task_struct *task = current;
1167          struct ccs_execve_entry *ee = ccs_allocate_execve_entry();          struct ccs_execve_entry *ee;
1168            *eep = NULL;
1169          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
1170                  ccs_load_policy(bprm->filename);                  ccs_load_policy(bprm->filename);
1171            ee = kzalloc(sizeof(*ee), GFP_KERNEL);
1172          if (!ee)          if (!ee)
1173                  return -ENOMEM;                  return -ENOMEM;
1174          ccs_init_request_info(&ee->r, NULL, CCS_MAC_FOR_FILE);          ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
1175            if (!ee->tmp) {
1176                    kfree(ee);
1177                    return -ENOMEM;
1178            }
1179            ee->reader_idx = ccs_read_lock();
1180            /* ee->dump->data is allocated by ccs_dump_page(). */
1181            ee->previous_domain = task->ccs_domain_info;
1182            /* Clear manager flag. */
1183            task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;
1184            *eep = ee;
1185            ccs_init_request_info(&ee->r, NULL, CCS_MAC_FILE_EXECUTE);
1186          ee->r.ee = ee;          ee->r.ee = ee;
1187          ee->bprm = bprm;          ee->bprm = bprm;
1188          ee->r.obj = &ee->obj;          ee->r.obj = &ee->obj;
1189          ee->obj.path1_dentry = bprm->file->f_dentry;          ee->obj.path1.dentry = bprm->file->f_dentry;
1190          ee->obj.path1_vfsmnt = bprm->file->f_vfsmnt;          ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1191          /* Clear manager flag. */          if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER)) {
         task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;  
         if (ccs_find_execute_handler(ee, TYPE_EXECUTE_HANDLER)) {  
1192                  retval = ccs_try_alt_exec(ee);                  retval = ccs_try_alt_exec(ee);
1193                  if (!retval)                  if (!retval)
1194                          ccs_audit_execute_handler_log(ee, true);                          ccs_audit_execute_handler_log(ee, true);
# Line 1573  int ccs_start_execve(struct linux_binprm Line 1197  int ccs_start_execve(struct linux_binprm
1197          retval = ccs_find_next_domain(ee);          retval = ccs_find_next_domain(ee);
1198          if (retval != -EPERM)          if (retval != -EPERM)
1199                  goto ok;                  goto ok;
1200          if (ccs_find_execute_handler(ee, TYPE_DENIED_EXECUTE_HANDLER)) {          if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) {
1201                  retval = ccs_try_alt_exec(ee);                  retval = ccs_try_alt_exec(ee);
1202                  if (!retval)                  if (!retval)
1203                          ccs_audit_execute_handler_log(ee, false);                          ccs_audit_execute_handler_log(ee, false);
# Line 1581  int ccs_start_execve(struct linux_binprm Line 1205  int ccs_start_execve(struct linux_binprm
1205   ok:   ok:
1206          if (retval < 0)          if (retval < 0)
1207                  goto out;                  goto out;
1208          ee->r.mode = ccs_check_flags(ee->r.domain, CCS_MAC_FOR_ENV);          /*
1209          retval = ccs_check_environ(ee);           * Tell GC that I started execve().
1210             * Also, tell open_exec() to check read permission.
1211             */
1212            task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
1213            /*
1214             * Make task->ccs_flags visible to GC before changing
1215             * task->ccs_domain_info .
1216             */
1217            smp_mb();
1218            /*
1219             * Proceed to the next domain in order to allow reaching via PID.
1220             * It will be reverted if execve() failed. Reverting is not good.
1221             * But it is better than being unable to reach via PID in interactive
1222             * enforcing mode.
1223             */
1224            task->ccs_domain_info = ee->r.domain;
1225            ee->r.mode = ccs_get_mode(ee->r.domain->profile, CCS_MAC_ENVIRON);
1226            retval = ccs_environ(ee);
1227          if (retval < 0)          if (retval < 0)
1228                  goto out;                  goto out;
         task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC;  
1229          retval = 0;          retval = 0;
1230   out:   out:
         if (retval)  
                 ccs_finish_execve(retval);  
1231          return retval;          return retval;
1232  }  }
1233    
# Line 1597  int ccs_start_execve(struct linux_binprm Line 1235  int ccs_start_execve(struct linux_binprm
1235   * ccs_finish_execve - Clean up execve() operation.   * ccs_finish_execve - Clean up execve() operation.
1236   *   *
1237   * @retval: Return code of an execve() operation.   * @retval: Return code of an execve() operation.
1238     * @ee:     Pointer to "struct ccs_execve_entry".
1239   *   *
1240   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
1241   */   */
1242  void ccs_finish_execve(int retval)  void ccs_finish_execve(int retval, struct ccs_execve_entry *ee)
1243  {  {
1244          struct task_struct *task = current;          struct task_struct *task = current;
         struct ccs_execve_entry *ee = ccs_find_execve_entry();  
         task->ccs_flags &= ~CCS_CHECK_READ_FOR_OPEN_EXEC;  
1245          if (!ee)          if (!ee)
1246                  return;                  return;
1247          if (retval < 0)          if (retval < 0) {
1248                  goto out;                  task->ccs_domain_info = ee->previous_domain;
1249          /* Proceed to next domain if execution suceeded. */                  /*
1250          task->ccs_domain_info = ee->r.domain;                   * Make task->ccs_domain_info visible to GC before changing
1251          /* Mark the current process as execute handler. */                   * task->ccs_flags .
1252          if (ee->handler)                   */
1253                  task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;                  smp_mb();
1254          /* Mark the current process as normal process. */          } else {
1255          else                  /* Mark the current process as execute handler. */
1256                  task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;                  if (ee->handler)
1257   out:                          task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1258          ccs_free_execve_entry(ee);                  /* Mark the current process as normal process. */
1259                    else
1260                            task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1261            }
1262            /* Tell GC that I finished execve(). */
1263            task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1264            ccs_read_unlock(ee->reader_idx);
1265            kfree(ee->handler_path);
1266            kfree(ee->tmp);
1267            kfree(ee->dump.data);
1268            kfree(ee);
1269  }  }

Legend:
Removed from v.2718  
changed lines
  Added in v.3261

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