common.c 60 KB

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