gc.c 11 KB

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