linux/include/uapi
Christian Brauner 06074fbbf0
file: add CLOSE_RANGE_CLOEXEC_ONLY
CLOSE_RANGE_CLOEXEC marks a range close-on-exec. Nothing happens to
these fds unless an exec happens. Add CLOSE_RANGE_CLOEXEC_ONLY which
closes the close-on-exec file descriptors in the range.

When combined with CLOSE_RANGE_EXCEPT it names the close-on-exec file
descriptors that are supposed to survive. Every close-on-exec fd outside
of the range is closed. File descriptors without that flag are left
alone. Whatever the caller deliberately passes down, stdio, LISTEN_FDS,
an inherited pipe, remains where it is.

The handful of close-on-exec descriptors the caller still needs for the
exec such as the executable, an error pipe, sit in a specific range.
That is what a task between clone(CLONE_FILES) and execve() actually
wants:

	clone(CLONE_FILES | CLONE_VM | CLONE_VFORK)
	child: close_range(lo, hi, CLOSE_RANGE_UNSHARE |
				   CLOSE_RANGE_CLOEXEC_ONLY |
				   CLOSE_RANGE_EXCEPT)
	child: rearrange descriptors in the now private table
	child: execve()

Between the clone and the close_range() the child holds no reference of
its own on any file because copy_files() only bumps the fdtable
refcount. So a close() in the parent takes effect immediately. The
unshare also never takes a reference on the fds it leaves behind either.

The range is expressed in the parent's numbering. So a caller that
cannot name the file descriptors it keeps contiguously picks a range
wide enough to cover them, unshares, and tidies up with a second
close_range() on the table it now owns alone. That one is cheap. To keep
nothing, name a range that cannot hold an open descriptor, e.g.
close_range(~0U, ~0U, ...).

CLOSE_RANGE_CLOEXEC and CLOSE_RANGE_CLOEXEC_ONLY are mutually exclusive.

Link: https://patch.msgid.link/20260921-work-file-close_range_except-v1-9-c20d0b49270d@kernel.org
Reviewed-by: Jann Horn <jannh@google.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
2026-10-05 13:02:41 +02:00
..
asm-generic Networking changes for 7.3. 2026-08-20 08:16:04 -07:00
cxl
drm drm/amdgpu/userq: fix struct drm_amdgpu_info_device padding for 32bit compile 2026-09-02 17:02:23 -04:00
fwctl fwctl/bnxt: Add DMA buffer support for HWRM commands 2026-08-07 14:18:13 -03:00
linux file: add CLOSE_RANGE_CLOEXEC_ONLY 2026-10-05 13:02:41 +02:00
misc misc: fastrpc: Add polling mode support for fastRPC driver 2026-07-31 14:15:53 +02:00
mtd UBI: support per-device wear-leveling threshold 2026-08-12 00:08:54 +02:00
rdma RDMA/mana_ib: UC QP support for UAPI 2026-08-05 10:44:30 -04:00
regulator regulator: uapi: Use UAPI integer type 2025-12-22 09:00:42 +00:00
scsi scsi: fc: Fix typo in fc_els.h 2026-03-27 16:35:04 -04:00
sound ASoC: Updates for v7.3 2026-08-18 08:35:39 +02:00
video
xen
Kbuild