policy.c 35 KB

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