* [PATCH] zram: validate decompressed page size
@ 2026-11-27 0:44 Artem Novak
2026-11-27 1:19 ` Sergey Senozhatsky
0 siblings, 1 reply; 5+ messages in thread
From: Artem Novak @ 2026-11-27 0:44 UTC (permalink / raw)
To: minchan, senozhatsky; +Cc: axboe, linux-kernel, linux-block, Artem Novak
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.
Signed-off-by: Artem Novak <artem122novak@gmail.com>
---
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(-)
diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c
index 3846a04c69d7..aa6ca77333c9 100644
--- a/drivers/block/zram/backend_842.c
+++ b/drivers/block/zram/backend_842.c
@@ -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 = {
diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c
index f71b11bcac78..54638d2c119e 100644
--- a/drivers/block/zram/backend_deflate.c
+++ b/drivers/block/zram/backend_deflate.c
@@ -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;
}
diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c
index 1e4ad31d39a6..36401651fce8 100644
--- a/drivers/block/zram/backend_lz4.c
+++ b/drivers/block/zram/backend_lz4.c
@@ -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;
}
diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c
index d8aa01bb258f..017e08038107 100644
--- a/drivers/block/zram/backend_lz4hc.c
+++ b/drivers/block/zram/backend_lz4hc.c
@@ -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;
}
diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c
index 08da3810cffd..31f07ee3797f 100644
--- a/drivers/block/zram/backend_zstd.c
+++ b/drivers/block/zram/backend_zstd.c
@@ -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;
}
diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index 028e2f0f587e..72a253adae84 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -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;
}
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index f38fd31f9e4e..37af71a7db1a 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -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;
};
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] zram: validate decompressed page size
2026-11-27 0:44 [PATCH] zram: validate decompressed page size Artem Novak
@ 2026-11-27 1:19 ` Sergey Senozhatsky
2026-11-27 1:34 ` Artem Novak
0 siblings, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-27 1:19 UTC (permalink / raw)
To: Artem Novak; +Cc: minchan, senozhatsky, axboe, linux-kernel, linux-block
On (26/11/26 18:44), Artem Novak wrote:
[..]
> @@ -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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zram: validate decompressed page size
2026-11-27 1:19 ` Sergey Senozhatsky
@ 2026-11-27 1:34 ` Artem Novak
2026-11-27 2:10 ` Sergey Senozhatsky
0 siblings, 1 reply; 5+ messages in thread
From: Artem Novak @ 2026-11-27 1:34 UTC (permalink / raw)
To: Sergey Senozhatsky; +Cc: minchan, axboe, linux-kernel, linux-block
Hey Sergey,
On Thu, Nov 26, 2026 at 7:19 PM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
> 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.
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?
>
> On (26/11/26 18:44), Artem Novak wrote:
> [..]
> > @@ -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;
> > }
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zram: validate decompressed page size
2026-11-27 1:34 ` Artem Novak
@ 2026-11-27 2:10 ` Sergey Senozhatsky
2026-11-27 2:41 ` Artem Novak
0 siblings, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-27 2:10 UTC (permalink / raw)
To: Artem Novak; +Cc: Sergey Senozhatsky, minchan, axboe, linux-kernel, linux-block
Hi Artem,
On (26/11/26 19:34), Artem Novak wrote:
> Hey Sergey,
>
> On Thu, Nov 26, 2026 at 7:19 PM Sergey Senozhatsky
> <senozhatsky@chromium.org> wrote:
> > 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.
>
> 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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zram: validate decompressed page size
2026-11-27 2:10 ` Sergey Senozhatsky
@ 2026-11-27 2:41 ` Artem Novak
0 siblings, 0 replies; 5+ messages in thread
From: Artem Novak @ 2026-11-27 2:41 UTC (permalink / raw)
To: Sergey Senozhatsky; +Cc: minchan, axboe, linux-kernel, linux-block
Hello again,
> 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
On Thu, Nov 26, 2026 at 8:11 PM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> Hi Artem,
>
> On (26/11/26 19:34), Artem Novak wrote:
> > Hey Sergey,
> >
> > On Thu, Nov 26, 2026 at 7:19 PM Sergey Senozhatsky
> > <senozhatsky@chromium.org> wrote:
> > > 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.
> >
> > 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?
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-27 2:41 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 0:44 [PATCH] zram: validate decompressed page size Artem Novak
2026-11-27 1:19 ` Sergey Senozhatsky
2026-11-27 1:34 ` Artem Novak
2026-11-27 2:10 ` Sergey Senozhatsky
2026-11-27 2:41 ` Artem Novak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox