[PATCH] crypto: zstd - Fix compression bug caused by truncation

Herbert Xu posted 1 patch 2 months, 2 weeks ago
[PATCH] crypto: zstd - Fix compression bug caused by truncation
Posted by Herbert Xu 2 months, 2 weeks ago
On Mon, Sep 29, 2025 at 11:51:36PM +0000, Han Xu wrote:
> Hi Suman,
> 
> The patch f5ad93ffb5411 "crypto: zstd - convert to acomp"
> leads to the following kernel dump during UBIFS write back.

Thanks for the detailed report and instructions!

Please let me know if you still get the crash with this patch:

---8<---
Use size_t for the return value of zstd_compress_cctx as otherwise
negative errors will be truncated to a positive value.

Reported-by: Han Xu <han.xu@nxp.com>
Fixes: f5ad93ffb541 ("crypto: zstd - convert to acomp")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/crypto/zstd.c b/crypto/zstd.c
index c2a19cb0879d..ac318d333b68 100644
--- a/crypto/zstd.c
+++ b/crypto/zstd.c
@@ -83,7 +83,7 @@ static void zstd_exit(struct crypto_acomp *acomp_tfm)
 static int zstd_compress_one(struct acomp_req *req, struct zstd_ctx *ctx,
 			     const void *src, void *dst, unsigned int *dlen)
 {
-	unsigned int out_len;
+	size_t out_len;
 
 	ctx->cctx = zstd_init_cctx(ctx->wksp, ctx->wksp_size);
 	if (!ctx->cctx)
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH] crypto: zstd - Fix compression bug caused by truncation
Posted by David Sterba 2 months, 2 weeks ago
On Tue, Sep 30, 2025 at 04:08:34PM +0800, Herbert Xu wrote:
> On Mon, Sep 29, 2025 at 11:51:36PM +0000, Han Xu wrote:
> > Hi Suman,
> > 
> > The patch f5ad93ffb5411 "crypto: zstd - convert to acomp"
> > leads to the following kernel dump during UBIFS write back.
> 
> Thanks for the detailed report and instructions!
> 
> Please let me know if you still get the crash with this patch:
> 
> ---8<---
> Use size_t for the return value of zstd_compress_cctx as otherwise
> negative errors will be truncated to a positive value.
> 
> Reported-by: Han Xu <han.xu@nxp.com>
> Fixes: f5ad93ffb541 ("crypto: zstd - convert to acomp")
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Reviewed-by: David Sterba <dsterba@suse.com>

> 
> diff --git a/crypto/zstd.c b/crypto/zstd.c
> index c2a19cb0879d..ac318d333b68 100644
> --- a/crypto/zstd.c
> +++ b/crypto/zstd.c
> @@ -83,7 +83,7 @@ static void zstd_exit(struct crypto_acomp *acomp_tfm)
>  static int zstd_compress_one(struct acomp_req *req, struct zstd_ctx *ctx,
>  			     const void *src, void *dst, unsigned int *dlen)
>  {
> -	unsigned int out_len;
> +	size_t out_len;
>  
>  	ctx->cctx = zstd_init_cctx(ctx->wksp, ctx->wksp_size);
>  	if (!ctx->cctx)
> -- 
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH] crypto: zstd - Fix compression bug caused by truncation
Posted by Han Xu 2 months, 2 weeks ago
On 25/09/30 03:32PM, David Sterba wrote:
> 
> On Tue, Sep 30, 2025 at 04:08:34PM +0800, Herbert Xu wrote:
> > On Mon, Sep 29, 2025 at 11:51:36PM +0000, Han Xu wrote:
> > > Hi Suman,
> > >
> > > The patch f5ad93ffb5411 "crypto: zstd - convert to acomp"
> > > leads to the following kernel dump during UBIFS write back.
> >
> > Thanks for the detailed report and instructions!
> >
> > Please let me know if you still get the crash with this patch:
> >
> > ---8<---
> > Use size_t for the return value of zstd_compress_cctx as otherwise
> > negative errors will be truncated to a positive value.
> >
> > Reported-by: Han Xu <han.xu@nxp.com>
> > Fixes: f5ad93ffb541 ("crypto: zstd - convert to acomp")
> > Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
> 
> Reviewed-by: David Sterba <dsterba@suse.com>

It works well now. Thanks for the quick fix.

Tested-by: Han Xu <han.xu@nxp.com>

> 
> >
> > diff --git a/crypto/zstd.c b/crypto/zstd.c
> > index c2a19cb0879d..ac318d333b68 100644
> > --- a/crypto/zstd.c
> > +++ b/crypto/zstd.c
> > @@ -83,7 +83,7 @@ static void zstd_exit(struct crypto_acomp *acomp_tfm)
> >  static int zstd_compress_one(struct acomp_req *req, struct zstd_ctx *ctx,
> >                            const void *src, void *dst, unsigned int *dlen)
> >  {
> > -     unsigned int out_len;
> > +     size_t out_len;
> >
> >       ctx->cctx = zstd_init_cctx(ctx->wksp, ctx->wksp_size);
> >       if (!ctx->cctx)
> > --
Re: [PATCH] crypto: zstd - Fix compression bug caused by truncation
Posted by Suman Kumar Chakraborty 2 months, 2 weeks ago
On Tue, Sep 30, 2025 at 04:08:34PM +0800, Herbert Xu wrote:
> On Mon, Sep 29, 2025 at 11:51:36PM +0000, Han Xu wrote:
> > Hi Suman,
> > 
> > The patch f5ad93ffb5411 "crypto: zstd - convert to acomp"
> > leads to the following kernel dump during UBIFS write back.
> 
> Thanks for the detailed report and instructions!
> 
> Please let me know if you still get the crash with this patch:

Thank you Herbert. It fixes the issue.

> 
> ---8<---
> Use size_t for the return value of zstd_compress_cctx as otherwise
> negative errors will be truncated to a positive value.
> 
> Reported-by: Han Xu <han.xu@nxp.com>
> Fixes: f5ad93ffb541 ("crypto: zstd - convert to acomp")
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
> 
> diff --git a/crypto/zstd.c b/crypto/zstd.c
> index c2a19cb0879d..ac318d333b68 100644
> --- a/crypto/zstd.c
> +++ b/crypto/zstd.c
> @@ -83,7 +83,7 @@ static void zstd_exit(struct crypto_acomp *acomp_tfm)
>  static int zstd_compress_one(struct acomp_req *req, struct zstd_ctx *ctx,
>  			     const void *src, void *dst, unsigned int *dlen)
>  {
> -	unsigned int out_len;
> +	size_t out_len;
>  
>  	ctx->cctx = zstd_init_cctx(ctx->wksp, ctx->wksp_size);
>  	if (!ctx->cctx)
> -- 
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt