]> git.saurik.com Git - apple/xnu.git/blame - bsd/security/audit/audit_bsd.h
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / security / audit / audit_bsd.h
CommitLineData
b0d623f7 1/*-
6d2010ae 2 * Copyright (c) 2008-2009, Apple Inc.
b0d623f7
A
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)
f427ee49 37#include <kern/kalloc.h>
b0d623f7 38
0a7de745 39#if DIAGNOSTIC
b0d623f7
A
40#ifdef KASSERT
41#undef KASSERT
42#endif
43#ifdef AUDIT_KASSERT_DEBUG
0a7de745
A
44#define KASSERT(exp, msg) do { \
45 if (__builtin_expect(!(exp), 0)) { \
46 printf("%s:%d KASSERT failed: ", __FILE__, __LINE__); \
47 printf msg; \
48 printf("\n"); \
49 } \
b0d623f7
A
50} while (0)
51#else
0a7de745
A
52#define KASSERT(exp, msg) do { \
53 if (__builtin_expect(!(exp), 0)) \
54 panic msg; \
b0d623f7
A
55} while (0)
56#endif
0a7de745 57#endif /* DIAGNOSTIC */
b0d623f7 58
0a7de745 59#define AU_MAX_LCK_NAME 32
6d2010ae 60
b0d623f7
A
61#if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
62#define be16enc(p, d) *(p) = (d)
63#define be32enc(p, d) *(p) = (d)
64#define be64enc(p, d) *(p) = (d)
65
66#else /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
67
68#include <libkern/OSByteOrder.h>
69
70#define be16enc(p, d) OSWriteSwapInt16(p, 0, d)
71#define be32enc(p, d) OSWriteSwapInt32(p, 0, d)
72#define be64enc(p, d) OSWriteSwapInt64(p, 0, d)
73#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
74
75/*
0a7de745 76 * BSD kernel memory allocation.
b0d623f7 77 */
0a7de745
A
78#define AUDIT_MALLOC_DEBUG 0 /* Change to 1 for malloc debugging. */
79
80#define M_AUDITUNKNOWN 0
81#define M_AUDITDATA 1
82#define M_AUDITPATH 2
83#define M_AUDITTEXT 3
84#define M_AUDITBSM 4
85#define M_AUDITEVCLASS 5
86#define M_AUDIT_PIPE 6
87#define M_AUDIT_PIPE_ENTRY 7
88#define M_AUDIT_PIPE_PRESELECT 8
89#define M_AU_SESSION 9
90#define M_AU_EV_PLIST 10
91
92#define NUM_MALLOC_TYPES 11
93
94#ifdef M_WAITOK
95#undef M_WAITOK
96#define M_WAITOK 0x0000 /* ok to block */
316670eb 97#endif
0a7de745
A
98#ifdef M_NOWAIT
99#undef M_NOWAIT
b0d623f7 100#endif
0a7de745
A
101#define M_NOWAIT 0x0001 /* do not block */
102#ifdef M_ZERO
103#undef M_ZERO
b0d623f7 104#endif
0a7de745 105#define M_ZERO 0x0004 /* bzero the allocation */
b0d623f7 106
0a7de745
A
107#ifdef M_MAGIC
108#undef M_MAGIC
b0d623f7 109#endif
0a7de745 110#define M_MAGIC 877983977
b0d623f7 111
0a7de745
A
112#ifdef MALLOC_DEFINE
113#undef MALLOC_DEFINE
b0d623f7
A
114#endif
115#if AUDIT_MALLOC_DEBUG
0a7de745
A
116struct au_malloc_type {
117 SInt64 mt_size;
118 SInt64 mt_maxsize;
119 SInt32 mt_inuse;
120 SInt32 mt_maxused;
121 unsigned mt_type;
122 unsigned mt_magic;
123 const char *mt_shortdesc;
124 const char *mt_lastcaller;
b0d623f7 125};
0a7de745 126typedef struct au_malloc_type au_malloc_type_t;
b0d623f7 127
0a7de745
A
128#define MALLOC_DEFINE(type, shortdesc, longdesc) \
129 au_malloc_type_t audit_##type[1] = { \
130 { 0, 0, 0, 0, (type < NUM_MALLOC_TYPES) ? type :\
131 M_AUDITUNKNOWN, M_MAGIC, shortdesc, NULL } \
b0d623f7
A
132 }
133
0a7de745 134extern au_malloc_type_t *audit_malloc_types[];
b0d623f7
A
135
136#else
137
0a7de745
A
138struct au_malloc_type {
139 uint32_t mt_magic;
140 const char *mt_shortdesc;
b0d623f7 141};
0a7de745 142typedef struct au_malloc_type au_malloc_type_t;
b0d623f7 143
0a7de745
A
144#define MALLOC_DEFINE(type, shortdesc, longdesc) \
145 au_malloc_type_t audit_##type[1] = { \
146 {M_MAGIC, shortdesc } \
b0d623f7
A
147 }
148
149#endif /* AUDIT_MALLOC_DEBUG */
150
0a7de745
A
151#ifdef MALLOC_DECLARE
152#undef MALLOC_DECLARE
b0d623f7 153#endif
0a7de745 154#define MALLOC_DECLARE(type) \
b0d623f7
A
155 extern au_malloc_type_t audit_##type[]
156
157#if AUDIT_MALLOC_DEBUG
0a7de745 158#define malloc(sz, tp, fl) _audit_malloc(sz, audit_##tp, fl, __FUNCTION__)
b0d623f7
A
159void *_audit_malloc(size_t size, au_malloc_type_t *type, int flags,
160 const char *fn);
161#else
0a7de745 162#define malloc(sz, tp, fl) _audit_malloc(sz, audit_##tp, fl)
b0d623f7
A
163void *_audit_malloc(size_t size, au_malloc_type_t *type, int flags);
164#endif
165
0a7de745 166#define free(ad, tp) _audit_free(ad, audit_##tp)
b0d623f7
A
167void _audit_free(void *addr, au_malloc_type_t *type);
168
169/*
170 * BSD condition variable.
171 */
172struct cv {
0a7de745
A
173 const char *cv_description;
174 int cv_waiters;
b0d623f7
A
175};
176
177/*
178 * BSD mutex.
179 */
180struct mtx {
0a7de745 181 lck_mtx_t *mtx_lock;
6d2010ae 182#if DIAGNOSTIC
0a7de745 183 char mtx_name[AU_MAX_LCK_NAME];
6d2010ae 184#endif
b0d623f7
A
185};
186
187/*
188 * BSD rw lock.
189 */
190struct rwlock {
0a7de745 191 lck_rw_t *rw_lock;
6d2010ae 192#if DIAGNOSTIC
0a7de745 193 char rw_name[AU_MAX_LCK_NAME];
6d2010ae 194#endif
b0d623f7
A
195};
196
197/*
198 * Sleep lock.
199 */
200struct slck {
0a7de745
A
201 lck_mtx_t *sl_mtx;
202 int sl_locked;
203 int sl_waiting;
6d2010ae 204#if DIAGNOSTIC
0a7de745 205 char sl_name[AU_MAX_LCK_NAME];
6d2010ae 206#endif
b0d623f7
A
207};
208
209/*
210 * Recursive lock.
211 */
212struct rlck {
0a7de745
A
213 lck_mtx_t *rl_mtx;
214 uint32_t rl_recurse;
215 thread_t rl_thread;
6d2010ae 216#if DIAGNOSTIC
0a7de745 217 char rl_name[AU_MAX_LCK_NAME];
6d2010ae 218#endif
b0d623f7 219};
0a7de745 220
b0d623f7
A
221/*
222 * BSD condition variables functions.
223 */
224void _audit_cv_init(struct cv *cvp, const char *desc);
225void _audit_cv_destroy(struct cv *cvp);
226void _audit_cv_signal(struct cv *cvp);
227void _audit_cv_broadcast(struct cv *cvp);
228void _audit_cv_wait(struct cv *cvp, lck_mtx_t *mp, const char *desc);
229int _audit_cv_wait_sig(struct cv *cvp, lck_mtx_t *mp, const char *desc);
0a7de745
A
230int _audit_cv_wait_continuation(struct cv *cvp, lck_mtx_t *mp,
231 thread_continue_t function);
232#define cv_init(cvp, desc) _audit_cv_init(cvp, desc)
233#define cv_destroy(cvp) _audit_cv_destroy(cvp)
234#define cv_signal(cvp) _audit_cv_signal(cvp)
235#define cv_broadcast(cvp) _audit_cv_broadcast(cvp)
b0d623f7 236#define cv_broadcastpri(cvp, pri) _audit_cv_broadcast(cvp)
0a7de745
A
237#define cv_wait(cvp, mp) _audit_cv_wait(cvp, (mp)->mtx_lock, #cvp)
238#define cv_wait_sig(cvp, mp) _audit_cv_wait_sig(cvp, (mp)->mtx_lock, #cvp)
239#define cv_wait_continuation(cvp, mp, f) \
6d2010ae 240 _audit_cv_wait_continuation(cvp, (mp)->mtx_lock, f)
b0d623f7
A
241
242/*
243 * BSD Mutexes.
244 */
0a7de745
A
245void _audit_mtx_init(struct mtx *mp, const char *name);
246void _audit_mtx_destroy(struct mtx *mp);
247#define mtx_init(mp, name, type, opts) \
248 _audit_mtx_init(mp, name)
249#define mtx_lock(mp) lck_mtx_lock((mp)->mtx_lock)
250#define mtx_unlock(mp) lck_mtx_unlock((mp)->mtx_lock)
251#define mtx_destroy(mp) _audit_mtx_destroy(mp)
252#define mtx_yield(mp) lck_mtx_yield((mp)->mtx_lock)
b0d623f7
A
253
254/*
255 * Sleep lock functions.
256 */
0a7de745
A
257void _audit_slck_init(struct slck *lp, const char *grpname);
258wait_result_t _audit_slck_lock(struct slck *lp, int intr);
259void _audit_slck_unlock(struct slck *lp);
260int _audit_slck_trylock(struct slck *lp);
261void _audit_slck_assert(struct slck *lp, u_int assert);
262void _audit_slck_destroy(struct slck *lp);
263#define slck_init(lp, name) _audit_slck_init((lp), (name))
264#define slck_lock(lp) _audit_slck_lock((lp), 0)
265#define slck_lock_sig(lp) (_audit_slck_lock((lp), 1) != THREAD_AWAKENED)
266#define slck_unlock(lp) _audit_slck_unlock((lp))
267#define slck_destroy(lp) _audit_slck_destroy((lp))
b0d623f7
A
268
269/*
270 * Recursive lock functions.
271 */
0a7de745
A
272void _audit_rlck_init(struct rlck *lp, const char *grpname);
273void _audit_rlck_lock(struct rlck *lp);
274void _audit_rlck_unlock(struct rlck *lp);
275void _audit_rlck_assert(struct rlck *lp, u_int assert);
276void _audit_rlck_destroy(struct rlck *lp);
277#define rlck_init(lp, name) _audit_rlck_init((lp), (name))
278#define rlck_lock(lp) _audit_rlck_lock((lp))
279#define rlck_unlock(lp) _audit_rlck_unlock((lp))
280#define rlck_destroy(lp) _audit_rlck_destroy((lp))
b0d623f7
A
281
282/*
283 * BSD rw locks.
284 */
0a7de745
A
285void _audit_rw_init(struct rwlock *lp, const char *name);
286void _audit_rw_destroy(struct rwlock *lp);
287#define rw_init(lp, name) _audit_rw_init(lp, name)
288#define rw_rlock(lp) lck_rw_lock_shared((lp)->rw_lock)
289#define rw_runlock(lp) lck_rw_unlock_shared((lp)->rw_lock)
290#define rw_wlock(lp) lck_rw_lock_exclusive((lp)->rw_lock)
291#define rw_wunlock(lp) lck_rw_unlock_exclusive((lp)->rw_lock)
292#define rw_destroy(lp) _audit_rw_destroy(lp)
293
294#define MA_OWNED LCK_MTX_ASSERT_OWNED
295#define RA_LOCKED LCK_RW_ASSERT_HELD
296#define RA_RLOCKED LCK_RW_ASSERT_SHARED
297#define RA_WLOCKED LCK_RW_ASSERT_EXCLUSIVE
298#define SA_LOCKED LCK_RW_ASSERT_HELD
299#define SA_XLOCKED LCK_RW_ASSERT_EXCLUSIVE
300#define SL_OWNED LCK_MTX_ASSERT_OWNED
301#define SL_NOTOWNED LCK_MTX_ASSERT_NOTOWNED
b0d623f7 302#if DIAGNOSTIC
0a7de745
A
303#define mtx_assert(mp, wht) lck_mtx_assert((mp)->mtx_lock, wht)
304#define rw_assert(lp, wht) lck_rw_assert((lp)->rw_lock, wht)
305#define sx_assert(lp, wht) lck_rw_assert((lp)->sx_lock, wht)
306#define rlck_assert(lp, wht) _audit_rlck_assert((lp), wht)
307#define slck_assert(lp, wht) _audit_slck_assert((lp), wht)
b0d623f7 308#else
0a7de745
A
309#define mtx_assert(mp, wht)
310#define rw_assert(lp, wht)
311#define sx_assert(lp, wht)
312#define rlck_assert(lp, wht)
313#define slck_assert(lp, wht)
b0d623f7
A
314#endif /* DIAGNOSTIC */
315
316/*
317 * BSD (IPv6) event rate limiter.
0a7de745 318 */
b0d623f7 319int _audit_ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps);
0a7de745 320#define ppsratecheck(tv, cr, mr) _audit_ppsratecheck(tv, cr, mr)
b0d623f7
A
321
322#endif /* defined(_KERNEL) || defined(KERNEL) */
323#endif /* _SECURITY_AUDIT_AUDIT_BSD_H */