gc.c 11 KB

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