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

Subversion リポジトリの参照

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

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

branches/ccs-patch/fs/tomoyo_domain.c revision 2738 by kumaneko, Tue Jul 7 01:28:46 2009 UTC branches/ccs-patch/security/ccsecurity/domain.c revision 3710 by kumaneko, Wed Jun 2 06:53:14 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * fs/tomoyo_domain.c   * security/ccsecurity/domain.c
3   *   *
4   * Implementation of the Domain-Based Mandatory Access Control.   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Version: 1.7.2+   2010/05/27
  *  
  * Version: 1.7.0-pre   2009/07/03  
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   */   */
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/version.h>
16  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
17  #include <linux/namei.h>  #include <linux/namei.h>
18  #include <linux/mount.h>  #include <linux/mount.h>
# Line 23  Line 20 
20  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
21  #include <linux/fs_struct.h>  #include <linux/fs_struct.h>
22  #endif  #endif
23    #include "internal.h"
 /* For compatibility with older kernels. */  
 #ifndef for_each_process  
 #define for_each_process for_each_task  
 #endif  
24    
25  /* Variables definitions.*/  /* Variables definitions.*/
26    
27    /* The global domain. */
28    struct ccs_domain_info ccs_global_domain;
29    
30  /* The initial domain. */  /* The initial domain. */
31  struct ccs_domain_info ccs_kernel_domain;  struct ccs_domain_info ccs_kernel_domain;
32    
33  /* The list for "struct ccs_domain_info". */  /* The list for "struct ccs_domain_info". */
34  LIST_HEAD(ccs_domain_list);  LIST_HEAD(ccs_domain_list);
35    
36    struct list_head ccs_policy_list[CCS_MAX_POLICY];
37    struct list_head ccs_group_list[CCS_MAX_GROUP];
38    struct list_head ccs_shared_list[CCS_MAX_LIST];
39    
40  /**  /**
41   * ccs_get_last_name - Get last component of a domainname.   * ccs_audit_execute_handler_log - Audit execute_handler log.
42   *   *
43   * @domain: Pointer to "struct ccs_domain_info".   * @ee:         Pointer to "struct ccs_execve".
44   *   *
45   * Returns the last component of the domainname.   * Returns 0 on success, negative value otherwise.
46   */   */
47  const char *ccs_get_last_name(const struct ccs_domain_info *domain)  static int ccs_audit_execute_handler_log(struct ccs_execve *ee)
48  {  {
49          const char *cp0 = domain->domainname->name;          struct ccs_request_info *r = &ee->r;
50          const char *cp1 = strrchr(cp0, ' ');          const char *handler = ee->handler->name;
51          if (cp1)          r->type = CCS_MAC_FILE_EXECUTE;
52                  return cp1 + 1;          r->mode = ccs_get_mode(r->profile, CCS_MAC_FILE_EXECUTE);
53          return cp0;          r->granted = true;
54            return ccs_write_log(r, "%s %s\n", ee->handler_type ==
55                                 CCS_TYPE_DENIED_EXECUTE_HANDLER ?
56                                 CCS_KEYWORD_DENIED_EXECUTE_HANDLER :
57                                 CCS_KEYWORD_EXECUTE_HANDLER, handler);
58  }  }
59    
60  /**  /**
61   * ccs_add_domain_acl - Add the given ACL to the given domain.   * ccs_audit_domain_creation_log - Audit domain creation log.
  *  
  * @domain: Pointer to "struct ccs_domain_info". May be NULL.  
  * @acl:    Pointer to "struct ccs_acl_info".  
62   *   *
63   * Returns 0.   * Returns 0 on success, negative value otherwise.
64   */   */
65  int ccs_add_domain_acl(struct ccs_domain_info *domain, struct ccs_acl_info *acl)  static int ccs_audit_domain_creation_log(void)
66  {  {
67          if (domain) {          struct ccs_request_info r;
68                  if (acl->cond)          ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE);
69                          atomic_inc(&acl->cond->users);          r.granted = false;
70                  list_add_tail_rcu(&acl->list, &domain->acl_info_list);          return ccs_write_log(&r, "use_profile %u\n", r.profile);
71          } else {  }
72                  acl->type &= ~ACL_DELETED;  
73    int ccs_update_policy(struct ccs_acl_head *new_entry, const int size,
74                          bool is_delete, const int idx, bool (*check_duplicate)
75                          (const struct ccs_acl_head *,
76                           const struct ccs_acl_head *))
77    {
78            int error = is_delete ? -ENOENT : -ENOMEM;
79            struct ccs_acl_head *entry;
80            if (mutex_lock_interruptible(&ccs_policy_lock))
81                    return -ENOMEM;
82            list_for_each_entry_rcu(entry, &ccs_policy_list[idx], list) {
83                    if (!check_duplicate(entry, new_entry))
84                            continue;
85                    entry->is_deleted = is_delete;
86                    error = 0;
87                    break;
88          }          }
89          return 0;          if (error && !is_delete) {
90                    entry = ccs_commit_ok(new_entry, size);
91                    if (entry) {
92                            list_add_tail_rcu(&entry->list, &ccs_policy_list[idx]);
93                            error = 0;
94                    }
95            }
96            mutex_unlock(&ccs_policy_lock);
97            return error;
98  }  }
99    
100  /**  int ccs_update_group(struct ccs_acl_head *new_entry, const int size,
101   * ccs_del_domain_acl - Delete the given ACL from the domain.                       bool is_delete, struct ccs_group *group,
102   *                       bool (*check_duplicate) (const struct ccs_acl_head *,
103   * @acl: Pointer to "struct ccs_acl_info". May be NULL.                                                const struct ccs_acl_head *))
  *  
  * Returns 0.  
  */  
 int ccs_del_domain_acl(struct ccs_acl_info *acl)  
104  {  {
105          if (acl)          int error = is_delete ? -ENOENT : -ENOMEM;
106                  acl->type |= ACL_DELETED;          struct ccs_acl_head *entry;
107          return 0;          if (mutex_lock_interruptible(&ccs_policy_lock))
108                    return -ENOMEM;
109            list_for_each_entry_rcu(entry, &group->member_list, list) {
110                    if (!check_duplicate(entry, new_entry))
111                            continue;
112                    entry->is_deleted = is_delete;
113                    error = 0;
114                    break;
115            }
116            if (!is_delete && error) {
117                    entry = ccs_commit_ok(new_entry, size);
118                    if (entry) {
119                            list_add_tail_rcu(&entry->list, &group->member_list);
120                            error = 0;
121                    }
122            }
123            mutex_unlock(&ccs_policy_lock);
124            return error;
125  }  }
126    
127  /**  static void ccs_delete_type(struct ccs_domain_info *domain, u8 type)
  * ccs_audit_execute_handler_log - Audit execute_handler log.  
  *  
  * @ee:         Pointer to "struct ccs_execve_entry".  
  * @is_default: True if it is "execute_handler" log.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_audit_execute_handler_log(struct ccs_execve_entry *ee,  
                                          const bool is_default)  
128  {  {
129          struct ccs_request_info *r = &ee->r;          struct ccs_acl_info *ptr;
130          const char *handler = ee->handler->name;          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
131          r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_FILE);                  if (ptr->type == type)
132          return ccs_write_audit_log(true, r, "%s %s\n",                          ptr->is_deleted = true;
133                                     is_default ? KEYWORD_EXECUTE_HANDLER :          }
                                    KEYWORD_DENIED_EXECUTE_HANDLER, handler);  
134  }  }
135    
136  /**  int ccs_update_domain(struct ccs_acl_info *new_entry, const int size,
137   * ccs_audit_domain_creation_log - Audit domain creation log.                        bool is_delete, struct ccs_domain_info *domain,
138   *                        bool (*check_duplicate) (const struct ccs_acl_info *,
139   * @domain:  Pointer to "struct ccs_domain_info".                                                 const struct ccs_acl_info *),
140   *                        bool (*merge_duplicate) (struct ccs_acl_info *,
141   * Returns 0 on success, negative value otherwise.                                                 struct ccs_acl_info *,
142   */                                                 const bool))
 static int ccs_audit_domain_creation_log(struct ccs_domain_info *domain)  
143  {  {
144          int error;          int error = is_delete ? -ENOENT : -ENOMEM;
145          struct ccs_request_info r;          struct ccs_acl_info *entry;
146          ccs_init_request_info(&r, domain, CCS_MAC_FOR_FILE);          /*
147          error = ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile);           * Only one "execute_handler" and "denied_execute_handler" can exist
148             * in a domain.
149             */
150            const u8 type = new_entry->type;
151            const bool exclusive = !is_delete &&
152                    (type == CCS_TYPE_EXECUTE_HANDLER ||
153                     type == CCS_TYPE_DENIED_EXECUTE_HANDLER);
154            if (mutex_lock_interruptible(&ccs_policy_lock))
155                    return error;
156            list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
157                    if (!check_duplicate(entry, new_entry))
158                            continue;
159                    if (exclusive)
160                            ccs_delete_type(domain, type);
161                    if (merge_duplicate)
162                            entry->is_deleted = merge_duplicate(entry, new_entry,
163                                                                is_delete);
164                    else
165                            entry->is_deleted = is_delete;
166                    error = 0;
167                    break;
168            }
169            if (error && !is_delete) {
170                    entry = ccs_commit_ok(new_entry, size);
171                    if (entry) {
172                            if (exclusive)
173                                    ccs_delete_type(domain, type);
174                            ccs_add_domain_acl(domain, entry);
175                            error = 0;
176                    }
177            }
178            mutex_unlock(&ccs_policy_lock);
179          return error;          return error;
180  }  }
181    
182  /* The list for "struct ccs_domain_initializer_entry". */  void ccs_check_acl(struct ccs_request_info *r,
183  LIST_HEAD(ccs_domain_initializer_list);                     bool (*check_entry) (const struct ccs_request_info *,
184                                            const struct ccs_acl_info *))
185    {
186            const struct ccs_domain_info *domain = ccs_current_domain();
187            struct ccs_acl_info *ptr;
188     retry:
189            list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
190                    if (ptr->is_deleted || ptr->type != r->param_type)
191                            continue;
192                    if (check_entry(r, ptr) && ccs_condition(r, ptr->cond)) {
193                            r->cond = ptr->cond;
194                            r->granted = true;
195                            return;
196                    }
197            }
198            if (domain != &ccs_global_domain && !domain->ignore_global &&
199                (r->param_type != CCS_TYPE_PATH_ACL ||
200                 r->param.path.operation != CCS_TYPE_READ ||
201                 !domain->ignore_global_allow_read) &&
202                (r->param_type != CCS_TYPE_ENV_ACL ||
203                 !domain->ignore_global_allow_env)) {
204                    domain = &ccs_global_domain;
205                    goto retry;
206            }
207            r->granted = false;
208    }
209    
210    static bool ccs_same_domain_initializer_entry(const struct ccs_acl_head *a,
211                                                  const struct ccs_acl_head *b)
212    {
213            const struct ccs_domain_initializer *p1 = container_of(a, typeof(*p1),
214                                                                   head);
215            const struct ccs_domain_initializer *p2 = container_of(b, typeof(*p2),
216                                                                   head);
217            return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
218                    && p1->domainname == p2->domainname
219                    && p1->program == p2->program;
220    }
221    
222  /**  /**
223   * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list.   * ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer" list.
224   *   *
225   * @domainname: The name of domain. May be NULL.   * @domainname: The name of domain. May be NULL.
226   * @program:    The name of program.   * @program:    The name of program.
# Line 140  static int ccs_update_domain_initializer Line 234  static int ccs_update_domain_initializer
234                                                 const bool is_not,                                                 const bool is_not,
235                                                 const bool is_delete)                                                 const bool is_delete)
236  {  {
237          struct ccs_domain_initializer_entry *entry = NULL;          struct ccs_domain_initializer e = { .is_not = is_not };
         struct ccs_domain_initializer_entry *ptr;  
         const struct ccs_path_info *saved_program;  
         const struct ccs_path_info *saved_domainname = NULL;  
238          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
239          bool is_last_name = false;          if (!ccs_correct_path(program))
240          if (!ccs_is_correct_path(program, 1, -1, -1))                  return -EINVAL;
                 return -EINVAL; /* No patterns allowed. */  
241          if (domainname) {          if (domainname) {
242                  if (!ccs_is_domain_def(domainname) &&                  if (!ccs_domain_def(domainname) &&
243                      ccs_is_correct_path(domainname, 1, -1, -1))                      ccs_correct_path(domainname))
244                          is_last_name = true;                          e.is_last_name = true;
245                  else if (!ccs_is_correct_domain(domainname))                  else if (!ccs_correct_domain(domainname))
246                          return -EINVAL;                          return -EINVAL;
247                  saved_domainname = ccs_get_name(domainname);                  e.domainname = ccs_get_name(domainname);
248                  if (!saved_domainname)                  if (!e.domainname)
249                          return -ENOMEM;                          goto out;
         }  
         saved_program = ccs_get_name(program);  
         if (!saved_program) {  
                 ccs_put_name(saved_domainname);  
                 return -ENOMEM;  
         }  
         if (!is_delete)  
                 entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
         mutex_lock(&ccs_policy_lock);  
         list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {  
                 if (ptr->is_not != is_not ||  
                     ptr->domainname != saved_domainname ||  
                     ptr->program != saved_program)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {  
                 entry->domainname = saved_domainname;  
                 saved_domainname = NULL;  
                 entry->program = saved_program;  
                 saved_program = NULL;  
                 entry->is_not = is_not;  
                 entry->is_last_name = is_last_name;  
                 list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list);  
                 entry = NULL;  
                 error = 0;  
250          }          }
251          mutex_unlock(&ccs_policy_lock);          e.program = ccs_get_name(program);
252          ccs_put_name(saved_domainname);          if (!e.program)
253          ccs_put_name(saved_program);                  goto out;
254          kfree(entry);          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
255                                      CCS_ID_DOMAIN_INITIALIZER,
256                                      ccs_same_domain_initializer_entry);
257     out:
258            ccs_put_name(e.domainname);
259            ccs_put_name(e.program);
260          return error;          return error;
261  }  }
262    
263  /**  /**
264   * ccs_read_domain_initializer_policy - Read "struct ccs_domain_initializer_entry" list.   * ccs_write_domain_initializer - Write "struct ccs_domain_initializer" list.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  *  
  * Caller holds srcu_read_lock(&ccs_ss).  
  */  
 bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head)  
 {  
         struct list_head *pos;  
         bool done = true;  
         list_for_each_cookie(pos, head->read_var2,  
                              &ccs_domain_initializer_list) {  
                 const char *no;  
                 const char *from = "";  
                 const char *domain = "";  
                 struct ccs_domain_initializer_entry *ptr;  
                 ptr = list_entry(pos, struct ccs_domain_initializer_entry,  
                                   list);  
                 if (ptr->is_deleted)  
                         continue;  
                 no = ptr->is_not ? "no_" : "";  
                 if (ptr->domainname) {  
                         from = " from ";  
                         domain = ptr->domainname->name;  
                 }  
                 done = ccs_io_printf(head,  
                                      "%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n",  
                                      no, ptr->program->name, from, domain);  
                 if (!done)  
                         break;  
         }  
         return done;  
 }  
   
 /**  
  * ccs_write_domain_initializer_policy - Write "struct ccs_domain_initializer_entry" list.  
265   *   *
266   * @data:      String to parse.   * @data:      String to parse.
  * @is_not:    True if it is "no_initialize_domain" entry.  
267   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
268   *   *
269   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
270   */   */
271  int ccs_write_domain_initializer_policy(char *data, const bool is_not,  int ccs_write_domain_initializer(char *data, const bool is_delete, const u8 flags)
                                         const bool is_delete)  
272  {  {
273          char *cp = strstr(data, " from ");          char *domainname = strstr(data, " from ");
274          if (cp) {          if (domainname) {
275                  *cp = '\0';                  *domainname = '\0';
276                  return ccs_update_domain_initializer_entry(cp + 6, data,                  domainname += 6;
                                                            is_not, is_delete);  
277          }          }
278          return ccs_update_domain_initializer_entry(NULL, data, is_not,          return ccs_update_domain_initializer_entry(domainname, data, flags,
279                                                     is_delete);                                                     is_delete);
280  }  }
281    
282  /**  /**
283   * ccs_is_domain_initializer - Check whether the given program causes domainname reinitialization.   * ccs_domain_initializer - Check whether the given program causes domainname reinitialization.
284   *   *
285   * @domainname: The name of domain.   * @domainname: The name of domain.
286   * @program:    The name of program.   * @program:    The name of program.
# Line 262  int ccs_write_domain_initializer_policy( Line 289  int ccs_write_domain_initializer_policy(
289   * Returns true if executing @program reinitializes domain transition,   * Returns true if executing @program reinitializes domain transition,
290   * false otherwise.   * false otherwise.
291   *   *
292   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
293   */   */
294  static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname,  static bool ccs_domain_initializer(const struct ccs_path_info *domainname,
295                                        const struct ccs_path_info *program,                                        const struct ccs_path_info *program,
296                                        const struct ccs_path_info *last_name)                                        const struct ccs_path_info *last_name)
297  {  {
298          struct ccs_domain_initializer_entry *ptr;          struct ccs_domain_initializer *ptr;
299          bool flag = false;          bool flag = false;
300          list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) {          list_for_each_entry_rcu(ptr, &ccs_policy_list
301                  if (ptr->is_deleted)                                  [CCS_ID_DOMAIN_INITIALIZER], head.list) {
302                    if (ptr->head.is_deleted)
303                          continue;                          continue;
304                  if (ptr->domainname) {                  if (ptr->domainname) {
305                          if (!ptr->is_last_name) {                          if (!ptr->is_last_name) {
# Line 293  static bool ccs_is_domain_initializer(co Line 321  static bool ccs_is_domain_initializer(co
321          return flag;          return flag;
322  }  }
323    
324  /* The list for "struct ccs_domain_keeper_entry". */  static bool ccs_same_domain_keeper_entry(const struct ccs_acl_head *a,
325  LIST_HEAD(ccs_domain_keeper_list);                                           const struct ccs_acl_head *b)
326    {
327            const struct ccs_domain_keeper *p1 = container_of(a, typeof(*p1),
328                                                              head);
329            const struct ccs_domain_keeper *p2 = container_of(b, typeof(*p2),
330                                                              head);
331            return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
332                    && p1->domainname == p2->domainname
333                    && p1->program == p2->program;
334    }
335    
336  /**  /**
337   * ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper_entry" list.   * ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper" list.
338   *   *
339   * @domainname: The name of domain.   * @domainname: The name of domain.
340   * @program:    The name of program. May be NULL.   * @program:    The name of program. May be NULL.
# Line 311  static int ccs_update_domain_keeper_entr Line 348  static int ccs_update_domain_keeper_entr
348                                            const bool is_not,                                            const bool is_not,
349                                            const bool is_delete)                                            const bool is_delete)
350  {  {
351          struct ccs_domain_keeper_entry *entry = NULL;          struct ccs_domain_keeper e = { .is_not = is_not };
         struct ccs_domain_keeper_entry *ptr;  
         const struct ccs_path_info *saved_domainname;  
         const struct ccs_path_info *saved_program = NULL;  
352          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
353          bool is_last_name = false;          if (!ccs_domain_def(domainname) && ccs_correct_path(domainname))
354          if (!ccs_is_domain_def(domainname) &&                  e.is_last_name = true;
355              ccs_is_correct_path(domainname, 1, -1, -1))          else if (!ccs_correct_domain(domainname))
                 is_last_name = true;  
         else if (!ccs_is_correct_domain(domainname))  
356                  return -EINVAL;                  return -EINVAL;
357          if (program) {          if (program) {
358                  if (!ccs_is_correct_path(program, 1, -1, -1))                  if (!ccs_correct_path(program))
359                          return -EINVAL;                          return -EINVAL;
360                  saved_program = ccs_get_name(program);                  e.program = ccs_get_name(program);
361                  if (!saved_program)                  if (!e.program)
362                          return -ENOMEM;                          goto out;
         }  
         saved_domainname = ccs_get_name(domainname);  
         if (!saved_domainname) {  
                 ccs_put_name(saved_program);  
                 return -ENOMEM;  
         }  
         if (!is_delete)  
                 entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
         mutex_lock(&ccs_policy_lock);  
         list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {  
                 if (ptr->is_not != is_not ||  
                     ptr->domainname != saved_domainname ||  
                     ptr->program != saved_program)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {  
                 entry->domainname = saved_domainname;  
                 saved_domainname = NULL;  
                 entry->program = saved_program;  
                 saved_program = NULL;  
                 entry->is_not = is_not;  
                 entry->is_last_name = is_last_name;  
                 list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list);  
                 entry = NULL;  
                 error = 0;  
363          }          }
364          mutex_unlock(&ccs_policy_lock);          e.domainname = ccs_get_name(domainname);
365          ccs_put_name(saved_domainname);          if (!e.domainname)
366          ccs_put_name(saved_program);                  goto out;
367          kfree(entry);          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
368                                      CCS_ID_DOMAIN_KEEPER,
369                                      ccs_same_domain_keeper_entry);
370     out:
371            ccs_put_name(e.domainname);
372            ccs_put_name(e.program);
373          return error;          return error;
374  }  }
375    
376  /**  /**
377   * ccs_write_domain_keeper_policy - Write "struct ccs_domain_keeper_entry" list.   * ccs_write_domain_keeper - Write "struct ccs_domain_keeper" list.
378   *   *
379   * @data:      String to parse.   * @data:      String to parse.
  * @is_not:    True if it is "no_keep_domain" entry.  
380   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
381   *   *
382     * Returns 0 on success, negative value otherwise.
383   */   */
384  int ccs_write_domain_keeper_policy(char *data, const bool is_not,  int ccs_write_domain_keeper(char *data, const bool is_delete, const u8 flags)
                                    const bool is_delete)  
 {  
         char *cp = strstr(data, " from ");  
         if (cp) {  
                 *cp = '\0';  
                 return ccs_update_domain_keeper_entry(cp + 6, data,  
                                                       is_not, is_delete);  
         }  
         return ccs_update_domain_keeper_entry(data, NULL, is_not, is_delete);  
 }  
   
 /**  
  * ccs_read_domain_keeper_policy - Read "struct ccs_domain_keeper_entry" list.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  *  
  * Caller holds srcu_read_lock(&ccs_ss).  
  */  
 bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head)  
385  {  {
386          struct list_head *pos;          char *domainname = strstr(data, " from ");
387          bool done = true;          if (domainname) {
388          list_for_each_cookie(pos, head->read_var2,                  *domainname = '\0';
389                               &ccs_domain_keeper_list) {                  domainname += 6;
390                  struct ccs_domain_keeper_entry *ptr;          } else {
391                  const char *no;                  domainname = data;
392                  const char *from = "";                  data = NULL;
                 const char *program = "";  
                 ptr = list_entry(pos, struct ccs_domain_keeper_entry, list);  
                 if (ptr->is_deleted)  
                         continue;  
                 no = ptr->is_not ? "no_" : "";  
                 if (ptr->program) {  
                         from = " from ";  
                         program = ptr->program->name;  
                 }  
                 done = ccs_io_printf(head,  
                                      "%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no,  
                                      program, from, ptr->domainname->name);  
                 if (!done)  
                         break;  
393          }          }
394          return done;          return ccs_update_domain_keeper_entry(domainname, data, flags,
395                                                  is_delete);
396  }  }
397    
398  /**  /**
399   * ccs_is_domain_keeper - Check whether the given program causes domain transition suppression.   * ccs_domain_keeper - Check whether the given program causes domain transition suppression.
400   *   *
401   * @domainname: The name of domain.   * @domainname: The name of domain.
402   * @program:    The name of program.   * @program:    The name of program.
# Line 430  bool ccs_read_domain_keeper_policy(struc Line 405  bool ccs_read_domain_keeper_policy(struc
405   * Returns true if executing @program supresses domain transition,   * Returns true if executing @program supresses domain transition,
406   * false otherwise.   * false otherwise.
407   *   *
408   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
409   */   */
410  static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname,  static bool ccs_domain_keeper(const struct ccs_path_info *domainname,
411                                   const struct ccs_path_info *program,                                   const struct ccs_path_info *program,
412                                   const struct ccs_path_info *last_name)                                   const struct ccs_path_info *last_name)
413  {  {
414          struct ccs_domain_keeper_entry *ptr;          struct ccs_domain_keeper *ptr;
415          bool flag = false;          bool flag = false;
416          list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) {          list_for_each_entry_rcu(ptr, &ccs_policy_list[CCS_ID_DOMAIN_KEEPER],
417                  if (ptr->is_deleted)                                  head.list) {
418                    if (ptr->head.is_deleted)
419                          continue;                          continue;
420                  if (!ptr->is_last_name) {                  if (!ptr->is_last_name) {
421                          if (ptr->domainname != domainname)                          if (ptr->domainname != domainname)
# Line 459  static bool ccs_is_domain_keeper(const s Line 435  static bool ccs_is_domain_keeper(const s
435          return flag;          return flag;
436  }  }
437    
438  /* The list for "struct ccs_aggregator_entry". */  static bool ccs_same_aggregator_entry(const struct ccs_acl_head *a,
439  LIST_HEAD(ccs_aggregator_list);                                        const struct ccs_acl_head *b)
440    {
441            const struct ccs_aggregator *p1 = container_of(a, typeof(*p1), head);
442            const struct ccs_aggregator *p2 = container_of(b, typeof(*p2), head);
443            return p1->original_name == p2->original_name &&
444                    p1->aggregated_name == p2->aggregated_name;
445    }
446    
447  /**  /**
448   * ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list.   * ccs_update_aggregator_entry - Update "struct ccs_aggregator" list.
449   *   *
450   * @original_name:   The original program's name.   * @original_name:   The original program's name.
451   * @aggregated_name: The aggregated program's name.   * @aggregated_name: The aggregated program's name.
# Line 475  static int ccs_update_aggregator_entry(c Line 457  static int ccs_update_aggregator_entry(c
457                                         const char *aggregated_name,                                         const char *aggregated_name,
458                                         const bool is_delete)                                         const bool is_delete)
459  {  {
460          struct ccs_aggregator_entry *entry = NULL;          struct ccs_aggregator e = { };
         struct ccs_aggregator_entry *ptr;  
         const struct ccs_path_info *saved_original_name;  
         const struct ccs_path_info *saved_aggregated_name;  
461          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
462          if (!ccs_is_correct_path(original_name, 1, 0, -1) ||          if (!ccs_correct_path(original_name) ||
463              !ccs_is_correct_path(aggregated_name, 1, -1, -1))              !ccs_correct_path(aggregated_name))
464                  return -EINVAL;                  return -EINVAL;
465          saved_original_name = ccs_get_name(original_name);          e.original_name = ccs_get_name(original_name);
466          saved_aggregated_name = ccs_get_name(aggregated_name);          e.aggregated_name = ccs_get_name(aggregated_name);
467          if (!saved_original_name || !saved_aggregated_name) {          if (!e.original_name || !e.aggregated_name ||
468                  ccs_put_name(saved_original_name);              e.aggregated_name->is_patterned) /* No patterns allowed. */
469                  ccs_put_name(saved_aggregated_name);                  goto out;
470                  return -ENOMEM;          error = ccs_update_policy(&e.head, sizeof(e), is_delete,
471          }                                    CCS_ID_AGGREGATOR,
472          if (!is_delete)                                    ccs_same_aggregator_entry);
473                  entry = kzalloc(sizeof(*entry), GFP_KERNEL);   out:
474          mutex_lock(&ccs_policy_lock);          ccs_put_name(e.original_name);
475          list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {          ccs_put_name(e.aggregated_name);
                 if (ptr->original_name != saved_original_name ||  
                     ptr->aggregated_name != saved_aggregated_name)  
                         continue;  
                 ptr->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {  
                 entry->original_name = saved_original_name;  
                 saved_original_name = NULL;  
                 entry->aggregated_name = saved_aggregated_name;  
                 saved_aggregated_name = NULL;  
                 list_add_tail_rcu(&entry->list, &ccs_aggregator_list);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
         ccs_put_name(saved_original_name);  
         ccs_put_name(saved_aggregated_name);  
         kfree(entry);  
476          return error;          return error;
477  }  }
478    
479  /**  /**
480   * ccs_read_aggregator_policy - Read "struct ccs_aggregator_entry" list.   * ccs_write_aggregator - Write "struct ccs_aggregator" list.
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  *  
  * Caller holds srcu_read_lock(&ccs_ss).  
  */  
 bool ccs_read_aggregator_policy(struct ccs_io_buffer *head)  
 {  
         struct list_head *pos;  
         bool done = true;  
         list_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) {  
                 struct ccs_aggregator_entry *ptr;  
                 ptr = list_entry(pos, struct ccs_aggregator_entry, list);  
                 if (ptr->is_deleted)  
                         continue;  
                 done = ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n",  
                                      ptr->original_name->name,  
                                      ptr->aggregated_name->name);  
                 if (!done)  
                         break;  
         }  
         return done;  
 }  
   
 /**  
  * ccs_write_aggregator_policy - Write "struct ccs_aggregator_entry" list.  
481   *   *
482   * @data:      String to parse.   * @data:      String to parse.
483   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
484   *   *
485   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
486   */   */
487  int ccs_write_aggregator_policy(char *data, const bool is_delete)  int ccs_write_aggregator(char *data, const bool is_delete, const u8 flags)
488  {  {
489          char *cp = strchr(data, ' ');          char *w[2];
490          if (!cp)          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
491                  return -EINVAL;                  return -EINVAL;
492          *cp++ = '\0';          return ccs_update_aggregator_entry(w[0], w[1], is_delete);
         return ccs_update_aggregator_entry(data, cp, is_delete);  
493  }  }
494    
495  /* Domain create/delete handler. */  /* Domain create/delete handler. */
# Line 576  int ccs_delete_domain(char *domainname) Line 507  int ccs_delete_domain(char *domainname)
507          struct ccs_path_info name;          struct ccs_path_info name;
508          name.name = domainname;          name.name = domainname;
509          ccs_fill_path_info(&name);          ccs_fill_path_info(&name);
510          mutex_lock(&ccs_policy_lock);          if (mutex_lock_interruptible(&ccs_policy_lock))
511                    return 0;
512          /* Is there an active domain? */          /* Is there an active domain? */
513          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {
514                  /* Never delete ccs_kernel_domain */                  /* Never delete ccs_kernel_domain */
# Line 593  int ccs_delete_domain(char *domainname) Line 525  int ccs_delete_domain(char *domainname)
525  }  }
526    
527  /**  /**
528   * ccs_find_or_assign_new_domain - Create a domain.   * ccs_assign_domain - Create a domain.
529   *   *
530   * @domainname: The name of domain.   * @domainname: The name of domain.
531   * @profile:    Profile number to assign if the domain was newly created.   * @profile:    Profile number to assign if the domain was newly created.
532   *   *
533   * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.   * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
534   */   */
535  struct ccs_domain_info *ccs_find_or_assign_new_domain(const char *domainname,  struct ccs_domain_info *ccs_assign_domain(const char *domainname,
536                                                        const u8 profile)                                            const u8 profile)
537  {  {
538          struct ccs_domain_info *entry;          struct ccs_domain_info e = { };
539          struct ccs_domain_info *domain;          struct ccs_domain_info *entry = NULL;
         const struct ccs_path_info *saved_domainname;  
540          bool found = false;          bool found = false;
541    
542          if (!ccs_is_correct_domain(domainname))          if (!ccs_correct_domain(domainname))
543                  return NULL;                  return NULL;
544          saved_domainname = ccs_get_name(domainname);          e.profile = profile;
545          if (!saved_domainname)          e.domainname = ccs_get_name(domainname);
546            if (!e.domainname)
547                  return NULL;                  return NULL;
548          entry = kzalloc(sizeof(*entry), GFP_KERNEL);          if (mutex_lock_interruptible(&ccs_policy_lock))
549          mutex_lock(&ccs_policy_lock);                  goto out;
550          list_for_each_entry_rcu(domain, &ccs_domain_list, list) {          list_for_each_entry_rcu(entry, &ccs_domain_list, list) {
551                  if (domain->is_deleted ||                  if (entry->is_deleted ||
552                      ccs_pathcmp(saved_domainname, domain->domainname))                      ccs_pathcmp(e.domainname, entry->domainname))
553                          continue;                          continue;
554                  found = true;                  found = true;
555                  break;                  break;
556          }          }
557          if (!found && ccs_memory_ok(entry, sizeof(*entry))) {          if (!found) {
558                  INIT_LIST_HEAD(&entry->acl_info_list);                  entry = ccs_commit_ok(&e, sizeof(e));
559                  entry->domainname = saved_domainname;                  if (entry) {
560                  saved_domainname = NULL;                          INIT_LIST_HEAD(&entry->acl_info_list);
561                  entry->profile = profile;                          list_add_tail_rcu(&entry->list, &ccs_domain_list);
562                  list_add_tail_rcu(&entry->list, &ccs_domain_list);                          found = true;
                 domain = entry;  
                 entry = NULL;  
                 found = true;  
         }  
         mutex_unlock(&ccs_policy_lock);  
         ccs_put_name(saved_domainname);  
         kfree(entry);  
         return found ? domain : NULL;  
 }  
   
 /**  
  * ccs_get_argv0 - Get argv[0].  
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
  *  
  * Returns true on success, false otherwise.  
  */  
 static bool ccs_get_argv0(struct ccs_execve_entry *ee)  
 {  
         struct linux_binprm *bprm = ee->bprm;  
         char *arg_ptr = ee->tmp;  
         int arg_len = 0;  
         unsigned long pos = bprm->p;  
         int offset = pos % PAGE_SIZE;  
         bool done = false;  
         if (!bprm->argc)  
                 goto out;  
         while (1) {  
                 if (!ccs_dump_page(bprm, pos, &ee->dump))  
                         goto out;  
                 pos += PAGE_SIZE - offset;  
                 /* Read. */  
                 while (offset < PAGE_SIZE) {  
                         const char *kaddr = ee->dump.data;  
                         const unsigned char c = kaddr[offset++];  
                         if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {  
                                 if (c == '\\') {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = '\\';  
                                 } else if (c == '/') {  
                                         arg_len = 0;  
                                 } else if (c > ' ' && c < 127) {  
                                         arg_ptr[arg_len++] = c;  
                                 } else {  
                                         arg_ptr[arg_len++] = '\\';  
                                         arg_ptr[arg_len++] = (c >> 6) + '0';  
                                         arg_ptr[arg_len++]  
                                                 = ((c >> 3) & 7) + '0';  
                                         arg_ptr[arg_len++] = (c & 7) + '0';  
                                 }  
                         } else {  
                                 arg_ptr[arg_len] = '\0';  
                                 done = true;  
                                 break;  
                         }  
563                  }                  }
                 offset = 0;  
                 if (done)  
                         break;  
564          }          }
565          return true;          mutex_unlock(&ccs_policy_lock);
566   out:   out:
567          return false;          ccs_put_name(e.domainname);
568            return found ? entry : NULL;
569  }  }
570    
571  /**  /**
572   * ccs_find_next_domain - Find a domain.   * ccs_find_next_domain - Find a domain.
573   *   *
574   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve".
575   *   *
576   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
577   *   *
578   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
579   */   */
580  static int ccs_find_next_domain(struct ccs_execve_entry *ee)  static int ccs_find_next_domain(struct ccs_execve *ee)
581  {  {
582          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
583          const struct ccs_path_info *handler = ee->handler;          const struct ccs_path_info *handler = ee->handler;
584          struct ccs_domain_info *domain = NULL;          struct ccs_domain_info *domain = NULL;
585          const char *old_domain_name = r->domain->domainname->name;          struct ccs_domain_info * const old_domain = ccs_current_domain();
586            const char *old_domain_name = old_domain->domainname->name;
587          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
588          const u8 mode = r->mode;          struct task_struct *task = current;
589          const bool is_enforce = (mode == 3);          const u32 ccs_flags = task->ccs_flags;
590          const u32 ccs_flags = current->ccs_flags;          struct ccs_path_info rn = { }; /* real name */
         char *new_domain_name = NULL;  
         struct ccs_path_info rn; /* real name */  
591          struct ccs_path_info ln; /* last name */          struct ccs_path_info ln; /* last name */
592          int retval;          int retval;
593            bool need_kfree = false;
594            bool domain_created = false;
595            ln.name = ccs_last_word(old_domain_name);
596            ccs_fill_path_info(&ln);
597   retry:   retry:
598          current->ccs_flags = ccs_flags;          current->ccs_flags = ccs_flags;
599          r->cond = NULL;          r->cond = NULL;
600            if (need_kfree) {
601                    kfree(rn.name);
602                    need_kfree = false;
603            }
604    
605          /* Get symlink's pathname of program. */          /* Get symlink's pathname of program. */
606          retval = ccs_symlink_path(bprm->filename, ee);          retval = ccs_symlink_path(bprm->filename, &rn);
607          if (retval < 0)          if (retval < 0)
608                  goto out;                  goto out;
609            need_kfree = true;
         rn.name = ee->program_path;  
         ccs_fill_path_info(&rn);  
         ln.name = ccs_get_last_name(r->domain);  
         ccs_fill_path_info(&ln);  
610    
611          if (handler) {          if (handler) {
612                  if (ccs_pathcmp(&rn, handler)) {                  if (ccs_pathcmp(&rn, handler)) {
# Line 740  static int ccs_find_next_domain(struct c Line 619  static int ccs_find_next_domain(struct c
619                          }                          }
620                          goto out;                          goto out;
621                  }                  }
622                  goto calculate_domain;          } else {
623          }                  struct ccs_aggregator *ptr;
624                    /* Check 'aggregator' directive. */
625          /* Compare basename of program_path and argv[0] */                  list_for_each_entry_rcu(ptr,
626          r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_ARGV0);                                          &ccs_policy_list[CCS_ID_AGGREGATOR],
627          if (bprm->argc > 0 && r->mode) {                                          head.list) {
628                  char *base_argv0 = ee->tmp;                          if (ptr->head.is_deleted ||
                 const char *base_filename;  
                 retval = -ENOMEM;  
                 if (!ccs_get_argv0(ee))  
                         goto out;  
                 base_filename = strrchr(ee->program_path, '/');  
                 if (!base_filename)  
                         base_filename = ee->program_path;  
                 else  
                         base_filename++;  
                 if (strcmp(base_argv0, base_filename)) {  
                         retval = ccs_check_argv0_perm(r, &rn, base_argv0);  
                         if (retval == 1)  
                                 goto retry;  
                         if (retval < 0)  
                                 goto out;  
                 }  
         }  
   
         /* Check 'aggregator' directive. */  
         {  
                 struct ccs_aggregator_entry *ptr;  
                 /* Is this program allowed to be aggregated? */  
                 list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) {  
                         if (ptr->is_deleted ||  
629                              !ccs_path_matches_pattern(&rn, ptr->original_name))                              !ccs_path_matches_pattern(&rn, ptr->original_name))
630                                  continue;                                  continue;
631                          strncpy(ee->program_path, ptr->aggregated_name->name,                          kfree(rn.name);
632                                  CCS_MAX_PATHNAME_LEN - 1);                          need_kfree = false;
633                          ccs_fill_path_info(&rn);                          /* This is OK because it is read only. */
634                            rn = *ptr->aggregated_name;
635                          break;                          break;
636                  }                  }
         }  
637    
638          /* Check execute permission. */                  /* Check execute permission. */
639          r->mode = mode;                  retval = ccs_path_permission(r, CCS_TYPE_EXECUTE, &rn);
640          retval = ccs_check_exec_perm(r, &rn);                  if (retval == CCS_RETRY_REQUEST)
641          if (retval == 1)                          goto retry;
642                  goto retry;                  if (retval < 0)
643          if (retval < 0)                          goto out;
644                  goto out;          }
645    
646   calculate_domain:          /* Calculate domain to transit to. */
647          new_domain_name = ee->tmp;          if (ccs_domain_initializer(old_domain->domainname, &rn, &ln)) {
         if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) {  
648                  /* Transit to the child of ccs_kernel_domain domain. */                  /* Transit to the child of ccs_kernel_domain domain. */
649                  snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, CCS_ROOT_NAME " " "%s",
650                           ROOT_NAME " " "%s", ee->program_path);                           rn.name);
651          } else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) {          } else if (old_domain == &ccs_kernel_domain && !ccs_policy_loaded) {
652                  /*                  /*
653                   * Needn't to transit from kernel domain before starting                   * Needn't to transit from kernel domain before starting
654                   * /sbin/init. But transit from kernel domain if executing                   * /sbin/init. But transit from kernel domain if executing
655                   * initializers because they might start before /sbin/init.                   * initializers because they might start before /sbin/init.
656                   */                   */
657                  domain = r->domain;                  domain = old_domain;
658          } else if (ccs_is_domain_keeper(r->domain->domainname, &rn, &ln)) {          } else if (ccs_domain_keeper(old_domain->domainname, &rn, &ln)) {
659                  /* Keep current domain. */                  /* Keep current domain. */
660                  domain = r->domain;                  domain = old_domain;
661          } else {          } else {
662                  /* Normal domain transition. */                  /* Normal domain transition. */
663                  snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
664                           "%s %s", old_domain_name, ee->program_path);                           old_domain_name, rn.name);
665          }          }
666          if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN)          if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10)
667                  goto done;                  goto done;
668          domain = ccs_find_domain(new_domain_name);          domain = ccs_find_domain(ee->tmp);
669          if (domain)          if (domain)
670                  goto done;                  goto done;
671          if (is_enforce) {          if (r->mode == CCS_CONFIG_ENFORCING) {
672                  int error = ccs_check_supervisor(r,                  int error = ccs_supervisor(r, "# wants to create domain\n"
673                                                   "# wants to create domain\n"                                             "%s\n", ee->tmp);
674                                                   "%s\n", new_domain_name);                  if (error == CCS_RETRY_REQUEST)
                 if (error == 1)  
675                          goto retry;                          goto retry;
676                  if (error < 0)                  if (error < 0)
677                          goto done;                          goto done;
678          }          }
679          domain = ccs_find_or_assign_new_domain(new_domain_name, r->profile);          domain = ccs_assign_domain(ee->tmp, r->profile);
680          if (domain)          if (domain)
681                  ccs_audit_domain_creation_log(r->domain);                  domain_created = true;
682   done:   done:
683          if (!domain) {          if (!domain) {
684                  printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",                  retval = (r->mode == CCS_CONFIG_ENFORCING) ? -EPERM : 0;
685                         new_domain_name);                  if (!old_domain->domain_transition_failed) {
686                  if (is_enforce)                          old_domain->domain_transition_failed = true;
687                          retval = -EPERM;                          r->granted = false;
688                  else {                          ccs_write_log(r, CCS_KEYWORD_TRANSITION_FAILED "\n");
689                          retval = 0;                          printk(KERN_WARNING
690                          r->domain->domain_transition_failed = true;                                 "ERROR: Domain '%s' not defined.\n", ee->tmp);
691                  }                  }
692          } else {          } else {
693                  retval = 0;                  retval = 0;
694          }          }
695   out:          if (!retval && handler)
696                    ccs_audit_execute_handler_log(ee);
697            /*
698             * Tell GC that I started execve().
699             * Also, tell open_exec() to check read permission.
700             */
701            task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
702            /*
703             * Make task->ccs_flags visible to GC before changing
704             * task->ccs_domain_info .
705             */
706            smp_mb();
707            /*
708             * Proceed to the next domain in order to allow reaching via PID.
709             * It will be reverted if execve() failed. Reverting is not good.
710             * But it is better than being unable to reach via PID in interactive
711             * enforcing mode.
712             */
713          if (domain)          if (domain)
714                  r->domain = domain;                  task->ccs_domain_info = domain;
715            if (domain_created)
716                    ccs_audit_domain_creation_log();
717     out:
718            if (need_kfree)
719                    kfree(rn.name);
720          return retval;          return retval;
721  }  }
722    
723  /**  /**
724   * ccs_check_environ - Check permission for environment variable names.   * ccs_environ - Check permission for environment variable names.
725   *   *
726   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve".
727   *   *
728   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
729   */   */
730  static int ccs_check_environ(struct ccs_execve_entry *ee)  static int ccs_environ(struct ccs_execve *ee)
731  {  {
732          struct ccs_request_info *r = &ee->r;          struct ccs_request_info *r = &ee->r;
733          struct linux_binprm *bprm = ee->bprm;          struct linux_binprm *bprm = ee->bprm;
734          char *arg_ptr = ee->tmp;          /* env_page->data is allocated by ccs_dump_page(). */
735            struct ccs_page_dump env_page = { };
736            char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
737          int arg_len = 0;          int arg_len = 0;
738          unsigned long pos = bprm->p;          unsigned long pos = bprm->p;
739          int offset = pos % PAGE_SIZE;          int offset = pos % PAGE_SIZE;
# Line 864  static int ccs_check_environ(struct ccs_ Line 741  static int ccs_check_environ(struct ccs_
741          int envp_count = bprm->envc;          int envp_count = bprm->envc;
742          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */          /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
743          int error = -ENOMEM;          int error = -ENOMEM;
744            ee->r.type = CCS_MAC_ENVIRON;
745            ee->r.mode = ccs_get_mode(ccs_current_domain()->profile,
746                                      CCS_MAC_ENVIRON);
747          if (!r->mode || !envp_count)          if (!r->mode || !envp_count)
748                  return 0;                  return 0;
749            arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
750            if (!arg_ptr)
751                    goto out;
752          while (error == -ENOMEM) {          while (error == -ENOMEM) {
753                  if (!ccs_dump_page(bprm, pos, &ee->dump))                  if (!ccs_dump_page(bprm, pos, &env_page))
754                          goto out;                          goto out;
755                  pos += PAGE_SIZE - offset;                  pos += PAGE_SIZE - offset;
756                  /* Read. */                  /* Read. */
757                  while (argv_count && offset < PAGE_SIZE) {                  while (argv_count && offset < PAGE_SIZE) {
758                          const char *kaddr = ee->dump.data;                          if (!env_page.data[offset++])
                         if (!kaddr[offset++])  
759                                  argv_count--;                                  argv_count--;
760                  }                  }
761                  if (argv_count) {                  if (argv_count) {
# Line 881  static int ccs_check_environ(struct ccs_ Line 763  static int ccs_check_environ(struct ccs_
763                          continue;                          continue;
764                  }                  }
765                  while (offset < PAGE_SIZE) {                  while (offset < PAGE_SIZE) {
766                          const char *kaddr = ee->dump.data;                          const unsigned char c = env_page.data[offset++];
767                          const unsigned char c = kaddr[offset++];                          if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
                         if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) {  
768                                  if (c == '=') {                                  if (c == '=') {
769                                          arg_ptr[arg_len++] = '\0';                                          arg_ptr[arg_len++] = '\0';
770                                  } else if (c == '\\') {                                  } else if (c == '\\') {
# Line 903  static int ccs_check_environ(struct ccs_ Line 784  static int ccs_check_environ(struct ccs_
784                          }                          }
785                          if (c)                          if (c)
786                                  continue;                                  continue;
787                          if (ccs_check_env_perm(r, arg_ptr)) {                          if (ccs_env_perm(r, arg_ptr)) {
788                                  error = -EPERM;                                  error = -EPERM;
789                                  break;                                  break;
790                          }                          }
# Line 918  static int ccs_check_environ(struct ccs_ Line 799  static int ccs_check_environ(struct ccs_
799   out:   out:
800          if (r->mode != 3)          if (r->mode != 3)
801                  error = 0;                  error = 0;
802            kfree(env_page.data);
803            kfree(arg_ptr);
804          return error;          return error;
805  }  }
806    
# Line 968  static void ccs_unescape(unsigned char * Line 851  static void ccs_unescape(unsigned char *
851   *   *
852   * Returns number of directories to strip.   * Returns number of directories to strip.
853   */   */
854  static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)  static int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)
855  {  {
856          int depth = 0;          int depth = 0;
         /***** CRITICAL SECTION START *****/  
857          ccs_realpath_lock();          ccs_realpath_lock();
858          for (;;) {          for (;;) {
859                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {                  if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
# Line 986  static inline int ccs_root_depth(struct Line 868  static inline int ccs_root_depth(struct
868                  depth++;                  depth++;
869          }          }
870          ccs_realpath_unlock();          ccs_realpath_unlock();
         /***** CRITICAL SECTION END *****/  
871          return depth;          return depth;
872  }  }
873    
# Line 1003  static int ccs_get_root_depth(void) Line 884  static int ccs_get_root_depth(void)
884  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
885          struct path root;          struct path root;
886  #endif  #endif
         /***** CRITICAL SECTION START *****/  
887          read_lock(&current->fs->lock);          read_lock(&current->fs->lock);
888  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
889          root = current->fs->root;          root = current->fs->root;
# Line 1015  static int ccs_get_root_depth(void) Line 895  static int ccs_get_root_depth(void)
895          vfsmnt = mntget(current->fs->rootmnt);          vfsmnt = mntget(current->fs->rootmnt);
896  #endif  #endif
897          read_unlock(&current->fs->lock);          read_unlock(&current->fs->lock);
         /***** CRITICAL SECTION END *****/  
898          depth = ccs_root_depth(dentry, vfsmnt);          depth = ccs_root_depth(dentry, vfsmnt);
899  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
900          path_put(&root);          path_put(&root);
# Line 1026  static int ccs_get_root_depth(void) Line 905  static int ccs_get_root_depth(void)
905          return depth;          return depth;
906  }  }
907    
 static LIST_HEAD(ccs_execve_list);  
 static DEFINE_SPINLOCK(ccs_execve_list_lock);  
   
 /**  
  * ccs_allocate_execve_entry - Allocate memory for execve().  
  *  
  * Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise.  
  */  
 static struct ccs_execve_entry *ccs_allocate_execve_entry(void)  
 {  
         struct ccs_execve_entry *ee = kzalloc(sizeof(*ee), GFP_KERNEL);  
         if (!ee)  
                 return NULL;  
         ee->program_path = kzalloc(CCS_MAX_PATHNAME_LEN, GFP_KERNEL);  
         ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);  
         if (!ee->program_path || !ee->tmp) {  
                 kfree(ee->program_path);  
                 kfree(ee->tmp);  
                 kfree(ee);  
                 return NULL;  
         }  
         ee->srcu_idx = srcu_read_lock(&ccs_ss);  
         /* ee->dump->data is allocated by ccs_dump_page(). */  
         ee->task = current;  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&ccs_execve_list_lock);  
         list_add(&ee->list, &ccs_execve_list);  
         spin_unlock(&ccs_execve_list_lock);  
         /***** CRITICAL SECTION END *****/  
         return ee;  
 }  
   
 /**  
  * ccs_find_execve_entry - Find ccs_execve_entry of current process.  
  *  
  * Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise.  
  */  
 static struct ccs_execve_entry *ccs_find_execve_entry(void)  
 {  
         struct task_struct *task = current;  
         struct ccs_execve_entry *ee = NULL;  
         struct ccs_execve_entry *p;  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&ccs_execve_list_lock);  
         list_for_each_entry(p, &ccs_execve_list, list) {  
                 if (p->task != task)  
                         continue;  
                 ee = p;  
                 break;  
         }  
         spin_unlock(&ccs_execve_list_lock);  
         /***** CRITICAL SECTION END *****/  
         return ee;  
 }  
   
 /**  
  * ccs_free_execve_entry - Free memory for execve().  
  *  
  * @ee: Pointer to "struct ccs_execve_entry".  
  */  
 static void ccs_free_execve_entry(struct ccs_execve_entry *ee)  
 {  
         if (!ee)  
                 return;  
         /***** CRITICAL SECTION START *****/  
         spin_lock(&ccs_execve_list_lock);  
         list_del(&ee->list);  
         spin_unlock(&ccs_execve_list_lock);  
         /***** CRITICAL SECTION END *****/  
         kfree(ee->program_path);  
         kfree(ee->tmp);  
         kfree(ee->dump.data);  
         srcu_read_unlock(&ccs_ss, ee->srcu_idx);  
         kfree(ee);  
 }  
   
908  /**  /**
909   * ccs_try_alt_exec - Try to start execute handler.   * ccs_try_alt_exec - Try to start execute handler.
910   *   *
911   * @ee: Pointer to "struct ccs_execve_entry".   * @ee: Pointer to "struct ccs_execve".
912   *   *
913   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
914   */   */
915  static int ccs_try_alt_exec(struct ccs_execve_entry *ee)  static int ccs_try_alt_exec(struct ccs_execve *ee)
916  {  {
917          /*          /*
918           * Contents of modified bprm.           * Contents of modified bprm.
# Line 1155  static int ccs_try_alt_exec(struct ccs_e Line 958  static int ccs_try_alt_exec(struct ccs_e
958          struct task_struct *task = current;          struct task_struct *task = current;
959    
960          /* Close the requested program's dentry. */          /* Close the requested program's dentry. */
961            ee->obj.path1.dentry = NULL;
962            ee->obj.path1.mnt = NULL;
963            ee->obj.validate_done = false;
964          allow_write_access(bprm->file);          allow_write_access(bprm->file);
965          fput(bprm->file);          fput(bprm->file);
966          bprm->file = NULL;          bprm->file = NULL;
# Line 1202  static int ccs_try_alt_exec(struct ccs_e Line 1008  static int ccs_try_alt_exec(struct ccs_e
1008                           "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "                           "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
1009                           "sgid=%d fsuid=%d fsgid=%d state[0]=%u "                           "sgid=%d fsuid=%d fsgid=%d state[0]=%u "
1010                           "state[1]=%u state[2]=%u",                           "state[1]=%u state[2]=%u",
1011                           (pid_t) sys_getpid(), current_uid(), current_gid(),                           (pid_t) ccsecurity_exports.sys_getpid(),
1012                           current_euid(), current_egid(), current_suid(),                           current_uid(), current_gid(), current_euid(),
1013                           current_sgid(), current_fsuid(), current_fsgid(),                           current_egid(), current_suid(), current_sgid(),
1014                             current_fsuid(), current_fsgid(),
1015                           (u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16),                           (u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16),
1016                           (u8) (ccs_flags >> 8));                           (u8) (ccs_flags >> 8));
1017                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
# Line 1221  static int ccs_try_alt_exec(struct ccs_e Line 1028  static int ccs_try_alt_exec(struct ccs_e
1028                          kfree(exe);                          kfree(exe);
1029                  } else {                  } else {
1030                          exe = ee->tmp;                          exe = ee->tmp;
1031                          strncpy(ee->tmp, "<unknown>", CCS_EXEC_TMPSIZE - 1);                          snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
1032                          retval = copy_strings_kernel(1, &exe, bprm);                          retval = copy_strings_kernel(1, &exe, bprm);
1033                  }                  }
1034                  if (retval < 0)                  if (retval < 0)
# Line 1232  static int ccs_try_alt_exec(struct ccs_e Line 1039  static int ccs_try_alt_exec(struct ccs_e
1039          /* Set argv[1] */          /* Set argv[1] */
1040          {          {
1041                  char *cp = ee->tmp;                  char *cp = ee->tmp;
1042                  strncpy(ee->tmp, ccs_current_domain()->domainname->name,                  snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
1043                          CCS_EXEC_TMPSIZE - 1);                           ccs_current_domain()->domainname->name);
1044                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1045                  if (retval < 0)                  if (retval < 0)
1046                          goto out;                          goto out;
# Line 1243  static int ccs_try_alt_exec(struct ccs_e Line 1050  static int ccs_try_alt_exec(struct ccs_e
1050          /* Set argv[0] */          /* Set argv[0] */
1051          {          {
1052                  int depth = ccs_get_root_depth();                  int depth = ccs_get_root_depth();
1053                  char *cp = ee->program_path;                  int len = ee->handler->total_len + 1;
1054                  strncpy(cp, ee->handler->name, CCS_MAX_PATHNAME_LEN - 1);                  char *cp = kmalloc(len, CCS_GFP_FLAGS);
1055                    if (!cp) {
1056                            retval = -ENOMEM;
1057                            goto out;
1058                    }
1059                    ee->handler_path = cp;
1060                    memmove(cp, ee->handler->name, len);
1061                  ccs_unescape(cp);                  ccs_unescape(cp);
1062                  retval = -ENOENT;                  retval = -ENOENT;
1063                  if (!*cp || *cp != '/')                  if (!*cp || *cp != '/')
# Line 1256  static int ccs_try_alt_exec(struct ccs_e Line 1069  static int ccs_try_alt_exec(struct ccs_e
1069                                  goto out;                                  goto out;
1070                          depth--;                          depth--;
1071                  }                  }
1072                  memmove(ee->program_path, cp, strlen(cp) + 1);                  memmove(ee->handler_path, cp, strlen(cp) + 1);
1073                  cp = ee->program_path;                  cp = ee->handler_path;
1074                  retval = copy_strings_kernel(1, &cp, bprm);                  retval = copy_strings_kernel(1, &cp, bprm);
1075                  if (retval < 0)                  if (retval < 0)
1076                          goto out;                          goto out;
# Line 1269  static int ccs_try_alt_exec(struct ccs_e Line 1082  static int ccs_try_alt_exec(struct ccs_e
1082  #endif  #endif
1083  #endif  #endif
1084    
1085          /* OK, now restart the process with execute handler program's dentry. */          /*
1086          filp = open_exec(ee->program_path);           * OK, now restart the process with execute handler program's dentry.
1087             */
1088            filp = open_exec(ee->handler_path);
1089          if (IS_ERR(filp)) {          if (IS_ERR(filp)) {
1090                  retval = PTR_ERR(filp);                  retval = PTR_ERR(filp);
1091                  goto out;                  goto out;
1092          }          }
1093            ee->obj.path1.dentry = filp->f_dentry;
1094            ee->obj.path1.mnt = filp->f_vfsmnt;
1095          bprm->file = filp;          bprm->file = filp;
1096          bprm->filename = ee->program_path;          bprm->filename = ee->handler_path;
1097  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1098          bprm->interp = bprm->filename;          bprm->interp = bprm->filename;
1099  #endif  #endif
1100          retval = prepare_binprm(bprm);          retval = prepare_binprm(bprm);
1101          if (retval < 0)          if (retval < 0)
1102                  goto out;                  goto out;
1103          {          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1104                  /*          retval = ccs_find_next_domain(ee);
1105                   * Backup ee->program_path because ccs_find_next_domain() will          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
                  * overwrite ee->program_path and ee->tmp.  
                  */  
                 const int len = strlen(ee->program_path) + 1;  
                 char *cp = kzalloc(len, GFP_KERNEL);  
                 if (!cp) {  
                         retval = -ENOMEM;  
                         goto out;  
                 }  
                 memmove(cp, ee->program_path, len);  
                 bprm->filename = cp;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  
                 bprm->interp = bprm->filename;  
 #endif  
                 task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
                 retval = ccs_find_next_domain(ee);  
                 task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
                 /* Restore ee->program_path for search_binary_handler(). */  
                 memmove(ee->program_path, cp, len);  
                 bprm->filename = ee->program_path;  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)  
                 bprm->interp = bprm->filename;  
 #endif  
                 kfree(cp);  
         }  
1106   out:   out:
1107          return retval;          return retval;
1108  }  }
# Line 1317  static int ccs_try_alt_exec(struct ccs_e Line 1110  static int ccs_try_alt_exec(struct ccs_e
1110  /**  /**
1111   * ccs_find_execute_handler - Find an execute handler.   * ccs_find_execute_handler - Find an execute handler.
1112   *   *
1113   * @ee:   Pointer to "struct ccs_execve_entry".   * @ee:   Pointer to "struct ccs_execve".
1114   * @type: Type of execute handler.   * @type: Type of execute handler.
1115   *   *
1116   * Returns true if found, false otherwise.   * Returns true if found, false otherwise.
1117   *   *
1118   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
1119   */   */
1120  static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,  static bool ccs_find_execute_handler(struct ccs_execve *ee, const u8 type)
                                      const u8 type)  
1121  {  {
1122          struct task_struct *task = current;          struct task_struct *task = current;
1123          const struct ccs_domain_info *domain = ccs_current_domain();          const struct ccs_domain_info * const domain = ccs_current_domain();
1124          struct ccs_acl_info *ptr;          struct ccs_acl_info *ptr;
1125          bool found = false;          bool found = false;
1126          /*          /*
# Line 1337  static bool ccs_find_execute_handler(str Line 1129  static bool ccs_find_execute_handler(str
1129           */           */
1130          if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)          if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
1131                  return false;                  return false;
1132          list_for_each_entry(ptr, &domain->acl_info_list, list) {          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
1133                  struct ccs_execute_handler_record *acl;                  struct ccs_execute_handler *acl;
1134                  if (ptr->type != type)                  if (ptr->type != type)
1135                          continue;                          continue;
1136                  acl = container_of(ptr, struct ccs_execute_handler_record,                  acl = container_of(ptr, struct ccs_execute_handler, head);
                                    head);  
1137                  ee->handler = acl->handler;                  ee->handler = acl->handler;
1138                    ee->handler_type = type;
1139                  found = true;                  found = true;
1140                  break;                  break;
1141          }          }
# Line 1363  bool ccs_dump_page(struct linux_binprm * Line 1155  bool ccs_dump_page(struct linux_binprm *
1155                     struct ccs_page_dump *dump)                     struct ccs_page_dump *dump)
1156  {  {
1157          struct page *page;          struct page *page;
1158          /* dump->data is released by ccs_free_execve_entry(). */          /* dump->data is released by ccs_finish_execve(). */
1159          if (!dump->data) {          if (!dump->data) {
1160                  dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);                  dump->data = kzalloc(PAGE_SIZE, CCS_GFP_FLAGS);
1161                  if (!dump->data)                  if (!dump->data)
1162                          return false;                          return false;
1163          }          }
# Line 1373  bool ccs_dump_page(struct linux_binprm * Line 1165  bool ccs_dump_page(struct linux_binprm *
1165  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1166          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1167                  return false;                  return false;
1168  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU)  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1169            if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1170                    return false;
1171    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1172          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)          if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1173                  return false;                  return false;
1174  #else  #else
# Line 1388  bool ccs_dump_page(struct linux_binprm * Line 1183  bool ccs_dump_page(struct linux_binprm *
1183                   */                   */
1184                  char *kaddr = kmap_atomic(page, KM_USER0);                  char *kaddr = kmap_atomic(page, KM_USER0);
1185                  dump->page = page;                  dump->page = page;
1186                  memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset);                  memcpy(dump->data + offset, kaddr + offset,
1187                           PAGE_SIZE - offset);
1188                  kunmap_atomic(kaddr, KM_USER0);                  kunmap_atomic(kaddr, KM_USER0);
1189          }          }
1190          /* Same with put_arg_page(page) in fs/exec.c */          /* Same with put_arg_page(page) in fs/exec.c */
1191  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1192          put_page(page);          put_page(page);
1193  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU)  #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1194            put_page(page);
1195    #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1196          put_page(page);          put_page(page);
1197  #endif  #endif
1198          return true;          return true;
1199  }  }
1200    
1201  /**  /**
  * ccs_fetch_next_domain - Fetch next_domain from the list.  
  *  
  * Returns pointer to "struct ccs_domain_info" which will be used if execve()  
  * succeeds. This function does not return NULL.  
  */  
 struct ccs_domain_info *ccs_fetch_next_domain(void)  
 {  
         struct ccs_execve_entry *ee = ccs_find_execve_entry();  
         struct ccs_domain_info *next_domain = NULL;  
         if (ee)  
                 next_domain = ee->r.domain;  
         if (!next_domain)  
                 next_domain = ccs_current_domain();  
         return next_domain;  
 }  
   
 /**  
1202   * ccs_start_execve - Prepare for execve() operation.   * ccs_start_execve - Prepare for execve() operation.
1203   *   *
1204   * @bprm: Pointer to "struct linux_binprm".   * @bprm: Pointer to "struct linux_binprm".
1205     * @eep:  Pointer to "struct ccs_execve *".
1206   *   *
1207   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
1208   */   */
1209  int ccs_start_execve(struct linux_binprm *bprm)  static int ccs_start_execve(struct linux_binprm *bprm,
1210                                struct ccs_execve **eep)
1211  {  {
1212          int retval;          int retval;
1213          struct task_struct *task = current;          struct task_struct *task = current;
1214          struct ccs_execve_entry *ee = ccs_allocate_execve_entry();          struct ccs_execve *ee;
1215          if (!ccs_policy_loaded)          *eep = NULL;
1216                  ccs_load_policy(bprm->filename);          ee = kzalloc(sizeof(*ee), CCS_GFP_FLAGS);
1217          if (!ee)          if (!ee)
1218                  return -ENOMEM;                  return -ENOMEM;
1219          ccs_init_request_info(&ee->r, NULL, CCS_MAC_FOR_FILE);          ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
1220            if (!ee->tmp) {
1221                    kfree(ee);
1222                    return -ENOMEM;
1223            }
1224            ee->reader_idx = ccs_read_lock();
1225            /* ee->dump->data is allocated by ccs_dump_page(). */
1226            ee->previous_domain = task->ccs_domain_info;
1227            /* Clear manager flag. */
1228            task->ccs_flags &= ~CCS_TASK_IS_MANAGER;
1229            *eep = ee;
1230            ccs_init_request_info(&ee->r, CCS_MAC_FILE_EXECUTE);
1231          ee->r.ee = ee;          ee->r.ee = ee;
1232          ee->bprm = bprm;          ee->bprm = bprm;
1233          ee->r.obj = &ee->obj;          ee->r.obj = &ee->obj;
1234          ee->obj.path1_dentry = bprm->file->f_dentry;          ee->obj.path1.dentry = bprm->file->f_dentry;
1235          ee->obj.path1_vfsmnt = bprm->file->f_vfsmnt;          ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1236          /* Clear manager flag. */          /*
1237          task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;           * No need to call ccs_environ() for execute handler because envp[] is
1238          if (ccs_find_execute_handler(ee, TYPE_EXECUTE_HANDLER)) {           * moved to argv[].
1239                  retval = ccs_try_alt_exec(ee);           */
1240                  if (!retval)          if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER))
1241                          ccs_audit_execute_handler_log(ee, true);                  return ccs_try_alt_exec(ee);
                 goto ok;  
         }  
1242          retval = ccs_find_next_domain(ee);          retval = ccs_find_next_domain(ee);
1243          if (retval != -EPERM)          if (retval == -EPERM) {
1244                  goto ok;                  if (ccs_find_execute_handler(ee,
1245          if (ccs_find_execute_handler(ee, TYPE_DENIED_EXECUTE_HANDLER)) {                                               CCS_TYPE_DENIED_EXECUTE_HANDLER))
1246                  retval = ccs_try_alt_exec(ee);                          return ccs_try_alt_exec(ee);
                 if (!retval)  
                         ccs_audit_execute_handler_log(ee, false);  
1247          }          }
1248   ok:          if (!retval)
1249          if (retval < 0)                  retval = ccs_environ(ee);
                 goto out;  
         ee->r.mode = ccs_check_flags(ee->r.domain, CCS_MAC_FOR_ENV);  
         retval = ccs_check_environ(ee);  
         if (retval < 0)  
                 goto out;  
         task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC;  
         retval = 0;  
  out:  
         if (retval)  
                 ccs_finish_execve(retval);  
1250          return retval;          return retval;
1251  }  }
1252    
# Line 1474  int ccs_start_execve(struct linux_binprm Line 1254  int ccs_start_execve(struct linux_binprm
1254   * ccs_finish_execve - Clean up execve() operation.   * ccs_finish_execve - Clean up execve() operation.
1255   *   *
1256   * @retval: Return code of an execve() operation.   * @retval: Return code of an execve() operation.
1257     * @ee:     Pointer to "struct ccs_execve".
1258   *   *
1259   * Caller holds srcu_read_lock(&ccs_ss).   * Caller holds ccs_read_lock().
1260   */   */
1261  void ccs_finish_execve(int retval)  static void ccs_finish_execve(int retval, struct ccs_execve *ee)
1262  {  {
1263          struct task_struct *task = current;          struct task_struct *task = current;
         struct ccs_execve_entry *ee = ccs_find_execve_entry();  
         task->ccs_flags &= ~CCS_CHECK_READ_FOR_OPEN_EXEC;  
1264          if (!ee)          if (!ee)
1265                  return;                  return;
1266          if (retval < 0)          if (retval < 0) {
1267                  goto out;                  task->ccs_domain_info = ee->previous_domain;
1268          /* Proceed to next domain if execution suceeded. */                  /*
1269          task->ccs_domain_info = ee->r.domain;                   * Make task->ccs_domain_info visible to GC before changing
1270          /* Mark the current process as execute handler. */                   * task->ccs_flags .
1271          if (ee->handler)                   */
1272                  task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;                  smp_mb();
1273          /* Mark the current process as normal process. */          } else {
1274          else                  /* Mark the current process as execute handler. */
1275                  task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;                  if (ee->handler)
1276   out:                          task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1277          ccs_free_execve_entry(ee);                  /* Mark the current process as normal process. */
1278                    else
1279                            task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1280            }
1281            /* Tell GC that I finished execve(). */
1282            task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1283            ccs_read_unlock(ee->reader_idx);
1284            kfree(ee->handler_path);
1285            kfree(ee->tmp);
1286            kfree(ee->dump.data);
1287            kfree(ee);
1288    }
1289    
1290    /**
1291     * ccs_may_transit - Check permission and do domain transition without execve().
1292     *
1293     * @domainname: Domainname to transit to.
1294     * @pathname: Pathname to check.
1295     *
1296     * Returns 0 on success, negative value otherwise.
1297     *
1298     * Caller holds ccs_read_lock().
1299     */
1300    int ccs_may_transit(const char *domainname, const char *pathname)
1301    {
1302            struct ccs_path_info name;
1303            struct ccs_request_info r;
1304            struct ccs_domain_info *domain;
1305            int error;
1306            bool domain_created = false;
1307            name.name = pathname;
1308            ccs_fill_path_info(&name);
1309            /* Check allow_transit permission. */
1310            ccs_init_request_info(&r, CCS_MAC_FILE_TRANSIT);
1311            error = ccs_path_permission(&r, CCS_TYPE_TRANSIT, &name);
1312            if (error)
1313                    return error;
1314            /* Check destination domain. */
1315            domain = ccs_find_domain(domainname);
1316            if (!domain && r.mode != CCS_CONFIG_ENFORCING &&
1317                strlen(domainname) < CCS_EXEC_TMPSIZE - 10) {
1318                    domain = ccs_assign_domain(domainname, r.profile);
1319                    if (domain)
1320                            domain_created = true;
1321            }
1322            if (domain) {
1323                    error = 0;
1324                    current->ccs_domain_info = domain;
1325                    if (domain_created)
1326                            ccs_audit_domain_creation_log();
1327            } else {
1328                    error = -ENOENT;
1329            }
1330            return error;
1331    }
1332    
1333    static int __ccs_search_binary_handler(struct linux_binprm *bprm,
1334                                           struct pt_regs *regs)
1335    {
1336            struct ccs_execve *ee;
1337            int retval;
1338            if (!ccs_policy_loaded)
1339                    ccsecurity_exports.load_policy(bprm->filename);
1340            retval = ccs_start_execve(bprm, &ee);
1341            if (!retval)
1342                    retval = search_binary_handler(bprm, regs);
1343            ccs_finish_execve(retval, ee);
1344            return retval;
1345    }
1346    
1347    void __init ccs_domain_init(void)
1348    {
1349            ccsecurity_ops.search_binary_handler = __ccs_search_binary_handler;
1350  }  }

Legend:
Removed from v.2738  
changed lines
  Added in v.3710

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