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 7876a93a947 net: Clear pool-allocated structures that used to arrive 
zeroed.
7876a93a947 is described below

commit 7876a93a947f82723760af54524df55425954a89
Author: zhanghongyu <[email protected]>
AuthorDate: Mon Nov 23 12:26:36 2026 +0800

    net: Clear pool-allocated structures that used to arrive zeroed.
    
    NET_BUFPOOL_TRYALLOC() returned zeroed memory, and the network code
    relied on that: the allocation helpers set only the few fields they
    care about and leave the rest at zero.  Commit ae571a4ff8c replaced
    the pools with MEMPOOL_DEFINE, and mempool_allocate() does not clear
    the block, so a recycled structure now starts with the contents of its
    previous user.
    
    devif_callback_alloc() is the clearest case.  It only links the new
    callback into the lists it was given, so with a non-NULL list_head the
    stale nxtconn and prevconn survive, and with a NULL list_head so do
    event, priv, flags and free_flags.  The result is a corrupted callback
    list: on a bridged simulator the node dies during netinit, either on
    the double-free DEBUGASSERT in devif_callback_free() or, once that
    check is passed, on a segmentation fault in the same function while
    walking the list from icmpv6_neighbor().
    
    Clear the block after mempool_allocate() everywhere the zeroed
    behaviour was assumed: the devif callbacks, the forwarding structures,
    the TCP and UDP write buffers, the UDP, ICMP, ICMPv6, netlink and
    packet connections, and the IPv4 and IPv6 route entries.
    
    Once mempool_zallocate is available, it needs to be replaced with
    mempool_zallocate.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/devif/devif_callback.c     |  2 ++
 net/icmp/icmp_conn.c           |  2 ++
 net/icmpv6/icmpv6_conn.c       |  2 ++
 net/netforward/netfwd_alloc.c  |  9 ++++++++-
 net/netlink/netlink_conn.c     |  2 ++
 net/pkt/pkt_conn.c             |  4 ++++
 net/route/net_alloc_ramroute.c | 12 ++++++++++++
 net/tcp/tcp_wrbuffer.c         |  2 ++
 net/udp/udp_conn.c             |  4 ++++
 net/udp/udp_wrbuffer.c         | 10 ++++++++++
 10 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/net/devif/devif_callback.c b/net/devif/devif_callback.c
index 78b130717b4..84eb24b981f 100644
--- a/net/devif/devif_callback.c
+++ b/net/devif/devif_callback.c
@@ -308,6 +308,8 @@ devif_callback_alloc(FAR struct net_driver_s *dev,
   ret = mempool_allocate(&g_cbprealloc, 0);
   if (ret)
     {
+      memset(ret, 0, sizeof(*ret));
+
       /* Add the newly allocated instance to the head of the device event
        * list.
        */
diff --git a/net/icmp/icmp_conn.c b/net/icmp/icmp_conn.c
index 849e73264c6..e7f9ace4a11 100644
--- a/net/icmp/icmp_conn.c
+++ b/net/icmp/icmp_conn.c
@@ -100,6 +100,8 @@ FAR struct icmp_conn_s *icmp_alloc(void)
   conn = mempool_allocate(&g_icmp_connections, 0);
   if (conn != NULL)
     {
+      memset(conn, 0, sizeof(*conn));
+
       /* Use conn_init to initialize the connection structure */
 
       conn_init(&conn->sconn);
diff --git a/net/icmpv6/icmpv6_conn.c b/net/icmpv6/icmpv6_conn.c
index 1ec1ee5d484..c168905da93 100644
--- a/net/icmpv6/icmpv6_conn.c
+++ b/net/icmpv6/icmpv6_conn.c
@@ -101,6 +101,8 @@ FAR struct icmpv6_conn_s *icmpv6_alloc(void)
   conn = mempool_allocate(&g_icmpv6_connections, 0);
   if (conn != NULL)
     {
+      memset(conn, 0, sizeof(*conn));
+
       /* Use conn_init to initialize the connection structure */
 
       conn_init(&conn->sconn);
diff --git a/net/netforward/netfwd_alloc.c b/net/netforward/netfwd_alloc.c
index 8bb29a87f80..5fbbf12b245 100644
--- a/net/netforward/netfwd_alloc.c
+++ b/net/netforward/netfwd_alloc.c
@@ -97,7 +97,14 @@ MEMPOOL_DEFINE(g_fwdpool, sizeof(struct forward_s),
 
 FAR struct forward_s *netfwd_alloc(void)
 {
-  return mempool_allocate(&g_fwdpool, 0);
+  FAR struct forward_s *fwd = mempool_allocate(&g_fwdpool, 0);
+
+  if (fwd != NULL)
+    {
+      memset(fwd, 0, sizeof(*fwd));
+    }
+
+  return fwd;
 }
 
 /****************************************************************************
diff --git a/net/netlink/netlink_conn.c b/net/netlink/netlink_conn.c
index 0d61a40bbea..addcce09179 100644
--- a/net/netlink/netlink_conn.c
+++ b/net/netlink/netlink_conn.c
@@ -169,6 +169,8 @@ FAR struct netlink_conn_s *netlink_alloc(void)
   conn = mempool_allocate(&g_netlink_connections, 0);
   if (conn != NULL)
     {
+      memset(conn, 0, sizeof(*conn));
+
       /* Use conn_init to initialize the connection structure */
 
       conn_init(&conn->sconn);
diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c
index 46b83af3707..0e18976704b 100644
--- a/net/pkt/pkt_conn.c
+++ b/net/pkt/pkt_conn.c
@@ -25,6 +25,8 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+
+#include <string.h>
 #if defined(CONFIG_NET) && defined(CONFIG_NET_PKT)
 
 #include <assert.h>
@@ -106,6 +108,8 @@ FAR struct pkt_conn_s *pkt_alloc(void)
   conn = mempool_allocate(&g_pkt_connections, 0);
   if (conn)
     {
+      memset(conn, 0, sizeof(*conn));
+
       /* Use conn_init to initialize the connection structure */
 
       conn_init(&conn->sconn);
diff --git a/net/route/net_alloc_ramroute.c b/net/route/net_alloc_ramroute.c
index c0ef6f1d8db..c4634f34d10 100644
--- a/net/route/net_alloc_ramroute.c
+++ b/net/route/net_alloc_ramroute.c
@@ -26,6 +26,8 @@
 
 #include <nuttx/config.h>
 
+#include <string.h>
+
 #include <stdint.h>
 #include <errno.h>
 #include <assert.h>
@@ -110,6 +112,11 @@ FAR struct net_route_ipv4_s *net_allocroute_ipv4(void)
    */
 
   route = mempool_allocate(&g_ipv4routes, 0);
+  if (route != NULL)
+    {
+      memset(route, 0, sizeof(*route));
+    }
+
   if (!route)
     {
       return NULL;
@@ -129,6 +136,11 @@ FAR struct net_route_ipv6_s *net_allocroute_ipv6(void)
    */
 
   route = mempool_allocate(&g_ipv6routes, 0);
+  if (route != NULL)
+    {
+      memset(route, 0, sizeof(*route));
+    }
+
   if (!route)
     {
       return NULL;
diff --git a/net/tcp/tcp_wrbuffer.c b/net/tcp/tcp_wrbuffer.c
index 9fa2483f52a..1254760ed86 100644
--- a/net/tcp/tcp_wrbuffer.c
+++ b/net/tcp/tcp_wrbuffer.c
@@ -102,6 +102,8 @@ tcp_wrbuffer_timedalloc(FAR struct socket_conn_s *sconn,
       return NULL;
     }
 
+  memset(wrb, 0, sizeof(*wrb));
+
   /* Now get the first I/O buffer for the write buffer structure */
 
   wrb->wb_iob = conn_dev_iobtimedalloc(true, timeout, sconn, dev);
diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c
index 051e53fe0f6..2f9dd871500 100644
--- a/net/udp/udp_conn.c
+++ b/net/udp/udp_conn.c
@@ -692,6 +692,10 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain)
   udp_conn_list_lock();
 
   conn = mempool_allocate(&g_udp_connections, 0);
+  if (conn != NULL)
+    {
+      memset(conn, 0, sizeof(*conn));
+    }
 
   if (conn)
     {
diff --git a/net/udp/udp_wrbuffer.c b/net/udp/udp_wrbuffer.c
index c72c434a492..3cd93355674 100644
--- a/net/udp/udp_wrbuffer.c
+++ b/net/udp/udp_wrbuffer.c
@@ -92,6 +92,11 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_timedalloc(unsigned 
int timeout)
    */
 
   wrb = mempool_allocate(&g_wrbuffer, timeout);
+  if (wrb != NULL)
+    {
+      memset(wrb, 0, sizeof(*wrb));
+    }
+
   if (wrb == NULL)
     {
       return NULL;
@@ -150,6 +155,11 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_tryalloc(void)
    */
 
   wrb = mempool_allocate(&g_wrbuffer, 0);
+  if (wrb != NULL)
+    {
+      memset(wrb, 0, sizeof(*wrb));
+    }
+
   if (wrb == NULL)
     {
       return NULL;

Reply via email to