Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/pcacheleak.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / pcacheleak.test` 99 lines ยท 3326 bytes Raw Blame History 1 # 2026 October 24 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 6 # May you do good and not evil. 7 # May you find forgiveness for yourself and forgive others. 8 # May you share freely, never taking more than you give. 9 # 10 #*********************************************************************** 11 # 12 # This file implements a regression test for an information-leak primitive 13 # that exploits the b-tree page overread. 14 # 15 16 set testdir [file dirname $argv0] 17 source $testdir/tester.tcl 18 set testprefix pcacheleak 19 20 database_may_be_corrupt 21 22 # Build the corrupted db. 23 # 24 proc build_leak_db {filename} { 25 forcedelete $filename 26 sqlite3 dbx $filename 27 dbx eval { 28 PRAGMA page_size=4096; 29 PRAGMA auto_vacuum=0; 30 CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT); 31 INSERT INTO users(id,name) VALUES(1,'Alice'); 32 INSERT INTO users(id,name) VALUES(2,'Bob'); 33 } 34 dbx close 35 36 # Modification 1: bump the page-2 cell count from 2 to 3. The 2-byte 37 # cell count is at page offset 3; page 2 starts at file offset 0x1000, 38 # so the low byte is at 0x1004 == 4100. 39 hexio_write $filename 4100 03 40 41 # Modification 2: write a third cell pointer into the cell pointer array 42 # (which begins at page offset 8, file offset 0x1008). The two real 43 # pointers occupy 0x1008 and 0x100A; the fake third pointer goes at 44 # 0x100C == 4108 and is set to the page-relative offset 0x0FF8, i.e. the 45 # last 8 bytes of the 4096-byte page. 46 hexio_write $filename 4108 0ff8 47 48 # Modification 3: overwrite those last 8 bytes (0x1FF8 == 8184) with 49 # 0xFF. Every byte has its high bit set, so the varint parser treats 50 # them all as continuation bytes and is forced to read past the end of 51 # the page buffer to find the end of the number. 52 hexio_write $filename 8184 ffffffffffffffff 53 } 54 55 # The fake cell is parsed, so the table now appears to hold three rows 56 # instead of two. 57 # 58 do_test 1.0 { 59 db close 60 build_leak_db test.db 61 sqlite3 db test.db 62 db eval {PRAGMA cell_size_check=OFF} 63 db eval {SELECT count(*) FROM users} 64 } {3} 65 66 67 # Turning the cell size check on causes the same file to be rejected as 68 # corrupt, because the fake cell is detected before it can be parsed. 69 # 70 do_test 1.1 { 71 sqlite3 db test.db 72 db eval {PRAGMA cell_size_check=ON} 73 list [catch {db eval {SELECT rowid FROM users}} msg] $msg 74 } {1 {database disk image is malformed}} 75 db close 76 77 # The leak itself. Make four byte-identical copies of the manipulated 78 # database and open them all at once. Each open allocates its own page-2 79 # buffer, and because all four buffers are live simultaneously they must 80 # occupy four different heap addresses. If the leaked rowid contains 81 # heap address bytes, all four values should be distinct. Otherwise, 82 # they should all be identical (and either 0 or 1) 83 # 84 do_test 2.0 { 85 set vals [list] 86 foreach h {d0 d1 d2 d3} { 87 forcecopy test.db test.db.$h 88 sqlite3 $h test.db.$h 89 $h eval {PRAGMA cell_size_check=OFF} 90 lappend vals [lindex [$h eval {SELECT rowid FROM users}] end] 91 } 92 foreach h {d0 d1 d2 d3} { $h close } 93 94 # More than two distinct values (0 and 1) proves the leaked rowid 95 # the buffer's memory address rather than on the (identical) file contents. 96 list [llength [lsort -unique $vals]] [lindex $vals 0] 97 } {1 0} 98 99 finish_test