Linux s390 Architecture development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Qican Ma" <goonsomeway@gmail.com>
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] net/smc: validate peer CDC cursors against buffer bounds and window
Date: Wed, 25 Nov 2026 03:21:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-232458@kernel.org> (raw)
In-Reply-To: <CA+OT+ON4Lt9mLvWPK8rUaHKZ01_p0fGCiCsWHwXWzT1sGaTtLA@mail.gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Critical] net/smc: missing monotonicity and wrap checks in
  smc_cdc_msg_recv_action()
- [High] net/smc: incomplete diff_prod window check in
  smc_cdc_msg_recv_action()
- [High] net/smc: valid initial CDC messages dropped in
  smc_cdc_msg_recv_action()
--

commit 1b4f7a808b103f512ec58fa5533fdc1b60707fd4
Author: Qican Ma <goonsomeway@gmail.com>

net/smc: validate peer CDC cursors against buffer bounds and window

This commit decodes incoming CDC messages into a temporary
smc_host_cdc_msg and validates cursor offsets and deltas against buffer
bounds and available capacity before updating conn->local_rx_ctrl.

> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index 32d6d03df3214..fd5e296e8becb 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -332,14 +332,48 @@ 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;
> +	struct smc_host_cdc_msg rx_ctrl = {};
> +	int diff_cons, diff_prod, diff_tx = 0;
>  
>  	smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
>  	smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
> -	smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
> +	rx_ctrl.prod = prod_old;
> +	rx_ctrl.cons = cons_old;
> +	smc_cdc_msg_to_host(&rx_ctrl, cdc, conn);
> +
> +	if (rx_ctrl.prod.count >= conn->rmb_desc->len ||
> +	    rx_ctrl.cons.count >= conn->peer_rmbe_size)
> +		return;

[Severity: High]
Does checking rx_ctrl.cons.count >= conn->peer_rmbe_size drop valid
initial CDC messages on a listening server before conn->peer_rmbe_size is
initialized?

In smc_listen_work(), the server allocates conn->rmb_desc and registers
the connection before sending SMC_CLC_ACCEPT, while conn->peer_rmbe_size
remains 0 until SMC_CLC_CONFIRM arrives and smc_conn_save_peer_info()
runs.  On the client, smc_connect_rdma() and smc_connect_ism() send
SMC_CLC_CONFIRM, set sk_state to SMC_ACTIVE, and return from connect()
immediately:

kworker (server)                  <softirq> (server)
smc_listen_work()
  smc_listen_find_device()
  smc_clc_send_accept()
                                  /* client receives ACCEPT, sends
                                   * CONFIRM + initial data + CDC
                                   * (prod.count > 0, cons.count == 0)
                                   */
                                  smc_cdc_msg_recv()
                                    bh_lock_sock(&smc->sk)
                                    smc_cdc_msg_recv_action()
                                      rx_ctrl.cons.count >=
                                        conn->peer_rmbe_size
                                      /* 0 >= 0 -> message dropped */
  smc_clc_wait_msg(CONFIRM)
  ...
  smc_conn_save_peer_info()
    conn->peer_rmbe_size = ...

Because delivered RDMA/ISM CDC messages are not retransmitted, dropping
this initial CDC message loses the producer cursor update (and any close
flags), leaving the server blocked in recvmsg().

>  	diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
> -				  &conn->local_rx_ctrl.cons);
> +				  &rx_ctrl.cons);
> +	if (diff_cons > conn->peer_rmbe_size - atomic_read(&conn->peer_rmbe_space))
> +		return;
> +
> +	if (diff_cons && conn->lgr->is_smcd &&
> +	    smc_ism_support_dmb_nocopy(conn->lgr->smcd)) {
> +		if (rx_ctrl.cons.count >= conn->sndbuf_desc->len)
> +			return;
> +		diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
> +					&conn->tx_curs_fin, &rx_ctrl.cons);
> +		if (diff_tx > conn->sndbuf_desc->len - atomic_read(&conn->sndbuf_space))
> +			return;
> +	}
> +
> +	diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
> +				  &rx_ctrl.prod);
> +	if (diff_prod > conn->rmb_desc->len - atomic_read(&conn->bytes_to_rcv))
> +		return;

[Severity: High]
Can a peer still advance conn->local_rx_ctrl.prod past
conn->local_tx_ctrl.cons because conn->bytes_to_rcv does not include
non-inline urgent bytes or in-flight spliced bytes?

When SOCK_URGINLINE is not set and rx_ctrl.prod_flags.urg_data_present is
set, smc_cdc_handle_urg_data_arrival() decrements diff_prod before
atomic_add(diff_prod, &conn->bytes_to_rcv), so each 1-byte urgent CDC
message advances conn->local_rx_ctrl.prod by 1 without increasing
conn->bytes_to_rcv.  Similarly, on splice() reads, smc_rx_recvmsg()
subtracts copylen from conn->bytes_to_rcv immediately while
conn->local_tx_ctrl.cons is not advanced until smc_rx_pipe_buf_release()
drops conn->splice_pending.  In both cases, checking only against
conn->rmb_desc->len - atomic_read(&conn->bytes_to_rcv) allows the peer to
wrap conn->local_rx_ctrl.prod past unconsumed data in the RMB.

> +
> +	conn->local_rx_ctrl.common.type = rx_ctrl.common.type;
> +	conn->local_rx_ctrl.len = rx_ctrl.len;
> +	conn->local_rx_ctrl.seqno = rx_ctrl.seqno;
> +	conn->local_rx_ctrl.token = rx_ctrl.token;
> +	smc_curs_copy(&conn->local_rx_ctrl.prod, &rx_ctrl.prod, conn);
> +	smc_curs_copy(&conn->local_rx_ctrl.cons, &rx_ctrl.cons, conn);

[Severity: Critical]
Can a peer bypass these delta checks by rewinding cursors on SMC-D or
jumping wrap counters on SMC-R and SMC-D?

On SMC-D, smcd_cdc_msg_to_host() copies peer->prod and peer->cons without
checking monotonicity.  If a peer sends new.wrap == old.wrap with
new.count < old.count, smc_curs_diff() clamps new->count - old->count to
0, which passes the upper-bound checks and commits the rewound cursor to
conn->local_rx_ctrl without decreasing bytes_to_rcv or peer_rmbe_space
(and on dmb_nocopy links, a subsequent small advance with diff_cons > 0
and diff_tx == 0 rewinds conn->tx_curs_fin as well).  Advancing the
cursor again then re-credits the same range and still inflates
bytes_to_rcv, peer_rmbe_space, and sndbuf_space past the buffer length.

In addition, when old->wrap != new->wrap, neither smc_curs_diff() nor
smc_cdc_cursor_to_host() verifies that new->wrap == (u16)(old->wrap + 1)
and new->count <= old->count.  A peer can jump rx_ctrl.cons.wrap while
keeping diff_cons within peer_rmbe_size - atomic_read(&conn->peer_rmbe_space),
leaving prod.wrap != cons.wrap in smc_tx_rdma_writes() when
prod.count + len > conn->peer_rmbe_size so that dst_len is not clamped to
conn->peer_rmbe_size - prod.count, causing an out-of-bounds write past
the peer RMBE/DMB.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/CA+OT+ON4Lt9mLvWPK8rUaHKZ01_p0fGCiCsWHwXWzT1sGaTtLA@mail.gmail.com?part=1

      parent reply	other threads:[~2026-11-25  3:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  3:21 [PATCH net] net/smc: validate peer CDC cursors against buffer bounds and window Qican Ma
2026-11-24  4:16 ` Dust Li
2026-11-24  7:28   ` Qican Ma
2026-11-25  3:21 ` sashiko-bot [this message]

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-232458@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=goonsomeway@gmail.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox