util.c 28 KB

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