Skip to content

Commit 7c2ea22

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Merge path_group and number_group.
Use common code for "path_group" and "number_group". Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 31845e8 commit 7c2ea22

9 files changed

Lines changed: 199 additions & 282 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 domain.o file.o gc.o load_policy.o memory.o mount.o number_group.o path_group.o realpath.o securityfs_if.o tomoyo.o util.o
1+
obj-y = common.o domain.o file.o gc.o group.o load_policy.o memory.o mount.o realpath.o securityfs_if.o tomoyo.o util.o

‎security/tomoyo/common.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,9 @@ static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
11861186
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
11871187
return tomoyo_write_no_rewrite_policy(data, is_delete);
11881188
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
1189-
return tomoyo_write_path_group_policy(data, is_delete);
1189+
return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
11901190
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
1191-
return tomoyo_write_number_group_policy(data, is_delete);
1191+
return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
11921192
return -EINVAL;
11931193
}
11941194

‎security/tomoyo/common.h‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,22 +838,21 @@ int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
838838
int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
839839
/* Create "file_pattern" entry in exception policy. */
840840
int tomoyo_write_pattern_policy(char *data, const bool is_delete);
841-
/* Create "path_group" entry in exception policy. */
842-
int tomoyo_write_path_group_policy(char *data, const bool is_delete);
841+
/* Create "path_group"/"number_group" entry in exception policy. */
842+
int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
843843
int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
844844
__attribute__ ((format(printf, 2, 3)));
845-
/* Create "number_group" entry in exception policy. */
846-
int tomoyo_write_number_group_policy(char *data, const bool is_delete);
847845
/* Find a domain by the given name. */
848846
struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
849847
/* Find or create a domain by the given name. */
850848
struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
851849
domainname,
852850
const u8 profile);
853851
struct tomoyo_profile *tomoyo_profile(const u8 profile);
854-
/* Allocate memory for "struct tomoyo_path_group". */
855-
struct tomoyo_group *tomoyo_get_path_group(const char *group_name);
856-
struct tomoyo_group *tomoyo_get_number_group(const char *group_name);
852+
/*
853+
* Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
854+
*/
855+
struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
857856

858857
/* Check mode for specified functionality. */
859858
unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,

‎security/tomoyo/gc.c‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,33 +266,25 @@ static void tomoyo_collect_entry(void)
266266
goto unlock;
267267
}
268268
}
269-
{
269+
for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
270+
struct list_head *list = &tomoyo_group_list[i];
271+
int id;
270272
struct tomoyo_group *group;
271-
list_for_each_entry_rcu(group,
272-
&tomoyo_group_list[TOMOYO_PATH_GROUP],
273-
list) {
274-
tomoyo_collect_member(&group->member_list,
275-
TOMOYO_ID_PATH_GROUP);
276-
if (!list_empty(&group->member_list) ||
277-
atomic_read(&group->users))
278-
continue;
279-
if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
280-
&group->list))
281-
goto unlock;
273+
switch (i) {
274+
case 0:
275+
id = TOMOYO_ID_PATH_GROUP;
276+
break;
277+
default:
278+
id = TOMOYO_ID_NUMBER_GROUP;
279+
break;
282280
}
283-
}
284-
{
285-
struct tomoyo_group *group;
286-
list_for_each_entry_rcu(group,
287-
&tomoyo_group_list[TOMOYO_NUMBER_GROUP],
288-
list) {
289-
tomoyo_collect_member(&group->member_list,
290-
TOMOYO_ID_NUMBER_GROUP);
281+
list_for_each_entry(group, list, list) {
282+
if (!tomoyo_collect_member(&group->member_list, id))
283+
goto unlock;
291284
if (!list_empty(&group->member_list) ||
292285
atomic_read(&group->users))
293286
continue;
294-
if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
295-
&group->list))
287+
if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, &group->list))
296288
goto unlock;
297289
}
298290
}

‎security/tomoyo/group.c‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* security/tomoyo/group.c
3+
*
4+
* Copyright (C) 2005-2010 NTT DATA CORPORATION
5+
*/
6+
7+
#include <linux/slab.h>
8+
#include "common.h"
9+
10+
static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
11+
const struct tomoyo_acl_head *b)
12+
{
13+
return container_of(a, struct tomoyo_path_group, head)->member_name ==
14+
container_of(b, struct tomoyo_path_group, head)->member_name;
15+
}
16+
17+
static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
18+
const struct tomoyo_acl_head *b)
19+
{
20+
return !memcmp(&container_of(a, struct tomoyo_number_group, head)
21+
->number,
22+
&container_of(b, struct tomoyo_number_group, head)
23+
->number,
24+
sizeof(container_of(a, struct tomoyo_number_group, head)
25+
->number));
26+
}
27+
28+
/**
29+
* tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
30+
*
31+
* @data: String to parse.
32+
* @is_delete: True if it is a delete request.
33+
* @type: Type of this group.
34+
*
35+
* Returns 0 on success, negative value otherwise.
36+
*/
37+
int tomoyo_write_group(char *data, const bool is_delete, const u8 type)
38+
{
39+
struct tomoyo_group *group;
40+
struct list_head *member;
41+
char *w[2];
42+
int error = -EINVAL;
43+
if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
44+
return -EINVAL;
45+
group = tomoyo_get_group(w[0], type);
46+
if (!group)
47+
return -ENOMEM;
48+
member = &group->member_list;
49+
if (type == TOMOYO_PATH_GROUP) {
50+
struct tomoyo_path_group e = { };
51+
e.member_name = tomoyo_get_name(w[1]);
52+
if (!e.member_name) {
53+
error = -ENOMEM;
54+
goto out;
55+
}
56+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
57+
member, tomoyo_same_path_group);
58+
tomoyo_put_name(e.member_name);
59+
} else if (type == TOMOYO_NUMBER_GROUP) {
60+
struct tomoyo_number_group e = { };
61+
if (w[1][0] == '@'
62+
|| !tomoyo_parse_number_union(w[1], &e.number)
63+
|| e.number.values[0] > e.number.values[1])
64+
goto out;
65+
error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
66+
member, tomoyo_same_number_group);
67+
/*
68+
* tomoyo_put_number_union() is not needed because
69+
* w[1][0] != '@'.
70+
*/
71+
}
72+
out:
73+
tomoyo_put_group(group);
74+
return error;
75+
}
76+
77+
/**
78+
* tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
79+
*
80+
* @pathname: The name of pathname.
81+
* @group: Pointer to "struct tomoyo_path_group".
82+
*
83+
* Returns true if @pathname matches pathnames in @group, false otherwise.
84+
*
85+
* Caller holds tomoyo_read_lock().
86+
*/
87+
bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
88+
const struct tomoyo_group *group)
89+
{
90+
struct tomoyo_path_group *member;
91+
bool matched = false;
92+
list_for_each_entry_rcu(member, &group->member_list, head.list) {
93+
if (member->head.is_deleted)
94+
continue;
95+
if (!tomoyo_path_matches_pattern(pathname, member->member_name))
96+
continue;
97+
matched = true;
98+
break;
99+
}
100+
return matched;
101+
}
102+
103+
/**
104+
* tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
105+
*
106+
* @min: Min number.
107+
* @max: Max number.
108+
* @group: Pointer to "struct tomoyo_number_group".
109+
*
110+
* Returns true if @min and @max partially overlaps @group, false otherwise.
111+
*
112+
* Caller holds tomoyo_read_lock().
113+
*/
114+
bool tomoyo_number_matches_group(const unsigned long min,
115+
const unsigned long max,
116+
const struct tomoyo_group *group)
117+
{
118+
struct tomoyo_number_group *member;
119+
bool matched = false;
120+
list_for_each_entry_rcu(member, &group->member_list, head.list) {
121+
if (member->head.is_deleted)
122+
continue;
123+
if (min > member->number.values[1] ||
124+
max < member->number.values[0])
125+
continue;
126+
matched = true;
127+
break;
128+
}
129+
return matched;
130+
}

‎security/tomoyo/memory.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,50 @@ void tomoyo_memory_free(void *ptr)
8989
kfree(ptr);
9090
}
9191

92+
/**
93+
* tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
94+
*
95+
* @group_name: The name of address group.
96+
* @idx: Index number.
97+
*
98+
* Returns pointer to "struct tomoyo_group" on success, NULL otherwise.
99+
*/
100+
struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 idx)
101+
{
102+
struct tomoyo_group e = { };
103+
struct tomoyo_group *group = NULL;
104+
bool found = false;
105+
if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
106+
return NULL;
107+
e.group_name = tomoyo_get_name(group_name);
108+
if (!e.group_name)
109+
return NULL;
110+
if (mutex_lock_interruptible(&tomoyo_policy_lock))
111+
goto out;
112+
list_for_each_entry(group, &tomoyo_group_list[idx], list) {
113+
if (e.group_name != group->group_name)
114+
continue;
115+
atomic_inc(&group->users);
116+
found = true;
117+
break;
118+
}
119+
if (!found) {
120+
struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
121+
if (entry) {
122+
INIT_LIST_HEAD(&entry->member_list);
123+
atomic_set(&entry->users, 1);
124+
list_add_tail_rcu(&entry->list,
125+
&tomoyo_group_list[idx]);
126+
group = entry;
127+
found = true;
128+
}
129+
}
130+
mutex_unlock(&tomoyo_policy_lock);
131+
out:
132+
tomoyo_put_name(e.group_name);
133+
return found ? group : NULL;
134+
}
135+
92136
/*
93137
* tomoyo_name_list is used for holding string data used by TOMOYO.
94138
* Since same string data is likely used for multiple times (e.g.

0 commit comments

Comments
 (0)