net/smc: validate peer CDC cursor against RMBE size before accepting it
Subsystems (6)
PATCH v2 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 stays 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 and a single advance can be at most one bufferful; the wire cursor is peer-controlled and was never checked against either.
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 the advance without clamping against size. A peer can inflate it two ways: an out-of-range prod.count (e.g. 0x7fffffff), or -- since on a wrap increment smc_curs_diff() returns (size - old.count) + new.count -- a cursor with a bumped wrap and new.count above old.count. Either 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.
Reject the bad cursor at ingress, mirroring the cursor-sanity idiom two lines above: drop any incoming cursor whose count is outside the buffer (>= size), and any whose advance from the last accepted cursor exceeds one bufferful (smc_curs_diff() > size), which catches the wrap case. 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, bounding 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.
---
v2: also bound the cursor *advance* (smc_curs_diff() <= size), not just
the count. A wrap increment with new.count above old.count passes the count check yet still inflates bytes_to_rcv past the buffer -- thanks to sashiko-bot for spotting the count-only gap. Resent standalone.
net/smc/smc_cdc.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
net/smc/smc_cdc.h +20 -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,21 @@ 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 ; +/* Bound the advance too: a wrap increment with temp.count above +* old.count makes smc_curs_diff() report more than one bufferful, +* which would inflate bytes_to_rcv / peer_rmbe_space past the buffer +* in smc_cdc_msg_recv_action(). Reject such a cursor. +*/ +if (smc_curs_diff (size, &old, &temp) > size) +return ;smc_curs_copy (local, &temp, conn); }
@@ -246,8 +262,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 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [Critical] The patch bounds the cursor advance of a single CDC message but fails to limit the cumulative accumulation of bytes_to_rcv across multiple messages. An attacker can repeatedly advance the cursor to inflate bytes_to_rcv beyond the buffer limits, resulting in the same out-of-bounds kernel memory read the patch attempted to fix.
- [Critical] The exact same remote kernel memory disclosure vulnerability remains unfixed for SMC-D connections, as smcd_cdc_msg_to_host() skips cursor validation.
- [High] Incoming CDC messages can be processed before connection buffers are allocated, leading to a NULL pointer dereference on conn->rmb_desc.
- [High] On 32-bit systems, reading the 64-bit cursor from the SMC-D shared DMA memory is protected only by a local OS spinlock, failing to prevent torn reads against concurrent hardware modifications by the peer.
--
--- Patch [1]: [PATCH net v2] net/smc: validate peer CDC cursor against RMBE size before accepting it --- commit 7b6c0d41f37fa23577369a83f25810b1711838f7
Author: Ibrahim Hashimov <[email protected]>
net/smc: validate peer CDC cursor against RMBE size before accepting it
This commit adds boundary checks to SMC-R CDC cursors at ingress to prevent out-of-bounds reads. It introduces a size parameter to smc_cdc_cursor_to_host() to reject cursors that exceed the buffer limits or advance more than one buffer size.
Quoted 4 lines
net/smc/smc_cdc.h +0
-0
[ ... ]
Quoted 9 lines
net/smc/smc_cdc.h +5
-0@@ -235,6 +236,21 @@ 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
net/smc/smc_cdc.h static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local, · line 253
Quoted 10 lines
net/smc/smc_cdc.h +10
-0+*/ +if (temp.count >= size) +return ; +/* Bound the advance too: a wrap increment with temp.count above +* old.count makes smc_curs_diff() report more than one bufferful, +* which would inflate bytes_to_rcv / peer_rmbe_space past the buffer +* in smc_cdc_msg_recv_action(). Reject such a cursor. +*/ +if (smc_curs_diff (size, &old, &temp) > size) +return ;
[Severity: Critical]
This is a pre-existing issue, but does this security check miss the stateful cumulative buildup of the cursor?
By only validating single-message advances, could an attacker repeatedly advance the cursor to inflate bytes_to_rcv beyond the buffer limits?
This could result in an out-of-bounds read if a malicious peer sends multiple valid CDC messages that each advance the cursor within the single-message bounds. These advances would continually add to bytes_to_rcv without clamping to rmb_desc->len in smc_cdc_msg_recv_action():
smc_cdc_msg_recv_action() {
...
atomic_add(diff_prod, &conn->bytes_to_rcv);
...
}A subsequent user-space read via smc_rx_recvmsg() could then use this inflated bytes_to_rcv value to read past the boundaries of the buffer:
smc_rx_recvmsg() {
...
chunk_len = copylen - chunk_len; /* remainder */
chunk_len_sum += chunk_len;
chunk_off = 0; /* modulo offset in recv ring buffer */
...
}net/smc/smc_cdc.h static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local, · line 266
Quoted 11 lines
net/smc/smc_cdc.h +2
-2smc_curs_copy (local, &temp, conn); }@@ -246,8 +262,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 could this unconditional dereference of conn->rmb_desc->len cause a kernel panic if rmb_desc is NULL?
If an attacker on an existing link group anticipates the predictable alert_token_local and sends a matching CDC message right after smc_lgr_register_conn() exposes the connection, the RX tasklet might access conn->rmb_desc->len in smcr_cdc_msg_to_host() before smc_buf_create() has allocated the buffer descriptors.
[Severity: Critical]
This is a pre-existing issue, but are SMC-D connections vulnerable to the same remote kernel memory disclosure?
In smcd_cdc_msg_to_host(), the untrusted SMC-D cursor values appear to be directly copied without applying the boundary checks introduced here for SMC-R:
smcd_cdc_msg_to_host() {
...
temp.wrap = peer->prod.wrap;
temp.count = peer->prod.count;
smc_curs_copy(&local->prod, &temp, conn);
...
}Could a malicious peer over SMC-D (ISM) send a CDC message with an inflated cursor, bypassing standard sanity checks and calculating arbitrary differentials via smc_curs_diff() to inflate bytes_to_rcv?
[Severity: High]
This is a pre-existing issue, but does the spinlock in smcd_curs_copy() actually protect against concurrent hardware DMA or cross-LPAR shared memory modifications?
smcd_curs_copy() {
...
spin_lock_irqsave(&conn->acurs_lock, flags);
tgt->acurs = src->acurs;
spin_unlock_irqrestore(&conn->acurs_lock, flags);
...
}On 32-bit systems (where KERNEL_HAS_ATOMIC64 is not defined), reading the 64-bit cursor from the SMC-D shared DMA memory is protected by a local OS spinlock.
Since the spinlock does not serialize with the peer's DMA/memory writes, could this lead to a torn read of the 64-bit cursor if the compiler emits two 32-bit loads that interleave with the peer's writes?
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
Paolo Abeni
maintainerQuoted 29 lines · Ibrahim Hashimov
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 stays 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 and a single advance can be at most one bufferful; the wire cursor is peer-controlled and was never checked against either.
smcr_cdc_msg_to_host()accepts the producer and consumer cursors, and the unbounded value feedssmc_curs_diff()insmc_cdc_msg_recv_action(), which computes the advance without clamping against size. A peer can inflate it two ways: an out-of-range prod.count (e.g.0x7fffffff), or -- since on a wrap incrementsmc_curs_diff()returns (size - old.count) + new.count -- a cursor with a bumped wrap and new.count above old.count. Either 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.
Quoted 10 lines
Reject the bad cursor at ingress, mirroring the cursor-sanity idiom two lines above: drop any incoming cursor whose count is outside the buffer (>= size), and any whose advance from the last accepted cursor exceeds one bufferful (
smc_curs_diff()> size), which catches the wrap case.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, bounding 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.
@SMC crew: please review.
The above looks so correlated and quite simple to address that possibly it makes sense to address it in the same series.
/P
D. Wythe
maintainerQuoted 81 lines · Ibrahim Hashimov
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 stays 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 and a single advance can be at most one bufferful; the wire cursor is peer-controlled and was never checked against either.
smcr_cdc_msg_to_host()accepts the producer and consumer cursors, and the unbounded value feedssmc_curs_diff()insmc_cdc_msg_recv_action(), which computes the advance without clamping against size. A peer can inflate it two ways: an out-of-range prod.count (e.g.0x7fffffff), or -- since on a wrap incrementsmc_curs_diff()returns (size - old.count) + new.count -- a cursor with a bumped wrap and new.count above old.count. Either 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.Reject the bad cursor at ingress, mirroring the cursor-sanity idiom two lines above: drop any incoming cursor whose count is outside the buffer (>= size), and any whose advance from the last accepted cursor exceeds one bufferful (
smc_curs_diff()> size), which catches the wrap case.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, bounding 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---
v2: also bound the cursor *advance* (smc_curs_diff()<= size), not just
the count. A wrap increment with new.count above old.count passes the count check yet still inflatesbytes_to_rcvpast the buffer -- thanks to sashiko-bot for spotting the count-only gap. Resent standalone.net/smc/smc_cdc.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)net/smc/smc_cdc.h +6
-0@@ -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,21 @@ 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
net/smc/smc_cdc.h static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local, · line 253
Quoted 10 lines
net/smc/smc_cdc.h +10
-0+*/ +if (temp.count >= size) +return ; +/* Bound the advance too: a wrap increment with temp.count above +* old.count makes smc_curs_diff() report more than one bufferful, +* which would inflate bytes_to_rcv / peer_rmbe_space past the buffer +* in smc_cdc_msg_recv_action(). Reject such a cursor. +*/ +if (smc_curs_diff (size, &old, &temp) > size) +return ;
At minimum there should be a rate-limited log indicating something unusual happened. Ideally the connection should be actively aborted, a peer sending an out-of-range cursor is either malicious, buggy, or the data is corrupted, and silently continuing is pointless.
Quoted 18 lines
net/smc/smc_cdc.h +4
-2smc_curs_copy (local, &temp, conn); }@@ -246,8 +262,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)
Dust Li
maintainerQuoted 41 lines · Paolo Abeni
Quoted 29 lines · Ibrahim Hashimov
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 stays 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 and a single advance can be at most one bufferful; the wire cursor is peer-controlled and was never checked against either.
smcr_cdc_msg_to_host()accepts the producer and consumer cursors, and the unbounded value feedssmc_curs_diff()insmc_cdc_msg_recv_action(), which computes the advance without clamping against size. A peer can inflate it two ways: an out-of-range prod.count (e.g.0x7fffffff), or -- since on a wrap incrementsmc_curs_diff()returns (size - old.count) + new.count -- a cursor with a bumped wrap and new.count above old.count. Either 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.Quoted 10 lines
Reject the bad cursor at ingress, mirroring the cursor-sanity idiom two lines above: drop any incoming cursor whose count is outside the buffer (>= size), and any whose advance from the last accepted cursor exceeds one bufferful (
smc_curs_diff()> size), which catches the wrap case.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, bounding 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.
Quoted 4 lines
@SMC crew: please review.
The above looks so correlated and quite simple to address that possibly it makes sense to address it in the same series.
Hi Paolo,
This patch was discussed in the v1 thread. Bryan had already sent a the v5 addressing the issue, so I think this patch can be dropped.
https://lore.kernel.org/netdev/[email protected]
Best regards,
Dust