common.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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_delete_domain - Delete a domain.
  1227. *
  1228. * @domainname: The name of domain.
  1229. *
  1230. * Returns 0.
  1231. */
  1232. static int tomoyo_delete_domain(char *domainname)
  1233. {
  1234. struct tomoyo_domain_info *domain;
  1235. struct tomoyo_path_info name;
  1236. name.name = domainname;
  1237. tomoyo_fill_path_info(&name);
  1238. down_write(&tomoyo_domain_list_lock);
  1239. /* Is there an active domain? */
  1240. list_for_each_entry(domain, &tomoyo_domain_list, list) {
  1241. /* Never delete tomoyo_kernel_domain */
  1242. if (domain == &tomoyo_kernel_domain)
  1243. continue;
  1244. if (domain->is_deleted ||
  1245. tomoyo_pathcmp(domain->domainname, &name))
  1246. continue;
  1247. domain->is_deleted = true;
  1248. break;
  1249. }
  1250. up_write(&tomoyo_domain_list_lock);
  1251. return 0;
  1252. }
  1253. /**
  1254. * tomoyo_write_domain_policy - Write domain policy.
  1255. *
  1256. * @head: Pointer to "struct tomoyo_io_buffer".
  1257. *
  1258. * Returns 0 on success, negative value otherwise.
  1259. */
  1260. static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
  1261. {
  1262. char *data = head->write_buf;
  1263. struct tomoyo_domain_info *domain = head->write_var1;
  1264. bool is_delete = false;
  1265. bool is_select = false;
  1266. unsigned int profile;
  1267. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  1268. is_delete = true;
  1269. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  1270. is_select = true;
  1271. if (is_select && tomoyo_is_select_one(head, data))
  1272. return 0;
  1273. /* Don't allow updating policies by non manager programs. */
  1274. if (!tomoyo_is_policy_manager())
  1275. return -EPERM;
  1276. if (tomoyo_is_domain_def(data)) {
  1277. domain = NULL;
  1278. if (is_delete)
  1279. tomoyo_delete_domain(data);
  1280. else if (is_select) {
  1281. down_read(&tomoyo_domain_list_lock);
  1282. domain = tomoyo_find_domain(data);
  1283. up_read(&tomoyo_domain_list_lock);
  1284. } else
  1285. domain = tomoyo_find_or_assign_new_domain(data, 0);
  1286. head->write_var1 = domain;
  1287. return 0;
  1288. }
  1289. if (!domain)
  1290. return -EINVAL;
  1291. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  1292. && profile < TOMOYO_MAX_PROFILES) {
  1293. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  1294. domain->profile = (u8) profile;
  1295. return 0;
  1296. }
  1297. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  1298. tomoyo_set_domain_flag(domain, is_delete,
  1299. TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
  1300. return 0;
  1301. }
  1302. return tomoyo_write_file_policy(data, domain, is_delete);
  1303. }
  1304. /**
  1305. * tomoyo_print_single_path_acl - Print a single path ACL entry.
  1306. *
  1307. * @head: Pointer to "struct tomoyo_io_buffer".
  1308. * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
  1309. *
  1310. * Returns true on success, false otherwise.
  1311. */
  1312. static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer *head,
  1313. struct tomoyo_single_path_acl_record *
  1314. ptr)
  1315. {
  1316. int pos;
  1317. u8 bit;
  1318. const char *atmark = "";
  1319. const char *filename;
  1320. const u16 perm = ptr->perm;
  1321. filename = ptr->filename->name;
  1322. for (bit = head->read_bit; bit < TOMOYO_MAX_SINGLE_PATH_OPERATION;
  1323. bit++) {
  1324. const char *msg;
  1325. if (!(perm & (1 << bit)))
  1326. continue;
  1327. /* Print "read/write" instead of "read" and "write". */
  1328. if ((bit == TOMOYO_TYPE_READ_ACL ||
  1329. bit == TOMOYO_TYPE_WRITE_ACL)
  1330. && (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
  1331. continue;
  1332. msg = tomoyo_sp2keyword(bit);
  1333. pos = head->read_avail;
  1334. if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
  1335. atmark, filename))
  1336. goto out;
  1337. }
  1338. head->read_bit = 0;
  1339. return true;
  1340. out:
  1341. head->read_bit = bit;
  1342. head->read_avail = pos;
  1343. return false;
  1344. }
  1345. /**
  1346. * tomoyo_print_double_path_acl - Print a double path ACL entry.
  1347. *
  1348. * @head: Pointer to "struct tomoyo_io_buffer".
  1349. * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
  1350. *
  1351. * Returns true on success, false otherwise.
  1352. */
  1353. static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer *head,
  1354. struct tomoyo_double_path_acl_record *
  1355. ptr)
  1356. {
  1357. int pos;
  1358. const char *atmark1 = "";
  1359. const char *atmark2 = "";
  1360. const char *filename1;
  1361. const char *filename2;
  1362. const u8 perm = ptr->perm;
  1363. u8 bit;
  1364. filename1 = ptr->filename1->name;
  1365. filename2 = ptr->filename2->name;
  1366. for (bit = head->read_bit; bit < TOMOYO_MAX_DOUBLE_PATH_OPERATION;
  1367. bit++) {
  1368. const char *msg;
  1369. if (!(perm & (1 << bit)))
  1370. continue;
  1371. msg = tomoyo_dp2keyword(bit);
  1372. pos = head->read_avail;
  1373. if (!tomoyo_io_printf(head, "allow_%s %s%s %s%s\n", msg,
  1374. atmark1, filename1, atmark2, filename2))
  1375. goto out;
  1376. }
  1377. head->read_bit = 0;
  1378. return true;
  1379. out:
  1380. head->read_bit = bit;
  1381. head->read_avail = pos;
  1382. return false;
  1383. }
  1384. /**
  1385. * tomoyo_print_entry - Print an ACL entry.
  1386. *
  1387. * @head: Pointer to "struct tomoyo_io_buffer".
  1388. * @ptr: Pointer to an ACL entry.
  1389. *
  1390. * Returns true on success, false otherwise.
  1391. */
  1392. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  1393. struct tomoyo_acl_info *ptr)
  1394. {
  1395. const u8 acl_type = tomoyo_acl_type2(ptr);
  1396. if (acl_type & TOMOYO_ACL_DELETED)
  1397. return true;
  1398. if (acl_type == TOMOYO_TYPE_SINGLE_PATH_ACL) {
  1399. struct tomoyo_single_path_acl_record *acl
  1400. = container_of(ptr,
  1401. struct tomoyo_single_path_acl_record,
  1402. head);
  1403. return tomoyo_print_single_path_acl(head, acl);
  1404. }
  1405. if (acl_type == TOMOYO_TYPE_DOUBLE_PATH_ACL) {
  1406. struct tomoyo_double_path_acl_record *acl
  1407. = container_of(ptr,
  1408. struct tomoyo_double_path_acl_record,
  1409. head);
  1410. return tomoyo_print_double_path_acl(head, acl);
  1411. }
  1412. BUG(); /* This must not happen. */
  1413. return false;
  1414. }
  1415. /**
  1416. * tomoyo_read_domain_policy - Read domain policy.
  1417. *
  1418. * @head: Pointer to "struct tomoyo_io_buffer".
  1419. *
  1420. * Returns 0.
  1421. */
  1422. static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
  1423. {
  1424. struct list_head *dpos;
  1425. struct list_head *apos;
  1426. bool done = true;
  1427. if (head->read_eof)
  1428. return 0;
  1429. if (head->read_step == 0)
  1430. head->read_step = 1;
  1431. down_read(&tomoyo_domain_list_lock);
  1432. list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
  1433. struct tomoyo_domain_info *domain;
  1434. const char *quota_exceeded = "";
  1435. const char *transition_failed = "";
  1436. const char *ignore_global_allow_read = "";
  1437. domain = list_entry(dpos, struct tomoyo_domain_info, list);
  1438. if (head->read_step != 1)
  1439. goto acl_loop;
  1440. if (domain->is_deleted && !head->read_single_domain)
  1441. continue;
  1442. /* Print domainname and flags. */
  1443. if (domain->quota_warned)
  1444. quota_exceeded = "quota_exceeded\n";
  1445. if (domain->flags & TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED)
  1446. transition_failed = "transition_failed\n";
  1447. if (domain->flags &
  1448. TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
  1449. ignore_global_allow_read
  1450. = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
  1451. done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
  1452. "%u\n%s%s%s\n",
  1453. domain->domainname->name,
  1454. domain->profile, quota_exceeded,
  1455. transition_failed,
  1456. ignore_global_allow_read);
  1457. if (!done)
  1458. break;
  1459. head->read_step = 2;
  1460. acl_loop:
  1461. if (head->read_step == 3)
  1462. goto tail_mark;
  1463. /* Print ACL entries in the domain. */
  1464. down_read(&tomoyo_domain_acl_info_list_lock);
  1465. list_for_each_cookie(apos, head->read_var2,
  1466. &domain->acl_info_list) {
  1467. struct tomoyo_acl_info *ptr
  1468. = list_entry(apos, struct tomoyo_acl_info,
  1469. list);
  1470. done = tomoyo_print_entry(head, ptr);
  1471. if (!done)
  1472. break;
  1473. }
  1474. up_read(&tomoyo_domain_acl_info_list_lock);
  1475. if (!done)
  1476. break;
  1477. head->read_step = 3;
  1478. tail_mark:
  1479. done = tomoyo_io_printf(head, "\n");
  1480. if (!done)
  1481. break;
  1482. head->read_step = 1;
  1483. if (head->read_single_domain)
  1484. break;
  1485. }
  1486. up_read(&tomoyo_domain_list_lock);
  1487. head->read_eof = done;
  1488. return 0;
  1489. }
  1490. /**
  1491. * tomoyo_write_domain_profile - Assign profile for specified domain.
  1492. *
  1493. * @head: Pointer to "struct tomoyo_io_buffer".
  1494. *
  1495. * Returns 0 on success, -EINVAL otherwise.
  1496. *
  1497. * This is equivalent to doing
  1498. *
  1499. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  1500. * /usr/lib/ccs/loadpolicy -d
  1501. */
  1502. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  1503. {
  1504. char *data = head->write_buf;
  1505. char *cp = strchr(data, ' ');
  1506. struct tomoyo_domain_info *domain;
  1507. unsigned long profile;
  1508. if (!cp)
  1509. return -EINVAL;
  1510. *cp = '\0';
  1511. down_read(&tomoyo_domain_list_lock);
  1512. domain = tomoyo_find_domain(cp + 1);
  1513. up_read(&tomoyo_domain_list_lock);
  1514. if (strict_strtoul(data, 10, &profile))
  1515. return -EINVAL;
  1516. if (domain && profile < TOMOYO_MAX_PROFILES
  1517. && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
  1518. domain->profile = (u8) profile;
  1519. return 0;
  1520. }
  1521. /**
  1522. * tomoyo_read_domain_profile - Read only domainname and profile.
  1523. *
  1524. * @head: Pointer to "struct tomoyo_io_buffer".
  1525. *
  1526. * Returns list of profile number and domainname pairs.
  1527. *
  1528. * This is equivalent to doing
  1529. *
  1530. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  1531. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  1532. * domainname = $0; } else if ( $1 == "use_profile" ) {
  1533. * print $2 " " domainname; domainname = ""; } } ; '
  1534. */
  1535. static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1536. {
  1537. struct list_head *pos;
  1538. bool done = true;
  1539. if (head->read_eof)
  1540. return 0;
  1541. down_read(&tomoyo_domain_list_lock);
  1542. list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
  1543. struct tomoyo_domain_info *domain;
  1544. domain = list_entry(pos, struct tomoyo_domain_info, list);
  1545. if (domain->is_deleted)
  1546. continue;
  1547. done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
  1548. domain->domainname->name);
  1549. if (!done)
  1550. break;
  1551. }
  1552. up_read(&tomoyo_domain_list_lock);
  1553. head->read_eof = done;
  1554. return 0;
  1555. }
  1556. /**
  1557. * tomoyo_write_pid: Specify PID to obtain domainname.
  1558. *
  1559. * @head: Pointer to "struct tomoyo_io_buffer".
  1560. *
  1561. * Returns 0.
  1562. */
  1563. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1564. {
  1565. unsigned long pid;
  1566. /* No error check. */
  1567. strict_strtoul(head->write_buf, 10, &pid);
  1568. head->read_step = (int) pid;
  1569. head->read_eof = false;
  1570. return 0;
  1571. }
  1572. /**
  1573. * tomoyo_read_pid - Get domainname of the specified PID.
  1574. *
  1575. * @head: Pointer to "struct tomoyo_io_buffer".
  1576. *
  1577. * Returns the domainname which the specified PID is in on success,
  1578. * empty string otherwise.
  1579. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1580. * using read()/write() interface rather than sysctl() interface.
  1581. */
  1582. static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1583. {
  1584. if (head->read_avail == 0 && !head->read_eof) {
  1585. const int pid = head->read_step;
  1586. struct task_struct *p;
  1587. struct tomoyo_domain_info *domain = NULL;
  1588. read_lock(&tasklist_lock);
  1589. p = find_task_by_vpid(pid);
  1590. if (p)
  1591. domain = tomoyo_real_domain(p);
  1592. read_unlock(&tasklist_lock);
  1593. if (domain)
  1594. tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
  1595. domain->domainname->name);
  1596. head->read_eof = true;
  1597. }
  1598. return 0;
  1599. }
  1600. /**
  1601. * tomoyo_write_exception_policy - Write exception policy.
  1602. *
  1603. * @head: Pointer to "struct tomoyo_io_buffer".
  1604. *
  1605. * Returns 0 on success, negative value otherwise.
  1606. */
  1607. static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
  1608. {
  1609. char *data = head->write_buf;
  1610. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1611. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
  1612. return tomoyo_write_domain_keeper_policy(data, false,
  1613. is_delete);
  1614. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
  1615. return tomoyo_write_domain_keeper_policy(data, true, is_delete);
  1616. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
  1617. return tomoyo_write_domain_initializer_policy(data, false,
  1618. is_delete);
  1619. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
  1620. return tomoyo_write_domain_initializer_policy(data, true,
  1621. is_delete);
  1622. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
  1623. return tomoyo_write_alias_policy(data, is_delete);
  1624. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
  1625. return tomoyo_write_globally_readable_policy(data, is_delete);
  1626. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
  1627. return tomoyo_write_pattern_policy(data, is_delete);
  1628. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
  1629. return tomoyo_write_no_rewrite_policy(data, is_delete);
  1630. return -EINVAL;
  1631. }
  1632. /**
  1633. * tomoyo_read_exception_policy - Read exception policy.
  1634. *
  1635. * @head: Pointer to "struct tomoyo_io_buffer".
  1636. *
  1637. * Returns 0 on success, -EINVAL otherwise.
  1638. */
  1639. static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
  1640. {
  1641. if (!head->read_eof) {
  1642. switch (head->read_step) {
  1643. case 0:
  1644. head->read_var2 = NULL;
  1645. head->read_step = 1;
  1646. case 1:
  1647. if (!tomoyo_read_domain_keeper_policy(head))
  1648. break;
  1649. head->read_var2 = NULL;
  1650. head->read_step = 2;
  1651. case 2:
  1652. if (!tomoyo_read_globally_readable_policy(head))
  1653. break;
  1654. head->read_var2 = NULL;
  1655. head->read_step = 3;
  1656. case 3:
  1657. head->read_var2 = NULL;
  1658. head->read_step = 4;
  1659. case 4:
  1660. if (!tomoyo_read_domain_initializer_policy(head))
  1661. break;
  1662. head->read_var2 = NULL;
  1663. head->read_step = 5;
  1664. case 5:
  1665. if (!tomoyo_read_alias_policy(head))
  1666. break;
  1667. head->read_var2 = NULL;
  1668. head->read_step = 6;
  1669. case 6:
  1670. head->read_var2 = NULL;
  1671. head->read_step = 7;
  1672. case 7:
  1673. if (!tomoyo_read_file_pattern(head))
  1674. break;
  1675. head->read_var2 = NULL;
  1676. head->read_step = 8;
  1677. case 8:
  1678. if (!tomoyo_read_no_rewrite_policy(head))
  1679. break;
  1680. head->read_var2 = NULL;
  1681. head->read_step = 9;
  1682. case 9:
  1683. head->read_eof = true;
  1684. break;
  1685. default:
  1686. return -EINVAL;
  1687. }
  1688. }
  1689. return 0;
  1690. }
  1691. /* path to policy loader */
  1692. static const char *tomoyo_loader = "/sbin/tomoyo-init";
  1693. /**
  1694. * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
  1695. *
  1696. * Returns true if /sbin/tomoyo-init exists, false otherwise.
  1697. */
  1698. static bool tomoyo_policy_loader_exists(void)
  1699. {
  1700. /*
  1701. * Don't activate MAC if the policy loader doesn't exist.
  1702. * If the initrd includes /sbin/init but real-root-dev has not
  1703. * mounted on / yet, activating MAC will block the system since
  1704. * policies are not loaded yet.
  1705. * Thus, let do_execve() call this function everytime.
  1706. */
  1707. struct path path;
  1708. if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
  1709. printk(KERN_INFO "Not activating Mandatory Access Control now "
  1710. "since %s doesn't exist.\n", tomoyo_loader);
  1711. return false;
  1712. }
  1713. path_put(&path);
  1714. return true;
  1715. }
  1716. /**
  1717. * tomoyo_load_policy - Run external policy loader to load policy.
  1718. *
  1719. * @filename: The program about to start.
  1720. *
  1721. * This function checks whether @filename is /sbin/init , and if so
  1722. * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
  1723. * and then continues invocation of /sbin/init.
  1724. * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
  1725. * writes to /sys/kernel/security/tomoyo/ interfaces.
  1726. *
  1727. * Returns nothing.
  1728. */
  1729. void tomoyo_load_policy(const char *filename)
  1730. {
  1731. char *argv[2];
  1732. char *envp[3];
  1733. if (tomoyo_policy_loaded)
  1734. return;
  1735. /*
  1736. * Check filename is /sbin/init or /sbin/tomoyo-start.
  1737. * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
  1738. * be passed.
  1739. * You can create /sbin/tomoyo-start by
  1740. * "ln -s /bin/true /sbin/tomoyo-start".
  1741. */
  1742. if (strcmp(filename, "/sbin/init") &&
  1743. strcmp(filename, "/sbin/tomoyo-start"))
  1744. return;
  1745. if (!tomoyo_policy_loader_exists())
  1746. return;
  1747. printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
  1748. tomoyo_loader);
  1749. argv[0] = (char *) tomoyo_loader;
  1750. argv[1] = NULL;
  1751. envp[0] = "HOME=/";
  1752. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  1753. envp[2] = NULL;
  1754. call_usermodehelper(argv[0], argv, envp, 1);
  1755. printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
  1756. printk(KERN_INFO "Mandatory Access Control activated.\n");
  1757. tomoyo_policy_loaded = true;
  1758. { /* Check all profiles currently assigned to domains are defined. */
  1759. struct tomoyo_domain_info *domain;
  1760. down_read(&tomoyo_domain_list_lock);
  1761. list_for_each_entry(domain, &tomoyo_domain_list, list) {
  1762. const u8 profile = domain->profile;
  1763. if (tomoyo_profile_ptr[profile])
  1764. continue;
  1765. panic("Profile %u (used by '%s') not defined.\n",
  1766. profile, domain->domainname->name);
  1767. }
  1768. up_read(&tomoyo_domain_list_lock);
  1769. }
  1770. }
  1771. /**
  1772. * tomoyo_read_version: Get version.
  1773. *
  1774. * @head: Pointer to "struct tomoyo_io_buffer".
  1775. *
  1776. * Returns version information.
  1777. */
  1778. static int tomoyo_read_version(struct tomoyo_io_buffer *head)
  1779. {
  1780. if (!head->read_eof) {
  1781. tomoyo_io_printf(head, "2.2.0");
  1782. head->read_eof = true;
  1783. }
  1784. return 0;
  1785. }
  1786. /**
  1787. * tomoyo_read_self_domain - Get the current process's domainname.
  1788. *
  1789. * @head: Pointer to "struct tomoyo_io_buffer".
  1790. *
  1791. * Returns the current process's domainname.
  1792. */
  1793. static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1794. {
  1795. if (!head->read_eof) {
  1796. /*
  1797. * tomoyo_domain()->domainname != NULL
  1798. * because every process belongs to a domain and
  1799. * the domain's name cannot be NULL.
  1800. */
  1801. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1802. head->read_eof = true;
  1803. }
  1804. return 0;
  1805. }
  1806. /**
  1807. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1808. *
  1809. * @type: Type of interface.
  1810. * @file: Pointer to "struct file".
  1811. *
  1812. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1813. */
  1814. static int tomoyo_open_control(const u8 type, struct file *file)
  1815. {
  1816. struct tomoyo_io_buffer *head = tomoyo_alloc(sizeof(*head));
  1817. if (!head)
  1818. return -ENOMEM;
  1819. mutex_init(&head->io_sem);
  1820. switch (type) {
  1821. case TOMOYO_DOMAINPOLICY:
  1822. /* /sys/kernel/security/tomoyo/domain_policy */
  1823. head->write = tomoyo_write_domain_policy;
  1824. head->read = tomoyo_read_domain_policy;
  1825. break;
  1826. case TOMOYO_EXCEPTIONPOLICY:
  1827. /* /sys/kernel/security/tomoyo/exception_policy */
  1828. head->write = tomoyo_write_exception_policy;
  1829. head->read = tomoyo_read_exception_policy;
  1830. break;
  1831. case TOMOYO_SELFDOMAIN:
  1832. /* /sys/kernel/security/tomoyo/self_domain */
  1833. head->read = tomoyo_read_self_domain;
  1834. break;
  1835. case TOMOYO_DOMAIN_STATUS:
  1836. /* /sys/kernel/security/tomoyo/.domain_status */
  1837. head->write = tomoyo_write_domain_profile;
  1838. head->read = tomoyo_read_domain_profile;
  1839. break;
  1840. case TOMOYO_PROCESS_STATUS:
  1841. /* /sys/kernel/security/tomoyo/.process_status */
  1842. head->write = tomoyo_write_pid;
  1843. head->read = tomoyo_read_pid;
  1844. break;
  1845. case TOMOYO_VERSION:
  1846. /* /sys/kernel/security/tomoyo/version */
  1847. head->read = tomoyo_read_version;
  1848. head->readbuf_size = 128;
  1849. break;
  1850. case TOMOYO_MEMINFO:
  1851. /* /sys/kernel/security/tomoyo/meminfo */
  1852. head->write = tomoyo_write_memory_quota;
  1853. head->read = tomoyo_read_memory_counter;
  1854. head->readbuf_size = 512;
  1855. break;
  1856. case TOMOYO_PROFILE:
  1857. /* /sys/kernel/security/tomoyo/profile */
  1858. head->write = tomoyo_write_profile;
  1859. head->read = tomoyo_read_profile;
  1860. break;
  1861. case TOMOYO_MANAGER:
  1862. /* /sys/kernel/security/tomoyo/manager */
  1863. head->write = tomoyo_write_manager_policy;
  1864. head->read = tomoyo_read_manager_policy;
  1865. break;
  1866. }
  1867. if (!(file->f_mode & FMODE_READ)) {
  1868. /*
  1869. * No need to allocate read_buf since it is not opened
  1870. * for reading.
  1871. */
  1872. head->read = NULL;
  1873. } else {
  1874. if (!head->readbuf_size)
  1875. head->readbuf_size = 4096 * 2;
  1876. head->read_buf = tomoyo_alloc(head->readbuf_size);
  1877. if (!head->read_buf) {
  1878. tomoyo_free(head);
  1879. return -ENOMEM;
  1880. }
  1881. }
  1882. if (!(file->f_mode & FMODE_WRITE)) {
  1883. /*
  1884. * No need to allocate write_buf since it is not opened
  1885. * for writing.
  1886. */
  1887. head->write = NULL;
  1888. } else if (head->write) {
  1889. head->writebuf_size = 4096 * 2;
  1890. head->write_buf = tomoyo_alloc(head->writebuf_size);
  1891. if (!head->write_buf) {
  1892. tomoyo_free(head->read_buf);
  1893. tomoyo_free(head);
  1894. return -ENOMEM;
  1895. }
  1896. }
  1897. file->private_data = head;
  1898. /*
  1899. * Call the handler now if the file is
  1900. * /sys/kernel/security/tomoyo/self_domain
  1901. * so that the user can use
  1902. * cat < /sys/kernel/security/tomoyo/self_domain"
  1903. * to know the current process's domainname.
  1904. */
  1905. if (type == TOMOYO_SELFDOMAIN)
  1906. tomoyo_read_control(file, NULL, 0);
  1907. return 0;
  1908. }
  1909. /**
  1910. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1911. *
  1912. * @file: Pointer to "struct file".
  1913. * @buffer: Poiner to buffer to write to.
  1914. * @buffer_len: Size of @buffer.
  1915. *
  1916. * Returns bytes read on success, negative value otherwise.
  1917. */
  1918. static int tomoyo_read_control(struct file *file, char __user *buffer,
  1919. const int buffer_len)
  1920. {
  1921. int len = 0;
  1922. struct tomoyo_io_buffer *head = file->private_data;
  1923. char *cp;
  1924. if (!head->read)
  1925. return -ENOSYS;
  1926. if (mutex_lock_interruptible(&head->io_sem))
  1927. return -EINTR;
  1928. /* Call the policy handler. */
  1929. len = head->read(head);
  1930. if (len < 0)
  1931. goto out;
  1932. /* Write to buffer. */
  1933. len = head->read_avail;
  1934. if (len > buffer_len)
  1935. len = buffer_len;
  1936. if (!len)
  1937. goto out;
  1938. /* head->read_buf changes by some functions. */
  1939. cp = head->read_buf;
  1940. if (copy_to_user(buffer, cp, len)) {
  1941. len = -EFAULT;
  1942. goto out;
  1943. }
  1944. head->read_avail -= len;
  1945. memmove(cp, cp + len, head->read_avail);
  1946. out:
  1947. mutex_unlock(&head->io_sem);
  1948. return len;
  1949. }
  1950. /**
  1951. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1952. *
  1953. * @file: Pointer to "struct file".
  1954. * @buffer: Pointer to buffer to read from.
  1955. * @buffer_len: Size of @buffer.
  1956. *
  1957. * Returns @buffer_len on success, negative value otherwise.
  1958. */
  1959. static int tomoyo_write_control(struct file *file, const char __user *buffer,
  1960. const int buffer_len)
  1961. {
  1962. struct tomoyo_io_buffer *head = file->private_data;
  1963. int error = buffer_len;
  1964. int avail_len = buffer_len;
  1965. char *cp0 = head->write_buf;
  1966. if (!head->write)
  1967. return -ENOSYS;
  1968. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1969. return -EFAULT;
  1970. /* Don't allow updating policies by non manager programs. */
  1971. if (head->write != tomoyo_write_pid &&
  1972. head->write != tomoyo_write_domain_policy &&
  1973. !tomoyo_is_policy_manager())
  1974. return -EPERM;
  1975. if (mutex_lock_interruptible(&head->io_sem))
  1976. return -EINTR;
  1977. /* Read a line and dispatch it to the policy handler. */
  1978. while (avail_len > 0) {
  1979. char c;
  1980. if (head->write_avail >= head->writebuf_size - 1) {
  1981. error = -ENOMEM;
  1982. break;
  1983. } else if (get_user(c, buffer)) {
  1984. error = -EFAULT;
  1985. break;
  1986. }
  1987. buffer++;
  1988. avail_len--;
  1989. cp0[head->write_avail++] = c;
  1990. if (c != '\n')
  1991. continue;
  1992. cp0[head->write_avail - 1] = '\0';
  1993. head->write_avail = 0;
  1994. tomoyo_normalize_line(cp0);
  1995. head->write(head);
  1996. }
  1997. mutex_unlock(&head->io_sem);
  1998. return error;
  1999. }
  2000. /**
  2001. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  2002. *
  2003. * @file: Pointer to "struct file".
  2004. *
  2005. * Releases memory and returns 0.
  2006. */
  2007. static int tomoyo_close_control(struct file *file)
  2008. {
  2009. struct tomoyo_io_buffer *head = file->private_data;
  2010. /* Release memory used for policy I/O. */
  2011. tomoyo_free(head->read_buf);
  2012. head->read_buf = NULL;
  2013. tomoyo_free(head->write_buf);
  2014. head->write_buf = NULL;
  2015. tomoyo_free(head);
  2016. head = NULL;
  2017. file->private_data = NULL;
  2018. return 0;
  2019. }
  2020. /**
  2021. * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
  2022. *
  2023. * @acl_type: Type of ACL entry.
  2024. *
  2025. * Returns pointer to the ACL entry on success, NULL otherwise.
  2026. */
  2027. void *tomoyo_alloc_acl_element(const u8 acl_type)
  2028. {
  2029. int len;
  2030. struct tomoyo_acl_info *ptr;
  2031. switch (acl_type) {
  2032. case TOMOYO_TYPE_SINGLE_PATH_ACL:
  2033. len = sizeof(struct tomoyo_single_path_acl_record);
  2034. break;
  2035. case TOMOYO_TYPE_DOUBLE_PATH_ACL:
  2036. len = sizeof(struct tomoyo_double_path_acl_record);
  2037. break;
  2038. default:
  2039. return NULL;
  2040. }
  2041. ptr = tomoyo_alloc_element(len);
  2042. if (!ptr)
  2043. return NULL;
  2044. ptr->type = acl_type;
  2045. return ptr;
  2046. }
  2047. /**
  2048. * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
  2049. *
  2050. * @inode: Pointer to "struct inode".
  2051. * @file: Pointer to "struct file".
  2052. *
  2053. * Returns 0 on success, negative value otherwise.
  2054. */
  2055. static int tomoyo_open(struct inode *inode, struct file *file)
  2056. {
  2057. const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
  2058. - ((u8 *) NULL);
  2059. return tomoyo_open_control(key, file);
  2060. }
  2061. /**
  2062. * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
  2063. *
  2064. * @inode: Pointer to "struct inode".
  2065. * @file: Pointer to "struct file".
  2066. *
  2067. * Returns 0 on success, negative value otherwise.
  2068. */
  2069. static int tomoyo_release(struct inode *inode, struct file *file)
  2070. {
  2071. return tomoyo_close_control(file);
  2072. }
  2073. /**
  2074. * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
  2075. *
  2076. * @file: Pointer to "struct file".
  2077. * @buf: Pointer to buffer.
  2078. * @count: Size of @buf.
  2079. * @ppos: Unused.
  2080. *
  2081. * Returns bytes read on success, negative value otherwise.
  2082. */
  2083. static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
  2084. loff_t *ppos)
  2085. {
  2086. return tomoyo_read_control(file, buf, count);
  2087. }
  2088. /**
  2089. * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
  2090. *
  2091. * @file: Pointer to "struct file".
  2092. * @buf: Pointer to buffer.
  2093. * @count: Size of @buf.
  2094. * @ppos: Unused.
  2095. *
  2096. * Returns @count on success, negative value otherwise.
  2097. */
  2098. static ssize_t tomoyo_write(struct file *file, const char __user *buf,
  2099. size_t count, loff_t *ppos)
  2100. {
  2101. return tomoyo_write_control(file, buf, count);
  2102. }
  2103. /*
  2104. * tomoyo_operations is a "struct file_operations" which is used for handling
  2105. * /sys/kernel/security/tomoyo/ interface.
  2106. *
  2107. * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
  2108. * See tomoyo_io_buffer for internals.
  2109. */
  2110. static const struct file_operations tomoyo_operations = {
  2111. .open = tomoyo_open,
  2112. .release = tomoyo_release,
  2113. .read = tomoyo_read,
  2114. .write = tomoyo_write,
  2115. };
  2116. /**
  2117. * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
  2118. *
  2119. * @name: The name of the interface file.
  2120. * @mode: The permission of the interface file.
  2121. * @parent: The parent directory.
  2122. * @key: Type of interface.
  2123. *
  2124. * Returns nothing.
  2125. */
  2126. static void __init tomoyo_create_entry(const char *name, const mode_t mode,
  2127. struct dentry *parent, const u8 key)
  2128. {
  2129. securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
  2130. &tomoyo_operations);
  2131. }
  2132. /**
  2133. * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
  2134. *
  2135. * Returns 0.
  2136. */
  2137. static int __init tomoyo_initerface_init(void)
  2138. {
  2139. struct dentry *tomoyo_dir;
  2140. /* Don't create securityfs entries unless registered. */
  2141. if (current_cred()->security != &tomoyo_kernel_domain)
  2142. return 0;
  2143. tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
  2144. tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
  2145. TOMOYO_DOMAINPOLICY);
  2146. tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
  2147. TOMOYO_EXCEPTIONPOLICY);
  2148. tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
  2149. TOMOYO_SELFDOMAIN);
  2150. tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
  2151. TOMOYO_DOMAIN_STATUS);
  2152. tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
  2153. TOMOYO_PROCESS_STATUS);
  2154. tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
  2155. TOMOYO_MEMINFO);
  2156. tomoyo_create_entry("profile", 0600, tomoyo_dir,
  2157. TOMOYO_PROFILE);
  2158. tomoyo_create_entry("manager", 0600, tomoyo_dir,
  2159. TOMOYO_MANAGER);
  2160. tomoyo_create_entry("version", 0400, tomoyo_dir,
  2161. TOMOYO_VERSION);
  2162. return 0;
  2163. }
  2164. fs_initcall(tomoyo_initerface_init);