policy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #define PROFILE_INVALID(_profile) ((_profile)->flags & PFLAG_INVALID)
  37. #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
  38. /*
  39. * FIXME: currently need a clean way to replace and remove profiles as a
  40. * set. It should be done at the namespace level.
  41. * Either, with a set of profiles loaded at the namespace level or via
  42. * a mark and remove marked interface.
  43. */
  44. enum profile_mode {
  45. APPARMOR_ENFORCE, /* enforce access rules */
  46. APPARMOR_COMPLAIN, /* allow and log access violations */
  47. APPARMOR_KILL, /* kill task on access violation */
  48. };
  49. enum profile_flags {
  50. PFLAG_HAT = 1, /* profile is a hat */
  51. PFLAG_UNCONFINED = 2, /* profile is an unconfined profile */
  52. PFLAG_NULL = 4, /* profile is null learning profile */
  53. PFLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */
  54. PFLAG_IMMUTABLE = 0x10, /* don't allow changes/replacement */
  55. PFLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */
  56. PFLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */
  57. PFLAG_OLD_NULL_TRANS = 0x100, /* use // as the null transition */
  58. PFLAG_INVALID = 0x200, /* profile replaced/removed */
  59. PFLAG_NS_COUNT = 0x400, /* carries NS ref count */
  60. /* These flags must correspond with PATH_flags */
  61. PFLAG_MEDIATE_DELETED = 0x10000, /* mediate instead delegate deleted */
  62. };
  63. struct aa_profile;
  64. /* struct aa_policy - common part of both namespaces and profiles
  65. * @name: name of the object
  66. * @hname - The hierarchical name
  67. * @list: list policy object is on
  68. * @profiles: head of the profiles list contained in the object
  69. */
  70. struct aa_policy {
  71. char *name;
  72. char *hname;
  73. struct list_head list;
  74. struct list_head profiles;
  75. };
  76. /* struct aa_ns_acct - accounting of profiles in namespace
  77. * @max_size: maximum space allowed for all profiles in namespace
  78. * @max_count: maximum number of profiles that can be in this namespace
  79. * @size: current size of profiles
  80. * @count: current count of profiles (includes null profiles)
  81. */
  82. struct aa_ns_acct {
  83. int max_size;
  84. int max_count;
  85. int size;
  86. int count;
  87. };
  88. /* struct aa_namespace - namespace for a set of profiles
  89. * @base: common policy
  90. * @parent: parent of namespace
  91. * @lock: lock for modifying the object
  92. * @acct: accounting for the namespace
  93. * @unconfined: special unconfined profile for the namespace
  94. * @sub_ns: list of namespaces under the current namespace.
  95. * @uniq_null: uniq value used for null learning profiles
  96. *
  97. * An aa_namespace defines the set profiles that are searched to determine
  98. * which profile to attach to a task. Profiles can not be shared between
  99. * aa_namespaces and profile names within a namespace are guaranteed to be
  100. * unique. When profiles in separate namespaces have the same name they
  101. * are NOT considered to be equivalent.
  102. *
  103. * Namespaces are hierarchical and only namespaces and profiles below the
  104. * current namespace are visible.
  105. *
  106. * Namespace names must be unique and can not contain the characters :/\0
  107. *
  108. * FIXME TODO: add vserver support of namespaces (can it all be done in
  109. * userspace?)
  110. */
  111. struct aa_namespace {
  112. struct aa_policy base;
  113. struct aa_namespace *parent;
  114. struct mutex lock;
  115. struct aa_ns_acct acct;
  116. struct aa_profile *unconfined;
  117. struct list_head sub_ns;
  118. atomic_t uniq_null;
  119. };
  120. /* struct aa_policydb - match engine for a policy
  121. * dfa: dfa pattern match
  122. * start: set of start states for the different classes of data
  123. */
  124. struct aa_policydb {
  125. /* Generic policy DFA specific rule types will be subsections of it */
  126. struct aa_dfa *dfa;
  127. unsigned int start[AA_CLASS_LAST + 1];
  128. };
  129. struct aa_replacedby {
  130. struct kref count;
  131. struct aa_profile __rcu *profile;
  132. };
  133. /* struct aa_profile - basic confinement data
  134. * @base - base components of the profile (name, refcount, lists, lock ...)
  135. * @count: reference count of the obj
  136. * @rcu: rcu head used when removing from @list
  137. * @parent: parent of profile
  138. * @ns: namespace the profile is in
  139. * @replacedby: is set to the profile that replaced this profile
  140. * @rename: optional profile name that this profile renamed
  141. * @xmatch: optional extended matching for unconfined executables names
  142. * @xmatch_len: xmatch prefix len, used to determine xmatch priority
  143. * @audit: the auditing mode of the profile
  144. * @mode: the enforcement mode of the profile
  145. * @flags: flags controlling profile behavior
  146. * @path_flags: flags controlling path generation behavior
  147. * @size: the memory consumed by this profiles rules
  148. * @policy: general match rules governing policy
  149. * @file: The set of rules governing basic file access and domain transitions
  150. * @caps: capabilities for the profile
  151. * @rlimits: rlimits for the profile
  152. *
  153. * The AppArmor profile contains the basic confinement data. Each profile
  154. * has a name, and exists in a namespace. The @name and @exec_match are
  155. * used to determine profile attachment against unconfined tasks. All other
  156. * attachments are determined by profile X transition rules.
  157. *
  158. * The @replacedby struct is write protected by the profile lock.
  159. *
  160. * Profiles have a hierarchy where hats and children profiles keep
  161. * a reference to their parent.
  162. *
  163. * Profile names can not begin with a : and can not contain the \0
  164. * character. If a profile name begins with / it will be considered when
  165. * determining profile attachment on "unconfined" tasks.
  166. */
  167. struct aa_profile {
  168. struct aa_policy base;
  169. struct kref count;
  170. struct rcu_head rcu;
  171. struct aa_profile __rcu *parent;
  172. struct aa_namespace *ns;
  173. struct aa_replacedby *replacedby;
  174. const char *rename;
  175. struct aa_dfa *xmatch;
  176. int xmatch_len;
  177. enum audit_mode audit;
  178. enum profile_mode mode;
  179. long flags;
  180. u32 path_flags;
  181. int size;
  182. struct aa_policydb policy;
  183. struct aa_file_rules file;
  184. struct aa_caps caps;
  185. struct aa_rlimit rlimits;
  186. };
  187. extern struct aa_namespace *root_ns;
  188. extern enum profile_mode aa_g_profile_mode;
  189. void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
  190. bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
  191. const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
  192. int aa_alloc_root_ns(void);
  193. void aa_free_root_ns(void);
  194. void aa_free_namespace_kref(struct kref *kref);
  195. struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
  196. const char *name);
  197. void aa_free_replacedby_kref(struct kref *kref);
  198. struct aa_profile *aa_alloc_profile(const char *name);
  199. struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
  200. void aa_free_profile_kref(struct kref *kref);
  201. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
  202. struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
  203. struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
  204. ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
  205. ssize_t aa_remove_profiles(char *name, size_t size);
  206. #define PROF_ADD 1
  207. #define PROF_REPLACE 0
  208. #define unconfined(X) ((X)->flags & PFLAG_UNCONFINED)
  209. /**
  210. * aa_get_profile - increment refcount on profile @p
  211. * @p: profile (MAYBE NULL)
  212. *
  213. * Returns: pointer to @p if @p is NULL will return NULL
  214. * Requires: @p must be held with valid refcount when called
  215. */
  216. static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
  217. {
  218. if (p)
  219. kref_get(&(p->count));
  220. return p;
  221. }
  222. /**
  223. * aa_get_profile_not0 - increment refcount on profile @p found via lookup
  224. * @p: profile (MAYBE NULL)
  225. *
  226. * Returns: pointer to @p if @p is NULL will return NULL
  227. * Requires: @p must be held with valid refcount when called
  228. */
  229. static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
  230. {
  231. if (p && kref_get_not0(&p->count))
  232. return p;
  233. return NULL;
  234. }
  235. /**
  236. * aa_get_profile_rcu - increment a refcount profile that can be replaced
  237. * @p: pointer to profile that can be replaced (NOT NULL)
  238. *
  239. * Returns: pointer to a refcounted profile.
  240. * else NULL if no profile
  241. */
  242. static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
  243. {
  244. struct aa_profile *c;
  245. rcu_read_lock();
  246. do {
  247. c = rcu_dereference(*p);
  248. } while (c && !kref_get_not0(&c->count));
  249. rcu_read_unlock();
  250. return c;
  251. }
  252. /**
  253. * aa_get_newest_profile - find the newest version of @profile
  254. * @profile: the profile to check for newer versions of
  255. *
  256. * Returns: refcounted newest version of @profile taking into account
  257. * replacement, renames and removals
  258. * return @profile.
  259. */
  260. static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
  261. {
  262. if (!p)
  263. return NULL;
  264. if (PROFILE_INVALID(p))
  265. return aa_get_profile_rcu(&p->replacedby->profile);
  266. return aa_get_profile(p);
  267. }
  268. /**
  269. * aa_put_profile - decrement refcount on profile @p
  270. * @p: profile (MAYBE NULL)
  271. */
  272. static inline void aa_put_profile(struct aa_profile *p)
  273. {
  274. if (p)
  275. kref_put(&p->count, aa_free_profile_kref);
  276. }
  277. static inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p)
  278. {
  279. if (p)
  280. kref_get(&(p->count));
  281. return p;
  282. }
  283. static inline void aa_put_replacedby(struct aa_replacedby *p)
  284. {
  285. if (p)
  286. kref_put(&p->count, aa_free_replacedby_kref);
  287. }
  288. /* requires profile list write lock held */
  289. static inline void __aa_update_replacedby(struct aa_profile *orig,
  290. struct aa_profile *new)
  291. {
  292. struct aa_profile *tmp = rcu_dereference(orig->replacedby->profile);
  293. rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new));
  294. orig->flags |= PFLAG_INVALID;
  295. aa_put_profile(tmp);
  296. }
  297. /**
  298. * aa_get_namespace - increment references count on @ns
  299. * @ns: namespace to increment reference count of (MAYBE NULL)
  300. *
  301. * Returns: pointer to @ns, if @ns is NULL returns NULL
  302. * Requires: @ns must be held with valid refcount when called
  303. */
  304. static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
  305. {
  306. if (ns)
  307. aa_get_profile(ns->unconfined);
  308. return ns;
  309. }
  310. /**
  311. * aa_put_namespace - decrement refcount on @ns
  312. * @ns: namespace to put reference of
  313. *
  314. * Decrement reference count of @ns and if no longer in use free it
  315. */
  316. static inline void aa_put_namespace(struct aa_namespace *ns)
  317. {
  318. if (ns)
  319. aa_put_profile(ns->unconfined);
  320. }
  321. static inline int AUDIT_MODE(struct aa_profile *profile)
  322. {
  323. if (aa_g_audit != AUDIT_NORMAL)
  324. return aa_g_audit;
  325. return profile->audit;
  326. }
  327. bool aa_may_manage_policy(int op);
  328. #endif /* __AA_POLICY_H */