2 * Copyright (c) 2008-2010 Apple Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/kernel.h>
34 #include <sys/systm.h>
36 #include <kern/host.h>
37 #include <kern/kalloc.h>
38 #include <kern/locks.h>
39 #include <kern/sched_prim.h>
41 #include <libkern/OSAtomic.h>
43 #include <bsm/audit.h>
44 #include <bsm/audit_internal.h>
46 #include <security/audit/audit_bsd.h>
47 #include <security/audit/audit.h>
48 #include <security/audit/audit_private.h>
50 #include <mach/host_priv.h>
51 #include <mach/host_special_ports.h>
52 #include <mach/audit_triggers_server.h>
54 #include <os/overflow.h>
56 extern void ipc_port_release_send(ipc_port_t port
);
61 au_malloc_type_t
*mh_type
;
67 * The lock group for the audit subsystem.
69 static lck_grp_t
*audit_lck_grp
= NULL
;
71 #define AUDIT_MHMAGIC 0x4D656C53
73 #if AUDIT_MALLOC_DEBUG
74 #define AU_MAX_SHORTDESC 20
75 #define AU_MAX_LASTCALLER 20
76 struct au_malloc_debug_info
{
83 char md_shortdesc
[AU_MAX_SHORTDESC
];
84 char md_lastcaller
[AU_MAX_LASTCALLER
];
86 typedef struct au_malloc_debug_info au_malloc_debug_info_t
;
88 au_malloc_type_t
*audit_malloc_types
[NUM_MALLOC_TYPES
];
90 static int audit_sysctl_malloc_debug(struct sysctl_oid
*oidp
, void *arg1
,
91 int arg2
, struct sysctl_req
*req
);
93 SYSCTL_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.");
97 #define AU_MALLOC_DBINFO_SZ \
98 (NUM_MALLOC_TYPES * sizeof(au_malloc_debug_info_t))
101 * Copy out the malloc debug info via the sysctl interface. The userland code
102 * is something like the following:
104 * error = sysctlbyname("kern.audit_malloc_debug", buffer_ptr, &buffer_len,
108 audit_sysctl_malloc_debug(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
109 __unused
int arg2
, struct sysctl_req
*req
)
113 au_malloc_debug_info_t
*amdi_ptr
, *nxt_ptr
;
117 * This provides a read-only node.
119 if (req
->newptr
!= USER_ADDR_NULL
)
123 * If just querying then return the space required.
125 if (req
->oldptr
== USER_ADDR_NULL
) {
126 req
->oldidx
= AU_MALLOC_DBINFO_SZ
;
131 * Alloc a temporary buffer.
133 if (req
->oldlen
< AU_MALLOC_DBINFO_SZ
)
135 amdi_ptr
= (au_malloc_debug_info_t
*)kalloc(AU_MALLOC_DBINFO_SZ
);
136 if (amdi_ptr
== NULL
)
138 bzero(amdi_ptr
, AU_MALLOC_DBINFO_SZ
);
141 * Build the record array.
145 for(i
= 0; i
< NUM_MALLOC_TYPES
; i
++) {
146 if (audit_malloc_types
[i
] == NULL
)
148 if (audit_malloc_types
[i
]->mt_magic
!= M_MAGIC
) {
149 nxt_ptr
->md_magic
= audit_malloc_types
[i
]->mt_magic
;
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
);
166 err
= SYSCTL_OUT(req
, amdi_ptr
, sz
);
167 kfree(amdi_ptr
, AU_MALLOC_DBINFO_SZ
);
171 #endif /* AUDIT_MALLOC_DEBUG */
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.
180 #if AUDIT_MALLOC_DEBUG
181 _audit_malloc(size_t size
, au_malloc_type_t
*type
, int flags
, const char *fn
)
183 _audit_malloc(size_t size
, au_malloc_type_t
*type
, int flags
)
188 if (os_add_overflow(sizeof(*hdr
), size
, &memsize
)) {
194 if (flags
& M_NOWAIT
) {
195 hdr
= (void *)kalloc_noblock(memsize
);
197 hdr
= (void *)kalloc(memsize
);
199 panic("_audit_malloc: kernel memory exhausted");
203 hdr
->mh_size
= memsize
;
205 hdr
->mh_magic
= AUDIT_MHMAGIC
;
207 memset(hdr
->mh_data
, 0, size
);
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
;
217 #endif /* AUDIT_MALLOC_DEBUG */
218 return (hdr
->mh_data
);
225 #if AUDIT_MALLOC_DEBUG
226 _audit_free(void *addr
, au_malloc_type_t
*type
)
228 _audit_free(void *addr
, __unused au_malloc_type_t
*type
)
237 if (hdr
->mh_magic
!= AUDIT_MHMAGIC
) {
238 panic("_audit_free(): hdr->mh_magic (%lx) != AUDIT_MHMAGIC", hdr
->mh_magic
);
241 #if AUDIT_MALLOC_DEBUG
243 OSAddAtomic64(-hdr
->mh_size
, &type
->mt_size
);
244 OSAddAtomic(-1, &type
->mt_inuse
);
246 #endif /* AUDIT_MALLOC_DEBUG */
247 kfree(hdr
, hdr
->mh_size
);
251 * Initialize a condition variable. Must be called before use.
254 _audit_cv_init(struct cv
*cvp
, const char *desc
)
258 cvp
->cv_description
= "UNKNOWN";
260 cvp
->cv_description
= desc
;
265 * Destory a condition variable.
268 _audit_cv_destroy(struct cv
*cvp
)
271 cvp
->cv_description
= NULL
;
276 * Signal a condition variable, wakes up one waiting thread.
279 _audit_cv_signal(struct cv
*cvp
)
282 if (cvp
->cv_waiters
> 0) {
283 wakeup_one((caddr_t
)cvp
);
289 * Broadcast a signal to a condition variable.
292 _audit_cv_broadcast(struct cv
*cvp
)
295 if (cvp
->cv_waiters
> 0) {
296 wakeup((caddr_t
)cvp
);
302 * Wait on a condition variable. A cv_signal or cv_broadcast on the same
303 * condition variable will resume the thread. It is recommended that the mutex
304 * be held when cv_signal or cv_broadcast are called.
307 _audit_cv_wait(struct cv
*cvp
, lck_mtx_t
*mp
, const char *desc
)
311 (void) msleep(cvp
, mp
, PZERO
, desc
, 0);
315 * Wait on a condition variable, allowing interruption by signals. Return 0
316 * if the thread was resumed with cv_signal or cv_broadcast, EINTR or
317 * ERESTART if a signal was caught. If ERESTART is returned the system call
318 * should be restarted if possible.
321 _audit_cv_wait_sig(struct cv
*cvp
, lck_mtx_t
*mp
, const char *desc
)
325 return (msleep(cvp
, mp
, PSOCK
| PCATCH
, desc
, 0));
333 _audit_mtx_init(struct mtx
*mp
, const char *lckname
)
335 _audit_mtx_init(struct mtx
*mp
, __unused
const char *lckname
)
338 mp
->mtx_lock
= lck_mtx_alloc_init(audit_lck_grp
, LCK_ATTR_NULL
);
339 KASSERT(mp
->mtx_lock
!= NULL
,
340 ("_audit_mtx_init: Could not allocate a mutex."));
342 strlcpy(mp
->mtx_name
, lckname
, AU_MAX_LCK_NAME
);
347 _audit_mtx_destroy(struct mtx
*mp
)
351 lck_mtx_free(mp
->mtx_lock
, audit_lck_grp
);
361 _audit_rw_init(struct rwlock
*lp
, const char *lckname
)
363 _audit_rw_init(struct rwlock
*lp
, __unused
const char *lckname
)
366 lp
->rw_lock
= lck_rw_alloc_init(audit_lck_grp
, LCK_ATTR_NULL
);
367 KASSERT(lp
->rw_lock
!= NULL
,
368 ("_audit_rw_init: Could not allocate a rw lock."));
370 strlcpy(lp
->rw_name
, lckname
, AU_MAX_LCK_NAME
);
375 _audit_rw_destroy(struct rwlock
*lp
)
379 lck_rw_free(lp
->rw_lock
, audit_lck_grp
);
384 * Wait on a condition variable in a continuation (i.e. yield kernel stack).
385 * A cv_signal or cv_broadcast on the same condition variable will cause
386 * the thread to be scheduled.
389 _audit_cv_wait_continuation(struct cv
*cvp
, lck_mtx_t
*mp
, thread_continue_t function
)
391 int status
= KERN_SUCCESS
;
394 assert_wait(cvp
, THREAD_UNINT
);
397 status
= thread_block(function
);
399 /* should not be reached, but just in case, re-lock */
406 * Simple recursive lock.
410 _audit_rlck_init(struct rlck
*lp
, const char *lckname
)
412 _audit_rlck_init(struct rlck
*lp
, __unused
const char *lckname
)
416 lp
->rl_mtx
= lck_mtx_alloc_init(audit_lck_grp
, LCK_ATTR_NULL
);
417 KASSERT(lp
->rl_mtx
!= NULL
,
418 ("_audit_rlck_init: Could not allocate a recursive lock."));
420 strlcpy(lp
->rl_name
, lckname
, AU_MAX_LCK_NAME
);
427 * Recursive lock. Allow same thread to recursively lock the same lock.
430 _audit_rlck_lock(struct rlck
*lp
)
433 if (lp
->rl_thread
== current_thread()) {
434 OSAddAtomic(1, &lp
->rl_recurse
);
435 KASSERT(lp
->rl_recurse
< 10000,
436 ("_audit_rlck_lock: lock nested too deep."));
438 lck_mtx_lock(lp
->rl_mtx
);
439 lp
->rl_thread
= current_thread();
445 * Recursive unlock. It should be the same thread that does the unlock.
448 _audit_rlck_unlock(struct rlck
*lp
)
450 KASSERT(lp
->rl_thread
== current_thread(),
451 ("_audit_rlck_unlock(): Don't own lock."));
453 /* Note: OSAddAtomic returns old value. */
454 if (OSAddAtomic(-1, &lp
->rl_recurse
) == 1) {
456 lck_mtx_unlock(lp
->rl_mtx
);
461 _audit_rlck_destroy(struct rlck
*lp
)
465 lck_mtx_free(lp
->rl_mtx
, audit_lck_grp
);
471 * Recursive lock assert.
474 _audit_rlck_assert(struct rlck
*lp
, u_int
assert)
476 thread_t cthd
= current_thread();
478 if (assert == LCK_MTX_ASSERT_OWNED
&& lp
->rl_thread
== cthd
)
479 panic("recursive lock (%p) not held by this thread (%p).",
481 if (assert == LCK_MTX_ASSERT_NOTOWNED
&& lp
->rl_thread
!= 0)
482 panic("recursive lock (%p) held by thread (%p).",
491 _audit_slck_init(struct slck
*lp
, const char *lckname
)
493 _audit_slck_init(struct slck
*lp
, __unused
const char *lckname
)
497 lp
->sl_mtx
= lck_mtx_alloc_init(audit_lck_grp
, LCK_ATTR_NULL
);
498 KASSERT(lp
->sl_mtx
!= NULL
,
499 ("_audit_slck_init: Could not allocate a sleep lock."));
501 strlcpy(lp
->sl_name
, lckname
, AU_MAX_LCK_NAME
);
508 * Sleep lock lock. The 'intr' flag determines if the lock is interruptible.
509 * If 'intr' is true then signals or other events can interrupt the sleep lock.
512 _audit_slck_lock(struct slck
*lp
, int intr
)
514 wait_result_t res
= THREAD_AWAKENED
;
516 lck_mtx_lock(lp
->sl_mtx
);
517 while (lp
->sl_locked
&& res
== THREAD_AWAKENED
) {
519 res
= lck_mtx_sleep(lp
->sl_mtx
, LCK_SLEEP_DEFAULT
,
520 (event_t
) lp
, (intr
) ? THREAD_INTERRUPTIBLE
: THREAD_UNINT
);
522 if (res
== THREAD_AWAKENED
)
524 lck_mtx_unlock(lp
->sl_mtx
);
530 * Sleep lock unlock. Wake up all the threads waiting for this lock.
533 _audit_slck_unlock(struct slck
*lp
)
536 lck_mtx_lock(lp
->sl_mtx
);
538 if (lp
->sl_waiting
) {
541 /* Wake up *all* sleeping threads. */
542 wakeup((event_t
) lp
);
544 lck_mtx_unlock(lp
->sl_mtx
);
548 * Sleep lock try. Don't sleep if it doesn't get the lock.
551 _audit_slck_trylock(struct slck
*lp
)
555 lck_mtx_lock(lp
->sl_mtx
);
556 result
= !lp
->sl_locked
;
559 lck_mtx_unlock(lp
->sl_mtx
);
568 _audit_slck_assert(struct slck
*lp
, u_int
assert)
571 if (assert == LCK_MTX_ASSERT_OWNED
&& lp
->sl_locked
== 0)
572 panic("sleep lock (%p) not held.", lp
);
573 if (assert == LCK_MTX_ASSERT_NOTOWNED
&& lp
->sl_locked
== 1)
574 panic("sleep lock (%p) held.", lp
);
578 _audit_slck_destroy(struct slck
*lp
)
582 lck_mtx_free(lp
->sl_mtx
, audit_lck_grp
);
588 * XXXss - This code was taken from bsd/netinet6/icmp6.c. Maybe ppsratecheck()
589 * should be made global in icmp6.c.
592 #define timersub(tvp, uvp, vvp) \
594 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
595 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
596 if ((vvp)->tv_usec < 0) { \
598 (vvp)->tv_usec += 1000000; \
604 * Packets (or events) per second limitation.
607 _audit_ppsratecheck(struct timeval
*lasttime
, int *curpps
, int maxpps
)
609 struct timeval tv
, delta
;
614 timersub(&tv
, lasttime
, &delta
);
617 * Check for 0,0 so that the message will be seen at least once.
618 * If more than one second has passed since the last update of
619 * lasttime, reset the counter.
621 * we do increment *curpps even in *curpps < maxpps case, as some may
622 * try to use *curpps for stat purposes as well.
624 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
629 } else if (maxpps
< 0)
631 else if (*curpps
< maxpps
)
636 *curpps
= *curpps
+ 1;
642 * Initialize lock group for audit related locks/mutexes.
645 _audit_lck_grp_init(void)
647 audit_lck_grp
= lck_grp_alloc_init("Audit", LCK_GRP_ATTR_NULL
);
649 KASSERT(audit_lck_grp
!= NULL
,
650 ("audit_get_lck_grp: Could not allocate the audit lock group."));
654 audit_send_trigger(unsigned int trigger
)
656 mach_port_t audit_port
;
659 error
= host_get_audit_control_port(host_priv_self(), &audit_port
);
660 if (error
== KERN_SUCCESS
&& audit_port
!= MACH_PORT_NULL
) {
661 (void)audit_triggers(audit_port
, trigger
);
662 ipc_port_release_send(audit_port
);
665 printf("Cannot get audit control port\n");
669 #endif /* CONFIG_AUDIT */