Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [BUG] net/smc: out-of-bounds WRITE of 869056 bytes into a 65504-byte send buffer, from a peer-credited sndbuf_space (SMC-D DMB-nocopy)
@ 2026-11-22  5:32 Piyus Kedia
  2026-11-22  5:32 ` [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit Piyus Kedia
  0 siblings, 1 reply; 4+ messages in thread
From: Piyus Kedia @ 2026-11-22  5:32 UTC (permalink / raw)
  To: alibuda, Dust Li, Sidraya Jayagond, Mahanta Jambigi, netdev
  Cc: Tony Lu, Wen Gu, linux-rdma, linux-s390, linux-kernel,
	Kapil Vaswani

Hi,

A second finding in net/smc, on the transmit side this time.  

Summary
=======

Affected: net.git main at d5b89d55a322 and the 7.3.1 tree we work on.

Problem: smc_tx_sendmsg() clamps the first of its two ring chunks against
the send buffer and the second not at all, so the copy is bounded only by
conn->sndbuf_space.  On the SMC-D DMB-nocopy path that counter is credited
from the peer's consumer cursor.  A peer that reports an oversized cursor
therefore makes memcpy_from_msg() write sendmsg() data past the end of the
send buffer.

Relationship to the other report: the cursor itself is what our other patch
("net/smc: validate the peer's CDC cursors against our own buffers", v2)
validates, and with that applied the known way to inflate this counter is
closed.  We are still reporting this separately because the bound
smc_tx_sendmsg() needs is its own: nothing at the copy says the length fits
the buffer, and a reader of the second chunk has to go and prove it
elsewhere.  Take this as hardening if you prefer - we would rather send it
than assume the cursor check is the last word.


1. Root cause
=============

net/smc/smc_tx.c:235-265, smc_tx_sendmsg():

	writespace = atomic_read(&conn->sndbuf_space);
	/* not more than what user space asked for */
	copylen = min_t(size_t, send_remaining, writespace);
	/* determine start of sndbuf */
	sndbuf_base = conn->sndbuf_desc->cpu_addr;
	smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
	tx_cnt_prep = prep.count;
	/* either unwrapped case, or 1st chunk of wrapped case */
	chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
			  tx_cnt_prep);
	chunk_len_sum = chunk_len;
	chunk_off = tx_cnt_prep;
	for (chunk = 0; chunk < 2; chunk++) {
		rc = memcpy_from_msg(sndbuf_base + chunk_off, msg, chunk_len);
		...

and for the second iteration (:263-265):

		chunk_len = copylen - chunk_len_sum;	/* remainder */
		chunk_len_sum += chunk_len;
		chunk_off = 0;				/* modulo offset in send ring buffer */

The first chunk cannot pass the end of the buffer because it is clamped to
sndbuf_desc->len - tx_cnt_prep.  The second is the remainder of copylen with
no clamp, written at offset 0.  So the total written is copylen, and copylen
is bounded only by writespace.


2. Why sndbuf_space is not ours
===============================

For SMC-R, and for SMC-D in the copying mode, the counter is credited from
cursors this side computed - smc_cdc_tx_handler() and the tail of
smc_cdc_msg_send() use conn->tx_curs_sent - and is safe.

The exception is the DMB-nocopy path.  net/smc/smc_cdc.c:356-370:

	if (conn->lgr->is_smcd &&
	    smc_ism_support_dmb_nocopy(conn->lgr->smcd)) {
		diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
					&conn->tx_curs_fin,
					&conn->local_rx_ctrl.cons);
		smp_mb__before_atomic();
		atomic_add(diff_tx, &conn->sndbuf_space);
		/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */

conn->local_rx_ctrl.cons is the peer's consumer cursor, taken from the CDC
message.  The comment states the invariant smc_tx_sendmsg() depends on; the
add does not establish it, and before our other patch nothing on the way
here compared that cursor with any buffer length.  On SMC-D in particular
smcd_cdc_rx_tsklet() reads the cursors out of the DMB the peer writes into
with no validation at all.

Two details that matter for judging it:

  - The nocopy sndbuf is the peer's DMB.  smcd_buf_attach()
    (net/smc/smc_core.c:2622-2650) attaches the peer's DMB and then does

	buf_desc->cpu_addr = (u8 *)buf_desc->cpu_addr +
			     sizeof(struct smcd_cdc_msg);
	buf_desc->len -= sizeof(struct smcd_cdc_msg);

    so an overflow of this buffer runs off the end of that DMB mapping, and
    sndbuf_desc->len equals the peer's RMB size - which is why a single
    bound on the consumer cursor covers both uses of it.
  - The data written is the local application's own sendmsg() payload.  The
    peer chooses the length; the sender chooses the bytes.

CONFIG_DIBS_LO advertises nocopy support (dibs_lo_support_dmb_nocopy()
returns DIBS_LO_SUPPORT_NOCOPY), so the branch is live on an ordinary
x86_64 kernel, not only on s390 ISM hardware.


3. What we observed
===================

A hostile peer is what the bug needs, and the only SMC-D device we have is
the loopback-ISM (dibs) device, where both ends are the same kernel and every
cursor is therefore kernel-generated and in range.  Userspace cannot write
the cursor either: the CDC sits at the base of the DMB, smcd_buf_attach()
starts the ghost sndbuf past it, and SMC sockets are .mmap = sock_no_mmap.

So we stood the peer in with a debug patch that overrides the consumer cursor
in smcd_cdc_rx_tsklet() AFTER validation - nothing else is changed, and that
is precisely the state the send path has to survive on its own: a credit it
did not compute.  simulated-peer-debug.patch in this directory is that patch
plus the two pr_info()s quoted below.

The credit goes out of range exactly as the code says it can:

  SMC2DBG diff_tx=1000000 space_before=65504 sndlen=65504 cons=1000000
  SMC2DBG diff_tx=1065504 space_before=0     sndlen=65504 cons=1000000

conn->sndbuf_space reaches 1065504 on a 65504-byte send buffer.  One
sendmsg() of 4 MB then turns that into an oversized copy, and the second
chunk is written at offset 0 with the remainder:

  SMCTXDBG chunk=0 off=0 len=65504  sndlen=65504  in bounds
  SMCTXDBG chunk=1 off=0 len=869056 sndlen=65504  past the end

  ==================================================================
  BUG: KASAN: use-after-free in _copy_from_iter+0x197/0xfd0
  Write of size 869056 at addr ffff888057980020 by task b/9467

   _copy_from_iter+0x197/0xfd0
   smc_tx_sendmsg+0x1011/0x12c0
   smc_sendmsg+0x1a2/0x2a0
   sock_sendmsg+0x19d/0x270
   __sys_sendto+0x22a/0x300

The full report is in
kasan-smc-tx-sendmsg-oob-write-mainline-7.4-rc3.txt.

With the patch applied and the same injected credit, every chunk stays inside
the buffer (len 65504 or less, and no second chunk past the end), KASAN is
silent, and an 8 MB transfer over the same connection completes with its
contents verified - so the clamp does not break a correct sender.

What this does and does not establish.  It establishes that an out-of-range
credit becomes an out-of-bounds write of attacker-chosen length, and that the
patch stops it.  It does not establish that a real SMC-D peer can produce
that credit - for that you need an ISM device, and the cursor's path from
the wire is the one our other patch validates.  We would rather you weigh
this knowing exactly which half was simulated.


4. Impact
=========

Stating only what the code shows, since we have no run to point at:

  - An out-of-bounds WRITE, not a read: memcpy_from_msg() into
    sndbuf_base + chunk_off for a length the peer influences - 869056 bytes
    into a 65504-byte buffer in our run.
  - The attacker is the remote SMC-D peer.  No local privilege is involved;
    the local application is an ordinary sender.
  - The bytes written are the local sender's payload rather than the peer's,
    which limits what a peer can place there but not where it lands.
  - Reachable only with CONFIG_SMC, an ISM/dibs device whose
    support_dmb_nocopy() is true, and an SMC-D connection to that peer.

We are not claiming an exploit, and we have not looked at what follows the
DMB mapping.


5. Reproducer
=============

There is no reproducer that uses only unmodified kernel interfaces, for the
reasons in section 3: the cursor cannot be forged from userspace on the one
SMC-D device we have.

What we can send, to anyone who wants to repeat the run: the debug patch that
stands in for the peer (simulated-peer-debug.patch, 70 lines, including the
two pr_info()s) and the SMC-D bulk-transfer program it is driven with.  The
program needs CONFIG_SMC, CONFIG_DIBS_LO and KASAN, and a registered UEID -
without one the SMC-Dv2 handshake declines with 0x03030008
(SMC_CLC_DECL_NOUEID) and silently falls back to TCP.


6. Suggested fix
================

Attached: 0001-net-smc-bound-a-send-against-the-send-buffer-not-aga.patch

One line: clamp copylen to conn->sndbuf_desc->len after it has been clamped
to what userspace asked for.  That bounds the copy at the point the copy is
made, whatever the credit says, and changes nothing for a correct peer.

The alternative is to cap the credit at the atomic_add(), which would
enforce the invariant the comment there states.  We are happy to send that
instead, or both; we chose the clamp because it is local to the code that
has to be safe.


7. What was and was not checked
===============================

  - The patch builds and net/smc/smc_tx.o compiles clean on net.git main at
    d5b89d55a322 with clang and KASAN.
  - SMC-D traffic over loopback-ISM works with it applied (section 3).
  - The three receive-side faults in our other report still do not fire on
    the kernel carrying both patches.

Not checked:

  - Real ISM hardware, or any SMC-D peer that is not this kernel.
  - The write side on SMC-R, where sndbuf_space is credited from our own
    cursors; we believe it is not reachable there and are not claiming it
    is.
  - Whether a peer can steer what the overflow lands on.

How this was found: by AI-driven fuzzing built on top of syzkaller.  Its analysis was then checked by hand against the code, and the credit inflation in
section 3 measured.  The out-of-bounds write was not reproduced.


Thanks,
Piyus Kedia
Kapil Vaswani

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

* [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit
  2026-11-22  5:32 [BUG] net/smc: out-of-bounds WRITE of 869056 bytes into a 65504-byte send buffer, from a peer-credited sndbuf_space (SMC-D DMB-nocopy) Piyus Kedia
@ 2026-11-22  5:32 ` Piyus Kedia
  2026-11-23  5:32   ` sashiko-bot
  2026-11-23  6:00   ` netdev-bot+sashiko
  0 siblings, 2 replies; 4+ messages in thread
From: Piyus Kedia @ 2026-11-22  5:32 UTC (permalink / raw)
  To: alibuda, Dust Li, Sidraya Jayagond, Mahanta Jambigi, netdev
  Cc: Tony Lu, Wen Gu, linux-rdma, linux-s390, linux-kernel,
	Kapil Vaswani, Piyus Kedia

smc_tx_sendmsg() decides how much to copy from the sendmsg() iterator with

	writespace = atomic_read(&conn->sndbuf_space);
	copylen = min_t(size_t, send_remaining, writespace);

and then splits the copy into the two chunks of the ring.  The first chunk
is
clamped against the buffer:

	chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len - tx_cnt_prep);

but the second is not - it is simply the remainder:

	chunk_len = copylen - chunk_len;	/* remainder */
	chunk_off = 0;

and each chunk is written with memcpy_from_msg(sndbuf_base + chunk_off,
msg,
chunk_len).  So the write is bounded only by sndbuf_space.

That counter is not always a value this side computed.  On the SMC-D
DMB-nocopy path smc_cdc_msg_recv_action() credits it from the peer's
consumer cursor:

	diff_tx = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
				&conn->local_rx_ctrl.cons);
	smp_mb__before_atomic();
	atomic_add(diff_tx, &conn->sndbuf_space);
	/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */

The comment states the invariant that smc_tx_sendmsg() relies on; the code
does not establish it, because local_rx_ctrl.cons arrives from the peer.  A
peer that reports an oversized consumer cursor therefore inflates
sndbuf_space, and the unclamped second chunk writes sendmsg() data past the
end of the send buffer - which under DMB-nocopy is the DMB the peer
published, so the overflow runs off the end of that mapping.

Clamp copylen to the buffer.  This bounds the write regardless of how the
credit was arrived at, which is the property smc_tx_sendmsg() needs and the
one a reader of the second chunk currently has to take on trust.  A correct
peer never credits more than the buffer holds, so this changes nothing for
one.

Capping the credit itself at the atomic_add() would also enforce the stated
invariant, and we would be glad to send that instead or as well; we chose
the
clamp because it bounds the copy at the point the copy is made.

Reproduced.  A hostile peer needs an ISM device we do not have, so the peer
was stood in for by a debug patch that overrides the consumer cursor after
validation, on an SMC-D connection over the loopback-ISM device.  The
credit then reads

  SMC2DBG diff_tx=1000000 space_before=65504 sndlen=65504 cons=1000000

- sndbuf_space 1065504 on a 65504-byte buffer - and one sendmsg() of 4 MB
turns that into an oversized copy, with the second chunk written at offset
0:

  SMCTXDBG chunk=0 off=0 len=65504  sndlen=65504  in bounds
  SMCTXDBG chunk=1 off=0 len=869056 sndlen=65504  past the end

  BUG: KASAN: use-after-free in _copy_from_iter+0x197/0xfd0
  Write of size 869056 at addr ffff888057980020 by task b/9467
   _copy_from_iter+0x197/0xfd0
   smc_tx_sendmsg+0x1011/0x12c0

With this patch applied and the same injected credit, every chunk stays
within the buffer (len 65504 or less, no second chunk past the end), KASAN
is silent, and an 8 MB transfer over the same connection completes with its
contents verified.  The injected cursor is the only thing the debug patch
changes; the fault and the fix are in the code above.

How this was found: by AI-driven fuzzing built on top of syzkaller, while
it was working on the receive-side cursor bugs fixed by "net/smc: validate
the peer's CDC cursors against our own buffers".  Its analysis was then
checked by hand against the code, and the credit inflation above measured.

Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
 net/smc/smc_tx.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c
index 0f0c6df3b..5dc489bf6 100644
--- a/net/smc/smc_tx.c
+++ b/net/smc/smc_tx.c
@@ -235,6 +235,13 @@ int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
 		writespace = atomic_read(&conn->sndbuf_space);
 		/* not more than what user space asked for */
 		copylen = min_t(size_t, send_remaining, writespace);
+		/* and never more than the send buffer holds.  sndbuf_space is
+		 * credited in smc_cdc_msg_recv_action() from the peer's
+		 * consumer cursor on the SMC-D DMB-nocopy path, so it is not a
+		 * value this side computed, and the second chunk below is not
+		 * clamped against the buffer the way the first one is.
+		 */
+		copylen = min_t(size_t, copylen, conn->sndbuf_desc->len);
 		/* determine start of sndbuf */
 		sndbuf_base = conn->sndbuf_desc->cpu_addr;
 		smc_curs_copy(&prep, &conn->tx_curs_prep, conn);

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

* Re: [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit
  2026-11-22  5:32 ` [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit Piyus Kedia
@ 2026-11-23  5:32   ` sashiko-bot
  2026-11-23  6:00   ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-23  5:32 UTC (permalink / raw)
  To: Piyus Kedia
  Cc: linux-rdma, linux-s390, Heiko Carstens, Vasily Gorbik,
	Alexander Gordeev, Christian Borntraeger

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] net/smc: incomplete sndbuf_space bounds in smc_tx_sendmsg() on
  peer credit
--

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

net/smc: bound a send by the send buffer, not by the peer's credit

This commit clamps copylen in smc_tx_sendmsg() to conn->sndbuf_desc->len so
that an inflated conn->sndbuf_space credited from a peer's consumer cursor
cannot cause a single ring-buffer write to copy past the end of the send
buffer.

> diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c
> index 0f0c6df3bf2ce..5dc489bf624ed 100644
> --- a/net/smc/smc_tx.c
> +++ b/net/smc/smc_tx.c
> @@ -235,6 +235,13 @@ int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
>  		writespace = atomic_read(&conn->sndbuf_space);
>  		/* not more than what user space asked for */
>  		copylen = min_t(size_t, send_remaining, writespace);
> +		/* and never more than the send buffer holds.  sndbuf_space is
> +		 * credited in smc_cdc_msg_recv_action() from the peer's
> +		 * consumer cursor on the SMC-D DMB-nocopy path, so it is not a
> +		 * value this side computed, and the second chunk below is not
> +		 * clamped against the buffer the way the first one is.
> +		 */
> +		copylen = min_t(size_t, copylen, conn->sndbuf_desc->len);

[Severity: High]
Does clamping copylen per loop iteration suffice when conn->sndbuf_space is
inflated by the peer?

Because smc_tx_sendmsg() loops while msg_data_left(msg) is non-zero, a single
sendmsg() call with len > conn->sndbuf_desc->len will subtract
conn->sndbuf_desc->len from the inflated conn->sndbuf_space and immediately
run another iteration whenever transmission is deferred or corked (for
example under MSG_MORE or TCP_CORK, where smc_tx_should_cork() stays true
because atomic_read(&conn->sndbuf_space) is still non-zero).  Subsequent
iterations overwrite unsent data in sndbuf and advance conn->tx_curs_prep
more than conn->sndbuf_desc->len ahead of conn->tx_curs_sent, which causes
smc_curs_diff() in smc_tx_rdma_writes() to compute to_send >
conn->sndbuf_desc->len or permanently desynchronize the cursor wrap counters.

In addition, if diff_tx in smc_cdc_msg_recv_action() overflows the signed
32-bit conn->sndbuf_space negative, !atomic_read(&conn->sndbuf_space) does
not block and min_t(size_t, send_remaining, writespace) sign-extends the
negative writespace to a large unsigned value that this clamp turns into a
full-buffer write.  Validating that conn->local_rx_ctrl.cons does not pass
conn->tx_curs_sent (and capping conn->sndbuf_space to conn->sndbuf_desc->len)
in smc_cdc_msg_recv_action() would preserve the 0 <= sndbuf_space <=
sndbuf_desc->len invariant across all iterations.

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

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

* Re: [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit
  2026-11-22  5:32 ` [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit Piyus Kedia
  2026-11-23  5:32   ` sashiko-bot
@ 2026-11-23  6:00   ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23  6:00 UTC (permalink / raw)
  To: piyus
  Cc: alibuda, dust.li, sidraya, mjambigi, netdev, tonylu, guwen,
	linux-rdma, linux-s390, linux-kernel, kapil, kuba

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

Critical: 0 (1) · High: 0 · Medium: 4 · Low: 1

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 commit message's threat model is wrong for the path it
  names.
- [Medium] The new comment in smc_tx_sendmsg() (net/smc/smc_tx.c:238-243)
  and the commit message name only the SMC-D DMB-nocopy credit in…
- [Medium] The patch fixes a write past the end of the send buffer and
  includes a KASAN splat, but it carries no Fixes: tag and no Cc:…
- [Medium] The clamp keeps each memcpy_from_msg() in smc_tx_sendmsg()
  inside the buffer, but the peer-derived credit stays uncapped.
- [Low] After this patch the tree contradicts itself about sndbuf_space.

Pre-existing issues:
- [Critical] This patch did not introduce this problem, but the receive
  path has the same flaw and it is still present.

--

net/smc: bound a send by the send buffer, not by the peer's credit

This patch clamps copylen in smc_tx_sendmsg() to sndbuf_desc->len. With
the clamp, the second (wrapped) chunk can no longer be written past the
end of the send buffer when sndbuf_space has been inflated. The commit
message attributes the inflated credit to the SMC-D DMB-nocopy path in
smc_cdc_msg_recv_action().

> That counter is not always a value this side computed.  On the SMC-D
> DMB-nocopy path smc_cdc_msg_recv_action() credits it from the peer's
> consumer cursor:

[ ... ]

> Reproduced.  A hostile peer needs an ISM device we do not have, so the peer
> was stood in for by a debug patch that overrides the consumer cursor after
> validation, on an SMC-D connection over the loopback-ISM device.  The

[Severity: Medium]
Is this threat model accurate for the path named here?

In smc_cdc_msg_recv_action(), the diff_tx credit only runs under this
check:

	if (conn->lgr->is_smcd &&
	    smc_ism_support_dmb_nocopy(conn->lgr->smcd)) {

smc_ism_support_dmb_nocopy() only returns true when the device provides
support_mmapped_rdmb:

	/* for now only loopback-ism supports
	 * merging sndbuf with peer DMB to avoid
	 * data copies between them.
	 */
	return (smcd->dibs->ops->support_mmapped_rdmb &&
		smcd->dibs->ops->support_mmapped_rdmb(smcd->dibs));

The only provider of that op appears to be drivers/dibs/dibs_loopback.c:

	.support_mmapped_rdmb = dibs_lo_support_dmb_nocopy,

With real ISM hardware this credit branch never runs. With loopback-ism,
both ends are the same kernel, so in-kernel SMC code writes the consumer
cursor, not a remote party.

Would it be more accurate to describe the nocopy part as hardening?

The text also says the cursor was overridden "after validation". It then
refers to "net/smc: validate the peer's CDC cursors against our own
buffers". That validation doesn't appear to be in this tree, because
smcd_cdc_msg_to_host() still copies the peer's prod/cons cursors as they
arrive. Should the message state that dependency?

The path a remote peer can actually reach seems to be smc_switch_cursor()
during SMC-R link failover (see the comment on the diff below). Could
the message name that path?

[ ... ]

> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>

[Severity: Medium]
Should this carry Fixes: tags and Cc: stable?

Before this patch, the second chunk in smc_tx_sendmsg() overflowed the
sndbuf whenever sndbuf_space exceeded sndbuf_desc->len. The commit
message includes a KASAN splat for that write.

There appear to be two sources of that credit:

cc0ab806fc52 ("net/smc: adapt cursor update when sndbuf and peer DMB
are merged") added the nocopy diff_tx credit in
smc_cdc_msg_recv_action().

c6f02ebeea3a ("net/smc: switch connections to alternate link") added
atomic_add(diff, &conn->sndbuf_space) in smc_switch_cursor(), which a
remote SMC-R peer can reach.

Without the tags, stable trees won't pick up the fix for the SMC-R case.

> diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c
> index 0f0c6df3bf2ce..5dc489bf624ed 100644
> --- a/net/smc/smc_tx.c
> +++ b/net/smc/smc_tx.c
> @@ -235,6 +235,13 @@ int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
>  		writespace = atomic_read(&conn->sndbuf_space);
>  		/* not more than what user space asked for */
>  		copylen = min_t(size_t, send_remaining, writespace);
> +		/* and never more than the send buffer holds.  sndbuf_space is
> +		 * credited in smc_cdc_msg_recv_action() from the peer's
> +		 * consumer cursor on the SMC-D DMB-nocopy path, so it is not a
> +		 * value this side computed, and the second chunk below is not
> +		 * clamped against the buffer the way the first one is.
> +		 */

[Severity: Medium]
Is the nocopy path the only place that credits sndbuf_space from a peer
cursor? smc_switch_cursor() in net/smc/smc_core.c runs on SMC-R link
failover and does:

	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);

Nothing bounds diff from above.

On SMC-R, smc_cdc_cursor_to_host() only rejects cursors that move
backward:

	if ((old.wrap == temp.wrap) &&
	    (old.count > temp.count))
		return;
	smc_curs_copy(local, &temp, conn);

A remote peer can therefore advance cons.count by a large amount. In a
multi-link group it can then force failover by sending an LLC
DELETE_LINK:

smc_llc_process_cli_delete_link()
  smc_switch_conns()
    smc_switch_cursor()
      atomic_add(diff, &conn->sndbuf_space)

That looks like the remotely reachable trigger for the overflow this
patch fixes. Should the comment and the commit message mention it?

The commit message also offers to cap the credit at the atomic_add()
instead. That cap would need to cover this site too.

smc_curs_add() subtracts the size only once. For a large enough diff,
could tx_curs_sent.count therefore stay at or above sndbuf_desc->len?
smc_tx_rdma_writes() later uses sent.count as the source offset:

	to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);

This patch leaves that cursor state as it is.

> +		copylen = min_t(size_t, copylen, conn->sndbuf_desc->len);

[Severity: Medium]
Does this clamp bound the send by the send buffer, as the subject says?
Each memcpy_from_msg() now stays inside the buffer, but sndbuf_space
itself is still uncapped.

With the numbers from the commit message, one iteration ends with:

	atomic_sub(copylen, &conn->sndbuf_space);

That leaves 1065504 - 65504 = 1000000, so this check never sends the
producer into smc_tx_wait():

	if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {

Each later loop iteration, and each later sendmsg(), can then write up
to a full ring at tx_curs_prep. Can that overwrite data the consumer
hasn't sent yet? Can it let prep get more than one wrap ahead of
tx_curs_sent and tx_curs_fin?

smc_curs_diff() only compares wraps with !=. In that state,
smc_tx_prepared_sends() and to_send in smc_tx_rdma_writes() would
misreport pending data. Meanwhile sendmsg() returns success for data
that was overwritten.

SIOCOUTQ in smc_ioctl() also still relies on the invariant:

	answ = smc->conn.sndbuf_desc->len -
			atomic_read(&smc->conn.sndbuf_space);

In that state SIOCOUTQ would return a negative queue length to
userspace.

Would the credit need to be capped where it is added, in
smc_cdc_msg_recv_action() and smc_switch_cursor(), as the commit message
offers? Without that, the invariant isn't restored.

[Severity: Low]
This isn't a functional bug, but the new comment says sndbuf_space can't
be trusted. The existing comments still claim the invariant holds. In
smc_cdc_msg_recv_action():

	atomic_add(diff_tx, &conn->sndbuf_space);
	/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */

and later in smc_tx_sendmsg():

	atomic_sub(copylen, &conn->sndbuf_space);
	/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */

copylen is now capped at len, so an inflated sndbuf_space stays above
len after the subtraction. In that case both comments are false.

The nocopy caller of smc_curs_diff() also passes an unchecked peer
cursor as new. That goes against the helper's documented contract:

	/* calculate cursor difference between old and new, where old <= new and
	 * difference cannot exceed size
	 */

Should these comments be updated along with this change?

>  		/* determine start of sndbuf */
>  		sndbuf_base = conn->sndbuf_desc->cpu_addr;
>  		smc_curs_copy(&prep, &conn->tx_curs_prep, conn);

[Severity: Critical]
This isn't a bug introduced by this patch, but the receive side appears
to have the same unclamped second chunk. In smc_rx_recvmsg():

		copylen = min_t(size_t, read_remaining, readable);
		...
			chunk_len = copylen - chunk_len; /* remainder */
			chunk_len_sum += chunk_len;
			chunk_off = 0; /* modulo offset in recv ring buffer */

The only bound on copylen is bytes_to_rcv. smc_cdc_msg_recv_action()
credits bytes_to_rcv from the peer's producer cursor with no cap:

	diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
				  &conn->local_rx_ctrl.prod);
	...
		atomic_add(diff_prod, &conn->bytes_to_rcv);
		/* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */

On SMC-R, smc_cdc_cursor_to_host() only rejects backward cursors. On
SMC-D, smcd_cdc_msg_to_host() copies prod as it arrives.

Suppose a remote peer sends the same wrap with a large count. Could
memcpy_to_msg() or smc_rx_splice() then read far past the end of the
RMB and copy kernel memory to user space?

The commit message points to "net/smc: validate the peer's CDC cursors
against our own buffers" for the receive side. That validation isn't in
this tree.

Would a similar clamp against conn->rmb_desc->len in smc_rx_recvmsg(),
or validating the cursors on receive, close this?

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

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22  5:32 [BUG] net/smc: out-of-bounds WRITE of 869056 bytes into a 65504-byte send buffer, from a peer-credited sndbuf_space (SMC-D DMB-nocopy) Piyus Kedia
2026-11-22  5:32 ` [PATCH net] net/smc: bound a send by the send buffer, not by the peer's credit Piyus Kedia
2026-11-23  5:32   ` sashiko-bot
2026-11-23  6:00   ` netdev-bot+sashiko

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