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