2 * security/tomoyo/file.c
4 * Pathname restriction functions.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
10 #include <linux/slab.h>
12 /* Keyword array for operations with one pathname. */
13 const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
14 [TOMOYO_TYPE_EXECUTE] = "execute",
15 [TOMOYO_TYPE_READ] = "read",
16 [TOMOYO_TYPE_WRITE] = "write",
17 [TOMOYO_TYPE_APPEND] = "append",
18 [TOMOYO_TYPE_UNLINK] = "unlink",
19 [TOMOYO_TYPE_GETATTR] = "getattr",
20 [TOMOYO_TYPE_RMDIR] = "rmdir",
21 [TOMOYO_TYPE_TRUNCATE] = "truncate",
22 [TOMOYO_TYPE_SYMLINK] = "symlink",
23 [TOMOYO_TYPE_CHROOT] = "chroot",
24 [TOMOYO_TYPE_UMOUNT] = "unmount",
27 /* Keyword array for operations with one pathname and three numbers. */
28 const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
29 [TOMOYO_TYPE_MKBLOCK] = "mkblock",
30 [TOMOYO_TYPE_MKCHAR] = "mkchar",
33 /* Keyword array for operations with two pathnames. */
34 const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
35 [TOMOYO_TYPE_LINK] = "link",
36 [TOMOYO_TYPE_RENAME] = "rename",
37 [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
40 /* Keyword array for operations with one pathname and one number. */
41 const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
42 [TOMOYO_TYPE_CREATE] = "create",
43 [TOMOYO_TYPE_MKDIR] = "mkdir",
44 [TOMOYO_TYPE_MKFIFO] = "mkfifo",
45 [TOMOYO_TYPE_MKSOCK] = "mksock",
46 [TOMOYO_TYPE_IOCTL] = "ioctl",
47 [TOMOYO_TYPE_CHMOD] = "chmod",
48 [TOMOYO_TYPE_CHOWN] = "chown",
49 [TOMOYO_TYPE_CHGRP] = "chgrp",
53 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
55 static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
56 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
57 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
58 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
59 [TOMOYO_TYPE_APPEND] = TOMOYO_MAC_FILE_OPEN,
60 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
61 [TOMOYO_TYPE_GETATTR] = TOMOYO_MAC_FILE_GETATTR,
62 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
63 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
64 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
65 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
66 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
70 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
72 const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
73 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
74 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
78 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
80 const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
81 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
82 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
83 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
87 * Mapping table from "enum tomoyo_path_number_acl_index" to
88 * "enum tomoyo_mac_index".
90 const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
91 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
92 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
93 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
94 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
95 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
96 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
97 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
98 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
102 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
104 * @ptr: Pointer to "struct tomoyo_name_union".
108 void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
110 tomoyo_put_group(ptr->group);
111 tomoyo_put_name(ptr->filename);
115 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
117 * @name: Pointer to "struct tomoyo_path_info".
118 * @ptr: Pointer to "struct tomoyo_name_union".
120 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
122 const struct tomoyo_path_info *
123 tomoyo_compare_name_union(const struct tomoyo_path_info *name,
124 const struct tomoyo_name_union *ptr)
127 return tomoyo_path_matches_group(name, ptr->group);
128 if (tomoyo_path_matches_pattern(name, ptr->filename))
129 return ptr->filename;
134 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
136 * @ptr: Pointer to "struct tomoyo_number_union".
140 void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
142 tomoyo_put_group(ptr->group);
146 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
148 * @value: Number to check.
149 * @ptr: Pointer to "struct tomoyo_number_union".
151 * Returns true if @value matches @ptr, false otherwise.
153 bool tomoyo_compare_number_union(const unsigned long value,
154 const struct tomoyo_number_union *ptr)
157 return tomoyo_number_matches_group(value, value, ptr->group);
158 return value >= ptr->values[0] && value <= ptr->values[1];
162 * tomoyo_add_slash - Add trailing '/' if needed.
164 * @buf: Pointer to "struct tomoyo_path_info".
168 * @buf must be generated by tomoyo_encode() because this function does not
169 * allocate memory for adding '/'.
171 static void tomoyo_add_slash(struct tomoyo_path_info *buf)
176 * This is OK because tomoyo_encode() reserves space for appending "/".
178 strcat((char *) buf->name, "/");
179 tomoyo_fill_path_info(buf);
183 * tomoyo_get_realpath - Get realpath.
185 * @buf: Pointer to "struct tomoyo_path_info".
186 * @path: Pointer to "struct path".
188 * Returns true on success, false otherwise.
190 static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
192 buf->name = tomoyo_realpath_from_path(path);
194 tomoyo_fill_path_info(buf);
201 * tomoyo_audit_path_log - Audit path request log.
203 * @r: Pointer to "struct tomoyo_request_info".
205 * Returns 0 on success, negative value otherwise.
207 static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
209 const char *operation = tomoyo_path_keyword[r->param.path.operation];
210 const struct tomoyo_path_info *filename = r->param.path.filename;
213 tomoyo_warn_log(r, "%s %s", operation, filename->name);
214 return tomoyo_supervisor(r, "file %s %s\n", operation, filename->name);
218 * tomoyo_audit_path2_log - Audit path/path request log.
220 * @r: Pointer to "struct tomoyo_request_info".
222 * Returns 0 on success, negative value otherwise.
224 static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
226 const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
227 const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
228 const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
231 tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
233 return tomoyo_supervisor(r, "file %s %s %s\n", operation,
234 filename1->name, filename2->name);
238 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
240 * @r: Pointer to "struct tomoyo_request_info".
242 * Returns 0 on success, negative value otherwise.
244 static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
246 const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
247 const struct tomoyo_path_info *filename = r->param.mkdev.filename;
248 const unsigned int major = r->param.mkdev.major;
249 const unsigned int minor = r->param.mkdev.minor;
250 const unsigned int mode = r->param.mkdev.mode;
253 tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
255 return tomoyo_supervisor(r, "file %s %s 0%o %u %u\n", operation,
256 filename->name, mode, major, minor);
260 * tomoyo_audit_path_number_log - Audit path/number request log.
262 * @r: Pointer to "struct tomoyo_request_info".
264 * Returns 0 on success, negative value otherwise.
266 static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
268 const u8 type = r->param.path_number.operation;
270 const struct tomoyo_path_info *filename = r->param.path_number.filename;
271 const char *operation = tomoyo_path_number_keyword[type];
276 case TOMOYO_TYPE_CREATE:
277 case TOMOYO_TYPE_MKDIR:
278 case TOMOYO_TYPE_MKFIFO:
279 case TOMOYO_TYPE_MKSOCK:
280 case TOMOYO_TYPE_CHMOD:
281 radix = TOMOYO_VALUE_TYPE_OCTAL;
283 case TOMOYO_TYPE_IOCTL:
284 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
287 radix = TOMOYO_VALUE_TYPE_DECIMAL;
290 tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
292 tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
293 return tomoyo_supervisor(r, "file %s %s %s\n", operation,
294 filename->name, buffer);
298 * tomoyo_check_path_acl - Check permission for path operation.
300 * @r: Pointer to "struct tomoyo_request_info".
301 * @ptr: Pointer to "struct tomoyo_acl_info".
303 * Returns true if granted, false otherwise.
305 * To be able to use wildcard for domain transition, this function sets
306 * matching entry on success. Since the caller holds tomoyo_read_lock(),
307 * it is safe to set matching entry.
309 static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
310 const struct tomoyo_acl_info *ptr)
312 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
314 if (acl->perm & (1 << r->param.path.operation)) {
315 r->param.path.matched_path =
316 tomoyo_compare_name_union(r->param.path.filename,
318 return r->param.path.matched_path != NULL;
324 * tomoyo_check_path_number_acl - Check permission for path number operation.
326 * @r: Pointer to "struct tomoyo_request_info".
327 * @ptr: Pointer to "struct tomoyo_acl_info".
329 * Returns true if granted, false otherwise.
331 static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
332 const struct tomoyo_acl_info *ptr)
334 const struct tomoyo_path_number_acl *acl =
335 container_of(ptr, typeof(*acl), head);
336 return (acl->perm & (1 << r->param.path_number.operation)) &&
337 tomoyo_compare_number_union(r->param.path_number.number,
339 tomoyo_compare_name_union(r->param.path_number.filename,
344 * tomoyo_check_path2_acl - Check permission for path path operation.
346 * @r: Pointer to "struct tomoyo_request_info".
347 * @ptr: Pointer to "struct tomoyo_acl_info".
349 * Returns true if granted, false otherwise.
351 static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
352 const struct tomoyo_acl_info *ptr)
354 const struct tomoyo_path2_acl *acl =
355 container_of(ptr, typeof(*acl), head);
356 return (acl->perm & (1 << r->param.path2.operation)) &&
357 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
358 && tomoyo_compare_name_union(r->param.path2.filename2,
363 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
365 * @r: Pointer to "struct tomoyo_request_info".
366 * @ptr: Pointer to "struct tomoyo_acl_info".
368 * Returns true if granted, false otherwise.
370 static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
371 const struct tomoyo_acl_info *ptr)
373 const struct tomoyo_mkdev_acl *acl =
374 container_of(ptr, typeof(*acl), head);
375 return (acl->perm & (1 << r->param.mkdev.operation)) &&
376 tomoyo_compare_number_union(r->param.mkdev.mode,
378 tomoyo_compare_number_union(r->param.mkdev.major,
380 tomoyo_compare_number_union(r->param.mkdev.minor,
382 tomoyo_compare_name_union(r->param.mkdev.filename,
387 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
389 * @a: Pointer to "struct tomoyo_acl_info".
390 * @b: Pointer to "struct tomoyo_acl_info".
392 * Returns true if @a == @b except permission bits, false otherwise.
394 static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
395 const struct tomoyo_acl_info *b)
397 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
398 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
399 return tomoyo_same_name_union(&p1->name, &p2->name);
403 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
405 * @a: Pointer to "struct tomoyo_acl_info".
406 * @b: Pointer to "struct tomoyo_acl_info".
407 * @is_delete: True for @a &= ~@b, false for @a |= @b.
409 * Returns true if @a is empty, false otherwise.
411 static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
412 struct tomoyo_acl_info *b,
413 const bool is_delete)
415 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
418 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
428 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
431 * @param: Pointer to "struct tomoyo_acl_param".
433 * Returns 0 on success, negative value otherwise.
435 * Caller holds tomoyo_read_lock().
437 static int tomoyo_update_path_acl(const u16 perm,
438 struct tomoyo_acl_param *param)
440 struct tomoyo_path_acl e = {
441 .head.type = TOMOYO_TYPE_PATH_ACL,
445 if (!tomoyo_parse_name_union(param, &e.name))
448 error = tomoyo_update_domain(&e.head, sizeof(e), param,
449 tomoyo_same_path_acl,
450 tomoyo_merge_path_acl);
451 tomoyo_put_name_union(&e.name);
456 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
458 * @a: Pointer to "struct tomoyo_acl_info".
459 * @b: Pointer to "struct tomoyo_acl_info".
461 * Returns true if @a == @b except permission bits, false otherwise.
463 static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
464 const struct tomoyo_acl_info *b)
466 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
467 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
468 return tomoyo_same_name_union(&p1->name, &p2->name) &&
469 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
470 tomoyo_same_number_union(&p1->major, &p2->major) &&
471 tomoyo_same_number_union(&p1->minor, &p2->minor);
475 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
477 * @a: Pointer to "struct tomoyo_acl_info".
478 * @b: Pointer to "struct tomoyo_acl_info".
479 * @is_delete: True for @a &= ~@b, false for @a |= @b.
481 * Returns true if @a is empty, false otherwise.
483 static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
484 struct tomoyo_acl_info *b,
485 const bool is_delete)
487 u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
490 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
501 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
504 * @param: Pointer to "struct tomoyo_acl_param".
506 * Returns 0 on success, negative value otherwise.
508 * Caller holds tomoyo_read_lock().
510 static int tomoyo_update_mkdev_acl(const u8 perm,
511 struct tomoyo_acl_param *param)
513 struct tomoyo_mkdev_acl e = {
514 .head.type = TOMOYO_TYPE_MKDEV_ACL,
518 if (!tomoyo_parse_name_union(param, &e.name) ||
519 !tomoyo_parse_number_union(param, &e.mode) ||
520 !tomoyo_parse_number_union(param, &e.major) ||
521 !tomoyo_parse_number_union(param, &e.minor))
524 error = tomoyo_update_domain(&e.head, sizeof(e), param,
525 tomoyo_same_mkdev_acl,
526 tomoyo_merge_mkdev_acl);
527 tomoyo_put_name_union(&e.name);
528 tomoyo_put_number_union(&e.mode);
529 tomoyo_put_number_union(&e.major);
530 tomoyo_put_number_union(&e.minor);
535 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
537 * @a: Pointer to "struct tomoyo_acl_info".
538 * @b: Pointer to "struct tomoyo_acl_info".
540 * Returns true if @a == @b except permission bits, false otherwise.
542 static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
543 const struct tomoyo_acl_info *b)
545 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
546 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
547 return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
548 tomoyo_same_name_union(&p1->name2, &p2->name2);
552 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
554 * @a: Pointer to "struct tomoyo_acl_info".
555 * @b: Pointer to "struct tomoyo_acl_info".
556 * @is_delete: True for @a &= ~@b, false for @a |= @b.
558 * Returns true if @a is empty, false otherwise.
560 static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
561 struct tomoyo_acl_info *b,
562 const bool is_delete)
564 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
567 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
577 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
580 * @param: Pointer to "struct tomoyo_acl_param".
582 * Returns 0 on success, negative value otherwise.
584 * Caller holds tomoyo_read_lock().
586 static int tomoyo_update_path2_acl(const u8 perm,
587 struct tomoyo_acl_param *param)
589 struct tomoyo_path2_acl e = {
590 .head.type = TOMOYO_TYPE_PATH2_ACL,
594 if (!tomoyo_parse_name_union(param, &e.name1) ||
595 !tomoyo_parse_name_union(param, &e.name2))
598 error = tomoyo_update_domain(&e.head, sizeof(e), param,
599 tomoyo_same_path2_acl,
600 tomoyo_merge_path2_acl);
601 tomoyo_put_name_union(&e.name1);
602 tomoyo_put_name_union(&e.name2);
607 * tomoyo_path_permission - Check permission for single path operation.
609 * @r: Pointer to "struct tomoyo_request_info".
610 * @operation: Type of operation.
611 * @filename: Filename to check.
613 * Returns 0 on success, negative value otherwise.
615 * Caller holds tomoyo_read_lock().
617 int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
618 const struct tomoyo_path_info *filename)
622 r->type = tomoyo_p2mac[operation];
623 r->mode = tomoyo_get_mode(r->profile, r->type);
624 if (r->mode == TOMOYO_CONFIG_DISABLED)
626 r->param_type = TOMOYO_TYPE_PATH_ACL;
627 r->param.path.filename = filename;
628 r->param.path.operation = operation;
630 tomoyo_check_acl(r, tomoyo_check_path_acl);
631 error = tomoyo_audit_path_log(r);
633 * Do not retry for execute request, for alias may have
636 } while (error == TOMOYO_RETRY_REQUEST &&
637 operation != TOMOYO_TYPE_EXECUTE);
642 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
644 * @a: Pointer to "struct tomoyo_acl_info".
645 * @b: Pointer to "struct tomoyo_acl_info".
647 * Returns true if @a == @b except permission bits, false otherwise.
649 static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
650 const struct tomoyo_acl_info *b)
652 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
654 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
656 return tomoyo_same_name_union(&p1->name, &p2->name) &&
657 tomoyo_same_number_union(&p1->number, &p2->number);
661 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
663 * @a: Pointer to "struct tomoyo_acl_info".
664 * @b: Pointer to "struct tomoyo_acl_info".
665 * @is_delete: True for @a &= ~@b, false for @a |= @b.
667 * Returns true if @a is empty, false otherwise.
669 static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
670 struct tomoyo_acl_info *b,
671 const bool is_delete)
673 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
676 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
687 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
690 * @param: Pointer to "struct tomoyo_acl_param".
692 * Returns 0 on success, negative value otherwise.
694 static int tomoyo_update_path_number_acl(const u8 perm,
695 struct tomoyo_acl_param *param)
697 struct tomoyo_path_number_acl e = {
698 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
702 if (!tomoyo_parse_name_union(param, &e.name) ||
703 !tomoyo_parse_number_union(param, &e.number))
706 error = tomoyo_update_domain(&e.head, sizeof(e), param,
707 tomoyo_same_path_number_acl,
708 tomoyo_merge_path_number_acl);
709 tomoyo_put_name_union(&e.name);
710 tomoyo_put_number_union(&e.number);
715 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
717 * @type: Type of operation.
718 * @path: Pointer to "struct path".
721 * Returns 0 on success, negative value otherwise.
723 int tomoyo_path_number_perm(const u8 type, struct path *path,
724 unsigned long number)
726 struct tomoyo_request_info r;
728 struct tomoyo_path_info buf;
731 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
732 == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
734 idx = tomoyo_read_lock();
735 if (!tomoyo_get_realpath(&buf, path))
737 if (type == TOMOYO_TYPE_MKDIR)
738 tomoyo_add_slash(&buf);
739 r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
740 r.param.path_number.operation = type;
741 r.param.path_number.filename = &buf;
742 r.param.path_number.number = number;
744 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
745 error = tomoyo_audit_path_number_log(&r);
746 } while (error == TOMOYO_RETRY_REQUEST);
749 tomoyo_read_unlock(idx);
750 if (r.mode != TOMOYO_CONFIG_ENFORCING)
756 * tomoyo_check_open_permission - Check permission for "read" and "write".
758 * @domain: Pointer to "struct tomoyo_domain_info".
759 * @path: Pointer to "struct path".
760 * @flag: Flags for open().
762 * Returns 0 on success, negative value otherwise.
764 int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
765 struct path *path, const int flag)
767 const u8 acc_mode = ACC_MODE(flag);
769 struct tomoyo_path_info buf;
770 struct tomoyo_request_info r;
776 r.mode = TOMOYO_CONFIG_DISABLED;
777 idx = tomoyo_read_lock();
779 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
780 != TOMOYO_CONFIG_DISABLED) {
781 if (!tomoyo_get_realpath(&buf, path)) {
785 if (acc_mode & MAY_READ)
786 error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
788 if (!error && (acc_mode & MAY_WRITE))
789 error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
796 tomoyo_read_unlock(idx);
797 if (r.mode != TOMOYO_CONFIG_ENFORCING)
803 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
805 * @operation: Type of operation.
806 * @path: Pointer to "struct path".
808 * Returns 0 on success, negative value otherwise.
810 int tomoyo_path_perm(const u8 operation, struct path *path)
812 struct tomoyo_request_info r;
814 struct tomoyo_path_info buf;
820 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
821 == TOMOYO_CONFIG_DISABLED)
823 is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
826 idx = tomoyo_read_lock();
827 if (!tomoyo_get_realpath(&buf, path))
830 case TOMOYO_TYPE_RMDIR:
831 case TOMOYO_TYPE_CHROOT:
832 tomoyo_add_slash(&buf);
835 error = tomoyo_path_permission(&r, operation, &buf);
838 tomoyo_read_unlock(idx);
845 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
847 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
848 * @path: Pointer to "struct path".
849 * @mode: Create mode.
850 * @dev: Device number.
852 * Returns 0 on success, negative value otherwise.
854 int tomoyo_mkdev_perm(const u8 operation, struct path *path,
855 const unsigned int mode, unsigned int dev)
857 struct tomoyo_request_info r;
859 struct tomoyo_path_info buf;
863 tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
864 == TOMOYO_CONFIG_DISABLED)
866 idx = tomoyo_read_lock();
868 if (tomoyo_get_realpath(&buf, path)) {
869 dev = new_decode_dev(dev);
870 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
871 r.param.mkdev.filename = &buf;
872 r.param.mkdev.operation = operation;
873 r.param.mkdev.mode = mode;
874 r.param.mkdev.major = MAJOR(dev);
875 r.param.mkdev.minor = MINOR(dev);
876 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
877 error = tomoyo_audit_mkdev_log(&r);
880 tomoyo_read_unlock(idx);
881 if (r.mode != TOMOYO_CONFIG_ENFORCING)
887 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
889 * @operation: Type of operation.
890 * @path1: Pointer to "struct path".
891 * @path2: Pointer to "struct path".
893 * Returns 0 on success, negative value otherwise.
895 int tomoyo_path2_perm(const u8 operation, struct path *path1,
899 struct tomoyo_path_info buf1;
900 struct tomoyo_path_info buf2;
901 struct tomoyo_request_info r;
904 if (!path1->mnt || !path2->mnt ||
905 tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
906 == TOMOYO_CONFIG_DISABLED)
910 idx = tomoyo_read_lock();
911 if (!tomoyo_get_realpath(&buf1, path1) ||
912 !tomoyo_get_realpath(&buf2, path2))
915 struct dentry *dentry;
916 case TOMOYO_TYPE_RENAME:
917 case TOMOYO_TYPE_LINK:
918 dentry = path1->dentry;
919 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
922 case TOMOYO_TYPE_PIVOT_ROOT:
923 tomoyo_add_slash(&buf1);
924 tomoyo_add_slash(&buf2);
927 r.param_type = TOMOYO_TYPE_PATH2_ACL;
928 r.param.path2.operation = operation;
929 r.param.path2.filename1 = &buf1;
930 r.param.path2.filename2 = &buf2;
932 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
933 error = tomoyo_audit_path2_log(&r);
934 } while (error == TOMOYO_RETRY_REQUEST);
938 tomoyo_read_unlock(idx);
939 if (r.mode != TOMOYO_CONFIG_ENFORCING)
945 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
947 * @a: Pointer to "struct tomoyo_acl_info".
948 * @b: Pointer to "struct tomoyo_acl_info".
950 * Returns true if @a == @b, false otherwise.
952 static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
953 const struct tomoyo_acl_info *b)
955 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
956 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
957 return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
958 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
959 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
960 tomoyo_same_number_union(&p1->flags, &p2->flags);
964 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
966 * @param: Pointer to "struct tomoyo_acl_param".
968 * Returns 0 on success, negative value otherwise.
970 * Caller holds tomoyo_read_lock().
972 static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
974 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
976 if (!tomoyo_parse_name_union(param, &e.dev_name) ||
977 !tomoyo_parse_name_union(param, &e.dir_name) ||
978 !tomoyo_parse_name_union(param, &e.fs_type) ||
979 !tomoyo_parse_number_union(param, &e.flags))
982 error = tomoyo_update_domain(&e.head, sizeof(e), param,
983 tomoyo_same_mount_acl, NULL);
984 tomoyo_put_name_union(&e.dev_name);
985 tomoyo_put_name_union(&e.dir_name);
986 tomoyo_put_name_union(&e.fs_type);
987 tomoyo_put_number_union(&e.flags);
992 * tomoyo_write_file - Update file related list.
994 * @param: Pointer to "struct tomoyo_acl_param".
996 * Returns 0 on success, negative value otherwise.
998 * Caller holds tomoyo_read_lock().
1000 int tomoyo_write_file(struct tomoyo_acl_param *param)
1004 const char *operation = tomoyo_read_token(param);
1005 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
1006 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
1009 return tomoyo_update_path_acl(perm, param);
1010 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
1011 if (tomoyo_permstr(operation, tomoyo_path2_keyword[type]))
1014 return tomoyo_update_path2_acl(perm, param);
1015 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
1016 if (tomoyo_permstr(operation,
1017 tomoyo_path_number_keyword[type]))
1020 return tomoyo_update_path_number_acl(perm, param);
1021 for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
1022 if (tomoyo_permstr(operation, tomoyo_mkdev_keyword[type]))
1025 return tomoyo_update_mkdev_acl(perm, param);
1026 if (tomoyo_permstr(operation, "mount"))
1027 return tomoyo_update_mount_acl(param);