domain.c 24 KB

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