common.c 54 KB

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