Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/PLUGIN_AUTHORING.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Defined by `PluginPermission` in `plugin-api/src/main/kotlin/com/itsaky/androidi
| `project.structure` | Modify project structure |
| `native.code` | Execute native machine code |
| `ide.environment.write` | Write to IDE-managed directories (Android SDK, NDK, cache) |
| `debugger.endpoints` | Read the port and token of the open project's SQLite debugger servers, and so query the apps' databases (read-only for now) |
| `debugger.endpoints` | Read the port and token of the open project's SQLite debugger servers, and so query and write the apps' databases |

Plugins run in Code on the Go's process. A permission declares what a plugin means to do, and the
services check it on every call, but it cannot stop a plugin that reaches into the host by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ enum class PluginPermission(
IDE_ENVIRONMENT_WRITE("ide.environment.write", "Write to IDE-managed directories such as the Android SDK, NDK, and cache"),
DEBUGGER_ENDPOINTS(
"debugger.endpoints",
"Read the port and token of the open project's SQLite debugger servers. The token lets the plugin query the " +
"databases of the project's apps: read-only for now, and later versions may allow writes. Plugins run in " +
"Read the port and token of the open project's SQLite debugger servers. The token lets the plugin query and " +
"change the databases of the project's apps. Plugins run in " +
"Code on the Go's process, so this declares intent and cannot stop reflection",
),
}
Expand Down
9 changes: 5 additions & 4 deletions subprojects/sqlite-debugger/README.md

Large diffs are not rendered by default.

58 changes: 41 additions & 17 deletions subprojects/sqlite-debugger/src/main/cpp/sqlitedbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,26 @@ void sqlitedbg_set_client_connected(int connected) {
__atomic_store_n(&g_client_connected, connected ? 1 : 0, __ATOMIC_RELAXED);
}

static int server_authorizer(void *unused, int action, const char *a1, const char *a2, const char *db,
// The user data of a writable server connection's authorizer; a read-only one gets NULL.
static const int k_writable = 1;

// PRAGMAs that read and take a table, an index or a check limit as their argument, the ones a client
// needs to read a schema.
static const char *const k_read_pragmas[] = {"table_info", "table_xinfo", "table_list", "index_info", "index_xinfo",
"index_list", "foreign_key_list", "foreign_key_check", "integrity_check",
"quick_check"};

// Whether a PRAGMA given an argument (PRAGMA x = v, PRAGMA x(v)) may run: a read of that kind, or
// user_version on a writable connection.
static int pragma_argument_allowed(const char *name, int writable) {
if (writable && sqlite3_stricmp(name, "user_version") == 0) return 1;
for (size_t i = 0; i < sizeof k_read_pragmas / sizeof k_read_pragmas[0]; i++)
if (sqlite3_stricmp(name, k_read_pragmas[i]) == 0) return 1;
return 0;
}

static int server_authorizer(void *writable, int action, const char *a1, const char *a2, const char *db,
const char *trigger) {
(void)unused;
(void)a2;
(void)db;
(void)trigger;
switch (action) {
Expand Down Expand Up @@ -422,6 +438,8 @@ static int server_authorizer(void *unused, int action, const char *a1, const cha
case SQLITE_ALTER_TABLE:
case SQLITE_REINDEX:
case SQLITE_ANALYZE:
return writable ? SQLITE_OK : SQLITE_DENY;
// ATTACH would open a file the app may not have open (ADFA-7131, Design 3), writable or not.
case SQLITE_ATTACH:
case SQLITE_DETACH:
// A client could open a transaction or savepoint and hold a lock on the app's database.
Expand All @@ -431,20 +449,25 @@ static int server_authorizer(void *unused, int action, const char *a1, const cha
// Only the ROLLBACK of sqlitedbg_end_transaction, which ends one, gets past.
return tls_ending_transaction && a1 && strcmp(a1, "ROLLBACK") == 0 ? SQLITE_OK : SQLITE_DENY;
case SQLITE_PRAGMA:
// PRAGMA query_only=0 would lift the guard, and a PRAGMA that stores a value writes.
// PRAGMA table_info(t) also has a second argument, so only these names are checked.
// locking_mode=EXCLUSIVE keeps the lock after a read, and busy_timeout (any form) lets a
// statement wait on the app's lock for as long as the client likes. The heap limits and
// temp_store_directory are process-wide: they would change the app's own connections.
// Refused on every server connection, as they would reach the app's own connections or its
// file format, not just data. locking_mode=EXCLUSIVE keeps the lock after a statement, and
// busy_timeout (any form) lets a statement wait on the app's lock for as long as the client
// likes. The heap limits and temp_store_directory are process-wide. query_only is the
// server's own guard. writable_schema can leave a schema the app's SQLite reads as corrupt.
if (a1 && (sqlite3_stricmp(a1, "query_only") == 0 || sqlite3_stricmp(a1, "writable_schema") == 0 ||
sqlite3_stricmp(a1, "locking_mode") == 0 || sqlite3_stricmp(a1, "busy_timeout") == 0 ||
sqlite3_stricmp(a1, "hard_heap_limit") == 0 || sqlite3_stricmp(a1, "soft_heap_limit") == 0 ||
sqlite3_stricmp(a1, "temp_store_directory") == 0 || sqlite3_stricmp(a1, "wal_checkpoint") == 0 ||
sqlite3_stricmp(a1, "incremental_vacuum") == 0 || sqlite3_stricmp(a1, "optimize") == 0))
sqlite3_stricmp(a1, "temp_store_directory") == 0))
return SQLITE_DENY;
if (a1 && a2 &&
(sqlite3_stricmp(a1, "journal_mode") == 0 || sqlite3_stricmp(a1, "user_version") == 0 ||
sqlite3_stricmp(a1, "application_id") == 0 || sqlite3_stricmp(a1, "schema_version") == 0))
// A PRAGMA with an argument sets a value, unless it is a read that takes a name or a limit.
// SQLite applies a setting while it prepares the statement, so refusing the write later
// (sqlite3_stmt_readonly) is too late: foreign_keys, ignore_check_constraints, journal_mode,
// schema_version, cache_size ... would stay changed on the cached connection.
if (a1 && a2 && !pragma_argument_allowed(a1, writable != NULL)) return SQLITE_DENY;
if (writable) return SQLITE_OK;
// Read-only: a PRAGMA that writes the file is a write.
if (a1 && (sqlite3_stricmp(a1, "wal_checkpoint") == 0 || sqlite3_stricmp(a1, "incremental_vacuum") == 0 ||
sqlite3_stricmp(a1, "optimize") == 0))
return SQLITE_DENY;
return SQLITE_OK;
default:
Expand Down Expand Up @@ -477,10 +500,11 @@ int sqlitedbg_open_server(const char *path, int writable, sqlite3 **db) {
tls_opening_server = 1;
int rc = sqlite3_open_v2(path, db, writable ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY, NULL);
tls_opening_server = 0;
if (rc == SQLITE_OK && !writable) {
rc = sqlite3_exec(*db, "PRAGMA query_only=1", NULL, NULL, NULL);
if (rc == SQLITE_OK) rc = sqlite3_set_authorizer(*db, server_authorizer, NULL);
}
if (rc == SQLITE_OK && !writable) rc = sqlite3_exec(*db, "PRAGMA query_only=1", NULL, NULL, NULL);
// A write must not leave orphan rows whatever the app's own connections do. db_config, not a PRAGMA:
// the authorizer refuses PRAGMA foreign_keys, and is not installed yet.
if (rc == SQLITE_OK && writable) rc = sqlite3_db_config(*db, SQLITE_DBCONFIG_ENABLE_FKEY, 1, NULL);
if (rc == SQLITE_OK) rc = sqlite3_set_authorizer(*db, server_authorizer, writable ? (void *)&k_writable : NULL);
// In rollback-journal mode a reader fails at once while the app holds a write lock. Wait a little,
// well under the tools' 50 s deadline; the client cannot change it (PRAGMA busy_timeout is refused).
if (rc == SQLITE_OK) rc = sqlite3_busy_timeout(*db, SQLITEDBG_SERVER_BUSY_MS);
Expand Down
39 changes: 25 additions & 14 deletions subprojects/sqlite-debugger/src/main/cpp/sqlitedbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,40 @@ void sqlitedbg_set_client_connected(int connected);
// (BUSY_TIMEOUT_MS in android_database_SQLiteConnection.cpp, requery/sqlite-android 3.49.0, copied from
// AOSP). The cap sits below that, with margin for the busy handler's back-off and the app's own work, so
// a long read never makes the app's commit fail with "database is locked" (ADFA-7136 acceptance
// criterion 5). A WAL database needs no cap: readers and the writer do not block each other.
// criterion 5). A WAL database needs no cap for a read: readers and the writer do not block each other.
// A write is capped in every mode: it holds the write lock, which the app's own writes wait for in the
// same busy handler (ADFA-7298).
#define SQLITEDBG_SERVER_NON_WAL_MS 1500

// Opens path for the server with the same SQLite the app uses: SQLITE_OPEN_READONLY unless
// writable, PRAGMA query_only when read-only, and an authorizer that refuses ordinary writes when
// read-only, and also BEGIN, SAVEPOINT, PRAGMA locking_mode and PRAGMA busy_timeout: a client cannot
// hold a lock across statements. Within one statement a lock is held while it runs, so the JNI query
// path stops a statement on a database that is not in WAL mode after SQLITEDBG_SERVER_NON_WAL_MS. The process-wide PRAGMAs hard_heap_limit, soft_heap_limit and
// temp_store_directory are refused too: they would change the app's own connections. Read-only mode guards against accidents; it is not a security boundary (ADFA-7131,
// D11). Never traced and never listed. The caller owns *db and closes it with sqlite3_close_v2.
// Opens path for the server with the same SQLite the app uses, SQLITE_OPEN_READONLY unless writable
// (never SQLITE_OPEN_CREATE), with an authorizer. On every server connection it refuses BEGIN, COMMIT,
// ROLLBACK, SAVEPOINT and RELEASE, ATTACH and DETACH, and the PRAGMAs that would reach the app rather
// than the data: locking_mode and busy_timeout (a client could hold a lock across statements, or wait
// on the app's for as long as it likes), the process-wide hard_heap_limit, soft_heap_limit and
// temp_store_directory, query_only and writable_schema. A PRAGMA with an argument is refused too, as SQLite
// applies a setting while it prepares the statement, unless it reads (table_info, table_xinfo, table_list,
// index_info, index_xinfo, index_list, foreign_key_list, foreign_key_check, integrity_check, quick_check)
// or is user_version on a writable connection.
// A writable connection also enforces foreign keys, whatever the app's connections do.
// Read-only also sets PRAGMA query_only and refuses every write. Within one statement a lock is held
// while it runs, so the JNI layer stops a read on a database that is not in WAL mode, and every
// write, once it has held its lock for SQLITEDBG_SERVER_NON_WAL_MS. Read-only mode guards against
// accidents; it is not a security boundary (ADFA-7131, D11), and a writable connection can change
// any data the app's can.
// Never traced and never listed. The caller owns *db and closes it with sqlite3_close_v2.
// A server connection, unlike an app connection, may use a progress handler (for time limits).
// The connection waits up to SQLITEDBG_SERVER_BUSY_MS for a lock the app holds (rollback-journal mode)
// and then reports SQLITE_BUSY.
// The connection waits up to SQLITEDBG_SERVER_BUSY_MS for a lock the app holds and then reports
// SQLITE_BUSY. The JNI layer's execute replaces that wait for the statement, to end it at the time
// limit too, and puts it back after.
// Returns SQLITE_MISUSE before any app connection has opened (the extension then has no SQLite
// API to call), or what sqlite3_open_v2 returns. A failure after the open (PRAGMA query_only, the
// authorizer or the busy timeout) closes the connection, sets *db to NULL and returns that code, as
// does a failed open: *db is non-NULL only with SQLITE_OK.
int sqlitedbg_open_server(const char *path, int writable, sqlite3 **db);

// Ends a transaction left open on a server connection with ROLLBACK, which the read-only authorizer
// lets past for this call only: a client's BEGIN, COMMIT and ROLLBACK stay refused. The JNI layer
// calls it after every query as a backstop; nothing a client can send opens one on a read-only
// connection. Returns SQLITE_OK when none was open or it was rolled back, else what ROLLBACK
// Ends a transaction left open on a server connection with ROLLBACK, which the authorizer lets past
// for this call only: a client's BEGIN, COMMIT and ROLLBACK stay refused. The JNI layer calls it after
// every query and execute as a backstop; nothing a client can send opens one. Returns SQLITE_OK when none was open or it was rolled back, else what ROLLBACK
// returned, and the transaction is still open.
int sqlitedbg_end_transaction(sqlite3 *db);

Expand Down
Loading
Loading