Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH net v3] net/rds: tcp: bound the memory held for an incoming message
@ 2026-12-02  5:23 Weidong Wang via B4 Relay
  2026-12-02  5:29 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Weidong Wang via B4 Relay @ 2026-12-02  5:23 UTC (permalink / raw)
  To: Allison Henderson, Andy Grover
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-rdma, rds-devel, linux-kernel, stable,
	Zijie Zhao, Weidong Wang

From: Weidong Wang <weidongw@illinois.edu>

rds_tcp_data_recv() queues pskb_extract() clones on the incoming
message until h_len bytes, taken from the peer's header, have arrived.
The clones are not charged to the socket, so a peer can make the
receiver hold up to 4 GiB by announcing a large h_len, or over 1 GiB
for a 1 MiB message sent one byte per segment, as each clone keeps the
truesize of the skb it was cut from.

Drop the connection when h_len exceeds RDS_MAX_MSG_SIZE, the limit
rds_sendmsg() enforces since v4.11, and discard the rest of the stream
until shutdown. Copy, rather than clone, pieces smaller than a quarter
of their skb's truesize, appending to a linear skb until it is full, so
that a message holds at most about four times its size.

Found by an AI code review agent and reproduced under QEMU. The fix and
changelog were drafted with AI assistance (Claude), then reviewed and
tested by the submitter.

Fixes: 70041088e3b9 ("RDS: Add TCP transport to RDS")
Cc: stable@vger.kernel.org
Cc: Zijie Zhao <zijie4@illinois.edu>
Signed-off-by: Weidong Wang <weidongw@illinois.edu>
---
Tested under QEMU with KASAN: a 1 MiB message sent one byte per segment
held 1239 MiB with v2; with v3 it is delivered intact at normal memory
use. The RDS selftests (-T tcp) pass. The reproducer can be shared on
request (AI-found issue).

Changes in v3:
- Also copy small pieces instead of cloning them (reported by Sashiko).
  A truesize limit that drops the connection would also drop valid
  messages, e.g. with a small MSS or 64 KiB pages. New subject.
- Link to v2: https://patch.msgid.link/20261125-h047-send-v2-1-b29031cf6b93@illinois.edu

Changes in v2:
- Drop the connection once, then discard the rest of the stream, so
  that the shutdown is not held up (reported by Sashiko). Allison's
  Reviewed-by is not carried over.
- Link to v1: https://patch.msgid.link/20261111-h047-send-v1-1-2727a38dbfcd@illinois.edu
---
 net/rds/tcp.h      |  1 +
 net/rds/tcp_recv.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/net/rds/tcp.h b/net/rds/tcp.h
index 39c86347188c..2bdff97c815b 100644
--- a/net/rds/tcp.h
+++ b/net/rds/tcp.h
@@ -22,6 +22,7 @@ struct rds_tcp_net {
 struct rds_tcp_incoming {
 	struct rds_incoming	ti_inc;
 	struct sk_buff_head	ti_skb_list;
+	bool			ti_tail_copy;	/* last skb is a copy, can be appended to */
 };
 
 struct rds_tcp_connection {
diff --git a/net/rds/tcp_recv.c b/net/rds/tcp_recv.c
index ffe843ca219c..372218e5ab76 100644
--- a/net/rds/tcp_recv.c
+++ b/net/rds/tcp_recv.c
@@ -147,6 +147,15 @@ static void rds_tcp_cong_recv(struct rds_connection *conn,
 	rds_cong_map_updated(map, ~(u64) 0);
 }
 
+/*
+ * A clone made by pskb_extract() keeps the truesize of the skb it is cut
+ * from, however few bytes it holds, and none of it is charged to a socket.
+ * Pieces smaller than a quarter of that are copied instead, and once a
+ * copy has started, data is appended to it until it is full. A message
+ * then holds at most about four times its size.
+ */
+#define RDS_TCP_MAX_CLONE_RATIO	4
+
 struct rds_tcp_desc_arg {
 	struct rds_conn_path *conn_path;
 	gfp_t gfp;
@@ -189,6 +198,7 @@ static int rds_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb,
 			 * we've already serialized at a higher level.
 			 */
 			skb_queue_head_init(&tinc->ti_skb_list);
+			tinc->ti_tail_copy = false;
 		}
 
 		if (left && tc->t_tinc_hdr_rem) {
@@ -210,20 +220,64 @@ static int rds_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb,
 					be32_to_cpu(tinc->ti_inc.i_hdr.h_len);
 				tinc->ti_inc.i_rx_lat_trace[RDS_MSG_RX_START] =
 					local_clock();
+				/*
+				 * The payload is queued on tinc until the
+				 * whole message has arrived, so a peer must
+				 * not announce more than a sender is allowed
+				 * to send.
+				 */
+				if (tc->t_tinc_data_rem > RDS_MAX_MSG_SIZE)
+					rds_conn_path_drop(cp, false);
 			}
 		}
 
+		/*
+		 * After an oversized header the connection is being dropped:
+		 * consume and discard the rest of the stream until it is shut
+		 * down, so that the receive queue drains and the shutdown is
+		 * not held up.
+		 */
+		if (!tc->t_tinc_hdr_rem &&
+		    tc->t_tinc_data_rem > RDS_MAX_MSG_SIZE) {
+			left = 0;
+			break;
+		}
+
 		if (left && tc->t_tinc_data_rem) {
 			to_copy = min(tc->t_tinc_data_rem, left);
 
-			clone = pskb_extract(skb, offset, to_copy, arg->gfp);
-			if (!clone) {
-				desc->error = -ENOMEM;
-				goto out;
+			/* unreadable (devmem) frags can only be cloned */
+			clone = skb_peek_tail(&tinc->ti_skb_list);
+			if (skb_frags_readable(skb) &&
+			    ((tinc->ti_tail_copy && skb_tailroom(clone)) ||
+			     skb->truesize > RDS_TCP_MAX_CLONE_RATIO * to_copy)) {
+				if (!tinc->ti_tail_copy || !skb_tailroom(clone)) {
+					clone = alloc_skb(min_t(size_t,
+								tc->t_tinc_data_rem,
+								SKB_MAX_ORDER(0, 0)),
+							  arg->gfp);
+					if (!clone) {
+						desc->error = -ENOMEM;
+						goto out;
+					}
+					skb_queue_tail(&tinc->ti_skb_list, clone);
+					tinc->ti_tail_copy = true;
+				}
+				to_copy = min_t(size_t, to_copy,
+						skb_tailroom(clone));
+				skb_copy_bits(skb, offset,
+					      skb_put(clone, to_copy), to_copy);
+			} else {
+				clone = pskb_extract(skb, offset, to_copy,
+						     arg->gfp);
+				if (!clone) {
+					desc->error = -ENOMEM;
+					goto out;
+				}
+				skb_queue_tail(&tinc->ti_skb_list, clone);
+				tinc->ti_tail_copy = false;
 			}
 
-			skb_queue_tail(&tinc->ti_skb_list, clone);
-
 			rdsdebug("skb %p data %p len %d off %u to_copy %zu -> "
 				 "clone %p data %p len %d\n",
 				 skb, skb->data, skb->len, offset, to_copy,

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261110-h047-send-52025d442823

Best regards,
--  
Weidong Wang <weidongw@illinois.edu>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  5:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  5:23 [PATCH net v3] net/rds: tcp: bound the memory held for an incoming message Weidong Wang via B4 Relay
2026-12-02  5:29 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox