Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1182917imm; Wed, 15 Aug 2018 12:56:11 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxMJvUPaa6hBGMj1CJxOqrCEMswdEnGLJFEOt8LEIixr/MViGmfGHXR3cjppgkPSzsfDgHc X-Received: by 2002:a62:5d55:: with SMTP id r82-v6mr29114137pfb.150.1534362971078; Wed, 15 Aug 2018 12:56:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534362971; cv=none; d=google.com; s=arc-20160816; b=UF/Rs5CfvRLt/hl6wdvcjAtLWRvAVWaroi8nj/r+d2qTXf64q4LNomukGhnSrMci9G GY8awnQ3wjv/N0rtfSe+BXyuFBdrKfL/ljg2L+TkIaVg2MDgjQEG1cC8WnTYp3aJcFFh irk/xjkJQCbJkLPk+/oT9jC4smXh6nuz2kLytvEr1MYDTNPvRHKrDTDSkZDAyxiimWtQ uzeRyXdv9u3iMsPaweMgH/kgF3MHy2OKmXYCjl/hrmNG3yVnZxx8Ok3eFMMQNNcd7icW 15eFqHaCv2x4+1+GXMvPAdJ0rqW1eP7yBHepi4jRV/9I6HZvP8gv17Bmv1sjTjG4zhV8 64OQ== 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=1svywvHXHC+/3YSygBgMl8aZ6Dw9DjsoXW1z7O9jlBA=; b=L9C77Aq4cbWLgrYxwOTu6oXsHQ46Mq0ETjh2caOg9X+SUA5KNb5q5ATj4zL7SS5GZk IwA79lmLQ2t40Zt2GMcc1I/bmBaLVN9tt50M5yiQvm4ylvOlGdb+97AOXINYK2rikDyP 6UgFkiwSb2/l01sooxt813zNLFOCjKOQj6XprbobwxfqHNDKzv78quH9cIKrsfT9Kfwo Cis/wYGjulRhy9aNfEtWfDrl2RnWO24AmmXK93PqZtkQZ0OOHgV6y4xzS6loSpkbbTQT IQyukyIJyH5JYPvLL8hLWY9EleUs+oC+H8Qj2g0afaqmj11zBIzBVNWfjJfVoYgNUloy WjMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=NLlGFKQz; 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 q18-v6si19021661plr.134.2018.08.15.12.55.32; Wed, 15 Aug 2018 12:56:11 -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=NLlGFKQz; 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 S1727817AbeHOWrF (ORCPT + 99 others); Wed, 15 Aug 2018 18:47:05 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:43168 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727562AbeHOWrF (ORCPT ); Wed, 15 Aug 2018 18:47:05 -0400 Received: by mail-pl0-f66.google.com with SMTP id x6-v6so904993plv.10 for ; Wed, 15 Aug 2018 12:53:31 -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=1svywvHXHC+/3YSygBgMl8aZ6Dw9DjsoXW1z7O9jlBA=; b=NLlGFKQz3j/zWusta5ednPTzRb3NfIEtFqzNYY6uzt5prVvgFGUA5Sfe1G1SsgU8NY AO+VJuMIMgMUGN/deVzA/BXQ6uAeUuAqLqO6EelNOJ2dnxn/i0o0FOw+s10BZ2i8DR6m MKSXfpKcNIbo731rstZvsspM6z92gmf1hjbjkQce5XqzUjazMSZVIH9pRy/KqJDvKguF ZXtkfjDzgLI1TNhYYE1YtR41qXj0WU9EFU9/X3wDacLQZi1VYKFr5GumQlM1+hC1/e8P /r84vbVyNXH/v0wHv+4OTg/+9XJEBbjB3akWJ/1Wyuq2L+SqkI744/YJQd0dPSs2iGN1 f4fQ== 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=1svywvHXHC+/3YSygBgMl8aZ6Dw9DjsoXW1z7O9jlBA=; b=KGSaq5vrFJ3+6jsNBrBDUWesF1q7agUXFSI1GLHJ1AsOTHGO1u+iGz95lVUiDhwXyP bfcdAcXIEwxDxImsDRYCwNSnPTbXZzI0ZBBPOBbmS5eLWXhOBRA1sgU8bQVV4Zf3n1ED 3VeLVj6g0SQ+xldwRr4zQ8tnXzTSqLAx28vTqtj7YzEqo8lguddsGn18Kj/mD2WE0oQd FCa//Q9expQjqZpqbvvRi7adcudsSWFhkSAmX1bSATrOAqCgjDGlrgRYQZ481KCC9cQB tVumTumeNi5BKOZ2YvpShj2mtv17M6pc/LWkpgNQyvyPASffaN5p4GzX2hLCHfpdKAU4 qZ1w== X-Gm-Message-State: AOUpUlGAs9lloUoV6ffqz484tq6h+DeOwQkyIvwP6B1/13fPB0bZ7faH wvyfTikwQ/tHUP58s5oiyplZ1w== X-Received: by 2002:a17:902:7793:: with SMTP id o19-v6mr26193653pll.306.1534362811154; Wed, 15 Aug 2018 12:53:31 -0700 (PDT) Received: from hackmann.mtv.corp.google.com ([2620:0:1000:1601:82f7:8f1:8c08:a97a]) by smtp.gmail.com with ESMTPSA id r23-v6sm72514617pfd.144.2018.08.15.12.53.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Aug 2018 12:53:30 -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 , stable@vger.kernel.org, Catalin Marinas , Will Deacon , Laura Abbott , Andrew Morton , Robin Murphy , Johannes Weiner , Kristina Martsenko , CHANDAN VN , Steve Capper , Stefan Agner , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid() Date: Wed, 15 Aug 2018 12:51:21 -0700 Message-Id: <20180815195123.187373-1-ghackmann@google.com> X-Mailer: git-send-email 2.18.0.865.gffc8e1a3cd6-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. Fixes: c1cc1552616d ("arm64: MMU initialisation") Cc: stable@vger.kernel.org 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.865.gffc8e1a3cd6-goog