From patchwork Wed Mar 2 17:11:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 8484001 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id A1A87C0553 for ; Wed, 2 Mar 2016 17:14:44 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A190C20148 for ; Wed, 2 Mar 2016 17:14:43 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BAEE6200D0 for ; Wed, 2 Mar 2016 17:14:42 +0000 (UTC) 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 1abAKm-0002L7-VH; Wed, 02 Mar 2016 17:13:12 +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 1abAK1-0001lO-9O for linux-arm-kernel@lists.infradead.org; Wed, 02 Mar 2016 17:12:28 +0000 Received: by mail-wm0-x22a.google.com with SMTP id p65so89472438wmp.1 for ; Wed, 02 Mar 2016 09:12:04 -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=D99ogocoGOxL1SY0pLTzGRzZNbFJJzFS0SdG3X1CKng=; b=dpSl6ZZNqxTJNTXIQ1XQze0QFAYLtV74TN51GcgzhFJ8Gd9illneCXcQmFoBNtlF4J nQSPmWpV5E4ziXAoVvXcFEFPoE1k+5t7PXNjuLNcOv17he4WtkcBGshB032c3qq39bsM rjoHS6sL4RrjmZVsYoYVPsO7sxVBCZCuFSLSA= 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=D99ogocoGOxL1SY0pLTzGRzZNbFJJzFS0SdG3X1CKng=; b=E0PumdARtPi34CpcXLPTCxEl1BzZuyEJpekWiIyfv107Yz19ELJ/YinRYi+eHhmaiB rPhe/uPVsHmo5KGS8m6LCCZFIFA1AN/kSywdyMwIaeASCOi1M7JR+XFvHqWV5K9lrPtt h0zfwm3wYZR0kPa5QcCe3KSP0dYOm6kWUpkHFY+rWpQ5sUsjp6jKkAke9BV+TO24rEjb 0go2l6FtHIQp+7r+OoyaO7WuW3x4hAAP5JFGiCTE/rA0QRn86xNVouRdBwjuDhZFe/PF QoAcw+QnE0O6TWuJC9fPMWeI83NHgEa5y1SblLIF+/LinlktIqF2QqsOXAH96mC2lfAw /1Ww== X-Gm-Message-State: AD7BkJLEfCcfwv/LV+awTCnVhC9rRmNV2JxDSzpJp6FMlJgRSoZEhoEQM9ztLltzqsxFUG6D X-Received: by 10.28.133.14 with SMTP id h14mr1067170wmd.100.1456938723682; Wed, 02 Mar 2016 09:12:03 -0800 (PST) Received: from localhost.localdomain ([195.55.142.58]) by smtp.gmail.com with ESMTPSA id gk4sm18023317wjd.7.2016.03.02.09.12.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 02 Mar 2016 09:12:03 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, keescook@chromium.org, catalin.marinas@arm.com, mark.rutland@arm.com Subject: [PATCH 3/3] arm64: kaslr: increase randomization granularity Date: Wed, 2 Mar 2016 18:11:52 +0100 Message-Id: <1456938712-11089-4-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1456938712-11089-1-git-send-email-ard.biesheuvel@linaro.org> References: <1456938712-11089-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160302_091225_812531_DEA492D3 X-CRM114-Status: GOOD ( 15.56 ) X-Spam-Score: -2.7 (--) 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: matt@codeblueprint.co.uk, david.brown@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, our KASLR implementation randomizes the placement of the core kernel at 2 MB granularity. This is based on the arm64 kernel boot protocol, which mandates that the kernel is loaded TEXT_OFFSET bytes above a 2 MB aligned base address. This requirement is a result of the fact that the block size used by the early mapping code may be 2 MB at the most (for a 4 KB granule kernel) But we can do better than that: since a KASLR kernel needs to be relocated in any case, we can tolerate a physical misalignment as long as the virtual misalignment is equal in size, and code to deal with this is already in place. The actual minimal alignment of the core kernel is either PAGE_SIZE or THREAD_SIZE, whichever is greater. The former is obvious, but the latter is due to the fact that the init stack is expected to live at an offset which is a round multiple of its size. The higher granularity adds between 5 and 7 bits of entropy, depending on page size. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/arm64-stub.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index e0e6b74fef8f..84584e7847df 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -61,15 +61,23 @@ efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table_arg, if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) { /* + * Produce a displacement in the interval [0, MIN_KIMG_ALIGN) + * that is a multiple of the actual minimal kernel alignment + * (either PAGE_SIZE or THREAD_SIZE, whichever is greater) + */ + const u32 offset = (phys_seed >> 32) & (MIN_KIMG_ALIGN - 1) & + ~(max_t(u32, PAGE_SIZE, THREAD_SIZE) - 1); + + /* * If KASLR is enabled, and we have some randomness available, * locate the kernel at a randomized offset in physical memory. */ - *reserve_size = kernel_memsize + TEXT_OFFSET; + *reserve_size = kernel_memsize + offset; status = efi_random_alloc(sys_table_arg, *reserve_size, MIN_KIMG_ALIGN, reserve_addr, - phys_seed); + (u32)phys_seed); - *image_addr = *reserve_addr + TEXT_OFFSET; + *image_addr = *reserve_addr + offset; } else { /* * Else, try a straight allocation at the preferred offset.