]> git.saurik.com Git - apple/xnu.git/blob - bsd/security/audit/audit_bsd.h
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / security / audit / audit_bsd.h
1 /*-
2 * Copyright (c) 2008, Apple Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef _SECURITY_AUDIT_AUDIT_BSD_H
31 #define _SECURITY_AUDIT_AUDIT_BSD_H
32
33 #include <sys/cdefs.h>
34 #include <machine/endian.h>
35
36 #if defined(_KERNEL) || defined(KERNEL)
37
38 #if DIAGNOSTIC
39 #ifdef KASSERT
40 #undef KASSERT
41 #endif
42 #ifdef AUDIT_KASSERT_DEBUG
43 #define KASSERT(exp, msg) do { \
44 if (__builtin_expect(!(exp), 0)) { \
45 printf("%s:%d KASSERT failed: ", __FILE__, __LINE__); \
46 printf msg; \
47 printf("\n"); \
48 } \
49 } while (0)
50 #else
51 #define KASSERT(exp, msg) do { \
52 if (__builtin_expect(!(exp), 0)) \
53 panic msg; \
54 } while (0)
55 #endif
56 #endif /* DIAGNOSTIC */
57
58 #if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
59 #define be16enc(p, d) *(p) = (d)
60 #define be32enc(p, d) *(p) = (d)
61 #define be64enc(p, d) *(p) = (d)
62
63 #else /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
64
65 #include <libkern/OSByteOrder.h>
66
67 #define be16enc(p, d) OSWriteSwapInt16(p, 0, d)
68 #define be32enc(p, d) OSWriteSwapInt32(p, 0, d)
69 #define be64enc(p, d) OSWriteSwapInt64(p, 0, d)
70 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
71
72 /*
73 * BSD kernel memory allocation.
74 */
75 #define AUDIT_MALLOC_DEBUG 0 /* Change to 1 for malloc debugging. */
76
77 #define M_AUDITUNKNOWN 0
78 #define M_AUDITDATA 1
79 #define M_AUDITPATH 2
80 #define M_AUDITTEXT 3
81 #define M_AUDITBSM 4
82 #define M_AUDITEVCLASS 5
83 #define M_AUDIT_PIPE 6
84 #define M_AUDIT_PIPE_ENTRY 7
85 #define M_AUDIT_PIPE_PRESELECT 8
86 #define M_AU_SESSION 9
87 #define M_AU_EV_PLIST 10
88
89 #define NUM_MALLOC_TYPES 11
90
91 #ifdef M_NOWAIT
92 #undef M_NOWAIT
93 #endif
94 #define M_NOWAIT 0x0000 /* do not block */
95 #ifdef M_WAITOK
96 #undef M_WAITOK
97 #define M_WAITOK 0x0001 /* ok to block */
98 #endif
99 #ifdef M_ZERO
100 #undef M_ZERO
101 #endif
102 #define M_ZERO 0x0004 /* bzero the allocation */
103
104 #ifdef M_MAGIC
105 #undef M_MAGIC
106 #endif
107 #define M_MAGIC 877983977
108
109 #ifdef MALLOC_DEFINE
110 #undef MALLOC_DEFINE
111 #endif
112 #if AUDIT_MALLOC_DEBUG
113 struct au_malloc_type {
114 SInt64 mt_size;
115 SInt64 mt_maxsize;
116 SInt32 mt_inuse;
117 SInt32 mt_maxused;
118 unsigned mt_type;
119 unsigned mt_magic;
120 const char *mt_shortdesc;
121 const char *mt_lastcaller;
122 };
123 typedef struct au_malloc_type au_malloc_type_t;
124
125 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
126 au_malloc_type_t audit_##type[1] = { \
127 { 0, 0, 0, 0, (type < NUM_MALLOC_TYPES) ? type :\
128 M_AUDITUNKNOWN, M_MAGIC, shortdesc, NULL } \
129 }
130
131 extern au_malloc_type_t *audit_malloc_types[];
132
133 #else
134
135 struct au_malloc_type {
136 uint32_t mt_magic;
137 const char *mt_shortdesc;
138 };
139 typedef struct au_malloc_type au_malloc_type_t;
140
141 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
142 au_malloc_type_t audit_##type[1] = { \
143 {M_MAGIC, shortdesc } \
144 }
145
146 #endif /* AUDIT_MALLOC_DEBUG */
147
148 #ifdef MALLOC_DECLARE
149 #undef MALLOC_DECLARE
150 #endif
151 #define MALLOC_DECLARE(type) \
152 extern au_malloc_type_t audit_##type[]
153
154 #if AUDIT_MALLOC_DEBUG
155 #define malloc(sz, tp, fl) _audit_malloc(sz, audit_##tp, fl, __FUNCTION__)
156 void *_audit_malloc(size_t size, au_malloc_type_t *type, int flags,
157 const char *fn);
158 #else
159 #define malloc(sz, tp, fl) _audit_malloc(sz, audit_##tp, fl)
160 void *_audit_malloc(size_t size, au_malloc_type_t *type, int flags);
161 #endif
162
163 #define free(ad, tp) _audit_free(ad, audit_##tp)
164 void _audit_free(void *addr, au_malloc_type_t *type);
165
166 /*
167 * BSD condition variable.
168 */
169 struct cv {
170 const char *cv_description;
171 int cv_waiters;
172 };
173
174 /*
175 * BSD mutex.
176 */
177 struct mtx {
178 lck_mtx_t *mtx_lock;
179 lck_grp_t *mtx_grp;
180 };
181
182 /*
183 * BSD rw lock.
184 */
185 struct rwlock {
186 lck_rw_t *rw_lock;
187 lck_grp_t *rw_grp;
188 };
189
190 /*
191 * Sleep lock.
192 */
193 struct slck {
194 lck_mtx_t *sl_mtx;
195 lck_grp_t *sl_grp;
196 int sl_locked;
197 int sl_waiting;
198 };
199
200 /*
201 * Recursive lock.
202 */
203 struct rlck {
204 lck_mtx_t *rl_mtx;
205 lck_grp_t *rl_grp;
206 uint32_t rl_recurse;
207 thread_t rl_thread;
208 };
209
210 /*
211 * BSD condition variables functions.
212 */
213 void _audit_cv_init(struct cv *cvp, const char *desc);
214 void _audit_cv_destroy(struct cv *cvp);
215 void _audit_cv_signal(struct cv *cvp);
216 void _audit_cv_broadcast(struct cv *cvp);
217 void _audit_cv_wait(struct cv *cvp, lck_mtx_t *mp, const char *desc);
218 int _audit_cv_wait_sig(struct cv *cvp, lck_mtx_t *mp, const char *desc);
219 #define cv_init(cvp, desc) _audit_cv_init(cvp, desc)
220 #define cv_destroy(cvp) _audit_cv_destroy(cvp)
221 #define cv_signal(cvp) _audit_cv_signal(cvp)
222 #define cv_broadcast(cvp) _audit_cv_broadcast(cvp)
223 #define cv_broadcastpri(cvp, pri) _audit_cv_broadcast(cvp)
224 #define cv_wait(cvp, mp) _audit_cv_wait(cvp, (mp)->mtx_lock, #cvp)
225 #define cv_wait_sig(cvp, mp) _audit_cv_wait_sig(cvp, (mp)->mtx_lock, #cvp)
226
227 /*
228 * BSD Mutexes.
229 */
230 #define LOCK_MAX_NAME 64
231 #define mtx_init(mp, name, type, opts) do { \
232 (mp)->mtx_grp = lck_grp_alloc_init(name, LCK_GRP_ATTR_NULL); \
233 (mp)->mtx_lock = lck_mtx_alloc_init((mp)->mtx_grp, \
234 LCK_ATTR_NULL); \
235 } while(0)
236 #define mtx_lock(mp) lck_mtx_lock((mp)->mtx_lock)
237 #define mtx_unlock(mp) lck_mtx_unlock((mp)->mtx_lock)
238 #define mtx_destroy(mp) do { \
239 if ((mp)->mtx_lock) { \
240 lck_mtx_free((mp)->mtx_lock, (mp)->mtx_grp); \
241 (mp)->mtx_lock = 0; \
242 } \
243 if ((mp)->mtx_grp) { \
244 lck_grp_free((mp)->mtx_grp); \
245 (mp)->mtx_grp = 0; \
246 } \
247 } while (0)
248
249 /*
250 * Sleep lock functions.
251 */
252 void _audit_slck_init(struct slck *lp, const char *grpname);
253 wait_result_t _audit_slck_lock(struct slck *lp, int intr);
254 void _audit_slck_unlock(struct slck *lp);
255 int _audit_slck_trylock(struct slck *lp);
256 void _audit_slck_assert(struct slck *lp, u_int assert);
257 void _audit_slck_destroy(struct slck *lp);
258 #define slck_init(lp, name) _audit_slck_init((lp), (name))
259 #define slck_lock(lp) _audit_slck_lock((lp), 0)
260 #define slck_lock_sig(lp) (_audit_slck_lock((lp), 1) != THREAD_AWAKENED)
261 #define slck_unlock(lp) _audit_slck_unlock((lp))
262 #define slck_destroy(lp) _audit_slck_destroy((lp))
263
264 /*
265 * Recursive lock functions.
266 */
267 void _audit_rlck_init(struct rlck *lp, const char *grpname);
268 void _audit_rlck_lock(struct rlck *lp);
269 void _audit_rlck_unlock(struct rlck *lp);
270 void _audit_rlck_assert(struct rlck *lp, u_int assert);
271 void _audit_rlck_destroy(struct rlck *lp);
272 #define rlck_init(lp, name) _audit_rlck_init((lp), (name))
273 #define rlck_lock(lp) _audit_rlck_lock((lp))
274 #define rlck_unlock(lp) _audit_rlck_unlock((lp))
275 #define rlck_destroy(lp) _audit_rlck_destroy((lp))
276
277 /*
278 * BSD rw locks.
279 */
280 #define rw_init(lp, name) do { \
281 (lp)->rw_grp = lck_grp_alloc_init(name, LCK_GRP_ATTR_NULL); \
282 (lp)->rw_lock = lck_rw_alloc_init((lp)->rw_grp, \
283 LCK_ATTR_NULL); \
284 } while(0)
285 #define rw_rlock(lp) lck_rw_lock_shared((lp)->rw_lock)
286 #define rw_runlock(lp) lck_rw_unlock_shared((lp)->rw_lock)
287 #define rw_wlock(lp) lck_rw_lock_exclusive((lp)->rw_lock)
288 #define rw_wunlock(lp) lck_rw_unlock_exclusive((lp)->rw_lock)
289 #define rw_destroy(lp) do { \
290 if ((lp)->rw_lock) { \
291 lck_rw_free((lp)->rw_lock, (lp)->rw_grp); \
292 (lp)->rw_lock = 0; \
293 } \
294 if ((lp)->rw_grp) { \
295 lck_grp_free((lp)->rw_grp); \
296 (lp)->rw_grp = 0; \
297 } \
298 } while (0)
299
300 #define MA_OWNED LCK_MTX_ASSERT_OWNED
301 #define RA_LOCKED LCK_RW_ASSERT_HELD
302 #define RA_RLOCKED LCK_RW_ASSERT_SHARED
303 #define RA_WLOCKED LCK_RW_ASSERT_EXCLUSIVE
304 #define SA_LOCKED LCK_RW_ASSERT_HELD
305 #define SA_XLOCKED LCK_RW_ASSERT_EXCLUSIVE
306 #define SL_OWNED LCK_MTX_ASSERT_OWNED
307 #define SL_NOTOWNED LCK_MTX_ASSERT_NOTOWNED
308 #if DIAGNOSTIC
309 #define mtx_assert(mp, wht) lck_mtx_assert((mp)->mtx_lock, wht)
310 #define rw_assert(lp, wht) lck_rw_assert((lp)->rw_lock, wht)
311 #define sx_assert(lp, wht) lck_rw_assert((lp)->sx_lock, wht)
312 #define rlck_assert(lp, wht) _audit_rlck_assert((lp), wht)
313 #define slck_assert(lp, wht) _audit_slck_assert((lp), wht)
314 #else
315 #define mtx_assert(mp, wht)
316 #define rw_assert(lp, wht)
317 #define sx_assert(lp, wht)
318 #define rlck_assert(lp, wht)
319 #define slck_assert(lp, wht)
320 #endif /* DIAGNOSTIC */
321
322 /*
323 * BSD (IPv6) event rate limiter.
324 */
325 int _audit_ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps);
326 #define ppsratecheck(tv, cr, mr) _audit_ppsratecheck(tv, cr, mr)
327
328 #endif /* defined(_KERNEL) || defined(KERNEL) */
329 #endif /* _SECURITY_AUDIT_AUDIT_BSD_H */