From patchwork Wed Mar 9 14:52:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Doebel, Bjoern" X-Patchwork-Id: 12775187 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2D6B3C433F5 for ; Wed, 9 Mar 2022 14:53:34 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.287900.488191 (Exim 4.92) (envelope-from ) id 1nRxgk-0000cp-6v; Wed, 09 Mar 2022 14:53:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 287900.488191; Wed, 09 Mar 2022 14:53:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nRxgk-0000ci-3r; Wed, 09 Mar 2022 14:53:18 +0000 Received: by outflank-mailman (input) for mailman id 287900; Wed, 09 Mar 2022 14:53:17 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nRxgh-0000cL-Qc for xen-devel@lists.xenproject.org; Wed, 09 Mar 2022 14:53:17 +0000 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a476d377-9fb8-11ec-8eba-a37418f5ba1a; Wed, 09 Mar 2022 15:53:13 +0100 (CET) Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2a-2dbf0206.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 09 Mar 2022 14:53:12 +0000 Received: from EX13D44EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2a-2dbf0206.us-west-2.amazon.com (Postfix) with ESMTPS id C75DDA277E; Wed, 9 Mar 2022 14:53:10 +0000 (UTC) Received: from EX13MTAUEA001.ant.amazon.com (10.43.61.82) by EX13D44EUC003.ant.amazon.com (10.43.164.229) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Wed, 9 Mar 2022 14:53:08 +0000 Received: from dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (10.13.240.106) by mail-relay.amazon.com (10.43.61.243) with Microsoft SMTP Server id 15.0.1497.28 via Frontend Transport; Wed, 9 Mar 2022 14:53:06 +0000 Received: by dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (Postfix, from userid 3160037) id 65C2A4E79; Wed, 9 Mar 2022 14:53:06 +0000 (UTC) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Inumbo-ID: a476d377-9fb8-11ec-8eba-a37418f5ba1a DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1646837594; x=1678373594; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=DagEakpqz6Dt5Y9fwUgvFiApWcxqurKY1A3qTM9nhpY=; b=RrZFIFpWIHtjTyQq8sHNjwqMPRppUEDdUfmU0pfzrW4kksr+EXlEI6pO 6Upyu+qs8xc3V2LlUTbLYYjq7lYsbYiMA1B8CX1/35FymxKdh+xmdhJ/9 k2JDB6W9L8vzs+TIHPIYdWvZmZM5TnmDA0fiVzGqaNiUKadwUu4mLw7jl A=; X-IronPort-AV: E=Sophos;i="5.90,167,1643673600"; d="scan'208";a="179765667" From: Bjoern Doebel To: CC: Michael Kurth , Martin Pohlack , Roger Pau Monne , Andrew Cooper , Bjoern Doebel , Konrad Rzeszutek Wilk , Ross Lagerwall Subject: [PATCH v5 1/2] Livepatch: resolve old address before function verification Date: Wed, 9 Mar 2022 14:52:59 +0000 Message-ID: <03798b67ec3a892a302e6be5d87ea676e6376036.1646837462.git.doebel@amazon.de> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Precedence: Bulk When verifying that a livepatch can be applied, we may as well want to inspect the target function to be patched. To do so, we need to resolve this function's address before running the arch-specific livepatch_verify hook. Signed-off-by: Bjoern Doebel Acked-by: Konrad Rzeszutek Wilk Reviewed-by: Ross Lagerwall --- xen/common/livepatch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index ec301a9f12..be2cf75c2d 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -684,11 +684,11 @@ static int prepare_payload(struct payload *payload, return -EINVAL; } - rc = arch_livepatch_verify_func(f); + rc = resolve_old_address(f, elf); if ( rc ) return rc; - rc = resolve_old_address(f, elf); + rc = arch_livepatch_verify_func(f); if ( rc ) return rc; From patchwork Wed Mar 9 14:53:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Doebel, Bjoern" X-Patchwork-Id: 12775188 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8C383C433F5 for ; Wed, 9 Mar 2022 14:53:45 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.287901.488202 (Exim 4.92) (envelope-from ) id 1nRxh1-00011B-EU; Wed, 09 Mar 2022 14:53:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 287901.488202; Wed, 09 Mar 2022 14:53:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nRxh1-000112-BQ; Wed, 09 Mar 2022 14:53:35 +0000 Received: by outflank-mailman (input) for mailman id 287901; Wed, 09 Mar 2022 14:53:33 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nRxgz-0000yq-Im for xen-devel@lists.xenproject.org; Wed, 09 Mar 2022 14:53:33 +0000 Received: from smtp-fw-9103.amazon.com (smtp-fw-9103.amazon.com [207.171.188.200]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id aee52d75-9fb8-11ec-8539-5f4723681683; Wed, 09 Mar 2022 15:53:32 +0100 (CET) Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1d-7a21ed79.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP; 09 Mar 2022 14:53:13 +0000 Received: from EX13D03EUC004.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-iad-1d-7a21ed79.us-east-1.amazon.com (Postfix) with ESMTPS id E25B22217FB; Wed, 9 Mar 2022 14:53:11 +0000 (UTC) Received: from EX13MTAUEE002.ant.amazon.com (10.43.62.24) by EX13D03EUC004.ant.amazon.com (10.43.164.33) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Wed, 9 Mar 2022 14:53:09 +0000 Received: from dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (10.13.240.106) by mail-relay.amazon.com (10.43.62.224) with Microsoft SMTP Server id 15.0.1497.28 via Frontend Transport; Wed, 9 Mar 2022 14:53:07 +0000 Received: by dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (Postfix, from userid 3160037) id 994AD4E7F; Wed, 9 Mar 2022 14:53:07 +0000 (UTC) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Inumbo-ID: aee52d75-9fb8-11ec-8539-5f4723681683 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1646837612; x=1678373612; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3/k2kvEImrHIXI5QhPO8pqI+rGf36IXCF/vzbmeE/gY=; b=ugrAiGwWr1cPdwtKhmf462ghco+M78ijyrsRGweKuv2GAnb8r3hSQI+E GVYFco34crgvhgErFsVccxKByqArNcG6VvS7ko49c+gP9FPI/LnAs/C5I MQVQGHxyqBT7HykAe0oepVK68V98iD7z/SiKYNmp+2/Ese/niItCdFUoK A=; X-IronPort-AV: E=Sophos;i="5.90,167,1643673600"; d="scan'208";a="997927067" From: Bjoern Doebel To: CC: Michael Kurth , Martin Pohlack , Roger Pau Monne , Andrew Cooper , Bjoern Doebel , Konrad Rzeszutek Wilk , Ross Lagerwall Subject: [PATCH v5 2/2] xen/x86: Livepatch: support patching CET-enhanced functions Date: Wed, 9 Mar 2022 14:53:00 +0000 Message-ID: <5cdd27addcec926eb48fdeab08ad3371e7c3fd7c.1646837462.git.doebel@amazon.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <03798b67ec3a892a302e6be5d87ea676e6376036.1646837462.git.doebel@amazon.de> References: <03798b67ec3a892a302e6be5d87ea676e6376036.1646837462.git.doebel@amazon.de> MIME-Version: 1.0 Precedence: Bulk Xen enabled CET for supporting architectures. The control flow aspect of CET expects functions that can be called indirectly (i.e., via function pointers) to start with an ENDBR64 instruction. Otherwise a control flow exception is raised. This expectation breaks livepatching flows because we patch functions by overwriting their first 5 bytes with a JMP + , thus breaking the ENDBR64. We fix this by checking the start of a patched function for being ENDBR64. In the positive case we move the livepatch JMP to start behind the ENDBR64 instruction. To avoid having to guess the ENDBR64 offset again on patch reversal (which might race with other mechanisms adding/removing ENDBR dynamically), use the livepatch metadata to store the computed offset along with the saved bytes of the overwritten function. Signed-off-by: Bjoern Doebel Acked-by: Konrad Rzeszutek Wilk CC: Ross Lagerwall ---- Changes since r1: * use sizeof_field() to avoid unused variable warning * make metadata variable const in arch_livepatch_revert * rebase on top and make use of Andrew Cooper's was_endbr64() patch * use padding byte to store offset rather than reducing opaque area Reviewed-by: Ross Lagerwall Tested-by: Jiamei xie Tested-by: Jiamei xie --- xen/arch/x86/livepatch.c | 39 +++++++++++++++++++++++++++++++------ xen/include/public/sysctl.h | 3 ++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c index 37c9b8435e..e71741743a 100644 --- a/xen/arch/x86/livepatch.c +++ b/xen/arch/x86/livepatch.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -114,8 +115,21 @@ int arch_livepatch_verify_func(const struct livepatch_func *func) if ( func->old_size < func->new_size ) return -EINVAL; } - else if ( func->old_size < ARCH_PATCH_INSN_SIZE ) - return -EINVAL; + else + { + /* + * Space needed now depends on whether the target function + * start{s,ed} with an ENDBR64 instruction. + */ + uint8_t needed; + + needed = ARCH_PATCH_INSN_SIZE; + if ( is_endbr64(func->old_addr) || was_endbr64(func->old_addr) ) + needed += ENDBR64_LEN; + + if ( func->old_size < needed ) + return -EINVAL; + } return 0; } @@ -130,12 +144,24 @@ void noinline arch_livepatch_apply(struct livepatch_func *func) uint8_t insn[sizeof(func->opaque)]; unsigned int len; + func->patch_offset = 0; old_ptr = func->old_addr; len = livepatch_insn_len(func); if ( !len ) return; - memcpy(func->opaque, old_ptr, len); + /* + * CET hotpatching support: We may have functions starting with an ENDBR64 + * instruction that MUST remain the first instruction of the function, hence + * we need to move any hotpatch trampoline further into the function. For that + * we need to keep track of the patching offset used for any loaded hotpatch + * (to avoid racing against other fixups adding/removing ENDBR64 or similar + * instructions). + */ + if ( is_endbr64(old_ptr) || was_endbr64(func->old_addr) ) + func->patch_offset += ENDBR64_LEN; + + memcpy(func->opaque, old_ptr + func->patch_offset, len); if ( func->new_addr ) { int32_t val; @@ -143,14 +169,15 @@ void noinline arch_livepatch_apply(struct livepatch_func *func) BUILD_BUG_ON(ARCH_PATCH_INSN_SIZE != (1 + sizeof(val))); insn[0] = 0xe9; /* Relative jump. */ - val = func->new_addr - func->old_addr - ARCH_PATCH_INSN_SIZE; + val = func->new_addr - (func->old_addr + func->patch_offset + + ARCH_PATCH_INSN_SIZE); memcpy(&insn[1], &val, sizeof(val)); } else add_nops(insn, len); - memcpy(old_ptr, insn, len); + memcpy(old_ptr + func->patch_offset, insn, len); } /* @@ -159,7 +186,7 @@ void noinline arch_livepatch_apply(struct livepatch_func *func) */ void noinline arch_livepatch_revert(const struct livepatch_func *func) { - memcpy(func->old_addr, func->opaque, livepatch_insn_len(func)); + memcpy(func->old_addr + func->patch_offset, func->opaque, livepatch_insn_len(func)); } /* diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index 55252e97f2..b0a4af8789 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -876,7 +876,8 @@ struct livepatch_func { uint8_t version; /* MUST be LIVEPATCH_PAYLOAD_VERSION. */ uint8_t opaque[LIVEPATCH_OPAQUE_SIZE]; uint8_t applied; - uint8_t _pad[7]; + uint8_t patch_offset; + uint8_t _pad[6]; livepatch_expectation_t expect; }; typedef struct livepatch_func livepatch_func_t;