condition.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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_argv - Check argv[] in "struct linux_binbrm".
  12. *
  13. * @index: Index number of @arg_ptr.
  14. * @arg_ptr: Contents of argv[@index].
  15. * @argc: Length of @argv.
  16. * @argv: Pointer to "struct tomoyo_argv".
  17. * @checked: Set to true if @argv[@index] was found.
  18. *
  19. * Returns true on success, false otherwise.
  20. */
  21. static bool tomoyo_argv(const unsigned int index, const char *arg_ptr,
  22. const int argc, const struct tomoyo_argv *argv,
  23. u8 *checked)
  24. {
  25. int i;
  26. struct tomoyo_path_info arg;
  27. arg.name = arg_ptr;
  28. for (i = 0; i < argc; argv++, checked++, i++) {
  29. bool result;
  30. if (index != argv->index)
  31. continue;
  32. *checked = 1;
  33. tomoyo_fill_path_info(&arg);
  34. result = tomoyo_path_matches_pattern(&arg, argv->value);
  35. if (argv->is_not)
  36. result = !result;
  37. if (!result)
  38. return false;
  39. }
  40. return true;
  41. }
  42. /**
  43. * tomoyo_envp - Check envp[] in "struct linux_binbrm".
  44. *
  45. * @env_name: The name of environment variable.
  46. * @env_value: The value of environment variable.
  47. * @envc: Length of @envp.
  48. * @envp: Pointer to "struct tomoyo_envp".
  49. * @checked: Set to true if @envp[@env_name] was found.
  50. *
  51. * Returns true on success, false otherwise.
  52. */
  53. static bool tomoyo_envp(const char *env_name, const char *env_value,
  54. const int envc, const struct tomoyo_envp *envp,
  55. u8 *checked)
  56. {
  57. int i;
  58. struct tomoyo_path_info name;
  59. struct tomoyo_path_info value;
  60. name.name = env_name;
  61. tomoyo_fill_path_info(&name);
  62. value.name = env_value;
  63. tomoyo_fill_path_info(&value);
  64. for (i = 0; i < envc; envp++, checked++, i++) {
  65. bool result;
  66. if (!tomoyo_path_matches_pattern(&name, envp->name))
  67. continue;
  68. *checked = 1;
  69. if (envp->value) {
  70. result = tomoyo_path_matches_pattern(&value,
  71. envp->value);
  72. if (envp->is_not)
  73. result = !result;
  74. } else {
  75. result = true;
  76. if (!envp->is_not)
  77. result = !result;
  78. }
  79. if (!result)
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * tomoyo_scan_bprm - Scan "struct linux_binprm".
  86. *
  87. * @ee: Pointer to "struct tomoyo_execve".
  88. * @argc: Length of @argc.
  89. * @argv: Pointer to "struct tomoyo_argv".
  90. * @envc: Length of @envp.
  91. * @envp: Poiner to "struct tomoyo_envp".
  92. *
  93. * Returns true on success, false otherwise.
  94. */
  95. static bool tomoyo_scan_bprm(struct tomoyo_execve *ee,
  96. const u16 argc, const struct tomoyo_argv *argv,
  97. const u16 envc, const struct tomoyo_envp *envp)
  98. {
  99. struct linux_binprm *bprm = ee->bprm;
  100. struct tomoyo_page_dump *dump = &ee->dump;
  101. char *arg_ptr = ee->tmp;
  102. int arg_len = 0;
  103. unsigned long pos = bprm->p;
  104. int offset = pos % PAGE_SIZE;
  105. int argv_count = bprm->argc;
  106. int envp_count = bprm->envc;
  107. bool result = true;
  108. u8 local_checked[32];
  109. u8 *checked;
  110. if (argc + envc <= sizeof(local_checked)) {
  111. checked = local_checked;
  112. memset(local_checked, 0, sizeof(local_checked));
  113. } else {
  114. checked = kzalloc(argc + envc, GFP_NOFS);
  115. if (!checked)
  116. return false;
  117. }
  118. while (argv_count || envp_count) {
  119. if (!tomoyo_dump_page(bprm, pos, dump)) {
  120. result = false;
  121. goto out;
  122. }
  123. pos += PAGE_SIZE - offset;
  124. while (offset < PAGE_SIZE) {
  125. /* Read. */
  126. const char *kaddr = dump->data;
  127. const unsigned char c = kaddr[offset++];
  128. if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
  129. if (c == '\\') {
  130. arg_ptr[arg_len++] = '\\';
  131. arg_ptr[arg_len++] = '\\';
  132. } else if (c > ' ' && c < 127) {
  133. arg_ptr[arg_len++] = c;
  134. } else {
  135. arg_ptr[arg_len++] = '\\';
  136. arg_ptr[arg_len++] = (c >> 6) + '0';
  137. arg_ptr[arg_len++] =
  138. ((c >> 3) & 7) + '0';
  139. arg_ptr[arg_len++] = (c & 7) + '0';
  140. }
  141. } else {
  142. arg_ptr[arg_len] = '\0';
  143. }
  144. if (c)
  145. continue;
  146. /* Check. */
  147. if (argv_count) {
  148. if (!tomoyo_argv(bprm->argc - argv_count,
  149. arg_ptr, argc, argv,
  150. checked)) {
  151. result = false;
  152. break;
  153. }
  154. argv_count--;
  155. } else if (envp_count) {
  156. char *cp = strchr(arg_ptr, '=');
  157. if (cp) {
  158. *cp = '\0';
  159. if (!tomoyo_envp(arg_ptr, cp + 1,
  160. envc, envp,
  161. checked + argc)) {
  162. result = false;
  163. break;
  164. }
  165. }
  166. envp_count--;
  167. } else {
  168. break;
  169. }
  170. arg_len = 0;
  171. }
  172. offset = 0;
  173. if (!result)
  174. break;
  175. }
  176. out:
  177. if (result) {
  178. int i;
  179. /* Check not-yet-checked entries. */
  180. for (i = 0; i < argc; i++) {
  181. if (checked[i])
  182. continue;
  183. /*
  184. * Return true only if all unchecked indexes in
  185. * bprm->argv[] are not matched.
  186. */
  187. if (argv[i].is_not)
  188. continue;
  189. result = false;
  190. break;
  191. }
  192. for (i = 0; i < envc; envp++, i++) {
  193. if (checked[argc + i])
  194. continue;
  195. /*
  196. * Return true only if all unchecked environ variables
  197. * in bprm->envp[] are either undefined or not matched.
  198. */
  199. if ((!envp->value && !envp->is_not) ||
  200. (envp->value && envp->is_not))
  201. continue;
  202. result = false;
  203. break;
  204. }
  205. }
  206. if (checked != local_checked)
  207. kfree(checked);
  208. return result;
  209. }
  210. /**
  211. * tomoyo_scan_exec_realpath - Check "exec.realpath" parameter of "struct tomoyo_condition".
  212. *
  213. * @file: Pointer to "struct file".
  214. * @ptr: Pointer to "struct tomoyo_name_union".
  215. * @match: True if "exec.realpath=", false if "exec.realpath!=".
  216. *
  217. * Returns true on success, false otherwise.
  218. */
  219. static bool tomoyo_scan_exec_realpath(struct file *file,
  220. const struct tomoyo_name_union *ptr,
  221. const bool match)
  222. {
  223. bool result;
  224. struct tomoyo_path_info exe;
  225. if (!file)
  226. return false;
  227. exe.name = tomoyo_realpath_from_path(&file->f_path);
  228. if (!exe.name)
  229. return false;
  230. tomoyo_fill_path_info(&exe);
  231. result = tomoyo_compare_name_union(&exe, ptr);
  232. kfree(exe.name);
  233. return result == match;
  234. }
  235. /**
  236. * tomoyo_get_dqword - tomoyo_get_name() for a quoted string.
  237. *
  238. * @start: String to save.
  239. *
  240. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  241. */
  242. static const struct tomoyo_path_info *tomoyo_get_dqword(char *start)
  243. {
  244. char *cp = start + strlen(start) - 1;
  245. if (cp == start || *start++ != '"' || *cp != '"')
  246. return NULL;
  247. *cp = '\0';
  248. if (*start && !tomoyo_correct_word(start))
  249. return NULL;
  250. return tomoyo_get_name(start);
  251. }
  252. /**
  253. * tomoyo_parse_name_union_quoted - Parse a quoted word.
  254. *
  255. * @param: Pointer to "struct tomoyo_acl_param".
  256. * @ptr: Pointer to "struct tomoyo_name_union".
  257. *
  258. * Returns true on success, false otherwise.
  259. */
  260. static bool tomoyo_parse_name_union_quoted(struct tomoyo_acl_param *param,
  261. struct tomoyo_name_union *ptr)
  262. {
  263. char *filename = param->data;
  264. if (*filename == '@')
  265. return tomoyo_parse_name_union(param, ptr);
  266. ptr->filename = tomoyo_get_dqword(filename);
  267. return ptr->filename != NULL;
  268. }
  269. /**
  270. * tomoyo_parse_argv - Parse an argv[] condition part.
  271. *
  272. * @left: Lefthand value.
  273. * @right: Righthand value.
  274. * @argv: Pointer to "struct tomoyo_argv".
  275. *
  276. * Returns true on success, false otherwise.
  277. */
  278. static bool tomoyo_parse_argv(char *left, char *right,
  279. struct tomoyo_argv *argv)
  280. {
  281. if (tomoyo_parse_ulong(&argv->index, &left) !=
  282. TOMOYO_VALUE_TYPE_DECIMAL || *left++ != ']' || *left)
  283. return false;
  284. argv->value = tomoyo_get_dqword(right);
  285. return argv->value != NULL;
  286. }
  287. /**
  288. * tomoyo_parse_envp - Parse an envp[] condition part.
  289. *
  290. * @left: Lefthand value.
  291. * @right: Righthand value.
  292. * @envp: Pointer to "struct tomoyo_envp".
  293. *
  294. * Returns true on success, false otherwise.
  295. */
  296. static bool tomoyo_parse_envp(char *left, char *right,
  297. struct tomoyo_envp *envp)
  298. {
  299. const struct tomoyo_path_info *name;
  300. const struct tomoyo_path_info *value;
  301. char *cp = left + strlen(left) - 1;
  302. if (*cp-- != ']' || *cp != '"')
  303. goto out;
  304. *cp = '\0';
  305. if (!tomoyo_correct_word(left))
  306. goto out;
  307. name = tomoyo_get_name(left);
  308. if (!name)
  309. goto out;
  310. if (!strcmp(right, "NULL")) {
  311. value = NULL;
  312. } else {
  313. value = tomoyo_get_dqword(right);
  314. if (!value) {
  315. tomoyo_put_name(name);
  316. goto out;
  317. }
  318. }
  319. envp->name = name;
  320. envp->value = value;
  321. return true;
  322. out:
  323. return false;
  324. }
  325. /**
  326. * tomoyo_same_condition - Check for duplicated "struct tomoyo_condition" entry.
  327. *
  328. * @a: Pointer to "struct tomoyo_condition".
  329. * @b: Pointer to "struct tomoyo_condition".
  330. *
  331. * Returns true if @a == @b, false otherwise.
  332. */
  333. static inline bool tomoyo_same_condition(const struct tomoyo_condition *a,
  334. const struct tomoyo_condition *b)
  335. {
  336. return a->size == b->size && a->condc == b->condc &&
  337. a->numbers_count == b->numbers_count &&
  338. a->names_count == b->names_count &&
  339. a->argc == b->argc && a->envc == b->envc &&
  340. !memcmp(a + 1, b + 1, a->size - sizeof(*a));
  341. }
  342. /**
  343. * tomoyo_condition_type - Get condition type.
  344. *
  345. * @word: Keyword string.
  346. *
  347. * Returns one of values in "enum tomoyo_conditions_index" on success,
  348. * TOMOYO_MAX_CONDITION_KEYWORD otherwise.
  349. */
  350. static u8 tomoyo_condition_type(const char *word)
  351. {
  352. u8 i;
  353. for (i = 0; i < TOMOYO_MAX_CONDITION_KEYWORD; i++) {
  354. if (!strcmp(word, tomoyo_condition_keyword[i]))
  355. break;
  356. }
  357. return i;
  358. }
  359. /* Define this to enable debug mode. */
  360. /* #define DEBUG_CONDITION */
  361. #ifdef DEBUG_CONDITION
  362. #define dprintk printk
  363. #else
  364. #define dprintk(...) do { } while (0)
  365. #endif
  366. /**
  367. * tomoyo_commit_condition - Commit "struct tomoyo_condition".
  368. *
  369. * @entry: Pointer to "struct tomoyo_condition".
  370. *
  371. * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
  372. *
  373. * This function merges duplicated entries. This function returns NULL if
  374. * @entry is not duplicated but memory quota for policy has exceeded.
  375. */
  376. static struct tomoyo_condition *tomoyo_commit_condition
  377. (struct tomoyo_condition *entry)
  378. {
  379. struct tomoyo_condition *ptr;
  380. bool found = false;
  381. if (mutex_lock_interruptible(&tomoyo_policy_lock)) {
  382. dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
  383. ptr = NULL;
  384. found = true;
  385. goto out;
  386. }
  387. list_for_each_entry_rcu(ptr, &tomoyo_condition_list, head.list) {
  388. if (!tomoyo_same_condition(ptr, entry))
  389. continue;
  390. /* Same entry found. Share this entry. */
  391. atomic_inc(&ptr->head.users);
  392. found = true;
  393. break;
  394. }
  395. if (!found) {
  396. if (tomoyo_memory_ok(entry)) {
  397. atomic_set(&entry->head.users, 1);
  398. list_add_rcu(&entry->head.list,
  399. &tomoyo_condition_list);
  400. } else {
  401. found = true;
  402. ptr = NULL;
  403. }
  404. }
  405. mutex_unlock(&tomoyo_policy_lock);
  406. out:
  407. if (found) {
  408. tomoyo_del_condition(&entry->head.list);
  409. kfree(entry);
  410. entry = ptr;
  411. }
  412. return entry;
  413. }
  414. /**
  415. * tomoyo_get_condition - Parse condition part.
  416. *
  417. * @param: Pointer to "struct tomoyo_acl_param".
  418. *
  419. * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
  420. */
  421. struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param)
  422. {
  423. struct tomoyo_condition *entry = NULL;
  424. struct tomoyo_condition_element *condp = NULL;
  425. struct tomoyo_number_union *numbers_p = NULL;
  426. struct tomoyo_name_union *names_p = NULL;
  427. struct tomoyo_argv *argv = NULL;
  428. struct tomoyo_envp *envp = NULL;
  429. struct tomoyo_condition e = { };
  430. char * const start_of_string = param->data;
  431. char * const end_of_string = start_of_string + strlen(start_of_string);
  432. char *pos;
  433. rerun:
  434. pos = start_of_string;
  435. while (1) {
  436. u8 left = -1;
  437. u8 right = -1;
  438. char *left_word = pos;
  439. char *cp;
  440. char *right_word;
  441. bool is_not;
  442. if (!*left_word)
  443. break;
  444. /*
  445. * Since left-hand condition does not allow use of "path_group"
  446. * or "number_group" and environment variable's names do not
  447. * accept '=', it is guaranteed that the original line consists
  448. * of one or more repetition of $left$operator$right blocks
  449. * where "$left is free from '=' and ' '" and "$operator is
  450. * either '=' or '!='" and "$right is free from ' '".
  451. * Therefore, we can reconstruct the original line at the end
  452. * of dry run even if we overwrite $operator with '\0'.
  453. */
  454. cp = strchr(pos, ' ');
  455. if (cp) {
  456. *cp = '\0'; /* Will restore later. */
  457. pos = cp + 1;
  458. } else {
  459. pos = "";
  460. }
  461. right_word = strchr(left_word, '=');
  462. if (!right_word || right_word == left_word)
  463. goto out;
  464. is_not = *(right_word - 1) == '!';
  465. if (is_not)
  466. *(right_word++ - 1) = '\0'; /* Will restore later. */
  467. else if (*(right_word + 1) != '=')
  468. *right_word++ = '\0'; /* Will restore later. */
  469. else
  470. goto out;
  471. dprintk(KERN_WARNING "%u: <%s>%s=<%s>\n", __LINE__, left_word,
  472. is_not ? "!" : "", right_word);
  473. if (!strncmp(left_word, "exec.argv[", 10)) {
  474. if (!argv) {
  475. e.argc++;
  476. e.condc++;
  477. } else {
  478. e.argc--;
  479. e.condc--;
  480. left = TOMOYO_ARGV_ENTRY;
  481. argv->is_not = is_not;
  482. if (!tomoyo_parse_argv(left_word + 10,
  483. right_word, argv++))
  484. goto out;
  485. }
  486. goto store_value;
  487. }
  488. if (!strncmp(left_word, "exec.envp[\"", 11)) {
  489. if (!envp) {
  490. e.envc++;
  491. e.condc++;
  492. } else {
  493. e.envc--;
  494. e.condc--;
  495. left = TOMOYO_ENVP_ENTRY;
  496. envp->is_not = is_not;
  497. if (!tomoyo_parse_envp(left_word + 11,
  498. right_word, envp++))
  499. goto out;
  500. }
  501. goto store_value;
  502. }
  503. left = tomoyo_condition_type(left_word);
  504. dprintk(KERN_WARNING "%u: <%s> left=%u\n", __LINE__, left_word,
  505. left);
  506. if (left == TOMOYO_MAX_CONDITION_KEYWORD) {
  507. if (!numbers_p) {
  508. e.numbers_count++;
  509. } else {
  510. e.numbers_count--;
  511. left = TOMOYO_NUMBER_UNION;
  512. param->data = left_word;
  513. if (*left_word == '@' ||
  514. !tomoyo_parse_number_union(param,
  515. numbers_p++))
  516. goto out;
  517. }
  518. }
  519. if (!condp)
  520. e.condc++;
  521. else
  522. e.condc--;
  523. if (left == TOMOYO_EXEC_REALPATH ||
  524. left == TOMOYO_SYMLINK_TARGET) {
  525. if (!names_p) {
  526. e.names_count++;
  527. } else {
  528. e.names_count--;
  529. right = TOMOYO_NAME_UNION;
  530. param->data = right_word;
  531. if (!tomoyo_parse_name_union_quoted(param,
  532. names_p++))
  533. goto out;
  534. }
  535. goto store_value;
  536. }
  537. right = tomoyo_condition_type(right_word);
  538. if (right == TOMOYO_MAX_CONDITION_KEYWORD) {
  539. if (!numbers_p) {
  540. e.numbers_count++;
  541. } else {
  542. e.numbers_count--;
  543. right = TOMOYO_NUMBER_UNION;
  544. param->data = right_word;
  545. if (!tomoyo_parse_number_union(param,
  546. numbers_p++))
  547. goto out;
  548. }
  549. }
  550. store_value:
  551. if (!condp) {
  552. dprintk(KERN_WARNING "%u: dry_run left=%u right=%u "
  553. "match=%u\n", __LINE__, left, right, !is_not);
  554. continue;
  555. }
  556. condp->left = left;
  557. condp->right = right;
  558. condp->equals = !is_not;
  559. dprintk(KERN_WARNING "%u: left=%u right=%u match=%u\n",
  560. __LINE__, condp->left, condp->right,
  561. condp->equals);
  562. condp++;
  563. }
  564. dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u ac=%u ec=%u\n",
  565. __LINE__, e.condc, e.numbers_count, e.names_count, e.argc,
  566. e.envc);
  567. if (entry) {
  568. BUG_ON(e.names_count | e.numbers_count | e.argc | e.envc |
  569. e.condc);
  570. return tomoyo_commit_condition(entry);
  571. }
  572. e.size = sizeof(*entry)
  573. + e.condc * sizeof(struct tomoyo_condition_element)
  574. + e.numbers_count * sizeof(struct tomoyo_number_union)
  575. + e.names_count * sizeof(struct tomoyo_name_union)
  576. + e.argc * sizeof(struct tomoyo_argv)
  577. + e.envc * sizeof(struct tomoyo_envp);
  578. entry = kzalloc(e.size, GFP_NOFS);
  579. if (!entry)
  580. return NULL;
  581. *entry = e;
  582. condp = (struct tomoyo_condition_element *) (entry + 1);
  583. numbers_p = (struct tomoyo_number_union *) (condp + e.condc);
  584. names_p = (struct tomoyo_name_union *) (numbers_p + e.numbers_count);
  585. argv = (struct tomoyo_argv *) (names_p + e.names_count);
  586. envp = (struct tomoyo_envp *) (argv + e.argc);
  587. {
  588. bool flag = false;
  589. for (pos = start_of_string; pos < end_of_string; pos++) {
  590. if (*pos)
  591. continue;
  592. if (flag) /* Restore " ". */
  593. *pos = ' ';
  594. else if (*(pos + 1) == '=') /* Restore "!=". */
  595. *pos = '!';
  596. else /* Restore "=". */
  597. *pos = '=';
  598. flag = !flag;
  599. }
  600. }
  601. goto rerun;
  602. out:
  603. dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
  604. if (entry) {
  605. tomoyo_del_condition(&entry->head.list);
  606. kfree(entry);
  607. }
  608. return NULL;
  609. }
  610. /**
  611. * tomoyo_get_attributes - Revalidate "struct inode".
  612. *
  613. * @obj: Pointer to "struct tomoyo_obj_info".
  614. *
  615. * Returns nothing.
  616. */
  617. void tomoyo_get_attributes(struct tomoyo_obj_info *obj)
  618. {
  619. u8 i;
  620. struct dentry *dentry = NULL;
  621. for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
  622. struct inode *inode;
  623. switch (i) {
  624. case TOMOYO_PATH1:
  625. dentry = obj->path1.dentry;
  626. if (!dentry)
  627. continue;
  628. break;
  629. case TOMOYO_PATH2:
  630. dentry = obj->path2.dentry;
  631. if (!dentry)
  632. continue;
  633. break;
  634. default:
  635. if (!dentry)
  636. continue;
  637. dentry = dget_parent(dentry);
  638. break;
  639. }
  640. inode = dentry->d_inode;
  641. if (inode) {
  642. struct tomoyo_mini_stat *stat = &obj->stat[i];
  643. stat->uid = inode->i_uid;
  644. stat->gid = inode->i_gid;
  645. stat->ino = inode->i_ino;
  646. stat->mode = inode->i_mode;
  647. stat->dev = inode->i_sb->s_dev;
  648. stat->rdev = inode->i_rdev;
  649. obj->stat_valid[i] = true;
  650. }
  651. if (i & 1) /* i == TOMOYO_PATH1_PARENT ||
  652. i == TOMOYO_PATH2_PARENT */
  653. dput(dentry);
  654. }
  655. }
  656. /**
  657. * tomoyo_condition - Check condition part.
  658. *
  659. * @r: Pointer to "struct tomoyo_request_info".
  660. * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
  661. *
  662. * Returns true on success, false otherwise.
  663. *
  664. * Caller holds tomoyo_read_lock().
  665. */
  666. bool tomoyo_condition(struct tomoyo_request_info *r,
  667. const struct tomoyo_condition *cond)
  668. {
  669. u32 i;
  670. unsigned long min_v[2] = { 0, 0 };
  671. unsigned long max_v[2] = { 0, 0 };
  672. const struct tomoyo_condition_element *condp;
  673. const struct tomoyo_number_union *numbers_p;
  674. const struct tomoyo_name_union *names_p;
  675. const struct tomoyo_argv *argv;
  676. const struct tomoyo_envp *envp;
  677. struct tomoyo_obj_info *obj;
  678. u16 condc;
  679. u16 argc;
  680. u16 envc;
  681. struct linux_binprm *bprm = NULL;
  682. if (!cond)
  683. return true;
  684. condc = cond->condc;
  685. argc = cond->argc;
  686. envc = cond->envc;
  687. obj = r->obj;
  688. if (r->ee)
  689. bprm = r->ee->bprm;
  690. if (!bprm && (argc || envc))
  691. return false;
  692. condp = (struct tomoyo_condition_element *) (cond + 1);
  693. numbers_p = (const struct tomoyo_number_union *) (condp + condc);
  694. names_p = (const struct tomoyo_name_union *)
  695. (numbers_p + cond->numbers_count);
  696. argv = (const struct tomoyo_argv *) (names_p + cond->names_count);
  697. envp = (const struct tomoyo_envp *) (argv + argc);
  698. for (i = 0; i < condc; i++) {
  699. const bool match = condp->equals;
  700. const u8 left = condp->left;
  701. const u8 right = condp->right;
  702. bool is_bitop[2] = { false, false };
  703. u8 j;
  704. condp++;
  705. /* Check argv[] and envp[] later. */
  706. if (left == TOMOYO_ARGV_ENTRY || left == TOMOYO_ENVP_ENTRY)
  707. continue;
  708. /* Check string expressions. */
  709. if (right == TOMOYO_NAME_UNION) {
  710. const struct tomoyo_name_union *ptr = names_p++;
  711. switch (left) {
  712. struct tomoyo_path_info *symlink;
  713. struct tomoyo_execve *ee;
  714. struct file *file;
  715. case TOMOYO_SYMLINK_TARGET:
  716. symlink = obj ? obj->symlink_target : NULL;
  717. if (!symlink ||
  718. !tomoyo_compare_name_union(symlink, ptr)
  719. == match)
  720. goto out;
  721. break;
  722. case TOMOYO_EXEC_REALPATH:
  723. ee = r->ee;
  724. file = ee ? ee->bprm->file : NULL;
  725. if (!tomoyo_scan_exec_realpath(file, ptr,
  726. match))
  727. goto out;
  728. break;
  729. }
  730. continue;
  731. }
  732. /* Check numeric or bit-op expressions. */
  733. for (j = 0; j < 2; j++) {
  734. const u8 index = j ? right : left;
  735. unsigned long value = 0;
  736. switch (index) {
  737. case TOMOYO_TASK_UID:
  738. value = current_uid();
  739. break;
  740. case TOMOYO_TASK_EUID:
  741. value = current_euid();
  742. break;
  743. case TOMOYO_TASK_SUID:
  744. value = current_suid();
  745. break;
  746. case TOMOYO_TASK_FSUID:
  747. value = current_fsuid();
  748. break;
  749. case TOMOYO_TASK_GID:
  750. value = current_gid();
  751. break;
  752. case TOMOYO_TASK_EGID:
  753. value = current_egid();
  754. break;
  755. case TOMOYO_TASK_SGID:
  756. value = current_sgid();
  757. break;
  758. case TOMOYO_TASK_FSGID:
  759. value = current_fsgid();
  760. break;
  761. case TOMOYO_TASK_PID:
  762. value = tomoyo_sys_getpid();
  763. break;
  764. case TOMOYO_TASK_PPID:
  765. value = tomoyo_sys_getppid();
  766. break;
  767. case TOMOYO_TYPE_IS_SOCKET:
  768. value = S_IFSOCK;
  769. break;
  770. case TOMOYO_TYPE_IS_SYMLINK:
  771. value = S_IFLNK;
  772. break;
  773. case TOMOYO_TYPE_IS_FILE:
  774. value = S_IFREG;
  775. break;
  776. case TOMOYO_TYPE_IS_BLOCK_DEV:
  777. value = S_IFBLK;
  778. break;
  779. case TOMOYO_TYPE_IS_DIRECTORY:
  780. value = S_IFDIR;
  781. break;
  782. case TOMOYO_TYPE_IS_CHAR_DEV:
  783. value = S_IFCHR;
  784. break;
  785. case TOMOYO_TYPE_IS_FIFO:
  786. value = S_IFIFO;
  787. break;
  788. case TOMOYO_MODE_SETUID:
  789. value = S_ISUID;
  790. break;
  791. case TOMOYO_MODE_SETGID:
  792. value = S_ISGID;
  793. break;
  794. case TOMOYO_MODE_STICKY:
  795. value = S_ISVTX;
  796. break;
  797. case TOMOYO_MODE_OWNER_READ:
  798. value = S_IRUSR;
  799. break;
  800. case TOMOYO_MODE_OWNER_WRITE:
  801. value = S_IWUSR;
  802. break;
  803. case TOMOYO_MODE_OWNER_EXECUTE:
  804. value = S_IXUSR;
  805. break;
  806. case TOMOYO_MODE_GROUP_READ:
  807. value = S_IRGRP;
  808. break;
  809. case TOMOYO_MODE_GROUP_WRITE:
  810. value = S_IWGRP;
  811. break;
  812. case TOMOYO_MODE_GROUP_EXECUTE:
  813. value = S_IXGRP;
  814. break;
  815. case TOMOYO_MODE_OTHERS_READ:
  816. value = S_IROTH;
  817. break;
  818. case TOMOYO_MODE_OTHERS_WRITE:
  819. value = S_IWOTH;
  820. break;
  821. case TOMOYO_MODE_OTHERS_EXECUTE:
  822. value = S_IXOTH;
  823. break;
  824. case TOMOYO_EXEC_ARGC:
  825. if (!bprm)
  826. goto out;
  827. value = bprm->argc;
  828. break;
  829. case TOMOYO_EXEC_ENVC:
  830. if (!bprm)
  831. goto out;
  832. value = bprm->envc;
  833. break;
  834. case TOMOYO_NUMBER_UNION:
  835. /* Fetch values later. */
  836. break;
  837. default:
  838. if (!obj)
  839. goto out;
  840. if (!obj->validate_done) {
  841. tomoyo_get_attributes(obj);
  842. obj->validate_done = true;
  843. }
  844. {
  845. u8 stat_index;
  846. struct tomoyo_mini_stat *stat;
  847. switch (index) {
  848. case TOMOYO_PATH1_UID:
  849. case TOMOYO_PATH1_GID:
  850. case TOMOYO_PATH1_INO:
  851. case TOMOYO_PATH1_MAJOR:
  852. case TOMOYO_PATH1_MINOR:
  853. case TOMOYO_PATH1_TYPE:
  854. case TOMOYO_PATH1_DEV_MAJOR:
  855. case TOMOYO_PATH1_DEV_MINOR:
  856. case TOMOYO_PATH1_PERM:
  857. stat_index = TOMOYO_PATH1;
  858. break;
  859. case TOMOYO_PATH2_UID:
  860. case TOMOYO_PATH2_GID:
  861. case TOMOYO_PATH2_INO:
  862. case TOMOYO_PATH2_MAJOR:
  863. case TOMOYO_PATH2_MINOR:
  864. case TOMOYO_PATH2_TYPE:
  865. case TOMOYO_PATH2_DEV_MAJOR:
  866. case TOMOYO_PATH2_DEV_MINOR:
  867. case TOMOYO_PATH2_PERM:
  868. stat_index = TOMOYO_PATH2;
  869. break;
  870. case TOMOYO_PATH1_PARENT_UID:
  871. case TOMOYO_PATH1_PARENT_GID:
  872. case TOMOYO_PATH1_PARENT_INO:
  873. case TOMOYO_PATH1_PARENT_PERM:
  874. stat_index =
  875. TOMOYO_PATH1_PARENT;
  876. break;
  877. case TOMOYO_PATH2_PARENT_UID:
  878. case TOMOYO_PATH2_PARENT_GID:
  879. case TOMOYO_PATH2_PARENT_INO:
  880. case TOMOYO_PATH2_PARENT_PERM:
  881. stat_index =
  882. TOMOYO_PATH2_PARENT;
  883. break;
  884. default:
  885. goto out;
  886. }
  887. if (!obj->stat_valid[stat_index])
  888. goto out;
  889. stat = &obj->stat[stat_index];
  890. switch (index) {
  891. case TOMOYO_PATH1_UID:
  892. case TOMOYO_PATH2_UID:
  893. case TOMOYO_PATH1_PARENT_UID:
  894. case TOMOYO_PATH2_PARENT_UID:
  895. value = stat->uid;
  896. break;
  897. case TOMOYO_PATH1_GID:
  898. case TOMOYO_PATH2_GID:
  899. case TOMOYO_PATH1_PARENT_GID:
  900. case TOMOYO_PATH2_PARENT_GID:
  901. value = stat->gid;
  902. break;
  903. case TOMOYO_PATH1_INO:
  904. case TOMOYO_PATH2_INO:
  905. case TOMOYO_PATH1_PARENT_INO:
  906. case TOMOYO_PATH2_PARENT_INO:
  907. value = stat->ino;
  908. break;
  909. case TOMOYO_PATH1_MAJOR:
  910. case TOMOYO_PATH2_MAJOR:
  911. value = MAJOR(stat->dev);
  912. break;
  913. case TOMOYO_PATH1_MINOR:
  914. case TOMOYO_PATH2_MINOR:
  915. value = MINOR(stat->dev);
  916. break;
  917. case TOMOYO_PATH1_TYPE:
  918. case TOMOYO_PATH2_TYPE:
  919. value = stat->mode & S_IFMT;
  920. break;
  921. case TOMOYO_PATH1_DEV_MAJOR:
  922. case TOMOYO_PATH2_DEV_MAJOR:
  923. value = MAJOR(stat->rdev);
  924. break;
  925. case TOMOYO_PATH1_DEV_MINOR:
  926. case TOMOYO_PATH2_DEV_MINOR:
  927. value = MINOR(stat->rdev);
  928. break;
  929. case TOMOYO_PATH1_PERM:
  930. case TOMOYO_PATH2_PERM:
  931. case TOMOYO_PATH1_PARENT_PERM:
  932. case TOMOYO_PATH2_PARENT_PERM:
  933. value = stat->mode & S_IALLUGO;
  934. break;
  935. }
  936. }
  937. break;
  938. }
  939. max_v[j] = value;
  940. min_v[j] = value;
  941. switch (index) {
  942. case TOMOYO_MODE_SETUID:
  943. case TOMOYO_MODE_SETGID:
  944. case TOMOYO_MODE_STICKY:
  945. case TOMOYO_MODE_OWNER_READ:
  946. case TOMOYO_MODE_OWNER_WRITE:
  947. case TOMOYO_MODE_OWNER_EXECUTE:
  948. case TOMOYO_MODE_GROUP_READ:
  949. case TOMOYO_MODE_GROUP_WRITE:
  950. case TOMOYO_MODE_GROUP_EXECUTE:
  951. case TOMOYO_MODE_OTHERS_READ:
  952. case TOMOYO_MODE_OTHERS_WRITE:
  953. case TOMOYO_MODE_OTHERS_EXECUTE:
  954. is_bitop[j] = true;
  955. }
  956. }
  957. if (left == TOMOYO_NUMBER_UNION) {
  958. /* Fetch values now. */
  959. const struct tomoyo_number_union *ptr = numbers_p++;
  960. min_v[0] = ptr->values[0];
  961. max_v[0] = ptr->values[1];
  962. }
  963. if (right == TOMOYO_NUMBER_UNION) {
  964. /* Fetch values now. */
  965. const struct tomoyo_number_union *ptr = numbers_p++;
  966. if (ptr->group) {
  967. if (tomoyo_number_matches_group(min_v[0],
  968. max_v[0],
  969. ptr->group)
  970. == match)
  971. continue;
  972. } else {
  973. if ((min_v[0] <= ptr->values[1] &&
  974. max_v[0] >= ptr->values[0]) == match)
  975. continue;
  976. }
  977. goto out;
  978. }
  979. /*
  980. * Bit operation is valid only when counterpart value
  981. * represents permission.
  982. */
  983. if (is_bitop[0] && is_bitop[1]) {
  984. goto out;
  985. } else if (is_bitop[0]) {
  986. switch (right) {
  987. case TOMOYO_PATH1_PERM:
  988. case TOMOYO_PATH1_PARENT_PERM:
  989. case TOMOYO_PATH2_PERM:
  990. case TOMOYO_PATH2_PARENT_PERM:
  991. if (!(max_v[0] & max_v[1]) == !match)
  992. continue;
  993. }
  994. goto out;
  995. } else if (is_bitop[1]) {
  996. switch (left) {
  997. case TOMOYO_PATH1_PERM:
  998. case TOMOYO_PATH1_PARENT_PERM:
  999. case TOMOYO_PATH2_PERM:
  1000. case TOMOYO_PATH2_PARENT_PERM:
  1001. if (!(max_v[0] & max_v[1]) == !match)
  1002. continue;
  1003. }
  1004. goto out;
  1005. }
  1006. /* Normal value range comparison. */
  1007. if ((min_v[0] <= max_v[1] && max_v[0] >= min_v[1]) == match)
  1008. continue;
  1009. out:
  1010. return false;
  1011. }
  1012. /* Check argv[] and envp[] now. */
  1013. if (r->ee && (argc || envc))
  1014. return tomoyo_scan_bprm(r->ee, argc, argv, envc, envp);
  1015. return true;
  1016. }