common.c 55 KB

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