common.c 75 KB

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