Skip to content

Commit eaebeb9

Browse files
yosrym93akpm00
authored andcommitted
mm: zswap: fix race between [de]compression and CPU hotunplug
In zswap_compress() and zswap_decompress(), the per-CPU acomp_ctx of the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU. If the original CPU is hotunplugged while the acomp_ctx is still in use, we run into a UAF bug as the resources attached to the acomp_ctx are freed during hotunplug in zswap_cpu_comp_dead(). The problem was introduced in commit 1ec3b5f ("mm/zswap: move to use crypto_acomp API for hardware acceleration") when the switch to the crypto_acomp API was made. Prior to that, the per-CPU crypto_comp was retrieved using get_cpu_ptr() which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with the crypto_acomp API as a sleepable context is needed. Commit 8ba2f84 ("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug. There are a few ways to fix this: (a) Add a refcount for acomp_ctx. (b) Disable migration while using the per-CPU acomp_ctx. (c) Disable CPU hotunplug while using the per-CPU acomp_ctx by holding the CPUs read lock. Implement (c) since it's simpler than (a), and (b) involves using migrate_disable() which is apparently undesired (see huge comment in include/linux/preempt.h). Link: https://lkml.kernel.org/r/[email protected] Fixes: 1ec3b5f ("mm/zswap: move to use crypto_acomp API for hardware acceleration") Signed-off-by: Yosry Ahmed <[email protected]> Reported-by: Johannes Weiner <[email protected]> Closes: https://lore.kernel.org/lkml/[email protected]/ Reported-by: Sam Sun <[email protected]> Closes: https://lore.kernel.org/lkml/CAEkJfYMtSdM5HceNsXUDf5haghD5+o2e7Qv4OcuruL4tPg6OaQ@mail.gmail.com/ Reviewed-by: Chengming Zhou <[email protected]> Acked-by: Barry Song <[email protected]> Reviewed-by: Nhat Pham <[email protected]> Cc: Vitaly Wool <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5f3fd77 commit eaebeb9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Diff for: mm/zswap.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
880880
return 0;
881881
}
882882

883+
/* Prevent CPU hotplug from freeing up the per-CPU acomp_ctx resources */
884+
static struct crypto_acomp_ctx *acomp_ctx_get_cpu(struct crypto_acomp_ctx __percpu *acomp_ctx)
885+
{
886+
cpus_read_lock();
887+
return raw_cpu_ptr(acomp_ctx);
888+
}
889+
890+
static void acomp_ctx_put_cpu(void)
891+
{
892+
cpus_read_unlock();
893+
}
894+
883895
static bool zswap_compress(struct page *page, struct zswap_entry *entry,
884896
struct zswap_pool *pool)
885897
{
@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
893905
gfp_t gfp;
894906
u8 *dst;
895907

896-
acomp_ctx = raw_cpu_ptr(pool->acomp_ctx);
897-
908+
acomp_ctx = acomp_ctx_get_cpu(pool->acomp_ctx);
898909
mutex_lock(&acomp_ctx->mutex);
899910

900911
dst = acomp_ctx->buffer;
@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
950961
zswap_reject_alloc_fail++;
951962

952963
mutex_unlock(&acomp_ctx->mutex);
964+
acomp_ctx_put_cpu();
953965
return comp_ret == 0 && alloc_ret == 0;
954966
}
955967

@@ -960,7 +972,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
960972
struct crypto_acomp_ctx *acomp_ctx;
961973
u8 *src;
962974

963-
acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
975+
acomp_ctx = acomp_ctx_get_cpu(entry->pool->acomp_ctx);
964976
mutex_lock(&acomp_ctx->mutex);
965977

966978
src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
@@ -990,6 +1002,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
9901002

9911003
if (src != acomp_ctx->buffer)
9921004
zpool_unmap_handle(zpool, entry->handle);
1005+
acomp_ctx_put_cpu();
9931006
}
9941007

9951008
/*********************************

0 commit comments

Comments
 (0)