policy.c 30 KB

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