Skip to content

Commit 0b3eb09

Browse files
Matthew Wilcox (Oracle)tehcaster
authored andcommitted
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. [ [email protected]: in check_heap_object() only convert to struct slab for actual PageSlab pages; use folio as intermediate step instead of page ] Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]> Reviewed-by: Roman Gushchin <[email protected]>
1 parent 7213230 commit 0b3eb09

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

include/linux/slab.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,6 @@ bool kmem_valid_obj(void *object);
189189
void kmem_dump_obj(void *object);
190190
#endif
191191

192-
#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
193-
void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
194-
bool to_user);
195-
#else
196-
static inline void __check_heap_object(const void *ptr, unsigned long n,
197-
struct page *page, bool to_user) { }
198-
#endif
199-
200192
/*
201193
* Some archs want to perform DMA into kmalloc caches and need a guaranteed
202194
* alignment larger than the alignment of a 64-bit integer.

mm/slab.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ static void **dbg_userword(struct kmem_cache *cachep, void *objp)
372372
static int slab_max_order = SLAB_MAX_ORDER_LO;
373373
static bool slab_max_order_set __initdata;
374374

375-
static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
376-
unsigned int idx)
375+
static inline void *index_to_obj(struct kmem_cache *cache,
376+
const struct page *page, unsigned int idx)
377377
{
378378
return page->s_mem + cache->size * idx;
379379
}
@@ -4166,8 +4166,8 @@ ssize_t slabinfo_write(struct file *file, const char __user *buffer,
41664166
* Returns NULL if check passes, otherwise const char * to name of cache
41674167
* to indicate an error.
41684168
*/
4169-
void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
4170-
bool to_user)
4169+
void __check_heap_object(const void *ptr, unsigned long n,
4170+
const struct slab *slab, bool to_user)
41714171
{
41724172
struct kmem_cache *cachep;
41734173
unsigned int objnr;
@@ -4176,15 +4176,15 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
41764176
ptr = kasan_reset_tag(ptr);
41774177

41784178
/* Find and validate object. */
4179-
cachep = page->slab_cache;
4180-
objnr = obj_to_index(cachep, page, (void *)ptr);
4179+
cachep = slab->slab_cache;
4180+
objnr = obj_to_index(cachep, slab_page(slab), (void *)ptr);
41814181
BUG_ON(objnr >= cachep->num);
41824182

41834183
/* Find offset within object. */
41844184
if (is_kfence_address(ptr))
41854185
offset = ptr - kfence_object_start(ptr);
41864186
else
4187-
offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep);
4187+
offset = ptr - index_to_obj(cachep, slab_page(slab), objnr) - obj_offset(cachep);
41884188

41894189
/* Allow address range falling entirely within usercopy region. */
41904190
if (offset >= cachep->useroffset &&

mm/slab.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,15 @@ struct kmem_obj_info {
812812
void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
813813
#endif
814814

815+
#ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
816+
void __check_heap_object(const void *ptr, unsigned long n,
817+
const struct slab *slab, bool to_user);
818+
#else
819+
static inline
820+
void __check_heap_object(const void *ptr, unsigned long n,
821+
const struct slab *slab, bool to_user)
822+
{
823+
}
824+
#endif
825+
815826
#endif /* MM_SLAB_H */

mm/slub.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,8 +4485,8 @@ EXPORT_SYMBOL(__kmalloc_node);
44854485
* Returns NULL if check passes, otherwise const char * to name of cache
44864486
* to indicate an error.
44874487
*/
4488-
void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
4489-
bool to_user)
4488+
void __check_heap_object(const void *ptr, unsigned long n,
4489+
const struct slab *slab, bool to_user)
44904490
{
44914491
struct kmem_cache *s;
44924492
unsigned int offset;
@@ -4495,18 +4495,18 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
44954495
ptr = kasan_reset_tag(ptr);
44964496

44974497
/* Find object and usable object size. */
4498-
s = page->slab_cache;
4498+
s = slab->slab_cache;
44994499

45004500
/* Reject impossible pointers. */
4501-
if (ptr < page_address(page))
4501+
if (ptr < slab_address(slab))
45024502
usercopy_abort("SLUB object not in SLUB page?!", NULL,
45034503
to_user, 0, n);
45044504

45054505
/* Find offset within object. */
45064506
if (is_kfence)
45074507
offset = ptr - kfence_object_start(ptr);
45084508
else
4509-
offset = (ptr - page_address(page)) % s->size;
4509+
offset = (ptr - slab_address(slab)) % s->size;
45104510

45114511
/* Adjust for redzone and reject if within the redzone. */
45124512
if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) {

mm/usercopy.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/atomic.h>
2121
#include <linux/jump_label.h>
2222
#include <asm/sections.h>
23+
#include "slab.h"
2324

2425
/*
2526
* Checks if a given pointer and length is contained by the current
@@ -223,24 +224,24 @@ static inline void check_page_span(const void *ptr, unsigned long n,
223224
static inline void check_heap_object(const void *ptr, unsigned long n,
224225
bool to_user)
225226
{
226-
struct page *page;
227+
struct folio *folio;
227228

228229
if (!virt_addr_valid(ptr))
229230
return;
230231

231232
/*
232233
* When CONFIG_HIGHMEM=y, kmap_to_page() will give either the
233234
* highmem page or fallback to virt_to_page(). The following
234-
* is effectively a highmem-aware virt_to_head_page().
235+
* is effectively a highmem-aware virt_to_slab().
235236
*/
236-
page = compound_head(kmap_to_page((void *)ptr));
237+
folio = page_folio(kmap_to_page((void *)ptr));
237238

238-
if (PageSlab(page)) {
239+
if (folio_test_slab(folio)) {
239240
/* Check slab allocator for flags and size. */
240-
__check_heap_object(ptr, n, page, to_user);
241+
__check_heap_object(ptr, n, folio_slab(folio), to_user);
241242
} else {
242243
/* Verify object does not incorrectly span multiple pages. */
243-
check_page_span(ptr, n, page, to_user);
244+
check_page_span(ptr, n, folio_page(folio, 0), to_user);
244245
}
245246
}
246247

0 commit comments

Comments
 (0)