policy.c 32 KB

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