domain.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * security/tomoyo/domain.c
  3. *
  4. * Domain transition functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/binfmts.h>
  10. #include <linux/slab.h>
  11. /* Variables definitions.*/
  12. /* The initial domain. */
  13. struct tomoyo_domain_info tomoyo_kernel_domain;
  14. /**
  15. * tomoyo_update_policy - Update an entry for exception policy.
  16. *
  17. * @new_entry: Pointer to "struct tomoyo_acl_info".
  18. * @size: Size of @new_entry in bytes.
  19. * @is_delete: True if it is a delete request.
  20. * @list: Pointer to "struct list_head".
  21. * @check_duplicate: Callback function to find duplicated entry.
  22. *
  23. * Returns 0 on success, negative value otherwise.
  24. *
  25. * Caller holds tomoyo_read_lock().
  26. */
  27. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  28. bool is_delete, struct list_head *list,
  29. bool (*check_duplicate) (const struct tomoyo_acl_head
  30. *,
  31. const struct tomoyo_acl_head
  32. *))
  33. {
  34. int error = is_delete ? -ENOENT : -ENOMEM;
  35. struct tomoyo_acl_head *entry;
  36. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  37. return -ENOMEM;
  38. list_for_each_entry_rcu(entry, list, list) {
  39. if (!check_duplicate(entry, new_entry))
  40. continue;
  41. entry->is_deleted = is_delete;
  42. error = 0;
  43. break;
  44. }
  45. if (error && !is_delete) {
  46. entry = tomoyo_commit_ok(new_entry, size);
  47. if (entry) {
  48. list_add_tail_rcu(&entry->list, list);
  49. error = 0;
  50. }
  51. }
  52. mutex_unlock(&tomoyo_policy_lock);
  53. return error;
  54. }
  55. /**
  56. * tomoyo_update_domain - Update an entry for domain policy.
  57. *
  58. * @new_entry: Pointer to "struct tomoyo_acl_info".
  59. * @size: Size of @new_entry in bytes.
  60. * @is_delete: True if it is a delete request.
  61. * @domain: Pointer to "struct tomoyo_domain_info".
  62. * @check_duplicate: Callback function to find duplicated entry.
  63. * @merge_duplicate: Callback function to merge duplicated entry.
  64. *
  65. * Returns 0 on success, negative value otherwise.
  66. *
  67. * Caller holds tomoyo_read_lock().
  68. */
  69. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  70. bool is_delete, struct tomoyo_domain_info *domain,
  71. bool (*check_duplicate) (const struct tomoyo_acl_info
  72. *,
  73. const struct tomoyo_acl_info
  74. *),
  75. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  76. struct tomoyo_acl_info *,
  77. const bool))
  78. {
  79. int error = is_delete ? -ENOENT : -ENOMEM;
  80. struct tomoyo_acl_info *entry;
  81. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  82. return error;
  83. list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
  84. if (!check_duplicate(entry, new_entry))
  85. continue;
  86. if (merge_duplicate)
  87. entry->is_deleted = merge_duplicate(entry, new_entry,
  88. is_delete);
  89. else
  90. entry->is_deleted = is_delete;
  91. error = 0;
  92. break;
  93. }
  94. if (error && !is_delete) {
  95. entry = tomoyo_commit_ok(new_entry, size);
  96. if (entry) {
  97. list_add_tail_rcu(&entry->list, &domain->acl_info_list);
  98. error = 0;
  99. }
  100. }
  101. mutex_unlock(&tomoyo_policy_lock);
  102. return error;
  103. }
  104. void tomoyo_check_acl(struct tomoyo_request_info *r,
  105. bool (*check_entry) (const struct tomoyo_request_info *,
  106. const struct tomoyo_acl_info *))
  107. {
  108. const struct tomoyo_domain_info *domain = r->domain;
  109. struct tomoyo_acl_info *ptr;
  110. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  111. if (ptr->is_deleted || ptr->type != r->param_type)
  112. continue;
  113. if (check_entry(r, ptr)) {
  114. r->granted = true;
  115. return;
  116. }
  117. }
  118. r->granted = false;
  119. }
  120. /* The list for "struct tomoyo_domain_info". */
  121. LIST_HEAD(tomoyo_domain_list);
  122. struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
  123. struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
  124. /**
  125. * tomoyo_get_last_name - Get last component of a domainname.
  126. *
  127. * @domain: Pointer to "struct tomoyo_domain_info".
  128. *
  129. * Returns the last component of the domainname.
  130. */
  131. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
  132. {
  133. const char *cp0 = domain->domainname->name;
  134. const char *cp1 = strrchr(cp0, ' ');
  135. if (cp1)
  136. return cp1 + 1;
  137. return cp0;
  138. }
  139. static bool tomoyo_same_transition_control_entry(const struct tomoyo_acl_head *
  140. a,
  141. const struct tomoyo_acl_head *
  142. b)
  143. {
  144. const struct tomoyo_transition_control *p1 = container_of(a,
  145. typeof(*p1),
  146. head);
  147. const struct tomoyo_transition_control *p2 = container_of(b,
  148. typeof(*p2),
  149. head);
  150. return p1->type == p2->type && p1->is_last_name == p2->is_last_name
  151. && p1->domainname == p2->domainname
  152. && p1->program == p2->program;
  153. }
  154. /**
  155. * tomoyo_update_transition_control_entry - Update "struct tomoyo_transition_control" list.
  156. *
  157. * @domainname: The name of domain. Maybe NULL.
  158. * @program: The name of program. Maybe NULL.
  159. * @type: Type of transition.
  160. * @is_delete: True if it is a delete request.
  161. *
  162. * Returns 0 on success, negative value otherwise.
  163. */
  164. static int tomoyo_update_transition_control_entry(const char *domainname,
  165. const char *program,
  166. const u8 type,
  167. const bool is_delete)
  168. {
  169. struct tomoyo_transition_control e = { .type = type };
  170. int error = is_delete ? -ENOENT : -ENOMEM;
  171. if (program) {
  172. if (!tomoyo_correct_path(program))
  173. return -EINVAL;
  174. e.program = tomoyo_get_name(program);
  175. if (!e.program)
  176. goto out;
  177. }
  178. if (domainname) {
  179. if (!tomoyo_correct_domain(domainname)) {
  180. if (!tomoyo_correct_path(domainname))
  181. goto out;
  182. e.is_last_name = true;
  183. }
  184. e.domainname = tomoyo_get_name(domainname);
  185. if (!e.domainname)
  186. goto out;
  187. }
  188. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  189. &tomoyo_policy_list
  190. [TOMOYO_ID_TRANSITION_CONTROL],
  191. tomoyo_same_transition_control_entry);
  192. out:
  193. tomoyo_put_name(e.domainname);
  194. tomoyo_put_name(e.program);
  195. return error;
  196. }
  197. /**
  198. * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
  199. *
  200. * @data: String to parse.
  201. * @is_delete: True if it is a delete request.
  202. * @type: Type of this entry.
  203. *
  204. * Returns 0 on success, negative value otherwise.
  205. */
  206. int tomoyo_write_transition_control(char *data, const bool is_delete,
  207. const u8 type)
  208. {
  209. char *domainname = strstr(data, " from ");
  210. if (domainname) {
  211. *domainname = '\0';
  212. domainname += 6;
  213. } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
  214. type == TOMOYO_TRANSITION_CONTROL_KEEP) {
  215. domainname = data;
  216. data = NULL;
  217. }
  218. return tomoyo_update_transition_control_entry(domainname, data, type,
  219. is_delete);
  220. }
  221. /**
  222. * tomoyo_transition_type - Get domain transition type.
  223. *
  224. * @domainname: The name of domain.
  225. * @program: The name of program.
  226. *
  227. * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
  228. * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
  229. * @program suppresses domain transition, others otherwise.
  230. *
  231. * Caller holds tomoyo_read_lock().
  232. */
  233. static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
  234. const struct tomoyo_path_info *program)
  235. {
  236. const struct tomoyo_transition_control *ptr;
  237. const char *last_name = tomoyo_last_word(domainname->name);
  238. u8 type;
  239. for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
  240. next:
  241. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  242. [TOMOYO_ID_TRANSITION_CONTROL],
  243. head.list) {
  244. if (ptr->head.is_deleted || ptr->type != type)
  245. continue;
  246. if (ptr->domainname) {
  247. if (!ptr->is_last_name) {
  248. if (ptr->domainname != domainname)
  249. continue;
  250. } else {
  251. /*
  252. * Use direct strcmp() since this is
  253. * unlikely used.
  254. */
  255. if (strcmp(ptr->domainname->name,
  256. last_name))
  257. continue;
  258. }
  259. }
  260. if (ptr->program &&
  261. tomoyo_pathcmp(ptr->program, program))
  262. continue;
  263. if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
  264. /*
  265. * Do not check for initialize_domain if
  266. * no_initialize_domain matched.
  267. */
  268. type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
  269. goto next;
  270. }
  271. goto done;
  272. }
  273. }
  274. done:
  275. return type;
  276. }
  277. static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
  278. const struct tomoyo_acl_head *b)
  279. {
  280. const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
  281. head);
  282. const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
  283. head);
  284. return p1->original_name == p2->original_name &&
  285. p1->aggregated_name == p2->aggregated_name;
  286. }
  287. /**
  288. * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
  289. *
  290. * @original_name: The original program's name.
  291. * @aggregated_name: The program name to use.
  292. * @is_delete: True if it is a delete request.
  293. *
  294. * Returns 0 on success, negative value otherwise.
  295. *
  296. * Caller holds tomoyo_read_lock().
  297. */
  298. static int tomoyo_update_aggregator_entry(const char *original_name,
  299. const char *aggregated_name,
  300. const bool is_delete)
  301. {
  302. struct tomoyo_aggregator_entry e = { };
  303. int error = is_delete ? -ENOENT : -ENOMEM;
  304. if (!tomoyo_correct_path(original_name) ||
  305. !tomoyo_correct_path(aggregated_name))
  306. return -EINVAL;
  307. e.original_name = tomoyo_get_name(original_name);
  308. e.aggregated_name = tomoyo_get_name(aggregated_name);
  309. if (!e.original_name || !e.aggregated_name ||
  310. e.aggregated_name->is_patterned) /* No patterns allowed. */
  311. goto out;
  312. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  313. &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
  314. tomoyo_same_aggregator_entry);
  315. out:
  316. tomoyo_put_name(e.original_name);
  317. tomoyo_put_name(e.aggregated_name);
  318. return error;
  319. }
  320. /**
  321. * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
  322. *
  323. * @data: String to parse.
  324. * @is_delete: True if it is a delete request.
  325. *
  326. * Returns 0 on success, negative value otherwise.
  327. *
  328. * Caller holds tomoyo_read_lock().
  329. */
  330. int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
  331. {
  332. char *cp = strchr(data, ' ');
  333. if (!cp)
  334. return -EINVAL;
  335. *cp++ = '\0';
  336. return tomoyo_update_aggregator_entry(data, cp, is_delete);
  337. }
  338. /**
  339. * tomoyo_find_or_assign_new_domain - Create a domain.
  340. *
  341. * @domainname: The name of domain.
  342. * @profile: Profile number to assign if the domain was newly created.
  343. *
  344. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  345. *
  346. * Caller holds tomoyo_read_lock().
  347. */
  348. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  349. domainname,
  350. const u8 profile)
  351. {
  352. struct tomoyo_domain_info *entry;
  353. struct tomoyo_domain_info *domain = NULL;
  354. const struct tomoyo_path_info *saved_domainname;
  355. bool found = false;
  356. if (!tomoyo_correct_domain(domainname))
  357. return NULL;
  358. saved_domainname = tomoyo_get_name(domainname);
  359. if (!saved_domainname)
  360. return NULL;
  361. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  362. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  363. goto out;
  364. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  365. if (domain->is_deleted ||
  366. tomoyo_pathcmp(saved_domainname, domain->domainname))
  367. continue;
  368. found = true;
  369. break;
  370. }
  371. if (!found && tomoyo_memory_ok(entry)) {
  372. INIT_LIST_HEAD(&entry->acl_info_list);
  373. entry->domainname = saved_domainname;
  374. saved_domainname = NULL;
  375. entry->profile = profile;
  376. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  377. domain = entry;
  378. entry = NULL;
  379. found = true;
  380. }
  381. mutex_unlock(&tomoyo_policy_lock);
  382. out:
  383. tomoyo_put_name(saved_domainname);
  384. kfree(entry);
  385. return found ? domain : NULL;
  386. }
  387. /**
  388. * tomoyo_find_next_domain - Find a domain.
  389. *
  390. * @bprm: Pointer to "struct linux_binprm".
  391. *
  392. * Returns 0 on success, negative value otherwise.
  393. *
  394. * Caller holds tomoyo_read_lock().
  395. */
  396. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  397. {
  398. struct tomoyo_request_info r;
  399. char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  400. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  401. struct tomoyo_domain_info *domain = NULL;
  402. const char *original_name = bprm->filename;
  403. u8 mode;
  404. bool is_enforce;
  405. int retval = -ENOMEM;
  406. bool need_kfree = false;
  407. struct tomoyo_path_info rn = { }; /* real name */
  408. struct tomoyo_path_info ln; /* last name */
  409. ln.name = tomoyo_get_last_name(old_domain);
  410. tomoyo_fill_path_info(&ln);
  411. mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  412. is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
  413. if (!tmp)
  414. goto out;
  415. retry:
  416. if (need_kfree) {
  417. kfree(rn.name);
  418. need_kfree = false;
  419. }
  420. /* Get symlink's pathname of program. */
  421. retval = -ENOENT;
  422. rn.name = tomoyo_realpath_nofollow(original_name);
  423. if (!rn.name)
  424. goto out;
  425. tomoyo_fill_path_info(&rn);
  426. need_kfree = true;
  427. /* Check 'aggregator' directive. */
  428. {
  429. struct tomoyo_aggregator_entry *ptr;
  430. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  431. [TOMOYO_ID_AGGREGATOR], head.list) {
  432. if (ptr->head.is_deleted ||
  433. !tomoyo_path_matches_pattern(&rn,
  434. ptr->original_name))
  435. continue;
  436. kfree(rn.name);
  437. need_kfree = false;
  438. /* This is OK because it is read only. */
  439. rn = *ptr->aggregated_name;
  440. break;
  441. }
  442. }
  443. /* Check execute permission. */
  444. retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
  445. if (retval == TOMOYO_RETRY_REQUEST)
  446. goto retry;
  447. if (retval < 0)
  448. goto out;
  449. /* Calculate domain to transit to. */
  450. switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
  451. case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
  452. /* Transit to the child of tomoyo_kernel_domain domain. */
  453. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
  454. "%s", rn.name);
  455. break;
  456. case TOMOYO_TRANSITION_CONTROL_KEEP:
  457. /* Keep current domain. */
  458. domain = old_domain;
  459. break;
  460. default:
  461. if (old_domain == &tomoyo_kernel_domain &&
  462. !tomoyo_policy_loaded) {
  463. /*
  464. * Needn't to transit from kernel domain before
  465. * starting /sbin/init. But transit from kernel domain
  466. * if executing initializers because they might start
  467. * before /sbin/init.
  468. */
  469. domain = old_domain;
  470. } else {
  471. /* Normal domain transition. */
  472. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  473. old_domain->domainname->name, rn.name);
  474. }
  475. break;
  476. }
  477. if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
  478. goto done;
  479. domain = tomoyo_find_domain(tmp);
  480. if (domain)
  481. goto done;
  482. if (is_enforce) {
  483. int error = tomoyo_supervisor(&r, "# wants to create domain\n"
  484. "%s\n", tmp);
  485. if (error == TOMOYO_RETRY_REQUEST)
  486. goto retry;
  487. if (error < 0)
  488. goto done;
  489. }
  490. domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
  491. done:
  492. if (domain)
  493. goto out;
  494. printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
  495. if (is_enforce)
  496. retval = -EPERM;
  497. else
  498. old_domain->transition_failed = true;
  499. out:
  500. if (!domain)
  501. domain = old_domain;
  502. /* Update reference count on "struct tomoyo_domain_info". */
  503. atomic_inc(&domain->users);
  504. bprm->cred->security = domain;
  505. if (need_kfree)
  506. kfree(rn.name);
  507. kfree(tmp);
  508. return retval;
  509. }