From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760207AbcCDQ2D (ORCPT ); Fri, 4 Mar 2016 11:28:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:35334 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759494AbcCDQ14 (ORCPT ); Fri, 4 Mar 2016 11:27:56 -0500 From: Baoquan He To: linux-kernel@vger.kernel.org Cc: yinghai@kernel.org, keescook@chromium.org, hpa@zytor.com, vgoyal@redhat.com, mingo@redhat.com, bp@alien8.de, luto@kernel.org, lasse.collin@tukaani.org, akpm@linux-foundation.org, dyoung@redhat.com, Baoquan He Subject: [PATCH v3 15/19] x86, kaslr: Introduce fetch_random_virt_offset to randomize the kernel text mapping address Date: Sat, 5 Mar 2016 00:25:13 +0800 Message-Id: <1457108717-12191-16-git-send-email-bhe@redhat.com> In-Reply-To: <1457108717-12191-1-git-send-email-bhe@redhat.com> References: <1457108717-12191-1-git-send-email-bhe@redhat.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Kaslr extended kernel text mapping region size from 512M to 1G, namely CONFIG_RANDOMIZE_BASE_MAX_OFFSET. This means kernel text can be mapped to below region: [__START_KERNEL_map + LOAD_PHYSICAL_ADDR, __START_KERNEL_map + 1G] Introduce a function find_random_virt_offset() to get random value between LOAD_PHYSICAL_ADDR and CONFIG_RANDOMIZE_BASE_MAX_OFFSET. This random value will be added to __START_KERNEL_map to get the starting address which kernel text is mapped from. Since slot can be anywhere of this region, means it is an independent slot_area, it is very simple to get a slot according to random value. Signed-off-by: Baoquan He --- arch/x86/boot/compressed/aslr.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/x86/boot/compressed/aslr.c b/arch/x86/boot/compressed/aslr.c index fa6192b..3730a33 100644 --- a/arch/x86/boot/compressed/aslr.c +++ b/arch/x86/boot/compressed/aslr.c @@ -366,6 +366,27 @@ static unsigned long find_random_addr(unsigned long minimum, return slots_fetch_random(); } +static unsigned long find_random_virt_offset(unsigned long minimum, + unsigned long image_size) +{ + unsigned long slot_num, random; + + /* Make sure minimum is aligned. */ + minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); + + if (image_size <= CONFIG_PHYSICAL_ALIGN) + slot_num = (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - minimum) / + CONFIG_PHYSICAL_ALIGN; + else + slot_num = (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - + minimum - image_size) / + CONFIG_PHYSICAL_ALIGN + 1; + + random = get_random_long() % slot_num; + + return random * CONFIG_PHYSICAL_ALIGN + minimum; +} + unsigned char *choose_kernel_location(unsigned char *input, unsigned long input_size, unsigned char *output, -- 2.5.0