Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3484241imm; Mon, 13 Aug 2018 12:32:07 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzgXFUReHEP68FYgFmX/P3fTxLy2ZoWtrquTewcCq95gpi2V3HFkRQF0sR8Z4nhGbc5ZtWj X-Received: by 2002:aa7:8118:: with SMTP id b24-v6mr20243467pfi.78.1534188726958; Mon, 13 Aug 2018 12:32:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534188726; cv=none; d=google.com; s=arc-20160816; b=sN/iUwTXiMusJWhsJHlo0VIvF4EjQJQIi8AnWdKaJL0CvjMZu2eHh2/izAjtfFmdZ/ BUUgZWTh2neSNPECJGbB2gEgLEEA+bXNtj3k701ApP2ZYPJk4+GyZNkZlP0BTqpVtDe9 kwNrzhHx4sGlj496lxLcNHzH4RUVDtxuhWloaaECUrAOB+Q5W+SedXu7BHUs1zA5hIVd y5eFQebgeEGcrTZNGqalPVPyK0TNsB8K4q0ijU6VP/YGc5JIz9uIXQr9/G1aKRAGf+GJ Ix/xafOSqt+3TMFsBduluziNzonMxi7lYl/jFvIQ2PJ0koygLR7G6I6WgpFEmnoFCL8J qc/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=AlhGfESb74txnepXozmi3KiRV4LT2aMLstlLTq8o0M8=; b=aLme4GlpONYjC8pfvvk/tkF5JXwJMKCfdj6gnnbQsYe9dM3EjtHiSOxb4RxKj5GYL2 YKxIcrm86mkWBWc1c4vLsRCzvVySbneWtLOs2zQ+synhxMxugo9ZQPPCxkeCyJzTVfAz YVy2QxVIf//aSHRrU+Uxj5Lhd2U43vZzWXt0W1phS9CJ/pl2iYTywNxc/MAA015v2Sdi g0JIe6hNn/rXPaQqcLsHOstO8qctrhsQlX5nn6Dmi7/NA9kda8fQfSXPT8ZfUY88SI4i 3A8Y8mDiczUNZleXeqbi2KjCV9jlbCX1G7DtZchNPKw4sVRyOoE/g0BypEkCs9IR8yZn A0Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=DUUqrLmq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d34-v6si14743833pld.301.2018.08.13.12.31.51; Mon, 13 Aug 2018 12:32:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=DUUqrLmq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730480AbeHMWOi (ORCPT + 99 others); Mon, 13 Aug 2018 18:14:38 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:40377 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729152AbeHMWOi (ORCPT ); Mon, 13 Aug 2018 18:14:38 -0400 Received: by mail-pg1-f196.google.com with SMTP id x5-v6so7978207pgp.7 for ; Mon, 13 Aug 2018 12:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AlhGfESb74txnepXozmi3KiRV4LT2aMLstlLTq8o0M8=; b=DUUqrLmqQ3lhpge/UGuSXBIwInmfyAo8b9x6BBAy7DHBiFEqrQgFnirQn5XNZo5k4o G0yBGBAhzpNCrpceBJ/9plW8gkOoXQTg996jj+iSpmaUtljNSNV8GyqtqPT3YqItGcHY slrbRG8WfwPLL8XSXDAtIDVy/XLYHbt0rHcMkihVlx5E5C6qlEH5VtqizaoEx+kUa5KQ lRxtV44prrT3pLZfLwDHqYfnZEiLofzerhXD9IztlP0PvhqKT3ij2ufl5+1Pe0LYxjMz 2fJ2agGjbRGKqcOw+OUOEzH8lTry235+6m6wqRtS8n1Cc0kFWE2tQ0epWlGQWu0fndrt 6H5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AlhGfESb74txnepXozmi3KiRV4LT2aMLstlLTq8o0M8=; b=a60iPD657IlK8ucYkCF4cc60voZraPGEzsuNQYpB1XGxP2yfWKpvIpChH6trqgVUlX P53ufA8eQ5h0Hn56meGkOX/H6mM+bSh8lAGicR6duspN9vAIrRIWeNi23ZlA0bkDVPXU d4b6hG7MqoTR69a9CmxbHFAO5gj7lYB8WH2k3KMVqsLwb/L5sn+WJPSOYPXxCZSlKbps mZk/RkoluAG1kCAn1+wDx2oK930UYHlLEX2VwxMmmQDNBL/EjXImyWyebrTQjOCe49ti xPFwxgIymTCaJv9Vqeu9mVPzL4A2iREo2aHJwvOmGzWJi9qo4IlVwRVyaypWvCJ3WlAj uADg== X-Gm-Message-State: AOUpUlHGPjZkB51pWat9kMHf+/4mefiZ2SGuK4aKBuqczww4JdAL+mc4 JRcdm5bigrl0b27AFzECEAUJIA== X-Received: by 2002:aa7:8591:: with SMTP id w17-v6mr20338767pfn.77.1534188661882; Mon, 13 Aug 2018 12:31:01 -0700 (PDT) Received: from hackmann.mtv.corp.google.com ([2620:0:1000:1601:82f7:8f1:8c08:a97a]) by smtp.gmail.com with ESMTPSA id h132-v6sm27460706pfc.100.2018.08.13.12.31.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Aug 2018 12:31:01 -0700 (PDT) From: Greg Hackmann X-Google-Original-From: Greg Hackmann To: linux-arm-kernel@lists.infradead.org Cc: kernel-team@android.com, Greg Hackmann , Catalin Marinas , Will Deacon , Andrew Morton , Robin Murphy , Laura Abbott , Steve Capper , Kristina Martsenko , Stefan Agner , CHANDAN VN , Johannes Weiner , linux-kernel@vger.kernel.org Subject: [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid() Date: Mon, 13 Aug 2018 12:30:11 -0700 Message-Id: <20180813193013.236362-1-ghackmann@google.com> X-Mailer: git-send-email 2.18.0.597.ga71716f1ad-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ARM64's pfn_valid() shifts away the upper PAGE_SHIFT bits of the input before seeing if the PFN is valid. This leads to false positives when some of the upper bits are set, but the lower bits match a valid PFN. For example, the following userspace code looks up a bogus entry in /proc/kpageflags: int pagemap = open("/proc/self/pagemap", O_RDONLY); int pageflags = open("/proc/kpageflags", O_RDONLY); uint64_t pfn, val; lseek64(pagemap, [...], SEEK_SET); read(pagemap, &pfn, sizeof(pfn)); if (pfn & (1UL << 63)) { /* valid PFN */ pfn &= ((1UL << 55) - 1); /* clear flag bits */ pfn |= (1UL << 55); lseek64(pageflags, pfn * sizeof(uint64_t), SEEK_SET); read(pageflags, &val, sizeof(val)); } On ARM64 this causes the userspace process to crash with SIGSEGV rather than reading (1 << KPF_NOPAGE). kpageflags_read() treats the offset as valid, and stable_page_flags() will try to access an address between the user and kernel address ranges. Signed-off-by: Greg Hackmann --- arch/arm64/mm/init.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 9abf8a1e7b25..787e27964ab9 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -287,7 +287,11 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max) #ifdef CONFIG_HAVE_ARCH_PFN_VALID int pfn_valid(unsigned long pfn) { - return memblock_is_map_memory(pfn << PAGE_SHIFT); + phys_addr_t addr = pfn << PAGE_SHIFT; + + if ((addr >> PAGE_SHIFT) != pfn) + return 0; + return memblock_is_map_memory(addr); } EXPORT_SYMBOL(pfn_valid); #endif -- 2.18.0.597.ga71716f1ad-goog