TOMOYO: Add socket operation restriction support.
[deliverable/linux.git] / security / tomoyo / domain.c
CommitLineData
26a2a1c9
KT
1/*
2 * security/tomoyo/domain.c
3 *
0f2a55d5 4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
26a2a1c9
KT
5 */
6
7#include "common.h"
26a2a1c9 8#include <linux/binfmts.h>
5a0e3ad6 9#include <linux/slab.h>
26a2a1c9
KT
10
11/* Variables definitions.*/
12
13/* The initial domain. */
14struct tomoyo_domain_info tomoyo_kernel_domain;
15
36f5e1ff
TH
16/**
17 * tomoyo_update_policy - Update an entry for exception policy.
18 *
19 * @new_entry: Pointer to "struct tomoyo_acl_info".
20 * @size: Size of @new_entry in bytes.
a238cf5b 21 * @param: Pointer to "struct tomoyo_acl_param".
36f5e1ff
TH
22 * @check_duplicate: Callback function to find duplicated entry.
23 *
24 * Returns 0 on success, negative value otherwise.
25 *
26 * Caller holds tomoyo_read_lock().
27 */
28int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
a238cf5b 29 struct tomoyo_acl_param *param,
36f5e1ff
TH
30 bool (*check_duplicate) (const struct tomoyo_acl_head
31 *,
32 const struct tomoyo_acl_head
33 *))
34{
a238cf5b 35 int error = param->is_delete ? -ENOENT : -ENOMEM;
36f5e1ff 36 struct tomoyo_acl_head *entry;
a238cf5b 37 struct list_head *list = param->list;
36f5e1ff
TH
38
39 if (mutex_lock_interruptible(&tomoyo_policy_lock))
40 return -ENOMEM;
41 list_for_each_entry_rcu(entry, list, list) {
42 if (!check_duplicate(entry, new_entry))
43 continue;
a238cf5b 44 entry->is_deleted = param->is_delete;
36f5e1ff
TH
45 error = 0;
46 break;
47 }
a238cf5b 48 if (error && !param->is_delete) {
36f5e1ff
TH
49 entry = tomoyo_commit_ok(new_entry, size);
50 if (entry) {
51 list_add_tail_rcu(&entry->list, list);
52 error = 0;
53 }
54 }
55 mutex_unlock(&tomoyo_policy_lock);
56 return error;
57}
58
0df7e8b8
TH
59/**
60 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
61 *
62 * @a: Pointer to "struct tomoyo_acl_info".
63 * @b: Pointer to "struct tomoyo_acl_info".
64 *
65 * Returns true if @a == @b, false otherwise.
66 */
67static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
68 const struct tomoyo_acl_info *b)
69{
2066a361 70 return a->type == b->type && a->cond == b->cond;
0df7e8b8
TH
71}
72
237ab459
TH
73/**
74 * tomoyo_update_domain - Update an entry for domain policy.
75 *
76 * @new_entry: Pointer to "struct tomoyo_acl_info".
77 * @size: Size of @new_entry in bytes.
a238cf5b 78 * @param: Pointer to "struct tomoyo_acl_param".
237ab459
TH
79 * @check_duplicate: Callback function to find duplicated entry.
80 * @merge_duplicate: Callback function to merge duplicated entry.
81 *
82 * Returns 0 on success, negative value otherwise.
83 *
84 * Caller holds tomoyo_read_lock().
85 */
86int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
a238cf5b 87 struct tomoyo_acl_param *param,
237ab459
TH
88 bool (*check_duplicate) (const struct tomoyo_acl_info
89 *,
90 const struct tomoyo_acl_info
91 *),
92 bool (*merge_duplicate) (struct tomoyo_acl_info *,
93 struct tomoyo_acl_info *,
94 const bool))
95{
a238cf5b 96 const bool is_delete = param->is_delete;
237ab459
TH
97 int error = is_delete ? -ENOENT : -ENOMEM;
98 struct tomoyo_acl_info *entry;
a238cf5b 99 struct list_head * const list = param->list;
237ab459 100
2066a361
TH
101 if (param->data[0]) {
102 new_entry->cond = tomoyo_get_condition(param);
103 if (!new_entry->cond)
104 return -EINVAL;
105 }
237ab459 106 if (mutex_lock_interruptible(&tomoyo_policy_lock))
2066a361 107 goto out;
a238cf5b 108 list_for_each_entry_rcu(entry, list, list) {
0df7e8b8
TH
109 if (!tomoyo_same_acl_head(entry, new_entry) ||
110 !check_duplicate(entry, new_entry))
237ab459
TH
111 continue;
112 if (merge_duplicate)
113 entry->is_deleted = merge_duplicate(entry, new_entry,
114 is_delete);
115 else
116 entry->is_deleted = is_delete;
117 error = 0;
118 break;
119 }
120 if (error && !is_delete) {
121 entry = tomoyo_commit_ok(new_entry, size);
122 if (entry) {
a238cf5b 123 list_add_tail_rcu(&entry->list, list);
237ab459
TH
124 error = 0;
125 }
126 }
127 mutex_unlock(&tomoyo_policy_lock);
2066a361
TH
128out:
129 tomoyo_put_condition(new_entry->cond);
237ab459
TH
130 return error;
131}
132
32997144
TH
133/**
134 * tomoyo_check_acl - Do permission check.
135 *
136 * @r: Pointer to "struct tomoyo_request_info".
137 * @check_entry: Callback function to check type specific parameters.
138 *
139 * Returns 0 on success, negative value otherwise.
140 *
141 * Caller holds tomoyo_read_lock().
142 */
99a85259 143void tomoyo_check_acl(struct tomoyo_request_info *r,
484ca79c 144 bool (*check_entry) (struct tomoyo_request_info *,
99a85259
TH
145 const struct tomoyo_acl_info *))
146{
147 const struct tomoyo_domain_info *domain = r->domain;
148 struct tomoyo_acl_info *ptr;
32997144
TH
149 bool retried = false;
150 const struct list_head *list = &domain->acl_info_list;
99a85259 151
32997144
TH
152retry:
153 list_for_each_entry_rcu(ptr, list, list) {
99a85259
TH
154 if (ptr->is_deleted || ptr->type != r->param_type)
155 continue;
2066a361
TH
156 if (!check_entry(r, ptr))
157 continue;
158 if (!tomoyo_condition(r, ptr->cond))
159 continue;
160 r->granted = true;
161 return;
99a85259 162 }
32997144
TH
163 if (!retried) {
164 retried = true;
bd03a3e4 165 list = &domain->ns->acl_group[domain->group];
32997144
TH
166 goto retry;
167 }
99a85259
TH
168 r->granted = false;
169}
170
a230f9e7 171/* The list for "struct tomoyo_domain_info". */
26a2a1c9 172LIST_HEAD(tomoyo_domain_list);
26a2a1c9 173
26a2a1c9 174/**
e2bf6907 175 * tomoyo_last_word - Get last component of a domainname.
26a2a1c9 176 *
bd03a3e4 177 * @name: Domainname to check.
26a2a1c9 178 *
e2bf6907 179 * Returns the last word of @domainname.
26a2a1c9 180 */
e2bf6907 181static const char *tomoyo_last_word(const char *name)
26a2a1c9 182{
0f2a55d5
TH
183 const char *cp = strrchr(name, ' ');
184 if (cp)
185 return cp + 1;
186 return name;
26a2a1c9
KT
187}
188
a238cf5b
TH
189/**
190 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
191 *
192 * @a: Pointer to "struct tomoyo_acl_head".
193 * @b: Pointer to "struct tomoyo_acl_head".
194 *
195 * Returns true if @a == @b, false otherwise.
196 */
e2bf6907
TH
197static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
198 const struct tomoyo_acl_head *b)
36f5e1ff 199{
5448ec4f
TH
200 const struct tomoyo_transition_control *p1 = container_of(a,
201 typeof(*p1),
202 head);
203 const struct tomoyo_transition_control *p2 = container_of(b,
204 typeof(*p2),
205 head);
206 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
36f5e1ff
TH
207 && p1->domainname == p2->domainname
208 && p1->program == p2->program;
209}
210
26a2a1c9 211/**
a238cf5b 212 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
26a2a1c9 213 *
a238cf5b
TH
214 * @param: Pointer to "struct tomoyo_acl_param".
215 * @type: Type of this entry.
26a2a1c9
KT
216 *
217 * Returns 0 on success, negative value otherwise.
218 */
a238cf5b
TH
219int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
220 const u8 type)
26a2a1c9 221{
5448ec4f 222 struct tomoyo_transition_control e = { .type = type };
a238cf5b
TH
223 int error = param->is_delete ? -ENOENT : -ENOMEM;
224 char *program = param->data;
225 char *domainname = strstr(program, " from ");
226 if (domainname) {
227 *domainname = '\0';
228 domainname += 6;
229 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
230 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
231 domainname = program;
232 program = NULL;
233 }
0d2171d7 234 if (program && strcmp(program, "any")) {
5448ec4f
TH
235 if (!tomoyo_correct_path(program))
236 return -EINVAL;
237 e.program = tomoyo_get_name(program);
238 if (!e.program)
239 goto out;
240 }
0d2171d7 241 if (domainname && strcmp(domainname, "any")) {
5448ec4f
TH
242 if (!tomoyo_correct_domain(domainname)) {
243 if (!tomoyo_correct_path(domainname))
244 goto out;
9e4b50e9 245 e.is_last_name = true;
5448ec4f 246 }
9e4b50e9
TH
247 e.domainname = tomoyo_get_name(domainname);
248 if (!e.domainname)
ca0b7df3 249 goto out;
26a2a1c9 250 }
bd03a3e4 251 param->list = &param->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
a238cf5b 252 error = tomoyo_update_policy(&e.head, sizeof(e), param,
e2bf6907 253 tomoyo_same_transition_control);
a238cf5b 254out:
9e4b50e9
TH
255 tomoyo_put_name(e.domainname);
256 tomoyo_put_name(e.program);
26a2a1c9
KT
257 return error;
258}
259
26a2a1c9 260/**
bd03a3e4 261 * tomoyo_scan_transition - Try to find specific domain transition type.
26a2a1c9 262 *
bd03a3e4
TH
263 * @list: Pointer to "struct list_head".
264 * @domainname: The name of current domain.
265 * @program: The name of requested program.
266 * @last_name: The last component of @domainname.
267 * @type: One of values in "enum tomoyo_transition_type".
26a2a1c9 268 *
bd03a3e4 269 * Returns true if found one, false otherwise.
fdb8ebb7
TH
270 *
271 * Caller holds tomoyo_read_lock().
26a2a1c9 272 */
bd03a3e4
TH
273static inline bool tomoyo_scan_transition
274(const struct list_head *list, const struct tomoyo_path_info *domainname,
275 const struct tomoyo_path_info *program, const char *last_name,
276 const enum tomoyo_transition_type type)
26a2a1c9 277{
5448ec4f 278 const struct tomoyo_transition_control *ptr;
bd03a3e4
TH
279 list_for_each_entry_rcu(ptr, list, head.list) {
280 if (ptr->head.is_deleted || ptr->type != type)
281 continue;
282 if (ptr->domainname) {
283 if (!ptr->is_last_name) {
284 if (ptr->domainname != domainname)
285 continue;
286 } else {
5448ec4f 287 /*
bd03a3e4
TH
288 * Use direct strcmp() since this is
289 * unlikely used.
5448ec4f 290 */
bd03a3e4
TH
291 if (strcmp(ptr->domainname->name, last_name))
292 continue;
5448ec4f 293 }
26a2a1c9 294 }
bd03a3e4
TH
295 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
296 continue;
297 return true;
298 }
299 return false;
300}
301
302/**
303 * tomoyo_transition_type - Get domain transition type.
304 *
305 * @ns: Pointer to "struct tomoyo_policy_namespace".
306 * @domainname: The name of current domain.
307 * @program: The name of requested program.
308 *
309 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
310 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
311 * executing @program reinitializes domain transition within that namespace,
312 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
313 * others otherwise.
314 *
315 * Caller holds tomoyo_read_lock().
316 */
317static enum tomoyo_transition_type tomoyo_transition_type
318(const struct tomoyo_policy_namespace *ns,
319 const struct tomoyo_path_info *domainname,
320 const struct tomoyo_path_info *program)
321{
322 const char *last_name = tomoyo_last_word(domainname->name);
323 enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
324 while (type < TOMOYO_MAX_TRANSITION_TYPE) {
325 const struct list_head * const list =
326 &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
327 if (!tomoyo_scan_transition(list, domainname, program,
328 last_name, type)) {
329 type++;
330 continue;
331 }
332 if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET &&
333 type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE)
334 break;
335 /*
336 * Do not check for reset_domain if no_reset_domain matched.
337 * Do not check for initialize_domain if no_initialize_domain
338 * matched.
339 */
340 type++;
341 type++;
26a2a1c9 342 }
5448ec4f 343 return type;
26a2a1c9
KT
344}
345
a238cf5b
TH
346/**
347 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
348 *
349 * @a: Pointer to "struct tomoyo_acl_head".
350 * @b: Pointer to "struct tomoyo_acl_head".
351 *
352 * Returns true if @a == @b, false otherwise.
353 */
e2bf6907
TH
354static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
355 const struct tomoyo_acl_head *b)
36f5e1ff 356{
a238cf5b
TH
357 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
358 head);
359 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
360 head);
36f5e1ff
TH
361 return p1->original_name == p2->original_name &&
362 p1->aggregated_name == p2->aggregated_name;
363}
364
1084307c 365/**
a238cf5b 366 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
1084307c 367 *
a238cf5b 368 * @param: Pointer to "struct tomoyo_acl_param".
1084307c
TH
369 *
370 * Returns 0 on success, negative value otherwise.
371 *
372 * Caller holds tomoyo_read_lock().
373 */
a238cf5b 374int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
1084307c 375{
e2bf6907 376 struct tomoyo_aggregator e = { };
a238cf5b
TH
377 int error = param->is_delete ? -ENOENT : -ENOMEM;
378 const char *original_name = tomoyo_read_token(param);
379 const char *aggregated_name = tomoyo_read_token(param);
380 if (!tomoyo_correct_word(original_name) ||
75093152 381 !tomoyo_correct_path(aggregated_name))
1084307c
TH
382 return -EINVAL;
383 e.original_name = tomoyo_get_name(original_name);
384 e.aggregated_name = tomoyo_get_name(aggregated_name);
385 if (!e.original_name || !e.aggregated_name ||
386 e.aggregated_name->is_patterned) /* No patterns allowed. */
387 goto out;
bd03a3e4 388 param->list = &param->ns->policy_list[TOMOYO_ID_AGGREGATOR];
a238cf5b 389 error = tomoyo_update_policy(&e.head, sizeof(e), param,
e2bf6907 390 tomoyo_same_aggregator);
a238cf5b 391out:
1084307c
TH
392 tomoyo_put_name(e.original_name);
393 tomoyo_put_name(e.aggregated_name);
394 return error;
395}
396
26a2a1c9 397/**
bd03a3e4 398 * tomoyo_find_namespace - Find specified namespace.
26a2a1c9 399 *
bd03a3e4
TH
400 * @name: Name of namespace to find.
401 * @len: Length of @name.
26a2a1c9 402 *
bd03a3e4
TH
403 * Returns pointer to "struct tomoyo_policy_namespace" if found,
404 * NULL otherwise.
fdb8ebb7
TH
405 *
406 * Caller holds tomoyo_read_lock().
26a2a1c9 407 */
bd03a3e4
TH
408static struct tomoyo_policy_namespace *tomoyo_find_namespace
409(const char *name, const unsigned int len)
26a2a1c9 410{
bd03a3e4
TH
411 struct tomoyo_policy_namespace *ns;
412 list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
413 if (strncmp(name, ns->name, len) ||
414 (name[len] && name[len] != ' '))
415 continue;
416 return ns;
417 }
418 return NULL;
419}
26a2a1c9 420
bd03a3e4
TH
421/**
422 * tomoyo_assign_namespace - Create a new namespace.
423 *
424 * @domainname: Name of namespace to create.
425 *
426 * Returns pointer to "struct tomoyo_policy_namespace" on success,
427 * NULL otherwise.
428 *
429 * Caller holds tomoyo_read_lock().
430 */
431struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname)
432{
433 struct tomoyo_policy_namespace *ptr;
434 struct tomoyo_policy_namespace *entry;
435 const char *cp = domainname;
436 unsigned int len = 0;
437 while (*cp && *cp++ != ' ')
438 len++;
439 ptr = tomoyo_find_namespace(domainname, len);
440 if (ptr)
441 return ptr;
442 if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname))
ca0b7df3 443 return NULL;
bd03a3e4
TH
444 entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS);
445 if (!entry)
ca0b7df3 446 return NULL;
29282381
TH
447 if (mutex_lock_interruptible(&tomoyo_policy_lock))
448 goto out;
bd03a3e4
TH
449 ptr = tomoyo_find_namespace(domainname, len);
450 if (!ptr && tomoyo_memory_ok(entry)) {
451 char *name = (char *) (entry + 1);
452 ptr = entry;
453 memmove(name, domainname, len);
454 name[len] = '\0';
455 entry->name = name;
456 tomoyo_init_policy_namespace(entry);
ca0b7df3 457 entry = NULL;
26a2a1c9 458 }
f737d95d 459 mutex_unlock(&tomoyo_policy_lock);
bd03a3e4 460out:
ca0b7df3 461 kfree(entry);
bd03a3e4
TH
462 return ptr;
463}
464
465/**
466 * tomoyo_namespace_jump - Check for namespace jump.
467 *
468 * @domainname: Name of domain.
469 *
470 * Returns true if namespace differs, false otherwise.
471 */
472static bool tomoyo_namespace_jump(const char *domainname)
473{
474 const char *namespace = tomoyo_current_namespace()->name;
475 const int len = strlen(namespace);
476 return strncmp(domainname, namespace, len) ||
477 (domainname[len] && domainname[len] != ' ');
478}
479
480/**
481 * tomoyo_assign_domain - Create a domain or a namespace.
482 *
483 * @domainname: The name of domain.
484 * @transit: True if transit to domain found or created.
485 *
486 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
487 *
488 * Caller holds tomoyo_read_lock().
489 */
490struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
491 const bool transit)
492{
493 struct tomoyo_domain_info e = { };
494 struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
495 bool created = false;
496 if (entry) {
497 if (transit) {
498 /*
499 * Since namespace is created at runtime, profiles may
500 * not be created by the moment the process transits to
501 * that domain. Do not perform domain transition if
502 * profile for that domain is not yet created.
503 */
504 if (!entry->ns->profile_ptr[entry->profile])
505 return NULL;
506 }
507 return entry;
508 }
509 /* Requested domain does not exist. */
510 /* Don't create requested domain if domainname is invalid. */
511 if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 ||
512 !tomoyo_correct_domain(domainname))
513 return NULL;
514 /*
515 * Since definition of profiles and acl_groups may differ across
516 * namespaces, do not inherit "use_profile" and "use_group" settings
517 * by automatically creating requested domain upon domain transition.
518 */
519 if (transit && tomoyo_namespace_jump(domainname))
520 return NULL;
521 e.ns = tomoyo_assign_namespace(domainname);
522 if (!e.ns)
523 return NULL;
524 /*
525 * "use_profile" and "use_group" settings for automatically created
526 * domains are inherited from current domain. These are 0 for manually
527 * created domains.
528 */
529 if (transit) {
530 const struct tomoyo_domain_info *domain = tomoyo_domain();
531 e.profile = domain->profile;
532 e.group = domain->group;
533 }
534 e.domainname = tomoyo_get_name(domainname);
535 if (!e.domainname)
536 return NULL;
537 if (mutex_lock_interruptible(&tomoyo_policy_lock))
538 goto out;
539 entry = tomoyo_find_domain(domainname);
540 if (!entry) {
541 entry = tomoyo_commit_ok(&e, sizeof(e));
542 if (entry) {
543 INIT_LIST_HEAD(&entry->acl_info_list);
544 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
545 created = true;
546 }
547 }
548 mutex_unlock(&tomoyo_policy_lock);
549out:
550 tomoyo_put_name(e.domainname);
551 if (entry && transit) {
552 if (created) {
553 struct tomoyo_request_info r;
554 tomoyo_init_request_info(&r, entry,
555 TOMOYO_MAC_FILE_EXECUTE);
556 r.granted = false;
557 tomoyo_write_log(&r, "use_profile %u\n",
558 entry->profile);
559 tomoyo_write_log(&r, "use_group %u\n", entry->group);
560 }
561 }
562 return entry;
26a2a1c9
KT
563}
564
d58e0da8
TH
565/**
566 * tomoyo_environ - Check permission for environment variable names.
567 *
568 * @ee: Pointer to "struct tomoyo_execve".
569 *
570 * Returns 0 on success, negative value otherwise.
571 */
572static int tomoyo_environ(struct tomoyo_execve *ee)
573{
574 struct tomoyo_request_info *r = &ee->r;
575 struct linux_binprm *bprm = ee->bprm;
576 /* env_page.data is allocated by tomoyo_dump_page(). */
577 struct tomoyo_page_dump env_page = { };
578 char *arg_ptr; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
579 int arg_len = 0;
580 unsigned long pos = bprm->p;
581 int offset = pos % PAGE_SIZE;
582 int argv_count = bprm->argc;
583 int envp_count = bprm->envc;
584 int error = -ENOMEM;
585
586 ee->r.type = TOMOYO_MAC_ENVIRON;
587 ee->r.profile = r->domain->profile;
588 ee->r.mode = tomoyo_get_mode(r->domain->ns, ee->r.profile,
589 TOMOYO_MAC_ENVIRON);
590 if (!r->mode || !envp_count)
591 return 0;
592 arg_ptr = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
593 if (!arg_ptr)
594 goto out;
595 while (error == -ENOMEM) {
596 if (!tomoyo_dump_page(bprm, pos, &env_page))
597 goto out;
598 pos += PAGE_SIZE - offset;
599 /* Read. */
600 while (argv_count && offset < PAGE_SIZE) {
601 if (!env_page.data[offset++])
602 argv_count--;
603 }
604 if (argv_count) {
605 offset = 0;
606 continue;
607 }
608 while (offset < PAGE_SIZE) {
609 const unsigned char c = env_page.data[offset++];
610
611 if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
612 if (c == '=') {
613 arg_ptr[arg_len++] = '\0';
614 } else if (c == '\\') {
615 arg_ptr[arg_len++] = '\\';
616 arg_ptr[arg_len++] = '\\';
617 } else if (c > ' ' && c < 127) {
618 arg_ptr[arg_len++] = c;
619 } else {
620 arg_ptr[arg_len++] = '\\';
621 arg_ptr[arg_len++] = (c >> 6) + '0';
622 arg_ptr[arg_len++]
623 = ((c >> 3) & 7) + '0';
624 arg_ptr[arg_len++] = (c & 7) + '0';
625 }
626 } else {
627 arg_ptr[arg_len] = '\0';
628 }
629 if (c)
630 continue;
631 if (tomoyo_env_perm(r, arg_ptr)) {
632 error = -EPERM;
633 break;
634 }
635 if (!--envp_count) {
636 error = 0;
637 break;
638 }
639 arg_len = 0;
640 }
641 offset = 0;
642 }
643out:
644 if (r->mode != TOMOYO_CONFIG_ENFORCING)
645 error = 0;
646 kfree(env_page.data);
647 kfree(arg_ptr);
648 return error;
649}
650
26a2a1c9
KT
651/**
652 * tomoyo_find_next_domain - Find a domain.
653 *
56f8c9bc 654 * @bprm: Pointer to "struct linux_binprm".
26a2a1c9
KT
655 *
656 * Returns 0 on success, negative value otherwise.
fdb8ebb7
TH
657 *
658 * Caller holds tomoyo_read_lock().
26a2a1c9 659 */
56f8c9bc 660int tomoyo_find_next_domain(struct linux_binprm *bprm)
26a2a1c9 661{
26a2a1c9
KT
662 struct tomoyo_domain_info *old_domain = tomoyo_domain();
663 struct tomoyo_domain_info *domain = NULL;
26a2a1c9 664 const char *original_name = bprm->filename;
26a2a1c9 665 int retval = -ENOMEM;
c8c57e84 666 bool need_kfree = false;
bd03a3e4 667 bool reject_on_transition_failure = false;
c8c57e84 668 struct tomoyo_path_info rn = { }; /* real name */
97fb35e4 669 struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
d58e0da8 670
97fb35e4
TH
671 if (!ee)
672 return -ENOMEM;
673 ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
674 if (!ee->tmp) {
675 kfree(ee);
676 return -ENOMEM;
677 }
678 /* ee->dump->data is allocated by tomoyo_dump_page(). */
679 tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
680 ee->r.ee = ee;
681 ee->bprm = bprm;
682 ee->r.obj = &ee->obj;
683 ee->obj.path1 = bprm->file->f_path;
17fcfbd9 684 retry:
c8c57e84
TH
685 if (need_kfree) {
686 kfree(rn.name);
687 need_kfree = false;
688 }
0617c7ff 689 /* Get symlink's pathname of program. */
26a2a1c9 690 retval = -ENOENT;
0617c7ff 691 rn.name = tomoyo_realpath_nofollow(original_name);
c8c57e84 692 if (!rn.name)
26a2a1c9 693 goto out;
c8c57e84
TH
694 tomoyo_fill_path_info(&rn);
695 need_kfree = true;
696
1084307c
TH
697 /* Check 'aggregator' directive. */
698 {
e2bf6907 699 struct tomoyo_aggregator *ptr;
bd03a3e4
TH
700 struct list_head *list =
701 &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
702 /* Check 'aggregator' directive. */
703 list_for_each_entry_rcu(ptr, list, head.list) {
82e0f001 704 if (ptr->head.is_deleted ||
1084307c
TH
705 !tomoyo_path_matches_pattern(&rn,
706 ptr->original_name))
707 continue;
0617c7ff 708 kfree(rn.name);
1084307c
TH
709 need_kfree = false;
710 /* This is OK because it is read only. */
711 rn = *ptr->aggregated_name;
712 break;
713 }
714 }
715
26a2a1c9 716 /* Check execute permission. */
97fb35e4 717 retval = tomoyo_path_permission(&ee->r, TOMOYO_TYPE_EXECUTE, &rn);
17fcfbd9
TH
718 if (retval == TOMOYO_RETRY_REQUEST)
719 goto retry;
26a2a1c9
KT
720 if (retval < 0)
721 goto out;
484ca79c
TH
722 /*
723 * To be able to specify domainnames with wildcards, use the
724 * pathname specified in the policy (which may contain
725 * wildcard) rather than the pathname passed to execve()
726 * (which never contains wildcard).
727 */
97fb35e4 728 if (ee->r.param.path.matched_path) {
484ca79c
TH
729 if (need_kfree)
730 kfree(rn.name);
731 need_kfree = false;
732 /* This is OK because it is read only. */
97fb35e4 733 rn = *ee->r.param.path.matched_path;
484ca79c 734 }
26a2a1c9 735
5448ec4f 736 /* Calculate domain to transit to. */
bd03a3e4
TH
737 switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
738 &rn)) {
739 case TOMOYO_TRANSITION_CONTROL_RESET:
740 /* Transit to the root of specified namespace. */
97fb35e4 741 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", rn.name);
bd03a3e4
TH
742 /*
743 * Make do_execve() fail if domain transition across namespaces
744 * has failed.
745 */
746 reject_on_transition_failure = true;
747 break;
5448ec4f 748 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
bd03a3e4 749 /* Transit to the child of current namespace's root. */
97fb35e4 750 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
bd03a3e4 751 old_domain->ns->name, rn.name);
5448ec4f
TH
752 break;
753 case TOMOYO_TRANSITION_CONTROL_KEEP:
26a2a1c9
KT
754 /* Keep current domain. */
755 domain = old_domain;
5448ec4f
TH
756 break;
757 default:
758 if (old_domain == &tomoyo_kernel_domain &&
759 !tomoyo_policy_loaded) {
760 /*
761 * Needn't to transit from kernel domain before
762 * starting /sbin/init. But transit from kernel domain
763 * if executing initializers because they might start
764 * before /sbin/init.
765 */
766 domain = old_domain;
767 } else {
768 /* Normal domain transition. */
97fb35e4 769 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
5448ec4f
TH
770 old_domain->domainname->name, rn.name);
771 }
772 break;
26a2a1c9 773 }
7c75964f 774 if (!domain)
97fb35e4 775 domain = tomoyo_assign_domain(ee->tmp, true);
26a2a1c9 776 if (domain)
bd03a3e4
TH
777 retval = 0;
778 else if (reject_on_transition_failure) {
97fb35e4
TH
779 printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n",
780 ee->tmp);
bd03a3e4 781 retval = -ENOMEM;
97fb35e4 782 } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
bd03a3e4
TH
783 retval = -ENOMEM;
784 else {
785 retval = 0;
2c47ab93
TH
786 if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
787 old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
97fb35e4
TH
788 ee->r.granted = false;
789 tomoyo_write_log(&ee->r, "%s", tomoyo_dif
2c47ab93 790 [TOMOYO_DIF_TRANSITION_FAILED]);
bd03a3e4 791 printk(KERN_WARNING
97fb35e4 792 "ERROR: Domain '%s' not defined.\n", ee->tmp);
bd03a3e4
TH
793 }
794 }
26a2a1c9 795 out:
56f8c9bc
TH
796 if (!domain)
797 domain = old_domain;
ec8e6a4e
TH
798 /* Update reference count on "struct tomoyo_domain_info". */
799 atomic_inc(&domain->users);
56f8c9bc 800 bprm->cred->security = domain;
c8c57e84
TH
801 if (need_kfree)
802 kfree(rn.name);
d58e0da8
TH
803 if (!retval) {
804 ee->r.domain = domain;
805 retval = tomoyo_environ(ee);
806 }
97fb35e4
TH
807 kfree(ee->tmp);
808 kfree(ee->dump.data);
809 kfree(ee);
26a2a1c9
KT
810 return retval;
811}
5b636857
TH
812
813/**
814 * tomoyo_dump_page - Dump a page to buffer.
815 *
816 * @bprm: Pointer to "struct linux_binprm".
817 * @pos: Location to dump.
818 * @dump: Poiner to "struct tomoyo_page_dump".
819 *
820 * Returns true on success, false otherwise.
821 */
822bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
823 struct tomoyo_page_dump *dump)
824{
825 struct page *page;
d58e0da8
TH
826
827 /* dump->data is released by tomoyo_find_next_domain(). */
5b636857
TH
828 if (!dump->data) {
829 dump->data = kzalloc(PAGE_SIZE, GFP_NOFS);
830 if (!dump->data)
831 return false;
832 }
833 /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
834#ifdef CONFIG_MMU
835 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
836 return false;
837#else
838 page = bprm->page[pos / PAGE_SIZE];
839#endif
840 if (page != dump->page) {
841 const unsigned int offset = pos % PAGE_SIZE;
842 /*
843 * Maybe kmap()/kunmap() should be used here.
844 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
845 * So do I.
846 */
847 char *kaddr = kmap_atomic(page, KM_USER0);
d58e0da8 848
5b636857
TH
849 dump->page = page;
850 memcpy(dump->data + offset, kaddr + offset,
851 PAGE_SIZE - offset);
852 kunmap_atomic(kaddr, KM_USER0);
853 }
854 /* Same with put_arg_page(page) in fs/exec.c */
855#ifdef CONFIG_MMU
856 put_page(page);
857#endif
858 return true;
859}
This page took 0.248549 seconds and 5 git commands to generate.