gc.c 9.0 KB

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