🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
4 messages
4 total messages Started by Piyus Kedia Sat, 21 Nov 2026 19:33
[PATCH net] net/smc: validate the peer's CDC cursors against our own buffers
#20207
Author: Piyus Kedia
Date: Sat, 21 Nov 2026 19:33
80 lines
14396 bytes
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 anywhere.

smcr_cdc_msg_to_host() copies 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.

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().

Validate both cursors where the message is accepted, next to the existing
length and sequence-number checks, so that one test 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.

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

diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
index 32d6d03df..03ff10fef 100644
--- a/net/smc/smc_cdc.c
+++ b/net/smc/smc_cdc.c
@@ -466,6 +466,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 +519,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:
Re: [PATCH net] net/smc: validate the peer's CDC cursors against our own buffers
#20214
Author: netdev-bot+sinfo
Date: Sat, 21 Nov 2026 19:38
19 lines
8604 bytes
Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
Re: [PATCH net] net/smc: validate the peer's CDC cursors against our own buffers
#20340
Author: Piyus Kedia
Date: Sun, 22 Nov 2026 04:21
91 lines
14612 bytes
> This is an automated message. This series looks like a fix, but its
> commit messages seem to be missing some information:
>
>  - How the issue was discovered, e.g. hit in production, hit during
>    development, syzbot report, manual code inspection, LLM or static
>    analysis tool scan.

Sorry for the omission.

How it was found
================

By AI-driven fuzzing built on top of syzkaller.

Its analysis was checked by hand against the code before we wrote anything,
and every claim in the report was reproduced on net.git main at d5b89d55a322
under KASAN:

  - a 131072-byte read past a 65536-byte RMB in smc_rx_recvmsg(), reaching
    userspace through _copy_to_iter();
  - a 1-byte read at an attacker-chosen offset in
    smc_cdc_handle_urg_data_arrival(), from the CDC tasklet;
  - a NULL dereference and oops in smc_rx_splice(), where vmalloc_to_page()
    returns NULL past the end of the RMB and get_page() dereferences it.

Each was run on an unpatched and a patched kernel built from the same tree in
the same scripted run: unpatched reports on all three, patched clean on all
three, with SMC_DIAG confirming SMC-R came up (mode=0, fb_reason=0x0) in
every run so that a clean result cannot be a setup failure.

What changed in v2
==================

Sending v2 separately, because it is a code change rather than only the
information above: v1 validated the SMC-R path only, while the report
acknowledged that SMC-D shares all three consumers.  smcd_cdc_rx_tsklet()
reads the cursors straight out of the DMB the peer writes into, in host byte
order, with no validation of any kind - not even the length and seqno tests
the SMC-R handler applies - so v1 left the same exposure open there.  v2 adds
the equivalent check on that path.  The SMC-R check itself is unchanged.

We have no ISM device, so the SMC-D half is reasoned from the code and from
the shared consumers rather than reproduced; what we did verify at runtime is
that ordinary SMC-D traffic over the loopback-ISM device still works with the
check in place, so it does not reject a correct peer.

The interdiff from the posted v1:

  diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
  index 03ff10fef..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);
   }

Thanks,
Piyus Kedia
Re: [PATCH net] net/smc: validate the peer's CDC cursors against our own buffers
#22724
Author: netdev-bot+sashiko
Date: Wed, 25 Nov 2026 19:56
286 lines
18389 bytes
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

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

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

- [High] The fix is incomplete: it checks only the `count` half of each
  peer cursor and never looks at `wrap`.
- [High] The new validation runs only on the SMC-R receive path,
  `smc_cdc_rx_handler()`.
- [Medium] This finding is speculative.
- [Low] `smcr_cdc_cursors_invalid()` uses `>` and so accepts `prod.count
  == rmb_desc->len` and `cons.count == peer_rmbe_size`.
- [Low] The commit fixes a remotely triggerable out-of-bounds read that
  leaks kernel memory to userspace (KASAN-confirmed per the commit…
- [Low] The trailer chain is inconsistent with submitting-patches.rst.

--

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

This patch adds smcr_cdc_cursors_invalid(). It drops an SMC-R CDC message
in smc_cdc_rx_handler() when the peer's producer cursor count is larger
than rmb_desc->len, or when the consumer cursor count is larger than
peer_rmbe_size. The goal is to stop the out-of-bounds reads that a forged
cursor causes in the urgent-data and recvmsg() paths.

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

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

Should a Co-developed-by: Kapil Vaswani tag go with that sign-off, as
submitting-patches.rst describes? Or should Kapil Vaswani be the author?

[Severity: Low]
This fixes an out-of-bounds read that a remote peer can trigger and that
reaches userspace. Would a Fixes: tag and Cc: stable be appropriate?

According to git blame, the diff_prod to bytes_to_rcv accounting in
smc_cdc_msg_recv_action() came from:

5f08318f617b ("smc: connection data control (CDC)")

It was later switched to rmb_desc->len by:

69cb7dc0218b ("net/smc: add common buffer size in send and receive buffer descriptors")

The urgent byte index in smc_cdc_handle_urg_data_arrival() came from:

de8474eb9d50 ("net/smc: urgent data support")

> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index 32d6d03df3214..03ff10fef7da6 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -466,6 +466,26 @@ void smcd_cdc_rx_init(struct smc_connection *conn)

[ ... ]

> +static bool smcr_cdc_cursors_invalid(struct smc_connection *conn,
> +				     struct smc_cdc_msg *cdc)
> +{
> +	if (!conn->rmb_desc)
> +		return true;

[Severity: Medium]
This one may be speculative. Can conn->rmb_desc change between this check
and the places that use it?

The check runs in smc_cdc_rx_handler() after conns_lock has been dropped
and before smc_cdc_msg_recv() takes bh_lock_sock().
smc_cdc_msg_recv_action(), smc_cdc_handle_urg_data_arrival() and
smc_rx_recvmsg() each read conn->rmb_desc again, so the check and the use
do not work on the same snapshot:

	diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
				  &conn->local_rx_ctrl.prod);

The handshake can retry with another device or SMC version on the same
connection. smc_listen_find_device() falls through from ISM to
smc_find_rdma_v2_device_serv() and then to
smc_find_rdma_v1_device_serv(). Each one calls smc_conn_create() and
smc_buf_create(), and calls smc_conn_abort() on failure.

On that path, smc_conn_free() unregisters the connection but never clears
conn->rmb_desc:

	if (!list_empty(&lgr->list))
		smc_buf_unuse(conn, lgr); /* allow buffer reuse */
	smc_lgr_unregister_conn(conn);

smc_conn_create() then registers the connection again, and
__smc_buf_create() installs the new buffer with a plain store:

	conn->rmb_desc = buf_desc;

Suppose an RX handler on the old SMC-R link has already found the
connection. Could it pass this check against the old, larger
rmb_desc->len, and then run smc_cdc_msg_recv_action() against the new,
smaller buffer?

Between smc_conn_create() and smc_buf_create(), the connection can be
found while rmb_desc still holds the old pointer, which is not NULL. That
buffer may already have been freed by the is_reg_err branch of
smcr_buf_unuse(). The !conn->rmb_desc test would not catch this. Would the
check itself then read freed memory?

bh_lock_sock() does not exclude the process-context writer, which holds
lock_sock(). The stale rmb_desc lifetime predates this patch; only the
split between check and use is new.

Would it be safer to do the check inside smc_cdc_msg_recv_action(), using
the same READ_ONCE() snapshot of rmb_desc as the arithmetic?

> +	if (ntohl(cdc->prod.count) > (u32)conn->rmb_desc->len)
> +		return true;

[Severity: High]
This only checks the count field. Can a peer still push
conn->bytes_to_rcv past rmb_desc->len by using the wrap field?

smc_cdc_cursor_to_host() only rejects a cursor that moves backwards, so it
accepts any increase in wrap:

net/smc/smc_cdc.h:smc_cdc_cursor_to_host() {
    ...
	if ((old.wrap > temp.wrap) && temp.wrap)
		return;
	if ((old.wrap == temp.wrap) &&
	    (old.count > temp.count))
		return;
	smc_curs_copy(local, &temp, conn);
}

smc_curs_diff() does not clamp the result when the wraps differ:

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

Its header comment says the "difference cannot exceed size". Nothing on
this path makes sure of that.

For example, start from prod {wrap=0, count=0}. Three CDC messages with
increasing seqno carry prod {1,0}, {2,0} and {3,0}. All three pass
smcr_cdc_cursors_invalid(), and each one adds rmb_desc->len in
smc_cdc_msg_recv_action():

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

bytes_to_rcv then reaches 3 * len. A single message carrying
{old.wrap + 1, count = len} adds 2 * len - old.count.

In smc_rx_recvmsg(), only the first chunk is clamped, to
rmb_desc->len - cons.count. The second chunk is read from offset 0 with no
bound:

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

So memcpy_to_msg() can still copy bytes from past the end of the RMB to
userspace. If cons.count > 0, a bytes_to_rcv of 2 * len is already enough.

The same trick on the consumer cursor would push peer_rmbe_space above
peer_rmbe_size through the diff_cons addition.

Given this, is this part of the commit message accurate?

  "Validate both cursors where the message is accepted, next to the
  existing length and sequence-number checks, so that one test covers
  every consumer."

Would it work better to bound the computed difference instead of the raw
count? One option is to require the new prod cursor to be no more than
rmb_desc->len ahead of the local consumer cursor. Another is to limit the
resulting bytes_to_rcv and peer_rmbe_space. smc_curs_diff_large() already
clamps in a similar way.

> +	if (ntohl(cdc->cons.count) > conn->peer_rmbe_size)
> +		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 true for Linux peers? smc_curs_add() always normalises the
cursor:

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

So a correct Linux peer never sends count == size. A full ring appears as
the same count with a wrap one higher. Should both comparisons here use >=
instead of >?

Accepting count == len does not cause a separate out-of-bounds access,
because {w, len} behaves like {w + 1, 0} in smc_curs_diff(). Combined with
a wrap increase, though, it lets one message add the maximum of
2 * len - old.count.

It isn't clear whether any non-Linux SMC implementation sends
count == size.

> +	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 +519,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;

[Severity: High]
This check is only added on the SMC-R receive path. Is the SMC-D path
still open to the same out-of-bounds reads?

smcd_cdc_rx_tsklet() takes a snapshot of the cursors the peer wrote into
the DMB and goes straight into the shared receive code:

net/smc/smc_cdc.c:smcd_cdc_rx_tsklet() {
    ...
	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);
	smc = container_of(conn, struct smc_sock, conn);
	smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
}

The call chain is then
smc_cdc_msg_recv()->smc_cdc_msg_recv_action()->smc_cdc_msg_to_host(),
which ends up in smcd_cdc_msg_to_host(). That function copies the raw wrap
and count. It has no range check, and it lacks even the backwards check
that smc_cdc_cursor_to_host() does:

	temp.wrap = peer->prod.wrap;
	temp.count = peer->prod.count;
	smc_curs_copy(&local->prod, &temp, conn);

>From there, the consumers listed in the commit message are shared with
SMC-R:

- the urgent byte read in smc_cdc_handle_urg_data_arrival():

	conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);

- the bytes_to_rcv accounting that feeds the second chunk in
  smc_rx_recvmsg();

- with dmb_nocopy, the diff_tx addition to sndbuf_space.

On s390, the ISM peer is another LPAR or guest. On loopback-ism it is the
local kernel.

Could the check move into smc_cdc_msg_recv_action(), after the host
conversion, so that both paths are covered? For SMC-D it would need to
work on the tasklet's local snapshot, since the peer can rewrite the DMB
at any time.

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

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads