gc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_entry {
  13. struct list_head list;
  14. int type;
  15. struct list_head *element;
  16. };
  17. static LIST_HEAD(tomoyo_gc_queue);
  18. static DEFINE_MUTEX(tomoyo_gc_mutex);
  19. /* Caller holds tomoyo_policy_lock mutex. */
  20. static bool tomoyo_add_to_gc(const int type, struct list_head *element)
  21. {
  22. struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  23. if (!entry)
  24. return false;
  25. entry->type = type;
  26. entry->element = element;
  27. list_add(&entry->list, &tomoyo_gc_queue);
  28. list_del_rcu(element);
  29. return true;
  30. }
  31. static void tomoyo_del_allow_read(struct list_head *element)
  32. {
  33. struct tomoyo_globally_readable_file_entry *ptr =
  34. container_of(element, typeof(*ptr), head.list);
  35. tomoyo_put_name(ptr->filename);
  36. }
  37. static void tomoyo_del_file_pattern(struct list_head *element)
  38. {
  39. struct tomoyo_pattern_entry *ptr =
  40. container_of(element, typeof(*ptr), head.list);
  41. tomoyo_put_name(ptr->pattern);
  42. }
  43. static void tomoyo_del_no_rewrite(struct list_head *element)
  44. {
  45. struct tomoyo_no_rewrite_entry *ptr =
  46. container_of(element, typeof(*ptr), head.list);
  47. tomoyo_put_name(ptr->pattern);
  48. }
  49. static void tomoyo_del_domain_initializer(struct list_head *element)
  50. {
  51. struct tomoyo_domain_initializer_entry *ptr =
  52. container_of(element, typeof(*ptr), head.list);
  53. tomoyo_put_name(ptr->domainname);
  54. tomoyo_put_name(ptr->program);
  55. }
  56. static void tomoyo_del_domain_keeper(struct list_head *element)
  57. {
  58. struct tomoyo_domain_keeper_entry *ptr =
  59. container_of(element, typeof(*ptr), head.list);
  60. tomoyo_put_name(ptr->domainname);
  61. tomoyo_put_name(ptr->program);
  62. }
  63. static void tomoyo_del_aggregator(struct list_head *element)
  64. {
  65. struct tomoyo_aggregator_entry *ptr =
  66. container_of(element, typeof(*ptr), head.list);
  67. tomoyo_put_name(ptr->original_name);
  68. tomoyo_put_name(ptr->aggregated_name);
  69. }
  70. static void tomoyo_del_alias(struct list_head *element)
  71. {
  72. struct tomoyo_alias_entry *ptr =
  73. container_of(element, typeof(*ptr), head.list);
  74. tomoyo_put_name(ptr->original_name);
  75. tomoyo_put_name(ptr->aliased_name);
  76. }
  77. static void tomoyo_del_manager(struct list_head *element)
  78. {
  79. struct tomoyo_policy_manager_entry *ptr =
  80. container_of(element, typeof(*ptr), head.list);
  81. tomoyo_put_name(ptr->manager);
  82. }
  83. static void tomoyo_del_acl(struct list_head *element)
  84. {
  85. struct tomoyo_acl_info *acl =
  86. container_of(element, typeof(*acl), list);
  87. switch (acl->type) {
  88. case TOMOYO_TYPE_PATH_ACL:
  89. {
  90. struct tomoyo_path_acl *entry
  91. = container_of(acl, typeof(*entry), head);
  92. tomoyo_put_name_union(&entry->name);
  93. }
  94. break;
  95. case TOMOYO_TYPE_PATH2_ACL:
  96. {
  97. struct tomoyo_path2_acl *entry
  98. = container_of(acl, typeof(*entry), head);
  99. tomoyo_put_name_union(&entry->name1);
  100. tomoyo_put_name_union(&entry->name2);
  101. }
  102. break;
  103. case TOMOYO_TYPE_PATH_NUMBER_ACL:
  104. {
  105. struct tomoyo_path_number_acl *entry
  106. = container_of(acl, typeof(*entry), head);
  107. tomoyo_put_name_union(&entry->name);
  108. tomoyo_put_number_union(&entry->number);
  109. }
  110. break;
  111. case TOMOYO_TYPE_MKDEV_ACL:
  112. {
  113. struct tomoyo_mkdev_acl *entry
  114. = container_of(acl, typeof(*entry), head);
  115. tomoyo_put_name_union(&entry->name);
  116. tomoyo_put_number_union(&entry->mode);
  117. tomoyo_put_number_union(&entry->major);
  118. tomoyo_put_number_union(&entry->minor);
  119. }
  120. break;
  121. case TOMOYO_TYPE_MOUNT_ACL:
  122. {
  123. struct tomoyo_mount_acl *entry
  124. = container_of(acl, typeof(*entry), head);
  125. tomoyo_put_name_union(&entry->dev_name);
  126. tomoyo_put_name_union(&entry->dir_name);
  127. tomoyo_put_name_union(&entry->fs_type);
  128. tomoyo_put_number_union(&entry->flags);
  129. }
  130. break;
  131. }
  132. }
  133. static bool tomoyo_del_domain(struct list_head *element)
  134. {
  135. struct tomoyo_domain_info *domain =
  136. container_of(element, typeof(*domain), list);
  137. struct tomoyo_acl_info *acl;
  138. struct tomoyo_acl_info *tmp;
  139. /*
  140. * Since we don't protect whole execve() operation using SRCU,
  141. * we need to recheck domain->users at this point.
  142. *
  143. * (1) Reader starts SRCU section upon execve().
  144. * (2) Reader traverses tomoyo_domain_list and finds this domain.
  145. * (3) Writer marks this domain as deleted.
  146. * (4) Garbage collector removes this domain from tomoyo_domain_list
  147. * because this domain is marked as deleted and used by nobody.
  148. * (5) Reader saves reference to this domain into
  149. * "struct linux_binprm"->cred->security .
  150. * (6) Reader finishes SRCU section, although execve() operation has
  151. * not finished yet.
  152. * (7) Garbage collector waits for SRCU synchronization.
  153. * (8) Garbage collector kfree() this domain because this domain is
  154. * used by nobody.
  155. * (9) Reader finishes execve() operation and restores this domain from
  156. * "struct linux_binprm"->cred->security.
  157. *
  158. * By updating domain->users at (5), we can solve this race problem
  159. * by rechecking domain->users at (8).
  160. */
  161. if (atomic_read(&domain->users))
  162. return false;
  163. list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
  164. tomoyo_del_acl(&acl->list);
  165. tomoyo_memory_free(acl);
  166. }
  167. tomoyo_put_name(domain->domainname);
  168. return true;
  169. }
  170. static void tomoyo_del_name(struct list_head *element)
  171. {
  172. const struct tomoyo_name_entry *ptr =
  173. container_of(element, typeof(*ptr), list);
  174. }
  175. static void tomoyo_del_path_group(struct list_head *element)
  176. {
  177. struct tomoyo_path_group *member =
  178. container_of(element, typeof(*member), head.list);
  179. tomoyo_put_name(member->member_name);
  180. }
  181. static void tomoyo_del_group(struct list_head *element)
  182. {
  183. struct tomoyo_group *group =
  184. container_of(element, typeof(*group), list);
  185. tomoyo_put_name(group->group_name);
  186. }
  187. static void tomoyo_del_number_group(struct list_head *element)
  188. {
  189. struct tomoyo_number_group *member =
  190. container_of(element, typeof(*member), head.list);
  191. }
  192. static bool tomoyo_collect_member(struct list_head *member_list, int id)
  193. {
  194. struct tomoyo_acl_head *member;
  195. list_for_each_entry(member, member_list, list) {
  196. if (!member->is_deleted)
  197. continue;
  198. if (!tomoyo_add_to_gc(id, &member->list))
  199. return false;
  200. }
  201. return true;
  202. }
  203. static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain)
  204. {
  205. struct tomoyo_acl_info *acl;
  206. list_for_each_entry(acl, &domain->acl_info_list, list) {
  207. if (!acl->is_deleted)
  208. continue;
  209. if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
  210. return false;
  211. }
  212. return true;
  213. }
  214. static void tomoyo_collect_entry(void)
  215. {
  216. int i;
  217. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  218. return;
  219. for (i = 0; i < TOMOYO_MAX_POLICY; i++) {
  220. if (!tomoyo_collect_member(&tomoyo_policy_list[i], i))
  221. goto unlock;
  222. }
  223. {
  224. struct tomoyo_domain_info *domain;
  225. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  226. if (!tomoyo_collect_acl(domain))
  227. goto unlock;
  228. if (!domain->is_deleted || atomic_read(&domain->users))
  229. continue;
  230. /*
  231. * Nobody is referring this domain. But somebody may
  232. * refer this domain after successful execve().
  233. * We recheck domain->users after SRCU synchronization.
  234. */
  235. if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
  236. goto unlock;
  237. }
  238. }
  239. for (i = 0; i < TOMOYO_MAX_HASH; i++) {
  240. struct tomoyo_name_entry *ptr;
  241. list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) {
  242. if (atomic_read(&ptr->users))
  243. continue;
  244. if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->list))
  245. goto unlock;
  246. }
  247. }
  248. for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
  249. struct list_head *list = &tomoyo_group_list[i];
  250. int id;
  251. struct tomoyo_group *group;
  252. switch (i) {
  253. case 0:
  254. id = TOMOYO_ID_PATH_GROUP;
  255. break;
  256. default:
  257. id = TOMOYO_ID_NUMBER_GROUP;
  258. break;
  259. }
  260. list_for_each_entry(group, list, list) {
  261. if (!tomoyo_collect_member(&group->member_list, id))
  262. goto unlock;
  263. if (!list_empty(&group->member_list) ||
  264. atomic_read(&group->users))
  265. continue;
  266. if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, &group->list))
  267. goto unlock;
  268. }
  269. }
  270. unlock:
  271. mutex_unlock(&tomoyo_policy_lock);
  272. }
  273. static void tomoyo_kfree_entry(void)
  274. {
  275. struct tomoyo_gc_entry *p;
  276. struct tomoyo_gc_entry *tmp;
  277. list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
  278. struct list_head *element = p->element;
  279. switch (p->type) {
  280. case TOMOYO_ID_DOMAIN_INITIALIZER:
  281. tomoyo_del_domain_initializer(element);
  282. break;
  283. case TOMOYO_ID_DOMAIN_KEEPER:
  284. tomoyo_del_domain_keeper(element);
  285. break;
  286. case TOMOYO_ID_AGGREGATOR:
  287. tomoyo_del_aggregator(element);
  288. break;
  289. case TOMOYO_ID_ALIAS:
  290. tomoyo_del_alias(element);
  291. break;
  292. case TOMOYO_ID_GLOBALLY_READABLE:
  293. tomoyo_del_allow_read(element);
  294. break;
  295. case TOMOYO_ID_PATTERN:
  296. tomoyo_del_file_pattern(element);
  297. break;
  298. case TOMOYO_ID_NO_REWRITE:
  299. tomoyo_del_no_rewrite(element);
  300. break;
  301. case TOMOYO_ID_MANAGER:
  302. tomoyo_del_manager(element);
  303. break;
  304. case TOMOYO_ID_NAME:
  305. tomoyo_del_name(element);
  306. break;
  307. case TOMOYO_ID_ACL:
  308. tomoyo_del_acl(element);
  309. break;
  310. case TOMOYO_ID_DOMAIN:
  311. if (!tomoyo_del_domain(element))
  312. continue;
  313. break;
  314. case TOMOYO_ID_PATH_GROUP:
  315. tomoyo_del_path_group(element);
  316. break;
  317. case TOMOYO_ID_GROUP:
  318. tomoyo_del_group(element);
  319. break;
  320. case TOMOYO_ID_NUMBER_GROUP:
  321. tomoyo_del_number_group(element);
  322. break;
  323. }
  324. tomoyo_memory_free(element);
  325. list_del(&p->list);
  326. kfree(p);
  327. }
  328. }
  329. static int tomoyo_gc_thread(void *unused)
  330. {
  331. daemonize("GC for TOMOYO");
  332. if (mutex_trylock(&tomoyo_gc_mutex)) {
  333. int i;
  334. for (i = 0; i < 10; i++) {
  335. tomoyo_collect_entry();
  336. if (list_empty(&tomoyo_gc_queue))
  337. break;
  338. synchronize_srcu(&tomoyo_ss);
  339. tomoyo_kfree_entry();
  340. }
  341. mutex_unlock(&tomoyo_gc_mutex);
  342. }
  343. do_exit(0);
  344. }
  345. void tomoyo_run_gc(void)
  346. {
  347. struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
  348. "GC for TOMOYO");
  349. if (!IS_ERR(task))
  350. wake_up_process(task);
  351. }