Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1270147pxb; Fri, 21 Jan 2022 13:58:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2LbZryQg8GWvxbp7gNre2CbMI3ssVknZQY6NEeZqh26107LEL/YcszXiolMFyZk/4r8UK X-Received: by 2002:a62:7e53:0:b0:4c7:c1a3:3911 with SMTP id z80-20020a627e53000000b004c7c1a33911mr1945488pfc.13.1642802280125; Fri, 21 Jan 2022 13:58:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642802280; cv=none; d=google.com; s=arc-20160816; b=e5Ipz0R+vyeUSeACZ6la2pv/1CHCbitBo7l0aedaa8iYoZAhYbYlhNpxWNXi4ImwSW wt5rsNb32HI+hjeP4Eavym1e5tqgO4byMpus4TSnO4ik6XWZoXZOWhbBh2w4RJhWHMjx lYeiPaaghqwmMWKOtsbsBShRCAYLPq+GH4pBuPvTFeq/2j0EKAB+kK0DmILguT6kWgSC kRIAQCZe4Fdg3PHjgt46GCdR6I/ocMPurhvsnWODFpLtI2+kWFb0Tt4NYSuwnOtKiMzV RC69zTK+MzvGKYFKfOekCtcBjGuXHFn73rgOCPGOdOX2bhErrxFu6LCStNt/AkK+t32K bOFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=f3ktHM+E9YZWFFSesjcwn2TLKAc17toym508ORnF+ZY=; b=fTlRgZI+Rm2JDOPqcgPBOmtr4f3Eutej5AVam0FeUjYbUGPaoRYvqyWR4cmARiNzRl /q1RFMPm3HLdeJcycDvCQ62PsRXfwdn30juIABO0z0DigRI0Pn5HUW7FfXJowQZVpBb9 dqapNKbnGdQPCBIxEEKuUjyO9BnbjbGbrlNMxnYh4sE4bwo+aL+dTxeBxE754TdjGLN5 uGEVuJtJmPyZvCD1HlsRjiOIuaZB39HjD9sxLjSzSi5V48XadIL3bquS8GEgKnhu/4Vb LDyfuaSqqybTVnbyJk5U0vz/c0Nl76x4PdDc/KqnW4lUZcA3d5J++ztqO71cIaPHh1sB c40g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f198si6671776pfa.347.2022.01.21.13.57.48; Fri, 21 Jan 2022 13:58:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235512AbiATMWo (ORCPT + 99 others); Thu, 20 Jan 2022 07:22:44 -0500 Received: from foss.arm.com ([217.140.110.172]:35958 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232146AbiATMWn (ORCPT ); Thu, 20 Jan 2022 07:22:43 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 91B2FED1; Thu, 20 Jan 2022 04:22:42 -0800 (PST) Received: from [10.57.68.26] (unknown [10.57.68.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 97FE53F774; Thu, 20 Jan 2022 04:22:40 -0800 (PST) Message-ID: <319b09bc-56a2-207f-6180-3cc7d8cd43d1@arm.com> Date: Thu, 20 Jan 2022 12:22:35 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH] vmap(): don't allow invalid pages Content-Language: en-GB To: "Russell King (Oracle)" Cc: Matthew Wilcox , Yury Norov , Catalin Marinas , Will Deacon , Andrew Morton , Nicholas Piggin , Ding Tianhong , Anshuman Khandual , Alexey Klimov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20220118235244.540103-1-yury.norov@gmail.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022-01-19 19:12, Russell King (Oracle) wrote: > On Wed, Jan 19, 2022 at 06:43:10PM +0000, Robin Murphy wrote: >> Indeed, my impression is that the only legitimate way to get hold of a page >> pointer without assumed provenance is via pfn_to_page(), which is where >> pfn_valid() comes in. Thus pfn_valid(page_to_pfn()) really *should* be a >> tautology. > > That can only be true if pfn == page_to_pfn(pfn_to_page(pfn)) for all > values of pfn. > > Given how pfn_to_page() is defined in the sparsemem case: > > #define __pfn_to_page(pfn) \ > ({ unsigned long __pfn = (pfn); \ > struct mem_section *__sec = __pfn_to_section(__pfn); \ > __section_mem_map_addr(__sec) + __pfn; \ > }) > #define page_to_pfn __page_to_pfn > > that isn't the case, especially when looking at page_to_pfn(): > > #define __page_to_pfn(pg) \ > ({ const struct page *__pg = (pg); \ > int __sec = page_to_section(__pg); \ > (unsigned long)(__pg - __section_mem_map_addr(__nr_to_section(__sec))); \ > }) > > Where: > > static inline unsigned long page_to_section(const struct page *page) > { > return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK; > } > > So if page_to_section() returns something that is, e.g. zero for an > invalid page in a non-zero section, you're not going to end up with > the right pfn from page_to_pfn(). Right, I emphasised "should" in an attempt to imply "in the absence of serious bugs that have further-reaching consequences anyway". > As I've said now a couple of times, trying to determine of a struct > page pointer is valid is the wrong question to be asking. And doing so in one single place, on the justification of avoiding an incredibly niche symptom, is even more so. Not to mention that an address size fault is one of the best possible outcomes anyway, vs. the untold damage that may stem from accesses actually going through to random parts of the physical memory map. Robin.