diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-17 16:15:55 -0500 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-17 16:15:55 -0500 |
commit | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch) | |
tree | a8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /security/tomoyo/gc.c | |
parent | 406089d01562f1e2bf9f089fd7637009ebaad589 (diff) |
Patched in Tegra support.
Diffstat (limited to 'security/tomoyo/gc.c')
-rw-r--r-- | security/tomoyo/gc.c | 540 |
1 files changed, 305 insertions, 235 deletions
diff --git a/security/tomoyo/gc.c b/security/tomoyo/gc.c index 986a6a75686..ae135fbbbe9 100644 --- a/security/tomoyo/gc.c +++ b/security/tomoyo/gc.c | |||
@@ -8,26 +8,36 @@ | |||
8 | #include <linux/kthread.h> | 8 | #include <linux/kthread.h> |
9 | #include <linux/slab.h> | 9 | #include <linux/slab.h> |
10 | 10 | ||
11 | /** | ||
12 | * tomoyo_memory_free - Free memory for elements. | ||
13 | * | ||
14 | * @ptr: Pointer to allocated memory. | ||
15 | * | ||
16 | * Returns nothing. | ||
17 | * | ||
18 | * Caller holds tomoyo_policy_lock mutex. | ||
19 | */ | ||
20 | static inline void tomoyo_memory_free(void *ptr) | ||
21 | { | ||
22 | tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= ksize(ptr); | ||
23 | kfree(ptr); | ||
24 | } | ||
25 | |||
26 | /* The list for "struct tomoyo_io_buffer". */ | 11 | /* The list for "struct tomoyo_io_buffer". */ |
27 | static LIST_HEAD(tomoyo_io_buffer_list); | 12 | static LIST_HEAD(tomoyo_io_buffer_list); |
28 | /* Lock for protecting tomoyo_io_buffer_list. */ | 13 | /* Lock for protecting tomoyo_io_buffer_list. */ |
29 | static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock); | 14 | static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock); |
30 | 15 | ||
16 | /* Size of an element. */ | ||
17 | static const u8 tomoyo_element_size[TOMOYO_MAX_POLICY] = { | ||
18 | [TOMOYO_ID_GROUP] = sizeof(struct tomoyo_group), | ||
19 | [TOMOYO_ID_PATH_GROUP] = sizeof(struct tomoyo_path_group), | ||
20 | [TOMOYO_ID_NUMBER_GROUP] = sizeof(struct tomoyo_number_group), | ||
21 | [TOMOYO_ID_AGGREGATOR] = sizeof(struct tomoyo_aggregator), | ||
22 | [TOMOYO_ID_TRANSITION_CONTROL] = | ||
23 | sizeof(struct tomoyo_transition_control), | ||
24 | [TOMOYO_ID_MANAGER] = sizeof(struct tomoyo_manager), | ||
25 | /* [TOMOYO_ID_CONDITION] = "struct tomoyo_condition"->size, */ | ||
26 | /* [TOMOYO_ID_NAME] = "struct tomoyo_name"->size, */ | ||
27 | /* [TOMOYO_ID_ACL] = | ||
28 | tomoyo_acl_size["struct tomoyo_acl_info"->type], */ | ||
29 | [TOMOYO_ID_DOMAIN] = sizeof(struct tomoyo_domain_info), | ||
30 | }; | ||
31 | |||
32 | /* Size of a domain ACL element. */ | ||
33 | static const u8 tomoyo_acl_size[] = { | ||
34 | [TOMOYO_TYPE_PATH_ACL] = sizeof(struct tomoyo_path_acl), | ||
35 | [TOMOYO_TYPE_PATH2_ACL] = sizeof(struct tomoyo_path2_acl), | ||
36 | [TOMOYO_TYPE_PATH_NUMBER_ACL] = sizeof(struct tomoyo_path_number_acl), | ||
37 | [TOMOYO_TYPE_MKDEV_ACL] = sizeof(struct tomoyo_mkdev_acl), | ||
38 | [TOMOYO_TYPE_MOUNT_ACL] = sizeof(struct tomoyo_mount_acl), | ||
39 | }; | ||
40 | |||
31 | /** | 41 | /** |
32 | * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not. | 42 | * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not. |
33 | * | 43 | * |
@@ -45,11 +55,15 @@ static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element) | |||
45 | list_for_each_entry(head, &tomoyo_io_buffer_list, list) { | 55 | list_for_each_entry(head, &tomoyo_io_buffer_list, list) { |
46 | head->users++; | 56 | head->users++; |
47 | spin_unlock(&tomoyo_io_buffer_list_lock); | 57 | spin_unlock(&tomoyo_io_buffer_list_lock); |
48 | mutex_lock(&head->io_sem); | 58 | if (mutex_lock_interruptible(&head->io_sem)) { |
59 | in_use = true; | ||
60 | goto out; | ||
61 | } | ||
49 | if (head->r.domain == element || head->r.group == element || | 62 | if (head->r.domain == element || head->r.group == element || |
50 | head->r.acl == element || &head->w.domain->list == element) | 63 | head->r.acl == element || &head->w.domain->list == element) |
51 | in_use = true; | 64 | in_use = true; |
52 | mutex_unlock(&head->io_sem); | 65 | mutex_unlock(&head->io_sem); |
66 | out: | ||
53 | spin_lock(&tomoyo_io_buffer_list_lock); | 67 | spin_lock(&tomoyo_io_buffer_list_lock); |
54 | head->users--; | 68 | head->users--; |
55 | if (in_use) | 69 | if (in_use) |
@@ -63,14 +77,15 @@ static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element) | |||
63 | * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not. | 77 | * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not. |
64 | * | 78 | * |
65 | * @string: String to check. | 79 | * @string: String to check. |
80 | * @size: Memory allocated for @string . | ||
66 | * | 81 | * |
67 | * Returns true if @string is used by /sys/kernel/security/tomoyo/ users, | 82 | * Returns true if @string is used by /sys/kernel/security/tomoyo/ users, |
68 | * false otherwise. | 83 | * false otherwise. |
69 | */ | 84 | */ |
70 | static bool tomoyo_name_used_by_io_buffer(const char *string) | 85 | static bool tomoyo_name_used_by_io_buffer(const char *string, |
86 | const size_t size) | ||
71 | { | 87 | { |
72 | struct tomoyo_io_buffer *head; | 88 | struct tomoyo_io_buffer *head; |
73 | const size_t size = strlen(string) + 1; | ||
74 | bool in_use = false; | 89 | bool in_use = false; |
75 | 90 | ||
76 | spin_lock(&tomoyo_io_buffer_list_lock); | 91 | spin_lock(&tomoyo_io_buffer_list_lock); |
@@ -78,7 +93,10 @@ static bool tomoyo_name_used_by_io_buffer(const char *string) | |||
78 | int i; | 93 | int i; |
79 | head->users++; | 94 | head->users++; |
80 | spin_unlock(&tomoyo_io_buffer_list_lock); | 95 | spin_unlock(&tomoyo_io_buffer_list_lock); |
81 | mutex_lock(&head->io_sem); | 96 | if (mutex_lock_interruptible(&head->io_sem)) { |
97 | in_use = true; | ||
98 | goto out; | ||
99 | } | ||
82 | for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) { | 100 | for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) { |
83 | const char *w = head->r.w[i]; | 101 | const char *w = head->r.w[i]; |
84 | if (w < string || w > string + size) | 102 | if (w < string || w > string + size) |
@@ -87,6 +105,7 @@ static bool tomoyo_name_used_by_io_buffer(const char *string) | |||
87 | break; | 105 | break; |
88 | } | 106 | } |
89 | mutex_unlock(&head->io_sem); | 107 | mutex_unlock(&head->io_sem); |
108 | out: | ||
90 | spin_lock(&tomoyo_io_buffer_list_lock); | 109 | spin_lock(&tomoyo_io_buffer_list_lock); |
91 | head->users--; | 110 | head->users--; |
92 | if (in_use) | 111 | if (in_use) |
@@ -96,6 +115,84 @@ static bool tomoyo_name_used_by_io_buffer(const char *string) | |||
96 | return in_use; | 115 | return in_use; |
97 | } | 116 | } |
98 | 117 | ||
118 | /* Structure for garbage collection. */ | ||
119 | struct tomoyo_gc { | ||
120 | struct list_head list; | ||
121 | enum tomoyo_policy_id type; | ||
122 | size_t size; | ||
123 | struct list_head *element; | ||
124 | }; | ||
125 | /* List of entries to be deleted. */ | ||
126 | static LIST_HEAD(tomoyo_gc_list); | ||
127 | /* Length of tomoyo_gc_list. */ | ||
128 | static int tomoyo_gc_list_len; | ||
129 | |||
130 | /** | ||
131 | * tomoyo_add_to_gc - Add an entry to to be deleted list. | ||
132 | * | ||
133 | * @type: One of values in "enum tomoyo_policy_id". | ||
134 | * @element: Pointer to "struct list_head". | ||
135 | * | ||
136 | * Returns true on success, false otherwise. | ||
137 | * | ||
138 | * Caller holds tomoyo_policy_lock mutex. | ||
139 | * | ||
140 | * Adding an entry needs kmalloc(). Thus, if we try to add thousands of | ||
141 | * entries at once, it will take too long time. Thus, do not add more than 128 | ||
142 | * entries per a scan. But to be able to handle worst case where all entries | ||
143 | * are in-use, we accept one more entry per a scan. | ||
144 | * | ||
145 | * If we use singly linked list using "struct list_head"->prev (which is | ||
146 | * LIST_POISON2), we can avoid kmalloc(). | ||
147 | */ | ||
148 | static bool tomoyo_add_to_gc(const int type, struct list_head *element) | ||
149 | { | ||
150 | struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC); | ||
151 | if (!entry) | ||
152 | return false; | ||
153 | entry->type = type; | ||
154 | if (type == TOMOYO_ID_ACL) | ||
155 | entry->size = tomoyo_acl_size[ | ||
156 | container_of(element, | ||
157 | typeof(struct tomoyo_acl_info), | ||
158 | list)->type]; | ||
159 | else if (type == TOMOYO_ID_NAME) | ||
160 | entry->size = strlen(container_of(element, | ||
161 | typeof(struct tomoyo_name), | ||
162 | head.list)->entry.name) + 1; | ||
163 | else if (type == TOMOYO_ID_CONDITION) | ||
164 | entry->size = | ||
165 | container_of(element, typeof(struct tomoyo_condition), | ||
166 | head.list)->size; | ||
167 | else | ||
168 | entry->size = tomoyo_element_size[type]; | ||
169 | entry->element = element; | ||
170 | list_add(&entry->list, &tomoyo_gc_list); | ||
171 | list_del_rcu(element); | ||
172 | return tomoyo_gc_list_len++ < 128; | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * tomoyo_element_linked_by_gc - Validate next element of an entry. | ||
177 | * | ||
178 | * @element: Pointer to an element. | ||
179 | * @size: Size of @element in byte. | ||
180 | * | ||
181 | * Returns true if @element is linked by other elements in the garbage | ||
182 | * collector's queue, false otherwise. | ||
183 | */ | ||
184 | static bool tomoyo_element_linked_by_gc(const u8 *element, const size_t size) | ||
185 | { | ||
186 | struct tomoyo_gc *p; | ||
187 | list_for_each_entry(p, &tomoyo_gc_list, list) { | ||
188 | const u8 *ptr = (const u8 *) p->element->next; | ||
189 | if (ptr < element || element + size < ptr) | ||
190 | continue; | ||
191 | return true; | ||
192 | } | ||
193 | return false; | ||
194 | } | ||
195 | |||
99 | /** | 196 | /** |
100 | * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control". | 197 | * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control". |
101 | * | 198 | * |
@@ -103,7 +200,7 @@ static bool tomoyo_name_used_by_io_buffer(const char *string) | |||
103 | * | 200 | * |
104 | * Returns nothing. | 201 | * Returns nothing. |
105 | */ | 202 | */ |
106 | static inline void tomoyo_del_transition_control(struct list_head *element) | 203 | static void tomoyo_del_transition_control(struct list_head *element) |
107 | { | 204 | { |
108 | struct tomoyo_transition_control *ptr = | 205 | struct tomoyo_transition_control *ptr = |
109 | container_of(element, typeof(*ptr), head.list); | 206 | container_of(element, typeof(*ptr), head.list); |
@@ -118,7 +215,7 @@ static inline void tomoyo_del_transition_control(struct list_head *element) | |||
118 | * | 215 | * |
119 | * Returns nothing. | 216 | * Returns nothing. |
120 | */ | 217 | */ |
121 | static inline void tomoyo_del_aggregator(struct list_head *element) | 218 | static void tomoyo_del_aggregator(struct list_head *element) |
122 | { | 219 | { |
123 | struct tomoyo_aggregator *ptr = | 220 | struct tomoyo_aggregator *ptr = |
124 | container_of(element, typeof(*ptr), head.list); | 221 | container_of(element, typeof(*ptr), head.list); |
@@ -133,7 +230,7 @@ static inline void tomoyo_del_aggregator(struct list_head *element) | |||
133 | * | 230 | * |
134 | * Returns nothing. | 231 | * Returns nothing. |
135 | */ | 232 | */ |
136 | static inline void tomoyo_del_manager(struct list_head *element) | 233 | static void tomoyo_del_manager(struct list_head *element) |
137 | { | 234 | { |
138 | struct tomoyo_manager *ptr = | 235 | struct tomoyo_manager *ptr = |
139 | container_of(element, typeof(*ptr), head.list); | 236 | container_of(element, typeof(*ptr), head.list); |
@@ -196,38 +293,6 @@ static void tomoyo_del_acl(struct list_head *element) | |||
196 | tomoyo_put_number_union(&entry->flags); | 293 | tomoyo_put_number_union(&entry->flags); |
197 | } | 294 | } |
198 | break; | 295 | break; |
199 | case TOMOYO_TYPE_ENV_ACL: | ||
200 | { | ||
201 | struct tomoyo_env_acl *entry = | ||
202 | container_of(acl, typeof(*entry), head); | ||
203 | |||
204 | tomoyo_put_name(entry->env); | ||
205 | } | ||
206 | break; | ||
207 | case TOMOYO_TYPE_INET_ACL: | ||
208 | { | ||
209 | struct tomoyo_inet_acl *entry = | ||
210 | container_of(acl, typeof(*entry), head); | ||
211 | |||
212 | tomoyo_put_group(entry->address.group); | ||
213 | tomoyo_put_number_union(&entry->port); | ||
214 | } | ||
215 | break; | ||
216 | case TOMOYO_TYPE_UNIX_ACL: | ||
217 | { | ||
218 | struct tomoyo_unix_acl *entry = | ||
219 | container_of(acl, typeof(*entry), head); | ||
220 | |||
221 | tomoyo_put_name_union(&entry->name); | ||
222 | } | ||
223 | break; | ||
224 | case TOMOYO_TYPE_MANUAL_TASK_ACL: | ||
225 | { | ||
226 | struct tomoyo_task_acl *entry = | ||
227 | container_of(acl, typeof(*entry), head); | ||
228 | tomoyo_put_name(entry->domainname); | ||
229 | } | ||
230 | break; | ||
231 | } | 296 | } |
232 | } | 297 | } |
233 | 298 | ||
@@ -236,26 +301,44 @@ static void tomoyo_del_acl(struct list_head *element) | |||
236 | * | 301 | * |
237 | * @element: Pointer to "struct list_head". | 302 | * @element: Pointer to "struct list_head". |
238 | * | 303 | * |
239 | * Returns nothing. | 304 | * Returns true if deleted, false otherwise. |
240 | * | ||
241 | * Caller holds tomoyo_policy_lock mutex. | ||
242 | */ | 305 | */ |
243 | static inline void tomoyo_del_domain(struct list_head *element) | 306 | static bool tomoyo_del_domain(struct list_head *element) |
244 | { | 307 | { |
245 | struct tomoyo_domain_info *domain = | 308 | struct tomoyo_domain_info *domain = |
246 | container_of(element, typeof(*domain), list); | 309 | container_of(element, typeof(*domain), list); |
247 | struct tomoyo_acl_info *acl; | 310 | struct tomoyo_acl_info *acl; |
248 | struct tomoyo_acl_info *tmp; | 311 | struct tomoyo_acl_info *tmp; |
249 | /* | 312 | /* |
250 | * Since this domain is referenced from neither | 313 | * Since we don't protect whole execve() operation using SRCU, |
251 | * "struct tomoyo_io_buffer" nor "struct cred"->security, we can delete | 314 | * we need to recheck domain->users at this point. |
252 | * elements without checking for is_deleted flag. | 315 | * |
316 | * (1) Reader starts SRCU section upon execve(). | ||
317 | * (2) Reader traverses tomoyo_domain_list and finds this domain. | ||
318 | * (3) Writer marks this domain as deleted. | ||
319 | * (4) Garbage collector removes this domain from tomoyo_domain_list | ||
320 | * because this domain is marked as deleted and used by nobody. | ||
321 | * (5) Reader saves reference to this domain into | ||
322 | * "struct linux_binprm"->cred->security . | ||
323 | * (6) Reader finishes SRCU section, although execve() operation has | ||
324 | * not finished yet. | ||
325 | * (7) Garbage collector waits for SRCU synchronization. | ||
326 | * (8) Garbage collector kfree() this domain because this domain is | ||
327 | * used by nobody. | ||
328 | * (9) Reader finishes execve() operation and restores this domain from | ||
329 | * "struct linux_binprm"->cred->security. | ||
330 | * | ||
331 | * By updating domain->users at (5), we can solve this race problem | ||
332 | * by rechecking domain->users at (8). | ||
253 | */ | 333 | */ |
334 | if (atomic_read(&domain->users)) | ||
335 | return false; | ||
254 | list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) { | 336 | list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) { |
255 | tomoyo_del_acl(&acl->list); | 337 | tomoyo_del_acl(&acl->list); |
256 | tomoyo_memory_free(acl); | 338 | tomoyo_memory_free(acl); |
257 | } | 339 | } |
258 | tomoyo_put_name(domain->domainname); | 340 | tomoyo_put_name(domain->domainname); |
341 | return true; | ||
259 | } | 342 | } |
260 | 343 | ||
261 | /** | 344 | /** |
@@ -304,9 +387,10 @@ void tomoyo_del_condition(struct list_head *element) | |||
304 | * | 387 | * |
305 | * Returns nothing. | 388 | * Returns nothing. |
306 | */ | 389 | */ |
307 | static inline void tomoyo_del_name(struct list_head *element) | 390 | static void tomoyo_del_name(struct list_head *element) |
308 | { | 391 | { |
309 | /* Nothing to do. */ | 392 | const struct tomoyo_name *ptr = |
393 | container_of(element, typeof(*ptr), head.list); | ||
310 | } | 394 | } |
311 | 395 | ||
312 | /** | 396 | /** |
@@ -316,7 +400,7 @@ static inline void tomoyo_del_name(struct list_head *element) | |||
316 | * | 400 | * |
317 | * Returns nothing. | 401 | * Returns nothing. |
318 | */ | 402 | */ |
319 | static inline void tomoyo_del_path_group(struct list_head *element) | 403 | static void tomoyo_del_path_group(struct list_head *element) |
320 | { | 404 | { |
321 | struct tomoyo_path_group *member = | 405 | struct tomoyo_path_group *member = |
322 | container_of(element, typeof(*member), head.list); | 406 | container_of(element, typeof(*member), head.list); |
@@ -330,7 +414,7 @@ static inline void tomoyo_del_path_group(struct list_head *element) | |||
330 | * | 414 | * |
331 | * Returns nothing. | 415 | * Returns nothing. |
332 | */ | 416 | */ |
333 | static inline void tomoyo_del_group(struct list_head *element) | 417 | static void tomoyo_del_group(struct list_head *element) |
334 | { | 418 | { |
335 | struct tomoyo_group *group = | 419 | struct tomoyo_group *group = |
336 | container_of(element, typeof(*group), head.list); | 420 | container_of(element, typeof(*group), head.list); |
@@ -338,128 +422,16 @@ static inline void tomoyo_del_group(struct list_head *element) | |||
338 | } | 422 | } |
339 | 423 | ||
340 | /** | 424 | /** |
341 | * tomoyo_del_address_group - Delete members in "struct tomoyo_address_group". | ||
342 | * | ||
343 | * @element: Pointer to "struct list_head". | ||
344 | * | ||
345 | * Returns nothing. | ||
346 | */ | ||
347 | static inline void tomoyo_del_address_group(struct list_head *element) | ||
348 | { | ||
349 | /* Nothing to do. */ | ||
350 | } | ||
351 | |||
352 | /** | ||
353 | * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group". | 425 | * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group". |
354 | * | 426 | * |
355 | * @element: Pointer to "struct list_head". | 427 | * @element: Pointer to "struct list_head". |
356 | * | 428 | * |
357 | * Returns nothing. | 429 | * Returns nothing. |
358 | */ | 430 | */ |
359 | static inline void tomoyo_del_number_group(struct list_head *element) | 431 | static void tomoyo_del_number_group(struct list_head *element) |
360 | { | 432 | { |
361 | /* Nothing to do. */ | 433 | struct tomoyo_number_group *member = |
362 | } | 434 | container_of(element, typeof(*member), head.list); |
363 | |||
364 | /** | ||
365 | * tomoyo_try_to_gc - Try to kfree() an entry. | ||
366 | * | ||
367 | * @type: One of values in "enum tomoyo_policy_id". | ||
368 | * @element: Pointer to "struct list_head". | ||
369 | * | ||
370 | * Returns nothing. | ||
371 | * | ||
372 | * Caller holds tomoyo_policy_lock mutex. | ||
373 | */ | ||
374 | static void tomoyo_try_to_gc(const enum tomoyo_policy_id type, | ||
375 | struct list_head *element) | ||
376 | { | ||
377 | /* | ||
378 | * __list_del_entry() guarantees that the list element became no longer | ||
379 | * reachable from the list which the element was originally on (e.g. | ||
380 | * tomoyo_domain_list). Also, synchronize_srcu() guarantees that the | ||
381 | * list element became no longer referenced by syscall users. | ||
382 | */ | ||
383 | __list_del_entry(element); | ||
384 | mutex_unlock(&tomoyo_policy_lock); | ||
385 | synchronize_srcu(&tomoyo_ss); | ||
386 | /* | ||
387 | * However, there are two users which may still be using the list | ||
388 | * element. We need to defer until both users forget this element. | ||
389 | * | ||
390 | * Don't kfree() until "struct tomoyo_io_buffer"->r.{domain,group,acl} | ||
391 | * and "struct tomoyo_io_buffer"->w.domain forget this element. | ||
392 | */ | ||
393 | if (tomoyo_struct_used_by_io_buffer(element)) | ||
394 | goto reinject; | ||
395 | switch (type) { | ||
396 | case TOMOYO_ID_TRANSITION_CONTROL: | ||
397 | tomoyo_del_transition_control(element); | ||
398 | break; | ||
399 | case TOMOYO_ID_MANAGER: | ||
400 | tomoyo_del_manager(element); | ||
401 | break; | ||
402 | case TOMOYO_ID_AGGREGATOR: | ||
403 | tomoyo_del_aggregator(element); | ||
404 | break; | ||
405 | case TOMOYO_ID_GROUP: | ||
406 | tomoyo_del_group(element); | ||
407 | break; | ||
408 | case TOMOYO_ID_PATH_GROUP: | ||
409 | tomoyo_del_path_group(element); | ||
410 | break; | ||
411 | case TOMOYO_ID_ADDRESS_GROUP: | ||
412 | tomoyo_del_address_group(element); | ||
413 | break; | ||
414 | case TOMOYO_ID_NUMBER_GROUP: | ||
415 | tomoyo_del_number_group(element); | ||
416 | break; | ||
417 | case TOMOYO_ID_CONDITION: | ||
418 | tomoyo_del_condition(element); | ||
419 | break; | ||
420 | case TOMOYO_ID_NAME: | ||
421 | /* | ||
422 | * Don't kfree() until all "struct tomoyo_io_buffer"->r.w[] | ||
423 | * forget this element. | ||
424 | */ | ||
425 | if (tomoyo_name_used_by_io_buffer | ||
426 | (container_of(element, typeof(struct tomoyo_name), | ||
427 | head.list)->entry.name)) | ||
428 | goto reinject; | ||
429 | tomoyo_del_name(element); | ||
430 | break; | ||
431 | case TOMOYO_ID_ACL: | ||
432 | tomoyo_del_acl(element); | ||
433 | break; | ||
434 | case TOMOYO_ID_DOMAIN: | ||
435 | /* | ||
436 | * Don't kfree() until all "struct cred"->security forget this | ||
437 | * element. | ||
438 | */ | ||
439 | if (atomic_read(&container_of | ||
440 | (element, typeof(struct tomoyo_domain_info), | ||
441 | list)->users)) | ||
442 | goto reinject; | ||
443 | break; | ||
444 | case TOMOYO_MAX_POLICY: | ||
445 | break; | ||
446 | } | ||
447 | mutex_lock(&tomoyo_policy_lock); | ||
448 | if (type == TOMOYO_ID_DOMAIN) | ||
449 | tomoyo_del_domain(element); | ||
450 | tomoyo_memory_free(element); | ||
451 | return; | ||
452 | reinject: | ||
453 | /* | ||
454 | * We can safely reinject this element here bacause | ||
455 | * (1) Appending list elements and removing list elements are protected | ||
456 | * by tomoyo_policy_lock mutex. | ||
457 | * (2) Only this function removes list elements and this function is | ||
458 | * exclusively executed by tomoyo_gc_mutex mutex. | ||
459 | * are true. | ||
460 | */ | ||
461 | mutex_lock(&tomoyo_policy_lock); | ||
462 | list_add_rcu(element, element->prev); | ||
463 | } | 435 | } |
464 | 436 | ||
465 | /** | 437 | /** |
@@ -468,19 +440,19 @@ reinject: | |||
468 | * @id: One of values in "enum tomoyo_policy_id". | 440 | * @id: One of values in "enum tomoyo_policy_id". |
469 | * @member_list: Pointer to "struct list_head". | 441 | * @member_list: Pointer to "struct list_head". |
470 | * | 442 | * |
471 | * Returns nothing. | 443 | * Returns true if some elements are deleted, false otherwise. |
472 | */ | 444 | */ |
473 | static void tomoyo_collect_member(const enum tomoyo_policy_id id, | 445 | static bool tomoyo_collect_member(const enum tomoyo_policy_id id, |
474 | struct list_head *member_list) | 446 | struct list_head *member_list) |
475 | { | 447 | { |
476 | struct tomoyo_acl_head *member; | 448 | struct tomoyo_acl_head *member; |
477 | struct tomoyo_acl_head *tmp; | 449 | list_for_each_entry(member, member_list, list) { |
478 | list_for_each_entry_safe(member, tmp, member_list, list) { | ||
479 | if (!member->is_deleted) | 450 | if (!member->is_deleted) |
480 | continue; | 451 | continue; |
481 | member->is_deleted = TOMOYO_GC_IN_PROGRESS; | 452 | if (!tomoyo_add_to_gc(id, &member->list)) |
482 | tomoyo_try_to_gc(id, &member->list); | 453 | return false; |
483 | } | 454 | } |
455 | return true; | ||
484 | } | 456 | } |
485 | 457 | ||
486 | /** | 458 | /** |
@@ -488,22 +460,22 @@ static void tomoyo_collect_member(const enum tomoyo_policy_id id, | |||
488 | * | 460 | * |
489 | * @list: Pointer to "struct list_head". | 461 | * @list: Pointer to "struct list_head". |
490 | * | 462 | * |
491 | * Returns nothing. | 463 | * Returns true if some elements are deleted, false otherwise. |
492 | */ | 464 | */ |
493 | static void tomoyo_collect_acl(struct list_head *list) | 465 | static bool tomoyo_collect_acl(struct list_head *list) |
494 | { | 466 | { |
495 | struct tomoyo_acl_info *acl; | 467 | struct tomoyo_acl_info *acl; |
496 | struct tomoyo_acl_info *tmp; | 468 | list_for_each_entry(acl, list, list) { |
497 | list_for_each_entry_safe(acl, tmp, list, list) { | ||
498 | if (!acl->is_deleted) | 469 | if (!acl->is_deleted) |
499 | continue; | 470 | continue; |
500 | acl->is_deleted = TOMOYO_GC_IN_PROGRESS; | 471 | if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list)) |
501 | tomoyo_try_to_gc(TOMOYO_ID_ACL, &acl->list); | 472 | return false; |
502 | } | 473 | } |
474 | return true; | ||
503 | } | 475 | } |
504 | 476 | ||
505 | /** | 477 | /** |
506 | * tomoyo_collect_entry - Try to kfree() deleted elements. | 478 | * tomoyo_collect_entry - Scan lists for deleted elements. |
507 | * | 479 | * |
508 | * Returns nothing. | 480 | * Returns nothing. |
509 | */ | 481 | */ |
@@ -512,82 +484,174 @@ static void tomoyo_collect_entry(void) | |||
512 | int i; | 484 | int i; |
513 | enum tomoyo_policy_id id; | 485 | enum tomoyo_policy_id id; |
514 | struct tomoyo_policy_namespace *ns; | 486 | struct tomoyo_policy_namespace *ns; |
515 | mutex_lock(&tomoyo_policy_lock); | 487 | int idx; |
488 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) | ||
489 | return; | ||
490 | idx = tomoyo_read_lock(); | ||
516 | { | 491 | { |
517 | struct tomoyo_domain_info *domain; | 492 | struct tomoyo_domain_info *domain; |
518 | struct tomoyo_domain_info *tmp; | 493 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
519 | list_for_each_entry_safe(domain, tmp, &tomoyo_domain_list, | 494 | if (!tomoyo_collect_acl(&domain->acl_info_list)) |
520 | list) { | 495 | goto unlock; |
521 | tomoyo_collect_acl(&domain->acl_info_list); | ||
522 | if (!domain->is_deleted || atomic_read(&domain->users)) | 496 | if (!domain->is_deleted || atomic_read(&domain->users)) |
523 | continue; | 497 | continue; |
524 | tomoyo_try_to_gc(TOMOYO_ID_DOMAIN, &domain->list); | 498 | /* |
499 | * Nobody is referring this domain. But somebody may | ||
500 | * refer this domain after successful execve(). | ||
501 | * We recheck domain->users after SRCU synchronization. | ||
502 | */ | ||
503 | if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list)) | ||
504 | goto unlock; | ||
525 | } | 505 | } |
526 | } | 506 | } |
527 | list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) { | 507 | list_for_each_entry_rcu(ns, &tomoyo_namespace_list, namespace_list) { |
528 | for (id = 0; id < TOMOYO_MAX_POLICY; id++) | 508 | for (id = 0; id < TOMOYO_MAX_POLICY; id++) |
529 | tomoyo_collect_member(id, &ns->policy_list[id]); | 509 | if (!tomoyo_collect_member(id, &ns->policy_list[id])) |
510 | goto unlock; | ||
530 | for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++) | 511 | for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++) |
531 | tomoyo_collect_acl(&ns->acl_group[i]); | 512 | if (!tomoyo_collect_acl(&ns->acl_group[i])) |
532 | } | 513 | goto unlock; |
533 | { | ||
534 | struct tomoyo_shared_acl_head *ptr; | ||
535 | struct tomoyo_shared_acl_head *tmp; | ||
536 | list_for_each_entry_safe(ptr, tmp, &tomoyo_condition_list, | ||
537 | list) { | ||
538 | if (atomic_read(&ptr->users) > 0) | ||
539 | continue; | ||
540 | atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS); | ||
541 | tomoyo_try_to_gc(TOMOYO_ID_CONDITION, &ptr->list); | ||
542 | } | ||
543 | } | ||
544 | list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) { | ||
545 | for (i = 0; i < TOMOYO_MAX_GROUP; i++) { | 514 | for (i = 0; i < TOMOYO_MAX_GROUP; i++) { |
546 | struct list_head *list = &ns->group_list[i]; | 515 | struct list_head *list = &ns->group_list[i]; |
547 | struct tomoyo_group *group; | 516 | struct tomoyo_group *group; |
548 | struct tomoyo_group *tmp; | ||
549 | switch (i) { | 517 | switch (i) { |
550 | case 0: | 518 | case 0: |
551 | id = TOMOYO_ID_PATH_GROUP; | 519 | id = TOMOYO_ID_PATH_GROUP; |
552 | break; | 520 | break; |
553 | case 1: | ||
554 | id = TOMOYO_ID_NUMBER_GROUP; | ||
555 | break; | ||
556 | default: | 521 | default: |
557 | id = TOMOYO_ID_ADDRESS_GROUP; | 522 | id = TOMOYO_ID_NUMBER_GROUP; |
558 | break; | 523 | break; |
559 | } | 524 | } |
560 | list_for_each_entry_safe(group, tmp, list, head.list) { | 525 | list_for_each_entry(group, list, head.list) { |
561 | tomoyo_collect_member(id, &group->member_list); | 526 | if (!tomoyo_collect_member |
527 | (id, &group->member_list)) | ||
528 | goto unlock; | ||
562 | if (!list_empty(&group->member_list) || | 529 | if (!list_empty(&group->member_list) || |
563 | atomic_read(&group->head.users) > 0) | 530 | atomic_read(&group->head.users)) |
564 | continue; | 531 | continue; |
565 | atomic_set(&group->head.users, | 532 | if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, |
566 | TOMOYO_GC_IN_PROGRESS); | 533 | &group->head.list)) |
567 | tomoyo_try_to_gc(TOMOYO_ID_GROUP, | 534 | goto unlock; |
568 | &group->head.list); | ||
569 | } | 535 | } |
570 | } | 536 | } |
571 | } | 537 | } |
572 | for (i = 0; i < TOMOYO_MAX_HASH; i++) { | 538 | id = TOMOYO_ID_CONDITION; |
573 | struct list_head *list = &tomoyo_name_list[i]; | 539 | for (i = 0; i < TOMOYO_MAX_HASH + 1; i++) { |
540 | struct list_head *list = !i ? | ||
541 | &tomoyo_condition_list : &tomoyo_name_list[i - 1]; | ||
574 | struct tomoyo_shared_acl_head *ptr; | 542 | struct tomoyo_shared_acl_head *ptr; |
575 | struct tomoyo_shared_acl_head *tmp; | 543 | list_for_each_entry(ptr, list, list) { |
576 | list_for_each_entry_safe(ptr, tmp, list, list) { | 544 | if (atomic_read(&ptr->users)) |
577 | if (atomic_read(&ptr->users) > 0) | ||
578 | continue; | 545 | continue; |
579 | atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS); | 546 | if (!tomoyo_add_to_gc(id, &ptr->list)) |
580 | tomoyo_try_to_gc(TOMOYO_ID_NAME, &ptr->list); | 547 | goto unlock; |
581 | } | 548 | } |
549 | id = TOMOYO_ID_NAME; | ||
582 | } | 550 | } |
551 | unlock: | ||
552 | tomoyo_read_unlock(idx); | ||
583 | mutex_unlock(&tomoyo_policy_lock); | 553 | mutex_unlock(&tomoyo_policy_lock); |
584 | } | 554 | } |
585 | 555 | ||
586 | /** | 556 | /** |
557 | * tomoyo_kfree_entry - Delete entries in tomoyo_gc_list. | ||
558 | * | ||
559 | * Returns true if some entries were kfree()d, false otherwise. | ||
560 | */ | ||
561 | static bool tomoyo_kfree_entry(void) | ||
562 | { | ||
563 | struct tomoyo_gc *p; | ||
564 | struct tomoyo_gc *tmp; | ||
565 | bool result = false; | ||
566 | |||
567 | list_for_each_entry_safe(p, tmp, &tomoyo_gc_list, list) { | ||
568 | struct list_head *element = p->element; | ||
569 | |||
570 | /* | ||
571 | * list_del_rcu() in tomoyo_add_to_gc() guarantees that the | ||
572 | * list element became no longer reachable from the list which | ||
573 | * the element was originally on (e.g. tomoyo_domain_list). | ||
574 | * Also, synchronize_srcu() in tomoyo_gc_thread() guarantees | ||
575 | * that the list element became no longer referenced by syscall | ||
576 | * users. | ||
577 | * | ||
578 | * However, there are three users which may still be using the | ||
579 | * list element. We need to defer until all of these users | ||
580 | * forget the list element. | ||
581 | * | ||
582 | * Firstly, defer until "struct tomoyo_io_buffer"->r.{domain, | ||
583 | * group,acl} and "struct tomoyo_io_buffer"->w.domain forget | ||
584 | * the list element. | ||
585 | */ | ||
586 | if (tomoyo_struct_used_by_io_buffer(element)) | ||
587 | continue; | ||
588 | /* | ||
589 | * Secondly, defer until all other elements in the | ||
590 | * tomoyo_gc_list list forget the list element. | ||
591 | */ | ||
592 | if (tomoyo_element_linked_by_gc((const u8 *) element, p->size)) | ||
593 | continue; | ||
594 | switch (p->type) { | ||
595 | case TOMOYO_ID_TRANSITION_CONTROL: | ||
596 | tomoyo_del_transition_control(element); | ||
597 | break; | ||
598 | case TOMOYO_ID_AGGREGATOR: | ||
599 | tomoyo_del_aggregator(element); | ||
600 | break; | ||
601 | case TOMOYO_ID_MANAGER: | ||
602 | tomoyo_del_manager(element); | ||
603 | break; | ||
604 | case TOMOYO_ID_CONDITION: | ||
605 | tomoyo_del_condition(element); | ||
606 | break; | ||
607 | case TOMOYO_ID_NAME: | ||
608 | /* | ||
609 | * Thirdly, defer until all "struct tomoyo_io_buffer" | ||
610 | * ->r.w[] forget the list element. | ||
611 | */ | ||
612 | if (tomoyo_name_used_by_io_buffer( | ||
613 | container_of(element, typeof(struct tomoyo_name), | ||
614 | head.list)->entry.name, p->size)) | ||
615 | continue; | ||
616 | tomoyo_del_name(element); | ||
617 | break; | ||
618 | case TOMOYO_ID_ACL: | ||
619 | tomoyo_del_acl(element); | ||
620 | break; | ||
621 | case TOMOYO_ID_DOMAIN: | ||
622 | if (!tomoyo_del_domain(element)) | ||
623 | continue; | ||
624 | break; | ||
625 | case TOMOYO_ID_PATH_GROUP: | ||
626 | tomoyo_del_path_group(element); | ||
627 | break; | ||
628 | case TOMOYO_ID_GROUP: | ||
629 | tomoyo_del_group(element); | ||
630 | break; | ||
631 | case TOMOYO_ID_NUMBER_GROUP: | ||
632 | tomoyo_del_number_group(element); | ||
633 | break; | ||
634 | case TOMOYO_MAX_POLICY: | ||
635 | break; | ||
636 | } | ||
637 | tomoyo_memory_free(element); | ||
638 | list_del(&p->list); | ||
639 | kfree(p); | ||
640 | tomoyo_gc_list_len--; | ||
641 | result = true; | ||
642 | } | ||
643 | return result; | ||
644 | } | ||
645 | |||
646 | /** | ||
587 | * tomoyo_gc_thread - Garbage collector thread function. | 647 | * tomoyo_gc_thread - Garbage collector thread function. |
588 | * | 648 | * |
589 | * @unused: Unused. | 649 | * @unused: Unused. |
590 | * | 650 | * |
651 | * In case OOM-killer choose this thread for termination, we create this thread | ||
652 | * as a short live thread whenever /sys/kernel/security/tomoyo/ interface was | ||
653 | * close()d. | ||
654 | * | ||
591 | * Returns 0. | 655 | * Returns 0. |
592 | */ | 656 | */ |
593 | static int tomoyo_gc_thread(void *unused) | 657 | static int tomoyo_gc_thread(void *unused) |
@@ -596,7 +660,13 @@ static int tomoyo_gc_thread(void *unused) | |||
596 | static DEFINE_MUTEX(tomoyo_gc_mutex); | 660 | static DEFINE_MUTEX(tomoyo_gc_mutex); |
597 | if (!mutex_trylock(&tomoyo_gc_mutex)) | 661 | if (!mutex_trylock(&tomoyo_gc_mutex)) |
598 | goto out; | 662 | goto out; |
599 | tomoyo_collect_entry(); | 663 | daemonize("GC for TOMOYO"); |
664 | do { | ||
665 | tomoyo_collect_entry(); | ||
666 | if (list_empty(&tomoyo_gc_list)) | ||
667 | break; | ||
668 | synchronize_srcu(&tomoyo_ss); | ||
669 | } while (tomoyo_kfree_entry()); | ||
600 | { | 670 | { |
601 | struct tomoyo_io_buffer *head; | 671 | struct tomoyo_io_buffer *head; |
602 | struct tomoyo_io_buffer *tmp; | 672 | struct tomoyo_io_buffer *tmp; |