policy.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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-2010 Canonical Ltd.
  9. *
  10. * AppArmor policy is based around profiles, which contain the rules a
  11. * task is confined by. Every task in the system has a profile attached
  12. * to it determined either by matching "unconfined" tasks against the
  13. * visible set of profiles or by following a profiles attachment rules.
  14. *
  15. * Each profile exists in a profile namespace which is a container of
  16. * visible profiles. Each namespace contains a special "unconfined" profile,
  17. * which doesn't enforce any confinement on a task beyond DAC.
  18. *
  19. * Namespace and profile names can be written together in either
  20. * of two syntaxes.
  21. * :namespace:profile - used by kernel interfaces for easy detection
  22. * namespace://profile - used by policy
  23. *
  24. * Profile names can not start with : or @ or ^ and may not contain \0
  25. *
  26. * Reserved profile names
  27. * unconfined - special automatically generated unconfined profile
  28. * inherit - special name to indicate profile inheritance
  29. * null-XXXX-YYYY - special automatically generated learning profiles
  30. *
  31. * Namespace names may not start with / or @ and may not contain \0 or :
  32. * Reserved namespace names
  33. * user-XXXX - user defined profiles
  34. *
  35. * a // in a profile or namespace name indicates a hierarchical name with the
  36. * name before the // being the parent and the name after the child.
  37. *
  38. * Profile and namespace hierarchies serve two different but similar purposes.
  39. * The namespace contains the set of visible profiles that are considered
  40. * for attachment. The hierarchy of namespaces allows for virtualizing
  41. * the namespace so that for example a chroot can have its own set of profiles
  42. * which may define some local user namespaces.
  43. * The profile hierarchy severs two distinct purposes,
  44. * - it allows for sub profiles or hats, which allows an application to run
  45. * subprograms under its own profile with different restriction than it
  46. * self, and not have it use the system profile.
  47. * eg. if a mail program starts an editor, the policy might make the
  48. * restrictions tighter on the editor tighter than the mail program,
  49. * and definitely different than general editor restrictions
  50. * - it allows for binary hierarchy of profiles, so that execution history
  51. * is preserved. This feature isn't exploited by AppArmor reference policy
  52. * but is allowed. NOTE: this is currently suboptimal because profile
  53. * aliasing is not currently implemented so that a profile for each
  54. * level must be defined.
  55. * eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
  56. * from /bin/bash
  57. *
  58. * A profile or namespace name that can contain one or more // separators
  59. * is referred to as an hname (hierarchical).
  60. * eg. /bin/bash//bin/ls
  61. *
  62. * An fqname is a name that may contain both namespace and profile hnames.
  63. * eg. :ns:/bin/bash//bin/ls
  64. *
  65. * NOTES:
  66. * - locking of profile lists is currently fairly coarse. All profile
  67. * lists within a namespace use the namespace lock.
  68. * FIXME: move profile lists to using rcu_lists
  69. */
  70. #include <linux/slab.h>
  71. #include <linux/spinlock.h>
  72. #include <linux/string.h>
  73. #include <linux/cred.h>
  74. #include <linux/rculist.h>
  75. #include <linux/user_namespace.h>
  76. #include "include/apparmor.h"
  77. #include "include/capability.h"
  78. #include "include/cred.h"
  79. #include "include/file.h"
  80. #include "include/ipc.h"
  81. #include "include/match.h"
  82. #include "include/path.h"
  83. #include "include/policy.h"
  84. #include "include/policy_ns.h"
  85. #include "include/policy_unpack.h"
  86. #include "include/resource.h"
  87. int unprivileged_userns_apparmor_policy = 1;
  88. int aa_unprivileged_unconfined_restricted;
  89. const char *const aa_profile_mode_names[] = {
  90. "enforce",
  91. "complain",
  92. "kill",
  93. "unconfined",
  94. "user",
  95. };
  96. static void aa_free_pdb(struct aa_policydb *pdb)
  97. {
  98. if (pdb) {
  99. aa_put_dfa(pdb->dfa);
  100. if (pdb->perms)
  101. kvfree(pdb->perms);
  102. aa_free_str_table(&pdb->trans);
  103. kfree(pdb);
  104. }
  105. }
  106. /**
  107. * aa_pdb_free_kref - free aa_policydb by kref (called by aa_put_pdb)
  108. * @kref: kref callback for freeing of a dfa (NOT NULL)
  109. */
  110. void aa_pdb_free_kref(struct kref *kref)
  111. {
  112. struct aa_policydb *pdb = container_of(kref, struct aa_policydb, count);
  113. aa_free_pdb(pdb);
  114. }
  115. struct aa_policydb *aa_alloc_pdb(gfp_t gfp)
  116. {
  117. struct aa_policydb *pdb = kzalloc(sizeof(struct aa_policydb), gfp);
  118. if (!pdb)
  119. return NULL;
  120. kref_init(&pdb->count);
  121. return pdb;
  122. }
  123. /**
  124. * __add_profile - add a profiles to list and label tree
  125. * @list: list to add it to (NOT NULL)
  126. * @profile: the profile to add (NOT NULL)
  127. *
  128. * refcount @profile, should be put by __list_remove_profile
  129. *
  130. * Requires: namespace lock be held, or list not be shared
  131. */
  132. static void __add_profile(struct list_head *list, struct aa_profile *profile)
  133. {
  134. struct aa_label *l;
  135. AA_BUG(!list);
  136. AA_BUG(!profile);
  137. AA_BUG(!profile->ns);
  138. AA_BUG(!mutex_is_locked(&profile->ns->lock));
  139. list_add_rcu(&profile->base.list, list);
  140. /* get list reference */
  141. aa_get_profile(profile);
  142. l = aa_label_insert(&profile->ns->labels, &profile->label);
  143. AA_BUG(l != &profile->label);
  144. aa_put_label(l);
  145. }
  146. /**
  147. * __list_remove_profile - remove a profile from the list it is on
  148. * @profile: the profile to remove (NOT NULL)
  149. *
  150. * remove a profile from the list, warning generally removal should
  151. * be done with __replace_profile as most profile removals are
  152. * replacements to the unconfined profile.
  153. *
  154. * put @profile list refcount
  155. *
  156. * Requires: namespace lock be held, or list not have been live
  157. */
  158. static void __list_remove_profile(struct aa_profile *profile)
  159. {
  160. AA_BUG(!profile);
  161. AA_BUG(!profile->ns);
  162. AA_BUG(!mutex_is_locked(&profile->ns->lock));
  163. list_del_rcu(&profile->base.list);
  164. aa_put_profile(profile);
  165. }
  166. /**
  167. * __remove_profile - remove old profile, and children
  168. * @profile: profile to be replaced (NOT NULL)
  169. *
  170. * Requires: namespace list lock be held, or list not be shared
  171. */
  172. static void __remove_profile(struct aa_profile *profile)
  173. {
  174. AA_BUG(!profile);
  175. AA_BUG(!profile->ns);
  176. AA_BUG(!mutex_is_locked(&profile->ns->lock));
  177. /* release any children lists first */
  178. __aa_profile_list_release(&profile->base.profiles);
  179. /* released by free_profile */
  180. aa_label_remove(&profile->label);
  181. __aafs_profile_rmdir(profile);
  182. __list_remove_profile(profile);
  183. }
  184. /**
  185. * __aa_profile_list_release - remove all profiles on the list and put refs
  186. * @head: list of profiles (NOT NULL)
  187. *
  188. * Requires: namespace lock be held
  189. */
  190. void __aa_profile_list_release(struct list_head *head)
  191. {
  192. struct aa_profile *profile, *tmp;
  193. list_for_each_entry_safe(profile, tmp, head, base.list)
  194. __remove_profile(profile);
  195. }
  196. /**
  197. * aa_free_data - free a data blob
  198. * @ptr: data to free
  199. * @arg: unused
  200. */
  201. static void aa_free_data(void *ptr, void *arg)
  202. {
  203. struct aa_data *data = ptr;
  204. kvfree_sensitive(data->data, data->size);
  205. kfree_sensitive(data->key);
  206. kfree_sensitive(data);
  207. }
  208. static void free_attachment(struct aa_attachment *attach)
  209. {
  210. int i;
  211. for (i = 0; i < attach->xattr_count; i++)
  212. kfree_sensitive(attach->xattrs[i]);
  213. kfree_sensitive(attach->xattrs);
  214. aa_put_pdb(attach->xmatch);
  215. }
  216. static void free_ruleset(struct aa_ruleset *rules)
  217. {
  218. int i;
  219. aa_put_pdb(rules->file);
  220. aa_put_pdb(rules->policy);
  221. aa_free_cap_rules(&rules->caps);
  222. aa_free_rlimit_rules(&rules->rlimits);
  223. for (i = 0; i < rules->secmark_count; i++)
  224. kfree_sensitive(rules->secmark[i].label);
  225. kfree_sensitive(rules->secmark);
  226. kfree_sensitive(rules);
  227. }
  228. struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp)
  229. {
  230. struct aa_ruleset *rules;
  231. rules = kzalloc(sizeof(*rules), gfp);
  232. if (rules)
  233. INIT_LIST_HEAD(&rules->list);
  234. return rules;
  235. }
  236. /**
  237. * aa_free_profile - free a profile
  238. * @profile: the profile to free (MAYBE NULL)
  239. *
  240. * Free a profile, its hats and null_profile. All references to the profile,
  241. * its hats and null_profile must have been put.
  242. *
  243. * If the profile was referenced from a task context, free_profile() will
  244. * be called from an rcu callback routine, so we must not sleep here.
  245. */
  246. void aa_free_profile(struct aa_profile *profile)
  247. {
  248. struct aa_ruleset *rule, *tmp;
  249. struct rhashtable *rht;
  250. AA_DEBUG("%s(%p)\n", __func__, profile);
  251. if (!profile)
  252. return;
  253. /* free children profiles */
  254. aa_policy_destroy(&profile->base);
  255. aa_put_profile(rcu_access_pointer(profile->parent));
  256. aa_put_ns(profile->ns);
  257. kfree_sensitive(profile->rename);
  258. kfree_sensitive(profile->disconnected);
  259. free_attachment(&profile->attach);
  260. /*
  261. * at this point there are no tasks that can have a reference
  262. * to rules
  263. */
  264. list_for_each_entry_safe(rule, tmp, &profile->rules, list) {
  265. list_del_init(&rule->list);
  266. free_ruleset(rule);
  267. }
  268. kfree_sensitive(profile->dirname);
  269. if (profile->data) {
  270. rht = profile->data;
  271. profile->data = NULL;
  272. rhashtable_free_and_destroy(rht, aa_free_data, NULL);
  273. kfree_sensitive(rht);
  274. }
  275. kfree_sensitive(profile->hash);
  276. aa_put_loaddata(profile->rawdata);
  277. aa_label_destroy(&profile->label);
  278. kfree_sensitive(profile);
  279. }
  280. /**
  281. * aa_alloc_profile - allocate, initialize and return a new profile
  282. * @hname: name of the profile (NOT NULL)
  283. * @proxy: proxy to use OR null if to allocate a new one
  284. * @gfp: allocation type
  285. *
  286. * Returns: refcount profile or NULL on failure
  287. */
  288. struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
  289. gfp_t gfp)
  290. {
  291. struct aa_profile *profile;
  292. struct aa_ruleset *rules;
  293. /* freed by free_profile - usually through aa_put_profile */
  294. profile = kzalloc(struct_size(profile, label.vec, 2), gfp);
  295. if (!profile)
  296. return NULL;
  297. if (!aa_policy_init(&profile->base, NULL, hname, gfp))
  298. goto fail;
  299. if (!aa_label_init(&profile->label, 1, gfp))
  300. goto fail;
  301. INIT_LIST_HEAD(&profile->rules);
  302. /* allocate the first ruleset, but leave it empty */
  303. rules = aa_alloc_ruleset(gfp);
  304. if (!rules)
  305. goto fail;
  306. list_add(&rules->list, &profile->rules);
  307. /* update being set needed by fs interface */
  308. if (!proxy) {
  309. proxy = aa_alloc_proxy(&profile->label, gfp);
  310. if (!proxy)
  311. goto fail;
  312. } else
  313. aa_get_proxy(proxy);
  314. profile->label.proxy = proxy;
  315. profile->label.hname = profile->base.hname;
  316. profile->label.flags |= FLAG_PROFILE;
  317. profile->label.vec[0] = profile;
  318. /* refcount released by caller */
  319. return profile;
  320. fail:
  321. aa_free_profile(profile);
  322. return NULL;
  323. }
  324. /* TODO: profile accounting - setup in remove */
  325. /**
  326. * __strn_find_child - find a profile on @head list using substring of @name
  327. * @head: list to search (NOT NULL)
  328. * @name: name of profile (NOT NULL)
  329. * @len: length of @name substring to match
  330. *
  331. * Requires: rcu_read_lock be held
  332. *
  333. * Returns: unrefcounted profile ptr, or NULL if not found
  334. */
  335. static struct aa_profile *__strn_find_child(struct list_head *head,
  336. const char *name, int len)
  337. {
  338. return (struct aa_profile *)__policy_strn_find(head, name, len);
  339. }
  340. /**
  341. * __find_child - find a profile on @head list with a name matching @name
  342. * @head: list to search (NOT NULL)
  343. * @name: name of profile (NOT NULL)
  344. *
  345. * Requires: rcu_read_lock be held
  346. *
  347. * Returns: unrefcounted profile ptr, or NULL if not found
  348. */
  349. static struct aa_profile *__find_child(struct list_head *head, const char *name)
  350. {
  351. return __strn_find_child(head, name, strlen(name));
  352. }
  353. /**
  354. * aa_find_child - find a profile by @name in @parent
  355. * @parent: profile to search (NOT NULL)
  356. * @name: profile name to search for (NOT NULL)
  357. *
  358. * Returns: a refcounted profile or NULL if not found
  359. */
  360. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
  361. {
  362. struct aa_profile *profile;
  363. rcu_read_lock();
  364. do {
  365. profile = __find_child(&parent->base.profiles, name);
  366. } while (profile && !aa_get_profile_not0(profile));
  367. rcu_read_unlock();
  368. /* refcount released by caller */
  369. return profile;
  370. }
  371. /**
  372. * __lookup_parent - lookup the parent of a profile of name @hname
  373. * @ns: namespace to lookup profile in (NOT NULL)
  374. * @hname: hierarchical profile name to find parent of (NOT NULL)
  375. *
  376. * Lookups up the parent of a fully qualified profile name, the profile
  377. * that matches hname does not need to exist, in general this
  378. * is used to load a new profile.
  379. *
  380. * Requires: rcu_read_lock be held
  381. *
  382. * Returns: unrefcounted policy or NULL if not found
  383. */
  384. static struct aa_policy *__lookup_parent(struct aa_ns *ns,
  385. const char *hname)
  386. {
  387. struct aa_policy *policy;
  388. struct aa_profile *profile = NULL;
  389. char *split;
  390. policy = &ns->base;
  391. for (split = strstr(hname, "//"); split;) {
  392. profile = __strn_find_child(&policy->profiles, hname,
  393. split - hname);
  394. if (!profile)
  395. return NULL;
  396. policy = &profile->base;
  397. hname = split + 2;
  398. split = strstr(hname, "//");
  399. }
  400. if (!profile)
  401. return &ns->base;
  402. return &profile->base;
  403. }
  404. /**
  405. * __create_missing_ancestors - create place holders for missing ancestores
  406. * @ns: namespace to lookup profile in (NOT NULL)
  407. * @hname: hierarchical profile name to find parent of (NOT NULL)
  408. * @gfp: type of allocation.
  409. *
  410. * Requires: ns mutex lock held
  411. *
  412. * Return: unrefcounted parent policy on success or %NULL if error creating
  413. * place holder profiles.
  414. */
  415. static struct aa_policy *__create_missing_ancestors(struct aa_ns *ns,
  416. const char *hname,
  417. gfp_t gfp)
  418. {
  419. struct aa_policy *policy;
  420. struct aa_profile *parent, *profile = NULL;
  421. char *split;
  422. AA_BUG(!ns);
  423. AA_BUG(!hname);
  424. policy = &ns->base;
  425. for (split = strstr(hname, "//"); split;) {
  426. parent = profile;
  427. profile = __strn_find_child(&policy->profiles, hname,
  428. split - hname);
  429. if (!profile) {
  430. const char *name = kstrndup(hname, split - hname,
  431. gfp);
  432. if (!name)
  433. return NULL;
  434. profile = aa_alloc_null(parent, name, gfp);
  435. kfree(name);
  436. if (!profile)
  437. return NULL;
  438. if (!parent)
  439. profile->ns = aa_get_ns(ns);
  440. }
  441. policy = &profile->base;
  442. hname = split + 2;
  443. split = strstr(hname, "//");
  444. }
  445. if (!profile)
  446. return &ns->base;
  447. return &profile->base;
  448. }
  449. /**
  450. * __lookupn_profile - lookup the profile matching @hname
  451. * @base: base list to start looking up profile name from (NOT NULL)
  452. * @hname: hierarchical profile name (NOT NULL)
  453. * @n: length of @hname
  454. *
  455. * Requires: rcu_read_lock be held
  456. *
  457. * Returns: unrefcounted profile pointer or NULL if not found
  458. *
  459. * Do a relative name lookup, recursing through profile tree.
  460. */
  461. static struct aa_profile *__lookupn_profile(struct aa_policy *base,
  462. const char *hname, size_t n)
  463. {
  464. struct aa_profile *profile = NULL;
  465. const char *split;
  466. for (split = strnstr(hname, "//", n); split;
  467. split = strnstr(hname, "//", n)) {
  468. profile = __strn_find_child(&base->profiles, hname,
  469. split - hname);
  470. if (!profile)
  471. return NULL;
  472. base = &profile->base;
  473. n -= split + 2 - hname;
  474. hname = split + 2;
  475. }
  476. if (n)
  477. return __strn_find_child(&base->profiles, hname, n);
  478. return NULL;
  479. }
  480. static struct aa_profile *__lookup_profile(struct aa_policy *base,
  481. const char *hname)
  482. {
  483. return __lookupn_profile(base, hname, strlen(hname));
  484. }
  485. /**
  486. * aa_lookupn_profile - find a profile by its full or partial name
  487. * @ns: the namespace to start from (NOT NULL)
  488. * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
  489. * @n: size of @hname
  490. *
  491. * Returns: refcounted profile or NULL if not found
  492. */
  493. struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
  494. size_t n)
  495. {
  496. struct aa_profile *profile;
  497. rcu_read_lock();
  498. do {
  499. profile = __lookupn_profile(&ns->base, hname, n);
  500. } while (profile && !aa_get_profile_not0(profile));
  501. rcu_read_unlock();
  502. /* the unconfined profile is not in the regular profile list */
  503. if (!profile && strncmp(hname, "unconfined", n) == 0)
  504. profile = aa_get_newest_profile(ns->unconfined);
  505. /* refcount released by caller */
  506. return profile;
  507. }
  508. struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
  509. {
  510. return aa_lookupn_profile(ns, hname, strlen(hname));
  511. }
  512. struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
  513. const char *fqname, size_t n)
  514. {
  515. struct aa_profile *profile;
  516. struct aa_ns *ns;
  517. const char *name, *ns_name;
  518. size_t ns_len;
  519. name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
  520. if (ns_name) {
  521. ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
  522. if (!ns)
  523. return NULL;
  524. } else
  525. ns = aa_get_ns(labels_ns(base));
  526. if (name)
  527. profile = aa_lookupn_profile(ns, name, n - (name - fqname));
  528. else if (ns)
  529. /* default profile for ns, currently unconfined */
  530. profile = aa_get_newest_profile(ns->unconfined);
  531. else
  532. profile = NULL;
  533. aa_put_ns(ns);
  534. return profile;
  535. }
  536. struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
  537. gfp_t gfp)
  538. {
  539. struct aa_profile *profile;
  540. struct aa_ruleset *rules;
  541. profile = aa_alloc_profile(name, NULL, gfp);
  542. if (!profile)
  543. return NULL;
  544. /* TODO: ideally we should inherit abi from parent */
  545. profile->label.flags |= FLAG_NULL;
  546. profile->attach.xmatch = aa_get_pdb(nullpdb);
  547. rules = list_first_entry(&profile->rules, typeof(*rules), list);
  548. rules->file = aa_get_pdb(nullpdb);
  549. rules->policy = aa_get_pdb(nullpdb);
  550. if (parent) {
  551. profile->path_flags = parent->path_flags;
  552. /* released on free_profile */
  553. rcu_assign_pointer(profile->parent, aa_get_profile(parent));
  554. profile->ns = aa_get_ns(parent->ns);
  555. }
  556. return profile;
  557. }
  558. /**
  559. * aa_new_learning_profile - create or find a null-X learning profile
  560. * @parent: profile that caused this profile to be created (NOT NULL)
  561. * @hat: true if the null- learning profile is a hat
  562. * @base: name to base the null profile off of
  563. * @gfp: type of allocation
  564. *
  565. * Find/Create a null- complain mode profile used in learning mode. The
  566. * name of the profile is unique and follows the format of parent//null-XXX.
  567. * where XXX is based on the @name or if that fails or is not supplied
  568. * a unique number
  569. *
  570. * null profiles are added to the profile list but the list does not
  571. * hold a count on them so that they are automatically released when
  572. * not in use.
  573. *
  574. * Returns: new refcounted profile else NULL on failure
  575. */
  576. struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
  577. const char *base, gfp_t gfp)
  578. {
  579. struct aa_profile *p, *profile;
  580. const char *bname;
  581. char *name = NULL;
  582. AA_BUG(!parent);
  583. if (base) {
  584. name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
  585. gfp);
  586. if (name) {
  587. sprintf(name, "%s//null-%s", parent->base.hname, base);
  588. goto name;
  589. }
  590. /* fall through to try shorter uniq */
  591. }
  592. name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
  593. if (!name)
  594. return NULL;
  595. sprintf(name, "%s//null-%x", parent->base.hname,
  596. atomic_inc_return(&parent->ns->uniq_null));
  597. name:
  598. /* lookup to see if this is a dup creation */
  599. bname = basename(name);
  600. profile = aa_find_child(parent, bname);
  601. if (profile)
  602. goto out;
  603. profile = aa_alloc_null(parent, name, gfp);
  604. if (!profile)
  605. goto fail;
  606. profile->mode = APPARMOR_COMPLAIN;
  607. if (hat)
  608. profile->label.flags |= FLAG_HAT;
  609. mutex_lock_nested(&profile->ns->lock, profile->ns->level);
  610. p = __find_child(&parent->base.profiles, bname);
  611. if (p) {
  612. aa_free_profile(profile);
  613. profile = aa_get_profile(p);
  614. } else {
  615. __add_profile(&parent->base.profiles, profile);
  616. }
  617. mutex_unlock(&profile->ns->lock);
  618. /* refcount released by caller */
  619. out:
  620. kfree(name);
  621. return profile;
  622. fail:
  623. kfree(name);
  624. aa_free_profile(profile);
  625. return NULL;
  626. }
  627. /**
  628. * replacement_allowed - test to see if replacement is allowed
  629. * @profile: profile to test if it can be replaced (MAYBE NULL)
  630. * @noreplace: true if replacement shouldn't be allowed but addition is okay
  631. * @info: Returns - info about why replacement failed (NOT NULL)
  632. *
  633. * Returns: %0 if replacement allowed else error code
  634. */
  635. static int replacement_allowed(struct aa_profile *profile, int noreplace,
  636. const char **info)
  637. {
  638. if (profile) {
  639. if (profile->label.flags & FLAG_IMMUTIBLE) {
  640. *info = "cannot replace immutable profile";
  641. return -EPERM;
  642. } else if (noreplace) {
  643. *info = "profile already exists";
  644. return -EEXIST;
  645. }
  646. }
  647. return 0;
  648. }
  649. /* audit callback for net specific fields */
  650. static void audit_cb(struct audit_buffer *ab, void *va)
  651. {
  652. struct common_audit_data *sa = va;
  653. struct apparmor_audit_data *ad = aad(sa);
  654. if (ad->iface.ns) {
  655. audit_log_format(ab, " ns=");
  656. audit_log_untrustedstring(ab, ad->iface.ns);
  657. }
  658. }
  659. /**
  660. * audit_policy - Do auditing of policy changes
  661. * @subj_label: label to check if it can manage policy
  662. * @op: policy operation being performed
  663. * @ns_name: name of namespace being manipulated
  664. * @name: name of profile being manipulated (NOT NULL)
  665. * @info: any extra information to be audited (MAYBE NULL)
  666. * @error: error code
  667. *
  668. * Returns: the error to be returned after audit is done
  669. */
  670. static int audit_policy(struct aa_label *subj_label, const char *op,
  671. const char *ns_name, const char *name,
  672. const char *info, int error)
  673. {
  674. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, op);
  675. ad.iface.ns = ns_name;
  676. ad.name = name;
  677. ad.info = info;
  678. ad.error = error;
  679. ad.subj_label = subj_label;
  680. aa_audit_msg(AUDIT_APPARMOR_STATUS, &ad, audit_cb);
  681. return error;
  682. }
  683. /* don't call out to other LSMs in the stack for apparmor policy admin
  684. * permissions
  685. */
  686. static int policy_ns_capable(const struct cred *subj_cred,
  687. struct aa_label *label,
  688. struct user_namespace *userns, int cap)
  689. {
  690. int err;
  691. /* check for MAC_ADMIN cap in cred */
  692. err = cap_capable(subj_cred, userns, cap, CAP_OPT_NONE);
  693. if (!err)
  694. err = aa_capable(subj_cred, label, cap, CAP_OPT_NONE);
  695. return err;
  696. }
  697. /**
  698. * aa_policy_view_capable - check if viewing policy in at @ns is allowed
  699. * @subj_cred: cred of subject
  700. * @label: label that is trying to view policy in ns
  701. * @ns: namespace being viewed by @label (may be NULL if @label's ns)
  702. *
  703. * Returns: true if viewing policy is allowed
  704. *
  705. * If @ns is NULL then the namespace being viewed is assumed to be the
  706. * tasks current namespace.
  707. */
  708. bool aa_policy_view_capable(const struct cred *subj_cred,
  709. struct aa_label *label, struct aa_ns *ns)
  710. {
  711. struct user_namespace *user_ns = subj_cred->user_ns;
  712. struct aa_ns *view_ns = labels_view(label);
  713. bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
  714. in_egroup_p(make_kgid(user_ns, 0));
  715. bool response = false;
  716. if (!ns)
  717. ns = view_ns;
  718. if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
  719. (user_ns == &init_user_ns ||
  720. (unprivileged_userns_apparmor_policy != 0 &&
  721. user_ns->level == view_ns->level)))
  722. response = true;
  723. return response;
  724. }
  725. bool aa_policy_admin_capable(const struct cred *subj_cred,
  726. struct aa_label *label, struct aa_ns *ns)
  727. {
  728. struct user_namespace *user_ns = subj_cred->user_ns;
  729. bool capable = policy_ns_capable(subj_cred, label, user_ns,
  730. CAP_MAC_ADMIN) == 0;
  731. AA_DEBUG("cap_mac_admin? %d\n", capable);
  732. AA_DEBUG("policy locked? %d\n", aa_g_lock_policy);
  733. return aa_policy_view_capable(subj_cred, label, ns) && capable &&
  734. !aa_g_lock_policy;
  735. }
  736. bool aa_current_policy_view_capable(struct aa_ns *ns)
  737. {
  738. struct aa_label *label;
  739. bool res;
  740. label = __begin_current_label_crit_section();
  741. res = aa_policy_view_capable(current_cred(), label, ns);
  742. __end_current_label_crit_section(label);
  743. return res;
  744. }
  745. bool aa_current_policy_admin_capable(struct aa_ns *ns)
  746. {
  747. struct aa_label *label;
  748. bool res;
  749. label = __begin_current_label_crit_section();
  750. res = aa_policy_admin_capable(current_cred(), label, ns);
  751. __end_current_label_crit_section(label);
  752. return res;
  753. }
  754. /**
  755. * aa_may_manage_policy - can the current task manage policy
  756. * @subj_cred: subjects cred
  757. * @label: label to check if it can manage policy
  758. * @ns: namespace being managed by @label (may be NULL if @label's ns)
  759. * @mask: contains the policy manipulation operation being done
  760. *
  761. * Returns: 0 if the task is allowed to manipulate policy else error
  762. */
  763. int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
  764. struct aa_ns *ns, u32 mask)
  765. {
  766. const char *op;
  767. if (mask & AA_MAY_REMOVE_POLICY)
  768. op = OP_PROF_RM;
  769. else if (mask & AA_MAY_REPLACE_POLICY)
  770. op = OP_PROF_REPL;
  771. else
  772. op = OP_PROF_LOAD;
  773. /* check if loading policy is locked out */
  774. if (aa_g_lock_policy)
  775. return audit_policy(label, op, NULL, NULL, "policy_locked",
  776. -EACCES);
  777. if (!aa_policy_admin_capable(subj_cred, label, ns))
  778. return audit_policy(label, op, NULL, NULL, "not policy admin",
  779. -EACCES);
  780. /* TODO: add fine grained mediation of policy loads */
  781. return 0;
  782. }
  783. static struct aa_profile *__list_lookup_parent(struct list_head *lh,
  784. struct aa_profile *profile)
  785. {
  786. const char *base = basename(profile->base.hname);
  787. long len = base - profile->base.hname;
  788. struct aa_load_ent *ent;
  789. /* parent won't have trailing // so remove from len */
  790. if (len <= 2)
  791. return NULL;
  792. len -= 2;
  793. list_for_each_entry(ent, lh, list) {
  794. if (ent->new == profile)
  795. continue;
  796. if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
  797. 0 && ent->new->base.hname[len] == 0)
  798. return ent->new;
  799. }
  800. return NULL;
  801. }
  802. /**
  803. * __replace_profile - replace @old with @new on a list
  804. * @old: profile to be replaced (NOT NULL)
  805. * @new: profile to replace @old with (NOT NULL)
  806. *
  807. * Will duplicate and refcount elements that @new inherits from @old
  808. * and will inherit @old children.
  809. *
  810. * refcount @new for list, put @old list refcount
  811. *
  812. * Requires: namespace list lock be held, or list not be shared
  813. */
  814. static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
  815. {
  816. struct aa_profile *child, *tmp;
  817. if (!list_empty(&old->base.profiles)) {
  818. LIST_HEAD(lh);
  819. list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
  820. list_for_each_entry_safe(child, tmp, &lh, base.list) {
  821. struct aa_profile *p;
  822. list_del_init(&child->base.list);
  823. p = __find_child(&new->base.profiles, child->base.name);
  824. if (p) {
  825. /* @p replaces @child */
  826. __replace_profile(child, p);
  827. continue;
  828. }
  829. /* inherit @child and its children */
  830. /* TODO: update hname of inherited children */
  831. /* list refcount transferred to @new */
  832. p = aa_deref_parent(child);
  833. rcu_assign_pointer(child->parent, aa_get_profile(new));
  834. list_add_rcu(&child->base.list, &new->base.profiles);
  835. aa_put_profile(p);
  836. }
  837. }
  838. if (!rcu_access_pointer(new->parent)) {
  839. struct aa_profile *parent = aa_deref_parent(old);
  840. rcu_assign_pointer(new->parent, aa_get_profile(parent));
  841. }
  842. aa_label_replace(&old->label, &new->label);
  843. /* migrate dents must come after label replacement b/c update */
  844. __aafs_profile_migrate_dents(old, new);
  845. if (list_empty(&new->base.list)) {
  846. /* new is not on a list already */
  847. list_replace_rcu(&old->base.list, &new->base.list);
  848. aa_get_profile(new);
  849. aa_put_profile(old);
  850. } else
  851. __list_remove_profile(old);
  852. }
  853. /**
  854. * __lookup_replace - lookup replacement information for a profile
  855. * @ns: namespace the lookup occurs in
  856. * @hname: name of profile to lookup
  857. * @noreplace: true if not replacing an existing profile
  858. * @p: Returns - profile to be replaced
  859. * @info: Returns - info string on why lookup failed
  860. *
  861. * Returns: profile to replace (no ref) on success else ptr error
  862. */
  863. static int __lookup_replace(struct aa_ns *ns, const char *hname,
  864. bool noreplace, struct aa_profile **p,
  865. const char **info)
  866. {
  867. *p = aa_get_profile(__lookup_profile(&ns->base, hname));
  868. if (*p) {
  869. int error = replacement_allowed(*p, noreplace, info);
  870. if (error) {
  871. *info = "profile can not be replaced";
  872. return error;
  873. }
  874. }
  875. return 0;
  876. }
  877. static void share_name(struct aa_profile *old, struct aa_profile *new)
  878. {
  879. aa_put_str(new->base.hname);
  880. aa_get_str(old->base.hname);
  881. new->base.hname = old->base.hname;
  882. new->base.name = old->base.name;
  883. new->label.hname = old->label.hname;
  884. }
  885. /* Update to newest version of parent after previous replacements
  886. * Returns: unrefcount newest version of parent
  887. */
  888. static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
  889. {
  890. struct aa_profile *parent, *newest;
  891. parent = rcu_dereference_protected(new->parent,
  892. mutex_is_locked(&new->ns->lock));
  893. newest = aa_get_newest_profile(parent);
  894. /* parent replaced in this atomic set? */
  895. if (newest != parent) {
  896. aa_put_profile(parent);
  897. rcu_assign_pointer(new->parent, newest);
  898. } else
  899. aa_put_profile(newest);
  900. return newest;
  901. }
  902. /**
  903. * aa_replace_profiles - replace profile(s) on the profile list
  904. * @policy_ns: namespace load is occurring on
  905. * @label: label that is attempting to load/replace policy
  906. * @mask: permission mask
  907. * @udata: serialized data stream (NOT NULL)
  908. *
  909. * unpack and replace a profile on the profile list and uses of that profile
  910. * by any task creds via invalidating the old version of the profile, which
  911. * tasks will notice to update their own cred. If the profile does not exist
  912. * on the profile list it is added.
  913. *
  914. * Returns: size of data consumed else error code on failure.
  915. */
  916. ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
  917. u32 mask, struct aa_loaddata *udata)
  918. {
  919. const char *ns_name = NULL, *info = NULL;
  920. struct aa_ns *ns = NULL;
  921. struct aa_load_ent *ent, *tmp;
  922. struct aa_loaddata *rawdata_ent;
  923. const char *op;
  924. ssize_t count, error;
  925. LIST_HEAD(lh);
  926. op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
  927. aa_get_loaddata(udata);
  928. /* released below */
  929. error = aa_unpack(udata, &lh, &ns_name);
  930. if (error)
  931. goto out;
  932. /* ensure that profiles are all for the same ns
  933. * TODO: update locking to remove this constaint. All profiles in
  934. * the load set must succeed as a set or the load will
  935. * fail. Sort ent list and take ns locks in hierarchy order
  936. */
  937. count = 0;
  938. list_for_each_entry(ent, &lh, list) {
  939. if (ns_name) {
  940. if (ent->ns_name &&
  941. strcmp(ent->ns_name, ns_name) != 0) {
  942. info = "policy load has mixed namespaces";
  943. error = -EACCES;
  944. goto fail;
  945. }
  946. } else if (ent->ns_name) {
  947. if (count) {
  948. info = "policy load has mixed namespaces";
  949. error = -EACCES;
  950. goto fail;
  951. }
  952. ns_name = ent->ns_name;
  953. } else
  954. count++;
  955. }
  956. if (ns_name) {
  957. ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
  958. ns_name);
  959. if (IS_ERR(ns)) {
  960. op = OP_PROF_LOAD;
  961. info = "failed to prepare namespace";
  962. error = PTR_ERR(ns);
  963. ns = NULL;
  964. ent = NULL;
  965. goto fail;
  966. }
  967. } else
  968. ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
  969. mutex_lock_nested(&ns->lock, ns->level);
  970. /* check for duplicate rawdata blobs: space and file dedup */
  971. if (!list_empty(&ns->rawdata_list)) {
  972. list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
  973. if (aa_rawdata_eq(rawdata_ent, udata)) {
  974. struct aa_loaddata *tmp;
  975. tmp = __aa_get_loaddata(rawdata_ent);
  976. /* check we didn't fail the race */
  977. if (tmp) {
  978. aa_put_loaddata(udata);
  979. udata = tmp;
  980. break;
  981. }
  982. }
  983. }
  984. }
  985. /* setup parent and ns info */
  986. list_for_each_entry(ent, &lh, list) {
  987. struct aa_policy *policy;
  988. struct aa_profile *p;
  989. if (aa_g_export_binary)
  990. ent->new->rawdata = aa_get_loaddata(udata);
  991. error = __lookup_replace(ns, ent->new->base.hname,
  992. !(mask & AA_MAY_REPLACE_POLICY),
  993. &ent->old, &info);
  994. if (error)
  995. goto fail_lock;
  996. if (ent->new->rename) {
  997. error = __lookup_replace(ns, ent->new->rename,
  998. !(mask & AA_MAY_REPLACE_POLICY),
  999. &ent->rename, &info);
  1000. if (error)
  1001. goto fail_lock;
  1002. }
  1003. /* released when @new is freed */
  1004. ent->new->ns = aa_get_ns(ns);
  1005. if (ent->old || ent->rename)
  1006. continue;
  1007. /* no ref on policy only use inside lock */
  1008. p = NULL;
  1009. policy = __lookup_parent(ns, ent->new->base.hname);
  1010. if (!policy) {
  1011. /* first check for parent in the load set */
  1012. p = __list_lookup_parent(&lh, ent->new);
  1013. if (!p) {
  1014. /*
  1015. * fill in missing parent with null
  1016. * profile that doesn't have
  1017. * permissions. This allows for
  1018. * individual profile loading where
  1019. * the child is loaded before the
  1020. * parent, and outside of the current
  1021. * atomic set. This unfortunately can
  1022. * happen with some userspaces. The
  1023. * null profile will be replaced once
  1024. * the parent is loaded.
  1025. */
  1026. policy = __create_missing_ancestors(ns,
  1027. ent->new->base.hname,
  1028. GFP_KERNEL);
  1029. if (!policy) {
  1030. error = -ENOENT;
  1031. info = "parent does not exist";
  1032. goto fail_lock;
  1033. }
  1034. }
  1035. }
  1036. if (!p && policy != &ns->base)
  1037. /* released on profile replacement or free_profile */
  1038. p = (struct aa_profile *) policy;
  1039. rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
  1040. }
  1041. /* create new fs entries for introspection if needed */
  1042. if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) {
  1043. error = __aa_fs_create_rawdata(ns, udata);
  1044. if (error) {
  1045. info = "failed to create raw_data dir and files";
  1046. ent = NULL;
  1047. goto fail_lock;
  1048. }
  1049. }
  1050. list_for_each_entry(ent, &lh, list) {
  1051. if (!ent->old) {
  1052. struct dentry *parent;
  1053. if (rcu_access_pointer(ent->new->parent)) {
  1054. struct aa_profile *p;
  1055. p = aa_deref_parent(ent->new);
  1056. parent = prof_child_dir(p);
  1057. } else
  1058. parent = ns_subprofs_dir(ent->new->ns);
  1059. error = __aafs_profile_mkdir(ent->new, parent);
  1060. }
  1061. if (error) {
  1062. info = "failed to create";
  1063. goto fail_lock;
  1064. }
  1065. }
  1066. /* Done with checks that may fail - do actual replacement */
  1067. __aa_bump_ns_revision(ns);
  1068. if (aa_g_export_binary)
  1069. __aa_loaddata_update(udata, ns->revision);
  1070. list_for_each_entry_safe(ent, tmp, &lh, list) {
  1071. list_del_init(&ent->list);
  1072. op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
  1073. if (ent->old && ent->old->rawdata == ent->new->rawdata &&
  1074. ent->new->rawdata) {
  1075. /* dedup actual profile replacement */
  1076. audit_policy(label, op, ns_name, ent->new->base.hname,
  1077. "same as current profile, skipping",
  1078. error);
  1079. /* break refcount cycle with proxy. */
  1080. aa_put_proxy(ent->new->label.proxy);
  1081. ent->new->label.proxy = NULL;
  1082. goto skip;
  1083. }
  1084. /*
  1085. * TODO: finer dedup based on profile range in data. Load set
  1086. * can differ but profile may remain unchanged
  1087. */
  1088. audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
  1089. error);
  1090. if (ent->old) {
  1091. share_name(ent->old, ent->new);
  1092. __replace_profile(ent->old, ent->new);
  1093. } else {
  1094. struct list_head *lh;
  1095. if (rcu_access_pointer(ent->new->parent)) {
  1096. struct aa_profile *parent;
  1097. parent = update_to_newest_parent(ent->new);
  1098. lh = &parent->base.profiles;
  1099. } else
  1100. lh = &ns->base.profiles;
  1101. __add_profile(lh, ent->new);
  1102. }
  1103. skip:
  1104. aa_load_ent_free(ent);
  1105. }
  1106. __aa_labelset_update_subtree(ns);
  1107. mutex_unlock(&ns->lock);
  1108. out:
  1109. aa_put_ns(ns);
  1110. aa_put_loaddata(udata);
  1111. kfree(ns_name);
  1112. if (error)
  1113. return error;
  1114. return udata->size;
  1115. fail_lock:
  1116. mutex_unlock(&ns->lock);
  1117. /* audit cause of failure */
  1118. op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
  1119. fail:
  1120. audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
  1121. info, error);
  1122. /* audit status that rest of profiles in the atomic set failed too */
  1123. info = "valid profile in failed atomic policy load";
  1124. list_for_each_entry(tmp, &lh, list) {
  1125. if (tmp == ent) {
  1126. info = "unchecked profile in failed atomic policy load";
  1127. /* skip entry that caused failure */
  1128. continue;
  1129. }
  1130. op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
  1131. audit_policy(label, op, ns_name, tmp->new->base.hname, info,
  1132. error);
  1133. }
  1134. list_for_each_entry_safe(ent, tmp, &lh, list) {
  1135. list_del_init(&ent->list);
  1136. aa_load_ent_free(ent);
  1137. }
  1138. goto out;
  1139. }
  1140. /**
  1141. * aa_remove_profiles - remove profile(s) from the system
  1142. * @policy_ns: namespace the remove is being done from
  1143. * @subj: label attempting to remove policy
  1144. * @fqname: name of the profile or namespace to remove (NOT NULL)
  1145. * @size: size of the name
  1146. *
  1147. * Remove a profile or sub namespace from the current namespace, so that
  1148. * they can not be found anymore and mark them as replaced by unconfined
  1149. *
  1150. * NOTE: removing confinement does not restore rlimits to preconfinement values
  1151. *
  1152. * Returns: size of data consume else error code if fails
  1153. */
  1154. ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
  1155. char *fqname, size_t size)
  1156. {
  1157. struct aa_ns *ns = NULL;
  1158. struct aa_profile *profile = NULL;
  1159. const char *name = fqname, *info = NULL;
  1160. const char *ns_name = NULL;
  1161. ssize_t error = 0;
  1162. if (*fqname == 0) {
  1163. info = "no profile specified";
  1164. error = -ENOENT;
  1165. goto fail;
  1166. }
  1167. if (fqname[0] == ':') {
  1168. size_t ns_len;
  1169. name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
  1170. /* released below */
  1171. ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
  1172. ns_name, ns_len);
  1173. if (!ns) {
  1174. info = "namespace does not exist";
  1175. error = -ENOENT;
  1176. goto fail;
  1177. }
  1178. } else
  1179. /* released below */
  1180. ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
  1181. if (!name) {
  1182. /* remove namespace - can only happen if fqname[0] == ':' */
  1183. mutex_lock_nested(&ns->parent->lock, ns->parent->level);
  1184. __aa_bump_ns_revision(ns);
  1185. __aa_remove_ns(ns);
  1186. mutex_unlock(&ns->parent->lock);
  1187. } else {
  1188. /* remove profile */
  1189. mutex_lock_nested(&ns->lock, ns->level);
  1190. profile = aa_get_profile(__lookup_profile(&ns->base, name));
  1191. if (!profile) {
  1192. error = -ENOENT;
  1193. info = "profile does not exist";
  1194. goto fail_ns_lock;
  1195. }
  1196. name = profile->base.hname;
  1197. __aa_bump_ns_revision(ns);
  1198. __remove_profile(profile);
  1199. __aa_labelset_update_subtree(ns);
  1200. mutex_unlock(&ns->lock);
  1201. }
  1202. /* don't fail removal if audit fails */
  1203. (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
  1204. error);
  1205. aa_put_ns(ns);
  1206. aa_put_profile(profile);
  1207. return size;
  1208. fail_ns_lock:
  1209. mutex_unlock(&ns->lock);
  1210. aa_put_ns(ns);
  1211. fail:
  1212. (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
  1213. error);
  1214. return error;
  1215. }