common.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. /*
  2. * security/tomoyo/common.c
  3. *
  4. * Common functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include <linux/uaccess.h>
  9. #include <linux/slab.h>
  10. #include <linux/security.h>
  11. #include <linux/hardirq.h>
  12. #include "common.h"
  13. /* String table for functionality that takes 4 modes. */
  14. static const char *tomoyo_mode_4[4] = {
  15. "disabled", "learning", "permissive", "enforcing"
  16. };
  17. /* String table for functionality that takes 2 modes. */
  18. static const char *tomoyo_mode_2[4] = {
  19. "disabled", "enabled", "enabled", "enabled"
  20. };
  21. /*
  22. * tomoyo_control_array is a static data which contains
  23. *
  24. * (1) functionality name used by /sys/kernel/security/tomoyo/profile .
  25. * (2) initial values for "struct tomoyo_profile".
  26. * (3) max values for "struct tomoyo_profile".
  27. */
  28. static struct {
  29. const char *keyword;
  30. unsigned int current_value;
  31. const unsigned int max_value;
  32. } tomoyo_control_array[TOMOYO_MAX_CONTROL_INDEX] = {
  33. [TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
  34. [TOMOYO_MAX_ACCEPT_ENTRY] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX },
  35. [TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
  36. };
  37. /*
  38. * tomoyo_profile is a structure which is used for holding the mode of access
  39. * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
  40. * An administrator can define up to 256 profiles.
  41. * The ->profile of "struct tomoyo_domain_info" is used for remembering
  42. * the profile's number (0 - 255) assigned to that domain.
  43. */
  44. static struct tomoyo_profile {
  45. unsigned int value[TOMOYO_MAX_CONTROL_INDEX];
  46. const struct tomoyo_path_info *comment;
  47. } *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
  48. /* Permit policy management by non-root user? */
  49. static bool tomoyo_manage_by_non_root;
  50. /* Utility functions. */
  51. /**
  52. * tomoyo_print_name_union - Print a tomoyo_name_union.
  53. *
  54. * @head: Pointer to "struct tomoyo_io_buffer".
  55. * @ptr: Pointer to "struct tomoyo_name_union".
  56. *
  57. * Returns true on success, false otherwise.
  58. */
  59. static bool tomoyo_print_name_union(struct tomoyo_io_buffer *head,
  60. const struct tomoyo_name_union *ptr)
  61. {
  62. int pos = head->read_avail;
  63. if (pos && head->read_buf[pos - 1] == ' ')
  64. head->read_avail--;
  65. if (ptr->is_group)
  66. return tomoyo_io_printf(head, " @%s",
  67. ptr->group->group_name->name);
  68. return tomoyo_io_printf(head, " %s", ptr->filename->name);
  69. }
  70. /**
  71. * tomoyo_print_number_union - Print a tomoyo_number_union.
  72. *
  73. * @head: Pointer to "struct tomoyo_io_buffer".
  74. * @ptr: Pointer to "struct tomoyo_number_union".
  75. *
  76. * Returns true on success, false otherwise.
  77. */
  78. bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
  79. const struct tomoyo_number_union *ptr)
  80. {
  81. unsigned long min;
  82. unsigned long max;
  83. u8 min_type;
  84. u8 max_type;
  85. if (!tomoyo_io_printf(head, " "))
  86. return false;
  87. if (ptr->is_group)
  88. return tomoyo_io_printf(head, "@%s",
  89. ptr->group->group_name->name);
  90. min_type = ptr->min_type;
  91. max_type = ptr->max_type;
  92. min = ptr->values[0];
  93. max = ptr->values[1];
  94. switch (min_type) {
  95. case TOMOYO_VALUE_TYPE_HEXADECIMAL:
  96. if (!tomoyo_io_printf(head, "0x%lX", min))
  97. return false;
  98. break;
  99. case TOMOYO_VALUE_TYPE_OCTAL:
  100. if (!tomoyo_io_printf(head, "0%lo", min))
  101. return false;
  102. break;
  103. default:
  104. if (!tomoyo_io_printf(head, "%lu", min))
  105. return false;
  106. break;
  107. }
  108. if (min == max && min_type == max_type)
  109. return true;
  110. switch (max_type) {
  111. case TOMOYO_VALUE_TYPE_HEXADECIMAL:
  112. return tomoyo_io_printf(head, "-0x%lX", max);
  113. case TOMOYO_VALUE_TYPE_OCTAL:
  114. return tomoyo_io_printf(head, "-0%lo", max);
  115. default:
  116. return tomoyo_io_printf(head, "-%lu", max);
  117. }
  118. }
  119. /**
  120. * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
  121. *
  122. * @head: Pointer to "struct tomoyo_io_buffer".
  123. * @fmt: The printf()'s format string, followed by parameters.
  124. *
  125. * Returns true if output was written, false otherwise.
  126. *
  127. * The snprintf() will truncate, but tomoyo_io_printf() won't.
  128. */
  129. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  130. {
  131. va_list args;
  132. int len;
  133. int pos = head->read_avail;
  134. int size = head->readbuf_size - pos;
  135. if (size <= 0)
  136. return false;
  137. va_start(args, fmt);
  138. len = vsnprintf(head->read_buf + pos, size, fmt, args);
  139. va_end(args);
  140. if (pos + len >= head->readbuf_size)
  141. return false;
  142. head->read_avail += len;
  143. return true;
  144. }
  145. /**
  146. * tomoyo_check_flags - Check mode for specified functionality.
  147. *
  148. * @domain: Pointer to "struct tomoyo_domain_info".
  149. * @index: The functionality to check mode.
  150. *
  151. * TOMOYO checks only process context.
  152. * This code disables TOMOYO's enforcement in case the function is called from
  153. * interrupt context.
  154. */
  155. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  156. const u8 index)
  157. {
  158. const u8 profile = domain->profile;
  159. if (WARN_ON(in_interrupt()))
  160. return 0;
  161. return tomoyo_policy_loaded && index < TOMOYO_MAX_CONTROL_INDEX
  162. #if TOMOYO_MAX_PROFILES != 256
  163. && profile < TOMOYO_MAX_PROFILES
  164. #endif
  165. && tomoyo_profile_ptr[profile] ?
  166. tomoyo_profile_ptr[profile]->value[index] : 0;
  167. }
  168. /**
  169. * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
  170. *
  171. * @domain: Pointer to "struct tomoyo_domain_info".
  172. *
  173. * Returns true if domain policy violation warning should be printed to
  174. * console.
  175. */
  176. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain)
  177. {
  178. return tomoyo_check_flags(domain, TOMOYO_VERBOSE) != 0;
  179. }
  180. /**
  181. * tomoyo_find_or_assign_new_profile - Create a new profile.
  182. *
  183. * @profile: Profile number to create.
  184. *
  185. * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
  186. */
  187. static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
  188. int profile)
  189. {
  190. struct tomoyo_profile *ptr = NULL;
  191. int i;
  192. if (profile >= TOMOYO_MAX_PROFILES)
  193. return NULL;
  194. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  195. return NULL;
  196. ptr = tomoyo_profile_ptr[profile];
  197. if (ptr)
  198. goto ok;
  199. ptr = kmalloc(sizeof(*ptr), GFP_NOFS);
  200. if (!tomoyo_memory_ok(ptr)) {
  201. kfree(ptr);
  202. ptr = NULL;
  203. goto ok;
  204. }
  205. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
  206. ptr->value[i] = tomoyo_control_array[i].current_value;
  207. mb(); /* Avoid out-of-order execution. */
  208. tomoyo_profile_ptr[profile] = ptr;
  209. ok:
  210. mutex_unlock(&tomoyo_policy_lock);
  211. return ptr;
  212. }
  213. /**
  214. * tomoyo_write_profile - Write to profile table.
  215. *
  216. * @head: Pointer to "struct tomoyo_io_buffer".
  217. *
  218. * Returns 0 on success, negative value otherwise.
  219. */
  220. static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
  221. {
  222. char *data = head->write_buf;
  223. unsigned int i;
  224. unsigned int value;
  225. char *cp;
  226. struct tomoyo_profile *profile;
  227. unsigned long num;
  228. cp = strchr(data, '-');
  229. if (cp)
  230. *cp = '\0';
  231. if (strict_strtoul(data, 10, &num))
  232. return -EINVAL;
  233. if (cp)
  234. data = cp + 1;
  235. profile = tomoyo_find_or_assign_new_profile(num);
  236. if (!profile)
  237. return -EINVAL;
  238. cp = strchr(data, '=');
  239. if (!cp)
  240. return -EINVAL;
  241. *cp = '\0';
  242. if (!strcmp(data, "COMMENT")) {
  243. const struct tomoyo_path_info *old_comment = profile->comment;
  244. profile->comment = tomoyo_get_name(cp + 1);
  245. tomoyo_put_name(old_comment);
  246. return 0;
  247. }
  248. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
  249. if (strcmp(data, tomoyo_control_array[i].keyword))
  250. continue;
  251. if (sscanf(cp + 1, "%u", &value) != 1) {
  252. int j;
  253. const char **modes;
  254. switch (i) {
  255. case TOMOYO_VERBOSE:
  256. modes = tomoyo_mode_2;
  257. break;
  258. default:
  259. modes = tomoyo_mode_4;
  260. break;
  261. }
  262. for (j = 0; j < 4; j++) {
  263. if (strcmp(cp + 1, modes[j]))
  264. continue;
  265. value = j;
  266. break;
  267. }
  268. if (j == 4)
  269. return -EINVAL;
  270. } else if (value > tomoyo_control_array[i].max_value) {
  271. value = tomoyo_control_array[i].max_value;
  272. }
  273. profile->value[i] = value;
  274. return 0;
  275. }
  276. return -EINVAL;
  277. }
  278. /**
  279. * tomoyo_read_profile - Read from profile table.
  280. *
  281. * @head: Pointer to "struct tomoyo_io_buffer".
  282. *
  283. * Returns 0.
  284. */
  285. static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
  286. {
  287. static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
  288. int step;
  289. if (head->read_eof)
  290. return 0;
  291. for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
  292. step++) {
  293. const u8 index = step / total;
  294. u8 type = step % total;
  295. const struct tomoyo_profile *profile
  296. = tomoyo_profile_ptr[index];
  297. head->read_step = step;
  298. if (!profile)
  299. continue;
  300. if (!type) { /* Print profile' comment tag. */
  301. if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
  302. index, profile->comment ?
  303. profile->comment->name : ""))
  304. break;
  305. continue;
  306. }
  307. type--;
  308. if (type < TOMOYO_MAX_CONTROL_INDEX) {
  309. const unsigned int value = profile->value[type];
  310. const char **modes = NULL;
  311. const char *keyword
  312. = tomoyo_control_array[type].keyword;
  313. switch (tomoyo_control_array[type].max_value) {
  314. case 3:
  315. modes = tomoyo_mode_4;
  316. break;
  317. case 1:
  318. modes = tomoyo_mode_2;
  319. break;
  320. }
  321. if (modes) {
  322. if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
  323. keyword, modes[value]))
  324. break;
  325. } else {
  326. if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
  327. keyword, value))
  328. break;
  329. }
  330. }
  331. }
  332. if (step == TOMOYO_MAX_PROFILES * total)
  333. head->read_eof = true;
  334. return 0;
  335. }
  336. /*
  337. * tomoyo_policy_manager_list is used for holding list of domainnames or
  338. * programs which are permitted to modify configuration via
  339. * /sys/kernel/security/tomoyo/ interface.
  340. *
  341. * An entry is added by
  342. *
  343. * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
  344. * /sys/kernel/security/tomoyo/manager
  345. * (if you want to specify by a domainname)
  346. *
  347. * or
  348. *
  349. * # echo '/usr/sbin/tomoyo-editpolicy' > /sys/kernel/security/tomoyo/manager
  350. * (if you want to specify by a program's location)
  351. *
  352. * and is deleted by
  353. *
  354. * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
  355. * /sys/kernel/security/tomoyo/manager
  356. *
  357. * or
  358. *
  359. * # echo 'delete /usr/sbin/tomoyo-editpolicy' > \
  360. * /sys/kernel/security/tomoyo/manager
  361. *
  362. * and all entries are retrieved by
  363. *
  364. * # cat /sys/kernel/security/tomoyo/manager
  365. */
  366. LIST_HEAD(tomoyo_policy_manager_list);
  367. /**
  368. * tomoyo_update_manager_entry - Add a manager entry.
  369. *
  370. * @manager: The path to manager or the domainnamme.
  371. * @is_delete: True if it is a delete request.
  372. *
  373. * Returns 0 on success, negative value otherwise.
  374. *
  375. * Caller holds tomoyo_read_lock().
  376. */
  377. static int tomoyo_update_manager_entry(const char *manager,
  378. const bool is_delete)
  379. {
  380. struct tomoyo_policy_manager_entry *ptr;
  381. struct tomoyo_policy_manager_entry e = { };
  382. int error = is_delete ? -ENOENT : -ENOMEM;
  383. if (tomoyo_is_domain_def(manager)) {
  384. if (!tomoyo_is_correct_domain(manager))
  385. return -EINVAL;
  386. e.is_domain = true;
  387. } else {
  388. if (!tomoyo_is_correct_path(manager, 1, -1, -1))
  389. return -EINVAL;
  390. }
  391. e.manager = tomoyo_get_name(manager);
  392. if (!e.manager)
  393. return -ENOMEM;
  394. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  395. goto out;
  396. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  397. if (ptr->manager != e.manager)
  398. continue;
  399. ptr->is_deleted = is_delete;
  400. error = 0;
  401. break;
  402. }
  403. if (!is_delete && error) {
  404. struct tomoyo_policy_manager_entry *entry =
  405. tomoyo_commit_ok(&e, sizeof(e));
  406. if (entry) {
  407. list_add_tail_rcu(&entry->list,
  408. &tomoyo_policy_manager_list);
  409. error = 0;
  410. }
  411. }
  412. mutex_unlock(&tomoyo_policy_lock);
  413. out:
  414. tomoyo_put_name(e.manager);
  415. return error;
  416. }
  417. /**
  418. * tomoyo_write_manager_policy - Write manager policy.
  419. *
  420. * @head: Pointer to "struct tomoyo_io_buffer".
  421. *
  422. * Returns 0 on success, negative value otherwise.
  423. *
  424. * Caller holds tomoyo_read_lock().
  425. */
  426. static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
  427. {
  428. char *data = head->write_buf;
  429. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  430. if (!strcmp(data, "manage_by_non_root")) {
  431. tomoyo_manage_by_non_root = !is_delete;
  432. return 0;
  433. }
  434. return tomoyo_update_manager_entry(data, is_delete);
  435. }
  436. /**
  437. * tomoyo_read_manager_policy - Read manager policy.
  438. *
  439. * @head: Pointer to "struct tomoyo_io_buffer".
  440. *
  441. * Returns 0.
  442. *
  443. * Caller holds tomoyo_read_lock().
  444. */
  445. static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
  446. {
  447. struct list_head *pos;
  448. bool done = true;
  449. if (head->read_eof)
  450. return 0;
  451. list_for_each_cookie(pos, head->read_var2,
  452. &tomoyo_policy_manager_list) {
  453. struct tomoyo_policy_manager_entry *ptr;
  454. ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
  455. list);
  456. if (ptr->is_deleted)
  457. continue;
  458. done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
  459. if (!done)
  460. break;
  461. }
  462. head->read_eof = done;
  463. return 0;
  464. }
  465. /**
  466. * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
  467. *
  468. * Returns true if the current process is permitted to modify policy
  469. * via /sys/kernel/security/tomoyo/ interface.
  470. *
  471. * Caller holds tomoyo_read_lock().
  472. */
  473. static bool tomoyo_is_policy_manager(void)
  474. {
  475. struct tomoyo_policy_manager_entry *ptr;
  476. const char *exe;
  477. const struct task_struct *task = current;
  478. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  479. bool found = false;
  480. if (!tomoyo_policy_loaded)
  481. return true;
  482. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  483. return false;
  484. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  485. if (!ptr->is_deleted && ptr->is_domain
  486. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  487. found = true;
  488. break;
  489. }
  490. }
  491. if (found)
  492. return true;
  493. exe = tomoyo_get_exe();
  494. if (!exe)
  495. return false;
  496. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  497. if (!ptr->is_deleted && !ptr->is_domain
  498. && !strcmp(exe, ptr->manager->name)) {
  499. found = true;
  500. break;
  501. }
  502. }
  503. if (!found) { /* Reduce error messages. */
  504. static pid_t last_pid;
  505. const pid_t pid = current->pid;
  506. if (last_pid != pid) {
  507. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  508. "update policies.\n", domainname->name, exe);
  509. last_pid = pid;
  510. }
  511. }
  512. kfree(exe);
  513. return found;
  514. }
  515. /**
  516. * tomoyo_is_select_one - Parse select command.
  517. *
  518. * @head: Pointer to "struct tomoyo_io_buffer".
  519. * @data: String to parse.
  520. *
  521. * Returns true on success, false otherwise.
  522. *
  523. * Caller holds tomoyo_read_lock().
  524. */
  525. static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
  526. const char *data)
  527. {
  528. unsigned int pid;
  529. struct tomoyo_domain_info *domain = NULL;
  530. bool global_pid = false;
  531. if (sscanf(data, "pid=%u", &pid) == 1 ||
  532. (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
  533. struct task_struct *p;
  534. rcu_read_lock();
  535. read_lock(&tasklist_lock);
  536. if (global_pid)
  537. p = find_task_by_pid_ns(pid, &init_pid_ns);
  538. else
  539. p = find_task_by_vpid(pid);
  540. if (p)
  541. domain = tomoyo_real_domain(p);
  542. read_unlock(&tasklist_lock);
  543. rcu_read_unlock();
  544. } else if (!strncmp(data, "domain=", 7)) {
  545. if (tomoyo_is_domain_def(data + 7))
  546. domain = tomoyo_find_domain(data + 7);
  547. } else
  548. return false;
  549. head->write_var1 = domain;
  550. /* Accessing read_buf is safe because head->io_sem is held. */
  551. if (!head->read_buf)
  552. return true; /* Do nothing if open(O_WRONLY). */
  553. head->read_avail = 0;
  554. tomoyo_io_printf(head, "# select %s\n", data);
  555. head->read_single_domain = true;
  556. head->read_eof = !domain;
  557. if (domain) {
  558. struct tomoyo_domain_info *d;
  559. head->read_var1 = NULL;
  560. list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
  561. if (d == domain)
  562. break;
  563. head->read_var1 = &d->list;
  564. }
  565. head->read_var2 = NULL;
  566. head->read_bit = 0;
  567. head->read_step = 0;
  568. if (domain->is_deleted)
  569. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  570. }
  571. return true;
  572. }
  573. /**
  574. * tomoyo_delete_domain - Delete a domain.
  575. *
  576. * @domainname: The name of domain.
  577. *
  578. * Returns 0.
  579. *
  580. * Caller holds tomoyo_read_lock().
  581. */
  582. static int tomoyo_delete_domain(char *domainname)
  583. {
  584. struct tomoyo_domain_info *domain;
  585. struct tomoyo_path_info name;
  586. name.name = domainname;
  587. tomoyo_fill_path_info(&name);
  588. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  589. return 0;
  590. /* Is there an active domain? */
  591. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  592. /* Never delete tomoyo_kernel_domain */
  593. if (domain == &tomoyo_kernel_domain)
  594. continue;
  595. if (domain->is_deleted ||
  596. tomoyo_pathcmp(domain->domainname, &name))
  597. continue;
  598. domain->is_deleted = true;
  599. break;
  600. }
  601. mutex_unlock(&tomoyo_policy_lock);
  602. return 0;
  603. }
  604. /**
  605. * tomoyo_write_domain_policy2 - Write domain policy.
  606. *
  607. * @head: Pointer to "struct tomoyo_io_buffer".
  608. *
  609. * Returns 0 on success, negative value otherwise.
  610. *
  611. * Caller holds tomoyo_read_lock().
  612. */
  613. static int tomoyo_write_domain_policy2(char *data,
  614. struct tomoyo_domain_info *domain,
  615. const bool is_delete)
  616. {
  617. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
  618. return tomoyo_write_mount_policy(data, domain, is_delete);
  619. return tomoyo_write_file_policy(data, domain, is_delete);
  620. }
  621. /**
  622. * tomoyo_write_domain_policy - Write domain policy.
  623. *
  624. * @head: Pointer to "struct tomoyo_io_buffer".
  625. *
  626. * Returns 0 on success, negative value otherwise.
  627. *
  628. * Caller holds tomoyo_read_lock().
  629. */
  630. static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
  631. {
  632. char *data = head->write_buf;
  633. struct tomoyo_domain_info *domain = head->write_var1;
  634. bool is_delete = false;
  635. bool is_select = false;
  636. unsigned int profile;
  637. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  638. is_delete = true;
  639. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  640. is_select = true;
  641. if (is_select && tomoyo_is_select_one(head, data))
  642. return 0;
  643. /* Don't allow updating policies by non manager programs. */
  644. if (!tomoyo_is_policy_manager())
  645. return -EPERM;
  646. if (tomoyo_is_domain_def(data)) {
  647. domain = NULL;
  648. if (is_delete)
  649. tomoyo_delete_domain(data);
  650. else if (is_select)
  651. domain = tomoyo_find_domain(data);
  652. else
  653. domain = tomoyo_find_or_assign_new_domain(data, 0);
  654. head->write_var1 = domain;
  655. return 0;
  656. }
  657. if (!domain)
  658. return -EINVAL;
  659. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  660. && profile < TOMOYO_MAX_PROFILES) {
  661. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  662. domain->profile = (u8) profile;
  663. return 0;
  664. }
  665. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  666. domain->ignore_global_allow_read = !is_delete;
  667. return 0;
  668. }
  669. if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
  670. domain->quota_warned = !is_delete;
  671. return 0;
  672. }
  673. if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
  674. domain->transition_failed = !is_delete;
  675. return 0;
  676. }
  677. return tomoyo_write_domain_policy2(data, domain, is_delete);
  678. }
  679. /**
  680. * tomoyo_print_path_acl - Print a single path ACL entry.
  681. *
  682. * @head: Pointer to "struct tomoyo_io_buffer".
  683. * @ptr: Pointer to "struct tomoyo_path_acl".
  684. *
  685. * Returns true on success, false otherwise.
  686. */
  687. static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
  688. struct tomoyo_path_acl *ptr)
  689. {
  690. int pos;
  691. u8 bit;
  692. const u16 perm = ptr->perm;
  693. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  694. if (!(perm & (1 << bit)))
  695. continue;
  696. /* Print "read/write" instead of "read" and "write". */
  697. if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
  698. && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  699. continue;
  700. pos = head->read_avail;
  701. if (!tomoyo_io_printf(head, "allow_%s ",
  702. tomoyo_path2keyword(bit)) ||
  703. !tomoyo_print_name_union(head, &ptr->name) ||
  704. !tomoyo_io_printf(head, "\n"))
  705. goto out;
  706. }
  707. head->read_bit = 0;
  708. return true;
  709. out:
  710. head->read_bit = bit;
  711. head->read_avail = pos;
  712. return false;
  713. }
  714. /**
  715. * tomoyo_print_path2_acl - Print a double path ACL entry.
  716. *
  717. * @head: Pointer to "struct tomoyo_io_buffer".
  718. * @ptr: Pointer to "struct tomoyo_path2_acl".
  719. *
  720. * Returns true on success, false otherwise.
  721. */
  722. static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
  723. struct tomoyo_path2_acl *ptr)
  724. {
  725. int pos;
  726. const u8 perm = ptr->perm;
  727. u8 bit;
  728. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
  729. if (!(perm & (1 << bit)))
  730. continue;
  731. pos = head->read_avail;
  732. if (!tomoyo_io_printf(head, "allow_%s ",
  733. tomoyo_path22keyword(bit)) ||
  734. !tomoyo_print_name_union(head, &ptr->name1) ||
  735. !tomoyo_print_name_union(head, &ptr->name2) ||
  736. !tomoyo_io_printf(head, "\n"))
  737. goto out;
  738. }
  739. head->read_bit = 0;
  740. return true;
  741. out:
  742. head->read_bit = bit;
  743. head->read_avail = pos;
  744. return false;
  745. }
  746. /**
  747. * tomoyo_print_path_number_acl - Print a path_number ACL entry.
  748. *
  749. * @head: Pointer to "struct tomoyo_io_buffer".
  750. * @ptr: Pointer to "struct tomoyo_path_number_acl".
  751. *
  752. * Returns true on success, false otherwise.
  753. */
  754. static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
  755. struct tomoyo_path_number_acl *ptr)
  756. {
  757. int pos;
  758. u8 bit;
  759. const u8 perm = ptr->perm;
  760. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
  761. bit++) {
  762. if (!(perm & (1 << bit)))
  763. continue;
  764. pos = head->read_avail;
  765. if (!tomoyo_io_printf(head, "allow_%s",
  766. tomoyo_path_number2keyword(bit)) ||
  767. !tomoyo_print_name_union(head, &ptr->name) ||
  768. !tomoyo_print_number_union(head, &ptr->number) ||
  769. !tomoyo_io_printf(head, "\n"))
  770. goto out;
  771. }
  772. head->read_bit = 0;
  773. return true;
  774. out:
  775. head->read_bit = bit;
  776. head->read_avail = pos;
  777. return false;
  778. }
  779. /**
  780. * tomoyo_print_path_number3_acl - Print a path_number3 ACL entry.
  781. *
  782. * @head: Pointer to "struct tomoyo_io_buffer".
  783. * @ptr: Pointer to "struct tomoyo_path_number3_acl".
  784. *
  785. * Returns true on success, false otherwise.
  786. */
  787. static bool tomoyo_print_path_number3_acl(struct tomoyo_io_buffer *head,
  788. struct tomoyo_path_number3_acl *ptr)
  789. {
  790. int pos;
  791. u8 bit;
  792. const u16 perm = ptr->perm;
  793. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER3_OPERATION;
  794. bit++) {
  795. if (!(perm & (1 << bit)))
  796. continue;
  797. pos = head->read_avail;
  798. if (!tomoyo_io_printf(head, "allow_%s",
  799. tomoyo_path_number32keyword(bit)) ||
  800. !tomoyo_print_name_union(head, &ptr->name) ||
  801. !tomoyo_print_number_union(head, &ptr->mode) ||
  802. !tomoyo_print_number_union(head, &ptr->major) ||
  803. !tomoyo_print_number_union(head, &ptr->minor) ||
  804. !tomoyo_io_printf(head, "\n"))
  805. goto out;
  806. }
  807. head->read_bit = 0;
  808. return true;
  809. out:
  810. head->read_bit = bit;
  811. head->read_avail = pos;
  812. return false;
  813. }
  814. /**
  815. * tomoyo_print_mount_acl - Print a mount ACL entry.
  816. *
  817. * @head: Pointer to "struct tomoyo_io_buffer".
  818. * @ptr: Pointer to "struct tomoyo_mount_acl".
  819. *
  820. * Returns true on success, false otherwise.
  821. */
  822. static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
  823. struct tomoyo_mount_acl *ptr)
  824. {
  825. const int pos = head->read_avail;
  826. if (ptr->is_deleted)
  827. return true;
  828. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
  829. !tomoyo_print_name_union(head, &ptr->dev_name) ||
  830. !tomoyo_print_name_union(head, &ptr->dir_name) ||
  831. !tomoyo_print_name_union(head, &ptr->fs_type) ||
  832. !tomoyo_print_number_union(head, &ptr->flags) ||
  833. !tomoyo_io_printf(head, "\n")) {
  834. head->read_avail = pos;
  835. return false;
  836. }
  837. return true;
  838. }
  839. /**
  840. * tomoyo_print_entry - Print an ACL entry.
  841. *
  842. * @head: Pointer to "struct tomoyo_io_buffer".
  843. * @ptr: Pointer to an ACL entry.
  844. *
  845. * Returns true on success, false otherwise.
  846. */
  847. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  848. struct tomoyo_acl_info *ptr)
  849. {
  850. const u8 acl_type = ptr->type;
  851. if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  852. struct tomoyo_path_acl *acl
  853. = container_of(ptr, struct tomoyo_path_acl, head);
  854. return tomoyo_print_path_acl(head, acl);
  855. }
  856. if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
  857. struct tomoyo_path2_acl *acl
  858. = container_of(ptr, struct tomoyo_path2_acl, head);
  859. return tomoyo_print_path2_acl(head, acl);
  860. }
  861. if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
  862. struct tomoyo_path_number_acl *acl
  863. = container_of(ptr, struct tomoyo_path_number_acl,
  864. head);
  865. return tomoyo_print_path_number_acl(head, acl);
  866. }
  867. if (acl_type == TOMOYO_TYPE_PATH_NUMBER3_ACL) {
  868. struct tomoyo_path_number3_acl *acl
  869. = container_of(ptr, struct tomoyo_path_number3_acl,
  870. head);
  871. return tomoyo_print_path_number3_acl(head, acl);
  872. }
  873. if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
  874. struct tomoyo_mount_acl *acl
  875. = container_of(ptr, struct tomoyo_mount_acl, head);
  876. return tomoyo_print_mount_acl(head, acl);
  877. }
  878. BUG(); /* This must not happen. */
  879. return false;
  880. }
  881. /**
  882. * tomoyo_read_domain_policy - Read domain policy.
  883. *
  884. * @head: Pointer to "struct tomoyo_io_buffer".
  885. *
  886. * Returns 0.
  887. *
  888. * Caller holds tomoyo_read_lock().
  889. */
  890. static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
  891. {
  892. struct list_head *dpos;
  893. struct list_head *apos;
  894. bool done = true;
  895. if (head->read_eof)
  896. return 0;
  897. if (head->read_step == 0)
  898. head->read_step = 1;
  899. list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
  900. struct tomoyo_domain_info *domain;
  901. const char *quota_exceeded = "";
  902. const char *transition_failed = "";
  903. const char *ignore_global_allow_read = "";
  904. domain = list_entry(dpos, struct tomoyo_domain_info, list);
  905. if (head->read_step != 1)
  906. goto acl_loop;
  907. if (domain->is_deleted && !head->read_single_domain)
  908. continue;
  909. /* Print domainname and flags. */
  910. if (domain->quota_warned)
  911. quota_exceeded = "quota_exceeded\n";
  912. if (domain->transition_failed)
  913. transition_failed = "transition_failed\n";
  914. if (domain->ignore_global_allow_read)
  915. ignore_global_allow_read
  916. = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
  917. done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
  918. "%u\n%s%s%s\n",
  919. domain->domainname->name,
  920. domain->profile, quota_exceeded,
  921. transition_failed,
  922. ignore_global_allow_read);
  923. if (!done)
  924. break;
  925. head->read_step = 2;
  926. acl_loop:
  927. if (head->read_step == 3)
  928. goto tail_mark;
  929. /* Print ACL entries in the domain. */
  930. list_for_each_cookie(apos, head->read_var2,
  931. &domain->acl_info_list) {
  932. struct tomoyo_acl_info *ptr
  933. = list_entry(apos, struct tomoyo_acl_info,
  934. list);
  935. done = tomoyo_print_entry(head, ptr);
  936. if (!done)
  937. break;
  938. }
  939. if (!done)
  940. break;
  941. head->read_step = 3;
  942. tail_mark:
  943. done = tomoyo_io_printf(head, "\n");
  944. if (!done)
  945. break;
  946. head->read_step = 1;
  947. if (head->read_single_domain)
  948. break;
  949. }
  950. head->read_eof = done;
  951. return 0;
  952. }
  953. /**
  954. * tomoyo_write_domain_profile - Assign profile for specified domain.
  955. *
  956. * @head: Pointer to "struct tomoyo_io_buffer".
  957. *
  958. * Returns 0 on success, -EINVAL otherwise.
  959. *
  960. * This is equivalent to doing
  961. *
  962. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  963. * /usr/sbin/tomoyo-loadpolicy -d
  964. *
  965. * Caller holds tomoyo_read_lock().
  966. */
  967. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  968. {
  969. char *data = head->write_buf;
  970. char *cp = strchr(data, ' ');
  971. struct tomoyo_domain_info *domain;
  972. unsigned long profile;
  973. if (!cp)
  974. return -EINVAL;
  975. *cp = '\0';
  976. domain = tomoyo_find_domain(cp + 1);
  977. if (strict_strtoul(data, 10, &profile))
  978. return -EINVAL;
  979. if (domain && profile < TOMOYO_MAX_PROFILES
  980. && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
  981. domain->profile = (u8) profile;
  982. return 0;
  983. }
  984. /**
  985. * tomoyo_read_domain_profile - Read only domainname and profile.
  986. *
  987. * @head: Pointer to "struct tomoyo_io_buffer".
  988. *
  989. * Returns list of profile number and domainname pairs.
  990. *
  991. * This is equivalent to doing
  992. *
  993. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  994. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  995. * domainname = $0; } else if ( $1 == "use_profile" ) {
  996. * print $2 " " domainname; domainname = ""; } } ; '
  997. *
  998. * Caller holds tomoyo_read_lock().
  999. */
  1000. static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1001. {
  1002. struct list_head *pos;
  1003. bool done = true;
  1004. if (head->read_eof)
  1005. return 0;
  1006. list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
  1007. struct tomoyo_domain_info *domain;
  1008. domain = list_entry(pos, struct tomoyo_domain_info, list);
  1009. if (domain->is_deleted)
  1010. continue;
  1011. done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
  1012. domain->domainname->name);
  1013. if (!done)
  1014. break;
  1015. }
  1016. head->read_eof = done;
  1017. return 0;
  1018. }
  1019. /**
  1020. * tomoyo_write_pid: Specify PID to obtain domainname.
  1021. *
  1022. * @head: Pointer to "struct tomoyo_io_buffer".
  1023. *
  1024. * Returns 0.
  1025. */
  1026. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1027. {
  1028. unsigned long pid;
  1029. /* No error check. */
  1030. strict_strtoul(head->write_buf, 10, &pid);
  1031. head->read_step = (int) pid;
  1032. head->read_eof = false;
  1033. return 0;
  1034. }
  1035. /**
  1036. * tomoyo_read_pid - Get domainname of the specified PID.
  1037. *
  1038. * @head: Pointer to "struct tomoyo_io_buffer".
  1039. *
  1040. * Returns the domainname which the specified PID is in on success,
  1041. * empty string otherwise.
  1042. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1043. * using read()/write() interface rather than sysctl() interface.
  1044. */
  1045. static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1046. {
  1047. if (head->read_avail == 0 && !head->read_eof) {
  1048. const int pid = head->read_step;
  1049. struct task_struct *p;
  1050. struct tomoyo_domain_info *domain = NULL;
  1051. rcu_read_lock();
  1052. read_lock(&tasklist_lock);
  1053. p = find_task_by_vpid(pid);
  1054. if (p)
  1055. domain = tomoyo_real_domain(p);
  1056. read_unlock(&tasklist_lock);
  1057. rcu_read_unlock();
  1058. if (domain)
  1059. tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
  1060. domain->domainname->name);
  1061. head->read_eof = true;
  1062. }
  1063. return 0;
  1064. }
  1065. /**
  1066. * tomoyo_write_exception_policy - Write exception policy.
  1067. *
  1068. * @head: Pointer to "struct tomoyo_io_buffer".
  1069. *
  1070. * Returns 0 on success, negative value otherwise.
  1071. *
  1072. * Caller holds tomoyo_read_lock().
  1073. */
  1074. static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
  1075. {
  1076. char *data = head->write_buf;
  1077. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1078. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
  1079. return tomoyo_write_domain_keeper_policy(data, false,
  1080. is_delete);
  1081. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
  1082. return tomoyo_write_domain_keeper_policy(data, true, is_delete);
  1083. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
  1084. return tomoyo_write_domain_initializer_policy(data, false,
  1085. is_delete);
  1086. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
  1087. return tomoyo_write_domain_initializer_policy(data, true,
  1088. is_delete);
  1089. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
  1090. return tomoyo_write_alias_policy(data, is_delete);
  1091. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
  1092. return tomoyo_write_globally_readable_policy(data, is_delete);
  1093. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
  1094. return tomoyo_write_pattern_policy(data, is_delete);
  1095. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
  1096. return tomoyo_write_no_rewrite_policy(data, is_delete);
  1097. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
  1098. return tomoyo_write_path_group_policy(data, is_delete);
  1099. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
  1100. return tomoyo_write_number_group_policy(data, is_delete);
  1101. return -EINVAL;
  1102. }
  1103. /**
  1104. * tomoyo_read_exception_policy - Read exception policy.
  1105. *
  1106. * @head: Pointer to "struct tomoyo_io_buffer".
  1107. *
  1108. * Returns 0 on success, -EINVAL otherwise.
  1109. *
  1110. * Caller holds tomoyo_read_lock().
  1111. */
  1112. static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
  1113. {
  1114. if (!head->read_eof) {
  1115. switch (head->read_step) {
  1116. case 0:
  1117. head->read_var2 = NULL;
  1118. head->read_step = 1;
  1119. case 1:
  1120. if (!tomoyo_read_domain_keeper_policy(head))
  1121. break;
  1122. head->read_var2 = NULL;
  1123. head->read_step = 2;
  1124. case 2:
  1125. if (!tomoyo_read_globally_readable_policy(head))
  1126. break;
  1127. head->read_var2 = NULL;
  1128. head->read_step = 3;
  1129. case 3:
  1130. head->read_var2 = NULL;
  1131. head->read_step = 4;
  1132. case 4:
  1133. if (!tomoyo_read_domain_initializer_policy(head))
  1134. break;
  1135. head->read_var2 = NULL;
  1136. head->read_step = 5;
  1137. case 5:
  1138. if (!tomoyo_read_alias_policy(head))
  1139. break;
  1140. head->read_var2 = NULL;
  1141. head->read_step = 6;
  1142. case 6:
  1143. head->read_var2 = NULL;
  1144. head->read_step = 7;
  1145. case 7:
  1146. if (!tomoyo_read_file_pattern(head))
  1147. break;
  1148. head->read_var2 = NULL;
  1149. head->read_step = 8;
  1150. case 8:
  1151. if (!tomoyo_read_no_rewrite_policy(head))
  1152. break;
  1153. head->read_var2 = NULL;
  1154. head->read_step = 9;
  1155. case 9:
  1156. if (!tomoyo_read_path_group_policy(head))
  1157. break;
  1158. head->read_var1 = NULL;
  1159. head->read_var2 = NULL;
  1160. head->read_step = 10;
  1161. case 10:
  1162. if (!tomoyo_read_number_group_policy(head))
  1163. break;
  1164. head->read_var1 = NULL;
  1165. head->read_var2 = NULL;
  1166. head->read_step = 11;
  1167. case 11:
  1168. head->read_eof = true;
  1169. break;
  1170. default:
  1171. return -EINVAL;
  1172. }
  1173. }
  1174. return 0;
  1175. }
  1176. /**
  1177. * tomoyo_print_header - Get header line of audit log.
  1178. *
  1179. * @r: Pointer to "struct tomoyo_request_info".
  1180. *
  1181. * Returns string representation.
  1182. *
  1183. * This function uses kmalloc(), so caller must kfree() if this function
  1184. * didn't return NULL.
  1185. */
  1186. static char *tomoyo_print_header(struct tomoyo_request_info *r)
  1187. {
  1188. static const char *tomoyo_mode_4[4] = {
  1189. "disabled", "learning", "permissive", "enforcing"
  1190. };
  1191. struct timeval tv;
  1192. const pid_t gpid = task_pid_nr(current);
  1193. static const int tomoyo_buffer_len = 4096;
  1194. char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
  1195. if (!buffer)
  1196. return NULL;
  1197. do_gettimeofday(&tv);
  1198. snprintf(buffer, tomoyo_buffer_len - 1,
  1199. "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
  1200. " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
  1201. " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
  1202. tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
  1203. (pid_t) sys_getpid(), (pid_t) sys_getppid(),
  1204. current_uid(), current_gid(), current_euid(),
  1205. current_egid(), current_suid(), current_sgid(),
  1206. current_fsuid(), current_fsgid());
  1207. return buffer;
  1208. }
  1209. /**
  1210. * tomoyo_init_audit_log - Allocate buffer for audit logs.
  1211. *
  1212. * @len: Required size.
  1213. * @r: Pointer to "struct tomoyo_request_info".
  1214. *
  1215. * Returns pointer to allocated memory.
  1216. *
  1217. * The @len is updated to add the header lines' size on success.
  1218. *
  1219. * This function uses kzalloc(), so caller must kfree() if this function
  1220. * didn't return NULL.
  1221. */
  1222. static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
  1223. {
  1224. char *buf = NULL;
  1225. const char *header;
  1226. const char *domainname;
  1227. if (!r->domain)
  1228. r->domain = tomoyo_domain();
  1229. domainname = r->domain->domainname->name;
  1230. header = tomoyo_print_header(r);
  1231. if (!header)
  1232. return NULL;
  1233. *len += strlen(domainname) + strlen(header) + 10;
  1234. buf = kzalloc(*len, GFP_NOFS);
  1235. if (buf)
  1236. snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
  1237. kfree(header);
  1238. return buf;
  1239. }
  1240. /* Wait queue for tomoyo_query_list. */
  1241. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1242. /* Lock for manipulating tomoyo_query_list. */
  1243. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1244. /* Structure for query. */
  1245. struct tomoyo_query_entry {
  1246. struct list_head list;
  1247. char *query;
  1248. int query_len;
  1249. unsigned int serial;
  1250. int timer;
  1251. int answer;
  1252. };
  1253. /* The list for "struct tomoyo_query_entry". */
  1254. static LIST_HEAD(tomoyo_query_list);
  1255. /*
  1256. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1257. * interface.
  1258. */
  1259. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1260. /**
  1261. * tomoyo_supervisor - Ask for the supervisor's decision.
  1262. *
  1263. * @r: Pointer to "struct tomoyo_request_info".
  1264. * @fmt: The printf()'s format string, followed by parameters.
  1265. *
  1266. * Returns 0 if the supervisor decided to permit the access request which
  1267. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1268. * supervisor decided to retry the access request which violated the policy in
  1269. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1270. */
  1271. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1272. {
  1273. va_list args;
  1274. int error = -EPERM;
  1275. int pos;
  1276. int len;
  1277. static unsigned int tomoyo_serial;
  1278. struct tomoyo_query_entry *tomoyo_query_entry = NULL;
  1279. bool quota_exceeded = false;
  1280. char *header;
  1281. switch (r->mode) {
  1282. char *buffer;
  1283. case TOMOYO_CONFIG_LEARNING:
  1284. if (!tomoyo_domain_quota_is_ok(r))
  1285. return 0;
  1286. va_start(args, fmt);
  1287. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
  1288. va_end(args);
  1289. buffer = kmalloc(len, GFP_NOFS);
  1290. if (!buffer)
  1291. return 0;
  1292. va_start(args, fmt);
  1293. vsnprintf(buffer, len - 1, fmt, args);
  1294. va_end(args);
  1295. tomoyo_normalize_line(buffer);
  1296. tomoyo_write_domain_policy2(buffer, r->domain, false);
  1297. kfree(buffer);
  1298. /* fall through */
  1299. case TOMOYO_CONFIG_PERMISSIVE:
  1300. return 0;
  1301. }
  1302. if (!r->domain)
  1303. r->domain = tomoyo_domain();
  1304. if (!atomic_read(&tomoyo_query_observers))
  1305. return -EPERM;
  1306. va_start(args, fmt);
  1307. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
  1308. va_end(args);
  1309. header = tomoyo_init_audit_log(&len, r);
  1310. if (!header)
  1311. goto out;
  1312. tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
  1313. if (!tomoyo_query_entry)
  1314. goto out;
  1315. tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
  1316. if (!tomoyo_query_entry->query)
  1317. goto out;
  1318. len = ksize(tomoyo_query_entry->query);
  1319. INIT_LIST_HEAD(&tomoyo_query_entry->list);
  1320. spin_lock(&tomoyo_query_list_lock);
  1321. if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
  1322. sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
  1323. quota_exceeded = true;
  1324. } else {
  1325. tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
  1326. tomoyo_query_entry->serial = tomoyo_serial++;
  1327. }
  1328. spin_unlock(&tomoyo_query_list_lock);
  1329. if (quota_exceeded)
  1330. goto out;
  1331. pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
  1332. tomoyo_query_entry->serial, r->retry, header);
  1333. kfree(header);
  1334. header = NULL;
  1335. va_start(args, fmt);
  1336. vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
  1337. tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
  1338. va_end(args);
  1339. spin_lock(&tomoyo_query_list_lock);
  1340. list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
  1341. spin_unlock(&tomoyo_query_list_lock);
  1342. /* Give 10 seconds for supervisor's opinion. */
  1343. for (tomoyo_query_entry->timer = 0;
  1344. atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
  1345. tomoyo_query_entry->timer++) {
  1346. wake_up(&tomoyo_query_wait);
  1347. set_current_state(TASK_INTERRUPTIBLE);
  1348. schedule_timeout(HZ / 10);
  1349. if (tomoyo_query_entry->answer)
  1350. break;
  1351. }
  1352. spin_lock(&tomoyo_query_list_lock);
  1353. list_del(&tomoyo_query_entry->list);
  1354. tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
  1355. spin_unlock(&tomoyo_query_list_lock);
  1356. switch (tomoyo_query_entry->answer) {
  1357. case 3: /* Asked to retry by administrator. */
  1358. error = TOMOYO_RETRY_REQUEST;
  1359. r->retry++;
  1360. break;
  1361. case 1:
  1362. /* Granted by administrator. */
  1363. error = 0;
  1364. break;
  1365. case 0:
  1366. /* Timed out. */
  1367. break;
  1368. default:
  1369. /* Rejected by administrator. */
  1370. break;
  1371. }
  1372. out:
  1373. if (tomoyo_query_entry)
  1374. kfree(tomoyo_query_entry->query);
  1375. kfree(tomoyo_query_entry);
  1376. kfree(header);
  1377. return error;
  1378. }
  1379. /**
  1380. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  1381. *
  1382. * @file: Pointer to "struct file".
  1383. * @wait: Pointer to "poll_table".
  1384. *
  1385. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  1386. *
  1387. * Waits for access requests which violated policy in enforcing mode.
  1388. */
  1389. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  1390. {
  1391. struct list_head *tmp;
  1392. bool found = false;
  1393. u8 i;
  1394. for (i = 0; i < 2; i++) {
  1395. spin_lock(&tomoyo_query_list_lock);
  1396. list_for_each(tmp, &tomoyo_query_list) {
  1397. struct tomoyo_query_entry *ptr
  1398. = list_entry(tmp, struct tomoyo_query_entry,
  1399. list);
  1400. if (ptr->answer)
  1401. continue;
  1402. found = true;
  1403. break;
  1404. }
  1405. spin_unlock(&tomoyo_query_list_lock);
  1406. if (found)
  1407. return POLLIN | POLLRDNORM;
  1408. if (i)
  1409. break;
  1410. poll_wait(file, &tomoyo_query_wait, wait);
  1411. }
  1412. return 0;
  1413. }
  1414. /**
  1415. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  1416. *
  1417. * @head: Pointer to "struct tomoyo_io_buffer".
  1418. *
  1419. * Returns 0.
  1420. */
  1421. static int tomoyo_read_query(struct tomoyo_io_buffer *head)
  1422. {
  1423. struct list_head *tmp;
  1424. int pos = 0;
  1425. int len = 0;
  1426. char *buf;
  1427. if (head->read_avail)
  1428. return 0;
  1429. if (head->read_buf) {
  1430. kfree(head->read_buf);
  1431. head->read_buf = NULL;
  1432. head->readbuf_size = 0;
  1433. }
  1434. spin_lock(&tomoyo_query_list_lock);
  1435. list_for_each(tmp, &tomoyo_query_list) {
  1436. struct tomoyo_query_entry *ptr
  1437. = list_entry(tmp, struct tomoyo_query_entry, list);
  1438. if (ptr->answer)
  1439. continue;
  1440. if (pos++ != head->read_step)
  1441. continue;
  1442. len = ptr->query_len;
  1443. break;
  1444. }
  1445. spin_unlock(&tomoyo_query_list_lock);
  1446. if (!len) {
  1447. head->read_step = 0;
  1448. return 0;
  1449. }
  1450. buf = kzalloc(len, GFP_NOFS);
  1451. if (!buf)
  1452. return 0;
  1453. pos = 0;
  1454. spin_lock(&tomoyo_query_list_lock);
  1455. list_for_each(tmp, &tomoyo_query_list) {
  1456. struct tomoyo_query_entry *ptr
  1457. = list_entry(tmp, struct tomoyo_query_entry, list);
  1458. if (ptr->answer)
  1459. continue;
  1460. if (pos++ != head->read_step)
  1461. continue;
  1462. /*
  1463. * Some query can be skipped because tomoyo_query_list
  1464. * can change, but I don't care.
  1465. */
  1466. if (len == ptr->query_len)
  1467. memmove(buf, ptr->query, len);
  1468. break;
  1469. }
  1470. spin_unlock(&tomoyo_query_list_lock);
  1471. if (buf[0]) {
  1472. head->read_avail = len;
  1473. head->readbuf_size = head->read_avail;
  1474. head->read_buf = buf;
  1475. head->read_step++;
  1476. } else {
  1477. kfree(buf);
  1478. }
  1479. return 0;
  1480. }
  1481. /**
  1482. * tomoyo_write_answer - Write the supervisor's decision.
  1483. *
  1484. * @head: Pointer to "struct tomoyo_io_buffer".
  1485. *
  1486. * Returns 0 on success, -EINVAL otherwise.
  1487. */
  1488. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  1489. {
  1490. char *data = head->write_buf;
  1491. struct list_head *tmp;
  1492. unsigned int serial;
  1493. unsigned int answer;
  1494. spin_lock(&tomoyo_query_list_lock);
  1495. list_for_each(tmp, &tomoyo_query_list) {
  1496. struct tomoyo_query_entry *ptr
  1497. = list_entry(tmp, struct tomoyo_query_entry, list);
  1498. ptr->timer = 0;
  1499. }
  1500. spin_unlock(&tomoyo_query_list_lock);
  1501. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  1502. return -EINVAL;
  1503. spin_lock(&tomoyo_query_list_lock);
  1504. list_for_each(tmp, &tomoyo_query_list) {
  1505. struct tomoyo_query_entry *ptr
  1506. = list_entry(tmp, struct tomoyo_query_entry, list);
  1507. if (ptr->serial != serial)
  1508. continue;
  1509. if (!ptr->answer)
  1510. ptr->answer = answer;
  1511. break;
  1512. }
  1513. spin_unlock(&tomoyo_query_list_lock);
  1514. return 0;
  1515. }
  1516. /**
  1517. * tomoyo_read_version: Get version.
  1518. *
  1519. * @head: Pointer to "struct tomoyo_io_buffer".
  1520. *
  1521. * Returns version information.
  1522. */
  1523. static int tomoyo_read_version(struct tomoyo_io_buffer *head)
  1524. {
  1525. if (!head->read_eof) {
  1526. tomoyo_io_printf(head, "2.2.0");
  1527. head->read_eof = true;
  1528. }
  1529. return 0;
  1530. }
  1531. /**
  1532. * tomoyo_read_self_domain - Get the current process's domainname.
  1533. *
  1534. * @head: Pointer to "struct tomoyo_io_buffer".
  1535. *
  1536. * Returns the current process's domainname.
  1537. */
  1538. static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1539. {
  1540. if (!head->read_eof) {
  1541. /*
  1542. * tomoyo_domain()->domainname != NULL
  1543. * because every process belongs to a domain and
  1544. * the domain's name cannot be NULL.
  1545. */
  1546. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1547. head->read_eof = true;
  1548. }
  1549. return 0;
  1550. }
  1551. /**
  1552. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1553. *
  1554. * @type: Type of interface.
  1555. * @file: Pointer to "struct file".
  1556. *
  1557. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1558. *
  1559. * Caller acquires tomoyo_read_lock().
  1560. */
  1561. int tomoyo_open_control(const u8 type, struct file *file)
  1562. {
  1563. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  1564. if (!head)
  1565. return -ENOMEM;
  1566. mutex_init(&head->io_sem);
  1567. head->type = type;
  1568. switch (type) {
  1569. case TOMOYO_DOMAINPOLICY:
  1570. /* /sys/kernel/security/tomoyo/domain_policy */
  1571. head->write = tomoyo_write_domain_policy;
  1572. head->read = tomoyo_read_domain_policy;
  1573. break;
  1574. case TOMOYO_EXCEPTIONPOLICY:
  1575. /* /sys/kernel/security/tomoyo/exception_policy */
  1576. head->write = tomoyo_write_exception_policy;
  1577. head->read = tomoyo_read_exception_policy;
  1578. break;
  1579. case TOMOYO_SELFDOMAIN:
  1580. /* /sys/kernel/security/tomoyo/self_domain */
  1581. head->read = tomoyo_read_self_domain;
  1582. break;
  1583. case TOMOYO_DOMAIN_STATUS:
  1584. /* /sys/kernel/security/tomoyo/.domain_status */
  1585. head->write = tomoyo_write_domain_profile;
  1586. head->read = tomoyo_read_domain_profile;
  1587. break;
  1588. case TOMOYO_PROCESS_STATUS:
  1589. /* /sys/kernel/security/tomoyo/.process_status */
  1590. head->write = tomoyo_write_pid;
  1591. head->read = tomoyo_read_pid;
  1592. break;
  1593. case TOMOYO_VERSION:
  1594. /* /sys/kernel/security/tomoyo/version */
  1595. head->read = tomoyo_read_version;
  1596. head->readbuf_size = 128;
  1597. break;
  1598. case TOMOYO_MEMINFO:
  1599. /* /sys/kernel/security/tomoyo/meminfo */
  1600. head->write = tomoyo_write_memory_quota;
  1601. head->read = tomoyo_read_memory_counter;
  1602. head->readbuf_size = 512;
  1603. break;
  1604. case TOMOYO_PROFILE:
  1605. /* /sys/kernel/security/tomoyo/profile */
  1606. head->write = tomoyo_write_profile;
  1607. head->read = tomoyo_read_profile;
  1608. break;
  1609. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  1610. head->poll = tomoyo_poll_query;
  1611. head->write = tomoyo_write_answer;
  1612. head->read = tomoyo_read_query;
  1613. break;
  1614. case TOMOYO_MANAGER:
  1615. /* /sys/kernel/security/tomoyo/manager */
  1616. head->write = tomoyo_write_manager_policy;
  1617. head->read = tomoyo_read_manager_policy;
  1618. break;
  1619. }
  1620. if (!(file->f_mode & FMODE_READ)) {
  1621. /*
  1622. * No need to allocate read_buf since it is not opened
  1623. * for reading.
  1624. */
  1625. head->read = NULL;
  1626. head->poll = NULL;
  1627. } else if (!head->poll) {
  1628. /* Don't allocate read_buf for poll() access. */
  1629. if (!head->readbuf_size)
  1630. head->readbuf_size = 4096 * 2;
  1631. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  1632. if (!head->read_buf) {
  1633. kfree(head);
  1634. return -ENOMEM;
  1635. }
  1636. }
  1637. if (!(file->f_mode & FMODE_WRITE)) {
  1638. /*
  1639. * No need to allocate write_buf since it is not opened
  1640. * for writing.
  1641. */
  1642. head->write = NULL;
  1643. } else if (head->write) {
  1644. head->writebuf_size = 4096 * 2;
  1645. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  1646. if (!head->write_buf) {
  1647. kfree(head->read_buf);
  1648. kfree(head);
  1649. return -ENOMEM;
  1650. }
  1651. }
  1652. if (type != TOMOYO_QUERY)
  1653. head->reader_idx = tomoyo_read_lock();
  1654. file->private_data = head;
  1655. /*
  1656. * Call the handler now if the file is
  1657. * /sys/kernel/security/tomoyo/self_domain
  1658. * so that the user can use
  1659. * cat < /sys/kernel/security/tomoyo/self_domain"
  1660. * to know the current process's domainname.
  1661. */
  1662. if (type == TOMOYO_SELFDOMAIN)
  1663. tomoyo_read_control(file, NULL, 0);
  1664. /*
  1665. * If the file is /sys/kernel/security/tomoyo/query , increment the
  1666. * observer counter.
  1667. * The obserber counter is used by tomoyo_supervisor() to see if
  1668. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  1669. */
  1670. else if (type == TOMOYO_QUERY)
  1671. atomic_inc(&tomoyo_query_observers);
  1672. return 0;
  1673. }
  1674. /**
  1675. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  1676. *
  1677. * @file: Pointer to "struct file".
  1678. * @wait: Pointer to "poll_table".
  1679. *
  1680. * Waits for read readiness.
  1681. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
  1682. */
  1683. int tomoyo_poll_control(struct file *file, poll_table *wait)
  1684. {
  1685. struct tomoyo_io_buffer *head = file->private_data;
  1686. if (!head->poll)
  1687. return -ENOSYS;
  1688. return head->poll(file, wait);
  1689. }
  1690. /**
  1691. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1692. *
  1693. * @file: Pointer to "struct file".
  1694. * @buffer: Poiner to buffer to write to.
  1695. * @buffer_len: Size of @buffer.
  1696. *
  1697. * Returns bytes read on success, negative value otherwise.
  1698. *
  1699. * Caller holds tomoyo_read_lock().
  1700. */
  1701. int tomoyo_read_control(struct file *file, char __user *buffer,
  1702. const int buffer_len)
  1703. {
  1704. int len = 0;
  1705. struct tomoyo_io_buffer *head = file->private_data;
  1706. char *cp;
  1707. if (!head->read)
  1708. return -ENOSYS;
  1709. if (mutex_lock_interruptible(&head->io_sem))
  1710. return -EINTR;
  1711. /* Call the policy handler. */
  1712. len = head->read(head);
  1713. if (len < 0)
  1714. goto out;
  1715. /* Write to buffer. */
  1716. len = head->read_avail;
  1717. if (len > buffer_len)
  1718. len = buffer_len;
  1719. if (!len)
  1720. goto out;
  1721. /* head->read_buf changes by some functions. */
  1722. cp = head->read_buf;
  1723. if (copy_to_user(buffer, cp, len)) {
  1724. len = -EFAULT;
  1725. goto out;
  1726. }
  1727. head->read_avail -= len;
  1728. memmove(cp, cp + len, head->read_avail);
  1729. out:
  1730. mutex_unlock(&head->io_sem);
  1731. return len;
  1732. }
  1733. /**
  1734. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1735. *
  1736. * @file: Pointer to "struct file".
  1737. * @buffer: Pointer to buffer to read from.
  1738. * @buffer_len: Size of @buffer.
  1739. *
  1740. * Returns @buffer_len on success, negative value otherwise.
  1741. *
  1742. * Caller holds tomoyo_read_lock().
  1743. */
  1744. int tomoyo_write_control(struct file *file, const char __user *buffer,
  1745. const int buffer_len)
  1746. {
  1747. struct tomoyo_io_buffer *head = file->private_data;
  1748. int error = buffer_len;
  1749. int avail_len = buffer_len;
  1750. char *cp0 = head->write_buf;
  1751. if (!head->write)
  1752. return -ENOSYS;
  1753. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1754. return -EFAULT;
  1755. /* Don't allow updating policies by non manager programs. */
  1756. if (head->write != tomoyo_write_pid &&
  1757. head->write != tomoyo_write_domain_policy &&
  1758. !tomoyo_is_policy_manager())
  1759. return -EPERM;
  1760. if (mutex_lock_interruptible(&head->io_sem))
  1761. return -EINTR;
  1762. /* Read a line and dispatch it to the policy handler. */
  1763. while (avail_len > 0) {
  1764. char c;
  1765. if (head->write_avail >= head->writebuf_size - 1) {
  1766. error = -ENOMEM;
  1767. break;
  1768. } else if (get_user(c, buffer)) {
  1769. error = -EFAULT;
  1770. break;
  1771. }
  1772. buffer++;
  1773. avail_len--;
  1774. cp0[head->write_avail++] = c;
  1775. if (c != '\n')
  1776. continue;
  1777. cp0[head->write_avail - 1] = '\0';
  1778. head->write_avail = 0;
  1779. tomoyo_normalize_line(cp0);
  1780. head->write(head);
  1781. }
  1782. mutex_unlock(&head->io_sem);
  1783. return error;
  1784. }
  1785. /**
  1786. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  1787. *
  1788. * @file: Pointer to "struct file".
  1789. *
  1790. * Releases memory and returns 0.
  1791. *
  1792. * Caller looses tomoyo_read_lock().
  1793. */
  1794. int tomoyo_close_control(struct file *file)
  1795. {
  1796. struct tomoyo_io_buffer *head = file->private_data;
  1797. const bool is_write = !!head->write_buf;
  1798. /*
  1799. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  1800. * observer counter.
  1801. */
  1802. if (head->type == TOMOYO_QUERY)
  1803. atomic_dec(&tomoyo_query_observers);
  1804. else
  1805. tomoyo_read_unlock(head->reader_idx);
  1806. /* Release memory used for policy I/O. */
  1807. kfree(head->read_buf);
  1808. head->read_buf = NULL;
  1809. kfree(head->write_buf);
  1810. head->write_buf = NULL;
  1811. kfree(head);
  1812. head = NULL;
  1813. file->private_data = NULL;
  1814. if (is_write)
  1815. tomoyo_run_gc();
  1816. return 0;
  1817. }
  1818. /**
  1819. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  1820. */
  1821. void tomoyo_check_profile(void)
  1822. {
  1823. struct tomoyo_domain_info *domain;
  1824. const int idx = tomoyo_read_lock();
  1825. tomoyo_policy_loaded = true;
  1826. /* Check all profiles currently assigned to domains are defined. */
  1827. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1828. const u8 profile = domain->profile;
  1829. if (tomoyo_profile_ptr[profile])
  1830. continue;
  1831. panic("Profile %u (used by '%s') not defined.\n",
  1832. profile, domain->domainname->name);
  1833. }
  1834. tomoyo_read_unlock(idx);
  1835. printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
  1836. printk(KERN_INFO "Mandatory Access Control activated.\n");
  1837. }