common.c 52 KB

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