From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A63D8C3526E for ; Wed, 16 Feb 2022 16:40:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236518AbiBPQkM (ORCPT ); Wed, 16 Feb 2022 11:40:12 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:41364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236519AbiBPQkC (ORCPT ); Wed, 16 Feb 2022 11:40:02 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AD68D25D243 for ; Wed, 16 Feb 2022 08:39:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645029588; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EYfjisjZqIY36JDFITzF8hRLD/Q7GSgYyUA2NagtatU=; b=LMIESuobEzhwAFzwd7R22XdABDKl0vazJFgIKehzuI/FxLZGIT77Vcb5pZqJuq4anUwrQ4 r+g0B7Les1xV/GgX2YyGmHtJPgKVcWZ8821Wupqr+GjCcMuCwDlhx5DZbfCknwYwHY7PI0 3bSQ6XGNOQUYevnDcikyRMvHzofZq4M= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-648-xA6M3ESfNtC0MMAB1RyWhQ-1; Wed, 16 Feb 2022 11:39:47 -0500 X-MC-Unique: xA6M3ESfNtC0MMAB1RyWhQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6C755100C612; Wed, 16 Feb 2022 16:39:46 +0000 (UTC) Received: from jlaw-desktop.redhat.com (unknown [10.22.8.49]) by smtp.corp.redhat.com (Postfix) with ESMTP id EEDF3753CD; Wed, 16 Feb 2022 16:39:45 +0000 (UTC) From: Joe Lawrence To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org Subject: [RFC PATCH v6 06/12] livepatch: Add sample livepatch module Date: Wed, 16 Feb 2022 11:39:34 -0500 Message-Id: <20220216163940.228309-7-joe.lawrence@redhat.com> In-Reply-To: <20220216163940.228309-1-joe.lawrence@redhat.com> References: <20220216163940.228309-1-joe.lawrence@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: live-patching@vger.kernel.org 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_MODULE_RELOC and with KLP_SYMPOS macros. 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". Update livepatch-sample.c to remove livepatch MODULE_INFO statement. Signed-off-by: Josh Poimboeuf Signed-off-by: Joao Moreira Signed-off-by: Joe Lawrence --- samples/livepatch/Makefile | 1 + .../livepatch/livepatch-annotated-sample.c | 93 +++++++++++++++++++ 2 files changed, 94 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..4fe0e16423c7 --- /dev/null +++ b/samples/livepatch/livepatch-annotated-sample.c @@ -0,0 +1,93 @@ +// 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_MODULE_RELOC/KLP_SYMPOS macros. + * These annotations are used by klp-convert to infer that the symbol + * saved_command_line is in the object vmlinux. + * + * As saved_command_line has no other homonimous symbol across + * kernel objects, this annotation is not a requirement, and can be + * suppressed with no harm to klp-convert. Yet, it is kept here as an + * example on how to annotate livepatch modules that contain symbols + * whose names are used in more than one kernel object. + * + * 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; + +#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; +} + +KLP_MODULE_RELOC(vmlinux) vmlinux_relocs[] = { + KLP_SYMPOS(saved_command_line, 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.26.3