]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /*- |
6d2010ae | 2 | * Copyright (c) 2008-2010 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 | #include <string.h> | |
31 | ||
32 | #include <sys/kernel.h> | |
33 | #include <sys/proc.h> | |
34 | #include <sys/systm.h> | |
35 | ||
36 | #include <kern/host.h> | |
37 | #include <kern/kalloc.h> | |
38 | #include <kern/locks.h> | |
39 | #include <kern/sched_prim.h> | |
40 | ||
41 | #include <libkern/OSAtomic.h> | |
42 | ||
43 | #include <bsm/audit.h> | |
44 | #include <bsm/audit_internal.h> | |
45 | ||
46 | #include <security/audit/audit_bsd.h> | |
47 | #include <security/audit/audit.h> | |
48 | #include <security/audit/audit_private.h> | |
49 | ||
50 | #include <mach/host_priv.h> | |
51 | #include <mach/host_special_ports.h> | |
52 | #include <mach/audit_triggers_server.h> | |
f427ee49 | 53 | #include <mach/audit_triggers_types.h> |
b0d623f7 | 54 | |
a39ff7e2 A |
55 | #include <os/overflow.h> |
56 | ||
39037602 A |
57 | extern void ipc_port_release_send(ipc_port_t port); |
58 | ||
b0d623f7 A |
59 | #if CONFIG_AUDIT |
60 | struct mhdr { | |
0a7de745 A |
61 | size_t mh_size; |
62 | au_malloc_type_t *mh_type; | |
63 | u_long mh_magic; | |
64 | char mh_data[0]; | |
b0d623f7 A |
65 | }; |
66 | ||
6d2010ae | 67 | /* |
0a7de745 | 68 | * The lock group for the audit subsystem. |
6d2010ae | 69 | */ |
f427ee49 | 70 | static LCK_GRP_DECLARE(audit_lck_grp, "Audit"); |
6d2010ae | 71 | |
0a7de745 | 72 | #define AUDIT_MHMAGIC 0x4D656C53 |
b0d623f7 A |
73 | |
74 | #if AUDIT_MALLOC_DEBUG | |
0a7de745 A |
75 | #define AU_MAX_SHORTDESC 20 |
76 | #define AU_MAX_LASTCALLER 20 | |
b0d623f7 | 77 | struct au_malloc_debug_info { |
0a7de745 A |
78 | SInt64 md_size; |
79 | SInt64 md_maxsize; | |
80 | SInt32 md_inuse; | |
81 | SInt32 md_maxused; | |
82 | unsigned md_type; | |
83 | unsigned md_magic; | |
84 | char md_shortdesc[AU_MAX_SHORTDESC]; | |
85 | char md_lastcaller[AU_MAX_LASTCALLER]; | |
b0d623f7 A |
86 | }; |
87 | typedef struct au_malloc_debug_info au_malloc_debug_info_t; | |
88 | ||
0a7de745 | 89 | au_malloc_type_t *audit_malloc_types[NUM_MALLOC_TYPES]; |
b0d623f7 A |
90 | |
91 | static int audit_sysctl_malloc_debug(struct sysctl_oid *oidp, void *arg1, | |
92 | int arg2, struct sysctl_req *req); | |
93 | ||
94 | SYSCTL_PROC(_kern, OID_AUTO, audit_malloc_debug, CTLFLAG_RD, NULL, 0, | |
95 | audit_sysctl_malloc_debug, "S,audit_malloc_debug", | |
96 | "Current malloc debug info for auditing."); | |
97 | ||
0a7de745 | 98 | #define AU_MALLOC_DBINFO_SZ \ |
b0d623f7 A |
99 | (NUM_MALLOC_TYPES * sizeof(au_malloc_debug_info_t)) |
100 | ||
101 | /* | |
0a7de745 | 102 | * Copy out the malloc debug info via the sysctl interface. The userland code |
b0d623f7 A |
103 | * is something like the following: |
104 | * | |
105 | * error = sysctlbyname("kern.audit_malloc_debug", buffer_ptr, &buffer_len, | |
106 | * NULL, 0); | |
107 | */ | |
108 | static int | |
109 | audit_sysctl_malloc_debug(__unused struct sysctl_oid *oidp, __unused void *arg1, | |
110 | __unused int arg2, struct sysctl_req *req) | |
111 | { | |
112 | int i; | |
113 | size_t sz; | |
114 | au_malloc_debug_info_t *amdi_ptr, *nxt_ptr; | |
115 | int err; | |
116 | ||
117 | /* | |
118 | * This provides a read-only node. | |
119 | */ | |
0a7de745 A |
120 | if (req->newptr != USER_ADDR_NULL) { |
121 | return EPERM; | |
122 | } | |
b0d623f7 A |
123 | |
124 | /* | |
0a7de745 | 125 | * If just querying then return the space required. |
b0d623f7 A |
126 | */ |
127 | if (req->oldptr == USER_ADDR_NULL) { | |
0a7de745 A |
128 | req->oldidx = AU_MALLOC_DBINFO_SZ; |
129 | return 0; | |
b0d623f7 A |
130 | } |
131 | ||
132 | /* | |
133 | * Alloc a temporary buffer. | |
134 | */ | |
0a7de745 A |
135 | if (req->oldlen < AU_MALLOC_DBINFO_SZ) { |
136 | return ENOMEM; | |
137 | } | |
f427ee49 | 138 | amdi_ptr = kheap_alloc(KHEAP_TEMP, AU_MALLOC_DBINFO_SZ, Z_WAITOK | Z_ZERO); |
0a7de745 A |
139 | if (amdi_ptr == NULL) { |
140 | return ENOMEM; | |
141 | } | |
b0d623f7 A |
142 | |
143 | /* | |
0a7de745 | 144 | * Build the record array. |
b0d623f7 A |
145 | */ |
146 | sz = 0; | |
147 | nxt_ptr = amdi_ptr; | |
0a7de745 A |
148 | for (i = 0; i < NUM_MALLOC_TYPES; i++) { |
149 | if (audit_malloc_types[i] == NULL) { | |
b0d623f7 | 150 | continue; |
0a7de745 | 151 | } |
b0d623f7 A |
152 | if (audit_malloc_types[i]->mt_magic != M_MAGIC) { |
153 | nxt_ptr->md_magic = audit_malloc_types[i]->mt_magic; | |
154 | continue; | |
155 | } | |
156 | nxt_ptr->md_magic = audit_malloc_types[i]->mt_magic; | |
157 | nxt_ptr->md_size = audit_malloc_types[i]->mt_size; | |
158 | nxt_ptr->md_maxsize = audit_malloc_types[i]->mt_maxsize; | |
159 | nxt_ptr->md_inuse = (int)audit_malloc_types[i]->mt_inuse; | |
160 | nxt_ptr->md_maxused = (int)audit_malloc_types[i]->mt_maxused; | |
161 | strlcpy(nxt_ptr->md_shortdesc, | |
162 | audit_malloc_types[i]->mt_shortdesc, AU_MAX_SHORTDESC - 1); | |
163 | strlcpy(nxt_ptr->md_lastcaller, | |
0a7de745 | 164 | audit_malloc_types[i]->mt_lastcaller, AU_MAX_LASTCALLER - 1); |
b0d623f7 A |
165 | sz += sizeof(au_malloc_debug_info_t); |
166 | nxt_ptr++; | |
167 | } | |
168 | ||
169 | req->oldlen = sz; | |
170 | err = SYSCTL_OUT(req, amdi_ptr, sz); | |
f427ee49 | 171 | kheap_free(KHEAP_TEMP, amdi_ptr, AU_MALLOC_DBINFO_SZ); |
b0d623f7 | 172 | |
0a7de745 | 173 | return err; |
b0d623f7 A |
174 | } |
175 | #endif /* AUDIT_MALLOC_DEBUG */ | |
0a7de745 | 176 | |
b0d623f7 A |
177 | /* |
178 | * BSD malloc() | |
0a7de745 | 179 | * |
b0d623f7 A |
180 | * If the M_NOWAIT flag is set then it may not block and return NULL. |
181 | * If the M_ZERO flag is set then zero out the buffer. | |
182 | */ | |
183 | void * | |
184 | #if AUDIT_MALLOC_DEBUG | |
185 | _audit_malloc(size_t size, au_malloc_type_t *type, int flags, const char *fn) | |
186 | #else | |
0a7de745 | 187 | _audit_malloc(size_t size, au_malloc_type_t * type, int flags) |
b0d623f7 A |
188 | #endif |
189 | { | |
0a7de745 A |
190 | struct mhdr *hdr; |
191 | size_t memsize; | |
a39ff7e2 | 192 | if (os_add_overflow(sizeof(*hdr), size, &memsize)) { |
0a7de745 | 193 | return NULL; |
a39ff7e2 | 194 | } |
b0d623f7 | 195 | |
0a7de745 A |
196 | if (size == 0) { |
197 | return NULL; | |
198 | } | |
f427ee49 | 199 | hdr = kheap_alloc(KHEAP_AUDIT, memsize, flags); |
0a7de745 A |
200 | if (hdr == NULL) { |
201 | return NULL; | |
b0d623f7 | 202 | } |
6d2010ae A |
203 | hdr->mh_size = memsize; |
204 | hdr->mh_type = type; | |
205 | hdr->mh_magic = AUDIT_MHMAGIC; | |
b0d623f7 A |
206 | #if AUDIT_MALLOC_DEBUG |
207 | if (type != NULL && type->mt_type < NUM_MALLOC_TYPES) { | |
208 | OSAddAtomic64(memsize, &type->mt_size); | |
209 | type->mt_maxsize = max(type->mt_size, type->mt_maxsize); | |
210 | OSAddAtomic(1, &type->mt_inuse); | |
211 | type->mt_maxused = max(type->mt_inuse, type->mt_maxused); | |
212 | type->mt_lastcaller = fn; | |
213 | audit_malloc_types[type->mt_type] = type; | |
214 | } | |
215 | #endif /* AUDIT_MALLOC_DEBUG */ | |
0a7de745 | 216 | return hdr->mh_data; |
b0d623f7 A |
217 | } |
218 | ||
219 | /* | |
220 | * BSD free() | |
221 | */ | |
222 | void | |
223 | #if AUDIT_MALLOC_DEBUG | |
224 | _audit_free(void *addr, au_malloc_type_t *type) | |
225 | #else | |
226 | _audit_free(void *addr, __unused au_malloc_type_t *type) | |
227 | #endif | |
228 | { | |
229 | struct mhdr *hdr; | |
0a7de745 A |
230 | |
231 | if (addr == NULL) { | |
b0d623f7 | 232 | return; |
0a7de745 | 233 | } |
b0d623f7 A |
234 | hdr = addr; hdr--; |
235 | ||
d9a64523 | 236 | if (hdr->mh_magic != AUDIT_MHMAGIC) { |
0a7de745 | 237 | panic("_audit_free(): hdr->mh_magic (%lx) != AUDIT_MHMAGIC", hdr->mh_magic); |
d9a64523 | 238 | } |
b0d623f7 A |
239 | |
240 | #if AUDIT_MALLOC_DEBUG | |
241 | if (type != NULL) { | |
242 | OSAddAtomic64(-hdr->mh_size, &type->mt_size); | |
243 | OSAddAtomic(-1, &type->mt_inuse); | |
244 | } | |
245 | #endif /* AUDIT_MALLOC_DEBUG */ | |
f427ee49 | 246 | kheap_free(KHEAP_AUDIT, hdr, hdr->mh_size); |
b0d623f7 A |
247 | } |
248 | ||
249 | /* | |
250 | * Initialize a condition variable. Must be called before use. | |
251 | */ | |
252 | void | |
253 | _audit_cv_init(struct cv *cvp, const char *desc) | |
254 | { | |
0a7de745 | 255 | if (desc == NULL) { |
b0d623f7 | 256 | cvp->cv_description = "UNKNOWN"; |
0a7de745 | 257 | } else { |
b0d623f7 | 258 | cvp->cv_description = desc; |
0a7de745 | 259 | } |
b0d623f7 A |
260 | cvp->cv_waiters = 0; |
261 | } | |
262 | ||
263 | /* | |
264 | * Destory a condition variable. | |
265 | */ | |
266 | void | |
267 | _audit_cv_destroy(struct cv *cvp) | |
268 | { | |
b0d623f7 A |
269 | cvp->cv_description = NULL; |
270 | cvp->cv_waiters = 0; | |
271 | } | |
272 | ||
273 | /* | |
274 | * Signal a condition variable, wakes up one waiting thread. | |
275 | */ | |
276 | void | |
277 | _audit_cv_signal(struct cv *cvp) | |
278 | { | |
b0d623f7 A |
279 | if (cvp->cv_waiters > 0) { |
280 | wakeup_one((caddr_t)cvp); | |
281 | cvp->cv_waiters--; | |
282 | } | |
283 | } | |
284 | ||
285 | /* | |
286 | * Broadcast a signal to a condition variable. | |
287 | */ | |
288 | void | |
289 | _audit_cv_broadcast(struct cv *cvp) | |
290 | { | |
b0d623f7 A |
291 | if (cvp->cv_waiters > 0) { |
292 | wakeup((caddr_t)cvp); | |
293 | cvp->cv_waiters = 0; | |
294 | } | |
295 | } | |
296 | ||
297 | /* | |
298 | * Wait on a condition variable. A cv_signal or cv_broadcast on the same | |
299 | * condition variable will resume the thread. It is recommended that the mutex | |
300 | * be held when cv_signal or cv_broadcast are called. | |
301 | */ | |
302 | void | |
303 | _audit_cv_wait(struct cv *cvp, lck_mtx_t *mp, const char *desc) | |
304 | { | |
b0d623f7 A |
305 | cvp->cv_waiters++; |
306 | (void) msleep(cvp, mp, PZERO, desc, 0); | |
307 | } | |
308 | ||
309 | /* | |
310 | * Wait on a condition variable, allowing interruption by signals. Return 0 | |
311 | * if the thread was resumed with cv_signal or cv_broadcast, EINTR or | |
312 | * ERESTART if a signal was caught. If ERESTART is returned the system call | |
313 | * should be restarted if possible. | |
314 | */ | |
315 | int | |
316 | _audit_cv_wait_sig(struct cv *cvp, lck_mtx_t *mp, const char *desc) | |
317 | { | |
b0d623f7 | 318 | cvp->cv_waiters++; |
0a7de745 | 319 | return msleep(cvp, mp, PSOCK | PCATCH, desc, 0); |
b0d623f7 A |
320 | } |
321 | ||
322 | /* | |
6d2010ae A |
323 | * BSD Mutexes. |
324 | */ | |
325 | void | |
326 | #if DIAGNOSTIC | |
327 | _audit_mtx_init(struct mtx *mp, const char *lckname) | |
328 | #else | |
329 | _audit_mtx_init(struct mtx *mp, __unused const char *lckname) | |
330 | #endif | |
331 | { | |
f427ee49 | 332 | mp->mtx_lock = lck_mtx_alloc_init(&audit_lck_grp, LCK_ATTR_NULL); |
0a7de745 | 333 | KASSERT(mp->mtx_lock != NULL, |
6d2010ae A |
334 | ("_audit_mtx_init: Could not allocate a mutex.")); |
335 | #if DIAGNOSTIC | |
0a7de745 | 336 | strlcpy(mp->mtx_name, lckname, AU_MAX_LCK_NAME); |
6d2010ae A |
337 | #endif |
338 | } | |
339 | ||
340 | void | |
341 | _audit_mtx_destroy(struct mtx *mp) | |
342 | { | |
6d2010ae | 343 | if (mp->mtx_lock) { |
f427ee49 | 344 | lck_mtx_free(mp->mtx_lock, &audit_lck_grp); |
6d2010ae A |
345 | mp->mtx_lock = NULL; |
346 | } | |
347 | } | |
348 | ||
349 | /* | |
350 | * BSD rw locks. | |
b0d623f7 A |
351 | */ |
352 | void | |
6d2010ae A |
353 | #if DIAGNOSTIC |
354 | _audit_rw_init(struct rwlock *lp, const char *lckname) | |
355 | #else | |
356 | _audit_rw_init(struct rwlock *lp, __unused const char *lckname) | |
357 | #endif | |
358 | { | |
f427ee49 | 359 | lp->rw_lock = lck_rw_alloc_init(&audit_lck_grp, LCK_ATTR_NULL); |
0a7de745 | 360 | KASSERT(lp->rw_lock != NULL, |
6d2010ae A |
361 | ("_audit_rw_init: Could not allocate a rw lock.")); |
362 | #if DIAGNOSTIC | |
0a7de745 | 363 | strlcpy(lp->rw_name, lckname, AU_MAX_LCK_NAME); |
6d2010ae A |
364 | #endif |
365 | } | |
366 | ||
367 | void | |
368 | _audit_rw_destroy(struct rwlock *lp) | |
369 | { | |
6d2010ae | 370 | if (lp->rw_lock) { |
f427ee49 | 371 | lck_rw_free(lp->rw_lock, &audit_lck_grp); |
6d2010ae A |
372 | lp->rw_lock = NULL; |
373 | } | |
374 | } | |
375 | /* | |
376 | * Wait on a condition variable in a continuation (i.e. yield kernel stack). | |
377 | * A cv_signal or cv_broadcast on the same condition variable will cause | |
378 | * the thread to be scheduled. | |
379 | */ | |
380 | int | |
381 | _audit_cv_wait_continuation(struct cv *cvp, lck_mtx_t *mp, thread_continue_t function) | |
b0d623f7 | 382 | { |
6d2010ae A |
383 | int status = KERN_SUCCESS; |
384 | ||
385 | cvp->cv_waiters++; | |
386 | assert_wait(cvp, THREAD_UNINT); | |
387 | lck_mtx_unlock(mp); | |
388 | ||
389 | status = thread_block(function); | |
b0d623f7 | 390 | |
6d2010ae A |
391 | /* should not be reached, but just in case, re-lock */ |
392 | lck_mtx_lock(mp); | |
393 | ||
394 | return status; | |
395 | } | |
396 | ||
397 | /* | |
0a7de745 | 398 | * Simple recursive lock. |
6d2010ae A |
399 | */ |
400 | void | |
401 | #if DIAGNOSTIC | |
402 | _audit_rlck_init(struct rlck *lp, const char *lckname) | |
403 | #else | |
404 | _audit_rlck_init(struct rlck *lp, __unused const char *lckname) | |
405 | #endif | |
406 | { | |
f427ee49 | 407 | lp->rl_mtx = lck_mtx_alloc_init(&audit_lck_grp, LCK_ATTR_NULL); |
0a7de745 | 408 | KASSERT(lp->rl_mtx != NULL, |
6d2010ae A |
409 | ("_audit_rlck_init: Could not allocate a recursive lock.")); |
410 | #if DIAGNOSTIC | |
0a7de745 | 411 | strlcpy(lp->rl_name, lckname, AU_MAX_LCK_NAME); |
6d2010ae | 412 | #endif |
b0d623f7 A |
413 | lp->rl_thread = 0; |
414 | lp->rl_recurse = 0; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Recursive lock. Allow same thread to recursively lock the same lock. | |
0a7de745 | 419 | */ |
b0d623f7 A |
420 | void |
421 | _audit_rlck_lock(struct rlck *lp) | |
422 | { | |
b0d623f7 A |
423 | if (lp->rl_thread == current_thread()) { |
424 | OSAddAtomic(1, &lp->rl_recurse); | |
425 | KASSERT(lp->rl_recurse < 10000, | |
426 | ("_audit_rlck_lock: lock nested too deep.")); | |
427 | } else { | |
428 | lck_mtx_lock(lp->rl_mtx); | |
429 | lp->rl_thread = current_thread(); | |
430 | lp->rl_recurse = 1; | |
431 | } | |
432 | } | |
433 | ||
434 | /* | |
435 | * Recursive unlock. It should be the same thread that does the unlock. | |
436 | */ | |
437 | void | |
438 | _audit_rlck_unlock(struct rlck *lp) | |
439 | { | |
0a7de745 | 440 | KASSERT(lp->rl_thread == current_thread(), |
b0d623f7 A |
441 | ("_audit_rlck_unlock(): Don't own lock.")); |
442 | ||
443 | /* Note: OSAddAtomic returns old value. */ | |
444 | if (OSAddAtomic(-1, &lp->rl_recurse) == 1) { | |
445 | lp->rl_thread = 0; | |
446 | lck_mtx_unlock(lp->rl_mtx); | |
447 | } | |
448 | } | |
0a7de745 | 449 | |
b0d623f7 A |
450 | void |
451 | _audit_rlck_destroy(struct rlck *lp) | |
452 | { | |
b0d623f7 | 453 | if (lp->rl_mtx) { |
f427ee49 | 454 | lck_mtx_free(lp->rl_mtx, &audit_lck_grp); |
6d2010ae | 455 | lp->rl_mtx = NULL; |
b0d623f7 A |
456 | } |
457 | } | |
458 | ||
459 | /* | |
460 | * Recursive lock assert. | |
461 | */ | |
462 | void | |
463 | _audit_rlck_assert(struct rlck *lp, u_int assert) | |
464 | { | |
465 | thread_t cthd = current_thread(); | |
0a7de745 A |
466 | |
467 | if (assert == LCK_MTX_ASSERT_OWNED && lp->rl_thread == cthd) { | |
b0d623f7 A |
468 | panic("recursive lock (%p) not held by this thread (%p).", |
469 | lp, cthd); | |
0a7de745 A |
470 | } |
471 | if (assert == LCK_MTX_ASSERT_NOTOWNED && lp->rl_thread != 0) { | |
b0d623f7 A |
472 | panic("recursive lock (%p) held by thread (%p).", |
473 | lp, cthd); | |
0a7de745 | 474 | } |
b0d623f7 A |
475 | } |
476 | ||
477 | /* | |
478 | * Simple sleep lock. | |
479 | */ | |
480 | void | |
6d2010ae A |
481 | #if DIAGNOSTIC |
482 | _audit_slck_init(struct slck *lp, const char *lckname) | |
483 | #else | |
484 | _audit_slck_init(struct slck *lp, __unused const char *lckname) | |
485 | #endif | |
b0d623f7 | 486 | { |
f427ee49 | 487 | lp->sl_mtx = lck_mtx_alloc_init(&audit_lck_grp, LCK_ATTR_NULL); |
0a7de745 | 488 | KASSERT(lp->sl_mtx != NULL, |
6d2010ae A |
489 | ("_audit_slck_init: Could not allocate a sleep lock.")); |
490 | #if DIAGNOSTIC | |
0a7de745 | 491 | strlcpy(lp->sl_name, lckname, AU_MAX_LCK_NAME); |
6d2010ae | 492 | #endif |
b0d623f7 A |
493 | lp->sl_locked = 0; |
494 | lp->sl_waiting = 0; | |
495 | } | |
496 | ||
497 | /* | |
498 | * Sleep lock lock. The 'intr' flag determines if the lock is interruptible. | |
0a7de745 | 499 | * If 'intr' is true then signals or other events can interrupt the sleep lock. |
b0d623f7 A |
500 | */ |
501 | wait_result_t | |
502 | _audit_slck_lock(struct slck *lp, int intr) | |
503 | { | |
504 | wait_result_t res = THREAD_AWAKENED; | |
505 | ||
506 | lck_mtx_lock(lp->sl_mtx); | |
507 | while (lp->sl_locked && res == THREAD_AWAKENED) { | |
508 | lp->sl_waiting = 1; | |
509 | res = lck_mtx_sleep(lp->sl_mtx, LCK_SLEEP_DEFAULT, | |
0a7de745 | 510 | (event_t) lp, (intr) ? THREAD_INTERRUPTIBLE : THREAD_UNINT); |
b0d623f7 | 511 | } |
0a7de745 | 512 | if (res == THREAD_AWAKENED) { |
b0d623f7 | 513 | lp->sl_locked = 1; |
0a7de745 | 514 | } |
b0d623f7 | 515 | lck_mtx_unlock(lp->sl_mtx); |
0a7de745 A |
516 | |
517 | return res; | |
b0d623f7 A |
518 | } |
519 | ||
520 | /* | |
521 | * Sleep lock unlock. Wake up all the threads waiting for this lock. | |
522 | */ | |
523 | void | |
524 | _audit_slck_unlock(struct slck *lp) | |
525 | { | |
b0d623f7 A |
526 | lck_mtx_lock(lp->sl_mtx); |
527 | lp->sl_locked = 0; | |
528 | if (lp->sl_waiting) { | |
529 | lp->sl_waiting = 0; | |
530 | ||
531 | /* Wake up *all* sleeping threads. */ | |
6d2010ae | 532 | wakeup((event_t) lp); |
b0d623f7 A |
533 | } |
534 | lck_mtx_unlock(lp->sl_mtx); | |
535 | } | |
536 | ||
537 | /* | |
0a7de745 | 538 | * Sleep lock try. Don't sleep if it doesn't get the lock. |
b0d623f7 A |
539 | */ |
540 | int | |
541 | _audit_slck_trylock(struct slck *lp) | |
542 | { | |
543 | int result; | |
544 | ||
545 | lck_mtx_lock(lp->sl_mtx); | |
546 | result = !lp->sl_locked; | |
0a7de745 | 547 | if (result) { |
b0d623f7 | 548 | lp->sl_locked = 1; |
0a7de745 | 549 | } |
b0d623f7 A |
550 | lck_mtx_unlock(lp->sl_mtx); |
551 | ||
0a7de745 | 552 | return result; |
b0d623f7 A |
553 | } |
554 | ||
555 | /* | |
556 | * Sleep lock assert. | |
557 | */ | |
558 | void | |
559 | _audit_slck_assert(struct slck *lp, u_int assert) | |
560 | { | |
0a7de745 | 561 | if (assert == LCK_MTX_ASSERT_OWNED && lp->sl_locked == 0) { |
b0d623f7 | 562 | panic("sleep lock (%p) not held.", lp); |
0a7de745 A |
563 | } |
564 | if (assert == LCK_MTX_ASSERT_NOTOWNED && lp->sl_locked == 1) { | |
b0d623f7 | 565 | panic("sleep lock (%p) held.", lp); |
0a7de745 | 566 | } |
b0d623f7 A |
567 | } |
568 | ||
569 | void | |
570 | _audit_slck_destroy(struct slck *lp) | |
571 | { | |
b0d623f7 | 572 | if (lp->sl_mtx) { |
f427ee49 | 573 | lck_mtx_free(lp->sl_mtx, &audit_lck_grp); |
6d2010ae | 574 | lp->sl_mtx = NULL; |
b0d623f7 A |
575 | } |
576 | } | |
577 | ||
578 | /* | |
579 | * XXXss - This code was taken from bsd/netinet6/icmp6.c. Maybe ppsratecheck() | |
580 | * should be made global in icmp6.c. | |
581 | */ | |
582 | #ifndef timersub | |
583 | #define timersub(tvp, uvp, vvp) \ | |
0a7de745 A |
584 | do { \ |
585 | (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ | |
586 | (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ | |
587 | if ((vvp)->tv_usec < 0) { \ | |
588 | (vvp)->tv_sec--; \ | |
589 | (vvp)->tv_usec += 1000000; \ | |
590 | } \ | |
591 | } while (0) | |
b0d623f7 A |
592 | #endif |
593 | ||
594 | /* | |
595 | * Packets (or events) per second limitation. | |
596 | */ | |
597 | int | |
598 | _audit_ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) | |
599 | { | |
600 | struct timeval tv, delta; | |
601 | int rv; | |
602 | ||
603 | microtime(&tv); | |
604 | ||
605 | timersub(&tv, lasttime, &delta); | |
606 | ||
607 | /* | |
608 | * Check for 0,0 so that the message will be seen at least once. | |
609 | * If more than one second has passed since the last update of | |
610 | * lasttime, reset the counter. | |
611 | * | |
612 | * we do increment *curpps even in *curpps < maxpps case, as some may | |
613 | * try to use *curpps for stat purposes as well. | |
614 | */ | |
615 | if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || | |
616 | delta.tv_sec >= 1) { | |
617 | *lasttime = tv; | |
618 | *curpps = 0; | |
619 | rv = 1; | |
0a7de745 | 620 | } else if (maxpps < 0) { |
b0d623f7 | 621 | rv = 1; |
0a7de745 | 622 | } else if (*curpps < maxpps) { |
b0d623f7 | 623 | rv = 1; |
0a7de745 | 624 | } else { |
b0d623f7 | 625 | rv = 0; |
0a7de745 A |
626 | } |
627 | if (*curpps + 1 > 0) { | |
b0d623f7 | 628 | *curpps = *curpps + 1; |
0a7de745 | 629 | } |
b0d623f7 | 630 | |
0a7de745 | 631 | return rv; |
b0d623f7 A |
632 | } |
633 | ||
f427ee49 A |
634 | int |
635 | audit_send_trigger(unsigned int trigger) | |
6d2010ae | 636 | { |
f427ee49 A |
637 | mach_port_t audit_port; |
638 | int error; | |
6d2010ae | 639 | |
f427ee49 A |
640 | error = host_get_audit_control_port(host_priv_self(), &audit_port); |
641 | if (error == KERN_SUCCESS && audit_port != MACH_PORT_NULL) { | |
642 | (void)audit_triggers(audit_port, trigger); | |
643 | ipc_port_release_send(audit_port); | |
644 | return 0; | |
645 | } else { | |
646 | printf("Cannot get audit control port\n"); | |
647 | return error; | |
648 | } | |
6d2010ae A |
649 | } |
650 | ||
b0d623f7 | 651 | int |
f427ee49 | 652 | audit_send_analytics(char* signing_id, char* process_name) |
b0d623f7 A |
653 | { |
654 | mach_port_t audit_port; | |
655 | int error; | |
656 | ||
657 | error = host_get_audit_control_port(host_priv_self(), &audit_port); | |
658 | if (error == KERN_SUCCESS && audit_port != MACH_PORT_NULL) { | |
f427ee49 | 659 | (void)audit_analytics(audit_port, signing_id, process_name); |
39037602 | 660 | ipc_port_release_send(audit_port); |
0a7de745 | 661 | return 0; |
b0d623f7 | 662 | } else { |
f427ee49 | 663 | printf("Cannot get audit control port for analytics \n"); |
0a7de745 | 664 | return error; |
b0d623f7 A |
665 | } |
666 | } | |
f427ee49 | 667 | |
b0d623f7 | 668 | #endif /* CONFIG_AUDIT */ |