domain.c 25 KB

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