Linux 禁用内存地址随机化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5194666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:07:11  来源:igfitidea点击:

Disable randomization of memory addresses

linuxmemory-addressaslr

提问by 0fnt

I'm trying to debug a binary that uses a lot of pointers. Sometimes for seeing output quickly to figure out errors, I print out the address of objects and their corresponding values, however, the object addresses are randomized and this defeats the purpose of this quick check up. Is there a way to disable this temporarily/permanently so that I get the same values every time I run the program.

我正在尝试调试使用大量指针的二进制文件。有时为了快速查看输出以找出错误,我会打印出对象的地址及其对应的值,但是,对象地址是随机的,这违背了快速检查的目的。有没有办法暂时/永久禁用此功能,以便每次运行程序时都能获得相同的值。

Oops. OS is Linux fsttcs1 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux

哎呀。操作系统是Linux fsttcs1 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux

采纳答案by Orbit

On Ubuntu , it can be disabled with...

在 Ubuntu 上,它可以被禁用...

echo 0 > /proc/sys/kernel/randomize_va_space

On Windows, this post might be of some help...

在 Windows 上,这篇文章可能会有所帮助...

http://blog.didierstevens.com/2007/11/20/quickpost-another-funny-vista-trick-with-aslr/

http://blog.didierstevens.com/2007/11/20/quickpost-another-funny-vista-trick-with-aslr/

回答by Stephen

To temporarily disable ASLR for a particular program you can always issue the following (no need for sudo)

要临时禁用特定程序的 ASLR,您可以随时发出以下命令(不需要 sudo)

setarch `uname -m` -R ./yourProgram

回答by rts1

You can also do this programmatically from C source before a UNIX exec.

您还可以在 UNIX 之前从 C 源以编程方式执行此操作exec

If you take a look at the sources for setarch(here's one source):

如果您查看setarch的来源(这是一个来源):

http://code.metager.de/source/xref/linux/utils/util-linux/sys-utils/setarch.c

http://code.metager.de/source/xref/linux/utils/util-linux/sys-utils/setarch.c

You can see if boils down to a system call (syscall) or a function call (depending on what your system defines). From setarch.c:

您可以查看归结为系统调用 ( syscall) 还是函数调用(取决于您的系统定义的内容)。来自 setarch.c:

#ifndef HAVE_PERSONALITY
# include <syscall.h>
# define personality(pers) ((long)syscall(SYS_personality, pers))
#endif

On my CentOS 6 64-bit system, it looks like it uses a function (which probably calls the self-same syscall above). Take a look at this snippet from the include file in /usr/include/sys/personality.h(as referenced as <sys/personality.h>in the setarch source code):

在我的 CentOS 6 64 位系统上,它看起来像是使用了一个函数(它可能调用了上面的同一个系统调用)。从包含文件中查看此片段/usr/include/sys/personality.h(如<sys/personality.h>setarch 源代码中所引用 ):

/* Set different ABIs (personalities).  */
extern int personality (unsigned long int __persona) __THROW;

What it boils down to, is that you can, from C code, call and set the personality to use ADDR_NO_RANDOMIZE and then exec(just like setarchdoes).

它归结为,您可以从 C 代码调用并设置个性以使用 ADDR_NO_RANDOMIZE 然后exec(就像setarch那样)。

#include <sys/personality.com>

#ifndef HAVE_PERSONALITY
# include <syscall.h>
# define personality(pers) ((long)syscall(SYS_personality, pers))
#endif

...

void mycode() 
{
   // If requested, turn off the address rand feature right before execing
   if (MyGlobalVar_Turn_Address_Randomization_Off) {
     personality(ADDR_NO_RANDOMIZE);
   } 
   execvp(argv[0], argv); // ... from set-arch.
}

It's pretty obvious you can't turn address randomization off in the process you are in (grin: unless maybe dynamic loading), so this only affects forks and execs later. I believe the Address Randomization flags are inherited by child sub-processes?

很明显,您无法在您所在的过程中关闭地址随机化(咧嘴笑:除非可能是动态加载),因此这只会影响以后的 fork 和 execs。我相信地址随机化标志是由子子进程继承的吗?

Anyway, that's how you can programmatically turn off the address randomization in C source code. This may be your only solution if you don't want the force a user to intervene manually and start-up with setarch or one of the other solutions listed earlier.

无论如何,这就是您如何以编程方式关闭 C 源代码中的地址随机化。如果您不希望强制用户手动干预并使用 setarch 或前面列出的其他解决方案之一启动,这可能是您唯一的解决方案。

Before you complain about security issues in turning this off, some shared memory libraries/tools (such as PickingTools shared memoryand some IBM databases) need to be able to turn off randomization of memory addresses.

在你抱怨关闭它的安全问题之前,一些共享内存库/工具(例如PickingTools 共享内存和一些IBM 数据库)需要能够关闭内存地址的随机化。