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