Skip to contentSkip to conversation

pidfs: support bind-mounts

RFC PATCH New Christian Brauner 1 participant 3 messages Dec 19, 2024

Subsystems (3)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover pidfs: support bind-mounts

Allow bind-mounting pidfds. Similar to nsfs let's allow bind-mounts for pidfds. This allows pidfds to be safely recovered and checked for process recycling.

Instead of checking d_ops for both nsfs and pidfs we could in a follow-up patch add a flag argument to struct dentry_operations that functions similar to file_operations->fop_flags.

---
Christian Brauner (2):
pidfs: allow bind-mounts
selftests: add pidfd bind-mount tests

 fs/namespace.c                                   |  10 +-
 fs/pidfs.c                                       |   2 +-
 include/linux/pidfs.h                            |   1 +
 tools/testing/selftests/pidfd/.gitignore         |   1 +
 tools/testing/selftests/pidfd/Makefile           |   2 +-
 tools/testing/selftests/pidfd/pidfd_bind_mount.c | 188 +++++++++++++++++++++++
 6 files changed, 200 insertions(+), 4 deletions(-)

---
base-commit: 16ecd47cb0cd895c7c2f5dd5db50f6c005c51639
change-id: 20241219-work-pidfs-mount-930f9899a1cd

author maintainer
#1 lore raw

RFC PATCH 1/2 pidfs: allow bind-mounts

Allow bind-mounting pidfds. Similar to nsfs let's allow bind-mounts for pidfds. This allows pidfds to be safely recovered and checked for process recycling.

---
 fs/namespace.c        | 10 ++++++++--
 fs/pidfs.c            |  2 +-
 include/linux/pidfs.h |  1 +
 3 files changed, 10 insertions(+), 3 deletions(-)
fs/namespace.c +8 -2
index 23e81c2a1e3fee7d97df2a84a69438a677933654..7baffa2ea582dacd0fa70959174fc2a47fb5de1f 100644
@@ -32,6 +32,7 @@
 #include <linux/fs_context.h>
 #include <linux/shmem_fs.h>
 #include <linux/mnt_idmapping.h>
+#include <linux/pidfs.h>
 #include <linux/nospec.h>
 
 #include "pnode.h"
@@ -2732,8 +2733,13 @@ static struct mount *__do_loopback(struct path *old_path, int recurse)
 	if (IS_MNT_UNBINDABLE(old))
 		return mnt;
 
-	if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
-		return mnt;
+	if (!check_mnt(old)) {
+		const struct dentry_operations *d_op = old_path->dentry->d_op;
+
+		if (d_op != &ns_dentry_operations &&
+		    d_op != &pidfs_dentry_operations)
+			return mnt;
+	}
 
 	if (!recurse && has_locked_children(old, old_path->dentry))
 		return mnt;
fs/pidfs.c +1 -1
index c5a51c69acc86694152ff006743bbfa516a2c1f5..049352f973de38967ad35c6440b9480e7e2e2775 100644
@@ -510,7 +510,7 @@ static char *pidfs_dname(struct dentry *dentry, char *buffer, int buflen)
 	return dynamic_dname(buffer, buflen, "anon_inode:[pidfd]");
 }
 
-static const struct dentry_operations pidfs_dentry_operations = {
+const struct dentry_operations pidfs_dentry_operations = {
 	.d_delete	= always_delete_dentry,
 	.d_dname	= pidfs_dname,
 	.d_prune	= stashed_dentry_prune,
include/linux/pidfs.h +1 -0
index df574d6708d423e2eb2f5154b4a0ead1765d5ad2..7c830d0dec9a7c098f65f98a9b9f605e9108da96 100644
@@ -6,5 +6,6 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags);
 void __init pidfs_init(void);
 void pidfs_add_pid(struct pid *pid);
 void pidfs_remove_pid(struct pid *pid);
+extern const struct dentry_operations pidfs_dentry_operations;
 
 #endif /* _LINUX_PID_FS_H */
Signature · 1 line
2.45.2
author maintainer
#2 lore raw

RFC PATCH 2/2 selftests: add pidfd bind-mount tests

In reply to Christian Brauner
---
 tools/testing/selftests/pidfd/.gitignore         |   1 +
 tools/testing/selftests/pidfd/Makefile           |   2 +-
 tools/testing/selftests/pidfd/pidfd_bind_mount.c | 188 +++++++++++++++++++++++
 3 files changed, 190 insertions(+), 1 deletion(-)
tools/testing/selftests/pidfd/.gitignore +1 -0
index 224260e1a4a24a235309a8fdd2186f44845773ed..bf92481f925c32ad4b2e49a9183711ec235bdadc 100644
@@ -7,3 +7,4 @@ pidfd_fdinfo_test
 pidfd_getfd_test
 pidfd_setns_test
 pidfd_file_handle_test
+pidfd_bind_mount
tools/testing/selftests/pidfd/Makefile +1 -1
index 3c16d8e776843f10d09f553ce6915317522d01dc..301343a11b62ecdf25d9f8ffeba690b84dc5c378 100644
@@ -3,7 +3,7 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
 
 TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
 	pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
-	pidfd_file_handle_test
+	pidfd_file_handle_test pidfd_bind_mount
 
 include ../lib.mk
 
tools/testing/selftests/pidfd/pidfd_bind_mount.c new file +188 -0

188 lines not on this page · raw message

Signature · 1 line
2.45.2