"vscode:/vscode.git/clone" did not exist on "8c1d91e150af55da79c70ea3a0be5844cdd2e0f0"
dmpatch.c 15.7 KB
Newer Older
longpanda's avatar
longpanda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/******************************************************************************
 * dmpatch.c  ---- patch for device-mapper
 *
 * Copyright (c) 2021, longpanda <admin@ventoy.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
#include <linux/mutex.h>
#include <linux/mempool.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/slab.h>

#define MAX_PATCH   4

#define magic_sig 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF

typedef int (*kprobe_reg_pf)(void *);
typedef void (*kprobe_unreg_pf)(void *);
typedef int (*printk_pf)(const char *fmt, ...);
typedef int (*set_memory_attr_pf)(unsigned long addr, int numpages);

#pragma pack(1)
typedef struct ko_param
{
    unsigned char magic[16];
    unsigned long struct_size;
    unsigned long pgsize;
    unsigned long printk_addr;
    unsigned long ro_addr;
    unsigned long rw_addr;
    unsigned long reg_kprobe_addr;
    unsigned long unreg_kprobe_addr;
    unsigned long sym_get_addr;
    unsigned long sym_get_size;
    unsigned long sym_put_addr;
    unsigned long sym_put_size;
longpanda's avatar
longpanda committed
54
    unsigned long kv_major;
55
    unsigned long ibt;
56
57
58
    unsigned long kv_minor;
    unsigned long blkdev_get_addr;
    unsigned long blkdev_put_addr;
59
60
    unsigned long bdev_open_addr;
    unsigned long kv_subminor;
61
    unsigned long padding[1];
longpanda's avatar
longpanda committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
}ko_param;

#pragma pack()

static printk_pf kprintf = NULL;
static set_memory_attr_pf set_mem_ro = NULL;
static set_memory_attr_pf set_mem_rw = NULL;
static kprobe_reg_pf reg_kprobe = NULL;
static kprobe_unreg_pf unreg_kprobe = NULL;

static volatile ko_param g_ko_param = 
{
    { magic_sig },
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

78
#if defined(CONFIG_X86_64)
longpanda's avatar
longpanda committed
79
80
#define PATCH_OP_POS1    3
#define CODE_MATCH1(code, i) \
longpanda's avatar
longpanda committed
81
    (code[i] == 0x40 && code[i + 1] == 0x80 && code[i + 2] == 0xce && code[i + 3] == 0x80)
longpanda's avatar
longpanda committed
82
83
84
85

#define PATCH_OP_POS2    1
#define CODE_MATCH2(code, i) \
    (code[i] == 0x0C && code[i + 1] == 0x80 && code[i + 2] == 0x89 && code[i + 3] == 0xC6)
86
87
88
89
90
91
92
93
    
#define PATCH_OP_POS3    4
#define CODE_MATCH3(code, i) \
    (code[i] == 0x44 && code[i + 1] == 0x89 && code[i + 2] == 0xe8 && code[i + 3] == 0x0c && code[i + 4] == 0x80)




longpanda's avatar
longpanda committed
94

95
#elif defined(CONFIG_X86_32)
longpanda's avatar
longpanda committed
96
97
#define PATCH_OP_POS1    2
#define CODE_MATCH1(code, i) \
98
    (code[i] == 0x80 && code[i + 1] == 0xca && code[i + 2] == 0x80 && code[i + 3] == 0xe8)
longpanda's avatar
longpanda committed
99

100
101
102
103
104
#define PATCH_OP_POS2    PATCH_OP_POS1
#define CODE_MATCH2      CODE_MATCH1
#define PATCH_OP_POS3    PATCH_OP_POS1
#define CODE_MATCH3      CODE_MATCH1

longpanda's avatar
longpanda committed
105

106
107
108
#else
#error "unsupported arch"
#endif
longpanda's avatar
longpanda committed
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifdef VTOY_IBT
#ifdef CONFIG_X86_64
/* Using 64-bit values saves one instruction clearing the high half of low */
#define DECLARE_ARGS(val, low, high)	unsigned long low, high
#define EAX_EDX_VAL(val, low, high)	((low) | (high) << 32)
#define EAX_EDX_RET(val, low, high)	"=a" (low), "=d" (high)
#else
#define DECLARE_ARGS(val, low, high)	unsigned long long val
#define EAX_EDX_VAL(val, low, high)	(val)
#define EAX_EDX_RET(val, low, high)	"=A" (val)
#endif

#define	EX_TYPE_WRMSR			 8
#define	EX_TYPE_RDMSR			 9
#define MSR_IA32_S_CET			0x000006a2 /* kernel mode cet */
#define CET_ENDBR_EN			(1ULL << 2)

/* Exception table entry */
#ifdef __ASSEMBLY__

#define _ASM_EXTABLE_TYPE(from, to, type)			\
	.pushsection "__ex_table","a" ;				\
	.balign 4 ;						\
	.long (from) - . ;					\
	.long (to) - . ;					\
	.long type ;						\
	.popsection

#else /* ! __ASSEMBLY__ */

#define _ASM_EXTABLE_TYPE(from, to, type)			\
	" .pushsection \"__ex_table\",\"a\"\n"			\
	" .balign 4\n"						\
	" .long (" #from ") - .\n"				\
	" .long (" #to ") - .\n"				\
	" .long " __stringify(type) " \n"			\
	" .popsection\n"

#endif /* __ASSEMBLY__ */
#endif /* VTOY_IBT */






longpanda's avatar
longpanda committed
156
157
#define vdebug(fmt, args...) if(kprintf) kprintf(KERN_ERR fmt, ##args)

158
static unsigned int g_claim_ptr = 0;
159
160
161
static unsigned char *g_get_patch[MAX_PATCH] = { NULL };
static unsigned char *g_put_patch[MAX_PATCH] = { NULL };

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
static int notrace dmpatch_kv_above(unsigned long Major, unsigned long Minor, unsigned long SubMinor)
{
    if (g_ko_param.kv_major != Major)
    {
        return (g_ko_param.kv_major > Major) ? 1 : 0;
    }

    if (g_ko_param.kv_minor != Minor)
    {
        return (g_ko_param.kv_minor > Minor) ? 1 : 0;
    }

    if (g_ko_param.kv_subminor != SubMinor)
    {
        return (g_ko_param.kv_subminor > SubMinor) ? 1 : 0;
    }

    return 1;
}

182
static void notrace dmpatch_restore_code(int bytes, unsigned char *opCode, unsigned int code)
183
184
185
186
187
188
189
{
    unsigned long align;

    if (opCode)
    {
        align = (unsigned long)opCode / g_ko_param.pgsize * g_ko_param.pgsize;
        set_mem_rw(align, 1);
190
191
192
193
194
195
196
197
        if (bytes == 1)
        {
            *opCode = (unsigned char)code;            
        }
        else
        {
            *(unsigned int *)opCode = code;
        }
198
199
200
201
202
203
        set_mem_ro(align, 1);        
    }
}

static int notrace dmpatch_replace_code
(
longpanda's avatar
longpanda committed
204
    int style,
205
206
207
208
209
210
    unsigned long addr, 
    unsigned long size, 
    int expect, 
    const char *desc, 
    unsigned char **patch
)
longpanda's avatar
longpanda committed
211
212
213
214
215
216
{
    int i = 0;
    int cnt = 0;
    unsigned long align;
    unsigned char *opCode = (unsigned char *)addr;

longpanda's avatar
longpanda committed
217
    vdebug("patch for %s style[%d] 0x%lx %d\n", desc, style, addr, (int)size);
longpanda's avatar
longpanda committed
218

219
    for (i = 0; i < (int)size - 8; i++)
longpanda's avatar
longpanda committed
220
    {
longpanda's avatar
longpanda committed
221
        if (style == 1)
longpanda's avatar
longpanda committed
222
        {
longpanda's avatar
longpanda committed
223
224
225
226
227
228
            if (CODE_MATCH1(opCode, i) && cnt < MAX_PATCH)
            {
                patch[cnt] = opCode + i + PATCH_OP_POS1;
                cnt++;
            }
        }
229
        else if (style == 2)
longpanda's avatar
longpanda committed
230
231
232
233
234
235
        {
            if (CODE_MATCH2(opCode, i) && cnt < MAX_PATCH)
            {
                patch[cnt] = opCode + i + PATCH_OP_POS2;
                cnt++;
            }
longpanda's avatar
longpanda committed
236
        }
237
238
239
240
241
242
243
244
        else if (style == 3)
        {
            if (CODE_MATCH3(opCode, i) && cnt < MAX_PATCH)
            {
                patch[cnt] = opCode + i + PATCH_OP_POS3;
                cnt++;
            }
        }
longpanda's avatar
longpanda committed
245
246
    }

247
248
249
250




longpanda's avatar
longpanda committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    if (cnt != expect || cnt >= MAX_PATCH)
    {
        vdebug("patch error: cnt=%d expect=%d\n", cnt, expect);
        return 1;
    }


    for (i = 0; i < cnt; i++)
    {
        opCode = patch[i];
        align = (unsigned long)opCode / g_ko_param.pgsize * g_ko_param.pgsize;

        set_mem_rw(align, 1);
        *opCode = 0;
        set_mem_ro(align, 1);
    }

    return 0;
}

271
static unsigned long notrace dmpatch_find_call_offset(unsigned long addr, unsigned long size, unsigned long func)
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
    unsigned long i = 0;
    unsigned long dest;
    unsigned char *opCode = NULL;
    unsigned char aucOffset[8] = { 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF };

    opCode = (unsigned char *)addr;
    
    for (i = 0; i + 4 < size; i++)
    {
        if (opCode[i] == 0xE8)
        {
            aucOffset[0] = opCode[i + 1];
            aucOffset[1] = opCode[i + 2];
            aucOffset[2] = opCode[i + 3];
            aucOffset[3] = opCode[i + 4];

            dest = addr + i + 5 + *(unsigned long *)aucOffset;
            if (dest == func)
            {
                return i;
            }
        }
    }

    return 0;
}

300
static unsigned int notrace dmpatch_patch_claim_ptr(void)
301
302
303
{
    unsigned long i = 0;
    unsigned long t = 0;
304
305
306
    unsigned long offset1 = 0;
    unsigned long offset2 = 0;
    unsigned long align = 0;
307
308
309
310
311
312
313
314
315
316
317
318
    unsigned char *opCode = NULL;

    opCode = (unsigned char *)g_ko_param.sym_get_addr;
    for (i = 0; i < 4; i++)
    {
        vdebug("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
            opCode[i + 0], opCode[i + 1], opCode[i + 2], opCode[i + 3],
            opCode[i + 4], opCode[i + 5], opCode[i + 6], opCode[i + 7],
            opCode[i + 8], opCode[i + 9], opCode[i + 10], opCode[i + 11],
            opCode[i + 12], opCode[i + 13], opCode[i + 14], opCode[i + 15]);
    }

319
    if (dmpatch_kv_above(6, 7, 0)) /* >= 6.7 kernel */
320
    {
321
322
323
324
325
326
327
        vdebug("Get addr: 0x%lx %lu 0x%lx\n", g_ko_param.sym_get_addr, g_ko_param.sym_get_size, g_ko_param.bdev_open_addr);
        offset1 = dmpatch_find_call_offset(g_ko_param.sym_get_addr, g_ko_param.sym_get_size, g_ko_param.bdev_open_addr);
        if (offset1 == 0)
        {
            vdebug("call bdev_open_addr Not found\n");
            return 1;
        }
328
    }
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    else
    {
        vdebug("Get addr: 0x%lx %lu 0x%lx\n", g_ko_param.sym_get_addr, g_ko_param.sym_get_size, g_ko_param.blkdev_get_addr);
        vdebug("Put addr: 0x%lx %lu 0x%lx\n", g_ko_param.sym_put_addr, g_ko_param.sym_put_size, g_ko_param.blkdev_put_addr);        

        offset1 = dmpatch_find_call_offset(g_ko_param.sym_get_addr, g_ko_param.sym_get_size, g_ko_param.blkdev_get_addr);
        offset2 = dmpatch_find_call_offset(g_ko_param.sym_put_addr, g_ko_param.sym_put_size, g_ko_param.blkdev_put_addr);
        if (offset1 == 0 || offset2 == 0)
        {
            vdebug("call blkdev_get or blkdev_put Not found, %lu %lu\n", offset1, offset2);
            return 1;
        }
    }
    
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    vdebug("call addr1:0x%lx  call addr2:0x%lx\n", 
        g_ko_param.sym_get_addr + offset1, 
        g_ko_param.sym_put_addr + offset2);
    
    opCode = (unsigned char *)g_ko_param.sym_get_addr;
    for (i = offset1 - 1, t = 0; (i > 0) && (t < 24); i--, t++)
    {
        /* rdx */
        if (opCode[i] == 0x48 && opCode[i + 1] == 0xc7 && opCode[i + 2] == 0xc2)
        {
            g_claim_ptr = *(unsigned int *)(opCode + i + 3);
            g_get_patch[0] = opCode + i + 3;
            vdebug("claim_ptr(%08X) found at get addr 0x%lx\n", g_claim_ptr, g_ko_param.sym_get_addr + i + 3);
            break;
        }
    }

    if (g_claim_ptr == 0)
    {
        vdebug("Claim_ptr not found in get\n");
        return 1;
    }

366
    
367
368
369
370
371
    align = (unsigned long)g_get_patch[0] / g_ko_param.pgsize * g_ko_param.pgsize;
    set_mem_rw(align, 1);
    *(unsigned int *)(g_get_patch[0]) = 0;
    set_mem_ro(align, 1);  

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

    if (offset2 > 0)
    {        
        opCode = (unsigned char *)g_ko_param.sym_put_addr;
        for (i = offset2 - 1, t = 0; (i > 0) && (t < 24); i--, t++)
        {
            /* rsi */
            if (opCode[i] == 0x48 && opCode[i + 1] == 0xc7 && opCode[i + 2] == 0xc6)
            {
                if (*(unsigned int *)(opCode + i + 3) == g_claim_ptr)
                {
                    vdebug("claim_ptr found at put addr 0x%lx\n", g_ko_param.sym_put_addr + i + 3);
                    g_put_patch[0] = opCode + i + 3;
                    break;                
                }
            }
        }    

        if (g_put_patch[0] == 0)
        {
            vdebug("Claim_ptr not found in put\n");
            return 1;
        }
        
        align = (unsigned long)g_put_patch[0] / g_ko_param.pgsize * g_ko_param.pgsize;
        set_mem_rw(align, 1);
        *(unsigned int *)(g_put_patch[0]) = 0;
        set_mem_ro(align, 1);   
    }
401
402
403
404

    return 0;
}

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifdef VTOY_IBT
static __always_inline unsigned long long dmpatch_rdmsr(unsigned int msr)
{
	DECLARE_ARGS(val, low, high);

	asm volatile("1: rdmsr\n"
		     "2:\n"
		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_RDMSR)
		     : EAX_EDX_RET(val, low, high) : "c" (msr));

	return EAX_EDX_VAL(val, low, high);
}

static __always_inline void dmpatch_wrmsr(unsigned int msr, u32 low, u32 high)
{
	asm volatile("1: wrmsr\n"
		     "2:\n"
		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_WRMSR)
		     : : "c" (msr), "a"(low), "d" (high) : "memory");
}

426
static u64 notrace dmpatch_ibt_save(void)
427
428
429
430
431
432
433
434
435
436
437
{
    u64 msr = 0;
    u64 val = 0;

    msr = dmpatch_rdmsr(MSR_IA32_S_CET);
    val = msr & ~CET_ENDBR_EN;
    dmpatch_wrmsr(MSR_IA32_S_CET, (u32)(val & 0xffffffffULL), (u32)(val >> 32));

    return msr;
}

438
static void notrace dmpatch_ibt_restore(u64 save)
439
440
441
442
443
444
445
446
447
448
449
{
	u64 msr;

    msr = dmpatch_rdmsr(MSR_IA32_S_CET);

	msr &= ~CET_ENDBR_EN;
	msr |= (save & CET_ENDBR_EN);

    dmpatch_wrmsr(MSR_IA32_S_CET, (u32)(msr & 0xffffffffULL), (u32)(msr >> 32));
}
#else
450
451
static u64 notrace dmpatch_ibt_save(void) { return 0; }
static void notrace dmpatch_ibt_restore(u64 save) { (void)save; }
452
453
#endif

longpanda's avatar
longpanda committed
454
455
456
457
static int notrace dmpatch_init(void)
{
    int r = 0;
    int rc = 0;
458
459
460
461
462
463
464
    u64 msr = 0;
    
    if (g_ko_param.ibt == 0x8888)
    {
        msr = dmpatch_ibt_save();
    }
    
longpanda's avatar
longpanda committed
465
466
    kprintf = (printk_pf)(g_ko_param.printk_addr); 

467
468
    vdebug("dmpatch_init start pagesize=%lu kernel=%lu.%lu.%lu ...\n", 
        g_ko_param.pgsize, g_ko_param.kv_major, g_ko_param.kv_minor, g_ko_param.kv_subminor);
longpanda's avatar
longpanda committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    
    if (g_ko_param.struct_size != sizeof(ko_param))
    {
        vdebug("Invalid struct size %d %d\n", (int)g_ko_param.struct_size, (int)sizeof(ko_param));
        return -EINVAL;
    }
    
    if (g_ko_param.sym_get_addr == 0 || g_ko_param.sym_put_addr == 0 || 
        g_ko_param.ro_addr == 0 || g_ko_param.rw_addr == 0)
    {
        return -EINVAL;
    }

    set_mem_ro = (set_memory_attr_pf)(g_ko_param.ro_addr);
    set_mem_rw = (set_memory_attr_pf)(g_ko_param.rw_addr);
    reg_kprobe = (kprobe_reg_pf)g_ko_param.reg_kprobe_addr;
    unreg_kprobe = (kprobe_unreg_pf)g_ko_param.unreg_kprobe_addr;

487
    if (dmpatch_kv_above(6, 5, 0)) /* >= kernel 6.5 */
longpanda's avatar
longpanda committed
488
    {
489
490
        vdebug("new interface patch dm_get_table_device...\n");
        r = dmpatch_patch_claim_ptr();
longpanda's avatar
longpanda committed
491
    }
492
    else
493
    {
494
495
496
497
498
499
500
501
502
503
504
505
        r = dmpatch_replace_code(1, g_ko_param.sym_get_addr, g_ko_param.sym_get_size, 2, "dm_get_table_device", g_get_patch);
        if (r && g_ko_param.kv_major >= 5)
        {
            vdebug("new2 patch dm_get_table_device...\n");
            r = dmpatch_replace_code(2, g_ko_param.sym_get_addr, g_ko_param.sym_get_size, 1, "dm_get_table_device", g_get_patch);
        }

        if (r && g_ko_param.kv_major >= 5)
        {
            vdebug("new3 patch dm_get_table_device...\n");
            r = dmpatch_replace_code(3, g_ko_param.sym_get_addr, g_ko_param.sym_get_size, 1, "dm_get_table_device", g_get_patch);
        }
506
    }
507

longpanda's avatar
longpanda committed
508
509
    if (r)
    {
510
        rc = -EFAULT;
longpanda's avatar
longpanda committed
511
512
513
514
        goto out;
    }
    vdebug("patch dm_get_table_device success\n");

515
    if (dmpatch_kv_above(6, 5, 0))
516
517
518
519
520
521
    {
        r = 0;
    }
    else
    {
        r = dmpatch_replace_code(1, g_ko_param.sym_put_addr, g_ko_param.sym_put_size, 1, "dm_put_table_device", g_put_patch);        
522
523
524
525
526
527
        if (r)
        {
            rc = -EFAULT;
            goto out;
        }
        vdebug("patch dm_put_table_device success\n");
528
    }
longpanda's avatar
longpanda committed
529
530
531
532
533

    vdebug("#####################################\n");
    vdebug("######## dm patch success ###########\n");
    vdebug("#####################################\n");

534
535
536
537
538
    if (g_ko_param.ibt == 0x8888)
    {
        dmpatch_ibt_restore(msr);
    }

longpanda's avatar
longpanda committed
539
540
541
542
543
544
545
out:

	return rc;
}

static void notrace dmpatch_exit(void)
{
546
    int i = 0;
547
548
549
550
551
552
    u64 msr;

    if (g_ko_param.ibt == 0x8888)
    {
        msr = dmpatch_ibt_save();
    }
553

554
    if (g_claim_ptr)
555
    {
556
        dmpatch_restore_code(4, g_get_patch[0], g_claim_ptr);
557
558
559
560
        if (g_put_patch[0])
        {
            dmpatch_restore_code(4, g_put_patch[0], g_claim_ptr);
        }
561
562
563
564
565
566
567
568
    }
    else
    {        
        for (i = 0; i < MAX_PATCH; i++)
        {
            dmpatch_restore_code(1, g_get_patch[i], 0x80);
            dmpatch_restore_code(1, g_put_patch[i], 0x80);
        }
569
    }
longpanda's avatar
longpanda committed
570

571
    vdebug("dmpatch_exit success\n");
572
573
574
575
576

    if (g_ko_param.ibt == 0x8888)
    {
        dmpatch_ibt_restore(msr);
    }
longpanda's avatar
longpanda committed
577
578
579
580
581
582
583
584
585
586
}

module_init(dmpatch_init);
module_exit(dmpatch_exit);


MODULE_DESCRIPTION("dmpatch driver");
MODULE_AUTHOR("longpanda <admin@ventoy.net>");
MODULE_LICENSE("GPL");