common.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  1. /*
  2. * security/tomoyo/common.c
  3. *
  4. * Common functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0 2009/04/01
  9. *
  10. */
  11. #include <linux/uaccess.h>
  12. #include <linux/security.h>
  13. #include <linux/hardirq.h>
  14. #include "common.h"
  15. /* Lock for protecting policy. */
  16. DEFINE_MUTEX(tomoyo_policy_lock);
  17. /* Has loading policy done? */
  18. bool tomoyo_policy_loaded;
  19. /* String table for functionality that takes 4 modes. */
  20. static const char *tomoyo_mode_4[4] = {
  21. "disabled", "learning", "permissive", "enforcing"
  22. };
  23. /* String table for functionality that takes 2 modes. */
  24. static const char *tomoyo_mode_2[4] = {
  25. "disabled", "enabled", "enabled", "enabled"
  26. };
  27. /*
  28. * tomoyo_control_array is a static data which contains
  29. *
  30. * (1) functionality name used by /sys/kernel/security/tomoyo/profile .
  31. * (2) initial values for "struct tomoyo_profile".
  32. * (3) max values for "struct tomoyo_profile".
  33. */
  34. static struct {
  35. const char *keyword;
  36. unsigned int current_value;
  37. const unsigned int max_value;
  38. } tomoyo_control_array[TOMOYO_MAX_CONTROL_INDEX] = {
  39. [TOMOYO_MAC_FOR_FILE] = { "MAC_FOR_FILE", 0, 3 },
  40. [TOMOYO_MAX_ACCEPT_ENTRY] = { "MAX_ACCEPT_ENTRY", 2048, INT_MAX },
  41. [TOMOYO_VERBOSE] = { "TOMOYO_VERBOSE", 1, 1 },
  42. };
  43. /*
  44. * tomoyo_profile is a structure which is used for holding the mode of access
  45. * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
  46. * An administrator can define up to 256 profiles.
  47. * The ->profile of "struct tomoyo_domain_info" is used for remembering
  48. * the profile's number (0 - 255) assigned to that domain.
  49. */
  50. static struct tomoyo_profile {
  51. unsigned int value[TOMOYO_MAX_CONTROL_INDEX];
  52. const struct tomoyo_path_info *comment;
  53. } *tomoyo_profile_ptr[TOMOYO_MAX_PROFILES];
  54. /* Permit policy management by non-root user? */
  55. static bool tomoyo_manage_by_non_root;
  56. /* Utility functions. */
  57. /* Open operation for /sys/kernel/security/tomoyo/ interface. */
  58. static int tomoyo_open_control(const u8 type, struct file *file);
  59. /* Close /sys/kernel/security/tomoyo/ interface. */
  60. static int tomoyo_close_control(struct file *file);
  61. /* Read operation for /sys/kernel/security/tomoyo/ interface. */
  62. static int tomoyo_read_control(struct file *file, char __user *buffer,
  63. const int buffer_len);
  64. /* Write operation for /sys/kernel/security/tomoyo/ interface. */
  65. static int tomoyo_write_control(struct file *file, const char __user *buffer,
  66. const int buffer_len);
  67. /**
  68. * tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
  69. *
  70. * @str: Pointer to the string.
  71. *
  72. * Returns true if @str is a \ooo style octal value, false otherwise.
  73. *
  74. * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
  75. * This function verifies that \ooo is in valid range.
  76. */
  77. static inline bool tomoyo_is_byte_range(const char *str)
  78. {
  79. return *str >= '0' && *str++ <= '3' &&
  80. *str >= '0' && *str++ <= '7' &&
  81. *str >= '0' && *str <= '7';
  82. }
  83. /**
  84. * tomoyo_is_alphabet_char - Check whether the character is an alphabet.
  85. *
  86. * @c: The character to check.
  87. *
  88. * Returns true if @c is an alphabet character, false otherwise.
  89. */
  90. static inline bool tomoyo_is_alphabet_char(const char c)
  91. {
  92. return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
  93. }
  94. /**
  95. * tomoyo_make_byte - Make byte value from three octal characters.
  96. *
  97. * @c1: The first character.
  98. * @c2: The second character.
  99. * @c3: The third character.
  100. *
  101. * Returns byte value.
  102. */
  103. static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
  104. {
  105. return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
  106. }
  107. /**
  108. * tomoyo_str_starts - Check whether the given string starts with the given keyword.
  109. *
  110. * @src: Pointer to pointer to the string.
  111. * @find: Pointer to the keyword.
  112. *
  113. * Returns true if @src starts with @find, false otherwise.
  114. *
  115. * The @src is updated to point the first character after the @find
  116. * if @src starts with @find.
  117. */
  118. static bool tomoyo_str_starts(char **src, const char *find)
  119. {
  120. const int len = strlen(find);
  121. char *tmp = *src;
  122. if (strncmp(tmp, find, len))
  123. return false;
  124. tmp += len;
  125. *src = tmp;
  126. return true;
  127. }
  128. /**
  129. * tomoyo_normalize_line - Format string.
  130. *
  131. * @buffer: The line to normalize.
  132. *
  133. * Leading and trailing whitespaces are removed.
  134. * Multiple whitespaces are packed into single space.
  135. *
  136. * Returns nothing.
  137. */
  138. static void tomoyo_normalize_line(unsigned char *buffer)
  139. {
  140. unsigned char *sp = buffer;
  141. unsigned char *dp = buffer;
  142. bool first = true;
  143. while (tomoyo_is_invalid(*sp))
  144. sp++;
  145. while (*sp) {
  146. if (!first)
  147. *dp++ = ' ';
  148. first = false;
  149. while (tomoyo_is_valid(*sp))
  150. *dp++ = *sp++;
  151. while (tomoyo_is_invalid(*sp))
  152. sp++;
  153. }
  154. *dp = '\0';
  155. }
  156. /**
  157. * tomoyo_is_correct_path - Validate a pathname.
  158. * @filename: The pathname to check.
  159. * @start_type: Should the pathname start with '/'?
  160. * 1 = must / -1 = must not / 0 = don't care
  161. * @pattern_type: Can the pathname contain a wildcard?
  162. * 1 = must / -1 = must not / 0 = don't care
  163. * @end_type: Should the pathname end with '/'?
  164. * 1 = must / -1 = must not / 0 = don't care
  165. *
  166. * Check whether the given filename follows the naming rules.
  167. * Returns true if @filename follows the naming rules, false otherwise.
  168. */
  169. bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
  170. const s8 pattern_type, const s8 end_type)
  171. {
  172. const char *const start = filename;
  173. bool in_repetition = false;
  174. bool contains_pattern = false;
  175. unsigned char c;
  176. unsigned char d;
  177. unsigned char e;
  178. if (!filename)
  179. goto out;
  180. c = *filename;
  181. if (start_type == 1) { /* Must start with '/' */
  182. if (c != '/')
  183. goto out;
  184. } else if (start_type == -1) { /* Must not start with '/' */
  185. if (c == '/')
  186. goto out;
  187. }
  188. if (c)
  189. c = *(filename + strlen(filename) - 1);
  190. if (end_type == 1) { /* Must end with '/' */
  191. if (c != '/')
  192. goto out;
  193. } else if (end_type == -1) { /* Must not end with '/' */
  194. if (c == '/')
  195. goto out;
  196. }
  197. while (1) {
  198. c = *filename++;
  199. if (!c)
  200. break;
  201. if (c == '\\') {
  202. c = *filename++;
  203. switch (c) {
  204. case '\\': /* "\\" */
  205. continue;
  206. case '$': /* "\$" */
  207. case '+': /* "\+" */
  208. case '?': /* "\?" */
  209. case '*': /* "\*" */
  210. case '@': /* "\@" */
  211. case 'x': /* "\x" */
  212. case 'X': /* "\X" */
  213. case 'a': /* "\a" */
  214. case 'A': /* "\A" */
  215. case '-': /* "\-" */
  216. if (pattern_type == -1)
  217. break; /* Must not contain pattern */
  218. contains_pattern = true;
  219. continue;
  220. case '{': /* "/\{" */
  221. if (filename - 3 < start ||
  222. *(filename - 3) != '/')
  223. break;
  224. if (pattern_type == -1)
  225. break; /* Must not contain pattern */
  226. contains_pattern = true;
  227. in_repetition = true;
  228. continue;
  229. case '}': /* "\}/" */
  230. if (*filename != '/')
  231. break;
  232. if (!in_repetition)
  233. break;
  234. in_repetition = false;
  235. continue;
  236. case '0': /* "\ooo" */
  237. case '1':
  238. case '2':
  239. case '3':
  240. d = *filename++;
  241. if (d < '0' || d > '7')
  242. break;
  243. e = *filename++;
  244. if (e < '0' || e > '7')
  245. break;
  246. c = tomoyo_make_byte(c, d, e);
  247. if (tomoyo_is_invalid(c))
  248. continue; /* pattern is not \000 */
  249. }
  250. goto out;
  251. } else if (in_repetition && c == '/') {
  252. goto out;
  253. } else if (tomoyo_is_invalid(c)) {
  254. goto out;
  255. }
  256. }
  257. if (pattern_type == 1) { /* Must contain pattern */
  258. if (!contains_pattern)
  259. goto out;
  260. }
  261. if (in_repetition)
  262. goto out;
  263. return true;
  264. out:
  265. return false;
  266. }
  267. /**
  268. * tomoyo_is_correct_domain - Check whether the given domainname follows the naming rules.
  269. * @domainname: The domainname to check.
  270. *
  271. * Returns true if @domainname follows the naming rules, false otherwise.
  272. */
  273. bool tomoyo_is_correct_domain(const unsigned char *domainname)
  274. {
  275. unsigned char c;
  276. unsigned char d;
  277. unsigned char e;
  278. if (!domainname || strncmp(domainname, TOMOYO_ROOT_NAME,
  279. TOMOYO_ROOT_NAME_LEN))
  280. goto out;
  281. domainname += TOMOYO_ROOT_NAME_LEN;
  282. if (!*domainname)
  283. return true;
  284. do {
  285. if (*domainname++ != ' ')
  286. goto out;
  287. if (*domainname++ != '/')
  288. goto out;
  289. while ((c = *domainname) != '\0' && c != ' ') {
  290. domainname++;
  291. if (c == '\\') {
  292. c = *domainname++;
  293. switch ((c)) {
  294. case '\\': /* "\\" */
  295. continue;
  296. case '0': /* "\ooo" */
  297. case '1':
  298. case '2':
  299. case '3':
  300. d = *domainname++;
  301. if (d < '0' || d > '7')
  302. break;
  303. e = *domainname++;
  304. if (e < '0' || e > '7')
  305. break;
  306. c = tomoyo_make_byte(c, d, e);
  307. if (tomoyo_is_invalid(c))
  308. /* pattern is not \000 */
  309. continue;
  310. }
  311. goto out;
  312. } else if (tomoyo_is_invalid(c)) {
  313. goto out;
  314. }
  315. }
  316. } while (*domainname);
  317. return true;
  318. out:
  319. return false;
  320. }
  321. /**
  322. * tomoyo_is_domain_def - Check whether the given token can be a domainname.
  323. *
  324. * @buffer: The token to check.
  325. *
  326. * Returns true if @buffer possibly be a domainname, false otherwise.
  327. */
  328. bool tomoyo_is_domain_def(const unsigned char *buffer)
  329. {
  330. return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
  331. }
  332. /**
  333. * tomoyo_find_domain - Find a domain by the given name.
  334. *
  335. * @domainname: The domainname to find.
  336. *
  337. * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
  338. *
  339. * Caller holds tomoyo_read_lock().
  340. */
  341. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
  342. {
  343. struct tomoyo_domain_info *domain;
  344. struct tomoyo_path_info name;
  345. name.name = domainname;
  346. tomoyo_fill_path_info(&name);
  347. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  348. if (!domain->is_deleted &&
  349. !tomoyo_pathcmp(&name, domain->domainname))
  350. return domain;
  351. }
  352. return NULL;
  353. }
  354. /**
  355. * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
  356. *
  357. * @filename: The string to evaluate.
  358. *
  359. * Returns the initial length without a pattern in @filename.
  360. */
  361. static int tomoyo_const_part_length(const char *filename)
  362. {
  363. char c;
  364. int len = 0;
  365. if (!filename)
  366. return 0;
  367. while ((c = *filename++) != '\0') {
  368. if (c != '\\') {
  369. len++;
  370. continue;
  371. }
  372. c = *filename++;
  373. switch (c) {
  374. case '\\': /* "\\" */
  375. len += 2;
  376. continue;
  377. case '0': /* "\ooo" */
  378. case '1':
  379. case '2':
  380. case '3':
  381. c = *filename++;
  382. if (c < '0' || c > '7')
  383. break;
  384. c = *filename++;
  385. if (c < '0' || c > '7')
  386. break;
  387. len += 4;
  388. continue;
  389. }
  390. break;
  391. }
  392. return len;
  393. }
  394. /**
  395. * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
  396. *
  397. * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
  398. *
  399. * The caller sets "struct tomoyo_path_info"->name.
  400. */
  401. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
  402. {
  403. const char *name = ptr->name;
  404. const int len = strlen(name);
  405. ptr->const_len = tomoyo_const_part_length(name);
  406. ptr->is_dir = len && (name[len - 1] == '/');
  407. ptr->is_patterned = (ptr->const_len < len);
  408. ptr->hash = full_name_hash(name, len);
  409. }
  410. /**
  411. * tomoyo_file_matches_pattern2 - Pattern matching without '/' character
  412. * and "\-" pattern.
  413. *
  414. * @filename: The start of string to check.
  415. * @filename_end: The end of string to check.
  416. * @pattern: The start of pattern to compare.
  417. * @pattern_end: The end of pattern to compare.
  418. *
  419. * Returns true if @filename matches @pattern, false otherwise.
  420. */
  421. static bool tomoyo_file_matches_pattern2(const char *filename,
  422. const char *filename_end,
  423. const char *pattern,
  424. const char *pattern_end)
  425. {
  426. while (filename < filename_end && pattern < pattern_end) {
  427. char c;
  428. if (*pattern != '\\') {
  429. if (*filename++ != *pattern++)
  430. return false;
  431. continue;
  432. }
  433. c = *filename;
  434. pattern++;
  435. switch (*pattern) {
  436. int i;
  437. int j;
  438. case '?':
  439. if (c == '/') {
  440. return false;
  441. } else if (c == '\\') {
  442. if (filename[1] == '\\')
  443. filename++;
  444. else if (tomoyo_is_byte_range(filename + 1))
  445. filename += 3;
  446. else
  447. return false;
  448. }
  449. break;
  450. case '\\':
  451. if (c != '\\')
  452. return false;
  453. if (*++filename != '\\')
  454. return false;
  455. break;
  456. case '+':
  457. if (!isdigit(c))
  458. return false;
  459. break;
  460. case 'x':
  461. if (!isxdigit(c))
  462. return false;
  463. break;
  464. case 'a':
  465. if (!tomoyo_is_alphabet_char(c))
  466. return false;
  467. break;
  468. case '0':
  469. case '1':
  470. case '2':
  471. case '3':
  472. if (c == '\\' && tomoyo_is_byte_range(filename + 1)
  473. && strncmp(filename + 1, pattern, 3) == 0) {
  474. filename += 3;
  475. pattern += 2;
  476. break;
  477. }
  478. return false; /* Not matched. */
  479. case '*':
  480. case '@':
  481. for (i = 0; i <= filename_end - filename; i++) {
  482. if (tomoyo_file_matches_pattern2(
  483. filename + i, filename_end,
  484. pattern + 1, pattern_end))
  485. return true;
  486. c = filename[i];
  487. if (c == '.' && *pattern == '@')
  488. break;
  489. if (c != '\\')
  490. continue;
  491. if (filename[i + 1] == '\\')
  492. i++;
  493. else if (tomoyo_is_byte_range(filename + i + 1))
  494. i += 3;
  495. else
  496. break; /* Bad pattern. */
  497. }
  498. return false; /* Not matched. */
  499. default:
  500. j = 0;
  501. c = *pattern;
  502. if (c == '$') {
  503. while (isdigit(filename[j]))
  504. j++;
  505. } else if (c == 'X') {
  506. while (isxdigit(filename[j]))
  507. j++;
  508. } else if (c == 'A') {
  509. while (tomoyo_is_alphabet_char(filename[j]))
  510. j++;
  511. }
  512. for (i = 1; i <= j; i++) {
  513. if (tomoyo_file_matches_pattern2(
  514. filename + i, filename_end,
  515. pattern + 1, pattern_end))
  516. return true;
  517. }
  518. return false; /* Not matched or bad pattern. */
  519. }
  520. filename++;
  521. pattern++;
  522. }
  523. while (*pattern == '\\' &&
  524. (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
  525. pattern += 2;
  526. return filename == filename_end && pattern == pattern_end;
  527. }
  528. /**
  529. * tomoyo_file_matches_pattern - Pattern matching without without '/' character.
  530. *
  531. * @filename: The start of string to check.
  532. * @filename_end: The end of string to check.
  533. * @pattern: The start of pattern to compare.
  534. * @pattern_end: The end of pattern to compare.
  535. *
  536. * Returns true if @filename matches @pattern, false otherwise.
  537. */
  538. static bool tomoyo_file_matches_pattern(const char *filename,
  539. const char *filename_end,
  540. const char *pattern,
  541. const char *pattern_end)
  542. {
  543. const char *pattern_start = pattern;
  544. bool first = true;
  545. bool result;
  546. while (pattern < pattern_end - 1) {
  547. /* Split at "\-" pattern. */
  548. if (*pattern++ != '\\' || *pattern++ != '-')
  549. continue;
  550. result = tomoyo_file_matches_pattern2(filename,
  551. filename_end,
  552. pattern_start,
  553. pattern - 2);
  554. if (first)
  555. result = !result;
  556. if (result)
  557. return false;
  558. first = false;
  559. pattern_start = pattern;
  560. }
  561. result = tomoyo_file_matches_pattern2(filename, filename_end,
  562. pattern_start, pattern_end);
  563. return first ? result : !result;
  564. }
  565. /**
  566. * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
  567. *
  568. * @f: The start of string to check.
  569. * @p: The start of pattern to compare.
  570. *
  571. * Returns true if @f matches @p, false otherwise.
  572. */
  573. static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
  574. {
  575. const char *f_delimiter;
  576. const char *p_delimiter;
  577. while (*f && *p) {
  578. f_delimiter = strchr(f, '/');
  579. if (!f_delimiter)
  580. f_delimiter = f + strlen(f);
  581. p_delimiter = strchr(p, '/');
  582. if (!p_delimiter)
  583. p_delimiter = p + strlen(p);
  584. if (*p == '\\' && *(p + 1) == '{')
  585. goto recursive;
  586. if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
  587. p_delimiter))
  588. return false;
  589. f = f_delimiter;
  590. if (*f)
  591. f++;
  592. p = p_delimiter;
  593. if (*p)
  594. p++;
  595. }
  596. /* Ignore trailing "\*" and "\@" in @pattern. */
  597. while (*p == '\\' &&
  598. (*(p + 1) == '*' || *(p + 1) == '@'))
  599. p += 2;
  600. return !*f && !*p;
  601. recursive:
  602. /*
  603. * The "\{" pattern is permitted only after '/' character.
  604. * This guarantees that below "*(p - 1)" is safe.
  605. * Also, the "\}" pattern is permitted only before '/' character
  606. * so that "\{" + "\}" pair will not break the "\-" operator.
  607. */
  608. if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
  609. *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
  610. return false; /* Bad pattern. */
  611. do {
  612. /* Compare current component with pattern. */
  613. if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
  614. p_delimiter - 2))
  615. break;
  616. /* Proceed to next component. */
  617. f = f_delimiter;
  618. if (!*f)
  619. break;
  620. f++;
  621. /* Continue comparison. */
  622. if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
  623. return true;
  624. f_delimiter = strchr(f, '/');
  625. } while (f_delimiter);
  626. return false; /* Not matched. */
  627. }
  628. /**
  629. * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
  630. *
  631. * @filename: The filename to check.
  632. * @pattern: The pattern to compare.
  633. *
  634. * Returns true if matches, false otherwise.
  635. *
  636. * The following patterns are available.
  637. * \\ \ itself.
  638. * \ooo Octal representation of a byte.
  639. * \* Zero or more repetitions of characters other than '/'.
  640. * \@ Zero or more repetitions of characters other than '/' or '.'.
  641. * \? 1 byte character other than '/'.
  642. * \$ One or more repetitions of decimal digits.
  643. * \+ 1 decimal digit.
  644. * \X One or more repetitions of hexadecimal digits.
  645. * \x 1 hexadecimal digit.
  646. * \A One or more repetitions of alphabet characters.
  647. * \a 1 alphabet character.
  648. *
  649. * \- Subtraction operator.
  650. *
  651. * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
  652. * /dir/dir/dir/ ).
  653. */
  654. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  655. const struct tomoyo_path_info *pattern)
  656. {
  657. const char *f = filename->name;
  658. const char *p = pattern->name;
  659. const int len = pattern->const_len;
  660. /* If @pattern doesn't contain pattern, I can use strcmp(). */
  661. if (!pattern->is_patterned)
  662. return !tomoyo_pathcmp(filename, pattern);
  663. /* Don't compare directory and non-directory. */
  664. if (filename->is_dir != pattern->is_dir)
  665. return false;
  666. /* Compare the initial length without patterns. */
  667. if (strncmp(f, p, len))
  668. return false;
  669. f += len;
  670. p += len;
  671. return tomoyo_path_matches_pattern2(f, p);
  672. }
  673. /**
  674. * tomoyo_io_printf - Transactional printf() to "struct tomoyo_io_buffer" structure.
  675. *
  676. * @head: Pointer to "struct tomoyo_io_buffer".
  677. * @fmt: The printf()'s format string, followed by parameters.
  678. *
  679. * Returns true if output was written, false otherwise.
  680. *
  681. * The snprintf() will truncate, but tomoyo_io_printf() won't.
  682. */
  683. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  684. {
  685. va_list args;
  686. int len;
  687. int pos = head->read_avail;
  688. int size = head->readbuf_size - pos;
  689. if (size <= 0)
  690. return false;
  691. va_start(args, fmt);
  692. len = vsnprintf(head->read_buf + pos, size, fmt, args);
  693. va_end(args);
  694. if (pos + len >= head->readbuf_size)
  695. return false;
  696. head->read_avail += len;
  697. return true;
  698. }
  699. /**
  700. * tomoyo_get_exe - Get tomoyo_realpath() of current process.
  701. *
  702. * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
  703. *
  704. * This function uses kzalloc(), so the caller must call kfree()
  705. * if this function didn't return NULL.
  706. */
  707. static const char *tomoyo_get_exe(void)
  708. {
  709. struct mm_struct *mm = current->mm;
  710. struct vm_area_struct *vma;
  711. const char *cp = NULL;
  712. if (!mm)
  713. return NULL;
  714. down_read(&mm->mmap_sem);
  715. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  716. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
  717. cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
  718. break;
  719. }
  720. }
  721. up_read(&mm->mmap_sem);
  722. return cp;
  723. }
  724. /**
  725. * tomoyo_get_msg - Get warning message.
  726. *
  727. * @is_enforce: Is it enforcing mode?
  728. *
  729. * Returns "ERROR" or "WARNING".
  730. */
  731. const char *tomoyo_get_msg(const bool is_enforce)
  732. {
  733. if (is_enforce)
  734. return "ERROR";
  735. else
  736. return "WARNING";
  737. }
  738. /**
  739. * tomoyo_check_flags - Check mode for specified functionality.
  740. *
  741. * @domain: Pointer to "struct tomoyo_domain_info".
  742. * @index: The functionality to check mode.
  743. *
  744. * TOMOYO checks only process context.
  745. * This code disables TOMOYO's enforcement in case the function is called from
  746. * interrupt context.
  747. */
  748. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  749. const u8 index)
  750. {
  751. const u8 profile = domain->profile;
  752. if (WARN_ON(in_interrupt()))
  753. return 0;
  754. return tomoyo_policy_loaded && index < TOMOYO_MAX_CONTROL_INDEX
  755. #if TOMOYO_MAX_PROFILES != 256
  756. && profile < TOMOYO_MAX_PROFILES
  757. #endif
  758. && tomoyo_profile_ptr[profile] ?
  759. tomoyo_profile_ptr[profile]->value[index] : 0;
  760. }
  761. /**
  762. * tomoyo_verbose_mode - Check whether TOMOYO is verbose mode.
  763. *
  764. * @domain: Pointer to "struct tomoyo_domain_info".
  765. *
  766. * Returns true if domain policy violation warning should be printed to
  767. * console.
  768. */
  769. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain)
  770. {
  771. return tomoyo_check_flags(domain, TOMOYO_VERBOSE) != 0;
  772. }
  773. /**
  774. * tomoyo_domain_quota_is_ok - Check for domain's quota.
  775. *
  776. * @domain: Pointer to "struct tomoyo_domain_info".
  777. *
  778. * Returns true if the domain is not exceeded quota, false otherwise.
  779. *
  780. * Caller holds tomoyo_read_lock().
  781. */
  782. bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain)
  783. {
  784. unsigned int count = 0;
  785. struct tomoyo_acl_info *ptr;
  786. if (!domain)
  787. return true;
  788. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  789. switch (ptr->type) {
  790. struct tomoyo_path_acl *acl;
  791. u32 perm;
  792. u8 i;
  793. case TOMOYO_TYPE_PATH_ACL:
  794. acl = container_of(ptr, struct tomoyo_path_acl, head);
  795. perm = acl->perm | (((u32) acl->perm_high) << 16);
  796. for (i = 0; i < TOMOYO_MAX_PATH_OPERATION; i++)
  797. if (perm & (1 << i))
  798. count++;
  799. if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
  800. count -= 2;
  801. break;
  802. case TOMOYO_TYPE_PATH2_ACL:
  803. perm = container_of(ptr, struct tomoyo_path2_acl, head)
  804. ->perm;
  805. for (i = 0; i < TOMOYO_MAX_PATH2_OPERATION; i++)
  806. if (perm & (1 << i))
  807. count++;
  808. break;
  809. }
  810. }
  811. if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
  812. return true;
  813. if (!domain->quota_warned) {
  814. domain->quota_warned = true;
  815. printk(KERN_WARNING "TOMOYO-WARNING: "
  816. "Domain '%s' has so many ACLs to hold. "
  817. "Stopped learning mode.\n", domain->domainname->name);
  818. }
  819. return false;
  820. }
  821. /**
  822. * tomoyo_find_or_assign_new_profile - Create a new profile.
  823. *
  824. * @profile: Profile number to create.
  825. *
  826. * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
  827. */
  828. static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
  829. int profile)
  830. {
  831. static DEFINE_MUTEX(lock);
  832. struct tomoyo_profile *ptr = NULL;
  833. int i;
  834. if (profile >= TOMOYO_MAX_PROFILES)
  835. return NULL;
  836. mutex_lock(&lock);
  837. ptr = tomoyo_profile_ptr[profile];
  838. if (ptr)
  839. goto ok;
  840. ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
  841. if (!tomoyo_memory_ok(ptr)) {
  842. kfree(ptr);
  843. goto ok;
  844. }
  845. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
  846. ptr->value[i] = tomoyo_control_array[i].current_value;
  847. mb(); /* Avoid out-of-order execution. */
  848. tomoyo_profile_ptr[profile] = ptr;
  849. ok:
  850. mutex_unlock(&lock);
  851. return ptr;
  852. }
  853. /**
  854. * tomoyo_write_profile - Write to profile table.
  855. *
  856. * @head: Pointer to "struct tomoyo_io_buffer".
  857. *
  858. * Returns 0 on success, negative value otherwise.
  859. */
  860. static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
  861. {
  862. char *data = head->write_buf;
  863. unsigned int i;
  864. unsigned int value;
  865. char *cp;
  866. struct tomoyo_profile *profile;
  867. unsigned long num;
  868. cp = strchr(data, '-');
  869. if (cp)
  870. *cp = '\0';
  871. if (strict_strtoul(data, 10, &num))
  872. return -EINVAL;
  873. if (cp)
  874. data = cp + 1;
  875. profile = tomoyo_find_or_assign_new_profile(num);
  876. if (!profile)
  877. return -EINVAL;
  878. cp = strchr(data, '=');
  879. if (!cp)
  880. return -EINVAL;
  881. *cp = '\0';
  882. if (!strcmp(data, "COMMENT")) {
  883. const struct tomoyo_path_info *old_comment = profile->comment;
  884. profile->comment = tomoyo_get_name(cp + 1);
  885. tomoyo_put_name(old_comment);
  886. return 0;
  887. }
  888. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
  889. if (strcmp(data, tomoyo_control_array[i].keyword))
  890. continue;
  891. if (sscanf(cp + 1, "%u", &value) != 1) {
  892. int j;
  893. const char **modes;
  894. switch (i) {
  895. case TOMOYO_VERBOSE:
  896. modes = tomoyo_mode_2;
  897. break;
  898. default:
  899. modes = tomoyo_mode_4;
  900. break;
  901. }
  902. for (j = 0; j < 4; j++) {
  903. if (strcmp(cp + 1, modes[j]))
  904. continue;
  905. value = j;
  906. break;
  907. }
  908. if (j == 4)
  909. return -EINVAL;
  910. } else if (value > tomoyo_control_array[i].max_value) {
  911. value = tomoyo_control_array[i].max_value;
  912. }
  913. profile->value[i] = value;
  914. return 0;
  915. }
  916. return -EINVAL;
  917. }
  918. /**
  919. * tomoyo_read_profile - Read from profile table.
  920. *
  921. * @head: Pointer to "struct tomoyo_io_buffer".
  922. *
  923. * Returns 0.
  924. */
  925. static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
  926. {
  927. static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
  928. int step;
  929. if (head->read_eof)
  930. return 0;
  931. for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
  932. step++) {
  933. const u8 index = step / total;
  934. u8 type = step % total;
  935. const struct tomoyo_profile *profile
  936. = tomoyo_profile_ptr[index];
  937. head->read_step = step;
  938. if (!profile)
  939. continue;
  940. if (!type) { /* Print profile' comment tag. */
  941. if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
  942. index, profile->comment ?
  943. profile->comment->name : ""))
  944. break;
  945. continue;
  946. }
  947. type--;
  948. if (type < TOMOYO_MAX_CONTROL_INDEX) {
  949. const unsigned int value = profile->value[type];
  950. const char **modes = NULL;
  951. const char *keyword
  952. = tomoyo_control_array[type].keyword;
  953. switch (tomoyo_control_array[type].max_value) {
  954. case 3:
  955. modes = tomoyo_mode_4;
  956. break;
  957. case 1:
  958. modes = tomoyo_mode_2;
  959. break;
  960. }
  961. if (modes) {
  962. if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
  963. keyword, modes[value]))
  964. break;
  965. } else {
  966. if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
  967. keyword, value))
  968. break;
  969. }
  970. }
  971. }
  972. if (step == TOMOYO_MAX_PROFILES * total)
  973. head->read_eof = true;
  974. return 0;
  975. }
  976. /*
  977. * tomoyo_policy_manager_list is used for holding list of domainnames or
  978. * programs which are permitted to modify configuration via
  979. * /sys/kernel/security/tomoyo/ interface.
  980. *
  981. * An entry is added by
  982. *
  983. * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
  984. * /sys/kernel/security/tomoyo/manager
  985. * (if you want to specify by a domainname)
  986. *
  987. * or
  988. *
  989. * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
  990. * (if you want to specify by a program's location)
  991. *
  992. * and is deleted by
  993. *
  994. * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
  995. * /sys/kernel/security/tomoyo/manager
  996. *
  997. * or
  998. *
  999. * # echo 'delete /usr/lib/ccs/editpolicy' > \
  1000. * /sys/kernel/security/tomoyo/manager
  1001. *
  1002. * and all entries are retrieved by
  1003. *
  1004. * # cat /sys/kernel/security/tomoyo/manager
  1005. */
  1006. LIST_HEAD(tomoyo_policy_manager_list);
  1007. /**
  1008. * tomoyo_update_manager_entry - Add a manager entry.
  1009. *
  1010. * @manager: The path to manager or the domainnamme.
  1011. * @is_delete: True if it is a delete request.
  1012. *
  1013. * Returns 0 on success, negative value otherwise.
  1014. *
  1015. * Caller holds tomoyo_read_lock().
  1016. */
  1017. static int tomoyo_update_manager_entry(const char *manager,
  1018. const bool is_delete)
  1019. {
  1020. struct tomoyo_policy_manager_entry *entry = NULL;
  1021. struct tomoyo_policy_manager_entry *ptr;
  1022. const struct tomoyo_path_info *saved_manager;
  1023. int error = is_delete ? -ENOENT : -ENOMEM;
  1024. bool is_domain = false;
  1025. if (tomoyo_is_domain_def(manager)) {
  1026. if (!tomoyo_is_correct_domain(manager))
  1027. return -EINVAL;
  1028. is_domain = true;
  1029. } else {
  1030. if (!tomoyo_is_correct_path(manager, 1, -1, -1))
  1031. return -EINVAL;
  1032. }
  1033. saved_manager = tomoyo_get_name(manager);
  1034. if (!saved_manager)
  1035. return -ENOMEM;
  1036. if (!is_delete)
  1037. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1038. mutex_lock(&tomoyo_policy_lock);
  1039. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1040. if (ptr->manager != saved_manager)
  1041. continue;
  1042. ptr->is_deleted = is_delete;
  1043. error = 0;
  1044. break;
  1045. }
  1046. if (!is_delete && error && tomoyo_memory_ok(entry)) {
  1047. entry->manager = saved_manager;
  1048. saved_manager = NULL;
  1049. entry->is_domain = is_domain;
  1050. list_add_tail_rcu(&entry->list, &tomoyo_policy_manager_list);
  1051. entry = NULL;
  1052. error = 0;
  1053. }
  1054. mutex_unlock(&tomoyo_policy_lock);
  1055. tomoyo_put_name(saved_manager);
  1056. kfree(entry);
  1057. return error;
  1058. }
  1059. /**
  1060. * tomoyo_write_manager_policy - Write manager policy.
  1061. *
  1062. * @head: Pointer to "struct tomoyo_io_buffer".
  1063. *
  1064. * Returns 0 on success, negative value otherwise.
  1065. *
  1066. * Caller holds tomoyo_read_lock().
  1067. */
  1068. static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
  1069. {
  1070. char *data = head->write_buf;
  1071. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1072. if (!strcmp(data, "manage_by_non_root")) {
  1073. tomoyo_manage_by_non_root = !is_delete;
  1074. return 0;
  1075. }
  1076. return tomoyo_update_manager_entry(data, is_delete);
  1077. }
  1078. /**
  1079. * tomoyo_read_manager_policy - Read manager policy.
  1080. *
  1081. * @head: Pointer to "struct tomoyo_io_buffer".
  1082. *
  1083. * Returns 0.
  1084. *
  1085. * Caller holds tomoyo_read_lock().
  1086. */
  1087. static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
  1088. {
  1089. struct list_head *pos;
  1090. bool done = true;
  1091. if (head->read_eof)
  1092. return 0;
  1093. list_for_each_cookie(pos, head->read_var2,
  1094. &tomoyo_policy_manager_list) {
  1095. struct tomoyo_policy_manager_entry *ptr;
  1096. ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
  1097. list);
  1098. if (ptr->is_deleted)
  1099. continue;
  1100. done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
  1101. if (!done)
  1102. break;
  1103. }
  1104. head->read_eof = done;
  1105. return 0;
  1106. }
  1107. /**
  1108. * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
  1109. *
  1110. * Returns true if the current process is permitted to modify policy
  1111. * via /sys/kernel/security/tomoyo/ interface.
  1112. *
  1113. * Caller holds tomoyo_read_lock().
  1114. */
  1115. static bool tomoyo_is_policy_manager(void)
  1116. {
  1117. struct tomoyo_policy_manager_entry *ptr;
  1118. const char *exe;
  1119. const struct task_struct *task = current;
  1120. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  1121. bool found = false;
  1122. if (!tomoyo_policy_loaded)
  1123. return true;
  1124. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  1125. return false;
  1126. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1127. if (!ptr->is_deleted && ptr->is_domain
  1128. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  1129. found = true;
  1130. break;
  1131. }
  1132. }
  1133. if (found)
  1134. return true;
  1135. exe = tomoyo_get_exe();
  1136. if (!exe)
  1137. return false;
  1138. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1139. if (!ptr->is_deleted && !ptr->is_domain
  1140. && !strcmp(exe, ptr->manager->name)) {
  1141. found = true;
  1142. break;
  1143. }
  1144. }
  1145. if (!found) { /* Reduce error messages. */
  1146. static pid_t last_pid;
  1147. const pid_t pid = current->pid;
  1148. if (last_pid != pid) {
  1149. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  1150. "update policies.\n", domainname->name, exe);
  1151. last_pid = pid;
  1152. }
  1153. }
  1154. kfree(exe);
  1155. return found;
  1156. }
  1157. /**
  1158. * tomoyo_is_select_one - Parse select command.
  1159. *
  1160. * @head: Pointer to "struct tomoyo_io_buffer".
  1161. * @data: String to parse.
  1162. *
  1163. * Returns true on success, false otherwise.
  1164. *
  1165. * Caller holds tomoyo_read_lock().
  1166. */
  1167. static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
  1168. const char *data)
  1169. {
  1170. unsigned int pid;
  1171. struct tomoyo_domain_info *domain = NULL;
  1172. if (sscanf(data, "pid=%u", &pid) == 1) {
  1173. struct task_struct *p;
  1174. rcu_read_lock();
  1175. read_lock(&tasklist_lock);
  1176. p = find_task_by_vpid(pid);
  1177. if (p)
  1178. domain = tomoyo_real_domain(p);
  1179. read_unlock(&tasklist_lock);
  1180. rcu_read_unlock();
  1181. } else if (!strncmp(data, "domain=", 7)) {
  1182. if (tomoyo_is_domain_def(data + 7))
  1183. domain = tomoyo_find_domain(data + 7);
  1184. } else
  1185. return false;
  1186. head->write_var1 = domain;
  1187. /* Accessing read_buf is safe because head->io_sem is held. */
  1188. if (!head->read_buf)
  1189. return true; /* Do nothing if open(O_WRONLY). */
  1190. head->read_avail = 0;
  1191. tomoyo_io_printf(head, "# select %s\n", data);
  1192. head->read_single_domain = true;
  1193. head->read_eof = !domain;
  1194. if (domain) {
  1195. struct tomoyo_domain_info *d;
  1196. head->read_var1 = NULL;
  1197. list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
  1198. if (d == domain)
  1199. break;
  1200. head->read_var1 = &d->list;
  1201. }
  1202. head->read_var2 = NULL;
  1203. head->read_bit = 0;
  1204. head->read_step = 0;
  1205. if (domain->is_deleted)
  1206. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  1207. }
  1208. return true;
  1209. }
  1210. /**
  1211. * tomoyo_delete_domain - Delete a domain.
  1212. *
  1213. * @domainname: The name of domain.
  1214. *
  1215. * Returns 0.
  1216. *
  1217. * Caller holds tomoyo_read_lock().
  1218. */
  1219. static int tomoyo_delete_domain(char *domainname)
  1220. {
  1221. struct tomoyo_domain_info *domain;
  1222. struct tomoyo_path_info name;
  1223. name.name = domainname;
  1224. tomoyo_fill_path_info(&name);
  1225. mutex_lock(&tomoyo_policy_lock);
  1226. /* Is there an active domain? */
  1227. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1228. /* Never delete tomoyo_kernel_domain */
  1229. if (domain == &tomoyo_kernel_domain)
  1230. continue;
  1231. if (domain->is_deleted ||
  1232. tomoyo_pathcmp(domain->domainname, &name))
  1233. continue;
  1234. domain->is_deleted = true;
  1235. break;
  1236. }
  1237. mutex_unlock(&tomoyo_policy_lock);
  1238. return 0;
  1239. }
  1240. /**
  1241. * tomoyo_write_domain_policy - Write domain policy.
  1242. *
  1243. * @head: Pointer to "struct tomoyo_io_buffer".
  1244. *
  1245. * Returns 0 on success, negative value otherwise.
  1246. *
  1247. * Caller holds tomoyo_read_lock().
  1248. */
  1249. static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
  1250. {
  1251. char *data = head->write_buf;
  1252. struct tomoyo_domain_info *domain = head->write_var1;
  1253. bool is_delete = false;
  1254. bool is_select = false;
  1255. unsigned int profile;
  1256. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  1257. is_delete = true;
  1258. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  1259. is_select = true;
  1260. if (is_select && tomoyo_is_select_one(head, data))
  1261. return 0;
  1262. /* Don't allow updating policies by non manager programs. */
  1263. if (!tomoyo_is_policy_manager())
  1264. return -EPERM;
  1265. if (tomoyo_is_domain_def(data)) {
  1266. domain = NULL;
  1267. if (is_delete)
  1268. tomoyo_delete_domain(data);
  1269. else if (is_select)
  1270. domain = tomoyo_find_domain(data);
  1271. else
  1272. domain = tomoyo_find_or_assign_new_domain(data, 0);
  1273. head->write_var1 = domain;
  1274. return 0;
  1275. }
  1276. if (!domain)
  1277. return -EINVAL;
  1278. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  1279. && profile < TOMOYO_MAX_PROFILES) {
  1280. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  1281. domain->profile = (u8) profile;
  1282. return 0;
  1283. }
  1284. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  1285. domain->ignore_global_allow_read = !is_delete;
  1286. return 0;
  1287. }
  1288. return tomoyo_write_file_policy(data, domain, is_delete);
  1289. }
  1290. /**
  1291. * tomoyo_print_path_acl - Print a single path ACL entry.
  1292. *
  1293. * @head: Pointer to "struct tomoyo_io_buffer".
  1294. * @ptr: Pointer to "struct tomoyo_path_acl".
  1295. *
  1296. * Returns true on success, false otherwise.
  1297. */
  1298. static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
  1299. struct tomoyo_path_acl *ptr)
  1300. {
  1301. int pos;
  1302. u8 bit;
  1303. const char *atmark = "";
  1304. const char *filename;
  1305. const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
  1306. filename = ptr->filename->name;
  1307. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  1308. const char *msg;
  1309. if (!(perm & (1 << bit)))
  1310. continue;
  1311. /* Print "read/write" instead of "read" and "write". */
  1312. if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
  1313. && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  1314. continue;
  1315. msg = tomoyo_path2keyword(bit);
  1316. pos = head->read_avail;
  1317. if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
  1318. atmark, filename))
  1319. goto out;
  1320. }
  1321. head->read_bit = 0;
  1322. return true;
  1323. out:
  1324. head->read_bit = bit;
  1325. head->read_avail = pos;
  1326. return false;
  1327. }
  1328. /**
  1329. * tomoyo_print_path2_acl - Print a double path ACL entry.
  1330. *
  1331. * @head: Pointer to "struct tomoyo_io_buffer".
  1332. * @ptr: Pointer to "struct tomoyo_path2_acl".
  1333. *
  1334. * Returns true on success, false otherwise.
  1335. */
  1336. static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
  1337. struct tomoyo_path2_acl *ptr)
  1338. {
  1339. int pos;
  1340. const char *atmark1 = "";
  1341. const char *atmark2 = "";
  1342. const char *filename1;
  1343. const char *filename2;
  1344. const u8 perm = ptr->perm;
  1345. u8 bit;
  1346. filename1 = ptr->filename1->name;
  1347. filename2 = ptr->filename2->name;
  1348. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
  1349. const char *msg;
  1350. if (!(perm & (1 << bit)))
  1351. continue;
  1352. msg = tomoyo_path22keyword(bit);
  1353. pos = head->read_avail;
  1354. if (!tomoyo_io_printf(head, "allow_%s %s%s %s%s\n", msg,
  1355. atmark1, filename1, atmark2, filename2))
  1356. goto out;
  1357. }
  1358. head->read_bit = 0;
  1359. return true;
  1360. out:
  1361. head->read_bit = bit;
  1362. head->read_avail = pos;
  1363. return false;
  1364. }
  1365. /**
  1366. * tomoyo_print_entry - Print an ACL entry.
  1367. *
  1368. * @head: Pointer to "struct tomoyo_io_buffer".
  1369. * @ptr: Pointer to an ACL entry.
  1370. *
  1371. * Returns true on success, false otherwise.
  1372. */
  1373. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  1374. struct tomoyo_acl_info *ptr)
  1375. {
  1376. const u8 acl_type = ptr->type;
  1377. if (acl_type == TOMOYO_TYPE_PATH_ACL) {
  1378. struct tomoyo_path_acl *acl
  1379. = container_of(ptr, struct tomoyo_path_acl, head);
  1380. return tomoyo_print_path_acl(head, acl);
  1381. }
  1382. if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
  1383. struct tomoyo_path2_acl *acl
  1384. = container_of(ptr, struct tomoyo_path2_acl, head);
  1385. return tomoyo_print_path2_acl(head, acl);
  1386. }
  1387. BUG(); /* This must not happen. */
  1388. return false;
  1389. }
  1390. /**
  1391. * tomoyo_read_domain_policy - Read domain policy.
  1392. *
  1393. * @head: Pointer to "struct tomoyo_io_buffer".
  1394. *
  1395. * Returns 0.
  1396. *
  1397. * Caller holds tomoyo_read_lock().
  1398. */
  1399. static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
  1400. {
  1401. struct list_head *dpos;
  1402. struct list_head *apos;
  1403. bool done = true;
  1404. if (head->read_eof)
  1405. return 0;
  1406. if (head->read_step == 0)
  1407. head->read_step = 1;
  1408. list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
  1409. struct tomoyo_domain_info *domain;
  1410. const char *quota_exceeded = "";
  1411. const char *transition_failed = "";
  1412. const char *ignore_global_allow_read = "";
  1413. domain = list_entry(dpos, struct tomoyo_domain_info, list);
  1414. if (head->read_step != 1)
  1415. goto acl_loop;
  1416. if (domain->is_deleted && !head->read_single_domain)
  1417. continue;
  1418. /* Print domainname and flags. */
  1419. if (domain->quota_warned)
  1420. quota_exceeded = "quota_exceeded\n";
  1421. if (domain->transition_failed)
  1422. transition_failed = "transition_failed\n";
  1423. if (domain->ignore_global_allow_read)
  1424. ignore_global_allow_read
  1425. = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
  1426. done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
  1427. "%u\n%s%s%s\n",
  1428. domain->domainname->name,
  1429. domain->profile, quota_exceeded,
  1430. transition_failed,
  1431. ignore_global_allow_read);
  1432. if (!done)
  1433. break;
  1434. head->read_step = 2;
  1435. acl_loop:
  1436. if (head->read_step == 3)
  1437. goto tail_mark;
  1438. /* Print ACL entries in the domain. */
  1439. list_for_each_cookie(apos, head->read_var2,
  1440. &domain->acl_info_list) {
  1441. struct tomoyo_acl_info *ptr
  1442. = list_entry(apos, struct tomoyo_acl_info,
  1443. list);
  1444. done = tomoyo_print_entry(head, ptr);
  1445. if (!done)
  1446. break;
  1447. }
  1448. if (!done)
  1449. break;
  1450. head->read_step = 3;
  1451. tail_mark:
  1452. done = tomoyo_io_printf(head, "\n");
  1453. if (!done)
  1454. break;
  1455. head->read_step = 1;
  1456. if (head->read_single_domain)
  1457. break;
  1458. }
  1459. head->read_eof = done;
  1460. return 0;
  1461. }
  1462. /**
  1463. * tomoyo_write_domain_profile - Assign profile for specified domain.
  1464. *
  1465. * @head: Pointer to "struct tomoyo_io_buffer".
  1466. *
  1467. * Returns 0 on success, -EINVAL otherwise.
  1468. *
  1469. * This is equivalent to doing
  1470. *
  1471. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  1472. * /usr/lib/ccs/loadpolicy -d
  1473. *
  1474. * Caller holds tomoyo_read_lock().
  1475. */
  1476. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  1477. {
  1478. char *data = head->write_buf;
  1479. char *cp = strchr(data, ' ');
  1480. struct tomoyo_domain_info *domain;
  1481. unsigned long profile;
  1482. if (!cp)
  1483. return -EINVAL;
  1484. *cp = '\0';
  1485. domain = tomoyo_find_domain(cp + 1);
  1486. if (strict_strtoul(data, 10, &profile))
  1487. return -EINVAL;
  1488. if (domain && profile < TOMOYO_MAX_PROFILES
  1489. && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
  1490. domain->profile = (u8) profile;
  1491. return 0;
  1492. }
  1493. /**
  1494. * tomoyo_read_domain_profile - Read only domainname and profile.
  1495. *
  1496. * @head: Pointer to "struct tomoyo_io_buffer".
  1497. *
  1498. * Returns list of profile number and domainname pairs.
  1499. *
  1500. * This is equivalent to doing
  1501. *
  1502. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  1503. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  1504. * domainname = $0; } else if ( $1 == "use_profile" ) {
  1505. * print $2 " " domainname; domainname = ""; } } ; '
  1506. *
  1507. * Caller holds tomoyo_read_lock().
  1508. */
  1509. static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1510. {
  1511. struct list_head *pos;
  1512. bool done = true;
  1513. if (head->read_eof)
  1514. return 0;
  1515. list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
  1516. struct tomoyo_domain_info *domain;
  1517. domain = list_entry(pos, struct tomoyo_domain_info, list);
  1518. if (domain->is_deleted)
  1519. continue;
  1520. done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
  1521. domain->domainname->name);
  1522. if (!done)
  1523. break;
  1524. }
  1525. head->read_eof = done;
  1526. return 0;
  1527. }
  1528. /**
  1529. * tomoyo_write_pid: Specify PID to obtain domainname.
  1530. *
  1531. * @head: Pointer to "struct tomoyo_io_buffer".
  1532. *
  1533. * Returns 0.
  1534. */
  1535. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1536. {
  1537. unsigned long pid;
  1538. /* No error check. */
  1539. strict_strtoul(head->write_buf, 10, &pid);
  1540. head->read_step = (int) pid;
  1541. head->read_eof = false;
  1542. return 0;
  1543. }
  1544. /**
  1545. * tomoyo_read_pid - Get domainname of the specified PID.
  1546. *
  1547. * @head: Pointer to "struct tomoyo_io_buffer".
  1548. *
  1549. * Returns the domainname which the specified PID is in on success,
  1550. * empty string otherwise.
  1551. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1552. * using read()/write() interface rather than sysctl() interface.
  1553. */
  1554. static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1555. {
  1556. if (head->read_avail == 0 && !head->read_eof) {
  1557. const int pid = head->read_step;
  1558. struct task_struct *p;
  1559. struct tomoyo_domain_info *domain = NULL;
  1560. rcu_read_lock();
  1561. read_lock(&tasklist_lock);
  1562. p = find_task_by_vpid(pid);
  1563. if (p)
  1564. domain = tomoyo_real_domain(p);
  1565. read_unlock(&tasklist_lock);
  1566. rcu_read_unlock();
  1567. if (domain)
  1568. tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
  1569. domain->domainname->name);
  1570. head->read_eof = true;
  1571. }
  1572. return 0;
  1573. }
  1574. /**
  1575. * tomoyo_write_exception_policy - Write exception policy.
  1576. *
  1577. * @head: Pointer to "struct tomoyo_io_buffer".
  1578. *
  1579. * Returns 0 on success, negative value otherwise.
  1580. *
  1581. * Caller holds tomoyo_read_lock().
  1582. */
  1583. static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
  1584. {
  1585. char *data = head->write_buf;
  1586. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1587. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
  1588. return tomoyo_write_domain_keeper_policy(data, false,
  1589. is_delete);
  1590. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
  1591. return tomoyo_write_domain_keeper_policy(data, true, is_delete);
  1592. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
  1593. return tomoyo_write_domain_initializer_policy(data, false,
  1594. is_delete);
  1595. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
  1596. return tomoyo_write_domain_initializer_policy(data, true,
  1597. is_delete);
  1598. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
  1599. return tomoyo_write_alias_policy(data, is_delete);
  1600. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
  1601. return tomoyo_write_globally_readable_policy(data, is_delete);
  1602. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
  1603. return tomoyo_write_pattern_policy(data, is_delete);
  1604. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
  1605. return tomoyo_write_no_rewrite_policy(data, is_delete);
  1606. return -EINVAL;
  1607. }
  1608. /**
  1609. * tomoyo_read_exception_policy - Read exception policy.
  1610. *
  1611. * @head: Pointer to "struct tomoyo_io_buffer".
  1612. *
  1613. * Returns 0 on success, -EINVAL otherwise.
  1614. *
  1615. * Caller holds tomoyo_read_lock().
  1616. */
  1617. static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
  1618. {
  1619. if (!head->read_eof) {
  1620. switch (head->read_step) {
  1621. case 0:
  1622. head->read_var2 = NULL;
  1623. head->read_step = 1;
  1624. case 1:
  1625. if (!tomoyo_read_domain_keeper_policy(head))
  1626. break;
  1627. head->read_var2 = NULL;
  1628. head->read_step = 2;
  1629. case 2:
  1630. if (!tomoyo_read_globally_readable_policy(head))
  1631. break;
  1632. head->read_var2 = NULL;
  1633. head->read_step = 3;
  1634. case 3:
  1635. head->read_var2 = NULL;
  1636. head->read_step = 4;
  1637. case 4:
  1638. if (!tomoyo_read_domain_initializer_policy(head))
  1639. break;
  1640. head->read_var2 = NULL;
  1641. head->read_step = 5;
  1642. case 5:
  1643. if (!tomoyo_read_alias_policy(head))
  1644. break;
  1645. head->read_var2 = NULL;
  1646. head->read_step = 6;
  1647. case 6:
  1648. head->read_var2 = NULL;
  1649. head->read_step = 7;
  1650. case 7:
  1651. if (!tomoyo_read_file_pattern(head))
  1652. break;
  1653. head->read_var2 = NULL;
  1654. head->read_step = 8;
  1655. case 8:
  1656. if (!tomoyo_read_no_rewrite_policy(head))
  1657. break;
  1658. head->read_var2 = NULL;
  1659. head->read_step = 9;
  1660. case 9:
  1661. head->read_eof = true;
  1662. break;
  1663. default:
  1664. return -EINVAL;
  1665. }
  1666. }
  1667. return 0;
  1668. }
  1669. /* path to policy loader */
  1670. static const char *tomoyo_loader = "/sbin/tomoyo-init";
  1671. /**
  1672. * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
  1673. *
  1674. * Returns true if /sbin/tomoyo-init exists, false otherwise.
  1675. */
  1676. static bool tomoyo_policy_loader_exists(void)
  1677. {
  1678. /*
  1679. * Don't activate MAC if the policy loader doesn't exist.
  1680. * If the initrd includes /sbin/init but real-root-dev has not
  1681. * mounted on / yet, activating MAC will block the system since
  1682. * policies are not loaded yet.
  1683. * Thus, let do_execve() call this function everytime.
  1684. */
  1685. struct path path;
  1686. if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
  1687. printk(KERN_INFO "Not activating Mandatory Access Control now "
  1688. "since %s doesn't exist.\n", tomoyo_loader);
  1689. return false;
  1690. }
  1691. path_put(&path);
  1692. return true;
  1693. }
  1694. /**
  1695. * tomoyo_load_policy - Run external policy loader to load policy.
  1696. *
  1697. * @filename: The program about to start.
  1698. *
  1699. * This function checks whether @filename is /sbin/init , and if so
  1700. * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
  1701. * and then continues invocation of /sbin/init.
  1702. * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
  1703. * writes to /sys/kernel/security/tomoyo/ interfaces.
  1704. *
  1705. * Returns nothing.
  1706. */
  1707. void tomoyo_load_policy(const char *filename)
  1708. {
  1709. char *argv[2];
  1710. char *envp[3];
  1711. if (tomoyo_policy_loaded)
  1712. return;
  1713. /*
  1714. * Check filename is /sbin/init or /sbin/tomoyo-start.
  1715. * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
  1716. * be passed.
  1717. * You can create /sbin/tomoyo-start by
  1718. * "ln -s /bin/true /sbin/tomoyo-start".
  1719. */
  1720. if (strcmp(filename, "/sbin/init") &&
  1721. strcmp(filename, "/sbin/tomoyo-start"))
  1722. return;
  1723. if (!tomoyo_policy_loader_exists())
  1724. return;
  1725. printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
  1726. tomoyo_loader);
  1727. argv[0] = (char *) tomoyo_loader;
  1728. argv[1] = NULL;
  1729. envp[0] = "HOME=/";
  1730. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  1731. envp[2] = NULL;
  1732. call_usermodehelper(argv[0], argv, envp, 1);
  1733. printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
  1734. printk(KERN_INFO "Mandatory Access Control activated.\n");
  1735. tomoyo_policy_loaded = true;
  1736. { /* Check all profiles currently assigned to domains are defined. */
  1737. struct tomoyo_domain_info *domain;
  1738. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1739. const u8 profile = domain->profile;
  1740. if (tomoyo_profile_ptr[profile])
  1741. continue;
  1742. panic("Profile %u (used by '%s') not defined.\n",
  1743. profile, domain->domainname->name);
  1744. }
  1745. }
  1746. }
  1747. /**
  1748. * tomoyo_read_version: Get version.
  1749. *
  1750. * @head: Pointer to "struct tomoyo_io_buffer".
  1751. *
  1752. * Returns version information.
  1753. */
  1754. static int tomoyo_read_version(struct tomoyo_io_buffer *head)
  1755. {
  1756. if (!head->read_eof) {
  1757. tomoyo_io_printf(head, "2.2.0");
  1758. head->read_eof = true;
  1759. }
  1760. return 0;
  1761. }
  1762. /**
  1763. * tomoyo_read_self_domain - Get the current process's domainname.
  1764. *
  1765. * @head: Pointer to "struct tomoyo_io_buffer".
  1766. *
  1767. * Returns the current process's domainname.
  1768. */
  1769. static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1770. {
  1771. if (!head->read_eof) {
  1772. /*
  1773. * tomoyo_domain()->domainname != NULL
  1774. * because every process belongs to a domain and
  1775. * the domain's name cannot be NULL.
  1776. */
  1777. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1778. head->read_eof = true;
  1779. }
  1780. return 0;
  1781. }
  1782. /**
  1783. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1784. *
  1785. * @type: Type of interface.
  1786. * @file: Pointer to "struct file".
  1787. *
  1788. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1789. *
  1790. * Caller acquires tomoyo_read_lock().
  1791. */
  1792. static int tomoyo_open_control(const u8 type, struct file *file)
  1793. {
  1794. struct tomoyo_io_buffer *head = kzalloc(sizeof(*head), GFP_KERNEL);
  1795. if (!head)
  1796. return -ENOMEM;
  1797. mutex_init(&head->io_sem);
  1798. switch (type) {
  1799. case TOMOYO_DOMAINPOLICY:
  1800. /* /sys/kernel/security/tomoyo/domain_policy */
  1801. head->write = tomoyo_write_domain_policy;
  1802. head->read = tomoyo_read_domain_policy;
  1803. break;
  1804. case TOMOYO_EXCEPTIONPOLICY:
  1805. /* /sys/kernel/security/tomoyo/exception_policy */
  1806. head->write = tomoyo_write_exception_policy;
  1807. head->read = tomoyo_read_exception_policy;
  1808. break;
  1809. case TOMOYO_SELFDOMAIN:
  1810. /* /sys/kernel/security/tomoyo/self_domain */
  1811. head->read = tomoyo_read_self_domain;
  1812. break;
  1813. case TOMOYO_DOMAIN_STATUS:
  1814. /* /sys/kernel/security/tomoyo/.domain_status */
  1815. head->write = tomoyo_write_domain_profile;
  1816. head->read = tomoyo_read_domain_profile;
  1817. break;
  1818. case TOMOYO_PROCESS_STATUS:
  1819. /* /sys/kernel/security/tomoyo/.process_status */
  1820. head->write = tomoyo_write_pid;
  1821. head->read = tomoyo_read_pid;
  1822. break;
  1823. case TOMOYO_VERSION:
  1824. /* /sys/kernel/security/tomoyo/version */
  1825. head->read = tomoyo_read_version;
  1826. head->readbuf_size = 128;
  1827. break;
  1828. case TOMOYO_MEMINFO:
  1829. /* /sys/kernel/security/tomoyo/meminfo */
  1830. head->write = tomoyo_write_memory_quota;
  1831. head->read = tomoyo_read_memory_counter;
  1832. head->readbuf_size = 512;
  1833. break;
  1834. case TOMOYO_PROFILE:
  1835. /* /sys/kernel/security/tomoyo/profile */
  1836. head->write = tomoyo_write_profile;
  1837. head->read = tomoyo_read_profile;
  1838. break;
  1839. case TOMOYO_MANAGER:
  1840. /* /sys/kernel/security/tomoyo/manager */
  1841. head->write = tomoyo_write_manager_policy;
  1842. head->read = tomoyo_read_manager_policy;
  1843. break;
  1844. }
  1845. if (!(file->f_mode & FMODE_READ)) {
  1846. /*
  1847. * No need to allocate read_buf since it is not opened
  1848. * for reading.
  1849. */
  1850. head->read = NULL;
  1851. } else {
  1852. if (!head->readbuf_size)
  1853. head->readbuf_size = 4096 * 2;
  1854. head->read_buf = kzalloc(head->readbuf_size, GFP_KERNEL);
  1855. if (!head->read_buf) {
  1856. kfree(head);
  1857. return -ENOMEM;
  1858. }
  1859. }
  1860. if (!(file->f_mode & FMODE_WRITE)) {
  1861. /*
  1862. * No need to allocate write_buf since it is not opened
  1863. * for writing.
  1864. */
  1865. head->write = NULL;
  1866. } else if (head->write) {
  1867. head->writebuf_size = 4096 * 2;
  1868. head->write_buf = kzalloc(head->writebuf_size, GFP_KERNEL);
  1869. if (!head->write_buf) {
  1870. kfree(head->read_buf);
  1871. kfree(head);
  1872. return -ENOMEM;
  1873. }
  1874. }
  1875. head->reader_idx = tomoyo_read_lock();
  1876. file->private_data = head;
  1877. /*
  1878. * Call the handler now if the file is
  1879. * /sys/kernel/security/tomoyo/self_domain
  1880. * so that the user can use
  1881. * cat < /sys/kernel/security/tomoyo/self_domain"
  1882. * to know the current process's domainname.
  1883. */
  1884. if (type == TOMOYO_SELFDOMAIN)
  1885. tomoyo_read_control(file, NULL, 0);
  1886. return 0;
  1887. }
  1888. /**
  1889. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1890. *
  1891. * @file: Pointer to "struct file".
  1892. * @buffer: Poiner to buffer to write to.
  1893. * @buffer_len: Size of @buffer.
  1894. *
  1895. * Returns bytes read on success, negative value otherwise.
  1896. *
  1897. * Caller holds tomoyo_read_lock().
  1898. */
  1899. static int tomoyo_read_control(struct file *file, char __user *buffer,
  1900. const int buffer_len)
  1901. {
  1902. int len = 0;
  1903. struct tomoyo_io_buffer *head = file->private_data;
  1904. char *cp;
  1905. if (!head->read)
  1906. return -ENOSYS;
  1907. if (mutex_lock_interruptible(&head->io_sem))
  1908. return -EINTR;
  1909. /* Call the policy handler. */
  1910. len = head->read(head);
  1911. if (len < 0)
  1912. goto out;
  1913. /* Write to buffer. */
  1914. len = head->read_avail;
  1915. if (len > buffer_len)
  1916. len = buffer_len;
  1917. if (!len)
  1918. goto out;
  1919. /* head->read_buf changes by some functions. */
  1920. cp = head->read_buf;
  1921. if (copy_to_user(buffer, cp, len)) {
  1922. len = -EFAULT;
  1923. goto out;
  1924. }
  1925. head->read_avail -= len;
  1926. memmove(cp, cp + len, head->read_avail);
  1927. out:
  1928. mutex_unlock(&head->io_sem);
  1929. return len;
  1930. }
  1931. /**
  1932. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1933. *
  1934. * @file: Pointer to "struct file".
  1935. * @buffer: Pointer to buffer to read from.
  1936. * @buffer_len: Size of @buffer.
  1937. *
  1938. * Returns @buffer_len on success, negative value otherwise.
  1939. *
  1940. * Caller holds tomoyo_read_lock().
  1941. */
  1942. static int tomoyo_write_control(struct file *file, const char __user *buffer,
  1943. const int buffer_len)
  1944. {
  1945. struct tomoyo_io_buffer *head = file->private_data;
  1946. int error = buffer_len;
  1947. int avail_len = buffer_len;
  1948. char *cp0 = head->write_buf;
  1949. if (!head->write)
  1950. return -ENOSYS;
  1951. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1952. return -EFAULT;
  1953. /* Don't allow updating policies by non manager programs. */
  1954. if (head->write != tomoyo_write_pid &&
  1955. head->write != tomoyo_write_domain_policy &&
  1956. !tomoyo_is_policy_manager())
  1957. return -EPERM;
  1958. if (mutex_lock_interruptible(&head->io_sem))
  1959. return -EINTR;
  1960. /* Read a line and dispatch it to the policy handler. */
  1961. while (avail_len > 0) {
  1962. char c;
  1963. if (head->write_avail >= head->writebuf_size - 1) {
  1964. error = -ENOMEM;
  1965. break;
  1966. } else if (get_user(c, buffer)) {
  1967. error = -EFAULT;
  1968. break;
  1969. }
  1970. buffer++;
  1971. avail_len--;
  1972. cp0[head->write_avail++] = c;
  1973. if (c != '\n')
  1974. continue;
  1975. cp0[head->write_avail - 1] = '\0';
  1976. head->write_avail = 0;
  1977. tomoyo_normalize_line(cp0);
  1978. head->write(head);
  1979. }
  1980. mutex_unlock(&head->io_sem);
  1981. return error;
  1982. }
  1983. /**
  1984. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  1985. *
  1986. * @file: Pointer to "struct file".
  1987. *
  1988. * Releases memory and returns 0.
  1989. *
  1990. * Caller looses tomoyo_read_lock().
  1991. */
  1992. static int tomoyo_close_control(struct file *file)
  1993. {
  1994. struct tomoyo_io_buffer *head = file->private_data;
  1995. const bool is_write = !!head->write_buf;
  1996. tomoyo_read_unlock(head->reader_idx);
  1997. /* Release memory used for policy I/O. */
  1998. kfree(head->read_buf);
  1999. head->read_buf = NULL;
  2000. kfree(head->write_buf);
  2001. head->write_buf = NULL;
  2002. kfree(head);
  2003. head = NULL;
  2004. file->private_data = NULL;
  2005. if (is_write)
  2006. tomoyo_run_gc();
  2007. return 0;
  2008. }
  2009. /**
  2010. * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
  2011. *
  2012. * @inode: Pointer to "struct inode".
  2013. * @file: Pointer to "struct file".
  2014. *
  2015. * Returns 0 on success, negative value otherwise.
  2016. */
  2017. static int tomoyo_open(struct inode *inode, struct file *file)
  2018. {
  2019. const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
  2020. - ((u8 *) NULL);
  2021. return tomoyo_open_control(key, file);
  2022. }
  2023. /**
  2024. * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
  2025. *
  2026. * @inode: Pointer to "struct inode".
  2027. * @file: Pointer to "struct file".
  2028. *
  2029. * Returns 0 on success, negative value otherwise.
  2030. */
  2031. static int tomoyo_release(struct inode *inode, struct file *file)
  2032. {
  2033. return tomoyo_close_control(file);
  2034. }
  2035. /**
  2036. * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
  2037. *
  2038. * @file: Pointer to "struct file".
  2039. * @buf: Pointer to buffer.
  2040. * @count: Size of @buf.
  2041. * @ppos: Unused.
  2042. *
  2043. * Returns bytes read on success, negative value otherwise.
  2044. */
  2045. static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
  2046. loff_t *ppos)
  2047. {
  2048. return tomoyo_read_control(file, buf, count);
  2049. }
  2050. /**
  2051. * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
  2052. *
  2053. * @file: Pointer to "struct file".
  2054. * @buf: Pointer to buffer.
  2055. * @count: Size of @buf.
  2056. * @ppos: Unused.
  2057. *
  2058. * Returns @count on success, negative value otherwise.
  2059. */
  2060. static ssize_t tomoyo_write(struct file *file, const char __user *buf,
  2061. size_t count, loff_t *ppos)
  2062. {
  2063. return tomoyo_write_control(file, buf, count);
  2064. }
  2065. /*
  2066. * tomoyo_operations is a "struct file_operations" which is used for handling
  2067. * /sys/kernel/security/tomoyo/ interface.
  2068. *
  2069. * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
  2070. * See tomoyo_io_buffer for internals.
  2071. */
  2072. static const struct file_operations tomoyo_operations = {
  2073. .open = tomoyo_open,
  2074. .release = tomoyo_release,
  2075. .read = tomoyo_read,
  2076. .write = tomoyo_write,
  2077. };
  2078. /**
  2079. * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
  2080. *
  2081. * @name: The name of the interface file.
  2082. * @mode: The permission of the interface file.
  2083. * @parent: The parent directory.
  2084. * @key: Type of interface.
  2085. *
  2086. * Returns nothing.
  2087. */
  2088. static void __init tomoyo_create_entry(const char *name, const mode_t mode,
  2089. struct dentry *parent, const u8 key)
  2090. {
  2091. securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
  2092. &tomoyo_operations);
  2093. }
  2094. /**
  2095. * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
  2096. *
  2097. * Returns 0.
  2098. */
  2099. static int __init tomoyo_initerface_init(void)
  2100. {
  2101. struct dentry *tomoyo_dir;
  2102. /* Don't create securityfs entries unless registered. */
  2103. if (current_cred()->security != &tomoyo_kernel_domain)
  2104. return 0;
  2105. tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
  2106. tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
  2107. TOMOYO_DOMAINPOLICY);
  2108. tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
  2109. TOMOYO_EXCEPTIONPOLICY);
  2110. tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
  2111. TOMOYO_SELFDOMAIN);
  2112. tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
  2113. TOMOYO_DOMAIN_STATUS);
  2114. tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
  2115. TOMOYO_PROCESS_STATUS);
  2116. tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
  2117. TOMOYO_MEMINFO);
  2118. tomoyo_create_entry("profile", 0600, tomoyo_dir,
  2119. TOMOYO_PROFILE);
  2120. tomoyo_create_entry("manager", 0600, tomoyo_dir,
  2121. TOMOYO_MANAGER);
  2122. tomoyo_create_entry("version", 0400, tomoyo_dir,
  2123. TOMOYO_VERSION);
  2124. return 0;
  2125. }
  2126. fs_initcall(tomoyo_initerface_init);