common.c 52 KB

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