Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * livepatch-sample.c - Kernel Live Patching Sample Module
4 *
5 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/livepatch.h>
13
14/*
15 * This (dumb) live patch overrides the function that prints the
16 * kernel boot cmdline when /proc/cmdline is read.
17 *
18 * Example:
19 *
20 * $ cat /proc/cmdline
21 * <your cmdline>
22 *
23 * $ insmod livepatch-sample.ko
24 * $ cat /proc/cmdline
25 * this has been live patched
26 *
27 * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
28 * $ cat /proc/cmdline
29 * <your cmdline>
30 */
31
32#include <linux/seq_file.h>
33static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
34{
35 seq_printf(m, "%s\n", "this has been live patched");
36 return 0;
37}
38
39static struct klp_func funcs[] = {
40 {
41 .old_name = "cmdline_proc_show",
42 .new_func = livepatch_cmdline_proc_show,
43 }, { }
44};
45
46static struct klp_object objs[] = {
47 {
48 /* name being NULL means vmlinux */
49 .funcs = funcs,
50 }, { }
51};
52
53static struct klp_patch patch = {
54 .mod = THIS_MODULE,
55 .objs = objs,
56};
57
58static int livepatch_init(void)
59{
60 return klp_enable_patch(&patch);
61}
62
63static void livepatch_exit(void)
64{
65}
66
67module_init(livepatch_init);
68module_exit(livepatch_exit);
69MODULE_LICENSE("GPL");
70MODULE_INFO(livepatch, "Y");
1/*
2 * livepatch-sample.c - Kernel Live Patching Sample Module
3 *
4 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/livepatch.h>
25
26/*
27 * This (dumb) live patch overrides the function that prints the
28 * kernel boot cmdline when /proc/cmdline is read.
29 *
30 * Example:
31 *
32 * $ cat /proc/cmdline
33 * <your cmdline>
34 *
35 * $ insmod livepatch-sample.ko
36 * $ cat /proc/cmdline
37 * this has been live patched
38 *
39 * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
40 * $ cat /proc/cmdline
41 * <your cmdline>
42 */
43
44#include <linux/seq_file.h>
45static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
46{
47 seq_printf(m, "%s\n", "this has been live patched");
48 return 0;
49}
50
51static struct klp_func funcs[] = {
52 {
53 .old_name = "cmdline_proc_show",
54 .new_func = livepatch_cmdline_proc_show,
55 }, { }
56};
57
58static struct klp_object objs[] = {
59 {
60 /* name being NULL means vmlinux */
61 .funcs = funcs,
62 }, { }
63};
64
65static struct klp_patch patch = {
66 .mod = THIS_MODULE,
67 .objs = objs,
68};
69
70static int livepatch_init(void)
71{
72 int ret;
73
74 ret = klp_register_patch(&patch);
75 if (ret)
76 return ret;
77 ret = klp_enable_patch(&patch);
78 if (ret) {
79 WARN_ON(klp_unregister_patch(&patch));
80 return ret;
81 }
82 return 0;
83}
84
85static void livepatch_exit(void)
86{
87 WARN_ON(klp_unregister_patch(&patch));
88}
89
90module_init(livepatch_init);
91module_exit(livepatch_exit);
92MODULE_LICENSE("GPL");
93MODULE_INFO(livepatch, "Y");