Check the fd number against FD_SETSIZE in PQsocketPoll

Поиск
Список
Период
Сортировка
Искать

Check the fd number against FD_SETSIZE in PQsocketPoll

От:
Roman Khapov <rkhapov@yandex-team.ru>
Дата:
Вложения:
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

Re: Check the fd number against FD_SETSIZE in PQsocketPoll

От:
Roman Khapov <rkhapov@yandex-team.ru>
Дата:
Вложения:
> 
> 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 

Well, testing on Windows showed that assumption was incorrect, and the
check is not needed on Windows. So now patch fixes only
the Unix without poll(2), what sounds mostly theoretical... 

Anyway, attached the v2.

--
Best regards,
Roman Khapov

Re: Check the fd number against FD_SETSIZE in PQsocketPoll

От:
Abraham Pérez <abraham1798@gmail.com>
Дата:
Hi Roman,

Thanks for the patch. I took a look at v2.

What I checked
--------------
- Applies cleanly on current master (git am, no fuzz/offset) at
  207b5293b8d.
- Builds with --enable-debug --enable-cassert --enable-tap-tests; no new
  compiler warnings.
- "make check" in src/test/regress is green (All 243 tests passed). As
  expected this exercises nothing of the change (see below), but confirms
  the tree is otherwise unaffected.

The concern: as written, the new code is never compiled
--------------------------------------------------------
The guard is added inside the "#else /* !HAVE_POLL */" arm of
PQsocketPoll() -- i.e. the select(2) fallback -- and is itself wrapped in
"#ifndef WIN32":

    #ifndef WIN32
        if (sock >= FD_SETSIZE)
        {
            SOCK_ERRNO_SET(EINVAL);
            return -1;
        }
    #endif

But src/include/port.h unconditionally does, for every non-Windows build:

    #ifndef WIN32
    ...
    #define HAVE_POLL 1
    ...
    #endif

(that line is a plain #define, not guarded by #ifndef HAVE_POLL). So:

  * On any non-Windows platform HAVE_POLL is always defined, hence the
    "#else /* !HAVE_POLL */" select() arm -- where the new check lives --
    is never compiled. I confirmed this by preprocessing fe-misc.c: the
    FD_SETSIZE check does not appear in the output, and even forcing
    -UHAVE_POLL on the command line does not help, because port.h redefines
    HAVE_POLL back to 1.

  * On Windows the select() arm *is* compiled (HAVE_POLL is not defined
    there), but the new block is excluded by the surrounding "#ifndef
    WIN32" -- which is also the behavior you intended per your v2 note that
    the check isn't needed on Windows.

So the net effect of v2 on every supported platform is a no-op: there is
no build configuration in which the new "sock >= FD_SETSIZE" test is
reached. The block itself is fine in isolation (I compiled it standalone
with -Werror; FD_SETSIZE, SOCK_ERRNO_SET and EINVAL are all in scope, and
the shape matches the existing precedent in fe-secure-gssapi.c), but it
can't protect anything as placed.

Because PostgreSQL assumes poll(2) on all Unix (that is exactly what the
port.h block encodes), the select() fallback in PQsocketPoll() is
effectively Windows-only today, and Windows is the one platform you
established does not need the guard. That seems to leave the original
scenario (a Unix without poll(2) handing out an fd >= FD_SETSIZE) without
a live code path to hit in the first place -- which may be why this reads
as "mostly theoretical".

Questions / suggestions
-----------------------
- Is there a real, currently-supported platform where PQsocketPoll() takes
  the select() path *and* lacks poll(2)? If not, it's worth asking on-list
  whether the hardening is still wanted, or whether the right fix is
  elsewhere (e.g. the pgbench/client-fd thread you referenced in [0]).
- If it is still wanted, the check needs to sit where it actually compiles
  and runs. As-is it will not.

Given the above, my tentative verdict is that v2 does not yet do what it
intends; I'd suggest clarifying the target platform before another spin.
Happy to re-test once that's settled.

Best regards,
Abraham


FAQ