util.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 || strncmp(domainname, TOMOYO_ROOT_NAME,
  398. TOMOYO_ROOT_NAME_LEN))
  399. goto out;
  400. domainname += TOMOYO_ROOT_NAME_LEN;
  401. if (!*domainname)
  402. return true;
  403. if (*domainname++ != ' ')
  404. goto out;
  405. while (1) {
  406. const unsigned char *cp = strchr(domainname, ' ');
  407. if (!cp)
  408. break;
  409. if (*domainname != '/' ||
  410. !tomoyo_correct_word2(domainname, cp - domainname))
  411. goto out;
  412. domainname = cp + 1;
  413. }
  414. return tomoyo_correct_path(domainname);
  415. out:
  416. return false;
  417. }
  418. /**
  419. * tomoyo_domain_def - Check whether the given token can be a domainname.
  420. *
  421. * @buffer: The token to check.
  422. *
  423. * Returns true if @buffer possibly be a domainname, false otherwise.
  424. */
  425. bool tomoyo_domain_def(const unsigned char *buffer)
  426. {
  427. return !strncmp(buffer, TOMOYO_ROOT_NAME, TOMOYO_ROOT_NAME_LEN);
  428. }
  429. /**
  430. * tomoyo_find_domain - Find a domain by the given name.
  431. *
  432. * @domainname: The domainname to find.
  433. *
  434. * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
  435. *
  436. * Caller holds tomoyo_read_lock().
  437. */
  438. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
  439. {
  440. struct tomoyo_domain_info *domain;
  441. struct tomoyo_path_info name;
  442. name.name = domainname;
  443. tomoyo_fill_path_info(&name);
  444. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  445. if (!domain->is_deleted &&
  446. !tomoyo_pathcmp(&name, domain->domainname))
  447. return domain;
  448. }
  449. return NULL;
  450. }
  451. /**
  452. * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
  453. *
  454. * @filename: The string to evaluate.
  455. *
  456. * Returns the initial length without a pattern in @filename.
  457. */
  458. static int tomoyo_const_part_length(const char *filename)
  459. {
  460. char c;
  461. int len = 0;
  462. if (!filename)
  463. return 0;
  464. while ((c = *filename++) != '\0') {
  465. if (c != '\\') {
  466. len++;
  467. continue;
  468. }
  469. c = *filename++;
  470. switch (c) {
  471. case '\\': /* "\\" */
  472. len += 2;
  473. continue;
  474. case '0': /* "\ooo" */
  475. case '1':
  476. case '2':
  477. case '3':
  478. c = *filename++;
  479. if (c < '0' || c > '7')
  480. break;
  481. c = *filename++;
  482. if (c < '0' || c > '7')
  483. break;
  484. len += 4;
  485. continue;
  486. }
  487. break;
  488. }
  489. return len;
  490. }
  491. /**
  492. * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
  493. *
  494. * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
  495. *
  496. * The caller sets "struct tomoyo_path_info"->name.
  497. */
  498. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
  499. {
  500. const char *name = ptr->name;
  501. const int len = strlen(name);
  502. ptr->const_len = tomoyo_const_part_length(name);
  503. ptr->is_dir = len && (name[len - 1] == '/');
  504. ptr->is_patterned = (ptr->const_len < len);
  505. ptr->hash = full_name_hash(name, len);
  506. }
  507. /**
  508. * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
  509. *
  510. * @filename: The start of string to check.
  511. * @filename_end: The end of string to check.
  512. * @pattern: The start of pattern to compare.
  513. * @pattern_end: The end of pattern to compare.
  514. *
  515. * Returns true if @filename matches @pattern, false otherwise.
  516. */
  517. static bool tomoyo_file_matches_pattern2(const char *filename,
  518. const char *filename_end,
  519. const char *pattern,
  520. const char *pattern_end)
  521. {
  522. while (filename < filename_end && pattern < pattern_end) {
  523. char c;
  524. if (*pattern != '\\') {
  525. if (*filename++ != *pattern++)
  526. return false;
  527. continue;
  528. }
  529. c = *filename;
  530. pattern++;
  531. switch (*pattern) {
  532. int i;
  533. int j;
  534. case '?':
  535. if (c == '/') {
  536. return false;
  537. } else if (c == '\\') {
  538. if (filename[1] == '\\')
  539. filename++;
  540. else if (tomoyo_byte_range(filename + 1))
  541. filename += 3;
  542. else
  543. return false;
  544. }
  545. break;
  546. case '\\':
  547. if (c != '\\')
  548. return false;
  549. if (*++filename != '\\')
  550. return false;
  551. break;
  552. case '+':
  553. if (!isdigit(c))
  554. return false;
  555. break;
  556. case 'x':
  557. if (!isxdigit(c))
  558. return false;
  559. break;
  560. case 'a':
  561. if (!tomoyo_alphabet_char(c))
  562. return false;
  563. break;
  564. case '0':
  565. case '1':
  566. case '2':
  567. case '3':
  568. if (c == '\\' && tomoyo_byte_range(filename + 1)
  569. && strncmp(filename + 1, pattern, 3) == 0) {
  570. filename += 3;
  571. pattern += 2;
  572. break;
  573. }
  574. return false; /* Not matched. */
  575. case '*':
  576. case '@':
  577. for (i = 0; i <= filename_end - filename; i++) {
  578. if (tomoyo_file_matches_pattern2(
  579. filename + i, filename_end,
  580. pattern + 1, pattern_end))
  581. return true;
  582. c = filename[i];
  583. if (c == '.' && *pattern == '@')
  584. break;
  585. if (c != '\\')
  586. continue;
  587. if (filename[i + 1] == '\\')
  588. i++;
  589. else if (tomoyo_byte_range(filename + i + 1))
  590. i += 3;
  591. else
  592. break; /* Bad pattern. */
  593. }
  594. return false; /* Not matched. */
  595. default:
  596. j = 0;
  597. c = *pattern;
  598. if (c == '$') {
  599. while (isdigit(filename[j]))
  600. j++;
  601. } else if (c == 'X') {
  602. while (isxdigit(filename[j]))
  603. j++;
  604. } else if (c == 'A') {
  605. while (tomoyo_alphabet_char(filename[j]))
  606. j++;
  607. }
  608. for (i = 1; i <= j; i++) {
  609. if (tomoyo_file_matches_pattern2(
  610. filename + i, filename_end,
  611. pattern + 1, pattern_end))
  612. return true;
  613. }
  614. return false; /* Not matched or bad pattern. */
  615. }
  616. filename++;
  617. pattern++;
  618. }
  619. while (*pattern == '\\' &&
  620. (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
  621. pattern += 2;
  622. return filename == filename_end && pattern == pattern_end;
  623. }
  624. /**
  625. * tomoyo_file_matches_pattern - Pattern matching without '/' character.
  626. *
  627. * @filename: The start of string to check.
  628. * @filename_end: The end of string to check.
  629. * @pattern: The start of pattern to compare.
  630. * @pattern_end: The end of pattern to compare.
  631. *
  632. * Returns true if @filename matches @pattern, false otherwise.
  633. */
  634. static bool tomoyo_file_matches_pattern(const char *filename,
  635. const char *filename_end,
  636. const char *pattern,
  637. const char *pattern_end)
  638. {
  639. const char *pattern_start = pattern;
  640. bool first = true;
  641. bool result;
  642. while (pattern < pattern_end - 1) {
  643. /* Split at "\-" pattern. */
  644. if (*pattern++ != '\\' || *pattern++ != '-')
  645. continue;
  646. result = tomoyo_file_matches_pattern2(filename,
  647. filename_end,
  648. pattern_start,
  649. pattern - 2);
  650. if (first)
  651. result = !result;
  652. if (result)
  653. return false;
  654. first = false;
  655. pattern_start = pattern;
  656. }
  657. result = tomoyo_file_matches_pattern2(filename, filename_end,
  658. pattern_start, pattern_end);
  659. return first ? result : !result;
  660. }
  661. /**
  662. * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
  663. *
  664. * @f: The start of string to check.
  665. * @p: The start of pattern to compare.
  666. *
  667. * Returns true if @f matches @p, false otherwise.
  668. */
  669. static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
  670. {
  671. const char *f_delimiter;
  672. const char *p_delimiter;
  673. while (*f && *p) {
  674. f_delimiter = strchr(f, '/');
  675. if (!f_delimiter)
  676. f_delimiter = f + strlen(f);
  677. p_delimiter = strchr(p, '/');
  678. if (!p_delimiter)
  679. p_delimiter = p + strlen(p);
  680. if (*p == '\\' && *(p + 1) == '{')
  681. goto recursive;
  682. if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
  683. p_delimiter))
  684. return false;
  685. f = f_delimiter;
  686. if (*f)
  687. f++;
  688. p = p_delimiter;
  689. if (*p)
  690. p++;
  691. }
  692. /* Ignore trailing "\*" and "\@" in @pattern. */
  693. while (*p == '\\' &&
  694. (*(p + 1) == '*' || *(p + 1) == '@'))
  695. p += 2;
  696. return !*f && !*p;
  697. recursive:
  698. /*
  699. * The "\{" pattern is permitted only after '/' character.
  700. * This guarantees that below "*(p - 1)" is safe.
  701. * Also, the "\}" pattern is permitted only before '/' character
  702. * so that "\{" + "\}" pair will not break the "\-" operator.
  703. */
  704. if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
  705. *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
  706. return false; /* Bad pattern. */
  707. do {
  708. /* Compare current component with pattern. */
  709. if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
  710. p_delimiter - 2))
  711. break;
  712. /* Proceed to next component. */
  713. f = f_delimiter;
  714. if (!*f)
  715. break;
  716. f++;
  717. /* Continue comparison. */
  718. if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
  719. return true;
  720. f_delimiter = strchr(f, '/');
  721. } while (f_delimiter);
  722. return false; /* Not matched. */
  723. }
  724. /**
  725. * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
  726. *
  727. * @filename: The filename to check.
  728. * @pattern: The pattern to compare.
  729. *
  730. * Returns true if matches, false otherwise.
  731. *
  732. * The following patterns are available.
  733. * \\ \ itself.
  734. * \ooo Octal representation of a byte.
  735. * \* Zero or more repetitions of characters other than '/'.
  736. * \@ Zero or more repetitions of characters other than '/' or '.'.
  737. * \? 1 byte character other than '/'.
  738. * \$ One or more repetitions of decimal digits.
  739. * \+ 1 decimal digit.
  740. * \X One or more repetitions of hexadecimal digits.
  741. * \x 1 hexadecimal digit.
  742. * \A One or more repetitions of alphabet characters.
  743. * \a 1 alphabet character.
  744. *
  745. * \- Subtraction operator.
  746. *
  747. * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
  748. * /dir/dir/dir/ ).
  749. */
  750. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  751. const struct tomoyo_path_info *pattern)
  752. {
  753. const char *f = filename->name;
  754. const char *p = pattern->name;
  755. const int len = pattern->const_len;
  756. /* If @pattern doesn't contain pattern, I can use strcmp(). */
  757. if (!pattern->is_patterned)
  758. return !tomoyo_pathcmp(filename, pattern);
  759. /* Don't compare directory and non-directory. */
  760. if (filename->is_dir != pattern->is_dir)
  761. return false;
  762. /* Compare the initial length without patterns. */
  763. if (strncmp(f, p, len))
  764. return false;
  765. f += len;
  766. p += len;
  767. return tomoyo_path_matches_pattern2(f, p);
  768. }
  769. /**
  770. * tomoyo_get_exe - Get tomoyo_realpath() of current process.
  771. *
  772. * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
  773. *
  774. * This function uses kzalloc(), so the caller must call kfree()
  775. * if this function didn't return NULL.
  776. */
  777. const char *tomoyo_get_exe(void)
  778. {
  779. struct mm_struct *mm = current->mm;
  780. struct vm_area_struct *vma;
  781. const char *cp = NULL;
  782. if (!mm)
  783. return NULL;
  784. down_read(&mm->mmap_sem);
  785. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  786. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
  787. cp = tomoyo_realpath_from_path(&vma->vm_file->f_path);
  788. break;
  789. }
  790. }
  791. up_read(&mm->mmap_sem);
  792. return cp;
  793. }
  794. /**
  795. * tomoyo_get_mode - Get MAC mode.
  796. *
  797. * @profile: Profile number.
  798. * @index: Index number of functionality.
  799. *
  800. * Returns mode.
  801. */
  802. int tomoyo_get_mode(const u8 profile, const u8 index)
  803. {
  804. u8 mode;
  805. const u8 category = TOMOYO_MAC_CATEGORY_FILE;
  806. if (!tomoyo_policy_loaded)
  807. return TOMOYO_CONFIG_DISABLED;
  808. mode = tomoyo_profile(profile)->config[index];
  809. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  810. mode = tomoyo_profile(profile)->config[category];
  811. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  812. mode = tomoyo_profile(profile)->default_config;
  813. return mode & 3;
  814. }
  815. /**
  816. * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
  817. *
  818. * @r: Pointer to "struct tomoyo_request_info" to initialize.
  819. * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
  820. * @index: Index number of functionality.
  821. *
  822. * Returns mode.
  823. */
  824. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  825. struct tomoyo_domain_info *domain, const u8 index)
  826. {
  827. u8 profile;
  828. memset(r, 0, sizeof(*r));
  829. if (!domain)
  830. domain = tomoyo_domain();
  831. r->domain = domain;
  832. profile = domain->profile;
  833. r->profile = profile;
  834. r->type = index;
  835. r->mode = tomoyo_get_mode(profile, index);
  836. return r->mode;
  837. }
  838. /**
  839. * tomoyo_last_word - Get last component of a line.
  840. *
  841. * @line: A line.
  842. *
  843. * Returns the last word of a line.
  844. */
  845. const char *tomoyo_last_word(const char *name)
  846. {
  847. const char *cp = strrchr(name, ' ');
  848. if (cp)
  849. return cp + 1;
  850. return name;
  851. }
  852. /**
  853. * tomoyo_domain_quota_is_ok - Check for domain's quota.
  854. *
  855. * @r: Pointer to "struct tomoyo_request_info".
  856. *
  857. * Returns true if the domain is not exceeded quota, false otherwise.
  858. *
  859. * Caller holds tomoyo_read_lock().
  860. */
  861. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
  862. {
  863. unsigned int count = 0;
  864. struct tomoyo_domain_info *domain = r->domain;
  865. struct tomoyo_acl_info *ptr;
  866. if (r->mode != TOMOYO_CONFIG_LEARNING)
  867. return false;
  868. if (!domain)
  869. return true;
  870. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  871. u16 perm;
  872. u8 i;
  873. if (ptr->is_deleted)
  874. continue;
  875. switch (ptr->type) {
  876. case TOMOYO_TYPE_PATH_ACL:
  877. perm = container_of(ptr, struct tomoyo_path_acl, head)
  878. ->perm;
  879. break;
  880. case TOMOYO_TYPE_PATH2_ACL:
  881. perm = container_of(ptr, struct tomoyo_path2_acl, head)
  882. ->perm;
  883. break;
  884. case TOMOYO_TYPE_PATH_NUMBER_ACL:
  885. perm = container_of(ptr, struct tomoyo_path_number_acl,
  886. head)->perm;
  887. break;
  888. case TOMOYO_TYPE_MKDEV_ACL:
  889. perm = container_of(ptr, struct tomoyo_mkdev_acl,
  890. head)->perm;
  891. break;
  892. default:
  893. perm = 1;
  894. }
  895. for (i = 0; i < 16; i++)
  896. if (perm & (1 << i))
  897. count++;
  898. }
  899. if (count < tomoyo_profile(domain->profile)->
  900. pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
  901. return true;
  902. if (!domain->quota_warned) {
  903. domain->quota_warned = true;
  904. printk(KERN_WARNING "TOMOYO-WARNING: "
  905. "Domain '%s' has too many ACLs to hold. "
  906. "Stopped learning mode.\n", domain->domainname->name);
  907. }
  908. return false;
  909. }