gc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. #include "common.h"
  10. #include <linux/kthread.h>
  11. #include <linux/slab.h>
  12. struct tomoyo_gc {
  13. struct list_head list;
  14. enum tomoyo_policy_id type;
  15. struct list_head *element;
  16. };
  17. static LIST_HEAD(tomoyo_gc_queue);
  18. static DEFINE_MUTEX(tomoyo_gc_mutex);
  19. /**
  20. * tomoyo_add_to_gc - Add an entry to to be deleted list.
  21. *
  22. * @type: One of values in "enum tomoyo_policy_id".
  23. * @element: Pointer to "struct list_head".
  24. *
  25. * Returns true on success, false otherwise.
  26. *
  27. * Caller holds tomoyo_policy_lock mutex.
  28. *
  29. * Adding an entry needs kmalloc(). Thus, if we try to add thousands of
  30. * entries at once, it will take too long time. Thus, do not add more than 128
  31. * entries per a scan. But to be able to handle worst case where all entries
  32. * are in-use, we accept one more entry per a scan.
  33. *
  34. * If we use singly linked list using "struct list_head"->prev (which is
  35. * LIST_POISON2), we can avoid kmalloc().
  36. */
  37. static bool tomoyo_add_to_gc(const int type, struct list_head *element)
  38. {
  39. struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  40. if (!entry)
  41. return false;
  42. entry->type = type;
  43. entry->element = element;
  44. list_add(&entry->list, &tomoyo_gc_queue);
  45. list_del_rcu(element);
  46. return true;
  47. }
  48. /**
  49. * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control".
  50. *
  51. * @element: Pointer to "struct list_head".
  52. *
  53. * Returns nothing.
  54. */
  55. static void tomoyo_del_transition_control(struct list_head *element)
  56. {
  57. struct tomoyo_transition_control *ptr =
  58. container_of(element, typeof(*ptr), head.list);
  59. tomoyo_put_name(ptr->domainname);
  60. tomoyo_put_name(ptr->program);
  61. }
  62. /**
  63. * tomoyo_del_aggregator - Delete members in "struct tomoyo_aggregator".
  64. *
  65. * @element: Pointer to "struct list_head".
  66. *
  67. * Returns nothing.
  68. */
  69. static void tomoyo_del_aggregator(struct list_head *element)
  70. {
  71. struct tomoyo_aggregator *ptr =
  72. container_of(element, typeof(*ptr), head.list);
  73. tomoyo_put_name(ptr->original_name);
  74. tomoyo_put_name(ptr->aggregated_name);
  75. }
  76. /**
  77. * tomoyo_del_manager - Delete members in "struct tomoyo_manager".
  78. *
  79. * @element: Pointer to "struct list_head".
  80. *
  81. * Returns nothing.
  82. */
  83. static void tomoyo_del_manager(struct list_head *element)
  84. {
  85. struct tomoyo_manager *ptr =
  86. container_of(element, typeof(*ptr), head.list);
  87. tomoyo_put_name(ptr->manager);
  88. }
  89. /**
  90. * tomoyo_del_acl - Delete members in "struct tomoyo_acl_info".
  91. *
  92. * @element: Pointer to "struct list_head".
  93. *
  94. * Returns nothing.
  95. */
  96. static void tomoyo_del_acl(struct list_head *element)
  97. {
  98. struct tomoyo_acl_info *acl =
  99. container_of(element, typeof(*acl), list);
  100. switch (acl->type) {
  101. case TOMOYO_TYPE_PATH_ACL:
  102. {
  103. struct tomoyo_path_acl *entry
  104. = container_of(acl, typeof(*entry), head);
  105. tomoyo_put_name_union(&entry->name);
  106. }
  107. break;
  108. case TOMOYO_TYPE_PATH2_ACL:
  109. {
  110. struct tomoyo_path2_acl *entry
  111. = container_of(acl, typeof(*entry), head);
  112. tomoyo_put_name_union(&entry->name1);
  113. tomoyo_put_name_union(&entry->name2);
  114. }
  115. break;
  116. case TOMOYO_TYPE_PATH_NUMBER_ACL:
  117. {
  118. struct tomoyo_path_number_acl *entry
  119. = container_of(acl, typeof(*entry), head);
  120. tomoyo_put_name_union(&entry->name);
  121. tomoyo_put_number_union(&entry->number);
  122. }
  123. break;
  124. case TOMOYO_TYPE_MKDEV_ACL:
  125. {
  126. struct tomoyo_mkdev_acl *entry
  127. = container_of(acl, typeof(*entry), head);
  128. tomoyo_put_name_union(&entry->name);
  129. tomoyo_put_number_union(&entry->mode);
  130. tomoyo_put_number_union(&entry->major);
  131. tomoyo_put_number_union(&entry->minor);
  132. }
  133. break;
  134. case TOMOYO_TYPE_MOUNT_ACL:
  135. {
  136. struct tomoyo_mount_acl *entry
  137. = container_of(acl, typeof(*entry), head);
  138. tomoyo_put_name_union(&entry->dev_name);
  139. tomoyo_put_name_union(&entry->dir_name);
  140. tomoyo_put_name_union(&entry->fs_type);
  141. tomoyo_put_number_union(&entry->flags);
  142. }
  143. break;
  144. }
  145. }
  146. static bool tomoyo_del_domain(struct list_head *element)
  147. {
  148. struct tomoyo_domain_info *domain =
  149. container_of(element, typeof(*domain), list);
  150. struct tomoyo_acl_info *acl;
  151. struct tomoyo_acl_info *tmp;
  152. /*
  153. * Since we don't protect whole execve() operation using SRCU,
  154. * we need to recheck domain->users at this point.
  155. *
  156. * (1) Reader starts SRCU section upon execve().
  157. * (2) Reader traverses tomoyo_domain_list and finds this domain.
  158. * (3) Writer marks this domain as deleted.
  159. * (4) Garbage collector removes this domain from tomoyo_domain_list
  160. * because this domain is marked as deleted and used by nobody.
  161. * (5) Reader saves reference to this domain into
  162. * "struct linux_binprm"->cred->security .
  163. * (6) Reader finishes SRCU section, although execve() operation has
  164. * not finished yet.
  165. * (7) Garbage collector waits for SRCU synchronization.
  166. * (8) Garbage collector kfree() this domain because this domain is
  167. * used by nobody.
  168. * (9) Reader finishes execve() operation and restores this domain from
  169. * "struct linux_binprm"->cred->security.
  170. *
  171. * By updating domain->users at (5), we can solve this race problem
  172. * by rechecking domain->users at (8).
  173. */
  174. if (atomic_read(&domain->users))
  175. return false;
  176. list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
  177. tomoyo_del_acl(&acl->list);
  178. tomoyo_memory_free(acl);
  179. }
  180. tomoyo_put_name(domain->domainname);
  181. return true;
  182. }
  183. /**
  184. * tomoyo_del_name - Delete members in "struct tomoyo_name".
  185. *
  186. * @element: Pointer to "struct list_head".
  187. *
  188. * Returns nothing.
  189. */
  190. static void tomoyo_del_name(struct list_head *element)
  191. {
  192. const struct tomoyo_name *ptr =
  193. container_of(element, typeof(*ptr), head.list);
  194. }
  195. /**
  196. * tomoyo_del_path_group - Delete members in "struct tomoyo_path_group".
  197. *
  198. * @element: Pointer to "struct list_head".
  199. *
  200. * Returns nothing.
  201. */
  202. static void tomoyo_del_path_group(struct list_head *element)
  203. {
  204. struct tomoyo_path_group *member =
  205. container_of(element, typeof(*member), head.list);
  206. tomoyo_put_name(member->member_name);
  207. }
  208. /**
  209. * tomoyo_del_group - Delete "struct tomoyo_group".
  210. *
  211. * @element: Pointer to "struct list_head".
  212. *
  213. * Returns nothing.
  214. */
  215. static void tomoyo_del_group(struct list_head *element)
  216. {
  217. struct tomoyo_group *group =
  218. container_of(element, typeof(*group), head.list);
  219. tomoyo_put_name(group->group_name);
  220. }
  221. /**
  222. * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group".
  223. *
  224. * @element: Pointer to "struct list_head".
  225. *
  226. * Returns nothing.
  227. */
  228. static void tomoyo_del_number_group(struct list_head *element)
  229. {
  230. struct tomoyo_number_group *member =
  231. container_of(element, typeof(*member), head.list);
  232. }
  233. /**
  234. * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head".
  235. *
  236. * @id: One of values in "enum tomoyo_policy_id".
  237. * @member_list: Pointer to "struct list_head".
  238. *
  239. * Returns true if some elements are deleted, false otherwise.
  240. */
  241. static bool tomoyo_collect_member(const enum tomoyo_policy_id id,
  242. struct list_head *member_list)
  243. {
  244. struct tomoyo_acl_head *member;
  245. list_for_each_entry(member, member_list, list) {
  246. if (!member->is_deleted)
  247. continue;
  248. if (!tomoyo_add_to_gc(id, &member->list))
  249. return false;
  250. }
  251. return true;
  252. }
  253. static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain)
  254. {
  255. struct tomoyo_acl_info *acl;
  256. list_for_each_entry(acl, &domain->acl_info_list, list) {
  257. if (!acl->is_deleted)
  258. continue;
  259. if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
  260. return false;
  261. }
  262. return true;
  263. }
  264. /**
  265. * tomoyo_collect_entry - Scan lists for deleted elements.
  266. *
  267. * Returns nothing.
  268. */
  269. static void tomoyo_collect_entry(void)
  270. {
  271. int i;
  272. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  273. return;
  274. for (i = 0; i < TOMOYO_MAX_POLICY; i++) {
  275. if (!tomoyo_collect_member(i, &tomoyo_policy_list[i]))
  276. goto unlock;
  277. }
  278. {
  279. struct tomoyo_domain_info *domain;
  280. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  281. if (!tomoyo_collect_acl(domain))
  282. goto unlock;
  283. if (!domain->is_deleted || atomic_read(&domain->users))
  284. continue;
  285. /*
  286. * Nobody is referring this domain. But somebody may
  287. * refer this domain after successful execve().
  288. * We recheck domain->users after SRCU synchronization.
  289. */
  290. if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
  291. goto unlock;
  292. }
  293. }
  294. for (i = 0; i < TOMOYO_MAX_HASH; i++) {
  295. struct tomoyo_name *ptr;
  296. list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], head.list) {
  297. if (atomic_read(&ptr->head.users))
  298. continue;
  299. if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->head.list))
  300. goto unlock;
  301. }
  302. }
  303. for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
  304. struct list_head *list = &tomoyo_group_list[i];
  305. int id;
  306. struct tomoyo_group *group;
  307. switch (i) {
  308. case 0:
  309. id = TOMOYO_ID_PATH_GROUP;
  310. break;
  311. default:
  312. id = TOMOYO_ID_NUMBER_GROUP;
  313. break;
  314. }
  315. list_for_each_entry(group, list, head.list) {
  316. if (!tomoyo_collect_member(id, &group->member_list))
  317. goto unlock;
  318. if (!list_empty(&group->member_list) ||
  319. atomic_read(&group->head.users))
  320. continue;
  321. if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
  322. &group->head.list))
  323. goto unlock;
  324. }
  325. }
  326. unlock:
  327. mutex_unlock(&tomoyo_policy_lock);
  328. }
  329. static void tomoyo_kfree_entry(void)
  330. {
  331. struct tomoyo_gc *p;
  332. struct tomoyo_gc *tmp;
  333. list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
  334. struct list_head *element = p->element;
  335. switch (p->type) {
  336. case TOMOYO_ID_TRANSITION_CONTROL:
  337. tomoyo_del_transition_control(element);
  338. break;
  339. case TOMOYO_ID_AGGREGATOR:
  340. tomoyo_del_aggregator(element);
  341. break;
  342. case TOMOYO_ID_MANAGER:
  343. tomoyo_del_manager(element);
  344. break;
  345. case TOMOYO_ID_NAME:
  346. tomoyo_del_name(element);
  347. break;
  348. case TOMOYO_ID_ACL:
  349. tomoyo_del_acl(element);
  350. break;
  351. case TOMOYO_ID_DOMAIN:
  352. if (!tomoyo_del_domain(element))
  353. continue;
  354. break;
  355. case TOMOYO_ID_PATH_GROUP:
  356. tomoyo_del_path_group(element);
  357. break;
  358. case TOMOYO_ID_GROUP:
  359. tomoyo_del_group(element);
  360. break;
  361. case TOMOYO_ID_NUMBER_GROUP:
  362. tomoyo_del_number_group(element);
  363. break;
  364. case TOMOYO_MAX_POLICY:
  365. break;
  366. }
  367. tomoyo_memory_free(element);
  368. list_del(&p->list);
  369. kfree(p);
  370. }
  371. }
  372. /**
  373. * tomoyo_gc_thread - Garbage collector thread function.
  374. *
  375. * @unused: Unused.
  376. *
  377. * In case OOM-killer choose this thread for termination, we create this thread
  378. * as a short live thread whenever /sys/kernel/security/tomoyo/ interface was
  379. * close()d.
  380. *
  381. * Returns 0.
  382. */
  383. static int tomoyo_gc_thread(void *unused)
  384. {
  385. daemonize("GC for TOMOYO");
  386. if (mutex_trylock(&tomoyo_gc_mutex)) {
  387. int i;
  388. for (i = 0; i < 10; i++) {
  389. tomoyo_collect_entry();
  390. if (list_empty(&tomoyo_gc_queue))
  391. break;
  392. synchronize_srcu(&tomoyo_ss);
  393. tomoyo_kfree_entry();
  394. }
  395. mutex_unlock(&tomoyo_gc_mutex);
  396. }
  397. do_exit(0);
  398. }
  399. void tomoyo_run_gc(void)
  400. {
  401. struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
  402. "GC for TOMOYO");
  403. if (!IS_ERR(task))
  404. wake_up_process(task);
  405. }