PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/security/apparmor/include/policy.h

https://gitlab.com/deepcypher/linux
C Header | 311 lines | 165 code | 52 blank | 94 comment | 17 complexity | ac86554a2b1aed40e61f60fcab0a7ac5 MD5 | raw file
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor policy definitions.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #ifndef __AA_POLICY_H
  11. #define __AA_POLICY_H
  12. #include <linux/capability.h>
  13. #include <linux/cred.h>
  14. #include <linux/kref.h>
  15. #include <linux/rhashtable.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/socket.h>
  19. #include "apparmor.h"
  20. #include "audit.h"
  21. #include "capability.h"
  22. #include "domain.h"
  23. #include "file.h"
  24. #include "lib.h"
  25. #include "label.h"
  26. #include "net.h"
  27. #include "perms.h"
  28. #include "resource.h"
  29. struct aa_ns;
  30. extern int unprivileged_userns_apparmor_policy;
  31. extern const char *const aa_profile_mode_names[];
  32. #define APPARMOR_MODE_NAMES_MAX_INDEX 4
  33. #define PROFILE_MODE(_profile, _mode) \
  34. ((aa_g_profile_mode == (_mode)) || \
  35. ((_profile)->mode == (_mode)))
  36. #define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN)
  37. #define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL)
  38. #define PROFILE_IS_HAT(_profile) ((_profile)->label.flags & FLAG_HAT)
  39. #define profile_is_stale(_profile) (label_is_stale(&(_profile)->label))
  40. #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
  41. /*
  42. * FIXME: currently need a clean way to replace and remove profiles as a
  43. * set. It should be done at the namespace level.
  44. * Either, with a set of profiles loaded at the namespace level or via
  45. * a mark and remove marked interface.
  46. */
  47. enum profile_mode {
  48. APPARMOR_ENFORCE, /* enforce access rules */
  49. APPARMOR_COMPLAIN, /* allow and log access violations */
  50. APPARMOR_KILL, /* kill task on access violation */
  51. APPARMOR_UNCONFINED, /* profile set to unconfined */
  52. };
  53. /* struct aa_policydb - match engine for a policy
  54. * dfa: dfa pattern match
  55. * start: set of start states for the different classes of data
  56. */
  57. struct aa_policydb {
  58. /* Generic policy DFA specific rule types will be subsections of it */
  59. struct aa_dfa *dfa;
  60. unsigned int start[AA_CLASS_LAST + 1];
  61. };
  62. /* struct aa_data - generic data structure
  63. * key: name for retrieving this data
  64. * size: size of data in bytes
  65. * data: binary data
  66. * head: reserved for rhashtable
  67. */
  68. struct aa_data {
  69. char *key;
  70. u32 size;
  71. char *data;
  72. struct rhash_head head;
  73. };
  74. /* struct aa_profile - basic confinement data
  75. * @base - base components of the profile (name, refcount, lists, lock ...)
  76. * @label - label this profile is an extension of
  77. * @parent: parent of profile
  78. * @ns: namespace the profile is in
  79. * @rename: optional profile name that this profile renamed
  80. * @attach: human readable attachment string
  81. * @xmatch: optional extended matching for unconfined executables names
  82. * @xmatch_len: xmatch prefix len, used to determine xmatch priority
  83. * @audit: the auditing mode of the profile
  84. * @mode: the enforcement mode of the profile
  85. * @path_flags: flags controlling path generation behavior
  86. * @disconnected: what to prepend if attach_disconnected is specified
  87. * @size: the memory consumed by this profiles rules
  88. * @policy: general match rules governing policy
  89. * @file: The set of rules governing basic file access and domain transitions
  90. * @caps: capabilities for the profile
  91. * @rlimits: rlimits for the profile
  92. *
  93. * @dents: dentries for the profiles file entries in apparmorfs
  94. * @dirname: name of the profile dir in apparmorfs
  95. * @data: hashtable for free-form policy aa_data
  96. *
  97. * The AppArmor profile contains the basic confinement data. Each profile
  98. * has a name, and exists in a namespace. The @name and @exec_match are
  99. * used to determine profile attachment against unconfined tasks. All other
  100. * attachments are determined by profile X transition rules.
  101. *
  102. * Profiles have a hierarchy where hats and children profiles keep
  103. * a reference to their parent.
  104. *
  105. * Profile names can not begin with a : and can not contain the \0
  106. * character. If a profile name begins with / it will be considered when
  107. * determining profile attachment on "unconfined" tasks.
  108. */
  109. struct aa_profile {
  110. struct aa_policy base;
  111. struct aa_profile __rcu *parent;
  112. struct aa_ns *ns;
  113. const char *rename;
  114. const char *attach;
  115. struct aa_dfa *xmatch;
  116. int xmatch_len;
  117. enum audit_mode audit;
  118. long mode;
  119. u32 path_flags;
  120. const char *disconnected;
  121. int size;
  122. struct aa_policydb policy;
  123. struct aa_file_rules file;
  124. struct aa_caps caps;
  125. int xattr_count;
  126. char **xattrs;
  127. struct aa_rlimit rlimits;
  128. int secmark_count;
  129. struct aa_secmark *secmark;
  130. struct aa_loaddata *rawdata;
  131. unsigned char *hash;
  132. char *dirname;
  133. struct dentry *dents[AAFS_PROF_SIZEOF];
  134. struct rhashtable *data;
  135. struct aa_label label;
  136. };
  137. extern enum profile_mode aa_g_profile_mode;
  138. #define AA_MAY_LOAD_POLICY AA_MAY_APPEND
  139. #define AA_MAY_REPLACE_POLICY AA_MAY_WRITE
  140. #define AA_MAY_REMOVE_POLICY AA_MAY_DELETE
  141. #define profiles_ns(P) ((P)->ns)
  142. #define name_is_shared(A, B) ((A)->hname && (A)->hname == (B)->hname)
  143. void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
  144. void aa_free_proxy_kref(struct kref *kref);
  145. struct aa_profile *aa_alloc_profile(const char *name, struct aa_proxy *proxy,
  146. gfp_t gfp);
  147. struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
  148. const char *base, gfp_t gfp);
  149. void aa_free_profile(struct aa_profile *profile);
  150. void aa_free_profile_kref(struct kref *kref);
  151. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
  152. struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
  153. size_t n);
  154. struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *name);
  155. struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
  156. const char *fqname, size_t n);
  157. struct aa_profile *aa_match_profile(struct aa_ns *ns, const char *name);
  158. ssize_t aa_replace_profiles(struct aa_ns *view, struct aa_label *label,
  159. u32 mask, struct aa_loaddata *udata);
  160. ssize_t aa_remove_profiles(struct aa_ns *view, struct aa_label *label,
  161. char *name, size_t size);
  162. void __aa_profile_list_release(struct list_head *head);
  163. #define PROF_ADD 1
  164. #define PROF_REPLACE 0
  165. #define profile_unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
  166. /**
  167. * aa_get_newest_profile - simple wrapper fn to wrap the label version
  168. * @p: profile (NOT NULL)
  169. *
  170. * Returns refcount to newest version of the profile (maybe @p)
  171. *
  172. * Requires: @p must be held with a valid refcount
  173. */
  174. static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
  175. {
  176. return labels_profile(aa_get_newest_label(&p->label));
  177. }
  178. static inline unsigned int PROFILE_MEDIATES(struct aa_profile *profile,
  179. unsigned char class)
  180. {
  181. if (class <= AA_CLASS_LAST)
  182. return profile->policy.start[class];
  183. else
  184. return aa_dfa_match_len(profile->policy.dfa,
  185. profile->policy.start[0], &class, 1);
  186. }
  187. static inline unsigned int PROFILE_MEDIATES_AF(struct aa_profile *profile,
  188. u16 AF) {
  189. unsigned int state = PROFILE_MEDIATES(profile, AA_CLASS_NET);
  190. __be16 be_af = cpu_to_be16(AF);
  191. if (!state)
  192. return 0;
  193. return aa_dfa_match_len(profile->policy.dfa, state, (char *) &be_af, 2);
  194. }
  195. /**
  196. * aa_get_profile - increment refcount on profile @p
  197. * @p: profile (MAYBE NULL)
  198. *
  199. * Returns: pointer to @p if @p is NULL will return NULL
  200. * Requires: @p must be held with valid refcount when called
  201. */
  202. static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
  203. {
  204. if (p)
  205. kref_get(&(p->label.count));
  206. return p;
  207. }
  208. /**
  209. * aa_get_profile_not0 - increment refcount on profile @p found via lookup
  210. * @p: profile (MAYBE NULL)
  211. *
  212. * Returns: pointer to @p if @p is NULL will return NULL
  213. * Requires: @p must be held with valid refcount when called
  214. */
  215. static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
  216. {
  217. if (p && kref_get_unless_zero(&p->label.count))
  218. return p;
  219. return NULL;
  220. }
  221. /**
  222. * aa_get_profile_rcu - increment a refcount profile that can be replaced
  223. * @p: pointer to profile that can be replaced (NOT NULL)
  224. *
  225. * Returns: pointer to a refcounted profile.
  226. * else NULL if no profile
  227. */
  228. static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
  229. {
  230. struct aa_profile *c;
  231. rcu_read_lock();
  232. do {
  233. c = rcu_dereference(*p);
  234. } while (c && !kref_get_unless_zero(&c->label.count));
  235. rcu_read_unlock();
  236. return c;
  237. }
  238. /**
  239. * aa_put_profile - decrement refcount on profile @p
  240. * @p: profile (MAYBE NULL)
  241. */
  242. static inline void aa_put_profile(struct aa_profile *p)
  243. {
  244. if (p)
  245. kref_put(&p->label.count, aa_label_kref);
  246. }
  247. static inline int AUDIT_MODE(struct aa_profile *profile)
  248. {
  249. if (aa_g_audit != AUDIT_NORMAL)
  250. return aa_g_audit;
  251. return profile->audit;
  252. }
  253. bool aa_policy_view_capable(struct aa_label *label, struct aa_ns *ns);
  254. bool aa_policy_admin_capable(struct aa_label *label, struct aa_ns *ns);
  255. int aa_may_manage_policy(struct aa_label *label, struct aa_ns *ns,
  256. u32 mask);
  257. bool aa_current_policy_view_capable(struct aa_ns *ns);
  258. bool aa_current_policy_admin_capable(struct aa_ns *ns);
  259. #endif /* __AA_POLICY_H */