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

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 329 by kumaneko, Wed Aug 8 11:15:09 2007 UTC branches/ccs-patch/security/ccsecurity/domain.c revision 3747 by kumaneko, Wed Jun 9 13:58:57 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-2007  NTT DATA CORPORATION   * Version: 1.7.2+   2010/06/04
  *  
  * Version: 1.5.0-pre   2007/08/06  
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 = { NULL, NULL, NULL, 0, 0, 0 };  
   
 /* /sbin/init started? */  
 extern int sbin_init_started;  
   
 #ifdef CONFIG_TOMOYO  
   
 /* Lock for appending domain's ACL. */  
 DECLARE_MUTEX(domain_acl_lock);  
   
 /*************************  UTILITY FUNCTIONS  *************************/  
   
 /***** The structure for program files to force domain reconstruction. *****/  
   
 struct domain_initializer_entry {  
         struct domain_initializer_entry *next;  
         const struct path_info *domainname;    /* This may be NULL */  
         const struct path_info *program;  
         u8 is_deleted;  
         u8 is_not;  
         u8 is_last_name;  
         u8 is_oldstyle;  
 };  
   
 /***** The structure for domains to not to transit domains. *****/  
   
 struct domain_keeper_entry {  
         struct domain_keeper_entry *next;  
         const struct path_info *domainname;  
         const struct path_info *program;       /* This may be NULL */  
         u8 is_deleted;  
         u8 is_not;  
         u8 is_last_name;  
 };  
   
 /***** The structure for program files that should be aggregated. *****/  
   
 struct aggregator_entry {  
         struct aggregator_entry *next;  
         const struct path_info *original_name;  
         const struct path_info *aggregated_name;  
         int is_deleted;  
 };  
   
 /***** The structure for program files that should be aliased. *****/  
26    
27  struct alias_entry {  /* The global domain. */
28          struct alias_entry *next;  struct ccs_domain_info ccs_global_domain;
         const struct path_info *original_name;  
         const struct path_info *aliased_name;  
         int 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 DECLARE_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  int IsDomainDef(const unsigned char *buffer)  /**
41  {   * ccs_audit_execute_handler_log - Audit execute_handler log.
42          /* while (*buffer && (*buffer <= ' ' || *buffer >= 127)) buffer++; */   *
43          return strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN) == 0;   * @ee:         Pointer to "struct ccs_execve".
44  }   *
45     * Returns 0 on success, negative value otherwise.
46  const char *GetLastName(const struct domain_info *domain)   */
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 ReadSelfDomain(struct io_buffer *head)  /**
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          if (!head->read_eof) {          struct ccs_request_info r;
68                  io_printf(head, "%s", current->domain_info->domainname->name);          ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE);
69                  head->read_eof = 1;          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          return 0;          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 AddDomainACL(struct acl_info *ptr, struct domain_info *domain, struct acl_info *new_ptr)  int ccs_update_group(struct ccs_acl_head *new_entry, const int size,
101  {                       bool is_delete, struct ccs_group *group,
102          mb(); /* Instead of using spinlock. */                       bool (*check_duplicate) (const struct ccs_acl_head *,
103          if (!ptr) domain->first_acl_ptr = (struct acl_info *) new_ptr;                                                const struct ccs_acl_head *))
104          else ptr->next = (struct acl_info *) new_ptr;  {
105          UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);          int error = is_delete ? -ENOENT : -ENOMEM;
106          return 0;          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  int TooManyDomainACL(struct domain_info * const domain) {  int ccs_update_domain(struct ccs_acl_info *new_entry, const int size,
137          unsigned int count = 0;                        bool is_delete, struct ccs_domain_info *domain,
138          struct acl_info *ptr;                        bool (*check_duplicate) (const struct ccs_acl_info *,
139          for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) {                                                 const struct ccs_acl_info *),
140                  if (!ptr->is_deleted) count++;                        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 there are so many entries, don't append if learning mode. */          if (error && !is_delete) {
170          if (count < CheckCCSFlags(CCS_TOMOYO_MAX_ACCEPT_ENTRY)) return 0;                  entry = ccs_commit_ok(new_entry, size);
171          if (!domain->quota_warned) {                  if (entry) {
172                  printk("TOMOYO-WARNING: Domain '%s' has so many ACLs to hold. Stopped learning mode.\n", domain->domainname->name);                          if (exclusive)
173                  domain->quota_warned = 1;                                  ccs_delete_type(domain, type);
174                            ccs_add_domain_acl(domain, entry);
175                            error = 0;
176                    }
177          }          }
178          return 1;          mutex_unlock(&ccs_policy_lock);
179            return error;
180  }  }
181    
182    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->flags[CCS_DIF_IGNORE_GLOBAL] &&
199                (r->param_type != CCS_TYPE_PATH_ACL ||
200                 r->param.path.operation != CCS_TYPE_READ ||
201                 !domain->flags[CCS_DIF_IGNORE_GLOBAL_ALLOW_READ]) &&
202                (r->param_type != CCS_TYPE_ENV_ACL ||
203                 !domain->flags[CCS_DIF_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  /*************************  DOMAIN INITIALIZER HANDLER  *************************/  /**
223     * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer" list.
224  static struct domain_initializer_entry *domain_initializer_list = NULL;   *
225     * @domainname: The name of domain. May be NULL.
226  static int AddDomainInitializerEntry(const char *domainname, const char *program, const int is_not, const int is_delete, const int is_oldstyle)   * @program:    The name of program.
227  {   * @is_not:     True if it is "no_initialize_domain" entry.
228          struct domain_initializer_entry *new_entry, *ptr;   * @is_delete:  True if it is a delete request.
229          static DECLARE_MUTEX(lock);   *
230          const struct path_info *saved_program, *saved_domainname = NULL;   * Returns 0 on success, negative value otherwise.
231          int error = -ENOMEM;   */
232          int is_last_name = 0;  static int ccs_update_domain_initializer_entry(const char *domainname,
233          if (!IsCorrectPath(program, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */                                                 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;  
         down(&lock);  
         for (ptr = domain_initializer_list; ptr; ptr = ptr->next) {  
                 if (ptr->is_not == is_not && ptr->is_oldstyle == is_oldstyle && 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;  
         new_entry->is_oldstyle = is_oldstyle;  
         mb(); /* Instead of using spinlock. */  
         if ((ptr = domain_initializer_list) != NULL) {  
                 while (ptr->next) ptr = ptr->next; ptr->next = new_entry;  
         } else {  
                 domain_initializer_list = new_entry;  
         }  
         error = 0;  
257   out:   out:
258          up(&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 domain_initializer_entry *ptr = head->read_var2;   *
266          if (!ptr) ptr = domain_initializer_list;   * @data:      String to parse.
267          while (ptr) {   * @is_delete: True if it is a delete request.
268                  head->read_var2 = ptr;   *
269                  if (!ptr->is_deleted) {   * Returns 0 on success, negative value otherwise.
270                          if (ptr->domainname) {   */
271                                  if (io_printf(head, "%s%s%s from %s\n", ptr->is_not ? "no_" : "", ptr->is_oldstyle ? KEYWORD_INITIALIZER : KEYWORD_INITIALIZE_DOMAIN, ptr->program->name, ptr->domainname->name)) break;  int ccs_write_domain_initializer(char *data, const bool is_delete, const u8 flags)
                         } else {  
                                 if (io_printf(head, "%s%s%s\n", ptr->is_not ? "no_" : "", ptr->is_oldstyle ? KEYWORD_INITIALIZER : KEYWORD_INITIALIZE_DOMAIN, ptr->program->name)) break;  
                         }  
                 }  
                 ptr = ptr->next;  
         }  
         return ptr ? -ENOMEM : 0;  
 }  
   
 int AddDomainInitializerPolicy(char *data, const int is_not, const int is_delete, const int is_oldstyle)  
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, is_oldstyle);                  domainname += 6;
         } else {  
                 return AddDomainInitializerEntry(NULL, data, is_not, is_delete, is_oldstyle);  
277          }          }
278            return ccs_update_domain_initializer_entry(domainname, data, flags,
279                                                       is_delete);
280  }  }
281    
282  static int 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          int flag = 0;   * @domainname: The name of domain.
286          for (ptr = domain_initializer_list; ptr; ptr = ptr->next) {   * @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 struct domain_keeper_entry *domain_keeper_list = NULL;  
   
 static int AddDomainKeeperEntry(const char *domainname, const char *program, const int is_not, const int 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 DECLARE_MUTEX(lock);          const struct ccs_domain_keeper *p2 = container_of(b, typeof(*p2),
330          int error = -ENOMEM;                                                            head);
331          int 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)
         down(&lock);  
         for (ptr = domain_keeper_list; ptr; ptr = ptr->next) {  
                 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;  
         mb(); /* Instead of using spinlock. */  
         if ((ptr = domain_keeper_list) != NULL) {  
                 while (ptr->next) ptr = ptr->next; ptr->next = new_entry;  
         } else {  
                 domain_keeper_list = new_entry;  
         }  
         error = 0;  
370   out:   out:
371          up(&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 int is_not, const int 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 domain_keeper_entry *ptr = head->read_var2;   *
401          if (!ptr) ptr = domain_keeper_list;   * @domainname: The name of domain.
402          while (ptr) {   * @program:    The name of program.
403                  head->read_var2 = ptr;   * @last_name:  The last component of @domainname.
404                  if (!ptr->is_deleted) {   *
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)) break;   * false otherwise.
407                          } else {   *
408                                  if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s\n", ptr->is_not ? "no_" : "", ptr->domainname->name)) break;   * Caller holds ccs_read_lock().
409                          }   */
410                  }  static bool ccs_domain_keeper(const struct ccs_path_info *domainname,
411                  ptr = ptr->next;                                   const struct ccs_path_info *program,
412          }                                   const struct ccs_path_info *last_name)
413          return ptr ? -ENOMEM : 0;  {
414  }          struct ccs_domain_keeper *ptr;
415            bool flag = false;
416  static int IsDomainKeeper(const struct path_info *domainname, const struct path_info *program, const struct path_info *last_name)          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_DOMAIN_KEEPER],
417  {                                  head.list) {
418          struct domain_keeper_entry *ptr;                  if (ptr->head.is_deleted)
419          int flag = 0;                          continue;
         for (ptr = domain_keeper_list; ptr; ptr = ptr->next) {  
                 if (ptr->is_deleted) 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 && ccs_pathcmp(ptr->program, program))
428                            continue;
429                    if (ptr->is_not) {
430                            flag = false;
431                            break;
432                  }                  }
433                  if (ptr->program && pathcmp(ptr->program, program)) continue;                  flag = true;
                 if (ptr->is_not) return 0;  
                 flag = 1;  
434          }          }
435          return flag;          return flag;
436  }  }
437    
438  /*************************  SYMBOLIC LINKED PROGRAM HANDLER  *************************/  static bool ccs_same_aggregator_entry(const struct ccs_acl_head *a,
439                                          const struct ccs_acl_head *b)
 static struct alias_entry *alias_list = NULL;  
   
 static int AddAliasEntry(const char *original_name, const char *aliased_name, const int is_delete)  
440  {  {
441          struct alias_entry *new_entry, *ptr;          const struct ccs_aggregator *p1 = container_of(a, typeof(*p1), head);
442          static DECLARE_MUTEX(lock);          const struct ccs_aggregator *p2 = container_of(b, typeof(*p2), head);
443          const struct path_info *saved_original_name, *saved_aliased_name;          return p1->original_name == p2->original_name &&
444          int error = -ENOMEM;                  p1->aggregated_name == p2->aggregated_name;
445          if (!IsCorrectPath(original_name, 1, -1, -1, __FUNCTION__) || !IsCorrectPath(aliased_name, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */  }
446          if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aliased_name = SaveName(aliased_name)) == NULL) return -ENOMEM;  
447          down(&lock);  /**
448          for (ptr = alias_list; ptr; ptr = ptr->next) {   * ccs_update_aggregator_entry - Update "struct ccs_aggregator" list.
449                  if (ptr->original_name == saved_original_name && ptr->aliased_name == saved_aliased_name) {   *
450                          ptr->is_deleted = is_delete;   * @original_name:   The original program's name.
451                          error = 0;   * @aggregated_name: The aggregated program's name.
452                          goto out;   * @is_delete:       True if it is a delete request.
453                  }   *
454          }   * Returns 0 on success, negative value otherwise.
455          if (is_delete) {   */
456                  error = -ENOENT;  static int ccs_update_aggregator_entry(const char *original_name,
457                                           const char *aggregated_name,
458                                           const bool is_delete)
459    {
460            struct ccs_aggregator e = { };
461            int error = is_delete ? -ENOENT : -ENOMEM;
462            if (!ccs_correct_path(original_name) ||
463                !ccs_correct_path(aggregated_name))
464                    return -EINVAL;
465            e.original_name = ccs_get_name(original_name);
466            e.aggregated_name = ccs_get_name(aggregated_name);
467            if (!e.original_name || !e.aggregated_name ||
468                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->aliased_name = saved_aliased_name;  
         mb(); /* Instead of using spinlock. */  
         if ((ptr = alias_list) != NULL) {  
                 while (ptr->next) ptr = ptr->next; ptr->next = new_entry;  
         } else {  
                 alias_list = new_entry;  
         }  
         error = 0;  
473   out:   out:
474          up(&lock);          ccs_put_name(e.original_name);
475            ccs_put_name(e.aggregated_name);
476          return error;          return error;
477  }  }
478    
479  int ReadAliasPolicy(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 alias_entry *ptr = head->read_var2;          char *w[2];
490          if (!ptr) ptr = alias_list;          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
491          while (ptr) {                  return -EINVAL;
492                  head->read_var2 = ptr;          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
                 if (!ptr->is_deleted && io_printf(head, KEYWORD_ALIAS "%s %s\n", ptr->original_name->name, ptr->aliased_name->name)) break;  
                 ptr = ptr->next;  
         }  
         return ptr ? -ENOMEM : 0;  
493  }  }
494    
495  int AddAliasPolicy(char *data, const int 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 AddAliasEntry(data, cp, is_delete);          ccs_fill_path_info(&name);
510            if (mutex_lock_interruptible(&ccs_policy_lock))
511                    return 0;
512            /* Is there an active domain? */
513            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
514                    /* Never delete ccs_kernel_domain */
515                    if (domain == &ccs_kernel_domain)
516                            continue;
517                    if (domain->is_deleted ||
518                        ccs_pathcmp(domain->domainname, &name))
519                            continue;
520                    domain->is_deleted = true;
521                    break;
522            }
523            mutex_unlock(&ccs_policy_lock);
524            return 0;
525  }  }
526    
527  /*************************  DOMAIN AGGREGATOR HANDLER  *************************/  /**
528     * ccs_assign_domain - Create a domain.
529  static struct aggregator_entry *aggregator_list = NULL;   *
530     * @domainname: The name of domain.
531  static int AddAggregatorEntry(const char *original_name, const char *aggregated_name, const int is_delete)   * @profile:    Profile number to assign if the domain was newly created.
532     *
533     * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
534     */
535    struct ccs_domain_info *ccs_assign_domain(const char *domainname,
536                                              const u8 profile)
537  {  {
538          struct aggregator_entry *new_entry, *ptr;          struct ccs_domain_info e = { };
539          static DECLARE_MUTEX(lock);          struct ccs_domain_info *entry = NULL;
540          const struct path_info *saved_original_name, *saved_aggregated_name;          bool found = false;
541          int error = -ENOMEM;  
542          if (!IsCorrectPath(original_name, 1, 0, -1, __FUNCTION__) || !IsCorrectPath(aggregated_name, 1, -1, -1, __FUNCTION__)) return -EINVAL;          if (!ccs_correct_domain(domainname))
543          if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aggregated_name = SaveName(aggregated_name)) == NULL) return -ENOMEM;                  return NULL;
544          down(&lock);          e.profile = profile;
545          for (ptr = aggregator_list; ptr; ptr = ptr->next) {          e.domainname = ccs_get_name(domainname);
546                  if (ptr->original_name == saved_original_name && ptr->aggregated_name == saved_aggregated_name) {          if (!e.domainname)
547                          ptr->is_deleted = is_delete;                  return NULL;
548                          error = 0;          if (mutex_lock_interruptible(&ccs_policy_lock))
                         goto out;  
                 }  
         }  
         if (is_delete) {  
                 error = -ENOENT;  
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          if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;          if (!found) {
558          new_entry->original_name = saved_original_name;                  entry = ccs_commit_ok(&e, sizeof(e));
559          new_entry->aggregated_name = saved_aggregated_name;                  if (entry) {
560          mb(); /* Instead of using spinlock. */                          INIT_LIST_HEAD(&entry->acl_info_list);
561          if ((ptr = aggregator_list) != NULL) {                          list_add_tail_rcu(&entry->list, &ccs_domain_list);
562                  while (ptr->next) ptr = ptr->next; ptr->next = new_entry;                          found = true;
563          } else {                  }
                 aggregator_list = new_entry;  
564          }          }
565          error = 0;          mutex_unlock(&ccs_policy_lock);
566   out:   out:
567          up(&lock);          ccs_put_name(e.domainname);
568          return error;          return found ? entry : NULL;
569  }  }
570    
571  int ReadAggregatorPolicy(struct io_buffer *head)  /**
572     * ccs_find_next_domain - Find a domain.
573     *
574     * @ee: Pointer to "struct ccs_execve".
575     *
576     * Returns 0 on success, negative value otherwise.
577     *
578     * Caller holds ccs_read_lock().
579     */
580    static int ccs_find_next_domain(struct ccs_execve *ee)
581  {  {
582          struct aggregator_entry *ptr = head->read_var2;          struct ccs_request_info *r = &ee->r;
583          if (!ptr) ptr = aggregator_list;          const struct ccs_path_info *handler = ee->handler;
584          while (ptr) {          struct ccs_domain_info *domain = NULL;
585                  head->read_var2 = ptr;          struct ccs_domain_info * const old_domain = ccs_current_domain();
586                  if (!ptr->is_deleted && io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", ptr->original_name->name, ptr->aggregated_name->name)) break;          const char *old_domain_name = old_domain->domainname->name;
587                  ptr = ptr->next;          struct linux_binprm *bprm = ee->bprm;
588            struct task_struct *task = current;
589            const u32 ccs_flags = task->ccs_flags;
590            struct ccs_path_info rn = { }; /* real name */
591            struct ccs_path_info ln; /* last name */
592            int retval;
593            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     retry:
598            current->ccs_flags = ccs_flags;
599            r->cond = NULL;
600            if (need_kfree) {
601                    kfree(rn.name);
602                    need_kfree = false;
603          }          }
         return ptr ? -ENOMEM : 0;  
 }  
   
 int AddAggregatorPolicy(char *data, const int is_delete)  
 {  
         char *cp = strchr(data, ' ');  
         if (!cp) return -EINVAL;  
         *cp++ = '\0';  
         return AddAggregatorEntry(data, cp, is_delete);  
 }  
604    
605  /*************************  DOMAIN DELETION HANDLER  *************************/          /* Get symlink's pathname of program. */
606            retval = ccs_symlink_path(bprm->filename, &rn);
607            if (retval < 0)
608                    goto out;
609            need_kfree = true;
610    
611  /* #define DEBUG_DOMAIN_UNDELETE */          if (handler) {
612                    if (ccs_pathcmp(&rn, handler)) {
613                            /* Failed to verify execute handler. */
614                            static u8 counter = 20;
615                            if (counter) {
616                                    counter--;
617                                    printk(KERN_WARNING "Failed to verify: %s\n",
618                                           handler->name);
619                            }
620                            goto out;
621                    }
622            } else {
623                    struct ccs_aggregator *ptr;
624                    /* Check 'aggregator' directive. */
625                    list_for_each_entry_rcu(ptr,
626                                            &ccs_policy_list[CCS_ID_AGGREGATOR],
627                                            head.list) {
628                            if (ptr->head.is_deleted ||
629                                !ccs_path_matches_pattern(&rn, ptr->original_name))
630                                    continue;
631                            kfree(rn.name);
632                            need_kfree = false;
633                            /* This is OK because it is read only. */
634                            rn = *ptr->aggregated_name;
635                            break;
636                    }
637    
638  int DeleteDomain(char *domainname0)                  /* Check execute permission. */
639  {                  retval = ccs_path_permission(r, CCS_TYPE_EXECUTE, &rn);
640          struct domain_info *domain;                  if (retval == CCS_RETRY_REQUEST)
641          struct path_info domainname;                          goto retry;
642          domainname.name = domainname0;                  if (retval < 0)
643          fill_path_info(&domainname);                          goto out;
         down(&new_domain_assign_lock);  
 #ifdef DEBUG_DOMAIN_UNDELETE  
         printk("DeleteDomain %s\n", domainname0);  
         for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {  
                 if (pathcmp(domain->domainname, &domainname)) continue;  
                 printk("List: %p %u\n", domain, domain->is_deleted);  
644          }          }
645  #endif  
646          /* Is there an active domain? */          /* Calculate domain to transit to. */
647          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { /* Never delete KERNEL_DOMAIN */          if (ccs_domain_initializer(old_domain->domainname, &rn, &ln)) {
648                  if (domain->is_deleted || pathcmp(domain->domainname, &domainname)) continue;                  /* Transit to the child of ccs_kernel_domain domain. */
649                  break;                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, CCS_ROOT_NAME " " "%s",
650                             rn.name);
651            } else if (old_domain == &ccs_kernel_domain && !ccs_policy_loaded) {
652                    /*
653                     * Needn't to transit from kernel domain before starting
654                     * /sbin/init. But transit from kernel domain if executing
655                     * initializers because they might start before /sbin/init.
656                     */
657                    domain = old_domain;
658            } else if (ccs_domain_keeper(old_domain->domainname, &rn, &ln)) {
659                    /* Keep current domain. */
660                    domain = old_domain;
661            } else {
662                    /* Normal domain transition. */
663                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
664                             old_domain_name, rn.name);
665          }          }
666          if (domain) {          if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10)
667                  struct domain_info *domain2;                  goto done;
668                  /* Mark already deleted domains as non undeletable. */          domain = ccs_find_domain(ee->tmp);
669                  for (domain2 = KERNEL_DOMAIN.next; domain2; domain2 = domain2->next) {          if (domain)
670                          if (!domain2->is_deleted || pathcmp(domain2->domainname, &domainname)) continue;                  goto done;
671  #ifdef DEBUG_DOMAIN_UNDELETE          if (r->mode == CCS_CONFIG_ENFORCING) {
672                          if (domain2->is_deleted != 255) printk("Marked %p as non undeletable\n", domain2);                  int error = ccs_supervisor(r, "# wants to create domain\n"
673  #endif                                             "%s\n", ee->tmp);
674                          domain2->is_deleted = 255;                  if (error == CCS_RETRY_REQUEST)
675                  }                          goto retry;
676                  /* Delete and mark active domain as undeletable. */                  if (error < 0)
677                  domain->is_deleted = 1;                          goto done;
678  #ifdef DEBUG_DOMAIN_UNDELETE          }
679                  printk("Marked %p as undeletable\n", domain);          domain = ccs_assign_domain(ee->tmp, r->profile);
680  #endif          if (domain)
681                    domain_created = true;
682     done:
683            if (!domain) {
684                    retval = (r->mode == CCS_CONFIG_ENFORCING) ? -EPERM : 0;
685                    if (!old_domain->flags[CCS_DIF_TRANSITION_FAILED]) {
686                            old_domain->flags[CCS_DIF_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 {
693                    retval = 0;
694          }          }
695          up(&new_domain_assign_lock);          if (!retval && handler)
696          return 0;                  ccs_audit_execute_handler_log(ee);
697            /*
698             * Tell GC that I started execve().
699             * 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;
721  }  }
722    
723  struct domain_info *UndeleteDomain(const char *domainname0)  /**
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          struct domain_info *domain, *candidate_domain = NULL;          struct ccs_request_info *r = &ee->r;
733          struct path_info domainname;          struct linux_binprm *bprm = ee->bprm;
734          domainname.name = domainname0;          /* env_page->data is allocated by ccs_dump_page(). */
735          fill_path_info(&domainname);          struct ccs_page_dump env_page = { };
736          down(&new_domain_assign_lock);          char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
737  #ifdef DEBUG_DOMAIN_UNDELETE          int arg_len = 0;
738          printk("UndeleteDomain %s\n", domainname0);          unsigned long pos = bprm->p;
739          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {          int offset = pos % PAGE_SIZE;
740                  if (pathcmp(domain->domainname, &domainname)) continue;          int argv_count = bprm->argc;
741                  printk("List: %p %u\n", domain, domain->is_deleted);          int envp_count = bprm->envc;
742          }          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
743  #endif          int error = -ENOMEM;
744          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {          ee->r.type = CCS_MAC_ENVIRON;
745                  if (pathcmp(&domainname, domain->domainname)) continue;          ee->r.mode = ccs_get_mode(ccs_current_domain()->profile,
746                  if (!domain->is_deleted) {                                    CCS_MAC_ENVIRON);
747                          /* This domain is active. I can't undelete. */          if (!r->mode || !envp_count)
748                          candidate_domain = NULL;                  return 0;
749  #ifdef DEBUG_DOMAIN_UNDELETE          arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
750                          printk("%p is active. I can't undelete.\n", domain);          if (!arg_ptr)
 #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  
         }  
         up(&new_domain_assign_lock);  
         return candidate_domain;  
 }  
   
 /*************************  DOMAIN TRANSITION HANDLER  *************************/  
   
 struct domain_info *FindDomain(const char *domainname0)  
 {  
         struct domain_info *domain;  
         static int first = 1;  
         struct path_info domainname;  
         domainname.name = domainname0;  
         fill_path_info(&domainname);  
         if (first) {  
                 KERNEL_DOMAIN.domainname = SaveName(ROOT_NAME);  
                 first = 0;  
         }  
         for (domain = &KERNEL_DOMAIN; domain; domain = domain->next) {  
                 if (!domain->is_deleted && !pathcmp(&domainname, domain->domainname)) return domain;  
         }  
         return NULL;  
 }  
   
 struct domain_info *FindOrAssignNewDomain(const char *domainname, const u8 profile)  
 {  
         struct domain_info *domain = NULL;  
         const struct path_info *saved_domainname;  
         down(&new_domain_assign_lock);  
         if ((domain = FindDomain(domainname)) != NULL) goto out;  
         if (!IsCorrectDomain(domainname, __FUNCTION__)) goto out;  
         if ((saved_domainname = SaveName(domainname)) == NULL) goto out;  
         /* Can I reuse memory of deleted domain? */  
         for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {  
                 struct task_struct *p;  
                 struct acl_info *ptr;  
                 int flag;  
                 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  
                 for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) ptr->is_deleted = 1;  
                 domain->profile = profile;  
                 domain->quota_warned = 0;  
                 mb(); /* Instead of using spinlock. */  
                 domain->is_deleted = 0;  
751                  goto out;                  goto out;
752            while (error == -ENOMEM) {
753                    if (!ccs_dump_page(bprm, pos, &env_page))
754                            goto out;
755                    pos += PAGE_SIZE - offset;
756                    /* Read. */
757                    while (argv_count && offset < PAGE_SIZE) {
758                            if (!env_page.data[offset++])
759                                    argv_count--;
760                    }
761                    if (argv_count) {
762                            offset = 0;
763                            continue;
764                    }
765                    while (offset < PAGE_SIZE) {
766                            const unsigned char c = env_page.data[offset++];
767                            if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
768                                    if (c == '=') {
769                                            arg_ptr[arg_len++] = '\0';
770                                    } else if (c == '\\') {
771                                            arg_ptr[arg_len++] = '\\';
772                                            arg_ptr[arg_len++] = '\\';
773                                    } else if (c > ' ' && c < 127) {
774                                            arg_ptr[arg_len++] = c;
775                                    } else {
776                                            arg_ptr[arg_len++] = '\\';
777                                            arg_ptr[arg_len++] = (c >> 6) + '0';
778                                            arg_ptr[arg_len++]
779                                                    = ((c >> 3) & 7) + '0';
780                                            arg_ptr[arg_len++] = (c & 7) + '0';
781                                    }
782                            } else {
783                                    arg_ptr[arg_len] = '\0';
784                            }
785                            if (c)
786                                    continue;
787                            if (ccs_env_perm(r, arg_ptr)) {
788                                    error = -EPERM;
789                                    break;
790                            }
791                            if (!--envp_count) {
792                                    error = 0;
793                                    break;
794                            }
795                            arg_len = 0;
796                    }
797                    offset = 0;
798          }          }
799          /* No memory reusable. Create using new memory. */   out:
800          if ((domain = alloc_element(sizeof(*domain))) != NULL) {          if (r->mode != 3)
801                  struct domain_info *ptr = &KERNEL_DOMAIN;                  error = 0;
802                  domain->domainname = saved_domainname;          kfree(env_page.data);
803                  domain->profile = profile;          kfree(arg_ptr);
804                  mb(); /* Instead of using spinlock. */          return error;
                 while (ptr->next) ptr = ptr->next; ptr->next = domain;  
         }  
  out: ;  
         up(&new_domain_assign_lock);  
         return domain;  
805  }  }
806    
807  static int Escape(char *dest, const char *src, int dest_len)  /**
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          while (*src) {          unsigned char *src = dest;
817                  const unsigned char c = * (const unsigned char *) src;          unsigned char c;
818            unsigned char d;
819            unsigned char e;
820            while (1) {
821                    c = *src++;
822                    if (!c)
823                            break;
824                    if (c != '\\') {
825                            *dest++ = c;
826                            continue;
827                    }
828                    c = *src++;
829                  if (c == '\\') {                  if (c == '\\') {
                         dest_len -= 2;  
                         if (dest_len <= 0) goto out;  
                         *dest++ = '\\';  
                         *dest++ = '\\';  
                 } else if (c > ' ' && c < 127) {  
                         if (--dest_len <= 0) goto out;  
830                          *dest++ = c;                          *dest++ = c;
831                  } else {                          continue;
                         dest_len -= 4;  
                         if (dest_len <= 0) goto out;  
                         *dest++ = '\\';  
                         *dest++ = (c >> 6) + '0';  
                         *dest++ = ((c >> 3) & 7) + '0';  
                         *dest++ = (c & 7) + '0';  
832                  }                  }
833                  src++;                  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          }          }
         if (--dest_len <= 0) goto out;  
843          *dest = '\0';          *dest = '\0';
         return 0;  
  out:  
         return -ENOMEM;  
844  }  }
845    
846  static char *get_argv0(struct linux_binprm *bprm)  /**
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          char *arg_ptr = ccs_alloc(PAGE_SIZE); /* Initial buffer. */          int depth = 0;
857          int arg_len = 0;          ccs_realpath_lock();
858          unsigned long pos = bprm->p;          for (;;) {
859          int i = pos / PAGE_SIZE, offset = pos % PAGE_SIZE;                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
860          if (!bprm->argc || !arg_ptr) goto out;                          /* Global root? */
861          while (1) {                          if (vfsmnt->mnt_parent == vfsmnt)
862                  struct page *page;                                  break;
863  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)                          dentry = vfsmnt->mnt_mountpoint;
864                  if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) goto out;                          vfsmnt = vfsmnt->mnt_parent;
865  #else                          continue;
866                  page = bprm->page[i];                  }
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  #endif
887                  { /* Map and copy to kernel buffer and unmap. */          read_lock(&current->fs->lock);
888                          const char *kaddr = kmap(page);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
889                          if (!kaddr) { /* Mapping failed. */          root = current->fs->root;
890  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)          path_get(&current->fs->root);
891                                  put_page(page);          dentry = root.dentry;
892            vfsmnt = root.mnt;
893    #else
894            dentry = dget(current->fs->root);
895            vfsmnt = mntget(current->fs->rootmnt);
896  #endif  #endif
897                                  goto out;          read_unlock(&current->fs->lock);
898                          }          depth = ccs_root_depth(dentry, vfsmnt);
899                          memmove(arg_ptr + arg_len, kaddr + offset, PAGE_SIZE - offset);  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
900                          kunmap(page);          path_put(&root);
901                  }  #else
902  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)          dput(dentry);
903                  put_page(page);          mntput(vfsmnt);
                 pos += PAGE_SIZE - offset;  
904  #endif  #endif
905                  arg_len += PAGE_SIZE - offset;          return depth;
                 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;  
906  }  }
907    
908  static int FindNextDomain(struct linux_binprm *bprm, struct domain_info **next_domain)  /**
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          /* This function assumes that the size of buffer returned by realpath() = CCS_MAX_PATHNAME_LEN. */          /*
918          struct domain_info *old_domain = current->domain_info, *domain = NULL;           * Contents of modified bprm.
919          const char *old_domain_name = old_domain->domainname->name;           * The envp[] in original bprm is moved to argv[] so that
920          const char *original_name = bprm->filename;           * the alternatively executed program won't be affected by
921          struct file *filp = bprm->file;           * some dangerous environment variables like LD_PRELOAD.
922          char *new_domain_name = NULL;           *
923          char *real_program_name = NULL, *symlink_program_name = NULL;           * modified bprm->argc
924          const int is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_FILE);           *    = 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;
955          int retval;          int retval;
956          struct path_info r, s, l;          const int original_argc = bprm->argc;
957            const int original_envc = bprm->envc;
958            struct task_struct *task = current;
959    
960            /* Close the requested program's dentry. */
961            ee->obj.path1.dentry = NULL;
962            ee->obj.path1.mnt = NULL;
963            ee->obj.validate_done = false;
964            allow_write_access(bprm->file);
965            fput(bprm->file);
966            bprm->file = NULL;
967    
968            /* Invalidate page dump cache. */
969            ee->dump.page = NULL;
970    
971            /* 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                   * Built-in initializers. This is needed because policies are not loaded until starting /sbin/init .                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc);
979                   */                  retval = copy_strings_kernel(1, &cp, bprm);
980                  static int first = 1;                  if (retval < 0)
981                  if (first) {                          goto out;
982                          AddDomainInitializerEntry(NULL, "/sbin/hotplug", 0, 0, 0);                  bprm->argc++;
                         AddDomainInitializerEntry(NULL, "/sbin/modprobe", 0, 0, 0);  
                         first = 0;  
                 }  
983          }          }
984    
985          /* Get realpath of program. */          /* Set argv[5] */
986          retval = -ENOENT; /* I hope realpath() won't fail with -ENOMEM. */          {
987          if ((real_program_name = realpath(original_name)) == NULL) goto out;                  char *cp = ee->tmp;
988          /* Get realpath of symbolic link. */                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
989          if ((symlink_program_name = realpath_nofollow(original_name)) == NULL) goto out;                  retval = copy_strings_kernel(1, &cp, bprm);
990                    if (retval < 0)
991          r.name = real_program_name;                          goto out;
992          fill_path_info(&r);                  bprm->argc++;
         s.name = symlink_program_name;  
         fill_path_info(&s);  
         if ((l.name = strrchr(old_domain_name, ' ')) != NULL) l.name++;  
         else l.name = old_domain_name;  
         fill_path_info(&l);  
   
         /* Check 'alias' directive. */  
         if (pathcmp(&r, &s)) {  
                 struct alias_entry *ptr;  
                 /* Is this program allowed to be called via symbolic links? */  
                 for (ptr = alias_list; ptr; ptr = ptr->next) {  
                         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;  
                 }  
993          }          }
994            
995          /* Compare basename of real_program_name and argv[0] */          /* Set argv[4] */
996          if (bprm->argc > 0 && CheckCCSFlags(CCS_TOMOYO_MAC_FOR_ARGV0)) {          {
997                  char *org_argv0 = get_argv0(bprm);                  retval = copy_strings_kernel(1, &bprm->filename, bprm);
998                  retval = -ENOMEM;                  if (retval < 0)
999                  if (org_argv0) {                          goto out;
1000                          const int len = strlen(org_argv0);                  bprm->argc++;
                         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;  
                         }  
                         ccs_free(printable_argv0);  
                         ccs_free(org_argv0);  
                 }  
                 if (retval) goto out;  
1001          }          }
1002            
1003          /* Check 'aggregator' directive. */          /* Set argv[3] */
1004          {          {
1005                  struct aggregator_entry *ptr;                  char *cp = ee->tmp;
1006                  /* Is this program allowed to be aggregated? */                  const u32 ccs_flags = task->ccs_flags;
1007                  for (ptr = aggregator_list; ptr; ptr = ptr->next) {                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
1008                          if (ptr->is_deleted || !PathMatchesToPattern(&r, ptr->original_name)) continue;                           "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
1009                          memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN);                           "sgid=%d fsuid=%d fsgid=%d state[0]=%u "
1010                          strncpy(real_program_name, ptr->aggregated_name->name, CCS_MAX_PATHNAME_LEN - 1);                           "state[1]=%u state[2]=%u",
1011                          fill_path_info(&r);                           (pid_t) ccsecurity_exports.sys_getpid(),
1012                          break;                           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          /* Check execute permission. */          /* Set argv[2] */
1024          if ((retval = CheckExecPerm(&r, filp)) < 0) goto out;          {
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          /* Allocate memory for calcurating domain name. */          /* Set argv[1] */
1040          retval = -ENOMEM;          {
1041          if ((new_domain_name = ccs_alloc(CCS_MAX_PATHNAME_LEN + 16)) == NULL) goto out;                  char *cp = ee->tmp;
1042                            snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
1043          if (IsDomainInitializer(old_domain->domainname, &r, &l)) {                           ccs_current_domain()->domainname->name);
1044                  /* Transit to the child of KERNEL_DOMAIN domain. */                  retval = copy_strings_kernel(1, &cp, bprm);
1045                  snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, ROOT_NAME " " "%s", real_program_name);                  if (retval < 0)
1046          } else if (old_domain == &KERNEL_DOMAIN && !sbin_init_started) {                          goto out;
1047                  /*                  bprm->argc++;
                  * Needn't to transit from kernel domain before starting /sbin/init .  
                  * But transit from kernel domain if executing initializers, for they might start before /sbin/init .  
                  */  
                 domain = old_domain;  
         } else if (IsDomainKeeper(old_domain->domainname, &r, &l)) {  
                 /* Keep current domain. */  
                 domain = old_domain;  
         } else {  
                 /* Normal domain transition. */  
                 snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, "%s %s", old_domain_name, real_program_name);  
1048          }          }
1049          if (!domain && strlen(new_domain_name) < CCS_MAX_PATHNAME_LEN) {  
1050                  if (is_enforce) {          /* Set argv[0] */
1051                          domain = FindDomain(new_domain_name);          {
1052                          if (!domain) if (CheckSupervisor("#Need to create domain\n%s\n", new_domain_name) == 0) domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile);                  int depth = ccs_get_root_depth();
1053                  } else {                  int len = ee->handler->total_len + 1;
1054                          domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile);                  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 (!domain) {  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
1080                  printk("TOMOYO-ERROR: Domain '%s' not defined.\n", new_domain_name);  #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
1081                  if (is_enforce) retval = -EPERM;          bprm->argv_len = bprm->exec - bprm->p;
1082          } else {  #endif
1083                  retval = 0;  #endif
1084    
1085            /*
1086             * OK, now restart the process with execute handler program's dentry.
1087             */
1088            filp = open_exec(ee->handler_path);
1089            if (IS_ERR(filp)) {
1090                    retval = PTR_ERR(filp);
1091                    goto out;
1092          }          }
1093   out: ;          ee->obj.path1.dentry = filp->f_dentry;
1094          ccs_free(new_domain_name);          ee->obj.path1.mnt = filp->f_vfsmnt;
1095          ccs_free(real_program_name);          bprm->file = filp;
1096          ccs_free(symlink_program_name);          bprm->filename = ee->handler_path;
1097          *next_domain = domain ? domain : old_domain;  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1098            bprm->interp = bprm->filename;
1099    #endif
1100            retval = prepare_binprm(bprm);
1101            if (retval < 0)
1102                    goto out;
1103            task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1104            retval = ccs_find_next_domain(ee);
1105            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1106     out:
1107          return retval;          return retval;
1108  }  }
1109    
1110    /**
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 ccs_request_info *r = &ee->r;
1123            const struct ccs_domain_info *domain = ccs_current_domain();
1124            struct ccs_acl_info *ptr;
1125            /*
1126             * Don't use execute handler if the current process is
1127             * marked as execute handler to avoid infinite execute handler loop.
1128             */
1129            if (current->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
1130                    return false;
1131     retry:
1132            list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1133                    struct ccs_execute_handler *acl;
1134                    if (ptr->type != type || !ccs_condition(r, ptr->cond))
1135                            continue;
1136                    acl = container_of(ptr, struct ccs_execute_handler, head);
1137                    ee->handler = acl->handler;
1138                    ee->handler_type = type;
1139                    r->cond = ptr->cond;
1140                    return true;
1141            }
1142            if (domain != &ccs_global_domain && !domain->flags[CCS_DIF_IGNORE_GLOBAL]) {
1143                    domain = &ccs_global_domain;
1144                    goto retry;
1145            }
1146            return false;
1147    }
1148    
1149    #ifdef CONFIG_MMU
1150    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
1151    #define CCS_BPRM_MMU
1152    #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3
1153    #define CCS_BPRM_MMU
1154    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2
1155    #define CCS_BPRM_MMU
1156    #endif
1157  #endif  #endif
1158    
1159  int search_binary_handler_with_transition(struct linux_binprm *bprm, struct pt_regs *regs)  /**
1160     * ccs_dump_page - Dump a page to buffer.
1161     *
1162     * @bprm: Pointer to "struct linux_binprm".
1163     * @pos:  Location to dump.
1164     * @dump: Poiner to "struct ccs_page_dump".
1165     *
1166     * Returns true on success, false otherwise.
1167     */
1168    bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
1169                       struct ccs_page_dump *dump)
1170  {  {
1171          struct domain_info *next_domain = NULL, *prev_domain = current->domain_info;          struct page *page;
1172          int retval;          /* dump->data is released by ccs_finish_execve(). */
1173  #if defined(CONFIG_SAKURA) || defined(CONFIG_TOMOYO)          if (!dump->data) {
1174          extern void CCS_LoadPolicy(const char *filename);                  dump->data = kzalloc(PAGE_SIZE, CCS_GFP_FLAGS);
1175          CCS_LoadPolicy(bprm->filename);                  if (!dump->data)
1176  #endif                          return false;
1177  #if defined(CONFIG_TOMOYO)          }
1178          retval = FindNextDomain(bprm, &next_domain);          /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
1179    #ifdef CCS_BPRM_MMU
1180            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1181                    return false;
1182  #else  #else
1183          retval = 0; next_domain = prev_domain;          page = bprm->page[pos / PAGE_SIZE];
1184  #endif  #endif
1185          if (retval == 0) {          if (page != dump->page) {
1186                  current->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC;                  const unsigned int offset = pos % PAGE_SIZE;
1187                  current->domain_info = next_domain;                  /*
1188                  retval = search_binary_handler(bprm, regs);                   * Maybe kmap()/kunmap() should be used here.
1189                  if (retval < 0) current->domain_info = prev_domain;                   * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
1190                  current->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC;                   * So do I.
1191                     */
1192                    char *kaddr = kmap_atomic(page, KM_USER0);
1193                    dump->page = page;
1194                    memcpy(dump->data + offset, kaddr + offset,
1195                           PAGE_SIZE - offset);
1196                    kunmap_atomic(kaddr, KM_USER0);
1197            }
1198            /* Same with put_arg_page(page) in fs/exec.c */
1199    #ifdef CCS_BPRM_MMU
1200            put_page(page);
1201    #endif
1202            return true;
1203    }
1204    
1205    /**
1206     * ccs_start_execve - Prepare for execve() operation.
1207     *
1208     * @bprm: Pointer to "struct linux_binprm".
1209     * @eep:  Pointer to "struct ccs_execve *".
1210     *
1211     * Returns 0 on success, negative value otherwise.
1212     */
1213    static int ccs_start_execve(struct linux_binprm *bprm,
1214                                struct ccs_execve **eep)
1215    {
1216            int retval;
1217            struct task_struct *task = current;
1218            struct ccs_execve *ee;
1219            *eep = NULL;
1220            ee = kzalloc(sizeof(*ee), CCS_GFP_FLAGS);
1221            if (!ee)
1222                    return -ENOMEM;
1223            ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
1224            if (!ee->tmp) {
1225                    kfree(ee);
1226                    return -ENOMEM;
1227            }
1228            ee->reader_idx = ccs_read_lock();
1229            /* ee->dump->data is allocated by ccs_dump_page(). */
1230            ee->previous_domain = task->ccs_domain_info;
1231            /* Clear manager flag. */
1232            task->ccs_flags &= ~CCS_TASK_IS_MANAGER;
1233            *eep = ee;
1234            ccs_init_request_info(&ee->r, CCS_MAC_FILE_EXECUTE);
1235            ee->r.ee = ee;
1236            ee->bprm = bprm;
1237            ee->r.obj = &ee->obj;
1238            ee->obj.path1.dentry = bprm->file->f_dentry;
1239            ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1240            /*
1241             * No need to call ccs_environ() for execute handler because envp[] is
1242             * moved to argv[].
1243             */
1244            if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER))
1245                    return ccs_try_alt_exec(ee);
1246            retval = ccs_find_next_domain(ee);
1247            if (retval == -EPERM) {
1248                    if (ccs_find_execute_handler(ee,
1249                                                 CCS_TYPE_DENIED_EXECUTE_HANDLER))
1250                            return ccs_try_alt_exec(ee);
1251          }          }
1252            if (!retval)
1253                    retval = ccs_environ(ee);
1254          return retval;          return retval;
1255  }  }
1256    
1257  /***** TOMOYO Linux end. *****/  /**
1258     * ccs_finish_execve - Clean up execve() operation.
1259     *
1260     * @retval: Return code of an execve() operation.
1261     * @ee:     Pointer to "struct ccs_execve".
1262     *
1263     * Caller holds ccs_read_lock().
1264     */
1265    static void ccs_finish_execve(int retval, struct ccs_execve *ee)
1266    {
1267            struct task_struct *task = current;
1268            if (!ee)
1269                    return;
1270            if (retval < 0) {
1271                    task->ccs_domain_info = ee->previous_domain;
1272                    /*
1273                     * Make task->ccs_domain_info visible to GC before changing
1274                     * task->ccs_flags .
1275                     */
1276                    smp_mb();
1277            } else {
1278                    /* Mark the current process as execute handler. */
1279                    if (ee->handler)
1280                            task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1281                    /* Mark the current process as normal process. */
1282                    else
1283                            task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1284            }
1285            /* Tell GC that I finished execve(). */
1286            task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1287            ccs_read_unlock(ee->reader_idx);
1288            kfree(ee->handler_path);
1289            kfree(ee->tmp);
1290            kfree(ee->dump.data);
1291            kfree(ee);
1292    }
1293    
1294    /**
1295     * ccs_may_transit - Check permission and do domain transition without execve().
1296     *
1297     * @domainname: Domainname to transit to.
1298     * @pathname: Pathname to check.
1299     *
1300     * Returns 0 on success, negative value otherwise.
1301     *
1302     * Caller holds ccs_read_lock().
1303     */
1304    int ccs_may_transit(const char *domainname, const char *pathname)
1305    {
1306            struct ccs_path_info name;
1307            struct ccs_request_info r;
1308            struct ccs_domain_info *domain;
1309            int error;
1310            bool domain_created = false;
1311            name.name = pathname;
1312            ccs_fill_path_info(&name);
1313            /* Check allow_transit permission. */
1314            ccs_init_request_info(&r, CCS_MAC_FILE_TRANSIT);
1315            error = ccs_path_permission(&r, CCS_TYPE_TRANSIT, &name);
1316            if (error)
1317                    return error;
1318            /* Check destination domain. */
1319            domain = ccs_find_domain(domainname);
1320            if (!domain && r.mode != CCS_CONFIG_ENFORCING &&
1321                strlen(domainname) < CCS_EXEC_TMPSIZE - 10) {
1322                    domain = ccs_assign_domain(domainname, r.profile);
1323                    if (domain)
1324                            domain_created = true;
1325            }
1326            if (domain) {
1327                    error = 0;
1328                    current->ccs_domain_info = domain;
1329                    if (domain_created)
1330                            ccs_audit_domain_creation_log();
1331            } else {
1332                    error = -ENOENT;
1333            }
1334            return error;
1335    }
1336    
1337    static int __ccs_search_binary_handler(struct linux_binprm *bprm,
1338                                           struct pt_regs *regs)
1339    {
1340            struct ccs_execve *ee;
1341            int retval;
1342            if (!ccs_policy_loaded)
1343                    ccsecurity_exports.load_policy(bprm->filename);
1344            retval = ccs_start_execve(bprm, &ee);
1345            if (!retval)
1346                    retval = search_binary_handler(bprm, regs);
1347            ccs_finish_execve(retval, ee);
1348            return retval;
1349    }
1350    
1351    void __init ccs_domain_init(void)
1352    {
1353            ccsecurity_ops.search_binary_handler = __ccs_search_binary_handler;
1354    }

Legend:
Removed from v.329  
changed lines
  Added in v.3747

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