domain.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. * @param: Pointer to "struct tomoyo_acl_param".
  20. * @check_duplicate: Callback function to find duplicated entry.
  21. *
  22. * Returns 0 on success, negative value otherwise.
  23. *
  24. * Caller holds tomoyo_read_lock().
  25. */
  26. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  27. struct tomoyo_acl_param *param,
  28. bool (*check_duplicate) (const struct tomoyo_acl_head
  29. *,
  30. const struct tomoyo_acl_head
  31. *))
  32. {
  33. int error = param->is_delete ? -ENOENT : -ENOMEM;
  34. struct tomoyo_acl_head *entry;
  35. struct list_head *list = param->list;
  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 = param->is_delete;
  42. error = 0;
  43. break;
  44. }
  45. if (error && !param->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_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
  57. *
  58. * @a: Pointer to "struct tomoyo_acl_info".
  59. * @b: Pointer to "struct tomoyo_acl_info".
  60. *
  61. * Returns true if @a == @b, false otherwise.
  62. */
  63. static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
  64. const struct tomoyo_acl_info *b)
  65. {
  66. return a->type == b->type;
  67. }
  68. /**
  69. * tomoyo_update_domain - Update an entry for domain policy.
  70. *
  71. * @new_entry: Pointer to "struct tomoyo_acl_info".
  72. * @size: Size of @new_entry in bytes.
  73. * @param: Pointer to "struct tomoyo_acl_param".
  74. * @check_duplicate: Callback function to find duplicated entry.
  75. * @merge_duplicate: Callback function to merge duplicated entry.
  76. *
  77. * Returns 0 on success, negative value otherwise.
  78. *
  79. * Caller holds tomoyo_read_lock().
  80. */
  81. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  82. struct tomoyo_acl_param *param,
  83. bool (*check_duplicate) (const struct tomoyo_acl_info
  84. *,
  85. const struct tomoyo_acl_info
  86. *),
  87. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  88. struct tomoyo_acl_info *,
  89. const bool))
  90. {
  91. const bool is_delete = param->is_delete;
  92. int error = is_delete ? -ENOENT : -ENOMEM;
  93. struct tomoyo_acl_info *entry;
  94. struct list_head * const list = param->list;
  95. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  96. return error;
  97. list_for_each_entry_rcu(entry, list, list) {
  98. if (!tomoyo_same_acl_head(entry, new_entry) ||
  99. !check_duplicate(entry, new_entry))
  100. continue;
  101. if (merge_duplicate)
  102. entry->is_deleted = merge_duplicate(entry, new_entry,
  103. is_delete);
  104. else
  105. entry->is_deleted = is_delete;
  106. error = 0;
  107. break;
  108. }
  109. if (error && !is_delete) {
  110. entry = tomoyo_commit_ok(new_entry, size);
  111. if (entry) {
  112. list_add_tail_rcu(&entry->list, list);
  113. error = 0;
  114. }
  115. }
  116. mutex_unlock(&tomoyo_policy_lock);
  117. return error;
  118. }
  119. /**
  120. * tomoyo_check_acl - Do permission check.
  121. *
  122. * @r: Pointer to "struct tomoyo_request_info".
  123. * @check_entry: Callback function to check type specific parameters.
  124. *
  125. * Returns 0 on success, negative value otherwise.
  126. *
  127. * Caller holds tomoyo_read_lock().
  128. */
  129. void tomoyo_check_acl(struct tomoyo_request_info *r,
  130. bool (*check_entry) (struct tomoyo_request_info *,
  131. const struct tomoyo_acl_info *))
  132. {
  133. const struct tomoyo_domain_info *domain = r->domain;
  134. struct tomoyo_acl_info *ptr;
  135. bool retried = false;
  136. const struct list_head *list = &domain->acl_info_list;
  137. retry:
  138. list_for_each_entry_rcu(ptr, list, list) {
  139. if (ptr->is_deleted || ptr->type != r->param_type)
  140. continue;
  141. if (check_entry(r, ptr)) {
  142. r->granted = true;
  143. return;
  144. }
  145. }
  146. if (!retried) {
  147. retried = true;
  148. list = &domain->ns->acl_group[domain->group];
  149. goto retry;
  150. }
  151. r->granted = false;
  152. }
  153. /* The list for "struct tomoyo_domain_info". */
  154. LIST_HEAD(tomoyo_domain_list);
  155. /**
  156. * tomoyo_last_word - Get last component of a domainname.
  157. *
  158. * @name: Domainname to check.
  159. *
  160. * Returns the last word of @domainname.
  161. */
  162. static const char *tomoyo_last_word(const char *name)
  163. {
  164. const char *cp = strrchr(name, ' ');
  165. if (cp)
  166. return cp + 1;
  167. return name;
  168. }
  169. /**
  170. * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
  171. *
  172. * @a: Pointer to "struct tomoyo_acl_head".
  173. * @b: Pointer to "struct tomoyo_acl_head".
  174. *
  175. * Returns true if @a == @b, false otherwise.
  176. */
  177. static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
  178. const struct tomoyo_acl_head *b)
  179. {
  180. const struct tomoyo_transition_control *p1 = container_of(a,
  181. typeof(*p1),
  182. head);
  183. const struct tomoyo_transition_control *p2 = container_of(b,
  184. typeof(*p2),
  185. head);
  186. return p1->type == p2->type && p1->is_last_name == p2->is_last_name
  187. && p1->domainname == p2->domainname
  188. && p1->program == p2->program;
  189. }
  190. /**
  191. * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
  192. *
  193. * @param: Pointer to "struct tomoyo_acl_param".
  194. * @type: Type of this entry.
  195. *
  196. * Returns 0 on success, negative value otherwise.
  197. */
  198. int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
  199. const u8 type)
  200. {
  201. struct tomoyo_transition_control e = { .type = type };
  202. int error = param->is_delete ? -ENOENT : -ENOMEM;
  203. char *program = param->data;
  204. char *domainname = strstr(program, " from ");
  205. if (domainname) {
  206. *domainname = '\0';
  207. domainname += 6;
  208. } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
  209. type == TOMOYO_TRANSITION_CONTROL_KEEP) {
  210. domainname = program;
  211. program = NULL;
  212. }
  213. if (program && strcmp(program, "any")) {
  214. if (!tomoyo_correct_path(program))
  215. return -EINVAL;
  216. e.program = tomoyo_get_name(program);
  217. if (!e.program)
  218. goto out;
  219. }
  220. if (domainname && strcmp(domainname, "any")) {
  221. if (!tomoyo_correct_domain(domainname)) {
  222. if (!tomoyo_correct_path(domainname))
  223. goto out;
  224. e.is_last_name = true;
  225. }
  226. e.domainname = tomoyo_get_name(domainname);
  227. if (!e.domainname)
  228. goto out;
  229. }
  230. param->list = &param->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
  231. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  232. tomoyo_same_transition_control);
  233. out:
  234. tomoyo_put_name(e.domainname);
  235. tomoyo_put_name(e.program);
  236. return error;
  237. }
  238. /**
  239. * tomoyo_scan_transition - Try to find specific domain transition type.
  240. *
  241. * @list: Pointer to "struct list_head".
  242. * @domainname: The name of current domain.
  243. * @program: The name of requested program.
  244. * @last_name: The last component of @domainname.
  245. * @type: One of values in "enum tomoyo_transition_type".
  246. *
  247. * Returns true if found one, false otherwise.
  248. *
  249. * Caller holds tomoyo_read_lock().
  250. */
  251. static inline bool tomoyo_scan_transition
  252. (const struct list_head *list, const struct tomoyo_path_info *domainname,
  253. const struct tomoyo_path_info *program, const char *last_name,
  254. const enum tomoyo_transition_type type)
  255. {
  256. const struct tomoyo_transition_control *ptr;
  257. list_for_each_entry_rcu(ptr, list, head.list) {
  258. if (ptr->head.is_deleted || ptr->type != type)
  259. continue;
  260. if (ptr->domainname) {
  261. if (!ptr->is_last_name) {
  262. if (ptr->domainname != domainname)
  263. continue;
  264. } else {
  265. /*
  266. * Use direct strcmp() since this is
  267. * unlikely used.
  268. */
  269. if (strcmp(ptr->domainname->name, last_name))
  270. continue;
  271. }
  272. }
  273. if (ptr->program && tomoyo_pathcmp(ptr->program, program))
  274. continue;
  275. return true;
  276. }
  277. return false;
  278. }
  279. /**
  280. * tomoyo_transition_type - Get domain transition type.
  281. *
  282. * @ns: Pointer to "struct tomoyo_policy_namespace".
  283. * @domainname: The name of current domain.
  284. * @program: The name of requested program.
  285. *
  286. * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
  287. * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
  288. * executing @program reinitializes domain transition within that namespace,
  289. * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
  290. * others otherwise.
  291. *
  292. * Caller holds tomoyo_read_lock().
  293. */
  294. static enum tomoyo_transition_type tomoyo_transition_type
  295. (const struct tomoyo_policy_namespace *ns,
  296. const struct tomoyo_path_info *domainname,
  297. const struct tomoyo_path_info *program)
  298. {
  299. const char *last_name = tomoyo_last_word(domainname->name);
  300. enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
  301. while (type < TOMOYO_MAX_TRANSITION_TYPE) {
  302. const struct list_head * const list =
  303. &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
  304. if (!tomoyo_scan_transition(list, domainname, program,
  305. last_name, type)) {
  306. type++;
  307. continue;
  308. }
  309. if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET &&
  310. type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE)
  311. break;
  312. /*
  313. * Do not check for reset_domain if no_reset_domain matched.
  314. * Do not check for initialize_domain if no_initialize_domain
  315. * matched.
  316. */
  317. type++;
  318. type++;
  319. }
  320. return type;
  321. }
  322. /**
  323. * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
  324. *
  325. * @a: Pointer to "struct tomoyo_acl_head".
  326. * @b: Pointer to "struct tomoyo_acl_head".
  327. *
  328. * Returns true if @a == @b, false otherwise.
  329. */
  330. static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
  331. const struct tomoyo_acl_head *b)
  332. {
  333. const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
  334. head);
  335. const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
  336. head);
  337. return p1->original_name == p2->original_name &&
  338. p1->aggregated_name == p2->aggregated_name;
  339. }
  340. /**
  341. * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
  342. *
  343. * @param: Pointer to "struct tomoyo_acl_param".
  344. *
  345. * Returns 0 on success, negative value otherwise.
  346. *
  347. * Caller holds tomoyo_read_lock().
  348. */
  349. int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
  350. {
  351. struct tomoyo_aggregator e = { };
  352. int error = param->is_delete ? -ENOENT : -ENOMEM;
  353. const char *original_name = tomoyo_read_token(param);
  354. const char *aggregated_name = tomoyo_read_token(param);
  355. if (!tomoyo_correct_word(original_name) ||
  356. !tomoyo_correct_path(aggregated_name))
  357. return -EINVAL;
  358. e.original_name = tomoyo_get_name(original_name);
  359. e.aggregated_name = tomoyo_get_name(aggregated_name);
  360. if (!e.original_name || !e.aggregated_name ||
  361. e.aggregated_name->is_patterned) /* No patterns allowed. */
  362. goto out;
  363. param->list = &param->ns->policy_list[TOMOYO_ID_AGGREGATOR];
  364. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  365. tomoyo_same_aggregator);
  366. out:
  367. tomoyo_put_name(e.original_name);
  368. tomoyo_put_name(e.aggregated_name);
  369. return error;
  370. }
  371. /**
  372. * tomoyo_find_namespace - Find specified namespace.
  373. *
  374. * @name: Name of namespace to find.
  375. * @len: Length of @name.
  376. *
  377. * Returns pointer to "struct tomoyo_policy_namespace" if found,
  378. * NULL otherwise.
  379. *
  380. * Caller holds tomoyo_read_lock().
  381. */
  382. static struct tomoyo_policy_namespace *tomoyo_find_namespace
  383. (const char *name, const unsigned int len)
  384. {
  385. struct tomoyo_policy_namespace *ns;
  386. list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
  387. if (strncmp(name, ns->name, len) ||
  388. (name[len] && name[len] != ' '))
  389. continue;
  390. return ns;
  391. }
  392. return NULL;
  393. }
  394. /**
  395. * tomoyo_assign_namespace - Create a new namespace.
  396. *
  397. * @domainname: Name of namespace to create.
  398. *
  399. * Returns pointer to "struct tomoyo_policy_namespace" on success,
  400. * NULL otherwise.
  401. *
  402. * Caller holds tomoyo_read_lock().
  403. */
  404. struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname)
  405. {
  406. struct tomoyo_policy_namespace *ptr;
  407. struct tomoyo_policy_namespace *entry;
  408. const char *cp = domainname;
  409. unsigned int len = 0;
  410. while (*cp && *cp++ != ' ')
  411. len++;
  412. ptr = tomoyo_find_namespace(domainname, len);
  413. if (ptr)
  414. return ptr;
  415. if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname))
  416. return NULL;
  417. entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS);
  418. if (!entry)
  419. return NULL;
  420. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  421. goto out;
  422. ptr = tomoyo_find_namespace(domainname, len);
  423. if (!ptr && tomoyo_memory_ok(entry)) {
  424. char *name = (char *) (entry + 1);
  425. ptr = entry;
  426. memmove(name, domainname, len);
  427. name[len] = '\0';
  428. entry->name = name;
  429. tomoyo_init_policy_namespace(entry);
  430. entry = NULL;
  431. }
  432. mutex_unlock(&tomoyo_policy_lock);
  433. out:
  434. kfree(entry);
  435. return ptr;
  436. }
  437. /**
  438. * tomoyo_namespace_jump - Check for namespace jump.
  439. *
  440. * @domainname: Name of domain.
  441. *
  442. * Returns true if namespace differs, false otherwise.
  443. */
  444. static bool tomoyo_namespace_jump(const char *domainname)
  445. {
  446. const char *namespace = tomoyo_current_namespace()->name;
  447. const int len = strlen(namespace);
  448. return strncmp(domainname, namespace, len) ||
  449. (domainname[len] && domainname[len] != ' ');
  450. }
  451. /**
  452. * tomoyo_assign_domain - Create a domain or a namespace.
  453. *
  454. * @domainname: The name of domain.
  455. * @transit: True if transit to domain found or created.
  456. *
  457. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  458. *
  459. * Caller holds tomoyo_read_lock().
  460. */
  461. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  462. const bool transit)
  463. {
  464. struct tomoyo_domain_info e = { };
  465. struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
  466. bool created = false;
  467. if (entry) {
  468. if (transit) {
  469. /*
  470. * Since namespace is created at runtime, profiles may
  471. * not be created by the moment the process transits to
  472. * that domain. Do not perform domain transition if
  473. * profile for that domain is not yet created.
  474. */
  475. if (!entry->ns->profile_ptr[entry->profile])
  476. return NULL;
  477. }
  478. return entry;
  479. }
  480. /* Requested domain does not exist. */
  481. /* Don't create requested domain if domainname is invalid. */
  482. if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 ||
  483. !tomoyo_correct_domain(domainname))
  484. return NULL;
  485. /*
  486. * Since definition of profiles and acl_groups may differ across
  487. * namespaces, do not inherit "use_profile" and "use_group" settings
  488. * by automatically creating requested domain upon domain transition.
  489. */
  490. if (transit && tomoyo_namespace_jump(domainname))
  491. return NULL;
  492. e.ns = tomoyo_assign_namespace(domainname);
  493. if (!e.ns)
  494. return NULL;
  495. /*
  496. * "use_profile" and "use_group" settings for automatically created
  497. * domains are inherited from current domain. These are 0 for manually
  498. * created domains.
  499. */
  500. if (transit) {
  501. const struct tomoyo_domain_info *domain = tomoyo_domain();
  502. e.profile = domain->profile;
  503. e.group = domain->group;
  504. }
  505. e.domainname = tomoyo_get_name(domainname);
  506. if (!e.domainname)
  507. return NULL;
  508. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  509. goto out;
  510. entry = tomoyo_find_domain(domainname);
  511. if (!entry) {
  512. entry = tomoyo_commit_ok(&e, sizeof(e));
  513. if (entry) {
  514. INIT_LIST_HEAD(&entry->acl_info_list);
  515. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  516. created = true;
  517. }
  518. }
  519. mutex_unlock(&tomoyo_policy_lock);
  520. out:
  521. tomoyo_put_name(e.domainname);
  522. if (entry && transit) {
  523. if (created) {
  524. struct tomoyo_request_info r;
  525. tomoyo_init_request_info(&r, entry,
  526. TOMOYO_MAC_FILE_EXECUTE);
  527. r.granted = false;
  528. tomoyo_write_log(&r, "use_profile %u\n",
  529. entry->profile);
  530. tomoyo_write_log(&r, "use_group %u\n", entry->group);
  531. }
  532. }
  533. return entry;
  534. }
  535. /**
  536. * tomoyo_find_next_domain - Find a domain.
  537. *
  538. * @bprm: Pointer to "struct linux_binprm".
  539. *
  540. * Returns 0 on success, negative value otherwise.
  541. *
  542. * Caller holds tomoyo_read_lock().
  543. */
  544. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  545. {
  546. struct tomoyo_request_info r;
  547. char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  548. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  549. struct tomoyo_domain_info *domain = NULL;
  550. const char *original_name = bprm->filename;
  551. u8 mode;
  552. bool is_enforce;
  553. int retval = -ENOMEM;
  554. bool need_kfree = false;
  555. bool reject_on_transition_failure = false;
  556. struct tomoyo_path_info rn = { }; /* real name */
  557. mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  558. is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
  559. if (!tmp)
  560. goto out;
  561. retry:
  562. if (need_kfree) {
  563. kfree(rn.name);
  564. need_kfree = false;
  565. }
  566. /* Get symlink's pathname of program. */
  567. retval = -ENOENT;
  568. rn.name = tomoyo_realpath_nofollow(original_name);
  569. if (!rn.name)
  570. goto out;
  571. tomoyo_fill_path_info(&rn);
  572. need_kfree = true;
  573. /* Check 'aggregator' directive. */
  574. {
  575. struct tomoyo_aggregator *ptr;
  576. struct list_head *list =
  577. &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
  578. /* Check 'aggregator' directive. */
  579. list_for_each_entry_rcu(ptr, list, head.list) {
  580. if (ptr->head.is_deleted ||
  581. !tomoyo_path_matches_pattern(&rn,
  582. ptr->original_name))
  583. continue;
  584. kfree(rn.name);
  585. need_kfree = false;
  586. /* This is OK because it is read only. */
  587. rn = *ptr->aggregated_name;
  588. break;
  589. }
  590. }
  591. /* Check execute permission. */
  592. retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
  593. if (retval == TOMOYO_RETRY_REQUEST)
  594. goto retry;
  595. if (retval < 0)
  596. goto out;
  597. /*
  598. * To be able to specify domainnames with wildcards, use the
  599. * pathname specified in the policy (which may contain
  600. * wildcard) rather than the pathname passed to execve()
  601. * (which never contains wildcard).
  602. */
  603. if (r.param.path.matched_path) {
  604. if (need_kfree)
  605. kfree(rn.name);
  606. need_kfree = false;
  607. /* This is OK because it is read only. */
  608. rn = *r.param.path.matched_path;
  609. }
  610. /* Calculate domain to transit to. */
  611. switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
  612. &rn)) {
  613. case TOMOYO_TRANSITION_CONTROL_RESET:
  614. /* Transit to the root of specified namespace. */
  615. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", rn.name);
  616. /*
  617. * Make do_execve() fail if domain transition across namespaces
  618. * has failed.
  619. */
  620. reject_on_transition_failure = true;
  621. break;
  622. case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
  623. /* Transit to the child of current namespace's root. */
  624. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  625. old_domain->ns->name, rn.name);
  626. break;
  627. case TOMOYO_TRANSITION_CONTROL_KEEP:
  628. /* Keep current domain. */
  629. domain = old_domain;
  630. break;
  631. default:
  632. if (old_domain == &tomoyo_kernel_domain &&
  633. !tomoyo_policy_loaded) {
  634. /*
  635. * Needn't to transit from kernel domain before
  636. * starting /sbin/init. But transit from kernel domain
  637. * if executing initializers because they might start
  638. * before /sbin/init.
  639. */
  640. domain = old_domain;
  641. } else {
  642. /* Normal domain transition. */
  643. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  644. old_domain->domainname->name, rn.name);
  645. }
  646. break;
  647. }
  648. if (!domain)
  649. domain = tomoyo_assign_domain(tmp, true);
  650. if (domain)
  651. retval = 0;
  652. else if (reject_on_transition_failure) {
  653. printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n", tmp);
  654. retval = -ENOMEM;
  655. } else if (r.mode == TOMOYO_CONFIG_ENFORCING)
  656. retval = -ENOMEM;
  657. else {
  658. retval = 0;
  659. if (!old_domain->transition_failed) {
  660. old_domain->transition_failed = true;
  661. r.granted = false;
  662. tomoyo_write_log(&r, "%s", "transition_failed\n");
  663. printk(KERN_WARNING
  664. "ERROR: Domain '%s' not defined.\n", tmp);
  665. }
  666. }
  667. out:
  668. if (!domain)
  669. domain = old_domain;
  670. /* Update reference count on "struct tomoyo_domain_info". */
  671. atomic_inc(&domain->users);
  672. bprm->cred->security = domain;
  673. if (need_kfree)
  674. kfree(rn.name);
  675. kfree(tmp);
  676. return retval;
  677. }