From patchwork Mon Apr 17 09:32:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 97509 Delivered-To: patch@linaro.org Received: by 10.140.109.52 with SMTP id k49csp1264084qgf; Mon, 17 Apr 2017 02:33:42 -0700 (PDT) X-Received: by 10.84.241.65 with SMTP id u1mr13080605plm.28.1492421622273; Mon, 17 Apr 2017 02:33:42 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d66si10569829pfb.67.2017.04.17.02.33.42; Mon, 17 Apr 2017 02:33:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757068AbdDQJdS (ORCPT + 2 others); Mon, 17 Apr 2017 05:33:18 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:35096 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756963AbdDQJdQ (ORCPT ); Mon, 17 Apr 2017 05:33:16 -0400 Received: by mail-wm0-f44.google.com with SMTP id w64so30526221wma.0 for ; Mon, 17 Apr 2017 02:33:15 -0700 (PDT) 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=st+X3v6pF7HzpHbD3Ryc4UnLdXW929lwz1UBMXFWfj0=; b=XxI4EBxIOH4ov6DSGHCP1cAGIeFqYlx7mAzut3Hv4Z0asjAD6v7lqlMGOb+ceORm6u ex2JUm0t/oQ2amgaiYHAx581Yp4qCh0z6Z+WUT4vTez877W8qHNmvIfpc9xgtKb/WFMG oiQ2cSaO1nNlsK0XpK+xnULyBViaCWMeE6R7E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=st+X3v6pF7HzpHbD3Ryc4UnLdXW929lwz1UBMXFWfj0=; b=Y8sQwyfC4Eq37nwn6mOWKJ7fecQME7xKcBe3EZo+VOejuDRAY/JkTLeB+dVlKUXeH3 9/Ud3cVP+BBuToNQ9/QsXUC6rhkIwLs8w5mIewsnsU9zXephm8SE+F3Qdwt/4A/HzAFC 5vNyEK/Kx454qegvnjDPiEALl+cZibJRYl6E6JY48c3KlFW8qETbbyN7xHLVyezhDS5h vdjaz2bgLtcTcbNZBuatlpApBK9QMl4xczdc9LngQNzQMFvb11clxFQCdfpC3MpKcuHD dPRGtqKDhQcX/RIo8LGZE/pVsHLuN3S9L44kio3FH0R4UlDWwjqPkwEpNby6LZ3HdEut nn/A== X-Gm-Message-State: AN3rC/7IS/EF5D4jMrMNAFBQD3oDhbvs09IQbEofjuiZZDJme8tnM07T dengmp4WRppwqexp X-Received: by 10.28.209.204 with SMTP id i195mr8295632wmg.113.1492421594976; Mon, 17 Apr 2017 02:33:14 -0700 (PDT) Received: from localhost.localdomain ([105.150.28.134]) by smtp.gmail.com with ESMTPSA id o123sm9753084wmg.16.2017.04.17.02.33.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Apr 2017 02:33:14 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, Ingo Molnar , Thomas Gleixner , "H . Peter Anvin" Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, Matt Fleming Subject: [PATCH] efi/libstub/arm: Don't use TASK_SIZE when randomising the RT space Date: Mon, 17 Apr 2017 10:32:01 +0100 Message-Id: <20170417093201.10181-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170417093201.10181-1-ard.biesheuvel@linaro.org> References: <20170417093201.10181-1-ard.biesheuvel@linaro.org> Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org As reported by James, Catalin and Mark, commit e69176d68d26 ("ef/libstub/arm/arm64: Randomize the base of the UEFI rt services region") results in a crash in the firmware regardless of whether KASLR is in effect or not, and whether the firmware implements EFI_RNG_PROTOCOL or not. Mark has identified the root cause to be the inappropriate use of TASK_SIZE in the stub, which arm64 defines as #define TASK_SIZE (test_thread_flag(TIF_32BIT) ? \ TASK_SIZE_32 : TASK_SIZE_64) and testing thread flags at this point results in the dereference of pointers in uninitialized structures. So instead, introduce a preprocessor symbol EFI_RT_VIRTUAL_LIMIT and define it to TASK_SIZE_64 on arm64 and TASK_SIZE on ARM, both of which are compile time constants. Also, change the 'headroom' variable to static const to force an error if this might change in the future. Cc: Matt Fleming Tested-by: Mark Rutland Tested-by: James Morse Tested-by: Catalin Marinas Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/arm-stub.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) -- 2.9.3 -- To unsubscribe from this list: send the line "unsubscribe linux-efi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/firmware/efi/libstub/arm-stub.c b/drivers/firmware/efi/libstub/arm-stub.c index 1e45ec51b094..34010ff3b77e 100644 --- a/drivers/firmware/efi/libstub/arm-stub.c +++ b/drivers/firmware/efi/libstub/arm-stub.c @@ -32,6 +32,12 @@ #define EFI_RT_VIRTUAL_BASE SZ_512M #define EFI_RT_VIRTUAL_SIZE SZ_512M +#ifdef CONFIG_ARM64 +#define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_64 +#else +#define EFI_RT_VIRTUAL_LIMIT TASK_SIZE +#endif + static u64 virtmap_base = EFI_RT_VIRTUAL_BASE; efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg, @@ -236,8 +242,9 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, * shift of 21 bit positions into account when scaling * the headroom value using a 32-bit random value. */ - u64 headroom = TASK_SIZE - EFI_RT_VIRTUAL_BASE - - EFI_RT_VIRTUAL_SIZE; + static const u64 headroom = EFI_RT_VIRTUAL_LIMIT - + EFI_RT_VIRTUAL_BASE - + EFI_RT_VIRTUAL_SIZE; u32 rnd; status = efi_get_random_bytes(sys_table, sizeof(rnd),