domain.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. }
  549. }
  550. return entry;
  551. }
  552. /**
  553. * tomoyo_environ - Check permission for environment variable names.
  554. *
  555. * @ee: Pointer to "struct tomoyo_execve".
  556. *
  557. * Returns 0 on success, negative value otherwise.
  558. */
  559. static int tomoyo_environ(struct tomoyo_execve *ee)
  560. {
  561. struct tomoyo_request_info *r = &ee->r;
  562. struct linux_binprm *bprm = ee->bprm;
  563. /* env_page.data is allocated by tomoyo_dump_page(). */
  564. struct tomoyo_page_dump env_page = { };
  565. char *arg_ptr; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
  566. int arg_len = 0;
  567. unsigned long pos = bprm->p;
  568. int offset = pos % PAGE_SIZE;
  569. int argv_count = bprm->argc;
  570. int envp_count = bprm->envc;
  571. int error = -ENOMEM;
  572. ee->r.type = TOMOYO_MAC_ENVIRON;
  573. ee->r.profile = r->domain->profile;
  574. ee->r.mode = tomoyo_get_mode(r->domain->ns, ee->r.profile,
  575. TOMOYO_MAC_ENVIRON);
  576. if (!r->mode || !envp_count)
  577. return 0;
  578. arg_ptr = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  579. if (!arg_ptr)
  580. goto out;
  581. while (error == -ENOMEM) {
  582. if (!tomoyo_dump_page(bprm, pos, &env_page))
  583. goto out;
  584. pos += PAGE_SIZE - offset;
  585. /* Read. */
  586. while (argv_count && offset < PAGE_SIZE) {
  587. if (!env_page.data[offset++])
  588. argv_count--;
  589. }
  590. if (argv_count) {
  591. offset = 0;
  592. continue;
  593. }
  594. while (offset < PAGE_SIZE) {
  595. const unsigned char c = env_page.data[offset++];
  596. if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
  597. if (c == '=') {
  598. arg_ptr[arg_len++] = '\0';
  599. } else if (c == '\\') {
  600. arg_ptr[arg_len++] = '\\';
  601. arg_ptr[arg_len++] = '\\';
  602. } else if (c > ' ' && c < 127) {
  603. arg_ptr[arg_len++] = c;
  604. } else {
  605. arg_ptr[arg_len++] = '\\';
  606. arg_ptr[arg_len++] = (c >> 6) + '0';
  607. arg_ptr[arg_len++]
  608. = ((c >> 3) & 7) + '0';
  609. arg_ptr[arg_len++] = (c & 7) + '0';
  610. }
  611. } else {
  612. arg_ptr[arg_len] = '\0';
  613. }
  614. if (c)
  615. continue;
  616. if (tomoyo_env_perm(r, arg_ptr)) {
  617. error = -EPERM;
  618. break;
  619. }
  620. if (!--envp_count) {
  621. error = 0;
  622. break;
  623. }
  624. arg_len = 0;
  625. }
  626. offset = 0;
  627. }
  628. out:
  629. if (r->mode != TOMOYO_CONFIG_ENFORCING)
  630. error = 0;
  631. kfree(env_page.data);
  632. kfree(arg_ptr);
  633. return error;
  634. }
  635. /**
  636. * tomoyo_find_next_domain - Find a domain.
  637. *
  638. * @bprm: Pointer to "struct linux_binprm".
  639. *
  640. * Returns 0 on success, negative value otherwise.
  641. *
  642. * Caller holds tomoyo_read_lock().
  643. */
  644. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  645. {
  646. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  647. struct tomoyo_domain_info *domain = NULL;
  648. const char *original_name = bprm->filename;
  649. int retval = -ENOMEM;
  650. bool reject_on_transition_failure = false;
  651. const struct tomoyo_path_info *candidate;
  652. struct tomoyo_path_info exename;
  653. struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
  654. if (!ee)
  655. return -ENOMEM;
  656. ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  657. if (!ee->tmp) {
  658. kfree(ee);
  659. return -ENOMEM;
  660. }
  661. /* ee->dump->data is allocated by tomoyo_dump_page(). */
  662. tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  663. ee->r.ee = ee;
  664. ee->bprm = bprm;
  665. ee->r.obj = &ee->obj;
  666. ee->obj.path1 = bprm->file->f_path;
  667. /* Get symlink's pathname of program. */
  668. retval = -ENOENT;
  669. exename.name = tomoyo_realpath_nofollow(original_name);
  670. if (!exename.name)
  671. goto out;
  672. tomoyo_fill_path_info(&exename);
  673. retry:
  674. /* Check 'aggregator' directive. */
  675. {
  676. struct tomoyo_aggregator *ptr;
  677. struct list_head *list =
  678. &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
  679. /* Check 'aggregator' directive. */
  680. candidate = &exename;
  681. list_for_each_entry_rcu(ptr, list, head.list) {
  682. if (ptr->head.is_deleted ||
  683. !tomoyo_path_matches_pattern(&exename,
  684. ptr->original_name))
  685. continue;
  686. candidate = ptr->aggregated_name;
  687. break;
  688. }
  689. }
  690. /* Check execute permission. */
  691. retval = tomoyo_execute_permission(&ee->r, candidate);
  692. if (retval == TOMOYO_RETRY_REQUEST)
  693. goto retry;
  694. if (retval < 0)
  695. goto out;
  696. /*
  697. * To be able to specify domainnames with wildcards, use the
  698. * pathname specified in the policy (which may contain
  699. * wildcard) rather than the pathname passed to execve()
  700. * (which never contains wildcard).
  701. */
  702. if (ee->r.param.path.matched_path)
  703. candidate = ee->r.param.path.matched_path;
  704. /*
  705. * Check for domain transition preference if "file execute" matched.
  706. * If preference is given, make do_execve() fail if domain transition
  707. * has failed, for domain transition preference should be used with
  708. * destination domain defined.
  709. */
  710. if (ee->transition) {
  711. const char *domainname = ee->transition->name;
  712. reject_on_transition_failure = true;
  713. if (!strcmp(domainname, "keep"))
  714. goto force_keep_domain;
  715. if (!strcmp(domainname, "child"))
  716. goto force_child_domain;
  717. if (!strcmp(domainname, "reset"))
  718. goto force_reset_domain;
  719. if (!strcmp(domainname, "initialize"))
  720. goto force_initialize_domain;
  721. if (!strcmp(domainname, "parent")) {
  722. char *cp;
  723. strncpy(ee->tmp, old_domain->domainname->name,
  724. TOMOYO_EXEC_TMPSIZE - 1);
  725. cp = strrchr(ee->tmp, ' ');
  726. if (cp)
  727. *cp = '\0';
  728. } else if (*domainname == '<')
  729. strncpy(ee->tmp, domainname, TOMOYO_EXEC_TMPSIZE - 1);
  730. else
  731. snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  732. old_domain->domainname->name, domainname);
  733. goto force_jump_domain;
  734. }
  735. /*
  736. * No domain transition preference specified.
  737. * Calculate domain to transit to.
  738. */
  739. switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
  740. candidate)) {
  741. case TOMOYO_TRANSITION_CONTROL_RESET:
  742. force_reset_domain:
  743. /* Transit to the root of specified namespace. */
  744. snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>",
  745. candidate->name);
  746. /*
  747. * Make do_execve() fail if domain transition across namespaces
  748. * has failed.
  749. */
  750. reject_on_transition_failure = true;
  751. break;
  752. case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
  753. force_initialize_domain:
  754. /* Transit to the child of current namespace's root. */
  755. snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  756. old_domain->ns->name, candidate->name);
  757. break;
  758. case TOMOYO_TRANSITION_CONTROL_KEEP:
  759. force_keep_domain:
  760. /* Keep current domain. */
  761. domain = old_domain;
  762. break;
  763. default:
  764. if (old_domain == &tomoyo_kernel_domain &&
  765. !tomoyo_policy_loaded) {
  766. /*
  767. * Needn't to transit from kernel domain before
  768. * starting /sbin/init. But transit from kernel domain
  769. * if executing initializers because they might start
  770. * before /sbin/init.
  771. */
  772. domain = old_domain;
  773. break;
  774. }
  775. force_child_domain:
  776. /* Normal domain transition. */
  777. snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  778. old_domain->domainname->name, candidate->name);
  779. break;
  780. }
  781. force_jump_domain:
  782. if (!domain)
  783. domain = tomoyo_assign_domain(ee->tmp, true);
  784. if (domain)
  785. retval = 0;
  786. else if (reject_on_transition_failure) {
  787. printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n",
  788. ee->tmp);
  789. retval = -ENOMEM;
  790. } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
  791. retval = -ENOMEM;
  792. else {
  793. retval = 0;
  794. if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
  795. old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
  796. ee->r.granted = false;
  797. tomoyo_write_log(&ee->r, "%s", tomoyo_dif
  798. [TOMOYO_DIF_TRANSITION_FAILED]);
  799. printk(KERN_WARNING
  800. "ERROR: Domain '%s' not defined.\n", ee->tmp);
  801. }
  802. }
  803. out:
  804. if (!domain)
  805. domain = old_domain;
  806. /* Update reference count on "struct tomoyo_domain_info". */
  807. atomic_inc(&domain->users);
  808. bprm->cred->security = domain;
  809. kfree(exename.name);
  810. if (!retval) {
  811. ee->r.domain = domain;
  812. retval = tomoyo_environ(ee);
  813. }
  814. kfree(ee->tmp);
  815. kfree(ee->dump.data);
  816. kfree(ee);
  817. return retval;
  818. }
  819. /**
  820. * tomoyo_dump_page - Dump a page to buffer.
  821. *
  822. * @bprm: Pointer to "struct linux_binprm".
  823. * @pos: Location to dump.
  824. * @dump: Poiner to "struct tomoyo_page_dump".
  825. *
  826. * Returns true on success, false otherwise.
  827. */
  828. bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
  829. struct tomoyo_page_dump *dump)
  830. {
  831. struct page *page;
  832. /* dump->data is released by tomoyo_find_next_domain(). */
  833. if (!dump->data) {
  834. dump->data = kzalloc(PAGE_SIZE, GFP_NOFS);
  835. if (!dump->data)
  836. return false;
  837. }
  838. /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
  839. #ifdef CONFIG_MMU
  840. if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
  841. return false;
  842. #else
  843. page = bprm->page[pos / PAGE_SIZE];
  844. #endif
  845. if (page != dump->page) {
  846. const unsigned int offset = pos % PAGE_SIZE;
  847. /*
  848. * Maybe kmap()/kunmap() should be used here.
  849. * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
  850. * So do I.
  851. */
  852. char *kaddr = kmap_atomic(page, KM_USER0);
  853. dump->page = page;
  854. memcpy(dump->data + offset, kaddr + offset,
  855. PAGE_SIZE - offset);
  856. kunmap_atomic(kaddr, KM_USER0);
  857. }
  858. /* Same with put_arg_page(page) in fs/exec.c */
  859. #ifdef CONFIG_MMU
  860. put_page(page);
  861. #endif
  862. return true;
  863. }