policy.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 "include/apparmor.h"
  79. #include "include/capability.h"
  80. #include "include/context.h"
  81. #include "include/file.h"
  82. #include "include/ipc.h"
  83. #include "include/match.h"
  84. #include "include/path.h"
  85. #include "include/policy.h"
  86. #include "include/policy_unpack.h"
  87. #include "include/resource.h"
  88. /* root profile namespace */
  89. struct aa_namespace *root_ns;
  90. const char *const profile_mode_names[] = {
  91. "enforce",
  92. "complain",
  93. "kill",
  94. };
  95. /**
  96. * hname_tail - find the last component of an hname
  97. * @name: hname to find the base profile name component of (NOT NULL)
  98. *
  99. * Returns: the tail (base profile name) name component of an hname
  100. */
  101. static const char *hname_tail(const char *hname)
  102. {
  103. char *split;
  104. hname = strim((char *)hname);
  105. for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
  106. hname = split + 2;
  107. return hname;
  108. }
  109. /**
  110. * policy_init - initialize a policy structure
  111. * @policy: policy to initialize (NOT NULL)
  112. * @prefix: prefix name if any is required. (MAYBE NULL)
  113. * @name: name of the policy, init will make a copy of it (NOT NULL)
  114. *
  115. * Note: this fn creates a copy of strings passed in
  116. *
  117. * Returns: true if policy init successful
  118. */
  119. static bool policy_init(struct aa_policy *policy, const char *prefix,
  120. const char *name)
  121. {
  122. /* freed by policy_free */
  123. if (prefix) {
  124. policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
  125. GFP_KERNEL);
  126. if (policy->hname)
  127. sprintf(policy->hname, "%s//%s", prefix, name);
  128. } else
  129. policy->hname = kstrdup(name, GFP_KERNEL);
  130. if (!policy->hname)
  131. return 0;
  132. /* base.name is a substring of fqname */
  133. policy->name = (char *)hname_tail(policy->hname);
  134. INIT_LIST_HEAD(&policy->list);
  135. INIT_LIST_HEAD(&policy->profiles);
  136. return 1;
  137. }
  138. /**
  139. * policy_destroy - free the elements referenced by @policy
  140. * @policy: policy that is to have its elements freed (NOT NULL)
  141. */
  142. static void policy_destroy(struct aa_policy *policy)
  143. {
  144. /* still contains profiles -- invalid */
  145. if (on_list_rcu(&policy->profiles)) {
  146. AA_ERROR("%s: internal error, "
  147. "policy '%s' still contains profiles\n",
  148. __func__, policy->name);
  149. BUG();
  150. }
  151. if (on_list_rcu(&policy->list)) {
  152. AA_ERROR("%s: internal error, policy '%s' still on list\n",
  153. __func__, policy->name);
  154. BUG();
  155. }
  156. /* don't free name as its a subset of hname */
  157. kzfree(policy->hname);
  158. }
  159. /**
  160. * __policy_find - find a policy by @name on a policy list
  161. * @head: list to search (NOT NULL)
  162. * @name: name to search for (NOT NULL)
  163. *
  164. * Requires: rcu_read_lock be held
  165. *
  166. * Returns: unrefcounted policy that match @name or NULL if not found
  167. */
  168. static struct aa_policy *__policy_find(struct list_head *head, const char *name)
  169. {
  170. struct aa_policy *policy;
  171. list_for_each_entry_rcu(policy, head, list) {
  172. if (!strcmp(policy->name, name))
  173. return policy;
  174. }
  175. return NULL;
  176. }
  177. /**
  178. * __policy_strn_find - find a policy that's name matches @len chars of @str
  179. * @head: list to search (NOT NULL)
  180. * @str: string to search for (NOT NULL)
  181. * @len: length of match required
  182. *
  183. * Requires: rcu_read_lock be held
  184. *
  185. * Returns: unrefcounted policy that match @str or NULL if not found
  186. *
  187. * if @len == strlen(@strlen) then this is equiv to __policy_find
  188. * other wise it allows searching for policy by a partial match of name
  189. */
  190. static struct aa_policy *__policy_strn_find(struct list_head *head,
  191. const char *str, int len)
  192. {
  193. struct aa_policy *policy;
  194. list_for_each_entry_rcu(policy, head, list) {
  195. if (aa_strneq(policy->name, str, len))
  196. return policy;
  197. }
  198. return NULL;
  199. }
  200. /*
  201. * Routines for AppArmor namespaces
  202. */
  203. static const char *hidden_ns_name = "---";
  204. /**
  205. * aa_ns_visible - test if @view is visible from @curr
  206. * @curr: namespace to treat as the parent (NOT NULL)
  207. * @view: namespace to test if visible from @curr (NOT NULL)
  208. *
  209. * Returns: true if @view is visible from @curr else false
  210. */
  211. bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view)
  212. {
  213. if (curr == view)
  214. return true;
  215. for ( ; view; view = view->parent) {
  216. if (view->parent == curr)
  217. return true;
  218. }
  219. return false;
  220. }
  221. /**
  222. * aa_na_name - Find the ns name to display for @view from @curr
  223. * @curr - current namespace (NOT NULL)
  224. * @view - namespace attempting to view (NOT NULL)
  225. *
  226. * Returns: name of @view visible from @curr
  227. */
  228. const char *aa_ns_name(struct aa_namespace *curr, struct aa_namespace *view)
  229. {
  230. /* if view == curr then the namespace name isn't displayed */
  231. if (curr == view)
  232. return "";
  233. if (aa_ns_visible(curr, view)) {
  234. /* at this point if a ns is visible it is in a view ns
  235. * thus the curr ns.hname is a prefix of its name.
  236. * Only output the virtualized portion of the name
  237. * Add + 2 to skip over // separating curr hname prefix
  238. * from the visible tail of the views hname
  239. */
  240. return view->base.hname + strlen(curr->base.hname) + 2;
  241. } else
  242. return hidden_ns_name;
  243. }
  244. /**
  245. * alloc_namespace - allocate, initialize and return a new namespace
  246. * @prefix: parent namespace name (MAYBE NULL)
  247. * @name: a preallocated name (NOT NULL)
  248. *
  249. * Returns: refcounted namespace or NULL on failure.
  250. */
  251. static struct aa_namespace *alloc_namespace(const char *prefix,
  252. const char *name)
  253. {
  254. struct aa_namespace *ns;
  255. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  256. AA_DEBUG("%s(%p)\n", __func__, ns);
  257. if (!ns)
  258. return NULL;
  259. if (!policy_init(&ns->base, prefix, name))
  260. goto fail_ns;
  261. INIT_LIST_HEAD(&ns->sub_ns);
  262. mutex_init(&ns->lock);
  263. /* released by free_namespace */
  264. ns->unconfined = aa_alloc_profile("unconfined");
  265. if (!ns->unconfined)
  266. goto fail_unconfined;
  267. ns->unconfined->flags = PFLAG_UNCONFINED | PFLAG_IX_ON_NAME_ERROR |
  268. PFLAG_IMMUTABLE | PFLAG_NS_COUNT;
  269. /* ns and ns->unconfined share ns->unconfined refcount */
  270. ns->unconfined->ns = ns;
  271. atomic_set(&ns->uniq_null, 0);
  272. return ns;
  273. fail_unconfined:
  274. kzfree(ns->base.hname);
  275. fail_ns:
  276. kzfree(ns);
  277. return NULL;
  278. }
  279. /**
  280. * free_namespace - free a profile namespace
  281. * @ns: the namespace to free (MAYBE NULL)
  282. *
  283. * Requires: All references to the namespace must have been put, if the
  284. * namespace was referenced by a profile confining a task,
  285. */
  286. static void free_namespace(struct aa_namespace *ns)
  287. {
  288. if (!ns)
  289. return;
  290. policy_destroy(&ns->base);
  291. aa_put_namespace(ns->parent);
  292. ns->unconfined->ns = NULL;
  293. aa_free_profile(ns->unconfined);
  294. kzfree(ns);
  295. }
  296. /**
  297. * __aa_find_namespace - find a namespace on a list by @name
  298. * @head: list to search for namespace on (NOT NULL)
  299. * @name: name of namespace to look for (NOT NULL)
  300. *
  301. * Returns: unrefcounted namespace
  302. *
  303. * Requires: rcu_read_lock be held
  304. */
  305. static struct aa_namespace *__aa_find_namespace(struct list_head *head,
  306. const char *name)
  307. {
  308. return (struct aa_namespace *)__policy_find(head, name);
  309. }
  310. /**
  311. * aa_find_namespace - look up a profile namespace on the namespace list
  312. * @root: namespace to search in (NOT NULL)
  313. * @name: name of namespace to find (NOT NULL)
  314. *
  315. * Returns: a refcounted namespace on the list, or NULL if no namespace
  316. * called @name exists.
  317. *
  318. * refcount released by caller
  319. */
  320. struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
  321. const char *name)
  322. {
  323. struct aa_namespace *ns = NULL;
  324. rcu_read_lock();
  325. ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name));
  326. rcu_read_unlock();
  327. return ns;
  328. }
  329. /**
  330. * aa_prepare_namespace - find an existing or create a new namespace of @name
  331. * @name: the namespace to find or add (MAYBE NULL)
  332. *
  333. * Returns: refcounted namespace or NULL if failed to create one
  334. */
  335. static struct aa_namespace *aa_prepare_namespace(const char *name)
  336. {
  337. struct aa_namespace *ns, *root;
  338. root = aa_current_profile()->ns;
  339. mutex_lock(&root->lock);
  340. /* if name isn't specified the profile is loaded to the current ns */
  341. if (!name) {
  342. /* released by caller */
  343. ns = aa_get_namespace(root);
  344. goto out;
  345. }
  346. /* try and find the specified ns and if it doesn't exist create it */
  347. /* released by caller */
  348. ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name));
  349. if (!ns) {
  350. ns = alloc_namespace(root->base.hname, name);
  351. if (!ns)
  352. goto out;
  353. /* add parent ref */
  354. ns->parent = aa_get_namespace(root);
  355. list_add_rcu(&ns->base.list, &root->sub_ns);
  356. /* add list ref */
  357. aa_get_namespace(ns);
  358. }
  359. out:
  360. mutex_unlock(&root->lock);
  361. /* return ref */
  362. return ns;
  363. }
  364. /**
  365. * __list_add_profile - add a profile to a list
  366. * @list: list to add it to (NOT NULL)
  367. * @profile: the profile to add (NOT NULL)
  368. *
  369. * refcount @profile, should be put by __list_remove_profile
  370. *
  371. * Requires: namespace lock be held, or list not be shared
  372. */
  373. static void __list_add_profile(struct list_head *list,
  374. struct aa_profile *profile)
  375. {
  376. list_add_rcu(&profile->base.list, list);
  377. /* get list reference */
  378. aa_get_profile(profile);
  379. }
  380. /**
  381. * __list_remove_profile - remove a profile from the list it is on
  382. * @profile: the profile to remove (NOT NULL)
  383. *
  384. * remove a profile from the list, warning generally removal should
  385. * be done with __replace_profile as most profile removals are
  386. * replacements to the unconfined profile.
  387. *
  388. * put @profile list refcount
  389. *
  390. * Requires: namespace lock be held, or list not have been live
  391. */
  392. static void __list_remove_profile(struct aa_profile *profile)
  393. {
  394. list_del_rcu(&profile->base.list);
  395. aa_put_profile(profile);
  396. }
  397. static void __profile_list_release(struct list_head *head);
  398. /**
  399. * __remove_profile - remove old profile, and children
  400. * @profile: profile to be replaced (NOT NULL)
  401. *
  402. * Requires: namespace list lock be held, or list not be shared
  403. */
  404. static void __remove_profile(struct aa_profile *profile)
  405. {
  406. /* release any children lists first */
  407. __profile_list_release(&profile->base.profiles);
  408. /* released by free_profile */
  409. __aa_update_replacedby(profile, profile->ns->unconfined);
  410. __list_remove_profile(profile);
  411. }
  412. /**
  413. * __profile_list_release - remove all profiles on the list and put refs
  414. * @head: list of profiles (NOT NULL)
  415. *
  416. * Requires: namespace lock be held
  417. */
  418. static void __profile_list_release(struct list_head *head)
  419. {
  420. struct aa_profile *profile, *tmp;
  421. list_for_each_entry_safe(profile, tmp, head, base.list)
  422. __remove_profile(profile);
  423. }
  424. static void __ns_list_release(struct list_head *head);
  425. /**
  426. * destroy_namespace - remove everything contained by @ns
  427. * @ns: namespace to have it contents removed (NOT NULL)
  428. */
  429. static void destroy_namespace(struct aa_namespace *ns)
  430. {
  431. if (!ns)
  432. return;
  433. mutex_lock(&ns->lock);
  434. /* release all profiles in this namespace */
  435. __profile_list_release(&ns->base.profiles);
  436. /* release all sub namespaces */
  437. __ns_list_release(&ns->sub_ns);
  438. if (ns->parent)
  439. __aa_update_replacedby(ns->unconfined, ns->parent->unconfined);
  440. mutex_unlock(&ns->lock);
  441. }
  442. /**
  443. * __remove_namespace - remove a namespace and all its children
  444. * @ns: namespace to be removed (NOT NULL)
  445. *
  446. * Requires: ns->parent->lock be held and ns removed from parent.
  447. */
  448. static void __remove_namespace(struct aa_namespace *ns)
  449. {
  450. /* remove ns from namespace list */
  451. list_del_rcu(&ns->base.list);
  452. destroy_namespace(ns);
  453. aa_put_namespace(ns);
  454. }
  455. /**
  456. * __ns_list_release - remove all profile namespaces on the list put refs
  457. * @head: list of profile namespaces (NOT NULL)
  458. *
  459. * Requires: namespace lock be held
  460. */
  461. static void __ns_list_release(struct list_head *head)
  462. {
  463. struct aa_namespace *ns, *tmp;
  464. list_for_each_entry_safe(ns, tmp, head, base.list)
  465. __remove_namespace(ns);
  466. }
  467. /**
  468. * aa_alloc_root_ns - allocate the root profile namespace
  469. *
  470. * Returns: %0 on success else error
  471. *
  472. */
  473. int __init aa_alloc_root_ns(void)
  474. {
  475. /* released by aa_free_root_ns - used as list ref*/
  476. root_ns = alloc_namespace(NULL, "root");
  477. if (!root_ns)
  478. return -ENOMEM;
  479. return 0;
  480. }
  481. /**
  482. * aa_free_root_ns - free the root profile namespace
  483. */
  484. void __init aa_free_root_ns(void)
  485. {
  486. struct aa_namespace *ns = root_ns;
  487. root_ns = NULL;
  488. destroy_namespace(ns);
  489. aa_put_namespace(ns);
  490. }
  491. static void free_replacedby(struct aa_replacedby *r)
  492. {
  493. if (r) {
  494. aa_put_profile(rcu_dereference(r->profile));
  495. kzfree(r);
  496. }
  497. }
  498. void aa_free_replacedby_kref(struct kref *kref)
  499. {
  500. struct aa_replacedby *r = container_of(kref, struct aa_replacedby,
  501. count);
  502. free_replacedby(r);
  503. }
  504. /**
  505. * aa_free_profile - free a profile
  506. * @profile: the profile to free (MAYBE NULL)
  507. *
  508. * Free a profile, its hats and null_profile. All references to the profile,
  509. * its hats and null_profile must have been put.
  510. *
  511. * If the profile was referenced from a task context, free_profile() will
  512. * be called from an rcu callback routine, so we must not sleep here.
  513. */
  514. void aa_free_profile(struct aa_profile *profile)
  515. {
  516. AA_DEBUG("%s(%p)\n", __func__, profile);
  517. if (!profile)
  518. return;
  519. /* free children profiles */
  520. policy_destroy(&profile->base);
  521. aa_put_profile(rcu_access_pointer(profile->parent));
  522. aa_put_namespace(profile->ns);
  523. kzfree(profile->rename);
  524. aa_free_file_rules(&profile->file);
  525. aa_free_cap_rules(&profile->caps);
  526. aa_free_rlimit_rules(&profile->rlimits);
  527. aa_put_dfa(profile->xmatch);
  528. aa_put_dfa(profile->policy.dfa);
  529. aa_put_replacedby(profile->replacedby);
  530. kzfree(profile);
  531. }
  532. /**
  533. * aa_free_profile_rcu - free aa_profile by rcu (called by aa_free_profile_kref)
  534. * @head: rcu_head callback for freeing of a profile (NOT NULL)
  535. */
  536. static void aa_free_profile_rcu(struct rcu_head *head)
  537. {
  538. struct aa_profile *p = container_of(head, struct aa_profile, rcu);
  539. if (p->flags & PFLAG_NS_COUNT)
  540. free_namespace(p->ns);
  541. else
  542. aa_free_profile(p);
  543. }
  544. /**
  545. * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile)
  546. * @kr: kref callback for freeing of a profile (NOT NULL)
  547. */
  548. void aa_free_profile_kref(struct kref *kref)
  549. {
  550. struct aa_profile *p = container_of(kref, struct aa_profile, count);
  551. call_rcu(&p->rcu, aa_free_profile_rcu);
  552. }
  553. /**
  554. * aa_alloc_profile - allocate, initialize and return a new profile
  555. * @hname: name of the profile (NOT NULL)
  556. *
  557. * Returns: refcount profile or NULL on failure
  558. */
  559. struct aa_profile *aa_alloc_profile(const char *hname)
  560. {
  561. struct aa_profile *profile;
  562. /* freed by free_profile - usually through aa_put_profile */
  563. profile = kzalloc(sizeof(*profile), GFP_KERNEL);
  564. if (!profile)
  565. return NULL;
  566. profile->replacedby = kzalloc(sizeof(struct aa_replacedby), GFP_KERNEL);
  567. if (!profile->replacedby)
  568. goto fail;
  569. kref_init(&profile->replacedby->count);
  570. if (!policy_init(&profile->base, NULL, hname))
  571. goto fail;
  572. kref_init(&profile->count);
  573. /* refcount released by caller */
  574. return profile;
  575. fail:
  576. kzfree(profile->replacedby);
  577. kzfree(profile);
  578. return NULL;
  579. }
  580. /**
  581. * aa_new_null_profile - create a new null-X learning profile
  582. * @parent: profile that caused this profile to be created (NOT NULL)
  583. * @hat: true if the null- learning profile is a hat
  584. *
  585. * Create a null- complain mode profile used in learning mode. The name of
  586. * the profile is unique and follows the format of parent//null-<uniq>.
  587. *
  588. * null profiles are added to the profile list but the list does not
  589. * hold a count on them so that they are automatically released when
  590. * not in use.
  591. *
  592. * Returns: new refcounted profile else NULL on failure
  593. */
  594. struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat)
  595. {
  596. struct aa_profile *profile = NULL;
  597. char *name;
  598. int uniq = atomic_inc_return(&parent->ns->uniq_null);
  599. /* freed below */
  600. name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, GFP_KERNEL);
  601. if (!name)
  602. goto fail;
  603. sprintf(name, "%s//null-%x", parent->base.hname, uniq);
  604. profile = aa_alloc_profile(name);
  605. kfree(name);
  606. if (!profile)
  607. goto fail;
  608. profile->mode = APPARMOR_COMPLAIN;
  609. profile->flags = PFLAG_NULL;
  610. if (hat)
  611. profile->flags |= PFLAG_HAT;
  612. /* released on free_profile */
  613. rcu_assign_pointer(profile->parent, aa_get_profile(parent));
  614. profile->ns = aa_get_namespace(parent->ns);
  615. mutex_lock(&profile->ns->lock);
  616. __list_add_profile(&parent->base.profiles, profile);
  617. mutex_unlock(&profile->ns->lock);
  618. /* refcount released by caller */
  619. return profile;
  620. fail:
  621. return NULL;
  622. }
  623. /* TODO: profile accounting - setup in remove */
  624. /**
  625. * __find_child - find a profile on @head list with a name matching @name
  626. * @head: list to search (NOT NULL)
  627. * @name: name of profile (NOT NULL)
  628. *
  629. * Requires: rcu_read_lock be held
  630. *
  631. * Returns: unrefcounted profile ptr, or NULL if not found
  632. */
  633. static struct aa_profile *__find_child(struct list_head *head, const char *name)
  634. {
  635. return (struct aa_profile *)__policy_find(head, name);
  636. }
  637. /**
  638. * __strn_find_child - find a profile on @head list using substring of @name
  639. * @head: list to search (NOT NULL)
  640. * @name: name of profile (NOT NULL)
  641. * @len: length of @name substring to match
  642. *
  643. * Requires: rcu_read_lock be held
  644. *
  645. * Returns: unrefcounted profile ptr, or NULL if not found
  646. */
  647. static struct aa_profile *__strn_find_child(struct list_head *head,
  648. const char *name, int len)
  649. {
  650. return (struct aa_profile *)__policy_strn_find(head, name, len);
  651. }
  652. /**
  653. * aa_find_child - find a profile by @name in @parent
  654. * @parent: profile to search (NOT NULL)
  655. * @name: profile name to search for (NOT NULL)
  656. *
  657. * Returns: a refcounted profile or NULL if not found
  658. */
  659. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
  660. {
  661. struct aa_profile *profile;
  662. rcu_read_lock();
  663. profile = aa_get_profile(__find_child(&parent->base.profiles, name));
  664. rcu_read_unlock();
  665. /* refcount released by caller */
  666. return profile;
  667. }
  668. /**
  669. * __lookup_parent - lookup the parent of a profile of name @hname
  670. * @ns: namespace to lookup profile in (NOT NULL)
  671. * @hname: hierarchical profile name to find parent of (NOT NULL)
  672. *
  673. * Lookups up the parent of a fully qualified profile name, the profile
  674. * that matches hname does not need to exist, in general this
  675. * is used to load a new profile.
  676. *
  677. * Requires: rcu_read_lock be held
  678. *
  679. * Returns: unrefcounted policy or NULL if not found
  680. */
  681. static struct aa_policy *__lookup_parent(struct aa_namespace *ns,
  682. const char *hname)
  683. {
  684. struct aa_policy *policy;
  685. struct aa_profile *profile = NULL;
  686. char *split;
  687. policy = &ns->base;
  688. for (split = strstr(hname, "//"); split;) {
  689. profile = __strn_find_child(&policy->profiles, hname,
  690. split - hname);
  691. if (!profile)
  692. return NULL;
  693. policy = &profile->base;
  694. hname = split + 2;
  695. split = strstr(hname, "//");
  696. }
  697. if (!profile)
  698. return &ns->base;
  699. return &profile->base;
  700. }
  701. /**
  702. * __lookup_profile - lookup the profile matching @hname
  703. * @base: base list to start looking up profile name from (NOT NULL)
  704. * @hname: hierarchical profile name (NOT NULL)
  705. *
  706. * Requires: rcu_read_lock be held
  707. *
  708. * Returns: unrefcounted profile pointer or NULL if not found
  709. *
  710. * Do a relative name lookup, recursing through profile tree.
  711. */
  712. static struct aa_profile *__lookup_profile(struct aa_policy *base,
  713. const char *hname)
  714. {
  715. struct aa_profile *profile = NULL;
  716. char *split;
  717. for (split = strstr(hname, "//"); split;) {
  718. profile = __strn_find_child(&base->profiles, hname,
  719. split - hname);
  720. if (!profile)
  721. return NULL;
  722. base = &profile->base;
  723. hname = split + 2;
  724. split = strstr(hname, "//");
  725. }
  726. profile = __find_child(&base->profiles, hname);
  727. return profile;
  728. }
  729. /**
  730. * aa_lookup_profile - find a profile by its full or partial name
  731. * @ns: the namespace to start from (NOT NULL)
  732. * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
  733. *
  734. * Returns: refcounted profile or NULL if not found
  735. */
  736. struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *hname)
  737. {
  738. struct aa_profile *profile;
  739. rcu_read_lock();
  740. do {
  741. profile = __lookup_profile(&ns->base, hname);
  742. } while (profile && !aa_get_profile_not0(profile));
  743. rcu_read_unlock();
  744. /* the unconfined profile is not in the regular profile list */
  745. if (!profile && strcmp(hname, "unconfined") == 0)
  746. profile = aa_get_newest_profile(ns->unconfined);
  747. /* refcount released by caller */
  748. return profile;
  749. }
  750. /**
  751. * replacement_allowed - test to see if replacement is allowed
  752. * @profile: profile to test if it can be replaced (MAYBE NULL)
  753. * @noreplace: true if replacement shouldn't be allowed but addition is okay
  754. * @info: Returns - info about why replacement failed (NOT NULL)
  755. *
  756. * Returns: %0 if replacement allowed else error code
  757. */
  758. static int replacement_allowed(struct aa_profile *profile, int noreplace,
  759. const char **info)
  760. {
  761. if (profile) {
  762. if (profile->flags & PFLAG_IMMUTABLE) {
  763. *info = "cannot replace immutible profile";
  764. return -EPERM;
  765. } else if (noreplace) {
  766. *info = "profile already exists";
  767. return -EEXIST;
  768. }
  769. }
  770. return 0;
  771. }
  772. /**
  773. * aa_audit_policy - Do auditing of policy changes
  774. * @op: policy operation being performed
  775. * @gfp: memory allocation flags
  776. * @name: name of profile being manipulated (NOT NULL)
  777. * @info: any extra information to be audited (MAYBE NULL)
  778. * @error: error code
  779. *
  780. * Returns: the error to be returned after audit is done
  781. */
  782. static int audit_policy(int op, gfp_t gfp, const char *name, const char *info,
  783. int error)
  784. {
  785. struct common_audit_data sa;
  786. struct apparmor_audit_data aad = {0,};
  787. sa.type = LSM_AUDIT_DATA_NONE;
  788. sa.aad = &aad;
  789. aad.op = op;
  790. aad.name = name;
  791. aad.info = info;
  792. aad.error = error;
  793. return aa_audit(AUDIT_APPARMOR_STATUS, __aa_current_profile(), gfp,
  794. &sa, NULL);
  795. }
  796. /**
  797. * aa_may_manage_policy - can the current task manage policy
  798. * @op: the policy manipulation operation being done
  799. *
  800. * Returns: true if the task is allowed to manipulate policy
  801. */
  802. bool aa_may_manage_policy(int op)
  803. {
  804. /* check if loading policy is locked out */
  805. if (aa_g_lock_policy) {
  806. audit_policy(op, GFP_KERNEL, NULL, "policy_locked", -EACCES);
  807. return 0;
  808. }
  809. if (!capable(CAP_MAC_ADMIN)) {
  810. audit_policy(op, GFP_KERNEL, NULL, "not policy admin", -EACCES);
  811. return 0;
  812. }
  813. return 1;
  814. }
  815. static struct aa_profile *__list_lookup_parent(struct list_head *lh,
  816. struct aa_profile *profile)
  817. {
  818. const char *base = hname_tail(profile->base.hname);
  819. long len = base - profile->base.hname;
  820. struct aa_load_ent *ent;
  821. /* parent won't have trailing // so remove from len */
  822. if (len <= 2)
  823. return NULL;
  824. len -= 2;
  825. list_for_each_entry(ent, lh, list) {
  826. if (ent->new == profile)
  827. continue;
  828. if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
  829. 0 && ent->new->base.hname[len] == 0)
  830. return ent->new;
  831. }
  832. return NULL;
  833. }
  834. /**
  835. * __replace_profile - replace @old with @new on a list
  836. * @old: profile to be replaced (NOT NULL)
  837. * @new: profile to replace @old with (NOT NULL)
  838. * @share_replacedby: transfer @old->replacedby to @new
  839. *
  840. * Will duplicate and refcount elements that @new inherits from @old
  841. * and will inherit @old children.
  842. *
  843. * refcount @new for list, put @old list refcount
  844. *
  845. * Requires: namespace list lock be held, or list not be shared
  846. */
  847. static void __replace_profile(struct aa_profile *old, struct aa_profile *new,
  848. bool share_replacedby)
  849. {
  850. struct aa_profile *child, *tmp;
  851. if (!list_empty(&old->base.profiles)) {
  852. LIST_HEAD(lh);
  853. list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
  854. list_for_each_entry_safe(child, tmp, &lh, base.list) {
  855. struct aa_profile *p;
  856. list_del_init(&child->base.list);
  857. p = __find_child(&new->base.profiles, child->base.name);
  858. if (p) {
  859. /* @p replaces @child */
  860. __replace_profile(child, p, share_replacedby);
  861. continue;
  862. }
  863. /* inherit @child and its children */
  864. /* TODO: update hname of inherited children */
  865. /* list refcount transferred to @new */
  866. p = rcu_dereference_protected(child->parent,
  867. mutex_is_locked(&child->ns->lock));
  868. rcu_assign_pointer(child->parent, aa_get_profile(new));
  869. list_add_rcu(&child->base.list, &new->base.profiles);
  870. aa_put_profile(p);
  871. }
  872. }
  873. if (!rcu_access_pointer(new->parent)) {
  874. struct aa_profile *parent = rcu_dereference(old->parent);
  875. rcu_assign_pointer(new->parent, aa_get_profile(parent));
  876. }
  877. __aa_update_replacedby(old, new);
  878. if (share_replacedby) {
  879. aa_put_replacedby(new->replacedby);
  880. new->replacedby = aa_get_replacedby(old->replacedby);
  881. }
  882. if (list_empty(&new->base.list)) {
  883. /* new is not on a list already */
  884. list_replace_rcu(&old->base.list, &new->base.list);
  885. aa_get_profile(new);
  886. aa_put_profile(old);
  887. } else
  888. __list_remove_profile(old);
  889. }
  890. /**
  891. * __lookup_replace - lookup replacement information for a profile
  892. * @ns - namespace the lookup occurs in
  893. * @hname - name of profile to lookup
  894. * @noreplace - true if not replacing an existing profile
  895. * @p - Returns: profile to be replaced
  896. * @info - Returns: info string on why lookup failed
  897. *
  898. * Returns: profile to replace (no ref) on success else ptr error
  899. */
  900. static int __lookup_replace(struct aa_namespace *ns, const char *hname,
  901. bool noreplace, struct aa_profile **p,
  902. const char **info)
  903. {
  904. *p = aa_get_profile(__lookup_profile(&ns->base, hname));
  905. if (*p) {
  906. int error = replacement_allowed(*p, noreplace, info);
  907. if (error) {
  908. *info = "profile can not be replaced";
  909. return error;
  910. }
  911. }
  912. return 0;
  913. }
  914. /**
  915. * aa_replace_profiles - replace profile(s) on the profile list
  916. * @udata: serialized data stream (NOT NULL)
  917. * @size: size of the serialized data stream
  918. * @noreplace: true if only doing addition, no replacement allowed
  919. *
  920. * unpack and replace a profile on the profile list and uses of that profile
  921. * by any aa_task_cxt. If the profile does not exist on the profile list
  922. * it is added.
  923. *
  924. * Returns: size of data consumed else error code on failure.
  925. */
  926. ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace)
  927. {
  928. const char *ns_name, *name = NULL, *info = NULL;
  929. struct aa_namespace *ns = NULL;
  930. struct aa_load_ent *ent, *tmp;
  931. int op = OP_PROF_REPL;
  932. ssize_t error;
  933. LIST_HEAD(lh);
  934. /* released below */
  935. error = aa_unpack(udata, size, &lh, &ns_name);
  936. if (error)
  937. goto out;
  938. /* released below */
  939. ns = aa_prepare_namespace(ns_name);
  940. if (!ns) {
  941. info = "failed to prepare namespace";
  942. error = -ENOMEM;
  943. name = ns_name;
  944. goto fail;
  945. }
  946. mutex_lock(&ns->lock);
  947. /* setup parent and ns info */
  948. list_for_each_entry(ent, &lh, list) {
  949. struct aa_policy *policy;
  950. name = ent->new->base.hname;
  951. error = __lookup_replace(ns, ent->new->base.hname, noreplace,
  952. &ent->old, &info);
  953. if (error)
  954. goto fail_lock;
  955. if (ent->new->rename) {
  956. error = __lookup_replace(ns, ent->new->rename,
  957. noreplace, &ent->rename,
  958. &info);
  959. if (error)
  960. goto fail_lock;
  961. }
  962. /* released when @new is freed */
  963. ent->new->ns = aa_get_namespace(ns);
  964. if (ent->old || ent->rename)
  965. continue;
  966. /* no ref on policy only use inside lock */
  967. policy = __lookup_parent(ns, ent->new->base.hname);
  968. if (!policy) {
  969. struct aa_profile *p;
  970. p = __list_lookup_parent(&lh, ent->new);
  971. if (!p) {
  972. error = -ENOENT;
  973. info = "parent does not exist";
  974. name = ent->new->base.hname;
  975. goto fail_lock;
  976. }
  977. rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
  978. } else if (policy != &ns->base) {
  979. /* released on profile replacement or free_profile */
  980. struct aa_profile *p = (struct aa_profile *) policy;
  981. rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
  982. }
  983. }
  984. /* do actual replacement */
  985. list_for_each_entry_safe(ent, tmp, &lh, list) {
  986. list_del_init(&ent->list);
  987. op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
  988. audit_policy(op, GFP_ATOMIC, ent->new->base.name, NULL, error);
  989. if (ent->old) {
  990. __replace_profile(ent->old, ent->new, 1);
  991. if (ent->rename)
  992. __replace_profile(ent->rename, ent->new, 0);
  993. } else if (ent->rename) {
  994. __replace_profile(ent->rename, ent->new, 0);
  995. } else if (ent->new->parent) {
  996. struct aa_profile *parent, *newest;
  997. parent = rcu_dereference_protected(ent->new->parent,
  998. mutex_is_locked(&ns->lock));
  999. newest = aa_get_newest_profile(parent);
  1000. /* parent replaced in this atomic set? */
  1001. if (newest != parent) {
  1002. aa_get_profile(newest);
  1003. aa_put_profile(parent);
  1004. rcu_assign_pointer(ent->new->parent, newest);
  1005. } else
  1006. aa_put_profile(newest);
  1007. __list_add_profile(&parent->base.profiles, ent->new);
  1008. } else
  1009. __list_add_profile(&ns->base.profiles, ent->new);
  1010. aa_load_ent_free(ent);
  1011. }
  1012. mutex_unlock(&ns->lock);
  1013. out:
  1014. aa_put_namespace(ns);
  1015. if (error)
  1016. return error;
  1017. return size;
  1018. fail_lock:
  1019. mutex_unlock(&ns->lock);
  1020. fail:
  1021. error = audit_policy(op, GFP_KERNEL, name, info, error);
  1022. list_for_each_entry_safe(ent, tmp, &lh, list) {
  1023. list_del_init(&ent->list);
  1024. aa_load_ent_free(ent);
  1025. }
  1026. goto out;
  1027. }
  1028. /**
  1029. * aa_remove_profiles - remove profile(s) from the system
  1030. * @fqname: name of the profile or namespace to remove (NOT NULL)
  1031. * @size: size of the name
  1032. *
  1033. * Remove a profile or sub namespace from the current namespace, so that
  1034. * they can not be found anymore and mark them as replaced by unconfined
  1035. *
  1036. * NOTE: removing confinement does not restore rlimits to preconfinemnet values
  1037. *
  1038. * Returns: size of data consume else error code if fails
  1039. */
  1040. ssize_t aa_remove_profiles(char *fqname, size_t size)
  1041. {
  1042. struct aa_namespace *root, *ns = NULL;
  1043. struct aa_profile *profile = NULL;
  1044. const char *name = fqname, *info = NULL;
  1045. ssize_t error = 0;
  1046. if (*fqname == 0) {
  1047. info = "no profile specified";
  1048. error = -ENOENT;
  1049. goto fail;
  1050. }
  1051. root = aa_current_profile()->ns;
  1052. if (fqname[0] == ':') {
  1053. char *ns_name;
  1054. name = aa_split_fqname(fqname, &ns_name);
  1055. /* released below */
  1056. ns = aa_find_namespace(root, ns_name);
  1057. if (!ns) {
  1058. info = "namespace does not exist";
  1059. error = -ENOENT;
  1060. goto fail;
  1061. }
  1062. } else
  1063. /* released below */
  1064. ns = aa_get_namespace(root);
  1065. if (!name) {
  1066. /* remove namespace - can only happen if fqname[0] == ':' */
  1067. mutex_lock(&ns->parent->lock);
  1068. __remove_namespace(ns);
  1069. mutex_unlock(&ns->parent->lock);
  1070. } else {
  1071. /* remove profile */
  1072. mutex_lock(&ns->lock);
  1073. profile = aa_get_profile(__lookup_profile(&ns->base, name));
  1074. if (!profile) {
  1075. error = -ENOENT;
  1076. info = "profile does not exist";
  1077. goto fail_ns_lock;
  1078. }
  1079. name = profile->base.hname;
  1080. __remove_profile(profile);
  1081. mutex_unlock(&ns->lock);
  1082. }
  1083. /* don't fail removal if audit fails */
  1084. (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
  1085. aa_put_namespace(ns);
  1086. aa_put_profile(profile);
  1087. return size;
  1088. fail_ns_lock:
  1089. mutex_unlock(&ns->lock);
  1090. aa_put_namespace(ns);
  1091. fail:
  1092. (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
  1093. return error;
  1094. }