From patchwork Thu Dec 14 10:17:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13492701 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 C1C28C4332F for ; Thu, 14 Dec 2023 10:17:56 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.654401.1021290 (Exim 4.92) (envelope-from ) id 1rDimo-0006So-53; Thu, 14 Dec 2023 10:17:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 654401.1021290; Thu, 14 Dec 2023 10:17: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 1rDimo-0006Sh-1z; Thu, 14 Dec 2023 10:17:46 +0000 Received: by outflank-mailman (input) for mailman id 654401; Thu, 14 Dec 2023 10:17:44 +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 1rDimm-0006DQ-81 for xen-devel@lists.xenproject.org; Thu, 14 Dec 2023 10:17:44 +0000 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [2a00:1450:4864:20::230]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 0456ce13-9a6a-11ee-9b0f-b553b5be7939; Thu, 14 Dec 2023 11:17:42 +0100 (CET) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2c9f72176cfso100225831fa.2 for ; Thu, 14 Dec 2023 02:17:42 -0800 (PST) Received: from localhost ([213.195.127.70]) by smtp.gmail.com with ESMTPSA id c12-20020a5d4f0c000000b00336442b3e80sm2272336wru.78.2023.12.14.02.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 02:17:41 -0800 (PST) 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 Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0456ce13-9a6a-11ee-9b0f-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1702549061; x=1703153861; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=//Q5HEM9v4eB3FhFejuA6Not40HTXEnc+em45fT0PEw=; b=CWLTEgAMiiQeDr0q8hQp6SCUU4SVcM2OiD4aS/lHmwyFZU8kAUuoXyIJ1EaigVYliQ QirpEurE96+QiAq7tdiSev08+vYouFh7Dsf8wGOOqUXpnIbXjUNbjbiCYZMcvfk8uw+f 1mi65djX9Q06AnkVIKgOrtsSpU7Vd6rkOZnuA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702549061; x=1703153861; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=//Q5HEM9v4eB3FhFejuA6Not40HTXEnc+em45fT0PEw=; b=ECV5QX5Swv4p1RZLya706V8boS5cRAEd2P7DikeNbWNyDjlK08+ay8G4ucXK/2cqLq 4eDDFoExL5+JLAoJbCD1d+OrX79+vOPwHl1KjvUvzUx1w9oediMhAV/Ew/XXqDqWalVi 6xKbk5gE9x+4u3x03Ec5BVYhZ6LM0ZEN2WTM9HqBb6SdyN20+AYB5DvDjaqFcjph3nr+ 5yvVnMSBHhEbLEXIL7mZiKUyasnHqrdIJfUj7ZZuQELNoHsxqAZCiP8LreTj8m75PrMx 4631wDO3RcKBvUEm7Te+HImxqufSq5XPMtcL5RTyerEHQSbqVjwTTsK9xU1PP6NrCEZA 1MDw== X-Gm-Message-State: AOJu0YyeFnO2uHba1Wn709ORkP4hcbqFSRONzObLXtK6yrS1w6bHmRnU Z5srXrJlho03GXbB8CmWJZHr2qypM40gqivl6P0= X-Google-Smtp-Source: AGHT+IHzTZTldeZBawBMC1DH1MAbS4NWQNJ/tz+riZfQmQhRnhpkb9+u9RhBmRNN8vQ1M4Q+sc5Bcw== X-Received: by 2002:a05:651c:154b:b0:2cc:27f4:67dc with SMTP id y11-20020a05651c154b00b002cc27f467dcmr1597105ljp.48.1702549061345; Thu, 14 Dec 2023 02:17:41 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , Ross Lagerwall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk Subject: [PATCH v3 1/4] x86/livepatch: align functions to ensure minimal distance between entry points Date: Thu, 14 Dec 2023 11:17:16 +0100 Message-ID: <20231214101719.18770-2-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231214101719.18770-1-roger.pau@citrix.com> References: <20231214101719.18770-1-roger.pau@citrix.com> MIME-Version: 1.0 The minimal function size requirements for livepatch are either 5 bytes (for jmp) or 9 bytes (for endbr + jmp) on x86, and always 4 bytes on Arm. Ensure that distance between functions entry points is always at least of the minimal required size for livepatch instruction replacement to be successful. Add an additional align directive to the linker script, in order to ensure that the next section placed after the .text.* (per-function sections) is also aligned to the required boundary, so that the distance of the last function entry point with the next symbol is also of minimal size. Note that it's possible for the compiler to end up using a higher function alignment regardless of the passed value, so this change just make sure that the minimum required for livepatch to work is present. The compiler option -falign-functions is not available on at least clang 3.8, so introduce a Kconfig check for it and make the livepatch option depend on the compiler supporting the option. The naming of the option(s) CONFIG_FUNCTION_ALIGNMENT is explicitly not mentioning CC in preparation for the option also being used by assembly code. Signed-off-by: Roger Pau Monné --- Changes since v2: - Add Arm side. - Align end of section in the linker script to ensure enough padding for the last function. - Expand commit message and subject. - Rework Kconfig options. - Check that the compiler supports the option. Changes since v1: - New in this version. --- xen/Kconfig | 18 ++++++++++++++++++ xen/Makefile | 3 +++ xen/arch/arm/livepatch.c | 2 ++ xen/arch/arm/xen.lds.S | 4 ++++ xen/arch/x86/livepatch.c | 4 ++++ xen/arch/x86/xen.lds.S | 4 ++++ xen/common/Kconfig | 5 ++++- 7 files changed, 39 insertions(+), 1 deletion(-) diff --git a/xen/Kconfig b/xen/Kconfig index 134e6e68ad84..7a61e130771c 100644 --- a/xen/Kconfig +++ b/xen/Kconfig @@ -37,6 +37,24 @@ config CC_HAS_VISIBILITY_ATTRIBUTE config CC_SPLIT_SECTIONS bool +# Set function alignment. +# +# Allow setting on a boolean basis, and then convert such selection to an +# integer for the build system and code to consume more easily. +config CC_HAS_FUNCTION_ALIGNMENT + def_bool $(cc-option,-falign-functions=8) +config FUNCTION_ALIGNMENT_4B + bool +config FUNCTION_ALIGNMENT_8B + bool +config FUNCTION_ALIGNMENT_16B + bool +config FUNCTION_ALIGNMENT + int + default 16 if FUNCTION_ALIGNMENT_16B + default 8 if FUNCTION_ALIGNMENT_8B + default 4 if FUNCTION_ALIGNMENT_4B + source "arch/$(SRCARCH)/Kconfig" config DEFCONFIG_LIST diff --git a/xen/Makefile b/xen/Makefile index 21832d640225..162cb2bda1c5 100644 --- a/xen/Makefile +++ b/xen/Makefile @@ -390,6 +390,9 @@ CFLAGS += -fomit-frame-pointer endif CFLAGS-$(CONFIG_CC_SPLIT_SECTIONS) += -ffunction-sections -fdata-sections +ifdef CONFIG_FUNCTION_ALIGNMENT +CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT) +endif CFLAGS += -nostdinc -fno-builtin -fno-common CFLAGS += -Werror -Wredundant-decls -Wwrite-strings -Wno-pointer-arith diff --git a/xen/arch/arm/livepatch.c b/xen/arch/arm/livepatch.c index bbca1e5a5ed3..aa8ae8c38d28 100644 --- a/xen/arch/arm/livepatch.c +++ b/xen/arch/arm/livepatch.c @@ -68,6 +68,8 @@ void arch_livepatch_revive(void) int arch_livepatch_verify_func(const struct livepatch_func *func) { + BUILD_BUG_ON(ARCH_PATCH_INSN_SIZE > CONFIG_FUNCTION_ALIGNMENT); + /* If NOPing only do up to maximum amount we can put in the ->opaque. */ if ( !func->new_addr && (func->new_size > LIVEPATCH_OPAQUE_SIZE || func->new_size % ARCH_PATCH_INSN_SIZE) ) diff --git a/xen/arch/arm/xen.lds.S b/xen/arch/arm/xen.lds.S index 59b80d122fd0..afaf1e996b0e 100644 --- a/xen/arch/arm/xen.lds.S +++ b/xen/arch/arm/xen.lds.S @@ -44,6 +44,10 @@ SECTIONS #ifdef CONFIG_CC_SPLIT_SECTIONS *(.text.*) #endif +#ifdef CONFIG_FUNCTION_ALIGNMENT + /* Ensure enough distance with the next placed section. */ + . = ALIGN(CONFIG_FUNCTION_ALIGNMENT); +#endif *(.fixup) *(.gnu.warning) diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c index ee539f001b73..b00ad7120da9 100644 --- a/xen/arch/x86/livepatch.c +++ b/xen/arch/x86/livepatch.c @@ -109,6 +109,10 @@ int arch_livepatch_verify_func(const struct livepatch_func *func) */ uint8_t needed = ARCH_PATCH_INSN_SIZE; + BUILD_BUG_ON(ARCH_PATCH_INSN_SIZE + + (IS_ENABLED(CONIFG_XEN_IBT) ? ENDBR64_LEN : 0) > + CONFIG_FUNCTION_ALIGNMENT); + if ( is_endbr64(func->old_addr) || is_endbr64_poison(func->old_addr) ) needed += ENDBR64_LEN; diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S index 8930e14fc40e..5b3332300d44 100644 --- a/xen/arch/x86/xen.lds.S +++ b/xen/arch/x86/xen.lds.S @@ -99,6 +99,10 @@ SECTIONS *(.text) #ifdef CONFIG_CC_SPLIT_SECTIONS *(.text.*) +#endif +#ifdef CONFIG_FUNCTION_ALIGNMENT + /* Ensure enough distance with the next placed section. */ + . = ALIGN(CONFIG_FUNCTION_ALIGNMENT); #endif *(.text.__x86_indirect_thunk_*) diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 310ad4229cdf..c9a21c3c8a07 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -395,8 +395,11 @@ config CRYPTO config LIVEPATCH bool "Live patching support" default X86 - depends on "$(XEN_HAS_BUILD_ID)" = "y" + depends on "$(XEN_HAS_BUILD_ID)" = "y" && CC_HAS_FUNCTION_ALIGNMENT select CC_SPLIT_SECTIONS + select FUNCTION_ALIGNMENT_16B if XEN_IBT + select FUNCTION_ALIGNMENT_8B if X86 + select FUNCTION_ALIGNMENT_4B ---help--- Allows a running Xen hypervisor to be dynamically patched using binary patches without rebooting. This is primarily used to binarily From patchwork Thu Dec 14 10:17:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13492705 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 708A4C41535 for ; Thu, 14 Dec 2023 10:18:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.654402.1021295 (Exim 4.92) (envelope-from ) id 1rDimo-0006VY-Dv; Thu, 14 Dec 2023 10:17:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 654402.1021295; Thu, 14 Dec 2023 10:17: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 1rDimo-0006U9-9I; Thu, 14 Dec 2023 10:17:46 +0000 Received: by outflank-mailman (input) for mailman id 654402; Thu, 14 Dec 2023 10:17:44 +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 1rDimm-0006Da-NN for xen-devel@lists.xenproject.org; Thu, 14 Dec 2023 10:17:44 +0000 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [2a00:1450:4864:20::335]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 04d37c37-9a6a-11ee-98e9-6d05b1d4d9a1; Thu, 14 Dec 2023 11:17:43 +0100 (CET) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40c38de1ee4so56228455e9.0 for ; Thu, 14 Dec 2023 02:17:43 -0800 (PST) Received: from localhost ([213.195.127.70]) by smtp.gmail.com with ESMTPSA id fm21-20020a05600c0c1500b0040c03c3289bsm24222464wmb.37.2023.12.14.02.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 02:17:42 -0800 (PST) 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 Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 04d37c37-9a6a-11ee-98e9-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1702549063; x=1703153863; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ViZ6HshgXIzZU2jJO110IUa3m0YwRdNIuCZWwBHHHR4=; b=RcGKljl3F+z6hAzSM7NwNsCjsTOLG8B1tfVtPpQVwIc7yqSWZcJzzbFvaPSMQUPqlj v1FkD2yVXBOeSGcCVsI9qGRQV9RbkSAb3KbJsOoRibabTLzkTz32QDPGZMRmmwVQek+b NHzLuZVN20ym/titjTUIkfR8nyatsANX/oqR0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702549063; x=1703153863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ViZ6HshgXIzZU2jJO110IUa3m0YwRdNIuCZWwBHHHR4=; b=eP0YOPzmBeuxP4djDnLltivGkiBp4e3DoDFvlSc1qjjLqFdQp7FnrKyngjJXPdUHJi WmxbGiVVbdhfQVJp2tdLjpeQHmGs4PrBdxIWOyhjA6o0gv+/O3l+EzLMFkrH8Utv5Eev DsYxXq83GTZXSlmqAGxf8WDNSPFTfdBNoGAwc9BfK4GS0YxwHutypEwQ8FozfNkSJg50 Fa6OnjqdnqvZ0TQhRiCihV4SX6l65pybr6VZnQlAP/7DeOMBc4jdAZnsP8eGzWgaqWqq iYIz2RvKr9nIGYG9NVATflKmTjIH286MbklhdQaYLoRkV/4/LrkA11O/zn8CLOgtrzeC KIww== X-Gm-Message-State: AOJu0Yxbd7JKL3PI4lgLRum7FAB+iy9mjvBKIHnGfBOIj/gl6qRIjN+6 EfwTjT9BftmkbVa4s04C5+5ruIzXEiXq0qerbUM= X-Google-Smtp-Source: AGHT+IFrof9xmOuW95ijQWLTufyd6KrOVDaHydrGvFqct3533PxijnOPr2RuZI5T/3fRx6J6Lv8FuQ== X-Received: by 2002:a7b:cd89:0:b0:40b:5e59:e9e8 with SMTP id y9-20020a7bcd89000000b0040b5e59e9e8mr4510564wmj.135.1702549062650; Thu, 14 Dec 2023 02:17:42 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Wei Liu , Anthony PERARD , Juergen Gross , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini Subject: [PATCH v3 2/4] xen/x86: introduce self modifying code test Date: Thu, 14 Dec 2023 11:17:17 +0100 Message-ID: <20231214101719.18770-3-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231214101719.18770-1-roger.pau@citrix.com> References: <20231214101719.18770-1-roger.pau@citrix.com> MIME-Version: 1.0 Introduce a helper to perform checks related to self modifying code, and start by creating a simple test to check that alternatives have been applied. Such test is hooked into the boot process and called just after alternatives have been applied. In case of failure a message is printed, and the hypervisor is tainted as not having passed the tests, this does require introducing a new taint bit (printed as 'A'). A new sysctl is also introduced to run the tests on demand. While there are no current users introduced here, further changes will introduce those, and it's helpful to have the interface defined in the sysctl header from the start. Note the sysctl visibility is not limited to x86, albeit the only implementation is for x86. It's expected that other architectures can reuse the same sysctl and structure, with possibly different tests. Leave adjusting those to when support for a different architecture is introduced, as the sysctl interface is not stable anyway. Signed-off-by: Roger Pau Monné --- Changes since v2: - Rename to smoc and place in test/smoc* - fix inline assembly. Changes since v1: - Rework test and interface. --- tools/include/xenctrl.h | 2 + tools/libs/ctrl/xc_misc.c | 14 ++++++ xen/arch/x86/Makefile | 1 + xen/arch/x86/include/asm/test-smoc.h | 18 ++++++++ xen/arch/x86/setup.c | 3 ++ xen/arch/x86/sysctl.c | 9 ++++ xen/arch/x86/test/Makefile | 1 + xen/arch/x86/test/smoc.c | 68 ++++++++++++++++++++++++++++ xen/common/kernel.c | 5 +- xen/include/public/sysctl.h | 10 ++++ xen/include/xen/lib.h | 1 + 11 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 xen/arch/x86/include/asm/test-smoc.h create mode 100644 xen/arch/x86/test/Makefile create mode 100644 xen/arch/x86/test/smoc.c diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index 2ef8b4e05422..0af796ae84e8 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -2658,6 +2658,8 @@ int xc_dt_overlay(xc_interface *xch, void *overlay_fdt, uint32_t overlay_fdt_size, uint8_t overlay_op); #endif +int xc_test_smoc(xc_interface *xch, uint32_t tests, uint32_t *result); + /* Compat shims */ #include "xenctrl_compat.h" diff --git a/tools/libs/ctrl/xc_misc.c b/tools/libs/ctrl/xc_misc.c index 5ecdfa2c7934..1d3d5929cf96 100644 --- a/tools/libs/ctrl/xc_misc.c +++ b/tools/libs/ctrl/xc_misc.c @@ -1021,6 +1021,20 @@ int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32 return _xc_livepatch_action(xch, name, LIVEPATCH_ACTION_REPLACE, timeout, flags); } +int xc_test_smoc(xc_interface *xch, uint32_t tests, uint32_t *result) +{ + struct xen_sysctl sysctl = { + .cmd = XEN_SYSCTL_test_smoc, + .u.test_smoc.tests = tests, + }; + int rc = do_sysctl(xch, &sysctl); + + if ( !rc ) + *result = sysctl.u.test_smoc.results; + + return rc; +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index f3abdf9cd111..ad5112b03c64 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_HVM) += hvm/ obj-y += mm/ obj-$(CONFIG_XENOPROF) += oprofile/ obj-$(CONFIG_PV) += pv/ +obj-y += test/ obj-y += x86_64/ obj-y += x86_emulate/ diff --git a/xen/arch/x86/include/asm/test-smoc.h b/xen/arch/x86/include/asm/test-smoc.h new file mode 100644 index 000000000000..2547b925d291 --- /dev/null +++ b/xen/arch/x86/include/asm/test-smoc.h @@ -0,0 +1,18 @@ +#ifndef _ASM_X86_TEST_SMC_H_ +#define _ASM_X86_TEST_SMC_H_ + +#include + +int test_smoc(uint32_t selection, uint32_t *results); + +#endif /* _ASM_X86_TEST_SMC_H_ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index 3cba2be0af6c..e026b0ea5adc 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -58,6 +58,7 @@ #include #include #include +#include /* opt_nosmp: If true, secondary processors are ignored. */ static bool __initdata opt_nosmp; @@ -1951,6 +1952,8 @@ void asmlinkage __init noreturn __start_xen(unsigned long mbi_p) alternative_branches(); + test_smoc(XEN_SYSCTL_TEST_SMOC_ALL, NULL); + /* * NB: when running as a PV shim VCPUOP_up/down is wired to the shim * physical cpu_add/remove functions, so launch the guest with only diff --git a/xen/arch/x86/sysctl.c b/xen/arch/x86/sysctl.c index 1d40d82c5ad2..918e56631b94 100644 --- a/xen/arch/x86/sysctl.c +++ b/xen/arch/x86/sysctl.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -423,6 +424,14 @@ long arch_do_sysctl( break; } + case XEN_SYSCTL_test_smoc: + ret = test_smoc(sysctl->u.test_smoc.tests, + &sysctl->u.test_smoc.results); + if ( !ret && __copy_field_to_guest(u_sysctl, sysctl, + u.test_smoc.results) ) + ret = -EFAULT; + break; + default: ret = -ENOSYS; break; diff --git a/xen/arch/x86/test/Makefile b/xen/arch/x86/test/Makefile new file mode 100644 index 000000000000..b504b8196659 --- /dev/null +++ b/xen/arch/x86/test/Makefile @@ -0,0 +1 @@ +obj-y += smoc.o diff --git a/xen/arch/x86/test/smoc.c b/xen/arch/x86/test/smoc.c new file mode 100644 index 000000000000..e7529f937a5a --- /dev/null +++ b/xen/arch/x86/test/smoc.c @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +#include +#include +#include + +static bool cf_check test_insn_replacement(void) +{ +#define EXPECTED_VALUE 2 + unsigned int r = ~EXPECTED_VALUE; + + alternative_io("", "mov %1, %0", X86_FEATURE_ALWAYS, + "+r" (r), "i" (EXPECTED_VALUE)); + + return r == EXPECTED_VALUE; +#undef EXPECTED_VALUE +} + +int test_smoc(uint32_t selection, uint32_t *results) +{ + struct { + unsigned int mask; + bool (*test)(void); + const char *name; + } static const tests[] = { + { XEN_SYSCTL_TEST_SMOC_INSN_REPL, &test_insn_replacement, + "alternative instruction replacement" }, + }; + unsigned int i; + + if ( selection & ~XEN_SYSCTL_TEST_SMOC_ALL ) + return -EINVAL; + + if ( results ) + *results = 0; + + printk(XENLOG_INFO "Checking Self Modify Code\n"); + + for ( i = 0; i < ARRAY_SIZE(tests); i++ ) + { + if ( !(selection & tests[i].mask) ) + continue; + + if ( tests[i].test() ) + { + if ( results ) + *results |= tests[i].mask; + continue; + } + + add_taint(TAINT_ERROR_SMOC); + printk(XENLOG_ERR "%s test failed\n", tests[i].name); + } + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/common/kernel.c b/xen/common/kernel.c index 08dbaa2a054c..8ddfd309f65e 100644 --- a/xen/common/kernel.c +++ b/xen/common/kernel.c @@ -386,13 +386,14 @@ char *print_tainted(char *str) { if ( tainted ) { - snprintf(str, TAINT_STRING_MAX_LEN, "Tainted: %c%c%c%c%c%c", + snprintf(str, TAINT_STRING_MAX_LEN, "Tainted: %c%c%c%c%c%c%c", tainted & TAINT_MACHINE_INSECURE ? 'I' : ' ', tainted & TAINT_MACHINE_CHECK ? 'M' : ' ', tainted & TAINT_SYNC_CONSOLE ? 'C' : ' ', tainted & TAINT_ERROR_INJECT ? 'E' : ' ', tainted & TAINT_HVM_FEP ? 'H' : ' ', - tainted & TAINT_CPU_OUT_OF_SPEC ? 'S' : ' '); + tainted & TAINT_CPU_OUT_OF_SPEC ? 'S' : ' ', + tainted & TAINT_ERROR_SMOC ? 'A' : ' '); } else { diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index 9b19679caeb1..4b17f1344732 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -1180,6 +1180,7 @@ struct xen_sysctl_cpu_policy { }; typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cpu_policy_t); + #endif #if defined(__arm__) || defined (__aarch64__) @@ -1201,6 +1202,13 @@ struct xen_sysctl_dt_overlay { }; #endif +struct xen_sysctl_test_smoc { + uint32_t tests; /* IN: bitmap with selected tests to execute. */ +#define XEN_SYSCTL_TEST_SMOC_INSN_REPL (1U << 0) +#define XEN_SYSCTL_TEST_SMOC_ALL (XEN_SYSCTL_TEST_SMOC_INSN_REPL) + uint32_t results; /* OUT: test result: 1 -> success, 0 -> failure. */ +}; + struct xen_sysctl { uint32_t cmd; #define XEN_SYSCTL_readconsole 1 @@ -1232,6 +1240,7 @@ struct xen_sysctl { /* #define XEN_SYSCTL_set_parameter 28 */ #define XEN_SYSCTL_get_cpu_policy 29 #define XEN_SYSCTL_dt_overlay 30 +#define XEN_SYSCTL_test_smoc 31 uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */ union { struct xen_sysctl_readconsole readconsole; @@ -1259,6 +1268,7 @@ struct xen_sysctl { struct xen_sysctl_cpu_levelling_caps cpu_levelling_caps; struct xen_sysctl_cpu_featureset cpu_featureset; struct xen_sysctl_livepatch_op livepatch; + struct xen_sysctl_test_smoc test_smoc; #if defined(__i386__) || defined(__x86_64__) struct xen_sysctl_cpu_policy cpu_policy; #endif diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index 1793be5b6b89..575431e4bd38 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -167,6 +167,7 @@ uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c); #define TAINT_HVM_FEP (1u << 3) #define TAINT_MACHINE_INSECURE (1u << 4) #define TAINT_CPU_OUT_OF_SPEC (1u << 5) +#define TAINT_ERROR_SMOC (1U << 6) extern unsigned int tainted; #define TAINT_STRING_MAX_LEN 20 extern char *print_tainted(char *str); From patchwork Thu Dec 14 10:17:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13492703 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 3F8F2C10F13 for ; Thu, 14 Dec 2023 10:18:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.654403.1021303 (Exim 4.92) (envelope-from ) id 1rDimo-0006ar-QW; Thu, 14 Dec 2023 10:17:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 654403.1021303; Thu, 14 Dec 2023 10:17: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 1rDimo-0006ZQ-Ig; Thu, 14 Dec 2023 10:17:46 +0000 Received: by outflank-mailman (input) for mailman id 654403; Thu, 14 Dec 2023 10:17:45 +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 1rDimn-0006Da-CH for xen-devel@lists.xenproject.org; Thu, 14 Dec 2023 10:17:45 +0000 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [2a00:1450:4864:20::32d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0554b220-9a6a-11ee-98e9-6d05b1d4d9a1; Thu, 14 Dec 2023 11:17:44 +0100 (CET) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-40c2db2ee28so84621525e9.2 for ; Thu, 14 Dec 2023 02:17:44 -0800 (PST) Received: from localhost ([213.195.127.70]) by smtp.gmail.com with ESMTPSA id gw18-20020a05600c851200b004053e9276easm26276280wmb.32.2023.12.14.02.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 02:17:43 -0800 (PST) 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 Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0554b220-9a6a-11ee-98e9-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1702549063; x=1703153863; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7uem9HU9IMtRwRD4U1/+rZK9QwDDjIx6C/QW5H+G6tU=; b=eoScLbzCAlE+/+SQ0LdM0UkCB0eQ2O+YnyhC7mM9Rqbdyo3X8Lv1gqdPOiaNhOWTqJ i2460U2w7yh7UZssuOM1jDG4JhmmWZK23Au25im76OUWlU0a+xuZj4wOlDmH8JRYcVv+ V/zKnK1xl2sPyyV0bmqxoO+1DmQF40unQnNKo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702549063; x=1703153863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7uem9HU9IMtRwRD4U1/+rZK9QwDDjIx6C/QW5H+G6tU=; b=PGCrmr8I5nyNZlIkCni8RvlFAjOF723rLJd4oGpLlyxKt+Oy7sS1Mct0h/Heok1mBj 32EGKtlpDZdbboaehWHS4JaBHvbUuJXf2/M/U9+tHfiinWmdMeXnI5Im3QnZJokGtFCC zYXyHcH0UZ/UV7r4YvEsBIyMnxZyFZJOxmBePFfjEJHllFZRRWy1q1fmwGT92BZv0G5w 4SP60al2K7/aAVl4eYAfM7cF8km7CoeSpQKdPqDSSt8ghIHHqKJJT+cnA1L2VlC67MvR MUF2xKaq3NZbQXGWwG/wiJc9E3Yt/KRHByCxILsFO4yrTHKKrBIsiQHY8Pa85WgH1dj2 pQDg== X-Gm-Message-State: AOJu0YzeKOUWVNShqHywYrkAg7IYbCAuqyxsrvjojJVMGqDepKpcqxEP WG32we7J0SV07BnpG97ONJH6ezyGiWeBcCLfB2w= X-Google-Smtp-Source: AGHT+IHCqH8ZUtNHk8Qx/sLuLfjtYkIYiJVgPji27dkFRJbFYjadLYaWNCkNyWDQXfD28fkQVvzpFw== X-Received: by 2002:a05:600c:1705:b0:40c:36ed:c5c6 with SMTP id c5-20020a05600c170500b0040c36edc5c6mr5405038wmn.29.1702549063621; Thu, 14 Dec 2023 02:17:43 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Konrad Rzeszutek Wilk , Ross Lagerwall , Wei Liu , Anthony PERARD , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini Subject: [PATCH v3 3/4] x86/livepatch: introduce a basic live patch test to gitlab CI Date: Thu, 14 Dec 2023 11:17:18 +0100 Message-ID: <20231214101719.18770-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231214101719.18770-1-roger.pau@citrix.com> References: <20231214101719.18770-1-roger.pau@citrix.com> MIME-Version: 1.0 Introduce a basic livepatch test using the interface to run self modifying tests. The introduced test relies on changing a function from returning false to returning true. To simplify the burden of keeping a patch that can be provided to livepatch-build-tools, introduce two new files: one containing the unpatched test functions, and another one that contains the patched forms of such functions. Note that only the former is linked into the Xen image, the latter is built but the object file is not consumed afterwards. Do this to assert that the file containing the patched functions continues to build. Since livepatch testing will ensure that the functions are not patched previous the applying the livepatch, allow the livepatch related tests to fail without tainting the hypervisor. Note the livepatch tests are not run as part of the self modifying checks executed during boot, as they would obviously fail. Signed-off-by: Roger Pau Monné Acked-by: Jan Beulich --- Changes since v2: - Clarify comment about xor vs mov instructions for return false/true encodings. Changes since v1: - New interface & test. --- tools/misc/xen-livepatch.c | 29 ++++++++++++++++++++++++++++ xen/arch/x86/include/asm/test-smoc.h | 4 ++++ xen/arch/x86/setup.c | 2 +- xen/arch/x86/test/Makefile | 2 ++ xen/arch/x86/test/smoc-lp-alt.c | 24 +++++++++++++++++++++++ xen/arch/x86/test/smoc-lp.c | 24 +++++++++++++++++++++++ xen/arch/x86/test/smoc.c | 11 ++++++++++- xen/include/public/sysctl.h | 6 +++++- 8 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 xen/arch/x86/test/smoc-lp-alt.c create mode 100644 xen/arch/x86/test/smoc-lp.c diff --git a/tools/misc/xen-livepatch.c b/tools/misc/xen-livepatch.c index 5bf9d9a32b65..4ebd1b4e936d 100644 --- a/tools/misc/xen-livepatch.c +++ b/tools/misc/xen-livepatch.c @@ -37,6 +37,7 @@ void show_help(void) " replace apply patch and revert all others.\n" " unload unload name patch.\n" " load [flags] upload and apply with name as the name\n" + " test execute self modifying code livepatch hypervisor tests\n" " Supported flags:\n" " --nodeps Disable inter-module buildid dependency check.\n" " Check only against hypervisor buildid.\n", @@ -542,6 +543,33 @@ error: return rc; } +static int test_func(int argc, char *argv[]) +{ + uint32_t results = 0; + int rc; + + if ( argc != 0 ) + { + show_help(); + return -1; + } + + rc = xc_test_smoc(xch, XEN_SYSCTL_TEST_SMOC_LP, &results); + if ( rc ) + { + fprintf(stderr, "test operation failed: %s\n", strerror(errno)); + return -1; + } + if ( (results & XEN_SYSCTL_TEST_SMOC_LP) != XEN_SYSCTL_TEST_SMOC_LP ) + { + fprintf(stderr, "some tests failed: %#x (expected %#x)\n", + results, XEN_SYSCTL_TEST_SMOC_LP); + return -1; + } + + return 0; +} + /* * These are also functions in action_options that are called in case * none of the ones in main_options match. @@ -554,6 +582,7 @@ struct { { "list", list_func }, { "upload", upload_func }, { "load", load_func }, + { "test", test_func }, }; int main(int argc, char *argv[]) diff --git a/xen/arch/x86/include/asm/test-smoc.h b/xen/arch/x86/include/asm/test-smoc.h index 2547b925d291..d369cf39687c 100644 --- a/xen/arch/x86/include/asm/test-smoc.h +++ b/xen/arch/x86/include/asm/test-smoc.h @@ -5,6 +5,10 @@ int test_smoc(uint32_t selection, uint32_t *results); +#ifdef CONFIG_LIVEPATCH +bool cf_check test_lp_insn_replacement(void); +#endif + #endif /* _ASM_X86_TEST_SMC_H_ */ /* diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index e026b0ea5adc..b97faa7a3e97 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -1952,7 +1952,7 @@ void asmlinkage __init noreturn __start_xen(unsigned long mbi_p) alternative_branches(); - test_smoc(XEN_SYSCTL_TEST_SMOC_ALL, NULL); + test_smoc(XEN_SYSCTL_TEST_SMOC_ALL & ~XEN_SYSCTL_TEST_SMOC_LP, NULL); /* * NB: when running as a PV shim VCPUOP_up/down is wired to the shim diff --git a/xen/arch/x86/test/Makefile b/xen/arch/x86/test/Makefile index b504b8196659..3a5a0a98e4db 100644 --- a/xen/arch/x86/test/Makefile +++ b/xen/arch/x86/test/Makefile @@ -1 +1,3 @@ obj-y += smoc.o +obj-$(CONFIG_LIVEPATCH) += smoc-lp.o # for livepatch testing +extra-$(CONFIG_LIVEPATCH) += smoc-lp-alt.o diff --git a/xen/arch/x86/test/smoc-lp-alt.c b/xen/arch/x86/test/smoc-lp-alt.c new file mode 100644 index 000000000000..d054e9bedc51 --- /dev/null +++ b/xen/arch/x86/test/smoc-lp-alt.c @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +/* + * Interesting case because `return false` can be encoded as `xor %eax, %eax`, + * which is shorter than `return true` which is encoded as a `mov $1, %eax` + * instruction (based on code generated by GCC 13.2 at -O2), and also shorter + * than the replacement `jmp` instruction. + */ +bool cf_check test_lp_insn_replacement(void) +{ + return true; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/test/smoc-lp.c b/xen/arch/x86/test/smoc-lp.c new file mode 100644 index 000000000000..e972791480b5 --- /dev/null +++ b/xen/arch/x86/test/smoc-lp.c @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +/* + * Interesting case because `return false` can be encoded as `xor %eax, %eax`, + * which is shorter than `return true` which is encoded as a `mov $1, %eax` + * instruction (based on code generated by GCC 13.2 at -O2), and also shorter + * than the replacement `jmp` instruction. + */ +bool cf_check test_lp_insn_replacement(void) +{ + return false; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/test/smoc.c b/xen/arch/x86/test/smoc.c index e7529f937a5a..8e74d26432ee 100644 --- a/xen/arch/x86/test/smoc.c +++ b/xen/arch/x86/test/smoc.c @@ -27,6 +27,10 @@ int test_smoc(uint32_t selection, uint32_t *results) } static const tests[] = { { XEN_SYSCTL_TEST_SMOC_INSN_REPL, &test_insn_replacement, "alternative instruction replacement" }, +#ifdef CONFIG_LIVEPATCH + { XEN_SYSCTL_TEST_SMOC_LP_INSN, &test_lp_insn_replacement, + "livepatch instruction replacement" }, +#endif }; unsigned int i; @@ -50,7 +54,12 @@ int test_smoc(uint32_t selection, uint32_t *results) continue; } - add_taint(TAINT_ERROR_SMOC); + /* + * livepatch related tests don't taint the hypervisor because we also + * want to check the failing case. + */ + if ( !(tests[i].mask & XEN_SYSCTL_TEST_SMOC_LP) ) + add_taint(TAINT_ERROR_SMOC); printk(XENLOG_ERR "%s test failed\n", tests[i].name); } diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index 4b17f1344732..aa4068d6996b 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -1205,7 +1205,11 @@ struct xen_sysctl_dt_overlay { struct xen_sysctl_test_smoc { uint32_t tests; /* IN: bitmap with selected tests to execute. */ #define XEN_SYSCTL_TEST_SMOC_INSN_REPL (1U << 0) -#define XEN_SYSCTL_TEST_SMOC_ALL (XEN_SYSCTL_TEST_SMOC_INSN_REPL) +#define XEN_SYSCTL_TEST_SMOC_LP_INSN (1U << 1) +#define XEN_SYSCTL_TEST_SMOC_ALL (XEN_SYSCTL_TEST_SMOC_INSN_REPL | \ + XEN_SYSCTL_TEST_SMOC_LP_INSN) +#define XEN_SYSCTL_TEST_SMOC_LP (XEN_SYSCTL_TEST_SMOC_LP_INSN) + uint32_t results; /* OUT: test result: 1 -> success, 0 -> failure. */ }; From patchwork Thu Dec 14 10:17:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13492704 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 16CC4C4167D for ; Thu, 14 Dec 2023 10:18:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.654404.1021321 (Exim 4.92) (envelope-from ) id 1rDimr-0007FT-9S; Thu, 14 Dec 2023 10:17:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 654404.1021321; Thu, 14 Dec 2023 10:17:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rDimr-0007FC-4M; Thu, 14 Dec 2023 10:17:49 +0000 Received: by outflank-mailman (input) for mailman id 654404; Thu, 14 Dec 2023 10:17:47 +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 1rDimp-0006DQ-Dy for xen-devel@lists.xenproject.org; Thu, 14 Dec 2023 10:17:47 +0000 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [2a00:1450:4864:20::32f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 062cf64c-9a6a-11ee-9b0f-b553b5be7939; Thu, 14 Dec 2023 11:17:45 +0100 (CET) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-40c38e292c8so2429645e9.0 for ; Thu, 14 Dec 2023 02:17:45 -0800 (PST) Received: from localhost ([213.195.127.70]) by smtp.gmail.com with ESMTPSA id j17-20020a05600c1c1100b0040b48690c49sm24179651wms.6.2023.12.14.02.17.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 02:17:44 -0800 (PST) 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 Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 062cf64c-9a6a-11ee-9b0f-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1702549064; x=1703153864; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xhWfcBNxu2blCdsb5H8j6/TvKkbd8xdFJdTYrKG7mJg=; b=wbonGj0Kwk8uGuZs5Df6z/JKDA4ljr22cnEQoMVtufA+ApFOkmguKdQ9jNMADydugH PJSloC33xFi4p/PqrC3p3hCmaXXp0UEf5qv1W/Bmc6DT8al8eaq+YRswlf1aAlWzofjt u82pmecob7/9WsAGY+PDkczsurz2WQl1puSVs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702549064; x=1703153864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xhWfcBNxu2blCdsb5H8j6/TvKkbd8xdFJdTYrKG7mJg=; b=ARjpeD1qSg8YxiHI8kvM7uh9vb8Og9SStoTgxLoVHKqxIWcDbDDVAcaINvQMdT0Sk6 cCVShmjABJkiJKiJi+qJd0aOvcgWGFz/EY8LAbfm/+1L76GEWyP3ElKXBvYCrb1IKGtE 2jzFpyl+s/82OElxWPGIkhiczUryDpDPjL8opmDFt47qDhJOK6pjHcGUSWcXUh669mwd K+dcr0Mks2lZjl64DUkfUZ9CrxoMq4MxRjzK6ZhKR4mgC88yKaLa28WsvPZj1Vc2xy6m fiwtB2Xs6XXIdKIH7wYbQlh2yslWKG8PzSMsoSO03yUl3rcHh9YCFGxoN+/iZ22p04Pb rHdg== X-Gm-Message-State: AOJu0YyN1ZEVehztzAXLCsRLjATDnx7s6sgVmKLYD3ttNvFLPLU8v9QI 7paJWKJTxyiTSVCzlfoRStDQEsGmsQjOGbaZs/E= X-Google-Smtp-Source: AGHT+IEc3PMulJYY6bnUPnHgqHCAJ8klvXR3bgb3dZiWTdxk4PHeDJW2ocvrQ8nVeSVg2y6jHgGuqw== X-Received: by 2002:a05:600c:3b99:b0:40b:5e26:237b with SMTP id n25-20020a05600c3b9900b0040b5e26237bmr4736968wms.44.1702549064581; Thu, 14 Dec 2023 02:17:44 -0800 (PST) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Doug Goldstein , Stefano Stabellini Subject: [PATCH v3 4/4] automation: add x86-64 livepatching test Date: Thu, 14 Dec 2023 11:17:19 +0100 Message-ID: <20231214101719.18770-5-roger.pau@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231214101719.18770-1-roger.pau@citrix.com> References: <20231214101719.18770-1-roger.pau@citrix.com> MIME-Version: 1.0 Introduce a new gitlab tests for livepatching, using livepatch-build-tools, which better reflects how downstreams build live patches rather than the in-tree tests. The tests applies the dummy in-tree patch example, checks that the patch is applied correctly and then reverts and unloads it. Signed-off-by: Roger Pau Monné Reviewed-by: Stefano Stabellini --- Changes since v2: - Split livepatch build into a separate script. --- automation/gitlab-ci/build.yaml | 9 +++ automation/gitlab-ci/test.yaml | 8 +++ automation/scripts/build-livepatch | 27 ++++++++ .../scripts/qemu-alpine-x86_64-livepatch.sh | 68 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100755 automation/scripts/build-livepatch create mode 100755 automation/scripts/qemu-alpine-x86_64-livepatch.sh diff --git a/automation/gitlab-ci/build.yaml b/automation/gitlab-ci/build.yaml index 32af30ccedc9..d770bffb845e 100644 --- a/automation/gitlab-ci/build.yaml +++ b/automation/gitlab-ci/build.yaml @@ -358,6 +358,15 @@ alpine-3.18-gcc-debug: variables: CONTAINER: alpine:3.18 +alpine-3.18-gcc-livepatch: + extends: .gcc-x86-64-build + script: + - ./automation/scripts/build-livepatch 2>&1 | tee build.log + variables: + CONTAINER: alpine:3.18 + EXTRA_XEN_CONFIG: | + CONFIG_LIVEPATCH=y + debian-stretch-gcc-debug: extends: .gcc-x86-64-build-debug variables: diff --git a/automation/gitlab-ci/test.yaml b/automation/gitlab-ci/test.yaml index 6aabdb9d156f..58a90be5ed0e 100644 --- a/automation/gitlab-ci/test.yaml +++ b/automation/gitlab-ci/test.yaml @@ -459,3 +459,11 @@ qemu-smoke-ppc64le-powernv9-gcc: needs: - qemu-system-ppc64-8.1.0-ppc64-export - debian-bullseye-gcc-ppc64le-debug + +qemu-alpine-x86_64-gcc-livepatch: + extends: .qemu-x86-64 + script: + - ./automation/scripts/qemu-alpine-x86_64-livepatch.sh 2>&1 | tee ${LOGFILE} + needs: + - *x86-64-test-needs + - alpine-3.18-gcc-livepatch diff --git a/automation/scripts/build-livepatch b/automation/scripts/build-livepatch new file mode 100755 index 000000000000..ac86b17ae5e4 --- /dev/null +++ b/automation/scripts/build-livepatch @@ -0,0 +1,27 @@ +#!/bin/bash -ex + +# Do a regular build first +./automation/scripts/build + +# Build a test livepatch using livepatch-build-tools. + +if [[ "$XEN_TARGET_ARCH" != "x86_64" ]]; then + exit 1 +fi + +BASE=xen/arch/x86/test/smoc-lp.c +ALT=xen/arch/x86/test/smoc-lp-alt.c + +[[ -f $BASE && -f $ALT ]] + +# git diff --no-index returns 0 if no differences, otherwise 1. +git diff --no-index --output=test.patch $BASE $ALT && exit 1 + +BUILDID=$(readelf -Wn xen/xen-syms | sed -n -e 's/^.*Build ID: //p') + +git clone https://xenbits.xen.org/git-http/livepatch-build-tools.git +cd livepatch-build-tools +make +./livepatch-build -s ../ -p ../test.patch -o out -c ../xen/.config \ + --depends $BUILDID --xen-depends $BUILDID +cp out/test.livepatch ../binaries/test.livepatch diff --git a/automation/scripts/qemu-alpine-x86_64-livepatch.sh b/automation/scripts/qemu-alpine-x86_64-livepatch.sh new file mode 100755 index 000000000000..da478cac4376 --- /dev/null +++ b/automation/scripts/qemu-alpine-x86_64-livepatch.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +set -ex + +cd binaries +# initrd.tar.gz is Dom0 rootfs +mkdir -p rootfs +cd rootfs +tar xvzf ../initrd.tar.gz +mkdir proc +mkdir run +mkdir srv +mkdir sys +rm var/run +cp -ar ../dist/install/* . +cp ../test.livepatch ./root/ +cat << "EOF" >> etc/local.d/xen-lp.start +#!/bin/bash + +set -ex + +trap poweroff EXIT + +export LD_LIBRARY_PATH=/usr/local/lib + +xen-livepatch test && exit 1 || true + +xen-livepatch load /root/test.livepatch + +# Cannot fail now +xen-livepatch test + +xen-livepatch revert test +xen-livepatch unload test + +xen-livepatch test && exit 1 || true + +echo "SUCCESS" +EOF +chmod +x etc/local.d/xen-lp.start +echo "rc_verbose=yes" >> etc/rc.conf +# rebuild Dom0 rootfs +find . |cpio -H newc -o|gzip > ../xen-rootfs.cpio.gz +cd ../.. + +cat >> binaries/pxelinux.0 << EOF +#!ipxe + +kernel xen console=com1 console_timestamps=boot +module bzImage console=hvc0 +module xen-rootfs.cpio.gz +boot +EOF + +# Run the test +rm -f smoke.serial +timeout -k 1 360 \ +qemu-system-x86_64 \ + -cpu qemu64,+svm \ + -m 2G -smp 2 \ + -monitor none -serial stdio \ + -nographic \ + -device virtio-net-pci,netdev=n0 \ + -netdev user,id=n0,tftp=binaries,bootfile=/pxelinux.0 |& \ + tee smoke.serial | sed 's/\r//' + +grep -q "SUCCESS" smoke.serial +exit 0