smc_tx_sendmsg() decides how much to copy from the sendmsg() iterator with writespace = atomic_read(&conn->sndbuf_space); copylen = min_t(size_t, send_remaining, writespace); and then splits the copy into the two chunks of the ring. The first chunk is clamped against the buffer: chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len - tx_cnt_prep); but the second is not - it is simply the remainder: chunk_len = copylen - chunk_len; /* remainder */ chunk_off = 0; and each chunk is written with memcpy_from_msg(sndbuf_base + chunk_off, msg, chunk_len). So the write is bounded only by sndbuf_space. That counter is not always a value this side computed. On the SMC-D DMB-nocopy path smc_cdc_msg_recv_action() credits it from the peer's consumer cursor: diff_tx = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin, &conn->local_rx_ctrl.cons); smp_mb__before_atomic(); atomic_add(diff_tx, &conn->sndbuf_space); /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */ The comment states the invariant that smc_tx_sendmsg() relies on; the code does not establish it, because local_rx_ctrl.cons arrives from the peer. A peer that reports an oversized consumer cursor therefore inflates sndbuf_space, and the unclamped second chunk writes sendmsg() data past the end of the send buffer - which under DMB-nocopy is the DMB the peer published, so the overflow runs off the end of that mapping. Clamp copylen to the buffer. This bounds the write regardless of how the credit was arrived at, which is the property smc_tx_sendmsg() needs and the one a reader of the second chunk currently has to take on trust. A correct peer never credits more than the buffer holds, so this changes nothing for one. Capping the credit itself at the atomic_add() would also enforce the stated invariant, and we would be glad to send that instead or as well; we chose the clamp because it bounds the copy at the point the copy is made. Reproduced. A hostile peer needs an ISM device we do not have, so the peer was stood in for by a debug patch that overrides the consumer cursor after validation, on an SMC-D connection over the loopback-ISM device. The credit then reads SMC2DBG diff_tx=1000000 space_before=65504 sndlen=65504 cons=1000000 - sndbuf_space 1065504 on a 65504-byte buffer - and one sendmsg() of 4 MB turns that into an oversized copy, with the second chunk written at offset 0: SMCTXDBG chunk=0 off=0 len=65504 sndlen=65504 in bounds SMCTXDBG chunk=1 off=0 len=869056 sndlen=65504 past the end BUG: KASAN: use-after-free in _copy_from_iter+0x197/0xfd0 Write of size 869056 at addr ffff888057980020 by task b/9467 _copy_from_iter+0x197/0xfd0 smc_tx_sendmsg+0x1011/0x12c0 With this patch applied and the same injected credit, every chunk stays within the buffer (len 65504 or less, no second chunk past the end), KASAN is silent, and an 8 MB transfer over the same connection completes with its contents verified. The injected cursor is the only thing the debug patch changes; the fault and the fix are in the code above. How this was found: by AI-driven fuzzing built on top of syzkaller, while it was working on the receive-side cursor bugs fixed by "net/smc: validate the peer's CDC cursors against our own buffers". Its analysis was then checked by hand against the code, and the credit inflation above measured. Signed-off-by: Kapil Vaswani Signed-off-by: Piyus Kedia --- net/smc/smc_tx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c index 0f0c6df3b..5dc489bf6 100644 --- a/net/smc/smc_tx.c +++ b/net/smc/smc_tx.c @@ -235,6 +235,13 @@ int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len) writespace = atomic_read(&conn->sndbuf_space); /* not more than what user space asked for */ copylen = min_t(size_t, send_remaining, writespace); + /* and never more than the send buffer holds. sndbuf_space is + * credited in smc_cdc_msg_recv_action() from the peer's + * consumer cursor on the SMC-D DMB-nocopy path, so it is not a + * value this side computed, and the second chunk below is not + * clamped against the buffer the way the first one is. + */ + copylen = min_t(size_t, copylen, conn->sndbuf_desc->len); /* determine start of sndbuf */ sndbuf_base = conn->sndbuf_desc->cpu_addr; smc_curs_copy(&prep, &conn->tx_curs_prep, conn);