common.c 76 KB

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