Path: blob/master/samples/livepatch/livepatch-shadow-fix2.c
26285 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (C) 2017 Joe Lawrence <[email protected]>3*/45/*6* livepatch-shadow-fix2.c - Shadow variables, livepatch demo7*8* Purpose9* -------10*11* Adds functionality to livepatch-shadow-mod's in-flight data12* structures through a shadow variable. The livepatch patches a13* routine that periodically inspects data structures, incrementing a14* per-data-structure counter, creating the counter if needed.15*16*17* Usage18* -----19*20* This module is not intended to be standalone. See the "Usage"21* section of livepatch-shadow-mod.c.22*/2324#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt2526#include <linux/module.h>27#include <linux/kernel.h>28#include <linux/livepatch.h>29#include <linux/slab.h>3031/* Shadow variable enums */32#define SV_LEAK 133#define SV_COUNTER 23435struct dummy {36struct list_head list;37unsigned long jiffies_expire;38};3940static bool livepatch_fix2_dummy_check(struct dummy *d, unsigned long jiffies)41{42int *shadow_count;4344/*45* Patch: handle in-flight dummy structures, if they do not46* already have a SV_COUNTER shadow variable, then attach a47* new one.48*/49shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER,50sizeof(*shadow_count), GFP_NOWAIT,51NULL, NULL);52if (shadow_count)53*shadow_count += 1;5455return time_after(jiffies, d->jiffies_expire);56}5758static void livepatch_fix2_dummy_leak_dtor(void *obj, void *shadow_data)59{60void *d = obj;61int **shadow_leak = shadow_data;6263pr_info("%s: dummy @ %p, prevented leak @ %p\n",64__func__, d, *shadow_leak);65kfree(*shadow_leak);66}6768static void livepatch_fix2_dummy_free(struct dummy *d)69{70int **shadow_leak;71int *shadow_count;7273/* Patch: copy the memory leak patch from the fix1 module. */74shadow_leak = klp_shadow_get(d, SV_LEAK);75if (shadow_leak)76klp_shadow_free(d, SV_LEAK, livepatch_fix2_dummy_leak_dtor);77else78pr_info("%s: dummy @ %p leaked!\n", __func__, d);7980/*81* Patch: fetch the SV_COUNTER shadow variable and display82* the final count. Detach the shadow variable.83*/84shadow_count = klp_shadow_get(d, SV_COUNTER);85if (shadow_count) {86pr_info("%s: dummy @ %p, check counter = %d\n",87__func__, d, *shadow_count);88klp_shadow_free(d, SV_COUNTER, NULL);89}9091kfree(d);92}9394static struct klp_func funcs[] = {95{96.old_name = "dummy_check",97.new_func = livepatch_fix2_dummy_check,98},99{100.old_name = "dummy_free",101.new_func = livepatch_fix2_dummy_free,102}, { }103};104105static struct klp_object objs[] = {106{107.name = "livepatch_shadow_mod",108.funcs = funcs,109}, { }110};111112static struct klp_patch patch = {113.mod = THIS_MODULE,114.objs = objs,115};116117static int livepatch_shadow_fix2_init(void)118{119return klp_enable_patch(&patch);120}121122static void livepatch_shadow_fix2_exit(void)123{124/* Cleanup any existing SV_COUNTER shadow variables */125klp_shadow_free_all(SV_COUNTER, NULL);126}127128module_init(livepatch_shadow_fix2_init);129module_exit(livepatch_shadow_fix2_exit);130MODULE_DESCRIPTION("Live patching demo for shadow variables");131MODULE_LICENSE("GPL");132MODULE_INFO(livepatch, "Y");133134135