This source file includes following definitions.
- aa_strneq
- aa_dfa_null_transition
- path_mediated_fs
- aa_get_str
- aa_put_str
- basename
- __policy_find
- __policy_strn_find
1
2
3
4
5
6
7
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
20
21
22
23
24 #define DEBUG_ON (aa_g_debug)
25 #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args)
26 #define AA_DEBUG(fmt, args...) \
27 do { \
28 if (DEBUG_ON) \
29 pr_debug_ratelimited("AppArmor: " fmt, ##args); \
30 } while (0)
31
32 #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X)
33
34 #define AA_BUG(X, args...) AA_BUG_FMT((X), "" args)
35 #ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS
36 #define AA_BUG_FMT(X, fmt, args...) \
37 WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args)
38 #else
39 #define AA_BUG_FMT(X, fmt, args...)
40 #endif
41
42 #define AA_ERROR(fmt, args...) \
43 pr_err_ratelimited("AppArmor: " fmt, ##args)
44
45
46 extern int apparmor_initialized;
47
48
49 const char *skipn_spaces(const char *str, size_t n);
50 char *aa_split_fqname(char *args, char **ns_name);
51 const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
52 size_t *ns_len);
53 void aa_info_message(const char *str);
54
55
56 extern struct lsm_blob_sizes apparmor_blob_sizes;
57
58
59
60
61
62
63
64
65
66 static inline bool aa_strneq(const char *str, const char *sub, int len)
67 {
68 return !strncmp(str, sub, len) && !str[len];
69 }
70
71
72
73
74
75
76
77
78
79
80 static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa,
81 unsigned int start)
82 {
83
84 return aa_dfa_next(dfa, start, 0);
85 }
86
87 static inline bool path_mediated_fs(struct dentry *dentry)
88 {
89 return !(dentry->d_sb->s_flags & SB_NOUSER);
90 }
91
92
93 struct counted_str {
94 struct kref count;
95 char name[];
96 };
97
98 #define str_to_counted(str) \
99 ((struct counted_str *)(str - offsetof(struct counted_str, name)))
100
101 #define __counted
102
103 void aa_str_kref(struct kref *kref);
104 char *aa_str_alloc(int size, gfp_t gfp);
105
106
107 static inline __counted char *aa_get_str(__counted char *str)
108 {
109 if (str)
110 kref_get(&(str_to_counted(str)->count));
111
112 return str;
113 }
114
115 static inline void aa_put_str(__counted char *str)
116 {
117 if (str)
118 kref_put(&str_to_counted(str)->count, aa_str_kref);
119 }
120
121
122
123
124
125
126
127
128 struct aa_policy {
129 const char *name;
130 __counted char *hname;
131 struct list_head list;
132 struct list_head profiles;
133 };
134
135
136
137
138
139
140
141 static inline const char *basename(const char *hname)
142 {
143 char *split;
144
145 hname = strim((char *)hname);
146 for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
147 hname = split + 2;
148
149 return hname;
150 }
151
152
153
154
155
156
157
158
159
160
161 static inline struct aa_policy *__policy_find(struct list_head *head,
162 const char *name)
163 {
164 struct aa_policy *policy;
165
166 list_for_each_entry_rcu(policy, head, list) {
167 if (!strcmp(policy->name, name))
168 return policy;
169 }
170 return NULL;
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 static inline struct aa_policy *__policy_strn_find(struct list_head *head,
187 const char *str, int len)
188 {
189 struct aa_policy *policy;
190
191 list_for_each_entry_rcu(policy, head, list) {
192 if (aa_strneq(policy->name, str, len))
193 return policy;
194 }
195
196 return NULL;
197 }
198
199 bool aa_policy_init(struct aa_policy *policy, const char *prefix,
200 const char *name, gfp_t gfp);
201 void aa_policy_destroy(struct aa_policy *policy);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 #define fn_label_build(L, P, GFP, FN) \
218 ({ \
219 __label__ __cleanup, __done; \
220 struct aa_label *__new_; \
221 \
222 if ((L)->size > 1) { \
223 \
224 struct label_it __i; \
225 int __j, __k, __count; \
226 DEFINE_VEC(label, __lvec); \
227 DEFINE_VEC(profile, __pvec); \
228 if (vec_setup(label, __lvec, (L)->size, (GFP))) { \
229 __new_ = NULL; \
230 goto __done; \
231 } \
232 __j = 0; \
233 label_for_each(__i, (L), (P)) { \
234 __new_ = (FN); \
235 AA_BUG(!__new_); \
236 if (IS_ERR(__new_)) \
237 goto __cleanup; \
238 __lvec[__j++] = __new_; \
239 } \
240 for (__j = __count = 0; __j < (L)->size; __j++) \
241 __count += __lvec[__j]->size; \
242 if (!vec_setup(profile, __pvec, __count, (GFP))) { \
243 for (__j = __k = 0; __j < (L)->size; __j++) { \
244 label_for_each(__i, __lvec[__j], (P)) \
245 __pvec[__k++] = aa_get_profile(P); \
246 } \
247 __count -= aa_vec_unique(__pvec, __count, 0); \
248 if (__count > 1) { \
249 __new_ = aa_vec_find_or_create_label(__pvec,\
250 __count, (GFP)); \
251 \
252 if (!__new_) \
253 __new_ = NULL; \
254 } else \
255 __new_ = aa_get_label(&__pvec[0]->label); \
256 vec_cleanup(profile, __pvec, __count); \
257 } else \
258 __new_ = NULL; \
259 __cleanup: \
260 vec_cleanup(label, __lvec, (L)->size); \
261 } else { \
262 (P) = labels_profile(L); \
263 __new_ = (FN); \
264 } \
265 __done: \
266 if (!__new_) \
267 AA_DEBUG("label build failed\n"); \
268 (__new_); \
269 })
270
271
272 #define __fn_build_in_ns(NS, P, NS_FN, OTHER_FN) \
273 ({ \
274 struct aa_label *__new; \
275 if ((P)->ns != (NS)) \
276 __new = (OTHER_FN); \
277 else \
278 __new = (NS_FN); \
279 (__new); \
280 })
281
282 #define fn_label_build_in_ns(L, P, GFP, NS_FN, OTHER_FN) \
283 ({ \
284 fn_label_build((L), (P), (GFP), \
285 __fn_build_in_ns(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \
286 })
287
288 #endif