Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1592499imm; Wed, 8 Aug 2018 21:33:18 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzNFUSEC1rMN2/hA1RxCgwYA+Uwtls8fh34r/u3e0ND+48h3pxkXO6shmWkYhFn3wZ3i23Y X-Received: by 2002:a65:608b:: with SMTP id t11-v6mr522656pgu.259.1533789198202; Wed, 08 Aug 2018 21:33:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533789198; cv=none; d=google.com; s=arc-20160816; b=A9AH6HIbju5us0Pl1mFRStWp6ZIfO/9YHuoqhKHWYgTAbgeFhCmPX8n17dAYwtCrJ/ 5iAnZZGDwxT9R/6J0ei+Q4Rd4+9Q4zZ/x+K0Pt3buUc4CznCmwGBFmFd+r68xpzHivIn LBBDsTHzfyRMRHvzJmsGb7SHOzS4BPEsPxj/6Iq5LzCO+XhGitGXwrqq8/YGnsJMM9/e mFnC/xGUDxCQqtUe8Xf4Io6JCEFdUmX9YjCI8acVXsm62wkowdmm1Wn2Z6d0FPxeufyw Ov2mF4GJ3rbrD1YpIqMlFfCl9CBFWVH3Sgan+zNkSZYaCfIRvfqD1jeuc/5TSKDoYOK8 7G3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=r3Gq5ca7dTIj6LfZjUtVFjb5DIoG+wW12KWLVeLTLU4=; b=x4N9dUfFTGgieNCzsk7Dk8awOkfwNuKB9PtP1jOIs8rrfJP26U8BALarQRiygYgmvV fPIX+26UzZPFp4kZ4gdj7N74wu0qiWL7GmxMhmXesZG4NxpmGytifiPKI5LVNzxScdXw qloZW3oUgyToouQjKQbfQSmq4iCKrTwwMReVq5Cyru2uA4xr1VcgO35+AcxWbtSYKKfq hvYyMk33NVNidbCs1UyJPjX6pm5gI6ZfqtP8T/styHhmm/6uS5izj57srElwMWyT4aWk 0agw9taOzeKZYBOhMU/AzQwZfQ/9Nd1fYjd9CrwWf5ojWdm1PcCLTF7vQzRCbO3yLT4A EH9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m64-v6si6550334pfc.17.2018.08.08.21.33.04; Wed, 08 Aug 2018 21:33:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728599AbeHIGy5 (ORCPT + 99 others); Thu, 9 Aug 2018 02:54:57 -0400 Received: from mga11.intel.com ([192.55.52.93]:22077 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727211AbeHIGyz (ORCPT ); Thu, 9 Aug 2018 02:54:55 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Aug 2018 21:32:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,213,1531810800"; d="scan'208";a="61024070" Received: from sai-dev-mach.sc.intel.com ([143.183.140.52]) by fmsmga007.fm.intel.com with ESMTP; 08 Aug 2018 21:32:04 -0700 From: Sai Praneeth Prakhya To: linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ricardo.ner@intel.com, matt@codeblueprint.co.uk, Sai Praneeth , Lee Chun-Yi , Al Stone , Borislav Petkov , Ingo Molnar , Andy Lutomirski , Bhupesh Sharma , Peter Zijlstra , Ard Biesheuvel Subject: [PATCH V1 6/6] x86/efi: Introduce EFI_WARN_ON_ILLEGAL_ACCESSES Date: Wed, 8 Aug 2018 21:31:17 -0700 Message-Id: <1533789077-16156-7-git-send-email-sai.praneeth.prakhya@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1533789077-16156-1-git-send-email-sai.praneeth.prakhya@intel.com> References: <1533789077-16156-1-git-send-email-sai.praneeth.prakhya@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth There may exist some buggy UEFI firmware implementations that might access efi regions other than EFI_RUNTIME_SERVICES_ even after the kernel has assumed control of the platform. This violates UEFI specification. If selected, this debug option will print a warning message if the UEFI firmware tries to access any memory region which it shouldn't. Along with the warning, the efi page fault handler will also try to fixup/recover from the page fault triggered by the firmware so that the machine doesn't hang. To support this feature, two changes should be made to the existing efi subsystem 1. Map EFI_BOOT_SERVICES_ regions only when EFI_WARN_ON_ILLEGAL_ACCESSES is disabled Presently, the kernel maps EFI_BOOT_SERVICES_ regions as a workaround for buggy firmware that accesses them even when they shouldn't. With EFI_WARN_ON_ILLEGAL_ACCESSES enabled (and hence efi page fault handler) kernel can now detect and handle such accesses dynamically. Hence, rather than safely mapping EFI_BOOT_SERVICES_ regions *all* the time, map them on demand. 2. If EFI_WARN_ON_ILLEGAL_ACCESSES is enabled don't call efi_free_boot_services() Presently, during early boot phase EFI_BOOT_SERVICES_ regions are marked as reserved by kernel (see efi_reserve_boot_services()) and are freed before entering runtime (see efi_free_boot_services()). But, while dynamically fixing page faults caused by the firmware, efi page fault handler assumes that EFI_BOOT_SERVICES_ regions are still intact. Hence, to make this assumption true, don't call efi_free_boot_services() if EFI_WARN_ON_ILLEGAL_ACCESSES is enabled. Suggested-by: Matt Fleming Based-on-code-from: Ricardo Neri Signed-off-by: Sai Praneeth Prakhya Cc: Lee Chun-Yi Cc: Al Stone Cc: Borislav Petkov Cc: Ingo Molnar Cc: Andy Lutomirski Cc: Bhupesh Sharma Cc: Peter Zijlstra Cc: Ard Biesheuvel --- arch/x86/Kconfig | 21 +++++++++++++++++++++ arch/x86/platform/efi/efi.c | 4 ++++ init/main.c | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index f1dbb4ee19d7..278e5820e8dd 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1957,6 +1957,27 @@ config EFI_MIXED If unsure, say N. +config EFI_WARN_ON_ILLEGAL_ACCESSES + bool "Warn about illegal memory accesses by firmware" + depends on EFI + help + Enable this debug feature so that the kernel can detect illegal + memory accesses by firmware and issue a warning. Also, + 1. If the illegally accessed region is EFI_BOOT_SERVICES_, + the kernel fixes it up by mapping the requested region. + 2. If the illegally accessed region is any other region (Eg: + EFI_CONVENTIONAL_MEMORY or EFI_LOADER_), then the + kernel freezes efi_rts_wq and schedules a new process. Also, it + disables EFI Runtime Services, so that it will never again call + buggy firmware. + 3. If the access is to any other efi region like above but if the + buggy efi runtime service is efi_reset_system(), then the + platform is rebooted through BIOS. + Please see the UEFI specification for details on the expectations + of memory usage. + + If unsure, say N. + config SECCOMP def_bool y prompt "Enable seccomp to safely compute untrusted bytecode" diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 7d18b7ed5d41..0ddb22a03d88 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -768,9 +768,13 @@ static bool should_map_region(efi_memory_desc_t *md) /* * Map boot services regions as a workaround for buggy * firmware that accesses them even when they shouldn't. + * (only if CONFIG_EFI_WARN_ON_ILLEGAL_ACCESSES is disabled) * * See efi_{reserve,free}_boot_services(). */ + if (IS_ENABLED(CONFIG_EFI_WARN_ON_ILLEGAL_ACCESSES)) + return false; + if (md->type == EFI_BOOT_SERVICES_CODE || md->type == EFI_BOOT_SERVICES_DATA) return true; diff --git a/init/main.c b/init/main.c index 3b4ada11ed52..dce0520861a1 100644 --- a/init/main.c +++ b/init/main.c @@ -730,7 +730,8 @@ asmlinkage __visible void __init start_kernel(void) arch_post_acpi_subsys_init(); sfi_init_late(); - if (efi_enabled(EFI_RUNTIME_SERVICES)) { + if (efi_enabled(EFI_RUNTIME_SERVICES) && + !IS_ENABLED(CONFIG_EFI_WARN_ON_ILLEGAL_ACCESSES)) { efi_free_boot_services(); } -- 2.7.4