policy.c 34 KB

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