a part of SLAB_FREELIST_HARDENED feature doesn't work well
(commit-webpage)[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ce6fa91b93630396ca220c33dd38ffc62686d499]
Test it on kernel 4.14.0.
When something goes like
kfree(a);
kfree(a);
then insmod crashed 'Segment Fault'
kfree(a);kfree(b);kfree(a);
Got nothing.
I add another kernel thread, just free some objects very close to the target object_a;
kfree(a);
another thread does some kfree(...)
kfree(a);
nothing happened, this patch didn't crash the insmod operation.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include <linux/kallsyms.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <asm/insn.h>
#define COUNT 0x20
char *p[COUNT];
struct task_struct *tsk;
int another_thread(void *arg)
{
int i = 0;
for (i = 0; i < COUNT-2; i+=2) {
pr_info("kfree i: %d, %p\n", i, p[i]);
kfree(p[i]);
}
msleep(10);
kfree(p[COUNT-2]);
return 0;
}static int __init test_init(void)
{
int i = 0;
for (i = 0; i < COUNT-1; i++)
p[i] = kmalloc(4096, GFP_KERNEL);
p[COUNT-1] = p[COUNT-3];
tsk = kthread_run(another_thread, NULL, "another_thread");
if (IS_ERR(tsk))
pr_info("kthread_run err\n");
for (i = 1; i < COUNT-1; i+=2) {
pr_info("kfree i: %d, %p\n", i, p[i]);
kfree(p[i]);
}
msleep(100);
kfree(p[COUNT-1]);
return 0;
}static void __exit test_exit(void)
{
if (!IS_ERR(tsk))
kthread_stop(tsk);
return;
}module_init(test_init);module_exit(test_exit);MODULE_LICENSE("GPL");
Quoted 12 lines · zerons
(commit-webpage)[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ce6fa91b93630396ca220c33dd38ffc62686d499]
Test it on kernel 4.14.0.
When something goes like
kfree(a);
kfree(a);
theninsmodcrashed 'Segment Fault'kfree(a);kfree(b);kfree(a);
Got nothing.
Quoted 6 lines
I add another kernel thread, just free some objects very close to the target
object_a;
kfree(a);
another thread does some kfree(...)
kfree(a);
nothing happened, this patch didn't crash theinsmodoperation.
Yup, that's expected and that's why it's called "naive". It's a very cheap check for a somewhat common condition, but it is not designed to catch all double-free conditions. If you want that, try booting with "slub_debug=FZP" (this is slower, but should catch a lot of bad cases).
One thing to note is that the naive check works within a single cache, which means that kmalloc()/kfree() will be less likely to be protected by this due to the many concurrent users. Specific caches (via kmem_cache_alloc()kmem_cache_free()) may be more well protected by this since there may be less contention.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Also see this out-of-tree patch:
https://github.com/copperhead/linux-hardened/commit/d6aa68684a28eac7270f715ece25998b2c9804c8
It's similar to the slub redzones but designed for hardening rather than finding bugs.
It's not great at detecting double-free without a quarantine like KASan though. Allocations get handed out again very quickly (LIFO policy via free lists). A quarantine could also improve poisoning-based write-after-free detection along with providing inherent resistance to use-after-free, etc. exploitation especially if it's partially randomized.
Quoted 26 lines · Kees Cook
Quoted 12 lines · zerons
(commit-webpage)[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ce6fa91b93630396ca220c33dd38ffc62686d499]
Test it on kernel 4.14.0.
When something goes like
kfree(a);
kfree(a);
theninsmodcrashed 'Segment Fault'kfree(a);kfree(b);kfree(a);
Got nothing.Quoted 6 lines
I add another kernel thread, just free some objects very close to the target
object_a;
kfree(a);
another thread does some kfree(...)
kfree(a);
nothing happened, this patch didn't crash theinsmodoperation.Yup, that's expected and that's why it's called "naive". It's a very cheap check for a somewhat common condition, but it is not designed to catch all double-free conditions. If you want that, try booting with "slub_debug=FZP" (this is slower, but should catch a lot of bad cases).
Quoted 8 lines
One thing to note is that the naive check works within a single cache, which means that kmalloc()/kfree() will be less likely to be protected by this due to the many concurrent users. Specific caches (via kmem_cache_alloc()kmem_cache_free()) may be more well protected by this since there may be less contention.
-Kees
After a few days of testing, I found that SLAB_FREELIST_HARDENED could do well in defending double-free-flaws-exploit, with xor obfuscation and prefetch_freepointer which would check the next-next freelist valid if the next object is not NULL.
Now, I wonder if it's possible that a thread does kfree(a) kfree(a) and another thread does kmalloc and get a returned just before the second kfree?
Thanks.