]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix O_CLOEXEC flag handling in Windows port.
authorThomas Munro <tmunro@postgresql.org>
Tue, 9 Dec 2025 20:01:35 +0000 (09:01 +1300)
committerThomas Munro <tmunro@postgresql.org>
Tue, 9 Dec 2025 20:05:21 +0000 (09:05 +1300)
commitbebb281b08b624d69fbb4a6fb94b2c1b5d0be7a5
tree578eb8ef1724c90f013432b7ea663ab9511d6d61
parent1412c8ea0740ffe97c154cd63760a214e26c94a8
Fix O_CLOEXEC flag handling in Windows port.

PostgreSQL's src/port/open.c has always set bInheritHandle = TRUE
when opening files on Windows, making all file descriptors inheritable
by child processes.  This meant the O_CLOEXEC flag, added to many call
sites by commit 1da569ca1f (v16), was silently ignored.

The original commit included a comment suggesting that our open()
replacement doesn't create inheritable handles, but it was a mis-
understanding of the code path.  In practice, the code was creating
inheritable handles in all cases.

This hasn't caused widespread problems because most child processes
(archive_command, COPY PROGRAM, etc.) operate on file paths passed as
arguments rather than inherited file descriptors.  Even if a child
wanted to use an inherited handle, it would need to learn the numeric
handle value, which isn't passed through our IPC mechanisms.

Nonetheless, the current behavior is wrong.  It violates documented
O_CLOEXEC semantics, contradicts our own code comments, and makes
PostgreSQL behave differently on Windows than on Unix.  It also creates
potential issues with future code or security auditing tools.

To fix, define O_CLOEXEC to _O_NOINHERIT in master, previously used by
O_DSYNC.  We use different values in the back branches to preserve
existing values.  In pgwin32_open_handle() we set bInheritHandle
according to whether O_CLOEXEC is specified, for the same atomic
semantics as POSIX in multi-threaded programs that create processes.

Backpatch-through: 16
Author: Bryan Green <dbryan.green@gmail.com>
Co-authored-by: Thomas Munro <thomas.munro@gmail.com> (minor adjustments)
Discussion: https://postgr.es/m/e2b16375-7430-4053-bda3-5d2194ff1880%40gmail.com
src/include/port.h
src/include/port/win32_port.h
src/port/open.c
src/test/modules/Makefile
src/test/modules/meson.build
src/test/modules/test_cloexec/Makefile [new file with mode: 0644]
src/test/modules/test_cloexec/meson.build [new file with mode: 0644]
src/test/modules/test_cloexec/t/001_cloexec.pl [new file with mode: 0644]
src/test/modules/test_cloexec/test_cloexec.c [new file with mode: 0644]