All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Piyus Kedia" <piyus@iiitd.ac.in>
Cc: linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org,
	"Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH net v2] net/smc: validate the peer's CDC cursors against our own buffers
Date: Mon, 23 Nov 2026 04:21:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-228652@kernel.org> (raw)
In-Reply-To: <20261122042120.2430434-1-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] net/smc: incomplete cursor window validation in
  smcr/smcd_cdc_cursors_invalid()
- [High] net/smc: missing sndbuf_desc check in
  smcd_cdc_cursors_invalid()
- [Medium] net/smc: off-by-one cursor bounds check in
  smcr/smcd_cdc_cursors_invalid()
- [Low] net/smc: missing Fixes trailers in commit message
--

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

> net/smc: validate the peer's CDC cursors against our own buffers
>
> 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>

[Severity: Low]
Should this include Fixes: tags for the commits that introduced unvalidated
CDC cursor reception on SMC-R and SMC-D:

  Fixes: 5f08318f617b ("smc: connection data control (CDC)")
  Fixes: be244f28d22f ("net/smc: add SMC-D support in data transfer")

> 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;
> +	if (cdc->cons.count > (u32)conn->peer_rmbe_size)
> +		return true;
> +	return false;
> +}

[Severity: High]
Does smcd_cdc_cursors_invalid() also need to validate conn->sndbuf_desc and
check cdc->cons.count against conn->sndbuf_desc->len?

On SMC-D DMB-nocopy devices, smc_cdc_msg_recv_action() uses
conn->sndbuf_desc->len when diff_cons is non-zero:

net/smc/smc_cdc.c:smc_cdc_msg_recv_action() {
    ...
		if (conn->lgr->is_smcd &&
		    smc_ism_support_dmb_nocopy(conn->lgr->smcd)) {
			/* Calculate consumed data and
			 * increment free send buffer space.
			 */
			diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
						&conn->tx_curs_fin,
						&conn->local_rx_ctrl.cons);
    ...
}

During handshake setup in smc_listen_work() and smc_connect_ism(),
__smc_buf_create() registers conn in smcd->conn[] with conn->sndbuf_desc
still NULL, and smc_conn_save_peer_info() sets conn->peer_rmbe_size before
smcd_buf_attach() attaches conn->sndbuf_desc:

kworker                           <softirq>
smc_listen_work()
  smc_buf_create()
    smc_ism_set_conn(conn)
  smc_conn_save_peer_info()
    conn->peer_rmbe_size = ...
                                  smcd_cdc_rx_tsklet()
                                    smc_cdc_msg_recv_action()
                                      conn->sndbuf_desc->len
                                        <-- NULL pointer dereference
  smcd_buf_attach()

Even after smcd_buf_attach() succeeds, conn->peer_rmbe_size comes from the
peer's CLC message while conn->sndbuf_desc->len is the attached DMB length.
If the peer advertises peer_rmbe_size > sndbuf_desc->len and sends
cdc.cons.count between the two, smc_cdc_msg_recv_action() credits
conn->sndbuf_space beyond conn->sndbuf_desc->len, allowing smc_tx_sendmsg()
to write past the end of conn->sndbuf_desc->cpu_addr.

> @@ -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;
> +}

[Severity: Critical]
Can a peer still inflate conn->bytes_to_rcv past conn->rmb_desc->len (and
conn->peer_rmbe_space or conn->sndbuf_space past the send buffer size) by
advancing the cursor's wrap counter while keeping count within the buffer
length?

smcd_cdc_cursors_invalid() and smcr_cdc_cursors_invalid() only bound the raw
count fields in isolation.  In smc_cdc_msg_recv_action(), diff_prod is
computed with smc_curs_diff():

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

	return max_t(int, 0, (new->count - old->count));
}

If a peer sends a single CDC message with prod.wrap != prod_old.wrap and
prod_old.count < prod.count <= conn->rmb_desc->len (for example, from
initial {wrap = 0, count = 0} to {wrap = 1, count = len - 1}), both helpers
accept the cursor, and smc_curs_diff() returns (len - 0) + (len - 1) =
2 * len - 1.  The peer can also advance prod across multiple CDC messages
faster than userspace drains conn->bytes_to_rcv.  In either case,
atomic_add(diff_prod, &conn->bytes_to_rcv) pushes conn->bytes_to_rcv past
conn->rmb_desc->len, triggering the same out-of-bounds read in
smc_rx_recvmsg() and NULL page dereference in smc_rx_splice() described in
the commit message.  The same applies to cdc->cons, where advancing
cons.wrap or moving cons ahead of transmitted data inflates
conn->peer_rmbe_space and (on SMC-D DMB-nocopy or SMC-R failover)
conn->sndbuf_space past conn->sndbuf_desc->len, causing smc_tx_sendmsg() to
write past the end of the send buffer.

[Severity: Medium]
Should the cursor count checks in smcd_cdc_cursors_invalid() and
smcr_cdc_cursors_invalid() use >= instead of >?

Every SMC ring cursor is advanced by smc_curs_add(), which wraps count back
to 0 as soon as it reaches size:

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

A full ring starting at {wrap = W, count = 0} is represented as
{wrap = W + 1, count = 0}, so count == size is never produced by a compliant
peer.  If {wrap = W, count = len} is accepted for cdc->prod, consuming those
len bytes in smc_rx_update_consumer() advances conn->rx_curs_confirmed via
smc_curs_add() to {wrap = W + 1, count = 0} while conn->local_rx_ctrl.prod
remains {wrap = W, count = len}.  In smc_tx_consumer_update(),
smc_curs_diff_large(len, &cfed, &prod) then sees old->wrap > new->wrap,
takes the 0xffff wrap-around branch, and treats an empty receive buffer as
full.

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

  parent reply	other threads:[~2026-11-23  4:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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:21 ` sashiko-bot [this message]
2026-11-23  4:22 ` netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-228652@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=piyus@iiitd.ac.in \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.