net/smc: validate peer CDC cursor against RMBE size before accepting it
Subsystems (6)
PATCH 1/1 net/smc: validate peer CDC cursor against RMBE size before accepting it
smc_cdc_cursor_to_host() converts the wire-format producer/consumer cursor of an incoming CDC message into a host smc_host_cursor. It rejects a cursor that goes backwards, but never checks that the cursor's count is inside the buffer it indexes. Per smc_host_cursor ("an offset in an RMBE") and the invariant smc_curs_add() enforces for every local advance (0 <= count < size), a valid cursor count must be < size; the wire cursor is peer-controlled and was never checked against that.
smcr_cdc_msg_to_host() accepts the producer and consumer cursors, and the unbounded value feeds smc_curs_diff() in smc_cdc_msg_recv_action(), which computes new->count - old->count without clamping against size. A peer that puts an out-of-range prod.count on the wire (e.g. 0x7fffffff) drives bytes_to_rcv far past rmb_desc->len -- the very invariant the comment above the atomic_add() claims but does not enforce.
smc_rx_recvmsg() then trusts bytes_to_rcv as the amount of valid RMB data. Its first copy chunk is safely bounded by rmb_desc->len - cons.count, but the second chunk copies (copylen - chunk_len) bytes from offset 0, and copylen came from the inflated readable -- an out-of-bounds read of the RMB's backing (v)malloc allocation, copied straight to the receiving process via _copy_to_iter(). This is a remote kernel-memory disclosure driven by a malicious SMC-R peer, needing no local privilege on the victim. The same unbounded count also reaches smc_cdc_handle_urg_data_arrival() (base + urg_curs.count - 1) -- a second, narrower OOB read.
Fix it where the wire cursor is accepted, mirroring the cursor-sanity idiom two lines above: reject any incoming cursor whose count is >= the size of the buffer it will index. smc_cdc_cursor_to_host() gains a size parameter; smcr_cdc_msg_to_host() passes conn->rmb_desc->len for the producer cursor and conn->peer_rmbe_size for the consumer cursor. This bounds it at its single origin instead of patching every downstream consumer. SMC-D (smcd_cdc_msg_to_host()) does not use this helper and is a separate, out-of-scope gap.
Reproduced on a v6.19 KASAN kernel (SMC-Rv2 over soft-RoCE): a peer emitting prod.count=0x7fffffff triggers a vmalloc-out-of-bounds read in _copy_to_iter() via smc_rx_recvmsg(), returning ~960 KB of RMB-adjacent kernel heap to userspace; an in-range cursor transfers cleanly. With the fix the out-of-range cursor is dropped before it reaches conn->local_rx_ctrl.prod, and the reproducer returns rc=0 with no report.
--- net/smc/smc_cdc.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
net/smc/smc_cdc.h +13 -2
@@ -221,6 +221,7 @@ static inline void smc_host_msg_to_cdc(struct smc_cdc_msg *peer,static inline void smc_cdc_cursor_to_host (union smc_host_cursor *local,union smc_cdc_cursor *peer, +unsigned int size,struct smc_connection *conn) {union smc_host_cursor temp, old;
@@ -235,6 +236,14 @@ static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local,if ((old.wrap == temp.wrap) && (old.count > temp.count))return ; +/* count is an offset into the RMBE and must always stay inside +* it (see smc_curs_add()); the peer is untrusted, so reject an +* out-of-range wire cursor the same way an out-of-order one is +* already rejected above, instead of letting it drive +* bytes_to_rcv / the urgent-byte offset past the buffer end +*/ +if (temp.count >= size) +return ;smc_curs_copy (local, &temp, conn); }
@@ -246,8 +255,10 @@ static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local, local->len = peer->len; local->seqno =ntohs (peer->seqno); local->token =ntohl (peer->token); -smc_cdc_cursor_to_host (&local->prod, &peer->prod, conn); -smc_cdc_cursor_to_host (&local->cons, &peer->cons, conn); +smc_cdc_cursor_to_host (&local->prod, &peer->prod, + conn->rmb_desc->len, conn); +smc_cdc_cursor_to_host (&local->cons, &peer->cons, + conn->peer_rmbe_size, conn); local->prod_flags = peer->prod_flags; local->conn_state_flags = peer->conn_state_flags; }
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] The patch fails to prevent remote kernel memory disclosure because an attacker can bypass the bounds check by exploiting a flaw in the wrap counter validation, which allows infinite inflation of bytes_to_rcv.
- [High] The patch explicitly excludes SMC-D from cursor bounds checking, leaving it fully vulnerable to the original unbounded cursor out-of-bounds read and write attacks.
- [High] Incoming CDC messages processed during early connection establishment can trigger a NULL pointer dereference on conn->rmb_desc->len.
--
--- Patch [1]: [PATCH net] net/smc: validate peer CDC cursor against RMBE size before accepting it --- commit 2f986d59395ef38131fb12b9fd2f5e861bf4a2d9
Author: Ibrahim Hashimov <[email protected]>
net/smc: validate peer CDC cursor against RMBE size before accepting it
This commit validates incoming CDC message cursors against the receiving buffer size to prevent out-of-bounds reads/writes. It modifies smc_cdc_cursor_to_host() and smcr_cdc_msg_to_host() to reject counts exceeding the buffer length.
Quoted 2 lines
SMC-D (
smcd_cdc_msg_to_host()) does not use this helper and is a separate, out-of-scope gap.
[Severity: High]
This isn't a bug introduced by this patch, but does explicitly excluding SMC-D leave it vulnerable to the same unbounded cursor out-of-bounds read and write attacks?
If an untrusted SMC-D peer sends a CDC message with a massive cursor count (e.g. 0x7FFFFFFF), smcd_cdc_msg_to_host() blindly copies it without bounds checks. When smc_cdc_msg_recv_action() consumes it via smc_curs_diff(), it massively inflates bytes_to_rcv.
Would it be safer to apply similar validation for SMC-D connections to prevent arbitrary out-of-bounds reads in smc_rx_recvmsg()?
Quoted 4 lines
net/smc/smc_cdc.h +0
-0
[ ... ]
net/smc/smc_cdc.h static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local, · line 237
Quoted 3 lines
net/smc/smc_cdc.h +0
-0@@ -235,6 +236,14 @@ static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local,if ((old.wrap > temp.wrap) && temp.wrap)return ;
[Severity: Critical]
This is a pre-existing issue, but does this wrap check flaw allow a malicious peer to bypass the newly added bounds check entirely?
If a malicious SMC-R peer alternates sending CDC messages with wrap=1 and wrap=0, the check above evaluates to false when temp.wrap is 0. This improperly allows a backward wrap jump to be accepted.
Consequently, smc_curs_diff() will compute a massive positive difference when the wraps differ, which is then unconditionally added to conn->bytes_to_rcv in smc_cdc_msg_recv_action().
Could this inflation of bytes_to_rcv defeat the count bounds check and still lead to remote out-of-bounds memory disclosure?
Quoted 13 lines
net/smc/smc_cdc.h +8
-0if ((old.wrap == temp.wrap) && (old.count > temp.count))return ; +/* count is an offset into the RMBE and must always stay inside +* it (see smc_curs_add()); the peer is untrusted, so reject an +* out-of-range wire cursor the same way an out-of-order one is +* already rejected above, instead of letting it drive +* bytes_to_rcv / the urgent-byte offset past the buffer end +*/ +if (temp.count >= size) +return ;smc_curs_copy (local, &temp, conn);}
[ ... ]
net/smc/smc_cdc.h static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local, · line 259
Quoted 8 lines
net/smc/smc_cdc.h +2
-2@@ -246,8 +255,10 @@ static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local, local->len = peer->len; local->seqno =ntohs (peer->seqno); local->token =ntohl (peer->token); -smc_cdc_cursor_to_host (&local->prod, &peer->prod, conn); -smc_cdc_cursor_to_host (&local->cons, &peer->cons, conn); +smc_cdc_cursor_to_host (&local->prod, &peer->prod, + conn->rmb_desc->len, conn);
[Severity: High]
This isn't a bug introduced by this patch, but does directly dereferencing conn->rmb_desc here expose a NULL pointer dereference during early connection establishment?
In smc_conn_create(), a connection is made globally discoverable by adding it to lgr->conns_all before the underlying buffers (like conn->rmb_desc) are allocated:
smc_core.c:smc_conn_create() {
...
write_lock_bh(&lgr->conns_lock);
rc = smc_lgr_register_conn(conn, true);
write_unlock_bh(&lgr->conns_lock);
...
}If an attacker predicts the sequential atomic token and sends a CDC message during this race window, smc_cdc_rx_handler() finds the connection and proceeds to message processing.
Would this result in a kernel panic when smcr_cdc_msg_to_host() evaluates conn->rmb_desc->len before the buffer is initialized?
Quoted 5 lines
net/smc/smc_cdc.h +2
-0+smc_cdc_cursor_to_host (&local->cons, &peer->cons, + conn->peer_rmbe_size, conn); local->prod_flags = peer->prod_flags; local->conn_state_flags = peer->conn_state_flags; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Dust Li
maintainerOn 2026-07-20 19:07:17, Ibrahim Hashimov wrote:
Hi Ibrahim,
I believe Bryam already sent a similar patch to the mailist and I have already reviewed it.
https://lore.kernel.org/netdev/[email protected]/
Best regards,
Dust
Quoted 92 lines
smc_cdc_cursor_to_host()converts the wire-format producer/consumer cursor of an incoming CDC message into a hostsmc_host_cursor. It rejects a cursor that goes backwards, but never checks that the cursor's count is inside the buffer it indexes. Persmc_host_cursor("an offset in an RMBE") and the invariantsmc_curs_add()enforces for every local advance (0 <= count < size), a valid cursor count must be < size; the wire cursor is peer-controlled and was never checked against that.
smcr_cdc_msg_to_host()accepts the producer and consumer cursors, and the unbounded value feedssmc_curs_diff()insmc_cdc_msg_recv_action(), which computesnew->count-old->countwithout clamping against size. A peer that puts an out-of-range prod.count on the wire (e.g.0x7fffffff) drivesbytes_to_rcvfar pastrmb_desc->len-- the very invariant the comment above theatomic_add()claims but does not enforce.
smc_rx_recvmsg()then trustsbytes_to_rcvas the amount of valid RMB data. Its first copy chunk is safely bounded byrmb_desc->len- cons.count, but the second chunk copies (copylen -chunk_len) bytes from offset 0, and copylen came from the inflated readable -- an out-of-bounds read of the RMB's backing (v)malloc allocation, copied straight to the receiving process via_copy_to_iter(). This is a remote kernel-memory disclosure driven by a malicious SMC-R peer, needing no local privilege on the victim. The same unbounded count also reachessmc_cdc_handle_urg_data_arrival()(base + urg_curs.count - 1) -- a second, narrower OOB read.Fix it where the wire cursor is accepted, mirroring the cursor-sanity idiom two lines above: reject any incoming cursor whose count is >= the size of the buffer it will index.
smc_cdc_cursor_to_host()gains a size parameter;smcr_cdc_msg_to_host()passesconn->rmb_desc->lenfor the producer cursor andconn->peer_rmbe_sizefor the consumer cursor. This bounds it at its single origin instead of patching every downstream consumer. SMC-D (smcd_cdc_msg_to_host()) does not use this helper and is a separate, out-of-scope gap.Reproduced on a v6.19 KASAN kernel (SMC-Rv2 over soft-RoCE): a peer emitting prod.count=0x7fffffff triggers a vmalloc-out-of-bounds read in
_copy_to_iter()viasmc_rx_recvmsg(), returning ~960 KB of RMB-adjacent kernel heap to userspace; an in-range cursor transfers cleanly. With the fix the out-of-range cursor is dropped before it reachesconn->local_rx_ctrl.prod, and the reproducer returns rc=0 with no report.Fixes 5f08318f617b ("smc: connection data control (CDC)")Signed-off-by Ibrahim Hashimov <[email protected]>Assisted-by AuditCode-AI:2026.07---
net/smc/smc_cdc.h| 15 +++++++++++++--1 file changed, 13 insertions(+), 2 deletions(-)net/smc/smc_cdc.h +13
-2@@ -221,6 +221,7 @@ static inline void smc_host_msg_to_cdc(struct smc_cdc_msg *peer,static inline void smc_cdc_cursor_to_host (union smc_host_cursor *local,union smc_cdc_cursor *peer, +unsigned int size,struct smc_connection *conn) {union smc_host_cursor temp, old;@@ -235,6 +236,14 @@ static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local,if ((old.wrap == temp.wrap) && (old.count > temp.count))return ; +/* count is an offset into the RMBE and must always stay inside +* it (see smc_curs_add()); the peer is untrusted, so reject an +* out-of-range wire cursor the same way an out-of-order one is +* already rejected above, instead of letting it drive +* bytes_to_rcv / the urgent-byte offset past the buffer end +*/ +if (temp.count >= size) +return ;smc_curs_copy (local, &temp, conn); }@@ -246,8 +255,10 @@ static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local, local->len = peer->len; local->seqno =ntohs (peer->seqno); local->token =ntohl (peer->token); -smc_cdc_cursor_to_host (&local->prod, &peer->prod, conn); -smc_cdc_cursor_to_host (&local->cons, &peer->cons, conn); +smc_cdc_cursor_to_host (&local->prod, &peer->prod, + conn->rmb_desc->len, conn); +smc_cdc_cursor_to_host (&local->cons, &peer->cons, + conn->peer_rmbe_size, conn); local->prod_flags = peer->prod_flags; local->conn_state_flags = peer->conn_state_flags; }Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 3 lines
I believe Bryam already sent a similar patch to the mailist and I have already reviewed it.
https://lore.kernel.org/netdev/[email protected]/
Thanks Dust -- yes, that fixes the same bug, so please drop mine. (I sent a v2 earlier today, before I saw your note, in reply to a sashiko review; please disregard it as a competing patch.)
One thing worth checking on Bryam's series, though: bounding only the cursor count (clamping temp.count to rmb_desc->len) still leaves the advance unbounded. smc_curs_diff() on a wrap increment returns
(size - old.count) + new.count, so a peer that sends prod (wrap=W, count=0) then prod (wrap=W+1, count=size) makes diff_prod ~= 2*size even though both counts are in range. smc_cdc_msg_recv_action() then atomic_add()s that into bytes_to_rcv without clamping (despite the "0 <= bytes_to_rcv <= rmb_desc->len" comment) -- which is exactly what smc_rx_recvmsg()'s second copy chunk trusts.
A sashiko review of my patch flagged the same gap; I bounded the advance too, with:
if (smc_curs_diff(size, &old, &temp) > size) return;
Might be worth folding into Bryam's version. Happy to send it as a follow-up if that helps.
Thanks,
Ibrahim
Agreed, and thanks for dropping the competing one.
Patch 1/3 bounds only the cursor count, not the advance: smc_curs_diff()'s differing-wrap branch returns (len - old.count) + new.count, so prod(W,0) then prod(W+1,len) is diff_prod == 2*len with both counts in range, and smc_cdc_msg_recv_action() adds that to bytes_to_rcv unclamped. wrap++/count==0 does the same over several CDCs, so a per-cursor bound can't catch it.
The out-of-bounds copy you point at is closed by patch 2/3, though. It bounds the derived readable length at the consumer:
if (readable < 0 || readable > conn->rmb_desc->len) readable = conn->rmb_desc->len;
so copylen <= rmb_desc->len and smc_rx_recvmsg()'s second chunk (copylen - first, offset 0) stays within the ring no matter how large bytes_to_rcv grew. I ran your wrap++/count=0 vector end to end on the real SMC-D path under KASAN: with 1/3 alone it trips slab-out-of-bounds in smc_rx_recvmsg (bytes_to_rcv reaches 6*len, read of size 5*len); with 2/3 applied recv() is bounded to rmb_desc->len and it is clean.
What 2/3 does not fix is the accounting: bytes_to_rcv is left > rmb_desc->len (and can sign-overflow negative over many messages), so the documented
0 <= bytes_to_rcv <= rmb_desc->len invariant is still violated at the producer even though the copy is now bounded. I have a net-next follow-up that records and aborts on that. Your
if (smc_curs_diff(size, &old, &temp) > size) return;
catches the single-CDC 2*len advance cleanly; the wrap++/count==0 form slips it, since each CDC advances exactly len (diff == len, not > len) and it is the accumulated bytes_to_rcv that overruns. So I'll pair your advance-bound as an early catch with a check on bytes_to_rcv after the atomic_add and abort+record there, which covers both. The insight is yours either way -- may I add your Suggested-by (or Co-developed-by)?
The v4 series otherwise stands as reviewed; I'll repost it for the netdev queue and keep the accounting bound as the net-next follow-up.
Thanks,
Bryam
Quoted 2 lines
The insight is yours either way -- may I add your Suggested-by (or Co-developed-by)?
Please do, and thanks for running it end to end. Suggested-by is the honest one -- I pointed at the gap and the advance-bound, you did the series. If you carry the
if (smc_curs_diff(size, &old, &temp) > size) return;
verbatim, Co-developed-by is fine too, your call. Either way:
Ibrahim Hashimov <[email protected]>
And your split is right -- advance-bound as the early catch, bytes_to_rcv after the atomic_add for the accumulation the per-cursor check can't see.
Thanks,
Ibrahim
Hi Bryam,
I have a standalone net-next patch that aborts the connection when bytes_to_rcv + diff_prod exceeds rmb_desc->len. The check sits before the atomic_add(), so the accumulator is never written with an out-of-range value:
if (atomic_read(&conn->bytes_to_rcv) + diff_prod > conn->rmb_desc->len) { smc_cdc_abort_conn(smc, NULL); return;
}
smp_mb__before_atomic(); atomic_add(diff_prod, &conn->bytes_to_rcv);
The patch also moves the open-coded abort sequence in smc_cdc_msg_validate() into a new smc_cdc_abort_conn() helper, which the overflow check above then reuses.
It is currently in internal review. I will post it to netdev shortly.
Thanks,
Hidayath
Quoted 42 lines · Bryam Vargas
Agreed, and thanks for dropping the competing one.
Patch 1/3 bounds only the cursor count, not the advance: smc_curs_diff()'s differing-wrap branch returns (len - old.count) + new.count, so
prod(W,0)thenprod(W+1,len)isdiff_prod== 2*len with both counts in range, andsmc_cdc_msg_recv_action()adds that tobytes_to_rcvunclamped. wrap++/count==0 does the same over several CDCs, so a per-cursor bound can't catch it.The out-of-bounds copy you point at is closed by patch 2/3, though. It bounds the derived readable length at the consumer:
if (readable < 0 || readable > conn->rmb_desc->len) readable = conn->rmb_desc->len;so copylen <=
rmb_desc->lenand smc_rx_recvmsg()'s second chunk (copylen - first, offset 0) stays within the ring no matter how largebytes_to_rcvgrew. I ran your wrap++/count=0 vector end to end on the real SMC-D path under KASAN: with 1/3 alone it trips slab-out-of-bounds insmc_rx_recvmsg(bytes_to_rcvreaches 6*len, read of size 5*len); with 2/3 appliedrecv()is bounded tormb_desc->lenand it is clean.What 2/3 does not fix is the accounting:
bytes_to_rcvis left >rmb_desc->len(and can sign-overflow negative over many messages), so the documented
0 <=bytes_to_rcv<=rmb_desc->leninvariant is still violated at the producer even though the copy is now bounded. I have a net-next follow-up that records and aborts on that. Yourif (smc_curs_diff(size, &old, &temp) > size) return;catches the single-CDC 2*len advance cleanly; the wrap++/count==0 form slips it, since each CDC advances exactly len (diff == len, not > len) and it is the accumulated
bytes_to_rcvthat overruns. So I'll pair your advance-bound as an early catch with a check onbytes_to_rcvafter theatomic_addand abort+record there, which covers both. The insight is yours either way -- may I add your Suggested-by (or Co-developed-by)?The v4 series otherwise stands as reviewed; I'll repost it for the netdev queue and keep the accounting bound as the net-next follow-up.
Thanks,
Bryam