mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fang Xieyan <fangxy@xiaopeng.com>
To: herbert@gondor.apana.org.au, davem@davemloft.net
Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] crypto: scomp - Bound the single-page source scratch buffer
Date: Sat, 28 Nov 2026 22:50:56 +0800	[thread overview]
Message-ID: <20261128145056.78807-1-fangxy@xiaopeng.com> (raw)

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@vger.kernel.org
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
 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


                 reply	other threads:[~2026-11-28 14:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128145056.78807-1-fangxy@xiaopeng.com \
    --to=fangxy@xiaopeng.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®