Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759181AbdCVJ10 (ORCPT ); Wed, 22 Mar 2017 05:27:26 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:34110 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759163AbdCVJ1Y (ORCPT ); Wed, 22 Mar 2017 05:27:24 -0400 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] arm64: kaslr: Add 2MB correction for aligning kernel image From: Ard Biesheuvel X-Mailer: iPhone Mail (14D27) In-Reply-To: <1490172943-826-1-git-send-email-sramana@codeaurora.org> Date: Wed, 22 Mar 2017 09:27:13 +0000 Cc: catalin.marinas@arm.com, will.deacon@arm.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Neeraj Upadhyay Message-Id: <904FACBF-3DFE-4DDE-ACB5-7109A137D477@linaro.org> References: <1490172943-826-1-git-send-email-sramana@codeaurora.org> To: Srinivas Ramana Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by mail.home.local id v2M9RaHL010886 Content-Length: 2849 Lines: 74 > On 22 Mar 2017, at 08:55, Srinivas Ramana wrote: > > From: Neeraj Upadhyay > > If kernel image extends across alignment boundary, existing > code increases the KASLR offset by size of kernel image. The > offset is masked after resizing. There are cases, where after > masking, we may still have kernel image extending across > boundary. This eventually results in only 2MB block getting > mapped while creating the page tables. This results in data aborts > while accessing unmapped regions during second relocation (with > kaslr offset) in __primary_switch. To fix this problem, add a > 2MB correction to offset along with the correction of kernel > image size, before applying mask. > > For example consider below case, where kernel image still crosses > 1GB alignment boundary, after masking the offset, which is fixed > by adding 2MB correction. > > SWAPPER_TABLE_SHIFT = 30 > Swapper using section maps with section size 2MB. > CONFIG_PGTABLE_LEVELS = 3 > VA_BITS = 39 > > _text : 0xffffff8008080000 > _end : 0xffffff800aa1b000 > offset : 0x1f35600000 > mask = ((1UL << (VA_BITS - 2)) - 1) & ~(SZ_2M - 1) > > (_text + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7c > (_end + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7d > > offset after existing correction (before mask) = 0x1f37f9b000 > (_text + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7d > (_end + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7d > > offset (after mask) = 0x1f37e00000 > (_text + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7c > (_end + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7d > > new offset w/ 2MB correction (before mask) = 0x1f37819b00 > new offset w/ 2MB correction (after mask) = 0x1f38000000 > (_text + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7d > (_end + offset) >> SWAPPER_TABLE_SHIFT = 0x3fffffe7d > > Fixes: f80fb3a3d508 ("arm64: add support for kernel ASLR") > Signed-off-by: Neeraj Upadhyay > Signed-off-by: Srinivas Ramana > --- > arch/arm64/kernel/kaslr.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/arm64/kernel/kaslr.c b/arch/arm64/kernel/kaslr.c > index 769f24ef628c..7b8af985e497 100644 > --- a/arch/arm64/kernel/kaslr.c > +++ b/arch/arm64/kernel/kaslr.c > @@ -135,7 +135,7 @@ u64 __init kaslr_early_init(u64 dt_phys, u64 modulo_offset) > */ > if ((((u64)_text + offset + modulo_offset) >> SWAPPER_TABLE_SHIFT) != > (((u64)_end + offset + modulo_offset) >> SWAPPER_TABLE_SHIFT)) > - offset = (offset + (u64)(_end - _text)) & mask; > + offset = (offset + (u64)(_end - _text) + SZ_2M) & mask; > > if (IS_ENABLED(CONFIG_KASAN)) > /* Hi, Thanks for spotting this! Instead of adding 2 MB, could we round up _end - _text to a SWAPPER_BLOCK_SIZE multiple instead?