18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * livepatch-sample.c - Kernel Live Patching Sample Module
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/livepatch.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * This (dumb) live patch overrides the function that prints the
168c2ecf20Sopenharmony_ci * kernel boot cmdline when /proc/cmdline is read.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Example:
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * $ cat /proc/cmdline
218c2ecf20Sopenharmony_ci * <your cmdline>
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * $ insmod livepatch-sample.ko
248c2ecf20Sopenharmony_ci * $ cat /proc/cmdline
258c2ecf20Sopenharmony_ci * this has been live patched
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
288c2ecf20Sopenharmony_ci * $ cat /proc/cmdline
298c2ecf20Sopenharmony_ci * <your cmdline>
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
338c2ecf20Sopenharmony_cistatic int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	seq_printf(m, "%s\n", "this has been live patched");
368c2ecf20Sopenharmony_ci	return 0;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic struct klp_func funcs[] = {
408c2ecf20Sopenharmony_ci	{
418c2ecf20Sopenharmony_ci		.old_name = "cmdline_proc_show",
428c2ecf20Sopenharmony_ci		.new_func = livepatch_cmdline_proc_show,
438c2ecf20Sopenharmony_ci	}, { }
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct klp_object objs[] = {
478c2ecf20Sopenharmony_ci	{
488c2ecf20Sopenharmony_ci		/* name being NULL means vmlinux */
498c2ecf20Sopenharmony_ci		.funcs = funcs,
508c2ecf20Sopenharmony_ci	}, { }
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic struct klp_patch patch = {
548c2ecf20Sopenharmony_ci	.mod = THIS_MODULE,
558c2ecf20Sopenharmony_ci	.objs = objs,
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic int livepatch_init(void)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return klp_enable_patch(&patch);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void livepatch_exit(void)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cimodule_init(livepatch_init);
688c2ecf20Sopenharmony_cimodule_exit(livepatch_exit);
698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
708c2ecf20Sopenharmony_ciMODULE_INFO(livepatch, "Y");
71