common.c 80 KB

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