policy.c 32 KB

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