From patchwork Mon Feb 1 13:09:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 60916 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp3071946lbb; Mon, 1 Feb 2016 05:18:17 -0800 (PST) X-Received: by 10.66.119.71 with SMTP id ks7mr38630430pab.151.1454332697731; Mon, 01 Feb 2016 05:18:17 -0800 (PST) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id z3si25435753par.37.2016.02.01.05.18.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Feb 2016 05:18:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aQELs-0006Lf-Aq; Mon, 01 Feb 2016 13:17:08 +0000 Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aQEFG-0005wr-93 for linux-arm-kernel@lists.infradead.org; Mon, 01 Feb 2016 13:10:22 +0000 Received: by mail-wm0-x22a.google.com with SMTP id p63so69596049wmp.1 for ; Mon, 01 Feb 2016 05:09:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xNoTwD/1oOejHDpZKHSLGgLxyIHRYtblUAP3ft1kUY0=; b=bomCkFWay1VeLQE42lORW/mFFhE1U3ox/ehV+Fk5XXGzE4DaTHnh0oVqzW6b4ClkkR nSoo9xaHDYZhUd0vqaA2ihGWkXUUvYwjIaNZom5lvYGOp9jBhTLYuoeRiwiX2A7MA6OX OV0WB9+doTUVC6wHgh9m3VaO0f/ch82kp8qSQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xNoTwD/1oOejHDpZKHSLGgLxyIHRYtblUAP3ft1kUY0=; b=OUu4Y3M+e6DqIx885pM0D4NpASQG22aYgk4ZvoFmuSfgqFWEwXNHfir329Witqt3KT CI9jF3dcCmZT+1n/H7rty7S+deUAarikaajS8ZuguFbPIbBfV4iCU7tWyJIxuZFwjSLa Vm7cHCmaci4FZaS0lBi6xokabXJTxGQHfejaWOJaL6YK7HsWke01VIvT9jhfp8k1+Z+9 +wb8ICcysgATpJKJtN7HNZq0iUdpNq88kLKoZvTG5uNx4YDn2D2jY5jGrZqDHkBSJoeQ OT5zhGXQ6SL0FYngnr7Va4KGmNeeBZD2iDM7v5bVLdpUQpSBhWFmTJJ9TdUh5kEFrmYr o5fA== X-Gm-Message-State: AG10YORIuSuBC4hAHJKSGv7xAvsBjuqHU1TSaLInmYJ4bt+OuHa/5Z1T8uvGoku6Zl2XIm8V X-Received: by 10.194.21.163 with SMTP id w3mr22321220wje.58.1454332197563; Mon, 01 Feb 2016 05:09:57 -0800 (PST) Received: from localhost.localdomain ([195.55.142.58]) by smtp.gmail.com with ESMTPSA id e9sm28992124wja.25.2016.02.01.05.09.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Feb 2016 05:09:56 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, catalin.marinas@arm.com, will.deacon@arm.com, mark.rutland@arm.com, keescook@chromium.org Subject: [PATCH v5sub2 8/8] arm64: kaslr: randomize the linear region Date: Mon, 1 Feb 2016 14:09:38 +0100 Message-Id: <1454332178-4414-9-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1454332178-4414-1-git-send-email-ard.biesheuvel@linaro.org> References: <1454332178-4414-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160201_051018_937933_82D6DFD1 X-CRM114-Status: GOOD ( 16.18 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:22a listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org When KASLR is enabled (CONFIG_RANDOMIZE_BASE=y), and entropy has been provided by the bootloader, randomize the placement of RAM inside the linear region if sufficient space is available. For instance, on a 4KB granule/3 levels kernel, the linear region is 256 GB in size, and we can choose any 1 GB aligned offset that is far enough from the top of the address space to fit the distance between the start of the lowest memblock and the top of the highest memblock. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/kaslr.c | 4 ++++ arch/arm64/mm/init.c | 22 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) -- 2.5.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/arch/arm64/kernel/kaslr.c b/arch/arm64/kernel/kaslr.c index b0bf628ba51f..c96f63670537 100644 --- a/arch/arm64/kernel/kaslr.c +++ b/arch/arm64/kernel/kaslr.c @@ -21,6 +21,7 @@ #include u32 __read_mostly module_load_offset; +u16 __initdata memstart_offset_seed; static __init u64 get_kaslr_seed(void *fdt) { @@ -135,5 +136,8 @@ u64 __init kaslr_early_init(u64 dt_phys) module_range = MODULES_VSIZE - (u64)(_etext - _stext); module_load_offset = ((module_range * (u16)seed) >> 16) & PAGE_MASK; + /* use the top 16 bits to randomize the linear region */ + memstart_offset_seed = seed >> 48; + return offset; } diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index e8e853a1024c..01fdb3d04bba 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -215,6 +215,23 @@ void __init arm64_memblock_init(void) if (memblock_end_of_DRAM() > linear_region_size) memblock_remove(0, memblock_end_of_DRAM() - linear_region_size); + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { + extern u16 memstart_offset_seed; + u64 range = linear_region_size - + (memblock_end_of_DRAM() - memblock_start_of_DRAM()); + + /* + * If the size of the linear region exceeds, by a sufficient + * margin, the size of the region that the available physical + * memory spans, randomize the linear region as well. + */ + if (memstart_offset_seed > 0 && range >= ARM64_MEMSTART_ALIGN) { + range = range / ARM64_MEMSTART_ALIGN + 1; + memstart_addr -= ARM64_MEMSTART_ALIGN * + ((range * memstart_offset_seed) >> 16); + } + } + if (memory_limit != (phys_addr_t)ULLONG_MAX) { u64 kbase = round_down(__pa(_text), MIN_KIMG_ALIGN); u64 kend = PAGE_ALIGN(__pa(_end)); @@ -399,12 +416,13 @@ void __init mem_init(void) #ifdef CONFIG_SPARSEMEM_VMEMMAP MLG((unsigned long)vmemmap, (unsigned long)vmemmap + VMEMMAP_SIZE), - MLM((unsigned long)virt_to_page(PAGE_OFFSET), + MLM((unsigned long)phys_to_page(memblock_start_of_DRAM()), (unsigned long)virt_to_page(high_memory)), #endif MLK(FIXADDR_START, FIXADDR_TOP), MLM(PCI_IO_START, PCI_IO_END), - MLM(PAGE_OFFSET, (unsigned long)high_memory)); + MLM(__phys_to_virt(memblock_start_of_DRAM()), + (unsigned long)high_memory)); #undef MLK #undef MLM