Skip to content

Commit 5934b1b

Browse files
Matthew Wilcox (Oracle)tehcaster
authored andcommitted
usercopy: Remove folio references from check_heap_object()
Use page_slab() instead of virt_to_folio() followed by folio_slab(). We do end up calling compound_head() twice for non-slab copies, but that will not be a problem once we allocate memdescs separately. Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Kees Cook <kees@kernel.org> Cc: Gustavo A. R. Silva <gustavoars@kernel.org> Cc: linux-hardening@vger.kernel.org Link: https://patch.msgid.link/20251113000932.1589073-14-willy@infradead.org Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent 025f5b8 commit 5934b1b

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

mm/usercopy.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
164164
{
165165
unsigned long addr = (unsigned long)ptr;
166166
unsigned long offset;
167-
struct folio *folio;
167+
struct page *page;
168+
struct slab *slab;
168169

169170
if (is_kmap_addr(ptr)) {
170171
offset = offset_in_page(ptr);
@@ -189,16 +190,23 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
189190
if (!virt_addr_valid(ptr))
190191
return;
191192

192-
folio = virt_to_folio(ptr);
193-
194-
if (folio_test_slab(folio)) {
193+
page = virt_to_page(ptr);
194+
slab = page_slab(page);
195+
if (slab) {
195196
/* Check slab allocator for flags and size. */
196-
__check_heap_object(ptr, n, folio_slab(folio), to_user);
197-
} else if (folio_test_large(folio)) {
198-
offset = ptr - folio_address(folio);
199-
if (n > folio_size(folio) - offset)
197+
__check_heap_object(ptr, n, slab, to_user);
198+
} else if (PageCompound(page)) {
199+
page = compound_head(page);
200+
offset = ptr - page_address(page);
201+
if (n > page_size(page) - offset)
200202
usercopy_abort("page alloc", NULL, to_user, offset, n);
201203
}
204+
205+
/*
206+
* We cannot check non-compound pages. They might be part of
207+
* a large allocation, in which case crossing a page boundary
208+
* is fine.
209+
*/
202210
}
203211

204212
DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_HARDENED_USERCOPY_DEFAULT_ON,

0 commit comments

Comments
 (0)