Module Name:    src
Committed By:   christos
Date:           Thu May  3 21:43:33 UTC 2018

Modified Files:
        src/sys/compat/common: uipc_syscalls_43.c
        src/sys/compat/linux/common: linux_socket.c
        src/sys/compat/netbsd32: netbsd32_compat_43.c netbsd32_socket.c
        src/sys/kern: uipc_syscalls.c
        src/sys/sys: param.h

Log Message:
Fix COMPAT_NETBSD32 cmsg handling:

1. alignment was wrong for > 1 message
2. macros were doing incorrect pointer comparisons, fortunately ending
   the iteration early after the fists cmsg instead of crashing.
3. don't output 32 bit ktrace records for cmsg. 32 bit programs running
   under emulation on 64 bit systems should produce 64 bit ktrace records
   so that the native ktrace can handle the records; remove extra arguments
   that are now not needed (the 32 bit msghdr).
4. output the correct type for cmsg trace records.
5. output all the cmsg records in traces instead of just the first one.

Welcome to 8.99.15 because of the argument removal.

XXX: Really all the code should be changed to use the CMSG_{FIRST,NXT}HDR
macros...


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/sys/compat/common/uipc_syscalls_43.c
cvs rdiff -u -r1.140 -r1.141 src/sys/compat/linux/common/linux_socket.c
cvs rdiff -u -r1.55 -r1.56 src/sys/compat/netbsd32/netbsd32_compat_43.c
cvs rdiff -u -r1.44 -r1.45 src/sys/compat/netbsd32/netbsd32_socket.c
cvs rdiff -u -r1.192 -r1.193 src/sys/kern/uipc_syscalls.c
cvs rdiff -u -r1.559 -r1.560 src/sys/sys/param.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/common/uipc_syscalls_43.c
diff -u src/sys/compat/common/uipc_syscalls_43.c:1.48 src/sys/compat/common/uipc_syscalls_43.c:1.49
--- src/sys/compat/common/uipc_syscalls_43.c:1.48	Fri Mar 16 13:25:04 2018
+++ src/sys/compat/common/uipc_syscalls_43.c	Thu May  3 17:43:33 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls_43.c,v 1.48 2018/03/16 17:25:04 christos Exp $	*/
+/*	$NetBSD: uipc_syscalls_43.c,v 1.49 2018/05/03 21:43:33 christos Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1990, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_43.c,v 1.48 2018/03/16 17:25:04 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_43.c,v 1.49 2018/05/03 21:43:33 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -216,7 +216,7 @@ compat_43_sys_recvmsg(struct lwp *l, con
 	msg.msg_iov	= omsg.msg_iov;
 	msg.msg_flags	= (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
 	    omsg.msg_accrights != NULL ? &control : NULL, retval);
 	if (error != 0)
 		return error;
@@ -363,7 +363,7 @@ compat_43_sys_sendmsg(struct lwp *l, con
 		goto bad;
 
 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
-	    NULL, 0, retval);
+	    retval);
 
     bad:
 	if (nam != NULL)

Index: src/sys/compat/linux/common/linux_socket.c
diff -u src/sys/compat/linux/common/linux_socket.c:1.140 src/sys/compat/linux/common/linux_socket.c:1.141
--- src/sys/compat/linux/common/linux_socket.c:1.140	Fri Mar 16 13:25:04 2018
+++ src/sys/compat/linux/common/linux_socket.c	Thu May  3 17:43:33 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_socket.c,v 1.140 2018/03/16 17:25:04 christos Exp $	*/
+/*	$NetBSD: linux_socket.c,v 1.141 2018/05/03 21:43:33 christos Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.140 2018/03/16 17:25:04 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.141 2018/05/03 21:43:33 christos Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_inet.h"
@@ -434,8 +434,7 @@ linux_sys_sendto(struct lwp *l, const st
 	aiov.iov_base = __UNCONST(SCARG(uap, msg));
 	aiov.iov_len = SCARG(uap, len);
 
-	return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags,
-	    NULL, 0, retval);
+	return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
 }
 
 static void
@@ -624,8 +623,7 @@ linux_sys_sendmsg(struct lwp *l, const s
 	}
 
 skipcmsg:
-	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags,
-	    NULL, 0, retval);
+	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
 	/* Freed internally */
 	ctl_mbuf = NULL;
 
@@ -787,7 +785,7 @@ linux_sys_recvmsg(struct lwp *l, const s
 	}
 	msg.msg_flags |= MSG_IOVUSRSPACE;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
 	    msg.msg_control != NULL ? &control : NULL, retval);
 	if (error != 0)
 		return error;
@@ -1789,8 +1787,7 @@ linux_sys_sendmmsg(struct lwp *l, const 
 
 		msg->msg_flags = flags;
 
-		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags,
-		    &msg, sizeof(msg), retval);
+		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
 		if (error)
 			break;
 
@@ -1871,7 +1868,7 @@ linux_sys_recvmmsg(struct lwp *l, const 
 			from = NULL;
 		}
 
-		error = do_sys_recvmsg_so(l, s, so, msg, NULL, 0, &from,
+		error = do_sys_recvmsg_so(l, s, so, msg, &from,
 		    msg->msg_control != NULL ? &control : NULL, retval);
 		if (error) {
 			if (error == EAGAIN && dg > 0)

Index: src/sys/compat/netbsd32/netbsd32_compat_43.c
diff -u src/sys/compat/netbsd32/netbsd32_compat_43.c:1.55 src/sys/compat/netbsd32/netbsd32_compat_43.c:1.56
--- src/sys/compat/netbsd32/netbsd32_compat_43.c:1.55	Fri Mar 16 13:25:04 2018
+++ src/sys/compat/netbsd32/netbsd32_compat_43.c	Thu May  3 17:43:33 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_compat_43.c,v 1.55 2018/03/16 17:25:04 christos Exp $	*/
+/*	$NetBSD: netbsd32_compat_43.c,v 1.56 2018/05/03 21:43:33 christos Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_43.c,v 1.55 2018/03/16 17:25:04 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_43.c,v 1.56 2018/05/03 21:43:33 christos Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_43.h"
@@ -451,7 +451,7 @@ compat_43_netbsd32_orecvmsg(struct lwp *
 	msg.msg_iov	= iov;
 	msg.msg_flags	= SCARG(uap, flags) & MSG_USERFLAGS;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
 	    NETBSD32PTR64(omsg.msg_accrights) != NULL ? &control : NULL,
 	    retval);
 	if (error != 0)
@@ -548,7 +548,7 @@ compat_43_netbsd32_osendmsg(struct lwp *
 	}
 
 	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
-	    &omsg, sizeof(omsg), retval);
+	    retval);
 
     out:
 	if (iov != aiov)

Index: src/sys/compat/netbsd32/netbsd32_socket.c
diff -u src/sys/compat/netbsd32/netbsd32_socket.c:1.44 src/sys/compat/netbsd32/netbsd32_socket.c:1.45
--- src/sys/compat/netbsd32/netbsd32_socket.c:1.44	Tue Sep 13 03:01:07 2016
+++ src/sys/compat/netbsd32/netbsd32_socket.c	Thu May  3 17:43:33 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_socket.c,v 1.44 2016/09/13 07:01:07 martin Exp $	*/
+/*	$NetBSD: netbsd32_socket.c,v 1.45 2018/05/03 21:43:33 christos Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_socket.c,v 1.44 2016/09/13 07:01:07 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_socket.c,v 1.45 2018/05/03 21:43:33 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -53,26 +53,29 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_soc
  */
 
 #define	CMSG32_ALIGN(n)	(((n) + ALIGNBYTES32) & ~ALIGNBYTES32)
-#define	CMSG32_DATA(cmsg) \
-	((u_char *)(void *)(cmsg) + CMSG32_ALIGN(sizeof(struct cmsghdr)))
-
-#define	CMSG32_NXTHDR(mhdr, cmsg)	\
-	(((char *)(cmsg) + CMSG32_ALIGN((cmsg)->cmsg_len) + \
-			    CMSG32_ALIGN(sizeof(struct cmsghdr)) > \
-	    (((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen)) ? \
-	    (struct cmsghdr *)0 : \
-	    (struct cmsghdr *)((char *)(cmsg) + \
-	        CMSG32_ALIGN((cmsg)->cmsg_len)))
+#define CMSG32_ASIZE	CMSG32_ALIGN(sizeof(struct cmsghdr))
+#define	CMSG32_DATA(cmsg) (__CASTV(u_char *, cmsg) + CMSG32_ASIZE)
+#define CMSG32_MSGNEXT(ucmsg, kcmsg) \
+    (__CASTV(char *, kcmsg) + CMSG32_ALIGN((ucmsg)->cmsg_len))
+#define CMSG32_MSGEND(mhdr) \
+    (__CASTV(char *, (mhdr)->msg_control) + (mhdr)->msg_controllen)
+
+#define	CMSG32_NXTHDR(mhdr, ucmsg, kcmsg)	\
+    __CASTV(struct cmsghdr *,  \
+	CMSG32_MSGNEXT(ucmsg, kcmsg) + \
+	CMSG32_ASIZE > CMSG32_MSGEND(mhdr) ? 0 : \
+	CMSG32_MSGNEXT(ucmsg, kcmsg))
 #define	CMSG32_FIRSTHDR(mhdr) \
-	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
-	 (struct cmsghdr *)(mhdr)->msg_control : \
-	 (struct cmsghdr *)0)
+    __CASTV(struct cmsghdr *, \
+	(mhdr)->msg_controllen < sizeof(struct cmsghdr) ? 0 : \
+	(mhdr)->msg_control)
 
 #define CMSG32_SPACE(l)	(CMSG32_ALIGN(sizeof(struct cmsghdr)) + CMSG32_ALIGN(l))
 #define CMSG32_LEN(l)	(CMSG32_ALIGN(sizeof(struct cmsghdr)) + (l))
 
 static int
-copyout32_msg_control_mbuf(struct lwp *l, struct msghdr *mp, int *len, struct mbuf *m, char **q, bool *truncated)
+copyout32_msg_control_mbuf(struct lwp *l, struct msghdr *mp, int *len,
+    struct mbuf *m, char **q, bool *truncated)
 {
 	struct cmsghdr *cmsg, cmsg32;
 	int i, j, error;
@@ -98,12 +101,13 @@ copyout32_msg_control_mbuf(struct lwp *l
 			i = *len;
 		}
 
-		ktrkuser("msgcontrol", cmsg, cmsg->cmsg_len);
+		ktrkuser(mbuftypes[MT_CONTROL], cmsg, cmsg->cmsg_len);
 		error = copyout(&cmsg32, *q, MAX(i, sizeof(cmsg32)));
 		if (error)
 			return (error);
 		if (i > CMSG32_LEN(0)) {
-			error = copyout(CMSG_DATA(cmsg), *q + CMSG32_LEN(0), i - CMSG32_LEN(0));
+			error = copyout(CMSG_DATA(cmsg), *q + CMSG32_LEN(0),
+			    i - CMSG32_LEN(0));
 			if (error)
 				return (error);
 		}
@@ -139,7 +143,8 @@ copyout32_msg_control(struct lwp *l, str
 	q = (char *)mp->msg_control;
 
 	for (m = control; len > 0 && m != NULL; m = m->m_next) {
-		error = copyout32_msg_control_mbuf(l, mp, &len, m, &q, &truncated);
+		error = copyout32_msg_control_mbuf(l, mp, &len, m, &q,
+		    &truncated);
 		if (truncated) {
 			m = control;
 			break;
@@ -155,7 +160,8 @@ copyout32_msg_control(struct lwp *l, str
 }
 
 int
-netbsd32_recvmsg(struct lwp *l, const struct netbsd32_recvmsg_args *uap, register_t *retval)
+netbsd32_recvmsg(struct lwp *l, const struct netbsd32_recvmsg_args *uap,
+    register_t *retval)
 {
 	/* {
 		syscallarg(int) s;
@@ -193,7 +199,7 @@ netbsd32_recvmsg(struct lwp *l, const st
 	msg.msg_iov = iov;
 	msg.msg_iovlen = msg32.msg_iovlen;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &msg32, sizeof(msg32),
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg,
 	    &from, msg.msg_control != NULL ? &control : NULL, retval);
 	if (error != 0)
 		goto done;
@@ -210,7 +216,7 @@ netbsd32_recvmsg(struct lwp *l, const st
 		msg32.msg_namelen = msg.msg_namelen;
 		msg32.msg_controllen = msg.msg_controllen;
 		msg32.msg_flags = msg.msg_flags;
-		ktrkuser("msghdr", &msg32, sizeof msg32);
+		ktrkuser("msghdr", &msg, sizeof msg);
 		error = copyout(&msg32, SCARG_P32(uap, msg), sizeof(msg32));
 	}
 
@@ -238,8 +244,8 @@ copyin32_msg_control(struct lwp *l, stru
 	control = mtod(ctl_mbuf, void *);
 	memset(control, 0, clen);
 
-	cc = CMSG32_FIRSTHDR(mp);
-	do {
+	for (cc = CMSG32_FIRSTHDR(mp); cc; cc = CMSG32_NXTHDR(mp, &cmsg32, cc))
+	{
 		error = copyin(cc, &cmsg32, sizeof(cmsg32));
 		if (error)
 			goto failure;
@@ -293,10 +299,11 @@ copyin32_msg_control(struct lwp *l, stru
 		    cmsg32.cmsg_len - CMSG32_LEN(0));
 		if (error)
 			goto failure;
+		ktrkuser(mbuftypes[MT_CONTROL], cmsg, cmsg->cmsg_len);
 
 		resid -= CMSG32_ALIGN(cmsg32.cmsg_len);
-		cidx += cmsg->cmsg_len;
-	} while (resid > 0 && (cc = CMSG32_NXTHDR(mp, &cmsg32)));
+		cidx += CMSG_ALIGN(cmsg->cmsg_len);
+	}
 
 	/* If we allocated a buffer, attach to mbuf */
 	if (cidx > MLEN) {
@@ -309,8 +316,6 @@ copyin32_msg_control(struct lwp *l, stru
 	mp->msg_control = ctl_mbuf;
 	mp->msg_flags |= MSG_CONTROLMBUF;
 
-	ktrkuser("msgcontrol", mtod(ctl_mbuf, void *),
-	    mp->msg_controllen);
 
 	return 0;
 
@@ -322,7 +327,8 @@ failure:
 }
 
 int
-netbsd32_sendmsg(struct lwp *l, const struct netbsd32_sendmsg_args *uap, register_t *retval)
+netbsd32_sendmsg(struct lwp *l, const struct netbsd32_sendmsg_args *uap,
+    register_t *retval)
 {
 	/* {
 		syscallarg(int) s;
@@ -368,7 +374,7 @@ netbsd32_sendmsg(struct lwp *l, const st
 	msg.msg_iov = iov;
 
 	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
-	    &msg32, sizeof(msg32), retval);
+	    retval);
 	/* msg.msg_control freed by do_sys_sendmsg() */
 
 	if (iov != aiov)
@@ -384,7 +390,8 @@ out:
 }
 
 int
-netbsd32_recvfrom(struct lwp *l, const struct netbsd32_recvfrom_args *uap, register_t *retval)
+netbsd32_recvfrom(struct lwp *l, const struct netbsd32_recvfrom_args *uap,
+    register_t *retval)
 {
 	/* {
 		syscallarg(int) s;
@@ -407,20 +414,20 @@ netbsd32_recvfrom(struct lwp *l, const s
 	msg.msg_control = NULL;
 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, ~0U,
-	    &from, NULL, retval);
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
 	if (error != 0)
 		return error;
 
-	error = copyout_sockname(SCARG_P32(uap, from), SCARG_P32(uap, fromlenaddr),
-	    MSG_LENUSRSPACE, from);
+	error = copyout_sockname(SCARG_P32(uap, from),
+	    SCARG_P32(uap, fromlenaddr), MSG_LENUSRSPACE, from);
 	if (from != NULL)
 		m_free(from);
 	return error;
 }
 
 int
-netbsd32_sendto(struct lwp *l, const struct netbsd32_sendto_args *uap, register_t *retval)
+netbsd32_sendto(struct lwp *l, const struct netbsd32_sendto_args *uap,
+    register_t *retval)
 {
 	/* {
 		syscallarg(int) s;
@@ -442,5 +449,5 @@ netbsd32_sendto(struct lwp *l, const str
 	aiov.iov_len = SCARG(uap, len);
 	msg.msg_flags = 0;
 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
-	    NULL, ~0U, retval);
+	    retval);
 }

Index: src/sys/kern/uipc_syscalls.c
diff -u src/sys/kern/uipc_syscalls.c:1.192 src/sys/kern/uipc_syscalls.c:1.193
--- src/sys/kern/uipc_syscalls.c:1.192	Fri Mar 16 13:25:04 2018
+++ src/sys/kern/uipc_syscalls.c	Thu May  3 17:43:33 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls.c,v 1.192 2018/03/16 17:25:04 christos Exp $	*/
+/*	$NetBSD: uipc_syscalls.c,v 1.193 2018/05/03 21:43:33 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.192 2018/03/16 17:25:04 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.193 2018/05/03 21:43:33 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pipe.h"
@@ -73,7 +73,6 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_syscall
 #include <sys/proc.h>
 #include <sys/file.h>
 #include <sys/buf.h>
-#define MBUFTYPES
 #include <sys/mbuf.h>
 #include <sys/protosw.h>
 #include <sys/socket.h>
@@ -504,7 +503,7 @@ sys_sendto(struct lwp *l, const struct s
 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
 	aiov.iov_len = SCARG(uap, len);
 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
-	    NULL, 0, retval);
+	    retval);
 }
 
 int
@@ -525,13 +524,12 @@ sys_sendmsg(struct lwp *l, const struct 
 
 	msg.msg_flags = MSG_IOVUSRSPACE;
 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
-	    NULL, 0, retval);
+	    retval);
 }
 
 int
 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
-    struct msghdr *mp, int flags, const void *kthdr, size_t ktsize,
-    register_t *retsize)
+    struct msghdr *mp, int flags, register_t *retsize)
 {
 
 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
@@ -541,12 +539,7 @@ do_sys_sendmsg_so(struct lwp *l, int s, 
 	size_t		len, iovsz;
 	int		i, error;
 
-	if (__predict_false(kthdr == NULL && ktsize == 0)) {
-		kthdr = mp;
-		ktsize = sizeof(*mp);
-	}
-	if (__predict_true(kthdr != NULL))
-		ktrkuser("msghdr", kthdr, ktsize);
+	ktrkuser("msghdr", mp, sizeof(*mp));
 
 	/* If the caller passed us stuff in mbufs, we must free them. */
 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
@@ -662,7 +655,7 @@ bad:
 
 int
 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
-    const void *kthdr, size_t ktsize, register_t *retsize)
+    register_t *retsize)
 {
 	int		error;
 	struct socket	*so;
@@ -676,8 +669,7 @@ do_sys_sendmsg(struct lwp *l, int s, str
 			m_freem(mp->msg_control);
 		return error;
 	}
-	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, kthdr, ktsize,
-	    retsize);
+	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
 	/* msg_name and msg_control freed */
 	fd_putfile(s);
 	return error;
@@ -708,8 +700,7 @@ sys_recvfrom(struct lwp *l, const struct
 	msg.msg_control = NULL;
 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
-	    NULL, retval);
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
 	if (error != 0)
 		return error;
 
@@ -739,7 +730,7 @@ sys_recvmsg(struct lwp *l, const struct 
 
 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
 
-	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, NULL, 0, &from,
+	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
 	    msg.msg_control != NULL ? &control : NULL, retval);
 	if (error != 0)
 		return error;
@@ -753,7 +744,7 @@ sys_recvmsg(struct lwp *l, const struct 
 	if (from != NULL)
 		m_free(from);
 	if (error == 0) {
-		ktrkuser("msghdr", &msg, sizeof msg);
+		ktrkuser("msghdr", &msg, sizeof(msg));
 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
 	}
 
@@ -794,12 +785,11 @@ sys_sendmmsg(struct lwp *l, const struct
 
 		msg->msg_flags = flags;
 
-		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags,
-		    &msg, sizeof(msg), retval);
+		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
 		if (error)
 			break;
 
-		ktrkuser("msghdr", msg, sizeof *msg);
+		ktrkuser("msghdr", msg, sizeof(*msg));
 		mmsg.msg_len = *retval;
 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
 		if (error)
@@ -899,7 +889,7 @@ copyout_msg_control(struct lwp *l, struc
 			i = len;
 		}
 		error = copyout(mtod(m, void *), q, i);
-		ktrkuser("msgcontrol", mtod(m, void *), i);
+		ktrkuser(mbuftypes[MT_CONTROL], cmsg, cmsg->cmsg_len);
 		if (error != 0) {
 			/* We must free all the SCM_RIGHTS */
 			m = control;
@@ -922,20 +912,14 @@ copyout_msg_control(struct lwp *l, struc
 
 int
 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
-    const void *ktrhdr, size_t ktsize, struct mbuf **from,
-    struct mbuf **control, register_t *retsize)
+    struct mbuf **from, struct mbuf **control, register_t *retsize)
 {
 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
 	struct uio	auio;
 	size_t		len, iovsz;
 	int		i, error;
 
-	if (__predict_false(ktrhdr == NULL && ktsize == 0)) {
-		ktrhdr = mp;
-		ktsize = sizeof *mp;
-	}
-	if (__predict_true(ktrhdr != NULL))
-		ktrkuser("msghdr", ktrhdr, ktsize);
+	ktrkuser("msghdr", mp, sizeof(*mp));
 
 	*from = NULL;
 	if (control != NULL)
@@ -1019,7 +1003,6 @@ do_sys_recvmsg_so(struct lwp *l, int s, 
 
 int
 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp,
-    const void *ktrhdr, size_t ktrsize,
     struct mbuf **from, struct mbuf **control, register_t *retsize)
 {
 	int error;
@@ -1027,8 +1010,7 @@ do_sys_recvmsg(struct lwp *l, int s, str
 
 	if ((error = fd_getsock(s, &so)) != 0)
 		return error;
-	error = do_sys_recvmsg_so(l, s, so, mp, ktrhdr, ktrsize, from,
-	    control, retsize);
+	error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize);
 	fd_putfile(s);
 	return error;
 }
@@ -1082,7 +1064,7 @@ sys_recvmmsg(struct lwp *l, const struct
 			from = NULL;
 		}
 
-		error = do_sys_recvmsg_so(l, s, so, msg, NULL, 0, &from,
+		error = do_sys_recvmsg_so(l, s, so, msg, &from,
 		    msg->msg_control != NULL ? &control : NULL, retval);
 		if (error) {
 			if (error == EAGAIN && dg > 0)
@@ -1532,7 +1514,6 @@ int
 sockargs(struct mbuf **mp, const void *bf, size_t buflen, enum uio_seg seg,
     int type)
 {
-	struct sockaddr	*sa;
 	struct mbuf	*m;
 	int		error;
 
@@ -1567,13 +1548,15 @@ sockargs(struct mbuf **mp, const void *b
 			(void)m_free(m);
 			return error;
 		}
-		ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
 	} else {
 		memcpy(mtod(m, void *), bf, buflen);
 	}
 	*mp = m;
-	if (type == MT_SONAME) {
-		sa = mtod(m, struct sockaddr *);
+	switch (type) {
+	case MT_SONAME:
+		ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
+
+		struct sockaddr *sa = mtod(m, struct sockaddr *);
 #if BYTE_ORDER != BIG_ENDIAN
 		/*
 		 * 4.3BSD compat thing - need to stay, since bind(2),
@@ -1583,6 +1566,20 @@ sockargs(struct mbuf **mp, const void *b
 			sa->sa_family = sa->sa_len;
 #endif
 		sa->sa_len = buflen;
+		return 0;
+	case MT_CONTROL:
+		if (!KTRPOINT(curproc, KTR_USER))
+			return 0;
+
+		struct msghdr mhdr;
+		mhdr.msg_control = mtod(m, void *);
+		mhdr.msg_controllen = buflen;
+		for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mhdr); cmsg;
+		    cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
+			ktrkuser(mbuftypes[type], cmsg, cmsg->cmsg_len);
+		}
+		return 0;
+	default:
+		return EINVAL;
 	}
-	return 0;
 }

Index: src/sys/sys/param.h
diff -u src/sys/sys/param.h:1.559 src/sys/sys/param.h:1.560
--- src/sys/sys/param.h:1.559	Fri Mar 16 13:25:04 2018
+++ src/sys/sys/param.h	Thu May  3 17:43:33 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.559 2018/03/16 17:25:04 christos Exp $	*/
+/*	$NetBSD: param.h,v 1.560 2018/05/03 21:43:33 christos Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -67,7 +67,7 @@
  *	2.99.9		(299000900)
  */
 
-#define	__NetBSD_Version__	899001400	/* NetBSD 8.99.14 */
+#define	__NetBSD_Version__	899001500	/* NetBSD 8.99.15 */
 
 #define __NetBSD_Prereq__(M,m,p) (((((M) * 100000000) + \
     (m) * 1000000) + (p) * 100) <= __NetBSD_Version__)

Reply via email to