1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * AppArmor security module
4 *
5 * This file contains AppArmor lib definitions
6 *
7 * 2017 Canonical Ltd.
8 */
9
10 #ifndef __AA_LIB_H
11 #define __AA_LIB_H
12
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 #include <linux/lsm_hooks.h>
16
17 #include "match.h"
18
19 extern struct aa_dfa *stacksplitdfa;
20
21 /*
22 * DEBUG remains global (no per profile flag) since it is mostly used in sysctl
23 * which is not related to profile accesses.
24 */
25
26 #define DEBUG_ON (aa_g_debug)
27 /*
28 * split individual debug cases out in preparation for finer grained
29 * debug controls in the future.
30 */
31 #define AA_DEBUG_LABEL DEBUG_ON
32 #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args)
33 #define AA_DEBUG(fmt, args...) \
34 do { \
35 if (DEBUG_ON) \
36 pr_debug_ratelimited("AppArmor: " fmt, ##args); \
37 } while (0)
38
39 #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X)
40
41 #define AA_BUG(X, args...) \
42 do { \
43 _Pragma("GCC diagnostic ignored \"-Wformat-zero-length\""); \
44 AA_BUG_FMT((X), "" args); \
45 _Pragma("GCC diagnostic warning \"-Wformat-zero-length\""); \
46 } while (0)
47 #ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS
48 #define AA_BUG_FMT(X, fmt, args...) \
49 WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args)
50 #else
51 #define AA_BUG_FMT(X, fmt, args...) \
52 do { \
53 BUILD_BUG_ON_INVALID(X); \
54 no_printk(fmt, ##args); \
55 } while (0)
56 #endif
57
58 #define AA_ERROR(fmt, args...) \
59 pr_err_ratelimited("AppArmor: " fmt, ##args)
60
61 /* Flag indicating whether initialization completed */
62 extern int apparmor_initialized;
63
64 /* fn's in lib */
65 const char *skipn_spaces(const char *str, size_t n);
66 char *aa_split_fqname(char *args, char **ns_name);
67 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
68 size_t *ns_len);
69 void aa_info_message(const char *str);
70
71 /* Security blob offsets */
72 extern struct lsm_blob_sizes apparmor_blob_sizes;
73
74 /**
75 * aa_strneq - compare null terminated @str to a non null terminated substring
76 * @str: a null terminated string
77 * @sub: a substring, not necessarily null terminated
78 * @len: length of @sub to compare
79 *
80 * The @str string must be full consumed for this to be considered a match
81 */
aa_strneq(const char * str,const char * sub,int len)82 static inline bool aa_strneq(const char *str, const char *sub, int len)
83 {
84 return !strncmp(str, sub, len) && !str[len];
85 }
86
87 /**
88 * aa_dfa_null_transition - step to next state after null character
89 * @dfa: the dfa to match against
90 * @start: the state of the dfa to start matching in
91 *
92 * aa_dfa_null_transition transitions to the next state after a null
93 * character which is not used in standard matching and is only
94 * used to separate pairs.
95 */
aa_dfa_null_transition(struct aa_dfa * dfa,aa_state_t start)96 static inline aa_state_t aa_dfa_null_transition(struct aa_dfa *dfa,
97 aa_state_t start)
98 {
99 /* the null transition only needs the string's null terminator byte */
100 return aa_dfa_next(dfa, start, 0);
101 }
102
path_mediated_fs(struct dentry * dentry)103 static inline bool path_mediated_fs(struct dentry *dentry)
104 {
105 return !(dentry->d_sb->s_flags & SB_NOUSER);
106 }
107
108 struct aa_str_table {
109 int size;
110 char **table;
111 };
112
113 void aa_free_str_table(struct aa_str_table *table);
114
115 struct counted_str {
116 struct kref count;
117 char name[];
118 };
119
120 #define str_to_counted(str) \
121 ((struct counted_str *)(str - offsetof(struct counted_str, name)))
122
123 #define __counted /* atm just a notation */
124
125 void aa_str_kref(struct kref *kref);
126 char *aa_str_alloc(int size, gfp_t gfp);
127
128
aa_get_str(__counted char * str)129 static inline __counted char *aa_get_str(__counted char *str)
130 {
131 if (str)
132 kref_get(&(str_to_counted(str)->count));
133
134 return str;
135 }
136
aa_put_str(__counted char * str)137 static inline void aa_put_str(__counted char *str)
138 {
139 if (str)
140 kref_put(&str_to_counted(str)->count, aa_str_kref);
141 }
142
143
144 /* struct aa_policy - common part of both namespaces and profiles
145 * @name: name of the object
146 * @hname - The hierarchical name
147 * @list: list policy object is on
148 * @profiles: head of the profiles list contained in the object
149 */
150 struct aa_policy {
151 const char *name;
152 __counted char *hname;
153 struct list_head list;
154 struct list_head profiles;
155 };
156
157 /**
158 * basename - find the last component of an hname
159 * @name: hname to find the base profile name component of (NOT NULL)
160 *
161 * Returns: the tail (base profile name) name component of an hname
162 */
basename(const char * hname)163 static inline const char *basename(const char *hname)
164 {
165 char *split;
166
167 hname = strim((char *)hname);
168 for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
169 hname = split + 2;
170
171 return hname;
172 }
173
174 /**
175 * __policy_find - find a policy by @name on a policy list
176 * @head: list to search (NOT NULL)
177 * @name: name to search for (NOT NULL)
178 *
179 * Requires: rcu_read_lock be held
180 *
181 * Returns: unrefcounted policy that match @name or NULL if not found
182 */
__policy_find(struct list_head * head,const char * name)183 static inline struct aa_policy *__policy_find(struct list_head *head,
184 const char *name)
185 {
186 struct aa_policy *policy;
187
188 list_for_each_entry_rcu(policy, head, list) {
189 if (!strcmp(policy->name, name))
190 return policy;
191 }
192 return NULL;
193 }
194
195 /**
196 * __policy_strn_find - find a policy that's name matches @len chars of @str
197 * @head: list to search (NOT NULL)
198 * @str: string to search for (NOT NULL)
199 * @len: length of match required
200 *
201 * Requires: rcu_read_lock be held
202 *
203 * Returns: unrefcounted policy that match @str or NULL if not found
204 *
205 * if @len == strlen(@strlen) then this is equiv to __policy_find
206 * other wise it allows searching for policy by a partial match of name
207 */
__policy_strn_find(struct list_head * head,const char * str,int len)208 static inline struct aa_policy *__policy_strn_find(struct list_head *head,
209 const char *str, int len)
210 {
211 struct aa_policy *policy;
212
213 list_for_each_entry_rcu(policy, head, list) {
214 if (aa_strneq(policy->name, str, len))
215 return policy;
216 }
217
218 return NULL;
219 }
220
221 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
222 const char *name, gfp_t gfp);
223 void aa_policy_destroy(struct aa_policy *policy);
224
225
226 /*
227 * fn_label_build - abstract out the build of a label transition
228 * @L: label the transition is being computed for
229 * @P: profile parameter derived from L by this macro, can be passed to FN
230 * @GFP: memory allocation type to use
231 * @FN: fn to call for each profile transition. @P is set to the profile
232 *
233 * Returns: new label on success
234 * ERR_PTR if build @FN fails
235 * NULL if label_build fails due to low memory conditions
236 *
237 * @FN must return a label or ERR_PTR on failure. NULL is not allowed
238 */
239 #define fn_label_build(L, P, GFP, FN) \
240 ({ \
241 __label__ __do_cleanup, __done; \
242 struct aa_label *__new_; \
243 \
244 if ((L)->size > 1) { \
245 /* TODO: add cache of transitions already done */ \
246 struct label_it __i; \
247 int __j, __k, __count; \
248 DEFINE_VEC(label, __lvec); \
249 DEFINE_VEC(profile, __pvec); \
250 if (vec_setup(label, __lvec, (L)->size, (GFP))) { \
251 __new_ = NULL; \
252 goto __done; \
253 } \
254 __j = 0; \
255 label_for_each(__i, (L), (P)) { \
256 __new_ = (FN); \
257 AA_BUG(!__new_); \
258 if (IS_ERR(__new_)) \
259 goto __do_cleanup; \
260 __lvec[__j++] = __new_; \
261 } \
262 for (__j = __count = 0; __j < (L)->size; __j++) \
263 __count += __lvec[__j]->size; \
264 if (!vec_setup(profile, __pvec, __count, (GFP))) { \
265 for (__j = __k = 0; __j < (L)->size; __j++) { \
266 label_for_each(__i, __lvec[__j], (P)) \
267 __pvec[__k++] = aa_get_profile(P); \
268 } \
269 __count -= aa_vec_unique(__pvec, __count, 0); \
270 if (__count > 1) { \
271 __new_ = aa_vec_find_or_create_label(__pvec,\
272 __count, (GFP)); \
273 /* only fails if out of Mem */ \
274 if (!__new_) \
275 __new_ = NULL; \
276 } else \
277 __new_ = aa_get_label(&__pvec[0]->label); \
278 vec_cleanup(profile, __pvec, __count); \
279 } else \
280 __new_ = NULL; \
281 __do_cleanup: \
282 vec_cleanup(label, __lvec, (L)->size); \
283 } else { \
284 (P) = labels_profile(L); \
285 __new_ = (FN); \
286 } \
287 __done: \
288 if (!__new_) \
289 AA_DEBUG("label build failed\n"); \
290 (__new_); \
291 })
292
293
294 #define __fn_build_in_ns(NS, P, NS_FN, OTHER_FN) \
295 ({ \
296 struct aa_label *__new; \
297 if ((P)->ns != (NS)) \
298 __new = (OTHER_FN); \
299 else \
300 __new = (NS_FN); \
301 (__new); \
302 })
303
304 #define fn_label_build_in_ns(L, P, GFP, NS_FN, OTHER_FN) \
305 ({ \
306 fn_label_build((L), (P), (GFP), \
307 __fn_build_in_ns(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \
308 })
309
310 #endif /* __AA_LIB_H */
311