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