zhhyu7 commented on code in PR #22358:
URL: https://github.com/apache/nuttx/pull/22358#discussion_r4704890771


##########
net/tcp/tcp_conn.c:
##########
@@ -564,6 +564,107 @@ static bool tcp_reclaimable(FAR struct tcp_conn_s *conn)
           conn->tcpstateflags == TCP_LAST_ACK);
 }
 
+/****************************************************************************
+ * Name: tcp_is_syn_backlog
+ *
+ * Description:
+ *   Return true if the connection is in TCP_SYN_RCVD on the address and
+ *   port of the listener.
+ *
+ ****************************************************************************/
+
+static bool tcp_is_syn_backlog(FAR struct tcp_conn_s *listener,
+                               FAR struct tcp_conn_s *conn)
+{
+  if (conn->tcpstateflags != TCP_SYN_RCVD)
+    {
+      return false;
+    }
+
+#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
+  return tcp_conn_cmp(listener->domain,
+                      (FAR const union ip_addr_u *)&listener->u,
+                      listener->lport, conn);
+#else
+  return tcp_conn_cmp((FAR const union ip_addr_u *)&listener->u,
+                      listener->lport, conn);
+#endif
+}
+
+/****************************************************************************
+ * Name: tcp_syn_room
+ *
+ * Description:
+ *   Return true if the listener can take one more connection in
+ *   TCP_SYN_RCVD.  Its connections in TCP_SYN_RCVD and the ones that wait
+ *   for accept() together take at most the backlog of listen().  If there
+ *   is no room, free the oldest connection in TCP_SYN_RCVD of the listener
+ *   on this device and take its place (RFC 4987, section 3.4).
+ *
+ * Input Parameters:
+ *   dev      - The device whose lock the caller holds.
+ *   listener - The listening connection.
+ *
+ * Returned Value:
+ *   true if a new connection in TCP_SYN_RCVD can be made.
+ *
+ ****************************************************************************/
+
+static bool tcp_syn_room(FAR struct net_driver_s *dev,
+                         FAR struct tcp_conn_s *listener)
+{
+  FAR struct tcp_conn_s *oldest = NULL;
+  FAR struct tcp_conn_s *conn;
+  size_t nsynrcvd = 0;
+  size_t nfree = SIZE_MAX;
+  bool room = true;
+
+  tcp_conn_list_lock();
+#ifdef CONFIG_NET_TCPBACKLOG
+  nfree = 0;
+  if (listener->backlog != NULL)
+    {
+      nfree = sq_count(&listener->backlog->bl_free);
+    }
+#endif
+
+  /* The list is in the order of creation: the first match is the oldest */
+
+  for (conn = (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
+       conn != NULL;
+       conn = (FAR struct tcp_conn_s *)conn->sconn.s_node.flink)
+    {
+      if (tcp_is_syn_backlog(listener, conn))
+        {
+          if (oldest == NULL && conn->dev == dev)

Review Comment:
   nsynrcvd counts the SYN_RCVD connections of this listener on all devices 
(tcp_conn_cmp() matches loosely when the listener's laddr is unspecified), but 
oldest is only searched among those on dev. The     
   accounting is global while the eviction is local.                            
                                                                                
                                               
                                                                                
                                                                                
                                               
   With a listener on 0.0.0.0:80 and backlog = 8: once device A holds 8 
connections in SYN_RCVD, a legitimate SYN arriving on device B gets nsynrcvd == 
8, nfree == 8, so room = false; nsynrcvd <= nfree holds
   but oldest == NULL, and the SYN is refused. Device B cannot accept anything 
until A's SYN_RCVD entries time out.
                                                                                
                                                                                
                                               
   A SYN flood on one interface therefore blocks legitimate connections on 
every other interface, which is the opposite of what the patch is for. The lock 
order (tcp_free() needs the device lock of the 
   victim, and the list lock is taken under the device lock) is what forces the 
local eviction, and the comment at line 707 acknowledges it, but the 
consequence is not handled. Either count only 
   conn->dev == dev so the budget matches what can be evicted, or let the 
caller retry with the victim's device lock.



##########
net/tcp/tcp_conn.c:
##########
@@ -564,6 +564,107 @@ static bool tcp_reclaimable(FAR struct tcp_conn_s *conn)
           conn->tcpstateflags == TCP_LAST_ACK);
 }
 
+/****************************************************************************
+ * Name: tcp_is_syn_backlog
+ *
+ * Description:
+ *   Return true if the connection is in TCP_SYN_RCVD on the address and
+ *   port of the listener.
+ *
+ ****************************************************************************/
+
+static bool tcp_is_syn_backlog(FAR struct tcp_conn_s *listener,
+                               FAR struct tcp_conn_s *conn)
+{
+  if (conn->tcpstateflags != TCP_SYN_RCVD)
+    {
+      return false;
+    }
+
+#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
+  return tcp_conn_cmp(listener->domain,
+                      (FAR const union ip_addr_u *)&listener->u,
+                      listener->lport, conn);
+#else
+  return tcp_conn_cmp((FAR const union ip_addr_u *)&listener->u,
+                      listener->lport, conn);
+#endif
+}
+
+/****************************************************************************
+ * Name: tcp_syn_room
+ *
+ * Description:
+ *   Return true if the listener can take one more connection in
+ *   TCP_SYN_RCVD.  Its connections in TCP_SYN_RCVD and the ones that wait
+ *   for accept() together take at most the backlog of listen().  If there
+ *   is no room, free the oldest connection in TCP_SYN_RCVD of the listener
+ *   on this device and take its place (RFC 4987, section 3.4).
+ *
+ * Input Parameters:
+ *   dev      - The device whose lock the caller holds.
+ *   listener - The listening connection.
+ *
+ * Returned Value:
+ *   true if a new connection in TCP_SYN_RCVD can be made.
+ *
+ ****************************************************************************/
+
+static bool tcp_syn_room(FAR struct net_driver_s *dev,
+                         FAR struct tcp_conn_s *listener)
+{
+  FAR struct tcp_conn_s *oldest = NULL;
+  FAR struct tcp_conn_s *conn;
+  size_t nsynrcvd = 0;
+  size_t nfree = SIZE_MAX;
+  bool room = true;
+
+  tcp_conn_list_lock();
+#ifdef CONFIG_NET_TCPBACKLOG
+  nfree = 0;
+  if (listener->backlog != NULL)
+    {
+      nfree = sq_count(&listener->backlog->bl_free);

Review Comment:
   bl_free and bl_pending are protected by the listener's s_lock, not by the 
connection list lock: tcp_backlogremove() returns a container under 
conn_lock(&conn->sconn) in tcp_accept.c:229 without the list 
   lock. Reading bl_free with only the list lock held races with accept(); a 
wrong count is the mild outcome, walking a node that is being relinked is the 
real one.
   
   listener->backlog itself is also unsafe here. tcp_backlogcreate() installs 
the new backlog under the listener's s_lock, then calls tcp_backlogfree(old) 
after unlocking and without the list lock, so the 
   list lock does not keep the block alive. A concurrent listen() can free it 
while tcp_syn_room() dereferences it.
   
   Taking conn_lock(&listener->sconn) inside the list lock matches the order 
already used by tcp_accept_connection() (list lock, then the listener's s_lock) 
and would close both.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to