policy.c 29 KB

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