common.c 75 KB

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