condition.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. a->grant_log == b->grant_log && a->transit == b->transit &&
  341. !memcmp(a + 1, b + 1, a->size - sizeof(*a));
  342. }
  343. /**
  344. * tomoyo_condition_type - Get condition type.
  345. *
  346. * @word: Keyword string.
  347. *
  348. * Returns one of values in "enum tomoyo_conditions_index" on success,
  349. * TOMOYO_MAX_CONDITION_KEYWORD otherwise.
  350. */
  351. static u8 tomoyo_condition_type(const char *word)
  352. {
  353. u8 i;
  354. for (i = 0; i < TOMOYO_MAX_CONDITION_KEYWORD; i++) {
  355. if (!strcmp(word, tomoyo_condition_keyword[i]))
  356. break;
  357. }
  358. return i;
  359. }
  360. /* Define this to enable debug mode. */
  361. /* #define DEBUG_CONDITION */
  362. #ifdef DEBUG_CONDITION
  363. #define dprintk printk
  364. #else
  365. #define dprintk(...) do { } while (0)
  366. #endif
  367. /**
  368. * tomoyo_commit_condition - Commit "struct tomoyo_condition".
  369. *
  370. * @entry: Pointer to "struct tomoyo_condition".
  371. *
  372. * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
  373. *
  374. * This function merges duplicated entries. This function returns NULL if
  375. * @entry is not duplicated but memory quota for policy has exceeded.
  376. */
  377. static struct tomoyo_condition *tomoyo_commit_condition
  378. (struct tomoyo_condition *entry)
  379. {
  380. struct tomoyo_condition *ptr;
  381. bool found = false;
  382. if (mutex_lock_interruptible(&tomoyo_policy_lock)) {
  383. dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
  384. ptr = NULL;
  385. found = true;
  386. goto out;
  387. }
  388. list_for_each_entry(ptr, &tomoyo_condition_list, head.list) {
  389. if (!tomoyo_same_condition(ptr, entry) ||
  390. atomic_read(&ptr->head.users) == TOMOYO_GC_IN_PROGRESS)
  391. continue;
  392. /* Same entry found. Share this entry. */
  393. atomic_inc(&ptr->head.users);
  394. found = true;
  395. break;
  396. }
  397. if (!found) {
  398. if (tomoyo_memory_ok(entry)) {
  399. atomic_set(&entry->head.users, 1);
  400. list_add(&entry->head.list, &tomoyo_condition_list);
  401. } else {
  402. found = true;
  403. ptr = NULL;
  404. }
  405. }
  406. mutex_unlock(&tomoyo_policy_lock);
  407. out:
  408. if (found) {
  409. tomoyo_del_condition(&entry->head.list);
  410. kfree(entry);
  411. entry = ptr;
  412. }
  413. return entry;
  414. }
  415. /**
  416. * tomoyo_get_transit_preference - Parse domain transition preference for execve().
  417. *
  418. * @param: Pointer to "struct tomoyo_acl_param".
  419. * @e: Pointer to "struct tomoyo_condition".
  420. *
  421. * Returns the condition string part.
  422. */
  423. static char *tomoyo_get_transit_preference(struct tomoyo_acl_param *param,
  424. struct tomoyo_condition *e)
  425. {
  426. char * const pos = param->data;
  427. bool flag;
  428. if (*pos == '<') {
  429. e->transit = tomoyo_get_domainname(param);
  430. goto done;
  431. }
  432. {
  433. char *cp = strchr(pos, ' ');
  434. if (cp)
  435. *cp = '\0';
  436. flag = tomoyo_correct_path(pos) || !strcmp(pos, "keep") ||
  437. !strcmp(pos, "initialize") || !strcmp(pos, "reset") ||
  438. !strcmp(pos, "child") || !strcmp(pos, "parent");
  439. if (cp)
  440. *cp = ' ';
  441. }
  442. if (!flag)
  443. return pos;
  444. e->transit = tomoyo_get_name(tomoyo_read_token(param));
  445. done:
  446. if (e->transit)
  447. return param->data;
  448. /*
  449. * Return a bad read-only condition string that will let
  450. * tomoyo_get_condition() return NULL.
  451. */
  452. return "/";
  453. }
  454. /**
  455. * tomoyo_get_condition - Parse condition part.
  456. *
  457. * @param: Pointer to "struct tomoyo_acl_param".
  458. *
  459. * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
  460. */
  461. struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param)
  462. {
  463. struct tomoyo_condition *entry = NULL;
  464. struct tomoyo_condition_element *condp = NULL;
  465. struct tomoyo_number_union *numbers_p = NULL;
  466. struct tomoyo_name_union *names_p = NULL;
  467. struct tomoyo_argv *argv = NULL;
  468. struct tomoyo_envp *envp = NULL;
  469. struct tomoyo_condition e = { };
  470. char * const start_of_string =
  471. tomoyo_get_transit_preference(param, &e);
  472. char * const end_of_string = start_of_string + strlen(start_of_string);
  473. char *pos;
  474. rerun:
  475. pos = start_of_string;
  476. while (1) {
  477. u8 left = -1;
  478. u8 right = -1;
  479. char *left_word = pos;
  480. char *cp;
  481. char *right_word;
  482. bool is_not;
  483. if (!*left_word)
  484. break;
  485. /*
  486. * Since left-hand condition does not allow use of "path_group"
  487. * or "number_group" and environment variable's names do not
  488. * accept '=', it is guaranteed that the original line consists
  489. * of one or more repetition of $left$operator$right blocks
  490. * where "$left is free from '=' and ' '" and "$operator is
  491. * either '=' or '!='" and "$right is free from ' '".
  492. * Therefore, we can reconstruct the original line at the end
  493. * of dry run even if we overwrite $operator with '\0'.
  494. */
  495. cp = strchr(pos, ' ');
  496. if (cp) {
  497. *cp = '\0'; /* Will restore later. */
  498. pos = cp + 1;
  499. } else {
  500. pos = "";
  501. }
  502. right_word = strchr(left_word, '=');
  503. if (!right_word || right_word == left_word)
  504. goto out;
  505. is_not = *(right_word - 1) == '!';
  506. if (is_not)
  507. *(right_word++ - 1) = '\0'; /* Will restore later. */
  508. else if (*(right_word + 1) != '=')
  509. *right_word++ = '\0'; /* Will restore later. */
  510. else
  511. goto out;
  512. dprintk(KERN_WARNING "%u: <%s>%s=<%s>\n", __LINE__, left_word,
  513. is_not ? "!" : "", right_word);
  514. if (!strcmp(left_word, "grant_log")) {
  515. if (entry) {
  516. if (is_not ||
  517. entry->grant_log != TOMOYO_GRANTLOG_AUTO)
  518. goto out;
  519. else if (!strcmp(right_word, "yes"))
  520. entry->grant_log = TOMOYO_GRANTLOG_YES;
  521. else if (!strcmp(right_word, "no"))
  522. entry->grant_log = TOMOYO_GRANTLOG_NO;
  523. else
  524. goto out;
  525. }
  526. continue;
  527. }
  528. if (!strncmp(left_word, "exec.argv[", 10)) {
  529. if (!argv) {
  530. e.argc++;
  531. e.condc++;
  532. } else {
  533. e.argc--;
  534. e.condc--;
  535. left = TOMOYO_ARGV_ENTRY;
  536. argv->is_not = is_not;
  537. if (!tomoyo_parse_argv(left_word + 10,
  538. right_word, argv++))
  539. goto out;
  540. }
  541. goto store_value;
  542. }
  543. if (!strncmp(left_word, "exec.envp[\"", 11)) {
  544. if (!envp) {
  545. e.envc++;
  546. e.condc++;
  547. } else {
  548. e.envc--;
  549. e.condc--;
  550. left = TOMOYO_ENVP_ENTRY;
  551. envp->is_not = is_not;
  552. if (!tomoyo_parse_envp(left_word + 11,
  553. right_word, envp++))
  554. goto out;
  555. }
  556. goto store_value;
  557. }
  558. left = tomoyo_condition_type(left_word);
  559. dprintk(KERN_WARNING "%u: <%s> left=%u\n", __LINE__, left_word,
  560. left);
  561. if (left == TOMOYO_MAX_CONDITION_KEYWORD) {
  562. if (!numbers_p) {
  563. e.numbers_count++;
  564. } else {
  565. e.numbers_count--;
  566. left = TOMOYO_NUMBER_UNION;
  567. param->data = left_word;
  568. if (*left_word == '@' ||
  569. !tomoyo_parse_number_union(param,
  570. numbers_p++))
  571. goto out;
  572. }
  573. }
  574. if (!condp)
  575. e.condc++;
  576. else
  577. e.condc--;
  578. if (left == TOMOYO_EXEC_REALPATH ||
  579. left == TOMOYO_SYMLINK_TARGET) {
  580. if (!names_p) {
  581. e.names_count++;
  582. } else {
  583. e.names_count--;
  584. right = TOMOYO_NAME_UNION;
  585. param->data = right_word;
  586. if (!tomoyo_parse_name_union_quoted(param,
  587. names_p++))
  588. goto out;
  589. }
  590. goto store_value;
  591. }
  592. right = tomoyo_condition_type(right_word);
  593. if (right == TOMOYO_MAX_CONDITION_KEYWORD) {
  594. if (!numbers_p) {
  595. e.numbers_count++;
  596. } else {
  597. e.numbers_count--;
  598. right = TOMOYO_NUMBER_UNION;
  599. param->data = right_word;
  600. if (!tomoyo_parse_number_union(param,
  601. numbers_p++))
  602. goto out;
  603. }
  604. }
  605. store_value:
  606. if (!condp) {
  607. dprintk(KERN_WARNING "%u: dry_run left=%u right=%u "
  608. "match=%u\n", __LINE__, left, right, !is_not);
  609. continue;
  610. }
  611. condp->left = left;
  612. condp->right = right;
  613. condp->equals = !is_not;
  614. dprintk(KERN_WARNING "%u: left=%u right=%u match=%u\n",
  615. __LINE__, condp->left, condp->right,
  616. condp->equals);
  617. condp++;
  618. }
  619. dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u ac=%u ec=%u\n",
  620. __LINE__, e.condc, e.numbers_count, e.names_count, e.argc,
  621. e.envc);
  622. if (entry) {
  623. BUG_ON(e.names_count | e.numbers_count | e.argc | e.envc |
  624. e.condc);
  625. return tomoyo_commit_condition(entry);
  626. }
  627. e.size = sizeof(*entry)
  628. + e.condc * sizeof(struct tomoyo_condition_element)
  629. + e.numbers_count * sizeof(struct tomoyo_number_union)
  630. + e.names_count * sizeof(struct tomoyo_name_union)
  631. + e.argc * sizeof(struct tomoyo_argv)
  632. + e.envc * sizeof(struct tomoyo_envp);
  633. entry = kzalloc(e.size, GFP_NOFS);
  634. if (!entry)
  635. goto out2;
  636. *entry = e;
  637. e.transit = NULL;
  638. condp = (struct tomoyo_condition_element *) (entry + 1);
  639. numbers_p = (struct tomoyo_number_union *) (condp + e.condc);
  640. names_p = (struct tomoyo_name_union *) (numbers_p + e.numbers_count);
  641. argv = (struct tomoyo_argv *) (names_p + e.names_count);
  642. envp = (struct tomoyo_envp *) (argv + e.argc);
  643. {
  644. bool flag = false;
  645. for (pos = start_of_string; pos < end_of_string; pos++) {
  646. if (*pos)
  647. continue;
  648. if (flag) /* Restore " ". */
  649. *pos = ' ';
  650. else if (*(pos + 1) == '=') /* Restore "!=". */
  651. *pos = '!';
  652. else /* Restore "=". */
  653. *pos = '=';
  654. flag = !flag;
  655. }
  656. }
  657. goto rerun;
  658. out:
  659. dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
  660. if (entry) {
  661. tomoyo_del_condition(&entry->head.list);
  662. kfree(entry);
  663. }
  664. out2:
  665. tomoyo_put_name(e.transit);
  666. return NULL;
  667. }
  668. /**
  669. * tomoyo_get_attributes - Revalidate "struct inode".
  670. *
  671. * @obj: Pointer to "struct tomoyo_obj_info".
  672. *
  673. * Returns nothing.
  674. */
  675. void tomoyo_get_attributes(struct tomoyo_obj_info *obj)
  676. {
  677. u8 i;
  678. struct dentry *dentry = NULL;
  679. for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
  680. struct inode *inode;
  681. switch (i) {
  682. case TOMOYO_PATH1:
  683. dentry = obj->path1.dentry;
  684. if (!dentry)
  685. continue;
  686. break;
  687. case TOMOYO_PATH2:
  688. dentry = obj->path2.dentry;
  689. if (!dentry)
  690. continue;
  691. break;
  692. default:
  693. if (!dentry)
  694. continue;
  695. dentry = dget_parent(dentry);
  696. break;
  697. }
  698. inode = d_backing_inode(dentry);
  699. if (inode) {
  700. struct tomoyo_mini_stat *stat = &obj->stat[i];
  701. stat->uid = inode->i_uid;
  702. stat->gid = inode->i_gid;
  703. stat->ino = inode->i_ino;
  704. stat->mode = inode->i_mode;
  705. stat->dev = inode->i_sb->s_dev;
  706. stat->rdev = inode->i_rdev;
  707. obj->stat_valid[i] = true;
  708. }
  709. if (i & 1) /* i == TOMOYO_PATH1_PARENT ||
  710. i == TOMOYO_PATH2_PARENT */
  711. dput(dentry);
  712. }
  713. }
  714. /**
  715. * tomoyo_condition - Check condition part.
  716. *
  717. * @r: Pointer to "struct tomoyo_request_info".
  718. * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
  719. *
  720. * Returns true on success, false otherwise.
  721. *
  722. * Caller holds tomoyo_read_lock().
  723. */
  724. bool tomoyo_condition(struct tomoyo_request_info *r,
  725. const struct tomoyo_condition *cond)
  726. {
  727. u32 i;
  728. unsigned long min_v[2] = { 0, 0 };
  729. unsigned long max_v[2] = { 0, 0 };
  730. const struct tomoyo_condition_element *condp;
  731. const struct tomoyo_number_union *numbers_p;
  732. const struct tomoyo_name_union *names_p;
  733. const struct tomoyo_argv *argv;
  734. const struct tomoyo_envp *envp;
  735. struct tomoyo_obj_info *obj;
  736. u16 condc;
  737. u16 argc;
  738. u16 envc;
  739. struct linux_binprm *bprm = NULL;
  740. if (!cond)
  741. return true;
  742. condc = cond->condc;
  743. argc = cond->argc;
  744. envc = cond->envc;
  745. obj = r->obj;
  746. if (r->ee)
  747. bprm = r->ee->bprm;
  748. if (!bprm && (argc || envc))
  749. return false;
  750. condp = (struct tomoyo_condition_element *) (cond + 1);
  751. numbers_p = (const struct tomoyo_number_union *) (condp + condc);
  752. names_p = (const struct tomoyo_name_union *)
  753. (numbers_p + cond->numbers_count);
  754. argv = (const struct tomoyo_argv *) (names_p + cond->names_count);
  755. envp = (const struct tomoyo_envp *) (argv + argc);
  756. for (i = 0; i < condc; i++) {
  757. const bool match = condp->equals;
  758. const u8 left = condp->left;
  759. const u8 right = condp->right;
  760. bool is_bitop[2] = { false, false };
  761. u8 j;
  762. condp++;
  763. /* Check argv[] and envp[] later. */
  764. if (left == TOMOYO_ARGV_ENTRY || left == TOMOYO_ENVP_ENTRY)
  765. continue;
  766. /* Check string expressions. */
  767. if (right == TOMOYO_NAME_UNION) {
  768. const struct tomoyo_name_union *ptr = names_p++;
  769. switch (left) {
  770. struct tomoyo_path_info *symlink;
  771. struct tomoyo_execve *ee;
  772. struct file *file;
  773. case TOMOYO_SYMLINK_TARGET:
  774. symlink = obj ? obj->symlink_target : NULL;
  775. if (!symlink ||
  776. !tomoyo_compare_name_union(symlink, ptr)
  777. == match)
  778. goto out;
  779. break;
  780. case TOMOYO_EXEC_REALPATH:
  781. ee = r->ee;
  782. file = ee ? ee->bprm->file : NULL;
  783. if (!tomoyo_scan_exec_realpath(file, ptr,
  784. match))
  785. goto out;
  786. break;
  787. }
  788. continue;
  789. }
  790. /* Check numeric or bit-op expressions. */
  791. for (j = 0; j < 2; j++) {
  792. const u8 index = j ? right : left;
  793. unsigned long value = 0;
  794. switch (index) {
  795. case TOMOYO_TASK_UID:
  796. value = from_kuid(&init_user_ns, current_uid());
  797. break;
  798. case TOMOYO_TASK_EUID:
  799. value = from_kuid(&init_user_ns, current_euid());
  800. break;
  801. case TOMOYO_TASK_SUID:
  802. value = from_kuid(&init_user_ns, current_suid());
  803. break;
  804. case TOMOYO_TASK_FSUID:
  805. value = from_kuid(&init_user_ns, current_fsuid());
  806. break;
  807. case TOMOYO_TASK_GID:
  808. value = from_kgid(&init_user_ns, current_gid());
  809. break;
  810. case TOMOYO_TASK_EGID:
  811. value = from_kgid(&init_user_ns, current_egid());
  812. break;
  813. case TOMOYO_TASK_SGID:
  814. value = from_kgid(&init_user_ns, current_sgid());
  815. break;
  816. case TOMOYO_TASK_FSGID:
  817. value = from_kgid(&init_user_ns, current_fsgid());
  818. break;
  819. case TOMOYO_TASK_PID:
  820. value = tomoyo_sys_getpid();
  821. break;
  822. case TOMOYO_TASK_PPID:
  823. value = tomoyo_sys_getppid();
  824. break;
  825. case TOMOYO_TYPE_IS_SOCKET:
  826. value = S_IFSOCK;
  827. break;
  828. case TOMOYO_TYPE_IS_SYMLINK:
  829. value = S_IFLNK;
  830. break;
  831. case TOMOYO_TYPE_IS_FILE:
  832. value = S_IFREG;
  833. break;
  834. case TOMOYO_TYPE_IS_BLOCK_DEV:
  835. value = S_IFBLK;
  836. break;
  837. case TOMOYO_TYPE_IS_DIRECTORY:
  838. value = S_IFDIR;
  839. break;
  840. case TOMOYO_TYPE_IS_CHAR_DEV:
  841. value = S_IFCHR;
  842. break;
  843. case TOMOYO_TYPE_IS_FIFO:
  844. value = S_IFIFO;
  845. break;
  846. case TOMOYO_MODE_SETUID:
  847. value = S_ISUID;
  848. break;
  849. case TOMOYO_MODE_SETGID:
  850. value = S_ISGID;
  851. break;
  852. case TOMOYO_MODE_STICKY:
  853. value = S_ISVTX;
  854. break;
  855. case TOMOYO_MODE_OWNER_READ:
  856. value = S_IRUSR;
  857. break;
  858. case TOMOYO_MODE_OWNER_WRITE:
  859. value = S_IWUSR;
  860. break;
  861. case TOMOYO_MODE_OWNER_EXECUTE:
  862. value = S_IXUSR;
  863. break;
  864. case TOMOYO_MODE_GROUP_READ:
  865. value = S_IRGRP;
  866. break;
  867. case TOMOYO_MODE_GROUP_WRITE:
  868. value = S_IWGRP;
  869. break;
  870. case TOMOYO_MODE_GROUP_EXECUTE:
  871. value = S_IXGRP;
  872. break;
  873. case TOMOYO_MODE_OTHERS_READ:
  874. value = S_IROTH;
  875. break;
  876. case TOMOYO_MODE_OTHERS_WRITE:
  877. value = S_IWOTH;
  878. break;
  879. case TOMOYO_MODE_OTHERS_EXECUTE:
  880. value = S_IXOTH;
  881. break;
  882. case TOMOYO_EXEC_ARGC:
  883. if (!bprm)
  884. goto out;
  885. value = bprm->argc;
  886. break;
  887. case TOMOYO_EXEC_ENVC:
  888. if (!bprm)
  889. goto out;
  890. value = bprm->envc;
  891. break;
  892. case TOMOYO_NUMBER_UNION:
  893. /* Fetch values later. */
  894. break;
  895. default:
  896. if (!obj)
  897. goto out;
  898. if (!obj->validate_done) {
  899. tomoyo_get_attributes(obj);
  900. obj->validate_done = true;
  901. }
  902. {
  903. u8 stat_index;
  904. struct tomoyo_mini_stat *stat;
  905. switch (index) {
  906. case TOMOYO_PATH1_UID:
  907. case TOMOYO_PATH1_GID:
  908. case TOMOYO_PATH1_INO:
  909. case TOMOYO_PATH1_MAJOR:
  910. case TOMOYO_PATH1_MINOR:
  911. case TOMOYO_PATH1_TYPE:
  912. case TOMOYO_PATH1_DEV_MAJOR:
  913. case TOMOYO_PATH1_DEV_MINOR:
  914. case TOMOYO_PATH1_PERM:
  915. stat_index = TOMOYO_PATH1;
  916. break;
  917. case TOMOYO_PATH2_UID:
  918. case TOMOYO_PATH2_GID:
  919. case TOMOYO_PATH2_INO:
  920. case TOMOYO_PATH2_MAJOR:
  921. case TOMOYO_PATH2_MINOR:
  922. case TOMOYO_PATH2_TYPE:
  923. case TOMOYO_PATH2_DEV_MAJOR:
  924. case TOMOYO_PATH2_DEV_MINOR:
  925. case TOMOYO_PATH2_PERM:
  926. stat_index = TOMOYO_PATH2;
  927. break;
  928. case TOMOYO_PATH1_PARENT_UID:
  929. case TOMOYO_PATH1_PARENT_GID:
  930. case TOMOYO_PATH1_PARENT_INO:
  931. case TOMOYO_PATH1_PARENT_PERM:
  932. stat_index =
  933. TOMOYO_PATH1_PARENT;
  934. break;
  935. case TOMOYO_PATH2_PARENT_UID:
  936. case TOMOYO_PATH2_PARENT_GID:
  937. case TOMOYO_PATH2_PARENT_INO:
  938. case TOMOYO_PATH2_PARENT_PERM:
  939. stat_index =
  940. TOMOYO_PATH2_PARENT;
  941. break;
  942. default:
  943. goto out;
  944. }
  945. if (!obj->stat_valid[stat_index])
  946. goto out;
  947. stat = &obj->stat[stat_index];
  948. switch (index) {
  949. case TOMOYO_PATH1_UID:
  950. case TOMOYO_PATH2_UID:
  951. case TOMOYO_PATH1_PARENT_UID:
  952. case TOMOYO_PATH2_PARENT_UID:
  953. value = from_kuid(&init_user_ns, stat->uid);
  954. break;
  955. case TOMOYO_PATH1_GID:
  956. case TOMOYO_PATH2_GID:
  957. case TOMOYO_PATH1_PARENT_GID:
  958. case TOMOYO_PATH2_PARENT_GID:
  959. value = from_kgid(&init_user_ns, stat->gid);
  960. break;
  961. case TOMOYO_PATH1_INO:
  962. case TOMOYO_PATH2_INO:
  963. case TOMOYO_PATH1_PARENT_INO:
  964. case TOMOYO_PATH2_PARENT_INO:
  965. value = stat->ino;
  966. break;
  967. case TOMOYO_PATH1_MAJOR:
  968. case TOMOYO_PATH2_MAJOR:
  969. value = MAJOR(stat->dev);
  970. break;
  971. case TOMOYO_PATH1_MINOR:
  972. case TOMOYO_PATH2_MINOR:
  973. value = MINOR(stat->dev);
  974. break;
  975. case TOMOYO_PATH1_TYPE:
  976. case TOMOYO_PATH2_TYPE:
  977. value = stat->mode & S_IFMT;
  978. break;
  979. case TOMOYO_PATH1_DEV_MAJOR:
  980. case TOMOYO_PATH2_DEV_MAJOR:
  981. value = MAJOR(stat->rdev);
  982. break;
  983. case TOMOYO_PATH1_DEV_MINOR:
  984. case TOMOYO_PATH2_DEV_MINOR:
  985. value = MINOR(stat->rdev);
  986. break;
  987. case TOMOYO_PATH1_PERM:
  988. case TOMOYO_PATH2_PERM:
  989. case TOMOYO_PATH1_PARENT_PERM:
  990. case TOMOYO_PATH2_PARENT_PERM:
  991. value = stat->mode & S_IALLUGO;
  992. break;
  993. }
  994. }
  995. break;
  996. }
  997. max_v[j] = value;
  998. min_v[j] = value;
  999. switch (index) {
  1000. case TOMOYO_MODE_SETUID:
  1001. case TOMOYO_MODE_SETGID:
  1002. case TOMOYO_MODE_STICKY:
  1003. case TOMOYO_MODE_OWNER_READ:
  1004. case TOMOYO_MODE_OWNER_WRITE:
  1005. case TOMOYO_MODE_OWNER_EXECUTE:
  1006. case TOMOYO_MODE_GROUP_READ:
  1007. case TOMOYO_MODE_GROUP_WRITE:
  1008. case TOMOYO_MODE_GROUP_EXECUTE:
  1009. case TOMOYO_MODE_OTHERS_READ:
  1010. case TOMOYO_MODE_OTHERS_WRITE:
  1011. case TOMOYO_MODE_OTHERS_EXECUTE:
  1012. is_bitop[j] = true;
  1013. }
  1014. }
  1015. if (left == TOMOYO_NUMBER_UNION) {
  1016. /* Fetch values now. */
  1017. const struct tomoyo_number_union *ptr = numbers_p++;
  1018. min_v[0] = ptr->values[0];
  1019. max_v[0] = ptr->values[1];
  1020. }
  1021. if (right == TOMOYO_NUMBER_UNION) {
  1022. /* Fetch values now. */
  1023. const struct tomoyo_number_union *ptr = numbers_p++;
  1024. if (ptr->group) {
  1025. if (tomoyo_number_matches_group(min_v[0],
  1026. max_v[0],
  1027. ptr->group)
  1028. == match)
  1029. continue;
  1030. } else {
  1031. if ((min_v[0] <= ptr->values[1] &&
  1032. max_v[0] >= ptr->values[0]) == match)
  1033. continue;
  1034. }
  1035. goto out;
  1036. }
  1037. /*
  1038. * Bit operation is valid only when counterpart value
  1039. * represents permission.
  1040. */
  1041. if (is_bitop[0] && is_bitop[1]) {
  1042. goto out;
  1043. } else if (is_bitop[0]) {
  1044. switch (right) {
  1045. case TOMOYO_PATH1_PERM:
  1046. case TOMOYO_PATH1_PARENT_PERM:
  1047. case TOMOYO_PATH2_PERM:
  1048. case TOMOYO_PATH2_PARENT_PERM:
  1049. if (!(max_v[0] & max_v[1]) == !match)
  1050. continue;
  1051. }
  1052. goto out;
  1053. } else if (is_bitop[1]) {
  1054. switch (left) {
  1055. case TOMOYO_PATH1_PERM:
  1056. case TOMOYO_PATH1_PARENT_PERM:
  1057. case TOMOYO_PATH2_PERM:
  1058. case TOMOYO_PATH2_PARENT_PERM:
  1059. if (!(max_v[0] & max_v[1]) == !match)
  1060. continue;
  1061. }
  1062. goto out;
  1063. }
  1064. /* Normal value range comparison. */
  1065. if ((min_v[0] <= max_v[1] && max_v[0] >= min_v[1]) == match)
  1066. continue;
  1067. out:
  1068. return false;
  1069. }
  1070. /* Check argv[] and envp[] now. */
  1071. if (r->ee && (argc || envc))
  1072. return tomoyo_scan_bprm(r->ee, argc, argv, envc, envp);
  1073. return true;
  1074. }