common.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  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. static bool tomoyo_same_manager_entry(const struct tomoyo_acl_head *a,
  420. const struct tomoyo_acl_head *b)
  421. {
  422. return container_of(a, struct tomoyo_policy_manager_entry, head)
  423. ->manager ==
  424. container_of(b, struct tomoyo_policy_manager_entry, head)
  425. ->manager;
  426. }
  427. /**
  428. * tomoyo_update_manager_entry - Add a manager entry.
  429. *
  430. * @manager: The path to manager or the domainnamme.
  431. * @is_delete: True if it is a delete request.
  432. *
  433. * Returns 0 on success, negative value otherwise.
  434. *
  435. * Caller holds tomoyo_read_lock().
  436. */
  437. static int tomoyo_update_manager_entry(const char *manager,
  438. const bool is_delete)
  439. {
  440. struct tomoyo_policy_manager_entry e = { };
  441. int error;
  442. if (tomoyo_domain_def(manager)) {
  443. if (!tomoyo_correct_domain(manager))
  444. return -EINVAL;
  445. e.is_domain = true;
  446. } else {
  447. if (!tomoyo_correct_path(manager))
  448. return -EINVAL;
  449. }
  450. e.manager = tomoyo_get_name(manager);
  451. if (!e.manager)
  452. return -ENOMEM;
  453. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  454. &tomoyo_policy_list[TOMOYO_ID_MANAGER],
  455. tomoyo_same_manager_entry);
  456. tomoyo_put_name(e.manager);
  457. return error;
  458. }
  459. /**
  460. * tomoyo_write_manager_policy - Write manager policy.
  461. *
  462. * @head: Pointer to "struct tomoyo_io_buffer".
  463. *
  464. * Returns 0 on success, negative value otherwise.
  465. *
  466. * Caller holds tomoyo_read_lock().
  467. */
  468. static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
  469. {
  470. char *data = head->write_buf;
  471. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  472. if (!strcmp(data, "manage_by_non_root")) {
  473. tomoyo_manage_by_non_root = !is_delete;
  474. return 0;
  475. }
  476. return tomoyo_update_manager_entry(data, is_delete);
  477. }
  478. /**
  479. * tomoyo_read_manager_policy - Read manager policy.
  480. *
  481. * @head: Pointer to "struct tomoyo_io_buffer".
  482. *
  483. * Caller holds tomoyo_read_lock().
  484. */
  485. static void tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
  486. {
  487. struct list_head *pos;
  488. bool done = true;
  489. if (head->read_eof)
  490. return;
  491. list_for_each_cookie(pos, head->read_var2,
  492. &tomoyo_policy_list[TOMOYO_ID_MANAGER]) {
  493. struct tomoyo_policy_manager_entry *ptr;
  494. ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
  495. head.list);
  496. if (ptr->head.is_deleted)
  497. continue;
  498. done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
  499. if (!done)
  500. break;
  501. }
  502. head->read_eof = done;
  503. }
  504. /**
  505. * tomoyo_policy_manager - Check whether the current process is a policy manager.
  506. *
  507. * Returns true if the current process is permitted to modify policy
  508. * via /sys/kernel/security/tomoyo/ interface.
  509. *
  510. * Caller holds tomoyo_read_lock().
  511. */
  512. static bool tomoyo_policy_manager(void)
  513. {
  514. struct tomoyo_policy_manager_entry *ptr;
  515. const char *exe;
  516. const struct task_struct *task = current;
  517. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  518. bool found = false;
  519. if (!tomoyo_policy_loaded)
  520. return true;
  521. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  522. return false;
  523. list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
  524. head.list) {
  525. if (!ptr->head.is_deleted && ptr->is_domain
  526. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  527. found = true;
  528. break;
  529. }
  530. }
  531. if (found)
  532. return true;
  533. exe = tomoyo_get_exe();
  534. if (!exe)
  535. return false;
  536. list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_MANAGER],
  537. head.list) {
  538. if (!ptr->head.is_deleted && !ptr->is_domain
  539. && !strcmp(exe, ptr->manager->name)) {
  540. found = true;
  541. break;
  542. }
  543. }
  544. if (!found) { /* Reduce error messages. */
  545. static pid_t last_pid;
  546. const pid_t pid = current->pid;
  547. if (last_pid != pid) {
  548. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  549. "update policies.\n", domainname->name, exe);
  550. last_pid = pid;
  551. }
  552. }
  553. kfree(exe);
  554. return found;
  555. }
  556. /**
  557. * tomoyo_select_one - Parse select command.
  558. *
  559. * @head: Pointer to "struct tomoyo_io_buffer".
  560. * @data: String to parse.
  561. *
  562. * Returns true on success, false otherwise.
  563. *
  564. * Caller holds tomoyo_read_lock().
  565. */
  566. static bool tomoyo_select_one(struct tomoyo_io_buffer *head,
  567. const char *data)
  568. {
  569. unsigned int pid;
  570. struct tomoyo_domain_info *domain = NULL;
  571. bool global_pid = false;
  572. if (sscanf(data, "pid=%u", &pid) == 1 ||
  573. (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
  574. struct task_struct *p;
  575. rcu_read_lock();
  576. read_lock(&tasklist_lock);
  577. if (global_pid)
  578. p = find_task_by_pid_ns(pid, &init_pid_ns);
  579. else
  580. p = find_task_by_vpid(pid);
  581. if (p)
  582. domain = tomoyo_real_domain(p);
  583. read_unlock(&tasklist_lock);
  584. rcu_read_unlock();
  585. } else if (!strncmp(data, "domain=", 7)) {
  586. if (tomoyo_domain_def(data + 7))
  587. domain = tomoyo_find_domain(data + 7);
  588. } else
  589. return false;
  590. head->write_var1 = domain;
  591. /* Accessing read_buf is safe because head->io_sem is held. */
  592. if (!head->read_buf)
  593. return true; /* Do nothing if open(O_WRONLY). */
  594. head->read_avail = 0;
  595. tomoyo_io_printf(head, "# select %s\n", data);
  596. head->read_single_domain = true;
  597. head->read_eof = !domain;
  598. if (domain) {
  599. struct tomoyo_domain_info *d;
  600. head->read_var1 = NULL;
  601. list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
  602. if (d == domain)
  603. break;
  604. head->read_var1 = &d->list;
  605. }
  606. head->read_var2 = NULL;
  607. head->read_bit = 0;
  608. head->read_step = 0;
  609. if (domain->is_deleted)
  610. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  611. }
  612. return true;
  613. }
  614. /**
  615. * tomoyo_delete_domain - Delete a domain.
  616. *
  617. * @domainname: The name of domain.
  618. *
  619. * Returns 0.
  620. *
  621. * Caller holds tomoyo_read_lock().
  622. */
  623. static int tomoyo_delete_domain(char *domainname)
  624. {
  625. struct tomoyo_domain_info *domain;
  626. struct tomoyo_path_info name;
  627. name.name = domainname;
  628. tomoyo_fill_path_info(&name);
  629. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  630. return 0;
  631. /* Is there an active domain? */
  632. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  633. /* Never delete tomoyo_kernel_domain */
  634. if (domain == &tomoyo_kernel_domain)
  635. continue;
  636. if (domain->is_deleted ||
  637. tomoyo_pathcmp(domain->domainname, &name))
  638. continue;
  639. domain->is_deleted = true;
  640. break;
  641. }
  642. mutex_unlock(&tomoyo_policy_lock);
  643. return 0;
  644. }
  645. /**
  646. * tomoyo_write_domain_policy2 - Write domain policy.
  647. *
  648. * @head: Pointer to "struct tomoyo_io_buffer".
  649. *
  650. * Returns 0 on success, negative value otherwise.
  651. *
  652. * Caller holds tomoyo_read_lock().
  653. */
  654. static int tomoyo_write_domain_policy2(char *data,
  655. struct tomoyo_domain_info *domain,
  656. const bool is_delete)
  657. {
  658. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_MOUNT))
  659. return tomoyo_write_mount_policy(data, domain, is_delete);
  660. return tomoyo_write_file_policy(data, domain, is_delete);
  661. }
  662. /**
  663. * tomoyo_write_domain_policy - Write domain policy.
  664. *
  665. * @head: Pointer to "struct tomoyo_io_buffer".
  666. *
  667. * Returns 0 on success, negative value otherwise.
  668. *
  669. * Caller holds tomoyo_read_lock().
  670. */
  671. static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
  672. {
  673. char *data = head->write_buf;
  674. struct tomoyo_domain_info *domain = head->write_var1;
  675. bool is_delete = false;
  676. bool is_select = false;
  677. unsigned int profile;
  678. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  679. is_delete = true;
  680. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  681. is_select = true;
  682. if (is_select && tomoyo_select_one(head, data))
  683. return 0;
  684. /* Don't allow updating policies by non manager programs. */
  685. if (!tomoyo_policy_manager())
  686. return -EPERM;
  687. if (tomoyo_domain_def(data)) {
  688. domain = NULL;
  689. if (is_delete)
  690. tomoyo_delete_domain(data);
  691. else if (is_select)
  692. domain = tomoyo_find_domain(data);
  693. else
  694. domain = tomoyo_find_or_assign_new_domain(data, 0);
  695. head->write_var1 = domain;
  696. return 0;
  697. }
  698. if (!domain)
  699. return -EINVAL;
  700. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  701. && profile < TOMOYO_MAX_PROFILES) {
  702. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  703. domain->profile = (u8) profile;
  704. return 0;
  705. }
  706. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  707. domain->ignore_global_allow_read = !is_delete;
  708. return 0;
  709. }
  710. if (!strcmp(data, TOMOYO_KEYWORD_QUOTA_EXCEEDED)) {
  711. domain->quota_warned = !is_delete;
  712. return 0;
  713. }
  714. if (!strcmp(data, TOMOYO_KEYWORD_TRANSITION_FAILED)) {
  715. domain->transition_failed = !is_delete;
  716. return 0;
  717. }
  718. return tomoyo_write_domain_policy2(data, domain, is_delete);
  719. }
  720. /**
  721. * tomoyo_print_path_acl - Print a single path ACL entry.
  722. *
  723. * @head: Pointer to "struct tomoyo_io_buffer".
  724. * @ptr: Pointer to "struct tomoyo_path_acl".
  725. *
  726. * Returns true on success, false otherwise.
  727. */
  728. static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
  729. struct tomoyo_path_acl *ptr)
  730. {
  731. int pos;
  732. u8 bit;
  733. const u16 perm = ptr->perm;
  734. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  735. if (!(perm & (1 << bit)))
  736. continue;
  737. /* Print "read/write" instead of "read" and "write". */
  738. if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
  739. && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  740. continue;
  741. pos = head->read_avail;
  742. if (!tomoyo_io_printf(head, "allow_%s ",
  743. tomoyo_path_keyword[bit]) ||
  744. !tomoyo_print_name_union(head, &ptr->name) ||
  745. !tomoyo_io_printf(head, "\n"))
  746. goto out;
  747. }
  748. head->read_bit = 0;
  749. return true;
  750. out:
  751. head->read_bit = bit;
  752. head->read_avail = pos;
  753. return false;
  754. }
  755. /**
  756. * tomoyo_print_path2_acl - Print a double path ACL entry.
  757. *
  758. * @head: Pointer to "struct tomoyo_io_buffer".
  759. * @ptr: Pointer to "struct tomoyo_path2_acl".
  760. *
  761. * Returns true on success, false otherwise.
  762. */
  763. static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
  764. struct tomoyo_path2_acl *ptr)
  765. {
  766. int pos;
  767. const u8 perm = ptr->perm;
  768. u8 bit;
  769. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
  770. if (!(perm & (1 << bit)))
  771. continue;
  772. pos = head->read_avail;
  773. if (!tomoyo_io_printf(head, "allow_%s ",
  774. tomoyo_path2_keyword[bit]) ||
  775. !tomoyo_print_name_union(head, &ptr->name1) ||
  776. !tomoyo_print_name_union(head, &ptr->name2) ||
  777. !tomoyo_io_printf(head, "\n"))
  778. goto out;
  779. }
  780. head->read_bit = 0;
  781. return true;
  782. out:
  783. head->read_bit = bit;
  784. head->read_avail = pos;
  785. return false;
  786. }
  787. /**
  788. * tomoyo_print_path_number_acl - Print a path_number ACL entry.
  789. *
  790. * @head: Pointer to "struct tomoyo_io_buffer".
  791. * @ptr: Pointer to "struct tomoyo_path_number_acl".
  792. *
  793. * Returns true on success, false otherwise.
  794. */
  795. static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
  796. struct tomoyo_path_number_acl *ptr)
  797. {
  798. int pos;
  799. u8 bit;
  800. const u8 perm = ptr->perm;
  801. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
  802. bit++) {
  803. if (!(perm & (1 << bit)))
  804. continue;
  805. pos = head->read_avail;
  806. if (!tomoyo_io_printf(head, "allow_%s",
  807. tomoyo_path_number_keyword[bit]) ||
  808. !tomoyo_print_name_union(head, &ptr->name) ||
  809. !tomoyo_print_number_union(head, &ptr->number) ||
  810. !tomoyo_io_printf(head, "\n"))
  811. goto out;
  812. }
  813. head->read_bit = 0;
  814. return true;
  815. out:
  816. head->read_bit = bit;
  817. head->read_avail = pos;
  818. return false;
  819. }
  820. /**
  821. * tomoyo_print_mkdev_acl - Print a mkdev ACL entry.
  822. *
  823. * @head: Pointer to "struct tomoyo_io_buffer".
  824. * @ptr: Pointer to "struct tomoyo_mkdev_acl".
  825. *
  826. * Returns true on success, false otherwise.
  827. */
  828. static bool tomoyo_print_mkdev_acl(struct tomoyo_io_buffer *head,
  829. struct tomoyo_mkdev_acl *ptr)
  830. {
  831. int pos;
  832. u8 bit;
  833. const u16 perm = ptr->perm;
  834. for (bit = head->read_bit; bit < TOMOYO_MAX_MKDEV_OPERATION;
  835. bit++) {
  836. if (!(perm & (1 << bit)))
  837. continue;
  838. pos = head->read_avail;
  839. if (!tomoyo_io_printf(head, "allow_%s",
  840. tomoyo_mkdev_keyword[bit]) ||
  841. !tomoyo_print_name_union(head, &ptr->name) ||
  842. !tomoyo_print_number_union(head, &ptr->mode) ||
  843. !tomoyo_print_number_union(head, &ptr->major) ||
  844. !tomoyo_print_number_union(head, &ptr->minor) ||
  845. !tomoyo_io_printf(head, "\n"))
  846. goto out;
  847. }
  848. head->read_bit = 0;
  849. return true;
  850. out:
  851. head->read_bit = bit;
  852. head->read_avail = pos;
  853. return false;
  854. }
  855. /**
  856. * tomoyo_print_mount_acl - Print a mount ACL entry.
  857. *
  858. * @head: Pointer to "struct tomoyo_io_buffer".
  859. * @ptr: Pointer to "struct tomoyo_mount_acl".
  860. *
  861. * Returns true on success, false otherwise.
  862. */
  863. static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
  864. struct tomoyo_mount_acl *ptr)
  865. {
  866. const int pos = head->read_avail;
  867. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
  868. !tomoyo_print_name_union(head, &ptr->dev_name) ||
  869. !tomoyo_print_name_union(head, &ptr->dir_name) ||
  870. !tomoyo_print_name_union(head, &ptr->fs_type) ||
  871. !tomoyo_print_number_union(head, &ptr->flags) ||
  872. !tomoyo_io_printf(head, "\n")) {
  873. head->read_avail = pos;
  874. return false;
  875. }
  876. return true;
  877. }
  878. /**
  879. * tomoyo_print_entry - Print an ACL entry.
  880. *
  881. * @head: Pointer to "struct tomoyo_io_buffer".
  882. * @ptr: Pointer to an ACL entry.
  883. *
  884. * Returns true on success, false otherwise.
  885. */
  886. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  887. struct tomoyo_acl_info *ptr)
  888. {
  889. const u8 acl_type = ptr->type;
  890. if (ptr->is_deleted)
  891. return true;
  892. if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  893. struct tomoyo_path_acl *acl
  894. = container_of(ptr, struct tomoyo_path_acl, head);
  895. return tomoyo_print_path_acl(head, acl);
  896. }
  897. if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
  898. struct tomoyo_path2_acl *acl
  899. = container_of(ptr, struct tomoyo_path2_acl, head);
  900. return tomoyo_print_path2_acl(head, acl);
  901. }
  902. if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
  903. struct tomoyo_path_number_acl *acl
  904. = container_of(ptr, struct tomoyo_path_number_acl,
  905. head);
  906. return tomoyo_print_path_number_acl(head, acl);
  907. }
  908. if (acl_type == TOMOYO_TYPE_MKDEV_ACL) {
  909. struct tomoyo_mkdev_acl *acl
  910. = container_of(ptr, struct tomoyo_mkdev_acl,
  911. head);
  912. return tomoyo_print_mkdev_acl(head, acl);
  913. }
  914. if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
  915. struct tomoyo_mount_acl *acl
  916. = container_of(ptr, struct tomoyo_mount_acl, head);
  917. return tomoyo_print_mount_acl(head, acl);
  918. }
  919. BUG(); /* This must not happen. */
  920. return false;
  921. }
  922. /**
  923. * tomoyo_read_domain_policy - Read domain policy.
  924. *
  925. * @head: Pointer to "struct tomoyo_io_buffer".
  926. *
  927. * Caller holds tomoyo_read_lock().
  928. */
  929. static void tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
  930. {
  931. struct list_head *dpos;
  932. struct list_head *apos;
  933. bool done = true;
  934. if (head->read_eof)
  935. return;
  936. if (head->read_step == 0)
  937. head->read_step = 1;
  938. list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
  939. struct tomoyo_domain_info *domain;
  940. const char *quota_exceeded = "";
  941. const char *transition_failed = "";
  942. const char *ignore_global_allow_read = "";
  943. domain = list_entry(dpos, struct tomoyo_domain_info, list);
  944. if (head->read_step != 1)
  945. goto acl_loop;
  946. if (domain->is_deleted && !head->read_single_domain)
  947. continue;
  948. /* Print domainname and flags. */
  949. if (domain->quota_warned)
  950. quota_exceeded = "quota_exceeded\n";
  951. if (domain->transition_failed)
  952. transition_failed = "transition_failed\n";
  953. if (domain->ignore_global_allow_read)
  954. ignore_global_allow_read
  955. = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
  956. done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
  957. "%u\n%s%s%s\n",
  958. domain->domainname->name,
  959. domain->profile, quota_exceeded,
  960. transition_failed,
  961. ignore_global_allow_read);
  962. if (!done)
  963. break;
  964. head->read_step = 2;
  965. acl_loop:
  966. if (head->read_step == 3)
  967. goto tail_mark;
  968. /* Print ACL entries in the domain. */
  969. list_for_each_cookie(apos, head->read_var2,
  970. &domain->acl_info_list) {
  971. struct tomoyo_acl_info *ptr
  972. = list_entry(apos, struct tomoyo_acl_info,
  973. list);
  974. done = tomoyo_print_entry(head, ptr);
  975. if (!done)
  976. break;
  977. }
  978. if (!done)
  979. break;
  980. head->read_step = 3;
  981. tail_mark:
  982. done = tomoyo_io_printf(head, "\n");
  983. if (!done)
  984. break;
  985. head->read_step = 1;
  986. if (head->read_single_domain)
  987. break;
  988. }
  989. head->read_eof = done;
  990. }
  991. /**
  992. * tomoyo_write_domain_profile - Assign profile for specified domain.
  993. *
  994. * @head: Pointer to "struct tomoyo_io_buffer".
  995. *
  996. * Returns 0 on success, -EINVAL otherwise.
  997. *
  998. * This is equivalent to doing
  999. *
  1000. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  1001. * /usr/sbin/tomoyo-loadpolicy -d
  1002. *
  1003. * Caller holds tomoyo_read_lock().
  1004. */
  1005. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  1006. {
  1007. char *data = head->write_buf;
  1008. char *cp = strchr(data, ' ');
  1009. struct tomoyo_domain_info *domain;
  1010. unsigned long profile;
  1011. if (!cp)
  1012. return -EINVAL;
  1013. *cp = '\0';
  1014. domain = tomoyo_find_domain(cp + 1);
  1015. if (strict_strtoul(data, 10, &profile))
  1016. return -EINVAL;
  1017. if (domain && profile < TOMOYO_MAX_PROFILES
  1018. && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
  1019. domain->profile = (u8) profile;
  1020. return 0;
  1021. }
  1022. /**
  1023. * tomoyo_read_domain_profile - Read only domainname and profile.
  1024. *
  1025. * @head: Pointer to "struct tomoyo_io_buffer".
  1026. *
  1027. * Returns list of profile number and domainname pairs.
  1028. *
  1029. * This is equivalent to doing
  1030. *
  1031. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  1032. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  1033. * domainname = $0; } else if ( $1 == "use_profile" ) {
  1034. * print $2 " " domainname; domainname = ""; } } ; '
  1035. *
  1036. * Caller holds tomoyo_read_lock().
  1037. */
  1038. static void tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1039. {
  1040. struct list_head *pos;
  1041. bool done = true;
  1042. if (head->read_eof)
  1043. return;
  1044. list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
  1045. struct tomoyo_domain_info *domain;
  1046. domain = list_entry(pos, struct tomoyo_domain_info, list);
  1047. if (domain->is_deleted)
  1048. continue;
  1049. done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
  1050. domain->domainname->name);
  1051. if (!done)
  1052. break;
  1053. }
  1054. head->read_eof = done;
  1055. }
  1056. /**
  1057. * tomoyo_write_pid: Specify PID to obtain domainname.
  1058. *
  1059. * @head: Pointer to "struct tomoyo_io_buffer".
  1060. *
  1061. * Returns 0.
  1062. */
  1063. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1064. {
  1065. unsigned long pid;
  1066. /* No error check. */
  1067. strict_strtoul(head->write_buf, 10, &pid);
  1068. head->read_step = (int) pid;
  1069. head->read_eof = false;
  1070. return 0;
  1071. }
  1072. /**
  1073. * tomoyo_read_pid - Get domainname of the specified PID.
  1074. *
  1075. * @head: Pointer to "struct tomoyo_io_buffer".
  1076. *
  1077. * Returns the domainname which the specified PID is in on success,
  1078. * empty string otherwise.
  1079. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1080. * using read()/write() interface rather than sysctl() interface.
  1081. */
  1082. static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1083. {
  1084. if (head->read_avail == 0 && !head->read_eof) {
  1085. const int pid = head->read_step;
  1086. struct task_struct *p;
  1087. struct tomoyo_domain_info *domain = NULL;
  1088. rcu_read_lock();
  1089. read_lock(&tasklist_lock);
  1090. p = find_task_by_vpid(pid);
  1091. if (p)
  1092. domain = tomoyo_real_domain(p);
  1093. read_unlock(&tasklist_lock);
  1094. rcu_read_unlock();
  1095. if (domain)
  1096. tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
  1097. domain->domainname->name);
  1098. head->read_eof = true;
  1099. }
  1100. }
  1101. /**
  1102. * tomoyo_write_exception_policy - Write exception policy.
  1103. *
  1104. * @head: Pointer to "struct tomoyo_io_buffer".
  1105. *
  1106. * Returns 0 on success, negative value otherwise.
  1107. *
  1108. * Caller holds tomoyo_read_lock().
  1109. */
  1110. static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
  1111. {
  1112. char *data = head->write_buf;
  1113. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1114. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
  1115. return tomoyo_write_domain_keeper_policy(data, false,
  1116. is_delete);
  1117. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
  1118. return tomoyo_write_domain_keeper_policy(data, true, is_delete);
  1119. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
  1120. return tomoyo_write_domain_initializer_policy(data, false,
  1121. is_delete);
  1122. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
  1123. return tomoyo_write_domain_initializer_policy(data, true,
  1124. is_delete);
  1125. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_AGGREGATOR))
  1126. return tomoyo_write_aggregator_policy(data, is_delete);
  1127. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
  1128. return tomoyo_write_alias_policy(data, is_delete);
  1129. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
  1130. return tomoyo_write_globally_readable_policy(data, is_delete);
  1131. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
  1132. return tomoyo_write_pattern_policy(data, is_delete);
  1133. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
  1134. return tomoyo_write_no_rewrite_policy(data, is_delete);
  1135. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
  1136. return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
  1137. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
  1138. return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
  1139. return -EINVAL;
  1140. }
  1141. static void tomoyo_print_number(char *buffer, int buffer_len,
  1142. const struct tomoyo_number_union *ptr)
  1143. {
  1144. int i;
  1145. unsigned long min = ptr->values[0];
  1146. const unsigned long max = ptr->values[1];
  1147. u8 min_type = ptr->min_type;
  1148. const u8 max_type = ptr->max_type;
  1149. memset(buffer, 0, buffer_len);
  1150. buffer_len -= 2;
  1151. for (i = 0; i < 2; i++) {
  1152. int len;
  1153. switch (min_type) {
  1154. case TOMOYO_VALUE_TYPE_HEXADECIMAL:
  1155. snprintf(buffer, buffer_len, "0x%lX", min);
  1156. break;
  1157. case TOMOYO_VALUE_TYPE_OCTAL:
  1158. snprintf(buffer, buffer_len, "0%lo", min);
  1159. break;
  1160. default:
  1161. snprintf(buffer, buffer_len, "%lu", min);
  1162. break;
  1163. }
  1164. if (min == max && min_type == max_type)
  1165. break;
  1166. len = strlen(buffer);
  1167. buffer[len++] = '-';
  1168. buffer += len;
  1169. buffer_len -= len;
  1170. min_type = max_type;
  1171. min = max;
  1172. }
  1173. }
  1174. static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
  1175. [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
  1176. [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
  1177. };
  1178. /**
  1179. * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  1180. *
  1181. * @head: Pointer to "struct tomoyo_io_buffer".
  1182. * @idx: Index number.
  1183. *
  1184. * Returns true on success, false otherwise.
  1185. *
  1186. * Caller holds tomoyo_read_lock().
  1187. */
  1188. static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
  1189. {
  1190. struct list_head *gpos;
  1191. struct list_head *mpos;
  1192. const char *w[3] = { "", "", "" };
  1193. w[0] = tomoyo_group_name[idx];
  1194. list_for_each_cookie(gpos, head->read_var1, &tomoyo_group_list[idx]) {
  1195. struct tomoyo_group *group =
  1196. list_entry(gpos, struct tomoyo_group, list);
  1197. w[1] = group->group_name->name;
  1198. list_for_each_cookie(mpos, head->read_var2,
  1199. &group->member_list) {
  1200. char buffer[128];
  1201. struct tomoyo_acl_head *ptr =
  1202. list_entry(mpos, struct tomoyo_acl_head, list);
  1203. if (ptr->is_deleted)
  1204. continue;
  1205. if (idx == TOMOYO_PATH_GROUP) {
  1206. w[2] = container_of(ptr,
  1207. struct tomoyo_path_group,
  1208. head)->member_name->name;
  1209. } else if (idx == TOMOYO_NUMBER_GROUP) {
  1210. tomoyo_print_number(buffer, sizeof(buffer),
  1211. &container_of
  1212. (ptr, struct
  1213. tomoyo_number_group,
  1214. head)->number);
  1215. w[2] = buffer;
  1216. }
  1217. if (!tomoyo_io_printf(head, "%s%s %s\n", w[0], w[1],
  1218. w[2]))
  1219. return false;
  1220. }
  1221. }
  1222. return true;
  1223. }
  1224. /**
  1225. * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
  1226. *
  1227. * @head: Pointer to "struct tomoyo_io_buffer".
  1228. * @idx: Index number.
  1229. *
  1230. * Returns true on success, false otherwise.
  1231. *
  1232. * Caller holds tomoyo_read_lock().
  1233. */
  1234. static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
  1235. {
  1236. struct list_head *pos;
  1237. list_for_each_cookie(pos, head->read_var2, &tomoyo_policy_list[idx]) {
  1238. const char *w[4] = { "", "", "", "" };
  1239. struct tomoyo_acl_head *acl = container_of(pos, typeof(*acl),
  1240. list);
  1241. if (acl->is_deleted)
  1242. continue;
  1243. switch (idx) {
  1244. case TOMOYO_ID_DOMAIN_KEEPER:
  1245. {
  1246. struct tomoyo_domain_keeper_entry *ptr =
  1247. container_of(acl, typeof(*ptr), head);
  1248. w[0] = ptr->is_not ?
  1249. TOMOYO_KEYWORD_NO_KEEP_DOMAIN :
  1250. TOMOYO_KEYWORD_KEEP_DOMAIN;
  1251. if (ptr->program) {
  1252. w[1] = ptr->program->name;
  1253. w[2] = " from ";
  1254. }
  1255. w[3] = ptr->domainname->name;
  1256. }
  1257. break;
  1258. case TOMOYO_ID_DOMAIN_INITIALIZER:
  1259. {
  1260. struct tomoyo_domain_initializer_entry *ptr =
  1261. container_of(acl, typeof(*ptr), head);
  1262. w[0] = ptr->is_not ?
  1263. TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN :
  1264. TOMOYO_KEYWORD_INITIALIZE_DOMAIN;
  1265. w[1] = ptr->program->name;
  1266. if (ptr->domainname) {
  1267. w[2] = " from ";
  1268. w[3] = ptr->domainname->name;
  1269. }
  1270. }
  1271. break;
  1272. case TOMOYO_ID_GLOBALLY_READABLE:
  1273. {
  1274. struct tomoyo_globally_readable_file_entry *ptr
  1275. = container_of(acl, typeof(*ptr), head);
  1276. w[0] = TOMOYO_KEYWORD_ALLOW_READ;
  1277. w[1] = ptr->filename->name;
  1278. }
  1279. break;
  1280. case TOMOYO_ID_ALIAS:
  1281. {
  1282. struct tomoyo_alias_entry *ptr =
  1283. container_of(acl, typeof(*ptr), head);
  1284. w[0] = TOMOYO_KEYWORD_ALIAS;
  1285. w[1] = ptr->original_name->name;
  1286. w[2] = " ";
  1287. w[3] = ptr->aliased_name->name;
  1288. }
  1289. break;
  1290. case TOMOYO_ID_AGGREGATOR:
  1291. {
  1292. struct tomoyo_aggregator_entry *ptr =
  1293. container_of(acl, typeof(*ptr), head);
  1294. w[0] = TOMOYO_KEYWORD_AGGREGATOR;
  1295. w[1] = ptr->original_name->name;
  1296. w[2] = " ";
  1297. w[3] = ptr->aggregated_name->name;
  1298. }
  1299. break;
  1300. case TOMOYO_ID_PATTERN:
  1301. {
  1302. struct tomoyo_pattern_entry *ptr =
  1303. container_of(acl, typeof(*ptr), head);
  1304. w[0] = TOMOYO_KEYWORD_FILE_PATTERN;
  1305. w[1] = ptr->pattern->name;
  1306. }
  1307. break;
  1308. case TOMOYO_ID_NO_REWRITE:
  1309. {
  1310. struct tomoyo_no_rewrite_entry *ptr =
  1311. container_of(acl, typeof(*ptr), head);
  1312. w[0] = TOMOYO_KEYWORD_DENY_REWRITE;
  1313. w[1] = ptr->pattern->name;
  1314. }
  1315. break;
  1316. default:
  1317. continue;
  1318. }
  1319. if (!tomoyo_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2],
  1320. w[3]))
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. /**
  1326. * tomoyo_read_exception_policy - Read exception policy.
  1327. *
  1328. * @head: Pointer to "struct tomoyo_io_buffer".
  1329. *
  1330. * Caller holds tomoyo_read_lock().
  1331. */
  1332. static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
  1333. {
  1334. if (head->read_eof)
  1335. return;
  1336. while (head->read_step < TOMOYO_MAX_POLICY &&
  1337. tomoyo_read_policy(head, head->read_step))
  1338. head->read_step++;
  1339. if (head->read_step < TOMOYO_MAX_POLICY)
  1340. return;
  1341. while (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
  1342. tomoyo_read_group(head, head->read_step - TOMOYO_MAX_POLICY))
  1343. head->read_step++;
  1344. if (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
  1345. return;
  1346. head->read_eof = true;
  1347. }
  1348. /**
  1349. * tomoyo_print_header - Get header line of audit log.
  1350. *
  1351. * @r: Pointer to "struct tomoyo_request_info".
  1352. *
  1353. * Returns string representation.
  1354. *
  1355. * This function uses kmalloc(), so caller must kfree() if this function
  1356. * didn't return NULL.
  1357. */
  1358. static char *tomoyo_print_header(struct tomoyo_request_info *r)
  1359. {
  1360. static const char *tomoyo_mode_4[4] = {
  1361. "disabled", "learning", "permissive", "enforcing"
  1362. };
  1363. struct timeval tv;
  1364. const pid_t gpid = task_pid_nr(current);
  1365. static const int tomoyo_buffer_len = 4096;
  1366. char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
  1367. if (!buffer)
  1368. return NULL;
  1369. do_gettimeofday(&tv);
  1370. snprintf(buffer, tomoyo_buffer_len - 1,
  1371. "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
  1372. " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
  1373. " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
  1374. tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
  1375. (pid_t) sys_getpid(), (pid_t) sys_getppid(),
  1376. current_uid(), current_gid(), current_euid(),
  1377. current_egid(), current_suid(), current_sgid(),
  1378. current_fsuid(), current_fsgid());
  1379. return buffer;
  1380. }
  1381. /**
  1382. * tomoyo_init_audit_log - Allocate buffer for audit logs.
  1383. *
  1384. * @len: Required size.
  1385. * @r: Pointer to "struct tomoyo_request_info".
  1386. *
  1387. * Returns pointer to allocated memory.
  1388. *
  1389. * The @len is updated to add the header lines' size on success.
  1390. *
  1391. * This function uses kzalloc(), so caller must kfree() if this function
  1392. * didn't return NULL.
  1393. */
  1394. static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
  1395. {
  1396. char *buf = NULL;
  1397. const char *header;
  1398. const char *domainname;
  1399. if (!r->domain)
  1400. r->domain = tomoyo_domain();
  1401. domainname = r->domain->domainname->name;
  1402. header = tomoyo_print_header(r);
  1403. if (!header)
  1404. return NULL;
  1405. *len += strlen(domainname) + strlen(header) + 10;
  1406. buf = kzalloc(*len, GFP_NOFS);
  1407. if (buf)
  1408. snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
  1409. kfree(header);
  1410. return buf;
  1411. }
  1412. /* Wait queue for tomoyo_query_list. */
  1413. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1414. /* Lock for manipulating tomoyo_query_list. */
  1415. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1416. /* Structure for query. */
  1417. struct tomoyo_query_entry {
  1418. struct list_head list;
  1419. char *query;
  1420. int query_len;
  1421. unsigned int serial;
  1422. int timer;
  1423. int answer;
  1424. };
  1425. /* The list for "struct tomoyo_query_entry". */
  1426. static LIST_HEAD(tomoyo_query_list);
  1427. /*
  1428. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1429. * interface.
  1430. */
  1431. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1432. /**
  1433. * tomoyo_supervisor - Ask for the supervisor's decision.
  1434. *
  1435. * @r: Pointer to "struct tomoyo_request_info".
  1436. * @fmt: The printf()'s format string, followed by parameters.
  1437. *
  1438. * Returns 0 if the supervisor decided to permit the access request which
  1439. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1440. * supervisor decided to retry the access request which violated the policy in
  1441. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1442. */
  1443. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1444. {
  1445. va_list args;
  1446. int error = -EPERM;
  1447. int pos;
  1448. int len;
  1449. static unsigned int tomoyo_serial;
  1450. struct tomoyo_query_entry *tomoyo_query_entry = NULL;
  1451. bool quota_exceeded = false;
  1452. char *header;
  1453. switch (r->mode) {
  1454. char *buffer;
  1455. case TOMOYO_CONFIG_LEARNING:
  1456. if (!tomoyo_domain_quota_is_ok(r))
  1457. return 0;
  1458. va_start(args, fmt);
  1459. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
  1460. va_end(args);
  1461. buffer = kmalloc(len, GFP_NOFS);
  1462. if (!buffer)
  1463. return 0;
  1464. va_start(args, fmt);
  1465. vsnprintf(buffer, len - 1, fmt, args);
  1466. va_end(args);
  1467. tomoyo_normalize_line(buffer);
  1468. tomoyo_write_domain_policy2(buffer, r->domain, false);
  1469. kfree(buffer);
  1470. /* fall through */
  1471. case TOMOYO_CONFIG_PERMISSIVE:
  1472. return 0;
  1473. }
  1474. if (!r->domain)
  1475. r->domain = tomoyo_domain();
  1476. if (!atomic_read(&tomoyo_query_observers))
  1477. return -EPERM;
  1478. va_start(args, fmt);
  1479. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
  1480. va_end(args);
  1481. header = tomoyo_init_audit_log(&len, r);
  1482. if (!header)
  1483. goto out;
  1484. tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
  1485. if (!tomoyo_query_entry)
  1486. goto out;
  1487. tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
  1488. if (!tomoyo_query_entry->query)
  1489. goto out;
  1490. len = ksize(tomoyo_query_entry->query);
  1491. INIT_LIST_HEAD(&tomoyo_query_entry->list);
  1492. spin_lock(&tomoyo_query_list_lock);
  1493. if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
  1494. sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
  1495. quota_exceeded = true;
  1496. } else {
  1497. tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
  1498. tomoyo_query_entry->serial = tomoyo_serial++;
  1499. }
  1500. spin_unlock(&tomoyo_query_list_lock);
  1501. if (quota_exceeded)
  1502. goto out;
  1503. pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
  1504. tomoyo_query_entry->serial, r->retry, header);
  1505. kfree(header);
  1506. header = NULL;
  1507. va_start(args, fmt);
  1508. vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
  1509. tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
  1510. va_end(args);
  1511. spin_lock(&tomoyo_query_list_lock);
  1512. list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
  1513. spin_unlock(&tomoyo_query_list_lock);
  1514. /* Give 10 seconds for supervisor's opinion. */
  1515. for (tomoyo_query_entry->timer = 0;
  1516. atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
  1517. tomoyo_query_entry->timer++) {
  1518. wake_up(&tomoyo_query_wait);
  1519. set_current_state(TASK_INTERRUPTIBLE);
  1520. schedule_timeout(HZ / 10);
  1521. if (tomoyo_query_entry->answer)
  1522. break;
  1523. }
  1524. spin_lock(&tomoyo_query_list_lock);
  1525. list_del(&tomoyo_query_entry->list);
  1526. tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
  1527. spin_unlock(&tomoyo_query_list_lock);
  1528. switch (tomoyo_query_entry->answer) {
  1529. case 3: /* Asked to retry by administrator. */
  1530. error = TOMOYO_RETRY_REQUEST;
  1531. r->retry++;
  1532. break;
  1533. case 1:
  1534. /* Granted by administrator. */
  1535. error = 0;
  1536. break;
  1537. case 0:
  1538. /* Timed out. */
  1539. break;
  1540. default:
  1541. /* Rejected by administrator. */
  1542. break;
  1543. }
  1544. out:
  1545. if (tomoyo_query_entry)
  1546. kfree(tomoyo_query_entry->query);
  1547. kfree(tomoyo_query_entry);
  1548. kfree(header);
  1549. return error;
  1550. }
  1551. /**
  1552. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  1553. *
  1554. * @file: Pointer to "struct file".
  1555. * @wait: Pointer to "poll_table".
  1556. *
  1557. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  1558. *
  1559. * Waits for access requests which violated policy in enforcing mode.
  1560. */
  1561. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  1562. {
  1563. struct list_head *tmp;
  1564. bool found = false;
  1565. u8 i;
  1566. for (i = 0; i < 2; i++) {
  1567. spin_lock(&tomoyo_query_list_lock);
  1568. list_for_each(tmp, &tomoyo_query_list) {
  1569. struct tomoyo_query_entry *ptr
  1570. = list_entry(tmp, struct tomoyo_query_entry,
  1571. list);
  1572. if (ptr->answer)
  1573. continue;
  1574. found = true;
  1575. break;
  1576. }
  1577. spin_unlock(&tomoyo_query_list_lock);
  1578. if (found)
  1579. return POLLIN | POLLRDNORM;
  1580. if (i)
  1581. break;
  1582. poll_wait(file, &tomoyo_query_wait, wait);
  1583. }
  1584. return 0;
  1585. }
  1586. /**
  1587. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  1588. *
  1589. * @head: Pointer to "struct tomoyo_io_buffer".
  1590. */
  1591. static void tomoyo_read_query(struct tomoyo_io_buffer *head)
  1592. {
  1593. struct list_head *tmp;
  1594. int pos = 0;
  1595. int len = 0;
  1596. char *buf;
  1597. if (head->read_avail)
  1598. return;
  1599. if (head->read_buf) {
  1600. kfree(head->read_buf);
  1601. head->read_buf = NULL;
  1602. head->readbuf_size = 0;
  1603. }
  1604. spin_lock(&tomoyo_query_list_lock);
  1605. list_for_each(tmp, &tomoyo_query_list) {
  1606. struct tomoyo_query_entry *ptr
  1607. = list_entry(tmp, struct tomoyo_query_entry, list);
  1608. if (ptr->answer)
  1609. continue;
  1610. if (pos++ != head->read_step)
  1611. continue;
  1612. len = ptr->query_len;
  1613. break;
  1614. }
  1615. spin_unlock(&tomoyo_query_list_lock);
  1616. if (!len) {
  1617. head->read_step = 0;
  1618. return;
  1619. }
  1620. buf = kzalloc(len, GFP_NOFS);
  1621. if (!buf)
  1622. return;
  1623. pos = 0;
  1624. spin_lock(&tomoyo_query_list_lock);
  1625. list_for_each(tmp, &tomoyo_query_list) {
  1626. struct tomoyo_query_entry *ptr
  1627. = list_entry(tmp, struct tomoyo_query_entry, list);
  1628. if (ptr->answer)
  1629. continue;
  1630. if (pos++ != head->read_step)
  1631. continue;
  1632. /*
  1633. * Some query can be skipped because tomoyo_query_list
  1634. * can change, but I don't care.
  1635. */
  1636. if (len == ptr->query_len)
  1637. memmove(buf, ptr->query, len);
  1638. break;
  1639. }
  1640. spin_unlock(&tomoyo_query_list_lock);
  1641. if (buf[0]) {
  1642. head->read_avail = len;
  1643. head->readbuf_size = head->read_avail;
  1644. head->read_buf = buf;
  1645. head->read_step++;
  1646. } else {
  1647. kfree(buf);
  1648. }
  1649. }
  1650. /**
  1651. * tomoyo_write_answer - Write the supervisor's decision.
  1652. *
  1653. * @head: Pointer to "struct tomoyo_io_buffer".
  1654. *
  1655. * Returns 0 on success, -EINVAL otherwise.
  1656. */
  1657. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  1658. {
  1659. char *data = head->write_buf;
  1660. struct list_head *tmp;
  1661. unsigned int serial;
  1662. unsigned int answer;
  1663. spin_lock(&tomoyo_query_list_lock);
  1664. list_for_each(tmp, &tomoyo_query_list) {
  1665. struct tomoyo_query_entry *ptr
  1666. = list_entry(tmp, struct tomoyo_query_entry, list);
  1667. ptr->timer = 0;
  1668. }
  1669. spin_unlock(&tomoyo_query_list_lock);
  1670. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  1671. return -EINVAL;
  1672. spin_lock(&tomoyo_query_list_lock);
  1673. list_for_each(tmp, &tomoyo_query_list) {
  1674. struct tomoyo_query_entry *ptr
  1675. = list_entry(tmp, struct tomoyo_query_entry, list);
  1676. if (ptr->serial != serial)
  1677. continue;
  1678. if (!ptr->answer)
  1679. ptr->answer = answer;
  1680. break;
  1681. }
  1682. spin_unlock(&tomoyo_query_list_lock);
  1683. return 0;
  1684. }
  1685. /**
  1686. * tomoyo_read_version: Get version.
  1687. *
  1688. * @head: Pointer to "struct tomoyo_io_buffer".
  1689. *
  1690. * Returns version information.
  1691. */
  1692. static void tomoyo_read_version(struct tomoyo_io_buffer *head)
  1693. {
  1694. if (!head->read_eof) {
  1695. tomoyo_io_printf(head, "2.3.0-pre");
  1696. head->read_eof = true;
  1697. }
  1698. }
  1699. /**
  1700. * tomoyo_read_self_domain - Get the current process's domainname.
  1701. *
  1702. * @head: Pointer to "struct tomoyo_io_buffer".
  1703. *
  1704. * Returns the current process's domainname.
  1705. */
  1706. static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1707. {
  1708. if (!head->read_eof) {
  1709. /*
  1710. * tomoyo_domain()->domainname != NULL
  1711. * because every process belongs to a domain and
  1712. * the domain's name cannot be NULL.
  1713. */
  1714. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1715. head->read_eof = true;
  1716. }
  1717. }
  1718. /**
  1719. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1720. *
  1721. * @type: Type of interface.
  1722. * @file: Pointer to "struct file".
  1723. *
  1724. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1725. *
  1726. * Caller acquires tomoyo_read_lock().
  1727. */
  1728. int tomoyo_open_control(const u8 type, struct file *file)
  1729. {
  1730. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  1731. if (!head)
  1732. return -ENOMEM;
  1733. mutex_init(&head->io_sem);
  1734. head->type = type;
  1735. switch (type) {
  1736. case TOMOYO_DOMAINPOLICY:
  1737. /* /sys/kernel/security/tomoyo/domain_policy */
  1738. head->write = tomoyo_write_domain_policy;
  1739. head->read = tomoyo_read_domain_policy;
  1740. break;
  1741. case TOMOYO_EXCEPTIONPOLICY:
  1742. /* /sys/kernel/security/tomoyo/exception_policy */
  1743. head->write = tomoyo_write_exception_policy;
  1744. head->read = tomoyo_read_exception_policy;
  1745. break;
  1746. case TOMOYO_SELFDOMAIN:
  1747. /* /sys/kernel/security/tomoyo/self_domain */
  1748. head->read = tomoyo_read_self_domain;
  1749. break;
  1750. case TOMOYO_DOMAIN_STATUS:
  1751. /* /sys/kernel/security/tomoyo/.domain_status */
  1752. head->write = tomoyo_write_domain_profile;
  1753. head->read = tomoyo_read_domain_profile;
  1754. break;
  1755. case TOMOYO_PROCESS_STATUS:
  1756. /* /sys/kernel/security/tomoyo/.process_status */
  1757. head->write = tomoyo_write_pid;
  1758. head->read = tomoyo_read_pid;
  1759. break;
  1760. case TOMOYO_VERSION:
  1761. /* /sys/kernel/security/tomoyo/version */
  1762. head->read = tomoyo_read_version;
  1763. head->readbuf_size = 128;
  1764. break;
  1765. case TOMOYO_MEMINFO:
  1766. /* /sys/kernel/security/tomoyo/meminfo */
  1767. head->write = tomoyo_write_memory_quota;
  1768. head->read = tomoyo_read_memory_counter;
  1769. head->readbuf_size = 512;
  1770. break;
  1771. case TOMOYO_PROFILE:
  1772. /* /sys/kernel/security/tomoyo/profile */
  1773. head->write = tomoyo_write_profile;
  1774. head->read = tomoyo_read_profile;
  1775. break;
  1776. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  1777. head->poll = tomoyo_poll_query;
  1778. head->write = tomoyo_write_answer;
  1779. head->read = tomoyo_read_query;
  1780. break;
  1781. case TOMOYO_MANAGER:
  1782. /* /sys/kernel/security/tomoyo/manager */
  1783. head->write = tomoyo_write_manager_policy;
  1784. head->read = tomoyo_read_manager_policy;
  1785. break;
  1786. }
  1787. if (!(file->f_mode & FMODE_READ)) {
  1788. /*
  1789. * No need to allocate read_buf since it is not opened
  1790. * for reading.
  1791. */
  1792. head->read = NULL;
  1793. head->poll = NULL;
  1794. } else if (!head->poll) {
  1795. /* Don't allocate read_buf for poll() access. */
  1796. if (!head->readbuf_size)
  1797. head->readbuf_size = 4096 * 2;
  1798. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  1799. if (!head->read_buf) {
  1800. kfree(head);
  1801. return -ENOMEM;
  1802. }
  1803. }
  1804. if (!(file->f_mode & FMODE_WRITE)) {
  1805. /*
  1806. * No need to allocate write_buf since it is not opened
  1807. * for writing.
  1808. */
  1809. head->write = NULL;
  1810. } else if (head->write) {
  1811. head->writebuf_size = 4096 * 2;
  1812. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  1813. if (!head->write_buf) {
  1814. kfree(head->read_buf);
  1815. kfree(head);
  1816. return -ENOMEM;
  1817. }
  1818. }
  1819. if (type != TOMOYO_QUERY)
  1820. head->reader_idx = tomoyo_read_lock();
  1821. file->private_data = head;
  1822. /*
  1823. * Call the handler now if the file is
  1824. * /sys/kernel/security/tomoyo/self_domain
  1825. * so that the user can use
  1826. * cat < /sys/kernel/security/tomoyo/self_domain"
  1827. * to know the current process's domainname.
  1828. */
  1829. if (type == TOMOYO_SELFDOMAIN)
  1830. tomoyo_read_control(file, NULL, 0);
  1831. /*
  1832. * If the file is /sys/kernel/security/tomoyo/query , increment the
  1833. * observer counter.
  1834. * The obserber counter is used by tomoyo_supervisor() to see if
  1835. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  1836. */
  1837. else if (type == TOMOYO_QUERY)
  1838. atomic_inc(&tomoyo_query_observers);
  1839. return 0;
  1840. }
  1841. /**
  1842. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  1843. *
  1844. * @file: Pointer to "struct file".
  1845. * @wait: Pointer to "poll_table".
  1846. *
  1847. * Waits for read readiness.
  1848. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
  1849. */
  1850. int tomoyo_poll_control(struct file *file, poll_table *wait)
  1851. {
  1852. struct tomoyo_io_buffer *head = file->private_data;
  1853. if (!head->poll)
  1854. return -ENOSYS;
  1855. return head->poll(file, wait);
  1856. }
  1857. /**
  1858. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1859. *
  1860. * @file: Pointer to "struct file".
  1861. * @buffer: Poiner to buffer to write to.
  1862. * @buffer_len: Size of @buffer.
  1863. *
  1864. * Returns bytes read on success, negative value otherwise.
  1865. *
  1866. * Caller holds tomoyo_read_lock().
  1867. */
  1868. int tomoyo_read_control(struct file *file, char __user *buffer,
  1869. const int buffer_len)
  1870. {
  1871. int len = 0;
  1872. struct tomoyo_io_buffer *head = file->private_data;
  1873. char *cp;
  1874. if (!head->read)
  1875. return -ENOSYS;
  1876. if (mutex_lock_interruptible(&head->io_sem))
  1877. return -EINTR;
  1878. /* Call the policy handler. */
  1879. head->read(head);
  1880. if (len < 0)
  1881. goto out;
  1882. /* Write to buffer. */
  1883. len = head->read_avail;
  1884. if (len > buffer_len)
  1885. len = buffer_len;
  1886. if (!len)
  1887. goto out;
  1888. /* head->read_buf changes by some functions. */
  1889. cp = head->read_buf;
  1890. if (copy_to_user(buffer, cp, len)) {
  1891. len = -EFAULT;
  1892. goto out;
  1893. }
  1894. head->read_avail -= len;
  1895. memmove(cp, cp + len, head->read_avail);
  1896. out:
  1897. mutex_unlock(&head->io_sem);
  1898. return len;
  1899. }
  1900. /**
  1901. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1902. *
  1903. * @file: Pointer to "struct file".
  1904. * @buffer: Pointer to buffer to read from.
  1905. * @buffer_len: Size of @buffer.
  1906. *
  1907. * Returns @buffer_len on success, negative value otherwise.
  1908. *
  1909. * Caller holds tomoyo_read_lock().
  1910. */
  1911. int tomoyo_write_control(struct file *file, const char __user *buffer,
  1912. const int buffer_len)
  1913. {
  1914. struct tomoyo_io_buffer *head = file->private_data;
  1915. int error = buffer_len;
  1916. int avail_len = buffer_len;
  1917. char *cp0 = head->write_buf;
  1918. if (!head->write)
  1919. return -ENOSYS;
  1920. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1921. return -EFAULT;
  1922. /* Don't allow updating policies by non manager programs. */
  1923. if (head->write != tomoyo_write_pid &&
  1924. head->write != tomoyo_write_domain_policy &&
  1925. !tomoyo_policy_manager())
  1926. return -EPERM;
  1927. if (mutex_lock_interruptible(&head->io_sem))
  1928. return -EINTR;
  1929. /* Read a line and dispatch it to the policy handler. */
  1930. while (avail_len > 0) {
  1931. char c;
  1932. if (head->write_avail >= head->writebuf_size - 1) {
  1933. error = -ENOMEM;
  1934. break;
  1935. } else if (get_user(c, buffer)) {
  1936. error = -EFAULT;
  1937. break;
  1938. }
  1939. buffer++;
  1940. avail_len--;
  1941. cp0[head->write_avail++] = c;
  1942. if (c != '\n')
  1943. continue;
  1944. cp0[head->write_avail - 1] = '\0';
  1945. head->write_avail = 0;
  1946. tomoyo_normalize_line(cp0);
  1947. head->write(head);
  1948. }
  1949. mutex_unlock(&head->io_sem);
  1950. return error;
  1951. }
  1952. /**
  1953. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  1954. *
  1955. * @file: Pointer to "struct file".
  1956. *
  1957. * Releases memory and returns 0.
  1958. *
  1959. * Caller looses tomoyo_read_lock().
  1960. */
  1961. int tomoyo_close_control(struct file *file)
  1962. {
  1963. struct tomoyo_io_buffer *head = file->private_data;
  1964. const bool is_write = !!head->write_buf;
  1965. /*
  1966. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  1967. * observer counter.
  1968. */
  1969. if (head->type == TOMOYO_QUERY)
  1970. atomic_dec(&tomoyo_query_observers);
  1971. else
  1972. tomoyo_read_unlock(head->reader_idx);
  1973. /* Release memory used for policy I/O. */
  1974. kfree(head->read_buf);
  1975. head->read_buf = NULL;
  1976. kfree(head->write_buf);
  1977. head->write_buf = NULL;
  1978. kfree(head);
  1979. head = NULL;
  1980. file->private_data = NULL;
  1981. if (is_write)
  1982. tomoyo_run_gc();
  1983. return 0;
  1984. }
  1985. /**
  1986. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  1987. */
  1988. void tomoyo_check_profile(void)
  1989. {
  1990. struct tomoyo_domain_info *domain;
  1991. const int idx = tomoyo_read_lock();
  1992. tomoyo_policy_loaded = true;
  1993. /* Check all profiles currently assigned to domains are defined. */
  1994. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1995. const u8 profile = domain->profile;
  1996. if (tomoyo_profile_ptr[profile])
  1997. continue;
  1998. panic("Profile %u (used by '%s') not defined.\n",
  1999. profile, domain->domainname->name);
  2000. }
  2001. tomoyo_read_unlock(idx);
  2002. if (tomoyo_profile_version != 20090903)
  2003. panic("Profile version %u is not supported.\n",
  2004. tomoyo_profile_version);
  2005. printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n");
  2006. printk(KERN_INFO "Mandatory Access Control activated.\n");
  2007. }