kasan: add ability to detect double-kmem_cache_destroy()
Subsystems (3)
PATCH 1/2 kasan: add ability to detect double-kmem_cache_destroy()
Because mm/slab_common.c is not instrumented with software KASAN modes, it is not possible to detect use-after-free of the kmem_cache passed into kmem_cache_destroy(). In particular, because of the s->refcount-- and subsequent early return if non-zero, KASAN would never be able to see the double-free via kmem_cache_free(kmem_cache, s). To be able to detect a double-kmem_cache_destroy(), check accessibility of the kmem_cache, and in case of failure return early.
While KASAN_HW_TAGS is able to detect such bugs, by checking accessibility and returning early we fail more gracefully and also avoid corrupting reused objects (where tags mismatch).
A recent case of a double-kmem_cache_destroy() was detected by KFENCE:
https://lkml.kernel.org/r/[email protected]
, which was not detectable by software KASAN modes.
--- mm/slab_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +1 -1
@@ -491,7 +491,7 @@ void kmem_cache_destroy(struct kmem_cache *s) {int err; -if (unlikely (!s)) +if (unlikely (!s || !kasan_check_byte (s)))return ;cpus_read_lock ();
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
PATCH 2/2 kasan: test: add test case for double-kmem_cache_destroy()
Add a test case for double-kmem_cache_destroy() detection.
--- lib/test_kasan.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
lib/test_kasan.c +11 -0
@@ -866,6 +866,16 @@ static void kmem_cache_invalid_free(struct kunit *test)kmem_cache_destroy (cache); } +static void kmem_cache_double_destroy (struct kunit *test) +{ +struct kmem_cache *cache; + + cache =kmem_cache_create ("test_cache" ,200 ,0 ,0 ,NULL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, cache); +kmem_cache_destroy (cache); +KUNIT_EXPECT_KASAN_FAIL (test,kmem_cache_destroy (cache)); +} +static void kasan_memchr (struct kunit *test) {char *ptr;
@@ -1183,6 +1193,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (ksize_uaf),KUNIT_CASE (kmem_cache_double_free),KUNIT_CASE (kmem_cache_invalid_free), +KUNIT_CASE (kmem_cache_double_destroy),KUNIT_CASE (kasan_memchr),KUNIT_CASE (kasan_memcmp), KUNIT_CASE(kasan_strings),
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
Re: [PATCH 2/2] kasan: test: add test case for double-kmem_cache_destroy()
Quoted 38 lines · Marco Elver
Add a test case for double-kmem_cache_destroy() detection.
Signed-off-by Marco Elver <[email protected]>--- lib/test_kasan.c | 11 +++++++++++ 1 file changed, 11 insertions(+)lib/test_kasan.c +11
-1@@ -866,6 +866,16 @@ static void kmem_cache_invalid_free(struct kunit *test)kmem_cache_destroy (cache); } +static void kmem_cache_double_destroy (struct kunit *test) +{ +struct kmem_cache *cache; + + cache =kmem_cache_create ("test_cache" ,200 ,0 ,0 ,NULL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, cache); +kmem_cache_destroy (cache); +KUNIT_EXPECT_KASAN_FAIL (test,kmem_cache_destroy (cache)); +} +static void kasan_memchr (struct kunit *test) {char *ptr;@@ -1183,6 +1193,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (ksize_uaf),KUNIT_CASE (kmem_cache_double_free),KUNIT_CASE (kmem_cache_invalid_free), +KUNIT_CASE (kmem_cache_double_destroy),KUNIT_CASE (kasan_memchr),KUNIT_CASE (kasan_memcmp), KUNIT_CASE(kasan_strings), --
Quoted 2 lines
2.34.0.rc2.393.gf8c9666880-goog
Thanks!
Quoted 12 lines · Marco Elver
Because
mm/slab_common.cis not instrumented with software KASAN modes, it is not possible to detect use-after-free of thekmem_cachepassed intokmem_cache_destroy(). In particular, because of the s->refcount-- and subsequent early return if non-zero, KASAN would never be able to see the double-free viakmem_cache_free(kmem_cache, s). To be able to detect a double-kmem_cache_destroy(), check accessibility of thekmem_cache, and in case of failure return early.While
KASAN_HW_TAGSis able to detect such bugs, by checking accessibility and returning early we fail more gracefully and also avoid corrupting reused objects (where tags mismatch).
Quoted 5 lines
A recent case of a double-kmem_cache_destroy() was detected by KFENCE:
https://lkml.kernel.org/r/[email protected]
, which was not detectable by software KASAN modes.Signed-off-by Marco Elver <[email protected]>
Quoted 18 lines
--- mm/slab_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab_common.c +1
-1@@ -491,7 +491,7 @@ void kmem_cache_destroy(struct kmem_cache *s) {int err; -if (unlikely (!s)) +if (unlikely (!s || !kasan_check_byte (s)))return ;cpus_read_lock ();
Quoted 36 lines · Marco Elver
Because
mm/slab_common.cis not instrumented with software KASAN modes, it is not possible to detect use-after-free of thekmem_cachepassed intokmem_cache_destroy(). In particular, because of the s->refcount-- and subsequent early return if non-zero, KASAN would never be able to see the double-free viakmem_cache_free(kmem_cache, s). To be able to detect a double-kmem_cache_destroy(), check accessibility of thekmem_cache, and in case of failure return early.While
KASAN_HW_TAGSis able to detect such bugs, by checking accessibility and returning early we fail more gracefully and also avoid corrupting reused objects (where tags mismatch).A recent case of a double-kmem_cache_destroy() was detected by KFENCE:
https://lkml.kernel.org/r/[email protected]
, which was not detectable by software KASAN modes.Signed-off-by Marco Elver <[email protected]>--- mm/slab_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab_common.c +1
-2@@ -491,7 +491,7 @@ void kmem_cache_destroy(struct kmem_cache *s) {int err; -if (unlikely (!s)) +if (unlikely (!s || !kasan_check_byte (s)))return ;cpus_read_lock (); --
Quoted 2 lines
2.34.0.rc2.393.gf8c9666880-goog
Thanks!