common.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  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. /* String table for operation mode. */
  13. const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE] = {
  14. [TOMOYO_CONFIG_DISABLED] = "disabled",
  15. [TOMOYO_CONFIG_LEARNING] = "learning",
  16. [TOMOYO_CONFIG_PERMISSIVE] = "permissive",
  17. [TOMOYO_CONFIG_ENFORCING] = "enforcing"
  18. };
  19. /* String table for /sys/kernel/security/tomoyo/profile */
  20. const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
  21. + TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
  22. [TOMOYO_MAC_FILE_EXECUTE] = "execute",
  23. [TOMOYO_MAC_FILE_OPEN] = "open",
  24. [TOMOYO_MAC_FILE_CREATE] = "create",
  25. [TOMOYO_MAC_FILE_UNLINK] = "unlink",
  26. [TOMOYO_MAC_FILE_GETATTR] = "getattr",
  27. [TOMOYO_MAC_FILE_MKDIR] = "mkdir",
  28. [TOMOYO_MAC_FILE_RMDIR] = "rmdir",
  29. [TOMOYO_MAC_FILE_MKFIFO] = "mkfifo",
  30. [TOMOYO_MAC_FILE_MKSOCK] = "mksock",
  31. [TOMOYO_MAC_FILE_TRUNCATE] = "truncate",
  32. [TOMOYO_MAC_FILE_SYMLINK] = "symlink",
  33. [TOMOYO_MAC_FILE_MKBLOCK] = "mkblock",
  34. [TOMOYO_MAC_FILE_MKCHAR] = "mkchar",
  35. [TOMOYO_MAC_FILE_LINK] = "link",
  36. [TOMOYO_MAC_FILE_RENAME] = "rename",
  37. [TOMOYO_MAC_FILE_CHMOD] = "chmod",
  38. [TOMOYO_MAC_FILE_CHOWN] = "chown",
  39. [TOMOYO_MAC_FILE_CHGRP] = "chgrp",
  40. [TOMOYO_MAC_FILE_IOCTL] = "ioctl",
  41. [TOMOYO_MAC_FILE_CHROOT] = "chroot",
  42. [TOMOYO_MAC_FILE_MOUNT] = "mount",
  43. [TOMOYO_MAC_FILE_UMOUNT] = "unmount",
  44. [TOMOYO_MAC_FILE_PIVOT_ROOT] = "pivot_root",
  45. [TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
  46. };
  47. /* String table for PREFERENCE keyword. */
  48. static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
  49. [TOMOYO_PREF_MAX_AUDIT_LOG] = "max_audit_log",
  50. [TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
  51. };
  52. /* String table for path operation. */
  53. const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
  54. [TOMOYO_TYPE_EXECUTE] = "execute",
  55. [TOMOYO_TYPE_READ] = "read",
  56. [TOMOYO_TYPE_WRITE] = "write",
  57. [TOMOYO_TYPE_APPEND] = "append",
  58. [TOMOYO_TYPE_UNLINK] = "unlink",
  59. [TOMOYO_TYPE_GETATTR] = "getattr",
  60. [TOMOYO_TYPE_RMDIR] = "rmdir",
  61. [TOMOYO_TYPE_TRUNCATE] = "truncate",
  62. [TOMOYO_TYPE_SYMLINK] = "symlink",
  63. [TOMOYO_TYPE_CHROOT] = "chroot",
  64. [TOMOYO_TYPE_UMOUNT] = "unmount",
  65. };
  66. /* String table for categories. */
  67. static const char * const tomoyo_category_keywords
  68. [TOMOYO_MAX_MAC_CATEGORY_INDEX] = {
  69. [TOMOYO_MAC_CATEGORY_FILE] = "file",
  70. };
  71. /* Permit policy management by non-root user? */
  72. static bool tomoyo_manage_by_non_root;
  73. /* Utility functions. */
  74. /**
  75. * tomoyo_yesno - Return "yes" or "no".
  76. *
  77. * @value: Bool value.
  78. */
  79. const char *tomoyo_yesno(const unsigned int value)
  80. {
  81. return value ? "yes" : "no";
  82. }
  83. /**
  84. * tomoyo_addprintf - strncat()-like-snprintf().
  85. *
  86. * @buffer: Buffer to write to. Must be '\0'-terminated.
  87. * @len: Size of @buffer.
  88. * @fmt: The printf()'s format string, followed by parameters.
  89. *
  90. * Returns nothing.
  91. */
  92. static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
  93. {
  94. va_list args;
  95. const int pos = strlen(buffer);
  96. va_start(args, fmt);
  97. vsnprintf(buffer + pos, len - pos - 1, fmt, args);
  98. va_end(args);
  99. }
  100. /**
  101. * tomoyo_flush - Flush queued string to userspace's buffer.
  102. *
  103. * @head: Pointer to "struct tomoyo_io_buffer".
  104. *
  105. * Returns true if all data was flushed, false otherwise.
  106. */
  107. static bool tomoyo_flush(struct tomoyo_io_buffer *head)
  108. {
  109. while (head->r.w_pos) {
  110. const char *w = head->r.w[0];
  111. size_t len = strlen(w);
  112. if (len) {
  113. if (len > head->read_user_buf_avail)
  114. len = head->read_user_buf_avail;
  115. if (!len)
  116. return false;
  117. if (copy_to_user(head->read_user_buf, w, len))
  118. return false;
  119. head->read_user_buf_avail -= len;
  120. head->read_user_buf += len;
  121. w += len;
  122. }
  123. head->r.w[0] = w;
  124. if (*w)
  125. return false;
  126. /* Add '\0' for audit logs and query. */
  127. if (head->poll) {
  128. if (!head->read_user_buf_avail ||
  129. copy_to_user(head->read_user_buf, "", 1))
  130. return false;
  131. head->read_user_buf_avail--;
  132. head->read_user_buf++;
  133. }
  134. head->r.w_pos--;
  135. for (len = 0; len < head->r.w_pos; len++)
  136. head->r.w[len] = head->r.w[len + 1];
  137. }
  138. head->r.avail = 0;
  139. return true;
  140. }
  141. /**
  142. * tomoyo_set_string - Queue string to "struct tomoyo_io_buffer" structure.
  143. *
  144. * @head: Pointer to "struct tomoyo_io_buffer".
  145. * @string: String to print.
  146. *
  147. * Note that @string has to be kept valid until @head is kfree()d.
  148. * This means that char[] allocated on stack memory cannot be passed to
  149. * this function. Use tomoyo_io_printf() for char[] allocated on stack memory.
  150. */
  151. static void tomoyo_set_string(struct tomoyo_io_buffer *head, const char *string)
  152. {
  153. if (head->r.w_pos < TOMOYO_MAX_IO_READ_QUEUE) {
  154. head->r.w[head->r.w_pos++] = string;
  155. tomoyo_flush(head);
  156. } else
  157. WARN_ON(1);
  158. }
  159. /**
  160. * tomoyo_io_printf - printf() to "struct tomoyo_io_buffer" structure.
  161. *
  162. * @head: Pointer to "struct tomoyo_io_buffer".
  163. * @fmt: The printf()'s format string, followed by parameters.
  164. */
  165. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  166. {
  167. va_list args;
  168. size_t len;
  169. size_t pos = head->r.avail;
  170. int size = head->readbuf_size - pos;
  171. if (size <= 0)
  172. return;
  173. va_start(args, fmt);
  174. len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
  175. va_end(args);
  176. if (pos + len >= head->readbuf_size) {
  177. WARN_ON(1);
  178. return;
  179. }
  180. head->r.avail += len;
  181. tomoyo_set_string(head, head->read_buf + pos);
  182. }
  183. /**
  184. * tomoyo_set_space - Put a space 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_space(struct tomoyo_io_buffer *head)
  191. {
  192. tomoyo_set_string(head, " ");
  193. }
  194. /**
  195. * tomoyo_set_lf - Put a line feed to "struct tomoyo_io_buffer" structure.
  196. *
  197. * @head: Pointer to "struct tomoyo_io_buffer".
  198. *
  199. * Returns nothing.
  200. */
  201. static bool tomoyo_set_lf(struct tomoyo_io_buffer *head)
  202. {
  203. tomoyo_set_string(head, "\n");
  204. return !head->r.w_pos;
  205. }
  206. /**
  207. * tomoyo_set_slash - Put a shash to "struct tomoyo_io_buffer" structure.
  208. *
  209. * @head: Pointer to "struct tomoyo_io_buffer".
  210. *
  211. * Returns nothing.
  212. */
  213. static void tomoyo_set_slash(struct tomoyo_io_buffer *head)
  214. {
  215. tomoyo_set_string(head, "/");
  216. }
  217. /* List of namespaces. */
  218. LIST_HEAD(tomoyo_namespace_list);
  219. /* True if namespace other than tomoyo_kernel_namespace is defined. */
  220. static bool tomoyo_namespace_enabled;
  221. /**
  222. * tomoyo_init_policy_namespace - Initialize namespace.
  223. *
  224. * @ns: Pointer to "struct tomoyo_policy_namespace".
  225. *
  226. * Returns nothing.
  227. */
  228. void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
  229. {
  230. unsigned int idx;
  231. for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
  232. INIT_LIST_HEAD(&ns->acl_group[idx]);
  233. for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
  234. INIT_LIST_HEAD(&ns->group_list[idx]);
  235. for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
  236. INIT_LIST_HEAD(&ns->policy_list[idx]);
  237. ns->profile_version = 20100903;
  238. tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
  239. list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
  240. }
  241. /**
  242. * tomoyo_print_namespace - Print namespace header.
  243. *
  244. * @head: Pointer to "struct tomoyo_io_buffer".
  245. *
  246. * Returns nothing.
  247. */
  248. static void tomoyo_print_namespace(struct tomoyo_io_buffer *head)
  249. {
  250. if (!tomoyo_namespace_enabled)
  251. return;
  252. tomoyo_set_string(head,
  253. container_of(head->r.ns,
  254. struct tomoyo_policy_namespace,
  255. namespace_list)->name);
  256. tomoyo_set_space(head);
  257. }
  258. /**
  259. * tomoyo_print_name_union - Print a tomoyo_name_union.
  260. *
  261. * @head: Pointer to "struct tomoyo_io_buffer".
  262. * @ptr: Pointer to "struct tomoyo_name_union".
  263. */
  264. static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
  265. const struct tomoyo_name_union *ptr)
  266. {
  267. tomoyo_set_space(head);
  268. if (ptr->group) {
  269. tomoyo_set_string(head, "@");
  270. tomoyo_set_string(head, ptr->group->group_name->name);
  271. } else {
  272. tomoyo_set_string(head, ptr->filename->name);
  273. }
  274. }
  275. /**
  276. * tomoyo_print_number_union - Print a tomoyo_number_union.
  277. *
  278. * @head: Pointer to "struct tomoyo_io_buffer".
  279. * @ptr: Pointer to "struct tomoyo_number_union".
  280. */
  281. static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
  282. const struct tomoyo_number_union *ptr)
  283. {
  284. tomoyo_set_space(head);
  285. if (ptr->group) {
  286. tomoyo_set_string(head, "@");
  287. tomoyo_set_string(head, ptr->group->group_name->name);
  288. } else {
  289. int i;
  290. unsigned long min = ptr->values[0];
  291. const unsigned long max = ptr->values[1];
  292. u8 min_type = ptr->value_type[0];
  293. const u8 max_type = ptr->value_type[1];
  294. char buffer[128];
  295. buffer[0] = '\0';
  296. for (i = 0; i < 2; i++) {
  297. switch (min_type) {
  298. case TOMOYO_VALUE_TYPE_HEXADECIMAL:
  299. tomoyo_addprintf(buffer, sizeof(buffer),
  300. "0x%lX", min);
  301. break;
  302. case TOMOYO_VALUE_TYPE_OCTAL:
  303. tomoyo_addprintf(buffer, sizeof(buffer),
  304. "0%lo", min);
  305. break;
  306. default:
  307. tomoyo_addprintf(buffer, sizeof(buffer),
  308. "%lu", min);
  309. break;
  310. }
  311. if (min == max && min_type == max_type)
  312. break;
  313. tomoyo_addprintf(buffer, sizeof(buffer), "-");
  314. min_type = max_type;
  315. min = max;
  316. }
  317. tomoyo_io_printf(head, "%s", buffer);
  318. }
  319. }
  320. /**
  321. * tomoyo_assign_profile - Create a new profile.
  322. *
  323. * @ns: Pointer to "struct tomoyo_policy_namespace".
  324. * @profile: Profile number to create.
  325. *
  326. * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
  327. */
  328. static struct tomoyo_profile *tomoyo_assign_profile
  329. (struct tomoyo_policy_namespace *ns, const unsigned int profile)
  330. {
  331. struct tomoyo_profile *ptr;
  332. struct tomoyo_profile *entry;
  333. if (profile >= TOMOYO_MAX_PROFILES)
  334. return NULL;
  335. ptr = ns->profile_ptr[profile];
  336. if (ptr)
  337. return ptr;
  338. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  339. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  340. goto out;
  341. ptr = ns->profile_ptr[profile];
  342. if (!ptr && tomoyo_memory_ok(entry)) {
  343. ptr = entry;
  344. ptr->default_config = TOMOYO_CONFIG_DISABLED |
  345. TOMOYO_CONFIG_WANT_GRANT_LOG |
  346. TOMOYO_CONFIG_WANT_REJECT_LOG;
  347. memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
  348. sizeof(ptr->config));
  349. ptr->pref[TOMOYO_PREF_MAX_AUDIT_LOG] = 1024;
  350. ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
  351. mb(); /* Avoid out-of-order execution. */
  352. ns->profile_ptr[profile] = ptr;
  353. entry = NULL;
  354. }
  355. mutex_unlock(&tomoyo_policy_lock);
  356. out:
  357. kfree(entry);
  358. return ptr;
  359. }
  360. /**
  361. * tomoyo_profile - Find a profile.
  362. *
  363. * @ns: Pointer to "struct tomoyo_policy_namespace".
  364. * @profile: Profile number to find.
  365. *
  366. * Returns pointer to "struct tomoyo_profile".
  367. */
  368. struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
  369. const u8 profile)
  370. {
  371. static struct tomoyo_profile tomoyo_null_profile;
  372. struct tomoyo_profile *ptr = ns->profile_ptr[profile];
  373. if (!ptr)
  374. ptr = &tomoyo_null_profile;
  375. return ptr;
  376. }
  377. /**
  378. * tomoyo_find_yesno - Find values for specified keyword.
  379. *
  380. * @string: String to check.
  381. * @find: Name of keyword.
  382. *
  383. * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
  384. */
  385. static s8 tomoyo_find_yesno(const char *string, const char *find)
  386. {
  387. const char *cp = strstr(string, find);
  388. if (cp) {
  389. cp += strlen(find);
  390. if (!strncmp(cp, "=yes", 4))
  391. return 1;
  392. else if (!strncmp(cp, "=no", 3))
  393. return 0;
  394. }
  395. return -1;
  396. }
  397. /**
  398. * tomoyo_set_uint - Set value for specified preference.
  399. *
  400. * @i: Pointer to "unsigned int".
  401. * @string: String to check.
  402. * @find: Name of keyword.
  403. *
  404. * Returns nothing.
  405. */
  406. static void tomoyo_set_uint(unsigned int *i, const char *string,
  407. const char *find)
  408. {
  409. const char *cp = strstr(string, find);
  410. if (cp)
  411. sscanf(cp + strlen(find), "=%u", i);
  412. }
  413. /**
  414. * tomoyo_set_mode - Set mode for specified profile.
  415. *
  416. * @name: Name of functionality.
  417. * @value: Mode for @name.
  418. * @profile: Pointer to "struct tomoyo_profile".
  419. *
  420. * Returns 0 on success, negative value otherwise.
  421. */
  422. static int tomoyo_set_mode(char *name, const char *value,
  423. struct tomoyo_profile *profile)
  424. {
  425. u8 i;
  426. u8 config;
  427. if (!strcmp(name, "CONFIG")) {
  428. i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
  429. config = profile->default_config;
  430. } else if (tomoyo_str_starts(&name, "CONFIG::")) {
  431. config = 0;
  432. for (i = 0; i < TOMOYO_MAX_MAC_INDEX
  433. + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
  434. int len = 0;
  435. if (i < TOMOYO_MAX_MAC_INDEX) {
  436. const u8 c = tomoyo_index2category[i];
  437. const char *category =
  438. tomoyo_category_keywords[c];
  439. len = strlen(category);
  440. if (strncmp(name, category, len) ||
  441. name[len++] != ':' || name[len++] != ':')
  442. continue;
  443. }
  444. if (strcmp(name + len, tomoyo_mac_keywords[i]))
  445. continue;
  446. config = profile->config[i];
  447. break;
  448. }
  449. if (i == TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
  450. return -EINVAL;
  451. } else {
  452. return -EINVAL;
  453. }
  454. if (strstr(value, "use_default")) {
  455. config = TOMOYO_CONFIG_USE_DEFAULT;
  456. } else {
  457. u8 mode;
  458. for (mode = 0; mode < 4; mode++)
  459. if (strstr(value, tomoyo_mode[mode]))
  460. /*
  461. * Update lower 3 bits in order to distinguish
  462. * 'config' from 'TOMOYO_CONFIG_USE_DEAFULT'.
  463. */
  464. config = (config & ~7) | mode;
  465. if (config != TOMOYO_CONFIG_USE_DEFAULT) {
  466. switch (tomoyo_find_yesno(value, "grant_log")) {
  467. case 1:
  468. config |= TOMOYO_CONFIG_WANT_GRANT_LOG;
  469. break;
  470. case 0:
  471. config &= ~TOMOYO_CONFIG_WANT_GRANT_LOG;
  472. break;
  473. }
  474. switch (tomoyo_find_yesno(value, "reject_log")) {
  475. case 1:
  476. config |= TOMOYO_CONFIG_WANT_REJECT_LOG;
  477. break;
  478. case 0:
  479. config &= ~TOMOYO_CONFIG_WANT_REJECT_LOG;
  480. break;
  481. }
  482. }
  483. }
  484. if (i < TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX)
  485. profile->config[i] = config;
  486. else if (config != TOMOYO_CONFIG_USE_DEFAULT)
  487. profile->default_config = config;
  488. return 0;
  489. }
  490. /**
  491. * tomoyo_write_profile - Write profile table.
  492. *
  493. * @head: Pointer to "struct tomoyo_io_buffer".
  494. *
  495. * Returns 0 on success, negative value otherwise.
  496. */
  497. static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
  498. {
  499. char *data = head->write_buf;
  500. unsigned int i;
  501. char *cp;
  502. struct tomoyo_profile *profile;
  503. if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
  504. == 1)
  505. return 0;
  506. i = simple_strtoul(data, &cp, 10);
  507. if (*cp != '-')
  508. return -EINVAL;
  509. data = cp + 1;
  510. profile = tomoyo_assign_profile(head->w.ns, i);
  511. if (!profile)
  512. return -EINVAL;
  513. cp = strchr(data, '=');
  514. if (!cp)
  515. return -EINVAL;
  516. *cp++ = '\0';
  517. if (!strcmp(data, "COMMENT")) {
  518. static DEFINE_SPINLOCK(lock);
  519. const struct tomoyo_path_info *new_comment
  520. = tomoyo_get_name(cp);
  521. const struct tomoyo_path_info *old_comment;
  522. if (!new_comment)
  523. return -ENOMEM;
  524. spin_lock(&lock);
  525. old_comment = profile->comment;
  526. profile->comment = new_comment;
  527. spin_unlock(&lock);
  528. tomoyo_put_name(old_comment);
  529. return 0;
  530. }
  531. if (!strcmp(data, "PREFERENCE")) {
  532. for (i = 0; i < TOMOYO_MAX_PREF; i++)
  533. tomoyo_set_uint(&profile->pref[i], cp,
  534. tomoyo_pref_keywords[i]);
  535. return 0;
  536. }
  537. return tomoyo_set_mode(data, cp, profile);
  538. }
  539. /**
  540. * tomoyo_print_config - Print mode for specified functionality.
  541. *
  542. * @head: Pointer to "struct tomoyo_io_buffer".
  543. * @config: Mode for that functionality.
  544. *
  545. * Returns nothing.
  546. *
  547. * Caller prints functionality's name.
  548. */
  549. static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
  550. {
  551. tomoyo_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
  552. tomoyo_mode[config & 3],
  553. tomoyo_yesno(config & TOMOYO_CONFIG_WANT_GRANT_LOG),
  554. tomoyo_yesno(config & TOMOYO_CONFIG_WANT_REJECT_LOG));
  555. }
  556. /**
  557. * tomoyo_read_profile - Read profile table.
  558. *
  559. * @head: Pointer to "struct tomoyo_io_buffer".
  560. *
  561. * Returns nothing.
  562. */
  563. static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
  564. {
  565. u8 index;
  566. struct tomoyo_policy_namespace *ns =
  567. container_of(head->r.ns, typeof(*ns), namespace_list);
  568. const struct tomoyo_profile *profile;
  569. if (head->r.eof)
  570. return;
  571. next:
  572. index = head->r.index;
  573. profile = ns->profile_ptr[index];
  574. switch (head->r.step) {
  575. case 0:
  576. tomoyo_print_namespace(head);
  577. tomoyo_io_printf(head, "PROFILE_VERSION=%u\n",
  578. ns->profile_version);
  579. head->r.step++;
  580. break;
  581. case 1:
  582. for ( ; head->r.index < TOMOYO_MAX_PROFILES;
  583. head->r.index++)
  584. if (ns->profile_ptr[head->r.index])
  585. break;
  586. if (head->r.index == TOMOYO_MAX_PROFILES)
  587. return;
  588. head->r.step++;
  589. break;
  590. case 2:
  591. {
  592. u8 i;
  593. const struct tomoyo_path_info *comment =
  594. profile->comment;
  595. tomoyo_print_namespace(head);
  596. tomoyo_io_printf(head, "%u-COMMENT=", index);
  597. tomoyo_set_string(head, comment ? comment->name : "");
  598. tomoyo_set_lf(head);
  599. tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
  600. for (i = 0; i < TOMOYO_MAX_PREF; i++)
  601. tomoyo_io_printf(head, "%s=%u ",
  602. tomoyo_pref_keywords[i],
  603. profile->pref[i]);
  604. tomoyo_set_string(head, "}\n");
  605. head->r.step++;
  606. }
  607. break;
  608. case 3:
  609. {
  610. tomoyo_print_namespace(head);
  611. tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
  612. tomoyo_print_config(head, profile->default_config);
  613. head->r.bit = 0;
  614. head->r.step++;
  615. }
  616. break;
  617. case 4:
  618. for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
  619. + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
  620. const u8 i = head->r.bit;
  621. const u8 config = profile->config[i];
  622. if (config == TOMOYO_CONFIG_USE_DEFAULT)
  623. continue;
  624. tomoyo_print_namespace(head);
  625. if (i < TOMOYO_MAX_MAC_INDEX)
  626. tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
  627. index,
  628. tomoyo_category_keywords
  629. [tomoyo_index2category[i]],
  630. tomoyo_mac_keywords[i]);
  631. else
  632. tomoyo_io_printf(head, "%u-CONFIG::%s", index,
  633. tomoyo_mac_keywords[i]);
  634. tomoyo_print_config(head, config);
  635. head->r.bit++;
  636. break;
  637. }
  638. if (head->r.bit == TOMOYO_MAX_MAC_INDEX
  639. + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
  640. head->r.index++;
  641. head->r.step = 1;
  642. }
  643. break;
  644. }
  645. if (tomoyo_flush(head))
  646. goto next;
  647. }
  648. static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
  649. const struct tomoyo_acl_head *b)
  650. {
  651. return container_of(a, struct tomoyo_manager, head)->manager ==
  652. container_of(b, struct tomoyo_manager, head)->manager;
  653. }
  654. /**
  655. * tomoyo_update_manager_entry - Add a manager entry.
  656. *
  657. * @manager: The path to manager or the domainnamme.
  658. * @is_delete: True if it is a delete request.
  659. *
  660. * Returns 0 on success, negative value otherwise.
  661. *
  662. * Caller holds tomoyo_read_lock().
  663. */
  664. static int tomoyo_update_manager_entry(const char *manager,
  665. const bool is_delete)
  666. {
  667. struct tomoyo_manager e = { };
  668. struct tomoyo_acl_param param = {
  669. /* .ns = &tomoyo_kernel_namespace, */
  670. .is_delete = is_delete,
  671. .list = &tomoyo_kernel_namespace.
  672. policy_list[TOMOYO_ID_MANAGER],
  673. };
  674. int error = is_delete ? -ENOENT : -ENOMEM;
  675. if (tomoyo_domain_def(manager)) {
  676. if (!tomoyo_correct_domain(manager))
  677. return -EINVAL;
  678. e.is_domain = true;
  679. } else {
  680. if (!tomoyo_correct_path(manager))
  681. return -EINVAL;
  682. }
  683. e.manager = tomoyo_get_name(manager);
  684. if (e.manager) {
  685. error = tomoyo_update_policy(&e.head, sizeof(e), &param,
  686. tomoyo_same_manager);
  687. tomoyo_put_name(e.manager);
  688. }
  689. return error;
  690. }
  691. /**
  692. * tomoyo_write_manager - Write manager policy.
  693. *
  694. * @head: Pointer to "struct tomoyo_io_buffer".
  695. *
  696. * Returns 0 on success, negative value otherwise.
  697. *
  698. * Caller holds tomoyo_read_lock().
  699. */
  700. static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
  701. {
  702. char *data = head->write_buf;
  703. if (!strcmp(data, "manage_by_non_root")) {
  704. tomoyo_manage_by_non_root = !head->w.is_delete;
  705. return 0;
  706. }
  707. return tomoyo_update_manager_entry(data, head->w.is_delete);
  708. }
  709. /**
  710. * tomoyo_read_manager - Read manager policy.
  711. *
  712. * @head: Pointer to "struct tomoyo_io_buffer".
  713. *
  714. * Caller holds tomoyo_read_lock().
  715. */
  716. static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
  717. {
  718. if (head->r.eof)
  719. return;
  720. list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
  721. policy_list[TOMOYO_ID_MANAGER]) {
  722. struct tomoyo_manager *ptr =
  723. list_entry(head->r.acl, typeof(*ptr), head.list);
  724. if (ptr->head.is_deleted)
  725. continue;
  726. if (!tomoyo_flush(head))
  727. return;
  728. tomoyo_set_string(head, ptr->manager->name);
  729. tomoyo_set_lf(head);
  730. }
  731. head->r.eof = true;
  732. }
  733. /**
  734. * tomoyo_manager - Check whether the current process is a policy manager.
  735. *
  736. * Returns true if the current process is permitted to modify policy
  737. * via /sys/kernel/security/tomoyo/ interface.
  738. *
  739. * Caller holds tomoyo_read_lock().
  740. */
  741. static bool tomoyo_manager(void)
  742. {
  743. struct tomoyo_manager *ptr;
  744. const char *exe;
  745. const struct task_struct *task = current;
  746. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  747. bool found = false;
  748. if (!tomoyo_policy_loaded)
  749. return true;
  750. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  751. return false;
  752. list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
  753. policy_list[TOMOYO_ID_MANAGER], head.list) {
  754. if (!ptr->head.is_deleted && ptr->is_domain
  755. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  756. found = true;
  757. break;
  758. }
  759. }
  760. if (found)
  761. return true;
  762. exe = tomoyo_get_exe();
  763. if (!exe)
  764. return false;
  765. list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
  766. policy_list[TOMOYO_ID_MANAGER], head.list) {
  767. if (!ptr->head.is_deleted && !ptr->is_domain
  768. && !strcmp(exe, ptr->manager->name)) {
  769. found = true;
  770. break;
  771. }
  772. }
  773. if (!found) { /* Reduce error messages. */
  774. static pid_t last_pid;
  775. const pid_t pid = current->pid;
  776. if (last_pid != pid) {
  777. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  778. "update policies.\n", domainname->name, exe);
  779. last_pid = pid;
  780. }
  781. }
  782. kfree(exe);
  783. return found;
  784. }
  785. /**
  786. * tomoyo_select_domain - Parse select command.
  787. *
  788. * @head: Pointer to "struct tomoyo_io_buffer".
  789. * @data: String to parse.
  790. *
  791. * Returns true on success, false otherwise.
  792. *
  793. * Caller holds tomoyo_read_lock().
  794. */
  795. static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
  796. const char *data)
  797. {
  798. unsigned int pid;
  799. struct tomoyo_domain_info *domain = NULL;
  800. bool global_pid = false;
  801. if (strncmp(data, "select ", 7))
  802. return false;
  803. data += 7;
  804. if (sscanf(data, "pid=%u", &pid) == 1 ||
  805. (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
  806. struct task_struct *p;
  807. rcu_read_lock();
  808. read_lock(&tasklist_lock);
  809. if (global_pid)
  810. p = find_task_by_pid_ns(pid, &init_pid_ns);
  811. else
  812. p = find_task_by_vpid(pid);
  813. if (p)
  814. domain = tomoyo_real_domain(p);
  815. read_unlock(&tasklist_lock);
  816. rcu_read_unlock();
  817. } else if (!strncmp(data, "domain=", 7)) {
  818. if (tomoyo_domain_def(data + 7))
  819. domain = tomoyo_find_domain(data + 7);
  820. } else
  821. return false;
  822. head->w.domain = domain;
  823. /* Accessing read_buf is safe because head->io_sem is held. */
  824. if (!head->read_buf)
  825. return true; /* Do nothing if open(O_WRONLY). */
  826. memset(&head->r, 0, sizeof(head->r));
  827. head->r.print_this_domain_only = true;
  828. if (domain)
  829. head->r.domain = &domain->list;
  830. else
  831. head->r.eof = 1;
  832. tomoyo_io_printf(head, "# select %s\n", data);
  833. if (domain && domain->is_deleted)
  834. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  835. return true;
  836. }
  837. /**
  838. * tomoyo_delete_domain - Delete a domain.
  839. *
  840. * @domainname: The name of domain.
  841. *
  842. * Returns 0.
  843. *
  844. * Caller holds tomoyo_read_lock().
  845. */
  846. static int tomoyo_delete_domain(char *domainname)
  847. {
  848. struct tomoyo_domain_info *domain;
  849. struct tomoyo_path_info name;
  850. name.name = domainname;
  851. tomoyo_fill_path_info(&name);
  852. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  853. return 0;
  854. /* Is there an active domain? */
  855. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  856. /* Never delete tomoyo_kernel_domain */
  857. if (domain == &tomoyo_kernel_domain)
  858. continue;
  859. if (domain->is_deleted ||
  860. tomoyo_pathcmp(domain->domainname, &name))
  861. continue;
  862. domain->is_deleted = true;
  863. break;
  864. }
  865. mutex_unlock(&tomoyo_policy_lock);
  866. return 0;
  867. }
  868. /**
  869. * tomoyo_write_domain2 - Write domain policy.
  870. *
  871. * @ns: Pointer to "struct tomoyo_policy_namespace".
  872. * @list: Pointer to "struct list_head".
  873. * @data: Policy to be interpreted.
  874. * @is_delete: True if it is a delete request.
  875. *
  876. * Returns 0 on success, negative value otherwise.
  877. *
  878. * Caller holds tomoyo_read_lock().
  879. */
  880. static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
  881. struct list_head *list, char *data,
  882. const bool is_delete)
  883. {
  884. struct tomoyo_acl_param param = {
  885. .ns = ns,
  886. .list = list,
  887. .data = data,
  888. .is_delete = is_delete,
  889. };
  890. static const struct {
  891. const char *keyword;
  892. int (*write) (struct tomoyo_acl_param *);
  893. } tomoyo_callback[1] = {
  894. { "file ", tomoyo_write_file },
  895. };
  896. u8 i;
  897. for (i = 0; i < 1; i++) {
  898. if (!tomoyo_str_starts(&param.data,
  899. tomoyo_callback[i].keyword))
  900. continue;
  901. return tomoyo_callback[i].write(&param);
  902. }
  903. return -EINVAL;
  904. }
  905. /* String table for domain flags. */
  906. const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
  907. [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n",
  908. [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
  909. };
  910. /**
  911. * tomoyo_write_domain - Write domain policy.
  912. *
  913. * @head: Pointer to "struct tomoyo_io_buffer".
  914. *
  915. * Returns 0 on success, negative value otherwise.
  916. *
  917. * Caller holds tomoyo_read_lock().
  918. */
  919. static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
  920. {
  921. char *data = head->write_buf;
  922. struct tomoyo_policy_namespace *ns;
  923. struct tomoyo_domain_info *domain = head->w.domain;
  924. const bool is_delete = head->w.is_delete;
  925. bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
  926. unsigned int profile;
  927. if (*data == '<') {
  928. domain = NULL;
  929. if (is_delete)
  930. tomoyo_delete_domain(data);
  931. else if (is_select)
  932. domain = tomoyo_find_domain(data);
  933. else
  934. domain = tomoyo_assign_domain(data, false);
  935. head->w.domain = domain;
  936. return 0;
  937. }
  938. if (!domain)
  939. return -EINVAL;
  940. ns = domain->ns;
  941. if (sscanf(data, "use_profile %u", &profile) == 1
  942. && profile < TOMOYO_MAX_PROFILES) {
  943. if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
  944. domain->profile = (u8) profile;
  945. return 0;
  946. }
  947. if (sscanf(data, "use_group %u\n", &profile) == 1
  948. && profile < TOMOYO_MAX_ACL_GROUPS) {
  949. if (!is_delete)
  950. domain->group = (u8) profile;
  951. return 0;
  952. }
  953. for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
  954. const char *cp = tomoyo_dif[profile];
  955. if (strncmp(data, cp, strlen(cp) - 1))
  956. continue;
  957. domain->flags[profile] = !is_delete;
  958. return 0;
  959. }
  960. return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
  961. is_delete);
  962. }
  963. /**
  964. * tomoyo_set_group - Print "acl_group " header keyword and category name.
  965. *
  966. * @head: Pointer to "struct tomoyo_io_buffer".
  967. * @category: Category name.
  968. *
  969. * Returns nothing.
  970. */
  971. static void tomoyo_set_group(struct tomoyo_io_buffer *head,
  972. const char *category)
  973. {
  974. if (head->type == TOMOYO_EXCEPTIONPOLICY) {
  975. tomoyo_print_namespace(head);
  976. tomoyo_io_printf(head, "acl_group %u ",
  977. head->r.acl_group_index);
  978. }
  979. tomoyo_set_string(head, category);
  980. }
  981. /**
  982. * tomoyo_print_entry - Print an ACL entry.
  983. *
  984. * @head: Pointer to "struct tomoyo_io_buffer".
  985. * @acl: Pointer to an ACL entry.
  986. *
  987. * Returns true on success, false otherwise.
  988. */
  989. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  990. struct tomoyo_acl_info *acl)
  991. {
  992. const u8 acl_type = acl->type;
  993. bool first = true;
  994. u8 bit;
  995. if (acl->is_deleted)
  996. return true;
  997. if (!tomoyo_flush(head))
  998. return false;
  999. else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  1000. struct tomoyo_path_acl *ptr =
  1001. container_of(acl, typeof(*ptr), head);
  1002. const u16 perm = ptr->perm;
  1003. for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  1004. if (!(perm & (1 << bit)))
  1005. continue;
  1006. if (head->r.print_transition_related_only &&
  1007. bit != TOMOYO_TYPE_EXECUTE)
  1008. continue;
  1009. if (first) {
  1010. tomoyo_set_group(head, "file ");
  1011. first = false;
  1012. } else {
  1013. tomoyo_set_slash(head);
  1014. }
  1015. tomoyo_set_string(head, tomoyo_path_keyword[bit]);
  1016. }
  1017. if (first)
  1018. return true;
  1019. tomoyo_print_name_union(head, &ptr->name);
  1020. } else if (head->r.print_transition_related_only) {
  1021. return true;
  1022. } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
  1023. struct tomoyo_path2_acl *ptr =
  1024. container_of(acl, typeof(*ptr), head);
  1025. const u8 perm = ptr->perm;
  1026. for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
  1027. if (!(perm & (1 << bit)))
  1028. continue;
  1029. if (first) {
  1030. tomoyo_set_group(head, "file ");
  1031. first = false;
  1032. } else {
  1033. tomoyo_set_slash(head);
  1034. }
  1035. tomoyo_set_string(head, tomoyo_mac_keywords
  1036. [tomoyo_pp2mac[bit]]);
  1037. }
  1038. if (first)
  1039. return true;
  1040. tomoyo_print_name_union(head, &ptr->name1);
  1041. tomoyo_print_name_union(head, &ptr->name2);
  1042. } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
  1043. struct tomoyo_path_number_acl *ptr =
  1044. container_of(acl, typeof(*ptr), head);
  1045. const u8 perm = ptr->perm;
  1046. for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
  1047. if (!(perm & (1 << bit)))
  1048. continue;
  1049. if (first) {
  1050. tomoyo_set_group(head, "file ");
  1051. first = false;
  1052. } else {
  1053. tomoyo_set_slash(head);
  1054. }
  1055. tomoyo_set_string(head, tomoyo_mac_keywords
  1056. [tomoyo_pn2mac[bit]]);
  1057. }
  1058. if (first)
  1059. return true;
  1060. tomoyo_print_name_union(head, &ptr->name);
  1061. tomoyo_print_number_union(head, &ptr->number);
  1062. } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
  1063. struct tomoyo_mkdev_acl *ptr =
  1064. container_of(acl, typeof(*ptr), head);
  1065. const u8 perm = ptr->perm;
  1066. for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
  1067. if (!(perm & (1 << bit)))
  1068. continue;
  1069. if (first) {
  1070. tomoyo_set_group(head, "file ");
  1071. first = false;
  1072. } else {
  1073. tomoyo_set_slash(head);
  1074. }
  1075. tomoyo_set_string(head, tomoyo_mac_keywords
  1076. [tomoyo_pnnn2mac[bit]]);
  1077. }
  1078. if (first)
  1079. return true;
  1080. tomoyo_print_name_union(head, &ptr->name);
  1081. tomoyo_print_number_union(head, &ptr->mode);
  1082. tomoyo_print_number_union(head, &ptr->major);
  1083. tomoyo_print_number_union(head, &ptr->minor);
  1084. } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
  1085. struct tomoyo_mount_acl *ptr =
  1086. container_of(acl, typeof(*ptr), head);
  1087. tomoyo_set_group(head, "file mount");
  1088. tomoyo_print_name_union(head, &ptr->dev_name);
  1089. tomoyo_print_name_union(head, &ptr->dir_name);
  1090. tomoyo_print_name_union(head, &ptr->fs_type);
  1091. tomoyo_print_number_union(head, &ptr->flags);
  1092. }
  1093. tomoyo_set_lf(head);
  1094. return true;
  1095. }
  1096. /**
  1097. * tomoyo_read_domain2 - Read domain policy.
  1098. *
  1099. * @head: Pointer to "struct tomoyo_io_buffer".
  1100. * @list: Pointer to "struct list_head".
  1101. *
  1102. * Caller holds tomoyo_read_lock().
  1103. *
  1104. * Returns true on success, false otherwise.
  1105. */
  1106. static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
  1107. struct list_head *list)
  1108. {
  1109. list_for_each_cookie(head->r.acl, list) {
  1110. struct tomoyo_acl_info *ptr =
  1111. list_entry(head->r.acl, typeof(*ptr), list);
  1112. if (!tomoyo_print_entry(head, ptr))
  1113. return false;
  1114. }
  1115. head->r.acl = NULL;
  1116. return true;
  1117. }
  1118. /**
  1119. * tomoyo_read_domain - Read domain policy.
  1120. *
  1121. * @head: Pointer to "struct tomoyo_io_buffer".
  1122. *
  1123. * Caller holds tomoyo_read_lock().
  1124. */
  1125. static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
  1126. {
  1127. if (head->r.eof)
  1128. return;
  1129. list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
  1130. struct tomoyo_domain_info *domain =
  1131. list_entry(head->r.domain, typeof(*domain), list);
  1132. switch (head->r.step) {
  1133. u8 i;
  1134. case 0:
  1135. if (domain->is_deleted &&
  1136. !head->r.print_this_domain_only)
  1137. continue;
  1138. /* Print domainname and flags. */
  1139. tomoyo_set_string(head, domain->domainname->name);
  1140. tomoyo_set_lf(head);
  1141. tomoyo_io_printf(head, "use_profile %u\n",
  1142. domain->profile);
  1143. tomoyo_io_printf(head, "use_group %u\n",
  1144. domain->group);
  1145. for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
  1146. if (domain->flags[i])
  1147. tomoyo_set_string(head, tomoyo_dif[i]);
  1148. head->r.step++;
  1149. tomoyo_set_lf(head);
  1150. /* fall through */
  1151. case 1:
  1152. if (!tomoyo_read_domain2(head, &domain->acl_info_list))
  1153. return;
  1154. head->r.step++;
  1155. if (!tomoyo_set_lf(head))
  1156. return;
  1157. /* fall through */
  1158. case 2:
  1159. head->r.step = 0;
  1160. if (head->r.print_this_domain_only)
  1161. goto done;
  1162. }
  1163. }
  1164. done:
  1165. head->r.eof = true;
  1166. }
  1167. /**
  1168. * tomoyo_write_domain_profile - Assign profile for specified domain.
  1169. *
  1170. * @head: Pointer to "struct tomoyo_io_buffer".
  1171. *
  1172. * Returns 0 on success, -EINVAL otherwise.
  1173. *
  1174. * This is equivalent to doing
  1175. *
  1176. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  1177. * /usr/sbin/tomoyo-loadpolicy -d
  1178. *
  1179. * Caller holds tomoyo_read_lock().
  1180. */
  1181. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  1182. {
  1183. char *data = head->write_buf;
  1184. char *cp = strchr(data, ' ');
  1185. struct tomoyo_domain_info *domain;
  1186. unsigned long profile;
  1187. if (!cp)
  1188. return -EINVAL;
  1189. *cp = '\0';
  1190. domain = tomoyo_find_domain(cp + 1);
  1191. if (strict_strtoul(data, 10, &profile))
  1192. return -EINVAL;
  1193. if (domain && (!tomoyo_policy_loaded ||
  1194. head->w.ns->profile_ptr[(u8) profile]))
  1195. domain->profile = (u8) profile;
  1196. return 0;
  1197. }
  1198. /**
  1199. * tomoyo_read_domain_profile - Read only domainname and profile.
  1200. *
  1201. * @head: Pointer to "struct tomoyo_io_buffer".
  1202. *
  1203. * Returns list of profile number and domainname pairs.
  1204. *
  1205. * This is equivalent to doing
  1206. *
  1207. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  1208. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  1209. * domainname = $0; } else if ( $1 == "use_profile" ) {
  1210. * print $2 " " domainname; domainname = ""; } } ; '
  1211. *
  1212. * Caller holds tomoyo_read_lock().
  1213. */
  1214. static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1215. {
  1216. if (head->r.eof)
  1217. return;
  1218. list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
  1219. struct tomoyo_domain_info *domain =
  1220. list_entry(head->r.domain, typeof(*domain), list);
  1221. if (domain->is_deleted)
  1222. continue;
  1223. if (!tomoyo_flush(head))
  1224. return;
  1225. tomoyo_io_printf(head, "%u ", domain->profile);
  1226. tomoyo_set_string(head, domain->domainname->name);
  1227. tomoyo_set_lf(head);
  1228. }
  1229. head->r.eof = true;
  1230. }
  1231. /**
  1232. * tomoyo_write_pid: Specify PID to obtain domainname.
  1233. *
  1234. * @head: Pointer to "struct tomoyo_io_buffer".
  1235. *
  1236. * Returns 0.
  1237. */
  1238. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1239. {
  1240. head->r.eof = false;
  1241. return 0;
  1242. }
  1243. /**
  1244. * tomoyo_read_pid - Get domainname of the specified PID.
  1245. *
  1246. * @head: Pointer to "struct tomoyo_io_buffer".
  1247. *
  1248. * Returns the domainname which the specified PID is in on success,
  1249. * empty string otherwise.
  1250. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1251. * using read()/write() interface rather than sysctl() interface.
  1252. */
  1253. static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1254. {
  1255. char *buf = head->write_buf;
  1256. bool global_pid = false;
  1257. unsigned int pid;
  1258. struct task_struct *p;
  1259. struct tomoyo_domain_info *domain = NULL;
  1260. /* Accessing write_buf is safe because head->io_sem is held. */
  1261. if (!buf) {
  1262. head->r.eof = true;
  1263. return; /* Do nothing if open(O_RDONLY). */
  1264. }
  1265. if (head->r.w_pos || head->r.eof)
  1266. return;
  1267. head->r.eof = true;
  1268. if (tomoyo_str_starts(&buf, "global-pid "))
  1269. global_pid = true;
  1270. pid = (unsigned int) simple_strtoul(buf, NULL, 10);
  1271. rcu_read_lock();
  1272. read_lock(&tasklist_lock);
  1273. if (global_pid)
  1274. p = find_task_by_pid_ns(pid, &init_pid_ns);
  1275. else
  1276. p = find_task_by_vpid(pid);
  1277. if (p)
  1278. domain = tomoyo_real_domain(p);
  1279. read_unlock(&tasklist_lock);
  1280. rcu_read_unlock();
  1281. if (!domain)
  1282. return;
  1283. tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
  1284. tomoyo_set_string(head, domain->domainname->name);
  1285. }
  1286. static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
  1287. [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
  1288. [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
  1289. [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
  1290. [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
  1291. [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
  1292. [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
  1293. };
  1294. static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
  1295. [TOMOYO_PATH_GROUP] = "path_group ",
  1296. [TOMOYO_NUMBER_GROUP] = "number_group ",
  1297. };
  1298. /**
  1299. * tomoyo_write_exception - Write exception policy.
  1300. *
  1301. * @head: Pointer to "struct tomoyo_io_buffer".
  1302. *
  1303. * Returns 0 on success, negative value otherwise.
  1304. *
  1305. * Caller holds tomoyo_read_lock().
  1306. */
  1307. static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
  1308. {
  1309. const bool is_delete = head->w.is_delete;
  1310. struct tomoyo_acl_param param = {
  1311. .ns = head->w.ns,
  1312. .is_delete = is_delete,
  1313. .data = head->write_buf,
  1314. };
  1315. u8 i;
  1316. if (tomoyo_str_starts(&param.data, "aggregator "))
  1317. return tomoyo_write_aggregator(&param);
  1318. for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
  1319. if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
  1320. return tomoyo_write_transition_control(&param, i);
  1321. for (i = 0; i < TOMOYO_MAX_GROUP; i++)
  1322. if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
  1323. return tomoyo_write_group(&param, i);
  1324. if (tomoyo_str_starts(&param.data, "acl_group ")) {
  1325. unsigned int group;
  1326. char *data;
  1327. group = simple_strtoul(param.data, &data, 10);
  1328. if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
  1329. return tomoyo_write_domain2
  1330. (head->w.ns, &head->w.ns->acl_group[group],
  1331. data, is_delete);
  1332. }
  1333. return -EINVAL;
  1334. }
  1335. /**
  1336. * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  1337. *
  1338. * @head: Pointer to "struct tomoyo_io_buffer".
  1339. * @idx: Index number.
  1340. *
  1341. * Returns true on success, false otherwise.
  1342. *
  1343. * Caller holds tomoyo_read_lock().
  1344. */
  1345. static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
  1346. {
  1347. struct tomoyo_policy_namespace *ns =
  1348. container_of(head->r.ns, typeof(*ns), namespace_list);
  1349. struct list_head *list = &ns->group_list[idx];
  1350. list_for_each_cookie(head->r.group, list) {
  1351. struct tomoyo_group *group =
  1352. list_entry(head->r.group, typeof(*group), head.list);
  1353. list_for_each_cookie(head->r.acl, &group->member_list) {
  1354. struct tomoyo_acl_head *ptr =
  1355. list_entry(head->r.acl, typeof(*ptr), list);
  1356. if (ptr->is_deleted)
  1357. continue;
  1358. if (!tomoyo_flush(head))
  1359. return false;
  1360. tomoyo_print_namespace(head);
  1361. tomoyo_set_string(head, tomoyo_group_name[idx]);
  1362. tomoyo_set_string(head, group->group_name->name);
  1363. if (idx == TOMOYO_PATH_GROUP) {
  1364. tomoyo_set_space(head);
  1365. tomoyo_set_string(head, container_of
  1366. (ptr, struct tomoyo_path_group,
  1367. head)->member_name->name);
  1368. } else if (idx == TOMOYO_NUMBER_GROUP) {
  1369. tomoyo_print_number_union(head, &container_of
  1370. (ptr,
  1371. struct tomoyo_number_group,
  1372. head)->number);
  1373. }
  1374. tomoyo_set_lf(head);
  1375. }
  1376. head->r.acl = NULL;
  1377. }
  1378. head->r.group = NULL;
  1379. return true;
  1380. }
  1381. /**
  1382. * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
  1383. *
  1384. * @head: Pointer to "struct tomoyo_io_buffer".
  1385. * @idx: Index number.
  1386. *
  1387. * Returns true on success, false otherwise.
  1388. *
  1389. * Caller holds tomoyo_read_lock().
  1390. */
  1391. static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
  1392. {
  1393. struct tomoyo_policy_namespace *ns =
  1394. container_of(head->r.ns, typeof(*ns), namespace_list);
  1395. struct list_head *list = &ns->policy_list[idx];
  1396. list_for_each_cookie(head->r.acl, list) {
  1397. struct tomoyo_acl_head *acl =
  1398. container_of(head->r.acl, typeof(*acl), list);
  1399. if (acl->is_deleted)
  1400. continue;
  1401. if (!tomoyo_flush(head))
  1402. return false;
  1403. switch (idx) {
  1404. case TOMOYO_ID_TRANSITION_CONTROL:
  1405. {
  1406. struct tomoyo_transition_control *ptr =
  1407. container_of(acl, typeof(*ptr), head);
  1408. tomoyo_print_namespace(head);
  1409. tomoyo_set_string(head, tomoyo_transition_type
  1410. [ptr->type]);
  1411. tomoyo_set_string(head, ptr->program ?
  1412. ptr->program->name : "any");
  1413. tomoyo_set_string(head, " from ");
  1414. tomoyo_set_string(head, ptr->domainname ?
  1415. ptr->domainname->name :
  1416. "any");
  1417. }
  1418. break;
  1419. case TOMOYO_ID_AGGREGATOR:
  1420. {
  1421. struct tomoyo_aggregator *ptr =
  1422. container_of(acl, typeof(*ptr), head);
  1423. tomoyo_print_namespace(head);
  1424. tomoyo_set_string(head, "aggregator ");
  1425. tomoyo_set_string(head,
  1426. ptr->original_name->name);
  1427. tomoyo_set_space(head);
  1428. tomoyo_set_string(head,
  1429. ptr->aggregated_name->name);
  1430. }
  1431. break;
  1432. default:
  1433. continue;
  1434. }
  1435. tomoyo_set_lf(head);
  1436. }
  1437. head->r.acl = NULL;
  1438. return true;
  1439. }
  1440. /**
  1441. * tomoyo_read_exception - Read exception policy.
  1442. *
  1443. * @head: Pointer to "struct tomoyo_io_buffer".
  1444. *
  1445. * Caller holds tomoyo_read_lock().
  1446. */
  1447. static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
  1448. {
  1449. struct tomoyo_policy_namespace *ns =
  1450. container_of(head->r.ns, typeof(*ns), namespace_list);
  1451. if (head->r.eof)
  1452. return;
  1453. while (head->r.step < TOMOYO_MAX_POLICY &&
  1454. tomoyo_read_policy(head, head->r.step))
  1455. head->r.step++;
  1456. if (head->r.step < TOMOYO_MAX_POLICY)
  1457. return;
  1458. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
  1459. tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
  1460. head->r.step++;
  1461. if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
  1462. return;
  1463. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
  1464. + TOMOYO_MAX_ACL_GROUPS) {
  1465. head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
  1466. - TOMOYO_MAX_GROUP;
  1467. if (!tomoyo_read_domain2(head, &ns->acl_group
  1468. [head->r.acl_group_index]))
  1469. return;
  1470. head->r.step++;
  1471. }
  1472. head->r.eof = true;
  1473. }
  1474. /* Wait queue for kernel -> userspace notification. */
  1475. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1476. /* Wait queue for userspace -> kernel notification. */
  1477. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
  1478. /* Structure for query. */
  1479. struct tomoyo_query {
  1480. struct list_head list;
  1481. char *query;
  1482. size_t query_len;
  1483. unsigned int serial;
  1484. u8 timer;
  1485. u8 answer;
  1486. u8 retry;
  1487. };
  1488. /* The list for "struct tomoyo_query". */
  1489. static LIST_HEAD(tomoyo_query_list);
  1490. /* Lock for manipulating tomoyo_query_list. */
  1491. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1492. /*
  1493. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1494. * interface.
  1495. */
  1496. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1497. /**
  1498. * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
  1499. *
  1500. * @domain: Pointer to "struct tomoyo_domain_info".
  1501. * @header: Lines containing ACL.
  1502. *
  1503. * Returns nothing.
  1504. */
  1505. static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
  1506. {
  1507. char *buffer;
  1508. char *cp = strchr(header, '\n');
  1509. int len;
  1510. if (!cp)
  1511. return;
  1512. cp = strchr(cp + 1, '\n');
  1513. if (!cp)
  1514. return;
  1515. *cp++ = '\0';
  1516. len = strlen(cp) + 1;
  1517. buffer = kmalloc(len, GFP_NOFS);
  1518. if (!buffer)
  1519. return;
  1520. snprintf(buffer, len - 1, "%s", cp);
  1521. tomoyo_normalize_line(buffer);
  1522. if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
  1523. false))
  1524. tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
  1525. kfree(buffer);
  1526. }
  1527. /**
  1528. * tomoyo_supervisor - Ask for the supervisor's decision.
  1529. *
  1530. * @r: Pointer to "struct tomoyo_request_info".
  1531. * @fmt: The printf()'s format string, followed by parameters.
  1532. *
  1533. * Returns 0 if the supervisor decided to permit the access request which
  1534. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1535. * supervisor decided to retry the access request which violated the policy in
  1536. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1537. */
  1538. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1539. {
  1540. va_list args;
  1541. int error;
  1542. int len;
  1543. static unsigned int tomoyo_serial;
  1544. struct tomoyo_query entry = { };
  1545. bool quota_exceeded = false;
  1546. va_start(args, fmt);
  1547. len = vsnprintf((char *) &len, 1, fmt, args) + 1;
  1548. va_end(args);
  1549. /* Write /sys/kernel/security/tomoyo/audit. */
  1550. va_start(args, fmt);
  1551. tomoyo_write_log2(r, len, fmt, args);
  1552. va_end(args);
  1553. /* Nothing more to do if granted. */
  1554. if (r->granted)
  1555. return 0;
  1556. if (r->mode)
  1557. tomoyo_update_stat(r->mode);
  1558. switch (r->mode) {
  1559. case TOMOYO_CONFIG_ENFORCING:
  1560. error = -EPERM;
  1561. if (atomic_read(&tomoyo_query_observers))
  1562. break;
  1563. goto out;
  1564. case TOMOYO_CONFIG_LEARNING:
  1565. error = 0;
  1566. /* Check max_learning_entry parameter. */
  1567. if (tomoyo_domain_quota_is_ok(r))
  1568. break;
  1569. /* fall through */
  1570. default:
  1571. return 0;
  1572. }
  1573. /* Get message. */
  1574. va_start(args, fmt);
  1575. entry.query = tomoyo_init_log(r, len, fmt, args);
  1576. va_end(args);
  1577. if (!entry.query)
  1578. goto out;
  1579. entry.query_len = strlen(entry.query) + 1;
  1580. if (!error) {
  1581. tomoyo_add_entry(r->domain, entry.query);
  1582. goto out;
  1583. }
  1584. len = tomoyo_round2(entry.query_len);
  1585. spin_lock(&tomoyo_query_list_lock);
  1586. if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
  1587. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
  1588. >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
  1589. quota_exceeded = true;
  1590. } else {
  1591. entry.serial = tomoyo_serial++;
  1592. entry.retry = r->retry;
  1593. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
  1594. list_add_tail(&entry.list, &tomoyo_query_list);
  1595. }
  1596. spin_unlock(&tomoyo_query_list_lock);
  1597. if (quota_exceeded)
  1598. goto out;
  1599. /* Give 10 seconds for supervisor's opinion. */
  1600. while (entry.timer < 10) {
  1601. wake_up_all(&tomoyo_query_wait);
  1602. if (wait_event_interruptible_timeout
  1603. (tomoyo_answer_wait, entry.answer ||
  1604. !atomic_read(&tomoyo_query_observers), HZ))
  1605. break;
  1606. else
  1607. entry.timer++;
  1608. }
  1609. spin_lock(&tomoyo_query_list_lock);
  1610. list_del(&entry.list);
  1611. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
  1612. spin_unlock(&tomoyo_query_list_lock);
  1613. switch (entry.answer) {
  1614. case 3: /* Asked to retry by administrator. */
  1615. error = TOMOYO_RETRY_REQUEST;
  1616. r->retry++;
  1617. break;
  1618. case 1:
  1619. /* Granted by administrator. */
  1620. error = 0;
  1621. break;
  1622. default:
  1623. /* Timed out or rejected by administrator. */
  1624. break;
  1625. }
  1626. out:
  1627. kfree(entry.query);
  1628. return error;
  1629. }
  1630. /**
  1631. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  1632. *
  1633. * @file: Pointer to "struct file".
  1634. * @wait: Pointer to "poll_table".
  1635. *
  1636. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  1637. *
  1638. * Waits for access requests which violated policy in enforcing mode.
  1639. */
  1640. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  1641. {
  1642. struct list_head *tmp;
  1643. bool found = false;
  1644. u8 i;
  1645. for (i = 0; i < 2; i++) {
  1646. spin_lock(&tomoyo_query_list_lock);
  1647. list_for_each(tmp, &tomoyo_query_list) {
  1648. struct tomoyo_query *ptr =
  1649. list_entry(tmp, typeof(*ptr), list);
  1650. if (ptr->answer)
  1651. continue;
  1652. found = true;
  1653. break;
  1654. }
  1655. spin_unlock(&tomoyo_query_list_lock);
  1656. if (found)
  1657. return POLLIN | POLLRDNORM;
  1658. if (i)
  1659. break;
  1660. poll_wait(file, &tomoyo_query_wait, wait);
  1661. }
  1662. return 0;
  1663. }
  1664. /**
  1665. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  1666. *
  1667. * @head: Pointer to "struct tomoyo_io_buffer".
  1668. */
  1669. static void tomoyo_read_query(struct tomoyo_io_buffer *head)
  1670. {
  1671. struct list_head *tmp;
  1672. unsigned int pos = 0;
  1673. size_t len = 0;
  1674. char *buf;
  1675. if (head->r.w_pos)
  1676. return;
  1677. if (head->read_buf) {
  1678. kfree(head->read_buf);
  1679. head->read_buf = NULL;
  1680. }
  1681. spin_lock(&tomoyo_query_list_lock);
  1682. list_for_each(tmp, &tomoyo_query_list) {
  1683. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1684. if (ptr->answer)
  1685. continue;
  1686. if (pos++ != head->r.query_index)
  1687. continue;
  1688. len = ptr->query_len;
  1689. break;
  1690. }
  1691. spin_unlock(&tomoyo_query_list_lock);
  1692. if (!len) {
  1693. head->r.query_index = 0;
  1694. return;
  1695. }
  1696. buf = kzalloc(len + 32, GFP_NOFS);
  1697. if (!buf)
  1698. return;
  1699. pos = 0;
  1700. spin_lock(&tomoyo_query_list_lock);
  1701. list_for_each(tmp, &tomoyo_query_list) {
  1702. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1703. if (ptr->answer)
  1704. continue;
  1705. if (pos++ != head->r.query_index)
  1706. continue;
  1707. /*
  1708. * Some query can be skipped because tomoyo_query_list
  1709. * can change, but I don't care.
  1710. */
  1711. if (len == ptr->query_len)
  1712. snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
  1713. ptr->retry, ptr->query);
  1714. break;
  1715. }
  1716. spin_unlock(&tomoyo_query_list_lock);
  1717. if (buf[0]) {
  1718. head->read_buf = buf;
  1719. head->r.w[head->r.w_pos++] = buf;
  1720. head->r.query_index++;
  1721. } else {
  1722. kfree(buf);
  1723. }
  1724. }
  1725. /**
  1726. * tomoyo_write_answer - Write the supervisor's decision.
  1727. *
  1728. * @head: Pointer to "struct tomoyo_io_buffer".
  1729. *
  1730. * Returns 0 on success, -EINVAL otherwise.
  1731. */
  1732. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  1733. {
  1734. char *data = head->write_buf;
  1735. struct list_head *tmp;
  1736. unsigned int serial;
  1737. unsigned int answer;
  1738. spin_lock(&tomoyo_query_list_lock);
  1739. list_for_each(tmp, &tomoyo_query_list) {
  1740. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1741. ptr->timer = 0;
  1742. }
  1743. spin_unlock(&tomoyo_query_list_lock);
  1744. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  1745. return -EINVAL;
  1746. spin_lock(&tomoyo_query_list_lock);
  1747. list_for_each(tmp, &tomoyo_query_list) {
  1748. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1749. if (ptr->serial != serial)
  1750. continue;
  1751. if (!ptr->answer)
  1752. ptr->answer = answer;
  1753. break;
  1754. }
  1755. spin_unlock(&tomoyo_query_list_lock);
  1756. return 0;
  1757. }
  1758. /**
  1759. * tomoyo_read_version: Get version.
  1760. *
  1761. * @head: Pointer to "struct tomoyo_io_buffer".
  1762. *
  1763. * Returns version information.
  1764. */
  1765. static void tomoyo_read_version(struct tomoyo_io_buffer *head)
  1766. {
  1767. if (!head->r.eof) {
  1768. tomoyo_io_printf(head, "2.4.0");
  1769. head->r.eof = true;
  1770. }
  1771. }
  1772. /**
  1773. * tomoyo_read_self_domain - Get the current process's domainname.
  1774. *
  1775. * @head: Pointer to "struct tomoyo_io_buffer".
  1776. *
  1777. * Returns the current process's domainname.
  1778. */
  1779. static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1780. {
  1781. if (!head->r.eof) {
  1782. /*
  1783. * tomoyo_domain()->domainname != NULL
  1784. * because every process belongs to a domain and
  1785. * the domain's name cannot be NULL.
  1786. */
  1787. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1788. head->r.eof = true;
  1789. }
  1790. }
  1791. /* String table for /sys/kernel/security/tomoyo/stat interface. */
  1792. static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
  1793. [TOMOYO_STAT_POLICY_UPDATES] = "update:",
  1794. [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:",
  1795. [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
  1796. [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
  1797. };
  1798. /* String table for /sys/kernel/security/tomoyo/stat interface. */
  1799. static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
  1800. [TOMOYO_MEMORY_POLICY] = "policy:",
  1801. [TOMOYO_MEMORY_AUDIT] = "audit log:",
  1802. [TOMOYO_MEMORY_QUERY] = "query message:",
  1803. };
  1804. /* Timestamp counter for last updated. */
  1805. static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
  1806. /* Counter for number of updates. */
  1807. static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
  1808. /**
  1809. * tomoyo_update_stat - Update statistic counters.
  1810. *
  1811. * @index: Index for policy type.
  1812. *
  1813. * Returns nothing.
  1814. */
  1815. void tomoyo_update_stat(const u8 index)
  1816. {
  1817. struct timeval tv;
  1818. do_gettimeofday(&tv);
  1819. /*
  1820. * I don't use atomic operations because race condition is not fatal.
  1821. */
  1822. tomoyo_stat_updated[index]++;
  1823. tomoyo_stat_modified[index] = tv.tv_sec;
  1824. }
  1825. /**
  1826. * tomoyo_read_stat - Read statistic data.
  1827. *
  1828. * @head: Pointer to "struct tomoyo_io_buffer".
  1829. *
  1830. * Returns nothing.
  1831. */
  1832. static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
  1833. {
  1834. u8 i;
  1835. unsigned int total = 0;
  1836. if (head->r.eof)
  1837. return;
  1838. for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
  1839. tomoyo_io_printf(head, "Policy %-30s %10u",
  1840. tomoyo_policy_headers[i],
  1841. tomoyo_stat_updated[i]);
  1842. if (tomoyo_stat_modified[i]) {
  1843. struct tomoyo_time stamp;
  1844. tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
  1845. tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
  1846. "%02u:%02u:%02u)",
  1847. stamp.year, stamp.month, stamp.day,
  1848. stamp.hour, stamp.min, stamp.sec);
  1849. }
  1850. tomoyo_set_lf(head);
  1851. }
  1852. for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
  1853. unsigned int used = tomoyo_memory_used[i];
  1854. total += used;
  1855. tomoyo_io_printf(head, "Memory used by %-22s %10u",
  1856. tomoyo_memory_headers[i], used);
  1857. used = tomoyo_memory_quota[i];
  1858. if (used)
  1859. tomoyo_io_printf(head, " (Quota: %10u)", used);
  1860. tomoyo_set_lf(head);
  1861. }
  1862. tomoyo_io_printf(head, "Total memory used: %10u\n",
  1863. total);
  1864. head->r.eof = true;
  1865. }
  1866. /**
  1867. * tomoyo_write_stat - Set memory quota.
  1868. *
  1869. * @head: Pointer to "struct tomoyo_io_buffer".
  1870. *
  1871. * Returns 0.
  1872. */
  1873. static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
  1874. {
  1875. char *data = head->write_buf;
  1876. u8 i;
  1877. if (tomoyo_str_starts(&data, "Memory used by "))
  1878. for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
  1879. if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
  1880. sscanf(data, "%u", &tomoyo_memory_quota[i]);
  1881. return 0;
  1882. }
  1883. /**
  1884. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1885. *
  1886. * @type: Type of interface.
  1887. * @file: Pointer to "struct file".
  1888. *
  1889. * Returns 0 on success, negative value otherwise.
  1890. */
  1891. int tomoyo_open_control(const u8 type, struct file *file)
  1892. {
  1893. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  1894. if (!head)
  1895. return -ENOMEM;
  1896. mutex_init(&head->io_sem);
  1897. head->type = type;
  1898. switch (type) {
  1899. case TOMOYO_DOMAINPOLICY:
  1900. /* /sys/kernel/security/tomoyo/domain_policy */
  1901. head->write = tomoyo_write_domain;
  1902. head->read = tomoyo_read_domain;
  1903. break;
  1904. case TOMOYO_EXCEPTIONPOLICY:
  1905. /* /sys/kernel/security/tomoyo/exception_policy */
  1906. head->write = tomoyo_write_exception;
  1907. head->read = tomoyo_read_exception;
  1908. break;
  1909. case TOMOYO_AUDIT:
  1910. /* /sys/kernel/security/tomoyo/audit */
  1911. head->poll = tomoyo_poll_log;
  1912. head->read = tomoyo_read_log;
  1913. break;
  1914. case TOMOYO_SELFDOMAIN:
  1915. /* /sys/kernel/security/tomoyo/self_domain */
  1916. head->read = tomoyo_read_self_domain;
  1917. break;
  1918. case TOMOYO_DOMAIN_STATUS:
  1919. /* /sys/kernel/security/tomoyo/.domain_status */
  1920. head->write = tomoyo_write_domain_profile;
  1921. head->read = tomoyo_read_domain_profile;
  1922. break;
  1923. case TOMOYO_PROCESS_STATUS:
  1924. /* /sys/kernel/security/tomoyo/.process_status */
  1925. head->write = tomoyo_write_pid;
  1926. head->read = tomoyo_read_pid;
  1927. break;
  1928. case TOMOYO_VERSION:
  1929. /* /sys/kernel/security/tomoyo/version */
  1930. head->read = tomoyo_read_version;
  1931. head->readbuf_size = 128;
  1932. break;
  1933. case TOMOYO_STAT:
  1934. /* /sys/kernel/security/tomoyo/stat */
  1935. head->write = tomoyo_write_stat;
  1936. head->read = tomoyo_read_stat;
  1937. head->readbuf_size = 1024;
  1938. break;
  1939. case TOMOYO_PROFILE:
  1940. /* /sys/kernel/security/tomoyo/profile */
  1941. head->write = tomoyo_write_profile;
  1942. head->read = tomoyo_read_profile;
  1943. break;
  1944. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  1945. head->poll = tomoyo_poll_query;
  1946. head->write = tomoyo_write_answer;
  1947. head->read = tomoyo_read_query;
  1948. break;
  1949. case TOMOYO_MANAGER:
  1950. /* /sys/kernel/security/tomoyo/manager */
  1951. head->write = tomoyo_write_manager;
  1952. head->read = tomoyo_read_manager;
  1953. break;
  1954. }
  1955. if (!(file->f_mode & FMODE_READ)) {
  1956. /*
  1957. * No need to allocate read_buf since it is not opened
  1958. * for reading.
  1959. */
  1960. head->read = NULL;
  1961. head->poll = NULL;
  1962. } else if (!head->poll) {
  1963. /* Don't allocate read_buf for poll() access. */
  1964. if (!head->readbuf_size)
  1965. head->readbuf_size = 4096 * 2;
  1966. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  1967. if (!head->read_buf) {
  1968. kfree(head);
  1969. return -ENOMEM;
  1970. }
  1971. }
  1972. if (!(file->f_mode & FMODE_WRITE)) {
  1973. /*
  1974. * No need to allocate write_buf since it is not opened
  1975. * for writing.
  1976. */
  1977. head->write = NULL;
  1978. } else if (head->write) {
  1979. head->writebuf_size = 4096 * 2;
  1980. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  1981. if (!head->write_buf) {
  1982. kfree(head->read_buf);
  1983. kfree(head);
  1984. return -ENOMEM;
  1985. }
  1986. }
  1987. /*
  1988. * If the file is /sys/kernel/security/tomoyo/query , increment the
  1989. * observer counter.
  1990. * The obserber counter is used by tomoyo_supervisor() to see if
  1991. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  1992. */
  1993. if (type == TOMOYO_QUERY)
  1994. atomic_inc(&tomoyo_query_observers);
  1995. file->private_data = head;
  1996. tomoyo_notify_gc(head, true);
  1997. return 0;
  1998. }
  1999. /**
  2000. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  2001. *
  2002. * @file: Pointer to "struct file".
  2003. * @wait: Pointer to "poll_table".
  2004. *
  2005. * Waits for read readiness.
  2006. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
  2007. * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
  2008. */
  2009. int tomoyo_poll_control(struct file *file, poll_table *wait)
  2010. {
  2011. struct tomoyo_io_buffer *head = file->private_data;
  2012. if (!head->poll)
  2013. return -ENOSYS;
  2014. return head->poll(file, wait);
  2015. }
  2016. /**
  2017. * tomoyo_set_namespace_cursor - Set namespace to read.
  2018. *
  2019. * @head: Pointer to "struct tomoyo_io_buffer".
  2020. *
  2021. * Returns nothing.
  2022. */
  2023. static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
  2024. {
  2025. struct list_head *ns;
  2026. if (head->type != TOMOYO_EXCEPTIONPOLICY &&
  2027. head->type != TOMOYO_PROFILE)
  2028. return;
  2029. /*
  2030. * If this is the first read, or reading previous namespace finished
  2031. * and has more namespaces to read, update the namespace cursor.
  2032. */
  2033. ns = head->r.ns;
  2034. if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
  2035. /* Clearing is OK because tomoyo_flush() returned true. */
  2036. memset(&head->r, 0, sizeof(head->r));
  2037. head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
  2038. }
  2039. }
  2040. /**
  2041. * tomoyo_has_more_namespace - Check for unread namespaces.
  2042. *
  2043. * @head: Pointer to "struct tomoyo_io_buffer".
  2044. *
  2045. * Returns true if we have more entries to print, false otherwise.
  2046. */
  2047. static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
  2048. {
  2049. return (head->type == TOMOYO_EXCEPTIONPOLICY ||
  2050. head->type == TOMOYO_PROFILE) && head->r.eof &&
  2051. head->r.ns->next != &tomoyo_namespace_list;
  2052. }
  2053. /**
  2054. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  2055. *
  2056. * @head: Pointer to "struct tomoyo_io_buffer".
  2057. * @buffer: Poiner to buffer to write to.
  2058. * @buffer_len: Size of @buffer.
  2059. *
  2060. * Returns bytes read on success, negative value otherwise.
  2061. */
  2062. ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  2063. const int buffer_len)
  2064. {
  2065. int len;
  2066. int idx;
  2067. if (!head->read)
  2068. return -ENOSYS;
  2069. if (mutex_lock_interruptible(&head->io_sem))
  2070. return -EINTR;
  2071. head->read_user_buf = buffer;
  2072. head->read_user_buf_avail = buffer_len;
  2073. idx = tomoyo_read_lock();
  2074. if (tomoyo_flush(head))
  2075. /* Call the policy handler. */
  2076. do {
  2077. tomoyo_set_namespace_cursor(head);
  2078. head->read(head);
  2079. } while (tomoyo_flush(head) &&
  2080. tomoyo_has_more_namespace(head));
  2081. tomoyo_read_unlock(idx);
  2082. len = head->read_user_buf - buffer;
  2083. mutex_unlock(&head->io_sem);
  2084. return len;
  2085. }
  2086. /**
  2087. * tomoyo_parse_policy - Parse a policy line.
  2088. *
  2089. * @head: Poiter to "struct tomoyo_io_buffer".
  2090. * @line: Line to parse.
  2091. *
  2092. * Returns 0 on success, negative value otherwise.
  2093. *
  2094. * Caller holds tomoyo_read_lock().
  2095. */
  2096. static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
  2097. {
  2098. /* Delete request? */
  2099. head->w.is_delete = !strncmp(line, "delete ", 7);
  2100. if (head->w.is_delete)
  2101. memmove(line, line + 7, strlen(line + 7) + 1);
  2102. /* Selecting namespace to update. */
  2103. if (head->type == TOMOYO_EXCEPTIONPOLICY ||
  2104. head->type == TOMOYO_PROFILE) {
  2105. if (*line == '<') {
  2106. char *cp = strchr(line, ' ');
  2107. if (cp) {
  2108. *cp++ = '\0';
  2109. head->w.ns = tomoyo_assign_namespace(line);
  2110. memmove(line, cp, strlen(cp) + 1);
  2111. } else
  2112. head->w.ns = NULL;
  2113. } else
  2114. head->w.ns = &tomoyo_kernel_namespace;
  2115. /* Don't allow updating if namespace is invalid. */
  2116. if (!head->w.ns)
  2117. return -ENOENT;
  2118. }
  2119. /* Do the update. */
  2120. return head->write(head);
  2121. }
  2122. /**
  2123. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  2124. *
  2125. * @head: Pointer to "struct tomoyo_io_buffer".
  2126. * @buffer: Pointer to buffer to read from.
  2127. * @buffer_len: Size of @buffer.
  2128. *
  2129. * Returns @buffer_len on success, negative value otherwise.
  2130. */
  2131. ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
  2132. const char __user *buffer, const int buffer_len)
  2133. {
  2134. int error = buffer_len;
  2135. size_t avail_len = buffer_len;
  2136. char *cp0 = head->write_buf;
  2137. int idx;
  2138. if (!head->write)
  2139. return -ENOSYS;
  2140. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  2141. return -EFAULT;
  2142. if (mutex_lock_interruptible(&head->io_sem))
  2143. return -EINTR;
  2144. idx = tomoyo_read_lock();
  2145. /* Read a line and dispatch it to the policy handler. */
  2146. while (avail_len > 0) {
  2147. char c;
  2148. if (head->w.avail >= head->writebuf_size - 1) {
  2149. const int len = head->writebuf_size * 2;
  2150. char *cp = kzalloc(len, GFP_NOFS);
  2151. if (!cp) {
  2152. error = -ENOMEM;
  2153. break;
  2154. }
  2155. memmove(cp, cp0, head->w.avail);
  2156. kfree(cp0);
  2157. head->write_buf = cp;
  2158. cp0 = cp;
  2159. head->writebuf_size = len;
  2160. }
  2161. if (get_user(c, buffer)) {
  2162. error = -EFAULT;
  2163. break;
  2164. }
  2165. buffer++;
  2166. avail_len--;
  2167. cp0[head->w.avail++] = c;
  2168. if (c != '\n')
  2169. continue;
  2170. cp0[head->w.avail - 1] = '\0';
  2171. head->w.avail = 0;
  2172. tomoyo_normalize_line(cp0);
  2173. if (!strcmp(cp0, "reset")) {
  2174. head->w.ns = &tomoyo_kernel_namespace;
  2175. head->w.domain = NULL;
  2176. memset(&head->r, 0, sizeof(head->r));
  2177. continue;
  2178. }
  2179. /* Don't allow updating policies by non manager programs. */
  2180. switch (head->type) {
  2181. case TOMOYO_PROCESS_STATUS:
  2182. /* This does not write anything. */
  2183. break;
  2184. case TOMOYO_DOMAINPOLICY:
  2185. if (tomoyo_select_domain(head, cp0))
  2186. continue;
  2187. /* fall through */
  2188. case TOMOYO_EXCEPTIONPOLICY:
  2189. if (!strcmp(cp0, "select transition_only")) {
  2190. head->r.print_transition_related_only = true;
  2191. continue;
  2192. }
  2193. /* fall through */
  2194. default:
  2195. if (!tomoyo_manager()) {
  2196. error = -EPERM;
  2197. goto out;
  2198. }
  2199. }
  2200. switch (tomoyo_parse_policy(head, cp0)) {
  2201. case -EPERM:
  2202. error = -EPERM;
  2203. goto out;
  2204. case 0:
  2205. switch (head->type) {
  2206. case TOMOYO_DOMAINPOLICY:
  2207. case TOMOYO_EXCEPTIONPOLICY:
  2208. case TOMOYO_DOMAIN_STATUS:
  2209. case TOMOYO_STAT:
  2210. case TOMOYO_PROFILE:
  2211. case TOMOYO_MANAGER:
  2212. tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
  2213. break;
  2214. default:
  2215. break;
  2216. }
  2217. break;
  2218. }
  2219. }
  2220. out:
  2221. tomoyo_read_unlock(idx);
  2222. mutex_unlock(&head->io_sem);
  2223. return error;
  2224. }
  2225. /**
  2226. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  2227. *
  2228. * @head: Pointer to "struct tomoyo_io_buffer".
  2229. *
  2230. * Returns 0.
  2231. */
  2232. int tomoyo_close_control(struct tomoyo_io_buffer *head)
  2233. {
  2234. /*
  2235. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  2236. * observer counter.
  2237. */
  2238. if (head->type == TOMOYO_QUERY &&
  2239. atomic_dec_and_test(&tomoyo_query_observers))
  2240. wake_up_all(&tomoyo_answer_wait);
  2241. tomoyo_notify_gc(head, false);
  2242. return 0;
  2243. }
  2244. /**
  2245. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  2246. */
  2247. void tomoyo_check_profile(void)
  2248. {
  2249. struct tomoyo_domain_info *domain;
  2250. const int idx = tomoyo_read_lock();
  2251. tomoyo_policy_loaded = true;
  2252. printk(KERN_INFO "TOMOYO: 2.4.0\n");
  2253. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  2254. const u8 profile = domain->profile;
  2255. const struct tomoyo_policy_namespace *ns = domain->ns;
  2256. if (ns->profile_version != 20100903)
  2257. printk(KERN_ERR
  2258. "Profile version %u is not supported.\n",
  2259. ns->profile_version);
  2260. else if (!ns->profile_ptr[profile])
  2261. printk(KERN_ERR
  2262. "Profile %u (used by '%s') is not defined.\n",
  2263. profile, domain->domainname->name);
  2264. else
  2265. continue;
  2266. printk(KERN_ERR
  2267. "Userland tools for TOMOYO 2.4 must be installed and "
  2268. "policy must be initialized.\n");
  2269. printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ "
  2270. "for more information.\n");
  2271. panic("STOP!");
  2272. }
  2273. tomoyo_read_unlock(idx);
  2274. printk(KERN_INFO "Mandatory Access Control activated.\n");
  2275. }
  2276. /**
  2277. * tomoyo_load_builtin_policy - Load built-in policy.
  2278. *
  2279. * Returns nothing.
  2280. */
  2281. void __init tomoyo_load_builtin_policy(void)
  2282. {
  2283. /*
  2284. * This include file is manually created and contains built-in policy
  2285. * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
  2286. * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
  2287. * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
  2288. */
  2289. #include "builtin-policy.h"
  2290. u8 i;
  2291. const int idx = tomoyo_read_lock();
  2292. for (i = 0; i < 5; i++) {
  2293. struct tomoyo_io_buffer head = { };
  2294. char *start = "";
  2295. switch (i) {
  2296. case 0:
  2297. start = tomoyo_builtin_profile;
  2298. head.type = TOMOYO_PROFILE;
  2299. head.write = tomoyo_write_profile;
  2300. break;
  2301. case 1:
  2302. start = tomoyo_builtin_exception_policy;
  2303. head.type = TOMOYO_EXCEPTIONPOLICY;
  2304. head.write = tomoyo_write_exception;
  2305. break;
  2306. case 2:
  2307. start = tomoyo_builtin_domain_policy;
  2308. head.type = TOMOYO_DOMAINPOLICY;
  2309. head.write = tomoyo_write_domain;
  2310. break;
  2311. case 3:
  2312. start = tomoyo_builtin_manager;
  2313. head.type = TOMOYO_MANAGER;
  2314. head.write = tomoyo_write_manager;
  2315. break;
  2316. case 4:
  2317. start = tomoyo_builtin_stat;
  2318. head.type = TOMOYO_STAT;
  2319. head.write = tomoyo_write_stat;
  2320. break;
  2321. }
  2322. while (1) {
  2323. char *end = strchr(start, '\n');
  2324. if (!end)
  2325. break;
  2326. *end = '\0';
  2327. tomoyo_normalize_line(start);
  2328. head.write_buf = start;
  2329. tomoyo_parse_policy(&head, start);
  2330. start = end + 1;
  2331. }
  2332. }
  2333. tomoyo_read_unlock(idx);
  2334. #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
  2335. tomoyo_check_profile();
  2336. #endif
  2337. }