Stranger Strings: An exploitable flaw in SQLite
A signed integer overflow in SQLite's printf implementation for the %q, %Q, and %w format specifiers can cause a stack buffer overflow when large strings are passed. An attacker could crash the program, and with specific format strings and compiler behaviour, potentially overwrite the saved return address or cause an infinite loop.
Published on blog.trailofbits.com
Trail of Bits is publicly disclosing CVE-2022-35737, which affects applications that use the SQLite library API. CVE-2022-35737 was introduced in SQLite version 1.0.12 (released on October 17, 2000) and fixed in release 3.39.2 (released on July 21, 2022).
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely a signed integer overflow in the calculation of the output buffer size when scanning large input strings for quote characters, combined with compiler optimisations that treat the same variable inconsistently.
Impact
An attacker could crash the program (denial of service) in all cases, and potentially achieve arbitrary code execution when the library is compiled without stack canaries; with stack canaries present, code execution is unconfirmed.
What the attacker needed
Application calls SQLite printf with %q, %Q, or %w; input string of around 2GB; 64-bit system; format string may need ! for code execution
How it was found
- Pass a very large string (around 2GB) to sqlite3_snprintf with a format string containing %q, %Q, or %w.
- Observe the program crash due to a stack buffer overflow.
- To avoid an immediate crash, include the ! character in the format string to enable unicode scanning and craft the input with many quote characters followed by unicode prefix bytes.
- Adjust the input size to control how many bytes overflow the stack buffer and overwrite the saved return address and stack canary.
- Run the proof of concept under a debugger to confirm the saved return address is overwritten.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
SQLite is a database library used inside many programs. Its custom string formatting functions have a bug: if you give them a very long string (around 2GB) with certain format codes, a number that tracks the output size can wrap around to a negative value. This makes the program write too much data into a small fixed-size memory area on the stack, which can crash it or, in some builds, let an attacker control the program's execution.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.