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

Subversion リポジトリの参照

Annotation of /branches/ccs-patch/security/ccsecurity/domain.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3109 - (hide annotations) (download) (as text)
Fri Oct 16 05:02:23 2009 UTC (14 years, 7 months ago) by kumaneko
Original Path: trunk/1.7.x/ccs-patch/security/ccsecurity/domain.c
File MIME type: text/x-csrc
File size: 33232 byte(s)


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

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