From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D368533585 for ; Tue, 8 Sep 2026 12:04:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869049; cv=none; b=g7R6hIiPbNrr6sTGJGE2ynFs5uaGsJ3LlKLhOybwsZEQikKtIbmXmzMc9WZVVIhg3cwrzFVnhI4haYtkN1vmt8s5wlcpyrKOr/kgkV38tYPyFzMgl8oWcse/jn3nNFLr0tECCPcQQhJDcHa4y0ayyma80VoY2YmRt5epKuH1qCE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869049; c=relaxed/simple; bh=cH9ZFlTTwBx6gTQvUA9o+bwd6zf5Ixzxuv5rrVI8jVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B3MAzVmayuK9FiaI4jMthT1qIP9/KG2rOdwAGu6FnvLc8UZ2xViIt7lQC0D19GjDKc5JJkXkmR26iQb2M9apMty2aWiQmv4vg/4dDM56LDAb1yZKKEPDHmI3E2Q+rPNOWHbf9aLngJrqlKrDF4X+ZJnyLzOx2PbS0P7qpcPsTrw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=aLnikJAc; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="aLnikJAc" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-48441fa5c37so3351868f8f.3 for ; Tue, 08 Sep 2026 05:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1788869040; x=1789473840; darn=vger.kernel.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:content-type; bh=jKgVI4X0gmbdB8ANT43nqIe9jOqilzM5WPOV0gw6+oU=; b=aLnikJAcHY11kthhbcKfItP7wJ+YUosCqdSM0yUxKVZtfK/8eY904fy+73rrxvK24u G6rj9T5qKZwTkZbCn3vpOwMCKIUp0U2VsDhzL6zPlOd2ABWtHYWlGqcQvpfDp8A99gDJ VJqOGGOhCiGbAl4HzGl4Q11vSTy5uGaJ/cr2O9t8eJ9gHkXhYVB1DLLwez4hlBMPAGfZ 7bbGusncX7YD/VW174sW0hyxcn9f8+cxIgVCUnO2Zl9ZOFeuRynJGGvzzwQKjAAxz6r1 hGGBAigOqSdG77+CKTFStQTNgOIze7o97CdKn/fRLqMs74EfnGbCpyXSD8PEkVJcXdge Or7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788869040; x=1789473840; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jKgVI4X0gmbdB8ANT43nqIe9jOqilzM5WPOV0gw6+oU=; b=n5PZGWbdTjn3dnCym2xtiWnjU9iTNWlJpIMHSu/GjBM1NAPmjH43C5zwvUz8lwMw7n 7tlLFbYMeEZMyhpQTUlUV3uJgAagNAE/52yTGhJ0+90zxJ3FBJlLr9jg0HFdcX4yOfBO YSHo/wMxRLDbfIIzV0HJH+W7EnzOwfc/F6kl9R6PtUFPBhQWwhIjzBJc5u0vKVk/LpiZ MAxCaNaS85L4On565dhd3W5l8zNDySHnOJj6AJgTkcbMFNIdXI2nNoDR/ro0e694B2ai F4qbdI946UteLlKVs5jEkV8cPqwJ52InJMgOxy9zKRTfu5va6+6TH1z0BAFMEFjTzFqr rnWQ== X-Forwarded-Encrypted: i=1; AKwUvBzsPgl9Bhhy7qgn1o+DuKhTCAW+Okn+ADny5AAJFK4tZs67iUBw/obS9+WDq14PLBT7LmRTale9EYsz3aA=@vger.kernel.org X-Gm-Message-State: AFuF++mVPfmsVEHkwAT6yYfv5/pyNpkchkio/3+yu+U/t7j/ErXYlW/B LB1Hv6CQ/eg2zuqfx6BLH49sJqAeqqcPSJHPEkG5h0mrakserj3IjVcbfnnPAYkepnw= X-Gm-Gg: AYBFou0u6bkGLS4E8NLbfGFhqcEQn0AbJS7vbMd0nM5R9nI2GU70H0wwbcHeO8YVOBa /JAiSftNwoeiicG77H1KeKDfq3//MoZ+hojORJhEFNvNfwkKi6N2bykjtt30BmpsKuNgg8XczkT Dp65A1lhIBjadSaXnx7fhxR7KVvWEfwkh6uWKRI282nMxpyKCIixVAukb2t8353/ramUUXlEm4x +Vo1nt1HobM1Q2ntX+r4f8JMLiaawBPFfj6by+9sV5h1mkQ2BsPqr+IfOA6S00Q6dC/XHloTpAD YrxGyVxVZLoyIeY0YBAJ//dBm8rJDLJnEfbT+WDSVlDv/dqtCAjOeuOY+qkMgu8i56gPJOtxOcp MUR2ahOITcV9OKDdc2Z18015Oq4qWYltT6KgwR1Visq0Z3NpwWeDXS+wpvweZBht6KNVhg/Dmrh lMZoxVP29iZbVQb0V3UaiZ515cRH+E4wj5/hFUkP8zU1LRqQV+qlc= X-Received: by 2002:a05:6000:2505:b0:485:8ad5:f965 with SMTP id ffacd0b85a97d-4858ad5ff97mr28316709f8f.2.1788869040340; Tue, 08 Sep 2026 05:04:00 -0700 (PDT) Received: from pathway ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-485883c6ba4sm40354754f8f.25.2026.09.08.05.03.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 05:03:59 -0700 (PDT) From: Petr Mladek To: Harry Hsu , jpoimboe@kernel.org, mbenes@suse.cz, joe.lawrence@redhat.com Cc: jikos@kernel.org, live-patching@vger.kernel.org, shuah@kernel.org, song@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/5] selftests/livepatch: Test rejection of aliased symbols in one object Date: Tue, 8 Sep 2026 14:03:22 +0200 Message-ID: <20260908120325.299649-3-pmladek@suse.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260908120325.299649-1-pmladek@suse.com> References: <20260908120325.299649-1-pmladek@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Harry Hsu klp_init_object_loaded() now rejects an object whose klp_funcs resolve to the same address, because aliased symbols would push two klp_funcs of one livepatch onto a single ops->func_stack and leave the redirection ambiguous. Add a target module providing test_klp_alias_show() together with its __alias() sibling, and a livepatch naming both of them. Two test cases cover both callers of klp_init_object_loaded(): the klp_enable_patch() path, where the target module is loaded before the livepatch, and the klp_module_coming() path, where the livepatch is loaded first and the module loader has to refuse the target module. Suggested-by: Song Liu Signed-off-by: Harry Hsu --- tools/testing/selftests/livepatch/Makefile | 3 +- .../testing/selftests/livepatch/test-alias.sh | 81 +++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 4 +- .../test_modules/test_klp_alias_patch.c | 62 ++++++++++++++ .../test_modules/test_klp_alias_target.c | 48 +++++++++++ 5 files changed, 196 insertions(+), 2 deletions(-) create mode 100755 tools/testing/selftests/livepatch/test-alias.sh create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_alias_patch.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_alias_target.c diff --git a/tools/testing/selftests/livepatch/Makefile b/tools/testing/selftests/livepatch/Makefile index a080eb54a215..ddbeff4cb53d 100644 --- a/tools/testing/selftests/livepatch/Makefile +++ b/tools/testing/selftests/livepatch/Makefile @@ -11,7 +11,8 @@ TEST_PROGS := \ test-ftrace.sh \ test-sysfs.sh \ test-syscall.sh \ - test-kprobe.sh + test-kprobe.sh \ + test-alias.sh TEST_FILES := settings diff --git a/tools/testing/selftests/livepatch/test-alias.sh b/tools/testing/selftests/livepatch/test-alias.sh new file mode 100755 index 000000000000..4ae701de0dbf --- /dev/null +++ b/tools/testing/selftests/livepatch/test-alias.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Harry Hsu + +. $(dirname $0)/functions.sh + +MOD_TARGET=test_klp_alias_target +MOD_LIVEPATCH=test_klp_alias_patch + +setup_config + + +# $MOD_TARGET provides two symbols that share a single address. A +# livepatch naming both of them would push two klp_funcs of the same +# patch onto one ops->func_stack, leaving the redirection ambiguous, so +# klp_init_object_loaded() has to reject the object. +# +# - load the target module and verify it produces the original output +# - verify that a livepatch naming both aliases fails to load +# - verify that the target module has been left unpatched + +start_test "livepatch of two aliased symbols in one object" + +load_mod $MOD_TARGET + +if [[ "$(cat /proc/$MOD_TARGET)" != "$MOD_TARGET: original output" ]] ; then + echo -e "FAIL\n\n" + die "livepatch kselftest(s) failed" +fi + +load_failing_mod $MOD_LIVEPATCH + +if [[ "$(cat /proc/$MOD_TARGET)" != "$MOD_TARGET: original output" ]] ; then + echo -e "FAIL\n\n" + die "livepatch kselftest(s) failed" +fi + +unload_mod $MOD_TARGET + +check_result "% insmod test_modules/$MOD_TARGET.ko +$MOD_TARGET: ${MOD_TARGET}_init +% insmod test_modules/$MOD_LIVEPATCH.ko +livepatch: 'test_klp_alias_show' and 'test_klp_alias_show_alias' resolve to the same address, aliased symbols are not supported +insmod: ERROR: could not insert module test_modules/$MOD_LIVEPATCH.ko: Invalid parameters +% rmmod $MOD_TARGET +$MOD_TARGET: ${MOD_TARGET}_exit" + + +# The same object is initialized from klp_module_coming() when the +# livepatch is loaded while the target module is still absent. There +# the error has to be propagated to the module loader instead. +# +# - load the livepatch, it is accepted because the object is not loaded +# - verify that loading the target module is refused afterwards + +start_test "aliased symbols in a module coming after the livepatch" + +load_lp $MOD_LIVEPATCH +load_failing_mod $MOD_TARGET +disable_lp $MOD_LIVEPATCH +unload_lp $MOD_LIVEPATCH + +check_result "% insmod test_modules/$MOD_LIVEPATCH.ko +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 +% insmod test_modules/$MOD_TARGET.ko +livepatch: 'test_klp_alias_show' and 'test_klp_alias_show_alias' resolve to the same address, aliased symbols are not supported +livepatch: failed to initialize patch '$MOD_LIVEPATCH' for module '$MOD_TARGET' (-22) +livepatch: patch '$MOD_LIVEPATCH' failed for module '$MOD_TARGET', refusing to load module '$MOD_TARGET' +insmod: ERROR: could not insert module test_modules/$MOD_TARGET.ko: Invalid parameters +% echo 0 > $SYSFS_KLP_DIR/$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" + +exit 0 diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile index a13d398585dc..532403e2b5ff 100644 --- a/tools/testing/selftests/livepatch/test_modules/Makefile +++ b/tools/testing/selftests/livepatch/test_modules/Makefile @@ -1,7 +1,9 @@ TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) KDIR ?= /lib/modules/$(shell uname -r)/build -obj-m += test_klp_atomic_replace.o \ +obj-m += test_klp_alias_patch.o \ + test_klp_alias_target.o \ + test_klp_atomic_replace.o \ test_klp_callbacks_busy.o \ test_klp_callbacks_demo.o \ test_klp_callbacks_demo2.o \ diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_alias_patch.c b/tools/testing/selftests/livepatch/test_modules/test_klp_alias_patch.c new file mode 100644 index 000000000000..1b50088bc92d --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_alias_patch.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Harry Hsu + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include + +static int livepatch_alias_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%s: %s\n", THIS_MODULE->name, + "this has been live patched"); + return 0; +} + +/* + * Both names resolve to one address, so they end up on a single + * ops->func_stack and the redirection would be ambiguous. Loading this + * livepatch is expected to fail. + */ +static struct klp_func funcs[] = { + { + .old_name = "test_klp_alias_show", + .new_func = livepatch_alias_show, + }, + { + .old_name = "test_klp_alias_show_alias", + .new_func = livepatch_alias_show, + }, + {}, +}; + +static struct klp_object objs[] = { + { + .name = "test_klp_alias_target", + .funcs = funcs, + }, + {}, +}; + +static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, +}; + +static int test_klp_alias_patch_init(void) +{ + return klp_enable_patch(&patch); +} + +static void test_klp_alias_patch_exit(void) +{ +} + +module_init(test_klp_alias_patch_init); +module_exit(test_klp_alias_patch_exit); +MODULE_LICENSE("GPL"); +MODULE_INFO(livepatch, "Y"); +MODULE_AUTHOR("Harry Hsu "); +MODULE_DESCRIPTION("Livepatch test: patch two aliased symbols of one object"); diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_alias_target.c b/tools/testing/selftests/livepatch/test_modules/test_klp_alias_target.c new file mode 100644 index 000000000000..b0f5fc35adf8 --- /dev/null +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_alias_target.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Harry Hsu + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include + +static struct proc_dir_entry *pde; + +static noinline int test_klp_alias_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%s: %s\n", THIS_MODULE->name, "original output"); + return 0; +} + +/* + * Alias the function above so that both names resolve to one address, the + * way __do_sys_fork(), __ia32_sys_fork() and __x64_sys_fork() do in vmlinux. + * Nothing calls the alias, it only has to show up in the module's symbol + * table for the livepatch to name it. + */ +static int test_klp_alias_show_alias(struct seq_file *m, void *v) + __used __alias(test_klp_alias_show); + +static int test_klp_alias_target_init(void) +{ + pr_info("%s\n", __func__); + pde = proc_create_single("test_klp_alias_target", 0, NULL, + test_klp_alias_show); + if (!pde) + return -ENOMEM; + return 0; +} + +static void test_klp_alias_target_exit(void) +{ + pr_info("%s\n", __func__); + proc_remove(pde); +} + +module_init(test_klp_alias_target_init); +module_exit(test_klp_alias_target_exit); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Harry Hsu "); +MODULE_DESCRIPTION("Livepatch test: target module with two aliased symbols"); -- 2.55.0