gc.c 20 KB

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