2 * AppArmor security module
4 * This file contains AppArmor lib definitions
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
17 #include <linux/slab.h>
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
28 #define write_is_locked(X) !write_can_lock(X)
30 #define write_is_locked(X) (1)
31 #endif /* CONFIG_SMP */
34 * DEBUG remains global (no per profile flag) since it is mostly used in sysctl
35 * which is not related to profile accesses.
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...) \
43 dbg_printk("AppArmor: " fmt, ##args); \
46 #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __FUNCTION__, #X)
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 )
52 #define AA_ERROR(fmt, args...) \
54 if (printk_ratelimit()) \
55 printk(KERN_ERR "AppArmor: " fmt, ##args); \
58 /* Flag indicating whether initialization completed */
59 extern int apparmor_initialized __initdata
;
62 const char *skipn_spaces(const char *str
, size_t n
);
63 char *aa_split_fqname(char *args
, char **ns_name
);
64 const char *aa_splitn_fqname(const char *fqname
, size_t n
, const char **ns_name
,
66 void aa_info_message(const char *str
);
67 void *__aa_kvmalloc(size_t size
, gfp_t flags
);
69 static inline void *kvmalloc(size_t size
)
71 return __aa_kvmalloc(size
, 0);
74 static inline void *kvzalloc(size_t size
)
76 return __aa_kvmalloc(size
, __GFP_ZERO
);
79 /* returns 0 if kref not incremented */
80 static inline int kref_get_not0(struct kref
*kref
)
82 return atomic_inc_not_zero(&kref
->refcount
);
86 * aa_strneq - compare null terminated @str to a non null terminated substring
87 * @str: a null terminated string
88 * @sub: a substring, not necessarily null terminated
89 * @len: length of @sub to compare
91 * The @str string must be full consumed for this to be considered a match
93 static inline bool aa_strneq(const char *str
, const char *sub
, int len
)
95 return !strncmp(str
, sub
, len
) && !str
[len
];
99 * aa_dfa_null_transition - step to next state after null character
100 * @dfa: the dfa to match against
101 * @start: the state of the dfa to start matching in
103 * aa_dfa_null_transition transitions to the next state after a null
104 * character which is not used in standard matching and is only
105 * used to separate pairs.
107 static inline unsigned int aa_dfa_null_transition(struct aa_dfa
*dfa
,
110 /* the null transition only needs the string's null terminator byte */
111 return aa_dfa_next(dfa
, start
, 0);
114 static inline bool path_mediated_fs(struct dentry
*dentry
)
116 return !(dentry
->d_sb
->s_flags
& MS_NOUSER
);
125 #define str_to_counted(str) \
126 ((struct counted_str *)(str - offsetof(struct counted_str,name)))
128 #define __counted /* atm just a notation */
130 void aa_str_kref(struct kref
*kref
);
131 char *aa_str_alloc(int size
, gfp_t gfp
);
134 static inline __counted
char *aa_get_str(__counted
char *str
)
137 kref_get(&(str_to_counted(str
)->count
));
142 static inline void aa_put_str(__counted
char *str
)
145 kref_put(&str_to_counted(str
)->count
, aa_str_kref
);
148 const char *aa_imode_name(umode_t mode
);
151 /* struct aa_policy - common part of both namespaces and profiles
152 * @name: name of the object
153 * @hname - The hierarchical name, NOTE: is .name of struct counted_str
154 * @list: list policy object is on
155 * @profiles: head of the profiles list contained in the object
159 __counted
char *hname
;
160 struct list_head list
;
161 struct list_head profiles
;
164 #define aa_peer_name(peer) (peer)->base.hname
167 * basename - find the last component of an hname
168 * @name: hname to find the base profile name component of (NOT NULL)
170 * Returns: the tail (base profile name) name component of an hname
172 static inline const char *basename(const char *hname
)
175 hname
= strim((char *)hname
);
176 for (split
= strstr(hname
, "//"); split
; split
= strstr(hname
, "//"))
183 * __policy_find - find a policy by @name on a policy list
184 * @head: list to search (NOT NULL)
185 * @name: name to search for (NOT NULL)
187 * Requires: rcu_read_lock be held
189 * Returns: unrefcounted policy that match @name or NULL if not found
191 static inline struct aa_policy
*__policy_find(struct list_head
*head
,
194 struct aa_policy
*policy
;
196 list_for_each_entry_rcu(policy
, head
, list
) {
197 if (!strcmp(policy
->name
, name
))
204 * __policy_strn_find - find a policy that's name matches @len chars of @str
205 * @head: list to search (NOT NULL)
206 * @str: string to search for (NOT NULL)
207 * @len: length of match required
209 * Requires: rcu_read_lock be held
211 * Returns: unrefcounted policy that match @str or NULL if not found
213 * if @len == strlen(@strlen) then this is equiv to __policy_find
214 * other wise it allows searching for policy by a partial match of name
216 static inline struct aa_policy
*__policy_strn_find(struct list_head
*head
,
217 const char *str
, int len
)
219 struct aa_policy
*policy
;
221 list_for_each_entry_rcu(policy
, head
, list
) {
222 if (aa_strneq(policy
->name
, str
, len
))
229 bool aa_policy_init(struct aa_policy
*policy
, const char *prefix
,
230 const char *name
, gfp_t gfp
);
231 void aa_policy_destroy(struct aa_policy
*policy
);
235 * fn_label_build - abstract out the build of a label transition
236 * @L: label the transition is being computed for
237 * @P: profile parameter derived from L by this macro, can be passed to FN
238 * @GFP: memory allocation type to use
239 * @FN: fn to call for each profile transition. @P is set to the profile
241 * Returns: new label on success
242 * ERR_PTR if build @FN fails
243 * NULL if label_build fails due to low memory conditions
245 * @FN must return a label or ERR_PTR on failure. NULL is not allowed
247 #define fn_label_build(L, P, GFP, FN) \
249 __label__ __cleanup, __done; \
250 struct aa_label *__new_; \
252 if ((L)->size > 1) { \
253 /* TODO: add cache of transitions already done */ \
254 struct label_it __i; \
255 int __j, __k, __count; \
256 DEFINE_VEC(label, __lvec); \
257 DEFINE_VEC(profile, __pvec); \
258 if (vec_setup(label, __lvec, (L)->size, (GFP))) { \
263 label_for_each(__i, (L), (P)) { \
266 if (IS_ERR(__new_)) \
268 __lvec[__j++] = __new_; \
270 for (__j = __count = 0; __j < (L)->size; __j++) \
271 __count += __lvec[__j]->size; \
272 if (!vec_setup(profile, __pvec, __count, (GFP))) { \
273 for (__j = __k = 0; __j < (L)->size; __j++) { \
274 label_for_each(__i, __lvec[__j], (P)) \
275 __pvec[__k++] = aa_get_profile(P); \
277 __count -= aa_vec_unique(__pvec, __count, 0); \
279 __new_ = aa_vec_find_or_create_label(__pvec,\
281 /* only fails if out of Mem */ \
285 __new_ = aa_get_label(&__pvec[0]->label); \
286 vec_cleanup(profile, __pvec, __count); \
290 vec_cleanup(label, __lvec, (L)->size); \
292 (P) = labels_profile(L); \
297 AA_DEBUG("label build failed\n"); \
302 #define __fn_build_in_ns(NS, P, NS_FN, OTHER_FN) \
304 struct aa_label *__new; \
305 if ((P)->ns != (NS)) \
306 __new = (OTHER_FN); \
312 #define fn_label_build_in_ns(L, P, GFP, NS_FN, OTHER_FN) \
314 fn_label_build((L), (P), (GFP), \
315 __fn_build_in_ns(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \
318 #endif /* __AA_LIB_H */