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