policy_ns.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor policy manipulation functions
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2017 Canonical Ltd.
  9. *
  10. * AppArmor policy namespaces, allow for different sets of policies
  11. * to be loaded for tasks within the namespace.
  12. */
  13. #include <linux/list.h>
  14. #include <linux/mutex.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include "include/apparmor.h"
  18. #include "include/cred.h"
  19. #include "include/policy_ns.h"
  20. #include "include/label.h"
  21. #include "include/policy.h"
  22. /* kernel label */
  23. struct aa_label *kernel_t;
  24. /* root profile namespace */
  25. struct aa_ns *root_ns;
  26. const char *aa_hidden_ns_name = "---";
  27. /**
  28. * aa_ns_visible - test if @view is visible from @curr
  29. * @curr: namespace to treat as the parent (NOT NULL)
  30. * @view: namespace to test if visible from @curr (NOT NULL)
  31. * @subns: whether view of a subns is allowed
  32. *
  33. * Returns: true if @view is visible from @curr else false
  34. */
  35. bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns)
  36. {
  37. if (curr == view)
  38. return true;
  39. if (!subns)
  40. return false;
  41. for ( ; view; view = view->parent) {
  42. if (view->parent == curr)
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * aa_ns_name - Find the ns name to display for @view from @curr
  49. * @curr: current namespace (NOT NULL)
  50. * @view: namespace attempting to view (NOT NULL)
  51. * @subns: are subns visible
  52. *
  53. * Returns: name of @view visible from @curr
  54. */
  55. const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns)
  56. {
  57. /* if view == curr then the namespace name isn't displayed */
  58. if (curr == view)
  59. return "";
  60. if (aa_ns_visible(curr, view, subns)) {
  61. /* at this point if a ns is visible it is in a view ns
  62. * thus the curr ns.hname is a prefix of its name.
  63. * Only output the virtualized portion of the name
  64. * Add + 2 to skip over // separating curr hname prefix
  65. * from the visible tail of the views hname
  66. */
  67. return view->base.hname + strlen(curr->base.hname) + 2;
  68. }
  69. return aa_hidden_ns_name;
  70. }
  71. static struct aa_profile *alloc_unconfined(const char *name)
  72. {
  73. struct aa_profile *profile;
  74. profile = aa_alloc_null(NULL, name, GFP_KERNEL);
  75. if (!profile)
  76. return NULL;
  77. profile->label.flags |= FLAG_IX_ON_NAME_ERROR |
  78. FLAG_IMMUTIBLE | FLAG_NS_COUNT | FLAG_UNCONFINED;
  79. profile->mode = APPARMOR_UNCONFINED;
  80. return profile;
  81. }
  82. /**
  83. * alloc_ns - allocate, initialize and return a new namespace
  84. * @prefix: parent namespace name (MAYBE NULL)
  85. * @name: a preallocated name (NOT NULL)
  86. *
  87. * Returns: refcounted namespace or NULL on failure.
  88. */
  89. static struct aa_ns *alloc_ns(const char *prefix, const char *name)
  90. {
  91. struct aa_ns *ns;
  92. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  93. AA_DEBUG("%s(%p)\n", __func__, ns);
  94. if (!ns)
  95. return NULL;
  96. if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL))
  97. goto fail_ns;
  98. INIT_LIST_HEAD(&ns->sub_ns);
  99. INIT_LIST_HEAD(&ns->rawdata_list);
  100. mutex_init(&ns->lock);
  101. init_waitqueue_head(&ns->wait);
  102. /* released by aa_free_ns() */
  103. ns->unconfined = alloc_unconfined("unconfined");
  104. if (!ns->unconfined)
  105. goto fail_unconfined;
  106. /* ns and ns->unconfined share ns->unconfined refcount */
  107. ns->unconfined->ns = ns;
  108. atomic_set(&ns->uniq_null, 0);
  109. aa_labelset_init(&ns->labels);
  110. return ns;
  111. fail_unconfined:
  112. aa_policy_destroy(&ns->base);
  113. fail_ns:
  114. kfree_sensitive(ns);
  115. return NULL;
  116. }
  117. /**
  118. * aa_free_ns - free a profile namespace
  119. * @ns: the namespace to free (MAYBE NULL)
  120. *
  121. * Requires: All references to the namespace must have been put, if the
  122. * namespace was referenced by a profile confining a task,
  123. */
  124. void aa_free_ns(struct aa_ns *ns)
  125. {
  126. if (!ns)
  127. return;
  128. aa_policy_destroy(&ns->base);
  129. aa_labelset_destroy(&ns->labels);
  130. aa_put_ns(ns->parent);
  131. ns->unconfined->ns = NULL;
  132. aa_free_profile(ns->unconfined);
  133. kfree_sensitive(ns);
  134. }
  135. /**
  136. * __aa_lookupn_ns - lookup the namespace matching @hname
  137. * @view: namespace to search in (NOT NULL)
  138. * @hname: hierarchical ns name (NOT NULL)
  139. * @n: length of @hname
  140. *
  141. * Requires: rcu_read_lock be held
  142. *
  143. * Returns: unrefcounted ns pointer or NULL if not found
  144. *
  145. * Do a relative name lookup, recursing through profile tree.
  146. */
  147. struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, const char *hname, size_t n)
  148. {
  149. struct aa_ns *ns = view;
  150. const char *split;
  151. for (split = strnstr(hname, "//", n); split;
  152. split = strnstr(hname, "//", n)) {
  153. ns = __aa_findn_ns(&ns->sub_ns, hname, split - hname);
  154. if (!ns)
  155. return NULL;
  156. n -= split + 2 - hname;
  157. hname = split + 2;
  158. }
  159. if (n)
  160. return __aa_findn_ns(&ns->sub_ns, hname, n);
  161. return NULL;
  162. }
  163. /**
  164. * aa_lookupn_ns - look up a policy namespace relative to @view
  165. * @view: namespace to search in (NOT NULL)
  166. * @name: name of namespace to find (NOT NULL)
  167. * @n: length of @name
  168. *
  169. * Returns: a refcounted namespace on the list, or NULL if no namespace
  170. * called @name exists.
  171. *
  172. * refcount released by caller
  173. */
  174. struct aa_ns *aa_lookupn_ns(struct aa_ns *view, const char *name, size_t n)
  175. {
  176. struct aa_ns *ns = NULL;
  177. rcu_read_lock();
  178. ns = aa_get_ns(__aa_lookupn_ns(view, name, n));
  179. rcu_read_unlock();
  180. return ns;
  181. }
  182. static struct aa_ns *__aa_create_ns(struct aa_ns *parent, const char *name,
  183. struct dentry *dir)
  184. {
  185. struct aa_ns *ns;
  186. int error;
  187. AA_BUG(!parent);
  188. AA_BUG(!name);
  189. AA_BUG(!mutex_is_locked(&parent->lock));
  190. ns = alloc_ns(parent->base.hname, name);
  191. if (!ns)
  192. return ERR_PTR(-ENOMEM);
  193. ns->level = parent->level + 1;
  194. mutex_lock_nested(&ns->lock, ns->level);
  195. error = __aafs_ns_mkdir(ns, ns_subns_dir(parent), name, dir);
  196. if (error) {
  197. AA_ERROR("Failed to create interface for ns %s\n",
  198. ns->base.name);
  199. mutex_unlock(&ns->lock);
  200. aa_free_ns(ns);
  201. return ERR_PTR(error);
  202. }
  203. ns->parent = aa_get_ns(parent);
  204. list_add_rcu(&ns->base.list, &parent->sub_ns);
  205. /* add list ref */
  206. aa_get_ns(ns);
  207. mutex_unlock(&ns->lock);
  208. return ns;
  209. }
  210. /**
  211. * __aa_find_or_create_ns - create an ns, fail if it already exists
  212. * @parent: the parent of the namespace being created
  213. * @name: the name of the namespace
  214. * @dir: if not null the dir to put the ns entries in
  215. *
  216. * Returns: the a refcounted ns that has been add or an ERR_PTR
  217. */
  218. struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name,
  219. struct dentry *dir)
  220. {
  221. struct aa_ns *ns;
  222. AA_BUG(!mutex_is_locked(&parent->lock));
  223. /* try and find the specified ns */
  224. /* released by caller */
  225. ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
  226. if (!ns)
  227. ns = __aa_create_ns(parent, name, dir);
  228. else
  229. ns = ERR_PTR(-EEXIST);
  230. /* return ref */
  231. return ns;
  232. }
  233. /**
  234. * aa_prepare_ns - find an existing or create a new namespace of @name
  235. * @parent: ns to treat as parent
  236. * @name: the namespace to find or add (NOT NULL)
  237. *
  238. * Returns: refcounted namespace or PTR_ERR if failed to create one
  239. */
  240. struct aa_ns *aa_prepare_ns(struct aa_ns *parent, const char *name)
  241. {
  242. struct aa_ns *ns;
  243. mutex_lock_nested(&parent->lock, parent->level);
  244. /* try and find the specified ns and if it doesn't exist create it */
  245. /* released by caller */
  246. ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
  247. if (!ns)
  248. ns = __aa_create_ns(parent, name, NULL);
  249. mutex_unlock(&parent->lock);
  250. /* return ref */
  251. return ns;
  252. }
  253. static void __ns_list_release(struct list_head *head);
  254. /**
  255. * destroy_ns - remove everything contained by @ns
  256. * @ns: namespace to have it contents removed (NOT NULL)
  257. */
  258. static void destroy_ns(struct aa_ns *ns)
  259. {
  260. if (!ns)
  261. return;
  262. mutex_lock_nested(&ns->lock, ns->level);
  263. /* release all profiles in this namespace */
  264. __aa_profile_list_release(&ns->base.profiles);
  265. /* release all sub namespaces */
  266. __ns_list_release(&ns->sub_ns);
  267. if (ns->parent) {
  268. unsigned long flags;
  269. write_lock_irqsave(&ns->labels.lock, flags);
  270. __aa_proxy_redirect(ns_unconfined(ns),
  271. ns_unconfined(ns->parent));
  272. write_unlock_irqrestore(&ns->labels.lock, flags);
  273. }
  274. __aafs_ns_rmdir(ns);
  275. mutex_unlock(&ns->lock);
  276. }
  277. /**
  278. * __aa_remove_ns - remove a namespace and all its children
  279. * @ns: namespace to be removed (NOT NULL)
  280. *
  281. * Requires: ns->parent->lock be held and ns removed from parent.
  282. */
  283. void __aa_remove_ns(struct aa_ns *ns)
  284. {
  285. /* remove ns from namespace list */
  286. list_del_rcu(&ns->base.list);
  287. destroy_ns(ns);
  288. aa_put_ns(ns);
  289. }
  290. /**
  291. * __ns_list_release - remove all profile namespaces on the list put refs
  292. * @head: list of profile namespaces (NOT NULL)
  293. *
  294. * Requires: namespace lock be held
  295. */
  296. static void __ns_list_release(struct list_head *head)
  297. {
  298. struct aa_ns *ns, *tmp;
  299. list_for_each_entry_safe(ns, tmp, head, base.list)
  300. __aa_remove_ns(ns);
  301. }
  302. /**
  303. * aa_alloc_root_ns - allocate the root profile namespace
  304. *
  305. * Returns: %0 on success else error
  306. *
  307. */
  308. int __init aa_alloc_root_ns(void)
  309. {
  310. struct aa_profile *kernel_p;
  311. /* released by aa_free_root_ns - used as list ref*/
  312. root_ns = alloc_ns(NULL, "root");
  313. if (!root_ns)
  314. return -ENOMEM;
  315. kernel_p = alloc_unconfined("kernel_t");
  316. if (!kernel_p) {
  317. destroy_ns(root_ns);
  318. aa_free_ns(root_ns);
  319. return -ENOMEM;
  320. }
  321. kernel_t = &kernel_p->label;
  322. root_ns->unconfined->ns = aa_get_ns(root_ns);
  323. return 0;
  324. }
  325. /**
  326. * aa_free_root_ns - free the root profile namespace
  327. */
  328. void __init aa_free_root_ns(void)
  329. {
  330. struct aa_ns *ns = root_ns;
  331. root_ns = NULL;
  332. aa_label_free(kernel_t);
  333. destroy_ns(ns);
  334. aa_put_ns(ns);
  335. }