Compile a kernel module as a position independant executable

爱⌒轻易说出口 提交于 2019-12-11 17:18:32

问题


For a PoC (context), I’m trying to build a kernel module as a position independent executable.

Currently, I compile my module using mcmodel=small -fpie -mno-red-zone -mnosse to my Makefile (/lib/modules/$(uname -r)fixed/build/Makefile) and then I resolve my symbols by parsing /proc/kallsyms and patching my binary using ld’s option --defsym symbol=address

But this is not satisfactory. I get a rip-relative addressing but no got/plt. Below an example of function in the generated module before linking

0000000000000024 <vm_close>:
{
  24:   ff 15 00 00 00 00       callq  *0x0(%rip)        # 2a <vm_close+0x6>
    printk(KERN_INFO "vm_close");
  2a:   48 8d 3d 00 00 00 00    lea    0x0(%rip),%rdi        # 31 <vm_close+0xd>
  31:   e8 00 00 00 00          callq  36 <vm_close+0x12>
}
  36:   c3                      retq  

And the same function after relocations

0000000000401024 <vm_close>:
{
  401024:   67 e8 d6 07 60 81       addr32 callq ffffffff81a01800 <__fentry__>
    printk(KERN_INFO "vm_close");
  40102a:   48 8d 3d da 0f 00 00    lea    0xfda(%rip),%rdi        # 40200b <myexit+0xbf5>
  401031:   e8 db 7b d4 80          callq  ffffffff81148c11 <printk>
}
  401036:   c3                      retq   

Here, the function directly calls printk and does not use a global offset table. How should I change my compilation/linking method so that my module is actually built as a pie ?

You can find the compilation options of the *.ko there (make V=1)

Note: I am perfectly aware that my PoC is very unusual and can be a bad practice but I'd like to do it anyway.

来源:https://stackoverflow.com/questions/58131500/compile-a-kernel-module-as-a-position-independant-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!