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

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 1687 by kumaneko, Fri Oct 10 13:53:24 2008 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/domain.c revision 3949 by kumaneko, Sun Sep 5 11:50:03 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * fs/tomoyo_domain.c   * security/ccsecurity/domain.c
3   *   *
4   * Implementation of the Domain-Based Mandatory Access Control.   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Copyright (C) 2005-2008  NTT DATA CORPORATION   * Version: 1.8.0-pre   2010/09/01
  *  
  * Version: 1.6.5-pre   2008/10/07  
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/binfmts.h>  #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    
27  /* The initial domain. */  /* The global domain. */
28  struct domain_info KERNEL_DOMAIN;  struct ccs_domain_info ccs_acl_group[CCS_MAX_ACL_GROUPS];
   
 /* The list for "struct domain_info". */  
 LIST1_HEAD(domain_list);  
29    
30  #ifdef CONFIG_TOMOYO  /* The initial domain. */
31    struct ccs_domain_info ccs_kernel_domain;
 /* Lock for appending domain's ACL. */  
 DEFINE_MUTEX(domain_acl_lock);  
   
 /* Domain creation lock. */  
 static DEFINE_MUTEX(new_domain_assign_lock);  
   
 /* Structure for "initialize_domain" and "no_initialize_domain" keyword. */  
 struct domain_initializer_entry {  
         struct list1_head list;  
         const struct path_info *domainname;    /* This may be NULL */  
         const struct 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 domain_keeper_entry {  
         struct list1_head list;  
         const struct path_info *domainname;  
         const struct 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 aggregator_entry {  
         struct list1_head list;  
         const struct path_info *original_name;  
         const struct path_info *aggregated_name;  
         bool is_deleted;  
 };  
   
 /* Structure for "alias" keyword. */  
 struct alias_entry {  
         struct list1_head list;  
         const struct path_info *original_name;  
         const struct path_info *aliased_name;  
         bool is_deleted;  
 };  
   
 /**  
  * ccs_set_domain_flag - Set or clear domain's attribute flags.  
  *  
  * @domain:    Pointer to "struct 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 domain_info *domain, const bool is_delete,  
                          const u8 flags)  
 {  
         mutex_lock(&new_domain_assign_lock);  
         if (!is_delete)  
                 domain->flags |= flags;  
         else  
                 domain->flags &= ~flags;  
         mutex_unlock(&new_domain_assign_lock);  
 }  
32    
33  /**  /* The list for "struct ccs_domain_info". */
34   * ccs_get_last_name - Get last component of a domainname.  LIST_HEAD(ccs_domain_list);
  *  
  * @domain: Pointer to "struct domain_info".  
  *  
  * Returns the last component of the domainname.  
  */  
 const char *ccs_get_last_name(const struct domain_info *domain)  
 {  
         const char *cp0 = domain->domainname->name;  
         const char *cp1 = strrchr(cp0, ' ');  
         if (cp1)  
                 return cp1 + 1;  
         return cp0;  
 }  
35    
36  /**  struct list_head ccs_policy_list[CCS_MAX_POLICY];
37   * ccs_add_domain_acl - Add the given ACL to the given domain.  struct list_head ccs_group_list[CCS_MAX_GROUP];
38   *  struct list_head ccs_shared_list[CCS_MAX_LIST];
  * @domain: Pointer to "struct domain_info". May be NULL.  
  * @acl:    Pointer to "struct acl_info".  
  *  
  * Returns 0.  
  */  
 int ccs_add_domain_acl(struct domain_info *domain, struct acl_info *acl)  
 {  
         if (domain)  
                 list1_add_tail_mb(&acl->list, &domain->acl_info_list);  
         else  
                 acl->type &= ~ACL_DELETED;  
         ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);  
         return 0;  
 }  
39    
40  /**  /**
41   * ccs_del_domain_acl - Delete the given ACL from the domain.   * ccs_update_policy - Update an entry for exception policy.
42   *   *
43   * @acl: Pointer to "struct acl_info". May be NULL.   * @new_entry:       Pointer to "struct ccs_acl_info".
44   *   * @size:            Size of @new_entry in bytes.
45   * Returns 0.   * @is_delete:       True if it is a delete request.
46   */   * @list:            Pointer to "struct list_head".
47  int ccs_del_domain_acl(struct acl_info *acl)   * @check_duplicate: Callback function to find duplicated entry.
 {  
         if (acl)  
                 acl->type |= ACL_DELETED;  
         ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);  
         return 0;  
 }  
   
 /**  
  * audit_execute_handler_log - Audit execute_handler log.  
  *  
  * @is_default: True if it is "execute_handler" log.  
  * @handler:    The realpath of the handler.  
  * @bprm:       Pointer to "struct linux_binprm".  
48   *   *
49   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
  */  
 static int audit_execute_handler_log(const bool is_default,  
                                      const char *handler,  
                                      struct linux_binprm *bprm)  
 {  
         struct ccs_request_info r;  
         ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_FILE);  
         r.bprm = bprm;  
         return ccs_write_audit_log(true, &r, "%s %s\n",  
                                    is_default ? KEYWORD_EXECUTE_HANDLER :  
                                    KEYWORD_DENIED_EXECUTE_HANDLER, handler);  
 }  
   
 /**  
  * audit_domain_creation_log - Audit domain creation log.  
50   *   *
51   * @domain:  Pointer to "struct domain_info".   * Caller holds ccs_read_lock().
  *  
  * Returns 0 on success, negative value otherwise.  
52   */   */
53  static int audit_domain_creation_log(struct domain_info *domain)  int ccs_update_policy(struct ccs_acl_head *new_entry, const int size,
54  {                        bool is_delete, struct list_head *list,
55          struct ccs_request_info r;                        bool (*check_duplicate) (const struct ccs_acl_head *,
56          ccs_init_request_info(&r, domain, CCS_TOMOYO_MAC_FOR_FILE);                                                 const struct ccs_acl_head *))
57          return ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile);  {
58  }          int error = is_delete ? -ENOENT : -ENOMEM;
59            struct ccs_acl_head *entry;
60  /* The list for "struct domain_initializer_entry". */          if (mutex_lock_interruptible(&ccs_policy_lock))
 static LIST1_HEAD(domain_initializer_list);  
   
 /**  
  * update_domain_initializer_entry - Update "struct domain_initializer_entry" list.  
  *  
  * @domainname: The name of domain. May be NULL.  
  * @program:    The name of program.  
  * @is_not:     True if it is "no_initialize_domain" entry.  
  * @is_delete:  True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int update_domain_initializer_entry(const char *domainname,  
                                            const char *program,  
                                            const bool is_not,  
                                            const bool is_delete)  
 {  
         struct domain_initializer_entry *new_entry;  
         struct domain_initializer_entry *ptr;  
         static DEFINE_MUTEX(lock);  
         const struct path_info *saved_program;  
         const struct path_info *saved_domainname = NULL;  
         int error = -ENOMEM;  
         bool is_last_name = false;  
         if (!ccs_is_correct_path(program, 1, -1, -1, __func__))  
                 return -EINVAL; /* No patterns allowed. */  
         if (domainname) {  
                 if (!ccs_is_domain_def(domainname) &&  
                     ccs_is_correct_path(domainname, 1, -1, -1, __func__))  
                         is_last_name = true;  
                 else if (!ccs_is_correct_domain(domainname, __func__))  
                         return -EINVAL;  
                 saved_domainname = ccs_save_name(domainname);  
                 if (!saved_domainname)  
                         return -ENOMEM;  
         }  
         saved_program = ccs_save_name(program);  
         if (!saved_program)  
61                  return -ENOMEM;                  return -ENOMEM;
62          mutex_lock(&lock);          list_for_each_entry_rcu(entry, list, list) {
63          list1_for_each_entry(ptr, &domain_initializer_list, list) {                  if (!check_duplicate(entry, new_entry))
                 if (ptr->is_not != is_not ||  
                     ptr->domainname != saved_domainname ||  
                     ptr->program != saved_program)  
64                          continue;                          continue;
65                  ptr->is_deleted = is_delete;                  entry->is_deleted = is_delete;
66                  error = 0;                  error = 0;
67                  goto out;                  break;
68          }          }
69          if (is_delete) {          if (error && !is_delete) {
70                  error = -ENOENT;                  entry = ccs_commit_ok(new_entry, size);
71                  goto out;                  if (entry) {
72                            list_add_tail_rcu(&entry->list, list);
73                            error = 0;
74                    }
75          }          }
76          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, &domain_initializer_list);  
         error = 0;  
  out:  
         mutex_unlock(&lock);  
         ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);  
77          return error;          return error;
78  }  }
79    
80  /**  static inline bool ccs_same_acl_head(const struct ccs_acl_info *p1,
81   * ccs_read_domain_initializer_policy - Read "struct domain_initializer_entry" list.                                       const struct ccs_acl_info *p2)
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  */  
 bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head)  
82  {  {
83          struct list1_head *pos;          return p1->type == p2->type && p1->cond == p2->cond;
         list1_for_each_cookie(pos, head->read_var2, &domain_initializer_list) {  
                 const char *no;  
                 const char *from = "";  
                 const char *domain = "";  
                 struct domain_initializer_entry *ptr;  
                 ptr = list1_entry(pos, struct domain_initializer_entry, list);  
                 if (ptr->is_deleted)  
                         continue;  
                 no = ptr->is_not ? "no_" : "";  
                 if (ptr->domainname) {  
                         from = " from ";  
                         domain = ptr->domainname->name;  
                 }  
                 if (!ccs_io_printf(head,  
                                    "%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n",  
                                    no, ptr->program->name, from, domain))  
                                 goto out;  
         }  
         return true;  
  out:  
         return false;  
84  }  }
85    
86  /**  /**
87   * ccs_write_domain_initializer_policy - Write "struct domain_initializer_entry" list.   * ccs_update_domain - Update an entry for domain policy.
88   *   *
89   * @data:      String to parse.   * @new_entry:       Pointer to "struct ccs_acl_info".
90   * @is_not:    True if it is "no_initialize_domain" entry.   * @size:            Size of @new_entry in bytes.
91   * @is_delete: True if it is a delete request.   * @param:           Pointer to "struct ccs_acl_param".
92     * @check_duplicate: Callback function to find duplicated entry.
93     * @merge_duplicate: Callback function to merge duplicated entry. Maybe NULL.
94   *   *
95   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
96     *
97     * Caller holds ccs_read_lock().
98   */   */
99  int ccs_write_domain_initializer_policy(char *data, const bool is_not,  int ccs_update_domain(struct ccs_acl_info *new_entry, const int size,
100                                          const bool is_delete)                        struct ccs_acl_param *param,
101  {                        bool (*check_duplicate) (const struct ccs_acl_info *,
102          char *cp = strstr(data, " from ");                                                 const struct ccs_acl_info *),
103          if (cp) {                        bool (*merge_duplicate) (struct ccs_acl_info *,
104                  *cp = '\0';                                                 struct ccs_acl_info *,
105                  return update_domain_initializer_entry(cp + 6, data, is_not,                                                 const bool))
106                                                         is_delete);  {
107            int error = param->is_delete ? -ENOENT : -ENOMEM;
108            struct ccs_acl_info *entry;
109            const u8 type = new_entry->type;
110            const u8 i = type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
111                    type == CCS_TYPE_DENIED_EXECUTE_HANDLER ||
112                    type == CCS_TYPE_AUTO_TASK_ACL;
113            const bool is_delete = param->is_delete;
114            struct ccs_domain_info * const domain = param->domain;
115            new_entry->cond = param->condition;
116            if (mutex_lock_interruptible(&ccs_policy_lock))
117                    return error;
118            list_for_each_entry_rcu(entry, &domain->acl_info_list[i], list) {
119                    if (!ccs_same_acl_head(entry, new_entry) ||
120                        !check_duplicate(entry, new_entry))
121                            continue;
122                    if (merge_duplicate)
123                            entry->is_deleted = merge_duplicate(entry, new_entry,
124                                                                is_delete);
125                    else
126                            entry->is_deleted = is_delete;
127                    error = 0;
128                    break;
129            }
130            if (error && !is_delete) {
131                    entry = ccs_commit_ok(new_entry, size);
132                    if (entry) {
133                            if (entry->cond)
134                                    atomic_inc(&entry->cond->head.users);
135                            list_add_tail_rcu(&entry->list,
136                                              &domain->acl_info_list[i]);
137                            error = 0;
138                    }
139          }          }
140          return update_domain_initializer_entry(NULL, data, is_not, is_delete);          mutex_unlock(&ccs_policy_lock);
141            return error;
142  }  }
143    
144  /**  void ccs_check_acl(struct ccs_request_info *r,
145   * is_domain_initializer - Check whether the given program causes domainname reinitialization.                     bool (*check_entry) (struct ccs_request_info *,
146   *                                          const struct ccs_acl_info *))
147   * @domainname: The name of domain.  {
148   * @program:    The name of program.          const struct ccs_domain_info *domain = ccs_current_domain();
149   * @last_name:  The last component of @domainname.          struct ccs_acl_info *ptr;
150   *          bool retried = false;
151   * Returns true if executing @program reinitializes domain transition,          const u8 i = !check_entry;
152   * false otherwise.   retry:
153   */          list_for_each_entry_rcu(ptr, &domain->acl_info_list[i], list) {
 static bool is_domain_initializer(const struct path_info *domainname,  
                                   const struct path_info *program,  
                                   const struct path_info *last_name)  
 {  
         struct domain_initializer_entry *ptr;  
         bool flag = false;  
         list1_for_each_entry(ptr,  &domain_initializer_list, list) {  
154                  if (ptr->is_deleted)                  if (ptr->is_deleted)
155                          continue;                          continue;
156                  if (ptr->domainname) {                  if (ptr->type != r->param_type)
                         if (!ptr->is_last_name) {  
                                 if (ptr->domainname != domainname)  
                                         continue;  
                         } else {  
                                 if (ccs_pathcmp(ptr->domainname, last_name))  
                                         continue;  
                         }  
                 }  
                 if (ccs_pathcmp(ptr->program, program))  
157                          continue;                          continue;
158                  if (ptr->is_not)                  if (check_entry && !check_entry(r, ptr))
159                          return false;                          continue;
160                  flag = true;                  if (!ccs_condition(r, ptr->cond))
161                            continue;
162                    r->matched_acl = ptr;
163                    r->granted = true;
164                    return;
165            }
166            if (!retried) {
167                    retried = true;
168                    domain = &ccs_acl_group[domain->group];
169                    goto retry;
170          }          }
171          return flag;          r->granted = false;
172  }  }
173    
174  /* The list for "struct domain_keeper_entry". */  static bool ccs_same_transition_control(const struct ccs_acl_head *a,
175  static LIST1_HEAD(domain_keeper_list);                                          const struct ccs_acl_head *b)
176    {
177            const struct ccs_transition_control *p1 = container_of(a, typeof(*p1),
178                                                                   head);
179            const struct ccs_transition_control *p2 = container_of(b, typeof(*p2),
180                                                                   head);
181            return p1->type == p2->type && p1->is_last_name == p2->is_last_name
182                    && p1->domainname == p2->domainname
183                    && p1->program == p2->program;
184    }
185    
186  /**  /**
187   * update_domain_keeper_entry - Update "struct domain_keeper_entry" list.   * ccs_update_transition_control_entry - Update "struct ccs_transition_control" list.
188   *   *
189   * @domainname: The name of domain.   * @domainname: The name of domain. Maybe NULL.
190   * @program:    The name of program. May be NULL.   * @program:    The name of program. Maybe NULL.
191   * @is_not:     True if it is "no_keep_domain" entry.   * @type:       Type of transition.
192   * @is_delete:  True if it is a delete request.   * @is_delete:  True if it is a delete request.
193   *   *
194   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
195   */   */
196  static int update_domain_keeper_entry(const char *domainname,  static int ccs_update_transition_control_entry(const char *domainname,
197                                        const char *program,                                                 const char *program,
198                                        const bool is_not, const bool is_delete)                                                 const u8 type,
199  {                                                 const bool is_delete)
200          struct domain_keeper_entry *new_entry;  {
201          struct domain_keeper_entry *ptr;          struct ccs_transition_control e = { .type = type };
202          const struct path_info *saved_domainname;          int error = is_delete ? -ENOENT : -ENOMEM;
203          const struct path_info *saved_program = NULL;          if (program && strcmp(program, "any")) {
204          static DEFINE_MUTEX(lock);                  if (!ccs_correct_path(program))
         int error = -ENOMEM;  
         bool is_last_name = false;  
         if (!ccs_is_domain_def(domainname) &&  
             ccs_is_correct_path(domainname, 1, -1, -1, __func__))  
                 is_last_name = true;  
         else if (!ccs_is_correct_domain(domainname, __func__))  
                 return -EINVAL;  
         if (program) {  
                 if (!ccs_is_correct_path(program, 1, -1, -1, __func__))  
205                          return -EINVAL;                          return -EINVAL;
206                  saved_program = ccs_save_name(program);                  e.program = ccs_get_name(program);
207                  if (!saved_program)                  if (!e.program)
208                          return -ENOMEM;                          goto out;
         }  
         saved_domainname = ccs_save_name(domainname);  
         if (!saved_domainname)  
                 return -ENOMEM;  
         mutex_lock(&lock);  
         list1_for_each_entry(ptr, &domain_keeper_list, list) {  
                 if (ptr->is_not != is_not ||  
                     ptr->domainname != saved_domainname ||  
                     ptr->program != saved_program)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 goto out;  
209          }          }
210          if (is_delete) {          if (domainname && strcmp(domainname, "any")) {
211                  error = -ENOENT;                  if (!ccs_correct_domain(domainname)) {
212                  goto out;                          if (!ccs_correct_path(domainname))
213                                    goto out;
214                            e.is_last_name = true;
215                    }
216                    e.domainname = ccs_get_name(domainname);
217                    if (!e.domainname)
218                            goto out;
219          }          }
220          new_entry = ccs_alloc_element(sizeof(*new_entry));          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
221          if (!new_entry)                                    &ccs_policy_list[CCS_ID_TRANSITION_CONTROL],
222                  goto out;                                    ccs_same_transition_control);
         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, &domain_keeper_list);  
         error = 0;  
223   out:   out:
224          mutex_unlock(&lock);          ccs_put_name(e.domainname);
225          ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);          ccs_put_name(e.program);
226          return error;          return error;
227  }  }
228    
229  /**  /**
230   * ccs_write_domain_keeper_policy - Write "struct domain_keeper_entry" list.   * ccs_write_transition_control - Write "struct ccs_transition_control" list.
231   *   *
232   * @data:      String to parse.   * @data:      String to parse.
  * @is_not:    True if it is "no_keep_domain" entry.  
233   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
234     * @type:      Type of this entry.
235   *   *
236     * Returns 0 on success, negative value otherwise.
237   */   */
238  int ccs_write_domain_keeper_policy(char *data, const bool is_not,  int ccs_write_transition_control(char *data, const bool is_delete,
239                                     const bool is_delete)                                   const u8 type)
 {  
         char *cp = strstr(data, " from ");  
         if (cp) {  
                 *cp = '\0';  
                 return update_domain_keeper_entry(cp + 6, data,  
                                                   is_not, is_delete);  
         }  
         return update_domain_keeper_entry(data, NULL, is_not, is_delete);  
 }  
   
 /**  
  * ccs_read_domain_keeper_policy - Read "struct domain_keeper_entry" list.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  */  
 bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head)  
240  {  {
241          struct list1_head *pos;          char *domainname = strstr(data, " from ");
242          list1_for_each_cookie(pos, head->read_var2, &domain_keeper_list) {          if (domainname) {
243                  struct domain_keeper_entry *ptr;                  *domainname = '\0';
244                  const char *no;                  domainname += 6;
245                  const char *from = "";          } else if (type == CCS_TRANSITION_CONTROL_NO_KEEP ||
246                  const char *program = "";                     type == CCS_TRANSITION_CONTROL_KEEP) {
247                  ptr = list1_entry(pos, struct domain_keeper_entry, list);                  domainname = data;
248                  if (ptr->is_deleted)                  data = NULL;
                         continue;  
                 no = ptr->is_not ? "no_" : "";  
                 if (ptr->program) {  
                         from = " from ";  
                         program = ptr->program->name;  
                 }  
                 if (!ccs_io_printf(head,  
                                    "%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no,  
                                    program, from, ptr->domainname->name))  
                                 goto out;  
249          }          }
250          return true;          return ccs_update_transition_control_entry(domainname, data, type,
251   out:                                                     is_delete);
         return false;  
252  }  }
253    
254  /**  /**
255   * is_domain_keeper - Check whether the given program causes domain transition suppression.   * ccs_transition_type - Get domain transition type.
256   *   *
257   * @domainname: The name of domain.   * @domainname: The name of domain.
258   * @program:    The name of program.   * @program:    The name of program.
  * @last_name:  The last component of @domainname.  
259   *   *
260   * Returns true if executing @program supresses domain transition,   * Returns CCS_TRANSITION_CONTROL_INITIALIZE if executing @program
261   * false otherwise.   * reinitializes domain transition, CCS_TRANSITION_CONTROL_KEEP if executing
262   */   * @program suppresses domain transition, others otherwise.
263  static bool is_domain_keeper(const struct path_info *domainname,   *
264                               const struct path_info *program,   * Caller holds ccs_read_lock().
265                               const struct path_info *last_name)   */
266  {  static u8 ccs_transition_type(const struct ccs_path_info *domainname,
267          struct domain_keeper_entry *ptr;                                const struct ccs_path_info *program)
268          bool flag = false;  {
269          list1_for_each_entry(ptr, &domain_keeper_list, list) {          const struct ccs_transition_control *ptr;
270                  if (ptr->is_deleted)          const char *last_name = ccs_last_word(domainname->name);
271                          continue;          u8 type;
272                  if (!ptr->is_last_name) {          for (type = 0; type < CCS_MAX_TRANSITION_TYPE; type++) {
273                          if (ptr->domainname != domainname)   next:
274                    list_for_each_entry_rcu(ptr, &ccs_policy_list
275                                            [CCS_ID_TRANSITION_CONTROL],
276                                            head.list) {
277                            if (ptr->head.is_deleted || ptr->type != type)
278                                  continue;                                  continue;
279                  } else {                          if (ptr->domainname) {
280                          if (ccs_pathcmp(ptr->domainname, last_name))                                  if (!ptr->is_last_name) {
281                                            if (ptr->domainname != domainname)
282                                                    continue;
283                                    } else {
284                                            /*
285                                             * Use direct strcmp() since this is
286                                             * unlikely used.
287                                             */
288                                            if (strcmp(ptr->domainname->name,
289                                                       last_name))
290                                                    continue;
291                                    }
292                            }
293                            if (ptr->program && ccs_pathcmp(ptr->program, program))
294                                  continue;                                  continue;
295                            if (type == CCS_TRANSITION_CONTROL_NO_INITIALIZE) {
296                                    /*
297                                     * Do not check for initialize_domain if
298                                     * no_initialize_domain matched.
299                                     */
300                                    type = CCS_TRANSITION_CONTROL_NO_KEEP;
301                                    goto next;
302                            }
303                            goto done;
304                  }                  }
                 if (ptr->program && ccs_pathcmp(ptr->program, program))  
                         continue;  
                 if (ptr->is_not)  
                         return false;  
                 flag = true;  
305          }          }
306          return flag;   done:
307  }          return type;
   
 /* The list for "struct alias_entry". */  
 static LIST1_HEAD(alias_list);  
   
 /**  
  * update_alias_entry - Update "struct 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 update_alias_entry(const char *original_name,  
                               const char *aliased_name,  
                               const bool is_delete)  
 {  
         struct alias_entry *new_entry;  
         struct alias_entry *ptr;  
         static DEFINE_MUTEX(lock);  
         const struct path_info *saved_original_name;  
         const struct 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, &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, &alias_list);  
         error = 0;  
  out:  
         mutex_unlock(&lock);  
         ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);  
         return error;  
308  }  }
309    
310  /**  static bool ccs_same_aggregator(const struct ccs_acl_head *a,
311   * ccs_read_alias_policy - Read "struct alias_entry" list.                                  const struct ccs_acl_head *b)
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  */  
 bool ccs_read_alias_policy(struct ccs_io_buffer *head)  
312  {  {
313          struct list1_head *pos;          const struct ccs_aggregator *p1 = container_of(a, typeof(*p1), head);
314          list1_for_each_cookie(pos, head->read_var2, &alias_list) {          const struct ccs_aggregator *p2 = container_of(b, typeof(*p2), head);
315                  struct alias_entry *ptr;          return p1->original_name == p2->original_name &&
316                  ptr = list1_entry(pos, struct alias_entry, list);                  p1->aggregated_name == p2->aggregated_name;
                 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;  
317  }  }
318    
319  /**  /**
320   * ccs_write_alias_policy - Write "struct alias_entry" list.   * ccs_update_aggregator_entry - Update "struct ccs_aggregator" 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 update_alias_entry(data, cp, is_delete);  
 }  
   
 /* The list for "struct aggregator_entry". */  
 static LIST1_HEAD(aggregator_list);  
   
 /**  
  * update_aggregator_entry - Update "struct aggregator_entry" list.  
321   *   *
322   * @original_name:   The original program's name.   * @original_name:   The original program's name.
323   * @aggregated_name: The aggregated program's name.   * @aggregated_name: The aggregated program's name.
# Line 608  static LIST1_HEAD(aggregator_list); Line 325  static LIST1_HEAD(aggregator_list);
325   *   *
326   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
327   */   */
328  static int update_aggregator_entry(const char *original_name,  static int ccs_update_aggregator_entry(const char *original_name,
329                                     const char *aggregated_name,                                         const char *aggregated_name,
330                                     const bool is_delete)                                         const bool is_delete)
331  {  {
332          struct aggregator_entry *new_entry;          struct ccs_aggregator e = { };
333          struct aggregator_entry *ptr;          int error = is_delete ? -ENOENT : -ENOMEM;
334          static DEFINE_MUTEX(lock);          if (!ccs_correct_word(original_name) ||
335          const struct path_info *saved_original_name;              !ccs_correct_path(aggregated_name))
         const struct path_info *saved_aggregated_name;  
         int error = -ENOMEM;  
         if (!ccs_is_correct_path(original_name, 1, 0, -1, __func__) ||  
             !ccs_is_correct_path(aggregated_name, 1, -1, -1, __func__))  
336                  return -EINVAL;                  return -EINVAL;
337          saved_original_name = ccs_save_name(original_name);          e.original_name = ccs_get_name(original_name);
338          saved_aggregated_name = ccs_save_name(aggregated_name);          e.aggregated_name = ccs_get_name(aggregated_name);
339          if (!saved_original_name || !saved_aggregated_name)          if (!e.original_name || !e.aggregated_name ||
340                  return -ENOMEM;              e.aggregated_name->is_patterned) /* No patterns allowed. */
341          mutex_lock(&lock);                  goto out;
342          list1_for_each_entry(ptr, &aggregator_list, list) {          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
343                  if (ptr->original_name != saved_original_name ||                                    &ccs_policy_list[CCS_ID_AGGREGATOR],
344                      ptr->aggregated_name != saved_aggregated_name)                                    ccs_same_aggregator);
                         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->aggregated_name = saved_aggregated_name;  
         list1_add_tail_mb(&new_entry->list, &aggregator_list);  
         error = 0;  
345   out:   out:
346          mutex_unlock(&lock);          ccs_put_name(e.original_name);
347          ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);          ccs_put_name(e.aggregated_name);
348          return error;          return error;
349  }  }
350    
351  /**  /**
352   * ccs_read_aggregator_policy - Read "struct aggregator_entry" list.   * ccs_write_aggregator - Write "struct ccs_aggregator" list.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  */  
 bool ccs_read_aggregator_policy(struct ccs_io_buffer *head)  
 {  
         struct list1_head *pos;  
         list1_for_each_cookie(pos, head->read_var2, &aggregator_list) {  
                 struct aggregator_entry *ptr;  
                 ptr = list1_entry(pos, struct aggregator_entry, list);  
                 if (ptr->is_deleted)  
                         continue;  
                 if (!ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n",  
                                    ptr->original_name->name,  
                                    ptr->aggregated_name->name))  
                         goto out;  
         }  
         return true;  
  out:  
         return false;  
 }  
   
 /**  
  * ccs_write_aggregator_policy - Write "struct aggregator_entry" list.  
353   *   *
354   * @data:      String to parse.   * @data:      String to parse.
355   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
356   *   *
357   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
358   */   */
359  int ccs_write_aggregator_policy(char *data, const bool is_delete)  int ccs_write_aggregator(char *data, const bool is_delete)
360  {  {
361          char *cp = strchr(data, ' ');          char *w[2];
362          if (!cp)          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
363                  return -EINVAL;                  return -EINVAL;
364          *cp++ = '\0';          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
         return update_aggregator_entry(data, cp, is_delete);  
365  }  }
366    
367  /* Domain create/delete/undelete handler. */  /* Domain create/delete handler. */
   
 /* #define DEBUG_DOMAIN_UNDELETE */  
368    
369  /**  /**
370   * ccs_delete_domain - Delete a domain.   * ccs_delete_domain - Delete a domain.
# Line 706  int ccs_write_aggregator_policy(char *da Line 375  int ccs_write_aggregator_policy(char *da
375   */   */
376  int ccs_delete_domain(char *domainname)  int ccs_delete_domain(char *domainname)
377  {  {
378          struct domain_info *domain;          struct ccs_domain_info *domain;
379          struct path_info name;          struct ccs_path_info name;
380          name.name = domainname;          name.name = domainname;
381          ccs_fill_path_info(&name);          ccs_fill_path_info(&name);
382          mutex_lock(&new_domain_assign_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
383  #ifdef DEBUG_DOMAIN_UNDELETE                  return 0;
         printk(KERN_DEBUG "ccs_delete_domain %s\n", domainname);  
         list1_for_each_entry(domain, &domain_list, list) {  
                 if (ccs_pathcmp(domain->domainname, &name))  
                         continue;  
                 printk(KERN_DEBUG "List: %p %u\n", domain, domain->is_deleted);  
         }  
 #endif  
384          /* Is there an active domain? */          /* Is there an active domain? */
385          list1_for_each_entry(domain, &domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
386                  struct domain_info *domain2;                  /* Never delete ccs_kernel_domain */
387                  /* Never delete KERNEL_DOMAIN */                  if (domain == &ccs_kernel_domain)
                 if (domain == &KERNEL_DOMAIN)  
388                          continue;                          continue;
389                  if (domain->is_deleted ||                  if (domain->is_deleted ||
390                      ccs_pathcmp(domain->domainname, &name))                      ccs_pathcmp(domain->domainname, &name))
391                          continue;                          continue;
392                  /* Mark already deleted domains as non undeletable. */                  domain->is_deleted = true;
                 list1_for_each_entry(domain2, &domain_list, list) {  
                         if (!domain2->is_deleted ||  
                             ccs_pathcmp(domain2->domainname, &name))  
                                 continue;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                         if (domain2->is_deleted != 255)  
                                 printk(KERN_DEBUG  
                                        "Marked %p as non undeletable\n",  
                                        domain2);  
 #endif  
                         domain2->is_deleted = 255;  
                 }  
                 /* Delete and mark active domain as undeletable. */  
                 domain->is_deleted = 1;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                 printk(KERN_DEBUG "Marked %p as undeletable\n", domain);  
 #endif  
393                  break;                  break;
394          }          }
395          mutex_unlock(&new_domain_assign_lock);          mutex_unlock(&ccs_policy_lock);
396          return 0;          return 0;
397  }  }
398    
399  /**  /**
400   * ccs_undelete_domain - Undelete a domain.   * ccs_assign_domain - Create a domain.
  *  
  * @domainname: The name of domain.  
  *  
  * Returns pointer to "struct domain_info" on success, NULL otherwise.  
  */  
 struct domain_info *ccs_undelete_domain(const char *domainname)  
 {  
         struct domain_info *domain;  
         struct domain_info *candidate_domain = NULL;  
         struct path_info name;  
         name.name = domainname;  
         ccs_fill_path_info(&name);  
         mutex_lock(&new_domain_assign_lock);  
 #ifdef DEBUG_DOMAIN_UNDELETE  
         printk(KERN_DEBUG "ccs_undelete_domain %s\n", domainname);  
         list1_for_each_entry(domain, &domain_list, list) {  
                 if (ccs_pathcmp(domain->domainname, &name))  
                         continue;  
                 printk(KERN_DEBUG "List: %p %u\n", domain, domain->is_deleted);  
         }  
 #endif  
         list1_for_each_entry(domain, &domain_list, list) {  
                 if (ccs_pathcmp(&name, domain->domainname))  
                         continue;  
                 if (!domain->is_deleted) {  
                         /* This domain is active. I can't undelete. */  
                         candidate_domain = NULL;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                         printk(KERN_DEBUG "%p is active. I can't undelete.\n",  
                                domain);  
 #endif  
                         break;  
                 }  
                 /* Is this domain undeletable? */  
                 if (domain->is_deleted == 1)  
                         candidate_domain = domain;  
         }  
         if (candidate_domain) {  
                 candidate_domain->is_deleted = 0;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                 printk(KERN_DEBUG "%p was undeleted.\n", candidate_domain);  
 #endif  
         }  
         mutex_unlock(&new_domain_assign_lock);  
         return candidate_domain;  
 }  
   
 /**  
  * ccs_find_or_assign_new_domain - Create a domain.  
401   *   *
402   * @domainname: The name of domain.   * @domainname: The name of domain.
403   * @profile:    Profile number to assign if the domain was newly created.   * @profile:    Profile number to assign if the domain was newly created.
404     * @group:      Group number to assign if the domain was newly created.
405     * @transit:    True if transit to domain found or created.
406   *   *
407   * Returns pointer to "struct domain_info" on success, NULL otherwise.   * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
408     *
409     * Caller holds ccs_read_lock().
410   */   */
411  struct domain_info *ccs_find_or_assign_new_domain(const char *domainname,  struct ccs_domain_info *ccs_assign_domain(const char *domainname,
412                                                    const u8 profile)                                            const u8 profile, const u8 group,
413                                              const bool transit)
414  {  {
415          struct domain_info *domain = NULL;          struct ccs_domain_info e = { };
416          const struct path_info *saved_domainname;          struct ccs_domain_info *entry = ccs_find_domain(domainname);
417          mutex_lock(&new_domain_assign_lock);          bool created = false;
418          domain = ccs_find_domain(domainname);          if (entry)
         if (domain)  
419                  goto out;                  goto out;
420          if (!ccs_is_correct_domain(domainname, __func__))          if (strlen(domainname) >= CCS_EXEC_TMPSIZE - 10 ||
421                  goto out;              !ccs_correct_domain(domainname))
422          saved_domainname = ccs_save_name(domainname);                  return NULL;
423          if (!saved_domainname)          e.profile = profile;
424            e.group = group;
425            e.domainname = ccs_get_name(domainname);
426            if (!e.domainname)
427                    return NULL;
428            if (mutex_lock_interruptible(&ccs_policy_lock))
429                  goto out;                  goto out;
430          /* Can I reuse memory of deleted domain? */          entry = ccs_find_domain(domainname);
431          list1_for_each_entry(domain, &domain_list, list) {          if (!entry) {
432                  struct task_struct *p;                  entry = ccs_commit_ok(&e, sizeof(e));
433                  struct acl_info *ptr;                  if (entry) {
434                  bool flag;                          INIT_LIST_HEAD(&entry->acl_info_list[0]);
435                  if (!domain->is_deleted ||                          INIT_LIST_HEAD(&entry->acl_info_list[1]);
436                      domain->domainname != saved_domainname)                          list_add_tail_rcu(&entry->list, &ccs_domain_list);
437                          continue;                          created = true;
                 flag = false;  
                 /***** CRITICAL SECTION START *****/  
                 read_lock(&tasklist_lock);  
                 for_each_process(p) {  
                         if (p->domain_info != domain)  
                                 continue;  
                         flag = true;  
                         break;  
438                  }                  }
                 read_unlock(&tasklist_lock);  
                 /***** CRITICAL SECTION END *****/  
                 if (flag)  
                         continue;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                 printk(KERN_DEBUG "Reusing %p %s\n", domain,  
                        domain->domainname->name);  
 #endif  
                 list1_for_each_entry(ptr, &domain->acl_info_list, list) {  
                         ptr->type |= ACL_DELETED;  
                 }  
                 /*  
                  * Don't use ccs_set_domain_flag() because  
                  * new_domain_assign_lock is held.  
                  */  
                 domain->flags = 0;  
                 domain->profile = profile;  
                 domain->quota_warned = false;  
                 mb(); /* Avoid out-of-order execution. */  
                 domain->is_deleted = 0;  
                 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, &domain_list);  
439          }          }
440            mutex_unlock(&ccs_policy_lock);
441   out:   out:
442          mutex_unlock(&new_domain_assign_lock);          ccs_put_name(e.domainname);
443          return domain;          if (entry && transit) {
444  }                  current->ccs_domain_info = entry;
445                    if (created) {
446  /**                          struct ccs_request_info r;
447   * get_argv0 - Get argv[0].                          ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE);
448   *                          r.granted = false;
449   * @bprm: Pointer to "struct linux_binprm".                          ccs_write_log(&r, "use_profile %u\n", r.profile);
  * @tmp:  Buffer for temporal use.  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool get_argv0(struct linux_binprm *bprm, struct ccs_page_buffer *tmp)  
 {  
         char *arg_ptr = tmp->buffer;  
         int arg_len = 0;  
         unsigned long pos = bprm->p;  
         int i = pos / PAGE_SIZE;  
         int offset = pos % PAGE_SIZE;  
         bool done = false;  
         if (!bprm->argc)  
                 goto out;  
         while (1) {  
                 struct page *page;  
                 const char *kaddr;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  
                 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page,  
                                    NULL) <= 0)  
                         goto out;  
                 pos += PAGE_SIZE - offset;  
 #else  
                 page = bprm->page[i];  
 #endif  
                 /* Map. */  
                 kaddr = kmap(page);  
                 if (!kaddr) { /* Mapping failed. */  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  
                         put_page(page);  
 #endif  
                         goto out;  
450                  }                  }
                 /* Read. */  
                 while (offset < PAGE_SIZE) {  
                         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;  
                         }  
                 }  
                 /* Unmap. */  
                 kunmap(page);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  
                 put_page(page);  
 #endif  
                 i++;  
                 offset = 0;  
                 if (done)  
                         break;  
451          }          }
452          return true;          return entry;
  out:  
         return false;  
453  }  }
454    
455  /**  /**
456   * find_next_domain - Find a domain.   * ccs_find_next_domain - Find a domain.
457   *   *
458   * @r:       Pointer to "struct ccs_request_info".   * @ee: Pointer to "struct ccs_execve".
  * @handler: Pathname to verify. May be NULL.  
459   *   *
460   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
461     *
462     * Caller holds ccs_read_lock().
463   */   */
464  static int find_next_domain(struct ccs_request_info *r,  static int ccs_find_next_domain(struct ccs_execve *ee)
                             const struct path_info *handler)  
465  {  {
466          /*          struct ccs_request_info *r = &ee->r;
467           * This function assumes that the size of buffer returned by          const struct ccs_path_info *handler = ee->handler;
468           * ccs_realpath() = CCS_MAX_PATHNAME_LEN.          struct ccs_domain_info *domain = NULL;
469           */          struct ccs_domain_info * const old_domain = ccs_current_domain();
470          struct domain_info *domain = NULL;          struct linux_binprm *bprm = ee->bprm;
471          const char *old_domain_name = r->domain->domainname->name;          struct task_struct *task = current;
472          struct linux_binprm *bprm = r->bprm;          struct ccs_path_info rn = { }; /* real name */
         struct ccs_page_buffer *tmp = r->obj->tmp;  
         const char *original_name = bprm->filename;  
         const u8 mode = r->mode;  
         const bool is_enforce = (mode == 3);  
         const u32 tomoyo_flags = r->tomoyo_flags;  
         char *new_domain_name = NULL;  
         char *real_program_name = NULL;  
         char *symlink_program_name = NULL;  
         struct path_info rn; /* real name */  
         struct path_info sn; /* symlink name */  
         struct path_info ln; /* last name */  
473          int retval;          int retval;
474            bool need_kfree = false;
475          {   retry:
476                  /*          r->matched_acl = NULL;
477                   * Built-in initializers. This is needed because policies are          if (need_kfree) {
478                   * not loaded until starting /sbin/init.                  kfree(rn.name);
479                   */                  need_kfree = false;
                 static bool first = true;  
                 if (first) {  
                         update_domain_initializer_entry(NULL, "/sbin/hotplug",  
                                                         false, false);  
                         update_domain_initializer_entry(NULL, "/sbin/modprobe",  
                                                         false, false);  
                         first = false;  
                 }  
480          }          }
481    
482   retry:          /* Get symlink's pathname of program. */
483          current->tomoyo_flags = tomoyo_flags;          retval = ccs_symlink_path(bprm->filename, &rn);
484          r->tomoyo_flags = tomoyo_flags;          if (retval < 0)
         /* Get ccs_realpath of program. */  
         retval = -ENOENT; /* I hope ccs_realpath() won't fail with -ENOMEM. */  
         ccs_free(real_program_name);  
         real_program_name = ccs_realpath(original_name);  
         if (!real_program_name)  
                 goto out;  
         /* Get ccs_realpath of symbolic link. */  
         ccs_free(symlink_program_name);  
         symlink_program_name = ccs_realpath_nofollow(original_name);  
         if (!symlink_program_name)  
485                  goto out;                  goto out;
486            need_kfree = true;
         rn.name = real_program_name;  
         ccs_fill_path_info(&rn);  
         sn.name = symlink_program_name;  
         ccs_fill_path_info(&sn);  
         ln.name = ccs_get_last_name(r->domain);  
         ccs_fill_path_info(&ln);  
487    
488          if (handler) {          if (handler) {
489                  if (ccs_pathcmp(&rn, handler)) {                  if (ccs_pathcmp(&rn, handler)) {
# Line 1030  static int find_next_domain(struct ccs_r Line 496  static int find_next_domain(struct ccs_r
496                          }                          }
497                          goto out;                          goto out;
498                  }                  }
499                  goto calculate_domain;          } else {
500          }                  struct ccs_aggregator *ptr;
501                    /* Check 'aggregator' directive. */
502          /* Check 'alias' directive. */                  list_for_each_entry_rcu(ptr,
503          if (ccs_pathcmp(&rn, &sn)) {                                          &ccs_policy_list[CCS_ID_AGGREGATOR],
504                  struct alias_entry *ptr;                                          head.list) {
505                  /* Is this program allowed to be called via symbolic links? */                          if (ptr->head.is_deleted ||
506                  list1_for_each_entry(ptr, &alias_list, list) {                              !ccs_path_matches_pattern(&rn, ptr->original_name))
                         if (ptr->is_deleted ||  
                             ccs_pathcmp(&rn, ptr->original_name) ||  
                             ccs_pathcmp(&sn, ptr->aliased_name))  
507                                  continue;                                  continue;
508                          memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN);                          kfree(rn.name);
509                          strncpy(real_program_name, ptr->aliased_name->name,                          need_kfree = false;
510                                  CCS_MAX_PATHNAME_LEN - 1);                          /* This is OK because it is read only. */
511                          ccs_fill_path_info(&rn);                          rn = *ptr->aggregated_name;
512                          break;                          break;
513                  }                  }
         }  
514    
515          /* Compare basename of real_program_name and argv[0] */                  /* Check execute permission. */
516          r->mode = ccs_check_flags(r->domain, CCS_TOMOYO_MAC_FOR_ARGV0);                  retval = ccs_path_permission(r, CCS_TYPE_EXECUTE, &rn);
517          if (bprm->argc > 0 && r->mode) {                  if (retval == CCS_RETRY_REQUEST)
518                  char *base_argv0 = tmp->buffer;                          goto retry;
519                  const char *base_filename;                  if (retval < 0)
                 retval = -ENOMEM;  
                 if (!get_argv0(bprm, tmp))  
520                          goto out;                          goto out;
521                  base_filename = strrchr(real_program_name, '/');                  /*
522                  if (!base_filename)                   * To be able to specify domainnames with wildcards, use the
523                          base_filename = real_program_name;                   * pathname specified in the policy (which may contain
524                  else                   * wildcard) rather than the pathname passed to execve()
525                          base_filename++;                   * (which never contains wildcard).
526                  if (strcmp(base_argv0, base_filename)) {                   */
527                          retval = ccs_check_argv0_perm(r, &rn, base_argv0);                  if (r->param.path.matched_path) {
528                          if (retval == 1) {                          if (need_kfree)
529                                  r->retry++;                                  kfree(rn.name);
530                                  goto retry;                          need_kfree = false;
531                          }                          /* This is OK because it is read only. */
532                          r->retry = 0;                          rn = *r->param.path.matched_path;
                         r->tomoyo_flags = current->tomoyo_flags;  
                         if (retval < 0)  
                                 goto out;  
                 }  
         }  
   
         /* Check 'aggregator' directive. */  
         {  
                 struct aggregator_entry *ptr;  
                 /* Is this program allowed to be aggregated? */  
                 list1_for_each_entry(ptr, &aggregator_list, list) {  
                         if (ptr->is_deleted ||  
                             !ccs_path_matches_pattern(&rn, ptr->original_name))  
                                 continue;  
                         memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN);  
                         strncpy(real_program_name, ptr->aggregated_name->name,  
                                 CCS_MAX_PATHNAME_LEN - 1);  
                         ccs_fill_path_info(&rn);  
                         break;  
533                  }                  }
534          }          }
535    
536          /* Check execute permission. */          /* Calculate domain to transit to. */
537          r->mode = mode;          switch (ccs_transition_type(old_domain->domainname, &rn)) {
538          retval = ccs_check_exec_perm(r, &rn);          case CCS_TRANSITION_CONTROL_INITIALIZE:
539          if (retval == 1) {                  /* Transit to the child of ccs_kernel_domain domain. */
540                  r->retry++;                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, CCS_ROOT_NAME " " "%s",
541                  goto retry;                           rn.name);
542          }                  break;
543          r->retry = 0;          case CCS_TRANSITION_CONTROL_KEEP:
         r->tomoyo_flags = current->tomoyo_flags;  
         if (retval < 0)  
                 goto out;  
   
  calculate_domain:  
         new_domain_name = tmp->buffer;  
         if (is_domain_initializer(r->domain->domainname, &rn, &ln)) {  
                 /* Transit to the child of KERNEL_DOMAIN domain. */  
                 snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1,  
                          ROOT_NAME " " "%s", real_program_name);  
         } else if (r->domain == &KERNEL_DOMAIN && !sbin_init_started) {  
                 /*  
                  * Needn't to transit from kernel domain before starting  
                  * /sbin/init. But transit from kernel domain if executing  
                  * initializers because they might start before /sbin/init.  
                  */  
                 domain = r->domain;  
         } else if (is_domain_keeper(r->domain->domainname, &rn, &ln)) {  
544                  /* Keep current domain. */                  /* Keep current domain. */
545                  domain = r->domain;                  domain = old_domain;
546          } else {                  break;
547                  /* Normal domain transition. */          default:
548                  snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1,                  if (old_domain == &ccs_kernel_domain && !ccs_policy_loaded) {
549                           "%s %s", old_domain_name, real_program_name);                          /*
550          }                           * Needn't to transit from kernel domain before
551          if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN)                           * starting /sbin/init. But transit from kernel domain
552                  goto done;                           * if executing initializers because they might start
553          domain = ccs_find_domain(new_domain_name);                           * before /sbin/init.
554          if (domain)                           */
555                  goto done;                          domain = old_domain;
556          if (is_enforce) {                  } else {
557                  int error = ccs_check_supervisor(r,                          /* Normal domain transition. */
558                                                   "# wants to create domain\n"                          snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
559                                                   "%s\n", new_domain_name);                                   old_domain->domainname->name, rn.name);
                 if (error == 1) {  
                         r->retry++;  
                         goto retry;  
560                  }                  }
561                  r->retry = 0;                  break;
                 if (error < 0)  
                         goto done;  
562          }          }
563          domain = ccs_find_or_assign_new_domain(new_domain_name, r->profile);          /*
564             * Tell GC that I started execve().
565             * Also, tell open_exec() to check read permission.
566             */
567            task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
568            /*
569             * Make task->ccs_flags visible to GC before changing
570             * task->ccs_domain_info .
571             */
572            smp_mb();
573            /*
574             * Proceed to the next domain in order to allow reaching via PID.
575             * It will be reverted if execve() failed. Reverting is not good.
576             * But it is better than being unable to reach via PID in interactive
577             * enforcing mode.
578             */
579            if (!domain)
580                    domain = ccs_assign_domain(ee->tmp, r->profile,
581                                               old_domain->group, true);
582          if (domain)          if (domain)
                 audit_domain_creation_log(domain);  
  done:  
         if (!domain) {  
                 printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",  
                        new_domain_name);  
                 if (is_enforce)  
                         retval = -EPERM;  
                 else {  
                         retval = 0;  
                         ccs_set_domain_flag(r->domain, false,  
                                             DOMAIN_FLAGS_TRANSITION_FAILED);  
                 }  
         } else {  
583                  retval = 0;                  retval = 0;
584            else if (r->mode == CCS_CONFIG_ENFORCING)
585                    retval = -ENOMEM;
586            else {
587                    retval = 0;
588                    if (!old_domain->flags[CCS_DIF_TRANSITION_FAILED]) {
589                            old_domain->flags[CCS_DIF_TRANSITION_FAILED] = true;
590                            r->granted = false;
591                            ccs_write_log(r, CCS_KEYWORD_TRANSITION_FAILED "\n");
592                            printk(KERN_WARNING
593                                   "ERROR: Domain '%s' not defined.\n", ee->tmp);
594                    }
595          }          }
596   out:   out:
597          ccs_free(real_program_name);          if (need_kfree)
598          ccs_free(symlink_program_name);                  kfree(rn.name);
         if (domain)  
                 r->domain = domain;  
599          return retval;          return retval;
600  }  }
601    
602  /**  /**
603   * check_environ - Check permission for environment variable names.   * ccs_environ - Check permission for environment variable names.
604   *   *
605   * @r: Pointer to "struct ccs_request_info".   * @ee: Pointer to "struct ccs_execve".
606   *   *
607   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
608   */   */
609  static int check_environ(struct ccs_request_info *r)  static int ccs_environ(struct ccs_execve *ee)
610  {  {
611          struct linux_binprm *bprm = r->bprm;          struct ccs_request_info *r = &ee->r;
612          struct ccs_page_buffer *tmp = r->obj->tmp;          struct linux_binprm *bprm = ee->bprm;
613          char *arg_ptr = tmp->buffer;          /* env_page->data is allocated by ccs_dump_page(). */
614            struct ccs_page_dump env_page = { };
615            char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
616          int arg_len = 0;          int arg_len = 0;
617          unsigned long pos = bprm->p;          unsigned long pos = bprm->p;
         int i = pos / PAGE_SIZE;  
618          int offset = pos % PAGE_SIZE;          int offset = pos % PAGE_SIZE;
619          int argv_count = bprm->argc;          int argv_count = bprm->argc;
620          int envp_count = bprm->envc;          int envp_count = bprm->envc;
621          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
622          int error = -ENOMEM;          int error = -ENOMEM;
623            ee->r.type = CCS_MAC_ENVIRON;
624            ee->r.mode = ccs_get_mode(ccs_current_domain()->profile,
625                                      CCS_MAC_ENVIRON);
626          if (!r->mode || !envp_count)          if (!r->mode || !envp_count)
627                  return 0;                  return 0;
628            arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
629            if (!arg_ptr)
630                    goto out;
631          while (error == -ENOMEM) {          while (error == -ENOMEM) {
632                  struct page *page;                  if (!ccs_dump_page(bprm, pos, &env_page))
                 const char *kaddr;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  
                 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page,  
                                    NULL) <= 0)  
633                          goto out;                          goto out;
634                  pos += PAGE_SIZE - offset;                  pos += PAGE_SIZE - offset;
 #else  
                 page = bprm->page[i];  
 #endif  
                 /* Map. */  
                 kaddr = kmap(page);  
                 if (!kaddr) { /* Mapping failed. */  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  
                         put_page(page);  
 #endif  
                         goto out;  
                 }  
635                  /* Read. */                  /* Read. */
636                  while (argv_count && offset < PAGE_SIZE) {                  while (argv_count && offset < PAGE_SIZE) {
637                          if (!kaddr[offset++])                          if (!env_page.data[offset++])
638                                  argv_count--;                                  argv_count--;
639                  }                  }
640                  if (argv_count)                  if (argv_count) {
641                          goto unmap_page;                          offset = 0;
642                            continue;
643                    }
644                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
645                          const unsigned char c = kaddr[offset++];                          const unsigned char c = env_page.data[offset++];
646                          if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
647                                  if (c == '=') {                                  if (c == '=') {
648                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
649                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 1237  static int check_environ(struct ccs_requ Line 663  static int check_environ(struct ccs_requ
663                          }                          }
664                          if (c)                          if (c)
665                                  continue;                                  continue;
666                          if (ccs_check_env_perm(r, arg_ptr)) {                          if (ccs_env_perm(r, arg_ptr)) {
667                                  error = -EPERM;                                  error = -EPERM;
668                                  break;                                  break;
669                          }                          }
# Line 1247  static int check_environ(struct ccs_requ Line 673  static int check_environ(struct ccs_requ
673                          }                          }
674                          arg_len = 0;                          arg_len = 0;
675                  }                  }
  unmap_page:  
                 /* Unmap. */  
                 kunmap(page);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  
                 put_page(page);  
 #endif  
                 i++;  
676                  offset = 0;                  offset = 0;
677          }          }
678   out:   out:
679          if (r->mode != 3)          if (r->mode != 3)
680                  error = 0;                  error = 0;
681            kfree(env_page.data);
682            kfree(arg_ptr);
683          return error;          return error;
684  }  }
685    
686  /**  /**
687   * unescape - Unescape escaped string.   * ccs_unescape - Unescape escaped string.
688   *   *
689   * @dest: String to unescape.   * @dest: String to unescape.
690   *   *
691   * Returns nothing.   * Returns nothing.
692   */   */
693  static void unescape(unsigned char *dest)  static void ccs_unescape(unsigned char *dest)
694  {  {
695          unsigned char *src = dest;          unsigned char *src = dest;
696          unsigned char c;          unsigned char c;
697          unsigned char d;          unsigned char d;
698          unsigned char e;          unsigned char e;
699          while ((c = *src++) != '\0') {          while (1) {
700                    c = *src++;
701                    if (!c)
702                            break;
703                  if (c != '\\') {                  if (c != '\\') {
704                          *dest++ = c;                          *dest++ = c;
705                          continue;                          continue;
# Line 1298  static void unescape(unsigned char *dest Line 722  static void unescape(unsigned char *dest
722          *dest = '\0';          *dest = '\0';
723  }  }
724    
725  /**  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
726   * root_depth - Get number of directories to strip.  static int ccs_copy_argv(const char *arg, struct linux_binprm *bprm)
  *  
  * @dentry: Pointer to "struct dentry".  
  * @vfsmnt: Pointer to "struct vfsmount".  
  *  
  * Returns number of directories to strip.  
  */  
 static inline int root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)  
727  {  {
728          int depth = 0;          const int ret = copy_strings_kernel(1, &arg, bprm);
729          /***** CRITICAL SECTION START *****/          if (ret >= 0)
730          ccs_realpath_lock();                  bprm->argc++;
731          for (;;) {          return ret;
                 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {  
                         /* Global root? */  
                         if (vfsmnt->mnt_parent == vfsmnt)  
                                 break;  
                         dentry = vfsmnt->mnt_mountpoint;  
                         vfsmnt = vfsmnt->mnt_parent;  
                         continue;  
                 }  
                 dentry = dentry->d_parent;  
                 depth++;  
         }  
         ccs_realpath_unlock();  
         /***** CRITICAL SECTION END *****/  
         return depth;  
732  }  }
   
 /**  
  * get_root_depth - return the depth of root directory.  
  *  
  * Returns number of directories to strip.  
  */  
 static int get_root_depth(void)  
 {  
         int depth;  
         struct dentry *dentry;  
         struct vfsmount *vfsmnt;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  
         struct path root;  
 #endif  
         /***** CRITICAL SECTION START *****/  
         read_lock(&current->fs->lock);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  
         root = current->fs->root;  
         path_get(&current->fs->root);  
         dentry = root.dentry;  
         vfsmnt = root.mnt;  
 #else  
         dentry = dget(current->fs->root);  
         vfsmnt = mntget(current->fs->rootmnt);  
 #endif  
         read_unlock(&current->fs->lock);  
         /***** CRITICAL SECTION END *****/  
         depth = root_depth(dentry, vfsmnt);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  
         path_put(&root);  
733  #else  #else
734          dput(dentry);  static int ccs_copy_argv(char *arg, struct linux_binprm *bprm)
735          mntput(vfsmnt);  {
736  #endif          const int ret = copy_strings_kernel(1, &arg, bprm);
737          return depth;          if (ret >= 0)
738                    bprm->argc++;
739            return ret;
740  }  }
741    #endif
742    
743  /**  /**
744   * try_alt_exec - Try to start execute handler.   * ccs_try_alt_exec - Try to start execute handler.
745   *   *
746   * @r:           Pointer to "struct ccs_request_info".   * @ee: Pointer to "struct ccs_execve".
  * @handler:     Pointer to the name of execute handler.  
  * @eh_path:     Pointer to pointer to the name of execute handler.  
747   *   *
748   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
749   */   */
750  static int try_alt_exec(struct ccs_request_info *r,  static int ccs_try_alt_exec(struct ccs_execve *ee)
                         const struct path_info *handler, char **eh_path)  
751  {  {
752          /*          /*
753           * Contents of modified bprm.           * Contents of modified bprm.
# Line 1388  static int try_alt_exec(struct ccs_reque Line 761  static int try_alt_exec(struct ccs_reque
761           *    = 0           *    = 0
762           *           *
763           * modified bprm->argv[0]           * modified bprm->argv[0]
764           *    = the program's name specified by execute_handler           *    = the program's name specified by *_execute_handler
765           * modified bprm->argv[1]           * modified bprm->argv[1]
766           *    = current->domain_info->domainname->name           *    = ccs_current_domain()->domainname->name
767           * modified bprm->argv[2]           * modified bprm->argv[2]
768           *    = the current process's name           *    = the current process's name
769           * modified bprm->argv[3]           * modified bprm->argv[3]
# Line 1412  static int try_alt_exec(struct ccs_reque Line 785  static int try_alt_exec(struct ccs_reque
785           * modified bprm->argv[bprm->envc + bprm->argc + 6]           * modified bprm->argv[bprm->envc + bprm->argc + 6]
786           *     = original bprm->envp[bprm->envc - 1]           *     = original bprm->envp[bprm->envc - 1]
787           */           */
788          struct linux_binprm *bprm = r->bprm;          struct linux_binprm *bprm = ee->bprm;
789          struct file *filp;          struct file *filp;
790          int retval;          int retval;
791          const int original_argc = bprm->argc;          const int original_argc = bprm->argc;
792          const int original_envc = bprm->envc;          const int original_envc = bprm->envc;
793          struct task_struct *task = current;          struct task_struct *task = current;
         char *buffer = r->obj->tmp->buffer;  
         /* Allocate memory for execute handler's pathname. */  
         char *execute_handler = ccs_alloc(sizeof(struct ccs_page_buffer));  
         *eh_path = execute_handler;  
         if (!execute_handler)  
                 return -ENOMEM;  
         strncpy(execute_handler, handler->name,  
                 sizeof(struct ccs_page_buffer) - 1);  
         unescape(execute_handler);  
794    
795          /* Close the requested program's dentry. */          /* Close the requested program's dentry. */
796            ee->obj.path1.dentry = NULL;
797            ee->obj.path1.mnt = NULL;
798            ee->obj.validate_done = false;
799          allow_write_access(bprm->file);          allow_write_access(bprm->file);
800          fput(bprm->file);          fput(bprm->file);
801          bprm->file = NULL;          bprm->file = NULL;
802    
803          { /* Adjust root directory for open_exec(). */          /* Invalidate page dump cache. */
804                  int depth = get_root_depth();          ee->dump.page = NULL;
                 char *cp = execute_handler;  
                 if (!*cp || *cp != '/')  
                         return -ENOENT;  
                 while (depth) {  
                         cp = strchr(cp + 1, '/');  
                         if (!cp)  
                                 return -ENOENT;  
                         depth--;  
                 }  
                 memmove(execute_handler, cp, strlen(cp) + 1);  
         }  
805    
806          /* Move envp[] to argv[] */          /* Move envp[] to argv[] */
807          bprm->argc += bprm->envc;          bprm->argc += bprm->envc;
# Line 1453  static int try_alt_exec(struct ccs_reque Line 809  static int try_alt_exec(struct ccs_reque
809    
810          /* Set argv[6] */          /* Set argv[6] */
811          {          {
812                  snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, "%d",                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc);
813                           original_envc);                  retval = ccs_copy_argv(ee->tmp, bprm);
                 retval = copy_strings_kernel(1, &buffer, bprm);  
814                  if (retval < 0)                  if (retval < 0)
815                          goto out;                          goto out;
                 bprm->argc++;  
816          }          }
817    
818          /* Set argv[5] */          /* Set argv[5] */
819          {          {
820                  snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, "%d",                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
821                           original_argc);                  retval = ccs_copy_argv(ee->tmp, bprm);
                 retval = copy_strings_kernel(1, &buffer, bprm);  
822                  if (retval < 0)                  if (retval < 0)
823                          goto out;                          goto out;
                 bprm->argc++;  
824          }          }
825    
826          /* Set argv[4] */          /* Set argv[4] */
827          {          {
828                  retval = copy_strings_kernel(1, &bprm->filename, bprm);                  retval = ccs_copy_argv(bprm->filename, bprm);
829                  if (retval < 0)                  if (retval < 0)
830                          goto out;                          goto out;
                 bprm->argc++;  
831          }          }
832    
833          /* Set argv[3] */          /* Set argv[3] */
834          {          {
835                  const u32 tomoyo_flags = task->tomoyo_flags;                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
                 snprintf(buffer, sizeof(struct ccs_page_buffer) - 1,  
836                           "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "                           "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
837                           "sgid=%d fsuid=%d fsgid=%d state[0]=%u "                           "sgid=%d fsuid=%d fsgid=%d",
838                           "state[1]=%u state[2]=%u",                           (pid_t) ccsecurity_exports.sys_getpid(),
839                           task->pid, task->uid, task->gid, task->euid,                           current_uid(), current_gid(), current_euid(),
840                           task->egid, task->suid, task->sgid, task->fsuid,                           current_egid(), current_suid(), current_sgid(),
841                           task->fsgid, (u8) (tomoyo_flags >> 24),                           current_fsuid(), current_fsgid());
842                           (u8) (tomoyo_flags >> 16), (u8) (tomoyo_flags >> 8));                  retval = ccs_copy_argv(ee->tmp, bprm);
                 retval = copy_strings_kernel(1, &buffer, bprm);  
843                  if (retval < 0)                  if (retval < 0)
844                          goto out;                          goto out;
                 bprm->argc++;  
845          }          }
846    
847          /* Set argv[2] */          /* Set argv[2] */
848          {          {
849                  char *exe = (char *) ccs_get_exe();                  char *exe = (char *) ccs_get_exe();
850                  if (exe) {                  if (exe) {
851                          retval = copy_strings_kernel(1, &exe, bprm);                          retval = ccs_copy_argv(exe, bprm);
852                          ccs_free(exe);                          kfree(exe);
853                  } else {                  } else {
854                          snprintf(buffer, sizeof(struct ccs_page_buffer) - 1,  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
855                                   "<unknown>");                          retval = ccs_copy_argv("<unknown>", bprm);
856                          retval = copy_strings_kernel(1, &buffer, bprm);  #else
857                            snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
858                            retval = ccs_copy_argv(ee->tmp, bprm);
859    #endif
860                  }                  }
861                  if (retval < 0)                  if (retval < 0)
862                          goto out;                          goto out;
                 bprm->argc++;  
863          }          }
864    
865          /* Set argv[1] */          /* Set argv[1] */
866          {          {
867                  strncpy(buffer, task->domain_info->domainname->name,  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
868                          sizeof(struct ccs_page_buffer) - 1);                  retval = ccs_copy_argv(ccs_current_domain()->domainname->name,
869                  retval = copy_strings_kernel(1, &buffer, bprm);                                         bprm);
870    #else
871                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
872                             ccs_current_domain()->domainname->name);
873                    retval = ccs_copy_argv(ee->tmp, bprm);
874    #endif
875                  if (retval < 0)                  if (retval < 0)
876                          goto out;                          goto out;
                 bprm->argc++;  
877          }          }
878    
879          /* Set argv[0] */          /* Set argv[0] */
880          {          {
881                  retval = copy_strings_kernel(1, &execute_handler, bprm);                  struct path root;
882                    char *cp;
883                    int root_len;
884                    int handler_len;
885                    get_fs_root(current->fs, &root);
886                    cp = ccs_realpath_from_path(&root);
887                    path_put(&root);
888                    if (!cp) {
889                            retval = -ENOMEM;
890                            goto out;
891                    }
892                    root_len = strlen(cp);
893                    retval = strncmp(ee->handler->name, cp, root_len);
894                    root_len--;
895                    kfree(cp);
896                    if (retval) {
897                            retval = -ENOENT;
898                            goto out;
899                    }
900                    handler_len = ee->handler->total_len + 1;
901                    cp = kmalloc(handler_len, CCS_GFP_FLAGS);
902                    if (!cp) {
903                            retval = -ENOMEM;
904                            goto out;
905                    }
906                    ee->handler_path = cp;
907                    /* Adjust root directory for open_exec(). */
908                    memmove(cp, ee->handler->name + root_len,
909                            handler_len - root_len);
910                    ccs_unescape(cp);
911                    retval = -ENOENT;
912                    if (!*cp || *cp != '/')
913                            goto out;
914                    retval = ccs_copy_argv(cp, bprm);
915                  if (retval < 0)                  if (retval < 0)
916                          goto out;                          goto out;
                 bprm->argc++;  
917          }          }
918  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
919  #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)  #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
# Line 1535  static int try_alt_exec(struct ccs_reque Line 921  static int try_alt_exec(struct ccs_reque
921  #endif  #endif
922  #endif  #endif
923    
924          /* OK, now restart the process with execute handler program's dentry. */          /*
925          filp = open_exec(execute_handler);           * OK, now restart the process with execute handler program's dentry.
926             */
927            filp = open_exec(ee->handler_path);
928          if (IS_ERR(filp)) {          if (IS_ERR(filp)) {
929                  retval = PTR_ERR(filp);                  retval = PTR_ERR(filp);
930                  goto out;                  goto out;
931          }          }
932            ee->obj.path1.dentry = filp->f_dentry;
933            ee->obj.path1.mnt = filp->f_vfsmnt;
934          bprm->file = filp;          bprm->file = filp;
935          bprm->filename = execute_handler;          bprm->filename = ee->handler_path;
936  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
937          bprm->interp = execute_handler;          bprm->interp = bprm->filename;
938  #endif  #endif
939          retval = prepare_binprm(bprm);          retval = prepare_binprm(bprm);
940          if (retval < 0)          if (retval < 0)
941                  goto out;                  goto out;
942          task->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
943          retval = find_next_domain(r, handler);          retval = ccs_find_next_domain(ee);
944          task->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
945   out:   out:
946          return retval;          return retval;
947  }  }
948    
949  /**  /**
950   * find_execute_handler - Find an execute handler.   * ccs_find_execute_handler - Find an execute handler.
951   *   *
952     * @ee:   Pointer to "struct ccs_execve".
953   * @type: Type of execute handler.   * @type: Type of execute handler.
954   *   *
955   * Returns pointer to "struct path_info" if found, NULL otherwise.   * Returns true if found, false otherwise.
956     *
957     * Caller holds ccs_read_lock().
958   */   */
959  static const struct path_info *find_execute_handler(const u8 type)  static bool ccs_find_execute_handler(struct ccs_execve *ee, const u8 type)
960  {  {
961          struct task_struct *task = current;          struct ccs_request_info *r = &ee->r;
         const struct domain_info *domain = task->domain_info;  
         struct acl_info *ptr;  
962          /*          /*
963           * Don't use execute handler if the current process is           * To avoid infinite execute handler loop, don't use execute handler
964           * marked as execute handler to avoid infinite execute handler loop.           * if the current process is marked as execute handler .
965           */           */
966          if (task->tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER)          if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
967                  return NULL;                  return false;
968          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          r->param_type = type;
969                  struct execute_handler_record *acl;          ccs_check_acl(r, NULL);
970                  if (ptr->type != type)          if (!r->granted)
971                          continue;                  return false;
972                  acl = container_of(ptr, struct execute_handler_record, head);          ee->handler = container_of(r->matched_acl, struct ccs_handler_acl,
973                  return acl->handler;                                     head)->handler;
974          }          return true;
         return NULL;  
975  }  }
976    
977  /* List of next_domain which is used for checking interpreter's permissions. */  #ifdef CONFIG_MMU
978  struct execve_entry {  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
979          struct list_head list;  #define CCS_BPRM_MMU
980          struct task_struct *task;  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3
981          struct domain_info *next_domain;  #define CCS_BPRM_MMU
982  };  #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2
983    #define CCS_BPRM_MMU
984  static LIST_HEAD(execve_list);  #endif
985  static DEFINE_SPINLOCK(execve_list_lock);  #endif
986    
987  /**  /**
988   * ccs_register_next_domain - Remember next_domain.   * ccs_dump_page - Dump a page to buffer.
989   *   *
990   * @next_domain: Pointer to "struct domain_info".   * @bprm: Pointer to "struct linux_binprm".
991     * @pos:  Location to dump.
992     * @dump: Poiner to "struct ccs_page_dump".
993   *   *
994   * Returns 0 on success, -ENOMEM otherwise.   * Returns true on success, false otherwise.
995   */   */
996  static int ccs_register_next_domain(struct domain_info *next_domain)  bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
997                       struct ccs_page_dump *dump)
998  {  {
999          struct execve_entry *ee = kmalloc(sizeof(*ee), GFP_KERNEL);          struct page *page;
1000          if (!ee)          /* dump->data is released by ccs_finish_execve(). */
1001                  return -ENOMEM;          if (!dump->data) {
1002          ee->task = current;                  dump->data = kzalloc(PAGE_SIZE, CCS_GFP_FLAGS);
1003          ee->next_domain = next_domain;                  if (!dump->data)
1004          /***** CRITICAL SECTION START *****/                          return false;
1005          spin_lock(&execve_list_lock);          }
1006          list_add(&ee->list, &execve_list);          /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
1007          spin_unlock(&execve_list_lock);  #ifdef CCS_BPRM_MMU
1008          /***** CRITICAL SECTION END *****/          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1009          return 0;                  return false;
1010    #else
1011            page = bprm->page[pos / PAGE_SIZE];
1012    #endif
1013            if (page != dump->page) {
1014                    const unsigned int offset = pos % PAGE_SIZE;
1015                    /*
1016                     * Maybe kmap()/kunmap() should be used here.
1017                     * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
1018                     * So do I.
1019                     */
1020                    char *kaddr = kmap_atomic(page, KM_USER0);
1021                    dump->page = page;
1022                    memcpy(dump->data + offset, kaddr + offset,
1023                           PAGE_SIZE - offset);
1024                    kunmap_atomic(kaddr, KM_USER0);
1025            }
1026            /* Same with put_arg_page(page) in fs/exec.c */
1027    #ifdef CCS_BPRM_MMU
1028            put_page(page);
1029    #endif
1030            return true;
1031  }  }
1032    
1033  /**  /**
1034   * ccs_fetch_next_domain - Fetch next_domain from the list.   * ccs_start_execve - Prepare for execve() operation.
1035   *   *
1036   * Returns pointer to "struct domain_info" which will be used if execve()   * @bprm: Pointer to "struct linux_binprm".
1037   * succeeds. This function does not return NULL.   * @eep:  Pointer to "struct ccs_execve *".
1038     *
1039     * Returns 0 on success, negative value otherwise.
1040   */   */
1041  struct domain_info *ccs_fetch_next_domain(void)  static int ccs_start_execve(struct linux_binprm *bprm,
1042                                struct ccs_execve **eep)
1043  {  {
1044            int retval;
1045          struct task_struct *task = current;          struct task_struct *task = current;
1046          struct domain_info *next_domain = task->domain_info;          struct ccs_execve *ee;
1047          struct execve_entry *p;          *eep = NULL;
1048          /***** CRITICAL SECTION START *****/          ee = kzalloc(sizeof(*ee), CCS_GFP_FLAGS);
1049          spin_lock(&execve_list_lock);          if (!ee)
1050          list_for_each_entry(p, &execve_list, list) {                  return -ENOMEM;
1051                  if (p->task != task)          ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
1052                          continue;          if (!ee->tmp) {
1053                  next_domain = p->next_domain;                  kfree(ee);
1054                  break;                  return -ENOMEM;
1055          }          }
1056          spin_unlock(&execve_list_lock);          ee->reader_idx = ccs_read_lock();
1057          /***** CRITICAL SECTION END *****/          /* ee->dump->data is allocated by ccs_dump_page(). */
1058          return next_domain;          ee->previous_domain = task->ccs_domain_info;
1059            /* Clear manager flag. */
1060            task->ccs_flags &= ~CCS_TASK_IS_MANAGER;
1061            *eep = ee;
1062            ccs_init_request_info(&ee->r, CCS_MAC_FILE_EXECUTE);
1063            ee->r.ee = ee;
1064            ee->bprm = bprm;
1065            ee->r.obj = &ee->obj;
1066            ee->obj.path1.dentry = bprm->file->f_dentry;
1067            ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1068            /*
1069             * No need to call ccs_environ() for execute handler because envp[] is
1070             * moved to argv[].
1071             */
1072            if (ccs_find_execute_handler(ee, CCS_TYPE_AUTO_EXECUTE_HANDLER))
1073                    return ccs_try_alt_exec(ee);
1074            retval = ccs_find_next_domain(ee);
1075            if (retval == -EPERM) {
1076                    if (ccs_find_execute_handler(ee,
1077                                                 CCS_TYPE_DENIED_EXECUTE_HANDLER))
1078                            return ccs_try_alt_exec(ee);
1079            }
1080            if (!retval)
1081                    retval = ccs_environ(ee);
1082            return retval;
1083  }  }
1084    
1085  /**  /**
1086   * ccs_unregister_next_domain - Forget next_domain.   * ccs_finish_execve - Clean up execve() operation.
1087     *
1088     * @retval: Return code of an execve() operation.
1089     * @ee:     Pointer to "struct ccs_execve".
1090     *
1091     * Caller holds ccs_read_lock().
1092   */   */
1093  static void ccs_unregister_next_domain(void)  static void ccs_finish_execve(int retval, struct ccs_execve *ee)
1094  {  {
1095          struct task_struct *task = current;          struct task_struct *task = current;
1096          struct execve_entry *p;          if (!ee)
1097          struct execve_entry *ee = NULL;                  return;
1098          /***** CRITICAL SECTION START *****/          if (retval < 0) {
1099          spin_lock(&execve_list_lock);                  task->ccs_domain_info = ee->previous_domain;
1100          list_for_each_entry(p, &execve_list, list) {                  /*
1101                  if (p->task != task)                   * Make task->ccs_domain_info visible to GC before changing
1102                          continue;                   * task->ccs_flags .
1103                  list_del(&p->list);                   */
1104                  ee = p;                  smp_mb();
1105                  break;          } else {
1106                    /* Mark the current process as execute handler. */
1107                    if (ee->handler)
1108                            task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1109                    /* Mark the current process as normal process. */
1110                    else
1111                            task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1112          }          }
1113          spin_unlock(&execve_list_lock);          /* Tell GC that I finished execve(). */
1114          /***** CRITICAL SECTION END *****/          task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1115            ccs_read_unlock(ee->reader_idx);
1116            kfree(ee->handler_path);
1117            kfree(ee->tmp);
1118            kfree(ee->dump.data);
1119          kfree(ee);          kfree(ee);
1120  }  }
1121    
1122  /**  static int __ccs_search_binary_handler(struct linux_binprm *bprm,
1123   * search_binary_handler_with_transition - Perform domain transition.                                         struct pt_regs *regs)
  *  
  * @bprm: Pointer to "struct linux_binprm".  
  * @regs: Pointer to "struct pt_regs".  
  *  
  * Returns result of search_binary_handler() on success,  
  * negative value otherwise.  
  */  
 int search_binary_handler_with_transition(struct linux_binprm *bprm,  
                                           struct pt_regs *regs)  
1124  {  {
1125            struct ccs_execve *ee;
1126          int retval;          int retval;
1127          struct task_struct *task = current;          if (!ccs_policy_loaded)
1128          const struct path_info *handler;                  ccsecurity_exports.load_policy(bprm->filename);
1129          struct ccs_request_info r;          retval = ccs_start_execve(bprm, &ee);
1130          struct obj_info obj;          if (!retval)
1131          /*                  retval = search_binary_handler(bprm, regs);
1132           * "eh_path" holds path to execute handler program.          ccs_finish_execve(retval, ee);
          * Thus, keep valid until search_binary_handler() finishes.  
          */  
         char *eh_path = NULL;  
         struct ccs_page_buffer *tmp = ccs_alloc(sizeof(struct ccs_page_buffer));  
         memset(&obj, 0, sizeof(obj));  
         if (!sbin_init_started)  
                 ccs_load_policy(bprm->filename);  
         if (!tmp)  
                 return -ENOMEM;  
   
         ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_FILE);  
         r.bprm = bprm;  
         r.obj = &obj;  
         obj.path1_dentry = bprm->file->f_dentry;  
         obj.path1_vfsmnt = bprm->file->f_vfsmnt;  
         obj.tmp = tmp;  
   
         /* Clear manager flag. */  
         task->tomoyo_flags &= ~CCS_TASK_IS_POLICY_MANAGER;  
         handler = find_execute_handler(TYPE_EXECUTE_HANDLER);  
         if (handler) {  
                 retval = try_alt_exec(&r, handler, &eh_path);  
                 if (!retval)  
                         audit_execute_handler_log(true, handler->name, bprm);  
                 goto ok;  
         }  
         retval = find_next_domain(&r, NULL);  
         if (retval != -EPERM)  
                 goto ok;  
         handler = find_execute_handler(TYPE_DENIED_EXECUTE_HANDLER);  
         if (handler) {  
                 retval = try_alt_exec(&r, handler, &eh_path);  
                 if (!retval)  
                         audit_execute_handler_log(false, handler->name, bprm);  
         }  
  ok:  
         if (retval < 0)  
                 goto out;  
         r.mode = ccs_check_flags(r.domain, CCS_TOMOYO_MAC_FOR_ENV);  
         retval = check_environ(&r);  
         if (retval < 0)  
                 goto out;  
         retval = ccs_register_next_domain(r.domain);  
         if (retval < 0)  
                 goto out;  
         task->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC;  
         retval = search_binary_handler(bprm, regs);  
         task->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC;  
         if (retval < 0)  
                 goto out;  
         /* Proceed to next domain if execution suceeded. */  
         task->domain_info = r.domain;  
         mb(); /* Make domain transition visible to other CPUs. */  
         /* Mark the current process as execute handler. */  
         if (handler)  
                 task->tomoyo_flags |= TOMOYO_TASK_IS_EXECUTE_HANDLER;  
         /* Mark the current process as normal process. */  
         else  
                 task->tomoyo_flags &= ~TOMOYO_TASK_IS_EXECUTE_HANDLER;  
  out:  
         ccs_unregister_next_domain();  
         ccs_free(eh_path);  
         ccs_free(tmp);  
1133          return retval;          return retval;
1134  }  }
1135    
1136  #else  void __init ccs_domain_init(void)
   
 /**  
  * search_binary_handler_with_transition - Wrapper for search_binary_handler().  
  *  
  * @bprm: Pointer to "struct linux_binprm".  
  * @regs: Pointer to "struct pt_regs".  
  *  
  * Returns the result of search_binary_handler().  
  */  
 int search_binary_handler_with_transition(struct linux_binprm *bprm,  
                                           struct pt_regs *regs)  
1137  {  {
1138  #ifdef CONFIG_SAKURA          ccsecurity_ops.search_binary_handler = __ccs_search_binary_handler;
         /* Clear manager flag. */  
         current->tomoyo_flags &= ~CCS_TASK_IS_POLICY_MANAGER;  
         ccs_load_policy(bprm->filename);  
 #endif  
         return search_binary_handler(bprm, regs);  
1139  }  }
   
 #endif  

Legend:
Removed from v.1687  
changed lines
  Added in v.3949

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