2 * Copyright (c) 1999-2010, Apple Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
31 * support for mandatory and extensible security protections. This notice
32 * is included in support of clause 2.2 (b) of the Apple Public License,
36 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>
40 #include <sys/namei.h>
41 #include <sys/proc_internal.h>
42 #include <sys/kauth.h>
43 #include <sys/queue.h>
44 #include <sys/systm.h>
46 #include <sys/ucred.h>
48 #include <sys/unistd.h>
49 #include <sys/file_internal.h>
50 #include <sys/vnode_internal.h>
52 #include <sys/syscall.h>
53 #include <sys/malloc.h>
55 #include <sys/sysent.h>
56 #include <sys/sysproto.h>
57 #include <sys/vfs_context.h>
58 #include <sys/domain.h>
59 #include <sys/protosw.h>
60 #include <sys/socketvar.h>
62 #include <bsm/audit.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/lock.h>
77 #include <kern/wait_queue.h>
78 #include <kern/sched_prim.h>
81 #include <bsm/audit_record.h>
82 #include <security/mac.h>
83 #include <security/mac_framework.h>
84 #include <security/mac_policy.h>
87 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_pcb.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 struct kaudit_record
*ar
;
155 struct uthread
*uthr
;
157 error
= suser(kauth_cred_get(), &p
->p_acflag
);
161 mtx_lock(&audit_mtx
);
162 if ((uap
->length
<= 0) || (uap
->length
> (int)audit_qctrl
.aq_bufsz
)) {
163 mtx_unlock(&audit_mtx
);
166 mtx_unlock(&audit_mtx
);
171 * If there's no current audit record (audit() itself not audited)
172 * commit the user audit record.
176 if (uthr
== NULL
) /* can this happen? */
180 * This is not very efficient; we're required to allocate a
181 * complete kernel audit record just so the user record can
184 uthr
->uu_ar
= audit_new(AUE_NULL
, p
, uthr
);
185 if (uthr
->uu_ar
== NULL
)
190 if (uap
->length
> MAX_AUDIT_RECORD_SIZE
)
193 rec
= malloc(uap
->length
, M_AUDITDATA
, M_WAITOK
);
195 error
= copyin(uap
->record
, rec
, uap
->length
);
200 error
= mac_system_check_audit(kauth_cred_get(), rec
, uap
->length
);
205 /* Verify the record. */
206 if (bsm_rec_verify(rec
) == 0) {
212 * Attach the user audit record to the kernel audit record. Because
213 * this system call is an auditable event, we will write the user
214 * record along with the record for this audit event.
216 * XXXAUDIT: KASSERT appropriate starting values of k_udata, k_ulen,
217 * k_ar_commit & AR_COMMIT_USER?
220 ar
->k_ulen
= uap
->length
;
221 ar
->k_ar_commit
|= AR_COMMIT_USER
;
224 * Currently we assume that all preselection has been performed in
225 * userspace. We unconditionally set these masks so that the records
226 * get committed both to the trail and pipe. In the future we will
227 * want to setup kernel based preselection.
229 ar
->k_ar_commit
|= (AR_PRESELECT_USER_TRAIL
| AR_PRESELECT_USER_PIPE
);
234 * audit_syscall_exit() will free the audit record on the thread even
235 * if we allocated it above.
237 free(rec
, M_AUDITDATA
);
242 * System call to manipulate auditing.
246 auditon(proc_t p
, struct auditon_args
*uap
, __unused
int32_t *retval
)
250 union auditon_udata udata
;
251 proc_t tp
= PROC_NULL
;
252 struct auditinfo_addr aia
;
254 AUDIT_ARG(cmd
, uap
->cmd
);
257 error
= mac_system_check_auditon(kauth_cred_get(), uap
->cmd
);
262 if ((uap
->length
<= 0) || (uap
->length
>
263 (int)sizeof(union auditon_udata
)))
266 memset((void *)&udata
, 0, sizeof(udata
));
269 * Some of the GET commands use the arguments too.
288 case A_GETPINFO_ADDR
:
290 case A_GETSINFO_ADDR
:
293 error
= copyin(uap
->data
, (void *)&udata
, uap
->length
);
296 AUDIT_ARG(auditon
, &udata
);
297 AUDIT_ARG(len
, uap
->length
);
301 /* Check appropriate privilege. */
304 * A_GETSINFO doesn't require priviledge but only superuser
305 * gets to see the audit masks.
307 case A_GETSINFO_ADDR
:
308 if ((sizeof(udata
.au_kau_info
) != uap
->length
) ||
309 (audit_session_lookup(udata
.au_kau_info
.ai_asid
,
310 &udata
.au_kau_info
) != 0))
312 else if (!kauth_cred_issuser(kauth_cred_get())) {
313 udata
.au_kau_info
.ai_mask
.am_success
= ~0;
314 udata
.au_kau_info
.ai_mask
.am_failure
= ~0;
319 /* Getting one's own audit session flags requires no
320 * privilege. Setting the flags is subject to access
321 * control implemented in audit_session_setaia().
325 error
= suser(kauth_cred_get(), &p
->p_acflag
);
332 * XXX Need to implement these commands by accessing the global
333 * values associated with the commands.
338 if (sizeof(udata
.au_policy64
) == uap
->length
) {
339 mtx_lock(&audit_mtx
);
340 if (!audit_fail_stop
)
341 udata
.au_policy64
|= AUDIT_CNT
;
342 if (audit_panic_on_write_fail
)
343 udata
.au_policy64
|= AUDIT_AHLT
;
345 udata
.au_policy64
|= AUDIT_ARGV
;
347 udata
.au_policy64
|= AUDIT_ARGE
;
348 mtx_unlock(&audit_mtx
);
351 if (sizeof(udata
.au_policy
) != uap
->length
)
353 mtx_lock(&audit_mtx
);
354 if (!audit_fail_stop
)
355 udata
.au_policy
|= AUDIT_CNT
;
356 if (audit_panic_on_write_fail
)
357 udata
.au_policy
|= AUDIT_AHLT
;
359 udata
.au_policy
|= AUDIT_ARGV
;
361 udata
.au_policy
|= AUDIT_ARGE
;
362 mtx_unlock(&audit_mtx
);
367 if (sizeof(udata
.au_policy64
) == uap
->length
) {
368 if (udata
.au_policy64
& ~(AUDIT_CNT
|AUDIT_AHLT
|
369 AUDIT_ARGV
|AUDIT_ARGE
))
371 mtx_lock(&audit_mtx
);
372 audit_fail_stop
= ((udata
.au_policy64
& AUDIT_CNT
) ==
374 audit_panic_on_write_fail
= (udata
.au_policy64
&
376 audit_argv
= (udata
.au_policy64
& AUDIT_ARGV
);
377 audit_arge
= (udata
.au_policy64
& AUDIT_ARGE
);
378 mtx_unlock(&audit_mtx
);
381 if ((sizeof(udata
.au_policy
) != uap
->length
) ||
382 (udata
.au_policy
& ~(AUDIT_CNT
|AUDIT_AHLT
|AUDIT_ARGV
|
386 * XXX - Need to wake up waiters if the policy relaxes?
388 mtx_lock(&audit_mtx
);
389 audit_fail_stop
= ((udata
.au_policy
& AUDIT_CNT
) == 0);
390 audit_panic_on_write_fail
= (udata
.au_policy
& AUDIT_AHLT
);
391 audit_argv
= (udata
.au_policy
& AUDIT_ARGV
);
392 audit_arge
= (udata
.au_policy
& AUDIT_ARGE
);
393 mtx_unlock(&audit_mtx
);
397 if (sizeof(udata
.au_mask
) != uap
->length
)
399 mtx_lock(&audit_mtx
);
400 udata
.au_mask
= audit_nae_mask
;
401 mtx_unlock(&audit_mtx
);
405 if (sizeof(udata
.au_mask
) != uap
->length
)
407 mtx_lock(&audit_mtx
);
408 audit_nae_mask
= udata
.au_mask
;
409 AUDIT_CHECK_IF_KEVENTS_MASK(audit_nae_mask
);
410 mtx_unlock(&audit_mtx
);
415 if (sizeof(udata
.au_qctrl64
) == uap
->length
) {
416 mtx_lock(&audit_mtx
);
417 udata
.au_qctrl64
.aq64_hiwater
=
418 (u_int64_t
)audit_qctrl
.aq_hiwater
;
419 udata
.au_qctrl64
.aq64_lowater
=
420 (u_int64_t
)audit_qctrl
.aq_lowater
;
421 udata
.au_qctrl64
.aq64_bufsz
=
422 (u_int64_t
)audit_qctrl
.aq_bufsz
;
423 udata
.au_qctrl64
.aq64_delay
=
424 (u_int64_t
)audit_qctrl
.aq_delay
;
425 udata
.au_qctrl64
.aq64_minfree
=
426 (int64_t)audit_qctrl
.aq_minfree
;
427 mtx_unlock(&audit_mtx
);
430 if (sizeof(udata
.au_qctrl
) != uap
->length
)
432 mtx_lock(&audit_mtx
);
433 udata
.au_qctrl
= audit_qctrl
;
434 mtx_unlock(&audit_mtx
);
439 if (sizeof(udata
.au_qctrl64
) == uap
->length
) {
440 if ((udata
.au_qctrl64
.aq64_hiwater
> AQ_MAXHIGH
) ||
441 (udata
.au_qctrl64
.aq64_lowater
>=
442 udata
.au_qctrl64
.aq64_hiwater
) ||
443 (udata
.au_qctrl64
.aq64_bufsz
> AQ_MAXBUFSZ
) ||
444 (udata
.au_qctrl64
.aq64_minfree
< 0) ||
445 (udata
.au_qctrl64
.aq64_minfree
> 100))
447 mtx_lock(&audit_mtx
);
448 audit_qctrl
.aq_hiwater
=
449 (int)udata
.au_qctrl64
.aq64_hiwater
;
450 audit_qctrl
.aq_lowater
=
451 (int)udata
.au_qctrl64
.aq64_lowater
;
452 audit_qctrl
.aq_bufsz
=
453 (int)udata
.au_qctrl64
.aq64_bufsz
;
454 audit_qctrl
.aq_minfree
=
455 (int)udata
.au_qctrl64
.aq64_minfree
;
456 audit_qctrl
.aq_delay
= -1; /* Not used. */
457 mtx_unlock(&audit_mtx
);
460 if ((sizeof(udata
.au_qctrl
) != uap
->length
) ||
461 (udata
.au_qctrl
.aq_hiwater
> AQ_MAXHIGH
) ||
462 (udata
.au_qctrl
.aq_lowater
>= udata
.au_qctrl
.aq_hiwater
) ||
463 (udata
.au_qctrl
.aq_bufsz
> AQ_MAXBUFSZ
) ||
464 (udata
.au_qctrl
.aq_minfree
< 0) ||
465 (udata
.au_qctrl
.aq_minfree
> 100))
468 mtx_lock(&audit_mtx
);
469 audit_qctrl
= udata
.au_qctrl
;
470 /* XXX The queue delay value isn't used with the kernel. */
471 audit_qctrl
.aq_delay
= -1;
472 mtx_unlock(&audit_mtx
);
495 if (sizeof(udata
.au_cond64
) == uap
->length
) {
496 mtx_lock(&audit_mtx
);
497 if (audit_enabled
&& !audit_suspended
)
498 udata
.au_cond64
= AUC_AUDITING
;
500 udata
.au_cond64
= AUC_NOAUDIT
;
501 mtx_unlock(&audit_mtx
);
504 if (sizeof(udata
.au_cond
) != uap
->length
)
506 mtx_lock(&audit_mtx
);
507 if (audit_enabled
&& !audit_suspended
)
508 udata
.au_cond
= AUC_AUDITING
;
510 udata
.au_cond
= AUC_NOAUDIT
;
511 mtx_unlock(&audit_mtx
);
516 if (sizeof(udata
.au_cond64
) == uap
->length
) {
517 mtx_lock(&audit_mtx
);
518 if (udata
.au_cond64
== AUC_NOAUDIT
)
520 if (udata
.au_cond64
== AUC_AUDITING
)
522 if (udata
.au_cond64
== AUC_DISABLED
) {
524 mtx_unlock(&audit_mtx
);
528 mtx_unlock(&audit_mtx
);
531 if (sizeof(udata
.au_cond
) != uap
->length
) {
534 mtx_lock(&audit_mtx
);
535 if (udata
.au_cond
== AUC_NOAUDIT
)
537 if (udata
.au_cond
== AUC_AUDITING
)
539 if (udata
.au_cond
== AUC_DISABLED
) {
541 mtx_unlock(&audit_mtx
);
545 mtx_unlock(&audit_mtx
);
549 if (sizeof(udata
.au_evclass
) != uap
->length
)
551 udata
.au_evclass
.ec_class
= au_event_class(
552 udata
.au_evclass
.ec_number
);
556 if (sizeof(udata
.au_evclass
) != uap
->length
)
558 au_evclassmap_insert(udata
.au_evclass
.ec_number
,
559 udata
.au_evclass
.ec_class
);
563 if ((sizeof(udata
.au_aupinfo
) != uap
->length
) ||
564 IS_NOT_VALID_PID(udata
.au_aupinfo
.ap_pid
))
566 if ((tp
= proc_find(udata
.au_aupinfo
.ap_pid
)) == NULL
)
569 scred
= kauth_cred_proc_ref(tp
);
570 if (scred
->cr_audit
.as_aia_p
->ai_termid
.at_type
== AU_IPv6
) {
571 kauth_cred_unref(&scred
);
576 udata
.au_aupinfo
.ap_auid
=
577 scred
->cr_audit
.as_aia_p
->ai_auid
;
578 udata
.au_aupinfo
.ap_mask
.am_success
=
579 scred
->cr_audit
.as_mask
.am_success
;
580 udata
.au_aupinfo
.ap_mask
.am_failure
=
581 scred
->cr_audit
.as_mask
.am_failure
;
582 udata
.au_aupinfo
.ap_termid
.machine
=
583 scred
->cr_audit
.as_aia_p
->ai_termid
.at_addr
[0];
584 udata
.au_aupinfo
.ap_termid
.port
=
585 scred
->cr_audit
.as_aia_p
->ai_termid
.at_port
;
586 udata
.au_aupinfo
.ap_asid
=
587 scred
->cr_audit
.as_aia_p
->ai_asid
;
588 kauth_cred_unref(&scred
);
594 if ((sizeof(udata
.au_aupinfo
) != uap
->length
) ||
595 IS_NOT_VALID_PID(udata
.au_aupinfo
.ap_pid
))
597 if ((tp
= proc_find(udata
.au_aupinfo
.ap_pid
)) == NULL
)
599 scred
= kauth_cred_proc_ref(tp
);
600 bcopy(scred
->cr_audit
.as_aia_p
, &aia
, sizeof(aia
));
601 kauth_cred_unref(&scred
);
602 aia
.ai_mask
.am_success
=
603 udata
.au_aupinfo
.ap_mask
.am_success
;
604 aia
.ai_mask
.am_failure
=
605 udata
.au_aupinfo
.ap_mask
.am_failure
;
606 AUDIT_CHECK_IF_KEVENTS_MASK(aia
.ai_mask
);
607 error
= audit_session_setaia(tp
, &aia
);
615 if ((sizeof(udata
.au_fstat
) != uap
->length
) ||
616 ((udata
.au_fstat
.af_filesz
!= 0) &&
617 (udata
.au_fstat
.af_filesz
< MIN_AUDIT_FILE_SIZE
)))
619 mtx_lock(&audit_mtx
);
620 audit_fstat
.af_filesz
= udata
.au_fstat
.af_filesz
;
621 mtx_unlock(&audit_mtx
);
625 if (sizeof(udata
.au_fstat
) != uap
->length
)
627 mtx_lock(&audit_mtx
);
628 udata
.au_fstat
.af_filesz
= audit_fstat
.af_filesz
;
629 udata
.au_fstat
.af_currsz
= audit_fstat
.af_currsz
;
630 mtx_unlock(&audit_mtx
);
633 case A_GETPINFO_ADDR
:
634 if ((sizeof(udata
.au_aupinfo_addr
) != uap
->length
) ||
635 IS_NOT_VALID_PID(udata
.au_aupinfo_addr
.ap_pid
))
637 if ((tp
= proc_find(udata
.au_aupinfo
.ap_pid
)) == NULL
)
639 WARN_IF_AINFO_ADDR_CHANGED(uap
->length
,
640 sizeof(auditpinfo_addr_t
), "auditon(A_GETPINFO_ADDR,...)",
641 "auditpinfo_addr_t");
642 scred
= kauth_cred_proc_ref(tp
);
643 udata
.au_aupinfo_addr
.ap_auid
=
644 scred
->cr_audit
.as_aia_p
->ai_auid
;
645 udata
.au_aupinfo_addr
.ap_asid
=
646 scred
->cr_audit
.as_aia_p
->ai_asid
;
647 udata
.au_aupinfo_addr
.ap_mask
.am_success
=
648 scred
->cr_audit
.as_mask
.am_success
;
649 udata
.au_aupinfo_addr
.ap_mask
.am_failure
=
650 scred
->cr_audit
.as_mask
.am_failure
;
651 bcopy(&scred
->cr_audit
.as_aia_p
->ai_termid
,
652 &udata
.au_aupinfo_addr
.ap_termid
,
653 sizeof(au_tid_addr_t
));
654 udata
.au_aupinfo_addr
.ap_flags
=
655 scred
->cr_audit
.as_aia_p
->ai_flags
;
656 kauth_cred_unref(&scred
);
662 if (sizeof(udata
.au_kau_info
) != uap
->length
)
664 audit_get_kinfo(&udata
.au_kau_info
);
668 if ((sizeof(udata
.au_kau_info
) != uap
->length
) ||
669 (udata
.au_kau_info
.ai_termid
.at_type
!= AU_IPv4
&&
670 udata
.au_kau_info
.ai_termid
.at_type
!= AU_IPv6
))
672 audit_set_kinfo(&udata
.au_kau_info
);
676 if ((sizeof(udata
.au_trigger
) != uap
->length
) ||
677 (udata
.au_trigger
< AUDIT_TRIGGER_MIN
) ||
678 (udata
.au_trigger
> AUDIT_TRIGGER_MAX
))
680 return (audit_send_trigger(udata
.au_trigger
));
682 case A_GETSINFO_ADDR
:
683 /* Handled above before switch(). */
687 if (sizeof(udata
.au_flags
) != uap
->length
)
689 bcopy(&(kauth_cred_get()->cr_audit
.as_aia_p
->ai_flags
),
690 &udata
.au_flags
, sizeof(udata
.au_flags
));
694 if (sizeof(udata
.au_flags
) != uap
->length
)
696 bcopy(kauth_cred_get()->cr_audit
.as_aia_p
, &aia
, sizeof(aia
));
697 aia
.ai_flags
= udata
.au_flags
;
698 error
= audit_session_setaia(p
, &aia
);
708 * Copy data back to userspace for the GET comands.
724 case A_GETPINFO_ADDR
:
726 case A_GETSINFO_ADDR
:
728 error
= copyout((void *)&udata
, uap
->data
, uap
->length
);
738 * System calls to manage the user audit information.
742 getauid(proc_t p
, struct getauid_args
*uap
, __unused
int32_t *retval
)
749 error
= mac_proc_check_getauid(p
);
753 scred
= kauth_cred_proc_ref(p
);
754 id
= scred
->cr_audit
.as_aia_p
->ai_auid
;
755 kauth_cred_unref(&scred
);
757 error
= copyout((void *)&id
, uap
->auid
, sizeof(id
));
766 setauid(proc_t p
, struct setauid_args
*uap
, __unused
int32_t *retval
)
771 struct auditinfo_addr aia
;
773 error
= copyin(uap
->auid
, &id
, sizeof(id
));
779 error
= mac_proc_check_setauid(p
, id
);
784 scred
= kauth_cred_proc_ref(p
);
785 error
= suser(scred
, &p
->p_acflag
);
787 kauth_cred_unref(&scred
);
791 bcopy(scred
->cr_audit
.as_aia_p
, &aia
, sizeof(aia
));
792 if (aia
.ai_asid
== AU_DEFAUDITSID
) {
793 aia
.ai_asid
= AU_ASSIGN_ASID
;
795 bcopy(&scred
->cr_audit
.as_mask
, &aia
.ai_mask
, sizeof(au_mask_t
));
796 kauth_cred_unref(&scred
);
798 error
= audit_session_setaia(p
, &aia
);
804 getaudit_addr_internal(proc_t p
, user_addr_t user_addr
, size_t length
)
807 auditinfo_addr_t aia
;
809 scred
= kauth_cred_proc_ref(p
);
810 bcopy(scred
->cr_audit
.as_aia_p
, &aia
, sizeof (auditinfo_addr_t
));
812 * Only superuser gets to see the real mask.
814 if (suser(scred
, &p
->p_acflag
)) {
815 aia
.ai_mask
.am_success
= ~0;
816 aia
.ai_mask
.am_failure
= ~0;
818 kauth_cred_unref(&scred
);
820 return (copyout(&aia
, user_addr
, min(sizeof(aia
), length
)));
825 getaudit_addr(proc_t p
, struct getaudit_addr_args
*uap
,
826 __unused
int32_t *retval
)
829 WARN_IF_AINFO_ADDR_CHANGED(uap
->length
, sizeof(auditinfo_addr_t
),
830 "getaudit_addr(2)", "auditinfo_addr_t");
832 return (getaudit_addr_internal(p
, uap
->auditinfo_addr
, uap
->length
));
837 setaudit_addr(proc_t p
, struct setaudit_addr_args
*uap
,
838 __unused
int32_t *retval
)
840 struct auditinfo_addr aia
;
844 bzero(&aia
, sizeof(auditinfo_addr_t
));
845 error
= copyin(uap
->auditinfo_addr
, &aia
,
846 min(sizeof(aia
), uap
->length
));
849 AUDIT_ARG(auditinfo_addr
, &aia
);
850 if (aia
.ai_termid
.at_type
!= AU_IPv6
&&
851 aia
.ai_termid
.at_type
!= AU_IPv4
)
853 if (aia
.ai_asid
!= AU_ASSIGN_ASID
&&
854 (uint32_t)aia
.ai_asid
> ASSIGNED_ASID_MAX
)
858 error
= mac_proc_check_setaudit(p
, &aia
);
863 scred
= kauth_cred_proc_ref(p
);
864 error
= suser(scred
, &p
->p_acflag
);
866 kauth_cred_unref(&scred
);
870 WARN_IF_AINFO_ADDR_CHANGED(uap
->length
, sizeof(auditinfo_addr_t
),
871 "setaudit_addr(2)", "auditinfo_addr_t");
872 WARN_IF_BAD_ASID(aia
.ai_asid
, "setaudit_addr(2)");
873 kauth_cred_unref(&scred
);
875 AUDIT_CHECK_IF_KEVENTS_MASK(aia
.ai_mask
);
876 if (aia
.ai_asid
== AU_DEFAUDITSID
)
877 aia
.ai_asid
= AU_ASSIGN_ASID
;
879 error
= audit_session_setaia(p
, &aia
);
884 * If asked to assign an ASID then let the user know what the ASID is
885 * by copying the auditinfo_addr struct back out.
887 if (aia
.ai_asid
== AU_ASSIGN_ASID
)
888 error
= getaudit_addr_internal(p
, uap
->auditinfo_addr
,
895 * Syscall to manage audit files.
900 auditctl(proc_t p
, struct auditctl_args
*uap
, __unused
int32_t *retval
)
907 error
= suser(kauth_cred_get(), &p
->p_acflag
);
915 * If a path is specified, open the replacement vnode, perform
916 * validity checks, and grab another reference to the current
919 * XXX Changes API slightly. NULL path no longer disables audit but
922 if (uap
->path
== USER_ADDR_NULL
)
925 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
926 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
:
927 UIO_USERSPACE32
), uap
->path
, vfs_context_current());
928 error
= vn_open(&nd
, AUDIT_OPEN_FLAGS
, 0);
934 * Accessibility of the vnode was determined in vn_open; the
935 * mac_system_check_auditctl should only determine whether that vnode
936 * is appropriate for storing audit data, or that the caller was
937 * permitted to control the auditing system at all. For example, a
938 * confidentiality policy may want to ensure that audit files are
939 * always high sensitivity.
941 error
= mac_system_check_auditctl(kauth_cred_get(), vp
);
943 vn_close(vp
, AUDIT_CLOSE_FLAGS
, vfs_context_current());
948 if (vp
->v_type
!= VREG
) {
949 vn_close(vp
, AUDIT_CLOSE_FLAGS
, vfs_context_current());
953 mtx_lock(&audit_mtx
);
955 * XXXAUDIT: Should audit_suspended actually be cleared by
959 mtx_unlock(&audit_mtx
);
962 * The following gets unreferenced in audit_rotate_vnode()
963 * after the rotation and it is no longer needed.
965 cred
= kauth_cred_get_with_ref();
966 audit_rotate_vnode(cred
, vp
);
972 #else /* !CONFIG_AUDIT */
975 audit(proc_t p
, struct audit_args
*uap
, int32_t *retval
)
977 #pragma unused(p, uap, retval)
983 auditon(proc_t p
, struct auditon_args
*uap
, int32_t *retval
)
985 #pragma unused(p, uap, retval)
991 getauid(proc_t p
, struct getauid_args
*uap
, int32_t *retval
)
993 #pragma unused(p, uap, retval)
999 setauid(proc_t p
, struct setauid_args
*uap
, int32_t *retval
)
1001 #pragma unused(p, uap, retval)
1007 getaudit_addr(proc_t p
, struct getaudit_addr_args
*uap
, int32_t *retval
)
1009 #pragma unused(p, uap, retval)
1015 setaudit_addr(proc_t p
, struct setaudit_addr_args
*uap
, int32_t *retval
)
1017 #pragma unused(p, uap, retval)
1023 auditctl(proc_t p
, struct auditctl_args
*uap
, int32_t *retval
)
1025 #pragma unused(p, uap, retval)
1030 #endif /* CONFIG_AUDIT */