common.c 70 KB

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