common.c 53 KB

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