[PATCH v2] crypto: scomp - Bound the single-page source scratch buffer
From: Fang Xieyan
Date: Fri Dec 04 2026 - 03:18:52 EST
When a non-virtual source scatterlist cannot be mapped directly,
scomp_acomp_comp_decomp() copies it into a per-CPU scratch buffer that
is exactly one page (alloc_pages_node(..., 0)) yet passes slen straight
to memcpy_from_sglist() without checking that size, so a multi-entry
source totalling more than a page overruns the scratch and corrupts
adjacent kernel memory.
Restore the source limit dropped when the scratch was shrunk to a
single page: at the top of the function, reject a non-virtual source
longer than a page with -ENOSYS, mirroring the destination side.
Link: https://lore.kernel.org/all/20261128145056.78807-1-fangxy@xxxxxxxxxxxx/
Fixes: 2d3553ecb4e3 ("crypto: scomp - Remove support for some non-trivial SG lists")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Fang Xieyan <fangxy@xxxxxxxxxxxx>
---
Changes since v1:
- Moved the bound to the top of the function as a single explicit
contract, dropped the mid-function bail (and the added out: label
and goto), and used -ENOSYS to match the destination-side limit.
Boundary analysis:
- src_isvirt is true for DMA/non-DMA virtual sources; those take
src = req->svirt and never enter the "!src_isvirt && !src" scratch
branch, so bounding only the non-virtual path cannot reject them.
req->src and req->svirt share a union, so the existing
"!req->src" null check still passes for them.
- Hibernation (kernel/power/swap.c) is the concrete caller that must
stay served: non-DMA virtual sources of up to
bytes_worst_compress(UNC_SIZE) (139333 bytes on 4 KiB pages) run
through req->svirt, so !src_isvirt leaves them untouched. zswap, an
SG decompression user, stays within entry->length <= PAGE_SIZE.
Testing:
- QEMU/KVM, KASAN probe build on d998a300b881 (7.3-rc7) with a
built-in lzo trigger and WARN_ON_ONCE(slen > PAGE_SIZE) at the
scratch copy. Baseline: a 2-entry SG source with len0=2048 and
slen=8192 warns at crypto/scompress.c:239 and still returns 0, i.e.
the one-page scratch is overrun silently.
- This patch: slen=4096 -> ret=0; slen=8192 -> ret=-38 (-ENOSYS) and
the probe no longer fires, so the copy is refused before it happens.
A single-entry SG that is directly mappable at slen=8192 goes from
ret=0 to ret=-38, pinning the SG contract where there is no
overflow. No new WARN/BUG/KASAN in dmesg.
- Hibernation shape, unaffected: non-DMA virtual source, compress
slen=131072 -> ret=0 (cmp_len=131590), decompress that back with
slen=131590 -> ret=0. Herbert's unconditional check turns the same
compress call into -ENOSYS, which is why the bound is scoped to
!src_isvirt. This is the acomp call shape, not a full
suspend/resume cycle.
crypto/scompress.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/crypto/scompress.c b/crypto/scompress.c
index de54227203ce..1c36141fcd40 100644
--- a/crypto/scompress.c
+++ b/crypto/scompress.c
@@ -186,6 +186,9 @@ static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
if (!req->dst || !dlen)
return -EINVAL;
+ if (!src_isvirt && slen > PAGE_SIZE)
+ return -ENOSYS;
+
if (dst_isvirt)
dst = req->dvirt;
else {
--
2.50.1