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-fix1.c - Shadow variables, livepatch demo 20*439e7271SJoe Lawrence * 21*439e7271SJoe Lawrence * Purpose 22*439e7271SJoe Lawrence * ------- 23*439e7271SJoe Lawrence * 24*439e7271SJoe Lawrence * Fixes the memory leak introduced in livepatch-shadow-mod through the 25*439e7271SJoe Lawrence * use of a shadow variable. This fix demonstrates the "extending" of 26*439e7271SJoe Lawrence * short-lived data structures by patching its allocation and release 27*439e7271SJoe Lawrence * functions. 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 47*439e7271SJoe Lawrence /* Allocate new dummies every second */ 48*439e7271SJoe Lawrence #define ALLOC_PERIOD 1 49*439e7271SJoe Lawrence /* Check for expired dummies after a few new ones have been allocated */ 50*439e7271SJoe Lawrence #define CLEANUP_PERIOD (3 * ALLOC_PERIOD) 51*439e7271SJoe Lawrence /* Dummies expire after a few cleanup instances */ 52*439e7271SJoe Lawrence #define EXPIRE_PERIOD (4 * CLEANUP_PERIOD) 53*439e7271SJoe Lawrence 54*439e7271SJoe Lawrence struct dummy { 55*439e7271SJoe Lawrence struct list_head list; 56*439e7271SJoe Lawrence unsigned long jiffies_expire; 57*439e7271SJoe Lawrence }; 58*439e7271SJoe Lawrence 59*439e7271SJoe Lawrence struct dummy *livepatch_fix1_dummy_alloc(void) 60*439e7271SJoe Lawrence { 61*439e7271SJoe Lawrence struct dummy *d; 62*439e7271SJoe Lawrence void *leak; 63*439e7271SJoe Lawrence 64*439e7271SJoe Lawrence d = kzalloc(sizeof(*d), GFP_KERNEL); 65*439e7271SJoe Lawrence if (!d) 66*439e7271SJoe Lawrence return NULL; 67*439e7271SJoe Lawrence 68*439e7271SJoe Lawrence d->jiffies_expire = jiffies + 69*439e7271SJoe Lawrence msecs_to_jiffies(1000 * EXPIRE_PERIOD); 70*439e7271SJoe Lawrence 71*439e7271SJoe Lawrence /* 72*439e7271SJoe Lawrence * Patch: save the extra memory location into a SV_LEAK shadow 73*439e7271SJoe Lawrence * variable. A patched dummy_free routine can later fetch this 74*439e7271SJoe Lawrence * pointer to handle resource release. 75*439e7271SJoe Lawrence */ 76*439e7271SJoe Lawrence leak = kzalloc(sizeof(int), GFP_KERNEL); 77*439e7271SJoe Lawrence klp_shadow_alloc(d, SV_LEAK, &leak, sizeof(leak), GFP_KERNEL); 78*439e7271SJoe Lawrence 79*439e7271SJoe Lawrence pr_info("%s: dummy @ %p, expires @ %lx\n", 80*439e7271SJoe Lawrence __func__, d, d->jiffies_expire); 81*439e7271SJoe Lawrence 82*439e7271SJoe Lawrence return d; 83*439e7271SJoe Lawrence } 84*439e7271SJoe Lawrence 85*439e7271SJoe Lawrence void livepatch_fix1_dummy_free(struct dummy *d) 86*439e7271SJoe Lawrence { 87*439e7271SJoe Lawrence void **shadow_leak, *leak; 88*439e7271SJoe Lawrence 89*439e7271SJoe Lawrence /* 90*439e7271SJoe Lawrence * Patch: fetch the saved SV_LEAK shadow variable, detach and 91*439e7271SJoe Lawrence * free it. Note: handle cases where this shadow variable does 92*439e7271SJoe Lawrence * not exist (ie, dummy structures allocated before this livepatch 93*439e7271SJoe Lawrence * was loaded.) 94*439e7271SJoe Lawrence */ 95*439e7271SJoe Lawrence shadow_leak = klp_shadow_get(d, SV_LEAK); 96*439e7271SJoe Lawrence if (shadow_leak) { 97*439e7271SJoe Lawrence leak = *shadow_leak; 98*439e7271SJoe Lawrence klp_shadow_free(d, SV_LEAK); 99*439e7271SJoe Lawrence kfree(leak); 100*439e7271SJoe Lawrence pr_info("%s: dummy @ %p, prevented leak @ %p\n", 101*439e7271SJoe Lawrence __func__, d, leak); 102*439e7271SJoe Lawrence } else { 103*439e7271SJoe Lawrence pr_info("%s: dummy @ %p leaked!\n", __func__, d); 104*439e7271SJoe Lawrence } 105*439e7271SJoe Lawrence 106*439e7271SJoe Lawrence kfree(d); 107*439e7271SJoe Lawrence } 108*439e7271SJoe Lawrence 109*439e7271SJoe Lawrence static struct klp_func funcs[] = { 110*439e7271SJoe Lawrence { 111*439e7271SJoe Lawrence .old_name = "dummy_alloc", 112*439e7271SJoe Lawrence .new_func = livepatch_fix1_dummy_alloc, 113*439e7271SJoe Lawrence }, 114*439e7271SJoe Lawrence { 115*439e7271SJoe Lawrence .old_name = "dummy_free", 116*439e7271SJoe Lawrence .new_func = livepatch_fix1_dummy_free, 117*439e7271SJoe Lawrence }, { } 118*439e7271SJoe Lawrence }; 119*439e7271SJoe Lawrence 120*439e7271SJoe Lawrence static struct klp_object objs[] = { 121*439e7271SJoe Lawrence { 122*439e7271SJoe Lawrence .name = "livepatch_shadow_mod", 123*439e7271SJoe Lawrence .funcs = funcs, 124*439e7271SJoe Lawrence }, { } 125*439e7271SJoe Lawrence }; 126*439e7271SJoe Lawrence 127*439e7271SJoe Lawrence static struct klp_patch patch = { 128*439e7271SJoe Lawrence .mod = THIS_MODULE, 129*439e7271SJoe Lawrence .objs = objs, 130*439e7271SJoe Lawrence }; 131*439e7271SJoe Lawrence 132*439e7271SJoe Lawrence static int livepatch_shadow_fix1_init(void) 133*439e7271SJoe Lawrence { 134*439e7271SJoe Lawrence int ret; 135*439e7271SJoe Lawrence 136*439e7271SJoe Lawrence if (!klp_have_reliable_stack() && !patch.immediate) { 137*439e7271SJoe Lawrence /* 138*439e7271SJoe Lawrence * WARNING: Be very careful when using 'patch.immediate' in 139*439e7271SJoe Lawrence * your patches. It's ok to use it for simple patches like 140*439e7271SJoe Lawrence * this, but for more complex patches which change function 141*439e7271SJoe Lawrence * semantics, locking semantics, or data structures, it may not 142*439e7271SJoe Lawrence * be safe. Use of this option will also prevent removal of 143*439e7271SJoe Lawrence * the patch. 144*439e7271SJoe Lawrence * 145*439e7271SJoe Lawrence * See Documentation/livepatch/livepatch.txt for more details. 146*439e7271SJoe Lawrence */ 147*439e7271SJoe Lawrence patch.immediate = true; 148*439e7271SJoe Lawrence pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n"); 149*439e7271SJoe Lawrence } 150*439e7271SJoe Lawrence 151*439e7271SJoe Lawrence ret = klp_register_patch(&patch); 152*439e7271SJoe Lawrence if (ret) 153*439e7271SJoe Lawrence return ret; 154*439e7271SJoe Lawrence ret = klp_enable_patch(&patch); 155*439e7271SJoe Lawrence if (ret) { 156*439e7271SJoe Lawrence WARN_ON(klp_unregister_patch(&patch)); 157*439e7271SJoe Lawrence return ret; 158*439e7271SJoe Lawrence } 159*439e7271SJoe Lawrence return 0; 160*439e7271SJoe Lawrence } 161*439e7271SJoe Lawrence 162*439e7271SJoe Lawrence static void livepatch_shadow_fix1_exit(void) 163*439e7271SJoe Lawrence { 164*439e7271SJoe Lawrence /* Cleanup any existing SV_LEAK shadow variables */ 165*439e7271SJoe Lawrence klp_shadow_free_all(SV_LEAK); 166*439e7271SJoe Lawrence 167*439e7271SJoe Lawrence WARN_ON(klp_unregister_patch(&patch)); 168*439e7271SJoe Lawrence } 169*439e7271SJoe Lawrence 170*439e7271SJoe Lawrence module_init(livepatch_shadow_fix1_init); 171*439e7271SJoe Lawrence module_exit(livepatch_shadow_fix1_exit); 172*439e7271SJoe Lawrence MODULE_LICENSE("GPL"); 173*439e7271SJoe Lawrence MODULE_INFO(livepatch, "Y"); 174