Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2241848rdf; Mon, 6 Nov 2023 08:27:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IHcdzbIMkb2wzecAI8jCE9DYX3NAkH9a0pqImmoOocRn+HF+5CfKIkSJxkZ8HeiNOboVDJP X-Received: by 2002:a17:90a:d78a:b0:280:cd7b:1fa5 with SMTP id z10-20020a17090ad78a00b00280cd7b1fa5mr11573338pju.4.1699288059140; Mon, 06 Nov 2023 08:27:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699288059; cv=none; d=google.com; s=arc-20160816; b=V0qC8v2+JRySwFjR/sejQs0zyvKx4EyO4r3L+qZ/eqHylRRXTX1clUGYwKwn2trgTo Cngi9FaQlBbTsMC3WPAoXVmY0xxHvUUG0R/tRMvIzqBictmURwsTnEb1DJZu2Czn5WDE s/MN0B5u/olYpsvlOmTe+pNchHKR60eWrgUAOpSNvED/4aQuEnA6w69/m4pB2mBKa9cK gbRPQcRb4FYvi7jE1u7Ncy02+ioS6c1P/8Gl8ly16zPhLOTTffDeW6zYn4kghMZERDJU 2QfhwlUsX+pbrUlJbaIMey62W3J99yBBSyU3L8gyqRY9ITtBlqy7PPVNhj9A02vdGDGt JNpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=yzy6WdAu4zUhGh00Y0VCC7JcjKgDFG4rvSRes5x9/wE=; fh=xhPEfvLLMjXYoLIiI5VbsbsTlvtoR/tb3IoiERHw3Yo=; b=JyJ0QSMb2X1hKTBtuu5dA6E2WWmONFzwWNay6EBo3sAx32XvYlSKZVgi+OTUnZ60ga T+0OK7/O1ZPFJZ3cCODX+kdQx6HLackxxqsPdeoBP2PLCIDGqr7+VADaItDgeO4Omd+T 47BykJNQ896SPaGg+QyIKPYH/XKhXEaAEF1og9RgJwROQZ3YIOZ4jCcWAUau/J85LlR+ nWrsA3MqwNIOspUu+fOtstXlnfnLK0LCplrtkqbjg6sYNzv6D/B791HdwYZdWXXF/4YB Utcui4VSchVFYL/0jWsKVXGPR6jQM4UZDOXLf8m9OfXrtIMTILNWb1hbEzsq7OE2K51I TEZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=NCk+r0Y4; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id a2-20020a17090a8c0200b0027cde7f5860si8661067pjo.30.2023.11.06.08.27.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 08:27:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=NCk+r0Y4; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id DCC3E8025B6B; Mon, 6 Nov 2023 08:27:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232202AbjKFQ0q (ORCPT + 99 others); Mon, 6 Nov 2023 11:26:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232783AbjKFQ0n (ORCPT ); Mon, 6 Nov 2023 11:26:43 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98F7AD6E; Mon, 6 Nov 2023 08:26:37 -0800 (PST) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 4A44B21FB1; Mon, 6 Nov 2023 16:26:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1699287996; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yzy6WdAu4zUhGh00Y0VCC7JcjKgDFG4rvSRes5x9/wE=; b=NCk+r0Y4zoBU6ywtEqoW+/+WJBBg1rXmewOi0LEv6MP6U8+nalS0XyEqUleJvtrjN8WVZV OwjEj4inL8x0zlfJTn0LbpbT0+SkBbppP3mxMPbL9/+F34QI2itmWniSrbJyzOYk3IJfNA yN4FW/E8Dv6SX/sLMT9+ZDutI8Z/5uE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1699287996; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yzy6WdAu4zUhGh00Y0VCC7JcjKgDFG4rvSRes5x9/wE=; b=DPkgDCIO8Kxi7hEJ3ed4g4OBpNmgdEGMOd89nz+SXMI+rwzd5r6hHSno/mMeWYBNVRz3Zo fP8eIP3Iz0B41zBw== Received: from localhost.cz (lhruska.udp.ovpn2.prg.suse.de [10.100.204.118]) by relay2.suse.de (Postfix) with ESMTP id D95612C1D6; Mon, 6 Nov 2023 16:26:35 +0000 (UTC) From: Lukas Hruska To: Petr Mladek , Miroslav Benes , Josh Poimboeuf Cc: Joe Lawrence , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Marcos Paulo de Souza , Josh Poimboeuf , Lukas Hruska Subject: [PATCH v1 4/5] livepatch: Add sample livepatch module Date: Mon, 6 Nov 2023 17:25:12 +0100 Message-ID: <20231106162513.17556-5-lhruska@suse.cz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231106162513.17556-1-lhruska@suse.cz> References: <20231106162513.17556-1-lhruska@suse.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 08:27:15 -0800 (PST) From: Josh Poimboeuf Add a new livepatch sample in samples/livepatch/ to make use of symbols that must be post-processed to enable load-time relocation resolution. As the new sample is to be used as an example, it is annotated with KLP_RELOC_SYMBOL macro. The livepatch sample updates the function cmdline_proc_show to print the string referenced by the symbol saved_command_line appended by the string "livepatch=1". Signed-off-by: Josh Poimboeuf Signed-off-by: Lukas Hruska --- samples/livepatch/Makefile | 1 + .../livepatch/livepatch-annotated-sample.c | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 samples/livepatch/livepatch-annotated-sample.c diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile index 9f853eeb6140..f2b41f4d6c16 100644 --- a/samples/livepatch/Makefile +++ b/samples/livepatch/Makefile @@ -6,3 +6,4 @@ obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-fix2.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-callbacks-demo.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-callbacks-mod.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-callbacks-busymod.o +obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-annotated-sample.o diff --git a/samples/livepatch/livepatch-annotated-sample.c b/samples/livepatch/livepatch-annotated-sample.c new file mode 100644 index 000000000000..36056670f0fa --- /dev/null +++ b/samples/livepatch/livepatch-annotated-sample.c @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2014 Seth Jennings + */ + +/* + * livepatch-annotated-sample.c - Kernel Live Patching Sample Module + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +/* + * This (dumb) live patch overrides the function that prints the + * kernel boot cmdline when /proc/cmdline is read. + * + * This livepatch uses the symbol saved_command_line whose relocation + * must be resolved during load time. To enable that, this module + * must be post-processed by a tool called klp-convert, which embeds + * information to be used by the loader to solve the relocation. + * + * The module is annotated with KLP_RELOC_SYMBOL macros. + * These annotations are used by klp-convert to infer that the symbol + * saved_command_line is in the object vmlinux. + * + * Example: + * + * $ cat /proc/cmdline + * + * + * $ insmod livepatch-sample.ko + * $ cat /proc/cmdline + * livepatch=1 + * + * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled + * $ cat /proc/cmdline + * + */ + +extern char *saved_command_line \ + KLP_RELOC_SYMBOL(vmlinux, vmlinux, saved_command_line); + +#include +static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%s livepatch=1\n", saved_command_line); + return 0; +} + +static struct klp_func funcs[] = { + { + .old_name = "cmdline_proc_show", + .new_func = livepatch_cmdline_proc_show, + }, { } +}; + +static struct klp_object objs[] = { + { + /* name being NULL means vmlinux */ + .funcs = funcs, + }, { } +}; + +static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, +}; + +static int livepatch_init(void) +{ + return klp_enable_patch(&patch); +} + +static void livepatch_exit(void) +{ +} + +module_init(livepatch_init); +module_exit(livepatch_exit); +MODULE_LICENSE("GPL"); +MODULE_INFO(livepatch, "Y"); -- 2.42.0