domain.c 25 KB

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