2019-05-19 16:51:43 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2017-08-31 23:37:41 +03:00
/*
* Copyright ( C ) 2017 Joe Lawrence < joe . lawrence @ redhat . com >
*/
/*
* livepatch - shadow - fix2 . c - Shadow variables , livepatch demo
*
* Purpose
* - - - - - - -
*
* Adds functionality to livepatch - shadow - mod ' s in - flight data
* structures through a shadow variable . The livepatch patches a
* routine that periodically inspects data structures , incrementing a
* per - data - structure counter , creating the counter if needed .
*
*
* Usage
* - - - - -
*
* This module is not intended to be standalone . See the " Usage "
* section of livepatch - shadow - mod . c .
*/
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
# include <linux/module.h>
# include <linux/kernel.h>
# include <linux/livepatch.h>
# include <linux/slab.h>
/* Shadow variable enums */
# define SV_LEAK 1
# define SV_COUNTER 2
struct dummy {
struct list_head list ;
unsigned long jiffies_expire ;
} ;
2019-01-24 04:48:16 +03:00
static bool livepatch_fix2_dummy_check ( struct dummy * d , unsigned long jiffies )
2017-08-31 23:37:41 +03:00
{
int * shadow_count ;
/*
* Patch : handle in - flight dummy structures , if they do not
* already have a SV_COUNTER shadow variable , then attach a
* new one .
*/
shadow_count = klp_shadow_get_or_alloc ( d , SV_COUNTER ,
2018-04-16 14:36:46 +03:00
sizeof ( * shadow_count ) , GFP_NOWAIT ,
NULL , NULL ) ;
2017-08-31 23:37:41 +03:00
if ( shadow_count )
* shadow_count + = 1 ;
return time_after ( jiffies , d - > jiffies_expire ) ;
}
2018-04-16 14:36:47 +03:00
static void livepatch_fix2_dummy_leak_dtor ( void * obj , void * shadow_data )
{
void * d = obj ;
2020-01-16 18:31:42 +03:00
int * * shadow_leak = shadow_data ;
2018-04-16 14:36:47 +03:00
pr_info ( " %s: dummy @ %p, prevented leak @ %p \n " ,
__func__ , d , * shadow_leak ) ;
2022-03-20 04:51:43 +03:00
kfree ( * shadow_leak ) ;
2018-04-16 14:36:47 +03:00
}
2019-01-24 04:48:16 +03:00
static void livepatch_fix2_dummy_free ( struct dummy * d )
2017-08-31 23:37:41 +03:00
{
2020-01-16 18:31:42 +03:00
int * * shadow_leak ;
2017-08-31 23:37:41 +03:00
int * shadow_count ;
/* Patch: copy the memory leak patch from the fix1 module. */
shadow_leak = klp_shadow_get ( d , SV_LEAK ) ;
2018-04-16 14:36:47 +03:00
if ( shadow_leak )
klp_shadow_free ( d , SV_LEAK , livepatch_fix2_dummy_leak_dtor ) ;
else
2017-08-31 23:37:41 +03:00
pr_info ( " %s: dummy @ %p leaked! \n " , __func__ , d ) ;
/*
* Patch : fetch the SV_COUNTER shadow variable and display
* the final count . Detach the shadow variable .
*/
shadow_count = klp_shadow_get ( d , SV_COUNTER ) ;
if ( shadow_count ) {
pr_info ( " %s: dummy @ %p, check counter = %d \n " ,
__func__ , d , * shadow_count ) ;
2018-04-16 14:36:47 +03:00
klp_shadow_free ( d , SV_COUNTER , NULL ) ;
2017-08-31 23:37:41 +03:00
}
kfree ( d ) ;
}
static struct klp_func funcs [ ] = {
{
. old_name = " dummy_check " ,
. new_func = livepatch_fix2_dummy_check ,
} ,
{
. old_name = " dummy_free " ,
. new_func = livepatch_fix2_dummy_free ,
} , { }
} ;
static struct klp_object objs [ ] = {
{
. name = " livepatch_shadow_mod " ,
. funcs = funcs ,
} , { }
} ;
static struct klp_patch patch = {
. mod = THIS_MODULE ,
. objs = objs ,
} ;
static int livepatch_shadow_fix2_init ( void )
{
2019-01-09 15:43:23 +03:00
return klp_enable_patch ( & patch ) ;
2017-08-31 23:37:41 +03:00
}
static void livepatch_shadow_fix2_exit ( void )
{
/* Cleanup any existing SV_COUNTER shadow variables */
2018-04-16 14:36:47 +03:00
klp_shadow_free_all ( SV_COUNTER , NULL ) ;
2017-08-31 23:37:41 +03:00
}
module_init ( livepatch_shadow_fix2_init ) ;
module_exit ( livepatch_shadow_fix2_exit ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_INFO ( livepatch , " Y " ) ;