1*1ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
213d1cf7eSSeth Jennings /*
313d1cf7eSSeth Jennings * livepatch-sample.c - Kernel Live Patching Sample Module
413d1cf7eSSeth Jennings *
513d1cf7eSSeth Jennings * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
613d1cf7eSSeth Jennings */
713d1cf7eSSeth Jennings
8d83a7cb3SJosh Poimboeuf #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9d83a7cb3SJosh Poimboeuf
1013d1cf7eSSeth Jennings #include <linux/module.h>
1113d1cf7eSSeth Jennings #include <linux/kernel.h>
1213d1cf7eSSeth Jennings #include <linux/livepatch.h>
1313d1cf7eSSeth Jennings
1413d1cf7eSSeth Jennings /*
1513d1cf7eSSeth Jennings * This (dumb) live patch overrides the function that prints the
1613d1cf7eSSeth Jennings * kernel boot cmdline when /proc/cmdline is read.
1713d1cf7eSSeth Jennings *
1813d1cf7eSSeth Jennings * Example:
19700a3048SJosh Poimboeuf *
2013d1cf7eSSeth Jennings * $ cat /proc/cmdline
2113d1cf7eSSeth Jennings * <your cmdline>
22700a3048SJosh Poimboeuf *
2313d1cf7eSSeth Jennings * $ insmod livepatch-sample.ko
2413d1cf7eSSeth Jennings * $ cat /proc/cmdline
2513d1cf7eSSeth Jennings * this has been live patched
26700a3048SJosh Poimboeuf *
27700a3048SJosh Poimboeuf * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
28700a3048SJosh Poimboeuf * $ cat /proc/cmdline
2913d1cf7eSSeth Jennings * <your cmdline>
3013d1cf7eSSeth Jennings */
3113d1cf7eSSeth Jennings
3213d1cf7eSSeth Jennings #include <linux/seq_file.h>
livepatch_cmdline_proc_show(struct seq_file * m,void * v)3313d1cf7eSSeth Jennings static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
3413d1cf7eSSeth Jennings {
3513d1cf7eSSeth Jennings seq_printf(m, "%s\n", "this has been live patched");
3613d1cf7eSSeth Jennings return 0;
3713d1cf7eSSeth Jennings }
3813d1cf7eSSeth Jennings
3913d1cf7eSSeth Jennings static struct klp_func funcs[] = {
4013d1cf7eSSeth Jennings {
4113d1cf7eSSeth Jennings .old_name = "cmdline_proc_show",
4213d1cf7eSSeth Jennings .new_func = livepatch_cmdline_proc_show,
4313d1cf7eSSeth Jennings }, { }
4413d1cf7eSSeth Jennings };
4513d1cf7eSSeth Jennings
4613d1cf7eSSeth Jennings static struct klp_object objs[] = {
4713d1cf7eSSeth Jennings {
4813d1cf7eSSeth Jennings /* name being NULL means vmlinux */
4913d1cf7eSSeth Jennings .funcs = funcs,
5013d1cf7eSSeth Jennings }, { }
5113d1cf7eSSeth Jennings };
5213d1cf7eSSeth Jennings
5313d1cf7eSSeth Jennings static struct klp_patch patch = {
5413d1cf7eSSeth Jennings .mod = THIS_MODULE,
5513d1cf7eSSeth Jennings .objs = objs,
5613d1cf7eSSeth Jennings };
5713d1cf7eSSeth Jennings
livepatch_init(void)5813d1cf7eSSeth Jennings static int livepatch_init(void)
5913d1cf7eSSeth Jennings {
60958ef1e3SPetr Mladek return klp_enable_patch(&patch);
6113d1cf7eSSeth Jennings }
6213d1cf7eSSeth Jennings
livepatch_exit(void)6313d1cf7eSSeth Jennings static void livepatch_exit(void)
6413d1cf7eSSeth Jennings {
6513d1cf7eSSeth Jennings }
6613d1cf7eSSeth Jennings
6713d1cf7eSSeth Jennings module_init(livepatch_init);
6813d1cf7eSSeth Jennings module_exit(livepatch_exit);
6913d1cf7eSSeth Jennings MODULE_LICENSE("GPL");
70425595a7SJessica Yu MODULE_INFO(livepatch, "Y");
71