common.c 75 KB

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