1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * AppArmor security module
5 * This file contains AppArmor basic permission sets definitions.
7 * Copyright 2017 Canonical Ltd.
16 #define AA_MAY_EXEC MAY_EXEC
17 #define AA_MAY_WRITE MAY_WRITE
18 #define AA_MAY_READ MAY_READ
19 #define AA_MAY_APPEND MAY_APPEND
21 #define AA_MAY_CREATE 0x0010
22 #define AA_MAY_DELETE 0x0020
23 #define AA_MAY_OPEN 0x0040
24 #define AA_MAY_RENAME 0x0080 /* pair */
26 #define AA_MAY_SETATTR 0x0100 /* meta write */
27 #define AA_MAY_GETATTR 0x0200 /* meta read */
28 #define AA_MAY_SETCRED 0x0400 /* security cred/attr */
29 #define AA_MAY_GETCRED 0x0800
31 #define AA_MAY_CHMOD 0x1000 /* pair */
32 #define AA_MAY_CHOWN 0x2000 /* pair */
33 #define AA_MAY_CHGRP 0x4000 /* pair */
34 #define AA_MAY_LOCK 0x8000 /* LINK_SUBSET overlaid */
36 #define AA_EXEC_MMAP 0x00010000
37 #define AA_MAY_MPROT 0x00020000 /* extend conditions */
38 #define AA_MAY_LINK 0x00040000 /* pair */
39 #define AA_MAY_SNAPSHOT 0x00080000 /* pair */
41 #define AA_MAY_DELEGATE
42 #define AA_CONT_MATCH 0x08000000
44 #define AA_MAY_STACK 0x10000000
45 #define AA_MAY_ONEXEC 0x20000000 /* either stack or change_profile */
46 #define AA_MAY_CHANGE_PROFILE 0x40000000
47 #define AA_MAY_CHANGEHAT 0x80000000
49 #define AA_LINK_SUBSET AA_MAY_LOCK /* overlaid */
51 #define AA_MAY_CREATE_SQPOLL AA_MAY_CREATE
52 #define AA_MAY_OVERRIDE_CRED AA_MAY_APPEND
53 #define AA_URING_PERM_MASK (AA_MAY_OVERRIDE_CRED | AA_MAY_CREATE_SQPOLL)
55 #define PERMS_CHRS_MASK (MAY_READ | MAY_WRITE | AA_MAY_CREATE | \
56 AA_MAY_DELETE | AA_MAY_LINK | AA_MAY_LOCK | \
57 AA_MAY_EXEC | AA_EXEC_MMAP | AA_MAY_APPEND)
59 #define PERMS_NAMES_MASK (PERMS_CHRS_MASK | AA_MAY_OPEN | AA_MAY_RENAME | \
60 AA_MAY_SETATTR | AA_MAY_GETATTR | AA_MAY_SETCRED | \
61 AA_MAY_GETCRED | AA_MAY_CHMOD | AA_MAY_CHOWN | \
62 AA_MAY_CHGRP | AA_MAY_MPROT | AA_MAY_SNAPSHOT | \
63 AA_MAY_STACK | AA_MAY_ONEXEC | \
64 AA_MAY_CHANGE_PROFILE | AA_MAY_CHANGEHAT)
66 extern const char aa_file_perm_chrs[];
67 extern const char *aa_file_perm_names[];
71 u32 deny; /* explicit deny, or conflict if allow also set */
73 u32 subtree; /* allow perm on full subtree only when allow is set */
74 u32 cond; /* set only when ~allow and ~deny */
76 u32 kill; /* set only when ~allow | deny */
77 u32 complain; /* accumulates only used when ~allow & ~deny */
78 u32 prompt; /* accumulates only used when ~allow & ~deny */
80 u32 audit; /* set only when allow is set */
81 u32 quiet; /* set only when ~allow | deny */
82 u32 hide; /* set only when ~allow | deny */
86 u32 tag; /* tag string index, if present */
87 u32 label; /* label string index, if present */
91 * Indexes are broken into a 24 bit index and 8 bit flag.
92 * For the index to be valid there must be a value in the flag
94 #define AA_INDEX_MASK 0x00ffffff
95 #define AA_INDEX_FLAG_MASK 0xff000000
96 #define AA_INDEX_NONE 0
98 #define ALL_PERMS_MASK 0xffffffff
99 extern struct aa_perms nullperms;
100 extern struct aa_perms allperms;
103 * aa_perms_accum_raw - accumulate perms with out masking off overlapping perms
104 * @accum - perms struct to accumulate into
105 * @addend - perms struct to add to @accum
107 static inline void aa_perms_accum_raw(struct aa_perms *accum,
108 struct aa_perms *addend)
110 accum->deny |= addend->deny;
111 accum->allow &= addend->allow & ~addend->deny;
112 accum->audit |= addend->audit & addend->allow;
113 accum->quiet &= addend->quiet & ~addend->allow;
114 accum->kill |= addend->kill & ~addend->allow;
115 accum->complain |= addend->complain & ~addend->allow & ~addend->deny;
116 accum->cond |= addend->cond & ~addend->allow & ~addend->deny;
117 accum->hide &= addend->hide & ~addend->allow;
118 accum->prompt |= addend->prompt & ~addend->allow & ~addend->deny;
119 accum->subtree |= addend->subtree & ~addend->deny;
122 accum->xindex = addend->xindex;
124 accum->tag = addend->tag;
126 accum->label = addend->label;
130 * aa_perms_accum - accumulate perms, masking off overlapping perms
131 * @accum - perms struct to accumulate into
132 * @addend - perms struct to add to @accum
134 static inline void aa_perms_accum(struct aa_perms *accum,
135 struct aa_perms *addend)
137 accum->deny |= addend->deny;
138 accum->allow &= addend->allow & ~accum->deny;
139 accum->audit |= addend->audit & accum->allow;
140 accum->quiet &= addend->quiet & ~accum->allow;
141 accum->kill |= addend->kill & ~accum->allow;
142 accum->complain |= addend->complain & ~accum->allow & ~accum->deny;
143 accum->cond |= addend->cond & ~accum->allow & ~accum->deny;
144 accum->hide &= addend->hide & ~accum->allow;
145 accum->prompt |= addend->prompt & ~accum->allow & ~accum->deny;
146 accum->subtree &= addend->subtree & ~accum->deny;
149 accum->xindex = addend->xindex;
151 accum->tag = addend->tag;
153 accum->label = addend->label;
156 #define xcheck(FN1, FN2) \
158 int e, error = FN1; \
167 * TODO: update for labels pointing to labels instead of profiles
168 * TODO: optimize the walk, currently does subwalk of L2 for each P in L1
169 * gah this doesn't allow for label compound check!!!!
171 #define xcheck_ns_profile_profile(P1, P2, FN, args...) \
174 if (P1->ns == P2->ns) \
175 ____e = FN((P1), (P2), args); \
179 #define xcheck_ns_profile_label(P, L, FN, args...) \
181 struct aa_profile *__p2; \
182 fn_for_each((L), __p2, \
183 xcheck_ns_profile_profile((P), __p2, (FN), args)); \
186 #define xcheck_ns_labels(L1, L2, FN, args...) \
188 struct aa_profile *__p1; \
189 fn_for_each((L1), __p1, FN(__p1, (L2), args)); \
192 /* Do the cross check but applying FN at the profiles level */
193 #define xcheck_labels_profiles(L1, L2, FN, args...) \
194 xcheck_ns_labels((L1), (L2), xcheck_ns_profile_label, (FN), args)
196 #define xcheck_labels(L1, L2, P, FN1, FN2) \
197 xcheck(fn_for_each((L1), (P), (FN1)), fn_for_each((L2), (P), (FN2)))
200 extern struct aa_perms default_perms;
203 void aa_perm_mask_to_str(char *str, size_t str_size, const char *chrs,
205 void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
207 void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
208 u32 chrsmask, const char * const *names, u32 namesmask);
209 void aa_apply_modes_to_perms(struct aa_profile *profile,
210 struct aa_perms *perms);
211 void aa_perms_accum(struct aa_perms *accum, struct aa_perms *addend);
212 void aa_perms_accum_raw(struct aa_perms *accum, struct aa_perms *addend);
213 void aa_profile_match_label(struct aa_profile *profile,
214 struct aa_ruleset *rules, struct aa_label *label,
215 int type, u32 request, struct aa_perms *perms);
216 int aa_check_perms(struct aa_profile *profile, struct aa_perms *perms,
217 u32 request, struct apparmor_audit_data *ad,
218 void (*cb)(struct audit_buffer *, void *));
219 #endif /* __AA_PERM_H */