net/smc: bound the failover cursor catch-up by what is outstanding
Subsystems (6)
PATCH v2 1/2 net/smc: bound the failover cursor catch-up by what is outstanding
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, which 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 outstanding, so bound the catch-up by that. For a correct peer the bound changes nothing. For a lying one sndbuf_space keeps the invariant that smc_tx_sendmsg(), smc_tx_prepared_sends() and SIOCOUTQ in smc_ioctl() all depend on, and both cursors stay inside the send buffer.
Bounding the credit where it is added is deliberate. Clamping the copy length in smc_tx_sendmsg() instead stops the overflow but leaves sndbuf_space inflated, so the producer never blocks in smc_tx_wait(), later writes can overwrite data that has not been sent yet, and SIOCOUTQ reports a negative queue length.
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. The bound was tested on the path we can drive, where the forged cursor no longer inflates sndbuf_space and the write stays inside the buffer.
---
Changes in v2:
- Corrected the threat model, which was wrong in v1 in a way that matters. v1's subject and commit message named the SMC-D DMB-nocopy path as the attack. That path runs only when smc_ism_support_dmb_nocopy() is true, which needs a dibs device providing support_mmapped_rdmb(), and the only provider in the tree is drivers/dibs/dibs_loopback.c - where both ends of the connection are the same kernel and the cursor is written by SMC itself. It is not reachable from a hostile peer. The remotely reachable writer is smc_switch_cursor(), which v1 did not mention at all; it is patch 1/2 here.
- Replaced the fix. v1 clamped the copy length in smc_tx_sendmsg(). That stops the overflow but leaves sndbuf_space inflated, so the producer never blocks in smc_tx_wait(), later writes can overwrite data that has not been sent yet while sendmsg() reports success, smc_tx_prepared_sends() misreports pending bytes, and SIOCOUTQ in smc_ioctl() returns a negative queue length. v2 bounds the credit where it is added instead, and does not touch smc_tx.c.
- Fixed a second fault v1 left alone: the cursors advanced beside the credit in smc_switch_cursor() could also leave the ring, because smc_curs_add() subtracts the size only once, and smc_tx_rdma_writes() uses tx_curs_sent.count as a source offset. Bounding the difference fixes both.
- Added Fixes: tags to both patches, each confirmed by blaming the atomic_add() in question, and Cc: stable to 1/2 only. 2/2 is unreachable on any device in the tree, so by stable-kernel-rules it is not a bug that bothers anyone.
- One patch became two, since the two sites have different Fixes: tags and different reachability.
Measured, and not: the overflow and the fix were both 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 without the series, and with it no report at forged cursors of 100000, 1000000 and 2000000, the credit refused because nothing was outstanding, and ordinary SMC-D traffic unaffected (SMC_DIAG mode=2, fb_reason=0x0). The route to the writer in 1/2 is from reading the code: we have no multi-link SMC-R group with two RDMA devices, so the LLC DELETE_LINK path to smc_switch_cursor() has not been exercised here.
The receive side has the same unclamped second chunk in smc_rx_recvmsg(), credited from the peer's producer cursor; that is a separate patch, "net/smc:
validate the peer's CDC cursors against our own buffers". The two are
independent: that one bounds cons by peer_rmbe_size, which still permits "I consumed more than you sent", so it does not bound the credit this series bounds.
net/smc/smc_core.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/smc/smc_core.c +11 -0
@@ -1057,6 +1057,7 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,union smc_host_cursor cons, fin;int rc =0 ;int diff; +int sent;smc_curs_copy (&conn->tx_curs_sent, &conn->tx_curs_fin, conn);smc_curs_copy (&fin, &conn->local_tx_ctrl_fin, conn);
@@ -1069,6 +1070,16 @@ 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 this +* difference is a value the peer chose. An honest peer cannot +* have consumed more than this side has outstanding, so bound +* the catch-up by that: sndbuf_space then keeps the invariant +* the tx path relies on, and the cursors advanced below stay +* within the send buffer. +*/ + sent = conn->sndbuf_desc->len -atomic_read (&conn->sndbuf_space); + diff =min_t (int, diff,max_t (int, sent,0 )); +smc_curs_add (conn->sndbuf_desc->len, &conn->tx_curs_sent, diff); smc_curs_add(conn->sndbuf_desc->len,
Signature · 1 line
2.34.1
PATCH v2 2/2 net/smc: bound the DMB-nocopy send-buffer credit as well
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 the previous patch 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 what this side has outstanding 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.
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 the outstanding bytes.
---
This is 2/2 of a two-patch series; there is no cover letter. The v2 changelog is under the --- line of patch 1/2, and in short: v1 was a single patch that clamped the copy length in smc_tx_sendmsg() and named this DMB-nocopy path as a remote attack. Both were wrong - the clamp left sndbuf_space inflated, and this path is reachable only on loopback-ism, where both ends are the same kernel. v2 bounds the credit at both sites where it is added; this patch is the hardening half.
net/smc/smc_cdc.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
net/smc/smc_cdc.c +16 -2
@@ -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 sent;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,26 @@ 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. A peer +* cannot have consumed more than this side has +* outstanding, 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, and tx_curs_fin stays +* inside the send buffer. +*/ + sent = conn->sndbuf_desc->len - +atomic_read (&conn->sndbuf_space); + diff_tx =min_t (int, diff_tx,max_t (int, sent,0 )); +/* 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); }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net v2 2/2] net/smc: bound the DMB-nocopy send-buffer credit as well
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] net/smc: smc_cdc_msg_recv_action() bounds diff_tx by prepared not sent bytes - [Medium] net/smc…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] net/smc: smc_cdc_msg_recv_action() bounds diff_tx by prepared not sent bytes
- [Medium] net/smc: smc_cdc_msg_recv_action() leaves local_rx_ctrl.cons unclamped
--
commit 07f52f148d1ba914d6b4ce3e90b172be0a15f763
Author: Piyus Kedia <[email protected]>
net/smc: bound the DMB-nocopy send-buffer credit as well
This commit bounds the send-buffer credit on the SMC-D DMB-nocopy path in smc_cdc_msg_recv_action() by the amount of data outstanding in the send buffer and advances tx_curs_fin by the bounded amount.
Quoted 18 lines
net/smc/smc_cdc.c +3
-0@@ -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 sent;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,26 @@ 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. A peer
net/smc/smc_cdc.c static void smc_cdc_msg_recv_action(struct smc_sock *smc, · line 376
Quoted 10 lines
net/smc/smc_cdc.c +10
-0+* cannot have consumed more than this side has +* outstanding, 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, and tx_curs_fin stays +* inside the send buffer. +*/ + sent = conn->sndbuf_desc->len - +atomic_read (&conn->sndbuf_space); + diff_tx =min_t (int, diff_tx,max_t (int, sent,0 ));
[Severity: Medium]
Can bounding diff_tx by conn->sndbuf_desc->len - sndbuf_space still advance conn->tx_curs_fin past conn->tx_curs_sent and credit sndbuf_space for prepared data that has not been sent yet?
smc_tx_sendmsg() advances conn->tx_curs_prep and subtracts copylen from conn->sndbuf_space as soon as data is copied into the send buffer, before smc_tx_sndbuf_nonempty() advances conn->tx_curs_sent (which is skipped when corked via MSG_MORE or TCP_CORK). When prepared-but-unsent bytes exist, conn->sndbuf_desc->len - sndbuf_space includes [tx_curs_sent, tx_curs_prep), so a peer consumer cursor ahead of tx_curs_sent pushes tx_curs_fin past tx_curs_sent and allows subsequent smc_tx_sendmsg() calls to overwrite the unsent bytes. Should sent be computed with smc_curs_diff() between conn->tx_curs_fin and conn->tx_curs_sent, as smcd_cdc_msg_send() does?
net/smc/smc_cdc.c static void smc_cdc_msg_recv_action(struct smc_sock *smc, · line 384
Quoted 9 lines
net/smc/smc_cdc.c +2
-2/* 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);
[Severity: Medium]
Does advancing conn->tx_curs_fin by the clamped diff_tx while leaving conn->local_rx_ctrl.cons at the peer's raw cursor desynchronize the two cursors for subsequent CDC messages?
smc_cdc_msg_to_host() has already written the unclamped cursor into conn->local_rx_ctrl.cons. Once diff_tx is clamped, local_rx_ctrl.cons sits ahead of tx_curs_fin: if more data is sent and the peer advances its forged cursor by 1 byte, diff_cons is 1 and smc_cdc_msg_recv_action() recomputes diff_tx across the entire remaining gap between tx_curs_fin and the forged local_rx_ctrl.cons, crediting all newly sent bytes at once (whereas a later valid cursor below the forged cons_old yields diff_cons == 0 and credits nothing).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH net v2 2/2] net/smc: bound the DMB-nocopy send-buffer credit as well
In reply to Piyus KediaThank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] In net/smc/smc_cdc.c:smc_cdc_msg_recv_action(), the new DMB- nocopy clamp uses `sent = conn->sndbuf_desc->len -…
Pre-existing issues:
- [Low] This problem wasn't introduced by this patch, but in net/smc/smc_cdc.c:smc_cdc_msg_recv_action() peer_rmbe_space is still…
--
net/smc: bound the DMB-nocopy send-buffer credit as well
On the SMC-D DMB-nocopy path, smc_cdc_msg_recv_action() credits sndbuf_space from the peer's consumer cursor. This patch limits that credit to the number of bytes this side has outstanding. It also moves tx_curs_fin forward by the limited amount instead of copying the peer-reported cursor into it. The commit message calls this hardening, because only dibs_loopback provides support_mmapped_rdmb() today.
Quoted 4 lines
net/smc/smc_cdc.c +0
-0
[ ... ]
Quoted 16 lines
net/smc/smc_cdc.c +12
-0@@ -361,13 +362,26 @@ 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. A peer +* cannot have consumed more than this side has +* outstanding, 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, and tx_curs_fin stays +* inside the send buffer. +*/ + sent = conn->sndbuf_desc->len - +atomic_read (&conn->sndbuf_space); + diff_tx =min_t (int, diff_tx,max_t (int, sent,0 ));
net/smc/smc_cdc.c static void smc_cdc_msg_recv_action(struct smc_sock *smc, · line 384
Quoted 10 lines
net/smc/smc_cdc.c +3
-2+/* 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);
[Severity: Low]
Is len - sndbuf_space the right bound here?
smc_tx_sendmsg() lowers sndbuf_space when data is prepared, not when it is sent:
net/smc/smc_tx.c:smc_tx_sendmsg() {
...
atomic_sub(copylen, &conn->sndbuf_space);
...
}On the nocopy path, smcd_cdc_msg_send() returns before it credits sndbuf_space, so this function is the only place that adds to it. That makes len - sndbuf_space the distance from tx_curs_fin to tx_curs_prep. This distance includes bytes that are prepared but not yet sent. smc_ioctl() uses the same expression for SIOCOUTQ and describes it as "not send + not acked".
The peer can only consume up to the producer cursor it was given. smc_tx_rdma_writes() advances that cursor together with tx_curs_sent. A consumer cursor that claims more than tx_curs_sent is therefore clamped only to prep - fin. The smc_curs_add() above can then move tx_curs_fin past tx_curs_sent.
Would that break the fin <= sent <= prep ordering implied by the tx_curs_sent ("sent data") and tx_curs_fin ("confirmed by peer") field comments in smc.h?
If so, a later smc_tx_sendmsg() could wrap from tx_curs_prep into [tx_curs_sent, tx_curs_prep). It would then overwrite queued data before that data is advertised. On the nocopy path this buffer is the peer's DMB. Today this needs a forged consumer cursor, since only loopback-ism reaches this branch.
The memory-safety goals do look met: sndbuf_space stays <= len, and tx_curs_fin.count stays below len. However, the new comment ("A peer cannot have consumed more than this side has outstanding"), the variable name sent, and the commit message ("credits nothing beyond the outstanding bytes") seem to describe a tighter bound than the code computes.
smc_switch_cursor() in smc_core.c uses the same formula. It advances tx_curs_sent together with tx_curs_fin, so the ordering holds there. Here only tx_curs_fin moves.
Would a bound like this match the comment, and leave the honest-peer case unchanged?
sent = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin, &conn->tx_curs_sent);
net/smc/smc_cdc.c static void smc_cdc_msg_recv_action(struct smc_sock *smc, · line 387
Quoted 3 lines
net/smc/smc_cdc.c +0
-0smc_tx_sndbuf_nonfull (smc); }
[Severity: Low]
This is a pre-existing issue, not one introduced by this patch. Earlier in smc_cdc_msg_recv_action(), peer_rmbe_space is still credited from the same peer-reported consumer cursor with no bound:
net/smc/smc_cdc.c:smc_cdc_msg_recv_action() {
...
diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
&conn->local_rx_ctrl.cons);
if (diff_cons) {
...
atomic_add(diff_cons, &conn->peer_rmbe_space);
/* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
...
}With a forged cursor, can peer_rmbe_space exceed peer_rmbe_size here? That would make the comment false. This path runs for all SMC-R and SMC-D connections, not only nocopy ones.
After this patch, the nocopy path bounds sndbuf_space and tx_curs_fin, but peer_rmbe_space still takes the full diff_cons. The two counters describe the same shared buffer, so they can now disagree. The comment above the nocopy branch says both are updated "since peer has already consumed the data".
smc_tx_rdma_writes() uses peer_rmbe_space to compute write_blocked and the transmit length. The urg_tx_pend wake-up at the end of this function also requires:
atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size
Would an inflated peer_rmbe_space mean the urgent data sender is never woken through that path?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123135327.3953509-1-piyus%40iiitd.ac.in
Re: [PATCH net v2 2/2] net/smc: bound the DMB-nocopy send-buffer credit as well
Thank you - both of these were worth the mail. The first changed the patch:
the bound is now the one you suggest. The second is pre-existing, as you say, and I have answered its two questions below rather than fold an unrelated counter into this one.
v3 of this patch is posted as a separate thread:
[PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what
was sent
Each patch of the v2 series drew its own review, so each is respun in its own thread; the other one is answered where it was reviewed. The v3 changelog is under the --- line with a Link: back to the v2 posting.
1. Is len - sndbuf_space the right bound here?
Quoted 8 lines
The peer can only consume up to the producer cursor it was given.
smc_tx_rdma_writes()advances that cursor together withtx_curs_sent. A consumer cursor that claims more thantx_curs_sentis therefore clamped only to prep - fin. Thesmc_curs_add()above can then movetx_curs_finpasttx_curs_sent.
Would that break the fin <= sent <= prep ordering implied by thetx_curs_sent("sent data") andtx_curs_fin("confirmed by peer") field comments insmc.h?
It is not the right bound, and yes it would. The ordering is what those comments say:
union smc_host_cursor tx_curs_prep; /* tx - prepared data */
union smc_host_cursor tx_curs_sent; /* tx - sent data */
union smc_host_cursor tx_curs_fin; /* tx - confirmed by peer */
and nothing else in this branch moves tx_curs_sent, so the clamp was the only thing standing between a forged cursor and fin > sent. Your consequence follows too: once fin is past sent, a later smc_tx_sendmsg() can wrap out of tx_curs_prep into [tx_curs_sent, tx_curs_prep) and overwrite data that has been queued but not yet advertised - on this path, in the peer's DMB.
v3 uses the bound you give:
unconfirmed = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin, &conn->tx_curs_sent); diff_tx = min_t(int, diff_tx, unconfirmed);
The honest-peer case is unchanged: a cursor that reflects real consumption cannot be past the producer cursor the peer was given, so it is at most tx_curs_sent and the bound does not bind. 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" any more - as you note, that described a tighter bound than the code computed.
Your reading of smc_switch_cursor() is right as well: it rewinds tx_curs_sent to tx_curs_fin and then advances both, so the order holds there even with the longer distance. That patch has a review of its own and v3 of it stops clamping altogether - a cursor that claims more than was prepared is treated as a protocol violation and the connection is dropped - so the two patches no longer share a formula.
2. peer_rmbe_space is credited with no bound (pre-existing)
Quoted 2 lines
With a forged cursor, can
peer_rmbe_spaceexceedpeer_rmbe_sizehere? That would make the comment false.
Yes. diff_cons is bounded only by peer_rmbe_size, since that is the size passed to smc_curs_diff(); the atomic_add() applies no clamp, and the only thing that lowers peer_rmbe_space is smc_tx_rdma_writes() as it writes. So a cursor that reports consumption this side never produced raises it without limit, and the "guarantee 0 <= peer_rmbe_space <= peer_rmbe_size" above it is not a guarantee. It runs for every SMC-R and SMC-D connection, as you say, not only the nocopy ones.
Quoted 2 lines
Would an inflated
peer_rmbe_spacemean the urgent data sender is never woken through that path?
Through that path, yes. The wake-up tests equality:
if (diff_cons && conn->urg_tx_pend &&
atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {so while the counter sits above peer_rmbe_size the test cannot become true and urg_tx_pend stays set. I have not traced whether another route clears it in practice, so I would not claim more than that the condition here can no longer be met.
And your point that the two counters can now disagree is fair: after this patch sndbuf_space and tx_curs_fin are bounded while peer_rmbe_space still takes the full diff_cons, and the comment above the nocopy branch does speak of both being updated because "peer has already consumed the data".
I have left it out of this patch rather than fixed it quietly. It is a different counter with a different Fixes: tag, it is not confined to the loopback-only branch this patch is about, and the natural bound is a different quantity - the distance from cons_old to the producer cursor the peer has actually been given, rather than anything derived from the send buffer. Getting that wrong would stall flow control on every connection, and we have no SMC-R setup here to exercise it on; the route to this patch's branch is loopback-ism, which is why this one is hardening rather than a fix. So I would rather post it on its own, or leave it to the SMC maintainers if they would prefer to take it. Either way it should not be lost - it is the same class of bug on a path that every connection uses.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] net/smc: smc_switch_cursor() bounds catch-up by prepared instead of sent bytes - [High] net/smc…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] net/smc: smc_switch_cursor() bounds catch-up by prepared instead of sent bytes
- [High] net/smc: smc_switch_cursor() clamp leaves local_tx_ctrl.prod behind cons
--
commit d641bcdb06babb4611de33b1b2a3ed34727ace6d
Author: Piyus Kedia <[email protected]>
net/smc: bound the failover cursor catch-up by what is outstanding
This commit bounds the cursor catch-up diff in smc_switch_cursor() by conn->sndbuf_desc->len - atomic_read(&conn->sndbuf_space) so a peer's consumer cursor cannot credit conn->sndbuf_space past the send buffer size.
Quoted 14 lines
net/smc/smc_core.c +1
-0@@ -1057,6 +1057,7 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,union smc_host_cursor cons, fin;int rc =0 ;int diff; +int sent;smc_curs_copy (&conn->tx_curs_sent, &conn->tx_curs_fin, conn);smc_curs_copy (&fin, &conn->local_tx_ctrl_fin, conn);@@ -1069,6 +1070,16 @@ 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.
net/smc/smc_core.c static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, · line 1080
Quoted 10 lines
net/smc/smc_core.c +8
-0*/ diff =smc_curs_diff (conn->peer_rmbe_size, &fin, &cons); +/* The peer's consumer cursor is taken as it arrives, so this +* difference is a value the peer chose. An honest peer cannot +* have consumed more than this side has outstanding, so bound +* the catch-up by that: sndbuf_space then keeps the invariant +* the tx path relies on, and the cursors advanced below stay +* within the send buffer. +*/ + sent = conn->sndbuf_desc->len -atomic_read (&conn->sndbuf_space);
[Severity: High]
Does conn->sndbuf_desc->len - atomic_read(&conn->sndbuf_space) measure all prepared bytes in the send buffer (tx_curs_prep - tx_curs_fin) rather than the unconfirmed bytes actually transmitted to the peer RMBE (tx_curs_sent - tx_curs_fin before tx_curs_sent is overwritten above)?
When prepared but unsent data sits between tx_curs_sent and tx_curs_prep (for example due to corking, autocorking, or a full peer RMBE), advancing tx_curs_sent and tx_curs_fin up to sent skips over unsent payload in sndbuf and credits sndbuf_space for data that was never put on the wire. Also, when conn->sndbuf_desc->len > conn->peer_rmbe_size, sent can exceed conn->peer_rmbe_size, allowing smc_curs_add(conn->peer_rmbe_size, ...) below (which subtracts peer_rmbe_size only once) to leave local_tx_ctrl.prod.count at or above conn->peer_rmbe_size. Finally, advancing tx_curs_fin past the old tx_curs_sent moves it ahead of any in-flight cdcpend->cursor on the old link (which is drained via smc_wr_tx_wait_no_pending_sends() only after smc_switch_conns() returns), so a completing CQE on the old link in smc_cdc_tx_handler() either computes a wrapped diff or rewinds tx_curs_fin and lets the new link's validation CQE double-credit sndbuf_space above sndbuf_desc->len.
Computing the bound from smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin, &conn->tx_curs_sent) before overwriting conn->tx_curs_sent would limit the catch-up to bytes actually sent on the old link.
net/smc/smc_core.c static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, · line 1081
Quoted 1 line
net/smc/smc_core.c +1
-0+ diff =min_t (int, diff,max_t (int, sent,0 ));
[Severity: High]
When this clamp reduces diff below smc_curs_diff(conn->peer_rmbe_size, &fin, &cons), does leaving conn->local_rx_ctrl.cons unclamped leave conn->local_tx_ctrl.prod behind conn->local_rx_ctrl.cons after this block?
smc_switch_cursor() resets conn->local_tx_ctrl.prod to fin and relies on adding diff to bring prod up to cons, then immediately updates peer_rmbe_space via smc_write_space():
net/smc/smc_core.c:smc_write_space() {
...
smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
/* determine rx_buf space */
space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
return space;
}Both smc_curs_diff(buffer_len, &cons, &prod) and smc_tx_rdma_writes() assume cons <= prod. If prod < cons across a wrap boundary (prod.wrap != cons.wrap), smc_curs_diff() takes the old->wrap != new->wrap branch and returns (buffer_len - cons.count) + prod.count; when prod.count > cons.count that exceeds buffer_len and makes peer_rmbe_space negative, and when peer_rmbe_space > 0 smc_tx_rdma_writes() sees prod.wrap != cons.wrap, skips clamping dst_len to conn->peer_rmbe_size - prod.count, and can RDMA-write past the end of the peer RMBE.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 2
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).
- [High] The new bound in smc_switch_cursor() reads sndbuf_space, then advances tx_curs_fin and sndbuf_space.
- [Low] In smc_switch_cursor() (net/smc/smc_core.c:1080-1081), the new bound is stored in a variable named sent, and the comment calls it…
- [Low] When the new clamp in smc_switch_cursor() lowers diff below the cons - fin gap the peer reported, local_tx_ctrl.prod and…
--
net/smc: bound the failover cursor catch-up by what is outstanding
This commit clamps the diff that smc_switch_cursor() uses to advance tx_curs_sent and tx_curs_fin and to credit sndbuf_space during SMC-R link failover. The bound is sndbuf_desc->len - sndbuf_space. The goal is to stop a forged peer consumer cursor from pushing sndbuf_space past the send buffer size, which leads to an out-of-bounds write in smc_tx_sendmsg().
Quoted 4 lines
net/smc/smc_core.c +0
-0
[ ... ]
Quoted 4 lines
net/smc/smc_core.c +0
-0@@ -1069,6 +1070,16 @@ 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);
net/smc/smc_core.c static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, · line 1082
Quoted 10 lines
net/smc/smc_core.c +10
-0+/* The peer's consumer cursor is taken as it arrives, so this +* difference is a value the peer chose. An honest peer cannot +* have consumed more than this side has outstanding, so bound +* the catch-up by that: sndbuf_space then keeps the invariant +* the tx path relies on, and the cursors advanced below stay +* within the send buffer. +*/ + sent = conn->sndbuf_desc->len -atomic_read (&conn->sndbuf_space); + diff =min_t (int, diff,max_t (int, sent,0 )); +
[Severity: High]
Does this bound keep sndbuf_space <= sndbuf_desc->len on the DELETE_LINK path, given that the old link's CDC completions can still run?
smc_switch_conns() calls smc_switch_cursor() with only conn->send_lock held:
spin_lock_bh(&conn->send_lock); smc_switch_link_and_count(conn, to_lnk); rc = smc_switch_cursor(smc, pend, wr_buf); spin_unlock_bh(&conn->send_lock);
CDC send completions on the old link go through smc_cdc_tx_handler(), which holds only bh_lock_sock():
bh_lock_sock(&smc->sk);
if (!wc_status && sndbuf_desc) {
diff = smc_curs_diff(sndbuf_desc->len,
&cdcpend->conn->tx_curs_fin,
&cdcpend->cursor);
...
atomic_add(diff, &cdcpend->conn->sndbuf_space);
...
smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);In smc_llc_process_cli_delete_link(), the old QP is still live when the connections are switched. It is moved to error and drained only later, in smcr_link_clear():
if (smc_link_downing(&lnk_del->state)) smc_switch_conns(lgr, lnk_del, false); smcr_link_clear(lnk_del, true);
So completions for CDCs already posted on the old QP can run during or after smc_switch_cursor().
In the concurrent case, smc_switch_cursor() snapshots sent as prep - fin_old. The old-link handler then credits C - fin_old and sets tx_curs_fin to C. smc_switch_cursor() then credits up to prep - fin_old more and adds diff to the tx_curs_fin that has already moved. The total credit is more than prep - fin_old.
In the late completion case, a peer forges cons so that the clamp moves tx_curs_fin all the way to prep. A completion for an older cursor C then arrives on the old QP. If C.wrap != prep.wrap, smc_curs_diff() looks only at the wraps:
if (old->wrap != new->wrap) return max_t(int, 0, ((size - old->count) + new->count));
It returns (len - prep.count) + C.count, which is larger than len, so sndbuf_space moves toward 2 * len. If the wraps are equal, the credit is 0 but tx_curs_fin moves back to C. The next completion on the new link then credits prep - C again.
Either way sndbuf_space ends up above sndbuf_desc->len. The second chunk in smc_tx_sendmsg() then writes past the send buffer:
rc = memcpy_from_msg(sndbuf_base + chunk_off, msg, chunk_len);
The commit message says:
For a lying one sndbuf_space keeps the invariant that smc_tx_sendmsg(), smc_tx_prepared_sends() and SIOCOUTQ in smc_ioctl() all depend on
Does that still hold on this path? The follow-up patch "net/smc: bound the DMB-nocopy send-buffer credit as well" only changes the SMC-D DMB-nocopy credit in smc_cdc_msg_recv_action(), so it doesn't cover this case.
Do the old link's TX completions also need to be quiesced or serialized with the switch? Two options: drain them before smc_switch_cursor(), or take the same lock and ignore completions whose cursor is behind tx_curs_fin.
[Severity: Low]
Is sent the right name for this value? Does "what this side has outstanding" in the comment describe it accurately?
sndbuf_desc->len - sndbuf_space is the amount prepared but not yet confirmed (tx_curs_prep - tx_curs_fin). smc_tx_sendmsg() lowers sndbuf_space when data is copied in, not when it is RDMA-written:
atomic_sub(copylen, &conn->sndbuf_space);
The real pre-switch send position is already lost at this point, because smc_switch_cursor() starts with:
smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
So a lying peer can still move tx_curs_sent and tx_curs_fin up to tx_curs_prep. Bytes that sendmsg() accepted but never wrote are then counted as confirmed and never sent. The clamp also hides the protocol
violation: out_of_sync is not set and the connection is not aborted.
Without a race, sndbuf_space and the cursors stay in range. But the variable name, the comment, and the commit message's "bound the catch-up by what is outstanding" all describe a tighter bound than the code applies. Could they be adjusted to match?
net/smc/smc_core.c static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, · line 1085
Quoted 3 lines
net/smc/smc_core.c +0
-0smc_curs_add (conn->sndbuf_desc->len, &conn->tx_curs_sent, diff); smc_curs_add(conn->sndbuf_desc->len,
[Severity: Low]
When the clamp makes diff smaller than the cons - fin gap the peer reported, where does local_tx_ctrl.prod end up?
prod was set to fin earlier, and this branch runs only when cons > fin. Further down, prod and local_tx_ctrl_fin move forward only by the clamped diff:
smc_curs_add(conn->peer_rmbe_size, &conn->local_tx_ctrl.prod, diff); smc_curs_add(conn->peer_rmbe_size, &conn->local_tx_ctrl_fin, diff);
That leaves prod behind local_rx_ctrl.cons, which is the state the existing "now prod is smaller than cons. Fix that." comment says this block repairs.
The next statement recomputes peer_rmbe_space from that inverted pair:
atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));
and smc_write_space() computes:
space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
If the wraps are equal, smc_curs_diff() returns 0 and peer_rmbe_space becomes peer_rmbe_size. If cons.wrap == prod.wrap + 1, it returns (size - cons.count) + prod.count. For example, size=100, prod=(0,90) and cons=(1,10) give space = -80.
With a negative peer_rmbe_space, smc_tx_rdma_writes() returns early on every call:
if (rmbespace <= 0) {Later CDCs in smc_cdc_msg_recv_action() only add diff_cons on top of that wrong base, so the value never recovers and the connection stops sending.
Should the clamp firing be treated as a protocol violation that aborts the connection, rather than continuing with inconsistent flow-control state?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123135327.3953509-1-piyus%40iiitd.ac.in
Thank you for the review. All three points were checked against net.git at af6737b90668 before answering, and two of them changed the patch rather than its wording: the clamp is gone, and v3 drops the connection instead. The third is real but is a pre-existing bug independent of this patch, and I would rather not fix it blind alongside it - details below.
v3 of this patch is posted as a separate thread:
[PATCH net v3] net/smc: drop the connection when the peer's failover
cursor is impossible
Only this patch is respun; 2/2 of the v2 series is unchanged and is being answered separately. The v3 changelog is under the --- line with a Link:
back to the v2 posting.
Point by point.
1. [High] Old-link CDC completions racing the switch
Quoted 4 lines
Does this bound keep
sndbuf_space<=sndbuf_desc->lenon theDELETE_LINKpath, given that the old link's CDC completions can still run? Do the old link's TX completions also need to be quiesced or serialized with the switch?
No, it does not, and yes, they do. The locking is exactly as you describe:
smc_switch_conns()
spin_lock_bh(&conn->send_lock);
smc_switch_cursor()
atomic_add(diff, &conn->sndbuf_space);
smc_curs_add(len, &conn->tx_curs_fin, diff);
smc_cdc_tx_handler()
bh_lock_sock(&smc->sk);
atomic_add(diff, &conn->sndbuf_space);
smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);Two writers of sndbuf_space and of tx_curs_fin, under two different locks, with no lock in common. That is true before this patch as well: the unpatched smc_switch_cursor() performs the same atomic_add() and the same tx_curs_fin advance under send_lock alone. The late-completion arithmetic you give - (len - prep.count) + C.count exceeding len when the wraps differ - is likewise reachable without the patch, since the unpatched code moves tx_curs_fin by an unbounded diff and so can place it at or beyond prep. So this is a second bug on the same path, not a consequence of the change.
What this patch is for is the route that needs no race at all: a forged cons on an otherwise quiet connection inflates the credit deterministically. v3 closes that by dropping the connection rather than crediting anything.
On the race itself, the shape of the fix looks like the drain, not a new
lock: smc_cdc_wait_pend_tx_wr(conn) in smc_switch_conns() before the send_lock is taken, in the region whose comment already says it may sleep ("pre-fetch buffer outside of send_lock, might sleep"). I have not put it in this series for two reasons.
- It is a different bug with a different Fixes: tag and a different trigger - it needs a completion to land in the window, not a hostile cursor - and netdev asks for one logical change per patch.
- I cannot test it, and I think it may be wrong as stated. smc_cdc_wait_pend_tx_wr() waits for conn->cdc_pend_tx_wr to reach zero. On the smcr_link_down() path into smc_switch_conns() the old QP may already be dead, and those completions arrive only once the QP is moved to error and flushed in smcr_link_clear(), which happens after the switch. A drain there would then stall on exactly the path it is meant to protect. Ignoring completions whose cursor is behind tx_curs_fin, your second suggestion, has no such problem, but it changes the meaning of a counter three call sites rely on.
We have no multi-link SMC-R group with two RDMA devices here - the route to smc_switch_cursor() in this patch comes from reading the code, as the commit message says - so I would be shipping an untested locking change bundled with a memory-safety fix. I am happy to post it separately if the drain is the right shape, or to leave it to the SMC maintainers, who can exercise failover. Either way it should be recorded; it is a real hole.
2. [Low] "sent" is the wrong name for sndbuf_desc->len - sndbuf_space
Quoted 2 lines
Is sent the right name for this value? Does "what this side has outstanding" in the comment describe it accurately?
It is not, and it does not. The value is tx_curs_prep - tx_curs_fin - prepared and not yet confirmed - because smc_tx_sendmsg() lowers sndbuf_space when data is copied in, not when it is RDMA-written. v3 names it unconfirmed and says so:
/* ... 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. ... */
The subject and the commit message no longer claim to bound the catch-up by "what is outstanding" either.
Your related observation is also right and is why v3 stops trying to repair the state: smc_switch_cursor() opens with
smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
so the pre-switch send position is already gone by the time the check runs. There is nothing reliable left to rebuild a correct cursor from, which makes "carry on with a smaller number" the wrong answer in principle. And you are right that the clamp hid the violation: out_of_sync stayed 0 and the connection was not aborted. v3 sets out_of_sync and peer_conn_abort and queues abort_work, as smc_cdc_msg_validate() does for a sequence number that cannot be honest.
3. [Low] Where local_tx_ctrl.prod ends up when the clamp fires
Quoted 2 lines
Should the clamp firing be treated as a protocol violation that aborts the connection, rather than continuing with inconsistent flow-control state?
Yes. This is the point that replaced the patch, and the arithmetic holds as you give it. With diff clamped, prod = fin + clamped_diff stays behind cons, which is the inversion the "now prod is smaller than cons. Fix that." comment exists to repair. smc_write_space() then computes
space = peer_rmbe_size - smc_curs_diff(peer_rmbe_size, &cons, &prod);
over that inverted pair. Equal wraps give 0, so space becomes the whole buffer - wrong, but harmless. cons.wrap == prod.wrap + 1 gives (size - cons.count) + prod.count: for size 100, prod (0,90) and cons (1,10), space = 100 - 180 = -80. smc_tx_rdma_writes() returns early on
if (rmbespace <= 0) {for every later call, and smc_cdc_msg_recv_action() only adds diff_cons on top of that base, so it never recovers. The clamp traded an out-of-bounds write for a connection that is memory-safe and permanently mute.
v3 therefore drops the connection and leaves every cursor alone. There is no partially-advanced state to be inconsistent, nothing to name wrongly, and for a correct peer the branch is not reached at all.
Thanks again - two of these were defects in the patch, and the third is a bug in the path that was there before it.
pw-bot: cr