mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what was sent
@ 2026-11-24 18:02 Piyus Kedia
  2026-11-28 18:17 ` netdev-bot+sashiko
  0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-24 18:02 UTC (permalink / raw)
  To: wenjia, jaka, alibuda, tonylu, guwen
  Cc: netdev, linux-s390, linux-kernel, davem, edumazet, kuba, pabeni,
	horms, kapil, stable

smc_cdc_msg_recv_action() credits sndbuf_space from the peer's consumer
cursor on the SMC-D DMB-nocopy path and then assigns that cursor to this
side's tx_curs_fin:

	diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
				&conn->tx_curs_fin,
				&conn->local_rx_ctrl.cons);
	/* increase local sndbuf space and fin_curs */
	smp_mb__before_atomic();
	atomic_add(diff_tx, &conn->sndbuf_space);
	/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
	smp_mb__after_atomic();
	smc_curs_copy(&conn->tx_curs_fin,
		      &conn->local_rx_ctrl.cons, conn);

sndbuf_space can already be non-zero when this runs, so the sum can
exceed sndbuf_desc->len and the comment asserting otherwise does not
hold; smcd_cdc_msg_to_host() takes the cursor in host order with no
validation, so tx_curs_fin can also be assigned a count outside the send
buffer.

Unlike the failover path in smc_switch_cursor() this is not reachable
from a hostile peer today.  The branch runs only when
smc_ism_support_dmb_nocopy() is true, which needs a dibs device providing
support_mmapped_rdmb(), and the only provider is
drivers/dibs/dibs_loopback.c - where both ends of the connection are the
same kernel and the cursor is written by SMC itself.  So this is
hardening rather than a fix for an observed attack: it keeps the
invariant in the comment true by construction, keeps the cursor inside
the buffer, and stops a future device that implements the op from
inheriting the problem.

Bound the credit by the bytes this side has sent and not yet had
confirmed - smc_curs_diff(len, &tx_curs_fin, &tx_curs_sent) - and
advance tx_curs_fin by the bounded amount instead of assigning the
reported cursor.  For a cursor that reflects real consumption both are
the value that is used today.

That the bound is tx_curs_sent and not tx_curs_prep matters here.  The
peer can only consume as far as the producer cursor it was given, and
smc_tx_rdma_writes() advances that cursor together with tx_curs_sent, so
sndbuf_desc->len - sndbuf_space is the wrong quantity: it is the
distance to tx_curs_prep and admits bytes that are only prepared.
Bounding by it would move tx_curs_fin past tx_curs_sent and break the
fin <= sent <= prep order the three cursors are documented with in
smc.h, after which a later smc_tx_sendmsg() could wrap out of
tx_curs_prep into [tx_curs_sent, tx_curs_prep) and overwrite queued data
before it has been advertised.  smc_switch_cursor() uses the longer
distance, but it rewinds tx_curs_sent to tx_curs_fin first and advances
both, so the order holds there; here only tx_curs_fin moves.

How this was found: by AI-driven fuzzing built on top of syzkaller, on a
loopback-ism connection with the peer's consumer cursor forged by a debug
patch so that it reports consuming data this side never sent.  KASAN
reported an 869056-byte write past the send buffer in smc_tx_sendmsg()
(use-after-free in _copy_from_iter).  With this patch the same forged
cursor credits nothing beyond what was sent.

Fixes: cc0ab806fc52 ("net/smc: adapt cursor update when sndbuf and peer DMB are merged")
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
Link: https://lore.kernel.org/all/179552966151.3135.8162716126718648323@kernel.org/

That link is the review this version answers, which is a reply inside the
thread of the posting it supersedes:

  [PATCH net v2 2/2] net/smc: bound the DMB-nocopy send-buffer credit as well
  posted 2026-11-23

so following it reaches that posting and its discussion.  The message-id of
the v2 mail itself is not to hand here; lore's thread view beside the review
has it.

v2 was a two-patch series, and each patch drew its own review.  This respins
the second one only; the first ("net/smc: drop the connection when the peer's
failover cursor is impossible") is answered in its own thread.

Changes in v3, from the review of v2 2/2:

 - Changed the bound, which is the whole of this patch.  v2 used
   sndbuf_desc->len - sndbuf_space.  The review pointed out that this is the
   distance from tx_curs_fin to tx_curs_PREP, so it admits bytes that have
   only been prepared, and that the peer can only consume as far as the
   producer cursor it was given - which smc_tx_rdma_writes() advances together
   with tx_curs_sent.  Bounding by the longer distance could therefore move
   tx_curs_fin past tx_curs_sent and break the fin <= sent <= prep order the
   three cursors are documented with in smc.h, after which a later
   smc_tx_sendmsg() could wrap out of tx_curs_prep into
   [tx_curs_sent, tx_curs_prep) and overwrite queued data before it has been
   advertised.  v3 uses the bound the review suggests:

	unconfirmed = smc_curs_diff(conn->sndbuf_desc->len,
				    &conn->tx_curs_fin, &conn->tx_curs_sent);

   smc_curs_diff() already returns 0 rather than a negative value, so the
   max_t() that guarded the subtraction is gone with it.

 - The variable is no longer called "sent" and neither the comment nor the
   commit message says "outstanding".  The value is what was sent and not yet
   confirmed, and the text now says that.

Not addressed here, deliberately: the review also notes that peer_rmbe_space
is credited from the same peer-reported cursor with no bound at all, a few
lines above this branch, and that its "guarantee 0 <= peer_rmbe_space <=
peer_rmbe_size" comment is therefore false for a forged cursor.  That is
pre-existing, as the review says, and it is a different counter with a
different Fixes: tag which runs for every SMC-R and SMC-D connection rather
than only this loopback-only branch.  Both of its questions are answered in
the reply to the review; it wants its own patch, exercised on real SMC-R,
which we cannot do here.

Measured, and not: the overflow this guards 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).
Those runs were made with the v2 bound; the v3 bound is narrower, so it
admits no case the v2 one refused.  smc_cdc.c builds clean and checkpatch
--strict reports nothing.
---
 net/smc/smc_cdc.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
index 32d6d03df321..ecac93023d32 100644
--- a/net/smc/smc_cdc.c
+++ b/net/smc/smc_cdc.c
@@ -333,6 +333,7 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
 	union smc_host_cursor cons_old, prod_old;
 	struct smc_connection *conn = &smc->conn;
 	int diff_cons, diff_prod, diff_tx;
+	int unconfirmed;
 
 	smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
 	smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
@@ -361,13 +362,34 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
 			diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
 						&conn->tx_curs_fin,
 						&conn->local_rx_ctrl.cons);
+			/* This difference comes from the cursor the peer
+			 * reported, which is taken as it arrives.  The peer
+			 * can only have consumed what this side actually
+			 * sent - smc_tx_rdma_writes() advances tx_curs_sent
+			 * together with the producer cursor the peer sees -
+			 * so bound it by that and advance tx_curs_fin by the
+			 * bounded amount rather than assigning the reported
+			 * cursor.  The invariant below then holds by
+			 * construction, tx_curs_fin stays inside the send
+			 * buffer, and it stays at or behind tx_curs_sent:
+			 * bounding by sndbuf_desc->len - sndbuf_space would
+			 * also admit bytes that are only PREPARED, which
+			 * would move tx_curs_fin past tx_curs_sent and break
+			 * the fin <= sent <= prep order the three cursors are
+			 * documented with.
+			 */
+			unconfirmed = smc_curs_diff(conn->sndbuf_desc->len,
+						    &conn->tx_curs_fin,
+						    &conn->tx_curs_sent);
+			diff_tx = min_t(int, diff_tx, unconfirmed);
+
 			/* increase local sndbuf space and fin_curs */
 			smp_mb__before_atomic();
 			atomic_add(diff_tx, &conn->sndbuf_space);
 			/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
 			smp_mb__after_atomic();
-			smc_curs_copy(&conn->tx_curs_fin,
-				      &conn->local_rx_ctrl.cons, conn);
+			smc_curs_add(conn->sndbuf_desc->len,
+				     &conn->tx_curs_fin, diff_tx);
 
 			smc_tx_sndbuf_nonfull(smc);
 		}
-- 
2.34.1


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

* Re: [PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what was sent
  2026-11-24 18:02 [PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what was sent Piyus Kedia
@ 2026-11-28 18:17 ` netdev-bot+sashiko
  0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 18:17 UTC (permalink / raw)
  To: piyus
  Cc: wenjia, jaka, alibuda, tonylu, guwen, netdev, linux-s390,
	linux-kernel, davem, edumazet, kuba, pabeni, horms, kapil,
	stable

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The new bound in smc_cdc_msg_recv_action()
  (net/smc/smc_cdc.c:381-384) reads the live cursor conn->tx_curs_sent…
- [Low] The commit message says "smc_switch_cursor() uses the longer
  distance, but it rewinds tx_curs_sent to tx_curs_fin first and…
- [Low] The commit message says the problem "is not reachable from a
  hostile peer today" and that "this is hardening rather than a fix for…
- [Low] The new block comment in smc_cdc_msg_recv_action()
  (net/smc/smc_cdc.c:365-380) refers to "the fin <= sent <= prep order…

--

net/smc: bound the DMB-nocopy send-buffer credit by what was sent

On the SMC-D DMB-nocopy path, the send-buffer credit taken from the
peer's consumer cursor in smc_cdc_msg_recv_action() is now bounded by
the bytes sent but not yet confirmed (tx_curs_fin to tx_curs_sent).
tx_curs_fin is advanced by the bounded amount instead of being assigned
the reported cursor.

Link: https://lore.kernel.org/all/179552966151.3135.8162716126718648323@kernel.org/

> Unlike the failover path in smc_switch_cursor() this is not reachable
> from a hostile peer today.

[ ... ]

> So this is
> hardening rather than a fix for an observed attack

[Severity: Low]
The message describes this as hardening with no in-tree trigger, but it
also carries:

> Fixes: cc0ab806fc52 ("net/smc: adapt cursor update when sndbuf and peer DMB are merged")

v2 was also posted against net.

The patched branch only runs when smc_ism_support_dmb_nocopy() is true.
The only provider of support_mmapped_rdmb() is
drivers/dibs/dibs_loopback.c. The overflow was only reproduced with a
debug patch that forges the cursor.

Should this go to net-next without the Fixes: tag instead? The maintainers
will need to decide which tree it targets and whether the tag stays.

> smc_switch_cursor() uses the longer
> distance, but it rewinds tx_curs_sent to tx_curs_fin first and advances
> both, so the order holds there; here only tx_curs_fin moves.

[Severity: Low]
Is this accurate for the tree this patch lands in? In this tree,
smc_switch_cursor() applies an unbounded diff taken from the peer's
cursor:

net/smc/smc_core.c:smc_switch_cursor() {
    ...
	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);
    ...
}

It has no sndbuf_desc->len - sndbuf_space limit. That bound seems to come
from the companion patch "net/smc: drop the connection when the peer's
failover cursor is impossible". This commit neither contains that patch
nor names it as a prerequisite.

Could the sentence be dropped, or could it name the companion patch
explicitly?

> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index 32d6d03df3214..ecac93023d327 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c

[ ... ]

> @@ -361,13 +362,34 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
>  			diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
>  						&conn->tx_curs_fin,
>  						&conn->local_rx_ctrl.cons);
> +			/* This difference comes from the cursor the peer
> +			 * reported, which is taken as it arrives.  The peer
> +			 * can only have consumed what this side actually
> +			 * sent - smc_tx_rdma_writes() advances tx_curs_sent
> +			 * together with the producer cursor the peer sees -
> +			 * so bound it by that and advance tx_curs_fin by the
> +			 * bounded amount rather than assigning the reported
> +			 * cursor.  The invariant below then holds by
> +			 * construction, tx_curs_fin stays inside the send
> +			 * buffer, and it stays at or behind tx_curs_sent:
> +			 * bounding by sndbuf_desc->len - sndbuf_space would
> +			 * also admit bytes that are only PREPARED, which
> +			 * would move tx_curs_fin past tx_curs_sent and break
> +			 * the fin <= sent <= prep order the three cursors are
> +			 * documented with.
> +			 */

[Severity: Low]
Where is this order documented? The commit message also says "the
fin <= sent <= prep order the three cursors are documented with in
smc.h".

In net/smc/smc.h, struct smc_connection describes each cursor on its own
and states no ordering between them:

	union smc_host_cursor	tx_curs_prep;	/* tx - prepared data
						 * snd_max..wmem_alloc
						 */
	union smc_host_cursor	tx_curs_sent;	/* tx - sent data
						 * snd_nxt ?
						 */
	union smc_host_cursor	tx_curs_fin;	/* tx - confirmed by peer
						 * snd-wnd-begin ?
						 */

Could the order be added to the smc.h struct comment, or stated here as
its own rule?

> +			unconfirmed = smc_curs_diff(conn->sndbuf_desc->len,
> +						    &conn->tx_curs_fin,
> +						    &conn->tx_curs_sent);
> +			diff_tx = min_t(int, diff_tx, unconfirmed);

[Severity: Medium]
Can this read of conn->tx_curs_sent tear? smc_curs_diff() loads
new->wrap and new->count separately. Nothing here takes a snapshot of
tx_curs_sent, and send_lock is not held.

The writer can run at the same time on another CPU:

smc_tx_sendmsg()                     (lock_sock() owner, slock dropped)
  smc_tx_sndbuf_nonempty()
    smcd_tx_sndbuf_nonempty()        (spin_lock_bh(&conn->send_lock))
      smc_tx_rdma_writes()
        smc_curs_copy(&conn->tx_curs_sent, &sent, conn);

The reader runs as:

smcd_cdc_rx_tsklet()
  smc_cdc_msg_recv()                 (bh_lock_sock() only)
    smc_cdc_msg_recv_action()

Take fin = {W, f}, old sent = {W, c1} and new sent = {W+1, c2}, with
c2 <= f.

If the read sees {W, c2}, unconfirmed is 0 and a valid credit is
dropped. Neither sndbuf_space nor tx_curs_fin advances, although the peer
has confirmed those bytes. The credit only comes back on a later CDC that
moves cons.

If the lost amount is more than half the buffer, the receiver's
smc_tx_consumer_update() may never send another update. Could a
unidirectional sender then block in smc_tx_wait() with
sndbuf_space == 0? This stall was not traced end to end.

If the read sees {W+1, c1}, which needs load reordering, unconfirmed is
>= len. min_t() then does not limit a forged cursor, so the overflow this
patch closes is open again inside that window.

Other readers of tx_curs_sent outside send_lock take a snapshot first.
One example is smc_tx_prepared_sends(), which this function calls a few
lines later:

net/smc/smc_tx.h:smc_tx_prepared_sends() {
    ...
	smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
	smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
	return smc_curs_diff(sndbuf_desc->len, &sent, &prep);
}

Would it work to take a local snapshot with
smc_curs_copy(&sent, &conn->tx_curs_sent, conn) and diff against &sent?
A stale but consistent value should be safe for this bound.

> +
>  			/* increase local sndbuf space and fin_curs */
>  			smp_mb__before_atomic();
>  			atomic_add(diff_tx, &conn->sndbuf_space);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124180257.622064-1-piyus%40iiitd.ac.in

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

* Re: [PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what was sent
       [not found] <sashiko-outbox-233892@kernel.org>
@ 2026-11-26 15:06 ` Piyus Kedia
  0 siblings, 0 replies; 3+ messages in thread
From: Piyus Kedia @ 2026-11-26 15:06 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 - the finding is right on both halves, and the remedy is the one
you name.  v4 is posted separately.

[Medium] torn cursor read and non-atomic update in
         smc_cdc_msg_recv_action()

Confirmed.  smc_curs_diff() reads wrap and count with plain loads:

	if (old->wrap != new->wrap)
		return max_t(int, 0, ((size - old->count) + new->count));

and this path runs in a tasklet under bh_lock_sock(), which does not
exclude smc_tx_rdma_writes() advancing conn->tx_curs_sent under
conn->send_lock.  So passing &conn->tx_curs_sent in place can pair a
pre-wrap wrap with a post-wrap count, exactly as your interleaving shows.
Both outcomes you describe follow: the difference collapses to zero and
the credit is lost after local_rx_ctrl.cons has already moved, or it
exceeds sndbuf_desc->len and the bound does nothing.

The second half is right as well.  smc_curs_add() is

	curs->count += value;
	if (curs->count >= size) {
		curs->wrap++;
		curs->count -= size;
	}

so in place it sets count and then wrap with plain stores and transiently
leaves count >= sndbuf_desc->len, where the code it replaced wrote the
cursor with a single atomic64 store through smc_curs_copy().

v4 does what you suggest, and what smc_tx_prepared_sends() already does
here: it snapshots both cursors with smc_curs_copy(), computes both
differences from the snapshots, advances the local copy of tx_curs_fin and
writes it back with smc_curs_copy().  The cursor update is one atomic
64-bit write again and neither read can tear.

One note for the record: the torn read is not only in the line v3 added.
The diff_tx computation read &conn->tx_curs_fin in place before this
series, and the snapshot closes that too.

What is bounded, and the bound itself, are unchanged from v3.

Tested on 7.3.1 with KASAN; net/smc/smc_cdc.o builds clean at W=1.

pw-bot: cr

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

end of thread, other threads:[~2026-11-28 18:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 18:02 [PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what was sent Piyus Kedia
2026-11-28 18:17 ` netdev-bot+sashiko
     [not found] <sashiko-outbox-233892@kernel.org>
2026-11-26 15:06 ` Piyus Kedia

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

all inboxes | Powered by JetHome®