policy.c 31 KB

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