policy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 int aa_unprivileged_unconfined_restricted;
  32. extern const char *const aa_profile_mode_names[];
  33. #define APPARMOR_MODE_NAMES_MAX_INDEX 4
  34. #define PROFILE_MODE(_profile, _mode) \
  35. ((aa_g_profile_mode == (_mode)) || \
  36. ((_profile)->mode == (_mode)))
  37. #define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN)
  38. #define USER_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_USER)
  39. #define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL)
  40. #define PROFILE_IS_HAT(_profile) ((_profile)->label.flags & FLAG_HAT)
  41. #define CHECK_DEBUG1(_profile) ((_profile)->label.flags & FLAG_DEBUG1)
  42. #define CHECK_DEBUG2(_profile) ((_profile)->label.flags & FLAG_DEBUG2)
  43. #define profile_is_stale(_profile) (label_is_stale(&(_profile)->label))
  44. #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
  45. /*
  46. * FIXME: currently need a clean way to replace and remove profiles as a
  47. * set. It should be done at the namespace level.
  48. * Either, with a set of profiles loaded at the namespace level or via
  49. * a mark and remove marked interface.
  50. */
  51. enum profile_mode {
  52. APPARMOR_ENFORCE, /* enforce access rules */
  53. APPARMOR_COMPLAIN, /* allow and log access violations */
  54. APPARMOR_KILL, /* kill task on access violation */
  55. APPARMOR_UNCONFINED, /* profile set to unconfined */
  56. APPARMOR_USER, /* modified complain mode to userspace */
  57. };
  58. /* struct aa_policydb - match engine for a policy
  59. * count: refcount for the pdb
  60. * dfa: dfa pattern match
  61. * perms: table of permissions
  62. * strs: table of strings, index by x
  63. * start: set of start states for the different classes of data
  64. */
  65. struct aa_policydb {
  66. struct kref count;
  67. struct aa_dfa *dfa;
  68. struct {
  69. struct aa_perms *perms;
  70. u32 size;
  71. };
  72. struct aa_str_table trans;
  73. aa_state_t start[AA_CLASS_LAST + 1];
  74. };
  75. extern struct aa_policydb *nullpdb;
  76. struct aa_policydb *aa_alloc_pdb(gfp_t gfp);
  77. void aa_pdb_free_kref(struct kref *kref);
  78. /**
  79. * aa_get_pdb - increment refcount on @pdb
  80. * @pdb: policydb (MAYBE NULL)
  81. *
  82. * Returns: pointer to @pdb if @pdb is NULL will return NULL
  83. * Requires: @pdb must be held with valid refcount when called
  84. */
  85. static inline struct aa_policydb *aa_get_pdb(struct aa_policydb *pdb)
  86. {
  87. if (pdb)
  88. kref_get(&(pdb->count));
  89. return pdb;
  90. }
  91. /**
  92. * aa_put_pdb - put a pdb refcount
  93. * @pdb: pdb to put refcount (MAYBE NULL)
  94. *
  95. * Requires: if @pdb != NULL that a valid refcount be held
  96. */
  97. static inline void aa_put_pdb(struct aa_policydb *pdb)
  98. {
  99. if (pdb)
  100. kref_put(&pdb->count, aa_pdb_free_kref);
  101. }
  102. static inline struct aa_perms *aa_lookup_perms(struct aa_policydb *policy,
  103. aa_state_t state)
  104. {
  105. unsigned int index = ACCEPT_TABLE(policy->dfa)[state];
  106. if (!(policy->perms))
  107. return &default_perms;
  108. return &(policy->perms[index]);
  109. }
  110. /* struct aa_data - generic data structure
  111. * key: name for retrieving this data
  112. * size: size of data in bytes
  113. * data: binary data
  114. * head: reserved for rhashtable
  115. */
  116. struct aa_data {
  117. char *key;
  118. u32 size;
  119. char *data;
  120. struct rhash_head head;
  121. };
  122. /* struct aa_ruleset - data covering mediation rules
  123. * @list: list the rule is on
  124. * @size: the memory consumed by this ruleset
  125. * @policy: general match rules governing policy
  126. * @file: The set of rules governing basic file access and domain transitions
  127. * @caps: capabilities for the profile
  128. * @rlimits: rlimits for the profile
  129. * @secmark_count: number of secmark entries
  130. * @secmark: secmark label match info
  131. */
  132. struct aa_ruleset {
  133. struct list_head list;
  134. int size;
  135. /* TODO: merge policy and file */
  136. struct aa_policydb *policy;
  137. struct aa_policydb *file;
  138. struct aa_caps caps;
  139. struct aa_rlimit rlimits;
  140. int secmark_count;
  141. struct aa_secmark *secmark;
  142. };
  143. /* struct aa_attachment - data and rules for a profiles attachment
  144. * @list:
  145. * @xmatch_str: 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. * @xattr_count: number of xattrs in table
  149. * @xattrs: table of xattrs
  150. */
  151. struct aa_attachment {
  152. const char *xmatch_str;
  153. struct aa_policydb *xmatch;
  154. unsigned int xmatch_len;
  155. int xattr_count;
  156. char **xattrs;
  157. };
  158. /* struct aa_profile - basic confinement data
  159. * @base - base components of the profile (name, refcount, lists, lock ...)
  160. * @label - label this profile is an extension of
  161. * @parent: parent of profile
  162. * @ns: namespace the profile is in
  163. * @rename: optional profile name that this profile renamed
  164. *
  165. * @audit: the auditing mode of the profile
  166. * @mode: the enforcement mode of the profile
  167. * @path_flags: flags controlling path generation behavior
  168. * @disconnected: what to prepend if attach_disconnected is specified
  169. * @attach: attachment rules for the profile
  170. * @rules: rules to be enforced
  171. *
  172. * @dents: dentries for the profiles file entries in apparmorfs
  173. * @dirname: name of the profile dir in apparmorfs
  174. * @data: hashtable for free-form policy aa_data
  175. *
  176. * The AppArmor profile contains the basic confinement data. Each profile
  177. * has a name, and exists in a namespace. The @name and @exec_match are
  178. * used to determine profile attachment against unconfined tasks. All other
  179. * attachments are determined by profile X transition rules.
  180. *
  181. * Profiles have a hierarchy where hats and children profiles keep
  182. * a reference to their parent.
  183. *
  184. * Profile names can not begin with a : and can not contain the \0
  185. * character. If a profile name begins with / it will be considered when
  186. * determining profile attachment on "unconfined" tasks.
  187. */
  188. struct aa_profile {
  189. struct aa_policy base;
  190. struct aa_profile __rcu *parent;
  191. struct aa_ns *ns;
  192. const char *rename;
  193. enum audit_mode audit;
  194. long mode;
  195. u32 path_flags;
  196. const char *disconnected;
  197. struct aa_attachment attach;
  198. struct list_head rules;
  199. struct aa_loaddata *rawdata;
  200. unsigned char *hash;
  201. char *dirname;
  202. struct dentry *dents[AAFS_PROF_SIZEOF];
  203. struct rhashtable *data;
  204. struct aa_label label;
  205. };
  206. extern enum profile_mode aa_g_profile_mode;
  207. #define AA_MAY_LOAD_POLICY AA_MAY_APPEND
  208. #define AA_MAY_REPLACE_POLICY AA_MAY_WRITE
  209. #define AA_MAY_REMOVE_POLICY AA_MAY_DELETE
  210. #define profiles_ns(P) ((P)->ns)
  211. #define name_is_shared(A, B) ((A)->hname && (A)->hname == (B)->hname)
  212. struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp);
  213. struct aa_profile *aa_alloc_profile(const char *name, struct aa_proxy *proxy,
  214. gfp_t gfp);
  215. struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
  216. gfp_t gfp);
  217. struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
  218. const char *base, gfp_t gfp);
  219. void aa_free_profile(struct aa_profile *profile);
  220. struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
  221. struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
  222. size_t n);
  223. struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *name);
  224. struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
  225. const char *fqname, size_t n);
  226. ssize_t aa_replace_profiles(struct aa_ns *view, struct aa_label *label,
  227. u32 mask, struct aa_loaddata *udata);
  228. ssize_t aa_remove_profiles(struct aa_ns *view, struct aa_label *label,
  229. char *name, size_t size);
  230. void __aa_profile_list_release(struct list_head *head);
  231. #define profile_unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
  232. /**
  233. * aa_get_newest_profile - simple wrapper fn to wrap the label version
  234. * @p: profile (NOT NULL)
  235. *
  236. * Returns refcount to newest version of the profile (maybe @p)
  237. *
  238. * Requires: @p must be held with a valid refcount
  239. */
  240. static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
  241. {
  242. return labels_profile(aa_get_newest_label(&p->label));
  243. }
  244. static inline aa_state_t RULE_MEDIATES(struct aa_ruleset *rules,
  245. unsigned char class)
  246. {
  247. if (class <= AA_CLASS_LAST)
  248. return rules->policy->start[class];
  249. else
  250. return aa_dfa_match_len(rules->policy->dfa,
  251. rules->policy->start[0], &class, 1);
  252. }
  253. static inline aa_state_t RULE_MEDIATES_AF(struct aa_ruleset *rules, u16 AF)
  254. {
  255. aa_state_t state = RULE_MEDIATES(rules, AA_CLASS_NET);
  256. __be16 be_af = cpu_to_be16(AF);
  257. if (!state)
  258. return DFA_NOMATCH;
  259. return aa_dfa_match_len(rules->policy->dfa, state, (char *) &be_af, 2);
  260. }
  261. static inline aa_state_t ANY_RULE_MEDIATES(struct list_head *head,
  262. unsigned char class)
  263. {
  264. struct aa_ruleset *rule;
  265. /* TODO: change to list walk */
  266. rule = list_first_entry(head, typeof(*rule), list);
  267. return RULE_MEDIATES(rule, class);
  268. }
  269. /**
  270. * aa_get_profile - increment refcount on profile @p
  271. * @p: profile (MAYBE NULL)
  272. *
  273. * Returns: pointer to @p if @p is NULL will return NULL
  274. * Requires: @p must be held with valid refcount when called
  275. */
  276. static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
  277. {
  278. if (p)
  279. kref_get(&(p->label.count));
  280. return p;
  281. }
  282. /**
  283. * aa_get_profile_not0 - increment refcount on profile @p found via lookup
  284. * @p: profile (MAYBE NULL)
  285. *
  286. * Returns: pointer to @p if @p is NULL will return NULL
  287. * Requires: @p must be held with valid refcount when called
  288. */
  289. static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
  290. {
  291. if (p && kref_get_unless_zero(&p->label.count))
  292. return p;
  293. return NULL;
  294. }
  295. /**
  296. * aa_get_profile_rcu - increment a refcount profile that can be replaced
  297. * @p: pointer to profile that can be replaced (NOT NULL)
  298. *
  299. * Returns: pointer to a refcounted profile.
  300. * else NULL if no profile
  301. */
  302. static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
  303. {
  304. struct aa_profile *c;
  305. rcu_read_lock();
  306. do {
  307. c = rcu_dereference(*p);
  308. } while (c && !kref_get_unless_zero(&c->label.count));
  309. rcu_read_unlock();
  310. return c;
  311. }
  312. /**
  313. * aa_put_profile - decrement refcount on profile @p
  314. * @p: profile (MAYBE NULL)
  315. */
  316. static inline void aa_put_profile(struct aa_profile *p)
  317. {
  318. if (p)
  319. kref_put(&p->label.count, aa_label_kref);
  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_policy_view_capable(const struct cred *subj_cred,
  328. struct aa_label *label, struct aa_ns *ns);
  329. bool aa_policy_admin_capable(const struct cred *subj_cred,
  330. struct aa_label *label, struct aa_ns *ns);
  331. int aa_may_manage_policy(const struct cred *subj_cred,
  332. struct aa_label *label, struct aa_ns *ns,
  333. u32 mask);
  334. bool aa_current_policy_view_capable(struct aa_ns *ns);
  335. bool aa_current_policy_admin_capable(struct aa_ns *ns);
  336. #endif /* __AA_POLICY_H */