]>
Commit | Line | Data |
---|---|---|
7c2ea22e TH |
1 | /* |
2 | * security/tomoyo/group.c | |
3 | * | |
0f2a55d5 | 4 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
7c2ea22e TH |
5 | */ |
6 | ||
7 | #include <linux/slab.h> | |
b2d09103 IM |
8 | #include <linux/rculist.h> |
9 | ||
7c2ea22e TH |
10 | #include "common.h" |
11 | ||
0f2a55d5 TH |
12 | /** |
13 | * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry. | |
14 | * | |
15 | * @a: Pointer to "struct tomoyo_acl_head". | |
16 | * @b: Pointer to "struct tomoyo_acl_head". | |
17 | * | |
18 | * Returns true if @a == @b, false otherwise. | |
19 | */ | |
7c2ea22e | 20 | static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a, |
0f2a55d5 | 21 | const struct tomoyo_acl_head *b) |
7c2ea22e TH |
22 | { |
23 | return container_of(a, struct tomoyo_path_group, head)->member_name == | |
24 | container_of(b, struct tomoyo_path_group, head)->member_name; | |
25 | } | |
26 | ||
0f2a55d5 TH |
27 | /** |
28 | * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry. | |
29 | * | |
30 | * @a: Pointer to "struct tomoyo_acl_head". | |
31 | * @b: Pointer to "struct tomoyo_acl_head". | |
32 | * | |
33 | * Returns true if @a == @b, false otherwise. | |
34 | */ | |
7c2ea22e | 35 | static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a, |
0f2a55d5 | 36 | const struct tomoyo_acl_head *b) |
7c2ea22e TH |
37 | { |
38 | return !memcmp(&container_of(a, struct tomoyo_number_group, head) | |
39 | ->number, | |
40 | &container_of(b, struct tomoyo_number_group, head) | |
41 | ->number, | |
42 | sizeof(container_of(a, struct tomoyo_number_group, head) | |
43 | ->number)); | |
44 | } | |
45 | ||
46 | /** | |
059d84db TH |
47 | * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry. |
48 | * | |
49 | * @a: Pointer to "struct tomoyo_acl_head". | |
50 | * @b: Pointer to "struct tomoyo_acl_head". | |
51 | * | |
52 | * Returns true if @a == @b, false otherwise. | |
53 | */ | |
54 | static bool tomoyo_same_address_group(const struct tomoyo_acl_head *a, | |
55 | const struct tomoyo_acl_head *b) | |
56 | { | |
57 | const struct tomoyo_address_group *p1 = container_of(a, typeof(*p1), | |
58 | head); | |
59 | const struct tomoyo_address_group *p2 = container_of(b, typeof(*p2), | |
60 | head); | |
61 | ||
62 | return tomoyo_same_ipaddr_union(&p1->address, &p2->address); | |
63 | } | |
64 | ||
65 | /** | |
66 | * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list. | |
7c2ea22e | 67 | * |
a238cf5b | 68 | * @param: Pointer to "struct tomoyo_acl_param". |
0f2a55d5 | 69 | * @type: Type of this group. |
7c2ea22e TH |
70 | * |
71 | * Returns 0 on success, negative value otherwise. | |
72 | */ | |
a238cf5b | 73 | int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type) |
7c2ea22e | 74 | { |
a238cf5b | 75 | struct tomoyo_group *group = tomoyo_get_group(param, type); |
7c2ea22e | 76 | int error = -EINVAL; |
7c2ea22e TH |
77 | if (!group) |
78 | return -ENOMEM; | |
a238cf5b | 79 | param->list = &group->member_list; |
7c2ea22e TH |
80 | if (type == TOMOYO_PATH_GROUP) { |
81 | struct tomoyo_path_group e = { }; | |
a238cf5b | 82 | e.member_name = tomoyo_get_name(tomoyo_read_token(param)); |
7c2ea22e TH |
83 | if (!e.member_name) { |
84 | error = -ENOMEM; | |
85 | goto out; | |
86 | } | |
a238cf5b TH |
87 | error = tomoyo_update_policy(&e.head, sizeof(e), param, |
88 | tomoyo_same_path_group); | |
7c2ea22e TH |
89 | tomoyo_put_name(e.member_name); |
90 | } else if (type == TOMOYO_NUMBER_GROUP) { | |
91 | struct tomoyo_number_group e = { }; | |
a238cf5b TH |
92 | if (param->data[0] == '@' || |
93 | !tomoyo_parse_number_union(param, &e.number)) | |
7c2ea22e | 94 | goto out; |
a238cf5b TH |
95 | error = tomoyo_update_policy(&e.head, sizeof(e), param, |
96 | tomoyo_same_number_group); | |
7c2ea22e TH |
97 | /* |
98 | * tomoyo_put_number_union() is not needed because | |
a238cf5b | 99 | * param->data[0] != '@'. |
7c2ea22e | 100 | */ |
059d84db TH |
101 | } else { |
102 | struct tomoyo_address_group e = { }; | |
103 | ||
104 | if (param->data[0] == '@' || | |
105 | !tomoyo_parse_ipaddr_union(param, &e.address)) | |
106 | goto out; | |
107 | error = tomoyo_update_policy(&e.head, sizeof(e), param, | |
108 | tomoyo_same_address_group); | |
7c2ea22e | 109 | } |
a238cf5b | 110 | out: |
7c2ea22e TH |
111 | tomoyo_put_group(group); |
112 | return error; | |
113 | } | |
114 | ||
115 | /** | |
116 | * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group. | |
117 | * | |
0f2a55d5 TH |
118 | * @pathname: The name of pathname. |
119 | * @group: Pointer to "struct tomoyo_path_group". | |
7c2ea22e | 120 | * |
484ca79c TH |
121 | * Returns matched member's pathname if @pathname matches pathnames in @group, |
122 | * NULL otherwise. | |
7c2ea22e TH |
123 | * |
124 | * Caller holds tomoyo_read_lock(). | |
125 | */ | |
484ca79c TH |
126 | const struct tomoyo_path_info * |
127 | tomoyo_path_matches_group(const struct tomoyo_path_info *pathname, | |
128 | const struct tomoyo_group *group) | |
7c2ea22e TH |
129 | { |
130 | struct tomoyo_path_group *member; | |
7c2ea22e TH |
131 | list_for_each_entry_rcu(member, &group->member_list, head.list) { |
132 | if (member->head.is_deleted) | |
133 | continue; | |
134 | if (!tomoyo_path_matches_pattern(pathname, member->member_name)) | |
135 | continue; | |
484ca79c | 136 | return member->member_name; |
7c2ea22e | 137 | } |
484ca79c | 138 | return NULL; |
7c2ea22e TH |
139 | } |
140 | ||
141 | /** | |
142 | * tomoyo_number_matches_group - Check whether the given number matches members of the given number group. | |
143 | * | |
144 | * @min: Min number. | |
145 | * @max: Max number. | |
146 | * @group: Pointer to "struct tomoyo_number_group". | |
147 | * | |
148 | * Returns true if @min and @max partially overlaps @group, false otherwise. | |
149 | * | |
150 | * Caller holds tomoyo_read_lock(). | |
151 | */ | |
152 | bool tomoyo_number_matches_group(const unsigned long min, | |
153 | const unsigned long max, | |
154 | const struct tomoyo_group *group) | |
155 | { | |
156 | struct tomoyo_number_group *member; | |
157 | bool matched = false; | |
158 | list_for_each_entry_rcu(member, &group->member_list, head.list) { | |
159 | if (member->head.is_deleted) | |
160 | continue; | |
161 | if (min > member->number.values[1] || | |
162 | max < member->number.values[0]) | |
163 | continue; | |
164 | matched = true; | |
165 | break; | |
166 | } | |
167 | return matched; | |
168 | } | |
059d84db TH |
169 | |
170 | /** | |
171 | * tomoyo_address_matches_group - Check whether the given address matches members of the given address group. | |
172 | * | |
173 | * @is_ipv6: True if @address is an IPv6 address. | |
174 | * @address: An IPv4 or IPv6 address. | |
175 | * @group: Pointer to "struct tomoyo_address_group". | |
176 | * | |
177 | * Returns true if @address matches addresses in @group group, false otherwise. | |
178 | * | |
179 | * Caller holds tomoyo_read_lock(). | |
180 | */ | |
181 | bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address, | |
182 | const struct tomoyo_group *group) | |
183 | { | |
184 | struct tomoyo_address_group *member; | |
185 | bool matched = false; | |
186 | const u8 size = is_ipv6 ? 16 : 4; | |
187 | ||
188 | list_for_each_entry_rcu(member, &group->member_list, head.list) { | |
189 | if (member->head.is_deleted) | |
190 | continue; | |
191 | if (member->address.is_ipv6 != is_ipv6) | |
192 | continue; | |
193 | if (memcmp(&member->address.ip[0], address, size) > 0 || | |
194 | memcmp(address, &member->address.ip[1], size) > 0) | |
195 | continue; | |
196 | matched = true; | |
197 | break; | |
198 | } | |
199 | return matched; | |
200 | } |