Source-Changes-HG archive

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

[src/netbsd-6-1]: src/sys/net/npf Pull up following revision(s) via patch (re...



details:   https://anonhg.NetBSD.org/src/rev/b8b59497a5b0
branches:  netbsd-6-1
changeset: 776334:b8b59497a5b0
user:      martin <martin%NetBSD.org@localhost>
date:      Thu May 17 13:47:24 2018 +0000

description:
Pull up following revision(s) via patch (requested by maxv in ticket #1549):

        sys/net/npf/npf_inet.c: revision 1.45
        sys/net/npf/npf_alg_icmp.c: revision 1.27,1.28

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.

diffstat:

 sys/net/npf/npf_alg_icmp.c |  100 +++++++++++++++++++++++++++++---------------
 sys/net/npf/npf_inet.c     |   13 ++++-
 2 files changed, 75 insertions(+), 38 deletions(-)

diffs (283 lines):

diff -r e3220876b6fc -r b8b59497a5b0 sys/net/npf/npf_alg_icmp.c
--- a/sys/net/npf/npf_alg_icmp.c        Thu May 03 15:05:21 2018 +0000
+++ b/sys/net/npf/npf_alg_icmp.c        Thu May 17 13:47:24 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_alg_icmp.c,v 1.8.4.7 2013/02/11 21:49:49 riz Exp $ */
+/*     $NetBSD: npf_alg_icmp.c,v 1.8.4.7.2.1 2018/05/17 13:47:24 martin Exp $  */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.8.4.7 2013/02/11 21:49:49 riz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.8.4.7.2.1 2018/05/17 13:47:24 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/module.h>
@@ -162,12 +162,14 @@
 /*
  * 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, nbuf_t *nbuf)
+npfa_icmp4_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf, bool *hasqid)
 {
-       u_int offby;
 
        /* Per RFC 792. */
        switch (type) {
@@ -191,12 +193,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;
@@ -205,9 +203,8 @@
 }
 
 static bool
-npfa_icmp6_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf)
+npfa_icmp6_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf, bool *hasqid)
 {
-       u_int offby;
 
        /* Per RFC 4443. */
        switch (type) {
@@ -226,12 +223,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;
@@ -242,12 +235,12 @@
 /*
  * npfa_icmp_session: 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, nbuf_t *nbuf, npf_cache_t *enpc)
 {
-       bool ret;
+       bool ret, hasqid = false;
 
        KASSERT(npf_iscached(npc, NPC_IP46));
        KASSERT(npf_iscached(npc, NPC_ICMP));
@@ -265,10 +258,10 @@
         */
        if (npf_iscached(npc, NPC_IP4)) {
                const struct icmp *ic = npc->npc_l4.icmp;
-               ret = npfa_icmp4_inspect(ic->icmp_type, enpc, nbuf);
+               ret = npfa_icmp4_inspect(ic->icmp_type, enpc, nbuf, &hasqid);
        } else if (npf_iscached(npc, NPC_IP6)) {
                const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
-               ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc, nbuf);
+               ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc, nbuf, &hasqid);
        } else {
                ret = false;
        }
@@ -277,25 +270,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_session_t *
 npfa_icmp_session(npf_cache_t *npc, nbuf_t *nbuf, int di)
 {
+       npf_session_t *sess = 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, nbuf, &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(nbuf, NBUF_DATAREF_RESET));
                return NULL;
+       }
 
        /*
         * Invert the identifiers of the embedded packet.
@@ -323,16 +325,18 @@
                break;
        case IPPROTO_ICMP: {
                const struct icmp *ic = enpc.npc_l4.icmp;
-               ret = npfa_icmp4_inspect(ic->icmp_type, &enpc, nbuf);
-               if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
-                       return false;
+               ret = npfa_icmp4_inspect(ic->icmp_type, &enpc, nbuf, &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, nbuf);
-               if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
-                       return false;
+               ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc, nbuf, &hasqid);
+               if (!ret || !hasqid)
+                       goto out;
+               enpc.npc_info |= NPC_ICMP_ID;
                break;
        }
        default:
@@ -340,7 +344,15 @@
        }
 
        /* Lookup for a session using embedded packet. */
-       return npf_session_lookup(&enpc, nbuf, di, &forw);
+       sess = npf_session_lookup(&enpc, nbuf, di, &forw);
+
+out:
+       /*
+        * Recache npc. The nbuf may have been updated as a result of
+        * caching enpc.
+        */
+       npf_recache(npc, nbuf);
+       return sess;
 }
 
 /*
@@ -355,7 +367,16 @@
        if (di != PFIL_IN || !npf_iscached(npc, NPC_ICMP))
                return false;
        if (!npfa_icmp_inspect(npc, nbuf, &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(nbuf, NBUF_DATAREF_RESET));
                return false;
+       }
 
        KASSERT(npf_iscached(&enpc, NPC_IP46));
        KASSERT(npf_iscached(&enpc, NPC_LAYER4));
@@ -401,7 +422,7 @@
        case IPPROTO_ICMPV6:
                break;
        default:
-               return false;
+               goto err;
        }
 
        /*
@@ -410,7 +431,7 @@
         * This updates the checksums in the embedded packet.
         */
        if (npf_nat_translate(&enpc, nbuf, nt, false, PFIL_OUT)) {
-               return false;
+               goto err;
        }
 
        /*
@@ -434,6 +455,17 @@
                }
                break;
        }
+       npf_recache(npc, nbuf);
+       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, nbuf);
+       return false;
 }
diff -r e3220876b6fc -r b8b59497a5b0 sys/net/npf/npf_inet.c
--- a/sys/net/npf/npf_inet.c    Thu May 03 15:05:21 2018 +0000
+++ b/sys/net/npf/npf_inet.c    Thu May 17 13:47:24 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_inet.c,v 1.10.4.9.2.1 2013/09/13 04:17:41 msaitoh Exp $    */
+/*     $NetBSD: npf_inet.c,v 1.10.4.9.2.2 2018/05/17 13:47:24 martin Exp $     */
 
 /*-
  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.10.4.9.2.1 2013/09/13 04:17:41 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.10.4.9.2.2 2018/05/17 13:47:24 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -447,6 +447,11 @@
        }
        hlen = npc->npc_hlen;
 
+       /*
+        * Note: we guarantee that the potential "Query Id" field of the
+        * ICMPv4/ICMPv6 packets is in the nbuf. This field is used in the
+        * ICMP ALG.
+        */
        switch (npc->npc_proto) {
        case IPPROTO_TCP:
                /* Cache: layer 4 - TCP. */
@@ -463,13 +468,13 @@
        case IPPROTO_ICMP:
                /* Cache: layer 4 - ICMPv4. */
                npc->npc_l4.icmp = nbuf_advance(nbuf, hlen,
-                   offsetof(struct icmp, icmp_void));
+                   ICMP_MINLEN);
                l4flags = NPC_LAYER4 | NPC_ICMP;
                break;
        case IPPROTO_ICMPV6:
                /* Cache: layer 4 - ICMPv6. */
                npc->npc_l4.icmp6 = nbuf_advance(nbuf, hlen,
-                   offsetof(struct icmp6_hdr, icmp6_data32));
+                   sizeof(struct icmp6_hdr));
                l4flags = NPC_LAYER4 | NPC_ICMP;
                break;
        default:



Home | Main Index | Thread Index | Old Index