Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/netbsd-8]: src/sys/net/npf Pull up following revision(s) (requested by m...
details: https://anonhg.NetBSD.org/src/rev/96ea4f212b86
branches: netbsd-8
changeset: 434993:96ea4f212b86
user: martin <martin%NetBSD.org@localhost>
date: Mon May 14 19:22:30 2018 +0000
description:
Pull up following revision(s) (requested by maxv in ticket #823):
sys/net/npf/npf_inet.c: revision 1.45-1.47
sys/net/npf/npf_alg_icmp.c: revision 1.27-1.30
sys/net/npf/npf_sendpkt.c: revision 1.19
Fix use-after-free.
The nbuf can be reallocated as a result of caching 'enpc', so it is
necessary to recache 'npc', otherwise it contains pointers to the freed
mbuf - pointers which are then used in the ruleset machinery.
We recache 'npc' when we are sure we won't use 'enpc' anymore, because
'enpc' can be clobbered as a result of caching 'npc' (in other words,
only one of the two can be cached at the same time).
Also, we recache 'npc' unconditionally, because there is no way to know
whether the nbuf got clobbered relatively to it. We can't use the
NBUF_DATAREF_RESET flag, because it is stored in the nbuf and not in the
cache.
Discussed with rmind@.
Change npf_cache_all so that it ensures the potential ICMP Query Id is in
the nbuf. In such a way that we don't need to ensure that later.
Change npfa_icmp4_inspect and npfa_icmp6_inspect so that they touch neither
the nbuf nor npc. Adapt their callers accordingly.
In the end, if a packet has a Query Id, we set NPC_ICMP_ID in npc and leave
right away, without recaching npc (not needed since we didn't touch the
nbuf).
This fixes the handling of Query Id packets (that I broke in my previous
commit), and also fixes another possible use-after-free.
Retrieve the complete IPv4 header right away, and make sure we did retrieve
the IPv6 option header we were iterating on.
Ah, fix compilation. I tested my previous change by loading the kernel
module from the filesystem, but the Makefile didn't have DIAGNOSTIC
enabled, and the two KASSERTs I added did not compile properly.
If we fail to advance inside TCP/UDP/ICMPv4/ICMPv6, stop pretending L4
is unknown, and error out right away.
This prevents bugs in machinery, if a place looks for L4 in 'npc_proto'
without checking the cache too. I've seen a ~similar problem already.
In addition to checking L4 in the cache, here we also need to check the
protocol. The NPF entry point does not ensure that
ICMPv6 can be set only in IPv6
ICMPv4 can be set only in IPv4
So we could have ICMPv6 in IPv4.
apply some INET6 so this compiles in INET6-less kernels again.
diffstat:
sys/net/npf/npf_alg_icmp.c | 125 +++++++++++++++++++++++++++++---------------
sys/net/npf/npf_inet.c | 45 +++++++++++----
sys/net/npf/npf_sendpkt.c | 10 ++-
3 files changed, 122 insertions(+), 58 deletions(-)
diffs (truncated from 441 to 300 lines):
diff -r 38d333fbc2cf -r 96ea4f212b86 sys/net/npf/npf_alg_icmp.c
--- a/sys/net/npf/npf_alg_icmp.c Mon May 14 19:17:39 2018 +0000
+++ b/sys/net/npf/npf_alg_icmp.c Mon May 14 19:22:30 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: npf_alg_icmp.c,v 1.24.8.1 2018/05/09 15:35:37 martin Exp $ */
+/* $NetBSD: npf_alg_icmp.c,v 1.24.8.2 2018/05/14 19:22:30 martin Exp $ */
/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
#ifdef _KERNEL
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.24.8.1 2018/05/09 15:35:37 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.24.8.2 2018/05/14 19:22:30 martin Exp $");
#include <sys/param.h>
#include <sys/module.h>
@@ -120,13 +120,15 @@
/*
* npfa_icmp{4,6}_inspect: retrieve unique identifiers - either ICMP query
* ID or TCP/UDP ports of the original packet, which is embedded.
+ *
+ * => Sets hasqid=true if the packet has a Query Id. In this case neither
+ * the nbuf nor npc is touched.
*/
static bool
-npfa_icmp4_inspect(const int type, npf_cache_t *npc)
+npfa_icmp4_inspect(const int type, npf_cache_t *npc, bool *hasqid)
{
nbuf_t *nbuf = npc->npc_nbuf;
- u_int offby;
/* Per RFC 792. */
switch (type) {
@@ -147,12 +149,8 @@
case ICMP_TSTAMPREPLY:
case ICMP_IREQ:
case ICMP_IREQREPLY:
- /* Should contain ICMP query ID - ensure. */
- offby = offsetof(struct icmp, icmp_id);
- if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
- return false;
- }
- npc->npc_info |= NPC_ICMP_ID;
+ /* Contains ICMP query ID. */
+ *hasqid = true;
return true;
default:
break;
@@ -161,10 +159,9 @@
}
static bool
-npfa_icmp6_inspect(const int type, npf_cache_t *npc)
+npfa_icmp6_inspect(const int type, npf_cache_t *npc, bool *hasqid)
{
nbuf_t *nbuf = npc->npc_nbuf;
- u_int offby;
/* Per RFC 4443. */
switch (type) {
@@ -180,12 +177,8 @@
case ICMP6_ECHO_REQUEST:
case ICMP6_ECHO_REPLY:
- /* Should contain ICMP query ID - ensure. */
- offby = offsetof(struct icmp6_hdr, icmp6_id);
- if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
- return false;
- }
- npc->npc_info |= NPC_ICMP_ID;
+ /* Contains ICMP query ID. */
+ *hasqid = true;
return true;
default:
break;
@@ -196,13 +189,13 @@
/*
* npfa_icmp_inspect: ALG ICMP inspector.
*
- * => Returns true if "enpc" is filled.
+ * => Returns false if there is a problem with the format.
*/
static bool
npfa_icmp_inspect(npf_cache_t *npc, npf_cache_t *enpc)
{
nbuf_t *nbuf = npc->npc_nbuf;
- bool ret;
+ bool ret, hasqid = false;
KASSERT(npf_iscached(npc, NPC_IP46));
KASSERT(npf_iscached(npc, NPC_ICMP));
@@ -220,12 +213,14 @@
* Inspect the ICMP packet. The relevant data might be in the
* embedded packet. Fill the "enpc" cache, if so.
*/
- if (npf_iscached(npc, NPC_IP4)) {
+ if (npf_iscached(npc, NPC_IP4) &&
+ npc->npc_proto == IPPROTO_ICMP) {
const struct icmp *ic = npc->npc_l4.icmp;
- ret = npfa_icmp4_inspect(ic->icmp_type, enpc);
- } else if (npf_iscached(npc, NPC_IP6)) {
+ ret = npfa_icmp4_inspect(ic->icmp_type, enpc, &hasqid);
+ } else if (npf_iscached(npc, NPC_IP6) &&
+ npc->npc_proto == IPPROTO_ICMPV6) {
const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
- ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc);
+ ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc, &hasqid);
} else {
ret = false;
}
@@ -234,25 +229,34 @@
}
/* ICMP ID is the original packet, just indicate it. */
- if (npf_iscached(enpc, NPC_ICMP_ID)) {
+ if (hasqid) {
npc->npc_info |= NPC_ICMP_ID;
- return false;
}
- /* Indicate that embedded packet is in the cache. */
return true;
}
static npf_conn_t *
npfa_icmp_conn(npf_cache_t *npc, int di)
{
+ npf_conn_t *conn = NULL;
npf_cache_t enpc;
+ bool hasqid = false;
/* Inspect ICMP packet for an embedded packet. */
if (!npf_iscached(npc, NPC_ICMP))
return NULL;
if (!npfa_icmp_inspect(npc, &enpc))
+ goto out;
+
+ /*
+ * If the ICMP packet had a Query Id, leave now. The packet didn't get
+ * modified, so no need to recache npc.
+ */
+ if (npf_iscached(npc, NPC_ICMP_ID)) {
+ KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
return NULL;
+ }
/*
* Invert the identifiers of the embedded packet.
@@ -280,24 +284,34 @@
break;
case IPPROTO_ICMP: {
const struct icmp *ic = enpc.npc_l4.icmp;
- ret = npfa_icmp4_inspect(ic->icmp_type, &enpc);
- if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
- return false;
+ ret = npfa_icmp4_inspect(ic->icmp_type, &enpc, &hasqid);
+ if (!ret || !hasqid)
+ goto out;
+ enpc.npc_info |= NPC_ICMP_ID;
break;
}
case IPPROTO_ICMPV6: {
const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
- ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc);
- if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
- return false;
+ ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc, &hasqid);
+ if (!ret || !hasqid)
+ goto out;
+ enpc.npc_info |= NPC_ICMP_ID;
break;
}
default:
- return false;
+ goto out;
}
/* Lookup a connection using the embedded packet. */
- return npf_conn_lookup(&enpc, di, &forw);
+ conn = npf_conn_lookup(&enpc, di, &forw);
+
+out:
+ /*
+ * Recache npc. The nbuf may have been updated as a result of
+ * caching enpc.
+ */
+ npf_recache(npc);
+ return conn;
}
/*
@@ -309,20 +323,32 @@
{
const u_int which = NPF_SRC;
npf_cache_t enpc;
+ struct icmp *ic;
+ uint16_t cksum;
if (forw || !npf_iscached(npc, NPC_ICMP))
return false;
- if (!npfa_icmp_inspect(npc, &enpc))
- return false;
-
- KASSERT(npf_iscached(&enpc, NPC_IP46));
- KASSERT(npf_iscached(&enpc, NPC_LAYER4));
/*
* ICMP: fetch the current checksum we are going to fixup.
*/
- struct icmp *ic = npc->npc_l4.icmp;
- uint16_t cksum = ic->icmp_cksum;
+ ic = npc->npc_l4.icmp;
+ cksum = ic->icmp_cksum;
+
+ if (!npfa_icmp_inspect(npc, &enpc))
+ goto err;
+
+ /*
+ * If the ICMP packet had a Query Id, leave now. The packet didn't get
+ * modified, so no need to recache npc.
+ */
+ if (npf_iscached(npc, NPC_ICMP_ID)) {
+ KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
+ return false;
+ }
+
+ KASSERT(npf_iscached(&enpc, NPC_IP46));
+ KASSERT(npf_iscached(&enpc, NPC_LAYER4));
CTASSERT(offsetof(struct icmp, icmp_cksum) ==
offsetof(struct icmp6_hdr, icmp6_cksum));
@@ -362,7 +388,7 @@
case IPPROTO_ICMPV6:
break;
default:
- return false;
+ goto err;
}
/*
@@ -377,7 +403,7 @@
* npfa_icmp_match() matches only for the PFIL_OUT traffic.
*/
if (npf_napt_rwr(&enpc, which, addr, port)) {
- return false;
+ goto err;
}
/*
@@ -401,8 +427,19 @@
}
break;
}
+ npf_recache(npc);
+ KASSERT(npf_iscached(npc, NPC_ICMP));
+ ic = npc->npc_l4.icmp;
ic->icmp_cksum = cksum;
return true;
+
+err:
+ /*
+ * Recache npc. The nbuf may have been updated as a result of
+ * caching enpc.
+ */
+ npf_recache(npc);
+ return false;
}
/*
diff -r 38d333fbc2cf -r 96ea4f212b86 sys/net/npf/npf_inet.c
--- a/sys/net/npf/npf_inet.c Mon May 14 19:17:39 2018 +0000
+++ b/sys/net/npf/npf_inet.c Mon May 14 19:22:30 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: npf_inet.c,v 1.37.6.1 2018/05/09 15:35:37 martin Exp $ */
+/* $NetBSD: npf_inet.c,v 1.37.6.2 2018/05/14 19:22:30 martin Exp $ */
/*-
* Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
#ifdef _KERNEL
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.37.6.1 2018/05/09 15:35:37 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.37.6.2 2018/05/14 19:22:30 martin Exp $");
#include <sys/param.h>
#include <sys/types.h>
@@ -336,10 +336,15 @@
return NPC_FMTERR;
}
- /* Check header length and fragment offset. */
+ /* Retrieve the complete header. */
if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
return NPC_FMTERR;
}
+ ip = nbuf_ensure_contig(nbuf, (u_int)(ip->ip_hl << 2));
Home |
Main Index |
Thread Index |
Old Index