common.c 75 KB

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