[PATCH 1/2] zram: avoid rollback UAF in zcomp_cpu_up_prepare()
From: Sergey Senozhatsky
Date: Sun Nov 29 2026 - 23:54:32 EST
A task on CPU1 can grab CPU1's per-CPU compression stream pointer via
raw_cpu_ptr() and get preempted before taking cstrm->lock. If CPU1 goes
offline, the task is migrated to another CPU while still holding its
pointer to CPU1's cstrm, and zcomp_cpu_dead() destroys that stream under
its lock. When CPU1 starts coming back online, the task can resume on
that other CPU and acquire CPU1's cstrm->lock while
zcomp_cpu_up_prepare() is still rebuilding CPU1's streams.
Currently zcomp_cpu_up_prepare() sets cstrm->buffer before calling
zcomp_dstrm_init(). If the resumed writer acquires cstrm->lock, sees
cstrm->buffer != NULL and starts compressing, and zcomp_dstrm_init()
then fails, the rollback calls zcomp_cstrm_free() without cstrm->lock,
freeing ->ctx and ->buffer under the active writer.
Splitting zcomp into separate R/W streams widened this window: before
that, the rollback could only follow a failed ->local_copy allocation in
zcomp_strm_init().
Move the buffer vzalloc() calls out of zcomp_{c,d}strm_init() (renamed
to zcomp_{c,d}strm_ctx_init(), paired with zcomp_{c,d}strm_ctx_free())
into zcomp_cpu_up_prepare() and zcomp_init(). Because ->buffer and
->local_copy remain NULL and untouched on the error paths, nothing reads
->ctx and rollback never races with a resumed writer.
Fixes: 2efa9e9eb4db ("zram: permit preemption with active compression stream")
Cc: <stable+noautosel@xxxxxxxxxx> # depends on the zram R/W stream split, not in stable trees
Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
---
drivers/block/zram/zcomp.c | 83 +++++++++++++++++++++++++-------------
1 file changed, 56 insertions(+), 27 deletions(-)
diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index ad8c7dc73922..365845af465f 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -45,16 +45,21 @@ static const struct zcomp_ops *backends[] = {
NULL
};
-static void zcomp_cstrm_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+static void zcomp_cstrm_ctx_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
{
if (comp->ops->destroy_cctx)
comp->ops->destroy_cctx(&zstrm->ctx);
+ zstrm->ctx.context = NULL;
+}
+
+static void zcomp_cstrm_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+{
+ zcomp_cstrm_ctx_free(comp, zstrm);
vfree(zstrm->buffer);
zstrm->buffer = NULL;
- zstrm->ctx.context = NULL;
}
-static int zcomp_cstrm_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+static int zcomp_cstrm_ctx_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
{
int ret;
@@ -67,28 +72,24 @@ static int zcomp_cstrm_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
}
}
- /*
- * allocate 2 pages. 1 for compressed data, plus 1 extra for the
- * case when compressed size is larger than the original one
- */
- zstrm->buffer = vzalloc(2 * PAGE_SIZE);
- if (!zstrm->buffer) {
- zcomp_cstrm_free(comp, zstrm);
- return -ENOMEM;
- }
return 0;
}
-static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+static void zcomp_dstrm_ctx_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
{
if (comp->ops->destroy_dctx)
comp->ops->destroy_dctx(&zstrm->ctx);
+ zstrm->ctx.context = NULL;
+}
+
+static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+{
+ zcomp_dstrm_ctx_free(comp, zstrm);
vfree(zstrm->local_copy);
zstrm->local_copy = NULL;
- zstrm->ctx.context = NULL;
}
-static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+static int zcomp_dstrm_ctx_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
{
int ret;
@@ -101,11 +102,6 @@ static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
}
}
- zstrm->local_copy = vzalloc(PAGE_SIZE);
- if (!zstrm->local_copy) {
- zcomp_dstrm_free(comp, zstrm);
- return -ENOMEM;
- }
return 0;
}
@@ -333,27 +329,55 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
struct zcomp_cstrm *cstrm = NULL;
struct zcomp_dstrm *dstrm;
+ void *buffer = NULL, *local_copy;
int ret;
if (comp->cstream) {
cstrm = per_cpu_ptr(comp->cstream, cpu);
- ret = zcomp_cstrm_init(comp, cstrm);
+ ret = zcomp_cstrm_ctx_init(comp, cstrm);
if (ret) {
pr_err("Can't allocate a compression stream\n");
return ret;
}
+
+ /*
+ * allocate 2 pages. 1 for compressed data, plus 1 extra for
+ * the case when compressed size is larger than the original
+ * one
+ */
+ buffer = vzalloc(2 * PAGE_SIZE);
+ if (!buffer) {
+ zcomp_cstrm_ctx_free(comp, cstrm);
+ return -ENOMEM;
+ }
}
dstrm = per_cpu_ptr(comp->dstream, cpu);
- ret = zcomp_dstrm_init(comp, dstrm);
+ ret = zcomp_dstrm_ctx_init(comp, dstrm);
if (ret) {
pr_err("Can't allocate a decompression stream\n");
- if (cstrm)
- zcomp_cstrm_free(comp, cstrm);
- return ret;
+ goto err;
+ }
+
+ local_copy = vzalloc(PAGE_SIZE);
+ if (!local_copy) {
+ zcomp_dstrm_ctx_free(comp, dstrm);
+ ret = -ENOMEM;
+ goto err;
}
+ if (cstrm)
+ cstrm->buffer = buffer;
+ dstrm->local_copy = local_copy;
+
return 0;
+
+err:
+ if (cstrm) {
+ vfree(buffer);
+ zcomp_cstrm_ctx_free(comp, cstrm);
+ }
+ return ret;
}
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
@@ -394,13 +418,18 @@ static int zcomp_init(struct zcomp *comp, struct zcomp_params *params,
goto cleanup;
}
mutex_init(&comp->recomp_cstream->lock);
- ret = zcomp_cstrm_init(comp, comp->recomp_cstream);
+ ret = zcomp_cstrm_ctx_init(comp, comp->recomp_cstream);
if (ret) {
- /* zcomp_cstrm_init() cleans up after itself */
+ /* zcomp_cstrm_ctx_init() cleans up after itself */
kfree(comp->recomp_cstream);
comp->recomp_cstream = NULL;
goto cleanup;
}
+ comp->recomp_cstream->buffer = vzalloc(2 * PAGE_SIZE);
+ if (!comp->recomp_cstream->buffer) {
+ ret = -ENOMEM;
+ goto cleanup;
+ }
} else {
comp->cstream = alloc_percpu(struct zcomp_cstrm);
if (!comp->cstream) {
--
2.56.1.839.g22a8290db9-goog