Commit | Line | Data |
---|---|---|
847b173e TH |
1 | /* |
2 | * security/tomoyo/gc.c | |
3 | * | |
0f2a55d5 | 4 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
847b173e TH |
5 | */ |
6 | ||
7 | #include "common.h" | |
8 | #include <linux/kthread.h> | |
5a0e3ad6 | 9 | #include <linux/slab.h> |
847b173e | 10 | |
2e503bbb TH |
11 | /* The list for "struct tomoyo_io_buffer". */ |
12 | static LIST_HEAD(tomoyo_io_buffer_list); | |
13 | /* Lock for protecting tomoyo_io_buffer_list. */ | |
14 | static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock); | |
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), | |
2066a361 | 25 | /* [TOMOYO_ID_CONDITION] = "struct tomoyo_condition"->size, */ |
2e503bbb TH |
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), | |
d58e0da8 | 39 | [TOMOYO_TYPE_ENV_ACL] = sizeof(struct tomoyo_env_acl), |
2e503bbb TH |
40 | }; |
41 | ||
42 | /** | |
43 | * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not. | |
44 | * | |
45 | * @element: Pointer to "struct list_head". | |
46 | * | |
47 | * Returns true if @element is used by /sys/kernel/security/tomoyo/ users, | |
48 | * false otherwise. | |
49 | */ | |
50 | static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element) | |
51 | { | |
52 | struct tomoyo_io_buffer *head; | |
53 | bool in_use = false; | |
54 | ||
55 | spin_lock(&tomoyo_io_buffer_list_lock); | |
56 | list_for_each_entry(head, &tomoyo_io_buffer_list, list) { | |
57 | head->users++; | |
58 | spin_unlock(&tomoyo_io_buffer_list_lock); | |
59 | if (mutex_lock_interruptible(&head->io_sem)) { | |
60 | in_use = true; | |
61 | goto out; | |
62 | } | |
63 | if (head->r.domain == element || head->r.group == element || | |
64 | head->r.acl == element || &head->w.domain->list == element) | |
65 | in_use = true; | |
66 | mutex_unlock(&head->io_sem); | |
67 | out: | |
68 | spin_lock(&tomoyo_io_buffer_list_lock); | |
69 | head->users--; | |
70 | if (in_use) | |
71 | break; | |
72 | } | |
73 | spin_unlock(&tomoyo_io_buffer_list_lock); | |
74 | return in_use; | |
75 | } | |
76 | ||
77 | /** | |
78 | * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not. | |
79 | * | |
80 | * @string: String to check. | |
81 | * @size: Memory allocated for @string . | |
82 | * | |
83 | * Returns true if @string is used by /sys/kernel/security/tomoyo/ users, | |
84 | * false otherwise. | |
85 | */ | |
86 | static bool tomoyo_name_used_by_io_buffer(const char *string, | |
87 | const size_t size) | |
88 | { | |
89 | struct tomoyo_io_buffer *head; | |
90 | bool in_use = false; | |
91 | ||
92 | spin_lock(&tomoyo_io_buffer_list_lock); | |
93 | list_for_each_entry(head, &tomoyo_io_buffer_list, list) { | |
94 | int i; | |
95 | head->users++; | |
96 | spin_unlock(&tomoyo_io_buffer_list_lock); | |
97 | if (mutex_lock_interruptible(&head->io_sem)) { | |
98 | in_use = true; | |
99 | goto out; | |
100 | } | |
101 | for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) { | |
102 | const char *w = head->r.w[i]; | |
103 | if (w < string || w > string + size) | |
104 | continue; | |
105 | in_use = true; | |
106 | break; | |
107 | } | |
108 | mutex_unlock(&head->io_sem); | |
109 | out: | |
110 | spin_lock(&tomoyo_io_buffer_list_lock); | |
111 | head->users--; | |
112 | if (in_use) | |
113 | break; | |
114 | } | |
115 | spin_unlock(&tomoyo_io_buffer_list_lock); | |
116 | return in_use; | |
117 | } | |
118 | ||
119 | /* Structure for garbage collection. */ | |
e2bf6907 | 120 | struct tomoyo_gc { |
847b173e | 121 | struct list_head list; |
0df7e8b8 | 122 | enum tomoyo_policy_id type; |
2e503bbb | 123 | size_t size; |
e79acf0e | 124 | struct list_head *element; |
847b173e | 125 | }; |
2e503bbb TH |
126 | /* List of entries to be deleted. */ |
127 | static LIST_HEAD(tomoyo_gc_list); | |
128 | /* Length of tomoyo_gc_list. */ | |
129 | static int tomoyo_gc_list_len; | |
847b173e | 130 | |
0df7e8b8 TH |
131 | /** |
132 | * tomoyo_add_to_gc - Add an entry to to be deleted list. | |
133 | * | |
134 | * @type: One of values in "enum tomoyo_policy_id". | |
135 | * @element: Pointer to "struct list_head". | |
136 | * | |
137 | * Returns true on success, false otherwise. | |
138 | * | |
139 | * Caller holds tomoyo_policy_lock mutex. | |
140 | * | |
141 | * Adding an entry needs kmalloc(). Thus, if we try to add thousands of | |
142 | * entries at once, it will take too long time. Thus, do not add more than 128 | |
143 | * entries per a scan. But to be able to handle worst case where all entries | |
144 | * are in-use, we accept one more entry per a scan. | |
145 | * | |
146 | * If we use singly linked list using "struct list_head"->prev (which is | |
147 | * LIST_POISON2), we can avoid kmalloc(). | |
148 | */ | |
e79acf0e | 149 | static bool tomoyo_add_to_gc(const int type, struct list_head *element) |
847b173e | 150 | { |
e2bf6907 | 151 | struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
847b173e TH |
152 | if (!entry) |
153 | return false; | |
154 | entry->type = type; | |
2e503bbb TH |
155 | if (type == TOMOYO_ID_ACL) |
156 | entry->size = tomoyo_acl_size[ | |
157 | container_of(element, | |
158 | typeof(struct tomoyo_acl_info), | |
159 | list)->type]; | |
160 | else if (type == TOMOYO_ID_NAME) | |
161 | entry->size = strlen(container_of(element, | |
162 | typeof(struct tomoyo_name), | |
163 | head.list)->entry.name) + 1; | |
2066a361 TH |
164 | else if (type == TOMOYO_ID_CONDITION) |
165 | entry->size = | |
166 | container_of(element, typeof(struct tomoyo_condition), | |
167 | head.list)->size; | |
2e503bbb TH |
168 | else |
169 | entry->size = tomoyo_element_size[type]; | |
847b173e | 170 | entry->element = element; |
2e503bbb | 171 | list_add(&entry->list, &tomoyo_gc_list); |
e79acf0e | 172 | list_del_rcu(element); |
2e503bbb TH |
173 | return tomoyo_gc_list_len++ < 128; |
174 | } | |
175 | ||
176 | /** | |
177 | * tomoyo_element_linked_by_gc - Validate next element of an entry. | |
178 | * | |
179 | * @element: Pointer to an element. | |
180 | * @size: Size of @element in byte. | |
181 | * | |
182 | * Returns true if @element is linked by other elements in the garbage | |
183 | * collector's queue, false otherwise. | |
184 | */ | |
185 | static bool tomoyo_element_linked_by_gc(const u8 *element, const size_t size) | |
186 | { | |
187 | struct tomoyo_gc *p; | |
188 | list_for_each_entry(p, &tomoyo_gc_list, list) { | |
189 | const u8 *ptr = (const u8 *) p->element->next; | |
190 | if (ptr < element || element + size < ptr) | |
191 | continue; | |
192 | return true; | |
193 | } | |
194 | return false; | |
847b173e TH |
195 | } |
196 | ||
0df7e8b8 TH |
197 | /** |
198 | * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control". | |
199 | * | |
200 | * @element: Pointer to "struct list_head". | |
201 | * | |
202 | * Returns nothing. | |
203 | */ | |
5448ec4f | 204 | static void tomoyo_del_transition_control(struct list_head *element) |
847b173e | 205 | { |
5448ec4f | 206 | struct tomoyo_transition_control *ptr = |
e79acf0e | 207 | container_of(element, typeof(*ptr), head.list); |
847b173e TH |
208 | tomoyo_put_name(ptr->domainname); |
209 | tomoyo_put_name(ptr->program); | |
210 | } | |
211 | ||
0df7e8b8 TH |
212 | /** |
213 | * tomoyo_del_aggregator - Delete members in "struct tomoyo_aggregator". | |
214 | * | |
215 | * @element: Pointer to "struct list_head". | |
216 | * | |
217 | * Returns nothing. | |
218 | */ | |
e79acf0e | 219 | static void tomoyo_del_aggregator(struct list_head *element) |
1084307c | 220 | { |
e2bf6907 | 221 | struct tomoyo_aggregator *ptr = |
e79acf0e | 222 | container_of(element, typeof(*ptr), head.list); |
1084307c TH |
223 | tomoyo_put_name(ptr->original_name); |
224 | tomoyo_put_name(ptr->aggregated_name); | |
225 | } | |
226 | ||
0df7e8b8 TH |
227 | /** |
228 | * tomoyo_del_manager - Delete members in "struct tomoyo_manager". | |
229 | * | |
230 | * @element: Pointer to "struct list_head". | |
231 | * | |
232 | * Returns nothing. | |
233 | */ | |
e79acf0e | 234 | static void tomoyo_del_manager(struct list_head *element) |
847b173e | 235 | { |
e2bf6907 | 236 | struct tomoyo_manager *ptr = |
e79acf0e | 237 | container_of(element, typeof(*ptr), head.list); |
847b173e TH |
238 | tomoyo_put_name(ptr->manager); |
239 | } | |
240 | ||
0df7e8b8 TH |
241 | /** |
242 | * tomoyo_del_acl - Delete members in "struct tomoyo_acl_info". | |
243 | * | |
244 | * @element: Pointer to "struct list_head". | |
245 | * | |
246 | * Returns nothing. | |
247 | */ | |
e79acf0e | 248 | static void tomoyo_del_acl(struct list_head *element) |
847b173e | 249 | { |
e79acf0e TH |
250 | struct tomoyo_acl_info *acl = |
251 | container_of(element, typeof(*acl), list); | |
2066a361 | 252 | tomoyo_put_condition(acl->cond); |
847b173e | 253 | switch (acl->type) { |
7ef61233 | 254 | case TOMOYO_TYPE_PATH_ACL: |
847b173e | 255 | { |
7ef61233 | 256 | struct tomoyo_path_acl *entry |
847b173e | 257 | = container_of(acl, typeof(*entry), head); |
7762fbff | 258 | tomoyo_put_name_union(&entry->name); |
847b173e TH |
259 | } |
260 | break; | |
7ef61233 | 261 | case TOMOYO_TYPE_PATH2_ACL: |
847b173e | 262 | { |
7ef61233 | 263 | struct tomoyo_path2_acl *entry |
847b173e | 264 | = container_of(acl, typeof(*entry), head); |
7762fbff TH |
265 | tomoyo_put_name_union(&entry->name1); |
266 | tomoyo_put_name_union(&entry->name2); | |
847b173e TH |
267 | } |
268 | break; | |
a1f9bb6a TH |
269 | case TOMOYO_TYPE_PATH_NUMBER_ACL: |
270 | { | |
271 | struct tomoyo_path_number_acl *entry | |
272 | = container_of(acl, typeof(*entry), head); | |
273 | tomoyo_put_name_union(&entry->name); | |
274 | tomoyo_put_number_union(&entry->number); | |
275 | } | |
276 | break; | |
75093152 | 277 | case TOMOYO_TYPE_MKDEV_ACL: |
a1f9bb6a | 278 | { |
75093152 | 279 | struct tomoyo_mkdev_acl *entry |
a1f9bb6a TH |
280 | = container_of(acl, typeof(*entry), head); |
281 | tomoyo_put_name_union(&entry->name); | |
282 | tomoyo_put_number_union(&entry->mode); | |
283 | tomoyo_put_number_union(&entry->major); | |
284 | tomoyo_put_number_union(&entry->minor); | |
285 | } | |
286 | break; | |
2106ccd9 TH |
287 | case TOMOYO_TYPE_MOUNT_ACL: |
288 | { | |
289 | struct tomoyo_mount_acl *entry | |
290 | = container_of(acl, typeof(*entry), head); | |
291 | tomoyo_put_name_union(&entry->dev_name); | |
292 | tomoyo_put_name_union(&entry->dir_name); | |
293 | tomoyo_put_name_union(&entry->fs_type); | |
294 | tomoyo_put_number_union(&entry->flags); | |
295 | } | |
296 | break; | |
d58e0da8 TH |
297 | case TOMOYO_TYPE_ENV_ACL: |
298 | { | |
299 | struct tomoyo_env_acl *entry = | |
300 | container_of(acl, typeof(*entry), head); | |
301 | ||
302 | tomoyo_put_name(entry->env); | |
303 | } | |
304 | break; | |
847b173e TH |
305 | } |
306 | } | |
307 | ||
2e503bbb TH |
308 | /** |
309 | * tomoyo_del_domain - Delete members in "struct tomoyo_domain_info". | |
310 | * | |
311 | * @element: Pointer to "struct list_head". | |
312 | * | |
313 | * Returns true if deleted, false otherwise. | |
314 | */ | |
e79acf0e | 315 | static bool tomoyo_del_domain(struct list_head *element) |
847b173e | 316 | { |
e79acf0e TH |
317 | struct tomoyo_domain_info *domain = |
318 | container_of(element, typeof(*domain), list); | |
847b173e TH |
319 | struct tomoyo_acl_info *acl; |
320 | struct tomoyo_acl_info *tmp; | |
321 | /* | |
322 | * Since we don't protect whole execve() operation using SRCU, | |
323 | * we need to recheck domain->users at this point. | |
324 | * | |
325 | * (1) Reader starts SRCU section upon execve(). | |
326 | * (2) Reader traverses tomoyo_domain_list and finds this domain. | |
327 | * (3) Writer marks this domain as deleted. | |
328 | * (4) Garbage collector removes this domain from tomoyo_domain_list | |
329 | * because this domain is marked as deleted and used by nobody. | |
330 | * (5) Reader saves reference to this domain into | |
331 | * "struct linux_binprm"->cred->security . | |
332 | * (6) Reader finishes SRCU section, although execve() operation has | |
333 | * not finished yet. | |
334 | * (7) Garbage collector waits for SRCU synchronization. | |
335 | * (8) Garbage collector kfree() this domain because this domain is | |
336 | * used by nobody. | |
337 | * (9) Reader finishes execve() operation and restores this domain from | |
338 | * "struct linux_binprm"->cred->security. | |
339 | * | |
340 | * By updating domain->users at (5), we can solve this race problem | |
341 | * by rechecking domain->users at (8). | |
342 | */ | |
343 | if (atomic_read(&domain->users)) | |
344 | return false; | |
345 | list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) { | |
e79acf0e | 346 | tomoyo_del_acl(&acl->list); |
847b173e TH |
347 | tomoyo_memory_free(acl); |
348 | } | |
349 | tomoyo_put_name(domain->domainname); | |
350 | return true; | |
351 | } | |
352 | ||
2066a361 TH |
353 | /** |
354 | * tomoyo_del_condition - Delete members in "struct tomoyo_condition". | |
355 | * | |
356 | * @element: Pointer to "struct list_head". | |
357 | * | |
358 | * Returns nothing. | |
359 | */ | |
360 | void tomoyo_del_condition(struct list_head *element) | |
361 | { | |
362 | struct tomoyo_condition *cond = container_of(element, typeof(*cond), | |
363 | head.list); | |
364 | const u16 condc = cond->condc; | |
365 | const u16 numbers_count = cond->numbers_count; | |
2ca9bf45 | 366 | const u16 names_count = cond->names_count; |
5b636857 TH |
367 | const u16 argc = cond->argc; |
368 | const u16 envc = cond->envc; | |
2066a361 TH |
369 | unsigned int i; |
370 | const struct tomoyo_condition_element *condp | |
371 | = (const struct tomoyo_condition_element *) (cond + 1); | |
372 | struct tomoyo_number_union *numbers_p | |
373 | = (struct tomoyo_number_union *) (condp + condc); | |
2ca9bf45 TH |
374 | struct tomoyo_name_union *names_p |
375 | = (struct tomoyo_name_union *) (numbers_p + numbers_count); | |
5b636857 TH |
376 | const struct tomoyo_argv *argv |
377 | = (const struct tomoyo_argv *) (names_p + names_count); | |
378 | const struct tomoyo_envp *envp | |
379 | = (const struct tomoyo_envp *) (argv + argc); | |
2066a361 TH |
380 | for (i = 0; i < numbers_count; i++) |
381 | tomoyo_put_number_union(numbers_p++); | |
2ca9bf45 TH |
382 | for (i = 0; i < names_count; i++) |
383 | tomoyo_put_name_union(names_p++); | |
5b636857 TH |
384 | for (i = 0; i < argc; argv++, i++) |
385 | tomoyo_put_name(argv->value); | |
386 | for (i = 0; i < envc; envp++, i++) { | |
387 | tomoyo_put_name(envp->name); | |
388 | tomoyo_put_name(envp->value); | |
389 | } | |
2066a361 | 390 | } |
847b173e | 391 | |
0df7e8b8 TH |
392 | /** |
393 | * tomoyo_del_name - Delete members in "struct tomoyo_name". | |
394 | * | |
395 | * @element: Pointer to "struct list_head". | |
396 | * | |
397 | * Returns nothing. | |
398 | */ | |
e79acf0e | 399 | static void tomoyo_del_name(struct list_head *element) |
847b173e | 400 | { |
e2bf6907 | 401 | const struct tomoyo_name *ptr = |
0df7e8b8 | 402 | container_of(element, typeof(*ptr), head.list); |
847b173e TH |
403 | } |
404 | ||
0df7e8b8 TH |
405 | /** |
406 | * tomoyo_del_path_group - Delete members in "struct tomoyo_path_group". | |
407 | * | |
408 | * @element: Pointer to "struct list_head". | |
409 | * | |
410 | * Returns nothing. | |
411 | */ | |
a98aa4de | 412 | static void tomoyo_del_path_group(struct list_head *element) |
7762fbff | 413 | { |
a98aa4de | 414 | struct tomoyo_path_group *member = |
e79acf0e | 415 | container_of(element, typeof(*member), head.list); |
7762fbff TH |
416 | tomoyo_put_name(member->member_name); |
417 | } | |
418 | ||
0df7e8b8 TH |
419 | /** |
420 | * tomoyo_del_group - Delete "struct tomoyo_group". | |
421 | * | |
422 | * @element: Pointer to "struct list_head". | |
423 | * | |
424 | * Returns nothing. | |
425 | */ | |
a98aa4de | 426 | static void tomoyo_del_group(struct list_head *element) |
7762fbff | 427 | { |
a98aa4de | 428 | struct tomoyo_group *group = |
0df7e8b8 | 429 | container_of(element, typeof(*group), head.list); |
7762fbff TH |
430 | tomoyo_put_name(group->group_name); |
431 | } | |
432 | ||
0df7e8b8 TH |
433 | /** |
434 | * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group". | |
435 | * | |
436 | * @element: Pointer to "struct list_head". | |
437 | * | |
438 | * Returns nothing. | |
439 | */ | |
e79acf0e | 440 | static void tomoyo_del_number_group(struct list_head *element) |
4c3e9e2d | 441 | { |
a98aa4de TH |
442 | struct tomoyo_number_group *member = |
443 | container_of(element, typeof(*member), head.list); | |
4c3e9e2d TH |
444 | } |
445 | ||
0df7e8b8 TH |
446 | /** |
447 | * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head". | |
448 | * | |
449 | * @id: One of values in "enum tomoyo_policy_id". | |
450 | * @member_list: Pointer to "struct list_head". | |
451 | * | |
452 | * Returns true if some elements are deleted, false otherwise. | |
453 | */ | |
454 | static bool tomoyo_collect_member(const enum tomoyo_policy_id id, | |
455 | struct list_head *member_list) | |
d2f8b234 TH |
456 | { |
457 | struct tomoyo_acl_head *member; | |
458 | list_for_each_entry(member, member_list, list) { | |
459 | if (!member->is_deleted) | |
460 | continue; | |
461 | if (!tomoyo_add_to_gc(id, &member->list)) | |
462 | return false; | |
d2f8b234 | 463 | } |
0f2a55d5 | 464 | return true; |
d2f8b234 TH |
465 | } |
466 | ||
32997144 TH |
467 | /** |
468 | * tomoyo_collect_acl - Delete elements in "struct tomoyo_domain_info". | |
469 | * | |
470 | * @list: Pointer to "struct list_head". | |
471 | * | |
472 | * Returns true if some elements are deleted, false otherwise. | |
473 | */ | |
474 | static bool tomoyo_collect_acl(struct list_head *list) | |
d2f8b234 TH |
475 | { |
476 | struct tomoyo_acl_info *acl; | |
32997144 | 477 | list_for_each_entry(acl, list, list) { |
d2f8b234 TH |
478 | if (!acl->is_deleted) |
479 | continue; | |
480 | if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list)) | |
481 | return false; | |
d2f8b234 TH |
482 | } |
483 | return true; | |
484 | } | |
485 | ||
0df7e8b8 TH |
486 | /** |
487 | * tomoyo_collect_entry - Scan lists for deleted elements. | |
488 | * | |
489 | * Returns nothing. | |
490 | */ | |
847b173e TH |
491 | static void tomoyo_collect_entry(void) |
492 | { | |
d2f8b234 | 493 | int i; |
bd03a3e4 TH |
494 | enum tomoyo_policy_id id; |
495 | struct tomoyo_policy_namespace *ns; | |
496 | int idx; | |
29282381 TH |
497 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
498 | return; | |
bd03a3e4 | 499 | idx = tomoyo_read_lock(); |
847b173e TH |
500 | { |
501 | struct tomoyo_domain_info *domain; | |
502 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { | |
32997144 | 503 | if (!tomoyo_collect_acl(&domain->acl_info_list)) |
d2f8b234 | 504 | goto unlock; |
847b173e TH |
505 | if (!domain->is_deleted || atomic_read(&domain->users)) |
506 | continue; | |
507 | /* | |
508 | * Nobody is referring this domain. But somebody may | |
509 | * refer this domain after successful execve(). | |
510 | * We recheck domain->users after SRCU synchronization. | |
511 | */ | |
e79acf0e | 512 | if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list)) |
d2f8b234 | 513 | goto unlock; |
847b173e TH |
514 | } |
515 | } | |
bd03a3e4 TH |
516 | list_for_each_entry_rcu(ns, &tomoyo_namespace_list, namespace_list) { |
517 | for (id = 0; id < TOMOYO_MAX_POLICY; id++) | |
518 | if (!tomoyo_collect_member(id, &ns->policy_list[id])) | |
d2f8b234 | 519 | goto unlock; |
bd03a3e4 TH |
520 | for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++) |
521 | if (!tomoyo_collect_acl(&ns->acl_group[i])) | |
522 | goto unlock; | |
523 | for (i = 0; i < TOMOYO_MAX_GROUP; i++) { | |
524 | struct list_head *list = &ns->group_list[i]; | |
525 | struct tomoyo_group *group; | |
526 | switch (i) { | |
527 | case 0: | |
528 | id = TOMOYO_ID_PATH_GROUP; | |
529 | break; | |
530 | default: | |
531 | id = TOMOYO_ID_NUMBER_GROUP; | |
532 | break; | |
533 | } | |
534 | list_for_each_entry(group, list, head.list) { | |
535 | if (!tomoyo_collect_member | |
536 | (id, &group->member_list)) | |
537 | goto unlock; | |
538 | if (!list_empty(&group->member_list) || | |
539 | atomic_read(&group->head.users)) | |
540 | continue; | |
541 | if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, | |
542 | &group->head.list)) | |
543 | goto unlock; | |
544 | } | |
847b173e TH |
545 | } |
546 | } | |
2066a361 TH |
547 | id = TOMOYO_ID_CONDITION; |
548 | for (i = 0; i < TOMOYO_MAX_HASH + 1; i++) { | |
549 | struct list_head *list = !i ? | |
550 | &tomoyo_condition_list : &tomoyo_name_list[i - 1]; | |
bd03a3e4 TH |
551 | struct tomoyo_shared_acl_head *ptr; |
552 | list_for_each_entry(ptr, list, list) { | |
553 | if (atomic_read(&ptr->users)) | |
4c3e9e2d | 554 | continue; |
2066a361 | 555 | if (!tomoyo_add_to_gc(id, &ptr->list)) |
d2f8b234 | 556 | goto unlock; |
4c3e9e2d | 557 | } |
2066a361 | 558 | id = TOMOYO_ID_NAME; |
4c3e9e2d | 559 | } |
bd03a3e4 TH |
560 | unlock: |
561 | tomoyo_read_unlock(idx); | |
29282381 | 562 | mutex_unlock(&tomoyo_policy_lock); |
847b173e TH |
563 | } |
564 | ||
2e503bbb TH |
565 | /** |
566 | * tomoyo_kfree_entry - Delete entries in tomoyo_gc_list. | |
567 | * | |
568 | * Returns true if some entries were kfree()d, false otherwise. | |
569 | */ | |
570 | static bool tomoyo_kfree_entry(void) | |
847b173e | 571 | { |
e2bf6907 TH |
572 | struct tomoyo_gc *p; |
573 | struct tomoyo_gc *tmp; | |
2e503bbb | 574 | bool result = false; |
847b173e | 575 | |
2e503bbb | 576 | list_for_each_entry_safe(p, tmp, &tomoyo_gc_list, list) { |
e79acf0e | 577 | struct list_head *element = p->element; |
2e503bbb TH |
578 | |
579 | /* | |
580 | * list_del_rcu() in tomoyo_add_to_gc() guarantees that the | |
581 | * list element became no longer reachable from the list which | |
582 | * the element was originally on (e.g. tomoyo_domain_list). | |
583 | * Also, synchronize_srcu() in tomoyo_gc_thread() guarantees | |
584 | * that the list element became no longer referenced by syscall | |
585 | * users. | |
586 | * | |
587 | * However, there are three users which may still be using the | |
588 | * list element. We need to defer until all of these users | |
589 | * forget the list element. | |
590 | * | |
591 | * Firstly, defer until "struct tomoyo_io_buffer"->r.{domain, | |
592 | * group,acl} and "struct tomoyo_io_buffer"->w.domain forget | |
593 | * the list element. | |
594 | */ | |
595 | if (tomoyo_struct_used_by_io_buffer(element)) | |
596 | continue; | |
597 | /* | |
598 | * Secondly, defer until all other elements in the | |
599 | * tomoyo_gc_list list forget the list element. | |
600 | */ | |
601 | if (tomoyo_element_linked_by_gc((const u8 *) element, p->size)) | |
602 | continue; | |
847b173e | 603 | switch (p->type) { |
5448ec4f TH |
604 | case TOMOYO_ID_TRANSITION_CONTROL: |
605 | tomoyo_del_transition_control(element); | |
847b173e | 606 | break; |
1084307c | 607 | case TOMOYO_ID_AGGREGATOR: |
e79acf0e | 608 | tomoyo_del_aggregator(element); |
1084307c | 609 | break; |
847b173e | 610 | case TOMOYO_ID_MANAGER: |
e79acf0e | 611 | tomoyo_del_manager(element); |
847b173e | 612 | break; |
2066a361 TH |
613 | case TOMOYO_ID_CONDITION: |
614 | tomoyo_del_condition(element); | |
615 | break; | |
847b173e | 616 | case TOMOYO_ID_NAME: |
2e503bbb TH |
617 | /* |
618 | * Thirdly, defer until all "struct tomoyo_io_buffer" | |
619 | * ->r.w[] forget the list element. | |
620 | */ | |
621 | if (tomoyo_name_used_by_io_buffer( | |
622 | container_of(element, typeof(struct tomoyo_name), | |
623 | head.list)->entry.name, p->size)) | |
624 | continue; | |
e79acf0e | 625 | tomoyo_del_name(element); |
847b173e TH |
626 | break; |
627 | case TOMOYO_ID_ACL: | |
e79acf0e | 628 | tomoyo_del_acl(element); |
847b173e TH |
629 | break; |
630 | case TOMOYO_ID_DOMAIN: | |
e79acf0e | 631 | if (!tomoyo_del_domain(element)) |
847b173e TH |
632 | continue; |
633 | break; | |
7762fbff | 634 | case TOMOYO_ID_PATH_GROUP: |
e79acf0e | 635 | tomoyo_del_path_group(element); |
7762fbff | 636 | break; |
a98aa4de TH |
637 | case TOMOYO_ID_GROUP: |
638 | tomoyo_del_group(element); | |
4c3e9e2d TH |
639 | break; |
640 | case TOMOYO_ID_NUMBER_GROUP: | |
e79acf0e | 641 | tomoyo_del_number_group(element); |
847b173e | 642 | break; |
0df7e8b8 TH |
643 | case TOMOYO_MAX_POLICY: |
644 | break; | |
847b173e | 645 | } |
e79acf0e | 646 | tomoyo_memory_free(element); |
847b173e TH |
647 | list_del(&p->list); |
648 | kfree(p); | |
2e503bbb TH |
649 | tomoyo_gc_list_len--; |
650 | result = true; | |
847b173e | 651 | } |
2e503bbb | 652 | return result; |
847b173e TH |
653 | } |
654 | ||
0df7e8b8 TH |
655 | /** |
656 | * tomoyo_gc_thread - Garbage collector thread function. | |
657 | * | |
658 | * @unused: Unused. | |
659 | * | |
660 | * In case OOM-killer choose this thread for termination, we create this thread | |
661 | * as a short live thread whenever /sys/kernel/security/tomoyo/ interface was | |
662 | * close()d. | |
663 | * | |
664 | * Returns 0. | |
665 | */ | |
847b173e TH |
666 | static int tomoyo_gc_thread(void *unused) |
667 | { | |
2e503bbb TH |
668 | /* Garbage collector thread is exclusive. */ |
669 | static DEFINE_MUTEX(tomoyo_gc_mutex); | |
670 | if (!mutex_trylock(&tomoyo_gc_mutex)) | |
671 | goto out; | |
09f464bf | 672 | |
2e503bbb TH |
673 | do { |
674 | tomoyo_collect_entry(); | |
675 | if (list_empty(&tomoyo_gc_list)) | |
676 | break; | |
677 | synchronize_srcu(&tomoyo_ss); | |
678 | } while (tomoyo_kfree_entry()); | |
679 | { | |
680 | struct tomoyo_io_buffer *head; | |
681 | struct tomoyo_io_buffer *tmp; | |
682 | ||
683 | spin_lock(&tomoyo_io_buffer_list_lock); | |
684 | list_for_each_entry_safe(head, tmp, &tomoyo_io_buffer_list, | |
685 | list) { | |
686 | if (head->users) | |
687 | continue; | |
688 | list_del(&head->list); | |
689 | kfree(head->read_buf); | |
690 | kfree(head->write_buf); | |
691 | kfree(head); | |
847b173e | 692 | } |
2e503bbb | 693 | spin_unlock(&tomoyo_io_buffer_list_lock); |
847b173e | 694 | } |
2e503bbb TH |
695 | mutex_unlock(&tomoyo_gc_mutex); |
696 | out: | |
697 | /* This acts as do_exit(0). */ | |
698 | return 0; | |
847b173e TH |
699 | } |
700 | ||
2e503bbb TH |
701 | /** |
702 | * tomoyo_notify_gc - Register/unregister /sys/kernel/security/tomoyo/ users. | |
703 | * | |
704 | * @head: Pointer to "struct tomoyo_io_buffer". | |
705 | * @is_register: True if register, false if unregister. | |
706 | * | |
707 | * Returns nothing. | |
708 | */ | |
709 | void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register) | |
847b173e | 710 | { |
2e503bbb TH |
711 | bool is_write = false; |
712 | ||
713 | spin_lock(&tomoyo_io_buffer_list_lock); | |
714 | if (is_register) { | |
715 | head->users = 1; | |
716 | list_add(&head->list, &tomoyo_io_buffer_list); | |
717 | } else { | |
718 | is_write = head->write_buf != NULL; | |
719 | if (!--head->users) { | |
720 | list_del(&head->list); | |
721 | kfree(head->read_buf); | |
722 | kfree(head->write_buf); | |
723 | kfree(head); | |
724 | } | |
725 | } | |
726 | spin_unlock(&tomoyo_io_buffer_list_lock); | |
727 | if (is_write) { | |
728 | struct task_struct *task = kthread_create(tomoyo_gc_thread, | |
729 | NULL, | |
730 | "GC for TOMOYO"); | |
731 | if (!IS_ERR(task)) | |
732 | wake_up_process(task); | |
733 | } | |
847b173e | 734 | } |