policy.c 33 KB

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