Re: Check the fd number against FD_SETSIZE in PQsocketPoll

From: Vedant Gharat <vedantgharat37(at)gmail(dot)com>
To: Roman Khapov <rkhapov(at)yandex-team(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Check the fd number against FD_SETSIZE in PQsocketPoll
Date: 2026-11-24 14:39:43
Message-ID: CAAupUtYp094PAFQpPNf3vRBDf-r4b6stiA=3YRcMwH7nZxBdSg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I reviewed v2. The FD_SETSIZE check appears to be before the FD_SET() calls
and the boundary looks right. I couldn’t test the select() fallback because
my Linux build uses poll(). I didn’t find any issues in the code I reviewed.

On Tue, 24 Nov 2026 at 16:18, Roman Khapov <rkhapov(at)yandex-team(dot)ru> wrote:

> Hi hackers!
>
> While digging into the pgbench client fd handling discussed in [0]
> (the FD_SETSIZE cap on select(2)-based platforms), I also looked
> at the client-side library itself and noticed that libpq
> has the same latent problem.
>
> PQsocketPoll() uses poll(2) when available, but its fallback path calls
> select(2) with FD_SET(sock, ...) on a socket descriptor without any check.
> If the application happens to hold a thousand or so open files before
> connecting, socket(2) can return an fd >= FD_SETSIZE, and FD_SET() then
> silently writes beyond the fd_set - i.e. stack corruption.
>
> Patch attached: fail cleanly with EINVAL instead of corrupting the stack.
> This only affects platforms without poll(2) (essentially Windows and
> ancient Unixes?), and the branch is not even compiled on modern systems...
> but I think defending here is cheap and worthwhile.
>
> [0] -
> https://www.postgresql.org/message-id/5EF180BD-3951-4E8C-903D-D91EF54A338D@yandex-team.ru
>
> --
> Best regards,
> Roman Khapov
>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Geier 2026-11-24 14:41:06 Re: Use local buffer for packed numerics
Previous Message Fujii Masao 2026-11-24 14:39:17 Re: Ensure temporary files are unlinked on errors in pg_receivewal