如何在不修改和预编译Linux内核的情况下替换静态内核函数
全部, 我想知道如何在不修改linux内核的情况下替换模块中的内核静态函数。 我知道Linux hook可以替代一些功能,但是 问题是我想在不修改linux内核的情况下替换静态函数。 你能帮我一下吗? 谢谢。
all,
I want to know how to replace a kernel static function in a module without modifying linux kernel.
I have known that Linux hook can replace some functions, but the
problem is that I want to replace a static function without modifying linux kernel.
Would you please help me ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,Linux 内核的编译方式不可能在运行时替换/挂钩静态函数(如果您正在谈论模块代码,则无法卸载/重新加载整个模块)。
这是因为编译内联
静态
在很多时候都起作用(除非你在某个地方获取它的地址),因此它们甚至不会出现在符号表中。编译后无法找出生成的二进制文件中静态代码的最终位置 - 并非不可能,您会在调用该 func 的所有调用站点中找到它的几个内联版本。所以基本问题是:为什么函数必须是静态的?您到底想要做什么来强制使用
static
?Generally the way the Linux kernel is compiled, replacing / hooking a static function at runtime isn't possible (short of unloading / reloading the entire module if you're talking module code).
That is because the compile inlines
static
functions much of the time (unless you take the address of it somewhere), and therefore they won't even show up in the symbol table. There's no way after the compile to find out where in the generated binary thestatic
code ended up - not unlikely, you'll find several inlined versions of it in all the call sites invoking the func.So the basic question: Why does the function have to be
static
? What exactly is it you're attempting to do that mandates the use ofstatic
?如果它实际上被编译为模块(不是内置的),那么只需重新编译代码,
rmmod
模块,并insmod
新的.ko
文件。很简单...某种陈词滥调的糕点。If it's actually compiled as a module (not built-in), then just recompile the code,
rmmod
the module, andinsmod
the new.ko
file. Easy as... some kind of cliche pastry.一般来说,您可以使用其中一些技术:
int3
挂钩函数,如果您根本不想修改内核的代码,您可以设置调试寄存器并监视访问异常(当然,在您的异常处理程序中)。除此之外,您可以尝试查找并使某些内核内部变量无效,以便从内核访问它们会导致无效指针取消引用异常。在这种情况下,您可以处理此类异常并进行回溯以实现目标函数。
In general, you may use some of this techniques:
int3
If you don't wish to modify the kernel's code at all, you might set up the debugging registers and watch for an access exceptions (in your exception handler, of course). Besides that, you can try to find and invalidate some of the kernel's internal variables so the access to them from the kernel causes the invalid pointer dereference exception. In that case you can handle such an exception and do a back-trace to achive target function.