common.c 70 KB

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