Message ID | 20211214162050.660953-17-glider@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Add KernelMemorySanitizer infrastructure | expand |
On 12/14/21 17:20, Alexander Potapenko wrote: > In order to report uninitialized memory coming from heap allocations > KMSAN has to poison them unless they're created with __GFP_ZERO. > > It's handy that we need KMSAN hooks in the places where > init_on_alloc/init_on_free initialization is performed. > > Signed-off-by: Alexander Potapenko <glider@google.com> > --- > Link: https://linux-review.googlesource.com/id/I6954b386c5c5d7f99f48bb6cbcc74b75136ce86e > --- > mm/slab.h | 1 + > mm/slub.c | 26 +++++++++++++++++++++++--- > 2 files changed, 24 insertions(+), 3 deletions(-) > > diff --git a/mm/slab.h b/mm/slab.h > index 56ad7eea3ddfb..6175a74047b47 100644 > --- a/mm/slab.h > +++ b/mm/slab.h > @@ -521,6 +521,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, > memset(p[i], 0, s->object_size); > kmemleak_alloc_recursive(p[i], s->object_size, 1, > s->flags, flags); > + kmsan_slab_alloc(s, p[i], flags); > } > > memcg_slab_post_alloc_hook(s, objcg, flags, size, p); > diff --git a/mm/slub.c b/mm/slub.c > index abe7db581d686..5a63486e52531 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -22,6 +22,7 @@ > #include <linux/proc_fs.h> > #include <linux/seq_file.h> > #include <linux/kasan.h> > +#include <linux/kmsan.h> > #include <linux/cpu.h> > #include <linux/cpuset.h> > #include <linux/mempolicy.h> > @@ -346,10 +347,13 @@ static inline void *freelist_dereference(const struct kmem_cache *s, > (unsigned long)ptr_addr); > } > > +/* > + * See the comment to get_freepointer_safe(). > + */ I did... > static inline void *get_freepointer(struct kmem_cache *s, void *object) > { > object = kasan_reset_tag(object); > - return freelist_dereference(s, object + s->offset); > + return kmsan_init(freelist_dereference(s, object + s->offset)); ... but I don't see why it applies to get_freepointer() too? What am I missing? > } > > static void prefetch_freepointer(const struct kmem_cache *s, void *object) > @@ -357,18 +361,28 @@ static void prefetch_freepointer(const struct kmem_cache *s, void *object) > prefetchw(object + s->offset); > } > > +/* > + * When running under KMSAN, get_freepointer_safe() may return an uninitialized > + * pointer value in the case the current thread loses the race for the next > + * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in > + * slab_alloc_node() will fail, so the uninitialized value won't be used, but > + * KMSAN will still check all arguments of cmpxchg because of imperfect > + * handling of inline assembly. > + * To work around this problem, use kmsan_init() to force initialize the > + * return value of get_freepointer_safe(). > + */ > static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) > { > unsigned long freepointer_addr; > void *p; > > if (!debug_pagealloc_enabled_static()) > - return get_freepointer(s, object); > + return kmsan_init(get_freepointer(s, object)); So here kmsan_init() is done twice? > > object = kasan_reset_tag(object); > freepointer_addr = (unsigned long)object + s->offset; > copy_from_kernel_nofault(&p, (void **)freepointer_addr, sizeof(p)); > - return freelist_ptr(s, p, freepointer_addr); > + return kmsan_init(freelist_ptr(s, p, freepointer_addr)); > } > > static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
> > static inline void *get_freepointer(struct kmem_cache *s, void *object) > > { > > object = kasan_reset_tag(object); > > - return freelist_dereference(s, object + s->offset); > > + return kmsan_init(freelist_dereference(s, object + s->offset)); > > ... but I don't see why it applies to get_freepointer() too? What am I missing? Agreed, kmsan_init() is not needed here. > > } > > > > static void prefetch_freepointer(const struct kmem_cache *s, void *object) > > @@ -357,18 +361,28 @@ static void prefetch_freepointer(const struct kmem_cache *s, void *object) > > prefetchw(object + s->offset); > > } > > > > +/* > > + * When running under KMSAN, get_freepointer_safe() may return an uninitialized > > + * pointer value in the case the current thread loses the race for the next > > + * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in > > + * slab_alloc_node() will fail, so the uninitialized value won't be used, but > > + * KMSAN will still check all arguments of cmpxchg because of imperfect > > + * handling of inline assembly. > > + * To work around this problem, use kmsan_init() to force initialize the > > + * return value of get_freepointer_safe(). > > + */ > > static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) > > { > > unsigned long freepointer_addr; > > void *p; > > > > if (!debug_pagealloc_enabled_static()) > > - return get_freepointer(s, object); > > + return kmsan_init(get_freepointer(s, object)); > > So here kmsan_init() is done twice? Yeah, removing it from get_freepointer() does not introduce new errors. I'll fix this in v2.
diff --git a/mm/slab.h b/mm/slab.h index 56ad7eea3ddfb..6175a74047b47 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -521,6 +521,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, memset(p[i], 0, s->object_size); kmemleak_alloc_recursive(p[i], s->object_size, 1, s->flags, flags); + kmsan_slab_alloc(s, p[i], flags); } memcg_slab_post_alloc_hook(s, objcg, flags, size, p); diff --git a/mm/slub.c b/mm/slub.c index abe7db581d686..5a63486e52531 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -22,6 +22,7 @@ #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/kasan.h> +#include <linux/kmsan.h> #include <linux/cpu.h> #include <linux/cpuset.h> #include <linux/mempolicy.h> @@ -346,10 +347,13 @@ static inline void *freelist_dereference(const struct kmem_cache *s, (unsigned long)ptr_addr); } +/* + * See the comment to get_freepointer_safe(). + */ static inline void *get_freepointer(struct kmem_cache *s, void *object) { object = kasan_reset_tag(object); - return freelist_dereference(s, object + s->offset); + return kmsan_init(freelist_dereference(s, object + s->offset)); } static void prefetch_freepointer(const struct kmem_cache *s, void *object) @@ -357,18 +361,28 @@ static void prefetch_freepointer(const struct kmem_cache *s, void *object) prefetchw(object + s->offset); } +/* + * When running under KMSAN, get_freepointer_safe() may return an uninitialized + * pointer value in the case the current thread loses the race for the next + * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in + * slab_alloc_node() will fail, so the uninitialized value won't be used, but + * KMSAN will still check all arguments of cmpxchg because of imperfect + * handling of inline assembly. + * To work around this problem, use kmsan_init() to force initialize the + * return value of get_freepointer_safe(). + */ static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) { unsigned long freepointer_addr; void *p; if (!debug_pagealloc_enabled_static()) - return get_freepointer(s, object); + return kmsan_init(get_freepointer(s, object)); object = kasan_reset_tag(object); freepointer_addr = (unsigned long)object + s->offset; copy_from_kernel_nofault(&p, (void **)freepointer_addr, sizeof(p)); - return freelist_ptr(s, p, freepointer_addr); + return kmsan_init(freelist_ptr(s, p, freepointer_addr)); } static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) @@ -1678,6 +1692,7 @@ static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags) ptr = kasan_kmalloc_large(ptr, size, flags); /* As ptr might get tagged, call kmemleak hook after KASAN. */ kmemleak_alloc(ptr, size, 1, flags); + kmsan_kmalloc_large(ptr, size, flags); return ptr; } @@ -1685,12 +1700,14 @@ static __always_inline void kfree_hook(void *x) { kmemleak_free(x); kasan_kfree_large(x); + kmsan_kfree_large(x); } static __always_inline bool slab_free_hook(struct kmem_cache *s, void *x, bool init) { kmemleak_free_recursive(x, s->flags); + kmsan_slab_free(s, x); debug_check_no_locks_freed(x, s->object_size); @@ -3729,6 +3746,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, */ slab_post_alloc_hook(s, objcg, flags, size, p, slab_want_init_on_alloc(flags, s)); + return i; error: slub_put_cpu_ptr(s->cpu_slab); @@ -5905,6 +5923,7 @@ static char *create_unique_id(struct kmem_cache *s) p += sprintf(p, "%07u", s->size); BUG_ON(p > name + ID_STR_LENGTH - 1); + kmsan_unpoison_memory(name, p - name); return name; } @@ -6006,6 +6025,7 @@ static int sysfs_slab_alias(struct kmem_cache *s, const char *name) al->name = name; al->next = alias_list; alias_list = al; + kmsan_unpoison_memory(al, sizeof(struct saved_alias)); return 0; }
In order to report uninitialized memory coming from heap allocations KMSAN has to poison them unless they're created with __GFP_ZERO. It's handy that we need KMSAN hooks in the places where init_on_alloc/init_on_free initialization is performed. Signed-off-by: Alexander Potapenko <glider@google.com> --- Link: https://linux-review.googlesource.com/id/I6954b386c5c5d7f99f48bb6cbcc74b75136ce86e --- mm/slab.h | 1 + mm/slub.c | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)