简体   繁体   English

如何为不同的 linux Z50484C19F1AFDAF3841A0D821ED39 编译 linux kernel 模块

[英]How to compile a linux kernel module for different linux kernel

I am sort of new to kernel programming, but i have been struggling a ton with this issue for days now.我对 kernel 编程有点陌生,但几天来我一直在为这个问题苦苦挣扎。 I have a machine with linux kernel '5.10.0-kali7-amd64' and im using it for development of a linux kernel module for Ubutnu 16.04.4 '4.4.0-119-generic', but i can't figure out any way that i can compile it on my machine for that version and for it to actually work on the 4.4.0 kernel machine. I have a machine with linux kernel '5.10.0-kali7-amd64' and im using it for development of a linux kernel module for Ubutnu 16.04.4 '4.4.0-119-generic', but i can't figure out any这样我就可以在我的机器上为该版本编译它,并让它在 4.4.0 kernel 机器上实际工作。

The closest i've got is this:我得到的最接近的是:

  1. I downloaded source from https://launchpad.net/ubuntu/xenial/+package/linux-headers-4.4.0-119 and installed with dpkg我从https://launchpad.net/ubuntu/xenial/+package/linux-headers-4.4.0-119下载了源代码并安装了 dpkg
  2. I then downloaded and installed the 4.4.0-119-generic from https://www.ubuntuupdates.org/package/core/xenial/main/updates/linux-image-4.4.0-119-generic然后我从https://www.ubuntuupdates.org/package/core/xenial/main/updates/linux-image-4.4.0-119-generic下载并安装了 4.4.0-119-generic
  3. Both of them installed with no issue.他们两个都安装没有问题。
  4. I compiled my module by using in my Makefile make -C /lib/modules/4.4.0-119-generic/build M=$(PWD) modules which also worked and compiled my hello world module.我通过在我的 Makefile make -C /lib/modules/4.4.0-119-generic/build M=$(PWD) modules中使用来编译我的模块,这些模块也可以工作并编译我的 hello world 模块。

However when uploaded to the 4.4.0 machine the insmod errored saying insmod: ERROR: could not insert module rootkitMy.ko: Invalid module format .然而,当上传到 4.4.0 机器时,insmod 出错说insmod: ERROR: could not insert module rootkitMy.ko: Invalid module format The dmesg says: module: rootkit: Unknown rela relocation: 4 I then compiled my source code on the 4.4.0 machine and created a module with literally the exact same modinfo, but that one did work. dmesg 说: module: rootkit: Unknown rela relocation: 4然后我在 4.4.0 机器上编译了我的源代码,并创建了一个具有完全相同 modinfo 的模块,但是那个确实有效。 here are the modinfos for both:这是两者的modinfos:

filename:       /rootkit.ko
version:        0.01
description:    Rootkit hook
author:         Bl4ckC4t
license:        GPL
srcversion:     46604268C8D1B7FA5115CB4
depends:        
vermagic:       4.4.0-119-generic SMP mod_unload modversions retpoline 



filename:       /rootkitMy.ko
version:        0.01
description:    Rootkit hook
author:         Bl4ckC4t
license:        GPL
srcversion:     46604268C8D1B7FA5115CB4
depends:        
vermagic:       4.4.0-119-generic SMP mod_unload modversions retpoline 

rootkitMy.ko was compiled on the 5.10 machine and didn't work while the rootkit.ko was compiled on the 4.4.0 machine and did work properly when injected with insmod What can I do to compile a working module from my 5.10 machine? rootkitMy.ko 是在 5.10 机器上编译的,但在 rootkit.ko 是在 4.4.0 机器上编译并且在注入insmod时可以正常工作 我可以做些什么来从我的 5.10 机器编译一个工作模块?

I managed to resolve the issue.我设法解决了这个问题。 Unknown rela relocation: 4 is an insmod error you get due to a change in the way the kernel handles PLT, more specifically the R_X86_64_PC32 and R_X86_64_PLT32. Unknown rela relocation: 4是由于 kernel 处理 PLT,更具体地说是 R_X86_64_PC32 和 R_X86_64_PLT32 的方式发生变化而导致的 insmod 错误。 With binutils >= 2.31, the linker has decided to use R_X86_64_PLT32 relocations, which aren't supported in the older kernel.在 binutils >= 2.31 的情况下,linker 决定使用 R_X86_64_PLT32 重定位,旧版 kernel 不支持这些重定位。

To fix this:要解决这个问题:

  1. I downloaded an older version of binutils (2.26.1) from https://ftp.gnu.org/gnu/binutils/我从https://ftp.gnu.org/gnu/binutils/下载了旧版本的 binutils (2.26.1)
  2. extracted the folder from the archive从存档中提取文件夹
  3. compiled the binutils to /usr/local/binutils-2.6 by running通过运行将 binutils 编译为 /usr/local/binutils-2.6
./configure --prefix=/usr/local/binutils-2.6
make
sudo make install
  1. exported the new binutils to my path and recompiled my module export PATH=/usr/local/binutils-2.6/bin:$PATH将新的 binutils 导出到我的路径并重新编译我的模块export PATH=/usr/local/binutils-2.6/bin:$PATH

And now it works!现在它起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM