From patchwork Tue Dec 13 18:04:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Jones X-Patchwork-Id: 13072305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DACD4C4167B for ; Tue, 13 Dec 2022 18:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236388AbiLMSFG (ORCPT ); Tue, 13 Dec 2022 13:05:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236362AbiLMSFE (ORCPT ); Tue, 13 Dec 2022 13:05:04 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07ACA23E99 for ; Tue, 13 Dec 2022 10:04:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670954655; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JF2s0tm6WDQRqQHFNtrMHbtnveQJl7hXDtEAPn+pSMA=; b=WExQXCoiOPsQA4r18oIk1+CGOYqpJIFeVqqnMGsLIQAPKwg0U8B+sCOnDcXhVex52A3WhV DrGR84le3eGfrbvm7gnu9AqU87RQVMKVk81YEbVri7vhdMq2zL795K4RjUZMMTCr2Z1gky 0nGvWDAFLk4SMk0Kf+HvkFq/88x/AQk= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-97-_ZICaESyNZOLTHro1IRTMg-1; Tue, 13 Dec 2022 13:04:11 -0500 X-MC-Unique: _ZICaESyNZOLTHro1IRTMg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A4DA29324AF; Tue, 13 Dec 2022 18:04:11 +0000 (UTC) Received: from random.internal.datastacks.com (unknown [10.2.17.61]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BED3492B02; Tue, 13 Dec 2022 18:04:10 +0000 (UTC) From: Peter Jones To: Evgeniy Baskov Cc: Ard Biesheuvel , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Mario Limonciello , joeyli , lvc-project@linuxtesting.org, x86@kernel.org, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] efi/x86: don't set unsupported memory attributes Date: Tue, 13 Dec 2022 13:04:02 -0500 Message-Id: <20221213180403.1308507-1-pjones@redhat.com> In-Reply-To: <20221213180317.qoy2l3mcpjparocq@redhat.com> References: <20221213180317.qoy2l3mcpjparocq@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org On platforms where the firmware uses DXE, but which do not implement the EFI Memory Attribute Protocol, we implement W^X support using DXE's set_memory_attributes() call. This call will fail without making any changes if an attribute is set that isn't supported on the platform. This patch changes efi_adjust_memory_range_protection() to avoid trying to set any attribute bits that aren't set in the memory region's capability flags. Signed-off-by: Peter Jones --- drivers/firmware/efi/libstub/mem.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/firmware/efi/libstub/mem.c b/drivers/firmware/efi/libstub/mem.c index d10996e4eb0..d52841adcc2 100644 --- a/drivers/firmware/efi/libstub/mem.c +++ b/drivers/firmware/efi/libstub/mem.c @@ -192,6 +192,7 @@ static efi_status_t adjust_mem_attrib_dxe(efi_physical_addr_t rounded_start, desc.attributes &= ~(EFI_MEMORY_RO | EFI_MEMORY_XP); desc.attributes |= attributes; + desc.attributes &= desc.capabilities; unprotect_start = max(rounded_start, desc.base_address); unprotect_size = min(rounded_end, next) - unprotect_start; From patchwork Tue Dec 13 18:04:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Jones X-Patchwork-Id: 13072306 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7C78C4167B for ; Tue, 13 Dec 2022 18:05:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236397AbiLMSFM (ORCPT ); Tue, 13 Dec 2022 13:05:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236366AbiLMSFK (ORCPT ); Tue, 13 Dec 2022 13:05:10 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4781A1EADF for ; Tue, 13 Dec 2022 10:04:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670954665; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9442XwFq1UOnuBhQolOjrgRBVO4GjmHmhrVClvO1uGo=; b=e5aaJZC9RIlbSy3YdNGVW+ykMr1yO7/aKnzq7zE0pT888tXzdKOr25TjscAm2Wft0/+N/p +ybYYed5RXT++dTeOV5qLs84Eb2yjIDawvhfnmDSn9KaC9lBQpxdx9tm/WOruwdwdltliY pdsQf+teis3DlLLZqwKj6EKbX7VQqJs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-326--myBm3lRMLecQIMerV2KPw-1; Tue, 13 Dec 2022 13:04:22 -0500 X-MC-Unique: -myBm3lRMLecQIMerV2KPw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1F3508032E6; Tue, 13 Dec 2022 18:04:21 +0000 (UTC) Received: from random.internal.datastacks.com (unknown [10.2.17.61]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82EE3492B00; Tue, 13 Dec 2022 18:04:20 +0000 (UTC) From: Peter Jones To: Evgeniy Baskov Cc: Ard Biesheuvel , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Mario Limonciello , joeyli , lvc-project@linuxtesting.org, x86@kernel.org, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/2] efi/x86: don't try to set page attributes on 0-sized regions. Date: Tue, 13 Dec 2022 13:04:03 -0500 Message-Id: <20221213180403.1308507-2-pjones@redhat.com> In-Reply-To: <20221213180403.1308507-1-pjones@redhat.com> References: <20221213180317.qoy2l3mcpjparocq@redhat.com> <20221213180403.1308507-1-pjones@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org In "efi/x86: Explicitly set sections memory attributes", the following region is defined to help compute page permissions: /* .setup [image_base, _head] */ efi_adjust_memory_range_protection(image_base, (unsigned long)_head - image_base, EFI_MEMORY_RO | EFI_MEMORY_XP); In at least some cases, that will result in a size of 0, which will produce an error and a message on the console, though no actual failure will be caused in the boot process. This patch checks that case in efi_adjust_memory_range_protection() and returns the error without logging. Signed-off-by: Peter Jones --- drivers/firmware/efi/libstub/mem.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/firmware/efi/libstub/mem.c b/drivers/firmware/efi/libstub/mem.c index d52841adcc2..d20b1986c9e 100644 --- a/drivers/firmware/efi/libstub/mem.c +++ b/drivers/firmware/efi/libstub/mem.c @@ -247,6 +247,9 @@ efi_status_t efi_adjust_memory_range_protection(unsigned long start, efi_physical_addr_t rounded_start, rounded_end; unsigned long attr_clear; + if (size == 0) + return EFI_INVALID_PARAMETER; + /* * This function should not be used to modify attributes * other than writable/executable.