domain.c 30 KB

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