common.c 69 KB

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