zram: validate decompressed page size
Subsystems (3)
PATCH 1/1 zram: validate decompressed page size
Several zram compression backends do not update dst_len after decompression, leaving it at the destination buffer capacity rather than reporting the actual decompressed size.
Update the 842, DEFLATE, LZ4, LZ4HC and Zstandard backends to record the decompressed length. Afterwards reject successful decompression operations that do not produce exactly PAGE_SIZE bytes;
Prevents zram from accepting decompressed data whose size does not match the expected page size.
--- drivers/block/zram/backend_842.c | 6 +++++- drivers/block/zram/backend_deflate.c | 1 + drivers/block/zram/backend_lz4.c | 1 + drivers/block/zram/backend_lz4hc.c | 1 + drivers/block/zram/backend_zstd.c | 1 + drivers/block/zram/zcomp.c | 2 ++ drivers/block/zram/zcomp.h | 1 + 7 files changed, 12 insertions(+), 1 deletion(-)
drivers/block/zram/backend_842.c +5 -1
@@ -56,8 +56,12 @@ static int decompress_842(struct zcomp_params *params, struct zcomp_ctx *ctx, struct zcomp_req *req) {unsigned int dlen = req->dst_len; +int ret; -return sw842_decompress (req->src, req->src_len, req->dst, &dlen); + ret =sw842_decompress (req->src, req->src_len, req->dst, &dlen); +if (!ret) + req->dst_len = dlen; +return ret; }const struct zcomp_ops backend_842 = {
drivers/block/zram/backend_deflate.c +1 -0
@@ -155,6 +155,7 @@ static int deflate_decompress(struct zcomp_params *params,if (ret !=Z_STREAM_END )return -EINVAL ; + req->dst_len = inflate->total_out;return 0 ; }
drivers/block/zram/backend_lz4.c +1 -0
@@ -144,6 +144,7 @@ static int lz4_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx, }if (ret <0 )return -EINVAL ; + req->dst_len = ret;return 0 ; }
drivers/block/zram/backend_lz4hc.c +1 -0
@@ -128,6 +128,7 @@ static int lz4hc_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx, }if (ret <0 )return -EINVAL ; + req->dst_len = ret;return 0 ; }
drivers/block/zram/backend_zstd.c +1 -0
@@ -208,6 +208,7 @@ static int zstd_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx, req->src_len, zp->ddict);if (zstd_is_error (ret))return -EINVAL ; + req->dst_len = ret;return 0 ; }
drivers/block/zram/zcomp.c +2 -0
@@ -185,6 +185,8 @@ int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, ret = comp->ops->decompress (comp->params, &zstrm->ctx, &req);if (src)kunmap_local (src); +if (!ret && req.dst_len !=PAGE_SIZE ) + ret = -EINVAL ;return ret; }
drivers/block/zram/zcomp.h +1 -0
@@ -53,6 +53,7 @@ struct zcomp_req {const size_t src_len;unsigned char *dst; +/* Destination capacity on input, decompressed size on success. */ size_t dst_len; };
Signature · 1 line
2.56.1
Sergey Senozhatsky
maintainerOn (26/11/26 18:44), Artem Novak wrote:
[..]
drivers/block/zram/zcomp.c int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, · line 191
Quoted 8 lines
drivers/block/zram/zcomp.c +2
-0@@ -185,6 +185,8 @@ int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, ret = comp->ops->decompress (comp->params, &zstrm->ctx, &req);if (src)kunmap_local (src); +if (!ret && req.dst_len !=PAGE_SIZE ) + ret = -EINVAL ;return ret; }
Yeah, I don't think we should do this. We only ever compress PAGE_SIZE buffers, if some backend decompresses it to a something that is not PAGE_SIZE and doesn't signal an error via "ret" then it's a bug in the backend and zram should not cover it up.
Hey Sergey,
Quoted 4 lines · Sergey Senozhatsky
Yeah, I don't think we should do this. We only ever compress
PAGE_SIZEbuffers, if some backend decompresses it to a something that is notPAGE_SIZEand doesn't signal an error via "ret" then it's a bug in the backend and zram should not cover it up.
I see.
I thought it might be worth having a defensive check here, since allowing an incorrectly sized result to propagate seems a bit sloppy.
Do you consider this useful, or should I rely completely on the backend contract?
Quoted 12 lines · Artem Novak
[..]
Quoted 8 lines
drivers/block/zram/zcomp.c +2
-0@@ -185,6 +185,8 @@ int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, ret = comp->ops->decompress (comp->params, &zstrm->ctx, &req);if (src)kunmap_local (src); +if (!ret && req.dst_len !=PAGE_SIZE ) + ret = -EINVAL ;return ret; }
Sergey Senozhatsky
maintainerHi Artem,
Quoted 9 lines · Artem Novak
Hey Sergey,
Quoted 4 lines · Sergey Senozhatsky
Yeah, I don't think we should do this. We only ever compress
PAGE_SIZEbuffers, if some backend decompresses it to a something that is notPAGE_SIZEand doesn't signal an error via "ret" then it's a bug in the backend and zram should not cover it up.
Quoted 7 lines
I see.
I thought it might be worth having a defensive check here, since allowing an incorrectly sized result to propagate seems a bit sloppy.
Do you consider this useful, or should I rely completely on the backend contract?
I would probably prefer to do nothing about it, IOW rely on the backend contract. Most of those compression libraries are used in other places in the kernel (f2fs, squashfs, etc. etc.), zram is not the only one to rely on them.
Hello again,
Quoted 4 lines
I would probably prefer to do nothing about it, IOW rely on the backend contract. Most of those compression libraries are used in other places in the kernel (f2fs, squashfs, etc. etc.), zram is not the only one to rely on them.
LGTM; I'll drop this change.
Thanks for the explanation.
Artem
Quoted 21 lines · Sergey Senozhatsky
Hi Artem,
Quoted 16 lines · Artem Novak
Hey Sergey,
Quoted 4 lines · Sergey Senozhatsky
Yeah, I don't think we should do this. We only ever compress
PAGE_SIZEbuffers, if some backend decompresses it to a something that is notPAGE_SIZEand doesn't signal an error via "ret" then it's a bug in the backend and zram should not cover it up.I see.
I thought it might be worth having a defensive check here, since allowing an incorrectly sized result to propagate seems a bit sloppy.
Do you consider this useful, or should I rely completely on the backend contract?