common.c 58 KB

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