From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E284C31E40 for ; Wed, 7 Aug 2019 03:18:23 +0000 (UTC) Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.kernel.org (Postfix) with SMTP id 61B2C214C6 for ; Wed, 7 Aug 2019 03:18:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 61B2C214C6 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kernel-hardening-return-16730-kernel-hardening=archiver.kernel.org@lists.openwall.com Received: (qmail 32394 invoked by uid 550); 7 Aug 2019 03:18:13 -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: Received: (qmail 32356 invoked from network); 7 Aug 2019 03:18:04 -0000 Subject: Re: [PATCH v4 07/10] powerpc/fsl_booke/32: randomize the kernel image offset To: Christophe Leroy , , , , , , , , CC: , , , , , , References: <20190805064335.19156-1-yanaijie@huawei.com> <20190805064335.19156-8-yanaijie@huawei.com> <3edec35b-8d61-07ff-558d-2d7e0c28a0e2@c-s.fr> From: Jason Yan Message-ID: <16e058a4-9794-6998-46e4-0e63b9fce7e3@huawei.com> Date: Wed, 7 Aug 2019 11:16:19 +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: <3edec35b-8d61-07ff-558d-2d7e0c28a0e2@c-s.fr> 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 On 2019/8/6 15:56, Christophe Leroy wrote: > > > Le 05/08/2019 à 08:43, 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 >> Reviewed-by: Diana Craciun >> Tested-by: Diana Craciun > > Reviewed-by: Christophe Leroy > Thanks for your help, > One small comment below > >> --- >>   arch/powerpc/kernel/kaslr_booke.c | 322 +++++++++++++++++++++++++++++- >>   1 file changed, 320 insertions(+), 2 deletions(-) >> >> diff --git a/arch/powerpc/kernel/kaslr_booke.c >> b/arch/powerpc/kernel/kaslr_booke.c >> index 30f84c0321b2..97250cad71de 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,329 @@ >>   #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) >> +{ >> +    int node = fdt_path_offset(fdt, "/chosen"); >> + >> +    early_init_dt_scan_chosen(node, "chosen", 1, boot_command_line); >> +} >> + >> +static unsigned long __init rotate_xor(unsigned long hash, const void >> *area, >> +                       size_t size) >> +{ >> +    size_t i; >> +    unsigned long *ptr = (unsigned long *)area; > > As area is a void *, this cast shouldn't be necessary. Or maybe it is > necessary because it discards the const ? > It's true the cast is not necessary. The ptr can be made const and then remove the cast. > Christophe >