common.c 56 KB

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