Skip to content
Merged
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
51 changes: 38 additions & 13 deletions apple/TablePro/Core/RemoteSQLite/RemoteSQLiteAgentSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// TablePro
//

import Compression
import Foundation

/// The Python program TablePro runs on the SSH server to open a SQLite database with the server's
Expand All @@ -19,27 +20,51 @@ enum RemoteSQLiteAgentSource {
/// `platform-python` under `/usr/libexec` with no `python3` on `PATH`.
private static let interpreters = ["python3", "/usr/bin/python3", "/usr/libexec/platform-python"]

/// The bootstrap each interpreter runs: decode and inflate the program from its first argument,
/// then run it. `COMPRESSION_ZLIB` emits raw DEFLATE, so `zlib.decompress` takes wbits `-15`.
static let interpreterBootstrap = "import base64,zlib,sys;exec(zlib.decompress(base64.b64decode(sys.argv[1]),-15))"

/// The program, raw-DEFLATE compressed then base64, as the launcher ships it. Compressed because
/// the exec request is one SSH packet, and libssh2 rejects an uncompressed payload at or above
/// `MAX_SSH_PACKET_LEN - 0x100` (34744) with `LIBSSH2_ERROR_INVAL`; the raw 24 KB base64, carried
/// once per interpreter, ran past that. 4.8 KB compressed leaves room as the program grows.
static let encodedProgram: String = {
let source = Data(python.utf8)
let capacity = source.count + 1_024
var deflated = Data(count: capacity)
let written = deflated.withUnsafeMutableBytes { destination -> Int in
source.withUnsafeBytes { input -> Int in
guard let destinationBase = destination.bindMemory(to: UInt8.self).baseAddress,
let inputBase = input.bindMemory(to: UInt8.self).baseAddress else { return 0 }
return compression_encode_buffer(destinationBase, capacity, inputBase, source.count, nil, COMPRESSION_ZLIB)
}
}
// 0 means the output did not fit `capacity`; raw DEFLATE of this text never grows past it.
precondition(written > 0, "RemoteSQLiteAgentSource could not compress the agent program")
deflated.removeSubrange(written..<deflated.count)
return deflated.base64EncodedString()
}()

/// The command handed to `libssh2_channel_process_startup("exec", …)`.
///
/// The whole string is a fixed literal but for the base64 of the program, which is drawn from
/// the base64 alphabet alone and is therefore safe as a bare shell word and inside single
/// quotes under sh, bash, zsh, fish and csh (measured). The server never sees a byte of user
/// data on this line: the database path and every value travel as framed protocol fields once
/// the program is running, never as shell text.
/// The whole string is a fixed literal but for the base64 blob, which is drawn from the base64
/// alphabet alone and is therefore safe as a bare shell word and inside single quotes under sh,
/// bash, zsh, fish and csh (measured). It is carried once, in `b`, rather than per interpreter.
/// The server never sees a byte of user data on this line: the database path and every value
/// travel as framed protocol fields once the program is running, never as shell text.
///
/// `-I` runs the interpreter isolated, so `PYTHON*` variables and the user site directory
/// cannot redirect it. When no interpreter is found the launcher prints the agent's own notice
/// on standard output, where it reaches the client as a launcher notice rather than being lost
/// on standard error, and exits cleanly.
/// `-I` runs the interpreter isolated, so `PYTHON*` variables and the user site directory cannot
/// redirect it. When no interpreter is found the launcher prints the agent's own notice on
/// standard output, where it reaches the client as a launcher notice rather than being lost on
/// standard error, and exits cleanly.
static func launcherCommand() -> String {
let encoded = Data(python.utf8).base64EncodedString()
let loop = interpreters.map { interpreter -> String in
let branches = interpreters.map { interpreter -> String in
let present = #"command -v "\#(interpreter)" >/dev/null 2>&1"#
let run = #"exec "\#(interpreter)" -I -S -c "import base64,sys;exec(base64.b64decode(sys.argv[1]))" \#(encoded)"#
let run = #"exec "\#(interpreter)" -I -S -c "\#(interpreterBootstrap)" "$b""#
return "if \(present); then \(run); fi"
}
.joined(separator: "; ")
return "sh -c '" + loop + "; echo " + RemoteSQLiteWire.noPythonNotice + "; exit 0'"
return "sh -c 'b=" + encodedProgram + "; " + branches + "; echo " + RemoteSQLiteWire.noPythonNotice + "; exit 0'"
}

static let python = #"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ struct RemoteSQLiteAdmissionTests {
#expect(token.allSatisfy { $0.isHexDigit })
}

@Test func launcherCommandCarriesNoUserDataAndFitsTheArgumentLimit() {
@Test func launcherCommandCarriesNoUserDataAndFitsThePacketLimit() {
let command = RemoteSQLiteAgentSource.launcherCommand()
#expect(command.hasPrefix("sh -c '"))
#expect(command.hasPrefix("sh -c 'b="))
#expect(command.contains(RemoteSQLiteWire.noPythonNotice))
#expect(command.contains("base64.b64decode"))
// Well under the Linux MAX_ARG_STRLEN of 128 KiB.
#expect(command.utf8.count < 100_000)
#expect(command.contains("zlib.decompress"))
// The exec request is one SSH packet: this command plus SSH_MSG_CHANNEL_REQUEST framing.
// libssh2 rejects a payload at or above MAX_SSH_PACKET_LEN - 0x100 (34744) with -34; 256 B
// covers the framing and still leaves room to spare.
#expect(command.utf8.count + 256 < 34_744)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ struct RemoteSQLiteAgentPipeTests {
private var buffer: [UInt8] = []

init(pythonPath: String) throws {
let encoded = Data(RemoteSQLiteAgentSource.python.utf8).base64EncodedString()
// Launch the program the way the launcher ships it: the compressed, base64 blob through
// the same bootstrap, so this suite also covers the decompression round-trip.
process.executableURL = URL(fileURLWithPath: pythonPath)
process.arguments = ["-I", "-S", "-c", "import base64,sys;exec(base64.b64decode(sys.argv[1]))", encoded]
process.arguments = [
"-I", "-S", "-c", RemoteSQLiteAgentSource.interpreterBootstrap, RemoteSQLiteAgentSource.encodedProgram,
]
process.standardInput = toAgent
process.standardOutput = fromAgent
process.standardError = FileHandle.nullDevice
Expand Down
1 change: 1 addition & 0 deletions apple/changelog.d/4249.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A SQLite over SSH connection set to open the database on the server never connected.
89 changes: 89 additions & 0 deletions apple/scripts/probes/remote-sqlite-launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""Probe the remote SQLite launcher: exec-request size and a HELLO -> READY round-trip.

Why this exists: the agent program is delivered inline on the SSH exec request, and libssh2
rejects an uncompressed packet payload at or above MAX_SSH_PACKET_LEN - 0x100 = 34744 with
LIBSSH2_ERROR_INVAL (-34). RemoteSQLiteAgentSource compresses the program (Apple
COMPRESSION_ZLIB, raw DEFLATE) and carries it once. This stands in on the CLI with zlib raw
DEFLATE (same format, so the production bootstrap's wbits -15 decompresses it; sizes are close,
not byte-identical to COMPRESSION_ZLIB), run against the local python3.

Run from the repo root: python3 apple/scripts/probes/remote-sqlite-launcher.py
"""
import base64
import re
import struct
import subprocess
import sys
import zlib
from pathlib import Path

LIMIT = 35000 - 0x100 # MAX_SSH_PACKET_LEN - 0x100, libssh2 src/transport.c
INTERPRETERS = ["python3", "/usr/bin/python3", "/usr/libexec/platform-python"]
BOOTSTRAP = "import base64,zlib,sys;exec(zlib.decompress(base64.b64decode(sys.argv[1]),-15))"
NOTICE = "TPRSQL:NO_PYTHON"
SOURCE = Path(__file__).resolve().parents[2] / "TablePro/Core/RemoteSQLite/RemoteSQLiteAgentSource.swift"


def agent_program() -> bytes:
text = SOURCE.read_text(encoding="utf-8")
match = re.search(r'static let python = #"""\n(.*)\n"""#', text, re.S)
if not match:
sys.exit("could not extract the agent program from " + str(SOURCE))
# Matches Data(python.utf8): the multiline literal has no trailing newline.
return match.group(1).encode("utf-8")


def raw_deflate(data: bytes) -> bytes:
comp = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
return comp.compress(data) + comp.flush()


def launcher_command(program: bytes) -> str:
blob = base64.b64encode(raw_deflate(program)).decode()
branches = "; ".join(
f'if command -v "{i}" >/dev/null 2>&1; then exec "{i}" -I -S -c "{BOOTSTRAP}" "$b"; fi'
for i in INTERPRETERS
)
return "sh -c 'b=" + blob + "; " + branches + "; echo " + NOTICE + "; exit 0'"


def frame(op: int, payload: bytes) -> bytes:
return struct.pack(">I", len(payload) + 1) + bytes([op]) + payload


def read_str(body: bytes, off: int):
n = struct.unpack(">I", body[off:off + 4])[0]
return body[off + 4:off + 4 + n].decode(), off + 4 + n


def main() -> int:
program = agent_program()
command = launcher_command(program)
size = len(command.encode("utf-8"))
print(f"program {len(program)} B -> exec request {size} B (limit {LIMIT}, headroom {LIMIT - size})")
if size >= LIMIT:
print("FAIL: exec request would be rejected by libssh2")
return 1

path = b":memory:"
hello = frame(0x01, struct.pack(">I", 1) + struct.pack(">I", len(path)) + path + struct.pack(">I", 0))
result = subprocess.run(command, shell=True, input=hello, capture_output=True, timeout=30)
out = result.stdout
if len(out) < 5:
print("FAIL: no reply. stderr:", result.stderr.decode(errors="replace")[:400])
return 1
op = out[4]
body = out[5:4 + struct.unpack(">I", out[:4])[0]]
if op != 0x81: # OP_READY
print(f"FAIL: expected OP_READY, got 0x{op:02x} {body[:80]!r}")
return 1
version = struct.unpack(">I", body[:4])[0]
sqlite_version, off = read_str(body, 4)
python_version, _ = read_str(body, off)
print(f"PASS: OP_READY proto={version} sqlite={sqlite_version} python={python_version}")
return 0


if __name__ == "__main__":
raise SystemExit(main())
Loading