Netdev List
 help / color / mirror / Atom feed
* [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible
@ 2026-11-26 15:08 Piyus Kedia
  2026-11-27 15:23 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-11-26 15:08 UTC (permalink / raw)
  To: wenjia, jaka, alibuda, tonylu, guwen
  Cc: netdev, linux-s390, linux-kernel, davem, edumazet, kuba, pabeni,
	horms, kapil, stable

smc_switch_cursor() repairs the send cursors when a connection moves to an
alternate link, and the repair trusts conn->local_rx_ctrl.cons, which the
peer wrote:

	diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
	smc_curs_add(conn->sndbuf_desc->len, &conn->tx_curs_sent, diff);
	atomic_add(diff, &conn->sndbuf_space);
	smc_curs_add(conn->peer_rmbe_size, &conn->local_tx_ctrl.prod, diff);

A cursor reporting more consumed than this side ever sent credits
sndbuf_space past sndbuf_desc->len, breaking the
0 <= sndbuf_space <= sndbuf_desc->len the tx path depends on, and pushes
tx_curs_sent out of the ring, which smc_tx_rdma_writes() uses as a source
offset.

Capping diff is not enough: the cap would leave local_tx_ctrl.prod behind
local_rx_ctrl.cons, which is the state this block exists to repair, so
smc_write_space() could return a negative peer_rmbe_space and the
connection would stop sending for good.  A peer that reports consuming
what was never sent has broken the protocol, so drop the connection, as
smc_cdc_msg_validate() does for a sequence number that cannot be honest.

Three things make the cursor impossible, and smc_cursor_cannot_be_honest()
checks all three:

 - cons.count at or past peer_rmbe_size.  smc_cdc_cursor_to_host() does
   not bound it.
 - a difference of peer_rmbe_size or more.  smc_curs_add() subtracts the
   size at most once, so a larger difference leaves
   local_tx_ctrl.prod.count outside the peer's RMBE.  Below peer_rmbe_size
   the repair is exact: prod starts at fin and diff is their distance, so
   prod lands on cons rather than behind it.
 - a difference past the bytes that were SENT and not yet confirmed, taken
   as the distance between two snapshots of tx_curs_fin and tx_curs_sent.

The check runs before anything is rewound, and the connection is dropped
with the cursors as they were found.  Doing it after the rewind is what
would make it unsafe: tx_curs_sent and local_tx_ctrl.prod are set back to
fin at the top of the function, and returning from there leaves prod behind
local_rx_ctrl.cons with peer_rmbe_space still holding the credit the forged
message produced.  The tx path tests conn->killed and
local_rx_ctrl.conn_state_flags.peer_conn_abort, not out_of_sync or the
local_tx_ctrl flag set here, so a send under lock_sock() can reach
smc_tx_rdma_writes() with prod < cons before abort_work gets the socket
lock - and with the wraps differing it takes the dst_len = len branch
without clamping to peer_rmbe_size - prod.count, writing past the end of
the peer RMBE.

Fixes: c6f02ebeea3a ("net/smc: switch connections to alternate link")
Cc: stable@vger.kernel.org
Co-developed-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
v4: addresses the review of v3
    (https://lore.kernel.org/r/sashiko-outbox-233843@kernel.org)

    All three findings are right.

    - the check moved ahead of the rewind.  v3 ran it inside the fix-up
      block and returned 0 from there, after tx_curs_sent and
      local_tx_ctrl.prod had been set back to fin, which is the window the
      review describes: the tx path does not look at out_of_sync or at the
      local_tx_ctrl abort flag, so a concurrent send could reach
      smc_tx_rdma_writes() with prod < cons before abort_work took the
      socket lock.  Nothing is modified now before the connection is
      dropped.
    - the bound is the SENT bytes, not sndbuf_desc->len - sndbuf_space.
      That counter is the PREPARED bytes, so v3 would have advanced
      tx_curs_sent and tx_curs_fin over data still in sndbuf, and it is
      also where the false positive came from: smc_cdc_tx_handler()
      credits sndbuf_space before updating local_tx_ctrl_fin and this
      holds only send_lock, so an honest completion between the two could
      make diff exceed it.  Both differences now come from cursor
      snapshots, so they are consistent with each other.
    - cons.count and diff are bounded by peer_rmbe_size, which v3 left to
      smc_curs_add()'s single subtraction.

    How this was found: an AI-driven fuzzing harness built on syzkaller,
    developed jointly by Kapil Vaswani and myself, driving a forged CDC
    stream at an SMC-R link under KASAN on x86_64.  The analysis and this
    changelog were written with the assistance of a large language model
    and checked by hand against the tree; the reproducers in the earlier
    postings were produced by us.  The v4 changes come from addressing an
    AI-generated review of v3 on the list, and the tx-path guards, the
    dst_len branch and the two cursor helpers it named were each read in
    the tree first.

Link: https://lore.kernel.org/all/20261122053223.2497832-2-piyus@iiitd.ac.in/

 net/smc/smc_core.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 66 insertions(+), 2 deletions(-)

diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
index 9974149659c2..31300ecd6b12 100644
--- a/net/smc/smc_core.c
+++ b/net/smc/smc_core.c
@@ -1030,6 +1030,48 @@
 	return space;
 }
 
+/* Whether the peer's consumer cursor can honestly have reached where it says.
+ *
+ * smc_switch_cursor() repairs the send cursors from conn->local_rx_ctrl.cons,
+ * which the peer wrote.  A cursor reporting more consumed than this side sent
+ * credits sndbuf_space past sndbuf_desc->len and pushes tx_curs_sent out of
+ * the ring, which smc_tx_rdma_writes() uses as a source offset.  Three things
+ * make it impossible:
+ *
+ *  - cons.count at or past peer_rmbe_size, which smc_cdc_cursor_to_host()
+ *    does not bound;
+ *  - a difference of peer_rmbe_size or more, which would survive the single
+ *    subtraction smc_curs_add() does and leave local_tx_ctrl.prod.count
+ *    outside the peer's RMBE.  Below that the repair is exact: prod starts at
+ *    fin and diff is their distance, so prod lands on cons, not behind it;
+ *  - a difference past what was SENT and not yet confirmed.  That is the
+ *    distance between two cursor snapshots, not
+ *    sndbuf_desc->len - sndbuf_space: the counter covers PREPARED bytes, so
+ *    bounding by it would advance the cursors over data still in sndbuf, and
+ *    comparing a cursor difference against it is a race in its own right -
+ *    smc_cdc_tx_handler() credits sndbuf_space before it updates
+ *    local_tx_ctrl_fin, and the caller holds only send_lock, so an honest
+ *    completion in between could make diff exceed it.
+ */
+static bool smc_cursor_cannot_be_honest(struct smc_connection *conn,
+					union smc_host_cursor *fin,
+					union smc_host_cursor *cons)
+{
+	union smc_host_cursor tx_fin, tx_sent;
+	int diff, unconfirmed;
+
+	if (smc_curs_comp(conn->peer_rmbe_size, cons, fin) >= 0)
+		return false;
+
+	smc_curs_copy(&tx_fin, &conn->tx_curs_fin, conn);
+	smc_curs_copy(&tx_sent, &conn->tx_curs_sent, conn);
+	diff = smc_curs_diff(conn->peer_rmbe_size, fin, cons);
+	unconfirmed = smc_curs_diff(conn->sndbuf_desc->len, &tx_fin, &tx_sent);
+
+	return cons->count >= (u32)conn->peer_rmbe_size ||
+	       diff >= conn->peer_rmbe_size || diff > unconfirmed;
+}
+
 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
 			     struct smc_wr_buf *wr_buf)
 {
@@ -1038,11 +1080,33 @@
 	int rc = 0;
 	int diff;
 
-	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
+	/* Before anything is rewound.  Returning from inside the repair below
+	 * would leave local_tx_ctrl.prod behind local_rx_ctrl.cons with
+	 * peer_rmbe_space still holding the credit the forged message made,
+	 * and the tx path tests conn->killed and
+	 * local_rx_ctrl.conn_state_flags.peer_conn_abort rather than
+	 * out_of_sync or the flag set here - so a send under lock_sock() could
+	 * reach smc_tx_rdma_writes() with prod < cons before abort_work takes
+	 * the socket lock, and with the wraps differing that takes the
+	 * dst_len = len branch without clamping to peer_rmbe_size -
+	 * prod.count and writes past the end of the peer RMBE.
+	 */
 	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
+	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
+	if (smc_cursor_cannot_be_honest(conn, &fin, &cons)) {
+		conn->out_of_sync = 1;	/* prevent any further receives */
+		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
+		smc_wr_tx_put_slot(conn->lnk,
+				   (struct smc_wr_tx_pend_priv *)pend);
+		sock_hold(&smc->sk); /* sock_put in abort_work */
+		if (!queue_work(smc_close_wq, &conn->abort_work))
+			sock_put(&smc->sk);
+		return 0;
+	}
+
+	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
 	/* set prod cursor to old state, enforce tx_rdma_writes() */
 	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
-	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
 
 	if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
 		/* cons cursor advanced more than fin, and prod was set
-- 
2.43.0


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

end of thread, other threads:[~2026-11-27 15:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 15:08 [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible Piyus Kedia
2026-11-27 15:23 ` netdev-bot+sashiko

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