As I stated before, I'm a fish out of water with C; however Claude Code
suggested the below diff which _seems_ fine.

--- a/sys/net/if_wg.c
+++ b/sys/net/if_wg.c
@@ -1591,7 +1591,30 @@ wg_encap(struct wg_softc *sc, struct mbuf *m)
        t = wg_tag_get(m);
        peer = t->t_peer;

+       /*
+        * t_mtu is a snapshot of if_mtu taken in wg_output, before the
+        * packet was staged for asynchronous encryption. Reject anything
+        * that already exceeds it rather than silently encapsulating an
+        * oversized plaintext; t_mtu == 0 marks a keepalive, which is
+        * always allowed through.
+        */
+       if (t->t_mtu != 0 && m->m_pkthdr.len > t->t_mtu) {
+               WGPRINTF(LOG_DEBUG, sc, NULL, "Packet too big (%d > %d) "
+                   "for peer %llu, dropping\n", m->m_pkthdr.len, t->t_mtu,
+                   peer->p_id);
+               goto error;
+       }
+
+       /*
+        * Padding rounds up to the next 16-byte boundary, which can push
+        * the plaintext past t_mtu even though the unpadded packet fit.
+        * Clamp the padding target so the padded plaintext never exceeds
+        * the interface MTU; m->m_pkthdr.len <= t_mtu is now guaranteed
+        * above, so this can never clamp below m->m_pkthdr.len.
+        */
        plaintext_len = WG_PKT_WITH_PADDING(m->m_pkthdr.len);
+       if (t->t_mtu != 0 && plaintext_len > (size_t)t->t_mtu)
+               plaintext_len = t->t_mtu;
        padding_len = plaintext_len - m->m_pkthdr.len;
        out_len = sizeof(struct wg_pkt_data) + plaintext_len +
            NOISE_AUTHTAG_LEN;

Reply via email to