common.c 57 KB

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