common.c 57 KB

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