gc.c 20.7 KB
Newer Older
Tetsuo Handa's avatar
Tetsuo Handa committed
1
2
3
/*
 * security/tomoyo/gc.c
 *
Tetsuo Handa's avatar
Tetsuo Handa committed
4
 * Copyright (C) 2005-2011  NTT DATA CORPORATION
Tetsuo Handa's avatar
Tetsuo Handa committed
5
6
7
8
 */

#include "common.h"
#include <linux/kthread.h>
9
#include <linux/slab.h>
Tetsuo Handa's avatar
Tetsuo Handa committed
10

Tetsuo Handa's avatar
Tetsuo Handa committed
11
12
13
14
15
16
17
18
/* The list for "struct tomoyo_io_buffer". */
static LIST_HEAD(tomoyo_io_buffer_list);
/* Lock for protecting tomoyo_io_buffer_list. */
static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock);

/* Size of an element. */
static const u8 tomoyo_element_size[TOMOYO_MAX_POLICY] = {
	[TOMOYO_ID_GROUP] = sizeof(struct tomoyo_group),
19
	[TOMOYO_ID_ADDRESS_GROUP] = sizeof(struct tomoyo_address_group),
Tetsuo Handa's avatar
Tetsuo Handa committed
20
21
22
23
24
25
	[TOMOYO_ID_PATH_GROUP] = sizeof(struct tomoyo_path_group),
	[TOMOYO_ID_NUMBER_GROUP] = sizeof(struct tomoyo_number_group),
	[TOMOYO_ID_AGGREGATOR] = sizeof(struct tomoyo_aggregator),
	[TOMOYO_ID_TRANSITION_CONTROL] =
	sizeof(struct tomoyo_transition_control),
	[TOMOYO_ID_MANAGER] = sizeof(struct tomoyo_manager),
26
	/* [TOMOYO_ID_CONDITION] = "struct tomoyo_condition"->size, */
Tetsuo Handa's avatar
Tetsuo Handa committed
27
28
29
30
31
32
33
34
35
36
37
38
39
	/* [TOMOYO_ID_NAME] = "struct tomoyo_name"->size, */
	/* [TOMOYO_ID_ACL] =
	   tomoyo_acl_size["struct tomoyo_acl_info"->type], */
	[TOMOYO_ID_DOMAIN] = sizeof(struct tomoyo_domain_info),
};

/* Size of a domain ACL element. */
static const u8 tomoyo_acl_size[] = {
	[TOMOYO_TYPE_PATH_ACL] = sizeof(struct tomoyo_path_acl),
	[TOMOYO_TYPE_PATH2_ACL] = sizeof(struct tomoyo_path2_acl),
	[TOMOYO_TYPE_PATH_NUMBER_ACL] = sizeof(struct tomoyo_path_number_acl),
	[TOMOYO_TYPE_MKDEV_ACL] = sizeof(struct tomoyo_mkdev_acl),
	[TOMOYO_TYPE_MOUNT_ACL] = sizeof(struct tomoyo_mount_acl),
40
41
	[TOMOYO_TYPE_INET_ACL] = sizeof(struct tomoyo_inet_acl),
	[TOMOYO_TYPE_UNIX_ACL] = sizeof(struct tomoyo_unix_acl),
42
	[TOMOYO_TYPE_ENV_ACL] = sizeof(struct tomoyo_env_acl),
Tetsuo Handa's avatar
Tetsuo Handa committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
};

/**
 * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not.
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns true if @element is used by /sys/kernel/security/tomoyo/ users,
 * false otherwise.
 */
static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element)
{
	struct tomoyo_io_buffer *head;
	bool in_use = false;

	spin_lock(&tomoyo_io_buffer_list_lock);
	list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
		head->users++;
		spin_unlock(&tomoyo_io_buffer_list_lock);
		if (mutex_lock_interruptible(&head->io_sem)) {
			in_use = true;
			goto out;
		}
		if (head->r.domain == element || head->r.group == element ||
		    head->r.acl == element || &head->w.domain->list == element)
			in_use = true;
		mutex_unlock(&head->io_sem);
out:
		spin_lock(&tomoyo_io_buffer_list_lock);
		head->users--;
		if (in_use)
			break;
	}
	spin_unlock(&tomoyo_io_buffer_list_lock);
	return in_use;
}

/**
 * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not.
 *
 * @string: String to check.
 * @size:   Memory allocated for @string .
 *
 * Returns true if @string is used by /sys/kernel/security/tomoyo/ users,
 * false otherwise.
 */
static bool tomoyo_name_used_by_io_buffer(const char *string,
					  const size_t size)
{
	struct tomoyo_io_buffer *head;
	bool in_use = false;

	spin_lock(&tomoyo_io_buffer_list_lock);
	list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
		int i;
		head->users++;
		spin_unlock(&tomoyo_io_buffer_list_lock);
		if (mutex_lock_interruptible(&head->io_sem)) {
			in_use = true;
			goto out;
		}
		for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) {
			const char *w = head->r.w[i];
			if (w < string || w > string + size)
				continue;
			in_use = true;
			break;
		}
		mutex_unlock(&head->io_sem);
out:
		spin_lock(&tomoyo_io_buffer_list_lock);
		head->users--;
		if (in_use)
			break;
	}
	spin_unlock(&tomoyo_io_buffer_list_lock);
	return in_use;
}

/* Structure for garbage collection. */
Tetsuo Handa's avatar
Tetsuo Handa committed
123
struct tomoyo_gc {
Tetsuo Handa's avatar
Tetsuo Handa committed
124
	struct list_head list;
Tetsuo Handa's avatar
Tetsuo Handa committed
125
	enum tomoyo_policy_id type;
Tetsuo Handa's avatar
Tetsuo Handa committed
126
	size_t size;
127
	struct list_head *element;
Tetsuo Handa's avatar
Tetsuo Handa committed
128
};
Tetsuo Handa's avatar
Tetsuo Handa committed
129
130
131
132
/* List of entries to be deleted. */
static LIST_HEAD(tomoyo_gc_list);
/* Length of tomoyo_gc_list. */
static int tomoyo_gc_list_len;
Tetsuo Handa's avatar
Tetsuo Handa committed
133

Tetsuo Handa's avatar
Tetsuo Handa committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 * tomoyo_add_to_gc - Add an entry to to be deleted list.
 *
 * @type:    One of values in "enum tomoyo_policy_id".
 * @element: Pointer to "struct list_head".
 *
 * Returns true on success, false otherwise.
 *
 * Caller holds tomoyo_policy_lock mutex.
 *
 * Adding an entry needs kmalloc(). Thus, if we try to add thousands of
 * entries at once, it will take too long time. Thus, do not add more than 128
 * entries per a scan. But to be able to handle worst case where all entries
 * are in-use, we accept one more entry per a scan.
 *
 * If we use singly linked list using "struct list_head"->prev (which is
 * LIST_POISON2), we can avoid kmalloc().
 */
152
static bool tomoyo_add_to_gc(const int type, struct list_head *element)
Tetsuo Handa's avatar
Tetsuo Handa committed
153
{
Tetsuo Handa's avatar
Tetsuo Handa committed
154
	struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Tetsuo Handa's avatar
Tetsuo Handa committed
155
156
157
	if (!entry)
		return false;
	entry->type = type;
Tetsuo Handa's avatar
Tetsuo Handa committed
158
159
160
161
162
163
164
165
166
	if (type == TOMOYO_ID_ACL)
		entry->size = tomoyo_acl_size[
			      container_of(element,
					   typeof(struct tomoyo_acl_info),
					   list)->type];
	else if (type == TOMOYO_ID_NAME)
		entry->size = strlen(container_of(element,
						  typeof(struct tomoyo_name),
						  head.list)->entry.name) + 1;
167
168
169
170
	else if (type == TOMOYO_ID_CONDITION)
		entry->size =
			container_of(element, typeof(struct tomoyo_condition),
				     head.list)->size;
Tetsuo Handa's avatar
Tetsuo Handa committed
171
172
	else
		entry->size = tomoyo_element_size[type];
Tetsuo Handa's avatar
Tetsuo Handa committed
173
	entry->element = element;
Tetsuo Handa's avatar
Tetsuo Handa committed
174
	list_add(&entry->list, &tomoyo_gc_list);
175
	list_del_rcu(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	return tomoyo_gc_list_len++ < 128;
}

/**
 * tomoyo_element_linked_by_gc - Validate next element of an entry.
 *
 * @element: Pointer to an element.
 * @size:    Size of @element in byte.
 *
 * Returns true if @element is linked by other elements in the garbage
 * collector's queue, false otherwise.
 */
static bool tomoyo_element_linked_by_gc(const u8 *element, const size_t size)
{
	struct tomoyo_gc *p;
	list_for_each_entry(p, &tomoyo_gc_list, list) {
		const u8 *ptr = (const u8 *) p->element->next;
		if (ptr < element || element + size < ptr)
			continue;
		return true;
	}
	return false;
Tetsuo Handa's avatar
Tetsuo Handa committed
198
199
}

Tetsuo Handa's avatar
Tetsuo Handa committed
200
201
202
203
204
205
206
/**
 * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
207
static void tomoyo_del_transition_control(struct list_head *element)
Tetsuo Handa's avatar
Tetsuo Handa committed
208
{
209
	struct tomoyo_transition_control *ptr =
210
		container_of(element, typeof(*ptr), head.list);
Tetsuo Handa's avatar
Tetsuo Handa committed
211
212
213
214
	tomoyo_put_name(ptr->domainname);
	tomoyo_put_name(ptr->program);
}

Tetsuo Handa's avatar
Tetsuo Handa committed
215
216
217
218
219
220
221
/**
 * tomoyo_del_aggregator - Delete members in "struct tomoyo_aggregator".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
222
static void tomoyo_del_aggregator(struct list_head *element)
223
{
Tetsuo Handa's avatar
Tetsuo Handa committed
224
	struct tomoyo_aggregator *ptr =
225
		container_of(element, typeof(*ptr), head.list);
226
227
228
229
	tomoyo_put_name(ptr->original_name);
	tomoyo_put_name(ptr->aggregated_name);
}

Tetsuo Handa's avatar
Tetsuo Handa committed
230
231
232
233
234
235
236
/**
 * tomoyo_del_manager - Delete members in "struct tomoyo_manager".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
237
static void tomoyo_del_manager(struct list_head *element)
Tetsuo Handa's avatar
Tetsuo Handa committed
238
{
Tetsuo Handa's avatar
Tetsuo Handa committed
239
	struct tomoyo_manager *ptr =
240
		container_of(element, typeof(*ptr), head.list);
Tetsuo Handa's avatar
Tetsuo Handa committed
241
242
243
	tomoyo_put_name(ptr->manager);
}

Tetsuo Handa's avatar
Tetsuo Handa committed
244
245
246
247
248
249
250
/**
 * tomoyo_del_acl - Delete members in "struct tomoyo_acl_info".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
251
static void tomoyo_del_acl(struct list_head *element)
Tetsuo Handa's avatar
Tetsuo Handa committed
252
{
253
254
	struct tomoyo_acl_info *acl =
		container_of(element, typeof(*acl), list);
255
	tomoyo_put_condition(acl->cond);
Tetsuo Handa's avatar
Tetsuo Handa committed
256
	switch (acl->type) {
Tetsuo Handa's avatar
Tetsuo Handa committed
257
	case TOMOYO_TYPE_PATH_ACL:
Tetsuo Handa's avatar
Tetsuo Handa committed
258
		{
Tetsuo Handa's avatar
Tetsuo Handa committed
259
			struct tomoyo_path_acl *entry
Tetsuo Handa's avatar
Tetsuo Handa committed
260
				= container_of(acl, typeof(*entry), head);
261
			tomoyo_put_name_union(&entry->name);
Tetsuo Handa's avatar
Tetsuo Handa committed
262
263
		}
		break;
Tetsuo Handa's avatar
Tetsuo Handa committed
264
	case TOMOYO_TYPE_PATH2_ACL:
Tetsuo Handa's avatar
Tetsuo Handa committed
265
		{
Tetsuo Handa's avatar
Tetsuo Handa committed
266
			struct tomoyo_path2_acl *entry
Tetsuo Handa's avatar
Tetsuo Handa committed
267
				= container_of(acl, typeof(*entry), head);
268
269
			tomoyo_put_name_union(&entry->name1);
			tomoyo_put_name_union(&entry->name2);
Tetsuo Handa's avatar
Tetsuo Handa committed
270
271
		}
		break;
272
273
274
275
276
277
278
279
	case TOMOYO_TYPE_PATH_NUMBER_ACL:
		{
			struct tomoyo_path_number_acl *entry
				= container_of(acl, typeof(*entry), head);
			tomoyo_put_name_union(&entry->name);
			tomoyo_put_number_union(&entry->number);
		}
		break;
Tetsuo Handa's avatar
Tetsuo Handa committed
280
	case TOMOYO_TYPE_MKDEV_ACL:
281
		{
Tetsuo Handa's avatar
Tetsuo Handa committed
282
			struct tomoyo_mkdev_acl *entry
283
284
285
286
287
288
289
				= container_of(acl, typeof(*entry), head);
			tomoyo_put_name_union(&entry->name);
			tomoyo_put_number_union(&entry->mode);
			tomoyo_put_number_union(&entry->major);
			tomoyo_put_number_union(&entry->minor);
		}
		break;
Tetsuo Handa's avatar
Tetsuo Handa committed
290
291
292
293
294
295
296
297
298
299
	case TOMOYO_TYPE_MOUNT_ACL:
		{
			struct tomoyo_mount_acl *entry
				= container_of(acl, typeof(*entry), head);
			tomoyo_put_name_union(&entry->dev_name);
			tomoyo_put_name_union(&entry->dir_name);
			tomoyo_put_name_union(&entry->fs_type);
			tomoyo_put_number_union(&entry->flags);
		}
		break;
300
301
302
303
304
305
306
307
	case TOMOYO_TYPE_ENV_ACL:
		{
			struct tomoyo_env_acl *entry =
				container_of(acl, typeof(*entry), head);

			tomoyo_put_name(entry->env);
		}
		break;
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	case TOMOYO_TYPE_INET_ACL:
		{
			struct tomoyo_inet_acl *entry =
				container_of(acl, typeof(*entry), head);

			tomoyo_put_group(entry->address.group);
			tomoyo_put_number_union(&entry->port);
		}
		break;
	case TOMOYO_TYPE_UNIX_ACL:
		{
			struct tomoyo_unix_acl *entry =
				container_of(acl, typeof(*entry), head);

			tomoyo_put_name_union(&entry->name);
		}
		break;
Tetsuo Handa's avatar
Tetsuo Handa committed
325
326
327
	}
}

Tetsuo Handa's avatar
Tetsuo Handa committed
328
329
330
331
332
333
334
/**
 * tomoyo_del_domain - Delete members in "struct tomoyo_domain_info".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns true if deleted, false otherwise.
 */
335
static bool tomoyo_del_domain(struct list_head *element)
Tetsuo Handa's avatar
Tetsuo Handa committed
336
{
337
338
	struct tomoyo_domain_info *domain =
		container_of(element, typeof(*domain), list);
Tetsuo Handa's avatar
Tetsuo Handa committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	struct tomoyo_acl_info *acl;
	struct tomoyo_acl_info *tmp;
	/*
	 * Since we don't protect whole execve() operation using SRCU,
	 * we need to recheck domain->users at this point.
	 *
	 * (1) Reader starts SRCU section upon execve().
	 * (2) Reader traverses tomoyo_domain_list and finds this domain.
	 * (3) Writer marks this domain as deleted.
	 * (4) Garbage collector removes this domain from tomoyo_domain_list
	 *     because this domain is marked as deleted and used by nobody.
	 * (5) Reader saves reference to this domain into
	 *     "struct linux_binprm"->cred->security .
	 * (6) Reader finishes SRCU section, although execve() operation has
	 *     not finished yet.
	 * (7) Garbage collector waits for SRCU synchronization.
	 * (8) Garbage collector kfree() this domain because this domain is
	 *     used by nobody.
	 * (9) Reader finishes execve() operation and restores this domain from
	 *     "struct linux_binprm"->cred->security.
	 *
	 * By updating domain->users at (5), we can solve this race problem
	 * by rechecking domain->users at (8).
	 */
	if (atomic_read(&domain->users))
		return false;
	list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
366
		tomoyo_del_acl(&acl->list);
Tetsuo Handa's avatar
Tetsuo Handa committed
367
368
369
370
371
372
		tomoyo_memory_free(acl);
	}
	tomoyo_put_name(domain->domainname);
	return true;
}

373
374
375
376
377
378
379
380
381
382
383
384
385
/**
 * tomoyo_del_condition - Delete members in "struct tomoyo_condition".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
void tomoyo_del_condition(struct list_head *element)
{
	struct tomoyo_condition *cond = container_of(element, typeof(*cond),
						     head.list);
	const u16 condc = cond->condc;
	const u16 numbers_count = cond->numbers_count;
386
	const u16 names_count = cond->names_count;
387
388
	const u16 argc = cond->argc;
	const u16 envc = cond->envc;
389
390
391
392
393
	unsigned int i;
	const struct tomoyo_condition_element *condp
		= (const struct tomoyo_condition_element *) (cond + 1);
	struct tomoyo_number_union *numbers_p
		= (struct tomoyo_number_union *) (condp + condc);
394
395
	struct tomoyo_name_union *names_p
		= (struct tomoyo_name_union *) (numbers_p + numbers_count);
396
397
398
399
	const struct tomoyo_argv *argv
		= (const struct tomoyo_argv *) (names_p + names_count);
	const struct tomoyo_envp *envp
		= (const struct tomoyo_envp *) (argv + argc);
400
401
	for (i = 0; i < numbers_count; i++)
		tomoyo_put_number_union(numbers_p++);
402
403
	for (i = 0; i < names_count; i++)
		tomoyo_put_name_union(names_p++);
404
405
406
407
408
409
	for (i = 0; i < argc; argv++, i++)
		tomoyo_put_name(argv->value);
	for (i = 0; i < envc; envp++, i++) {
		tomoyo_put_name(envp->name);
		tomoyo_put_name(envp->value);
	}
410
}
Tetsuo Handa's avatar
Tetsuo Handa committed
411

Tetsuo Handa's avatar
Tetsuo Handa committed
412
413
414
415
416
417
418
/**
 * tomoyo_del_name - Delete members in "struct tomoyo_name".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
419
static void tomoyo_del_name(struct list_head *element)
Tetsuo Handa's avatar
Tetsuo Handa committed
420
{
Tetsuo Handa's avatar
Tetsuo Handa committed
421
	const struct tomoyo_name *ptr =
Tetsuo Handa's avatar
Tetsuo Handa committed
422
		container_of(element, typeof(*ptr), head.list);
Tetsuo Handa's avatar
Tetsuo Handa committed
423
424
}

Tetsuo Handa's avatar
Tetsuo Handa committed
425
426
427
428
429
430
431
/**
 * tomoyo_del_path_group - Delete members in "struct tomoyo_path_group".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
432
static void tomoyo_del_path_group(struct list_head *element)
433
{
434
	struct tomoyo_path_group *member =
435
		container_of(element, typeof(*member), head.list);
436
437
438
	tomoyo_put_name(member->member_name);
}

Tetsuo Handa's avatar
Tetsuo Handa committed
439
440
441
442
443
444
445
/**
 * tomoyo_del_group - Delete "struct tomoyo_group".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
446
static void tomoyo_del_group(struct list_head *element)
447
{
448
	struct tomoyo_group *group =
Tetsuo Handa's avatar
Tetsuo Handa committed
449
		container_of(element, typeof(*group), head.list);
450
451
452
	tomoyo_put_name(group->group_name);
}

453
454
455
456
457
458
459
460
461
462
463
464
/**
 * tomoyo_del_address_group - Delete members in "struct tomoyo_address_group".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
static inline void tomoyo_del_address_group(struct list_head *element)
{
	/* Nothing to do. */
}

Tetsuo Handa's avatar
Tetsuo Handa committed
465
466
467
468
469
470
471
/**
 * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group".
 *
 * @element: Pointer to "struct list_head".
 *
 * Returns nothing.
 */
472
static void tomoyo_del_number_group(struct list_head *element)
473
{
474
475
	struct tomoyo_number_group *member =
		container_of(element, typeof(*member), head.list);
476
477
}

Tetsuo Handa's avatar
Tetsuo Handa committed
478
479
480
481
482
483
484
485
486
487
/**
 * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head".
 *
 * @id:          One of values in "enum tomoyo_policy_id".
 * @member_list: Pointer to "struct list_head".
 *
 * Returns true if some elements are deleted, false otherwise.
 */
static bool tomoyo_collect_member(const enum tomoyo_policy_id id,
				  struct list_head *member_list)
488
489
490
491
492
493
494
495
{
	struct tomoyo_acl_head *member;
	list_for_each_entry(member, member_list, list) {
		if (!member->is_deleted)
			continue;
		if (!tomoyo_add_to_gc(id, &member->list))
			return false;
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
496
	return true;
497
498
}

Tetsuo Handa's avatar
Tetsuo Handa committed
499
500
501
502
503
504
505
506
/**
 * tomoyo_collect_acl - Delete elements in "struct tomoyo_domain_info".
 *
 * @list: Pointer to "struct list_head".
 *
 * Returns true if some elements are deleted, false otherwise.
 */
static bool tomoyo_collect_acl(struct list_head *list)
507
508
{
	struct tomoyo_acl_info *acl;
Tetsuo Handa's avatar
Tetsuo Handa committed
509
	list_for_each_entry(acl, list, list) {
510
511
512
513
514
515
516
517
		if (!acl->is_deleted)
			continue;
		if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
			return false;
	}
	return true;
}

Tetsuo Handa's avatar
Tetsuo Handa committed
518
519
520
521
522
/**
 * tomoyo_collect_entry - Scan lists for deleted elements.
 *
 * Returns nothing.
 */
Tetsuo Handa's avatar
Tetsuo Handa committed
523
524
static void tomoyo_collect_entry(void)
{
525
	int i;
526
527
528
	enum tomoyo_policy_id id;
	struct tomoyo_policy_namespace *ns;
	int idx;
529
530
	if (mutex_lock_interruptible(&tomoyo_policy_lock))
		return;
531
	idx = tomoyo_read_lock();
Tetsuo Handa's avatar
Tetsuo Handa committed
532
533
534
	{
		struct tomoyo_domain_info *domain;
		list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handa's avatar
Tetsuo Handa committed
535
			if (!tomoyo_collect_acl(&domain->acl_info_list))
536
				goto unlock;
Tetsuo Handa's avatar
Tetsuo Handa committed
537
538
539
540
541
542
543
			if (!domain->is_deleted || atomic_read(&domain->users))
				continue;
			/*
			 * Nobody is referring this domain. But somebody may
			 * refer this domain after successful execve().
			 * We recheck domain->users after SRCU synchronization.
			 */
544
			if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
545
				goto unlock;
Tetsuo Handa's avatar
Tetsuo Handa committed
546
547
		}
	}
548
549
550
	list_for_each_entry_rcu(ns, &tomoyo_namespace_list, namespace_list) {
		for (id = 0; id < TOMOYO_MAX_POLICY; id++)
			if (!tomoyo_collect_member(id, &ns->policy_list[id]))
551
				goto unlock;
552
553
554
555
556
557
558
559
560
561
		for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++)
			if (!tomoyo_collect_acl(&ns->acl_group[i]))
				goto unlock;
		for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
			struct list_head *list = &ns->group_list[i];
			struct tomoyo_group *group;
			switch (i) {
			case 0:
				id = TOMOYO_ID_PATH_GROUP;
				break;
562
			case 1:
563
564
				id = TOMOYO_ID_NUMBER_GROUP;
				break;
565
566
567
			default:
				id = TOMOYO_ID_ADDRESS_GROUP;
				break;
568
569
570
571
572
573
574
575
576
577
578
579
			}
			list_for_each_entry(group, list, head.list) {
				if (!tomoyo_collect_member
				    (id, &group->member_list))
					goto unlock;
				if (!list_empty(&group->member_list) ||
				    atomic_read(&group->head.users))
					continue;
				if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
						      &group->head.list))
					goto unlock;
			}
Tetsuo Handa's avatar
Tetsuo Handa committed
580
581
		}
	}
582
583
584
585
	id = TOMOYO_ID_CONDITION;
	for (i = 0; i < TOMOYO_MAX_HASH + 1; i++) {
		struct list_head *list = !i ?
			&tomoyo_condition_list : &tomoyo_name_list[i - 1];
586
587
588
		struct tomoyo_shared_acl_head *ptr;
		list_for_each_entry(ptr, list, list) {
			if (atomic_read(&ptr->users))
589
				continue;
590
			if (!tomoyo_add_to_gc(id, &ptr->list))
591
				goto unlock;
592
		}
593
		id = TOMOYO_ID_NAME;
594
	}
595
596
unlock:
	tomoyo_read_unlock(idx);
597
	mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa's avatar
Tetsuo Handa committed
598
599
}

Tetsuo Handa's avatar
Tetsuo Handa committed
600
601
602
603
604
605
/**
 * tomoyo_kfree_entry - Delete entries in tomoyo_gc_list.
 *
 * Returns true if some entries were kfree()d, false otherwise.
 */
static bool tomoyo_kfree_entry(void)
Tetsuo Handa's avatar
Tetsuo Handa committed
606
{
Tetsuo Handa's avatar
Tetsuo Handa committed
607
608
	struct tomoyo_gc *p;
	struct tomoyo_gc *tmp;
Tetsuo Handa's avatar
Tetsuo Handa committed
609
	bool result = false;
Tetsuo Handa's avatar
Tetsuo Handa committed
610

Tetsuo Handa's avatar
Tetsuo Handa committed
611
	list_for_each_entry_safe(p, tmp, &tomoyo_gc_list, list) {
612
		struct list_head *element = p->element;
Tetsuo Handa's avatar
Tetsuo Handa committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637

		/*
		 * list_del_rcu() in tomoyo_add_to_gc() guarantees that the
		 * list element became no longer reachable from the list which
		 * the element was originally on (e.g. tomoyo_domain_list).
		 * Also, synchronize_srcu() in tomoyo_gc_thread() guarantees
		 * that the list element became no longer referenced by syscall
		 * users.
		 *
		 * However, there are three users which may still be using the
		 * list element. We need to defer until all of these users
		 * forget the list element.
		 *
		 * Firstly, defer until "struct tomoyo_io_buffer"->r.{domain,
		 * group,acl} and "struct tomoyo_io_buffer"->w.domain forget
		 * the list element.
		 */
		if (tomoyo_struct_used_by_io_buffer(element))
			continue;
		/*
		 * Secondly, defer until all other elements in the
		 * tomoyo_gc_list list forget the list element.
		 */
		if (tomoyo_element_linked_by_gc((const u8 *) element, p->size))
			continue;
Tetsuo Handa's avatar
Tetsuo Handa committed
638
		switch (p->type) {
639
640
		case TOMOYO_ID_TRANSITION_CONTROL:
			tomoyo_del_transition_control(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
641
			break;
642
		case TOMOYO_ID_AGGREGATOR:
643
			tomoyo_del_aggregator(element);
644
			break;
Tetsuo Handa's avatar
Tetsuo Handa committed
645
		case TOMOYO_ID_MANAGER:
646
			tomoyo_del_manager(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
647
			break;
648
649
650
		case TOMOYO_ID_CONDITION:
			tomoyo_del_condition(element);
			break;
Tetsuo Handa's avatar
Tetsuo Handa committed
651
		case TOMOYO_ID_NAME:
Tetsuo Handa's avatar
Tetsuo Handa committed
652
653
654
655
656
657
658
659
			/*
			 * Thirdly, defer until all "struct tomoyo_io_buffer"
			 * ->r.w[] forget the list element.
			 */
			if (tomoyo_name_used_by_io_buffer(
			    container_of(element, typeof(struct tomoyo_name),
					 head.list)->entry.name, p->size))
				continue;
660
			tomoyo_del_name(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
661
662
			break;
		case TOMOYO_ID_ACL:
663
			tomoyo_del_acl(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
664
665
			break;
		case TOMOYO_ID_DOMAIN:
666
			if (!tomoyo_del_domain(element))
Tetsuo Handa's avatar
Tetsuo Handa committed
667
668
				continue;
			break;
669
		case TOMOYO_ID_PATH_GROUP:
670
			tomoyo_del_path_group(element);
671
			break;
672
673
674
		case TOMOYO_ID_ADDRESS_GROUP:
			tomoyo_del_address_group(element);
			break;
675
676
		case TOMOYO_ID_GROUP:
			tomoyo_del_group(element);
677
678
			break;
		case TOMOYO_ID_NUMBER_GROUP:
679
			tomoyo_del_number_group(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
680
			break;
Tetsuo Handa's avatar
Tetsuo Handa committed
681
682
		case TOMOYO_MAX_POLICY:
			break;
Tetsuo Handa's avatar
Tetsuo Handa committed
683
		}
684
		tomoyo_memory_free(element);
Tetsuo Handa's avatar
Tetsuo Handa committed
685
686
		list_del(&p->list);
		kfree(p);
Tetsuo Handa's avatar
Tetsuo Handa committed
687
688
		tomoyo_gc_list_len--;
		result = true;
Tetsuo Handa's avatar
Tetsuo Handa committed
689
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
690
	return result;
Tetsuo Handa's avatar
Tetsuo Handa committed
691
692
}

Tetsuo Handa's avatar
Tetsuo Handa committed
693
694
695
696
697
698
699
700
701
702
703
/**
 * tomoyo_gc_thread - Garbage collector thread function.
 *
 * @unused: Unused.
 *
 * In case OOM-killer choose this thread for termination, we create this thread
 * as a short live thread whenever /sys/kernel/security/tomoyo/ interface was
 * close()d.
 *
 * Returns 0.
 */
Tetsuo Handa's avatar
Tetsuo Handa committed
704
705
static int tomoyo_gc_thread(void *unused)
{
Tetsuo Handa's avatar
Tetsuo Handa committed
706
707
708
709
	/* Garbage collector thread is exclusive. */
	static DEFINE_MUTEX(tomoyo_gc_mutex);
	if (!mutex_trylock(&tomoyo_gc_mutex))
		goto out;
710

Tetsuo Handa's avatar
Tetsuo Handa committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
	do {
		tomoyo_collect_entry();
		if (list_empty(&tomoyo_gc_list))
			break;
		synchronize_srcu(&tomoyo_ss);
	} while (tomoyo_kfree_entry());
	{
		struct tomoyo_io_buffer *head;
		struct tomoyo_io_buffer *tmp;

		spin_lock(&tomoyo_io_buffer_list_lock);
		list_for_each_entry_safe(head, tmp, &tomoyo_io_buffer_list,
					 list) {
			if (head->users)
				continue;
			list_del(&head->list);
			kfree(head->read_buf);
			kfree(head->write_buf);
			kfree(head);
Tetsuo Handa's avatar
Tetsuo Handa committed
730
		}
Tetsuo Handa's avatar
Tetsuo Handa committed
731
		spin_unlock(&tomoyo_io_buffer_list_lock);
Tetsuo Handa's avatar
Tetsuo Handa committed
732
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
733
734
735
736
	mutex_unlock(&tomoyo_gc_mutex);
out:
	/* This acts as do_exit(0). */
	return 0;
Tetsuo Handa's avatar
Tetsuo Handa committed
737
738
}

Tetsuo Handa's avatar
Tetsuo Handa committed
739
740
741
742
743
744
745
746
747
/**
 * tomoyo_notify_gc - Register/unregister /sys/kernel/security/tomoyo/ users.
 *
 * @head:        Pointer to "struct tomoyo_io_buffer".
 * @is_register: True if register, false if unregister.
 *
 * Returns nothing.
 */
void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register)
Tetsuo Handa's avatar
Tetsuo Handa committed
748
{
Tetsuo Handa's avatar
Tetsuo Handa committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
	bool is_write = false;

	spin_lock(&tomoyo_io_buffer_list_lock);
	if (is_register) {
		head->users = 1;
		list_add(&head->list, &tomoyo_io_buffer_list);
	} else {
		is_write = head->write_buf != NULL;
		if (!--head->users) {
			list_del(&head->list);
			kfree(head->read_buf);
			kfree(head->write_buf);
			kfree(head);
		}
	}
	spin_unlock(&tomoyo_io_buffer_list_lock);
	if (is_write) {
		struct task_struct *task = kthread_create(tomoyo_gc_thread,
							  NULL,
							  "GC for TOMOYO");
		if (!IS_ERR(task))
			wake_up_process(task);
	}
Tetsuo Handa's avatar
Tetsuo Handa committed
772
}