
Report e PoC di un Buffer Overflow Globale su SmallBASIC prima di 02364eff880ba62afac67bcceebafade2b40d21f
SmallBASIC è un interprete del linguaggio BASIC veloce e facile da apprendere, ideale per calcoli quotidiani, script e prototipi. SmallBASIC include funzioni trigonometriche, di matrici e algebra, un IDE integrato, una potente libreria per stringhe, comandi di sistema, suono e grafica, insieme a una sintassi di programmazione strutturata.
Alla riga 277 di src/platform/sdl/main.cpp, la funzione strcpy viene chiamata per copiare argv nella variabile globale opt_command, definita in src/common/smbas.h. La variabile globale è un array di dimensione fissa, secondo la macro OPT_CMD_CZ definita nello stesso file.
// 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;
}
Secondo src/common/smbas.h, OPT_CMD_CZ è definito come 1024.
#define OPT_CMD_CZ 1024
A differenza della funzione strlcpy, strcpy non verifica quanti byte devono essere copiati. Pertanto, più di 1024 byte possono essere copiati nell'array opt_command. Poiché l'argomento della riga di comando può essere più lungo di 1024 byte, l'input può sovrascrivere altri oggetti. Ciò può portare il programma sbasicg a comportamenti imprevisti.
Nota che la lunghezza massima della stringa che puoi utilizzare al prompt dei comandi in Windows è di 8191 caratteri. In Linux, la lunghezza dipende da ARG_MAX e MAX_ARG_STRINGS.
Compila il progetto SmallBASIC, poi esegui
$ src/platform/sdl/sbasicg $(python -c 'print("a"*1024)')
Causa un Global Buffer Overflow durante l'analisi dell'argomento della riga di comando implementato nella funzione main. Di seguito è riportato l'output di 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
L'impatto della vulnerabilità non è chiaro. Ma il Global Buffer Overflow può sovrascrivere altre variabili globali, il che potrebbe portare alla sovrascrittura di flag e impostazioni del programma.
La vulnerabilità è stata corretta sostituendo quasi tutti i strcpy con strlcpy, incluso il problema che ho segnalato.
@@ -277,1 +277,1 @@ int main(int argc, char* argv[]) {
< strcpy(opt_command, command);
---
> strlcpy(opt_command, command, sizeof(opt_command));
Vedi il commit per la patch dettagliata.