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