How to build a vermagic matched kmod-* module without any help from original manufacturer?

I have a device on which runs an OpenWrt based system.

/proc/version:
Linux version 5.10.120 (noname@noname) (x86_64-openwrt-linux-gnu-gcc (OpenWrt GCC 8.4.0 r0-bec7196d) 8.4.0, GNU ld (GNU Binutils) 2.38) #0 SMP Tue Nov 29 07:36:11 2022

zcat /proc/config.gz can output kernel config.

/etc/opkg/distfeeds.conf shows:
src/gz openwrt_core https://downloads.openwrt.org/releases/22.03-SNAPSHOT/targets/x86/64/packages
src/gz openwrt_base https://downloads.openwrt.org/releases/22.03-SNAPSHOT/packages/x86_64/base

There is no kmod-tun and kmod-pppoe installed in the system. Cause the kernel lib (gnu: glibc) is different with the lib in opkg source, so I can't install them by opkg install kmod-tun directly.

I have tried to build OpenWrt(v22.03.0-rc4 , with kernel version same to 5.10.120) with the kernel config (zcat /proc/config.gz > .config), but get mismatched vermagic. I tried to modify the files:

diff --git a/include/kernel-defaults.mk b/include/kernel-defaults.mk
index 2e21392016..c6677ef3dd 100644
--- a/include/kernel-defaults.mk
+++ b/include/kernel-defaults.mk
@@ -118,7 +118,7 @@ define Kernel/Configure/Default
                cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev; \
        }
        $(_SINGLE) [ -d $(LINUX_DIR)/user_headers ] || $(KERNEL_MAKE) INSTALL_HDR_PATH=$(LINUX_DIR)/user_headers headers_install
-       grep '=[ym]' $(LINUX_DIR)/.config.set | LC_ALL=C sort | $(MKHASH) md5 > $(LINUX_DIR)/.vermagic
+       echo 65b9d929c6a7cd31c6df424826091f15 > $(LINUX_DIR)/.vermagic
 endef

diff --git a/package/kernel/linux/Makefile b/package/kernel/linux/Makefile
index 9fa68d78f7..96f86975a6 100644
--- a/package/kernel/linux/Makefile
+++ b/package/kernel/linux/Makefile
@@ -24,7 +24,7 @@ export SHELL:=/bin/sh
 include $(INCLUDE_DIR)/package.mk

 ifeq ($(DUMP),)
-  STAMP_BUILT:=$(STAMP_BUILT)_$(shell $(SCRIPT_DIR)/kconfig.pl $(LINUX_DIR)/.config | $(MKHASH) md5)
+  STAMP_BUILT:=$(STAMP_BUILT)_65b9d929c6a7cd31c6df424826091f15
   -include $(LINUX_DIR)/.config
 endif

It really does work in installing, but when I try to use the zerotier, the system crashed.
With crash log:

<4>[   71.886917] process '/usr/bin/zerotier-one' started with executable stack
<1>[  100.832329] BUG: unable to handle page fault for address: 0000000100000044
<1>[  100.839578] #PF: supervisor write access in kernel mode
<1>[  100.845095] #PF: error_code(0x0002) - not-present page
<6>[  100.850536] PGD 122fc3067 P4D 122fc3067 PUD 0 
<4>[  100.855252] Oops: 0002 [#1] SMP NOPTI
<4>[  100.859122] CPU: 1 PID: 10737 Comm: zerotier-one Tainted: G     U            5.10.120 #0
<4>[  100.867648] Hardware name: UDX46/To be filled by O.E.M, BIOS 5.19 06/16/2022
Oops#1 Part3
<4>[  100.875819] RIP: 0010:sk_alloc+0xbb/0x1d0
<4>[  100.880052] Code: 00 00 00 00 00 45 85 f6 0f 94 c0 c1 e0 06 89 c2 0f b6 43 13 83 e0 bf 09 d0 88 43 13 a8 40 74 33 49 8d 7c 24 04 b8 01 00 00 00 <f0> 41 0f c1 44 24 04 85 c0 0f 84 f7 00 00 00 0f 88 e2 00 00 00 83
<4>[  100.899806] RSP: 0018:ffffc90001527c00 EFLAGS: 00010202
<4>[  100.905325] RAX: 0000000000000001 RBX: ffff888129bf4000 RCX: 0000000000000003
<4>[  100.912845] RDX: 000000006fd00040 RSI: ffffffff820839b4 RDI: 0000000100000044
<4>[  100.920374] RBP: ffffc90001527c28 R08: 0000000000000000 R09: ffff888129bf4000
<4>[  100.927928] R10: ffff888100545a80 R11: 0000000000000fe0 R12: 0000000100000040
<4>[  100.935451] R13: ffffffffa06330e0 R14: 0000000000000000 R15: 0000000000000000
<4>[  100.942916] FS:  00007f9357f57740(0000) GS:ffff88846fe80000(0000) knlGS:0000000000000000
<4>[  100.951424] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
<4>[  100.957479] CR2: 0000000100000044 CR3: 000000011e4d2000 CR4: 0000000000350ee0
<4>[  100.965008] Call Trace:
<4>[  100.967603]  tun_ptr_free+0x634/0x6cb0 [tun]
<4>[  100.972121]  misc_open+0x117/0x170
<4>[  100.975709]  chrdev_open+0xbf/0x190
<4>[  100.979372]  ? cdev_default_release+0x20/0x20
<4>[  100.983963]  do_dentry_open+0x13d/0x370
<4>[  100.987973]  vfs_open+0x28/0x30
<4>[  100.991283]  path_openat+0xa84/0xd90
<4>[  100.995044]  ? x2apic_send_IPI+0x29/0x30
<4>[  100.999198]  ? check_preempt_curr+0x29/0x60
<4>[  101.003610]  do_filp_open+0x7b/0xd0
<4>[  101.007284]  ? __alloc_fd+0x41/0x150
<4>[  101.011044]  do_sys_openat2+0x1fd/0x2b0
<4>[  101.015096]  do_sys_open+0x39/0x50
<4>[  101.018677]  __x64_sys_openat+0x1b/0x20
<4>[  101.022713]  do_syscall_64+0x37/0x50
<4>[  101.026473]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Oops#1 Part2
<4>[  101.031784] RIP: 0033:0x7f9358050a09
<4>[  101.035543] Code: 24 18 eb a1 0f 1f 44 00 00 89 f3 48 89 fd e8 6e c2 f8 ff 45 89 e2 89 da 48 89 ee 41 89 c0 bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00 f0 ff ff 77 3f 89 c3 44 89 c7 e8 c5 c2 f8 ff 48 83 c4 50
<4>[  101.055384] RSP: 002b:00007ffc7b680ae0 EFLAGS: 00000293 ORIG_RAX: 0000000000000101
<4>[  101.063326] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 00007f9358050a09
<4>[  101.070819] RDX: 0000000000000002 RSI: 00000000004d5a75 RDI: 00000000ffffff9c
<4>[  101.078340] RBP: 00000000004d5a75 R08: 0000000000000000 R09: 000000007fffffff
<4>[  101.085859] R10: 0000000000000000 R11: 0000000000000293 R12: 0000000000000000
<4>[  101.093318] R13: 00000000020eca10 R14: 0cccb752f7afbe6c R15: 00007f9357fe2080
<4>[  101.100811] Modules linked in: cdc_mbim ipt_REJECT dm9601 cdc_subset cdc_ncm cdc_ether cdc_eem ax88179_178a asix aqc111 zstd xt_time xt_tcpudp xt_tcpmss xt_statistic xt_state xt_socket xt_recent xt_quota xt_pkttype xt_physdev xt_owner xt_nat xt_multiport xt_mark xt_mac xt_limit xt_length xt_hl xt_helper xt_ecn xt_dscp xt_conntrack xt_connmark xt_connlimit xt_connbytes xt_comment xt_cgroup xt_addrtype xt_TCPMSS xt_REDIRECT xt_NFQUEUE xt_MASQUERADE xt_LOG xt_HL xt_FLOWOFFLOAD xt_DSCP xt_CT xt_CLASSIFY usbnet usblp ums_usbat ums_sddr55 ums_sddr09 ums_karma ums_jumpshot ums_isd200 ums_freecom ums_datafab ums_cypress ums_alauda rtl8812au r8169 nfnetlink_queue nf_socket_ipv6 nf_socket_ipv4 nf_reject_ipv4 nf_log_ipv6 nf_log_ipv4 nf_log_common nf_flow_table nf_conntrack_netlink nf_conncount mdio_devres lzo_rle lzo lan78xx kvm_intel kvm kaweth irqbypass iptable_raw iptable_nat iptable_mangle iptable_filter ipt_ECN ip_tables igc fixed_phy crc_ccitt cfg80211 cdc_wdm br_netfilter ledtrig_netdev2
Oops#1 Part1
<4>[  101.100869]  ledtrig_normal_ht32f52231 ledtrig_breath_ht32f52231 leds_mcu_ht32f52231 fuse i915 apfs configs videodev i2c_i801 i2c_designware_pci i2c_smbus i2c_designware_core regmap_core i2c_dev xt_set ip_set_list_set ip_set_hash_netportnet ip_set_hash_netport ip_set_hash_netnet ip_set_hash_netiface ip_set_hash_net ip_set_hash_mac ip_set_hash_ipportnet ip_set_hash_ipportip ip_set_hash_ipport ip_set_hash_ipmark ip_set_hash_ip ip_set_bitmap_port ip_set_bitmap_ipmac ip_set_bitmap_ip ip_set nfnetlink ip6table_nat nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 ip6t_NPT ip6table_mangle ip6table_filter ip6_tables ip6t_REJECT x_tables nf_reject_ipv6 nfsv4 nfsv3 nfs nfs_ssc msdos bonding e1000 rpcsec_gss_krb5 auth_rpcgss dm_raid veth tun snd_rawmidi snd_seq_device snd_pcm_oss snd_mixer_oss snd_hwdep snd_compress snd_pcm snd_timer snd soundcore intelfb ntfs lockd sunrpc grace hfsplus cramfs cifs autofs4 fb_ddc dns_resolver raid456 async_raid6_recov async_pq async_xor async_memcpy async_tx raid10
<4>[  101.192551]  raid1 raid0 md_mod nls_utf8 nls_cp936 sha512_ssse3 sha512_generic sha256_ssse3 sha1_ssse3 sha1_generic seqiv jitterentropy_rng drbg md5 md4 hmac des_generic libdes cts cmac nls_iso8859_1 nls_cp437 vfat fat it86x_sio_dx4600 ioports_hotplug_dx4600 gpio_dx4600 xhci_plat_hcd it87_wdt sata_via sata_sil24 sata_sil sata_nv sata_mv fsl_mph_dr_of ehci_platform ehci_fsl xfs exfat btrfs zstd_decompress zstd_compress xxhash xor raid6_pq lzo_decompress lzo_compress dm_mirror dm_region_hash dm_log dm_crypt dm_cache dm_persistent_data dm_bufio libcrc32c dm_mod dm_bio_prison dax button_hotplug ptp realtek microchip pps_core mii libphy cbc encrypted_keys trusted tpm
<4>[  101.346535] CR2: 0000000100000044
<4>[  101.350057] ---[ end trace b13042da73fe5146 ]---

So:
Is it possible to build a correct kmod-tun module with the only above information? How to get to the right position? Thx in advance!

It's x86_64, just install a normal (release-) image of OpenWrt.

Thx. It's a NAS device. There are some other applications from original manufacturer. I wanna keep them, so I don't want to reinstall image from OpenWrt official site

No.

The vermagic hash is there just due to the crashes like you experience.
You might try to override the hash in installation, like you did, but the later crash then revealed the underlying subtle incompatibility (that the hash check tries to prevent).

You can't replicate the possible GCC options, glibc options, possible kernel source changes / patches from the OEM manufacturer.

The best bet would be to the GPL sources from the manufacturer. That should give you the closest compilation options in use.

2 Likes

Got it. Thx very much!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.