Debian Codegraph @

tests/sqlite/rustsec.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use sqlxrust-analyzer cargo sqlx 0.8.3 crate/::{Connectionrust-analyzer cargo sqlx-core 0.8.3 connection/Connection#, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#, SqliteConnectionrust-analyzer cargo sqlx-sqlite 0.8.3 connection/SqliteConnection#};
2
3 // https://rustsec.org/advisories/RUSTSEC-2024-0363.html
4 //
5 // Similar theory to the Postgres exploit in `tests/postgres/rustsec.rs` but much simpler
6 // since we just want to overflow the query length itself.
7 #[sqlxrust-analyzer cargo sqlx 0.8.3 crate/::testrust-analyzer cargo sqlx-macros 0.8.3 test:]
•8 #[cfg(not(target_pointer_width = "32"))]
9 async fn rustsec_2024_0363rust-analyzer cargo sqlx 0.8.3 rustsec_2024_0363().() -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> {
10 let overflow_len = 4 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 1024 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 1024 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 1024; // 4 GiB
11
12 // `real_query_prefix` plus `fake_message` will be the first query that SQLite "sees"
13 //
14 // Rather contrived because this already represents a regular SQL injection,
15 // but this is the easiest way to demonstrate the exploit for SQLite.
16 let real_query_prefix = "INSERT INTO injection_target(message) VALUES ('";
17 let fake_message = "fake_msg') RETURNING id;";
18 let real_query_suffix = "') RETURNING id";
19
20 // Our actual payload is another query
21 let real_payload =
22 "\nUPDATE injection_target SET message = 'you''ve been pwned!' WHERE id = 1;\n--";
23
24 // This will parse the query up to `real_payload`.
25 let fake_payload_len = real_query_prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). fake_message.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().();
26
27 // Pretty easy to see that this will overflow to `fake_payload_len`
28 let target_len = overflow_len +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). fake_payload_len;
29
30 let inject_len = target_len -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). real_query_prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). real_query_suffix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().();
31
32 let pad_len = inject_len -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). fake_message.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). real_payload.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().();
33
34 let mut injected_value = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(inject_len);
35 injected_value.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(fake_message);
36 injected_value.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(real_payload);
37
38 let padding = " ".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(pad_len);
39 injected_value.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&padding);
40
41 let query = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{real_query_prefix}{injected_value}{real_query_suffix}");
42
43 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(query.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(), target_len);
44
45 let mut conn = SqliteConnectionrust-analyzer cargo sqlx-sqlite 0.8.3 connection/SqliteConnection#::connectrust-analyzer cargo sqlx-core 0.8.3 connection/Connection#connect().("sqlite://:memory:").rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
46
47 sqlxrust-analyzer cargo sqlx 0.8.3 crate/::raw_sqlrust-analyzer cargo sqlx-core 0.8.3 raw_sql/raw_sql().(
48 "CREATE TABLE injection_target(id INTEGER PRIMARY KEY, message TEXT);\n\
49 INSERT INTO injection_target(message) VALUES ('existing message');",
50 )
51 .executerust-analyzer cargo sqlx-core 0.8.3 raw_sql/impl#[`RawSql<'q>`]execute().(&mut conn)
52 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
53
54 let res = sqlxrust-analyzer cargo sqlx 0.8.3 crate/::raw_sqlrust-analyzer cargo sqlx-core 0.8.3 raw_sql/raw_sql().(&query).executerust-analyzer cargo sqlx-core 0.8.3 raw_sql/impl#[`RawSql<'q>`]execute().(&mut conn).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().;
55
56 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) = res {
57 // Connection rejected the query; we're happy.
58 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(e, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#::Protocolrust-analyzer cargo sqlx-core 0.8.3 error/Error#Protocol#(_)) {
59 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(());
60 }
61
62 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("unexpected error: {e:?}");
63 }
64
65 let messages: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> =
66 sqlxrust-analyzer cargo sqlx 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("SELECT message FROM injection_target ORDER BY id")
67 .fetch_allrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_all().(&mut conn)
68 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
69
70 // If the injection succeeds, `messages` will look like:
71 // ["you've been pwned!'.to_string(), "fake_msg".to_string()]
72 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
73 messages,
74 ["existing message".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "fake_msg".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()]
75 );
76
77 // Injection didn't affect our database; we're happy.
78 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
79 }