From patchwork Tue Aug 9 16:51:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9271837 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6428B6075A for ; Tue, 9 Aug 2016 16:52:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 559B627F80 for ; Tue, 9 Aug 2016 16:52:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4975128383; Tue, 9 Aug 2016 16:52:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 518E627F80 for ; Tue, 9 Aug 2016 16:52:10 +0000 (UTC) Received: (qmail 26353 invoked by uid 550); 9 Aug 2016 16:52:09 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: kernel-hardening@lists.openwall.com Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 26335 invoked from network); 9 Aug 2016 16:52:08 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=xxTrbMcj1hXpvF1JmrKXZNwEuTzw1ynGnnKYZP+FG0s=; b=cSgKkN6kmDPMv1LDJv73MwYAdXJ+XcS3N1C8Qh0Yjc1LKgx+vJ/ZRTsWeOkFBo/ssQ BRul2C9/MQgs7xeOTAlYzbFABVFXuLhO8zbAS/S99wgfDLKosWt7g/7FzY5dTc1fA261 J+BwMvAqCQpc4w4xLg1DGm7vL0nngMenE1XpDfXI7eEYOHuBZLRYjAR0mbVfu7egcRn5 ohh5IGmgckzB1oEG18Pl1kFApO/ws7kWiBNmwckAQUB7yfcbLpxl3fDmySubDNpFeD+V OPkS1OjoD2nJU22XHKGO0blFHLsv1WeccXR5aWDXIfqqbxj5GhGgIlealQXm4DnML+S3 0tww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=xxTrbMcj1hXpvF1JmrKXZNwEuTzw1ynGnnKYZP+FG0s=; b=A64xcvLJCiKv6zwPPF2ymsF4VKQXqpJcgfKdi6rf6ttTemL48+whj0Llqa5UAZrM4n 0JJUyzQ865cQaP1ES0Jm2GPrq+/fzVx1jEL1J42Q0Zm+ZzcgSf0leAFandE6qwK28Ibn nPxDz3UgWl6qgsOSub/EUPsL6nfCGe3cL0afM0LIG2g55XFzIrwv39odNHIeLlnXfEBC BHWt21FZVs21gBjONM6ki3kLVaD3Wt2cTuyb7T4odXhV9qk48YM8MOL1l+6ACCFRHrsy 5F9/bz0e1HlGIVP6Y/Ndeo7eeIxnC/8YtIt1Cxe61quzuaTieSWm4UrdBK3vc4zlLmwg 3kPA== X-Gm-Message-State: AEkoouvfeFmx/LPwEy4iOJDb2AOWma6IPOTtAl27fLBiBYFCxsFRfCsEVKwF+FjNLMYH/+9d X-Received: by 10.66.242.201 with SMTP id ws9mr172979908pac.7.1470761516355; Tue, 09 Aug 2016 09:51:56 -0700 (PDT) From: Thomas Garnier To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Borislav Petkov , Joerg Roedel , Dave Young , "Rafael J . Wysocki" , Lv Zheng , Thomas Garnier , Baoquan He , Dave Hansen , Mark Salter , Aleksey Makarov , Kees Cook , Andrew Morton , Christian Borntraeger , Fabian Frederick , Toshi Kani , Dan Williams Cc: x86@kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Tue, 9 Aug 2016 09:51:22 -0700 Message-Id: <1470761483-62501-1-git-send-email-thgarnie@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Subject: [kernel-hardening] [PATCH v3 1/2] x86/KASLR: Fix physical memory calculation on KASLR memory randomization X-Virus-Scanned: ClamAV using ClamSMTP Initialize KASLR memory randomization after max_pfn is initialized. Also ensure the size is rounded up. Could have create problems on machines with more than 1Tb of memory on certain random addresses. Fixes: 021182e52fe0 ("Enable KASLR for physical mapping memory regions") Signed-off-by: Thomas Garnier --- Based on next-20160805 --- arch/x86/kernel/setup.c | 8 ++++++-- arch/x86/mm/kaslr.c | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index bcabb88..dc50644 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -936,8 +936,6 @@ void __init setup_arch(char **cmdline_p) x86_init.oem.arch_setup(); - kernel_randomize_memory(); - iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1; setup_memory_map(); parse_setup_data(); @@ -1055,6 +1053,12 @@ void __init setup_arch(char **cmdline_p) max_possible_pfn = max_pfn; + /* + * Define random base addresses for memory sections after max_pfn is + * defined and before each memory section base is used. + */ + kernel_randomize_memory(); + #ifdef CONFIG_X86_32 /* max_low_pfn get updated here */ find_low_pfn_range(); diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c index 26dccd6..ec8654f 100644 --- a/arch/x86/mm/kaslr.c +++ b/arch/x86/mm/kaslr.c @@ -97,7 +97,7 @@ void __init kernel_randomize_memory(void) * add padding if needed (especially for memory hotplug support). */ BUG_ON(kaslr_regions[0].base != &page_offset_base); - memory_tb = ((max_pfn << PAGE_SHIFT) >> TB_SHIFT) + + memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) + CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING; /* Adapt phyiscal memory region size based on available memory */