1 /******************************************************************************
2 * dmpatch.c ---- patch for device-mapper
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mutex.h>
25 #include <linux/mempool.h>
26 #include <linux/delay.h>
27 #include <linux/wait.h>
28 #include <linux/slab.h>
32 #define magic_sig 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF
34 typedef int (*kprobe_reg_pf
)(void *);
35 typedef void (*kprobe_unreg_pf
)(void *);
36 typedef int (*printk_pf
)(const char *fmt
, ...);
37 typedef int (*set_memory_attr_pf
)(unsigned long addr
, int numpages
);
40 typedef struct ko_param
42 unsigned char magic
[16];
43 unsigned long struct_size
;
45 unsigned long printk_addr
;
46 unsigned long ro_addr
;
47 unsigned long rw_addr
;
48 unsigned long reg_kprobe_addr
;
49 unsigned long unreg_kprobe_addr
;
50 unsigned long sym_get_addr
;
51 unsigned long sym_get_size
;
52 unsigned long sym_put_addr
;
53 unsigned long sym_put_size
;
54 unsigned long padding
[3];
59 static printk_pf kprintf
= NULL
;
60 static set_memory_attr_pf set_mem_ro
= NULL
;
61 static set_memory_attr_pf set_mem_rw
= NULL
;
62 static kprobe_reg_pf reg_kprobe
= NULL
;
63 static kprobe_unreg_pf unreg_kprobe
= NULL
;
65 static volatile ko_param g_ko_param
=
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71 #if defined(CONFIG_X86_64)
72 #define PATCH_OP_POS 3
73 #define CODE_MATCH(code, i) \
74 (code[i] == 0x40 && code[i + 1] == 0x80 && code[i + 2] == 0xce && code[i + 3] == 0x80)
75 #elif defined(CONFIG_X86_32)
76 #define PATCH_OP_POS 2
77 #define CODE_MATCH(code, i) \
78 (code[i] == 0x80 && code[i + 1] == 0xca && code[i + 2] == 0x80 && code[i + 3] == 0xe8)
80 #error "unsupported arch"
83 #define vdebug(fmt, args...) if(kprintf) kprintf(KERN_ERR fmt, ##args)
85 static unsigned char *g_get_patch
[MAX_PATCH
] = { NULL
};
86 static unsigned char *g_put_patch
[MAX_PATCH
] = { NULL
};
88 static void notrace
dmpatch_restore_code(unsigned char *opCode
)
94 align
= (unsigned long)opCode
/ g_ko_param
.pgsize
* g_ko_param
.pgsize
;
101 static int notrace dmpatch_replace_code
107 unsigned char **patch
113 unsigned char *opCode
= (unsigned char *)addr
;
115 vdebug("patch for %s 0x%lx %d\n", desc
, addr
, (int)size
);
117 for (i
= 0; i
< (int)size
- 4; i
++)
119 if (CODE_MATCH(opCode
, i
) && cnt
< MAX_PATCH
)
121 patch
[cnt
] = opCode
+ i
+ PATCH_OP_POS
;
126 if (cnt
!= expect
|| cnt
>= MAX_PATCH
)
128 vdebug("patch error: cnt=%d expect=%d\n", cnt
, expect
);
133 for (i
= 0; i
< cnt
; i
++)
136 align
= (unsigned long)opCode
/ g_ko_param
.pgsize
* g_ko_param
.pgsize
;
138 set_mem_rw(align
, 1);
140 set_mem_ro(align
, 1);
146 static int notrace
dmpatch_init(void)
151 kprintf
= (printk_pf
)(g_ko_param
.printk_addr
);
153 vdebug("dmpatch_init start pagesize=%lu ...\n", g_ko_param
.pgsize
);
155 if (g_ko_param
.struct_size
!= sizeof(ko_param
))
157 vdebug("Invalid struct size %d %d\n", (int)g_ko_param
.struct_size
, (int)sizeof(ko_param
));
161 if (g_ko_param
.sym_get_addr
== 0 || g_ko_param
.sym_put_addr
== 0 ||
162 g_ko_param
.ro_addr
== 0 || g_ko_param
.rw_addr
== 0)
167 set_mem_ro
= (set_memory_attr_pf
)(g_ko_param
.ro_addr
);
168 set_mem_rw
= (set_memory_attr_pf
)(g_ko_param
.rw_addr
);
169 reg_kprobe
= (kprobe_reg_pf
)g_ko_param
.reg_kprobe_addr
;
170 unreg_kprobe
= (kprobe_unreg_pf
)g_ko_param
.unreg_kprobe_addr
;
172 r
= dmpatch_replace_code(g_ko_param
.sym_get_addr
, g_ko_param
.sym_get_size
, 2, "dm_get_table_device", g_get_patch
);
178 vdebug("patch dm_get_table_device success\n");
180 r
= dmpatch_replace_code(g_ko_param
.sym_put_addr
, g_ko_param
.sym_put_size
, 1, "dm_put_table_device", g_put_patch
);
186 vdebug("patch dm_put_table_device success\n");
188 vdebug("#####################################\n");
189 vdebug("######## dm patch success ###########\n");
190 vdebug("#####################################\n");
197 static void notrace
dmpatch_exit(void)
201 for (i
= 0; i
< MAX_PATCH
; i
++)
203 dmpatch_restore_code(g_get_patch
[i
]);
204 dmpatch_restore_code(g_put_patch
[i
]);
207 vdebug("dmpatch_exit success\n");
210 module_init(dmpatch_init
);
211 module_exit(dmpatch_exit
);
214 MODULE_DESCRIPTION("dmpatch driver");
215 MODULE_AUTHOR("longpanda <admin@ventoy.net>");
216 MODULE_LICENSE("GPL");