domain.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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) (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_last_word - Get last component of a domainname.
  126. *
  127. * @domainname: Domainname to check.
  128. *
  129. * Returns the last word of @domainname.
  130. */
  131. static const char *tomoyo_last_word(const char *name)
  132. {
  133. const char *cp = strrchr(name, ' ');
  134. if (cp)
  135. return cp + 1;
  136. return name;
  137. }
  138. static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
  139. const struct tomoyo_acl_head *b)
  140. {
  141. const struct tomoyo_transition_control *p1 = container_of(a,
  142. typeof(*p1),
  143. head);
  144. const struct tomoyo_transition_control *p2 = container_of(b,
  145. typeof(*p2),
  146. head);
  147. return p1->type == p2->type && p1->is_last_name == p2->is_last_name
  148. && p1->domainname == p2->domainname
  149. && p1->program == p2->program;
  150. }
  151. /**
  152. * tomoyo_update_transition_control_entry - Update "struct tomoyo_transition_control" list.
  153. *
  154. * @domainname: The name of domain. Maybe NULL.
  155. * @program: The name of program. Maybe NULL.
  156. * @type: Type of transition.
  157. * @is_delete: True if it is a delete request.
  158. *
  159. * Returns 0 on success, negative value otherwise.
  160. */
  161. static int tomoyo_update_transition_control_entry(const char *domainname,
  162. const char *program,
  163. const u8 type,
  164. const bool is_delete)
  165. {
  166. struct tomoyo_transition_control e = { .type = type };
  167. int error = is_delete ? -ENOENT : -ENOMEM;
  168. if (program) {
  169. if (!tomoyo_correct_path(program))
  170. return -EINVAL;
  171. e.program = tomoyo_get_name(program);
  172. if (!e.program)
  173. goto out;
  174. }
  175. if (domainname) {
  176. if (!tomoyo_correct_domain(domainname)) {
  177. if (!tomoyo_correct_path(domainname))
  178. goto out;
  179. e.is_last_name = true;
  180. }
  181. e.domainname = tomoyo_get_name(domainname);
  182. if (!e.domainname)
  183. goto out;
  184. }
  185. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  186. &tomoyo_policy_list
  187. [TOMOYO_ID_TRANSITION_CONTROL],
  188. tomoyo_same_transition_control);
  189. out:
  190. tomoyo_put_name(e.domainname);
  191. tomoyo_put_name(e.program);
  192. return error;
  193. }
  194. /**
  195. * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
  196. *
  197. * @data: String to parse.
  198. * @is_delete: True if it is a delete request.
  199. * @type: Type of this entry.
  200. *
  201. * Returns 0 on success, negative value otherwise.
  202. */
  203. int tomoyo_write_transition_control(char *data, const bool is_delete,
  204. const u8 type)
  205. {
  206. char *domainname = strstr(data, " from ");
  207. if (domainname) {
  208. *domainname = '\0';
  209. domainname += 6;
  210. } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
  211. type == TOMOYO_TRANSITION_CONTROL_KEEP) {
  212. domainname = data;
  213. data = NULL;
  214. }
  215. return tomoyo_update_transition_control_entry(domainname, data, type,
  216. is_delete);
  217. }
  218. /**
  219. * tomoyo_transition_type - Get domain transition type.
  220. *
  221. * @domainname: The name of domain.
  222. * @program: The name of program.
  223. *
  224. * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
  225. * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
  226. * @program suppresses domain transition, others otherwise.
  227. *
  228. * Caller holds tomoyo_read_lock().
  229. */
  230. static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
  231. const struct tomoyo_path_info *program)
  232. {
  233. const struct tomoyo_transition_control *ptr;
  234. const char *last_name = tomoyo_last_word(domainname->name);
  235. u8 type;
  236. for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
  237. next:
  238. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  239. [TOMOYO_ID_TRANSITION_CONTROL],
  240. head.list) {
  241. if (ptr->head.is_deleted || ptr->type != type)
  242. continue;
  243. if (ptr->domainname) {
  244. if (!ptr->is_last_name) {
  245. if (ptr->domainname != domainname)
  246. continue;
  247. } else {
  248. /*
  249. * Use direct strcmp() since this is
  250. * unlikely used.
  251. */
  252. if (strcmp(ptr->domainname->name,
  253. last_name))
  254. continue;
  255. }
  256. }
  257. if (ptr->program &&
  258. tomoyo_pathcmp(ptr->program, program))
  259. continue;
  260. if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
  261. /*
  262. * Do not check for initialize_domain if
  263. * no_initialize_domain matched.
  264. */
  265. type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
  266. goto next;
  267. }
  268. goto done;
  269. }
  270. }
  271. done:
  272. return type;
  273. }
  274. static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
  275. const struct tomoyo_acl_head *b)
  276. {
  277. const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1), head);
  278. const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2), head);
  279. return p1->original_name == p2->original_name &&
  280. p1->aggregated_name == p2->aggregated_name;
  281. }
  282. /**
  283. * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator" list.
  284. *
  285. * @original_name: The original program's name.
  286. * @aggregated_name: The program name to use.
  287. * @is_delete: True if it is a delete request.
  288. *
  289. * Returns 0 on success, negative value otherwise.
  290. *
  291. * Caller holds tomoyo_read_lock().
  292. */
  293. static int tomoyo_update_aggregator_entry(const char *original_name,
  294. const char *aggregated_name,
  295. const bool is_delete)
  296. {
  297. struct tomoyo_aggregator e = { };
  298. int error = is_delete ? -ENOENT : -ENOMEM;
  299. if (!tomoyo_correct_path(original_name) ||
  300. !tomoyo_correct_path(aggregated_name))
  301. return -EINVAL;
  302. e.original_name = tomoyo_get_name(original_name);
  303. e.aggregated_name = tomoyo_get_name(aggregated_name);
  304. if (!e.original_name || !e.aggregated_name ||
  305. e.aggregated_name->is_patterned) /* No patterns allowed. */
  306. goto out;
  307. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  308. &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
  309. tomoyo_same_aggregator);
  310. out:
  311. tomoyo_put_name(e.original_name);
  312. tomoyo_put_name(e.aggregated_name);
  313. return error;
  314. }
  315. /**
  316. * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
  317. *
  318. * @data: String to parse.
  319. * @is_delete: True if it is a delete request.
  320. *
  321. * Returns 0 on success, negative value otherwise.
  322. *
  323. * Caller holds tomoyo_read_lock().
  324. */
  325. int tomoyo_write_aggregator(char *data, const bool is_delete)
  326. {
  327. char *cp = strchr(data, ' ');
  328. if (!cp)
  329. return -EINVAL;
  330. *cp++ = '\0';
  331. return tomoyo_update_aggregator_entry(data, cp, is_delete);
  332. }
  333. /**
  334. * tomoyo_assign_domain - Create a domain.
  335. *
  336. * @domainname: The name of domain.
  337. * @profile: Profile number to assign if the domain was newly created.
  338. *
  339. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  340. *
  341. * Caller holds tomoyo_read_lock().
  342. */
  343. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  344. const u8 profile)
  345. {
  346. struct tomoyo_domain_info *entry;
  347. struct tomoyo_domain_info *domain = NULL;
  348. const struct tomoyo_path_info *saved_domainname;
  349. bool found = false;
  350. if (!tomoyo_correct_domain(domainname))
  351. return NULL;
  352. saved_domainname = tomoyo_get_name(domainname);
  353. if (!saved_domainname)
  354. return NULL;
  355. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  356. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  357. goto out;
  358. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  359. if (domain->is_deleted ||
  360. tomoyo_pathcmp(saved_domainname, domain->domainname))
  361. continue;
  362. found = true;
  363. break;
  364. }
  365. if (!found && tomoyo_memory_ok(entry)) {
  366. INIT_LIST_HEAD(&entry->acl_info_list);
  367. entry->domainname = saved_domainname;
  368. saved_domainname = NULL;
  369. entry->profile = profile;
  370. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  371. domain = entry;
  372. entry = NULL;
  373. found = true;
  374. }
  375. mutex_unlock(&tomoyo_policy_lock);
  376. out:
  377. tomoyo_put_name(saved_domainname);
  378. kfree(entry);
  379. return found ? domain : NULL;
  380. }
  381. /**
  382. * tomoyo_find_next_domain - Find a domain.
  383. *
  384. * @bprm: Pointer to "struct linux_binprm".
  385. *
  386. * Returns 0 on success, negative value otherwise.
  387. *
  388. * Caller holds tomoyo_read_lock().
  389. */
  390. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  391. {
  392. struct tomoyo_request_info r;
  393. char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  394. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  395. struct tomoyo_domain_info *domain = NULL;
  396. const char *original_name = bprm->filename;
  397. u8 mode;
  398. bool is_enforce;
  399. int retval = -ENOMEM;
  400. bool need_kfree = false;
  401. struct tomoyo_path_info rn = { }; /* real name */
  402. mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  403. is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
  404. if (!tmp)
  405. goto out;
  406. retry:
  407. if (need_kfree) {
  408. kfree(rn.name);
  409. need_kfree = false;
  410. }
  411. /* Get symlink's pathname of program. */
  412. retval = -ENOENT;
  413. rn.name = tomoyo_realpath_nofollow(original_name);
  414. if (!rn.name)
  415. goto out;
  416. tomoyo_fill_path_info(&rn);
  417. need_kfree = true;
  418. /* Check 'aggregator' directive. */
  419. {
  420. struct tomoyo_aggregator *ptr;
  421. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  422. [TOMOYO_ID_AGGREGATOR], head.list) {
  423. if (ptr->head.is_deleted ||
  424. !tomoyo_path_matches_pattern(&rn,
  425. ptr->original_name))
  426. continue;
  427. kfree(rn.name);
  428. need_kfree = false;
  429. /* This is OK because it is read only. */
  430. rn = *ptr->aggregated_name;
  431. break;
  432. }
  433. }
  434. /* Check execute permission. */
  435. retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
  436. if (retval == TOMOYO_RETRY_REQUEST)
  437. goto retry;
  438. if (retval < 0)
  439. goto out;
  440. /*
  441. * To be able to specify domainnames with wildcards, use the
  442. * pathname specified in the policy (which may contain
  443. * wildcard) rather than the pathname passed to execve()
  444. * (which never contains wildcard).
  445. */
  446. if (r.param.path.matched_path) {
  447. if (need_kfree)
  448. kfree(rn.name);
  449. need_kfree = false;
  450. /* This is OK because it is read only. */
  451. rn = *r.param.path.matched_path;
  452. }
  453. /* Calculate domain to transit to. */
  454. switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
  455. case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
  456. /* Transit to the child of tomoyo_kernel_domain domain. */
  457. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
  458. "%s", rn.name);
  459. break;
  460. case TOMOYO_TRANSITION_CONTROL_KEEP:
  461. /* Keep current domain. */
  462. domain = old_domain;
  463. break;
  464. default:
  465. if (old_domain == &tomoyo_kernel_domain &&
  466. !tomoyo_policy_loaded) {
  467. /*
  468. * Needn't to transit from kernel domain before
  469. * starting /sbin/init. But transit from kernel domain
  470. * if executing initializers because they might start
  471. * before /sbin/init.
  472. */
  473. domain = old_domain;
  474. } else {
  475. /* Normal domain transition. */
  476. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  477. old_domain->domainname->name, rn.name);
  478. }
  479. break;
  480. }
  481. if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
  482. goto done;
  483. domain = tomoyo_find_domain(tmp);
  484. if (domain)
  485. goto done;
  486. if (is_enforce) {
  487. int error = tomoyo_supervisor(&r, "# wants to create domain\n"
  488. "%s\n", tmp);
  489. if (error == TOMOYO_RETRY_REQUEST)
  490. goto retry;
  491. if (error < 0)
  492. goto done;
  493. }
  494. domain = tomoyo_assign_domain(tmp, old_domain->profile);
  495. done:
  496. if (domain)
  497. goto out;
  498. printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
  499. if (is_enforce)
  500. retval = -EPERM;
  501. else
  502. old_domain->transition_failed = true;
  503. out:
  504. if (!domain)
  505. domain = old_domain;
  506. /* Update reference count on "struct tomoyo_domain_info". */
  507. atomic_inc(&domain->users);
  508. bprm->cred->security = domain;
  509. if (need_kfree)
  510. kfree(rn.name);
  511. kfree(tmp);
  512. return retval;
  513. }