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
11 #ifndef __AA_POLICY_H
12 #define __AA_POLICY_H
13
14 #include <linux/capability.h>
15 #include <linux/cred.h>
16 #include <linux/kref.h>
17 #include <linux/rhashtable.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/socket.h>
21
22 #include "apparmor.h"
23 #include "audit.h"
24 #include "capability.h"
25 #include "domain.h"
26 #include "file.h"
27 #include "lib.h"
28 #include "label.h"
29 #include "perms.h"
30 #include "resource.h"
31
32
33 struct aa_ns;
34
35 extern int unprivileged_userns_apparmor_policy;
36 extern int aa_unprivileged_unconfined_restricted;
37
38 extern const char *const aa_profile_mode_names[];
39
40 #define PROFILE_MODE(_profile, _mode) \
41 ((aa_g_profile_mode == (_mode)) || \
42 ((_profile)->mode == (_mode)))
43
44 #define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN)
45
46 #define USER_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_USER)
47
48 #define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL)
49
50 #define PROFILE_IS_HAT(_profile) ((_profile)->label.flags & FLAG_HAT)
51
52 #define CHECK_DEBUG1(_profile) ((_profile)->label.flags & FLAG_DEBUG1)
53
54 #define CHECK_DEBUG2(_profile) ((_profile)->label.flags & FLAG_DEBUG2)
55
56 #define profile_is_stale(_profile) (label_is_stale(&(_profile)->label))
57
58 #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
59
60 /* flags in the dfa accept2 table */
61 enum dfa_accept_flags {
62 ACCEPT_FLAG_OWNER = 1,
63 };
64
65 /*
66 * FIXME: currently need a clean way to replace and remove profiles as a
67 * set. It should be done at the namespace level.
68 * Either, with a set of profiles loaded at the namespace level or via
69 * a mark and remove marked interface.
70 */
71 enum profile_mode {
72 APPARMOR_ENFORCE, /* enforce access rules */
73 APPARMOR_COMPLAIN, /* allow and log access violations */
74 APPARMOR_KILL, /* kill task on access violation */
75 APPARMOR_UNCONFINED, /* profile set to unconfined */
76 APPARMOR_USER, /* modified complain mode to userspace */
77 PROFILE_MODE_NAMES_COUNT /* Must be last entry */
78 };
79
80
81 struct aa_tags_header {
82 u32 mask; /* bit mask matching permissions */
83 u32 count; /* number of strings per entry */
84 u32 size; /* size of all strings covered by count */
85 u32 tags; /* index into string table */
86 };
87
88 struct aa_tags_struct {
89 struct {
90 u32 size; /* number of entries in tagsets */
91 u32 *table; /* indexes into headers & strs */
92 } sets;
93 struct {
94 u32 size; /* number of headers == num of strs */
95 struct aa_tags_header *table;
96 } hdrs;
97 struct aa_str_table strs;
98 };
99
100 /* struct aa_policydb - match engine for a policy
101 * @count: refcount for the pdb
102 * @dfa: dfa pattern match
103 * @perms: table of permissions
104 * @size: number of entries in @perms
105 * @trans: table of strings, index by x
106 * @tags: table of tags that perms->tag indexes
107 * @start:_states to start in for each class
108 * start: set of start states for the different classes of data
109 */
110 struct aa_policydb {
111 struct kref count;
112 struct aa_dfa *dfa;
113 struct {
114 struct aa_perms *perms;
115 u32 size;
116 };
117 struct aa_str_table trans;
118 struct aa_tags_struct tags;
119 aa_state_t start[AA_CLASS_LAST + 1];
120 };
121
122 extern struct aa_policydb *nullpdb;
123
124 void aa_destroy_tags(struct aa_tags_struct *tags);
125 struct aa_policydb *aa_alloc_pdb(gfp_t gfp);
126 void aa_pdb_free_kref(struct kref *kref);
127
128 /**
129 * aa_get_pdb - increment refcount on @pdb
130 * @pdb: policydb (MAYBE NULL)
131 *
132 * Returns: pointer to @pdb if @pdb is NULL will return NULL
133 * Requires: @pdb must be held with valid refcount when called
134 */
aa_get_pdb(struct aa_policydb * pdb)135 static inline struct aa_policydb *aa_get_pdb(struct aa_policydb *pdb)
136 {
137 if (pdb)
138 kref_get(&(pdb->count));
139
140 return pdb;
141 }
142
143 /**
144 * aa_put_pdb - put a pdb refcount
145 * @pdb: pdb to put refcount (MAYBE NULL)
146 *
147 * Requires: if @pdb != NULL that a valid refcount be held
148 */
aa_put_pdb(struct aa_policydb * pdb)149 static inline void aa_put_pdb(struct aa_policydb *pdb)
150 {
151 if (pdb)
152 kref_put(&pdb->count, aa_pdb_free_kref);
153 }
154
155 /* lookup perm that doesn't have and object conditional */
aa_lookup_perms(struct aa_policydb * policy,aa_state_t state)156 static inline struct aa_perms *aa_lookup_perms(struct aa_policydb *policy,
157 aa_state_t state)
158 {
159 unsigned int index = ACCEPT_TABLE(policy->dfa)[state];
160
161 if (!(policy->perms))
162 return &default_perms;
163
164 return &(policy->perms[index]);
165 }
166
167 /* struct aa_data - generic data structure
168 * key: name for retrieving this data
169 * size: size of data in bytes
170 * data: binary data
171 * head: reserved for rhashtable
172 */
173 struct aa_data {
174 char *key;
175 u32 size;
176 char *data;
177 struct rhash_head head;
178 };
179
180 /* struct aa_ruleset - data covering mediation rules
181 * @list: list the rule is on
182 * @size: the memory consumed by this ruleset
183 * @policy: general match rules governing policy
184 * @file: The set of rules governing basic file access and domain transitions
185 * @caps: capabilities for the profile
186 * @rlimits: rlimits for the profile
187 * @secmark_count: number of secmark entries
188 * @secmark: secmark label match info
189 */
190 struct aa_ruleset {
191 int size;
192
193 /* TODO: merge policy and file */
194 struct aa_policydb *policy;
195 struct aa_policydb *file;
196 struct aa_caps caps;
197
198 struct aa_rlimit rlimits;
199
200 int secmark_count;
201 struct aa_secmark *secmark;
202 };
203
204
205 /* struct aa_attachment - data and rules for a profiles attachment
206 * @list:
207 * @xmatch_str: human readable attachment string
208 * @xmatch: optional extended matching for unconfined executables names
209 * @xmatch_len: xmatch prefix len, used to determine xmatch priority
210 * @xattr_count: number of xattrs in table
211 * @xattrs: table of xattrs
212 */
213 struct aa_attachment {
214 const char *xmatch_str;
215 struct aa_policydb *xmatch;
216 unsigned int xmatch_len;
217 int xattr_count;
218 char **xattrs;
219 };
220
221 /* struct aa_profile - basic confinement data
222 * @base - base components of the profile (name, refcount, lists, lock ...)
223 * @parent: parent of profile
224 * @ns: namespace the profile is in
225 * @rename: optional profile name that this profile renamed
226 *
227 * @audit: the auditing mode of the profile
228 * @mode: the enforcement mode of the profile
229 * @path_flags: flags controlling path generation behavior
230 * @signal: the signal that should be used when kill is used
231 * @disconnected: what to prepend if attach_disconnected is specified
232 * @attach: attachment rules for the profile
233 * @rules: rules to be enforced
234 *
235 * learning_cache: the accesses learned in complain mode
236 * raw_data: rawdata of the loaded profile policy
237 * hash: cryptographic hash of the profile
238 * @dents: dentries for the profiles file entries in apparmorfs
239 * @dirname: name of the profile dir in apparmorfs
240 * @dents: set of dentries associated with the profile
241 * @data: hashtable for free-form policy aa_data
242 * @label - label this profile is an extension of
243 * @rules - label with the rule vec on its end
244 *
245 * The AppArmor profile contains the basic confinement data. Each profile
246 * has a name, and exists in a namespace. The @name and @exec_match are
247 * used to determine profile attachment against unconfined tasks. All other
248 * attachments are determined by profile X transition rules.
249 *
250 * Profiles have a hierarchy where hats and children profiles keep
251 * a reference to their parent.
252 *
253 * Profile names can not begin with a : and can not contain the \0
254 * character. If a profile name begins with / it will be considered when
255 * determining profile attachment on "unconfined" tasks.
256 */
257 struct aa_profile {
258 struct aa_policy base;
259 struct aa_profile __rcu *parent;
260
261 struct aa_ns *ns;
262 const char *rename;
263
264 enum audit_mode audit;
265 long mode;
266 u32 path_flags;
267 int signal;
268 const char *disconnected;
269
270 struct aa_attachment attach;
271
272 struct aa_loaddata *rawdata;
273 unsigned char *hash;
274 char *dirname;
275 struct dentry *dents[AAFS_PROF_SIZEOF];
276 struct rhashtable *data;
277
278 int n_rules;
279 /* special - variable length must be last entry in profile */
280 struct aa_label label;
281 };
282
283 extern enum profile_mode aa_g_profile_mode;
284
285 #define AA_MAY_LOAD_POLICY AA_MAY_APPEND
286 #define AA_MAY_REPLACE_POLICY AA_MAY_WRITE
287 #define AA_MAY_REMOVE_POLICY AA_MAY_DELETE
288
289 #define profiles_ns(P) ((P)->ns)
290 #define name_is_shared(A, B) ((A)->hname && (A)->hname == (B)->hname)
291
292 struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp);
293 struct aa_profile *aa_alloc_profile(const char *name, struct aa_proxy *proxy,
294 gfp_t gfp);
295 struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
296 gfp_t gfp);
297 struct aa_profile *__aa_new_learning_profile(struct aa_profile *parent,
298 bool hat, const char *base,
299 gfp_t gfp);
300 struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
301 const char *base, gfp_t gfp);
302 void aa_free_profile(struct aa_profile *profile);
303 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
304 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
305 size_t n);
306 struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
307 const char *fqname, size_t n);
308
309 ssize_t aa_replace_profiles(struct aa_ns *view, struct aa_label *label,
310 u32 mask, struct aa_loaddata *udata,
311 char *compressed_profile, size_t compressed_size);
312 ssize_t aa_remove_profiles(struct aa_ns *view, struct aa_label *label,
313 char *name, size_t size);
314 void __aa_profile_list_release(struct list_head *head);
315
316 #define profile_unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
317
318 /**
319 * aa_get_newest_profile - simple wrapper fn to wrap the label version
320 * @p: profile (NOT NULL)
321 *
322 * Returns refcount to newest version of the profile (maybe @p)
323 *
324 * Requires: @p must be held with a valid refcount
325 */
aa_get_newest_profile(struct aa_profile * p)326 static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
327 {
328 return labels_profile(aa_get_newest_label(&p->label));
329 }
330
RULE_MEDIATES(struct aa_ruleset * rules,unsigned char class)331 static inline aa_state_t RULE_MEDIATES(struct aa_ruleset *rules,
332 unsigned char class)
333 {
334 if (class <= AA_CLASS_LAST)
335 return rules->policy->start[class];
336 else
337 return aa_dfa_match_len(rules->policy->dfa,
338 rules->policy->start[0], &class, 1);
339 }
340
RULE_MEDIATES_v9NET(struct aa_ruleset * rules)341 static inline aa_state_t RULE_MEDIATES_v9NET(struct aa_ruleset *rules)
342 {
343 return RULE_MEDIATES(rules, AA_CLASS_NETV9);
344 }
345
RULE_MEDIATES_NET(struct aa_ruleset * rules)346 static inline aa_state_t RULE_MEDIATES_NET(struct aa_ruleset *rules)
347 {
348 /* can not use RULE_MEDIATE_v9AF here, because AF match fail
349 * can not be distiguished from class match fail, and we only
350 * fallback to checking older class on class match failure
351 */
352 aa_state_t state = RULE_MEDIATES(rules, AA_CLASS_NETV9);
353
354 /* fallback and check v7/8 if v9 is NOT mediated */
355 if (!state)
356 state = RULE_MEDIATES(rules, AA_CLASS_NET);
357 return state;
358 }
359
RULE_MEDIATES_UNIX(struct aa_ruleset * rules)360 static inline aa_state_t RULE_MEDIATES_UNIX(struct aa_ruleset *rules)
361 {
362 return RULE_MEDIATES_v9NET(rules);
363 }
364
365
366 void aa_compute_profile_mediates(struct aa_profile *profile);
profile_mediates(struct aa_profile * profile,unsigned char class)367 static inline bool profile_mediates(struct aa_profile *profile,
368 unsigned char class)
369 {
370 return label_mediates(&profile->label, class);
371 }
372
profile_mediates_safe(struct aa_profile * profile,unsigned char class)373 static inline bool profile_mediates_safe(struct aa_profile *profile,
374 unsigned char class)
375 {
376 return label_mediates_safe(&profile->label, class);
377 }
378
379 /**
380 * aa_get_profile - increment refcount on profile @p
381 * @p: profile (MAYBE NULL)
382 *
383 * Returns: pointer to @p if @p is NULL will return NULL
384 * Requires: @p must be held with valid refcount when called
385 */
aa_get_profile(struct aa_profile * p)386 static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
387 {
388 if (p)
389 kref_get(&(p->label.count.count));
390
391 return p;
392 }
393
394 /**
395 * aa_get_profile_not0 - increment refcount on profile @p found via lookup
396 * @p: profile (MAYBE NULL)
397 *
398 * Returns: pointer to @p if @p is NULL will return NULL
399 * Requires: @p must be held with valid refcount when called
400 */
aa_get_profile_not0(struct aa_profile * p)401 static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
402 {
403 if (p && kref_get_unless_zero(&p->label.count.count))
404 return p;
405
406 return NULL;
407 }
408
409 /**
410 * aa_get_profile_rcu - increment a refcount profile that can be replaced
411 * @p: pointer to profile that can be replaced (NOT NULL)
412 *
413 * Returns: pointer to a refcounted profile.
414 * else NULL if no profile
415 */
aa_get_profile_rcu(struct aa_profile __rcu ** p)416 static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
417 {
418 struct aa_profile *c;
419
420 rcu_read_lock();
421 do {
422 c = rcu_dereference(*p);
423 } while (c && !kref_get_unless_zero(&c->label.count.count));
424 rcu_read_unlock();
425
426 return c;
427 }
428
429 /**
430 * aa_put_profile - decrement refcount on profile @p
431 * @p: profile (MAYBE NULL)
432 */
aa_put_profile(struct aa_profile * p)433 static inline void aa_put_profile(struct aa_profile *p)
434 {
435 if (p)
436 kref_put(&p->label.count.count, aa_label_kref);
437 }
438
AUDIT_MODE(const struct aa_profile * profile)439 static inline int AUDIT_MODE(const struct aa_profile *profile)
440 {
441 if (aa_g_audit != AUDIT_NORMAL)
442 return aa_g_audit;
443
444 return profile->audit;
445 }
446
447 bool aa_policy_view_capable(const struct cred *subj_cred,
448 struct aa_label *label, struct aa_ns *ns);
449 bool aa_policy_admin_capable(const struct cred *subj_cred,
450 struct aa_label *label, struct aa_ns *ns);
451 int aa_may_manage_policy(const struct cred *subj_cred,
452 struct aa_label *label, struct aa_ns *ns,
453 const struct cred *ocred, u32 mask);
454 bool aa_current_policy_view_capable(struct aa_ns *ns);
455 bool aa_current_policy_admin_capable(struct aa_ns *ns);
456
457 #endif /* __AA_POLICY_H */
458