From patchwork Tue Mar 8 15:23:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Doebel, Bjoern" X-Patchwork-Id: 12773959 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 D6E61C433F5 for ; Tue, 8 Mar 2022 15:23:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.287074.486873 (Exim 4.92) (envelope-from ) id 1nRbgg-0007ah-CT; Tue, 08 Mar 2022 15:23:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 287074.486873; Tue, 08 Mar 2022 15:23:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nRbgg-0007aa-9S; Tue, 08 Mar 2022 15:23:46 +0000 Received: by outflank-mailman (input) for mailman id 287074; Tue, 08 Mar 2022 15:23:45 +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 1nRbge-0007aT-Ng for xen-devel@lists.xenproject.org; Tue, 08 Mar 2022 15:23:45 +0000 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bca01a70-9ef3-11ec-8539-5f4723681683; Tue, 08 Mar 2022 16:23:43 +0100 (CET) Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2a-92ba9394.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 08 Mar 2022 15:23:31 +0000 Received: from EX13D44EUB004.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2a-92ba9394.us-west-2.amazon.com (Postfix) with ESMTPS id 5D173417EF; Tue, 8 Mar 2022 15:23:29 +0000 (UTC) Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13D44EUB004.ant.amazon.com (10.43.166.198) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Tue, 8 Mar 2022 15:23:27 +0000 Received: from dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (10.13.240.106) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1497.28 via Frontend Transport; Tue, 8 Mar 2022 15:23:26 +0000 Received: by dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (Postfix, from userid 3160037) id E06444EA9; Tue, 8 Mar 2022 15:23:25 +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: bca01a70-9ef3-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=1646753023; x=1678289023; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=DagEakpqz6Dt5Y9fwUgvFiApWcxqurKY1A3qTM9nhpY=; b=OpntINO+n8kEI8d9KA9+7Iuou6EB0Ib/MZgIRZxBj26x/C1mfWyFD9+G RcsO+GuTqBLynzioEl6v+rjmgdQh3aUMyEfn5VszLF6awdgX4/EHzF99K QVv6Kmk1mKPfprlmRo0lza6rBZdUyJtSWw47PxJ1eFxUeVdcqEgE47BqB 0=; X-IronPort-AV: E=Sophos;i="5.90,165,1643673600"; d="scan'208";a="179452231" From: Bjoern Doebel To: CC: Michael Kurth , Martin Pohlack , Roger Pau Monne , Andrew Cooper , Bjoern Doebel , Konrad Rzeszutek Wilk , Ross Lagerwall Subject: [PATCH v4 1/2] Livepatch: resolve old address before function verification Date: Tue, 8 Mar 2022 15:23:19 +0000 Message-ID: <7e547248b6c24b9f3761720f84919614af69c802.1646752948.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 Tue Mar 8 15:23:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Doebel, Bjoern" X-Patchwork-Id: 12773960 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 9C63DC433F5 for ; Tue, 8 Mar 2022 15:24:02 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.287075.486884 (Exim 4.92) (envelope-from ) id 1nRbgl-0007tA-Kh; Tue, 08 Mar 2022 15:23:51 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 287075.486884; Tue, 08 Mar 2022 15:23:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nRbgl-0007t3-Hc; Tue, 08 Mar 2022 15:23:51 +0000 Received: by outflank-mailman (input) for mailman id 287075; Tue, 08 Mar 2022 15:23:50 +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 1nRbgk-0007aT-0o for xen-devel@lists.xenproject.org; Tue, 08 Mar 2022 15:23:50 +0000 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bf920d1c-9ef3-11ec-8539-5f4723681683; Tue, 08 Mar 2022 16:23:48 +0100 (CET) Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2a-92ba9394.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP; 08 Mar 2022 15:23:32 +0000 Received: from EX13D03EUA001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2a-92ba9394.us-west-2.amazon.com (Postfix) with ESMTPS id 1F6B841AD3; Tue, 8 Mar 2022 15:23:29 +0000 (UTC) Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D03EUA001.ant.amazon.com (10.43.165.33) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Tue, 8 Mar 2022 15:23:28 +0000 Received: from dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (10.13.240.106) by mail-relay.amazon.com (10.43.160.118) with Microsoft SMTP Server id 15.0.1497.28 via Frontend Transport; Tue, 8 Mar 2022 15:23:27 +0000 Received: by dev-dsk-doebel-1c-c6d5f274.eu-west-1.amazon.com (Postfix, from userid 3160037) id 8763F54CF; Tue, 8 Mar 2022 15:23:26 +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: bf920d1c-9ef3-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=1646753029; x=1678289029; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=t2GRG3qC+EChIjAjEMa9qnXPMGK10DzsvMDsOutzQT8=; b=QB9VSZ5l2UDMGU0OX7K+5+uhypF2lUCVhUg9lZdIPZ0uFJ6avubJu9em 12yRvYlxRK95NW1+zlV9X6vaedZOr1ftkqkXLfUqBSlCeuBd42ce4e8kg 2/7KDqQVX9ENLryGo1qmsXbCbaiOLSumvR9acuFnE7EXtCtFoEzTugwtj E=; X-IronPort-AV: E=Sophos;i="5.90,165,1643673600"; d="scan'208";a="180564210" From: Bjoern Doebel To: CC: Michael Kurth , Martin Pohlack , Roger Pau Monne , Andrew Cooper , Bjoern Doebel , Konrad Rzeszutek Wilk , Ross Lagerwall Subject: [PATCH v4 2/2] xen/x86: Livepatch: support patching CET-enhanced functions Date: Tue, 8 Mar 2022 15:23:20 +0000 Message-ID: <7807aba5916fa4d983ec876ae560854093211cf0.1646752948.git.doebel@amazon.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <7e547248b6c24b9f3761720f84919614af69c802.1646752948.git.doebel@amazon.de> References: <7e547248b6c24b9f3761720f84919614af69c802.1646752948.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 ---- Note that on top of livepatching functions, Xen supports an additional mode where we can "remove" a function by overwriting it with NOPs. This is only supported for functions up to 31 bytes in size and this patch reduces this limit to 30 bytes. 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 --- xen/arch/x86/livepatch.c | 61 ++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c index 65530c1e57..5380e18bd9 100644 --- a/xen/arch/x86/livepatch.c +++ b/xen/arch/x86/livepatch.c @@ -14,11 +14,29 @@ #include #include +#include #include #include #include #include +/* + * 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). + * + * We do so by making use of the existing opaque metadata area. We use its + * first 4 bytes to track the offset into the function used for patching and + * the remainder of the data to store overwritten code bytes. + */ +struct x86_livepatch_meta { + uint8_t patch_offset; + uint8_t instruction[LIVEPATCH_OPAQUE_SIZE - sizeof(uint8_t)]; +}; + static bool has_active_waitqueue(const struct vm_event_domain *ved) { /* ved may be xzalloc()'d without INIT_LIST_HEAD() yet. */ @@ -104,18 +122,34 @@ void noinline arch_livepatch_revive(void) int arch_livepatch_verify_func(const struct livepatch_func *func) { + BUILD_BUG_ON(sizeof(struct x86_livepatch_meta) != LIVEPATCH_OPAQUE_SIZE); + /* If NOPing.. */ if ( !func->new_addr ) { /* Only do up to maximum amount we can put in the ->opaque. */ - if ( func->new_size > sizeof(func->opaque) ) + if ( func->new_size > sizeof_field(struct x86_livepatch_meta, + instruction) ) return -EOPNOTSUPP; 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 + * starts 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; } @@ -127,15 +161,21 @@ int arch_livepatch_verify_func(const struct livepatch_func *func) void noinline arch_livepatch_apply(struct livepatch_func *func) { uint8_t *old_ptr; - uint8_t insn[sizeof(func->opaque)]; + struct x86_livepatch_meta *lp; + uint8_t insn[sizeof(lp->instruction)]; unsigned int len; + lp = (struct x86_livepatch_meta *)func->opaque; + lp->patch_offset = 0; old_ptr = func->old_addr; len = livepatch_insn_len(func); if ( !len ) return; - memcpy(func->opaque, old_ptr, len); + if ( is_endbr64(old_ptr) ) + lp->patch_offset += ENDBR64_LEN; + + memcpy(lp->instruction, old_ptr + lp->patch_offset, len); if ( func->new_addr ) { int32_t val; @@ -143,14 +183,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 + lp->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 + lp->patch_offset, insn, len); } /* @@ -159,7 +200,11 @@ 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)); + const struct x86_livepatch_meta *lp; + + lp = (struct x86_livepatch_meta *)func->opaque; + + memcpy(func->old_addr + lp->patch_offset, lp->instruction, livepatch_insn_len(func)); } /*