policy_ns.c 9.8 KB

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