gc.c 9.6 KB

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