common.c 75 KB

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