common.c 58 KB

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