domain.c 25 KB

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