Linux s390 Architecture development
 help / color / mirror / Atom feed
* [PATCH net v3] net/smc: drop the connection when the peer's failover cursor is impossible
@ 2026-11-24 17:30 Piyus Kedia
  2026-11-25 17:31 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-24 17:30 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() credits conn->sndbuf_space from the consumer cursor
the peer reported, with nothing bounding the amount:

	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);

	if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
		diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
		smc_curs_add(conn->sndbuf_desc->len,
			     &conn->tx_curs_sent, diff);
		smc_curs_add(conn->sndbuf_desc->len,
			     &conn->tx_curs_fin, diff);

		smp_mb__before_atomic();
		atomic_add(diff, &conn->sndbuf_space);

local_rx_ctrl.cons is whatever arrived in the peer's last CDC message.
smc_cdc_cursor_to_host() drops only a cursor that moves backward within
one wrap, so a peer can advance cons.count by nearly peer_rmbe_size and
report consuming data this side never sent.  diff becomes that amount and
sndbuf_space ends above sndbuf_desc->len, breaking the
"0 <= sndbuf_space <= sndbuf_desc->len" that smc_cdc_tx_handler() states
and the tx path reads as free room in the send ring:

	writespace = atomic_read(&conn->sndbuf_space);
	/* not more than what user space asked for */
	copylen = min_t(size_t, send_remaining, writespace);

The first chunk of a two-part write is clamped against the buffer, the
second is not, so memcpy_from_msg() copies past the end of the send
buffer.  The two cursors advanced beside the credit can also leave the
ring: smc_curs_add() subtracts the size only once, so a diff of two
buffers or more leaves tx_curs_sent.count at or above sndbuf_desc->len,
and smc_tx_rdma_writes() uses that count as a source offset.

A peer reaches this path on an SMC-R link group with a second link by
asking for the active one to be deleted:

  smc_llc_rx_delete_link()
    smc_llc_process_cli_delete_link()
      smc_switch_conns()
        smc_switch_cursor()

An honest peer cannot have consumed more than this side has prepared and
not yet had confirmed, which is sndbuf_desc->len - sndbuf_space.  A peer
that reports more than that has broken the protocol, so drop the
connection the way smc_cdc_msg_validate() does for a sequence number that
cannot be honest - out_of_sync, peer_conn_abort, abort_work - and leave
the cursors alone.

Capping diff at that value instead was tried and is worse than it looks:
the cap keeps sndbuf_space and the cursors in range, but it leaves
local_tx_ctrl.prod behind local_rx_ctrl.cons, which is the state this
block exists to repair.  smc_write_space() then computes
peer_rmbe_size - smc_curs_diff(size, &cons, &prod) over an inverted pair;
with cons.wrap == prod.wrap + 1 that is negative, smc_tx_rdma_writes()
returns early on "rmbespace <= 0" for every later call, and the
connection stops sending for good.  Dropping the connection has no such
state to get wrong, and for a correct peer neither path is reached.

How this was found: by AI-driven fuzzing built on top of syzkaller.  It
reached the overflow through the other writer of sndbuf_space, the
DMB-nocopy credit patched next, on a loopback-ism connection with the
peer's consumer cursor forged by a debug patch: KASAN reported an
869056-byte write past the send buffer in smc_tx_sendmsg()
(use-after-free in _copy_from_iter).  The failover route above was then
found by reading the other writers of that counter; we do not have a
multi-link SMC-R group with two RDMA devices, so that route comes from
the code and not from a run.

Fixes: c6f02ebeea3a ("net/smc: switch connections to alternate link")
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
Link: https://lore.kernel.org/all/20261122053223.2497832-2-piyus@iiitd.ac.in/   

v2 was a two-patch series.  This is a respin of the first patch only, which is
the one the review covered; the second ("net/smc: bound the DMB-nocopy
send-buffer credit as well") is unchanged and is being answered separately.

Changes in v3, all from the review of v2:

 - Replaced the fix.  v2 capped diff at sndbuf_desc->len - sndbuf_space and
   carried on.  The review pointed out where that leaves local_tx_ctrl.prod:
   behind local_rx_ctrl.cons, which is the inversion the block exists to
   repair.  smc_write_space() then computes peer_rmbe_size -
   smc_curs_diff(size, &cons, &prod) over an inverted pair - for size 100,
   prod (0,90) and cons (1,10) that is -80 - and smc_tx_rdma_writes() returns
   early on "rmbespace <= 0" for every later call, so the connection stops
   sending for good.  The cap traded an out-of-bounds write for a permanently
   mute connection.  v3 drops the connection instead and leaves every cursor
   alone, which has no partially-advanced state to get wrong.

 - The bound is no longer called "sent", in the code or in the subject.  It is
   tx_curs_prep - tx_curs_fin, prepared and not yet confirmed, because
   smc_tx_sendmsg() lowers sndbuf_space when data is copied in rather than
   when it is written; the review was right that "outstanding" described a
   tighter bound than the code applied.

 - The violation is now visible: out_of_sync and peer_conn_abort are set and
   abort_work is queued, as smc_cdc_msg_validate() does for a sequence number
   that cannot be honest.  v2 silently continued.

Not addressed here, deliberately: the review also found that
smc_cdc_tx_handler() updates sndbuf_space and tx_curs_fin under
bh_lock_sock() while smc_switch_cursor() updates both under
conn->send_lock, so the old link's completions can race the switch.  That
is true before this patch too - the unpatched smc_switch_cursor() does the
same atomic_add() and the same tx_curs_fin advance under send_lock alone -
and it needs a completion to land in the window rather than a hostile
cursor, so it is a separate bug with its own Fixes: tag.  It is answered in
the reply to the review rather than fixed blind here: we have no multi-link
SMC-R group with two RDMA devices, and the obvious drain
(smc_cdc_wait_pend_tx_wr() before the send_lock) may stall on the
smcr_link_down() path, where the old QP's completions arrive only after
smcr_link_clear() flushes it.

Measured, and not: the overflow was measured on a loopback-ism connection
with the peer's consumer cursor forged by a debug patch - KASAN reported an
869056-byte write past a 65504-byte send buffer - and with the bound in place
no report at forged cursors of 100000, 1000000 and 2000000, with ordinary
SMC-D traffic unaffected (SMC_DIAG mode=2, fb_reason=0x0).  The LLC
DELETE_LINK route to smc_switch_cursor() has not been exercised here; it
comes from reading the code.  smc_core.c builds clean and checkpatch
--strict reports nothing.
---
 net/smc/smc_core.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
index a3164201c142..ccfc3b1dc849 100644
--- a/net/smc/smc_core.c
+++ b/net/smc/smc_core.c
@@ -1055,6 +1055,7 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
 {
 	struct smc_connection *conn = &smc->conn;
 	union smc_host_cursor cons, fin;
+	int unconfirmed;
 	int rc = 0;
 	int diff;
 
@@ -1069,6 +1070,38 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
 		 * fin above, so now prod is smaller than cons. Fix that.
 		 */
 		diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
+		/* The peer's consumer cursor is taken as it arrives, so diff
+		 * is a value the peer chose.  It cannot honestly exceed what
+		 * this side has prepared and not yet had confirmed, which is
+		 * sndbuf_desc->len - sndbuf_space: smc_tx_sendmsg() lowers
+		 * sndbuf_space as data is copied in, and smc_cdc_tx_handler()
+		 * raises it again as the peer confirms.  A larger diff credits
+		 * sndbuf_space past the end of the send buffer - breaking the
+		 * "0 <= sndbuf_space <= sndbuf_desc->len" that the tx path
+		 * depends on - and pushes tx_curs_sent out of the ring, which
+		 * smc_tx_rdma_writes() then uses as a source offset.
+		 *
+		 * Simply 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 prepared has broken the protocol, so drop the
+		 * connection, as smc_cdc_msg_validate() does for a sequence
+		 * number that cannot be honest.
+		 */
+		unconfirmed = conn->sndbuf_desc->len -
+			      atomic_read(&conn->sndbuf_space);
+		if (diff > max_t(int, unconfirmed, 0)) {
+			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_add(conn->sndbuf_desc->len,
 			     &conn->tx_curs_sent, diff);
 		smc_curs_add(conn->sndbuf_desc->len,
-- 
2.34.1


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

* Re: [PATCH net v3] net/smc: drop the connection when the peer's failover cursor is impossible
  2026-11-24 17:30 [PATCH net v3] net/smc: drop the connection when the peer's failover cursor is impossible Piyus Kedia
@ 2026-11-25 17:31 ` sashiko-bot
  2026-11-26 15:08   ` Piyus Kedia
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-11-25 17:31 UTC (permalink / raw)
  To: Piyus Kedia
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] net/smc: premature cursor rewind before abort check in
  smc_switch_cursor()
- [High] net/smc: incomplete peer consumer cursor validation in
  smc_switch_cursor()
- [Medium] net/smc: false-positive abort in smc_switch_cursor() racing
  tx completion
--

commit d3ead3890b762ec4e3d6c5169a82d5ec4fde7de0
Author: Piyus Kedia <piyus@iiitd.ac.in>

net/smc: drop the connection when the peer's failover cursor is impossible

This commit aborts the SMC connection in smc_switch_cursor() when the
peer's consumer cursor advances past the unconfirmed send buffer bytes
during link failover.

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

> diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
> index a3164201c1420..ccfc3b1dc8498 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1055,6 +1055,7 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  {
>  	struct smc_connection *conn = &smc->conn;
>  	union smc_host_cursor cons, fin;
> +	int unconfirmed;
>  	int rc = 0;
>  	int diff;
>  
> @@ -1069,6 +1070,38 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  		 * fin above, so now prod is smaller than cons. Fix that.
>  		 */
>  		diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
> +		/* The peer's consumer cursor is taken as it arrives, so diff
> +		 * is a value the peer chose.  It cannot honestly exceed what
> +		 * this side has prepared and not yet had confirmed, which is
> +		 * sndbuf_desc->len - sndbuf_space: smc_tx_sendmsg() lowers
> +		 * sndbuf_space as data is copied in, and smc_cdc_tx_handler()
> +		 * raises it again as the peer confirms.  A larger diff credits
> +		 * sndbuf_space past the end of the send buffer - breaking the
> +		 * "0 <= sndbuf_space <= sndbuf_desc->len" that the tx path
> +		 * depends on - and pushes tx_curs_sent out of the ring, which
> +		 * smc_tx_rdma_writes() then uses as a source offset.
> +		 *
> +		 * Simply 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 prepared has broken the protocol, so drop the
> +		 * connection, as smc_cdc_msg_validate() does for a sequence
> +		 * number that cannot be honest.
> +		 */
> +		unconfirmed = conn->sndbuf_desc->len -
> +			      atomic_read(&conn->sndbuf_space);
> +		if (diff > max_t(int, unconfirmed, 0)) {

[Severity: High]
Is checking diff against unconfirmed sufficient to validate the peer's
consumer cursor?

First, when conn->sndbuf_desc->len exceeds conn->peer_rmbe_size,
unconfirmed can be larger than conn->peer_rmbe_size (or even twice that).
A forged diff in (conn->peer_rmbe_size, unconfirmed] passes this check,
and because smc_curs_add(conn->peer_rmbe_size, &conn->local_tx_ctrl.prod,
diff) subtracts peer_rmbe_size at most once, local_tx_ctrl.prod.count can
be left >= conn->peer_rmbe_size.

Second, unconfirmed is tx_curs_prep - tx_curs_fin, which includes data
copied into the send buffer that has not been sent yet.  A diff greater
than tx_curs_sent - tx_curs_fin advances tx_curs_sent and tx_curs_fin past
unsent bytes in sndbuf, silently dropping them.

Third, smc_cdc_cursor_to_host() does not enforce cons.count <
conn->peer_rmbe_size or bound cons.wrap jumps, and smc_curs_diff() ignores
multi-wrap differences and uses 32-bit arithmetic.  A peer can send a
cursor with cons.wrap >= fin.wrap + 2 or an out-of-range cons.count that
wraps in smc_curs_diff() to a small diff <= unconfirmed; advancing
local_tx_ctrl.prod by diff then leaves it behind local_rx_ctrl.cons so
smc_write_space() stays <= 0 and halts sending.

[Severity: Medium]
Can an in-flight TX completion on the old link race with this check and
abort a valid connection?

smc_switch_cursor() holds only spin_lock_bh(&conn->send_lock), whereas
smc_cdc_tx_handler() runs under bh_lock_sock(&smc->sk) and credits
conn->sndbuf_space before updating conn->local_tx_ctrl_fin:

CPU0                                  CPU1 (<softirq>)
smc_switch_cursor()                   smc_cdc_tx_handler()
  smc_curs_copy(&fin,
    &conn->local_tx_ctrl_fin, conn)
                                        atomic_add(diff,
                                          &cdcpend->conn->sndbuf_space)
                                        smc_curs_copy(
                                          &conn->local_tx_ctrl_fin, ...)
  diff = smc_curs_diff(..., &fin, &cons)
  unconfirmed = len -
    atomic_read(&conn->sndbuf_space)
  if (diff > max_t(int, unconfirmed, 0))
    queue_work(..., &conn->abort_work)  <-- false-positive abort

Here diff is computed from the pre-completion fin cursor while unconfirmed
reflects the post-completion sndbuf_space, so diff exceeds unconfirmed on
an honest connection.

> +			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;
> +		}

[Severity: High]
Does returning early here leave conn->tx_curs_sent and
conn->local_tx_ctrl.prod rewound while TX can still run before abort_work
acquires the socket lock?

At the start of smc_switch_cursor(), before this check runs, the cursors
are already overwritten:

net/smc/smc_core.c:smc_switch_cursor() {
	...
	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
	/* set prod cursor to old state, enforce tx_rdma_writes() */
	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
	...
}

Returning here leaves local_tx_ctrl.prod behind local_rx_ctrl.cons and
skips recalculating conn->peer_rmbe_space, which still holds the positive
credit computed when the forged CDC message arrived.  Meanwhile, the TX
path checks conn->killed and local_rx_ctrl.conn_state_flags.peer_conn_abort
rather than conn->out_of_sync or
local_tx_ctrl.conn_state_flags.peer_conn_abort:

net/smc/smc_tx.c:smc_tx_sndbuf_nonempty() {
	...
	if (conn->killed ||
	    conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
		rc = -EPIPE;    /* connection being aborted */
		goto out;
	}
	...
}

Until conn->abort_work runs and takes lock_sock(&smc->sk) to call
smc_conn_kill(), concurrent TX under lock_sock(&smc->sk) (such as
smc_tx_sendmsg(), smc_release_cb(), or smc_tx_work()) can call
smc_tx_rdma_writes() with prod < cons.  When prod.wrap != cons.wrap,
smc_tx_rdma_writes() takes the "dst_len = len" branch without clamping to
conn->peer_rmbe_size - prod.count, posting an RDMA write past the end of
the peer RMBE.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124173044.591937-1-piyus@iiitd.ac.in?part=1

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

* Re: [PATCH net v3] net/smc: drop the connection when the peer's failover cursor is impossible
  2026-11-25 17:31 ` sashiko-bot
@ 2026-11-26 15:08   ` Piyus Kedia
  0 siblings, 0 replies; 3+ 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, sashiko-reviews

Thank you - all three findings are right, and each mechanism was read in
the tree before acting on it.  v4 is posted separately.

[High] premature cursor rewind before abort check

Correct, and this is the one that mattered most.  v3 ran the check inside
the fix-up block and returned 0 from there, by which point

	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);

had already run, so the connection was left with prod behind
local_rx_ctrl.cons and peer_rmbe_space still holding the credit the forged
message produced - the atomic_set() that recalculates it is below the
block I returned from.  And 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 v3 set, so a send under lock_sock() could reach
smc_tx_rdma_writes() before abort_work got the socket lock.

v4 validates before anything is rewound and drops the connection with the
cursors as it found them.

[High] incomplete peer consumer cursor validation

Right on all three counts.

The bound is now the bytes that were SENT and not yet confirmed - the
distance between snapshots of tx_curs_fin and tx_curs_sent - rather than
sndbuf_desc->len - sndbuf_space.  As you say, that counter covers the
PREPARED bytes, so v3 would have advanced the cursors over data still
sitting in sndbuf.

cons.count and the difference are both bounded by peer_rmbe_size now.  v3
left that to smc_curs_add()'s single subtraction, which is exactly as much
as you describe: with sndbuf_desc->len larger than peer_rmbe_size, a
difference in between passed and left prod.count outside the RMBE.  Below
peer_rmbe_size the repair is exact - prod starts at fin and the difference
is their distance, so prod lands on cons rather than behind it, which also
closes the "halts sending" case in your third point.

[Medium] false-positive abort racing tx completion

Right, and it is the same line that caused it.  smc_cdc_tx_handler()
credits sndbuf_space before updating local_tx_ctrl_fin, and
smc_switch_cursor() holds only send_lock, so an honest completion between
the two reads gave a diff computed from the pre-completion cursor and an
unconfirmed from the post-completion counter.  Comparing two cursor
differences removes the mismatch: both come from snapshots taken together,
and neither is an atomic counter the other path updates first.

Tested on 7.3.1 with KASAN; net/smc/smc_core.o builds clean.

pw-bot: cr

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

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

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

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