首先,我尝试对它进行一些逆向工程:
printf '
#include <stdio.h>
int main() {
puts("hello world");
}
' > main.c
gcc -std=c99 -pie -fpie -ggdb3 -o pie main.c
echo 2 | sudo tee /proc/sys/kernel/randomize_va_space
readelf -s ./pie | grep -E 'main$'
gdb -batch -nh \
-ex 'set disable-randomization off' \
-ex 'start' -ex 'info line' \
-ex 'start' -ex 'info line' \
-ex 'set disable-randomization on' \
-ex 'start' -ex 'info line' \
-ex 'start' -ex 'info line' \
./pie \
;
输出:
64: 000000000000063a 23 FUNC GLOBAL DEFAULT 14 main
Temporary breakpoint 1, main () at main.c:4
4 puts("hello world");
Line 4 of "main.c" starts at address 0x5575f5fd263e <main+4> and ends at 0x5575f5fd264f <main+21>.
Temporary breakpoint 2 at 0x5575f5fd263e: file main.c, line 4.
Temporary breakpoint 2, main () at main.c:4
4 puts("hello world");
Line 4 of "main.c" starts at address 0x55e3fbc9363e <main+4> and ends at 0x55e3fbc9364f <main+21>.
Temporary breakpoint 3 at 0x55e3fbc9363e: file main.c, line 4.
Temporary breakpoint 3, main () at main.c:4
4 puts("hello world");
Line 4 of "main.c" starts at address 0x55555555463e <main+4> and ends at 0x55555555464f <main+21>.
Temporary breakpoint 4 at 0x55555555463e: file main.c, line 4.
Temporary breakpoint 4, main () at main.c:4
4 puts("hello world");
Line 4 of "main.c" starts at address 0x55555555463e <main+4> and ends at 0x55555555464f <main+21>.
表示它是0x555555554000
+随机偏移量+ 63e
。
但是后来我尝试为555555554
复制Linux内核和glibc源代码,但没有成功。
哪个代码中的哪一部分计算该地址?
我在回答What is the -fPIE option for position-independent executables in gcc and ld?
时遇到了这个问题答案 0 :(得分:3)
某些 Internet搜索为0x555555554000提示:ThreadSanitizer https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
存在问题问:运行程序时,它说:致命:ThreadSanitizer无法映射影子内存(某些内容映射为0x555555554000 <0x7cf000000000)。该怎么办?您需要启用ASLR:$ echo 2 >/proc/sys/kernel/randomize_va_space
此问题可能会在以后的内核中修复,请参见https://bugzilla.kernel.org/show_bug.cgi?id=66721 ...
$ gdb -ex 'set disable-randomization off' --args ./a.out
和https://lwn.net/Articles/730120/“稳定的内核更新”。由hmh(订阅者)于2017年8月7日世界标准时间(星期一)发布https://marc.info/?t=150213704600001&r=1&w=2 (https://patchwork.kernel.org/patch/9886105/,commit c715b72c1ba4)
将x86_64和arm64 PIE基础从0x555555554000移动到0x000100000000 破坏了AddressSanitizer。这是以下内容的部分还原:
- commit eab09532d400(“ binfmt_elf:仅将ELF_ET_DYN_BASE用于PIE”)(https://patchwork.kernel.org/patch/9807325/ https://lkml.org/lkml/2017/6/21/560)
- commit 02445990a96e(“ arm64:将ELF_ET_DYN_BASE移至4GB / 4MB”)(https://patchwork.kernel.org/patch/9807319/)
还原后的代码为:
b/arch/arm64/include/asm/elf.h /* * This is the base location for PIE (ET_DYN with INTERP) loads. On - * 64-bit, this is raised to 4GB to leave the entire 32-bit address + * 64-bit, this is above 4GB to leave the entire 32-bit address * space open for things that want to use the area for 32-bit pointers. */ -#define ELF_ET_DYN_BASE 0x100000000UL +#define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) +++ b/arch/x86/include/asm/elf.h /* * This is the base location for PIE (ET_DYN with INTERP) loads. On - * 64-bit, this is raised to 4GB to leave the entire 32-bit address + * 64-bit, this is above 4GB to leave the entire 32-bit address * space open for things that want to use the area for 32-bit pointers. */ #define ELF_ET_DYN_BASE (mmap_is_ia32() ? 0x000400000UL : \ - 0x100000000UL) + (TASK_SIZE / 3 * 2))
因此,0x555555554000与ELF_ET_DYN_BASE
macro(在fs/binfmt_elf.c for ET_DYN
as not randomized load_bias
中引用)相关,对于x86_64和arm64,它类似于TASK_SIZE的2/3。如果没有CONFIG_X86_32
,则x86_64的TASK_SIZE为2^47 - one page in arch/x86/include/asm/processor.h
/*
* User space process size. 47bits minus one guard page. The guard
* page is necessary on Intel CPUs: if a SYSCALL instruction is at
* the highest possible canonical userspace address, then that
* syscall will enter the kernel with a non-canonical return
* address, and SYSRET will explode dangerously. We avoid this
* particular problem by preventing anything from being mapped
* at the maximum canonical address.
*/
#define TASK_SIZE_MAX ((1UL << 47) - PAGE_SIZE)
旧版本:
/*
* User space process size. 47bits minus one guard page.
*/
#define TASK_SIZE_MAX ((1UL << 47) - PAGE_SIZE)
较新的版本也支持5level和__VIRTUAL_MASK_SHIFT
of 56 bit-v4.17/source/arch/x86/include/asm/processor.h
(但don't want to use it before enabled by user + commit b569bab78d8d ".. Not all user space is ready to handle wide addresses"))。
因此,根据公式 2 ^ 47-1page * 3/2 (对于{{3},则为2 ^ 56)将0x555555554000向下舍入(按load_bias = ELF_PAGESTART(load_bias - vaddr);
,vaddr为零)。 }):
$ echo 'obase=16; (2^47-4096)/3*2'| bc -q
555555554AAA
$ echo 'obase=16; (2^56-4096)/3*2'| bc -q
AAAAAAAAAAA000
大约2/3的历史记录* TASK_SIZE:
使用2 * TASK_SIZE larger systems和commit 9b1bbf6ea9b2 "use ELF_ET_DYN_BASE only for PIE"不会溢出32位:
几乎所有的拱门都将ELF_ET_DYN_BASE定义为TASK_SIZE的2/3。虽然 似乎某些体系结构以错误的方式执行此操作。问题 是2 * TASK_SIZE可能溢出32位,所以真正的ELF_ET_DYN_BASE 变得错误。通过在除以TASK_SIZE之前解决此溢出问题 乘法:
(TASK_SIZE / 3 * 2)
diff --git a/include/asm-i386/elf.h b/include/asm-i386/elf.h +/* This is the location that an ET_DYN program is loaded if exec'ed. Typical + use of this is to invoke "./ld.so someprog" to test out a new version of + the loader. We need to make sure that it is out of the way of the program + that it will "exec", and that there is sufficient room for the brk. */ + +#define ELF_ET_DYN_BASE (2 * TASK_SIZE / 3)