Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp95050ybn; Thu, 3 Oct 2019 02:04:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqxgWr/gFM6STWRys1QamfTHV9yuteDHmTBmV111acZeToCudjtuKj617HzfMJZP2NilLCzS X-Received: by 2002:a05:6402:13c2:: with SMTP id a2mr8173128edx.21.1570093471378; Thu, 03 Oct 2019 02:04:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570093471; cv=none; d=google.com; s=arc-20160816; b=eKA+fitHlEvIQTO4IrP0C/j303Z44hhm/v30mf8yxACyz+lTJ1NA0vNfp37q/oaDxE U1kx8ZmJanKjbf9+qmws8A/h1GYbHsKiPHudcaCLJdJxrTuRMj9K5RhkcgA5JYfdgEA0 RaywvPyMDG+Hmfbk6A8h7RncZAfV+q4uy0EdU+z3pJ8HQZpB/u06vDVxttmQ/4hZDV2J ATNL6zYe2cW4ylBYcOs4TDidkfrN0o2l84euP7DmSg5dmnx0UDTbWgOwfEPoFujiLhxc sfJp573WIAtUN3+q+ijJt6IYxIsndQw/tupJB5u4/+W+w9tvZO+FJemnS24+HNYsHuxI LeBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=aP3pXfBN12w+U0XM3Wlqj737K9019OT8YL7hIeBfK98=; b=1KK1yNrff6hr1ukN2ls7K7QSIvfFh4L8HBZRnHcHJMHm2t4qzCBKqONjjuJG11SV45 cV7Zw3aNLpAA1WlIr1lw8vJgureR4UDHsCOBhUrjeEDmiMX+7/Mr+xxVoOVgxJ1qdGM+ A3pFFtLRYYlGqDN9H/GRqBF/BwPhADcP8Veaww0uJdzO9IMCofH06sdHFd7ZEcv4SYAz e4mK3Uqw7mVntw+qpV/NLG3H8F4QUUKx3NdQJGTRPBYFOlh3v+q6uzi42bQY28Hl6+bD dwk+JWYIYj3CqutatNxBsP1XIQBmuDZ1Tk/GA76/MwK8ir0ctzbcmYGKYC1QihGkExbR s3Gw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w22si891063eja.115.2019.10.03.02.04.06; Thu, 03 Oct 2019 02:04:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729027AbfJCJB7 (ORCPT + 99 others); Thu, 3 Oct 2019 05:01:59 -0400 Received: from mx2.suse.de ([195.135.220.15]:50316 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728969AbfJCJB5 (ORCPT ); Thu, 3 Oct 2019 05:01:57 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id E42C0B14A; Thu, 3 Oct 2019 09:01:55 +0000 (UTC) From: Petr Mladek To: Jiri Kosina , Josh Poimboeuf , Miroslav Benes Cc: Joe Lawrence , Kamalesh Babulal , Nicolai Stange , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH v3 2/5] livepatch: Basic API to track system state changes Date: Thu, 3 Oct 2019 11:01:34 +0200 Message-Id: <20191003090137.6874-3-pmladek@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191003090137.6874-1-pmladek@suse.com> References: <20191003090137.6874-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is another step how to help maintaining more livepatches. One big help was the atomic replace and cumulative livepatches. These livepatches replace the already installed ones. Therefore it should be enough when each cumulative livepatch is consistent. The problems might come with shadow variables and callbacks. They might change the system behavior or state so that it is no longer safe to go back and use an older livepatch or the original kernel code. Also, a new livepatch must be able to detect changes which were made by the already installed livepatches. This is where the livepatch system state tracking gets useful. It allows to: - find whether a system state has already been modified by previous livepatches - store data needed to manipulate and restore the system state The information about the manipulated system states is stored in an array of struct klp_state. It can be searched by two new functions klp_get_state() and klp_get_prev_state(). The dependencies are going to be solved by a version field added later. The only important information is that it will be allowed to modify the same state by more non-cumulative livepatches. It is similar to allowing to modify the same function several times. The livepatch author is responsible for preventing incompatible changes. Signed-off-by: Petr Mladek Acked-by: Miroslav Benes --- include/linux/livepatch.h | 15 +++++++++ kernel/livepatch/Makefile | 2 +- kernel/livepatch/state.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 kernel/livepatch/state.c diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 273400814020..726947338fd5 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -130,10 +130,21 @@ struct klp_object { bool patched; }; +/** + * struct klp_state - state of the system modified by the livepatch + * @id: system state identifier (non-zero) + * @data: custom data + */ +struct klp_state { + unsigned long id; + void *data; +}; + /** * struct klp_patch - patch structure for live patching * @mod: reference to the live patch module * @objs: object entries for kernel objects to be patched + * @states: system states that can get modified * @replace: replace all actively used patches * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources @@ -147,6 +158,7 @@ struct klp_patch { /* external */ struct module *mod; struct klp_object *objs; + struct klp_state *states; bool replace; /* internal */ @@ -217,6 +229,9 @@ void *klp_shadow_get_or_alloc(void *obj, unsigned long id, void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor); void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor); +struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id); +struct klp_state *klp_get_prev_state(unsigned long id); + #else /* !CONFIG_LIVEPATCH */ static inline int klp_module_coming(struct module *mod) { return 0; } diff --git a/kernel/livepatch/Makefile b/kernel/livepatch/Makefile index cf9b5bcdb952..cf03d4bdfc66 100644 --- a/kernel/livepatch/Makefile +++ b/kernel/livepatch/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_LIVEPATCH) += livepatch.o -livepatch-objs := core.o patch.o shadow.o transition.o +livepatch-objs := core.o patch.o shadow.o state.o transition.o diff --git a/kernel/livepatch/state.c b/kernel/livepatch/state.c new file mode 100644 index 000000000000..0a7014c57478 --- /dev/null +++ b/kernel/livepatch/state.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * system_state.c - State of the system modified by livepatches + * + * Copyright (C) 2019 SUSE + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include "core.h" +#include "transition.h" + +#define klp_for_each_state(patch, state) \ + for (state = patch->states; \ + state && state->id; \ + state++) + +/** + * klp_get_state() - get information about system state modified by + * the given patch + * @patch: livepatch that modifies the given system state + * @id: custom identifier of the modified system state + * + * Checks whether the given patch modifies the given system state. + * + * The function can be called either from pre/post (un)patch + * callbacks or from the kernel code added by the livepatch. + * + * Return: pointer to struct klp_state when found, otherwise NULL. + */ +struct klp_state *klp_get_state(struct klp_patch *patch, unsigned long id) +{ + struct klp_state *state; + + klp_for_each_state(patch, state) { + if (state->id == id) + return state; + } + + return NULL; +} +EXPORT_SYMBOL_GPL(klp_get_state); + +/** + * klp_get_prev_state() - get information about system state modified by + * the already installed livepatches + * @id: custom identifier of the modified system state + * + * Checks whether already installed livepatches modify the given + * system state. + * + * The same system state can be modified by more non-cumulative + * livepatches. It is expected that the latest livepatch has + * the most up-to-date information. + * + * The function can be called only during transition when a new + * livepatch is being enabled or when such a transition is reverted. + * It is typically called only from from pre/post (un)patch + * callbacks. + * + * Return: pointer to the latest struct klp_state from already + * installed livepatches, NULL when not found. + */ +struct klp_state *klp_get_prev_state(unsigned long id) +{ + struct klp_patch *patch; + struct klp_state *state, *last_state = NULL; + + if (WARN_ON_ONCE(!klp_transition_patch)) + return NULL; + + klp_for_each_patch(patch) { + if (patch == klp_transition_patch) + goto out; + + state = klp_get_state(patch, id); + if (state) + last_state = state; + } + +out: + return last_state; +} +EXPORT_SYMBOL_GPL(klp_get_prev_state); -- 2.16.4