可变堆栈大小

发布于 2024-09-12 03:11:29 字数 455 浏览 6 评论 0原文

我的系统(linux 内核 2.6.32-24)正在实现名为地址空间布局随机化的功能(ASLR)。 ASLR 似乎改变了堆栈大小:

void f(int n) 
{
    printf(" %d ", n);
    f(n + 1);
}

int main(...)
{
    f(0);
}

显然,如果您执行该程序,就会出现堆栈溢出。问题在于每次执行时“n”的不同值都会发生分段错误。这显然是由 ASLR 引起的(如果禁用它,程序总是以相同的“n”值退出)。

我有两个问题:

  1. 这是否意味着 ASLR 使堆栈大小略有变化?
  2. 如果是这样,您认为这个事实有问题吗?可能是内核错误吗?

My system (linux kernel 2.6.32-24) is implementing a feature named Address Space Layout Randomization (ASLR). ASLR seems to change the stack size:

void f(int n) 
{
    printf(" %d ", n);
    f(n + 1);
}

int main(...)
{
    f(0);
}

Obviously if you execute the program you'll get a stack overflow. The problem is that segmentation fault happens on different values of "n" on each execution. This is clearly caused by the ASLR (if you disable it the program exits always at the same value of "n").

I have two questions:

  1. does it mean that ASLR make stack size slightly variable?
  2. if so, do you see a problem in this fact? Could be a kernel bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春有你 2024-09-19 03:11:29

这可能意味着在一种情况下,堆栈恰好流入其他某个已分配的块,而在另一种情况下,它会越过未分配的地址空间。

It might mean that in one instance the stack happens to flow into some other allocated block, and in the other instance, it trips over unallocated address-space.

三月梨花 2024-09-19 03:11:29

ASLR 代表“地址空间布局随机化”。它的作用是在每次运行时更改各个节/段的起始地址,是的,这包括堆栈。

这不是一个错误;而是一个错误。这是设计使然。其目的部分是为了通过溢出缓冲区来增加访问权限,因为为了执行任意代码,您需要欺骗 CPU“返回”堆栈上或运行时库中的某个点。合法的代码会知道返回到哪里,但一些预装的漏洞不会——它每次都可能是不同的地址。

至于为什么表观堆栈大小会发生变化,堆栈空间是以页为单位分配的,而不是以字节为单位。调整堆栈指针,特别是如果它不是页面大小的倍数,会改变您看到的可用空间量。

ASLR stands for "address space layout randomization". What it does is change various section/segment start addresses on each run, and yes, this includes the stack.

It's not a bug; it's by design. Its purpose, in part, is to make it harder to gain access by overflowing buffers, since in order to execute arbitrary code, you need to trick the CPU into "returning" to a certain point on the stack, or in the runtime libraries. Legitimate code would know where to return to, but some canned exploit wouldn't -- it could be a different address every time.

As for why the apparent stack size changes, stack space is allocated in pages, not bytes. Tweaking the stack pointer, especially if it's not by a multiple of the page size, changes the amount of space you see available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文