common.c 61 KB

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