served

Articles ·

The SQLite function that almost shipped

Before launch, our tests found a way for a customer's SQL to hand the server a memory address and call it code. Here is how it got past the first round of tests, and what we changed.

Served runs SQL written by strangers. The whole design rests on one rule: inside your own database you can do anything, and nothing you run can reach outside it. Before we opened the doors, a test showed that rule had a hole in it.

A function that takes a pointer

SQLite's older full-text search engine, FTS3, lets a program plug in its own tokenizer, the piece of code that splits text into words. The way a program registers one is a SQL function:

SELECT fts3_tokenizer('mine', ?);

The second argument is a memory address, passed as a blob. It points at a table of C functions. From then on, any FTS3 table that asks for the tokenizer mine calls whatever is at that address.

With one argument, the function goes the other way and returns the address of a tokenizer that already exists:

SELECT hex(fts3_tokenizer('simple'));

So one call tells you where real code lives in the server's memory, and another lets you point the database at an address of your choosing. Put together, that is how you get a server to run code it was never meant to.

SQLite already knows

None of this is a secret. SQLite has shipped with the two-argument form switched off for years. But there are two exceptions, and we fell into both.

The first is that a build can switch it back on. Debian's build of SQLite does, to keep old software working. Debian is what our production image was based on.

The second is subtler. Even when the feature is off, SQLite still accepts the address if it arrives as a bound parameter and not as text in the SQL. The reasoning is sound for an ordinary program: SQL text might come from a user, but a bound parameter is supplied by the program itself, which is trusted.

On a hosted database that reasoning turns inside out. Our API takes parameters from the customer:

{"sql": "SELECT fts3_tokenizer('mine', ?)", "args": [{"base64": "..."}]}

The parameter SQLite trusts because "the application supplied it" is supplied by exactly the person it should not trust.

How it got past the first tests

We keep a list of every way we know to reach outside a database from SQL, and a test that tries each one and fails if any succeeds. fts3_tokenizer was on the list. The test passed.

It passed because the test tried the address written into the SQL, and the machine it ran on had a SQLite build that refuses that form. The test was checking the build's default, not our defence.

What caught it was running the same tests on a second machine with the production build. There, the plain form was accepted, the test failed, and looking into why turned up the bound-parameter form, which worked everywhere, including on the machine where everything had been green.

What we changed

What we took from it

A feature being off is not a defence. It is someone else's default, and it varies by build. If something must never run, refuse it yourself, by name.

Test on what production runs. The cheapest fix was the last one: make there be only one SQLite, so that "it passed here" means it passed.

Read the exceptions. The parameter rule is documented. It is a sensible rule for the program SQLite's authors had in mind. A service that passes strangers' parameters straight through is not that program, and every "trusted because the application did it" in a library's documentation needs reading again with that in mind.

This was found and fixed before Served held anyone's data. We are writing it down because it is the sort of thing you should expect a database host to tell you.

Served is hosted SQLite. Real SQLite behind an HTTP API, $3 a month, and the file is yours to download whenever you like.

Read the docs