2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
24 #include <sys/fcntl.h>
25 #include <sys/kernel.h>
27 #include <sys/namei.h>
28 #include <sys/proc_internal.h>
29 #include <sys/kauth.h>
30 #include <sys/queue.h>
31 #include <sys/systm.h>
33 #include <sys/ucred.h>
35 #include <sys/unistd.h>
36 #include <sys/file_internal.h>
37 #include <sys/vnode_internal.h>
39 #include <sys/syscall.h>
40 #include <sys/malloc.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44 #include <sys/vfs_context.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socketvar.h>
49 #include <bsm/audit.h>
50 #include <bsm/audit_kevents.h>
51 #include <bsm/audit_klib.h>
52 #include <bsm/audit_kernel.h>
54 #include <mach/host_priv.h>
55 #include <mach/host_special_ports.h>
56 #include <mach/audit_triggers_server.h>
58 #include <kern/host.h>
59 #include <kern/kalloc.h>
60 #include <kern/zalloc.h>
61 #include <kern/lock.h>
62 #include <kern/wait_queue.h>
63 #include <kern/sched_prim.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
73 * The AUDIT_EXCESSIVELY_VERBOSE define enables a number of
74 * gratuitously noisy printf's to the console. Due to the
75 * volume, it should be left off unless you want your system
76 * to churn a lot whenever the audit record flow gets high.
78 /* #define AUDIT_EXCESSIVELY_VERBOSE */
79 #ifdef AUDIT_EXCESSIVELY_VERBOSE
80 #define AUDIT_PRINTF_ONLY
81 #define AUDIT_PRINTF(x) printf x
83 #define AUDIT_PRINTF_ONLY __unused
84 #define AUDIT_PRINTF(X)
91 #define assert(cond) \
92 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
94 #include <kern/assert.h>
95 #endif /* DIAGNOSTIC */
98 * Define the audit control flags.
104 * Mutex to protect global variables shared between various threads and
107 static mutex_t
*audit_mtx
;
110 * Queue of audit records ready for delivery to disk. We insert new
111 * records at the tail, and remove records from the head. Also,
112 * a count of the number of records used for checking queue depth.
113 * In addition, a counter of records that we have allocated but are
114 * not yet in the queue, which is needed to estimate the total
115 * size of the combined set of records outstanding in the system.
117 static TAILQ_HEAD(, kaudit_record
) audit_q
;
118 static size_t audit_q_len
;
119 static size_t audit_pre_q_len
;
121 static wait_queue_t audit_wait_queue
;
122 static zone_t audit_zone
;
125 * Condition variable to signal to the worker that it has work to do:
126 * either new records are in the queue, or a log replacement is taking
129 static int audit_worker_event
;
130 #define AUDIT_WORKER_EVENT ((event_t)&audit_worker_event)
133 * The audit worker thread (which is lazy started when we first
134 * rotate the audit log.
136 static thread_t audit_worker_thread
= THREAD_NULL
;
139 * When an audit log is rotated, the actual rotation must be performed
140 * by the audit worker thread, as it may have outstanding writes on the
141 * current audit log. audit_replacement_vp holds the vnode replacing
142 * the current vnode. We can't let more than one replacement occur
143 * at a time, so if more than one thread requests a replacement, only
144 * one can have the replacement "in progress" at any given moment. If
145 * a thread tries to replace the audit vnode and discovers a replacement
146 * is already in progress (i.e., audit_replacement_flag != 0), then it
147 * will sleep on audit_replacement_cv waiting its turn to perform a
148 * replacement. When a replacement is completed, this cv is signalled
149 * by the worker thread so a waiting thread can start another replacement.
150 * We also store a credential to perform audit log write operations with.
152 static int audit_replacement_event
;
153 #define AUDIT_REPLACEMENT_EVENT ((event_t)&audit_replacement_event)
155 static int audit_replacement_flag
;
156 static struct vnode
*audit_replacement_vp
;
157 static kauth_cred_t audit_replacement_cred
;
160 * Wait queue for auditing threads that cannot commit the audit
161 * record at the present time. Also, the queue control parameter
164 static int audit_commit_event
;
165 #define AUDIT_COMMIT_EVENT ((event_t)&audit_commit_event)
167 static struct au_qctrl audit_qctrl
;
170 * Flags to use on audit files when opening and closing.
172 static const int audit_open_flags
= FWRITE
| O_APPEND
;
173 static const int audit_close_flags
= FWRITE
| O_APPEND
;
176 * Global audit statistiscs.
178 static struct audit_fstat audit_fstat
;
181 Preselection mask for non-attributable events.
183 static struct au_mask audit_nae_mask
;
186 * Flags related to Kernel->user-space communication.
188 static int audit_file_rotate_wait
;
191 * Flags controlling behavior in low storage situations.
192 * Should we panic if a write fails? Should we fail stop
193 * if we're out of disk space? Are we currently "failing
194 * stop" due to out of disk space?
196 static int audit_panic_on_write_fail
;
197 static int audit_fail_stop
;
198 static int audit_in_failure
;
201 * When in a fail-stop mode, threads will drop into this wait queue
202 * rather than perform auditable events. They won't ever get woken
205 static int audit_failure_event
;
206 #define AUDIT_FAILURE_EVENT ((event_t)&audit_failure_event)
209 * XXX: Couldn't find the include file for this, so copied kern_exec.c's
212 extern task_t kernel_task
;
215 audit_free(struct kaudit_record
*ar
)
217 if (ar
->k_ar
.ar_arg_upath1
!= NULL
) {
218 kfree(ar
->k_ar
.ar_arg_upath1
, MAXPATHLEN
);
220 if (ar
->k_ar
.ar_arg_upath2
!= NULL
) {
221 kfree(ar
->k_ar
.ar_arg_upath2
, MAXPATHLEN
);
224 if (ar
->k_ar
.ar_arg_kpath1
!= NULL
) {
225 kfree(ar
->k_ar
.ar_arg_kpath1
, MAXPATHLEN
);
228 if (ar
->k_ar
.ar_arg_kpath2
!= NULL
) {
229 kfree(ar
->k_ar
.ar_arg_kpath2
, MAXPATHLEN
);
232 if (ar
->k_ar
.ar_arg_text
!= NULL
) {
233 kfree(ar
->k_ar
.ar_arg_text
, MAXPATHLEN
);
236 if (ar
->k_udata
!= NULL
) {
237 kfree(ar
->k_udata
, ar
->k_ulen
);
240 zfree(audit_zone
, ar
);
244 audit_write(struct vnode
*vp
, struct kaudit_record
*ar
, kauth_cred_t cred
,
247 struct vfsstatfs
*mnt_stat
= &vp
->v_mount
->mnt_vfsstat
;
249 struct au_record
*bsm
;
250 /* KVV maybe we should take a context as a param to audit_write? */
251 struct vfs_context context
;
254 mach_port_t audit_port
;
257 * First, gather statistics on the audit log file and file system
258 * so that we know how we're doing on space. In both cases,
259 * if we're unable to perform the operation, we drop the record
260 * and return. However, this is arguably an assertion failure.
263 context
.vc_ucred
= cred
;
264 ret
= vfs_update_vfsstat(vp
->v_mount
, &context
);
268 /* update the global stats struct */
269 if ((ret
= vnode_size(vp
, &file_size
, &context
)) != 0)
271 audit_fstat
.af_currsz
= file_size
;
274 * Send a message to the audit daemon when disk space is getting
276 * XXX Need to decide what to do if the trigger to the audit daemon
279 if(host_get_audit_control_port(host_priv_self(), &audit_port
)
281 printf("Cannot get audit control port\n");
283 if (audit_port
!= MACH_PORT_NULL
) {
287 * If we fall below percent free blocks, then trigger the
288 * audit daemon to do something about it.
290 if (audit_qctrl
.aq_minfree
!= 0) {
291 temp
= mnt_stat
->f_blocks
/ (100 / audit_qctrl
.aq_minfree
);
292 if (mnt_stat
->f_bfree
< temp
) {
293 ret
= audit_triggers(audit_port
,
294 AUDIT_TRIGGER_LOW_SPACE
);
295 if (ret
!= KERN_SUCCESS
) {
297 "Failed audit_triggers(AUDIT_TRIGGER_LOW_SPACE): %d\n", ret
);
299 * XXX: What to do here? Disable auditing?
305 /* Check if the current log file is full; if so, call for
306 * a log rotate. This is not an exact comparison; we may
307 * write some records over the limit. If that's not
308 * acceptable, then add a fudge factor here.
310 if ((audit_fstat
.af_filesz
!= 0) &&
311 (audit_file_rotate_wait
== 0) &&
312 (file_size
>= audit_fstat
.af_filesz
)) {
313 audit_file_rotate_wait
= 1;
314 ret
= audit_triggers(audit_port
,
315 AUDIT_TRIGGER_FILE_FULL
);
316 if (ret
!= KERN_SUCCESS
) {
318 "Failed audit_triggers(AUDIT_TRIGGER_FILE_FULL): %d\n", ret
);
319 /* XXX what to do here? */
325 * If the estimated amount of audit data in the audit event queue
326 * (plus records allocated but not yet queued) has reached the
327 * amount of free space on the disk, then we need to go into an
328 * audit fail stop state, in which we do not permit the
329 * allocation/committing of any new audit records. We continue to
330 * process packets but don't allow any activities that might
331 * generate new records. In the future, we might want to detect
332 * when space is available again and allow operation to continue,
333 * but this behavior is sufficient to meet fail stop requirements
336 if (audit_fail_stop
&&
338 ((audit_q_len
+ audit_pre_q_len
+ 1) * MAX_AUDIT_RECORD_SIZE
) /
339 mnt_stat
->f_bsize
>= (unsigned long)(mnt_stat
->f_bfree
)) {
341 "audit_worker: free space below size of audit queue, failing stop\n");
342 audit_in_failure
= 1;
346 * If there is a user audit record attached to the kernel record,
347 * then write the user record.
349 /* XXX Need to decide a few things here: IF the user audit
350 * record is written, but the write of the kernel record fails,
351 * what to do? Should the kernel record come before or after the
352 * user record? For now, we write the user record first, and
355 if (ar
->k_ar_commit
& AR_COMMIT_USER
) {
356 if (vnode_getwithref(vp
) == 0) {
357 ret
= vn_rdwr(UIO_WRITE
, vp
, (void *)ar
->k_udata
, ar
->k_ulen
,
358 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
);
368 * Convert the internal kernel record to BSM format and write it
369 * out if everything's OK.
371 if (!(ar
->k_ar_commit
& AR_COMMIT_KERNEL
)) {
376 ret
= kaudit_to_bsm(ar
, &bsm
);
377 if (ret
== BSM_NOAUDIT
) {
383 * XXX: We drop the record on BSM conversion failure, but really
384 * this is an assertion failure.
386 if (ret
== BSM_FAILURE
) {
387 AUDIT_PRINTF(("BSM conversion failure\n"));
392 /* XXX This function can be called with the kernel funnel held,
393 * which is not optimal. We should break the write functionality
394 * away from the BSM record generation and have the BSM generation
395 * done before this function is called. This function will then
396 * take the BSM record as a parameter.
398 if ((ret
= vnode_getwithref(vp
)) == 0) {
399 ret
= (vn_rdwr(UIO_WRITE
, vp
, (void *)bsm
->data
, bsm
->len
,
400 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, cred
, NULL
, p
));
407 * When we're done processing the current record, we have to
408 * check to see if we're in a failure mode, and if so, whether
409 * this was the last record left to be drained. If we're done
410 * draining, then we fsync the vnode and panic.
412 if (audit_in_failure
&&
413 audit_q_len
== 0 && audit_pre_q_len
== 0) {
414 (void)VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
415 panic("Audit store overflow; record queue drained.");
424 int do_replacement_signal
, error
, release_funnel
;
425 TAILQ_HEAD(, kaudit_record
) ar_worklist
;
426 struct kaudit_record
*ar
;
427 struct vnode
*audit_vp
, *old_vp
;
428 kauth_cred_t audit_cred
;
429 kauth_cred_t old_cred
;
430 struct proc
*audit_p
;
432 AUDIT_PRINTF(("audit_worker starting\n"));
434 TAILQ_INIT(&ar_worklist
);
436 audit_p
= current_proc();
440 * XXX: Presumably we can assume Mach threads are started without
441 * holding the BSD kernel funnel?
443 thread_funnel_set(kernel_flock
, FALSE
);
445 mutex_lock(audit_mtx
);
448 * First priority: replace the audit log target if requested.
449 * As we actually close the vnode in the worker thread, we
450 * need to grab the funnel, which means releasing audit_mtx.
451 * In case another replacement was scheduled while the mutex
452 * we released, we loop.
454 * XXX It could well be we should drain existing records
455 * first to ensure that the timestamps and ordering
458 do_replacement_signal
= 0;
459 while (audit_replacement_flag
!= 0) {
460 old_cred
= audit_cred
;
462 audit_cred
= audit_replacement_cred
;
463 audit_vp
= audit_replacement_vp
;
464 audit_replacement_cred
= NULL
;
465 audit_replacement_vp
= NULL
;
466 audit_replacement_flag
= 0;
468 audit_enabled
= (audit_vp
!= NULL
);
470 if (old_vp
!= NULL
|| audit_vp
!= NULL
) {
471 mutex_unlock(audit_mtx
);
472 thread_funnel_set(kernel_flock
, TRUE
);
477 * XXX: What to do about write failures here?
479 if (old_vp
!= NULL
) {
480 AUDIT_PRINTF(("Closing old audit file\n"));
481 vn_close(old_vp
, audit_close_flags
, old_cred
,
483 kauth_cred_rele(old_cred
);
486 AUDIT_PRINTF(("Audit file closed\n"));
488 if (audit_vp
!= NULL
) {
489 AUDIT_PRINTF(("Opening new audit file\n"));
491 if (release_funnel
) {
492 thread_funnel_set(kernel_flock
, FALSE
);
493 mutex_lock(audit_mtx
);
495 do_replacement_signal
= 1;
498 * Signal that replacement have occurred to wake up and
499 * start any other replacements started in parallel. We can
500 * continue about our business in the mean time. We
501 * broadcast so that both new replacements can be inserted,
502 * but also so that the source(s) of replacement can return
505 if (do_replacement_signal
)
506 wait_queue_wakeup_all(audit_wait_queue
,
507 AUDIT_REPLACEMENT_EVENT
, THREAD_AWAKENED
);
510 * Next, check to see if we have any records to drain into
511 * the vnode. If not, go back to waiting for an event.
513 if (TAILQ_EMPTY(&audit_q
)) {
516 AUDIT_PRINTF(("audit_worker waiting\n"));
517 ret
= wait_queue_assert_wait(audit_wait_queue
,
521 mutex_unlock(audit_mtx
);
523 assert(ret
== THREAD_WAITING
);
524 ret
= thread_block(THREAD_CONTINUE_NULL
);
525 assert(ret
== THREAD_AWAKENED
);
526 AUDIT_PRINTF(("audit_worker woken up\n"));
527 AUDIT_PRINTF(("audit_worker: new vp = %p; value of flag %d\n",
528 audit_replacement_vp
, audit_replacement_flag
));
530 mutex_lock(audit_mtx
);
535 * If we have records, but there's no active vnode to
536 * write to, drain the record queue. Generally, we
537 * prevent the unnecessary allocation of records
538 * elsewhere, but we need to allow for races between
539 * conditional allocation and queueing. Go back to
540 * waiting when we're done.
542 * XXX: We go out of our way to avoid calling audit_free()
543 * with the audit_mtx held, to avoid a lock order reversal
544 * as free() may grab the funnel. This will be fixed at
547 if (audit_vp
== NULL
) {
548 while ((ar
= TAILQ_FIRST(&audit_q
))) {
549 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
551 if (audit_q_len
<= audit_qctrl
.aq_lowater
)
552 wait_queue_wakeup_one(
557 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
559 mutex_unlock(audit_mtx
);
560 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
561 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
564 mutex_lock(audit_mtx
);
569 * We have both records to write, and an active vnode
570 * to write to. Dequeue a record, and start the write.
571 * Eventually, it might make sense to dequeue several
572 * records and perform our own clustering, if the lower
573 * layers aren't doing it automatically enough.
575 * XXX: We go out of our way to avoid calling audit_free()
576 * with the audit_mtx held, to avoid a lock order reversal
577 * as free() may grab the funnel. This will be fixed at
580 while ((ar
= TAILQ_FIRST(&audit_q
))) {
581 TAILQ_REMOVE(&audit_q
, ar
, k_q
);
583 if (audit_q_len
<= audit_qctrl
.aq_lowater
) {
584 wait_queue_wakeup_one(audit_wait_queue
,
585 AUDIT_COMMIT_EVENT
, THREAD_AWAKENED
);
588 TAILQ_INSERT_TAIL(&ar_worklist
, ar
, k_q
);
590 mutex_unlock(audit_mtx
);
592 while ((ar
= TAILQ_FIRST(&ar_worklist
))) {
593 TAILQ_REMOVE(&ar_worklist
, ar
, k_q
);
594 if (audit_vp
!= NULL
) {
596 * XXX: What should happen if there's a write
599 if (!release_funnel
) {
600 thread_funnel_set(kernel_flock
, TRUE
);
603 error
= audit_write(audit_vp
, ar
, audit_cred
,
605 if (error
&& audit_panic_on_write_fail
) {
606 panic("audit_worker: write error %d\n",
609 printf("audit_worker: write error %d\n",
616 thread_funnel_set(kernel_flock
, FALSE
);
617 mutex_lock(audit_mtx
);
625 /* Verify that the syscall to audit event table is the same
626 * size as the system call table.
628 if (nsys_au_event
!= nsysent
) {
629 printf("Security auditing service initialization failed, ");
630 printf("audit event table doesn't match syscall table.\n");
634 printf("Security auditing service present\n");
635 TAILQ_INIT(&audit_q
);
639 audit_replacement_cred
= NULL
;
640 audit_replacement_flag
= 0;
641 audit_file_rotate_wait
= 0;
642 audit_replacement_vp
= NULL
;
643 audit_fstat
.af_filesz
= 0; /* '0' means unset, unbounded */
644 audit_fstat
.af_currsz
= 0;
645 audit_qctrl
.aq_hiwater
= AQ_HIWATER
;
646 audit_qctrl
.aq_lowater
= AQ_LOWATER
;
647 audit_qctrl
.aq_bufsz
= AQ_BUFSZ
;
648 audit_qctrl
.aq_minfree
= AU_FS_MINFREE
;
650 audit_mtx
= mutex_alloc(0);
651 audit_wait_queue
= wait_queue_alloc(SYNC_POLICY_FIFO
);
652 audit_zone
= zinit(sizeof(struct kaudit_record
),
653 AQ_HIWATER
*sizeof(struct kaudit_record
),
657 /* Initialize the BSM audit subsystem. */
662 audit_rotate_vnode(kauth_cred_t cred
, struct vnode
*vp
)
667 * If other parallel log replacements have been requested, we wait
668 * until they've finished before continuing.
670 mutex_lock(audit_mtx
);
671 while (audit_replacement_flag
!= 0) {
673 AUDIT_PRINTF(("audit_rotate_vnode: sleeping to wait for "
675 ret
= wait_queue_assert_wait(audit_wait_queue
,
676 AUDIT_REPLACEMENT_EVENT
,
679 mutex_unlock(audit_mtx
);
681 assert(ret
== THREAD_WAITING
);
682 ret
= thread_block(THREAD_CONTINUE_NULL
);
683 assert(ret
== THREAD_AWAKENED
);
684 AUDIT_PRINTF(("audit_rotate_vnode: woken up (flag %d)\n",
685 audit_replacement_flag
));
687 mutex_lock(audit_mtx
);
689 audit_replacement_cred
= cred
;
690 audit_replacement_flag
= 1;
691 audit_replacement_vp
= vp
;
694 * Start or wake up the audit worker to perform the exchange.
695 * It will have to wait until we release the mutex.
697 if (audit_worker_thread
== THREAD_NULL
)
698 audit_worker_thread
= kernel_thread(kernel_task
,
701 wait_queue_wakeup_one(audit_wait_queue
,
706 * Wait for the audit_worker to broadcast that a replacement has
707 * taken place; we know that once this has happened, our vnode
708 * has been replaced in, so we can return successfully.
710 AUDIT_PRINTF(("audit_rotate_vnode: waiting for news of "
712 ret
= wait_queue_assert_wait(audit_wait_queue
,
713 AUDIT_REPLACEMENT_EVENT
,
716 mutex_unlock(audit_mtx
);
718 assert(ret
== THREAD_WAITING
);
719 ret
= thread_block(THREAD_CONTINUE_NULL
);
720 assert(ret
== THREAD_AWAKENED
);
721 AUDIT_PRINTF(("audit_rotate_vnode: change acknowledged by "
722 "audit_worker (flag " "now %d)\n", audit_replacement_flag
));
724 audit_file_rotate_wait
= 0; /* We can now request another rotation */
728 * Drain the audit queue and close the log at shutdown.
733 audit_rotate_vnode(NULL
, NULL
);
736 static __inline__
struct uthread
*
739 return (get_bsdthread_info(current_thread()));
742 static __inline__
struct kaudit_record
*
745 return (curuthread()->uu_ar
);
748 /**********************************
749 * Begin system calls. *
750 **********************************/
752 * System call to allow a user space application to submit a BSM audit
753 * record to the kernel for inclusion in the audit log. This function
754 * does little verification on the audit record that is submitted.
756 * XXXAUDIT: Audit preselection for user records does not currently
757 * work, since we pre-select only based on the AUE_audit event type,
758 * not the event type submitted as part of the user audit data.
762 audit(struct proc
*p
, struct audit_args
*uap
, __unused register_t
*retval
)
766 struct kaudit_record
*ar
;
767 struct uthread
*uthr
;
769 error
= suser(kauth_cred_get(), &p
->p_acflag
);
773 if ((uap
->length
<= 0) || (uap
->length
> (int)audit_qctrl
.aq_bufsz
))
778 /* If there's no current audit record (audit() itself not audited)
779 * commit the user audit record.
783 if (uthr
== NULL
) /* can this happen? */
786 /* This is not very efficient; we're required to allocate
787 * a complete kernel audit record just so the user record
790 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
791 if (uthr
->uu_ar
== NULL
) /* auditing not on, or memory error */
796 if (uap
->length
> MAX_AUDIT_RECORD_SIZE
)
799 rec
= (void *)kalloc((vm_size_t
)uap
->length
);
801 error
= copyin(uap
->record
, rec
, uap
->length
);
805 /* Verify the record */
806 if (bsm_rec_verify(rec
) == 0) {
811 /* Attach the user audit record to the kernel audit record. Because
812 * this system call is an auditable event, we will write the user
813 * record along with the record for this audit event.
816 ar
->k_ar_commit
|= AR_COMMIT_USER
;
817 ar
->k_ulen
= uap
->length
;
821 /* audit_syscall_exit() will free the audit record on the thread
822 * even if we allocated it above.
824 kfree(rec
, uap
->length
);
829 * System call to manipulate auditing.
833 auditon(struct proc
*p
, __unused
struct auditon_args
*uap
, __unused register_t
*retval
)
837 union auditon_udata udata
;
840 AUDIT_ARG(cmd
, uap
->cmd
);
841 ret
= suser(kauth_cred_get(), &p
->p_acflag
);
846 if ((len
<= 0) || (len
> (int)sizeof(union auditon_udata
)))
849 memset((void *)&udata
, 0, sizeof(udata
));
852 /* Some of the GET commands use the arguments too */
866 case A_GETPINFO_ADDR
:
867 ret
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
870 AUDIT_ARG(auditon
, &udata
);
874 /* XXX Need to implement these commands by accessing the global
875 * values associated with the commands.
879 if (!audit_fail_stop
)
880 udata
.au_policy
|= AUDIT_CNT
;
881 if (audit_panic_on_write_fail
)
882 udata
.au_policy
|= AUDIT_AHLT
;
885 if (udata
.au_policy
& ~(AUDIT_CNT
|AUDIT_AHLT
))
888 * XXX - Need to wake up waiters if the policy relaxes?
890 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
891 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
894 udata
.au_mask
= audit_nae_mask
;
897 audit_nae_mask
= udata
.au_mask
;
900 udata
.au_qctrl
= audit_qctrl
;
903 if ((udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
904 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
905 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
906 (udata
.au_qctrl
.aq_minfree
< 0) ||
907 (udata
.au_qctrl
.aq_minfree
> 100))
910 audit_qctrl
= udata
.au_qctrl
;
911 /* XXX The queue delay value isn't used with the kernel. */
912 audit_qctrl
.aq_delay
= -1;
933 if (audit_enabled
&& !audit_suspended
)
934 udata
.au_cond
= AUC_AUDITING
;
936 udata
.au_cond
= AUC_NOAUDIT
;
939 if (udata
.au_cond
== AUC_NOAUDIT
)
941 if (udata
.au_cond
== AUC_AUDITING
)
943 if (udata
.au_cond
== AUC_DISABLED
) {
949 udata
.au_evclass
.ec_class
=
950 au_event_class(udata
.au_evclass
.ec_number
);
953 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
954 udata
.au_evclass
.ec_class
);
957 if (udata
.au_aupinfo
.ap_pid
< 1)
959 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
962 udata
.au_aupinfo
.ap_auid
= tp
->p_ucred
->cr_au
.ai_auid
;
963 udata
.au_aupinfo
.ap_mask
.am_success
=
964 tp
->p_ucred
->cr_au
.ai_mask
.am_success
;
965 udata
.au_aupinfo
.ap_mask
.am_failure
=
966 tp
->p_ucred
->cr_au
.ai_mask
.am_failure
;
967 udata
.au_aupinfo
.ap_termid
.machine
=
968 tp
->p_ucred
->cr_au
.ai_termid
.machine
;
969 udata
.au_aupinfo
.ap_termid
.port
=
970 tp
->p_ucred
->cr_au
.ai_termid
.port
;
971 udata
.au_aupinfo
.ap_asid
= tp
->p_ucred
->cr_au
.ai_asid
;
974 if (udata
.au_aupinfo
.ap_pid
< 1)
976 if ((tp
= pfind(udata
.au_aupinfo
.ap_pid
)) == NULL
)
980 * we are modifying the audit info in a credential so we need a new
981 * credential (or take another reference on an existing credential that
982 * matches our new one). We must do this because the audit info in the
983 * credential is used as part of our hash key. Get current credential
984 * in the target process and take a reference while we muck with it.
987 kauth_cred_t my_cred
, my_new_cred
;
988 struct auditinfo temp_auditinfo
;
990 my_cred
= kauth_cred_proc_ref(tp
);
992 * set the credential with new info. If there is no change we get back
993 * the same credential we passed in.
995 temp_auditinfo
= my_cred
->cr_au
;
996 temp_auditinfo
.ai_mask
.am_success
=
997 udata
.au_aupinfo
.ap_mask
.am_success
;
998 temp_auditinfo
.ai_mask
.am_failure
=
999 udata
.au_aupinfo
.ap_mask
.am_failure
;
1000 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1002 if (my_cred
!= my_new_cred
) {
1004 /* need to protect for a race where another thread also changed
1005 * the credential after we took our reference. If p_ucred has
1006 * changed then we should restart this again with the new cred.
1008 if (tp
->p_ucred
!= my_cred
) {
1010 kauth_cred_rele(my_cred
);
1011 kauth_cred_rele(my_new_cred
);
1015 tp
->p_ucred
= my_new_cred
;
1018 /* drop our extra reference */
1019 kauth_cred_rele(my_cred
);
1024 if ((udata
.au_fstat
.af_filesz
!= 0) &&
1025 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
))
1027 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
1030 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
1031 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
1033 case A_GETPINFO_ADDR
:
1043 /* Copy data back to userspace for the GET comands */
1055 case A_GETPINFO_ADDR
:
1057 ret
= copyout((void *)&udata
, uap
->data
, uap
->length
);
1067 * System calls to manage the user audit information.
1068 * XXXAUDIT May need to lock the proc structure.
1072 getauid(struct proc
*p
, struct getauid_args
*uap
, __unused register_t
*retval
)
1076 error
= copyout((void *)&kauth_cred_get()->cr_au
.ai_auid
,
1077 uap
->auid
, sizeof(au_id_t
));
1086 setauid(struct proc
*p
, struct setauid_args
*uap
, __unused register_t
*retval
)
1091 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1095 error
= copyin(uap
->auid
,
1096 (void *)&temp_au_id
,
1102 * we are modifying the audit info in a credential so we need a new
1103 * credential (or take another reference on an existing credential that
1104 * matches our new one). We must do this because the audit info in the
1105 * credential is used as part of our hash key. Get current credential
1106 * in the target process and take a reference while we muck with it.
1109 kauth_cred_t my_cred
, my_new_cred
;
1110 struct auditinfo temp_auditinfo
;
1112 my_cred
= kauth_cred_proc_ref(p
);
1114 * set the credential with new info. If there is no change we get back
1115 * the same credential we passed in.
1117 temp_auditinfo
= my_cred
->cr_au
;
1118 temp_auditinfo
.ai_auid
= temp_au_id
;
1119 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1121 if (my_cred
!= my_new_cred
) {
1123 /* need to protect for a race where another thread also changed
1124 * the credential after we took our reference. If p_ucred has
1125 * changed then we should restart this again with the new cred.
1127 if (p
->p_ucred
!= my_cred
) {
1129 kauth_cred_rele(my_cred
);
1130 kauth_cred_rele(my_new_cred
);
1134 p
->p_ucred
= my_new_cred
;
1137 /* drop our extra reference */
1138 kauth_cred_rele(my_cred
);
1142 /* propagate the change from the process to Mach task */
1143 set_security_token(p
);
1145 audit_arg_auid(kauth_cred_get()->cr_au
.ai_auid
);
1150 * System calls to get and set process audit information.
1151 * If the caller is privileged, they get the whole set of
1152 * audit information. Otherwise, the real audit mask is
1153 * filtered out - but the rest of the information is
1158 getaudit(struct proc
*p
, struct getaudit_args
*uap
, __unused register_t
*retval
)
1160 struct auditinfo ai
;
1163 ai
= kauth_cred_get()->cr_au
;
1165 /* only superuser gets to see the real mask */
1166 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1168 ai
.ai_mask
.am_success
= ~0;
1169 ai
.ai_mask
.am_failure
= ~0;
1172 error
= copyout(&ai
, uap
->auditinfo
, sizeof(ai
));
1181 setaudit(struct proc
*p
, struct setaudit_args
*uap
, __unused register_t
*retval
)
1184 struct auditinfo temp_auditinfo
;
1186 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1190 error
= copyin(uap
->auditinfo
,
1191 (void *)&temp_auditinfo
,
1192 sizeof(temp_auditinfo
));
1197 * we are modifying the audit info in a credential so we need a new
1198 * credential (or take another reference on an existing credential that
1199 * matches our new one). We must do this because the audit info in the
1200 * credential is used as part of our hash key. Get current credential
1201 * in the target process and take a reference while we muck with it.
1204 kauth_cred_t my_cred
, my_new_cred
;
1206 my_cred
= kauth_cred_proc_ref(p
);
1208 * set the credential with new info. If there is no change we get back
1209 * the same credential we passed in.
1211 my_new_cred
= kauth_cred_setauditinfo(my_cred
, &temp_auditinfo
);
1213 if (my_cred
!= my_new_cred
) {
1215 /* need to protect for a race where another thread also changed
1216 * the credential after we took our reference. If p_ucred has
1217 * changed then we should restart this again with the new cred.
1219 if (p
->p_ucred
!= my_cred
) {
1221 kauth_cred_rele(my_cred
);
1222 kauth_cred_rele(my_new_cred
);
1226 p
->p_ucred
= my_new_cred
;
1229 /* drop our extra reference */
1230 kauth_cred_rele(my_cred
);
1234 /* propagate the change from the process to Mach task */
1235 set_security_token(p
);
1237 audit_arg_auditinfo(&p
->p_ucred
->cr_au
);
1244 getaudit_addr(struct proc
*p
, __unused
struct getaudit_addr_args
*uap
, __unused register_t
*retval
)
1251 setaudit_addr(struct proc
*p
, __unused
struct setaudit_addr_args
*uap
, __unused register_t
*retval
)
1255 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1262 * Syscall to manage audit files.
1267 auditctl(struct proc
*p
, struct auditctl_args
*uap
, __unused register_t
*retval
)
1269 struct nameidata nd
;
1273 struct vfs_context context
;
1275 context
.vc_proc
= p
;
1276 context
.vc_ucred
= kauth_cred_get();
1278 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1286 * If a path is specified, open the replacement vnode, perform
1287 * validity checks, and grab another reference to the current
1290 if (uap
->path
!= 0) {
1291 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1292 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1293 uap
->path
, &context
);
1294 flags
= audit_open_flags
;
1295 error
= vn_open(&nd
, flags
, 0);
1299 if (vp
->v_type
!= VREG
) {
1300 vn_close(vp
, audit_close_flags
, kauth_cred_get(), p
);
1305 cred
= kauth_cred_get_with_ref();
1306 audit_suspended
= 0;
1309 * a vp and cred of NULL is valid at this point
1310 * and indicates we're to turn off auditing...
1312 audit_rotate_vnode(cred
, vp
);
1319 /**********************************
1320 * End of system calls. *
1321 **********************************/
1326 struct kaudit_record
*
1327 audit_new(int event
, struct proc
*p
, __unused
struct uthread
*uthread
)
1329 struct kaudit_record
*ar
;
1333 * Eventually, there may be certain classes of events that
1334 * we will audit regardless of the audit state at the time
1335 * the record is created. These events will generally
1336 * correspond to changes in the audit state. The dummy
1337 * code below is from our first prototype, but may also
1338 * be used in the final version (with modified event numbers).
1341 if (event
!= AUDIT_EVENT_FILESTOP
&& event
!= AUDIT_EVENT_FILESTART
) {
1343 mutex_lock(audit_mtx
);
1344 no_record
= (audit_suspended
|| !audit_enabled
);
1345 mutex_unlock(audit_mtx
);
1353 * Initialize the audit record header.
1354 * XXX: We may want to fail-stop if allocation fails.
1355 * XXX: The number of outstanding uncommitted audit records is
1356 * limited by the number of concurrent threads servicing system
1357 * calls in the kernel.
1360 ar
= (struct kaudit_record
*)zalloc(audit_zone
);
1364 mutex_lock(audit_mtx
);
1366 mutex_unlock(audit_mtx
);
1368 bzero(ar
, sizeof(*ar
));
1369 ar
->k_ar
.ar_magic
= AUDIT_RECORD_MAGIC
;
1370 ar
->k_ar
.ar_event
= event
;
1371 nanotime(&ar
->k_ar
.ar_starttime
);
1373 /* Export the subject credential. */
1374 cru2x(p
->p_ucred
, &ar
->k_ar
.ar_subj_cred
);
1375 ar
->k_ar
.ar_subj_ruid
= p
->p_ucred
->cr_ruid
;
1376 ar
->k_ar
.ar_subj_rgid
= p
->p_ucred
->cr_rgid
;
1377 ar
->k_ar
.ar_subj_egid
= p
->p_ucred
->cr_groups
[0];
1378 ar
->k_ar
.ar_subj_auid
= p
->p_ucred
->cr_au
.ai_auid
;
1379 ar
->k_ar
.ar_subj_asid
= p
->p_ucred
->cr_au
.ai_asid
;
1380 ar
->k_ar
.ar_subj_pid
= p
->p_pid
;
1381 ar
->k_ar
.ar_subj_amask
= p
->p_ucred
->cr_au
.ai_mask
;
1382 ar
->k_ar
.ar_subj_term
= p
->p_ucred
->cr_au
.ai_termid
;
1383 bcopy(p
->p_comm
, ar
->k_ar
.ar_subj_comm
, MAXCOMLEN
);
1390 * XXXAUDIT: So far, this is unused, and should probably be GC'd.
1393 audit_abort(struct kaudit_record
*ar
)
1395 mutex_lock(audit_mtx
);
1397 mutex_unlock(audit_mtx
);
1405 audit_commit(struct kaudit_record
*ar
, int error
, int retval
)
1409 struct au_mask
*aumask
;
1415 * Decide whether to commit the audit record by checking the
1416 * error value from the system call and using the appropriate
1419 if (ar
->k_ar
.ar_subj_auid
== AU_DEFAUDITID
)
1420 aumask
= &audit_nae_mask
;
1422 aumask
= &ar
->k_ar
.ar_subj_amask
;
1425 sorf
= AU_PRS_FAILURE
;
1427 sorf
= AU_PRS_SUCCESS
;
1429 switch(ar
->k_ar
.ar_event
) {
1432 /* The open syscall always writes a OPEN_RWTC event; limit the
1433 * to the proper type of event based on the flags and the error
1436 ar
->k_ar
.ar_event
= flags_and_error_to_openevent(ar
->k_ar
.ar_arg_fflags
, error
);
1440 ar
->k_ar
.ar_event
= ctlname_to_sysctlevent(ar
->k_ar
.ar_arg_ctlname
, ar
->k_ar
.ar_valid_arg
);
1444 /* Convert the auditon() command to an event */
1445 ar
->k_ar
.ar_event
= auditon_command_event(ar
->k_ar
.ar_arg_cmd
);
1449 if (au_preselect(ar
->k_ar
.ar_event
, aumask
, sorf
) != 0)
1450 ar
->k_ar_commit
|= AR_COMMIT_KERNEL
;
1452 if ((ar
->k_ar_commit
& (AR_COMMIT_USER
| AR_COMMIT_KERNEL
)) == 0) {
1453 mutex_lock(audit_mtx
);
1455 mutex_unlock(audit_mtx
);
1460 ar
->k_ar
.ar_errno
= error
;
1461 ar
->k_ar
.ar_retval
= retval
;
1464 * We might want to do some system-wide post-filtering
1465 * here at some point.
1469 * Timestamp system call end.
1471 nanotime(&ar
->k_ar
.ar_endtime
);
1473 mutex_lock(audit_mtx
);
1475 * Note: it could be that some records initiated while audit was
1476 * enabled should still be committed?
1478 if (audit_suspended
|| !audit_enabled
) {
1480 mutex_unlock(audit_mtx
);
1486 * Constrain the number of committed audit records based on
1487 * the configurable parameter.
1489 while (audit_q_len
>= audit_qctrl
.aq_hiwater
) {
1491 ret
= wait_queue_assert_wait(audit_wait_queue
,
1495 mutex_unlock(audit_mtx
);
1497 assert(ret
== THREAD_WAITING
);
1499 ret
= thread_block(THREAD_CONTINUE_NULL
);
1500 assert(ret
== THREAD_AWAKENED
);
1501 mutex_lock(audit_mtx
);
1504 TAILQ_INSERT_TAIL(&audit_q
, ar
, k_q
);
1507 wait_queue_wakeup_one(audit_wait_queue
, AUDIT_WORKER_EVENT
, THREAD_AWAKENED
);
1508 mutex_unlock(audit_mtx
);
1512 * Calls to set up and tear down audit structures associated with
1516 audit_syscall_enter(unsigned short code
, struct proc
*proc
,
1517 struct uthread
*uthread
)
1520 struct au_mask
*aumask
;
1522 audit_event
= sys_au_event
[code
];
1523 if (audit_event
== AUE_NULL
)
1526 assert(uthread
->uu_ar
== NULL
);
1528 /* Check which audit mask to use; either the kernel non-attributable
1529 * event mask or the process audit mask.
1531 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1532 aumask
= &audit_nae_mask
;
1534 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1537 * Allocate an audit record, if preselection allows it, and store
1538 * in the BSD thread for later use.
1540 if (au_preselect(audit_event
, aumask
,
1541 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1543 * If we're out of space and need to suspend unprivileged
1544 * processes, do that here rather than trying to allocate
1545 * another audit record.
1547 if (audit_in_failure
&&
1548 suser(kauth_cred_get(), &proc
->p_acflag
) != 0) {
1551 assert(audit_worker_thread
!= THREAD_NULL
);
1552 ret
= wait_queue_assert_wait(audit_wait_queue
,
1553 AUDIT_FAILURE_EVENT
, THREAD_UNINT
, 0);
1554 assert(ret
== THREAD_WAITING
);
1555 (void)thread_block(THREAD_CONTINUE_NULL
);
1556 panic("audit_failing_stop: thread continued");
1558 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1560 uthread
->uu_ar
= NULL
;
1565 audit_syscall_exit(int error
, AUDIT_PRINTF_ONLY
struct proc
*proc
, struct uthread
*uthread
)
1570 * Commit the audit record as desired; once we pass the record
1571 * into audit_commit(), the memory is owned by the audit
1573 * The return value from the system call is stored on the user
1574 * thread. If there was an error, the return value is set to -1,
1575 * imitating the behavior of the cerror routine.
1580 retval
= uthread
->uu_rval
[0];
1582 audit_commit(uthread
->uu_ar
, error
, retval
);
1583 if (uthread
->uu_ar
!= NULL
) {
1584 AUDIT_PRINTF(("audit record committed by pid %d\n", proc
->p_pid
));
1586 uthread
->uu_ar
= NULL
;
1591 * Calls to set up and tear down audit structures used during Mach
1595 audit_mach_syscall_enter(unsigned short audit_event
)
1597 struct uthread
*uthread
;
1599 struct au_mask
*aumask
;
1601 if (audit_event
== AUE_NULL
)
1604 uthread
= curuthread();
1605 if (uthread
== NULL
)
1608 proc
= current_proc();
1612 assert(uthread
->uu_ar
== NULL
);
1614 /* Check which audit mask to use; either the kernel non-attributable
1615 * event mask or the process audit mask.
1617 if (proc
->p_ucred
->cr_au
.ai_auid
== AU_DEFAUDITID
)
1618 aumask
= &audit_nae_mask
;
1620 aumask
= &proc
->p_ucred
->cr_au
.ai_mask
;
1623 * Allocate an audit record, if desired, and store in the BSD
1624 * thread for later use.
1626 if (au_preselect(audit_event
, aumask
,
1627 AU_PRS_FAILURE
| AU_PRS_SUCCESS
)) {
1628 uthread
->uu_ar
= audit_new(audit_event
, proc
, uthread
);
1630 uthread
->uu_ar
= NULL
;
1635 audit_mach_syscall_exit(int retval
, struct uthread
*uthread
)
1637 /* The error code from Mach system calls is the same as the
1640 /* XXX Is the above statement always true? */
1641 audit_commit(uthread
->uu_ar
, retval
, retval
);
1642 uthread
->uu_ar
= NULL
;
1647 * Calls to manipulate elements of the audit record structure from system
1648 * call code. Macro wrappers will prevent this functions from being
1649 * entered if auditing is disabled, avoiding the function call cost. We
1650 * check the thread audit record pointer anyway, as the audit condition
1651 * could change, and pre-selection may not have allocated an audit
1652 * record for this event.
1655 audit_arg_addr(user_addr_t addr
)
1657 struct kaudit_record
*ar
;
1663 ar
->k_ar
.ar_arg_addr
= CAST_DOWN(void *, addr
); /* XXX */
1664 ar
->k_ar
.ar_valid_arg
|= ARG_ADDR
;
1668 audit_arg_len(user_size_t len
)
1670 struct kaudit_record
*ar
;
1676 ar
->k_ar
.ar_arg_len
= CAST_DOWN(int, len
); /* XXX */
1677 ar
->k_ar
.ar_valid_arg
|= ARG_LEN
;
1681 audit_arg_fd(int fd
)
1683 struct kaudit_record
*ar
;
1689 ar
->k_ar
.ar_arg_fd
= fd
;
1690 ar
->k_ar
.ar_valid_arg
|= ARG_FD
;
1694 audit_arg_fflags(int fflags
)
1696 struct kaudit_record
*ar
;
1702 ar
->k_ar
.ar_arg_fflags
= fflags
;
1703 ar
->k_ar
.ar_valid_arg
|= ARG_FFLAGS
;
1707 audit_arg_gid(gid_t gid
, gid_t egid
, gid_t rgid
, gid_t sgid
)
1709 struct kaudit_record
*ar
;
1715 ar
->k_ar
.ar_arg_gid
= gid
;
1716 ar
->k_ar
.ar_arg_egid
= egid
;
1717 ar
->k_ar
.ar_arg_rgid
= rgid
;
1718 ar
->k_ar
.ar_arg_sgid
= sgid
;
1719 ar
->k_ar
.ar_valid_arg
|= (ARG_GID
| ARG_EGID
| ARG_RGID
| ARG_SGID
);
1723 audit_arg_uid(uid_t uid
, uid_t euid
, uid_t ruid
, uid_t suid
)
1725 struct kaudit_record
*ar
;
1731 ar
->k_ar
.ar_arg_uid
= uid
;
1732 ar
->k_ar
.ar_arg_euid
= euid
;
1733 ar
->k_ar
.ar_arg_ruid
= ruid
;
1734 ar
->k_ar
.ar_arg_suid
= suid
;
1735 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_EUID
| ARG_RUID
| ARG_SUID
);
1739 audit_arg_groupset(const gid_t
*gidset
, u_int gidset_size
)
1742 struct kaudit_record
*ar
;
1748 for (i
= 0; i
< gidset_size
; i
++)
1749 ar
->k_ar
.ar_arg_groups
.gidset
[i
] = gidset
[i
];
1750 ar
->k_ar
.ar_arg_groups
.gidset_size
= gidset_size
;
1751 ar
->k_ar
.ar_valid_arg
|= ARG_GROUPSET
;
1755 audit_arg_login(const char *login
)
1757 struct kaudit_record
*ar
;
1765 * XXX: Add strlcpy() to Darwin for improved safety.
1767 strlcpy(ar
->k_ar
.ar_arg_login
, login
, MAXLOGNAME
);
1769 strcpy(ar
->k_ar
.ar_arg_login
, login
);
1772 ar
->k_ar
.ar_valid_arg
|= ARG_LOGIN
;
1776 audit_arg_ctlname(const int *name
, int namelen
)
1778 struct kaudit_record
*ar
;
1784 bcopy(name
, &ar
->k_ar
.ar_arg_ctlname
, namelen
* sizeof(int));
1785 ar
->k_ar
.ar_arg_len
= namelen
;
1786 ar
->k_ar
.ar_valid_arg
|= (ARG_CTLNAME
| ARG_LEN
);
1790 audit_arg_mask(int mask
)
1792 struct kaudit_record
*ar
;
1798 ar
->k_ar
.ar_arg_mask
= mask
;
1799 ar
->k_ar
.ar_valid_arg
|= ARG_MASK
;
1803 audit_arg_mode(mode_t mode
)
1805 struct kaudit_record
*ar
;
1811 ar
->k_ar
.ar_arg_mode
= mode
;
1812 ar
->k_ar
.ar_valid_arg
|= ARG_MODE
;
1816 audit_arg_dev(int dev
)
1818 struct kaudit_record
*ar
;
1824 ar
->k_ar
.ar_arg_dev
= dev
;
1825 ar
->k_ar
.ar_valid_arg
|= ARG_DEV
;
1829 audit_arg_value(long value
)
1831 struct kaudit_record
*ar
;
1837 ar
->k_ar
.ar_arg_value
= value
;
1838 ar
->k_ar
.ar_valid_arg
|= ARG_VALUE
;
1842 audit_arg_owner(uid_t uid
, gid_t gid
)
1844 struct kaudit_record
*ar
;
1850 ar
->k_ar
.ar_arg_uid
= uid
;
1851 ar
->k_ar
.ar_arg_gid
= gid
;
1852 ar
->k_ar
.ar_valid_arg
|= (ARG_UID
| ARG_GID
);
1856 audit_arg_pid(pid_t pid
)
1858 struct kaudit_record
*ar
;
1864 ar
->k_ar
.ar_arg_pid
= pid
;
1865 ar
->k_ar
.ar_valid_arg
|= ARG_PID
;
1869 audit_arg_process(struct proc
*p
)
1871 struct kaudit_record
*ar
;
1874 if ((ar
== NULL
) || (p
== NULL
))
1877 ar
->k_ar
.ar_arg_auid
= p
->p_ucred
->cr_au
.ai_auid
;
1878 ar
->k_ar
.ar_arg_euid
= p
->p_ucred
->cr_uid
;
1879 ar
->k_ar
.ar_arg_egid
= p
->p_ucred
->cr_groups
[0];
1880 ar
->k_ar
.ar_arg_ruid
= p
->p_ucred
->cr_ruid
;
1881 ar
->k_ar
.ar_arg_rgid
= p
->p_ucred
->cr_rgid
;
1882 ar
->k_ar
.ar_arg_asid
= p
->p_ucred
->cr_au
.ai_asid
;
1883 ar
->k_ar
.ar_arg_termid
= p
->p_ucred
->cr_au
.ai_termid
;
1885 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_EUID
| ARG_EGID
| ARG_RUID
|
1886 ARG_RGID
| ARG_ASID
| ARG_TERMID
| ARG_PROCESS
;
1890 audit_arg_signum(u_int signum
)
1892 struct kaudit_record
*ar
;
1898 ar
->k_ar
.ar_arg_signum
= signum
;
1899 ar
->k_ar
.ar_valid_arg
|= ARG_SIGNUM
;
1903 audit_arg_socket(int sodomain
, int sotype
, int soprotocol
)
1906 struct kaudit_record
*ar
;
1912 ar
->k_ar
.ar_arg_sockinfo
.so_domain
= sodomain
;
1913 ar
->k_ar
.ar_arg_sockinfo
.so_type
= sotype
;
1914 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
= soprotocol
;
1915 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
1919 audit_arg_sockaddr(struct proc
*p
, struct sockaddr
*so
)
1921 struct kaudit_record
*ar
;
1924 if (ar
== NULL
|| p
== NULL
|| so
== NULL
)
1927 bcopy(so
, &ar
->k_ar
.ar_arg_sockaddr
, sizeof(ar
->k_ar
.ar_arg_sockaddr
));
1928 switch (so
->sa_family
) {
1930 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET
;
1933 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRINET6
;
1936 audit_arg_upath(p
, ((struct sockaddr_un
*)so
)->sun_path
,
1938 ar
->k_ar
.ar_valid_arg
|= ARG_SADDRUNIX
;
1944 audit_arg_auid(uid_t auid
)
1946 struct kaudit_record
*ar
;
1952 ar
->k_ar
.ar_arg_auid
= auid
;
1953 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
;
1957 audit_arg_auditinfo(const struct auditinfo
*au_info
)
1959 struct kaudit_record
*ar
;
1965 ar
->k_ar
.ar_arg_auid
= au_info
->ai_auid
;
1966 ar
->k_ar
.ar_arg_asid
= au_info
->ai_asid
;
1967 ar
->k_ar
.ar_arg_amask
.am_success
= au_info
->ai_mask
.am_success
;
1968 ar
->k_ar
.ar_arg_amask
.am_failure
= au_info
->ai_mask
.am_failure
;
1969 ar
->k_ar
.ar_arg_termid
.port
= au_info
->ai_termid
.port
;
1970 ar
->k_ar
.ar_arg_termid
.machine
= au_info
->ai_termid
.machine
;
1971 ar
->k_ar
.ar_valid_arg
|= ARG_AUID
| ARG_ASID
| ARG_AMASK
| ARG_TERMID
;
1975 audit_arg_text(const char *text
)
1977 struct kaudit_record
*ar
;
1983 /* Invalidate the text string */
1984 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_TEXT
);
1988 if (ar
->k_ar
.ar_arg_text
== NULL
) {
1989 ar
->k_ar
.ar_arg_text
= (char *)kalloc(MAXPATHLEN
);
1990 if (ar
->k_ar
.ar_arg_text
== NULL
)
1994 strncpy(ar
->k_ar
.ar_arg_text
, text
, MAXPATHLEN
);
1995 ar
->k_ar
.ar_valid_arg
|= ARG_TEXT
;
1999 audit_arg_cmd(int cmd
)
2001 struct kaudit_record
*ar
;
2007 ar
->k_ar
.ar_arg_cmd
= cmd
;
2008 ar
->k_ar
.ar_valid_arg
|= ARG_CMD
;
2012 audit_arg_svipc_cmd(int cmd
)
2014 struct kaudit_record
*ar
;
2020 ar
->k_ar
.ar_arg_svipc_cmd
= cmd
;
2021 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_CMD
;
2025 audit_arg_svipc_perm(const struct ipc_perm
*perm
)
2027 struct kaudit_record
*ar
;
2033 bcopy(perm
, &ar
->k_ar
.ar_arg_svipc_perm
,
2034 sizeof(ar
->k_ar
.ar_arg_svipc_perm
));
2035 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_PERM
;
2039 audit_arg_svipc_id(int id
)
2041 struct kaudit_record
*ar
;
2047 ar
->k_ar
.ar_arg_svipc_id
= id
;
2048 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ID
;
2052 audit_arg_svipc_addr(void * addr
)
2054 struct kaudit_record
*ar
;
2060 ar
->k_ar
.ar_arg_svipc_addr
= addr
;
2061 ar
->k_ar
.ar_valid_arg
|= ARG_SVIPC_ADDR
;
2065 audit_arg_posix_ipc_perm(uid_t uid
, gid_t gid
, mode_t mode
)
2067 struct kaudit_record
*ar
;
2073 ar
->k_ar
.ar_arg_pipc_perm
.pipc_uid
= uid
;
2074 ar
->k_ar
.ar_arg_pipc_perm
.pipc_gid
= gid
;
2075 ar
->k_ar
.ar_arg_pipc_perm
.pipc_mode
= mode
;
2076 ar
->k_ar
.ar_valid_arg
|= ARG_POSIX_IPC_PERM
;
2080 audit_arg_auditon(const union auditon_udata
*udata
)
2082 struct kaudit_record
*ar
;
2088 bcopy((const void *)udata
, &ar
->k_ar
.ar_arg_auditon
,
2089 sizeof(ar
->k_ar
.ar_arg_auditon
));
2090 ar
->k_ar
.ar_valid_arg
|= ARG_AUDITON
;
2094 * Audit information about a file, either the file's vnode info, or its
2095 * socket address info.
2098 audit_arg_file(__unused
struct proc
*p
, const struct fileproc
*fp
)
2100 struct kaudit_record
*ar
;
2104 if (fp
->f_fglob
->fg_type
== DTYPE_VNODE
) {
2105 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2109 if (fp
->f_fglob
->fg_type
== DTYPE_SOCKET
) {
2113 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
2114 if (INP_CHECK_SOCKAF(so
, PF_INET
)) {
2115 if (so
->so_pcb
== NULL
)
2117 ar
->k_ar
.ar_arg_sockinfo
.so_type
=
2119 ar
->k_ar
.ar_arg_sockinfo
.so_domain
=
2121 ar
->k_ar
.ar_arg_sockinfo
.so_protocol
=
2122 so
->so_proto
->pr_protocol
;
2123 pcb
= (struct inpcb
*)so
->so_pcb
;
2124 ar
->k_ar
.ar_arg_sockinfo
.so_raddr
=
2125 pcb
->inp_faddr
.s_addr
;
2126 ar
->k_ar
.ar_arg_sockinfo
.so_laddr
=
2127 pcb
->inp_laddr
.s_addr
;
2128 ar
->k_ar
.ar_arg_sockinfo
.so_rport
=
2130 ar
->k_ar
.ar_arg_sockinfo
.so_lport
=
2132 ar
->k_ar
.ar_valid_arg
|= ARG_SOCKINFO
;
2140 * Store a path as given by the user process for auditing into the audit
2141 * record stored on the user thread. This function will allocate the memory to
2142 * store the path info if not already available. This memory will be
2143 * freed when the audit record is freed.
2146 audit_arg_upath(struct proc
*p
, char *upath
, u_int64_t flags
)
2148 struct kaudit_record
*ar
;
2151 if (p
== NULL
|| upath
== NULL
)
2152 return; /* nothing to do! */
2154 if ((flags
& (ARG_UPATH1
| ARG_UPATH2
)) == 0)
2158 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2161 if (flags
& ARG_UPATH1
) {
2162 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH1
);
2163 pathp
= &ar
->k_ar
.ar_arg_upath1
;
2166 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_UPATH2
);
2167 pathp
= &ar
->k_ar
.ar_arg_upath2
;
2170 if (*pathp
== NULL
) {
2171 *pathp
= (char *)kalloc(MAXPATHLEN
);
2176 if (canon_path(p
, upath
, *pathp
) == 0) {
2177 if (flags
& ARG_UPATH1
)
2178 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH1
;
2180 ar
->k_ar
.ar_valid_arg
|= ARG_UPATH2
;
2182 kfree(*pathp
, MAXPATHLEN
);
2188 * Function to save the path and vnode attr information into the audit
2191 * It is assumed that the caller will hold any vnode locks necessary to
2192 * perform a VNOP_GETATTR() on the passed vnode.
2194 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but
2195 * always provides access to the generation number as we need that
2196 * to construct the BSM file ID.
2197 * XXX: We should accept the process argument from the caller, since
2198 * it's very likely they already have a reference.
2199 * XXX: Error handling in this function is poor.
2202 audit_arg_vnpath(struct vnode
*vp
, u_int64_t flags
)
2204 struct kaudit_record
*ar
;
2205 struct vnode_attr va
;
2209 struct vnode_au_info
*vnp
;
2211 struct vfs_context context
;
2217 if (ar
== NULL
) /* This will be the case for unaudited system calls */
2220 if ((flags
& (ARG_VNODE1
| ARG_VNODE2
)) == 0)
2225 if (flags
& ARG_VNODE1
) {
2226 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH1
);
2227 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE1
);
2228 pathp
= &ar
->k_ar
.ar_arg_kpath1
;
2229 vnp
= &ar
->k_ar
.ar_arg_vnode1
;
2232 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_KPATH2
);
2233 ar
->k_ar
.ar_valid_arg
&= (ARG_ALL
^ ARG_VNODE2
);
2234 pathp
= &ar
->k_ar
.ar_arg_kpath2
;
2235 vnp
= &ar
->k_ar
.ar_arg_vnode2
;
2238 if (*pathp
== NULL
) {
2239 *pathp
= (char *)kalloc(MAXPATHLEN
);
2245 * If vn_getpath() succeeds, place it in a string buffer
2246 * attached to the audit record, and set a flag indicating
2250 if (vn_getpath(vp
, *pathp
, &len
) == 0) {
2251 if (flags
& ARG_VNODE1
)
2252 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH1
;
2254 ar
->k_ar
.ar_valid_arg
|= ARG_KPATH2
;
2256 kfree(*pathp
, MAXPATHLEN
);
2260 context
.vc_proc
= p
;
2261 context
.vc_ucred
= kauth_cred_get();
2264 VATTR_WANTED(&va
, va_mode
);
2265 VATTR_WANTED(&va
, va_uid
);
2266 VATTR_WANTED(&va
, va_gid
);
2267 VATTR_WANTED(&va
, va_rdev
);
2268 VATTR_WANTED(&va
, va_fsid
);
2269 VATTR_WANTED(&va
, va_fileid
);
2270 VATTR_WANTED(&va
, va_gen
);
2271 error
= vnode_getattr(vp
, &va
, &context
);
2273 /* XXX: How to handle this case? */
2277 /* XXX do we want to fall back here when these aren't supported? */
2278 vnp
->vn_mode
= va
.va_mode
;
2279 vnp
->vn_uid
= va
.va_uid
;
2280 vnp
->vn_gid
= va
.va_gid
;
2281 vnp
->vn_dev
= va
.va_rdev
;
2282 vnp
->vn_fsid
= va
.va_fsid
;
2283 vnp
->vn_fileid
= (u_long
)va
.va_fileid
;
2284 vnp
->vn_gen
= va
.va_gen
;
2285 if (flags
& ARG_VNODE1
)
2286 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE1
;
2288 ar
->k_ar
.ar_valid_arg
|= ARG_VNODE2
;
2293 audit_arg_vnpath_withref(struct vnode
*vp
, u_int64_t flags
)
2295 if (vp
== NULL
|| vnode_getwithref(vp
))
2297 audit_arg_vnpath(vp
, flags
);
2298 (void)vnode_put(vp
);
2302 audit_arg_mach_port1(mach_port_name_t port
)
2304 struct kaudit_record
*ar
;
2310 ar
->k_ar
.ar_arg_mach_port1
= port
;
2311 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT1
;
2315 audit_arg_mach_port2(mach_port_name_t port
)
2317 struct kaudit_record
*ar
;
2323 ar
->k_ar
.ar_arg_mach_port2
= port
;
2324 ar
->k_ar
.ar_valid_arg
|= ARG_MACHPORT2
;
2328 * The close() system call uses it's own audit call to capture the
2329 * path/vnode information because those pieces are not easily obtained
2330 * within the system call itself.
2333 audit_sysclose(struct proc
*p
, int fd
)
2335 struct fileproc
*fp
;
2340 if (fp_getfvp(p
, fd
, &fp
, &vp
) != 0)
2343 audit_arg_vnpath_withref((struct vnode
*)fp
->f_fglob
->fg_data
, ARG_VNODE1
);
2356 audit_shutdown(void)
2362 audit(struct proc
*p
, struct audit_args
*uap
, register_t
*retval
)
2368 auditon(struct proc
*p
, struct auditon_args
*uap
, register_t
*retval
)
2374 getauid(struct proc
*p
, struct getauid_args
*uap
, register_t
*retval
)
2380 setauid(struct proc
*p
, struct setauid_args
*uap
, register_t
*retval
)
2386 getaudit(struct proc
*p
, struct getaudit_args
*uap
, register_t
*retval
)
2392 setaudit(struct proc
*p
, struct setaudit_args
*uap
, register_t
*retval
)
2398 getaudit_addr(struct proc
*p
, struct getaudit_addr_args
*uap
, register_t
*retval
)
2404 setaudit_addr(struct proc
*p
, struct setaudit_addr_args
*uap
, register_t
*retval
)
2410 auditctl(struct proc
*p
, struct auditctl_args
*uap
, register_t
*retval
)