condition.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * security/tomoyo/condition.c
  3. *
  4. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  5. */
  6. #include "common.h"
  7. #include <linux/slab.h>
  8. /* List of "struct tomoyo_condition". */
  9. LIST_HEAD(tomoyo_condition_list);
  10. /**
  11. * tomoyo_scan_exec_realpath - Check "exec.realpath" parameter of "struct tomoyo_condition".
  12. *
  13. * @file: Pointer to "struct file".
  14. * @ptr: Pointer to "struct tomoyo_name_union".
  15. * @match: True if "exec.realpath=", false if "exec.realpath!=".
  16. *
  17. * Returns true on success, false otherwise.
  18. */
  19. static bool tomoyo_scan_exec_realpath(struct file *file,
  20. const struct tomoyo_name_union *ptr,
  21. const bool match)
  22. {
  23. bool result;
  24. struct tomoyo_path_info exe;
  25. if (!file)
  26. return false;
  27. exe.name = tomoyo_realpath_from_path(&file->f_path);
  28. if (!exe.name)
  29. return false;
  30. tomoyo_fill_path_info(&exe);
  31. result = tomoyo_compare_name_union(&exe, ptr);
  32. kfree(exe.name);
  33. return result == match;
  34. }
  35. /**
  36. * tomoyo_get_dqword - tomoyo_get_name() for a quoted string.
  37. *
  38. * @start: String to save.
  39. *
  40. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  41. */
  42. static const struct tomoyo_path_info *tomoyo_get_dqword(char *start)
  43. {
  44. char *cp = start + strlen(start) - 1;
  45. if (cp == start || *start++ != '"' || *cp != '"')
  46. return NULL;
  47. *cp = '\0';
  48. if (*start && !tomoyo_correct_word(start))
  49. return NULL;
  50. return tomoyo_get_name(start);
  51. }
  52. /**
  53. * tomoyo_parse_name_union_quoted - Parse a quoted word.
  54. *
  55. * @param: Pointer to "struct tomoyo_acl_param".
  56. * @ptr: Pointer to "struct tomoyo_name_union".
  57. *
  58. * Returns true on success, false otherwise.
  59. */
  60. static bool tomoyo_parse_name_union_quoted(struct tomoyo_acl_param *param,
  61. struct tomoyo_name_union *ptr)
  62. {
  63. char *filename = param->data;
  64. if (*filename == '@')
  65. return tomoyo_parse_name_union(param, ptr);
  66. ptr->filename = tomoyo_get_dqword(filename);
  67. return ptr->filename != NULL;
  68. }
  69. /**
  70. * tomoyo_same_condition - Check for duplicated "struct tomoyo_condition" entry.
  71. *
  72. * @a: Pointer to "struct tomoyo_condition".
  73. * @b: Pointer to "struct tomoyo_condition".
  74. *
  75. * Returns true if @a == @b, false otherwise.
  76. */
  77. static inline bool tomoyo_same_condition(const struct tomoyo_condition *a,
  78. const struct tomoyo_condition *b)
  79. {
  80. return a->size == b->size && a->condc == b->condc &&
  81. a->numbers_count == b->numbers_count &&
  82. a->names_count == b->names_count &&
  83. !memcmp(a + 1, b + 1, a->size - sizeof(*a));
  84. }
  85. /**
  86. * tomoyo_condition_type - Get condition type.
  87. *
  88. * @word: Keyword string.
  89. *
  90. * Returns one of values in "enum tomoyo_conditions_index" on success,
  91. * TOMOYO_MAX_CONDITION_KEYWORD otherwise.
  92. */
  93. static u8 tomoyo_condition_type(const char *word)
  94. {
  95. u8 i;
  96. for (i = 0; i < TOMOYO_MAX_CONDITION_KEYWORD; i++) {
  97. if (!strcmp(word, tomoyo_condition_keyword[i]))
  98. break;
  99. }
  100. return i;
  101. }
  102. /* Define this to enable debug mode. */
  103. /* #define DEBUG_CONDITION */
  104. #ifdef DEBUG_CONDITION
  105. #define dprintk printk
  106. #else
  107. #define dprintk(...) do { } while (0)
  108. #endif
  109. /**
  110. * tomoyo_commit_condition - Commit "struct tomoyo_condition".
  111. *
  112. * @entry: Pointer to "struct tomoyo_condition".
  113. *
  114. * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
  115. *
  116. * This function merges duplicated entries. This function returns NULL if
  117. * @entry is not duplicated but memory quota for policy has exceeded.
  118. */
  119. static struct tomoyo_condition *tomoyo_commit_condition
  120. (struct tomoyo_condition *entry)
  121. {
  122. struct tomoyo_condition *ptr;
  123. bool found = false;
  124. if (mutex_lock_interruptible(&tomoyo_policy_lock)) {
  125. dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
  126. ptr = NULL;
  127. found = true;
  128. goto out;
  129. }
  130. list_for_each_entry_rcu(ptr, &tomoyo_condition_list, head.list) {
  131. if (!tomoyo_same_condition(ptr, entry))
  132. continue;
  133. /* Same entry found. Share this entry. */
  134. atomic_inc(&ptr->head.users);
  135. found = true;
  136. break;
  137. }
  138. if (!found) {
  139. if (tomoyo_memory_ok(entry)) {
  140. atomic_set(&entry->head.users, 1);
  141. list_add_rcu(&entry->head.list,
  142. &tomoyo_condition_list);
  143. } else {
  144. found = true;
  145. ptr = NULL;
  146. }
  147. }
  148. mutex_unlock(&tomoyo_policy_lock);
  149. out:
  150. if (found) {
  151. tomoyo_del_condition(&entry->head.list);
  152. kfree(entry);
  153. entry = ptr;
  154. }
  155. return entry;
  156. }
  157. /**
  158. * tomoyo_get_condition - Parse condition part.
  159. *
  160. * @param: Pointer to "struct tomoyo_acl_param".
  161. *
  162. * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
  163. */
  164. struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param)
  165. {
  166. struct tomoyo_condition *entry = NULL;
  167. struct tomoyo_condition_element *condp = NULL;
  168. struct tomoyo_number_union *numbers_p = NULL;
  169. struct tomoyo_name_union *names_p = NULL;
  170. struct tomoyo_condition e = { };
  171. char * const start_of_string = param->data;
  172. char * const end_of_string = start_of_string + strlen(start_of_string);
  173. char *pos;
  174. rerun:
  175. pos = start_of_string;
  176. while (1) {
  177. u8 left = -1;
  178. u8 right = -1;
  179. char *left_word = pos;
  180. char *cp;
  181. char *right_word;
  182. bool is_not;
  183. if (!*left_word)
  184. break;
  185. /*
  186. * Since left-hand condition does not allow use of "path_group"
  187. * or "number_group" and environment variable's names do not
  188. * accept '=', it is guaranteed that the original line consists
  189. * of one or more repetition of $left$operator$right blocks
  190. * where "$left is free from '=' and ' '" and "$operator is
  191. * either '=' or '!='" and "$right is free from ' '".
  192. * Therefore, we can reconstruct the original line at the end
  193. * of dry run even if we overwrite $operator with '\0'.
  194. */
  195. cp = strchr(pos, ' ');
  196. if (cp) {
  197. *cp = '\0'; /* Will restore later. */
  198. pos = cp + 1;
  199. } else {
  200. pos = "";
  201. }
  202. right_word = strchr(left_word, '=');
  203. if (!right_word || right_word == left_word)
  204. goto out;
  205. is_not = *(right_word - 1) == '!';
  206. if (is_not)
  207. *(right_word++ - 1) = '\0'; /* Will restore later. */
  208. else if (*(right_word + 1) != '=')
  209. *right_word++ = '\0'; /* Will restore later. */
  210. else
  211. goto out;
  212. dprintk(KERN_WARNING "%u: <%s>%s=<%s>\n", __LINE__, left_word,
  213. is_not ? "!" : "", right_word);
  214. left = tomoyo_condition_type(left_word);
  215. dprintk(KERN_WARNING "%u: <%s> left=%u\n", __LINE__, left_word,
  216. left);
  217. if (left == TOMOYO_MAX_CONDITION_KEYWORD) {
  218. if (!numbers_p) {
  219. e.numbers_count++;
  220. } else {
  221. e.numbers_count--;
  222. left = TOMOYO_NUMBER_UNION;
  223. param->data = left_word;
  224. if (*left_word == '@' ||
  225. !tomoyo_parse_number_union(param,
  226. numbers_p++))
  227. goto out;
  228. }
  229. }
  230. if (!condp)
  231. e.condc++;
  232. else
  233. e.condc--;
  234. if (left == TOMOYO_EXEC_REALPATH ||
  235. left == TOMOYO_SYMLINK_TARGET) {
  236. if (!names_p) {
  237. e.names_count++;
  238. } else {
  239. e.names_count--;
  240. right = TOMOYO_NAME_UNION;
  241. param->data = right_word;
  242. if (!tomoyo_parse_name_union_quoted(param,
  243. names_p++))
  244. goto out;
  245. }
  246. goto store_value;
  247. }
  248. right = tomoyo_condition_type(right_word);
  249. if (right == TOMOYO_MAX_CONDITION_KEYWORD) {
  250. if (!numbers_p) {
  251. e.numbers_count++;
  252. } else {
  253. e.numbers_count--;
  254. right = TOMOYO_NUMBER_UNION;
  255. param->data = right_word;
  256. if (!tomoyo_parse_number_union(param,
  257. numbers_p++))
  258. goto out;
  259. }
  260. }
  261. store_value:
  262. if (!condp) {
  263. dprintk(KERN_WARNING "%u: dry_run left=%u right=%u "
  264. "match=%u\n", __LINE__, left, right, !is_not);
  265. continue;
  266. }
  267. condp->left = left;
  268. condp->right = right;
  269. condp->equals = !is_not;
  270. dprintk(KERN_WARNING "%u: left=%u right=%u match=%u\n",
  271. __LINE__, condp->left, condp->right,
  272. condp->equals);
  273. condp++;
  274. }
  275. dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u\n",
  276. __LINE__, e.condc, e.numbers_count, e.names_count);
  277. if (entry) {
  278. BUG_ON(e.names_count | e.numbers_count | e.condc);
  279. return tomoyo_commit_condition(entry);
  280. }
  281. e.size = sizeof(*entry)
  282. + e.condc * sizeof(struct tomoyo_condition_element)
  283. + e.numbers_count * sizeof(struct tomoyo_number_union)
  284. + e.names_count * sizeof(struct tomoyo_name_union);
  285. entry = kzalloc(e.size, GFP_NOFS);
  286. if (!entry)
  287. return NULL;
  288. *entry = e;
  289. condp = (struct tomoyo_condition_element *) (entry + 1);
  290. numbers_p = (struct tomoyo_number_union *) (condp + e.condc);
  291. names_p = (struct tomoyo_name_union *) (numbers_p + e.numbers_count);
  292. {
  293. bool flag = false;
  294. for (pos = start_of_string; pos < end_of_string; pos++) {
  295. if (*pos)
  296. continue;
  297. if (flag) /* Restore " ". */
  298. *pos = ' ';
  299. else if (*(pos + 1) == '=') /* Restore "!=". */
  300. *pos = '!';
  301. else /* Restore "=". */
  302. *pos = '=';
  303. flag = !flag;
  304. }
  305. }
  306. goto rerun;
  307. out:
  308. dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
  309. if (entry) {
  310. tomoyo_del_condition(&entry->head.list);
  311. kfree(entry);
  312. }
  313. return NULL;
  314. }
  315. /**
  316. * tomoyo_get_attributes - Revalidate "struct inode".
  317. *
  318. * @obj: Pointer to "struct tomoyo_obj_info".
  319. *
  320. * Returns nothing.
  321. */
  322. void tomoyo_get_attributes(struct tomoyo_obj_info *obj)
  323. {
  324. u8 i;
  325. struct dentry *dentry = NULL;
  326. for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
  327. struct inode *inode;
  328. switch (i) {
  329. case TOMOYO_PATH1:
  330. dentry = obj->path1.dentry;
  331. if (!dentry)
  332. continue;
  333. break;
  334. case TOMOYO_PATH2:
  335. dentry = obj->path2.dentry;
  336. if (!dentry)
  337. continue;
  338. break;
  339. default:
  340. if (!dentry)
  341. continue;
  342. dentry = dget_parent(dentry);
  343. break;
  344. }
  345. inode = dentry->d_inode;
  346. if (inode) {
  347. struct tomoyo_mini_stat *stat = &obj->stat[i];
  348. stat->uid = inode->i_uid;
  349. stat->gid = inode->i_gid;
  350. stat->ino = inode->i_ino;
  351. stat->mode = inode->i_mode;
  352. stat->dev = inode->i_sb->s_dev;
  353. stat->rdev = inode->i_rdev;
  354. obj->stat_valid[i] = true;
  355. }
  356. if (i & 1) /* i == TOMOYO_PATH1_PARENT ||
  357. i == TOMOYO_PATH2_PARENT */
  358. dput(dentry);
  359. }
  360. }
  361. /**
  362. * tomoyo_condition - Check condition part.
  363. *
  364. * @r: Pointer to "struct tomoyo_request_info".
  365. * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
  366. *
  367. * Returns true on success, false otherwise.
  368. *
  369. * Caller holds tomoyo_read_lock().
  370. */
  371. bool tomoyo_condition(struct tomoyo_request_info *r,
  372. const struct tomoyo_condition *cond)
  373. {
  374. u32 i;
  375. unsigned long min_v[2] = { 0, 0 };
  376. unsigned long max_v[2] = { 0, 0 };
  377. const struct tomoyo_condition_element *condp;
  378. const struct tomoyo_number_union *numbers_p;
  379. const struct tomoyo_name_union *names_p;
  380. struct tomoyo_obj_info *obj;
  381. u16 condc;
  382. if (!cond)
  383. return true;
  384. condc = cond->condc;
  385. obj = r->obj;
  386. condp = (struct tomoyo_condition_element *) (cond + 1);
  387. numbers_p = (const struct tomoyo_number_union *) (condp + condc);
  388. names_p = (const struct tomoyo_name_union *)
  389. (numbers_p + cond->numbers_count);
  390. for (i = 0; i < condc; i++) {
  391. const bool match = condp->equals;
  392. const u8 left = condp->left;
  393. const u8 right = condp->right;
  394. bool is_bitop[2] = { false, false };
  395. u8 j;
  396. condp++;
  397. /* Check string expressions. */
  398. if (right == TOMOYO_NAME_UNION) {
  399. const struct tomoyo_name_union *ptr = names_p++;
  400. switch (left) {
  401. struct tomoyo_path_info *symlink;
  402. struct tomoyo_execve *ee;
  403. struct file *file;
  404. case TOMOYO_SYMLINK_TARGET:
  405. symlink = obj ? obj->symlink_target : NULL;
  406. if (!symlink ||
  407. !tomoyo_compare_name_union(symlink, ptr)
  408. == match)
  409. goto out;
  410. break;
  411. case TOMOYO_EXEC_REALPATH:
  412. ee = r->ee;
  413. file = ee ? ee->bprm->file : NULL;
  414. if (!tomoyo_scan_exec_realpath(file, ptr,
  415. match))
  416. goto out;
  417. break;
  418. }
  419. continue;
  420. }
  421. /* Check numeric or bit-op expressions. */
  422. for (j = 0; j < 2; j++) {
  423. const u8 index = j ? right : left;
  424. unsigned long value = 0;
  425. switch (index) {
  426. case TOMOYO_TASK_UID:
  427. value = current_uid();
  428. break;
  429. case TOMOYO_TASK_EUID:
  430. value = current_euid();
  431. break;
  432. case TOMOYO_TASK_SUID:
  433. value = current_suid();
  434. break;
  435. case TOMOYO_TASK_FSUID:
  436. value = current_fsuid();
  437. break;
  438. case TOMOYO_TASK_GID:
  439. value = current_gid();
  440. break;
  441. case TOMOYO_TASK_EGID:
  442. value = current_egid();
  443. break;
  444. case TOMOYO_TASK_SGID:
  445. value = current_sgid();
  446. break;
  447. case TOMOYO_TASK_FSGID:
  448. value = current_fsgid();
  449. break;
  450. case TOMOYO_TASK_PID:
  451. value = tomoyo_sys_getpid();
  452. break;
  453. case TOMOYO_TASK_PPID:
  454. value = tomoyo_sys_getppid();
  455. break;
  456. case TOMOYO_TYPE_IS_SOCKET:
  457. value = S_IFSOCK;
  458. break;
  459. case TOMOYO_TYPE_IS_SYMLINK:
  460. value = S_IFLNK;
  461. break;
  462. case TOMOYO_TYPE_IS_FILE:
  463. value = S_IFREG;
  464. break;
  465. case TOMOYO_TYPE_IS_BLOCK_DEV:
  466. value = S_IFBLK;
  467. break;
  468. case TOMOYO_TYPE_IS_DIRECTORY:
  469. value = S_IFDIR;
  470. break;
  471. case TOMOYO_TYPE_IS_CHAR_DEV:
  472. value = S_IFCHR;
  473. break;
  474. case TOMOYO_TYPE_IS_FIFO:
  475. value = S_IFIFO;
  476. break;
  477. case TOMOYO_MODE_SETUID:
  478. value = S_ISUID;
  479. break;
  480. case TOMOYO_MODE_SETGID:
  481. value = S_ISGID;
  482. break;
  483. case TOMOYO_MODE_STICKY:
  484. value = S_ISVTX;
  485. break;
  486. case TOMOYO_MODE_OWNER_READ:
  487. value = S_IRUSR;
  488. break;
  489. case TOMOYO_MODE_OWNER_WRITE:
  490. value = S_IWUSR;
  491. break;
  492. case TOMOYO_MODE_OWNER_EXECUTE:
  493. value = S_IXUSR;
  494. break;
  495. case TOMOYO_MODE_GROUP_READ:
  496. value = S_IRGRP;
  497. break;
  498. case TOMOYO_MODE_GROUP_WRITE:
  499. value = S_IWGRP;
  500. break;
  501. case TOMOYO_MODE_GROUP_EXECUTE:
  502. value = S_IXGRP;
  503. break;
  504. case TOMOYO_MODE_OTHERS_READ:
  505. value = S_IROTH;
  506. break;
  507. case TOMOYO_MODE_OTHERS_WRITE:
  508. value = S_IWOTH;
  509. break;
  510. case TOMOYO_MODE_OTHERS_EXECUTE:
  511. value = S_IXOTH;
  512. break;
  513. case TOMOYO_NUMBER_UNION:
  514. /* Fetch values later. */
  515. break;
  516. default:
  517. if (!obj)
  518. goto out;
  519. if (!obj->validate_done) {
  520. tomoyo_get_attributes(obj);
  521. obj->validate_done = true;
  522. }
  523. {
  524. u8 stat_index;
  525. struct tomoyo_mini_stat *stat;
  526. switch (index) {
  527. case TOMOYO_PATH1_UID:
  528. case TOMOYO_PATH1_GID:
  529. case TOMOYO_PATH1_INO:
  530. case TOMOYO_PATH1_MAJOR:
  531. case TOMOYO_PATH1_MINOR:
  532. case TOMOYO_PATH1_TYPE:
  533. case TOMOYO_PATH1_DEV_MAJOR:
  534. case TOMOYO_PATH1_DEV_MINOR:
  535. case TOMOYO_PATH1_PERM:
  536. stat_index = TOMOYO_PATH1;
  537. break;
  538. case TOMOYO_PATH2_UID:
  539. case TOMOYO_PATH2_GID:
  540. case TOMOYO_PATH2_INO:
  541. case TOMOYO_PATH2_MAJOR:
  542. case TOMOYO_PATH2_MINOR:
  543. case TOMOYO_PATH2_TYPE:
  544. case TOMOYO_PATH2_DEV_MAJOR:
  545. case TOMOYO_PATH2_DEV_MINOR:
  546. case TOMOYO_PATH2_PERM:
  547. stat_index = TOMOYO_PATH2;
  548. break;
  549. case TOMOYO_PATH1_PARENT_UID:
  550. case TOMOYO_PATH1_PARENT_GID:
  551. case TOMOYO_PATH1_PARENT_INO:
  552. case TOMOYO_PATH1_PARENT_PERM:
  553. stat_index =
  554. TOMOYO_PATH1_PARENT;
  555. break;
  556. case TOMOYO_PATH2_PARENT_UID:
  557. case TOMOYO_PATH2_PARENT_GID:
  558. case TOMOYO_PATH2_PARENT_INO:
  559. case TOMOYO_PATH2_PARENT_PERM:
  560. stat_index =
  561. TOMOYO_PATH2_PARENT;
  562. break;
  563. default:
  564. goto out;
  565. }
  566. if (!obj->stat_valid[stat_index])
  567. goto out;
  568. stat = &obj->stat[stat_index];
  569. switch (index) {
  570. case TOMOYO_PATH1_UID:
  571. case TOMOYO_PATH2_UID:
  572. case TOMOYO_PATH1_PARENT_UID:
  573. case TOMOYO_PATH2_PARENT_UID:
  574. value = stat->uid;
  575. break;
  576. case TOMOYO_PATH1_GID:
  577. case TOMOYO_PATH2_GID:
  578. case TOMOYO_PATH1_PARENT_GID:
  579. case TOMOYO_PATH2_PARENT_GID:
  580. value = stat->gid;
  581. break;
  582. case TOMOYO_PATH1_INO:
  583. case TOMOYO_PATH2_INO:
  584. case TOMOYO_PATH1_PARENT_INO:
  585. case TOMOYO_PATH2_PARENT_INO:
  586. value = stat->ino;
  587. break;
  588. case TOMOYO_PATH1_MAJOR:
  589. case TOMOYO_PATH2_MAJOR:
  590. value = MAJOR(stat->dev);
  591. break;
  592. case TOMOYO_PATH1_MINOR:
  593. case TOMOYO_PATH2_MINOR:
  594. value = MINOR(stat->dev);
  595. break;
  596. case TOMOYO_PATH1_TYPE:
  597. case TOMOYO_PATH2_TYPE:
  598. value = stat->mode & S_IFMT;
  599. break;
  600. case TOMOYO_PATH1_DEV_MAJOR:
  601. case TOMOYO_PATH2_DEV_MAJOR:
  602. value = MAJOR(stat->rdev);
  603. break;
  604. case TOMOYO_PATH1_DEV_MINOR:
  605. case TOMOYO_PATH2_DEV_MINOR:
  606. value = MINOR(stat->rdev);
  607. break;
  608. case TOMOYO_PATH1_PERM:
  609. case TOMOYO_PATH2_PERM:
  610. case TOMOYO_PATH1_PARENT_PERM:
  611. case TOMOYO_PATH2_PARENT_PERM:
  612. value = stat->mode & S_IALLUGO;
  613. break;
  614. }
  615. }
  616. break;
  617. }
  618. max_v[j] = value;
  619. min_v[j] = value;
  620. switch (index) {
  621. case TOMOYO_MODE_SETUID:
  622. case TOMOYO_MODE_SETGID:
  623. case TOMOYO_MODE_STICKY:
  624. case TOMOYO_MODE_OWNER_READ:
  625. case TOMOYO_MODE_OWNER_WRITE:
  626. case TOMOYO_MODE_OWNER_EXECUTE:
  627. case TOMOYO_MODE_GROUP_READ:
  628. case TOMOYO_MODE_GROUP_WRITE:
  629. case TOMOYO_MODE_GROUP_EXECUTE:
  630. case TOMOYO_MODE_OTHERS_READ:
  631. case TOMOYO_MODE_OTHERS_WRITE:
  632. case TOMOYO_MODE_OTHERS_EXECUTE:
  633. is_bitop[j] = true;
  634. }
  635. }
  636. if (left == TOMOYO_NUMBER_UNION) {
  637. /* Fetch values now. */
  638. const struct tomoyo_number_union *ptr = numbers_p++;
  639. min_v[0] = ptr->values[0];
  640. max_v[0] = ptr->values[1];
  641. }
  642. if (right == TOMOYO_NUMBER_UNION) {
  643. /* Fetch values now. */
  644. const struct tomoyo_number_union *ptr = numbers_p++;
  645. if (ptr->group) {
  646. if (tomoyo_number_matches_group(min_v[0],
  647. max_v[0],
  648. ptr->group)
  649. == match)
  650. continue;
  651. } else {
  652. if ((min_v[0] <= ptr->values[1] &&
  653. max_v[0] >= ptr->values[0]) == match)
  654. continue;
  655. }
  656. goto out;
  657. }
  658. /*
  659. * Bit operation is valid only when counterpart value
  660. * represents permission.
  661. */
  662. if (is_bitop[0] && is_bitop[1]) {
  663. goto out;
  664. } else if (is_bitop[0]) {
  665. switch (right) {
  666. case TOMOYO_PATH1_PERM:
  667. case TOMOYO_PATH1_PARENT_PERM:
  668. case TOMOYO_PATH2_PERM:
  669. case TOMOYO_PATH2_PARENT_PERM:
  670. if (!(max_v[0] & max_v[1]) == !match)
  671. continue;
  672. }
  673. goto out;
  674. } else if (is_bitop[1]) {
  675. switch (left) {
  676. case TOMOYO_PATH1_PERM:
  677. case TOMOYO_PATH1_PARENT_PERM:
  678. case TOMOYO_PATH2_PERM:
  679. case TOMOYO_PATH2_PARENT_PERM:
  680. if (!(max_v[0] & max_v[1]) == !match)
  681. continue;
  682. }
  683. goto out;
  684. }
  685. /* Normal value range comparison. */
  686. if ((min_v[0] <= max_v[1] && max_v[0] >= min_v[1]) == match)
  687. continue;
  688. out:
  689. return false;
  690. }
  691. return true;
  692. }