common.c 53 KB

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