util.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * security/tomoyo/util.c
  3. *
  4. * Utility functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include <linux/slab.h>
  9. #include "common.h"
  10. /* Lock for protecting policy. */
  11. DEFINE_MUTEX(tomoyo_policy_lock);
  12. /* Has /sbin/init started? */
  13. bool tomoyo_policy_loaded;
  14. /**
  15. * tomoyo_parse_ulong - Parse an "unsigned long" value.
  16. *
  17. * @result: Pointer to "unsigned long".
  18. * @str: Pointer to string to parse.
  19. *
  20. * Returns value type on success, 0 otherwise.
  21. *
  22. * The @src is updated to point the first character after the value
  23. * on success.
  24. */
  25. static u8 tomoyo_parse_ulong(unsigned long *result, char **str)
  26. {
  27. const char *cp = *str;
  28. char *ep;
  29. int base = 10;
  30. if (*cp == '0') {
  31. char c = *(cp + 1);
  32. if (c == 'x' || c == 'X') {
  33. base = 16;
  34. cp += 2;
  35. } else if (c >= '0' && c <= '7') {
  36. base = 8;
  37. cp++;
  38. }
  39. }
  40. *result = simple_strtoul(cp, &ep, base);
  41. if (cp == ep)
  42. return 0;
  43. *str = ep;
  44. switch (base) {
  45. case 16:
  46. return TOMOYO_VALUE_TYPE_HEXADECIMAL;
  47. case 8:
  48. return TOMOYO_VALUE_TYPE_OCTAL;
  49. default:
  50. return TOMOYO_VALUE_TYPE_DECIMAL;
  51. }
  52. }
  53. /**
  54. * tomoyo_print_ulong - Print an "unsigned long" value.
  55. *
  56. * @buffer: Pointer to buffer.
  57. * @buffer_len: Size of @buffer.
  58. * @value: An "unsigned long" value.
  59. * @type: Type of @value.
  60. *
  61. * Returns nothing.
  62. */
  63. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  64. const unsigned long value, const u8 type)
  65. {
  66. if (type == TOMOYO_VALUE_TYPE_DECIMAL)
  67. snprintf(buffer, buffer_len, "%lu", value);
  68. else if (type == TOMOYO_VALUE_TYPE_OCTAL)
  69. snprintf(buffer, buffer_len, "0%lo", value);
  70. else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
  71. snprintf(buffer, buffer_len, "0x%lX", value);
  72. else
  73. snprintf(buffer, buffer_len, "type(%u)", type);
  74. }
  75. /**
  76. * tomoyo_parse_name_union - Parse a tomoyo_name_union.
  77. *
  78. * @filename: Name or name group.
  79. * @ptr: Pointer to "struct tomoyo_name_union".
  80. *
  81. * Returns true on success, false otherwise.
  82. */
  83. bool tomoyo_parse_name_union(const char *filename,
  84. struct tomoyo_name_union *ptr)
  85. {
  86. if (!tomoyo_correct_word(filename))
  87. return false;
  88. if (filename[0] == '@') {
  89. ptr->group = tomoyo_get_group(filename + 1, TOMOYO_PATH_GROUP);
  90. ptr->is_group = true;
  91. return ptr->group != NULL;
  92. }
  93. ptr->filename = tomoyo_get_name(filename);
  94. ptr->is_group = false;
  95. return ptr->filename != NULL;
  96. }
  97. /**
  98. * tomoyo_parse_number_union - Parse a tomoyo_number_union.
  99. *
  100. * @data: Number or number range or number group.
  101. * @ptr: Pointer to "struct tomoyo_number_union".
  102. *
  103. * Returns true on success, false otherwise.
  104. */
  105. bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num)
  106. {
  107. u8 type;
  108. unsigned long v;
  109. memset(num, 0, sizeof(*num));
  110. if (data[0] == '@') {
  111. if (!tomoyo_correct_word(data))
  112. return false;
  113. num->group = tomoyo_get_group(data + 1, TOMOYO_NUMBER_GROUP);
  114. num->is_group = true;
  115. return num->group != NULL;
  116. }
  117. type = tomoyo_parse_ulong(&v, &data);
  118. if (!type)
  119. return false;
  120. num->values[0] = v;
  121. num->min_type = type;
  122. if (!*data) {
  123. num->values[1] = v;
  124. num->max_type = type;
  125. return true;
  126. }
  127. if (*data++ != '-')
  128. return false;
  129. type = tomoyo_parse_ulong(&v, &data);
  130. if (!type || *data)
  131. return false;
  132. num->values[1] = v;
  133. num->max_type = type;
  134. return true;
  135. }
  136. /**
  137. * tomoyo_byte_range - Check whether the string is a \ooo style octal value.
  138. *
  139. * @str: Pointer to the string.
  140. *
  141. * Returns true if @str is a \ooo style octal value, false otherwise.
  142. *
  143. * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF.
  144. * This function verifies that \ooo is in valid range.
  145. */
  146. static inline bool tomoyo_byte_range(const char *str)
  147. {
  148. return *str >= '0' && *str++ <= '3' &&
  149. *str >= '0' && *str++ <= '7' &&
  150. *str >= '0' && *str <= '7';
  151. }
  152. /**
  153. * tomoyo_alphabet_char - Check whether the character is an alphabet.
  154. *
  155. * @c: The character to check.
  156. *
  157. * Returns true if @c is an alphabet character, false otherwise.
  158. */
  159. static inline bool tomoyo_alphabet_char(const char c)
  160. {
  161. return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
  162. }
  163. /**
  164. * tomoyo_make_byte - Make byte value from three octal characters.
  165. *
  166. * @c1: The first character.
  167. * @c2: The second character.
  168. * @c3: The third character.
  169. *
  170. * Returns byte value.
  171. */
  172. static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3)
  173. {
  174. return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
  175. }
  176. /**
  177. * tomoyo_str_starts - Check whether the given string starts with the given keyword.
  178. *
  179. * @src: Pointer to pointer to the string.
  180. * @find: Pointer to the keyword.
  181. *
  182. * Returns true if @src starts with @find, false otherwise.
  183. *
  184. * The @src is updated to point the first character after the @find
  185. * if @src starts with @find.
  186. */
  187. bool tomoyo_str_starts(char **src, const char *find)
  188. {
  189. const int len = strlen(find);
  190. char *tmp = *src;
  191. if (strncmp(tmp, find, len))
  192. return false;
  193. tmp += len;
  194. *src = tmp;
  195. return true;
  196. }
  197. /**
  198. * tomoyo_normalize_line - Format string.
  199. *
  200. * @buffer: The line to normalize.
  201. *
  202. * Leading and trailing whitespaces are removed.
  203. * Multiple whitespaces are packed into single space.
  204. *
  205. * Returns nothing.
  206. */
  207. void tomoyo_normalize_line(unsigned char *buffer)
  208. {
  209. unsigned char *sp = buffer;
  210. unsigned char *dp = buffer;
  211. bool first = true;
  212. while (tomoyo_invalid(*sp))
  213. sp++;
  214. while (*sp) {
  215. if (!first)
  216. *dp++ = ' ';
  217. first = false;
  218. while (tomoyo_valid(*sp))
  219. *dp++ = *sp++;
  220. while (tomoyo_invalid(*sp))
  221. sp++;
  222. }
  223. *dp = '\0';
  224. }
  225. /**
  226. * tomoyo_tokenize - Tokenize string.
  227. *
  228. * @buffer: The line to tokenize.
  229. * @w: Pointer to "char *".
  230. * @size: Sizeof @w .
  231. *
  232. * Returns true on success, false otherwise.
  233. */
  234. bool tomoyo_tokenize(char *buffer, char *w[], size_t size)
  235. {
  236. int count = size / sizeof(char *);
  237. int i;
  238. for (i = 0; i < count; i++)
  239. w[i] = "";
  240. for (i = 0; i < count; i++) {
  241. char *cp = strchr(buffer, ' ');
  242. if (cp)
  243. *cp = '\0';
  244. w[i] = buffer;
  245. if (!cp)
  246. break;
  247. buffer = cp + 1;
  248. }
  249. return i < count || !*buffer;
  250. }
  251. /**
  252. * tomoyo_correct_word2 - Validate a string.
  253. *
  254. * @string: The string to check. May be non-'\0'-terminated.
  255. * @len: Length of @string.
  256. *
  257. * Check whether the given string follows the naming rules.
  258. * Returns true if @string follows the naming rules, false otherwise.
  259. */
  260. static bool tomoyo_correct_word2(const char *string, size_t len)
  261. {
  262. const char *const start = string;
  263. bool in_repetition = false;
  264. unsigned char c;
  265. unsigned char d;
  266. unsigned char e;
  267. if (!len)
  268. goto out;
  269. while (len--) {
  270. c = *string++;
  271. if (c == '\\') {
  272. if (!len--)
  273. goto out;
  274. c = *string++;
  275. switch (c) {
  276. case '\\': /* "\\" */
  277. continue;
  278. case '$': /* "\$" */
  279. case '+': /* "\+" */
  280. case '?': /* "\?" */
  281. case '*': /* "\*" */
  282. case '@': /* "\@" */
  283. case 'x': /* "\x" */
  284. case 'X': /* "\X" */
  285. case 'a': /* "\a" */
  286. case 'A': /* "\A" */
  287. case '-': /* "\-" */
  288. continue;
  289. case '{': /* "/\{" */
  290. if (string - 3 < start || *(string - 3) != '/')
  291. break;
  292. in_repetition = true;
  293. continue;
  294. case '}': /* "\}/" */
  295. if (*string != '/')
  296. break;
  297. if (!in_repetition)
  298. break;
  299. in_repetition = false;
  300. continue;
  301. case '0': /* "\ooo" */
  302. case '1':
  303. case '2':
  304. case '3':
  305. if (!len-- || !len--)
  306. break;
  307. d = *string++;
  308. e = *string++;
  309. if (d < '0' || d > '7' || e < '0' || e > '7')
  310. break;
  311. c = tomoyo_make_byte(c, d, e);
  312. if (tomoyo_invalid(c))
  313. continue; /* pattern is not \000 */
  314. }
  315. goto out;
  316. } else if (in_repetition && c == '/') {
  317. goto out;
  318. } else if (tomoyo_invalid(c)) {
  319. goto out;
  320. }
  321. }
  322. if (in_repetition)
  323. goto out;
  324. return true;
  325. out:
  326. return false;
  327. }
  328. /**
  329. * tomoyo_correct_word - Validate a string.
  330. *
  331. * @string: The string to check.
  332. *
  333. * Check whether the given string follows the naming rules.
  334. * Returns true if @string follows the naming rules, false otherwise.
  335. */
  336. bool tomoyo_correct_word(const char *string)
  337. {
  338. return tomoyo_correct_word2(string, strlen(string));
  339. }
  340. /**
  341. * tomoyo_correct_path - Validate a pathname.
  342. *
  343. * @filename: The pathname to check.
  344. *
  345. * Check whether the given pathname follows the naming rules.
  346. * Returns true if @filename follows the naming rules, false otherwise.
  347. */
  348. bool tomoyo_correct_path(const char *filename)
  349. {
  350. return *filename == '/' && tomoyo_correct_word(filename);
  351. }
  352. /**
  353. * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
  354. *
  355. * @domainname: The domainname to check.
  356. *
  357. * Returns true if @domainname follows the naming rules, false otherwise.
  358. */
  359. bool tomoyo_correct_domain(const unsigned char *domainname)
  360. {
  361. if (!domainname || strncmp(domainname, TOMOYO_ROOT_NAME,
  362. TOMOYO_ROOT_NAME_LEN))
  363. goto out;
  364. domainname += TOMOYO_ROOT_NAME_LEN;
  365. if (!*domainname)
  366. return true;
  367. if (*domainname++ != ' ')
  368. goto out;
  369. while (1) {
  370. const unsigned char *cp = strchr(domainname, ' ');
  371. if (!cp)
  372. break;
  373. if (*domainname != '/' ||
  374. !tomoyo_correct_word2(domainname, cp - domainname))
  375. goto out;
  376. domainname = cp + 1;
  377. }
  378. return tomoyo_correct_path(domainname);
  379. out:
  380. return false;
  381. }
  382. /**
  383. * tomoyo_domain_def - Check whether the given token can be a domainname.
  384. *
  385. * @buffer: The token to check.
  386. *
  387. * Returns true if @buffer possibly be a domainname, false otherwise.
  388. */
  389. bool tomoyo_domain_def(const unsigned char *buffer)
  390. {
  391. return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
  392. }
  393. /**
  394. * tomoyo_find_domain - Find a domain by the given name.
  395. *
  396. * @domainname: The domainname to find.
  397. *
  398. * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
  399. *
  400. * Caller holds tomoyo_read_lock().
  401. */
  402. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
  403. {
  404. struct tomoyo_domain_info *domain;
  405. struct tomoyo_path_info name;
  406. name.name = domainname;
  407. tomoyo_fill_path_info(&name);
  408. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  409. if (!domain->is_deleted &&
  410. !tomoyo_pathcmp(&name, domain->domainname))
  411. return domain;
  412. }
  413. return NULL;
  414. }
  415. /**
  416. * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
  417. *
  418. * @filename: The string to evaluate.
  419. *
  420. * Returns the initial length without a pattern in @filename.
  421. */
  422. static int tomoyo_const_part_length(const char *filename)
  423. {
  424. char c;
  425. int len = 0;
  426. if (!filename)
  427. return 0;
  428. while ((c = *filename++) != '\0') {
  429. if (c != '\\') {
  430. len++;
  431. continue;
  432. }
  433. c = *filename++;
  434. switch (c) {
  435. case '\\': /* "\\" */
  436. len += 2;
  437. continue;
  438. case '0': /* "\ooo" */
  439. case '1':
  440. case '2':
  441. case '3':
  442. c = *filename++;
  443. if (c < '0' || c > '7')
  444. break;
  445. c = *filename++;
  446. if (c < '0' || c > '7')
  447. break;
  448. len += 4;
  449. continue;
  450. }
  451. break;
  452. }
  453. return len;
  454. }
  455. /**
  456. * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
  457. *
  458. * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
  459. *
  460. * The caller sets "struct tomoyo_path_info"->name.
  461. */
  462. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
  463. {
  464. const char *name = ptr->name;
  465. const int len = strlen(name);
  466. ptr->const_len = tomoyo_const_part_length(name);
  467. ptr->is_dir = len && (name[len - 1] == '/');
  468. ptr->is_patterned = (ptr->const_len < len);
  469. ptr->hash = full_name_hash(name, len);
  470. }
  471. /**
  472. * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
  473. *
  474. * @filename: The start of string to check.
  475. * @filename_end: The end of string to check.
  476. * @pattern: The start of pattern to compare.
  477. * @pattern_end: The end of pattern to compare.
  478. *
  479. * Returns true if @filename matches @pattern, false otherwise.
  480. */
  481. static bool tomoyo_file_matches_pattern2(const char *filename,
  482. const char *filename_end,
  483. const char *pattern,
  484. const char *pattern_end)
  485. {
  486. while (filename < filename_end && pattern < pattern_end) {
  487. char c;
  488. if (*pattern != '\\') {
  489. if (*filename++ != *pattern++)
  490. return false;
  491. continue;
  492. }
  493. c = *filename;
  494. pattern++;
  495. switch (*pattern) {
  496. int i;
  497. int j;
  498. case '?':
  499. if (c == '/') {
  500. return false;
  501. } else if (c == '\\') {
  502. if (filename[1] == '\\')
  503. filename++;
  504. else if (tomoyo_byte_range(filename + 1))
  505. filename += 3;
  506. else
  507. return false;
  508. }
  509. break;
  510. case '\\':
  511. if (c != '\\')
  512. return false;
  513. if (*++filename != '\\')
  514. return false;
  515. break;
  516. case '+':
  517. if (!isdigit(c))
  518. return false;
  519. break;
  520. case 'x':
  521. if (!isxdigit(c))
  522. return false;
  523. break;
  524. case 'a':
  525. if (!tomoyo_alphabet_char(c))
  526. return false;
  527. break;
  528. case '0':
  529. case '1':
  530. case '2':
  531. case '3':
  532. if (c == '\\' && tomoyo_byte_range(filename + 1)
  533. && strncmp(filename + 1, pattern, 3) == 0) {
  534. filename += 3;
  535. pattern += 2;
  536. break;
  537. }
  538. return false; /* Not matched. */
  539. case '*':
  540. case '@':
  541. for (i = 0; i <= filename_end - filename; i++) {
  542. if (tomoyo_file_matches_pattern2(
  543. filename + i, filename_end,
  544. pattern + 1, pattern_end))
  545. return true;
  546. c = filename[i];
  547. if (c == '.' && *pattern == '@')
  548. break;
  549. if (c != '\\')
  550. continue;
  551. if (filename[i + 1] == '\\')
  552. i++;
  553. else if (tomoyo_byte_range(filename + i + 1))
  554. i += 3;
  555. else
  556. break; /* Bad pattern. */
  557. }
  558. return false; /* Not matched. */
  559. default:
  560. j = 0;
  561. c = *pattern;
  562. if (c == '$') {
  563. while (isdigit(filename[j]))
  564. j++;
  565. } else if (c == 'X') {
  566. while (isxdigit(filename[j]))
  567. j++;
  568. } else if (c == 'A') {
  569. while (tomoyo_alphabet_char(filename[j]))
  570. j++;
  571. }
  572. for (i = 1; i <= j; i++) {
  573. if (tomoyo_file_matches_pattern2(
  574. filename + i, filename_end,
  575. pattern + 1, pattern_end))
  576. return true;
  577. }
  578. return false; /* Not matched or bad pattern. */
  579. }
  580. filename++;
  581. pattern++;
  582. }
  583. while (*pattern == '\\' &&
  584. (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
  585. pattern += 2;
  586. return filename == filename_end && pattern == pattern_end;
  587. }
  588. /**
  589. * tomoyo_file_matches_pattern - Pattern matching without '/' character.
  590. *
  591. * @filename: The start of string to check.
  592. * @filename_end: The end of string to check.
  593. * @pattern: The start of pattern to compare.
  594. * @pattern_end: The end of pattern to compare.
  595. *
  596. * Returns true if @filename matches @pattern, false otherwise.
  597. */
  598. static bool tomoyo_file_matches_pattern(const char *filename,
  599. const char *filename_end,
  600. const char *pattern,
  601. const char *pattern_end)
  602. {
  603. const char *pattern_start = pattern;
  604. bool first = true;
  605. bool result;
  606. while (pattern < pattern_end - 1) {
  607. /* Split at "\-" pattern. */
  608. if (*pattern++ != '\\' || *pattern++ != '-')
  609. continue;
  610. result = tomoyo_file_matches_pattern2(filename,
  611. filename_end,
  612. pattern_start,
  613. pattern - 2);
  614. if (first)
  615. result = !result;
  616. if (result)
  617. return false;
  618. first = false;
  619. pattern_start = pattern;
  620. }
  621. result = tomoyo_file_matches_pattern2(filename, filename_end,
  622. pattern_start, pattern_end);
  623. return first ? result : !result;
  624. }
  625. /**
  626. * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
  627. *
  628. * @f: The start of string to check.
  629. * @p: The start of pattern to compare.
  630. *
  631. * Returns true if @f matches @p, false otherwise.
  632. */
  633. static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
  634. {
  635. const char *f_delimiter;
  636. const char *p_delimiter;
  637. while (*f && *p) {
  638. f_delimiter = strchr(f, '/');
  639. if (!f_delimiter)
  640. f_delimiter = f + strlen(f);
  641. p_delimiter = strchr(p, '/');
  642. if (!p_delimiter)
  643. p_delimiter = p + strlen(p);
  644. if (*p == '\\' && *(p + 1) == '{')
  645. goto recursive;
  646. if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
  647. p_delimiter))
  648. return false;
  649. f = f_delimiter;
  650. if (*f)
  651. f++;
  652. p = p_delimiter;
  653. if (*p)
  654. p++;
  655. }
  656. /* Ignore trailing "\*" and "\@" in @pattern. */
  657. while (*p == '\\' &&
  658. (*(p + 1) == '*' || *(p + 1) == '@'))
  659. p += 2;
  660. return !*f && !*p;
  661. recursive:
  662. /*
  663. * The "\{" pattern is permitted only after '/' character.
  664. * This guarantees that below "*(p - 1)" is safe.
  665. * Also, the "\}" pattern is permitted only before '/' character
  666. * so that "\{" + "\}" pair will not break the "\-" operator.
  667. */
  668. if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
  669. *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
  670. return false; /* Bad pattern. */
  671. do {
  672. /* Compare current component with pattern. */
  673. if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
  674. p_delimiter - 2))
  675. break;
  676. /* Proceed to next component. */
  677. f = f_delimiter;
  678. if (!*f)
  679. break;
  680. f++;
  681. /* Continue comparison. */
  682. if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
  683. return true;
  684. f_delimiter = strchr(f, '/');
  685. } while (f_delimiter);
  686. return false; /* Not matched. */
  687. }
  688. /**
  689. * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
  690. *
  691. * @filename: The filename to check.
  692. * @pattern: The pattern to compare.
  693. *
  694. * Returns true if matches, false otherwise.
  695. *
  696. * The following patterns are available.
  697. * \\ \ itself.
  698. * \ooo Octal representation of a byte.
  699. * \* Zero or more repetitions of characters other than '/'.
  700. * \@ Zero or more repetitions of characters other than '/' or '.'.
  701. * \? 1 byte character other than '/'.
  702. * \$ One or more repetitions of decimal digits.
  703. * \+ 1 decimal digit.
  704. * \X One or more repetitions of hexadecimal digits.
  705. * \x 1 hexadecimal digit.
  706. * \A One or more repetitions of alphabet characters.
  707. * \a 1 alphabet character.
  708. *
  709. * \- Subtraction operator.
  710. *
  711. * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
  712. * /dir/dir/dir/ ).
  713. */
  714. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  715. const struct tomoyo_path_info *pattern)
  716. {
  717. const char *f = filename->name;
  718. const char *p = pattern->name;
  719. const int len = pattern->const_len;
  720. /* If @pattern doesn't contain pattern, I can use strcmp(). */
  721. if (!pattern->is_patterned)
  722. return !tomoyo_pathcmp(filename, pattern);
  723. /* Don't compare directory and non-directory. */
  724. if (filename->is_dir != pattern->is_dir)
  725. return false;
  726. /* Compare the initial length without patterns. */
  727. if (strncmp(f, p, len))
  728. return false;
  729. f += len;
  730. p += len;
  731. return tomoyo_path_matches_pattern2(f, p);
  732. }
  733. /**
  734. * tomoyo_get_exe - Get tomoyo_realpath() of current process.
  735. *
  736. * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
  737. *
  738. * This function uses kzalloc(), so the caller must call kfree()
  739. * if this function didn't return NULL.
  740. */
  741. const char *tomoyo_get_exe(void)
  742. {
  743. struct mm_struct *mm = current->mm;
  744. struct vm_area_struct *vma;
  745. const char *cp = NULL;
  746. if (!mm)
  747. return NULL;
  748. down_read(&mm->mmap_sem);
  749. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  750. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
  751. cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
  752. break;
  753. }
  754. }
  755. up_read(&mm->mmap_sem);
  756. return cp;
  757. }
  758. /**
  759. * tomoyo_get_mode - Get MAC mode.
  760. *
  761. * @profile: Profile number.
  762. * @index: Index number of functionality.
  763. *
  764. * Returns mode.
  765. */
  766. int tomoyo_get_mode(const u8 profile, const u8 index)
  767. {
  768. u8 mode;
  769. const u8 category = TOMOYO_MAC_CATEGORY_FILE;
  770. if (!tomoyo_policy_loaded)
  771. return TOMOYO_CONFIG_DISABLED;
  772. mode = tomoyo_profile(profile)->config[index];
  773. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  774. mode = tomoyo_profile(profile)->config[category];
  775. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  776. mode = tomoyo_profile(profile)->default_config;
  777. return mode & 3;
  778. }
  779. /**
  780. * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
  781. *
  782. * @r: Pointer to "struct tomoyo_request_info" to initialize.
  783. * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
  784. * @index: Index number of functionality.
  785. *
  786. * Returns mode.
  787. */
  788. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  789. struct tomoyo_domain_info *domain, const u8 index)
  790. {
  791. u8 profile;
  792. memset(r, 0, sizeof(*r));
  793. if (!domain)
  794. domain = tomoyo_domain();
  795. r->domain = domain;
  796. profile = domain->profile;
  797. r->profile = profile;
  798. r->type = index;
  799. r->mode = tomoyo_get_mode(profile, index);
  800. return r->mode;
  801. }
  802. /**
  803. * tomoyo_last_word - Get last component of a line.
  804. *
  805. * @line: A line.
  806. *
  807. * Returns the last word of a line.
  808. */
  809. const char *tomoyo_last_word(const char *name)
  810. {
  811. const char *cp = strrchr(name, ' ');
  812. if (cp)
  813. return cp + 1;
  814. return name;
  815. }
  816. /**
  817. * tomoyo_warn_log - Print warning or error message on console.
  818. *
  819. * @r: Pointer to "struct tomoyo_request_info".
  820. * @fmt: The printf()'s format string, followed by parameters.
  821. */
  822. void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
  823. {
  824. va_list args;
  825. char *buffer;
  826. const struct tomoyo_domain_info * const domain = r->domain;
  827. const struct tomoyo_profile *profile = tomoyo_profile(domain->profile);
  828. switch (r->mode) {
  829. case TOMOYO_CONFIG_ENFORCING:
  830. if (!profile->enforcing->enforcing_verbose)
  831. return;
  832. break;
  833. case TOMOYO_CONFIG_PERMISSIVE:
  834. if (!profile->permissive->permissive_verbose)
  835. return;
  836. break;
  837. case TOMOYO_CONFIG_LEARNING:
  838. if (!profile->learning->learning_verbose)
  839. return;
  840. break;
  841. }
  842. buffer = kmalloc(4096, GFP_NOFS);
  843. if (!buffer)
  844. return;
  845. va_start(args, fmt);
  846. vsnprintf(buffer, 4095, fmt, args);
  847. va_end(args);
  848. buffer[4095] = '\0';
  849. printk(KERN_WARNING "%s: Access %s denied for %s\n",
  850. r->mode == TOMOYO_CONFIG_ENFORCING ? "ERROR" : "WARNING", buffer,
  851. tomoyo_last_word(domain->domainname->name));
  852. kfree(buffer);
  853. }
  854. /**
  855. * tomoyo_domain_quota_is_ok - Check for domain's quota.
  856. *
  857. * @r: Pointer to "struct tomoyo_request_info".
  858. *
  859. * Returns true if the domain is not exceeded quota, false otherwise.
  860. *
  861. * Caller holds tomoyo_read_lock().
  862. */
  863. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
  864. {
  865. unsigned int count = 0;
  866. struct tomoyo_domain_info *domain = r->domain;
  867. struct tomoyo_acl_info *ptr;
  868. if (r->mode != TOMOYO_CONFIG_LEARNING)
  869. return false;
  870. if (!domain)
  871. return true;
  872. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  873. if (ptr->is_deleted)
  874. continue;
  875. switch (ptr->type) {
  876. u16 perm;
  877. u8 i;
  878. case TOMOYO_TYPE_PATH_ACL:
  879. perm = container_of(ptr, struct tomoyo_path_acl, head)
  880. ->perm;
  881. for (i = 0; i < TOMOYO_MAX_PATH_OPERATION; i++)
  882. if (perm & (1 << i))
  883. count++;
  884. if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
  885. count -= 2;
  886. break;
  887. case TOMOYO_TYPE_PATH2_ACL:
  888. perm = container_of(ptr, struct tomoyo_path2_acl, head)
  889. ->perm;
  890. for (i = 0; i < TOMOYO_MAX_PATH2_OPERATION; i++)
  891. if (perm & (1 << i))
  892. count++;
  893. break;
  894. case TOMOYO_TYPE_PATH_NUMBER_ACL:
  895. perm = container_of(ptr, struct tomoyo_path_number_acl,
  896. head)->perm;
  897. for (i = 0; i < TOMOYO_MAX_PATH_NUMBER_OPERATION; i++)
  898. if (perm & (1 << i))
  899. count++;
  900. break;
  901. case TOMOYO_TYPE_MKDEV_ACL:
  902. perm = container_of(ptr, struct tomoyo_mkdev_acl,
  903. head)->perm;
  904. for (i = 0; i < TOMOYO_MAX_MKDEV_OPERATION; i++)
  905. if (perm & (1 << i))
  906. count++;
  907. break;
  908. default:
  909. count++;
  910. }
  911. }
  912. if (count < tomoyo_profile(domain->profile)->learning->
  913. learning_max_entry)
  914. return true;
  915. if (!domain->quota_warned) {
  916. domain->quota_warned = true;
  917. printk(KERN_WARNING "TOMOYO-WARNING: "
  918. "Domain '%s' has so many ACLs to hold. "
  919. "Stopped learning mode.\n", domain->domainname->name);
  920. }
  921. return false;
  922. }