1439e7271SJoe Lawrence /* 2439e7271SJoe Lawrence * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com> 3439e7271SJoe Lawrence * 4439e7271SJoe Lawrence * This program is free software; you can redistribute it and/or 5439e7271SJoe Lawrence * modify it under the terms of the GNU General Public License 6439e7271SJoe Lawrence * as published by the Free Software Foundation; either version 2 7439e7271SJoe Lawrence * of the License, or (at your option) any later version. 8439e7271SJoe Lawrence * 9439e7271SJoe Lawrence * This program is distributed in the hope that it will be useful, 10439e7271SJoe Lawrence * but WITHOUT ANY WARRANTY; without even the implied warranty of 11439e7271SJoe Lawrence * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12439e7271SJoe Lawrence * GNU General Public License for more details. 13439e7271SJoe Lawrence * 14439e7271SJoe Lawrence * You should have received a copy of the GNU General Public License 15439e7271SJoe Lawrence * along with this program; if not, see <http://www.gnu.org/licenses/>. 16439e7271SJoe Lawrence */ 17439e7271SJoe Lawrence 18439e7271SJoe Lawrence /* 19439e7271SJoe Lawrence * livepatch-shadow-fix2.c - Shadow variables, livepatch demo 20439e7271SJoe Lawrence * 21439e7271SJoe Lawrence * Purpose 22439e7271SJoe Lawrence * ------- 23439e7271SJoe Lawrence * 24439e7271SJoe Lawrence * Adds functionality to livepatch-shadow-mod's in-flight data 25439e7271SJoe Lawrence * structures through a shadow variable. The livepatch patches a 26439e7271SJoe Lawrence * routine that periodically inspects data structures, incrementing a 27439e7271SJoe Lawrence * per-data-structure counter, creating the counter if needed. 28439e7271SJoe Lawrence * 29439e7271SJoe Lawrence * 30439e7271SJoe Lawrence * Usage 31439e7271SJoe Lawrence * ----- 32439e7271SJoe Lawrence * 33439e7271SJoe Lawrence * This module is not intended to be standalone. See the "Usage" 34439e7271SJoe Lawrence * section of livepatch-shadow-mod.c. 35439e7271SJoe Lawrence */ 36439e7271SJoe Lawrence 37439e7271SJoe Lawrence #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 38439e7271SJoe Lawrence 39439e7271SJoe Lawrence #include <linux/module.h> 40439e7271SJoe Lawrence #include <linux/kernel.h> 41439e7271SJoe Lawrence #include <linux/livepatch.h> 42439e7271SJoe Lawrence #include <linux/slab.h> 43439e7271SJoe Lawrence 44439e7271SJoe Lawrence /* Shadow variable enums */ 45439e7271SJoe Lawrence #define SV_LEAK 1 46439e7271SJoe Lawrence #define SV_COUNTER 2 47439e7271SJoe Lawrence 48439e7271SJoe Lawrence struct dummy { 49439e7271SJoe Lawrence struct list_head list; 50439e7271SJoe Lawrence unsigned long jiffies_expire; 51439e7271SJoe Lawrence }; 52439e7271SJoe Lawrence 53439e7271SJoe Lawrence bool livepatch_fix2_dummy_check(struct dummy *d, unsigned long jiffies) 54439e7271SJoe Lawrence { 55439e7271SJoe Lawrence int *shadow_count; 56439e7271SJoe Lawrence 57439e7271SJoe Lawrence /* 58439e7271SJoe Lawrence * Patch: handle in-flight dummy structures, if they do not 59439e7271SJoe Lawrence * already have a SV_COUNTER shadow variable, then attach a 60439e7271SJoe Lawrence * new one. 61439e7271SJoe Lawrence */ 62439e7271SJoe Lawrence shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER, 63e91c2518SPetr Mladek sizeof(*shadow_count), GFP_NOWAIT, 64e91c2518SPetr Mladek NULL, NULL); 65439e7271SJoe Lawrence if (shadow_count) 66439e7271SJoe Lawrence *shadow_count += 1; 67439e7271SJoe Lawrence 68439e7271SJoe Lawrence return time_after(jiffies, d->jiffies_expire); 69439e7271SJoe Lawrence } 70439e7271SJoe Lawrence 71*3b2c77d0SPetr Mladek static void livepatch_fix2_dummy_leak_dtor(void *obj, void *shadow_data) 72*3b2c77d0SPetr Mladek { 73*3b2c77d0SPetr Mladek void *d = obj; 74*3b2c77d0SPetr Mladek void **shadow_leak = shadow_data; 75*3b2c77d0SPetr Mladek 76*3b2c77d0SPetr Mladek kfree(*shadow_leak); 77*3b2c77d0SPetr Mladek pr_info("%s: dummy @ %p, prevented leak @ %p\n", 78*3b2c77d0SPetr Mladek __func__, d, *shadow_leak); 79*3b2c77d0SPetr Mladek } 80*3b2c77d0SPetr Mladek 81439e7271SJoe Lawrence void livepatch_fix2_dummy_free(struct dummy *d) 82439e7271SJoe Lawrence { 83*3b2c77d0SPetr Mladek void **shadow_leak; 84439e7271SJoe Lawrence int *shadow_count; 85439e7271SJoe Lawrence 86439e7271SJoe Lawrence /* Patch: copy the memory leak patch from the fix1 module. */ 87439e7271SJoe Lawrence shadow_leak = klp_shadow_get(d, SV_LEAK); 88*3b2c77d0SPetr Mladek if (shadow_leak) 89*3b2c77d0SPetr Mladek klp_shadow_free(d, SV_LEAK, livepatch_fix2_dummy_leak_dtor); 90*3b2c77d0SPetr Mladek else 91439e7271SJoe Lawrence pr_info("%s: dummy @ %p leaked!\n", __func__, d); 92439e7271SJoe Lawrence 93439e7271SJoe Lawrence /* 94439e7271SJoe Lawrence * Patch: fetch the SV_COUNTER shadow variable and display 95439e7271SJoe Lawrence * the final count. Detach the shadow variable. 96439e7271SJoe Lawrence */ 97439e7271SJoe Lawrence shadow_count = klp_shadow_get(d, SV_COUNTER); 98439e7271SJoe Lawrence if (shadow_count) { 99439e7271SJoe Lawrence pr_info("%s: dummy @ %p, check counter = %d\n", 100439e7271SJoe Lawrence __func__, d, *shadow_count); 101*3b2c77d0SPetr Mladek klp_shadow_free(d, SV_COUNTER, NULL); 102439e7271SJoe Lawrence } 103439e7271SJoe Lawrence 104439e7271SJoe Lawrence kfree(d); 105439e7271SJoe Lawrence } 106439e7271SJoe Lawrence 107439e7271SJoe Lawrence static struct klp_func funcs[] = { 108439e7271SJoe Lawrence { 109439e7271SJoe Lawrence .old_name = "dummy_check", 110439e7271SJoe Lawrence .new_func = livepatch_fix2_dummy_check, 111439e7271SJoe Lawrence }, 112439e7271SJoe Lawrence { 113439e7271SJoe Lawrence .old_name = "dummy_free", 114439e7271SJoe Lawrence .new_func = livepatch_fix2_dummy_free, 115439e7271SJoe Lawrence }, { } 116439e7271SJoe Lawrence }; 117439e7271SJoe Lawrence 118439e7271SJoe Lawrence static struct klp_object objs[] = { 119439e7271SJoe Lawrence { 120439e7271SJoe Lawrence .name = "livepatch_shadow_mod", 121439e7271SJoe Lawrence .funcs = funcs, 122439e7271SJoe Lawrence }, { } 123439e7271SJoe Lawrence }; 124439e7271SJoe Lawrence 125439e7271SJoe Lawrence static struct klp_patch patch = { 126439e7271SJoe Lawrence .mod = THIS_MODULE, 127439e7271SJoe Lawrence .objs = objs, 128439e7271SJoe Lawrence }; 129439e7271SJoe Lawrence 130439e7271SJoe Lawrence static int livepatch_shadow_fix2_init(void) 131439e7271SJoe Lawrence { 132439e7271SJoe Lawrence int ret; 133439e7271SJoe Lawrence 134439e7271SJoe Lawrence ret = klp_register_patch(&patch); 135439e7271SJoe Lawrence if (ret) 136439e7271SJoe Lawrence return ret; 137439e7271SJoe Lawrence ret = klp_enable_patch(&patch); 138439e7271SJoe Lawrence if (ret) { 139439e7271SJoe Lawrence WARN_ON(klp_unregister_patch(&patch)); 140439e7271SJoe Lawrence return ret; 141439e7271SJoe Lawrence } 142439e7271SJoe Lawrence return 0; 143439e7271SJoe Lawrence } 144439e7271SJoe Lawrence 145439e7271SJoe Lawrence static void livepatch_shadow_fix2_exit(void) 146439e7271SJoe Lawrence { 147439e7271SJoe Lawrence /* Cleanup any existing SV_COUNTER shadow variables */ 148*3b2c77d0SPetr Mladek klp_shadow_free_all(SV_COUNTER, NULL); 149439e7271SJoe Lawrence 150439e7271SJoe Lawrence WARN_ON(klp_unregister_patch(&patch)); 151439e7271SJoe Lawrence } 152439e7271SJoe Lawrence 153439e7271SJoe Lawrence module_init(livepatch_shadow_fix2_init); 154439e7271SJoe Lawrence module_exit(livepatch_shadow_fix2_exit); 155439e7271SJoe Lawrence MODULE_LICENSE("GPL"); 156439e7271SJoe Lawrence MODULE_INFO(livepatch, "Y"); 157