Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/net/lagg lagg(4): Safely handle misaligned mbufs.



details:   https://anonhg.NetBSD.org/src/rev/5988b4ea4b6b
branches:  trunk
changeset: 368169:5988b4ea4b6b
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Jun 26 17:55:24 2022 +0000

description:
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

diffstat:

 sys/net/lagg/if_lagg.c      |  18 ++++++++++--------
 sys/net/lagg/if_laggproto.h |   8 +++++---
 2 files changed, 15 insertions(+), 11 deletions(-)

diffs (103 lines):

diff -r 9257c0e20c4e -r 5988b4ea4b6b sys/net/lagg/if_lagg.c
--- a/sys/net/lagg/if_lagg.c    Sun Jun 26 17:53:06 2022 +0000
+++ b/sys/net/lagg/if_lagg.c    Sun Jun 26 17:55:24 2022 +0000
@@ -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 <reyk%openbsd.org@localhost>
@@ -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 @@
        *(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 @@
        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 @@
 
        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 @@
                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 @@
 
        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 @@
                }
                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;
 
diff -r 9257c0e20c4e -r 5988b4ea4b6b sys/net/lagg/if_laggproto.h
--- a/sys/net/lagg/if_laggproto.h       Sun Jun 26 17:53:06 2022 +0000
+++ b/sys/net/lagg/if_laggproto.h       Sun Jun 26 17:55:24 2022 +0000
@@ -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 @@
        (_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 @@
                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);



Home | Main Index | Thread Index | Old Index