common.c 58 KB

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