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
= NULL
;
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_rele(old_cred
);
485 AUDIT_PRINTF(("Audit file closed\n"));
487 if (audit_vp
!= NULL
) {
488 AUDIT_PRINTF(("Opening new audit file\n"));
490 if (release_funnel
) {
491 thread_funnel_set(kernel_flock
, FALSE
);
492 mutex_lock(audit_mtx
);
494 do_replacement_signal
= 1;
497 * Signal that replacement have occurred to wake up and
498 * start any other replacements started in parallel. We can
499 * continue about our business in the mean time. We
500 * broadcast so that both new replacements can be inserted,
501 * but also so that the source(s) of replacement can return
504 if (do_replacement_signal
)
505 wait_queue_wakeup_all(audit_wait_queue
,
506 AUDIT_REPLACEMENT_EVENT
, THREAD_AWAKENED
);
509 * Next, check to see if we have any records to drain into
510 * the vnode. If not, go back to waiting for an event.
512 if (TAILQ_EMPTY(&audit_q
)) {
515 AUDIT_PRINTF(("audit_worker waiting\n"));
516 ret
= wait_queue_assert_wait(audit_wait_queue
,
520 mutex_unlock(audit_mtx
);
522 assert(ret
== THREAD_WAITING
);
523 ret
= thread_block(THREAD_CONTINUE_NULL
);
524 assert(ret
== THREAD_AWAKENED
);
525 AUDIT_PRINTF(("audit_worker woken up\n"));
526 AUDIT_PRINTF(("audit_worker: new vp = %p; value of flag %d\n",
527 audit_replacement_vp
, audit_replacement_flag
));
529 mutex_lock(audit_mtx
);
534 * If we have records, but there's no active vnode to
535 * write to, drain the record queue. Generally, we
536 * prevent the unnecessary allocation of records
537 * elsewhere, but we need to allow for races between
538 * conditional allocation and queueing. Go back to
539 * waiting when we're done.
541 * XXX: We go out of our way to avoid calling audit_free()
542 * with the audit_mtx held, to avoid a lock order reversal
543 * as free() may grab the funnel. This will be fixed at
546 if (audit_vp
== NULL
) {
547 while ((ar
= TAILQ_FIRST(&audit_q
))) {
548 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
550 if (audit_q_len
<= audit_qctrl
.aq_lowater
)
551 wait_queue_wakeup_one(
556 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
558 mutex_unlock(audit_mtx
);
559 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
560 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
563 mutex_lock(audit_mtx
);
568 * We have both records to write, and an active vnode
569 * to write to. Dequeue a record, and start the write.
570 * Eventually, it might make sense to dequeue several
571 * records and perform our own clustering, if the lower
572 * layers aren't doing it automatically enough.
574 * XXX: We go out of our way to avoid calling audit_free()
575 * with the audit_mtx held, to avoid a lock order reversal
576 * as free() may grab the funnel. This will be fixed at
579 while ((ar
= TAILQ_FIRST(&audit_q
))) {
580 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
582 if (audit_q_len
<= audit_qctrl
.aq_lowater
) {
583 wait_queue_wakeup_one(audit_wait_queue
,
584 AUDIT_COMMIT_EVENT
, THREAD_AWAKENED
);
587 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
589 mutex_unlock(audit_mtx
);
591 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
592 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
593 if (audit_vp
!= NULL
) {
595 * XXX: What should happen if there's a write
598 if (!release_funnel
) {
599 thread_funnel_set(kernel_flock
, TRUE
);
602 error
= audit_write(audit_vp
, ar
, audit_cred
,
604 if (error
&& audit_panic_on_write_fail
) {
605 panic("audit_worker: write error %d\n",
608 printf("audit_worker: write error %d\n",
615 thread_funnel_set(kernel_flock
, FALSE
);
616 mutex_lock(audit_mtx
);
624 /* Verify that the syscall to audit event table is the same
625 * size as the system call table.
627 if (nsys_au_event
!= nsysent
) {
628 printf("Security auditing service initialization failed, ");
629 printf("audit event table doesn't match syscall table.\n");
633 printf("Security auditing service present\n");
634 TAILQ_INIT(&audit_q
);
638 audit_replacement_cred
= NULL
;
639 audit_replacement_flag
= 0;
640 audit_file_rotate_wait
= 0;
641 audit_replacement_vp
= NULL
;
642 audit_fstat
.af_filesz
= 0; /* '0' means unset, unbounded */
643 audit_fstat
.af_currsz
= 0;
644 audit_qctrl
.aq_hiwater
= AQ_HIWATER
;
645 audit_qctrl
.aq_lowater
= AQ_LOWATER
;
646 audit_qctrl
.aq_bufsz
= AQ_BUFSZ
;
647 audit_qctrl
.aq_minfree
= AU_FS_MINFREE
;
649 audit_mtx
= mutex_alloc(0);
650 audit_wait_queue
= wait_queue_alloc(SYNC_POLICY_FIFO
);
651 audit_zone
= zinit(sizeof(struct kaudit_record
),
652 AQ_HIWATER
*sizeof(struct kaudit_record
),
656 /* Initialize the BSM audit subsystem. */
661 audit_rotate_vnode(kauth_cred_t cred
, struct vnode
*vp
)
666 * If other parallel log replacements have been requested, we wait
667 * until they've finished before continuing.
669 mutex_lock(audit_mtx
);
670 while (audit_replacement_flag
!= 0) {
672 AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for "
674 ret
= wait_queue_assert_wait(audit_wait_queue
,
675 AUDIT_REPLACEMENT_EVENT
,
678 mutex_unlock(audit_mtx
);
680 assert(ret
== THREAD_WAITING
);
681 ret
= thread_block(THREAD_CONTINUE_NULL
);
682 assert(ret
== THREAD_AWAKENED
);
683 AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n",
684 audit_replacement_flag
));
686 mutex_lock(audit_mtx
);
688 audit_replacement_cred
= cred
;
689 audit_replacement_flag
= 1;
690 audit_replacement_vp
= vp
;
693 * Start or wake up the audit worker to perform the exchange.
694 * It will have to wait until we release the mutex.
696 if (audit_worker_thread
== THREAD_NULL
)
697 audit_worker_thread
= kernel_thread(kernel_task
,
700 wait_queue_wakeup_one(audit_wait_queue
,
705 * Wait for the audit_worker to broadcast that a replacement has
706 * taken place; we know that once this has happened, our vnode
707 * has been replaced in, so we can return successfully.
709 AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of "
711 ret
= wait_queue_assert_wait(audit_wait_queue
,
712 AUDIT_REPLACEMENT_EVENT
,
715 mutex_unlock(audit_mtx
);
717 assert(ret
== THREAD_WAITING
);
718 ret
= thread_block(THREAD_CONTINUE_NULL
);
719 assert(ret
== THREAD_AWAKENED
);
720 AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by "
721 "audit_worker (flag " "now %d)\n", audit_replacement_flag
));
723 audit_file_rotate_wait
= 0; /* We can now request another rotation */
727 * Drain the audit queue and close the log at shutdown.
732 audit_rotate_vnode(NULL
, NULL
);
735 static __inline__
struct uthread
*
738 return (get_bsdthread_info(current_thread()));
741 static __inline__
struct kaudit_record
*
744 return (curuthread()->uu_ar
);
747 /**********************************
748 * Begin system calls. *
749 **********************************/
751 * System call to allow a user space application to submit a BSM audit
752 * record to the kernel for inclusion in the audit log. This function
753 * does little verification on the audit record that is submitted.
755 * XXXAUDIT: Audit preselection for user records does not currently
756 * work, since we pre-select only based on the AUE_audit event type,
757 * not the event type submitted as part of the user audit data.
761 audit(struct proc
*p
, struct audit_args
*uap
, __unused register_t
*retval
)
765 struct kaudit_record
*ar
;
766 struct uthread
*uthr
;
768 error
= suser(kauth_cred_get(), &p
->p_acflag
);
772 if ((uap
->length
<= 0) || (uap
->length
> (int)audit_qctrl
.aq_bufsz
))
777 /* If there's no current audit record (audit() itself not audited)
778 * commit the user audit record.
782 if (uthr
== NULL
) /* can this happen? */
785 /* This is not very efficient; we're required to allocate
786 * a complete kernel audit record just so the user record
789 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
790 if (uthr
->uu_ar
== NULL
) /* auditing not on, or memory error */
795 if (uap
->length
> MAX_AUDIT_RECORD_SIZE
)
798 rec
= (void *)kalloc((vm_size_t
)uap
->length
);
800 error
= copyin(uap
->record
, rec
, uap
->length
);
804 /* Verify the record */
805 if (bsm_rec_verify(rec
) == 0) {
810 /* Attach the user audit record to the kernel audit record. Because
811 * this system call is an auditable event, we will write the user
812 * record along with the record for this audit event.
815 ar
->k_ar_commit
|= AR_COMMIT_USER
;
816 ar
->k_ulen
= uap
->length
;
820 /* audit_syscall_exit() will free the audit record on the thread
821 * even if we allocated it above.
823 kfree(rec
, uap
->length
);
828 * System call to manipulate auditing.
832 auditon(struct proc
*p
, __unused
struct auditon_args
*uap
, __unused register_t
*retval
)
836 union auditon_udata udata
;
839 AUDIT_ARG(cmd
, uap
->cmd
);
840 ret
= suser(kauth_cred_get(), &p
->p_acflag
);
845 if ((len
<= 0) || (len
> (int)sizeof(union auditon_udata
)))
848 memset((void *)&udata
, 0, sizeof(udata
));
851 /* Some of the GET commands use the arguments too */
865 case A_GETPINFO_ADDR
:
866 ret
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
869 AUDIT_ARG(auditon
, &udata
);
873 /* XXX Need to implement these commands by accessing the global
874 * values associated with the commands.
878 if (!audit_fail_stop
)
879 udata
.au_policy
|= AUDIT_CNT
;
880 if (audit_panic_on_write_fail
)
881 udata
.au_policy
|= AUDIT_AHLT
;
884 if (udata
.au_policy
& ~(AUDIT_CNT
|AUDIT_AHLT
))
887 * XXX - Need to wake up waiters if the policy relaxes?
889 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
890 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
893 udata
.au_mask
= audit_nae_mask
;
896 audit_nae_mask
= udata
.au_mask
;
899 udata
.au_qctrl
= audit_qctrl
;
902 if ((udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
903 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
904 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
905 (udata
.au_qctrl
.aq_minfree
< 0) ||
906 (udata
.au_qctrl
.aq_minfree
> 100))
909 audit_qctrl
= udata
.au_qctrl
;
910 /* XXX The queue delay value isn't used with the kernel. */
911 audit_qctrl
.aq_delay
= -1;
932 if (audit_enabled
&& !audit_suspended
)
933 udata
.au_cond
= AUC_AUDITING
;
935 udata
.au_cond
= AUC_NOAUDIT
;
938 if (udata
.au_cond
== AUC_NOAUDIT
)
940 if (udata
.au_cond
== AUC_AUDITING
)
942 if (udata
.au_cond
== AUC_DISABLED
) {
948 udata
.au_evclass
.ec_class
=
949 au_event_class(udata
.au_evclass
.ec_number
);
952 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
953 udata
.au_evclass
.ec_class
);
956 if (udata
.au_aupinfo
.ap_pid
< 1)
958 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
961 udata
.au_aupinfo
.ap_auid
= tp
->p_ucred
->cr_au
.ai_auid
;
962 udata
.au_aupinfo
.ap_mask
.am_success
=
963 tp
->p_ucred
->cr_au
.ai_mask
.am_success
;
964 udata
.au_aupinfo
.ap_mask
.am_failure
=
965 tp
->p_ucred
->cr_au
.ai_mask
.am_failure
;
966 udata
.au_aupinfo
.ap_termid
.machine
=
967 tp
->p_ucred
->cr_au
.ai_termid
.machine
;
968 udata
.au_aupinfo
.ap_termid
.port
=
969 tp
->p_ucred
->cr_au
.ai_termid
.port
;
970 udata
.au_aupinfo
.ap_asid
= tp
->p_ucred
->cr_au
.ai_asid
;
973 if (udata
.au_aupinfo
.ap_pid
< 1)
975 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
979 * we are modifying the audit info in a credential so we need a new
980 * credential (or take another reference on an existing credential that
981 * matches our new one). We must do this because the audit info in the
982 * credential is used as part of our hash key. Get current credential
983 * in the target process and take a reference while we muck with it.
986 kauth_cred_t my_cred
, my_new_cred
;
987 struct auditinfo temp_auditinfo
;
989 my_cred
= kauth_cred_proc_ref(tp
);
991 * set the credential with new info. If there is no change we get back
992 * the same credential we passed in.
994 temp_auditinfo
= my_cred
->cr_au
;
995 temp_auditinfo
.ai_mask
.am_success
=
996 udata
.au_aupinfo
.ap_mask
.am_success
;
997 temp_auditinfo
.ai_mask
.am_failure
=
998 udata
.au_aupinfo
.ap_mask
.am_failure
;
999 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1001 if (my_cred
!= my_new_cred
) {
1003 /* need to protect for a race where another thread also changed
1004 * the credential after we took our reference. If p_ucred has
1005 * changed then we should restart this again with the new cred.
1007 if (tp
->p_ucred
!= my_cred
) {
1009 kauth_cred_rele(my_cred
);
1010 kauth_cred_rele(my_new_cred
);
1014 tp
->p_ucred
= my_new_cred
;
1017 /* drop our extra reference */
1018 kauth_cred_rele(my_cred
);
1023 if ((udata
.au_fstat
.af_filesz
!= 0) &&
1024 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
))
1026 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
1029 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
1030 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
1032 case A_GETPINFO_ADDR
:
1042 /* Copy data back to userspace for the GET comands */
1054 case A_GETPINFO_ADDR
:
1056 ret
= copyout((void *)&udata
, uap
->data
, uap
->length
);
1066 * System calls to manage the user audit information.
1067 * XXXAUDIT May need to lock the proc structure.
1071 getauid(struct proc
*p
, struct getauid_args
*uap
, __unused register_t
*retval
)
1075 error
= copyout((void *)&kauth_cred_get()->cr_au
.ai_auid
,
1076 uap
->auid
, sizeof(au_id_t
));
1085 setauid(struct proc
*p
, struct setauid_args
*uap
, __unused register_t
*retval
)
1090 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1094 error
= copyin(uap
->auid
,
1095 (void *)&temp_au_id
,
1101 * we are modifying the audit info in a credential so we need a new
1102 * credential (or take another reference on an existing credential that
1103 * matches our new one). We must do this because the audit info in the
1104 * credential is used as part of our hash key. Get current credential
1105 * in the target process and take a reference while we muck with it.
1108 kauth_cred_t my_cred
, my_new_cred
;
1109 struct auditinfo temp_auditinfo
;
1111 my_cred
= kauth_cred_proc_ref(p
);
1113 * set the credential with new info. If there is no change we get back
1114 * the same credential we passed in.
1116 temp_auditinfo
= my_cred
->cr_au
;
1117 temp_auditinfo
.ai_auid
= temp_au_id
;
1118 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1120 if (my_cred
!= my_new_cred
) {
1122 /* need to protect for a race where another thread also changed
1123 * the credential after we took our reference. If p_ucred has
1124 * changed then we should restart this again with the new cred.
1126 if (p
->p_ucred
!= my_cred
) {
1128 kauth_cred_rele(my_cred
);
1129 kauth_cred_rele(my_new_cred
);
1133 p
->p_ucred
= my_new_cred
;
1136 /* drop our extra reference */
1137 kauth_cred_rele(my_cred
);
1141 /* propagate the change from the process to Mach task */
1142 set_security_token(p
);
1144 audit_arg_auid(kauth_cred_get()->cr_au
.ai_auid
);
1149 * System calls to get and set process audit information.
1150 * If the caller is privileged, they get the whole set of
1151 * audit information. Otherwise, the real audit mask is
1152 * filtered out - but the rest of the information is
1157 getaudit(struct proc
*p
, struct getaudit_args
*uap
, __unused register_t
*retval
)
1159 struct auditinfo ai
;
1162 ai
= kauth_cred_get()->cr_au
;
1164 /* only superuser gets to see the real mask */
1165 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1167 ai
.ai_mask
.am_success
= ~0;
1168 ai
.ai_mask
.am_failure
= ~0;
1171 error
= copyout(&ai
, uap
->auditinfo
, sizeof(ai
));
1180 setaudit(struct proc
*p
, struct setaudit_args
*uap
, __unused register_t
*retval
)
1183 struct auditinfo temp_auditinfo
;
1185 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1189 error
= copyin(uap
->auditinfo
,
1190 (void *)&temp_auditinfo
,
1191 sizeof(temp_auditinfo
));
1196 * we are modifying the audit info in a credential so we need a new
1197 * credential (or take another reference on an existing credential that
1198 * matches our new one). We must do this because the audit info in the
1199 * credential is used as part of our hash key. Get current credential
1200 * in the target process and take a reference while we muck with it.
1203 kauth_cred_t my_cred
, my_new_cred
;
1205 my_cred
= kauth_cred_proc_ref(p
);
1207 * set the credential with new info. If there is no change we get back
1208 * the same credential we passed in.
1210 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1212 if (my_cred
!= my_new_cred
) {
1214 /* need to protect for a race where another thread also changed
1215 * the credential after we took our reference. If p_ucred has
1216 * changed then we should restart this again with the new cred.
1218 if (p
->p_ucred
!= my_cred
) {
1220 kauth_cred_rele(my_cred
);
1221 kauth_cred_rele(my_new_cred
);
1225 p
->p_ucred
= my_new_cred
;
1228 /* drop our extra reference */
1229 kauth_cred_rele(my_cred
);
1233 /* propagate the change from the process to Mach task */
1234 set_security_token(p
);
1236 audit_arg_auditinfo(&p
->p_ucred
->cr_au
);
1243 getaudit_addr(struct proc
*p
, __unused
struct getaudit_addr_args
*uap
, __unused register_t
*retval
)
1250 setaudit_addr(struct proc
*p
, __unused
struct setaudit_addr_args
*uap
, __unused register_t
*retval
)
1254 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1261 * Syscall to manage audit files.
1266 auditctl(struct proc
*p
, struct auditctl_args
*uap
, __unused register_t
*retval
)
1268 struct nameidata nd
;
1272 struct vfs_context context
;
1274 context
.vc_proc
= p
;
1275 context
.vc_ucred
= kauth_cred_get();
1277 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1285 * If a path is specified, open the replacement vnode, perform
1286 * validity checks, and grab another reference to the current
1289 if (uap
->path
!= 0) {
1290 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1291 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1292 uap
->path
, &context
);
1293 flags
= audit_open_flags
;
1294 error
= vn_open(&nd
, flags
, 0);
1298 if (vp
->v_type
!= VREG
) {
1299 vn_close(vp
, audit_close_flags
, kauth_cred_get(), p
);
1304 cred
= kauth_cred_get_with_ref();
1305 audit_suspended
= 0;
1308 * a vp and cred of NULL is valid at this point
1309 * and indicates we're to turn off auditing...
1311 audit_rotate_vnode(cred
, vp
);
1318 /**********************************
1319 * End of system calls. *
1320 **********************************/
1325 struct kaudit_record
*
1326 audit_new(int event
, struct proc
*p
, __unused
struct uthread
*uthread
)
1328 struct kaudit_record
*ar
;
1332 * Eventually, there may be certain classes of events that
1333 * we will audit regardless of the audit state at the time
1334 * the record is created. These events will generally
1335 * correspond to changes in the audit state. The dummy
1336 * code below is from our first prototype, but may also
1337 * be used in the final version (with modified event numbers).
1340 if (event
!= AUDIT_EVENT_FILESTOP
&& event
!= AUDIT_EVENT_FILESTART
) {
1342 mutex_lock(audit_mtx
);
1343 no_record
= (audit_suspended
|| !audit_enabled
);
1344 mutex_unlock(audit_mtx
);
1352 * Initialize the audit record header.
1353 * XXX: We may want to fail-stop if allocation fails.
1354 * XXX: The number of outstanding uncommitted audit records is
1355 * limited by the number of concurrent threads servicing system
1356 * calls in the kernel.
1359 ar
= (struct kaudit_record
*)zalloc(audit_zone
);
1363 mutex_lock(audit_mtx
);
1365 mutex_unlock(audit_mtx
);
1367 bzero(ar
, sizeof(*ar
));
1368 ar
->k_ar
.ar_magic
= AUDIT_RECORD_MAGIC
;
1369 ar
->k_ar
.ar_event
= event
;
1370 nanotime(&ar
->k_ar
.ar_starttime
);
1372 /* Export the subject credential. */
1373 cru2x(p
->p_ucred
, &ar
->k_ar
.ar_subj_cred
);
1374 ar
->k_ar
.ar_subj_ruid
= p
->p_ucred
->cr_ruid
;
1375 ar
->k_ar
.ar_subj_rgid
= p
->p_ucred
->cr_rgid
;
1376 ar
->k_ar
.ar_subj_egid
= p
->p_ucred
->cr_groups
[0];
1377 ar
->k_ar
.ar_subj_auid
= p
->p_ucred
->cr_au
.ai_auid
;
1378 ar
->k_ar
.ar_subj_asid
= p
->p_ucred
->cr_au
.ai_asid
;
1379 ar
->k_ar
.ar_subj_pid
= p
->p_pid
;
1380 ar
->k_ar
.ar_subj_amask
= p
->p_ucred
->cr_au
.ai_mask
;
1381 ar
->k_ar
.ar_subj_term
= p
->p_ucred
->cr_au
.ai_termid
;
1382 bcopy(p
->p_comm
, ar
->k_ar
.ar_subj_comm
, MAXCOMLEN
);
1389 * XXXAUDIT: So far, this is unused, and should probably be GC'd.
1392 audit_abort(struct kaudit_record
*ar
)
1394 mutex_lock(audit_mtx
);
1396 mutex_unlock(audit_mtx
);
1404 audit_commit(struct kaudit_record
*ar
, int error
, int retval
)
1408 struct au_mask
*aumask
;
1414 * Decide whether to commit the audit record by checking the
1415 * error value from the system call and using the appropriate
1418 if (ar
->k_ar
.ar_subj_auid
== AU_DEFAUDITID
)
1419 aumask
= &audit_nae_mask
;
1421 aumask
= &ar
->k_ar
.ar_subj_amask
;
1424 sorf
= AU_PRS_FAILURE
;
1426 sorf
= AU_PRS_SUCCESS
;
1428 switch(ar
->k_ar
.ar_event
) {
1431 /* The open syscall always writes a OPEN_RWTC event; limit the
1432 * to the proper type of event based on the flags and the error
1435 ar
->k_ar
.ar_event
= flags_and_error_to_openevent(ar
->k_ar
.ar_arg_fflags
, error
);
1439 ar
->k_ar
.ar_event
= ctlname_to_sysctlevent(ar
->k_ar
.ar_arg_ctlname
, ar
->k_ar
.ar_valid_arg
);
1443 /* Convert the auditon() command to an event */
1444 ar
->k_ar
.ar_event
= auditon_command_event(ar
->k_ar
.ar_arg_cmd
);
1448 if (au_preselect(ar
->k_ar
.ar_event
, aumask
, sorf
) != 0)
1449 ar
->k_ar_commit
|= AR_COMMIT_KERNEL
;
1451 if ((ar
->k_ar_commit
& (AR_COMMIT_USER
| AR_COMMIT_KERNEL
)) == 0) {
1452 mutex_lock(audit_mtx
);
1454 mutex_unlock(audit_mtx
);
1459 ar
->k_ar
.ar_errno
= error
;
1460 ar
->k_ar
.ar_retval
= retval
;
1463 * We might want to do some system-wide post-filtering
1464 * here at some point.
1468 * Timestamp system call end.
1470 nanotime(&ar
->k_ar
.ar_endtime
);
1472 mutex_lock(audit_mtx
);
1474 * Note: it could be that some records initiated while audit was
1475 * enabled should still be committed?
1477 if (audit_suspended
|| !audit_enabled
) {
1479 mutex_unlock(audit_mtx
);
1485 * Constrain the number of committed audit records based on
1486 * the configurable parameter.
1488 while (audit_q_len
>= audit_qctrl
.aq_hiwater
) {
1490 ret
= wait_queue_assert_wait(audit_wait_queue
,
1494 mutex_unlock(audit_mtx
);
1496 assert(ret
== THREAD_WAITING
);
1498 ret
= thread_block(THREAD_CONTINUE_NULL
);
1499 assert(ret
== THREAD_AWAKENED
);
1500 mutex_lock(audit_mtx
);
1503 TAILQ_INSERT_TAIL(&audit_q
, ar
, k_q
);
1506 wait_queue_wakeup_one(audit_wait_queue
, AUDIT_WORKER_EVENT
, THREAD_AWAKENED
);
1507 mutex_unlock(audit_mtx
);
1511 * Calls to set up and tear down audit structures associated with
1515 audit_syscall_enter(unsigned short code
, struct proc
*proc
,
1516 struct uthread
*uthread
)
1519 struct au_mask
*aumask
;
1521 audit_event
= sys_au_event
[code
];
1522 if (audit_event
== AUE_NULL
)
1525 assert(uthread
->uu_ar
== NULL
);
1527 /* Check which audit mask to use; either the kernel non-attributable
1528 * event mask or the process audit mask.
1530 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1531 aumask
= &audit_nae_mask
;
1533 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1536 * Allocate an audit record, if preselection allows it, and store
1537 * in the BSD thread for later use.
1539 if (au_preselect(audit_event
, aumask
,
1540 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1542 * If we're out of space and need to suspend unprivileged
1543 * processes, do that here rather than trying to allocate
1544 * another audit record.
1546 if (audit_in_failure
&&
1547 suser(kauth_cred_get(), &proc
->p_acflag
) != 0) {
1550 assert(audit_worker_thread
!= THREAD_NULL
);
1551 ret
= wait_queue_assert_wait(audit_wait_queue
,
1552 AUDIT_FAILURE_EVENT
, THREAD_UNINT
, 0);
1553 assert(ret
== THREAD_WAITING
);
1554 (void)thread_block(THREAD_CONTINUE_NULL
);
1555 panic("audit_failing_stop: thread continued");
1557 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1559 uthread
->uu_ar
= NULL
;
1564 audit_syscall_exit(int error
, AUDIT_PRINTF_ONLY
struct proc
*proc
, struct uthread
*uthread
)
1569 * Commit the audit record as desired; once we pass the record
1570 * into audit_commit(), the memory is owned by the audit
1572 * The return value from the system call is stored on the user
1573 * thread. If there was an error, the return value is set to -1,
1574 * imitating the behavior of the cerror routine.
1579 retval
= uthread
->uu_rval
[0];
1581 audit_commit(uthread
->uu_ar
, error
, retval
);
1582 if (uthread
->uu_ar
!= NULL
) {
1583 AUDIT_PRINTF(("audit record committed by pid %d\n", proc
->p_pid
));
1585 uthread
->uu_ar
= NULL
;
1590 * Calls to set up and tear down audit structures used during Mach
1594 audit_mach_syscall_enter(unsigned short audit_event
)
1596 struct uthread
*uthread
;
1598 struct au_mask
*aumask
;
1600 if (audit_event
== AUE_NULL
)
1603 uthread
= curuthread();
1604 if (uthread
== NULL
)
1607 proc
= current_proc();
1611 assert(uthread
->uu_ar
== NULL
);
1613 /* Check which audit mask to use; either the kernel non-attributable
1614 * event mask or the process audit mask.
1616 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1617 aumask
= &audit_nae_mask
;
1619 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1622 * Allocate an audit record, if desired, and store in the BSD
1623 * thread for later use.
1625 if (au_preselect(audit_event
, aumask
,
1626 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1627 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1629 uthread
->uu_ar
= NULL
;
1634 audit_mach_syscall_exit(int retval
, struct uthread
*uthread
)
1636 /* The error code from Mach system calls is the same as the
1639 /* XXX Is the above statement always true? */
1640 audit_commit(uthread
->uu_ar
, retval
, retval
);
1641 uthread
->uu_ar
= NULL
;
1646 * Calls to manipulate elements of the audit record structure from system
1647 * call code. Macro wrappers will prevent this functions from being
1648 * entered if auditing is disabled, avoiding the function call cost. We
1649 * check the thread audit record pointer anyway, as the audit condition
1650 * could change, and pre-selection may not have allocated an audit
1651 * record for this event.
1654 audit_arg_addr(user_addr_t addr
)
1656 struct kaudit_record
*ar
;
1662 ar
->k_ar
.ar_arg_addr
= CAST_DOWN(void *, addr
); /* XXX */
1663 ar
->k_ar
.ar_valid_arg
|= ARG_ADDR
;
1667 audit_arg_len(user_size_t len
)
1669 struct kaudit_record
*ar
;
1675 ar
->k_ar
.ar_arg_len
= CAST_DOWN(int, len
); /* XXX */
1676 ar
->k_ar
.ar_valid_arg
|= ARG_LEN
;
1680 audit_arg_fd(int fd
)
1682 struct kaudit_record
*ar
;
1688 ar
->k_ar
.ar_arg_fd
= fd
;
1689 ar
->k_ar
.ar_valid_arg
|= ARG_FD
;
1693 audit_arg_fflags(int fflags
)
1695 struct kaudit_record
*ar
;
1701 ar
->k_ar
.ar_arg_fflags
= fflags
;
1702 ar
->k_ar
.ar_valid_arg
|= ARG_FFLAGS
;
1706 audit_arg_gid(gid_t gid
, gid_t egid
, gid_t rgid
, gid_t sgid
)
1708 struct kaudit_record
*ar
;
1714 ar
->k_ar
.ar_arg_gid
= gid
;
1715 ar
->k_ar
.ar_arg_egid
= egid
;
1716 ar
->k_ar
.ar_arg_rgid
= rgid
;
1717 ar
->k_ar
.ar_arg_sgid
= sgid
;
1718 ar
->k_ar
.ar_valid_arg
|= (ARG_GID
| ARG_EGID
| ARG_RGID
| ARG_SGID
);
1722 audit_arg_uid(uid_t uid
, uid_t euid
, uid_t ruid
, uid_t suid
)
1724 struct kaudit_record
*ar
;
1730 ar
->k_ar
.ar_arg_uid
= uid
;
1731 ar
->k_ar
.ar_arg_euid
= euid
;
1732 ar
->k_ar
.ar_arg_ruid
= ruid
;
1733 ar
->k_ar
.ar_arg_suid
= suid
;
1734 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_EUID
| ARG_RUID
| ARG_SUID
);
1738 audit_arg_groupset(const gid_t
*gidset
, u_int gidset_size
)
1741 struct kaudit_record
*ar
;
1747 for (i
= 0; i
< gidset_size
; i
++)
1748 ar
->k_ar
.ar_arg_groups
.gidset
[i
] = gidset
[i
];
1749 ar
->k_ar
.ar_arg_groups
.gidset_size
= gidset_size
;
1750 ar
->k_ar
.ar_valid_arg
|= ARG_GROUPSET
;
1754 audit_arg_login(const char *login
)
1756 struct kaudit_record
*ar
;
1764 * XXX: Add strlcpy() to Darwin for improved safety.
1766 strlcpy(ar
->k_ar
.ar_arg_login
, login
, MAXLOGNAME
);
1768 strcpy(ar
->k_ar
.ar_arg_login
, login
);
1771 ar
->k_ar
.ar_valid_arg
|= ARG_LOGIN
;
1775 audit_arg_ctlname(const int *name
, int namelen
)
1777 struct kaudit_record
*ar
;
1783 bcopy(name
, &ar
->k_ar
.ar_arg_ctlname
, namelen
* sizeof(int));
1784 ar
->k_ar
.ar_arg_len
= namelen
;
1785 ar
->k_ar
.ar_valid_arg
|= (ARG_CTLNAME
| ARG_LEN
);
1789 audit_arg_mask(int mask
)
1791 struct kaudit_record
*ar
;
1797 ar
->k_ar
.ar_arg_mask
= mask
;
1798 ar
->k_ar
.ar_valid_arg
|= ARG_MASK
;
1802 audit_arg_mode(mode_t mode
)
1804 struct kaudit_record
*ar
;
1810 ar
->k_ar
.ar_arg_mode
= mode
;
1811 ar
->k_ar
.ar_valid_arg
|= ARG_MODE
;
1815 audit_arg_dev(int dev
)
1817 struct kaudit_record
*ar
;
1823 ar
->k_ar
.ar_arg_dev
= dev
;
1824 ar
->k_ar
.ar_valid_arg
|= ARG_DEV
;
1828 audit_arg_value(long value
)
1830 struct kaudit_record
*ar
;
1836 ar
->k_ar
.ar_arg_value
= value
;
1837 ar
->k_ar
.ar_valid_arg
|= ARG_VALUE
;
1841 audit_arg_owner(uid_t uid
, gid_t gid
)
1843 struct kaudit_record
*ar
;
1849 ar
->k_ar
.ar_arg_uid
= uid
;
1850 ar
->k_ar
.ar_arg_gid
= gid
;
1851 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_GID
);
1855 audit_arg_pid(pid_t pid
)
1857 struct kaudit_record
*ar
;
1863 ar
->k_ar
.ar_arg_pid
= pid
;
1864 ar
->k_ar
.ar_valid_arg
|= ARG_PID
;
1868 audit_arg_process(struct proc
*p
)
1870 struct kaudit_record
*ar
;
1873 if ((ar
== NULL
) || (p
== NULL
))
1876 ar
->k_ar
.ar_arg_auid
= p
->p_ucred
->cr_au
.ai_auid
;
1877 ar
->k_ar
.ar_arg_euid
= p
->p_ucred
->cr_uid
;
1878 ar
->k_ar
.ar_arg_egid
= p
->p_ucred
->cr_groups
[0];
1879 ar
->k_ar
.ar_arg_ruid
= p
->p_ucred
->cr_ruid
;
1880 ar
->k_ar
.ar_arg_rgid
= p
->p_ucred
->cr_rgid
;
1881 ar
->k_ar
.ar_arg_asid
= p
->p_ucred
->cr_au
.ai_asid
;
1882 ar
->k_ar
.ar_arg_termid
= p
->p_ucred
->cr_au
.ai_termid
;
1884 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_EUID
| ARG_EGID
| ARG_RUID
|
1885 ARG_RGID
| ARG_ASID
| ARG_TERMID
| ARG_PROCESS
;
1889 audit_arg_signum(u_int signum
)
1891 struct kaudit_record
*ar
;
1897 ar
->k_ar
.ar_arg_signum
= signum
;
1898 ar
->k_ar
.ar_valid_arg
|= ARG_SIGNUM
;
1902 audit_arg_socket(int sodomain
, int sotype
, int soprotocol
)
1905 struct kaudit_record
*ar
;
1911 ar
->k_ar
.ar_arg_sockinfo
.so_domain
= sodomain
;
1912 ar
->k_ar
.ar_arg_sockinfo
.so_type
= sotype
;
1913 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
= soprotocol
;
1914 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
1918 audit_arg_sockaddr(struct proc
*p
, struct sockaddr
*so
)
1920 struct kaudit_record
*ar
;
1923 if (ar
== NULL
|| p
== NULL
|| so
== NULL
)
1926 bcopy(so
, &ar
->k_ar
.ar_arg_sockaddr
, sizeof(ar
->k_ar
.ar_arg_sockaddr
));
1927 switch (so
->sa_family
) {
1929 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET
;
1932 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET6
;
1935 audit_arg_upath(p
, ((struct sockaddr_un
*)so
)->sun_path
,
1937 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRUNIX
;
1943 audit_arg_auid(uid_t auid
)
1945 struct kaudit_record
*ar
;
1951 ar
->k_ar
.ar_arg_auid
= auid
;
1952 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
;
1956 audit_arg_auditinfo(const struct auditinfo
*au_info
)
1958 struct kaudit_record
*ar
;
1964 ar
->k_ar
.ar_arg_auid
= au_info
->ai_auid
;
1965 ar
->k_ar
.ar_arg_asid
= au_info
->ai_asid
;
1966 ar
->k_ar
.ar_arg_amask
.am_success
= au_info
->ai_mask
.am_success
;
1967 ar
->k_ar
.ar_arg_amask
.am_failure
= au_info
->ai_mask
.am_failure
;
1968 ar
->k_ar
.ar_arg_termid
.port
= au_info
->ai_termid
.port
;
1969 ar
->k_ar
.ar_arg_termid
.machine
= au_info
->ai_termid
.machine
;
1970 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_ASID
| ARG_AMASK
| ARG_TERMID
;
1974 audit_arg_text(const char *text
)
1976 struct kaudit_record
*ar
;
1982 /* Invalidate the text string */
1983 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_TEXT
);
1987 if (ar
->k_ar
.ar_arg_text
== NULL
) {
1988 ar
->k_ar
.ar_arg_text
= (char *)kalloc(MAXPATHLEN
);
1989 if (ar
->k_ar
.ar_arg_text
== NULL
)
1993 strncpy(ar
->k_ar
.ar_arg_text
, text
, MAXPATHLEN
);
1994 ar
->k_ar
.ar_valid_arg
|= ARG_TEXT
;
1998 audit_arg_cmd(int cmd
)
2000 struct kaudit_record
*ar
;
2006 ar
->k_ar
.ar_arg_cmd
= cmd
;
2007 ar
->k_ar
.ar_valid_arg
|= ARG_CMD
;
2011 audit_arg_svipc_cmd(int cmd
)
2013 struct kaudit_record
*ar
;
2019 ar
->k_ar
.ar_arg_svipc_cmd
= cmd
;
2020 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_CMD
;
2024 audit_arg_svipc_perm(const struct ipc_perm
*perm
)
2026 struct kaudit_record
*ar
;
2032 bcopy(perm
, &ar
->k_ar
.ar_arg_svipc_perm
,
2033 sizeof(ar
->k_ar
.ar_arg_svipc_perm
));
2034 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_PERM
;
2038 audit_arg_svipc_id(int id
)
2040 struct kaudit_record
*ar
;
2046 ar
->k_ar
.ar_arg_svipc_id
= id
;
2047 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ID
;
2051 audit_arg_svipc_addr(void * addr
)
2053 struct kaudit_record
*ar
;
2059 ar
->k_ar
.ar_arg_svipc_addr
= addr
;
2060 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ADDR
;
2064 audit_arg_posix_ipc_perm(uid_t uid
, gid_t gid
, mode_t mode
)
2066 struct kaudit_record
*ar
;
2072 ar
->k_ar
.ar_arg_pipc_perm
.pipc_uid
= uid
;
2073 ar
->k_ar
.ar_arg_pipc_perm
.pipc_gid
= gid
;
2074 ar
->k_ar
.ar_arg_pipc_perm
.pipc_mode
= mode
;
2075 ar
->k_ar
.ar_valid_arg
|= ARG_POSIX_IPC_PERM
;
2079 audit_arg_auditon(const union auditon_udata
*udata
)
2081 struct kaudit_record
*ar
;
2087 bcopy((const void *)udata
, &ar
->k_ar
.ar_arg_auditon
,
2088 sizeof(ar
->k_ar
.ar_arg_auditon
));
2089 ar
->k_ar
.ar_valid_arg
|= ARG_AUDITON
;
2093 * Audit information about a file, either the file's vnode info, or its
2094 * socket address info.
2097 audit_arg_file(__unused
struct proc
*p
, const struct fileproc
*fp
)
2099 struct kaudit_record
*ar
;
2103 if (fp
->f_fglob
->fg_type
== DTYPE_VNODE
) {
2104 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2108 if (fp
->f_fglob
->fg_type
== DTYPE_SOCKET
) {
2112 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
2113 if (INP_CHECK_SOCKAF(so
, PF_INET
)) {
2114 if (so
->so_pcb
== NULL
)
2116 ar
->k_ar
.ar_arg_sockinfo
.so_type
=
2118 ar
->k_ar
.ar_arg_sockinfo
.so_domain
=
2120 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
=
2121 so
->so_proto
->pr_protocol
;
2122 pcb
= (struct inpcb
*)so
->so_pcb
;
2123 ar
->k_ar
.ar_arg_sockinfo
.so_raddr
=
2124 pcb
->inp_faddr
.s_addr
;
2125 ar
->k_ar
.ar_arg_sockinfo
.so_laddr
=
2126 pcb
->inp_laddr
.s_addr
;
2127 ar
->k_ar
.ar_arg_sockinfo
.so_rport
=
2129 ar
->k_ar
.ar_arg_sockinfo
.so_lport
=
2131 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
2139 * Store a path as given by the user process for auditing into the audit
2140 * record stored on the user thread. This function will allocate the memory to
2141 * store the path info if not already available. This memory will be
2142 * freed when the audit record is freed.
2145 audit_arg_upath(struct proc
*p
, char *upath
, u_int64_t flags
)
2147 struct kaudit_record
*ar
;
2150 if (p
== NULL
|| upath
== NULL
)
2151 return; /* nothing to do! */
2153 if ((flags
& (ARG_UPATH1
| ARG_UPATH2
)) == 0)
2157 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2160 if (flags
& ARG_UPATH1
) {
2161 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH1
);
2162 pathp
= &ar
->k_ar
.ar_arg_upath1
;
2165 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH2
);
2166 pathp
= &ar
->k_ar
.ar_arg_upath2
;
2169 if (*pathp
== NULL
) {
2170 *pathp
= (char *)kalloc(MAXPATHLEN
);
2175 if (canon_path(p
, upath
, *pathp
) == 0) {
2176 if (flags
& ARG_UPATH1
)
2177 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH1
;
2179 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH2
;
2181 kfree(*pathp
, MAXPATHLEN
);
2187 * Function to save the path and vnode attr information into the audit
2190 * It is assumed that the caller will hold any vnode locks necessary to
2191 * perform a VNOP_GETATTR() on the passed vnode.
2193 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but
2194 * always provides access to the generation number as we need that
2195 * to construct the BSM file ID.
2196 * XXX: We should accept the process argument from the caller, since
2197 * it's very likely they already have a reference.
2198 * XXX: Error handling in this function is poor.
2201 audit_arg_vnpath(struct vnode
*vp
, u_int64_t flags
)
2203 struct kaudit_record
*ar
;
2204 struct vnode_attr va
;
2208 struct vnode_au_info
*vnp
;
2210 struct vfs_context context
;
2216 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2219 if ((flags
& (ARG_VNODE1
| ARG_VNODE2
)) == 0)
2224 if (flags
& ARG_VNODE1
) {
2225 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH1
);
2226 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE1
);
2227 pathp
= &ar
->k_ar
.ar_arg_kpath1
;
2228 vnp
= &ar
->k_ar
.ar_arg_vnode1
;
2231 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH2
);
2232 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE2
);
2233 pathp
= &ar
->k_ar
.ar_arg_kpath2
;
2234 vnp
= &ar
->k_ar
.ar_arg_vnode2
;
2237 if (*pathp
== NULL
) {
2238 *pathp
= (char *)kalloc(MAXPATHLEN
);
2244 * If vn_getpath() succeeds, place it in a string buffer
2245 * attached to the audit record, and set a flag indicating
2249 if (vn_getpath(vp
, *pathp
, &len
) == 0) {
2250 if (flags
& ARG_VNODE1
)
2251 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH1
;
2253 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH2
;
2255 kfree(*pathp
, MAXPATHLEN
);
2259 context
.vc_proc
= p
;
2260 context
.vc_ucred
= kauth_cred_get();
2263 VATTR_WANTED(&va
, va_mode
);
2264 VATTR_WANTED(&va
, va_uid
);
2265 VATTR_WANTED(&va
, va_gid
);
2266 VATTR_WANTED(&va
, va_rdev
);
2267 VATTR_WANTED(&va
, va_fsid
);
2268 VATTR_WANTED(&va
, va_fileid
);
2269 VATTR_WANTED(&va
, va_gen
);
2270 error
= vnode_getattr(vp
, &va
, &context
);
2272 /* XXX: How to handle this case? */
2276 /* XXX do we want to fall back here when these aren't supported? */
2277 vnp
->vn_mode
= va
.va_mode
;
2278 vnp
->vn_uid
= va
.va_uid
;
2279 vnp
->vn_gid
= va
.va_gid
;
2280 vnp
->vn_dev
= va
.va_rdev
;
2281 vnp
->vn_fsid
= va
.va_fsid
;
2282 vnp
->vn_fileid
= (u_long
)va
.va_fileid
;
2283 vnp
->vn_gen
= va
.va_gen
;
2284 if (flags
& ARG_VNODE1
)
2285 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE1
;
2287 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE2
;
2292 audit_arg_vnpath_withref(struct vnode
*vp
, u_int64_t flags
)
2294 if (vp
== NULL
|| vnode_getwithref(vp
))
2296 audit_arg_vnpath(vp
, flags
);
2297 (void)vnode_put(vp
);
2301 audit_arg_mach_port1(mach_port_name_t port
)
2303 struct kaudit_record
*ar
;
2309 ar
->k_ar
.ar_arg_mach_port1
= port
;
2310 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT1
;
2314 audit_arg_mach_port2(mach_port_name_t port
)
2316 struct kaudit_record
*ar
;
2322 ar
->k_ar
.ar_arg_mach_port2
= port
;
2323 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT2
;
2327 * The close() system call uses it's own audit call to capture the
2328 * path/vnode information because those pieces are not easily obtained
2329 * within the system call itself.
2332 audit_sysclose(struct proc
*p
, int fd
)
2334 struct fileproc
*fp
;
2339 if (fp_getfvp(p
, fd
, &fp
, &vp
) != 0)
2342 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2355 audit_shutdown(void)
2361 audit(struct proc
*p
, struct audit_args
*uap
, register_t
*retval
)
2367 auditon(struct proc
*p
, struct auditon_args
*uap
, register_t
*retval
)
2373 getauid(struct proc
*p
, struct getauid_args
*uap
, register_t
*retval
)
2379 setauid(struct proc
*p
, struct setauid_args
*uap
, register_t
*retval
)
2385 getaudit(struct proc
*p
, struct getaudit_args
*uap
, register_t
*retval
)
2391 setaudit(struct proc
*p
, struct setaudit_args
*uap
, register_t
*retval
)
2397 getaudit_addr(struct proc
*p
, struct getaudit_addr_args
*uap
, register_t
*retval
)
2403 setaudit_addr(struct proc
*p
, struct setaudit_addr_args
*uap
, register_t
*retval
)
2409 auditctl(struct proc
*p
, struct auditctl_args
*uap
, register_t
*retval
)