policy_ns.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_profile(name, NULL, 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. profile->file.dfa = aa_get_dfa(nulldfa);
  81. profile->policy.dfa = aa_get_dfa(nulldfa);
  82. return profile;
  83. }
  84. /**
  85. * alloc_ns - allocate, initialize and return a new namespace
  86. * @prefix: parent namespace name (MAYBE NULL)
  87. * @name: a preallocated name (NOT NULL)
  88. *
  89. * Returns: refcounted namespace or NULL on failure.
  90. */
  91. static struct aa_ns *alloc_ns(const char *prefix, const char *name)
  92. {
  93. struct aa_ns *ns;
  94. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  95. AA_DEBUG("%s(%p)\n", __func__, ns);
  96. if (!ns)
  97. return NULL;
  98. if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL))
  99. goto fail_ns;
  100. INIT_LIST_HEAD(&ns->sub_ns);
  101. INIT_LIST_HEAD(&ns->rawdata_list);
  102. mutex_init(&ns->lock);
  103. init_waitqueue_head(&ns->wait);
  104. /* released by aa_free_ns() */
  105. ns->unconfined = alloc_unconfined("unconfined");
  106. if (!ns->unconfined)
  107. goto fail_unconfined;
  108. /* ns and ns->unconfined share ns->unconfined refcount */
  109. ns->unconfined->ns = ns;
  110. atomic_set(&ns->uniq_null, 0);
  111. aa_labelset_init(&ns->labels);
  112. return ns;
  113. fail_unconfined:
  114. aa_policy_destroy(&ns->base);
  115. fail_ns:
  116. kfree_sensitive(ns);
  117. return NULL;
  118. }
  119. /**
  120. * aa_free_ns - free a profile namespace
  121. * @ns: the namespace to free (MAYBE NULL)
  122. *
  123. * Requires: All references to the namespace must have been put, if the
  124. * namespace was referenced by a profile confining a task,
  125. */
  126. void aa_free_ns(struct aa_ns *ns)
  127. {
  128. if (!ns)
  129. return;
  130. aa_policy_destroy(&ns->base);
  131. aa_labelset_destroy(&ns->labels);
  132. aa_put_ns(ns->parent);
  133. ns->unconfined->ns = NULL;
  134. aa_free_profile(ns->unconfined);
  135. kfree_sensitive(ns);
  136. }
  137. /**
  138. * aa_findn_ns - look up a profile namespace on the namespace list
  139. * @root: namespace to search in (NOT NULL)
  140. * @name: name of namespace to find (NOT NULL)
  141. * @n: length of @name
  142. *
  143. * Returns: a refcounted namespace on the list, or NULL if no namespace
  144. * called @name exists.
  145. *
  146. * refcount released by caller
  147. */
  148. struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n)
  149. {
  150. struct aa_ns *ns = NULL;
  151. rcu_read_lock();
  152. ns = aa_get_ns(__aa_findn_ns(&root->sub_ns, name, n));
  153. rcu_read_unlock();
  154. return ns;
  155. }
  156. /**
  157. * aa_find_ns - look up a profile namespace on the namespace list
  158. * @root: namespace to search in (NOT NULL)
  159. * @name: name of namespace to find (NOT NULL)
  160. *
  161. * Returns: a refcounted namespace on the list, or NULL if no namespace
  162. * called @name exists.
  163. *
  164. * refcount released by caller
  165. */
  166. struct aa_ns *aa_find_ns(struct aa_ns *root, const char *name)
  167. {
  168. return aa_findn_ns(root, name, strlen(name));
  169. }
  170. /**
  171. * __aa_lookupn_ns - lookup the namespace matching @hname
  172. * @view: namespace to search in (NOT NULL)
  173. * @hname: hierarchical ns name (NOT NULL)
  174. * @n: length of @hname
  175. *
  176. * Requires: rcu_read_lock be held
  177. *
  178. * Returns: unrefcounted ns pointer or NULL if not found
  179. *
  180. * Do a relative name lookup, recursing through profile tree.
  181. */
  182. struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, const char *hname, size_t n)
  183. {
  184. struct aa_ns *ns = view;
  185. const char *split;
  186. for (split = strnstr(hname, "//", n); split;
  187. split = strnstr(hname, "//", n)) {
  188. ns = __aa_findn_ns(&ns->sub_ns, hname, split - hname);
  189. if (!ns)
  190. return NULL;
  191. n -= split + 2 - hname;
  192. hname = split + 2;
  193. }
  194. if (n)
  195. return __aa_findn_ns(&ns->sub_ns, hname, n);
  196. return NULL;
  197. }
  198. /**
  199. * aa_lookupn_ns - look up a policy namespace relative to @view
  200. * @view: namespace to search in (NOT NULL)
  201. * @name: name of namespace to find (NOT NULL)
  202. * @n: length of @name
  203. *
  204. * Returns: a refcounted namespace on the list, or NULL if no namespace
  205. * called @name exists.
  206. *
  207. * refcount released by caller
  208. */
  209. struct aa_ns *aa_lookupn_ns(struct aa_ns *view, const char *name, size_t n)
  210. {
  211. struct aa_ns *ns = NULL;
  212. rcu_read_lock();
  213. ns = aa_get_ns(__aa_lookupn_ns(view, name, n));
  214. rcu_read_unlock();
  215. return ns;
  216. }
  217. static struct aa_ns *__aa_create_ns(struct aa_ns *parent, const char *name,
  218. struct dentry *dir)
  219. {
  220. struct aa_ns *ns;
  221. int error;
  222. AA_BUG(!parent);
  223. AA_BUG(!name);
  224. AA_BUG(!mutex_is_locked(&parent->lock));
  225. ns = alloc_ns(parent->base.hname, name);
  226. if (!ns)
  227. return ERR_PTR(-ENOMEM);
  228. ns->level = parent->level + 1;
  229. mutex_lock_nested(&ns->lock, ns->level);
  230. error = __aafs_ns_mkdir(ns, ns_subns_dir(parent), name, dir);
  231. if (error) {
  232. AA_ERROR("Failed to create interface for ns %s\n",
  233. ns->base.name);
  234. mutex_unlock(&ns->lock);
  235. aa_free_ns(ns);
  236. return ERR_PTR(error);
  237. }
  238. ns->parent = aa_get_ns(parent);
  239. list_add_rcu(&ns->base.list, &parent->sub_ns);
  240. /* add list ref */
  241. aa_get_ns(ns);
  242. mutex_unlock(&ns->lock);
  243. return ns;
  244. }
  245. /**
  246. * __aa_find_or_create_ns - create an ns, fail if it already exists
  247. * @parent: the parent of the namespace being created
  248. * @name: the name of the namespace
  249. * @dir: if not null the dir to put the ns entries in
  250. *
  251. * Returns: the a refcounted ns that has been add or an ERR_PTR
  252. */
  253. struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name,
  254. struct dentry *dir)
  255. {
  256. struct aa_ns *ns;
  257. AA_BUG(!mutex_is_locked(&parent->lock));
  258. /* try and find the specified ns */
  259. /* released by caller */
  260. ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
  261. if (!ns)
  262. ns = __aa_create_ns(parent, name, dir);
  263. else
  264. ns = ERR_PTR(-EEXIST);
  265. /* return ref */
  266. return ns;
  267. }
  268. /**
  269. * aa_prepare_ns - find an existing or create a new namespace of @name
  270. * @parent: ns to treat as parent
  271. * @name: the namespace to find or add (NOT NULL)
  272. *
  273. * Returns: refcounted namespace or PTR_ERR if failed to create one
  274. */
  275. struct aa_ns *aa_prepare_ns(struct aa_ns *parent, const char *name)
  276. {
  277. struct aa_ns *ns;
  278. mutex_lock_nested(&parent->lock, parent->level);
  279. /* try and find the specified ns and if it doesn't exist create it */
  280. /* released by caller */
  281. ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
  282. if (!ns)
  283. ns = __aa_create_ns(parent, name, NULL);
  284. mutex_unlock(&parent->lock);
  285. /* return ref */
  286. return ns;
  287. }
  288. static void __ns_list_release(struct list_head *head);
  289. /**
  290. * destroy_ns - remove everything contained by @ns
  291. * @ns: namespace to have it contents removed (NOT NULL)
  292. */
  293. static void destroy_ns(struct aa_ns *ns)
  294. {
  295. if (!ns)
  296. return;
  297. mutex_lock_nested(&ns->lock, ns->level);
  298. /* release all profiles in this namespace */
  299. __aa_profile_list_release(&ns->base.profiles);
  300. /* release all sub namespaces */
  301. __ns_list_release(&ns->sub_ns);
  302. if (ns->parent) {
  303. unsigned long flags;
  304. write_lock_irqsave(&ns->labels.lock, flags);
  305. __aa_proxy_redirect(ns_unconfined(ns),
  306. ns_unconfined(ns->parent));
  307. write_unlock_irqrestore(&ns->labels.lock, flags);
  308. }
  309. __aafs_ns_rmdir(ns);
  310. mutex_unlock(&ns->lock);
  311. }
  312. /**
  313. * __aa_remove_ns - remove a namespace and all its children
  314. * @ns: namespace to be removed (NOT NULL)
  315. *
  316. * Requires: ns->parent->lock be held and ns removed from parent.
  317. */
  318. void __aa_remove_ns(struct aa_ns *ns)
  319. {
  320. /* remove ns from namespace list */
  321. list_del_rcu(&ns->base.list);
  322. destroy_ns(ns);
  323. aa_put_ns(ns);
  324. }
  325. /**
  326. * __ns_list_release - remove all profile namespaces on the list put refs
  327. * @head: list of profile namespaces (NOT NULL)
  328. *
  329. * Requires: namespace lock be held
  330. */
  331. static void __ns_list_release(struct list_head *head)
  332. {
  333. struct aa_ns *ns, *tmp;
  334. list_for_each_entry_safe(ns, tmp, head, base.list)
  335. __aa_remove_ns(ns);
  336. }
  337. /**
  338. * aa_alloc_root_ns - allocate the root profile namespace
  339. *
  340. * Returns: %0 on success else error
  341. *
  342. */
  343. int __init aa_alloc_root_ns(void)
  344. {
  345. struct aa_profile *kernel_p;
  346. /* released by aa_free_root_ns - used as list ref*/
  347. root_ns = alloc_ns(NULL, "root");
  348. if (!root_ns)
  349. return -ENOMEM;
  350. kernel_p = alloc_unconfined("kernel_t");
  351. if (!kernel_p) {
  352. destroy_ns(root_ns);
  353. aa_free_ns(root_ns);
  354. return -ENOMEM;
  355. }
  356. kernel_t = &kernel_p->label;
  357. root_ns->unconfined->ns = aa_get_ns(root_ns);
  358. return 0;
  359. }
  360. /**
  361. * aa_free_root_ns - free the root profile namespace
  362. */
  363. void __init aa_free_root_ns(void)
  364. {
  365. struct aa_ns *ns = root_ns;
  366. root_ns = NULL;
  367. aa_label_free(kernel_t);
  368. destroy_ns(ns);
  369. aa_put_ns(ns);
  370. }