policy.c 32 KB

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