common.c 57 KB

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