Skip to content

Commit 4e140f5

Browse files
Matthew Wilcox (Oracle)kees
authored andcommitted
mm/usercopy: Check kmap addresses properly
If you are copying to an address in the kmap region, you may not copy across a page boundary, no matter what the size of the underlying allocation. You can't kmap() a slab page because slab pages always come from low memory. Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Acked-by: Kees Cook <[email protected]> Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent a199448 commit 4e140f5

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

arch/x86/include/asm/highmem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <asm/tlbflush.h>
2727
#include <asm/paravirt.h>
2828
#include <asm/fixmap.h>
29+
#include <asm/pgtable_areas.h>
2930

3031
/* declarations for highmem.c */
3132
extern unsigned long highstart_pfn, highend_pfn;

include/linux/highmem-internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ static inline void totalhigh_pages_add(long count)
149149
atomic_long_add(count, &_totalhigh_pages);
150150
}
151151

152+
static inline bool is_kmap_addr(const void *x)
153+
{
154+
unsigned long addr = (unsigned long)x;
155+
return addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP);
156+
}
152157
#else /* CONFIG_HIGHMEM */
153158

154159
static inline struct page *kmap_to_page(void *addr)
@@ -234,6 +239,11 @@ static inline void __kunmap_atomic(void *addr)
234239
static inline unsigned int nr_free_highpages(void) { return 0; }
235240
static inline unsigned long totalhigh_pages(void) { return 0UL; }
236241

242+
static inline bool is_kmap_addr(const void *x)
243+
{
244+
return false;
245+
}
246+
237247
#endif /* CONFIG_HIGHMEM */
238248

239249
/*

mm/usercopy.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,16 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
229229
if (!virt_addr_valid(ptr))
230230
return;
231231

232-
/*
233-
* When CONFIG_HIGHMEM=y, kmap_to_page() will give either the
234-
* highmem page or fallback to virt_to_page(). The following
235-
* is effectively a highmem-aware virt_to_slab().
236-
*/
237-
folio = page_folio(kmap_to_page((void *)ptr));
232+
if (is_kmap_addr(ptr)) {
233+
unsigned long page_end = (unsigned long)ptr | (PAGE_SIZE - 1);
234+
235+
if ((unsigned long)ptr + n - 1 > page_end)
236+
usercopy_abort("kmap", NULL, to_user,
237+
offset_in_page(ptr), n);
238+
return;
239+
}
240+
241+
folio = virt_to_folio(ptr);
238242

239243
if (folio_test_slab(folio)) {
240244
/* Check slab allocator for flags and size. */

0 commit comments

Comments
 (0)