domain.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * security/tomoyo/domain.c
  3. *
  4. * Domain transition functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/binfmts.h>
  10. #include <linux/slab.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. * @is_delete: True if it is a delete request.
  20. * @list: Pointer to "struct list_head".
  21. * @check_duplicate: Callback function to find duplicated entry.
  22. *
  23. * Returns 0 on success, negative value otherwise.
  24. *
  25. * Caller holds tomoyo_read_lock().
  26. */
  27. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  28. bool is_delete, struct list_head *list,
  29. bool (*check_duplicate) (const struct tomoyo_acl_head
  30. *,
  31. const struct tomoyo_acl_head
  32. *))
  33. {
  34. int error = is_delete ? -ENOENT : -ENOMEM;
  35. struct tomoyo_acl_head *entry;
  36. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  37. return -ENOMEM;
  38. list_for_each_entry_rcu(entry, list, list) {
  39. if (!check_duplicate(entry, new_entry))
  40. continue;
  41. entry->is_deleted = is_delete;
  42. error = 0;
  43. break;
  44. }
  45. if (error && !is_delete) {
  46. entry = tomoyo_commit_ok(new_entry, size);
  47. if (entry) {
  48. list_add_tail_rcu(&entry->list, list);
  49. error = 0;
  50. }
  51. }
  52. mutex_unlock(&tomoyo_policy_lock);
  53. return error;
  54. }
  55. /**
  56. * tomoyo_update_domain - Update an entry for domain policy.
  57. *
  58. * @new_entry: Pointer to "struct tomoyo_acl_info".
  59. * @size: Size of @new_entry in bytes.
  60. * @is_delete: True if it is a delete request.
  61. * @domain: Pointer to "struct tomoyo_domain_info".
  62. * @check_duplicate: Callback function to find duplicated entry.
  63. * @merge_duplicate: Callback function to merge duplicated entry.
  64. *
  65. * Returns 0 on success, negative value otherwise.
  66. *
  67. * Caller holds tomoyo_read_lock().
  68. */
  69. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  70. bool is_delete, struct tomoyo_domain_info *domain,
  71. bool (*check_duplicate) (const struct tomoyo_acl_info
  72. *,
  73. const struct tomoyo_acl_info
  74. *),
  75. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  76. struct tomoyo_acl_info *,
  77. const bool))
  78. {
  79. int error = is_delete ? -ENOENT : -ENOMEM;
  80. struct tomoyo_acl_info *entry;
  81. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  82. return error;
  83. list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
  84. if (!check_duplicate(entry, new_entry))
  85. continue;
  86. if (merge_duplicate)
  87. entry->is_deleted = merge_duplicate(entry, new_entry,
  88. is_delete);
  89. else
  90. entry->is_deleted = is_delete;
  91. error = 0;
  92. break;
  93. }
  94. if (error && !is_delete) {
  95. entry = tomoyo_commit_ok(new_entry, size);
  96. if (entry) {
  97. list_add_tail_rcu(&entry->list, &domain->acl_info_list);
  98. error = 0;
  99. }
  100. }
  101. mutex_unlock(&tomoyo_policy_lock);
  102. return error;
  103. }
  104. void tomoyo_check_acl(struct tomoyo_request_info *r,
  105. bool (*check_entry) (const struct tomoyo_request_info *,
  106. const struct tomoyo_acl_info *))
  107. {
  108. const struct tomoyo_domain_info *domain = r->domain;
  109. struct tomoyo_acl_info *ptr;
  110. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  111. if (ptr->is_deleted || ptr->type != r->param_type)
  112. continue;
  113. if (check_entry(r, ptr)) {
  114. r->granted = true;
  115. return;
  116. }
  117. }
  118. r->granted = false;
  119. }
  120. /* The list for "struct tomoyo_domain_info". */
  121. LIST_HEAD(tomoyo_domain_list);
  122. struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
  123. struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
  124. /**
  125. * tomoyo_get_last_name - Get last component of a domainname.
  126. *
  127. * @domain: Pointer to "struct tomoyo_domain_info".
  128. *
  129. * Returns the last component of the domainname.
  130. */
  131. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
  132. {
  133. const char *cp0 = domain->domainname->name;
  134. const char *cp1 = strrchr(cp0, ' ');
  135. if (cp1)
  136. return cp1 + 1;
  137. return cp0;
  138. }
  139. static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
  140. a,
  141. const struct tomoyo_acl_head *
  142. b)
  143. {
  144. const struct tomoyo_domain_initializer_entry *p1 =
  145. container_of(a, typeof(*p1), head);
  146. const struct tomoyo_domain_initializer_entry *p2 =
  147. container_of(b, typeof(*p2), head);
  148. return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
  149. && p1->domainname == p2->domainname
  150. && p1->program == p2->program;
  151. }
  152. /**
  153. * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
  154. *
  155. * @domainname: The name of domain. May be NULL.
  156. * @program: The name of program.
  157. * @is_not: True if it is "no_initialize_domain" entry.
  158. * @is_delete: True if it is a delete request.
  159. *
  160. * Returns 0 on success, negative value otherwise.
  161. *
  162. * Caller holds tomoyo_read_lock().
  163. */
  164. static int tomoyo_update_domain_initializer_entry(const char *domainname,
  165. const char *program,
  166. const bool is_not,
  167. const bool is_delete)
  168. {
  169. struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
  170. int error = is_delete ? -ENOENT : -ENOMEM;
  171. if (!tomoyo_correct_path(program))
  172. return -EINVAL;
  173. if (domainname) {
  174. if (!tomoyo_domain_def(domainname) &&
  175. tomoyo_correct_path(domainname))
  176. e.is_last_name = true;
  177. else if (!tomoyo_correct_domain(domainname))
  178. return -EINVAL;
  179. e.domainname = tomoyo_get_name(domainname);
  180. if (!e.domainname)
  181. goto out;
  182. }
  183. e.program = tomoyo_get_name(program);
  184. if (!e.program)
  185. goto out;
  186. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  187. &tomoyo_policy_list
  188. [TOMOYO_ID_DOMAIN_INITIALIZER],
  189. tomoyo_same_domain_initializer_entry);
  190. out:
  191. tomoyo_put_name(e.domainname);
  192. tomoyo_put_name(e.program);
  193. return error;
  194. }
  195. /**
  196. * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
  197. *
  198. * @head: Pointer to "struct tomoyo_io_buffer".
  199. *
  200. * Returns true on success, false otherwise.
  201. *
  202. * Caller holds tomoyo_read_lock().
  203. */
  204. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
  205. {
  206. struct list_head *pos;
  207. bool done = true;
  208. list_for_each_cookie(pos, head->read_var2, &tomoyo_policy_list
  209. [TOMOYO_ID_DOMAIN_INITIALIZER]) {
  210. const char *no;
  211. const char *from = "";
  212. const char *domain = "";
  213. struct tomoyo_domain_initializer_entry *ptr;
  214. ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
  215. head.list);
  216. if (ptr->head.is_deleted)
  217. continue;
  218. no = ptr->is_not ? "no_" : "";
  219. if (ptr->domainname) {
  220. from = " from ";
  221. domain = ptr->domainname->name;
  222. }
  223. done = tomoyo_io_printf(head,
  224. "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
  225. "%s%s%s\n", no, ptr->program->name,
  226. from, domain);
  227. if (!done)
  228. break;
  229. }
  230. return done;
  231. }
  232. /**
  233. * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
  234. *
  235. * @data: String to parse.
  236. * @is_not: True if it is "no_initialize_domain" entry.
  237. * @is_delete: True if it is a delete request.
  238. *
  239. * Returns 0 on success, negative value otherwise.
  240. *
  241. * Caller holds tomoyo_read_lock().
  242. */
  243. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  244. const bool is_delete)
  245. {
  246. char *cp = strstr(data, " from ");
  247. if (cp) {
  248. *cp = '\0';
  249. return tomoyo_update_domain_initializer_entry(cp + 6, data,
  250. is_not,
  251. is_delete);
  252. }
  253. return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
  254. is_delete);
  255. }
  256. /**
  257. * tomoyo_domain_initializer - Check whether the given program causes domainname reinitialization.
  258. *
  259. * @domainname: The name of domain.
  260. * @program: The name of program.
  261. * @last_name: The last component of @domainname.
  262. *
  263. * Returns true if executing @program reinitializes domain transition,
  264. * false otherwise.
  265. *
  266. * Caller holds tomoyo_read_lock().
  267. */
  268. static bool tomoyo_domain_initializer(const struct tomoyo_path_info *
  269. domainname,
  270. const struct tomoyo_path_info *program,
  271. const struct tomoyo_path_info *
  272. last_name)
  273. {
  274. struct tomoyo_domain_initializer_entry *ptr;
  275. bool flag = false;
  276. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  277. [TOMOYO_ID_DOMAIN_INITIALIZER], head.list) {
  278. if (ptr->head.is_deleted)
  279. continue;
  280. if (ptr->domainname) {
  281. if (!ptr->is_last_name) {
  282. if (ptr->domainname != domainname)
  283. continue;
  284. } else {
  285. if (tomoyo_pathcmp(ptr->domainname, last_name))
  286. continue;
  287. }
  288. }
  289. if (tomoyo_pathcmp(ptr->program, program))
  290. continue;
  291. if (ptr->is_not) {
  292. flag = false;
  293. break;
  294. }
  295. flag = true;
  296. }
  297. return flag;
  298. }
  299. static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
  300. const struct tomoyo_acl_head *b)
  301. {
  302. const struct tomoyo_domain_keeper_entry *p1 =
  303. container_of(a, typeof(*p1), head);
  304. const struct tomoyo_domain_keeper_entry *p2 =
  305. container_of(b, typeof(*p2), head);
  306. return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
  307. && p1->domainname == p2->domainname
  308. && p1->program == p2->program;
  309. }
  310. /**
  311. * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
  312. *
  313. * @domainname: The name of domain.
  314. * @program: The name of program. May be NULL.
  315. * @is_not: True if it is "no_keep_domain" entry.
  316. * @is_delete: True if it is a delete request.
  317. *
  318. * Returns 0 on success, negative value otherwise.
  319. *
  320. * Caller holds tomoyo_read_lock().
  321. */
  322. static int tomoyo_update_domain_keeper_entry(const char *domainname,
  323. const char *program,
  324. const bool is_not,
  325. const bool is_delete)
  326. {
  327. struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
  328. int error = is_delete ? -ENOENT : -ENOMEM;
  329. if (!tomoyo_domain_def(domainname) &&
  330. tomoyo_correct_path(domainname))
  331. e.is_last_name = true;
  332. else if (!tomoyo_correct_domain(domainname))
  333. return -EINVAL;
  334. if (program) {
  335. if (!tomoyo_correct_path(program))
  336. return -EINVAL;
  337. e.program = tomoyo_get_name(program);
  338. if (!e.program)
  339. goto out;
  340. }
  341. e.domainname = tomoyo_get_name(domainname);
  342. if (!e.domainname)
  343. goto out;
  344. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  345. &tomoyo_policy_list
  346. [TOMOYO_ID_DOMAIN_KEEPER],
  347. tomoyo_same_domain_keeper_entry);
  348. out:
  349. tomoyo_put_name(e.domainname);
  350. tomoyo_put_name(e.program);
  351. return error;
  352. }
  353. /**
  354. * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
  355. *
  356. * @data: String to parse.
  357. * @is_not: True if it is "no_keep_domain" entry.
  358. * @is_delete: True if it is a delete request.
  359. *
  360. * Caller holds tomoyo_read_lock().
  361. */
  362. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  363. const bool is_delete)
  364. {
  365. char *cp = strstr(data, " from ");
  366. if (cp) {
  367. *cp = '\0';
  368. return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
  369. is_delete);
  370. }
  371. return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
  372. }
  373. /**
  374. * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
  375. *
  376. * @head: Pointer to "struct tomoyo_io_buffer".
  377. *
  378. * Returns true on success, false otherwise.
  379. *
  380. * Caller holds tomoyo_read_lock().
  381. */
  382. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
  383. {
  384. struct list_head *pos;
  385. bool done = true;
  386. list_for_each_cookie(pos, head->read_var2,
  387. &tomoyo_policy_list[TOMOYO_ID_DOMAIN_KEEPER]) {
  388. struct tomoyo_domain_keeper_entry *ptr;
  389. const char *no;
  390. const char *from = "";
  391. const char *program = "";
  392. ptr = list_entry(pos, struct tomoyo_domain_keeper_entry,
  393. head.list);
  394. if (ptr->head.is_deleted)
  395. continue;
  396. no = ptr->is_not ? "no_" : "";
  397. if (ptr->program) {
  398. from = " from ";
  399. program = ptr->program->name;
  400. }
  401. done = tomoyo_io_printf(head,
  402. "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
  403. "%s%s%s\n", no, program, from,
  404. ptr->domainname->name);
  405. if (!done)
  406. break;
  407. }
  408. return done;
  409. }
  410. /**
  411. * tomoyo_domain_keeper - Check whether the given program causes domain transition suppression.
  412. *
  413. * @domainname: The name of domain.
  414. * @program: The name of program.
  415. * @last_name: The last component of @domainname.
  416. *
  417. * Returns true if executing @program supresses domain transition,
  418. * false otherwise.
  419. *
  420. * Caller holds tomoyo_read_lock().
  421. */
  422. static bool tomoyo_domain_keeper(const struct tomoyo_path_info *domainname,
  423. const struct tomoyo_path_info *program,
  424. const struct tomoyo_path_info *last_name)
  425. {
  426. struct tomoyo_domain_keeper_entry *ptr;
  427. bool flag = false;
  428. list_for_each_entry_rcu(ptr,
  429. &tomoyo_policy_list[TOMOYO_ID_DOMAIN_KEEPER],
  430. head.list) {
  431. if (ptr->head.is_deleted)
  432. continue;
  433. if (!ptr->is_last_name) {
  434. if (ptr->domainname != domainname)
  435. continue;
  436. } else {
  437. if (tomoyo_pathcmp(ptr->domainname, last_name))
  438. continue;
  439. }
  440. if (ptr->program && tomoyo_pathcmp(ptr->program, program))
  441. continue;
  442. if (ptr->is_not) {
  443. flag = false;
  444. break;
  445. }
  446. flag = true;
  447. }
  448. return flag;
  449. }
  450. static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
  451. const struct tomoyo_acl_head *b)
  452. {
  453. const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
  454. head);
  455. const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
  456. head);
  457. return p1->original_name == p2->original_name &&
  458. p1->aggregated_name == p2->aggregated_name;
  459. }
  460. /**
  461. * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
  462. *
  463. * @original_name: The original program's name.
  464. * @aggregated_name: The program name to use.
  465. * @is_delete: True if it is a delete request.
  466. *
  467. * Returns 0 on success, negative value otherwise.
  468. *
  469. * Caller holds tomoyo_read_lock().
  470. */
  471. static int tomoyo_update_aggregator_entry(const char *original_name,
  472. const char *aggregated_name,
  473. const bool is_delete)
  474. {
  475. struct tomoyo_aggregator_entry e = { };
  476. int error = is_delete ? -ENOENT : -ENOMEM;
  477. if (!tomoyo_correct_path(original_name) ||
  478. !tomoyo_correct_path(aggregated_name))
  479. return -EINVAL;
  480. e.original_name = tomoyo_get_name(original_name);
  481. e.aggregated_name = tomoyo_get_name(aggregated_name);
  482. if (!e.original_name || !e.aggregated_name ||
  483. e.aggregated_name->is_patterned) /* No patterns allowed. */
  484. goto out;
  485. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  486. &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
  487. tomoyo_same_aggregator_entry);
  488. out:
  489. tomoyo_put_name(e.original_name);
  490. tomoyo_put_name(e.aggregated_name);
  491. return error;
  492. }
  493. /**
  494. * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
  495. *
  496. * @head: Pointer to "struct tomoyo_io_buffer".
  497. *
  498. * Returns true on success, false otherwise.
  499. *
  500. * Caller holds tomoyo_read_lock().
  501. */
  502. bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head)
  503. {
  504. struct list_head *pos;
  505. bool done = true;
  506. list_for_each_cookie(pos, head->read_var2,
  507. &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR]) {
  508. struct tomoyo_aggregator_entry *ptr;
  509. ptr = list_entry(pos, struct tomoyo_aggregator_entry,
  510. head.list);
  511. if (ptr->head.is_deleted)
  512. continue;
  513. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR
  514. "%s %s\n", ptr->original_name->name,
  515. ptr->aggregated_name->name);
  516. if (!done)
  517. break;
  518. }
  519. return done;
  520. }
  521. /**
  522. * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
  523. *
  524. * @data: String to parse.
  525. * @is_delete: True if it is a delete request.
  526. *
  527. * Returns 0 on success, negative value otherwise.
  528. *
  529. * Caller holds tomoyo_read_lock().
  530. */
  531. int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
  532. {
  533. char *cp = strchr(data, ' ');
  534. if (!cp)
  535. return -EINVAL;
  536. *cp++ = '\0';
  537. return tomoyo_update_aggregator_entry(data, cp, is_delete);
  538. }
  539. static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
  540. const struct tomoyo_acl_head *b)
  541. {
  542. const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
  543. head);
  544. const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
  545. head);
  546. return p1->original_name == p2->original_name &&
  547. p1->aliased_name == p2->aliased_name;
  548. }
  549. /**
  550. * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
  551. *
  552. * @original_name: The original program's real name.
  553. * @aliased_name: The symbolic program's symbolic link's name.
  554. * @is_delete: True if it is a delete request.
  555. *
  556. * Returns 0 on success, negative value otherwise.
  557. *
  558. * Caller holds tomoyo_read_lock().
  559. */
  560. static int tomoyo_update_alias_entry(const char *original_name,
  561. const char *aliased_name,
  562. const bool is_delete)
  563. {
  564. struct tomoyo_alias_entry e = { };
  565. int error = is_delete ? -ENOENT : -ENOMEM;
  566. if (!tomoyo_correct_path(original_name) ||
  567. !tomoyo_correct_path(aliased_name))
  568. return -EINVAL;
  569. e.original_name = tomoyo_get_name(original_name);
  570. e.aliased_name = tomoyo_get_name(aliased_name);
  571. if (!e.original_name || !e.aliased_name ||
  572. e.original_name->is_patterned || e.aliased_name->is_patterned)
  573. goto out; /* No patterns allowed. */
  574. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  575. &tomoyo_policy_list[TOMOYO_ID_ALIAS],
  576. tomoyo_same_alias_entry);
  577. out:
  578. tomoyo_put_name(e.original_name);
  579. tomoyo_put_name(e.aliased_name);
  580. return error;
  581. }
  582. /**
  583. * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
  584. *
  585. * @head: Pointer to "struct tomoyo_io_buffer".
  586. *
  587. * Returns true on success, false otherwise.
  588. *
  589. * Caller holds tomoyo_read_lock().
  590. */
  591. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
  592. {
  593. struct list_head *pos;
  594. bool done = true;
  595. list_for_each_cookie(pos, head->read_var2,
  596. &tomoyo_policy_list[TOMOYO_ID_ALIAS]) {
  597. struct tomoyo_alias_entry *ptr;
  598. ptr = list_entry(pos, struct tomoyo_alias_entry, head.list);
  599. if (ptr->head.is_deleted)
  600. continue;
  601. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
  602. ptr->original_name->name,
  603. ptr->aliased_name->name);
  604. if (!done)
  605. break;
  606. }
  607. return done;
  608. }
  609. /**
  610. * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
  611. *
  612. * @data: String to parse.
  613. * @is_delete: True if it is a delete request.
  614. *
  615. * Returns 0 on success, negative value otherwise.
  616. *
  617. * Caller holds tomoyo_read_lock().
  618. */
  619. int tomoyo_write_alias_policy(char *data, const bool is_delete)
  620. {
  621. char *cp = strchr(data, ' ');
  622. if (!cp)
  623. return -EINVAL;
  624. *cp++ = '\0';
  625. return tomoyo_update_alias_entry(data, cp, is_delete);
  626. }
  627. /**
  628. * tomoyo_find_or_assign_new_domain - Create a domain.
  629. *
  630. * @domainname: The name of domain.
  631. * @profile: Profile number to assign if the domain was newly created.
  632. *
  633. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  634. *
  635. * Caller holds tomoyo_read_lock().
  636. */
  637. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  638. domainname,
  639. const u8 profile)
  640. {
  641. struct tomoyo_domain_info *entry;
  642. struct tomoyo_domain_info *domain = NULL;
  643. const struct tomoyo_path_info *saved_domainname;
  644. bool found = false;
  645. if (!tomoyo_correct_domain(domainname))
  646. return NULL;
  647. saved_domainname = tomoyo_get_name(domainname);
  648. if (!saved_domainname)
  649. return NULL;
  650. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  651. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  652. goto out;
  653. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  654. if (domain->is_deleted ||
  655. tomoyo_pathcmp(saved_domainname, domain->domainname))
  656. continue;
  657. found = true;
  658. break;
  659. }
  660. if (!found && tomoyo_memory_ok(entry)) {
  661. INIT_LIST_HEAD(&entry->acl_info_list);
  662. entry->domainname = saved_domainname;
  663. saved_domainname = NULL;
  664. entry->profile = profile;
  665. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  666. domain = entry;
  667. entry = NULL;
  668. found = true;
  669. }
  670. mutex_unlock(&tomoyo_policy_lock);
  671. out:
  672. tomoyo_put_name(saved_domainname);
  673. kfree(entry);
  674. return found ? domain : NULL;
  675. }
  676. /**
  677. * tomoyo_find_next_domain - Find a domain.
  678. *
  679. * @bprm: Pointer to "struct linux_binprm".
  680. *
  681. * Returns 0 on success, negative value otherwise.
  682. *
  683. * Caller holds tomoyo_read_lock().
  684. */
  685. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  686. {
  687. struct tomoyo_request_info r;
  688. char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  689. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  690. struct tomoyo_domain_info *domain = NULL;
  691. const char *old_domain_name = old_domain->domainname->name;
  692. const char *original_name = bprm->filename;
  693. u8 mode;
  694. bool is_enforce;
  695. int retval = -ENOMEM;
  696. bool need_kfree = false;
  697. struct tomoyo_path_info rn = { }; /* real name */
  698. struct tomoyo_path_info sn = { }; /* symlink name */
  699. struct tomoyo_path_info ln; /* last name */
  700. ln.name = tomoyo_get_last_name(old_domain);
  701. tomoyo_fill_path_info(&ln);
  702. mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  703. is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
  704. if (!tmp)
  705. goto out;
  706. retry:
  707. if (need_kfree) {
  708. kfree(rn.name);
  709. need_kfree = false;
  710. }
  711. /* Get tomoyo_realpath of program. */
  712. retval = -ENOENT;
  713. rn.name = tomoyo_realpath(original_name);
  714. if (!rn.name)
  715. goto out;
  716. tomoyo_fill_path_info(&rn);
  717. need_kfree = true;
  718. /* Get tomoyo_realpath of symbolic link. */
  719. sn.name = tomoyo_realpath_nofollow(original_name);
  720. if (!sn.name)
  721. goto out;
  722. tomoyo_fill_path_info(&sn);
  723. /* Check 'alias' directive. */
  724. if (tomoyo_pathcmp(&rn, &sn)) {
  725. struct tomoyo_alias_entry *ptr;
  726. /* Is this program allowed to be called via symbolic links? */
  727. list_for_each_entry_rcu(ptr,
  728. &tomoyo_policy_list[TOMOYO_ID_ALIAS],
  729. head.list) {
  730. if (ptr->head.is_deleted ||
  731. tomoyo_pathcmp(&rn, ptr->original_name) ||
  732. tomoyo_pathcmp(&sn, ptr->aliased_name))
  733. continue;
  734. kfree(rn.name);
  735. need_kfree = false;
  736. /* This is OK because it is read only. */
  737. rn = *ptr->aliased_name;
  738. break;
  739. }
  740. }
  741. /* Check 'aggregator' directive. */
  742. {
  743. struct tomoyo_aggregator_entry *ptr;
  744. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  745. [TOMOYO_ID_AGGREGATOR], head.list) {
  746. if (ptr->head.is_deleted ||
  747. !tomoyo_path_matches_pattern(&rn,
  748. ptr->original_name))
  749. continue;
  750. if (need_kfree)
  751. kfree(rn.name);
  752. need_kfree = false;
  753. /* This is OK because it is read only. */
  754. rn = *ptr->aggregated_name;
  755. break;
  756. }
  757. }
  758. /* Check execute permission. */
  759. retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
  760. if (retval == TOMOYO_RETRY_REQUEST)
  761. goto retry;
  762. if (retval < 0)
  763. goto out;
  764. if (tomoyo_domain_initializer(old_domain->domainname, &rn, &ln)) {
  765. /* Transit to the child of tomoyo_kernel_domain domain. */
  766. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
  767. TOMOYO_ROOT_NAME " " "%s", rn.name);
  768. } else if (old_domain == &tomoyo_kernel_domain &&
  769. !tomoyo_policy_loaded) {
  770. /*
  771. * Needn't to transit from kernel domain before starting
  772. * /sbin/init. But transit from kernel domain if executing
  773. * initializers because they might start before /sbin/init.
  774. */
  775. domain = old_domain;
  776. } else if (tomoyo_domain_keeper(old_domain->domainname, &rn, &ln)) {
  777. /* Keep current domain. */
  778. domain = old_domain;
  779. } else {
  780. /* Normal domain transition. */
  781. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
  782. "%s %s", old_domain_name, rn.name);
  783. }
  784. if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
  785. goto done;
  786. domain = tomoyo_find_domain(tmp);
  787. if (domain)
  788. goto done;
  789. if (is_enforce) {
  790. int error = tomoyo_supervisor(&r, "# wants to create domain\n"
  791. "%s\n", tmp);
  792. if (error == TOMOYO_RETRY_REQUEST)
  793. goto retry;
  794. if (error < 0)
  795. goto done;
  796. }
  797. domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
  798. done:
  799. if (domain)
  800. goto out;
  801. printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
  802. if (is_enforce)
  803. retval = -EPERM;
  804. else
  805. old_domain->transition_failed = true;
  806. out:
  807. if (!domain)
  808. domain = old_domain;
  809. /* Update reference count on "struct tomoyo_domain_info". */
  810. atomic_inc(&domain->users);
  811. bprm->cred->security = domain;
  812. if (need_kfree)
  813. kfree(rn.name);
  814. kfree(sn.name);
  815. kfree(tmp);
  816. return retval;
  817. }