common.c 53 KB

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