2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <sys/param.h>
29 #include <sys/fcntl.h>
30 #include <sys/kernel.h>
32 #include <sys/namei.h>
33 #include <sys/proc_internal.h>
34 #include <sys/kauth.h>
35 #include <sys/queue.h>
36 #include <sys/systm.h>
38 #include <sys/ucred.h>
40 #include <sys/unistd.h>
41 #include <sys/file_internal.h>
42 #include <sys/vnode_internal.h>
44 #include <sys/syscall.h>
45 #include <sys/malloc.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/vfs_context.h>
50 #include <sys/domain.h>
51 #include <sys/protosw.h>
52 #include <sys/socketvar.h>
54 #include <bsm/audit.h>
55 #include <bsm/audit_kevents.h>
56 #include <bsm/audit_klib.h>
57 #include <bsm/audit_kernel.h>
59 #include <mach/host_priv.h>
60 #include <mach/host_special_ports.h>
61 #include <mach/audit_triggers_server.h>
63 #include <kern/host.h>
64 #include <kern/kalloc.h>
65 #include <kern/zalloc.h>
66 #include <kern/lock.h>
67 #include <kern/wait_queue.h>
68 #include <kern/sched_prim.h>
70 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_pcb.h>
78 * The AUDIT_EXCESSIVELY_VERBOSE define enables a number of
79 * gratuitously noisy printf's to the console. Due to the
80 * volume, it should be left off unless you want your system
81 * to churn a lot whenever the audit record flow gets high.
83 /* #define AUDIT_EXCESSIVELY_VERBOSE */
84 #ifdef AUDIT_EXCESSIVELY_VERBOSE
85 #define AUDIT_PRINTF_ONLY
86 #define AUDIT_PRINTF(x) printf x
88 #define AUDIT_PRINTF_ONLY __unused
89 #define AUDIT_PRINTF(X)
96 #define assert(cond) \
97 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
99 #include <kern/assert.h>
100 #endif /* DIAGNOSTIC */
103 * Define the audit control flags.
109 * Mutex to protect global variables shared between various threads and
112 static mutex_t
*audit_mtx
;
115 * Queue of audit records ready for delivery to disk. We insert new
116 * records at the tail, and remove records from the head. Also,
117 * a count of the number of records used for checking queue depth.
118 * In addition, a counter of records that we have allocated but are
119 * not yet in the queue, which is needed to estimate the total
120 * size of the combined set of records outstanding in the system.
122 static TAILQ_HEAD(, kaudit_record
) audit_q
;
123 static size_t audit_q_len
;
124 static size_t audit_pre_q_len
;
126 static wait_queue_t audit_wait_queue
;
127 static zone_t audit_zone
;
130 * Condition variable to signal to the worker that it has work to do:
131 * either new records are in the queue, or a log replacement is taking
134 static int audit_worker_event
;
135 #define AUDIT_WORKER_EVENT ((event_t)&audit_worker_event)
138 * The audit worker thread (which is lazy started when we first
139 * rotate the audit log.
141 static thread_t audit_worker_thread
= THREAD_NULL
;
144 * When an audit log is rotated, the actual rotation must be performed
145 * by the audit worker thread, as it may have outstanding writes on the
146 * current audit log. audit_replacement_vp holds the vnode replacing
147 * the current vnode. We can't let more than one replacement occur
148 * at a time, so if more than one thread requests a replacement, only
149 * one can have the replacement "in progress" at any given moment. If
150 * a thread tries to replace the audit vnode and discovers a replacement
151 * is already in progress (i.e., audit_replacement_flag != 0), then it
152 * will sleep on audit_replacement_cv waiting its turn to perform a
153 * replacement. When a replacement is completed, this cv is signalled
154 * by the worker thread so a waiting thread can start another replacement.
155 * We also store a credential to perform audit log write operations with.
157 static int audit_replacement_event
;
158 #define AUDIT_REPLACEMENT_EVENT ((event_t)&audit_replacement_event)
160 static int audit_replacement_flag
;
161 static struct vnode
*audit_replacement_vp
;
162 static kauth_cred_t audit_replacement_cred
;
165 * Wait queue for auditing threads that cannot commit the audit
166 * record at the present time. Also, the queue control parameter
169 static int audit_commit_event
;
170 #define AUDIT_COMMIT_EVENT ((event_t)&audit_commit_event)
172 static struct au_qctrl audit_qctrl
;
175 * Flags to use on audit files when opening and closing.
177 static const int audit_open_flags
= FWRITE
| O_APPEND
;
178 static const int audit_close_flags
= FWRITE
| O_APPEND
;
181 * Global audit statistiscs.
183 static struct audit_fstat audit_fstat
;
186 Preselection mask for non-attributable events.
188 static struct au_mask audit_nae_mask
;
191 * Flags related to Kernel->user-space communication.
193 static int audit_file_rotate_wait
;
196 * Flags controlling behavior in low storage situations.
197 * Should we panic if a write fails? Should we fail stop
198 * if we're out of disk space? Are we currently "failing
199 * stop" due to out of disk space?
201 static int audit_panic_on_write_fail
;
202 static int audit_fail_stop
;
203 static int audit_in_failure
;
206 * When in a fail-stop mode, threads will drop into this wait queue
207 * rather than perform auditable events. They won't ever get woken
210 static int audit_failure_event
;
211 #define AUDIT_FAILURE_EVENT ((event_t)&audit_failure_event)
214 * XXX: Couldn't find the include file for this, so copied kern_exec.c's
217 extern task_t kernel_task
;
220 audit_free(struct kaudit_record
*ar
)
222 if (ar
->k_ar
.ar_arg_upath1
!= NULL
) {
223 kfree(ar
->k_ar
.ar_arg_upath1
, MAXPATHLEN
);
225 if (ar
->k_ar
.ar_arg_upath2
!= NULL
) {
226 kfree(ar
->k_ar
.ar_arg_upath2
, MAXPATHLEN
);
229 if (ar
->k_ar
.ar_arg_kpath1
!= NULL
) {
230 kfree(ar
->k_ar
.ar_arg_kpath1
, MAXPATHLEN
);
233 if (ar
->k_ar
.ar_arg_kpath2
!= NULL
) {
234 kfree(ar
->k_ar
.ar_arg_kpath2
, MAXPATHLEN
);
237 if (ar
->k_ar
.ar_arg_text
!= NULL
) {
238 kfree(ar
->k_ar
.ar_arg_text
, MAXPATHLEN
);
241 if (ar
->k_udata
!= NULL
) {
242 kfree(ar
->k_udata
, ar
->k_ulen
);
245 zfree(audit_zone
, ar
);
249 audit_write(struct vnode
*vp
, struct kaudit_record
*ar
, kauth_cred_t cred
,
252 struct vfsstatfs
*mnt_stat
= &vp
->v_mount
->mnt_vfsstat
;
254 struct au_record
*bsm
;
255 /* KVV maybe we should take a context as a param to audit_write? */
256 struct vfs_context context
;
259 mach_port_t audit_port
;
262 * First, gather statistics on the audit log file and file system
263 * so that we know how we're doing on space. In both cases,
264 * if we're unable to perform the operation, we drop the record
265 * and return. However, this is arguably an assertion failure.
268 context
.vc_ucred
= cred
;
269 ret
= vfs_update_vfsstat(vp
->v_mount
, &context
);
273 /* update the global stats struct */
274 if ((ret
= vnode_size(vp
, &file_size
, &context
)) != 0)
276 audit_fstat
.af_currsz
= file_size
;
279 * Send a message to the audit daemon when disk space is getting
281 * XXX Need to decide what to do if the trigger to the audit daemon
284 if(host_get_audit_control_port(host_priv_self(), &audit_port
)
286 printf("Cannot get audit control port\n");
288 if (audit_port
!= MACH_PORT_NULL
) {
292 * If we fall below percent free blocks, then trigger the
293 * audit daemon to do something about it.
295 if (audit_qctrl
.aq_minfree
!= 0) {
296 temp
= mnt_stat
->f_blocks
/ (100 / audit_qctrl
.aq_minfree
);
297 if (mnt_stat
->f_bfree
< temp
) {
298 ret
= audit_triggers(audit_port
,
299 AUDIT_TRIGGER_LOW_SPACE
);
300 if (ret
!= KERN_SUCCESS
) {
302 "Failed audit_triggers(AUDIT_TRIGGER_LOW_SPACE): %d\n", ret
);
304 * XXX: What to do here? Disable auditing?
310 /* Check if the current log file is full; if so, call for
311 * a log rotate. This is not an exact comparison; we may
312 * write some records over the limit. If that's not
313 * acceptable, then add a fudge factor here.
315 if ((audit_fstat
.af_filesz
!= 0) &&
316 (audit_file_rotate_wait
== 0) &&
317 (file_size
>= audit_fstat
.af_filesz
)) {
318 audit_file_rotate_wait
= 1;
319 ret
= audit_triggers(audit_port
,
320 AUDIT_TRIGGER_FILE_FULL
);
321 if (ret
!= KERN_SUCCESS
) {
323 "Failed audit_triggers(AUDIT_TRIGGER_FILE_FULL): %d\n", ret
);
324 /* XXX what to do here? */
330 * If the estimated amount of audit data in the audit event queue
331 * (plus records allocated but not yet queued) has reached the
332 * amount of free space on the disk, then we need to go into an
333 * audit fail stop state, in which we do not permit the
334 * allocation/committing of any new audit records. We continue to
335 * process packets but don't allow any activities that might
336 * generate new records. In the future, we might want to detect
337 * when space is available again and allow operation to continue,
338 * but this behavior is sufficient to meet fail stop requirements
341 if (audit_fail_stop
&&
343 ((audit_q_len
+ audit_pre_q_len
+ 1) * MAX_AUDIT_RECORD_SIZE
) /
344 mnt_stat
->f_bsize
>= (unsigned long)(mnt_stat
->f_bfree
)) {
346 "audit_worker: free space below size of audit queue, failing stop\n");
347 audit_in_failure
= 1;
351 * If there is a user audit record attached to the kernel record,
352 * then write the user record.
354 /* XXX Need to decide a few things here: IF the user audit
355 * record is written, but the write of the kernel record fails,
356 * what to do? Should the kernel record come before or after the
357 * user record? For now, we write the user record first, and
360 if (ar
->k_ar_commit
& AR_COMMIT_USER
) {
361 if (vnode_getwithref(vp
) == 0) {
362 ret
= vn_rdwr(UIO_WRITE
, vp
, (void *)ar
->k_udata
, ar
->k_ulen
,
363 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
);
373 * Convert the internal kernel record to BSM format and write it
374 * out if everything's OK.
376 if (!(ar
->k_ar_commit
& AR_COMMIT_KERNEL
)) {
381 ret
= kaudit_to_bsm(ar
, &bsm
);
382 if (ret
== BSM_NOAUDIT
) {
388 * XXX: We drop the record on BSM conversion failure, but really
389 * this is an assertion failure.
391 if (ret
== BSM_FAILURE
) {
392 AUDIT_PRINTF(("BSM conversion failure\n"));
397 /* XXX This function can be called with the kernel funnel held,
398 * which is not optimal. We should break the write functionality
399 * away from the BSM record generation and have the BSM generation
400 * done before this function is called. This function will then
401 * take the BSM record as a parameter.
403 if ((ret
= vnode_getwithref(vp
)) == 0) {
404 ret
= (vn_rdwr(UIO_WRITE
, vp
, (void *)bsm
->data
, bsm
->len
,
405 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
));
412 * When we're done processing the current record, we have to
413 * check to see if we're in a failure mode, and if so, whether
414 * this was the last record left to be drained. If we're done
415 * draining, then we fsync the vnode and panic.
417 if (audit_in_failure
&&
418 audit_q_len
== 0 && audit_pre_q_len
== 0) {
419 (void)VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
420 panic("Audit store overflow; record queue drained.");
429 int do_replacement_signal
, error
, release_funnel
;
430 TAILQ_HEAD(, kaudit_record
) ar_worklist
;
431 struct kaudit_record
*ar
;
432 struct vnode
*audit_vp
, *old_vp
;
433 kauth_cred_t audit_cred
;
434 kauth_cred_t old_cred
;
435 struct proc
*audit_p
;
437 AUDIT_PRINTF(("audit_worker starting\n"));
439 TAILQ_INIT(&ar_worklist
);
441 audit_p
= current_proc();
445 * XXX: Presumably we can assume Mach threads are started without
446 * holding the BSD kernel funnel?
448 thread_funnel_set(kernel_flock
, FALSE
);
450 mutex_lock(audit_mtx
);
453 * First priority: replace the audit log target if requested.
454 * As we actually close the vnode in the worker thread, we
455 * need to grab the funnel, which means releasing audit_mtx.
456 * In case another replacement was scheduled while the mutex
457 * we released, we loop.
459 * XXX It could well be we should drain existing records
460 * first to ensure that the timestamps and ordering
463 do_replacement_signal
= 0;
464 while (audit_replacement_flag
!= 0) {
465 old_cred
= audit_cred
;
467 audit_cred
= audit_replacement_cred
;
468 audit_vp
= audit_replacement_vp
;
469 audit_replacement_cred
= NOCRED
;
470 audit_replacement_vp
= NULL
;
471 audit_replacement_flag
= 0;
473 audit_enabled
= (audit_vp
!= NULL
);
475 if (old_vp
!= NULL
|| audit_vp
!= NULL
) {
476 mutex_unlock(audit_mtx
);
477 thread_funnel_set(kernel_flock
, TRUE
);
482 * XXX: What to do about write failures here?
484 if (old_vp
!= NULL
) {
485 AUDIT_PRINTF(("Closing old audit file\n"));
486 vn_close(old_vp
, audit_close_flags
, old_cred
,
488 kauth_cred_unref(&old_cred
);
490 AUDIT_PRINTF(("Audit file closed\n"));
492 if (audit_vp
!= NULL
) {
493 AUDIT_PRINTF(("Opening new audit file\n"));
495 if (release_funnel
) {
496 thread_funnel_set(kernel_flock
, FALSE
);
497 mutex_lock(audit_mtx
);
499 do_replacement_signal
= 1;
502 * Signal that replacement have occurred to wake up and
503 * start any other replacements started in parallel. We can
504 * continue about our business in the mean time. We
505 * broadcast so that both new replacements can be inserted,
506 * but also so that the source(s) of replacement can return
509 if (do_replacement_signal
)
510 wait_queue_wakeup_all(audit_wait_queue
,
511 AUDIT_REPLACEMENT_EVENT
, THREAD_AWAKENED
);
514 * Next, check to see if we have any records to drain into
515 * the vnode. If not, go back to waiting for an event.
517 if (TAILQ_EMPTY(&audit_q
)) {
520 AUDIT_PRINTF(("audit_worker waiting\n"));
521 ret
= wait_queue_assert_wait(audit_wait_queue
,
525 mutex_unlock(audit_mtx
);
527 assert(ret
== THREAD_WAITING
);
528 ret
= thread_block(THREAD_CONTINUE_NULL
);
529 assert(ret
== THREAD_AWAKENED
);
530 AUDIT_PRINTF(("audit_worker woken up\n"));
531 AUDIT_PRINTF(("audit_worker: new vp = %p; value of flag %d\n",
532 audit_replacement_vp
, audit_replacement_flag
));
534 mutex_lock(audit_mtx
);
539 * If we have records, but there's no active vnode to
540 * write to, drain the record queue. Generally, we
541 * prevent the unnecessary allocation of records
542 * elsewhere, but we need to allow for races between
543 * conditional allocation and queueing. Go back to
544 * waiting when we're done.
546 * XXX: We go out of our way to avoid calling audit_free()
547 * with the audit_mtx held, to avoid a lock order reversal
548 * as free() may grab the funnel. This will be fixed at
551 if (audit_vp
== NULL
) {
552 while ((ar
= TAILQ_FIRST(&audit_q
))) {
553 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
555 if (audit_q_len
<= audit_qctrl
.aq_lowater
)
556 wait_queue_wakeup_one(
561 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
563 mutex_unlock(audit_mtx
);
564 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
565 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
568 mutex_lock(audit_mtx
);
573 * We have both records to write, and an active vnode
574 * to write to. Dequeue a record, and start the write.
575 * Eventually, it might make sense to dequeue several
576 * records and perform our own clustering, if the lower
577 * layers aren't doing it automatically enough.
579 * XXX: We go out of our way to avoid calling audit_free()
580 * with the audit_mtx held, to avoid a lock order reversal
581 * as free() may grab the funnel. This will be fixed at
584 while ((ar
= TAILQ_FIRST(&audit_q
))) {
585 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
587 if (audit_q_len
<= audit_qctrl
.aq_lowater
) {
588 wait_queue_wakeup_one(audit_wait_queue
,
589 AUDIT_COMMIT_EVENT
, THREAD_AWAKENED
);
592 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
594 mutex_unlock(audit_mtx
);
596 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
597 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
598 if (audit_vp
!= NULL
) {
600 * XXX: What should happen if there's a write
603 if (!release_funnel
) {
604 thread_funnel_set(kernel_flock
, TRUE
);
607 error
= audit_write(audit_vp
, ar
, audit_cred
,
609 if (error
&& audit_panic_on_write_fail
) {
610 panic("audit_worker: write error %d\n",
613 printf("audit_worker: write error %d\n",
620 thread_funnel_set(kernel_flock
, FALSE
);
621 mutex_lock(audit_mtx
);
629 /* Verify that the syscall to audit event table is the same
630 * size as the system call table.
632 if (nsys_au_event
!= nsysent
) {
633 printf("Security auditing service initialization failed, ");
634 printf("audit event table doesn't match syscall table.\n");
638 printf("Security auditing service present\n");
639 TAILQ_INIT(&audit_q
);
643 audit_replacement_cred
= NULL
;
644 audit_replacement_flag
= 0;
645 audit_file_rotate_wait
= 0;
646 audit_replacement_vp
= NULL
;
647 audit_fstat
.af_filesz
= 0; /* '0' means unset, unbounded */
648 audit_fstat
.af_currsz
= 0;
649 audit_qctrl
.aq_hiwater
= AQ_HIWATER
;
650 audit_qctrl
.aq_lowater
= AQ_LOWATER
;
651 audit_qctrl
.aq_bufsz
= AQ_BUFSZ
;
652 audit_qctrl
.aq_minfree
= AU_FS_MINFREE
;
654 audit_mtx
= mutex_alloc(0);
655 audit_wait_queue
= wait_queue_alloc(SYNC_POLICY_FIFO
);
656 audit_zone
= zinit(sizeof(struct kaudit_record
),
657 AQ_HIWATER
*sizeof(struct kaudit_record
),
661 /* Initialize the BSM audit subsystem. */
666 audit_rotate_vnode(kauth_cred_t cred
, struct vnode
*vp
)
671 * If other parallel log replacements have been requested, we wait
672 * until they've finished before continuing.
674 mutex_lock(audit_mtx
);
675 while (audit_replacement_flag
!= 0) {
677 AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for "
679 ret
= wait_queue_assert_wait(audit_wait_queue
,
680 AUDIT_REPLACEMENT_EVENT
,
683 mutex_unlock(audit_mtx
);
685 assert(ret
== THREAD_WAITING
);
686 ret
= thread_block(THREAD_CONTINUE_NULL
);
687 assert(ret
== THREAD_AWAKENED
);
688 AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n",
689 audit_replacement_flag
));
691 mutex_lock(audit_mtx
);
693 audit_replacement_cred
= cred
;
694 audit_replacement_flag
= 1;
695 audit_replacement_vp
= vp
;
698 * Start or wake up the audit worker to perform the exchange.
699 * It will have to wait until we release the mutex.
701 if (audit_worker_thread
== THREAD_NULL
)
702 audit_worker_thread
= kernel_thread(kernel_task
,
705 wait_queue_wakeup_one(audit_wait_queue
,
710 * Wait for the audit_worker to broadcast that a replacement has
711 * taken place; we know that once this has happened, our vnode
712 * has been replaced in, so we can return successfully.
714 AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of "
716 ret
= wait_queue_assert_wait(audit_wait_queue
,
717 AUDIT_REPLACEMENT_EVENT
,
720 mutex_unlock(audit_mtx
);
722 assert(ret
== THREAD_WAITING
);
723 ret
= thread_block(THREAD_CONTINUE_NULL
);
724 assert(ret
== THREAD_AWAKENED
);
725 AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by "
726 "audit_worker (flag " "now %d)\n", audit_replacement_flag
));
728 audit_file_rotate_wait
= 0; /* We can now request another rotation */
732 * Drain the audit queue and close the log at shutdown.
737 audit_rotate_vnode(NULL
, NULL
);
740 static __inline__
struct uthread
*
743 return (get_bsdthread_info(current_thread()));
746 static __inline__
struct kaudit_record
*
749 return (curuthread()->uu_ar
);
752 /**********************************
753 * Begin system calls. *
754 **********************************/
756 * System call to allow a user space application to submit a BSM audit
757 * record to the kernel for inclusion in the audit log. This function
758 * does little verification on the audit record that is submitted.
760 * XXXAUDIT: Audit preselection for user records does not currently
761 * work, since we pre-select only based on the AUE_audit event type,
762 * not the event type submitted as part of the user audit data.
766 audit(struct proc
*p
, struct audit_args
*uap
, __unused register_t
*retval
)
770 struct kaudit_record
*ar
;
771 struct uthread
*uthr
;
773 error
= suser(kauth_cred_get(), &p
->p_acflag
);
777 if ((uap
->length
<= 0) || (uap
->length
> (int)audit_qctrl
.aq_bufsz
))
782 /* If there's no current audit record (audit() itself not audited)
783 * commit the user audit record.
787 if (uthr
== NULL
) /* can this happen? */
790 /* This is not very efficient; we're required to allocate
791 * a complete kernel audit record just so the user record
794 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
795 if (uthr
->uu_ar
== NULL
) /* auditing not on, or memory error */
800 if (uap
->length
> MAX_AUDIT_RECORD_SIZE
)
803 rec
= (void *)kalloc((vm_size_t
)uap
->length
);
805 error
= copyin(uap
->record
, rec
, uap
->length
);
809 /* Verify the record */
810 if (bsm_rec_verify(rec
) == 0) {
815 /* Attach the user audit record to the kernel audit record. Because
816 * this system call is an auditable event, we will write the user
817 * record along with the record for this audit event.
820 ar
->k_ar_commit
|= AR_COMMIT_USER
;
821 ar
->k_ulen
= uap
->length
;
825 /* audit_syscall_exit() will free the audit record on the thread
826 * even if we allocated it above.
828 kfree(rec
, uap
->length
);
833 * System call to manipulate auditing.
837 auditon(struct proc
*p
, __unused
struct auditon_args
*uap
, __unused register_t
*retval
)
841 union auditon_udata udata
;
844 AUDIT_ARG(cmd
, uap
->cmd
);
845 ret
= suser(kauth_cred_get(), &p
->p_acflag
);
850 if ((len
<= 0) || (len
> (int)sizeof(union auditon_udata
)))
853 memset((void *)&udata
, 0, sizeof(udata
));
856 /* Some of the GET commands use the arguments too */
870 case A_GETPINFO_ADDR
:
871 ret
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
874 AUDIT_ARG(auditon
, &udata
);
878 /* XXX Need to implement these commands by accessing the global
879 * values associated with the commands.
883 if (!audit_fail_stop
)
884 udata
.au_policy
|= AUDIT_CNT
;
885 if (audit_panic_on_write_fail
)
886 udata
.au_policy
|= AUDIT_AHLT
;
889 if (udata
.au_policy
& ~(AUDIT_CNT
|AUDIT_AHLT
))
892 * XXX - Need to wake up waiters if the policy relaxes?
894 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
895 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
898 udata
.au_mask
= audit_nae_mask
;
901 audit_nae_mask
= udata
.au_mask
;
904 udata
.au_qctrl
= audit_qctrl
;
907 if ((udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
908 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
909 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
910 (udata
.au_qctrl
.aq_minfree
< 0) ||
911 (udata
.au_qctrl
.aq_minfree
> 100))
914 audit_qctrl
= udata
.au_qctrl
;
915 /* XXX The queue delay value isn't used with the kernel. */
916 audit_qctrl
.aq_delay
= -1;
937 if (audit_enabled
&& !audit_suspended
)
938 udata
.au_cond
= AUC_AUDITING
;
940 udata
.au_cond
= AUC_NOAUDIT
;
943 if (udata
.au_cond
== AUC_NOAUDIT
)
945 if (udata
.au_cond
== AUC_AUDITING
)
947 if (udata
.au_cond
== AUC_DISABLED
) {
953 udata
.au_evclass
.ec_class
=
954 au_event_class(udata
.au_evclass
.ec_number
);
957 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
958 udata
.au_evclass
.ec_class
);
961 if (udata
.au_aupinfo
.ap_pid
< 1)
963 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
966 udata
.au_aupinfo
.ap_auid
= tp
->p_ucred
->cr_au
.ai_auid
;
967 udata
.au_aupinfo
.ap_mask
.am_success
=
968 tp
->p_ucred
->cr_au
.ai_mask
.am_success
;
969 udata
.au_aupinfo
.ap_mask
.am_failure
=
970 tp
->p_ucred
->cr_au
.ai_mask
.am_failure
;
971 udata
.au_aupinfo
.ap_termid
.machine
=
972 tp
->p_ucred
->cr_au
.ai_termid
.machine
;
973 udata
.au_aupinfo
.ap_termid
.port
=
974 tp
->p_ucred
->cr_au
.ai_termid
.port
;
975 udata
.au_aupinfo
.ap_asid
= tp
->p_ucred
->cr_au
.ai_asid
;
978 if (udata
.au_aupinfo
.ap_pid
< 1)
980 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
984 * we are modifying the audit info in a credential so we need a new
985 * credential (or take another reference on an existing credential that
986 * matches our new one). We must do this because the audit info in the
987 * credential is used as part of our hash key. Get current credential
988 * in the target process and take a reference while we muck with it.
991 kauth_cred_t my_cred
, my_new_cred
;
992 struct auditinfo temp_auditinfo
;
994 my_cred
= kauth_cred_proc_ref(tp
);
996 * Set the credential with new info. If there is no
997 * change, we get back the same credential we passed
998 * in; if there is a change, we drop the reference on
999 * the credential we passed in. The subsequent
1000 * compare is safe, because it is a pointer compare
1001 * rather than a contents compare.
1003 temp_auditinfo
= my_cred
->cr_au
;
1004 temp_auditinfo
.ai_mask
.am_success
=
1005 udata
.au_aupinfo
.ap_mask
.am_success
;
1006 temp_auditinfo
.ai_mask
.am_failure
=
1007 udata
.au_aupinfo
.ap_mask
.am_failure
;
1008 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1010 if (my_cred
!= my_new_cred
) {
1012 /* need to protect for a race where another thread also changed
1013 * the credential after we took our reference. If p_ucred has
1014 * changed then we should restart this again with the new cred.
1016 if (tp
->p_ucred
!= my_cred
) {
1018 kauth_cred_unref(&my_new_cred
);
1022 tp
->p_ucred
= my_new_cred
;
1025 /* drop old proc reference or our extra reference */
1026 kauth_cred_unref(&my_cred
);
1031 if ((udata
.au_fstat
.af_filesz
!= 0) &&
1032 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
))
1034 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
1037 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
1038 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
1040 case A_GETPINFO_ADDR
:
1050 /* Copy data back to userspace for the GET comands */
1062 case A_GETPINFO_ADDR
:
1064 ret
= copyout((void *)&udata
, uap
->data
, uap
->length
);
1074 * System calls to manage the user audit information.
1075 * XXXAUDIT May need to lock the proc structure.
1079 getauid(struct proc
*p
, struct getauid_args
*uap
, __unused register_t
*retval
)
1083 error
= copyout((void *)&kauth_cred_get()->cr_au
.ai_auid
,
1084 uap
->auid
, sizeof(au_id_t
));
1093 setauid(struct proc
*p
, struct setauid_args
*uap
, __unused register_t
*retval
)
1098 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1102 error
= copyin(uap
->auid
,
1103 (void *)&temp_au_id
,
1109 * we are modifying the audit info in a credential so we need a new
1110 * credential (or take another reference on an existing credential that
1111 * matches our new one). We must do this because the audit info in the
1112 * credential is used as part of our hash key. Get current credential
1113 * in the target process and take a reference while we muck with it.
1116 kauth_cred_t my_cred
, my_new_cred
;
1117 struct auditinfo temp_auditinfo
;
1119 my_cred
= kauth_cred_proc_ref(p
);
1121 * Set the credential with new info. If there is no change,
1122 * we get back the same credential we passed in; if there is
1123 * a change, we drop the reference on the credential we
1124 * passed in. The subsequent compare is safe, because it is
1125 * a pointer compare rather than a contents compare.
1127 temp_auditinfo
= my_cred
->cr_au
;
1128 temp_auditinfo
.ai_auid
= temp_au_id
;
1129 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1131 if (my_cred
!= my_new_cred
) {
1133 /* need to protect for a race where another thread also changed
1134 * the credential after we took our reference. If p_ucred has
1135 * changed then we should restart this again with the new cred.
1137 if (p
->p_ucred
!= my_cred
) {
1139 kauth_cred_unref(&my_new_cred
);
1143 p
->p_ucred
= my_new_cred
;
1146 /* drop old proc reference or our extra reference */
1147 kauth_cred_unref(&my_cred
);
1151 /* propagate the change from the process to Mach task */
1152 set_security_token(p
);
1154 audit_arg_auid(kauth_cred_get()->cr_au
.ai_auid
);
1159 * System calls to get and set process audit information.
1160 * If the caller is privileged, they get the whole set of
1161 * audit information. Otherwise, the real audit mask is
1162 * filtered out - but the rest of the information is
1167 getaudit(struct proc
*p
, struct getaudit_args
*uap
, __unused register_t
*retval
)
1169 struct auditinfo ai
;
1172 ai
= kauth_cred_get()->cr_au
;
1174 /* only superuser gets to see the real mask */
1175 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1177 ai
.ai_mask
.am_success
= ~0;
1178 ai
.ai_mask
.am_failure
= ~0;
1181 error
= copyout(&ai
, uap
->auditinfo
, sizeof(ai
));
1190 setaudit(struct proc
*p
, struct setaudit_args
*uap
, __unused register_t
*retval
)
1193 struct auditinfo temp_auditinfo
;
1194 kauth_cred_t safecred
;
1196 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1200 error
= copyin(uap
->auditinfo
,
1201 (void *)&temp_auditinfo
,
1202 sizeof(temp_auditinfo
));
1207 * we are modifying the audit info in a credential so we need a new
1208 * credential (or take another reference on an existing credential that
1209 * matches our new one). We must do this because the audit info in the
1210 * credential is used as part of our hash key. Get current credential
1211 * in the target process and take a reference while we muck with it.
1214 kauth_cred_t my_cred
, my_new_cred
;
1216 my_cred
= kauth_cred_proc_ref(p
);
1218 * Set the credential with new info. If there is no change,
1219 * we get back the same credential we passed in; if there is
1220 * a change, we drop the reference on the credential we
1221 * passed in. The subsequent compare is safe, because it is
1222 * a pointer compare rather than a contents compare.
1224 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1226 if (my_cred
!= my_new_cred
) {
1228 /* need to protect for a race where another thread also changed
1229 * the credential after we took our reference. If p_ucred has
1230 * changed then we should restart this again with the new cred.
1232 if (p
->p_ucred
!= my_cred
) {
1234 kauth_cred_unref(&my_new_cred
);
1238 p
->p_ucred
= my_new_cred
;
1241 /* drop old proc reference or our extra reference */
1242 kauth_cred_unref(&my_cred
);
1246 /* propagate the change from the process to Mach task */
1247 set_security_token(p
);
1249 safecred
= kauth_cred_proc_ref(p
);
1250 audit_arg_auditinfo(&safecred
->cr_au
);
1251 kauth_cred_unref(&safecred
);
1258 getaudit_addr(struct proc
*p
, __unused
struct getaudit_addr_args
*uap
, __unused register_t
*retval
)
1265 setaudit_addr(struct proc
*p
, __unused
struct setaudit_addr_args
*uap
, __unused register_t
*retval
)
1269 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1276 * Syscall to manage audit files.
1281 auditctl(struct proc
*p
, struct auditctl_args
*uap
, __unused register_t
*retval
)
1283 struct nameidata nd
;
1287 struct vfs_context context
;
1289 context
.vc_proc
= p
;
1290 context
.vc_ucred
= kauth_cred_get();
1292 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1300 * If a path is specified, open the replacement vnode, perform
1301 * validity checks, and grab another reference to the current
1304 if (uap
->path
!= 0) {
1305 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1306 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1307 uap
->path
, &context
);
1308 flags
= audit_open_flags
;
1309 error
= vn_open(&nd
, flags
, 0);
1313 if (vp
->v_type
!= VREG
) {
1314 vn_close(vp
, audit_close_flags
, kauth_cred_get(), p
);
1319 cred
= kauth_cred_get_with_ref();
1320 audit_suspended
= 0;
1323 * a vp and cred of NULL is valid at this point
1324 * and indicates we're to turn off auditing...
1326 audit_rotate_vnode(cred
, vp
);
1333 /**********************************
1334 * End of system calls. *
1335 **********************************/
1340 struct kaudit_record
*
1341 audit_new(int event
, struct proc
*p
, __unused
struct uthread
*uthread
)
1343 struct kaudit_record
*ar
;
1345 kauth_cred_t safecred
;
1348 * Eventually, there may be certain classes of events that
1349 * we will audit regardless of the audit state at the time
1350 * the record is created. These events will generally
1351 * correspond to changes in the audit state. The dummy
1352 * code below is from our first prototype, but may also
1353 * be used in the final version (with modified event numbers).
1356 if (event
!= AUDIT_EVENT_FILESTOP
&& event
!= AUDIT_EVENT_FILESTART
) {
1358 mutex_lock(audit_mtx
);
1359 no_record
= (audit_suspended
|| !audit_enabled
);
1360 mutex_unlock(audit_mtx
);
1368 * Initialize the audit record header.
1369 * XXX: We may want to fail-stop if allocation fails.
1370 * XXX: The number of outstanding uncommitted audit records is
1371 * limited by the number of concurrent threads servicing system
1372 * calls in the kernel.
1375 ar
= (struct kaudit_record
*)zalloc(audit_zone
);
1379 mutex_lock(audit_mtx
);
1381 mutex_unlock(audit_mtx
);
1383 bzero(ar
, sizeof(*ar
));
1384 ar
->k_ar
.ar_magic
= AUDIT_RECORD_MAGIC
;
1385 ar
->k_ar
.ar_event
= event
;
1386 nanotime(&ar
->k_ar
.ar_starttime
);
1388 safecred
= kauth_cred_proc_ref(p
);
1389 /* Export the subject credential. */
1390 cru2x(safecred
, &ar
->k_ar
.ar_subj_cred
);
1392 ar
->k_ar
.ar_subj_ruid
= safecred
->cr_ruid
;
1393 ar
->k_ar
.ar_subj_rgid
= safecred
->cr_rgid
;
1394 ar
->k_ar
.ar_subj_egid
= safecred
->cr_groups
[0];
1395 ar
->k_ar
.ar_subj_auid
= safecred
->cr_au
.ai_auid
;
1396 ar
->k_ar
.ar_subj_asid
= safecred
->cr_au
.ai_asid
;
1397 ar
->k_ar
.ar_subj_amask
= safecred
->cr_au
.ai_mask
;
1398 ar
->k_ar
.ar_subj_term
= safecred
->cr_au
.ai_termid
;
1399 kauth_cred_unref(&safecred
);
1401 ar
->k_ar
.ar_subj_pid
= p
->p_pid
;
1402 bcopy(p
->p_comm
, ar
->k_ar
.ar_subj_comm
, MAXCOMLEN
);
1409 * XXXAUDIT: So far, this is unused, and should probably be GC'd.
1412 audit_abort(struct kaudit_record
*ar
)
1414 mutex_lock(audit_mtx
);
1416 mutex_unlock(audit_mtx
);
1424 audit_commit(struct kaudit_record
*ar
, int error
, int retval
)
1428 struct au_mask
*aumask
;
1434 * Decide whether to commit the audit record by checking the
1435 * error value from the system call and using the appropriate
1438 if (ar
->k_ar
.ar_subj_auid
== AU_DEFAUDITID
)
1439 aumask
= &audit_nae_mask
;
1441 aumask
= &ar
->k_ar
.ar_subj_amask
;
1444 sorf
= AU_PRS_FAILURE
;
1446 sorf
= AU_PRS_SUCCESS
;
1448 switch(ar
->k_ar
.ar_event
) {
1451 /* The open syscall always writes a OPEN_RWTC event; limit the
1452 * to the proper type of event based on the flags and the error
1455 ar
->k_ar
.ar_event
= flags_and_error_to_openevent(ar
->k_ar
.ar_arg_fflags
, error
);
1459 ar
->k_ar
.ar_event
= ctlname_to_sysctlevent(ar
->k_ar
.ar_arg_ctlname
, ar
->k_ar
.ar_valid_arg
);
1463 /* Convert the auditon() command to an event */
1464 ar
->k_ar
.ar_event
= auditon_command_event(ar
->k_ar
.ar_arg_cmd
);
1468 if (au_preselect(ar
->k_ar
.ar_event
, aumask
, sorf
) != 0)
1469 ar
->k_ar_commit
|= AR_COMMIT_KERNEL
;
1471 if ((ar
->k_ar_commit
& (AR_COMMIT_USER
| AR_COMMIT_KERNEL
)) == 0) {
1472 mutex_lock(audit_mtx
);
1474 mutex_unlock(audit_mtx
);
1479 ar
->k_ar
.ar_errno
= error
;
1480 ar
->k_ar
.ar_retval
= retval
;
1483 * We might want to do some system-wide post-filtering
1484 * here at some point.
1488 * Timestamp system call end.
1490 nanotime(&ar
->k_ar
.ar_endtime
);
1492 mutex_lock(audit_mtx
);
1494 * Note: it could be that some records initiated while audit was
1495 * enabled should still be committed?
1497 if (audit_suspended
|| !audit_enabled
) {
1499 mutex_unlock(audit_mtx
);
1505 * Constrain the number of committed audit records based on
1506 * the configurable parameter.
1508 while (audit_q_len
>= audit_qctrl
.aq_hiwater
) {
1510 ret
= wait_queue_assert_wait(audit_wait_queue
,
1514 mutex_unlock(audit_mtx
);
1516 assert(ret
== THREAD_WAITING
);
1518 ret
= thread_block(THREAD_CONTINUE_NULL
);
1519 assert(ret
== THREAD_AWAKENED
);
1520 mutex_lock(audit_mtx
);
1523 TAILQ_INSERT_TAIL(&audit_q
, ar
, k_q
);
1526 wait_queue_wakeup_one(audit_wait_queue
, AUDIT_WORKER_EVENT
, THREAD_AWAKENED
);
1527 mutex_unlock(audit_mtx
);
1531 * Calls to set up and tear down audit structures associated with
1535 audit_syscall_enter(unsigned short code
, struct proc
*proc
,
1536 struct uthread
*uthread
)
1539 struct au_mask
*aumask
;
1541 audit_event
= sys_au_event
[code
];
1542 if (audit_event
== AUE_NULL
)
1545 assert(uthread
->uu_ar
== NULL
);
1547 /* Check which audit mask to use; either the kernel non-attributable
1548 * event mask or the process audit mask.
1550 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1551 aumask
= &audit_nae_mask
;
1553 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1556 * Allocate an audit record, if preselection allows it, and store
1557 * in the BSD thread for later use.
1559 if (au_preselect(audit_event
, aumask
,
1560 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1562 * If we're out of space and need to suspend unprivileged
1563 * processes, do that here rather than trying to allocate
1564 * another audit record.
1566 if (audit_in_failure
&&
1567 suser(kauth_cred_get(), &proc
->p_acflag
) != 0) {
1570 assert(audit_worker_thread
!= THREAD_NULL
);
1571 ret
= wait_queue_assert_wait(audit_wait_queue
,
1572 AUDIT_FAILURE_EVENT
, THREAD_UNINT
, 0);
1573 assert(ret
== THREAD_WAITING
);
1574 (void)thread_block(THREAD_CONTINUE_NULL
);
1575 panic("audit_failing_stop: thread continued");
1577 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1579 uthread
->uu_ar
= NULL
;
1584 audit_syscall_exit(int error
, AUDIT_PRINTF_ONLY
struct proc
*proc
, struct uthread
*uthread
)
1589 * Commit the audit record as desired; once we pass the record
1590 * into audit_commit(), the memory is owned by the audit
1592 * The return value from the system call is stored on the user
1593 * thread. If there was an error, the return value is set to -1,
1594 * imitating the behavior of the cerror routine.
1599 retval
= uthread
->uu_rval
[0];
1601 audit_commit(uthread
->uu_ar
, error
, retval
);
1602 if (uthread
->uu_ar
!= NULL
) {
1603 AUDIT_PRINTF(("audit record committed by pid %d\n", proc
->p_pid
));
1605 uthread
->uu_ar
= NULL
;
1610 * Calls to set up and tear down audit structures used during Mach
1614 audit_mach_syscall_enter(unsigned short audit_event
)
1616 struct uthread
*uthread
;
1618 struct au_mask
*aumask
;
1620 if (audit_event
== AUE_NULL
)
1623 uthread
= curuthread();
1624 if (uthread
== NULL
)
1627 proc
= current_proc();
1631 assert(uthread
->uu_ar
== NULL
);
1633 /* Check which audit mask to use; either the kernel non-attributable
1634 * event mask or the process audit mask.
1636 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1637 aumask
= &audit_nae_mask
;
1639 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1642 * Allocate an audit record, if desired, and store in the BSD
1643 * thread for later use.
1645 if (au_preselect(audit_event
, aumask
,
1646 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1647 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1649 uthread
->uu_ar
= NULL
;
1654 audit_mach_syscall_exit(int retval
, struct uthread
*uthread
)
1656 /* The error code from Mach system calls is the same as the
1659 /* XXX Is the above statement always true? */
1660 audit_commit(uthread
->uu_ar
, retval
, retval
);
1661 uthread
->uu_ar
= NULL
;
1666 * Calls to manipulate elements of the audit record structure from system
1667 * call code. Macro wrappers will prevent this functions from being
1668 * entered if auditing is disabled, avoiding the function call cost. We
1669 * check the thread audit record pointer anyway, as the audit condition
1670 * could change, and pre-selection may not have allocated an audit
1671 * record for this event.
1674 audit_arg_addr(user_addr_t addr
)
1676 struct kaudit_record
*ar
;
1682 ar
->k_ar
.ar_arg_addr
= CAST_DOWN(void *, addr
); /* XXX */
1683 ar
->k_ar
.ar_valid_arg
|= ARG_ADDR
;
1687 audit_arg_len(user_size_t len
)
1689 struct kaudit_record
*ar
;
1695 ar
->k_ar
.ar_arg_len
= CAST_DOWN(int, len
); /* XXX */
1696 ar
->k_ar
.ar_valid_arg
|= ARG_LEN
;
1700 audit_arg_fd(int fd
)
1702 struct kaudit_record
*ar
;
1708 ar
->k_ar
.ar_arg_fd
= fd
;
1709 ar
->k_ar
.ar_valid_arg
|= ARG_FD
;
1713 audit_arg_fflags(int fflags
)
1715 struct kaudit_record
*ar
;
1721 ar
->k_ar
.ar_arg_fflags
= fflags
;
1722 ar
->k_ar
.ar_valid_arg
|= ARG_FFLAGS
;
1726 audit_arg_gid(gid_t gid
, gid_t egid
, gid_t rgid
, gid_t sgid
)
1728 struct kaudit_record
*ar
;
1734 ar
->k_ar
.ar_arg_gid
= gid
;
1735 ar
->k_ar
.ar_arg_egid
= egid
;
1736 ar
->k_ar
.ar_arg_rgid
= rgid
;
1737 ar
->k_ar
.ar_arg_sgid
= sgid
;
1738 ar
->k_ar
.ar_valid_arg
|= (ARG_GID
| ARG_EGID
| ARG_RGID
| ARG_SGID
);
1742 audit_arg_uid(uid_t uid
, uid_t euid
, uid_t ruid
, uid_t suid
)
1744 struct kaudit_record
*ar
;
1750 ar
->k_ar
.ar_arg_uid
= uid
;
1751 ar
->k_ar
.ar_arg_euid
= euid
;
1752 ar
->k_ar
.ar_arg_ruid
= ruid
;
1753 ar
->k_ar
.ar_arg_suid
= suid
;
1754 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_EUID
| ARG_RUID
| ARG_SUID
);
1758 audit_arg_groupset(const gid_t
*gidset
, u_int gidset_size
)
1761 struct kaudit_record
*ar
;
1767 for (i
= 0; i
< gidset_size
; i
++)
1768 ar
->k_ar
.ar_arg_groups
.gidset
[i
] = gidset
[i
];
1769 ar
->k_ar
.ar_arg_groups
.gidset_size
= gidset_size
;
1770 ar
->k_ar
.ar_valid_arg
|= ARG_GROUPSET
;
1774 audit_arg_login(const char *login
)
1776 struct kaudit_record
*ar
;
1784 * XXX: Add strlcpy() to Darwin for improved safety.
1786 strlcpy(ar
->k_ar
.ar_arg_login
, login
, MAXLOGNAME
);
1788 strcpy(ar
->k_ar
.ar_arg_login
, login
);
1791 ar
->k_ar
.ar_valid_arg
|= ARG_LOGIN
;
1795 audit_arg_ctlname(const int *name
, int namelen
)
1797 struct kaudit_record
*ar
;
1803 bcopy(name
, &ar
->k_ar
.ar_arg_ctlname
, namelen
* sizeof(int));
1804 ar
->k_ar
.ar_arg_len
= namelen
;
1805 ar
->k_ar
.ar_valid_arg
|= (ARG_CTLNAME
| ARG_LEN
);
1809 audit_arg_mask(int mask
)
1811 struct kaudit_record
*ar
;
1817 ar
->k_ar
.ar_arg_mask
= mask
;
1818 ar
->k_ar
.ar_valid_arg
|= ARG_MASK
;
1822 audit_arg_mode(mode_t mode
)
1824 struct kaudit_record
*ar
;
1830 ar
->k_ar
.ar_arg_mode
= mode
;
1831 ar
->k_ar
.ar_valid_arg
|= ARG_MODE
;
1835 audit_arg_dev(int dev
)
1837 struct kaudit_record
*ar
;
1843 ar
->k_ar
.ar_arg_dev
= dev
;
1844 ar
->k_ar
.ar_valid_arg
|= ARG_DEV
;
1848 audit_arg_value(long value
)
1850 struct kaudit_record
*ar
;
1856 ar
->k_ar
.ar_arg_value
= value
;
1857 ar
->k_ar
.ar_valid_arg
|= ARG_VALUE
;
1861 audit_arg_owner(uid_t uid
, gid_t gid
)
1863 struct kaudit_record
*ar
;
1869 ar
->k_ar
.ar_arg_uid
= uid
;
1870 ar
->k_ar
.ar_arg_gid
= gid
;
1871 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_GID
);
1875 audit_arg_pid(pid_t pid
)
1877 struct kaudit_record
*ar
;
1883 ar
->k_ar
.ar_arg_pid
= pid
;
1884 ar
->k_ar
.ar_valid_arg
|= ARG_PID
;
1888 audit_arg_process(struct proc
*p
)
1890 struct kaudit_record
*ar
;
1893 if ((ar
== NULL
) || (p
== NULL
))
1896 ar
->k_ar
.ar_arg_auid
= p
->p_ucred
->cr_au
.ai_auid
;
1897 ar
->k_ar
.ar_arg_euid
= p
->p_ucred
->cr_uid
;
1898 ar
->k_ar
.ar_arg_egid
= p
->p_ucred
->cr_groups
[0];
1899 ar
->k_ar
.ar_arg_ruid
= p
->p_ucred
->cr_ruid
;
1900 ar
->k_ar
.ar_arg_rgid
= p
->p_ucred
->cr_rgid
;
1901 ar
->k_ar
.ar_arg_asid
= p
->p_ucred
->cr_au
.ai_asid
;
1902 ar
->k_ar
.ar_arg_termid
= p
->p_ucred
->cr_au
.ai_termid
;
1904 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_EUID
| ARG_EGID
| ARG_RUID
|
1905 ARG_RGID
| ARG_ASID
| ARG_TERMID
| ARG_PROCESS
;
1909 audit_arg_signum(u_int signum
)
1911 struct kaudit_record
*ar
;
1917 ar
->k_ar
.ar_arg_signum
= signum
;
1918 ar
->k_ar
.ar_valid_arg
|= ARG_SIGNUM
;
1922 audit_arg_socket(int sodomain
, int sotype
, int soprotocol
)
1925 struct kaudit_record
*ar
;
1931 ar
->k_ar
.ar_arg_sockinfo
.so_domain
= sodomain
;
1932 ar
->k_ar
.ar_arg_sockinfo
.so_type
= sotype
;
1933 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
= soprotocol
;
1934 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
1938 audit_arg_sockaddr(struct proc
*p
, struct sockaddr
*so
)
1940 struct kaudit_record
*ar
;
1943 if (ar
== NULL
|| p
== NULL
|| so
== NULL
)
1946 bcopy(so
, &ar
->k_ar
.ar_arg_sockaddr
, sizeof(ar
->k_ar
.ar_arg_sockaddr
));
1947 switch (so
->sa_family
) {
1949 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET
;
1952 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET6
;
1955 audit_arg_upath(p
, ((struct sockaddr_un
*)so
)->sun_path
,
1957 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRUNIX
;
1963 audit_arg_auid(uid_t auid
)
1965 struct kaudit_record
*ar
;
1971 ar
->k_ar
.ar_arg_auid
= auid
;
1972 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
;
1976 audit_arg_auditinfo(const struct auditinfo
*au_info
)
1978 struct kaudit_record
*ar
;
1984 ar
->k_ar
.ar_arg_auid
= au_info
->ai_auid
;
1985 ar
->k_ar
.ar_arg_asid
= au_info
->ai_asid
;
1986 ar
->k_ar
.ar_arg_amask
.am_success
= au_info
->ai_mask
.am_success
;
1987 ar
->k_ar
.ar_arg_amask
.am_failure
= au_info
->ai_mask
.am_failure
;
1988 ar
->k_ar
.ar_arg_termid
.port
= au_info
->ai_termid
.port
;
1989 ar
->k_ar
.ar_arg_termid
.machine
= au_info
->ai_termid
.machine
;
1990 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_ASID
| ARG_AMASK
| ARG_TERMID
;
1994 audit_arg_text(const char *text
)
1996 struct kaudit_record
*ar
;
2002 /* Invalidate the text string */
2003 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_TEXT
);
2007 if (ar
->k_ar
.ar_arg_text
== NULL
) {
2008 ar
->k_ar
.ar_arg_text
= (char *)kalloc(MAXPATHLEN
);
2009 if (ar
->k_ar
.ar_arg_text
== NULL
)
2013 strncpy(ar
->k_ar
.ar_arg_text
, text
, MAXPATHLEN
);
2014 ar
->k_ar
.ar_valid_arg
|= ARG_TEXT
;
2018 audit_arg_cmd(int cmd
)
2020 struct kaudit_record
*ar
;
2026 ar
->k_ar
.ar_arg_cmd
= cmd
;
2027 ar
->k_ar
.ar_valid_arg
|= ARG_CMD
;
2031 audit_arg_svipc_cmd(int cmd
)
2033 struct kaudit_record
*ar
;
2039 ar
->k_ar
.ar_arg_svipc_cmd
= cmd
;
2040 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_CMD
;
2044 audit_arg_svipc_perm(const struct ipc_perm
*perm
)
2046 struct kaudit_record
*ar
;
2052 bcopy(perm
, &ar
->k_ar
.ar_arg_svipc_perm
,
2053 sizeof(ar
->k_ar
.ar_arg_svipc_perm
));
2054 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_PERM
;
2058 audit_arg_svipc_id(int id
)
2060 struct kaudit_record
*ar
;
2066 ar
->k_ar
.ar_arg_svipc_id
= id
;
2067 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ID
;
2071 audit_arg_svipc_addr(void * addr
)
2073 struct kaudit_record
*ar
;
2079 ar
->k_ar
.ar_arg_svipc_addr
= addr
;
2080 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ADDR
;
2084 audit_arg_posix_ipc_perm(uid_t uid
, gid_t gid
, mode_t mode
)
2086 struct kaudit_record
*ar
;
2092 ar
->k_ar
.ar_arg_pipc_perm
.pipc_uid
= uid
;
2093 ar
->k_ar
.ar_arg_pipc_perm
.pipc_gid
= gid
;
2094 ar
->k_ar
.ar_arg_pipc_perm
.pipc_mode
= mode
;
2095 ar
->k_ar
.ar_valid_arg
|= ARG_POSIX_IPC_PERM
;
2099 audit_arg_auditon(const union auditon_udata
*udata
)
2101 struct kaudit_record
*ar
;
2107 bcopy((const void *)udata
, &ar
->k_ar
.ar_arg_auditon
,
2108 sizeof(ar
->k_ar
.ar_arg_auditon
));
2109 ar
->k_ar
.ar_valid_arg
|= ARG_AUDITON
;
2113 * Audit information about a file, either the file's vnode info, or its
2114 * socket address info.
2117 audit_arg_file(__unused
struct proc
*p
, const struct fileproc
*fp
)
2119 struct kaudit_record
*ar
;
2123 if (fp
->f_fglob
->fg_type
== DTYPE_VNODE
) {
2124 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2128 if (fp
->f_fglob
->fg_type
== DTYPE_SOCKET
) {
2132 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
2133 if (INP_CHECK_SOCKAF(so
, PF_INET
)) {
2134 if (so
->so_pcb
== NULL
)
2136 ar
->k_ar
.ar_arg_sockinfo
.so_type
=
2138 ar
->k_ar
.ar_arg_sockinfo
.so_domain
=
2140 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
=
2141 so
->so_proto
->pr_protocol
;
2142 pcb
= (struct inpcb
*)so
->so_pcb
;
2143 ar
->k_ar
.ar_arg_sockinfo
.so_raddr
=
2144 pcb
->inp_faddr
.s_addr
;
2145 ar
->k_ar
.ar_arg_sockinfo
.so_laddr
=
2146 pcb
->inp_laddr
.s_addr
;
2147 ar
->k_ar
.ar_arg_sockinfo
.so_rport
=
2149 ar
->k_ar
.ar_arg_sockinfo
.so_lport
=
2151 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
2159 * Store a path as given by the user process for auditing into the audit
2160 * record stored on the user thread. This function will allocate the memory to
2161 * store the path info if not already available. This memory will be
2162 * freed when the audit record is freed.
2165 audit_arg_upath(struct proc
*p
, char *upath
, u_int64_t flags
)
2167 struct kaudit_record
*ar
;
2170 if (p
== NULL
|| upath
== NULL
)
2171 return; /* nothing to do! */
2173 if ((flags
& (ARG_UPATH1
| ARG_UPATH2
)) == 0)
2177 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2180 if (flags
& ARG_UPATH1
) {
2181 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH1
);
2182 pathp
= &ar
->k_ar
.ar_arg_upath1
;
2185 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH2
);
2186 pathp
= &ar
->k_ar
.ar_arg_upath2
;
2189 if (*pathp
== NULL
) {
2190 *pathp
= (char *)kalloc(MAXPATHLEN
);
2195 if (canon_path(p
, upath
, *pathp
) == 0) {
2196 if (flags
& ARG_UPATH1
)
2197 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH1
;
2199 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH2
;
2201 kfree(*pathp
, MAXPATHLEN
);
2207 * Function to save the path and vnode attr information into the audit
2210 * It is assumed that the caller will hold any vnode locks necessary to
2211 * perform a VNOP_GETATTR() on the passed vnode.
2213 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but
2214 * always provides access to the generation number as we need that
2215 * to construct the BSM file ID.
2216 * XXX: We should accept the process argument from the caller, since
2217 * it's very likely they already have a reference.
2218 * XXX: Error handling in this function is poor.
2221 audit_arg_vnpath(struct vnode
*vp
, u_int64_t flags
)
2223 struct kaudit_record
*ar
;
2224 struct vnode_attr va
;
2228 struct vnode_au_info
*vnp
;
2230 struct vfs_context context
;
2236 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2239 if ((flags
& (ARG_VNODE1
| ARG_VNODE2
)) == 0)
2244 if (flags
& ARG_VNODE1
) {
2245 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH1
);
2246 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE1
);
2247 pathp
= &ar
->k_ar
.ar_arg_kpath1
;
2248 vnp
= &ar
->k_ar
.ar_arg_vnode1
;
2251 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH2
);
2252 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE2
);
2253 pathp
= &ar
->k_ar
.ar_arg_kpath2
;
2254 vnp
= &ar
->k_ar
.ar_arg_vnode2
;
2257 if (*pathp
== NULL
) {
2258 *pathp
= (char *)kalloc(MAXPATHLEN
);
2264 * If vn_getpath() succeeds, place it in a string buffer
2265 * attached to the audit record, and set a flag indicating
2269 if (vn_getpath(vp
, *pathp
, &len
) == 0) {
2270 if (flags
& ARG_VNODE1
)
2271 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH1
;
2273 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH2
;
2275 kfree(*pathp
, MAXPATHLEN
);
2279 context
.vc_proc
= p
;
2280 context
.vc_ucred
= kauth_cred_get();
2283 VATTR_WANTED(&va
, va_mode
);
2284 VATTR_WANTED(&va
, va_uid
);
2285 VATTR_WANTED(&va
, va_gid
);
2286 VATTR_WANTED(&va
, va_rdev
);
2287 VATTR_WANTED(&va
, va_fsid
);
2288 VATTR_WANTED(&va
, va_fileid
);
2289 VATTR_WANTED(&va
, va_gen
);
2290 error
= vnode_getattr(vp
, &va
, &context
);
2292 /* XXX: How to handle this case? */
2296 /* XXX do we want to fall back here when these aren't supported? */
2297 vnp
->vn_mode
= va
.va_mode
;
2298 vnp
->vn_uid
= va
.va_uid
;
2299 vnp
->vn_gid
= va
.va_gid
;
2300 vnp
->vn_dev
= va
.va_rdev
;
2301 vnp
->vn_fsid
= va
.va_fsid
;
2302 vnp
->vn_fileid
= (u_long
)va
.va_fileid
;
2303 vnp
->vn_gen
= va
.va_gen
;
2304 if (flags
& ARG_VNODE1
)
2305 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE1
;
2307 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE2
;
2312 audit_arg_vnpath_withref(struct vnode
*vp
, u_int64_t flags
)
2314 if (vp
== NULL
|| vnode_getwithref(vp
))
2316 audit_arg_vnpath(vp
, flags
);
2317 (void)vnode_put(vp
);
2321 audit_arg_mach_port1(mach_port_name_t port
)
2323 struct kaudit_record
*ar
;
2329 ar
->k_ar
.ar_arg_mach_port1
= port
;
2330 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT1
;
2334 audit_arg_mach_port2(mach_port_name_t port
)
2336 struct kaudit_record
*ar
;
2342 ar
->k_ar
.ar_arg_mach_port2
= port
;
2343 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT2
;
2347 * The close() system call uses it's own audit call to capture the
2348 * path/vnode information because those pieces are not easily obtained
2349 * within the system call itself.
2352 audit_sysclose(struct proc
*p
, int fd
)
2354 struct fileproc
*fp
;
2359 if (fp_getfvp(p
, fd
, &fp
, &vp
) != 0)
2362 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2375 audit_shutdown(void)
2381 audit(struct proc
*p
, struct audit_args
*uap
, register_t
*retval
)
2387 auditon(struct proc
*p
, struct auditon_args
*uap
, register_t
*retval
)
2393 getauid(struct proc
*p
, struct getauid_args
*uap
, register_t
*retval
)
2399 setauid(struct proc
*p
, struct setauid_args
*uap
, register_t
*retval
)
2405 getaudit(struct proc
*p
, struct getaudit_args
*uap
, register_t
*retval
)
2411 setaudit(struct proc
*p
, struct setaudit_args
*uap
, register_t
*retval
)
2417 getaudit_addr(struct proc
*p
, struct getaudit_addr_args
*uap
, register_t
*retval
)
2423 setaudit_addr(struct proc
*p
, struct setaudit_addr_args
*uap
, register_t
*retval
)
2429 auditctl(struct proc
*p
, struct auditctl_args
*uap
, register_t
*retval
)