为了避免对程序进行stack clash攻击,we尝试将堆栈大小设置为setrlimit(RLIMIT_STACK)
到大约2 MB。
这个限制适用于我们程序自身的内部需求,但我们注意到exec()
外部程序的尝试在某些具有此新限制的系统上开始失败。我们使用下面的测试程序调查的一个系统似乎具有超过4 MiB的exec()
d个程序的最小堆栈大小。
我的问题是,我们怎样才能知道给定系统上堆栈大小的安全最小值,以便exec()
不会失败?
我们不希望在我们当前测试的所有系统上停止失败之前提出这个问题,因为这可能会导致将来失败,因为程序被移植到具有更高最低要求的较新系统类型。 / p>
下面的C测试程序是用system()
编写的,但是较低级别的症状是execl()
系统调用失败。根据您测试的主机操作系统,当您为被调用程序提供太少的堆栈空间来启动时,您可以在被调用程序中获得errno == E2BIG
或段错误。
构建:
$ CFLAGS="-std=c99 -D_POSIX_C_SOURCE=200809" make stacklim
这个问题与“To check the E2BIG error condition in exec”切线相关,但我们的实际问题却有所不同:我们对此限制导致的潜在可移植性问题感兴趣。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
static enum try_code {
raise_minimum,
lower_maximum,
failed_to_launch
}
try_stack_limit(rlim_t try)
{
// Update the stack limit to the given value
struct rlimit x;
getrlimit(RLIMIT_STACK, &x);
static int first_time = 1;
if (first_time) {
first_time = 0;
printf("The default stack limit here is %llu bytes.\n", x.rlim_cur);
}
x.rlim_cur = try;
setrlimit(RLIMIT_STACK, &x);
// Test the limit with a do-nothing shell launch
int status = system("exit");
switch (status) {
case 0: return lower_maximum;
case -1:
perror("Failed to start shell");
return failed_to_launch;
default:
if (WIFEXITED(status) && WEXITSTATUS(status) == 127) {
// system() couldn't run /bin/sh, so assume it was
// because we set the stack limit too low.
return raise_minimum;
}
else if (WIFSIGNALED(status)) {
fprintf(stderr, "system() failed with signal %s.\n",
strsignal(WTERMSIG(status)));
return failed_to_launch;
}
else {
fprintf(stderr, "system() failed: %d.\n", status);
return failed_to_launch;
}
}
}
int main(void)
{
extern char **environ;
size_t etot = 0;
for (size_t i = 0; environ[i]; ++i) {
etot += strlen(environ[i]) + 1;
}
printf("Environment size = %lu\n", etot + sizeof(char*));
size_t tries = 0;
rlim_t try = 1 * 1000 * 1000, min = 0, max = 0;
while (1) {
enum try_code code = try_stack_limit(try);
switch (code) {
case lower_maximum:
// Call succeded, so lower max and try a lower limit.
++tries;
max = try;
printf("Lowered max to %llu bytes.\n", max);
try = min + ((max - min) / 2);
break;
case failed_to_launch:
if (tries == 0) {
// Our first try failed, so there may be a bug in
// the system() call. Stop immediately.
return 2;
}
// Else, consider it a failure of the new limit, and
// assume we need to limit it.
case raise_minimum:
// Call failed, so raise minimum and try a higher limit.
++tries;
min = try > min ? try : min;
rlim_t next = max ?
min + ((max - min) / 2) :
try * 2;
if (next == try) {
printf("Min stack size here for exec is %llu.\n", max);
return 0;
}
else {
printf("Raising limit from %llu to %llu.\n", try, next);
try = next;
}
break;
default:
return 1;
}
}
}
答案 0 :(得分:0)
您的程序已成功启动,因此您的程序被隐式给出正确的堆栈大小,以便依次启动其他程序:在程序启动期间,在设置新的下限之前获取当前限制:
struct rlimit g_default_stack_limit; /* put in global scope */
getrlimit(RLIMIT_STACK, &g_default_stack_limit);
struct rlimit our_stack_limit;
memcpy(&our_stack_limit, &g_default_stack_limit, sizeof(our_stack_limit));
our_stack_limit.rlim_cur = 2000000; /* some lower value */
setrlimit(RLIMIT_STACK, &our_stack_limit);
然后在启动外部程序之前恢复该初始值,并在创建子fork()
或程序的同步调用(例如,通过system()
)退出后重新应用新限制:
struct rlimit our_stack_limit;
getrlimit(RLIMIT_STACK, &our_stack_limit);
setrlimit(RLIMIT_STACK, &g_default_stack_limit);
if (system(...) == 0) {
....
}
setrlimit(RLIMIT_STACK, &our_stack_limit);
此初始值可能是操作系统的默认值,也可能是调用程序的程序设置的限制。无论哪种方式,几乎可以肯定的是,依次传递给程序调用程序的正确起始值。
答案 1 :(得分:0)
调用exec()时,调用进程的内存映射无效。它被更改为容纳新的可执行文件。为新的可执行文件分配堆栈内存,堆内存,全局数据和代码内存。映射通常在链接时定义,并且在调用main()之前由语言库分配内存