common.c 54 KB

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