Remove M_PKTHDR from secondary mbufs when reassembling packets.

This is a real problem, because I found at least one component that relies
on the fact that only the first mbuf has M_PKTHDR: far from here, in
m_splithdr, we don't update m->m_pkthdr.len if M_PKTHDR is found in a
secondary mbuf. (The initial intention there was to avoid updating
m_pkthdr.len twice, the assumption was that if M_PKTHDR is set then we're
dealing with the first mbuf.) Therefore, when handling fragmented IPsec
packets (in particular IPv6, IPv4 is a bit more complicated), we may end
up with an incorrect m_pkthdr.len after authentication or decryption. In
the case of ESP, this can lead to a remote crash on this instruction:

	m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);

m_pkthdr.len is bigger than the actual mbuf chain.

It seems possible to me to trigger this bug even if you don't have the ESP
key, because the fragmentation part is outside of the encrypted ESP
payload.

So if you MITM the target, and intercept an incoming ESP packet (which you
can't decrypt), you should be able to forge a new specially-crafted,
fragmented packet and stuff the ESP payload (still encrypted, as you
intercepted it) into it. The decryption succeeds and the target crashes.
This commit is contained in:
maxv 2018-03-09 11:57:38 +00:00
parent 796d7c9466
commit 47afec3d88
4 changed files with 25 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: uipc_mbuf.c,v 1.181 2018/01/22 15:05:27 maxv Exp $ */
/* $NetBSD: uipc_mbuf.c,v 1.182 2018/03/09 11:57:38 maxv Exp $ */
/*
* Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
@ -62,7 +62,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.181 2018/01/22 15:05:27 maxv Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.182 2018/03/09 11:57:38 maxv Exp $");
#ifdef _KERNEL_OPT
#include "opt_mbuftrace.h"
@ -455,6 +455,16 @@ mb_ctor(void *arg, void *object, int flags)
return (0);
}
void
m_pkthdr_remove(struct mbuf *m)
{
KASSERT(m->m_flags & M_PKTHDR);
m_tag_delete_chain(m, NULL);
m->m_flags &= ~M_PKTHDR;
memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
}
/*
* Add mbuf to the end of a chain
*/

View File

@ -1,4 +1,4 @@
/* $NetBSD: ip_reass.c,v 1.13 2018/02/08 10:03:52 maxv Exp $ */
/* $NetBSD: ip_reass.c,v 1.14 2018/03/09 11:57:38 maxv Exp $ */
/*
* Copyright (c) 1982, 1986, 1988, 1993
@ -46,7 +46,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ip_reass.c,v 1.13 2018/02/08 10:03:52 maxv Exp $");
__KERNEL_RCSID(0, "$NetBSD: ip_reass.c,v 1.14 2018/03/09 11:57:38 maxv Exp $");
#include <sys/param.h>
#include <sys/types.h>
@ -389,6 +389,7 @@ insert:
t = q->ipqe_m;
nq = TAILQ_NEXT(q, ipqe_q);
pool_cache_put(ipfren_cache, q);
m_pkthdr_remove(t);
m_cat(m, t);
}
@ -406,7 +407,8 @@ insert:
m->m_data -= (ip->ip_hl << 2);
/* Fix up mbuf. XXX This should be done elsewhere. */
if (m->m_flags & M_PKTHDR) {
{
KASSERT(m->m_flags & M_PKTHDR);
int plen = 0;
for (t = m; t; t = t->m_next) {
plen += t->m_len;

View File

@ -1,4 +1,4 @@
/* $NetBSD: frag6.c,v 1.66 2018/02/07 09:53:08 maxv Exp $ */
/* $NetBSD: frag6.c,v 1.67 2018/03/09 11:57:38 maxv Exp $ */
/* $KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun Exp $ */
/*
@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.66 2018/02/07 09:53:08 maxv Exp $");
__KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.67 2018/03/09 11:57:38 maxv Exp $");
#ifdef _KERNEL_OPT
#include "opt_net_mpsafe.h"
@ -434,6 +434,7 @@ insert:
t = t->m_next;
t->m_next = af6->ip6af_m;
m_adj(t->m_next, af6->ip6af_offset);
m_pkthdr_remove(t->m_next);
kmem_intr_free(af6, sizeof(struct ip6asfrag));
af6 = af6dwn;
}
@ -472,12 +473,10 @@ insert:
kmem_intr_free(q6, sizeof(struct ip6q));
frag6_nfragpackets--;
if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
{
KASSERT(m->m_flags & M_PKTHDR);
int plen = 0;
for (t = m; t; t = t->m_next) {
/*
* XXX XXX Why don't we remove M_PKTHDR?
*/
plen += t->m_len;
}
m->m_pkthdr.len = plen;

View File

@ -1,4 +1,4 @@
/* $NetBSD: mbuf.h,v 1.178 2018/02/28 10:30:20 maxv Exp $ */
/* $NetBSD: mbuf.h,v 1.179 2018/03/09 11:57:38 maxv Exp $ */
/*
* Copyright (c) 1996, 1997, 1999, 2001, 2007 The NetBSD Foundation, Inc.
@ -837,6 +837,8 @@ extern struct mowner revoked_mowner;
MALLOC_DECLARE(M_MBUF);
MALLOC_DECLARE(M_SONAME);
void m_pkthdr_remove(struct mbuf *);
struct mbuf *m_copym(struct mbuf *, int, int, int);
struct mbuf *m_copypacket(struct mbuf *, int);
struct mbuf *m_devget(char *, int, int, struct ifnet *,