common.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651
  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. head->r.eof = true;
  683. return;
  684. }
  685. head->r.step++;
  686. break;
  687. case 2:
  688. {
  689. u8 i;
  690. const struct tomoyo_path_info *comment =
  691. profile->comment;
  692. tomoyo_print_namespace(head);
  693. tomoyo_io_printf(head, "%u-COMMENT=", index);
  694. tomoyo_set_string(head, comment ? comment->name : "");
  695. tomoyo_set_lf(head);
  696. tomoyo_print_namespace(head);
  697. tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
  698. for (i = 0; i < TOMOYO_MAX_PREF; i++)
  699. tomoyo_io_printf(head, "%s=%u ",
  700. tomoyo_pref_keywords[i],
  701. profile->pref[i]);
  702. tomoyo_set_string(head, "}\n");
  703. head->r.step++;
  704. }
  705. break;
  706. case 3:
  707. {
  708. tomoyo_print_namespace(head);
  709. tomoyo_io_printf(head, "%u-%s", index, "CONFIG");
  710. tomoyo_print_config(head, profile->default_config);
  711. head->r.bit = 0;
  712. head->r.step++;
  713. }
  714. break;
  715. case 4:
  716. for ( ; head->r.bit < TOMOYO_MAX_MAC_INDEX
  717. + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
  718. const u8 i = head->r.bit;
  719. const u8 config = profile->config[i];
  720. if (config == TOMOYO_CONFIG_USE_DEFAULT)
  721. continue;
  722. tomoyo_print_namespace(head);
  723. if (i < TOMOYO_MAX_MAC_INDEX)
  724. tomoyo_io_printf(head, "%u-CONFIG::%s::%s",
  725. index,
  726. tomoyo_category_keywords
  727. [tomoyo_index2category[i]],
  728. tomoyo_mac_keywords[i]);
  729. else
  730. tomoyo_io_printf(head, "%u-CONFIG::%s", index,
  731. tomoyo_mac_keywords[i]);
  732. tomoyo_print_config(head, config);
  733. head->r.bit++;
  734. break;
  735. }
  736. if (head->r.bit == TOMOYO_MAX_MAC_INDEX
  737. + TOMOYO_MAX_MAC_CATEGORY_INDEX) {
  738. head->r.index++;
  739. head->r.step = 1;
  740. }
  741. break;
  742. }
  743. if (tomoyo_flush(head))
  744. goto next;
  745. }
  746. /**
  747. * tomoyo_same_manager - Check for duplicated "struct tomoyo_manager" entry.
  748. *
  749. * @a: Pointer to "struct tomoyo_acl_head".
  750. * @b: Pointer to "struct tomoyo_acl_head".
  751. *
  752. * Returns true if @a == @b, false otherwise.
  753. */
  754. static bool tomoyo_same_manager(const struct tomoyo_acl_head *a,
  755. const struct tomoyo_acl_head *b)
  756. {
  757. return container_of(a, struct tomoyo_manager, head)->manager ==
  758. container_of(b, struct tomoyo_manager, head)->manager;
  759. }
  760. /**
  761. * tomoyo_update_manager_entry - Add a manager entry.
  762. *
  763. * @manager: The path to manager or the domainnamme.
  764. * @is_delete: True if it is a delete request.
  765. *
  766. * Returns 0 on success, negative value otherwise.
  767. *
  768. * Caller holds tomoyo_read_lock().
  769. */
  770. static int tomoyo_update_manager_entry(const char *manager,
  771. const bool is_delete)
  772. {
  773. struct tomoyo_manager e = { };
  774. struct tomoyo_acl_param param = {
  775. /* .ns = &tomoyo_kernel_namespace, */
  776. .is_delete = is_delete,
  777. .list = &tomoyo_kernel_namespace.
  778. policy_list[TOMOYO_ID_MANAGER],
  779. };
  780. int error = is_delete ? -ENOENT : -ENOMEM;
  781. if (tomoyo_domain_def(manager)) {
  782. if (!tomoyo_correct_domain(manager))
  783. return -EINVAL;
  784. e.is_domain = true;
  785. } else {
  786. if (!tomoyo_correct_path(manager))
  787. return -EINVAL;
  788. }
  789. e.manager = tomoyo_get_name(manager);
  790. if (e.manager) {
  791. error = tomoyo_update_policy(&e.head, sizeof(e), &param,
  792. tomoyo_same_manager);
  793. tomoyo_put_name(e.manager);
  794. }
  795. return error;
  796. }
  797. /**
  798. * tomoyo_write_manager - Write manager policy.
  799. *
  800. * @head: Pointer to "struct tomoyo_io_buffer".
  801. *
  802. * Returns 0 on success, negative value otherwise.
  803. *
  804. * Caller holds tomoyo_read_lock().
  805. */
  806. static int tomoyo_write_manager(struct tomoyo_io_buffer *head)
  807. {
  808. char *data = head->write_buf;
  809. if (!strcmp(data, "manage_by_non_root")) {
  810. tomoyo_manage_by_non_root = !head->w.is_delete;
  811. return 0;
  812. }
  813. return tomoyo_update_manager_entry(data, head->w.is_delete);
  814. }
  815. /**
  816. * tomoyo_read_manager - Read manager policy.
  817. *
  818. * @head: Pointer to "struct tomoyo_io_buffer".
  819. *
  820. * Caller holds tomoyo_read_lock().
  821. */
  822. static void tomoyo_read_manager(struct tomoyo_io_buffer *head)
  823. {
  824. if (head->r.eof)
  825. return;
  826. list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.
  827. policy_list[TOMOYO_ID_MANAGER]) {
  828. struct tomoyo_manager *ptr =
  829. list_entry(head->r.acl, typeof(*ptr), head.list);
  830. if (ptr->head.is_deleted)
  831. continue;
  832. if (!tomoyo_flush(head))
  833. return;
  834. tomoyo_set_string(head, ptr->manager->name);
  835. tomoyo_set_lf(head);
  836. }
  837. head->r.eof = true;
  838. }
  839. /**
  840. * tomoyo_manager - Check whether the current process is a policy manager.
  841. *
  842. * Returns true if the current process is permitted to modify policy
  843. * via /sys/kernel/security/tomoyo/ interface.
  844. *
  845. * Caller holds tomoyo_read_lock().
  846. */
  847. static bool tomoyo_manager(void)
  848. {
  849. struct tomoyo_manager *ptr;
  850. const char *exe;
  851. const struct task_struct *task = current;
  852. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  853. bool found = false;
  854. if (!tomoyo_policy_loaded)
  855. return true;
  856. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  857. return false;
  858. list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
  859. policy_list[TOMOYO_ID_MANAGER], head.list) {
  860. if (!ptr->head.is_deleted && ptr->is_domain
  861. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  862. found = true;
  863. break;
  864. }
  865. }
  866. if (found)
  867. return true;
  868. exe = tomoyo_get_exe();
  869. if (!exe)
  870. return false;
  871. list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.
  872. policy_list[TOMOYO_ID_MANAGER], head.list) {
  873. if (!ptr->head.is_deleted && !ptr->is_domain
  874. && !strcmp(exe, ptr->manager->name)) {
  875. found = true;
  876. break;
  877. }
  878. }
  879. if (!found) { /* Reduce error messages. */
  880. static pid_t last_pid;
  881. const pid_t pid = current->pid;
  882. if (last_pid != pid) {
  883. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  884. "update policies.\n", domainname->name, exe);
  885. last_pid = pid;
  886. }
  887. }
  888. kfree(exe);
  889. return found;
  890. }
  891. /**
  892. * tomoyo_select_domain - Parse select command.
  893. *
  894. * @head: Pointer to "struct tomoyo_io_buffer".
  895. * @data: String to parse.
  896. *
  897. * Returns true on success, false otherwise.
  898. *
  899. * Caller holds tomoyo_read_lock().
  900. */
  901. static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
  902. const char *data)
  903. {
  904. unsigned int pid;
  905. struct tomoyo_domain_info *domain = NULL;
  906. bool global_pid = false;
  907. if (strncmp(data, "select ", 7))
  908. return false;
  909. data += 7;
  910. if (sscanf(data, "pid=%u", &pid) == 1 ||
  911. (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
  912. struct task_struct *p;
  913. rcu_read_lock();
  914. read_lock(&tasklist_lock);
  915. if (global_pid)
  916. p = find_task_by_pid_ns(pid, &init_pid_ns);
  917. else
  918. p = find_task_by_vpid(pid);
  919. if (p)
  920. domain = tomoyo_real_domain(p);
  921. read_unlock(&tasklist_lock);
  922. rcu_read_unlock();
  923. } else if (!strncmp(data, "domain=", 7)) {
  924. if (tomoyo_domain_def(data + 7))
  925. domain = tomoyo_find_domain(data + 7);
  926. } else
  927. return false;
  928. head->w.domain = domain;
  929. /* Accessing read_buf is safe because head->io_sem is held. */
  930. if (!head->read_buf)
  931. return true; /* Do nothing if open(O_WRONLY). */
  932. memset(&head->r, 0, sizeof(head->r));
  933. head->r.print_this_domain_only = true;
  934. if (domain)
  935. head->r.domain = &domain->list;
  936. else
  937. head->r.eof = 1;
  938. tomoyo_io_printf(head, "# select %s\n", data);
  939. if (domain && domain->is_deleted)
  940. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  941. return true;
  942. }
  943. /**
  944. * tomoyo_delete_domain - Delete a domain.
  945. *
  946. * @domainname: The name of domain.
  947. *
  948. * Returns 0.
  949. *
  950. * Caller holds tomoyo_read_lock().
  951. */
  952. static int tomoyo_delete_domain(char *domainname)
  953. {
  954. struct tomoyo_domain_info *domain;
  955. struct tomoyo_path_info name;
  956. name.name = domainname;
  957. tomoyo_fill_path_info(&name);
  958. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  959. return 0;
  960. /* Is there an active domain? */
  961. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  962. /* Never delete tomoyo_kernel_domain */
  963. if (domain == &tomoyo_kernel_domain)
  964. continue;
  965. if (domain->is_deleted ||
  966. tomoyo_pathcmp(domain->domainname, &name))
  967. continue;
  968. domain->is_deleted = true;
  969. break;
  970. }
  971. mutex_unlock(&tomoyo_policy_lock);
  972. return 0;
  973. }
  974. /**
  975. * tomoyo_write_domain2 - Write domain policy.
  976. *
  977. * @ns: Pointer to "struct tomoyo_policy_namespace".
  978. * @list: Pointer to "struct list_head".
  979. * @data: Policy to be interpreted.
  980. * @is_delete: True if it is a delete request.
  981. *
  982. * Returns 0 on success, negative value otherwise.
  983. *
  984. * Caller holds tomoyo_read_lock().
  985. */
  986. static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
  987. struct list_head *list, char *data,
  988. const bool is_delete)
  989. {
  990. struct tomoyo_acl_param param = {
  991. .ns = ns,
  992. .list = list,
  993. .data = data,
  994. .is_delete = is_delete,
  995. };
  996. static const struct {
  997. const char *keyword;
  998. int (*write) (struct tomoyo_acl_param *);
  999. } tomoyo_callback[1] = {
  1000. { "file ", tomoyo_write_file },
  1001. };
  1002. u8 i;
  1003. for (i = 0; i < 1; i++) {
  1004. if (!tomoyo_str_starts(&param.data,
  1005. tomoyo_callback[i].keyword))
  1006. continue;
  1007. return tomoyo_callback[i].write(&param);
  1008. }
  1009. return -EINVAL;
  1010. }
  1011. /* String table for domain flags. */
  1012. const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS] = {
  1013. [TOMOYO_DIF_QUOTA_WARNED] = "quota_exceeded\n",
  1014. [TOMOYO_DIF_TRANSITION_FAILED] = "transition_failed\n",
  1015. };
  1016. /**
  1017. * tomoyo_write_domain - Write domain policy.
  1018. *
  1019. * @head: Pointer to "struct tomoyo_io_buffer".
  1020. *
  1021. * Returns 0 on success, negative value otherwise.
  1022. *
  1023. * Caller holds tomoyo_read_lock().
  1024. */
  1025. static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
  1026. {
  1027. char *data = head->write_buf;
  1028. struct tomoyo_policy_namespace *ns;
  1029. struct tomoyo_domain_info *domain = head->w.domain;
  1030. const bool is_delete = head->w.is_delete;
  1031. bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
  1032. unsigned int profile;
  1033. if (*data == '<') {
  1034. domain = NULL;
  1035. if (is_delete)
  1036. tomoyo_delete_domain(data);
  1037. else if (is_select)
  1038. domain = tomoyo_find_domain(data);
  1039. else
  1040. domain = tomoyo_assign_domain(data, false);
  1041. head->w.domain = domain;
  1042. return 0;
  1043. }
  1044. if (!domain)
  1045. return -EINVAL;
  1046. ns = domain->ns;
  1047. if (sscanf(data, "use_profile %u", &profile) == 1
  1048. && profile < TOMOYO_MAX_PROFILES) {
  1049. if (!tomoyo_policy_loaded || ns->profile_ptr[profile])
  1050. domain->profile = (u8) profile;
  1051. return 0;
  1052. }
  1053. if (sscanf(data, "use_group %u\n", &profile) == 1
  1054. && profile < TOMOYO_MAX_ACL_GROUPS) {
  1055. if (!is_delete)
  1056. domain->group = (u8) profile;
  1057. return 0;
  1058. }
  1059. for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) {
  1060. const char *cp = tomoyo_dif[profile];
  1061. if (strncmp(data, cp, strlen(cp) - 1))
  1062. continue;
  1063. domain->flags[profile] = !is_delete;
  1064. return 0;
  1065. }
  1066. return tomoyo_write_domain2(ns, &domain->acl_info_list, data,
  1067. is_delete);
  1068. }
  1069. /**
  1070. * tomoyo_print_condition - Print condition part.
  1071. *
  1072. * @head: Pointer to "struct tomoyo_io_buffer".
  1073. * @cond: Pointer to "struct tomoyo_condition".
  1074. *
  1075. * Returns true on success, false otherwise.
  1076. */
  1077. static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
  1078. const struct tomoyo_condition *cond)
  1079. {
  1080. switch (head->r.cond_step) {
  1081. case 0:
  1082. head->r.cond_index = 0;
  1083. head->r.cond_step++;
  1084. /* fall through */
  1085. case 1:
  1086. {
  1087. const u16 condc = cond->condc;
  1088. const struct tomoyo_condition_element *condp =
  1089. (typeof(condp)) (cond + 1);
  1090. const struct tomoyo_number_union *numbers_p =
  1091. (typeof(numbers_p)) (condp + condc);
  1092. const struct tomoyo_name_union *names_p =
  1093. (typeof(names_p))
  1094. (numbers_p + cond->numbers_count);
  1095. const struct tomoyo_argv *argv =
  1096. (typeof(argv)) (names_p + cond->names_count);
  1097. const struct tomoyo_envp *envp =
  1098. (typeof(envp)) (argv + cond->argc);
  1099. u16 skip;
  1100. for (skip = 0; skip < head->r.cond_index; skip++) {
  1101. const u8 left = condp->left;
  1102. const u8 right = condp->right;
  1103. condp++;
  1104. switch (left) {
  1105. case TOMOYO_ARGV_ENTRY:
  1106. argv++;
  1107. continue;
  1108. case TOMOYO_ENVP_ENTRY:
  1109. envp++;
  1110. continue;
  1111. case TOMOYO_NUMBER_UNION:
  1112. numbers_p++;
  1113. break;
  1114. }
  1115. switch (right) {
  1116. case TOMOYO_NAME_UNION:
  1117. names_p++;
  1118. break;
  1119. case TOMOYO_NUMBER_UNION:
  1120. numbers_p++;
  1121. break;
  1122. }
  1123. }
  1124. while (head->r.cond_index < condc) {
  1125. const u8 match = condp->equals;
  1126. const u8 left = condp->left;
  1127. const u8 right = condp->right;
  1128. if (!tomoyo_flush(head))
  1129. return false;
  1130. condp++;
  1131. head->r.cond_index++;
  1132. tomoyo_set_space(head);
  1133. switch (left) {
  1134. case TOMOYO_ARGV_ENTRY:
  1135. tomoyo_io_printf(head,
  1136. "exec.argv[%lu]%s=\"",
  1137. argv->index, argv->
  1138. is_not ? "!" : "");
  1139. tomoyo_set_string(head,
  1140. argv->value->name);
  1141. tomoyo_set_string(head, "\"");
  1142. argv++;
  1143. continue;
  1144. case TOMOYO_ENVP_ENTRY:
  1145. tomoyo_set_string(head,
  1146. "exec.envp[\"");
  1147. tomoyo_set_string(head,
  1148. envp->name->name);
  1149. tomoyo_io_printf(head, "\"]%s=", envp->
  1150. is_not ? "!" : "");
  1151. if (envp->value) {
  1152. tomoyo_set_string(head, "\"");
  1153. tomoyo_set_string(head, envp->
  1154. value->name);
  1155. tomoyo_set_string(head, "\"");
  1156. } else {
  1157. tomoyo_set_string(head,
  1158. "NULL");
  1159. }
  1160. envp++;
  1161. continue;
  1162. case TOMOYO_NUMBER_UNION:
  1163. tomoyo_print_number_union_nospace
  1164. (head, numbers_p++);
  1165. break;
  1166. default:
  1167. tomoyo_set_string(head,
  1168. tomoyo_condition_keyword[left]);
  1169. break;
  1170. }
  1171. tomoyo_set_string(head, match ? "=" : "!=");
  1172. switch (right) {
  1173. case TOMOYO_NAME_UNION:
  1174. tomoyo_print_name_union_quoted
  1175. (head, names_p++);
  1176. break;
  1177. case TOMOYO_NUMBER_UNION:
  1178. tomoyo_print_number_union_nospace
  1179. (head, numbers_p++);
  1180. break;
  1181. default:
  1182. tomoyo_set_string(head,
  1183. tomoyo_condition_keyword[right]);
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. head->r.cond_step++;
  1189. /* fall through */
  1190. case 2:
  1191. if (!tomoyo_flush(head))
  1192. break;
  1193. head->r.cond_step++;
  1194. /* fall through */
  1195. case 3:
  1196. tomoyo_set_lf(head);
  1197. return true;
  1198. }
  1199. return false;
  1200. }
  1201. /**
  1202. * tomoyo_set_group - Print "acl_group " header keyword and category name.
  1203. *
  1204. * @head: Pointer to "struct tomoyo_io_buffer".
  1205. * @category: Category name.
  1206. *
  1207. * Returns nothing.
  1208. */
  1209. static void tomoyo_set_group(struct tomoyo_io_buffer *head,
  1210. const char *category)
  1211. {
  1212. if (head->type == TOMOYO_EXCEPTIONPOLICY) {
  1213. tomoyo_print_namespace(head);
  1214. tomoyo_io_printf(head, "acl_group %u ",
  1215. head->r.acl_group_index);
  1216. }
  1217. tomoyo_set_string(head, category);
  1218. }
  1219. /**
  1220. * tomoyo_print_entry - Print an ACL entry.
  1221. *
  1222. * @head: Pointer to "struct tomoyo_io_buffer".
  1223. * @acl: Pointer to an ACL entry.
  1224. *
  1225. * Returns true on success, false otherwise.
  1226. */
  1227. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  1228. struct tomoyo_acl_info *acl)
  1229. {
  1230. const u8 acl_type = acl->type;
  1231. bool first = true;
  1232. u8 bit;
  1233. if (head->r.print_cond_part)
  1234. goto print_cond_part;
  1235. if (acl->is_deleted)
  1236. return true;
  1237. if (!tomoyo_flush(head))
  1238. return false;
  1239. else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  1240. struct tomoyo_path_acl *ptr =
  1241. container_of(acl, typeof(*ptr), head);
  1242. const u16 perm = ptr->perm;
  1243. for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  1244. if (!(perm & (1 << bit)))
  1245. continue;
  1246. if (head->r.print_transition_related_only &&
  1247. bit != TOMOYO_TYPE_EXECUTE)
  1248. continue;
  1249. if (first) {
  1250. tomoyo_set_group(head, "file ");
  1251. first = false;
  1252. } else {
  1253. tomoyo_set_slash(head);
  1254. }
  1255. tomoyo_set_string(head, tomoyo_path_keyword[bit]);
  1256. }
  1257. if (first)
  1258. return true;
  1259. tomoyo_print_name_union(head, &ptr->name);
  1260. } else if (head->r.print_transition_related_only) {
  1261. return true;
  1262. } else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
  1263. struct tomoyo_path2_acl *ptr =
  1264. container_of(acl, typeof(*ptr), head);
  1265. const u8 perm = ptr->perm;
  1266. for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
  1267. if (!(perm & (1 << bit)))
  1268. continue;
  1269. if (first) {
  1270. tomoyo_set_group(head, "file ");
  1271. first = false;
  1272. } else {
  1273. tomoyo_set_slash(head);
  1274. }
  1275. tomoyo_set_string(head, tomoyo_mac_keywords
  1276. [tomoyo_pp2mac[bit]]);
  1277. }
  1278. if (first)
  1279. return true;
  1280. tomoyo_print_name_union(head, &ptr->name1);
  1281. tomoyo_print_name_union(head, &ptr->name2);
  1282. } else if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
  1283. struct tomoyo_path_number_acl *ptr =
  1284. container_of(acl, typeof(*ptr), head);
  1285. const u8 perm = ptr->perm;
  1286. for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
  1287. if (!(perm & (1 << bit)))
  1288. continue;
  1289. if (first) {
  1290. tomoyo_set_group(head, "file ");
  1291. first = false;
  1292. } else {
  1293. tomoyo_set_slash(head);
  1294. }
  1295. tomoyo_set_string(head, tomoyo_mac_keywords
  1296. [tomoyo_pn2mac[bit]]);
  1297. }
  1298. if (first)
  1299. return true;
  1300. tomoyo_print_name_union(head, &ptr->name);
  1301. tomoyo_print_number_union(head, &ptr->number);
  1302. } else if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
  1303. struct tomoyo_mkdev_acl *ptr =
  1304. container_of(acl, typeof(*ptr), head);
  1305. const u8 perm = ptr->perm;
  1306. for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
  1307. if (!(perm & (1 << bit)))
  1308. continue;
  1309. if (first) {
  1310. tomoyo_set_group(head, "file ");
  1311. first = false;
  1312. } else {
  1313. tomoyo_set_slash(head);
  1314. }
  1315. tomoyo_set_string(head, tomoyo_mac_keywords
  1316. [tomoyo_pnnn2mac[bit]]);
  1317. }
  1318. if (first)
  1319. return true;
  1320. tomoyo_print_name_union(head, &ptr->name);
  1321. tomoyo_print_number_union(head, &ptr->mode);
  1322. tomoyo_print_number_union(head, &ptr->major);
  1323. tomoyo_print_number_union(head, &ptr->minor);
  1324. } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
  1325. struct tomoyo_mount_acl *ptr =
  1326. container_of(acl, typeof(*ptr), head);
  1327. tomoyo_set_group(head, "file mount");
  1328. tomoyo_print_name_union(head, &ptr->dev_name);
  1329. tomoyo_print_name_union(head, &ptr->dir_name);
  1330. tomoyo_print_name_union(head, &ptr->fs_type);
  1331. tomoyo_print_number_union(head, &ptr->flags);
  1332. }
  1333. if (acl->cond) {
  1334. head->r.print_cond_part = true;
  1335. head->r.cond_step = 0;
  1336. if (!tomoyo_flush(head))
  1337. return false;
  1338. print_cond_part:
  1339. if (!tomoyo_print_condition(head, acl->cond))
  1340. return false;
  1341. head->r.print_cond_part = false;
  1342. } else {
  1343. tomoyo_set_lf(head);
  1344. }
  1345. return true;
  1346. }
  1347. /**
  1348. * tomoyo_read_domain2 - Read domain policy.
  1349. *
  1350. * @head: Pointer to "struct tomoyo_io_buffer".
  1351. * @list: Pointer to "struct list_head".
  1352. *
  1353. * Caller holds tomoyo_read_lock().
  1354. *
  1355. * Returns true on success, false otherwise.
  1356. */
  1357. static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head,
  1358. struct list_head *list)
  1359. {
  1360. list_for_each_cookie(head->r.acl, list) {
  1361. struct tomoyo_acl_info *ptr =
  1362. list_entry(head->r.acl, typeof(*ptr), list);
  1363. if (!tomoyo_print_entry(head, ptr))
  1364. return false;
  1365. }
  1366. head->r.acl = NULL;
  1367. return true;
  1368. }
  1369. /**
  1370. * tomoyo_read_domain - Read domain policy.
  1371. *
  1372. * @head: Pointer to "struct tomoyo_io_buffer".
  1373. *
  1374. * Caller holds tomoyo_read_lock().
  1375. */
  1376. static void tomoyo_read_domain(struct tomoyo_io_buffer *head)
  1377. {
  1378. if (head->r.eof)
  1379. return;
  1380. list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
  1381. struct tomoyo_domain_info *domain =
  1382. list_entry(head->r.domain, typeof(*domain), list);
  1383. switch (head->r.step) {
  1384. u8 i;
  1385. case 0:
  1386. if (domain->is_deleted &&
  1387. !head->r.print_this_domain_only)
  1388. continue;
  1389. /* Print domainname and flags. */
  1390. tomoyo_set_string(head, domain->domainname->name);
  1391. tomoyo_set_lf(head);
  1392. tomoyo_io_printf(head, "use_profile %u\n",
  1393. domain->profile);
  1394. tomoyo_io_printf(head, "use_group %u\n",
  1395. domain->group);
  1396. for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
  1397. if (domain->flags[i])
  1398. tomoyo_set_string(head, tomoyo_dif[i]);
  1399. head->r.step++;
  1400. tomoyo_set_lf(head);
  1401. /* fall through */
  1402. case 1:
  1403. if (!tomoyo_read_domain2(head, &domain->acl_info_list))
  1404. return;
  1405. head->r.step++;
  1406. if (!tomoyo_set_lf(head))
  1407. return;
  1408. /* fall through */
  1409. case 2:
  1410. head->r.step = 0;
  1411. if (head->r.print_this_domain_only)
  1412. goto done;
  1413. }
  1414. }
  1415. done:
  1416. head->r.eof = true;
  1417. }
  1418. /**
  1419. * tomoyo_write_pid: Specify PID to obtain domainname.
  1420. *
  1421. * @head: Pointer to "struct tomoyo_io_buffer".
  1422. *
  1423. * Returns 0.
  1424. */
  1425. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1426. {
  1427. head->r.eof = false;
  1428. return 0;
  1429. }
  1430. /**
  1431. * tomoyo_read_pid - Get domainname of the specified PID.
  1432. *
  1433. * @head: Pointer to "struct tomoyo_io_buffer".
  1434. *
  1435. * Returns the domainname which the specified PID is in on success,
  1436. * empty string otherwise.
  1437. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1438. * using read()/write() interface rather than sysctl() interface.
  1439. */
  1440. static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1441. {
  1442. char *buf = head->write_buf;
  1443. bool global_pid = false;
  1444. unsigned int pid;
  1445. struct task_struct *p;
  1446. struct tomoyo_domain_info *domain = NULL;
  1447. /* Accessing write_buf is safe because head->io_sem is held. */
  1448. if (!buf) {
  1449. head->r.eof = true;
  1450. return; /* Do nothing if open(O_RDONLY). */
  1451. }
  1452. if (head->r.w_pos || head->r.eof)
  1453. return;
  1454. head->r.eof = true;
  1455. if (tomoyo_str_starts(&buf, "global-pid "))
  1456. global_pid = true;
  1457. pid = (unsigned int) simple_strtoul(buf, NULL, 10);
  1458. rcu_read_lock();
  1459. read_lock(&tasklist_lock);
  1460. if (global_pid)
  1461. p = find_task_by_pid_ns(pid, &init_pid_ns);
  1462. else
  1463. p = find_task_by_vpid(pid);
  1464. if (p)
  1465. domain = tomoyo_real_domain(p);
  1466. read_unlock(&tasklist_lock);
  1467. rcu_read_unlock();
  1468. if (!domain)
  1469. return;
  1470. tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
  1471. tomoyo_set_string(head, domain->domainname->name);
  1472. }
  1473. /* String table for domain transition control keywords. */
  1474. static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
  1475. [TOMOYO_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ",
  1476. [TOMOYO_TRANSITION_CONTROL_RESET] = "reset_domain ",
  1477. [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
  1478. [TOMOYO_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ",
  1479. [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ",
  1480. [TOMOYO_TRANSITION_CONTROL_KEEP] = "keep_domain ",
  1481. };
  1482. /* String table for grouping keywords. */
  1483. static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
  1484. [TOMOYO_PATH_GROUP] = "path_group ",
  1485. [TOMOYO_NUMBER_GROUP] = "number_group ",
  1486. };
  1487. /**
  1488. * tomoyo_write_exception - Write exception policy.
  1489. *
  1490. * @head: Pointer to "struct tomoyo_io_buffer".
  1491. *
  1492. * Returns 0 on success, negative value otherwise.
  1493. *
  1494. * Caller holds tomoyo_read_lock().
  1495. */
  1496. static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
  1497. {
  1498. const bool is_delete = head->w.is_delete;
  1499. struct tomoyo_acl_param param = {
  1500. .ns = head->w.ns,
  1501. .is_delete = is_delete,
  1502. .data = head->write_buf,
  1503. };
  1504. u8 i;
  1505. if (tomoyo_str_starts(&param.data, "aggregator "))
  1506. return tomoyo_write_aggregator(&param);
  1507. for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
  1508. if (tomoyo_str_starts(&param.data, tomoyo_transition_type[i]))
  1509. return tomoyo_write_transition_control(&param, i);
  1510. for (i = 0; i < TOMOYO_MAX_GROUP; i++)
  1511. if (tomoyo_str_starts(&param.data, tomoyo_group_name[i]))
  1512. return tomoyo_write_group(&param, i);
  1513. if (tomoyo_str_starts(&param.data, "acl_group ")) {
  1514. unsigned int group;
  1515. char *data;
  1516. group = simple_strtoul(param.data, &data, 10);
  1517. if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
  1518. return tomoyo_write_domain2
  1519. (head->w.ns, &head->w.ns->acl_group[group],
  1520. data, is_delete);
  1521. }
  1522. return -EINVAL;
  1523. }
  1524. /**
  1525. * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  1526. *
  1527. * @head: Pointer to "struct tomoyo_io_buffer".
  1528. * @idx: Index number.
  1529. *
  1530. * Returns true on success, false otherwise.
  1531. *
  1532. * Caller holds tomoyo_read_lock().
  1533. */
  1534. static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
  1535. {
  1536. struct tomoyo_policy_namespace *ns =
  1537. container_of(head->r.ns, typeof(*ns), namespace_list);
  1538. struct list_head *list = &ns->group_list[idx];
  1539. list_for_each_cookie(head->r.group, list) {
  1540. struct tomoyo_group *group =
  1541. list_entry(head->r.group, typeof(*group), head.list);
  1542. list_for_each_cookie(head->r.acl, &group->member_list) {
  1543. struct tomoyo_acl_head *ptr =
  1544. list_entry(head->r.acl, typeof(*ptr), list);
  1545. if (ptr->is_deleted)
  1546. continue;
  1547. if (!tomoyo_flush(head))
  1548. return false;
  1549. tomoyo_print_namespace(head);
  1550. tomoyo_set_string(head, tomoyo_group_name[idx]);
  1551. tomoyo_set_string(head, group->group_name->name);
  1552. if (idx == TOMOYO_PATH_GROUP) {
  1553. tomoyo_set_space(head);
  1554. tomoyo_set_string(head, container_of
  1555. (ptr, struct tomoyo_path_group,
  1556. head)->member_name->name);
  1557. } else if (idx == TOMOYO_NUMBER_GROUP) {
  1558. tomoyo_print_number_union(head, &container_of
  1559. (ptr,
  1560. struct tomoyo_number_group,
  1561. head)->number);
  1562. }
  1563. tomoyo_set_lf(head);
  1564. }
  1565. head->r.acl = NULL;
  1566. }
  1567. head->r.group = NULL;
  1568. return true;
  1569. }
  1570. /**
  1571. * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
  1572. *
  1573. * @head: Pointer to "struct tomoyo_io_buffer".
  1574. * @idx: Index number.
  1575. *
  1576. * Returns true on success, false otherwise.
  1577. *
  1578. * Caller holds tomoyo_read_lock().
  1579. */
  1580. static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
  1581. {
  1582. struct tomoyo_policy_namespace *ns =
  1583. container_of(head->r.ns, typeof(*ns), namespace_list);
  1584. struct list_head *list = &ns->policy_list[idx];
  1585. list_for_each_cookie(head->r.acl, list) {
  1586. struct tomoyo_acl_head *acl =
  1587. container_of(head->r.acl, typeof(*acl), list);
  1588. if (acl->is_deleted)
  1589. continue;
  1590. if (!tomoyo_flush(head))
  1591. return false;
  1592. switch (idx) {
  1593. case TOMOYO_ID_TRANSITION_CONTROL:
  1594. {
  1595. struct tomoyo_transition_control *ptr =
  1596. container_of(acl, typeof(*ptr), head);
  1597. tomoyo_print_namespace(head);
  1598. tomoyo_set_string(head, tomoyo_transition_type
  1599. [ptr->type]);
  1600. tomoyo_set_string(head, ptr->program ?
  1601. ptr->program->name : "any");
  1602. tomoyo_set_string(head, " from ");
  1603. tomoyo_set_string(head, ptr->domainname ?
  1604. ptr->domainname->name :
  1605. "any");
  1606. }
  1607. break;
  1608. case TOMOYO_ID_AGGREGATOR:
  1609. {
  1610. struct tomoyo_aggregator *ptr =
  1611. container_of(acl, typeof(*ptr), head);
  1612. tomoyo_print_namespace(head);
  1613. tomoyo_set_string(head, "aggregator ");
  1614. tomoyo_set_string(head,
  1615. ptr->original_name->name);
  1616. tomoyo_set_space(head);
  1617. tomoyo_set_string(head,
  1618. ptr->aggregated_name->name);
  1619. }
  1620. break;
  1621. default:
  1622. continue;
  1623. }
  1624. tomoyo_set_lf(head);
  1625. }
  1626. head->r.acl = NULL;
  1627. return true;
  1628. }
  1629. /**
  1630. * tomoyo_read_exception - Read exception policy.
  1631. *
  1632. * @head: Pointer to "struct tomoyo_io_buffer".
  1633. *
  1634. * Caller holds tomoyo_read_lock().
  1635. */
  1636. static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
  1637. {
  1638. struct tomoyo_policy_namespace *ns =
  1639. container_of(head->r.ns, typeof(*ns), namespace_list);
  1640. if (head->r.eof)
  1641. return;
  1642. while (head->r.step < TOMOYO_MAX_POLICY &&
  1643. tomoyo_read_policy(head, head->r.step))
  1644. head->r.step++;
  1645. if (head->r.step < TOMOYO_MAX_POLICY)
  1646. return;
  1647. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
  1648. tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
  1649. head->r.step++;
  1650. if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
  1651. return;
  1652. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP
  1653. + TOMOYO_MAX_ACL_GROUPS) {
  1654. head->r.acl_group_index = head->r.step - TOMOYO_MAX_POLICY
  1655. - TOMOYO_MAX_GROUP;
  1656. if (!tomoyo_read_domain2(head, &ns->acl_group
  1657. [head->r.acl_group_index]))
  1658. return;
  1659. head->r.step++;
  1660. }
  1661. head->r.eof = true;
  1662. }
  1663. /* Wait queue for kernel -> userspace notification. */
  1664. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1665. /* Wait queue for userspace -> kernel notification. */
  1666. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_answer_wait);
  1667. /* Structure for query. */
  1668. struct tomoyo_query {
  1669. struct list_head list;
  1670. char *query;
  1671. size_t query_len;
  1672. unsigned int serial;
  1673. u8 timer;
  1674. u8 answer;
  1675. u8 retry;
  1676. };
  1677. /* The list for "struct tomoyo_query". */
  1678. static LIST_HEAD(tomoyo_query_list);
  1679. /* Lock for manipulating tomoyo_query_list. */
  1680. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1681. /*
  1682. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1683. * interface.
  1684. */
  1685. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1686. /**
  1687. * tomoyo_truncate - Truncate a line.
  1688. *
  1689. * @str: String to truncate.
  1690. *
  1691. * Returns length of truncated @str.
  1692. */
  1693. static int tomoyo_truncate(char *str)
  1694. {
  1695. char *start = str;
  1696. while (*(unsigned char *) str > (unsigned char) ' ')
  1697. str++;
  1698. *str = '\0';
  1699. return strlen(start) + 1;
  1700. }
  1701. /**
  1702. * tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
  1703. *
  1704. * @domain: Pointer to "struct tomoyo_domain_info".
  1705. * @header: Lines containing ACL.
  1706. *
  1707. * Returns nothing.
  1708. */
  1709. static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
  1710. {
  1711. char *buffer;
  1712. char *realpath = NULL;
  1713. char *argv0 = NULL;
  1714. char *symlink = NULL;
  1715. char *cp = strchr(header, '\n');
  1716. int len;
  1717. if (!cp)
  1718. return;
  1719. cp = strchr(cp + 1, '\n');
  1720. if (!cp)
  1721. return;
  1722. *cp++ = '\0';
  1723. len = strlen(cp) + 1;
  1724. /* strstr() will return NULL if ordering is wrong. */
  1725. if (*cp == 'f') {
  1726. argv0 = strstr(header, " argv[]={ \"");
  1727. if (argv0) {
  1728. argv0 += 10;
  1729. len += tomoyo_truncate(argv0) + 14;
  1730. }
  1731. realpath = strstr(header, " exec={ realpath=\"");
  1732. if (realpath) {
  1733. realpath += 8;
  1734. len += tomoyo_truncate(realpath) + 6;
  1735. }
  1736. symlink = strstr(header, " symlink.target=\"");
  1737. if (symlink)
  1738. len += tomoyo_truncate(symlink + 1) + 1;
  1739. }
  1740. buffer = kmalloc(len, GFP_NOFS);
  1741. if (!buffer)
  1742. return;
  1743. snprintf(buffer, len - 1, "%s", cp);
  1744. if (realpath)
  1745. tomoyo_addprintf(buffer, len, " exec.%s", realpath);
  1746. if (argv0)
  1747. tomoyo_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
  1748. if (symlink)
  1749. tomoyo_addprintf(buffer, len, "%s", symlink);
  1750. tomoyo_normalize_line(buffer);
  1751. if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
  1752. false))
  1753. tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
  1754. kfree(buffer);
  1755. }
  1756. /**
  1757. * tomoyo_supervisor - Ask for the supervisor's decision.
  1758. *
  1759. * @r: Pointer to "struct tomoyo_request_info".
  1760. * @fmt: The printf()'s format string, followed by parameters.
  1761. *
  1762. * Returns 0 if the supervisor decided to permit the access request which
  1763. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1764. * supervisor decided to retry the access request which violated the policy in
  1765. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1766. */
  1767. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1768. {
  1769. va_list args;
  1770. int error;
  1771. int len;
  1772. static unsigned int tomoyo_serial;
  1773. struct tomoyo_query entry = { };
  1774. bool quota_exceeded = false;
  1775. va_start(args, fmt);
  1776. len = vsnprintf((char *) &len, 1, fmt, args) + 1;
  1777. va_end(args);
  1778. /* Write /sys/kernel/security/tomoyo/audit. */
  1779. va_start(args, fmt);
  1780. tomoyo_write_log2(r, len, fmt, args);
  1781. va_end(args);
  1782. /* Nothing more to do if granted. */
  1783. if (r->granted)
  1784. return 0;
  1785. if (r->mode)
  1786. tomoyo_update_stat(r->mode);
  1787. switch (r->mode) {
  1788. case TOMOYO_CONFIG_ENFORCING:
  1789. error = -EPERM;
  1790. if (atomic_read(&tomoyo_query_observers))
  1791. break;
  1792. goto out;
  1793. case TOMOYO_CONFIG_LEARNING:
  1794. error = 0;
  1795. /* Check max_learning_entry parameter. */
  1796. if (tomoyo_domain_quota_is_ok(r))
  1797. break;
  1798. /* fall through */
  1799. default:
  1800. return 0;
  1801. }
  1802. /* Get message. */
  1803. va_start(args, fmt);
  1804. entry.query = tomoyo_init_log(r, len, fmt, args);
  1805. va_end(args);
  1806. if (!entry.query)
  1807. goto out;
  1808. entry.query_len = strlen(entry.query) + 1;
  1809. if (!error) {
  1810. tomoyo_add_entry(r->domain, entry.query);
  1811. goto out;
  1812. }
  1813. len = tomoyo_round2(entry.query_len);
  1814. spin_lock(&tomoyo_query_list_lock);
  1815. if (tomoyo_memory_quota[TOMOYO_MEMORY_QUERY] &&
  1816. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] + len
  1817. >= tomoyo_memory_quota[TOMOYO_MEMORY_QUERY]) {
  1818. quota_exceeded = true;
  1819. } else {
  1820. entry.serial = tomoyo_serial++;
  1821. entry.retry = r->retry;
  1822. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] += len;
  1823. list_add_tail(&entry.list, &tomoyo_query_list);
  1824. }
  1825. spin_unlock(&tomoyo_query_list_lock);
  1826. if (quota_exceeded)
  1827. goto out;
  1828. /* Give 10 seconds for supervisor's opinion. */
  1829. while (entry.timer < 10) {
  1830. wake_up_all(&tomoyo_query_wait);
  1831. if (wait_event_interruptible_timeout
  1832. (tomoyo_answer_wait, entry.answer ||
  1833. !atomic_read(&tomoyo_query_observers), HZ))
  1834. break;
  1835. else
  1836. entry.timer++;
  1837. }
  1838. spin_lock(&tomoyo_query_list_lock);
  1839. list_del(&entry.list);
  1840. tomoyo_memory_used[TOMOYO_MEMORY_QUERY] -= len;
  1841. spin_unlock(&tomoyo_query_list_lock);
  1842. switch (entry.answer) {
  1843. case 3: /* Asked to retry by administrator. */
  1844. error = TOMOYO_RETRY_REQUEST;
  1845. r->retry++;
  1846. break;
  1847. case 1:
  1848. /* Granted by administrator. */
  1849. error = 0;
  1850. break;
  1851. default:
  1852. /* Timed out or rejected by administrator. */
  1853. break;
  1854. }
  1855. out:
  1856. kfree(entry.query);
  1857. return error;
  1858. }
  1859. /**
  1860. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  1861. *
  1862. * @file: Pointer to "struct file".
  1863. * @wait: Pointer to "poll_table".
  1864. *
  1865. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  1866. *
  1867. * Waits for access requests which violated policy in enforcing mode.
  1868. */
  1869. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  1870. {
  1871. struct list_head *tmp;
  1872. bool found = false;
  1873. u8 i;
  1874. for (i = 0; i < 2; i++) {
  1875. spin_lock(&tomoyo_query_list_lock);
  1876. list_for_each(tmp, &tomoyo_query_list) {
  1877. struct tomoyo_query *ptr =
  1878. list_entry(tmp, typeof(*ptr), list);
  1879. if (ptr->answer)
  1880. continue;
  1881. found = true;
  1882. break;
  1883. }
  1884. spin_unlock(&tomoyo_query_list_lock);
  1885. if (found)
  1886. return POLLIN | POLLRDNORM;
  1887. if (i)
  1888. break;
  1889. poll_wait(file, &tomoyo_query_wait, wait);
  1890. }
  1891. return 0;
  1892. }
  1893. /**
  1894. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  1895. *
  1896. * @head: Pointer to "struct tomoyo_io_buffer".
  1897. */
  1898. static void tomoyo_read_query(struct tomoyo_io_buffer *head)
  1899. {
  1900. struct list_head *tmp;
  1901. unsigned int pos = 0;
  1902. size_t len = 0;
  1903. char *buf;
  1904. if (head->r.w_pos)
  1905. return;
  1906. if (head->read_buf) {
  1907. kfree(head->read_buf);
  1908. head->read_buf = NULL;
  1909. }
  1910. spin_lock(&tomoyo_query_list_lock);
  1911. list_for_each(tmp, &tomoyo_query_list) {
  1912. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1913. if (ptr->answer)
  1914. continue;
  1915. if (pos++ != head->r.query_index)
  1916. continue;
  1917. len = ptr->query_len;
  1918. break;
  1919. }
  1920. spin_unlock(&tomoyo_query_list_lock);
  1921. if (!len) {
  1922. head->r.query_index = 0;
  1923. return;
  1924. }
  1925. buf = kzalloc(len + 32, GFP_NOFS);
  1926. if (!buf)
  1927. return;
  1928. pos = 0;
  1929. spin_lock(&tomoyo_query_list_lock);
  1930. list_for_each(tmp, &tomoyo_query_list) {
  1931. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1932. if (ptr->answer)
  1933. continue;
  1934. if (pos++ != head->r.query_index)
  1935. continue;
  1936. /*
  1937. * Some query can be skipped because tomoyo_query_list
  1938. * can change, but I don't care.
  1939. */
  1940. if (len == ptr->query_len)
  1941. snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
  1942. ptr->retry, ptr->query);
  1943. break;
  1944. }
  1945. spin_unlock(&tomoyo_query_list_lock);
  1946. if (buf[0]) {
  1947. head->read_buf = buf;
  1948. head->r.w[head->r.w_pos++] = buf;
  1949. head->r.query_index++;
  1950. } else {
  1951. kfree(buf);
  1952. }
  1953. }
  1954. /**
  1955. * tomoyo_write_answer - Write the supervisor's decision.
  1956. *
  1957. * @head: Pointer to "struct tomoyo_io_buffer".
  1958. *
  1959. * Returns 0 on success, -EINVAL otherwise.
  1960. */
  1961. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  1962. {
  1963. char *data = head->write_buf;
  1964. struct list_head *tmp;
  1965. unsigned int serial;
  1966. unsigned int answer;
  1967. spin_lock(&tomoyo_query_list_lock);
  1968. list_for_each(tmp, &tomoyo_query_list) {
  1969. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1970. ptr->timer = 0;
  1971. }
  1972. spin_unlock(&tomoyo_query_list_lock);
  1973. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  1974. return -EINVAL;
  1975. spin_lock(&tomoyo_query_list_lock);
  1976. list_for_each(tmp, &tomoyo_query_list) {
  1977. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1978. if (ptr->serial != serial)
  1979. continue;
  1980. if (!ptr->answer)
  1981. ptr->answer = answer;
  1982. break;
  1983. }
  1984. spin_unlock(&tomoyo_query_list_lock);
  1985. return 0;
  1986. }
  1987. /**
  1988. * tomoyo_read_version: Get version.
  1989. *
  1990. * @head: Pointer to "struct tomoyo_io_buffer".
  1991. *
  1992. * Returns version information.
  1993. */
  1994. static void tomoyo_read_version(struct tomoyo_io_buffer *head)
  1995. {
  1996. if (!head->r.eof) {
  1997. tomoyo_io_printf(head, "2.4.0");
  1998. head->r.eof = true;
  1999. }
  2000. }
  2001. /**
  2002. * tomoyo_read_self_domain - Get the current process's domainname.
  2003. *
  2004. * @head: Pointer to "struct tomoyo_io_buffer".
  2005. *
  2006. * Returns the current process's domainname.
  2007. */
  2008. static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  2009. {
  2010. if (!head->r.eof) {
  2011. /*
  2012. * tomoyo_domain()->domainname != NULL
  2013. * because every process belongs to a domain and
  2014. * the domain's name cannot be NULL.
  2015. */
  2016. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  2017. head->r.eof = true;
  2018. }
  2019. }
  2020. /* String table for /sys/kernel/security/tomoyo/stat interface. */
  2021. static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
  2022. [TOMOYO_STAT_POLICY_UPDATES] = "update:",
  2023. [TOMOYO_STAT_POLICY_LEARNING] = "violation in learning mode:",
  2024. [TOMOYO_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
  2025. [TOMOYO_STAT_POLICY_ENFORCING] = "violation in enforcing mode:",
  2026. };
  2027. /* String table for /sys/kernel/security/tomoyo/stat interface. */
  2028. static const char * const tomoyo_memory_headers[TOMOYO_MAX_MEMORY_STAT] = {
  2029. [TOMOYO_MEMORY_POLICY] = "policy:",
  2030. [TOMOYO_MEMORY_AUDIT] = "audit log:",
  2031. [TOMOYO_MEMORY_QUERY] = "query message:",
  2032. };
  2033. /* Timestamp counter for last updated. */
  2034. static unsigned int tomoyo_stat_updated[TOMOYO_MAX_POLICY_STAT];
  2035. /* Counter for number of updates. */
  2036. static unsigned int tomoyo_stat_modified[TOMOYO_MAX_POLICY_STAT];
  2037. /**
  2038. * tomoyo_update_stat - Update statistic counters.
  2039. *
  2040. * @index: Index for policy type.
  2041. *
  2042. * Returns nothing.
  2043. */
  2044. void tomoyo_update_stat(const u8 index)
  2045. {
  2046. struct timeval tv;
  2047. do_gettimeofday(&tv);
  2048. /*
  2049. * I don't use atomic operations because race condition is not fatal.
  2050. */
  2051. tomoyo_stat_updated[index]++;
  2052. tomoyo_stat_modified[index] = tv.tv_sec;
  2053. }
  2054. /**
  2055. * tomoyo_read_stat - Read statistic data.
  2056. *
  2057. * @head: Pointer to "struct tomoyo_io_buffer".
  2058. *
  2059. * Returns nothing.
  2060. */
  2061. static void tomoyo_read_stat(struct tomoyo_io_buffer *head)
  2062. {
  2063. u8 i;
  2064. unsigned int total = 0;
  2065. if (head->r.eof)
  2066. return;
  2067. for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) {
  2068. tomoyo_io_printf(head, "Policy %-30s %10u",
  2069. tomoyo_policy_headers[i],
  2070. tomoyo_stat_updated[i]);
  2071. if (tomoyo_stat_modified[i]) {
  2072. struct tomoyo_time stamp;
  2073. tomoyo_convert_time(tomoyo_stat_modified[i], &stamp);
  2074. tomoyo_io_printf(head, " (Last: %04u/%02u/%02u "
  2075. "%02u:%02u:%02u)",
  2076. stamp.year, stamp.month, stamp.day,
  2077. stamp.hour, stamp.min, stamp.sec);
  2078. }
  2079. tomoyo_set_lf(head);
  2080. }
  2081. for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
  2082. unsigned int used = tomoyo_memory_used[i];
  2083. total += used;
  2084. tomoyo_io_printf(head, "Memory used by %-22s %10u",
  2085. tomoyo_memory_headers[i], used);
  2086. used = tomoyo_memory_quota[i];
  2087. if (used)
  2088. tomoyo_io_printf(head, " (Quota: %10u)", used);
  2089. tomoyo_set_lf(head);
  2090. }
  2091. tomoyo_io_printf(head, "Total memory used: %10u\n",
  2092. total);
  2093. head->r.eof = true;
  2094. }
  2095. /**
  2096. * tomoyo_write_stat - Set memory quota.
  2097. *
  2098. * @head: Pointer to "struct tomoyo_io_buffer".
  2099. *
  2100. * Returns 0.
  2101. */
  2102. static int tomoyo_write_stat(struct tomoyo_io_buffer *head)
  2103. {
  2104. char *data = head->write_buf;
  2105. u8 i;
  2106. if (tomoyo_str_starts(&data, "Memory used by "))
  2107. for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
  2108. if (tomoyo_str_starts(&data, tomoyo_memory_headers[i]))
  2109. sscanf(data, "%u", &tomoyo_memory_quota[i]);
  2110. return 0;
  2111. }
  2112. /**
  2113. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  2114. *
  2115. * @type: Type of interface.
  2116. * @file: Pointer to "struct file".
  2117. *
  2118. * Returns 0 on success, negative value otherwise.
  2119. */
  2120. int tomoyo_open_control(const u8 type, struct file *file)
  2121. {
  2122. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  2123. if (!head)
  2124. return -ENOMEM;
  2125. mutex_init(&head->io_sem);
  2126. head->type = type;
  2127. switch (type) {
  2128. case TOMOYO_DOMAINPOLICY:
  2129. /* /sys/kernel/security/tomoyo/domain_policy */
  2130. head->write = tomoyo_write_domain;
  2131. head->read = tomoyo_read_domain;
  2132. break;
  2133. case TOMOYO_EXCEPTIONPOLICY:
  2134. /* /sys/kernel/security/tomoyo/exception_policy */
  2135. head->write = tomoyo_write_exception;
  2136. head->read = tomoyo_read_exception;
  2137. break;
  2138. case TOMOYO_AUDIT:
  2139. /* /sys/kernel/security/tomoyo/audit */
  2140. head->poll = tomoyo_poll_log;
  2141. head->read = tomoyo_read_log;
  2142. break;
  2143. case TOMOYO_SELFDOMAIN:
  2144. /* /sys/kernel/security/tomoyo/self_domain */
  2145. head->read = tomoyo_read_self_domain;
  2146. break;
  2147. case TOMOYO_PROCESS_STATUS:
  2148. /* /sys/kernel/security/tomoyo/.process_status */
  2149. head->write = tomoyo_write_pid;
  2150. head->read = tomoyo_read_pid;
  2151. break;
  2152. case TOMOYO_VERSION:
  2153. /* /sys/kernel/security/tomoyo/version */
  2154. head->read = tomoyo_read_version;
  2155. head->readbuf_size = 128;
  2156. break;
  2157. case TOMOYO_STAT:
  2158. /* /sys/kernel/security/tomoyo/stat */
  2159. head->write = tomoyo_write_stat;
  2160. head->read = tomoyo_read_stat;
  2161. head->readbuf_size = 1024;
  2162. break;
  2163. case TOMOYO_PROFILE:
  2164. /* /sys/kernel/security/tomoyo/profile */
  2165. head->write = tomoyo_write_profile;
  2166. head->read = tomoyo_read_profile;
  2167. break;
  2168. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  2169. head->poll = tomoyo_poll_query;
  2170. head->write = tomoyo_write_answer;
  2171. head->read = tomoyo_read_query;
  2172. break;
  2173. case TOMOYO_MANAGER:
  2174. /* /sys/kernel/security/tomoyo/manager */
  2175. head->write = tomoyo_write_manager;
  2176. head->read = tomoyo_read_manager;
  2177. break;
  2178. }
  2179. if (!(file->f_mode & FMODE_READ)) {
  2180. /*
  2181. * No need to allocate read_buf since it is not opened
  2182. * for reading.
  2183. */
  2184. head->read = NULL;
  2185. head->poll = NULL;
  2186. } else if (!head->poll) {
  2187. /* Don't allocate read_buf for poll() access. */
  2188. if (!head->readbuf_size)
  2189. head->readbuf_size = 4096 * 2;
  2190. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  2191. if (!head->read_buf) {
  2192. kfree(head);
  2193. return -ENOMEM;
  2194. }
  2195. }
  2196. if (!(file->f_mode & FMODE_WRITE)) {
  2197. /*
  2198. * No need to allocate write_buf since it is not opened
  2199. * for writing.
  2200. */
  2201. head->write = NULL;
  2202. } else if (head->write) {
  2203. head->writebuf_size = 4096 * 2;
  2204. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  2205. if (!head->write_buf) {
  2206. kfree(head->read_buf);
  2207. kfree(head);
  2208. return -ENOMEM;
  2209. }
  2210. }
  2211. /*
  2212. * If the file is /sys/kernel/security/tomoyo/query , increment the
  2213. * observer counter.
  2214. * The obserber counter is used by tomoyo_supervisor() to see if
  2215. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  2216. */
  2217. if (type == TOMOYO_QUERY)
  2218. atomic_inc(&tomoyo_query_observers);
  2219. file->private_data = head;
  2220. tomoyo_notify_gc(head, true);
  2221. return 0;
  2222. }
  2223. /**
  2224. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  2225. *
  2226. * @file: Pointer to "struct file".
  2227. * @wait: Pointer to "poll_table".
  2228. *
  2229. * Waits for read readiness.
  2230. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd and
  2231. * /sys/kernel/security/tomoyo/audit is handled by /usr/sbin/tomoyo-auditd.
  2232. */
  2233. int tomoyo_poll_control(struct file *file, poll_table *wait)
  2234. {
  2235. struct tomoyo_io_buffer *head = file->private_data;
  2236. if (!head->poll)
  2237. return -ENOSYS;
  2238. return head->poll(file, wait);
  2239. }
  2240. /**
  2241. * tomoyo_set_namespace_cursor - Set namespace to read.
  2242. *
  2243. * @head: Pointer to "struct tomoyo_io_buffer".
  2244. *
  2245. * Returns nothing.
  2246. */
  2247. static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
  2248. {
  2249. struct list_head *ns;
  2250. if (head->type != TOMOYO_EXCEPTIONPOLICY &&
  2251. head->type != TOMOYO_PROFILE)
  2252. return;
  2253. /*
  2254. * If this is the first read, or reading previous namespace finished
  2255. * and has more namespaces to read, update the namespace cursor.
  2256. */
  2257. ns = head->r.ns;
  2258. if (!ns || (head->r.eof && ns->next != &tomoyo_namespace_list)) {
  2259. /* Clearing is OK because tomoyo_flush() returned true. */
  2260. memset(&head->r, 0, sizeof(head->r));
  2261. head->r.ns = ns ? ns->next : tomoyo_namespace_list.next;
  2262. }
  2263. }
  2264. /**
  2265. * tomoyo_has_more_namespace - Check for unread namespaces.
  2266. *
  2267. * @head: Pointer to "struct tomoyo_io_buffer".
  2268. *
  2269. * Returns true if we have more entries to print, false otherwise.
  2270. */
  2271. static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
  2272. {
  2273. return (head->type == TOMOYO_EXCEPTIONPOLICY ||
  2274. head->type == TOMOYO_PROFILE) && head->r.eof &&
  2275. head->r.ns->next != &tomoyo_namespace_list;
  2276. }
  2277. /**
  2278. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  2279. *
  2280. * @head: Pointer to "struct tomoyo_io_buffer".
  2281. * @buffer: Poiner to buffer to write to.
  2282. * @buffer_len: Size of @buffer.
  2283. *
  2284. * Returns bytes read on success, negative value otherwise.
  2285. */
  2286. ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  2287. const int buffer_len)
  2288. {
  2289. int len;
  2290. int idx;
  2291. if (!head->read)
  2292. return -ENOSYS;
  2293. if (mutex_lock_interruptible(&head->io_sem))
  2294. return -EINTR;
  2295. head->read_user_buf = buffer;
  2296. head->read_user_buf_avail = buffer_len;
  2297. idx = tomoyo_read_lock();
  2298. if (tomoyo_flush(head))
  2299. /* Call the policy handler. */
  2300. do {
  2301. tomoyo_set_namespace_cursor(head);
  2302. head->read(head);
  2303. } while (tomoyo_flush(head) &&
  2304. tomoyo_has_more_namespace(head));
  2305. tomoyo_read_unlock(idx);
  2306. len = head->read_user_buf - buffer;
  2307. mutex_unlock(&head->io_sem);
  2308. return len;
  2309. }
  2310. /**
  2311. * tomoyo_parse_policy - Parse a policy line.
  2312. *
  2313. * @head: Poiter to "struct tomoyo_io_buffer".
  2314. * @line: Line to parse.
  2315. *
  2316. * Returns 0 on success, negative value otherwise.
  2317. *
  2318. * Caller holds tomoyo_read_lock().
  2319. */
  2320. static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
  2321. {
  2322. /* Delete request? */
  2323. head->w.is_delete = !strncmp(line, "delete ", 7);
  2324. if (head->w.is_delete)
  2325. memmove(line, line + 7, strlen(line + 7) + 1);
  2326. /* Selecting namespace to update. */
  2327. if (head->type == TOMOYO_EXCEPTIONPOLICY ||
  2328. head->type == TOMOYO_PROFILE) {
  2329. if (*line == '<') {
  2330. char *cp = strchr(line, ' ');
  2331. if (cp) {
  2332. *cp++ = '\0';
  2333. head->w.ns = tomoyo_assign_namespace(line);
  2334. memmove(line, cp, strlen(cp) + 1);
  2335. } else
  2336. head->w.ns = NULL;
  2337. } else
  2338. head->w.ns = &tomoyo_kernel_namespace;
  2339. /* Don't allow updating if namespace is invalid. */
  2340. if (!head->w.ns)
  2341. return -ENOENT;
  2342. }
  2343. /* Do the update. */
  2344. return head->write(head);
  2345. }
  2346. /**
  2347. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  2348. *
  2349. * @head: Pointer to "struct tomoyo_io_buffer".
  2350. * @buffer: Pointer to buffer to read from.
  2351. * @buffer_len: Size of @buffer.
  2352. *
  2353. * Returns @buffer_len on success, negative value otherwise.
  2354. */
  2355. ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
  2356. const char __user *buffer, const int buffer_len)
  2357. {
  2358. int error = buffer_len;
  2359. size_t avail_len = buffer_len;
  2360. char *cp0 = head->write_buf;
  2361. int idx;
  2362. if (!head->write)
  2363. return -ENOSYS;
  2364. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  2365. return -EFAULT;
  2366. if (mutex_lock_interruptible(&head->io_sem))
  2367. return -EINTR;
  2368. idx = tomoyo_read_lock();
  2369. /* Read a line and dispatch it to the policy handler. */
  2370. while (avail_len > 0) {
  2371. char c;
  2372. if (head->w.avail >= head->writebuf_size - 1) {
  2373. const int len = head->writebuf_size * 2;
  2374. char *cp = kzalloc(len, GFP_NOFS);
  2375. if (!cp) {
  2376. error = -ENOMEM;
  2377. break;
  2378. }
  2379. memmove(cp, cp0, head->w.avail);
  2380. kfree(cp0);
  2381. head->write_buf = cp;
  2382. cp0 = cp;
  2383. head->writebuf_size = len;
  2384. }
  2385. if (get_user(c, buffer)) {
  2386. error = -EFAULT;
  2387. break;
  2388. }
  2389. buffer++;
  2390. avail_len--;
  2391. cp0[head->w.avail++] = c;
  2392. if (c != '\n')
  2393. continue;
  2394. cp0[head->w.avail - 1] = '\0';
  2395. head->w.avail = 0;
  2396. tomoyo_normalize_line(cp0);
  2397. if (!strcmp(cp0, "reset")) {
  2398. head->w.ns = &tomoyo_kernel_namespace;
  2399. head->w.domain = NULL;
  2400. memset(&head->r, 0, sizeof(head->r));
  2401. continue;
  2402. }
  2403. /* Don't allow updating policies by non manager programs. */
  2404. switch (head->type) {
  2405. case TOMOYO_PROCESS_STATUS:
  2406. /* This does not write anything. */
  2407. break;
  2408. case TOMOYO_DOMAINPOLICY:
  2409. if (tomoyo_select_domain(head, cp0))
  2410. continue;
  2411. /* fall through */
  2412. case TOMOYO_EXCEPTIONPOLICY:
  2413. if (!strcmp(cp0, "select transition_only")) {
  2414. head->r.print_transition_related_only = true;
  2415. continue;
  2416. }
  2417. /* fall through */
  2418. default:
  2419. if (!tomoyo_manager()) {
  2420. error = -EPERM;
  2421. goto out;
  2422. }
  2423. }
  2424. switch (tomoyo_parse_policy(head, cp0)) {
  2425. case -EPERM:
  2426. error = -EPERM;
  2427. goto out;
  2428. case 0:
  2429. switch (head->type) {
  2430. case TOMOYO_DOMAINPOLICY:
  2431. case TOMOYO_EXCEPTIONPOLICY:
  2432. case TOMOYO_STAT:
  2433. case TOMOYO_PROFILE:
  2434. case TOMOYO_MANAGER:
  2435. tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
  2436. break;
  2437. default:
  2438. break;
  2439. }
  2440. break;
  2441. }
  2442. }
  2443. out:
  2444. tomoyo_read_unlock(idx);
  2445. mutex_unlock(&head->io_sem);
  2446. return error;
  2447. }
  2448. /**
  2449. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  2450. *
  2451. * @head: Pointer to "struct tomoyo_io_buffer".
  2452. *
  2453. * Returns 0.
  2454. */
  2455. int tomoyo_close_control(struct tomoyo_io_buffer *head)
  2456. {
  2457. /*
  2458. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  2459. * observer counter.
  2460. */
  2461. if (head->type == TOMOYO_QUERY &&
  2462. atomic_dec_and_test(&tomoyo_query_observers))
  2463. wake_up_all(&tomoyo_answer_wait);
  2464. tomoyo_notify_gc(head, false);
  2465. return 0;
  2466. }
  2467. /**
  2468. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  2469. */
  2470. void tomoyo_check_profile(void)
  2471. {
  2472. struct tomoyo_domain_info *domain;
  2473. const int idx = tomoyo_read_lock();
  2474. tomoyo_policy_loaded = true;
  2475. printk(KERN_INFO "TOMOYO: 2.4.0\n");
  2476. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  2477. const u8 profile = domain->profile;
  2478. const struct tomoyo_policy_namespace *ns = domain->ns;
  2479. if (ns->profile_version != 20100903)
  2480. printk(KERN_ERR
  2481. "Profile version %u is not supported.\n",
  2482. ns->profile_version);
  2483. else if (!ns->profile_ptr[profile])
  2484. printk(KERN_ERR
  2485. "Profile %u (used by '%s') is not defined.\n",
  2486. profile, domain->domainname->name);
  2487. else
  2488. continue;
  2489. printk(KERN_ERR
  2490. "Userland tools for TOMOYO 2.4 must be installed and "
  2491. "policy must be initialized.\n");
  2492. printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.4/ "
  2493. "for more information.\n");
  2494. panic("STOP!");
  2495. }
  2496. tomoyo_read_unlock(idx);
  2497. printk(KERN_INFO "Mandatory Access Control activated.\n");
  2498. }
  2499. /**
  2500. * tomoyo_load_builtin_policy - Load built-in policy.
  2501. *
  2502. * Returns nothing.
  2503. */
  2504. void __init tomoyo_load_builtin_policy(void)
  2505. {
  2506. /*
  2507. * This include file is manually created and contains built-in policy
  2508. * named "tomoyo_builtin_profile", "tomoyo_builtin_exception_policy",
  2509. * "tomoyo_builtin_domain_policy", "tomoyo_builtin_manager",
  2510. * "tomoyo_builtin_stat" in the form of "static char [] __initdata".
  2511. */
  2512. #include "builtin-policy.h"
  2513. u8 i;
  2514. const int idx = tomoyo_read_lock();
  2515. for (i = 0; i < 5; i++) {
  2516. struct tomoyo_io_buffer head = { };
  2517. char *start = "";
  2518. switch (i) {
  2519. case 0:
  2520. start = tomoyo_builtin_profile;
  2521. head.type = TOMOYO_PROFILE;
  2522. head.write = tomoyo_write_profile;
  2523. break;
  2524. case 1:
  2525. start = tomoyo_builtin_exception_policy;
  2526. head.type = TOMOYO_EXCEPTIONPOLICY;
  2527. head.write = tomoyo_write_exception;
  2528. break;
  2529. case 2:
  2530. start = tomoyo_builtin_domain_policy;
  2531. head.type = TOMOYO_DOMAINPOLICY;
  2532. head.write = tomoyo_write_domain;
  2533. break;
  2534. case 3:
  2535. start = tomoyo_builtin_manager;
  2536. head.type = TOMOYO_MANAGER;
  2537. head.write = tomoyo_write_manager;
  2538. break;
  2539. case 4:
  2540. start = tomoyo_builtin_stat;
  2541. head.type = TOMOYO_STAT;
  2542. head.write = tomoyo_write_stat;
  2543. break;
  2544. }
  2545. while (1) {
  2546. char *end = strchr(start, '\n');
  2547. if (!end)
  2548. break;
  2549. *end = '\0';
  2550. tomoyo_normalize_line(start);
  2551. head.write_buf = start;
  2552. tomoyo_parse_policy(&head, start);
  2553. start = end + 1;
  2554. }
  2555. }
  2556. tomoyo_read_unlock(idx);
  2557. #ifdef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
  2558. tomoyo_check_profile();
  2559. #endif
  2560. }