[PATCH] crypto: scomp - Bound the single-page source scratch buffer

From: Fang Xieyan

Date: Sat Nov 28 2026 - 09:51:21 EST


scomp_acomp_comp_decomp() linearises a non-trivial source scatterlist into
a per-CPU scratch buffer that is exactly one page in size, because the buffer
is allocated with alloc_pages_node(..., 0). The destination side bails out
with -ENOSYS on non-trivial SG lists, but the source side hands slen straight
to memcpy_from_sglist() with no bound at all.

When a request's source spans several SG entries whose total length exceeds
PAGE_SIZE and the source cannot be mapped directly, memcpy_from_sglist()
copies slen bytes into the one-page scratch buffer and writes past its end.
This is an out-of-bounds write into the adjacent direct-mapped page: the
overflow is silent, the compress returns success, and neighbouring kernel
memory is corrupted.

Reject scratch-path requests whose slen exceeds the buffer size with -EINVAL,
and perform the check before taking the per-CPU scratch lock so a request we
can never serve does not take that lock. Commit 2d3553ecb4e3 shrank the source
scratch to a single page and dropped the old SCOMP_SCRATCH_SIZE bound along
with it; this restores a length check that matches the buffer actually
allocated. Only requests that genuinely need the scratch buffer are affected:
a directly mappable source, however long, still bypasses the copy as before.

Fixes: 2d3553ecb4e3 ("crypto: scomp - Remove support for some non-trivial SG lists")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Fang Xieyan <fangxy@xxxxxxxxxxxx>
---
Reproduction / validation (evidence, not part of the commit message):

Env: v7.3-rc7 (d998a300b881), CONFIG_KASAN=y (generic),
QEMU 8.2.2 with KVM. The scratch buffer is allocated directly from
the page allocator, which does not add a kmalloc-style
allocation-boundary redzone; generic KASAN therefore does not
necessarily detect this overflow. In our reproduction no KASAN
reports were observed across runs, so the deterministic evidence is a
WARN_ON_ONCE(slen > PAGE_SIZE) probe plus a synthetic caller that
forces the scratch path (non-virt, multi-entry SG, slen > PAGE_SIZE),
which the real in-tree callers do not hit with an overflow on x86_64
(IPComp linearises skb->len > PAGE_SIZE into one directly-mappable
entry and bypasses the copy).

Unpatched baseline (probe build):
WARNING: crypto/scompress.c:239 at scomp_acomp_comp_decomp+0x66b/0x7f0
F077PROBE slen=8192 PAGE_SIZE=4096 scratch=ff11000004c92000
F077TRIG compress ret=0 slen=8192 # overflow "succeeded", silent
(grep -c 'BUG: KASAN' = 0 -> no sanitizer report)

Fixed (this patch), same synthetic trigger:
F077TRIG accept slen=4096 ret=0 # scratch path still served
F077TRIG reject slen=8192 ret=-22 # -EINVAL, checked before the lock
crypto/scompress.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/crypto/scompress.c b/crypto/scompress.c
index de54227203ce..02438d8f1751 100644
--- a/crypto/scompress.c
+++ b/crypto/scompress.c
@@ -232,8 +232,16 @@ static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
struct crypto_acomp_stream *stream = crypto_acomp_lock_stream_bh(&crypto_scomp_alg(scomp)->streams);

if (!src_isvirt && !src) {
- struct scomp_scratch *scratch = scomp_lock_scratch();
- const u8 *src = scratch->src;
+ struct scomp_scratch *scratch;
+ const u8 *src;
+
+ if (slen > PAGE_SIZE) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ scratch = scomp_lock_scratch();
+ src = scratch->src;

memcpy_from_sglist(scratch->src, req->src, 0, slen);

@@ -252,6 +260,7 @@ static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
ret = crypto_scomp_decompress(scomp, src, slen,
dst, &dlen, stream->ctx);

+out:
crypto_acomp_unlock_stream_bh(stream);

req->dlen = dlen;
--
2.50.1