file.c 29.7 KB
Newer Older
1 2 3
/*
 * security/tomoyo/file.c
 *
4
 * Pathname restriction functions.
5
 *
6
 * Copyright (C) 2005-2010  NTT DATA CORPORATION
7 8 9
 */

#include "common.h"
10
#include <linux/slab.h>
11

12
/* Keyword array for operations with one pathname. */
13
const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
T
Tetsuo Handa 已提交
14 15 16
	[TOMOYO_TYPE_EXECUTE]    = "execute",
	[TOMOYO_TYPE_READ]       = "read",
	[TOMOYO_TYPE_WRITE]      = "write",
T
Tetsuo Handa 已提交
17
	[TOMOYO_TYPE_APPEND]     = "append",
T
Tetsuo Handa 已提交
18
	[TOMOYO_TYPE_UNLINK]     = "unlink",
T
Tetsuo Handa 已提交
19
	[TOMOYO_TYPE_GETATTR]    = "getattr",
T
Tetsuo Handa 已提交
20 21 22 23 24
	[TOMOYO_TYPE_RMDIR]      = "rmdir",
	[TOMOYO_TYPE_TRUNCATE]   = "truncate",
	[TOMOYO_TYPE_SYMLINK]    = "symlink",
	[TOMOYO_TYPE_CHROOT]     = "chroot",
	[TOMOYO_TYPE_UMOUNT]     = "unmount",
25 26
};

27
/* Keyword array for operations with one pathname and three numbers. */
28
const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
29 30 31 32 33
	[TOMOYO_TYPE_MKBLOCK]    = "mkblock",
	[TOMOYO_TYPE_MKCHAR]     = "mkchar",
};

/* Keyword array for operations with two pathnames. */
34
const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
35 36
	[TOMOYO_TYPE_LINK]       = "link",
	[TOMOYO_TYPE_RENAME]     = "rename",
T
Tetsuo Handa 已提交
37
	[TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
38 39
};

40
/* Keyword array for operations with one pathname and one number. */
41
const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
42 43 44 45 46 47 48 49 50 51
	[TOMOYO_TYPE_CREATE]     = "create",
	[TOMOYO_TYPE_MKDIR]      = "mkdir",
	[TOMOYO_TYPE_MKFIFO]     = "mkfifo",
	[TOMOYO_TYPE_MKSOCK]     = "mksock",
	[TOMOYO_TYPE_IOCTL]      = "ioctl",
	[TOMOYO_TYPE_CHMOD]      = "chmod",
	[TOMOYO_TYPE_CHOWN]      = "chown",
	[TOMOYO_TYPE_CHGRP]      = "chgrp",
};

T
Tetsuo Handa 已提交
52 53 54
/*
 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
 */
T
Tetsuo Handa 已提交
55 56 57 58
static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
	[TOMOYO_TYPE_EXECUTE]    = TOMOYO_MAC_FILE_EXECUTE,
	[TOMOYO_TYPE_READ]       = TOMOYO_MAC_FILE_OPEN,
	[TOMOYO_TYPE_WRITE]      = TOMOYO_MAC_FILE_OPEN,
T
Tetsuo Handa 已提交
59
	[TOMOYO_TYPE_APPEND]     = TOMOYO_MAC_FILE_OPEN,
T
Tetsuo Handa 已提交
60
	[TOMOYO_TYPE_UNLINK]     = TOMOYO_MAC_FILE_UNLINK,
T
Tetsuo Handa 已提交
61
	[TOMOYO_TYPE_GETATTR]    = TOMOYO_MAC_FILE_GETATTR,
T
Tetsuo Handa 已提交
62 63 64 65 66 67 68
	[TOMOYO_TYPE_RMDIR]      = TOMOYO_MAC_FILE_RMDIR,
	[TOMOYO_TYPE_TRUNCATE]   = TOMOYO_MAC_FILE_TRUNCATE,
	[TOMOYO_TYPE_SYMLINK]    = TOMOYO_MAC_FILE_SYMLINK,
	[TOMOYO_TYPE_CHROOT]     = TOMOYO_MAC_FILE_CHROOT,
	[TOMOYO_TYPE_UMOUNT]     = TOMOYO_MAC_FILE_UMOUNT,
};

T
Tetsuo Handa 已提交
69 70 71
/*
 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
 */
T
Tetsuo Handa 已提交
72
static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
T
Tetsuo Handa 已提交
73 74 75 76
	[TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
	[TOMOYO_TYPE_MKCHAR]  = TOMOYO_MAC_FILE_MKCHAR,
};

T
Tetsuo Handa 已提交
77 78 79
/*
 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
 */
T
Tetsuo Handa 已提交
80 81 82 83 84 85
static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
	[TOMOYO_TYPE_LINK]       = TOMOYO_MAC_FILE_LINK,
	[TOMOYO_TYPE_RENAME]     = TOMOYO_MAC_FILE_RENAME,
	[TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
};

T
Tetsuo Handa 已提交
86 87 88 89
/*
 * Mapping table from "enum tomoyo_path_number_acl_index" to
 * "enum tomoyo_mac_index".
 */
T
Tetsuo Handa 已提交
90 91 92 93 94 95 96 97 98 99 100
static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
	[TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
	[TOMOYO_TYPE_MKDIR]  = TOMOYO_MAC_FILE_MKDIR,
	[TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
	[TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
	[TOMOYO_TYPE_IOCTL]  = TOMOYO_MAC_FILE_IOCTL,
	[TOMOYO_TYPE_CHMOD]  = TOMOYO_MAC_FILE_CHMOD,
	[TOMOYO_TYPE_CHOWN]  = TOMOYO_MAC_FILE_CHOWN,
	[TOMOYO_TYPE_CHGRP]  = TOMOYO_MAC_FILE_CHGRP,
};

T
Tetsuo Handa 已提交
101 102 103 104 105 106 107
/**
 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
 *
 * @ptr: Pointer to "struct tomoyo_name_union".
 *
 * Returns nothing.
 */
108 109
void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
{
T
Tetsuo Handa 已提交
110 111
	tomoyo_put_group(ptr->group);
	tomoyo_put_name(ptr->filename);
112 113
}

T
Tetsuo Handa 已提交
114 115 116 117 118 119 120 121
/**
 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
 *
 * @name: Pointer to "struct tomoyo_path_info".
 * @ptr:  Pointer to "struct tomoyo_name_union".
 *
 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
 */
122 123 124
const struct tomoyo_path_info *
tomoyo_compare_name_union(const struct tomoyo_path_info *name,
			  const struct tomoyo_name_union *ptr)
125
{
T
Tetsuo Handa 已提交
126
	if (ptr->group)
127
		return tomoyo_path_matches_group(name, ptr->group);
128 129 130
	if (tomoyo_path_matches_pattern(name, ptr->filename))
		return ptr->filename;
	return NULL;
131 132
}

T
Tetsuo Handa 已提交
133 134 135 136 137 138 139
/**
 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
 *
 * @ptr: Pointer to "struct tomoyo_number_union".
 *
 * Returns nothing.
 */
140 141
void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
{
T
Tetsuo Handa 已提交
142
	tomoyo_put_group(ptr->group);
143 144
}

T
Tetsuo Handa 已提交
145 146 147 148 149 150 151 152
/**
 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
 *
 * @value: Number to check.
 * @ptr:   Pointer to "struct tomoyo_number_union".
 *
 * Returns true if @value matches @ptr, false otherwise.
 */
153 154 155
bool tomoyo_compare_number_union(const unsigned long value,
				 const struct tomoyo_number_union *ptr)
{
T
Tetsuo Handa 已提交
156
	if (ptr->group)
157 158 159 160
		return tomoyo_number_matches_group(value, value, ptr->group);
	return value >= ptr->values[0] && value <= ptr->values[1];
}

T
Tetsuo Handa 已提交
161 162 163 164 165 166 167 168 169 170
/**
 * tomoyo_add_slash - Add trailing '/' if needed.
 *
 * @buf: Pointer to "struct tomoyo_path_info".
 *
 * Returns nothing.
 *
 * @buf must be generated by tomoyo_encode() because this function does not
 * allocate memory for adding '/'.
 */
T
Tetsuo Handa 已提交
171 172 173 174 175 176 177 178 179 180 181
static void tomoyo_add_slash(struct tomoyo_path_info *buf)
{
	if (buf->is_dir)
		return;
	/*
	 * This is OK because tomoyo_encode() reserves space for appending "/".
	 */
	strcat((char *) buf->name, "/");
	tomoyo_fill_path_info(buf);
}

182
/**
T
Tetsuo Handa 已提交
183
 * tomoyo_get_realpath - Get realpath.
184
 *
T
Tetsuo Handa 已提交
185
 * @buf:  Pointer to "struct tomoyo_path_info".
186 187
 * @path: Pointer to "struct path".
 *
T
Tetsuo Handa 已提交
188
 * Returns true on success, false otherwise.
189
 */
T
Tetsuo Handa 已提交
190
static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
191
{
T
Tetsuo Handa 已提交
192 193 194 195
	buf->name = tomoyo_realpath_from_path(path);
	if (buf->name) {
		tomoyo_fill_path_info(buf);
		return true;
196
	}
T
Tetsuo Handa 已提交
197
        return false;
198 199
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/**
 * tomoyo_audit_path_log - Audit path request log.
 *
 * @r: Pointer to "struct tomoyo_request_info".
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
{
	const char *operation = tomoyo_path_keyword[r->param.path.operation];
	const struct tomoyo_path_info *filename = r->param.path.filename;
	if (r->granted)
		return 0;
	tomoyo_warn_log(r, "%s %s", operation, filename->name);
	return tomoyo_supervisor(r, "allow_%s %s\n", operation,
T
Tetsuo Handa 已提交
215
				 filename->name);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

/**
 * tomoyo_audit_path2_log - Audit path/path request log.
 *
 * @r: Pointer to "struct tomoyo_request_info".
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
{
	const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
	const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
	const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
	if (r->granted)
		return 0;
	tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
			filename2->name);
	return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
T
Tetsuo Handa 已提交
235
				 filename1->name, filename2->name);
236 237 238 239 240 241 242 243 244 245 246
}

/**
 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
 *
 * @r: Pointer to "struct tomoyo_request_info".
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
{
247
	const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
248 249 250 251 252 253 254 255 256
	const struct tomoyo_path_info *filename = r->param.mkdev.filename;
	const unsigned int major = r->param.mkdev.major;
	const unsigned int minor = r->param.mkdev.minor;
	const unsigned int mode = r->param.mkdev.mode;
	if (r->granted)
		return 0;
	tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
			major, minor);
	return tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", operation,
T
Tetsuo Handa 已提交
257
				 filename->name, mode, major, minor);
258 259 260 261 262
}

/**
 * tomoyo_audit_path_number_log - Audit path/number request log.
 *
T
Tetsuo Handa 已提交
263
 * @r: Pointer to "struct tomoyo_request_info".
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
{
	const u8 type = r->param.path_number.operation;
	u8 radix;
	const struct tomoyo_path_info *filename = r->param.path_number.filename;
	const char *operation = tomoyo_path_number_keyword[type];
	char buffer[64];
	if (r->granted)
		return 0;
	switch (type) {
	case TOMOYO_TYPE_CREATE:
	case TOMOYO_TYPE_MKDIR:
	case TOMOYO_TYPE_MKFIFO:
	case TOMOYO_TYPE_MKSOCK:
	case TOMOYO_TYPE_CHMOD:
		radix = TOMOYO_VALUE_TYPE_OCTAL;
		break;
	case TOMOYO_TYPE_IOCTL:
		radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
		break;
	default:
		radix = TOMOYO_VALUE_TYPE_DECIMAL;
		break;
	}
	tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
			   radix);
	tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
	return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
T
Tetsuo Handa 已提交
295
				 filename->name, buffer);
296 297
}

T
Tetsuo Handa 已提交
298 299 300 301 302 303 304 305 306 307 308 309
/**
 * tomoyo_check_path_acl - Check permission for path operation.
 *
 * @r:   Pointer to "struct tomoyo_request_info".
 * @ptr: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if granted, false otherwise.
 *
 * To be able to use wildcard for domain transition, this function sets
 * matching entry on success. Since the caller holds tomoyo_read_lock(),
 * it is safe to set matching entry.
 */
310
static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
311
				  const struct tomoyo_acl_info *ptr)
312
{
313 314
	const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
							 head);
315 316 317 318 319 320 321
	if (acl->perm & (1 << r->param.path.operation)) {
		r->param.path.matched_path =
			tomoyo_compare_name_union(r->param.path.filename,
						  &acl->name);
		return r->param.path.matched_path != NULL;
	}
	return false;
322
}
323

T
Tetsuo Handa 已提交
324 325 326 327 328 329 330 331
/**
 * tomoyo_check_path_number_acl - Check permission for path number operation.
 *
 * @r:   Pointer to "struct tomoyo_request_info".
 * @ptr: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if granted, false otherwise.
 */
332
static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
333 334 335 336 337 338 339 340 341 342 343
					 const struct tomoyo_acl_info *ptr)
{
	const struct tomoyo_path_number_acl *acl =
		container_of(ptr, typeof(*acl), head);
	return (acl->perm & (1 << r->param.path_number.operation)) &&
		tomoyo_compare_number_union(r->param.path_number.number,
					    &acl->number) &&
		tomoyo_compare_name_union(r->param.path_number.filename,
					  &acl->name);
}

T
Tetsuo Handa 已提交
344 345 346 347 348 349 350 351
/**
 * tomoyo_check_path2_acl - Check permission for path path operation.
 *
 * @r:   Pointer to "struct tomoyo_request_info".
 * @ptr: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if granted, false otherwise.
 */
352
static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
353 354 355 356 357 358 359 360 361 362
				   const struct tomoyo_acl_info *ptr)
{
	const struct tomoyo_path2_acl *acl =
		container_of(ptr, typeof(*acl), head);
	return (acl->perm & (1 << r->param.path2.operation)) &&
		tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
		&& tomoyo_compare_name_union(r->param.path2.filename2,
					     &acl->name2);
}

T
Tetsuo Handa 已提交
363 364 365 366 367 368 369 370
/**
 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
 *
 * @r:   Pointer to "struct tomoyo_request_info".
 * @ptr: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if granted, false otherwise.
 */
371
static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
T
Tetsuo Handa 已提交
372
				   const struct tomoyo_acl_info *ptr)
373
{
T
Tetsuo Handa 已提交
374
	const struct tomoyo_mkdev_acl *acl =
375 376 377 378 379 380 381 382 383 384
		container_of(ptr, typeof(*acl), head);
	return (acl->perm & (1 << r->param.mkdev.operation)) &&
		tomoyo_compare_number_union(r->param.mkdev.mode,
					    &acl->mode) &&
		tomoyo_compare_number_union(r->param.mkdev.major,
					    &acl->major) &&
		tomoyo_compare_number_union(r->param.mkdev.minor,
					    &acl->minor) &&
		tomoyo_compare_name_union(r->param.mkdev.filename,
					  &acl->name);
385 386
}

T
Tetsuo Handa 已提交
387 388 389 390 391 392 393 394
/**
 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_info".
 * @b: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if @a == @b except permission bits, false otherwise.
 */
395 396 397 398 399
static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
				 const struct tomoyo_acl_info *b)
{
	const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
	const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
T
Tetsuo Handa 已提交
400
	return tomoyo_same_name_union(&p1->name, &p2->name);
401 402
}

T
Tetsuo Handa 已提交
403 404 405 406 407 408 409 410 411
/**
 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
 *
 * @a:         Pointer to "struct tomoyo_acl_info".
 * @b:         Pointer to "struct tomoyo_acl_info".
 * @is_delete: True for @a &= ~@b, false for @a |= @b.
 *
 * Returns true if @a is empty, false otherwise.
 */
412 413 414 415 416 417 418 419
static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
				  struct tomoyo_acl_info *b,
				  const bool is_delete)
{
	u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
		->perm;
	u16 perm = *a_perm;
	const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
T
Tetsuo Handa 已提交
420
	if (is_delete)
421
		perm &= ~b_perm;
T
Tetsuo Handa 已提交
422
	else
423 424 425 426 427
		perm |= b_perm;
	*a_perm = perm;
	return !perm;
}

428
/**
T
Tetsuo Handa 已提交
429
 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
430
 *
431 432
 * @perm:  Permission.
 * @param: Pointer to "struct tomoyo_acl_param".
433 434
 *
 * Returns 0 on success, negative value otherwise.
435 436
 *
 * Caller holds tomoyo_read_lock().
437
 */
438 439
static int tomoyo_update_path_acl(const u16 perm,
				  struct tomoyo_acl_param *param)
440
{
441 442
	struct tomoyo_path_acl e = {
		.head.type = TOMOYO_TYPE_PATH_ACL,
443
		.perm = perm
444
	};
445
	int error;
446 447 448 449 450 451
	if (!tomoyo_parse_name_union(param, &e.name))
		error = -EINVAL;
	else
		error = tomoyo_update_domain(&e.head, sizeof(e), param,
					     tomoyo_same_path_acl,
					     tomoyo_merge_path_acl);
452
	tomoyo_put_name_union(&e.name);
453 454 455
	return error;
}

T
Tetsuo Handa 已提交
456 457 458 459 460 461 462 463
/**
 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_info".
 * @b: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if @a == @b except permission bits, false otherwise.
 */
T
Tetsuo Handa 已提交
464
static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
465 466
					 const struct tomoyo_acl_info *b)
{
T
Tetsuo Handa 已提交
467 468 469 470 471 472
	const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
	const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
	return tomoyo_same_name_union(&p1->name, &p2->name) &&
		tomoyo_same_number_union(&p1->mode, &p2->mode) &&
		tomoyo_same_number_union(&p1->major, &p2->major) &&
		tomoyo_same_number_union(&p1->minor, &p2->minor);
473 474
}

T
Tetsuo Handa 已提交
475 476 477 478 479 480 481 482 483
/**
 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
 *
 * @a:         Pointer to "struct tomoyo_acl_info".
 * @b:         Pointer to "struct tomoyo_acl_info".
 * @is_delete: True for @a &= ~@b, false for @a |= @b.
 *
 * Returns true if @a is empty, false otherwise.
 */
T
Tetsuo Handa 已提交
484
static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
T
Tetsuo Handa 已提交
485 486
				   struct tomoyo_acl_info *b,
				   const bool is_delete)
487
{
T
Tetsuo Handa 已提交
488
	u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
489 490
					 head)->perm;
	u8 perm = *a_perm;
T
Tetsuo Handa 已提交
491
	const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
492 493 494 495 496 497 498 499 500
		->perm;
	if (is_delete)
		perm &= ~b_perm;
	else
		perm |= b_perm;
	*a_perm = perm;
	return !perm;
}

501
/**
T
Tetsuo Handa 已提交
502
 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
503
 *
504 505
 * @perm:  Permission.
 * @param: Pointer to "struct tomoyo_acl_param".
506 507
 *
 * Returns 0 on success, negative value otherwise.
508 509
 *
 * Caller holds tomoyo_read_lock().
510
 */
511 512
static int tomoyo_update_mkdev_acl(const u8 perm,
				   struct tomoyo_acl_param *param)
513
{
T
Tetsuo Handa 已提交
514 515
	struct tomoyo_mkdev_acl e = {
		.head.type = TOMOYO_TYPE_MKDEV_ACL,
516
		.perm = perm
517
	};
518 519 520 521 522 523 524 525 526 527
	int error;
	if (!tomoyo_parse_name_union(param, &e.name) ||
	    !tomoyo_parse_number_union(param, &e.mode) ||
	    !tomoyo_parse_number_union(param, &e.major) ||
	    !tomoyo_parse_number_union(param, &e.minor))
		error = -EINVAL;
	else
		error = tomoyo_update_domain(&e.head, sizeof(e), param,
					     tomoyo_same_mkdev_acl,
					     tomoyo_merge_mkdev_acl);
528 529 530 531 532 533 534
	tomoyo_put_name_union(&e.name);
	tomoyo_put_number_union(&e.mode);
	tomoyo_put_number_union(&e.major);
	tomoyo_put_number_union(&e.minor);
	return error;
}

T
Tetsuo Handa 已提交
535 536 537 538 539 540 541 542
/**
 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_info".
 * @b: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if @a == @b except permission bits, false otherwise.
 */
543 544 545 546 547
static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
				  const struct tomoyo_acl_info *b)
{
	const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
	const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
T
Tetsuo Handa 已提交
548 549
	return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
		tomoyo_same_name_union(&p1->name2, &p2->name2);
550 551
}

T
Tetsuo Handa 已提交
552 553 554 555 556 557 558 559 560
/**
 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
 *
 * @a:         Pointer to "struct tomoyo_acl_info".
 * @b:         Pointer to "struct tomoyo_acl_info".
 * @is_delete: True for @a &= ~@b, false for @a |= @b.
 *
 * Returns true if @a is empty, false otherwise.
 */
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
				   struct tomoyo_acl_info *b,
				   const bool is_delete)
{
	u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
		->perm;
	u8 perm = *a_perm;
	const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
	if (is_delete)
		perm &= ~b_perm;
	else
		perm |= b_perm;
	*a_perm = perm;
	return !perm;
}

577
/**
T
Tetsuo Handa 已提交
578
 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
579
 *
580 581
 * @perm:  Permission.
 * @param: Pointer to "struct tomoyo_acl_param".
582 583
 *
 * Returns 0 on success, negative value otherwise.
584 585
 *
 * Caller holds tomoyo_read_lock().
586
 */
587 588
static int tomoyo_update_path2_acl(const u8 perm,
				   struct tomoyo_acl_param *param)
589
{
590 591
	struct tomoyo_path2_acl e = {
		.head.type = TOMOYO_TYPE_PATH2_ACL,
592
		.perm = perm
593
	};
594 595 596 597 598 599 600 601
	int error;
	if (!tomoyo_parse_name_union(param, &e.name1) ||
	    !tomoyo_parse_name_union(param, &e.name2))
		error = -EINVAL;
	else
		error = tomoyo_update_domain(&e.head, sizeof(e), param,
					     tomoyo_same_path2_acl,
					     tomoyo_merge_path2_acl);
602 603
	tomoyo_put_name_union(&e.name1);
	tomoyo_put_name_union(&e.name2);
604 605 606 607
	return error;
}

/**
608
 * tomoyo_path_permission - Check permission for single path operation.
609
 *
610
 * @r:         Pointer to "struct tomoyo_request_info".
611 612 613 614
 * @operation: Type of operation.
 * @filename:  Filename to check.
 *
 * Returns 0 on success, negative value otherwise.
615 616
 *
 * Caller holds tomoyo_read_lock().
617
 */
618 619
int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
			   const struct tomoyo_path_info *filename)
620 621 622
{
	int error;

T
Tetsuo Handa 已提交
623 624 625 626
	r->type = tomoyo_p2mac[operation];
	r->mode = tomoyo_get_mode(r->profile, r->type);
	if (r->mode == TOMOYO_CONFIG_DISABLED)
		return 0;
627 628 629
	r->param_type = TOMOYO_TYPE_PATH_ACL;
	r->param.path.filename = filename;
	r->param.path.operation = operation;
630
	do {
631 632
		tomoyo_check_acl(r, tomoyo_check_path_acl);
		error = tomoyo_audit_path_log(r);
633 634 635 636 637 638
		/*
		 * Do not retry for execute request, for alias may have
		 * changed.
		 */
	} while (error == TOMOYO_RETRY_REQUEST &&
		 operation != TOMOYO_TYPE_EXECUTE);
639 640 641
	return error;
}

T
Tetsuo Handa 已提交
642 643 644 645 646 647 648 649
/**
 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_info".
 * @b: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if @a == @b except permission bits, false otherwise.
 */
650 651 652 653 654 655 656
static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
					const struct tomoyo_acl_info *b)
{
	const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
							       head);
	const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
							       head);
T
Tetsuo Handa 已提交
657 658
	return tomoyo_same_name_union(&p1->name, &p2->name) &&
		tomoyo_same_number_union(&p1->number, &p2->number);
659 660
}

T
Tetsuo Handa 已提交
661 662 663 664 665 666 667 668 669
/**
 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
 *
 * @a:         Pointer to "struct tomoyo_acl_info".
 * @b:         Pointer to "struct tomoyo_acl_info".
 * @is_delete: True for @a &= ~@b, false for @a |= @b.
 *
 * Returns true if @a is empty, false otherwise.
 */
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
					 struct tomoyo_acl_info *b,
					 const bool is_delete)
{
	u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
					  head)->perm;
	u8 perm = *a_perm;
	const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
		->perm;
	if (is_delete)
		perm &= ~b_perm;
	else
		perm |= b_perm;
	*a_perm = perm;
	return !perm;
}

687 688 689
/**
 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
 *
690 691
 * @perm:  Permission.
 * @param: Pointer to "struct tomoyo_acl_param".
692 693 694
 *
 * Returns 0 on success, negative value otherwise.
 */
695 696
static int tomoyo_update_path_number_acl(const u8 perm,
					 struct tomoyo_acl_param *param)
697 698 699
{
	struct tomoyo_path_number_acl e = {
		.head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
700
		.perm = perm
701
	};
702 703 704 705 706 707 708 709
	int error;
	if (!tomoyo_parse_name_union(param, &e.name) ||
	    !tomoyo_parse_number_union(param, &e.number))
		error = -EINVAL;
	else
		error = tomoyo_update_domain(&e.head, sizeof(e), param,
					     tomoyo_same_path_number_acl,
					     tomoyo_merge_path_number_acl);
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	tomoyo_put_name_union(&e.name);
	tomoyo_put_number_union(&e.number);
	return error;
}

/**
 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
 *
 * @type:   Type of operation.
 * @path:   Pointer to "struct path".
 * @number: Number.
 *
 * Returns 0 on success, negative value otherwise.
 */
int tomoyo_path_number_perm(const u8 type, struct path *path,
			    unsigned long number)
{
	struct tomoyo_request_info r;
	int error = -ENOMEM;
T
Tetsuo Handa 已提交
729
	struct tomoyo_path_info buf;
730 731
	int idx;

T
Tetsuo Handa 已提交
732 733
	if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
	    == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
734 735
		return 0;
	idx = tomoyo_read_lock();
T
Tetsuo Handa 已提交
736
	if (!tomoyo_get_realpath(&buf, path))
737
		goto out;
T
Tetsuo Handa 已提交
738 739
	if (type == TOMOYO_TYPE_MKDIR)
		tomoyo_add_slash(&buf);
T
Tetsuo Handa 已提交
740 741 742 743 744 745 746 747
	r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
	r.param.path_number.operation = type;
	r.param.path_number.filename = &buf;
	r.param.path_number.number = number;
	do {
		tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
		error = tomoyo_audit_path_number_log(&r);
	} while (error == TOMOYO_RETRY_REQUEST);
T
Tetsuo Handa 已提交
748
	kfree(buf.name);
T
Tetsuo Handa 已提交
749
 out:
750 751 752 753 754 755
	tomoyo_read_unlock(idx);
	if (r.mode != TOMOYO_CONFIG_ENFORCING)
		error = 0;
	return error;
}

756 757 758 759 760 761 762 763 764 765 766 767 768
/**
 * tomoyo_check_open_permission - Check permission for "read" and "write".
 *
 * @domain: Pointer to "struct tomoyo_domain_info".
 * @path:   Pointer to "struct path".
 * @flag:   Flags for open().
 *
 * Returns 0 on success, negative value otherwise.
 */
int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
				 struct path *path, const int flag)
{
	const u8 acc_mode = ACC_MODE(flag);
769
	int error = 0;
T
Tetsuo Handa 已提交
770
	struct tomoyo_path_info buf;
771
	struct tomoyo_request_info r;
772
	int idx;
773

T
Tetsuo Handa 已提交
774
	if (!path->mnt)
775
		return 0;
T
Tetsuo Handa 已提交
776 777
	buf.name = NULL;
	r.mode = TOMOYO_CONFIG_DISABLED;
778
	idx = tomoyo_read_lock();
T
Tetsuo Handa 已提交
779 780
	if (acc_mode &&
	    tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
T
Tetsuo Handa 已提交
781 782 783 784 785
	    != TOMOYO_CONFIG_DISABLED) {
		if (!tomoyo_get_realpath(&buf, path)) {
			error = -ENOMEM;
			goto out;
		}
T
Tetsuo Handa 已提交
786 787 788 789 790 791 792
		if (acc_mode & MAY_READ)
			error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
						       &buf);
		if (!error && (acc_mode & MAY_WRITE))
			error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
						       TOMOYO_TYPE_APPEND :
						       TOMOYO_TYPE_WRITE,
T
Tetsuo Handa 已提交
793 794
						       &buf);
	}
795
 out:
T
Tetsuo Handa 已提交
796
	kfree(buf.name);
797
	tomoyo_read_unlock(idx);
798
	if (r.mode != TOMOYO_CONFIG_ENFORCING)
799 800 801 802 803
		error = 0;
	return error;
}

/**
T
Tetsuo Handa 已提交
804
 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
805 806 807 808 809 810
 *
 * @operation: Type of operation.
 * @path:      Pointer to "struct path".
 *
 * Returns 0 on success, negative value otherwise.
 */
811
int tomoyo_path_perm(const u8 operation, struct path *path)
812
{
813
	struct tomoyo_request_info r;
T
Tetsuo Handa 已提交
814 815 816
	int error;
	struct tomoyo_path_info buf;
	bool is_enforce;
817
	int idx;
818

T
Tetsuo Handa 已提交
819 820 821 822
	if (!path->mnt)
		return 0;
	if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
	    == TOMOYO_CONFIG_DISABLED)
823
		return 0;
T
Tetsuo Handa 已提交
824 825
	is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
	error = -ENOMEM;
T
Tetsuo Handa 已提交
826
	buf.name = NULL;
827
	idx = tomoyo_read_lock();
T
Tetsuo Handa 已提交
828
	if (!tomoyo_get_realpath(&buf, path))
829 830
		goto out;
	switch (operation) {
T
Tetsuo Handa 已提交
831 832
	case TOMOYO_TYPE_RMDIR:
	case TOMOYO_TYPE_CHROOT:
T
Tetsuo Handa 已提交
833 834
		tomoyo_add_slash(&buf);
		break;
835
	}
T
Tetsuo Handa 已提交
836
	error = tomoyo_path_permission(&r, operation, &buf);
837
 out:
T
Tetsuo Handa 已提交
838
	kfree(buf.name);
839
	tomoyo_read_unlock(idx);
T
Tetsuo Handa 已提交
840
	if (!is_enforce)
841 842 843 844
		error = 0;
	return error;
}

845
/**
T
Tetsuo Handa 已提交
846
 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
847 848 849 850 851 852 853 854
 *
 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
 * @path:      Pointer to "struct path".
 * @mode:      Create mode.
 * @dev:       Device number.
 *
 * Returns 0 on success, negative value otherwise.
 */
T
Tetsuo Handa 已提交
855
int tomoyo_mkdev_perm(const u8 operation, struct path *path,
T
Tetsuo Handa 已提交
856
		      const unsigned int mode, unsigned int dev)
857 858 859
{
	struct tomoyo_request_info r;
	int error = -ENOMEM;
T
Tetsuo Handa 已提交
860
	struct tomoyo_path_info buf;
861 862
	int idx;

T
Tetsuo Handa 已提交
863 864 865
	if (!path->mnt ||
	    tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
	    == TOMOYO_CONFIG_DISABLED)
866 867 868
		return 0;
	idx = tomoyo_read_lock();
	error = -ENOMEM;
T
Tetsuo Handa 已提交
869
	if (tomoyo_get_realpath(&buf, path)) {
870
		dev = new_decode_dev(dev);
T
Tetsuo Handa 已提交
871
		r.param_type = TOMOYO_TYPE_MKDEV_ACL;
872 873 874 875 876
		r.param.mkdev.filename = &buf;
		r.param.mkdev.operation = operation;
		r.param.mkdev.mode = mode;
		r.param.mkdev.major = MAJOR(dev);
		r.param.mkdev.minor = MINOR(dev);
877 878
		tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
		error = tomoyo_audit_mkdev_log(&r);
T
Tetsuo Handa 已提交
879
		kfree(buf.name);
880 881 882 883 884 885 886
	}
	tomoyo_read_unlock(idx);
	if (r.mode != TOMOYO_CONFIG_ENFORCING)
		error = 0;
	return error;
}

887
/**
T
Tetsuo Handa 已提交
888
 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
889 890 891 892 893 894 895
 *
 * @operation: Type of operation.
 * @path1:      Pointer to "struct path".
 * @path2:      Pointer to "struct path".
 *
 * Returns 0 on success, negative value otherwise.
 */
896
int tomoyo_path2_perm(const u8 operation, struct path *path1,
T
Tetsuo Handa 已提交
897
		      struct path *path2)
898 899
{
	int error = -ENOMEM;
T
Tetsuo Handa 已提交
900 901
	struct tomoyo_path_info buf1;
	struct tomoyo_path_info buf2;
902
	struct tomoyo_request_info r;
903
	int idx;
904

T
Tetsuo Handa 已提交
905 906 907
	if (!path1->mnt || !path2->mnt ||
	    tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
	    == TOMOYO_CONFIG_DISABLED)
908
		return 0;
T
Tetsuo Handa 已提交
909 910
	buf1.name = NULL;
	buf2.name = NULL;
911
	idx = tomoyo_read_lock();
T
Tetsuo Handa 已提交
912 913
	if (!tomoyo_get_realpath(&buf1, path1) ||
	    !tomoyo_get_realpath(&buf2, path2))
914
		goto out;
T
Tetsuo Handa 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927
	switch (operation) {
		struct dentry *dentry;
	case TOMOYO_TYPE_RENAME:
        case TOMOYO_TYPE_LINK:
		dentry = path1->dentry;
	        if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
                        break;
                /* fall through */
        case TOMOYO_TYPE_PIVOT_ROOT:
                tomoyo_add_slash(&buf1);
                tomoyo_add_slash(&buf2);
		break;
        }
928 929 930 931
	r.param_type = TOMOYO_TYPE_PATH2_ACL;
	r.param.path2.operation = operation;
	r.param.path2.filename1 = &buf1;
	r.param.path2.filename2 = &buf2;
932
	do {
933 934 935
		tomoyo_check_acl(&r, tomoyo_check_path2_acl);
		error = tomoyo_audit_path2_log(&r);
	} while (error == TOMOYO_RETRY_REQUEST);
936
 out:
T
Tetsuo Handa 已提交
937 938
	kfree(buf1.name);
	kfree(buf2.name);
939
	tomoyo_read_unlock(idx);
940
	if (r.mode != TOMOYO_CONFIG_ENFORCING)
941 942 943
		error = 0;
	return error;
}
944

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
/**
 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
 *
 * @a: Pointer to "struct tomoyo_acl_info".
 * @b: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if @a == @b, false otherwise.
 */
static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
				  const struct tomoyo_acl_info *b)
{
	const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
	const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
	return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
		tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
		tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
		tomoyo_same_number_union(&p1->flags, &p2->flags);
}

/**
 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
 *
 * @param: Pointer to "struct tomoyo_acl_param".
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
{
	struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
	int error;
	if (!tomoyo_parse_name_union(param, &e.dev_name) ||
	    !tomoyo_parse_name_union(param, &e.dir_name) ||
	    !tomoyo_parse_name_union(param, &e.fs_type) ||
	    !tomoyo_parse_number_union(param, &e.flags))
		error = -EINVAL;
	else
		error = tomoyo_update_domain(&e.head, sizeof(e), param,
					     tomoyo_same_mount_acl, NULL);
	tomoyo_put_name_union(&e.dev_name);
	tomoyo_put_name_union(&e.dir_name);
	tomoyo_put_name_union(&e.fs_type);
	tomoyo_put_number_union(&e.flags);
	return error;
}

992
/**
T
Tetsuo Handa 已提交
993
 * tomoyo_write_file - Update file related list.
994
 *
995
 * @param: Pointer to "struct tomoyo_acl_param".
996 997 998 999 1000
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
1001
int tomoyo_write_file(struct tomoyo_acl_param *param)
1002
{
1003
	u16 perm = 0;
1004
	u8 type;
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	const char *operation = tomoyo_read_token(param);
	for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
		if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
			perm |= 1 << type;
	if (perm)
		return tomoyo_update_path_acl(perm, param);
	for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
		if (tomoyo_permstr(operation, tomoyo_path2_keyword[type]))
			perm |= 1 << type;
	if (perm)
		return tomoyo_update_path2_acl(perm, param);
	for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
		if (tomoyo_permstr(operation,
				   tomoyo_path_number_keyword[type]))
			perm |= 1 << type;
	if (perm)
		return tomoyo_update_path_number_acl(perm, param);
	for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
		if (tomoyo_permstr(operation, tomoyo_mkdev_keyword[type]))
			perm |= 1 << type;
	if (perm)
		return tomoyo_update_mkdev_acl(perm, param);
	if (tomoyo_permstr(operation, "mount"))
		return tomoyo_update_mount_acl(param);
1029 1030
	return -EINVAL;
}