common.c 57 KB

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