common.c 50 KB

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