Skip to content

ns: support file handles

Christian Brauner cgroups linux-block linux-fsdevel linux-kselftest linux-nfs lkml netdev 2 versions – In review

Get series with b4
b4 am [email protected] change-id 20250905-work-namespace-c68826dda0d4

Versions newest first

  1. v2 Latest33 patches 37 repliesIn review
  2. v132 patches 47 repliesSuperseded

Changes in v2 from the cover letter

- Address various review comments.
- Use a common NS_GET_ID ioctl() instead of individual ioctls.
- Link to v1: https://lore.kernel.org/[email protected]

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/33pidfs: validate extensible ioctlsfs/pidfs.c include/linux/fs.h+15 -10 replies
2/33nsfs: drop tautological ioctl() checkfs/nsfs.c+1 -10 replies
3/33nsfs: validate extensible ioctlsfs/nsfs.c+3 -10 replies
4/33block: use extensible_ioctl_valid()block/blk-integrity.c+3 -54 replies
5/33ns: move to_ns_common() to ns_common.hinclude/linux/ns_common.h include/linux/nsproxy.h+20 -110 replies
6/33nsfs: add nsfs.h headerinclude/linux/nsfs.h include/linux/proc_ns.h+27 -120 replies
7/33ns: uniformly initialize ns_commoninclude/linux/proc_ns.h+16 -00 replies
8/33cgroup: use ns_common_init()kernel/cgroup/namespace.c+4 -81 reply
9/33ipc: use ns_common_init()ipc/namespace.c+1 -30 replies
10/33mnt: use ns_common_init()fs/namespace.c+6 -90 replies
11/33net: use ns_common_init()net/core/net_namespace.c+33 -134 replies
12/33pid: use ns_common_init()kernel/pid_namespace.c+1 -30 replies
13/33time: use ns_common_init()kernel/time/namespace.c+2 -50 replies
14/33user: use ns_common_init()kernel/user_namespace.c+2 -30 replies
15/33uts: use ns_common_init()kernel/utsname.c+2 -140 replies
16/33ns: remove ns_alloc_inum()include/linux/proc_ns.h+0 -60 replies
17/33nstree: make iterator genericinclude/linux/ns_common.h include/linux/nstree.h include/linux/proc_ns.h +2 more+335 -11 reply
18/33mnt: support ns lookupfs/mount.h fs/namespace.c fs/nsfs.c+35 -1209 replies
19/33cgroup: support ns lookupkernel/cgroup/cgroup.c kernel/cgroup/namespace.c+7 -21 reply
20/33ipc: support ns lookupipc/msgutil.c ipc/namespace.c ipc/shm.c+6 -01 reply
21/33net: support ns lookupinclude/net/net_namespace.h net/core/net_namespace.c+7 -23 replies
22/33pid: support ns lookupkernel/pid_namespace.c+4 -01 reply
23/33time: support ns lookupinclude/linux/time_namespace.h init/main.c kernel/time/namespace.c+17 -11 reply
24/33user: support ns lookupkernel/user_namespace.c+6 -13 replies
25/33uts: support ns lookupkernel/utsname.c+6 -11 reply
26/33ns: add to_<type>_ns() to respective headersinclude/linux/cgroup.h include/linux/ipc_namespace.h include/linux/pid_namespace.h +11 more+34 -351 reply
27/33nsfs: add current_in_namespace()include/linux/nsfs.h+15 -11 reply
28/33nsfs: support file handlesfs/nsfs.c include/linux/exportfs.h include/uapi/linux/nsfs.h+173 -02 replies
29/33nsfs: support exhaustive file handlesfs/fhandle.c fs/internal.h fs/nsfs.c +1 more+18 -01 reply
30/33nsfs: add missing id retrieval supportfs/nsfs.c include/uapi/linux/nsfs.h+17 -141 reply
31/33tools: update nsfs.h uapi headertools/include/uapi/linux/nsfs.h+15 -20 replies
32/33selftests/namespaces: add identifier selfteststools/testing/selftests/namespaces/.gitignore tools/testing/selftests/namespaces/Makefile tools/testing/selftests/namespaces/config +1 more+1001 -00 replies
33/33selftests/namespaces: add file handle selfteststools/testing/selftests/namespaces/.gitignore tools/testing/selftests/namespaces/Makefile tools/testing/selftests/namespaces/file_handle_test.c+1431 -10 replies

Reviews trailers per patch

30 of 33 patches have a Reviewed-by or Acked-by.

PatchAleksa SaraiJan KaraJens AxboeThomas GleixnerTejun HeoAmir Goldstein
1/33R-b R-b
2/33
3/33R-b
4/33R-b R-b
5/33R-b
6/33R-b
7/33R-b
8/33R-b A-b
9/33R-b
10/33R-b
11/33R-b
12/33R-b
13/33R-b R-b
14/33R-b
15/33R-b
16/33R-b
17/33R-b
18/33R-b
19/33R-b A-b
20/33R-b
21/33R-b
22/33R-b
23/33R-b R-b
24/33R-b
25/33R-b
26/33R-b R-b
27/33R-b R-b
28/33R-b R-b
29/33R-b R-b
30/33R-b
31/33
32/33
33/33R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 6 reviewers.

Read the v2 thread

Cover letter [PATCH v2 00/33] ns: support file handles

For a while now we have supported file handles for pidfds. This has proven to be very useful.

Extend the concept to cover namespaces as well. After this patchset it is possible to encode and decode namespace file handles using the commong name_to_handle_at() and open_by_handle_at() apis.

Namespaces file descriptors can already be derived from pidfds which means they aren't subject to overmount protection bugs. IOW, it's irrelevant if the caller would not have access to an appropriate /proc/<pid>/ns/ directory as they could always just derive the namespace based on a pidfd already.

It has the same advantage as pidfds. It's possible to reliably and for the lifetime of the system refer to a namespace without pinning any resources and to compare them.

Permission checking is kept simple. If the caller is located in the namespace the file handle refers to they are able to open it otherwise they must hold privilege over the owning namespace of the relevant namespace.

Both the network namespace and the mount namespace already have an associated cookie that isn't recycled and is fully exposed to userspace. Move this into ns_common and use the same id space for all namespaces so they can trivially and reliably be compared.

There's more coming based on the iterator infrastructure but the series is large enough and focuses on file handles.

Extensive selftests included.

---
Changes in v2:
- Address various review comments.
- Use a common NS_GET_ID ioctl() instead of individual ioctls.
- Link to v1: https://lore.kernel.org/[email protected]

---
Christian Brauner (33):
pidfs: validate extensible ioctls
nsfs: drop tautological ioctl() check
nsfs: validate extensible ioctls
block: use extensible_ioctl_valid()
ns: move to_ns_common() to ns_common.h
nsfs: add nsfs.h header
ns: uniformly initialize ns_common
cgroup: use ns_common_init()
ipc: use ns_common_init()
mnt: use ns_common_init()
net: use ns_common_init()
pid: use ns_common_init()
time: use ns_common_init()
user: use ns_common_init()
uts: use ns_common_init()
ns: remove ns_alloc_inum()
nstree: make iterator generic
mnt: support ns lookup
cgroup: support ns lookup
ipc: support ns lookup
net: support ns lookup
pid: support ns lookup
time: support ns lookup
user: support ns lookup
uts: support ns lookup
ns: add to_<type>_ns() to respective headers
nsfs: add current_in_namespace()
nsfs: support file handles
nsfs: support exhaustive file handles
nsfs: add missing id retrieval support
tools: update nsfs.h uapi header selftests/namespaces: add identifier selftests selftests/namespaces: add file handle selftests

 block/blk-integrity.c                              |    8 +-
 fs/fhandle.c                                       |    6 +
 fs/internal.h                                      |    1 +
 fs/mount.h                                         |   10 +-
 fs/namespace.c                                     |  156 +--
 fs/nsfs.c                                          |  201 ++-
 fs/pidfs.c                                         |    2 +-
 include/linux/cgroup.h                             |    5 +
 include/linux/exportfs.h                           |    6 +
 include/linux/fs.h                                 |   14 +
 include/linux/ipc_namespace.h                      |    5 +
 include/linux/ns_common.h                          |   29 +
 include/linux/nsfs.h                               |   40 +
 include/linux/nsproxy.h                            |   11 -
 include/linux/nstree.h                             |   89 ++
 include/linux/pid_namespace.h                      |    5 +
 include/linux/proc_ns.h                            |   32 +-
 include/linux/time_namespace.h                     |    9 +
 include/linux/user_namespace.h                     |    5 +
 include/linux/utsname.h                            |    5 +
 include/net/net_namespace.h                        |    6 +
 include/uapi/linux/fcntl.h                         |    1 +
 include/uapi/linux/nsfs.h                          |   15 +-
 init/main.c                                        |    2 +
 ipc/msgutil.c                                      |    1 +
 ipc/namespace.c                                    |   12 +-
 ipc/shm.c                                          |    2 +
 kernel/Makefile                                    |    2 +-
 kernel/cgroup/cgroup.c                             |    2 +
 kernel/cgroup/namespace.c                          |   24 +-
 kernel/nstree.c                                    |  233 ++++
 kernel/pid_namespace.c                             |   13 +-
 kernel/time/namespace.c                            |   23 +-
 kernel/user_namespace.c                            |   17 +-
 kernel/utsname.c                                   |   28 +-
 net/core/net_namespace.c                           |   59 +-
 tools/include/uapi/linux/nsfs.h                    |   17 +-
 tools/testing/selftests/namespaces/.gitignore      |    2 +
 tools/testing/selftests/namespaces/Makefile        |    7 +
 tools/testing/selftests/namespaces/config          |    7 +
 .../selftests/namespaces/file_handle_test.c        | 1429 ++++++++++++++++++++
 tools/testing/selftests/namespaces/nsid_test.c     |  986 ++++++++++++++
 42 files changed, 3257 insertions(+), 270 deletions(-)

---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250905-work-namespace-c68826dda0d4