Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp5106974yba; Wed, 10 Apr 2019 11:28:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqyB6ZuSPXdKvZ3WQdMTBEho3wFPxyxw2p2D1cqhEd1cdy4TVFMijW7bTfS01trbbwCInxlX X-Received: by 2002:a65:62c9:: with SMTP id m9mr39535002pgv.309.1554920913462; Wed, 10 Apr 2019 11:28:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554920913; cv=none; d=google.com; s=arc-20160816; b=GcCyOcNbxcWsHUSdvBFqKgm4Z6av5WfzzKZgpRId0Ba+PDSYmf3BkkXkx+dH0DGMlP 40OZEcEq6aGoElv/6fr2nDrXX9AnYCTKmVH/hPq3klrl6epO4OejIMPnmpmfZM/kn+YN SKYL/zUBvsRch8LCSPWWiWF5CpxYYwOP4CV6IKUstaHTTN6ZQZUJNd7zEEet+/+VzvGU JOxGuRzdKRW8IzF3AqI5vOv+u8xiltx+H8xT9dQa3UDcdHSOAXImTHe/EVkSxPXxRdTr /OYV7bx7Jj08s5gqDJgQTVQTAj3tFgAtLYhrEXANccGmywggTvYf8/QUcLQQklFvMTiJ M9pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=uXOBXncxzM/YmhhN2dC1Qa5MVPZD6Mtc77rQrWhAXq0=; b=M0vKQO68x6PaN3Ke6qpoDYyPGJ2E3dblimK2xHRTCSlQSn2dle8uMy1iyVRkjwOous RTC6pkOMWyV4pvV2jS9VRJdzBwPqfk5nMYqqybaHbiQBcpwwFEb4BnXNftVMfNhsek+8 wNfXL46SgVPMcn/KfMD863HgfAMVJZ0JLbdzb/GpDvWG3gzGSYldhYB3Bxby43X28jVj 4yK7MzOsQcIFec08N4hUxZ/QAy0tFp/v93poctouo6vUdEEJSkiBa74Ypv/+bD+hvKCV Dxvgl3B4iuPdxqKTTtG4OuW8L+fnsMUXpwrSNPQ8DnUAhlO5JXGxqfhz8Io93Y61WNWx RDnw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l62si23587942pge.579.2019.04.10.11.28.16; Wed, 10 Apr 2019 11:28:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387553AbfDJPvU (ORCPT + 99 others); Wed, 10 Apr 2019 11:51:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:30576 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387528AbfDJPvP (ORCPT ); Wed, 10 Apr 2019 11:51:15 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EBFFD316891D; Wed, 10 Apr 2019 15:51:14 +0000 (UTC) Received: from jlaw-desktop.bos.redhat.com (dhcp-17-208.bos.redhat.com [10.18.17.208]) by smtp.corp.redhat.com (Postfix) with ESMTP id 95D841062225; Wed, 10 Apr 2019 15:51:13 +0000 (UTC) From: Joe Lawrence To: linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, linux-kbuild@vger.kernel.org Cc: Jessica Yu , Jiri Kosina , Joao Moreira , Joe Lawrence , Josh Poimboeuf , Konstantin Khlebnikov , Masahiro Yamada , Michael Matz , Miroslav Benes , Nicolai Stange , Petr Mladek Subject: [PATCH v3 7/9] livepatch: Add sample livepatch module Date: Wed, 10 Apr 2019 11:50:56 -0400 Message-Id: <20190410155058.9437-8-joe.lawrence@redhat.com> In-Reply-To: <20190410155058.9437-1-joe.lawrence@redhat.com> References: <20190410155058.9437-1-joe.lawrence@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 10 Apr 2019 15:51:15 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org 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 | 2 + .../livepatch/livepatch-annotated-sample.c | 102 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 samples/livepatch/livepatch-annotated-sample.c diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile index 5fb3280bbdc4..dea530840725 100644 --- a/samples/livepatch/Makefile +++ b/samples/livepatch/Makefile @@ -2,6 +2,7 @@ LIVEPATCH_livepatch-sample := y LIVEPATCH_livepatch-shadow-fix1 := y LIVEPATCH_livepatch-shadow-fix2 := y LIVEPATCH_livepatch-callbacks-demo := y +LIVEPATCH_livepatch-annotated-sample := y obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-mod.o @@ -10,3 +11,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..556ce7e0bdab --- /dev/null +++ b/samples/livepatch/livepatch-annotated-sample.c @@ -0,0 +1,102 @@ +/* + * livepatch-annotated-sample.c - Kernel Live Patching Sample Module + * + * Copyright (C) 2014 Seth Jennings + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#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"); -- 2.20.1