domain.c 21 KB

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