Turning a 22-year-old Postgres type-confusion primitive into RCE
mehmetince.net
mehmetince.net
These seem more like privilege escalation, you have access through a database account and use that to gain full access to the whole server. So an issue for multi-tenant databases mostly.
CREATE ROLE initial LOGIN NOINHERIT NOCREATEDB NOCREATEROLE NOSUPERUSER;
And then you run SQL to validate any credentials and then check the database for any permissions, ending with SET LOCAL ROLE someuser;
It is uncommon but some people appreciate the simplicity of making the application layer do as little work as possible while pushing work into the database. It’s the logical conclusion after years of telling developers about the N+1 query problem.Now as to if this is actually a good idea and the database internal security is robust enough to handle full access, Well I have my doubts, It is a complicated poorly tested interface and results as disclosed by the article in question are probably not surprising.
I do run a toy application, where the experiment was to see if I could push all authentication and access control into the database, It is actually sort of neat not needing an auth layer in the application, but attacks like this were half expected.
see also: the schemaverse. A multiplayer space game where the game engine is postgres. https://github.com/Abstrct/Schemaverse
Remote does not mean unauthenticated, it means "not local" - as in, over the network.
According to the article, Postgres has an internal Datum that might be a pointer or not a pointer. Okay, this is already a bit risky.
Datum comes along with a small-integer type id. That id is stored separately, presumably for efficiency? Maybe you have lots of Datums and one id and you do this for efficiency. Okay, as long as there is a clear story for why the id stays correctly bound to the datum. The “internal” thing where a bogus AST will cause type confusion makes me think the story here isn’t great.
But then I learn that the type id is something that the code looks up in a system table in the database. I’m speechless. This is the kind of thing that can’t be represented in any safe language because the indication of whether a value is a pointer isn’t even in application memory.
Eww. Maybe the AIs can exhaustively enumerate every code path that might get it wrong and rule them all out in a particular release. Maybe there’s even a Lean program that can verify it. But this is still the kind of design that is wrong by construction and is only okay if every single detail avoids the giant foot-cannon.
Welcome to the wonderful world of high performance low level optimization.
Storing type ids everywhere would double the size small fields and quadruple tiny ones like booleans. It’s also not necessary for most persisted data as you know the columns for a table.
Smaller representation makes reading and writing faster, and it also makes more data fit in memory.
I’ve written plenty of high performance code. You can have safety and high performance at the same timetime.
Yes, that's how it works in Postgres