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

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3128 - (hide annotations) (download) (as text)
Mon Nov 2 06:16:20 2009 UTC (14 years, 6 months ago) by kumaneko
Original Path: trunk/1.7.x/ccs-patch/security/ccsecurity/domain.c
File MIME type: text/x-csrc
File size: 33392 byte(s)
Fix buffer contention.
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 3128 * Version: 1.7.1-pre 2009/11/02
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 kumaneko 3128 /* env_page->data is allocated by ccs_dump_page(). */
695     struct ccs_page_dump env_page = { };
696     char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
697 kumaneko 581 int arg_len = 0;
698     unsigned long pos = bprm->p;
699 kumaneko 1064 int offset = pos % PAGE_SIZE;
700 kumaneko 581 int argv_count = bprm->argc;
701     int envp_count = bprm->envc;
702 kumaneko 1052 /* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
703 kumaneko 581 int error = -ENOMEM;
704 kumaneko 1657 if (!r->mode || !envp_count)
705 kumaneko 1052 return 0;
706 kumaneko 3128 arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
707     if (!arg_ptr)
708     goto out;
709 kumaneko 581 while (error == -ENOMEM) {
710 kumaneko 3128 if (!ccs_dump_page(bprm, pos, &env_page))
711 kumaneko 1052 goto out;
712 kumaneko 986 pos += PAGE_SIZE - offset;
713 kumaneko 581 /* Read. */
714     while (argv_count && offset < PAGE_SIZE) {
715 kumaneko 3128 if (!env_page.data[offset++])
716 kumaneko 1052 argv_count--;
717 kumaneko 581 }
718 kumaneko 2037 if (argv_count) {
719     offset = 0;
720     continue;
721     }
722 kumaneko 581 while (offset < PAGE_SIZE) {
723 kumaneko 3128 const unsigned char c = env_page.data[offset++];
724 kumaneko 2932 if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
725 kumaneko 581 if (c == '=') {
726     arg_ptr[arg_len++] = '\0';
727     } else if (c == '\\') {
728     arg_ptr[arg_len++] = '\\';
729     arg_ptr[arg_len++] = '\\';
730     } else if (c > ' ' && c < 127) {
731     arg_ptr[arg_len++] = c;
732     } else {
733     arg_ptr[arg_len++] = '\\';
734     arg_ptr[arg_len++] = (c >> 6) + '0';
735 kumaneko 1052 arg_ptr[arg_len++]
736     = ((c >> 3) & 7) + '0';
737 kumaneko 581 arg_ptr[arg_len++] = (c & 7) + '0';
738     }
739     } else {
740     arg_ptr[arg_len] = '\0';
741     }
742 kumaneko 1052 if (c)
743     continue;
744 kumaneko 2922 if (ccs_env_perm(r, arg_ptr)) {
745 kumaneko 581 error = -EPERM;
746     break;
747     }
748     if (!--envp_count) {
749     error = 0;
750     break;
751     }
752     arg_len = 0;
753     }
754     offset = 0;
755     }
756     out:
757 kumaneko 1657 if (r->mode != 3)
758 kumaneko 1052 error = 0;
759 kumaneko 3128 kfree(env_page.data);
760 kumaneko 581 return error;
761     }
762 kumaneko 111
763 kumaneko 1052 /**
764 kumaneko 2002 * ccs_unescape - Unescape escaped string.
765 kumaneko 1052 *
766 kumaneko 2075 * @dest: String to unescape.
767 kumaneko 1052 *
768     * Returns nothing.
769     */
770 kumaneko 2002 static void ccs_unescape(unsigned char *dest)
771 kumaneko 708 {
772     unsigned char *src = dest;
773 kumaneko 1064 unsigned char c;
774     unsigned char d;
775     unsigned char e;
776 kumaneko 1747 while (1) {
777     c = *src++;
778     if (!c)
779     break;
780 kumaneko 708 if (c != '\\') {
781     *dest++ = c;
782     continue;
783     }
784     c = *src++;
785     if (c == '\\') {
786     *dest++ = c;
787 kumaneko 1052 continue;
788     }
789     if (c < '0' || c > '3')
790 kumaneko 708 break;
791 kumaneko 1052 d = *src++;
792     if (d < '0' || d > '7')
793     break;
794     e = *src++;
795     if (e < '0' || e > '7')
796     break;
797     *dest++ = ((c - '0') << 6) + ((d - '0') << 3) + (e - '0');
798 kumaneko 708 }
799     *dest = '\0';
800     }
801    
802 kumaneko 1052 /**
803 kumaneko 2002 * ccs_root_depth - Get number of directories to strip.
804 kumaneko 1052 *
805     * @dentry: Pointer to "struct dentry".
806     * @vfsmnt: Pointer to "struct vfsmount".
807     *
808     * Returns number of directories to strip.
809 kumaneko 1029 */
810 kumaneko 2002 static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt)
811 kumaneko 708 {
812 kumaneko 1029 int depth = 0;
813 kumaneko 1474 ccs_realpath_lock();
814 kumaneko 1029 for (;;) {
815     if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
816     /* Global root? */
817 kumaneko 1052 if (vfsmnt->mnt_parent == vfsmnt)
818     break;
819 kumaneko 1029 dentry = vfsmnt->mnt_mountpoint;
820     vfsmnt = vfsmnt->mnt_parent;
821     continue;
822     }
823     dentry = dentry->d_parent;
824     depth++;
825     }
826 kumaneko 1474 ccs_realpath_unlock();
827 kumaneko 1052 return depth;
828     }
829    
830     /**
831 kumaneko 2002 * ccs_get_root_depth - return the depth of root directory.
832 kumaneko 1052 *
833     * Returns number of directories to strip.
834     */
835 kumaneko 2002 static int ccs_get_root_depth(void)
836 kumaneko 1052 {
837     int depth;
838     struct dentry *dentry;
839     struct vfsmount *vfsmnt;
840     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
841     struct path root;
842     #endif
843     read_lock(&current->fs->lock);
844     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
845     root = current->fs->root;
846     path_get(&current->fs->root);
847     dentry = root.dentry;
848     vfsmnt = root.mnt;
849     #else
850     dentry = dget(current->fs->root);
851     vfsmnt = mntget(current->fs->rootmnt);
852     #endif
853     read_unlock(&current->fs->lock);
854 kumaneko 2002 depth = ccs_root_depth(dentry, vfsmnt);
855 kumaneko 1052 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
856 kumaneko 1029 path_put(&root);
857     #else
858 kumaneko 1052 dput(dentry);
859     mntput(vfsmnt);
860 kumaneko 1029 #endif
861     return depth;
862     }
863    
864 kumaneko 1052 /**
865 kumaneko 2002 * ccs_try_alt_exec - Try to start execute handler.
866 kumaneko 1052 *
867 kumaneko 2075 * @ee: Pointer to "struct ccs_execve_entry".
868 kumaneko 1052 *
869     * Returns 0 on success, negative value otherwise.
870     */
871 kumaneko 2037 static int ccs_try_alt_exec(struct ccs_execve_entry *ee)
872 kumaneko 1029 {
873 kumaneko 1005 /*
874     * Contents of modified bprm.
875     * The envp[] in original bprm is moved to argv[] so that
876     * the alternatively executed program won't be affected by
877 kumaneko 1064 * some dangerous environment variables like LD_PRELOAD.
878 kumaneko 1005 *
879     * modified bprm->argc
880     * = original bprm->argc + original bprm->envc + 7
881     * modified bprm->envc
882     * = 0
883     *
884     * modified bprm->argv[0]
885 kumaneko 1029 * = the program's name specified by execute_handler
886 kumaneko 1005 * modified bprm->argv[1]
887 kumaneko 2282 * = ccs_current_domain()->domainname->name
888 kumaneko 1005 * modified bprm->argv[2]
889     * = the current process's name
890     * modified bprm->argv[3]
891     * = the current process's information (e.g. uid/gid).
892     * modified bprm->argv[4]
893     * = original bprm->filename
894     * modified bprm->argv[5]
895     * = original bprm->argc in string expression
896     * modified bprm->argv[6]
897     * = original bprm->envc in string expression
898     * modified bprm->argv[7]
899     * = original bprm->argv[0]
900     * ...
901     * modified bprm->argv[bprm->argc + 6]
902     * = original bprm->argv[bprm->argc - 1]
903     * modified bprm->argv[bprm->argc + 7]
904     * = original bprm->envp[0]
905     * ...
906     * modified bprm->argv[bprm->envc + bprm->argc + 6]
907     * = original bprm->envp[bprm->envc - 1]
908     */
909 kumaneko 2037 struct linux_binprm *bprm = ee->bprm;
910 kumaneko 708 struct file *filp;
911     int retval;
912 kumaneko 1005 const int original_argc = bprm->argc;
913     const int original_envc = bprm->envc;
914     struct task_struct *task = current;
915 kumaneko 1032
916 kumaneko 1029 /* Close the requested program's dentry. */
917 kumaneko 3064 ee->obj.path1.dentry = NULL;
918     ee->obj.path1.mnt = NULL;
919     ee->obj.validate_done = false;
920 kumaneko 708 allow_write_access(bprm->file);
921     fput(bprm->file);
922     bprm->file = NULL;
923 kumaneko 1005
924 kumaneko 2037 /* Invalidate page dump cache. */
925     ee->dump.page = NULL;
926 kumaneko 1029
927 kumaneko 1005 /* Move envp[] to argv[] */
928     bprm->argc += bprm->envc;
929     bprm->envc = 0;
930    
931     /* Set argv[6] */
932     {
933 kumaneko 2037 char *cp = ee->tmp;
934     snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc);
935     retval = copy_strings_kernel(1, &cp, bprm);
936 kumaneko 1052 if (retval < 0)
937     goto out;
938 kumaneko 1005 bprm->argc++;
939     }
940    
941     /* Set argv[5] */
942     {
943 kumaneko 2037 char *cp = ee->tmp;
944     snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
945     retval = copy_strings_kernel(1, &cp, bprm);
946 kumaneko 1052 if (retval < 0)
947     goto out;
948 kumaneko 1005 bprm->argc++;
949     }
950    
951     /* Set argv[4] */
952     {
953 kumaneko 2075 retval = copy_strings_kernel(1, &bprm->filename, bprm);
954 kumaneko 1052 if (retval < 0)
955     goto out;
956 kumaneko 1005 bprm->argc++;
957     }
958    
959     /* Set argv[3] */
960     {
961 kumaneko 2037 char *cp = ee->tmp;
962 kumaneko 2282 const u32 ccs_flags = task->ccs_flags;
963 kumaneko 2037 snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
964 kumaneko 1052 "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
965     "sgid=%d fsuid=%d fsgid=%d state[0]=%u "
966     "state[1]=%u state[2]=%u",
967 kumaneko 2016 (pid_t) sys_getpid(), current_uid(), current_gid(),
968     current_euid(), current_egid(), current_suid(),
969     current_sgid(), current_fsuid(), current_fsgid(),
970 kumaneko 2282 (u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16),
971     (u8) (ccs_flags >> 8));
972 kumaneko 2037 retval = copy_strings_kernel(1, &cp, bprm);
973 kumaneko 1052 if (retval < 0)
974     goto out;
975 kumaneko 1005 bprm->argc++;
976     }
977    
978     /* Set argv[2] */
979     {
980 kumaneko 1052 char *exe = (char *) ccs_get_exe();
981 kumaneko 1005 if (exe) {
982     retval = copy_strings_kernel(1, &exe, bprm);
983 kumaneko 2711 kfree(exe);
984 kumaneko 1005 } else {
985 kumaneko 2037 exe = ee->tmp;
986 kumaneko 2932 snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
987 kumaneko 2037 retval = copy_strings_kernel(1, &exe, bprm);
988 kumaneko 1005 }
989 kumaneko 1052 if (retval < 0)
990     goto out;
991 kumaneko 1005 bprm->argc++;
992     }
993    
994     /* Set argv[1] */
995     {
996 kumaneko 2037 char *cp = ee->tmp;
997 kumaneko 2932 snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
998     ccs_current_domain()->domainname->name);
999 kumaneko 2037 retval = copy_strings_kernel(1, &cp, bprm);
1000 kumaneko 1052 if (retval < 0)
1001     goto out;
1002 kumaneko 1005 bprm->argc++;
1003     }
1004    
1005     /* Set argv[0] */
1006     {
1007 kumaneko 2037 int depth = ccs_get_root_depth();
1008 kumaneko 2932 int len = ee->handler->total_len + 1;
1009     char *cp = kmalloc(len, GFP_KERNEL);
1010     if (!cp) {
1011 kumaneko 2998 retval = -ENOMEM;
1012 kumaneko 2932 goto out;
1013     }
1014     ee->handler_path = cp;
1015     memmove(cp, ee->handler->name, len);
1016 kumaneko 2037 ccs_unescape(cp);
1017     retval = -ENOENT;
1018     if (!*cp || *cp != '/')
1019     goto out;
1020     /* Adjust root directory for open_exec(). */
1021     while (depth) {
1022     cp = strchr(cp + 1, '/');
1023     if (!cp)
1024     goto out;
1025     depth--;
1026     }
1027 kumaneko 2932 memmove(ee->handler_path, cp, strlen(cp) + 1);
1028     cp = ee->handler_path;
1029 kumaneko 2037 retval = copy_strings_kernel(1, &cp, bprm);
1030 kumaneko 1052 if (retval < 0)
1031     goto out;
1032 kumaneko 1005 bprm->argc++;
1033     }
1034 kumaneko 1052 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
1035     #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
1036 kumaneko 1007 bprm->argv_len = bprm->exec - bprm->p;
1037     #endif
1038 kumaneko 1052 #endif
1039 kumaneko 1005
1040 kumaneko 1029 /* OK, now restart the process with execute handler program's dentry. */
1041 kumaneko 2932 filp = open_exec(ee->handler_path);
1042 kumaneko 1005 if (IS_ERR(filp)) {
1043     retval = PTR_ERR(filp);
1044     goto out;
1045     }
1046 kumaneko 3064 ee->obj.path1.dentry = filp->f_dentry;
1047     ee->obj.path1.mnt = filp->f_vfsmnt;
1048 kumaneko 1052 bprm->file = filp;
1049 kumaneko 2932 bprm->filename = ee->handler_path;
1050 kumaneko 1052 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
1051 kumaneko 2076 bprm->interp = bprm->filename;
1052 kumaneko 708 #endif
1053 kumaneko 1029 retval = prepare_binprm(bprm);
1054 kumaneko 1052 if (retval < 0)
1055     goto out;
1056 kumaneko 2932 task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1057     retval = ccs_find_next_domain(ee);
1058     task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
1059 kumaneko 1005 out:
1060     return retval;
1061 kumaneko 708 }
1062 kumaneko 581
1063 kumaneko 1052 /**
1064 kumaneko 2002 * ccs_find_execute_handler - Find an execute handler.
1065 kumaneko 1052 *
1066 kumaneko 2037 * @ee: Pointer to "struct ccs_execve_entry".
1067 kumaneko 1052 * @type: Type of execute handler.
1068     *
1069 kumaneko 2075 * Returns true if found, false otherwise.
1070 kumaneko 2690 *
1071 kumaneko 2828 * Caller holds ccs_read_lock().
1072 kumaneko 1052 */
1073 kumaneko 2037 static bool ccs_find_execute_handler(struct ccs_execve_entry *ee,
1074     const u8 type)
1075 kumaneko 1029 {
1076 kumaneko 1064 struct task_struct *task = current;
1077 kumaneko 2282 const struct ccs_domain_info *domain = ccs_current_domain();
1078 kumaneko 2002 struct ccs_acl_info *ptr;
1079 kumaneko 2540 bool found = false;
1080 kumaneko 1064 /*
1081     * Don't use execute handler if the current process is
1082     * marked as execute handler to avoid infinite execute handler loop.
1083     */
1084 kumaneko 2282 if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER)
1085 kumaneko 2037 return false;
1086 kumaneko 2540 list_for_each_entry(ptr, &domain->acl_info_list, list) {
1087 kumaneko 2002 struct ccs_execute_handler_record *acl;
1088 kumaneko 1058 if (ptr->type != type)
1089 kumaneko 1052 continue;
1090 kumaneko 2002 acl = container_of(ptr, struct ccs_execute_handler_record,
1091     head);
1092 kumaneko 2037 ee->handler = acl->handler;
1093 kumaneko 2540 found = true;
1094     break;
1095 kumaneko 1029 }
1096 kumaneko 2540 return found;
1097 kumaneko 1029 }
1098    
1099 kumaneko 1052 /**
1100 kumaneko 2037 * ccs_dump_page - Dump a page to buffer.
1101 kumaneko 1657 *
1102 kumaneko 2037 * @bprm: Pointer to "struct linux_binprm".
1103     * @pos: Location to dump.
1104     * @dump: Poiner to "struct ccs_page_dump".
1105 kumaneko 1657 *
1106 kumaneko 2037 * Returns true on success, false otherwise.
1107 kumaneko 1657 */
1108 kumaneko 2037 bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
1109     struct ccs_page_dump *dump)
1110 kumaneko 1657 {
1111 kumaneko 2037 struct page *page;
1112 kumaneko 3056 /* dump->data is released by ccs_finish_execve(). */
1113 kumaneko 2037 if (!dump->data) {
1114 kumaneko 2711 dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL);
1115 kumaneko 2037 if (!dump->data)
1116     return false;
1117     }
1118     /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
1119     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1120     if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1121     return false;
1122 kumaneko 2996 #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1123 kumaneko 2346 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1124     return false;
1125 kumaneko 3018 #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1126     if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
1127     return false;
1128 kumaneko 2037 #else
1129     page = bprm->page[pos / PAGE_SIZE];
1130     #endif
1131     if (page != dump->page) {
1132     const unsigned int offset = pos % PAGE_SIZE;
1133     /*
1134     * Maybe kmap()/kunmap() should be used here.
1135     * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
1136     * So do I.
1137     */
1138     char *kaddr = kmap_atomic(page, KM_USER0);
1139     dump->page = page;
1140     memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset);
1141     kunmap_atomic(kaddr, KM_USER0);
1142     }
1143     /* Same with put_arg_page(page) in fs/exec.c */
1144     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU)
1145     put_page(page);
1146 kumaneko 2996 #elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU)
1147 kumaneko 2346 put_page(page);
1148 kumaneko 3018 #elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU)
1149     put_page(page);
1150 kumaneko 2037 #endif
1151     return true;
1152 kumaneko 1657 }
1153    
1154     /**
1155 kumaneko 2037 * ccs_start_execve - Prepare for execve() operation.
1156 kumaneko 1052 *
1157     * @bprm: Pointer to "struct linux_binprm".
1158 kumaneko 3056 * @eep: Pointer to "struct ccs_execve_entry *".
1159 kumaneko 1052 *
1160 kumaneko 2037 * Returns 0 on success, negative value otherwise.
1161 kumaneko 1052 */
1162 kumaneko 3056 int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve_entry **eep)
1163 kumaneko 115 {
1164 kumaneko 1657 int retval;
1165 kumaneko 1029 struct task_struct *task = current;
1166 kumaneko 3056 struct ccs_execve_entry *ee;
1167     *eep = NULL;
1168 kumaneko 2040 if (!ccs_policy_loaded)
1169 kumaneko 1657 ccs_load_policy(bprm->filename);
1170 kumaneko 3056 ee = kzalloc(sizeof(*ee), GFP_KERNEL);
1171 kumaneko 2037 if (!ee)
1172 kumaneko 1052 return -ENOMEM;
1173 kumaneko 3056 ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL);
1174     if (!ee->tmp) {
1175     kfree(ee);
1176     return -ENOMEM;
1177     }
1178     ee->reader_idx = ccs_read_lock();
1179     /* ee->dump->data is allocated by ccs_dump_page(). */
1180     ee->previous_domain = task->ccs_domain_info;
1181     /* Clear manager flag. */
1182     task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER;
1183     /* Tell GC that I started execve(). */
1184     task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
1185     /*
1186     * Make task->ccs_flags visible to GC before changing
1187     * task->ccs_domain_info .
1188     */
1189     smp_mb();
1190     *eep = ee;
1191 kumaneko 2943 ccs_init_request_info(&ee->r, NULL, CCS_MAC_FILE_EXECUTE);
1192 kumaneko 2037 ee->r.ee = ee;
1193     ee->bprm = bprm;
1194     ee->r.obj = &ee->obj;
1195 kumaneko 2915 ee->obj.path1.dentry = bprm->file->f_dentry;
1196     ee->obj.path1.mnt = bprm->file->f_vfsmnt;
1197 kumaneko 2892 if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER)) {
1198 kumaneko 2037 retval = ccs_try_alt_exec(ee);
1199 kumaneko 1052 if (!retval)
1200 kumaneko 2037 ccs_audit_execute_handler_log(ee, true);
1201 kumaneko 1052 goto ok;
1202 kumaneko 708 }
1203 kumaneko 2037 retval = ccs_find_next_domain(ee);
1204 kumaneko 1052 if (retval != -EPERM)
1205     goto ok;
1206 kumaneko 2892 if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) {
1207 kumaneko 2037 retval = ccs_try_alt_exec(ee);
1208 kumaneko 1052 if (!retval)
1209 kumaneko 2037 ccs_audit_execute_handler_log(ee, false);
1210 kumaneko 1052 }
1211     ok:
1212 kumaneko 1561 if (retval < 0)
1213 kumaneko 1052 goto out;
1214 kumaneko 2968 /*
1215     * Proceed to the next domain in order to allow reaching via PID.
1216     * It will be reverted if execve() failed. Reverting is not good.
1217     * But it is better than being unable to reach via PID in interactive
1218     * enforcing mode.
1219     */
1220     task->ccs_domain_info = ee->r.domain;
1221 kumaneko 2960 ee->r.mode = ccs_get_mode(ee->r.domain->profile, CCS_MAC_ENVIRON);
1222 kumaneko 2922 retval = ccs_environ(ee);
1223 kumaneko 1561 if (retval < 0)
1224 kumaneko 1052 goto out;
1225 kumaneko 2037 retval = 0;
1226     out:
1227     return retval;
1228     }
1229    
1230     /**
1231     * ccs_finish_execve - Clean up execve() operation.
1232 kumaneko 2076 *
1233     * @retval: Return code of an execve() operation.
1234 kumaneko 3056 * @ee: Pointer to "struct ccs_execve_entry".
1235 kumaneko 2690 *
1236 kumaneko 2828 * Caller holds ccs_read_lock().
1237 kumaneko 2037 */
1238 kumaneko 3056 void ccs_finish_execve(int retval, struct ccs_execve_entry *ee)
1239 kumaneko 2037 {
1240     struct task_struct *task = current;
1241     if (!ee)
1242     return;
1243 kumaneko 2968 if (retval < 0) {
1244     task->ccs_domain_info = ee->previous_domain;
1245 kumaneko 3056 /*
1246     * Make task->ccs_domain_info visible to GC before changing
1247     * task->ccs_flags .
1248     */
1249     smp_mb();
1250     } else {
1251     /* Mark the current process as execute handler. */
1252     if (ee->handler)
1253     task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
1254     /* Mark the current process as normal process. */
1255     else
1256     task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
1257 kumaneko 2968 }
1258 kumaneko 3056 /* Tell GC that I finished execve(). */
1259     task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
1260     ccs_read_unlock(ee->reader_idx);
1261     kfree(ee->handler_path);
1262     kfree(ee->tmp);
1263     kfree(ee->dump.data);
1264     kfree(ee);
1265 kumaneko 115 }

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