policy.h 9.5 KB

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