1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/file.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
9 #include <linux/slab.h>
12 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
14 static const u8 tomoyo_p2mac
[TOMOYO_MAX_PATH_OPERATION
] = {
15 [TOMOYO_TYPE_EXECUTE
] = TOMOYO_MAC_FILE_EXECUTE
,
16 [TOMOYO_TYPE_READ
] = TOMOYO_MAC_FILE_OPEN
,
17 [TOMOYO_TYPE_WRITE
] = TOMOYO_MAC_FILE_OPEN
,
18 [TOMOYO_TYPE_APPEND
] = TOMOYO_MAC_FILE_OPEN
,
19 [TOMOYO_TYPE_UNLINK
] = TOMOYO_MAC_FILE_UNLINK
,
20 [TOMOYO_TYPE_GETATTR
] = TOMOYO_MAC_FILE_GETATTR
,
21 [TOMOYO_TYPE_RMDIR
] = TOMOYO_MAC_FILE_RMDIR
,
22 [TOMOYO_TYPE_TRUNCATE
] = TOMOYO_MAC_FILE_TRUNCATE
,
23 [TOMOYO_TYPE_SYMLINK
] = TOMOYO_MAC_FILE_SYMLINK
,
24 [TOMOYO_TYPE_CHROOT
] = TOMOYO_MAC_FILE_CHROOT
,
25 [TOMOYO_TYPE_UMOUNT
] = TOMOYO_MAC_FILE_UMOUNT
,
29 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
31 const u8 tomoyo_pnnn2mac
[TOMOYO_MAX_MKDEV_OPERATION
] = {
32 [TOMOYO_TYPE_MKBLOCK
] = TOMOYO_MAC_FILE_MKBLOCK
,
33 [TOMOYO_TYPE_MKCHAR
] = TOMOYO_MAC_FILE_MKCHAR
,
37 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
39 const u8 tomoyo_pp2mac
[TOMOYO_MAX_PATH2_OPERATION
] = {
40 [TOMOYO_TYPE_LINK
] = TOMOYO_MAC_FILE_LINK
,
41 [TOMOYO_TYPE_RENAME
] = TOMOYO_MAC_FILE_RENAME
,
42 [TOMOYO_TYPE_PIVOT_ROOT
] = TOMOYO_MAC_FILE_PIVOT_ROOT
,
46 * Mapping table from "enum tomoyo_path_number_acl_index" to
47 * "enum tomoyo_mac_index".
49 const u8 tomoyo_pn2mac
[TOMOYO_MAX_PATH_NUMBER_OPERATION
] = {
50 [TOMOYO_TYPE_CREATE
] = TOMOYO_MAC_FILE_CREATE
,
51 [TOMOYO_TYPE_MKDIR
] = TOMOYO_MAC_FILE_MKDIR
,
52 [TOMOYO_TYPE_MKFIFO
] = TOMOYO_MAC_FILE_MKFIFO
,
53 [TOMOYO_TYPE_MKSOCK
] = TOMOYO_MAC_FILE_MKSOCK
,
54 [TOMOYO_TYPE_IOCTL
] = TOMOYO_MAC_FILE_IOCTL
,
55 [TOMOYO_TYPE_CHMOD
] = TOMOYO_MAC_FILE_CHMOD
,
56 [TOMOYO_TYPE_CHOWN
] = TOMOYO_MAC_FILE_CHOWN
,
57 [TOMOYO_TYPE_CHGRP
] = TOMOYO_MAC_FILE_CHGRP
,
61 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
63 * @ptr: Pointer to "struct tomoyo_name_union".
67 void tomoyo_put_name_union(struct tomoyo_name_union
*ptr
)
69 tomoyo_put_group(ptr
->group
);
70 tomoyo_put_name(ptr
->filename
);
74 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
76 * @name: Pointer to "struct tomoyo_path_info".
77 * @ptr: Pointer to "struct tomoyo_name_union".
79 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
81 const struct tomoyo_path_info
*
82 tomoyo_compare_name_union(const struct tomoyo_path_info
*name
,
83 const struct tomoyo_name_union
*ptr
)
86 return tomoyo_path_matches_group(name
, ptr
->group
);
87 if (tomoyo_path_matches_pattern(name
, ptr
->filename
))
93 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
95 * @ptr: Pointer to "struct tomoyo_number_union".
99 void tomoyo_put_number_union(struct tomoyo_number_union
*ptr
)
101 tomoyo_put_group(ptr
->group
);
105 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
107 * @value: Number to check.
108 * @ptr: Pointer to "struct tomoyo_number_union".
110 * Returns true if @value matches @ptr, false otherwise.
112 bool tomoyo_compare_number_union(const unsigned long value
,
113 const struct tomoyo_number_union
*ptr
)
116 return tomoyo_number_matches_group(value
, value
, ptr
->group
);
117 return value
>= ptr
->values
[0] && value
<= ptr
->values
[1];
121 * tomoyo_add_slash - Add trailing '/' if needed.
123 * @buf: Pointer to "struct tomoyo_path_info".
127 * @buf must be generated by tomoyo_encode() because this function does not
128 * allocate memory for adding '/'.
130 static void tomoyo_add_slash(struct tomoyo_path_info
*buf
)
135 * This is OK because tomoyo_encode() reserves space for appending "/".
137 strcat((char *) buf
->name
, "/");
138 tomoyo_fill_path_info(buf
);
142 * tomoyo_get_realpath - Get realpath.
144 * @buf: Pointer to "struct tomoyo_path_info".
145 * @path: Pointer to "struct path".
147 * Returns true on success, false otherwise.
149 static bool tomoyo_get_realpath(struct tomoyo_path_info
*buf
, const struct path
*path
)
151 buf
->name
= tomoyo_realpath_from_path(path
);
153 tomoyo_fill_path_info(buf
);
160 * tomoyo_audit_path_log - Audit path request log.
162 * @r: Pointer to "struct tomoyo_request_info".
164 * Returns 0 on success, negative value otherwise.
166 static int tomoyo_audit_path_log(struct tomoyo_request_info
*r
)
168 return tomoyo_supervisor(r
, "file %s %s\n", tomoyo_path_keyword
169 [r
->param
.path
.operation
],
170 r
->param
.path
.filename
->name
);
174 * tomoyo_audit_path2_log - Audit path/path request log.
176 * @r: Pointer to "struct tomoyo_request_info".
178 * Returns 0 on success, negative value otherwise.
180 static int tomoyo_audit_path2_log(struct tomoyo_request_info
*r
)
182 return tomoyo_supervisor(r
, "file %s %s %s\n", tomoyo_mac_keywords
183 [tomoyo_pp2mac
[r
->param
.path2
.operation
]],
184 r
->param
.path2
.filename1
->name
,
185 r
->param
.path2
.filename2
->name
);
189 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
191 * @r: Pointer to "struct tomoyo_request_info".
193 * Returns 0 on success, negative value otherwise.
195 static int tomoyo_audit_mkdev_log(struct tomoyo_request_info
*r
)
197 return tomoyo_supervisor(r
, "file %s %s 0%o %u %u\n",
199 [tomoyo_pnnn2mac
[r
->param
.mkdev
.operation
]],
200 r
->param
.mkdev
.filename
->name
,
201 r
->param
.mkdev
.mode
, r
->param
.mkdev
.major
,
202 r
->param
.mkdev
.minor
);
206 * tomoyo_audit_path_number_log - Audit path/number request log.
208 * @r: Pointer to "struct tomoyo_request_info".
210 * Returns 0 on success, negative value otherwise.
212 static int tomoyo_audit_path_number_log(struct tomoyo_request_info
*r
)
214 const u8 type
= r
->param
.path_number
.operation
;
219 case TOMOYO_TYPE_CREATE
:
220 case TOMOYO_TYPE_MKDIR
:
221 case TOMOYO_TYPE_MKFIFO
:
222 case TOMOYO_TYPE_MKSOCK
:
223 case TOMOYO_TYPE_CHMOD
:
224 radix
= TOMOYO_VALUE_TYPE_OCTAL
;
226 case TOMOYO_TYPE_IOCTL
:
227 radix
= TOMOYO_VALUE_TYPE_HEXADECIMAL
;
230 radix
= TOMOYO_VALUE_TYPE_DECIMAL
;
233 tomoyo_print_ulong(buffer
, sizeof(buffer
), r
->param
.path_number
.number
,
235 return tomoyo_supervisor(r
, "file %s %s %s\n", tomoyo_mac_keywords
236 [tomoyo_pn2mac
[type
]],
237 r
->param
.path_number
.filename
->name
, buffer
);
241 * tomoyo_check_path_acl - Check permission for path operation.
243 * @r: Pointer to "struct tomoyo_request_info".
244 * @ptr: Pointer to "struct tomoyo_acl_info".
246 * Returns true if granted, false otherwise.
248 * To be able to use wildcard for domain transition, this function sets
249 * matching entry on success. Since the caller holds tomoyo_read_lock(),
250 * it is safe to set matching entry.
252 static bool tomoyo_check_path_acl(struct tomoyo_request_info
*r
,
253 const struct tomoyo_acl_info
*ptr
)
255 const struct tomoyo_path_acl
*acl
= container_of(ptr
, typeof(*acl
),
258 if (acl
->perm
& (1 << r
->param
.path
.operation
)) {
259 r
->param
.path
.matched_path
=
260 tomoyo_compare_name_union(r
->param
.path
.filename
,
262 return r
->param
.path
.matched_path
!= NULL
;
268 * tomoyo_check_path_number_acl - Check permission for path number operation.
270 * @r: Pointer to "struct tomoyo_request_info".
271 * @ptr: Pointer to "struct tomoyo_acl_info".
273 * Returns true if granted, false otherwise.
275 static bool tomoyo_check_path_number_acl(struct tomoyo_request_info
*r
,
276 const struct tomoyo_acl_info
*ptr
)
278 const struct tomoyo_path_number_acl
*acl
=
279 container_of(ptr
, typeof(*acl
), head
);
281 return (acl
->perm
& (1 << r
->param
.path_number
.operation
)) &&
282 tomoyo_compare_number_union(r
->param
.path_number
.number
,
284 tomoyo_compare_name_union(r
->param
.path_number
.filename
,
289 * tomoyo_check_path2_acl - Check permission for path path operation.
291 * @r: Pointer to "struct tomoyo_request_info".
292 * @ptr: Pointer to "struct tomoyo_acl_info".
294 * Returns true if granted, false otherwise.
296 static bool tomoyo_check_path2_acl(struct tomoyo_request_info
*r
,
297 const struct tomoyo_acl_info
*ptr
)
299 const struct tomoyo_path2_acl
*acl
=
300 container_of(ptr
, typeof(*acl
), head
);
302 return (acl
->perm
& (1 << r
->param
.path2
.operation
)) &&
303 tomoyo_compare_name_union(r
->param
.path2
.filename1
, &acl
->name1
)
304 && tomoyo_compare_name_union(r
->param
.path2
.filename2
,
309 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
311 * @r: Pointer to "struct tomoyo_request_info".
312 * @ptr: Pointer to "struct tomoyo_acl_info".
314 * Returns true if granted, false otherwise.
316 static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info
*r
,
317 const struct tomoyo_acl_info
*ptr
)
319 const struct tomoyo_mkdev_acl
*acl
=
320 container_of(ptr
, typeof(*acl
), head
);
322 return (acl
->perm
& (1 << r
->param
.mkdev
.operation
)) &&
323 tomoyo_compare_number_union(r
->param
.mkdev
.mode
,
325 tomoyo_compare_number_union(r
->param
.mkdev
.major
,
327 tomoyo_compare_number_union(r
->param
.mkdev
.minor
,
329 tomoyo_compare_name_union(r
->param
.mkdev
.filename
,
334 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
336 * @a: Pointer to "struct tomoyo_acl_info".
337 * @b: Pointer to "struct tomoyo_acl_info".
339 * Returns true if @a == @b except permission bits, false otherwise.
341 static bool tomoyo_same_path_acl(const struct tomoyo_acl_info
*a
,
342 const struct tomoyo_acl_info
*b
)
344 const struct tomoyo_path_acl
*p1
= container_of(a
, typeof(*p1
), head
);
345 const struct tomoyo_path_acl
*p2
= container_of(b
, typeof(*p2
), head
);
347 return tomoyo_same_name_union(&p1
->name
, &p2
->name
);
351 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
353 * @a: Pointer to "struct tomoyo_acl_info".
354 * @b: Pointer to "struct tomoyo_acl_info".
355 * @is_delete: True for @a &= ~@b, false for @a |= @b.
357 * Returns true if @a is empty, false otherwise.
359 static bool tomoyo_merge_path_acl(struct tomoyo_acl_info
*a
,
360 struct tomoyo_acl_info
*b
,
361 const bool is_delete
)
363 u16
* const a_perm
= &container_of(a
, struct tomoyo_path_acl
, head
)
366 const u16 b_perm
= container_of(b
, struct tomoyo_path_acl
, head
)->perm
;
377 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
380 * @param: Pointer to "struct tomoyo_acl_param".
382 * Returns 0 on success, negative value otherwise.
384 * Caller holds tomoyo_read_lock().
386 static int tomoyo_update_path_acl(const u16 perm
,
387 struct tomoyo_acl_param
*param
)
389 struct tomoyo_path_acl e
= {
390 .head
.type
= TOMOYO_TYPE_PATH_ACL
,
395 if (!tomoyo_parse_name_union(param
, &e
.name
))
398 error
= tomoyo_update_domain(&e
.head
, sizeof(e
), param
,
399 tomoyo_same_path_acl
,
400 tomoyo_merge_path_acl
);
401 tomoyo_put_name_union(&e
.name
);
406 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
408 * @a: Pointer to "struct tomoyo_acl_info".
409 * @b: Pointer to "struct tomoyo_acl_info".
411 * Returns true if @a == @b except permission bits, false otherwise.
413 static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info
*a
,
414 const struct tomoyo_acl_info
*b
)
416 const struct tomoyo_mkdev_acl
*p1
= container_of(a
, typeof(*p1
), head
);
417 const struct tomoyo_mkdev_acl
*p2
= container_of(b
, typeof(*p2
), head
);
419 return tomoyo_same_name_union(&p1
->name
, &p2
->name
) &&
420 tomoyo_same_number_union(&p1
->mode
, &p2
->mode
) &&
421 tomoyo_same_number_union(&p1
->major
, &p2
->major
) &&
422 tomoyo_same_number_union(&p1
->minor
, &p2
->minor
);
426 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
428 * @a: Pointer to "struct tomoyo_acl_info".
429 * @b: Pointer to "struct tomoyo_acl_info".
430 * @is_delete: True for @a &= ~@b, false for @a |= @b.
432 * Returns true if @a is empty, false otherwise.
434 static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info
*a
,
435 struct tomoyo_acl_info
*b
,
436 const bool is_delete
)
438 u8
*const a_perm
= &container_of(a
, struct tomoyo_mkdev_acl
,
441 const u8 b_perm
= container_of(b
, struct tomoyo_mkdev_acl
, head
)
453 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
456 * @param: Pointer to "struct tomoyo_acl_param".
458 * Returns 0 on success, negative value otherwise.
460 * Caller holds tomoyo_read_lock().
462 static int tomoyo_update_mkdev_acl(const u8 perm
,
463 struct tomoyo_acl_param
*param
)
465 struct tomoyo_mkdev_acl e
= {
466 .head
.type
= TOMOYO_TYPE_MKDEV_ACL
,
471 if (!tomoyo_parse_name_union(param
, &e
.name
) ||
472 !tomoyo_parse_number_union(param
, &e
.mode
) ||
473 !tomoyo_parse_number_union(param
, &e
.major
) ||
474 !tomoyo_parse_number_union(param
, &e
.minor
))
477 error
= tomoyo_update_domain(&e
.head
, sizeof(e
), param
,
478 tomoyo_same_mkdev_acl
,
479 tomoyo_merge_mkdev_acl
);
480 tomoyo_put_name_union(&e
.name
);
481 tomoyo_put_number_union(&e
.mode
);
482 tomoyo_put_number_union(&e
.major
);
483 tomoyo_put_number_union(&e
.minor
);
488 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
490 * @a: Pointer to "struct tomoyo_acl_info".
491 * @b: Pointer to "struct tomoyo_acl_info".
493 * Returns true if @a == @b except permission bits, false otherwise.
495 static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info
*a
,
496 const struct tomoyo_acl_info
*b
)
498 const struct tomoyo_path2_acl
*p1
= container_of(a
, typeof(*p1
), head
);
499 const struct tomoyo_path2_acl
*p2
= container_of(b
, typeof(*p2
), head
);
501 return tomoyo_same_name_union(&p1
->name1
, &p2
->name1
) &&
502 tomoyo_same_name_union(&p1
->name2
, &p2
->name2
);
506 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
508 * @a: Pointer to "struct tomoyo_acl_info".
509 * @b: Pointer to "struct tomoyo_acl_info".
510 * @is_delete: True for @a &= ~@b, false for @a |= @b.
512 * Returns true if @a is empty, false otherwise.
514 static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info
*a
,
515 struct tomoyo_acl_info
*b
,
516 const bool is_delete
)
518 u8
* const a_perm
= &container_of(a
, struct tomoyo_path2_acl
, head
)
521 const u8 b_perm
= container_of(b
, struct tomoyo_path2_acl
, head
)->perm
;
532 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
535 * @param: Pointer to "struct tomoyo_acl_param".
537 * Returns 0 on success, negative value otherwise.
539 * Caller holds tomoyo_read_lock().
541 static int tomoyo_update_path2_acl(const u8 perm
,
542 struct tomoyo_acl_param
*param
)
544 struct tomoyo_path2_acl e
= {
545 .head
.type
= TOMOYO_TYPE_PATH2_ACL
,
550 if (!tomoyo_parse_name_union(param
, &e
.name1
) ||
551 !tomoyo_parse_name_union(param
, &e
.name2
))
554 error
= tomoyo_update_domain(&e
.head
, sizeof(e
), param
,
555 tomoyo_same_path2_acl
,
556 tomoyo_merge_path2_acl
);
557 tomoyo_put_name_union(&e
.name1
);
558 tomoyo_put_name_union(&e
.name2
);
563 * tomoyo_path_permission - Check permission for single path operation.
565 * @r: Pointer to "struct tomoyo_request_info".
566 * @operation: Type of operation.
567 * @filename: Filename to check.
569 * Returns 0 on success, negative value otherwise.
571 * Caller holds tomoyo_read_lock().
573 static int tomoyo_path_permission(struct tomoyo_request_info
*r
, u8 operation
,
574 const struct tomoyo_path_info
*filename
)
578 r
->type
= tomoyo_p2mac
[operation
];
579 r
->mode
= tomoyo_get_mode(r
->domain
->ns
, r
->profile
, r
->type
);
580 if (r
->mode
== TOMOYO_CONFIG_DISABLED
)
582 r
->param_type
= TOMOYO_TYPE_PATH_ACL
;
583 r
->param
.path
.filename
= filename
;
584 r
->param
.path
.operation
= operation
;
586 tomoyo_check_acl(r
, tomoyo_check_path_acl
);
587 error
= tomoyo_audit_path_log(r
);
588 } while (error
== TOMOYO_RETRY_REQUEST
);
593 * tomoyo_execute_permission - Check permission for execute operation.
595 * @r: Pointer to "struct tomoyo_request_info".
596 * @filename: Filename to check.
598 * Returns 0 on success, negative value otherwise.
600 * Caller holds tomoyo_read_lock().
602 int tomoyo_execute_permission(struct tomoyo_request_info
*r
,
603 const struct tomoyo_path_info
*filename
)
606 * Unlike other permission checks, this check is done regardless of
607 * profile mode settings in order to check for domain transition
610 r
->type
= TOMOYO_MAC_FILE_EXECUTE
;
611 r
->mode
= tomoyo_get_mode(r
->domain
->ns
, r
->profile
, r
->type
);
612 r
->param_type
= TOMOYO_TYPE_PATH_ACL
;
613 r
->param
.path
.filename
= filename
;
614 r
->param
.path
.operation
= TOMOYO_TYPE_EXECUTE
;
615 tomoyo_check_acl(r
, tomoyo_check_path_acl
);
616 r
->ee
->transition
= r
->matched_acl
&& r
->matched_acl
->cond
?
617 r
->matched_acl
->cond
->transit
: NULL
;
618 if (r
->mode
!= TOMOYO_CONFIG_DISABLED
)
619 return tomoyo_audit_path_log(r
);
624 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
626 * @a: Pointer to "struct tomoyo_acl_info".
627 * @b: Pointer to "struct tomoyo_acl_info".
629 * Returns true if @a == @b except permission bits, false otherwise.
631 static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info
*a
,
632 const struct tomoyo_acl_info
*b
)
634 const struct tomoyo_path_number_acl
*p1
= container_of(a
, typeof(*p1
),
636 const struct tomoyo_path_number_acl
*p2
= container_of(b
, typeof(*p2
),
639 return tomoyo_same_name_union(&p1
->name
, &p2
->name
) &&
640 tomoyo_same_number_union(&p1
->number
, &p2
->number
);
644 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
646 * @a: Pointer to "struct tomoyo_acl_info".
647 * @b: Pointer to "struct tomoyo_acl_info".
648 * @is_delete: True for @a &= ~@b, false for @a |= @b.
650 * Returns true if @a is empty, false otherwise.
652 static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info
*a
,
653 struct tomoyo_acl_info
*b
,
654 const bool is_delete
)
656 u8
* const a_perm
= &container_of(a
, struct tomoyo_path_number_acl
,
659 const u8 b_perm
= container_of(b
, struct tomoyo_path_number_acl
, head
)
671 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
674 * @param: Pointer to "struct tomoyo_acl_param".
676 * Returns 0 on success, negative value otherwise.
678 static int tomoyo_update_path_number_acl(const u8 perm
,
679 struct tomoyo_acl_param
*param
)
681 struct tomoyo_path_number_acl e
= {
682 .head
.type
= TOMOYO_TYPE_PATH_NUMBER_ACL
,
687 if (!tomoyo_parse_name_union(param
, &e
.name
) ||
688 !tomoyo_parse_number_union(param
, &e
.number
))
691 error
= tomoyo_update_domain(&e
.head
, sizeof(e
), param
,
692 tomoyo_same_path_number_acl
,
693 tomoyo_merge_path_number_acl
);
694 tomoyo_put_name_union(&e
.name
);
695 tomoyo_put_number_union(&e
.number
);
700 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
702 * @type: Type of operation.
703 * @path: Pointer to "struct path".
706 * Returns 0 on success, negative value otherwise.
708 int tomoyo_path_number_perm(const u8 type
, const struct path
*path
,
709 unsigned long number
)
711 struct tomoyo_request_info r
;
712 struct tomoyo_obj_info obj
= {
713 .path1
= { .mnt
= path
->mnt
, .dentry
= path
->dentry
},
716 struct tomoyo_path_info buf
;
719 if (tomoyo_init_request_info(&r
, NULL
, tomoyo_pn2mac
[type
])
720 == TOMOYO_CONFIG_DISABLED
|| !path
->dentry
)
722 idx
= tomoyo_read_lock();
723 if (!tomoyo_get_realpath(&buf
, path
))
726 if (type
== TOMOYO_TYPE_MKDIR
)
727 tomoyo_add_slash(&buf
);
728 r
.param_type
= TOMOYO_TYPE_PATH_NUMBER_ACL
;
729 r
.param
.path_number
.operation
= type
;
730 r
.param
.path_number
.filename
= &buf
;
731 r
.param
.path_number
.number
= number
;
733 tomoyo_check_acl(&r
, tomoyo_check_path_number_acl
);
734 error
= tomoyo_audit_path_number_log(&r
);
735 } while (error
== TOMOYO_RETRY_REQUEST
);
738 tomoyo_read_unlock(idx
);
739 if (r
.mode
!= TOMOYO_CONFIG_ENFORCING
)
745 * tomoyo_check_open_permission - Check permission for "read" and "write".
747 * @domain: Pointer to "struct tomoyo_domain_info".
748 * @path: Pointer to "struct path".
749 * @flag: Flags for open().
751 * Returns 0 on success, negative value otherwise.
753 int tomoyo_check_open_permission(struct tomoyo_domain_info
*domain
,
754 const struct path
*path
, const int flag
)
756 const u8 acc_mode
= ACC_MODE(flag
);
758 struct tomoyo_path_info buf
;
759 struct tomoyo_request_info r
;
760 struct tomoyo_obj_info obj
= {
761 .path1
= { .mnt
= path
->mnt
, .dentry
= path
->dentry
},
766 r
.mode
= TOMOYO_CONFIG_DISABLED
;
767 idx
= tomoyo_read_lock();
769 tomoyo_init_request_info(&r
, domain
, TOMOYO_MAC_FILE_OPEN
)
770 != TOMOYO_CONFIG_DISABLED
) {
771 if (!tomoyo_get_realpath(&buf
, path
)) {
776 if (acc_mode
& MAY_READ
)
777 error
= tomoyo_path_permission(&r
, TOMOYO_TYPE_READ
,
779 if (!error
&& (acc_mode
& MAY_WRITE
))
780 error
= tomoyo_path_permission(&r
, (flag
& O_APPEND
) ?
787 tomoyo_read_unlock(idx
);
788 if (r
.mode
!= TOMOYO_CONFIG_ENFORCING
)
794 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
796 * @operation: Type of operation.
797 * @path: Pointer to "struct path".
798 * @target: Symlink's target if @operation is TOMOYO_TYPE_SYMLINK,
801 * Returns 0 on success, negative value otherwise.
803 int tomoyo_path_perm(const u8 operation
, const struct path
*path
, const char *target
)
805 struct tomoyo_request_info r
;
806 struct tomoyo_obj_info obj
= {
807 .path1
= { .mnt
= path
->mnt
, .dentry
= path
->dentry
},
810 struct tomoyo_path_info buf
;
812 struct tomoyo_path_info symlink_target
;
815 if (tomoyo_init_request_info(&r
, NULL
, tomoyo_p2mac
[operation
])
816 == TOMOYO_CONFIG_DISABLED
)
818 is_enforce
= (r
.mode
== TOMOYO_CONFIG_ENFORCING
);
821 idx
= tomoyo_read_lock();
822 if (!tomoyo_get_realpath(&buf
, path
))
826 case TOMOYO_TYPE_RMDIR
:
827 case TOMOYO_TYPE_CHROOT
:
828 tomoyo_add_slash(&buf
);
830 case TOMOYO_TYPE_SYMLINK
:
831 symlink_target
.name
= tomoyo_encode(target
);
832 if (!symlink_target
.name
)
834 tomoyo_fill_path_info(&symlink_target
);
835 obj
.symlink_target
= &symlink_target
;
838 error
= tomoyo_path_permission(&r
, operation
, &buf
);
839 if (operation
== TOMOYO_TYPE_SYMLINK
)
840 kfree(symlink_target
.name
);
843 tomoyo_read_unlock(idx
);
850 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
852 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
853 * @path: Pointer to "struct path".
854 * @mode: Create mode.
855 * @dev: Device number.
857 * Returns 0 on success, negative value otherwise.
859 int tomoyo_mkdev_perm(const u8 operation
, const struct path
*path
,
860 const unsigned int mode
, unsigned int dev
)
862 struct tomoyo_request_info r
;
863 struct tomoyo_obj_info obj
= {
864 .path1
= { .mnt
= path
->mnt
, .dentry
= path
->dentry
},
867 struct tomoyo_path_info buf
;
870 if (tomoyo_init_request_info(&r
, NULL
, tomoyo_pnnn2mac
[operation
])
871 == TOMOYO_CONFIG_DISABLED
)
873 idx
= tomoyo_read_lock();
875 if (tomoyo_get_realpath(&buf
, path
)) {
877 dev
= new_decode_dev(dev
);
878 r
.param_type
= TOMOYO_TYPE_MKDEV_ACL
;
879 r
.param
.mkdev
.filename
= &buf
;
880 r
.param
.mkdev
.operation
= operation
;
881 r
.param
.mkdev
.mode
= mode
;
882 r
.param
.mkdev
.major
= MAJOR(dev
);
883 r
.param
.mkdev
.minor
= MINOR(dev
);
884 tomoyo_check_acl(&r
, tomoyo_check_mkdev_acl
);
885 error
= tomoyo_audit_mkdev_log(&r
);
888 tomoyo_read_unlock(idx
);
889 if (r
.mode
!= TOMOYO_CONFIG_ENFORCING
)
895 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
897 * @operation: Type of operation.
898 * @path1: Pointer to "struct path".
899 * @path2: Pointer to "struct path".
901 * Returns 0 on success, negative value otherwise.
903 int tomoyo_path2_perm(const u8 operation
, const struct path
*path1
,
904 const struct path
*path2
)
907 struct tomoyo_path_info buf1
;
908 struct tomoyo_path_info buf2
;
909 struct tomoyo_request_info r
;
910 struct tomoyo_obj_info obj
= {
911 .path1
= { .mnt
= path1
->mnt
, .dentry
= path1
->dentry
},
912 .path2
= { .mnt
= path2
->mnt
, .dentry
= path2
->dentry
}
916 if (tomoyo_init_request_info(&r
, NULL
, tomoyo_pp2mac
[operation
])
917 == TOMOYO_CONFIG_DISABLED
)
921 idx
= tomoyo_read_lock();
922 if (!tomoyo_get_realpath(&buf1
, path1
) ||
923 !tomoyo_get_realpath(&buf2
, path2
))
926 case TOMOYO_TYPE_RENAME
:
927 case TOMOYO_TYPE_LINK
:
928 if (!d_is_dir(path1
->dentry
))
931 case TOMOYO_TYPE_PIVOT_ROOT
:
932 tomoyo_add_slash(&buf1
);
933 tomoyo_add_slash(&buf2
);
937 r
.param_type
= TOMOYO_TYPE_PATH2_ACL
;
938 r
.param
.path2
.operation
= operation
;
939 r
.param
.path2
.filename1
= &buf1
;
940 r
.param
.path2
.filename2
= &buf2
;
942 tomoyo_check_acl(&r
, tomoyo_check_path2_acl
);
943 error
= tomoyo_audit_path2_log(&r
);
944 } while (error
== TOMOYO_RETRY_REQUEST
);
948 tomoyo_read_unlock(idx
);
949 if (r
.mode
!= TOMOYO_CONFIG_ENFORCING
)
955 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
957 * @a: Pointer to "struct tomoyo_acl_info".
958 * @b: Pointer to "struct tomoyo_acl_info".
960 * Returns true if @a == @b, false otherwise.
962 static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info
*a
,
963 const struct tomoyo_acl_info
*b
)
965 const struct tomoyo_mount_acl
*p1
= container_of(a
, typeof(*p1
), head
);
966 const struct tomoyo_mount_acl
*p2
= container_of(b
, typeof(*p2
), head
);
968 return tomoyo_same_name_union(&p1
->dev_name
, &p2
->dev_name
) &&
969 tomoyo_same_name_union(&p1
->dir_name
, &p2
->dir_name
) &&
970 tomoyo_same_name_union(&p1
->fs_type
, &p2
->fs_type
) &&
971 tomoyo_same_number_union(&p1
->flags
, &p2
->flags
);
975 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
977 * @param: Pointer to "struct tomoyo_acl_param".
979 * Returns 0 on success, negative value otherwise.
981 * Caller holds tomoyo_read_lock().
983 static int tomoyo_update_mount_acl(struct tomoyo_acl_param
*param
)
985 struct tomoyo_mount_acl e
= { .head
.type
= TOMOYO_TYPE_MOUNT_ACL
};
988 if (!tomoyo_parse_name_union(param
, &e
.dev_name
) ||
989 !tomoyo_parse_name_union(param
, &e
.dir_name
) ||
990 !tomoyo_parse_name_union(param
, &e
.fs_type
) ||
991 !tomoyo_parse_number_union(param
, &e
.flags
))
994 error
= tomoyo_update_domain(&e
.head
, sizeof(e
), param
,
995 tomoyo_same_mount_acl
, NULL
);
996 tomoyo_put_name_union(&e
.dev_name
);
997 tomoyo_put_name_union(&e
.dir_name
);
998 tomoyo_put_name_union(&e
.fs_type
);
999 tomoyo_put_number_union(&e
.flags
);
1004 * tomoyo_write_file - Update file related list.
1006 * @param: Pointer to "struct tomoyo_acl_param".
1008 * Returns 0 on success, negative value otherwise.
1010 * Caller holds tomoyo_read_lock().
1012 int tomoyo_write_file(struct tomoyo_acl_param
*param
)
1016 const char *operation
= tomoyo_read_token(param
);
1018 for (type
= 0; type
< TOMOYO_MAX_PATH_OPERATION
; type
++)
1019 if (tomoyo_permstr(operation
, tomoyo_path_keyword
[type
]))
1022 return tomoyo_update_path_acl(perm
, param
);
1023 for (type
= 0; type
< TOMOYO_MAX_PATH2_OPERATION
; type
++)
1024 if (tomoyo_permstr(operation
,
1025 tomoyo_mac_keywords
[tomoyo_pp2mac
[type
]]))
1028 return tomoyo_update_path2_acl(perm
, param
);
1029 for (type
= 0; type
< TOMOYO_MAX_PATH_NUMBER_OPERATION
; type
++)
1030 if (tomoyo_permstr(operation
,
1031 tomoyo_mac_keywords
[tomoyo_pn2mac
[type
]]))
1034 return tomoyo_update_path_number_acl(perm
, param
);
1035 for (type
= 0; type
< TOMOYO_MAX_MKDEV_OPERATION
; type
++)
1036 if (tomoyo_permstr(operation
,
1037 tomoyo_mac_keywords
[tomoyo_pnnn2mac
[type
]]))
1040 return tomoyo_update_mkdev_acl(perm
, param
);
1041 if (tomoyo_permstr(operation
,
1042 tomoyo_mac_keywords
[TOMOYO_MAC_FILE_MOUNT
]))
1043 return tomoyo_update_mount_acl(param
);