From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F7B3C433F5 for ; Wed, 6 Oct 2021 12:44:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C487E61177 for ; Wed, 6 Oct 2021 12:44:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C487E61177 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 4CFDB940007; Wed, 6 Oct 2021 08:44:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 47F456B0071; Wed, 6 Oct 2021 08:44:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36EC8940007; Wed, 6 Oct 2021 08:44:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0228.hostedemail.com [216.40.44.228]) by kanga.kvack.org (Postfix) with ESMTP id 284906B006C for ; Wed, 6 Oct 2021 08:44:21 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E106C18037D50 for ; Wed, 6 Oct 2021 12:44:20 +0000 (UTC) X-FDA: 78665980680.30.0410819 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf09.hostedemail.com (Postfix) with ESMTP id 7FE6D30008C7 for ; Wed, 6 Oct 2021 12:44:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=o9WmfUO+Fo5cx+wqyijCOv0cN5Ciqh5Mu4Par0voGQ8=; b=OQTJPRMnP9iK1ylqcGNb8daffX wW1kQCHotKMvFuljVY60WLF20z6pWqxWio7aufpD3wrYtwEs0fQIFVbAW9DZiOJGyS+O6W8kzsZjJ qUX5KFNQlQCTKHu6YvRvmYinkOQpPRtq4wYZ30fAxbMH+HnC5m9zlQeK9/L8uGu7vscCXXwNeoQ1B s0iO2ZhYiB5OKJPbQ5QEKwgSgP1V7430jtWZTo0uJylJFXne+kuqYEh5YnJq0NyPH1uADo9dnOv2P Q2KYZc5LxVui8U9VkpTxAa/6I/xFD9BYjDpLQIJgo+2eBmUYt8OPPcX4MRc3UDCMMe2yZ7aVdnhSE AnQZnM4g==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mY6GF-000sWe-S8; Wed, 06 Oct 2021 12:43:18 +0000 From: "Matthew Wilcox (Oracle)" To: Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Thomas Gleixner Subject: [PATCH v2 1/3] mm/usercopy: Check kmap addresses properly Date: Wed, 6 Oct 2021 13:42:24 +0100 Message-Id: <20211006124226.209484-2-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211006124226.209484-1-willy@infradead.org> References: <20211006124226.209484-1-willy@infradead.org> MIME-Version: 1.0 Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=OQTJPRMn; spf=none (imf09.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7FE6D30008C7 X-Stat-Signature: kubrmaixgwhaq79haex9zfa4hkwtzn95 X-HE-Tag: 1633524260-367977 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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) Acked-by: Kees Cook --- arch/x86/include/asm/highmem.h | 1 + include/linux/highmem-internal.h | 10 ++++++++++ mm/usercopy.c | 16 ++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/highmem.h b/arch/x86/include/asm/highme= m.h index 032e020853aa..731ee7cc40a5 100644 --- a/arch/x86/include/asm/highmem.h +++ b/arch/x86/include/asm/highmem.h @@ -26,6 +26,7 @@ #include #include #include +#include =20 /* declarations for highmem.c */ extern unsigned long highstart_pfn, highend_pfn; diff --git a/include/linux/highmem-internal.h b/include/linux/highmem-int= ernal.h index 4aa1031d3e4c..97d6dc836749 100644 --- a/include/linux/highmem-internal.h +++ b/include/linux/highmem-internal.h @@ -143,6 +143,11 @@ static inline void totalhigh_pages_add(long count) atomic_long_add(count, &_totalhigh_pages); } =20 +static inline bool is_kmap_addr(const void *x) +{ + unsigned long addr =3D (unsigned long)x; + return addr >=3D PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP); +} #else /* CONFIG_HIGHMEM */ =20 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; } =20 +static inline bool is_kmap_addr(const void *x) +{ + return false; +} + #endif /* CONFIG_HIGHMEM */ =20 /* diff --git a/mm/usercopy.c b/mm/usercopy.c index b3de3c4eefba..8c039302465f 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -228,12 +228,16 @@ static inline void check_heap_object(const void *pt= r, unsigned long n, if (!virt_addr_valid(ptr)) return; =20 - /* - * When CONFIG_HIGHMEM=3Dy, 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 =3D compound_head(kmap_to_page((void *)ptr)); + if (is_kmap_addr(ptr)) { + unsigned long page_end =3D (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 =3D virt_to_head_page(ptr); =20 if (PageSlab(page)) { /* Check slab allocator for flags and size. */ --=20 2.32.0