common.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  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. head->r.eof = !domain;
  734. head->r.domain = &domain->list;
  735. tomoyo_io_printf(head, "# select %s\n", data);
  736. if (domain && domain->is_deleted)
  737. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  738. return true;
  739. }
  740. /**
  741. * tomoyo_delete_domain - Delete a domain.
  742. *
  743. * @domainname: The name of domain.
  744. *
  745. * Returns 0.
  746. *
  747. * Caller holds tomoyo_read_lock().
  748. */
  749. static int tomoyo_delete_domain(char *domainname)
  750. {
  751. struct tomoyo_domain_info *domain;
  752. struct tomoyo_path_info name;
  753. name.name = domainname;
  754. tomoyo_fill_path_info(&name);
  755. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  756. return 0;
  757. /* Is there an active domain? */
  758. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  759. /* Never delete tomoyo_kernel_domain */
  760. if (domain == &tomoyo_kernel_domain)
  761. continue;
  762. if (domain->is_deleted ||
  763. tomoyo_pathcmp(domain->domainname, &name))
  764. continue;
  765. domain->is_deleted = true;
  766. break;
  767. }
  768. mutex_unlock(&tomoyo_policy_lock);
  769. return 0;
  770. }
  771. /**
  772. * tomoyo_write_domain2 - Write domain policy.
  773. *
  774. * @head: Pointer to "struct tomoyo_io_buffer".
  775. *
  776. * Returns 0 on success, negative value otherwise.
  777. *
  778. * Caller holds tomoyo_read_lock().
  779. */
  780. static int tomoyo_write_domain2(char *data, struct tomoyo_domain_info *domain,
  781. const bool is_delete)
  782. {
  783. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
  784. return tomoyo_write_mount(data, domain, is_delete);
  785. return tomoyo_write_file(data, domain, is_delete);
  786. }
  787. /**
  788. * tomoyo_write_domain - Write domain policy.
  789. *
  790. * @head: Pointer to "struct tomoyo_io_buffer".
  791. *
  792. * Returns 0 on success, negative value otherwise.
  793. *
  794. * Caller holds tomoyo_read_lock().
  795. */
  796. static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
  797. {
  798. char *data = head->write_buf;
  799. struct tomoyo_domain_info *domain = head->write_var1;
  800. bool is_delete = false;
  801. bool is_select = false;
  802. unsigned int profile;
  803. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  804. is_delete = true;
  805. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  806. is_select = true;
  807. if (is_select && tomoyo_select_one(head, data))
  808. return 0;
  809. /* Don't allow updating policies by non manager programs. */
  810. if (!tomoyo_manager())
  811. return -EPERM;
  812. if (tomoyo_domain_def(data)) {
  813. domain = NULL;
  814. if (is_delete)
  815. tomoyo_delete_domain(data);
  816. else if (is_select)
  817. domain = tomoyo_find_domain(data);
  818. else
  819. domain = tomoyo_assign_domain(data, 0);
  820. head->write_var1 = domain;
  821. return 0;
  822. }
  823. if (!domain)
  824. return -EINVAL;
  825. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  826. && profile < TOMOYO_MAX_PROFILES) {
  827. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  828. domain->profile = (u8) profile;
  829. return 0;
  830. }
  831. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  832. domain->ignore_global_allow_read = !is_delete;
  833. return 0;
  834. }
  835. if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
  836. domain->quota_warned = !is_delete;
  837. return 0;
  838. }
  839. if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
  840. domain->transition_failed = !is_delete;
  841. return 0;
  842. }
  843. return tomoyo_write_domain2(data, domain, is_delete);
  844. }
  845. /**
  846. * tomoyo_fns - Find next set bit.
  847. *
  848. * @perm: 8 bits value.
  849. * @bit: First bit to find.
  850. *
  851. * Returns next on-bit on success, 8 otherwise.
  852. */
  853. static u8 tomoyo_fns(const u8 perm, u8 bit)
  854. {
  855. for ( ; bit < 8; bit++)
  856. if (perm & (1 << bit))
  857. break;
  858. return bit;
  859. }
  860. /**
  861. * tomoyo_print_entry - Print an ACL entry.
  862. *
  863. * @head: Pointer to "struct tomoyo_io_buffer".
  864. * @acl: Pointer to an ACL entry.
  865. *
  866. * Returns true on success, false otherwise.
  867. */
  868. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  869. struct tomoyo_acl_info *acl)
  870. {
  871. const u8 acl_type = acl->type;
  872. u8 bit;
  873. if (acl->is_deleted)
  874. return true;
  875. next:
  876. bit = head->r.bit;
  877. if (!tomoyo_flush(head))
  878. return false;
  879. else if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  880. struct tomoyo_path_acl *ptr =
  881. container_of(acl, typeof(*ptr), head);
  882. const u16 perm = ptr->perm;
  883. for ( ; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  884. if (!(perm & (1 << bit)))
  885. continue;
  886. if (head->r.print_execute_only &&
  887. bit != TOMOYO_TYPE_EXECUTE)
  888. continue;
  889. /* Print "read/write" instead of "read" and "write". */
  890. if ((bit == TOMOYO_TYPE_READ ||
  891. bit == TOMOYO_TYPE_WRITE)
  892. && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  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,
  994. TOMOYO_KEYWORD_USE_PROFILE "%u\n",
  995. domain->profile);
  996. if (domain->quota_warned)
  997. tomoyo_set_string(head, "quota_exceeded\n");
  998. if (domain->transition_failed)
  999. tomoyo_set_string(head, "transition_failed\n");
  1000. if (domain->ignore_global_allow_read)
  1001. tomoyo_set_string(head,
  1002. TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ
  1003. "\n");
  1004. head->r.step++;
  1005. tomoyo_set_lf(head);
  1006. /* fall through */
  1007. case 1:
  1008. if (!tomoyo_read_domain2(head, domain))
  1009. return;
  1010. head->r.step++;
  1011. if (!tomoyo_set_lf(head))
  1012. return;
  1013. /* fall through */
  1014. case 2:
  1015. head->r.step = 0;
  1016. if (head->r.print_this_domain_only)
  1017. goto done;
  1018. }
  1019. }
  1020. done:
  1021. head->r.eof = true;
  1022. }
  1023. /**
  1024. * tomoyo_write_domain_profile - Assign profile for specified domain.
  1025. *
  1026. * @head: Pointer to "struct tomoyo_io_buffer".
  1027. *
  1028. * Returns 0 on success, -EINVAL otherwise.
  1029. *
  1030. * This is equivalent to doing
  1031. *
  1032. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  1033. * /usr/sbin/tomoyo-loadpolicy -d
  1034. *
  1035. * Caller holds tomoyo_read_lock().
  1036. */
  1037. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  1038. {
  1039. char *data = head->write_buf;
  1040. char *cp = strchr(data, ' ');
  1041. struct tomoyo_domain_info *domain;
  1042. unsigned long profile;
  1043. if (!cp)
  1044. return -EINVAL;
  1045. *cp = '\0';
  1046. domain = tomoyo_find_domain(cp + 1);
  1047. if (strict_strtoul(data, 10, &profile))
  1048. return -EINVAL;
  1049. if (domain && profile < TOMOYO_MAX_PROFILES
  1050. && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
  1051. domain->profile = (u8) profile;
  1052. return 0;
  1053. }
  1054. /**
  1055. * tomoyo_read_domain_profile - Read only domainname and profile.
  1056. *
  1057. * @head: Pointer to "struct tomoyo_io_buffer".
  1058. *
  1059. * Returns list of profile number and domainname pairs.
  1060. *
  1061. * This is equivalent to doing
  1062. *
  1063. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  1064. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  1065. * domainname = $0; } else if ( $1 == "use_profile" ) {
  1066. * print $2 " " domainname; domainname = ""; } } ; '
  1067. *
  1068. * Caller holds tomoyo_read_lock().
  1069. */
  1070. static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1071. {
  1072. if (head->r.eof)
  1073. return;
  1074. list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
  1075. struct tomoyo_domain_info *domain =
  1076. list_entry(head->r.domain, typeof(*domain), list);
  1077. if (domain->is_deleted)
  1078. continue;
  1079. if (!tomoyo_flush(head))
  1080. return;
  1081. tomoyo_io_printf(head, "%u ", domain->profile);
  1082. tomoyo_set_string(head, domain->domainname->name);
  1083. tomoyo_set_lf(head);
  1084. }
  1085. head->r.eof = true;
  1086. }
  1087. /**
  1088. * tomoyo_write_pid: Specify PID to obtain domainname.
  1089. *
  1090. * @head: Pointer to "struct tomoyo_io_buffer".
  1091. *
  1092. * Returns 0.
  1093. */
  1094. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1095. {
  1096. head->r.eof = false;
  1097. return 0;
  1098. }
  1099. /**
  1100. * tomoyo_read_pid - Get domainname of the specified PID.
  1101. *
  1102. * @head: Pointer to "struct tomoyo_io_buffer".
  1103. *
  1104. * Returns the domainname which the specified PID is in on success,
  1105. * empty string otherwise.
  1106. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1107. * using read()/write() interface rather than sysctl() interface.
  1108. */
  1109. static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1110. {
  1111. char *buf = head->write_buf;
  1112. bool global_pid = false;
  1113. unsigned int pid;
  1114. struct task_struct *p;
  1115. struct tomoyo_domain_info *domain = NULL;
  1116. /* Accessing write_buf is safe because head->io_sem is held. */
  1117. if (!buf) {
  1118. head->r.eof = true;
  1119. return; /* Do nothing if open(O_RDONLY). */
  1120. }
  1121. if (head->r.w_pos || head->r.eof)
  1122. return;
  1123. head->r.eof = true;
  1124. if (tomoyo_str_starts(&buf, "global-pid "))
  1125. global_pid = true;
  1126. pid = (unsigned int) simple_strtoul(buf, NULL, 10);
  1127. rcu_read_lock();
  1128. read_lock(&tasklist_lock);
  1129. if (global_pid)
  1130. p = find_task_by_pid_ns(pid, &init_pid_ns);
  1131. else
  1132. p = find_task_by_vpid(pid);
  1133. if (p)
  1134. domain = tomoyo_real_domain(p);
  1135. read_unlock(&tasklist_lock);
  1136. rcu_read_unlock();
  1137. if (!domain)
  1138. return;
  1139. tomoyo_io_printf(head, "%u %u ", pid, domain->profile);
  1140. tomoyo_set_string(head, domain->domainname->name);
  1141. }
  1142. static const char *tomoyo_transition_type[TOMOYO_MAX_TRANSITION_TYPE] = {
  1143. [TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE]
  1144. = TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN,
  1145. [TOMOYO_TRANSITION_CONTROL_INITIALIZE]
  1146. = TOMOYO_KEYWORD_INITIALIZE_DOMAIN,
  1147. [TOMOYO_TRANSITION_CONTROL_NO_KEEP] = TOMOYO_KEYWORD_NO_KEEP_DOMAIN,
  1148. [TOMOYO_TRANSITION_CONTROL_KEEP] = TOMOYO_KEYWORD_KEEP_DOMAIN
  1149. };
  1150. static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
  1151. [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
  1152. [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
  1153. };
  1154. /**
  1155. * tomoyo_write_exception - Write exception policy.
  1156. *
  1157. * @head: Pointer to "struct tomoyo_io_buffer".
  1158. *
  1159. * Returns 0 on success, negative value otherwise.
  1160. *
  1161. * Caller holds tomoyo_read_lock().
  1162. */
  1163. static int tomoyo_write_exception(struct tomoyo_io_buffer *head)
  1164. {
  1165. char *data = head->write_buf;
  1166. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1167. u8 i;
  1168. static const struct {
  1169. const char *keyword;
  1170. int (*write) (char *, const bool);
  1171. } tomoyo_callback[4] = {
  1172. { TOMOYO_KEYWORD_AGGREGATOR, tomoyo_write_aggregator },
  1173. { TOMOYO_KEYWORD_FILE_PATTERN, tomoyo_write_pattern },
  1174. { TOMOYO_KEYWORD_DENY_REWRITE, tomoyo_write_no_rewrite },
  1175. { TOMOYO_KEYWORD_ALLOW_READ, tomoyo_write_globally_readable },
  1176. };
  1177. for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++)
  1178. if (tomoyo_str_starts(&data, tomoyo_transition_type[i]))
  1179. return tomoyo_write_transition_control(data, is_delete,
  1180. i);
  1181. for (i = 0; i < 4; i++)
  1182. if (tomoyo_str_starts(&data, tomoyo_callback[i].keyword))
  1183. return tomoyo_callback[i].write(data, is_delete);
  1184. for (i = 0; i < TOMOYO_MAX_GROUP; i++)
  1185. if (tomoyo_str_starts(&data, tomoyo_group_name[i]))
  1186. return tomoyo_write_group(data, is_delete, i);
  1187. return -EINVAL;
  1188. }
  1189. /**
  1190. * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  1191. *
  1192. * @head: Pointer to "struct tomoyo_io_buffer".
  1193. * @idx: Index number.
  1194. *
  1195. * Returns true on success, false otherwise.
  1196. *
  1197. * Caller holds tomoyo_read_lock().
  1198. */
  1199. static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
  1200. {
  1201. list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
  1202. struct tomoyo_group *group =
  1203. list_entry(head->r.group, typeof(*group), list);
  1204. list_for_each_cookie(head->r.acl, &group->member_list) {
  1205. struct tomoyo_acl_head *ptr =
  1206. list_entry(head->r.acl, typeof(*ptr), list);
  1207. if (ptr->is_deleted)
  1208. continue;
  1209. if (!tomoyo_flush(head))
  1210. return false;
  1211. tomoyo_set_string(head, tomoyo_group_name[idx]);
  1212. tomoyo_set_string(head, group->group_name->name);
  1213. if (idx == TOMOYO_PATH_GROUP) {
  1214. tomoyo_set_space(head);
  1215. tomoyo_set_string(head, container_of
  1216. (ptr, struct tomoyo_path_group,
  1217. head)->member_name->name);
  1218. } else if (idx == TOMOYO_NUMBER_GROUP) {
  1219. tomoyo_print_number_union(head, &container_of
  1220. (ptr,
  1221. struct tomoyo_number_group,
  1222. head)->number);
  1223. }
  1224. tomoyo_set_lf(head);
  1225. }
  1226. head->r.acl = NULL;
  1227. }
  1228. head->r.group = NULL;
  1229. return true;
  1230. }
  1231. /**
  1232. * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
  1233. *
  1234. * @head: Pointer to "struct tomoyo_io_buffer".
  1235. * @idx: Index number.
  1236. *
  1237. * Returns true on success, false otherwise.
  1238. *
  1239. * Caller holds tomoyo_read_lock().
  1240. */
  1241. static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
  1242. {
  1243. list_for_each_cookie(head->r.acl, &tomoyo_policy_list[idx]) {
  1244. struct tomoyo_acl_head *acl =
  1245. container_of(head->r.acl, typeof(*acl), list);
  1246. if (acl->is_deleted)
  1247. continue;
  1248. if (!tomoyo_flush(head))
  1249. return false;
  1250. switch (idx) {
  1251. case TOMOYO_ID_TRANSITION_CONTROL:
  1252. {
  1253. struct tomoyo_transition_control *ptr =
  1254. container_of(acl, typeof(*ptr), head);
  1255. tomoyo_set_string(head,
  1256. tomoyo_transition_type
  1257. [ptr->type]);
  1258. if (ptr->program)
  1259. tomoyo_set_string(head,
  1260. ptr->program->name);
  1261. if (ptr->program && ptr->domainname)
  1262. tomoyo_set_string(head, " from ");
  1263. if (ptr->domainname)
  1264. tomoyo_set_string(head,
  1265. ptr->domainname->
  1266. name);
  1267. }
  1268. break;
  1269. case TOMOYO_ID_GLOBALLY_READABLE:
  1270. {
  1271. struct tomoyo_readable_file *ptr =
  1272. container_of(acl, typeof(*ptr), head);
  1273. tomoyo_set_string(head,
  1274. TOMOYO_KEYWORD_ALLOW_READ);
  1275. tomoyo_set_string(head, ptr->filename->name);
  1276. }
  1277. break;
  1278. case TOMOYO_ID_AGGREGATOR:
  1279. {
  1280. struct tomoyo_aggregator *ptr =
  1281. container_of(acl, typeof(*ptr), head);
  1282. tomoyo_set_string(head,
  1283. TOMOYO_KEYWORD_AGGREGATOR);
  1284. tomoyo_set_string(head,
  1285. ptr->original_name->name);
  1286. tomoyo_set_space(head);
  1287. tomoyo_set_string(head,
  1288. ptr->aggregated_name->name);
  1289. }
  1290. break;
  1291. case TOMOYO_ID_PATTERN:
  1292. {
  1293. struct tomoyo_no_pattern *ptr =
  1294. container_of(acl, typeof(*ptr), head);
  1295. tomoyo_set_string(head,
  1296. TOMOYO_KEYWORD_FILE_PATTERN);
  1297. tomoyo_set_string(head, ptr->pattern->name);
  1298. }
  1299. break;
  1300. case TOMOYO_ID_NO_REWRITE:
  1301. {
  1302. struct tomoyo_no_rewrite *ptr =
  1303. container_of(acl, typeof(*ptr), head);
  1304. tomoyo_set_string(head,
  1305. TOMOYO_KEYWORD_DENY_REWRITE);
  1306. tomoyo_set_string(head, ptr->pattern->name);
  1307. }
  1308. break;
  1309. default:
  1310. continue;
  1311. }
  1312. tomoyo_set_lf(head);
  1313. }
  1314. head->r.acl = NULL;
  1315. return true;
  1316. }
  1317. /**
  1318. * tomoyo_read_exception - Read exception policy.
  1319. *
  1320. * @head: Pointer to "struct tomoyo_io_buffer".
  1321. *
  1322. * Caller holds tomoyo_read_lock().
  1323. */
  1324. static void tomoyo_read_exception(struct tomoyo_io_buffer *head)
  1325. {
  1326. if (head->r.eof)
  1327. return;
  1328. while (head->r.step < TOMOYO_MAX_POLICY &&
  1329. tomoyo_read_policy(head, head->r.step))
  1330. head->r.step++;
  1331. if (head->r.step < TOMOYO_MAX_POLICY)
  1332. return;
  1333. while (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
  1334. tomoyo_read_group(head, head->r.step - TOMOYO_MAX_POLICY))
  1335. head->r.step++;
  1336. if (head->r.step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
  1337. return;
  1338. head->r.eof = true;
  1339. }
  1340. /**
  1341. * tomoyo_print_header - Get header line of audit log.
  1342. *
  1343. * @r: Pointer to "struct tomoyo_request_info".
  1344. *
  1345. * Returns string representation.
  1346. *
  1347. * This function uses kmalloc(), so caller must kfree() if this function
  1348. * didn't return NULL.
  1349. */
  1350. static char *tomoyo_print_header(struct tomoyo_request_info *r)
  1351. {
  1352. struct timeval tv;
  1353. const pid_t gpid = task_pid_nr(current);
  1354. static const int tomoyo_buffer_len = 4096;
  1355. char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
  1356. if (!buffer)
  1357. return NULL;
  1358. do_gettimeofday(&tv);
  1359. snprintf(buffer, tomoyo_buffer_len - 1,
  1360. "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
  1361. " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
  1362. " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
  1363. tv.tv_sec, r->profile, tomoyo_mode[r->mode], gpid,
  1364. (pid_t) sys_getpid(), (pid_t) sys_getppid(),
  1365. current_uid(), current_gid(), current_euid(),
  1366. current_egid(), current_suid(), current_sgid(),
  1367. current_fsuid(), current_fsgid());
  1368. return buffer;
  1369. }
  1370. /**
  1371. * tomoyo_init_audit_log - Allocate buffer for audit logs.
  1372. *
  1373. * @len: Required size.
  1374. * @r: Pointer to "struct tomoyo_request_info".
  1375. *
  1376. * Returns pointer to allocated memory.
  1377. *
  1378. * The @len is updated to add the header lines' size on success.
  1379. *
  1380. * This function uses kzalloc(), so caller must kfree() if this function
  1381. * didn't return NULL.
  1382. */
  1383. static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
  1384. {
  1385. char *buf = NULL;
  1386. const char *header;
  1387. const char *domainname;
  1388. if (!r->domain)
  1389. r->domain = tomoyo_domain();
  1390. domainname = r->domain->domainname->name;
  1391. header = tomoyo_print_header(r);
  1392. if (!header)
  1393. return NULL;
  1394. *len += strlen(domainname) + strlen(header) + 10;
  1395. buf = kzalloc(*len, GFP_NOFS);
  1396. if (buf)
  1397. snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
  1398. kfree(header);
  1399. return buf;
  1400. }
  1401. /* Wait queue for tomoyo_query_list. */
  1402. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1403. /* Lock for manipulating tomoyo_query_list. */
  1404. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1405. /* Structure for query. */
  1406. struct tomoyo_query {
  1407. struct list_head list;
  1408. char *query;
  1409. int query_len;
  1410. unsigned int serial;
  1411. int timer;
  1412. int answer;
  1413. };
  1414. /* The list for "struct tomoyo_query". */
  1415. static LIST_HEAD(tomoyo_query_list);
  1416. /*
  1417. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1418. * interface.
  1419. */
  1420. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1421. /**
  1422. * tomoyo_supervisor - Ask for the supervisor's decision.
  1423. *
  1424. * @r: Pointer to "struct tomoyo_request_info".
  1425. * @fmt: The printf()'s format string, followed by parameters.
  1426. *
  1427. * Returns 0 if the supervisor decided to permit the access request which
  1428. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1429. * supervisor decided to retry the access request which violated the policy in
  1430. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1431. */
  1432. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1433. {
  1434. va_list args;
  1435. int error = -EPERM;
  1436. int pos;
  1437. int len;
  1438. static unsigned int tomoyo_serial;
  1439. struct tomoyo_query *entry = NULL;
  1440. bool quota_exceeded = false;
  1441. char *header;
  1442. switch (r->mode) {
  1443. char *buffer;
  1444. case TOMOYO_CONFIG_LEARNING:
  1445. if (!tomoyo_domain_quota_is_ok(r))
  1446. return 0;
  1447. va_start(args, fmt);
  1448. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
  1449. va_end(args);
  1450. buffer = kmalloc(len, GFP_NOFS);
  1451. if (!buffer)
  1452. return 0;
  1453. va_start(args, fmt);
  1454. vsnprintf(buffer, len - 1, fmt, args);
  1455. va_end(args);
  1456. tomoyo_normalize_line(buffer);
  1457. tomoyo_write_domain2(buffer, r->domain, false);
  1458. kfree(buffer);
  1459. /* fall through */
  1460. case TOMOYO_CONFIG_PERMISSIVE:
  1461. return 0;
  1462. }
  1463. if (!r->domain)
  1464. r->domain = tomoyo_domain();
  1465. if (!atomic_read(&tomoyo_query_observers))
  1466. return -EPERM;
  1467. va_start(args, fmt);
  1468. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
  1469. va_end(args);
  1470. header = tomoyo_init_audit_log(&len, r);
  1471. if (!header)
  1472. goto out;
  1473. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  1474. if (!entry)
  1475. goto out;
  1476. entry->query = kzalloc(len, GFP_NOFS);
  1477. if (!entry->query)
  1478. goto out;
  1479. len = ksize(entry->query);
  1480. spin_lock(&tomoyo_query_list_lock);
  1481. if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
  1482. sizeof(*entry) >= tomoyo_quota_for_query) {
  1483. quota_exceeded = true;
  1484. } else {
  1485. tomoyo_query_memory_size += len + sizeof(*entry);
  1486. entry->serial = tomoyo_serial++;
  1487. }
  1488. spin_unlock(&tomoyo_query_list_lock);
  1489. if (quota_exceeded)
  1490. goto out;
  1491. pos = snprintf(entry->query, len - 1, "Q%u-%hu\n%s",
  1492. entry->serial, r->retry, header);
  1493. kfree(header);
  1494. header = NULL;
  1495. va_start(args, fmt);
  1496. vsnprintf(entry->query + pos, len - 1 - pos, fmt, args);
  1497. entry->query_len = strlen(entry->query) + 1;
  1498. va_end(args);
  1499. spin_lock(&tomoyo_query_list_lock);
  1500. list_add_tail(&entry->list, &tomoyo_query_list);
  1501. spin_unlock(&tomoyo_query_list_lock);
  1502. /* Give 10 seconds for supervisor's opinion. */
  1503. for (entry->timer = 0;
  1504. atomic_read(&tomoyo_query_observers) && entry->timer < 100;
  1505. entry->timer++) {
  1506. wake_up(&tomoyo_query_wait);
  1507. set_current_state(TASK_INTERRUPTIBLE);
  1508. schedule_timeout(HZ / 10);
  1509. if (entry->answer)
  1510. break;
  1511. }
  1512. spin_lock(&tomoyo_query_list_lock);
  1513. list_del(&entry->list);
  1514. tomoyo_query_memory_size -= len + sizeof(*entry);
  1515. spin_unlock(&tomoyo_query_list_lock);
  1516. switch (entry->answer) {
  1517. case 3: /* Asked to retry by administrator. */
  1518. error = TOMOYO_RETRY_REQUEST;
  1519. r->retry++;
  1520. break;
  1521. case 1:
  1522. /* Granted by administrator. */
  1523. error = 0;
  1524. break;
  1525. case 0:
  1526. /* Timed out. */
  1527. break;
  1528. default:
  1529. /* Rejected by administrator. */
  1530. break;
  1531. }
  1532. out:
  1533. if (entry)
  1534. kfree(entry->query);
  1535. kfree(entry);
  1536. kfree(header);
  1537. return error;
  1538. }
  1539. /**
  1540. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  1541. *
  1542. * @file: Pointer to "struct file".
  1543. * @wait: Pointer to "poll_table".
  1544. *
  1545. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  1546. *
  1547. * Waits for access requests which violated policy in enforcing mode.
  1548. */
  1549. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  1550. {
  1551. struct list_head *tmp;
  1552. bool found = false;
  1553. u8 i;
  1554. for (i = 0; i < 2; i++) {
  1555. spin_lock(&tomoyo_query_list_lock);
  1556. list_for_each(tmp, &tomoyo_query_list) {
  1557. struct tomoyo_query *ptr =
  1558. list_entry(tmp, typeof(*ptr), list);
  1559. if (ptr->answer)
  1560. continue;
  1561. found = true;
  1562. break;
  1563. }
  1564. spin_unlock(&tomoyo_query_list_lock);
  1565. if (found)
  1566. return POLLIN | POLLRDNORM;
  1567. if (i)
  1568. break;
  1569. poll_wait(file, &tomoyo_query_wait, wait);
  1570. }
  1571. return 0;
  1572. }
  1573. /**
  1574. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  1575. *
  1576. * @head: Pointer to "struct tomoyo_io_buffer".
  1577. */
  1578. static void tomoyo_read_query(struct tomoyo_io_buffer *head)
  1579. {
  1580. struct list_head *tmp;
  1581. int pos = 0;
  1582. int len = 0;
  1583. char *buf;
  1584. if (head->r.w_pos)
  1585. return;
  1586. if (head->read_buf) {
  1587. kfree(head->read_buf);
  1588. head->read_buf = NULL;
  1589. }
  1590. spin_lock(&tomoyo_query_list_lock);
  1591. list_for_each(tmp, &tomoyo_query_list) {
  1592. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1593. if (ptr->answer)
  1594. continue;
  1595. if (pos++ != head->r.query_index)
  1596. continue;
  1597. len = ptr->query_len;
  1598. break;
  1599. }
  1600. spin_unlock(&tomoyo_query_list_lock);
  1601. if (!len) {
  1602. head->r.query_index = 0;
  1603. return;
  1604. }
  1605. buf = kzalloc(len, GFP_NOFS);
  1606. if (!buf)
  1607. return;
  1608. pos = 0;
  1609. spin_lock(&tomoyo_query_list_lock);
  1610. list_for_each(tmp, &tomoyo_query_list) {
  1611. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1612. if (ptr->answer)
  1613. continue;
  1614. if (pos++ != head->r.query_index)
  1615. continue;
  1616. /*
  1617. * Some query can be skipped because tomoyo_query_list
  1618. * can change, but I don't care.
  1619. */
  1620. if (len == ptr->query_len)
  1621. memmove(buf, ptr->query, len);
  1622. break;
  1623. }
  1624. spin_unlock(&tomoyo_query_list_lock);
  1625. if (buf[0]) {
  1626. head->read_buf = buf;
  1627. head->r.w[head->r.w_pos++] = buf;
  1628. head->r.query_index++;
  1629. } else {
  1630. kfree(buf);
  1631. }
  1632. }
  1633. /**
  1634. * tomoyo_write_answer - Write the supervisor's decision.
  1635. *
  1636. * @head: Pointer to "struct tomoyo_io_buffer".
  1637. *
  1638. * Returns 0 on success, -EINVAL otherwise.
  1639. */
  1640. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  1641. {
  1642. char *data = head->write_buf;
  1643. struct list_head *tmp;
  1644. unsigned int serial;
  1645. unsigned int answer;
  1646. spin_lock(&tomoyo_query_list_lock);
  1647. list_for_each(tmp, &tomoyo_query_list) {
  1648. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1649. ptr->timer = 0;
  1650. }
  1651. spin_unlock(&tomoyo_query_list_lock);
  1652. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  1653. return -EINVAL;
  1654. spin_lock(&tomoyo_query_list_lock);
  1655. list_for_each(tmp, &tomoyo_query_list) {
  1656. struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
  1657. if (ptr->serial != serial)
  1658. continue;
  1659. if (!ptr->answer)
  1660. ptr->answer = answer;
  1661. break;
  1662. }
  1663. spin_unlock(&tomoyo_query_list_lock);
  1664. return 0;
  1665. }
  1666. /**
  1667. * tomoyo_read_version: Get version.
  1668. *
  1669. * @head: Pointer to "struct tomoyo_io_buffer".
  1670. *
  1671. * Returns version information.
  1672. */
  1673. static void tomoyo_read_version(struct tomoyo_io_buffer *head)
  1674. {
  1675. if (!head->r.eof) {
  1676. tomoyo_io_printf(head, "2.3.0");
  1677. head->r.eof = true;
  1678. }
  1679. }
  1680. /**
  1681. * tomoyo_read_self_domain - Get the current process's domainname.
  1682. *
  1683. * @head: Pointer to "struct tomoyo_io_buffer".
  1684. *
  1685. * Returns the current process's domainname.
  1686. */
  1687. static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1688. {
  1689. if (!head->r.eof) {
  1690. /*
  1691. * tomoyo_domain()->domainname != NULL
  1692. * because every process belongs to a domain and
  1693. * the domain's name cannot be NULL.
  1694. */
  1695. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1696. head->r.eof = true;
  1697. }
  1698. }
  1699. /**
  1700. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1701. *
  1702. * @type: Type of interface.
  1703. * @file: Pointer to "struct file".
  1704. *
  1705. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1706. *
  1707. * Caller acquires tomoyo_read_lock().
  1708. */
  1709. int tomoyo_open_control(const u8 type, struct file *file)
  1710. {
  1711. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  1712. if (!head)
  1713. return -ENOMEM;
  1714. mutex_init(&head->io_sem);
  1715. head->type = type;
  1716. switch (type) {
  1717. case TOMOYO_DOMAINPOLICY:
  1718. /* /sys/kernel/security/tomoyo/domain_policy */
  1719. head->write = tomoyo_write_domain;
  1720. head->read = tomoyo_read_domain;
  1721. break;
  1722. case TOMOYO_EXCEPTIONPOLICY:
  1723. /* /sys/kernel/security/tomoyo/exception_policy */
  1724. head->write = tomoyo_write_exception;
  1725. head->read = tomoyo_read_exception;
  1726. break;
  1727. case TOMOYO_SELFDOMAIN:
  1728. /* /sys/kernel/security/tomoyo/self_domain */
  1729. head->read = tomoyo_read_self_domain;
  1730. break;
  1731. case TOMOYO_DOMAIN_STATUS:
  1732. /* /sys/kernel/security/tomoyo/.domain_status */
  1733. head->write = tomoyo_write_domain_profile;
  1734. head->read = tomoyo_read_domain_profile;
  1735. break;
  1736. case TOMOYO_PROCESS_STATUS:
  1737. /* /sys/kernel/security/tomoyo/.process_status */
  1738. head->write = tomoyo_write_pid;
  1739. head->read = tomoyo_read_pid;
  1740. break;
  1741. case TOMOYO_VERSION:
  1742. /* /sys/kernel/security/tomoyo/version */
  1743. head->read = tomoyo_read_version;
  1744. head->readbuf_size = 128;
  1745. break;
  1746. case TOMOYO_MEMINFO:
  1747. /* /sys/kernel/security/tomoyo/meminfo */
  1748. head->write = tomoyo_write_memory_quota;
  1749. head->read = tomoyo_read_memory_counter;
  1750. head->readbuf_size = 512;
  1751. break;
  1752. case TOMOYO_PROFILE:
  1753. /* /sys/kernel/security/tomoyo/profile */
  1754. head->write = tomoyo_write_profile;
  1755. head->read = tomoyo_read_profile;
  1756. break;
  1757. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  1758. head->poll = tomoyo_poll_query;
  1759. head->write = tomoyo_write_answer;
  1760. head->read = tomoyo_read_query;
  1761. break;
  1762. case TOMOYO_MANAGER:
  1763. /* /sys/kernel/security/tomoyo/manager */
  1764. head->write = tomoyo_write_manager;
  1765. head->read = tomoyo_read_manager;
  1766. break;
  1767. }
  1768. if (!(file->f_mode & FMODE_READ)) {
  1769. /*
  1770. * No need to allocate read_buf since it is not opened
  1771. * for reading.
  1772. */
  1773. head->read = NULL;
  1774. head->poll = NULL;
  1775. } else if (!head->poll) {
  1776. /* Don't allocate read_buf for poll() access. */
  1777. if (!head->readbuf_size)
  1778. head->readbuf_size = 4096 * 2;
  1779. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  1780. if (!head->read_buf) {
  1781. kfree(head);
  1782. return -ENOMEM;
  1783. }
  1784. }
  1785. if (!(file->f_mode & FMODE_WRITE)) {
  1786. /*
  1787. * No need to allocate write_buf since it is not opened
  1788. * for writing.
  1789. */
  1790. head->write = NULL;
  1791. } else if (head->write) {
  1792. head->writebuf_size = 4096 * 2;
  1793. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  1794. if (!head->write_buf) {
  1795. kfree(head->read_buf);
  1796. kfree(head);
  1797. return -ENOMEM;
  1798. }
  1799. }
  1800. if (type != TOMOYO_QUERY)
  1801. head->reader_idx = tomoyo_read_lock();
  1802. file->private_data = head;
  1803. /*
  1804. * Call the handler now if the file is
  1805. * /sys/kernel/security/tomoyo/self_domain
  1806. * so that the user can use
  1807. * cat < /sys/kernel/security/tomoyo/self_domain"
  1808. * to know the current process's domainname.
  1809. */
  1810. if (type == TOMOYO_SELFDOMAIN)
  1811. tomoyo_read_control(file, NULL, 0);
  1812. /*
  1813. * If the file is /sys/kernel/security/tomoyo/query , increment the
  1814. * observer counter.
  1815. * The obserber counter is used by tomoyo_supervisor() to see if
  1816. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  1817. */
  1818. else if (type == TOMOYO_QUERY)
  1819. atomic_inc(&tomoyo_query_observers);
  1820. return 0;
  1821. }
  1822. /**
  1823. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  1824. *
  1825. * @file: Pointer to "struct file".
  1826. * @wait: Pointer to "poll_table".
  1827. *
  1828. * Waits for read readiness.
  1829. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
  1830. */
  1831. int tomoyo_poll_control(struct file *file, poll_table *wait)
  1832. {
  1833. struct tomoyo_io_buffer *head = file->private_data;
  1834. if (!head->poll)
  1835. return -ENOSYS;
  1836. return head->poll(file, wait);
  1837. }
  1838. /**
  1839. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1840. *
  1841. * @file: Pointer to "struct file".
  1842. * @buffer: Poiner to buffer to write to.
  1843. * @buffer_len: Size of @buffer.
  1844. *
  1845. * Returns bytes read on success, negative value otherwise.
  1846. *
  1847. * Caller holds tomoyo_read_lock().
  1848. */
  1849. int tomoyo_read_control(struct file *file, char __user *buffer,
  1850. const int buffer_len)
  1851. {
  1852. int len;
  1853. struct tomoyo_io_buffer *head = file->private_data;
  1854. if (!head->read)
  1855. return -ENOSYS;
  1856. if (mutex_lock_interruptible(&head->io_sem))
  1857. return -EINTR;
  1858. head->read_user_buf = buffer;
  1859. head->read_user_buf_avail = buffer_len;
  1860. if (tomoyo_flush(head))
  1861. /* Call the policy handler. */
  1862. head->read(head);
  1863. tomoyo_flush(head);
  1864. len = head->read_user_buf - buffer;
  1865. mutex_unlock(&head->io_sem);
  1866. return len;
  1867. }
  1868. /**
  1869. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1870. *
  1871. * @file: Pointer to "struct file".
  1872. * @buffer: Pointer to buffer to read from.
  1873. * @buffer_len: Size of @buffer.
  1874. *
  1875. * Returns @buffer_len on success, negative value otherwise.
  1876. *
  1877. * Caller holds tomoyo_read_lock().
  1878. */
  1879. int tomoyo_write_control(struct file *file, const char __user *buffer,
  1880. const int buffer_len)
  1881. {
  1882. struct tomoyo_io_buffer *head = file->private_data;
  1883. int error = buffer_len;
  1884. int avail_len = buffer_len;
  1885. char *cp0 = head->write_buf;
  1886. if (!head->write)
  1887. return -ENOSYS;
  1888. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1889. return -EFAULT;
  1890. /* Don't allow updating policies by non manager programs. */
  1891. if (head->write != tomoyo_write_pid &&
  1892. head->write != tomoyo_write_domain && !tomoyo_manager())
  1893. return -EPERM;
  1894. if (mutex_lock_interruptible(&head->io_sem))
  1895. return -EINTR;
  1896. /* Read a line and dispatch it to the policy handler. */
  1897. while (avail_len > 0) {
  1898. char c;
  1899. if (head->write_avail >= head->writebuf_size - 1) {
  1900. error = -ENOMEM;
  1901. break;
  1902. } else if (get_user(c, buffer)) {
  1903. error = -EFAULT;
  1904. break;
  1905. }
  1906. buffer++;
  1907. avail_len--;
  1908. cp0[head->write_avail++] = c;
  1909. if (c != '\n')
  1910. continue;
  1911. cp0[head->write_avail - 1] = '\0';
  1912. head->write_avail = 0;
  1913. tomoyo_normalize_line(cp0);
  1914. head->write(head);
  1915. }
  1916. mutex_unlock(&head->io_sem);
  1917. return error;
  1918. }
  1919. /**
  1920. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  1921. *
  1922. * @file: Pointer to "struct file".
  1923. *
  1924. * Releases memory and returns 0.
  1925. *
  1926. * Caller looses tomoyo_read_lock().
  1927. */
  1928. int tomoyo_close_control(struct file *file)
  1929. {
  1930. struct tomoyo_io_buffer *head = file->private_data;
  1931. const bool is_write = !!head->write_buf;
  1932. /*
  1933. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  1934. * observer counter.
  1935. */
  1936. if (head->type == TOMOYO_QUERY)
  1937. atomic_dec(&tomoyo_query_observers);
  1938. else
  1939. tomoyo_read_unlock(head->reader_idx);
  1940. /* Release memory used for policy I/O. */
  1941. kfree(head->read_buf);
  1942. head->read_buf = NULL;
  1943. kfree(head->write_buf);
  1944. head->write_buf = NULL;
  1945. kfree(head);
  1946. head = NULL;
  1947. file->private_data = NULL;
  1948. if (is_write)
  1949. tomoyo_run_gc();
  1950. return 0;
  1951. }
  1952. /**
  1953. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  1954. */
  1955. void tomoyo_check_profile(void)
  1956. {
  1957. struct tomoyo_domain_info *domain;
  1958. const int idx = tomoyo_read_lock();
  1959. tomoyo_policy_loaded = true;
  1960. /* Check all profiles currently assigned to domains are defined. */
  1961. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1962. const u8 profile = domain->profile;
  1963. if (tomoyo_profile_ptr[profile])
  1964. continue;
  1965. panic("Profile %u (used by '%s') not defined.\n",
  1966. profile, domain->domainname->name);
  1967. }
  1968. tomoyo_read_unlock(idx);
  1969. if (tomoyo_profile_version != 20090903)
  1970. panic("Profile version %u is not supported.\n",
  1971. tomoyo_profile_version);
  1972. printk(KERN_INFO "TOMOYO: 2.3.0\n");
  1973. printk(KERN_INFO "Mandatory Access Control activated.\n");
  1974. }