Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp5517253ybi; Tue, 30 Jul 2019 22:35:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyQPLQh+n/XfmAk5wYBf9dGat3mz8CsFcZNeQE/lVMCaWqNyTsXZ50xHba7XSrLYuT7W4e0 X-Received: by 2002:aa7:84d4:: with SMTP id x20mr44944414pfn.60.1564551338552; Tue, 30 Jul 2019 22:35:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564551338; cv=none; d=google.com; s=arc-20160816; b=FiqlhZ/Ywg6HH+Yhjfrvbsq9a4wz0DV0mQtKdDPo0p19huduU2Ltl+QOzrWonPhfGN L2kiRAXmVopeuCUWTuFNJyMirDH338i7jNDphzDlShiqyGPIRYXMxV1r2YDpaDAd0qN9 1eXzTxepsSFKgmUS1x+m152nW3vdtDvjNyrZZGdPi1j/uSadfQfFom57qb+XtF+D18JE WdtoiqtxA7/5ouuEr3ienx4x7eG95kh9tpPeXYxmsPsNQo8J6Xr5Va9NzchnfWMLe0os ISx2ppRWIsjMjR9QRVy/hO9KSnb9x5YYDMbOZ5z6/Jxl+UP8YKwXcpKUTbVTOKbuzBKr hPSg== 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; bh=tzwaRgwMOoxgzcRpjFS+U1zYl9vZMWrmrYjauyyqPR8=; b=ITOX+SurwCUKcyvQbRBAwXkKuwYZyjkRrzrJ2G/iglwEBhtzJvwqMjlWymMY7EyoDh LU26XKajNT8hLLk/O6amr88N9Juh5VzesOciZ4YJYe66Z644YbCFfxucV94bNMBZlyR+ 09NscYlvf/pUol4QxVTxAVlg5zrGdS0YNm/dI24aSuPlzjoi2b6iFxCCH4AgGao4Rd0D 57m4vD2uBGp5S06lmQvV9LrwIu1ndTzH913nAX6679H1hCTH2t6+KA1XCyWKU+TSqKL2 8fttBWMDCpMncXO+qn1hL0SCywLF61SsP5BmMOUJ0rJFExHxAyWoE7fANU85QTSNPh90 9q9Q== ARC-Authentication-Results: i=1; mx.google.com; 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 y187si34567845pgb.480.2019.07.30.22.35.23; Tue, 30 Jul 2019 22:35:38 -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; 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 S1728054AbfGaCHJ (ORCPT + 99 others); Tue, 30 Jul 2019 22:07:09 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:3660 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727300AbfGaCHJ (ORCPT ); Tue, 30 Jul 2019 22:07:09 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 8E1B96ADE25FEEF3F3FE; Wed, 31 Jul 2019 10:07:06 +0800 (CST) Received: from [127.0.0.1] (10.177.96.203) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Wed, 31 Jul 2019 10:06:56 +0800 Subject: Re: [PATCH v2 07/10] powerpc/fsl_booke/32: randomize the kernel image offset To: Christophe Leroy , , , , , , , , CC: , , , , , , References: <20190730074225.39544-1-yanaijie@huawei.com> <20190730074225.39544-8-yanaijie@huawei.com> From: Jason Yan Message-ID: Date: Wed, 31 Jul 2019 10:06:55 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [10.177.96.203] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/7/30 17:44, Christophe Leroy wrote: > > > 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 ? > Yes, it's too late. > If so, what about calling early_init_dt_scan_chosen() instead of recoding ? > Good suggestion. I will have a try. > Christophe > > . >