简体   繁体   English

如何从内核模块中查找动态链接/加载的库的PHDR?

[英]How to find the PHDR of dynamically linked/loaded libraries from a kernel module?

I need to access the program header tables (or alternatively to the section headers) of a process from the kernel in order to find the addresses of .eh_frame and .eh_frame_hdr sections from a linux kernel module. 我需要从内核访问进程的程序头表(或节头),以便从Linux内核模块中找到.eh_frame和.eh_frame_hdr节的地址。 In userspace I would use dl_iterate_phdr(), but I need a kernel-space solution. 在用户空间中,我将使用dl_iterate_phdr(),但我需要一个内核空间解决方案。 If possible, it would not need to go through the elf files. 如果可能的话,就不需要遍历elf文件。

The auxiliary vector has the AT_PHDR field, but it does not help to find the PHDRs of dynamically linked/loaded libraries. 辅助向量具有AT_PHDR字段,但是它无助于查找动态链接/加载的库的PHDR。

My other idea was to iterate on the vm_areas to find the PHDR address from every file that has an executable mmap in the task's memory. 我的另一个想法是在vm_areas上进行迭代,以从任务内存中具有可执行mmap的每个文件中查找PHDR地址。 The problem with this solution is that the elf file can be changed or deleted after load. 该解决方案的问题在于,加载后可以更改或删除elf文件。

Is there a way to do this that relies only on memory and not on the elf file? 有没有办法只依靠内存而不依靠elf文件来做到这一点?

It looks like the Elf header (which has the file offset to the phdr table - often the same as the offset in memory) is always at the beginning of executable mmaps. 看起来Elf标头(具有与phdr表的文件偏移-通常与内存中的偏移相同)始终位于可执行mmap的开头。 It does not seem really reliable as I could not find any documentation about the appearance of the Ehdr but it seems present in practice. 由于我找不到有关Ehdr外观的任何文档,因此它似乎并不可靠,但实际上似乎存在。 This could be because of the fact that it must be at the beginning of Elf files and that the page size and alignment makes the executable segment start at offset 0x0. 这可能是由于以下事实:它必须位于Elf文件的开头,并且页面大小和对齐方式使可执行段从偏移量0x0开始。

We can verify that executable mappings start at offset 0x0 for all running processes and loaded shared object with this bash line: 我们可以使用以下bash行验证所有正在运行的进程和已加载的共享库的可执行文件映射都从偏移量0x0开始:

sudo cat /proc/*/maps | awk '{ print $2 " " $3 " " $6;}' | egrep '^..x.' | grep -vE '.... 0{8}'

It outputs all the executable mappings that do not start at offset 0x0, so no output means that the Ehdrs are at the beginning of executable vm_areas. 它输出所有不是从偏移量0x0开始的可执行映射,因此没有输出意味着Ehdr位于可执行vm_areas的开头。

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

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