From patchwork Tue Aug 9 17:11:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9271899 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 CA88360839 for ; Tue, 9 Aug 2016 17:11:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B7DCB28066 for ; Tue, 9 Aug 2016 17:11:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ABDA528358; Tue, 9 Aug 2016 17:11:49 +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 E396728066 for ; Tue, 9 Aug 2016 17:11:48 +0000 (UTC) Received: (qmail 4074 invoked by uid 550); 9 Aug 2016 17:11:47 -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 4054 invoked from network); 9 Aug 2016 17:11:46 -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=txN8n7UUYhOGOr4+Pdt6xYinyZs2L4CcdzeRBFdqQLo=; b=B6DSSP4xjaOWmNiWbB9zvvOYwPN4RfOgKl7JUkfAiB3k2MGBJqkvP8KUcXMP5Hz4MB EnewQHOQBTrvY/cDneE5JrEvgPEjoE9ryGUP9GJBpTwSomKBn2/W1dPbFxmh0qAFH3KF laX1pC5bGAciPbQI3YZzT2939em3BjN9Kf9TqgwIMIF0akDOGyFv0csjwq0fYoe5+waQ ctM9ipI1QatQmo5q5TtdZh68XUwjvW6bKvNfQUDunarmVYlEet0deLTRHqbHguVHjWBu 4Uag9I+MEEhDrm6+ZrD6lYkXOv9P1V1G94gpd4IsM70to6JeFjH+VdUGHoRQXt8t4aOe +bHA== 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=txN8n7UUYhOGOr4+Pdt6xYinyZs2L4CcdzeRBFdqQLo=; b=dZIuO1iFS521dRQxa0YxSf8Ya20aoWKXwAME3An+80QELxZa1owXLz+bV6o3IllEI5 xLj+BZbvTxkf5vEg3IspAGPwFKlEj6887jO3JtkIDTm1/0QAj+YrI3ZElgpxo+WXu4nY raflT9fjyJmLBLgqDDTNRoTBfGHh1wohbhtVtQHgrpkVaQFjl88ufs1u/0WUebwKk21T E80N4XtnS4y5gZz6IqL9eoLfi1IErLlphPhEIeZBdPhPUfMD+0rirxeVk18EkgCZlRT2 K1WTkjgx6OCRJC33mfnXuAyxUYpHKuqJhORwwcmn//giVQXkWGjsQJ5i1PuRv7eBjE/8 j8Ig== X-Gm-Message-State: AEkoousYaznk552dPv4vX7NxbuMYscKylFnP+VFRYFd+yA+NPFc66LcHGcp3vHC3x2Vhju8v X-Received: by 10.66.5.72 with SMTP id q8mr63057541paq.38.1470762694205; Tue, 09 Aug 2016 10:11:34 -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 10:11:04 -0700 Message-Id: <1470762665-88032-1-git-send-email-thgarnie@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Subject: [kernel-hardening] [PATCH v4 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. It 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 */