From nobody Sat Sep 26 20:27:58 2026 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (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 E4A4A3AFAE1 for ; Sun, 30 Aug 2026 17:33:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788111236; cv=none; b=QpbsIJq9nTFCCRXjqvpEFlsbD3fVG2qNj8LC1qHxPAIeqe4wyry4jhU5+HbeMiI2qSHTeEHv8hRErIj8TDR0WFM0xfe2HetrIZW8kuBD8cHzXe07nT5RIv+i7aR3NxgGkY5Ck0FYWMFwYd/tCZcBPUHJHZbfrKo7A5vm3RZQI44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788111236; c=relaxed/simple; bh=9+d50aJS4yrHmVYywdbowmC008hb2YhVKmPkK2viC28=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XW14wQRsVKz0cT/WoBV/jubqtEJM+63ip8W7tb01G5qD+0MTmCe2GA76TB/MkicwToEek/vPZndIdfIUjy4SRfXftPPDaLlT7n7MHSPernNm24dTe/iYXVu4BQt/K8gglEwMgyu3hi3PReOH+nvu/vdS5JJNnY/RH7z6G43pY58= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=q2wtXCYr; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="q2wtXCYr" Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-cc1c8d4a959so1945599a12.3 for ; Sun, 30 Aug 2026 10:33:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788111233; x=1788716033; 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=Aae9jrrTwCiQuXGOwqRy4yR8oXohuqBow9xps7fLUpg=; b=q2wtXCYr4shP2l9wTaEKAuy9bwwOKR0fFDvJdswC2V6FzaonPv6/IxDL+4pjmOSpa5 eJGNf63SqmTAr8T/UV0QIbiK3Ucee4Xz0p2CRY5t3NGsNxcOs7bgDUay+CSb896DDfrN GyM7cStJnYAwPLivXkg7BOurezfCHL21GYePPVUKEphY/0ixe2osFHGWEZgifMaSWrvo 6a3ZavbGpVFB6jZ32s5tLBbg+1PTPhLZ4LiFYc3pJcB8qgqI2LcOe1JULvePL3GSPnoe S3hfv9WFyoLHQ+PQnwAPsHCSiKAUFs3W3Ef8dkCDVMxOtG5cLiZDydNR9Kz7iyTD7voN cAhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788111233; x=1788716033; 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=Aae9jrrTwCiQuXGOwqRy4yR8oXohuqBow9xps7fLUpg=; b=a2sbSeBgB4SD4h+MFFoQBE8NvlP3zAjF5BG3UlbZcl7pMlq/f7c8/RI175Z3Cz+joY wiVLvBWdz+3JCyx6KdyV0AH892FCffZ2UeGSTKaNClyqIXjxx+CrU61D0CJQqrc3jXW6 oVjFjcnQt1ZQjq41epkrGhHjq783N3i9uRbTbbyGv7dWpvaUyfn9hXBGz8lR56FInEn5 1nU6K9hmX8Wc1B9rfrWhGKHGdEuWMxFoo4WYh8Zk9R/1xbcniuOdnEVRo5FBImIdGp45 kymakaSgksEVK1G9ErU44JPAUuV4G1I5rTJCfqAngaUF6HDKvZDwWCRImqMDCEU3bPcM /yzQ== X-Forwarded-Encrypted: i=1; AKwUvBwxPTKWFDih6JrMfl2bCX+WWc0TuktWk3Lz3/CNdUfWaODeqYgdMTAwQ7c3krBePjdJl07HAgVYWY9XsYQ=@vger.kernel.org X-Gm-Message-State: AFuF++mXkyk7n2nRLVDTYxCJh6+nr7zOmo1aT4cQW8Y1F7ZVMIVXKNul NREW6bfb2rX90wiwHi4ecea9TpUeFlHCU9AsdlbHJH1bbXzCzNA3u9Oj X-Gm-Gg: AYBFou0HqTz82rV6113jRR6qRaOSxF6yI94ZHOLgoJHuODUMJqp+QpnV21WcuQlXQjq cC2SMqG1ci4vT56rfFaP2XtiZ9nDEgJQxqQG/bbRUVuyV/PYlIA6P3eWqZIZSrRvHruaKmJz/FV Gfg0KwCPH+8ArU22BxzR80Cqu3YfEtx/iHSXZfy/U0iTxlosh/1Cq0Hb3bLgN6y+3cO4D1vmW22 0mWeFL4V+GutRocTp1wOM9GPnYoYMs7Uv7EazV4w9omTwP7SOuNyonTIo/z2zuoovROsXBERmKx FqRqKFlQbx4Lcr7cGZfRuaUHk95xpebZr6ScIRPs+zl4y7UsDp4GY4Aa0U3o1Nj79xq5T7JrQ4Z nOyhhf0FvNjCLkHBR/ou2FU44Oygs0U+nuyTD+7MwiFhvVxwLE0WsNApr50t3VxeSXoU9tQeoZx LDEMXfaef95A8sjwSOHoaRPn5sh+WlTg67LB6hNzkE3jqlWmisE8x7ZevM7GzXJ0IL3KGFnvwK+ Kf4m5Nn0cYryb8sJl1e X-Received: by 2002:a17:90a:b8f:b0:398:d292:e6d5 with SMTP id 98e67ed59e1d1-398d292ea0emr4173955a91.24.1788111233151; Sun, 30 Aug 2026 10:33:53 -0700 (PDT) Received: from localhost.localdomain ([2407:4d00:6c05:13e8:a91f:d3ab:5c4:f6ed]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-396b186809dsm17237749a91.10.2026.08.30.10.33.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 30 Aug 2026 10:33:52 -0700 (PDT) From: Harry Hsu To: pmladek@suse.com Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, joe.lawrence@redhat.com, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Harry Hsu Subject: [PATCH 1/3] livepatch: Fail object initialization on duplicate patched function Date: Mon, 31 Aug 2026 01:33:06 +0800 Message-ID: <20260830173343.52759-2-x90613@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260830173343.52759-1-x90613@gmail.com> References: <20260830173343.52759-1-x90613@gmail.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" 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 Reviewed-by: Petr Mladek Tested-by: Petr Mladek --- 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 28d15ba58a26..0dd8cda5c9b8 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -866,7 +866,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)) { @@ -888,6 +888,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.43.0 From nobody Sat Sep 26 20:27:58 2026 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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 290292DB7BE for ; Sun, 30 Aug 2026 17:33:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788111241; cv=none; b=uxUS5M1MEfjBh4rjaS+fLLZpQ2PNuxaQxwI9l4WgIUMb2CE7dq8ebFuFmR1lUNRbWKX8W16LMvkaJuhYRBpOYZSOCXiwDP+tX7r4KoRPRVb6ryhJASrLW0nu6M6yCzX+Vl181ppNDtPWAanIRkfyKIiOET66+6C1Mjgg3KTkAz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788111241; c=relaxed/simple; bh=JQtLJ4B4xGi9RbJQfiBzfWbJpI1166z6juWdKP7ddh0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iv8W/6NISFqSLoTUiRo5fEjhkXi7ESx8MtSWrZcfzx08pXQfrvzw8oi334y0xDUmRRF9VylXi26n/s0ThUUyj2dJuPmpNjd5UEqtsSxvi4wNCCdpRxLf33yTIphibPxSO9/36jukk5g40hltMSY2LmnCBvy5NpoXwrGMBl8JoVo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YqbD3NIv; arc=none smtp.client-ip=209.85.215.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YqbD3NIv" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-c9e607d81fcso1711093a12.2 for ; Sun, 30 Aug 2026 10:33:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788111239; x=1788716039; 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=4KElhGwb7qyJUiQrdOeMIsu3TKGiuI6DnUOH7u1OvrE=; b=YqbD3NIvXvoA3UKeI7aXgjvdt0NkrUObpTm1cmIa04Bz/VeE05EHSLA1N6K3+1gfYH t8I4izSscfj+ZIvbH++2nUC4yLTVPTm5qyTm8Y9q2S99fT31uzN+FdFhMKoYk/HOpH0B ZQK9tnixQzH9seK6yNxeNXdc4R3YI62GqRYBKmVq1nbMkWQXM9GuG1fzAHw0lbFEW6EX Wghuj5XzxcKf5R/vPlycXX91AxYDoJoryudLgSOpVGnXTea4L7+qxLMrWPiM3WXBBqq0 H1cuRRmxN+yleALWQPIz3kk4lo07o6Pg2npMGovwW2lxbeECny8VYqVgi+zZKBT/E+dp Zgcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788111239; x=1788716039; 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=4KElhGwb7qyJUiQrdOeMIsu3TKGiuI6DnUOH7u1OvrE=; b=ZQWLBzl9uKwSJj0J56ygMJkJWS3mXaDB15bsR3QZlhhQ0YUSCUFhqKoGqnV6umV7ZM 2dEQfdcPvZuOUwqkRp2b7Jk5BFbJCSQktItTF6EA6SBEN5pYwY1Sse3y/iOn+mbS4gDe OA9uf9IjQJpjBMUEKJ1GUw1rugztH/2TH+WajpS7qBPm9gl64nKoyMvr7jApJcT5BQSe r26pp2QWOZ4swQpbVLSueJ55+7tY7Lrz8Xl9tTR5yFohChbUgaPdVqoOMwNgYtsgvIO3 MySueatQ3THAa/qPNOLHwXYTk01fo0i7vTScWKZeFGMqXOJjNUR/2JQQHQGfJXJFA7QY Cdcw== X-Forwarded-Encrypted: i=1; AKwUvBydg0tx8hNn573pmfbcP6Z0TVI2dIF+B3ksZdzcUwOq/9relpiaKFZ0x89U15jALfgwK3fExBdmbxKsOoY=@vger.kernel.org X-Gm-Message-State: AFuF++nNKOu8d+AmPdRifpVAXrTQGR2KVuahQMMskjHuEpy7UzM0nOck eKyNr7p82cuXTBZqRvRwY/C2NfLX5IFKflAtBxsGH1X0MTVKxMsLIkMX X-Gm-Gg: AYBFou0RIqupu9bjhQRVKyOjXYHgMxTSCS6AlU9HGc8uI1HIAVrIhbnawmlDeizzR+w QFf6W5DIAIB76leSE8F00N9qG5mogRgVZZ22O00gRCGDz80+dIGnHSCO3IvPR7DqQArN+71xJt8 icSLVgP8wsxJv8fbxM5TI0kKklWJP467z7rp0It5up2Bj1OFejThvjKdh5YIiu+n2ysFBuOH7oQ O8yAXnin0A1xgNveBoltnZTXk0vWxIEKYSoJ39qAuoVhsL09YcgW7GVbNlAc/VNb4713cBlOEl1 pRrNeQQnnySpevm1LnS6RrN56u+KcD3sRr9t4LcRq7zYhByXKeQojdhzRcWpKpeQ76Rm43s8zOS RRRKSh7QNMmBSfoCjtckqRywdhZrboYHei2frkhoYOjdfqn7CrCveCEZV3IpLJwdR0wIaFPNhvk Ky0x4NFdjfY4LSvF8k4s0HgIPlCiqBaHd1J+3RSUwgr+kX80JV02F2LQT1nzmFbDs6FOx/zTz8U L/j1Cx4pB/vBlHRfh0rLmgP4bNdpLw= X-Received: by 2002:a17:90b:1d83:b0:38e:2e86:ed02 with SMTP id 98e67ed59e1d1-396d1023217mr27801427a91.14.1788111239411; Sun, 30 Aug 2026 10:33:59 -0700 (PDT) Received: from localhost.localdomain ([2407:4d00:6c05:13e8:a91f:d3ab:5c4:f6ed]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-396b186809dsm17237749a91.10.2026.08.30.10.33.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 30 Aug 2026 10:33:59 -0700 (PDT) From: Harry Hsu To: pmladek@suse.com Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, joe.lawrence@redhat.com, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Harry Hsu Subject: [PATCH 2/3] livepatch: Move code for updating livepatch object relocations Date: Mon, 31 Aug 2026 01:33:07 +0800 Message-ID: <20260830173343.52759-3-x90613@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260830173343.52759-1-x90613@gmail.com> References: <20260830173343.52759-1-x90613@gmail.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: Petr Mladek 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 --- 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 0dd8cda5c9b8..aa895e8259b0 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 * @@ -826,42 +862,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.43.0 From nobody Sat Sep 26 20:27:58 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 3F7173CCFB0 for ; Sun, 30 Aug 2026 17:34:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788111244; cv=none; b=uOZFp0jNkBKIHvumtTYiyRv+OX7TG97YvnT3/IQGZ3NKY36y4XPTp8nLewYGBiy9YK/ymOPWHVW7fvKmARPPLCw601aeJgwAcZQoQ31l9SCkS+siNaq1uLdMTO7ZFMFdvHQqpz38p43xCjD4sMmnbFVbTygZRWuAs8bL2XqagM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788111244; c=relaxed/simple; bh=350E+24cgIQPrkNeuQvH0zUzV5ibQBMEBZPP3eO8cUY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kujsl+2Y8HC54tNhq/ZEoKwOijlhf1l9UXwT6u/6u2ccIMQpcbXF6f8AZWSCgXJ9PcDTJT52qmZmFtJY7oNQqWUyo9IcWa2STRIw/paJKra6zXgyoA1Iy1A3FVJMasbEvyQRZIHstSzUSOKOuQn5gIEfSaze0RlRHVQ+khm/V2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=megOhzs1; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="megOhzs1" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-398b3d66515so1120565a91.0 for ; Sun, 30 Aug 2026 10:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788111242; x=1788716042; 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=ns3vlUtF6VYFQyrrstAS/BpbWDETkTxgnU62R5+4dgY=; b=megOhzs1jqFkuowHlEtJod5YHZ0uGjw31R+j4yx5IbMDZxc6fuSJqMOES2pb4FR7rg I44IoD+UX1VSKdx1lJNQMs5zZkzRK0yPB/KkC+EsdIk4t9Dm+2wQf8AM/sjBMAmHYirC dRESoGIb8/juTskpFbQ92xWvReUPU23RqXnjYygwZoBeabijX9b+uSqKoGjJnW+Jm9Dq Crlp4VCj0UalE6/l+n52zSnAjfXYfisWO6/Xon3JQ3Op/G0p/v8VJW6OnqttHfmT0dG0 6FSKLKGcxXFpxY8JuZ9i8F7YFicyvnvRR9eIUuHyq8rDq2JEt1R4BFrHfEp9T4i/NyEe MmuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788111242; x=1788716042; 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=ns3vlUtF6VYFQyrrstAS/BpbWDETkTxgnU62R5+4dgY=; b=Ihdk3fwSm7qogEyYFnZkJVFGNk5gNTT9y/SAfQRcCDwldtzpl8VLjAPIeNxEiJzutn 9i9e29bbMqgN02y82h4n2DCbBpZsAPTWHP7rEWWW2fFWoiIc7zQlAKpe39sF1gSE1idV d2YsTt9x18VWmHMPAfnltn6CuSB+G3dq6ZYeKDw11XRMEpzElB2z/rAWHPKHrbfiyoc4 YAtNKZd+g23nlHoJAYm2lGAoZrrcTQ8o/aZaeJa8wqDmT+6rdBE3vR7jnJzKNMR+zrDB w/EhKLA9ZA5aPm31vmWVbBEJ0suxpdblmx5qfD+qOFJbW2U0qVs61kz6/671LBeIiAV6 Yg/A== X-Forwarded-Encrypted: i=1; AKwUvBygY0ZsGBmVZT8zXUZ3ooHLVZ3+DogN8nhJy8lo4UIpMlZqDoW9mHFDI5A2/MJDXTEO4LutsSqg/eGMwUw=@vger.kernel.org X-Gm-Message-State: AFuF++lR7Foe4gji1iQsp7qfuWQ38NNayuSeUPwIWvn2k6pNZij8nMht 2Hg9SAxeI4NrfkO70EYc9YpftzxcKeCYsdgvVtDZFar1zk+lZDHSCqoy X-Gm-Gg: AYBFou0j0oZ5ZlijRsQeLqDq1XvyvbSenGyWLlUrsvEPPCHoUO5QXTH40LY+Navia9J vjRXbse2rLLkufudukmcrwLuwIlaDb/APSWegghifi1thQPro58dvXBNsKm18rdDKLx9A8uoM7u nWOA0sYGLuPp9Sxc63fOEUj5/kiplL93F3gWFfiyx/Ea/z8NFBfdaeUj7c1W1QLLebOz9L+8Ou8 BLxbHAJd56cH/zoKwQAHwhqt10x6k45Lcrt7SSfPENGAIsCWNdjxBO3eckKHnnFE2Fctxlim50N rXJ2KsVKNAAT/k0xSrxnmUB+wJo6fIKryr3TpNJ15NNGAIrj6zdCv2MSNhIgVN6dTtpfp2Iv2QG J9CKOwbNmbLr3UEAOp1Y4/GovIQksSdDbgruSlrlcXtbkkleDvsJYTHkw+MPgm3cjs5KDpzWUzs y2rW3mSN3QZhCHUDMQLDNWwoc0tYiQZcHDTJrxQE/lShRyUTB8kWyonscyrbXN2KeATxCbHpWXr vxAWAI99vujZLYzD5Vkzg== X-Received: by 2002:a17:90b:534b:b0:398:ceef:edbd with SMTP id 98e67ed59e1d1-398ceefee8emr6899772a91.18.1788111242577; Sun, 30 Aug 2026 10:34:02 -0700 (PDT) Received: from localhost.localdomain ([2407:4d00:6c05:13e8:a91f:d3ab:5c4:f6ed]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-396b186809dsm17237749a91.10.2026.08.30.10.34.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 30 Aug 2026 10:34:02 -0700 (PDT) From: Harry Hsu To: pmladek@suse.com Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, joe.lawrence@redhat.com, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Harry Hsu , sashiko-bot@kernel.org Subject: [PATCH 3/3] livepatch: Clean up klp_init_object_loaded() when fails Date: Mon, 31 Aug 2026 01:33:08 +0800 Message-ID: <20260830173343.52759-4-x90613@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260830173343.52759-1-x90613@gmail.com> References: <20260830173343.52759-1-x90613@gmail.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: Petr Mladek When a module is loaded, klp_module_coming() iterates over patches and calls klp_init_object_loaded(). If initialization fails, it delegates cleanup to klp_cleanup_module_patches_limited(). However, the cleanup loop skips the failing patch. Each function called in klp_init_object_loaded() is supposed to clean its own changes. This works except for the changes done by klp_init_object_loaded(). 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. Finally, call klp_free_object_loaded() in the error path in klp_init_object_loaded(). Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/r/20260823062313.1321B1F000E9@smtp.kernel.o= rg Signed-off-by: Petr Mladek --- kernel/livepatch/core.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index aa895e8259b0..4fff00a40d88 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -725,18 +725,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) @@ -878,7 +880,7 @@ static int klp_init_object_loaded(struct klp_patch *pat= ch, */ ret =3D klp_apply_object_relocs(patch, obj); if (ret) - return ret; + goto err; } =20 klp_for_each_func(obj, func) { @@ -886,7 +888,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 @@ -899,7 +901,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 @@ -908,7 +911,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) @@ -919,11 +923,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) @@ -1264,8 +1274,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; } } --=20 2.43.0 From nobody Sat Sep 26 20:27:58 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 B7CF830F543 for ; Sat, 5 Sep 2026 15:55:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788623722; cv=none; b=PskqBP2uTiE0eybFdJQ8NFqy5+raIuq336PNtP/gwaNkHdmkQAzPWq79yy+/AL3pF0duKWc5wtoe/ln33JlCTRmrO2NlUyzAC/YiBnTS0jscTT6W4ONiVkWt7UnmeD02Sai3O5SPjhvuEys1S7u3bSaCHn2qsSZXyVZ6qE4iNLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788623722; c=relaxed/simple; bh=pV+5IwkMLFn8Fx7djk9Yp6iUxrqGZmoFgCFV9E+GkBY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oPou65d/yqvkrqlG2gmRMMtPdrE7ZeTbfjEdQkQxwUpG0mMe9sosGukBjhe3WJcqeExmhIZBgl/8Fsso8PDka9JEF/QeYto0i50dhGFpHBNGbFDToPqyFnFXL8VL4C9RHZmIprLCipZeYe+/hbE02cZl9WPI4KmXrldkK2KMY+w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PFeq9kiS; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PFeq9kiS" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-85377c8bc96so1860791b3a.3 for ; Sat, 05 Sep 2026 08:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788623715; x=1789228515; 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=F2dEMJL1vhZK8124yTxwhBFPgoAwEv2z5aul9rIHN7w=; b=PFeq9kiSaw+J5qTIcXskZ+geHKAbNgPJFhwy4EtTIXOL08y1nJHI0u3njprFtSXEPK 8V2skKdyhBC2P3rtgvPMeBBTwt5oeGZcqKP835z+gkEfV/QHtO18JjqF+p0dM7HVLhN4 +jyeRUSfqsuc520bNSKpEbjvYHhLwGXczS6uKSUcUaAZp/Q/pIvJ8YcuqDZFmt+0DJOV 8dQICAtYVeeKfo983hbZ4ND0lcVpjQWNRjKwnKKDrWTco2lKLnZx+86VuaEOTXDIL1/4 f2TuZoKClrZRE96fzAZURcmZ0JZ8HivYs1pjgscek5aDIjBjgK4APAtL4b5dOSc58MtM pA9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788623715; x=1789228515; 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=F2dEMJL1vhZK8124yTxwhBFPgoAwEv2z5aul9rIHN7w=; b=RvaM4Wi7RnkdPr4NreKYxL7slyDKR24qOJy9uPIiRetcuIyX1l/8uW+nND/nBxDlOb 8JASe1Nq1ADDeOsLfSNlrHXmZlVqtCWr+Vp27jDi/YA8DjRxaiSKzjtNj2ibsYbkYC7W OyEIoYbSDfr2qs7e2HRybYUv0ztr44wO26Dfom0sRl0OLe31TUy0ZVwpmWvSxeiYfqyn 3yJmISp3lDMtR9dHJBmWcfv/RRxZksy+zsdSiV4aBUA5ax6tW/d/FEgbyGm7u74WQ0hL w+6agF1m3FOUKamyzYv4B6/zODC+4UKwhle74FKICUtP3vSt0y8LlCUh4NfWLXVw4Cr6 s4OQ== X-Forwarded-Encrypted: i=1; AKwUvBwVbR9h1qLsWRG2JWkXvRF3xyjZotkZ77GoYPRAcRw8ndxBmYhh8RcCt/jDRTHJAY6yOxF9CniYW69ovvU=@vger.kernel.org X-Gm-Message-State: AFuF++lG8LZlg/4ek9A/KELNNGaDPJHp53g47DnK370NhSEU53XrOWN5 rqe1le1E7tOa15lLh0x42wmKz9OgfFTxvaZQDxzSXT6ZVlWD6RjO4rc7 X-Gm-Gg: AYBFou2RE/5d3UGpTXTxHyzbvltJ1Ww3aWaraJZX7kD2mSpxmiXDbdLzzuQvQYjaiQk kQtxfE8DPHxjn3+Ru0TKrk3Fs6mWJYOpMS+EjXnkJVm2Er17H0UAD/9rfoS1Qz65jvHNsNaDhmi nU7SNcLCL2QYq9pL2kZzdIQTFE6H74uestWkPQ6VeY1Eso1ZsQJoIsUj0y5qsbL1klejXzR5RnK Td8GvRbFtZNDzOfsPFxYRk3WL1z6i5vv+Wc7kN3binWO1CJH1DNfSRa77aEXzwpx1uLiely+sCt +U9jbYfeBPE3RYo6msI8ShsL/RbKpb1kpPuKJfHqFNylkRxsIQr85QpWFAwLTsjNPxRlAW4hWrA 8AGybnXE+M4+VBk9nfxb9szUBaAveMIxJKALDTCXknEfg/6SgnavEnQkEbbN+n3hM1gNZjpAn8B R3Avh/V5ebGB5B2SYeT1JMzndevfGdKd4/15RAsd9GYI5v9nogdqHGwMHo1lUBJjL5xOZivjeN3 AH3wldPVcJmJ4HDjoHMcrXXwMLLCiBr5k78BtThcrwsTRHus0zutwCpOpKhXkSo2/GXVYRqZw== X-Received: by 2002:a05:6a00:4c8f:b0:84f:5cd7:e3c6 with SMTP id d2e1a72fcca58-861662d4ce4mr18597533b3a.5.1788623715142; Sat, 05 Sep 2026 08:55:15 -0700 (PDT) Received: from lima-arm64-dev.hitronhub.home (180-176-144-38.dynamic.kbronet.com.tw. [180.176.144.38]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc455448d54sm2101674a12.17.2026.09.05.08.55.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Sep 2026 08:55:14 -0700 (PDT) From: Harry Hsu To: pmladek@suse.com, jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, joe.lawrence@redhat.com, shuah@kernel.org, song@kernel.org Cc: live-patching@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Harry Hsu Subject: [PATCH] selftests/livepatch: Test rejection of aliased symbols in one object Date: Sat, 5 Sep 2026 23:55:07 +0800 Message-ID: <20260905155507.273262-1-x90613@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260830173343.52759-1-x90613@gmail.com> References: <20260830173343.52759-1-x90613@gmail.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_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: Song Liu Reviewed-by: Petr Mladek Tested-by: Petr Mladek --- This is the selftest I promised in the v2 thread [1]. It applies on top of patch 1/3 of the series [2] and does not touch the rest of it. Petr, since you are going to post v4 of the whole patchset anyway, please feel free to fold this in as the last patch. Otherwise I am happy to resend it as a separate follow-up once the series lands -- whichever is less work for you. Tested on arm64 with CONFIG_LIVEPATCH=3Dy: # ./test-alias.sh TEST: livepatch of two aliased symbols in one object ... ok TEST: aliased symbols in a module coming after the livepatch ... ok [1] https://lore.kernel.org/all/CAPhsuW70RpkZ1ciioSjt6qkQePWyeic_L+98d0h-Ao= 3ze-TmkA@mail.gmail.com/ [2] https://lore.kernel.org/all/20260830173343.52759-1-x90613@gmail.com/ 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.43.0