policy.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 aa_profile_mode_names[];
  29. #define APPARMOR_MODE_NAMES_MAX_INDEX 4
  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. APPARMOR_UNCONFINED, /* profile set to unconfined */
  49. };
  50. enum profile_flags {
  51. PFLAG_HAT = 1, /* profile is a hat */
  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. * @uniq_id: a unique id count for the profiles in the namespace
  97. * @dents: dentries for the namespaces file entries in apparmorfs
  98. *
  99. * An aa_namespace defines the set profiles that are searched to determine
  100. * which profile to attach to a task. Profiles can not be shared between
  101. * aa_namespaces and profile names within a namespace are guaranteed to be
  102. * unique. When profiles in separate namespaces have the same name they
  103. * are NOT considered to be equivalent.
  104. *
  105. * Namespaces are hierarchical and only namespaces and profiles below the
  106. * current namespace are visible.
  107. *
  108. * Namespace names must be unique and can not contain the characters :/\0
  109. *
  110. * FIXME TODO: add vserver support of namespaces (can it all be done in
  111. * userspace?)
  112. */
  113. struct aa_namespace {
  114. struct aa_policy base;
  115. struct aa_namespace *parent;
  116. struct mutex lock;
  117. struct aa_ns_acct acct;
  118. struct aa_profile *unconfined;
  119. struct list_head sub_ns;
  120. atomic_t uniq_null;
  121. long uniq_id;
  122. struct dentry *dents[AAFS_NS_SIZEOF];
  123. };
  124. /* struct aa_policydb - match engine for a policy
  125. * dfa: dfa pattern match
  126. * start: set of start states for the different classes of data
  127. */
  128. struct aa_policydb {
  129. /* Generic policy DFA specific rule types will be subsections of it */
  130. struct aa_dfa *dfa;
  131. unsigned int start[AA_CLASS_LAST + 1];
  132. };
  133. struct aa_replacedby {
  134. struct kref count;
  135. struct aa_profile __rcu *profile;
  136. };
  137. /* struct aa_profile - basic confinement data
  138. * @base - base components of the profile (name, refcount, lists, lock ...)
  139. * @count: reference count of the obj
  140. * @rcu: rcu head used when removing from @list
  141. * @parent: parent of profile
  142. * @ns: namespace the profile is in
  143. * @replacedby: is set to the profile that replaced this profile
  144. * @rename: optional profile name that this profile renamed
  145. * @attach: human readable attachment string
  146. * @xmatch: optional extended matching for unconfined executables names
  147. * @xmatch_len: xmatch prefix len, used to determine xmatch priority
  148. * @audit: the auditing mode of the profile
  149. * @mode: the enforcement mode of the profile
  150. * @flags: flags controlling profile behavior
  151. * @path_flags: flags controlling path generation behavior
  152. * @size: the memory consumed by this profiles rules
  153. * @policy: general match rules governing policy
  154. * @file: The set of rules governing basic file access and domain transitions
  155. * @caps: capabilities for the profile
  156. * @rlimits: rlimits for the profile
  157. *
  158. * @dents: dentries for the profiles file entries in apparmorfs
  159. * @dirname: name of the profile dir in apparmorfs
  160. *
  161. * The AppArmor profile contains the basic confinement data. Each profile
  162. * has a name, and exists in a namespace. The @name and @exec_match are
  163. * used to determine profile attachment against unconfined tasks. All other
  164. * attachments are determined by profile X transition rules.
  165. *
  166. * The @replacedby struct is write protected by the profile lock.
  167. *
  168. * Profiles have a hierarchy where hats and children profiles keep
  169. * a reference to their parent.
  170. *
  171. * Profile names can not begin with a : and can not contain the \0
  172. * character. If a profile name begins with / it will be considered when
  173. * determining profile attachment on "unconfined" tasks.
  174. */
  175. struct aa_profile {
  176. struct aa_policy base;
  177. struct kref count;
  178. struct rcu_head rcu;
  179. struct aa_profile __rcu *parent;
  180. struct aa_namespace *ns;
  181. struct aa_replacedby *replacedby;
  182. const char *rename;
  183. const char *attach;
  184. struct aa_dfa *xmatch;
  185. int xmatch_len;
  186. enum audit_mode audit;
  187. long mode;
  188. long flags;
  189. u32 path_flags;
  190. int size;
  191. struct aa_policydb policy;
  192. struct aa_file_rules file;
  193. struct aa_caps caps;
  194. struct aa_rlimit rlimits;
  195. unsigned char *hash;
  196. char *dirname;
  197. struct dentry *dents[AAFS_PROF_SIZEOF];
  198. };
  199. extern struct aa_namespace *root_ns;
  200. extern enum profile_mode aa_g_profile_mode;
  201. void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
  202. bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
  203. const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
  204. int aa_alloc_root_ns(void);
  205. void aa_free_root_ns(void);
  206. void aa_free_namespace_kref(struct kref *kref);
  207. struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
  208. const char *name);
  209. void aa_free_replacedby_kref(struct kref *kref);
  210. struct aa_profile *aa_alloc_profile(const char *name);
  211. struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
  212. void aa_free_profile(struct aa_profile *profile);
  213. void aa_free_profile_kref(struct kref *kref);
  214. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
  215. struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
  216. struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
  217. ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
  218. ssize_t aa_remove_profiles(char *name, size_t size);
  219. #define PROF_ADD 1
  220. #define PROF_REPLACE 0
  221. #define unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
  222. static inline struct aa_profile *aa_deref_parent(struct aa_profile *p)
  223. {
  224. return rcu_dereference_protected(p->parent,
  225. mutex_is_locked(&p->ns->lock));
  226. }
  227. /**
  228. * aa_get_profile - increment refcount on profile @p
  229. * @p: profile (MAYBE NULL)
  230. *
  231. * Returns: pointer to @p if @p is NULL will return NULL
  232. * Requires: @p must be held with valid refcount when called
  233. */
  234. static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
  235. {
  236. if (p)
  237. kref_get(&(p->count));
  238. return p;
  239. }
  240. /**
  241. * aa_get_profile_not0 - increment refcount on profile @p found via lookup
  242. * @p: profile (MAYBE NULL)
  243. *
  244. * Returns: pointer to @p if @p is NULL will return NULL
  245. * Requires: @p must be held with valid refcount when called
  246. */
  247. static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
  248. {
  249. if (p && kref_get_not0(&p->count))
  250. return p;
  251. return NULL;
  252. }
  253. /**
  254. * aa_get_profile_rcu - increment a refcount profile that can be replaced
  255. * @p: pointer to profile that can be replaced (NOT NULL)
  256. *
  257. * Returns: pointer to a refcounted profile.
  258. * else NULL if no profile
  259. */
  260. static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
  261. {
  262. struct aa_profile *c;
  263. rcu_read_lock();
  264. do {
  265. c = rcu_dereference(*p);
  266. } while (c && !kref_get_not0(&c->count));
  267. rcu_read_unlock();
  268. return c;
  269. }
  270. /**
  271. * aa_get_newest_profile - find the newest version of @profile
  272. * @profile: the profile to check for newer versions of
  273. *
  274. * Returns: refcounted newest version of @profile taking into account
  275. * replacement, renames and removals
  276. * return @profile.
  277. */
  278. static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
  279. {
  280. if (!p)
  281. return NULL;
  282. if (PROFILE_INVALID(p))
  283. return aa_get_profile_rcu(&p->replacedby->profile);
  284. return aa_get_profile(p);
  285. }
  286. /**
  287. * aa_put_profile - decrement refcount on profile @p
  288. * @p: profile (MAYBE NULL)
  289. */
  290. static inline void aa_put_profile(struct aa_profile *p)
  291. {
  292. if (p)
  293. kref_put(&p->count, aa_free_profile_kref);
  294. }
  295. static inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p)
  296. {
  297. if (p)
  298. kref_get(&(p->count));
  299. return p;
  300. }
  301. static inline void aa_put_replacedby(struct aa_replacedby *p)
  302. {
  303. if (p)
  304. kref_put(&p->count, aa_free_replacedby_kref);
  305. }
  306. /* requires profile list write lock held */
  307. static inline void __aa_update_replacedby(struct aa_profile *orig,
  308. struct aa_profile *new)
  309. {
  310. struct aa_profile *tmp;
  311. tmp = rcu_dereference_protected(orig->replacedby->profile,
  312. mutex_is_locked(&orig->ns->lock));
  313. rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new));
  314. orig->flags |= PFLAG_INVALID;
  315. aa_put_profile(tmp);
  316. }
  317. /**
  318. * aa_get_namespace - increment references count on @ns
  319. * @ns: namespace to increment reference count of (MAYBE NULL)
  320. *
  321. * Returns: pointer to @ns, if @ns is NULL returns NULL
  322. * Requires: @ns must be held with valid refcount when called
  323. */
  324. static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
  325. {
  326. if (ns)
  327. aa_get_profile(ns->unconfined);
  328. return ns;
  329. }
  330. /**
  331. * aa_put_namespace - decrement refcount on @ns
  332. * @ns: namespace to put reference of
  333. *
  334. * Decrement reference count of @ns and if no longer in use free it
  335. */
  336. static inline void aa_put_namespace(struct aa_namespace *ns)
  337. {
  338. if (ns)
  339. aa_put_profile(ns->unconfined);
  340. }
  341. static inline int AUDIT_MODE(struct aa_profile *profile)
  342. {
  343. if (aa_g_audit != AUDIT_NORMAL)
  344. return aa_g_audit;
  345. return profile->audit;
  346. }
  347. bool aa_may_manage_policy(int op);
  348. #endif /* __AA_POLICY_H */