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

Subversion リポジトリの参照

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

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

trunk/1.6.x/ccs-patch/fs/tomoyo_domain.c revision 2393 by kumaneko, Mon Apr 6 05:24:05 2009 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/domain.c revision 3109 by kumaneko, Fri Oct 16 05:02: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.6.7   2009/04/01   * Version: 1.7.1-pre   2009/10/16
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>
19  #endif  #endif
20    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
21  /* For compatibility with older kernels. */  #include <linux/fs_struct.h>
 #ifndef for_each_process  
 #define for_each_process for_each_task  
22  #endif  #endif
23    #include "internal.h"
24    
25  /* Variables definitions.*/  /* Variables definitions.*/
26    
# Line 32  Line 28 
28  struct ccs_domain_info ccs_kernel_domain;  struct ccs_domain_info ccs_kernel_domain;
29    
30  /* The list for "struct ccs_domain_info". */  /* The list for "struct ccs_domain_info". */
31  LIST1_HEAD(ccs_domain_list);  LIST_HEAD(ccs_domain_list);
   
 #ifdef CONFIG_TOMOYO  
   
 /* Domain creation lock. */  
 static DEFINE_MUTEX(ccs_domain_list_lock);  
   
 /* Structure for "initialize_domain" and "no_initialize_domain" keyword. */  
 struct ccs_domain_initializer_entry {  
         struct list1_head list;  
         const struct ccs_path_info *domainname;    /* This may be NULL */  
         const struct ccs_path_info *program;  
         bool is_deleted;  
         bool is_not;       /* True if this entry is "no_initialize_domain".  */  
         bool is_last_name; /* True if the domainname is ccs_get_last_name(). */  
 };  
   
 /* Structure for "keep_domain" and "no_keep_domain" keyword. */  
 struct ccs_domain_keeper_entry {  
         struct list1_head list;  
         const struct ccs_path_info *domainname;  
         const struct ccs_path_info *program;       /* This may be NULL */  
         bool is_deleted;  
         bool is_not;       /* True if this entry is "no_keep_domain".        */  
         bool is_last_name; /* True if the domainname is ccs_get_last_name(). */  
 };  
   
 /* Structure for "aggregator" keyword. */  
 struct ccs_aggregator_entry {  
         struct list1_head list;  
         const struct ccs_path_info *original_name;  
         const struct ccs_path_info *aggregated_name;  
         bool is_deleted;  
 };  
   
 /* Structure for "alias" keyword. */  
 struct ccs_alias_entry {  
         struct list1_head list;  
         const struct ccs_path_info *original_name;  
         const struct ccs_path_info *aliased_name;  
         bool is_deleted;  
 };  
   
 /**  
  * ccs_set_domain_flag - Set or clear domain's attribute flags.  
  *  
  * @domain:    Pointer to "struct ccs_domain_info".  
  * @is_delete: True if it is a delete request.  
  * @flags:     Flags to set or clear.  
  *  
  * Returns nothing.  
  */  
 void ccs_set_domain_flag(struct ccs_domain_info *domain, const bool is_delete,  
                          const u8 flags)  
 {  
         /* We need to serialize because this is bitfield operation. */  
         static DEFINE_SPINLOCK(lock);  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&lock);  
         if (!is_delete)  
                 domain->flags |= flags;  
         else  
                 domain->flags &= ~flags;  
         spin_unlock(&lock);  
         /***** CRITICAL SECTION END *****/  
 }  
   
 /**  
  * 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) {  
                 /*  
                  * We need to serialize because this function is called by  
                  * various update functions.  
                  */  
                 static DEFINE_SPINLOCK(lock);  
                 /***** CRITICAL SECTION START *****/  
                 spin_lock(&lock);  
                 list1_add_tail_mb(&acl->list, &domain->acl_info_list);  
                 spin_unlock(&lock);  
                 /***** CRITICAL SECTION END *****/  
         } else {  
                 acl->type &= ~ACL_DELETED;  
         }  
         ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);  
         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;  
         ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);  
         return 0;  
 }  
32    
33  /**  /**
34   * ccs_audit_execute_handler_log - Audit execute_handler log.   * ccs_audit_execute_handler_log - Audit execute_handler log.
# Line 171  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 186  static int ccs_audit_execute_handler_log Line 58  static int ccs_audit_execute_handler_log
58   */   */
59  static int ccs_audit_domain_creation_log(struct ccs_domain_info *domain)  static int ccs_audit_domain_creation_log(struct ccs_domain_info *domain)
60  {  {
61            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          return 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;
66  }  }
67    
68  /* The list for "struct ccs_domain_initializer_entry". */  /* The list for "struct ccs_domain_initializer_entry". */
69  static LIST1_HEAD(ccs_domain_initializer_list);  LIST_HEAD(ccs_domain_initializer_list);
70    
71  /**  /**
72   * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list.   * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list.
# Line 209  static int ccs_update_domain_initializer Line 83  static int ccs_update_domain_initializer
83                                                 const bool is_not,                                                 const bool is_not,
84                                                 const bool is_delete)                                                 const bool is_delete)
85  {  {
86          struct ccs_domain_initializer_entry *new_entry;          struct ccs_domain_initializer_entry *entry = NULL;
87          struct ccs_domain_initializer_entry *ptr;          struct ccs_domain_initializer_entry *ptr;
88          static DEFINE_MUTEX(lock);          struct ccs_domain_initializer_entry e = { .is_not = is_not };
89          const struct ccs_path_info *saved_program;          int error = is_delete ? -ENOENT : -ENOMEM;
90          const struct ccs_path_info *saved_domainname = NULL;          if (!ccs_is_correct_path(program, 1, -1, -1))
         int error = -ENOMEM;  
         bool is_last_name = false;  
         if (!ccs_is_correct_path(program, 1, -1, -1, __func__))  
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, __func__))                      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, __func__))                  else if (!ccs_is_correct_domain(domainname))
97                          return -EINVAL;                          return -EINVAL;
98                  saved_domainname = ccs_save_name(domainname);                  e.domainname = ccs_get_name(domainname);
99                  if (!saved_domainname)                  if (!e.domainname)
100                          return -ENOMEM;                          goto out;
101          }          }
102          saved_program = ccs_save_name(program);          e.program = ccs_get_name(program);
103          if (!saved_program)          if (!e.program)
104                  return -ENOMEM;                  goto out;
105          mutex_lock(&lock);          if (!is_delete)
106          list1_for_each_entry(ptr, &ccs_domain_initializer_list, list) {                  entry = kmalloc(sizeof(e), GFP_KERNEL);
107                  if (ptr->is_not != is_not ||          mutex_lock(&ccs_policy_lock);
108                      ptr->domainname != saved_domainname ||          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {
109                      ptr->program != saved_program)                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not),
110                                   sizeof(e)))
111                          continue;                          continue;
112                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
113                  error = 0;                  error = 0;
114                  goto out;                  break;
115          }          }
116          if (is_delete) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
117                  error = -ENOENT;                  list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);
118                  goto out;                  entry = NULL;
119                    error = 0;
120          }          }
121          new_entry = ccs_alloc_element(sizeof(*new_entry));          mutex_unlock(&ccs_policy_lock);
         if (!new_entry)  
                 goto out;  
         new_entry->domainname = saved_domainname;  
         new_entry->program = saved_program;  
         new_entry->is_not = is_not;  
         new_entry->is_last_name = is_last_name;  
         list1_add_tail_mb(&new_entry->list, &ccs_domain_initializer_list);  
         error = 0;  
122   out:   out:
123          mutex_unlock(&lock);          ccs_put_name(e.domainname);
124          ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);          ccs_put_name(e.program);
125            kfree(entry);
126          return error;          return error;
127  }  }
128    
# Line 266  static int ccs_update_domain_initializer Line 132  static int ccs_update_domain_initializer
132   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
133   *   *
134   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
135     *
136     * 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  {  {
140          struct list1_head *pos;          struct list_head *pos;
141          list1_for_each_cookie(pos, head->read_var2,          bool done = true;
142                                &ccs_domain_initializer_list) {          list_for_each_cookie(pos, head->read_var2,
143                                 &ccs_domain_initializer_list) {
144                  const char *no;                  const char *no;
145                  const char *from = "";                  const char *from = "";
146                  const char *domain = "";                  const char *domain = "";
147                  struct ccs_domain_initializer_entry *ptr;                  struct ccs_domain_initializer_entry *ptr;
148                  ptr = list1_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 285  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                  if (!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                          goto out;                  if (!done)
161                            break;
162          }          }
163          return true;          return done;
  out:  
         return false;  
164  }  }
165    
166  /**  /**
# Line 326  int ccs_write_domain_initializer_policy( Line 194  int ccs_write_domain_initializer_policy(
194   *   *
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 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 333  static bool ccs_is_domain_initializer(co Line 203  static bool ccs_is_domain_initializer(co
203  {  {
204          struct ccs_domain_initializer_entry *ptr;          struct ccs_domain_initializer_entry *ptr;
205          bool flag = false;          bool flag = false;
206          list1_for_each_entry(ptr, &ccs_domain_initializer_list, list) {          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {
207                  if (ptr->is_deleted)                  if (ptr->is_deleted)
208                          continue;                          continue;
209                  if (ptr->domainname) {                  if (ptr->domainname) {
# Line 347  static bool ccs_is_domain_initializer(co Line 217  static bool ccs_is_domain_initializer(co
217                  }                  }
218                  if (ccs_pathcmp(ptr->program, program))                  if (ccs_pathcmp(ptr->program, program))
219                          continue;                          continue;
220                  if (ptr->is_not)                  if (ptr->is_not) {
221                          return false;                          flag = false;
222                            break;
223                    }
224                  flag = true;                  flag = true;
225          }          }
226          return flag;          return flag;
227  }  }
228    
229  /* The list for "struct ccs_domain_keeper_entry". */  /* The list for "struct ccs_domain_keeper_entry". */
230  static LIST1_HEAD(ccs_domain_keeper_list);  LIST_HEAD(ccs_domain_keeper_list);
231    
232  /**  /**
233   * ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper_entry" list.   * ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper_entry" list.
# Line 372  static int ccs_update_domain_keeper_entr Line 244  static int ccs_update_domain_keeper_entr
244                                            const bool is_not,                                            const bool is_not,
245                                            const bool is_delete)                                            const bool is_delete)
246  {  {
247          struct ccs_domain_keeper_entry *new_entry;          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 };
250          const struct ccs_path_info *saved_program = NULL;          int error = is_delete ? -ENOENT : -ENOMEM;
         static DEFINE_MUTEX(lock);  
         int error = -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, __func__))              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, __func__))          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, __func__))                  if (!ccs_is_correct_path(program, 1, -1, -1))
258                          return -EINVAL;                          return -EINVAL;
259                  saved_program = ccs_save_name(program);                  e.program = ccs_get_name(program);
260                  if (!saved_program)                  if (!e.program)
261                          return -ENOMEM;                          goto out;
262          }          }
263          saved_domainname = ccs_save_name(domainname);          e.domainname = ccs_get_name(domainname);
264          if (!saved_domainname)          if (!e.domainname)
265                  return -ENOMEM;                  goto out;
266          mutex_lock(&lock);          if (!is_delete)
267          list1_for_each_entry(ptr, &ccs_domain_keeper_list, list) {                  entry = kmalloc(sizeof(e), GFP_KERNEL);
268                  if (ptr->is_not != is_not ||          mutex_lock(&ccs_policy_lock);
269                      ptr->domainname != saved_domainname ||          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {
270                      ptr->program != saved_program)                  if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not),
271                                   sizeof(e)))
272                          continue;                          continue;
273                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
274                  error = 0;                  error = 0;
275                  goto out;                  break;
276          }          }
277          if (is_delete) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
278                  error = -ENOENT;                  list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list);
279                  goto out;                  entry = NULL;
280                    error = 0;
281          }          }
282          new_entry = ccs_alloc_element(sizeof(*new_entry));          mutex_unlock(&ccs_policy_lock);
         if (!new_entry)  
                 goto out;  
         new_entry->domainname = saved_domainname;  
         new_entry->program = saved_program;  
         new_entry->is_not = is_not;  
         new_entry->is_last_name = is_last_name;  
         list1_add_tail_mb(&new_entry->list, &ccs_domain_keeper_list);  
         error = 0;  
283   out:   out:
284          mutex_unlock(&lock);          ccs_put_name(e.domainname);
285          ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);          ccs_put_name(e.program);
286            kfree(entry);
287          return error;          return error;
288  }  }
289    
# Line 449  int ccs_write_domain_keeper_policy(char Line 313  int ccs_write_domain_keeper_policy(char
313   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
314   *   *
315   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
316     *
317     * 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  {  {
321          struct list1_head *pos;          struct list_head *pos;
322          list1_for_each_cookie(pos, head->read_var2, &ccs_domain_keeper_list) {          bool done = true;
323            list_for_each_cookie(pos, head->read_var2,
324                                 &ccs_domain_keeper_list) {
325                  struct ccs_domain_keeper_entry *ptr;                  struct ccs_domain_keeper_entry *ptr;
326                  const char *no;                  const char *no;
327                  const char *from = "";                  const char *from = "";
328                  const char *program = "";                  const char *program = "";
329                  ptr = list1_entry(pos, struct ccs_domain_keeper_entry, list);                  ptr = list_entry(pos, struct ccs_domain_keeper_entry, list);
330                  if (ptr->is_deleted)                  if (ptr->is_deleted)
331                          continue;                          continue;
332                  no = ptr->is_not ? "no_" : "";                  no = ptr->is_not ? "no_" : "";
# Line 466  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                  if (!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                          goto out;                  if (!done)
341                            break;
342          }          }
343          return true;          return done;
  out:  
         return false;  
344  }  }
345    
346  /**  /**
# Line 485  bool ccs_read_domain_keeper_policy(struc Line 352  bool ccs_read_domain_keeper_policy(struc
352   *   *
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 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 492  static bool ccs_is_domain_keeper(const s Line 361  static bool ccs_is_domain_keeper(const s
361  {  {
362          struct ccs_domain_keeper_entry *ptr;          struct ccs_domain_keeper_entry *ptr;
363          bool flag = false;          bool flag = false;
364          list1_for_each_entry(ptr, &ccs_domain_keeper_list, list) {          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {
365                  if (ptr->is_deleted)                  if (ptr->is_deleted)
366                          continue;                          continue;
367                  if (!ptr->is_last_name) {                  if (!ptr->is_last_name) {
# Line 504  static bool ccs_is_domain_keeper(const s Line 373  static bool ccs_is_domain_keeper(const s
373                  }                  }
374                  if (ptr->program && ccs_pathcmp(ptr->program, program))                  if (ptr->program && ccs_pathcmp(ptr->program, program))
375                          continue;                          continue;
376                  if (ptr->is_not)                  if (ptr->is_not) {
377                          return false;                          flag = false;
378                            break;
379                    }
380                  flag = true;                  flag = true;
381          }          }
382          return flag;          return flag;
383  }  }
384    
 /* The list for "struct ccs_alias_entry". */  
 static LIST1_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 *new_entry;  
         struct ccs_alias_entry *ptr;  
         static DEFINE_MUTEX(lock);  
         const struct ccs_path_info *saved_original_name;  
         const struct ccs_path_info *saved_aliased_name;  
         int error = -ENOMEM;  
         if (!ccs_is_correct_path(original_name, 1, -1, -1, __func__) ||  
             !ccs_is_correct_path(aliased_name, 1, -1, -1, __func__))  
                 return -EINVAL; /* No patterns allowed. */  
         saved_original_name = ccs_save_name(original_name);  
         saved_aliased_name = ccs_save_name(aliased_name);  
         if (!saved_original_name || !saved_aliased_name)  
                 return -ENOMEM;  
         mutex_lock(&lock);  
         list1_for_each_entry(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;  
                 goto out;  
         }  
         if (is_delete) {  
                 error = -ENOENT;  
                 goto out;  
         }  
         new_entry = ccs_alloc_element(sizeof(*new_entry));  
         if (!new_entry)  
                 goto out;  
         new_entry->original_name = saved_original_name;  
         new_entry->aliased_name = saved_aliased_name;  
         list1_add_tail_mb(&new_entry->list, &ccs_alias_list);  
         error = 0;  
  out:  
         mutex_unlock(&lock);  
         ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);  
         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.  
  */  
 bool ccs_read_alias_policy(struct ccs_io_buffer *head)  
 {  
         struct list1_head *pos;  
         list1_for_each_cookie(pos, head->read_var2, &ccs_alias_list) {  
                 struct ccs_alias_entry *ptr;  
                 ptr = list1_entry(pos, struct ccs_alias_entry, list);  
                 if (ptr->is_deleted)  
                         continue;  
                 if (!ccs_io_printf(head, KEYWORD_ALIAS "%s %s\n",  
                                    ptr->original_name->name,  
                                    ptr->aliased_name->name))  
                         goto out;  
         }  
         return true;  
  out:  
         return false;  
 }  
   
 /**  
  * 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  static LIST1_HEAD(ccs_aggregator_list);  LIST_HEAD(ccs_aggregator_list);
387    
388  /**  /**
389   * ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list.   * ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list.
# Line 624  static int ccs_update_aggregator_entry(c Line 398  static int ccs_update_aggregator_entry(c
398                                         const char *aggregated_name,                                         const char *aggregated_name,
399                                         const bool is_delete)                                         const bool is_delete)
400  {  {
401          struct ccs_aggregator_entry *new_entry;          struct ccs_aggregator_entry *entry = NULL;
402          struct ccs_aggregator_entry *ptr;          struct ccs_aggregator_entry *ptr;
403          static DEFINE_MUTEX(lock);          struct ccs_aggregator_entry e = { };
404          const struct ccs_path_info *saved_original_name;          int error = is_delete ? -ENOENT : -ENOMEM;
405          const struct ccs_path_info *saved_aggregated_name;          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||
406          int error = -ENOMEM;              !ccs_is_correct_path(aggregated_name, 1, -1, -1))
         if (!ccs_is_correct_path(original_name, 1, 0, -1, __func__) ||  
             !ccs_is_correct_path(aggregated_name, 1, -1, -1, __func__))  
407                  return -EINVAL;                  return -EINVAL;
408          saved_original_name = ccs_save_name(original_name);          e.original_name = ccs_get_name(original_name);
409          saved_aggregated_name = ccs_save_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                  return -ENOMEM;                  goto out;
412          mutex_lock(&lock);          if (!is_delete)
413          list1_for_each_entry(ptr, &ccs_aggregator_list, list) {                  entry = kmalloc(sizeof(e), GFP_KERNEL);
414                  if (ptr->original_name != saved_original_name ||          mutex_lock(&ccs_policy_lock);
415                      ptr->aggregated_name != saved_aggregated_name)          list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {
416                    if (ccs_memcmp(ptr, &e, offsetof(typeof(e), original_name),
417                                   sizeof(e)))
418                          continue;                          continue;
419                  ptr->is_deleted = is_delete;                  ptr->is_deleted = is_delete;
420                  error = 0;                  error = 0;
421                  goto out;                  break;
422          }          }
423          if (is_delete) {          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
424                  error = -ENOENT;                  list_add_tail_rcu(&entry->list, &ccs_aggregator_list);
425                  goto out;                  entry = NULL;
426                    error = 0;
427          }          }
428          new_entry = ccs_alloc_element(sizeof(*new_entry));          mutex_unlock(&ccs_policy_lock);
         if (!new_entry)  
                 goto out;  
         new_entry->original_name = saved_original_name;  
         new_entry->aggregated_name = saved_aggregated_name;  
         list1_add_tail_mb(&new_entry->list, &ccs_aggregator_list);  
         error = 0;  
429   out:   out:
430          mutex_unlock(&lock);          ccs_put_name(e.original_name);
431          ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);          ccs_put_name(e.aggregated_name);
432            kfree(entry);
433          return error;          return error;
434  }  }
435    
# Line 669  static int ccs_update_aggregator_entry(c Line 439  static int ccs_update_aggregator_entry(c
439   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
440   *   *
441   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
442     *
443     * 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  {  {
447          struct list1_head *pos;          struct list_head *pos;
448          list1_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) {          bool done = true;
449            list_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) {
450                  struct ccs_aggregator_entry *ptr;                  struct ccs_aggregator_entry *ptr;
451                  ptr = list1_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                  if (!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                          goto out;                  if (!done)
458                            break;
459          }          }
460          return true;          return done;
  out:  
         return false;  
461  }  }
462    
463  /**  /**
# Line 698  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 720  int ccs_delete_domain(char *domainname) Line 491  int ccs_delete_domain(char *domainname)
491          struct ccs_path_info name;          struct ccs_path_info name;
492          name.name = domainname;          name.name = domainname;
493          ccs_fill_path_info(&name);          ccs_fill_path_info(&name);
494          mutex_lock(&ccs_domain_list_lock);          mutex_lock(&ccs_policy_lock);
495          /* Is there an active domain? */          /* Is there an active domain? */
496          list1_for_each_entry(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
497                  /* Never delete ccs_kernel_domain */                  /* Never delete ccs_kernel_domain */
498                  if (domain == &ccs_kernel_domain)                  if (domain == &ccs_kernel_domain)
499                          continue;                          continue;
# Line 732  int ccs_delete_domain(char *domainname) Line 503  int ccs_delete_domain(char *domainname)
503                  domain->is_deleted = true;                  domain->is_deleted = true;
504                  break;                  break;
505          }          }
506          mutex_unlock(&ccs_domain_list_lock);          mutex_unlock(&ccs_policy_lock);
507          return 0;          return 0;
508  }  }
509    
# Line 745  int ccs_delete_domain(char *domainname) Line 516  int ccs_delete_domain(char *domainname)
516   * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.   * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
517   */   */
518  struct ccs_domain_info *ccs_find_or_assign_new_domain(const char *domainname,  struct ccs_domain_info *ccs_find_or_assign_new_domain(const char *domainname,
519                                                    const u8 profile)                                                        const u8 profile)
520  {  {
521          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *entry;
522            struct ccs_domain_info *domain;
523          const struct ccs_path_info *saved_domainname;          const struct ccs_path_info *saved_domainname;
524          mutex_lock(&ccs_domain_list_lock);          bool found = false;
525          domain = ccs_find_domain(domainname);  
526          if (domain)          if (!ccs_is_correct_domain(domainname))
527                  goto out;                  return NULL;
528          if (!ccs_is_correct_domain(domainname, __func__))          saved_domainname = ccs_get_name(domainname);
                 goto out;  
         saved_domainname = ccs_save_name(domainname);  
529          if (!saved_domainname)          if (!saved_domainname)
530                  goto out;                  return NULL;
531          /* Can I reuse memory of deleted domain? */          entry = kzalloc(sizeof(*entry), GFP_KERNEL);
532          list1_for_each_entry(domain, &ccs_domain_list, list) {          mutex_lock(&ccs_policy_lock);
533                  struct task_struct *p;          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
534                  struct ccs_acl_info *ptr;                  if (domain->is_deleted ||
535                  bool flag;                      ccs_pathcmp(saved_domainname, domain->domainname))
                 if (!domain->is_deleted ||  
                     domain->domainname != saved_domainname)  
                         continue;  
                 flag = false;  
                 /***** CRITICAL SECTION START *****/  
                 read_lock(&tasklist_lock);  
                 for_each_process(p) {  
                         if (ccs_task_domain(p) != domain)  
                                 continue;  
                         flag = true;  
                         break;  
                 }  
                 read_unlock(&tasklist_lock);  
                 /***** CRITICAL SECTION END *****/  
                 if (flag)  
536                          continue;                          continue;
537                  list1_for_each_entry(ptr, &domain->acl_info_list, list) {                  found = true;
538                          ptr->type |= ACL_DELETED;                  break;
                 }  
                 ccs_set_domain_flag(domain, true, domain->flags);  
                 domain->profile = profile;  
                 domain->quota_warned = false;  
                 mb(); /* Avoid out-of-order execution. */  
                 domain->is_deleted = false;  
                 goto out;  
         }  
         /* No memory reusable. Create using new memory. */  
         domain = ccs_alloc_element(sizeof(*domain));  
         if (domain) {  
                 INIT_LIST1_HEAD(&domain->acl_info_list);  
                 domain->domainname = saved_domainname;  
                 domain->profile = profile;  
                 list1_add_tail_mb(&domain->list, &ccs_domain_list);  
         }  
  out:  
         mutex_unlock(&ccs_domain_list_lock);  
         return domain;  
 }  
   
 /**  
  * 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;  
539          }          }
540          return true;          if (!found && ccs_memory_ok(entry, sizeof(*entry))) {
541   out:                  INIT_LIST_HEAD(&entry->acl_info_list);
542          return false;                  entry->domainname = saved_domainname;
543                    saved_domainname = NULL;
544                    entry->profile = profile;
545                    list_add_tail_rcu(&entry->list, &ccs_domain_list);
546                    domain = entry;
547                    entry = NULL;
548                    found = true;
549            }
550            mutex_unlock(&ccs_policy_lock);
551            ccs_put_name(saved_domainname);
552            kfree(entry);
553            return found ? domain : NULL;
554  }  }
555    
556  /**  /**
# Line 863  static bool ccs_get_argv0(struct ccs_exe Line 559  static bool ccs_get_argv0(struct ccs_exe
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 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 871  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 905  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? */  
                 list1_for_each_entry(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                  list1_for_each_entry(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 988  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(domain);                  ccs_audit_domain_creation_log(r->domain);
662   done:   done:
663          if (!domain) {          if (!domain) {
664                  printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",                  printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n",
665                         new_domain_name);                         ee->tmp);
666                  if (is_enforce)                  if (r->mode == CCS_CONFIG_ENFORCING)
667                          retval = -EPERM;                          retval = -EPERM;
668                  else {                  else {
669                          retval = 0;                          retval = 0;
670                          ccs_set_domain_flag(r->domain, false,                          r->domain->domain_transition_failed = true;
                                             DOMAIN_FLAGS_TRANSITION_FAILED);  
671                  }                  }
672          } else {          } else {
673                  retval = 0;                  retval = 0;
# Line 1025  static int ccs_find_next_domain(struct c Line 675  static int ccs_find_next_domain(struct c
675   out:   out:
676          if (domain)          if (domain)
677                  r->domain = domain;                  r->domain = domain;
678            if (need_kfree)
679                    kfree(rn.name);
680          return retval;          return retval;
681  }  }
682    
683  /**  /**
684   * ccs_check_environ - Check permission for environment variable names.   * ccs_environ - Check permission for environment variable names.
685   *   *
686   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve_entry".
687   *   *
688   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
689   */   */
690  static int ccs_check_environ(struct ccs_execve_entry *ee)  static int ccs_environ(struct ccs_execve_entry *ee)
691  {  {
692          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
693          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
# Line 1066  static int ccs_check_environ(struct ccs_ Line 718  static int ccs_check_environ(struct ccs_
718                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
719                          const char *kaddr = ee->dump.data;                          const char *kaddr = ee->dump.data;
720                          const unsigned char c = kaddr[offset++];                          const unsigned char c = kaddr[offset++];
721                          if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
722                                  if (c == '=') {                                  if (c == '=') {
723                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
724                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 1086  static int ccs_check_environ(struct ccs_ Line 738  static int ccs_check_environ(struct ccs_
738                          }                          }
739                          if (c)                          if (c)
740                                  continue;                                  continue;
741                          if (ccs_check_env_perm(r, arg_ptr)) {                          if (ccs_env_perm(r, arg_ptr)) {
742                                  error = -EPERM;                                  error = -EPERM;
743                                  break;                                  break;
744                          }                          }
# Line 1154  static void ccs_unescape(unsigned char * Line 806  static void ccs_unescape(unsigned char *
806  static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)  static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)
807  {  {
808          int depth = 0;          int depth = 0;
         /***** CRITICAL SECTION START *****/  
809          ccs_realpath_lock();          ccs_realpath_lock();
810          for (;;) {          for (;;) {
811                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
# Line 1169  static inline int ccs_root_depth(struct Line 820  static inline int ccs_root_depth(struct
820                  depth++;                  depth++;
821          }          }
822          ccs_realpath_unlock();          ccs_realpath_unlock();
         /***** CRITICAL SECTION END *****/  
823          return depth;          return depth;
824  }  }
825    
# Line 1186  static int ccs_get_root_depth(void) Line 836  static int ccs_get_root_depth(void)
836  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
837          struct path root;          struct path root;
838  #endif  #endif
         /***** CRITICAL SECTION START *****/  
839          read_lock(&current->fs->lock);          read_lock(&current->fs->lock);
840  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
841          root = current->fs->root;          root = current->fs->root;
# Line 1198  static int ccs_get_root_depth(void) Line 847  static int ccs_get_root_depth(void)
847          vfsmnt = mntget(current->fs->rootmnt);          vfsmnt = mntget(current->fs->rootmnt);
848  #endif  #endif
849          read_unlock(&current->fs->lock);          read_unlock(&current->fs->lock);
         /***** CRITICAL SECTION END *****/  
850          depth = ccs_root_depth(dentry, vfsmnt);          depth = ccs_root_depth(dentry, vfsmnt);
851  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
852          path_put(&root);          path_put(&root);
# Line 1209  static int ccs_get_root_depth(void) Line 857  static int ccs_get_root_depth(void)
857          return depth;          return depth;
858  }  }
859    
 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 = ccs_alloc(sizeof(*ee), false);  
         if (!ee)  
                 return NULL;  
         memset(ee, 0, sizeof(*ee));  
         ee->program_path = ccs_alloc(CCS_MAX_PATHNAME_LEN, false);  
         ee->tmp = ccs_alloc(CCS_MAX_PATHNAME_LEN, false);  
         if (!ee->program_path || !ee->tmp) {  
                 ccs_free(ee->program_path);  
                 ccs_free(ee->tmp);  
                 ccs_free(ee);  
                 return NULL;  
         }  
         /* 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 *****/  
         ccs_free(ee->program_path);  
         ccs_free(ee->tmp);  
         kfree(ee->dump.data);  
         ccs_free(ee);  
 }  
   
860  /**  /**
861   * ccs_try_alt_exec - Try to start execute handler.   * ccs_try_alt_exec - Try to start execute handler.
862   *   *
# Line 1337  static int ccs_try_alt_exec(struct ccs_e Line 910  static int ccs_try_alt_exec(struct ccs_e
910          struct task_struct *task = current;          struct task_struct *task = current;
911    
912          /* Close the requested program's dentry. */          /* Close the requested program's dentry. */
913            ee->obj.path1.dentry = NULL;
914            ee->obj.path1.mnt = NULL;
915            ee->obj.validate_done = false;
916          allow_write_access(bprm->file);          allow_write_access(bprm->file);
917          fput(bprm->file);          fput(bprm->file);
918          bprm->file = NULL;          bprm->file = NULL;
# Line 1400  static int ccs_try_alt_exec(struct ccs_e Line 976  static int ccs_try_alt_exec(struct ccs_e
976                  char *exe = (char *) ccs_get_exe();                  char *exe = (char *) ccs_get_exe();
977                  if (exe) {                  if (exe) {
978                          retval = copy_strings_kernel(1, &exe, bprm);                          retval = copy_strings_kernel(1, &exe, bprm);
979                          ccs_free(exe);                          kfree(exe);
980                  } else {                  } else {
981                          exe = ee->tmp;                          exe = ee->tmp;
982                          strncpy(ee->tmp, "<unknown>", CCS_EXEC_TMPSIZE - 1);                          snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
983                          retval = copy_strings_kernel(1, &exe, bprm);                          retval = copy_strings_kernel(1, &exe, bprm);
984                  }                  }
985                  if (retval < 0)                  if (retval < 0)
# Line 1414  static int ccs_try_alt_exec(struct ccs_e Line 990  static int ccs_try_alt_exec(struct ccs_e
990          /* Set argv[1] */          /* Set argv[1] */
991          {          {
992                  char *cp = ee->tmp;                  char *cp = ee->tmp;
993                  strncpy(ee->tmp, ccs_current_domain()->domainname->name,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
994                          CCS_EXEC_TMPSIZE - 1);                           ccs_current_domain()->domainname->name);
995                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
996                  if (retval < 0)                  if (retval < 0)
997                          goto out;                          goto out;
# Line 1425  static int ccs_try_alt_exec(struct ccs_e Line 1001  static int ccs_try_alt_exec(struct ccs_e
1001          /* Set argv[0] */          /* Set argv[0] */
1002          {          {
1003                  int depth = ccs_get_root_depth();                  int depth = ccs_get_root_depth();
1004                  char *cp = ee->program_path;                  int len = ee->handler->total_len + 1;
1005                  strncpy(cp, ee->handler->name, CCS_MAX_PATHNAME_LEN - 1);                  char *cp = kmalloc(len, GFP_KERNEL);
1006                    if (!cp) {
1007                            retval = -ENOMEM;
1008                            goto out;
1009                    }
1010                    ee->handler_path = cp;
1011                    memmove(cp, ee->handler->name, len);
1012                  ccs_unescape(cp);                  ccs_unescape(cp);
1013                  retval = -ENOENT;                  retval = -ENOENT;
1014                  if (!*cp || *cp != '/')                  if (!*cp || *cp != '/')
# Line 1438  static int ccs_try_alt_exec(struct ccs_e Line 1020  static int ccs_try_alt_exec(struct ccs_e
1020                                  goto out;                                  goto out;
1021                          depth--;                          depth--;
1022                  }                  }
1023                  memmove(ee->program_path, cp, strlen(cp) + 1);                  memmove(ee->handler_path, cp, strlen(cp) + 1);
1024                  cp = ee->program_path;                  cp = ee->handler_path;
1025                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1026                  if (retval < 0)                  if (retval < 0)
1027                          goto out;                          goto out;
# Line 1452  static int ccs_try_alt_exec(struct ccs_e Line 1034  static int ccs_try_alt_exec(struct ccs_e
1034  #endif  #endif
1035    
1036          /* OK, now restart the process with execute handler program's dentry. */          /* OK, now restart the process with execute handler program's dentry. */
1037          filp = open_exec(ee->program_path);          filp = open_exec(ee->handler_path);
1038          if (IS_ERR(filp)) {          if (IS_ERR(filp)) {
1039                  retval = PTR_ERR(filp);                  retval = PTR_ERR(filp);
1040                  goto out;                  goto out;
1041          }          }
1042            ee->obj.path1.dentry = filp->f_dentry;
1043            ee->obj.path1.mnt = filp->f_vfsmnt;
1044          bprm->file = filp;          bprm->file = filp;
1045          bprm->filename = ee->program_path;          bprm->filename = ee->handler_path;
1046  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1047          bprm->interp = bprm->filename;          bprm->interp = bprm->filename;
1048  #endif  #endif
1049          retval = prepare_binprm(bprm);          retval = prepare_binprm(bprm);
1050          if (retval < 0)          if (retval < 0)
1051                  goto out;                  goto out;
1052          {          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1053                  /*          retval = ccs_find_next_domain(ee);
1054                   * 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 = kmalloc(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);  
         }  
1055   out:   out:
1056          return retval;          return retval;
1057  }  }
# Line 1503  static int ccs_try_alt_exec(struct ccs_e Line 1063  static int ccs_try_alt_exec(struct ccs_e
1063   * @type: Type of execute handler.   * @type: Type of execute handler.
1064   *   *
1065   * Returns true if found, false otherwise.   * Returns true if found, false otherwise.
1066     *
1067     * Caller holds ccs_read_lock().
1068   */   */
1069  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,
1070                                       const u8 type)                                       const u8 type)
# Line 1510  static bool ccs_find_execute_handler(str Line 1072  static bool ccs_find_execute_handler(str
1072          struct task_struct *task = current;          struct task_struct *task = current;
1073          const struct ccs_domain_info *domain = ccs_current_domain();          const struct ccs_domain_info *domain = ccs_current_domain();
1074          struct ccs_acl_info *ptr;          struct ccs_acl_info *ptr;
1075            bool found = false;
1076          /*          /*
1077           * Don't use execute handler if the current process is           * Don't use execute handler if the current process is
1078           * marked as execute handler to avoid infinite execute handler loop.           * marked as execute handler to avoid infinite execute handler loop.
1079           */           */
1080          if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)          if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
1081                  return false;                  return false;
1082          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          list_for_each_entry(ptr, &domain->acl_info_list, list) {
1083                  struct ccs_execute_handler_record *acl;                  struct ccs_execute_handler_record *acl;
1084                  if (ptr->type != type)                  if (ptr->type != type)
1085                          continue;                          continue;
1086                  acl = container_of(ptr, struct ccs_execute_handler_record,                  acl = container_of(ptr, struct ccs_execute_handler_record,
1087                                     head);                                     head);
1088                  ee->handler = acl->handler;                  ee->handler = acl->handler;
1089                  return true;                  found = true;
1090                    break;
1091          }          }
1092          return false;          return found;
1093  }  }
1094    
1095  /**  /**
# Line 1541  bool ccs_dump_page(struct linux_binprm * Line 1105  bool ccs_dump_page(struct linux_binprm *
1105                     struct ccs_page_dump *dump)                     struct ccs_page_dump *dump)
1106  {  {
1107          struct page *page;          struct page *page;
1108          /* dump->data is released by ccs_free_execve_entry(). */          /* dump->data is released by ccs_finish_execve(). */
1109          if (!dump->data) {          if (!dump->data) {
1110                  dump->data = kmalloc(PAGE_SIZE, GFP_KERNEL);                  dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);
1111                  if (!dump->data)                  if (!dump->data)
1112                          return false;                          return false;
1113          }          }
# Line 1551  bool ccs_dump_page(struct linux_binprm * Line 1115  bool ccs_dump_page(struct linux_binprm *
1115  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1116          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)
1117                  return false;                  return false;
1118  #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)
1119            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1120                    return false;
1121    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1122          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)
1123                  return false;                  return false;
1124  #else  #else
# Line 1572  bool ccs_dump_page(struct linux_binprm * Line 1139  bool ccs_dump_page(struct linux_binprm *
1139          /* Same with put_arg_page(page) in fs/exec.c */          /* Same with put_arg_page(page) in fs/exec.c */
1140  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1141          put_page(page);          put_page(page);
1142  #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)
1143            put_page(page);
1144    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1145          put_page(page);          put_page(page);
1146  #endif  #endif
1147          return true;          return true;
1148  }  }
1149    
1150  /**  /**
  * 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->next_domain;  
         if (!next_domain)  
                 next_domain = ccs_current_domain();  
         return next_domain;  
 }  
   
 /**  
1151   * ccs_start_execve - Prepare for execve() operation.   * ccs_start_execve - Prepare for execve() operation.
1152   *   *
1153   * @bprm: Pointer to "struct linux_binprm".   * @bprm: Pointer to "struct linux_binprm".
1154     * @eep:  Pointer to "struct ccs_execve_entry *".
1155   *   *
1156   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1157   */   */
1158  int ccs_start_execve(struct linux_binprm *bprm)  int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve_entry **eep)
1159  {  {
1160          int retval;          int retval;
1161          struct task_struct *task = current;          struct task_struct *task = current;
1162          struct ccs_execve_entry *ee = ccs_allocate_execve_entry();          struct ccs_execve_entry *ee;
1163            *eep = NULL;
1164          if (!ccs_policy_loaded)          if (!ccs_policy_loaded)
1165                  ccs_load_policy(bprm->filename);                  ccs_load_policy(bprm->filename);
1166            ee = kzalloc(sizeof(*ee), GFP_KERNEL);
1167          if (!ee)          if (!ee)
1168                  return -ENOMEM;                  return -ENOMEM;
1169          ccs_init_request_info(&ee->r, NULL, CCS_MAC_FOR_FILE);          ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
1170            if (!ee->tmp) {
1171                    kfree(ee);
1172                    return -ENOMEM;
1173            }
1174            ee->reader_idx = ccs_read_lock();
1175            /* ee->dump->data is allocated by ccs_dump_page(). */
1176            ee->previous_domain = task->ccs_domain_info;
1177            /* Clear manager flag. */
1178            task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;
1179            /* Tell GC that I started execve(). */
1180            task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
1181            /*
1182             * Make task->ccs_flags visible to GC before changing
1183             * task->ccs_domain_info .
1184             */
1185            smp_mb();
1186            *eep = ee;
1187            ccs_init_request_info(&ee->r, NULL, CCS_MAC_FILE_EXECUTE);
1188          ee->r.ee = ee;          ee->r.ee = ee;
1189          ee->bprm = bprm;          ee->bprm = bprm;
1190          ee->r.obj = &ee->obj;          ee->r.obj = &ee->obj;
1191          ee->obj.path1_dentry = bprm->file->f_dentry;          ee->obj.path1.dentry = bprm->file->f_dentry;
1192          ee->obj.path1_vfsmnt = bprm->file->f_vfsmnt;          ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1193          /* 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)) {  
1194                  retval = ccs_try_alt_exec(ee);                  retval = ccs_try_alt_exec(ee);
1195                  if (!retval)                  if (!retval)
1196                          ccs_audit_execute_handler_log(ee, true);                          ccs_audit_execute_handler_log(ee, true);
# Line 1628  int ccs_start_execve(struct linux_binprm Line 1199  int ccs_start_execve(struct linux_binprm
1199          retval = ccs_find_next_domain(ee);          retval = ccs_find_next_domain(ee);
1200          if (retval != -EPERM)          if (retval != -EPERM)
1201                  goto ok;                  goto ok;
1202          if (ccs_find_execute_handler(ee, TYPE_DENIED_EXECUTE_HANDLER)) {          if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) {
1203                  retval = ccs_try_alt_exec(ee);                  retval = ccs_try_alt_exec(ee);
1204                  if (!retval)                  if (!retval)
1205                          ccs_audit_execute_handler_log(ee, false);                          ccs_audit_execute_handler_log(ee, false);
# Line 1636  int ccs_start_execve(struct linux_binprm Line 1207  int ccs_start_execve(struct linux_binprm
1207   ok:   ok:
1208          if (retval < 0)          if (retval < 0)
1209                  goto out;                  goto out;
1210          ee->r.mode = ccs_check_flags(ee->r.domain, CCS_MAC_FOR_ENV);          /*
1211          retval = ccs_check_environ(ee);           * Proceed to the next domain in order to allow reaching via PID.
1212             * It will be reverted if execve() failed. Reverting is not good.
1213             * But it is better than being unable to reach via PID in interactive
1214             * enforcing mode.
1215             */
1216            task->ccs_domain_info = ee->r.domain;
1217            ee->r.mode = ccs_get_mode(ee->r.domain->profile, CCS_MAC_ENVIRON);
1218            retval = ccs_environ(ee);
1219          if (retval < 0)          if (retval < 0)
1220                  goto out;                  goto out;
         ee->next_domain = ee->r.domain;  
         task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC;  
1221          retval = 0;          retval = 0;
1222   out:   out:
         if (retval)  
                 ccs_finish_execve(retval);  
1223          return retval;          return retval;
1224  }  }
1225    
# Line 1653  int ccs_start_execve(struct linux_binprm Line 1227  int ccs_start_execve(struct linux_binprm
1227   * ccs_finish_execve - Clean up execve() operation.   * ccs_finish_execve - Clean up execve() operation.
1228   *   *
1229   * @retval: Return code of an execve() operation.   * @retval: Return code of an execve() operation.
1230     * @ee:     Pointer to "struct ccs_execve_entry".
1231     *
1232     * Caller holds ccs_read_lock().
1233   */   */
1234  void ccs_finish_execve(int retval)  void ccs_finish_execve(int retval, struct ccs_execve_entry *ee)
1235  {  {
1236          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;  
1237          if (!ee)          if (!ee)
1238                  return;                  return;
1239          if (retval < 0)          if (retval < 0) {
1240                  goto out;                  task->ccs_domain_info = ee->previous_domain;
1241          /* Proceed to next domain if execution suceeded. */                  /*
1242          task->ccs_domain_info = ee->r.domain;                   * Make task->ccs_domain_info visible to GC before changing
1243          mb(); /* Make domain transition visible to other CPUs. */                   * task->ccs_flags .
1244          /* Mark the current process as execute handler. */                   */
1245          if (ee->handler)                  smp_mb();
1246                  task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;          } else {
1247          /* Mark the current process as normal process. */                  /* Mark the current process as execute handler. */
1248          else                  if (ee->handler)
1249                  task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;                          task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1250   out:                  /* Mark the current process as normal process. */
1251          ccs_free_execve_entry(ee);                  else
1252  }                          task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1253            }
1254  #else          /* Tell GC that I finished execve(). */
1255            task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1256  /**          ccs_read_unlock(ee->reader_idx);
1257   * ccs_start_execve - Prepare for execve() operation.          kfree(ee->handler_path);
1258   *          kfree(ee->tmp);
1259   * @bprm: Pointer to "struct linux_binprm".          kfree(ee->dump.data);
1260   *          kfree(ee);
  * Returns 0.  
  */  
 int ccs_start_execve(struct linux_binprm *bprm)  
 {  
 #ifdef CONFIG_SAKURA  
         /* Clear manager flag. */  
         current->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;  
         if (!ccs_policy_loaded)  
                 ccs_load_policy(bprm->filename);  
 #endif  
         return 0;  
 }  
   
 /**  
  * ccs_finish_execve - Clean up execve() operation.  
  */  
 void ccs_finish_execve(int retval)  
 {  
1261  }  }
   
 #endif  

Legend:
Removed from v.2393  
changed lines
  Added in v.3109

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