lkdtm: fix potential use after free
Subsystems (2)
PATCH 1/1 lkdtm: fix potential use after free
From: swkhack <[email protected]>
The function lkdtm_READ_AFTER_FREE calls kfree(base) to free the memory of base. However, following kfree(base),
it access the memory which base point to via base[offset]. This may result in a use-after-free bug. This patch moves kfree(base) after the dereference.
--- drivers/misc/lkdtm/heap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/lkdtm/heap.c +1 -1
@@ -77,7 +77,6 @@ void lkdtm_READ_AFTER_FREE(void) base[offset] = *val;pr_info ("Value in memory before free: %x\n" , base[offset]); -kfree (base);pr_info ("Attempting bad read from freed memory\n" ); saw = base[offset];
@@ -88,6 +87,7 @@ void lkdtm_READ_AFTER_FREE(void) }pr_info ("Memory was not poisoned\n" ); +kfree (base);kfree (val); }
Signature · 1 line
2.17.1
Hi,
Quoted 6 lines · Weikang shi
From: swkhack <[email protected]>
The function
lkdtm_READ_AFTER_FREEcalls kfree(base) to free the memory of base. However, following kfree(base),
it access the memory which base point to via base[offset]. This may result in a use-after-free bug.
This is deliberate; the test is designed to provoke a use-after-free to verify that this can be detected by tools such as KASAN.
Quoted 1 line
This patch moves kfree(base) after the dereference.
Doing this would break the test, so we should not make this change.
Was this something you spotted with a static analysis tool?
Quoted 1 line
Signed-off-by swkhack <[email protected]>
A signed-off-by line should have your real name rather than an alias.
Thanks,
Mark.
Quoted 27 lines
--- drivers/misc/lkdtm/heap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/misc/lkdtm/heap.c +1
-1@@ -77,7 +77,6 @@ void lkdtm_READ_AFTER_FREE(void) base[offset] = *val;pr_info ("Value in memory before free: %x\n" , base[offset]); -kfree (base);pr_info ("Attempting bad read from freed memory\n" ); saw = base[offset];@@ -88,6 +87,7 @@ void lkdtm_READ_AFTER_FREE(void) }pr_info ("Memory was not poisoned\n" ); +kfree (base);kfree (val); }Signature · 1 line
2.17.1