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. enum tomoyo_gc_id {
  12. TOMOYO_ID_DOMAIN_INITIALIZER,
  13. TOMOYO_ID_DOMAIN_KEEPER,
  14. TOMOYO_ID_ALIAS,
  15. TOMOYO_ID_GLOBALLY_READABLE,
  16. TOMOYO_ID_PATTERN,
  17. TOMOYO_ID_NO_REWRITE,
  18. TOMOYO_ID_MANAGER,
  19. TOMOYO_ID_NAME,
  20. TOMOYO_ID_ACL,
  21. TOMOYO_ID_DOMAIN
  22. };
  23. struct tomoyo_gc_entry {
  24. struct list_head list;
  25. int type;
  26. void *element;
  27. };
  28. static LIST_HEAD(tomoyo_gc_queue);
  29. static DEFINE_MUTEX(tomoyo_gc_mutex);
  30. /* Caller holds tomoyo_policy_lock mutex. */
  31. static bool tomoyo_add_to_gc(const int type, void *element)
  32. {
  33. struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  34. if (!entry)
  35. return false;
  36. entry->type = type;
  37. entry->element = element;
  38. list_add(&entry->list, &tomoyo_gc_queue);
  39. return true;
  40. }
  41. static void tomoyo_del_allow_read
  42. (struct tomoyo_globally_readable_file_entry *ptr)
  43. {
  44. tomoyo_put_name(ptr->filename);
  45. }
  46. static void tomoyo_del_file_pattern(struct tomoyo_pattern_entry *ptr)
  47. {
  48. tomoyo_put_name(ptr->pattern);
  49. }
  50. static void tomoyo_del_no_rewrite(struct tomoyo_no_rewrite_entry *ptr)
  51. {
  52. tomoyo_put_name(ptr->pattern);
  53. }
  54. static void tomoyo_del_domain_initializer
  55. (struct tomoyo_domain_initializer_entry *ptr)
  56. {
  57. tomoyo_put_name(ptr->domainname);
  58. tomoyo_put_name(ptr->program);
  59. }
  60. static void tomoyo_del_domain_keeper(struct tomoyo_domain_keeper_entry *ptr)
  61. {
  62. tomoyo_put_name(ptr->domainname);
  63. tomoyo_put_name(ptr->program);
  64. }
  65. static void tomoyo_del_alias(struct tomoyo_alias_entry *ptr)
  66. {
  67. tomoyo_put_name(ptr->original_name);
  68. tomoyo_put_name(ptr->aliased_name);
  69. }
  70. static void tomoyo_del_manager(struct tomoyo_policy_manager_entry *ptr)
  71. {
  72. tomoyo_put_name(ptr->manager);
  73. }
  74. static void tomoyo_del_acl(struct tomoyo_acl_info *acl)
  75. {
  76. switch (acl->type) {
  77. case TOMOYO_TYPE_PATH_ACL:
  78. {
  79. struct tomoyo_path_acl *entry
  80. = container_of(acl, typeof(*entry), head);
  81. tomoyo_put_name(entry->filename);
  82. }
  83. break;
  84. case TOMOYO_TYPE_PATH2_ACL:
  85. {
  86. struct tomoyo_path2_acl *entry
  87. = container_of(acl, typeof(*entry), head);
  88. tomoyo_put_name(entry->filename1);
  89. tomoyo_put_name(entry->filename2);
  90. }
  91. break;
  92. default:
  93. printk(KERN_WARNING "Unknown type\n");
  94. break;
  95. }
  96. }
  97. static bool tomoyo_del_domain(struct tomoyo_domain_info *domain)
  98. {
  99. struct tomoyo_acl_info *acl;
  100. struct tomoyo_acl_info *tmp;
  101. /*
  102. * Since we don't protect whole execve() operation using SRCU,
  103. * we need to recheck domain->users at this point.
  104. *
  105. * (1) Reader starts SRCU section upon execve().
  106. * (2) Reader traverses tomoyo_domain_list and finds this domain.
  107. * (3) Writer marks this domain as deleted.
  108. * (4) Garbage collector removes this domain from tomoyo_domain_list
  109. * because this domain is marked as deleted and used by nobody.
  110. * (5) Reader saves reference to this domain into
  111. * "struct linux_binprm"->cred->security .
  112. * (6) Reader finishes SRCU section, although execve() operation has
  113. * not finished yet.
  114. * (7) Garbage collector waits for SRCU synchronization.
  115. * (8) Garbage collector kfree() this domain because this domain is
  116. * used by nobody.
  117. * (9) Reader finishes execve() operation and restores this domain from
  118. * "struct linux_binprm"->cred->security.
  119. *
  120. * By updating domain->users at (5), we can solve this race problem
  121. * by rechecking domain->users at (8).
  122. */
  123. if (atomic_read(&domain->users))
  124. return false;
  125. list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
  126. tomoyo_del_acl(acl);
  127. tomoyo_memory_free(acl);
  128. }
  129. tomoyo_put_name(domain->domainname);
  130. return true;
  131. }
  132. static void tomoyo_del_name(const struct tomoyo_name_entry *ptr)
  133. {
  134. }
  135. static void tomoyo_collect_entry(void)
  136. {
  137. mutex_lock(&tomoyo_policy_lock);
  138. {
  139. struct tomoyo_globally_readable_file_entry *ptr;
  140. list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
  141. list) {
  142. if (!ptr->is_deleted)
  143. continue;
  144. if (tomoyo_add_to_gc(TOMOYO_ID_GLOBALLY_READABLE, ptr))
  145. list_del_rcu(&ptr->list);
  146. else
  147. break;
  148. }
  149. }
  150. {
  151. struct tomoyo_pattern_entry *ptr;
  152. list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
  153. if (!ptr->is_deleted)
  154. continue;
  155. if (tomoyo_add_to_gc(TOMOYO_ID_PATTERN, ptr))
  156. list_del_rcu(&ptr->list);
  157. else
  158. break;
  159. }
  160. }
  161. {
  162. struct tomoyo_no_rewrite_entry *ptr;
  163. list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
  164. if (!ptr->is_deleted)
  165. continue;
  166. if (tomoyo_add_to_gc(TOMOYO_ID_NO_REWRITE, ptr))
  167. list_del_rcu(&ptr->list);
  168. else
  169. break;
  170. }
  171. }
  172. {
  173. struct tomoyo_domain_initializer_entry *ptr;
  174. list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
  175. list) {
  176. if (!ptr->is_deleted)
  177. continue;
  178. if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN_INITIALIZER, ptr))
  179. list_del_rcu(&ptr->list);
  180. else
  181. break;
  182. }
  183. }
  184. {
  185. struct tomoyo_domain_keeper_entry *ptr;
  186. list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
  187. if (!ptr->is_deleted)
  188. continue;
  189. if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN_KEEPER, ptr))
  190. list_del_rcu(&ptr->list);
  191. else
  192. break;
  193. }
  194. }
  195. {
  196. struct tomoyo_alias_entry *ptr;
  197. list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
  198. if (!ptr->is_deleted)
  199. continue;
  200. if (tomoyo_add_to_gc(TOMOYO_ID_ALIAS, ptr))
  201. list_del_rcu(&ptr->list);
  202. else
  203. break;
  204. }
  205. }
  206. {
  207. struct tomoyo_policy_manager_entry *ptr;
  208. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list,
  209. list) {
  210. if (!ptr->is_deleted)
  211. continue;
  212. if (tomoyo_add_to_gc(TOMOYO_ID_MANAGER, ptr))
  213. list_del_rcu(&ptr->list);
  214. else
  215. break;
  216. }
  217. }
  218. {
  219. struct tomoyo_domain_info *domain;
  220. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  221. struct tomoyo_acl_info *acl;
  222. list_for_each_entry_rcu(acl, &domain->acl_info_list,
  223. list) {
  224. switch (acl->type) {
  225. case TOMOYO_TYPE_PATH_ACL:
  226. if (container_of(acl,
  227. struct tomoyo_path_acl,
  228. head)->perm ||
  229. container_of(acl,
  230. struct tomoyo_path_acl,
  231. head)->perm_high)
  232. continue;
  233. break;
  234. case TOMOYO_TYPE_PATH2_ACL:
  235. if (container_of(acl,
  236. struct tomoyo_path2_acl,
  237. head)->perm)
  238. continue;
  239. break;
  240. default:
  241. continue;
  242. }
  243. if (tomoyo_add_to_gc(TOMOYO_ID_ACL, acl))
  244. list_del_rcu(&acl->list);
  245. else
  246. break;
  247. }
  248. if (!domain->is_deleted || atomic_read(&domain->users))
  249. continue;
  250. /*
  251. * Nobody is referring this domain. But somebody may
  252. * refer this domain after successful execve().
  253. * We recheck domain->users after SRCU synchronization.
  254. */
  255. if (tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, domain))
  256. list_del_rcu(&domain->list);
  257. else
  258. break;
  259. }
  260. }
  261. mutex_unlock(&tomoyo_policy_lock);
  262. mutex_lock(&tomoyo_name_list_lock);
  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_name_list_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. }