policy.c 32 KB

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