domain.c 23 KB

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