domain.c 26 KB

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