Module Name:    src
Committed By:   riastradh
Date:           Sun Jun 26 17:55:24 UTC 2022

Modified Files:
        src/sys/net/lagg: if_lagg.c if_laggproto.h

Log Message:
lagg(4): Safely handle misaligned mbufs.

Optimizing for non-strict-alignment architectures -- without falling
afoul of alignment sanitizers or overeager compilers -- is left as an
exercise for the reader.

PR kern/56894


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/net/lagg/if_lagg.c
cvs rdiff -u -r1.17 -r1.18 src/sys/net/lagg/if_laggproto.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/net/lagg/if_lagg.c
diff -u src/sys/net/lagg/if_lagg.c:1.47 src/sys/net/lagg/if_lagg.c:1.48
--- src/sys/net/lagg/if_lagg.c:1.47	Mon Apr  4 09:59:41 2022
+++ src/sys/net/lagg/if_lagg.c	Sun Jun 26 17:55:24 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_lagg.c,v 1.47 2022/04/04 09:59:41 martin Exp $	*/
+/*	$NetBSD: if_lagg.c,v 1.48 2022/06/26 17:55:24 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2005, 2006 Reyk Floeter <r...@openbsd.org>
@@ -20,7 +20,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_lagg.c,v 1.47 2022/04/04 09:59:41 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_lagg.c,v 1.48 2022/06/26 17:55:24 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -895,7 +895,7 @@ lagg_hashmbuf(struct lagg_softc *sc, str
 	*(hp) = hash32_buf(&(v), sizeof(v), *(hp));	\
 } while(0)
 
-	eh = lagg_m_extract(m, 0, sizeof(*eh), &buf);
+	eh = lagg_m_extract(m, 0, sizeof(*eh), __alignof(*eh), &buf);
 	if (eh == NULL)
 		goto out;
 
@@ -903,7 +903,8 @@ lagg_hashmbuf(struct lagg_softc *sc, str
 	etype = ntohs(eh->ether_type);
 
 	if (etype == ETHERTYPE_VLAN) {
-		evl = lagg_m_extract(m, 0, sizeof(*evl), &buf);
+		evl = lagg_m_extract(m, 0, sizeof(*evl), __alignof(*evl),
+		    &buf);
 		if (evl == NULL)
 			goto out;
 
@@ -924,7 +925,7 @@ lagg_hashmbuf(struct lagg_softc *sc, str
 
 	switch (etype) {
 	case ETHERTYPE_IP:
-		ip = lagg_m_extract(m, off, sizeof(*ip), &buf);
+		ip = lagg_m_extract(m, off, sizeof(*ip), __alignof(*ip), &buf);
 		if (ip == NULL)
 			goto out;
 
@@ -937,7 +938,8 @@ lagg_hashmbuf(struct lagg_softc *sc, str
 		proto = ip->ip_p;
 		break;
 	case ETHERTYPE_IPV6:
-		ip6 = lagg_m_extract(m, off, sizeof(*ip6), &buf);
+		ip6 = lagg_m_extract(m, off, sizeof(*ip6), __alignof(*ip6),
+		    &buf);
 		if (ip6 == NULL)
 			goto out;
 
@@ -957,7 +959,7 @@ lagg_hashmbuf(struct lagg_softc *sc, str
 
 	switch (proto) {
 	case IPPROTO_TCP:
-		th = lagg_m_extract(m, off, sizeof(*th), &buf);
+		th = lagg_m_extract(m, off, sizeof(*th), __alignof(*th), &buf);
 		if (th == NULL)
 			goto out;
 
@@ -967,7 +969,7 @@ lagg_hashmbuf(struct lagg_softc *sc, str
 		}
 		break;
 	case IPPROTO_UDP:
-		uh = lagg_m_extract(m, off, sizeof(*uh), &buf);
+		uh = lagg_m_extract(m, off, sizeof(*uh), __alignof(*uh), &buf);
 		if (uh == NULL)
 			goto out;
 

Index: src/sys/net/lagg/if_laggproto.h
diff -u src/sys/net/lagg/if_laggproto.h:1.17 src/sys/net/lagg/if_laggproto.h:1.18
--- src/sys/net/lagg/if_laggproto.h:1.17	Tue May 24 20:50:20 2022
+++ src/sys/net/lagg/if_laggproto.h	Sun Jun 26 17:55:24 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_laggproto.h,v 1.17 2022/05/24 20:50:20 andvar Exp $	*/
+/*	$NetBSD: if_laggproto.h,v 1.18 2022/06/26 17:55:24 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2021 Internet Initiative Japan Inc.
@@ -217,7 +217,8 @@ struct lagg_softc {
 	(_lp)->lp_ioctl((_lp)->lp_ifp, (_cmd), (_data))
 
 static inline const void *
-lagg_m_extract(struct mbuf *m, size_t off, size_t reqlen, void *buf)
+lagg_m_extract(struct mbuf *m, size_t off, size_t reqlen, size_t align,
+    void *buf)
 {
 	ssize_t len;
 	const void *rv;
@@ -229,7 +230,8 @@ lagg_m_extract(struct mbuf *m, size_t of
 		return NULL;
 	}
 
-	if (m->m_len >= len) {
+	if (m->m_len >= len &&
+	    ((uintptr_t)(mtod(m, uint8_t *) + off) % align) == 0) {
 		rv = mtod(m, uint8_t *) + off;
 	} else {
 		m_copydata(m, off, reqlen, buf);

Reply via email to