From patchwork Mon Jan 10 23:15:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 12709264 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 0CA25C433EF for ; Mon, 10 Jan 2022 23:15:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A1CA6B0078; Mon, 10 Jan 2022 18:15:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 929C76B007B; Mon, 10 Jan 2022 18:15:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77D896B007D; Mon, 10 Jan 2022 18:15:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0175.hostedemail.com [216.40.44.175]) by kanga.kvack.org (Postfix) with ESMTP id 692BD6B0078 for ; Mon, 10 Jan 2022 18:15:44 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 20C548248D52 for ; Mon, 10 Jan 2022 23:15:44 +0000 (UTC) X-FDA: 79015936608.27.2185D5B Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf29.hostedemail.com (Postfix) with ESMTP id D10C112000B for ; Mon, 10 Jan 2022 23:15:43 +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=BHhXo+AiVU90OItLmEg1G7k1aWJm7bvB04PrbDkRSvg=; b=J0IBKvayJTZCRIbOwYHIOgFiCw PrQhqFhxJ0wdgWPH4IzsV9cfwFN6167MVzdwDY/IpXWVZWnZBlt/FRGbZ+A6ZnzKsiX34lRkBqw6C c2KGJERqfWWc/AFQZQpM9Cpt52CKGpHLvB+naedmQWMPcv60C8+oxGrbzqr2XDXQmCcxSzdwp2NW/ HYsvzJ4ZBoabfdQwXOd3PSlNCdmHIhWoQJiTGhrGYKewQQbXM8hfvsLggQOxlL818cKCtMximhMoV oq8wsJrgpuSUjSpFN1H0MM9oJD97WmWf2yQhFH5NQQiet6LqUfvu4sza8oMGkjQxvYAzmqE36tgj2 D9CD8egQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1n73sy-002nGJ-Hv; Mon, 10 Jan 2022 23:15:32 +0000 From: "Matthew Wilcox (Oracle)" To: Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: [PATCH 3/4] mm/usercopy: Detect large folio overruns Date: Mon, 10 Jan 2022 23:15:29 +0000 Message-Id: <20220110231530.665970-4-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220110231530.665970-1-willy@infradead.org> References: <20220110231530.665970-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: D10C112000B X-Stat-Signature: j3bm516ym7pibmtaofwosfgu8h6o3gna Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=J0IBKvay; dmarc=none; spf=none (imf29.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-HE-Tag: 1641856543-980935 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 and convert it to use folios so it's enabled for more people. Signed-off-by: Matthew Wilcox (Oracle) Acked-by: Kees Cook Reviewed-by: David Hildenbrand --- mm/usercopy.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mm/usercopy.c b/mm/usercopy.c index dcf71b7e3098..e1cb98087a05 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -164,7 +164,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; /* @@ -195,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 @@ -259,6 +253,10 @@ static inline void check_heap_object(const void *ptr, unsigned long n, if (folio_test_slab(folio)) { /* 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)) + usercopy_abort("page alloc", NULL, to_user, offset, n); } else { /* Verify object does not incorrectly span multiple pages. */ check_page_span(ptr, n, folio_page(folio, 0), to_user);