2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
25 #include <sys/fcntl.h>
26 #include <sys/kernel.h>
28 #include <sys/namei.h>
30 #include <sys/queue.h>
31 #include <sys/systm.h>
33 #include <sys/ucred.h>
35 #include <sys/unistd.h>
36 #include <sys/vnode.h>
38 #include <sys/syscall.h>
39 #include <sys/malloc.h>
41 #include <netinet/in.h>
42 #include <sys/socketvar.h>
43 #include <sys/protosw.h>
44 #include <sys/domain.h>
45 #include <sys/mount.h>
46 #include <net/route.h>
47 #include <netinet/in_pcb.h>
49 #include <bsm/audit.h>
50 #include <bsm/audit_kevents.h>
51 #include <bsm/audit_klib.h>
52 #include <bsm/audit_kernel.h>
54 #include <mach/message.h>
55 #include <mach/port.h>
56 #include <mach/host_special_ports.h>
57 #include <mach/audit_triggers.h>
59 #include <kern/lock.h>
60 #include <kern/wait_queue.h>
61 #include <kern/zalloc.h>
62 #include <kern/kalloc.h>
69 * The AUDIT_EXCESSIVELY_VERBOSE define enables a number of
70 * gratuitously noisy printf's to the console. Due to the
71 * volume, it should be left off unless you want your system
72 * to churn a lot whenever the audit record flow gets high.
74 /* #define AUDIT_EXCESSIVELY_VERBOSE */
75 #ifdef AUDIT_EXCESSIVELY_VERBOSE
76 #define AUDIT_PRINTF(x) printf x
78 #define AUDIT_PRINTF(X)
85 #define assert(cond) \
86 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
88 #include <kern/assert.h>
89 #endif /* DIAGNOSTIC */
92 * Define the audit control flags.
98 * Mutex to protect global variables shared between various threads and
101 static mutex_t
*audit_mtx
;
104 * Queue of audit records ready for delivery to disk. We insert new
105 * records at the tail, and remove records from the head. Also,
106 * a count of the number of records used for checking queue depth.
107 * In addition, a counter of records that we have allocated but are
108 * not yet in the queue, which is needed to estimate the total
109 * size of the combined set of records outstanding in the system.
111 static TAILQ_HEAD(, kaudit_record
) audit_q
;
112 static int audit_q_len
;
113 static int audit_pre_q_len
;
115 static wait_queue_t audit_wait_queue
;
116 static zone_t audit_zone
;
119 * Condition variable to signal to the worker that it has work to do:
120 * either new records are in the queue, or a log replacement is taking
123 static int audit_worker_event
;
124 #define AUDIT_WORKER_EVENT ((event_t)&audit_worker_event)
127 * When an audit log is rotated, the actual rotation must be performed
128 * by the audit worker thread, as it may have outstanding writes on the
129 * current audit log. audit_replacement_vp holds the vnode replacing
130 * the current vnode. We can't let more than one replacement occur
131 * at a time, so if more than one thread requests a replacement, only
132 * one can have the replacement "in progress" at any given moment. If
133 * a thread tries to replace the audit vnode and discovers a replacement
134 * is already in progress (i.e., audit_replacement_flag != 0), then it
135 * will sleep on audit_replacement_cv waiting its turn to perform a
136 * replacement. When a replacement is completed, this cv is signalled
137 * by the worker thread so a waiting thread can start another replacement.
138 * We also store a credential to perform audit log write operations with.
140 static int audit_replacement_event
;
141 #define AUDIT_REPLACEMENT_EVENT ((event_t)&audit_replacement_event)
143 static int audit_replacement_flag
;
144 static struct vnode
*audit_replacement_vp
;
145 static struct ucred
*audit_replacement_cred
;
148 * Wait queue for auditing threads that cannot commit the audit
149 * record at the present time. Also, the queue control parameter
152 static int audit_commit_event
;
153 #define AUDIT_COMMIT_EVENT ((event_t)&audit_commit_event)
155 static struct au_qctrl audit_qctrl
;
158 * Flags to use on audit files when opening and closing.
160 const static int audit_open_flags
= FWRITE
| O_APPEND
;
161 const static int audit_close_flags
= FWRITE
| O_APPEND
;
164 * Global audit statistiscs.
166 static struct audit_fstat audit_fstat
;
169 Preselection mask for non-attributable events.
171 static struct au_mask audit_nae_mask
;
174 * Flags related to Kernel->user-space communication.
176 static int audit_file_rotate_wait
;
179 * Flags controlling behavior in low storage situations.
180 * Should we panic if a write fails? Should we fail stop
181 * if we're out of disk space? Are we currently "failing
182 * stop" due to out of disk space?
184 static int audit_panic_on_write_fail
;
185 static int audit_fail_stop
;
186 static int audit_in_failure
;
189 * When in a fail-stop mode, threads will drop into this wait queue
190 * rather than perform auditable events. They won't ever get woken
193 static int audit_failure_event
;
194 #define AUDIT_FAILURE_EVENT ((event_t)&audit_failure_event)
197 * XXX: Couldn't find the include file for this, so copied kern_exec.c's
200 extern task_t kernel_task
;
203 audit_free(struct kaudit_record
*ar
)
205 if (ar
->k_ar
.ar_arg_upath1
!= NULL
) {
206 kfree((vm_offset_t
)ar
->k_ar
.ar_arg_upath1
, MAXPATHLEN
);
208 if (ar
->k_ar
.ar_arg_upath2
!= NULL
) {
209 kfree((vm_offset_t
)ar
->k_ar
.ar_arg_upath2
, MAXPATHLEN
);
211 if (ar
->k_ar
.ar_arg_kpath1
!= NULL
) {
212 kfree((vm_offset_t
)ar
->k_ar
.ar_arg_kpath1
, MAXPATHLEN
);
214 if (ar
->k_ar
.ar_arg_kpath2
!= NULL
) {
215 kfree((vm_offset_t
)ar
->k_ar
.ar_arg_kpath2
, MAXPATHLEN
);
217 if (ar
->k_ar
.ar_arg_text
!= NULL
) {
218 kfree((vm_offset_t
)ar
->k_ar
.ar_arg_text
, MAXPATHLEN
);
220 if (ar
->k_udata
!= NULL
) {
221 kfree((vm_offset_t
)ar
->k_udata
, (vm_size_t
)ar
->k_ulen
);
223 zfree(audit_zone
, (vm_offset_t
)ar
);
227 audit_write(struct vnode
*vp
, struct kaudit_record
*ar
, struct ucred
*cred
,
230 struct statfs
*mnt_stat
= &vp
->v_mount
->mnt_stat
;
232 struct au_record
*bsm
;
235 mach_port_t audit_port
;
238 * First, gather statistics on the audit log file and file system
239 * so that we know how we're doing on space. In both cases,
240 * if we're unable to perform the operation, we drop the record
241 * and return. However, this is arguably an assertion failure.
243 ret
= VFS_STATFS(vp
->v_mount
, mnt_stat
, p
);
247 ret
= VOP_GETATTR(vp
, &vattr
, cred
, p
);
251 /* update the global stats struct */
252 audit_fstat
.af_currsz
= vattr
.va_size
;
255 * Send a message to the audit daemon when disk space is getting
257 * XXX Need to decide what to do if the trigger to the audit daemon
260 if(host_get_audit_control_port(host_priv_self(), &audit_port
)
262 printf("Cannot get audit control port\n");
264 if (audit_port
!= MACH_PORT_NULL
) {
268 * If we fall below percent free blocks, then trigger the
269 * audit daemon to do something about it.
271 if (audit_qctrl
.aq_minfree
!= 0) {
272 temp
= mnt_stat
->f_blocks
/ (100 / audit_qctrl
.aq_minfree
);
273 if (mnt_stat
->f_bfree
< temp
) {
274 ret
= audit_triggers(audit_port
,
275 AUDIT_TRIGGER_LOW_SPACE
);
276 if (ret
!= KERN_SUCCESS
) {
278 "Failed audit_triggers(AUDIT_TRIGGER_LOW_SPACE): %d\n", ret
);
280 * XXX: What to do here? Disable auditing?
286 /* Check if the current log file is full; if so, call for
287 * a log rotate. This is not an exact comparison; we may
288 * write some records over the limit. If that's not
289 * acceptable, then add a fudge factor here.
291 if ((audit_fstat
.af_filesz
!= 0) &&
292 (audit_file_rotate_wait
== 0) &&
293 (vattr
.va_size
>= audit_fstat
.af_filesz
)) {
294 audit_file_rotate_wait
= 1;
295 ret
= audit_triggers(audit_port
,
296 AUDIT_TRIGGER_FILE_FULL
);
297 if (ret
!= KERN_SUCCESS
) {
299 "Failed audit_triggers(AUDIT_TRIGGER_FILE_FULL): %d\n", ret
);
300 /* XXX what to do here? */
306 * If the estimated amount of audit data in the audit event queue
307 * (plus records allocated but not yet queued) has reached the
308 * amount of free space on the disk, then we need to go into an
309 * audit fail stop state, in which we do not permit the
310 * allocation/committing of any new audit records. We continue to
311 * process packets but don't allow any activities that might
312 * generate new records. In the future, we might want to detect
313 * when space is available again and allow operation to continue,
314 * but this behavior is sufficient to meet fail stop requirements
317 if (audit_fail_stop
&&
319 ((audit_q_len
+ audit_pre_q_len
+ 1) * MAX_AUDIT_RECORD_SIZE
) /
320 mnt_stat
->f_bsize
>= (unsigned long)(mnt_stat
->f_bfree
)) {
322 "audit_worker: free space below size of audit queue, failing stop\n");
323 audit_in_failure
= 1;
327 * If there is a user audit record attached to the kernel record,
328 * then write the user record.
330 /* XXX Need to decide a few things here: IF the user audit
331 * record is written, but the write of the kernel record fails,
332 * what to do? Should the kernel record come before or after the
333 * user record? For now, we write the user record first, and
336 if (ar
->k_ar_commit
& AR_COMMIT_USER
) {
337 ret
= vn_rdwr(UIO_WRITE
, vp
, (void *)ar
->k_udata
, ar
->k_ulen
,
338 (off_t
)0, UIO_SYSSPACE
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
);
344 * Convert the internal kernel record to BSM format and write it
345 * out if everything's OK.
347 if (!(ar
->k_ar_commit
& AR_COMMIT_KERNEL
)) {
352 ret
= kaudit_to_bsm(ar
, &bsm
);
353 if (ret
== BSM_NOAUDIT
) {
359 * XXX: We drop the record on BSM conversion failure, but really
360 * this is an assertion failure.
362 if (ret
== BSM_FAILURE
) {
363 AUDIT_PRINTF(("BSM conversion failure\n"));
368 /* XXX This function can be called with the kernel funnel held,
369 * which is not optimal. We should break the write functionality
370 * away from the BSM record generation and have the BSM generation
371 * done before this function is called. This function will then
372 * take the BSM record as a parameter.
374 ret
= (vn_rdwr(UIO_WRITE
, vp
, (void *)bsm
->data
, bsm
->len
,
375 (off_t
)0, UIO_SYSSPACE
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
));
381 * When we're done processing the current record, we have to
382 * check to see if we're in a failure mode, and if so, whether
383 * this was the last record left to be drained. If we're done
384 * draining, then we fsync the vnode and panic.
386 if (audit_in_failure
&&
387 audit_q_len
== 0 && audit_pre_q_len
== 0) {
388 VOP_LOCK(vp
, LK_DRAIN
| LK_INTERLOCK
, p
);
389 (void)VOP_FSYNC(vp
, cred
, MNT_WAIT
, p
);
390 VOP_UNLOCK(vp
, 0, p
);
391 panic("Audit store overflow; record queue drained.");
400 int do_replacement_signal
, error
, release_funnel
;
401 TAILQ_HEAD(, kaudit_record
) ar_worklist
;
402 struct kaudit_record
*ar
, *ar_start
, *ar_stop
;
403 struct vnode
*audit_vp
, *old_vp
;
405 struct ucred
*audit_cred
, *old_cred
;
406 struct proc
*audit_p
;
408 AUDIT_PRINTF(("audit_worker starting\n"));
410 TAILQ_INIT(&ar_worklist
);
412 audit_p
= current_proc();
416 * XXX: Presumably we can assume Mach threads are started without
417 * holding the BSD kernel funnel?
419 thread_funnel_set(kernel_flock
, FALSE
);
421 mutex_lock(audit_mtx
);
424 * First priority: replace the audit log target if requested.
425 * As we actually close the vnode in the worker thread, we
426 * need to grab the funnel, which means releasing audit_mtx.
427 * In case another replacement was scheduled while the mutex
428 * we released, we loop.
430 * XXX It could well be we should drain existing records
431 * first to ensure that the timestamps and ordering
434 do_replacement_signal
= 0;
435 while (audit_replacement_flag
!= 0) {
436 old_cred
= audit_cred
;
438 audit_cred
= audit_replacement_cred
;
439 audit_vp
= audit_replacement_vp
;
440 audit_replacement_cred
= NULL
;
441 audit_replacement_vp
= NULL
;
442 audit_replacement_flag
= 0;
444 audit_enabled
= (audit_vp
!= NULL
);
446 if (old_vp
!= NULL
|| audit_vp
!= NULL
) {
447 mutex_unlock(audit_mtx
);
448 thread_funnel_set(kernel_flock
, TRUE
);
453 * XXX: What to do about write failures here?
455 if (old_vp
!= NULL
) {
456 AUDIT_PRINTF(("Closing old audit file\n"));
457 vn_close(old_vp
, audit_close_flags
, old_cred
,
462 AUDIT_PRINTF(("Audit file closed\n"));
464 if (audit_vp
!= NULL
) {
465 AUDIT_PRINTF(("Opening new audit file\n"));
467 if (release_funnel
) {
468 thread_funnel_set(kernel_flock
, FALSE
);
469 mutex_lock(audit_mtx
);
471 do_replacement_signal
= 1;
474 * Signal that replacement have occurred to wake up and
475 * start any other replacements started in parallel. We can
476 * continue about our business in the mean time. We
477 * broadcast so that both new replacements can be inserted,
478 * but also so that the source(s) of replacement can return
481 if (do_replacement_signal
)
482 wait_queue_wakeup_all(audit_wait_queue
,
483 AUDIT_REPLACEMENT_EVENT
, THREAD_AWAKENED
);
486 * Next, check to see if we have any records to drain into
487 * the vnode. If not, go back to waiting for an event.
489 if (TAILQ_EMPTY(&audit_q
)) {
492 AUDIT_PRINTF(("audit_worker waiting\n"));
493 ret
= wait_queue_assert_wait(audit_wait_queue
,
496 mutex_unlock(audit_mtx
);
498 assert(ret
== THREAD_WAITING
);
499 ret
= thread_block(THREAD_CONTINUE_NULL
);
500 assert(ret
== THREAD_AWAKENED
);
501 AUDIT_PRINTF(("audit_worker woken up\n"));
502 AUDIT_PRINTF(("audit_worker: new vp = %p; value of flag %d\n",
503 audit_replacement_vp
, audit_replacement_flag
));
504 mutex_lock(audit_mtx
);
509 * If we have records, but there's no active vnode to
510 * write to, drain the record queue. Generally, we
511 * prevent the unnecessary allocation of records
512 * elsewhere, but we need to allow for races between
513 * conditional allocation and queueing. Go back to
514 * waiting when we're done.
516 * XXX: We go out of our way to avoid calling audit_free()
517 * with the audit_mtx held, to avoid a lock order reversal
518 * as free() may grab the funnel. This will be fixed at
521 if (audit_vp
== NULL
) {
522 while ((ar
= TAILQ_FIRST(&audit_q
))) {
523 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
525 if (audit_q_len
<= audit_qctrl
.aq_lowater
)
526 wait_queue_wakeup_one(
531 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
533 mutex_unlock(audit_mtx
);
534 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
535 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
538 mutex_lock(audit_mtx
);
543 * We have both records to write, and an active vnode
544 * to write to. Dequeue a record, and start the write.
545 * Eventually, it might make sense to dequeue several
546 * records and perform our own clustering, if the lower
547 * layers aren't doing it automatically enough.
549 * XXX: We go out of our way to avoid calling audit_free()
550 * with the audit_mtx held, to avoid a lock order reversal
551 * as free() may grab the funnel. This will be fixed at
554 while ((ar
= TAILQ_FIRST(&audit_q
))) {
555 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
557 if (audit_q_len
<= audit_qctrl
.aq_lowater
) {
558 wait_queue_wakeup_one(audit_wait_queue
,
559 AUDIT_COMMIT_EVENT
, THREAD_AWAKENED
);
562 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
565 mutex_unlock(audit_mtx
);
567 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
568 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
569 if (audit_vp
!= NULL
) {
571 * XXX: What should happen if there's a write
574 if (!release_funnel
) {
575 thread_funnel_set(kernel_flock
, TRUE
);
578 VOP_LEASE(audit_vp
, audit_p
, audit_cred
,
580 error
= audit_write(audit_vp
, ar
, audit_cred
,
582 if (error
&& audit_panic_on_write_fail
)
583 panic("audit_worker: write error %d\n",
586 printf("audit_worker: write error %d\n",
592 thread_funnel_set(kernel_flock
, FALSE
);
593 mutex_lock(audit_mtx
);
601 /* Verify that the syscall to audit event table is the same
602 * size as the system call table.
604 if (nsys_au_event
!= nsysent
) {
605 printf("Security auditing service initialization failed, ");
606 printf("audit event table doesn't match syscall table.\n");
610 printf("Security auditing service present\n");
611 TAILQ_INIT(&audit_q
);
615 audit_replacement_cred
= NULL
;
616 audit_replacement_flag
= 0;
617 audit_file_rotate_wait
= 0;
618 audit_replacement_vp
= NULL
;
619 audit_fstat
.af_filesz
= 0; /* '0' means unset, unbounded */
620 audit_fstat
.af_currsz
= 0;
621 audit_qctrl
.aq_hiwater
= AQ_HIWATER
;
622 audit_qctrl
.aq_lowater
= AQ_LOWATER
;
623 audit_qctrl
.aq_bufsz
= AQ_BUFSZ
;
624 audit_qctrl
.aq_minfree
= AU_FS_MINFREE
;
626 audit_mtx
= mutex_alloc(ETAP_NO_TRACE
);
627 audit_wait_queue
= wait_queue_alloc(SYNC_POLICY_FIFO
);
628 audit_zone
= zinit(sizeof(struct kaudit_record
),
629 AQ_HIWATER
*sizeof(struct kaudit_record
),
633 /* Initialize the BSM audit subsystem. */
636 kernel_thread(kernel_task
, audit_worker
);
640 audit_rotate_vnode(struct ucred
*cred
, struct vnode
*vp
)
645 * If other parallel log replacements have been requested, we wait
646 * until they've finished before continuing.
648 mutex_lock(audit_mtx
);
649 while (audit_replacement_flag
!= 0) {
651 AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for "
653 ret
= wait_queue_assert_wait(audit_wait_queue
,
654 AUDIT_REPLACEMENT_EVENT
,
656 mutex_unlock(audit_mtx
);
658 assert(ret
== THREAD_WAITING
);
659 ret
= thread_block(THREAD_CONTINUE_NULL
);
660 assert(ret
== THREAD_AWAKENED
);
661 AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n",
662 audit_replacement_flag
));
664 mutex_lock(audit_mtx
);
666 audit_replacement_cred
= cred
;
667 audit_replacement_flag
= 1;
668 audit_replacement_vp
= vp
;
671 * Wake up the audit worker to perform the exchange once we
674 wait_queue_wakeup_one(audit_wait_queue
, AUDIT_WORKER_EVENT
, THREAD_AWAKENED
);
677 * Wait for the audit_worker to broadcast that a replacement has
678 * taken place; we know that once this has happened, our vnode
679 * has been replaced in, so we can return successfully.
681 AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of "
683 ret
= wait_queue_assert_wait(audit_wait_queue
,
684 AUDIT_REPLACEMENT_EVENT
,
686 mutex_unlock(audit_mtx
);
688 assert(ret
== THREAD_WAITING
);
689 ret
= thread_block(THREAD_CONTINUE_NULL
);
690 assert(ret
== THREAD_AWAKENED
);
691 AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by "
692 "audit_worker (flag " "now %d)\n", audit_replacement_flag
));
694 audit_file_rotate_wait
= 0; /* We can now request another rotation */
698 * Drain the audit queue and close the log at shutdown.
703 audit_rotate_vnode(NULL
, NULL
);
706 static __inline__
struct uthread
*
709 return (get_bsdthread_info(current_act()));
712 static __inline__
struct kaudit_record
*
715 return (curuthread()->uu_ar
);
718 /**********************************
719 * Begin system calls. *
720 **********************************/
722 * System call to allow a user space application to submit a BSM audit
723 * record to the kernel for inclusion in the audit log. This function
724 * does little verification on the audit record that is submitted.
726 * XXXAUDIT: Audit preselection for user records does not currently
727 * work, since we pre-select only based on the AUE_audit event type,
728 * not the event type submitted as part of the user audit data.
736 audit(struct proc
*p
, struct audit_args
*uap
, register_t
*retval
)
738 register struct pcred
*pc
= p
->p_cred
;
741 struct kaudit_record
*ar
;
742 struct uthread
*uthr
;
744 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
748 if ((uap
->length
<= 0) || (uap
->length
> audit_qctrl
.aq_bufsz
))
753 /* If there's no current audit record (audit() itself not audited)
754 * commit the user audit record.
758 if (uthr
== NULL
) /* can this happen? */
761 /* This is not very efficient; we're required to allocate
762 * a complete kernel audit record just so the user record
765 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
766 if (uthr
->uu_ar
== NULL
) /* auditing not on, or memory error */
771 if (uap
->length
> MAX_AUDIT_RECORD_SIZE
)
774 rec
= (void *)kalloc((vm_size_t
)uap
->length
);
776 error
= copyin(uap
->record
, rec
, uap
->length
);
780 /* Verify the record */
781 if (bsm_rec_verify(rec
) == 0) {
786 /* Attach the user audit record to the kernel audit record. Because
787 * this system call is an auditable event, we will write the user
788 * record along with the record for this audit event.
791 ar
->k_ar_commit
|= AR_COMMIT_USER
;
792 ar
->k_ulen
= uap
->length
;
796 /* audit_syscall_exit() will free the audit record on the thread
797 * even if we allocated it above.
799 kfree((vm_offset_t
)rec
, (vm_size_t
)uap
->length
);
804 * System call to manipulate auditing.
806 struct auditon_args
{
813 auditon(struct proc
*p
, struct auditon_args
*uap
, register_t
*retval
)
815 register struct pcred
*pc
= p
->p_cred
;
818 union auditon_udata udata
;
821 AUDIT_ARG(cmd
, uap
->cmd
);
822 ret
= suser(pc
->pc_ucred
, &p
->p_acflag
);
827 if ((len
<= 0) || (len
> sizeof(union auditon_udata
)))
830 memset((void *)&udata
, 0, sizeof(udata
));
833 /* Some of the GET commands use the arguments too */
847 case A_GETPINFO_ADDR
:
848 ret
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
851 AUDIT_ARG(auditon
, &udata
);
855 /* XXX Need to implement these commands by accessing the global
856 * values associated with the commands.
860 if (!audit_fail_stop
)
861 udata
.au_policy
|= AUDIT_CNT
;
862 if (audit_panic_on_write_fail
)
863 udata
.au_policy
|= AUDIT_AHLT
;
866 if (udata
.au_policy
& ~(AUDIT_CNT
|AUDIT_AHLT
))
869 * XXX - Need to wake up waiters if the policy relaxes?
871 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
872 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
875 udata
.au_mask
= audit_nae_mask
;
878 audit_nae_mask
= udata
.au_mask
;
881 udata
.au_qctrl
= audit_qctrl
;
884 if ((udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
885 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
886 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
887 (udata
.au_qctrl
.aq_minfree
< 0) ||
888 (udata
.au_qctrl
.aq_minfree
> 100))
891 audit_qctrl
= udata
.au_qctrl
;
892 /* XXX The queue delay value isn't used with the kernel. */
893 audit_qctrl
.aq_delay
= -1;
914 if (audit_enabled
&& !audit_suspended
)
915 udata
.au_cond
= AUC_AUDITING
;
917 udata
.au_cond
= AUC_NOAUDIT
;
920 if (udata
.au_cond
== AUC_NOAUDIT
)
922 if (udata
.au_cond
== AUC_AUDITING
)
924 if (udata
.au_cond
== AUC_DISABLED
) {
930 udata
.au_evclass
.ec_class
=
931 au_event_class(udata
.au_evclass
.ec_number
);
934 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
935 udata
.au_evclass
.ec_class
);
938 if (udata
.au_aupinfo
.ap_pid
< 1)
940 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
943 udata
.au_aupinfo
.ap_auid
= tp
->p_au
->ai_auid
;
944 udata
.au_aupinfo
.ap_mask
.am_success
=
945 tp
->p_au
->ai_mask
.am_success
;
946 udata
.au_aupinfo
.ap_mask
.am_failure
=
947 tp
->p_au
->ai_mask
.am_failure
;
948 udata
.au_aupinfo
.ap_termid
.machine
=
949 tp
->p_au
->ai_termid
.machine
;
950 udata
.au_aupinfo
.ap_termid
.port
=
951 tp
->p_au
->ai_termid
.port
;
952 udata
.au_aupinfo
.ap_asid
= tp
->p_au
->ai_asid
;
955 if (udata
.au_aupinfo
.ap_pid
< 1)
957 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
960 tp
->p_au
->ai_mask
.am_success
=
961 udata
.au_aupinfo
.ap_mask
.am_success
;
962 tp
->p_au
->ai_mask
.am_failure
=
963 udata
.au_aupinfo
.ap_mask
.am_failure
;
966 if ((udata
.au_fstat
.af_filesz
!= 0) &&
967 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
))
969 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
972 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
973 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
975 case A_GETPINFO_ADDR
:
985 /* Copy data back to userspace for the GET comands */
997 case A_GETPINFO_ADDR
:
999 ret
= copyout((void *)&udata
, uap
->data
, uap
->length
);
1009 * System calls to manage the user audit information.
1010 * XXXAUDIT May need to lock the proc structure.
1012 struct getauid_args
{
1017 getauid(struct proc
*p
, struct getauid_args
*uap
, register_t
*retval
)
1019 register struct pcred
*pc
= p
->p_cred
;
1022 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
1026 error
= copyout((void *)&p
->p_au
->ai_auid
, (void *)uap
->auid
,
1027 sizeof(*uap
->auid
));
1034 struct setauid_args
{
1039 setauid(struct proc
*p
, struct setauid_args
*uap
, register_t
*retval
)
1041 register struct pcred
*pc
= p
->p_cred
;
1044 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
1048 error
= copyin((void *)uap
->auid
, (void *)&p
->p_au
->ai_auid
,
1049 sizeof(p
->p_au
->ai_auid
));
1053 /* propagate the change from the process to Mach task */
1054 set_security_token(p
);
1056 audit_arg_auid(p
->p_au
->ai_auid
);
1061 * System calls to get and set process audit information.
1063 struct getaudit_args
{
1064 struct auditinfo
*auditinfo
;
1068 getaudit(struct proc
*p
, struct getaudit_args
*uap
, register_t
*retval
)
1070 register struct pcred
*pc
= p
->p_cred
;
1073 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
1076 error
= copyout((void *)p
->p_au
, (void *)uap
->auditinfo
,
1077 sizeof(*uap
->auditinfo
));
1084 struct setaudit_args
{
1085 struct auditinfo
*auditinfo
;
1089 setaudit(struct proc
*p
, struct setaudit_args
*uap
, register_t
*retval
)
1091 register struct pcred
*pc
= p
->p_cred
;
1094 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
1097 error
= copyin((void *)uap
->auditinfo
, (void *)p
->p_au
,
1102 /* propagate the change from the process to Mach task */
1103 set_security_token(p
);
1105 audit_arg_auditinfo(p
->p_au
);
1110 struct getaudit_addr_args
{
1111 struct auditinfo_addr
*auditinfo_addr
;
1116 getaudit_addr(struct proc
*p
, struct getaudit_addr_args
*uap
, register_t
*retval
)
1118 register struct pcred
*pc
= p
->p_cred
;
1121 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
1127 struct setaudit_addr_args
{
1128 struct auditinfo_addr
*auditinfo_addr
;
1133 setaudit_addr(struct proc
*p
, struct setaudit_addr_args
*uap
, register_t
*retval
)
1135 register struct pcred
*pc
= p
->p_cred
;
1138 error
= suser(pc
->pc_ucred
, &p
->p_acflag
);
1145 * Syscall to manage audit files.
1148 struct auditctl_args
{
1153 auditctl(struct proc
*p
, struct auditctl_args
*uap
)
1155 struct kaudit_record
*ar
;
1156 struct nameidata nd
;
1159 int error
, flags
, ret
;
1161 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1169 * If a path is specified, open the replacement vnode, perform
1170 * validity checks, and grab another reference to the current
1173 if (uap
->path
!= NULL
) {
1174 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1175 UIO_USERSPACE
, uap
->path
, p
);
1176 flags
= audit_open_flags
;
1177 error
= vn_open(&nd
, flags
, 0);
1180 VOP_UNLOCK(nd
.ni_vp
, 0, p
);
1182 if (vp
->v_type
!= VREG
) {
1183 vn_close(vp
, audit_close_flags
, p
->p_ucred
, p
);
1189 audit_suspended
= 0;
1192 audit_rotate_vnode(cred
, vp
);
1197 /**********************************
1198 * End of system calls. *
1199 **********************************/
1204 struct kaudit_record
*
1205 audit_new(int event
, struct proc
*p
, struct uthread
*uthread
)
1207 struct kaudit_record
*ar
;
1211 * Eventually, there may be certain classes of events that
1212 * we will audit regardless of the audit state at the time
1213 * the record is created. These events will generally
1214 * correspond to changes in the audit state. The dummy
1215 * code below is from our first prototype, but may also
1216 * be used in the final version (with modified event numbers).
1219 if (event
!= AUDIT_EVENT_FILESTOP
&& event
!= AUDIT_EVENT_FILESTART
) {
1221 mutex_lock(audit_mtx
);
1222 no_record
= (audit_suspended
|| !audit_enabled
);
1223 mutex_unlock(audit_mtx
);
1231 * Initialize the audit record header.
1232 * XXX: We may want to fail-stop if allocation fails.
1233 * XXX: The number of outstanding uncommitted audit records is
1234 * limited by the number of concurrent threads servicing system
1235 * calls in the kernel.
1238 ar
= (struct kaudit_record
*)zalloc(audit_zone
);
1242 mutex_lock(audit_mtx
);
1244 mutex_unlock(audit_mtx
);
1246 bzero(ar
, sizeof(*ar
));
1247 ar
->k_ar
.ar_magic
= AUDIT_RECORD_MAGIC
;
1248 ar
->k_ar
.ar_event
= event
;
1249 nanotime(&ar
->k_ar
.ar_starttime
);
1251 /* Export the subject credential. */
1252 cru2x(p
->p_ucred
, &ar
->k_ar
.ar_subj_cred
);
1253 ar
->k_ar
.ar_subj_ruid
= p
->p_cred
->p_ruid
;
1254 ar
->k_ar
.ar_subj_rgid
= p
->p_cred
->p_rgid
;
1255 ar
->k_ar
.ar_subj_egid
= p
->p_ucred
->cr_groups
[0];
1256 ar
->k_ar
.ar_subj_auid
= p
->p_au
->ai_auid
;
1257 ar
->k_ar
.ar_subj_asid
= p
->p_au
->ai_asid
;
1258 ar
->k_ar
.ar_subj_pid
= p
->p_pid
;
1259 ar
->k_ar
.ar_subj_amask
= p
->p_au
->ai_mask
;
1260 ar
->k_ar
.ar_subj_term
= p
->p_au
->ai_termid
;
1261 bcopy(p
->p_comm
, ar
->k_ar
.ar_subj_comm
, MAXCOMLEN
);
1268 * XXXAUDIT: So far, this is unused, and should probably be GC'd.
1271 audit_abort(struct kaudit_record
*ar
)
1274 mutex_lock(audit_mtx
);
1276 mutex_unlock(audit_mtx
);
1284 audit_commit(struct kaudit_record
*ar
, int error
, int retval
)
1288 struct au_mask
*aumask
;
1294 * Decide whether to commit the audit record by checking the
1295 * error value from the system call and using the appropriate
1298 if (ar
->k_ar
.ar_subj_auid
== AU_DEFAUDITID
)
1299 aumask
= &audit_nae_mask
;
1301 aumask
= &ar
->k_ar
.ar_subj_amask
;
1304 sorf
= AU_PRS_FAILURE
;
1306 sorf
= AU_PRS_SUCCESS
;
1308 switch(ar
->k_ar
.ar_event
) {
1311 /* The open syscall always writes a OPEN_RWTC event; limit the
1312 * to the proper type of event based on the flags and the error
1315 ar
->k_ar
.ar_event
= flags_and_error_to_openevent(ar
->k_ar
.ar_arg_fflags
, error
);
1319 ar
->k_ar
.ar_event
= ctlname_to_sysctlevent(ar
->k_ar
.ar_arg_ctlname
, ar
->k_ar
.ar_valid_arg
);
1323 /* Convert the auditon() command to an event */
1324 ar
->k_ar
.ar_event
= auditon_command_event(ar
->k_ar
.ar_arg_cmd
);
1328 if (au_preselect(ar
->k_ar
.ar_event
, aumask
, sorf
) != 0)
1329 ar
->k_ar_commit
|= AR_COMMIT_KERNEL
;
1331 if (ar
->k_ar_commit
& (AR_COMMIT_USER
| AR_COMMIT_KERNEL
) == 0) {
1332 mutex_lock(audit_mtx
);
1334 mutex_unlock(audit_mtx
);
1339 ar
->k_ar
.ar_errno
= error
;
1340 ar
->k_ar
.ar_retval
= retval
;
1343 * We might want to do some system-wide post-filtering
1344 * here at some point.
1348 * Timestamp system call end.
1350 nanotime(&ar
->k_ar
.ar_endtime
);
1352 mutex_lock(audit_mtx
);
1355 * Note: it could be that some records initiated while audit was
1356 * enabled should still be committed?
1358 if (audit_suspended
|| !audit_enabled
) {
1360 mutex_unlock(audit_mtx
);
1366 * Constrain the number of committed audit records based on
1367 * the configurable parameter.
1369 while (audit_q_len
>= audit_qctrl
.aq_hiwater
) {
1371 ret
= wait_queue_assert_wait(audit_wait_queue
,
1374 mutex_unlock(audit_mtx
);
1376 assert(ret
== THREAD_WAITING
);
1378 ret
= thread_block(THREAD_CONTINUE_NULL
);
1379 assert(ret
== THREAD_AWAKENED
);
1380 mutex_lock(audit_mtx
);
1383 TAILQ_INSERT_TAIL(&audit_q
, ar
, k_q
);
1386 wait_queue_wakeup_one(audit_wait_queue
, AUDIT_WORKER_EVENT
, THREAD_AWAKENED
);
1387 mutex_unlock(audit_mtx
);
1391 * Calls to set up and tear down audit structures associated with
1395 audit_syscall_enter(unsigned short code
, struct proc
*proc
,
1396 struct uthread
*uthread
)
1399 struct au_mask
*aumask
;
1401 audit_event
= sys_au_event
[code
];
1402 if (audit_event
== AUE_NULL
)
1405 assert(uthread
->uu_ar
== NULL
);
1407 /* Check which audit mask to use; either the kernel non-attributable
1408 * event mask or the process audit mask.
1410 if (proc
->p_au
->ai_auid
== AU_DEFAUDITID
)
1411 aumask
= &audit_nae_mask
;
1413 aumask
= &proc
->p_au
->ai_mask
;
1416 * Allocate an audit record, if preselection allows it, and store
1417 * in the BSD thread for later use.
1419 if (au_preselect(audit_event
, aumask
,
1420 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1422 * If we're out of space and need to suspend unprivileged
1423 * processes, do that here rather than trying to allocate
1424 * another audit record.
1426 if (audit_in_failure
&&
1427 suser(proc
->p_ucred
, &proc
->p_acflag
) != 0) {
1430 ret
= wait_queue_assert_wait(audit_wait_queue
,
1431 AUDIT_FAILURE_EVENT
, THREAD_UNINT
);
1432 assert(ret
== THREAD_WAITING
);
1433 (void)thread_block(THREAD_CONTINUE_NULL
);
1434 panic("audit_failing_stop: thread continued");
1436 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1438 uthread
->uu_ar
= NULL
;
1442 audit_syscall_exit(int error
, struct proc
*proc
, struct uthread
*uthread
)
1447 * Commit the audit record as desired; once we pass the record
1448 * into audit_commit(), the memory is owned by the audit
1450 * The return value from the system call is stored on the user
1451 * thread. If there was an error, the return value is set to -1,
1452 * imitating the behavior of the cerror routine.
1457 retval
= uthread
->uu_rval
[0];
1459 audit_commit(uthread
->uu_ar
, error
, retval
);
1460 if (uthread
->uu_ar
!= NULL
)
1461 AUDIT_PRINTF(("audit record committed by pid %d\n", proc
->p_pid
));
1462 uthread
->uu_ar
= NULL
;
1467 * Calls to set up and tear down audit structures used during Mach
1471 audit_mach_syscall_enter(unsigned short audit_event
)
1473 struct uthread
*uthread
;
1475 struct au_mask
*aumask
;
1477 if (audit_event
== AUE_NULL
)
1480 uthread
= curuthread();
1481 if (uthread
== NULL
)
1484 proc
= current_proc();
1488 assert(uthread
->uu_ar
== NULL
);
1490 /* Check which audit mask to use; either the kernel non-attributable
1491 * event mask or the process audit mask.
1493 if (proc
->p_au
->ai_auid
== AU_DEFAUDITID
)
1494 aumask
= &audit_nae_mask
;
1496 aumask
= &proc
->p_au
->ai_mask
;
1499 * Allocate an audit record, if desired, and store in the BSD
1500 * thread for later use.
1502 if (au_preselect(audit_event
, aumask
,
1503 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1504 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1506 uthread
->uu_ar
= NULL
;
1511 audit_mach_syscall_exit(int retval
, struct uthread
*uthread
)
1513 /* The error code from Mach system calls is the same as the
1516 /* XXX Is the above statement always true? */
1517 audit_commit(uthread
->uu_ar
, retval
, retval
);
1518 uthread
->uu_ar
= NULL
;
1523 * Calls to manipulate elements of the audit record structure from system
1524 * call code. Macro wrappers will prevent this functions from being
1525 * entered if auditing is disabled, avoiding the function call cost. We
1526 * check the thread audit record pointer anyway, as the audit condition
1527 * could change, and pre-selection may not have allocated an audit
1528 * record for this event.
1531 audit_arg_addr(void * addr
)
1533 struct kaudit_record
*ar
;
1539 ar
->k_ar
.ar_arg_addr
= addr
;
1540 ar
->k_ar
.ar_valid_arg
|= ARG_ADDR
;
1544 audit_arg_len(int len
)
1546 struct kaudit_record
*ar
;
1552 ar
->k_ar
.ar_arg_len
= len
;
1553 ar
->k_ar
.ar_valid_arg
|= ARG_LEN
;
1557 audit_arg_fd(int fd
)
1559 struct kaudit_record
*ar
;
1565 ar
->k_ar
.ar_arg_fd
= fd
;
1566 ar
->k_ar
.ar_valid_arg
|= ARG_FD
;
1570 audit_arg_fflags(int fflags
)
1572 struct kaudit_record
*ar
;
1578 ar
->k_ar
.ar_arg_fflags
= fflags
;
1579 ar
->k_ar
.ar_valid_arg
|= ARG_FFLAGS
;
1583 audit_arg_gid(gid_t gid
, gid_t egid
, gid_t rgid
, gid_t sgid
)
1585 struct kaudit_record
*ar
;
1591 ar
->k_ar
.ar_arg_gid
= gid
;
1592 ar
->k_ar
.ar_arg_egid
= egid
;
1593 ar
->k_ar
.ar_arg_rgid
= rgid
;
1594 ar
->k_ar
.ar_arg_sgid
= sgid
;
1595 ar
->k_ar
.ar_valid_arg
|= (ARG_GID
| ARG_EGID
| ARG_RGID
| ARG_SGID
);
1599 audit_arg_uid(uid_t uid
, uid_t euid
, uid_t ruid
, uid_t suid
)
1601 struct kaudit_record
*ar
;
1607 ar
->k_ar
.ar_arg_uid
= uid
;
1608 ar
->k_ar
.ar_arg_euid
= euid
;
1609 ar
->k_ar
.ar_arg_ruid
= ruid
;
1610 ar
->k_ar
.ar_arg_suid
= suid
;
1611 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_EUID
| ARG_RUID
| ARG_SUID
);
1615 audit_arg_groupset(gid_t
*gidset
, u_int gidset_size
)
1618 struct kaudit_record
*ar
;
1624 for (i
= 0; i
< gidset_size
; i
++)
1625 ar
->k_ar
.ar_arg_groups
.gidset
[i
] = gidset
[i
];
1626 ar
->k_ar
.ar_arg_groups
.gidset_size
= gidset_size
;
1627 ar
->k_ar
.ar_valid_arg
|= ARG_GROUPSET
;
1631 audit_arg_login(char *login
)
1633 struct kaudit_record
*ar
;
1641 * XXX: Add strlcpy() to Darwin for improved safety.
1643 strlcpy(ar
->k_ar
.ar_arg_login
, login
, MAXLOGNAME
);
1645 strcpy(ar
->k_ar
.ar_arg_login
, login
);
1648 ar
->k_ar
.ar_valid_arg
|= ARG_LOGIN
;
1652 audit_arg_ctlname(int *name
, int namelen
)
1654 struct kaudit_record
*ar
;
1660 bcopy(name
, &ar
->k_ar
.ar_arg_ctlname
, namelen
* sizeof(int));
1661 ar
->k_ar
.ar_arg_len
= namelen
;
1662 ar
->k_ar
.ar_valid_arg
|= (ARG_CTLNAME
| ARG_LEN
);
1666 audit_arg_mask(int mask
)
1668 struct kaudit_record
*ar
;
1674 ar
->k_ar
.ar_arg_mask
= mask
;
1675 ar
->k_ar
.ar_valid_arg
|= ARG_MASK
;
1679 audit_arg_mode(mode_t mode
)
1681 struct kaudit_record
*ar
;
1687 ar
->k_ar
.ar_arg_mode
= mode
;
1688 ar
->k_ar
.ar_valid_arg
|= ARG_MODE
;
1692 audit_arg_dev(int dev
)
1694 struct kaudit_record
*ar
;
1700 ar
->k_ar
.ar_arg_dev
= dev
;
1701 ar
->k_ar
.ar_valid_arg
|= ARG_DEV
;
1705 audit_arg_value(long value
)
1707 struct kaudit_record
*ar
;
1713 ar
->k_ar
.ar_arg_value
= value
;
1714 ar
->k_ar
.ar_valid_arg
|= ARG_VALUE
;
1718 audit_arg_owner(uid_t uid
, gid_t gid
)
1720 struct kaudit_record
*ar
;
1726 ar
->k_ar
.ar_arg_uid
= uid
;
1727 ar
->k_ar
.ar_arg_gid
= gid
;
1728 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_GID
);
1732 audit_arg_pid(pid_t pid
)
1734 struct kaudit_record
*ar
;
1741 ar
->k_ar
.ar_arg_pid
= pid
;
1742 ar
->k_ar
.ar_valid_arg
|= ARG_PID
;
1747 audit_arg_process(struct proc
*p
)
1749 struct kaudit_record
*ar
;
1752 if ((ar
== NULL
) || (p
== NULL
))
1755 /* XXX May need to lock the credentials structures */
1756 ar
->k_ar
.ar_arg_auid
= p
->p_au
->ai_auid
;
1757 ar
->k_ar
.ar_arg_euid
= p
->p_ucred
->cr_uid
;
1758 ar
->k_ar
.ar_arg_egid
= p
->p_ucred
->cr_groups
[0];
1759 ar
->k_ar
.ar_arg_ruid
= p
->p_cred
->p_ruid
;
1760 ar
->k_ar
.ar_arg_rgid
= p
->p_cred
->p_rgid
;
1761 ar
->k_ar
.ar_arg_asid
= p
->p_au
->ai_asid
;
1763 ar
->k_ar
.ar_arg_termid
= p
->p_au
->ai_termid
;
1765 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_EUID
| ARG_EGID
| ARG_RUID
|
1766 ARG_RGID
| ARG_ASID
| ARG_TERMID
| ARG_PROCESS
;
1770 audit_arg_signum(u_int signum
)
1772 struct kaudit_record
*ar
;
1778 ar
->k_ar
.ar_arg_signum
= signum
;
1779 ar
->k_ar
.ar_valid_arg
|= ARG_SIGNUM
;
1783 audit_arg_socket(int sodomain
, int sotype
, int soprotocol
)
1786 struct kaudit_record
*ar
;
1792 ar
->k_ar
.ar_arg_sockinfo
.so_domain
= sodomain
;
1793 ar
->k_ar
.ar_arg_sockinfo
.so_type
= sotype
;
1794 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
= soprotocol
;
1795 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
1799 audit_arg_sockaddr(struct proc
*p
, struct sockaddr
*so
)
1801 struct kaudit_record
*ar
;
1804 if (ar
== NULL
|| p
== NULL
|| so
== NULL
)
1807 bcopy(so
, &ar
->k_ar
.ar_arg_sockaddr
, sizeof(ar
->k_ar
.ar_arg_sockaddr
));
1808 switch (so
->sa_family
) {
1810 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET
;
1813 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET6
;
1816 audit_arg_upath(p
, ((struct sockaddr_un
*)so
)->sun_path
,
1818 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRUNIX
;
1824 audit_arg_auid(uid_t auid
)
1826 struct kaudit_record
*ar
;
1832 ar
->k_ar
.ar_arg_auid
= auid
;
1833 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
;
1837 audit_arg_auditinfo(struct auditinfo
*au_info
)
1839 struct kaudit_record
*ar
;
1845 ar
->k_ar
.ar_arg_auid
= au_info
->ai_auid
;
1846 ar
->k_ar
.ar_arg_asid
= au_info
->ai_asid
;
1847 ar
->k_ar
.ar_arg_amask
.am_success
= au_info
->ai_mask
.am_success
;
1848 ar
->k_ar
.ar_arg_amask
.am_failure
= au_info
->ai_mask
.am_failure
;
1849 ar
->k_ar
.ar_arg_termid
.port
= au_info
->ai_termid
.port
;
1850 ar
->k_ar
.ar_arg_termid
.machine
= au_info
->ai_termid
.machine
;
1851 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_ASID
| ARG_AMASK
| ARG_TERMID
;
1855 audit_arg_text(char *text
)
1857 struct kaudit_record
*ar
;
1863 /* Invalidate the text string */
1864 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_TEXT
);
1868 if (ar
->k_ar
.ar_arg_text
== NULL
) {
1869 ar
->k_ar
.ar_arg_text
= (char *)kalloc(MAXPATHLEN
);
1870 if (ar
->k_ar
.ar_arg_text
== NULL
)
1874 strncpy(ar
->k_ar
.ar_arg_text
, text
, MAXPATHLEN
);
1875 ar
->k_ar
.ar_valid_arg
|= ARG_TEXT
;
1879 audit_arg_cmd(int cmd
)
1881 struct kaudit_record
*ar
;
1887 ar
->k_ar
.ar_arg_cmd
= cmd
;
1888 ar
->k_ar
.ar_valid_arg
|= ARG_CMD
;
1892 audit_arg_svipc_cmd(int cmd
)
1894 struct kaudit_record
*ar
;
1900 ar
->k_ar
.ar_arg_svipc_cmd
= cmd
;
1901 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_CMD
;
1905 audit_arg_svipc_perm(struct ipc_perm
*perm
)
1907 struct kaudit_record
*ar
;
1913 bcopy(perm
, &ar
->k_ar
.ar_arg_svipc_perm
,
1914 sizeof(ar
->k_ar
.ar_arg_svipc_perm
));
1915 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_PERM
;
1919 audit_arg_svipc_id(int id
)
1921 struct kaudit_record
*ar
;
1927 ar
->k_ar
.ar_arg_svipc_id
= id
;
1928 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ID
;
1932 audit_arg_svipc_addr(void * addr
)
1934 struct kaudit_record
*ar
;
1940 ar
->k_ar
.ar_arg_svipc_addr
= addr
;
1941 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ADDR
;
1945 audit_arg_posix_ipc_perm(uid_t uid
, gid_t gid
, mode_t mode
)
1947 struct kaudit_record
*ar
;
1953 ar
->k_ar
.ar_arg_pipc_perm
.pipc_uid
= uid
;
1954 ar
->k_ar
.ar_arg_pipc_perm
.pipc_gid
= gid
;
1955 ar
->k_ar
.ar_arg_pipc_perm
.pipc_mode
= mode
;
1956 ar
->k_ar
.ar_valid_arg
|= ARG_POSIX_IPC_PERM
;
1960 audit_arg_auditon(union auditon_udata
*udata
)
1962 struct kaudit_record
*ar
;
1968 bcopy((void *)udata
, &ar
->k_ar
.ar_arg_auditon
,
1969 sizeof(ar
->k_ar
.ar_arg_auditon
));
1970 ar
->k_ar
.ar_valid_arg
|= ARG_AUDITON
;
1974 * Audit information about a file, either the file's vnode info, or its
1975 * socket address info.
1978 audit_arg_file(struct proc
*p
, struct file
*fp
)
1980 struct kaudit_record
*ar
;
1984 if (fp
->f_type
== DTYPE_VNODE
) {
1985 audit_arg_vnpath((struct vnode
*)fp
->f_data
, ARG_VNODE1
);
1989 if (fp
->f_type
== DTYPE_SOCKET
) {
1993 so
= (struct socket
*)fp
->f_data
;
1994 if (INP_CHECK_SOCKAF(so
, PF_INET
)) {
1995 if (so
->so_pcb
== NULL
)
1997 ar
->k_ar
.ar_arg_sockinfo
.so_type
=
1999 ar
->k_ar
.ar_arg_sockinfo
.so_domain
=
2001 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
=
2002 so
->so_proto
->pr_protocol
;
2003 pcb
= (struct inpcb
*)so
->so_pcb
;
2004 ar
->k_ar
.ar_arg_sockinfo
.so_raddr
=
2005 pcb
->inp_faddr
.s_addr
;
2006 ar
->k_ar
.ar_arg_sockinfo
.so_laddr
=
2007 pcb
->inp_laddr
.s_addr
;
2008 ar
->k_ar
.ar_arg_sockinfo
.so_rport
=
2010 ar
->k_ar
.ar_arg_sockinfo
.so_lport
=
2012 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
2019 * Initialize the audit information for the a process, presumably the first
2020 * process in the system.
2021 * XXX It is not clear what the initial values should be for session ID,
2025 audit_proc_init(struct proc
*p
)
2027 MALLOC_ZONE(p
->p_au
, struct auditinfo
*, sizeof(*p
->p_au
),
2028 M_SUBPROC
, M_WAITOK
);
2030 bzero((void *)p
->p_au
, sizeof(*p
->p_au
));
2032 p
->p_au
->ai_auid
= AU_DEFAUDITID
;
2036 * Copy the audit info from the parent process to the child process when
2037 * a fork takes place.
2038 * XXX Need to check for failure from the memory allocation, in here
2039 * as well as in any functions that use the process auditing info.
2042 audit_proc_fork(struct proc
*parent
, struct proc
*child
)
2044 /* Always set up the audit information pointer as this function
2045 * should only be called when the proc is new. If proc structures
2046 * are ever cached and reused, then this behavior will leak memory.
2048 MALLOC_ZONE(child
->p_au
, struct auditinfo
*, sizeof(*child
->p_au
),
2049 M_SUBPROC
, M_WAITOK
);
2051 bcopy(parent
->p_au
, child
->p_au
, sizeof(*child
->p_au
));
2055 * Free the auditing structure for the process.
2058 audit_proc_free(struct proc
*p
)
2060 FREE_ZONE((void *)p
->p_au
, sizeof(*p
->p_au
), M_SUBPROC
);
2065 * Store a path as given by the user process for auditing into the audit
2066 * record stored on the user thread. This function will allocate the memory to
2067 * store the path info if not already available. This memory will be
2068 * freed when the audit record is freed.
2071 audit_arg_upath(struct proc
*p
, char *upath
, u_int64_t flags
)
2073 struct kaudit_record
*ar
;
2076 if (p
== NULL
|| upath
== NULL
)
2077 return; /* nothing to do! */
2079 if (flags
& (ARG_UPATH1
| ARG_UPATH2
) == 0)
2083 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2086 if (flags
& ARG_UPATH1
) {
2087 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH1
);
2088 pathp
= &ar
->k_ar
.ar_arg_upath1
;
2091 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH2
);
2092 pathp
= &ar
->k_ar
.ar_arg_upath2
;
2095 if (*pathp
== NULL
) {
2096 *pathp
= (char *)kalloc(MAXPATHLEN
);
2101 if (canon_path(p
, upath
, *pathp
) == 0) {
2102 if (flags
& ARG_UPATH1
)
2103 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH1
;
2105 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH2
;
2107 kfree((vm_offset_t
)*pathp
, MAXPATHLEN
);
2113 * Function to save the path and vnode attr information into the audit
2116 * It is assumed that the caller will hold any vnode locks necessary to
2117 * perform a VOP_GETATTR() on the passed vnode.
2119 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but
2120 * always provides access to the generation number as we need that
2121 * to construct the BSM file ID.
2122 * XXX: We should accept the process argument from the caller, since
2123 * it's very likely they already have a reference.
2124 * XXX: Error handling in this function is poor.
2127 audit_arg_vnpath(struct vnode
*vp
, u_int64_t flags
)
2129 struct kaudit_record
*ar
;
2134 struct vnode_au_info
*vnp
;
2141 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2144 if (flags
& (ARG_VNODE1
| ARG_VNODE2
) == 0)
2149 if (flags
& ARG_VNODE1
) {
2150 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH1
);
2151 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE1
);
2152 pathp
= &ar
->k_ar
.ar_arg_kpath1
;
2153 vnp
= &ar
->k_ar
.ar_arg_vnode1
;
2156 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH2
);
2157 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE2
);
2158 pathp
= &ar
->k_ar
.ar_arg_kpath2
;
2159 vnp
= &ar
->k_ar
.ar_arg_vnode2
;
2162 if (*pathp
== NULL
) {
2163 *pathp
= (char *)kalloc(MAXPATHLEN
);
2169 * If vn_getpath() succeeds, place it in a string buffer
2170 * attached to the audit record, and set a flag indicating
2174 if (vn_getpath(vp
, *pathp
, &len
) == 0) {
2175 if (flags
& ARG_VNODE1
)
2176 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH1
;
2178 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH2
;
2180 kfree((vm_offset_t
)*pathp
, MAXPATHLEN
);
2185 * XXX: We'd assert the vnode lock here, only Darwin doesn't
2186 * appear to have vnode locking assertions.
2188 error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
);
2190 /* XXX: How to handle this case? */
2194 vnp
->vn_mode
= vattr
.va_mode
;
2195 vnp
->vn_uid
= vattr
.va_uid
;
2196 vnp
->vn_gid
= vattr
.va_gid
;
2197 vnp
->vn_dev
= vattr
.va_rdev
;
2198 vnp
->vn_fsid
= vattr
.va_fsid
;
2199 vnp
->vn_fileid
= vattr
.va_fileid
;
2200 vnp
->vn_gen
= vattr
.va_gen
;
2201 if (flags
& ARG_VNODE1
)
2202 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE1
;
2204 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE2
;
2209 audit_arg_mach_port1(mach_port_t port
)
2211 struct kaudit_record
*ar
;
2217 ar
->k_ar
.ar_arg_mach_port1
= port
;
2218 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT1
;
2222 audit_arg_mach_port2(mach_port_t port
)
2224 struct kaudit_record
*ar
;
2230 ar
->k_ar
.ar_arg_mach_port2
= port
;
2231 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT2
;
2235 * The close() system call uses it's own audit call to capture the
2236 * path/vnode information because those pieces are not easily obtained
2237 * within the system call itself.
2240 audit_sysclose(struct proc
*p
, int fd
)
2246 if (getvnode(p
, fd
, &fp
) != 0)
2249 audit_arg_vnpath((struct vnode
*)fp
->f_data
, ARG_VNODE1
);
2262 audit_shutdown(void)
2268 audit(struct proc
*p
, struct audit_args
*uap
, register_t
*retval
)
2274 auditon(struct proc
*p
, struct auditon_args
*uap
, register_t
*retval
)
2280 getauid(struct proc
*p
, struct getauid_args
*uap
, register_t
*retval
)
2286 setauid(struct proc
*p
, struct setauid_args
*uap
, register_t
*retval
)
2292 getaudit(struct proc
*p
, struct getaudit_args
*uap
, register_t
*retval
)
2298 setaudit(struct proc
*p
, struct setaudit_args
*uap
, register_t
*retval
)
2304 getaudit_addr(struct proc
*p
, struct getaudit_addr_args
*uap
, register_t
*retval
)
2310 setaudit_addr(struct proc
*p
, struct setaudit_addr_args
*uap
, register_t
*retval
)
2316 auditctl(struct proc
*p
, struct auditctl_args
*uap
, register_t
*retval
)
2322 audit_proc_init(struct proc
*p
)
2328 audit_proc_fork(struct proc
*parent
, struct proc
*child
)
2334 audit_proc_free(struct proc
*p
)