From patchwork Fri Nov 19 09:03:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miroslav Benes X-Patchwork-Id: 12628511 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D068C433EF for ; Fri, 19 Nov 2021 09:03:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3734461401 for ; Fri, 19 Nov 2021 09:03:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233716AbhKSJGd (ORCPT ); Fri, 19 Nov 2021 04:06:33 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:53964 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233624AbhKSJGb (ORCPT ); Fri, 19 Nov 2021 04:06:31 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id B667F212C9; Fri, 19 Nov 2021 09:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1637312608; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kDsgLT8c9xMnLP5HTg9s3ku1Ze0kqVMgaNncakOaoJY=; b=iDCOuYb7bzBWVqw0KvfB3O4cMj8fmBcUrzghBbDhwmIxGHlXPIlPQRsW4MSzpl9Z4cibGP 33CYMsEzsQ4DRpU/RngmgOVNB5Ia4hz+bVhT1iuWo9OX53EUw0pQBR00oVJfouUbZhTFPX vaF/MI4SbMpIZN46XHM5yhb4AOJhkaU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1637312608; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kDsgLT8c9xMnLP5HTg9s3ku1Ze0kqVMgaNncakOaoJY=; b=n5Ai3hYM80qTZ6YTkz20lrVnxDxXFanrbaoeLOmceCQaBkbcJHoWvA18K7NTyVi/+QkIZa UruNQOyqxvuQB6CQ== Received: from san.suse.cz (san.suse.cz [10.100.12.79]) by relay2.suse.de (Postfix) with ESMTP id 9E936A3B83; Fri, 19 Nov 2021 09:03:28 +0000 (UTC) From: Miroslav Benes To: jpoimboe@redhat.com, jikos@kernel.org, pmladek@suse.com, joe.lawrence@redhat.com Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, shuah@kernel.org, linux-kselftest@vger.kernel.org, Miroslav Benes Subject: [PATCH 1/3] livepatch: Move the initialization of old_func to a new function Date: Fri, 19 Nov 2021 10:03:25 +0100 Message-Id: <20211119090327.12811-2-mbenes@suse.cz> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119090327.12811-1-mbenes@suse.cz> References: <20211119090327.12811-1-mbenes@suse.cz> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org struct klp_func will be used not only for functions to be patched but also for functions which must not be found on a stack. Move the initialization of needed struct members to a separate function, so the code can be reused. Signed-off-by: Miroslav Benes --- kernel/livepatch/core.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 335d988bd811..3d8e3caf9f92 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -767,6 +767,28 @@ static int klp_apply_object_relocs(struct klp_patch *patch, return 0; } +static int klp_init_old_func(struct klp_object *obj, + struct klp_func *func) +{ + int ret; + + ret = klp_find_object_symbol(obj->name, func->old_name, + func->old_sympos, + (unsigned long *)&func->old_func); + if (ret) + return ret; + + ret = kallsyms_lookup_size_offset((unsigned long)func->old_func, + &func->old_size, NULL); + if (!ret) { + pr_err("kallsyms size lookup failed for '%s'\n", + func->old_name); + return -ENOENT; + } + + return 0; +} + /* parts of the initialization that is done only when the object is loaded */ static int klp_init_object_loaded(struct klp_patch *patch, struct klp_object *obj) @@ -787,20 +809,10 @@ static int klp_init_object_loaded(struct klp_patch *patch, } klp_for_each_func(obj, func) { - ret = klp_find_object_symbol(obj->name, func->old_name, - func->old_sympos, - (unsigned long *)&func->old_func); + ret = klp_init_old_func(obj, func); if (ret) return ret; - ret = kallsyms_lookup_size_offset((unsigned long)func->old_func, - &func->old_size, NULL); - if (!ret) { - pr_err("kallsyms size lookup failed for '%s'\n", - func->old_name); - return -ENOENT; - } - if (func->nop) func->new_func = func->old_func; From patchwork Fri Nov 19 09:03:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miroslav Benes X-Patchwork-Id: 12628513 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40DCDC433F5 for ; Fri, 19 Nov 2021 09:03:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2AF7461401 for ; Fri, 19 Nov 2021 09:03:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234535AbhKSJGe (ORCPT ); Fri, 19 Nov 2021 04:06:34 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:53980 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233721AbhKSJGb (ORCPT ); Fri, 19 Nov 2021 04:06:31 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id CF96921637; Fri, 19 Nov 2021 09:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1637312608; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=37oDjltHx1iDdukyCIaYT3+MO3HlBauFF/E+7rA0K9Y=; b=ZuFF3kFTYndokknLUM+/OWKLYmo+GN8O/QFQ4EA/XAhT2dORmzb+Jij3P8HGQnteUu2Q02 2SjO5N65Qz3F3RZ1nBC/F53PHmneirQgsTZN3Yvufv3XeHUI9petzDN5aaNztdEgcIzB2u QH4HVvha0CwcjrhFpFMHAzkYu1+vTJI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1637312608; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=37oDjltHx1iDdukyCIaYT3+MO3HlBauFF/E+7rA0K9Y=; b=UdgU843KQuTzzhlG1tDQKNyhU1/RDXmCitHG/V+f8yT1hNKPk8e+Ga3lRjqe7Th9iVvHke eD5n6TphzFw2Q8Dw== Received: from san.suse.cz (san.suse.cz [10.100.12.79]) by relay2.suse.de (Postfix) with ESMTP id B84BDA3B84; Fri, 19 Nov 2021 09:03:28 +0000 (UTC) From: Miroslav Benes To: jpoimboe@redhat.com, jikos@kernel.org, pmladek@suse.com, joe.lawrence@redhat.com Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, shuah@kernel.org, linux-kselftest@vger.kernel.org, Miroslav Benes Subject: [PATCH 2/3] livepatch: Allow user to specify functions to search for on a stack Date: Fri, 19 Nov 2021 10:03:26 +0100 Message-Id: <20211119090327.12811-3-mbenes@suse.cz> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119090327.12811-1-mbenes@suse.cz> References: <20211119090327.12811-1-mbenes@suse.cz> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org livepatch's consistency model requires that no live patched function must be found on any task's stack during a transition process after a live patch is applied. It is achieved by walking through stacks of all blocked tasks. The user might also want to define more functions to search for without them being patched at all. It may either help with preparing a live patch, which would otherwise require additional touches to achieve the consistency, or it can be used to overcome deficiencies the stack checking inherently has. For example, GCC may optimize a function so that a part of it is moved to a different section and the function would jump to it. This child function would not be found on a stack in this case, but it may be important to search for it so that, again, the consistency is achieved. Allow the user to specify such functions on klp_object level. Signed-off-by: Miroslav Benes --- include/linux/livepatch.h | 11 +++++++++++ kernel/livepatch/core.c | 16 ++++++++++++++++ kernel/livepatch/transition.c | 21 ++++++++++++++++----- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 2614247a9781..89df578af8c3 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -106,9 +106,11 @@ struct klp_callbacks { * struct klp_object - kernel object structure for live patching * @name: module name (or NULL for vmlinux) * @funcs: function entries for functions to be patched in the object + * @funcs_stack: function entries for functions to be stack checked * @callbacks: functions to be executed pre/post (un)patching * @kobj: kobject for sysfs resources * @func_list: dynamic list of the function entries + * @func_stack_list: dynamic list of the function entries for stack checking * @node: list node for klp_patch obj_list * @mod: kernel module associated with the patched object * (NULL for vmlinux) @@ -119,11 +121,13 @@ struct klp_object { /* external */ const char *name; struct klp_func *funcs; + struct klp_func *funcs_stack; struct klp_callbacks callbacks; /* internal */ struct kobject kobj; struct list_head func_list; + struct list_head func_stack_list; struct list_head node; struct module *mod; bool dynamic; @@ -187,12 +191,19 @@ struct klp_patch { func->old_name || func->new_func || func->old_sympos; \ func++) +#define klp_for_each_func_stack_static(obj, func) \ + for (func = obj->funcs_stack; \ + func && (func->old_name || func->old_sympos); func++) + #define klp_for_each_func_safe(obj, func, tmp_func) \ list_for_each_entry_safe(func, tmp_func, &obj->func_list, node) #define klp_for_each_func(obj, func) \ list_for_each_entry(func, &obj->func_list, node) +#define klp_for_each_func_stack(obj, func) \ + list_for_each_entry(func, &obj->func_stack_list, node) + int klp_enable_patch(struct klp_patch *); /* Called from the module loader during module coming/going states */ diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 3d8e3caf9f92..86fc73a06844 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -825,6 +825,12 @@ static int klp_init_object_loaded(struct klp_patch *patch, } } + klp_for_each_func_stack(obj, func) { + ret = klp_init_old_func(obj, func); + if (ret) + return ret; + } + return 0; } @@ -853,6 +859,11 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) return ret; } + klp_for_each_func_stack(obj, func) { + if (strlen(func->old_name) >= KSYM_NAME_LEN) + return -EINVAL; + } + if (klp_is_object_loaded(obj)) ret = klp_init_object_loaded(patch, obj); @@ -870,6 +881,7 @@ static void klp_init_object_early(struct klp_patch *patch, struct klp_object *obj) { INIT_LIST_HEAD(&obj->func_list); + INIT_LIST_HEAD(&obj->func_stack_list); kobject_init(&obj->kobj, &klp_ktype_object); list_add_tail(&obj->node, &patch->obj_list); } @@ -899,6 +911,10 @@ static int klp_init_patch_early(struct klp_patch *patch) klp_for_each_func_static(obj, func) { klp_init_func_early(obj, func); } + + klp_for_each_func_stack_static(obj, func) { + list_add_tail(&func->node, &obj->func_stack_list); + } } if (!try_module_get(patch->mod)) diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index 5683ac0d2566..be7afc5dc275 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -200,7 +200,10 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, for (i = 0; i < nr_entries; i++) { address = entries[i]; - if (klp_target_state == KLP_UNPATCHED) { + if (!func->new_func) { + func_addr = (unsigned long)func->old_func; + func_size = func->old_size; + } else if (klp_target_state == KLP_UNPATCHED) { /* * Check for the to-be-unpatched function * (the func itself). @@ -256,14 +259,22 @@ static int klp_check_stack(struct task_struct *task, const char **oldname) continue; klp_for_each_func(obj, func) { ret = klp_check_stack_func(func, entries, nr_entries); - if (ret) { - *oldname = func->old_name; - return -EADDRINUSE; - } + if (ret) + goto err; + } + + klp_for_each_func_stack(obj, func) { + ret = klp_check_stack_func(func, entries, nr_entries); + if (ret) + goto err; } } return 0; + +err: + *oldname = func->old_name; + return -EADDRINUSE; } static int klp_check_and_switch_task(struct task_struct *task, void *arg) From patchwork Fri Nov 19 09:03:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miroslav Benes X-Patchwork-Id: 12628515 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2E3FC43219 for ; Fri, 19 Nov 2021 09:03:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F7B261401 for ; Fri, 19 Nov 2021 09:03:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234540AbhKSJGe (ORCPT ); Fri, 19 Nov 2021 04:06:34 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:53990 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233845AbhKSJGb (ORCPT ); Fri, 19 Nov 2021 04:06:31 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id E8CEE2170E; Fri, 19 Nov 2021 09:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1637312608; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yr1/cvzgX2Oc+f3PB8NBBdQ0jQcpbd7E13OWVfePSbs=; b=xXdwPUHIWUVsSSaRzorAY8R0dLTO36vOJqlv253syWS15xIhphWh063F7PAfTSvrAT4T4g Moo/g0ZuudfljYlsyOEWJ3F+vuK67PmCoSih00BWQTRRmLrF+D78yEVUkXxmFL1sy16TPQ hkV0bpIXUAnZLRRJh7O/mJS/kWodRhg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1637312608; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yr1/cvzgX2Oc+f3PB8NBBdQ0jQcpbd7E13OWVfePSbs=; b=2YOd9T6VbLOjigofQoasH1iC+nDZsb3mFPmr/gYT2YSfa8gFloexJGQW/XAxMYhO161VJe gfwAUww2OVub2PAw== Received: from san.suse.cz (san.suse.cz [10.100.12.79]) by relay2.suse.de (Postfix) with ESMTP id D1A10A3B81; Fri, 19 Nov 2021 09:03:28 +0000 (UTC) From: Miroslav Benes To: jpoimboe@redhat.com, jikos@kernel.org, pmladek@suse.com, joe.lawrence@redhat.com Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, shuah@kernel.org, linux-kselftest@vger.kernel.org, Miroslav Benes Subject: [PATCH 3/3] selftests/livepatch: Test of the API for specifying functions to search for on a stack Date: Fri, 19 Nov 2021 10:03:27 +0100 Message-Id: <20211119090327.12811-4-mbenes@suse.cz> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119090327.12811-1-mbenes@suse.cz> References: <20211119090327.12811-1-mbenes@suse.cz> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a test for the API which allows the user to specify functions which are then searched for on any tasks's stack during a transition process. Signed-off-by: Miroslav Benes --- lib/Kconfig.debug | 1 + lib/livepatch/Makefile | 4 +- lib/livepatch/test_klp_funcstack_demo.c | 61 +++++++++++++ lib/livepatch/test_klp_funcstack_mod.c | 72 +++++++++++++++ tools/testing/selftests/livepatch/Makefile | 3 +- .../selftests/livepatch/test-func-stack.sh | 88 +++++++++++++++++++ 6 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 lib/livepatch/test_klp_funcstack_demo.c create mode 100644 lib/livepatch/test_klp_funcstack_mod.c create mode 100755 tools/testing/selftests/livepatch/test-func-stack.sh diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 9ef7ce18b4f5..aa4c97098f41 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2529,6 +2529,7 @@ config TEST_LIVEPATCH default n depends on DYNAMIC_DEBUG depends on LIVEPATCH + depends on DEBUG_FS depends on m help Test kernel livepatching features for correctness. The tests will diff --git a/lib/livepatch/Makefile b/lib/livepatch/Makefile index dcc912b3478f..584e3b8b5415 100644 --- a/lib/livepatch/Makefile +++ b/lib/livepatch/Makefile @@ -11,4 +11,6 @@ obj-$(CONFIG_TEST_LIVEPATCH) += test_klp_atomic_replace.o \ test_klp_shadow_vars.o \ test_klp_state.o \ test_klp_state2.o \ - test_klp_state3.o + test_klp_state3.o \ + test_klp_funcstack_mod.o \ + test_klp_funcstack_demo.o diff --git a/lib/livepatch/test_klp_funcstack_demo.c b/lib/livepatch/test_klp_funcstack_demo.c new file mode 100644 index 000000000000..902798077f05 --- /dev/null +++ b/lib/livepatch/test_klp_funcstack_demo.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2021 Miroslav Benes + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +static int funcstack; +module_param(funcstack, int, 0644); +MODULE_PARM_DESC(funcstack, "func_stack (default=0)"); + +static noinline void livepatch_child2_function(void) +{ + pr_info("%s\n", __func__); +} + +static struct klp_func funcs[] = { + { + .old_name = "child2_function", + .new_func = livepatch_child2_function, + }, {} +}; + +static struct klp_func funcs_stack[] = { + { + .old_name = "parent_function", + }, {} +}; + +static struct klp_object objs[] = { + { + .name = "test_klp_funcstack_mod", + .funcs = funcs, + }, {} +}; + +static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, +}; + +static int test_klp_funcstack_demo_init(void) +{ + if (funcstack) + objs[0].funcs_stack = funcs_stack; + + return klp_enable_patch(&patch); +} + +static void test_klp_funcstack_demo_exit(void) +{ +} + +module_init(test_klp_funcstack_demo_init); +module_exit(test_klp_funcstack_demo_exit); +MODULE_LICENSE("GPL"); +MODULE_INFO(livepatch, "Y"); +MODULE_AUTHOR("Miroslav Benes "); +MODULE_DESCRIPTION("Livepatch test: func_stack demo"); diff --git a/lib/livepatch/test_klp_funcstack_mod.c b/lib/livepatch/test_klp_funcstack_mod.c new file mode 100644 index 000000000000..127c6093d890 --- /dev/null +++ b/lib/livepatch/test_klp_funcstack_mod.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2021 Miroslav Benes + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include + +static int sleep_length = 10000; +module_param(sleep_length, int, 0644); +MODULE_PARM_DESC(sleep_length, "length of sleep in seconds (default=10)"); + +static noinline void child_function(void) +{ + pr_info("%s enter\n", __func__); + msleep(sleep_length); + pr_info("%s exit\n", __func__); +} + +static noinline void child2_function(void) +{ + pr_info("%s\n", __func__); +} + +static noinline void parent_function(void) +{ + pr_info("%s enter\n", __func__); + child_function(); + child2_function(); + pr_info("%s exit\n", __func__); +} + +static int parent_function_get(void *data, u64 *val) +{ + *val = 0; + parent_function(); + + return 0; +} + +DEFINE_DEBUGFS_ATTRIBUTE(fops_parent_function, parent_function_get, NULL, "%llu\n"); + +static struct dentry *debugfs_dir; + +static int test_klp_funcstack_mod_init(void) +{ + struct dentry *d; + + debugfs_dir = debugfs_create_dir("test_klp_funcstack", NULL); + if (IS_ERR(debugfs_dir)) + return PTR_ERR(debugfs_dir); + + d = debugfs_create_file("parent_function", 0400, debugfs_dir, NULL, + &fops_parent_function); + if (IS_ERR(d)) + debugfs_remove_recursive(debugfs_dir); + + return 0; +} + +static void test_klp_funcstack_mod_exit(void) +{ + debugfs_remove_recursive(debugfs_dir); +} + +module_init(test_klp_funcstack_mod_init); +module_exit(test_klp_funcstack_mod_exit); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Miroslav Benes "); +MODULE_DESCRIPTION("Livepatch test: func_stack module"); diff --git a/tools/testing/selftests/livepatch/Makefile b/tools/testing/selftests/livepatch/Makefile index 1acc9e1fa3fb..40f8a3a2e9aa 100644 --- a/tools/testing/selftests/livepatch/Makefile +++ b/tools/testing/selftests/livepatch/Makefile @@ -6,7 +6,8 @@ TEST_PROGS := \ test-callbacks.sh \ test-shadow-vars.sh \ test-state.sh \ - test-ftrace.sh + test-ftrace.sh \ + test-func-stack.sh TEST_FILES := settings diff --git a/tools/testing/selftests/livepatch/test-func-stack.sh b/tools/testing/selftests/livepatch/test-func-stack.sh new file mode 100755 index 000000000000..b7da62c9f5a1 --- /dev/null +++ b/tools/testing/selftests/livepatch/test-func-stack.sh @@ -0,0 +1,88 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2021 Miroslav Benes + +. $(dirname $0)/functions.sh + +MOD_TARGET=test_klp_funcstack_mod +MOD_LIVEPATCH=test_klp_funcstack_demo + +setup_config + +# - load a target module and call its parent_function(). It will sleep in its +# child_function() callee. +# - load a live patch with new child2_function() called from parent_function() +# too. The patching does not wait for child_function() to return, because +# child2_function() is not on any stack. +# - clean up afterwards + +start_test "non-blocking patching without the function on a stack" + +load_mod $MOD_TARGET + +(cat /sys/kernel/debug/test_klp_funcstack/parent_function) >/dev/null & +PID=$! + +load_lp $MOD_LIVEPATCH + +wait $PID + +disable_lp $MOD_LIVEPATCH +unload_lp $MOD_LIVEPATCH +unload_mod $MOD_TARGET + +check_result "% modprobe $MOD_TARGET +$MOD_TARGET: parent_function enter +$MOD_TARGET: child_function enter +% modprobe $MOD_LIVEPATCH +livepatch: enabling patch '$MOD_LIVEPATCH' +livepatch: '$MOD_LIVEPATCH': initializing patching transition +livepatch: '$MOD_LIVEPATCH': starting patching transition +livepatch: '$MOD_LIVEPATCH': completing patching transition +livepatch: '$MOD_LIVEPATCH': patching complete +$MOD_TARGET: child_function exit +$MOD_LIVEPATCH: livepatch_child2_function +$MOD_TARGET: parent_function exit +% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled +livepatch: '$MOD_LIVEPATCH': initializing unpatching transition +livepatch: '$MOD_LIVEPATCH': starting unpatching transition +livepatch: '$MOD_LIVEPATCH': completing unpatching transition +livepatch: '$MOD_LIVEPATCH': unpatching complete +% rmmod $MOD_LIVEPATCH +% rmmod $MOD_TARGET" + +# Similar to the previous test but now the patching has to wait for +# child2_function() to return, because parent_function() is also checked for. + +start_test "patching delayed due to the function on a stack" + +load_mod $MOD_TARGET + +(cat /sys/kernel/debug/test_klp_funcstack/parent_function) >/dev/null & + +load_lp $MOD_LIVEPATCH funcstack=1 +disable_lp $MOD_LIVEPATCH +unload_lp $MOD_LIVEPATCH +unload_mod $MOD_TARGET + +check_result "% modprobe $MOD_TARGET +$MOD_TARGET: parent_function enter +$MOD_TARGET: child_function enter +% modprobe $MOD_LIVEPATCH funcstack=1 +livepatch: enabling patch '$MOD_LIVEPATCH' +livepatch: '$MOD_LIVEPATCH': initializing patching transition +livepatch: '$MOD_LIVEPATCH': starting patching transition +$MOD_TARGET: child_function exit +$MOD_TARGET: child2_function +$MOD_TARGET: parent_function exit +livepatch: '$MOD_LIVEPATCH': completing patching transition +livepatch: '$MOD_LIVEPATCH': patching complete +% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled +livepatch: '$MOD_LIVEPATCH': initializing unpatching transition +livepatch: '$MOD_LIVEPATCH': starting unpatching transition +livepatch: '$MOD_LIVEPATCH': completing unpatching transition +livepatch: '$MOD_LIVEPATCH': unpatching complete +% rmmod $MOD_LIVEPATCH +% rmmod $MOD_TARGET" + +exit 0