Re: postgres_fdw can crash on malformed remote results

From: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
To: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>
Subject: Re: postgres_fdw can crash on malformed remote results
Date: 2026-12-01 19:10:01
Message-ID: CADkLM=dZxn-RhS2RkT0u3OmPkweKwUb_-qNZXvkdszEsZ7PSkg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Nov 26, 2026 at 11:26 PM Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
wrote:

> 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.
>

This got me curious if dblink had similar issues. It does not appear to,
though it does appear to silently ignore result sets with 0 columns.

> 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).
>

Looking over this problem, I'm left with the impression that the fake
server is valuable on its own. Can you tell us more about it?

> Still, any non-superuser with USAGE on postgres_fdw can point a server
> wherever they like, so it seemed worth fixing to me.
>

+1

>
> 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.
>

It's a trivial and lightweight fix, with cited precedent for making the
same change, applies clean and passes tests.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2026-12-01 19:14:36 Re: Should we skip foreign key checks in binary upgrade mode?
Previous Message Masahiko Sawada 2026-12-01 18:47:30 Re: Fix LogStandbySnapshot() race during logical decoding activation