提交 0b3eb091 编写于 作者: M Matthew Wilcox (Oracle) 提交者: Vlastimil Babka

mm: Convert check_heap_object() to use struct slab

Ensure that we're not seeing a tail page inside __check_heap_object() by
converting to a slab instead of a page.  Take the opportunity to mark
the slab as const since we're not modifying it.  Also move the
declaration of __check_heap_object() to mm/slab.h so it's not available
to the wider kernel.

[ vbabka@suse.cz: in check_heap_object() only convert to struct slab for
  actual PageSlab pages; use folio as intermediate step instead of page ]
Signed-off-by: NMatthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: NVlastimil Babka <vbabka@suse.cz>
Reviewed-by: NRoman Gushchin <guro@fb.com>
上级 7213230a
...@@ -189,14 +189,6 @@ bool kmem_valid_obj(void *object); ...@@ -189,14 +189,6 @@ bool kmem_valid_obj(void *object);
void kmem_dump_obj(void *object); void kmem_dump_obj(void *object);
#endif #endif
#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
bool to_user);
#else
static inline void __check_heap_object(const void *ptr, unsigned long n,
struct page *page, bool to_user) { }
#endif
/* /*
* Some archs want to perform DMA into kmalloc caches and need a guaranteed * Some archs want to perform DMA into kmalloc caches and need a guaranteed
* alignment larger than the alignment of a 64-bit integer. * alignment larger than the alignment of a 64-bit integer.
......
...@@ -372,8 +372,8 @@ static void **dbg_userword(struct kmem_cache *cachep, void *objp) ...@@ -372,8 +372,8 @@ static void **dbg_userword(struct kmem_cache *cachep, void *objp)
static int slab_max_order = SLAB_MAX_ORDER_LO; static int slab_max_order = SLAB_MAX_ORDER_LO;
static bool slab_max_order_set __initdata; static bool slab_max_order_set __initdata;
static inline void *index_to_obj(struct kmem_cache *cache, struct page *page, static inline void *index_to_obj(struct kmem_cache *cache,
unsigned int idx) const struct page *page, unsigned int idx)
{ {
return page->s_mem + cache->size * idx; return page->s_mem + cache->size * idx;
} }
...@@ -4166,8 +4166,8 @@ ssize_t slabinfo_write(struct file *file, const char __user *buffer, ...@@ -4166,8 +4166,8 @@ ssize_t slabinfo_write(struct file *file, const char __user *buffer,
* Returns NULL if check passes, otherwise const char * to name of cache * Returns NULL if check passes, otherwise const char * to name of cache
* to indicate an error. * to indicate an error.
*/ */
void __check_heap_object(const void *ptr, unsigned long n, struct page *page, void __check_heap_object(const void *ptr, unsigned long n,
bool to_user) const struct slab *slab, bool to_user)
{ {
struct kmem_cache *cachep; struct kmem_cache *cachep;
unsigned int objnr; unsigned int objnr;
...@@ -4176,15 +4176,15 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page, ...@@ -4176,15 +4176,15 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
ptr = kasan_reset_tag(ptr); ptr = kasan_reset_tag(ptr);
/* Find and validate object. */ /* Find and validate object. */
cachep = page->slab_cache; cachep = slab->slab_cache;
objnr = obj_to_index(cachep, page, (void *)ptr); objnr = obj_to_index(cachep, slab_page(slab), (void *)ptr);
BUG_ON(objnr >= cachep->num); BUG_ON(objnr >= cachep->num);
/* Find offset within object. */ /* Find offset within object. */
if (is_kfence_address(ptr)) if (is_kfence_address(ptr))
offset = ptr - kfence_object_start(ptr); offset = ptr - kfence_object_start(ptr);
else else
offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep); offset = ptr - index_to_obj(cachep, slab_page(slab), objnr) - obj_offset(cachep);
/* Allow address range falling entirely within usercopy region. */ /* Allow address range falling entirely within usercopy region. */
if (offset >= cachep->useroffset && if (offset >= cachep->useroffset &&
......
...@@ -812,4 +812,15 @@ struct kmem_obj_info { ...@@ -812,4 +812,15 @@ struct kmem_obj_info {
void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab); void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
#endif #endif
#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
void __check_heap_object(const void *ptr, unsigned long n,
const struct slab *slab, bool to_user);
#else
static inline
void __check_heap_object(const void *ptr, unsigned long n,
const struct slab *slab, bool to_user)
{
}
#endif
#endif /* MM_SLAB_H */ #endif /* MM_SLAB_H */
...@@ -4485,8 +4485,8 @@ EXPORT_SYMBOL(__kmalloc_node); ...@@ -4485,8 +4485,8 @@ EXPORT_SYMBOL(__kmalloc_node);
* Returns NULL if check passes, otherwise const char * to name of cache * Returns NULL if check passes, otherwise const char * to name of cache
* to indicate an error. * to indicate an error.
*/ */
void __check_heap_object(const void *ptr, unsigned long n, struct page *page, void __check_heap_object(const void *ptr, unsigned long n,
bool to_user) const struct slab *slab, bool to_user)
{ {
struct kmem_cache *s; struct kmem_cache *s;
unsigned int offset; unsigned int offset;
...@@ -4495,10 +4495,10 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page, ...@@ -4495,10 +4495,10 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
ptr = kasan_reset_tag(ptr); ptr = kasan_reset_tag(ptr);
/* Find object and usable object size. */ /* Find object and usable object size. */
s = page->slab_cache; s = slab->slab_cache;
/* Reject impossible pointers. */ /* Reject impossible pointers. */
if (ptr < page_address(page)) if (ptr < slab_address(slab))
usercopy_abort("SLUB object not in SLUB page?!", NULL, usercopy_abort("SLUB object not in SLUB page?!", NULL,
to_user, 0, n); to_user, 0, n);
...@@ -4506,7 +4506,7 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page, ...@@ -4506,7 +4506,7 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
if (is_kfence) if (is_kfence)
offset = ptr - kfence_object_start(ptr); offset = ptr - kfence_object_start(ptr);
else else
offset = (ptr - page_address(page)) % s->size; offset = (ptr - slab_address(slab)) % s->size;
/* Adjust for redzone and reject if within the redzone. */ /* Adjust for redzone and reject if within the redzone. */
if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) { if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) {
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <linux/atomic.h> #include <linux/atomic.h>
#include <linux/jump_label.h> #include <linux/jump_label.h>
#include <asm/sections.h> #include <asm/sections.h>
#include "slab.h"
/* /*
* Checks if a given pointer and length is contained by the current * Checks if a given pointer and length is contained by the current
...@@ -223,7 +224,7 @@ static inline void check_page_span(const void *ptr, unsigned long n, ...@@ -223,7 +224,7 @@ static inline void check_page_span(const void *ptr, unsigned long n,
static inline void check_heap_object(const void *ptr, unsigned long n, static inline void check_heap_object(const void *ptr, unsigned long n,
bool to_user) bool to_user)
{ {
struct page *page; struct folio *folio;
if (!virt_addr_valid(ptr)) if (!virt_addr_valid(ptr))
return; return;
...@@ -231,16 +232,16 @@ static inline void check_heap_object(const void *ptr, unsigned long n, ...@@ -231,16 +232,16 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
/* /*
* When CONFIG_HIGHMEM=y, kmap_to_page() will give either the * When CONFIG_HIGHMEM=y, kmap_to_page() will give either the
* highmem page or fallback to virt_to_page(). The following * highmem page or fallback to virt_to_page(). The following
* is effectively a highmem-aware virt_to_head_page(). * is effectively a highmem-aware virt_to_slab().
*/ */
page = compound_head(kmap_to_page((void *)ptr)); folio = page_folio(kmap_to_page((void *)ptr));
if (PageSlab(page)) { if (folio_test_slab(folio)) {
/* Check slab allocator for flags and size. */ /* Check slab allocator for flags and size. */
__check_heap_object(ptr, n, page, to_user); __check_heap_object(ptr, n, folio_slab(folio), to_user);
} else { } else {
/* Verify object does not incorrectly span multiple pages. */ /* Verify object does not incorrectly span multiple pages. */
check_page_span(ptr, n, page, to_user); check_page_span(ptr, n, folio_page(folio, 0), to_user);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册