domain.c 21 KB

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