Skip to content

files,close_range: add CLOSE_RANGE_{CLOEXEC_ONLY,EXCEPT}

Christian Brauner linux-fsdevel 1 version In review

Get series with b4
b4 am 20260921-work-file-close_range_except-v1-0-c20d0b49270d@kernel.org change-id 20260918-work-file-close_range_except-e100cfb38561

Versions newest first

  1. v1 Latest10 patches 3 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/10file: let dup_fd() leave the punched hole behindfs/file.c+46 -140 replies
2/10selftests/core: test the hole CLOSE_RANGE_UNSHARE leaves behindtools/testing/selftests/core/close_range_test.c+44 -00 replies
3/10file: rename dup_fd()'s punch_hole to rangefs/file.c+14 -140 replies
4/10file: let dup_fd() drop everything outside of the rangefs/file.c include/linux/fdtable.h+16 -50 replies
5/10close_range: turn the flags into an enuminclude/uapi/linux/close_range.h+17 -40 replies
6/10file: add CLOSE_RANGE_EXCEPTfs/file.c include/uapi/linux/close_range.h+64 -170 replies
7/10selftests/core: test CLOSE_RANGE_EXCEPTtools/testing/selftests/core/close_range_test.c+426 -00 replies
8/10file: let dup_fd() drop only close-on-exec descriptorsfs/file.c include/linux/fdtable.h+37 -92 replies
9/10file: add CLOSE_RANGE_CLOEXEC_ONLYfs/file.c include/uapi/linux/close_range.h+32 -30 replies
10/10selftests/core: test CLOSE_RANGE_CLOEXEC_ONLYtools/testing/selftests/core/close_range_test.c+481 -00 replies

Reviews trailers per patch

None of the 10 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchJann Horn
SeriesR-b
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 00/10] files,close_range: add CLOSE_RANGE_{CLOEXEC_ONLY,EXCEPT}

close_range(CLOSE_RANGE_CLOEXEC) marks a range of file descriptors close-on-exec. It only marks them. The file descriptors are only ever closed during an exec. This series adds two new flags to extend the abilities of close_range():

(1) CLOSE_RANGE_EXCEPT

While regular close_range() operates on all file descriptors in the range [fd, max_fd] and not on any others, CLOSE_RANGE_EXCEPT makes close_range() operate on all file descriptors outside of the range.

When combined with CLOSE_RANGE_UNSHARE dup_fd() never takes a reference on what is dropped.

A task between clone(CLONE_FILES | CLONE_VM | CLONE_VFORK) and execve() that has the descriptors for the child in one window can shed the rest of the shared table in one call:

close_range(lo, hi, CLOSE_RANGE_UNSHARE | CLOSE_RANGE_EXCEPT)

Nothing outside of the window is referenced by the child at any point.

(2) CLOSE_RANGE_CLOEXEC_ONLY closes the close-on-exec file descriptors in the range

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, can sit in a specific range. A task between clone(CLONE_FILES) and execve() may do:

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.

---
Christian Brauner (10):
file: let dup_fd() leave the punched hole behind selftests/core: test the hole CLOSE_RANGE_UNSHARE leaves behind
file: rename dup_fd()'s punch_hole to range
file: let dup_fd() drop everything outside of the range
close_range: turn the flags into an enum
file: add CLOSE_RANGE_EXCEPT
selftests/core: test CLOSE_RANGE_EXCEPT
file: let dup_fd() drop only close-on-exec descriptors
file: add CLOSE_RANGE_CLOEXEC_ONLY
selftests/core: test CLOSE_RANGE_CLOEXEC_ONLY

 fs/file.c                                       | 200 ++++-
 include/linux/fdtable.h                         |   9 +
 include/uapi/linux/close_range.h                |  31 +-
 tools/testing/selftests/core/close_range_test.c | 951 ++++++++++++++++++++++++
 4 files changed, 1151 insertions(+), 40 deletions(-)

---
base-commit: 5dd1818b15d98d4a20806cd00b1b40320b06004f
change-id: 20260918-work-file-close_range_except-e100cfb38561