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