common.c 75 KB

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