From patchwork Thu Dec 16 21:53:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 12682825 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F6E0C433F5 for ; Thu, 16 Dec 2021 21:55:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8D806B0075; Thu, 16 Dec 2021 16:54:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A3BE86B0078; Thu, 16 Dec 2021 16:54:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 903766B007B; Thu, 16 Dec 2021 16:54:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0030.hostedemail.com [216.40.44.30]) by kanga.kvack.org (Postfix) with ESMTP id 7DDBF6B0075 for ; Thu, 16 Dec 2021 16:54:14 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 308FD8249980 for ; Thu, 16 Dec 2021 21:54:04 +0000 (UTC) X-FDA: 78925010808.04.7B56DDB Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf15.hostedemail.com (Postfix) with ESMTP id E6999A0018 for ; Thu, 16 Dec 2021 21:53:58 +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=jtkiEbeC8BH5Pq80FDCgjIrecOy7gCyzuNpIoqv5nX0=; b=DJwMtd4jnj3lLUZ0yZEetfivZS yHv/lAf3GgRuYuwfu+huJY7nUxInXVVeByfxdMGwS5hqgKQJ84T2U8CKSkeyGAiHgx8gkHXfZcgoG sKBEaiNe5/E1+e0r4Tpn6imTF3zEb0kR7cV63MHAw5vyfVgKmw0mWBepC4nLc8yORAoFz8+vOs0+Z ovsmwRtjUjuwwhdV0gD5gEDHCYxnafQGZNkPVAOdvCv8oprVEhVeBksy+xwLZaKni5TwIlT2AcUpF mBOd4vqvve/ATY6CGdt7feEV3gC/hWEPRGaJCNGVlfeYhingGDFntV5fwgvkM+thA2//m/JRAmgEi PDhy927A==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mxyhE-00FzYA-RJ; Thu, 16 Dec 2021 21:53:52 +0000 From: "Matthew Wilcox (Oracle)" To: Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-hardening@vger.kernel.org, William Kucharski Subject: [PATCH v4 3/4] mm/usercopy: Detect compound page overruns Date: Thu, 16 Dec 2021 21:53:50 +0000 Message-Id: <20211216215351.3811471-4-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211216215351.3811471-1-willy@infradead.org> References: <20211216215351.3811471-1-willy@infradead.org> MIME-Version: 1.0 Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=DJwMtd4j; dmarc=none; spf=none (imf15.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E6999A0018 X-Stat-Signature: 9du35np7dw1pgju47p8bmpuq9qo74k7k X-HE-Tag: 1639691638-444924 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: Move the compound page overrun detection out of CONFIG_HARDENED_USERCOPY_PAGESPAN so it's enabled for more people. Signed-off-by: Matthew Wilcox (Oracle) Acked-by: Kees Cook Reviewed-by: William Kucharski --- mm/usercopy.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mm/usercopy.c b/mm/usercopy.c index 63476e1506e0..db2e8c4f79fd 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -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; /* @@ -194,11 +193,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 +252,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);