Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp4887067ybh; Tue, 6 Aug 2019 20:17:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqy+RaTFJEUPyOC6WRzg7stKZt+cSNQHXpI07uuoIYlyeWMT3Y+6TqoBt4UxhlwnQYrA70eL X-Received: by 2002:a17:902:a504:: with SMTP id s4mr6056500plq.117.1565147845816; Tue, 06 Aug 2019 20:17:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565147845; cv=none; d=google.com; s=arc-20160816; b=WEGGUGfqw+rTarjDlBwwwLMjewZ043ss4KZYSU35wtvi7MN/Ne1nSaDgMPGImi42NU 2OkVN01lghOqtShrwCKpj/NfvYz+VBxeB+MJ9aSZ2+dIAGOJtC+Y6tEwhMYcXZCL7mKY ZmZAArsg32iTnKnADwUiEvEzeP26HAzVP2HJd7fVxGr4AAxp4FVaz2wpWtIheoKs+8AB O+DPgRch0xO3w6kgDGmdzKn5y0EnLxnV52lyIpudY9rslbZFmBA5aB3HZBYqTmyZidkE hXJsg2h7zMcJoTYJdxpSAhlPeVEbxDulJ2/V39E2G+suRd8FHSdBF61FN/WQaIw5yqIx cW4g== 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=+hqqDX3H/HPxH4x8b9HQLmPpyBG5onMJQfa4/7/sKK8=; b=fkuoQ5Ft9LtVRl4C4nVMdzYLszh/W/ELMc3gp+G5U0bQ32kLVrMqg5R5v6D8Bf9/xT tIZRxIuh4B4VIjQiRRS2rKmCMjSkCsl85rkpZhLuwx2bMcDddCllh6gNvEjGjLVKTxCL nTmwkC0BMDFpSLatJPFb4vV+IjVKV+ybhXp1wWYiorPmw3huyMumXYItCHO7rmiYznkT w645fSK4DqfRcw9fHjUUJhuCQ5nkxdtwLlJwt+AOVgCrJg9bZjVWudQ8I/5h0cu8017j zSc06ESo1OYRi8f08cxHhIrDoJ7SiL1kegwfN6W+4ZBpu7Y3QcDeF1TORm/cWenrsu1q bS/A== 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 65si49811947pfg.241.2019.08.06.20.17.10; Tue, 06 Aug 2019 20:17:25 -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 S1727541AbfHGDQb (ORCPT + 99 others); Tue, 6 Aug 2019 23:16:31 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:54958 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727359AbfHGDQa (ORCPT ); Tue, 6 Aug 2019 23:16:30 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 10C93FA9AE40F21242A4; Wed, 7 Aug 2019 11:16:28 +0800 (CST) Received: from [127.0.0.1] (10.177.96.203) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.439.0; Wed, 7 Aug 2019 11:16:20 +0800 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 >