Skip to contentSkip to conversation

zram: validate decompressed page size

PATCH In review Artem Novak 2 participants 5 messages Nov 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
index 3846a04c69d7..aa6ca77333c9 100644
@@ -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
index f71b11bcac78..54638d2c119e 100644
@@ -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
index 1e4ad31d39a6..36401651fce8 100644
@@ -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
index d8aa01bb258f..017e08038107 100644
@@ -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
index 08da3810cffd..31f07ee3797f 100644
@@ -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
index 028e2f0f587e..72a253adae84 100644
@@ -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
index f38fd31f9e4e..37af71a7db1a 100644
@@ -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
#1 lore raw

On (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.

#2 lore raw

Hey Sergey,

Quoted 4 lines · Sergey Senozhatsky

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?

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;
 }
#3 lore raw

Hi 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_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.

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.

#4 lore raw

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_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?