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/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. ptr = NULL;
  844. goto ok;
  845. }
  846. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
  847. ptr->value[i] = tomoyo_control_array[i].current_value;
  848. mb(); /* Avoid out-of-order execution. */
  849. tomoyo_profile_ptr[profile] = ptr;
  850. ok:
  851. mutex_unlock(&lock);
  852. return ptr;
  853. }
  854. /**
  855. * tomoyo_write_profile - Write to profile table.
  856. *
  857. * @head: Pointer to "struct tomoyo_io_buffer".
  858. *
  859. * Returns 0 on success, negative value otherwise.
  860. */
  861. static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
  862. {
  863. char *data = head->write_buf;
  864. unsigned int i;
  865. unsigned int value;
  866. char *cp;
  867. struct tomoyo_profile *profile;
  868. unsigned long num;
  869. cp = strchr(data, '-');
  870. if (cp)
  871. *cp = '\0';
  872. if (strict_strtoul(data, 10, &num))
  873. return -EINVAL;
  874. if (cp)
  875. data = cp + 1;
  876. profile = tomoyo_find_or_assign_new_profile(num);
  877. if (!profile)
  878. return -EINVAL;
  879. cp = strchr(data, '=');
  880. if (!cp)
  881. return -EINVAL;
  882. *cp = '\0';
  883. if (!strcmp(data, "COMMENT")) {
  884. const struct tomoyo_path_info *old_comment = profile->comment;
  885. profile->comment = tomoyo_get_name(cp + 1);
  886. tomoyo_put_name(old_comment);
  887. return 0;
  888. }
  889. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
  890. if (strcmp(data, tomoyo_control_array[i].keyword))
  891. continue;
  892. if (sscanf(cp + 1, "%u", &value) != 1) {
  893. int j;
  894. const char **modes;
  895. switch (i) {
  896. case TOMOYO_VERBOSE:
  897. modes = tomoyo_mode_2;
  898. break;
  899. default:
  900. modes = tomoyo_mode_4;
  901. break;
  902. }
  903. for (j = 0; j < 4; j++) {
  904. if (strcmp(cp + 1, modes[j]))
  905. continue;
  906. value = j;
  907. break;
  908. }
  909. if (j == 4)
  910. return -EINVAL;
  911. } else if (value > tomoyo_control_array[i].max_value) {
  912. value = tomoyo_control_array[i].max_value;
  913. }
  914. profile->value[i] = value;
  915. return 0;
  916. }
  917. return -EINVAL;
  918. }
  919. /**
  920. * tomoyo_read_profile - Read from profile table.
  921. *
  922. * @head: Pointer to "struct tomoyo_io_buffer".
  923. *
  924. * Returns 0.
  925. */
  926. static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
  927. {
  928. static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
  929. int step;
  930. if (head->read_eof)
  931. return 0;
  932. for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
  933. step++) {
  934. const u8 index = step / total;
  935. u8 type = step % total;
  936. const struct tomoyo_profile *profile
  937. = tomoyo_profile_ptr[index];
  938. head->read_step = step;
  939. if (!profile)
  940. continue;
  941. if (!type) { /* Print profile' comment tag. */
  942. if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
  943. index, profile->comment ?
  944. profile->comment->name : ""))
  945. break;
  946. continue;
  947. }
  948. type--;
  949. if (type < TOMOYO_MAX_CONTROL_INDEX) {
  950. const unsigned int value = profile->value[type];
  951. const char **modes = NULL;
  952. const char *keyword
  953. = tomoyo_control_array[type].keyword;
  954. switch (tomoyo_control_array[type].max_value) {
  955. case 3:
  956. modes = tomoyo_mode_4;
  957. break;
  958. case 1:
  959. modes = tomoyo_mode_2;
  960. break;
  961. }
  962. if (modes) {
  963. if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
  964. keyword, modes[value]))
  965. break;
  966. } else {
  967. if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
  968. keyword, value))
  969. break;
  970. }
  971. }
  972. }
  973. if (step == TOMOYO_MAX_PROFILES * total)
  974. head->read_eof = true;
  975. return 0;
  976. }
  977. /*
  978. * tomoyo_policy_manager_list is used for holding list of domainnames or
  979. * programs which are permitted to modify configuration via
  980. * /sys/kernel/security/tomoyo/ interface.
  981. *
  982. * An entry is added by
  983. *
  984. * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
  985. * /sys/kernel/security/tomoyo/manager
  986. * (if you want to specify by a domainname)
  987. *
  988. * or
  989. *
  990. * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
  991. * (if you want to specify by a program's location)
  992. *
  993. * and is deleted by
  994. *
  995. * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
  996. * /sys/kernel/security/tomoyo/manager
  997. *
  998. * or
  999. *
  1000. * # echo 'delete /usr/lib/ccs/editpolicy' > \
  1001. * /sys/kernel/security/tomoyo/manager
  1002. *
  1003. * and all entries are retrieved by
  1004. *
  1005. * # cat /sys/kernel/security/tomoyo/manager
  1006. */
  1007. LIST_HEAD(tomoyo_policy_manager_list);
  1008. /**
  1009. * tomoyo_update_manager_entry - Add a manager entry.
  1010. *
  1011. * @manager: The path to manager or the domainnamme.
  1012. * @is_delete: True if it is a delete request.
  1013. *
  1014. * Returns 0 on success, negative value otherwise.
  1015. *
  1016. * Caller holds tomoyo_read_lock().
  1017. */
  1018. static int tomoyo_update_manager_entry(const char *manager,
  1019. const bool is_delete)
  1020. {
  1021. struct tomoyo_policy_manager_entry *entry = NULL;
  1022. struct tomoyo_policy_manager_entry *ptr;
  1023. const struct tomoyo_path_info *saved_manager;
  1024. int error = is_delete ? -ENOENT : -ENOMEM;
  1025. bool is_domain = false;
  1026. if (tomoyo_is_domain_def(manager)) {
  1027. if (!tomoyo_is_correct_domain(manager))
  1028. return -EINVAL;
  1029. is_domain = true;
  1030. } else {
  1031. if (!tomoyo_is_correct_path(manager, 1, -1, -1))
  1032. return -EINVAL;
  1033. }
  1034. saved_manager = tomoyo_get_name(manager);
  1035. if (!saved_manager)
  1036. return -ENOMEM;
  1037. if (!is_delete)
  1038. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1039. mutex_lock(&tomoyo_policy_lock);
  1040. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1041. if (ptr->manager != saved_manager)
  1042. continue;
  1043. ptr->is_deleted = is_delete;
  1044. error = 0;
  1045. break;
  1046. }
  1047. if (!is_delete && error && tomoyo_memory_ok(entry)) {
  1048. entry->manager = saved_manager;
  1049. saved_manager = NULL;
  1050. entry->is_domain = is_domain;
  1051. list_add_tail_rcu(&entry->list, &tomoyo_policy_manager_list);
  1052. entry = NULL;
  1053. error = 0;
  1054. }
  1055. mutex_unlock(&tomoyo_policy_lock);
  1056. tomoyo_put_name(saved_manager);
  1057. kfree(entry);
  1058. return error;
  1059. }
  1060. /**
  1061. * tomoyo_write_manager_policy - Write manager policy.
  1062. *
  1063. * @head: Pointer to "struct tomoyo_io_buffer".
  1064. *
  1065. * Returns 0 on success, negative value otherwise.
  1066. *
  1067. * Caller holds tomoyo_read_lock().
  1068. */
  1069. static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
  1070. {
  1071. char *data = head->write_buf;
  1072. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1073. if (!strcmp(data, "manage_by_non_root")) {
  1074. tomoyo_manage_by_non_root = !is_delete;
  1075. return 0;
  1076. }
  1077. return tomoyo_update_manager_entry(data, is_delete);
  1078. }
  1079. /**
  1080. * tomoyo_read_manager_policy - Read manager policy.
  1081. *
  1082. * @head: Pointer to "struct tomoyo_io_buffer".
  1083. *
  1084. * Returns 0.
  1085. *
  1086. * Caller holds tomoyo_read_lock().
  1087. */
  1088. static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
  1089. {
  1090. struct list_head *pos;
  1091. bool done = true;
  1092. if (head->read_eof)
  1093. return 0;
  1094. list_for_each_cookie(pos, head->read_var2,
  1095. &tomoyo_policy_manager_list) {
  1096. struct tomoyo_policy_manager_entry *ptr;
  1097. ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
  1098. list);
  1099. if (ptr->is_deleted)
  1100. continue;
  1101. done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
  1102. if (!done)
  1103. break;
  1104. }
  1105. head->read_eof = done;
  1106. return 0;
  1107. }
  1108. /**
  1109. * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
  1110. *
  1111. * Returns true if the current process is permitted to modify policy
  1112. * via /sys/kernel/security/tomoyo/ interface.
  1113. *
  1114. * Caller holds tomoyo_read_lock().
  1115. */
  1116. static bool tomoyo_is_policy_manager(void)
  1117. {
  1118. struct tomoyo_policy_manager_entry *ptr;
  1119. const char *exe;
  1120. const struct task_struct *task = current;
  1121. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  1122. bool found = false;
  1123. if (!tomoyo_policy_loaded)
  1124. return true;
  1125. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  1126. return false;
  1127. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1128. if (!ptr->is_deleted && ptr->is_domain
  1129. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  1130. found = true;
  1131. break;
  1132. }
  1133. }
  1134. if (found)
  1135. return true;
  1136. exe = tomoyo_get_exe();
  1137. if (!exe)
  1138. return false;
  1139. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1140. if (!ptr->is_deleted && !ptr->is_domain
  1141. && !strcmp(exe, ptr->manager->name)) {
  1142. found = true;
  1143. break;
  1144. }
  1145. }
  1146. if (!found) { /* Reduce error messages. */
  1147. static pid_t last_pid;
  1148. const pid_t pid = current->pid;
  1149. if (last_pid != pid) {
  1150. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  1151. "update policies.\n", domainname->name, exe);
  1152. last_pid = pid;
  1153. }
  1154. }
  1155. kfree(exe);
  1156. return found;
  1157. }
  1158. /**
  1159. * tomoyo_is_select_one - Parse select command.
  1160. *
  1161. * @head: Pointer to "struct tomoyo_io_buffer".
  1162. * @data: String to parse.
  1163. *
  1164. * Returns true on success, false otherwise.
  1165. *
  1166. * Caller holds tomoyo_read_lock().
  1167. */
  1168. static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
  1169. const char *data)
  1170. {
  1171. unsigned int pid;
  1172. struct tomoyo_domain_info *domain = NULL;
  1173. if (sscanf(data, "pid=%u", &pid) == 1) {
  1174. struct task_struct *p;
  1175. rcu_read_lock();
  1176. read_lock(&tasklist_lock);
  1177. p = find_task_by_vpid(pid);
  1178. if (p)
  1179. domain = tomoyo_real_domain(p);
  1180. read_unlock(&tasklist_lock);
  1181. rcu_read_unlock();
  1182. } else if (!strncmp(data, "domain=", 7)) {
  1183. if (tomoyo_is_domain_def(data + 7))
  1184. domain = tomoyo_find_domain(data + 7);
  1185. } else
  1186. return false;
  1187. head->write_var1 = domain;
  1188. /* Accessing read_buf is safe because head->io_sem is held. */
  1189. if (!head->read_buf)
  1190. return true; /* Do nothing if open(O_WRONLY). */
  1191. head->read_avail = 0;
  1192. tomoyo_io_printf(head, "# select %s\n", data);
  1193. head->read_single_domain = true;
  1194. head->read_eof = !domain;
  1195. if (domain) {
  1196. struct tomoyo_domain_info *d;
  1197. head->read_var1 = NULL;
  1198. list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
  1199. if (d == domain)
  1200. break;
  1201. head->read_var1 = &d->list;
  1202. }
  1203. head->read_var2 = NULL;
  1204. head->read_bit = 0;
  1205. head->read_step = 0;
  1206. if (domain->is_deleted)
  1207. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  1208. }
  1209. return true;
  1210. }
  1211. /**
  1212. * tomoyo_delete_domain - Delete a domain.
  1213. *
  1214. * @domainname: The name of domain.
  1215. *
  1216. * Returns 0.
  1217. *
  1218. * Caller holds tomoyo_read_lock().
  1219. */
  1220. static int tomoyo_delete_domain(char *domainname)
  1221. {
  1222. struct tomoyo_domain_info *domain;
  1223. struct tomoyo_path_info name;
  1224. name.name = domainname;
  1225. tomoyo_fill_path_info(&name);
  1226. mutex_lock(&tomoyo_policy_lock);
  1227. /* Is there an active domain? */
  1228. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1229. /* Never delete tomoyo_kernel_domain */
  1230. if (domain == &tomoyo_kernel_domain)
  1231. continue;
  1232. if (domain->is_deleted ||
  1233. tomoyo_pathcmp(domain->domainname, &name))
  1234. continue;
  1235. domain->is_deleted = true;
  1236. break;
  1237. }
  1238. mutex_unlock(&tomoyo_policy_lock);
  1239. return 0;
  1240. }
  1241. /**
  1242. * tomoyo_write_domain_policy - Write domain policy.
  1243. *
  1244. * @head: Pointer to "struct tomoyo_io_buffer".
  1245. *
  1246. * Returns 0 on success, negative value otherwise.
  1247. *
  1248. * Caller holds tomoyo_read_lock().
  1249. */
  1250. static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
  1251. {
  1252. char *data = head->write_buf;
  1253. struct tomoyo_domain_info *domain = head->write_var1;
  1254. bool is_delete = false;
  1255. bool is_select = false;
  1256. unsigned int profile;
  1257. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  1258. is_delete = true;
  1259. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  1260. is_select = true;
  1261. if (is_select && tomoyo_is_select_one(head, data))
  1262. return 0;
  1263. /* Don't allow updating policies by non manager programs. */
  1264. if (!tomoyo_is_policy_manager())
  1265. return -EPERM;
  1266. if (tomoyo_is_domain_def(data)) {
  1267. domain = NULL;
  1268. if (is_delete)
  1269. tomoyo_delete_domain(data);
  1270. else if (is_select)
  1271. domain = tomoyo_find_domain(data);
  1272. else
  1273. domain = tomoyo_find_or_assign_new_domain(data, 0);
  1274. head->write_var1 = domain;
  1275. return 0;
  1276. }
  1277. if (!domain)
  1278. return -EINVAL;
  1279. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  1280. && profile < TOMOYO_MAX_PROFILES) {
  1281. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  1282. domain->profile = (u8) profile;
  1283. return 0;
  1284. }
  1285. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  1286. domain->ignore_global_allow_read = !is_delete;
  1287. return 0;
  1288. }
  1289. return tomoyo_write_file_policy(data, domain, is_delete);
  1290. }
  1291. /**
  1292. * tomoyo_print_path_acl - Print a single path ACL entry.
  1293. *
  1294. * @head: Pointer to "struct tomoyo_io_buffer".
  1295. * @ptr: Pointer to "struct tomoyo_path_acl".
  1296. *
  1297. * Returns true on success, false otherwise.
  1298. */
  1299. static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
  1300. struct tomoyo_path_acl *ptr)
  1301. {
  1302. int pos;
  1303. u8 bit;
  1304. const char *atmark = "";
  1305. const char *filename;
  1306. const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
  1307. filename = ptr->filename->name;
  1308. for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
  1309. const char *msg;
  1310. if (!(perm & (1 << bit)))
  1311. continue;
  1312. /* Print "read/write" instead of "read" and "write". */
  1313. if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
  1314. && (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  1315. continue;
  1316. msg = tomoyo_path2keyword(bit);
  1317. pos = head->read_avail;
  1318. if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
  1319. atmark, filename))
  1320. goto out;
  1321. }
  1322. head->read_bit = 0;
  1323. return true;
  1324. out:
  1325. head->read_bit = bit;
  1326. head->read_avail = pos;
  1327. return false;
  1328. }
  1329. /**
  1330. * tomoyo_print_path2_acl - Print a double path ACL entry.
  1331. *
  1332. * @head: Pointer to "struct tomoyo_io_buffer".
  1333. * @ptr: Pointer to "struct tomoyo_path2_acl".
  1334. *
  1335. * Returns true on success, false otherwise.
  1336. */
  1337. static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
  1338. struct tomoyo_path2_acl *ptr)
  1339. {
  1340. int pos;
  1341. const char *atmark1 = "";
  1342. const char *atmark2 = "";
  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 %s%s\n", msg,
  1356. atmark1, filename1, atmark2, 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_KERNEL);
  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_KERNEL);
  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_KERNEL);
  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);