Skip to contentSkip to conversation

Assorted improvements to usercopy

PATCH Superseded Matthew Wilcox (Oracle) 2 participants 6 messages Oct 6, 2021

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Assorted improvements to usercopy

We must prohibit page boundary crossing for kmap() addresses. vmap() addresses are limited by the length of the mapping, and compound pages are limited by the size of the page.

These should probably all have test cases?

v2:
- Prevent a NULL pointer dereference when a vmalloc-range pointer doesn't have an associated allocation (me)
- Report better offsets than "0" (Kees)

Matthew Wilcox (Oracle) (3):
mm/usercopy: Check kmap addresses properly
mm/usercopy: Detect vmalloc overruns
mm/usercopy: Detect compound page overruns

 arch/x86/include/asm/highmem.h   |  1 +
 include/linux/highmem-internal.h | 10 ++++++++
 mm/usercopy.c                    | 42 +++++++++++++++++++++++---------
 3 files changed, 42 insertions(+), 11 deletions(-)
Signature · 1 line
2.32.0
#1 lore raw

PATCH v2 1/3 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.

---
 arch/x86/include/asm/highmem.h   |  1 +
 include/linux/highmem-internal.h | 10 ++++++++++
 mm/usercopy.c                    | 16 ++++++++++------
 3 files changed, 21 insertions(+), 6 deletions(-)
arch/x86/include/asm/highmem.h +1 -0
index 032e020853aa..731ee7cc40a5 100644
@@ -26,6 +26,7 @@
 #include <asm/tlbflush.h>
 #include <asm/paravirt.h>
 #include <asm/fixmap.h>
+#include <asm/pgtable_areas.h>
 
 /* declarations for highmem.c */
 extern unsigned long highstart_pfn, highend_pfn;
include/linux/highmem-internal.h +10 -0
index 4aa1031d3e4c..97d6dc836749 100644
@@ -143,6 +143,11 @@ static inline void totalhigh_pages_add(long count)
 	atomic_long_add(count, &_totalhigh_pages);
 }
 
+static inline bool is_kmap_addr(const void *x)
+{
+	unsigned long addr = (unsigned long)x;
+	return addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP);
+}
 #else /* CONFIG_HIGHMEM */
 
 static inline struct page *kmap_to_page(void *addr)
@@ -223,6 +228,11 @@ static inline void __kunmap_atomic(void *addr)
 static inline unsigned int nr_free_highpages(void) { return 0; }
 static inline unsigned long totalhigh_pages(void) { return 0UL; }
 
+static inline bool is_kmap_addr(const void *x)
+{
+	return false;
+}
+
 #endif /* CONFIG_HIGHMEM */
 
 /*
mm/usercopy.c +10 -6
index b3de3c4eefba..8c039302465f 100644
@@ -228,12 +228,16 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
 	if (!virt_addr_valid(ptr))
 		return;
 
-	/*
-	 * When CONFIG_HIGHMEM=y, kmap_to_page() will give either the
-	 * highmem page or fallback to virt_to_page(). The following
-	 * is effectively a highmem-aware virt_to_head_page().
-	 */
-	page = compound_head(kmap_to_page((void *)ptr));
+	if (is_kmap_addr(ptr)) {
+		unsigned long page_end = (unsigned long)ptr | (PAGE_SIZE - 1);
+
+		if ((unsigned long)ptr + n - 1 > page_end)
+			usercopy_abort("kmap", NULL, to_user,
+					offset_in_page(ptr), n);
+		return;
+	}
+
+	page = virt_to_head_page(ptr);
 
 	if (PageSlab(page)) {
 		/* Check slab allocator for flags and size. */
Signature · 1 line
2.32.0
#2 lore raw

PATCH v2 2/3 mm/usercopy: Detect vmalloc overruns

In reply to Matthew Wilcox (Oracle)

If you have a vmalloc() allocation, or an address from calling vmap(), you cannot overrun the vm_area which describes it, regardless of the size of the underlying allocation. This probably doesn't do much for security because vmalloc comes with guard pages these days, but it prevents usercopy aborts when copying to a vmap() of smaller pages.

---
 mm/usercopy.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
mm/usercopy.c +16 -0
index 8c039302465f..63476e1506e0 100644
@@ -17,6 +17,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/task_stack.h>
 #include <linux/thread_info.h>
+#include <linux/vmalloc.h>
 #include <linux/atomic.h>
 #include <linux/jump_label.h>
 #include <asm/sections.h>
@@ -237,6 +238,21 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
 		return;
 	}
 
+	if (is_vmalloc_addr(ptr)) {
+		struct vm_struct *vm = find_vm_area(ptr);
+		unsigned long offset;
+
+		if (!vm) {
+			usercopy_abort("vmalloc", "no area", to_user, 0, n);
+			return;
+		}
+
+		offset = ptr - vm->addr;
+		if (offset + n > vm->size)
+			usercopy_abort("vmalloc", NULL, to_user, offset, n);
+		return;
+	}
+
 	page = virt_to_head_page(ptr);
 
 	if (PageSlab(page)) {
Signature · 1 line
2.32.0
#3 lore raw

PATCH v2 3/3 mm/usercopy: Detect compound page overruns

In reply to Matthew Wilcox (Oracle)

Move the compound page overrun detection out of CONFIG_HARDENED_USERCOPY_PAGESPAN so it's enabled for more people.

---
 mm/usercopy.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/usercopy.c +5 -5
index 63476e1506e0..b825c4344917 100644
@@ -194,11 +194,6 @@ static inline void check_page_span(const void *ptr, unsigned long n,
 		   ((unsigned long)end & (unsigned long)PAGE_MASK)))
 		return;
 
-	/* Allow if fully inside the same compound (__GFP_COMP) page. */
-	endpage = virt_to_head_page(end);
-	if (likely(endpage == page))
-		return;
-
 	/*
 	 * Reject if range is entirely either Reserved (i.e. special or
 	 * device memory), or CMA. Otherwise, reject since the object spans
@@ -258,6 +253,11 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
 	if (PageSlab(page)) {
 		/* Check slab allocator for flags and size. */
 		__check_heap_object(ptr, n, page, to_user);
+	} else if (PageHead(page)) {
+		/* A compound allocation */
+		unsigned long offset = ptr - page_address(page);
+		if (offset + n > page_size(page))
+			usercopy_abort("page alloc", NULL, to_user, offset, n);
 	} else {
 		/* Verify object does not incorrectly span multiple pages. */
 		check_page_span(ptr, n, page, to_user);
Signature · 1 line
2.32.0
#4 lore raw

Re: [PATCH v2 3/3] mm/usercopy: Detect compound page overruns

Quoted 15 lines · Matthew Wilcox (Oracle)

Move the compound page overrun detection out of CONFIG_HARDENED_USERCOPY_PAGESPAN so it's enabled for more people.

---
 mm/usercopy.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/usercopy.c +0 -0
index 63476e1506e0..b825c4344917 100644
@@ -194,11 +194,6 @@ static inline void check_page_span(const void *ptr, unsigned long n,
 		   ((unsigned long)end & (unsigned long)PAGE_MASK)))

mm/usercopy.c static inline void check_page_span(const void *ptr, unsigned long n, · line 199

Quoted 10 lines
mm/usercopy.c +0 -5
 		return;
 
-	/* Allow if fully inside the same compound (__GFP_COMP) page. */
-	endpage = virt_to_head_page(end);
-	if (likely(endpage == page))
-		return;
-
 	/*
 	 * Reject if range is entirely either Reserved (i.e. special or
 	 * device memory), or CMA. Otherwise, reject since the object spans

Needs an extra hunk to avoid a warning with that config:

@@ -163,7 +163,6 @@ static inline void check_page_span(const void *ptr, unsigned long n,
{

 #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
        const void *end = ptr + n - 1;
-       struct page *endpage;
        bool is_reserved, is_cma;

        /*

I'll wait a few days and send a v3.

#5 lore raw

Re: [PATCH v2 3/3] mm/usercopy: Detect compound page overruns

Quoted 18 lines · Matthew Wilcox
Quoted 17 lines · Matthew Wilcox (Oracle)

Move the compound page overrun detection out of CONFIG_HARDENED_USERCOPY_PAGESPAN so it's enabled for more people.

---
 mm/usercopy.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/usercopy.c +0 -0
index 63476e1506e0..b825c4344917 100644
@@ -194,11 +194,6 @@ static inline void check_page_span(const void *ptr, unsigned long n,
 		   ((unsigned long)end & (unsigned long)PAGE_MASK)))
 		return;
 
Quoted 10 lines

mm/usercopy.c static inline void check_page_span(const void *ptr, unsigned long n, · line 199

Quoted 8 lines
mm/usercopy.c +0 -5
-	/* Allow if fully inside the same compound (__GFP_COMP) page. */
-	endpage = virt_to_head_page(end);
-	if (likely(endpage == page))
-		return;
-
 	/*
 	 * Reject if range is entirely either Reserved (i.e. special or
 	 * device memory), or CMA. Otherwise, reject since the object spans

Needs an extra hunk to avoid a warning with that config:

Ah yeah, good catch.

Quoted 11 lines
(quoted diff) +0 -1
@@ -163,7 +163,6 @@ static inline void check_page_span(const void *ptr, unsigned long n,
 {
 #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
        const void *end = ptr + n - 1;
-       struct page *endpage;
        bool is_reserved, is_cma;
 
        /*

I'll wait a few days and send a v3.

When you send v3, can you CC [email protected] too?

Thanks for poking at this!

-Kees

Signature · 1 line
Kees Cook