domain.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * security/tomoyo/domain.c
  3. *
  4. * Implementation of the Domain-Based Mandatory Access Control.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0 2009/04/01
  9. *
  10. */
  11. #include "common.h"
  12. #include <linux/binfmts.h>
  13. #include <linux/slab.h>
  14. /* Variables definitions.*/
  15. /* The initial domain. */
  16. struct tomoyo_domain_info tomoyo_kernel_domain;
  17. /*
  18. * tomoyo_domain_list is used for holding list of domains.
  19. * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
  20. * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  21. *
  22. * An entry is added by
  23. *
  24. * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \
  25. * /sys/kernel/security/tomoyo/domain_policy
  26. *
  27. * and is deleted by
  28. *
  29. * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \
  30. * /sys/kernel/security/tomoyo/domain_policy
  31. *
  32. * and all entries are retrieved by
  33. *
  34. * # cat /sys/kernel/security/tomoyo/domain_policy
  35. *
  36. * A domain is added by
  37. *
  38. * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy
  39. *
  40. * and is deleted by
  41. *
  42. * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy
  43. *
  44. * and all domains are retrieved by
  45. *
  46. * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy
  47. *
  48. * Normally, a domainname is monotonically getting longer because a domainname
  49. * which the process will belong to if an execve() operation succeeds is
  50. * defined as a concatenation of "current domainname" + "pathname passed to
  51. * execve()".
  52. * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for
  53. * exceptions.
  54. */
  55. LIST_HEAD(tomoyo_domain_list);
  56. /**
  57. * tomoyo_get_last_name - Get last component of a domainname.
  58. *
  59. * @domain: Pointer to "struct tomoyo_domain_info".
  60. *
  61. * Returns the last component of the domainname.
  62. */
  63. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
  64. {
  65. const char *cp0 = domain->domainname->name;
  66. const char *cp1 = strrchr(cp0, ' ');
  67. if (cp1)
  68. return cp1 + 1;
  69. return cp0;
  70. }
  71. /*
  72. * tomoyo_domain_initializer_list is used for holding list of programs which
  73. * triggers reinitialization of domainname. Normally, a domainname is
  74. * monotonically getting longer. But sometimes, we restart daemon programs.
  75. * It would be convenient for us that "a daemon started upon system boot" and
  76. * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO
  77. * provides a way to shorten domainnames.
  78. *
  79. * An entry is added by
  80. *
  81. * # echo 'initialize_domain /usr/sbin/httpd' > \
  82. * /sys/kernel/security/tomoyo/exception_policy
  83. *
  84. * and is deleted by
  85. *
  86. * # echo 'delete initialize_domain /usr/sbin/httpd' > \
  87. * /sys/kernel/security/tomoyo/exception_policy
  88. *
  89. * and all entries are retrieved by
  90. *
  91. * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy
  92. *
  93. * In the example above, /usr/sbin/httpd will belong to
  94. * "<kernel> /usr/sbin/httpd" domain.
  95. *
  96. * You may specify a domainname using "from" keyword.
  97. * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
  98. * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd"
  99. * domain to belong to "<kernel> /usr/sbin/httpd" domain.
  100. *
  101. * You may add "no_" prefix to "initialize_domain".
  102. * "initialize_domain /usr/sbin/httpd" and
  103. * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
  104. * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain
  105. * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain.
  106. */
  107. LIST_HEAD(tomoyo_domain_initializer_list);
  108. /**
  109. * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
  110. *
  111. * @domainname: The name of domain. May be NULL.
  112. * @program: The name of program.
  113. * @is_not: True if it is "no_initialize_domain" entry.
  114. * @is_delete: True if it is a delete request.
  115. *
  116. * Returns 0 on success, negative value otherwise.
  117. *
  118. * Caller holds tomoyo_read_lock().
  119. */
  120. static int tomoyo_update_domain_initializer_entry(const char *domainname,
  121. const char *program,
  122. const bool is_not,
  123. const bool is_delete)
  124. {
  125. struct tomoyo_domain_initializer_entry *ptr;
  126. struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
  127. int error = is_delete ? -ENOENT : -ENOMEM;
  128. if (!tomoyo_is_correct_path(program, 1, -1, -1))
  129. return -EINVAL; /* No patterns allowed. */
  130. if (domainname) {
  131. if (!tomoyo_is_domain_def(domainname) &&
  132. tomoyo_is_correct_path(domainname, 1, -1, -1))
  133. e.is_last_name = true;
  134. else if (!tomoyo_is_correct_domain(domainname))
  135. return -EINVAL;
  136. e.domainname = tomoyo_get_name(domainname);
  137. if (!e.domainname)
  138. goto out;
  139. }
  140. e.program = tomoyo_get_name(program);
  141. if (!e.program)
  142. goto out;
  143. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  144. goto out;
  145. list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
  146. if (!tomoyo_is_same_domain_initializer_entry(ptr, &e))
  147. continue;
  148. ptr->is_deleted = is_delete;
  149. error = 0;
  150. break;
  151. }
  152. if (!is_delete && error) {
  153. struct tomoyo_domain_initializer_entry *entry =
  154. tomoyo_commit_ok(&e, sizeof(e));
  155. if (entry) {
  156. list_add_tail_rcu(&entry->list,
  157. &tomoyo_domain_initializer_list);
  158. error = 0;
  159. }
  160. }
  161. mutex_unlock(&tomoyo_policy_lock);
  162. out:
  163. tomoyo_put_name(e.domainname);
  164. tomoyo_put_name(e.program);
  165. return error;
  166. }
  167. /**
  168. * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
  169. *
  170. * @head: Pointer to "struct tomoyo_io_buffer".
  171. *
  172. * Returns true on success, false otherwise.
  173. *
  174. * Caller holds tomoyo_read_lock().
  175. */
  176. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
  177. {
  178. struct list_head *pos;
  179. bool done = true;
  180. list_for_each_cookie(pos, head->read_var2,
  181. &tomoyo_domain_initializer_list) {
  182. const char *no;
  183. const char *from = "";
  184. const char *domain = "";
  185. struct tomoyo_domain_initializer_entry *ptr;
  186. ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
  187. list);
  188. if (ptr->is_deleted)
  189. continue;
  190. no = ptr->is_not ? "no_" : "";
  191. if (ptr->domainname) {
  192. from = " from ";
  193. domain = ptr->domainname->name;
  194. }
  195. done = tomoyo_io_printf(head,
  196. "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
  197. "%s%s%s\n", no, ptr->program->name,
  198. from, domain);
  199. if (!done)
  200. break;
  201. }
  202. return done;
  203. }
  204. /**
  205. * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
  206. *
  207. * @data: String to parse.
  208. * @is_not: True if it is "no_initialize_domain" entry.
  209. * @is_delete: True if it is a delete request.
  210. *
  211. * Returns 0 on success, negative value otherwise.
  212. *
  213. * Caller holds tomoyo_read_lock().
  214. */
  215. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  216. const bool is_delete)
  217. {
  218. char *cp = strstr(data, " from ");
  219. if (cp) {
  220. *cp = '\0';
  221. return tomoyo_update_domain_initializer_entry(cp + 6, data,
  222. is_not,
  223. is_delete);
  224. }
  225. return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
  226. is_delete);
  227. }
  228. /**
  229. * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
  230. *
  231. * @domainname: The name of domain.
  232. * @program: The name of program.
  233. * @last_name: The last component of @domainname.
  234. *
  235. * Returns true if executing @program reinitializes domain transition,
  236. * false otherwise.
  237. *
  238. * Caller holds tomoyo_read_lock().
  239. */
  240. static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
  241. domainname,
  242. const struct tomoyo_path_info *program,
  243. const struct tomoyo_path_info *
  244. last_name)
  245. {
  246. struct tomoyo_domain_initializer_entry *ptr;
  247. bool flag = false;
  248. list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
  249. if (ptr->is_deleted)
  250. continue;
  251. if (ptr->domainname) {
  252. if (!ptr->is_last_name) {
  253. if (ptr->domainname != domainname)
  254. continue;
  255. } else {
  256. if (tomoyo_pathcmp(ptr->domainname, last_name))
  257. continue;
  258. }
  259. }
  260. if (tomoyo_pathcmp(ptr->program, program))
  261. continue;
  262. if (ptr->is_not) {
  263. flag = false;
  264. break;
  265. }
  266. flag = true;
  267. }
  268. return flag;
  269. }
  270. /*
  271. * tomoyo_domain_keeper_list is used for holding list of domainnames which
  272. * suppresses domain transition. Normally, a domainname is monotonically
  273. * getting longer. But sometimes, we want to suppress domain transition.
  274. * It would be convenient for us that programs executed from a login session
  275. * belong to the same domain. Thus, TOMOYO provides a way to suppress domain
  276. * transition.
  277. *
  278. * An entry is added by
  279. *
  280. * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
  281. * /sys/kernel/security/tomoyo/exception_policy
  282. *
  283. * and is deleted by
  284. *
  285. * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
  286. * /sys/kernel/security/tomoyo/exception_policy
  287. *
  288. * and all entries are retrieved by
  289. *
  290. * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy
  291. *
  292. * In the example above, any process which belongs to
  293. * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain,
  294. * unless explicitly specified by "initialize_domain" or "no_keep_domain".
  295. *
  296. * You may specify a program using "from" keyword.
  297. * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash"
  298. * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash"
  299. * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain.
  300. *
  301. * You may add "no_" prefix to "keep_domain".
  302. * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and
  303. * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will
  304. * cause "/usr/bin/passwd" to belong to
  305. * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless
  306. * explicitly specified by "initialize_domain".
  307. */
  308. LIST_HEAD(tomoyo_domain_keeper_list);
  309. /**
  310. * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
  311. *
  312. * @domainname: The name of domain.
  313. * @program: The name of program. May be NULL.
  314. * @is_not: True if it is "no_keep_domain" entry.
  315. * @is_delete: True if it is a delete request.
  316. *
  317. * Returns 0 on success, negative value otherwise.
  318. *
  319. * Caller holds tomoyo_read_lock().
  320. */
  321. static int tomoyo_update_domain_keeper_entry(const char *domainname,
  322. const char *program,
  323. const bool is_not,
  324. const bool is_delete)
  325. {
  326. struct tomoyo_domain_keeper_entry *ptr;
  327. struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
  328. int error = is_delete ? -ENOENT : -ENOMEM;
  329. if (!tomoyo_is_domain_def(domainname) &&
  330. tomoyo_is_correct_path(domainname, 1, -1, -1))
  331. e.is_last_name = true;
  332. else if (!tomoyo_is_correct_domain(domainname))
  333. return -EINVAL;
  334. if (program) {
  335. if (!tomoyo_is_correct_path(program, 1, -1, -1))
  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. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  345. goto out;
  346. list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
  347. if (!tomoyo_is_same_domain_keeper_entry(ptr, &e))
  348. continue;
  349. ptr->is_deleted = is_delete;
  350. error = 0;
  351. break;
  352. }
  353. if (!is_delete && error) {
  354. struct tomoyo_domain_keeper_entry *entry =
  355. tomoyo_commit_ok(&e, sizeof(e));
  356. if (entry) {
  357. list_add_tail_rcu(&entry->list,
  358. &tomoyo_domain_keeper_list);
  359. error = 0;
  360. }
  361. }
  362. mutex_unlock(&tomoyo_policy_lock);
  363. out:
  364. tomoyo_put_name(e.domainname);
  365. tomoyo_put_name(e.program);
  366. return error;
  367. }
  368. /**
  369. * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
  370. *
  371. * @data: String to parse.
  372. * @is_not: True if it is "no_keep_domain" entry.
  373. * @is_delete: True if it is a delete request.
  374. *
  375. * Caller holds tomoyo_read_lock().
  376. */
  377. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  378. const bool is_delete)
  379. {
  380. char *cp = strstr(data, " from ");
  381. if (cp) {
  382. *cp = '\0';
  383. return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
  384. is_delete);
  385. }
  386. return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
  387. }
  388. /**
  389. * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
  390. *
  391. * @head: Pointer to "struct tomoyo_io_buffer".
  392. *
  393. * Returns true on success, false otherwise.
  394. *
  395. * Caller holds tomoyo_read_lock().
  396. */
  397. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
  398. {
  399. struct list_head *pos;
  400. bool done = true;
  401. list_for_each_cookie(pos, head->read_var2,
  402. &tomoyo_domain_keeper_list) {
  403. struct tomoyo_domain_keeper_entry *ptr;
  404. const char *no;
  405. const char *from = "";
  406. const char *program = "";
  407. ptr = list_entry(pos, struct tomoyo_domain_keeper_entry, list);
  408. if (ptr->is_deleted)
  409. continue;
  410. no = ptr->is_not ? "no_" : "";
  411. if (ptr->program) {
  412. from = " from ";
  413. program = ptr->program->name;
  414. }
  415. done = tomoyo_io_printf(head,
  416. "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
  417. "%s%s%s\n", no, program, from,
  418. ptr->domainname->name);
  419. if (!done)
  420. break;
  421. }
  422. return done;
  423. }
  424. /**
  425. * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
  426. *
  427. * @domainname: The name of domain.
  428. * @program: The name of program.
  429. * @last_name: The last component of @domainname.
  430. *
  431. * Returns true if executing @program supresses domain transition,
  432. * false otherwise.
  433. *
  434. * Caller holds tomoyo_read_lock().
  435. */
  436. static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
  437. const struct tomoyo_path_info *program,
  438. const struct tomoyo_path_info *last_name)
  439. {
  440. struct tomoyo_domain_keeper_entry *ptr;
  441. bool flag = false;
  442. list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
  443. if (ptr->is_deleted)
  444. continue;
  445. if (!ptr->is_last_name) {
  446. if (ptr->domainname != domainname)
  447. continue;
  448. } else {
  449. if (tomoyo_pathcmp(ptr->domainname, last_name))
  450. continue;
  451. }
  452. if (ptr->program && tomoyo_pathcmp(ptr->program, program))
  453. continue;
  454. if (ptr->is_not) {
  455. flag = false;
  456. break;
  457. }
  458. flag = true;
  459. }
  460. return flag;
  461. }
  462. /*
  463. * tomoyo_alias_list is used for holding list of symlink's pathnames which are
  464. * allowed to be passed to an execve() request. Normally, the domainname which
  465. * the current process will belong to after execve() succeeds is calculated
  466. * using dereferenced pathnames. But some programs behave differently depending
  467. * on the name passed to argv[0]. For busybox, calculating domainname using
  468. * dereferenced pathnames will cause all programs in the busybox to belong to
  469. * the same domain. Thus, TOMOYO provides a way to allow use of symlink's
  470. * pathname for checking execve()'s permission and calculating domainname which
  471. * the current process will belong to after execve() succeeds.
  472. *
  473. * An entry is added by
  474. *
  475. * # echo 'alias /bin/busybox /bin/cat' > \
  476. * /sys/kernel/security/tomoyo/exception_policy
  477. *
  478. * and is deleted by
  479. *
  480. * # echo 'delete alias /bin/busybox /bin/cat' > \
  481. * /sys/kernel/security/tomoyo/exception_policy
  482. *
  483. * and all entries are retrieved by
  484. *
  485. * # grep ^alias /sys/kernel/security/tomoyo/exception_policy
  486. *
  487. * In the example above, if /bin/cat is a symlink to /bin/busybox and execution
  488. * of /bin/cat is requested, permission is checked for /bin/cat rather than
  489. * /bin/busybox and domainname which the current process will belong to after
  490. * execve() succeeds is calculated using /bin/cat rather than /bin/busybox .
  491. */
  492. LIST_HEAD(tomoyo_alias_list);
  493. /**
  494. * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
  495. *
  496. * @original_name: The original program's real name.
  497. * @aliased_name: The symbolic program's symbolic link's name.
  498. * @is_delete: True if it is a delete request.
  499. *
  500. * Returns 0 on success, negative value otherwise.
  501. *
  502. * Caller holds tomoyo_read_lock().
  503. */
  504. static int tomoyo_update_alias_entry(const char *original_name,
  505. const char *aliased_name,
  506. const bool is_delete)
  507. {
  508. struct tomoyo_alias_entry *ptr;
  509. struct tomoyo_alias_entry e = { };
  510. int error = is_delete ? -ENOENT : -ENOMEM;
  511. if (!tomoyo_is_correct_path(original_name, 1, -1, -1) ||
  512. !tomoyo_is_correct_path(aliased_name, 1, -1, -1))
  513. return -EINVAL; /* No patterns allowed. */
  514. e.original_name = tomoyo_get_name(original_name);
  515. e.aliased_name = tomoyo_get_name(aliased_name);
  516. if (!e.original_name || !e.aliased_name)
  517. goto out;
  518. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  519. goto out;
  520. list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
  521. if (!tomoyo_is_same_alias_entry(ptr, &e))
  522. continue;
  523. ptr->is_deleted = is_delete;
  524. error = 0;
  525. break;
  526. }
  527. if (!is_delete && error) {
  528. struct tomoyo_alias_entry *entry =
  529. tomoyo_commit_ok(&e, sizeof(e));
  530. if (entry) {
  531. list_add_tail_rcu(&entry->list, &tomoyo_alias_list);
  532. error = 0;
  533. }
  534. }
  535. mutex_unlock(&tomoyo_policy_lock);
  536. out:
  537. tomoyo_put_name(e.original_name);
  538. tomoyo_put_name(e.aliased_name);
  539. return error;
  540. }
  541. /**
  542. * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
  543. *
  544. * @head: Pointer to "struct tomoyo_io_buffer".
  545. *
  546. * Returns true on success, false otherwise.
  547. *
  548. * Caller holds tomoyo_read_lock().
  549. */
  550. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
  551. {
  552. struct list_head *pos;
  553. bool done = true;
  554. list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
  555. struct tomoyo_alias_entry *ptr;
  556. ptr = list_entry(pos, struct tomoyo_alias_entry, list);
  557. if (ptr->is_deleted)
  558. continue;
  559. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
  560. ptr->original_name->name,
  561. ptr->aliased_name->name);
  562. if (!done)
  563. break;
  564. }
  565. return done;
  566. }
  567. /**
  568. * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
  569. *
  570. * @data: String to parse.
  571. * @is_delete: True if it is a delete request.
  572. *
  573. * Returns 0 on success, negative value otherwise.
  574. *
  575. * Caller holds tomoyo_read_lock().
  576. */
  577. int tomoyo_write_alias_policy(char *data, const bool is_delete)
  578. {
  579. char *cp = strchr(data, ' ');
  580. if (!cp)
  581. return -EINVAL;
  582. *cp++ = '\0';
  583. return tomoyo_update_alias_entry(data, cp, is_delete);
  584. }
  585. /**
  586. * tomoyo_find_or_assign_new_domain - Create a domain.
  587. *
  588. * @domainname: The name of domain.
  589. * @profile: Profile number to assign if the domain was newly created.
  590. *
  591. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  592. *
  593. * Caller holds tomoyo_read_lock().
  594. */
  595. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  596. domainname,
  597. const u8 profile)
  598. {
  599. struct tomoyo_domain_info *entry;
  600. struct tomoyo_domain_info *domain = NULL;
  601. const struct tomoyo_path_info *saved_domainname;
  602. bool found = false;
  603. if (!tomoyo_is_correct_domain(domainname))
  604. return NULL;
  605. saved_domainname = tomoyo_get_name(domainname);
  606. if (!saved_domainname)
  607. return NULL;
  608. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  609. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  610. goto out;
  611. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  612. if (domain->is_deleted ||
  613. tomoyo_pathcmp(saved_domainname, domain->domainname))
  614. continue;
  615. found = true;
  616. break;
  617. }
  618. if (!found && tomoyo_memory_ok(entry)) {
  619. INIT_LIST_HEAD(&entry->acl_info_list);
  620. entry->domainname = saved_domainname;
  621. saved_domainname = NULL;
  622. entry->profile = profile;
  623. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  624. domain = entry;
  625. entry = NULL;
  626. found = true;
  627. }
  628. mutex_unlock(&tomoyo_policy_lock);
  629. out:
  630. tomoyo_put_name(saved_domainname);
  631. kfree(entry);
  632. return found ? domain : NULL;
  633. }
  634. /**
  635. * tomoyo_find_next_domain - Find a domain.
  636. *
  637. * @bprm: Pointer to "struct linux_binprm".
  638. *
  639. * Returns 0 on success, negative value otherwise.
  640. *
  641. * Caller holds tomoyo_read_lock().
  642. */
  643. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  644. {
  645. /*
  646. * This function assumes that the size of buffer returned by
  647. * tomoyo_realpath() = TOMOYO_MAX_PATHNAME_LEN.
  648. */
  649. struct tomoyo_page_buffer *tmp = kzalloc(sizeof(*tmp), GFP_NOFS);
  650. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  651. struct tomoyo_domain_info *domain = NULL;
  652. const char *old_domain_name = old_domain->domainname->name;
  653. const char *original_name = bprm->filename;
  654. char *new_domain_name = NULL;
  655. char *real_program_name = NULL;
  656. char *symlink_program_name = NULL;
  657. const u8 mode = tomoyo_check_flags(old_domain, TOMOYO_MAC_FOR_FILE);
  658. const bool is_enforce = (mode == 3);
  659. int retval = -ENOMEM;
  660. struct tomoyo_path_info r; /* real name */
  661. struct tomoyo_path_info s; /* symlink name */
  662. struct tomoyo_path_info l; /* last name */
  663. static bool initialized;
  664. if (!tmp)
  665. goto out;
  666. if (!initialized) {
  667. /*
  668. * Built-in initializers. This is needed because policies are
  669. * not loaded until starting /sbin/init.
  670. */
  671. tomoyo_update_domain_initializer_entry(NULL, "/sbin/hotplug",
  672. false, false);
  673. tomoyo_update_domain_initializer_entry(NULL, "/sbin/modprobe",
  674. false, false);
  675. initialized = true;
  676. }
  677. /* Get tomoyo_realpath of program. */
  678. retval = -ENOENT;
  679. /* I hope tomoyo_realpath() won't fail with -ENOMEM. */
  680. real_program_name = tomoyo_realpath(original_name);
  681. if (!real_program_name)
  682. goto out;
  683. /* Get tomoyo_realpath of symbolic link. */
  684. symlink_program_name = tomoyo_realpath_nofollow(original_name);
  685. if (!symlink_program_name)
  686. goto out;
  687. r.name = real_program_name;
  688. tomoyo_fill_path_info(&r);
  689. s.name = symlink_program_name;
  690. tomoyo_fill_path_info(&s);
  691. l.name = tomoyo_get_last_name(old_domain);
  692. tomoyo_fill_path_info(&l);
  693. /* Check 'alias' directive. */
  694. if (tomoyo_pathcmp(&r, &s)) {
  695. struct tomoyo_alias_entry *ptr;
  696. /* Is this program allowed to be called via symbolic links? */
  697. list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
  698. if (ptr->is_deleted ||
  699. tomoyo_pathcmp(&r, ptr->original_name) ||
  700. tomoyo_pathcmp(&s, ptr->aliased_name))
  701. continue;
  702. memset(real_program_name, 0, TOMOYO_MAX_PATHNAME_LEN);
  703. strncpy(real_program_name, ptr->aliased_name->name,
  704. TOMOYO_MAX_PATHNAME_LEN - 1);
  705. tomoyo_fill_path_info(&r);
  706. break;
  707. }
  708. }
  709. /* Check execute permission. */
  710. retval = tomoyo_check_exec_perm(old_domain, &r);
  711. if (retval < 0)
  712. goto out;
  713. new_domain_name = tmp->buffer;
  714. if (tomoyo_is_domain_initializer(old_domain->domainname, &r, &l)) {
  715. /* Transit to the child of tomoyo_kernel_domain domain. */
  716. snprintf(new_domain_name, TOMOYO_MAX_PATHNAME_LEN + 1,
  717. TOMOYO_ROOT_NAME " " "%s", real_program_name);
  718. } else if (old_domain == &tomoyo_kernel_domain &&
  719. !tomoyo_policy_loaded) {
  720. /*
  721. * Needn't to transit from kernel domain before starting
  722. * /sbin/init. But transit from kernel domain if executing
  723. * initializers because they might start before /sbin/init.
  724. */
  725. domain = old_domain;
  726. } else if (tomoyo_is_domain_keeper(old_domain->domainname, &r, &l)) {
  727. /* Keep current domain. */
  728. domain = old_domain;
  729. } else {
  730. /* Normal domain transition. */
  731. snprintf(new_domain_name, TOMOYO_MAX_PATHNAME_LEN + 1,
  732. "%s %s", old_domain_name, real_program_name);
  733. }
  734. if (domain || strlen(new_domain_name) >= TOMOYO_MAX_PATHNAME_LEN)
  735. goto done;
  736. domain = tomoyo_find_domain(new_domain_name);
  737. if (domain)
  738. goto done;
  739. if (is_enforce)
  740. goto done;
  741. domain = tomoyo_find_or_assign_new_domain(new_domain_name,
  742. old_domain->profile);
  743. done:
  744. if (domain)
  745. goto out;
  746. printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n",
  747. new_domain_name);
  748. if (is_enforce)
  749. retval = -EPERM;
  750. else
  751. old_domain->transition_failed = true;
  752. out:
  753. if (!domain)
  754. domain = old_domain;
  755. /* Update reference count on "struct tomoyo_domain_info". */
  756. atomic_inc(&domain->users);
  757. bprm->cred->security = domain;
  758. kfree(real_program_name);
  759. kfree(symlink_program_name);
  760. kfree(tmp);
  761. return retval;
  762. }