From patchwork Wed Aug 14 07:58:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wieczorkiewicz, Pawel" X-Patchwork-Id: 11093487 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5FE9F6C5 for ; Wed, 14 Aug 2019 08:00:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B23F22B1F for ; Wed, 14 Aug 2019 08:00:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3EC76287A4; Wed, 14 Aug 2019 08:00:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_ADSP_ALL, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0848522B1F for ; Wed, 14 Aug 2019 08:00:23 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hxoBU-0001Tw-Ik; Wed, 14 Aug 2019 07:59:04 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hxoBU-0001Tq-4a for xen-devel@lists.xen.org; Wed, 14 Aug 2019 07:59:04 +0000 X-Inumbo-ID: 6198f11a-be69-11e9-ac16-5beba594774b Received: from smtp-fw-6002.amazon.com (unknown [52.95.49.90]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6198f11a-be69-11e9-ac16-5beba594774b; Wed, 14 Aug 2019 07:59:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1565769542; x=1597305542; h=from:to:cc:subject:date:message-id:mime-version; bh=N0Xjb44SoXY229XZ17xnLZJpdqrjLArGLuQOpbMphsw=; b=kYw5r1X8bqRJS23fJ+kILgRWC+XbZBtzl7Af6Ne6e9gPyzpMCAb8zRbg svlm7gy/C3SkyfovMkghr3N2/kJ2vsgpDZzSfigd613b1EQ/iPRvlS3/b esTcHRPSyAlDt2dB9s2h13DtTX4/6GMwdn735hmT0JLxCYAFLlpZLbglO k=; X-IronPort-AV: E=Sophos;i="5.64,384,1559520000"; d="scan'208";a="415358383" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-303d0b0e.us-east-1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 14 Aug 2019 07:59:02 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-303d0b0e.us-east-1.amazon.com (Postfix) with ESMTPS id EC84BA2120; Wed, 14 Aug 2019 07:59:01 +0000 (UTC) Received: from EX13D05EUB002.ant.amazon.com (10.43.166.45) by EX13MTAUEA001.ant.amazon.com (10.43.61.243) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 14 Aug 2019 07:59:01 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D05EUB002.ant.amazon.com (10.43.166.45) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 14 Aug 2019 07:58:59 +0000 Received: from dev-dsk-wipawel-1a-0c4e6d58.eu-west-1.amazon.com (10.4.134.33) by mail-relay.amazon.com (10.43.160.118) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Wed, 14 Aug 2019 07:58:58 +0000 From: Pawel Wieczorkiewicz To: Date: Wed, 14 Aug 2019 07:58:54 +0000 Message-ID: <20190814075854.83923-1-wipawel@amazon.de> X-Mailer: git-send-email 2.16.5 MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [livepatch-hooks-1 PATCH 3/3] livepatch: Implement pre-|post- apply|revert hooks X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: mpohlack@amazon.de, ross.lagerwall@citrix.com, wipawel@amazon.de, konrad.wilk@oracle.com Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This is an implementation of 4 new livepatch module vetoing hooks, that can be optionally supplied along with modules. Hooks that currently exists in the livepatch mechanism aren't agile enough and have various limitations: * run only from within a quiescing zone * cannot conditionally prevent applying or reverting * do not have access to the module context To address these limitations the following has been implemented: 1) pre-apply hook runs before the apply action is scheduled for execution. Its main purpose is to prevent from applying a hotpatch when certain expected conditions aren't met or when mutating actions implemented in the hook fail or cannot be executed. 2) post-apply hook runs after the apply action has been executed and quiescing zone exited. Its main purpose is to provide an ability to follow-up on actions performed by the pre- hook, when module application was successful or undo certain preparation steps of the pre- hook in case of a failure. The success/failure error code is proviVded to the post- hooks via the rc field of the payload structure. 3) pre-revert hook runs before the revert action is scheduled for execution. Its main purpose is to prevent from reverting a hotpatch when certain expected conditions aren't met or when mutating actions implemented in the hook fail or cannot be executed. 4) post-revert hook runs after the revert action has been executed and quiescing zone exited. Its main purpose is to perform cleanup of all previously executed mutating actions in order to restore the original system state from before the current module application. The success/failure error code is provided to the post- hooks via the rc field of the payload structure. The replace action performs atomically the following actions: - revert all applied modules - apply a single replacement module. With the vetoing hooks in place various inter-hook dependencies may arise. Also, during the revert part of the operation certain vetoing hooks may detect failing conditions that previously were satisfied. That could in turn lead to situation when the revert part must be rolled back with all the pre- and post- hooks re-applied, which again can't be guaranteed to always succeed. The simplest response to this complication is to disallow the replace action completely on modules with vetoing hooks. Signed-off-by: Pawel Wieczorkiewicz Reviewed-by: Andra-Irina Paraschiv Reviewed-by: Petre Eftime Reviewed-by: Martin Pohlack Reviewed-by: Norbert Manthey --- xen/common/livepatch.c | 179 ++++++++++++++++++++++++++++++++---- xen/include/xen/livepatch_payload.h | 27 ++++++ 2 files changed, 189 insertions(+), 17 deletions(-) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index ed5756a032..464c07ad28 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -28,6 +28,8 @@ #include #include +#define is_hook_enabled(hook) ({ (hook) && *(hook); }) + /* * Protects against payload_list operations and also allows only one * caller in schedule_work. @@ -501,6 +503,35 @@ static int check_special_sections(const struct livepatch_elf *elf) return 0; } +/* + * Lookup specified section and when exists assign its address to a specified hook. + * Perform section pointer and size validation: single hook sections must contain a + * single pointer only. + */ +#define LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, hook, section_name) do { \ + const struct livepatch_elf_sec *__sec = livepatch_elf_sec_by_name(elf, section_name); \ + if ( !__sec ) \ + break; \ + if ( !section_ok(elf, __sec, sizeof(*hook)) || __sec->sec->sh_size != sizeof(*hook) ) \ + return -EINVAL; \ + hook = __sec->load_addr; \ +} while (0) + +/* + * Lookup specified section and when exists assign its address to a specified hook. + * Perform section pointer and size validation: multi hook sections must contain an + * array whose size must be a multiple of the array's items size. + */ +#define LIVEPATCH_ASSIGN_MULTI_HOOK(elf, hook, nhooks, section_name) do { \ + const struct livepatch_elf_sec *__sec = livepatch_elf_sec_by_name(elf, section_name); \ + if ( !__sec ) \ + break; \ + if ( !section_ok(elf, __sec, sizeof(*hook)) ) \ + return -EINVAL; \ + hook = __sec->load_addr; \ + nhooks = __sec->sec->sh_size / sizeof(*hook); \ +} while (0) + static int prepare_payload(struct payload *payload, struct livepatch_elf *elf) { @@ -552,25 +583,14 @@ static int prepare_payload(struct payload *payload, return rc; } - sec = livepatch_elf_sec_by_name(elf, ".livepatch.hooks.load"); - if ( sec ) - { - if ( !section_ok(elf, sec, sizeof(*payload->load_funcs)) ) - return -EINVAL; - - payload->load_funcs = sec->load_addr; - payload->n_load_funcs = sec->sec->sh_size / sizeof(*payload->load_funcs); - } + LIVEPATCH_ASSIGN_MULTI_HOOK(elf, payload->load_funcs, payload->n_load_funcs, ".livepatch.hooks.load"); + LIVEPATCH_ASSIGN_MULTI_HOOK(elf, payload->unload_funcs, payload->n_unload_funcs, ".livepatch.hooks.unload"); - sec = livepatch_elf_sec_by_name(elf, ".livepatch.hooks.unload"); - if ( sec ) - { - if ( !section_ok(elf, sec, sizeof(*payload->unload_funcs)) ) - return -EINVAL; + LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, payload->hooks.apply.pre, ".livepatch.hooks.preapply"); + LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, payload->hooks.apply.post, ".livepatch.hooks.postapply"); + LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, payload->hooks.revert.pre, ".livepatch.hooks.prerevert"); + LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, payload->hooks.revert.post, ".livepatch.hooks.postrevert"); - payload->unload_funcs = sec->load_addr; - payload->n_unload_funcs = sec->sec->sh_size / sizeof(*payload->unload_funcs); - } sec = livepatch_elf_sec_by_name(elf, ELF_BUILD_ID_NOTE); if ( sec ) { @@ -1217,6 +1237,39 @@ static bool_t is_work_scheduled(const struct payload *data) return livepatch_work.do_work && livepatch_work.data == data; } +/* + * Check if payload has any of the vetoing, non-atomic hooks assigned. + * A vetoing, non-atmic hook may perform an operation that changes the + * hypervisor state and may not be guaranteed to succeed. Result of + * such operation may be returned and may change the livepatch workflow. + * Such hooks may require additional cleanup actions performed by other + * hooks. Thus they are not suitable for replace action. + */ +static inline bool_t has_payload_any_vetoing_hooks(const struct payload *payload) +{ + return is_hook_enabled(payload->hooks.apply.pre) || + is_hook_enabled(payload->hooks.apply.post) || + is_hook_enabled(payload->hooks.revert.pre) || + is_hook_enabled(payload->hooks.revert.post); +} + +/* + * Checks if any of the already applied hotpatches has any vetoing, + * non-atomic hooks assigned. + */ +static inline bool_t livepatch_applied_have_vetoing_hooks(void) +{ + struct payload *p; + + list_for_each_entry ( p, &applied_list, applied_list ) + { + if ( has_payload_any_vetoing_hooks(p) ) + return true; + } + + return false; +} + static int schedule_work(struct payload *data, uint32_t cmd, uint32_t timeout) { ASSERT(spin_is_locked(&payload_lock)); @@ -1317,6 +1370,7 @@ void check_for_livepatch_work(void) { struct payload *p; unsigned int cpus; + bool_t action_done = false; p = livepatch_work.data; if ( !get_cpu_maps() ) @@ -1369,6 +1423,7 @@ void check_for_livepatch_work(void) livepatch_do_action(); /* Serialize and flush out the CPU via CPUID instruction (on x86). */ arch_livepatch_post_action(); + action_done = true; local_irq_restore(flags); } @@ -1381,6 +1436,43 @@ void check_for_livepatch_work(void) /* put_cpu_maps has an barrier(). */ put_cpu_maps(); + if ( action_done ) + { + switch ( livepatch_work.cmd ) + { + case LIVEPATCH_ACTION_REVERT: + if ( is_hook_enabled(p->hooks.revert.post) ) + { + printk(XENLOG_INFO LIVEPATCH "%s: Calling post-revert hook function with rc=%d\n", + p->name, p->rc); + + (*p->hooks.revert.post)(p); + } + break; + + case LIVEPATCH_ACTION_APPLY: + if ( is_hook_enabled(p->hooks.apply.post) ) + { + printk(XENLOG_INFO LIVEPATCH "%s: Calling post-apply hook function with rc=%d\n", + p->name, p->rc); + + (*p->hooks.apply.post)(p); + } + break; + + case LIVEPATCH_ACTION_REPLACE: + if ( has_payload_any_vetoing_hooks(p) ) + { + /* It should be impossible to get here since livepatch_action() guards against that. */ + panic(LIVEPATCH "%s: REPLACE action is not supported on hotpatches with vetoing hooks!\n", + p->name); + ASSERT_UNREACHABLE(); + } + default: + break; + } + } + printk(XENLOG_INFO LIVEPATCH "%s finished %s with rc=%d\n", p->name, names[livepatch_work.cmd], p->rc); } @@ -1516,6 +1608,21 @@ static int livepatch_action(struct xen_sysctl_livepatch_action *action) rc = -EBUSY; break; } + + if ( is_hook_enabled(data->hooks.revert.pre) ) + { + printk(XENLOG_INFO LIVEPATCH "%s: Calling pre-revert hook function\n", data->name); + + rc = (*data->hooks.revert.pre)(data); + if ( rc ) + { + printk(XENLOG_ERR LIVEPATCH "%s: pre-revert hook failed (rc=%d), aborting!\n", + data->name, rc); + data->rc = rc; + break; + } + } + data->rc = -EAGAIN; rc = schedule_work(data, action->cmd, action->timeout); } @@ -1549,6 +1656,20 @@ static int livepatch_action(struct xen_sysctl_livepatch_action *action) break; } + if ( is_hook_enabled(data->hooks.apply.pre) ) + { + printk(XENLOG_INFO LIVEPATCH "%s: Calling pre-apply hook function\n", data->name); + + rc = (*data->hooks.apply.pre)(data); + if ( rc ) + { + printk(XENLOG_ERR LIVEPATCH "%s: pre-apply hook failed (rc=%d), aborting!\n", + data->name, rc); + data->rc = rc; + break; + } + } + data->rc = -EAGAIN; rc = schedule_work(data, action->cmd, action->timeout); } @@ -1560,6 +1681,30 @@ static int livepatch_action(struct xen_sysctl_livepatch_action *action) rc = build_id_dep(data, 1 /* against hypervisor. */); if ( rc ) break; + + /* + * REPLACE action is not supported on hotpatches with vetoing hooks. + * Vetoing hooks usually perform mutating actions on the system and + * typically exist in pairs (pre- hook doing an action and post- hook + * undoing the action). Coalescing all hooks from all applied modules + * cannot be performed without inspecting potential dependencies between + * the mutating hooks and hence cannot be performed automatically by + * the replace action. Also, the replace action cannot safely assume a + * successful revert of all the module with vetoing hooks. When one + * of the hooks fails due to not meeting certain conditions the whole + * replace operation must have been reverted with all previous pre- and + * post- hooks re-executed (which cannot be guaranteed to succeed). + * The simplest response to this complication is disallow replace + * action on modules with vetoing hooks. + */ + if ( has_payload_any_vetoing_hooks(data) || livepatch_applied_have_vetoing_hooks() ) + { + printk(XENLOG_ERR LIVEPATCH "%s: REPLACE action is not supported on hotpatches with vetoing hooks!\n", + data->name); + rc = -EOPNOTSUPP; + break; + } + data->rc = -EAGAIN; rc = schedule_work(data, action->cmd, action->timeout); } diff --git a/xen/include/xen/livepatch_payload.h b/xen/include/xen/livepatch_payload.h index 99613af2db..cd20944cc4 100644 --- a/xen/include/xen/livepatch_payload.h +++ b/xen/include/xen/livepatch_payload.h @@ -21,6 +21,16 @@ typedef struct payload livepatch_payload_t; typedef void livepatch_loadcall_t(void); typedef void livepatch_unloadcall_t(void); +typedef int livepatch_precall_t(livepatch_payload_t *arg); +typedef void livepatch_postcall_t(livepatch_payload_t *arg); + +struct livepatch_hooks { + struct { + livepatch_precall_t *const *pre; + livepatch_postcall_t *const *post; + } apply, revert; +}; + struct payload { uint32_t state; /* One of the LIVEPATCH_STATE_*. */ int32_t rc; /* 0 or -XEN_EXX. */ @@ -47,6 +57,7 @@ struct payload { struct livepatch_build_id xen_dep; /* ELFNOTE_DESC(.livepatch.xen_depends). */ livepatch_loadcall_t *const *load_funcs; /* The array of funcs to call after */ livepatch_unloadcall_t *const *unload_funcs;/* load and unload of the payload. */ + struct livepatch_hooks hooks; /* Pre and post hooks for apply and revert */ unsigned int n_load_funcs; /* Nr of the funcs to load and execute. */ unsigned int n_unload_funcs; /* Nr of funcs to call durung unload. */ char name[XEN_LIVEPATCH_NAME_SIZE]; /* Name of it. */ @@ -76,6 +87,22 @@ struct payload { livepatch_unloadcall_t *__weak \ const livepatch_unload_data_##_fn __section(".livepatch.hooks.unload") = _fn; +#define LIVEPATCH_PREAPPLY_HOOK(_fn) \ + livepatch_precall_t *__attribute__((weak, used)) \ + const livepatch_preapply_data_##_fn __section(".livepatch.hooks.preapply") = _fn; + +#define LIVEPATCH_POSTAPPLY_HOOK(_fn) \ + livepatch_postcall_t *__attribute__((weak, used)) \ + const livepatch_postapply_data_##_fn __section(".livepatch.hooks.postapply") = _fn; + +#define LIVEPATCH_PREREVERT_HOOK(_fn) \ + livepatch_precall_t *__attribute__((weak, used)) \ + const livepatch_prerevert_data_##_fn __section(".livepatch.hooks.prerevert") = _fn; + +#define LIVEPATCH_POSTREVERT_HOOK(_fn) \ + livepatch_postcall_t *__attribute__((weak, used)) \ + const livepatch_postrevert_data_##_fn __section(".livepatch.hooks.postrevert") = _fn; + #endif /* __XEN_LIVEPATCH_PAYLOAD_H__ */ /*