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

Subversion リポジトリの参照

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

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

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

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

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