gc.c 12 KB

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