domain.c 25 KB

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