1*439e7271SJoe Lawrence /* 2*439e7271SJoe Lawrence * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com> 3*439e7271SJoe Lawrence * 4*439e7271SJoe Lawrence * This program is free software; you can redistribute it and/or 5*439e7271SJoe Lawrence * modify it under the terms of the GNU General Public License 6*439e7271SJoe Lawrence * as published by the Free Software Foundation; either version 2 7*439e7271SJoe Lawrence * of the License, or (at your option) any later version. 8*439e7271SJoe Lawrence * 9*439e7271SJoe Lawrence * This program is distributed in the hope that it will be useful, 10*439e7271SJoe Lawrence * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*439e7271SJoe Lawrence * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*439e7271SJoe Lawrence * GNU General Public License for more details. 13*439e7271SJoe Lawrence * 14*439e7271SJoe Lawrence * You should have received a copy of the GNU General Public License 15*439e7271SJoe Lawrence * along with this program; if not, see <http://www.gnu.org/licenses/>. 16*439e7271SJoe Lawrence */ 17*439e7271SJoe Lawrence 18*439e7271SJoe Lawrence /* 19*439e7271SJoe Lawrence * livepatch-shadow-fix2.c - Shadow variables, livepatch demo 20*439e7271SJoe Lawrence * 21*439e7271SJoe Lawrence * Purpose 22*439e7271SJoe Lawrence * ------- 23*439e7271SJoe Lawrence * 24*439e7271SJoe Lawrence * Adds functionality to livepatch-shadow-mod's in-flight data 25*439e7271SJoe Lawrence * structures through a shadow variable. The livepatch patches a 26*439e7271SJoe Lawrence * routine that periodically inspects data structures, incrementing a 27*439e7271SJoe Lawrence * per-data-structure counter, creating the counter if needed. 28*439e7271SJoe Lawrence * 29*439e7271SJoe Lawrence * 30*439e7271SJoe Lawrence * Usage 31*439e7271SJoe Lawrence * ----- 32*439e7271SJoe Lawrence * 33*439e7271SJoe Lawrence * This module is not intended to be standalone. See the "Usage" 34*439e7271SJoe Lawrence * section of livepatch-shadow-mod.c. 35*439e7271SJoe Lawrence */ 36*439e7271SJoe Lawrence 37*439e7271SJoe Lawrence #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 38*439e7271SJoe Lawrence 39*439e7271SJoe Lawrence #include <linux/module.h> 40*439e7271SJoe Lawrence #include <linux/kernel.h> 41*439e7271SJoe Lawrence #include <linux/livepatch.h> 42*439e7271SJoe Lawrence #include <linux/slab.h> 43*439e7271SJoe Lawrence 44*439e7271SJoe Lawrence /* Shadow variable enums */ 45*439e7271SJoe Lawrence #define SV_LEAK 1 46*439e7271SJoe Lawrence #define SV_COUNTER 2 47*439e7271SJoe Lawrence 48*439e7271SJoe Lawrence struct dummy { 49*439e7271SJoe Lawrence struct list_head list; 50*439e7271SJoe Lawrence unsigned long jiffies_expire; 51*439e7271SJoe Lawrence }; 52*439e7271SJoe Lawrence 53*439e7271SJoe Lawrence bool livepatch_fix2_dummy_check(struct dummy *d, unsigned long jiffies) 54*439e7271SJoe Lawrence { 55*439e7271SJoe Lawrence int *shadow_count; 56*439e7271SJoe Lawrence int count; 57*439e7271SJoe Lawrence 58*439e7271SJoe Lawrence /* 59*439e7271SJoe Lawrence * Patch: handle in-flight dummy structures, if they do not 60*439e7271SJoe Lawrence * already have a SV_COUNTER shadow variable, then attach a 61*439e7271SJoe Lawrence * new one. 62*439e7271SJoe Lawrence */ 63*439e7271SJoe Lawrence count = 0; 64*439e7271SJoe Lawrence shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER, 65*439e7271SJoe Lawrence &count, sizeof(count), 66*439e7271SJoe Lawrence GFP_NOWAIT); 67*439e7271SJoe Lawrence if (shadow_count) 68*439e7271SJoe Lawrence *shadow_count += 1; 69*439e7271SJoe Lawrence 70*439e7271SJoe Lawrence return time_after(jiffies, d->jiffies_expire); 71*439e7271SJoe Lawrence } 72*439e7271SJoe Lawrence 73*439e7271SJoe Lawrence void livepatch_fix2_dummy_free(struct dummy *d) 74*439e7271SJoe Lawrence { 75*439e7271SJoe Lawrence void **shadow_leak, *leak; 76*439e7271SJoe Lawrence int *shadow_count; 77*439e7271SJoe Lawrence 78*439e7271SJoe Lawrence /* Patch: copy the memory leak patch from the fix1 module. */ 79*439e7271SJoe Lawrence shadow_leak = klp_shadow_get(d, SV_LEAK); 80*439e7271SJoe Lawrence if (shadow_leak) { 81*439e7271SJoe Lawrence leak = *shadow_leak; 82*439e7271SJoe Lawrence klp_shadow_free(d, SV_LEAK); 83*439e7271SJoe Lawrence kfree(leak); 84*439e7271SJoe Lawrence pr_info("%s: dummy @ %p, prevented leak @ %p\n", 85*439e7271SJoe Lawrence __func__, d, leak); 86*439e7271SJoe Lawrence } else { 87*439e7271SJoe Lawrence pr_info("%s: dummy @ %p leaked!\n", __func__, d); 88*439e7271SJoe Lawrence } 89*439e7271SJoe Lawrence 90*439e7271SJoe Lawrence /* 91*439e7271SJoe Lawrence * Patch: fetch the SV_COUNTER shadow variable and display 92*439e7271SJoe Lawrence * the final count. Detach the shadow variable. 93*439e7271SJoe Lawrence */ 94*439e7271SJoe Lawrence shadow_count = klp_shadow_get(d, SV_COUNTER); 95*439e7271SJoe Lawrence if (shadow_count) { 96*439e7271SJoe Lawrence pr_info("%s: dummy @ %p, check counter = %d\n", 97*439e7271SJoe Lawrence __func__, d, *shadow_count); 98*439e7271SJoe Lawrence klp_shadow_free(d, SV_COUNTER); 99*439e7271SJoe Lawrence } 100*439e7271SJoe Lawrence 101*439e7271SJoe Lawrence kfree(d); 102*439e7271SJoe Lawrence } 103*439e7271SJoe Lawrence 104*439e7271SJoe Lawrence static struct klp_func funcs[] = { 105*439e7271SJoe Lawrence { 106*439e7271SJoe Lawrence .old_name = "dummy_check", 107*439e7271SJoe Lawrence .new_func = livepatch_fix2_dummy_check, 108*439e7271SJoe Lawrence }, 109*439e7271SJoe Lawrence { 110*439e7271SJoe Lawrence .old_name = "dummy_free", 111*439e7271SJoe Lawrence .new_func = livepatch_fix2_dummy_free, 112*439e7271SJoe Lawrence }, { } 113*439e7271SJoe Lawrence }; 114*439e7271SJoe Lawrence 115*439e7271SJoe Lawrence static struct klp_object objs[] = { 116*439e7271SJoe Lawrence { 117*439e7271SJoe Lawrence .name = "livepatch_shadow_mod", 118*439e7271SJoe Lawrence .funcs = funcs, 119*439e7271SJoe Lawrence }, { } 120*439e7271SJoe Lawrence }; 121*439e7271SJoe Lawrence 122*439e7271SJoe Lawrence static struct klp_patch patch = { 123*439e7271SJoe Lawrence .mod = THIS_MODULE, 124*439e7271SJoe Lawrence .objs = objs, 125*439e7271SJoe Lawrence }; 126*439e7271SJoe Lawrence 127*439e7271SJoe Lawrence static int livepatch_shadow_fix2_init(void) 128*439e7271SJoe Lawrence { 129*439e7271SJoe Lawrence int ret; 130*439e7271SJoe Lawrence 131*439e7271SJoe Lawrence if (!klp_have_reliable_stack() && !patch.immediate) { 132*439e7271SJoe Lawrence /* 133*439e7271SJoe Lawrence * WARNING: Be very careful when using 'patch.immediate' in 134*439e7271SJoe Lawrence * your patches. It's ok to use it for simple patches like 135*439e7271SJoe Lawrence * this, but for more complex patches which change function 136*439e7271SJoe Lawrence * semantics, locking semantics, or data structures, it may not 137*439e7271SJoe Lawrence * be safe. Use of this option will also prevent removal of 138*439e7271SJoe Lawrence * the patch. 139*439e7271SJoe Lawrence * 140*439e7271SJoe Lawrence * See Documentation/livepatch/livepatch.txt for more details. 141*439e7271SJoe Lawrence */ 142*439e7271SJoe Lawrence patch.immediate = true; 143*439e7271SJoe Lawrence pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n"); 144*439e7271SJoe Lawrence } 145*439e7271SJoe Lawrence 146*439e7271SJoe Lawrence ret = klp_register_patch(&patch); 147*439e7271SJoe Lawrence if (ret) 148*439e7271SJoe Lawrence return ret; 149*439e7271SJoe Lawrence ret = klp_enable_patch(&patch); 150*439e7271SJoe Lawrence if (ret) { 151*439e7271SJoe Lawrence WARN_ON(klp_unregister_patch(&patch)); 152*439e7271SJoe Lawrence return ret; 153*439e7271SJoe Lawrence } 154*439e7271SJoe Lawrence return 0; 155*439e7271SJoe Lawrence } 156*439e7271SJoe Lawrence 157*439e7271SJoe Lawrence static void livepatch_shadow_fix2_exit(void) 158*439e7271SJoe Lawrence { 159*439e7271SJoe Lawrence /* Cleanup any existing SV_COUNTER shadow variables */ 160*439e7271SJoe Lawrence klp_shadow_free_all(SV_COUNTER); 161*439e7271SJoe Lawrence 162*439e7271SJoe Lawrence WARN_ON(klp_unregister_patch(&patch)); 163*439e7271SJoe Lawrence } 164*439e7271SJoe Lawrence 165*439e7271SJoe Lawrence module_init(livepatch_shadow_fix2_init); 166*439e7271SJoe Lawrence module_exit(livepatch_shadow_fix2_exit); 167*439e7271SJoe Lawrence MODULE_LICENSE("GPL"); 168*439e7271SJoe Lawrence MODULE_INFO(livepatch, "Y"); 169