common.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  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_ALLOW_READ))
  1128. return tomoyo_write_globally_readable_policy(data, is_delete);
  1129. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
  1130. return tomoyo_write_pattern_policy(data, is_delete);
  1131. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
  1132. return tomoyo_write_no_rewrite_policy(data, is_delete);
  1133. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
  1134. return tomoyo_write_group(data, is_delete, TOMOYO_PATH_GROUP);
  1135. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
  1136. return tomoyo_write_group(data, is_delete, TOMOYO_NUMBER_GROUP);
  1137. return -EINVAL;
  1138. }
  1139. static void tomoyo_print_number(char *buffer, int buffer_len,
  1140. const struct tomoyo_number_union *ptr)
  1141. {
  1142. int i;
  1143. unsigned long min = ptr->values[0];
  1144. const unsigned long max = ptr->values[1];
  1145. u8 min_type = ptr->min_type;
  1146. const u8 max_type = ptr->max_type;
  1147. memset(buffer, 0, buffer_len);
  1148. buffer_len -= 2;
  1149. for (i = 0; i < 2; i++) {
  1150. int len;
  1151. switch (min_type) {
  1152. case TOMOYO_VALUE_TYPE_HEXADECIMAL:
  1153. snprintf(buffer, buffer_len, "0x%lX", min);
  1154. break;
  1155. case TOMOYO_VALUE_TYPE_OCTAL:
  1156. snprintf(buffer, buffer_len, "0%lo", min);
  1157. break;
  1158. default:
  1159. snprintf(buffer, buffer_len, "%lu", min);
  1160. break;
  1161. }
  1162. if (min == max && min_type == max_type)
  1163. break;
  1164. len = strlen(buffer);
  1165. buffer[len++] = '-';
  1166. buffer += len;
  1167. buffer_len -= len;
  1168. min_type = max_type;
  1169. min = max;
  1170. }
  1171. }
  1172. static const char *tomoyo_group_name[TOMOYO_MAX_GROUP] = {
  1173. [TOMOYO_PATH_GROUP] = TOMOYO_KEYWORD_PATH_GROUP,
  1174. [TOMOYO_NUMBER_GROUP] = TOMOYO_KEYWORD_NUMBER_GROUP
  1175. };
  1176. /**
  1177. * tomoyo_read_group - Read "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  1178. *
  1179. * @head: Pointer to "struct tomoyo_io_buffer".
  1180. * @idx: Index number.
  1181. *
  1182. * Returns true on success, false otherwise.
  1183. *
  1184. * Caller holds tomoyo_read_lock().
  1185. */
  1186. static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
  1187. {
  1188. struct list_head *gpos;
  1189. struct list_head *mpos;
  1190. const char *w[3] = { "", "", "" };
  1191. w[0] = tomoyo_group_name[idx];
  1192. list_for_each_cookie(gpos, head->read_var1, &tomoyo_group_list[idx]) {
  1193. struct tomoyo_group *group =
  1194. list_entry(gpos, struct tomoyo_group, list);
  1195. w[1] = group->group_name->name;
  1196. list_for_each_cookie(mpos, head->read_var2,
  1197. &group->member_list) {
  1198. char buffer[128];
  1199. struct tomoyo_acl_head *ptr =
  1200. list_entry(mpos, struct tomoyo_acl_head, list);
  1201. if (ptr->is_deleted)
  1202. continue;
  1203. if (idx == TOMOYO_PATH_GROUP) {
  1204. w[2] = container_of(ptr,
  1205. struct tomoyo_path_group,
  1206. head)->member_name->name;
  1207. } else if (idx == TOMOYO_NUMBER_GROUP) {
  1208. tomoyo_print_number(buffer, sizeof(buffer),
  1209. &container_of
  1210. (ptr, struct
  1211. tomoyo_number_group,
  1212. head)->number);
  1213. w[2] = buffer;
  1214. }
  1215. if (!tomoyo_io_printf(head, "%s%s %s\n", w[0], w[1],
  1216. w[2]))
  1217. return false;
  1218. }
  1219. }
  1220. return true;
  1221. }
  1222. /**
  1223. * tomoyo_read_policy - Read "struct tomoyo_..._entry" list.
  1224. *
  1225. * @head: Pointer to "struct tomoyo_io_buffer".
  1226. * @idx: Index number.
  1227. *
  1228. * Returns true on success, false otherwise.
  1229. *
  1230. * Caller holds tomoyo_read_lock().
  1231. */
  1232. static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx)
  1233. {
  1234. struct list_head *pos;
  1235. list_for_each_cookie(pos, head->read_var2, &tomoyo_policy_list[idx]) {
  1236. const char *w[4] = { "", "", "", "" };
  1237. struct tomoyo_acl_head *acl = container_of(pos, typeof(*acl),
  1238. list);
  1239. if (acl->is_deleted)
  1240. continue;
  1241. switch (idx) {
  1242. case TOMOYO_ID_DOMAIN_KEEPER:
  1243. {
  1244. struct tomoyo_domain_keeper_entry *ptr =
  1245. container_of(acl, typeof(*ptr), head);
  1246. w[0] = ptr->is_not ?
  1247. TOMOYO_KEYWORD_NO_KEEP_DOMAIN :
  1248. TOMOYO_KEYWORD_KEEP_DOMAIN;
  1249. if (ptr->program) {
  1250. w[1] = ptr->program->name;
  1251. w[2] = " from ";
  1252. }
  1253. w[3] = ptr->domainname->name;
  1254. }
  1255. break;
  1256. case TOMOYO_ID_DOMAIN_INITIALIZER:
  1257. {
  1258. struct tomoyo_domain_initializer_entry *ptr =
  1259. container_of(acl, typeof(*ptr), head);
  1260. w[0] = ptr->is_not ?
  1261. TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN :
  1262. TOMOYO_KEYWORD_INITIALIZE_DOMAIN;
  1263. w[1] = ptr->program->name;
  1264. if (ptr->domainname) {
  1265. w[2] = " from ";
  1266. w[3] = ptr->domainname->name;
  1267. }
  1268. }
  1269. break;
  1270. case TOMOYO_ID_GLOBALLY_READABLE:
  1271. {
  1272. struct tomoyo_globally_readable_file_entry *ptr
  1273. = container_of(acl, typeof(*ptr), head);
  1274. w[0] = TOMOYO_KEYWORD_ALLOW_READ;
  1275. w[1] = ptr->filename->name;
  1276. }
  1277. break;
  1278. case TOMOYO_ID_AGGREGATOR:
  1279. {
  1280. struct tomoyo_aggregator_entry *ptr =
  1281. container_of(acl, typeof(*ptr), head);
  1282. w[0] = TOMOYO_KEYWORD_AGGREGATOR;
  1283. w[1] = ptr->original_name->name;
  1284. w[2] = " ";
  1285. w[3] = ptr->aggregated_name->name;
  1286. }
  1287. break;
  1288. case TOMOYO_ID_PATTERN:
  1289. {
  1290. struct tomoyo_pattern_entry *ptr =
  1291. container_of(acl, typeof(*ptr), head);
  1292. w[0] = TOMOYO_KEYWORD_FILE_PATTERN;
  1293. w[1] = ptr->pattern->name;
  1294. }
  1295. break;
  1296. case TOMOYO_ID_NO_REWRITE:
  1297. {
  1298. struct tomoyo_no_rewrite_entry *ptr =
  1299. container_of(acl, typeof(*ptr), head);
  1300. w[0] = TOMOYO_KEYWORD_DENY_REWRITE;
  1301. w[1] = ptr->pattern->name;
  1302. }
  1303. break;
  1304. default:
  1305. continue;
  1306. }
  1307. if (!tomoyo_io_printf(head, "%s%s%s%s\n", w[0], w[1], w[2],
  1308. w[3]))
  1309. return false;
  1310. }
  1311. return true;
  1312. }
  1313. /**
  1314. * tomoyo_read_exception_policy - Read exception policy.
  1315. *
  1316. * @head: Pointer to "struct tomoyo_io_buffer".
  1317. *
  1318. * Caller holds tomoyo_read_lock().
  1319. */
  1320. static void tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
  1321. {
  1322. if (head->read_eof)
  1323. return;
  1324. while (head->read_step < TOMOYO_MAX_POLICY &&
  1325. tomoyo_read_policy(head, head->read_step))
  1326. head->read_step++;
  1327. if (head->read_step < TOMOYO_MAX_POLICY)
  1328. return;
  1329. while (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP &&
  1330. tomoyo_read_group(head, head->read_step - TOMOYO_MAX_POLICY))
  1331. head->read_step++;
  1332. if (head->read_step < TOMOYO_MAX_POLICY + TOMOYO_MAX_GROUP)
  1333. return;
  1334. head->read_eof = true;
  1335. }
  1336. /**
  1337. * tomoyo_print_header - Get header line of audit log.
  1338. *
  1339. * @r: Pointer to "struct tomoyo_request_info".
  1340. *
  1341. * Returns string representation.
  1342. *
  1343. * This function uses kmalloc(), so caller must kfree() if this function
  1344. * didn't return NULL.
  1345. */
  1346. static char *tomoyo_print_header(struct tomoyo_request_info *r)
  1347. {
  1348. static const char *tomoyo_mode_4[4] = {
  1349. "disabled", "learning", "permissive", "enforcing"
  1350. };
  1351. struct timeval tv;
  1352. const pid_t gpid = task_pid_nr(current);
  1353. static const int tomoyo_buffer_len = 4096;
  1354. char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
  1355. if (!buffer)
  1356. return NULL;
  1357. do_gettimeofday(&tv);
  1358. snprintf(buffer, tomoyo_buffer_len - 1,
  1359. "#timestamp=%lu profile=%u mode=%s (global-pid=%u)"
  1360. " task={ pid=%u ppid=%u uid=%u gid=%u euid=%u"
  1361. " egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
  1362. tv.tv_sec, r->profile, tomoyo_mode_4[r->mode], gpid,
  1363. (pid_t) sys_getpid(), (pid_t) sys_getppid(),
  1364. current_uid(), current_gid(), current_euid(),
  1365. current_egid(), current_suid(), current_sgid(),
  1366. current_fsuid(), current_fsgid());
  1367. return buffer;
  1368. }
  1369. /**
  1370. * tomoyo_init_audit_log - Allocate buffer for audit logs.
  1371. *
  1372. * @len: Required size.
  1373. * @r: Pointer to "struct tomoyo_request_info".
  1374. *
  1375. * Returns pointer to allocated memory.
  1376. *
  1377. * The @len is updated to add the header lines' size on success.
  1378. *
  1379. * This function uses kzalloc(), so caller must kfree() if this function
  1380. * didn't return NULL.
  1381. */
  1382. static char *tomoyo_init_audit_log(int *len, struct tomoyo_request_info *r)
  1383. {
  1384. char *buf = NULL;
  1385. const char *header;
  1386. const char *domainname;
  1387. if (!r->domain)
  1388. r->domain = tomoyo_domain();
  1389. domainname = r->domain->domainname->name;
  1390. header = tomoyo_print_header(r);
  1391. if (!header)
  1392. return NULL;
  1393. *len += strlen(domainname) + strlen(header) + 10;
  1394. buf = kzalloc(*len, GFP_NOFS);
  1395. if (buf)
  1396. snprintf(buf, (*len) - 1, "%s\n%s\n", header, domainname);
  1397. kfree(header);
  1398. return buf;
  1399. }
  1400. /* Wait queue for tomoyo_query_list. */
  1401. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_query_wait);
  1402. /* Lock for manipulating tomoyo_query_list. */
  1403. static DEFINE_SPINLOCK(tomoyo_query_list_lock);
  1404. /* Structure for query. */
  1405. struct tomoyo_query_entry {
  1406. struct list_head list;
  1407. char *query;
  1408. int query_len;
  1409. unsigned int serial;
  1410. int timer;
  1411. int answer;
  1412. };
  1413. /* The list for "struct tomoyo_query_entry". */
  1414. static LIST_HEAD(tomoyo_query_list);
  1415. /*
  1416. * Number of "struct file" referring /sys/kernel/security/tomoyo/query
  1417. * interface.
  1418. */
  1419. static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
  1420. /**
  1421. * tomoyo_supervisor - Ask for the supervisor's decision.
  1422. *
  1423. * @r: Pointer to "struct tomoyo_request_info".
  1424. * @fmt: The printf()'s format string, followed by parameters.
  1425. *
  1426. * Returns 0 if the supervisor decided to permit the access request which
  1427. * violated the policy in enforcing mode, TOMOYO_RETRY_REQUEST if the
  1428. * supervisor decided to retry the access request which violated the policy in
  1429. * enforcing mode, 0 if it is not in enforcing mode, -EPERM otherwise.
  1430. */
  1431. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  1432. {
  1433. va_list args;
  1434. int error = -EPERM;
  1435. int pos;
  1436. int len;
  1437. static unsigned int tomoyo_serial;
  1438. struct tomoyo_query_entry *tomoyo_query_entry = NULL;
  1439. bool quota_exceeded = false;
  1440. char *header;
  1441. switch (r->mode) {
  1442. char *buffer;
  1443. case TOMOYO_CONFIG_LEARNING:
  1444. if (!tomoyo_domain_quota_is_ok(r))
  1445. return 0;
  1446. va_start(args, fmt);
  1447. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 4;
  1448. va_end(args);
  1449. buffer = kmalloc(len, GFP_NOFS);
  1450. if (!buffer)
  1451. return 0;
  1452. va_start(args, fmt);
  1453. vsnprintf(buffer, len - 1, fmt, args);
  1454. va_end(args);
  1455. tomoyo_normalize_line(buffer);
  1456. tomoyo_write_domain_policy2(buffer, r->domain, false);
  1457. kfree(buffer);
  1458. /* fall through */
  1459. case TOMOYO_CONFIG_PERMISSIVE:
  1460. return 0;
  1461. }
  1462. if (!r->domain)
  1463. r->domain = tomoyo_domain();
  1464. if (!atomic_read(&tomoyo_query_observers))
  1465. return -EPERM;
  1466. va_start(args, fmt);
  1467. len = vsnprintf((char *) &pos, sizeof(pos) - 1, fmt, args) + 32;
  1468. va_end(args);
  1469. header = tomoyo_init_audit_log(&len, r);
  1470. if (!header)
  1471. goto out;
  1472. tomoyo_query_entry = kzalloc(sizeof(*tomoyo_query_entry), GFP_NOFS);
  1473. if (!tomoyo_query_entry)
  1474. goto out;
  1475. tomoyo_query_entry->query = kzalloc(len, GFP_NOFS);
  1476. if (!tomoyo_query_entry->query)
  1477. goto out;
  1478. len = ksize(tomoyo_query_entry->query);
  1479. INIT_LIST_HEAD(&tomoyo_query_entry->list);
  1480. spin_lock(&tomoyo_query_list_lock);
  1481. if (tomoyo_quota_for_query && tomoyo_query_memory_size + len +
  1482. sizeof(*tomoyo_query_entry) >= tomoyo_quota_for_query) {
  1483. quota_exceeded = true;
  1484. } else {
  1485. tomoyo_query_memory_size += len + sizeof(*tomoyo_query_entry);
  1486. tomoyo_query_entry->serial = tomoyo_serial++;
  1487. }
  1488. spin_unlock(&tomoyo_query_list_lock);
  1489. if (quota_exceeded)
  1490. goto out;
  1491. pos = snprintf(tomoyo_query_entry->query, len - 1, "Q%u-%hu\n%s",
  1492. tomoyo_query_entry->serial, r->retry, header);
  1493. kfree(header);
  1494. header = NULL;
  1495. va_start(args, fmt);
  1496. vsnprintf(tomoyo_query_entry->query + pos, len - 1 - pos, fmt, args);
  1497. tomoyo_query_entry->query_len = strlen(tomoyo_query_entry->query) + 1;
  1498. va_end(args);
  1499. spin_lock(&tomoyo_query_list_lock);
  1500. list_add_tail(&tomoyo_query_entry->list, &tomoyo_query_list);
  1501. spin_unlock(&tomoyo_query_list_lock);
  1502. /* Give 10 seconds for supervisor's opinion. */
  1503. for (tomoyo_query_entry->timer = 0;
  1504. atomic_read(&tomoyo_query_observers) && tomoyo_query_entry->timer < 100;
  1505. tomoyo_query_entry->timer++) {
  1506. wake_up(&tomoyo_query_wait);
  1507. set_current_state(TASK_INTERRUPTIBLE);
  1508. schedule_timeout(HZ / 10);
  1509. if (tomoyo_query_entry->answer)
  1510. break;
  1511. }
  1512. spin_lock(&tomoyo_query_list_lock);
  1513. list_del(&tomoyo_query_entry->list);
  1514. tomoyo_query_memory_size -= len + sizeof(*tomoyo_query_entry);
  1515. spin_unlock(&tomoyo_query_list_lock);
  1516. switch (tomoyo_query_entry->answer) {
  1517. case 3: /* Asked to retry by administrator. */
  1518. error = TOMOYO_RETRY_REQUEST;
  1519. r->retry++;
  1520. break;
  1521. case 1:
  1522. /* Granted by administrator. */
  1523. error = 0;
  1524. break;
  1525. case 0:
  1526. /* Timed out. */
  1527. break;
  1528. default:
  1529. /* Rejected by administrator. */
  1530. break;
  1531. }
  1532. out:
  1533. if (tomoyo_query_entry)
  1534. kfree(tomoyo_query_entry->query);
  1535. kfree(tomoyo_query_entry);
  1536. kfree(header);
  1537. return error;
  1538. }
  1539. /**
  1540. * tomoyo_poll_query - poll() for /sys/kernel/security/tomoyo/query.
  1541. *
  1542. * @file: Pointer to "struct file".
  1543. * @wait: Pointer to "poll_table".
  1544. *
  1545. * Returns POLLIN | POLLRDNORM when ready to read, 0 otherwise.
  1546. *
  1547. * Waits for access requests which violated policy in enforcing mode.
  1548. */
  1549. static int tomoyo_poll_query(struct file *file, poll_table *wait)
  1550. {
  1551. struct list_head *tmp;
  1552. bool found = false;
  1553. u8 i;
  1554. for (i = 0; i < 2; i++) {
  1555. spin_lock(&tomoyo_query_list_lock);
  1556. list_for_each(tmp, &tomoyo_query_list) {
  1557. struct tomoyo_query_entry *ptr
  1558. = list_entry(tmp, struct tomoyo_query_entry,
  1559. list);
  1560. if (ptr->answer)
  1561. continue;
  1562. found = true;
  1563. break;
  1564. }
  1565. spin_unlock(&tomoyo_query_list_lock);
  1566. if (found)
  1567. return POLLIN | POLLRDNORM;
  1568. if (i)
  1569. break;
  1570. poll_wait(file, &tomoyo_query_wait, wait);
  1571. }
  1572. return 0;
  1573. }
  1574. /**
  1575. * tomoyo_read_query - Read access requests which violated policy in enforcing mode.
  1576. *
  1577. * @head: Pointer to "struct tomoyo_io_buffer".
  1578. */
  1579. static void tomoyo_read_query(struct tomoyo_io_buffer *head)
  1580. {
  1581. struct list_head *tmp;
  1582. int pos = 0;
  1583. int len = 0;
  1584. char *buf;
  1585. if (head->read_avail)
  1586. return;
  1587. if (head->read_buf) {
  1588. kfree(head->read_buf);
  1589. head->read_buf = NULL;
  1590. head->readbuf_size = 0;
  1591. }
  1592. spin_lock(&tomoyo_query_list_lock);
  1593. list_for_each(tmp, &tomoyo_query_list) {
  1594. struct tomoyo_query_entry *ptr
  1595. = list_entry(tmp, struct tomoyo_query_entry, list);
  1596. if (ptr->answer)
  1597. continue;
  1598. if (pos++ != head->read_step)
  1599. continue;
  1600. len = ptr->query_len;
  1601. break;
  1602. }
  1603. spin_unlock(&tomoyo_query_list_lock);
  1604. if (!len) {
  1605. head->read_step = 0;
  1606. return;
  1607. }
  1608. buf = kzalloc(len, GFP_NOFS);
  1609. if (!buf)
  1610. return;
  1611. pos = 0;
  1612. spin_lock(&tomoyo_query_list_lock);
  1613. list_for_each(tmp, &tomoyo_query_list) {
  1614. struct tomoyo_query_entry *ptr
  1615. = list_entry(tmp, struct tomoyo_query_entry, list);
  1616. if (ptr->answer)
  1617. continue;
  1618. if (pos++ != head->read_step)
  1619. continue;
  1620. /*
  1621. * Some query can be skipped because tomoyo_query_list
  1622. * can change, but I don't care.
  1623. */
  1624. if (len == ptr->query_len)
  1625. memmove(buf, ptr->query, len);
  1626. break;
  1627. }
  1628. spin_unlock(&tomoyo_query_list_lock);
  1629. if (buf[0]) {
  1630. head->read_avail = len;
  1631. head->readbuf_size = head->read_avail;
  1632. head->read_buf = buf;
  1633. head->read_step++;
  1634. } else {
  1635. kfree(buf);
  1636. }
  1637. }
  1638. /**
  1639. * tomoyo_write_answer - Write the supervisor's decision.
  1640. *
  1641. * @head: Pointer to "struct tomoyo_io_buffer".
  1642. *
  1643. * Returns 0 on success, -EINVAL otherwise.
  1644. */
  1645. static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
  1646. {
  1647. char *data = head->write_buf;
  1648. struct list_head *tmp;
  1649. unsigned int serial;
  1650. unsigned int answer;
  1651. spin_lock(&tomoyo_query_list_lock);
  1652. list_for_each(tmp, &tomoyo_query_list) {
  1653. struct tomoyo_query_entry *ptr
  1654. = list_entry(tmp, struct tomoyo_query_entry, list);
  1655. ptr->timer = 0;
  1656. }
  1657. spin_unlock(&tomoyo_query_list_lock);
  1658. if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
  1659. return -EINVAL;
  1660. spin_lock(&tomoyo_query_list_lock);
  1661. list_for_each(tmp, &tomoyo_query_list) {
  1662. struct tomoyo_query_entry *ptr
  1663. = list_entry(tmp, struct tomoyo_query_entry, list);
  1664. if (ptr->serial != serial)
  1665. continue;
  1666. if (!ptr->answer)
  1667. ptr->answer = answer;
  1668. break;
  1669. }
  1670. spin_unlock(&tomoyo_query_list_lock);
  1671. return 0;
  1672. }
  1673. /**
  1674. * tomoyo_read_version: Get version.
  1675. *
  1676. * @head: Pointer to "struct tomoyo_io_buffer".
  1677. *
  1678. * Returns version information.
  1679. */
  1680. static void tomoyo_read_version(struct tomoyo_io_buffer *head)
  1681. {
  1682. if (!head->read_eof) {
  1683. tomoyo_io_printf(head, "2.3.0-pre");
  1684. head->read_eof = true;
  1685. }
  1686. }
  1687. /**
  1688. * tomoyo_read_self_domain - Get the current process's domainname.
  1689. *
  1690. * @head: Pointer to "struct tomoyo_io_buffer".
  1691. *
  1692. * Returns the current process's domainname.
  1693. */
  1694. static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1695. {
  1696. if (!head->read_eof) {
  1697. /*
  1698. * tomoyo_domain()->domainname != NULL
  1699. * because every process belongs to a domain and
  1700. * the domain's name cannot be NULL.
  1701. */
  1702. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1703. head->read_eof = true;
  1704. }
  1705. }
  1706. /**
  1707. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1708. *
  1709. * @type: Type of interface.
  1710. * @file: Pointer to "struct file".
  1711. *
  1712. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1713. *
  1714. * Caller acquires tomoyo_read_lock().
  1715. */
  1716. int tomoyo_open_control(const u8 type, struct file *file)
  1717. {
  1718. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_NOFS);
  1719. if (!head)
  1720. return -ENOMEM;
  1721. mutex_init(&head->io_sem);
  1722. head->type = type;
  1723. switch (type) {
  1724. case TOMOYO_DOMAINPOLICY:
  1725. /* /sys/kernel/security/tomoyo/domain_policy */
  1726. head->write = tomoyo_write_domain_policy;
  1727. head->read = tomoyo_read_domain_policy;
  1728. break;
  1729. case TOMOYO_EXCEPTIONPOLICY:
  1730. /* /sys/kernel/security/tomoyo/exception_policy */
  1731. head->write = tomoyo_write_exception_policy;
  1732. head->read = tomoyo_read_exception_policy;
  1733. break;
  1734. case TOMOYO_SELFDOMAIN:
  1735. /* /sys/kernel/security/tomoyo/self_domain */
  1736. head->read = tomoyo_read_self_domain;
  1737. break;
  1738. case TOMOYO_DOMAIN_STATUS:
  1739. /* /sys/kernel/security/tomoyo/.domain_status */
  1740. head->write = tomoyo_write_domain_profile;
  1741. head->read = tomoyo_read_domain_profile;
  1742. break;
  1743. case TOMOYO_PROCESS_STATUS:
  1744. /* /sys/kernel/security/tomoyo/.process_status */
  1745. head->write = tomoyo_write_pid;
  1746. head->read = tomoyo_read_pid;
  1747. break;
  1748. case TOMOYO_VERSION:
  1749. /* /sys/kernel/security/tomoyo/version */
  1750. head->read = tomoyo_read_version;
  1751. head->readbuf_size = 128;
  1752. break;
  1753. case TOMOYO_MEMINFO:
  1754. /* /sys/kernel/security/tomoyo/meminfo */
  1755. head->write = tomoyo_write_memory_quota;
  1756. head->read = tomoyo_read_memory_counter;
  1757. head->readbuf_size = 512;
  1758. break;
  1759. case TOMOYO_PROFILE:
  1760. /* /sys/kernel/security/tomoyo/profile */
  1761. head->write = tomoyo_write_profile;
  1762. head->read = tomoyo_read_profile;
  1763. break;
  1764. case TOMOYO_QUERY: /* /sys/kernel/security/tomoyo/query */
  1765. head->poll = tomoyo_poll_query;
  1766. head->write = tomoyo_write_answer;
  1767. head->read = tomoyo_read_query;
  1768. break;
  1769. case TOMOYO_MANAGER:
  1770. /* /sys/kernel/security/tomoyo/manager */
  1771. head->write = tomoyo_write_manager_policy;
  1772. head->read = tomoyo_read_manager_policy;
  1773. break;
  1774. }
  1775. if (!(file->f_mode & FMODE_READ)) {
  1776. /*
  1777. * No need to allocate read_buf since it is not opened
  1778. * for reading.
  1779. */
  1780. head->read = NULL;
  1781. head->poll = NULL;
  1782. } else if (!head->poll) {
  1783. /* Don't allocate read_buf for poll() access. */
  1784. if (!head->readbuf_size)
  1785. head->readbuf_size = 4096 * 2;
  1786. head->read_buf = kzalloc(head->readbuf_size, GFP_NOFS);
  1787. if (!head->read_buf) {
  1788. kfree(head);
  1789. return -ENOMEM;
  1790. }
  1791. }
  1792. if (!(file->f_mode & FMODE_WRITE)) {
  1793. /*
  1794. * No need to allocate write_buf since it is not opened
  1795. * for writing.
  1796. */
  1797. head->write = NULL;
  1798. } else if (head->write) {
  1799. head->writebuf_size = 4096 * 2;
  1800. head->write_buf = kzalloc(head->writebuf_size, GFP_NOFS);
  1801. if (!head->write_buf) {
  1802. kfree(head->read_buf);
  1803. kfree(head);
  1804. return -ENOMEM;
  1805. }
  1806. }
  1807. if (type != TOMOYO_QUERY)
  1808. head->reader_idx = tomoyo_read_lock();
  1809. file->private_data = head;
  1810. /*
  1811. * Call the handler now if the file is
  1812. * /sys/kernel/security/tomoyo/self_domain
  1813. * so that the user can use
  1814. * cat < /sys/kernel/security/tomoyo/self_domain"
  1815. * to know the current process's domainname.
  1816. */
  1817. if (type == TOMOYO_SELFDOMAIN)
  1818. tomoyo_read_control(file, NULL, 0);
  1819. /*
  1820. * If the file is /sys/kernel/security/tomoyo/query , increment the
  1821. * observer counter.
  1822. * The obserber counter is used by tomoyo_supervisor() to see if
  1823. * there is some process monitoring /sys/kernel/security/tomoyo/query.
  1824. */
  1825. else if (type == TOMOYO_QUERY)
  1826. atomic_inc(&tomoyo_query_observers);
  1827. return 0;
  1828. }
  1829. /**
  1830. * tomoyo_poll_control - poll() for /sys/kernel/security/tomoyo/ interface.
  1831. *
  1832. * @file: Pointer to "struct file".
  1833. * @wait: Pointer to "poll_table".
  1834. *
  1835. * Waits for read readiness.
  1836. * /sys/kernel/security/tomoyo/query is handled by /usr/sbin/tomoyo-queryd .
  1837. */
  1838. int tomoyo_poll_control(struct file *file, poll_table *wait)
  1839. {
  1840. struct tomoyo_io_buffer *head = file->private_data;
  1841. if (!head->poll)
  1842. return -ENOSYS;
  1843. return head->poll(file, wait);
  1844. }
  1845. /**
  1846. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1847. *
  1848. * @file: Pointer to "struct file".
  1849. * @buffer: Poiner to buffer to write to.
  1850. * @buffer_len: Size of @buffer.
  1851. *
  1852. * Returns bytes read on success, negative value otherwise.
  1853. *
  1854. * Caller holds tomoyo_read_lock().
  1855. */
  1856. int tomoyo_read_control(struct file *file, char __user *buffer,
  1857. const int buffer_len)
  1858. {
  1859. int len = 0;
  1860. struct tomoyo_io_buffer *head = file->private_data;
  1861. char *cp;
  1862. if (!head->read)
  1863. return -ENOSYS;
  1864. if (mutex_lock_interruptible(&head->io_sem))
  1865. return -EINTR;
  1866. /* Call the policy handler. */
  1867. head->read(head);
  1868. if (len < 0)
  1869. goto out;
  1870. /* Write to buffer. */
  1871. len = head->read_avail;
  1872. if (len > buffer_len)
  1873. len = buffer_len;
  1874. if (!len)
  1875. goto out;
  1876. /* head->read_buf changes by some functions. */
  1877. cp = head->read_buf;
  1878. if (copy_to_user(buffer, cp, len)) {
  1879. len = -EFAULT;
  1880. goto out;
  1881. }
  1882. head->read_avail -= len;
  1883. memmove(cp, cp + len, head->read_avail);
  1884. out:
  1885. mutex_unlock(&head->io_sem);
  1886. return len;
  1887. }
  1888. /**
  1889. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1890. *
  1891. * @file: Pointer to "struct file".
  1892. * @buffer: Pointer to buffer to read from.
  1893. * @buffer_len: Size of @buffer.
  1894. *
  1895. * Returns @buffer_len on success, negative value otherwise.
  1896. *
  1897. * Caller holds tomoyo_read_lock().
  1898. */
  1899. int tomoyo_write_control(struct file *file, const char __user *buffer,
  1900. const int buffer_len)
  1901. {
  1902. struct tomoyo_io_buffer *head = file->private_data;
  1903. int error = buffer_len;
  1904. int avail_len = buffer_len;
  1905. char *cp0 = head->write_buf;
  1906. if (!head->write)
  1907. return -ENOSYS;
  1908. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1909. return -EFAULT;
  1910. /* Don't allow updating policies by non manager programs. */
  1911. if (head->write != tomoyo_write_pid &&
  1912. head->write != tomoyo_write_domain_policy &&
  1913. !tomoyo_policy_manager())
  1914. return -EPERM;
  1915. if (mutex_lock_interruptible(&head->io_sem))
  1916. return -EINTR;
  1917. /* Read a line and dispatch it to the policy handler. */
  1918. while (avail_len > 0) {
  1919. char c;
  1920. if (head->write_avail >= head->writebuf_size - 1) {
  1921. error = -ENOMEM;
  1922. break;
  1923. } else if (get_user(c, buffer)) {
  1924. error = -EFAULT;
  1925. break;
  1926. }
  1927. buffer++;
  1928. avail_len--;
  1929. cp0[head->write_avail++] = c;
  1930. if (c != '\n')
  1931. continue;
  1932. cp0[head->write_avail - 1] = '\0';
  1933. head->write_avail = 0;
  1934. tomoyo_normalize_line(cp0);
  1935. head->write(head);
  1936. }
  1937. mutex_unlock(&head->io_sem);
  1938. return error;
  1939. }
  1940. /**
  1941. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  1942. *
  1943. * @file: Pointer to "struct file".
  1944. *
  1945. * Releases memory and returns 0.
  1946. *
  1947. * Caller looses tomoyo_read_lock().
  1948. */
  1949. int tomoyo_close_control(struct file *file)
  1950. {
  1951. struct tomoyo_io_buffer *head = file->private_data;
  1952. const bool is_write = !!head->write_buf;
  1953. /*
  1954. * If the file is /sys/kernel/security/tomoyo/query , decrement the
  1955. * observer counter.
  1956. */
  1957. if (head->type == TOMOYO_QUERY)
  1958. atomic_dec(&tomoyo_query_observers);
  1959. else
  1960. tomoyo_read_unlock(head->reader_idx);
  1961. /* Release memory used for policy I/O. */
  1962. kfree(head->read_buf);
  1963. head->read_buf = NULL;
  1964. kfree(head->write_buf);
  1965. head->write_buf = NULL;
  1966. kfree(head);
  1967. head = NULL;
  1968. file->private_data = NULL;
  1969. if (is_write)
  1970. tomoyo_run_gc();
  1971. return 0;
  1972. }
  1973. /**
  1974. * tomoyo_check_profile - Check all profiles currently assigned to domains are defined.
  1975. */
  1976. void tomoyo_check_profile(void)
  1977. {
  1978. struct tomoyo_domain_info *domain;
  1979. const int idx = tomoyo_read_lock();
  1980. tomoyo_policy_loaded = true;
  1981. /* Check all profiles currently assigned to domains are defined. */
  1982. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1983. const u8 profile = domain->profile;
  1984. if (tomoyo_profile_ptr[profile])
  1985. continue;
  1986. panic("Profile %u (used by '%s') not defined.\n",
  1987. profile, domain->domainname->name);
  1988. }
  1989. tomoyo_read_unlock(idx);
  1990. if (tomoyo_profile_version != 20090903)
  1991. panic("Profile version %u is not supported.\n",
  1992. tomoyo_profile_version);
  1993. printk(KERN_INFO "TOMOYO: 2.3.0-pre 2010/06/03\n");
  1994. printk(KERN_INFO "Mandatory Access Control activated.\n");
  1995. }