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 52A91C433F5 for ; Tue, 5 Oct 2021 21:23:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E47D461216 for ; Tue, 5 Oct 2021 21:23:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E47D461216 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 7C19F6B006C; Tue, 5 Oct 2021 17:23:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 770AE6B0071; Tue, 5 Oct 2021 17:23:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65F9F900002; Tue, 5 Oct 2021 17:23:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0141.hostedemail.com [216.40.44.141]) by kanga.kvack.org (Postfix) with ESMTP id 5595D6B006C for ; Tue, 5 Oct 2021 17:23:12 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 11A768249980 for ; Tue, 5 Oct 2021 21:23:12 +0000 (UTC) X-FDA: 78663659424.22.9D5E19F Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf22.hostedemail.com (Postfix) with ESMTP id AAC822D93 for ; Tue, 5 Oct 2021 21:23:11 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id d13-20020a17090ad3cd00b0019e746f7bd4so3010094pjw.0 for ; Tue, 05 Oct 2021 14:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=wcIcPyvu1I+vbmczbeTwLcOLp1C78lh5uAdHXZFutxw=; b=SSLdL1ReIscrGZkLvHLa85PwmUx+Hi/rpwQhix4XKeBSMJrfjEvDOvHjkyulDIH/uU ot+P6YhUTauAKJRklF8oFIlQtJP00i+m7YRl4DJ0r3uimwAFrmSDBd+78xGnu1y9RB0b 4O46f3IhJKoXqaDFFDX/Uby/67Lr/uu38JFdM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=wcIcPyvu1I+vbmczbeTwLcOLp1C78lh5uAdHXZFutxw=; b=5jcNSLms0mG8uC7lIEykZzWL6A2R3d7RJEpbl8iccdlKqyRESoUvyt61CVuonHN+UE acqBX8v8y5May5Pl7Qc0Et52O1otcdKyIzTUJ1S5tGcXR11IbnjNtC1o5waa0pJoRT2K Od8gUm0lB+N1lf47AVhKYxSBBniO1PY9zUzLPIfLpnFPwYqilXbGfLxwmupm36QpgkTU rOBRYWfNPtNXQNPYCiyoJhtGNvCVjy4SF0qyEpkLG7FoIC4LsBsYCIGNzLYeK/3CXyF1 7XMSh8hCzRXe7b+86zlpuRbbyGiIC7DMtkcLEKGIpkWgtDBu/ePoR8PJBjFF3f0sO4Pp 9MnQ== X-Gm-Message-State: AOAM531lrBiSCGy+Wf3I1eFGS9J1QBuvcXercgBH3OekzBAIsjWYrN34 MDq4QAsFkPQSEZMWSqiXiOHR1Q== X-Google-Smtp-Source: ABdhPJyTsxYSWGTkDrhOfbBk4ca1+v9E3yK6fH4pJy1gvTXqwrcwLb+lRx7PMCdMramU9IuPuXPTbQ== X-Received: by 2002:a17:902:be0f:b0:13a:19b6:6870 with SMTP id r15-20020a170902be0f00b0013a19b66870mr7513847pls.64.1633468990458; Tue, 05 Oct 2021 14:23:10 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u12sm18001135pgi.21.2021.10.05.14.23.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Oct 2021 14:23:10 -0700 (PDT) Date: Tue, 5 Oct 2021 14:23:09 -0700 From: Kees Cook To: "Matthew Wilcox (Oracle)" Cc: linux-mm@kvack.org, Thomas Gleixner Subject: Re: [PATCH 1/3] mm/usercopy: Check kmap addresses properly Message-ID: <202110051422.3E52FBDE18@keescook> References: <20211004224224.4137992-1-willy@infradead.org> <20211004224224.4137992-2-willy@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211004224224.4137992-2-willy@infradead.org> X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: AAC822D93 X-Stat-Signature: z37xa1kcaxhyorfzqzd1thryxhg9kepr Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=SSLdL1Re; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf22.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.45 as permitted sender) smtp.mailfrom=keescook@chromium.org X-HE-Tag: 1633468991-692719 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: On Mon, Oct 04, 2021 at 11:42:21PM +0100, Matthew Wilcox (Oracle) wrote: > 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) > --- > arch/x86/include/asm/highmem.h | 1 + > include/linux/highmem-internal.h | 10 ++++++++++ > mm/usercopy.c | 15 +++++++++------ > 3 files changed, 20 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/include/asm/highmem.h b/arch/x86/include/asm/highmem.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 > > /* declarations for highmem.c */ > extern unsigned long highstart_pfn, highend_pfn; > diff --git a/include/linux/highmem-internal.h b/include/linux/highmem-internal.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); > } > > +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 */ > > /* > diff --git a/mm/usercopy.c b/mm/usercopy.c > index b3de3c4eefba..ac95b22fbbce 100644 > --- a/mm/usercopy.c > +++ b/mm/usercopy.c > @@ -228,12 +228,15 @@ 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, 0, n); It's likely not worth getting an offset here, but "0" above could be something like "ptr - PKMAP_ADDR(0)". Either way: Acked-by: Kees Cook Thanks! -Kees > + return; > + } > + > + page = virt_to_head_page(ptr); > > if (PageSlab(page)) { > /* Check slab allocator for flags and size. */ > -- > 2.32.0 > -- Kees Cook