linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: Kees Cook <keescook@chromium.org>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
	linux-mm@kvack.org, Uladzislau Rezki <urezki@gmail.com>,
	Zorro Lang <zlang@redhat.com>,
	linux-xfs@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: [PATCH 3/3] usercopy: Make usercopy resilient against ridiculously large copies
Date: Sun, 12 Jun 2022 22:32:27 +0100	[thread overview]
Message-ID: <20220612213227.3881769-4-willy@infradead.org> (raw)
In-Reply-To: <20220612213227.3881769-1-willy@infradead.org>

If 'n' is so large that it's negative, we might wrap around and mistakenly
think that the copy is OK when it's not.  Such a copy would probably
crash, but just doing the arithmetic in a more simple way lets us detect
and refuse this case.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/usercopy.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/mm/usercopy.c b/mm/usercopy.c
index 31deee7dd2f5..ff16083cf1c8 100644
--- a/mm/usercopy.c
+++ b/mm/usercopy.c
@@ -162,20 +162,18 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
 				     bool to_user)
 {
 	uintptr_t addr = (uintptr_t)ptr;
+	unsigned long offset;
 	struct folio *folio;
 
 	if (is_kmap_addr(ptr)) {
-		unsigned long page_end = addr | (PAGE_SIZE - 1);
-
-		if (addr + n - 1 > page_end)
-			usercopy_abort("kmap", NULL, to_user,
-					offset_in_page(ptr), n);
+		offset = offset_in_page(ptr);
+		if (n > PAGE_SIZE - offset)
+			usercopy_abort("kmap", NULL, to_user, offset, n);
 		return;
 	}
 
 	if (is_vmalloc_addr(ptr)) {
 		struct vmap_area *area = find_vmap_area(addr);
-		unsigned long offset;
 
 		if (!area) {
 			usercopy_abort("vmalloc", "no area", to_user, 0, n);
@@ -184,9 +182,10 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
 
 		/* XXX: We should also abort for free vmap_areas */
 
-		offset = addr - area->va_start;
-		if (addr + n > area->va_end)
+		if (n > area->va_end - addr) {
+			offset = addr - area->va_start;
 			usercopy_abort("vmalloc", NULL, to_user, offset, n);
+		}
 		return;
 	}
 
@@ -199,8 +198,8 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
 		/* Check slab allocator for flags and size. */
 		__check_heap_object(ptr, n, folio_slab(folio), to_user);
 	} else if (folio_test_large(folio)) {
-		unsigned long offset = ptr - folio_address(folio);
-		if (offset + n > folio_size(folio))
+		offset = ptr - folio_address(folio);
+		if (n > folio_size(folio) - offset)
 			usercopy_abort("page alloc", NULL, to_user, offset, n);
 	}
 }
-- 
2.35.1



  parent reply	other threads:[~2022-06-12 21:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-12 21:32 [PATCH 0/3] Fixes for usercopy Matthew Wilcox (Oracle)
2022-06-12 21:32 ` [PATCH 1/3] usercopy: Handle vm_map_ram() areas Matthew Wilcox (Oracle)
2022-06-13 10:00   ` Uladzislau Rezki
2022-06-13 11:52     ` Baoquan He
2022-06-13 12:56       ` Uladzislau Rezki
2022-06-13 16:23   ` Kees Cook
2022-06-13 16:44     ` Matthew Wilcox
2022-06-13 17:02       ` Uladzislau Rezki
2022-06-13 17:04         ` Kees Cook
2022-06-12 21:32 ` [PATCH 2/3] usercopy: Cast pointer to an integer once Matthew Wilcox (Oracle)
2022-06-13  9:51   ` Uladzislau Rezki
2022-06-13 16:20     ` Kees Cook
2022-06-13 16:27       ` Uladzislau Rezki
2022-06-12 21:32 ` Matthew Wilcox (Oracle) [this message]
2022-06-13  9:57   ` [PATCH 3/3] usercopy: Make usercopy resilient against ridiculously large copies Uladzislau Rezki
2022-06-13  8:04 ` [PATCH 0/3] Fixes for usercopy Zorro Lang
2022-06-13 16:25 ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220612213227.3881769-4-willy@infradead.org \
    --to=willy@infradead.org \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=urezki@gmail.com \
    --cc=zlang@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox