util.c 23 KB

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