This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 011dac5bd88 net/ipfrag: Fix IPv4/IPv6 incomplete/incontiguous 
fragments reassembly.
011dac5bd88 is described below

commit 011dac5bd88ccd459de3de0aebf0b7eb49ef2538
Author: Tomasz 'CeDeROM' CEDRO <[email protected]>
AuthorDate: Sun Nov 22 01:10:28 2026 +0100

    net/ipfrag: Fix IPv4/IPv6 incomplete/incontiguous fragments reassembly.
    
    ip_fragin_check() marked a datagram as complete as soon as a tail
    fragment's offset equalled the summed length of the preceding
    more-fragments entries but did not require the zero fragment what
    might lead to invalid memory access.
    
    The sum-based test also accepted overlapping fragments (producing a
    reassembled packet with a hole in it) and fragments located beyond the
    tail fragment (which were concatenated past the end of the datagram).
    
    Only declare the datagram complete when both the zero and the tail
    fragment have been seen and the offset-ordered list starts at 0, has no
    holes or overlaps and ends with the tail fragment. Added DEBUGASSERT
    in both reassembly functions documenting this precondition.
    
    Functional test before/old and after/new results comparison:
      normal 3 frags in order                      old=COMPLETE new=COMPLETE ok
      normal 3 frags reversed                      old=COMPLETE new=COMPLETE ok
      duplicate of middle frag                     old=COMPLETE new=COMPLETE ok
      REPORT: no zero frag (8/16 + tail16)         old=COMPLETE new=wait     ok
      no zero frag, overlapping MF + tail          old=COMPLETE new=wait     ok
      hole: 0/16, tail at 24 + 40 MF               old=wait     new=wait     ok
      overlap: 0/16, 8/8, tail 24                  old=COMPLETE new=wait     ok
      frag beyond tail (arrives first)             old=COMPLETE new=wait     ok
      missing middle, tail first                   old=wait     new=wait     ok
    
    Assisted-by: Claude:claude-opus-5.5
    Signed-off-by: Tomasz 'CeDeROM' CEDRO <[email protected]>
---
 net/ipfrag/ipfrag.c    | 55 +++++++++++++++++++++++++++++++++-----------------
 net/ipfrag/ipv4_frag.c |  8 ++++++++
 net/ipfrag/ipv6_frag.c | 13 +++++++++---
 3 files changed, 55 insertions(+), 21 deletions(-)

diff --git a/net/ipfrag/ipfrag.c b/net/ipfrag/ipfrag.c
index e4e47bc3bdc..4259d91cf6e 100644
--- a/net/ipfrag/ipfrag.c
+++ b/net/ipfrag/ipfrag.c
@@ -366,8 +366,15 @@ ip_fragin_freelink(FAR struct ip_fraglink_s *fraglink)
  * Name: ip_fragin_check
  *
  * Description:
- *   Check whether the zero fragment has been received or all fragments have
- *   been received.
+ *   Check whether all fragments of an IP datagram have been received.
+ *
+ *   The datagram is only considered complete when the fragment list,
+ *   which is ordered by fragment offset, starts with the zero fragment,
+ *   every fragment begins exactly where the previous one ended (no holes
+ *   and no overlaps) and the list ends with the tail fragment (the only
+ *   one with the more-fragments flag cleared).  The reassembly functions
+ *   rely on all of these properties; in particular they use the zero
+ *   fragment as the head of the reassembled IOB chain.
  *
  * Input Parameters:
  *   fragsnode - node of the upper-level linked list, it maintains
@@ -380,30 +387,40 @@ ip_fragin_freelink(FAR struct ip_fraglink_s *fraglink)
 
 static void ip_fragin_check(FAR struct ip_fragsnode_s *fragsnode)
 {
-  uint16_t formerlen = 0;
+  uint32_t expected = 0;
   FAR struct ip_fraglink_s *entry;
 
-  if (fragsnode->verifyflag & IP_FRAGVERIFY_RECVDTAILFRAG)
+  if ((fragsnode->verifyflag &
+       (IP_FRAGVERIFY_RECVDZEROFRAG | IP_FRAGVERIFY_RECVDTAILFRAG)) !=
+      (IP_FRAGVERIFY_RECVDZEROFRAG | IP_FRAGVERIFY_RECVDTAILFRAG))
     {
-      entry = fragsnode->frags;
-      while (entry != NULL)
+      return;
+    }
+
+  for (entry = fragsnode->frags; entry != NULL; entry = entry->flink)
+    {
+      if (entry->fragoff != expected)
         {
-          if (entry->morefrags)
-            {
-              formerlen += entry->fraglen;
-            }
-          else
-            {
-              /* Only the last entry has a 0 morefrags flag */
+          /* A hole or an overlap, not complete (yet) */
 
-              if (entry->fragoff == formerlen)
-                {
-                  fragsnode->verifyflag |= IP_FRAGVERIFY_RECVDALLFRAGS;
-                }
+          return;
+        }
+
+      if (!entry->morefrags)
+        {
+          /* The tail fragment must be the last one in the list, nothing
+           * may follow the end of the datagram.
+           */
+
+          if (entry->flink == NULL)
+            {
+              fragsnode->verifyflag |= IP_FRAGVERIFY_RECVDALLFRAGS;
             }
 
-          entry = entry->flink;
+          return;
         }
+
+      expected += entry->fraglen;
     }
 }
 
@@ -1124,6 +1141,7 @@ void ip_frag_stop(FAR struct net_driver_s *dev)
   while (entry != NULL)
     {
       FAR struct ip_fragsnode_s *node = (FAR struct ip_fragsnode_s *)entry;
+
       entrynext = sq_next(entry);
 
       if (dev == node->dev)
@@ -1179,6 +1197,7 @@ void ip_frag_remallfrags(void)
   while (entry != NULL)
     {
       FAR struct ip_fragsnode_s *node = (FAR struct ip_fragsnode_s *)entry;
+
       entrynext = sq_next(entry);
 
       if (node->frags != NULL)
diff --git a/net/ipfrag/ipv4_frag.c b/net/ipfrag/ipv4_frag.c
index 314ec3f15f6..f843f260983 100644
--- a/net/ipfrag/ipv4_frag.c
+++ b/net/ipfrag/ipv4_frag.c
@@ -29,6 +29,7 @@
 #if defined(CONFIG_NET_IPv4) && defined (CONFIG_NET_IPFRAG)
 
 #include <sys/ioctl.h>
+#include <assert.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <nuttx/debug.h>
@@ -129,6 +130,12 @@ static uint32_t ipv4_fragin_reassemble(FAR struct 
ip_fragsnode_s *node)
    * the fraglink list was ordered by fragment offset value
    */
 
+  /* ip_fragin_check() guarantees that the list starts with the zero
+   * fragment, which becomes the head of the reassembled IOB chain.
+   */
+
+  DEBUGASSERT(node->frags != NULL && node->frags->fragoff == 0);
+
   fraglink    = node->frags;
   node->frags = NULL;
 
@@ -222,6 +229,7 @@ ipv4_fragout_buildipv4header(FAR struct ipv4_hdr_s *ref,
   if (ref != ipv4)
     {
       uint32_t iphdrlen = (ref->vhl & IPv4_HLMASK) << 2;
+
       memcpy(ipv4, ref, iphdrlen);
     }
 
diff --git a/net/ipfrag/ipv6_frag.c b/net/ipfrag/ipv6_frag.c
index 5ae861cd9a1..af0ae294c46 100644
--- a/net/ipfrag/ipv6_frag.c
+++ b/net/ipfrag/ipv6_frag.c
@@ -28,6 +28,7 @@
 #if defined(CONFIG_NET_IPv6) && defined (CONFIG_NET_IPFRAG)
 
 #include <sys/ioctl.h>
+#include <assert.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <nuttx/debug.h>
@@ -181,6 +182,12 @@ static uint32_t ipv6_fragin_reassemble(FAR struct 
ip_fragsnode_s *node)
    * the fraglink list was ordered by fragment offset value
    */
 
+  /* ip_fragin_check() guarantees that the list starts with the zero
+   * fragment, which becomes the head of the reassembled IOB chain.
+   */
+
+  DEBUGASSERT(node->frags != NULL && node->frags->fragoff == 0);
+
   fraglink = node->frags;
   node->frags = NULL;
 
@@ -430,9 +437,9 @@ static uint16_t ipv6_fragout_getunfraginfo(FAR struct iob_s 
*iob,
 
           case NEXT_ROUTING_EH:
           case NEXT_HOPBYBOT_EH:
-              unfraglen = delta + extlen;
-              *hdroff = delta;
-              *hdrtype = exthdr->nxthdr;
+            unfraglen = delta + extlen;
+            *hdroff = delta;
+            *hdrtype = exthdr->nxthdr;
         }
 
       payload += extlen;

Reply via email to