Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4776346ybi; Tue, 30 Jul 2019 07:59:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZLdxpfFWiWsKWdqXaVMn1F5d/+wc6Z2CHkwHbCqq+MuQHpgp8sTIofkcuDYdv12RDPX/Z X-Received: by 2002:a65:6415:: with SMTP id a21mr95303806pgv.98.1564498794078; Tue, 30 Jul 2019 07:59:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564498794; cv=none; d=google.com; s=arc-20160816; b=ZEZL8mKp6YNElmzPZGAdSLp3cxSnN/vNwoBVYx5ZH7BkJz/W4gXdfo8k/QYaTAZ3dM Vlxpt76fxpHWwmOUGYRyps+76vy2EKZ5lQrxsZHLfa5nU82MQGMn7KCceAOkXBVNP2m+ kRceNgahuVRlg8Ev4JD0dhpgsa6O04mje1ElnWe1+sW83J8jo1cxziZfo3rLvsU4eGr8 B+wumO/tXVqzL8qMhE3E9HEKNPHzcXSmeGPyde2nMJ0NgQTQOBypa6IDoNw4dYm0U0hb +84Jve9Yc33l4I1nDoMW+BXWg1JZWAkqCjN0ECa0uNfTm3G3ghud4YXUI8NyiMlJH6ZM I4bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=VdYKK+YSpvMaj6YTHxEjAE3L6fR8opLRNMn0FGSQBcg=; b=vgWnaa+DPHTSQAOTi1oaAjE6L5SXCiJD59elb2vbE9SUZ+0qQ7fTll9J5N+/3c/xXm w+/K8EDmHe0DrCt2x6xO7eQ3NIreGVLtPQJqEX2+UUTkiT6Tv4iJoaXh9+rI25i98Dqs VjDkwBqov+Tst0awTp2cEXMf3nzdQvLIt92Enz73JKnPp26QHqOjI6IgNXdBLCjTRmmQ oAHBp8BwKJ+wDc9bpwe/TRW5zojpzRpOuF7jmfDt2XZ9cCwwZdCwL1LZ+doCVa4AGk5c tpFUD4lMgSL+Chm1OmKhW+t61J46r3vpTgH08KjzE1c1993Z/bCaiqTiFZyEyPLxkEeT 375g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=f1cEUQin; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v9si25807070plp.4.2019.07.30.07.59.39; Tue, 30 Jul 2019 07:59:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=f1cEUQin; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731591AbfG3Jop (ORCPT + 99 others); Tue, 30 Jul 2019 05:44:45 -0400 Received: from pegase1.c-s.fr ([93.17.236.30]:25682 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727582AbfG3Joo (ORCPT ); Tue, 30 Jul 2019 05:44:44 -0400 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 45yWqY4NmFz9vBLV; Tue, 30 Jul 2019 11:44:41 +0200 (CEST) Authentication-Results: localhost; dkim=pass reason="1024-bit key; insecure key" header.d=c-s.fr header.i=@c-s.fr header.b=f1cEUQin; dkim-adsp=pass; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id iGCYl8cF8axD; Tue, 30 Jul 2019 11:44:41 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 45yWqY370Rz9vBLN; Tue, 30 Jul 2019 11:44:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1564479881; bh=VdYKK+YSpvMaj6YTHxEjAE3L6fR8opLRNMn0FGSQBcg=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=f1cEUQinoonCEuYv57Lh3FcFKWxydZE4rtPElNT27QTsBUBPliVDKXNyy9cds+wmI Vg4w95bSX+AzVtqqUvszg4k2OLt1q9JufqylY7weAM24O+yVOsGjOvg5IEtvY+from nwIuRrfUDHV1LissnP93YcBRc7DjIx08t4xQ8psE= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 923DE8B803; Tue, 30 Jul 2019 11:44:42 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id tsqn-8Xg7D6M; Tue, 30 Jul 2019 11:44:42 +0200 (CEST) Received: from [172.25.230.101] (po15451.idsi0.si.c-s.fr [172.25.230.101]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 467628B801; Tue, 30 Jul 2019 11:44:42 +0200 (CEST) Subject: Re: [PATCH v2 07/10] powerpc/fsl_booke/32: randomize the kernel image offset To: Jason Yan , mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, diana.craciun@nxp.com, benh@kernel.crashing.org, paulus@samba.org, npiggin@gmail.com, keescook@chromium.org, kernel-hardening@lists.openwall.com Cc: linux-kernel@vger.kernel.org, wangkefeng.wang@huawei.com, yebin10@huawei.com, thunder.leizhen@huawei.com, jingxiangfeng@huawei.com, fanchengyang@huawei.com, zhaohongjiang@huawei.com References: <20190730074225.39544-1-yanaijie@huawei.com> <20190730074225.39544-8-yanaijie@huawei.com> From: Christophe Leroy Message-ID: Date: Tue, 30 Jul 2019 11:44:42 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20190730074225.39544-8-yanaijie@huawei.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: fr Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le 30/07/2019 à 09:42, Jason Yan a écrit : > After we have the basic support of relocate the kernel in some > appropriate place, we can start to randomize the offset now. > > Entropy is derived from the banner and timer, which will change every > build and boot. This not so much safe so additionally the bootloader may > pass entropy via the /chosen/kaslr-seed node in device tree. > > We will use the first 512M of the low memory to randomize the kernel > image. The memory will be split in 64M zones. We will use the lower 8 > bit of the entropy to decide the index of the 64M zone. Then we chose a > 16K aligned offset inside the 64M zone to put the kernel in. > > KERNELBASE > > |--> 64M <--| > | | > +---------------+ +----------------+---------------+ > | |....| |kernel| | | > +---------------+ +----------------+---------------+ > | | > |-----> offset <-----| > > kimage_vaddr > > We also check if we will overlap with some areas like the dtb area, the > initrd area or the crashkernel area. If we cannot find a proper area, > kaslr will be disabled and boot from the original kernel. > > Signed-off-by: Jason Yan > Cc: Diana Craciun > Cc: Michael Ellerman > Cc: Christophe Leroy > Cc: Benjamin Herrenschmidt > Cc: Paul Mackerras > Cc: Nicholas Piggin > Cc: Kees Cook > --- > arch/powerpc/kernel/kaslr_booke.c | 334 +++++++++++++++++++++++++++++- > 1 file changed, 332 insertions(+), 2 deletions(-) > > diff --git a/arch/powerpc/kernel/kaslr_booke.c b/arch/powerpc/kernel/kaslr_booke.c > index 960bce4aa8b9..0bb02e45b928 100644 > --- a/arch/powerpc/kernel/kaslr_booke.c > +++ b/arch/powerpc/kernel/kaslr_booke.c > @@ -23,6 +23,8 @@ > #include > #include > #include > +#include > +#include > #include > #include > #include > @@ -34,15 +36,341 @@ > #include > #include > #include > +#include > #include > +#include > +#include > + > +#ifdef DEBUG > +#define DBG(fmt...) printk(KERN_ERR fmt) > +#else > +#define DBG(fmt...) > +#endif > + > +struct regions { > + unsigned long pa_start; > + unsigned long pa_end; > + unsigned long kernel_size; > + unsigned long dtb_start; > + unsigned long dtb_end; > + unsigned long initrd_start; > + unsigned long initrd_end; > + unsigned long crash_start; > + unsigned long crash_end; > + int reserved_mem; > + int reserved_mem_addr_cells; > + int reserved_mem_size_cells; > +}; > > extern int is_second_reloc; > > +/* Simplified build-specific string for starting entropy. */ > +static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@" > + LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION; > + > +static __init void kaslr_get_cmdline(void *fdt) > +{ > + const char *cmdline = CONFIG_CMDLINE; > + if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) { > + int node; > + const u8 *prop; > + node = fdt_path_offset(fdt, "/chosen"); > + if (node < 0) > + goto out; > + > + prop = fdt_getprop(fdt, node, "bootargs", NULL); > + if (!prop) > + goto out; > + cmdline = prop; > + } > +out: > + strscpy(boot_command_line, cmdline, COMMAND_LINE_SIZE); boot_command_line is set by early_init_devtree() in arch/powerpc/kernel/prom.c Is that too late for you ? If so, what about calling early_init_dt_scan_chosen() instead of recoding ? Christophe