common.c 56 KB

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