2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999-2005 Apple Inc.
5 * Copyright (c) 2016-2017 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/filedesc.h>
43 #include <sys/capsicum.h>
45 #include <sys/mount.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/domain.h>
52 #include <sys/systm.h>
54 #include <sys/vnode.h>
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
59 #include <security/audit/audit.h>
60 #include <security/audit/audit_private.h>
63 * Calls to manipulate elements of the audit record structure from system
64 * call code. Macro wrappers will prevent this functions from being entered
65 * if auditing is disabled, avoiding the function call cost. We check the
66 * thread audit record pointer anyway, as the audit condition could change,
67 * and pre-selection may not have allocated an audit record for this event.
69 * XXXAUDIT: Should we assert, in each case, that this field of the record
70 * hasn't already been filled in?
73 audit_arg_addr(void *addr)
75 struct kaudit_record *ar;
81 ar->k_ar.ar_arg_addr = addr;
82 ARG_SET_VALID(ar, ARG_ADDR);
86 audit_arg_exit(int status, int retval)
88 struct kaudit_record *ar;
94 ar->k_ar.ar_arg_exitstatus = status;
95 ar->k_ar.ar_arg_exitretval = retval;
96 ARG_SET_VALID(ar, ARG_EXIT);
100 audit_arg_len(int len)
102 struct kaudit_record *ar;
108 ar->k_ar.ar_arg_len = len;
109 ARG_SET_VALID(ar, ARG_LEN);
113 audit_arg_atfd1(int atfd)
115 struct kaudit_record *ar;
121 ar->k_ar.ar_arg_atfd1 = atfd;
122 ARG_SET_VALID(ar, ARG_ATFD1);
126 audit_arg_atfd2(int atfd)
128 struct kaudit_record *ar;
134 ar->k_ar.ar_arg_atfd2 = atfd;
135 ARG_SET_VALID(ar, ARG_ATFD2);
141 struct kaudit_record *ar;
147 ar->k_ar.ar_arg_fd = fd;
148 ARG_SET_VALID(ar, ARG_FD);
152 audit_arg_fflags(int fflags)
154 struct kaudit_record *ar;
160 ar->k_ar.ar_arg_fflags = fflags;
161 ARG_SET_VALID(ar, ARG_FFLAGS);
165 audit_arg_gid(gid_t gid)
167 struct kaudit_record *ar;
173 ar->k_ar.ar_arg_gid = gid;
174 ARG_SET_VALID(ar, ARG_GID);
178 audit_arg_uid(uid_t uid)
180 struct kaudit_record *ar;
186 ar->k_ar.ar_arg_uid = uid;
187 ARG_SET_VALID(ar, ARG_UID);
191 audit_arg_egid(gid_t egid)
193 struct kaudit_record *ar;
199 ar->k_ar.ar_arg_egid = egid;
200 ARG_SET_VALID(ar, ARG_EGID);
204 audit_arg_euid(uid_t euid)
206 struct kaudit_record *ar;
212 ar->k_ar.ar_arg_euid = euid;
213 ARG_SET_VALID(ar, ARG_EUID);
217 audit_arg_rgid(gid_t rgid)
219 struct kaudit_record *ar;
225 ar->k_ar.ar_arg_rgid = rgid;
226 ARG_SET_VALID(ar, ARG_RGID);
230 audit_arg_ruid(uid_t ruid)
232 struct kaudit_record *ar;
238 ar->k_ar.ar_arg_ruid = ruid;
239 ARG_SET_VALID(ar, ARG_RUID);
243 audit_arg_sgid(gid_t sgid)
245 struct kaudit_record *ar;
251 ar->k_ar.ar_arg_sgid = sgid;
252 ARG_SET_VALID(ar, ARG_SGID);
256 audit_arg_suid(uid_t suid)
258 struct kaudit_record *ar;
264 ar->k_ar.ar_arg_suid = suid;
265 ARG_SET_VALID(ar, ARG_SUID);
269 audit_arg_groupset(gid_t *gidset, u_int gidset_size)
272 struct kaudit_record *ar;
274 KASSERT(gidset_size <= ngroups_max + 1,
275 ("audit_arg_groupset: gidset_size > (kern.ngroups + 1)"));
281 if (ar->k_ar.ar_arg_groups.gidset == NULL)
282 ar->k_ar.ar_arg_groups.gidset = malloc(
283 sizeof(gid_t) * gidset_size, M_AUDITGIDSET, M_WAITOK);
285 for (i = 0; i < gidset_size; i++)
286 ar->k_ar.ar_arg_groups.gidset[i] = gidset[i];
287 ar->k_ar.ar_arg_groups.gidset_size = gidset_size;
288 ARG_SET_VALID(ar, ARG_GROUPSET);
292 audit_arg_login(char *login)
294 struct kaudit_record *ar;
300 strlcpy(ar->k_ar.ar_arg_login, login, MAXLOGNAME);
301 ARG_SET_VALID(ar, ARG_LOGIN);
305 audit_arg_ctlname(int *name, int namelen)
307 struct kaudit_record *ar;
313 bcopy(name, &ar->k_ar.ar_arg_ctlname, namelen * sizeof(int));
314 ar->k_ar.ar_arg_len = namelen;
315 ARG_SET_VALID(ar, ARG_CTLNAME | ARG_LEN);
319 audit_arg_mask(int mask)
321 struct kaudit_record *ar;
327 ar->k_ar.ar_arg_mask = mask;
328 ARG_SET_VALID(ar, ARG_MASK);
332 audit_arg_mode(mode_t mode)
334 struct kaudit_record *ar;
340 ar->k_ar.ar_arg_mode = mode;
341 ARG_SET_VALID(ar, ARG_MODE);
345 audit_arg_dev(int dev)
347 struct kaudit_record *ar;
353 ar->k_ar.ar_arg_dev = dev;
354 ARG_SET_VALID(ar, ARG_DEV);
358 audit_arg_value(long value)
360 struct kaudit_record *ar;
366 ar->k_ar.ar_arg_value = value;
367 ARG_SET_VALID(ar, ARG_VALUE);
371 audit_arg_owner(uid_t uid, gid_t gid)
373 struct kaudit_record *ar;
379 ar->k_ar.ar_arg_uid = uid;
380 ar->k_ar.ar_arg_gid = gid;
381 ARG_SET_VALID(ar, ARG_UID | ARG_GID);
385 audit_arg_pid(pid_t pid)
387 struct kaudit_record *ar;
393 ar->k_ar.ar_arg_pid = pid;
394 ARG_SET_VALID(ar, ARG_PID);
398 audit_arg_process(struct proc *p)
400 struct kaudit_record *ar;
403 KASSERT(p != NULL, ("audit_arg_process: p == NULL"));
405 PROC_LOCK_ASSERT(p, MA_OWNED);
412 ar->k_ar.ar_arg_auid = cred->cr_audit.ai_auid;
413 ar->k_ar.ar_arg_euid = cred->cr_uid;
414 ar->k_ar.ar_arg_egid = cred->cr_groups[0];
415 ar->k_ar.ar_arg_ruid = cred->cr_ruid;
416 ar->k_ar.ar_arg_rgid = cred->cr_rgid;
417 ar->k_ar.ar_arg_asid = cred->cr_audit.ai_asid;
418 ar->k_ar.ar_arg_termid_addr = cred->cr_audit.ai_termid;
419 ar->k_ar.ar_arg_pid = p->p_pid;
420 ARG_SET_VALID(ar, ARG_AUID | ARG_EUID | ARG_EGID | ARG_RUID |
421 ARG_RGID | ARG_ASID | ARG_TERMID_ADDR | ARG_PID | ARG_PROCESS);
425 audit_arg_signum(u_int signum)
427 struct kaudit_record *ar;
433 ar->k_ar.ar_arg_signum = signum;
434 ARG_SET_VALID(ar, ARG_SIGNUM);
438 audit_arg_socket(int sodomain, int sotype, int soprotocol)
440 struct kaudit_record *ar;
446 ar->k_ar.ar_arg_sockinfo.so_domain = sodomain;
447 ar->k_ar.ar_arg_sockinfo.so_type = sotype;
448 ar->k_ar.ar_arg_sockinfo.so_protocol = soprotocol;
449 ARG_SET_VALID(ar, ARG_SOCKINFO);
453 audit_arg_sockaddr(struct thread *td, int dirfd, struct sockaddr *sa)
455 struct kaudit_record *ar;
457 KASSERT(td != NULL, ("audit_arg_sockaddr: td == NULL"));
458 KASSERT(sa != NULL, ("audit_arg_sockaddr: sa == NULL"));
464 bcopy(sa, &ar->k_ar.ar_arg_sockaddr, sa->sa_len);
465 switch (sa->sa_family) {
467 ARG_SET_VALID(ar, ARG_SADDRINET);
471 ARG_SET_VALID(ar, ARG_SADDRINET6);
475 if (dirfd != AT_FDCWD)
476 audit_arg_atfd1(dirfd);
477 audit_arg_upath1(td, dirfd,
478 ((struct sockaddr_un *)sa)->sun_path);
479 ARG_SET_VALID(ar, ARG_SADDRUNIX);
481 /* XXXAUDIT: default:? */
486 audit_arg_auid(uid_t auid)
488 struct kaudit_record *ar;
494 ar->k_ar.ar_arg_auid = auid;
495 ARG_SET_VALID(ar, ARG_AUID);
499 audit_arg_auditinfo(struct auditinfo *au_info)
501 struct kaudit_record *ar;
507 ar->k_ar.ar_arg_auid = au_info->ai_auid;
508 ar->k_ar.ar_arg_asid = au_info->ai_asid;
509 ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
510 ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
511 ar->k_ar.ar_arg_termid.port = au_info->ai_termid.port;
512 ar->k_ar.ar_arg_termid.machine = au_info->ai_termid.machine;
513 ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID);
517 audit_arg_auditinfo_addr(struct auditinfo_addr *au_info)
519 struct kaudit_record *ar;
525 ar->k_ar.ar_arg_auid = au_info->ai_auid;
526 ar->k_ar.ar_arg_asid = au_info->ai_asid;
527 ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
528 ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
529 ar->k_ar.ar_arg_termid_addr.at_type = au_info->ai_termid.at_type;
530 ar->k_ar.ar_arg_termid_addr.at_port = au_info->ai_termid.at_port;
531 ar->k_ar.ar_arg_termid_addr.at_addr[0] = au_info->ai_termid.at_addr[0];
532 ar->k_ar.ar_arg_termid_addr.at_addr[1] = au_info->ai_termid.at_addr[1];
533 ar->k_ar.ar_arg_termid_addr.at_addr[2] = au_info->ai_termid.at_addr[2];
534 ar->k_ar.ar_arg_termid_addr.at_addr[3] = au_info->ai_termid.at_addr[3];
535 ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID_ADDR);
539 audit_arg_text(const char *text)
541 struct kaudit_record *ar;
543 KASSERT(text != NULL, ("audit_arg_text: text == NULL"));
549 /* Invalidate the text string */
550 ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_TEXT);
552 if (ar->k_ar.ar_arg_text == NULL)
553 ar->k_ar.ar_arg_text = malloc(MAXPATHLEN, M_AUDITTEXT,
556 strncpy(ar->k_ar.ar_arg_text, text, MAXPATHLEN);
557 ARG_SET_VALID(ar, ARG_TEXT);
561 audit_arg_cmd(int cmd)
563 struct kaudit_record *ar;
569 ar->k_ar.ar_arg_cmd = cmd;
570 ARG_SET_VALID(ar, ARG_CMD);
574 audit_arg_svipc_cmd(int cmd)
576 struct kaudit_record *ar;
582 ar->k_ar.ar_arg_svipc_cmd = cmd;
583 ARG_SET_VALID(ar, ARG_SVIPC_CMD);
587 audit_arg_svipc_perm(struct ipc_perm *perm)
589 struct kaudit_record *ar;
595 bcopy(perm, &ar->k_ar.ar_arg_svipc_perm,
596 sizeof(ar->k_ar.ar_arg_svipc_perm));
597 ARG_SET_VALID(ar, ARG_SVIPC_PERM);
601 audit_arg_svipc_id(int id)
603 struct kaudit_record *ar;
609 ar->k_ar.ar_arg_svipc_id = id;
610 ARG_SET_VALID(ar, ARG_SVIPC_ID);
614 audit_arg_svipc_addr(void * addr)
616 struct kaudit_record *ar;
622 ar->k_ar.ar_arg_svipc_addr = addr;
623 ARG_SET_VALID(ar, ARG_SVIPC_ADDR);
627 audit_arg_svipc_which(int which)
629 struct kaudit_record *ar;
635 ar->k_ar.ar_arg_svipc_which = which;
636 ARG_SET_VALID(ar, ARG_SVIPC_WHICH);
640 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode)
642 struct kaudit_record *ar;
648 ar->k_ar.ar_arg_pipc_perm.pipc_uid = uid;
649 ar->k_ar.ar_arg_pipc_perm.pipc_gid = gid;
650 ar->k_ar.ar_arg_pipc_perm.pipc_mode = mode;
651 ARG_SET_VALID(ar, ARG_POSIX_IPC_PERM);
655 audit_arg_auditon(union auditon_udata *udata)
657 struct kaudit_record *ar;
663 bcopy((void *)udata, &ar->k_ar.ar_arg_auditon,
664 sizeof(ar->k_ar.ar_arg_auditon));
665 ARG_SET_VALID(ar, ARG_AUDITON);
669 * Audit information about a file, either the file's vnode info, or its
670 * socket address info.
673 audit_arg_file(struct proc *p, struct file *fp)
675 struct kaudit_record *ar;
684 switch (fp->f_type) {
688 * XXXAUDIT: Only possibly to record as first vnode?
691 vn_lock(vp, LK_SHARED | LK_RETRY);
692 audit_arg_vnode1(vp);
697 so = (struct socket *)fp->f_data;
698 if (INP_CHECK_SOCKAF(so, PF_INET)) {
700 ar->k_ar.ar_arg_sockinfo.so_type =
702 ar->k_ar.ar_arg_sockinfo.so_domain =
704 ar->k_ar.ar_arg_sockinfo.so_protocol =
705 so->so_proto->pr_protocol;
707 pcb = (struct inpcb *)so->so_pcb;
709 ar->k_ar.ar_arg_sockinfo.so_raddr =
710 pcb->inp_faddr.s_addr;
711 ar->k_ar.ar_arg_sockinfo.so_laddr =
712 pcb->inp_laddr.s_addr;
713 ar->k_ar.ar_arg_sockinfo.so_rport =
715 ar->k_ar.ar_arg_sockinfo.so_lport =
718 ARG_SET_VALID(ar, ARG_SOCKINFO);
723 /* XXXAUDIT: else? */
729 * Store a path as given by the user process for auditing into the audit
730 * record stored on the user thread. This function will allocate the memory
731 * to store the path info if not already available. This memory will be
732 * freed when the audit record is freed. The path is canonlicalised with
733 * respect to the thread and directory descriptor passed.
736 audit_arg_upath(struct thread *td, int dirfd, char *upath, char **pathp)
740 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
741 audit_canon_path(td, dirfd, upath, *pathp);
745 audit_arg_upath1(struct thread *td, int dirfd, char *upath)
747 struct kaudit_record *ar;
753 audit_arg_upath(td, dirfd, upath, &ar->k_ar.ar_arg_upath1);
754 ARG_SET_VALID(ar, ARG_UPATH1);
758 audit_arg_upath2(struct thread *td, int dirfd, char *upath)
760 struct kaudit_record *ar;
766 audit_arg_upath(td, dirfd, upath, &ar->k_ar.ar_arg_upath2);
767 ARG_SET_VALID(ar, ARG_UPATH2);
771 audit_arg_upath_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir,
772 char *upath, char **pathp)
776 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
777 audit_canon_path_vp(td, rdir, cdir, upath, *pathp);
781 audit_arg_upath1_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir,
784 struct kaudit_record *ar;
790 audit_arg_upath_vp(td, rdir, cdir, upath, &ar->k_ar.ar_arg_upath1);
791 ARG_SET_VALID(ar, ARG_UPATH1);
795 audit_arg_upath2_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir,
798 struct kaudit_record *ar;
804 audit_arg_upath_vp(td, rdir, cdir, upath, &ar->k_ar.ar_arg_upath2);
805 ARG_SET_VALID(ar, ARG_UPATH2);
809 * Variants on path auditing that do not canonicalise the path passed in;
810 * these are for use with filesystem-like subsystems that employ string names,
811 * but do not support a hierarchical namespace -- for example, POSIX IPC
812 * objects. The subsystem should have performed any necessary
813 * canonicalisation required to make the paths useful to audit analysis.
816 audit_arg_upath_canon(char *upath, char **pathp)
820 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
821 (void)snprintf(*pathp, MAXPATHLEN, "%s", upath);
825 audit_arg_upath1_canon(char *upath)
827 struct kaudit_record *ar;
833 audit_arg_upath_canon(upath, &ar->k_ar.ar_arg_upath1);
834 ARG_SET_VALID(ar, ARG_UPATH1);
838 audit_arg_upath2_canon(char *upath)
840 struct kaudit_record *ar;
846 audit_arg_upath_canon(upath, &ar->k_ar.ar_arg_upath2);
847 ARG_SET_VALID(ar, ARG_UPATH2);
851 * Function to save the path and vnode attr information into the audit
854 * It is assumed that the caller will hold any vnode locks necessary to
855 * perform a VOP_GETATTR() on the passed vnode.
857 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but always
858 * provides access to the generation number as we need that to construct the
861 * XXX: We should accept the process argument from the caller, since it's
862 * very likely they already have a reference.
864 * XXX: Error handling in this function is poor.
866 * XXXAUDIT: Possibly KASSERT the path pointer is NULL?
869 audit_arg_vnode(struct vnode *vp, struct vnode_au_info *vnp)
874 ASSERT_VOP_LOCKED(vp, "audit_arg_vnode");
876 error = VOP_GETATTR(vp, &vattr, curthread->td_ucred);
878 /* XXX: How to handle this case? */
882 vnp->vn_mode = vattr.va_mode;
883 vnp->vn_uid = vattr.va_uid;
884 vnp->vn_gid = vattr.va_gid;
885 vnp->vn_dev = vattr.va_rdev;
886 vnp->vn_fsid = vattr.va_fsid;
887 vnp->vn_fileid = vattr.va_fileid;
888 vnp->vn_gen = vattr.va_gen;
893 audit_arg_vnode1(struct vnode *vp)
895 struct kaudit_record *ar;
902 ARG_CLEAR_VALID(ar, ARG_VNODE1);
903 error = audit_arg_vnode(vp, &ar->k_ar.ar_arg_vnode1);
905 ARG_SET_VALID(ar, ARG_VNODE1);
909 audit_arg_vnode2(struct vnode *vp)
911 struct kaudit_record *ar;
918 ARG_CLEAR_VALID(ar, ARG_VNODE2);
919 error = audit_arg_vnode(vp, &ar->k_ar.ar_arg_vnode2);
921 ARG_SET_VALID(ar, ARG_VNODE2);
925 * Audit the argument strings passed to exec.
928 audit_arg_argv(char *argv, int argc, int length)
930 struct kaudit_record *ar;
939 ar->k_ar.ar_arg_argv = malloc(length, M_AUDITTEXT, M_WAITOK);
940 bcopy(argv, ar->k_ar.ar_arg_argv, length);
941 ar->k_ar.ar_arg_argc = argc;
942 ARG_SET_VALID(ar, ARG_ARGV);
946 * Audit the environment strings passed to exec.
949 audit_arg_envv(char *envv, int envc, int length)
951 struct kaudit_record *ar;
960 ar->k_ar.ar_arg_envv = malloc(length, M_AUDITTEXT, M_WAITOK);
961 bcopy(envv, ar->k_ar.ar_arg_envv, length);
962 ar->k_ar.ar_arg_envc = envc;
963 ARG_SET_VALID(ar, ARG_ENVV);
967 audit_arg_rights(cap_rights_t *rightsp)
969 struct kaudit_record *ar;
975 ar->k_ar.ar_arg_rights = *rightsp;
976 ARG_SET_VALID(ar, ARG_RIGHTS);
980 audit_arg_fcntl_rights(uint32_t fcntlrights)
982 struct kaudit_record *ar;
988 ar->k_ar.ar_arg_fcntl_rights = fcntlrights;
989 ARG_SET_VALID(ar, ARG_FCNTL_RIGHTS);
993 * The close() system call uses it's own audit call to capture the path/vnode
994 * information because those pieces are not easily obtained within the system
998 audit_sysclose(struct thread *td, int fd)
1000 cap_rights_t rights;
1001 struct kaudit_record *ar;
1005 KASSERT(td != NULL, ("audit_sysclose: td == NULL"));
1013 if (getvnode(td, fd, cap_rights_init(&rights), &fp) != 0)
1017 vn_lock(vp, LK_SHARED | LK_RETRY);
1018 audit_arg_vnode1(vp);