gc.c 9.4 KB

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