common.c 74 KB

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