diff --git a/DMPATCH/dmpatch.c b/DMPATCH/dmpatch.c index e508a062..2e5d1a98 100644 --- a/DMPATCH/dmpatch.c +++ b/DMPATCH/dmpatch.c @@ -78,15 +78,25 @@ static volatile ko_param g_ko_param = #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) + +#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) + + + + #elif defined(CONFIG_X86_32) #define PATCH_OP_POS1 2 #define CODE_MATCH1(code, i) \ (code[i] == 0x80 && code[i + 1] == 0xca && code[i + 2] == 0x80 && code[i + 3] == 0xe8) -#define PATCH_OP_POS2 2 -#define CODE_MATCH2(code, i) \ - (code[i] == 0x80 && code[i + 1] == 0xca && code[i + 2] == 0x80 && code[i + 3] == 0xe8) +#define PATCH_OP_POS2 PATCH_OP_POS1 +#define CODE_MATCH2 CODE_MATCH1 +#define PATCH_OP_POS3 PATCH_OP_POS1 +#define CODE_MATCH3 CODE_MATCH1 + #else #error "unsupported arch" @@ -173,7 +183,7 @@ static int notrace dmpatch_replace_code vdebug("patch for %s style[%d] 0x%lx %d\n", desc, style, addr, (int)size); - for (i = 0; i < (int)size - 4; i++) + for (i = 0; i < (int)size - 8; i++) { if (style == 1) { @@ -183,7 +193,7 @@ static int notrace dmpatch_replace_code cnt++; } } - else + else if (style == 2) { if (CODE_MATCH2(opCode, i) && cnt < MAX_PATCH) { @@ -191,8 +201,20 @@ static int notrace dmpatch_replace_code cnt++; } } + else if (style == 3) + { + if (CODE_MATCH3(opCode, i) && cnt < MAX_PATCH) + { + patch[cnt] = opCode + i + PATCH_OP_POS3; + cnt++; + } + } } + + + + if (cnt != expect || cnt >= MAX_PATCH) { vdebug("patch error: cnt=%d expect=%d\n", cnt, expect); @@ -297,9 +319,16 @@ static int notrace dmpatch_init(void) 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("new patch dm_get_table_device...\n"); + 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); + } + if (r) { diff --git a/DMPATCH/readme.txt b/DMPATCH/readme.txt index c71e0699..c427d3a8 100644 --- a/DMPATCH/readme.txt +++ b/DMPATCH/readme.txt @@ -1,10 +1,12 @@ -1. install ubuntu 21.10 -2. apt-get install build-essential flex libncurses-dev linux-headers-generic linux-source libssl-dev ...... and so on -3. cp /lib/modules/5.13.0-23-generic/build/Module.symvers ./ -4. /boot/config-5.13.0-23-generic as .config make oldconfig +1. install ubuntu 22.04 5.15.0-25 +2. apt-get install build-essential flex libncurses-dev linux-headers-generic linux-source libssl-dev bison yacc vim libelf-dev ...... and so on +3. cp /lib/modules/5.15.0-25-generic/build/Module.symvers ./ +4. /boot/config-5.15.0-25-generic as .config make oldconfig 5. make menuconfig 1. close CONFIG_STACKPROTECTOR 2. close CONFIG_RETPOLINE + 3. close CONFIG_UBSAN_BOUNDS + 4. close CONFIG_UBSAN_ENUM 6. modify ./scripts/mod/modpost.c 1. skip add_srcversion (just return) diff --git a/DMPATCH/ubuntu_build.sh b/DMPATCH/ubuntu_build.sh index 995603bc..521b82a1 100644 --- a/DMPATCH/ubuntu_build.sh +++ b/DMPATCH/ubuntu_build.sh @@ -1,6 +1,6 @@ #!/bin/bash -FTPIP=168.0.0.209 +FTPIP=192.168.44.1 FTPUSR='a:a' rm -f dmpatch.c Makefile Makefile_IBT @@ -27,7 +27,7 @@ mkdir ./aa cp -a *.c aa/ cp -a Makefile aa/ -cd /home/panda/linux-source-5.13.0 +cd /home/panda/linux-source-5.15.0 make modules M=/home/panda/build/aa/ strip --strip-debug /home/panda/build/aa/dm_patch.ko cd - @@ -43,7 +43,7 @@ mkdir ./aa cp -a *.c aa/ cp -a Makefile_IBT aa/Makefile -cd /home/panda/linux-source-5.13.0 +cd /home/panda/linux-source-5.15.0 make modules M=/home/panda/build/aa/ strip --strip-debug /home/panda/build/aa/dm_patch_ibt.ko cd - diff --git a/IMG/cpio/ventoy/hook/suse/udev_disk_hook.sh b/IMG/cpio/ventoy/hook/suse/udev_disk_hook.sh index 67f36922..d5e8170d 100644 --- a/IMG/cpio/ventoy/hook/suse/udev_disk_hook.sh +++ b/IMG/cpio/ventoy/hook/suse/udev_disk_hook.sh @@ -54,6 +54,21 @@ if [ -z "$dmsetup_path" ]; then ventoy_os_install_dmsetup "/dev/${1:0:-1}" fi +if [ -f /proc/devices ]; then + vtlog "/proc/devices exist OK" +else + for i in 1 2 3 4 5 6 7 8 9; do + if [ -f /proc/devices ]; then + vtlog "/proc/devices exist OK now" + break + else + vtlog "/proc/devices NOT exist, wait $i" + $BUSYBOX_PATH/sleep 1 + fi + done +fi + + ventoy_udev_disk_common_hook $* # OK finish diff --git a/IMG/cpio/ventoy/hook/ventoy-hook-lib.sh b/IMG/cpio/ventoy/hook/ventoy-hook-lib.sh index e8567929..62c29967 100644 --- a/IMG/cpio/ventoy/hook/ventoy-hook-lib.sh +++ b/IMG/cpio/ventoy/hook/ventoy-hook-lib.sh @@ -209,11 +209,20 @@ ventoy_check_dm_module() { vtlog "modprobe failed, now try to insmod ko..." $FIND /lib/modules/ -name "dm-mod.ko*" | while read vtline; do - vtlog "insmode $vtline " + vtlog "insmod $vtline " $BUSYBOX_PATH/insmod $vtline >>$VTLOG 2>&1 + if [ $? -eq 0 ]; then + vtlog "insmod success" + else + vtlog "insmod failed, try decompress" + if echo $vtline | $GREP -q "\.zst"; then + $VTOY_PATH/tool/zstdcat $vtline > $VTOY_PATH/extract_dm_mod.ko + $BUSYBOX_PATH/insmod $VTOY_PATH/extract_dm_mod.ko >>$VTLOG 2>&1 + fi + fi done fi - + if $GREP -q 'device-mapper' /proc/devices; then vtlog "device-mapper found in /proc/devices after retry" $BUSYBOX_PATH/true; return @@ -292,6 +301,7 @@ ventoy_need_dm_patch() { } ventoy_dm_patch() { + vtDmPatchDebug=0 vtMType=$($BUSYBOX_PATH/uname -m) vtlog "######### ventoy_dm_patch ############" @@ -350,6 +360,15 @@ ventoy_dm_patch() { kprobe_unreg_addr=$($GREP ' unregister_kprobe$' /proc/kallsyms | $AWK '{print $1}') if [ "$VTOY_DEBUG_LEVEL" = "01" ]; then + vtDmPatchDebug=1 + fi + + if $GREP -q 'dmpatch_debug' /proc/cmdline; then + vtDmPatchDebug=1 + fi + + + if [ $vtDmPatchDebug -eq 1 ]; then printk_addr=$($GREP ' printk$' /proc/kallsyms | $AWK '{print $1}') if [ -z "$printk_addr" ]; then printk_addr=$($GREP ' _printk$' /proc/kallsyms | $AWK '{print $1}') @@ -384,14 +403,17 @@ ventoy_dm_patch() { return elif [ -d /lib/modules/$vtKv/kernel/fs ]; then vtModPath=$($FIND /lib/modules/$vtKv/kernel/fs/ -name "*.ko*" | $HEAD -n1) - else + elif [ -d /lib/modules/$vtKv/kernel ]; then vtModPath=$($FIND /lib/modules/$vtKv/kernel/ -name "xfs.ko*" | $HEAD -n1) + elif [ -d /lib/modules/$vtKv/initrd ]; then + vtModPath=$($FIND /lib/modules/$vtKv/initrd/ -name "xfs.ko*" | $HEAD -n1) fi - + + if [ -z "$vtModPath" ]; then - vtModPath=$($FIND /lib/modules/$vtKv/kernel/ -name "*.ko*" | $HEAD -n1) + vtModPath=$($FIND /lib/modules/$vtKv/ -name "*.ko*" | $HEAD -n1) fi - + vtModName=$($BUSYBOX_PATH/basename $vtModPath) vtlog "template module is $vtModPath $vtModName" @@ -405,6 +427,8 @@ ventoy_dm_patch() { $BUSYBOX_PATH/xzcat $vtModPath > $VTOY_PATH/$vtModName elif echo $vtModPath | $GREP -q "[.]ko[.]gz$"; then $BUSYBOX_PATH/zcat $vtModPath > $VTOY_PATH/$vtModName + elif echo $vtModPath | $GREP -q "[.]ko[.]zst$"; then + $VTOY_PATH/tool/zstdcat $vtModPath > $VTOY_PATH/$vtModName else vtlog "unsupport module type" return @@ -414,17 +438,21 @@ ventoy_dm_patch() { #step1: modify vermagic/mod crc/relocation vtlog "$VTOY_PATH/tool/vtoykmod -u $VTOY_PATH/tool/$vtKoName $VTOY_PATH/$vtModName $vtDebug" - $VTOY_PATH/tool/vtoykmod -u $VTOY_PATH/tool/$vtKoName $VTOY_PATH/$vtModName $vtDebug + $VTOY_PATH/tool/vtoykmod -u $VTOY_PATH/tool/$vtKoName $VTOY_PATH/$vtModName $vtDebug >>$VTLOG 2>&1 #step2: fill parameters vtPgsize=$($VTOY_PATH/tool/vtoyksym -p) vtlog "$VTOY_PATH/tool/vtoykmod -f $VTOY_PATH/tool/$vtKoName $vtPgsize 0x$printk_addr 0x$ro_addr 0x$rw_addr $get_addr $get_size $put_addr $put_size 0x$kprobe_reg_addr 0x$kprobe_unreg_addr $vtKv $vtIBT $vtDebug" - $VTOY_PATH/tool/vtoykmod -f $VTOY_PATH/tool/$vtKoName $vtPgsize 0x$printk_addr 0x$ro_addr 0x$rw_addr $get_addr $get_size $put_addr $put_size 0x$kprobe_reg_addr 0x$kprobe_unreg_addr $vtKv $vtIBT $vtDebug + $VTOY_PATH/tool/vtoykmod -f $VTOY_PATH/tool/$vtKoName $vtPgsize 0x$printk_addr 0x$ro_addr 0x$rw_addr $get_addr $get_size $put_addr $put_size 0x$kprobe_reg_addr 0x$kprobe_unreg_addr $vtKv $vtIBT $vtDebug >>$VTLOG 2>&1 - $BUSYBOX_PATH/insmod $VTOY_PATH/tool/$vtKoName + vtlog "insmod $VTOY_PATH/tool/$vtKoName" + $BUSYBOX_PATH/insmod $VTOY_PATH/tool/$vtKoName >>$VTLOG 2>&1 if $GREP -q 'dm_patch' /proc/modules; then + vtlog "dm_patch module OK" echo "done" > $VTOY_PATH/dm_patch_done + else + vtlog "dm_patch module FAILED" fi } diff --git a/IMG/cpio_x86/ventoy/tool/dm_patch_64.ko b/IMG/cpio_x86/ventoy/tool/dm_patch_64.ko index 48050f6d..dc26bf9d 100644 Binary files a/IMG/cpio_x86/ventoy/tool/dm_patch_64.ko and b/IMG/cpio_x86/ventoy/tool/dm_patch_64.ko differ diff --git a/IMG/cpio_x86/ventoy/tool/dm_patch_ibt_64.ko b/IMG/cpio_x86/ventoy/tool/dm_patch_ibt_64.ko index 47fb9b29..a73119ba 100644 Binary files a/IMG/cpio_x86/ventoy/tool/dm_patch_ibt_64.ko and b/IMG/cpio_x86/ventoy/tool/dm_patch_ibt_64.ko differ diff --git a/VtoyTool/vtoykmod.c b/VtoyTool/vtoykmod.c index a1e2ea8d..5cc42a52 100644 --- a/VtoyTool/vtoykmod.c +++ b/VtoyTool/vtoykmod.c @@ -164,6 +164,13 @@ struct modversion_info { char name[64 - sizeof(unsigned long)]; }; +struct modversion_info2 { + /* Offset of the next modversion entry in relation to this one. */ + uint32_t next; + uint32_t crc; + char name[0]; +}; + typedef struct ko_param { @@ -294,14 +301,15 @@ static int vtoykmod_find_section32(char *buf, char *section, int *offset, int *l return 1; } -static int vtoykmod_update_modcrc(char *oldmodver, int oldcnt, char *newmodver, int newcnt) +static int vtoykmod_update_modcrc1(char *oldmodver, int oldcnt, char *newmodver, int newcnt) { int i, j; struct modversion_info *pold, *pnew; - + pold = (struct modversion_info *)oldmodver; pnew = (struct modversion_info *)newmodver; + debug("module update modver format 1\n"); for (i = 0; i < oldcnt; i++) { for (j = 0; j < newcnt; j++) @@ -318,6 +326,51 @@ static int vtoykmod_update_modcrc(char *oldmodver, int oldcnt, char *newmodver, return 0; } + +static int vtoykmod_update_modcrc2(char *oldmodver, int oldlen, char *newmodver, int newlen) +{ + struct modversion_info2 *pold, *pnew, *pnewend; + + pold = (struct modversion_info2 *)oldmodver; + pnew = (struct modversion_info2 *)newmodver; + pnewend = (struct modversion_info2 *)(newmodver + newlen); + + debug("module update modver format 2\n"); + /* here we think that there is only module_layout in oldmodver */ + + for (; pnew < pnewend && pnew->next; pnew = (struct modversion_info2 *)((char *)pnew + pnew->next)) + { + if (strcmp(pnew->name, "module_layout") == 0) + { + debug("CRC 0x%08x --> 0x%08x %s\n", pold->crc, pnew->crc, pnew->name); + memset(pold, 0, oldlen); + pold->next = 0x18; /* 8 + module_layout align 8 */ + pold->crc = pnew->crc; + strcpy(pold->name, pnew->name); + break; + } + } + + return 0; +} + + +static int vtoykmod_update_modcrc(char *oldmodver, int oldlen, char *newmodver, int newlen) +{ + uint32_t uiCrc = 0; + + memcpy(&uiCrc, newmodver + 4, 4); + + if (uiCrc > 0) + { + return vtoykmod_update_modcrc2(oldmodver, oldlen, newmodver, newlen); + } + else + { + return vtoykmod_update_modcrc1(oldmodver, oldlen / 64, newmodver, newlen / 64); + } +} + static int vtoykmod_update_vermagic(char *oldbuf, int oldsize, char *newbuf, int newsize, int *modver) { int i = 0; @@ -393,7 +446,7 @@ int vtoykmod_update(char *oldko, char *newko) if (rc == 0) { - vtoykmod_update_modcrc(oldbuf + oldoff, oldlen / 64, newbuf + newoff, newlen / 64); + vtoykmod_update_modcrc(oldbuf + oldoff, oldlen, newbuf + newoff, newlen); } } else diff --git a/VtoyTool/vtoytool/00/vtoytool_32 b/VtoyTool/vtoytool/00/vtoytool_32 index 556f11bd..cca95539 100644 Binary files a/VtoyTool/vtoytool/00/vtoytool_32 and b/VtoyTool/vtoytool/00/vtoytool_32 differ diff --git a/VtoyTool/vtoytool/00/vtoytool_64 b/VtoyTool/vtoytool/00/vtoytool_64 index 45edfacd..950f5322 100644 Binary files a/VtoyTool/vtoytool/00/vtoytool_64 and b/VtoyTool/vtoytool/00/vtoytool_64 differ diff --git a/VtoyTool/vtoytool/00/vtoytool_aa64 b/VtoyTool/vtoytool/00/vtoytool_aa64 index 2546cb93..5bc9fad3 100644 Binary files a/VtoyTool/vtoytool/00/vtoytool_aa64 and b/VtoyTool/vtoytool/00/vtoytool_aa64 differ diff --git a/VtoyTool/vtoytool/00/vtoytool_m64e b/VtoyTool/vtoytool/00/vtoytool_m64e index 6a1283ee..bac502a1 100644 Binary files a/VtoyTool/vtoytool/00/vtoytool_m64e and b/VtoyTool/vtoytool/00/vtoytool_m64e differ