common.c 58 KB

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