linux – 为什么变量的地址在运行之间不断变化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 为什么变量的地址在运行之间不断变化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1595字,纯文字阅读大概需要3分钟。
内容图文
我制作了两个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 – 为什么变量的地址在运行之间不断变化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。