Skip to content

Commit 2106ccd

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Add mount restriction.
mount(2) has three string and one numeric parameters. Split mount restriction code from security/tomoyo/file.c . Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent a1f9bb6 commit 2106ccd

7 files changed

Lines changed: 469 additions & 11 deletions

File tree

‎security/tomoyo/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
obj-y = common.o realpath.o tomoyo.o domain.o file.o gc.o path_group.o number_group.o
1+
obj-y = common.o realpath.o tomoyo.o domain.o file.o gc.o path_group.o number_group.o mount.o

‎security/tomoyo/common.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,10 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
10751075
if (perm & (1 << i))
10761076
count++;
10771077
break;
1078+
case TOMOYO_TYPE_MOUNT_ACL:
1079+
if (!container_of(ptr, struct tomoyo_mount_acl, head)->
1080+
is_deleted)
1081+
count++;
10781082
}
10791083
}
10801084
if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
@@ -1576,6 +1580,8 @@ static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
15761580
domain->ignore_global_allow_read = !is_delete;
15771581
return 0;
15781582
}
1583+
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
1584+
return tomoyo_write_mount_policy(data, domain, is_delete);
15791585
return tomoyo_write_file_policy(data, domain, is_delete);
15801586
}
15811587

@@ -1720,6 +1726,30 @@ static bool tomoyo_print_path_number3_acl(struct tomoyo_io_buffer *head,
17201726
return false;
17211727
}
17221728

1729+
/**
1730+
* tomoyo_print_mount_acl - Print a mount ACL entry.
1731+
*
1732+
* @head: Pointer to "struct tomoyo_io_buffer".
1733+
* @ptr: Pointer to "struct tomoyo_mount_acl".
1734+
*
1735+
* Returns true on success, false otherwise.
1736+
*/
1737+
static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
1738+
struct tomoyo_mount_acl *ptr)
1739+
{
1740+
const int pos = head->read_avail;
1741+
if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
1742+
!tomoyo_print_name_union(head, &ptr->dev_name) ||
1743+
!tomoyo_print_name_union(head, &ptr->dir_name) ||
1744+
!tomoyo_print_name_union(head, &ptr->fs_type) ||
1745+
!tomoyo_print_number_union(head, &ptr->flags) ||
1746+
!tomoyo_io_printf(head, "\n")) {
1747+
head->read_avail = pos;
1748+
return false;
1749+
}
1750+
return true;
1751+
}
1752+
17231753
/**
17241754
* tomoyo_print_entry - Print an ACL entry.
17251755
*
@@ -1755,6 +1785,11 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
17551785
head);
17561786
return tomoyo_print_path_number3_acl(head, acl);
17571787
}
1788+
if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
1789+
struct tomoyo_mount_acl *acl
1790+
= container_of(ptr, struct tomoyo_mount_acl, head);
1791+
return tomoyo_print_mount_acl(head, acl);
1792+
}
17581793
BUG(); /* This must not happen. */
17591794
return false;
17601795
}

‎security/tomoyo/common.h‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum tomoyo_mode_index {
5353

5454
/* Keywords for ACLs. */
5555
#define TOMOYO_KEYWORD_ALIAS "alias "
56+
#define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
5657
#define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
5758
#define TOMOYO_KEYWORD_DELETE "delete "
5859
#define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
@@ -90,6 +91,7 @@ enum tomoyo_acl_entry_type_index {
9091
TOMOYO_TYPE_PATH2_ACL,
9192
TOMOYO_TYPE_PATH_NUMBER_ACL,
9293
TOMOYO_TYPE_PATH_NUMBER3_ACL,
94+
TOMOYO_TYPE_MOUNT_ACL,
9395
};
9496

9597
/* Index numbers for File Controls. */
@@ -116,7 +118,6 @@ enum tomoyo_path_acl_index {
116118
TOMOYO_TYPE_SYMLINK,
117119
TOMOYO_TYPE_REWRITE,
118120
TOMOYO_TYPE_CHROOT,
119-
TOMOYO_TYPE_MOUNT,
120121
TOMOYO_TYPE_UMOUNT,
121122
TOMOYO_MAX_PATH_OPERATION
122123
};
@@ -360,8 +361,8 @@ struct tomoyo_domain_info {
360361
*
361362
* Directives held by this structure are "allow_read/write", "allow_execute",
362363
* "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
363-
* "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot",
364-
* "allow_mount" and "allow_unmount".
364+
* "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
365+
* "allow_unmount".
365366
*/
366367
struct tomoyo_path_acl {
367368
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
@@ -434,6 +435,29 @@ struct tomoyo_path2_acl {
434435
struct tomoyo_name_union name2;
435436
};
436437

438+
/*
439+
* tomoyo_mount_acl is a structure which is used for holding an
440+
* entry for mount operation.
441+
* It has following fields.
442+
*
443+
* (1) "head" which is a "struct tomoyo_acl_info".
444+
* (2) "is_deleted" is boolean.
445+
* (3) "dev_name" is the device name.
446+
* (4) "dir_name" is the mount point.
447+
* (5) "flags" is the mount flags.
448+
*
449+
* Directives held by this structure are "allow_rename", "allow_link" and
450+
* "allow_pivot_root".
451+
*/
452+
struct tomoyo_mount_acl {
453+
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
454+
bool is_deleted;
455+
struct tomoyo_name_union dev_name;
456+
struct tomoyo_name_union dir_name;
457+
struct tomoyo_name_union fs_type;
458+
struct tomoyo_number_union flags;
459+
};
460+
437461
/*
438462
* tomoyo_io_buffer is a structure which is used for reading and modifying
439463
* configuration via /sys/kernel/security/tomoyo/ interface.
@@ -638,6 +662,9 @@ struct tomoyo_policy_manager_entry {
638662
/* Check whether the given name matches the given name_union. */
639663
bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
640664
const struct tomoyo_name_union *ptr);
665+
/* Check whether the given number matches the given number_union. */
666+
bool tomoyo_compare_number_union(const unsigned long value,
667+
const struct tomoyo_number_union *ptr);
641668
/* Check whether the domain has too many ACL entries to hold. */
642669
bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
643670
/* Transactional sprintf() for policy dump. */
@@ -699,6 +726,12 @@ const char *tomoyo_path_number32keyword(const u8 operation);
699726
const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
700727
/* Convert single path operation to operation name. */
701728
const char *tomoyo_path2keyword(const u8 operation);
729+
/* Fill "struct tomoyo_request_info". */
730+
int tomoyo_init_request_info(struct tomoyo_request_info *r,
731+
struct tomoyo_domain_info *domain);
732+
/* Check permission for mount operation. */
733+
int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
734+
unsigned long flags, void *data_page);
702735
/* Create "alias" entry in exception policy. */
703736
int tomoyo_write_alias_policy(char *data, const bool is_delete);
704737
/*
@@ -721,6 +754,9 @@ int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
721754
const bool is_delete);
722755
/* Create "allow_read" entry in exception policy. */
723756
int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
757+
/* Create "allow_mount" entry in domain policy. */
758+
int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
759+
const bool is_delete);
724760
/* Create "deny_rewrite" entry in exception policy. */
725761
int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
726762
/* Create "file_pattern" entry in exception policy. */
@@ -735,7 +771,9 @@ struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
735771
struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
736772
domainname,
737773
const u8 profile);
738-
774+
/* Get patterned pathname. */
775+
const struct tomoyo_path_info *
776+
tomoyo_get_file_pattern(const struct tomoyo_path_info *filename);
739777
/* Allocate memory for "struct tomoyo_path_group". */
740778
struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
741779
struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
@@ -972,6 +1010,16 @@ static inline bool tomoyo_is_same_path_number_acl
9721010
&& tomoyo_is_same_number_union(&p1->number, &p2->number);
9731011
}
9741012

1013+
static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
1014+
const struct tomoyo_mount_acl *p2)
1015+
{
1016+
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1017+
tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
1018+
tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
1019+
tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
1020+
tomoyo_is_same_number_union(&p1->flags, &p2->flags);
1021+
}
1022+
9751023
static inline bool tomoyo_is_same_domain_initializer_entry
9761024
(const struct tomoyo_domain_initializer_entry *p1,
9771025
const struct tomoyo_domain_initializer_entry *p2)

‎security/tomoyo/file.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ static const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
2424
[TOMOYO_TYPE_SYMLINK] = "symlink",
2525
[TOMOYO_TYPE_REWRITE] = "rewrite",
2626
[TOMOYO_TYPE_CHROOT] = "chroot",
27-
[TOMOYO_TYPE_MOUNT] = "mount",
2827
[TOMOYO_TYPE_UMOUNT] = "unmount",
2928
};
3029

@@ -108,8 +107,8 @@ bool tomoyo_compare_number_union(const unsigned long value,
108107
*
109108
* Returns mode.
110109
*/
111-
static int tomoyo_init_request_info(struct tomoyo_request_info *r,
112-
struct tomoyo_domain_info *domain)
110+
int tomoyo_init_request_info(struct tomoyo_request_info *r,
111+
struct tomoyo_domain_info *domain)
113112
{
114113
memset(r, 0, sizeof(*r));
115114
if (!domain)
@@ -487,7 +486,7 @@ static int tomoyo_update_file_pattern_entry(const char *pattern,
487486
*
488487
* Caller holds tomoyo_read_lock().
489488
*/
490-
static const struct tomoyo_path_info *
489+
const struct tomoyo_path_info *
491490
tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
492491
{
493492
struct tomoyo_pattern_entry *ptr;
@@ -1418,7 +1417,7 @@ int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
14181417
}
14191418

14201419
/**
1421-
* tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot", "mount" and "unmount".
1420+
* tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
14221421
*
14231422
* @operation: Type of operation.
14241423
* @path: Pointer to "struct path".

‎security/tomoyo/gc.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ static void tomoyo_del_acl(struct tomoyo_acl_info *acl)
124124
tomoyo_put_number_union(&entry->minor);
125125
}
126126
break;
127+
case TOMOYO_TYPE_MOUNT_ACL:
128+
{
129+
struct tomoyo_mount_acl *entry
130+
= container_of(acl, typeof(*entry), head);
131+
tomoyo_put_name_union(&entry->dev_name);
132+
tomoyo_put_name_union(&entry->dir_name);
133+
tomoyo_put_name_union(&entry->fs_type);
134+
tomoyo_put_number_union(&entry->flags);
135+
}
136+
break;
127137
default:
128138
printk(KERN_WARNING "Unknown type\n");
129139
break;

0 commit comments

Comments
 (0)