2 * security/tomoyo/file.c
4 * Implementation of the Domain-Based Mandatory Access Control.
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
8 * Version: 2.2.0 2009/04/01
13 #include <linux/slab.h>
15 /* Keyword array for single path operations. */
16 static const char *tomoyo_path_keyword
[TOMOYO_MAX_PATH_OPERATION
] = {
17 [TOMOYO_TYPE_READ_WRITE
] = "read/write",
18 [TOMOYO_TYPE_EXECUTE
] = "execute",
19 [TOMOYO_TYPE_READ
] = "read",
20 [TOMOYO_TYPE_WRITE
] = "write",
21 [TOMOYO_TYPE_CREATE
] = "create",
22 [TOMOYO_TYPE_UNLINK
] = "unlink",
23 [TOMOYO_TYPE_MKDIR
] = "mkdir",
24 [TOMOYO_TYPE_RMDIR
] = "rmdir",
25 [TOMOYO_TYPE_MKFIFO
] = "mkfifo",
26 [TOMOYO_TYPE_MKSOCK
] = "mksock",
27 [TOMOYO_TYPE_MKBLOCK
] = "mkblock",
28 [TOMOYO_TYPE_MKCHAR
] = "mkchar",
29 [TOMOYO_TYPE_TRUNCATE
] = "truncate",
30 [TOMOYO_TYPE_SYMLINK
] = "symlink",
31 [TOMOYO_TYPE_REWRITE
] = "rewrite",
32 [TOMOYO_TYPE_IOCTL
] = "ioctl",
33 [TOMOYO_TYPE_CHMOD
] = "chmod",
34 [TOMOYO_TYPE_CHOWN
] = "chown",
35 [TOMOYO_TYPE_CHGRP
] = "chgrp",
36 [TOMOYO_TYPE_CHROOT
] = "chroot",
37 [TOMOYO_TYPE_MOUNT
] = "mount",
38 [TOMOYO_TYPE_UMOUNT
] = "unmount",
41 /* Keyword array for double path operations. */
42 static const char *tomoyo_path2_keyword
[TOMOYO_MAX_PATH2_OPERATION
] = {
43 [TOMOYO_TYPE_LINK
] = "link",
44 [TOMOYO_TYPE_RENAME
] = "rename",
45 [TOMOYO_TYPE_PIVOT_ROOT
] = "pivot_root",
48 void tomoyo_put_name_union(struct tomoyo_name_union
*ptr
)
53 tomoyo_put_path_group(ptr
->group
);
55 tomoyo_put_name(ptr
->filename
);
58 bool tomoyo_compare_name_union(const struct tomoyo_path_info
*name
,
59 const struct tomoyo_name_union
*ptr
)
62 return tomoyo_path_matches_group(name
, ptr
->group
, 1);
63 return tomoyo_path_matches_pattern(name
, ptr
->filename
);
66 static bool tomoyo_compare_name_union_pattern(const struct tomoyo_path_info
68 const struct tomoyo_name_union
69 *ptr
, const bool may_use_pattern
)
72 return tomoyo_path_matches_group(name
, ptr
->group
,
74 if (may_use_pattern
|| !ptr
->filename
->is_patterned
)
75 return tomoyo_path_matches_pattern(name
, ptr
->filename
);
80 * tomoyo_path2keyword - Get the name of single path operation.
82 * @operation: Type of operation.
84 * Returns the name of single path operation.
86 const char *tomoyo_path2keyword(const u8 operation
)
88 return (operation
< TOMOYO_MAX_PATH_OPERATION
)
89 ? tomoyo_path_keyword
[operation
] : NULL
;
93 * tomoyo_path22keyword - Get the name of double path operation.
95 * @operation: Type of operation.
97 * Returns the name of double path operation.
99 const char *tomoyo_path22keyword(const u8 operation
)
101 return (operation
< TOMOYO_MAX_PATH2_OPERATION
)
102 ? tomoyo_path2_keyword
[operation
] : NULL
;
106 * tomoyo_strendswith - Check whether the token ends with the given token.
108 * @name: The token to check.
109 * @tail: The token to find.
111 * Returns true if @name ends with @tail, false otherwise.
113 static bool tomoyo_strendswith(const char *name
, const char *tail
)
119 len
= strlen(name
) - strlen(tail
);
120 return len
>= 0 && !strcmp(name
+ len
, tail
);
124 * tomoyo_get_path - Get realpath.
126 * @path: Pointer to "struct path".
128 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
130 static struct tomoyo_path_info
*tomoyo_get_path(struct path
*path
)
133 struct tomoyo_path_info_with_data
*buf
= kzalloc(sizeof(*buf
),
138 /* Reserve one byte for appending "/". */
139 error
= tomoyo_realpath_from_path2(path
, buf
->body
,
140 sizeof(buf
->body
) - 2);
142 buf
->head
.name
= buf
->body
;
143 tomoyo_fill_path_info(&buf
->head
);
150 static int tomoyo_update_path2_acl(const u8 type
, const char *filename1
,
151 const char *filename2
,
152 struct tomoyo_domain_info
*const domain
,
153 const bool is_delete
);
154 static int tomoyo_update_path_acl(const u8 type
, const char *filename
,
155 struct tomoyo_domain_info
*const domain
,
156 const bool is_delete
);
159 * tomoyo_globally_readable_list is used for holding list of pathnames which
160 * are by default allowed to be open()ed for reading by any process.
162 * An entry is added by
164 * # echo 'allow_read /lib/libc-2.5.so' > \
165 * /sys/kernel/security/tomoyo/exception_policy
169 * # echo 'delete allow_read /lib/libc-2.5.so' > \
170 * /sys/kernel/security/tomoyo/exception_policy
172 * and all entries are retrieved by
174 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
176 * In the example above, any process is allowed to
177 * open("/lib/libc-2.5.so", O_RDONLY).
178 * One exception is, if the domain which current process belongs to is marked
179 * as "ignore_global_allow_read", current process can't do so unless explicitly
180 * given "allow_read /lib/libc-2.5.so" to the domain which current process
183 LIST_HEAD(tomoyo_globally_readable_list
);
186 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
188 * @filename: Filename unconditionally permitted to open() for reading.
189 * @is_delete: True if it is a delete request.
191 * Returns 0 on success, negative value otherwise.
193 * Caller holds tomoyo_read_lock().
195 static int tomoyo_update_globally_readable_entry(const char *filename
,
196 const bool is_delete
)
198 struct tomoyo_globally_readable_file_entry
*ptr
;
199 struct tomoyo_globally_readable_file_entry e
= { };
200 int error
= is_delete
? -ENOENT
: -ENOMEM
;
202 if (!tomoyo_is_correct_path(filename
, 1, 0, -1))
204 e
.filename
= tomoyo_get_name(filename
);
207 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
209 list_for_each_entry_rcu(ptr
, &tomoyo_globally_readable_list
, list
) {
210 if (ptr
->filename
!= e
.filename
)
212 ptr
->is_deleted
= is_delete
;
216 if (!is_delete
&& error
) {
217 struct tomoyo_globally_readable_file_entry
*entry
=
218 tomoyo_commit_ok(&e
, sizeof(e
));
220 list_add_tail_rcu(&entry
->list
,
221 &tomoyo_globally_readable_list
);
225 mutex_unlock(&tomoyo_policy_lock
);
227 tomoyo_put_name(e
.filename
);
232 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
234 * @filename: The filename to check.
236 * Returns true if any domain can open @filename for reading, false otherwise.
238 * Caller holds tomoyo_read_lock().
240 static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info
*
243 struct tomoyo_globally_readable_file_entry
*ptr
;
246 list_for_each_entry_rcu(ptr
, &tomoyo_globally_readable_list
, list
) {
247 if (!ptr
->is_deleted
&&
248 tomoyo_path_matches_pattern(filename
, ptr
->filename
)) {
257 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
259 * @data: String to parse.
260 * @is_delete: True if it is a delete request.
262 * Returns 0 on success, negative value otherwise.
264 * Caller holds tomoyo_read_lock().
266 int tomoyo_write_globally_readable_policy(char *data
, const bool is_delete
)
268 return tomoyo_update_globally_readable_entry(data
, is_delete
);
272 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
274 * @head: Pointer to "struct tomoyo_io_buffer".
276 * Returns true on success, false otherwise.
278 * Caller holds tomoyo_read_lock().
280 bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer
*head
)
282 struct list_head
*pos
;
285 list_for_each_cookie(pos
, head
->read_var2
,
286 &tomoyo_globally_readable_list
) {
287 struct tomoyo_globally_readable_file_entry
*ptr
;
288 ptr
= list_entry(pos
,
289 struct tomoyo_globally_readable_file_entry
,
293 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_ALLOW_READ
"%s\n",
294 ptr
->filename
->name
);
301 /* tomoyo_pattern_list is used for holding list of pathnames which are used for
302 * converting pathnames to pathname patterns during learning mode.
304 * An entry is added by
306 * # echo 'file_pattern /proc/\$/mounts' > \
307 * /sys/kernel/security/tomoyo/exception_policy
311 * # echo 'delete file_pattern /proc/\$/mounts' > \
312 * /sys/kernel/security/tomoyo/exception_policy
314 * and all entries are retrieved by
316 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
318 * In the example above, if a process which belongs to a domain which is in
319 * learning mode requested open("/proc/1/mounts", O_RDONLY),
320 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
321 * process belongs to.
323 * It is not a desirable behavior that we have to use /proc/\$/ instead of
324 * /proc/self/ when current process needs to access only current process's
325 * information. As of now, LSM version of TOMOYO is using __d_path() for
326 * calculating pathname. Non LSM version of TOMOYO is using its own function
327 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
328 * current process from accessing other process's information.
330 LIST_HEAD(tomoyo_pattern_list
);
333 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
335 * @pattern: Pathname pattern.
336 * @is_delete: True if it is a delete request.
338 * Returns 0 on success, negative value otherwise.
340 * Caller holds tomoyo_read_lock().
342 static int tomoyo_update_file_pattern_entry(const char *pattern
,
343 const bool is_delete
)
345 struct tomoyo_pattern_entry
*ptr
;
346 struct tomoyo_pattern_entry e
= { .pattern
= tomoyo_get_name(pattern
) };
347 int error
= is_delete
? -ENOENT
: -ENOMEM
;
351 if (!e
.pattern
->is_patterned
)
353 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
355 list_for_each_entry_rcu(ptr
, &tomoyo_pattern_list
, list
) {
356 if (e
.pattern
!= ptr
->pattern
)
358 ptr
->is_deleted
= is_delete
;
362 if (!is_delete
&& error
) {
363 struct tomoyo_pattern_entry
*entry
=
364 tomoyo_commit_ok(&e
, sizeof(e
));
366 list_add_tail_rcu(&entry
->list
, &tomoyo_pattern_list
);
370 mutex_unlock(&tomoyo_policy_lock
);
372 tomoyo_put_name(e
.pattern
);
377 * tomoyo_get_file_pattern - Get patterned pathname.
379 * @filename: The filename to find patterned pathname.
381 * Returns pointer to pathname pattern if matched, @filename otherwise.
383 * Caller holds tomoyo_read_lock().
385 static const struct tomoyo_path_info
*
386 tomoyo_get_file_pattern(const struct tomoyo_path_info
*filename
)
388 struct tomoyo_pattern_entry
*ptr
;
389 const struct tomoyo_path_info
*pattern
= NULL
;
391 list_for_each_entry_rcu(ptr
, &tomoyo_pattern_list
, list
) {
394 if (!tomoyo_path_matches_pattern(filename
, ptr
->pattern
))
396 pattern
= ptr
->pattern
;
397 if (tomoyo_strendswith(pattern
->name
, "/\\*")) {
398 /* Do nothing. Try to find the better match. */
400 /* This would be the better match. Use this. */
410 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
412 * @data: String to parse.
413 * @is_delete: True if it is a delete request.
415 * Returns 0 on success, negative value otherwise.
417 * Caller holds tomoyo_read_lock().
419 int tomoyo_write_pattern_policy(char *data
, const bool is_delete
)
421 return tomoyo_update_file_pattern_entry(data
, is_delete
);
425 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
427 * @head: Pointer to "struct tomoyo_io_buffer".
429 * Returns true on success, false otherwise.
431 * Caller holds tomoyo_read_lock().
433 bool tomoyo_read_file_pattern(struct tomoyo_io_buffer
*head
)
435 struct list_head
*pos
;
438 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_pattern_list
) {
439 struct tomoyo_pattern_entry
*ptr
;
440 ptr
= list_entry(pos
, struct tomoyo_pattern_entry
, list
);
443 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_FILE_PATTERN
444 "%s\n", ptr
->pattern
->name
);
452 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
453 * default forbidden to modify already written content of a file.
455 * An entry is added by
457 * # echo 'deny_rewrite /var/log/messages' > \
458 * /sys/kernel/security/tomoyo/exception_policy
462 * # echo 'delete deny_rewrite /var/log/messages' > \
463 * /sys/kernel/security/tomoyo/exception_policy
465 * and all entries are retrieved by
467 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
469 * In the example above, if a process requested to rewrite /var/log/messages ,
470 * the process can't rewrite unless the domain which that process belongs to
471 * has "allow_rewrite /var/log/messages" entry.
473 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
474 * when we want to allow rewriting already unlink()ed file. As of now,
475 * LSM version of TOMOYO is using __d_path() for calculating pathname.
476 * Non LSM version of TOMOYO is using its own function which doesn't append
477 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
478 * need to worry whether the file is already unlink()ed or not.
480 LIST_HEAD(tomoyo_no_rewrite_list
);
483 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
485 * @pattern: Pathname pattern that are not rewritable by default.
486 * @is_delete: True if it is a delete request.
488 * Returns 0 on success, negative value otherwise.
490 * Caller holds tomoyo_read_lock().
492 static int tomoyo_update_no_rewrite_entry(const char *pattern
,
493 const bool is_delete
)
495 struct tomoyo_no_rewrite_entry
*ptr
;
496 struct tomoyo_no_rewrite_entry e
= { };
497 int error
= is_delete
? -ENOENT
: -ENOMEM
;
499 if (!tomoyo_is_correct_path(pattern
, 0, 0, 0))
501 e
.pattern
= tomoyo_get_name(pattern
);
504 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
506 list_for_each_entry_rcu(ptr
, &tomoyo_no_rewrite_list
, list
) {
507 if (ptr
->pattern
!= e
.pattern
)
509 ptr
->is_deleted
= is_delete
;
513 if (!is_delete
&& error
) {
514 struct tomoyo_no_rewrite_entry
*entry
=
515 tomoyo_commit_ok(&e
, sizeof(e
));
517 list_add_tail_rcu(&entry
->list
,
518 &tomoyo_no_rewrite_list
);
522 mutex_unlock(&tomoyo_policy_lock
);
524 tomoyo_put_name(e
.pattern
);
529 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
531 * @filename: Filename to check.
533 * Returns true if @filename is specified by "deny_rewrite" directive,
536 * Caller holds tomoyo_read_lock().
538 static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info
*filename
)
540 struct tomoyo_no_rewrite_entry
*ptr
;
543 list_for_each_entry_rcu(ptr
, &tomoyo_no_rewrite_list
, list
) {
546 if (!tomoyo_path_matches_pattern(filename
, ptr
->pattern
))
555 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
557 * @data: String to parse.
558 * @is_delete: True if it is a delete request.
560 * Returns 0 on success, negative value otherwise.
562 * Caller holds tomoyo_read_lock().
564 int tomoyo_write_no_rewrite_policy(char *data
, const bool is_delete
)
566 return tomoyo_update_no_rewrite_entry(data
, is_delete
);
570 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
572 * @head: Pointer to "struct tomoyo_io_buffer".
574 * Returns true on success, false otherwise.
576 * Caller holds tomoyo_read_lock().
578 bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer
*head
)
580 struct list_head
*pos
;
583 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_no_rewrite_list
) {
584 struct tomoyo_no_rewrite_entry
*ptr
;
585 ptr
= list_entry(pos
, struct tomoyo_no_rewrite_entry
, list
);
588 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_DENY_REWRITE
589 "%s\n", ptr
->pattern
->name
);
597 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
599 * @filename: Filename.
600 * @perm: Permission (between 1 to 7).
601 * @domain: Pointer to "struct tomoyo_domain_info".
602 * @is_delete: True if it is a delete request.
604 * Returns 0 on success, negative value otherwise.
606 * This is legacy support interface for older policy syntax.
607 * Current policy syntax uses "allow_read/write" instead of "6",
608 * "allow_read" instead of "4", "allow_write" instead of "2",
609 * "allow_execute" instead of "1".
611 * Caller holds tomoyo_read_lock().
613 static int tomoyo_update_file_acl(const char *filename
, u8 perm
,
614 struct tomoyo_domain_info
* const domain
,
615 const bool is_delete
)
617 if (perm
> 7 || !perm
) {
618 printk(KERN_DEBUG
"%s: Invalid permission '%d %s'\n",
619 __func__
, perm
, filename
);
622 if (filename
[0] != '@' && tomoyo_strendswith(filename
, "/"))
624 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
625 * directory permissions.
629 tomoyo_update_path_acl(TOMOYO_TYPE_READ
, filename
, domain
,
632 tomoyo_update_path_acl(TOMOYO_TYPE_WRITE
, filename
, domain
,
635 tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE
, filename
, domain
,
641 * tomoyo_path_acl2 - Check permission for single path operation.
643 * @domain: Pointer to "struct tomoyo_domain_info".
644 * @filename: Filename to check.
646 * @may_use_pattern: True if patterned ACL is permitted.
648 * Returns 0 on success, -EPERM otherwise.
650 * Caller holds tomoyo_read_lock().
652 static int tomoyo_path_acl2(const struct tomoyo_domain_info
*domain
,
653 const struct tomoyo_path_info
*filename
,
654 const u32 perm
, const bool may_use_pattern
)
656 struct tomoyo_acl_info
*ptr
;
659 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
660 struct tomoyo_path_acl
*acl
;
661 if (ptr
->type
!= TOMOYO_TYPE_PATH_ACL
)
663 acl
= container_of(ptr
, struct tomoyo_path_acl
, head
);
664 if (perm
<= 0xFFFF) {
665 if (!(acl
->perm
& perm
))
668 if (!(acl
->perm_high
& (perm
>> 16)))
671 if (!tomoyo_compare_name_union_pattern(filename
, &acl
->name
,
681 * tomoyo_check_file_acl - Check permission for opening files.
683 * @domain: Pointer to "struct tomoyo_domain_info".
684 * @filename: Filename to check.
685 * @operation: Mode ("read" or "write" or "read/write" or "execute").
687 * Returns 0 on success, -EPERM otherwise.
689 * Caller holds tomoyo_read_lock().
691 static int tomoyo_check_file_acl(const struct tomoyo_domain_info
*domain
,
692 const struct tomoyo_path_info
*filename
,
697 if (!tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
))
700 perm
= 1 << TOMOYO_TYPE_READ_WRITE
;
701 else if (operation
== 4)
702 perm
= 1 << TOMOYO_TYPE_READ
;
703 else if (operation
== 2)
704 perm
= 1 << TOMOYO_TYPE_WRITE
;
705 else if (operation
== 1)
706 perm
= 1 << TOMOYO_TYPE_EXECUTE
;
709 return tomoyo_path_acl2(domain
, filename
, perm
, operation
!= 1);
713 * tomoyo_check_file_perm2 - Check permission for opening files.
715 * @domain: Pointer to "struct tomoyo_domain_info".
716 * @filename: Filename to check.
717 * @perm: Mode ("read" or "write" or "read/write" or "execute").
718 * @operation: Operation name passed used for verbose mode.
719 * @mode: Access control mode.
721 * Returns 0 on success, negative value otherwise.
723 * Caller holds tomoyo_read_lock().
725 static int tomoyo_check_file_perm2(struct tomoyo_domain_info
* const domain
,
726 const struct tomoyo_path_info
*filename
,
727 const u8 perm
, const char *operation
,
730 const bool is_enforce
= (mode
== 3);
731 const char *msg
= "<unknown>";
736 error
= tomoyo_check_file_acl(domain
, filename
, perm
);
737 if (error
&& perm
== 4 && !domain
->ignore_global_allow_read
738 && tomoyo_is_globally_readable_file(filename
))
741 msg
= tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE
);
743 msg
= tomoyo_path2keyword(TOMOYO_TYPE_READ
);
745 msg
= tomoyo_path2keyword(TOMOYO_TYPE_WRITE
);
747 msg
= tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE
);
752 if (tomoyo_verbose_mode(domain
))
753 printk(KERN_WARNING
"TOMOYO-%s: Access '%s(%s) %s' denied "
754 "for %s\n", tomoyo_get_msg(is_enforce
), msg
, operation
,
755 filename
->name
, tomoyo_get_last_name(domain
));
758 if (mode
== 1 && tomoyo_domain_quota_is_ok(domain
)) {
759 /* Don't use patterns for execute permission. */
760 const struct tomoyo_path_info
*patterned_file
= (perm
!= 1) ?
761 tomoyo_get_file_pattern(filename
) : filename
;
762 tomoyo_update_file_acl(patterned_file
->name
, perm
,
769 * tomoyo_write_file_policy - Update file related list.
771 * @data: String to parse.
772 * @domain: Pointer to "struct tomoyo_domain_info".
773 * @is_delete: True if it is a delete request.
775 * Returns 0 on success, negative value otherwise.
777 * Caller holds tomoyo_read_lock().
779 int tomoyo_write_file_policy(char *data
, struct tomoyo_domain_info
*domain
,
780 const bool is_delete
)
782 char *filename
= strchr(data
, ' ');
790 if (sscanf(data
, "%u", &perm
) == 1)
791 return tomoyo_update_file_acl(filename
, (u8
) perm
, domain
,
793 if (strncmp(data
, "allow_", 6))
796 for (type
= 0; type
< TOMOYO_MAX_PATH_OPERATION
; type
++) {
797 if (strcmp(data
, tomoyo_path_keyword
[type
]))
799 return tomoyo_update_path_acl(type
, filename
, domain
,
802 filename2
= strchr(filename
, ' ');
806 for (type
= 0; type
< TOMOYO_MAX_PATH2_OPERATION
; type
++) {
807 if (strcmp(data
, tomoyo_path2_keyword
[type
]))
809 return tomoyo_update_path2_acl(type
, filename
, filename2
,
817 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
819 * @type: Type of operation.
820 * @filename: Filename.
821 * @domain: Pointer to "struct tomoyo_domain_info".
822 * @is_delete: True if it is a delete request.
824 * Returns 0 on success, negative value otherwise.
826 * Caller holds tomoyo_read_lock().
828 static int tomoyo_update_path_acl(const u8 type
, const char *filename
,
829 struct tomoyo_domain_info
*const domain
,
830 const bool is_delete
)
832 static const u32 tomoyo_rw_mask
=
833 (1 << TOMOYO_TYPE_READ
) | (1 << TOMOYO_TYPE_WRITE
);
834 const u32 perm
= 1 << type
;
835 struct tomoyo_acl_info
*ptr
;
836 struct tomoyo_path_acl e
= {
837 .head
.type
= TOMOYO_TYPE_PATH_ACL
,
838 .perm_high
= perm
>> 16,
841 int error
= is_delete
? -ENOENT
: -ENOMEM
;
843 if (type
== TOMOYO_TYPE_READ_WRITE
)
844 e
.perm
|= tomoyo_rw_mask
;
847 if (!tomoyo_parse_name_union(filename
, &e
.name
))
849 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
851 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
852 struct tomoyo_path_acl
*acl
=
853 container_of(ptr
, struct tomoyo_path_acl
, head
);
854 if (!tomoyo_is_same_path_acl(acl
, &e
))
860 acl
->perm_high
&= ~(perm
>> 16);
861 if ((acl
->perm
& tomoyo_rw_mask
) != tomoyo_rw_mask
)
862 acl
->perm
&= ~(1 << TOMOYO_TYPE_READ_WRITE
);
863 else if (!(acl
->perm
& (1 << TOMOYO_TYPE_READ_WRITE
)))
864 acl
->perm
&= ~tomoyo_rw_mask
;
869 acl
->perm_high
|= (perm
>> 16);
870 if ((acl
->perm
& tomoyo_rw_mask
) == tomoyo_rw_mask
)
871 acl
->perm
|= 1 << TOMOYO_TYPE_READ_WRITE
;
872 else if (acl
->perm
& (1 << TOMOYO_TYPE_READ_WRITE
))
873 acl
->perm
|= tomoyo_rw_mask
;
878 if (!is_delete
&& error
) {
879 struct tomoyo_path_acl
*entry
=
880 tomoyo_commit_ok(&e
, sizeof(e
));
882 list_add_tail_rcu(&entry
->head
.list
,
883 &domain
->acl_info_list
);
887 mutex_unlock(&tomoyo_policy_lock
);
889 tomoyo_put_name_union(&e
.name
);
894 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
896 * @type: Type of operation.
897 * @filename1: First filename.
898 * @filename2: Second filename.
899 * @domain: Pointer to "struct tomoyo_domain_info".
900 * @is_delete: True if it is a delete request.
902 * Returns 0 on success, negative value otherwise.
904 * Caller holds tomoyo_read_lock().
906 static int tomoyo_update_path2_acl(const u8 type
, const char *filename1
,
907 const char *filename2
,
908 struct tomoyo_domain_info
*const domain
,
909 const bool is_delete
)
911 const u8 perm
= 1 << type
;
912 struct tomoyo_path2_acl e
= {
913 .head
.type
= TOMOYO_TYPE_PATH2_ACL
,
916 struct tomoyo_acl_info
*ptr
;
917 int error
= is_delete
? -ENOENT
: -ENOMEM
;
921 if (!tomoyo_parse_name_union(filename1
, &e
.name1
) ||
922 !tomoyo_parse_name_union(filename2
, &e
.name2
))
924 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
926 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
927 struct tomoyo_path2_acl
*acl
=
928 container_of(ptr
, struct tomoyo_path2_acl
, head
);
929 if (!tomoyo_is_same_path2_acl(acl
, &e
))
938 if (!is_delete
&& error
) {
939 struct tomoyo_path2_acl
*entry
=
940 tomoyo_commit_ok(&e
, sizeof(e
));
942 list_add_tail_rcu(&entry
->head
.list
,
943 &domain
->acl_info_list
);
947 mutex_unlock(&tomoyo_policy_lock
);
949 tomoyo_put_name_union(&e
.name1
);
950 tomoyo_put_name_union(&e
.name2
);
955 * tomoyo_path_acl - Check permission for single path operation.
957 * @domain: Pointer to "struct tomoyo_domain_info".
958 * @type: Type of operation.
959 * @filename: Filename to check.
961 * Returns 0 on success, negative value otherwise.
963 * Caller holds tomoyo_read_lock().
965 static int tomoyo_path_acl(struct tomoyo_domain_info
*domain
, const u8 type
,
966 const struct tomoyo_path_info
*filename
)
968 if (!tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
))
970 return tomoyo_path_acl2(domain
, filename
, 1 << type
, 1);
974 * tomoyo_path2_acl - Check permission for double path operation.
976 * @domain: Pointer to "struct tomoyo_domain_info".
977 * @type: Type of operation.
978 * @filename1: First filename to check.
979 * @filename2: Second filename to check.
981 * Returns 0 on success, -EPERM otherwise.
983 * Caller holds tomoyo_read_lock().
985 static int tomoyo_path2_acl(const struct tomoyo_domain_info
*domain
,
987 const struct tomoyo_path_info
*filename1
,
988 const struct tomoyo_path_info
*filename2
)
990 struct tomoyo_acl_info
*ptr
;
991 const u8 perm
= 1 << type
;
994 if (!tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
))
996 list_for_each_entry_rcu(ptr
, &domain
->acl_info_list
, list
) {
997 struct tomoyo_path2_acl
*acl
;
998 if (ptr
->type
!= TOMOYO_TYPE_PATH2_ACL
)
1000 acl
= container_of(ptr
, struct tomoyo_path2_acl
, head
);
1001 if (!(acl
->perm
& perm
))
1003 if (!tomoyo_compare_name_union(filename1
, &acl
->name1
))
1005 if (!tomoyo_compare_name_union(filename2
, &acl
->name2
))
1014 * tomoyo_path_permission2 - Check permission for single path operation.
1016 * @domain: Pointer to "struct tomoyo_domain_info".
1017 * @operation: Type of operation.
1018 * @filename: Filename to check.
1019 * @mode: Access control mode.
1021 * Returns 0 on success, negative value otherwise.
1023 * Caller holds tomoyo_read_lock().
1025 static int tomoyo_path_permission2(struct tomoyo_domain_info
*const domain
,
1027 const struct tomoyo_path_info
*filename
,
1032 const bool is_enforce
= (mode
== 3);
1037 error
= tomoyo_path_acl(domain
, operation
, filename
);
1038 msg
= tomoyo_path2keyword(operation
);
1041 if (tomoyo_verbose_mode(domain
))
1042 printk(KERN_WARNING
"TOMOYO-%s: Access '%s %s' denied for %s\n",
1043 tomoyo_get_msg(is_enforce
), msg
, filename
->name
,
1044 tomoyo_get_last_name(domain
));
1045 if (mode
== 1 && tomoyo_domain_quota_is_ok(domain
)) {
1046 const char *name
= tomoyo_get_file_pattern(filename
)->name
;
1047 tomoyo_update_path_acl(operation
, name
, domain
, false);
1053 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1054 * we need to check "allow_rewrite" permission if the filename is
1055 * specified by "deny_rewrite" keyword.
1057 if (!error
&& operation
== TOMOYO_TYPE_TRUNCATE
&&
1058 tomoyo_is_no_rewrite_file(filename
)) {
1059 operation
= TOMOYO_TYPE_REWRITE
;
1066 * tomoyo_check_exec_perm - Check permission for "execute".
1068 * @domain: Pointer to "struct tomoyo_domain_info".
1069 * @filename: Check permission for "execute".
1071 * Returns 0 on success, negativevalue otherwise.
1073 * Caller holds tomoyo_read_lock().
1075 int tomoyo_check_exec_perm(struct tomoyo_domain_info
*domain
,
1076 const struct tomoyo_path_info
*filename
)
1078 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1082 return tomoyo_check_file_perm2(domain
, filename
, 1, "do_execve", mode
);
1086 * tomoyo_check_open_permission - Check permission for "read" and "write".
1088 * @domain: Pointer to "struct tomoyo_domain_info".
1089 * @path: Pointer to "struct path".
1090 * @flag: Flags for open().
1092 * Returns 0 on success, negative value otherwise.
1094 int tomoyo_check_open_permission(struct tomoyo_domain_info
*domain
,
1095 struct path
*path
, const int flag
)
1097 const u8 acc_mode
= ACC_MODE(flag
);
1098 int error
= -ENOMEM
;
1099 struct tomoyo_path_info
*buf
;
1100 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1101 const bool is_enforce
= (mode
== 3);
1104 if (!mode
|| !path
->mnt
)
1108 if (path
->dentry
->d_inode
&& S_ISDIR(path
->dentry
->d_inode
->i_mode
))
1110 * I don't check directories here because mkdir() and rmdir()
1114 idx
= tomoyo_read_lock();
1115 buf
= tomoyo_get_path(path
);
1120 * If the filename is specified by "deny_rewrite" keyword,
1121 * we need to check "allow_rewrite" permission when the filename is not
1122 * opened for append mode or the filename is truncated at open time.
1124 if ((acc_mode
& MAY_WRITE
) &&
1125 ((flag
& O_TRUNC
) || !(flag
& O_APPEND
)) &&
1126 (tomoyo_is_no_rewrite_file(buf
))) {
1127 error
= tomoyo_path_permission2(domain
, TOMOYO_TYPE_REWRITE
,
1131 error
= tomoyo_check_file_perm2(domain
, buf
, acc_mode
, "open",
1133 if (!error
&& (flag
& O_TRUNC
))
1134 error
= tomoyo_path_permission2(domain
, TOMOYO_TYPE_TRUNCATE
,
1138 tomoyo_read_unlock(idx
);
1145 * tomoyo_path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate", "symlink", "ioctl", "chmod", "chown", "chgrp", "chroot", "mount" and "unmount".
1147 * @operation: Type of operation.
1148 * @path: Pointer to "struct path".
1150 * Returns 0 on success, negative value otherwise.
1152 int tomoyo_path_perm(const u8 operation
, struct path
*path
)
1154 int error
= -ENOMEM
;
1155 struct tomoyo_path_info
*buf
;
1156 struct tomoyo_domain_info
*domain
= tomoyo_domain();
1157 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1158 const bool is_enforce
= (mode
== 3);
1161 if (!mode
|| !path
->mnt
)
1163 idx
= tomoyo_read_lock();
1164 buf
= tomoyo_get_path(path
);
1167 switch (operation
) {
1168 case TOMOYO_TYPE_MKDIR
:
1169 case TOMOYO_TYPE_RMDIR
:
1170 case TOMOYO_TYPE_CHROOT
:
1173 * tomoyo_get_path() reserves space for appending "/."
1175 strcat((char *) buf
->name
, "/");
1176 tomoyo_fill_path_info(buf
);
1179 error
= tomoyo_path_permission2(domain
, operation
, buf
, mode
);
1182 tomoyo_read_unlock(idx
);
1189 * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1191 * @filp: Pointer to "struct file".
1193 * Returns 0 on success, negative value otherwise.
1195 int tomoyo_check_rewrite_permission(struct file
*filp
)
1197 int error
= -ENOMEM
;
1198 struct tomoyo_domain_info
*domain
= tomoyo_domain();
1199 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1200 const bool is_enforce
= (mode
== 3);
1201 struct tomoyo_path_info
*buf
;
1204 if (!mode
|| !filp
->f_path
.mnt
)
1207 idx
= tomoyo_read_lock();
1208 buf
= tomoyo_get_path(&filp
->f_path
);
1211 if (!tomoyo_is_no_rewrite_file(buf
)) {
1215 error
= tomoyo_path_permission2(domain
, TOMOYO_TYPE_REWRITE
, buf
, mode
);
1218 tomoyo_read_unlock(idx
);
1225 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
1227 * @operation: Type of operation.
1228 * @path1: Pointer to "struct path".
1229 * @path2: Pointer to "struct path".
1231 * Returns 0 on success, negative value otherwise.
1233 int tomoyo_path2_perm(const u8 operation
, struct path
*path1
,
1236 int error
= -ENOMEM
;
1237 struct tomoyo_path_info
*buf1
, *buf2
;
1238 struct tomoyo_domain_info
*domain
= tomoyo_domain();
1239 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1240 const bool is_enforce
= (mode
== 3);
1244 if (!mode
|| !path1
->mnt
|| !path2
->mnt
)
1246 idx
= tomoyo_read_lock();
1247 buf1
= tomoyo_get_path(path1
);
1248 buf2
= tomoyo_get_path(path2
);
1252 struct dentry
*dentry
= path1
->dentry
;
1253 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
1255 * tomoyo_get_path() reserves space for appending "/."
1257 if (!buf1
->is_dir
) {
1258 strcat((char *) buf1
->name
, "/");
1259 tomoyo_fill_path_info(buf1
);
1261 if (!buf2
->is_dir
) {
1262 strcat((char *) buf2
->name
, "/");
1263 tomoyo_fill_path_info(buf2
);
1267 error
= tomoyo_path2_acl(domain
, operation
, buf1
, buf2
);
1268 msg
= tomoyo_path22keyword(operation
);
1271 if (tomoyo_verbose_mode(domain
))
1272 printk(KERN_WARNING
"TOMOYO-%s: Access '%s %s %s' "
1273 "denied for %s\n", tomoyo_get_msg(is_enforce
),
1274 msg
, buf1
->name
, buf2
->name
,
1275 tomoyo_get_last_name(domain
));
1276 if (mode
== 1 && tomoyo_domain_quota_is_ok(domain
)) {
1277 const char *name1
= tomoyo_get_file_pattern(buf1
)->name
;
1278 const char *name2
= tomoyo_get_file_pattern(buf2
)->name
;
1279 tomoyo_update_path2_acl(operation
, name1
, name2
, domain
,
1285 tomoyo_read_unlock(idx
);