domain.c 21.2 KB
Newer Older
Kentaro Takeda's avatar
Kentaro Takeda committed
1 2 3
/*
 * security/tomoyo/domain.c
 *
4
 * Domain transition functions for TOMOYO.
Kentaro Takeda's avatar
Kentaro Takeda committed
5
 *
6
 * Copyright (C) 2005-2010  NTT DATA CORPORATION
Kentaro Takeda's avatar
Kentaro Takeda committed
7 8 9 10
 */

#include "common.h"
#include <linux/binfmts.h>
11
#include <linux/slab.h>
Kentaro Takeda's avatar
Kentaro Takeda committed
12 13 14 15 16 17

/* Variables definitions.*/

/* The initial domain. */
struct tomoyo_domain_info tomoyo_kernel_domain;

18 19 20 21 22
/**
 * tomoyo_update_policy - Update an entry for exception policy.
 *
 * @new_entry:       Pointer to "struct tomoyo_acl_info".
 * @size:            Size of @new_entry in bytes.
23
 * @param:           Pointer to "struct tomoyo_acl_param".
24 25 26 27 28 29 30
 * @check_duplicate: Callback function to find duplicated entry.
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
31
			 struct tomoyo_acl_param *param,
32 33 34 35 36
			 bool (*check_duplicate) (const struct tomoyo_acl_head
						  *,
						  const struct tomoyo_acl_head
						  *))
{
37
	int error = param->is_delete ? -ENOENT : -ENOMEM;
38
	struct tomoyo_acl_head *entry;
39
	struct list_head *list = param->list;
40 41 42 43 44 45

	if (mutex_lock_interruptible(&tomoyo_policy_lock))
		return -ENOMEM;
	list_for_each_entry_rcu(entry, list, list) {
		if (!check_duplicate(entry, new_entry))
			continue;
46
		entry->is_deleted = param->is_delete;
47 48 49
		error = 0;
		break;
	}
50
	if (error && !param->is_delete) {
51 52 53 54 55 56 57 58 59 60
		entry = tomoyo_commit_ok(new_entry, size);
		if (entry) {
			list_add_tail_rcu(&entry->list, list);
			error = 0;
		}
	}
	mutex_unlock(&tomoyo_policy_lock);
	return error;
}

Tetsuo Handa's avatar
Tetsuo Handa committed
61 62 63 64 65 66 67 68 69 70 71
/**
 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_info".
 * @b: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if @a == @b, false otherwise.
 */
static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
					const struct tomoyo_acl_info *b)
{
72
	return a->type == b->type && a->cond == b->cond;
Tetsuo Handa's avatar
Tetsuo Handa committed
73 74
}

75 76 77 78 79
/**
 * tomoyo_update_domain - Update an entry for domain policy.
 *
 * @new_entry:       Pointer to "struct tomoyo_acl_info".
 * @size:            Size of @new_entry in bytes.
80
 * @param:           Pointer to "struct tomoyo_acl_param".
81 82 83 84 85 86 87 88
 * @check_duplicate: Callback function to find duplicated entry.
 * @merge_duplicate: Callback function to merge duplicated entry.
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
89
			 struct tomoyo_acl_param *param,
90 91 92 93 94 95 96 97
			 bool (*check_duplicate) (const struct tomoyo_acl_info
						  *,
						  const struct tomoyo_acl_info
						  *),
			 bool (*merge_duplicate) (struct tomoyo_acl_info *,
						  struct tomoyo_acl_info *,
						  const bool))
{
98
	const bool is_delete = param->is_delete;
99 100
	int error = is_delete ? -ENOENT : -ENOMEM;
	struct tomoyo_acl_info *entry;
101
	struct list_head * const list = param->list;
102

103 104 105 106 107
	if (param->data[0]) {
		new_entry->cond = tomoyo_get_condition(param);
		if (!new_entry->cond)
			return -EINVAL;
	}
108
	if (mutex_lock_interruptible(&tomoyo_policy_lock))
109
		goto out;
110
	list_for_each_entry_rcu(entry, list, list) {
Tetsuo Handa's avatar
Tetsuo Handa committed
111 112
		if (!tomoyo_same_acl_head(entry, new_entry) ||
		    !check_duplicate(entry, new_entry))
113 114 115 116 117 118 119 120 121 122 123 124
			continue;
		if (merge_duplicate)
			entry->is_deleted = merge_duplicate(entry, new_entry,
							    is_delete);
		else
			entry->is_deleted = is_delete;
		error = 0;
		break;
	}
	if (error && !is_delete) {
		entry = tomoyo_commit_ok(new_entry, size);
		if (entry) {
125
			list_add_tail_rcu(&entry->list, list);
126 127 128 129
			error = 0;
		}
	}
	mutex_unlock(&tomoyo_policy_lock);
130 131
out:
	tomoyo_put_condition(new_entry->cond);
132 133 134
	return error;
}

135 136 137 138 139 140 141 142 143 144
/**
 * tomoyo_check_acl - Do permission check.
 *
 * @r:           Pointer to "struct tomoyo_request_info".
 * @check_entry: Callback function to check type specific parameters.
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
145
void tomoyo_check_acl(struct tomoyo_request_info *r,
146
		      bool (*check_entry) (struct tomoyo_request_info *,
147 148 149 150
					   const struct tomoyo_acl_info *))
{
	const struct tomoyo_domain_info *domain = r->domain;
	struct tomoyo_acl_info *ptr;
151 152
	bool retried = false;
	const struct list_head *list = &domain->acl_info_list;
153

154 155
retry:
	list_for_each_entry_rcu(ptr, list, list) {
156 157
		if (ptr->is_deleted || ptr->type != r->param_type)
			continue;
158 159 160 161 162 163
		if (!check_entry(r, ptr))
			continue;
		if (!tomoyo_condition(r, ptr->cond))
			continue;
		r->granted = true;
		return;
164
	}
165 166
	if (!retried) {
		retried = true;
167
		list = &domain->ns->acl_group[domain->group];
168 169
		goto retry;
	}
170 171 172
	r->granted = false;
}

173
/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda's avatar
Kentaro Takeda committed
174 175 176
LIST_HEAD(tomoyo_domain_list);

/**
Tetsuo Handa's avatar
Tetsuo Handa committed
177
 * tomoyo_last_word - Get last component of a domainname.
Kentaro Takeda's avatar
Kentaro Takeda committed
178
 *
179
 * @name: Domainname to check.
Kentaro Takeda's avatar
Kentaro Takeda committed
180
 *
Tetsuo Handa's avatar
Tetsuo Handa committed
181
 * Returns the last word of @domainname.
Kentaro Takeda's avatar
Kentaro Takeda committed
182
 */
Tetsuo Handa's avatar
Tetsuo Handa committed
183
static const char *tomoyo_last_word(const char *name)
Kentaro Takeda's avatar
Kentaro Takeda committed
184
{
Tetsuo Handa's avatar
Tetsuo Handa committed
185 186 187 188
        const char *cp = strrchr(name, ' ');
        if (cp)
                return cp + 1;
        return name;
Kentaro Takeda's avatar
Kentaro Takeda committed
189 190
}

191 192 193 194 195 196 197 198
/**
 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_head".
 * @b: Pointer to "struct tomoyo_acl_head".
 *
 * Returns true if @a == @b, false otherwise.
 */
Tetsuo Handa's avatar
Tetsuo Handa committed
199 200
static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
					   const struct tomoyo_acl_head *b)
201
{
202 203 204 205 206 207 208
	const struct tomoyo_transition_control *p1 = container_of(a,
								  typeof(*p1),
								  head);
	const struct tomoyo_transition_control *p2 = container_of(b,
								  typeof(*p2),
								  head);
	return p1->type == p2->type && p1->is_last_name == p2->is_last_name
209 210 211 212
		&& p1->domainname == p2->domainname
		&& p1->program == p2->program;
}

Kentaro Takeda's avatar
Kentaro Takeda committed
213
/**
214
 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda's avatar
Kentaro Takeda committed
215
 *
216 217
 * @param: Pointer to "struct tomoyo_acl_param".
 * @type:  Type of this entry.
Kentaro Takeda's avatar
Kentaro Takeda committed
218 219 220
 *
 * Returns 0 on success, negative value otherwise.
 */
221 222
int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
				    const u8 type)
Kentaro Takeda's avatar
Kentaro Takeda committed
223
{
224
	struct tomoyo_transition_control e = { .type = type };
225 226 227 228 229 230 231 232 233 234 235
	int error = param->is_delete ? -ENOENT : -ENOMEM;
	char *program = param->data;
	char *domainname = strstr(program, " from ");
	if (domainname) {
		*domainname = '\0';
		domainname += 6;
	} else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
		   type == TOMOYO_TRANSITION_CONTROL_KEEP) {
		domainname = program;
		program = NULL;
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
236
	if (program && strcmp(program, "any")) {
237 238 239 240 241 242
		if (!tomoyo_correct_path(program))
			return -EINVAL;
		e.program = tomoyo_get_name(program);
		if (!e.program)
			goto out;
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
243
	if (domainname && strcmp(domainname, "any")) {
244 245 246
		if (!tomoyo_correct_domain(domainname)) {
			if (!tomoyo_correct_path(domainname))
				goto out;
247
			e.is_last_name = true;
248
		}
249 250
		e.domainname = tomoyo_get_name(domainname);
		if (!e.domainname)
251
			goto out;
Kentaro Takeda's avatar
Kentaro Takeda committed
252
	}
253
	param->list = &param->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
254
	error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handa's avatar
Tetsuo Handa committed
255
				     tomoyo_same_transition_control);
256
out:
257 258
	tomoyo_put_name(e.domainname);
	tomoyo_put_name(e.program);
Kentaro Takeda's avatar
Kentaro Takeda committed
259 260 261 262
	return error;
}

/**
263
 * tomoyo_scan_transition - Try to find specific domain transition type.
Kentaro Takeda's avatar
Kentaro Takeda committed
264
 *
265 266 267 268 269
 * @list:       Pointer to "struct list_head".
 * @domainname: The name of current domain.
 * @program:    The name of requested program.
 * @last_name:  The last component of @domainname.
 * @type:       One of values in "enum tomoyo_transition_type".
Kentaro Takeda's avatar
Kentaro Takeda committed
270
 *
271
 * Returns true if found one, false otherwise.
272 273
 *
 * Caller holds tomoyo_read_lock().
Kentaro Takeda's avatar
Kentaro Takeda committed
274
 */
275 276 277 278
static inline bool tomoyo_scan_transition
(const struct list_head *list, const struct tomoyo_path_info *domainname,
 const struct tomoyo_path_info *program, const char *last_name,
 const enum tomoyo_transition_type type)
Kentaro Takeda's avatar
Kentaro Takeda committed
279
{
280
	const struct tomoyo_transition_control *ptr;
281 282 283 284 285 286 287 288
	list_for_each_entry_rcu(ptr, list, head.list) {
		if (ptr->head.is_deleted || ptr->type != type)
			continue;
		if (ptr->domainname) {
			if (!ptr->is_last_name) {
				if (ptr->domainname != domainname)
					continue;
			} else {
289
				/*
290 291
				 * Use direct strcmp() since this is
				 * unlikely used.
292
				 */
293 294
				if (strcmp(ptr->domainname->name, last_name))
					continue;
295
			}
Kentaro Takeda's avatar
Kentaro Takeda committed
296
		}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		if (ptr->program && tomoyo_pathcmp(ptr->program, program))
			continue;
		return true;
	}
	return false;
}

/**
 * tomoyo_transition_type - Get domain transition type.
 *
 * @ns:         Pointer to "struct tomoyo_policy_namespace".
 * @domainname: The name of current domain.
 * @program:    The name of requested program.
 *
 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
 * executing @program reinitializes domain transition within that namespace,
 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
 * others otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
static enum tomoyo_transition_type tomoyo_transition_type
(const struct tomoyo_policy_namespace *ns,
 const struct tomoyo_path_info *domainname,
 const struct tomoyo_path_info *program)
{
	const char *last_name = tomoyo_last_word(domainname->name);
	enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
	while (type < TOMOYO_MAX_TRANSITION_TYPE) {
		const struct list_head * const list =
			&ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
		if (!tomoyo_scan_transition(list, domainname, program,
					    last_name, type)) {
			type++;
			continue;
		}
		if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET &&
		    type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE)
			break;
		/*
		 * Do not check for reset_domain if no_reset_domain matched.
		 * Do not check for initialize_domain if no_initialize_domain
		 * matched.
		 */
		type++;
		type++;
Kentaro Takeda's avatar
Kentaro Takeda committed
344
	}
345
	return type;
Kentaro Takeda's avatar
Kentaro Takeda committed
346 347
}

348 349 350 351 352 353 354 355
/**
 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_head".
 * @b: Pointer to "struct tomoyo_acl_head".
 *
 * Returns true if @a == @b, false otherwise.
 */
Tetsuo Handa's avatar
Tetsuo Handa committed
356 357
static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
				   const struct tomoyo_acl_head *b)
358
{
359 360 361 362
	const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
							  head);
	const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
							  head);
363 364 365 366
	return p1->original_name == p2->original_name &&
		p1->aggregated_name == p2->aggregated_name;
}

367
/**
368
 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
369
 *
370
 * @param: Pointer to "struct tomoyo_acl_param".
371 372 373 374 375
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
376
int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
377
{
Tetsuo Handa's avatar
Tetsuo Handa committed
378
	struct tomoyo_aggregator e = { };
379 380 381 382
	int error = param->is_delete ? -ENOENT : -ENOMEM;
	const char *original_name = tomoyo_read_token(param);
	const char *aggregated_name = tomoyo_read_token(param);
	if (!tomoyo_correct_word(original_name) ||
Tetsuo Handa's avatar
Tetsuo Handa committed
383
	    !tomoyo_correct_path(aggregated_name))
384 385 386 387 388 389
		return -EINVAL;
	e.original_name = tomoyo_get_name(original_name);
	e.aggregated_name = tomoyo_get_name(aggregated_name);
	if (!e.original_name || !e.aggregated_name ||
	    e.aggregated_name->is_patterned) /* No patterns allowed. */
		goto out;
390
	param->list = &param->ns->policy_list[TOMOYO_ID_AGGREGATOR];
391
	error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handa's avatar
Tetsuo Handa committed
392
				     tomoyo_same_aggregator);
393
out:
394 395 396 397 398
	tomoyo_put_name(e.original_name);
	tomoyo_put_name(e.aggregated_name);
	return error;
}

Kentaro Takeda's avatar
Kentaro Takeda committed
399
/**
400
 * tomoyo_find_namespace - Find specified namespace.
Kentaro Takeda's avatar
Kentaro Takeda committed
401
 *
402 403
 * @name: Name of namespace to find.
 * @len:  Length of @name.
Kentaro Takeda's avatar
Kentaro Takeda committed
404
 *
405 406
 * Returns pointer to "struct tomoyo_policy_namespace" if found,
 * NULL otherwise.
407 408
 *
 * Caller holds tomoyo_read_lock().
Kentaro Takeda's avatar
Kentaro Takeda committed
409
 */
410 411
static struct tomoyo_policy_namespace *tomoyo_find_namespace
(const char *name, const unsigned int len)
Kentaro Takeda's avatar
Kentaro Takeda committed
412
{
413 414 415 416 417 418 419 420 421
	struct tomoyo_policy_namespace *ns;
	list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
		if (strncmp(name, ns->name, len) ||
		    (name[len] && name[len] != ' '))
			continue;
		return ns;
	}
	return NULL;
}
Kentaro Takeda's avatar
Kentaro Takeda committed
422

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/**
 * tomoyo_assign_namespace - Create a new namespace.
 *
 * @domainname: Name of namespace to create.
 *
 * Returns pointer to "struct tomoyo_policy_namespace" on success,
 * NULL otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname)
{
	struct tomoyo_policy_namespace *ptr;
	struct tomoyo_policy_namespace *entry;
	const char *cp = domainname;
	unsigned int len = 0;
	while (*cp && *cp++ != ' ')
		len++;
	ptr = tomoyo_find_namespace(domainname, len);
	if (ptr)
		return ptr;
	if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname))
445
		return NULL;
446 447
	entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS);
	if (!entry)
448
		return NULL;
449 450
	if (mutex_lock_interruptible(&tomoyo_policy_lock))
		goto out;
451 452 453 454 455 456 457 458
	ptr = tomoyo_find_namespace(domainname, len);
	if (!ptr && tomoyo_memory_ok(entry)) {
		char *name = (char *) (entry + 1);
		ptr = entry;
		memmove(name, domainname, len);
		name[len] = '\0';
		entry->name = name;
		tomoyo_init_policy_namespace(entry);
459
		entry = NULL;
Kentaro Takeda's avatar
Kentaro Takeda committed
460
	}
461
	mutex_unlock(&tomoyo_policy_lock);
462
out:
463
	kfree(entry);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	return ptr;
}

/**
 * tomoyo_namespace_jump - Check for namespace jump.
 *
 * @domainname: Name of domain.
 *
 * Returns true if namespace differs, false otherwise.
 */
static bool tomoyo_namespace_jump(const char *domainname)
{
	const char *namespace = tomoyo_current_namespace()->name;
	const int len = strlen(namespace);
	return strncmp(domainname, namespace, len) ||
		(domainname[len] && domainname[len] != ' ');
}

/**
 * tomoyo_assign_domain - Create a domain or a namespace.
 *
 * @domainname: The name of domain.
 * @transit:    True if transit to domain found or created.
 *
 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
						const bool transit)
{
	struct tomoyo_domain_info e = { };
	struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
	bool created = false;
	if (entry) {
		if (transit) {
			/*
			 * Since namespace is created at runtime, profiles may
			 * not be created by the moment the process transits to
			 * that domain. Do not perform domain transition if
			 * profile for that domain is not yet created.
			 */
			if (!entry->ns->profile_ptr[entry->profile])
				return NULL;
		}
		return entry;
	}
	/* Requested domain does not exist. */
	/* Don't create requested domain if domainname is invalid. */
	if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 ||
	    !tomoyo_correct_domain(domainname))
		return NULL;
	/*
	 * Since definition of profiles and acl_groups may differ across
	 * namespaces, do not inherit "use_profile" and "use_group" settings
	 * by automatically creating requested domain upon domain transition.
	 */
	if (transit && tomoyo_namespace_jump(domainname))
		return NULL;
	e.ns = tomoyo_assign_namespace(domainname);
	if (!e.ns)
		return NULL;
	/*
	 * "use_profile" and "use_group" settings for automatically created
	 * domains are inherited from current domain. These are 0 for manually
	 * created domains.
	 */
	if (transit) {
		const struct tomoyo_domain_info *domain = tomoyo_domain();
		e.profile = domain->profile;
		e.group = domain->group;
	}
	e.domainname = tomoyo_get_name(domainname);
	if (!e.domainname)
		return NULL;
	if (mutex_lock_interruptible(&tomoyo_policy_lock))
		goto out;
	entry = tomoyo_find_domain(domainname);
	if (!entry) {
		entry = tomoyo_commit_ok(&e, sizeof(e));
		if (entry) {
			INIT_LIST_HEAD(&entry->acl_info_list);
			list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
			created = true;
		}
	}
	mutex_unlock(&tomoyo_policy_lock);
out:
	tomoyo_put_name(e.domainname);
	if (entry && transit) {
		if (created) {
			struct tomoyo_request_info r;
			tomoyo_init_request_info(&r, entry,
						 TOMOYO_MAC_FILE_EXECUTE);
			r.granted = false;
			tomoyo_write_log(&r, "use_profile %u\n",
					 entry->profile);
			tomoyo_write_log(&r, "use_group %u\n", entry->group);
		}
	}
	return entry;
Kentaro Takeda's avatar
Kentaro Takeda committed
565 566 567 568 569
}

/**
 * tomoyo_find_next_domain - Find a domain.
 *
570
 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda's avatar
Kentaro Takeda committed
571 572
 *
 * Returns 0 on success, negative value otherwise.
573 574
 *
 * Caller holds tomoyo_read_lock().
Kentaro Takeda's avatar
Kentaro Takeda committed
575
 */
576
int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda's avatar
Kentaro Takeda committed
577 578 579 580 581
{
	struct tomoyo_domain_info *old_domain = tomoyo_domain();
	struct tomoyo_domain_info *domain = NULL;
	const char *original_name = bprm->filename;
	int retval = -ENOMEM;
582
	bool need_kfree = false;
583
	bool reject_on_transition_failure = false;
584
	struct tomoyo_path_info rn = { }; /* real name */
585 586 587 588 589 590 591 592 593 594 595 596 597 598
	struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
	if (!ee)
		return -ENOMEM;
	ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
	if (!ee->tmp) {
		kfree(ee);
		return -ENOMEM;
	}
	/* ee->dump->data is allocated by tomoyo_dump_page(). */
	tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
	ee->r.ee = ee;
	ee->bprm = bprm;
	ee->r.obj = &ee->obj;
	ee->obj.path1 = bprm->file->f_path;
599
 retry:
600 601 602 603
	if (need_kfree) {
		kfree(rn.name);
		need_kfree = false;
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
604
	/* Get symlink's pathname of program. */
Kentaro Takeda's avatar
Kentaro Takeda committed
605
	retval = -ENOENT;
Tetsuo Handa's avatar
Tetsuo Handa committed
606
	rn.name = tomoyo_realpath_nofollow(original_name);
607
	if (!rn.name)
Kentaro Takeda's avatar
Kentaro Takeda committed
608
		goto out;
609 610 611
	tomoyo_fill_path_info(&rn);
	need_kfree = true;

612 613
	/* Check 'aggregator' directive. */
	{
Tetsuo Handa's avatar
Tetsuo Handa committed
614
		struct tomoyo_aggregator *ptr;
615 616 617 618
		struct list_head *list =
			&old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
		/* Check 'aggregator' directive. */
		list_for_each_entry_rcu(ptr, list, head.list) {
619
			if (ptr->head.is_deleted ||
620 621 622
			    !tomoyo_path_matches_pattern(&rn,
							 ptr->original_name))
				continue;
Tetsuo Handa's avatar
Tetsuo Handa committed
623
			kfree(rn.name);
624 625 626 627 628 629 630
			need_kfree = false;
			/* This is OK because it is read only. */
			rn = *ptr->aggregated_name;
			break;
		}
	}

Kentaro Takeda's avatar
Kentaro Takeda committed
631
	/* Check execute permission. */
632
	retval = tomoyo_path_permission(&ee->r, TOMOYO_TYPE_EXECUTE, &rn);
633 634
	if (retval == TOMOYO_RETRY_REQUEST)
		goto retry;
Kentaro Takeda's avatar
Kentaro Takeda committed
635 636
	if (retval < 0)
		goto out;
637 638 639 640 641 642
	/*
	 * To be able to specify domainnames with wildcards, use the
	 * pathname specified in the policy (which may contain
	 * wildcard) rather than the pathname passed to execve()
	 * (which never contains wildcard).
	 */
643
	if (ee->r.param.path.matched_path) {
644 645 646 647
		if (need_kfree)
			kfree(rn.name);
		need_kfree = false;
		/* This is OK because it is read only. */
648
		rn = *ee->r.param.path.matched_path;
649
	}
Kentaro Takeda's avatar
Kentaro Takeda committed
650

651
	/* Calculate domain to transit to. */
652 653 654 655
	switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
				       &rn)) {
	case TOMOYO_TRANSITION_CONTROL_RESET:
		/* Transit to the root of specified namespace. */
656
		snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", rn.name);
657 658 659 660 661 662
		/*
		 * Make do_execve() fail if domain transition across namespaces
		 * has failed.
		 */
		reject_on_transition_failure = true;
		break;
663
	case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
664
		/* Transit to the child of current namespace's root. */
665
		snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
666
			 old_domain->ns->name, rn.name);
667 668
		break;
	case TOMOYO_TRANSITION_CONTROL_KEEP:
Kentaro Takeda's avatar
Kentaro Takeda committed
669 670
		/* Keep current domain. */
		domain = old_domain;
671 672 673 674 675 676 677 678 679 680 681 682 683
		break;
	default:
		if (old_domain == &tomoyo_kernel_domain &&
		    !tomoyo_policy_loaded) {
			/*
			 * Needn't to transit from kernel domain before
			 * starting /sbin/init. But transit from kernel domain
			 * if executing initializers because they might start
			 * before /sbin/init.
			 */
			domain = old_domain;
		} else {
			/* Normal domain transition. */
684
			snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
685 686 687
				 old_domain->domainname->name, rn.name);
		}
		break;
Kentaro Takeda's avatar
Kentaro Takeda committed
688
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
689
	if (!domain)
690
		domain = tomoyo_assign_domain(ee->tmp, true);
Kentaro Takeda's avatar
Kentaro Takeda committed
691
	if (domain)
692 693
		retval = 0;
	else if (reject_on_transition_failure) {
694 695
		printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n",
		       ee->tmp);
696
		retval = -ENOMEM;
697
	} else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
698 699 700
		retval = -ENOMEM;
	else {
		retval = 0;
Tetsuo Handa's avatar
Tetsuo Handa committed
701 702
		if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
			old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
703 704
			ee->r.granted = false;
			tomoyo_write_log(&ee->r, "%s", tomoyo_dif
Tetsuo Handa's avatar
Tetsuo Handa committed
705
					 [TOMOYO_DIF_TRANSITION_FAILED]);
706
			printk(KERN_WARNING
707
			       "ERROR: Domain '%s' not defined.\n", ee->tmp);
708 709
		}
	}
Kentaro Takeda's avatar
Kentaro Takeda committed
710
 out:
711 712
	if (!domain)
		domain = old_domain;
713 714
	/* Update reference count on "struct tomoyo_domain_info". */
	atomic_inc(&domain->users);
715
	bprm->cred->security = domain;
716 717
	if (need_kfree)
		kfree(rn.name);
718 719 720
	kfree(ee->tmp);
	kfree(ee->dump.data);
	kfree(ee);
Kentaro Takeda's avatar
Kentaro Takeda committed
721 722
	return retval;
}
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768

/**
 * tomoyo_dump_page - Dump a page to buffer.
 *
 * @bprm: Pointer to "struct linux_binprm".
 * @pos:  Location to dump.
 * @dump: Poiner to "struct tomoyo_page_dump".
 *
 * Returns true on success, false otherwise.
 */
bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
		      struct tomoyo_page_dump *dump)
{
	struct page *page;
	/* dump->data is released by tomoyo_finish_execve(). */
	if (!dump->data) {
		dump->data = kzalloc(PAGE_SIZE, GFP_NOFS);
		if (!dump->data)
			return false;
	}
	/* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
#ifdef CONFIG_MMU
	if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
		return false;
#else
	page = bprm->page[pos / PAGE_SIZE];
#endif
	if (page != dump->page) {
		const unsigned int offset = pos % PAGE_SIZE;
		/*
		 * Maybe kmap()/kunmap() should be used here.
		 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
		 * So do I.
		 */
		char *kaddr = kmap_atomic(page, KM_USER0);
		dump->page = page;
		memcpy(dump->data + offset, kaddr + offset,
		       PAGE_SIZE - offset);
		kunmap_atomic(kaddr, KM_USER0);
	}
	/* Same with put_arg_page(page) in fs/exec.c */
#ifdef CONFIG_MMU
	put_page(page);
#endif
	return true;
}