common.c 54 KB

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