policy.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 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_policydb - match engine for a policy
  118. * dfa: dfa pattern match
  119. * start: set of start states for the different classes of data
  120. */
  121. struct aa_policydb {
  122. /* Generic policy DFA specific rule types will be subsections of it */
  123. struct aa_dfa *dfa;
  124. unsigned int start[AA_CLASS_LAST + 1];
  125. };
  126. /* struct aa_profile - basic confinement data
  127. * @base - base components of the profile (name, refcount, lists, lock ...)
  128. * @parent: parent of profile
  129. * @ns: namespace the profile is in
  130. * @replacedby: is set to the profile that replaced this profile
  131. * @rename: optional profile name that this profile renamed
  132. * @xmatch: optional extended matching for unconfined executables names
  133. * @xmatch_len: xmatch prefix len, used to determine xmatch priority
  134. * @sid: the unique security id number of this profile
  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. u32 sid;
  169. enum audit_mode audit;
  170. enum profile_mode mode;
  171. u32 flags;
  172. u32 path_flags;
  173. int size;
  174. struct aa_policydb policy;
  175. struct aa_file_rules file;
  176. struct aa_caps caps;
  177. struct aa_rlimit rlimits;
  178. };
  179. extern struct aa_namespace *root_ns;
  180. extern enum profile_mode aa_g_profile_mode;
  181. void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
  182. bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
  183. const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
  184. int aa_alloc_root_ns(void);
  185. void aa_free_root_ns(void);
  186. void aa_free_namespace_kref(struct kref *kref);
  187. struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
  188. const char *name);
  189. static inline struct aa_policy *aa_get_common(struct aa_policy *c)
  190. {
  191. if (c)
  192. kref_get(&c->count);
  193. return c;
  194. }
  195. /**
  196. * aa_get_namespace - increment references count on @ns
  197. * @ns: namespace to increment reference count of (MAYBE NULL)
  198. *
  199. * Returns: pointer to @ns, if @ns is NULL returns NULL
  200. * Requires: @ns must be held with valid refcount when called
  201. */
  202. static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
  203. {
  204. if (ns)
  205. kref_get(&(ns->base.count));
  206. return ns;
  207. }
  208. /**
  209. * aa_put_namespace - decrement refcount on @ns
  210. * @ns: namespace to put reference of
  211. *
  212. * Decrement reference count of @ns and if no longer in use free it
  213. */
  214. static inline void aa_put_namespace(struct aa_namespace *ns)
  215. {
  216. if (ns)
  217. kref_put(&ns->base.count, aa_free_namespace_kref);
  218. }
  219. struct aa_profile *aa_alloc_profile(const char *name);
  220. struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
  221. void aa_free_profile_kref(struct kref *kref);
  222. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
  223. struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
  224. struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
  225. ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
  226. ssize_t aa_remove_profiles(char *name, size_t size);
  227. #define PROF_ADD 1
  228. #define PROF_REPLACE 0
  229. #define unconfined(X) ((X)->flags & PFLAG_UNCONFINED)
  230. /**
  231. * aa_newest_version - find the newest version of @profile
  232. * @profile: the profile to check for newer versions of (NOT NULL)
  233. *
  234. * Returns: newest version of @profile, if @profile is the newest version
  235. * return @profile.
  236. *
  237. * NOTE: the profile returned is not refcounted, The refcount on @profile
  238. * must be held until the caller decides what to do with the returned newest
  239. * version.
  240. */
  241. static inline struct aa_profile *aa_newest_version(struct aa_profile *profile)
  242. {
  243. while (profile->replacedby)
  244. profile = profile->replacedby;
  245. return profile;
  246. }
  247. /**
  248. * aa_get_profile - increment refcount on profile @p
  249. * @p: profile (MAYBE NULL)
  250. *
  251. * Returns: pointer to @p if @p is NULL will return NULL
  252. * Requires: @p must be held with valid refcount when called
  253. */
  254. static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
  255. {
  256. if (p)
  257. kref_get(&(p->base.count));
  258. return p;
  259. }
  260. /**
  261. * aa_put_profile - decrement refcount on profile @p
  262. * @p: profile (MAYBE NULL)
  263. */
  264. static inline void aa_put_profile(struct aa_profile *p)
  265. {
  266. if (p)
  267. kref_put(&p->base.count, aa_free_profile_kref);
  268. }
  269. static inline int AUDIT_MODE(struct aa_profile *profile)
  270. {
  271. if (aa_g_audit != AUDIT_NORMAL)
  272. return aa_g_audit;
  273. return profile->audit;
  274. }
  275. bool aa_may_manage_policy(int op);
  276. #endif /* __AA_POLICY_H */