policy.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor policy definitions.
  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. #ifndef __AA_POLICY_H
  15. #define __AA_POLICY_H
  16. #include <linux/capability.h>
  17. #include <linux/cred.h>
  18. #include <linux/kref.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/socket.h>
  22. #include "apparmor.h"
  23. #include "audit.h"
  24. #include "capability.h"
  25. #include "domain.h"
  26. #include "file.h"
  27. #include "resource.h"
  28. extern const char *profile_mode_names[];
  29. #define APPARMOR_NAMES_MAX_INDEX 3
  30. #define COMPLAIN_MODE(_profile) \
  31. ((aa_g_profile_mode == APPARMOR_COMPLAIN) || \
  32. ((_profile)->mode == APPARMOR_COMPLAIN))
  33. #define KILL_MODE(_profile) \
  34. ((aa_g_profile_mode == APPARMOR_KILL) || \
  35. ((_profile)->mode == APPARMOR_KILL))
  36. #define PROFILE_IS_HAT(_profile) ((_profile)->flags & PFLAG_HAT)
  37. /*
  38. * FIXME: currently need a clean way to replace and remove profiles as a
  39. * set. It should be done at the namespace level.
  40. * Either, with a set of profiles loaded at the namespace level or via
  41. * a mark and remove marked interface.
  42. */
  43. enum profile_mode {
  44. APPARMOR_ENFORCE, /* enforce access rules */
  45. APPARMOR_COMPLAIN, /* allow and log access violations */
  46. APPARMOR_KILL, /* kill task on access violation */
  47. };
  48. enum profile_flags {
  49. PFLAG_HAT = 1, /* profile is a hat */
  50. PFLAG_UNCONFINED = 2, /* profile is an unconfined profile */
  51. PFLAG_NULL = 4, /* profile is null learning profile */
  52. PFLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */
  53. PFLAG_IMMUTABLE = 0x10, /* don't allow changes/replacement */
  54. PFLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */
  55. PFLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */
  56. PFLAG_OLD_NULL_TRANS = 0x100, /* use // as the null transition */
  57. /* These flags must correspond with PATH_flags */
  58. PFLAG_MEDIATE_DELETED = 0x10000, /* mediate instead delegate deleted */
  59. };
  60. struct aa_profile;
  61. /* struct aa_policy - common part of both namespaces and profiles
  62. * @name: name of the object
  63. * @hname - The hierarchical name
  64. * @count: reference count of the obj
  65. * @list: list policy object is on
  66. * @profiles: head of the profiles list contained in the object
  67. */
  68. struct aa_policy {
  69. char *name;
  70. char *hname;
  71. struct kref count;
  72. struct list_head list;
  73. struct list_head profiles;
  74. };
  75. /* struct aa_ns_acct - accounting of profiles in namespace
  76. * @max_size: maximum space allowed for all profiles in namespace
  77. * @max_count: maximum number of profiles that can be in this namespace
  78. * @size: current size of profiles
  79. * @count: current count of profiles (includes null profiles)
  80. */
  81. struct aa_ns_acct {
  82. int max_size;
  83. int max_count;
  84. int size;
  85. int count;
  86. };
  87. /* struct aa_namespace - namespace for a set of profiles
  88. * @base: common policy
  89. * @parent: parent of namespace
  90. * @lock: lock for modifying the object
  91. * @acct: accounting for the namespace
  92. * @unconfined: special unconfined profile for the namespace
  93. * @sub_ns: list of namespaces under the current namespace.
  94. *
  95. * An aa_namespace defines the set profiles that are searched to determine
  96. * which profile to attach to a task. Profiles can not be shared between
  97. * aa_namespaces and profile names within a namespace are guaranteed to be
  98. * unique. When profiles in separate namespaces have the same name they
  99. * are NOT considered to be equivalent.
  100. *
  101. * Namespaces are hierarchical and only namespaces and profiles below the
  102. * current namespace are visible.
  103. *
  104. * Namespace names must be unique and can not contain the characters :/\0
  105. *
  106. * FIXME TODO: add vserver support of namespaces (can it all be done in
  107. * userspace?)
  108. */
  109. struct aa_namespace {
  110. struct aa_policy base;
  111. struct aa_namespace *parent;
  112. rwlock_t lock;
  113. struct aa_ns_acct acct;
  114. struct aa_profile *unconfined;
  115. struct list_head sub_ns;
  116. };
  117. /* struct aa_profile - basic confinement data
  118. * @base - base components of the profile (name, refcount, lists, lock ...)
  119. * @parent: parent of profile
  120. * @ns: namespace the profile is in
  121. * @replacedby: is set to the profile that replaced this profile
  122. * @rename: optional profile name that this profile renamed
  123. * @xmatch: optional extended matching for unconfined executables names
  124. * @xmatch_len: xmatch prefix len, used to determine xmatch priority
  125. * @sid: the unique security id number of this profile
  126. * @audit: the auditing mode of the profile
  127. * @mode: the enforcement mode of the profile
  128. * @flags: flags controlling profile behavior
  129. * @path_flags: flags controlling path generation behavior
  130. * @size: the memory consumed by this profiles rules
  131. * @file: The set of rules governing basic file access and domain transitions
  132. * @caps: capabilities for the profile
  133. * @rlimits: rlimits for the profile
  134. *
  135. * The AppArmor profile contains the basic confinement data. Each profile
  136. * has a name, and exists in a namespace. The @name and @exec_match are
  137. * used to determine profile attachment against unconfined tasks. All other
  138. * attachments are determined by profile X transition rules.
  139. *
  140. * The @replacedby field is write protected by the profile lock. Reads
  141. * are assumed to be atomic, and are done without locking.
  142. *
  143. * Profiles have a hierarchy where hats and children profiles keep
  144. * a reference to their parent.
  145. *
  146. * Profile names can not begin with a : and can not contain the \0
  147. * character. If a profile name begins with / it will be considered when
  148. * determining profile attachment on "unconfined" tasks.
  149. */
  150. struct aa_profile {
  151. struct aa_policy base;
  152. struct aa_profile *parent;
  153. struct aa_namespace *ns;
  154. struct aa_profile *replacedby;
  155. const char *rename;
  156. struct aa_dfa *xmatch;
  157. int xmatch_len;
  158. u32 sid;
  159. enum audit_mode audit;
  160. enum profile_mode mode;
  161. u32 flags;
  162. u32 path_flags;
  163. int size;
  164. struct aa_file_rules file;
  165. struct aa_caps caps;
  166. struct aa_rlimit rlimits;
  167. };
  168. extern struct aa_namespace *root_ns;
  169. extern enum profile_mode aa_g_profile_mode;
  170. void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
  171. bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
  172. const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
  173. int aa_alloc_root_ns(void);
  174. void aa_free_root_ns(void);
  175. void aa_free_namespace_kref(struct kref *kref);
  176. struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
  177. const char *name);
  178. static inline struct aa_policy *aa_get_common(struct aa_policy *c)
  179. {
  180. if (c)
  181. kref_get(&c->count);
  182. return c;
  183. }
  184. /**
  185. * aa_get_namespace - increment references count on @ns
  186. * @ns: namespace to increment reference count of (MAYBE NULL)
  187. *
  188. * Returns: pointer to @ns, if @ns is NULL returns NULL
  189. * Requires: @ns must be held with valid refcount when called
  190. */
  191. static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
  192. {
  193. if (ns)
  194. kref_get(&(ns->base.count));
  195. return ns;
  196. }
  197. /**
  198. * aa_put_namespace - decrement refcount on @ns
  199. * @ns: namespace to put reference of
  200. *
  201. * Decrement reference count of @ns and if no longer in use free it
  202. */
  203. static inline void aa_put_namespace(struct aa_namespace *ns)
  204. {
  205. if (ns)
  206. kref_put(&ns->base.count, aa_free_namespace_kref);
  207. }
  208. struct aa_profile *aa_alloc_profile(const char *name);
  209. struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
  210. void aa_free_profile_kref(struct kref *kref);
  211. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
  212. struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
  213. struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
  214. ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
  215. ssize_t aa_remove_profiles(char *name, size_t size);
  216. #define PROF_ADD 1
  217. #define PROF_REPLACE 0
  218. #define unconfined(X) ((X)->flags & PFLAG_UNCONFINED)
  219. /**
  220. * aa_newest_version - find the newest version of @profile
  221. * @profile: the profile to check for newer versions of (NOT NULL)
  222. *
  223. * Returns: newest version of @profile, if @profile is the newest version
  224. * return @profile.
  225. *
  226. * NOTE: the profile returned is not refcounted, The refcount on @profile
  227. * must be held until the caller decides what to do with the returned newest
  228. * version.
  229. */
  230. static inline struct aa_profile *aa_newest_version(struct aa_profile *profile)
  231. {
  232. while (profile->replacedby)
  233. profile = profile->replacedby;
  234. return profile;
  235. }
  236. /**
  237. * aa_get_profile - increment refcount on profile @p
  238. * @p: profile (MAYBE NULL)
  239. *
  240. * Returns: pointer to @p if @p is NULL will return NULL
  241. * Requires: @p must be held with valid refcount when called
  242. */
  243. static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
  244. {
  245. if (p)
  246. kref_get(&(p->base.count));
  247. return p;
  248. }
  249. /**
  250. * aa_put_profile - decrement refcount on profile @p
  251. * @p: profile (MAYBE NULL)
  252. */
  253. static inline void aa_put_profile(struct aa_profile *p)
  254. {
  255. if (p)
  256. kref_put(&p->base.count, aa_free_profile_kref);
  257. }
  258. static inline int AUDIT_MODE(struct aa_profile *profile)
  259. {
  260. if (aa_g_audit != AUDIT_NORMAL)
  261. return aa_g_audit;
  262. return profile->audit;
  263. }
  264. bool aa_may_manage_policy(int op);
  265. #endif /* __AA_POLICY_H */