common.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  1. /*
  2. * security/tomoyo/common.c
  3. *
  4. * Common functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0 2009/04/01
  9. *
  10. */
  11. #include <linux/uaccess.h>
  12. #include <linux/security.h>
  13. #include <linux/hardirq.h>
  14. #include "realpath.h"
  15. #include "common.h"
  16. #include "tomoyo.h"
  17. /* 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 tomoyo_alloc(), so the caller must call tomoyo_free()
  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. if (ptr->type & TOMOYO_ACL_DELETED)
  802. continue;
  803. switch (tomoyo_acl_type2(ptr)) {
  804. struct tomoyo_single_path_acl_record *acl;
  805. u32 perm;
  806. u8 i;
  807. case TOMOYO_TYPE_SINGLE_PATH_ACL:
  808. acl = container_of(ptr,
  809. struct tomoyo_single_path_acl_record,
  810. head);
  811. perm = acl->perm | (((u32) acl->perm_high) << 16);
  812. for (i = 0; i < TOMOYO_MAX_SINGLE_PATH_OPERATION; i++)
  813. if (perm & (1 << i))
  814. count++;
  815. if (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL))
  816. count -= 2;
  817. break;
  818. case TOMOYO_TYPE_DOUBLE_PATH_ACL:
  819. perm = container_of(ptr,
  820. struct tomoyo_double_path_acl_record,
  821. head)->perm;
  822. for (i = 0; i < TOMOYO_MAX_DOUBLE_PATH_OPERATION; i++)
  823. if (perm & (1 << i))
  824. count++;
  825. break;
  826. }
  827. }
  828. if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
  829. return true;
  830. if (!domain->quota_warned) {
  831. domain->quota_warned = true;
  832. printk(KERN_WARNING "TOMOYO-WARNING: "
  833. "Domain '%s' has so many ACLs to hold. "
  834. "Stopped learning mode.\n", domain->domainname->name);
  835. }
  836. return false;
  837. }
  838. /**
  839. * tomoyo_find_or_assign_new_profile - Create a new profile.
  840. *
  841. * @profile: Profile number to create.
  842. *
  843. * Returns pointer to "struct tomoyo_profile" on success, NULL otherwise.
  844. */
  845. static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
  846. int profile)
  847. {
  848. static DEFINE_MUTEX(lock);
  849. struct tomoyo_profile *ptr = NULL;
  850. int i;
  851. if (profile >= TOMOYO_MAX_PROFILES)
  852. return NULL;
  853. mutex_lock(&lock);
  854. ptr = tomoyo_profile_ptr[profile];
  855. if (ptr)
  856. goto ok;
  857. ptr = tomoyo_alloc_element(sizeof(*ptr));
  858. if (!ptr)
  859. goto ok;
  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. profile->comment = tomoyo_save_name(cp + 1);
  899. return 0;
  900. }
  901. for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++) {
  902. if (strcmp(data, tomoyo_control_array[i].keyword))
  903. continue;
  904. if (sscanf(cp + 1, "%u", &value) != 1) {
  905. int j;
  906. const char **modes;
  907. switch (i) {
  908. case TOMOYO_VERBOSE:
  909. modes = tomoyo_mode_2;
  910. break;
  911. default:
  912. modes = tomoyo_mode_4;
  913. break;
  914. }
  915. for (j = 0; j < 4; j++) {
  916. if (strcmp(cp + 1, modes[j]))
  917. continue;
  918. value = j;
  919. break;
  920. }
  921. if (j == 4)
  922. return -EINVAL;
  923. } else if (value > tomoyo_control_array[i].max_value) {
  924. value = tomoyo_control_array[i].max_value;
  925. }
  926. profile->value[i] = value;
  927. return 0;
  928. }
  929. return -EINVAL;
  930. }
  931. /**
  932. * tomoyo_read_profile - Read from profile table.
  933. *
  934. * @head: Pointer to "struct tomoyo_io_buffer".
  935. *
  936. * Returns 0.
  937. */
  938. static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
  939. {
  940. static const int total = TOMOYO_MAX_CONTROL_INDEX + 1;
  941. int step;
  942. if (head->read_eof)
  943. return 0;
  944. for (step = head->read_step; step < TOMOYO_MAX_PROFILES * total;
  945. step++) {
  946. const u8 index = step / total;
  947. u8 type = step % total;
  948. const struct tomoyo_profile *profile
  949. = tomoyo_profile_ptr[index];
  950. head->read_step = step;
  951. if (!profile)
  952. continue;
  953. if (!type) { /* Print profile' comment tag. */
  954. if (!tomoyo_io_printf(head, "%u-COMMENT=%s\n",
  955. index, profile->comment ?
  956. profile->comment->name : ""))
  957. break;
  958. continue;
  959. }
  960. type--;
  961. if (type < TOMOYO_MAX_CONTROL_INDEX) {
  962. const unsigned int value = profile->value[type];
  963. const char **modes = NULL;
  964. const char *keyword
  965. = tomoyo_control_array[type].keyword;
  966. switch (tomoyo_control_array[type].max_value) {
  967. case 3:
  968. modes = tomoyo_mode_4;
  969. break;
  970. case 1:
  971. modes = tomoyo_mode_2;
  972. break;
  973. }
  974. if (modes) {
  975. if (!tomoyo_io_printf(head, "%u-%s=%s\n", index,
  976. keyword, modes[value]))
  977. break;
  978. } else {
  979. if (!tomoyo_io_printf(head, "%u-%s=%u\n", index,
  980. keyword, value))
  981. break;
  982. }
  983. }
  984. }
  985. if (step == TOMOYO_MAX_PROFILES * total)
  986. head->read_eof = true;
  987. return 0;
  988. }
  989. /*
  990. * tomoyo_policy_manager_entry is a structure which is used for holding list of
  991. * domainnames or programs which are permitted to modify configuration via
  992. * /sys/kernel/security/tomoyo/ interface.
  993. * It has following fields.
  994. *
  995. * (1) "list" which is linked to tomoyo_policy_manager_list .
  996. * (2) "manager" is a domainname or a program's pathname.
  997. * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
  998. * otherwise.
  999. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  1000. * otherwise.
  1001. */
  1002. struct tomoyo_policy_manager_entry {
  1003. struct list_head list;
  1004. /* A path to program or a domainname. */
  1005. const struct tomoyo_path_info *manager;
  1006. bool is_domain; /* True if manager is a domainname. */
  1007. bool is_deleted; /* True if this entry is deleted. */
  1008. };
  1009. /*
  1010. * tomoyo_policy_manager_list is used for holding list of domainnames or
  1011. * programs which are permitted to modify configuration via
  1012. * /sys/kernel/security/tomoyo/ interface.
  1013. *
  1014. * An entry is added by
  1015. *
  1016. * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
  1017. * /sys/kernel/security/tomoyo/manager
  1018. * (if you want to specify by a domainname)
  1019. *
  1020. * or
  1021. *
  1022. * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
  1023. * (if you want to specify by a program's location)
  1024. *
  1025. * and is deleted by
  1026. *
  1027. * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
  1028. * /sys/kernel/security/tomoyo/manager
  1029. *
  1030. * or
  1031. *
  1032. * # echo 'delete /usr/lib/ccs/editpolicy' > \
  1033. * /sys/kernel/security/tomoyo/manager
  1034. *
  1035. * and all entries are retrieved by
  1036. *
  1037. * # cat /sys/kernel/security/tomoyo/manager
  1038. */
  1039. static LIST_HEAD(tomoyo_policy_manager_list);
  1040. /**
  1041. * tomoyo_update_manager_entry - Add a manager entry.
  1042. *
  1043. * @manager: The path to manager or the domainnamme.
  1044. * @is_delete: True if it is a delete request.
  1045. *
  1046. * Returns 0 on success, negative value otherwise.
  1047. *
  1048. * Caller holds tomoyo_read_lock().
  1049. */
  1050. static int tomoyo_update_manager_entry(const char *manager,
  1051. const bool is_delete)
  1052. {
  1053. struct tomoyo_policy_manager_entry *new_entry;
  1054. struct tomoyo_policy_manager_entry *ptr;
  1055. const struct tomoyo_path_info *saved_manager;
  1056. int error = -ENOMEM;
  1057. bool is_domain = false;
  1058. if (tomoyo_is_domain_def(manager)) {
  1059. if (!tomoyo_is_correct_domain(manager, __func__))
  1060. return -EINVAL;
  1061. is_domain = true;
  1062. } else {
  1063. if (!tomoyo_is_correct_path(manager, 1, -1, -1, __func__))
  1064. return -EINVAL;
  1065. }
  1066. saved_manager = tomoyo_save_name(manager);
  1067. if (!saved_manager)
  1068. return -ENOMEM;
  1069. mutex_lock(&tomoyo_policy_lock);
  1070. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1071. if (ptr->manager != saved_manager)
  1072. continue;
  1073. ptr->is_deleted = is_delete;
  1074. error = 0;
  1075. goto out;
  1076. }
  1077. if (is_delete) {
  1078. error = -ENOENT;
  1079. goto out;
  1080. }
  1081. new_entry = tomoyo_alloc_element(sizeof(*new_entry));
  1082. if (!new_entry)
  1083. goto out;
  1084. new_entry->manager = saved_manager;
  1085. new_entry->is_domain = is_domain;
  1086. list_add_tail_rcu(&new_entry->list, &tomoyo_policy_manager_list);
  1087. error = 0;
  1088. out:
  1089. mutex_unlock(&tomoyo_policy_lock);
  1090. return error;
  1091. }
  1092. /**
  1093. * tomoyo_write_manager_policy - Write manager policy.
  1094. *
  1095. * @head: Pointer to "struct tomoyo_io_buffer".
  1096. *
  1097. * Returns 0 on success, negative value otherwise.
  1098. *
  1099. * Caller holds tomoyo_read_lock().
  1100. */
  1101. static int tomoyo_write_manager_policy(struct tomoyo_io_buffer *head)
  1102. {
  1103. char *data = head->write_buf;
  1104. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1105. if (!strcmp(data, "manage_by_non_root")) {
  1106. tomoyo_manage_by_non_root = !is_delete;
  1107. return 0;
  1108. }
  1109. return tomoyo_update_manager_entry(data, is_delete);
  1110. }
  1111. /**
  1112. * tomoyo_read_manager_policy - Read manager policy.
  1113. *
  1114. * @head: Pointer to "struct tomoyo_io_buffer".
  1115. *
  1116. * Returns 0.
  1117. *
  1118. * Caller holds tomoyo_read_lock().
  1119. */
  1120. static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
  1121. {
  1122. struct list_head *pos;
  1123. bool done = true;
  1124. if (head->read_eof)
  1125. return 0;
  1126. list_for_each_cookie(pos, head->read_var2,
  1127. &tomoyo_policy_manager_list) {
  1128. struct tomoyo_policy_manager_entry *ptr;
  1129. ptr = list_entry(pos, struct tomoyo_policy_manager_entry,
  1130. list);
  1131. if (ptr->is_deleted)
  1132. continue;
  1133. done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
  1134. if (!done)
  1135. break;
  1136. }
  1137. head->read_eof = done;
  1138. return 0;
  1139. }
  1140. /**
  1141. * tomoyo_is_policy_manager - Check whether the current process is a policy manager.
  1142. *
  1143. * Returns true if the current process is permitted to modify policy
  1144. * via /sys/kernel/security/tomoyo/ interface.
  1145. *
  1146. * Caller holds tomoyo_read_lock().
  1147. */
  1148. static bool tomoyo_is_policy_manager(void)
  1149. {
  1150. struct tomoyo_policy_manager_entry *ptr;
  1151. const char *exe;
  1152. const struct task_struct *task = current;
  1153. const struct tomoyo_path_info *domainname = tomoyo_domain()->domainname;
  1154. bool found = false;
  1155. if (!tomoyo_policy_loaded)
  1156. return true;
  1157. if (!tomoyo_manage_by_non_root && (task->cred->uid || task->cred->euid))
  1158. return false;
  1159. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1160. if (!ptr->is_deleted && ptr->is_domain
  1161. && !tomoyo_pathcmp(domainname, ptr->manager)) {
  1162. found = true;
  1163. break;
  1164. }
  1165. }
  1166. if (found)
  1167. return true;
  1168. exe = tomoyo_get_exe();
  1169. if (!exe)
  1170. return false;
  1171. list_for_each_entry_rcu(ptr, &tomoyo_policy_manager_list, list) {
  1172. if (!ptr->is_deleted && !ptr->is_domain
  1173. && !strcmp(exe, ptr->manager->name)) {
  1174. found = true;
  1175. break;
  1176. }
  1177. }
  1178. if (!found) { /* Reduce error messages. */
  1179. static pid_t last_pid;
  1180. const pid_t pid = current->pid;
  1181. if (last_pid != pid) {
  1182. printk(KERN_WARNING "%s ( %s ) is not permitted to "
  1183. "update policies.\n", domainname->name, exe);
  1184. last_pid = pid;
  1185. }
  1186. }
  1187. tomoyo_free(exe);
  1188. return found;
  1189. }
  1190. /**
  1191. * tomoyo_is_select_one - Parse select command.
  1192. *
  1193. * @head: Pointer to "struct tomoyo_io_buffer".
  1194. * @data: String to parse.
  1195. *
  1196. * Returns true on success, false otherwise.
  1197. *
  1198. * Caller holds tomoyo_read_lock().
  1199. */
  1200. static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
  1201. const char *data)
  1202. {
  1203. unsigned int pid;
  1204. struct tomoyo_domain_info *domain = NULL;
  1205. if (sscanf(data, "pid=%u", &pid) == 1) {
  1206. struct task_struct *p;
  1207. read_lock(&tasklist_lock);
  1208. p = find_task_by_vpid(pid);
  1209. if (p)
  1210. domain = tomoyo_real_domain(p);
  1211. read_unlock(&tasklist_lock);
  1212. } else if (!strncmp(data, "domain=", 7)) {
  1213. if (tomoyo_is_domain_def(data + 7))
  1214. domain = tomoyo_find_domain(data + 7);
  1215. } else
  1216. return false;
  1217. head->write_var1 = domain;
  1218. /* Accessing read_buf is safe because head->io_sem is held. */
  1219. if (!head->read_buf)
  1220. return true; /* Do nothing if open(O_WRONLY). */
  1221. head->read_avail = 0;
  1222. tomoyo_io_printf(head, "# select %s\n", data);
  1223. head->read_single_domain = true;
  1224. head->read_eof = !domain;
  1225. if (domain) {
  1226. struct tomoyo_domain_info *d;
  1227. head->read_var1 = NULL;
  1228. list_for_each_entry_rcu(d, &tomoyo_domain_list, list) {
  1229. if (d == domain)
  1230. break;
  1231. head->read_var1 = &d->list;
  1232. }
  1233. head->read_var2 = NULL;
  1234. head->read_bit = 0;
  1235. head->read_step = 0;
  1236. if (domain->is_deleted)
  1237. tomoyo_io_printf(head, "# This is a deleted domain.\n");
  1238. }
  1239. return true;
  1240. }
  1241. /**
  1242. * tomoyo_delete_domain - Delete a domain.
  1243. *
  1244. * @domainname: The name of domain.
  1245. *
  1246. * Returns 0.
  1247. *
  1248. * Caller holds tomoyo_read_lock().
  1249. */
  1250. static int tomoyo_delete_domain(char *domainname)
  1251. {
  1252. struct tomoyo_domain_info *domain;
  1253. struct tomoyo_path_info name;
  1254. name.name = domainname;
  1255. tomoyo_fill_path_info(&name);
  1256. mutex_lock(&tomoyo_policy_lock);
  1257. /* Is there an active domain? */
  1258. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1259. /* Never delete tomoyo_kernel_domain */
  1260. if (domain == &tomoyo_kernel_domain)
  1261. continue;
  1262. if (domain->is_deleted ||
  1263. tomoyo_pathcmp(domain->domainname, &name))
  1264. continue;
  1265. domain->is_deleted = true;
  1266. break;
  1267. }
  1268. mutex_unlock(&tomoyo_policy_lock);
  1269. return 0;
  1270. }
  1271. /**
  1272. * tomoyo_write_domain_policy - Write domain policy.
  1273. *
  1274. * @head: Pointer to "struct tomoyo_io_buffer".
  1275. *
  1276. * Returns 0 on success, negative value otherwise.
  1277. *
  1278. * Caller holds tomoyo_read_lock().
  1279. */
  1280. static int tomoyo_write_domain_policy(struct tomoyo_io_buffer *head)
  1281. {
  1282. char *data = head->write_buf;
  1283. struct tomoyo_domain_info *domain = head->write_var1;
  1284. bool is_delete = false;
  1285. bool is_select = false;
  1286. unsigned int profile;
  1287. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE))
  1288. is_delete = true;
  1289. else if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_SELECT))
  1290. is_select = true;
  1291. if (is_select && tomoyo_is_select_one(head, data))
  1292. return 0;
  1293. /* Don't allow updating policies by non manager programs. */
  1294. if (!tomoyo_is_policy_manager())
  1295. return -EPERM;
  1296. if (tomoyo_is_domain_def(data)) {
  1297. domain = NULL;
  1298. if (is_delete)
  1299. tomoyo_delete_domain(data);
  1300. else if (is_select)
  1301. domain = tomoyo_find_domain(data);
  1302. else
  1303. domain = tomoyo_find_or_assign_new_domain(data, 0);
  1304. head->write_var1 = domain;
  1305. return 0;
  1306. }
  1307. if (!domain)
  1308. return -EINVAL;
  1309. if (sscanf(data, TOMOYO_KEYWORD_USE_PROFILE "%u", &profile) == 1
  1310. && profile < TOMOYO_MAX_PROFILES) {
  1311. if (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded)
  1312. domain->profile = (u8) profile;
  1313. return 0;
  1314. }
  1315. if (!strcmp(data, TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ)) {
  1316. tomoyo_set_domain_flag(domain, is_delete,
  1317. TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ);
  1318. return 0;
  1319. }
  1320. return tomoyo_write_file_policy(data, domain, is_delete);
  1321. }
  1322. /**
  1323. * tomoyo_print_single_path_acl - Print a single path ACL entry.
  1324. *
  1325. * @head: Pointer to "struct tomoyo_io_buffer".
  1326. * @ptr: Pointer to "struct tomoyo_single_path_acl_record".
  1327. *
  1328. * Returns true on success, false otherwise.
  1329. */
  1330. static bool tomoyo_print_single_path_acl(struct tomoyo_io_buffer *head,
  1331. struct tomoyo_single_path_acl_record *
  1332. ptr)
  1333. {
  1334. int pos;
  1335. u8 bit;
  1336. const char *atmark = "";
  1337. const char *filename;
  1338. const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
  1339. filename = ptr->filename->name;
  1340. for (bit = head->read_bit; bit < TOMOYO_MAX_SINGLE_PATH_OPERATION;
  1341. bit++) {
  1342. const char *msg;
  1343. if (!(perm & (1 << bit)))
  1344. continue;
  1345. /* Print "read/write" instead of "read" and "write". */
  1346. if ((bit == TOMOYO_TYPE_READ_ACL ||
  1347. bit == TOMOYO_TYPE_WRITE_ACL)
  1348. && (perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
  1349. continue;
  1350. msg = tomoyo_sp2keyword(bit);
  1351. pos = head->read_avail;
  1352. if (!tomoyo_io_printf(head, "allow_%s %s%s\n", msg,
  1353. atmark, filename))
  1354. goto out;
  1355. }
  1356. head->read_bit = 0;
  1357. return true;
  1358. out:
  1359. head->read_bit = bit;
  1360. head->read_avail = pos;
  1361. return false;
  1362. }
  1363. /**
  1364. * tomoyo_print_double_path_acl - Print a double path ACL entry.
  1365. *
  1366. * @head: Pointer to "struct tomoyo_io_buffer".
  1367. * @ptr: Pointer to "struct tomoyo_double_path_acl_record".
  1368. *
  1369. * Returns true on success, false otherwise.
  1370. */
  1371. static bool tomoyo_print_double_path_acl(struct tomoyo_io_buffer *head,
  1372. struct tomoyo_double_path_acl_record *
  1373. ptr)
  1374. {
  1375. int pos;
  1376. const char *atmark1 = "";
  1377. const char *atmark2 = "";
  1378. const char *filename1;
  1379. const char *filename2;
  1380. const u8 perm = ptr->perm;
  1381. u8 bit;
  1382. filename1 = ptr->filename1->name;
  1383. filename2 = ptr->filename2->name;
  1384. for (bit = head->read_bit; bit < TOMOYO_MAX_DOUBLE_PATH_OPERATION;
  1385. bit++) {
  1386. const char *msg;
  1387. if (!(perm & (1 << bit)))
  1388. continue;
  1389. msg = tomoyo_dp2keyword(bit);
  1390. pos = head->read_avail;
  1391. if (!tomoyo_io_printf(head, "allow_%s %s%s %s%s\n", msg,
  1392. atmark1, filename1, atmark2, filename2))
  1393. goto out;
  1394. }
  1395. head->read_bit = 0;
  1396. return true;
  1397. out:
  1398. head->read_bit = bit;
  1399. head->read_avail = pos;
  1400. return false;
  1401. }
  1402. /**
  1403. * tomoyo_print_entry - Print an ACL entry.
  1404. *
  1405. * @head: Pointer to "struct tomoyo_io_buffer".
  1406. * @ptr: Pointer to an ACL entry.
  1407. *
  1408. * Returns true on success, false otherwise.
  1409. */
  1410. static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
  1411. struct tomoyo_acl_info *ptr)
  1412. {
  1413. const u8 acl_type = tomoyo_acl_type2(ptr);
  1414. if (acl_type & TOMOYO_ACL_DELETED)
  1415. return true;
  1416. if (acl_type == TOMOYO_TYPE_SINGLE_PATH_ACL) {
  1417. struct tomoyo_single_path_acl_record *acl
  1418. = container_of(ptr,
  1419. struct tomoyo_single_path_acl_record,
  1420. head);
  1421. return tomoyo_print_single_path_acl(head, acl);
  1422. }
  1423. if (acl_type == TOMOYO_TYPE_DOUBLE_PATH_ACL) {
  1424. struct tomoyo_double_path_acl_record *acl
  1425. = container_of(ptr,
  1426. struct tomoyo_double_path_acl_record,
  1427. head);
  1428. return tomoyo_print_double_path_acl(head, acl);
  1429. }
  1430. BUG(); /* This must not happen. */
  1431. return false;
  1432. }
  1433. /**
  1434. * tomoyo_read_domain_policy - Read domain policy.
  1435. *
  1436. * @head: Pointer to "struct tomoyo_io_buffer".
  1437. *
  1438. * Returns 0.
  1439. *
  1440. * Caller holds tomoyo_read_lock().
  1441. */
  1442. static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
  1443. {
  1444. struct list_head *dpos;
  1445. struct list_head *apos;
  1446. bool done = true;
  1447. if (head->read_eof)
  1448. return 0;
  1449. if (head->read_step == 0)
  1450. head->read_step = 1;
  1451. list_for_each_cookie(dpos, head->read_var1, &tomoyo_domain_list) {
  1452. struct tomoyo_domain_info *domain;
  1453. const char *quota_exceeded = "";
  1454. const char *transition_failed = "";
  1455. const char *ignore_global_allow_read = "";
  1456. domain = list_entry(dpos, struct tomoyo_domain_info, list);
  1457. if (head->read_step != 1)
  1458. goto acl_loop;
  1459. if (domain->is_deleted && !head->read_single_domain)
  1460. continue;
  1461. /* Print domainname and flags. */
  1462. if (domain->quota_warned)
  1463. quota_exceeded = "quota_exceeded\n";
  1464. if (domain->flags & TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED)
  1465. transition_failed = "transition_failed\n";
  1466. if (domain->flags &
  1467. TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
  1468. ignore_global_allow_read
  1469. = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
  1470. done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
  1471. "%u\n%s%s%s\n",
  1472. domain->domainname->name,
  1473. domain->profile, quota_exceeded,
  1474. transition_failed,
  1475. ignore_global_allow_read);
  1476. if (!done)
  1477. break;
  1478. head->read_step = 2;
  1479. acl_loop:
  1480. if (head->read_step == 3)
  1481. goto tail_mark;
  1482. /* Print ACL entries in the domain. */
  1483. list_for_each_cookie(apos, head->read_var2,
  1484. &domain->acl_info_list) {
  1485. struct tomoyo_acl_info *ptr
  1486. = list_entry(apos, struct tomoyo_acl_info,
  1487. list);
  1488. done = tomoyo_print_entry(head, ptr);
  1489. if (!done)
  1490. break;
  1491. }
  1492. if (!done)
  1493. break;
  1494. head->read_step = 3;
  1495. tail_mark:
  1496. done = tomoyo_io_printf(head, "\n");
  1497. if (!done)
  1498. break;
  1499. head->read_step = 1;
  1500. if (head->read_single_domain)
  1501. break;
  1502. }
  1503. head->read_eof = done;
  1504. return 0;
  1505. }
  1506. /**
  1507. * tomoyo_write_domain_profile - Assign profile for specified domain.
  1508. *
  1509. * @head: Pointer to "struct tomoyo_io_buffer".
  1510. *
  1511. * Returns 0 on success, -EINVAL otherwise.
  1512. *
  1513. * This is equivalent to doing
  1514. *
  1515. * ( echo "select " $domainname; echo "use_profile " $profile ) |
  1516. * /usr/lib/ccs/loadpolicy -d
  1517. *
  1518. * Caller holds tomoyo_read_lock().
  1519. */
  1520. static int tomoyo_write_domain_profile(struct tomoyo_io_buffer *head)
  1521. {
  1522. char *data = head->write_buf;
  1523. char *cp = strchr(data, ' ');
  1524. struct tomoyo_domain_info *domain;
  1525. unsigned long profile;
  1526. if (!cp)
  1527. return -EINVAL;
  1528. *cp = '\0';
  1529. domain = tomoyo_find_domain(cp + 1);
  1530. if (strict_strtoul(data, 10, &profile))
  1531. return -EINVAL;
  1532. if (domain && profile < TOMOYO_MAX_PROFILES
  1533. && (tomoyo_profile_ptr[profile] || !tomoyo_policy_loaded))
  1534. domain->profile = (u8) profile;
  1535. return 0;
  1536. }
  1537. /**
  1538. * tomoyo_read_domain_profile - Read only domainname and profile.
  1539. *
  1540. * @head: Pointer to "struct tomoyo_io_buffer".
  1541. *
  1542. * Returns list of profile number and domainname pairs.
  1543. *
  1544. * This is equivalent to doing
  1545. *
  1546. * grep -A 1 '^<kernel>' /sys/kernel/security/tomoyo/domain_policy |
  1547. * awk ' { if ( domainname == "" ) { if ( $1 == "<kernel>" )
  1548. * domainname = $0; } else if ( $1 == "use_profile" ) {
  1549. * print $2 " " domainname; domainname = ""; } } ; '
  1550. *
  1551. * Caller holds tomoyo_read_lock().
  1552. */
  1553. static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
  1554. {
  1555. struct list_head *pos;
  1556. bool done = true;
  1557. if (head->read_eof)
  1558. return 0;
  1559. list_for_each_cookie(pos, head->read_var1, &tomoyo_domain_list) {
  1560. struct tomoyo_domain_info *domain;
  1561. domain = list_entry(pos, struct tomoyo_domain_info, list);
  1562. if (domain->is_deleted)
  1563. continue;
  1564. done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
  1565. domain->domainname->name);
  1566. if (!done)
  1567. break;
  1568. }
  1569. head->read_eof = done;
  1570. return 0;
  1571. }
  1572. /**
  1573. * tomoyo_write_pid: Specify PID to obtain domainname.
  1574. *
  1575. * @head: Pointer to "struct tomoyo_io_buffer".
  1576. *
  1577. * Returns 0.
  1578. */
  1579. static int tomoyo_write_pid(struct tomoyo_io_buffer *head)
  1580. {
  1581. unsigned long pid;
  1582. /* No error check. */
  1583. strict_strtoul(head->write_buf, 10, &pid);
  1584. head->read_step = (int) pid;
  1585. head->read_eof = false;
  1586. return 0;
  1587. }
  1588. /**
  1589. * tomoyo_read_pid - Get domainname of the specified PID.
  1590. *
  1591. * @head: Pointer to "struct tomoyo_io_buffer".
  1592. *
  1593. * Returns the domainname which the specified PID is in on success,
  1594. * empty string otherwise.
  1595. * The PID is specified by tomoyo_write_pid() so that the user can obtain
  1596. * using read()/write() interface rather than sysctl() interface.
  1597. */
  1598. static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
  1599. {
  1600. if (head->read_avail == 0 && !head->read_eof) {
  1601. const int pid = head->read_step;
  1602. struct task_struct *p;
  1603. struct tomoyo_domain_info *domain = NULL;
  1604. read_lock(&tasklist_lock);
  1605. p = find_task_by_vpid(pid);
  1606. if (p)
  1607. domain = tomoyo_real_domain(p);
  1608. read_unlock(&tasklist_lock);
  1609. if (domain)
  1610. tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
  1611. domain->domainname->name);
  1612. head->read_eof = true;
  1613. }
  1614. return 0;
  1615. }
  1616. /**
  1617. * tomoyo_write_exception_policy - Write exception policy.
  1618. *
  1619. * @head: Pointer to "struct tomoyo_io_buffer".
  1620. *
  1621. * Returns 0 on success, negative value otherwise.
  1622. *
  1623. * Caller holds tomoyo_read_lock().
  1624. */
  1625. static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
  1626. {
  1627. char *data = head->write_buf;
  1628. bool is_delete = tomoyo_str_starts(&data, TOMOYO_KEYWORD_DELETE);
  1629. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_KEEP_DOMAIN))
  1630. return tomoyo_write_domain_keeper_policy(data, false,
  1631. is_delete);
  1632. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_KEEP_DOMAIN))
  1633. return tomoyo_write_domain_keeper_policy(data, true, is_delete);
  1634. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_INITIALIZE_DOMAIN))
  1635. return tomoyo_write_domain_initializer_policy(data, false,
  1636. is_delete);
  1637. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN))
  1638. return tomoyo_write_domain_initializer_policy(data, true,
  1639. is_delete);
  1640. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALIAS))
  1641. return tomoyo_write_alias_policy(data, is_delete);
  1642. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_ALLOW_READ))
  1643. return tomoyo_write_globally_readable_policy(data, is_delete);
  1644. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_FILE_PATTERN))
  1645. return tomoyo_write_pattern_policy(data, is_delete);
  1646. if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
  1647. return tomoyo_write_no_rewrite_policy(data, is_delete);
  1648. return -EINVAL;
  1649. }
  1650. /**
  1651. * tomoyo_read_exception_policy - Read exception policy.
  1652. *
  1653. * @head: Pointer to "struct tomoyo_io_buffer".
  1654. *
  1655. * Returns 0 on success, -EINVAL otherwise.
  1656. *
  1657. * Caller holds tomoyo_read_lock().
  1658. */
  1659. static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
  1660. {
  1661. if (!head->read_eof) {
  1662. switch (head->read_step) {
  1663. case 0:
  1664. head->read_var2 = NULL;
  1665. head->read_step = 1;
  1666. case 1:
  1667. if (!tomoyo_read_domain_keeper_policy(head))
  1668. break;
  1669. head->read_var2 = NULL;
  1670. head->read_step = 2;
  1671. case 2:
  1672. if (!tomoyo_read_globally_readable_policy(head))
  1673. break;
  1674. head->read_var2 = NULL;
  1675. head->read_step = 3;
  1676. case 3:
  1677. head->read_var2 = NULL;
  1678. head->read_step = 4;
  1679. case 4:
  1680. if (!tomoyo_read_domain_initializer_policy(head))
  1681. break;
  1682. head->read_var2 = NULL;
  1683. head->read_step = 5;
  1684. case 5:
  1685. if (!tomoyo_read_alias_policy(head))
  1686. break;
  1687. head->read_var2 = NULL;
  1688. head->read_step = 6;
  1689. case 6:
  1690. head->read_var2 = NULL;
  1691. head->read_step = 7;
  1692. case 7:
  1693. if (!tomoyo_read_file_pattern(head))
  1694. break;
  1695. head->read_var2 = NULL;
  1696. head->read_step = 8;
  1697. case 8:
  1698. if (!tomoyo_read_no_rewrite_policy(head))
  1699. break;
  1700. head->read_var2 = NULL;
  1701. head->read_step = 9;
  1702. case 9:
  1703. head->read_eof = true;
  1704. break;
  1705. default:
  1706. return -EINVAL;
  1707. }
  1708. }
  1709. return 0;
  1710. }
  1711. /* path to policy loader */
  1712. static const char *tomoyo_loader = "/sbin/tomoyo-init";
  1713. /**
  1714. * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
  1715. *
  1716. * Returns true if /sbin/tomoyo-init exists, false otherwise.
  1717. */
  1718. static bool tomoyo_policy_loader_exists(void)
  1719. {
  1720. /*
  1721. * Don't activate MAC if the policy loader doesn't exist.
  1722. * If the initrd includes /sbin/init but real-root-dev has not
  1723. * mounted on / yet, activating MAC will block the system since
  1724. * policies are not loaded yet.
  1725. * Thus, let do_execve() call this function everytime.
  1726. */
  1727. struct path path;
  1728. if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
  1729. printk(KERN_INFO "Not activating Mandatory Access Control now "
  1730. "since %s doesn't exist.\n", tomoyo_loader);
  1731. return false;
  1732. }
  1733. path_put(&path);
  1734. return true;
  1735. }
  1736. /**
  1737. * tomoyo_load_policy - Run external policy loader to load policy.
  1738. *
  1739. * @filename: The program about to start.
  1740. *
  1741. * This function checks whether @filename is /sbin/init , and if so
  1742. * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
  1743. * and then continues invocation of /sbin/init.
  1744. * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
  1745. * writes to /sys/kernel/security/tomoyo/ interfaces.
  1746. *
  1747. * Returns nothing.
  1748. */
  1749. void tomoyo_load_policy(const char *filename)
  1750. {
  1751. char *argv[2];
  1752. char *envp[3];
  1753. if (tomoyo_policy_loaded)
  1754. return;
  1755. /*
  1756. * Check filename is /sbin/init or /sbin/tomoyo-start.
  1757. * /sbin/tomoyo-start is a dummy filename in case where /sbin/init can't
  1758. * be passed.
  1759. * You can create /sbin/tomoyo-start by
  1760. * "ln -s /bin/true /sbin/tomoyo-start".
  1761. */
  1762. if (strcmp(filename, "/sbin/init") &&
  1763. strcmp(filename, "/sbin/tomoyo-start"))
  1764. return;
  1765. if (!tomoyo_policy_loader_exists())
  1766. return;
  1767. printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
  1768. tomoyo_loader);
  1769. argv[0] = (char *) tomoyo_loader;
  1770. argv[1] = NULL;
  1771. envp[0] = "HOME=/";
  1772. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  1773. envp[2] = NULL;
  1774. call_usermodehelper(argv[0], argv, envp, 1);
  1775. printk(KERN_INFO "TOMOYO: 2.2.0 2009/04/01\n");
  1776. printk(KERN_INFO "Mandatory Access Control activated.\n");
  1777. tomoyo_policy_loaded = true;
  1778. { /* Check all profiles currently assigned to domains are defined. */
  1779. struct tomoyo_domain_info *domain;
  1780. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  1781. const u8 profile = domain->profile;
  1782. if (tomoyo_profile_ptr[profile])
  1783. continue;
  1784. panic("Profile %u (used by '%s') not defined.\n",
  1785. profile, domain->domainname->name);
  1786. }
  1787. }
  1788. }
  1789. /**
  1790. * tomoyo_read_version: Get version.
  1791. *
  1792. * @head: Pointer to "struct tomoyo_io_buffer".
  1793. *
  1794. * Returns version information.
  1795. */
  1796. static int tomoyo_read_version(struct tomoyo_io_buffer *head)
  1797. {
  1798. if (!head->read_eof) {
  1799. tomoyo_io_printf(head, "2.2.0");
  1800. head->read_eof = true;
  1801. }
  1802. return 0;
  1803. }
  1804. /**
  1805. * tomoyo_read_self_domain - Get the current process's domainname.
  1806. *
  1807. * @head: Pointer to "struct tomoyo_io_buffer".
  1808. *
  1809. * Returns the current process's domainname.
  1810. */
  1811. static int tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
  1812. {
  1813. if (!head->read_eof) {
  1814. /*
  1815. * tomoyo_domain()->domainname != NULL
  1816. * because every process belongs to a domain and
  1817. * the domain's name cannot be NULL.
  1818. */
  1819. tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
  1820. head->read_eof = true;
  1821. }
  1822. return 0;
  1823. }
  1824. /**
  1825. * tomoyo_open_control - open() for /sys/kernel/security/tomoyo/ interface.
  1826. *
  1827. * @type: Type of interface.
  1828. * @file: Pointer to "struct file".
  1829. *
  1830. * Associates policy handler and returns 0 on success, -ENOMEM otherwise.
  1831. *
  1832. * Caller acquires tomoyo_read_lock().
  1833. */
  1834. static int tomoyo_open_control(const u8 type, struct file *file)
  1835. {
  1836. struct tomoyo_io_buffer *head = tomoyo_alloc(sizeof(*head));
  1837. if (!head)
  1838. return -ENOMEM;
  1839. mutex_init(&head->io_sem);
  1840. switch (type) {
  1841. case TOMOYO_DOMAINPOLICY:
  1842. /* /sys/kernel/security/tomoyo/domain_policy */
  1843. head->write = tomoyo_write_domain_policy;
  1844. head->read = tomoyo_read_domain_policy;
  1845. break;
  1846. case TOMOYO_EXCEPTIONPOLICY:
  1847. /* /sys/kernel/security/tomoyo/exception_policy */
  1848. head->write = tomoyo_write_exception_policy;
  1849. head->read = tomoyo_read_exception_policy;
  1850. break;
  1851. case TOMOYO_SELFDOMAIN:
  1852. /* /sys/kernel/security/tomoyo/self_domain */
  1853. head->read = tomoyo_read_self_domain;
  1854. break;
  1855. case TOMOYO_DOMAIN_STATUS:
  1856. /* /sys/kernel/security/tomoyo/.domain_status */
  1857. head->write = tomoyo_write_domain_profile;
  1858. head->read = tomoyo_read_domain_profile;
  1859. break;
  1860. case TOMOYO_PROCESS_STATUS:
  1861. /* /sys/kernel/security/tomoyo/.process_status */
  1862. head->write = tomoyo_write_pid;
  1863. head->read = tomoyo_read_pid;
  1864. break;
  1865. case TOMOYO_VERSION:
  1866. /* /sys/kernel/security/tomoyo/version */
  1867. head->read = tomoyo_read_version;
  1868. head->readbuf_size = 128;
  1869. break;
  1870. case TOMOYO_MEMINFO:
  1871. /* /sys/kernel/security/tomoyo/meminfo */
  1872. head->write = tomoyo_write_memory_quota;
  1873. head->read = tomoyo_read_memory_counter;
  1874. head->readbuf_size = 512;
  1875. break;
  1876. case TOMOYO_PROFILE:
  1877. /* /sys/kernel/security/tomoyo/profile */
  1878. head->write = tomoyo_write_profile;
  1879. head->read = tomoyo_read_profile;
  1880. break;
  1881. case TOMOYO_MANAGER:
  1882. /* /sys/kernel/security/tomoyo/manager */
  1883. head->write = tomoyo_write_manager_policy;
  1884. head->read = tomoyo_read_manager_policy;
  1885. break;
  1886. }
  1887. if (!(file->f_mode & FMODE_READ)) {
  1888. /*
  1889. * No need to allocate read_buf since it is not opened
  1890. * for reading.
  1891. */
  1892. head->read = NULL;
  1893. } else {
  1894. if (!head->readbuf_size)
  1895. head->readbuf_size = 4096 * 2;
  1896. head->read_buf = tomoyo_alloc(head->readbuf_size);
  1897. if (!head->read_buf) {
  1898. tomoyo_free(head);
  1899. return -ENOMEM;
  1900. }
  1901. }
  1902. if (!(file->f_mode & FMODE_WRITE)) {
  1903. /*
  1904. * No need to allocate write_buf since it is not opened
  1905. * for writing.
  1906. */
  1907. head->write = NULL;
  1908. } else if (head->write) {
  1909. head->writebuf_size = 4096 * 2;
  1910. head->write_buf = tomoyo_alloc(head->writebuf_size);
  1911. if (!head->write_buf) {
  1912. tomoyo_free(head->read_buf);
  1913. tomoyo_free(head);
  1914. return -ENOMEM;
  1915. }
  1916. }
  1917. head->reader_idx = tomoyo_read_lock();
  1918. file->private_data = head;
  1919. /*
  1920. * Call the handler now if the file is
  1921. * /sys/kernel/security/tomoyo/self_domain
  1922. * so that the user can use
  1923. * cat < /sys/kernel/security/tomoyo/self_domain"
  1924. * to know the current process's domainname.
  1925. */
  1926. if (type == TOMOYO_SELFDOMAIN)
  1927. tomoyo_read_control(file, NULL, 0);
  1928. return 0;
  1929. }
  1930. /**
  1931. * tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
  1932. *
  1933. * @file: Pointer to "struct file".
  1934. * @buffer: Poiner to buffer to write to.
  1935. * @buffer_len: Size of @buffer.
  1936. *
  1937. * Returns bytes read on success, negative value otherwise.
  1938. *
  1939. * Caller holds tomoyo_read_lock().
  1940. */
  1941. static int tomoyo_read_control(struct file *file, char __user *buffer,
  1942. const int buffer_len)
  1943. {
  1944. int len = 0;
  1945. struct tomoyo_io_buffer *head = file->private_data;
  1946. char *cp;
  1947. if (!head->read)
  1948. return -ENOSYS;
  1949. if (mutex_lock_interruptible(&head->io_sem))
  1950. return -EINTR;
  1951. /* Call the policy handler. */
  1952. len = head->read(head);
  1953. if (len < 0)
  1954. goto out;
  1955. /* Write to buffer. */
  1956. len = head->read_avail;
  1957. if (len > buffer_len)
  1958. len = buffer_len;
  1959. if (!len)
  1960. goto out;
  1961. /* head->read_buf changes by some functions. */
  1962. cp = head->read_buf;
  1963. if (copy_to_user(buffer, cp, len)) {
  1964. len = -EFAULT;
  1965. goto out;
  1966. }
  1967. head->read_avail -= len;
  1968. memmove(cp, cp + len, head->read_avail);
  1969. out:
  1970. mutex_unlock(&head->io_sem);
  1971. return len;
  1972. }
  1973. /**
  1974. * tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
  1975. *
  1976. * @file: Pointer to "struct file".
  1977. * @buffer: Pointer to buffer to read from.
  1978. * @buffer_len: Size of @buffer.
  1979. *
  1980. * Returns @buffer_len on success, negative value otherwise.
  1981. *
  1982. * Caller holds tomoyo_read_lock().
  1983. */
  1984. static int tomoyo_write_control(struct file *file, const char __user *buffer,
  1985. const int buffer_len)
  1986. {
  1987. struct tomoyo_io_buffer *head = file->private_data;
  1988. int error = buffer_len;
  1989. int avail_len = buffer_len;
  1990. char *cp0 = head->write_buf;
  1991. if (!head->write)
  1992. return -ENOSYS;
  1993. if (!access_ok(VERIFY_READ, buffer, buffer_len))
  1994. return -EFAULT;
  1995. /* Don't allow updating policies by non manager programs. */
  1996. if (head->write != tomoyo_write_pid &&
  1997. head->write != tomoyo_write_domain_policy &&
  1998. !tomoyo_is_policy_manager())
  1999. return -EPERM;
  2000. if (mutex_lock_interruptible(&head->io_sem))
  2001. return -EINTR;
  2002. /* Read a line and dispatch it to the policy handler. */
  2003. while (avail_len > 0) {
  2004. char c;
  2005. if (head->write_avail >= head->writebuf_size - 1) {
  2006. error = -ENOMEM;
  2007. break;
  2008. } else if (get_user(c, buffer)) {
  2009. error = -EFAULT;
  2010. break;
  2011. }
  2012. buffer++;
  2013. avail_len--;
  2014. cp0[head->write_avail++] = c;
  2015. if (c != '\n')
  2016. continue;
  2017. cp0[head->write_avail - 1] = '\0';
  2018. head->write_avail = 0;
  2019. tomoyo_normalize_line(cp0);
  2020. head->write(head);
  2021. }
  2022. mutex_unlock(&head->io_sem);
  2023. return error;
  2024. }
  2025. /**
  2026. * tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
  2027. *
  2028. * @file: Pointer to "struct file".
  2029. *
  2030. * Releases memory and returns 0.
  2031. *
  2032. * Caller looses tomoyo_read_lock().
  2033. */
  2034. static int tomoyo_close_control(struct file *file)
  2035. {
  2036. struct tomoyo_io_buffer *head = file->private_data;
  2037. tomoyo_read_unlock(head->reader_idx);
  2038. /* Release memory used for policy I/O. */
  2039. tomoyo_free(head->read_buf);
  2040. head->read_buf = NULL;
  2041. tomoyo_free(head->write_buf);
  2042. head->write_buf = NULL;
  2043. tomoyo_free(head);
  2044. head = NULL;
  2045. file->private_data = NULL;
  2046. return 0;
  2047. }
  2048. /**
  2049. * tomoyo_alloc_acl_element - Allocate permanent memory for ACL entry.
  2050. *
  2051. * @acl_type: Type of ACL entry.
  2052. *
  2053. * Returns pointer to the ACL entry on success, NULL otherwise.
  2054. */
  2055. void *tomoyo_alloc_acl_element(const u8 acl_type)
  2056. {
  2057. int len;
  2058. struct tomoyo_acl_info *ptr;
  2059. switch (acl_type) {
  2060. case TOMOYO_TYPE_SINGLE_PATH_ACL:
  2061. len = sizeof(struct tomoyo_single_path_acl_record);
  2062. break;
  2063. case TOMOYO_TYPE_DOUBLE_PATH_ACL:
  2064. len = sizeof(struct tomoyo_double_path_acl_record);
  2065. break;
  2066. default:
  2067. return NULL;
  2068. }
  2069. ptr = tomoyo_alloc_element(len);
  2070. if (!ptr)
  2071. return NULL;
  2072. ptr->type = acl_type;
  2073. return ptr;
  2074. }
  2075. /**
  2076. * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
  2077. *
  2078. * @inode: Pointer to "struct inode".
  2079. * @file: Pointer to "struct file".
  2080. *
  2081. * Returns 0 on success, negative value otherwise.
  2082. */
  2083. static int tomoyo_open(struct inode *inode, struct file *file)
  2084. {
  2085. const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
  2086. - ((u8 *) NULL);
  2087. return tomoyo_open_control(key, file);
  2088. }
  2089. /**
  2090. * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
  2091. *
  2092. * @inode: Pointer to "struct inode".
  2093. * @file: Pointer to "struct file".
  2094. *
  2095. * Returns 0 on success, negative value otherwise.
  2096. */
  2097. static int tomoyo_release(struct inode *inode, struct file *file)
  2098. {
  2099. return tomoyo_close_control(file);
  2100. }
  2101. /**
  2102. * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
  2103. *
  2104. * @file: Pointer to "struct file".
  2105. * @buf: Pointer to buffer.
  2106. * @count: Size of @buf.
  2107. * @ppos: Unused.
  2108. *
  2109. * Returns bytes read on success, negative value otherwise.
  2110. */
  2111. static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
  2112. loff_t *ppos)
  2113. {
  2114. return tomoyo_read_control(file, buf, count);
  2115. }
  2116. /**
  2117. * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
  2118. *
  2119. * @file: Pointer to "struct file".
  2120. * @buf: Pointer to buffer.
  2121. * @count: Size of @buf.
  2122. * @ppos: Unused.
  2123. *
  2124. * Returns @count on success, negative value otherwise.
  2125. */
  2126. static ssize_t tomoyo_write(struct file *file, const char __user *buf,
  2127. size_t count, loff_t *ppos)
  2128. {
  2129. return tomoyo_write_control(file, buf, count);
  2130. }
  2131. /*
  2132. * tomoyo_operations is a "struct file_operations" which is used for handling
  2133. * /sys/kernel/security/tomoyo/ interface.
  2134. *
  2135. * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
  2136. * See tomoyo_io_buffer for internals.
  2137. */
  2138. static const struct file_operations tomoyo_operations = {
  2139. .open = tomoyo_open,
  2140. .release = tomoyo_release,
  2141. .read = tomoyo_read,
  2142. .write = tomoyo_write,
  2143. };
  2144. /**
  2145. * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
  2146. *
  2147. * @name: The name of the interface file.
  2148. * @mode: The permission of the interface file.
  2149. * @parent: The parent directory.
  2150. * @key: Type of interface.
  2151. *
  2152. * Returns nothing.
  2153. */
  2154. static void __init tomoyo_create_entry(const char *name, const mode_t mode,
  2155. struct dentry *parent, const u8 key)
  2156. {
  2157. securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
  2158. &tomoyo_operations);
  2159. }
  2160. /**
  2161. * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
  2162. *
  2163. * Returns 0.
  2164. */
  2165. static int __init tomoyo_initerface_init(void)
  2166. {
  2167. struct dentry *tomoyo_dir;
  2168. /* Don't create securityfs entries unless registered. */
  2169. if (current_cred()->security != &tomoyo_kernel_domain)
  2170. return 0;
  2171. tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
  2172. tomoyo_create_entry("domain_policy", 0600, tomoyo_dir,
  2173. TOMOYO_DOMAINPOLICY);
  2174. tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
  2175. TOMOYO_EXCEPTIONPOLICY);
  2176. tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
  2177. TOMOYO_SELFDOMAIN);
  2178. tomoyo_create_entry(".domain_status", 0600, tomoyo_dir,
  2179. TOMOYO_DOMAIN_STATUS);
  2180. tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
  2181. TOMOYO_PROCESS_STATUS);
  2182. tomoyo_create_entry("meminfo", 0600, tomoyo_dir,
  2183. TOMOYO_MEMINFO);
  2184. tomoyo_create_entry("profile", 0600, tomoyo_dir,
  2185. TOMOYO_PROFILE);
  2186. tomoyo_create_entry("manager", 0600, tomoyo_dir,
  2187. TOMOYO_MANAGER);
  2188. tomoyo_create_entry("version", 0400, tomoyo_dir,
  2189. TOMOYO_VERSION);
  2190. return 0;
  2191. }
  2192. fs_initcall(tomoyo_initerface_init);