Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers
@ 2026-11-22  4:21 Piyus Kedia
  2026-11-22 13:39 ` Dust Li
  2026-11-23  4:22 ` netdev-bot+sashiko
  0 siblings, 2 replies; 4+ messages in thread
From: Piyus Kedia @ 2026-11-22  4:21 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

A peer's CDC message carries two cursors that are offsets into buffers
described on this side: the producer cursor counts the bytes the peer has
written into the RMB we published to it, and the consumer cursor the bytes
it has taken from the RMB it published to us.  Neither is checked, on either
transport.

smcr_cdc_msg_to_host() and smcd_cdc_msg_to_host() copy both into the
connection as they arrive, and every later user treats them as trustworthy:

 - smc_cdc_msg_recv_action() adds the producer difference to
   conn->bytes_to_rcv under a comment asserting
   "guarantee 0 <= bytes_to_rcv <= rmb_desc->len", which nothing enforces;
 - smc_cdc_handle_urg_data_arrival() reads
   *(base + conn->urg_curs.count - 1), an unbounded index into the RMB;
 - smc_rx_recvmsg() clamps only the first of its two ring chunks
   (rmb_desc->len - cons.count) and uses the remainder unclamped for the
   second, so bytes_to_rcv becomes a copy length;
 - smc_rx_splice() walks PAGE_ALIGN(len + offset) / PAGE_SIZE pages calling
   vmalloc_to_page() without checking the result, and get_page() then
   dereferences NULL once the walk passes the end of the RMB.

A peer that reports a cursor larger than the buffer therefore makes the
kernel read past it.  With a 64 KiB RMB and a forged cursor, KASAN on
7.4.0-rc3 reports a 1-byte read in the urgent-data path and a 131072-byte
read in the receive path, the latter via _copy_to_iter() - i.e. the
out-of-bounds bytes are delivered to userspace by recvmsg() - and splice()
oopses on the NULL page.

Validate both cursors where each transport accepts the message, so that one
test per transport covers every consumer.  Each cursor is bounded by the
buffer its own arithmetic uses: the producer cursor by conn->rmb_desc->len
as in smc_cdc_msg_recv_action(), the consumer cursor by
conn->peer_rmbe_size as in the diff_cons calculation beside it.  A cursor
equal to the buffer length is legitimate (a full ring), so only a larger one
is rejected.  An out-of-range cursor cannot occur in a correct peer, and the
malformed message is dropped like any other.

The SMC-R check sits next to the existing length and sequence-number tests
in smc_cdc_rx_handler().  SMC-D needs its own, because smcd_cdc_rx_tsklet()
reads the cursors straight out of the DMB the peer writes into, in host
byte order and with no validation of any kind on that path today.

How this was found: by AI-driven fuzzing built on top of syzkaller.  Its
analysis was then checked by hand against the code, and all three faults
were reproduced on net.git main under KASAN, with the patched kernel clean
on the same three reproducers and ordinary SMC-R and SMC-D traffic
unaffected.

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

diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
index 32d6d03df..8d24e0c83 100644
--- a/net/smc/smc_cdc.c
+++ b/net/smc/smc_cdc.c
@@ -433,6 +433,18 @@ static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
 	sock_put(&smc->sk); /* no free sk in softirq-context */
 }
 
+static bool smcd_cdc_cursors_invalid(struct smc_connection *conn,
+				     struct smcd_cdc_msg *cdc)
+{
+	if (!conn->rmb_desc)
+		return true;
+	if (cdc->prod.count > (u32)conn->rmb_desc->len)
+		return true;
+	if (cdc->cons.count > (u32)conn->peer_rmbe_size)
+		return true;
+	return false;
+}
+
 /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
  * handler to indicate update in the DMBE.
  *
@@ -452,6 +464,15 @@ static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
 	data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
 	smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
 	smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
+	/* This CDC was written by the peer into the DMB we published to it, so
+	 * both cursors are peer-supplied and index buffers described on this
+	 * side.  Nothing else checks them, and smc_cdc_msg_recv_action() uses
+	 * the producer cursor as a receive length and the consumer cursor to
+	 * credit sndbuf_space - which under DMB-nocopy is the memory the peer
+	 * itself owns.  Unlike the SMC-R path these values are host order.
+	 */
+	if (smcd_cdc_cursors_invalid(conn, &cdc))
+		return;
 	smc = container_of(conn, struct smc_sock, conn);
 	smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
 }
@@ -466,6 +487,26 @@ void smcd_cdc_rx_init(struct smc_connection *conn)
 
 /***************************** init, exit, misc ******************************/
 
+/* A peer's cursors are offsets into OUR buffers: the producer cursor counts the
+ * bytes it has written into the RMB we gave it, the consumer cursor the bytes it
+ * has taken from the RMB it published to us.  Nothing else checks them.  smcr_cdc_msg_to_host()
+ * copies both into the connection, and from there smc_cdc_msg_recv_action() adds the
+ * producer difference to conn->bytes_to_rcv and the receive path uses that as a
+ * length - so a peer that reports more than the buffer holds makes the kernel read
+ * past it.
+ */
+static bool smcr_cdc_cursors_invalid(struct smc_connection *conn,
+				     struct smc_cdc_msg *cdc)
+{
+	if (!conn->rmb_desc)
+		return true;
+	if (ntohl(cdc->prod.count) > (u32)conn->rmb_desc->len)
+		return true;
+	if (ntohl(cdc->cons.count) > conn->peer_rmbe_size)
+		return true;
+	return false;
+}
+
 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
 {
 	struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
@@ -499,6 +540,9 @@ static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
 			   conn->local_rx_ctrl.seqno))
 		/* received seqno is old */
 		goto out;
+	if (smcr_cdc_cursors_invalid(conn, cdc))
+		/* peer cursor outside the buffer it describes */
+		goto out;
 
 	smc_cdc_msg_recv(smc, cdc);
 out:

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

* Re: [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers
  2026-11-22  4:21 [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers Piyus Kedia
@ 2026-11-22 13:39 ` Dust Li
  2026-11-22 14:06   ` Piyus Kedia
  2026-11-23  4:22 ` netdev-bot+sashiko
  1 sibling, 1 reply; 4+ messages in thread
From: Dust Li @ 2026-11-22 13:39 UTC (permalink / raw)
  To: Piyus Kedia, alibuda, Sidraya Jayagond, Mahanta Jambigi, netdev
  Cc: Tony Lu, Wen Gu, linux-rdma, linux-s390, linux-kernel,
	Kapil Vaswani

On 2026-11-22 09:51:20, Piyus Kedia wrote:
>A peer's CDC message carries two cursors that are offsets into buffers
>described on this side: the producer cursor counts the bytes the peer has
>written into the RMB we published to it, and the consumer cursor the bytes
>it has taken from the RMB it published to us.  Neither is checked, on either
>transport.
>
>smcr_cdc_msg_to_host() and smcd_cdc_msg_to_host() copy both into the
>connection as they arrive, and every later user treats them as trustworthy:
>
> - smc_cdc_msg_recv_action() adds the producer difference to
>   conn->bytes_to_rcv under a comment asserting
>   "guarantee 0 <= bytes_to_rcv <= rmb_desc->len", which nothing enforces;
> - smc_cdc_handle_urg_data_arrival() reads
>   *(base + conn->urg_curs.count - 1), an unbounded index into the RMB;
> - smc_rx_recvmsg() clamps only the first of its two ring chunks
>   (rmb_desc->len - cons.count) and uses the remainder unclamped for the
>   second, so bytes_to_rcv becomes a copy length;
> - smc_rx_splice() walks PAGE_ALIGN(len + offset) / PAGE_SIZE pages calling
>   vmalloc_to_page() without checking the result, and get_page() then
>   dereferences NULL once the walk passes the end of the RMB.

I think Bryam's patch is solving the same problem here:
https://lore.kernel.org/netdev/20261112-b4-disp-0287c16e-v9-0-e5e8fa688177@proton.me

Best regards,
Dust


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

* Re: [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers
  2026-11-22 13:39 ` Dust Li
@ 2026-11-22 14:06   ` Piyus Kedia
  0 siblings, 0 replies; 4+ messages in thread
From: Piyus Kedia @ 2026-11-22 14:06 UTC (permalink / raw)
  To: dust.li
  Cc: alibuda, Sidraya Jayagond, Mahanta Jambigi, netdev, Tony Lu,
	Wen Gu, linux-rdma, linux-s390, linux-kernel, Kapil Vaswani

Hi Dust,

Thanks for pointing this out. I checked that Bryam's patch indeed
fixes the same issue. Please drop this patch.

Best,
Piyus


On Sun, Nov 22, 2026 at 7:09 PM Dust Li <dust.li@linux.alibaba.com> wrote:
>
> On 2026-11-22 09:51:20, Piyus Kedia wrote:
> >A peer's CDC message carries two cursors that are offsets into buffers
> >described on this side: the producer cursor counts the bytes the peer has
> >written into the RMB we published to it, and the consumer cursor the bytes
> >it has taken from the RMB it published to us.  Neither is checked, on either
> >transport.
> >
> >smcr_cdc_msg_to_host() and smcd_cdc_msg_to_host() copy both into the
> >connection as they arrive, and every later user treats them as trustworthy:
> >
> > - smc_cdc_msg_recv_action() adds the producer difference to
> >   conn->bytes_to_rcv under a comment asserting
> >   "guarantee 0 <= bytes_to_rcv <= rmb_desc->len", which nothing enforces;
> > - smc_cdc_handle_urg_data_arrival() reads
> >   *(base + conn->urg_curs.count - 1), an unbounded index into the RMB;
> > - smc_rx_recvmsg() clamps only the first of its two ring chunks
> >   (rmb_desc->len - cons.count) and uses the remainder unclamped for the
> >   second, so bytes_to_rcv becomes a copy length;
> > - smc_rx_splice() walks PAGE_ALIGN(len + offset) / PAGE_SIZE pages calling
> >   vmalloc_to_page() without checking the result, and get_page() then
> >   dereferences NULL once the walk passes the end of the RMB.
>
> I think Bryam's patch is solving the same problem here:
> https://lore.kernel.org/netdev/20261112-b4-disp-0287c16e-v9-0-e5e8fa688177@proton.me
>
> Best regards,
> Dust
>

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

* Re: [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers
  2026-11-22  4:21 [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers Piyus Kedia
  2026-11-22 13:39 ` Dust Li
@ 2026-11-23  4:22 ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23  4:22 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 6 potential
issue(s) to consider.

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

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 fix is incomplete.
- [Low] The new validators use `>` and accept count == buffer length.
- [Low] The commit fixes remotely triggerable KASAN-confirmed faults: an
  out-of-bounds RMB read whose bytes reach userspace via recvmsg(), plus…
- [Low] The From: author is Piyus Kedia, but the first Signed-off-by is
  Kapil Vaswani, with no matching Co-developed-by: tag.
- [Low] smcd_cdc_cursors_invalid() starts with `if (!conn->rmb_desc)
  return true;`.
- [Low] The new comment in smcd_cdc_rx_tsklet() says the check guards the
  consumer-cursor credit to sndbuf_space under DMB-nocopy.

--

net/smc: validate the peer's CDC cursors against our own buffers

This commit checks the producer and consumer cursors in incoming CDC
messages, in smc_cdc_rx_handler() for SMC-R and smcd_cdc_rx_tsklet() for
SMC-D. A message is dropped if a cursor count is larger than
rmb_desc->len (producer) or peer_rmbe_size (consumer). The goal is to
stop out-of-bounds reads in the urgent data, recvmsg() and splice()
paths.

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

[Severity: Low]
This patch fixes out-of-bounds reads that a remote peer can trigger on
both SMC-R and SMC-D. There is no Fixes: tag and no Cc: stable.

Could Fixes: tags be added? The SMC-R path probably goes back to the
original CDC receive code. The SMC-D path probably goes back to the
commit that added SMC-D CDC receive. So two tags may be needed.

[Severity: Low]
The author is Piyus Kedia, but the first Signed-off-by is Kapil Vaswani,
and there is no Co-developed-by: tag.

Documentation/process/submitting-patches.rst lists a co-author with
Co-developed-by: immediately followed by their Signed-off-by. Otherwise
the Signed-off-by order should follow how the patch was handled.

Should this be Co-developed-by: plus Signed-off-by? If Kapil reported or
analysed the issue, Reported-by: or Suggested-by: may fit better.

> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index 32d6d03df3214..8d24e0c839260 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -433,6 +433,18 @@ static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
>  	sock_put(&smc->sk); /* no free sk in softirq-context */
>  }
>  
> +static bool smcd_cdc_cursors_invalid(struct smc_connection *conn,
> +				     struct smcd_cdc_msg *cdc)
> +{
> +	if (!conn->rmb_desc)
> +		return true;
> +	if (cdc->prod.count > (u32)conn->rmb_desc->len)
> +		return true;

[Severity: High]
Is checking only the count field enough to keep bytes_to_rcv within
rmb_desc->len? Neither this check nor the one in
smcr_cdc_cursors_invalid() looks at wrap. Neither looks at how far the
cursor moved since the previous message.

When the wraps differ, smc_curs_diff() can return up to twice the
buffer size:

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

On SMC-R, smc_cdc_cursor_to_host() rejects only backward wrap moves, and
the peer controls seqno. On SMC-D, smcd_cdc_msg_to_host() has no
ordering check at all.

Take an RMB of length L, with prod starting at {wrap 0, count 0}. A peer
could send:

  prod {wrap 1, count L}  -> passes, diff_prod = 2L
  prod {wrap 2, count L}  -> passes, diff_prod = L

smc_cdc_msg_recv_action() then adds both with no cap:

	atomic_add(diff_prod, &conn->bytes_to_rcv);
	/* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */

That leaves bytes_to_rcv at 3L. In smc_rx_recvmsg(), only the first
chunk is clamped to rmb_desc->len - cons.count. The remainder is used
as-is:

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

The second chunk would then be 2L bytes from offset 0. memcpy_to_msg()
would copy L bytes past the RMB to userspace.

splice() gets the same oversized chunk in smc_rx_splice(). Past the end
of the RMB, vmalloc_to_page() can return NULL, and get_page()
dereferences it.

Using >= instead of > does not change this. {1, L-1} followed by
{2, L-1} still gives 3L-1.

The consumer side looks similar. diff_cons is added to peer_rmbe_space
with no cap. Under DMB-nocopy, diff_tx is added to sndbuf_space with no
cap either.

smc_tx_sendmsg() uses the same unclamped second-chunk pattern. An
inflated sndbuf_space could therefore become a write past
sndbuf_desc->len. smc_tx_rdma_writes() can also size dst_len from an
inflated peer_rmbe_space. DMB-nocopy is only used by loopback-ism today,
which narrows this part.

Given this, does this statement in the commit message hold?

  "Validate both cursors where each transport accepts the message, so
  that one test per transport covers every consumer."

Would a relational bound work better? For example, reject a prod that is
more than rmb_desc->len ahead of local_tx_ctrl.cons (via
smc_curs_comp()). Or reject when bytes_to_rcv + diff_prod would exceed
rmb_desc->len.

For cons, the new value could be required to lie between the previous
cons and our sent producer cursor. Wrap jumps greater than 1 could also
be rejected. Together these would bound the credit side.

> +	if (cdc->cons.count > (u32)conn->peer_rmbe_size)
> +		return true;
> +	return false;
> +}
> +

[ ... ]

> @@ -452,6 +464,15 @@ static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
>  	data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
>  	smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
>  	smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
> +	/* This CDC was written by the peer into the DMB we published to it, so
> +	 * both cursors are peer-supplied and index buffers described on this
> +	 * side.  Nothing else checks them, and smc_cdc_msg_recv_action() uses
> +	 * the producer cursor as a receive length and the consumer cursor to
> +	 * credit sndbuf_space - which under DMB-nocopy is the memory the peer
> +	 * itself owns.  Unlike the SMC-R path these values are host order.
> +	 */

[Severity: Low]
This comment says the check protects the consumer cursor's credit to
sndbuf_space under DMB-nocopy. smc_cdc_msg_recv_action() computes that
credit against sndbuf_desc->len:

	diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
				&conn->tx_curs_fin,
				&conn->local_rx_ctrl.cons);

smcd_cdc_cursors_invalid() bounds cons.count by peer_rmbe_size instead.

The two lengths come from different places. peer_rmbe_size comes from
the DMBE size advertised in CLC. sndbuf_desc->len is set in
smcd_buf_attach() from the dmb_len that attach_dmb returns.

If those two ever differ, could a cons.count that passes this check
exceed sndbuf_desc->len and reach tx_curs_fin and the sndbuf_space
arithmetic?

Conforming in-kernel peers produce equal values, so this is mostly about
the comment. Even when the lengths match, the check does not bound the
accumulated sndbuf_space credit. The comment seems to promise more than
the check does.

> +	if (smcd_cdc_cursors_invalid(conn, &cdc))
> +		return;

[Severity: Low]
This isn't a functional problem, but the NULL test at the start of
smcd_cdc_cursors_invalid() can never fire on this path:

	if (!conn->rmb_desc)
		return true;

smcd_cdc_rx_tsklet() has already dereferenced conn->rmb_desc->cpu_addr
and read both cursors from it before it calls the helper.

Should the test be dropped? If rmb_desc really can be NULL here, should
the test move ahead of the dereference?

>  	smc = container_of(conn, struct smc_sock, conn);
>  	smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
>  }

[ ... ]

> +static bool smcr_cdc_cursors_invalid(struct smc_connection *conn,
> +				     struct smc_cdc_msg *cdc)
> +{
> +	if (!conn->rmb_desc)
> +		return true;
> +	if (ntohl(cdc->prod.count) > (u32)conn->rmb_desc->len)
> +		return true;

[Severity: Low]
The commit message says:

  "A cursor equal to the buffer length is legitimate (a full ring), so
  only a larger one is rejected."

Is that how cursors are actually encoded? smc_curs_add() normalises
every cursor so that count stays below size:

net/smc/smc_cdc.h:smc_curs_add() {
	curs->count += value;
	if (curs->count >= size) {
		curs->wrap++;
		curs->count -= size;
	}
}

The senders put that host cursor on the wire unchanged. A conforming
peer therefore always sends a count in [0, size). A full ring shows up
as a wrap difference, not as count == size.

Should this check, the cons check below it, and the two checks in
smcd_cdc_cursors_invalid() use >= instead?

Accepting count == len lets in a value that only a non-conforming peer
can send. It then lands in local_rx_ctrl.prod/cons, urg_curs and, under
DMB-nocopy, tx_curs_fin. It also allows the single-message credit of
2 * size from smc_curs_diff() described above. Changing this alone would
not fix the accumulation problem, though.

> +	if (ntohl(cdc->cons.count) > conn->peer_rmbe_size)
> +		return true;
> +	return false;
> +}

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

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22  4:21 [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers Piyus Kedia
2026-11-22 13:39 ` Dust Li
2026-11-22 14:06   ` Piyus Kedia
2026-11-23  4:22 ` 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