common.c 53 KB

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