gc.c 10 KB

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