Linux s390 Architecture development
 help / color / mirror / Atom feed
* [PATCH net] net/smc: validate peer CDC cursors against buffer bounds and window
@ 2026-11-24  3:21 Qican Ma
  2026-11-24  4:16 ` Dust Li
  2026-11-25  3:21 ` sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Qican Ma @ 2026-11-24  3:21 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, horms, linux-rdma,
	linux-s390, netdev

smc_cdc_cursor_to_host() only checks cursor monotonicity and
smcd_cdc_msg_to_host() copies peer cursors without any validation. Neither
verifies that prod.count < conn->rmb_desc->len or
cons.count < conn->peer_rmbe_size, nor that the resulting cursor deltas
stay within available buffer capacity. A malformed or malicious peer CDC
message can therefore:

1. Drive conn->urg_curs.count past conn->rmb_desc->len in
   smc_cdc_handle_urg_data_arrival(), causing an out-of-bounds read from
   conn->rmb_desc->cpu_addr exposed via recvmsg(MSG_OOB).
2. Inflate conn->bytes_to_rcv past conn->rmb_desc->len (either via an
   out-of-range prod.count or repeated wrap increments), causing the
   second chunk iteration in smc_rx_recvmsg() to copy past the end of
   rcvbuf_base.
3. On SMC-D dmb_nocopy links, inflate conn->sndbuf_space past
   conn->sndbuf_desc->len, causing the second chunk iteration in
   smc_tx_sendmsg() to write past the end of sndbuf_base.

Decode incoming CDC messages into a temporary smc_host_cdc_msg and verify
both cursor offsets and their deltas against rmb_desc->len,
peer_rmbe_size, and (for dmb_nocopy) sndbuf_desc->len before committing
the update to conn->local_rx_ctrl.

Found with an LLM-assisted code review tool and verified by inspection on
v7.4-rc4 (b6a9fa34ce4). Build-tested only; not runtime-tested due to lack
of an SMC-R/SMC-D setup.

Fixes: 5f08318f617b ("smc: connection data control (CDC)")
Cc: stable@vger.kernel.org
Signed-off-by: Qican Ma <goonsomeway@gmail.com>
---
 net/smc/smc_cdc.c | 46 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 9 deletions(-)

diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
index 32d6d03df32..fd5e296e8be 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;

 	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;
+
+	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);
+	conn->local_rx_ctrl.prod_flags = rx_ctrl.prod_flags;
+	conn->local_rx_ctrl.conn_state_flags = rx_ctrl.conn_state_flags;
+
 	if (diff_cons) {
 		/* peer_rmbe_space is decreased during data transfer with RDMA
 		 * write
@@ -355,12 +389,6 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
 		 */
 		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);
 			/* increase local sndbuf space and fin_curs */
 			smp_mb__before_atomic();
 			atomic_add(diff_tx, &conn->sndbuf_space);
-- 
2.50.1 (Apple Git-155)

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

* Re: [PATCH net] net/smc: validate peer CDC cursors against buffer bounds and window
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Dust Li @ 2026-11-24  4:16 UTC (permalink / raw)
  To: Qican Ma, alibuda, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, horms, linux-rdma,
	linux-s390, netdev

On 2026-11-23 19:21:01, Qican Ma wrote:
>smc_cdc_cursor_to_host() only checks cursor monotonicity and
>smcd_cdc_msg_to_host() copies peer cursors without any validation. Neither
>verifies that prod.count < conn->rmb_desc->len or
>cons.count < conn->peer_rmbe_size, nor that the resulting cursor deltas
>stay within available buffer capacity. A malformed or malicious peer CDC
>message can therefore:
>
>1. Drive conn->urg_curs.count past conn->rmb_desc->len in
>   smc_cdc_handle_urg_data_arrival(), causing an out-of-bounds read from
>   conn->rmb_desc->cpu_addr exposed via recvmsg(MSG_OOB).
>2. Inflate conn->bytes_to_rcv past conn->rmb_desc->len (either via an
>   out-of-range prod.count or repeated wrap increments), causing the
>   second chunk iteration in smc_rx_recvmsg() to copy past the end of
>   rcvbuf_base.
>3. On SMC-D dmb_nocopy links, inflate conn->sndbuf_space past
>   conn->sndbuf_desc->len, causing the second chunk iteration in
>   smc_tx_sendmsg() to write past the end of sndbuf_base.
>
>Decode incoming CDC messages into a temporary smc_host_cdc_msg and verify
>both cursor offsets and their deltas against rmb_desc->len,
>peer_rmbe_size, and (for dmb_nocopy) sndbuf_desc->len before committing
>the update to conn->local_rx_ctrl.

Have you checked this patch ?
https://lore.kernel.org/netdev/20261112-b4-disp-0287c16e-v9-0-e5e8fa688177@proton.me

Best regards,
Dust

>
>Found with an LLM-assisted code review tool and verified by inspection on
>v7.4-rc4 (b6a9fa34ce4). Build-tested only; not runtime-tested due to lack
>of an SMC-R/SMC-D setup.

You can test using SMC-R & rxe + veth

Best regards,
Dust


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

* Re: [PATCH net] net/smc: validate peer CDC cursors against buffer bounds and window
  2026-11-24  4:16 ` Dust Li
@ 2026-11-24  7:28   ` Qican Ma
  0 siblings, 0 replies; 4+ messages in thread
From: Qican Ma @ 2026-11-24  7:28 UTC (permalink / raw)
  To: dust.li, alibuda, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, horms, linux-rdma,
	linux-s390, netdev

Hi Dust,

Thank you for the pointer and the testing tip!

I checked Bryam Vargas's v9 series (specifically patches 1/6, 2/6 and
3/6) and confirmed that it covers the same cursor and accumulator bound
issues completely. Please disregard this patch as superseded by his
series.

Best regards,
Qican Ma

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

* Re: [PATCH net] net/smc: validate peer CDC cursors against buffer bounds and window
  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-25  3:21 ` sashiko-bot
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-25  3:21 UTC (permalink / raw)
  To: Qican Ma
  Cc: linux-rdma, linux-s390, Heiko Carstens, Vasily Gorbik,
	Alexander Gordeev, Christian Borntraeger

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

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

end of thread, other threads:[~2026-11-25  3:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox