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

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 987 by kumaneko, Thu Feb 14 08:30:47 2008 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/domain.c revision 3258 by kumaneko, Tue Dec 15 12:46:47 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   2009/11/11
  *  
  * Version: 1.6.0-pre   2008/02/14  
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);
387    
388  static LIST1_HEAD(alias_list);  /**
389     * ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list.
390  static int AddAliasEntry(const char *original_name, const char *aliased_name, const bool is_delete)   *
391  {   * @original_name:   The original program's name.
392          struct alias_entry *new_entry, *ptr;   * @aggregated_name: The aggregated program's name.
393          static DEFINE_MUTEX(lock);   * @is_delete:       True if it is a delete request.
394          const struct path_info *saved_original_name, *saved_aliased_name;   *
395          int error = -ENOMEM;   * Returns 0 on success, negative value otherwise.
396          if (!IsCorrectPath(original_name, 1, -1, -1, __FUNCTION__) || !IsCorrectPath(aliased_name, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */   */
397          if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aliased_name = SaveName(aliased_name)) == NULL) return -ENOMEM;  static int ccs_update_aggregator_entry(const char *original_name,
398          mutex_lock(&lock);                                         const char *aggregated_name,
399          list1_for_each_entry(ptr, &alias_list, list) {                                         const bool is_delete)
400                  if (ptr->original_name == saved_original_name && ptr->aliased_name == saved_aliased_name) {  {
401                          ptr->is_deleted = is_delete;          struct ccs_aggregator_entry *entry = NULL;
402                          error = 0;          struct ccs_aggregator_entry *ptr;
403                          goto out;          struct ccs_aggregator_entry e = { };
404                  }          int error = is_delete ? -ENOENT : -ENOMEM;
405          }          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||
406          if (is_delete) {              !ccs_is_correct_path(aggregated_name, 1, -1, -1))
407                  error = -ENOENT;                  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 (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
424                    list_add_tail_rcu(&entry->list, &ccs_aggregator_list);
425                    entry = NULL;
426                    error = 0;
427          }          }
428          if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out;          mutex_unlock(&ccs_policy_lock);
         new_entry->original_name = saved_original_name;  
         new_entry->aliased_name = saved_aliased_name;  
         list1_add_tail_mb(&new_entry->list, &alias_list);  
         error = 0;  
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;
461  }  }
462    
463  int AddAliasPolicy(char *data, const bool is_delete)  /**
464  {   * ccs_write_aggregator_policy - Write "struct ccs_aggregator_entry" list.
465          char *cp = strchr(data, ' ');   *
466          if (!cp) return -EINVAL;   * @data:      String to parse.
467          *cp++ = '\0';   * @is_delete: True if it is a delete request.
468          return AddAliasEntry(data, cp, is_delete);   *
469  }   * Returns 0 on success, negative value otherwise.
470     */
471  /*************************  DOMAIN AGGREGATOR HANDLER  *************************/  int ccs_write_aggregator_policy(char *data, const bool is_delete)
   
 static LIST1_HEAD(aggregator_list);  
   
 static int AddAggregatorEntry(const char *original_name, const char *aggregated_name, 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)  
520  {  {
521          struct domain_info *domain = NULL;          struct ccs_domain_info *entry;
522          const struct path_info *saved_domainname;          struct ccs_domain_info *domain;
523          mutex_lock(&new_domain_assign_lock);          const struct ccs_path_info *saved_domainname;
524          if ((domain = FindDomain(domainname)) != NULL) goto out;          bool found = false;
525          if (!IsCorrectDomain(domainname, __FUNCTION__)) goto out;  
526          if ((saved_domainname = SaveName(domainname)) == NULL) goto out;          if (!ccs_is_correct_domain(domainname))
527          /* Can I reuse memory of deleted domain? */                  return NULL;
528          list1_for_each_entry(domain, &domain_list, list) {          saved_domainname = ccs_get_name(domainname);
529                  struct task_struct *p;          if (!saved_domainname)
530                  struct acl_info *ptr;                  return NULL;
531                  bool flag;          entry = kzalloc(sizeof(*entry), GFP_KERNEL);
532                  if (!domain->is_deleted || domain->domainname != saved_domainname) continue;          mutex_lock(&ccs_policy_lock);
533                  flag = 0;          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
534                  /***** CRITICAL SECTION START *****/                  if (domain->is_deleted ||
535                  read_lock(&tasklist_lock);                      ccs_pathcmp(saved_domainname, domain->domainname))
536                  for_each_process(p) {                          continue;
537                          if (p->domain_info == domain) { flag = 1; break; }                  found = true;
538                  }                  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;  
539          }          }
540          /* No memory reusable. Create using new memory. */          if (!found && ccs_memory_ok(entry, sizeof(*entry))) {
541          if ((domain = alloc_element(sizeof(*domain))) != NULL) {                  INIT_LIST_HEAD(&entry->acl_info_list);
542                  INIT_LIST1_HEAD(&domain->acl_info_list);                  entry->domainname = saved_domainname;
543                  domain->domainname = saved_domainname;                  saved_domainname = NULL;
544                  domain->profile = profile;                  entry->profile = profile;
545                  list1_add_tail_mb(&domain->list, &domain_list);                  list_add_tail_rcu(&entry->list, &ccs_domain_list);
546          }                  domain = entry;
547   out: ;                  entry = NULL;
548          mutex_unlock(&new_domain_assign_lock);                  found = true;
549          return domain;          }
550            mutex_unlock(&ccs_policy_lock);
551            ccs_put_name(saved_domainname);
552            kfree(entry);
553            return found ? domain : NULL;
554  }  }
555    
556  static int Escape(char *dest, const char *src, int dest_len)  /**
557  {   * ccs_find_next_domain - Find a domain.
558          while (*src) {   *
559                  const unsigned char c = * (const unsigned char *) src;   * @ee: Pointer to "struct ccs_execve_entry".
560                  if (c == '\\') {   *
561                          dest_len -= 2;   * Returns 0 on success, negative value otherwise.
562                          if (dest_len <= 0) goto out;   *
563                          *dest++ = '\\';   * Caller holds ccs_read_lock().
564                          *dest++ = '\\';   */
565                  } else if (c > ' ' && c < 127) {  static int ccs_find_next_domain(struct ccs_execve_entry *ee)
                         if (--dest_len <= 0) goto out;  
                         *dest++ = c;  
                 } else {  
                         dest_len -= 4;  
                         if (dest_len <= 0) goto out;  
                         *dest++ = '\\';  
                         *dest++ = (c >> 6) + '0';  
                         *dest++ = ((c >> 3) & 7) + '0';  
                         *dest++ = (c & 7) + '0';  
                 }  
                 src++;  
         }  
         if (--dest_len <= 0) goto out;  
         *dest = '\0';  
         return 0;  
  out:  
         return -ENOMEM;  
 }  
   
 static char *get_argv0(struct linux_binprm *bprm)  
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;  
         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, bprm)) < 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(domain);
662     done:
663          if (!domain) {          if (!domain) {
664                  printk("TOMOYO-ERROR: Domain '%s' not defined.\n", new_domain_name);                  retval = (r->mode == CCS_CONFIG_ENFORCING) ? -EPERM : 0;
665                  if (is_enforce) retval = -EPERM;                  if (!r->domain->domain_transition_failed) {
666                            r->domain->domain_transition_failed = true;
667                            ccs_write_audit_log(false, r,
668                                                CCS_KEYWORD_TRANSITION_FAILED
669                                                "\n");
670                            printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n",
671                                   ee->tmp);
672                    }
673          } else {          } else {
674                  retval = 0;                  retval = 0;
675          }          }
676   out: ;   out:
677          ccs_free(new_domain_name);          if (domain)
678          ccs_free(real_program_name);                  r->domain = domain;
679          ccs_free(symlink_program_name);          if (need_kfree)
680          *next_domain = domain ? domain : old_domain;                  kfree(rn.name);
681          return retval;          return retval;
682  }  }
683    
684  static int CheckEnviron(struct linux_binprm *bprm)  /**
685     * ccs_environ - Check permission for environment variable names.
686     *
687     * @ee: Pointer to "struct ccs_execve_entry".
688     *
689     * Returns 0 on success, negative value otherwise.
690     */
691    static int ccs_environ(struct ccs_execve_entry *ee)
692  {  {
693          const u8 profile = current->domain_info->profile;          struct ccs_request_info *r = &ee->r;
694          const u8 mode = CheckCCSFlags(CCS_TOMOYO_MAC_FOR_ENV);          struct linux_binprm *bprm = ee->bprm;
695          char *arg_ptr;          /* env_page->data is allocated by ccs_dump_page(). */
696            struct ccs_page_dump env_page = { };
697            char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
698          int arg_len = 0;          int arg_len = 0;
699          unsigned long pos = bprm->p;          unsigned long pos = bprm->p;
700          int i = pos / PAGE_SIZE, offset = pos % PAGE_SIZE;          int offset = pos % PAGE_SIZE;
701          int argv_count = bprm->argc;          int argv_count = bprm->argc;
702          int envp_count = bprm->envc;          int envp_count = bprm->envc;
703          //printk("start %d %d\n", argv_count, envp_count);          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
704          int error = -ENOMEM;          int error = -ENOMEM;
705          if (!mode || !envp_count) return 0;          if (!r->mode || !envp_count)
706          arg_ptr = ccs_alloc(CCS_MAX_PATHNAME_LEN);                  return 0;
707          if (!arg_ptr) goto out;          arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
708            if (!arg_ptr)
709                    goto out;
710          while (error == -ENOMEM) {          while (error == -ENOMEM) {
711                  struct page *page;                  if (!ccs_dump_page(bprm, pos, &env_page))
                 const char *kaddr;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)  
                 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) goto out;  
                 pos += PAGE_SIZE - offset;  
 #else  
                 page = bprm->page[i];  
 #endif  
                 /* Map */  
                 kaddr = kmap(page);  
                 if (!kaddr) { /* Mapping failed. */  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)  
                         put_page(page);  
 #endif  
712                          goto out;                          goto out;
713                  }                  pos += PAGE_SIZE - offset;
714                  /* Read. */                  /* Read. */
715                  while (argv_count && offset < PAGE_SIZE) {                  while (argv_count && offset < PAGE_SIZE) {
716                          if (!kaddr[offset++]) argv_count--;                          if (!env_page.data[offset++])
717                                    argv_count--;
718                    }
719                    if (argv_count) {
720                            offset = 0;
721                            continue;
722                  }                  }
                 if (argv_count) goto unmap_page;  
723                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
724                          const unsigned char c = kaddr[offset++];                          const unsigned char c = env_page.data[offset++];
725                          if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
726                                  if (c == '=') {                                  if (c == '=') {
727                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
728                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 786  static int CheckEnviron(struct linux_bin Line 733  static int CheckEnviron(struct linux_bin
733                                  } else {                                  } else {
734                                          arg_ptr[arg_len++] = '\\';                                          arg_ptr[arg_len++] = '\\';
735                                          arg_ptr[arg_len++] = (c >> 6) + '0';                                          arg_ptr[arg_len++] = (c >> 6) + '0';
736                                          arg_ptr[arg_len++] = ((c >> 3) & 7) + '0';                                          arg_ptr[arg_len++]
737                                                    = ((c >> 3) & 7) + '0';
738                                          arg_ptr[arg_len++] = (c & 7) + '0';                                          arg_ptr[arg_len++] = (c & 7) + '0';
739                                  }                                  }
740                          } else {                          } else {
741                                  arg_ptr[arg_len] = '\0';                                  arg_ptr[arg_len] = '\0';
742                          }                          }
743                          if (c) continue;                          if (c)
744                          if (CheckEnvPerm(arg_ptr, profile, mode)) {                                  continue;
745                            if (ccs_env_perm(r, arg_ptr)) {
746                                  error = -EPERM;                                  error = -EPERM;
747                                  break;                                  break;
748                          }                          }
# Line 803  static int CheckEnviron(struct linux_bin Line 752  static int CheckEnviron(struct linux_bin
752                          }                          }
753                          arg_len = 0;                          arg_len = 0;
754                  }                  }
         unmap_page:  
                 /* Unmap. */  
                 kunmap(page);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU)  
                 put_page(page);  
 #endif  
                 i++;  
755                  offset = 0;                  offset = 0;
756          }          }
757   out:   out:
758          ccs_free(arg_ptr);          if (r->mode != 3)
759          if (error && mode != 3) error = 0;                  error = 0;
760            kfree(env_page.data);
761          return error;          return error;
762  }  }
763    
764  static void UnEscape(unsigned char *dest)  /**
765     * ccs_unescape - Unescape escaped string.
766     *
767     * @dest: String to unescape.
768     *
769     * Returns nothing.
770     */
771    static void ccs_unescape(unsigned char *dest)
772  {  {
773          unsigned char *src = dest;          unsigned char *src = dest;
774          unsigned char c, d, e;          unsigned char c;
775          while ((c = *src++) != '\0') {          unsigned char d;
776            unsigned char e;
777            while (1) {
778                    c = *src++;
779                    if (!c)
780                            break;
781                  if (c != '\\') {                  if (c != '\\') {
782                          *dest++ = c;                          *dest++ = c;
783                          continue;                          continue;
# Line 830  static void UnEscape(unsigned char *dest Line 785  static void UnEscape(unsigned char *dest
785                  c = *src++;                  c = *src++;
786                  if (c == '\\') {                  if (c == '\\') {
787                          *dest++ = c;                          *dest++ = c;
788                  } 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;  
789                  }                  }
790                    if (c < '0' || c > '3')
791                            break;
792                    d = *src++;
793                    if (d < '0' || d > '7')
794                            break;
795                    e = *src++;
796                    if (e < '0' || e > '7')
797                            break;
798                    *dest++ = ((c - '0') << 6) + ((d - '0') << 3) + (e - '0');
799          }          }
800          *dest = '\0';          *dest = '\0';
801  }  }
802    
803  static int try_alt_exec(struct linux_binprm *bprm, char **alt_exec0)  /**
804     * ccs_root_depth - Get number of directories to strip.
805     *
806     * @dentry: Pointer to "struct dentry".
807     * @vfsmnt: Pointer to "struct vfsmount".
808     *
809     * Returns number of directories to strip.
810     */
811    static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)
812    {
813            int depth = 0;
814            ccs_realpath_lock();
815            for (;;) {
816                    if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
817                            /* Global root? */
818                            if (vfsmnt->mnt_parent == vfsmnt)
819                                    break;
820                            dentry = vfsmnt->mnt_mountpoint;
821                            vfsmnt = vfsmnt->mnt_parent;
822                            continue;
823                    }
824                    dentry = dentry->d_parent;
825                    depth++;
826            }
827            ccs_realpath_unlock();
828            return depth;
829    }
830    
831    /**
832     * ccs_get_root_depth - return the depth of root directory.
833     *
834     * Returns number of directories to strip.
835     */
836    static int ccs_get_root_depth(void)
837  {  {
838            int depth;
839            struct dentry *dentry;
840            struct vfsmount *vfsmnt;
841    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
842            struct path root;
843    #endif
844            read_lock(&current->fs->lock);
845    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
846            root = current->fs->root;
847            path_get(&current->fs->root);
848            dentry = root.dentry;
849            vfsmnt = root.mnt;
850    #else
851            dentry = dget(current->fs->root);
852            vfsmnt = mntget(current->fs->rootmnt);
853    #endif
854            read_unlock(&current->fs->lock);
855            depth = ccs_root_depth(dentry, vfsmnt);
856    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
857            path_put(&root);
858    #else
859            dput(dentry);
860            mntput(vfsmnt);
861    #endif
862            return depth;
863    }
864    
865    /**
866     * ccs_try_alt_exec - Try to start execute handler.
867     *
868     * @ee: Pointer to "struct ccs_execve_entry".
869     *
870     * Returns 0 on success, negative value otherwise.
871     */
872    static int ccs_try_alt_exec(struct ccs_execve_entry *ee)
873    {
874            /*
875             * Contents of modified bprm.
876             * The envp[] in original bprm is moved to argv[] so that
877             * the alternatively executed program won't be affected by
878             * some dangerous environment variables like LD_PRELOAD.
879             *
880             * modified bprm->argc
881             *    = original bprm->argc + original bprm->envc + 7
882             * modified bprm->envc
883             *    = 0
884             *
885             * modified bprm->argv[0]
886             *    = the program's name specified by execute_handler
887             * modified bprm->argv[1]
888             *    = ccs_current_domain()->domainname->name
889             * modified bprm->argv[2]
890             *    = the current process's name
891             * modified bprm->argv[3]
892             *    = the current process's information (e.g. uid/gid).
893             * modified bprm->argv[4]
894             *    = original bprm->filename
895             * modified bprm->argv[5]
896             *    = original bprm->argc in string expression
897             * modified bprm->argv[6]
898             *    = original bprm->envc in string expression
899             * modified bprm->argv[7]
900             *    = original bprm->argv[0]
901             *  ...
902             * modified bprm->argv[bprm->argc + 6]
903             *     = original bprm->argv[bprm->argc - 1]
904             * modified bprm->argv[bprm->argc + 7]
905             *     = original bprm->envp[0]
906             *  ...
907             * modified bprm->argv[bprm->envc + bprm->argc + 6]
908             *     = original bprm->envp[bprm->envc - 1]
909             */
910            struct linux_binprm *bprm = ee->bprm;
911          struct file *filp;          struct file *filp;
912          int retval;          int retval;
913          /* domainname must not be modified. */          const int original_argc = bprm->argc;
914          char *domainname = (char *) current->domain_info->domainname->name;          const int original_envc = bprm->envc;
915          char *alt_exec;          struct task_struct *task = current;
916          const char *alt_exec1 = GetAltExec();  
917          if (!alt_exec1 || *alt_exec1 != '/') return -EINVAL;          /* Close the requested program's dentry. */
918          retval = strlen(alt_exec1) + 1;          ee->obj.path1.dentry = NULL;
919          *alt_exec0 = alt_exec = ccs_alloc(retval);          ee->obj.path1.mnt = NULL;
920          if (!alt_exec) return -ENOMEM;          ee->obj.validate_done = false;
         memmove(alt_exec, alt_exec1, retval);  
         UnEscape(alt_exec);  
921          allow_write_access(bprm->file);          allow_write_access(bprm->file);
922          fput(bprm->file);          fput(bprm->file);
923          bprm->file = NULL;          bprm->file = NULL;
924  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)  
925          retval = remove_arg_zero(bprm);          /* Invalidate page dump cache. */
926          if (retval) return retval;          ee->dump.page = NULL;
927  #else  
928          remove_arg_zero(bprm);          /* Move envp[] to argv[] */
929            bprm->argc += bprm->envc;
930            bprm->envc = 0;
931    
932            /* Set argv[6] */
933            {
934                    char *cp = ee->tmp;
935                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc);
936                    retval = copy_strings_kernel(1, &cp, bprm);
937                    if (retval < 0)
938                            goto out;
939                    bprm->argc++;
940            }
941    
942            /* Set argv[5] */
943            {
944                    char *cp = ee->tmp;
945                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
946                    retval = copy_strings_kernel(1, &cp, bprm);
947                    if (retval < 0)
948                            goto out;
949                    bprm->argc++;
950            }
951    
952            /* Set argv[4] */
953            {
954                    retval = copy_strings_kernel(1, &bprm->filename, bprm);
955                    if (retval < 0)
956                            goto out;
957                    bprm->argc++;
958            }
959    
960            /* Set argv[3] */
961            {
962                    char *cp = ee->tmp;
963                    const u32 ccs_flags = task->ccs_flags;
964                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
965                             "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
966                             "sgid=%d fsuid=%d fsgid=%d state[0]=%u "
967                             "state[1]=%u state[2]=%u",
968                             (pid_t) sys_getpid(), current_uid(), current_gid(),
969                             current_euid(), current_egid(), current_suid(),
970                             current_sgid(), current_fsuid(), current_fsgid(),
971                             (u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16),
972                             (u8) (ccs_flags >> 8));
973                    retval = copy_strings_kernel(1, &cp, bprm);
974                    if (retval < 0)
975                            goto out;
976                    bprm->argc++;
977            }
978    
979            /* Set argv[2] */
980            {
981                    char *exe = (char *) ccs_get_exe();
982                    if (exe) {
983                            retval = copy_strings_kernel(1, &exe, bprm);
984                            kfree(exe);
985                    } else {
986                            exe = ee->tmp;
987                            snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
988                            retval = copy_strings_kernel(1, &exe, bprm);
989                    }
990                    if (retval < 0)
991                            goto out;
992                    bprm->argc++;
993            }
994    
995            /* Set argv[1] */
996            {
997                    char *cp = ee->tmp;
998                    snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
999                             ccs_current_domain()->domainname->name);
1000                    retval = copy_strings_kernel(1, &cp, bprm);
1001                    if (retval < 0)
1002                            goto out;
1003                    bprm->argc++;
1004            }
1005    
1006            /* Set argv[0] */
1007            {
1008                    int depth = ccs_get_root_depth();
1009                    int len = ee->handler->total_len + 1;
1010                    char *cp = kmalloc(len, GFP_KERNEL);
1011                    if (!cp) {
1012                            retval = -ENOMEM;
1013                            goto out;
1014                    }
1015                    ee->handler_path = cp;
1016                    memmove(cp, ee->handler->name, len);
1017                    ccs_unescape(cp);
1018                    retval = -ENOENT;
1019                    if (!*cp || *cp != '/')
1020                            goto out;
1021                    /* Adjust root directory for open_exec(). */
1022                    while (depth) {
1023                            cp = strchr(cp + 1, '/');
1024                            if (!cp)
1025                                    goto out;
1026                            depth--;
1027                    }
1028                    memmove(ee->handler_path, cp, strlen(cp) + 1);
1029                    cp = ee->handler_path;
1030                    retval = copy_strings_kernel(1, &cp, bprm);
1031                    if (retval < 0)
1032                            goto out;
1033                    bprm->argc++;
1034            }
1035    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
1036    #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
1037            bprm->argv_len = bprm->exec - bprm->p;
1038  #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);  
1039  #endif  #endif
1040          if (retval < 0) return retval;  
1041          bprm->argc++;          /* OK, now restart the process with execute handler program's dentry. */
1042          retval = copy_strings_kernel(1, &domainname, bprm);          filp = open_exec(ee->handler_path);
1043          if (retval < 0) return retval;          if (IS_ERR(filp)) {
1044          bprm->argc++;                  retval = PTR_ERR(filp);
1045          retval = copy_strings_kernel(1, &alt_exec, bprm);                  goto out;
1046          if (retval < 0) return retval;          }
1047          bprm->argc++;          ee->obj.path1.dentry = filp->f_dentry;
1048  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)          ee->obj.path1.mnt = filp->f_vfsmnt;
1049          bprm->interp = alt_exec;          bprm->file = filp;
1050            bprm->filename = ee->handler_path;
1051    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1052            bprm->interp = bprm->filename;
1053  #endif  #endif
1054          filp = open_exec(alt_exec);          retval = prepare_binprm(bprm);
1055          if (IS_ERR(filp)) return PTR_ERR(filp);          if (retval < 0)
1056          bprm->file= filp;                  goto out;
1057          bprm->filename = alt_exec;          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1058          return prepare_binprm(bprm);          retval = ccs_find_next_domain(ee);
1059            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1060     out:
1061            return retval;
1062  }  }
1063    
1064  #endif  /**
1065     * ccs_find_execute_handler - Find an execute handler.
1066     *
1067     * @ee:   Pointer to "struct ccs_execve_entry".
1068     * @type: Type of execute handler.
1069     *
1070     * Returns true if found, false otherwise.
1071     *
1072     * Caller holds ccs_read_lock().
1073     */
1074    static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,
1075                                         const u8 type)
1076    {
1077            struct task_struct *task = current;
1078            const struct ccs_domain_info *domain = ccs_current_domain();
1079            struct ccs_acl_info *ptr;
1080            bool found = false;
1081            /*
1082             * Don't use execute handler if the current process is
1083             * marked as execute handler to avoid infinite execute handler loop.
1084             */
1085            if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
1086                    return false;
1087            list_for_each_entry(ptr, &domain->acl_info_list, list) {
1088                    struct ccs_execute_handler_record *acl;
1089                    if (ptr->type != type)
1090                            continue;
1091                    acl = container_of(ptr, struct ccs_execute_handler_record,
1092                                       head);
1093                    ee->handler = acl->handler;
1094                    found = true;
1095                    break;
1096            }
1097            return found;
1098    }
1099    
1100  int search_binary_handler_with_transition(struct linux_binprm *bprm, struct pt_regs *regs)  /**
1101     * ccs_dump_page - Dump a page to buffer.
1102     *
1103     * @bprm: Pointer to "struct linux_binprm".
1104     * @pos:  Location to dump.
1105     * @dump: Poiner to "struct ccs_page_dump".
1106     *
1107     * Returns true on success, false otherwise.
1108     */
1109    bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
1110                       struct ccs_page_dump *dump)
1111  {  {
1112          struct domain_info *next_domain = NULL, *prev_domain = current->domain_info;          struct page *page;
1113          int retval;          /* dump->data is released by ccs_finish_execve(). */
1114          char *alt_exec = NULL; /* Keep valid until search_binary_handler() finishes. */          if (!dump->data) {
1115  #if defined(CONFIG_SAKURA) || defined(CONFIG_TOMOYO)                  dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);
1116          extern void CCS_LoadPolicy(const char *filename);                  if (!dump->data)
1117          CCS_LoadPolicy(bprm->filename);                          return false;
1118            }
1119            /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
1120    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1121            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1122                    return false;
1123    #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1124            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1125                    return false;
1126    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1127            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1128                    return false;
1129    #else
1130            page = bprm->page[pos / PAGE_SIZE];
1131  #endif  #endif
1132  #if defined(CONFIG_TOMOYO)          if (page != dump->page) {
1133          retval = FindNextDomain(bprm, &next_domain, 1);                  const unsigned int offset = pos % PAGE_SIZE;
1134          if (retval == -EPERM && try_alt_exec(bprm, &alt_exec) >= 0) {                  /*
1135                  current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                   * Maybe kmap()/kunmap() should be used here.
1136                  retval = FindNextDomain(bprm, &next_domain, 0);                   * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
1137                  current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                   * So do I.
1138          }                   */
1139          if (retval == 0) {                  char *kaddr = kmap_atomic(page, KM_USER0);
1140                  current->domain_info = next_domain;                  dump->page = page;
1141                  retval = CheckEnviron(bprm);                  memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset);
1142                  current->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC;                  kunmap_atomic(kaddr, KM_USER0);
1143                  if (!retval) retval = search_binary_handler(bprm, regs);          }
1144                  current->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC;          /* Same with put_arg_page(page) in fs/exec.c */
1145                  if (retval < 0) current->domain_info = prev_domain;  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1146            put_page(page);
1147    #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1148            put_page(page);
1149    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1150            put_page(page);
1151    #endif
1152            return true;
1153    }
1154    
1155    /**
1156     * ccs_start_execve - Prepare for execve() operation.
1157     *
1158     * @bprm: Pointer to "struct linux_binprm".
1159     * @eep:  Pointer to "struct ccs_execve_entry *".
1160     *
1161     * Returns 0 on success, negative value otherwise.
1162     */
1163    int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve_entry **eep)
1164    {
1165            int retval;
1166            struct task_struct *task = current;
1167            struct ccs_execve_entry *ee;
1168            *eep = NULL;
1169            if (!ccs_policy_loaded)
1170                    ccs_load_policy(bprm->filename);
1171            ee = kzalloc(sizeof(*ee), GFP_KERNEL);
1172            if (!ee)
1173                    return -ENOMEM;
1174            ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
1175            if (!ee->tmp) {
1176                    kfree(ee);
1177                    return -ENOMEM;
1178            }
1179            ee->reader_idx = ccs_read_lock();
1180            /* ee->dump->data is allocated by ccs_dump_page(). */
1181            ee->previous_domain = task->ccs_domain_info;
1182            /* Clear manager flag. */
1183            task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;
1184            /* Tell GC that I started execve(). */
1185            task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
1186            /*
1187             * Make task->ccs_flags visible to GC before changing
1188             * task->ccs_domain_info .
1189             */
1190            smp_mb();
1191            *eep = ee;
1192            ccs_init_request_info(&ee->r, NULL, CCS_MAC_FILE_EXECUTE);
1193            ee->r.ee = ee;
1194            ee->bprm = bprm;
1195            ee->r.obj = &ee->obj;
1196            ee->obj.path1.dentry = bprm->file->f_dentry;
1197            ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1198            if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER)) {
1199                    retval = ccs_try_alt_exec(ee);
1200                    if (!retval)
1201                            ccs_audit_execute_handler_log(ee, true);
1202                    goto ok;
1203            }
1204            retval = ccs_find_next_domain(ee);
1205            if (retval != -EPERM)
1206                    goto ok;
1207            if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) {
1208                    retval = ccs_try_alt_exec(ee);
1209                    if (!retval)
1210                            ccs_audit_execute_handler_log(ee, false);
1211          }          }
1212          ccs_free(alt_exec);   ok:
1213            if (retval < 0)
1214                    goto out;
1215            /*
1216             * Proceed to the next domain in order to allow reaching via PID.
1217             * It will be reverted if execve() failed. Reverting is not good.
1218             * But it is better than being unable to reach via PID in interactive
1219             * enforcing mode.
1220             */
1221            task->ccs_domain_info = ee->r.domain;
1222            ee->r.mode = ccs_get_mode(ee->r.domain->profile, CCS_MAC_ENVIRON);
1223            retval = ccs_environ(ee);
1224            if (retval < 0)
1225                    goto out;
1226            retval = 0;
1227     out:
1228          return retval;          return retval;
 #else  
         return search_binary_handler(bprm, regs);  
 #endif  
1229  }  }
1230    
1231  /***** TOMOYO Linux end. *****/  /**
1232     * ccs_finish_execve - Clean up execve() operation.
1233     *
1234     * @retval: Return code of an execve() operation.
1235     * @ee:     Pointer to "struct ccs_execve_entry".
1236     *
1237     * Caller holds ccs_read_lock().
1238     */
1239    void ccs_finish_execve(int retval, struct ccs_execve_entry *ee)
1240    {
1241            struct task_struct *task = current;
1242            if (!ee)
1243                    return;
1244            if (retval < 0) {
1245                    task->ccs_domain_info = ee->previous_domain;
1246                    /*
1247                     * Make task->ccs_domain_info visible to GC before changing
1248                     * task->ccs_flags .
1249                     */
1250                    smp_mb();
1251            } else {
1252                    /* Mark the current process as execute handler. */
1253                    if (ee->handler)
1254                            task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1255                    /* Mark the current process as normal process. */
1256                    else
1257                            task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1258            }
1259            /* Tell GC that I finished execve(). */
1260            task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1261            ccs_read_unlock(ee->reader_idx);
1262            kfree(ee->handler_path);
1263            kfree(ee->tmp);
1264            kfree(ee->dump.data);
1265            kfree(ee);
1266    }

Legend:
Removed from v.987  
changed lines
  Added in v.3258

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