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: 32908 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp279175wrn; Tue, 13 Dec 2022 10:07:35 -0800 (PST) X-Google-Smtp-Source: AA0mqf5ksV2vsPHxAKZvt4TeUr2r0RAYUaHSn7oPE/yhjOvxT7gFd1pN9TzbTal9hObIURmsuxrV X-Received: by 2002:a05:6a20:9d91:b0:a3:dc4e:74f9 with SMTP id mu17-20020a056a209d9100b000a3dc4e74f9mr24359286pzb.19.1670954854904; Tue, 13 Dec 2022 10:07:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670954854; cv=none; d=google.com; s=arc-20160816; b=TuFWHtRjpGa6h5E3wcj6mliZYS2VF7NRTQjx9d+myEyN62xThCsv6ZZxIusz252B8a FE6e7FOU0oZkHD1rD7iEpJa36l42MlADj0i7CXdJuoJ34j3cCimK0gmch+bcApzqtcYI xLmDy3kBq3T9bUE7P5boIq/sb7IGlojuTW+7+kK7cvGk3SPvHIp+nsWlRTF48S0PyVQK h3LCXYK8CzSXDP82OhVWXUhl50pX0g9LDRh+3wDmSv+dFE9ETP73Pps22WrGFs562RJ7 7AjfYFfnXfGhY4WOSKgMzxDO3uD/x7n2MnktA3nPKENp0fXuTCWyS2y1dlcRs25fWyfb DUpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JF2s0tm6WDQRqQHFNtrMHbtnveQJl7hXDtEAPn+pSMA=; b=oSwpuUehPJL0cPMbBhHXs0V/vIqh44iXSykWJzBYaEmzgZ60f6+D4Gz5Q3Qf0ytG12 PpOPzMVi5rgsBAWV69FZXAwomdSAsYxAPqMWPHMxfRxfuitBe8h5nVM2+shlNdAQAsSi +sVB0XHV0nMuoPsPhyhqh4vGe1wUssSU5KH4PRPWT66wLYq44/VZC6n4l9+rWMOgtkeh B+4OBwDM9H0rpPmCLe8lm/SBUYlgbKZ4w4GESDdhEIj5Tl7FuxTEEw5glmOsPGnAkFZn fotJV2f5LSv0zw1a8wi3g51okSEVJbS30wK276FBvYggT52xereCAEAINgw+8UerU8kt hH4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GJWAjvI5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id iw7-20020a170903044700b001871c762263si369591plb.185.2022.12.13.10.07.21; Tue, 13 Dec 2022 10:07:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GJWAjvI5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236367AbiLMSFF (ORCPT + 99 others); Tue, 13 Dec 2022 13:05:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235762AbiLMSFC (ORCPT ); Tue, 13 Dec 2022 13:05:02 -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 315D123EAC for ; Tue, 13 Dec 2022 10:04:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670954656; 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=GJWAjvI5ijnc2mAjYcsn0CB0Yz/iCxGfg0TJEzDQZKXrIiTtVUWO3VcpDM0Od9RX9K9XSM OEKTGvQ/uWbhb2DZGr2V10sxE+1XOjja3XNTTgPjjkqAKhKHoR2xQxQUZziEbeJJb1ERCd VNnJOQs7hUAlTdMNGR9rt2T2hySvfOU= 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752123157458476902?= X-GMAIL-MSGID: =?utf-8?q?1752123157458476902?= 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: 32909 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp279439wrn; Tue, 13 Dec 2022 10:07:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf7JxfnSJ8AAnA9BsId0M42CK4TTaU1K7FVmCc31maNlnEOwHwW9Ydjp5RUguxw8JBpMZ828 X-Received: by 2002:a05:6a20:9f87:b0:9d:efbe:e5f4 with SMTP id mm7-20020a056a209f8700b0009defbee5f4mr22712872pzb.16.1670954878577; Tue, 13 Dec 2022 10:07:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670954878; cv=none; d=google.com; s=arc-20160816; b=U5p9lf0BjRn9kPgOahWf33GIRsOofwEV1JBc/JEKa6O8zeaoqviihCZU3hWMpvEMMu /pZDbWAEmA0Vx5GpQUPM43nczdcyJ7GcYmY0HtDi9KZULYXEuRTKhiJaorr1B1bgJloy DaBZ643R5lh0D5zj3RimLEjj3lEVxNb9Y3qtJrzp1Xn2puMAMk+zaONgldopRQ03evnq MDOpntLYhdU3sJrKTRfubiJLv0s6ySZysvanJI827ItHCfUIzftkRNg3/UNpzFE/MAGF SJ0d/yImppRxzzSHmaIoS4JG4Xj5VXx1T+8+9pvX2oCT5UM60vPiOeb+TRW3bBcgJ3Iy s9CQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9442XwFq1UOnuBhQolOjrgRBVO4GjmHmhrVClvO1uGo=; b=CK4hviqgz9PcBqeOesXgZd4nHmZgbw2MnPHKxtFihuCsD3vgFTXUsc6KU1fH7HmN8x vuGhN5dLg0kzvT4tl1VZE6P+yJMpJSJ2/H1t1j9bZadcjJiw7cvRhohcnBiy8oCox92d wP4sOYwR23MP+ikNsV+S5bmpOy/aEuHnznKiMqVe/efmtsw0SNA2TpPfw4rGI1azwu52 w7mGXQLWhVpgpYzTfm/m7a2bHoqvFPrZpCe8znPQu5TQSpRoYHuaPURNMSp4t35iWKt9 VoFQwTtSnGgZQv8Dh0OobZDWceE7G2G/IeDhbuyFSCyt95isrP/d0rxsbVMdzCyMqVLn lPXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EFlB+4Y8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l25-20020a635719000000b004790794f1fasi12630646pgb.428.2022.12.13.10.07.45; Tue, 13 Dec 2022 10:07:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EFlB+4Y8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236405AbiLMSFS (ORCPT + 99 others); Tue, 13 Dec 2022 13:05:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235762AbiLMSFO (ORCPT ); Tue, 13 Dec 2022 13:05:14 -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 0446E23EAF for ; Tue, 13 Dec 2022 10:04:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670954669; 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=EFlB+4Y8/kZgUtX/dL0FgJSMmoiJ02c2CzPlOe/pZz++TaFqFZrASG/W7rp7obYcXpxTQV /cIryL0rHMGhP/yrjyMyRf6NTkshishsdym1WhwErC13gnaVNiE5+EQZkGenZG20jHGARA lmmJCcwtu58MCnLSbdyxmZD3bqXs4Dc= 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752123183039796612?= X-GMAIL-MSGID: =?utf-8?q?1752123183039796612?= 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.