]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
oauth: Ensure unused socket registrations are removed
authorJacob Champion <jchampion@postgresql.org>
Fri, 8 Aug 2025 15:44:46 +0000 (08:44 -0700)
committerJacob Champion <jchampion@postgresql.org>
Fri, 8 Aug 2025 15:44:46 +0000 (08:44 -0700)
If Curl needs to switch the direction of a socket's registration (e.g.
from CURL_POLL_IN to CURL_POLL_OUT), it expects the old registration to
be discarded. For epoll, this happened via EPOLL_CTL_MOD, but for
kqueue, the old registration would remain if it was not explicitly
removed by Curl.

Explicitly remove the opposite-direction event during registrations. (If
that event doesn't exist, we'll just get an ENOENT, which will be
ignored by the same code that handles CURL_POLL_REMOVE.) A few
assertions are also added to strengthen the relationship between the
number of events added, the number of events pulled off the queue, and
the lengths of the kevent arrays.

Reviewed-by: Thomas Munro <thomas.munro@gmail.com>
Backpatch-through: 18
Discussion: https://postgr.es/m/CAOYmi+nDZxJHaWj9_jRSyf8uMToCADAmOfJEggsKW-kY7aUwHA@mail.gmail.com

src/interfaces/libpq-oauth/oauth-curl.c

index 433135cb86d59d94e4e9ec7379a1403b71a3be38..97c33299a79acfde2a5990fd84c661818ae7bd8d 100644 (file)
@@ -1291,22 +1291,31 @@ register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx,
 
        return 0;
 #elif defined(HAVE_SYS_EVENT_H)
-       struct kevent ev[2] = {0};
+       struct kevent ev[2];
        struct kevent ev_out[2];
        struct timespec timeout = {0};
        int                     nev = 0;
        int                     res;
 
+       /*
+        * We don't know which of the events is currently registered, perhaps
+        * both, so we always try to remove unneeded events. This means we need to
+        * tolerate ENOENT below.
+        */
        switch (what)
        {
                case CURL_POLL_IN:
                        EV_SET(&ev[nev], socket, EVFILT_READ, EV_ADD | EV_RECEIPT, 0, 0, 0);
                        nev++;
+                       EV_SET(&ev[nev], socket, EVFILT_WRITE, EV_DELETE | EV_RECEIPT, 0, 0, 0);
+                       nev++;
                        break;
 
                case CURL_POLL_OUT:
                        EV_SET(&ev[nev], socket, EVFILT_WRITE, EV_ADD | EV_RECEIPT, 0, 0, 0);
                        nev++;
+                       EV_SET(&ev[nev], socket, EVFILT_READ, EV_DELETE | EV_RECEIPT, 0, 0, 0);
+                       nev++;
                        break;
 
                case CURL_POLL_INOUT:
@@ -1317,12 +1326,6 @@ register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx,
                        break;
 
                case CURL_POLL_REMOVE:
-
-                       /*
-                        * We don't know which of these is currently registered, perhaps
-                        * both, so we try to remove both.  This means we need to tolerate
-                        * ENOENT below.
-                        */
                        EV_SET(&ev[nev], socket, EVFILT_READ, EV_DELETE | EV_RECEIPT, 0, 0, 0);
                        nev++;
                        EV_SET(&ev[nev], socket, EVFILT_WRITE, EV_DELETE | EV_RECEIPT, 0, 0, 0);
@@ -1334,7 +1337,10 @@ register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx,
                        return -1;
        }
 
-       res = kevent(actx->mux, ev, nev, ev_out, lengthof(ev_out), &timeout);
+       Assert(nev <= lengthof(ev));
+       Assert(nev <= lengthof(ev_out));
+
+       res = kevent(actx->mux, ev, nev, ev_out, nev, &timeout);
        if (res < 0)
        {
                actx_error(actx, "could not modify kqueue: %m");