gc.c 11 KB

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