【发布时间】:2021-06-29 09:16:02
【问题描述】:
我的代码看起来像
struct MyData {
int len;
char data[1];
};
MyData *d1 = malloc(1024);
strcpy(d1->data, a_string);
我将它用作可变长度缓冲区,并小心地保证缓冲区不会溢出。但是程序崩溃了。
objdump -d
2f3dc: 48 8b 44 24 10 mov 0x10(%rsp),%rax
2f3e1: 48 89 ee mov %rbp,%rsi
2f3e4: ba 01 00 00 00 mov $0x1,%edx
2f3e9: 48 8d 78 20 lea 0x20(%rax),%rdi
2f3ed: e8 ce b3 fd ff callq a7c0 <__strcpy_chk@plt>
我怎样才能说服 gcc 这个特定的缓冲区不会溢出并关闭对它的检查,但仍然对其他人进行检查。
BR/2021 年 4 月 2 日
这里是gcc-4.8编译的简化代码 ======>>>>
#include <string.h>
typedef struct _EncodedBuffer1 {
int encoding;
char data[1024];
} EncodedBuffer1;
typedef struct _EncodedBuffer2 {
int encoding;
char data[512];
} EncodedBuffer2;
typedef struct _MyData1 {
int size;
int data_type;
union {
bool bool_balue;
int int_value;
double double_value;
char string1[1];
};
/* something else */
union {
struct {
EncodedBuffer1 x_buf1;
EncodedBuffer2 x_buf2;
};
struct {
EncodedBuffer2 y_buf1;
EncodedBuffer1 y_buf2;
};
};
} MyData1;
int main(int argc, char *argv[])
{
MyData1 d1;
memset(&d1, 0, sizeof(d1));
d1.size = sizeof(d1);
strcpy(d1.string1, argv[1]);
return 0;
}
gcc -O2 -v
Using built-in specs.
COLLECT_GCC=gcc-4.8
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-4ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu2)
COLLECT_GCC_OPTIONS='-O2' '-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE 2.cpp -quiet -dumpbase 2.cpp -mtune=generic -march=x86-64 -auxbase 2 -O2 -version -fstack-protector -Wformat -Wformat-security -o /tmp/cc7vXPnz.s
GNU C++ (Ubuntu 4.8.5-4ubuntu2) version 4.8.5 (x86_64-linux-gnu)
compiled by GNU C version 4.8.5, GMP version 6.1.0, MPFR version 3.1.3-p5, MPC version 1.0.3
warning: MPFR header version 3.1.3-p5 differs from library version 3.1.4.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/4.8"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.8
/usr/include/x86_64-linux-gnu/c++/4.8
/usr/include/c++/4.8/backward
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu 4.8.5-4ubuntu2) version 4.8.5 (x86_64-linux-gnu)
compiled by GNU C version 4.8.5, GMP version 6.1.0, MPFR version 3.1.3-p5, MPC version 1.0.3
warning: MPFR header version 3.1.3-p5 differs from library version 3.1.4.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f5d21a1dc5d9b70befc71d2006b2e7c5
COLLECT_GCC_OPTIONS='-O2' '-v' '-mtune=generic' '-march=x86-64'
as -v --64 -o /tmp/ccpgLPzV.o /tmp/cc7vXPnz.s
GNU assembler version 2.26.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.26.1
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-O2' '-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /tmp/ccpgLPzV.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
【问题讨论】:
-
能否请您发布实际的可编译代码,包括
#include和int main?请包括您正在使用的编译器版本和编译器选项。the program crashed.为什么会崩溃?a_string比 1024 长吗? -
@KamilCuk,请检查我对帖子的更改。
-
_EncodedBuffer1以_开头,后跟大写字母的标识符被保留。不要在你的代码中使用它们。strcpy(d1.string1, argv[1]);你用什么参数执行程序?当argv[1]是NULL时,它肯定会“崩溃”。您正在使用哪些编译器选项?bool bool_balue;至少#include <stdbool.h>。我知道我可能很挑剔 - 但minimal reproducible example 真的会有所帮助。 -
由 gcc -O2 和 ./a.out 编译 123 崩溃:*** 检测到缓冲区溢出 ***:./a.out 终止
-
那会很奇怪 - 要在 glibc 上生成
__strcpy_chk,您的编译器选项中必须有-D_FORTIFY_SOURCE=1。你?如果您在源文件顶部添加#undef _FORTIFY_SOURCE会发生什么?如果您将-D_FORTIFY_SOURCE=0添加到您的编译器选项会发生什么?你能在编译文件时显示gcc -O2 -v的输出吗?您使用的是什么 linux 发行版(如果使用的是 linux,是吗?)?
标签: gcc buffer-overflow strcpy variable-length