linux – 为什么变量的地址在运行之间不断变化

linux – 为什么变量的地址在运行之间不断变化,第1张

概述我制作了两个C程序,它们是彼此的精确副本.使用 gcc编译器在 Linux平台(Ubuntu 10.04)上编译它们并获得两个单独的可执行文件.然后我使用objdump获取了两个可执行文件的汇编代码,发现汇编代码是完全相同,甚至两个汇编文件中相应指令的地址相同.程序是打印变量的地址.运行时程序产生不同的地址,而且每次运行时同一程序产生不同的地址.为什么两个程序中代码行的地址是相同的,但每次运行时, 我制作了两个C程序,它们是彼此的精确副本.使用 gcc编译器在 Linux平台(Ubuntu 10.04)上编译它们并获得两个单独的可执行文件.然后我使用objdump获取了两个可执行文件的汇编代码,发现汇编代码是完全相同,甚至两个汇编文件中相应指令的地址相同.程序是打印变量的地址.运行时程序产生不同的地址,而且每次运行时同一程序产生不同的地址.为什么两个程序中代码行的地址是相同的,但每次运行时,变量的地址甚至会改变相同的程序.我认为在屏幕上打印变量的地址是虚拟地址,但如果它的虚拟地址为什么它不能是每次都一样.通过objdump获得的汇编代码中显示的地址也是虚拟的吗?解决方法 这是由于 address space layout randomization.

引用维基百科:

Address space layout randomization (ASLR) is a computer security method which involves randomly arranging the positions of key data areas,usually including the base of the executable and position of librarIEs,heap,and stack,in a process’s address space.

Benefits

Address space randomization hinders some types of security attacks by making it more difficult for an attacker to predict target addresses. For example,attackers trying to execute return-to-libc attacks must locate the code to be executed,while other attackers trying to execute shellcode injected on the stack have to find the stack first. In both cases,the related memory addresses are obscured from the attackers. These values have to be guessed,and a mistaken guess is not usually recoverable due to the application crashing.

例如,当我在我的Ubuntu 10.10盒子上重复运行从以下C代码生成的相同可执行文件时:

#include <stdio.h>int g = 0;int main() {  int x = 0;  printf("%p %p\n",&x,&g);}

局部变量(x)的地址不断变化,但全局变量(g)的地址保持不变.

总结

以上是内存溢出为你收集整理的linux – 为什么变量的地址在运行之间不断变化全部内容,希望文章能够帮你解决linux – 为什么变量的地址在运行之间不断变化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1025643.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存