common.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  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. return;
  719. head->r.step++;
  720. break;
  721. case 2:
  722. {
  723. u8 i;
  724. const struct tomoyo_path_info *comment =
  725. profile->comment;
  726. tomoyo_print_namespace(head);
  727. tomoyo_io_printf(head, "%u-COMMENT=", index);
  728. tomoyo_set_string(head, comment ? comment->name : "");
  729. tomoyo_set_lf(head);
  730. tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
  731. for (i = 0; i < TOMOYO_MAX_PREF; i++)
  732. tomoyo_io_printf(head, "%s=%u ",
  733. tomoyo_pref_keywords[i],
  734. profile->pref[i]);
  735. tomoyo_set_string(head, "}\n");
  736. head->r.step++;
  737. }
  738. break;
  739. case 3:
  740. {
  741. tomoyo_print_namespace(head);
  742. tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
  743. tomoyo_print_config(head, profile->default_config);
  744. head->r.bit = 0;
  745. head->r.step++;
  746. }
  747. break;
  748. case 4:
  749. for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
  750. + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
  751. const u8 i = head->r.bit;
  752. const u8 config = profile->config[i];
  753. if (config == TOMOYO_CONFIG_USE_DEFAULT)
  754. continue;
  755. tomoyo_print_namespace(head);
  756. if (i < TOMOYO_MAX_MAC_INDEX)
  757. tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
  758. index,
  759. tomoyo_category_keywords
  760. [tomoyo_index2category[i]],
  761. tomoyo_mac_keywords[i]);
  762. else
  763. tomoyo_io_printf(head, "%u-CONFIG::%s", index,
  764. tomoyo_mac_keywords[i]);
  765. tomoyo_print_config(head, config);
  766. head->r.bit++;
  767. break;
  768. }
  769. if (head->r.bit == TOMOYO_MAX_MAC_INDEX
  770. + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
  771. head->r.index++;
  772. head->r.step = 1;
  773. }
  774. break;
  775. }
  776. if (tomoyo_flush(head))
  777. goto next;
  778. }
  779. /**
  780. * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
  781. *
  782. * @a: Pointer to "struct tomoyo_acl_head".
  783. * @b: Pointer to "struct tomoyo_acl_head".
  784. *
  785. * Returns true if @a == @b, false otherwise.
  786. */
  787. static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
  788. const struct tomoyo_acl_head *b)
  789. {
  790. return container_of(a, struct tomoyo_manager, head)->manager ==
  791. container_of(b, struct tomoyo_manager, head)->manager;
  792. }
  793. /**
  794. * tomoyo_update_manager_entry - Add a manager entry.
  795. *
  796. * @manager: The path to manager or the domainnamme.
  797. * @is_delete: True if it is a delete request.
  798. *
  799. * Returns 0 on success, negative value otherwise.
  800. *
  801. * Caller holds tomoyo_read_lock().
  802. */
  803. static int tomoyo_update_manager_entry(const char *manager,
  804. const bool is_delete)
  805. {
  806. struct tomoyo_manager e = { };
  807. struct tomoyo_acl_param param = {
  808. /* .ns = &tomoyo_kernel_namespace, */
  809. .is_delete = is_delete,
  810. .list = &tomoyo_kernel_namespace.
  811. policy_list[TOMOYO_ID_MANAGER],
  812. };
  813. int error = is_delete ? -ENOENT : -ENOMEM;
  814. if (tomoyo_domain_def(manager)) {
  815. if (!tomoyo_correct_domain(manager))
  816. return -EINVAL;
  817. e.is_domain = true;
  818. } else {
  819. if (!tomoyo_correct_path(manager))
  820. return -EINVAL;
  821. }
  822. e.manager = tomoyo_get_name(manager);
  823. if (e.manager) {
  824. error = tomoyo_update_policy(&e.head, sizeof(e), &param,
  825. tomoyo_same_manager);
  826. tomoyo_put_name(e.manager);
  827. }
  828. return error;
  829. }
  830. /**
  831. * tomoyo_write_manager - Write manager policy.
  832. *
  833. * @head: Pointer to "struct tomoyo_io_buffer".
  834. *
  835. * Returns 0 on success, negative value otherwise.
  836. *
  837. * Caller holds tomoyo_read_lock().
  838. */
  839. static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
  840. {
  841. char *data = head->write_buf;
  842. if (!strcmp(data, "manage_by_non_root")) {
  843. tomoyo_manage_by_non_root = !head->w.is_delete;
  844. return 0;
  845. }
  846. return tomoyo_update_manager_entry(data, head->w.is_delete);
  847. }
  848. /**
  849. * tomoyo_read_manager - Read manager policy.
  850. *
  851. * @head: Pointer to "struct tomoyo_io_buffer".
  852. *
  853. * Caller holds tomoyo_read_lock().
  854. */
  855. static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
  856. {
  857. if (head->r.eof)
  858. return;
  859. list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
  860. policy_list[TOMOYO_ID_MANAGER]) {
  861. struct tomoyo_manager *ptr =
  862. list_entry(head->r.acl, typeof(*ptr), head.list);
  863. if (ptr->head.is_deleted)
  864. continue;
  865. if (!tomoyo_flush(head))
  866. return;
  867. tomoyo_set_string(head, ptr->manager->name);
  868. tomoyo_set_lf(head);
  869. }
  870. head->r.eof = true;
  871. }
  872. /**
  873. * tomoyo_manager - Check whether the current process is a policy manager.
  874. *
  875. * Returns true if the current process is permitted to modify policy
  876. * via /sys/kernel/security/tomoyo/ interface.
  877. *
  878. * Caller holds tomoyo_read_lock().
  879. */
  880. static bool tomoyo_manager(void)
  881. {
  882. struct tomoyo_manager *ptr;
  883. const char *exe;
  884. const struct task_struct *task = current;
  885. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  886. bool found = false;
  887. if (!tomoyo_policy_loaded)
  888. return true;
  889. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  890. return false;
  891. list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
  892. policy_list[TOMOYO_ID_MANAGER], head.list) {
  893. if (!ptr->head.is_deleted && ptr->is_domain
  894. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  895. found = true;
  896. break;
  897. }
  898. }
  899. if (found)
  900. return true;
  901. exe = tomoyo_get_exe();
  902. if (!exe)
  903. return false;
  904. list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
  905. policy_list[TOMOYO_ID_MANAGER], head.list) {
  906. if (!ptr->head.is_deleted && !ptr->is_domain
  907. && !strcmp(exe, ptr->manager->name)) {
  908. found = true;
  909. break;
  910. }
  911. }
  912. if (!found) { /* Reduce error messages. */
  913. static pid_t last_pid;
  914. const pid_t pid = current->pid;
  915. if (last_pid != pid) {
  916. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  917. "update policies.\n", domainname->name, exe);
  918. last_pid = pid;
  919. }
  920. }
  921. kfree(exe);
  922. return found;
  923. }
  924. /**
  925. * tomoyo_select_domain - Parse select command.
  926. *
  927. * @head: Pointer to "struct tomoyo_io_buffer".
  928. * @data: String to parse.
  929. *
  930. * Returns true on success, false otherwise.
  931. *
  932. * Caller holds tomoyo_read_lock().
  933. */
  934. static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
  935. const char *data)
  936. {
  937. unsigned int pid;
  938. struct tomoyo_domain_info *domain = NULL;
  939. bool global_pid = false;
  940. if (strncmp(data, "select ", 7))
  941. return false;
  942. data += 7;
  943. if (sscanf(data, "pid=%u", &pid) == 1 ||
  944. (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
  945. struct task_struct *p;
  946. rcu_read_lock();
  947. if (global_pid)
  948. p = find_task_by_pid_ns(pid, &init_pid_ns);
  949. else
  950. p = find_task_by_vpid(pid);
  951. if (p)
  952. domain = tomoyo_real_domain(p);
  953. rcu_read_unlock();
  954. } else if (!strncmp(data, "domain=", 7)) {
  955. if (tomoyo_domain_def(data + 7))
  956. domain = tomoyo_find_domain(data + 7);
  957. } else
  958. return false;
  959. head->w.domain = domain;
  960. /* Accessing read_buf is safe because head->io_sem is held. */
  961. if (!head->read_buf)
  962. return true; /* Do nothing if open(O_WRONLY). */
  963. memset(&head->r, 0, sizeof(head->r));
  964. head->r.print_this_domain_only = true;
  965. if (domain)
  966. head->r.domain = &domain->list;
  967. else
  968. head->r.eof = 1;
  969. tomoyo_io_printf(head, "# select %s\n", data);
  970. if (domain && domain->is_deleted)
  971. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  972. return true;
  973. }
  974. /**
  975. * tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
  976. *
  977. * @a: Pointer to "struct tomoyo_acl_info".
  978. * @b: Pointer to "struct tomoyo_acl_info".
  979. *
  980. * Returns true if @a == @b, false otherwise.
  981. */
  982. static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
  983. const struct tomoyo_acl_info *b)
  984. {
  985. const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
  986. const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
  987. return p1->domainname == p2->domainname;
  988. }
  989. /**
  990. * tomoyo_write_task - Update task related list.
  991. *
  992. * @param: Pointer to "struct tomoyo_acl_param".
  993. *
  994. * Returns 0 on success, negative value otherwise.
  995. *
  996. * Caller holds tomoyo_read_lock().
  997. */
  998. static int tomoyo_write_task(struct tomoyo_acl_param *param)
  999. {
  1000. int error = -EINVAL;
  1001. if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
  1002. struct tomoyo_task_acl e = {
  1003. .head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
  1004. .domainname = tomoyo_get_domainname(param),
  1005. };
  1006. if (e.domainname)
  1007. error = tomoyo_update_domain(&e.head, sizeof(e), param,
  1008. tomoyo_same_task_acl,
  1009. NULL);
  1010. tomoyo_put_name(e.domainname);
  1011. }
  1012. return error;
  1013. }
  1014. /**
  1015. * tomoyo_delete_domain - Delete a domain.
  1016. *
  1017. * @domainname: The name of domain.
  1018. *
  1019. * Returns 0.
  1020. *
  1021. * Caller holds tomoyo_read_lock().
  1022. */
  1023. static int tomoyo_delete_domain(char *domainname)
  1024. {
  1025. struct tomoyo_domain_info *domain;
  1026. struct tomoyo_path_info name;
  1027. name.name = domainname;
  1028. tomoyo_fill_path_info(&name);
  1029. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  1030. return 0;
  1031. /* Is there an active domain? */
  1032. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1033. /* Never delete tomoyo_kernel_domain */
  1034. if (domain == &tomoyo_kernel_domain)
  1035. continue;
  1036. if (domain->is_deleted ||
  1037. tomoyo_pathcmp(domain->domainname, &name))
  1038. continue;
  1039. domain->is_deleted = true;
  1040. break;
  1041. }
  1042. mutex_unlock(&tomoyo_policy_lock);
  1043. return 0;
  1044. }
  1045. /**
  1046. * tomoyo_write_domain2 - Write domain policy.
  1047. *
  1048. * @ns: Pointer to "struct tomoyo_policy_namespace".
  1049. * @list: Pointer to "struct list_head".
  1050. * @data: Policy to be interpreted.
  1051. * @is_delete: True if it is a delete request.
  1052. *
  1053. * Returns 0 on success, negative value otherwise.
  1054. *
  1055. * Caller holds tomoyo_read_lock().
  1056. */
  1057. static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
  1058. struct list_head *list, char *data,
  1059. const bool is_delete)
  1060. {
  1061. struct tomoyo_acl_param param = {
  1062. .ns = ns,
  1063. .list = list,
  1064. .data = data,
  1065. .is_delete = is_delete,
  1066. };
  1067. static const struct {
  1068. const char *keyword;
  1069. int (*write) (struct tomoyo_acl_param *);
  1070. } tomoyo_callback[5] = {
  1071. { "file ", tomoyo_write_file },
  1072. { "network inet ", tomoyo_write_inet_network },
  1073. { "network unix ", tomoyo_write_unix_network },
  1074. { "misc ", tomoyo_write_misc },
  1075. { "task ", tomoyo_write_task },
  1076. };
  1077. u8 i;
  1078. for (i = 0; i < ARRAY_SIZE(tomoyo_callback); i++) {
  1079. if (!tomoyo_str_starts(&param.data,
  1080. tomoyo_callback[i].keyword))
  1081. continue;
  1082. return tomoyo_callback[i].write(&param);
  1083. }
  1084. return -EINVAL;
  1085. }
  1086. /* String table for domain flags. */
  1087. const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
  1088. [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n",
  1089. [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
  1090. };
  1091. /**
  1092. * tomoyo_write_domain - Write domain policy.
  1093. *
  1094. * @head: Pointer to "struct tomoyo_io_buffer".
  1095. *
  1096. * Returns 0 on success, negative value otherwise.
  1097. *
  1098. * Caller holds tomoyo_read_lock().
  1099. */
  1100. static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
  1101. {
  1102. char *data = head->write_buf;
  1103. struct tomoyo_policy_namespace *ns;
  1104. struct tomoyo_domain_info *domain = head->w.domain;
  1105. const bool is_delete = head->w.is_delete;
  1106. bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
  1107. unsigned int profile;
  1108. if (*data == '<') {
  1109. domain = NULL;
  1110. if (is_delete)
  1111. tomoyo_delete_domain(data);
  1112. else if (is_select)
  1113. domain = tomoyo_find_domain(data);
  1114. else
  1115. domain = tomoyo_assign_domain(data, false);
  1116. head->w.domain = domain;
  1117. return 0;
  1118. }
  1119. if (!domain)
  1120. return -EINVAL;
  1121. ns = domain->ns;
  1122. if (sscanf(data, "use_profile %u", &profile) == 1
  1123. && profile < TOMOYO_MAX_PROFILES) {
  1124. if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
  1125. domain->profile = (u8) profile;
  1126. return 0;
  1127. }
  1128. if (sscanf(data, "use_group %u\n", &profile) == 1
  1129. && profile < TOMOYO_MAX_ACL_GROUPS) {
  1130. if (!is_delete)
  1131. domain->group = (u8) profile;
  1132. return 0;
  1133. }
  1134. for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
  1135. const char *cp = tomoyo_dif[profile];
  1136. if (strncmp(data, cp, strlen(cp) - 1))
  1137. continue;
  1138. domain->flags[profile] = !is_delete;
  1139. return 0;
  1140. }
  1141. return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
  1142. is_delete);
  1143. }
  1144. /**
  1145. * tomoyo_print_condition - Print condition part.
  1146. *
  1147. * @head: Pointer to "struct tomoyo_io_buffer".
  1148. * @cond: Pointer to "struct tomoyo_condition".
  1149. *
  1150. * Returns true on success, false otherwise.
  1151. */
  1152. static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
  1153. const struct tomoyo_condition *cond)
  1154. {
  1155. switch (head->r.cond_step) {
  1156. case 0:
  1157. head->r.cond_index = 0;
  1158. head->r.cond_step++;
  1159. if (cond->transit) {
  1160. tomoyo_set_space(head);
  1161. tomoyo_set_string(head, cond->transit->name);
  1162. }
  1163. /* fall through */
  1164. case 1:
  1165. {
  1166. const u16 condc = cond->condc;
  1167. const struct tomoyo_condition_element *condp =
  1168. (typeof(condp)) (cond + 1);
  1169. const struct tomoyo_number_union *numbers_p =
  1170. (typeof(numbers_p)) (condp + condc);
  1171. const struct tomoyo_name_union *names_p =
  1172. (typeof(names_p))
  1173. (numbers_p + cond->numbers_count);
  1174. const struct tomoyo_argv *argv =
  1175. (typeof(argv)) (names_p + cond->names_count);
  1176. const struct tomoyo_envp *envp =
  1177. (typeof(envp)) (argv + cond->argc);
  1178. u16 skip;
  1179. for (skip = 0; skip < head->r.cond_index; skip++) {
  1180. const u8 left = condp->left;
  1181. const u8 right = condp->right;
  1182. condp++;
  1183. switch (left) {
  1184. case TOMOYO_ARGV_ENTRY:
  1185. argv++;
  1186. continue;
  1187. case TOMOYO_ENVP_ENTRY:
  1188. envp++;
  1189. continue;
  1190. case TOMOYO_NUMBER_UNION:
  1191. numbers_p++;
  1192. break;
  1193. }
  1194. switch (right) {
  1195. case TOMOYO_NAME_UNION:
  1196. names_p++;
  1197. break;
  1198. case TOMOYO_NUMBER_UNION:
  1199. numbers_p++;
  1200. break;
  1201. }
  1202. }
  1203. while (head->r.cond_index < condc) {
  1204. const u8 match = condp->equals;
  1205. const u8 left = condp->left;
  1206. const u8 right = condp->right;
  1207. if (!tomoyo_flush(head))
  1208. return false;
  1209. condp++;
  1210. head->r.cond_index++;
  1211. tomoyo_set_space(head);
  1212. switch (left) {
  1213. case TOMOYO_ARGV_ENTRY:
  1214. tomoyo_io_printf(head,
  1215. "exec.argv[%lu]%s=\"",
  1216. argv->index, argv->
  1217. is_not ? "!" : "");
  1218. tomoyo_set_string(head,
  1219. argv->value->name);
  1220. tomoyo_set_string(head, "\"");
  1221. argv++;
  1222. continue;
  1223. case TOMOYO_ENVP_ENTRY:
  1224. tomoyo_set_string(head,
  1225. "exec.envp[\"");
  1226. tomoyo_set_string(head,
  1227. envp->name->name);
  1228. tomoyo_io_printf(head, "\"]%s=", envp->
  1229. is_not ? "!" : "");
  1230. if (envp->value) {
  1231. tomoyo_set_string(head, "\"");
  1232. tomoyo_set_string(head, envp->
  1233. value->name);
  1234. tomoyo_set_string(head, "\"");
  1235. } else {
  1236. tomoyo_set_string(head,
  1237. "NULL");
  1238. }
  1239. envp++;
  1240. continue;
  1241. case TOMOYO_NUMBER_UNION:
  1242. tomoyo_print_number_union_nospace
  1243. (head, numbers_p++);
  1244. break;
  1245. default:
  1246. tomoyo_set_string(head,
  1247. tomoyo_condition_keyword[left]);
  1248. break;
  1249. }
  1250. tomoyo_set_string(head, match ? "=" : "!=");
  1251. switch (right) {
  1252. case TOMOYO_NAME_UNION:
  1253. tomoyo_print_name_union_quoted
  1254. (head, names_p++);
  1255. break;
  1256. case TOMOYO_NUMBER_UNION:
  1257. tomoyo_print_number_union_nospace
  1258. (head, numbers_p++);
  1259. break;
  1260. default:
  1261. tomoyo_set_string(head,
  1262. tomoyo_condition_keyword[right]);
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. head->r.cond_step++;
  1268. /* fall through */
  1269. case 2:
  1270. if (!tomoyo_flush(head))
  1271. break;
  1272. head->r.cond_step++;
  1273. /* fall through */
  1274. case 3:
  1275. if (cond->grant_log != TOMOYO_GRANTLOG_AUTO)
  1276. tomoyo_io_printf(head, " grant_log=%s",
  1277. tomoyo_yesno(cond->grant_log ==
  1278. TOMOYO_GRANTLOG_YES));
  1279. tomoyo_set_lf(head);
  1280. return true;
  1281. }
  1282. return false;
  1283. }
  1284. /**
  1285. * tomoyo_set_group - Print "acl_group " header keyword and category name.
  1286. *
  1287. * @head: Pointer to "struct tomoyo_io_buffer".
  1288. * @category: Category name.
  1289. *
  1290. * Returns nothing.
  1291. */
  1292. static void tomoyo_set_group(struct tomoyo_io_buffer *head,
  1293. const char *category)
  1294. {
  1295. if (head->type == TOMOYO_EXCEPTIONPOLICY) {
  1296. tomoyo_print_namespace(head);
  1297. tomoyo_io_printf(head, "acl_group %u ",
  1298. head->r.acl_group_index);
  1299. }
  1300. tomoyo_set_string(head, category);
  1301. }
  1302. /**
  1303. * tomoyo_print_entry - Print an ACL entry.
  1304. *
  1305. * @head: Pointer to "struct tomoyo_io_buffer".
  1306. * @acl: Pointer to an ACL entry.
  1307. *
  1308. * Returns true on success, false otherwise.
  1309. */
  1310. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  1311. struct tomoyo_acl_info *acl)
  1312. {
  1313. const u8 acl_type = acl->type;
  1314. bool first = true;
  1315. u8 bit;
  1316. if (head->r.print_cond_part)
  1317. goto print_cond_part;
  1318. if (acl->is_deleted)
  1319. return true;
  1320. if (!tomoyo_flush(head))
  1321. return false;
  1322. else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  1323. struct tomoyo_path_acl *ptr =
  1324. container_of(acl, typeof(*ptr), head);
  1325. const u16 perm = ptr->perm;
  1326. for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  1327. if (!(perm & (1 << bit)))
  1328. continue;
  1329. if (head->r.print_transition_related_only &&
  1330. bit != TOMOYO_TYPE_EXECUTE)
  1331. continue;
  1332. if (first) {
  1333. tomoyo_set_group(head, "file ");
  1334. first = false;
  1335. } else {
  1336. tomoyo_set_slash(head);
  1337. }
  1338. tomoyo_set_string(head, tomoyo_path_keyword[bit]);
  1339. }
  1340. if (first)
  1341. return true;
  1342. tomoyo_print_name_union(head, &ptr->name);
  1343. } else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
  1344. struct tomoyo_task_acl *ptr =
  1345. container_of(acl, typeof(*ptr), head);
  1346. tomoyo_set_group(head, "task ");
  1347. tomoyo_set_string(head, "manual_domain_transition ");
  1348. tomoyo_set_string(head, ptr->domainname->name);
  1349. } else if (head->r.print_transition_related_only) {
  1350. return true;
  1351. } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
  1352. struct tomoyo_path2_acl *ptr =
  1353. container_of(acl, typeof(*ptr), head);
  1354. const u8 perm = ptr->perm;
  1355. for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
  1356. if (!(perm & (1 << bit)))
  1357. continue;
  1358. if (first) {
  1359. tomoyo_set_group(head, "file ");
  1360. first = false;
  1361. } else {
  1362. tomoyo_set_slash(head);
  1363. }
  1364. tomoyo_set_string(head, tomoyo_mac_keywords
  1365. [tomoyo_pp2mac[bit]]);
  1366. }
  1367. if (first)
  1368. return true;
  1369. tomoyo_print_name_union(head, &ptr->name1);
  1370. tomoyo_print_name_union(head, &ptr->name2);
  1371. } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
  1372. struct tomoyo_path_number_acl *ptr =
  1373. container_of(acl, typeof(*ptr), head);
  1374. const u8 perm = ptr->perm;
  1375. for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
  1376. if (!(perm & (1 << bit)))
  1377. continue;
  1378. if (first) {
  1379. tomoyo_set_group(head, "file ");
  1380. first = false;
  1381. } else {
  1382. tomoyo_set_slash(head);
  1383. }
  1384. tomoyo_set_string(head, tomoyo_mac_keywords
  1385. [tomoyo_pn2mac[bit]]);
  1386. }
  1387. if (first)
  1388. return true;
  1389. tomoyo_print_name_union(head, &ptr->name);
  1390. tomoyo_print_number_union(head, &ptr->number);
  1391. } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
  1392. struct tomoyo_mkdev_acl *ptr =
  1393. container_of(acl, typeof(*ptr), head);
  1394. const u8 perm = ptr->perm;
  1395. for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
  1396. if (!(perm & (1 << bit)))
  1397. continue;
  1398. if (first) {
  1399. tomoyo_set_group(head, "file ");
  1400. first = false;
  1401. } else {
  1402. tomoyo_set_slash(head);
  1403. }
  1404. tomoyo_set_string(head, tomoyo_mac_keywords
  1405. [tomoyo_pnnn2mac[bit]]);
  1406. }
  1407. if (first)
  1408. return true;
  1409. tomoyo_print_name_union(head, &ptr->name);
  1410. tomoyo_print_number_union(head, &ptr->mode);
  1411. tomoyo_print_number_union(head, &ptr->major);
  1412. tomoyo_print_number_union(head, &ptr->minor);
  1413. } else if (acl_type == TOMOYO_TYPE_INET_ACL) {
  1414. struct tomoyo_inet_acl *ptr =
  1415. container_of(acl, typeof(*ptr), head);
  1416. const u8 perm = ptr->perm;
  1417. for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
  1418. if (!(perm & (1 << bit)))
  1419. continue;
  1420. if (first) {
  1421. tomoyo_set_group(head, "network inet ");
  1422. tomoyo_set_string(head, tomoyo_proto_keyword
  1423. [ptr->protocol]);
  1424. tomoyo_set_space(head);
  1425. first = false;
  1426. } else {
  1427. tomoyo_set_slash(head);
  1428. }
  1429. tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
  1430. }
  1431. if (first)
  1432. return true;
  1433. tomoyo_set_space(head);
  1434. if (ptr->address.group) {
  1435. tomoyo_set_string(head, "@");
  1436. tomoyo_set_string(head, ptr->address.group->group_name
  1437. ->name);
  1438. } else {
  1439. char buf[128];
  1440. tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
  1441. tomoyo_io_printf(head, "%s", buf);
  1442. }
  1443. tomoyo_print_number_union(head, &ptr->port);
  1444. } else if (acl_type == TOMOYO_TYPE_UNIX_ACL) {
  1445. struct tomoyo_unix_acl *ptr =
  1446. container_of(acl, typeof(*ptr), head);
  1447. const u8 perm = ptr->perm;
  1448. for (bit = 0; bit < TOMOYO_MAX_NETWORK_OPERATION; bit++) {
  1449. if (!(perm & (1 << bit)))
  1450. continue;
  1451. if (first) {
  1452. tomoyo_set_group(head, "network unix ");
  1453. tomoyo_set_string(head, tomoyo_proto_keyword
  1454. [ptr->protocol]);
  1455. tomoyo_set_space(head);
  1456. first = false;
  1457. } else {
  1458. tomoyo_set_slash(head);
  1459. }
  1460. tomoyo_set_string(head, tomoyo_socket_keyword[bit]);
  1461. }
  1462. if (first)
  1463. return true;
  1464. tomoyo_print_name_union(head, &ptr->name);
  1465. } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
  1466. struct tomoyo_mount_acl *ptr =
  1467. container_of(acl, typeof(*ptr), head);
  1468. tomoyo_set_group(head, "file mount");
  1469. tomoyo_print_name_union(head, &ptr->dev_name);
  1470. tomoyo_print_name_union(head, &ptr->dir_name);
  1471. tomoyo_print_name_union(head, &ptr->fs_type);
  1472. tomoyo_print_number_union(head, &ptr->flags);
  1473. } else if (acl_type == TOMOYO_TYPE_ENV_ACL) {
  1474. struct tomoyo_env_acl *ptr =
  1475. container_of(acl, typeof(*ptr), head);
  1476. tomoyo_set_group(head, "misc env ");
  1477. tomoyo_set_string(head, ptr->env->name);
  1478. }
  1479. if (acl->cond) {
  1480. head->r.print_cond_part = true;
  1481. head->r.cond_step = 0;
  1482. if (!tomoyo_flush(head))
  1483. return false;
  1484. print_cond_part:
  1485. if (!tomoyo_print_condition(head, acl->cond))
  1486. return false;
  1487. head->r.print_cond_part = false;
  1488. } else {
  1489. tomoyo_set_lf(head);
  1490. }
  1491. return true;
  1492. }
  1493. /**
  1494. * tomoyo_read_domain2 - Read domain policy.
  1495. *
  1496. * @head: Pointer to "struct tomoyo_io_buffer".
  1497. * @list: Pointer to "struct list_head".
  1498. *
  1499. * Caller holds tomoyo_read_lock().
  1500. *
  1501. * Returns true on success, false otherwise.
  1502. */
  1503. static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
  1504. struct list_head *list)
  1505. {
  1506. list_for_each_cookie(head->r.acl, list) {
  1507. struct tomoyo_acl_info *ptr =
  1508. list_entry(head->r.acl, typeof(*ptr), list);
  1509. if (!tomoyo_print_entry(head, ptr))
  1510. return false;
  1511. }
  1512. head->r.acl = NULL;
  1513. return true;
  1514. }
  1515. /**
  1516. * tomoyo_read_domain - Read domain policy.
  1517. *
  1518. * @head: Pointer to "struct tomoyo_io_buffer".
  1519. *
  1520. * Caller holds tomoyo_read_lock().
  1521. */
  1522. static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
  1523. {
  1524. if (head->r.eof)
  1525. return;
  1526. list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
  1527. struct tomoyo_domain_info *domain =
  1528. list_entry(head->r.domain, typeof(*domain), list);
  1529. switch (head->r.step) {
  1530. u8 i;
  1531. case 0:
  1532. if (domain->is_deleted &&
  1533. !head->r.print_this_domain_only)
  1534. continue;
  1535. /* Print domainname and flags. */
  1536. tomoyo_set_string(head, domain->domainname->name);
  1537. tomoyo_set_lf(head);
  1538. tomoyo_io_printf(head, "use_profile %u\n",
  1539. domain->profile);
  1540. tomoyo_io_printf(head, "use_group %u\n",
  1541. domain->group);
  1542. for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
  1543. if (domain->flags[i])
  1544. tomoyo_set_string(head, tomoyo_dif[i]);
  1545. head->r.step++;
  1546. tomoyo_set_lf(head);
  1547. /* fall through */
  1548. case 1:
  1549. if (!tomoyo_read_domain2(head, &domain->acl_info_list))
  1550. return;
  1551. head->r.step++;
  1552. if (!tomoyo_set_lf(head))
  1553. return;
  1554. /* fall through */
  1555. case 2:
  1556. head->r.step = 0;
  1557. if (head->r.print_this_domain_only)
  1558. goto done;
  1559. }
  1560. }
  1561. done:
  1562. head->r.eof = true;
  1563. }
  1564. /**
  1565. * tomoyo_write_pid: Specify PID to obtain domainname.
  1566. *
  1567. * @head: Pointer to "struct tomoyo_io_buffer".
  1568. *
  1569. * Returns 0.
  1570. */
  1571. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1572. {
  1573. head->r.eof = false;
  1574. return 0;
  1575. }
  1576. /**
  1577. * tomoyo_read_pid - Get domainname of the specified PID.
  1578. *
  1579. * @head: Pointer to "struct tomoyo_io_buffer".
  1580. *
  1581. * Returns the domainname which the specified PID is in on success,
  1582. * empty string otherwise.
  1583. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1584. * using read()/write() interface rather than sysctl() interface.
  1585. */
  1586. static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1587. {
  1588. char *buf = head->write_buf;
  1589. bool global_pid = false;
  1590. unsigned int pid;
  1591. struct task_struct *p;
  1592. struct tomoyo_domain_info *domain = NULL;
  1593. /* Accessing write_buf is safe because head->io_sem is held. */
  1594. if (!buf) {
  1595. head->r.eof = true;
  1596. return; /* Do nothing if open(O_RDONLY). */
  1597. }
  1598. if (head->r.w_pos || head->r.eof)
  1599. return;
  1600. head->r.eof = true;
  1601. if (tomoyo_str_starts(&buf, "global-pid "))
  1602. global_pid = true;
  1603. pid = (unsigned int) simple_strtoul(buf, NULL, 10);
  1604. rcu_read_lock();
  1605. if (global_pid)
  1606. p = find_task_by_pid_ns(pid, &init_pid_ns);
  1607. else
  1608. p = find_task_by_vpid(pid);
  1609. if (p)
  1610. domain = tomoyo_real_domain(p);
  1611. rcu_read_unlock();
  1612. if (!domain)
  1613. return;
  1614. tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
  1615. tomoyo_set_string(head, domain->domainname->name);
  1616. }
  1617. /* String table for domain transition control keywords. */
  1618. static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
  1619. [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
  1620. [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
  1621. [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
  1622. [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
  1623. [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
  1624. [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
  1625. };
  1626. /* String table for grouping keywords. */
  1627. static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
  1628. [TOMOYO_PATH_GROUP] = "path_group ",
  1629. [TOMOYO_NUMBER_GROUP] = "number_group ",
  1630. [TOMOYO_ADDRESS_GROUP] = "address_group ",
  1631. };
  1632. /**
  1633. * tomoyo_write_exception - Write exception policy.
  1634. *
  1635. * @head: Pointer to "struct tomoyo_io_buffer".
  1636. *
  1637. * Returns 0 on success, negative value otherwise.
  1638. *
  1639. * Caller holds tomoyo_read_lock().
  1640. */
  1641. static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
  1642. {
  1643. const bool is_delete = head->w.is_delete;
  1644. struct tomoyo_acl_param param = {
  1645. .ns = head->w.ns,
  1646. .is_delete = is_delete,
  1647. .data = head->write_buf,
  1648. };
  1649. u8 i;
  1650. if (tomoyo_str_starts(&param.data, "aggregator "))
  1651. return tomoyo_write_aggregator(&param);
  1652. for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
  1653. if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
  1654. return tomoyo_write_transition_control(&param, i);
  1655. for (i = 0; i < TOMOYO_MAX_GROUP; i++)
  1656. if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
  1657. return tomoyo_write_group(&param, i);
  1658. if (tomoyo_str_starts(&param.data, "acl_group ")) {
  1659. unsigned int group;
  1660. char *data;
  1661. group = simple_strtoul(param.data, &data, 10);
  1662. if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
  1663. return tomoyo_write_domain2
  1664. (head->w.ns, &head->w.ns->acl_group[group],
  1665. data, is_delete);
  1666. }
  1667. return -EINVAL;
  1668. }
  1669. /**
  1670. * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
  1671. *
  1672. * @head: Pointer to "struct tomoyo_io_buffer".
  1673. * @idx: Index number.
  1674. *
  1675. * Returns true on success, false otherwise.
  1676. *
  1677. * Caller holds tomoyo_read_lock().
  1678. */
  1679. static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
  1680. {
  1681. struct tomoyo_policy_namespace *ns =
  1682. container_of(head->r.ns, typeof(*ns), namespace_list);
  1683. struct list_head *list = &ns->group_list[idx];
  1684. list_for_each_cookie(head->r.group, list) {
  1685. struct tomoyo_group *group =
  1686. list_entry(head->r.group, typeof(*group), head.list);
  1687. list_for_each_cookie(head->r.acl, &group->member_list) {
  1688. struct tomoyo_acl_head *ptr =
  1689. list_entry(head->r.acl, typeof(*ptr), list);
  1690. if (ptr->is_deleted)
  1691. continue;
  1692. if (!tomoyo_flush(head))
  1693. return false;
  1694. tomoyo_print_namespace(head);
  1695. tomoyo_set_string(head, tomoyo_group_name[idx]);
  1696. tomoyo_set_string(head, group->group_name->name);
  1697. if (idx == TOMOYO_PATH_GROUP) {
  1698. tomoyo_set_space(head);
  1699. tomoyo_set_string(head, container_of
  1700. (ptr, struct tomoyo_path_group,
  1701. head)->member_name->name);
  1702. } else if (idx == TOMOYO_NUMBER_GROUP) {
  1703. tomoyo_print_number_union(head, &container_of
  1704. (ptr,
  1705. struct tomoyo_number_group,
  1706. head)->number);
  1707. } else if (idx == TOMOYO_ADDRESS_GROUP) {
  1708. char buffer[128];
  1709. struct tomoyo_address_group *member =
  1710. container_of(ptr, typeof(*member),
  1711. head);
  1712. tomoyo_print_ip(buffer, sizeof(buffer),
  1713. &member->address);
  1714. tomoyo_io_printf(head, " %s", buffer);
  1715. }
  1716. tomoyo_set_lf(head);
  1717. }
  1718. head->r.acl = NULL;
  1719. }
  1720. head->r.group = NULL;
  1721. return true;
  1722. }
  1723. /**
  1724. * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
  1725. *
  1726. * @head: Pointer to "struct tomoyo_io_buffer".
  1727. * @idx: Index number.
  1728. *
  1729. * Returns true on success, false otherwise.
  1730. *
  1731. * Caller holds tomoyo_read_lock().
  1732. */
  1733. static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
  1734. {
  1735. struct tomoyo_policy_namespace *ns =
  1736. container_of(head->r.ns, typeof(*ns), namespace_list);
  1737. struct list_head *list = &ns->policy_list[idx];
  1738. list_for_each_cookie(head->r.acl, list) {
  1739. struct tomoyo_acl_head *acl =
  1740. container_of(head->r.acl, typeof(*acl), list);
  1741. if (acl->is_deleted)
  1742. continue;
  1743. if (!tomoyo_flush(head))
  1744. return false;
  1745. switch (idx) {
  1746. case TOMOYO_ID_TRANSITION_CONTROL:
  1747. {
  1748. struct tomoyo_transition_control *ptr =
  1749. container_of(acl, typeof(*ptr), head);
  1750. tomoyo_print_namespace(head);
  1751. tomoyo_set_string(head, tomoyo_transition_type
  1752. [ptr->type]);
  1753. tomoyo_set_string(head, ptr->program ?
  1754. ptr->program->name : "any");
  1755. tomoyo_set_string(head, " from ");
  1756. tomoyo_set_string(head, ptr->domainname ?
  1757. ptr->domainname->name :
  1758. "any");
  1759. }
  1760. break;
  1761. case TOMOYO_ID_AGGREGATOR:
  1762. {
  1763. struct tomoyo_aggregator *ptr =
  1764. container_of(acl, typeof(*ptr), head);
  1765. tomoyo_print_namespace(head);
  1766. tomoyo_set_string(head, "aggregator ");
  1767. tomoyo_set_string(head,
  1768. ptr->original_name->name);
  1769. tomoyo_set_space(head);
  1770. tomoyo_set_string(head,
  1771. ptr->aggregated_name->name);
  1772. }
  1773. break;
  1774. default:
  1775. continue;
  1776. }
  1777. tomoyo_set_lf(head);
  1778. }
  1779. head->r.acl = NULL;
  1780. return true;
  1781. }
  1782. /**
  1783. * tomoyo_read_exception - Read exception policy.
  1784. *
  1785. * @head: Pointer to "struct tomoyo_io_buffer".
  1786. *
  1787. * Caller holds tomoyo_read_lock().
  1788. */
  1789. static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
  1790. {
  1791. struct tomoyo_policy_namespace *ns =
  1792. container_of(head->r.ns, typeof(*ns), namespace_list);
  1793. if (head->r.eof)
  1794. return;
  1795. while (head->r.step < TOMOYO_MAX_POLICY &&
  1796. tomoyo_read_policy(head, head->r.step))
  1797. head->r.step++;
  1798. if (head->r.step < TOMOYO_MAX_POLICY)
  1799. return;
  1800. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
  1801. tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
  1802. head->r.step++;
  1803. if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
  1804. return;
  1805. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
  1806. + TOMOYO_MAX_ACL_GROUPS) {
  1807. head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
  1808. - TOMOYO_MAX_GROUP;
  1809. if (!tomoyo_read_domain2(head, &ns->acl_group
  1810. [head->r.acl_group_index]))
  1811. return;
  1812. head->r.step++;
  1813. }
  1814. head->r.eof = true;
  1815. }
  1816. /* Wait queue for kernel -> userspace notification. */
  1817. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1818. /* Wait queue for userspace -> kernel notification. */
  1819. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
  1820. /* Structure for query. */
  1821. struct tomoyo_query {
  1822. struct list_head list;
  1823. char *query;
  1824. size_t query_len;
  1825. unsigned int serial;
  1826. u8 timer;
  1827. u8 answer;
  1828. u8 retry;
  1829. };
  1830. /* The list for "struct tomoyo_query". */
  1831. static LIST_HEAD(tomoyo_query_list);
  1832. /* Lock for manipulating tomoyo_query_list. */
  1833. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1834. /*
  1835. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1836. * interface.
  1837. */
  1838. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1839. /**
  1840. * tomoyo_truncate - Truncate a line.
  1841. *
  1842. * @str: String to truncate.
  1843. *
  1844. * Returns length of truncated @str.
  1845. */
  1846. static int tomoyo_truncate(char *str)
  1847. {
  1848. char *start = str;
  1849. while (*(unsigned char *) str > (unsigned char) ' ')
  1850. str++;
  1851. *str = '\0';
  1852. return strlen(start) + 1;
  1853. }
  1854. /**
  1855. * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
  1856. *
  1857. * @domain: Pointer to "struct tomoyo_domain_info".
  1858. * @header: Lines containing ACL.
  1859. *
  1860. * Returns nothing.
  1861. */
  1862. static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
  1863. {
  1864. char *buffer;
  1865. char *realpath = NULL;
  1866. char *argv0 = NULL;
  1867. char *symlink = NULL;
  1868. char *cp = strchr(header, '\n');
  1869. int len;
  1870. if (!cp)
  1871. return;
  1872. cp = strchr(cp + 1, '\n');
  1873. if (!cp)
  1874. return;
  1875. *cp++ = '\0';
  1876. len = strlen(cp) + 1;
  1877. /* strstr() will return NULL if ordering is wrong. */
  1878. if (*cp == 'f') {
  1879. argv0 = strstr(header, " argv[]={ \"");
  1880. if (argv0) {
  1881. argv0 += 10;
  1882. len += tomoyo_truncate(argv0) + 14;
  1883. }
  1884. realpath = strstr(header, " exec={ realpath=\"");
  1885. if (realpath) {
  1886. realpath += 8;
  1887. len += tomoyo_truncate(realpath) + 6;
  1888. }
  1889. symlink = strstr(header, " symlink.target=\"");
  1890. if (symlink)
  1891. len += tomoyo_truncate(symlink + 1) + 1;
  1892. }
  1893. buffer = kmalloc(len, GFP_NOFS);
  1894. if (!buffer)
  1895. return;
  1896. snprintf(buffer, len - 1, "%s", cp);
  1897. if (realpath)
  1898. tomoyo_addprintf(buffer, len, " exec.%s", realpath);
  1899. if (argv0)
  1900. tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
  1901. if (symlink)
  1902. tomoyo_addprintf(buffer, len, "%s", symlink);
  1903. tomoyo_normalize_line(buffer);
  1904. if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
  1905. false))
  1906. tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
  1907. kfree(buffer);
  1908. }
  1909. /**
  1910. * tomoyo_supervisor - Ask for the supervisor's decision.
  1911. *
  1912. * @r: Pointer to "struct tomoyo_request_info".
  1913. * @fmt: The printf()'s format string, followed by parameters.
  1914. *
  1915. * Returns 0 if the supervisor decided to permit the access request which
  1916. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1917. * supervisor decided to retry the access request which violated the policy in
  1918. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1919. */
  1920. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1921. {
  1922. va_list args;
  1923. int error;
  1924. int len;
  1925. static unsigned int tomoyo_serial;
  1926. struct tomoyo_query entry = { };
  1927. bool quota_exceeded = false;
  1928. va_start(args, fmt);
  1929. len = vsnprintf((char *) &len, 1, fmt, args) + 1;
  1930. va_end(args);
  1931. /* Write /sys/kernel/security/tomoyo/audit. */
  1932. va_start(args, fmt);
  1933. tomoyo_write_log2(r, len, fmt, args);
  1934. va_end(args);
  1935. /* Nothing more to do if granted. */
  1936. if (r->granted)
  1937. return 0;
  1938. if (r->mode)
  1939. tomoyo_update_stat(r->mode);
  1940. switch (r->mode) {
  1941. case TOMOYO_CONFIG_ENFORCING:
  1942. error = -EPERM;
  1943. if (atomic_read(&tomoyo_query_observers))
  1944. break;
  1945. goto out;
  1946. case TOMOYO_CONFIG_LEARNING:
  1947. error = 0;
  1948. /* Check max_learning_entry parameter. */
  1949. if (tomoyo_domain_quota_is_ok(r))
  1950. break;
  1951. /* fall through */
  1952. default:
  1953. return 0;
  1954. }
  1955. /* Get message. */
  1956. va_start(args, fmt);
  1957. entry.query = tomoyo_init_log(r, len, fmt, args);
  1958. va_end(args);
  1959. if (!entry.query)
  1960. goto out;
  1961. entry.query_len = strlen(entry.query) + 1;
  1962. if (!error) {
  1963. tomoyo_add_entry(r->domain, entry.query);
  1964. goto out;
  1965. }
  1966. len = tomoyo_round2(entry.query_len);
  1967. spin_lock(&tomoyo_query_list_lock);
  1968. if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
  1969. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
  1970. >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
  1971. quota_exceeded = true;
  1972. } else {
  1973. entry.serial = tomoyo_serial++;
  1974. entry.retry = r->retry;
  1975. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
  1976. list_add_tail(&entry.list, &tomoyo_query_list);
  1977. }
  1978. spin_unlock(&tomoyo_query_list_lock);
  1979. if (quota_exceeded)
  1980. goto out;
  1981. /* Give 10 seconds for supervisor's opinion. */
  1982. while (entry.timer < 10) {
  1983. wake_up_all(&tomoyo_query_wait);
  1984. if (wait_event_interruptible_timeout
  1985. (tomoyo_answer_wait, entry.answer ||
  1986. !atomic_read(&tomoyo_query_observers), HZ))
  1987. break;
  1988. else
  1989. entry.timer++;
  1990. }
  1991. spin_lock(&tomoyo_query_list_lock);
  1992. list_del(&entry.list);
  1993. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
  1994. spin_unlock(&tomoyo_query_list_lock);
  1995. switch (entry.answer) {
  1996. case 3: /* Asked to retry by administrator. */
  1997. error = TOMOYO_RETRY_REQUEST;
  1998. r->retry++;
  1999. break;
  2000. case 1:
  2001. /* Granted by administrator. */
  2002. error = 0;
  2003. break;
  2004. default:
  2005. /* Timed out or rejected by administrator. */
  2006. break;
  2007. }
  2008. out:
  2009. kfree(entry.query);
  2010. return error;
  2011. }
  2012. /**
  2013. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  2014. *
  2015. * @file: Pointer to "struct file".
  2016. * @wait: Pointer to "poll_table".
  2017. *
  2018. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  2019. *
  2020. * Waits for access requests which violated policy in enforcing mode.
  2021. */
  2022. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  2023. {
  2024. struct list_head *tmp;
  2025. bool found = false;
  2026. u8 i;
  2027. for (i = 0; i < 2; i++) {
  2028. spin_lock(&tomoyo_query_list_lock);
  2029. list_for_each(tmp, &tomoyo_query_list) {
  2030. struct tomoyo_query *ptr =
  2031. list_entry(tmp, typeof(*ptr), list);
  2032. if (ptr->answer)
  2033. continue;
  2034. found = true;
  2035. break;
  2036. }
  2037. spin_unlock(&tomoyo_query_list_lock);
  2038. if (found)
  2039. return POLLIN | POLLRDNORM;
  2040. if (i)
  2041. break;
  2042. poll_wait(file, &tomoyo_query_wait, wait);
  2043. }
  2044. return 0;
  2045. }
  2046. /**
  2047. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  2048. *
  2049. * @head: Pointer to "struct tomoyo_io_buffer".
  2050. */
  2051. static void tomoyo_read_query(struct tomoyo_io_buffer *head)
  2052. {
  2053. struct list_head *tmp;
  2054. unsigned int pos = 0;
  2055. size_t len = 0;
  2056. char *buf;
  2057. if (head->r.w_pos)
  2058. return;
  2059. if (head->read_buf) {
  2060. kfree(head->read_buf);
  2061. head->read_buf = NULL;
  2062. }
  2063. spin_lock(&tomoyo_query_list_lock);
  2064. list_for_each(tmp, &tomoyo_query_list) {
  2065. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  2066. if (ptr->answer)
  2067. continue;
  2068. if (pos++ != head->r.query_index)
  2069. continue;
  2070. len = ptr->query_len;
  2071. break;
  2072. }
  2073. spin_unlock(&tomoyo_query_list_lock);
  2074. if (!len) {
  2075. head->r.query_index = 0;
  2076. return;
  2077. }
  2078. buf = kzalloc(len + 32, GFP_NOFS);
  2079. if (!buf)
  2080. return;
  2081. pos = 0;
  2082. spin_lock(&tomoyo_query_list_lock);
  2083. list_for_each(tmp, &tomoyo_query_list) {
  2084. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  2085. if (ptr->answer)
  2086. continue;
  2087. if (pos++ != head->r.query_index)
  2088. continue;
  2089. /*
  2090. * Some query can be skipped because tomoyo_query_list
  2091. * can change, but I don't care.
  2092. */
  2093. if (len == ptr->query_len)
  2094. snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
  2095. ptr->retry, ptr->query);
  2096. break;
  2097. }
  2098. spin_unlock(&tomoyo_query_list_lock);
  2099. if (buf[0]) {
  2100. head->read_buf = buf;
  2101. head->r.w[head->r.w_pos++] = buf;
  2102. head->r.query_index++;
  2103. } else {
  2104. kfree(buf);
  2105. }
  2106. }
  2107. /**
  2108. * tomoyo_write_answer - Write the supervisor's decision.
  2109. *
  2110. * @head: Pointer to "struct tomoyo_io_buffer".
  2111. *
  2112. * Returns 0 on success, -EINVAL otherwise.
  2113. */
  2114. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  2115. {
  2116. char *data = head->write_buf;
  2117. struct list_head *tmp;
  2118. unsigned int serial;
  2119. unsigned int answer;
  2120. spin_lock(&tomoyo_query_list_lock);
  2121. list_for_each(tmp, &tomoyo_query_list) {
  2122. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  2123. ptr->timer = 0;
  2124. }
  2125. spin_unlock(&tomoyo_query_list_lock);
  2126. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  2127. return -EINVAL;
  2128. spin_lock(&tomoyo_query_list_lock);
  2129. list_for_each(tmp, &tomoyo_query_list) {
  2130. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  2131. if (ptr->serial != serial)
  2132. continue;
  2133. if (!ptr->answer)
  2134. ptr->answer = answer;
  2135. break;
  2136. }
  2137. spin_unlock(&tomoyo_query_list_lock);
  2138. return 0;
  2139. }
  2140. /**
  2141. * tomoyo_read_version: Get version.
  2142. *
  2143. * @head: Pointer to "struct tomoyo_io_buffer".
  2144. *
  2145. * Returns version information.
  2146. */
  2147. static void tomoyo_read_version(struct tomoyo_io_buffer *head)
  2148. {
  2149. if (!head->r.eof) {
  2150. tomoyo_io_printf(head, "2.5.0");
  2151. head->r.eof = true;
  2152. }
  2153. }
  2154. /* String table for /sys/kernel/security/tomoyo/stat interface. */
  2155. static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
  2156. [TOMOYO_STAT_POLICY_UPDATES] = "update:",
  2157. [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:",
  2158. [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
  2159. [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
  2160. };
  2161. /* String table for /sys/kernel/security/tomoyo/stat interface. */
  2162. static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
  2163. [TOMOYO_MEMORY_POLICY] = "policy:",
  2164. [TOMOYO_MEMORY_AUDIT] = "audit log:",
  2165. [TOMOYO_MEMORY_QUERY] = "query message:",
  2166. };
  2167. /* Timestamp counter for last updated. */
  2168. static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
  2169. /* Counter for number of updates. */
  2170. static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
  2171. /**
  2172. * tomoyo_update_stat - Update statistic counters.
  2173. *
  2174. * @index: Index for policy type.
  2175. *
  2176. * Returns nothing.
  2177. */
  2178. void tomoyo_update_stat(const u8 index)
  2179. {
  2180. struct timeval tv;
  2181. do_gettimeofday(&tv);
  2182. /*
  2183. * I don't use atomic operations because race condition is not fatal.
  2184. */
  2185. tomoyo_stat_updated[index]++;
  2186. tomoyo_stat_modified[index] = tv.tv_sec;
  2187. }
  2188. /**
  2189. * tomoyo_read_stat - Read statistic data.
  2190. *
  2191. * @head: Pointer to "struct tomoyo_io_buffer".
  2192. *
  2193. * Returns nothing.
  2194. */
  2195. static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
  2196. {
  2197. u8 i;
  2198. unsigned int total = 0;
  2199. if (head->r.eof)
  2200. return;
  2201. for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
  2202. tomoyo_io_printf(head, "Policy %-30s %10u",
  2203. tomoyo_policy_headers[i],
  2204. tomoyo_stat_updated[i]);
  2205. if (tomoyo_stat_modified[i]) {
  2206. struct tomoyo_time stamp;
  2207. tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
  2208. tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
  2209. "%02u:%02u:%02u)",
  2210. stamp.year, stamp.month, stamp.day,
  2211. stamp.hour, stamp.min, stamp.sec);
  2212. }
  2213. tomoyo_set_lf(head);
  2214. }
  2215. for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
  2216. unsigned int used = tomoyo_memory_used[i];
  2217. total += used;
  2218. tomoyo_io_printf(head, "Memory used by %-22s %10u",
  2219. tomoyo_memory_headers[i], used);
  2220. used = tomoyo_memory_quota[i];
  2221. if (used)
  2222. tomoyo_io_printf(head, " (Quota: %10u)", used);
  2223. tomoyo_set_lf(head);
  2224. }
  2225. tomoyo_io_printf(head, "Total memory used: %10u\n",
  2226. total);
  2227. head->r.eof = true;
  2228. }
  2229. /**
  2230. * tomoyo_write_stat - Set memory quota.
  2231. *
  2232. * @head: Pointer to "struct tomoyo_io_buffer".
  2233. *
  2234. * Returns 0.
  2235. */
  2236. static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
  2237. {
  2238. char *data = head->write_buf;
  2239. u8 i;
  2240. if (tomoyo_str_starts(&data, "Memory used by "))
  2241. for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
  2242. if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
  2243. sscanf(data, "%u", &tomoyo_memory_quota[i]);
  2244. return 0;
  2245. }
  2246. /**
  2247. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  2248. *
  2249. * @type: Type of interface.
  2250. * @file: Pointer to "struct file".
  2251. *
  2252. * Returns 0 on success, negative value otherwise.
  2253. */
  2254. int tomoyo_open_control(const u8 type, struct file *file)
  2255. {
  2256. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  2257. if (!head)
  2258. return -ENOMEM;
  2259. mutex_init(&head->io_sem);
  2260. head->type = type;
  2261. switch (type) {
  2262. case TOMOYO_DOMAINPOLICY:
  2263. /* /sys/kernel/security/tomoyo/domain_policy */
  2264. head->write = tomoyo_write_domain;
  2265. head->read = tomoyo_read_domain;
  2266. break;
  2267. case TOMOYO_EXCEPTIONPOLICY:
  2268. /* /sys/kernel/security/tomoyo/exception_policy */
  2269. head->write = tomoyo_write_exception;
  2270. head->read = tomoyo_read_exception;
  2271. break;
  2272. case TOMOYO_AUDIT:
  2273. /* /sys/kernel/security/tomoyo/audit */
  2274. head->poll = tomoyo_poll_log;
  2275. head->read = tomoyo_read_log;
  2276. break;
  2277. case TOMOYO_PROCESS_STATUS:
  2278. /* /sys/kernel/security/tomoyo/.process_status */
  2279. head->write = tomoyo_write_pid;
  2280. head->read = tomoyo_read_pid;
  2281. break;
  2282. case TOMOYO_VERSION:
  2283. /* /sys/kernel/security/tomoyo/version */
  2284. head->read = tomoyo_read_version;
  2285. head->readbuf_size = 128;
  2286. break;
  2287. case TOMOYO_STAT:
  2288. /* /sys/kernel/security/tomoyo/stat */
  2289. head->write = tomoyo_write_stat;
  2290. head->read = tomoyo_read_stat;
  2291. head->readbuf_size = 1024;
  2292. break;
  2293. case TOMOYO_PROFILE:
  2294. /* /sys/kernel/security/tomoyo/profile */
  2295. head->write = tomoyo_write_profile;
  2296. head->read = tomoyo_read_profile;
  2297. break;
  2298. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  2299. head->poll = tomoyo_poll_query;
  2300. head->write = tomoyo_write_answer;
  2301. head->read = tomoyo_read_query;
  2302. break;
  2303. case TOMOYO_MANAGER:
  2304. /* /sys/kernel/security/tomoyo/manager */
  2305. head->write = tomoyo_write_manager;
  2306. head->read = tomoyo_read_manager;
  2307. break;
  2308. }
  2309. if (!(file->f_mode & FMODE_READ)) {
  2310. /*
  2311. * No need to allocate read_buf since it is not opened
  2312. * for reading.
  2313. */
  2314. head->read = NULL;
  2315. head->poll = NULL;
  2316. } else if (!head->poll) {
  2317. /* Don't allocate read_buf for poll() access. */
  2318. if (!head->readbuf_size)
  2319. head->readbuf_size = 4096 * 2;
  2320. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  2321. if (!head->read_buf) {
  2322. kfree(head);
  2323. return -ENOMEM;
  2324. }
  2325. }
  2326. if (!(file->f_mode & FMODE_WRITE)) {
  2327. /*
  2328. * No need to allocate write_buf since it is not opened
  2329. * for writing.
  2330. */
  2331. head->write = NULL;
  2332. } else if (head->write) {
  2333. head->writebuf_size = 4096 * 2;
  2334. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  2335. if (!head->write_buf) {
  2336. kfree(head->read_buf);
  2337. kfree(head);
  2338. return -ENOMEM;
  2339. }
  2340. }
  2341. /*
  2342. * If the file is /sys/kernel/security/tomoyo/query , increment the
  2343. * observer counter.
  2344. * The obserber counter is used by tomoyo_supervisor() to see if
  2345. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  2346. */
  2347. if (type == TOMOYO_QUERY)
  2348. atomic_inc(&tomoyo_query_observers);
  2349. file->private_data = head;
  2350. tomoyo_notify_gc(head, true);
  2351. return 0;
  2352. }
  2353. /**
  2354. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  2355. *
  2356. * @file: Pointer to "struct file".
  2357. * @wait: Pointer to "poll_table".
  2358. *
  2359. * Waits for read readiness.
  2360. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
  2361. * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
  2362. */
  2363. int tomoyo_poll_control(struct file *file, poll_table *wait)
  2364. {
  2365. struct tomoyo_io_buffer *head = file->private_data;
  2366. if (!head->poll)
  2367. return -ENOSYS;
  2368. return head->poll(file, wait);
  2369. }
  2370. /**
  2371. * tomoyo_set_namespace_cursor - Set namespace to read.
  2372. *
  2373. * @head: Pointer to "struct tomoyo_io_buffer".
  2374. *
  2375. * Returns nothing.
  2376. */
  2377. static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
  2378. {
  2379. struct list_head *ns;
  2380. if (head->type != TOMOYO_EXCEPTIONPOLICY &&
  2381. head->type != TOMOYO_PROFILE)
  2382. return;
  2383. /*
  2384. * If this is the first read, or reading previous namespace finished
  2385. * and has more namespaces to read, update the namespace cursor.
  2386. */
  2387. ns = head->r.ns;
  2388. if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
  2389. /* Clearing is OK because tomoyo_flush() returned true. */
  2390. memset(&head->r, 0, sizeof(head->r));
  2391. head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
  2392. }
  2393. }
  2394. /**
  2395. * tomoyo_has_more_namespace - Check for unread namespaces.
  2396. *
  2397. * @head: Pointer to "struct tomoyo_io_buffer".
  2398. *
  2399. * Returns true if we have more entries to print, false otherwise.
  2400. */
  2401. static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
  2402. {
  2403. return (head->type == TOMOYO_EXCEPTIONPOLICY ||
  2404. head->type == TOMOYO_PROFILE) && head->r.eof &&
  2405. head->r.ns->next != &tomoyo_namespace_list;
  2406. }
  2407. /**
  2408. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  2409. *
  2410. * @head: Pointer to "struct tomoyo_io_buffer".
  2411. * @buffer: Poiner to buffer to write to.
  2412. * @buffer_len: Size of @buffer.
  2413. *
  2414. * Returns bytes read on success, negative value otherwise.
  2415. */
  2416. ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  2417. const int buffer_len)
  2418. {
  2419. int len;
  2420. int idx;
  2421. if (!head->read)
  2422. return -ENOSYS;
  2423. if (mutex_lock_interruptible(&head->io_sem))
  2424. return -EINTR;
  2425. head->read_user_buf = buffer;
  2426. head->read_user_buf_avail = buffer_len;
  2427. idx = tomoyo_read_lock();
  2428. if (tomoyo_flush(head))
  2429. /* Call the policy handler. */
  2430. do {
  2431. tomoyo_set_namespace_cursor(head);
  2432. head->read(head);
  2433. } while (tomoyo_flush(head) &&
  2434. tomoyo_has_more_namespace(head));
  2435. tomoyo_read_unlock(idx);
  2436. len = head->read_user_buf - buffer;
  2437. mutex_unlock(&head->io_sem);
  2438. return len;
  2439. }
  2440. /**
  2441. * tomoyo_parse_policy - Parse a policy line.
  2442. *
  2443. * @head: Poiter to "struct tomoyo_io_buffer".
  2444. * @line: Line to parse.
  2445. *
  2446. * Returns 0 on success, negative value otherwise.
  2447. *
  2448. * Caller holds tomoyo_read_lock().
  2449. */
  2450. static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
  2451. {
  2452. /* Delete request? */
  2453. head->w.is_delete = !strncmp(line, "delete ", 7);
  2454. if (head->w.is_delete)
  2455. memmove(line, line + 7, strlen(line + 7) + 1);
  2456. /* Selecting namespace to update. */
  2457. if (head->type == TOMOYO_EXCEPTIONPOLICY ||
  2458. head->type == TOMOYO_PROFILE) {
  2459. if (*line == '<') {
  2460. char *cp = strchr(line, ' ');
  2461. if (cp) {
  2462. *cp++ = '\0';
  2463. head->w.ns = tomoyo_assign_namespace(line);
  2464. memmove(line, cp, strlen(cp) + 1);
  2465. } else
  2466. head->w.ns = NULL;
  2467. } else
  2468. head->w.ns = &tomoyo_kernel_namespace;
  2469. /* Don't allow updating if namespace is invalid. */
  2470. if (!head->w.ns)
  2471. return -ENOENT;
  2472. }
  2473. /* Do the update. */
  2474. return head->write(head);
  2475. }
  2476. /**
  2477. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  2478. *
  2479. * @head: Pointer to "struct tomoyo_io_buffer".
  2480. * @buffer: Pointer to buffer to read from.
  2481. * @buffer_len: Size of @buffer.
  2482. *
  2483. * Returns @buffer_len on success, negative value otherwise.
  2484. */
  2485. ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
  2486. const char __user *buffer, const int buffer_len)
  2487. {
  2488. int error = buffer_len;
  2489. size_t avail_len = buffer_len;
  2490. char *cp0 = head->write_buf;
  2491. int idx;
  2492. if (!head->write)
  2493. return -ENOSYS;
  2494. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  2495. return -EFAULT;
  2496. if (mutex_lock_interruptible(&head->io_sem))
  2497. return -EINTR;
  2498. head->read_user_buf_avail = 0;
  2499. idx = tomoyo_read_lock();
  2500. /* Read a line and dispatch it to the policy handler. */
  2501. while (avail_len > 0) {
  2502. char c;
  2503. if (head->w.avail >= head->writebuf_size - 1) {
  2504. const int len = head->writebuf_size * 2;
  2505. char *cp = kzalloc(len, GFP_NOFS);
  2506. if (!cp) {
  2507. error = -ENOMEM;
  2508. break;
  2509. }
  2510. memmove(cp, cp0, head->w.avail);
  2511. kfree(cp0);
  2512. head->write_buf = cp;
  2513. cp0 = cp;
  2514. head->writebuf_size = len;
  2515. }
  2516. if (get_user(c, buffer)) {
  2517. error = -EFAULT;
  2518. break;
  2519. }
  2520. buffer++;
  2521. avail_len--;
  2522. cp0[head->w.avail++] = c;
  2523. if (c != '\n')
  2524. continue;
  2525. cp0[head->w.avail - 1] = '\0';
  2526. head->w.avail = 0;
  2527. tomoyo_normalize_line(cp0);
  2528. if (!strcmp(cp0, "reset")) {
  2529. head->w.ns = &tomoyo_kernel_namespace;
  2530. head->w.domain = NULL;
  2531. memset(&head->r, 0, sizeof(head->r));
  2532. continue;
  2533. }
  2534. /* Don't allow updating policies by non manager programs. */
  2535. switch (head->type) {
  2536. case TOMOYO_PROCESS_STATUS:
  2537. /* This does not write anything. */
  2538. break;
  2539. case TOMOYO_DOMAINPOLICY:
  2540. if (tomoyo_select_domain(head, cp0))
  2541. continue;
  2542. /* fall through */
  2543. case TOMOYO_EXCEPTIONPOLICY:
  2544. if (!strcmp(cp0, "select transition_only")) {
  2545. head->r.print_transition_related_only = true;
  2546. continue;
  2547. }
  2548. /* fall through */
  2549. default:
  2550. if (!tomoyo_manager()) {
  2551. error = -EPERM;
  2552. goto out;
  2553. }
  2554. }
  2555. switch (tomoyo_parse_policy(head, cp0)) {
  2556. case -EPERM:
  2557. error = -EPERM;
  2558. goto out;
  2559. case 0:
  2560. switch (head->type) {
  2561. case TOMOYO_DOMAINPOLICY:
  2562. case TOMOYO_EXCEPTIONPOLICY:
  2563. case TOMOYO_STAT:
  2564. case TOMOYO_PROFILE:
  2565. case TOMOYO_MANAGER:
  2566. tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
  2567. break;
  2568. default:
  2569. break;
  2570. }
  2571. break;
  2572. }
  2573. }
  2574. out:
  2575. tomoyo_read_unlock(idx);
  2576. mutex_unlock(&head->io_sem);
  2577. return error;
  2578. }
  2579. /**
  2580. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  2581. *
  2582. * @head: Pointer to "struct tomoyo_io_buffer".
  2583. *
  2584. * Returns 0.
  2585. */
  2586. int tomoyo_close_control(struct tomoyo_io_buffer *head)
  2587. {
  2588. /*
  2589. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  2590. * observer counter.
  2591. */
  2592. if (head->type == TOMOYO_QUERY &&
  2593. atomic_dec_and_test(&tomoyo_query_observers))
  2594. wake_up_all(&tomoyo_answer_wait);
  2595. tomoyo_notify_gc(head, false);
  2596. return 0;
  2597. }
  2598. /**
  2599. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  2600. */
  2601. void tomoyo_check_profile(void)
  2602. {
  2603. struct tomoyo_domain_info *domain;
  2604. const int idx = tomoyo_read_lock();
  2605. tomoyo_policy_loaded = true;
  2606. printk(KERN_INFO "TOMOYO: 2.5.0\n");
  2607. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  2608. const u8 profile = domain->profile;
  2609. const struct tomoyo_policy_namespace *ns = domain->ns;
  2610. if (ns->profile_version != 20110903)
  2611. printk(KERN_ERR
  2612. "Profile version %u is not supported.\n",
  2613. ns->profile_version);
  2614. else if (!ns->profile_ptr[profile])
  2615. printk(KERN_ERR
  2616. "Profile %u (used by '%s') is not defined.\n",
  2617. profile, domain->domainname->name);
  2618. else
  2619. continue;
  2620. printk(KERN_ERR
  2621. "Userland tools for TOMOYO 2.5 must be installed and "
  2622. "policy must be initialized.\n");
  2623. printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ "
  2624. "for more information.\n");
  2625. panic("STOP!");
  2626. }
  2627. tomoyo_read_unlock(idx);
  2628. printk(KERN_INFO "Mandatory Access Control activated.\n");
  2629. }
  2630. /**
  2631. * tomoyo_load_builtin_policy - Load built-in policy.
  2632. *
  2633. * Returns nothing.
  2634. */
  2635. void __init tomoyo_load_builtin_policy(void)
  2636. {
  2637. /*
  2638. * This include file is manually created and contains built-in policy
  2639. * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
  2640. * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
  2641. * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
  2642. */
  2643. #include "builtin-policy.h"
  2644. u8 i;
  2645. const int idx = tomoyo_read_lock();
  2646. for (i = 0; i < 5; i++) {
  2647. struct tomoyo_io_buffer head = { };
  2648. char *start = "";
  2649. switch (i) {
  2650. case 0:
  2651. start = tomoyo_builtin_profile;
  2652. head.type = TOMOYO_PROFILE;
  2653. head.write = tomoyo_write_profile;
  2654. break;
  2655. case 1:
  2656. start = tomoyo_builtin_exception_policy;
  2657. head.type = TOMOYO_EXCEPTIONPOLICY;
  2658. head.write = tomoyo_write_exception;
  2659. break;
  2660. case 2:
  2661. start = tomoyo_builtin_domain_policy;
  2662. head.type = TOMOYO_DOMAINPOLICY;
  2663. head.write = tomoyo_write_domain;
  2664. break;
  2665. case 3:
  2666. start = tomoyo_builtin_manager;
  2667. head.type = TOMOYO_MANAGER;
  2668. head.write = tomoyo_write_manager;
  2669. break;
  2670. case 4:
  2671. start = tomoyo_builtin_stat;
  2672. head.type = TOMOYO_STAT;
  2673. head.write = tomoyo_write_stat;
  2674. break;
  2675. }
  2676. while (1) {
  2677. char *end = strchr(start, '\n');
  2678. if (!end)
  2679. break;
  2680. *end = '\0';
  2681. tomoyo_normalize_line(start);
  2682. head.write_buf = start;
  2683. tomoyo_parse_policy(&head, start);
  2684. start = end + 1;
  2685. }
  2686. }
  2687. tomoyo_read_unlock(idx);
  2688. #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
  2689. tomoyo_check_profile();
  2690. #endif
  2691. }