util.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. * @time64: 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. u8 recursion = 20;
  406. const char *const start = string;
  407. bool in_repetition = false;
  408. if (!len)
  409. goto out;
  410. while (len--) {
  411. unsigned char c = *string++;
  412. if (c == '\\') {
  413. if (!len--)
  414. goto out;
  415. c = *string++;
  416. if (c >= '0' && c <= '3') {
  417. unsigned char d;
  418. unsigned char e;
  419. if (!len-- || !len--)
  420. goto out;
  421. d = *string++;
  422. e = *string++;
  423. if (d < '0' || d > '7' || e < '0' || e > '7')
  424. goto out;
  425. c = tomoyo_make_byte(c, d, e);
  426. if (c <= ' ' || c >= 127)
  427. continue;
  428. goto out;
  429. }
  430. switch (c) {
  431. case '\\': /* "\\" */
  432. case '+': /* "\+" */
  433. case '?': /* "\?" */
  434. case 'x': /* "\x" */
  435. case 'a': /* "\a" */
  436. case '-': /* "\-" */
  437. continue;
  438. }
  439. if (!recursion--)
  440. goto out;
  441. switch (c) {
  442. case '*': /* "\*" */
  443. case '@': /* "\@" */
  444. case '$': /* "\$" */
  445. case 'X': /* "\X" */
  446. case 'A': /* "\A" */
  447. continue;
  448. case '{': /* "/\{" */
  449. if (string - 3 < start || *(string - 3) != '/')
  450. goto out;
  451. in_repetition = true;
  452. continue;
  453. case '}': /* "\}/" */
  454. if (*string != '/')
  455. goto out;
  456. if (!in_repetition)
  457. goto out;
  458. in_repetition = false;
  459. continue;
  460. }
  461. goto out;
  462. } else if (in_repetition && c == '/') {
  463. goto out;
  464. } else if (c <= ' ' || c >= 127) {
  465. goto out;
  466. }
  467. }
  468. if (in_repetition)
  469. goto out;
  470. return true;
  471. out:
  472. return false;
  473. }
  474. /**
  475. * tomoyo_correct_word - Validate a string.
  476. *
  477. * @string: The string to check.
  478. *
  479. * Check whether the given string follows the naming rules.
  480. * Returns true if @string follows the naming rules, false otherwise.
  481. */
  482. bool tomoyo_correct_word(const char *string)
  483. {
  484. return tomoyo_correct_word2(string, strlen(string));
  485. }
  486. /**
  487. * tomoyo_correct_path2 - Check whether the given pathname follows the naming rules.
  488. *
  489. * @filename: The pathname to check.
  490. * @len: Length of @filename.
  491. *
  492. * Returns true if @filename follows the naming rules, false otherwise.
  493. */
  494. static bool tomoyo_correct_path2(const char *filename, const size_t len)
  495. {
  496. const char *cp1 = memchr(filename, '/', len);
  497. const char *cp2 = memchr(filename, '.', len);
  498. return cp1 && (!cp2 || (cp1 < cp2)) && tomoyo_correct_word2(filename, len);
  499. }
  500. /**
  501. * tomoyo_correct_path - Validate a pathname.
  502. *
  503. * @filename: The pathname to check.
  504. *
  505. * Check whether the given pathname follows the naming rules.
  506. * Returns true if @filename follows the naming rules, false otherwise.
  507. */
  508. bool tomoyo_correct_path(const char *filename)
  509. {
  510. return tomoyo_correct_path2(filename, strlen(filename));
  511. }
  512. /**
  513. * tomoyo_correct_domain - Check whether the given domainname follows the naming rules.
  514. *
  515. * @domainname: The domainname to check.
  516. *
  517. * Returns true if @domainname follows the naming rules, false otherwise.
  518. */
  519. bool tomoyo_correct_domain(const unsigned char *domainname)
  520. {
  521. if (!domainname || !tomoyo_domain_def(domainname))
  522. return false;
  523. domainname = strchr(domainname, ' ');
  524. if (!domainname++)
  525. return true;
  526. while (1) {
  527. const unsigned char *cp = strchr(domainname, ' ');
  528. if (!cp)
  529. break;
  530. if (!tomoyo_correct_path2(domainname, cp - domainname))
  531. return false;
  532. domainname = cp + 1;
  533. }
  534. return tomoyo_correct_path(domainname);
  535. }
  536. /**
  537. * tomoyo_domain_def - Check whether the given token can be a domainname.
  538. *
  539. * @buffer: The token to check.
  540. *
  541. * Returns true if @buffer possibly be a domainname, false otherwise.
  542. */
  543. bool tomoyo_domain_def(const unsigned char *buffer)
  544. {
  545. const unsigned char *cp;
  546. int len;
  547. if (*buffer != '<')
  548. return false;
  549. cp = strchr(buffer, ' ');
  550. if (!cp)
  551. len = strlen(buffer);
  552. else
  553. len = cp - buffer;
  554. if (buffer[len - 1] != '>' ||
  555. !tomoyo_correct_word2(buffer + 1, len - 2))
  556. return false;
  557. return true;
  558. }
  559. /**
  560. * tomoyo_find_domain - Find a domain by the given name.
  561. *
  562. * @domainname: The domainname to find.
  563. *
  564. * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise.
  565. *
  566. * Caller holds tomoyo_read_lock().
  567. */
  568. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
  569. {
  570. struct tomoyo_domain_info *domain;
  571. struct tomoyo_path_info name;
  572. name.name = domainname;
  573. tomoyo_fill_path_info(&name);
  574. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list,
  575. srcu_read_lock_held(&tomoyo_ss)) {
  576. if (!domain->is_deleted &&
  577. !tomoyo_pathcmp(&name, domain->domainname))
  578. return domain;
  579. }
  580. return NULL;
  581. }
  582. /**
  583. * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
  584. *
  585. * @filename: The string to evaluate.
  586. *
  587. * Returns the initial length without a pattern in @filename.
  588. */
  589. static int tomoyo_const_part_length(const char *filename)
  590. {
  591. char c;
  592. int len = 0;
  593. if (!filename)
  594. return 0;
  595. while ((c = *filename++) != '\0') {
  596. if (c != '\\') {
  597. len++;
  598. continue;
  599. }
  600. c = *filename++;
  601. switch (c) {
  602. case '\\': /* "\\" */
  603. len += 2;
  604. continue;
  605. case '0': /* "\ooo" */
  606. case '1':
  607. case '2':
  608. case '3':
  609. c = *filename++;
  610. if (c < '0' || c > '7')
  611. break;
  612. c = *filename++;
  613. if (c < '0' || c > '7')
  614. break;
  615. len += 4;
  616. continue;
  617. }
  618. break;
  619. }
  620. return len;
  621. }
  622. /**
  623. * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members.
  624. *
  625. * @ptr: Pointer to "struct tomoyo_path_info" to fill in.
  626. *
  627. * The caller sets "struct tomoyo_path_info"->name.
  628. */
  629. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
  630. {
  631. const char *name = ptr->name;
  632. const int len = strlen(name);
  633. ptr->const_len = tomoyo_const_part_length(name);
  634. ptr->is_dir = len && (name[len - 1] == '/');
  635. ptr->is_patterned = (ptr->const_len < len);
  636. ptr->hash = full_name_hash(NULL, name, len);
  637. }
  638. /**
  639. * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
  640. *
  641. * @filename: The start of string to check.
  642. * @filename_end: The end of string to check.
  643. * @pattern: The start of pattern to compare.
  644. * @pattern_end: The end of pattern to compare.
  645. *
  646. * Returns true if @filename matches @pattern, false otherwise.
  647. */
  648. static bool tomoyo_file_matches_pattern2(const char *filename,
  649. const char *filename_end,
  650. const char *pattern,
  651. const char *pattern_end)
  652. {
  653. while (filename < filename_end && pattern < pattern_end) {
  654. char c;
  655. int i;
  656. int j;
  657. if (*pattern != '\\') {
  658. if (*filename++ != *pattern++)
  659. return false;
  660. continue;
  661. }
  662. c = *filename;
  663. pattern++;
  664. switch (*pattern) {
  665. case '?':
  666. if (c == '/') {
  667. return false;
  668. } else if (c == '\\') {
  669. if (filename[1] == '\\')
  670. filename++;
  671. else if (tomoyo_byte_range(filename + 1))
  672. filename += 3;
  673. else
  674. return false;
  675. }
  676. break;
  677. case '\\':
  678. if (c != '\\')
  679. return false;
  680. if (*++filename != '\\')
  681. return false;
  682. break;
  683. case '+':
  684. if (!isdigit(c))
  685. return false;
  686. break;
  687. case 'x':
  688. if (!isxdigit(c))
  689. return false;
  690. break;
  691. case 'a':
  692. if (!tomoyo_alphabet_char(c))
  693. return false;
  694. break;
  695. case '0':
  696. case '1':
  697. case '2':
  698. case '3':
  699. if (c == '\\' && tomoyo_byte_range(filename + 1)
  700. && strncmp(filename + 1, pattern, 3) == 0) {
  701. filename += 3;
  702. pattern += 2;
  703. break;
  704. }
  705. return false; /* Not matched. */
  706. case '*':
  707. case '@':
  708. for (i = 0; i <= filename_end - filename; i++) {
  709. if (tomoyo_file_matches_pattern2(
  710. filename + i, filename_end,
  711. pattern + 1, pattern_end))
  712. return true;
  713. c = filename[i];
  714. if (c == '.' && *pattern == '@')
  715. break;
  716. if (c != '\\')
  717. continue;
  718. if (filename[i + 1] == '\\')
  719. i++;
  720. else if (tomoyo_byte_range(filename + i + 1))
  721. i += 3;
  722. else
  723. break; /* Bad pattern. */
  724. }
  725. return false; /* Not matched. */
  726. default:
  727. j = 0;
  728. c = *pattern;
  729. if (c == '$') {
  730. while (isdigit(filename[j]))
  731. j++;
  732. } else if (c == 'X') {
  733. while (isxdigit(filename[j]))
  734. j++;
  735. } else if (c == 'A') {
  736. while (tomoyo_alphabet_char(filename[j]))
  737. j++;
  738. }
  739. for (i = 1; i <= j; i++) {
  740. if (tomoyo_file_matches_pattern2(
  741. filename + i, filename_end,
  742. pattern + 1, pattern_end))
  743. return true;
  744. }
  745. return false; /* Not matched or bad pattern. */
  746. }
  747. filename++;
  748. pattern++;
  749. }
  750. while (*pattern == '\\' &&
  751. (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
  752. pattern += 2;
  753. return filename == filename_end && pattern == pattern_end;
  754. }
  755. /**
  756. * tomoyo_file_matches_pattern - Pattern matching without '/' character.
  757. *
  758. * @filename: The start of string to check.
  759. * @filename_end: The end of string to check.
  760. * @pattern: The start of pattern to compare.
  761. * @pattern_end: The end of pattern to compare.
  762. *
  763. * Returns true if @filename matches @pattern, false otherwise.
  764. */
  765. static bool tomoyo_file_matches_pattern(const char *filename,
  766. const char *filename_end,
  767. const char *pattern,
  768. const char *pattern_end)
  769. {
  770. const char *pattern_start = pattern;
  771. bool first = true;
  772. bool result;
  773. while (pattern < pattern_end - 1) {
  774. /* Split at "\-" pattern. */
  775. if (*pattern++ != '\\' || *pattern++ != '-')
  776. continue;
  777. result = tomoyo_file_matches_pattern2(filename,
  778. filename_end,
  779. pattern_start,
  780. pattern - 2);
  781. if (first)
  782. result = !result;
  783. if (result)
  784. return false;
  785. first = false;
  786. pattern_start = pattern;
  787. }
  788. result = tomoyo_file_matches_pattern2(filename, filename_end,
  789. pattern_start, pattern_end);
  790. return first ? result : !result;
  791. }
  792. /**
  793. * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
  794. *
  795. * @f: The start of string to check.
  796. * @p: The start of pattern to compare.
  797. *
  798. * Returns true if @f matches @p, false otherwise.
  799. */
  800. static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
  801. {
  802. const char *f_delimiter;
  803. const char *p_delimiter;
  804. while (*f && *p) {
  805. f_delimiter = strchr(f, '/');
  806. if (!f_delimiter)
  807. f_delimiter = f + strlen(f);
  808. p_delimiter = strchr(p, '/');
  809. if (!p_delimiter)
  810. p_delimiter = p + strlen(p);
  811. if (*p == '\\' && *(p + 1) == '{')
  812. goto recursive;
  813. if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
  814. p_delimiter))
  815. return false;
  816. f = f_delimiter;
  817. if (*f)
  818. f++;
  819. p = p_delimiter;
  820. if (*p)
  821. p++;
  822. }
  823. /* Ignore trailing "\*" and "\@" in @pattern. */
  824. while (*p == '\\' &&
  825. (*(p + 1) == '*' || *(p + 1) == '@'))
  826. p += 2;
  827. return !*f && !*p;
  828. recursive:
  829. /*
  830. * The "\{" pattern is permitted only after '/' character.
  831. * This guarantees that below "*(p - 1)" is safe.
  832. * Also, the "\}" pattern is permitted only before '/' character
  833. * so that "\{" + "\}" pair will not break the "\-" operator.
  834. */
  835. if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
  836. *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
  837. return false; /* Bad pattern. */
  838. do {
  839. /* Compare current component with pattern. */
  840. if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
  841. p_delimiter - 2))
  842. break;
  843. /* Proceed to next component. */
  844. f = f_delimiter;
  845. if (!*f)
  846. break;
  847. f++;
  848. /* Continue comparison. */
  849. if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
  850. return true;
  851. f_delimiter = strchr(f, '/');
  852. } while (f_delimiter);
  853. return false; /* Not matched. */
  854. }
  855. /**
  856. * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
  857. *
  858. * @filename: The filename to check.
  859. * @pattern: The pattern to compare.
  860. *
  861. * Returns true if matches, false otherwise.
  862. *
  863. * The following patterns are available.
  864. * \\ \ itself.
  865. * \ooo Octal representation of a byte.
  866. * \* Zero or more repetitions of characters other than '/'.
  867. * \@ Zero or more repetitions of characters other than '/' or '.'.
  868. * \? 1 byte character other than '/'.
  869. * \$ One or more repetitions of decimal digits.
  870. * \+ 1 decimal digit.
  871. * \X One or more repetitions of hexadecimal digits.
  872. * \x 1 hexadecimal digit.
  873. * \A One or more repetitions of alphabet characters.
  874. * \a 1 alphabet character.
  875. *
  876. * \- Subtraction operator.
  877. *
  878. * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
  879. * /dir/dir/dir/ ).
  880. */
  881. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  882. const struct tomoyo_path_info *pattern)
  883. {
  884. const char *f = filename->name;
  885. const char *p = pattern->name;
  886. const int len = pattern->const_len;
  887. /* If @pattern doesn't contain pattern, I can use strcmp(). */
  888. if (!pattern->is_patterned)
  889. return !tomoyo_pathcmp(filename, pattern);
  890. /* Don't compare directory and non-directory. */
  891. if (filename->is_dir != pattern->is_dir)
  892. return false;
  893. /* Compare the initial length without patterns. */
  894. if (strncmp(f, p, len))
  895. return false;
  896. f += len;
  897. p += len;
  898. return tomoyo_path_matches_pattern2(f, p);
  899. }
  900. /**
  901. * tomoyo_get_exe - Get tomoyo_realpath() of current process.
  902. *
  903. * Returns the tomoyo_realpath() of current process on success, NULL otherwise.
  904. *
  905. * This function uses kzalloc(), so the caller must call kfree()
  906. * if this function didn't return NULL.
  907. */
  908. const char *tomoyo_get_exe(void)
  909. {
  910. struct file *exe_file;
  911. const char *cp;
  912. struct mm_struct *mm = current->mm;
  913. if (!mm)
  914. return NULL;
  915. exe_file = get_mm_exe_file(mm);
  916. if (!exe_file)
  917. return NULL;
  918. cp = tomoyo_realpath_from_path(&exe_file->f_path);
  919. fput(exe_file);
  920. return cp;
  921. }
  922. /**
  923. * tomoyo_get_mode - Get MAC mode.
  924. *
  925. * @ns: Pointer to "struct tomoyo_policy_namespace".
  926. * @profile: Profile number.
  927. * @index: Index number of functionality.
  928. *
  929. * Returns mode.
  930. */
  931. int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
  932. const u8 index)
  933. {
  934. u8 mode;
  935. struct tomoyo_profile *p;
  936. if (!tomoyo_policy_loaded)
  937. return TOMOYO_CONFIG_DISABLED;
  938. p = tomoyo_profile(ns, profile);
  939. mode = p->config[index];
  940. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  941. mode = p->config[tomoyo_index2category[index]
  942. + TOMOYO_MAX_MAC_INDEX];
  943. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  944. mode = p->default_config;
  945. return mode & 3;
  946. }
  947. /**
  948. * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members.
  949. *
  950. * @r: Pointer to "struct tomoyo_request_info" to initialize.
  951. * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain().
  952. * @index: Index number of functionality.
  953. *
  954. * Returns mode.
  955. */
  956. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  957. struct tomoyo_domain_info *domain, const u8 index)
  958. {
  959. u8 profile;
  960. memset(r, 0, sizeof(*r));
  961. if (!domain)
  962. domain = tomoyo_domain();
  963. r->domain = domain;
  964. profile = domain->profile;
  965. r->profile = profile;
  966. r->type = index;
  967. r->mode = tomoyo_get_mode(domain->ns, profile, index);
  968. return r->mode;
  969. }
  970. /**
  971. * tomoyo_domain_quota_is_ok - Check for domain's quota.
  972. *
  973. * @r: Pointer to "struct tomoyo_request_info".
  974. *
  975. * Returns true if the domain is not exceeded quota, false otherwise.
  976. *
  977. * Caller holds tomoyo_read_lock().
  978. */
  979. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
  980. {
  981. unsigned int count = 0;
  982. struct tomoyo_domain_info *domain = r->domain;
  983. struct tomoyo_acl_info *ptr;
  984. if (r->mode != TOMOYO_CONFIG_LEARNING)
  985. return false;
  986. if (!domain)
  987. return true;
  988. if (READ_ONCE(domain->flags[TOMOYO_DIF_QUOTA_WARNED]))
  989. return false;
  990. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list,
  991. srcu_read_lock_held(&tomoyo_ss)) {
  992. u16 perm;
  993. if (ptr->is_deleted)
  994. continue;
  995. /*
  996. * Reading perm bitmap might race with tomoyo_merge_*() because
  997. * caller does not hold tomoyo_policy_lock mutex. But exceeding
  998. * max_learning_entry parameter by a few entries does not harm.
  999. */
  1000. switch (ptr->type) {
  1001. case TOMOYO_TYPE_PATH_ACL:
  1002. perm = data_race(container_of(ptr, struct tomoyo_path_acl, head)->perm);
  1003. break;
  1004. case TOMOYO_TYPE_PATH2_ACL:
  1005. perm = data_race(container_of(ptr, struct tomoyo_path2_acl, head)->perm);
  1006. break;
  1007. case TOMOYO_TYPE_PATH_NUMBER_ACL:
  1008. perm = data_race(container_of(ptr, struct tomoyo_path_number_acl, head)
  1009. ->perm);
  1010. break;
  1011. case TOMOYO_TYPE_MKDEV_ACL:
  1012. perm = data_race(container_of(ptr, struct tomoyo_mkdev_acl, head)->perm);
  1013. break;
  1014. case TOMOYO_TYPE_INET_ACL:
  1015. perm = data_race(container_of(ptr, struct tomoyo_inet_acl, head)->perm);
  1016. break;
  1017. case TOMOYO_TYPE_UNIX_ACL:
  1018. perm = data_race(container_of(ptr, struct tomoyo_unix_acl, head)->perm);
  1019. break;
  1020. case TOMOYO_TYPE_MANUAL_TASK_ACL:
  1021. perm = 0;
  1022. break;
  1023. default:
  1024. perm = 1;
  1025. }
  1026. count += hweight16(perm);
  1027. }
  1028. if (count < tomoyo_profile(domain->ns, domain->profile)->
  1029. pref[TOMOYO_PREF_MAX_LEARNING_ENTRY])
  1030. return true;
  1031. WRITE_ONCE(domain->flags[TOMOYO_DIF_QUOTA_WARNED], true);
  1032. /* r->granted = false; */
  1033. tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]);
  1034. #ifndef CONFIG_SECURITY_TOMOYO_INSECURE_BUILTIN_SETTING
  1035. pr_warn("WARNING: Domain '%s' has too many ACLs to hold. Stopped learning mode.\n",
  1036. domain->domainname->name);
  1037. #endif
  1038. return false;
  1039. }