common.c 76 KB

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