2 * Copyright (c) 1999-2019 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
13 * its contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
35 #include <sys/param.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
39 #include <sys/namei.h>
40 #include <sys/proc_internal.h>
41 #include <sys/kauth.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
45 #include <sys/ucred.h>
47 #include <sys/unistd.h>
48 #include <sys/file_internal.h>
49 #include <sys/vnode_internal.h>
51 #include <sys/syscall.h>
52 #include <sys/malloc.h>
54 #include <sys/sysent.h>
55 #include <sys/sysproto.h>
56 #include <sys/vfs_context.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/socketvar.h>
61 #include <bsm/audit.h>
62 #include <bsm/audit_internal.h>
63 #include <bsm/audit_kevents.h>
65 #include <security/audit/audit.h>
66 #include <security/audit/audit_bsd.h>
67 #include <security/audit/audit_private.h>
69 #include <mach/host_priv.h>
70 #include <mach/host_special_ports.h>
71 #include <mach/audit_triggers_server.h>
73 #include <kern/host.h>
74 #include <kern/kalloc.h>
75 #include <kern/zalloc.h>
76 #include <kern/sched_prim.h>
79 #include <bsm/audit_record.h>
80 #include <security/mac.h>
81 #include <security/mac_framework.h>
82 #include <security/mac_policy.h>
85 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_pcb.h>
90 #include <IOKit/IOBSD.h>
94 #define IS_NOT_VALID_PID(p) ((p) < 1 || (p) > PID_MAX)
96 #ifdef AUDIT_API_WARNINGS
98 * Macro to warn about auditinfo_addr_t/auditpinfo_addr_t changing sizes
99 * to encourage the userland code to be recompiled and updated.
101 #define WARN_IF_AINFO_ADDR_CHANGED(sz1, sz2, scall, tp) do { \
102 if ((size_t)(sz1) != (size_t)(sz2)) { \
103 char pn[MAXCOMLEN + 1]; \
105 proc_selfname(pn, MAXCOMLEN + 1); \
106 printf("Size of %s used by %s in %s is different from " \
107 "kernel's. Please recompile %s.\n", (tp), \
113 * Macro to warn about using ASID's outside the range [1 to PID_MAX] to
114 * encourage userland code changes.
116 #define WARN_IF_BAD_ASID(asid, scall) do { \
117 if (((asid) < 1 || (asid) > PID_MAX) && \
118 (asid) != AU_ASSIGN_ASID) { \
119 char pn[MAXCOMLEN + 1]; \
121 proc_selfname(pn, MAXCOMLEN + 1); \
122 printf("%s in %s is using an ASID (%u) outside the " \
123 "range [1 to %d]. Please change %s to use an ASID "\
124 "within this range or use AU_ASSIGN_ASID.\n", \
125 (scall), pn, (uint32_t)(asid), PID_MAX, pn); \
129 #else /* ! AUDIT_API_WARNINGS */
131 #define WARN_IF_AINFO_ADDR_CHANGED(sz1, sz2, scall, tp) do { \
134 #define WARN_IF_BAD_ASID(asid, scall) do { \
137 #endif /* AUDIT_API_WARNINGS */
140 * System call to allow a user space application to submit a BSM audit record
141 * to the kernel for inclusion in the audit log. This function does little
142 * verification on the audit record that is submitted.
144 * XXXAUDIT: Audit preselection for user records does not currently work,
145 * since we pre-select only based on the AUE_audit event type, not the event
146 * type submitted as part of the user audit data.
150 audit(proc_t p
, struct audit_args
*uap
, __unused
int32_t *retval
)
154 void * full_rec
= NULL
;
155 struct kaudit_record
*ar
= NULL
;
156 struct uthread
*uthr
= NULL
;
157 int add_identity_token
= 1;
158 int max_record_length
= MAX_AUDIT_RECORD_SIZE
;
161 struct au_identity_info id_info
= {
164 .signing_id_trunc
= 0,
170 token_t
*id_tok
= NULL
;
171 boolean_t kern_events_allowed
= FALSE
;
173 error
= suser(kauth_cred_get(), &p
->p_acflag
);
176 * If a process is not running as root but is properly
177 * entitled, allow it to audit non-kernel events only.
179 if (!IOTaskHasEntitlement(current_task(),
180 AU_AUDIT_USER_ENTITLEMENT
)) {
184 kern_events_allowed
= TRUE
;
187 mtx_lock(&audit_mtx
);
188 max_record_length
= MIN(audit_qctrl
.aq_bufsz
, MAX_AUDIT_RECORD_SIZE
);
189 mtx_unlock(&audit_mtx
);
191 if (IOTaskHasEntitlement(current_task(),
192 AU_CLASS_RESERVED_ENTITLEMENT
)) {
193 /* Entitled tasks are trusted to add appropriate identity info */
194 add_identity_token
= 0;
197 * If the caller is unentitled, an identity token will be added and
198 * the space must be accounted for
200 max_record_length
-= MAX_AUDIT_IDENTITY_SIZE
;
203 if ((uap
->length
<= 0) || (uap
->length
> max_record_length
)) {
211 * If there's no current audit record (audit() itself not audited)
212 * commit the user audit record.
217 /* can this happen? */
223 * This is not very efficient; we're required to allocate a
224 * complete kernel audit record just so the user record can
227 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
228 if (uthr
->uu_ar
== NULL
) {
235 rec
= malloc(uap
->length
, M_AUDITDATA
, M_WAITOK
);
241 error
= copyin(uap
->record
, rec
, uap
->length
);
247 error
= mac_system_check_audit(kauth_cred_get(), rec
, uap
->length
);
253 /* Verify the record. */
254 if (bsm_rec_verify(rec
, uap
->length
, kern_events_allowed
) == 0) {
259 if (add_identity_token
) {
260 struct hdr_tok_partial
*hdr
;
261 struct trl_tok_partial
*trl
;
262 int bytes_copied
= 0;
264 /* Create a new identity token for this buffer */
265 audit_identity_info_construct(&id_info
);
266 id_tok
= au_to_identity(id_info
.signer_type
, id_info
.signing_id
,
267 id_info
.signing_id_trunc
, id_info
.team_id
, id_info
.team_id_trunc
,
268 id_info
.cdhash
, id_info
.cdhash_len
);
274 /* Splice the record together using a new buffer */
275 full_rec
= malloc(uap
->length
+ id_tok
->len
, M_AUDITDATA
, M_WAITOK
);
281 /* Copy the original buffer up to but not including the trailer */
282 memcpy(full_rec
, rec
, uap
->length
- AUDIT_TRAILER_SIZE
);
283 bytes_copied
= uap
->length
- AUDIT_TRAILER_SIZE
;
285 /* Copy the identity token */
286 memcpy(full_rec
+ bytes_copied
, id_tok
->t_data
, id_tok
->len
);
287 bytes_copied
+= id_tok
->len
;
289 /* Copy the old trailer */
290 memcpy(full_rec
+ bytes_copied
,
291 rec
+ (uap
->length
- AUDIT_TRAILER_SIZE
), AUDIT_TRAILER_SIZE
);
292 bytes_copied
+= AUDIT_TRAILER_SIZE
;
294 /* Fix the record size stored in the header token */
295 hdr
= (struct hdr_tok_partial
*)full_rec
;
296 hdr
->len
= htonl(bytes_copied
);
298 /* Fix the record size stored in the trailer token */
299 trl
= (struct trl_tok_partial
*)
300 (full_rec
+ bytes_copied
- AUDIT_TRAILER_SIZE
);
301 trl
->len
= htonl(bytes_copied
);
311 * Attach the user audit record to the kernel audit record. Because
312 * this system call is an auditable event, we will write the user
313 * record along with the record for this audit event.
315 * XXXAUDIT: KASSERT appropriate starting values of k_udata, k_ulen,
316 * k_ar_commit & AR_COMMIT_USER?
320 ar
->k_ar_commit
|= AR_COMMIT_USER
;
323 * Currently we assume that all preselection has been performed in
324 * userspace. We unconditionally set these masks so that the records
325 * get committed both to the trail and pipe. In the future we will
326 * want to setup kernel based preselection.
328 ar
->k_ar_commit
|= (AR_PRESELECT_USER_TRAIL
| AR_PRESELECT_USER_PIPE
);
332 * If rec was allocated, it must be freed if an identity token was added
333 * (since full_rec will be used) OR there was an error (since nothing
334 * will be attached to the kernel structure).
336 if (rec
&& (add_identity_token
|| error
)) {
337 free(rec
, M_AUDITDATA
);
340 /* Only free full_rec if an error occurred */
341 if (full_rec
&& error
) {
342 free(full_rec
, M_AUDITDATA
);
345 audit_identity_info_destruct(&id_info
);
347 if (id_tok
->t_data
) {
348 free(id_tok
->t_data
, M_AUDITBSM
);
350 free(id_tok
, M_AUDITBSM
);
357 * System call to manipulate auditing.
361 auditon(proc_t p
, struct auditon_args
*uap
, __unused
int32_t *retval
)
365 union auditon_udata udata
;
366 proc_t tp
= PROC_NULL
;
367 struct auditinfo_addr aia
;
369 AUDIT_ARG(cmd
, uap
->cmd
);
372 error
= mac_system_check_auditon(kauth_cred_get(), uap
->cmd
);
378 if ((uap
->length
<= 0) || (uap
->length
>
379 (int)sizeof(union auditon_udata
))) {
383 memset((void *)&udata
, 0, sizeof(udata
));
386 * Some of the GET commands use the arguments too.
405 case A_GETPINFO_ADDR
:
407 case A_GETSINFO_ADDR
:
412 error
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
416 AUDIT_ARG(auditon
, &udata
);
417 AUDIT_ARG(len
, uap
->length
);
421 /* Check appropriate privilege. */
424 * A_GETSINFO doesn't require priviledge but only superuser
425 * gets to see the audit masks.
427 case A_GETSINFO_ADDR
:
428 if ((sizeof(udata
.au_kau_info
) != uap
->length
) ||
429 (audit_session_lookup(udata
.au_kau_info
.ai_asid
,
430 &udata
.au_kau_info
) != 0)) {
432 } else if (!kauth_cred_issuser(kauth_cred_get())) {
433 udata
.au_kau_info
.ai_mask
.am_success
= ~0;
434 udata
.au_kau_info
.ai_mask
.am_failure
= ~0;
439 /* Getting one's own audit session flags requires no
440 * privilege. Setting the flags is subject to access
441 * control implemented in audit_session_setaia().
446 if (!IOTaskHasEntitlement(current_task(),
447 AU_CLASS_RESERVED_ENTITLEMENT
)) {
452 error
= suser(kauth_cred_get(), &p
->p_acflag
);
460 * If the audit subsytem is in external control mode, additional
461 * privilege checks are required for a subset of auditon commands
463 if (audit_ctl_mode
== AUDIT_CTLMODE_EXTERNAL
) {
469 if (!IOTaskHasEntitlement(current_task(),
470 AU_CLASS_RESERVED_ENTITLEMENT
)) {
481 * XXX Need to implement these commands by accessing the global
482 * values associated with the commands.
487 if (sizeof(udata
.au_policy64
) == uap
->length
) {
488 mtx_lock(&audit_mtx
);
489 if (!audit_fail_stop
) {
490 udata
.au_policy64
|= AUDIT_CNT
;
492 if (audit_panic_on_write_fail
) {
493 udata
.au_policy64
|= AUDIT_AHLT
;
496 udata
.au_policy64
|= AUDIT_ARGV
;
499 udata
.au_policy64
|= AUDIT_ARGE
;
501 mtx_unlock(&audit_mtx
);
504 if (sizeof(udata
.au_policy
) != uap
->length
) {
507 mtx_lock(&audit_mtx
);
508 if (!audit_fail_stop
) {
509 udata
.au_policy
|= AUDIT_CNT
;
511 if (audit_panic_on_write_fail
) {
512 udata
.au_policy
|= AUDIT_AHLT
;
515 udata
.au_policy
|= AUDIT_ARGV
;
518 udata
.au_policy
|= AUDIT_ARGE
;
520 mtx_unlock(&audit_mtx
);
525 if (sizeof(udata
.au_policy64
) == uap
->length
) {
526 if (udata
.au_policy64
& ~(AUDIT_CNT
| AUDIT_AHLT
|
527 AUDIT_ARGV
| AUDIT_ARGE
)) {
530 mtx_lock(&audit_mtx
);
531 audit_fail_stop
= ((udata
.au_policy64
& AUDIT_CNT
) ==
533 audit_panic_on_write_fail
= (udata
.au_policy64
&
535 audit_argv
= (udata
.au_policy64
& AUDIT_ARGV
);
536 audit_arge
= (udata
.au_policy64
& AUDIT_ARGE
);
537 mtx_unlock(&audit_mtx
);
540 if ((sizeof(udata
.au_policy
) != uap
->length
) ||
541 (udata
.au_policy
& ~(AUDIT_CNT
| AUDIT_AHLT
| AUDIT_ARGV
|
546 * XXX - Need to wake up waiters if the policy relaxes?
548 mtx_lock(&audit_mtx
);
549 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
550 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
551 audit_argv
= (udata
.au_policy
& AUDIT_ARGV
);
552 audit_arge
= (udata
.au_policy
& AUDIT_ARGE
);
553 mtx_unlock(&audit_mtx
);
557 if (sizeof(udata
.au_mask
) != uap
->length
) {
560 mtx_lock(&audit_mtx
);
561 udata
.au_mask
= audit_nae_mask
;
562 mtx_unlock(&audit_mtx
);
566 if (sizeof(udata
.au_mask
) != uap
->length
) {
569 mtx_lock(&audit_mtx
);
570 audit_nae_mask
= udata
.au_mask
;
571 AUDIT_CHECK_IF_KEVENTS_MASK(audit_nae_mask
);
572 mtx_unlock(&audit_mtx
);
577 if (sizeof(udata
.au_qctrl64
) == uap
->length
) {
578 mtx_lock(&audit_mtx
);
579 udata
.au_qctrl64
.aq64_hiwater
=
580 (u_int64_t
)audit_qctrl
.aq_hiwater
;
581 udata
.au_qctrl64
.aq64_lowater
=
582 (u_int64_t
)audit_qctrl
.aq_lowater
;
583 udata
.au_qctrl64
.aq64_bufsz
=
584 (u_int64_t
)audit_qctrl
.aq_bufsz
;
585 udata
.au_qctrl64
.aq64_delay
=
586 (u_int64_t
)audit_qctrl
.aq_delay
;
587 udata
.au_qctrl64
.aq64_minfree
=
588 (int64_t)audit_qctrl
.aq_minfree
;
589 mtx_unlock(&audit_mtx
);
592 if (sizeof(udata
.au_qctrl
) != uap
->length
) {
595 mtx_lock(&audit_mtx
);
596 udata
.au_qctrl
= audit_qctrl
;
597 mtx_unlock(&audit_mtx
);
602 if (sizeof(udata
.au_qctrl64
) == uap
->length
) {
603 if ((udata
.au_qctrl64
.aq64_hiwater
> AQ_MAXHIGH
) ||
604 (udata
.au_qctrl64
.aq64_lowater
>=
605 udata
.au_qctrl64
.aq64_hiwater
) ||
606 (udata
.au_qctrl64
.aq64_bufsz
> AQ_MAXBUFSZ
) ||
607 (udata
.au_qctrl64
.aq64_minfree
< 0) ||
608 (udata
.au_qctrl64
.aq64_minfree
> 100)) {
611 mtx_lock(&audit_mtx
);
612 audit_qctrl
.aq_hiwater
=
613 (int)udata
.au_qctrl64
.aq64_hiwater
;
614 audit_qctrl
.aq_lowater
=
615 (int)udata
.au_qctrl64
.aq64_lowater
;
616 audit_qctrl
.aq_bufsz
=
617 (int)udata
.au_qctrl64
.aq64_bufsz
;
618 audit_qctrl
.aq_minfree
=
619 (int)udata
.au_qctrl64
.aq64_minfree
;
620 audit_qctrl
.aq_delay
= -1; /* Not used. */
621 mtx_unlock(&audit_mtx
);
624 if ((sizeof(udata
.au_qctrl
) != uap
->length
) ||
625 (udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
626 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
627 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
628 (udata
.au_qctrl
.aq_minfree
< 0) ||
629 (udata
.au_qctrl
.aq_minfree
> 100)) {
633 mtx_lock(&audit_mtx
);
634 audit_qctrl
= udata
.au_qctrl
;
635 /* XXX The queue delay value isn't used with the kernel. */
636 audit_qctrl
.aq_delay
= -1;
637 mtx_unlock(&audit_mtx
);
660 if (sizeof(udata
.au_cond64
) == uap
->length
) {
661 mtx_lock(&audit_mtx
);
662 if (audit_enabled
&& !audit_suspended
) {
663 udata
.au_cond64
= AUC_AUDITING
;
665 udata
.au_cond64
= AUC_NOAUDIT
;
667 mtx_unlock(&audit_mtx
);
670 if (sizeof(udata
.au_cond
) != uap
->length
) {
673 mtx_lock(&audit_mtx
);
674 if (audit_enabled
&& !audit_suspended
) {
675 udata
.au_cond
= AUC_AUDITING
;
677 udata
.au_cond
= AUC_NOAUDIT
;
679 mtx_unlock(&audit_mtx
);
684 if (sizeof(udata
.au_cond64
) == uap
->length
) {
685 mtx_lock(&audit_mtx
);
686 if (udata
.au_cond64
== AUC_NOAUDIT
) {
689 if (udata
.au_cond64
== AUC_AUDITING
) {
692 if (udata
.au_cond64
== AUC_DISABLED
) {
694 mtx_unlock(&audit_mtx
);
698 mtx_unlock(&audit_mtx
);
701 if (sizeof(udata
.au_cond
) != uap
->length
) {
704 mtx_lock(&audit_mtx
);
705 if (udata
.au_cond
== AUC_NOAUDIT
) {
708 if (udata
.au_cond
== AUC_AUDITING
) {
711 if (udata
.au_cond
== AUC_DISABLED
) {
713 mtx_unlock(&audit_mtx
);
717 mtx_unlock(&audit_mtx
);
721 if (sizeof(udata
.au_evclass
) != uap
->length
) {
724 udata
.au_evclass
.ec_class
= au_event_class(
725 udata
.au_evclass
.ec_number
);
729 if (sizeof(udata
.au_evclass
) != uap
->length
) {
732 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
733 udata
.au_evclass
.ec_class
);
737 if ((sizeof(udata
.au_aupinfo
) != uap
->length
) ||
738 IS_NOT_VALID_PID(udata
.au_aupinfo
.ap_pid
)) {
741 if ((tp
= proc_find(udata
.au_aupinfo
.ap_pid
)) == NULL
) {
745 scred
= kauth_cred_proc_ref(tp
);
746 if (scred
->cr_audit
.as_aia_p
->ai_termid
.at_type
== AU_IPv6
) {
747 kauth_cred_unref(&scred
);
752 udata
.au_aupinfo
.ap_auid
=
753 scred
->cr_audit
.as_aia_p
->ai_auid
;
754 udata
.au_aupinfo
.ap_mask
.am_success
=
755 scred
->cr_audit
.as_mask
.am_success
;
756 udata
.au_aupinfo
.ap_mask
.am_failure
=
757 scred
->cr_audit
.as_mask
.am_failure
;
758 udata
.au_aupinfo
.ap_termid
.machine
=
759 scred
->cr_audit
.as_aia_p
->ai_termid
.at_addr
[0];
760 udata
.au_aupinfo
.ap_termid
.port
=
761 scred
->cr_audit
.as_aia_p
->ai_termid
.at_port
;
762 udata
.au_aupinfo
.ap_asid
=
763 scred
->cr_audit
.as_aia_p
->ai_asid
;
764 kauth_cred_unref(&scred
);
770 if ((sizeof(udata
.au_aupinfo
) != uap
->length
) ||
771 IS_NOT_VALID_PID(udata
.au_aupinfo
.ap_pid
)) {
774 if ((tp
= proc_find(udata
.au_aupinfo
.ap_pid
)) == NULL
) {
777 scred
= kauth_cred_proc_ref(tp
);
778 bcopy(scred
->cr_audit
.as_aia_p
, &aia
, sizeof(aia
));
779 kauth_cred_unref(&scred
);
780 aia
.ai_mask
.am_success
=
781 udata
.au_aupinfo
.ap_mask
.am_success
;
782 aia
.ai_mask
.am_failure
=
783 udata
.au_aupinfo
.ap_mask
.am_failure
;
784 AUDIT_CHECK_IF_KEVENTS_MASK(aia
.ai_mask
);
785 error
= audit_session_setaia(tp
, &aia
);
794 if ((sizeof(udata
.au_fstat
) != uap
->length
) ||
795 ((udata
.au_fstat
.af_filesz
!= 0) &&
796 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
))) {
799 mtx_lock(&audit_mtx
);
800 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
801 mtx_unlock(&audit_mtx
);
805 if (sizeof(udata
.au_fstat
) != uap
->length
) {
808 mtx_lock(&audit_mtx
);
809 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
810 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
811 mtx_unlock(&audit_mtx
);
814 case A_GETPINFO_ADDR
:
815 if ((sizeof(udata
.au_aupinfo_addr
) != uap
->length
) ||
816 IS_NOT_VALID_PID(udata
.au_aupinfo_addr
.ap_pid
)) {
819 if ((tp
= proc_find(udata
.au_aupinfo
.ap_pid
)) == NULL
) {
822 WARN_IF_AINFO_ADDR_CHANGED(uap
->length
,
823 sizeof(auditpinfo_addr_t
), "auditon(A_GETPINFO_ADDR,...)",
824 "auditpinfo_addr_t");
825 scred
= kauth_cred_proc_ref(tp
);
826 udata
.au_aupinfo_addr
.ap_auid
=
827 scred
->cr_audit
.as_aia_p
->ai_auid
;
828 udata
.au_aupinfo_addr
.ap_asid
=
829 scred
->cr_audit
.as_aia_p
->ai_asid
;
830 udata
.au_aupinfo_addr
.ap_mask
.am_success
=
831 scred
->cr_audit
.as_mask
.am_success
;
832 udata
.au_aupinfo_addr
.ap_mask
.am_failure
=
833 scred
->cr_audit
.as_mask
.am_failure
;
834 bcopy(&scred
->cr_audit
.as_aia_p
->ai_termid
,
835 &udata
.au_aupinfo_addr
.ap_termid
,
836 sizeof(au_tid_addr_t
));
837 udata
.au_aupinfo_addr
.ap_flags
=
838 scred
->cr_audit
.as_aia_p
->ai_flags
;
839 kauth_cred_unref(&scred
);
845 if (sizeof(udata
.au_kau_info
) != uap
->length
) {
848 audit_get_kinfo(&udata
.au_kau_info
);
852 if ((sizeof(udata
.au_kau_info
) != uap
->length
) ||
853 (udata
.au_kau_info
.ai_termid
.at_type
!= AU_IPv4
&&
854 udata
.au_kau_info
.ai_termid
.at_type
!= AU_IPv6
)) {
857 audit_set_kinfo(&udata
.au_kau_info
);
861 if ((sizeof(udata
.au_trigger
) != uap
->length
) ||
862 (udata
.au_trigger
< AUDIT_TRIGGER_MIN
) ||
863 (udata
.au_trigger
> AUDIT_TRIGGER_MAX
)) {
866 return audit_send_trigger(udata
.au_trigger
);
868 case A_GETSINFO_ADDR
:
869 /* Handled above before switch(). */
873 if (sizeof(udata
.au_flags
) != uap
->length
) {
876 bcopy(&(kauth_cred_get()->cr_audit
.as_aia_p
->ai_flags
),
877 &udata
.au_flags
, sizeof(udata
.au_flags
));
881 if (sizeof(udata
.au_flags
) != uap
->length
) {
884 bcopy(kauth_cred_get()->cr_audit
.as_aia_p
, &aia
, sizeof(aia
));
885 aia
.ai_flags
= udata
.au_flags
;
886 error
= audit_session_setaia(p
, &aia
);
893 if (sizeof(udata
.au_ctl_mode
) != uap
->length
) {
896 mtx_lock(&audit_mtx
);
897 udata
.au_ctl_mode
= audit_ctl_mode
;
898 mtx_unlock(&audit_mtx
);
902 if (sizeof(udata
.au_ctl_mode
) != uap
->length
) {
906 mtx_lock(&audit_mtx
);
908 if (udata
.au_ctl_mode
== AUDIT_CTLMODE_NORMAL
) {
909 audit_ctl_mode
= AUDIT_CTLMODE_NORMAL
;
910 } else if (udata
.au_ctl_mode
== AUDIT_CTLMODE_EXTERNAL
) {
911 audit_ctl_mode
= AUDIT_CTLMODE_EXTERNAL
;
913 mtx_unlock(&audit_mtx
);
917 mtx_unlock(&audit_mtx
);
921 if (sizeof(udata
.au_expire_after
) != uap
->length
) {
924 mtx_lock(&audit_mtx
);
925 udata
.au_expire_after
.age
= audit_expire_after
.age
;
926 udata
.au_expire_after
.size
= audit_expire_after
.size
;
927 udata
.au_expire_after
.op_type
= audit_expire_after
.op_type
;
928 mtx_unlock(&audit_mtx
);
932 if (sizeof(udata
.au_expire_after
) != uap
->length
) {
935 mtx_lock(&audit_mtx
);
936 audit_expire_after
.age
= udata
.au_expire_after
.age
;
937 audit_expire_after
.size
= udata
.au_expire_after
.size
;
938 audit_expire_after
.op_type
= udata
.au_expire_after
.op_type
;
939 mtx_unlock(&audit_mtx
);
947 * Copy data back to userspace for the GET comands.
963 case A_GETPINFO_ADDR
:
965 case A_GETSINFO_ADDR
:
969 error
= copyout((void *)&udata
, uap
->data
, uap
->length
);
980 * System calls to manage the user audit information.
984 getauid(proc_t p
, struct getauid_args
*uap
, __unused
int32_t *retval
)
991 error
= mac_proc_check_getauid(p
);
996 scred
= kauth_cred_proc_ref(p
);
997 id
= scred
->cr_audit
.as_aia_p
->ai_auid
;
998 kauth_cred_unref(&scred
);
1000 error
= copyout((void *)&id
, uap
->auid
, sizeof(id
));
1010 setauid(proc_t p
, struct setauid_args
*uap
, __unused
int32_t *retval
)
1015 struct auditinfo_addr aia
;
1017 error
= copyin(uap
->auid
, &id
, sizeof(id
));
1021 AUDIT_ARG(auid
, id
);
1024 error
= mac_proc_check_setauid(p
, id
);
1030 scred
= kauth_cred_proc_ref(p
);
1031 error
= suser(scred
, &p
->p_acflag
);
1033 kauth_cred_unref(&scred
);
1037 bcopy(scred
->cr_audit
.as_aia_p
, &aia
, sizeof(aia
));
1038 if (aia
.ai_asid
== AU_DEFAUDITSID
) {
1039 aia
.ai_asid
= AU_ASSIGN_ASID
;
1041 bcopy(&scred
->cr_audit
.as_mask
, &aia
.ai_mask
, sizeof(au_mask_t
));
1042 kauth_cred_unref(&scred
);
1044 error
= audit_session_setaia(p
, &aia
);
1050 getaudit_addr_internal(proc_t p
, user_addr_t user_addr
, size_t length
)
1053 auditinfo_addr_t aia
;
1055 scred
= kauth_cred_proc_ref(p
);
1056 bcopy(scred
->cr_audit
.as_aia_p
, &aia
, sizeof(auditinfo_addr_t
));
1058 * Only superuser gets to see the real mask.
1060 if (suser(scred
, &p
->p_acflag
)) {
1061 aia
.ai_mask
.am_success
= ~0;
1062 aia
.ai_mask
.am_failure
= ~0;
1064 kauth_cred_unref(&scred
);
1066 return copyout(&aia
, user_addr
, min(sizeof(aia
), length
));
1071 getaudit_addr(proc_t p
, struct getaudit_addr_args
*uap
,
1072 __unused
int32_t *retval
)
1075 int error
= mac_proc_check_getaudit(p
);
1080 #endif /* CONFIG_MACF */
1081 WARN_IF_AINFO_ADDR_CHANGED(uap
->length
, sizeof(auditinfo_addr_t
),
1082 "getaudit_addr(2)", "auditinfo_addr_t");
1084 return getaudit_addr_internal(p
, uap
->auditinfo_addr
, uap
->length
);
1089 setaudit_addr(proc_t p
, struct setaudit_addr_args
*uap
,
1090 __unused
int32_t *retval
)
1092 struct auditinfo_addr aia
;
1096 bzero(&aia
, sizeof(auditinfo_addr_t
));
1097 error
= copyin(uap
->auditinfo_addr
, &aia
,
1098 min(sizeof(aia
), uap
->length
));
1102 AUDIT_ARG(auditinfo_addr
, &aia
);
1103 if (aia
.ai_termid
.at_type
!= AU_IPv6
&&
1104 aia
.ai_termid
.at_type
!= AU_IPv4
) {
1107 if (aia
.ai_asid
!= AU_ASSIGN_ASID
&&
1108 (uint32_t)aia
.ai_asid
> ASSIGNED_ASID_MAX
) {
1113 error
= mac_proc_check_setaudit(p
, &aia
);
1119 scred
= kauth_cred_proc_ref(p
);
1120 error
= suser(scred
, &p
->p_acflag
);
1122 kauth_cred_unref(&scred
);
1126 WARN_IF_AINFO_ADDR_CHANGED(uap
->length
, sizeof(auditinfo_addr_t
),
1127 "setaudit_addr(2)", "auditinfo_addr_t");
1128 WARN_IF_BAD_ASID(aia
.ai_asid
, "setaudit_addr(2)");
1129 kauth_cred_unref(&scred
);
1131 AUDIT_CHECK_IF_KEVENTS_MASK(aia
.ai_mask
);
1132 if (aia
.ai_asid
== AU_DEFAUDITSID
) {
1133 aia
.ai_asid
= AU_ASSIGN_ASID
;
1136 error
= audit_session_setaia(p
, &aia
);
1142 * If asked to assign an ASID then let the user know what the ASID is
1143 * by copying the auditinfo_addr struct back out.
1145 if (aia
.ai_asid
== AU_ASSIGN_ASID
) {
1146 error
= getaudit_addr_internal(p
, uap
->auditinfo_addr
,
1154 * Syscall to manage audit files.
1159 auditctl(proc_t p
, struct auditctl_args
*uap
, __unused
int32_t *retval
)
1161 struct nameidata nd
;
1165 au_ctlmode_t ctlmode
;
1167 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1172 ctlmode
= audit_ctl_mode
;
1175 * Do not allow setting of a path when auditing is in reserved mode
1177 if (ctlmode
== AUDIT_CTLMODE_EXTERNAL
&&
1178 !IOTaskHasEntitlement(current_task(), AU_AUDITCTL_RESERVED_ENTITLEMENT
)) {
1186 * If a path is specified, open the replacement vnode, perform
1187 * validity checks, and grab another reference to the current
1190 * XXX Changes API slightly. NULL path no longer disables audit but
1193 if (uap
->path
== USER_ADDR_NULL
) {
1197 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1198 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
:
1199 UIO_USERSPACE32
), uap
->path
, vfs_context_current());
1200 error
= vn_open(&nd
, AUDIT_OPEN_FLAGS
, 0);
1207 * Accessibility of the vnode was determined in vn_open; the
1208 * mac_system_check_auditctl should only determine whether that vnode
1209 * is appropriate for storing audit data, or that the caller was
1210 * permitted to control the auditing system at all. For example, a
1211 * confidentiality policy may want to ensure that audit files are
1212 * always high sensitivity.
1214 error
= mac_system_check_auditctl(kauth_cred_get(), vp
);
1216 vn_close(vp
, AUDIT_CLOSE_FLAGS
, vfs_context_current());
1221 if (vp
->v_type
!= VREG
) {
1222 vn_close(vp
, AUDIT_CLOSE_FLAGS
, vfs_context_current());
1226 mtx_lock(&audit_mtx
);
1228 * XXXAUDIT: Should audit_suspended actually be cleared by
1231 audit_suspended
= 0;
1232 mtx_unlock(&audit_mtx
);
1235 * The following gets unreferenced in audit_rotate_vnode()
1236 * after the rotation and it is no longer needed.
1238 cred
= kauth_cred_get_with_ref();
1239 audit_rotate_vnode(cred
, vp
);
1245 #else /* !CONFIG_AUDIT */
1248 audit(proc_t p
, struct audit_args
*uap
, int32_t *retval
)
1250 #pragma unused(p, uap, retval)
1256 auditon(proc_t p
, struct auditon_args
*uap
, int32_t *retval
)
1258 #pragma unused(p, uap, retval)
1264 getauid(proc_t p
, struct getauid_args
*uap
, int32_t *retval
)
1266 #pragma unused(p, uap, retval)
1272 setauid(proc_t p
, struct setauid_args
*uap
, int32_t *retval
)
1274 #pragma unused(p, uap, retval)
1280 getaudit_addr(proc_t p
, struct getaudit_addr_args
*uap
, int32_t *retval
)
1282 #pragma unused(p, uap, retval)
1288 setaudit_addr(proc_t p
, struct setaudit_addr_args
*uap
, int32_t *retval
)
1290 #pragma unused(p, uap, retval)
1296 auditctl(proc_t p
, struct auditctl_args
*uap
, int32_t *retval
)
1298 #pragma unused(p, uap, retval)
1303 #endif /* CONFIG_AUDIT */