加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 00:59:57 所属栏目:Linux 来源:网络整理
导读:我制作了两个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 %pn",&x,&g);
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读