From patchwork Wed Aug 10 22:28:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9274311 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 35578600CA for ; Wed, 10 Aug 2016 22:28:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 234E62810E for ; Wed, 10 Aug 2016 22:28:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 17A992840F; Wed, 10 Aug 2016 22:28:25 +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_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 3C30E2810E for ; Wed, 10 Aug 2016 22:28:23 +0000 (UTC) Received: (qmail 13352 invoked by uid 550); 10 Aug 2016 22:28:22 -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 13323 invoked from network); 10 Aug 2016 22:28:19 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=e91/wuFxZRwV0cP4YIRKcllNQ5xptxawM+94oaf1cGM=; b=QbAHYCc9FMWl888eZJS94ad/sRq2bAKDds6szqCYK2+lFHP/r5mK+ZfkyD+Ez7aFnl nI1RL4TI0mdNA5a9o06xdC7jhw/NN5Q5rLchlsCyx25XmirRkWmRgdkCuHpNChN1yu3d H/Hu0IuPdo+EqtA0EvfJWNRTsLxfQhoYDiTow= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=e91/wuFxZRwV0cP4YIRKcllNQ5xptxawM+94oaf1cGM=; b=RP5zOBk3tfZgho1gDIzyl4s/3RpI0wixcttVNrtHEHTHhdk+vY+5/Z5eUL4kPE0h32 sZWlifo2nc6nnWtFPyR1tXzpyeBNxnanMZxIibVL2ZbRVDuSEzZNDDhBZaSHhOtWl68e ZBg1Qr5tG8PzCe4pLT3plj/E38n4HhO0pmoC1tiuZpOUdSg98NvFUUDChWIh8pIDVZeP los+XwaAnQVGr/P4k3c3CYui8QQ/B89T7E2C8SZNWGCg8ttkm6nWPLYslOD5chsskyOI HA/pC6xYvuPmlp2EbHEf9l7FFiVS1ly3tmMfTva8tMz5nL86J8IiPOEU+0/gpCn/0gRx KkHg== X-Gm-Message-State: AEkoouteg2T3LF5TCCo/JIKzxDAZaVsY7mFsE43SFmTFqFab9hyUN8oxijZVFgcbhTdJSKC+ X-Received: by 10.98.105.135 with SMTP id e129mr11192007pfc.59.1470868087617; Wed, 10 Aug 2016 15:28:07 -0700 (PDT) Date: Wed, 10 Aug 2016 15:28:05 -0700 From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Linus Torvalds , Emese Revfy , Andrew Morton , Jonathan Corbet , Vlastimil Babka , Mel Gorman , Michal Hocko , Johannes Weiner , Joonsoo Kim , "Kirill A. Shutemov" , Taku Izumi , linux-doc@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Message-ID: <20160810222805.GA13733@www.outflux.net> MIME-Version: 1.0 Content-Disposition: inline Subject: [kernel-hardening] [PATCH] mm: Add the ram_latent_entropy kernel parameter X-Virus-Scanned: ClamAV using ClamSMTP From: Emese Revfy When "ram_latent_entropy" is passed on the kernel command line, entropy will be extracted from up to the first 4GB of RAM while the runtime memory allocator is being initialized. This entropy isn't cryptographically secure, but does help provide additional unpredictability on otherwise low-entropy systems. Based on work created by the PaX Team. Signed-off-by: Emese Revfy [kees: renamed parameter, dropped relationship with plugin, updated log] Signed-off-by: Kees Cook --- This patch has been extracted from the latent_entropy gcc plugin, as suggested by Linus: https://lkml.org/lkml/2016/8/8/840 --- Documentation/kernel-parameters.txt | 5 +++++ mm/page_alloc.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index 46c030a49186..9d054984370f 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -3245,6 +3245,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. raid= [HW,RAID] See Documentation/md.txt. + ram_latent_entropy + Enable a very simple form of latent entropy extraction + from the first 4GB of memory as the bootmem allocator + passes the memory pages to the buddy allocator. + ramdisk_size= [RAM] Sizes of RAM disks in kilobytes See Documentation/blockdev/ramdisk.txt. diff --git a/mm/page_alloc.c b/mm/page_alloc.c index fb975cec3518..1de94f0ff29d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -64,6 +64,7 @@ #include #include #include +#include #include #include @@ -1236,6 +1237,15 @@ static void __free_pages_ok(struct page *page, unsigned int order) local_irq_restore(flags); } +bool __meminitdata ram_latent_entropy; + +static int __init setup_ram_latent_entropy(char *str) +{ + ram_latent_entropy = true; + return 0; +} +early_param("ram_latent_entropy", setup_ram_latent_entropy); + static void __init __free_pages_boot_core(struct page *page, unsigned int order) { unsigned int nr_pages = 1 << order; @@ -1251,6 +1261,17 @@ static void __init __free_pages_boot_core(struct page *page, unsigned int order) __ClearPageReserved(p); set_page_count(p, 0); + if (ram_latent_entropy && !PageHighMem(page) && + page_to_pfn(page) < 0x100000) { + u64 hash = 0; + size_t index, end = PAGE_SIZE * nr_pages / sizeof(hash); + const u64 *data = lowmem_page_address(page); + + for (index = 0; index < end; index++) + hash ^= hash + data[index]; + add_device_randomness((const void *)&hash, sizeof(hash)); + } + page_zone(page)->managed_pages += nr_pages; set_page_refcounted(page); __free_pages(page, order);