Skip to content

Commit

Permalink
Fix the issue that VTOY_LINUX_REMOUNT=1 does not work on some distros…
Browse files Browse the repository at this point in the history
… (e.g. Pop OS/openSUSE/Ubuntu 23.04) (ventoy#2475  ventoy#2551)
  • Loading branch information
ventoy committed Oct 6, 2023
1 parent d617985 commit 7f63a1c
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 25 deletions.
41 changes: 35 additions & 6 deletions DMPATCH/dmpatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand All @@ -183,16 +193,28 @@ static int notrace dmpatch_replace_code
cnt++;
}
}
else
else if (style == 2)
{
if (CODE_MATCH2(opCode, i) && cnt < MAX_PATCH)
{
patch[cnt] = opCode + i + PATCH_OP_POS2;
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);
Expand Down Expand Up @@ -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)
{
Expand Down
10 changes: 6 additions & 4 deletions DMPATCH/readme.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions DMPATCH/ubuntu_build.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 -
Expand All @@ -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 -
Expand Down
15 changes: 15 additions & 0 deletions IMG/cpio/ventoy/hook/suse/udev_disk_hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 37 additions & 9 deletions IMG/cpio/ventoy/hook/ventoy-hook-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -292,6 +301,7 @@ ventoy_need_dm_patch() {
}

ventoy_dm_patch() {
vtDmPatchDebug=0
vtMType=$($BUSYBOX_PATH/uname -m)

vtlog "######### ventoy_dm_patch ############"
Expand Down Expand Up @@ -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}')
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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

}
Expand Down
Binary file modified IMG/cpio_x86/ventoy/tool/dm_patch_64.ko
Binary file not shown.
Binary file modified IMG/cpio_x86/ventoy/tool/dm_patch_ibt_64.ko
Binary file not shown.
59 changes: 56 additions & 3 deletions VtoyTool/vtoykmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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++)
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Binary file modified VtoyTool/vtoytool/00/vtoytool_32
Binary file not shown.
Binary file modified VtoyTool/vtoytool/00/vtoytool_64
Binary file not shown.
Binary file modified VtoyTool/vtoytool/00/vtoytool_aa64
Binary file not shown.
Binary file modified VtoyTool/vtoytool/00/vtoytool_m64e
Binary file not shown.

0 comments on commit 7f63a1c

Please sign in to comment.