policy.c 31 KB

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