domain.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. /*
  105. * tomoyo_domain_list is used for holding list of domains.
  106. * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
  107. * permissions (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  108. *
  109. * An entry is added by
  110. *
  111. * # ( echo "<kernel>"; echo "allow_execute /sbin/init" ) > \
  112. * /sys/kernel/security/tomoyo/domain_policy
  113. *
  114. * and is deleted by
  115. *
  116. * # ( echo "<kernel>"; echo "delete allow_execute /sbin/init" ) > \
  117. * /sys/kernel/security/tomoyo/domain_policy
  118. *
  119. * and all entries are retrieved by
  120. *
  121. * # cat /sys/kernel/security/tomoyo/domain_policy
  122. *
  123. * A domain is added by
  124. *
  125. * # echo "<kernel>" > /sys/kernel/security/tomoyo/domain_policy
  126. *
  127. * and is deleted by
  128. *
  129. * # echo "delete <kernel>" > /sys/kernel/security/tomoyo/domain_policy
  130. *
  131. * and all domains are retrieved by
  132. *
  133. * # grep '^<kernel>' /sys/kernel/security/tomoyo/domain_policy
  134. *
  135. * Normally, a domainname is monotonically getting longer because a domainname
  136. * which the process will belong to if an execve() operation succeeds is
  137. * defined as a concatenation of "current domainname" + "pathname passed to
  138. * execve()".
  139. * See tomoyo_domain_initializer_list and tomoyo_domain_keeper_list for
  140. * exceptions.
  141. */
  142. LIST_HEAD(tomoyo_domain_list);
  143. /**
  144. * tomoyo_get_last_name - Get last component of a domainname.
  145. *
  146. * @domain: Pointer to "struct tomoyo_domain_info".
  147. *
  148. * Returns the last component of the domainname.
  149. */
  150. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
  151. {
  152. const char *cp0 = domain->domainname->name;
  153. const char *cp1 = strrchr(cp0, ' ');
  154. if (cp1)
  155. return cp1 + 1;
  156. return cp0;
  157. }
  158. /*
  159. * tomoyo_domain_initializer_list is used for holding list of programs which
  160. * triggers reinitialization of domainname. Normally, a domainname is
  161. * monotonically getting longer. But sometimes, we restart daemon programs.
  162. * It would be convenient for us that "a daemon started upon system boot" and
  163. * "the daemon restarted from console" belong to the same domain. Thus, TOMOYO
  164. * provides a way to shorten domainnames.
  165. *
  166. * An entry is added by
  167. *
  168. * # echo 'initialize_domain /usr/sbin/httpd' > \
  169. * /sys/kernel/security/tomoyo/exception_policy
  170. *
  171. * and is deleted by
  172. *
  173. * # echo 'delete initialize_domain /usr/sbin/httpd' > \
  174. * /sys/kernel/security/tomoyo/exception_policy
  175. *
  176. * and all entries are retrieved by
  177. *
  178. * # grep ^initialize_domain /sys/kernel/security/tomoyo/exception_policy
  179. *
  180. * In the example above, /usr/sbin/httpd will belong to
  181. * "<kernel> /usr/sbin/httpd" domain.
  182. *
  183. * You may specify a domainname using "from" keyword.
  184. * "initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
  185. * will cause "/usr/sbin/httpd" executed from "<kernel> /etc/rc.d/init.d/httpd"
  186. * domain to belong to "<kernel> /usr/sbin/httpd" domain.
  187. *
  188. * You may add "no_" prefix to "initialize_domain".
  189. * "initialize_domain /usr/sbin/httpd" and
  190. * "no_initialize_domain /usr/sbin/httpd from <kernel> /etc/rc.d/init.d/httpd"
  191. * will cause "/usr/sbin/httpd" to belong to "<kernel> /usr/sbin/httpd" domain
  192. * unless executed from "<kernel> /etc/rc.d/init.d/httpd" domain.
  193. */
  194. LIST_HEAD(tomoyo_domain_initializer_list);
  195. static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
  196. a,
  197. const struct tomoyo_acl_head *
  198. b)
  199. {
  200. const struct tomoyo_domain_initializer_entry *p1 =
  201. container_of(a, typeof(*p1), head);
  202. const struct tomoyo_domain_initializer_entry *p2 =
  203. container_of(b, typeof(*p2), head);
  204. return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
  205. && p1->domainname == p2->domainname
  206. && p1->program == p2->program;
  207. }
  208. /**
  209. * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
  210. *
  211. * @domainname: The name of domain. May be NULL.
  212. * @program: The name of program.
  213. * @is_not: True if it is "no_initialize_domain" entry.
  214. * @is_delete: True if it is a delete request.
  215. *
  216. * Returns 0 on success, negative value otherwise.
  217. *
  218. * Caller holds tomoyo_read_lock().
  219. */
  220. static int tomoyo_update_domain_initializer_entry(const char *domainname,
  221. const char *program,
  222. const bool is_not,
  223. const bool is_delete)
  224. {
  225. struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
  226. int error = is_delete ? -ENOENT : -ENOMEM;
  227. if (!tomoyo_is_correct_path(program))
  228. return -EINVAL;
  229. if (domainname) {
  230. if (!tomoyo_is_domain_def(domainname) &&
  231. tomoyo_is_correct_path(domainname))
  232. e.is_last_name = true;
  233. else if (!tomoyo_is_correct_domain(domainname))
  234. return -EINVAL;
  235. e.domainname = tomoyo_get_name(domainname);
  236. if (!e.domainname)
  237. goto out;
  238. }
  239. e.program = tomoyo_get_name(program);
  240. if (!e.program)
  241. goto out;
  242. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  243. &tomoyo_domain_initializer_list,
  244. tomoyo_same_domain_initializer_entry);
  245. out:
  246. tomoyo_put_name(e.domainname);
  247. tomoyo_put_name(e.program);
  248. return error;
  249. }
  250. /**
  251. * tomoyo_read_domain_initializer_policy - Read "struct tomoyo_domain_initializer_entry" list.
  252. *
  253. * @head: Pointer to "struct tomoyo_io_buffer".
  254. *
  255. * Returns true on success, false otherwise.
  256. *
  257. * Caller holds tomoyo_read_lock().
  258. */
  259. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
  260. {
  261. struct list_head *pos;
  262. bool done = true;
  263. list_for_each_cookie(pos, head->read_var2,
  264. &tomoyo_domain_initializer_list) {
  265. const char *no;
  266. const char *from = "";
  267. const char *domain = "";
  268. struct tomoyo_domain_initializer_entry *ptr;
  269. ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
  270. head.list);
  271. if (ptr->head.is_deleted)
  272. continue;
  273. no = ptr->is_not ? "no_" : "";
  274. if (ptr->domainname) {
  275. from = " from ";
  276. domain = ptr->domainname->name;
  277. }
  278. done = tomoyo_io_printf(head,
  279. "%s" TOMOYO_KEYWORD_INITIALIZE_DOMAIN
  280. "%s%s%s\n", no, ptr->program->name,
  281. from, domain);
  282. if (!done)
  283. break;
  284. }
  285. return done;
  286. }
  287. /**
  288. * tomoyo_write_domain_initializer_policy - Write "struct tomoyo_domain_initializer_entry" list.
  289. *
  290. * @data: String to parse.
  291. * @is_not: True if it is "no_initialize_domain" entry.
  292. * @is_delete: True if it is a delete request.
  293. *
  294. * Returns 0 on success, negative value otherwise.
  295. *
  296. * Caller holds tomoyo_read_lock().
  297. */
  298. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  299. const bool is_delete)
  300. {
  301. char *cp = strstr(data, " from ");
  302. if (cp) {
  303. *cp = '\0';
  304. return tomoyo_update_domain_initializer_entry(cp + 6, data,
  305. is_not,
  306. is_delete);
  307. }
  308. return tomoyo_update_domain_initializer_entry(NULL, data, is_not,
  309. is_delete);
  310. }
  311. /**
  312. * tomoyo_is_domain_initializer - Check whether the given program causes domainname reinitialization.
  313. *
  314. * @domainname: The name of domain.
  315. * @program: The name of program.
  316. * @last_name: The last component of @domainname.
  317. *
  318. * Returns true if executing @program reinitializes domain transition,
  319. * false otherwise.
  320. *
  321. * Caller holds tomoyo_read_lock().
  322. */
  323. static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
  324. domainname,
  325. const struct tomoyo_path_info *program,
  326. const struct tomoyo_path_info *
  327. last_name)
  328. {
  329. struct tomoyo_domain_initializer_entry *ptr;
  330. bool flag = false;
  331. list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
  332. head.list) {
  333. if (ptr->head.is_deleted)
  334. continue;
  335. if (ptr->domainname) {
  336. if (!ptr->is_last_name) {
  337. if (ptr->domainname != domainname)
  338. continue;
  339. } else {
  340. if (tomoyo_pathcmp(ptr->domainname, last_name))
  341. continue;
  342. }
  343. }
  344. if (tomoyo_pathcmp(ptr->program, program))
  345. continue;
  346. if (ptr->is_not) {
  347. flag = false;
  348. break;
  349. }
  350. flag = true;
  351. }
  352. return flag;
  353. }
  354. /*
  355. * tomoyo_domain_keeper_list is used for holding list of domainnames which
  356. * suppresses domain transition. Normally, a domainname is monotonically
  357. * getting longer. But sometimes, we want to suppress domain transition.
  358. * It would be convenient for us that programs executed from a login session
  359. * belong to the same domain. Thus, TOMOYO provides a way to suppress domain
  360. * transition.
  361. *
  362. * An entry is added by
  363. *
  364. * # echo 'keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
  365. * /sys/kernel/security/tomoyo/exception_policy
  366. *
  367. * and is deleted by
  368. *
  369. * # echo 'delete keep_domain <kernel> /usr/sbin/sshd /bin/bash' > \
  370. * /sys/kernel/security/tomoyo/exception_policy
  371. *
  372. * and all entries are retrieved by
  373. *
  374. * # grep ^keep_domain /sys/kernel/security/tomoyo/exception_policy
  375. *
  376. * In the example above, any process which belongs to
  377. * "<kernel> /usr/sbin/sshd /bin/bash" domain will remain in that domain,
  378. * unless explicitly specified by "initialize_domain" or "no_keep_domain".
  379. *
  380. * You may specify a program using "from" keyword.
  381. * "keep_domain /bin/pwd from <kernel> /usr/sbin/sshd /bin/bash"
  382. * will cause "/bin/pwd" executed from "<kernel> /usr/sbin/sshd /bin/bash"
  383. * domain to remain in "<kernel> /usr/sbin/sshd /bin/bash" domain.
  384. *
  385. * You may add "no_" prefix to "keep_domain".
  386. * "keep_domain <kernel> /usr/sbin/sshd /bin/bash" and
  387. * "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" will
  388. * cause "/usr/bin/passwd" to belong to
  389. * "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain, unless
  390. * explicitly specified by "initialize_domain".
  391. */
  392. LIST_HEAD(tomoyo_domain_keeper_list);
  393. static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
  394. const struct tomoyo_acl_head *b)
  395. {
  396. const struct tomoyo_domain_keeper_entry *p1 =
  397. container_of(a, typeof(*p1), head);
  398. const struct tomoyo_domain_keeper_entry *p2 =
  399. container_of(b, typeof(*p2), head);
  400. return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
  401. && p1->domainname == p2->domainname
  402. && p1->program == p2->program;
  403. }
  404. /**
  405. * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
  406. *
  407. * @domainname: The name of domain.
  408. * @program: The name of program. May be NULL.
  409. * @is_not: True if it is "no_keep_domain" entry.
  410. * @is_delete: True if it is a delete request.
  411. *
  412. * Returns 0 on success, negative value otherwise.
  413. *
  414. * Caller holds tomoyo_read_lock().
  415. */
  416. static int tomoyo_update_domain_keeper_entry(const char *domainname,
  417. const char *program,
  418. const bool is_not,
  419. const bool is_delete)
  420. {
  421. struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
  422. int error = is_delete ? -ENOENT : -ENOMEM;
  423. if (!tomoyo_is_domain_def(domainname) &&
  424. tomoyo_is_correct_path(domainname))
  425. e.is_last_name = true;
  426. else if (!tomoyo_is_correct_domain(domainname))
  427. return -EINVAL;
  428. if (program) {
  429. if (!tomoyo_is_correct_path(program))
  430. return -EINVAL;
  431. e.program = tomoyo_get_name(program);
  432. if (!e.program)
  433. goto out;
  434. }
  435. e.domainname = tomoyo_get_name(domainname);
  436. if (!e.domainname)
  437. goto out;
  438. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  439. &tomoyo_domain_keeper_list,
  440. tomoyo_same_domain_keeper_entry);
  441. out:
  442. tomoyo_put_name(e.domainname);
  443. tomoyo_put_name(e.program);
  444. return error;
  445. }
  446. /**
  447. * tomoyo_write_domain_keeper_policy - Write "struct tomoyo_domain_keeper_entry" list.
  448. *
  449. * @data: String to parse.
  450. * @is_not: True if it is "no_keep_domain" entry.
  451. * @is_delete: True if it is a delete request.
  452. *
  453. * Caller holds tomoyo_read_lock().
  454. */
  455. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  456. const bool is_delete)
  457. {
  458. char *cp = strstr(data, " from ");
  459. if (cp) {
  460. *cp = '\0';
  461. return tomoyo_update_domain_keeper_entry(cp + 6, data, is_not,
  462. is_delete);
  463. }
  464. return tomoyo_update_domain_keeper_entry(data, NULL, is_not, is_delete);
  465. }
  466. /**
  467. * tomoyo_read_domain_keeper_policy - Read "struct tomoyo_domain_keeper_entry" list.
  468. *
  469. * @head: Pointer to "struct tomoyo_io_buffer".
  470. *
  471. * Returns true on success, false otherwise.
  472. *
  473. * Caller holds tomoyo_read_lock().
  474. */
  475. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
  476. {
  477. struct list_head *pos;
  478. bool done = true;
  479. list_for_each_cookie(pos, head->read_var2,
  480. &tomoyo_domain_keeper_list) {
  481. struct tomoyo_domain_keeper_entry *ptr;
  482. const char *no;
  483. const char *from = "";
  484. const char *program = "";
  485. ptr = list_entry(pos, struct tomoyo_domain_keeper_entry,
  486. head.list);
  487. if (ptr->head.is_deleted)
  488. continue;
  489. no = ptr->is_not ? "no_" : "";
  490. if (ptr->program) {
  491. from = " from ";
  492. program = ptr->program->name;
  493. }
  494. done = tomoyo_io_printf(head,
  495. "%s" TOMOYO_KEYWORD_KEEP_DOMAIN
  496. "%s%s%s\n", no, program, from,
  497. ptr->domainname->name);
  498. if (!done)
  499. break;
  500. }
  501. return done;
  502. }
  503. /**
  504. * tomoyo_is_domain_keeper - Check whether the given program causes domain transition suppression.
  505. *
  506. * @domainname: The name of domain.
  507. * @program: The name of program.
  508. * @last_name: The last component of @domainname.
  509. *
  510. * Returns true if executing @program supresses domain transition,
  511. * false otherwise.
  512. *
  513. * Caller holds tomoyo_read_lock().
  514. */
  515. static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
  516. const struct tomoyo_path_info *program,
  517. const struct tomoyo_path_info *last_name)
  518. {
  519. struct tomoyo_domain_keeper_entry *ptr;
  520. bool flag = false;
  521. list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, head.list) {
  522. if (ptr->head.is_deleted)
  523. continue;
  524. if (!ptr->is_last_name) {
  525. if (ptr->domainname != domainname)
  526. continue;
  527. } else {
  528. if (tomoyo_pathcmp(ptr->domainname, last_name))
  529. continue;
  530. }
  531. if (ptr->program && tomoyo_pathcmp(ptr->program, program))
  532. continue;
  533. if (ptr->is_not) {
  534. flag = false;
  535. break;
  536. }
  537. flag = true;
  538. }
  539. return flag;
  540. }
  541. /*
  542. * tomoyo_aggregator_list is used for holding list of rewrite table for
  543. * execve() request. Some programs provides similar functionality. This keyword
  544. * allows users to aggregate such programs.
  545. *
  546. * Entries are added by
  547. *
  548. * # echo 'aggregator /usr/bin/vi /./editor' > \
  549. * /sys/kernel/security/tomoyo/exception_policy
  550. * # echo 'aggregator /usr/bin/emacs /./editor' > \
  551. * /sys/kernel/security/tomoyo/exception_policy
  552. *
  553. * and are deleted by
  554. *
  555. * # echo 'delete aggregator /usr/bin/vi /./editor' > \
  556. * /sys/kernel/security/tomoyo/exception_policy
  557. * # echo 'delete aggregator /usr/bin/emacs /./editor' > \
  558. * /sys/kernel/security/tomoyo/exception_policy
  559. *
  560. * and all entries are retrieved by
  561. *
  562. * # grep ^aggregator /sys/kernel/security/tomoyo/exception_policy
  563. *
  564. * In the example above, if /usr/bin/vi or /usr/bin/emacs are executed,
  565. * permission is checked for /./editor and domainname which the current process
  566. * will belong to after execve() succeeds is calculated using /./editor .
  567. */
  568. LIST_HEAD(tomoyo_aggregator_list);
  569. static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
  570. const struct tomoyo_acl_head *b)
  571. {
  572. const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
  573. head);
  574. const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
  575. head);
  576. return p1->original_name == p2->original_name &&
  577. p1->aggregated_name == p2->aggregated_name;
  578. }
  579. /**
  580. * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
  581. *
  582. * @original_name: The original program's name.
  583. * @aggregated_name: The program name to use.
  584. * @is_delete: True if it is a delete request.
  585. *
  586. * Returns 0 on success, negative value otherwise.
  587. *
  588. * Caller holds tomoyo_read_lock().
  589. */
  590. static int tomoyo_update_aggregator_entry(const char *original_name,
  591. const char *aggregated_name,
  592. const bool is_delete)
  593. {
  594. struct tomoyo_aggregator_entry e = { };
  595. int error = is_delete ? -ENOENT : -ENOMEM;
  596. if (!tomoyo_is_correct_path(original_name) ||
  597. !tomoyo_is_correct_path(aggregated_name))
  598. return -EINVAL;
  599. e.original_name = tomoyo_get_name(original_name);
  600. e.aggregated_name = tomoyo_get_name(aggregated_name);
  601. if (!e.original_name || !e.aggregated_name ||
  602. e.aggregated_name->is_patterned) /* No patterns allowed. */
  603. goto out;
  604. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  605. &tomoyo_aggregator_list,
  606. tomoyo_same_aggregator_entry);
  607. out:
  608. tomoyo_put_name(e.original_name);
  609. tomoyo_put_name(e.aggregated_name);
  610. return error;
  611. }
  612. /**
  613. * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
  614. *
  615. * @head: Pointer to "struct tomoyo_io_buffer".
  616. *
  617. * Returns true on success, false otherwise.
  618. *
  619. * Caller holds tomoyo_read_lock().
  620. */
  621. bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head)
  622. {
  623. struct list_head *pos;
  624. bool done = true;
  625. list_for_each_cookie(pos, head->read_var2, &tomoyo_aggregator_list) {
  626. struct tomoyo_aggregator_entry *ptr;
  627. ptr = list_entry(pos, struct tomoyo_aggregator_entry,
  628. head.list);
  629. if (ptr->head.is_deleted)
  630. continue;
  631. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR
  632. "%s %s\n", ptr->original_name->name,
  633. ptr->aggregated_name->name);
  634. if (!done)
  635. break;
  636. }
  637. return done;
  638. }
  639. /**
  640. * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
  641. *
  642. * @data: String to parse.
  643. * @is_delete: True if it is a delete request.
  644. *
  645. * Returns 0 on success, negative value otherwise.
  646. *
  647. * Caller holds tomoyo_read_lock().
  648. */
  649. int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
  650. {
  651. char *cp = strchr(data, ' ');
  652. if (!cp)
  653. return -EINVAL;
  654. *cp++ = '\0';
  655. return tomoyo_update_aggregator_entry(data, cp, is_delete);
  656. }
  657. /*
  658. * tomoyo_alias_list is used for holding list of symlink's pathnames which are
  659. * allowed to be passed to an execve() request. Normally, the domainname which
  660. * the current process will belong to after execve() succeeds is calculated
  661. * using dereferenced pathnames. But some programs behave differently depending
  662. * on the name passed to argv[0]. For busybox, calculating domainname using
  663. * dereferenced pathnames will cause all programs in the busybox to belong to
  664. * the same domain. Thus, TOMOYO provides a way to allow use of symlink's
  665. * pathname for checking execve()'s permission and calculating domainname which
  666. * the current process will belong to after execve() succeeds.
  667. *
  668. * An entry is added by
  669. *
  670. * # echo 'alias /bin/busybox /bin/cat' > \
  671. * /sys/kernel/security/tomoyo/exception_policy
  672. *
  673. * and is deleted by
  674. *
  675. * # echo 'delete alias /bin/busybox /bin/cat' > \
  676. * /sys/kernel/security/tomoyo/exception_policy
  677. *
  678. * and all entries are retrieved by
  679. *
  680. * # grep ^alias /sys/kernel/security/tomoyo/exception_policy
  681. *
  682. * In the example above, if /bin/cat is a symlink to /bin/busybox and execution
  683. * of /bin/cat is requested, permission is checked for /bin/cat rather than
  684. * /bin/busybox and domainname which the current process will belong to after
  685. * execve() succeeds is calculated using /bin/cat rather than /bin/busybox .
  686. */
  687. LIST_HEAD(tomoyo_alias_list);
  688. static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
  689. const struct tomoyo_acl_head *b)
  690. {
  691. const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
  692. head);
  693. const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
  694. head);
  695. return p1->original_name == p2->original_name &&
  696. p1->aliased_name == p2->aliased_name;
  697. }
  698. /**
  699. * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
  700. *
  701. * @original_name: The original program's real name.
  702. * @aliased_name: The symbolic program's symbolic link's name.
  703. * @is_delete: True if it is a delete request.
  704. *
  705. * Returns 0 on success, negative value otherwise.
  706. *
  707. * Caller holds tomoyo_read_lock().
  708. */
  709. static int tomoyo_update_alias_entry(const char *original_name,
  710. const char *aliased_name,
  711. const bool is_delete)
  712. {
  713. struct tomoyo_alias_entry e = { };
  714. int error = is_delete ? -ENOENT : -ENOMEM;
  715. if (!tomoyo_is_correct_path(original_name) ||
  716. !tomoyo_is_correct_path(aliased_name))
  717. return -EINVAL;
  718. e.original_name = tomoyo_get_name(original_name);
  719. e.aliased_name = tomoyo_get_name(aliased_name);
  720. if (!e.original_name || !e.aliased_name ||
  721. e.original_name->is_patterned || e.aliased_name->is_patterned)
  722. goto out; /* No patterns allowed. */
  723. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  724. &tomoyo_alias_list,
  725. tomoyo_same_alias_entry);
  726. out:
  727. tomoyo_put_name(e.original_name);
  728. tomoyo_put_name(e.aliased_name);
  729. return error;
  730. }
  731. /**
  732. * tomoyo_read_alias_policy - Read "struct tomoyo_alias_entry" list.
  733. *
  734. * @head: Pointer to "struct tomoyo_io_buffer".
  735. *
  736. * Returns true on success, false otherwise.
  737. *
  738. * Caller holds tomoyo_read_lock().
  739. */
  740. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
  741. {
  742. struct list_head *pos;
  743. bool done = true;
  744. list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
  745. struct tomoyo_alias_entry *ptr;
  746. ptr = list_entry(pos, struct tomoyo_alias_entry, head.list);
  747. if (ptr->head.is_deleted)
  748. continue;
  749. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
  750. ptr->original_name->name,
  751. ptr->aliased_name->name);
  752. if (!done)
  753. break;
  754. }
  755. return done;
  756. }
  757. /**
  758. * tomoyo_write_alias_policy - Write "struct tomoyo_alias_entry" list.
  759. *
  760. * @data: String to parse.
  761. * @is_delete: True if it is a delete request.
  762. *
  763. * Returns 0 on success, negative value otherwise.
  764. *
  765. * Caller holds tomoyo_read_lock().
  766. */
  767. int tomoyo_write_alias_policy(char *data, const bool is_delete)
  768. {
  769. char *cp = strchr(data, ' ');
  770. if (!cp)
  771. return -EINVAL;
  772. *cp++ = '\0';
  773. return tomoyo_update_alias_entry(data, cp, is_delete);
  774. }
  775. /**
  776. * tomoyo_find_or_assign_new_domain - Create a domain.
  777. *
  778. * @domainname: The name of domain.
  779. * @profile: Profile number to assign if the domain was newly created.
  780. *
  781. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  782. *
  783. * Caller holds tomoyo_read_lock().
  784. */
  785. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  786. domainname,
  787. const u8 profile)
  788. {
  789. struct tomoyo_domain_info *entry;
  790. struct tomoyo_domain_info *domain = NULL;
  791. const struct tomoyo_path_info *saved_domainname;
  792. bool found = false;
  793. if (!tomoyo_is_correct_domain(domainname))
  794. return NULL;
  795. saved_domainname = tomoyo_get_name(domainname);
  796. if (!saved_domainname)
  797. return NULL;
  798. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  799. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  800. goto out;
  801. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  802. if (domain->is_deleted ||
  803. tomoyo_pathcmp(saved_domainname, domain->domainname))
  804. continue;
  805. found = true;
  806. break;
  807. }
  808. if (!found && tomoyo_memory_ok(entry)) {
  809. INIT_LIST_HEAD(&entry->acl_info_list);
  810. entry->domainname = saved_domainname;
  811. saved_domainname = NULL;
  812. entry->profile = profile;
  813. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  814. domain = entry;
  815. entry = NULL;
  816. found = true;
  817. }
  818. mutex_unlock(&tomoyo_policy_lock);
  819. out:
  820. tomoyo_put_name(saved_domainname);
  821. kfree(entry);
  822. return found ? domain : NULL;
  823. }
  824. /**
  825. * tomoyo_find_next_domain - Find a domain.
  826. *
  827. * @bprm: Pointer to "struct linux_binprm".
  828. *
  829. * Returns 0 on success, negative value otherwise.
  830. *
  831. * Caller holds tomoyo_read_lock().
  832. */
  833. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  834. {
  835. struct tomoyo_request_info r;
  836. char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  837. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  838. struct tomoyo_domain_info *domain = NULL;
  839. const char *old_domain_name = old_domain->domainname->name;
  840. const char *original_name = bprm->filename;
  841. u8 mode;
  842. bool is_enforce;
  843. int retval = -ENOMEM;
  844. bool need_kfree = false;
  845. struct tomoyo_path_info rn = { }; /* real name */
  846. struct tomoyo_path_info sn = { }; /* symlink name */
  847. struct tomoyo_path_info ln; /* last name */
  848. ln.name = tomoyo_get_last_name(old_domain);
  849. tomoyo_fill_path_info(&ln);
  850. mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  851. is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
  852. if (!tmp)
  853. goto out;
  854. retry:
  855. if (need_kfree) {
  856. kfree(rn.name);
  857. need_kfree = false;
  858. }
  859. /* Get tomoyo_realpath of program. */
  860. retval = -ENOENT;
  861. rn.name = tomoyo_realpath(original_name);
  862. if (!rn.name)
  863. goto out;
  864. tomoyo_fill_path_info(&rn);
  865. need_kfree = true;
  866. /* Get tomoyo_realpath of symbolic link. */
  867. sn.name = tomoyo_realpath_nofollow(original_name);
  868. if (!sn.name)
  869. goto out;
  870. tomoyo_fill_path_info(&sn);
  871. /* Check 'alias' directive. */
  872. if (tomoyo_pathcmp(&rn, &sn)) {
  873. struct tomoyo_alias_entry *ptr;
  874. /* Is this program allowed to be called via symbolic links? */
  875. list_for_each_entry_rcu(ptr, &tomoyo_alias_list, head.list) {
  876. if (ptr->head.is_deleted ||
  877. tomoyo_pathcmp(&rn, ptr->original_name) ||
  878. tomoyo_pathcmp(&sn, ptr->aliased_name))
  879. continue;
  880. kfree(rn.name);
  881. need_kfree = false;
  882. /* This is OK because it is read only. */
  883. rn = *ptr->aliased_name;
  884. break;
  885. }
  886. }
  887. /* Check 'aggregator' directive. */
  888. {
  889. struct tomoyo_aggregator_entry *ptr;
  890. list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list,
  891. head.list) {
  892. if (ptr->head.is_deleted ||
  893. !tomoyo_path_matches_pattern(&rn,
  894. ptr->original_name))
  895. continue;
  896. if (need_kfree)
  897. kfree(rn.name);
  898. need_kfree = false;
  899. /* This is OK because it is read only. */
  900. rn = *ptr->aggregated_name;
  901. break;
  902. }
  903. }
  904. /* Check execute permission. */
  905. retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
  906. if (retval == TOMOYO_RETRY_REQUEST)
  907. goto retry;
  908. if (retval < 0)
  909. goto out;
  910. if (tomoyo_is_domain_initializer(old_domain->domainname, &rn, &ln)) {
  911. /* Transit to the child of tomoyo_kernel_domain domain. */
  912. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
  913. TOMOYO_ROOT_NAME " " "%s", rn.name);
  914. } else if (old_domain == &tomoyo_kernel_domain &&
  915. !tomoyo_policy_loaded) {
  916. /*
  917. * Needn't to transit from kernel domain before starting
  918. * /sbin/init. But transit from kernel domain if executing
  919. * initializers because they might start before /sbin/init.
  920. */
  921. domain = old_domain;
  922. } else if (tomoyo_is_domain_keeper(old_domain->domainname, &rn, &ln)) {
  923. /* Keep current domain. */
  924. domain = old_domain;
  925. } else {
  926. /* Normal domain transition. */
  927. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1,
  928. "%s %s", old_domain_name, rn.name);
  929. }
  930. if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
  931. goto done;
  932. domain = tomoyo_find_domain(tmp);
  933. if (domain)
  934. goto done;
  935. if (is_enforce) {
  936. int error = tomoyo_supervisor(&r, "# wants to create domain\n"
  937. "%s\n", tmp);
  938. if (error == TOMOYO_RETRY_REQUEST)
  939. goto retry;
  940. if (error < 0)
  941. goto done;
  942. }
  943. domain = tomoyo_find_or_assign_new_domain(tmp, old_domain->profile);
  944. done:
  945. if (domain)
  946. goto out;
  947. printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
  948. if (is_enforce)
  949. retval = -EPERM;
  950. else
  951. old_domain->transition_failed = true;
  952. out:
  953. if (!domain)
  954. domain = old_domain;
  955. /* Update reference count on "struct tomoyo_domain_info". */
  956. atomic_inc(&domain->users);
  957. bprm->cred->security = domain;
  958. if (need_kfree)
  959. kfree(rn.name);
  960. kfree(sn.name);
  961. kfree(tmp);
  962. return retval;
  963. }