2 * Copyright (c) 2003-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@
22 #include <sys/param.h>
23 #include <sys/fcntl.h>
24 #include <sys/kernel.h>
26 #include <sys/namei.h>
27 #include <sys/proc_internal.h>
28 #include <sys/kauth.h>
29 #include <sys/queue.h>
30 #include <sys/systm.h>
32 #include <sys/ucred.h>
34 #include <sys/unistd.h>
35 #include <sys/file_internal.h>
36 #include <sys/vnode_internal.h>
38 #include <sys/syscall.h>
39 #include <sys/malloc.h>
41 #include <sys/sysent.h>
42 #include <sys/sysproto.h>
43 #include <sys/vfs_context.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socketvar.h>
48 #include <bsm/audit.h>
49 #include <bsm/audit_kevents.h>
50 #include <bsm/audit_klib.h>
51 #include <bsm/audit_kernel.h>
53 #include <mach/host_priv.h>
54 #include <mach/host_special_ports.h>
55 #include <mach/audit_triggers_server.h>
57 #include <kern/host.h>
58 #include <kern/kalloc.h>
59 #include <kern/zalloc.h>
60 #include <kern/lock.h>
61 #include <kern/wait_queue.h>
62 #include <kern/sched_prim.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_pcb.h>
72 * The AUDIT_EXCESSIVELY_VERBOSE define enables a number of
73 * gratuitously noisy printf's to the console. Due to the
74 * volume, it should be left off unless you want your system
75 * to churn a lot whenever the audit record flow gets high.
77 /* #define AUDIT_EXCESSIVELY_VERBOSE */
78 #ifdef AUDIT_EXCESSIVELY_VERBOSE
79 #define AUDIT_PRINTF_ONLY
80 #define AUDIT_PRINTF(x) printf x
82 #define AUDIT_PRINTF_ONLY __unused
83 #define AUDIT_PRINTF(X)
90 #define assert(cond) \
91 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
93 #include <kern/assert.h>
94 #endif /* DIAGNOSTIC */
97 * Define the audit control flags.
103 * Mutex to protect global variables shared between various threads and
106 static mutex_t
*audit_mtx
;
109 * Queue of audit records ready for delivery to disk. We insert new
110 * records at the tail, and remove records from the head. Also,
111 * a count of the number of records used for checking queue depth.
112 * In addition, a counter of records that we have allocated but are
113 * not yet in the queue, which is needed to estimate the total
114 * size of the combined set of records outstanding in the system.
116 static TAILQ_HEAD(, kaudit_record
) audit_q
;
117 static size_t audit_q_len
;
118 static size_t audit_pre_q_len
;
120 static wait_queue_t audit_wait_queue
;
121 static zone_t audit_zone
;
124 * Condition variable to signal to the worker that it has work to do:
125 * either new records are in the queue, or a log replacement is taking
128 static int audit_worker_event
;
129 #define AUDIT_WORKER_EVENT ((event_t)&audit_worker_event)
132 * The audit worker thread (which is lazy started when we first
133 * rotate the audit log.
135 static thread_t audit_worker_thread
= THREAD_NULL
;
138 * When an audit log is rotated, the actual rotation must be performed
139 * by the audit worker thread, as it may have outstanding writes on the
140 * current audit log. audit_replacement_vp holds the vnode replacing
141 * the current vnode. We can't let more than one replacement occur
142 * at a time, so if more than one thread requests a replacement, only
143 * one can have the replacement "in progress" at any given moment. If
144 * a thread tries to replace the audit vnode and discovers a replacement
145 * is already in progress (i.e., audit_replacement_flag != 0), then it
146 * will sleep on audit_replacement_cv waiting its turn to perform a
147 * replacement. When a replacement is completed, this cv is signalled
148 * by the worker thread so a waiting thread can start another replacement.
149 * We also store a credential to perform audit log write operations with.
151 static int audit_replacement_event
;
152 #define AUDIT_REPLACEMENT_EVENT ((event_t)&audit_replacement_event)
154 static int audit_replacement_flag
;
155 static struct vnode
*audit_replacement_vp
;
156 static kauth_cred_t audit_replacement_cred
;
159 * Wait queue for auditing threads that cannot commit the audit
160 * record at the present time. Also, the queue control parameter
163 static int audit_commit_event
;
164 #define AUDIT_COMMIT_EVENT ((event_t)&audit_commit_event)
166 static struct au_qctrl audit_qctrl
;
169 * Flags to use on audit files when opening and closing.
171 static const int audit_open_flags
= FWRITE
| O_APPEND
;
172 static const int audit_close_flags
= FWRITE
| O_APPEND
;
175 * Global audit statistiscs.
177 static struct audit_fstat audit_fstat
;
180 Preselection mask for non-attributable events.
182 static struct au_mask audit_nae_mask
;
185 * Flags related to Kernel->user-space communication.
187 static int audit_file_rotate_wait
;
190 * Flags controlling behavior in low storage situations.
191 * Should we panic if a write fails? Should we fail stop
192 * if we're out of disk space? Are we currently "failing
193 * stop" due to out of disk space?
195 static int audit_panic_on_write_fail
;
196 static int audit_fail_stop
;
197 static int audit_in_failure
;
200 * When in a fail-stop mode, threads will drop into this wait queue
201 * rather than perform auditable events. They won't ever get woken
204 static int audit_failure_event
;
205 #define AUDIT_FAILURE_EVENT ((event_t)&audit_failure_event)
208 * XXX: Couldn't find the include file for this, so copied kern_exec.c's
211 extern task_t kernel_task
;
214 audit_free(struct kaudit_record
*ar
)
216 if (ar
->k_ar
.ar_arg_upath1
!= NULL
) {
217 kfree(ar
->k_ar
.ar_arg_upath1
, MAXPATHLEN
);
219 if (ar
->k_ar
.ar_arg_upath2
!= NULL
) {
220 kfree(ar
->k_ar
.ar_arg_upath2
, MAXPATHLEN
);
223 if (ar
->k_ar
.ar_arg_kpath1
!= NULL
) {
224 kfree(ar
->k_ar
.ar_arg_kpath1
, MAXPATHLEN
);
227 if (ar
->k_ar
.ar_arg_kpath2
!= NULL
) {
228 kfree(ar
->k_ar
.ar_arg_kpath2
, MAXPATHLEN
);
231 if (ar
->k_ar
.ar_arg_text
!= NULL
) {
232 kfree(ar
->k_ar
.ar_arg_text
, MAXPATHLEN
);
235 if (ar
->k_udata
!= NULL
) {
236 kfree(ar
->k_udata
, ar
->k_ulen
);
239 zfree(audit_zone
, ar
);
243 audit_write(struct vnode
*vp
, struct kaudit_record
*ar
, kauth_cred_t cred
,
246 struct vfsstatfs
*mnt_stat
= &vp
->v_mount
->mnt_vfsstat
;
248 struct au_record
*bsm
;
249 /* KVV maybe we should take a context as a param to audit_write? */
250 struct vfs_context context
;
253 mach_port_t audit_port
;
256 * First, gather statistics on the audit log file and file system
257 * so that we know how we're doing on space. In both cases,
258 * if we're unable to perform the operation, we drop the record
259 * and return. However, this is arguably an assertion failure.
262 context
.vc_ucred
= cred
;
263 ret
= vfs_update_vfsstat(vp
->v_mount
, &context
);
267 /* update the global stats struct */
268 if ((ret
= vnode_size(vp
, &file_size
, &context
)) != 0)
270 audit_fstat
.af_currsz
= file_size
;
273 * Send a message to the audit daemon when disk space is getting
275 * XXX Need to decide what to do if the trigger to the audit daemon
278 if(host_get_audit_control_port(host_priv_self(), &audit_port
)
280 printf("Cannot get audit control port\n");
282 if (audit_port
!= MACH_PORT_NULL
) {
286 * If we fall below percent free blocks, then trigger the
287 * audit daemon to do something about it.
289 if (audit_qctrl
.aq_minfree
!= 0) {
290 temp
= mnt_stat
->f_blocks
/ (100 / audit_qctrl
.aq_minfree
);
291 if (mnt_stat
->f_bfree
< temp
) {
292 ret
= audit_triggers(audit_port
,
293 AUDIT_TRIGGER_LOW_SPACE
);
294 if (ret
!= KERN_SUCCESS
) {
296 "Failed audit_triggers(AUDIT_TRIGGER_LOW_SPACE): %d\n", ret
);
298 * XXX: What to do here? Disable auditing?
304 /* Check if the current log file is full; if so, call for
305 * a log rotate. This is not an exact comparison; we may
306 * write some records over the limit. If that's not
307 * acceptable, then add a fudge factor here.
309 if ((audit_fstat
.af_filesz
!= 0) &&
310 (audit_file_rotate_wait
== 0) &&
311 (file_size
>= audit_fstat
.af_filesz
)) {
312 audit_file_rotate_wait
= 1;
313 ret
= audit_triggers(audit_port
,
314 AUDIT_TRIGGER_FILE_FULL
);
315 if (ret
!= KERN_SUCCESS
) {
317 "Failed audit_triggers(AUDIT_TRIGGER_FILE_FULL): %d\n", ret
);
318 /* XXX what to do here? */
324 * If the estimated amount of audit data in the audit event queue
325 * (plus records allocated but not yet queued) has reached the
326 * amount of free space on the disk, then we need to go into an
327 * audit fail stop state, in which we do not permit the
328 * allocation/committing of any new audit records. We continue to
329 * process packets but don't allow any activities that might
330 * generate new records. In the future, we might want to detect
331 * when space is available again and allow operation to continue,
332 * but this behavior is sufficient to meet fail stop requirements
335 if (audit_fail_stop
&&
337 ((audit_q_len
+ audit_pre_q_len
+ 1) * MAX_AUDIT_RECORD_SIZE
) /
338 mnt_stat
->f_bsize
>= (unsigned long)(mnt_stat
->f_bfree
)) {
340 "audit_worker: free space below size of audit queue, failing stop\n");
341 audit_in_failure
= 1;
345 * If there is a user audit record attached to the kernel record,
346 * then write the user record.
348 /* XXX Need to decide a few things here: IF the user audit
349 * record is written, but the write of the kernel record fails,
350 * what to do? Should the kernel record come before or after the
351 * user record? For now, we write the user record first, and
354 if (ar
->k_ar_commit
& AR_COMMIT_USER
) {
355 if (vnode_getwithref(vp
) == 0) {
356 ret
= vn_rdwr(UIO_WRITE
, vp
, (void *)ar
->k_udata
, ar
->k_ulen
,
357 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
);
367 * Convert the internal kernel record to BSM format and write it
368 * out if everything's OK.
370 if (!(ar
->k_ar_commit
& AR_COMMIT_KERNEL
)) {
375 ret
= kaudit_to_bsm(ar
, &bsm
);
376 if (ret
== BSM_NOAUDIT
) {
382 * XXX: We drop the record on BSM conversion failure, but really
383 * this is an assertion failure.
385 if (ret
== BSM_FAILURE
) {
386 AUDIT_PRINTF(("BSM conversion failure\n"));
391 /* XXX This function can be called with the kernel funnel held,
392 * which is not optimal. We should break the write functionality
393 * away from the BSM record generation and have the BSM generation
394 * done before this function is called. This function will then
395 * take the BSM record as a parameter.
397 if ((ret
= vnode_getwithref(vp
)) == 0) {
398 ret
= (vn_rdwr(UIO_WRITE
, vp
, (void *)bsm
->data
, bsm
->len
,
399 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
));
406 * When we're done processing the current record, we have to
407 * check to see if we're in a failure mode, and if so, whether
408 * this was the last record left to be drained. If we're done
409 * draining, then we fsync the vnode and panic.
411 if (audit_in_failure
&&
412 audit_q_len
== 0 && audit_pre_q_len
== 0) {
413 (void)VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
414 panic("Audit store overflow; record queue drained.");
423 int do_replacement_signal
, error
, release_funnel
;
424 TAILQ_HEAD(, kaudit_record
) ar_worklist
;
425 struct kaudit_record
*ar
;
426 struct vnode
*audit_vp
, *old_vp
;
427 kauth_cred_t audit_cred
;
428 kauth_cred_t old_cred
;
429 struct proc
*audit_p
;
431 AUDIT_PRINTF(("audit_worker starting\n"));
433 TAILQ_INIT(&ar_worklist
);
435 audit_p
= current_proc();
439 * XXX: Presumably we can assume Mach threads are started without
440 * holding the BSD kernel funnel?
442 thread_funnel_set(kernel_flock
, FALSE
);
444 mutex_lock(audit_mtx
);
447 * First priority: replace the audit log target if requested.
448 * As we actually close the vnode in the worker thread, we
449 * need to grab the funnel, which means releasing audit_mtx.
450 * In case another replacement was scheduled while the mutex
451 * we released, we loop.
453 * XXX It could well be we should drain existing records
454 * first to ensure that the timestamps and ordering
457 do_replacement_signal
= 0;
458 while (audit_replacement_flag
!= 0) {
459 old_cred
= audit_cred
;
461 audit_cred
= audit_replacement_cred
;
462 audit_vp
= audit_replacement_vp
;
463 audit_replacement_cred
= NOCRED
;
464 audit_replacement_vp
= NULL
;
465 audit_replacement_flag
= 0;
467 audit_enabled
= (audit_vp
!= NULL
);
469 if (old_vp
!= NULL
|| audit_vp
!= NULL
) {
470 mutex_unlock(audit_mtx
);
471 thread_funnel_set(kernel_flock
, TRUE
);
476 * XXX: What to do about write failures here?
478 if (old_vp
!= NULL
) {
479 AUDIT_PRINTF(("Closing old audit file\n"));
480 vn_close(old_vp
, audit_close_flags
, old_cred
,
482 kauth_cred_unref(&old_cred
);
484 AUDIT_PRINTF(("Audit file closed\n"));
486 if (audit_vp
!= NULL
) {
487 AUDIT_PRINTF(("Opening new audit file\n"));
489 if (release_funnel
) {
490 thread_funnel_set(kernel_flock
, FALSE
);
491 mutex_lock(audit_mtx
);
493 do_replacement_signal
= 1;
496 * Signal that replacement have occurred to wake up and
497 * start any other replacements started in parallel. We can
498 * continue about our business in the mean time. We
499 * broadcast so that both new replacements can be inserted,
500 * but also so that the source(s) of replacement can return
503 if (do_replacement_signal
)
504 wait_queue_wakeup_all(audit_wait_queue
,
505 AUDIT_REPLACEMENT_EVENT
, THREAD_AWAKENED
);
508 * Next, check to see if we have any records to drain into
509 * the vnode. If not, go back to waiting for an event.
511 if (TAILQ_EMPTY(&audit_q
)) {
514 AUDIT_PRINTF(("audit_worker waiting\n"));
515 ret
= wait_queue_assert_wait(audit_wait_queue
,
519 mutex_unlock(audit_mtx
);
521 assert(ret
== THREAD_WAITING
);
522 ret
= thread_block(THREAD_CONTINUE_NULL
);
523 assert(ret
== THREAD_AWAKENED
);
524 AUDIT_PRINTF(("audit_worker woken up\n"));
525 AUDIT_PRINTF(("audit_worker: new vp = %p; value of flag %d\n",
526 audit_replacement_vp
, audit_replacement_flag
));
528 mutex_lock(audit_mtx
);
533 * If we have records, but there's no active vnode to
534 * write to, drain the record queue. Generally, we
535 * prevent the unnecessary allocation of records
536 * elsewhere, but we need to allow for races between
537 * conditional allocation and queueing. Go back to
538 * waiting when we're done.
540 * XXX: We go out of our way to avoid calling audit_free()
541 * with the audit_mtx held, to avoid a lock order reversal
542 * as free() may grab the funnel. This will be fixed at
545 if (audit_vp
== NULL
) {
546 while ((ar
= TAILQ_FIRST(&audit_q
))) {
547 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
549 if (audit_q_len
<= audit_qctrl
.aq_lowater
)
550 wait_queue_wakeup_one(
555 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
557 mutex_unlock(audit_mtx
);
558 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
559 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
562 mutex_lock(audit_mtx
);
567 * We have both records to write, and an active vnode
568 * to write to. Dequeue a record, and start the write.
569 * Eventually, it might make sense to dequeue several
570 * records and perform our own clustering, if the lower
571 * layers aren't doing it automatically enough.
573 * XXX: We go out of our way to avoid calling audit_free()
574 * with the audit_mtx held, to avoid a lock order reversal
575 * as free() may grab the funnel. This will be fixed at
578 while ((ar
= TAILQ_FIRST(&audit_q
))) {
579 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
581 if (audit_q_len
<= audit_qctrl
.aq_lowater
) {
582 wait_queue_wakeup_one(audit_wait_queue
,
583 AUDIT_COMMIT_EVENT
, THREAD_AWAKENED
);
586 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
588 mutex_unlock(audit_mtx
);
590 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
591 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
592 if (audit_vp
!= NULL
) {
594 * XXX: What should happen if there's a write
597 if (!release_funnel
) {
598 thread_funnel_set(kernel_flock
, TRUE
);
601 error
= audit_write(audit_vp
, ar
, audit_cred
,
603 if (error
&& audit_panic_on_write_fail
) {
604 panic("audit_worker: write error %d\n",
607 printf("audit_worker: write error %d\n",
614 thread_funnel_set(kernel_flock
, FALSE
);
615 mutex_lock(audit_mtx
);
623 /* Verify that the syscall to audit event table is the same
624 * size as the system call table.
626 if (nsys_au_event
!= nsysent
) {
627 printf("Security auditing service initialization failed, ");
628 printf("audit event table doesn't match syscall table.\n");
632 printf("Security auditing service present\n");
633 TAILQ_INIT(&audit_q
);
637 audit_replacement_cred
= NULL
;
638 audit_replacement_flag
= 0;
639 audit_file_rotate_wait
= 0;
640 audit_replacement_vp
= NULL
;
641 audit_fstat
.af_filesz
= 0; /* '0' means unset, unbounded */
642 audit_fstat
.af_currsz
= 0;
643 audit_qctrl
.aq_hiwater
= AQ_HIWATER
;
644 audit_qctrl
.aq_lowater
= AQ_LOWATER
;
645 audit_qctrl
.aq_bufsz
= AQ_BUFSZ
;
646 audit_qctrl
.aq_minfree
= AU_FS_MINFREE
;
648 audit_mtx
= mutex_alloc(0);
649 audit_wait_queue
= wait_queue_alloc(SYNC_POLICY_FIFO
);
650 audit_zone
= zinit(sizeof(struct kaudit_record
),
651 AQ_HIWATER
*sizeof(struct kaudit_record
),
655 /* Initialize the BSM audit subsystem. */
660 audit_rotate_vnode(kauth_cred_t cred
, struct vnode
*vp
)
665 * If other parallel log replacements have been requested, we wait
666 * until they've finished before continuing.
668 mutex_lock(audit_mtx
);
669 while (audit_replacement_flag
!= 0) {
671 AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for "
673 ret
= wait_queue_assert_wait(audit_wait_queue
,
674 AUDIT_REPLACEMENT_EVENT
,
677 mutex_unlock(audit_mtx
);
679 assert(ret
== THREAD_WAITING
);
680 ret
= thread_block(THREAD_CONTINUE_NULL
);
681 assert(ret
== THREAD_AWAKENED
);
682 AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n",
683 audit_replacement_flag
));
685 mutex_lock(audit_mtx
);
687 audit_replacement_cred
= cred
;
688 audit_replacement_flag
= 1;
689 audit_replacement_vp
= vp
;
692 * Start or wake up the audit worker to perform the exchange.
693 * It will have to wait until we release the mutex.
695 if (audit_worker_thread
== THREAD_NULL
)
696 audit_worker_thread
= kernel_thread(kernel_task
,
699 wait_queue_wakeup_one(audit_wait_queue
,
704 * Wait for the audit_worker to broadcast that a replacement has
705 * taken place; we know that once this has happened, our vnode
706 * has been replaced in, so we can return successfully.
708 AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of "
710 ret
= wait_queue_assert_wait(audit_wait_queue
,
711 AUDIT_REPLACEMENT_EVENT
,
714 mutex_unlock(audit_mtx
);
716 assert(ret
== THREAD_WAITING
);
717 ret
= thread_block(THREAD_CONTINUE_NULL
);
718 assert(ret
== THREAD_AWAKENED
);
719 AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by "
720 "audit_worker (flag " "now %d)\n", audit_replacement_flag
));
722 audit_file_rotate_wait
= 0; /* We can now request another rotation */
726 * Drain the audit queue and close the log at shutdown.
731 audit_rotate_vnode(NULL
, NULL
);
734 static __inline__
struct uthread
*
737 return (get_bsdthread_info(current_thread()));
740 static __inline__
struct kaudit_record
*
743 return (curuthread()->uu_ar
);
746 /**********************************
747 * Begin system calls. *
748 **********************************/
750 * System call to allow a user space application to submit a BSM audit
751 * record to the kernel for inclusion in the audit log. This function
752 * does little verification on the audit record that is submitted.
754 * XXXAUDIT: Audit preselection for user records does not currently
755 * work, since we pre-select only based on the AUE_audit event type,
756 * not the event type submitted as part of the user audit data.
760 audit(struct proc
*p
, struct audit_args
*uap
, __unused register_t
*retval
)
764 struct kaudit_record
*ar
;
765 struct uthread
*uthr
;
767 error
= suser(kauth_cred_get(), &p
->p_acflag
);
771 if ((uap
->length
<= 0) || (uap
->length
> (int)audit_qctrl
.aq_bufsz
))
776 /* If there's no current audit record (audit() itself not audited)
777 * commit the user audit record.
781 if (uthr
== NULL
) /* can this happen? */
784 /* This is not very efficient; we're required to allocate
785 * a complete kernel audit record just so the user record
788 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
789 if (uthr
->uu_ar
== NULL
) /* auditing not on, or memory error */
794 if (uap
->length
> MAX_AUDIT_RECORD_SIZE
)
797 rec
= (void *)kalloc((vm_size_t
)uap
->length
);
799 error
= copyin(uap
->record
, rec
, uap
->length
);
803 /* Verify the record */
804 if (bsm_rec_verify(rec
) == 0) {
809 /* Attach the user audit record to the kernel audit record. Because
810 * this system call is an auditable event, we will write the user
811 * record along with the record for this audit event.
814 ar
->k_ar_commit
|= AR_COMMIT_USER
;
815 ar
->k_ulen
= uap
->length
;
819 /* audit_syscall_exit() will free the audit record on the thread
820 * even if we allocated it above.
822 kfree(rec
, uap
->length
);
827 * System call to manipulate auditing.
831 auditon(struct proc
*p
, __unused
struct auditon_args
*uap
, __unused register_t
*retval
)
835 union auditon_udata udata
;
838 AUDIT_ARG(cmd
, uap
->cmd
);
839 ret
= suser(kauth_cred_get(), &p
->p_acflag
);
844 if ((len
<= 0) || (len
> (int)sizeof(union auditon_udata
)))
847 memset((void *)&udata
, 0, sizeof(udata
));
850 /* Some of the GET commands use the arguments too */
864 case A_GETPINFO_ADDR
:
865 ret
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
868 AUDIT_ARG(auditon
, &udata
);
872 /* XXX Need to implement these commands by accessing the global
873 * values associated with the commands.
877 if (!audit_fail_stop
)
878 udata
.au_policy
|= AUDIT_CNT
;
879 if (audit_panic_on_write_fail
)
880 udata
.au_policy
|= AUDIT_AHLT
;
883 if (udata
.au_policy
& ~(AUDIT_CNT
|AUDIT_AHLT
))
886 * XXX - Need to wake up waiters if the policy relaxes?
888 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
889 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
892 udata
.au_mask
= audit_nae_mask
;
895 audit_nae_mask
= udata
.au_mask
;
898 udata
.au_qctrl
= audit_qctrl
;
901 if ((udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
902 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
903 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
904 (udata
.au_qctrl
.aq_minfree
< 0) ||
905 (udata
.au_qctrl
.aq_minfree
> 100))
908 audit_qctrl
= udata
.au_qctrl
;
909 /* XXX The queue delay value isn't used with the kernel. */
910 audit_qctrl
.aq_delay
= -1;
931 if (audit_enabled
&& !audit_suspended
)
932 udata
.au_cond
= AUC_AUDITING
;
934 udata
.au_cond
= AUC_NOAUDIT
;
937 if (udata
.au_cond
== AUC_NOAUDIT
)
939 if (udata
.au_cond
== AUC_AUDITING
)
941 if (udata
.au_cond
== AUC_DISABLED
) {
947 udata
.au_evclass
.ec_class
=
948 au_event_class(udata
.au_evclass
.ec_number
);
951 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
952 udata
.au_evclass
.ec_class
);
955 if (udata
.au_aupinfo
.ap_pid
< 1)
957 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
960 udata
.au_aupinfo
.ap_auid
= tp
->p_ucred
->cr_au
.ai_auid
;
961 udata
.au_aupinfo
.ap_mask
.am_success
=
962 tp
->p_ucred
->cr_au
.ai_mask
.am_success
;
963 udata
.au_aupinfo
.ap_mask
.am_failure
=
964 tp
->p_ucred
->cr_au
.ai_mask
.am_failure
;
965 udata
.au_aupinfo
.ap_termid
.machine
=
966 tp
->p_ucred
->cr_au
.ai_termid
.machine
;
967 udata
.au_aupinfo
.ap_termid
.port
=
968 tp
->p_ucred
->cr_au
.ai_termid
.port
;
969 udata
.au_aupinfo
.ap_asid
= tp
->p_ucred
->cr_au
.ai_asid
;
972 if (udata
.au_aupinfo
.ap_pid
< 1)
974 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
978 * we are modifying the audit info in a credential so we need a new
979 * credential (or take another reference on an existing credential that
980 * matches our new one). We must do this because the audit info in the
981 * credential is used as part of our hash key. Get current credential
982 * in the target process and take a reference while we muck with it.
985 kauth_cred_t my_cred
, my_new_cred
;
986 struct auditinfo temp_auditinfo
;
988 my_cred
= kauth_cred_proc_ref(tp
);
990 * Set the credential with new info. If there is no
991 * change, we get back the same credential we passed
992 * in; if there is a change, we drop the reference on
993 * the credential we passed in. The subsequent
994 * compare is safe, because it is a pointer compare
995 * rather than a contents compare.
997 temp_auditinfo
= my_cred
->cr_au
;
998 temp_auditinfo
.ai_mask
.am_success
=
999 udata
.au_aupinfo
.ap_mask
.am_success
;
1000 temp_auditinfo
.ai_mask
.am_failure
=
1001 udata
.au_aupinfo
.ap_mask
.am_failure
;
1002 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1004 if (my_cred
!= my_new_cred
) {
1006 /* need to protect for a race where another thread also changed
1007 * the credential after we took our reference. If p_ucred has
1008 * changed then we should restart this again with the new cred.
1010 if (tp
->p_ucred
!= my_cred
) {
1012 kauth_cred_unref(&my_new_cred
);
1016 tp
->p_ucred
= my_new_cred
;
1019 /* drop old proc reference or our extra reference */
1020 kauth_cred_unref(&my_cred
);
1025 if ((udata
.au_fstat
.af_filesz
!= 0) &&
1026 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
))
1028 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
1031 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
1032 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
1034 case A_GETPINFO_ADDR
:
1044 /* Copy data back to userspace for the GET comands */
1056 case A_GETPINFO_ADDR
:
1058 ret
= copyout((void *)&udata
, uap
->data
, uap
->length
);
1068 * System calls to manage the user audit information.
1069 * XXXAUDIT May need to lock the proc structure.
1073 getauid(struct proc
*p
, struct getauid_args
*uap
, __unused register_t
*retval
)
1077 error
= copyout((void *)&kauth_cred_get()->cr_au
.ai_auid
,
1078 uap
->auid
, sizeof(au_id_t
));
1087 setauid(struct proc
*p
, struct setauid_args
*uap
, __unused register_t
*retval
)
1092 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1096 error
= copyin(uap
->auid
,
1097 (void *)&temp_au_id
,
1103 * we are modifying the audit info in a credential so we need a new
1104 * credential (or take another reference on an existing credential that
1105 * matches our new one). We must do this because the audit info in the
1106 * credential is used as part of our hash key. Get current credential
1107 * in the target process and take a reference while we muck with it.
1110 kauth_cred_t my_cred
, my_new_cred
;
1111 struct auditinfo temp_auditinfo
;
1113 my_cred
= kauth_cred_proc_ref(p
);
1115 * Set the credential with new info. If there is no change,
1116 * we get back the same credential we passed in; if there is
1117 * a change, we drop the reference on the credential we
1118 * passed in. The subsequent compare is safe, because it is
1119 * a pointer compare rather than a contents compare.
1121 temp_auditinfo
= my_cred
->cr_au
;
1122 temp_auditinfo
.ai_auid
= temp_au_id
;
1123 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1125 if (my_cred
!= my_new_cred
) {
1127 /* need to protect for a race where another thread also changed
1128 * the credential after we took our reference. If p_ucred has
1129 * changed then we should restart this again with the new cred.
1131 if (p
->p_ucred
!= my_cred
) {
1133 kauth_cred_unref(&my_new_cred
);
1137 p
->p_ucred
= my_new_cred
;
1140 /* drop old proc reference or our extra reference */
1141 kauth_cred_unref(&my_cred
);
1145 /* propagate the change from the process to Mach task */
1146 set_security_token(p
);
1148 audit_arg_auid(kauth_cred_get()->cr_au
.ai_auid
);
1153 * System calls to get and set process audit information.
1154 * If the caller is privileged, they get the whole set of
1155 * audit information. Otherwise, the real audit mask is
1156 * filtered out - but the rest of the information is
1161 getaudit(struct proc
*p
, struct getaudit_args
*uap
, __unused register_t
*retval
)
1163 struct auditinfo ai
;
1166 ai
= kauth_cred_get()->cr_au
;
1168 /* only superuser gets to see the real mask */
1169 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1171 ai
.ai_mask
.am_success
= ~0;
1172 ai
.ai_mask
.am_failure
= ~0;
1175 error
= copyout(&ai
, uap
->auditinfo
, sizeof(ai
));
1184 setaudit(struct proc
*p
, struct setaudit_args
*uap
, __unused register_t
*retval
)
1187 struct auditinfo temp_auditinfo
;
1188 kauth_cred_t safecred
;
1190 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1194 error
= copyin(uap
->auditinfo
,
1195 (void *)&temp_auditinfo
,
1196 sizeof(temp_auditinfo
));
1201 * we are modifying the audit info in a credential so we need a new
1202 * credential (or take another reference on an existing credential that
1203 * matches our new one). We must do this because the audit info in the
1204 * credential is used as part of our hash key. Get current credential
1205 * in the target process and take a reference while we muck with it.
1208 kauth_cred_t my_cred
, my_new_cred
;
1210 my_cred
= kauth_cred_proc_ref(p
);
1212 * Set the credential with new info. If there is no change,
1213 * we get back the same credential we passed in; if there is
1214 * a change, we drop the reference on the credential we
1215 * passed in. The subsequent compare is safe, because it is
1216 * a pointer compare rather than a contents compare.
1218 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1220 if (my_cred
!= my_new_cred
) {
1222 /* need to protect for a race where another thread also changed
1223 * the credential after we took our reference. If p_ucred has
1224 * changed then we should restart this again with the new cred.
1226 if (p
->p_ucred
!= my_cred
) {
1228 kauth_cred_unref(&my_new_cred
);
1232 p
->p_ucred
= my_new_cred
;
1235 /* drop old proc reference or our extra reference */
1236 kauth_cred_unref(&my_cred
);
1240 /* propagate the change from the process to Mach task */
1241 set_security_token(p
);
1243 safecred
= kauth_cred_proc_ref(p
);
1244 audit_arg_auditinfo(&safecred
->cr_au
);
1245 kauth_cred_unref(&safecred
);
1252 getaudit_addr(struct proc
*p
, __unused
struct getaudit_addr_args
*uap
, __unused register_t
*retval
)
1259 setaudit_addr(struct proc
*p
, __unused
struct setaudit_addr_args
*uap
, __unused register_t
*retval
)
1263 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1270 * Syscall to manage audit files.
1275 auditctl(struct proc
*p
, struct auditctl_args
*uap
, __unused register_t
*retval
)
1277 struct nameidata nd
;
1281 struct vfs_context context
;
1283 context
.vc_proc
= p
;
1284 context
.vc_ucred
= kauth_cred_get();
1286 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1294 * If a path is specified, open the replacement vnode, perform
1295 * validity checks, and grab another reference to the current
1298 if (uap
->path
!= 0) {
1299 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1300 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1301 uap
->path
, &context
);
1302 flags
= audit_open_flags
;
1303 error
= vn_open(&nd
, flags
, 0);
1307 if (vp
->v_type
!= VREG
) {
1308 vn_close(vp
, audit_close_flags
, kauth_cred_get(), p
);
1313 cred
= kauth_cred_get_with_ref();
1314 audit_suspended
= 0;
1317 * a vp and cred of NULL is valid at this point
1318 * and indicates we're to turn off auditing...
1320 audit_rotate_vnode(cred
, vp
);
1327 /**********************************
1328 * End of system calls. *
1329 **********************************/
1334 struct kaudit_record
*
1335 audit_new(int event
, struct proc
*p
, __unused
struct uthread
*uthread
)
1337 struct kaudit_record
*ar
;
1339 kauth_cred_t safecred
;
1342 * Eventually, there may be certain classes of events that
1343 * we will audit regardless of the audit state at the time
1344 * the record is created. These events will generally
1345 * correspond to changes in the audit state. The dummy
1346 * code below is from our first prototype, but may also
1347 * be used in the final version (with modified event numbers).
1350 if (event
!= AUDIT_EVENT_FILESTOP
&& event
!= AUDIT_EVENT_FILESTART
) {
1352 mutex_lock(audit_mtx
);
1353 no_record
= (audit_suspended
|| !audit_enabled
);
1354 mutex_unlock(audit_mtx
);
1362 * Initialize the audit record header.
1363 * XXX: We may want to fail-stop if allocation fails.
1364 * XXX: The number of outstanding uncommitted audit records is
1365 * limited by the number of concurrent threads servicing system
1366 * calls in the kernel.
1369 ar
= (struct kaudit_record
*)zalloc(audit_zone
);
1373 mutex_lock(audit_mtx
);
1375 mutex_unlock(audit_mtx
);
1377 bzero(ar
, sizeof(*ar
));
1378 ar
->k_ar
.ar_magic
= AUDIT_RECORD_MAGIC
;
1379 ar
->k_ar
.ar_event
= event
;
1380 nanotime(&ar
->k_ar
.ar_starttime
);
1382 safecred
= kauth_cred_proc_ref(p
);
1383 /* Export the subject credential. */
1384 cru2x(safecred
, &ar
->k_ar
.ar_subj_cred
);
1386 ar
->k_ar
.ar_subj_ruid
= safecred
->cr_ruid
;
1387 ar
->k_ar
.ar_subj_rgid
= safecred
->cr_rgid
;
1388 ar
->k_ar
.ar_subj_egid
= safecred
->cr_groups
[0];
1389 ar
->k_ar
.ar_subj_auid
= safecred
->cr_au
.ai_auid
;
1390 ar
->k_ar
.ar_subj_asid
= safecred
->cr_au
.ai_asid
;
1391 ar
->k_ar
.ar_subj_amask
= safecred
->cr_au
.ai_mask
;
1392 ar
->k_ar
.ar_subj_term
= safecred
->cr_au
.ai_termid
;
1393 kauth_cred_unref(&safecred
);
1395 ar
->k_ar
.ar_subj_pid
= p
->p_pid
;
1396 bcopy(p
->p_comm
, ar
->k_ar
.ar_subj_comm
, MAXCOMLEN
);
1403 * XXXAUDIT: So far, this is unused, and should probably be GC'd.
1406 audit_abort(struct kaudit_record
*ar
)
1408 mutex_lock(audit_mtx
);
1410 mutex_unlock(audit_mtx
);
1418 audit_commit(struct kaudit_record
*ar
, int error
, int retval
)
1422 struct au_mask
*aumask
;
1428 * Decide whether to commit the audit record by checking the
1429 * error value from the system call and using the appropriate
1432 if (ar
->k_ar
.ar_subj_auid
== AU_DEFAUDITID
)
1433 aumask
= &audit_nae_mask
;
1435 aumask
= &ar
->k_ar
.ar_subj_amask
;
1438 sorf
= AU_PRS_FAILURE
;
1440 sorf
= AU_PRS_SUCCESS
;
1442 switch(ar
->k_ar
.ar_event
) {
1445 /* The open syscall always writes a OPEN_RWTC event; limit the
1446 * to the proper type of event based on the flags and the error
1449 ar
->k_ar
.ar_event
= flags_and_error_to_openevent(ar
->k_ar
.ar_arg_fflags
, error
);
1453 ar
->k_ar
.ar_event
= ctlname_to_sysctlevent(ar
->k_ar
.ar_arg_ctlname
, ar
->k_ar
.ar_valid_arg
);
1457 /* Convert the auditon() command to an event */
1458 ar
->k_ar
.ar_event
= auditon_command_event(ar
->k_ar
.ar_arg_cmd
);
1462 if (au_preselect(ar
->k_ar
.ar_event
, aumask
, sorf
) != 0)
1463 ar
->k_ar_commit
|= AR_COMMIT_KERNEL
;
1465 if ((ar
->k_ar_commit
& (AR_COMMIT_USER
| AR_COMMIT_KERNEL
)) == 0) {
1466 mutex_lock(audit_mtx
);
1468 mutex_unlock(audit_mtx
);
1473 ar
->k_ar
.ar_errno
= error
;
1474 ar
->k_ar
.ar_retval
= retval
;
1477 * We might want to do some system-wide post-filtering
1478 * here at some point.
1482 * Timestamp system call end.
1484 nanotime(&ar
->k_ar
.ar_endtime
);
1486 mutex_lock(audit_mtx
);
1488 * Note: it could be that some records initiated while audit was
1489 * enabled should still be committed?
1491 if (audit_suspended
|| !audit_enabled
) {
1493 mutex_unlock(audit_mtx
);
1499 * Constrain the number of committed audit records based on
1500 * the configurable parameter.
1502 while (audit_q_len
>= audit_qctrl
.aq_hiwater
) {
1504 ret
= wait_queue_assert_wait(audit_wait_queue
,
1508 mutex_unlock(audit_mtx
);
1510 assert(ret
== THREAD_WAITING
);
1512 ret
= thread_block(THREAD_CONTINUE_NULL
);
1513 assert(ret
== THREAD_AWAKENED
);
1514 mutex_lock(audit_mtx
);
1517 TAILQ_INSERT_TAIL(&audit_q
, ar
, k_q
);
1520 wait_queue_wakeup_one(audit_wait_queue
, AUDIT_WORKER_EVENT
, THREAD_AWAKENED
);
1521 mutex_unlock(audit_mtx
);
1525 * Calls to set up and tear down audit structures associated with
1529 audit_syscall_enter(unsigned short code
, struct proc
*proc
,
1530 struct uthread
*uthread
)
1533 struct au_mask
*aumask
;
1535 audit_event
= sys_au_event
[code
];
1536 if (audit_event
== AUE_NULL
)
1539 assert(uthread
->uu_ar
== NULL
);
1541 /* Check which audit mask to use; either the kernel non-attributable
1542 * event mask or the process audit mask.
1544 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1545 aumask
= &audit_nae_mask
;
1547 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1550 * Allocate an audit record, if preselection allows it, and store
1551 * in the BSD thread for later use.
1553 if (au_preselect(audit_event
, aumask
,
1554 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1556 * If we're out of space and need to suspend unprivileged
1557 * processes, do that here rather than trying to allocate
1558 * another audit record.
1560 if (audit_in_failure
&&
1561 suser(kauth_cred_get(), &proc
->p_acflag
) != 0) {
1564 assert(audit_worker_thread
!= THREAD_NULL
);
1565 ret
= wait_queue_assert_wait(audit_wait_queue
,
1566 AUDIT_FAILURE_EVENT
, THREAD_UNINT
, 0);
1567 assert(ret
== THREAD_WAITING
);
1568 (void)thread_block(THREAD_CONTINUE_NULL
);
1569 panic("audit_failing_stop: thread continued");
1571 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1573 uthread
->uu_ar
= NULL
;
1578 audit_syscall_exit(int error
, AUDIT_PRINTF_ONLY
struct proc
*proc
, struct uthread
*uthread
)
1583 * Commit the audit record as desired; once we pass the record
1584 * into audit_commit(), the memory is owned by the audit
1586 * The return value from the system call is stored on the user
1587 * thread. If there was an error, the return value is set to -1,
1588 * imitating the behavior of the cerror routine.
1593 retval
= uthread
->uu_rval
[0];
1595 audit_commit(uthread
->uu_ar
, error
, retval
);
1596 if (uthread
->uu_ar
!= NULL
) {
1597 AUDIT_PRINTF(("audit record committed by pid %d\n", proc
->p_pid
));
1599 uthread
->uu_ar
= NULL
;
1604 * Calls to set up and tear down audit structures used during Mach
1608 audit_mach_syscall_enter(unsigned short audit_event
)
1610 struct uthread
*uthread
;
1612 struct au_mask
*aumask
;
1614 if (audit_event
== AUE_NULL
)
1617 uthread
= curuthread();
1618 if (uthread
== NULL
)
1621 proc
= current_proc();
1625 assert(uthread
->uu_ar
== NULL
);
1627 /* Check which audit mask to use; either the kernel non-attributable
1628 * event mask or the process audit mask.
1630 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1631 aumask
= &audit_nae_mask
;
1633 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1636 * Allocate an audit record, if desired, and store in the BSD
1637 * thread for later use.
1639 if (au_preselect(audit_event
, aumask
,
1640 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1641 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1643 uthread
->uu_ar
= NULL
;
1648 audit_mach_syscall_exit(int retval
, struct uthread
*uthread
)
1650 /* The error code from Mach system calls is the same as the
1653 /* XXX Is the above statement always true? */
1654 audit_commit(uthread
->uu_ar
, retval
, retval
);
1655 uthread
->uu_ar
= NULL
;
1660 * Calls to manipulate elements of the audit record structure from system
1661 * call code. Macro wrappers will prevent this functions from being
1662 * entered if auditing is disabled, avoiding the function call cost. We
1663 * check the thread audit record pointer anyway, as the audit condition
1664 * could change, and pre-selection may not have allocated an audit
1665 * record for this event.
1668 audit_arg_addr(user_addr_t addr
)
1670 struct kaudit_record
*ar
;
1676 ar
->k_ar
.ar_arg_addr
= CAST_DOWN(void *, addr
); /* XXX */
1677 ar
->k_ar
.ar_valid_arg
|= ARG_ADDR
;
1681 audit_arg_len(user_size_t len
)
1683 struct kaudit_record
*ar
;
1689 ar
->k_ar
.ar_arg_len
= CAST_DOWN(int, len
); /* XXX */
1690 ar
->k_ar
.ar_valid_arg
|= ARG_LEN
;
1694 audit_arg_fd(int fd
)
1696 struct kaudit_record
*ar
;
1702 ar
->k_ar
.ar_arg_fd
= fd
;
1703 ar
->k_ar
.ar_valid_arg
|= ARG_FD
;
1707 audit_arg_fflags(int fflags
)
1709 struct kaudit_record
*ar
;
1715 ar
->k_ar
.ar_arg_fflags
= fflags
;
1716 ar
->k_ar
.ar_valid_arg
|= ARG_FFLAGS
;
1720 audit_arg_gid(gid_t gid
, gid_t egid
, gid_t rgid
, gid_t sgid
)
1722 struct kaudit_record
*ar
;
1728 ar
->k_ar
.ar_arg_gid
= gid
;
1729 ar
->k_ar
.ar_arg_egid
= egid
;
1730 ar
->k_ar
.ar_arg_rgid
= rgid
;
1731 ar
->k_ar
.ar_arg_sgid
= sgid
;
1732 ar
->k_ar
.ar_valid_arg
|= (ARG_GID
| ARG_EGID
| ARG_RGID
| ARG_SGID
);
1736 audit_arg_uid(uid_t uid
, uid_t euid
, uid_t ruid
, uid_t suid
)
1738 struct kaudit_record
*ar
;
1744 ar
->k_ar
.ar_arg_uid
= uid
;
1745 ar
->k_ar
.ar_arg_euid
= euid
;
1746 ar
->k_ar
.ar_arg_ruid
= ruid
;
1747 ar
->k_ar
.ar_arg_suid
= suid
;
1748 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_EUID
| ARG_RUID
| ARG_SUID
);
1752 audit_arg_groupset(const gid_t
*gidset
, u_int gidset_size
)
1755 struct kaudit_record
*ar
;
1761 for (i
= 0; i
< gidset_size
; i
++)
1762 ar
->k_ar
.ar_arg_groups
.gidset
[i
] = gidset
[i
];
1763 ar
->k_ar
.ar_arg_groups
.gidset_size
= gidset_size
;
1764 ar
->k_ar
.ar_valid_arg
|= ARG_GROUPSET
;
1768 audit_arg_login(const char *login
)
1770 struct kaudit_record
*ar
;
1778 * XXX: Add strlcpy() to Darwin for improved safety.
1780 strlcpy(ar
->k_ar
.ar_arg_login
, login
, MAXLOGNAME
);
1782 strcpy(ar
->k_ar
.ar_arg_login
, login
);
1785 ar
->k_ar
.ar_valid_arg
|= ARG_LOGIN
;
1789 audit_arg_ctlname(const int *name
, int namelen
)
1791 struct kaudit_record
*ar
;
1797 bcopy(name
, &ar
->k_ar
.ar_arg_ctlname
, namelen
* sizeof(int));
1798 ar
->k_ar
.ar_arg_len
= namelen
;
1799 ar
->k_ar
.ar_valid_arg
|= (ARG_CTLNAME
| ARG_LEN
);
1803 audit_arg_mask(int mask
)
1805 struct kaudit_record
*ar
;
1811 ar
->k_ar
.ar_arg_mask
= mask
;
1812 ar
->k_ar
.ar_valid_arg
|= ARG_MASK
;
1816 audit_arg_mode(mode_t mode
)
1818 struct kaudit_record
*ar
;
1824 ar
->k_ar
.ar_arg_mode
= mode
;
1825 ar
->k_ar
.ar_valid_arg
|= ARG_MODE
;
1829 audit_arg_dev(int dev
)
1831 struct kaudit_record
*ar
;
1837 ar
->k_ar
.ar_arg_dev
= dev
;
1838 ar
->k_ar
.ar_valid_arg
|= ARG_DEV
;
1842 audit_arg_value(long value
)
1844 struct kaudit_record
*ar
;
1850 ar
->k_ar
.ar_arg_value
= value
;
1851 ar
->k_ar
.ar_valid_arg
|= ARG_VALUE
;
1855 audit_arg_owner(uid_t uid
, gid_t gid
)
1857 struct kaudit_record
*ar
;
1863 ar
->k_ar
.ar_arg_uid
= uid
;
1864 ar
->k_ar
.ar_arg_gid
= gid
;
1865 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_GID
);
1869 audit_arg_pid(pid_t pid
)
1871 struct kaudit_record
*ar
;
1877 ar
->k_ar
.ar_arg_pid
= pid
;
1878 ar
->k_ar
.ar_valid_arg
|= ARG_PID
;
1882 audit_arg_process(struct proc
*p
)
1884 struct kaudit_record
*ar
;
1887 if ((ar
== NULL
) || (p
== NULL
))
1890 ar
->k_ar
.ar_arg_auid
= p
->p_ucred
->cr_au
.ai_auid
;
1891 ar
->k_ar
.ar_arg_euid
= p
->p_ucred
->cr_uid
;
1892 ar
->k_ar
.ar_arg_egid
= p
->p_ucred
->cr_groups
[0];
1893 ar
->k_ar
.ar_arg_ruid
= p
->p_ucred
->cr_ruid
;
1894 ar
->k_ar
.ar_arg_rgid
= p
->p_ucred
->cr_rgid
;
1895 ar
->k_ar
.ar_arg_asid
= p
->p_ucred
->cr_au
.ai_asid
;
1896 ar
->k_ar
.ar_arg_termid
= p
->p_ucred
->cr_au
.ai_termid
;
1898 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_EUID
| ARG_EGID
| ARG_RUID
|
1899 ARG_RGID
| ARG_ASID
| ARG_TERMID
| ARG_PROCESS
;
1903 audit_arg_signum(u_int signum
)
1905 struct kaudit_record
*ar
;
1911 ar
->k_ar
.ar_arg_signum
= signum
;
1912 ar
->k_ar
.ar_valid_arg
|= ARG_SIGNUM
;
1916 audit_arg_socket(int sodomain
, int sotype
, int soprotocol
)
1919 struct kaudit_record
*ar
;
1925 ar
->k_ar
.ar_arg_sockinfo
.so_domain
= sodomain
;
1926 ar
->k_ar
.ar_arg_sockinfo
.so_type
= sotype
;
1927 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
= soprotocol
;
1928 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
1932 audit_arg_sockaddr(struct proc
*p
, struct sockaddr
*so
)
1934 struct kaudit_record
*ar
;
1937 if (ar
== NULL
|| p
== NULL
|| so
== NULL
)
1940 bcopy(so
, &ar
->k_ar
.ar_arg_sockaddr
, sizeof(ar
->k_ar
.ar_arg_sockaddr
));
1941 switch (so
->sa_family
) {
1943 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET
;
1946 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET6
;
1949 audit_arg_upath(p
, ((struct sockaddr_un
*)so
)->sun_path
,
1951 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRUNIX
;
1957 audit_arg_auid(uid_t auid
)
1959 struct kaudit_record
*ar
;
1965 ar
->k_ar
.ar_arg_auid
= auid
;
1966 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
;
1970 audit_arg_auditinfo(const struct auditinfo
*au_info
)
1972 struct kaudit_record
*ar
;
1978 ar
->k_ar
.ar_arg_auid
= au_info
->ai_auid
;
1979 ar
->k_ar
.ar_arg_asid
= au_info
->ai_asid
;
1980 ar
->k_ar
.ar_arg_amask
.am_success
= au_info
->ai_mask
.am_success
;
1981 ar
->k_ar
.ar_arg_amask
.am_failure
= au_info
->ai_mask
.am_failure
;
1982 ar
->k_ar
.ar_arg_termid
.port
= au_info
->ai_termid
.port
;
1983 ar
->k_ar
.ar_arg_termid
.machine
= au_info
->ai_termid
.machine
;
1984 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_ASID
| ARG_AMASK
| ARG_TERMID
;
1988 audit_arg_text(const char *text
)
1990 struct kaudit_record
*ar
;
1996 /* Invalidate the text string */
1997 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_TEXT
);
2001 if (ar
->k_ar
.ar_arg_text
== NULL
) {
2002 ar
->k_ar
.ar_arg_text
= (char *)kalloc(MAXPATHLEN
);
2003 if (ar
->k_ar
.ar_arg_text
== NULL
)
2007 strncpy(ar
->k_ar
.ar_arg_text
, text
, MAXPATHLEN
);
2008 ar
->k_ar
.ar_valid_arg
|= ARG_TEXT
;
2012 audit_arg_cmd(int cmd
)
2014 struct kaudit_record
*ar
;
2020 ar
->k_ar
.ar_arg_cmd
= cmd
;
2021 ar
->k_ar
.ar_valid_arg
|= ARG_CMD
;
2025 audit_arg_svipc_cmd(int cmd
)
2027 struct kaudit_record
*ar
;
2033 ar
->k_ar
.ar_arg_svipc_cmd
= cmd
;
2034 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_CMD
;
2038 audit_arg_svipc_perm(const struct ipc_perm
*perm
)
2040 struct kaudit_record
*ar
;
2046 bcopy(perm
, &ar
->k_ar
.ar_arg_svipc_perm
,
2047 sizeof(ar
->k_ar
.ar_arg_svipc_perm
));
2048 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_PERM
;
2052 audit_arg_svipc_id(int id
)
2054 struct kaudit_record
*ar
;
2060 ar
->k_ar
.ar_arg_svipc_id
= id
;
2061 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ID
;
2065 audit_arg_svipc_addr(void * addr
)
2067 struct kaudit_record
*ar
;
2073 ar
->k_ar
.ar_arg_svipc_addr
= addr
;
2074 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ADDR
;
2078 audit_arg_posix_ipc_perm(uid_t uid
, gid_t gid
, mode_t mode
)
2080 struct kaudit_record
*ar
;
2086 ar
->k_ar
.ar_arg_pipc_perm
.pipc_uid
= uid
;
2087 ar
->k_ar
.ar_arg_pipc_perm
.pipc_gid
= gid
;
2088 ar
->k_ar
.ar_arg_pipc_perm
.pipc_mode
= mode
;
2089 ar
->k_ar
.ar_valid_arg
|= ARG_POSIX_IPC_PERM
;
2093 audit_arg_auditon(const union auditon_udata
*udata
)
2095 struct kaudit_record
*ar
;
2101 bcopy((const void *)udata
, &ar
->k_ar
.ar_arg_auditon
,
2102 sizeof(ar
->k_ar
.ar_arg_auditon
));
2103 ar
->k_ar
.ar_valid_arg
|= ARG_AUDITON
;
2107 * Audit information about a file, either the file's vnode info, or its
2108 * socket address info.
2111 audit_arg_file(__unused
struct proc
*p
, const struct fileproc
*fp
)
2113 struct kaudit_record
*ar
;
2117 if (fp
->f_fglob
->fg_type
== DTYPE_VNODE
) {
2118 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2122 if (fp
->f_fglob
->fg_type
== DTYPE_SOCKET
) {
2126 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
2127 if (INP_CHECK_SOCKAF(so
, PF_INET
)) {
2128 if (so
->so_pcb
== NULL
)
2130 ar
->k_ar
.ar_arg_sockinfo
.so_type
=
2132 ar
->k_ar
.ar_arg_sockinfo
.so_domain
=
2134 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
=
2135 so
->so_proto
->pr_protocol
;
2136 pcb
= (struct inpcb
*)so
->so_pcb
;
2137 ar
->k_ar
.ar_arg_sockinfo
.so_raddr
=
2138 pcb
->inp_faddr
.s_addr
;
2139 ar
->k_ar
.ar_arg_sockinfo
.so_laddr
=
2140 pcb
->inp_laddr
.s_addr
;
2141 ar
->k_ar
.ar_arg_sockinfo
.so_rport
=
2143 ar
->k_ar
.ar_arg_sockinfo
.so_lport
=
2145 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
2153 * Store a path as given by the user process for auditing into the audit
2154 * record stored on the user thread. This function will allocate the memory to
2155 * store the path info if not already available. This memory will be
2156 * freed when the audit record is freed.
2159 audit_arg_upath(struct proc
*p
, char *upath
, u_int64_t flags
)
2161 struct kaudit_record
*ar
;
2164 if (p
== NULL
|| upath
== NULL
)
2165 return; /* nothing to do! */
2167 if ((flags
& (ARG_UPATH1
| ARG_UPATH2
)) == 0)
2171 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2174 if (flags
& ARG_UPATH1
) {
2175 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH1
);
2176 pathp
= &ar
->k_ar
.ar_arg_upath1
;
2179 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH2
);
2180 pathp
= &ar
->k_ar
.ar_arg_upath2
;
2183 if (*pathp
== NULL
) {
2184 *pathp
= (char *)kalloc(MAXPATHLEN
);
2189 if (canon_path(p
, upath
, *pathp
) == 0) {
2190 if (flags
& ARG_UPATH1
)
2191 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH1
;
2193 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH2
;
2195 kfree(*pathp
, MAXPATHLEN
);
2201 * Function to save the path and vnode attr information into the audit
2204 * It is assumed that the caller will hold any vnode locks necessary to
2205 * perform a VNOP_GETATTR() on the passed vnode.
2207 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but
2208 * always provides access to the generation number as we need that
2209 * to construct the BSM file ID.
2210 * XXX: We should accept the process argument from the caller, since
2211 * it's very likely they already have a reference.
2212 * XXX: Error handling in this function is poor.
2215 audit_arg_vnpath(struct vnode
*vp
, u_int64_t flags
)
2217 struct kaudit_record
*ar
;
2218 struct vnode_attr va
;
2222 struct vnode_au_info
*vnp
;
2224 struct vfs_context context
;
2230 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2233 if ((flags
& (ARG_VNODE1
| ARG_VNODE2
)) == 0)
2238 if (flags
& ARG_VNODE1
) {
2239 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH1
);
2240 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE1
);
2241 pathp
= &ar
->k_ar
.ar_arg_kpath1
;
2242 vnp
= &ar
->k_ar
.ar_arg_vnode1
;
2245 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH2
);
2246 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE2
);
2247 pathp
= &ar
->k_ar
.ar_arg_kpath2
;
2248 vnp
= &ar
->k_ar
.ar_arg_vnode2
;
2251 if (*pathp
== NULL
) {
2252 *pathp
= (char *)kalloc(MAXPATHLEN
);
2258 * If vn_getpath() succeeds, place it in a string buffer
2259 * attached to the audit record, and set a flag indicating
2263 if (vn_getpath(vp
, *pathp
, &len
) == 0) {
2264 if (flags
& ARG_VNODE1
)
2265 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH1
;
2267 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH2
;
2269 kfree(*pathp
, MAXPATHLEN
);
2273 context
.vc_proc
= p
;
2274 context
.vc_ucred
= kauth_cred_get();
2277 VATTR_WANTED(&va
, va_mode
);
2278 VATTR_WANTED(&va
, va_uid
);
2279 VATTR_WANTED(&va
, va_gid
);
2280 VATTR_WANTED(&va
, va_rdev
);
2281 VATTR_WANTED(&va
, va_fsid
);
2282 VATTR_WANTED(&va
, va_fileid
);
2283 VATTR_WANTED(&va
, va_gen
);
2284 error
= vnode_getattr(vp
, &va
, &context
);
2286 /* XXX: How to handle this case? */
2290 /* XXX do we want to fall back here when these aren't supported? */
2291 vnp
->vn_mode
= va
.va_mode
;
2292 vnp
->vn_uid
= va
.va_uid
;
2293 vnp
->vn_gid
= va
.va_gid
;
2294 vnp
->vn_dev
= va
.va_rdev
;
2295 vnp
->vn_fsid
= va
.va_fsid
;
2296 vnp
->vn_fileid
= (u_long
)va
.va_fileid
;
2297 vnp
->vn_gen
= va
.va_gen
;
2298 if (flags
& ARG_VNODE1
)
2299 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE1
;
2301 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE2
;
2306 audit_arg_vnpath_withref(struct vnode
*vp
, u_int64_t flags
)
2308 if (vp
== NULL
|| vnode_getwithref(vp
))
2310 audit_arg_vnpath(vp
, flags
);
2311 (void)vnode_put(vp
);
2315 audit_arg_mach_port1(mach_port_name_t port
)
2317 struct kaudit_record
*ar
;
2323 ar
->k_ar
.ar_arg_mach_port1
= port
;
2324 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT1
;
2328 audit_arg_mach_port2(mach_port_name_t port
)
2330 struct kaudit_record
*ar
;
2336 ar
->k_ar
.ar_arg_mach_port2
= port
;
2337 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT2
;
2341 * The close() system call uses it's own audit call to capture the
2342 * path/vnode information because those pieces are not easily obtained
2343 * within the system call itself.
2346 audit_sysclose(struct proc
*p
, int fd
)
2348 struct fileproc
*fp
;
2353 if (fp_getfvp(p
, fd
, &fp
, &vp
) != 0)
2356 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2369 audit_shutdown(void)
2375 audit(struct proc
*p
, struct audit_args
*uap
, register_t
*retval
)
2381 auditon(struct proc
*p
, struct auditon_args
*uap
, register_t
*retval
)
2387 getauid(struct proc
*p
, struct getauid_args
*uap
, register_t
*retval
)
2393 setauid(struct proc
*p
, struct setauid_args
*uap
, register_t
*retval
)
2399 getaudit(struct proc
*p
, struct getaudit_args
*uap
, register_t
*retval
)
2405 setaudit(struct proc
*p
, struct setaudit_args
*uap
, register_t
*retval
)
2411 getaudit_addr(struct proc
*p
, struct getaudit_addr_args
*uap
, register_t
*retval
)
2417 setaudit_addr(struct proc
*p
, struct setaudit_addr_args
*uap
, register_t
*retval
)
2423 auditctl(struct proc
*p
, struct auditctl_args
*uap
, register_t
*retval
)