gc.c 8.6 KB

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