SmallBASIC 是一个快速且易于学习的 BASIC 语言解释器,非常适合日常计算、脚本和原型开发。SmallBASIC 包含三角函数、矩阵和代数函数、内置 IDE、强大的字符串库、系统、声音和图形命令,以及结构化编程语法。
在 src/platform/sdl/main.cpp 的第 277 行,调用了 strcpy 将 argv 复制到全局变量 opt_command,该变量定义在 src/common/smbas.h 中。根据同一文件中定义的 OPT_CMD_CZ 宏,该全局变量是一个固定大小的数组。
// src/common/smbas.h:105
EXTERN char opt_command[OPT_CMD_CZ] //
// src/platform/sdl/main.cpp:263
while (1) {
int option_index = 0;
int c = getopt_long(argc, argv, "hvkc:f:r:x:n:m:e:d:p:", OPTIONS, &option_index);
if (c == -1) {
// no more options
if (!option_index) {
for (int i = 1; i < argc; i++) {
const char *s = argv[i];
int len = strlen(s);
if (runFile == NULL
&& ((strcasecmp(s + len - 4, ".bas") == 0 && access(s, 0) == 0)
|| (strstr(s, "://") != NULL))) {
runFile = strdup(s);
} else if (chdir(s) != 0) {
strcpy(opt_command, s); // VULNERABLE
}
}
}
break;
}
根据 src/common/smbas.h,OPT_CMD_CZ 定义为 1024。
#define OPT_CMD_CZ 1024
与 strlcpy 函数不同,strcpy 不检查要复制的字节数。因此,超过 1024 字节的数据可以被复制到 opt_command 数组中。由于命令行参数可能超过 1024 字节,输入可以覆盖其他对象。这可能导致程序 sbasicg 出现意外行为。
请注意,在 Windows 的命令提示符中,可使用的字符串最大长度为 8191 个字符。在 Linux 中,长度取决于 ARG_MAX 和 MAX_ARG_STRINGS。
构建 SmallBASIC 项目,然后运行:
$ src/platform/sdl/sbasicg $(python -c 'print("a"*1024)')
这会在 main 函数解析命令行参数时导致全局缓冲区溢出。以下是 Address Sanitizer 的输出:
=================================================================
==5322==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000016c22a0 at pc 0x0000004ba2e4 bp 0x7ffeb3ebac70 sp 0x7ffeb3eba428
WRITE of size 1025 at 0x0000016c22a0 thread T0
#0 0x4ba2e3 in strcpy (/home/ch1keen/fuzz/SmallBASIC/src/platform/sdl/sbasicg+0x4ba2e3) (BuildId: 4a800899b4dac082870bb647e1d872811e8633ea)
#1 0x8759c6 in main /home/ch1keen/fuzz/SmallBASIC/src/platform/sdl/main.cpp:277:13
#2 0x79e3ca62a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#3 0x79e3ca62a28a in __libc_start_main csu/../csu/libc-start.c:360:3
#4 0x4378b4 in _start (/home/ch1keen/fuzz/SmallBASIC/src/platform/sdl/sbasicg+0x4378b4) (BuildId: 4a800899b4dac082870bb647e1d872811e8633ea)
0x0000016c22a0 is located 0 bytes after global variable 'opt_command' defined in '/home/ch1keen/fuzz/SmallBASIC/src/common/smbas.h:105' (0x16c1ea0) of size 1024
SUMMARY: AddressSanitizer: global-buffer-overflow (/home/ch1keen/fuzz/SmallBASIC/src/platform/sdl/sbasicg+0x4ba2e3) (BuildId: 4a800899b4dac082870bb647e1d872811e8633ea) in strcpy
Shadow bytes around the buggy address:
0x0000016c2000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0000016c2280: 00 00 00 00[f9]f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
0x0000016c2300: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
0x0000016c2380: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0000016c2500: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==5322==ABORTING
该漏洞的影响尚不明确。但全局缓冲区溢出可能会覆盖其他全局变量,这可能导致程序标志和设置被覆盖。
该漏洞已通过将几乎所有 strcpy 替换为 strlcpy 来修复,包括我报告的问题。
@@ -277,1 +277,1 @@ int main(int argc, char* argv[]) {
< strcpy(opt_command, command);
---
> strlcpy(opt_command, command, sizeof(opt_command));
查看该 commit 了解详细的补丁内容。