]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Protect PGPROC lookup when terminating background workers
authorMichael Paquier <michael@paquier.xyz>
Wed, 29 Jul 2026 08:39:37 +0000 (17:39 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 29 Jul 2026 08:39:37 +0000 (17:39 +0900)
TerminateBackgroundWorkersForDatabase() uses BackendPidGetProc() and,
until now, accessed fields of the returned PGPROC after releasing
ProcArrayLock, including its database OID.  If the PGPROC slot is
recycled during this window, the database OID being checked may belong
to a different backend, causing an unrelated background worker to be
terminated.

Triggering this bug requires a very narrow race: the background worker
identified by BackendPidGetProc() must exit, its PGPROC slot must be
released and reused, and only then must
TerminateBackgroundWorkersForDatabase() examine the database OID.

TerminateBackgroundWorkersForDatabase() holds BackgroundWorkerLock,
preventing parallel workers and dynamically registered workers (such as
those created by worker_spi) from reusing the slot.  As far as I know,
the only plausible scenario is a static background worker that exits and
is restarted quickly enough to reuse the same PGPROC slot within the
race window.  In practice, this race is extremely unlikely, still
reachable in theory.

Oversight in f1e251be80a0.

Author: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Aya Iwata <iwata.aya@fujitsu.com>
Reviewed-by: Haibo Yan <tristan.yim@gmail.com>
Discussion: https://postgr.es/m/78E81763-EA1D-4788-9741-4092BCB997A5@gmail.com
Backpatch-through: 19

src/backend/postmaster/bgworker.c

index f2cffce3ff6d243f736e50143e7f5677680903e9..3da2a90417fc67004bdd21706e04ca3d162063dd 100644 (file)
@@ -1442,16 +1442,20 @@ TerminateBackgroundWorkersForDatabase(Oid databaseId)
                if (slot->in_use &&
                        (slot->worker.bgw_flags & BGWORKER_INTERRUPTIBLE))
                {
-                       PGPROC     *proc = BackendPidGetProc(slot->pid);
+                       PGPROC     *proc;
+                       pid_t           pid = slot->pid;
 
+                       LWLockAcquire(ProcArrayLock, LW_SHARED);
+                       proc = BackendPidGetProcWithLock(pid);
                        if (proc && proc->databaseId == databaseId)
                        {
                                slot->terminate = true;
                                signal_postmaster = true;
 
                                elog(DEBUG1, "termination requested for worker (PID %d) on database %u",
-                                        (int) slot->pid, databaseId);
+                                        (int) pid, databaseId);
                        }
+                       LWLockRelease(ProcArrayLock);
                }
        }