domain.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * security/tomoyo/domain.c
  3. *
  4. * Domain transition functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/binfmts.h>
  10. #include <linux/slab.h>
  11. /* Variables definitions.*/
  12. /* The initial domain. */
  13. struct tomoyo_domain_info tomoyo_kernel_domain;
  14. /**
  15. * tomoyo_update_policy - Update an entry for exception policy.
  16. *
  17. * @new_entry: Pointer to "struct tomoyo_acl_info".
  18. * @size: Size of @new_entry in bytes.
  19. * @is_delete: True if it is a delete request.
  20. * @list: Pointer to "struct list_head".
  21. * @check_duplicate: Callback function to find duplicated entry.
  22. *
  23. * Returns 0 on success, negative value otherwise.
  24. *
  25. * Caller holds tomoyo_read_lock().
  26. */
  27. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  28. bool is_delete, struct list_head *list,
  29. bool (*check_duplicate) (const struct tomoyo_acl_head
  30. *,
  31. const struct tomoyo_acl_head
  32. *))
  33. {
  34. int error = is_delete ? -ENOENT : -ENOMEM;
  35. struct tomoyo_acl_head *entry;
  36. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  37. return -ENOMEM;
  38. list_for_each_entry_rcu(entry, list, list) {
  39. if (!check_duplicate(entry, new_entry))
  40. continue;
  41. entry->is_deleted = is_delete;
  42. error = 0;
  43. break;
  44. }
  45. if (error && !is_delete) {
  46. entry = tomoyo_commit_ok(new_entry, size);
  47. if (entry) {
  48. list_add_tail_rcu(&entry->list, list);
  49. error = 0;
  50. }
  51. }
  52. mutex_unlock(&tomoyo_policy_lock);
  53. return error;
  54. }
  55. /**
  56. * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
  57. *
  58. * @a: Pointer to "struct tomoyo_acl_info".
  59. * @b: Pointer to "struct tomoyo_acl_info".
  60. *
  61. * Returns true if @a == @b, false otherwise.
  62. */
  63. static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
  64. const struct tomoyo_acl_info *b)
  65. {
  66. return a->type == b->type;
  67. }
  68. /**
  69. * tomoyo_update_domain - Update an entry for domain policy.
  70. *
  71. * @new_entry: Pointer to "struct tomoyo_acl_info".
  72. * @size: Size of @new_entry in bytes.
  73. * @is_delete: True if it is a delete request.
  74. * @domain: Pointer to "struct tomoyo_domain_info".
  75. * @check_duplicate: Callback function to find duplicated entry.
  76. * @merge_duplicate: Callback function to merge duplicated entry.
  77. *
  78. * Returns 0 on success, negative value otherwise.
  79. *
  80. * Caller holds tomoyo_read_lock().
  81. */
  82. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  83. bool is_delete, struct tomoyo_domain_info *domain,
  84. bool (*check_duplicate) (const struct tomoyo_acl_info
  85. *,
  86. const struct tomoyo_acl_info
  87. *),
  88. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  89. struct tomoyo_acl_info *,
  90. const bool))
  91. {
  92. int error = is_delete ? -ENOENT : -ENOMEM;
  93. struct tomoyo_acl_info *entry;
  94. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  95. return error;
  96. list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
  97. if (!tomoyo_same_acl_head(entry, new_entry) ||
  98. !check_duplicate(entry, new_entry))
  99. continue;
  100. if (merge_duplicate)
  101. entry->is_deleted = merge_duplicate(entry, new_entry,
  102. is_delete);
  103. else
  104. entry->is_deleted = is_delete;
  105. error = 0;
  106. break;
  107. }
  108. if (error && !is_delete) {
  109. entry = tomoyo_commit_ok(new_entry, size);
  110. if (entry) {
  111. list_add_tail_rcu(&entry->list, &domain->acl_info_list);
  112. error = 0;
  113. }
  114. }
  115. mutex_unlock(&tomoyo_policy_lock);
  116. return error;
  117. }
  118. void tomoyo_check_acl(struct tomoyo_request_info *r,
  119. bool (*check_entry) (struct tomoyo_request_info *,
  120. const struct tomoyo_acl_info *))
  121. {
  122. const struct tomoyo_domain_info *domain = r->domain;
  123. struct tomoyo_acl_info *ptr;
  124. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  125. if (ptr->is_deleted || ptr->type != r->param_type)
  126. continue;
  127. if (check_entry(r, ptr)) {
  128. r->granted = true;
  129. return;
  130. }
  131. }
  132. r->granted = false;
  133. }
  134. /* The list for "struct tomoyo_domain_info". */
  135. LIST_HEAD(tomoyo_domain_list);
  136. struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
  137. struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
  138. /**
  139. * tomoyo_last_word - Get last component of a domainname.
  140. *
  141. * @domainname: Domainname to check.
  142. *
  143. * Returns the last word of @domainname.
  144. */
  145. static const char *tomoyo_last_word(const char *name)
  146. {
  147. const char *cp = strrchr(name, ' ');
  148. if (cp)
  149. return cp + 1;
  150. return name;
  151. }
  152. static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
  153. const struct tomoyo_acl_head *b)
  154. {
  155. const struct tomoyo_transition_control *p1 = container_of(a,
  156. typeof(*p1),
  157. head);
  158. const struct tomoyo_transition_control *p2 = container_of(b,
  159. typeof(*p2),
  160. head);
  161. return p1->type == p2->type && p1->is_last_name == p2->is_last_name
  162. && p1->domainname == p2->domainname
  163. && p1->program == p2->program;
  164. }
  165. /**
  166. * tomoyo_update_transition_control_entry - Update "struct tomoyo_transition_control" list.
  167. *
  168. * @domainname: The name of domain. Maybe NULL.
  169. * @program: The name of program. Maybe NULL.
  170. * @type: Type of transition.
  171. * @is_delete: True if it is a delete request.
  172. *
  173. * Returns 0 on success, negative value otherwise.
  174. */
  175. static int tomoyo_update_transition_control_entry(const char *domainname,
  176. const char *program,
  177. const u8 type,
  178. const bool is_delete)
  179. {
  180. struct tomoyo_transition_control e = { .type = type };
  181. int error = is_delete ? -ENOENT : -ENOMEM;
  182. if (program) {
  183. if (!tomoyo_correct_path(program))
  184. return -EINVAL;
  185. e.program = tomoyo_get_name(program);
  186. if (!e.program)
  187. goto out;
  188. }
  189. if (domainname) {
  190. if (!tomoyo_correct_domain(domainname)) {
  191. if (!tomoyo_correct_path(domainname))
  192. goto out;
  193. e.is_last_name = true;
  194. }
  195. e.domainname = tomoyo_get_name(domainname);
  196. if (!e.domainname)
  197. goto out;
  198. }
  199. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  200. &tomoyo_policy_list
  201. [TOMOYO_ID_TRANSITION_CONTROL],
  202. tomoyo_same_transition_control);
  203. out:
  204. tomoyo_put_name(e.domainname);
  205. tomoyo_put_name(e.program);
  206. return error;
  207. }
  208. /**
  209. * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
  210. *
  211. * @data: String to parse.
  212. * @is_delete: True if it is a delete request.
  213. * @type: Type of this entry.
  214. *
  215. * Returns 0 on success, negative value otherwise.
  216. */
  217. int tomoyo_write_transition_control(char *data, const bool is_delete,
  218. const u8 type)
  219. {
  220. char *domainname = strstr(data, " from ");
  221. if (domainname) {
  222. *domainname = '\0';
  223. domainname += 6;
  224. } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
  225. type == TOMOYO_TRANSITION_CONTROL_KEEP) {
  226. domainname = data;
  227. data = NULL;
  228. }
  229. return tomoyo_update_transition_control_entry(domainname, data, type,
  230. is_delete);
  231. }
  232. /**
  233. * tomoyo_transition_type - Get domain transition type.
  234. *
  235. * @domainname: The name of domain.
  236. * @program: The name of program.
  237. *
  238. * Returns TOMOYO_TRANSITION_CONTROL_INITIALIZE if executing @program
  239. * reinitializes domain transition, TOMOYO_TRANSITION_CONTROL_KEEP if executing
  240. * @program suppresses domain transition, others otherwise.
  241. *
  242. * Caller holds tomoyo_read_lock().
  243. */
  244. static u8 tomoyo_transition_type(const struct tomoyo_path_info *domainname,
  245. const struct tomoyo_path_info *program)
  246. {
  247. const struct tomoyo_transition_control *ptr;
  248. const char *last_name = tomoyo_last_word(domainname->name);
  249. u8 type;
  250. for (type = 0; type < TOMOYO_MAX_TRANSITION_TYPE; type++) {
  251. next:
  252. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  253. [TOMOYO_ID_TRANSITION_CONTROL],
  254. head.list) {
  255. if (ptr->head.is_deleted || ptr->type != type)
  256. continue;
  257. if (ptr->domainname) {
  258. if (!ptr->is_last_name) {
  259. if (ptr->domainname != domainname)
  260. continue;
  261. } else {
  262. /*
  263. * Use direct strcmp() since this is
  264. * unlikely used.
  265. */
  266. if (strcmp(ptr->domainname->name,
  267. last_name))
  268. continue;
  269. }
  270. }
  271. if (ptr->program &&
  272. tomoyo_pathcmp(ptr->program, program))
  273. continue;
  274. if (type == TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE) {
  275. /*
  276. * Do not check for initialize_domain if
  277. * no_initialize_domain matched.
  278. */
  279. type = TOMOYO_TRANSITION_CONTROL_NO_KEEP;
  280. goto next;
  281. }
  282. goto done;
  283. }
  284. }
  285. done:
  286. return type;
  287. }
  288. static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
  289. const struct tomoyo_acl_head *b)
  290. {
  291. const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1), head);
  292. const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2), head);
  293. return p1->original_name == p2->original_name &&
  294. p1->aggregated_name == p2->aggregated_name;
  295. }
  296. /**
  297. * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator" list.
  298. *
  299. * @original_name: The original program's name.
  300. * @aggregated_name: The program name to use.
  301. * @is_delete: True if it is a delete request.
  302. *
  303. * Returns 0 on success, negative value otherwise.
  304. *
  305. * Caller holds tomoyo_read_lock().
  306. */
  307. static int tomoyo_update_aggregator_entry(const char *original_name,
  308. const char *aggregated_name,
  309. const bool is_delete)
  310. {
  311. struct tomoyo_aggregator e = { };
  312. int error = is_delete ? -ENOENT : -ENOMEM;
  313. if (!tomoyo_correct_path(original_name) ||
  314. !tomoyo_correct_path(aggregated_name))
  315. return -EINVAL;
  316. e.original_name = tomoyo_get_name(original_name);
  317. e.aggregated_name = tomoyo_get_name(aggregated_name);
  318. if (!e.original_name || !e.aggregated_name ||
  319. e.aggregated_name->is_patterned) /* No patterns allowed. */
  320. goto out;
  321. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  322. &tomoyo_policy_list[TOMOYO_ID_AGGREGATOR],
  323. tomoyo_same_aggregator);
  324. out:
  325. tomoyo_put_name(e.original_name);
  326. tomoyo_put_name(e.aggregated_name);
  327. return error;
  328. }
  329. /**
  330. * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
  331. *
  332. * @data: String to parse.
  333. * @is_delete: True if it is a delete request.
  334. *
  335. * Returns 0 on success, negative value otherwise.
  336. *
  337. * Caller holds tomoyo_read_lock().
  338. */
  339. int tomoyo_write_aggregator(char *data, const bool is_delete)
  340. {
  341. char *cp = strchr(data, ' ');
  342. if (!cp)
  343. return -EINVAL;
  344. *cp++ = '\0';
  345. return tomoyo_update_aggregator_entry(data, cp, is_delete);
  346. }
  347. /**
  348. * tomoyo_assign_domain - Create a domain.
  349. *
  350. * @domainname: The name of domain.
  351. * @profile: Profile number to assign if the domain was newly created.
  352. *
  353. * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
  354. *
  355. * Caller holds tomoyo_read_lock().
  356. */
  357. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  358. const u8 profile)
  359. {
  360. struct tomoyo_domain_info *entry;
  361. struct tomoyo_domain_info *domain = NULL;
  362. const struct tomoyo_path_info *saved_domainname;
  363. bool found = false;
  364. if (!tomoyo_correct_domain(domainname))
  365. return NULL;
  366. saved_domainname = tomoyo_get_name(domainname);
  367. if (!saved_domainname)
  368. return NULL;
  369. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  370. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  371. goto out;
  372. list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
  373. if (domain->is_deleted ||
  374. tomoyo_pathcmp(saved_domainname, domain->domainname))
  375. continue;
  376. found = true;
  377. break;
  378. }
  379. if (!found && tomoyo_memory_ok(entry)) {
  380. INIT_LIST_HEAD(&entry->acl_info_list);
  381. entry->domainname = saved_domainname;
  382. saved_domainname = NULL;
  383. entry->profile = profile;
  384. list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
  385. domain = entry;
  386. entry = NULL;
  387. found = true;
  388. }
  389. mutex_unlock(&tomoyo_policy_lock);
  390. out:
  391. tomoyo_put_name(saved_domainname);
  392. kfree(entry);
  393. return found ? domain : NULL;
  394. }
  395. /**
  396. * tomoyo_find_next_domain - Find a domain.
  397. *
  398. * @bprm: Pointer to "struct linux_binprm".
  399. *
  400. * Returns 0 on success, negative value otherwise.
  401. *
  402. * Caller holds tomoyo_read_lock().
  403. */
  404. int tomoyo_find_next_domain(struct linux_binprm *bprm)
  405. {
  406. struct tomoyo_request_info r;
  407. char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
  408. struct tomoyo_domain_info *old_domain = tomoyo_domain();
  409. struct tomoyo_domain_info *domain = NULL;
  410. const char *original_name = bprm->filename;
  411. u8 mode;
  412. bool is_enforce;
  413. int retval = -ENOMEM;
  414. bool need_kfree = false;
  415. struct tomoyo_path_info rn = { }; /* real name */
  416. mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
  417. is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
  418. if (!tmp)
  419. goto out;
  420. retry:
  421. if (need_kfree) {
  422. kfree(rn.name);
  423. need_kfree = false;
  424. }
  425. /* Get symlink's pathname of program. */
  426. retval = -ENOENT;
  427. rn.name = tomoyo_realpath_nofollow(original_name);
  428. if (!rn.name)
  429. goto out;
  430. tomoyo_fill_path_info(&rn);
  431. need_kfree = true;
  432. /* Check 'aggregator' directive. */
  433. {
  434. struct tomoyo_aggregator *ptr;
  435. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  436. [TOMOYO_ID_AGGREGATOR], head.list) {
  437. if (ptr->head.is_deleted ||
  438. !tomoyo_path_matches_pattern(&rn,
  439. ptr->original_name))
  440. continue;
  441. kfree(rn.name);
  442. need_kfree = false;
  443. /* This is OK because it is read only. */
  444. rn = *ptr->aggregated_name;
  445. break;
  446. }
  447. }
  448. /* Check execute permission. */
  449. retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
  450. if (retval == TOMOYO_RETRY_REQUEST)
  451. goto retry;
  452. if (retval < 0)
  453. goto out;
  454. /*
  455. * To be able to specify domainnames with wildcards, use the
  456. * pathname specified in the policy (which may contain
  457. * wildcard) rather than the pathname passed to execve()
  458. * (which never contains wildcard).
  459. */
  460. if (r.param.path.matched_path) {
  461. if (need_kfree)
  462. kfree(rn.name);
  463. need_kfree = false;
  464. /* This is OK because it is read only. */
  465. rn = *r.param.path.matched_path;
  466. }
  467. /* Calculate domain to transit to. */
  468. switch (tomoyo_transition_type(old_domain->domainname, &rn)) {
  469. case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
  470. /* Transit to the child of tomoyo_kernel_domain domain. */
  471. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, TOMOYO_ROOT_NAME " "
  472. "%s", rn.name);
  473. break;
  474. case TOMOYO_TRANSITION_CONTROL_KEEP:
  475. /* Keep current domain. */
  476. domain = old_domain;
  477. break;
  478. default:
  479. if (old_domain == &tomoyo_kernel_domain &&
  480. !tomoyo_policy_loaded) {
  481. /*
  482. * Needn't to transit from kernel domain before
  483. * starting /sbin/init. But transit from kernel domain
  484. * if executing initializers because they might start
  485. * before /sbin/init.
  486. */
  487. domain = old_domain;
  488. } else {
  489. /* Normal domain transition. */
  490. snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
  491. old_domain->domainname->name, rn.name);
  492. }
  493. break;
  494. }
  495. if (domain || strlen(tmp) >= TOMOYO_EXEC_TMPSIZE - 10)
  496. goto done;
  497. domain = tomoyo_find_domain(tmp);
  498. if (!domain)
  499. domain = tomoyo_assign_domain(tmp, old_domain->profile);
  500. done:
  501. if (domain)
  502. goto out;
  503. printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", tmp);
  504. if (is_enforce)
  505. retval = -EPERM;
  506. else
  507. old_domain->transition_failed = true;
  508. out:
  509. if (!domain)
  510. domain = old_domain;
  511. /* Update reference count on "struct tomoyo_domain_info". */
  512. atomic_inc(&domain->users);
  513. bprm->cred->security = domain;
  514. if (need_kfree)
  515. kfree(rn.name);
  516. kfree(tmp);
  517. return retval;
  518. }