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

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/ccs-patch/fs/tomoyo_domain.c revision 240 by kumaneko, Fri May 25 12:57:26 2007 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-2007  NTT DATA CORPORATION   * Version: 1.7.1-pre   2009/10/16
  *  
  * Version: 1.4.1-rc2   2007/05/25  
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 = { NULL, NULL, NULL, 0, 0, 0 };  struct ccs_domain_info ccs_kernel_domain;
   
 /* /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. *****/  
   
 struct alias_entry {  
         struct alias_entry *next;  
         const struct path_info *original_name;  
         const struct path_info *aliased_name;  
         int is_deleted;  
 };  
   
 /*************************  VARIABLES  *************************/  
   
 /* Domain creation lock. */  
 static DECLARE_MUTEX(new_domain_assign_lock);  
   
 /*************************  UTILITY FUNCTIONS  *************************/  
   
 int IsDomainDef(const unsigned char *buffer)  
 {  
         /* while (*buffer && (*buffer <= ' ' || *buffer >= 127)) buffer++; */  
         return strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN) == 0;  
 }  
29    
30  const char *GetLastName(const struct domain_info *domain)  /* The list for "struct ccs_domain_info". */
31  {  LIST_HEAD(ccs_domain_list);
         const char *cp0 = domain->domainname->name, *cp1;  
         if ((cp1 = strrchr(cp0, ' ')) != NULL) return cp1 + 1;  
         return cp0;  
 }  
   
 int ReadSelfDomain(struct io_buffer *head)  
 {  
         if (!head->read_eof) {  
                 io_printf(head, "%s", current->domain_info->domainname->name);  
                 head->read_eof = 1;  
         }  
         return 0;  
 }  
32    
33  int AddDomainACL(struct acl_info *ptr, struct domain_info *domain, struct acl_info *new_ptr)  /**
34     * ccs_audit_execute_handler_log - Audit execute_handler log.
35     *
36     * @ee:         Pointer to "struct ccs_execve_entry".
37     * @is_default: True if it is "execute_handler" log.
38     *
39     * Returns 0 on success, negative value otherwise.
40     */
41    static int ccs_audit_execute_handler_log(struct ccs_execve_entry *ee,
42                                             const bool is_default)
43  {  {
44          mb(); /* Instead of using spinlock. */          struct ccs_request_info *r = &ee->r;
45          if (!ptr) domain->first_acl_ptr = (struct acl_info *) new_ptr;          const char *handler = ee->handler->name;
46          else ptr->next = (struct acl_info *) new_ptr;          r->mode = ccs_get_mode(r->profile, CCS_MAC_FILE_EXECUTE);
47          UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY);          return ccs_write_audit_log(true, r, "%s %s\n",
48          return 0;                                     is_default ? CCS_KEYWORD_EXECUTE_HANDLER :
49                                       CCS_KEYWORD_DENIED_EXECUTE_HANDLER, handler);
50  }  }
51    
52  int DelDomainACL(struct acl_info *ptr)  /**
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          ptr->is_deleted = 1;          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;
 int TooManyDomainACL(struct domain_info * const domain) {  
         unsigned int count = 0;  
         struct acl_info *ptr;  
         for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) {  
                 if (!ptr->is_deleted) count++;  
         }  
         /* If there are so many entries, don't append if accept mode. */  
         if (count < CheckCCSFlags(CCS_TOMOYO_MAX_ACCEPT_ENTRY)) return 0;  
         if (!domain->quota_warned) {  
                 printk("TOMOYO-WARNING: Domain '%s' has so many ACLs to hold. Stopped auto-append mode.\n", domain->domainname->name);  
                 domain->quota_warned = 1;  
         }  
         return 1;  
66  }  }
67    
68    /* The list for "struct ccs_domain_initializer_entry". */
69    LIST_HEAD(ccs_domain_initializer_list);
70    
71  /*************************  DOMAIN INITIALIZER HANDLER  *************************/  /**
72     * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list.
73  static struct domain_initializer_entry *domain_initializer_list = NULL;   *
74     * @domainname: The name of domain. May be NULL.
75  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.
76  {   * @is_not:     True if it is "no_initialize_domain" entry.
77          struct domain_initializer_entry *new_entry, *ptr;   * @is_delete:  True if it is a delete request.
78          static DECLARE_MUTEX(lock);   *
79          const struct path_info *saved_program, *saved_domainname = NULL;   * Returns 0 on success, negative value otherwise.
80          int error = -ENOMEM;   */
81          int is_last_name = 0;  static int ccs_update_domain_initializer_entry(const char *domainname,
82          if (!IsCorrectPath(program, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */                                                 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;  
         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;  
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 ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;          if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
117          new_entry->domainname = saved_domainname;                  list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);
118          new_entry->program = saved_program;                  entry = NULL;
119          new_entry->is_not = is_not;                  error = 0;
         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;  
120          }          }
121          error = 0;          mutex_unlock(&ccs_policy_lock);
122   out:   out:
123          up(&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 domain_initializer_entry *ptr = head->read_var2;          struct list_head *pos;
141          if (!ptr) ptr = domain_initializer_list;          bool done = true;
142          while (ptr) {          list_for_each_cookie(pos, head->read_var2,
143                  head->read_var2 = ptr;                               &ccs_domain_initializer_list) {
144                  if (!ptr->is_deleted) {                  const char *no;
145                          if (ptr->domainname) {                  const char *from = "";
146                                  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;                  const char *domain = "";
147                          } else {                  struct ccs_domain_initializer_entry *ptr;
148                                  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 = 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) {
154                            from = " from ";
155                            domain = ptr->domainname->name;
156                  }                  }
157                  ptr = ptr->next;                  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 ptr ? -ENOMEM : 0;          return done;
164  }  }
165    
166  int AddDomainInitializerPolicy(char *data, const int is_not, const int is_delete, const int is_oldstyle)  /**
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, is_oldstyle);                  return ccs_update_domain_initializer_entry(cp + 6, data,
182          } else {                                                             is_not, is_delete);
                 return AddDomainInitializerEntry(NULL, data, is_not, is_delete, is_oldstyle);  
183          }          }
184            return ccs_update_domain_initializer_entry(NULL, data, is_not,
185                                                       is_delete);
186  }  }
187    
188  static int 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          int flag = 0;   * @domainname: The name of domain.
192          for (ptr = domain_initializer_list; ptr; ptr = ptr->next) {   * @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 struct domain_keeper_entry *domain_keeper_list = NULL;  
231    
232  static int AddDomainKeeperEntry(const char *domainname, const char *program, const int is_not, const int 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 DECLARE_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          int 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)
         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;  
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;
         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;  
281          }          }
282          error = 0;          mutex_unlock(&ccs_policy_lock);
283   out:   out:
284          up(&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 int is_not, const int 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 domain_keeper_entry *ptr = head->read_var2;          struct list_head *pos;
322          if (!ptr) ptr = domain_keeper_list;          bool done = true;
323          while (ptr) {          list_for_each_cookie(pos, head->read_var2,
324                  head->read_var2 = ptr;                               &ccs_domain_keeper_list) {
325                  if (!ptr->is_deleted) {                  struct ccs_domain_keeper_entry *ptr;
326                          if (ptr->program) {                  const char *no;
327                                  if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s from %s\n", ptr->is_not ? "no_" : "", ptr->program->name, ptr->domainname->name)) break;                  const char *from = "";
328                          } else {                  const char *program = "";
329                                  if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s\n", ptr->is_not ? "no_" : "", ptr->domainname->name)) break;                  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) {
334                            from = " from ";
335                            program = ptr->program->name;
336                  }                  }
337                  ptr = ptr->next;                  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 ptr ? -ENOMEM : 0;          return done;
344  }  }
345    
346  static int 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          int flag = 0;   * @domainname: The name of domain.
350          for (ptr = domain_keeper_list; ptr; ptr = ptr->next) {   * @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 struct alias_entry *alias_list = NULL;  
387    
388  static int AddAliasEntry(const char *original_name, const char *aliased_name, const int is_delete)  /**
389  {   * ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list.
390          struct alias_entry *new_entry, *ptr;   *
391          static DECLARE_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          down(&lock);   */
397          for (ptr = alias_list; ptr; ptr = ptr->next) {  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          mb(); /* Instead of using spinlock. */                  error = 0;
         if ((ptr = alias_list) != NULL) {  
                 while (ptr->next) ptr = ptr->next; ptr->next = new_entry;  
         } else {  
                 alias_list = new_entry;  
427          }          }
428          error = 0;          mutex_unlock(&ccs_policy_lock);
429   out:   out:
430          up(&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 alias_entry *ptr = head->read_var2;          struct list_head *pos;
448          if (!ptr) ptr = alias_list;          bool done = true;
449          while (ptr) {          list_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) {
450                  head->read_var2 = ptr;                  struct ccs_aggregator_entry *ptr;
451                  if (!ptr->is_deleted && io_printf(head, KEYWORD_ALIAS "%s %s\n", ptr->original_name->name, ptr->aliased_name->name)) break;                  ptr = list_entry(pos, struct ccs_aggregator_entry, list);
452                  ptr = ptr->next;                  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 ptr ? -ENOMEM : 0;          return done;
461  }  }
462    
463  int AddAliasPolicy(char *data, const int is_delete)  /**
464     * ccs_write_aggregator_policy - Write "struct ccs_aggregator_entry" list.
465     *
466     * @data:      String to parse.
467     * @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          char *cp = strchr(data, ' ');          char *w[2];
474          if (!cp) return -EINVAL;          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
475          *cp++ = '\0';                  return -EINVAL;
476          return AddAliasEntry(data, cp, is_delete);          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
477  }  }
478    
479  /*************************  DOMAIN AGGREGATOR HANDLER  *************************/  /* Domain create/delete handler. */
480    
481  static struct aggregator_entry *aggregator_list = NULL;  /**
482     * ccs_delete_domain - Delete a domain.
483  static int AddAggregatorEntry(const char *original_name, const char *aggregated_name, const int is_delete)   *
484     * @domainname: The name of domain.
485     *
486     * Returns 0.
487     */
488    int ccs_delete_domain(char *domainname)
489  {  {
490          struct aggregator_entry *new_entry, *ptr;          struct ccs_domain_info *domain;
491          static DECLARE_MUTEX(lock);          struct ccs_path_info name;
492          const struct path_info *saved_original_name, *saved_aggregated_name;          name.name = domainname;
493          int error = -ENOMEM;          ccs_fill_path_info(&name);
494          if (!IsCorrectPath(original_name, 1, 0, -1, __FUNCTION__) || !IsCorrectPath(aggregated_name, 1, -1, -1, __FUNCTION__)) return -EINVAL;          mutex_lock(&ccs_policy_lock);
495          if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aggregated_name = SaveName(aggregated_name)) == NULL) return -ENOMEM;          /* Is there an active domain? */
496          down(&lock);          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
497          for (ptr = aggregator_list; ptr; ptr = ptr->next) {                  /* Never delete ccs_kernel_domain */
498                  if (ptr->original_name == saved_original_name && ptr->aggregated_name == saved_aggregated_name) {                  if (domain == &ccs_kernel_domain)
499                          ptr->is_deleted = is_delete;                          continue;
500                          error = 0;                  if (domain->is_deleted ||
501                          goto out;                      ccs_pathcmp(domain->domainname, &name))
502                  }                          continue;
503          }                  domain->is_deleted = true;
504          if (is_delete) {                  break;
                 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;  
         mb(); /* Instead of using spinlock. */  
         if ((ptr = aggregator_list) != NULL) {  
                 while (ptr->next) ptr = ptr->next; ptr->next = new_entry;  
         } else {  
                 aggregator_list = new_entry;  
505          }          }
506          error = 0;          mutex_unlock(&ccs_policy_lock);
507   out:          return 0;
         up(&lock);  
         return error;  
508  }  }
509    
510  int ReadAggregatorPolicy(struct io_buffer *head)  /**
511     * ccs_find_or_assign_new_domain - Create a domain.
512     *
513     * @domainname: The name of domain.
514     * @profile:    Profile number to assign if the domain was newly created.
515     *
516     * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
517     */
518    struct ccs_domain_info *ccs_find_or_assign_new_domain(const char *domainname,
519                                                          const u8 profile)
520  {  {
521          struct aggregator_entry *ptr = head->read_var2;          struct ccs_domain_info *entry;
522          if (!ptr) ptr = aggregator_list;          struct ccs_domain_info *domain;
523          while (ptr) {          const struct ccs_path_info *saved_domainname;
524                  head->read_var2 = ptr;          bool found = false;
525                  if (!ptr->is_deleted && io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", ptr->original_name->name, ptr->aggregated_name->name)) break;  
526                  ptr = ptr->next;          if (!ccs_is_correct_domain(domainname))
527                    return NULL;
528            saved_domainname = ccs_get_name(domainname);
529            if (!saved_domainname)
530                    return NULL;
531            entry = kzalloc(sizeof(*entry), GFP_KERNEL);
532            mutex_lock(&ccs_policy_lock);
533            list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
534                    if (domain->is_deleted ||
535                        ccs_pathcmp(saved_domainname, domain->domainname))
536                            continue;
537                    found = true;
538                    break;
539          }          }
540          return ptr ? -ENOMEM : 0;          if (!found && ccs_memory_ok(entry, sizeof(*entry))) {
541                    INIT_LIST_HEAD(&entry->acl_info_list);
542                    entry->domainname = saved_domainname;
543                    saved_domainname = NULL;
544                    entry->profile = profile;
545                    list_add_tail_rcu(&entry->list, &ccs_domain_list);
546                    domain = entry;
547                    entry = NULL;
548                    found = true;
549            }
550            mutex_unlock(&ccs_policy_lock);
551            ccs_put_name(saved_domainname);
552            kfree(entry);
553            return found ? domain : NULL;
554  }  }
555    
556  int AddAggregatorPolicy(char *data, const int is_delete)  /**
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 *cp = strchr(data, ' ');          struct ccs_request_info *r = &ee->r;
568          if (!cp) return -EINVAL;          const struct ccs_path_info *handler = ee->handler;
569          *cp++ = '\0';          struct ccs_domain_info *domain = NULL;
570          return AddAggregatorEntry(data, cp, is_delete);          const char *old_domain_name = r->domain->domainname->name;
571  }          struct linux_binprm *bprm = ee->bprm;
572            const u32 ccs_flags = current->ccs_flags;
573  /*************************  DOMAIN DELETION HANDLER  *************************/          struct ccs_path_info rn = { }; /* real name */
574            struct ccs_path_info ln; /* last name */
575            int retval;
576            bool need_kfree = false;
577            ln.name = ccs_last_word(old_domain_name);
578            ccs_fill_path_info(&ln);
579     retry:
580            current->ccs_flags = ccs_flags;
581            r->cond = NULL;
582            if (need_kfree) {
583                    kfree(rn.name);
584                    need_kfree = false;
585            }
586    
587  /* #define DEBUG_DOMAIN_UNDELETE */          /* Get symlink's pathname of program. */
588            retval = ccs_symlink_path(bprm->filename, &rn);
589            if (retval < 0)
590                    goto out;
591            need_kfree = true;
592    
593  int DeleteDomain(char *domainname0)          if (handler) {
594  {                  if (ccs_pathcmp(&rn, handler)) {
595          struct domain_info *domain;                          /* Failed to verify execute handler. */
596          struct path_info domainname;                          static u8 counter = 20;
597          domainname.name = domainname0;                          if (counter) {
598          fill_path_info(&domainname);                                  counter--;
599          down(&new_domain_assign_lock);                                  printk(KERN_WARNING "Failed to verify: %s\n",
600  #ifdef DEBUG_DOMAIN_UNDELETE                                         handler->name);
601          printk("DeleteDomain %s\n", domainname0);                          }
602          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {                          goto out;
                 if (pathcmp(domain->domainname, &domainname)) continue;  
                 printk("List: %p %u\n", domain, domain->is_deleted);  
         }  
 #endif  
         /* Is there an active domain? */  
         for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { /* Never delete KERNEL_DOMAIN */  
                 if (domain->is_deleted || pathcmp(domain->domainname, &domainname)) continue;  
                 break;  
         }  
         if (domain) {  
                 struct domain_info *domain2;  
                 /* Mark already deleted domains as non undeletable. */  
                 for (domain2 = KERNEL_DOMAIN.next; domain2; domain2 = domain2->next) {  
                         if (!domain2->is_deleted || pathcmp(domain2->domainname, &domainname)) continue;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                         if (domain2->is_deleted != 255) printk("Marked %p as non undeletable\n", domain2);  
 #endif  
                         domain2->is_deleted = 255;  
603                  }                  }
604                  /* Delete and mark active domain as undeletable. */          } else {
605                  domain->is_deleted = 1;                  struct ccs_aggregator_entry *ptr;
606  #ifdef DEBUG_DOMAIN_UNDELETE                  /* Check 'aggregator' directive. */
607                  printk("Marked %p as undeletable\n", domain);                  list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {
608  #endif                          if (ptr->is_deleted ||
609                                !ccs_path_matches_pattern(&rn, ptr->original_name))
610                                    continue;
611                            kfree(rn.name);
612                            need_kfree = false;
613                            /* This is OK because it is read only. */
614                            rn = *ptr->aggregated_name;
615                            break;
616                    }
617    
618                    /* Check execute permission. */
619                    retval = ccs_exec_perm(r, &rn);
620                    if (retval == 1)
621                            goto retry;
622                    if (retval < 0)
623                            goto out;
624          }          }
         up(&new_domain_assign_lock);  
         return 0;  
 }  
625    
626  struct domain_info *UndeleteDomain(const char *domainname0)          /* Calculate domain to transit to. */
627  {          if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) {
628          struct domain_info *domain, *candidate_domain = NULL;                  /* Transit to the child of ccs_kernel_domain domain. */
629          struct path_info domainname;                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, ROOT_NAME " " "%s",
630          domainname.name = domainname0;                           rn.name);
631          fill_path_info(&domainname);          } else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) {
632          down(&new_domain_assign_lock);                  /*
633  #ifdef DEBUG_DOMAIN_UNDELETE                   * Needn't to transit from kernel domain before starting
634          printk("UndeleteDomain %s\n", domainname0);                   * /sbin/init. But transit from kernel domain if executing
635          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {                   * initializers because they might start before /sbin/init.
636                  if (pathcmp(domain->domainname, &domainname)) continue;                   */
637                  printk("List: %p %u\n", domain, domain->is_deleted);                  domain = r->domain;
638            } else if (ccs_is_domain_keeper(r->domain->domainname, &rn, &ln)) {
639                    /* Keep current domain. */
640                    domain = r->domain;
641            } else {
642                    /* Normal domain transition. */
643                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
644                             old_domain_name, rn.name);
645          }          }
646  #endif          if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10)
647          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {                  goto done;
648                  if (pathcmp(&domainname, domain->domainname)) continue;          domain = ccs_find_domain(ee->tmp);
649                  if (!domain->is_deleted) {          if (domain)
650                          /* This domain is active. I can't undelete. */                  goto done;
651                          candidate_domain = NULL;          if (r->mode == CCS_CONFIG_ENFORCING) {
652  #ifdef DEBUG_DOMAIN_UNDELETE                  int error = ccs_supervisor(r, "# wants to create domain\n"
653                          printk("%p is active. I can't undelete.\n", domain);                                             "%s\n", ee->tmp);
654  #endif                  if (error == 1)
655                          break;                          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) {
664                    printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n",
665                           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                  /* Is this domain undeletable? */          } else {
673                  if (domain->is_deleted == 1) candidate_domain = domain;                  retval = 0;
         }  
         if (candidate_domain) {  
                 candidate_domain->is_deleted = 0;  
 #ifdef DEBUG_DOMAIN_UNDELETE  
                 printk("%p was undeleted.\n", candidate_domain);  
 #endif  
674          }          }
675          up(&new_domain_assign_lock);   out:
676          return candidate_domain;          if (domain)
677                    r->domain = domain;
678            if (need_kfree)
679                    kfree(rn.name);
680            return retval;
681  }  }
682    
683  /*************************  DOMAIN TRANSITION HANDLER  *************************/  /**
684     * ccs_environ - Check permission for environment variable names.
685  struct domain_info *FindDomain(const char *domainname0)   *
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          struct domain_info *domain;          struct ccs_request_info *r = &ee->r;
693          static int first = 1;          struct linux_binprm *bprm = ee->bprm;
694          struct path_info domainname;          char *arg_ptr = ee->tmp;
695          domainname.name = domainname0;          int arg_len = 0;
696          fill_path_info(&domainname);          unsigned long pos = bprm->p;
697          if (first) {          int offset = pos % PAGE_SIZE;
698                  KERNEL_DOMAIN.domainname = SaveName(ROOT_NAME);          int argv_count = bprm->argc;
699                  first = 0;          int envp_count = bprm->envc;
700          }          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
701          for (domain = &KERNEL_DOMAIN; domain; domain = domain->next) {          int error = -ENOMEM;
702                  if (!domain->is_deleted && !pathcmp(&domainname, domain->domainname)) return domain;          if (!r->mode || !envp_count)
703          }                  return 0;
704          return NULL;          while (error == -ENOMEM) {
705  }                  if (!ccs_dump_page(bprm, pos, &ee->dump))
706                            goto out;
707  struct domain_info *FindOrAssignNewDomain(const char *domainname, const u8 profile)                  pos += PAGE_SIZE - offset;
708  {                  /* Read. */
709          struct domain_info *domain = NULL;                  while (argv_count && offset < PAGE_SIZE) {
710          const struct path_info *saved_domainname;                          const char *kaddr = ee->dump.data;
711          down(&new_domain_assign_lock);                          if (!kaddr[offset++])
712          if ((domain = FindDomain(domainname)) != NULL) goto out;                                  argv_count--;
713          if (!IsCorrectDomain(domainname, __FUNCTION__)) goto out;                  }
714          if ((saved_domainname = SaveName(domainname)) == NULL) goto out;                  if (argv_count) {
715          /* Can I reuse memory of deleted domain? */                          offset = 0;
716          for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) {                          continue;
717                  struct task_struct *p;                  }
718                  struct acl_info *ptr;                  while (offset < PAGE_SIZE) {
719                  int flag;                          const char *kaddr = ee->dump.data;
720                  if (!domain->is_deleted || domain->domainname != saved_domainname) continue;                          const unsigned char c = kaddr[offset++];
721                  flag = 0;                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
722                  /***** CRITICAL SECTION START *****/                                  if (c == '=') {
723                  read_lock(&tasklist_lock);                                          arg_ptr[arg_len++] = '\0';
724                  for_each_process(p) {                                  } else if (c == '\\') {
725                          if (p->domain_info == domain) { flag = 1; break; }                                          arg_ptr[arg_len++] = '\\';
726                  }                                          arg_ptr[arg_len++] = '\\';
727                  read_unlock(&tasklist_lock);                                  } else if (c > ' ' && c < 127) {
728                  /***** CRITICAL SECTION END *****/                                          arg_ptr[arg_len++] = c;
729                  if (flag) continue;                                  } else {
730  #ifdef DEBUG_DOMAIN_UNDELETE                                          arg_ptr[arg_len++] = '\\';
731                  printk("Reusing %p %s\n", domain, domain->domainname->name);                                          arg_ptr[arg_len++] = (c >> 6) + '0';
732  #endif                                          arg_ptr[arg_len++]
733                  for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) ptr->is_deleted = 1;                                                  = ((c >> 3) & 7) + '0';
734                  domain->profile = profile;                                          arg_ptr[arg_len++] = (c & 7) + '0';
735                  domain->quota_warned = 0;                                  }
736                  mb(); /* Instead of using spinlock. */                          } else {
737                  domain->is_deleted = 0;                                  arg_ptr[arg_len] = '\0';
738                  goto out;                          }
739                            if (c)
740                                    continue;
741                            if (ccs_env_perm(r, arg_ptr)) {
742                                    error = -EPERM;
743                                    break;
744                            }
745                            if (!--envp_count) {
746                                    error = 0;
747                                    break;
748                            }
749                            arg_len = 0;
750                    }
751                    offset = 0;
752          }          }
753          /* No memory reusable. Create using new memory. */   out:
754          if ((domain = alloc_element(sizeof(*domain))) != NULL) {          if (r->mode != 3)
755                  struct domain_info *ptr = &KERNEL_DOMAIN;                  error = 0;
756                  domain->domainname = saved_domainname;          return error;
                 domain->profile = profile;  
                 mb(); /* Instead of using spinlock. */  
                 while (ptr->next) ptr = ptr->next; ptr->next = domain;  
         }  
  out: ;  
         up(&new_domain_assign_lock);  
         return domain;  
757  }  }
758    
759  static int Escape(char *dest, const char *src, int dest_len)  /**
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          while (*src) {          unsigned char *src = dest;
769                  const unsigned char c = * (const unsigned char *) src;          unsigned char c;
770            unsigned char d;
771            unsigned char e;
772            while (1) {
773                    c = *src++;
774                    if (!c)
775                            break;
776                    if (c != '\\') {
777                            *dest++ = c;
778                            continue;
779                    }
780                    c = *src++;
781                  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;  
782                          *dest++ = c;                          *dest++ = c;
783                  } 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';  
784                  }                  }
785                  src++;                  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          }          }
         if (--dest_len <= 0) goto out;  
795          *dest = '\0';          *dest = '\0';
         return 0;  
  out:  
         return -ENOMEM;  
796  }  }
797    
798  static char *get_argv0(struct linux_binprm *bprm)  /**
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          if (bprm->argc > 0) {          int depth = 0;
809                  char *arg_ptr = ccs_alloc(PAGE_SIZE);          ccs_realpath_lock();
810                  int arg_len = 0;          for (;;) {
811                  const unsigned long pos = bprm->p;                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
812                  int i = pos / PAGE_SIZE, offset = pos % PAGE_SIZE;                          /* Global root? */
813                  if (!arg_ptr) goto out;                          if (vfsmnt->mnt_parent == vfsmnt)
814                  while (1) {                                  break;
815                          struct page *page = bprm->page[i];                          dentry = vfsmnt->mnt_mountpoint;
816                          const char *kaddr = kmap(page);                          vfsmnt = vfsmnt->mnt_parent;
817                          if (!kaddr) goto out;                          continue;
                         memmove(arg_ptr + arg_len, kaddr + offset, PAGE_SIZE - offset);  
                         kunmap(page);  
                         arg_len += PAGE_SIZE - offset;  
                         if (memchr(arg_ptr, '\0', arg_len)) break;  
                         {  
                                 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;  
818                  }                  }
819                  return arg_ptr;                  dentry = dentry->d_parent;
820          out:                  depth++;
821                  ccs_free(arg_ptr);          }
822          }          ccs_realpath_unlock();
823          return NULL;          return depth;
824  }  }
825    
826  static int FindNextDomain(struct linux_binprm *bprm, struct domain_info **next_domain)  /**
827  {   * ccs_get_root_depth - return the depth of root directory.
828          /* This function assumes that the size of buffer returned by realpath() = CCS_MAX_PATHNAME_LEN. */   *
829          struct domain_info *old_domain = current->domain_info, *domain = NULL;   * Returns number of directories to strip.
830          const char *old_domain_name = old_domain->domainname->name;   */
831          const char *original_name = bprm->filename;  static int ccs_get_root_depth(void)
832          struct file *filp = bprm->file;  {
833          char *new_domain_name = NULL;          int depth;
834          char *real_program_name = NULL, *symlink_program_name = NULL;          struct dentry *dentry;
835          const int is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_FILE);          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;
907          int retval;          int retval;
908          struct path_info r, s, l;          const int original_argc = bprm->argc;
909            const int original_envc = bprm->envc;
910            struct task_struct *task = current;
911    
912            /* Close the requested program's dentry. */
913            ee->obj.path1.dentry = NULL;
914            ee->obj.path1.mnt = NULL;
915            ee->obj.validate_done = false;
916            allow_write_access(bprm->file);
917            fput(bprm->file);
918            bprm->file = NULL;
919    
920            /* Invalidate page dump cache. */
921            ee->dump.page = NULL;
922    
923            /* 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                   * 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);
931                   */                  retval = copy_strings_kernel(1, &cp, bprm);
932                  static int first = 1;                  if (retval < 0)
933                  if (first) {                          goto out;
934                          AddDomainInitializerEntry(NULL, "/sbin/hotplug", 0, 0, 0);                  bprm->argc++;
                         AddDomainInitializerEntry(NULL, "/sbin/modprobe", 0, 0, 0);  
                         first = 0;  
                 }  
935          }          }
936    
937          /* Get realpath of program. */          /* Set argv[5] */
938          retval = -ENOENT; /* I hope realpath() won't fail with -ENOMEM. */          {
939          if ((real_program_name = realpath(original_name)) == NULL) goto out;                  char *cp = ee->tmp;
940          /* Get realpath of symbolic link. */                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
941          if ((symlink_program_name = realpath_nofollow(original_name)) == NULL) goto out;                  retval = copy_strings_kernel(1, &cp, bprm);
942                    if (retval < 0)
943          r.name = real_program_name;                          goto out;
944          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;  
                 }  
945          }          }
946            
947          /* Compare basename of real_program_name and argv[0] */          /* Set argv[4] */
948          if (bprm->argc > 0 && CheckCCSFlags(CCS_TOMOYO_MAC_FOR_ARGV0)) {          {
949                  char *org_argv0 = get_argv0(bprm);                  retval = copy_strings_kernel(1, &bprm->filename, bprm);
950                  retval = -ENOMEM;                  if (retval < 0)
951                  if (org_argv0) {                          goto out;
952                          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;  
953          }          }
954            
955          /* Check 'aggregator' directive. */          /* Set argv[3] */
956          {          {
957                  struct aggregator_entry *ptr;                  char *cp = ee->tmp;
958                  /* Is this program allowed to be aggregated? */                  const u32 ccs_flags = task->ccs_flags;
959                  for (ptr = aggregator_list; ptr; ptr = ptr->next) {                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
960                          if (ptr->is_deleted || !PathMatchesToPattern(&r, ptr->original_name)) continue;                           "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
961                          memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN);                           "sgid=%d fsuid=%d fsgid=%d state[0]=%u "
962                          strncpy(real_program_name, ptr->aggregated_name->name, CCS_MAX_PATHNAME_LEN - 1);                           "state[1]=%u state[2]=%u",
963                          fill_path_info(&r);                           (pid_t) sys_getpid(), current_uid(), current_gid(),
964                          break;                           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          /* Check execute permission. */          /* Set argv[2] */
975          if ((retval = CheckExecPerm(&r, filp)) < 0) goto out;          {
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          /* Allocate memory for calcurating domain name. */          /* Set argv[1] */
991          retval = -ENOMEM;          {
992          if ((new_domain_name = ccs_alloc(CCS_MAX_PATHNAME_LEN + 16)) == NULL) goto out;                  char *cp = ee->tmp;
993                            snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
994          if (IsDomainInitializer(old_domain->domainname, &r, &l)) {                           ccs_current_domain()->domainname->name);
995                  /* Transit to the child of KERNEL_DOMAIN domain. */                  retval = copy_strings_kernel(1, &cp, bprm);
996                  snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, ROOT_NAME " " "%s", real_program_name);                  if (retval < 0)
997          } else if (old_domain == &KERNEL_DOMAIN && !sbin_init_started) {                          goto out;
998                  /*                  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);  
999          }          }
1000          if (!domain && strlen(new_domain_name) < CCS_MAX_PATHNAME_LEN) {  
1001                  if (is_enforce) {          /* Set argv[0] */
1002                          domain = FindDomain(new_domain_name);          {
1003                          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();
1004                  } else {                  int len = ee->handler->total_len + 1;
1005                          domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile);                  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 (!domain) {  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
1031                  printk("TOMOYO-ERROR: Domain '%s' not defined.\n", new_domain_name);  #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
1032                  if (is_enforce) retval = -EPERM;          bprm->argv_len = bprm->exec - bprm->p;
1033          } else {  #endif
1034                  retval = 0;  #endif
1035    
1036            /* OK, now restart the process with execute handler program's dentry. */
1037            filp = open_exec(ee->handler_path);
1038            if (IS_ERR(filp)) {
1039                    retval = PTR_ERR(filp);
1040                    goto out;
1041          }          }
1042   out: ;          ee->obj.path1.dentry = filp->f_dentry;
1043          ccs_free(new_domain_name);          ee->obj.path1.mnt = filp->f_vfsmnt;
1044          ccs_free(real_program_name);          bprm->file = filp;
1045          ccs_free(symlink_program_name);          bprm->filename = ee->handler_path;
1046          *next_domain = domain ? domain : old_domain;  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1047            bprm->interp = bprm->filename;
1048    #endif
1049            retval = prepare_binprm(bprm);
1050            if (retval < 0)
1051                    goto out;
1052            task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1053            retval = ccs_find_next_domain(ee);
1054            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1055     out:
1056          return retval;          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  #if defined(CONFIG_SAKURA) || defined(CONFIG_TOMOYO)          if (!dump->data) {
1110          extern void CCS_LoadPolicy(const char *filename);                  dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);
1111          CCS_LoadPolicy(bprm->filename);                  if (!dump->data)
1112  #endif                          return false;
1113  #if defined(CONFIG_TOMOYO)          }
1114          retval = FindNextDomain(bprm, &next_domain);          /* 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  #else
1125          retval = 0; next_domain = prev_domain;          page = bprm->page[pos / PAGE_SIZE];
1126  #endif  #endif
1127          if (retval == 0) {          if (page != dump->page) {
1128                  current->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC;                  const unsigned int offset = pos % PAGE_SIZE;
1129                  current->domain_info = next_domain;                  /*
1130                  retval = search_binary_handler(bprm, regs);                   * Maybe kmap()/kunmap() should be used here.
1131                  if (retval < 0) current->domain_info = prev_domain;                   * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
1132                  current->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC;                   * So do I.
1133                     */
1134                    char *kaddr = kmap_atomic(page, KM_USER0);
1135                    dump->page = page;
1136                    memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset);
1137                    kunmap_atomic(kaddr, KM_USER0);
1138            }
1139            /* Same with put_arg_page(page) in fs/exec.c */
1140    #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     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;
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.240  
changed lines
  Added in v.3109

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