gcc canaries:对__stack_chk_guard的未定义引用

Jér*_*yet 7 c gcc compilation

我正在尝试启用gcc的canaries的生成,但未获得对__stack_chk_guard的引用。

来自gcc的关于金丝雀的人:

-mstack-protector-guard=guard
       Generate stack protection code using canary at guard.  Supported locations are global for
       global canary or tls for per-thread canary in the TLS block (the default).  This option
       has effect only when -fstack-protector or -fstack-protector-all is specified.

   These -m switches are supported in addition to the above on x86-64 processors in 64-bit
   environments.
Run Code Online (Sandbox Code Playgroud)

我已经完成了这个测试程序:

#define VALUE 2048
int    main()
{
  char arr[VALUE];
  int  i;

  for (i = 0; i < VALUE + 15; i++) // "i < VALUE + 15" is to test if canaries works but the code doesn't compile anymore with "i < 10" 
    arr[i] = '0';
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如在gcc的人中所说,我的编译行是:

gcc main.c -fstack-protector-all -mstack-protector-guard=global
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

/tmp/ccXxxxVd.o: In function `main':
main.c:(.text+0xe): undefined reference to `__stack_chk_guard'
main.c:(.text+0x51): undefined reference to `__stack_chk_guard'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

如何清除此错误?

编辑:

  • 操作系统:ubuntu 14.10 utopic
  • 架构:x86-64
  • 环境:64位

Art*_*Art 5

-mstack-protector-guard选项似乎只是为了与过去堆栈保护器的工作方式向后兼容。在过去,金丝雀是在一个全局变量中。后来它被切换到TLS。您使用的操作系统/libc 似乎已被删除或从未支持全局变量金丝雀,因此只有 TLS 有效。

不要触摸该-mstack-protector-guard选项,一切都应该有效。使用-fstack-protector-all.


Sat*_*iya 5

在 c 文件中提供__stack_chk_guard随机值,避免使用常规值(例如全零或 FF),因为堆栈可以在任何内存操作期间轻松获取这些值。Wiki上提供了幻数实现。这__stack_chk_guard将被放置在堆栈的顶部和底部,在每次堆栈访问期间都会检查它们。该值的任何更改都意味着堆栈已损坏,并返回错误并提供堆栈保护。

unsigned long __stack_chk_guard;
void __stack_chk_guard_setup(void)
{
     __stack_chk_guard = 0xBAAAAAAD;//provide some magic numbers
}

void __stack_chk_fail(void)                         
{
 /* Error message */                                 
}// will be called when guard variable is corrupted 
Run Code Online (Sandbox Code Playgroud)