domain.c 24 KB

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