All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Piyus Kedia" <piyus@iiitd.ac.in>
Cc: linux-s390@vger.kernel.org, "Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible
Date: Fri, 27 Nov 2026 15:09:06 +0000	[thread overview]
Message-ID: <sashiko-outbox-237679@kernel.org> (raw)
In-Reply-To: <20261126150848.2116315-1-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] net/smc: early smc_curs_comp() return in
  smc_cursor_cannot_be_honest() bypasses cons->count bounds check
- [High] net/smc: missing cons->wrap check in
  smc_cursor_cannot_be_honest() allows multi-wrap bypass
- [High] net/smc: abort path in smc_switch_cursor() still allows
  concurrent TX with prod < cons
- [Medium] net/smc: reversed fin cursor snapshots in smc_switch_cursor()
  race with smc_cdc_tx_handler()
--

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

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

This commit adds smc_cursor_cannot_be_honest() to validate the peer's
consumer cursor in smc_switch_cursor() before rewinding and repairing
the send cursors during link failover, aborting the connection if the
cursor is invalid.

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..e15629b4e384b 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1050,6 +1050,48 @@ static int smc_write_space(struct smc_connection *conn)
>  	return space;
>  }
>  
> +/* Whether the peer's consumer cursor can honestly have reached where it says.
[ ... ]
> + */
> +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;

[Severity: High]
Can the early return on smc_curs_comp() >= 0 bypass the cons->count
bounds check below?

smc_cdc_cursor_to_host() does not bound cons->count, and smc_curs_diff()
casts unsigned 32-bit cursor arithmetic to signed int inside
max_t(int, 0, ...).  If a peer sends cons->count >= 0x80000000U with
cons->wrap > fin->wrap so that (size - fin->count) + cons->count has bit
31 set, smc_curs_diff() clamps the negative int to 0 and smc_curs_comp()
returns 0.  smc_cursor_cannot_be_honest() then returns false without
checking cons->count >= (u32)conn->peer_rmbe_size, leaving
smc_switch_cursor() to skip the repair with prod.wrap != cons.wrap and
compute a positive peer_rmbe_space so smc_tx_rdma_writes() takes the
unclamped dst_len = len branch past the end of the peer RMBE.

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

[Severity: High]
Can a peer with cons->wrap more than one wrap ahead of fin->wrap pass
smc_cursor_cannot_be_honest() and leave conn->local_tx_ctrl.prod behind
conn->local_rx_ctrl.cons?

smc_curs_diff() only checks old->wrap != new->wrap and ignores how many
wraps separate them:

net/smc/smc_cdc.h:smc_curs_diff() {
	if (old->wrap != new->wrap)
		return max_t(int, 0,
			     ((size - old->count) + new->count));
...
}

If the peer sends cons->wrap >= fin->wrap + 2 with cons->count <
fin->count and diff <= unconfirmed, smc_cursor_cannot_be_honest() returns
false, but smc_curs_add() in smc_switch_cursor() increments
conn->local_tx_ctrl.prod.wrap at most once (to fin->wrap + 1).  That
leaves prod.wrap != cons.wrap after the repair; any subsequent CDC update
that advances cons credits peer_rmbe_space > 0 while prod.wrap !=
cons.wrap, allowing smc_tx_rdma_writes() to take the unclamped
dst_len = len branch and write past the end of the peer RMBE.

> +
>  static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  			     struct smc_wr_buf *wr_buf)
>  {
> @@ -1058,11 +1100,33 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  	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)) {

[Severity: Medium]
Can reading conn->local_tx_ctrl_fin before conn->tx_curs_fin race with
smc_cdc_tx_handler() and falsely abort a healthy connection?

smc_switch_cursor() holds conn->send_lock, while smc_cdc_tx_handler()
runs under bh_lock_sock() without conn->send_lock and updates the two
cursors in the opposite order:

CPU0                                  CPU1 (<softirq>)
smc_switch_cursor()                   smc_cdc_tx_handler()
  smc_curs_copy(&fin,
    &conn->local_tx_ctrl_fin, conn)
                                        smc_curs_copy(&conn->tx_curs_fin,
                                          &cdcpend->cursor, conn)
                                        smc_curs_copy(&conn->local_tx_ctrl_fin,
                                          &cdcpend->p_cursor, conn)
  smc_cursor_cannot_be_honest()
    smc_curs_copy(&tx_fin,
      &conn->tx_curs_fin, conn)
    diff > unconfirmed  <-- false positive abort

Because fin has the pre-completion value while tx_fin has the
post-completion value, diff includes the newly confirmed bytes that an
honest peer may already have consumed, while unconfirmed has already
subtracted them.  Snapshotting conn->tx_curs_fin before
conn->local_tx_ctrl_fin avoids the false positive, though a completion
after the check can still cause the repair block below to use the older
fin snapshot.

> +		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]
Can a concurrent sender under lock_sock() still reach smc_tx_rdma_writes()
with prod < cons after this abort branch returns?

When the peer forges cons past what was ever sent (diff > unconfirmed),
smc_cdc_msg_recv_action() has already written the forged cursor to
conn->local_rx_ctrl.cons and credited conn->peer_rmbe_space, so
conn->local_tx_ctrl.prod (which sits at fin + unconfirmed) is already
behind conn->local_rx_ctrl.cons before smc_switch_cursor() runs.
smc_switch_conns() switches conn->lnk to the new active link right before
calling smc_switch_cursor():

CPU0                                  CPU1
smc_tx_sendmsg()
  lock_sock(&smc->sk)
                                      smc_switch_conns()
                                        spin_lock_bh(&conn->send_lock)
                                        smc_switch_link_and_count(conn, to_lnk)
                                        smc_switch_cursor()
                                          conn->out_of_sync = 1
                                          queue_work(..., &conn->abort_work)
                                        spin_unlock_bh(&conn->send_lock)
  smc_tx_sndbuf_nonempty()
    spin_lock_bh(&conn->send_lock)
    smc_tx_rdma_writes()  <-- prod < cons on to_lnk

Because smc_tx_sndbuf_nonempty() checks only peer_rmbe_space <= 0,
conn->killed, and conn->local_rx_ctrl.conn_state_flags.peer_conn_abort,
CPU0 is not stopped before abort_work acquires lock_sock(&smc->sk), and
can take the unclamped dst_len = len branch when prod.wrap != cons.wrap.
Setting conn->killed = 1 or atomic_set(&conn->peer_rmbe_space, 0) before
dropping conn->send_lock would block the TX path immediately.

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

  reply	other threads:[~2026-11-27 15:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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:09 ` sashiko-bot [this message]
2026-11-27 15:23 ` netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-237679@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=piyus@iiitd.ac.in \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.