Check write to slab memory which freed already using mudflap v3
Subsystems (5)
PATCH 1/1 Check write to slab memory which freed already using mudflap v3
slab debug detects the corruption when it allocates and frees the slab. But if someone write to slab memory when it already freed, slab debug could only report its last free point when slab allocates the same block.
This information is not enough for debugging when system has complex memory usage. gcc mudflap could insert some check point when a memory is writing and it is helpful to check if the address is writing freed.
Because most codes of kernel are drivers, it is most possible that drivers have such problem. So I use mudflap to compile all drivers.
kmemcheck is also helpful to check same issue, and it triggers trap every reading and writing, and it need to analysis the instruct which trigger the trap to get the memory address. This heavy depends on the cpu capability. arm has no single-step like x86, so kmemcheck is too heavy to run on real arm system. mudflap way is lighter than kmemcheck for this purpose.
As stack allocation is useless for checking slab, kernel mudflap does not implement alloc which is required by gcc mudflap when codes use variable array. So variable array, such as array[i] which i could not determined by compiler, is not supported.
Note: gcc 4.1.2 generates a wrong code in kbd_init of drivers/char/keyboard.c when use mudflap flag. Version 4.2 and 4.3 are ok.
mudflap link: http://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging
Thanks a lot for comments
Changeslong:
V3: make codes more arch independence according to Pekka Enberg comments
V2: add slub support according to Pekka Enberg comments
V1: add mudflap core and slab support
arch/arm/include/asm/elf.h | 1 + arch/arm/include/asm/mf-runtime.h | 6 ++ arch/arm/kernel/Makefile | 2 +- arch/arm/kernel/module.c | 1 + arch/arm/kernel/mudflap.c | 24 ++++++++ drivers/Makefile | 4 +- include/linux/slab_def.h | 4 ++ include/linux/slub_def.h | 4 ++ include/mf-runtime.h | 45 ++++++++++++++++ kernel/Makefile | 2 +- kernel/mudflap.c | 105 +++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 7 +++ mm/slab.c | 40 ++++++++++++++ mm/slub.c | 28 ++++++++++ 14 files changed, 270 insertions(+), 3 deletions(-)
arch/arm/include/asm/elf.h +1 -0
@@ -50,6 +50,7 @@ typedef struct user_fp elf_fpregset_t;#define R_ARM_ABS32 2#define R_ARM_CALL 28#define R_ARM_JUMP24 29 +#define R_ARM_TARGET1 38#define R_ARM_V4BX 40#define R_ARM_PREL31 42#define R_ARM_MOVW_ABS_NC 43
arch/arm/include/asm/mf-runtime.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef _LINUX_ARM_MF_RUNTIME +#define _LINUX_ARM_MF_RUNTIME +#ifdef _MUDFLAP +#pragma redefine_extname __memzero kmudflap_memzero +#endif /* _MUDFLAP */ +#endif
arch/arm/kernel/Makefile +1 -1
@@ -13,7 +13,7 @@ endif obj-y := compat.o elf.o entry-armv.o entry-common.o irq.o \ process.o ptrace.o setup.o signal.o \ sys_arm.o stacktrace.o time.o traps.o - +obj-$(CONFIG_MUDFLAP) += mudflap.o obj-$(CONFIG_ISA_DMA_API) += dma.o obj-$(CONFIG_ARCH_ACORN) += ecard.o obj-$(CONFIG_FIQ) += fiq.o
arch/arm/kernel/module.c +1 -0
@@ -128,6 +128,7 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,break ;case R_ARM_ABS32 : +case R_ARM_TARGET1 : *(u32 *)loc += sym->st_value;break ;
arch/arm/kernel/mudflap.c new file +24 -0
@@ -0,0 +1,24 @@ +/* +* kernel/mudflap.c +* +* Copyright (C) 2009 Motorola Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +*/ +#include <mf-runtime.h> +#include <asm/string.h> +#include <linux/module.h> + +void kmudflap_memzero (void *ptr,__kernel_size_t n) +{ +__mf_check (ptr, n, __MF_CHECK_WRITE,"memzero dest" ); +return __memzero (ptr, n); +} +EXPORT_SYMBOL (kmudflap_memzero);
drivers/Makefile +3 -1
@@ -4,7 +4,9 @@ # 15 Sep 2000, Christoph Hellwig <[email protected]> # Rewritten to use lists instead of if-statements. # - +ifdef CONFIG_MUDFLAP +subdir-ccflags-y += -fmudflap -fmudflapir +endif obj-y += gpio/ obj-$(CONFIG_PCI) += pci/ obj-$(CONFIG_PARISC) += parisc/
include/linux/slab_def.h +4 -0
@@ -220,4 +220,8 @@ found:#endif /* CONFIG_NUMA */ +#ifdef CONFIG_MUDFLAP +void slab_check_write (void *ptr,unsigned int sz,const char *location); +#endif +#endif /* _LINUX_SLAB_DEF_H */
include/linux/slub_def.h +4 -0
@@ -304,4 +304,8 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)void __init kmem_cache_init_late (void ); +#ifdef CONFIG_MUDFLAP +void slab_check_write (void *ptr,unsigned int sz,const char *location); +#endif +#endif /* _LINUX_SLUB_DEF_H */
include/mf-runtime.h new file +45 -0
45 lines not on this page · raw message
kernel/Makefile +1 -1
@@ -22,7 +22,7 @@ CFLAGS_REMOVE_rtmutex-debug.o = -pg CFLAGS_REMOVE_cgroup-debug.o = -pg CFLAGS_REMOVE_sched_clock.o = -pg endif - +obj-$(CONFIG_MUDFLAP) += mudflap.o obj-$(CONFIG_FREEZER) += freezer.o obj-$(CONFIG_PROFILING) += profile.o obj-$(CONFIG_SYSCTL_SYSCALL_CHECK) += sysctl_check.o
kernel/mudflap.c new file +105 -0
105 lines not on this page · raw message
lib/Kconfig.debug +7 -0
@@ -35,6 +35,13 @@ config FRAME_WARN Setting this too low will cause a lot of warnings. Setting it to 0 disables the warning. Requires gcc 4.4 +config MUDFLAP + bool "Enable mudflap to check slab write after free" + depends on DEBUG_SLAB_LEAK || SLUB_DEBUG_ON + help + This uses mudflap which introduced by gcc4 to check slab corruption + which is caused by writing after free. + This is smaller and faster than kmemcheck. config MAGIC_SYSRQ bool "Magic SysRq key"
mm/slab.c +40 -0
@@ -4503,3 +4503,43 @@ size_t ksize(const void *objp)return obj_size (virt_to_cache (objp)); }EXPORT_SYMBOL (ksize); + +#ifdef CONFIG_MUDFLAP +void slab_check_write (void *ptr,unsigned int sz,const char *location) +{ +struct page *page; +struct kmem_cache *cache; +struct slab *slab; +char *realobj; +void *obj; +unsigned long objnr, size; + + page =virt_to_head_page (ptr); +if (!PageSlab (page)) +return ; + + cache =page_get_cache (page); + slab =page_get_slab (page); + objnr = (unsigned )(ptr - slab->s_mem) / cache->buffer_size; + obj =index_to_obj (cache, slab, objnr); + realobj = obj +obj_offset (cache); + size =obj_size (cache); + +if (slab_bufctl (slab)[objnr] !=BUFCTL_FREE ) +return ; + +printk (KERN_ERR "ptr: %p, sz: %d, location: %s\n" , + ptr, sz, location); +printk (KERN_ERR "write to freed object, size %ld\n" , + size); +if (cache->flags &SLAB_STORE_USER ) { +printk (KERN_ERR "Last user: [<%p>]" , + *dbg_userword (cache, obj)); +print_symbol ("(%s)" , + (unsigned long )*dbg_userword (cache, obj)); +printk ("\n" ); + } + +dump_stack (); +} +#endif
mm/slub.c +28 -0
@@ -4729,3 +4729,31 @@ static int __init slab_proc_init(void) }module_init (slab_proc_init);#endif /* CONFIG_SLABINFO */ + +#ifdef CONFIG_MUDFLAP +void slab_check_write (void *ptr,unsigned int sz,const char *location) +{ +struct page *page; +struct kmem_cache *s; +void *obj, *start; +unsigned long objnr; + + page =virt_to_head_page (ptr); +if (!PageSlab (page)) +return ; + + s = page->slab; + start =page_address (page); + objnr = (unsigned long )(ptr - start) / s->size; + obj = start + s->size * objnr; +if (!on_freelist (s, page, obj)) +return ; + +printk (KERN_ERR "ptr: %p, sz: %d, location: %s\n" , + ptr, sz, location); +printk (KERN_ERR "write to freed object, size %d\n" , + s->size); +print_tracking (s, obj); +dump_stack (); +} +#endif