[PATCH v3 01/10] slab, kunit: pin test slabs by an extra allocation
From: Vlastimil Babka (SUSE)
Date: Thu Nov 05 2026 - 03:48:08 EST
Several of slab kunit tests allocate and free a single object from the
test cache, perform intentional UAF actions and validation, while
expecting the empty slab to stay on the partial list.
This subtly relies on non-zero s->min_partial. It currently compile-time
defaults to a non-zero value, but in theory a sysfs write racing with
the kunit test could change it. More importantly, the upcoming
CONFIG_SLUB_TINY changes will make it possible to run the kunit tests
with s->min_partial being a compile-time zero, which means the tests
will now do unintended UAF writes on the page allocator level, and also
report failures, as flagged by sashiko [1].
To prevent this in a robust way, add an extra object allocation to the
tests, that will prevent the slab being freed. Use migrate_disable() to
prevent migration to a different NUMA node, which would place the
allocation to a different slab.
[1] https://sashiko.dev/#/patchset/20261024-slub_tiny_rework-v2-0-bf1419ab5ffd@xxxxxxxxxx?part=8
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
---
lib/tests/slub_kunit.c | 36 ++++++++++++++++++++++++++++++++----
1 file changed, 32 insertions(+), 4 deletions(-)
diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c
index e3b63f0338d5..40427f7edf4c 100644
--- a/lib/tests/slub_kunit.c
+++ b/lib/tests/slub_kunit.c
@@ -51,10 +51,16 @@ static void test_next_pointer(struct kunit *test)
{
struct kmem_cache *s = test_kmem_cache_create("TestSlub_next_ptr_free",
64, SLAB_POISON);
- u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ void *pin;
+ u8 *p;
unsigned long tmp;
unsigned long *ptr_addr;
+ migrate_disable();
+ pin = kmem_cache_alloc(s, GFP_KERNEL);
+ p = kmem_cache_alloc(s, GFP_KERNEL);
+ migrate_enable();
+
kmem_cache_free(s, p);
ptr_addr = (unsigned long *)(p + s->offset);
@@ -89,6 +95,7 @@ static void test_next_pointer(struct kunit *test)
validate_slab_cache(s);
KUNIT_EXPECT_EQ(test, 0, slab_errors);
+ kmem_cache_free(s, pin);
kmem_cache_destroy(s);
}
@@ -96,7 +103,13 @@ static void test_first_word(struct kunit *test)
{
struct kmem_cache *s = test_kmem_cache_create("TestSlub_1th_word_free",
64, SLAB_POISON);
- u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ void *pin;
+ u8 *p;
+
+ migrate_disable();
+ pin = kmem_cache_alloc(s, GFP_KERNEL);
+ p = kmem_cache_alloc(s, GFP_KERNEL);
+ migrate_enable();
kmem_cache_free(s, p);
*p = 0x78;
@@ -104,6 +117,7 @@ static void test_first_word(struct kunit *test)
validate_slab_cache(s);
KUNIT_EXPECT_EQ(test, 2, slab_errors);
+ kmem_cache_free(s, pin);
kmem_cache_destroy(s);
}
@@ -111,7 +125,13 @@ static void test_clobber_50th_byte(struct kunit *test)
{
struct kmem_cache *s = test_kmem_cache_create("TestSlub_50th_word_free",
64, SLAB_POISON);
- u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ void *pin;
+ u8 *p;
+
+ migrate_disable();
+ pin = kmem_cache_alloc(s, GFP_KERNEL);
+ p = kmem_cache_alloc(s, GFP_KERNEL);
+ migrate_enable();
kmem_cache_free(s, p);
p[50] = 0x9a;
@@ -119,6 +139,7 @@ static void test_clobber_50th_byte(struct kunit *test)
validate_slab_cache(s);
KUNIT_EXPECT_EQ(test, 2, slab_errors);
+ kmem_cache_free(s, pin);
kmem_cache_destroy(s);
}
#endif
@@ -127,7 +148,13 @@ static void test_clobber_redzone_free(struct kunit *test)
{
struct kmem_cache *s = test_kmem_cache_create("TestSlub_RZ_free", 64,
SLAB_RED_ZONE);
- u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ void *pin;
+ u8 *p;
+
+ migrate_disable();
+ pin = kmem_cache_alloc(s, GFP_KERNEL);
+ p = kmem_cache_alloc(s, GFP_KERNEL);
+ migrate_enable();
kasan_disable_current();
kmem_cache_free(s, p);
@@ -137,6 +164,7 @@ static void test_clobber_redzone_free(struct kunit *test)
KUNIT_EXPECT_EQ(test, 2, slab_errors);
kasan_enable_current();
+ kmem_cache_free(s, pin);
kmem_cache_destroy(s);
}
--
2.56.0