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
15 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
18 * tomoyo_globally_readable_file_entry is a structure which is used for holding
19 * "allow_read" entries.
20 * It has following fields.
22 * (1) "list" which is linked to tomoyo_globally_readable_list .
23 * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
24 * (3) "is_deleted" is a bool which is true if marked as deleted, false
27 struct tomoyo_globally_readable_file_entry
{
28 struct list_head list
;
29 const struct tomoyo_path_info
*filename
;
34 * tomoyo_pattern_entry is a structure which is used for holding
35 * "tomoyo_pattern_list" entries.
36 * It has following fields.
38 * (1) "list" which is linked to tomoyo_pattern_list .
39 * (2) "pattern" is a pathname pattern which is used for converting pathnames
40 * to pathname patterns during learning mode.
41 * (3) "is_deleted" is a bool which is true if marked as deleted, false
44 struct tomoyo_pattern_entry
{
45 struct list_head list
;
46 const struct tomoyo_path_info
*pattern
;
51 * tomoyo_no_rewrite_entry is a structure which is used for holding
52 * "deny_rewrite" entries.
53 * It has following fields.
55 * (1) "list" which is linked to tomoyo_no_rewrite_list .
56 * (2) "pattern" is a pathname which is by default not permitted to modify
57 * already existing content.
58 * (3) "is_deleted" is a bool which is true if marked as deleted, false
61 struct tomoyo_no_rewrite_entry
{
62 struct list_head list
;
63 const struct tomoyo_path_info
*pattern
;
67 /* Keyword array for single path operations. */
68 static const char *tomoyo_sp_keyword
[TOMOYO_MAX_SINGLE_PATH_OPERATION
] = {
69 [TOMOYO_TYPE_READ_WRITE_ACL
] = "read/write",
70 [TOMOYO_TYPE_EXECUTE_ACL
] = "execute",
71 [TOMOYO_TYPE_READ_ACL
] = "read",
72 [TOMOYO_TYPE_WRITE_ACL
] = "write",
73 [TOMOYO_TYPE_CREATE_ACL
] = "create",
74 [TOMOYO_TYPE_UNLINK_ACL
] = "unlink",
75 [TOMOYO_TYPE_MKDIR_ACL
] = "mkdir",
76 [TOMOYO_TYPE_RMDIR_ACL
] = "rmdir",
77 [TOMOYO_TYPE_MKFIFO_ACL
] = "mkfifo",
78 [TOMOYO_TYPE_MKSOCK_ACL
] = "mksock",
79 [TOMOYO_TYPE_MKBLOCK_ACL
] = "mkblock",
80 [TOMOYO_TYPE_MKCHAR_ACL
] = "mkchar",
81 [TOMOYO_TYPE_TRUNCATE_ACL
] = "truncate",
82 [TOMOYO_TYPE_SYMLINK_ACL
] = "symlink",
83 [TOMOYO_TYPE_REWRITE_ACL
] = "rewrite",
86 /* Keyword array for double path operations. */
87 static const char *tomoyo_dp_keyword
[TOMOYO_MAX_DOUBLE_PATH_OPERATION
] = {
88 [TOMOYO_TYPE_LINK_ACL
] = "link",
89 [TOMOYO_TYPE_RENAME_ACL
] = "rename",
93 * tomoyo_sp2keyword - Get the name of single path operation.
95 * @operation: Type of operation.
97 * Returns the name of single path operation.
99 const char *tomoyo_sp2keyword(const u8 operation
)
101 return (operation
< TOMOYO_MAX_SINGLE_PATH_OPERATION
)
102 ? tomoyo_sp_keyword
[operation
] : NULL
;
106 * tomoyo_dp2keyword - Get the name of double path operation.
108 * @operation: Type of operation.
110 * Returns the name of double path operation.
112 const char *tomoyo_dp2keyword(const u8 operation
)
114 return (operation
< TOMOYO_MAX_DOUBLE_PATH_OPERATION
)
115 ? tomoyo_dp_keyword
[operation
] : NULL
;
119 * tomoyo_strendswith - Check whether the token ends with the given token.
121 * @name: The token to check.
122 * @tail: The token to find.
124 * Returns true if @name ends with @tail, false otherwise.
126 static bool tomoyo_strendswith(const char *name
, const char *tail
)
132 len
= strlen(name
) - strlen(tail
);
133 return len
>= 0 && !strcmp(name
+ len
, tail
);
137 * tomoyo_get_path - Get realpath.
139 * @path: Pointer to "struct path".
141 * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
143 static struct tomoyo_path_info
*tomoyo_get_path(struct path
*path
)
146 struct tomoyo_path_info_with_data
*buf
= tomoyo_alloc(sizeof(*buf
));
150 /* Reserve one byte for appending "/". */
151 error
= tomoyo_realpath_from_path2(path
, buf
->body
,
152 sizeof(buf
->body
) - 2);
154 buf
->head
.name
= buf
->body
;
155 tomoyo_fill_path_info(&buf
->head
);
162 /* Lock for domain->acl_info_list. */
163 DECLARE_RWSEM(tomoyo_domain_acl_info_list_lock
);
165 static int tomoyo_update_double_path_acl(const u8 type
, const char *filename1
,
166 const char *filename2
,
167 struct tomoyo_domain_info
*
168 const domain
, const bool is_delete
);
169 static int tomoyo_update_single_path_acl(const u8 type
, const char *filename
,
170 struct tomoyo_domain_info
*
171 const domain
, const bool is_delete
);
174 * tomoyo_globally_readable_list is used for holding list of pathnames which
175 * are by default allowed to be open()ed for reading by any process.
177 * An entry is added by
179 * # echo 'allow_read /lib/libc-2.5.so' > \
180 * /sys/kernel/security/tomoyo/exception_policy
184 * # echo 'delete allow_read /lib/libc-2.5.so' > \
185 * /sys/kernel/security/tomoyo/exception_policy
187 * and all entries are retrieved by
189 * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
191 * In the example above, any process is allowed to
192 * open("/lib/libc-2.5.so", O_RDONLY).
193 * One exception is, if the domain which current process belongs to is marked
194 * as "ignore_global_allow_read", current process can't do so unless explicitly
195 * given "allow_read /lib/libc-2.5.so" to the domain which current process
198 static LIST_HEAD(tomoyo_globally_readable_list
);
199 static DECLARE_RWSEM(tomoyo_globally_readable_list_lock
);
202 * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
204 * @filename: Filename unconditionally permitted to open() for reading.
205 * @is_delete: True if it is a delete request.
207 * Returns 0 on success, negative value otherwise.
209 static int tomoyo_update_globally_readable_entry(const char *filename
,
210 const bool is_delete
)
212 struct tomoyo_globally_readable_file_entry
*new_entry
;
213 struct tomoyo_globally_readable_file_entry
*ptr
;
214 const struct tomoyo_path_info
*saved_filename
;
217 if (!tomoyo_is_correct_path(filename
, 1, 0, -1, __func__
))
219 saved_filename
= tomoyo_save_name(filename
);
222 down_write(&tomoyo_globally_readable_list_lock
);
223 list_for_each_entry(ptr
, &tomoyo_globally_readable_list
, list
) {
224 if (ptr
->filename
!= saved_filename
)
226 ptr
->is_deleted
= is_delete
;
234 new_entry
= tomoyo_alloc_element(sizeof(*new_entry
));
237 new_entry
->filename
= saved_filename
;
238 list_add_tail(&new_entry
->list
, &tomoyo_globally_readable_list
);
241 up_write(&tomoyo_globally_readable_list_lock
);
246 * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
248 * @filename: The filename to check.
250 * Returns true if any domain can open @filename for reading, false otherwise.
252 static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info
*
255 struct tomoyo_globally_readable_file_entry
*ptr
;
257 down_read(&tomoyo_globally_readable_list_lock
);
258 list_for_each_entry(ptr
, &tomoyo_globally_readable_list
, list
) {
259 if (!ptr
->is_deleted
&&
260 tomoyo_path_matches_pattern(filename
, ptr
->filename
)) {
265 up_read(&tomoyo_globally_readable_list_lock
);
270 * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
272 * @data: String to parse.
273 * @is_delete: True if it is a delete request.
275 * Returns 0 on success, negative value otherwise.
277 int tomoyo_write_globally_readable_policy(char *data
, const bool is_delete
)
279 return tomoyo_update_globally_readable_entry(data
, is_delete
);
283 * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
285 * @head: Pointer to "struct tomoyo_io_buffer".
287 * Returns true on success, false otherwise.
289 bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer
*head
)
291 struct list_head
*pos
;
294 down_read(&tomoyo_globally_readable_list_lock
);
295 list_for_each_cookie(pos
, head
->read_var2
,
296 &tomoyo_globally_readable_list
) {
297 struct tomoyo_globally_readable_file_entry
*ptr
;
298 ptr
= list_entry(pos
,
299 struct tomoyo_globally_readable_file_entry
,
303 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_ALLOW_READ
"%s\n",
304 ptr
->filename
->name
);
308 up_read(&tomoyo_globally_readable_list_lock
);
312 /* tomoyo_pattern_list is used for holding list of pathnames which are used for
313 * converting pathnames to pathname patterns during learning mode.
315 * An entry is added by
317 * # echo 'file_pattern /proc/\$/mounts' > \
318 * /sys/kernel/security/tomoyo/exception_policy
322 * # echo 'delete file_pattern /proc/\$/mounts' > \
323 * /sys/kernel/security/tomoyo/exception_policy
325 * and all entries are retrieved by
327 * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
329 * In the example above, if a process which belongs to a domain which is in
330 * learning mode requested open("/proc/1/mounts", O_RDONLY),
331 * "allow_read /proc/\$/mounts" is automatically added to the domain which that
332 * process belongs to.
334 * It is not a desirable behavior that we have to use /proc/\$/ instead of
335 * /proc/self/ when current process needs to access only current process's
336 * information. As of now, LSM version of TOMOYO is using __d_path() for
337 * calculating pathname. Non LSM version of TOMOYO is using its own function
338 * which pretends as if /proc/self/ is not a symlink; so that we can forbid
339 * current process from accessing other process's information.
341 static LIST_HEAD(tomoyo_pattern_list
);
342 static DECLARE_RWSEM(tomoyo_pattern_list_lock
);
345 * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
347 * @pattern: Pathname pattern.
348 * @is_delete: True if it is a delete request.
350 * Returns 0 on success, negative value otherwise.
352 static int tomoyo_update_file_pattern_entry(const char *pattern
,
353 const bool is_delete
)
355 struct tomoyo_pattern_entry
*new_entry
;
356 struct tomoyo_pattern_entry
*ptr
;
357 const struct tomoyo_path_info
*saved_pattern
;
360 if (!tomoyo_is_correct_path(pattern
, 0, 1, 0, __func__
))
362 saved_pattern
= tomoyo_save_name(pattern
);
365 down_write(&tomoyo_pattern_list_lock
);
366 list_for_each_entry(ptr
, &tomoyo_pattern_list
, list
) {
367 if (saved_pattern
!= ptr
->pattern
)
369 ptr
->is_deleted
= is_delete
;
377 new_entry
= tomoyo_alloc_element(sizeof(*new_entry
));
380 new_entry
->pattern
= saved_pattern
;
381 list_add_tail(&new_entry
->list
, &tomoyo_pattern_list
);
384 up_write(&tomoyo_pattern_list_lock
);
389 * tomoyo_get_file_pattern - Get patterned pathname.
391 * @filename: The filename to find patterned pathname.
393 * Returns pointer to pathname pattern if matched, @filename otherwise.
395 static const struct tomoyo_path_info
*
396 tomoyo_get_file_pattern(const struct tomoyo_path_info
*filename
)
398 struct tomoyo_pattern_entry
*ptr
;
399 const struct tomoyo_path_info
*pattern
= NULL
;
401 down_read(&tomoyo_pattern_list_lock
);
402 list_for_each_entry(ptr
, &tomoyo_pattern_list
, list
) {
405 if (!tomoyo_path_matches_pattern(filename
, ptr
->pattern
))
407 pattern
= ptr
->pattern
;
408 if (tomoyo_strendswith(pattern
->name
, "/\\*")) {
409 /* Do nothing. Try to find the better match. */
411 /* This would be the better match. Use this. */
415 up_read(&tomoyo_pattern_list_lock
);
422 * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
424 * @data: String to parse.
425 * @is_delete: True if it is a delete request.
427 * Returns 0 on success, negative value otherwise.
429 int tomoyo_write_pattern_policy(char *data
, const bool is_delete
)
431 return tomoyo_update_file_pattern_entry(data
, is_delete
);
435 * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
437 * @head: Pointer to "struct tomoyo_io_buffer".
439 * Returns true on success, false otherwise.
441 bool tomoyo_read_file_pattern(struct tomoyo_io_buffer
*head
)
443 struct list_head
*pos
;
446 down_read(&tomoyo_pattern_list_lock
);
447 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_pattern_list
) {
448 struct tomoyo_pattern_entry
*ptr
;
449 ptr
= list_entry(pos
, struct tomoyo_pattern_entry
, list
);
452 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_FILE_PATTERN
453 "%s\n", ptr
->pattern
->name
);
457 up_read(&tomoyo_pattern_list_lock
);
462 * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
463 * default forbidden to modify already written content of a file.
465 * An entry is added by
467 * # echo 'deny_rewrite /var/log/messages' > \
468 * /sys/kernel/security/tomoyo/exception_policy
472 * # echo 'delete deny_rewrite /var/log/messages' > \
473 * /sys/kernel/security/tomoyo/exception_policy
475 * and all entries are retrieved by
477 * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
479 * In the example above, if a process requested to rewrite /var/log/messages ,
480 * the process can't rewrite unless the domain which that process belongs to
481 * has "allow_rewrite /var/log/messages" entry.
483 * It is not a desirable behavior that we have to add "\040(deleted)" suffix
484 * when we want to allow rewriting already unlink()ed file. As of now,
485 * LSM version of TOMOYO is using __d_path() for calculating pathname.
486 * Non LSM version of TOMOYO is using its own function which doesn't append
487 * " (deleted)" suffix if the file is already unlink()ed; so that we don't
488 * need to worry whether the file is already unlink()ed or not.
490 static LIST_HEAD(tomoyo_no_rewrite_list
);
491 static DECLARE_RWSEM(tomoyo_no_rewrite_list_lock
);
494 * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
496 * @pattern: Pathname pattern that are not rewritable by default.
497 * @is_delete: True if it is a delete request.
499 * Returns 0 on success, negative value otherwise.
501 static int tomoyo_update_no_rewrite_entry(const char *pattern
,
502 const bool is_delete
)
504 struct tomoyo_no_rewrite_entry
*new_entry
, *ptr
;
505 const struct tomoyo_path_info
*saved_pattern
;
508 if (!tomoyo_is_correct_path(pattern
, 0, 0, 0, __func__
))
510 saved_pattern
= tomoyo_save_name(pattern
);
513 down_write(&tomoyo_no_rewrite_list_lock
);
514 list_for_each_entry(ptr
, &tomoyo_no_rewrite_list
, list
) {
515 if (ptr
->pattern
!= saved_pattern
)
517 ptr
->is_deleted
= is_delete
;
525 new_entry
= tomoyo_alloc_element(sizeof(*new_entry
));
528 new_entry
->pattern
= saved_pattern
;
529 list_add_tail(&new_entry
->list
, &tomoyo_no_rewrite_list
);
532 up_write(&tomoyo_no_rewrite_list_lock
);
537 * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
539 * @filename: Filename to check.
541 * Returns true if @filename is specified by "deny_rewrite" directive,
544 static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info
*filename
)
546 struct tomoyo_no_rewrite_entry
*ptr
;
549 down_read(&tomoyo_no_rewrite_list_lock
);
550 list_for_each_entry(ptr
, &tomoyo_no_rewrite_list
, list
) {
553 if (!tomoyo_path_matches_pattern(filename
, ptr
->pattern
))
558 up_read(&tomoyo_no_rewrite_list_lock
);
563 * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
565 * @data: String to parse.
566 * @is_delete: True if it is a delete request.
568 * Returns 0 on success, negative value otherwise.
570 int tomoyo_write_no_rewrite_policy(char *data
, const bool is_delete
)
572 return tomoyo_update_no_rewrite_entry(data
, is_delete
);
576 * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
578 * @head: Pointer to "struct tomoyo_io_buffer".
580 * Returns true on success, false otherwise.
582 bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer
*head
)
584 struct list_head
*pos
;
587 down_read(&tomoyo_no_rewrite_list_lock
);
588 list_for_each_cookie(pos
, head
->read_var2
, &tomoyo_no_rewrite_list
) {
589 struct tomoyo_no_rewrite_entry
*ptr
;
590 ptr
= list_entry(pos
, struct tomoyo_no_rewrite_entry
, list
);
593 done
= tomoyo_io_printf(head
, TOMOYO_KEYWORD_DENY_REWRITE
594 "%s\n", ptr
->pattern
->name
);
598 up_read(&tomoyo_no_rewrite_list_lock
);
603 * tomoyo_update_file_acl - Update file's read/write/execute ACL.
605 * @filename: Filename.
606 * @perm: Permission (between 1 to 7).
607 * @domain: Pointer to "struct tomoyo_domain_info".
608 * @is_delete: True if it is a delete request.
610 * Returns 0 on success, negative value otherwise.
612 * This is legacy support interface for older policy syntax.
613 * Current policy syntax uses "allow_read/write" instead of "6",
614 * "allow_read" instead of "4", "allow_write" instead of "2",
615 * "allow_execute" instead of "1".
617 static int tomoyo_update_file_acl(const char *filename
, u8 perm
,
618 struct tomoyo_domain_info
* const domain
,
619 const bool is_delete
)
621 if (perm
> 7 || !perm
) {
622 printk(KERN_DEBUG
"%s: Invalid permission '%d %s'\n",
623 __func__
, perm
, filename
);
626 if (filename
[0] != '@' && tomoyo_strendswith(filename
, "/"))
628 * Only 'allow_mkdir' and 'allow_rmdir' are valid for
629 * directory permissions.
633 tomoyo_update_single_path_acl(TOMOYO_TYPE_READ_ACL
, filename
,
636 tomoyo_update_single_path_acl(TOMOYO_TYPE_WRITE_ACL
, filename
,
639 tomoyo_update_single_path_acl(TOMOYO_TYPE_EXECUTE_ACL
,
640 filename
, domain
, is_delete
);
645 * tomoyo_check_single_path_acl2 - Check permission for single path operation.
647 * @domain: Pointer to "struct tomoyo_domain_info".
648 * @filename: Filename to check.
650 * @may_use_pattern: True if patterned ACL is permitted.
652 * Returns 0 on success, -EPERM otherwise.
654 static int tomoyo_check_single_path_acl2(const struct tomoyo_domain_info
*
656 const struct tomoyo_path_info
*
659 const bool may_use_pattern
)
661 struct tomoyo_acl_info
*ptr
;
664 down_read(&tomoyo_domain_acl_info_list_lock
);
665 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
666 struct tomoyo_single_path_acl_record
*acl
;
667 if (tomoyo_acl_type2(ptr
) != TOMOYO_TYPE_SINGLE_PATH_ACL
)
669 acl
= container_of(ptr
, struct tomoyo_single_path_acl_record
,
671 if (!(acl
->perm
& perm
))
673 if (may_use_pattern
|| !acl
->filename
->is_patterned
) {
674 if (!tomoyo_path_matches_pattern(filename
,
683 up_read(&tomoyo_domain_acl_info_list_lock
);
688 * tomoyo_check_file_acl - Check permission for opening files.
690 * @domain: Pointer to "struct tomoyo_domain_info".
691 * @filename: Filename to check.
692 * @operation: Mode ("read" or "write" or "read/write" or "execute").
694 * Returns 0 on success, -EPERM otherwise.
696 static int tomoyo_check_file_acl(const struct tomoyo_domain_info
*domain
,
697 const struct tomoyo_path_info
*filename
,
702 if (!tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
))
705 perm
= 1 << TOMOYO_TYPE_READ_WRITE_ACL
;
706 else if (operation
== 4)
707 perm
= 1 << TOMOYO_TYPE_READ_ACL
;
708 else if (operation
== 2)
709 perm
= 1 << TOMOYO_TYPE_WRITE_ACL
;
710 else if (operation
== 1)
711 perm
= 1 << TOMOYO_TYPE_EXECUTE_ACL
;
714 return tomoyo_check_single_path_acl2(domain
, filename
, perm
,
719 * tomoyo_check_file_perm2 - Check permission for opening files.
721 * @domain: Pointer to "struct tomoyo_domain_info".
722 * @filename: Filename to check.
723 * @perm: Mode ("read" or "write" or "read/write" or "execute").
724 * @operation: Operation name passed used for verbose mode.
725 * @mode: Access control mode.
727 * Returns 0 on success, negative value otherwise.
729 static int tomoyo_check_file_perm2(struct tomoyo_domain_info
* const domain
,
730 const struct tomoyo_path_info
*filename
,
731 const u8 perm
, const char *operation
,
734 const bool is_enforce
= (mode
== 3);
735 const char *msg
= "<unknown>";
740 error
= tomoyo_check_file_acl(domain
, filename
, perm
);
741 if (error
&& perm
== 4 &&
742 (domain
->flags
& TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ
) == 0
743 && tomoyo_is_globally_readable_file(filename
))
746 msg
= tomoyo_sp2keyword(TOMOYO_TYPE_READ_WRITE_ACL
);
748 msg
= tomoyo_sp2keyword(TOMOYO_TYPE_READ_ACL
);
750 msg
= tomoyo_sp2keyword(TOMOYO_TYPE_WRITE_ACL
);
752 msg
= tomoyo_sp2keyword(TOMOYO_TYPE_EXECUTE_ACL
);
757 if (tomoyo_verbose_mode(domain
))
758 printk(KERN_WARNING
"TOMOYO-%s: Access '%s(%s) %s' denied "
759 "for %s\n", tomoyo_get_msg(is_enforce
), msg
, operation
,
760 filename
->name
, tomoyo_get_last_name(domain
));
763 if (mode
== 1 && tomoyo_domain_quota_is_ok(domain
)) {
764 /* Don't use patterns for execute permission. */
765 const struct tomoyo_path_info
*patterned_file
= (perm
!= 1) ?
766 tomoyo_get_file_pattern(filename
) : filename
;
767 tomoyo_update_file_acl(patterned_file
->name
, perm
,
774 * tomoyo_write_file_policy - Update file related list.
776 * @data: String to parse.
777 * @domain: Pointer to "struct tomoyo_domain_info".
778 * @is_delete: True if it is a delete request.
780 * Returns 0 on success, negative value otherwise.
782 int tomoyo_write_file_policy(char *data
, struct tomoyo_domain_info
*domain
,
783 const bool is_delete
)
785 char *filename
= strchr(data
, ' ');
793 if (sscanf(data
, "%u", &perm
) == 1)
794 return tomoyo_update_file_acl(filename
, (u8
) perm
, domain
,
796 if (strncmp(data
, "allow_", 6))
799 for (type
= 0; type
< TOMOYO_MAX_SINGLE_PATH_OPERATION
; type
++) {
800 if (strcmp(data
, tomoyo_sp_keyword
[type
]))
802 return tomoyo_update_single_path_acl(type
, filename
,
805 filename2
= strchr(filename
, ' ');
809 for (type
= 0; type
< TOMOYO_MAX_DOUBLE_PATH_OPERATION
; type
++) {
810 if (strcmp(data
, tomoyo_dp_keyword
[type
]))
812 return tomoyo_update_double_path_acl(type
, filename
, filename2
,
820 * tomoyo_update_single_path_acl - Update "struct tomoyo_single_path_acl_record" list.
822 * @type: Type of operation.
823 * @filename: Filename.
824 * @domain: Pointer to "struct tomoyo_domain_info".
825 * @is_delete: True if it is a delete request.
827 * Returns 0 on success, negative value otherwise.
829 static int tomoyo_update_single_path_acl(const u8 type
, const char *filename
,
830 struct tomoyo_domain_info
*
831 const domain
, const bool is_delete
)
833 static const u16 rw_mask
=
834 (1 << TOMOYO_TYPE_READ_ACL
) | (1 << TOMOYO_TYPE_WRITE_ACL
);
835 const struct tomoyo_path_info
*saved_filename
;
836 struct tomoyo_acl_info
*ptr
;
837 struct tomoyo_single_path_acl_record
*acl
;
839 const u16 perm
= 1 << type
;
843 if (!tomoyo_is_correct_path(filename
, 0, 0, 0, __func__
))
845 saved_filename
= tomoyo_save_name(filename
);
848 down_write(&tomoyo_domain_acl_info_list_lock
);
851 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
852 if (tomoyo_acl_type1(ptr
) != TOMOYO_TYPE_SINGLE_PATH_ACL
)
854 acl
= container_of(ptr
, struct tomoyo_single_path_acl_record
,
856 if (acl
->filename
!= saved_filename
)
858 /* Special case. Clear all bits if marked as deleted. */
859 if (ptr
->type
& TOMOYO_ACL_DELETED
)
862 if ((acl
->perm
& rw_mask
) == rw_mask
)
863 acl
->perm
|= 1 << TOMOYO_TYPE_READ_WRITE_ACL
;
864 else if (acl
->perm
& (1 << TOMOYO_TYPE_READ_WRITE_ACL
))
865 acl
->perm
|= rw_mask
;
866 ptr
->type
&= ~TOMOYO_ACL_DELETED
;
870 /* Not found. Append it to the tail. */
871 acl
= tomoyo_alloc_acl_element(TOMOYO_TYPE_SINGLE_PATH_ACL
);
875 if (perm
== (1 << TOMOYO_TYPE_READ_WRITE_ACL
))
876 acl
->perm
|= rw_mask
;
877 acl
->filename
= saved_filename
;
878 list_add_tail(&acl
->head
.list
, &domain
->acl_info_list
);
883 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
884 if (tomoyo_acl_type2(ptr
) != TOMOYO_TYPE_SINGLE_PATH_ACL
)
886 acl
= container_of(ptr
, struct tomoyo_single_path_acl_record
,
888 if (acl
->filename
!= saved_filename
)
891 if ((acl
->perm
& rw_mask
) != rw_mask
)
892 acl
->perm
&= ~(1 << TOMOYO_TYPE_READ_WRITE_ACL
);
893 else if (!(acl
->perm
& (1 << TOMOYO_TYPE_READ_WRITE_ACL
)))
894 acl
->perm
&= ~rw_mask
;
896 ptr
->type
|= TOMOYO_ACL_DELETED
;
901 up_write(&tomoyo_domain_acl_info_list_lock
);
906 * tomoyo_update_double_path_acl - Update "struct tomoyo_double_path_acl_record" list.
908 * @type: Type of operation.
909 * @filename1: First filename.
910 * @filename2: Second filename.
911 * @domain: Pointer to "struct tomoyo_domain_info".
912 * @is_delete: True if it is a delete request.
914 * Returns 0 on success, negative value otherwise.
916 static int tomoyo_update_double_path_acl(const u8 type
, const char *filename1
,
917 const char *filename2
,
918 struct tomoyo_domain_info
*
919 const domain
, const bool is_delete
)
921 const struct tomoyo_path_info
*saved_filename1
;
922 const struct tomoyo_path_info
*saved_filename2
;
923 struct tomoyo_acl_info
*ptr
;
924 struct tomoyo_double_path_acl_record
*acl
;
926 const u8 perm
= 1 << type
;
930 if (!tomoyo_is_correct_path(filename1
, 0, 0, 0, __func__
) ||
931 !tomoyo_is_correct_path(filename2
, 0, 0, 0, __func__
))
933 saved_filename1
= tomoyo_save_name(filename1
);
934 saved_filename2
= tomoyo_save_name(filename2
);
935 if (!saved_filename1
|| !saved_filename2
)
937 down_write(&tomoyo_domain_acl_info_list_lock
);
940 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
941 if (tomoyo_acl_type1(ptr
) != TOMOYO_TYPE_DOUBLE_PATH_ACL
)
943 acl
= container_of(ptr
, struct tomoyo_double_path_acl_record
,
945 if (acl
->filename1
!= saved_filename1
||
946 acl
->filename2
!= saved_filename2
)
948 /* Special case. Clear all bits if marked as deleted. */
949 if (ptr
->type
& TOMOYO_ACL_DELETED
)
952 ptr
->type
&= ~TOMOYO_ACL_DELETED
;
956 /* Not found. Append it to the tail. */
957 acl
= tomoyo_alloc_acl_element(TOMOYO_TYPE_DOUBLE_PATH_ACL
);
961 acl
->filename1
= saved_filename1
;
962 acl
->filename2
= saved_filename2
;
963 list_add_tail(&acl
->head
.list
, &domain
->acl_info_list
);
968 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
969 if (tomoyo_acl_type2(ptr
) != TOMOYO_TYPE_DOUBLE_PATH_ACL
)
971 acl
= container_of(ptr
, struct tomoyo_double_path_acl_record
,
973 if (acl
->filename1
!= saved_filename1
||
974 acl
->filename2
!= saved_filename2
)
978 ptr
->type
|= TOMOYO_ACL_DELETED
;
983 up_write(&tomoyo_domain_acl_info_list_lock
);
988 * tomoyo_check_single_path_acl - Check permission for single path operation.
990 * @domain: Pointer to "struct tomoyo_domain_info".
991 * @type: Type of operation.
992 * @filename: Filename to check.
994 * Returns 0 on success, negative value otherwise.
996 static int tomoyo_check_single_path_acl(struct tomoyo_domain_info
*domain
,
998 const struct tomoyo_path_info
*filename
)
1000 if (!tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
))
1002 return tomoyo_check_single_path_acl2(domain
, filename
, 1 << type
, 1);
1006 * tomoyo_check_double_path_acl - Check permission for double path operation.
1008 * @domain: Pointer to "struct tomoyo_domain_info".
1009 * @type: Type of operation.
1010 * @filename1: First filename to check.
1011 * @filename2: Second filename to check.
1013 * Returns 0 on success, -EPERM otherwise.
1015 static int tomoyo_check_double_path_acl(const struct tomoyo_domain_info
*domain
,
1017 const struct tomoyo_path_info
*
1019 const struct tomoyo_path_info
*
1022 struct tomoyo_acl_info
*ptr
;
1023 const u8 perm
= 1 << type
;
1026 if (!tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
))
1028 down_read(&tomoyo_domain_acl_info_list_lock
);
1029 list_for_each_entry(ptr
, &domain
->acl_info_list
, list
) {
1030 struct tomoyo_double_path_acl_record
*acl
;
1031 if (tomoyo_acl_type2(ptr
) != TOMOYO_TYPE_DOUBLE_PATH_ACL
)
1033 acl
= container_of(ptr
, struct tomoyo_double_path_acl_record
,
1035 if (!(acl
->perm
& perm
))
1037 if (!tomoyo_path_matches_pattern(filename1
, acl
->filename1
))
1039 if (!tomoyo_path_matches_pattern(filename2
, acl
->filename2
))
1044 up_read(&tomoyo_domain_acl_info_list_lock
);
1049 * tomoyo_check_single_path_permission2 - Check permission for single path operation.
1051 * @domain: Pointer to "struct tomoyo_domain_info".
1052 * @operation: Type of operation.
1053 * @filename: Filename to check.
1054 * @mode: Access control mode.
1056 * Returns 0 on success, negative value otherwise.
1058 static int tomoyo_check_single_path_permission2(struct tomoyo_domain_info
*
1059 const domain
, u8 operation
,
1060 const struct tomoyo_path_info
*
1061 filename
, const u8 mode
)
1065 const bool is_enforce
= (mode
== 3);
1070 error
= tomoyo_check_single_path_acl(domain
, operation
, filename
);
1071 msg
= tomoyo_sp2keyword(operation
);
1074 if (tomoyo_verbose_mode(domain
))
1075 printk(KERN_WARNING
"TOMOYO-%s: Access '%s %s' denied for %s\n",
1076 tomoyo_get_msg(is_enforce
), msg
, filename
->name
,
1077 tomoyo_get_last_name(domain
));
1078 if (mode
== 1 && tomoyo_domain_quota_is_ok(domain
)) {
1079 const char *name
= tomoyo_get_file_pattern(filename
)->name
;
1080 tomoyo_update_single_path_acl(operation
, name
, domain
, false);
1086 * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
1087 * we need to check "allow_rewrite" permission if the filename is
1088 * specified by "deny_rewrite" keyword.
1090 if (!error
&& operation
== TOMOYO_TYPE_TRUNCATE_ACL
&&
1091 tomoyo_is_no_rewrite_file(filename
)) {
1092 operation
= TOMOYO_TYPE_REWRITE_ACL
;
1099 * tomoyo_check_file_perm - Check permission for sysctl()'s "read" and "write".
1101 * @domain: Pointer to "struct tomoyo_domain_info".
1102 * @filename: Filename to check.
1103 * @perm: Mode ("read" or "write" or "read/write").
1104 * Returns 0 on success, negative value otherwise.
1106 int tomoyo_check_file_perm(struct tomoyo_domain_info
*domain
,
1107 const char *filename
, const u8 perm
)
1109 struct tomoyo_path_info name
;
1110 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1114 name
.name
= filename
;
1115 tomoyo_fill_path_info(&name
);
1116 return tomoyo_check_file_perm2(domain
, &name
, perm
, "sysctl", mode
);
1120 * tomoyo_check_exec_perm - Check permission for "execute".
1122 * @domain: Pointer to "struct tomoyo_domain_info".
1123 * @filename: Check permission for "execute".
1125 * Returns 0 on success, negativevalue otherwise.
1127 int tomoyo_check_exec_perm(struct tomoyo_domain_info
*domain
,
1128 const struct tomoyo_path_info
*filename
)
1130 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1134 return tomoyo_check_file_perm2(domain
, filename
, 1, "do_execve", mode
);
1138 * tomoyo_check_open_permission - Check permission for "read" and "write".
1140 * @domain: Pointer to "struct tomoyo_domain_info".
1141 * @path: Pointer to "struct path".
1142 * @flag: Flags for open().
1144 * Returns 0 on success, negative value otherwise.
1146 int tomoyo_check_open_permission(struct tomoyo_domain_info
*domain
,
1147 struct path
*path
, const int flag
)
1149 const u8 acc_mode
= ACC_MODE(flag
);
1150 int error
= -ENOMEM
;
1151 struct tomoyo_path_info
*buf
;
1152 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1153 const bool is_enforce
= (mode
== 3);
1155 if (!mode
|| !path
->mnt
)
1159 if (path
->dentry
->d_inode
&& S_ISDIR(path
->dentry
->d_inode
->i_mode
))
1161 * I don't check directories here because mkdir() and rmdir()
1165 buf
= tomoyo_get_path(path
);
1170 * If the filename is specified by "deny_rewrite" keyword,
1171 * we need to check "allow_rewrite" permission when the filename is not
1172 * opened for append mode or the filename is truncated at open time.
1174 if ((acc_mode
& MAY_WRITE
) &&
1175 ((flag
& O_TRUNC
) || !(flag
& O_APPEND
)) &&
1176 (tomoyo_is_no_rewrite_file(buf
))) {
1177 error
= tomoyo_check_single_path_permission2(domain
,
1178 TOMOYO_TYPE_REWRITE_ACL
,
1182 error
= tomoyo_check_file_perm2(domain
, buf
, acc_mode
, "open",
1184 if (!error
&& (flag
& O_TRUNC
))
1185 error
= tomoyo_check_single_path_permission2(domain
,
1186 TOMOYO_TYPE_TRUNCATE_ACL
,
1196 * tomoyo_check_1path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate" and "symlink".
1198 * @domain: Pointer to "struct tomoyo_domain_info".
1199 * @operation: Type of operation.
1200 * @path: Pointer to "struct path".
1202 * Returns 0 on success, negative value otherwise.
1204 int tomoyo_check_1path_perm(struct tomoyo_domain_info
*domain
,
1205 const u8 operation
, struct path
*path
)
1207 int error
= -ENOMEM
;
1208 struct tomoyo_path_info
*buf
;
1209 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1210 const bool is_enforce
= (mode
== 3);
1212 if (!mode
|| !path
->mnt
)
1214 buf
= tomoyo_get_path(path
);
1217 switch (operation
) {
1218 case TOMOYO_TYPE_MKDIR_ACL
:
1219 case TOMOYO_TYPE_RMDIR_ACL
:
1222 * tomoyo_get_path() reserves space for appending "/."
1224 strcat((char *) buf
->name
, "/");
1225 tomoyo_fill_path_info(buf
);
1228 error
= tomoyo_check_single_path_permission2(domain
, operation
, buf
,
1238 * tomoyo_check_rewrite_permission - Check permission for "rewrite".
1240 * @domain: Pointer to "struct tomoyo_domain_info".
1241 * @filp: Pointer to "struct file".
1243 * Returns 0 on success, negative value otherwise.
1245 int tomoyo_check_rewrite_permission(struct tomoyo_domain_info
*domain
,
1248 int error
= -ENOMEM
;
1249 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1250 const bool is_enforce
= (mode
== 3);
1251 struct tomoyo_path_info
*buf
;
1253 if (!mode
|| !filp
->f_path
.mnt
)
1255 buf
= tomoyo_get_path(&filp
->f_path
);
1258 if (!tomoyo_is_no_rewrite_file(buf
)) {
1262 error
= tomoyo_check_single_path_permission2(domain
,
1263 TOMOYO_TYPE_REWRITE_ACL
,
1273 * tomoyo_check_2path_perm - Check permission for "rename" and "link".
1275 * @domain: Pointer to "struct tomoyo_domain_info".
1276 * @operation: Type of operation.
1277 * @path1: Pointer to "struct path".
1278 * @path2: Pointer to "struct path".
1280 * Returns 0 on success, negative value otherwise.
1282 int tomoyo_check_2path_perm(struct tomoyo_domain_info
* const domain
,
1283 const u8 operation
, struct path
*path1
,
1286 int error
= -ENOMEM
;
1287 struct tomoyo_path_info
*buf1
, *buf2
;
1288 const u8 mode
= tomoyo_check_flags(domain
, TOMOYO_MAC_FOR_FILE
);
1289 const bool is_enforce
= (mode
== 3);
1292 if (!mode
|| !path1
->mnt
|| !path2
->mnt
)
1294 buf1
= tomoyo_get_path(path1
);
1295 buf2
= tomoyo_get_path(path2
);
1299 struct dentry
*dentry
= path1
->dentry
;
1300 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
1302 * tomoyo_get_path() reserves space for appending "/."
1304 if (!buf1
->is_dir
) {
1305 strcat((char *) buf1
->name
, "/");
1306 tomoyo_fill_path_info(buf1
);
1308 if (!buf2
->is_dir
) {
1309 strcat((char *) buf2
->name
, "/");
1310 tomoyo_fill_path_info(buf2
);
1314 error
= tomoyo_check_double_path_acl(domain
, operation
, buf1
, buf2
);
1315 msg
= tomoyo_dp2keyword(operation
);
1318 if (tomoyo_verbose_mode(domain
))
1319 printk(KERN_WARNING
"TOMOYO-%s: Access '%s %s %s' "
1320 "denied for %s\n", tomoyo_get_msg(is_enforce
),
1321 msg
, buf1
->name
, buf2
->name
,
1322 tomoyo_get_last_name(domain
));
1323 if (mode
== 1 && tomoyo_domain_quota_is_ok(domain
)) {
1324 const char *name1
= tomoyo_get_file_pattern(buf1
)->name
;
1325 const char *name2
= tomoyo_get_file_pattern(buf2
)->name
;
1326 tomoyo_update_double_path_acl(operation
, name1
, name2
, domain
,