2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999-2009 Apple Inc.
5 * Copyright (c) 2016 Robert N. M. Watson
8 * Portions of this software were developed by BAE Systems, the University of
9 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
10 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
11 * Computing (TC) research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
22 * its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
46 #include <sys/sysproto.h>
47 #include <sys/systm.h>
48 #include <sys/vnode.h>
51 #include <bsm/audit.h>
52 #include <bsm/audit_kevents.h>
54 #include <security/audit/audit.h>
55 #include <security/audit/audit_private.h>
56 #include <security/mac/mac_framework.h>
61 * System call to allow a user space application to submit a BSM audit record
62 * to the kernel for inclusion in the audit log. This function does little
63 * verification on the audit record that is submitted.
65 * XXXAUDIT: Audit preselection for user records does not currently work,
66 * since we pre-select only based on the AUE_audit event type, not the event
67 * type submitted as part of the user audit data.
71 sys_audit(struct thread *td, struct audit_args *uap)
75 struct kaudit_record *ar;
77 if (jailed(td->td_ucred))
79 error = priv_check(td, PRIV_AUDIT_SUBMIT);
83 if ((uap->length <= 0) || (uap->length > audit_qctrl.aq_bufsz))
89 * If there's no current audit record (audit() itself not audited)
90 * commit the user audit record.
95 * This is not very efficient; we're required to allocate a
96 * complete kernel audit record just so the user record can
99 * XXXAUDIT: Maybe AUE_AUDIT in the system call context and
100 * special pre-select handling?
102 td->td_ar = audit_new(AUE_NULL, td);
103 if (td->td_ar == NULL)
105 td->td_pflags |= TDP_AUDITREC;
109 if (uap->length > MAX_AUDIT_RECORD_SIZE)
112 rec = malloc(uap->length, M_AUDITDATA, M_WAITOK);
114 error = copyin(uap->record, rec, uap->length);
118 /* Verify the record. */
119 if (bsm_rec_verify(rec) == 0) {
125 error = mac_system_check_audit(td->td_ucred, rec, uap->length);
131 * Attach the user audit record to the kernel audit record. Because
132 * this system call is an auditable event, we will write the user
133 * record along with the record for this audit event.
135 * XXXAUDIT: KASSERT appropriate starting values of k_udata, k_ulen,
136 * k_ar_commit & AR_COMMIT_USER?
139 ar->k_ulen = uap->length;
140 ar->k_ar_commit |= AR_COMMIT_USER;
143 * Currently we assume that all preselection has been performed in
144 * userspace. We unconditionally set these masks so that the records
145 * get committed both to the trail and pipe. In the future we will
146 * want to setup kernel based preselection.
148 ar->k_ar_commit |= (AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE);
153 * audit_syscall_exit() will free the audit record on the thread even
154 * if we allocated it above.
156 free(rec, M_AUDITDATA);
161 * System call to manipulate auditing.
165 sys_auditon(struct thread *td, struct auditon_args *uap)
167 struct ucred *cred, *newcred, *oldcred;
169 union auditon_udata udata;
172 if (jailed(td->td_ucred))
174 AUDIT_ARG_CMD(uap->cmd);
177 error = mac_system_check_auditon(td->td_ucred, uap->cmd);
182 error = priv_check(td, PRIV_AUDIT_CONTROL);
186 if ((uap->length <= 0) || (uap->length > sizeof(union auditon_udata)))
189 memset((void *)&udata, 0, sizeof(udata));
192 * Some of the GET commands use the arguments too.
213 case A_GETPINFO_ADDR:
215 error = copyin(uap->data, (void *)&udata, uap->length);
218 AUDIT_ARG_AUDITON(&udata);
228 if (uap->length == sizeof(udata.au_policy64)) {
229 if (!audit_fail_stop)
230 udata.au_policy64 |= AUDIT_CNT;
231 if (audit_panic_on_write_fail)
232 udata.au_policy64 |= AUDIT_AHLT;
234 udata.au_policy64 |= AUDIT_ARGV;
236 udata.au_policy64 |= AUDIT_ARGE;
239 if (uap->length != sizeof(udata.au_policy))
241 if (!audit_fail_stop)
242 udata.au_policy |= AUDIT_CNT;
243 if (audit_panic_on_write_fail)
244 udata.au_policy |= AUDIT_AHLT;
246 udata.au_policy |= AUDIT_ARGV;
248 udata.au_policy |= AUDIT_ARGE;
253 if (uap->length == sizeof(udata.au_policy64)) {
254 if (udata.au_policy & ~(AUDIT_CNT|AUDIT_AHLT|
255 AUDIT_ARGV|AUDIT_ARGE))
257 audit_fail_stop = ((udata.au_policy64 & AUDIT_CNT) ==
259 audit_panic_on_write_fail = (udata.au_policy64 &
261 audit_argv = (udata.au_policy64 & AUDIT_ARGV);
262 audit_arge = (udata.au_policy64 & AUDIT_ARGE);
265 if (uap->length != sizeof(udata.au_policy))
267 if (udata.au_policy & ~(AUDIT_CNT|AUDIT_AHLT|AUDIT_ARGV|
271 * XXX - Need to wake up waiters if the policy relaxes?
273 audit_fail_stop = ((udata.au_policy & AUDIT_CNT) == 0);
274 audit_panic_on_write_fail = (udata.au_policy & AUDIT_AHLT);
275 audit_argv = (udata.au_policy & AUDIT_ARGV);
276 audit_arge = (udata.au_policy & AUDIT_ARGE);
280 if (uap->length != sizeof(udata.au_mask))
282 udata.au_mask = audit_nae_mask;
286 if (uap->length != sizeof(udata.au_mask))
288 audit_nae_mask = udata.au_mask;
293 if (uap->length == sizeof(udata.au_qctrl64)) {
294 udata.au_qctrl64.aq64_hiwater =
295 (u_int64_t)audit_qctrl.aq_hiwater;
296 udata.au_qctrl64.aq64_lowater =
297 (u_int64_t)audit_qctrl.aq_lowater;
298 udata.au_qctrl64.aq64_bufsz =
299 (u_int64_t)audit_qctrl.aq_bufsz;
300 udata.au_qctrl64.aq64_minfree =
301 (u_int64_t)audit_qctrl.aq_minfree;
304 if (uap->length != sizeof(udata.au_qctrl))
306 udata.au_qctrl = audit_qctrl;
311 if (uap->length == sizeof(udata.au_qctrl64)) {
312 /* NB: aq64_minfree is unsigned unlike aq_minfree. */
313 if ((udata.au_qctrl64.aq64_hiwater > AQ_MAXHIGH) ||
314 (udata.au_qctrl64.aq64_lowater >=
315 udata.au_qctrl.aq_hiwater) ||
316 (udata.au_qctrl64.aq64_bufsz > AQ_MAXBUFSZ) ||
317 (udata.au_qctrl64.aq64_minfree > 100))
319 audit_qctrl.aq_hiwater =
320 (int)udata.au_qctrl64.aq64_hiwater;
321 audit_qctrl.aq_lowater =
322 (int)udata.au_qctrl64.aq64_lowater;
323 audit_qctrl.aq_bufsz =
324 (int)udata.au_qctrl64.aq64_bufsz;
325 audit_qctrl.aq_minfree =
326 (int)udata.au_qctrl64.aq64_minfree;
327 audit_qctrl.aq_delay = -1; /* Not used. */
330 if (uap->length != sizeof(udata.au_qctrl))
332 if ((udata.au_qctrl.aq_hiwater > AQ_MAXHIGH) ||
333 (udata.au_qctrl.aq_lowater >= udata.au_qctrl.aq_hiwater) ||
334 (udata.au_qctrl.aq_bufsz > AQ_MAXBUFSZ) ||
335 (udata.au_qctrl.aq_minfree < 0) ||
336 (udata.au_qctrl.aq_minfree > 100))
339 audit_qctrl = udata.au_qctrl;
340 /* XXX The queue delay value isn't used with the kernel. */
341 audit_qctrl.aq_delay = -1;
370 if (uap->length == sizeof(udata.au_cond64)) {
371 if (audit_enabled && !audit_suspended)
372 udata.au_cond64 = AUC_AUDITING;
374 udata.au_cond64 = AUC_NOAUDIT;
377 if (uap->length != sizeof(udata.au_cond))
379 if (audit_enabled && !audit_suspended)
380 udata.au_cond = AUC_AUDITING;
382 udata.au_cond = AUC_NOAUDIT;
387 if (uap->length == sizeof(udata.au_cond64)) {
388 if (udata.au_cond64 == AUC_NOAUDIT)
390 if (udata.au_cond64 == AUC_AUDITING)
392 if (udata.au_cond64 == AUC_DISABLED) {
394 audit_shutdown(NULL, 0);
398 if (uap->length != sizeof(udata.au_cond))
400 if (udata.au_cond == AUC_NOAUDIT)
402 if (udata.au_cond == AUC_AUDITING)
404 if (udata.au_cond == AUC_DISABLED) {
406 audit_shutdown(NULL, 0);
411 if (uap->length != sizeof(udata.au_evclass))
413 udata.au_evclass.ec_class = au_event_class(
414 udata.au_evclass.ec_number);
418 if (uap->length != sizeof(udata.au_evname))
420 error = au_event_name(udata.au_evname.en_number,
421 udata.au_evname.en_name);
427 if (uap->length != sizeof(udata.au_evclass))
429 au_evclassmap_insert(udata.au_evclass.ec_number,
430 udata.au_evclass.ec_class);
434 if (uap->length != sizeof(udata.au_evname))
437 /* Ensure nul termination from userspace. */
438 udata.au_evname.en_name[sizeof(udata.au_evname.en_name) - 1]
440 au_evnamemap_insert(udata.au_evname.en_number,
441 udata.au_evname.en_name);
445 if (uap->length != sizeof(udata.au_aupinfo))
447 if (udata.au_aupinfo.ap_pid < 1)
449 if ((tp = pfind(udata.au_aupinfo.ap_pid)) == NULL)
451 if ((error = p_cansee(td, tp)) != 0) {
456 if (cred->cr_audit.ai_termid.at_type == AU_IPv6) {
460 udata.au_aupinfo.ap_auid = cred->cr_audit.ai_auid;
461 udata.au_aupinfo.ap_mask.am_success =
462 cred->cr_audit.ai_mask.am_success;
463 udata.au_aupinfo.ap_mask.am_failure =
464 cred->cr_audit.ai_mask.am_failure;
465 udata.au_aupinfo.ap_termid.machine =
466 cred->cr_audit.ai_termid.at_addr[0];
467 udata.au_aupinfo.ap_termid.port =
468 (dev_t)cred->cr_audit.ai_termid.at_port;
469 udata.au_aupinfo.ap_asid = cred->cr_audit.ai_asid;
474 if (uap->length != sizeof(udata.au_aupinfo))
476 if (udata.au_aupinfo.ap_pid < 1)
479 if ((tp = pfind(udata.au_aupinfo.ap_pid)) == NULL) {
483 if ((error = p_cansee(td, tp)) != 0) {
488 oldcred = tp->p_ucred;
489 crcopy(newcred, oldcred);
490 newcred->cr_audit.ai_mask.am_success =
491 udata.au_aupinfo.ap_mask.am_success;
492 newcred->cr_audit.ai_mask.am_failure =
493 udata.au_aupinfo.ap_mask.am_failure;
494 proc_set_cred(tp, newcred);
500 if (uap->length != sizeof(udata.au_fstat))
502 if ((udata.au_fstat.af_filesz != 0) &&
503 (udata.au_fstat.af_filesz < MIN_AUDIT_FILE_SIZE))
505 audit_fstat.af_filesz = udata.au_fstat.af_filesz;
509 if (uap->length != sizeof(udata.au_fstat))
511 udata.au_fstat.af_filesz = audit_fstat.af_filesz;
512 udata.au_fstat.af_currsz = audit_fstat.af_currsz;
515 case A_GETPINFO_ADDR:
516 if (uap->length != sizeof(udata.au_aupinfo_addr))
518 if (udata.au_aupinfo_addr.ap_pid < 1)
520 if ((tp = pfind(udata.au_aupinfo_addr.ap_pid)) == NULL)
523 udata.au_aupinfo_addr.ap_auid = cred->cr_audit.ai_auid;
524 udata.au_aupinfo_addr.ap_mask.am_success =
525 cred->cr_audit.ai_mask.am_success;
526 udata.au_aupinfo_addr.ap_mask.am_failure =
527 cred->cr_audit.ai_mask.am_failure;
528 udata.au_aupinfo_addr.ap_termid = cred->cr_audit.ai_termid;
529 udata.au_aupinfo_addr.ap_asid = cred->cr_audit.ai_asid;
534 if (uap->length != sizeof(udata.au_kau_info))
536 audit_get_kinfo(&udata.au_kau_info);
540 if (uap->length != sizeof(udata.au_kau_info))
542 if (udata.au_kau_info.ai_termid.at_type != AU_IPv4 &&
543 udata.au_kau_info.ai_termid.at_type != AU_IPv6)
545 audit_set_kinfo(&udata.au_kau_info);
549 if (uap->length != sizeof(udata.au_trigger))
551 if ((udata.au_trigger < AUDIT_TRIGGER_MIN) ||
552 (udata.au_trigger > AUDIT_TRIGGER_MAX))
554 return (audit_send_trigger(udata.au_trigger));
561 * Copy data back to userspace for the GET comands.
577 case A_GETPINFO_ADDR:
579 error = copyout((void *)&udata, uap->data, uap->length);
589 * System calls to manage the user audit information.
593 sys_getauid(struct thread *td, struct getauid_args *uap)
597 if (jailed(td->td_ucred))
599 error = priv_check(td, PRIV_AUDIT_GETAUDIT);
602 return (copyout(&td->td_ucred->cr_audit.ai_auid, uap->auid,
603 sizeof(td->td_ucred->cr_audit.ai_auid)));
608 sys_setauid(struct thread *td, struct setauid_args *uap)
610 struct ucred *newcred, *oldcred;
614 if (jailed(td->td_ucred))
616 error = copyin(uap->auid, &id, sizeof(id));
621 PROC_LOCK(td->td_proc);
622 oldcred = td->td_proc->p_ucred;
623 crcopy(newcred, oldcred);
625 error = mac_cred_check_setauid(oldcred, id);
629 error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT, 0);
632 newcred->cr_audit.ai_auid = id;
633 proc_set_cred(td->td_proc, newcred);
634 PROC_UNLOCK(td->td_proc);
638 PROC_UNLOCK(td->td_proc);
644 * System calls to get and set process audit information.
648 sys_getaudit(struct thread *td, struct getaudit_args *uap)
657 error = priv_check(td, PRIV_AUDIT_GETAUDIT);
660 if (cred->cr_audit.ai_termid.at_type == AU_IPv6)
662 bzero(&ai, sizeof(ai));
663 ai.ai_auid = cred->cr_audit.ai_auid;
664 ai.ai_mask = cred->cr_audit.ai_mask;
665 ai.ai_asid = cred->cr_audit.ai_asid;
666 ai.ai_termid.machine = cred->cr_audit.ai_termid.at_addr[0];
667 ai.ai_termid.port = cred->cr_audit.ai_termid.at_port;
668 return (copyout(&ai, uap->auditinfo, sizeof(ai)));
673 sys_setaudit(struct thread *td, struct setaudit_args *uap)
675 struct ucred *newcred, *oldcred;
679 if (jailed(td->td_ucred))
681 error = copyin(uap->auditinfo, &ai, sizeof(ai));
684 audit_arg_auditinfo(&ai);
686 PROC_LOCK(td->td_proc);
687 oldcred = td->td_proc->p_ucred;
688 crcopy(newcred, oldcred);
690 error = mac_cred_check_setaudit(oldcred, &ai);
694 error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT, 0);
697 bzero(&newcred->cr_audit, sizeof(newcred->cr_audit));
698 newcred->cr_audit.ai_auid = ai.ai_auid;
699 newcred->cr_audit.ai_mask = ai.ai_mask;
700 newcred->cr_audit.ai_asid = ai.ai_asid;
701 newcred->cr_audit.ai_termid.at_addr[0] = ai.ai_termid.machine;
702 newcred->cr_audit.ai_termid.at_port = ai.ai_termid.port;
703 newcred->cr_audit.ai_termid.at_type = AU_IPv4;
704 proc_set_cred(td->td_proc, newcred);
705 PROC_UNLOCK(td->td_proc);
709 PROC_UNLOCK(td->td_proc);
716 sys_getaudit_addr(struct thread *td, struct getaudit_addr_args *uap)
720 if (jailed(td->td_ucred))
722 if (uap->length < sizeof(*uap->auditinfo_addr))
724 error = priv_check(td, PRIV_AUDIT_GETAUDIT);
727 return (copyout(&td->td_ucred->cr_audit, uap->auditinfo_addr,
728 sizeof(*uap->auditinfo_addr)));
733 sys_setaudit_addr(struct thread *td, struct setaudit_addr_args *uap)
735 struct ucred *newcred, *oldcred;
736 struct auditinfo_addr aia;
739 if (jailed(td->td_ucred))
741 error = copyin(uap->auditinfo_addr, &aia, sizeof(aia));
744 audit_arg_auditinfo_addr(&aia);
745 if (aia.ai_termid.at_type != AU_IPv6 &&
746 aia.ai_termid.at_type != AU_IPv4)
749 PROC_LOCK(td->td_proc);
750 oldcred = td->td_proc->p_ucred;
751 crcopy(newcred, oldcred);
753 error = mac_cred_check_setaudit_addr(oldcred, &aia);
757 error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT, 0);
760 newcred->cr_audit = aia;
761 proc_set_cred(td->td_proc, newcred);
762 PROC_UNLOCK(td->td_proc);
766 PROC_UNLOCK(td->td_proc);
772 * Syscall to manage audit files.
776 sys_auditctl(struct thread *td, struct auditctl_args *uap)
784 if (jailed(td->td_ucred))
786 error = priv_check(td, PRIV_AUDIT_CONTROL);
794 * If a path is specified, open the replacement vnode, perform
795 * validity checks, and grab another reference to the current
798 * On Darwin, a NULL path argument is also used to disable audit.
800 if (uap->path == NULL)
803 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
804 UIO_USERSPACE, uap->path, td);
805 flags = AUDIT_OPEN_FLAGS;
806 error = vn_open(&nd, &flags, 0, NULL);
811 error = mac_system_check_auditctl(td->td_ucred, vp);
814 vn_close(vp, AUDIT_CLOSE_FLAGS, td->td_ucred, td);
820 NDFREE(&nd, NDF_ONLY_PNBUF);
821 if (vp->v_type != VREG) {
822 vn_close(vp, AUDIT_CLOSE_FLAGS, td->td_ucred, td);
829 * XXXAUDIT: Should audit_suspended actually be cleared by
834 audit_rotate_vnode(cred, vp);
842 sys_audit(struct thread *td, struct audit_args *uap)
849 sys_auditon(struct thread *td, struct auditon_args *uap)
856 sys_getauid(struct thread *td, struct getauid_args *uap)
863 sys_setauid(struct thread *td, struct setauid_args *uap)
870 sys_getaudit(struct thread *td, struct getaudit_args *uap)
877 sys_setaudit(struct thread *td, struct setaudit_args *uap)
884 sys_getaudit_addr(struct thread *td, struct getaudit_addr_args *uap)
891 sys_setaudit_addr(struct thread *td, struct setaudit_addr_args *uap)
898 sys_auditctl(struct thread *td, struct auditctl_args *uap)