common.c 76 KB

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