From nobody Fri Sep 25 21:40:25 2026 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 352F55335A5 for ; Tue, 8 Sep 2026 12:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869035; cv=none; b=tptoG9klDzLHwjpfu/1vdnEjYb+EqUcd5ZglJt/YMGUffLdr9Y4BU2Jn9wjS1fqm7Lsv9cuuCKCckpotnhoqQD/+FUu2AQbRBWORDchJwqBAor5qlv2ZbEiJbfisByfK4DNB+P9NnmlTRgzSOwGXo3CfbkOMO4qYmnGpVykqN5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869035; c=relaxed/simple; bh=qExtoH4DxAKYg2NEpWN2pLnmFbV2eKsziD3oZ/VnUk8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KFd3hdeUI/ZFKbRYFgfPxG0cx6WmrqDWchROjO4+WbP6gNO+rEEkJBHtv8UgHCm/WlM69W7Q19UHne6fLGEFTdioi1On/+l9sRhDFa02ejKF1AcFLtl0o1vt7xpAP/ctnVp2t1Qtgc3DShWpRpGc5IJgo2YoQTvOKT/C13lZpmk= 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=RytTrp66; arc=none smtp.client-ip=209.85.128.48 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="RytTrp66" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-49b9320423cso51325455e9.0 for ; Tue, 08 Sep 2026 05:03:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1788869028; x=1789473828; 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=UPIDmw2XzGEgiwQZ4y7rbDSaWhFnhGlig2TC9EBp00g=; b=RytTrp66RYESyMlG3ZMuafCA/Wkg3YruMFYVBjPpv8+2g37L0/hMueKe8O1R6jZGQt K8/LxQnkx4O6+tkjIc+G3QM2CwFXIrLg5KHoA2TbOMYv62kwbu7n/ChVEUUm8W3r2wbJ wDsThrGrVnUSXUiSsem2S81RTkBWOkMX5SGcStMEHHGJ90imFiaKnltayyeqMW3M5lXQ Uyvya9XW1qtq4pCXaaDrQraVGCbzCRe4XihxRXguVqRLCU+4n0ZS8uHdRdbz6FebEZPZ N6sciddiGn7AWEsIwY5beEtPZoR86owdXUXJnhneXkhagKl9oWBHBOv6v2klujQvnNoA /Sqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788869028; x=1789473828; 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=UPIDmw2XzGEgiwQZ4y7rbDSaWhFnhGlig2TC9EBp00g=; b=WvYP+qUF0oUd/W0YhLn2kDGrwSXPta9bHPucIG8OMmICA+ByjlFZhQodbcKN1Us4DR 4RvHzWCF31zTOgcwzM2GcX47+P6HtT3skv4fL6N7glCCnejju0M3Oog1btJ3onfxbjpd 7w0JKjNSAoCoZvbZf58A4F3Pk+ikyublvspnEFGG+Mx2YlGebRzj+V+xBrCZFJYdVKE5 TjwG7hcLTTcVKSVIYGn0Pq8eMObh0BdI5PBv2MReSm/e+8gqQdBc+PZUR80ls79ZPaeC v+OckR/xlnsstZ4UrREGc/3DvziZp4a52FwoLLSRQ+mKwKIhQAD+vCMyTlK2SVwwPigN KBpA== X-Forwarded-Encrypted: i=1; AKwUvBw4a23YEgqiRMJRLZl++2XJ1Uey7/spn2pNRT3uUAEXufLgsoTNqEZ+6ibMC1XGHLcZJBrUExvPx2ab7Z0=@vger.kernel.org X-Gm-Message-State: AFuF++lkcS8fEIRBMmaIWysnUpe7xSpdtM7Y4cq1InGwnqgXpOH7/c2V XrbodisEM4tDWu+7D+8R5CqeeyEGAnarqNHAWdBGGy+qvmYrlE3Xm3KO+/xhGzFY4Vs= X-Gm-Gg: AYBFou3qTjAfRZQwyYnPmPGKmGD49Prt4z1muoXv9Q/PBRt4+4TiA+17twvAXmuBTYg BE6lrxA8hugcC9fnxAggDBANJ2PoOzcozmvbhDdbdstw2djbr1vUWEntgGSqpDoDTgHYBnqZTQw SgdXgsme1pkTEwKXzHUy8XizMVOhgQ4Ug7/G6+HPYNGWmVECntwQpqIcu5YqkAWfm68nkeRVYDh TOXzMRO2bfkLklX575sRqb/440m7jSKOvcSlBDh/I6Ynzhw0JuDR9cDSiwHHZjNDsxY/SczdkME A5nCHGZCDhQ7cfKStlsLkhUjn1173tiv6ZNtON9Y/tnxU2FJJa9x0ZcJh8CXS9MlTuJvbGFinkn dptvuLV9PQ4tsNdYhq7cXG3282vPs3mLnVvv6xFvQ9cHSN6TSZ1ivKESZi8Wrjn/2kINbfwjcvp uBDuYfNu+lAZVhfm2PV+JZYiUhgQuHBbXg4ITl3//cYjAXbNAAWwg= X-Received: by 2002:a05:600c:4f89:b0:49c:f617:7cf with SMTP id 5b1f17b1804b1-49cf7f32d0amr442300865e9.0.1788869027560; Tue, 08 Sep 2026 05:03:47 -0700 (PDT) Received: from pathway ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49ce5927b68sm468299665e9.1.2026.09.08.05.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 05:03:46 -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, Petr Mladek Subject: [PATCH v4 1/5] livepatch: Fail object initialization on duplicate patched function Date: Tue, 8 Sep 2026 14:03:21 +0200 Message-ID: <20260908120325.299649-2-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Harry Hsu Several symbols can share one address: ffffffff8ed7fef0 t __do_sys_fork ffffffff8ed7fef0 T __ia32_sys_fork ffffffff8ed7fef0 T __x64_sys_fork klp_find_ops() looks the ops up by func->old_func, i.e. by address, so two klp_funcs of the same livepatch naming two of these symbols resolve to the same klp_ops and are both pushed onto one ops->func_stack. This breaks the assumption that a single livepatch contributes at most one entry to any func_stack. klp_ftrace_handler() picks the entry at the top of the stack, but when both entries belong to the same livepatch there is nothing that says which of them should be used in the PATCHED state, and the UNPATCHED state has to end up at the original function either way. klp_check_stack_func() cannot tell them apart either: it asks whether the preceding entry is the original function or another livepatch's replacement, and an aliased sibling is neither. Patching two aliases of one function from a single livepatch was never meaningful, so fail object initialization in klp_init_object_loaded() rather than leave the redirection undefined. Fixes: 3c33f5b99d68 ("livepatch: support for repatching a function") Suggested-by: Petr Mladek Signed-off-by: Harry Hsu --- kernel/livepatch/core.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index a240d1144e89..a6762cbe74b7 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -863,7 +863,7 @@ static void klp_clear_object_relocs(struct klp_patch *p= atch, static int klp_init_object_loaded(struct klp_patch *patch, struct klp_object *obj) { - struct klp_func *func; + struct klp_func *func, *prev_func; int ret; =20 if (klp_is_module(obj)) { @@ -885,6 +885,21 @@ static int klp_init_object_loaded(struct klp_patch *pa= tch, if (ret) return ret; =20 + /* + * Aliased symbols share one address, so they would resolve to + * the same klp_ops and stack up on a single ops->func_stack, + * leaving the redirection ambiguous. + */ + klp_for_each_func(obj, prev_func) { + if (prev_func =3D=3D func) + break; + if (prev_func->old_func =3D=3D func->old_func) { + pr_err("'%s' and '%s' resolve to the same address, aliased symbols are= not supported\n", + prev_func->old_name, func->old_name); + return -EINVAL; + } + } + ret =3D kallsyms_lookup_size_offset((unsigned long)func->old_func, &func->old_size, NULL); if (!ret) { --=20 2.55.0 From nobody Fri Sep 25 21:40:25 2026 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: Miroslav Benes --- 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/sel= ftests/livepatch/Makefile index a080eb54a215..ddbeff4cb53d 100644 --- a/tools/testing/selftests/livepatch/Makefile +++ b/tools/testing/selftests/livepatch/Makefile @@ -11,7 +11,8 @@ TEST_PROGS :=3D \ test-ftrace.sh \ test-sysfs.sh \ test-syscall.sh \ - test-kprobe.sh + test-kprobe.sh \ + test-alias.sh =20 TEST_FILES :=3D settings =20 diff --git a/tools/testing/selftests/livepatch/test-alias.sh b/tools/testin= g/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=3Dtest_klp_alias_target +MOD_LIVEPATCH=3Dtest_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)" !=3D "$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)" !=3D "$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 t= o the same address, aliased symbols are not supported +insmod: ERROR: could not insert module test_modules/$MOD_LIVEPATCH.ko: Inv= alid 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 t= o the same address, aliased symbols are not supported +livepatch: failed to initialize patch '$MOD_LIVEPATCH' for module '$MOD_TA= RGET' (-22) +livepatch: patch '$MOD_LIVEPATCH' failed for module '$MOD_TARGET', refusin= g to load module '$MOD_TARGET' +insmod: ERROR: could not insert module test_modules/$MOD_TARGET.ko: Invali= d 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/tool= s/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 :=3D $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))= )) KDIR ?=3D /lib/modules/$(shell uname -r)/build =20 -obj-m +=3D test_klp_atomic_replace.o \ +obj-m +=3D 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_pat= ch.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[] =3D { + { + .old_name =3D "test_klp_alias_show", + .new_func =3D livepatch_alias_show, + }, + { + .old_name =3D "test_klp_alias_show_alias", + .new_func =3D livepatch_alias_show, + }, + {}, +}; + +static struct klp_object objs[] =3D { + { + .name =3D "test_klp_alias_target", + .funcs =3D funcs, + }, + {}, +}; + +static struct klp_patch patch =3D { + .mod =3D THIS_MODULE, + .objs =3D 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 objec= t"); diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_alias_= target.c b/tools/testing/selftests/livepatch/test_modules/test_klp_alias_ta= rget.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 vmlin= ux. + * 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 =3D 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= "); --=20 2.55.0 From nobody Fri Sep 25 21:40:25 2026 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 A03F64ADD8F for ; Tue, 8 Sep 2026 12:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869063; cv=none; b=RGGrrHbaI6NC5oGYhFYch3kXCY/twjq4uaj7R3wAp3kfz/ke7nIWakQ8Rk1t9IAB80KMEiHL7kdXNs7sOfZOA6TkGp1/oTo4xWjy5Or9u8GI+BX3COcXO+JPESlko7r6DJCU2LSc6nI6sb7kedtxrkEQVeXD47zcfC7wgMfo/Gc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869063; c=relaxed/simple; bh=fuQOCFjP8UkL1Qqt1zyRFi9QdF7VSvTkUwrEl2/BiJI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QoHcw/cy2rdYgc3mIS51BR0cou6Vu96Iut4EpigEQex5bWjOxOKBcmG56S8aHlOdURNs4BTOM0RXsWLRmchzm6PapGQQHKx2dd6OgLaqkj7fi1+lVQ4AJrvO+RoQ5VGEMkH+GpQF9WE+9QU4AcHK9KuvDwXaOsABECnhSpGasXU= 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=fZ3rstIX; arc=none smtp.client-ip=209.85.221.53 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="fZ3rstIX" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-485850cbac3so2930897f8f.3 for ; Tue, 08 Sep 2026 05:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1788869052; x=1789473852; 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=7Sj5DuG2I5WrYzkNIww4uEg16kFBgrgPS5/z1js5sn8=; b=fZ3rstIXQK39bJJqeN2HNAUYyjqG5Hcalr+gps03z4PvfYpcMCxfV/sR6pyOI1EcGf Xc2cUml4yCG/dNg7NNA0RJs2ZKhpB75wl5Y2mPJ+qIJoU7qr11/iVXYl/wJFbgG3cbEw sv8xKGY18PSuh4EAxQlaYVQrraLDI/8B7uyxGr3AKtIf49A5+mpfTpbuOZwcnxzwIZp8 dHaKpCESJuPbXmDuIcrPIHviOC30Xxo6h5Tq8lv2XQwQJQ1ktzMTeHsRB00HWZEOd3qQ IlsrvaQvJKiee/zU4/ozrkpLDf/R4BlKOnjVYWXPEzex81GKz67OR33CP/5e1L2fvGdC qm6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788869052; x=1789473852; 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=7Sj5DuG2I5WrYzkNIww4uEg16kFBgrgPS5/z1js5sn8=; b=R5r7d8jSpn9wVqRi4xK2Rif1CSF7hVoft8VJRAyCnwbeEG7L5wzPayBGtPV0E7Lhs2 w7p2dwW0hCiDFC1SeZ/nYQW2XGf2Ieq5Xn5eHD8X2MMReMdKSqjDXQISPB4eOQMlJ6xx Qbq4H8ju6dmS0qX+u2b9Qy1G05+OEJB/OVEmwPXXfzGCzdhir2BunuoRY23lnIuWrzUj KosSUP3rB05qFjCkKceqprmVoWl/tfjK0bOunQH7HSJp1QZDrnnw1T0HjQuMUpZqyvqc RS7ug2e4PKiMu8eP1sArNbIujqPvN7/x+5e6ahUMGzu7RBQPQsMPJowhY19nSnW55utM RYEg== X-Forwarded-Encrypted: i=1; AKwUvBztdB3JBHdKVjhhCOn+7lJ0FGBN1RFIL2spoRxAVp6AiZgokui6FN9Jpu5btxFFFLn5EjuTg2Wx1gwbhzk=@vger.kernel.org X-Gm-Message-State: AFuF++kACqUtx4D3xq1GbNdva6QlnrZwTEQM0BBRMgShmts6tsOQCXJX azPW1hyG986z234c7cl3BsHzFrMkq6VdjuR1vPL0y4+HHQlOJmuFsogbmM3LjJzyOYI= X-Gm-Gg: AYBFou2YU/dNuhpPqOcJkd8tjkc38Gn0eRKTmiDsgyz4dFf/aLBw6JT+N4MueiL58Z7 OAr4P9pshYlXhQQokErCjaC58CvgUsF5t7ru3B9E8ndopnAw8m5q5o3/WMkczxmmYet59AZZq+N f3ncgl4dPDqJWB7niAS8oZiiygNf7wky0OafOHN53fgCQk4SzafHgz+AsImP5wIYbC/pEY23rs4 C0E2Z5dESgmdwlbf/BkhKRFr3oQLLiCu4Ej7g0uEVmQKz2K69rdjZbXp/OmPF/jXczFfdv2U30y dk1gGejXGglaQZ83cw1TK8ke/Ggl3xrrQT8lzDPXaJFMUMDzv/9h8pmVlwyXd7yFa3UHatIZ5a9 IjuaKiPfgKjeUj7lbVScKHVe/Zd8gGWrnkxVNtbEZAHtvOwm+NdK4OIVi/PSKsRNw7e7rSM9J6J MyheutwDCrXvd4EVnaYcimybOKSvAlqjXn9Hl5hfu8FYtGE4QJidE= X-Received: by 2002:a05:6000:220f:b0:485:8c16:a350 with SMTP id ffacd0b85a97d-4858c16a62emr27247710f8f.40.1788869052365; Tue, 08 Sep 2026 05:04:12 -0700 (PDT) Received: from pathway ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4859207c28fsm30329290f8f.5.2026.09.08.05.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 05:04:12 -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, Petr Mladek Subject: [PATCH v4 3/5] livepatch: Move code for updating livepatch object relocations Date: Tue, 8 Sep 2026 14:03:23 +0200 Message-ID: <20260908120325.299649-4-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: quoted-printable Content-Type: text/plain; charset="utf-8" klp_free_object_loaded() is supposed to clear changes made by klp_init_object_loaded(). It should call klp_clear_object_relocs() which is currently defined later. Move the code for updating object relocations up. This is just a preparation step. No functional changes. Signed-off-by: Petr Mladek Acked-by: Miroslav Benes --- kernel/livepatch/core.c | 72 ++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index a6762cbe74b7..a6796cd6b65f 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -342,6 +342,42 @@ int klp_apply_section_relocs(struct module *pmod, Elf_= Shdr *sechdrs, secndx, objname, true); } =20 +static int klp_write_object_relocs(struct klp_patch *patch, + struct klp_object *obj, + bool apply) +{ + int i, ret; + struct klp_modinfo *info =3D patch->mod->klp_info; + + for (i =3D 1; i < info->hdr.e_shnum; i++) { + Elf_Shdr *sec =3D info->sechdrs + i; + + if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) + continue; + + ret =3D klp_write_section_relocs(patch->mod, info->sechdrs, + info->secstrings, + patch->mod->core_kallsyms.strtab, + info->symndx, i, obj->name, apply); + if (ret) + return ret; + } + + return 0; +} + +static int klp_apply_object_relocs(struct klp_patch *patch, + struct klp_object *obj) +{ + return klp_write_object_relocs(patch, obj, true); +} + +static void klp_clear_object_relocs(struct klp_patch *patch, + struct klp_object *obj) +{ + klp_write_object_relocs(patch, obj, false); +} + /* * Sysfs Interface * @@ -823,42 +859,6 @@ static int klp_init_func(struct klp_object *obj, struc= t klp_func *func) func->old_sympos ? func->old_sympos : 1); } =20 -static int klp_write_object_relocs(struct klp_patch *patch, - struct klp_object *obj, - bool apply) -{ - int i, ret; - struct klp_modinfo *info =3D patch->mod->klp_info; - - for (i =3D 1; i < info->hdr.e_shnum; i++) { - Elf_Shdr *sec =3D info->sechdrs + i; - - if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) - continue; - - ret =3D klp_write_section_relocs(patch->mod, info->sechdrs, - info->secstrings, - patch->mod->core_kallsyms.strtab, - info->symndx, i, obj->name, apply); - if (ret) - return ret; - } - - return 0; -} - -static int klp_apply_object_relocs(struct klp_patch *patch, - struct klp_object *obj) -{ - return klp_write_object_relocs(patch, obj, true); -} - -static void klp_clear_object_relocs(struct klp_patch *patch, - struct klp_object *obj) -{ - klp_write_object_relocs(patch, obj, false); -} - /* 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) --=20 2.55.0 From nobody Fri Sep 25 21:40:25 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 66CD952FE51 for ; Tue, 8 Sep 2026 12:04:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869072; cv=none; b=Bdx+iKkrQeqx7AJWD00DO1zP1o1hPcu6Tes7m1lD5yDYaztkCTkU8B1ux1pAywQoglHv2SSxWQUw6iFT3P/4qx9K4s5XeCAYVeiE7FbY+XzgKPh7muuY7emxYPy6EOiKCDZ1vAhErMja7FeYDoDhAG540jAT27tm+h/0Lh9Jnos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869072; c=relaxed/simple; bh=+Z7gOrM6+d068hznh0NTfalE7EDajgg3rW0Apn+UpFg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EwgboX4vYvhI5WXbB/zigWfUxvSgb70/reSb0L2R1aPvygTNMN4dJ8X6iBNlOphvkFBXQS+EN1MxbnkIzReazCPN/ORrfEZJlcD/BgO7EPMjf5Ld0RM/wd0UG768LwgFfDw2Uro43822VZbbEoHP02RDonFPrf3FrxDjFfPM+j8= 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=YyaEjzf1; arc=none smtp.client-ip=209.85.128.47 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="YyaEjzf1" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-49b965570d7so52249175e9.0 for ; Tue, 08 Sep 2026 05:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1788869064; x=1789473864; 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=nHaXAOzQB22k62cPhYcK673Kb5UPCqNoZZMm/VKn/iQ=; b=YyaEjzf16i4PlC+BaBx/k5vRZJgD+Ohlt6dTH8WEM4LK3N9kDbBrgtyan/BsX9EeFM qMw4TOnEWcZq9zM0Q8xQbqYXT35GvN+BxMK4cnbqixfeGZbpAAh8xRYDvG5gXOJR/Sw6 R9wgDuwPnrxf0FSHPJqHICwA0IUFWnwxb1XrOWXkzGZkYLE4rsGVtbpQoR5u2Kan0q3z pw1SkdUOEz5gVnxQMA2O/tqScW3AJFP+S48FmOJGGGVXAqUTZjH0bekkYE+1sq1mZpxK S9EEiwS5GrA8WBOlCq101d6QqbKk4aErbmASTIKQkHsAeElgwEINTwJtlXuvS+S0jmsx 0FcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788869064; x=1789473864; 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=nHaXAOzQB22k62cPhYcK673Kb5UPCqNoZZMm/VKn/iQ=; b=sxvhm+lhpMBPZtCRtbJcjIRJTy3YpaYc9udkgUIwkRjHf7ySDn/RQFAL4owV6eZWCm Npln7w6XMy6cdD/LmVpH+zH0bcHCV2KwmgOEPdmT8Xs3umd46TKk5oA9c9SKRbKIbnkw oyVarzyEAa0aSSWY1W/XxhsqyQqaHy53eLWtoUxvJ8DToncAUzp12LFxB5xnk13kg9zy IrpZlPrWyJrSGCu7vUEueROtqdAX9j+wqqrbH8TLF8rYwvaHLpxXdKRr2gxEAvH6KQol 0zrhIWTcAeVaJG8dHaj+J0rOuUpYr2FNcGHMURFUzcz32skv/g+Izc6wYNUmd6k2r1Ma Cqng== X-Forwarded-Encrypted: i=1; AKwUvBwLKjN+jNfu/mizpIk80wstl4IL1kCYo60gmKplCSOP2KNZjsRB0Si2HV9d4uGapOPAUWrrAbMtYmDhzls=@vger.kernel.org X-Gm-Message-State: AFuF++moZ7fojQykxduG7RoJZDKH9Qoz/gICH77IJFf8kHwvASwSCJq0 wC7beFi+InnkBAE50yTHC9ztsc4qVAW0ezSLNqvWnLlA1bPGqDH+wGjw6E3eJSKobqQ= X-Gm-Gg: AYBFou2ZDhePgCa+1pnV2p2YhASTdwLofnVX1acjie5FW26Oax+lsF0XyThIRIQ8Nle d8ChAcATaPV6ZXC9eyJ2eMkuKIsOs4UJjzNzbd1vRnZ2pjB/Kuf+lNYSMXia2G472DlTUQVJRsc 5kVSpWUKB/OdZqFMLsNhIIx4nkrLXq8e6LPQScz9WlBnZveQiB88IfCpn8SCvA5ujSBepevWJ+g G3cIUuwgcDxFckxIXasvBwWFVmditBgsKORSlqTGJOyCPUWBnW5isvvJv9PnPS8pCMm0ysdWBCq pNpereT9OyZXEhV69EbelxNsaPSPv8hy905bRdfY7JlpoxoK5/qUwvko8WS2K+aV07zWMW+W8V2 AfqNmGR1UWvp4Uwn4DY/fyj2MCbHmnz0ZF2PAGNXcQs00JceLrKDjxXpt9zHsd1qLIlgPFmubcl drRQLO5YLPj2GSzK+JgoOb+c3VZNtfXQHNyMY8Enexum5YCO0x8wg= X-Received: by 2002:a05:600c:1914:b0:49c:ff23:a6d5 with SMTP id 5b1f17b1804b1-49cff23a70bmr243819815e9.0.1788869064334; Tue, 08 Sep 2026 05:04:24 -0700 (PDT) Received: from pathway ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49cf755c22esm390099035e9.0.2026.09.08.05.04.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 05:04:24 -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, Petr Mladek , sashiko-bot@kernel.org Subject: [PATCH v4 4/5] livepatch: Clear relocations when klp_apply_object_relocs() fails Date: Tue, 8 Sep 2026 14:03:24 +0200 Message-ID: <20260908120325.299649-5-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: quoted-printable Content-Type: text/plain; charset="utf-8" When a module is loaded, klp_module_coming() updates all enabled livepatches. If an error occurs, it delegates cleanup to klp_cleanup_module_patches_limited(). However, this cleanup loop skips the partially updated patch, leaving any changes made prior to failure unreverted. One unhandled failure path occurs inside klp_apply_object_relocs(). On architectures like x86_64, apply_relocate_add() performs a verification step using memcmp() to check that memory contains the expected relocated or zeroed value. If relocations left behind by a failed patch are not cleared, subsequent patch operations or reloads can fail this validation. Introduce klp_write_object_relocs_limited() to unwind and clear only the relocations that were successfully applied before klp_write_object_relocs() encountered an error. There is no need to clear relocations for other objects in the failing patch because klp_module_coming() operates strictly on the specific module being loaded. Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/r/20260830175608.4BABB1F000E9@smtp.kernel.o= rg Signed-off-by: Petr Mladek --- kernel/livepatch/core.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index a6796cd6b65f..714f97fdd271 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -342,14 +342,17 @@ int klp_apply_section_relocs(struct module *pmod, Elf= _Shdr *sechdrs, secndx, objname, true); } =20 -static int klp_write_object_relocs(struct klp_patch *patch, - struct klp_object *obj, - bool apply) +static int klp_write_object_relocs_limited(struct klp_patch *patch, + struct klp_object *obj, + bool apply, int limit) { int i, ret; struct klp_modinfo *info =3D patch->mod->klp_info; =20 - for (i =3D 1; i < info->hdr.e_shnum; i++) { + if (!limit || limit > info->hdr.e_shnum) + limit =3D info->hdr.e_shnum; + + for (i =3D 1; i < limit; i++) { Elf_Shdr *sec =3D info->sechdrs + i; =20 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) @@ -359,13 +362,23 @@ static int klp_write_object_relocs(struct klp_patch *= patch, info->secstrings, patch->mod->core_kallsyms.strtab, info->symndx, i, obj->name, apply); - if (ret) + if (ret) { + if (apply) + klp_write_object_relocs_limited(patch, obj, false, i); return ret; + } } =20 return 0; } =20 +static int klp_write_object_relocs(struct klp_patch *patch, + struct klp_object *obj, + bool apply) +{ + return klp_write_object_relocs_limited(patch, obj, apply, 0); +} + static int klp_apply_object_relocs(struct klp_patch *patch, struct klp_object *obj) { --=20 2.55.0 From nobody Fri Sep 25 21:40:25 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 140825237BD for ; Tue, 8 Sep 2026 12:04:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869085; cv=none; b=oJ0w+f53FgX270C8i1W9mwwIzf3zWlM1Rsn6BHPGz3waW0EWFgnR/xuzJaTKJ8J38hdSKH20kMwREdMVNFcvlsSnk/Pk3Kv+Uxf9E+LUOZ1LE9SwPbEnx9+70lyRO4/i6Km035Y3gbcbAeL5eX3spzQvlHzuuo/mqeEoC9wGYQg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788869085; c=relaxed/simple; bh=DVuuPUwXLwtT0Ymk60JQVhNGfMYWdFufX5xFsm2SN0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hxhT9GEKmdUUPjMqThyRtRnX4H7kIScc6JvGp0U/k2x/JVQ4RUe0YuT2j4HjIEJNKC75d16oyEHam5I8ErJgyvwz/Jb9atsZoBfCMZzLOcguzYpa91Gc6Rz0dbok+GTvWNtAxYz9LLT+VTFFeQ+DgG8Qd37vPI2qRkUDH82J7P4= 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=AE4oPbrB; arc=none smtp.client-ip=209.85.128.45 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="AE4oPbrB" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-49d0d2d37ceso13985035e9.0 for ; Tue, 08 Sep 2026 05:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1788869078; x=1789473878; 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=FWdRM8KWaj2QeHMO2XI9aeMbB/7rTG7L5o5/mygY1k4=; b=AE4oPbrBXiKJIGNuH5An6h8EXwLlG/v3FDNKEj55VhC7ysk+7ZEJqfOqjoXXHN5YmF fo0Vpxb7sTdPzjwxttrscfAp1Yz8L7L9BRyaz9WctV1r1u00meHO4OJZ34/+NEBl080k CBYaDmwESAQlBDNINM7DDBwVJvq4VOa+p4FkNoihjf/KZI2KeTMXVH9NUKRzAxMFpEg6 in1hSy8hfGpK/YweQJe1IPkUBdCMhMwwmEvRHw9WEMK+eRqmDkGyMiMptuwO+c4lrrry qdDCFO8/jUQSbRk1FgKIFFpTByhMI6/JQpTG9fv73GyAb9p4Q4mph5rBe+gA0cMNaVa4 HLuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788869078; x=1789473878; 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=FWdRM8KWaj2QeHMO2XI9aeMbB/7rTG7L5o5/mygY1k4=; b=Olp4/6QKxEA6hAJxiNiD+us3daqY5mWL8bGHKxPMX2zPOsNTbz5oaQruOfYDnHAbNt wbQnN+gHb88mWHywmxfpApXLPUo0czlbN6VIUSEWzVyTfq1jfYoKYGnZQTTe8Icf+Peu JBACtwtE201u4rdSUJbAxRMyjZjBuEeKQNLDg0ZZIffQJsqYlWe1asyC+xB3BjyFhnVj ezObaMLR9mrP8rXBZh+wmFeOtu6I3h3xTujIQ7hareLot6Aokp5lNqgqsTIoLA6htN6/ oGmKx1PvM9fhZq0WwNmvsM0MI0gdX5kH+M0H5/FlQJypqfeFCGnK6RfYXXtUstjMbgYR DKdA== X-Forwarded-Encrypted: i=1; AKwUvBw3Bf5oN99S2KWDK1VWMAnmA97+S9hJ7Xj9z1BDTN8jMTJBSjrrFXsgC3iRGxWYqO4q1moPiTZTnYklfUc=@vger.kernel.org X-Gm-Message-State: AFuF++kLhOdI7iPMp3VIhiEwqcTd6Qucbjy21wWzCXrg4uLNCCqq7Nez xDjoXuGjpAAjBahl9+FjgdKjQFPKlM9sKXWBEN97iCP7UfKMc/qsq/hM3EqmssefYbQ= X-Gm-Gg: AYBFou3fMVmPkEX57adVQieH5YiZGqVuuBSGSL3WMv/P/SrDhV16jPwst3Bz8BMIPVY hRX8muUsmg1aRPT7zxllSWYqzRMokCAM6v9mKuoLaTC/POcooAOmlZ96ok88Ezy1xZbAR5NSmVW K/2c6x30BjHQ0+AHI4WRdoJjf4QyRuWhtqk5CK9eNS2SQwHeWmuLhd0t+ZhnUpcagzmG8xB8Vn8 ePo9bLf8fWjeAmrOcAexH6/p9U1uOPqcv2xCS3f7B6DBJCoZ6GMcUyb7fEULSnyBQELZI3DTWFs gJqMLfpRzaqsgtcmi4DUAG5Tq7XUrSDat61nVeOUGEzkARW6tqHb14+DlxIWX0TE8EqWw7r25uj RZsbMRHnbv+h/h6hTKep3OJYqXCxv2upuT5i4NgoXIivKYyPpJipemrmE3UwN1KWdqpEt6asEDG d5p1UrtAnFo0Ci/EZrdcdj7KuzPg3rC80xDGF3n9IAf60ssGzEogA= X-Received: by 2002:a05:600c:3b01:b0:49c:fa21:e73b with SMTP id 5b1f17b1804b1-49cfa21e86cmr239776125e9.17.1788869078416; Tue, 08 Sep 2026 05:04:38 -0700 (PDT) Received: from pathway ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-485885b7bfasm36371435f8f.29.2026.09.08.05.04.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 05:04:36 -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, Petr Mladek , sashiko-bot@kernel.org Subject: [PATCH v4 5/5] livepatch: Clean up klp_init_object_loaded() when fails Date: Tue, 8 Sep 2026 14:03:25 +0200 Message-ID: <20260908120325.299649-6-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: quoted-printable Content-Type: text/plain; charset="utf-8" When loading a module, klp_module_coming() updates all enabled patches. If an error occurs, klp_cleanup_module_patches_limited() cleans up fully processed patches, but skips the patch that failed midway. The current code is a bit messy. The changes done by klp_init_object_loaded() should get cleared by klp_free_object_loaded(). But this function also clears obj->mod which is set by klp_module_coming(). And relocations are cleared separately. Fix the situations by updating klp_free_object_loaded(). It should revert all and only changes made by klp_init_object_loaded(). This requires some shuffling: + Clear obj->mod explicitly in klp_cleanup_module_patches_limited() and do not rely on klp_free_object_loaded(). + Clear relocations in klp_free_object_loaded(). Remove the explicit call from klp_cleanup_module_patches_limited(). This requires adding the @patch parameter. Next, klp_init_object_loaded() has to clear its own changes on failure. It just returns an error when relocations failed because they clear their own mess. It could call klp_free_object_loaded() in other situations because all relocations were done and other values are just cleared. Finally, in klp_module_coming(), avoid code duplication by goto targets. There is no need to clear relocations for other objects in the failing patch because klp_module_coming() operates strictly on the specific module being loaded. Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/r/20260823062313.1321B1F000E9@smtp.kernel.o= rg Closes: https://lore.kernel.org/r/20260830175608.4BABB1F000E9@smtp.kernel.o= rg Acked-by: Song Liu Signed-off-by: Petr Mladek Acked-by: Miroslav Benes --- kernel/livepatch/core.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 714f97fdd271..87b2331486d4 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -738,18 +738,20 @@ static void __klp_free_funcs(struct klp_object *obj, = bool nops_only) } =20 /* Clean up when a patched object is unloaded */ -static void klp_free_object_loaded(struct klp_object *obj) +static void klp_free_object_loaded(struct klp_patch *patch, + struct klp_object *obj) { struct klp_func *func; =20 - obj->mod =3D NULL; - klp_for_each_func(obj, func) { func->old_func =3D NULL; =20 if (func->nop) func->new_func =3D NULL; } + + if (klp_is_module(obj)) + klp_clear_object_relocs(patch, obj); } =20 static void __klp_free_objects(struct klp_patch *patch, bool nops_only) @@ -896,7 +898,7 @@ static int klp_init_object_loaded(struct klp_patch *pat= ch, func->old_sympos, (unsigned long *)&func->old_func); if (ret) - return ret; + goto err; =20 /* * Aliased symbols share one address, so they would resolve to @@ -909,7 +911,8 @@ static int klp_init_object_loaded(struct klp_patch *pat= ch, if (prev_func->old_func =3D=3D func->old_func) { pr_err("'%s' and '%s' resolve to the same address, aliased symbols are= not supported\n", prev_func->old_name, func->old_name); - return -EINVAL; + ret =3D -EINVAL; + goto err; } } =20 @@ -918,7 +921,8 @@ static int klp_init_object_loaded(struct klp_patch *pat= ch, if (!ret) { pr_err("kallsyms size lookup failed for '%s'\n", func->old_name); - return -ENOENT; + ret =3D -ENOENT; + goto err; } =20 if (func->nop) @@ -929,11 +933,17 @@ static int klp_init_object_loaded(struct klp_patch *p= atch, if (!ret) { pr_err("kallsyms size lookup failed for '%s' replacement\n", func->old_name); - return -ENOENT; + ret =3D -ENOENT; + goto err; } } =20 return 0; + +err: + klp_free_object_loaded(patch, obj); + + return ret; } =20 static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) @@ -1287,8 +1297,8 @@ static void klp_cleanup_module_patches_limited(struct= module *mod, klp_unpatch_object(obj); =20 klp_post_unpatch_callback(obj); - klp_clear_object_relocs(patch, obj); - klp_free_object_loaded(obj); + klp_free_object_loaded(patch, obj); + obj->mod =3D NULL; break; } } @@ -1337,7 +1347,7 @@ int klp_module_coming(struct module *mod) if (ret) { pr_warn("pre-patch callback failed for object '%s'\n", obj->name); - goto err; + goto err_free_object; } =20 ret =3D klp_patch_object(obj); @@ -1345,8 +1355,7 @@ int klp_module_coming(struct module *mod) pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", patch->mod->name, obj->mod->name, ret); =20 - klp_post_unpatch_callback(obj); - goto err; + goto err_unpatch_callback; } =20 if (patch !=3D klp_transition_patch) @@ -1360,6 +1369,10 @@ int klp_module_coming(struct module *mod) =20 return 0; =20 +err_unpatch_callback: + klp_post_unpatch_callback(obj); +err_free_object: + klp_free_object_loaded(patch, obj); err: /* * If a patch is unsuccessfully applied, return --=20 2.55.0