git: ec45f952a232 - main - sockbuf: Add KMSAN checks to sbappend*()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 27 Apr 2023 17:19:04 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=ec45f952a232068ba17b091e96a8b6e79bcec85a

commit ec45f952a232068ba17b091e96a8b6e79bcec85a
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-04-27 16:58:56 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-04-27 16:58:56 +0000

    sockbuf: Add KMSAN checks to sbappend*()
    
    Otherwise KMSAN only detects uninitialized memory when the contents of
    the buffer are copied out to userspace or transmitted to a network
    interface.  At that point the KMSAN violation will be far removed from
    its origin, so let's try to make debugging such problems a bit easier.
    
    Reviewed by:    glebius
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D38101
---
 sys/kern/uipc_sockbuf.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/sys/kern/uipc_sockbuf.c b/sys/kern/uipc_sockbuf.c
index 61ec0c794270..170f67be4216 100644
--- a/sys/kern/uipc_sockbuf.c
+++ b/sys/kern/uipc_sockbuf.c
@@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
+#include <sys/msan.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/protosw.h>
@@ -908,6 +909,7 @@ sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
 
 	if (m == NULL)
 		return;
+	kmsan_check_mbuf(m, "sbappend");
 	sbm_clrprotoflags(m, flags);
 	SBLASTRECORDCHK(sb);
 	n = sb->sb_mb;
@@ -1022,6 +1024,8 @@ sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
 
 	KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
 
+	kmsan_check_mbuf(m, "sbappend");
+
 #ifdef KERN_TLS
 	/*
 	 * Decrypted TLS records are appended as records via
@@ -1170,7 +1174,10 @@ sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
 
 	if (m0 == NULL)
 		return;
+
+	kmsan_check_mbuf(m0, "sbappend");
 	m_clrprotoflags(m0);
+
 	/*
 	 * Put the first mbuf on the queue.  Note this permits zero length
 	 * records.
@@ -1207,6 +1214,12 @@ sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
     struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
 {
 	struct mbuf *m, *n, *nlast;
+
+	if (m0 != NULL)
+		kmsan_check_mbuf(m0, "sbappend");
+	if (control != NULL)
+		kmsan_check_mbuf(control, "sbappend");
+
 #if MSIZE <= 256
 	if (asa->sa_len > MLEN)
 		return (0);
@@ -1317,6 +1330,9 @@ sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
 {
 	struct mbuf *m, *mlast;
 
+	kmsan_check_mbuf(m0, "sbappend");
+	kmsan_check_mbuf(control, "sbappend");
+
 	sbm_clrprotoflags(m0, flags);
 	m_last(control)->m_next = m0;