From patchwork Mon Aug 8 18:40:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9269135 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 4983F60839 for ; Mon, 8 Aug 2016 18:40:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3BAEA27F95 for ; Mon, 8 Aug 2016 18:40:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 300AF2815E; Mon, 8 Aug 2016 18:40:51 +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 68DE627F95 for ; Mon, 8 Aug 2016 18:40:49 +0000 (UTC) Received: (qmail 11665 invoked by uid 550); 8 Aug 2016 18:40:48 -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 11647 invoked from network); 8 Aug 2016 18:40:47 -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=0LOvnxSk3sY5ZC4sI26vNXcA337kaSsX7Q2WU6CDAkg=; b=o8hB3qMOtdjO1Hz6uNl9LUlofAAwDHUaldOcBQ1ut7wBJoKHIC9eRUEXh8VPgH7Cf9 Xz7wAE9sagTRE10XuoJ+ZmCd3NWHVwItHOXb+c4AMdndK3UMdVa6smXTFvlWNef1PAPY nU53NEzr8qSJslcL8o6RM7H5a94PaCFZOjheWPprXcBtaTQTS3SmWKnlOIpO78mwh8jW Q4yrBTtXljc48eATKms1zUSKFuMngdijlbdep/BNZVKI+UzocWeLdVGaM7ej9yGHTDq+ 3ARYm17gVtVEp5Dw9PL+hTn8833qXy8q4v95ofWiqUShjgLXbOx6h+n0nvmp/EHkWH3Q +mrA== 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=0LOvnxSk3sY5ZC4sI26vNXcA337kaSsX7Q2WU6CDAkg=; b=gh31m2uahIO3zeX4gFCwpu28Ui0uMQy7JHHnduLEJNGq1Mw+UPfr1RQgseRj4hEIMb r0C1CEI9feSvwbI0o0YuaTQGqOhTJfpha7zhOWlldNdruqijzYsWpUSnSW/Klg0MG/5W Xu28r8Qn6SqguZ9puIZO9az94qn8Ao8uOmQgaYp5aR2hpIrMJ2hAhjLiKi1tlNKnhLsS lbF5gSgeT6P5nu4GKbFB9HIzNWpW8xxl16gwHiP7luGvruBh5ZJljh/FKijVw0s/IRK9 GsZKHavUkWTisndRj6PZuFKujaFw3Cgrn0QFOddOhD/KltiBGZ/Yu08Crg7QyOh1nG9w 0IIQ== X-Gm-Message-State: AEkoousBcCX6eOUxpcNWRjza3z/stunUrnsJdGyGclIlGM2KuK/nqqOTwj1JktjigTJVknL8 X-Received: by 10.98.80.29 with SMTP id e29mr164915448pfb.76.1470681635216; Mon, 08 Aug 2016 11:40:35 -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: Mon, 8 Aug 2016 11:40:06 -0700 Message-Id: <1470681607-36883-1-git-send-email-thgarnie@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Subject: [kernel-hardening] [PATCH v1 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. Signed-off-by: Thomas Garnier --- Based on next-20160805 --- arch/x86/kernel/setup.c | 4 ++-- arch/x86/mm/kaslr.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index bcabb88..8dda0ce 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(); @@ -1071,6 +1069,8 @@ void __init setup_arch(char **cmdline_p) high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1; #endif + kernel_randomize_memory(); + /* * Find and reserve possible boot-time SMP configuration: */ 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 */