From patchwork Tue Aug 9 16:35:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9271741 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 93C6D60754 for ; Tue, 9 Aug 2016 16:36:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 84D9E27569 for ; Tue, 9 Aug 2016 16:36:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7945728364; Tue, 9 Aug 2016 16:36:32 +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 A2D9E2833F for ; Tue, 9 Aug 2016 16:36:31 +0000 (UTC) Received: (qmail 20226 invoked by uid 550); 9 Aug 2016 16:36:29 -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 20207 invoked from network); 9 Aug 2016 16:36:28 -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=12iqtfe60SZxTDPd74OHa4K/rRZbwzhfEuKyTNIVwo0=; b=W95TgU9fEP4cL4icGEeNsie4VykzoKrJusshU2PcUZbaZ2fw7yh5+/Gze2mt0DJLBd AzfzS//HkNd7SxcKvCVFo0zDRK+PTZi+fNksfHECGuXZD48YNNx8YC6N9ooP9IwOHrjG Ezqej+0SB6ogAZd+IRey4XmlShgGKkFilc6vWQ8KUdV0QQSQl0ug/p3fcsWsEX1Pqach ZEAocQUN7g2QegjKK0x9PPNQHSCM15Uxw8C0QEkcUvCgkclKn0FpQUVc7px/rj0Rf10H eIk/Z8dtBoADfecRZxwbVYrAHEG3LXvhEYcs3UkEIuY5S3ztyEKQtI66hsZZuB8tMWZL Ux2A== 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=12iqtfe60SZxTDPd74OHa4K/rRZbwzhfEuKyTNIVwo0=; b=RSo0cfD/FgoORYK9ZyfGH/DHfs4g5RKWzn1jY63kBMNbTmPPgDjEM6FAa+24r1dI/P oHMJ2ShGvlwUUqjmuD+K7ZE3ykVQ36qKSdq+ltlobMlYxBsSEKE768AnvVSq7xGsA+Ql Wrn0Yx3L1RjN2oEfTsQVs9o2XKcUIUs+OK7ssld+P7GSF8DaK53dP/TTZmDBFpYS+OPV aP9TMqH6wCRHHd51AkEfZbEWDcyzbWwzXQxPP+41+SrBY2fGHDH99JgCOvY2tad5+Rap 2TAENDXTC/gIsRiI/wTOgp7/XDmu8IxebejTz+LZGLXGYhRzID+SfcGVyqetL0pM91WF HBqQ== X-Gm-Message-State: AEkoouug7zsyJzPOjg9fjd+FpbYpBtH32bfJe1YhHQ/3GR5n9NZYjXrcVeFzIubK8A0gjBVL X-Received: by 10.98.63.1 with SMTP id m1mr173703127pfa.14.1470760576360; Tue, 09 Aug 2016 09:36:16 -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:35:53 -0700 Message-Id: <1470760554-129111-1-git-send-email-thgarnie@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Subject: [kernel-hardening] [PATCH v2 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 section after max_pfn is + * defined and before each memory section based 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 */