common.c 55 KB

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