Skip to content

Commit 237ab45

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Use callback for updating entries.
Use common "struct list_head" + "bool" + "u8" structure and use common code for elements using that structure. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 927942a commit 237ab45

7 files changed

Lines changed: 253 additions & 312 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,6 @@ static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
950950
struct tomoyo_mount_acl *ptr)
951951
{
952952
const int pos = head->read_avail;
953-
if (ptr->is_deleted)
954-
return true;
955953
if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
956954
!tomoyo_print_name_union(head, &ptr->dev_name) ||
957955
!tomoyo_print_name_union(head, &ptr->dir_name) ||
@@ -977,6 +975,8 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
977975
{
978976
const u8 acl_type = ptr->type;
979977

978+
if (ptr->is_deleted)
979+
return true;
980980
if (acl_type == TOMOYO_TYPE_PATH_ACL) {
981981
struct tomoyo_path_acl *acl
982982
= container_of(ptr, struct tomoyo_path_acl, head);

‎security/tomoyo/common.h‎

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ enum tomoyo_path_acl_index {
112112
TOMOYO_MAX_PATH_OPERATION
113113
};
114114

115+
#define TOMOYO_RW_MASK ((1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE))
116+
115117
enum tomoyo_path_number3_acl_index {
116118
TOMOYO_TYPE_MKBLOCK,
117119
TOMOYO_TYPE_MKCHAR,
@@ -289,17 +291,19 @@ struct tomoyo_number_group_member {
289291
*
290292
* (1) "list" which is linked to the ->acl_info_list of
291293
* "struct tomoyo_domain_info"
292-
* (2) "type" which tells type of the entry (either
293-
* "struct tomoyo_path_acl" or "struct tomoyo_path2_acl").
294+
* (2) "is_deleted" is a bool which is true if this domain is marked as
295+
* "deleted", false otherwise.
296+
* (3) "type" which tells type of the entry.
294297
*
295298
* Packing "struct tomoyo_acl_info" allows
296-
* "struct tomoyo_path_acl" to embed "u8" + "u16" and
297-
* "struct tomoyo_path2_acl" to embed "u8"
298-
* without enlarging their structure size.
299+
* "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
300+
* "struct tomoyo_path_number_acl" "struct tomoyo_path_number3_acl" to embed
301+
* "u8" without enlarging their structure size.
299302
*/
300303
struct tomoyo_acl_info {
301304
struct list_head list;
302-
u8 type;
305+
bool is_deleted;
306+
u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
303307
} __packed;
304308

305309
/*
@@ -438,17 +442,15 @@ struct tomoyo_path2_acl {
438442
* It has following fields.
439443
*
440444
* (1) "head" which is a "struct tomoyo_acl_info".
441-
* (2) "is_deleted" is boolean.
442-
* (3) "dev_name" is the device name.
443-
* (4) "dir_name" is the mount point.
445+
* (2) "dev_name" is the device name.
446+
* (3) "dir_name" is the mount point.
447+
* (4) "fs_type" is the filesystem type.
444448
* (5) "flags" is the mount flags.
445449
*
446-
* Directives held by this structure are "allow_rename", "allow_link" and
447-
* "allow_pivot_root".
450+
* Directive held by this structure is "allow_mount".
448451
*/
449452
struct tomoyo_mount_acl {
450453
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
451-
bool is_deleted;
452454
struct tomoyo_name_union dev_name;
453455
struct tomoyo_name_union dir_name;
454456
struct tomoyo_name_union fs_type;
@@ -914,6 +916,16 @@ void tomoyo_run_gc(void);
914916

915917
void tomoyo_memory_free(void *ptr);
916918

919+
int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
920+
bool is_delete, struct tomoyo_domain_info *domain,
921+
bool (*check_duplicate) (const struct tomoyo_acl_info
922+
*,
923+
const struct tomoyo_acl_info
924+
*),
925+
bool (*merge_duplicate) (struct tomoyo_acl_info *,
926+
struct tomoyo_acl_info *,
927+
const bool));
928+
917929
/********** External variable definitions. **********/
918930

919931
/* Lock for GC. */
@@ -1042,52 +1054,6 @@ static inline bool tomoyo_is_same_number_union
10421054
p1->max_type == p2->max_type && p1->is_group == p2->is_group;
10431055
}
10441056

1045-
static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
1046-
const struct tomoyo_path_acl *p2)
1047-
{
1048-
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1049-
tomoyo_is_same_name_union(&p1->name, &p2->name);
1050-
}
1051-
1052-
static inline bool tomoyo_is_same_path_number3_acl
1053-
(const struct tomoyo_path_number3_acl *p1,
1054-
const struct tomoyo_path_number3_acl *p2)
1055-
{
1056-
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1057-
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
1058-
&& tomoyo_is_same_number_union(&p1->mode, &p2->mode)
1059-
&& tomoyo_is_same_number_union(&p1->major, &p2->major)
1060-
&& tomoyo_is_same_number_union(&p1->minor, &p2->minor);
1061-
}
1062-
1063-
1064-
static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
1065-
const struct tomoyo_path2_acl *p2)
1066-
{
1067-
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1068-
tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
1069-
tomoyo_is_same_name_union(&p1->name2, &p2->name2);
1070-
}
1071-
1072-
static inline bool tomoyo_is_same_path_number_acl
1073-
(const struct tomoyo_path_number_acl *p1,
1074-
const struct tomoyo_path_number_acl *p2)
1075-
{
1076-
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
1077-
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
1078-
&& tomoyo_is_same_number_union(&p1->number, &p2->number);
1079-
}
1080-
1081-
static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
1082-
const struct tomoyo_mount_acl *p2)
1083-
{
1084-
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
1085-
tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
1086-
tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
1087-
tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
1088-
tomoyo_is_same_number_union(&p1->flags, &p2->flags);
1089-
}
1090-
10911057
static inline bool tomoyo_is_same_domain_initializer_entry
10921058
(const struct tomoyo_domain_initializer_entry *p1,
10931059
const struct tomoyo_domain_initializer_entry *p2)

‎security/tomoyo/domain.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,57 @@
1515
/* The initial domain. */
1616
struct tomoyo_domain_info tomoyo_kernel_domain;
1717

18+
/**
19+
* tomoyo_update_domain - Update an entry for domain policy.
20+
*
21+
* @new_entry: Pointer to "struct tomoyo_acl_info".
22+
* @size: Size of @new_entry in bytes.
23+
* @is_delete: True if it is a delete request.
24+
* @domain: Pointer to "struct tomoyo_domain_info".
25+
* @check_duplicate: Callback function to find duplicated entry.
26+
* @merge_duplicate: Callback function to merge duplicated entry.
27+
*
28+
* Returns 0 on success, negative value otherwise.
29+
*
30+
* Caller holds tomoyo_read_lock().
31+
*/
32+
int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
33+
bool is_delete, struct tomoyo_domain_info *domain,
34+
bool (*check_duplicate) (const struct tomoyo_acl_info
35+
*,
36+
const struct tomoyo_acl_info
37+
*),
38+
bool (*merge_duplicate) (struct tomoyo_acl_info *,
39+
struct tomoyo_acl_info *,
40+
const bool))
41+
{
42+
int error = is_delete ? -ENOENT : -ENOMEM;
43+
struct tomoyo_acl_info *entry;
44+
45+
if (mutex_lock_interruptible(&tomoyo_policy_lock))
46+
return error;
47+
list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
48+
if (!check_duplicate(entry, new_entry))
49+
continue;
50+
if (merge_duplicate)
51+
entry->is_deleted = merge_duplicate(entry, new_entry,
52+
is_delete);
53+
else
54+
entry->is_deleted = is_delete;
55+
error = 0;
56+
break;
57+
}
58+
if (error && !is_delete) {
59+
entry = tomoyo_commit_ok(new_entry, size);
60+
if (entry) {
61+
list_add_tail_rcu(&entry->list, &domain->acl_info_list);
62+
error = 0;
63+
}
64+
}
65+
mutex_unlock(&tomoyo_policy_lock);
66+
return error;
67+
}
68+
1869
/*
1970
* tomoyo_domain_list is used for holding list of domains.
2071
* The ->acl_info_list of "struct tomoyo_domain_info" is used for holding

0 commit comments

Comments
 (0)