common.c 76 KB

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