common.c 57 KB

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