Hi,

postgres_fdw trusts the shape of a couple of remote results a bit too
much. get_remote_estimate() reads the first line of the remote EXPLAIN
output without checking that there is one, and IMPORT FOREIGN SCHEMA
reads eight columns from its catalog query without checking how many
came back. When something's missing, PQgetvalue() returns NULL and the
backend can segfault on it.

It's basically the same bug that was recently fixed in libpq and
pg_basebackup [1], except that here it's a backend that crashes, so the
whole cluster gets restarted.

A real PostgreSQL server won't send anything like that, so you'd need a
broken or malicious remote to hit this (I used a small fake server).
Still, any non-superuser with USAGE on postgres_fdw can point a server
wherever they like, so it seemed worth fixing to me.

The attached patch checks the counts before reading anything and errors
out if they're off, like the other "unexpected result" checks in that
file. An import that returns no rows still works as before.

Thoughts?

Regards,
Ayush

[1] https://postgr.es/m/[email protected]

Attachment: v1-0001-postgres_fdw-Validate-remote-query-result-shapes.patch
Description: Binary data

Reply via email to