Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1037886pxb; Wed, 16 Feb 2022 09:34:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJxlwLu3uqJuVTJNfJ3vr6ytxB3qKBXFxjG7YgTZz9zUdTnWuRAA+sDRMTrjTG/+l5oxth98 X-Received: by 2002:a17:902:8497:b0:14f:919:9fdd with SMTP id c23-20020a170902849700b0014f09199fddmr3674027plo.52.1645032873146; Wed, 16 Feb 2022 09:34:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645032873; cv=none; d=google.com; s=arc-20160816; b=Mu1csiLIg64OQNjlXIaKGVwfkU96vwdEdp9pNEiLpDx2PD6mjvPRPaVSp3+nwBhk7v vvNKtHUjx15gW2XUNmN/TW1gL4LAENzgjRHjbhLL5HA05iZlpu54cptoM3vxnFlypp7Z nM/6rsAfLLznkJa8EGrss7/p05/+FVMtqANCHsSLaNKebKdgl5iHYeDzDS2Z4sTN9+cY OM7MJ0UA+NdBbQ+26LqIJ1KOZvi7766XV2cni53KureW/W7XHDCa0QrDI/0HMU6QDZnF w0pkiz8Jm4sgt/F1f/Br5MCYmv8EYmEdc2X8E8fWA5p8Qen/PGlNO8CaYZr/2rmoNnFV HmJA== 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:to:from :dkim-signature; bh=EYfjisjZqIY36JDFITzF8hRLD/Q7GSgYyUA2NagtatU=; b=oCzNu3CHg/z/SK53Uu75oqjgwXtJ2TgmbeZesHaYfWibHcUqqj8u6idWrSn8XGCvBI jNxYxfV0o5aT26gkWIyqSkbZCB2FJEMtPQAWdWoDm4NX7GIa0Lq7U3uB8bWLqRBskGCK idgEBzrLOm5x7NzVWt0D8eSw8EnNFFBfilyoQq3O/kYP011ACwKik46fHmtpMmazIYFi ZBuKC32un6faMW73CEz+CRTIO/2pSIyVjtRG1MaBi9qxeoEWmIDjyKg0T5Nhf03pAjmg ZHD/qOC4/Bp3YqkbI7k+gdFqflhsR1Jcg5yW35MR2Urb/xZgnJWL7dMBKz7hkNjfOJNJ cUDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=D1kdrVes; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u9si22183909ple.105.2022.02.16.09.34.16; Wed, 16 Feb 2022 09:34:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=D1kdrVes; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236519AbiBPQkN (ORCPT + 99 others); Wed, 16 Feb 2022 11:40:13 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:41828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236534AbiBPQkH (ORCPT ); Wed, 16 Feb 2022 11:40:07 -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 A117025D264 for ; Wed, 16 Feb 2022 08:39:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645029590; 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=D1kdrVesXBmbx6GAb6Lx0SkQycATPz2uxIV+JepU74xl+OjMBpk2bS5RMctR+e7BuZHume rejxzTTENuzEZ/Q1ZU56QiHhUYEFUEog1SKIY0rJRIucSdsIPTUcb5nT5XhINPOQ7kt1IF Z9RPpnb/Rzp7ipAkVZFeSppumPLbbuQ= 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 X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 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