verity_fec_ctr() creates a slab cache for the FEC buffers of every
verity target that uses FEC, always under the name
"dm_verity_fec_buffers". With CONFIG_DEBUG_VM=y, as soon as two such
targets exist at the same time, for example two FEC-enabled devices or
a table reload of one, the second kmem_cache_create() trips the
duplicate name check:

kmem_cache of name 'dm_verity_fec_buffers' already exists
WARNING: mm/slab_common.c:111 at kmem_cache_sanity_check mm/slab_common.c:111 
[inline], CPU#2: syz.1.18/4545
WARNING: mm/slab_common.c:111 at __kmem_cache_create_args+0xab/0x450 
mm/slab_common.c:352, CPU#2: syz.1.18/4545
Modules linked in:
CPU: 2 UID: 0 PID: 4545 Comm: syz.1.18 Not tainted 
7.4.0-rc1-next-20261104-alt-syz-box-drivers_md_bcache #1 PREEMPT(lazy)
RIP: 0010:kmem_cache_sanity_check mm/slab_common.c:111 [inline]
RIP: 0010:__kmem_cache_create_args+0xae/0x450 mm/slab_common.c:352
Call Trace:
 <TASK>
 __kmem_cache_create include/linux/slab.h:402 [inline]
 verity_fec_ctr+0x9eb/0x1070 drivers/md/dm-verity-fec.c:709
 verity_ctr+0x1a6a/0x1ed0 drivers/md/dm-verity-target.c:1650
 dm_table_add_target+0x7c2/0xf30 drivers/md/dm-table.c:749
 populate_table drivers/md/dm-ioctl.c:1602 [inline]
 table_load+0x38b/0x910 drivers/md/dm-ioctl.c:1645
 ctl_ioctl+0x4d2/0xd70 drivers/md/dm-ioctl.c:2221
 dm_ctl_ioctl+0x1e/0x30 drivers/md/dm-ioctl.c:2243
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl fs/ioctl.c:583 [inline]
 __x64_sys_ioctl+0x18a/0x210 fs/ioctl.c:583
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x112/0x630 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x00007f213c87a829
 </TASK>

The buffer size depends on the number of RS roots of each target, so a
single cache can't be created at module init. Append a unique sequence
number to the name, as dm-bufio does. The slab core copies the name, so
a stack buffer is enough.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: 4c39529663b9 ("slab: Warn on duplicate cache names when DEBUG_VM=y")
Cc: [email protected]
Signed-off-by: Vasiliy Kovalev <[email protected]>
---
With panic_on_warn the warning panics the kernel.

 drivers/md/dm-verity-fec.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm-verity-fec.c b/drivers/md/dm-verity-fec.c
index c79f60df3a90..dfe44f25a5eb 100644
--- a/drivers/md/dm-verity-fec.c
+++ b/drivers/md/dm-verity-fec.c
@@ -594,6 +594,8 @@ int verity_fec_ctr(struct dm_verity *v)
        struct dm_target *ti = v->ti;
        u64 hash_blocks;
        int ret;
+       char cache_name[64];
+       static atomic_t seqno = ATOMIC_INIT(0);
 
        if (!verity_fec_is_enabled(v)) {
                verity_fec_dtr(v);
@@ -706,7 +708,9 @@ int verity_fec_ctr(struct dm_verity *v)
                return ret;
        }
 
-       f->cache = kmem_cache_create("dm_verity_fec_buffers",
+       snprintf(cache_name, sizeof(cache_name), "dm_verity_fec_buffers-%d",
+                atomic_inc_return(&seqno));
+       f->cache = kmem_cache_create(cache_name,
                                     f->rs_k << DM_VERITY_FEC_BUF_RS_BITS,
                                     0, 0, NULL);
        if (!f->cache) {
-- 
2.50.1


Reply via email to