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

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.5.x/ccs-patch/fs/tomoyo_domain.c revision 851 by kumaneko, Wed Jan 2 06:29:56 2008 UTC branches/ccs-patch/security/ccsecurity/domain.c revision 3731 by kumaneko, Fri Jun 4 01:42:54 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.7.2+   2010/06/04
  *  
  * Version: 1.5.3-pre   2008/01/02  
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   */   */
 /***** TOMOYO Linux start. *****/  
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)
17  #ifndef for_each_process  #include <linux/namei.h>
18  #define for_each_process for_each_task  #include <linux/mount.h>
19  #endif  #endif
20    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
21    #include <linux/fs_struct.h>
22    #endif
23    #include "internal.h"
24    
25  /*************************  VARIABLES  *************************/  /* Variables definitions.*/
   
 /* The initial domain. */  
 struct domain_info KERNEL_DOMAIN;  
   
 /* List of domains. */  
 LIST1_HEAD(domain_list);  
   
 /* /sbin/init started? */  
 extern int sbin_init_started;  
   
 #ifdef CONFIG_TOMOYO  
   
 /* Lock for appending domain's ACL. */  
 DEFINE_MUTEX(domain_acl_lock);  
   
 /*************************  UTILITY FUNCTIONS  *************************/  
   
 /***** The structure for program files to force domain reconstruction. *****/  
   
 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;  
         bool is_last_name;  
 };  
   
 /***** The structure for domains to not to transit domains. *****/  
   
 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;  
         bool is_last_name;  
 };  
   
 /***** The structure for program files that should be aggregated. *****/  
   
 struct aggregator_entry {  
         struct list1_head list;  
         const struct path_info *original_name;  
         const struct path_info *aggregated_name;  
         bool is_deleted;  
 };  
   
 /***** The structure for program files that should be aliased. *****/  
26    
27  struct alias_entry {  /* The global domain. */
28          struct list1_head list;  struct ccs_domain_info ccs_global_domain;
         const struct path_info *original_name;  
         const struct path_info *aliased_name;  
         bool is_deleted;  
 };  
29    
30  /*************************  VARIABLES  *************************/  /* The initial domain. */
31    struct ccs_domain_info ccs_kernel_domain;
32    
33  /* Domain creation lock. */  /* The list for "struct ccs_domain_info". */
34  static DEFINE_MUTEX(new_domain_assign_lock);  LIST_HEAD(ccs_domain_list);
35    
36  /*************************  UTILITY FUNCTIONS  *************************/  struct list_head ccs_policy_list[CCS_MAX_POLICY];
37    struct list_head ccs_group_list[CCS_MAX_GROUP];
38    struct list_head ccs_shared_list[CCS_MAX_LIST];
39    
40  const char *GetLastName(const struct domain_info *domain)  /**
41     * ccs_audit_execute_handler_log - Audit execute_handler log.
42     *
43     * @ee:         Pointer to "struct ccs_execve".
44     *
45     * Returns 0 on success, negative value otherwise.
46     */
47    static int ccs_audit_execute_handler_log(struct ccs_execve *ee)
48  {  {
49          const char *cp0 = domain->domainname->name, *cp1;          struct ccs_request_info *r = &ee->r;
50          if ((cp1 = strrchr(cp0, ' ')) != NULL) return cp1 + 1;          const char *handler = ee->handler->name;
51          return cp0;          r->type = CCS_MAC_FILE_EXECUTE;
52            r->mode = ccs_get_mode(r->profile, CCS_MAC_FILE_EXECUTE);
53            r->granted = true;
54            return ccs_write_log(r, "%s %s\n", ee->handler_type ==
55                                 CCS_TYPE_DENIED_EXECUTE_HANDLER ?
56                                 CCS_KEYWORD_DENIED_EXECUTE_HANDLER :
57                                 CCS_KEYWORD_EXECUTE_HANDLER, handler);
58  }  }
59    
60  int AddDomainACL(struct domain_info *domain, struct acl_info *acl)  /**
61     * ccs_audit_domain_creation_log - Audit domain creation log.
62     *
63     * Returns 0 on success, negative value otherwise.
64     */
65    static int ccs_audit_domain_creation_log(void)
66  {  {
67          list1_add_tail_mb(&acl->list, &domain->acl_info_list);          struct ccs_request_info r;
68          UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);          ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE);
69          return 0;          r.granted = false;
70            return ccs_write_log(&r, "use_profile %u\n", r.profile);
71    }
72    
73    int ccs_update_policy(struct ccs_acl_head *new_entry, const int size,
74                          bool is_delete, const int idx, bool (*check_duplicate)
75                          (const struct ccs_acl_head *,
76                           const struct ccs_acl_head *))
77    {
78            int error = is_delete ? -ENOENT : -ENOMEM;
79            struct ccs_acl_head *entry;
80            if (mutex_lock_interruptible(&ccs_policy_lock))
81                    return -ENOMEM;
82            list_for_each_entry_rcu(entry, &ccs_policy_list[idx], list) {
83                    if (!check_duplicate(entry, new_entry))
84                            continue;
85                    entry->is_deleted = is_delete;
86                    error = 0;
87                    break;
88            }
89            if (error && !is_delete) {
90                    entry = ccs_commit_ok(new_entry, size);
91                    if (entry) {
92                            list_add_tail_rcu(&entry->list, &ccs_policy_list[idx]);
93                            error = 0;
94                    }
95            }
96            mutex_unlock(&ccs_policy_lock);
97            return error;
98    }
99    
100    int ccs_update_group(struct ccs_acl_head *new_entry, const int size,
101                         bool is_delete, struct ccs_group *group,
102                         bool (*check_duplicate) (const struct ccs_acl_head *,
103                                                  const struct ccs_acl_head *))
104    {
105            int error = is_delete ? -ENOENT : -ENOMEM;
106            struct ccs_acl_head *entry;
107            if (mutex_lock_interruptible(&ccs_policy_lock))
108                    return -ENOMEM;
109            list_for_each_entry_rcu(entry, &group->member_list, list) {
110                    if (!check_duplicate(entry, new_entry))
111                            continue;
112                    entry->is_deleted = is_delete;
113                    error = 0;
114                    break;
115            }
116            if (!is_delete && error) {
117                    entry = ccs_commit_ok(new_entry, size);
118                    if (entry) {
119                            list_add_tail_rcu(&entry->list, &group->member_list);
120                            error = 0;
121                    }
122            }
123            mutex_unlock(&ccs_policy_lock);
124            return error;
125  }  }
126    
127  int DelDomainACL(struct acl_info *ptr)  static void ccs_delete_type(struct ccs_domain_info *domain, u8 type)
128  {  {
129          ptr->is_deleted = 1;          struct ccs_acl_info *ptr;
130          UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
131          return 0;                  if (ptr->type == type)
132                            ptr->is_deleted = true;
133            }
134  }  }
135    
136  /*************************  DOMAIN INITIALIZER HANDLER  *************************/  int ccs_update_domain(struct ccs_acl_info *new_entry, const int size,
137                          bool is_delete, struct ccs_domain_info *domain,
138                          bool (*check_duplicate) (const struct ccs_acl_info *,
139                                                   const struct ccs_acl_info *),
140                          bool (*merge_duplicate) (struct ccs_acl_info *,
141                                                   struct ccs_acl_info *,
142                                                   const bool))
143    {
144            int error = is_delete ? -ENOENT : -ENOMEM;
145            struct ccs_acl_info *entry;
146            /*
147             * Only one "execute_handler" and "denied_execute_handler" can exist
148             * in a domain.
149             */
150            const u8 type = new_entry->type;
151            const bool exclusive = !is_delete &&
152                    (type == CCS_TYPE_EXECUTE_HANDLER ||
153                     type == CCS_TYPE_DENIED_EXECUTE_HANDLER);
154            if (mutex_lock_interruptible(&ccs_policy_lock))
155                    return error;
156            list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
157                    if (!check_duplicate(entry, new_entry))
158                            continue;
159                    if (exclusive)
160                            ccs_delete_type(domain, type);
161                    if (merge_duplicate)
162                            entry->is_deleted = merge_duplicate(entry, new_entry,
163                                                                is_delete);
164                    else
165                            entry->is_deleted = is_delete;
166                    error = 0;
167                    break;
168            }
169            if (error && !is_delete) {
170                    entry = ccs_commit_ok(new_entry, size);
171                    if (entry) {
172                            if (exclusive)
173                                    ccs_delete_type(domain, type);
174                            ccs_add_domain_acl(domain, entry);
175                            error = 0;
176                    }
177            }
178            mutex_unlock(&ccs_policy_lock);
179            return error;
180    }
181    
182  static LIST1_HEAD(domain_initializer_list);  void ccs_check_acl(struct ccs_request_info *r,
183                       bool (*check_entry) (const struct ccs_request_info *,
184                                            const struct ccs_acl_info *))
185    {
186            const struct ccs_domain_info *domain = ccs_current_domain();
187            struct ccs_acl_info *ptr;
188     retry:
189            list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
190                    if (ptr->is_deleted || ptr->type != r->param_type)
191                            continue;
192                    if (check_entry(r, ptr) && ccs_condition(r, ptr->cond)) {
193                            r->cond = ptr->cond;
194                            r->granted = true;
195                            return;
196                    }
197            }
198            if (domain != &ccs_global_domain && !domain->ignore_global &&
199                (r->param_type != CCS_TYPE_PATH_ACL ||
200                 r->param.path.operation != CCS_TYPE_READ ||
201                 !domain->ignore_global_allow_read) &&
202                (r->param_type != CCS_TYPE_ENV_ACL ||
203                 !domain->ignore_global_allow_env)) {
204                    domain = &ccs_global_domain;
205                    goto retry;
206            }
207            r->granted = false;
208    }
209    
210    static bool ccs_same_domain_initializer_entry(const struct ccs_acl_head *a,
211                                                  const struct ccs_acl_head *b)
212    {
213            const struct ccs_domain_initializer *p1 = container_of(a, typeof(*p1),
214                                                                   head);
215            const struct ccs_domain_initializer *p2 = container_of(b, typeof(*p2),
216                                                                   head);
217            return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
218                    && p1->domainname == p2->domainname
219                    && p1->program == p2->program;
220    }
221    
222  static int AddDomainInitializerEntry(const char *domainname, const char *program, const bool is_not, const bool is_delete)  /**
223  {   * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer" list.
224          struct domain_initializer_entry *new_entry, *ptr;   *
225          static DEFINE_MUTEX(lock);   * @domainname: The name of domain. May be NULL.
226          const struct path_info *saved_program, *saved_domainname = NULL;   * @program:    The name of program.
227          int error = -ENOMEM;   * @is_not:     True if it is "no_initialize_domain" entry.
228          bool is_last_name = 0;   * @is_delete:  True if it is a delete request.
229          if (!IsCorrectPath(program, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */   *
230     * Returns 0 on success, negative value otherwise.
231     */
232    static int ccs_update_domain_initializer_entry(const char *domainname,
233                                                   const char *program,
234                                                   const bool is_not,
235                                                   const bool is_delete)
236    {
237            struct ccs_domain_initializer e = { .is_not = is_not };
238            int error = is_delete ? -ENOENT : -ENOMEM;
239            if (!ccs_correct_path(program))
240                    return -EINVAL;
241          if (domainname) {          if (domainname) {
242                  if (!IsDomainDef(domainname) && IsCorrectPath(domainname, 1, -1, -1, __FUNCTION__)) {                  if (!ccs_domain_def(domainname) &&
243                          is_last_name = 1;                      ccs_correct_path(domainname))
244                  } else if (!IsCorrectDomain(domainname, __FUNCTION__)) {                          e.is_last_name = true;
245                    else if (!ccs_correct_domain(domainname))
246                          return -EINVAL;                          return -EINVAL;
247                  }                  e.domainname = ccs_get_name(domainname);
248                  if ((saved_domainname = SaveName(domainname)) == NULL) return -ENOMEM;                  if (!e.domainname)
         }  
         if ((saved_program = SaveName(program)) == NULL) return -ENOMEM;  
         mutex_lock(&lock);  
         list1_for_each_entry(ptr, &domain_initializer_list, list) {  
                 if (ptr->is_not == is_not && ptr->domainname == saved_domainname && ptr->program == saved_program) {  
                         ptr->is_deleted = is_delete;  
                         error = 0;  
249                          goto out;                          goto out;
                 }  
250          }          }
251          if (is_delete) {          e.program = ccs_get_name(program);
252                  error = -ENOENT;          if (!e.program)
253                  goto out;                  goto out;
254          }          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
255          if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;                                    CCS_ID_DOMAIN_INITIALIZER,
256          new_entry->domainname = saved_domainname;                                    ccs_same_domain_initializer_entry);
         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;  
257   out:   out:
258          mutex_unlock(&lock);          ccs_put_name(e.domainname);
259            ccs_put_name(e.program);
260          return error;          return error;
261  }  }
262    
263  int ReadDomainInitializerPolicy(struct io_buffer *head)  /**
264  {   * ccs_write_domain_initializer - Write "struct ccs_domain_initializer" list.
265          struct list1_head *pos;   *
266          list1_for_each_cookie(pos, head->read_var2, &domain_initializer_list) {   * @data:      String to parse.
267                  struct domain_initializer_entry *ptr;   * @is_delete: True if it is a delete request.
268                  ptr = list1_entry(pos, struct domain_initializer_entry, list);   *
269                  if (ptr->is_deleted) continue;   * Returns 0 on success, negative value otherwise.
270                  if (ptr->domainname) {   */
271                          if (io_printf(head, "%s" KEYWORD_INITIALIZE_DOMAIN "%s from %s\n", ptr->is_not ? "no_" : "", ptr->program->name, ptr->domainname->name)) return -ENOMEM;  int ccs_write_domain_initializer(char *data, const bool is_delete, const u8 flags)
                 } else {  
                         if (io_printf(head, "%s" KEYWORD_INITIALIZE_DOMAIN "%s\n", ptr->is_not ? "no_" : "", ptr->program->name)) return -ENOMEM;  
                 }  
         }  
         return 0;  
 }  
   
 int AddDomainInitializerPolicy(char *data, const bool is_not, const bool is_delete)  
272  {  {
273          char *cp = strstr(data, " from ");          char *domainname = strstr(data, " from ");
274          if (cp) {          if (domainname) {
275                  *cp = '\0';                  *domainname = '\0';
276                  return AddDomainInitializerEntry(cp + 6, data, is_not, is_delete);                  domainname += 6;
         } else {  
                 return AddDomainInitializerEntry(NULL, data, is_not, is_delete);  
277          }          }
278            return ccs_update_domain_initializer_entry(domainname, data, flags,
279                                                       is_delete);
280  }  }
281    
282  static bool IsDomainInitializer(const struct path_info *domainname, const struct path_info *program, const struct path_info *last_name)  /**
283  {   * ccs_domain_initializer - Check whether the given program causes domainname reinitialization.
284          struct domain_initializer_entry *ptr;   *
285          bool flag = 0;   * @domainname: The name of domain.
286          list1_for_each_entry(ptr,  &domain_initializer_list, list) {   * @program:    The name of program.
287                  if (ptr->is_deleted) continue;   * @last_name:  The last component of @domainname.
288     *
289     * Returns true if executing @program reinitializes domain transition,
290     * false otherwise.
291     *
292     * Caller holds ccs_read_lock().
293     */
294    static bool ccs_domain_initializer(const struct ccs_path_info *domainname,
295                                          const struct ccs_path_info *program,
296                                          const struct ccs_path_info *last_name)
297    {
298            struct ccs_domain_initializer *ptr;
299            bool flag = false;
300            list_for_each_entry_rcu(ptr, &ccs_policy_list
301                                    [CCS_ID_DOMAIN_INITIALIZER], head.list) {
302                    if (ptr->head.is_deleted)
303                            continue;
304                  if (ptr->domainname) {                  if (ptr->domainname) {
305                          if (!ptr->is_last_name) {                          if (!ptr->is_last_name) {
306                                  if (ptr->domainname != domainname) continue;                                  if (ptr->domainname != domainname)
307                                            continue;
308                          } else {                          } else {
309                                  if (pathcmp(ptr->domainname, last_name)) continue;                                  if (ccs_pathcmp(ptr->domainname, last_name))
310                                            continue;
311                          }                          }
312                  }                  }
313                  if (pathcmp(ptr->program, program)) continue;                  if (ccs_pathcmp(ptr->program, program))
314                  if (ptr->is_not) return 0;                          continue;
315                  flag = 1;                  if (ptr->is_not) {
316                            flag = false;
317                            break;
318                    }
319                    flag = true;
320          }          }
321          return flag;          return flag;
322  }  }
323    
324  /*************************  DOMAIN KEEPER HANDLER  *************************/  static bool ccs_same_domain_keeper_entry(const struct ccs_acl_head *a,
325                                             const struct ccs_acl_head *b)
 static LIST1_HEAD(domain_keeper_list);  
   
 static int AddDomainKeeperEntry(const char *domainname, const char *program, const bool is_not, const bool is_delete)  
326  {  {
327          struct domain_keeper_entry *new_entry, *ptr;          const struct ccs_domain_keeper *p1 = container_of(a, typeof(*p1),
328          const struct path_info *saved_domainname, *saved_program = NULL;                                                            head);
329          static DEFINE_MUTEX(lock);          const struct ccs_domain_keeper *p2 = container_of(b, typeof(*p2),
330          int error = -ENOMEM;                                                            head);
331          bool is_last_name = 0;          return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
332          if (!IsDomainDef(domainname) && IsCorrectPath(domainname, 1, -1, -1, __FUNCTION__)) {                  && p1->domainname == p2->domainname
333                  is_last_name = 1;                  && p1->program == p2->program;
334          } else if (!IsCorrectDomain(domainname, __FUNCTION__)) {  }
335    
336    /**
337     * ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper" list.
338     *
339     * @domainname: The name of domain.
340     * @program:    The name of program. May be NULL.
341     * @is_not:     True if it is "no_keep_domain" entry.
342     * @is_delete:  True if it is a delete request.
343     *
344     * Returns 0 on success, negative value otherwise.
345     */
346    static int ccs_update_domain_keeper_entry(const char *domainname,
347                                              const char *program,
348                                              const bool is_not,
349                                              const bool is_delete)
350    {
351            struct ccs_domain_keeper e = { .is_not = is_not };
352            int error = is_delete ? -ENOENT : -ENOMEM;
353            if (!ccs_domain_def(domainname) && ccs_correct_path(domainname))
354                    e.is_last_name = true;
355            else if (!ccs_correct_domain(domainname))
356                  return -EINVAL;                  return -EINVAL;
         }  
357          if (program) {          if (program) {
358                  if (!IsCorrectPath(program, 1, -1, -1, __FUNCTION__)) return -EINVAL;                  if (!ccs_correct_path(program))
359                  if ((saved_program = SaveName(program)) == NULL) return -ENOMEM;                          return -EINVAL;
360          }                  e.program = ccs_get_name(program);
361          if ((saved_domainname = SaveName(domainname)) == NULL) return -ENOMEM;                  if (!e.program)
         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) {  
                         ptr->is_deleted = is_delete;  
                         error = 0;  
362                          goto out;                          goto out;
                 }  
363          }          }
364          if (is_delete) {          e.domainname = ccs_get_name(domainname);
365                  error = -ENOENT;          if (!e.domainname)
366                  goto out;                  goto out;
367          }          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
368          if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;                                    CCS_ID_DOMAIN_KEEPER,
369          new_entry->domainname = saved_domainname;                                    ccs_same_domain_keeper_entry);
         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;  
370   out:   out:
371          mutex_unlock(&lock);          ccs_put_name(e.domainname);
372            ccs_put_name(e.program);
373          return error;          return error;
374  }  }
375    
376  int AddDomainKeeperPolicy(char *data, const bool is_not, const bool is_delete)  /**
377     * ccs_write_domain_keeper - Write "struct ccs_domain_keeper" list.
378     *
379     * @data:      String to parse.
380     * @is_delete: True if it is a delete request.
381     *
382     * Returns 0 on success, negative value otherwise.
383     */
384    int ccs_write_domain_keeper(char *data, const bool is_delete, const u8 flags)
385  {  {
386          char *cp = strstr(data, " from ");          char *domainname = strstr(data, " from ");
387          if (cp) {          if (domainname) {
388                  *cp = '\0';                  *domainname = '\0';
389                  return AddDomainKeeperEntry(cp + 6, data, is_not, is_delete);                  domainname += 6;
390          } else {          } else {
391                  return AddDomainKeeperEntry(data, NULL, is_not, is_delete);                  domainname = data;
392                    data = NULL;
393          }          }
394            return ccs_update_domain_keeper_entry(domainname, data, flags,
395                                                  is_delete);
396  }  }
397    
398  int ReadDomainKeeperPolicy(struct io_buffer *head)  /**
399  {   * ccs_domain_keeper - Check whether the given program causes domain transition suppression.
400          struct list1_head *pos;   *
401          list1_for_each_cookie(pos, head->read_var2, &domain_keeper_list) {   * @domainname: The name of domain.
402                  struct domain_keeper_entry *ptr;   * @program:    The name of program.
403                  ptr = list1_entry(pos, struct domain_keeper_entry, list);   * @last_name:  The last component of @domainname.
404                  if (ptr->is_deleted) continue;   *
405                  if (ptr->program) {   * Returns true if executing @program supresses domain transition,
406                          if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s from %s\n", ptr->is_not ? "no_" : "", ptr->program->name, ptr->domainname->name)) return -ENOMEM;   * false otherwise.
407                  } else {   *
408                          if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s\n", ptr->is_not ? "no_" : "", ptr->domainname->name)) return -ENOMEM;   * Caller holds ccs_read_lock().
409                  }   */
410          }  static bool ccs_domain_keeper(const struct ccs_path_info *domainname,
411          return 0;                                   const struct ccs_path_info *program,
412  }                                   const struct ccs_path_info *last_name)
413    {
414  static bool IsDomainKeeper(const struct path_info *domainname, const struct path_info *program, const struct path_info *last_name)          struct ccs_domain_keeper *ptr;
415  {          bool flag = false;
416          struct domain_keeper_entry *ptr;          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_DOMAIN_KEEPER],
417          bool flag = 0;                                  head.list) {
418          list1_for_each_entry(ptr, &domain_keeper_list, list) {                  if (ptr->head.is_deleted)
419                  if (ptr->is_deleted) continue;                          continue;
420                  if (!ptr->is_last_name) {                  if (!ptr->is_last_name) {
421                          if (ptr->domainname != domainname) continue;                          if (ptr->domainname != domainname)
422                                    continue;
423                  } else {                  } else {
424                          if (pathcmp(ptr->domainname, last_name)) continue;                          if (ccs_pathcmp(ptr->domainname, last_name))
425                                    continue;
426                  }                  }
427                  if (ptr->program && pathcmp(ptr->program, program)) continue;                  if (ptr->program && ccs_pathcmp(ptr->program, program))
428                  if (ptr->is_not) return 0;                          continue;
429                  flag = 1;                  if (ptr->is_not) {
430          }                          flag = false;
431          return flag;                          break;
 }  
   
 /*************************  SYMBOLIC LINKED PROGRAM HANDLER  *************************/  
   
 static LIST1_HEAD(alias_list);  
   
 static int AddAliasEntry(const char *original_name, const char *aliased_name, const bool is_delete)  
 {  
         struct alias_entry *new_entry, *ptr;  
         static DEFINE_MUTEX(lock);  
         const struct path_info *saved_original_name, *saved_aliased_name;  
         int error = -ENOMEM;  
         if (!IsCorrectPath(original_name, 1, -1, -1, __FUNCTION__) || !IsCorrectPath(aliased_name, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */  
         if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aliased_name = SaveName(aliased_name)) == NULL) 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) {  
                         ptr->is_deleted = is_delete;  
                         error = 0;  
                         goto out;  
432                  }                  }
433                    flag = true;
434          }          }
435          if (is_delete) {          return flag;
                 error = -ENOENT;  
                 goto out;  
         }  
         if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) 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);  
         return error;  
 }  
   
 int ReadAliasPolicy(struct io_buffer *head)  
 {  
         struct list1_head *pos;  
         list1_for_each_cookie(pos, head->read_var2, &alias_list) {  
                 struct alias_entry *ptr;  
                 ptr = list1_entry(pos, struct alias_entry, list);  
                 if (ptr->is_deleted) continue;  
                 if (io_printf(head, KEYWORD_ALIAS "%s %s\n", ptr->original_name->name, ptr->aliased_name->name)) return -ENOMEM;  
         }  
         return 0;  
436  }  }
437    
438  int AddAliasPolicy(char *data, const bool is_delete)  static bool ccs_same_aggregator_entry(const struct ccs_acl_head *a,
439                                          const struct ccs_acl_head *b)
440  {  {
441          char *cp = strchr(data, ' ');          const struct ccs_aggregator *p1 = container_of(a, typeof(*p1), head);
442          if (!cp) return -EINVAL;          const struct ccs_aggregator *p2 = container_of(b, typeof(*p2), head);
443          *cp++ = '\0';          return p1->original_name == p2->original_name &&
444          return AddAliasEntry(data, cp, is_delete);                  p1->aggregated_name == p2->aggregated_name;
445  }  }
446    
447  /*************************  DOMAIN AGGREGATOR HANDLER  *************************/  /**
448     * ccs_update_aggregator_entry - Update "struct ccs_aggregator" list.
449  static LIST1_HEAD(aggregator_list);   *
450     * @original_name:   The original program's name.
451  static int AddAggregatorEntry(const char *original_name, const char *aggregated_name, const bool is_delete)   * @aggregated_name: The aggregated program's name.
452  {   * @is_delete:       True if it is a delete request.
453          struct aggregator_entry *new_entry, *ptr;   *
454          static DEFINE_MUTEX(lock);   * Returns 0 on success, negative value otherwise.
455          const struct path_info *saved_original_name, *saved_aggregated_name;   */
456          int error = -ENOMEM;  static int ccs_update_aggregator_entry(const char *original_name,
457          if (!IsCorrectPath(original_name, 1, 0, -1, __FUNCTION__) || !IsCorrectPath(aggregated_name, 1, -1, -1, __FUNCTION__)) return -EINVAL;                                         const char *aggregated_name,
458          if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aggregated_name = SaveName(aggregated_name)) == NULL) return -ENOMEM;                                         const bool is_delete)
459          mutex_lock(&lock);  {
460          list1_for_each_entry(ptr, &aggregator_list, list) {          struct ccs_aggregator e = { };
461                  if (ptr->original_name == saved_original_name && ptr->aggregated_name == saved_aggregated_name) {          int error = is_delete ? -ENOENT : -ENOMEM;
462                          ptr->is_deleted = is_delete;          if (!ccs_correct_path(original_name) ||
463                          error = 0;              !ccs_correct_path(aggregated_name))
464                          goto out;                  return -EINVAL;
465                  }          e.original_name = ccs_get_name(original_name);
466          }          e.aggregated_name = ccs_get_name(aggregated_name);
467          if (is_delete) {          if (!e.original_name || !e.aggregated_name ||
468                  error = -ENOENT;              e.aggregated_name->is_patterned) /* No patterns allowed. */
469                  goto out;                  goto out;
470          }          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
471          if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;                                    CCS_ID_AGGREGATOR,
472          new_entry->original_name = saved_original_name;                                    ccs_same_aggregator_entry);
         new_entry->aggregated_name = saved_aggregated_name;  
         list1_add_tail_mb(&new_entry->list, &aggregator_list);  
         error = 0;  
473   out:   out:
474          mutex_unlock(&lock);          ccs_put_name(e.original_name);
475            ccs_put_name(e.aggregated_name);
476          return error;          return error;
477  }  }
478    
479  int ReadAggregatorPolicy(struct io_buffer *head)  /**
480     * ccs_write_aggregator - Write "struct ccs_aggregator" list.
481     *
482     * @data:      String to parse.
483     * @is_delete: True if it is a delete request.
484     *
485     * Returns 0 on success, negative value otherwise.
486     */
487    int ccs_write_aggregator(char *data, const bool is_delete, const u8 flags)
488  {  {
489          struct list1_head *pos;          char *w[2];
490          list1_for_each_cookie(pos, head->read_var2, &aggregator_list) {          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
491                  struct aggregator_entry *ptr;                  return -EINVAL;
492                  ptr = list1_entry(pos, struct aggregator_entry, list);          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
                 if (ptr->is_deleted) continue;  
                 if (io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", ptr->original_name->name, ptr->aggregated_name->name)) return -ENOMEM;  
         }  
         return 0;  
493  }  }
494    
495  int AddAggregatorPolicy(char *data, const bool is_delete)  /* Domain create/delete handler. */
496    
497    /**
498     * ccs_delete_domain - Delete a domain.
499     *
500     * @domainname: The name of domain.
501     *
502     * Returns 0.
503     */
504    int ccs_delete_domain(char *domainname)
505  {  {
506          char *cp = strchr(data, ' ');          struct ccs_domain_info *domain;
507          if (!cp) return -EINVAL;          struct ccs_path_info name;
508          *cp++ = '\0';          name.name = domainname;
509          return AddAggregatorEntry(data, cp, is_delete);          ccs_fill_path_info(&name);
510  }          if (mutex_lock_interruptible(&ccs_policy_lock))
511                    return 0;
 /*************************  DOMAIN DELETION HANDLER  *************************/  
   
 /* #define DEBUG_DOMAIN_UNDELETE */  
   
 int DeleteDomain(char *domainname0)  
 {  
         struct domain_info *domain;  
         struct path_info domainname;  
         domainname.name = domainname0;  
         fill_path_info(&domainname);  
         mutex_lock(&new_domain_assign_lock);  
 #ifdef DEBUG_DOMAIN_UNDELETE  
         printk("DeleteDomain %s\n", domainname0);  
         list1_for_each_entry(domain, &domain_list, list) {  
                 if (pathcmp(domain->domainname, &domainname)) continue;  
                 printk("List: %p %u\n", domain, domain->is_deleted);  
         }  
 #endif  
512          /* Is there an active domain? */          /* Is there an active domain? */
513          list1_for_each_entry(domain, &domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
514                  struct domain_info *domain2;                  /* Never delete ccs_kernel_domain */
515                  /* Never delete KERNEL_DOMAIN */                  if (domain == &ccs_kernel_domain)
516                  if (domain == &KERNEL_DOMAIN || domain->is_deleted || pathcmp(domain->domainname, &domainname)) continue;                          continue;
517                  /* Mark already deleted domains as non undeletable. */                  if (domain->is_deleted ||
518                  list1_for_each_entry(domain2, &domain_list, list) {                      ccs_pathcmp(domain->domainname, &name))
519                          if (!domain2->is_deleted || pathcmp(domain2->domainname, &domainname)) continue;                          continue;
520  #ifdef DEBUG_DOMAIN_UNDELETE                  domain->is_deleted = true;
                         if (domain2->is_deleted != 255) printk("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("Marked %p as undeletable\n", domain);  
 #endif  
521                  break;                  break;
522          }          }
523          mutex_unlock(&new_domain_assign_lock);          mutex_unlock(&ccs_policy_lock);
524          return 0;          return 0;
525  }  }
526    
527  struct domain_info *UndeleteDomain(const char *domainname0)  /**
528  {   * ccs_assign_domain - Create a domain.
529          struct domain_info *domain, *candidate_domain = NULL;   *
530          struct path_info domainname;   * @domainname: The name of domain.
531          domainname.name = domainname0;   * @profile:    Profile number to assign if the domain was newly created.
532          fill_path_info(&domainname);   *
533          mutex_lock(&new_domain_assign_lock);   * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
534  #ifdef DEBUG_DOMAIN_UNDELETE   */
535          printk("UndeleteDomain %s\n", domainname0);  struct ccs_domain_info *ccs_assign_domain(const char *domainname,
536          list1_for_each_entry(domain, &domain_list, list) {                                            const u8 profile)
                 if (pathcmp(domain->domainname, &domainname)) continue;  
                 printk("List: %p %u\n", domain, domain->is_deleted);  
         }  
 #endif  
         list1_for_each_entry(domain, &domain_list, list) {  
                 if (pathcmp(&domainname, domain->domainname)) continue;  
                 if (!domain->is_deleted) {  
                         /* This domain is active. I can't undelete. */  
                         candidate_domain = NULL;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                         printk("%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("%p was undeleted.\n", candidate_domain);  
 #endif  
         }  
         mutex_unlock(&new_domain_assign_lock);  
         return candidate_domain;  
 }  
   
 /*************************  DOMAIN TRANSITION HANDLER  *************************/  
   
 struct domain_info *FindOrAssignNewDomain(const char *domainname, const u8 profile)  
537  {  {
538          struct domain_info *domain = NULL;          struct ccs_domain_info e = { };
539          const struct path_info *saved_domainname;          struct ccs_domain_info *entry = NULL;
540          mutex_lock(&new_domain_assign_lock);          bool found = false;
541          if ((domain = FindDomain(domainname)) != NULL) goto out;  
542          if (!IsCorrectDomain(domainname, __FUNCTION__)) goto out;          if (!ccs_correct_domain(domainname))
543          if ((saved_domainname = SaveName(domainname)) == NULL) goto out;                  return NULL;
544          /* Can I reuse memory of deleted domain? */          e.profile = profile;
545          list1_for_each_entry(domain, &domain_list, list) {          e.domainname = ccs_get_name(domainname);
546                  struct task_struct *p;          if (!e.domainname)
547                  struct acl_info *ptr;                  return NULL;
548                  int flag;          if (mutex_lock_interruptible(&ccs_policy_lock))
                 if (!domain->is_deleted || domain->domainname != saved_domainname) continue;  
                 flag = 0;  
                 /***** CRITICAL SECTION START *****/  
                 read_lock(&tasklist_lock);  
                 for_each_process(p) {  
                         if (p->domain_info == domain) { flag = 1; break; }  
                 }  
                 read_unlock(&tasklist_lock);  
                 /***** CRITICAL SECTION END *****/  
                 if (flag) continue;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                 printk("Reusing %p %s\n", domain, domain->domainname->name);  
 #endif  
                 list1_for_each_entry(ptr, &domain->acl_info_list, list) ptr->is_deleted = 1;  
                 domain->profile = profile;  
                 domain->quota_warned = 0;  
                 mb(); /* Avoid out-of-order execution. */  
                 domain->is_deleted = 0;  
549                  goto out;                  goto out;
550            list_for_each_entry_rcu(entry, &ccs_domain_list, list) {
551                    if (entry->is_deleted ||
552                        ccs_pathcmp(e.domainname, entry->domainname))
553                            continue;
554                    found = true;
555                    break;
556          }          }
557          /* No memory reusable. Create using new memory. */          if (!found) {
558          if ((domain = alloc_element(sizeof(*domain))) != NULL) {                  entry = ccs_commit_ok(&e, sizeof(e));
559                  INIT_LIST1_HEAD(&domain->acl_info_list);                  if (entry) {
560                  domain->domainname = saved_domainname;                          INIT_LIST_HEAD(&entry->acl_info_list);
561                  domain->profile = profile;                          list_add_tail_rcu(&entry->list, &ccs_domain_list);
562                  list1_add_tail_mb(&domain->list, &domain_list);                          found = true;
         }  
  out: ;  
         mutex_unlock(&new_domain_assign_lock);  
         return domain;  
 }  
   
 static int Escape(char *dest, const char *src, int dest_len)  
 {  
         while (*src) {  
                 const unsigned char c = * (const unsigned char *) src;  
                 if (c == '\\') {  
                         dest_len -= 2;  
                         if (dest_len <= 0) goto out;  
                         *dest++ = '\\';  
                         *dest++ = '\\';  
                 } else if (c > ' ' && c < 127) {  
                         if (--dest_len <= 0) goto out;  
                         *dest++ = c;  
                 } else {  
                         dest_len -= 4;  
                         if (dest_len <= 0) goto out;  
                         *dest++ = '\\';  
                         *dest++ = (c >> 6) + '0';  
                         *dest++ = ((c >> 3) & 7) + '0';  
                         *dest++ = (c & 7) + '0';  
563                  }                  }
                 src++;  
564          }          }
565          if (--dest_len <= 0) goto out;          mutex_unlock(&ccs_policy_lock);
         *dest = '\0';  
         return 0;  
566   out:   out:
567          return -ENOMEM;          ccs_put_name(e.domainname);
568            return found ? entry : NULL;
569  }  }
570    
571  static char *get_argv0(struct linux_binprm *bprm)  /**
572  {   * ccs_find_next_domain - Find a domain.
573          char *arg_ptr = ccs_alloc(PAGE_SIZE); /* Initial buffer. */   *
574          int arg_len = 0;   * @ee: Pointer to "struct ccs_execve".
575          unsigned long pos = bprm->p;   *
576          int i = pos / PAGE_SIZE, offset = pos % PAGE_SIZE;   * Returns 0 on success, negative value otherwise.
577          if (!bprm->argc || !arg_ptr) goto out;   *
578          while (1) {   * Caller holds ccs_read_lock().
579                  struct page *page;   */
580  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)  static int ccs_find_next_domain(struct ccs_execve *ee)
                 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) goto out;  
 #else  
                 page = bprm->page[i];  
 #endif  
                 { /* Map and copy to kernel buffer and unmap. */  
                         const char *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;  
                         }  
                         memmove(arg_ptr + arg_len, kaddr + offset, PAGE_SIZE - offset);  
                         kunmap(page);  
                 }  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)  
                 put_page(page);  
                 pos += PAGE_SIZE - offset;  
 #endif  
                 arg_len += PAGE_SIZE - offset;  
                 if (memchr(arg_ptr, '\0', arg_len)) break;  
                 { /* Initial buffer was too small for argv[0]. Retry after expanding buffer. */  
                         char *tmp_arg_ptr = ccs_alloc(arg_len + PAGE_SIZE);  
                         if (!tmp_arg_ptr) goto out;  
                         memmove(tmp_arg_ptr, arg_ptr, arg_len);  
                         ccs_free(arg_ptr);  
                         arg_ptr = tmp_arg_ptr;  
                 }  
                 i++;  
                 offset = 0;  
         }  
         return arg_ptr;  
  out: /* Release initial buffer. */  
         ccs_free(arg_ptr);  
         return NULL;  
 }  
   
 static int FindNextDomain(struct linux_binprm *bprm, struct domain_info **next_domain, const u8 do_perm_check)  
581  {  {
582          /* This function assumes that the size of buffer returned by realpath() = CCS_MAX_PATHNAME_LEN. */          struct ccs_request_info *r = &ee->r;
583          struct domain_info *old_domain = current->domain_info, *domain = NULL;          const struct ccs_path_info *handler = ee->handler;
584            struct ccs_domain_info *domain = NULL;
585            struct ccs_domain_info * const old_domain = ccs_current_domain();
586          const char *old_domain_name = old_domain->domainname->name;          const char *old_domain_name = old_domain->domainname->name;
587          const char *original_name = bprm->filename;          struct linux_binprm *bprm = ee->bprm;
588          struct file *filp = bprm->file;          struct task_struct *task = current;
589          char *new_domain_name = NULL;          const u32 ccs_flags = task->ccs_flags;
590          char *real_program_name = NULL, *symlink_program_name = NULL;          struct ccs_path_info rn = { }; /* real name */
591          const bool is_enforce = (CheckCCSFlags(CCS_TOMOYO_MAC_FOR_FILE) == 3);          struct ccs_path_info ln; /* last name */
592          int retval;          int retval;
593          struct path_info r, s, l;          bool need_kfree = false;
594            bool domain_created = false;
595          {          ln.name = ccs_last_word(old_domain_name);
596                  /*          ccs_fill_path_info(&ln);
597                   * Built-in initializers. This is needed because policies are not loaded until starting /sbin/init .   retry:
598                   */          current->ccs_flags = ccs_flags;
599                  static int first = 1;          r->cond = NULL;
600                  if (first) {          if (need_kfree) {
601                          AddDomainInitializerEntry(NULL, "/sbin/hotplug", 0, 0);                  kfree(rn.name);
602                          AddDomainInitializerEntry(NULL, "/sbin/modprobe", 0, 0);                  need_kfree = false;
                         first = 0;  
                 }  
603          }          }
604    
605          /* Get realpath of program. */          /* Get symlink's pathname of program. */
606          retval = -ENOENT; /* I hope realpath() won't fail with -ENOMEM. */          retval = ccs_symlink_path(bprm->filename, &rn);
607          if ((real_program_name = realpath(original_name)) == NULL) goto out;          if (retval < 0)
608          /* Get realpath of symbolic link. */                  goto out;
609          if ((symlink_program_name = realpath_nofollow(original_name)) == NULL) goto out;          need_kfree = true;
610    
611          r.name = real_program_name;          if (handler) {
612          fill_path_info(&r);                  if (ccs_pathcmp(&rn, handler)) {
613          s.name = symlink_program_name;                          /* Failed to verify execute handler. */
614          fill_path_info(&s);                          static u8 counter = 20;
615          if ((l.name = strrchr(old_domain_name, ' ')) != NULL) l.name++;                          if (counter) {
616          else l.name = old_domain_name;                                  counter--;
617          fill_path_info(&l);                                  printk(KERN_WARNING "Failed to verify: %s\n",
618                                           handler->name);
         if (!do_perm_check) goto ok;  
   
         /* Check 'alias' directive. */  
         if (pathcmp(&r, &s)) {  
                 struct alias_entry *ptr;  
                 /* Is this program allowed to be called via symbolic links? */  
                 list1_for_each_entry(ptr, &alias_list, list) {  
                         if (ptr->is_deleted || pathcmp(&r, ptr->original_name) || pathcmp(&s, ptr->aliased_name)) continue;  
                         memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN);  
                         strncpy(real_program_name, ptr->aliased_name->name, CCS_MAX_PATHNAME_LEN - 1);  
                         fill_path_info(&r);  
                         break;  
                 }  
         }  
           
         /* Compare basename of real_program_name and argv[0] */  
         if (bprm->argc > 0 && CheckCCSFlags(CCS_TOMOYO_MAC_FOR_ARGV0)) {  
                 char *org_argv0 = get_argv0(bprm);  
                 retval = -ENOMEM;  
                 if (org_argv0) {  
                         const int len = strlen(org_argv0);  
                         char *printable_argv0 = ccs_alloc(len * 4 + 8);  
                         if (printable_argv0 && Escape(printable_argv0, org_argv0, len * 4 + 8) == 0) {  
                                 const char *base_argv0, *base_filename;  
                                 if ((base_argv0 = strrchr(printable_argv0, '/')) == NULL) base_argv0 = printable_argv0; else base_argv0++;  
                                 if ((base_filename = strrchr(real_program_name, '/')) == NULL) base_filename = real_program_name; else base_filename++;  
                                 if (strcmp(base_argv0, base_filename)) retval = CheckArgv0Perm(&r, base_argv0);  
                                 else retval = 0;  
619                          }                          }
620                          ccs_free(printable_argv0);                          goto out;
                         ccs_free(org_argv0);  
621                  }                  }
622                  if (retval) goto out;          } else {
623          }                  struct ccs_aggregator *ptr;
624                            /* Check 'aggregator' directive. */
625          /* Check 'aggregator' directive. */                  list_for_each_entry_rcu(ptr,
626          {                                          &ccs_policy_list[CCS_ID_AGGREGATOR],
627                  struct aggregator_entry *ptr;                                          head.list) {
628                  /* Is this program allowed to be aggregated? */                          if (ptr->head.is_deleted ||
629                  list1_for_each_entry(ptr, &aggregator_list, list) {                              !ccs_path_matches_pattern(&rn, ptr->original_name))
630                          if (ptr->is_deleted || !PathMatchesToPattern(&r, ptr->original_name)) continue;                                  continue;
631                          memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN);                          kfree(rn.name);
632                          strncpy(real_program_name, ptr->aggregated_name->name, CCS_MAX_PATHNAME_LEN - 1);                          need_kfree = false;
633                          fill_path_info(&r);                          /* This is OK because it is read only. */
634                            rn = *ptr->aggregated_name;
635                          break;                          break;
636                  }                  }
         }  
637    
638          /* Check execute permission. */                  /* Check execute permission. */
639          if ((retval = CheckExecPerm(&r, filp)) < 0) goto out;                  retval = ccs_path_permission(r, CCS_TYPE_EXECUTE, &rn);
640                    if (retval == CCS_RETRY_REQUEST)
641                            goto retry;
642                    if (retval < 0)
643                            goto out;
644            }
645    
646   ok: ;          /* Calculate domain to transit to. */
647          /* Allocate memory for calcurating domain name. */          if (ccs_domain_initializer(old_domain->domainname, &rn, &ln)) {
648          retval = -ENOMEM;                  /* Transit to the child of ccs_kernel_domain domain. */
649          if ((new_domain_name = ccs_alloc(CCS_MAX_PATHNAME_LEN + 16)) == NULL) goto out;                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, CCS_ROOT_NAME " " "%s",
650                                     rn.name);
651          if (IsDomainInitializer(old_domain->domainname, &r, &l)) {          } else if (old_domain == &ccs_kernel_domain && !ccs_policy_loaded) {
                 /* 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 (old_domain == &KERNEL_DOMAIN && !sbin_init_started) {  
652                  /*                  /*
653                   * Needn't to transit from kernel domain before starting /sbin/init .                   * Needn't to transit from kernel domain before starting
654                   * But transit from kernel domain if executing initializers, for they might start before /sbin/init .                   * /sbin/init. But transit from kernel domain if executing
655                     * initializers because they might start before /sbin/init.
656                   */                   */
657                  domain = old_domain;                  domain = old_domain;
658          } else if (IsDomainKeeper(old_domain->domainname, &r, &l)) {          } else if (ccs_domain_keeper(old_domain->domainname, &rn, &ln)) {
659                  /* Keep current domain. */                  /* Keep current domain. */
660                  domain = old_domain;                  domain = old_domain;
661          } else {          } else {
662                  /* Normal domain transition. */                  /* Normal domain transition. */
663                  snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, "%s %s", old_domain_name, real_program_name);                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
664          }                           old_domain_name, rn.name);
         if (!domain && strlen(new_domain_name) < CCS_MAX_PATHNAME_LEN) {  
                 if (is_enforce) {  
                         domain = FindDomain(new_domain_name);  
                         if (!domain) if (CheckSupervisor("#Need to create domain\n%s\n", new_domain_name) == 0) domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile);  
                 } else {  
                         domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile);  
                 }  
665          }          }
666            if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10)
667                    goto done;
668            domain = ccs_find_domain(ee->tmp);
669            if (domain)
670                    goto done;
671            if (r->mode == CCS_CONFIG_ENFORCING) {
672                    int error = ccs_supervisor(r, "# wants to create domain\n"
673                                               "%s\n", ee->tmp);
674                    if (error == CCS_RETRY_REQUEST)
675                            goto retry;
676                    if (error < 0)
677                            goto done;
678            }
679            domain = ccs_assign_domain(ee->tmp, r->profile);
680            if (domain)
681                    domain_created = true;
682     done:
683          if (!domain) {          if (!domain) {
684                  printk("TOMOYO-ERROR: Domain '%s' not defined.\n", new_domain_name);                  retval = (r->mode == CCS_CONFIG_ENFORCING) ? -EPERM : 0;
685                  if (is_enforce) retval = -EPERM;                  if (!old_domain->domain_transition_failed) {
686                            old_domain->domain_transition_failed = true;
687                            r->granted = false;
688                            ccs_write_log(r, CCS_KEYWORD_TRANSITION_FAILED "\n");
689                            printk(KERN_WARNING
690                                   "ERROR: Domain '%s' not defined.\n", ee->tmp);
691                    }
692          } else {          } else {
693                  retval = 0;                  retval = 0;
694          }          }
695   out: ;          if (!retval && handler)
696          ccs_free(new_domain_name);                  ccs_audit_execute_handler_log(ee);
697          ccs_free(real_program_name);          /*
698          ccs_free(symlink_program_name);           * Tell GC that I started execve().
699          *next_domain = domain ? domain : old_domain;           * Also, tell open_exec() to check read permission.
700             */
701            task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
702            /*
703             * Make task->ccs_flags visible to GC before changing
704             * task->ccs_domain_info .
705             */
706            smp_mb();
707            /*
708             * Proceed to the next domain in order to allow reaching via PID.
709             * It will be reverted if execve() failed. Reverting is not good.
710             * But it is better than being unable to reach via PID in interactive
711             * enforcing mode.
712             */
713            if (domain)
714                    task->ccs_domain_info = domain;
715            if (domain_created)
716                    ccs_audit_domain_creation_log();
717     out:
718            if (need_kfree)
719                    kfree(rn.name);
720          return retval;          return retval;
721  }  }
722    
723  static int CheckEnviron(struct linux_binprm *bprm)  /**
724     * ccs_environ - Check permission for environment variable names.
725     *
726     * @ee: Pointer to "struct ccs_execve".
727     *
728     * Returns 0 on success, negative value otherwise.
729     */
730    static int ccs_environ(struct ccs_execve *ee)
731  {  {
732          const u8 profile = current->domain_info->profile;          struct ccs_request_info *r = &ee->r;
733          const u8 mode = CheckCCSFlags(CCS_TOMOYO_MAC_FOR_ENV);          struct linux_binprm *bprm = ee->bprm;
734          char *arg_ptr;          /* env_page->data is allocated by ccs_dump_page(). */
735            struct ccs_page_dump env_page = { };
736            char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
737          int arg_len = 0;          int arg_len = 0;
738          unsigned long pos = bprm->p;          unsigned long pos = bprm->p;
739          int i = pos / PAGE_SIZE, offset = pos % PAGE_SIZE;          int offset = pos % PAGE_SIZE;
740          int argv_count = bprm->argc;          int argv_count = bprm->argc;
741          int envp_count = bprm->envc;          int envp_count = bprm->envc;
742          //printk("start %d %d\n", argv_count, envp_count);          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
743          int error = -ENOMEM;          int error = -ENOMEM;
744          if (!mode || !envp_count) return 0;          ee->r.type = CCS_MAC_ENVIRON;
745          arg_ptr = ccs_alloc(CCS_MAX_PATHNAME_LEN);          ee->r.mode = ccs_get_mode(ccs_current_domain()->profile,
746          if (!arg_ptr) goto out;                                    CCS_MAC_ENVIRON);
747            if (!r->mode || !envp_count)
748                    return 0;
749            arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
750            if (!arg_ptr)
751                    goto out;
752          while (error == -ENOMEM) {          while (error == -ENOMEM) {
753                  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) goto out;  
 #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  
754                          goto out;                          goto out;
755                  }                  pos += PAGE_SIZE - offset;
756                  /* Read. */                  /* Read. */
757                  while (argv_count && offset < PAGE_SIZE) {                  while (argv_count && offset < PAGE_SIZE) {
758                          if (!kaddr[offset++]) argv_count--;                          if (!env_page.data[offset++])
759                                    argv_count--;
760                    }
761                    if (argv_count) {
762                            offset = 0;
763                            continue;
764                  }                  }
                 if (argv_count) goto unmap_page;  
765                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
766                          const unsigned char c = kaddr[offset++];                          const unsigned char c = env_page.data[offset++];
767                          if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
768                                  if (c == '=') {                                  if (c == '=') {
769                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
770                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 783  static int CheckEnviron(struct linux_bin Line 775  static int CheckEnviron(struct linux_bin
775                                  } else {                                  } else {
776                                          arg_ptr[arg_len++] = '\\';                                          arg_ptr[arg_len++] = '\\';
777                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          arg_ptr[arg_len++] = (c >> 6) + '0';
778                                          arg_ptr[arg_len++] = ((c >> 3) & 7) + '0';                                          arg_ptr[arg_len++]
779                                                    = ((c >> 3) & 7) + '0';
780                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          arg_ptr[arg_len++] = (c & 7) + '0';
781                                  }                                  }
782                          } else {                          } else {
783                                  arg_ptr[arg_len] = '\0';                                  arg_ptr[arg_len] = '\0';
784                          }                          }
785                          if (c) continue;                          if (c)
786                          if (CheckEnvPerm(arg_ptr, profile, mode)) {                                  continue;
787                            if (ccs_env_perm(r, arg_ptr)) {
788                                  error = -EPERM;                                  error = -EPERM;
789                                  break;                                  break;
790                          }                          }
# Line 800  static int CheckEnviron(struct linux_bin Line 794  static int CheckEnviron(struct linux_bin
794                          }                          }
795                          arg_len = 0;                          arg_len = 0;
796                  }                  }
         unmap_page:  
                 /* Unmap. */  
                 kunmap(page);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)  
                 put_page(page);  
                 pos += PAGE_SIZE - offset;  
 #endif  
                 i++;  
797                  offset = 0;                  offset = 0;
798          }          }
799   out:   out:
800          ccs_free(arg_ptr);          if (r->mode != 3)
801          if (error && mode != 3) error = 0;                  error = 0;
802            kfree(env_page.data);
803            kfree(arg_ptr);
804          return error;          return error;
805  }  }
806    
807  static void UnEscape(unsigned char *dest)  /**
808     * ccs_unescape - Unescape escaped string.
809     *
810     * @dest: String to unescape.
811     *
812     * Returns nothing.
813     */
814    static void ccs_unescape(unsigned char *dest)
815  {  {
816          unsigned char *src = dest;          unsigned char *src = dest;
817          unsigned char c, d, e;          unsigned char c;
818          while ((c = *src++) != '\0') {          unsigned char d;
819            unsigned char e;
820            while (1) {
821                    c = *src++;
822                    if (!c)
823                            break;
824                  if (c != '\\') {                  if (c != '\\') {
825                          *dest++ = c;                          *dest++ = c;
826                          continue;                          continue;
# Line 828  static void UnEscape(unsigned char *dest Line 828  static void UnEscape(unsigned char *dest
828                  c = *src++;                  c = *src++;
829                  if (c == '\\') {                  if (c == '\\') {
830                          *dest++ = c;                          *dest++ = c;
831                  } else if (c >= '0' && c <= '3' &&                          continue;
                            (d = *src++) >= '0' && d <= '7' &&  
                            (e = *src++) >= '0' && e <= '7') {  
                         *dest++ = ((c - '0') << 6) | ((d - '0') << 3) | (e - '0');  
                 } else {  
                         break;  
832                  }                  }
833                    if (c < '0' || c > '3')
834                            break;
835                    d = *src++;
836                    if (d < '0' || d > '7')
837                            break;
838                    e = *src++;
839                    if (e < '0' || e > '7')
840                            break;
841                    *dest++ = ((c - '0') << 6) + ((d - '0') << 3) + (e - '0');
842          }          }
843          *dest = '\0';          *dest = '\0';
844  }  }
845    
846  static int try_alt_exec(struct linux_binprm *bprm, char **alt_exec0)  /**
847     * ccs_root_depth - Get number of directories to strip.
848     *
849     * @dentry: Pointer to "struct dentry".
850     * @vfsmnt: Pointer to "struct vfsmount".
851     *
852     * Returns number of directories to strip.
853     */
854    static int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)
855    {
856            int depth = 0;
857            ccs_realpath_lock();
858            for (;;) {
859                    if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
860                            /* Global root? */
861                            if (vfsmnt->mnt_parent == vfsmnt)
862                                    break;
863                            dentry = vfsmnt->mnt_mountpoint;
864                            vfsmnt = vfsmnt->mnt_parent;
865                            continue;
866                    }
867                    dentry = dentry->d_parent;
868                    depth++;
869            }
870            ccs_realpath_unlock();
871            return depth;
872    }
873    
874    /**
875     * ccs_get_root_depth - return the depth of root directory.
876     *
877     * Returns number of directories to strip.
878     */
879    static int ccs_get_root_depth(void)
880    {
881            int depth;
882            struct dentry *dentry;
883            struct vfsmount *vfsmnt;
884    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
885            struct path root;
886    #endif
887            read_lock(&current->fs->lock);
888    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
889            root = current->fs->root;
890            path_get(&current->fs->root);
891            dentry = root.dentry;
892            vfsmnt = root.mnt;
893    #else
894            dentry = dget(current->fs->root);
895            vfsmnt = mntget(current->fs->rootmnt);
896    #endif
897            read_unlock(&current->fs->lock);
898            depth = ccs_root_depth(dentry, vfsmnt);
899    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
900            path_put(&root);
901    #else
902            dput(dentry);
903            mntput(vfsmnt);
904    #endif
905            return depth;
906    }
907    
908    /**
909     * ccs_try_alt_exec - Try to start execute handler.
910     *
911     * @ee: Pointer to "struct ccs_execve".
912     *
913     * Returns 0 on success, negative value otherwise.
914     */
915    static int ccs_try_alt_exec(struct ccs_execve *ee)
916  {  {
917            /*
918             * Contents of modified bprm.
919             * The envp[] in original bprm is moved to argv[] so that
920             * the alternatively executed program won't be affected by
921             * some dangerous environment variables like LD_PRELOAD.
922             *
923             * modified bprm->argc
924             *    = original bprm->argc + original bprm->envc + 7
925             * modified bprm->envc
926             *    = 0
927             *
928             * modified bprm->argv[0]
929             *    = the program's name specified by execute_handler
930             * modified bprm->argv[1]
931             *    = ccs_current_domain()->domainname->name
932             * modified bprm->argv[2]
933             *    = the current process's name
934             * modified bprm->argv[3]
935             *    = the current process's information (e.g. uid/gid).
936             * modified bprm->argv[4]
937             *    = original bprm->filename
938             * modified bprm->argv[5]
939             *    = original bprm->argc in string expression
940             * modified bprm->argv[6]
941             *    = original bprm->envc in string expression
942             * modified bprm->argv[7]
943             *    = original bprm->argv[0]
944             *  ...
945             * modified bprm->argv[bprm->argc + 6]
946             *     = original bprm->argv[bprm->argc - 1]
947             * modified bprm->argv[bprm->argc + 7]
948             *     = original bprm->envp[0]
949             *  ...
950             * modified bprm->argv[bprm->envc + bprm->argc + 6]
951             *     = original bprm->envp[bprm->envc - 1]
952             */
953            struct linux_binprm *bprm = ee->bprm;
954          struct file *filp;          struct file *filp;
955          int retval;          int retval;
956          /* domainname must not be modified. */          const int original_argc = bprm->argc;
957          char *domainname = (char *) current->domain_info->domainname->name;          const int original_envc = bprm->envc;
958          char *alt_exec;          struct task_struct *task = current;
959          const char *alt_exec1 = GetAltExec();  
960          if (!alt_exec1 || *alt_exec1 != '/') return -EINVAL;          /* Close the requested program's dentry. */
961          retval = strlen(alt_exec1) + 1;          ee->obj.path1.dentry = NULL;
962          *alt_exec0 = alt_exec = ccs_alloc(retval);          ee->obj.path1.mnt = NULL;
963          if (!alt_exec) return -ENOMEM;          ee->obj.validate_done = false;
         memmove(alt_exec, alt_exec1, retval);  
         UnEscape(alt_exec);  
964          allow_write_access(bprm->file);          allow_write_access(bprm->file);
965          fput(bprm->file);          fput(bprm->file);
966          bprm->file = NULL;          bprm->file = NULL;
967  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)  
968          retval = remove_arg_zero(bprm);          /* Invalidate page dump cache. */
969          if (retval) return retval;          ee->dump.page = NULL;
970  #else  
971          remove_arg_zero(bprm);          /* Move envp[] to argv[] */
972            bprm->argc += bprm->envc;
973            bprm->envc = 0;
974    
975            /* Set argv[6] */
976            {
977                    char *cp = ee->tmp;
978                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc);
979                    retval = copy_strings_kernel(1, &cp, bprm);
980                    if (retval < 0)
981                            goto out;
982                    bprm->argc++;
983            }
984    
985            /* Set argv[5] */
986            {
987                    char *cp = ee->tmp;
988                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
989                    retval = copy_strings_kernel(1, &cp, bprm);
990                    if (retval < 0)
991                            goto out;
992                    bprm->argc++;
993            }
994    
995            /* Set argv[4] */
996            {
997                    retval = copy_strings_kernel(1, &bprm->filename, bprm);
998                    if (retval < 0)
999                            goto out;
1000                    bprm->argc++;
1001            }
1002    
1003            /* Set argv[3] */
1004            {
1005                    char *cp = ee->tmp;
1006                    const u32 ccs_flags = task->ccs_flags;
1007                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
1008                             "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
1009                             "sgid=%d fsuid=%d fsgid=%d state[0]=%u "
1010                             "state[1]=%u state[2]=%u",
1011                             (pid_t) ccsecurity_exports.sys_getpid(),
1012                             current_uid(), current_gid(), current_euid(),
1013                             current_egid(), current_suid(), current_sgid(),
1014                             current_fsuid(), current_fsgid(),
1015                             (u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16),
1016                             (u8) (ccs_flags >> 8));
1017                    retval = copy_strings_kernel(1, &cp, bprm);
1018                    if (retval < 0)
1019                            goto out;
1020                    bprm->argc++;
1021            }
1022    
1023            /* Set argv[2] */
1024            {
1025                    char *exe = (char *) ccs_get_exe();
1026                    if (exe) {
1027                            retval = copy_strings_kernel(1, &exe, bprm);
1028                            kfree(exe);
1029                    } else {
1030                            exe = ee->tmp;
1031                            snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
1032                            retval = copy_strings_kernel(1, &exe, bprm);
1033                    }
1034                    if (retval < 0)
1035                            goto out;
1036                    bprm->argc++;
1037            }
1038    
1039            /* Set argv[1] */
1040            {
1041                    char *cp = ee->tmp;
1042                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
1043                             ccs_current_domain()->domainname->name);
1044                    retval = copy_strings_kernel(1, &cp, bprm);
1045                    if (retval < 0)
1046                            goto out;
1047                    bprm->argc++;
1048            }
1049    
1050            /* Set argv[0] */
1051            {
1052                    int depth = ccs_get_root_depth();
1053                    int len = ee->handler->total_len + 1;
1054                    char *cp = kmalloc(len, CCS_GFP_FLAGS);
1055                    if (!cp) {
1056                            retval = -ENOMEM;
1057                            goto out;
1058                    }
1059                    ee->handler_path = cp;
1060                    memmove(cp, ee->handler->name, len);
1061                    ccs_unescape(cp);
1062                    retval = -ENOENT;
1063                    if (!*cp || *cp != '/')
1064                            goto out;
1065                    /* Adjust root directory for open_exec(). */
1066                    while (depth) {
1067                            cp = strchr(cp + 1, '/');
1068                            if (!cp)
1069                                    goto out;
1070                            depth--;
1071                    }
1072                    memmove(ee->handler_path, cp, strlen(cp) + 1);
1073                    cp = ee->handler_path;
1074                    retval = copy_strings_kernel(1, &cp, bprm);
1075                    if (retval < 0)
1076                            goto out;
1077                    bprm->argc++;
1078            }
1079    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
1080    #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
1081            bprm->argv_len = bprm->exec - bprm->p;
1082  #endif  #endif
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)  
         retval = copy_strings_kernel(1, &bprm->interp, bprm);  
 #else  
         retval = copy_strings_kernel(1, &bprm->filename, bprm);  
1083  #endif  #endif
1084          if (retval < 0) return retval;  
1085          bprm->argc++;          /*
1086          retval = copy_strings_kernel(1, &domainname, bprm);           * OK, now restart the process with execute handler program's dentry.
1087          if (retval < 0) return retval;           */
1088          bprm->argc++;          filp = open_exec(ee->handler_path);
1089          retval = copy_strings_kernel(1, &alt_exec, bprm);          if (IS_ERR(filp)) {
1090          if (retval < 0) return retval;                  retval = PTR_ERR(filp);
1091          bprm->argc++;                  goto out;
1092  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)          }
1093          bprm->interp = alt_exec;          ee->obj.path1.dentry = filp->f_dentry;
1094            ee->obj.path1.mnt = filp->f_vfsmnt;
1095            bprm->file = filp;
1096            bprm->filename = ee->handler_path;
1097    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1098            bprm->interp = bprm->filename;
1099  #endif  #endif
1100          filp = open_exec(alt_exec);          retval = prepare_binprm(bprm);
1101          if (IS_ERR(filp)) return PTR_ERR(filp);          if (retval < 0)
1102          bprm->file= filp;                  goto out;
1103          bprm->filename = alt_exec;          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1104          return prepare_binprm(bprm);          retval = ccs_find_next_domain(ee);
1105            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1106     out:
1107            return retval;
1108  }  }
1109    
1110  #endif  /**
1111     * ccs_find_execute_handler - Find an execute handler.
1112     *
1113     * @ee:   Pointer to "struct ccs_execve".
1114     * @type: Type of execute handler.
1115     *
1116     * Returns true if found, false otherwise.
1117     *
1118     * Caller holds ccs_read_lock().
1119     */
1120    static bool ccs_find_execute_handler(struct ccs_execve *ee, const u8 type)
1121    {
1122            struct task_struct *task = current;
1123            const struct ccs_domain_info * const domain = ccs_current_domain();
1124            struct ccs_acl_info *ptr;
1125            bool found = false;
1126            /*
1127             * Don't use execute handler if the current process is
1128             * marked as execute handler to avoid infinite execute handler loop.
1129             */
1130            if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
1131                    return false;
1132            list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1133                    struct ccs_execute_handler *acl;
1134                    if (ptr->type != type)
1135                            continue;
1136                    acl = container_of(ptr, struct ccs_execute_handler, head);
1137                    ee->handler = acl->handler;
1138                    ee->handler_type = type;
1139                    found = true;
1140                    break;
1141            }
1142            return found;
1143    }
1144    
1145  int search_binary_handler_with_transition(struct linux_binprm *bprm, struct pt_regs *regs)  /**
1146     * ccs_dump_page - Dump a page to buffer.
1147     *
1148     * @bprm: Pointer to "struct linux_binprm".
1149     * @pos:  Location to dump.
1150     * @dump: Poiner to "struct ccs_page_dump".
1151     *
1152     * Returns true on success, false otherwise.
1153     */
1154    bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
1155                       struct ccs_page_dump *dump)
1156  {  {
1157          struct domain_info *next_domain = NULL, *prev_domain = current->domain_info;          struct page *page;
1158          int retval;          /* dump->data is released by ccs_finish_execve(). */
1159          char *alt_exec = NULL; /* Keep valid until search_binary_handler() finishes. */          if (!dump->data) {
1160  #if defined(CONFIG_SAKURA) || defined(CONFIG_TOMOYO)                  dump->data = kzalloc(PAGE_SIZE, CCS_GFP_FLAGS);
1161          extern void CCS_LoadPolicy(const char *filename);                  if (!dump->data)
1162          CCS_LoadPolicy(bprm->filename);                          return false;
1163            }
1164            /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
1165    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1166            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1167                    return false;
1168    #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1169            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1170                    return false;
1171    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1172            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1173                    return false;
1174    #else
1175            page = bprm->page[pos / PAGE_SIZE];
1176    #endif
1177            if (page != dump->page) {
1178                    const unsigned int offset = pos % PAGE_SIZE;
1179                    /*
1180                     * Maybe kmap()/kunmap() should be used here.
1181                     * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
1182                     * So do I.
1183                     */
1184                    char *kaddr = kmap_atomic(page, KM_USER0);
1185                    dump->page = page;
1186                    memcpy(dump->data + offset, kaddr + offset,
1187                           PAGE_SIZE - offset);
1188                    kunmap_atomic(kaddr, KM_USER0);
1189            }
1190            /* Same with put_arg_page(page) in fs/exec.c */
1191    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1192            put_page(page);
1193    #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1194            put_page(page);
1195    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1196            put_page(page);
1197  #endif  #endif
1198  #if defined(CONFIG_TOMOYO)          return true;
1199          retval = FindNextDomain(bprm, &next_domain, 1);  }
1200          if (retval == -EPERM && try_alt_exec(bprm, &alt_exec) >= 0) {  
1201                  current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  /**
1202                  retval = FindNextDomain(bprm, &next_domain, 0);   * ccs_start_execve - Prepare for execve() operation.
1203                  current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;   *
1204          }   * @bprm: Pointer to "struct linux_binprm".
1205          if (retval == 0) {   * @eep:  Pointer to "struct ccs_execve *".
1206                  current->domain_info = next_domain;   *
1207                  retval = CheckEnviron(bprm);   * Returns 0 on success, negative value otherwise.
1208                  current->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC;   */
1209                  if (!retval) retval = search_binary_handler(bprm, regs);  static int ccs_start_execve(struct linux_binprm *bprm,
1210                  current->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC;                              struct ccs_execve **eep)
1211                  if (retval < 0) current->domain_info = prev_domain;  {
1212            int retval;
1213            struct task_struct *task = current;
1214            struct ccs_execve *ee;
1215            *eep = NULL;
1216            ee = kzalloc(sizeof(*ee), CCS_GFP_FLAGS);
1217            if (!ee)
1218                    return -ENOMEM;
1219            ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
1220            if (!ee->tmp) {
1221                    kfree(ee);
1222                    return -ENOMEM;
1223            }
1224            ee->reader_idx = ccs_read_lock();
1225            /* ee->dump->data is allocated by ccs_dump_page(). */
1226            ee->previous_domain = task->ccs_domain_info;
1227            /* Clear manager flag. */
1228            task->ccs_flags &= ~CCS_TASK_IS_MANAGER;
1229            *eep = ee;
1230            ccs_init_request_info(&ee->r, CCS_MAC_FILE_EXECUTE);
1231            ee->r.ee = ee;
1232            ee->bprm = bprm;
1233            ee->r.obj = &ee->obj;
1234            ee->obj.path1.dentry = bprm->file->f_dentry;
1235            ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1236            /*
1237             * No need to call ccs_environ() for execute handler because envp[] is
1238             * moved to argv[].
1239             */
1240            if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER))
1241                    return ccs_try_alt_exec(ee);
1242            retval = ccs_find_next_domain(ee);
1243            if (retval == -EPERM) {
1244                    if (ccs_find_execute_handler(ee,
1245                                                 CCS_TYPE_DENIED_EXECUTE_HANDLER))
1246                            return ccs_try_alt_exec(ee);
1247          }          }
1248          ccs_free(alt_exec);          if (!retval)
1249                    retval = ccs_environ(ee);
1250            return retval;
1251    }
1252    
1253    /**
1254     * ccs_finish_execve - Clean up execve() operation.
1255     *
1256     * @retval: Return code of an execve() operation.
1257     * @ee:     Pointer to "struct ccs_execve".
1258     *
1259     * Caller holds ccs_read_lock().
1260     */
1261    static void ccs_finish_execve(int retval, struct ccs_execve *ee)
1262    {
1263            struct task_struct *task = current;
1264            if (!ee)
1265                    return;
1266            if (retval < 0) {
1267                    task->ccs_domain_info = ee->previous_domain;
1268                    /*
1269                     * Make task->ccs_domain_info visible to GC before changing
1270                     * task->ccs_flags .
1271                     */
1272                    smp_mb();
1273            } else {
1274                    /* Mark the current process as execute handler. */
1275                    if (ee->handler)
1276                            task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1277                    /* Mark the current process as normal process. */
1278                    else
1279                            task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1280            }
1281            /* Tell GC that I finished execve(). */
1282            task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1283            ccs_read_unlock(ee->reader_idx);
1284            kfree(ee->handler_path);
1285            kfree(ee->tmp);
1286            kfree(ee->dump.data);
1287            kfree(ee);
1288    }
1289    
1290    /**
1291     * ccs_may_transit - Check permission and do domain transition without execve().
1292     *
1293     * @domainname: Domainname to transit to.
1294     * @pathname: Pathname to check.
1295     *
1296     * Returns 0 on success, negative value otherwise.
1297     *
1298     * Caller holds ccs_read_lock().
1299     */
1300    int ccs_may_transit(const char *domainname, const char *pathname)
1301    {
1302            struct ccs_path_info name;
1303            struct ccs_request_info r;
1304            struct ccs_domain_info *domain;
1305            int error;
1306            bool domain_created = false;
1307            name.name = pathname;
1308            ccs_fill_path_info(&name);
1309            /* Check allow_transit permission. */
1310            ccs_init_request_info(&r, CCS_MAC_FILE_TRANSIT);
1311            error = ccs_path_permission(&r, CCS_TYPE_TRANSIT, &name);
1312            if (error)
1313                    return error;
1314            /* Check destination domain. */
1315            domain = ccs_find_domain(domainname);
1316            if (!domain && r.mode != CCS_CONFIG_ENFORCING &&
1317                strlen(domainname) < CCS_EXEC_TMPSIZE - 10) {
1318                    domain = ccs_assign_domain(domainname, r.profile);
1319                    if (domain)
1320                            domain_created = true;
1321            }
1322            if (domain) {
1323                    error = 0;
1324                    current->ccs_domain_info = domain;
1325                    if (domain_created)
1326                            ccs_audit_domain_creation_log();
1327            } else {
1328                    error = -ENOENT;
1329            }
1330            return error;
1331    }
1332    
1333    static int __ccs_search_binary_handler(struct linux_binprm *bprm,
1334                                           struct pt_regs *regs)
1335    {
1336            struct ccs_execve *ee;
1337            int retval;
1338            if (!ccs_policy_loaded)
1339                    ccsecurity_exports.load_policy(bprm->filename);
1340            retval = ccs_start_execve(bprm, &ee);
1341            if (!retval)
1342                    retval = search_binary_handler(bprm, regs);
1343            ccs_finish_execve(retval, ee);
1344          return retval;          return retval;
 #else  
         return search_binary_handler(bprm, regs);  
 #endif  
1345  }  }
1346    
1347  /***** TOMOYO Linux end. *****/  void __init ccs_domain_init(void)
1348    {
1349            ccsecurity_ops.search_binary_handler = __ccs_search_binary_handler;
1350    }

Legend:
Removed from v.851  
changed lines
  Added in v.3731

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