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

Subversion リポジトリの参照

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

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

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

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

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