gc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. {
  249. struct tomoyo_group *group;
  250. list_for_each_entry_rcu(group,
  251. &tomoyo_group_list[TOMOYO_PATH_GROUP],
  252. list) {
  253. tomoyo_collect_member(&group->member_list,
  254. TOMOYO_ID_PATH_GROUP);
  255. if (!list_empty(&group->member_list) ||
  256. atomic_read(&group->users))
  257. continue;
  258. if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
  259. &group->list))
  260. goto unlock;
  261. }
  262. }
  263. {
  264. struct tomoyo_group *group;
  265. list_for_each_entry_rcu(group,
  266. &tomoyo_group_list[TOMOYO_NUMBER_GROUP],
  267. list) {
  268. tomoyo_collect_member(&group->member_list,
  269. TOMOYO_ID_NUMBER_GROUP);
  270. if (!list_empty(&group->member_list) ||
  271. atomic_read(&group->users))
  272. continue;
  273. if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP,
  274. &group->list))
  275. goto unlock;
  276. }
  277. }
  278. unlock:
  279. mutex_unlock(&tomoyo_policy_lock);
  280. }
  281. static void tomoyo_kfree_entry(void)
  282. {
  283. struct tomoyo_gc_entry *p;
  284. struct tomoyo_gc_entry *tmp;
  285. list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
  286. struct list_head *element = p->element;
  287. switch (p->type) {
  288. case TOMOYO_ID_DOMAIN_INITIALIZER:
  289. tomoyo_del_domain_initializer(element);
  290. break;
  291. case TOMOYO_ID_DOMAIN_KEEPER:
  292. tomoyo_del_domain_keeper(element);
  293. break;
  294. case TOMOYO_ID_AGGREGATOR:
  295. tomoyo_del_aggregator(element);
  296. break;
  297. case TOMOYO_ID_ALIAS:
  298. tomoyo_del_alias(element);
  299. break;
  300. case TOMOYO_ID_GLOBALLY_READABLE:
  301. tomoyo_del_allow_read(element);
  302. break;
  303. case TOMOYO_ID_PATTERN:
  304. tomoyo_del_file_pattern(element);
  305. break;
  306. case TOMOYO_ID_NO_REWRITE:
  307. tomoyo_del_no_rewrite(element);
  308. break;
  309. case TOMOYO_ID_MANAGER:
  310. tomoyo_del_manager(element);
  311. break;
  312. case TOMOYO_ID_NAME:
  313. tomoyo_del_name(element);
  314. break;
  315. case TOMOYO_ID_ACL:
  316. tomoyo_del_acl(element);
  317. break;
  318. case TOMOYO_ID_DOMAIN:
  319. if (!tomoyo_del_domain(element))
  320. continue;
  321. break;
  322. case TOMOYO_ID_PATH_GROUP:
  323. tomoyo_del_path_group(element);
  324. break;
  325. case TOMOYO_ID_GROUP:
  326. tomoyo_del_group(element);
  327. break;
  328. case TOMOYO_ID_NUMBER_GROUP:
  329. tomoyo_del_number_group(element);
  330. break;
  331. }
  332. tomoyo_memory_free(element);
  333. list_del(&p->list);
  334. kfree(p);
  335. }
  336. }
  337. static int tomoyo_gc_thread(void *unused)
  338. {
  339. daemonize("GC for TOMOYO");
  340. if (mutex_trylock(&tomoyo_gc_mutex)) {
  341. int i;
  342. for (i = 0; i < 10; i++) {
  343. tomoyo_collect_entry();
  344. if (list_empty(&tomoyo_gc_queue))
  345. break;
  346. synchronize_srcu(&tomoyo_ss);
  347. tomoyo_kfree_entry();
  348. }
  349. mutex_unlock(&tomoyo_gc_mutex);
  350. }
  351. do_exit(0);
  352. }
  353. void tomoyo_run_gc(void)
  354. {
  355. struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
  356. "GC for TOMOYO");
  357. if (!IS_ERR(task))
  358. wake_up_process(task);
  359. }