nsfs: iterate through mount namespaces
Christian Brauner
maintainercover nsfs: iterate through mount namespaces
Hey,
Recently, we added the ability to list mounts in other mount namespaces and the ability to retrieve namespace file descriptors without having to go through procfs by deriving them from pidfds.
This extends nsfs in two ways:
(1) Add the ability to retrieve information about a mount namespace via NS_MNT_GET_INFO. This will return the mount namespace id and the number of mounts currently in the mount namespace. The number of mounts can be used to size the buffer that needs to be used for listmount() and is in general useful without having to actually iterate through all the mounts.
The structure is extensible.
(2) Add the ability to iterate through all mount namespaces over which the caller holds privilege returning the file descriptor for the next or previous mount namespace.
To retrieve a mount namespace the caller must be privileged wrt to it's owning user namespace. This means that PID 1 on the host can list all mounts in all mount namespaces or that a container can list all mounts of its nested containers.
Optionally pass a structure for NS_MNT_GET_INFO with NS_MNT_GET_{PREV,NEXT} to retrieve information about the mount namespace in one go.
(1) and (2) can be implemented for other namespace types easily.
Together with recent api additions this means one can iterate through all mounts in all mount namespaces without ever touching procfs. Here's a sample program list_all_mounts_everywhere.c:
// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <asm/unistd.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <linux/stat.h>
#include <sched.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/pidfd.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#define die_errno(format, ...) \
do { \
fprintf(stderr, "%m | %s: %d: %s: " format "\n", __FILE__, \
__LINE__, __func__, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0) /* Get the id for a mount namespace */
#define NS_GET_MNTNS_ID _IO(0xb7, 0x5)
/* Get next mount namespace. */
struct mnt_ns_info {
__u32 size;
__u32 nr_mounts;
__u64 mnt_ns_id;
};
#define MNT_NS_INFO_SIZE_VER0 16 /* size of first published struct */
/* Get information about namespace. */
#define NS_MNT_GET_INFO _IOR(0xb7, 10, struct mnt_ns_info)
/* Get next namespace. */
#define NS_MNT_GET_NEXT _IOR(0xb7, 11, struct mnt_ns_info)
/* Get previous namespace. */
#define NS_MNT_GET_PREV _IOR(0xb7, 12, struct mnt_ns_info)
#define PIDFD_GET_MNT_NAMESPACE _IO(0xFF, 3)
#define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */
#define __NR_listmount 458
#define __NR_statmount 457
/*
* @mask bits for statmount(2)
*/
#define STATMOUNT_SB_BASIC 0x00000001U /* Want/got sb_... */
#define STATMOUNT_MNT_BASIC 0x00000002U /* Want/got mnt_... */
#define STATMOUNT_PROPAGATE_FROM 0x00000004U /* Want/got propagate_from */
#define STATMOUNT_MNT_ROOT 0x00000008U /* Want/got mnt_root */
#define STATMOUNT_MNT_POINT 0x00000010U /* Want/got mnt_point */
#define STATMOUNT_FS_TYPE 0x00000020U /* Want/got fs_type */
#define STATMOUNT_MNT_NS_ID 0x00000040U /* Want/got mnt_ns_id */
#define STATMOUNT_MNT_OPTS 0x00000080U /* Want/got mnt_opts */ struct statmount {
__u32 size; /* Total size, including strings */
__u32 mnt_opts;
__u64 mask; /* What results were written */
__u32 sb_dev_major; /* Device ID */
__u32 sb_dev_minor;
__u64 sb_magic; /* ..._SUPER_MAGIC */
__u32 sb_flags; /* SB_{RDONLY,SYNCHRONOUS,DIRSYNC,LAZYTIME} */
__u32 fs_type; /* [str] Filesystem type */
__u64 mnt_id; /* Unique ID of mount */
__u64 mnt_parent_id; /* Unique ID of parent (for root == mnt_id) */
__u32 mnt_id_old; /* Reused IDs used in proc/.../mountinfo */
__u32 mnt_parent_id_old;
__u64 mnt_attr; /* MOUNT_ATTR_... */
__u64 mnt_propagation; /* MS_{SHARED,SLAVE,PRIVATE,UNBINDABLE} */
__u64 mnt_peer_group; /* ID of shared peer group */
__u64 mnt_master; /* Mount receives propagation from this ID */
__u64 propagate_from; /* Propagation from in current namespace */
__u32 mnt_root; /* [str] Root of mount relative to root of fs */
__u32 mnt_point; /* [str] Mountpoint relative to current root */
__u64 mnt_ns_id;
__u64 __spare2[49];
char str[]; /* Variable size part containing strings */
};
struct mnt_id_req {
__u32 size;
__u32 spare;
__u64 mnt_id;
__u64 param;
__u64 mnt_ns_id;
};
#define MNT_ID_REQ_SIZE_VER1 32 /* sizeof second published struct */
#define LSMT_ROOT 0xffffffffffffffff /* root mount */
static int __statmount(__u64 mnt_id, __u64 mnt_ns_id, __u64 mask,
struct statmount *stmnt, size_t bufsize, unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER1,
.mnt_id = mnt_id,
.param = mask,
.mnt_ns_id = mnt_ns_id,
};
return syscall(__NR_statmount, &req, stmnt, bufsize, flags);
}
static struct statmount *sys_statmount(__u64 mnt_id, __u64 mnt_ns_id,
__u64 mask, unsigned int flags)
{
size_t bufsize = 1 << 15;
struct statmount *stmnt = NULL, *tmp = NULL;
int ret;
for (;;) {
tmp = realloc(stmnt, bufsize);
if (!tmp)
goto out; stmnt = tmp;
ret = __statmount(mnt_id, mnt_ns_id, mask, stmnt, bufsize, flags); if (!ret)
return stmnt;
if (errno != EOVERFLOW)
goto out;
bufsize <<= 1;
if (bufsize >= UINT_MAX / 2)
goto out;
}
out:
free(stmnt);
printf("statmount failed");
return NULL;
}
static ssize_t sys_listmount(__u64 mnt_id, __u64 last_mnt_id, __u64 mnt_ns_id,
__u64 list[], size_t num, unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER1,
.mnt_id = mnt_id,
.param = last_mnt_id,
.mnt_ns_id = mnt_ns_id,
};
return syscall(__NR_listmount, &req, list, num, flags);
}
int main(int argc, char *argv[])
{
#define LISTMNT_BUFFER 10
__u64 list[LISTMNT_BUFFER], last_mnt_id = 0;
int ret, pidfd, fd_mntns;
struct mnt_ns_info info = {};
pidfd = pidfd_open(getpid(), 0);
if (pidfd < 0)
die_errno("pidfd_open failed");
fd_mntns = ioctl(pidfd, PIDFD_GET_MNT_NAMESPACE, 0);
if (fd_mntns < 0)
die_errno("ioctl(PIDFD_GET_MNT_NAMESPACE) failed");
ret = ioctl(fd_mntns, NS_MNT_GET_INFO, &info);
if (ret < 0)
die_errno("ioctl(NS_GET_MNTNS_ID) failed");
printf("Listing %u mounts for mount namespace %d:%llu\n", info.nr_mounts, fd_mntns, info.mnt_ns_id);
for (;;) {
ssize_t nr_mounts;
next:
nr_mounts = sys_listmount(LSMT_ROOT, last_mnt_id, info.mnt_ns_id, list, LISTMNT_BUFFER, 0);
if (nr_mounts <= 0) {
printf("Finished listing mounts for mount namespace %d:%llu\n\n", fd_mntns, info.mnt_ns_id);
ret = ioctl(fd_mntns, NS_MNT_GET_NEXT, 0);
if (ret < 0)
die_errno("ioctl(NS_MNT_GET_NEXT) failed");
close(ret);
ret = ioctl(fd_mntns, NS_MNT_GET_NEXT, &info);
if (ret < 0) {
if (errno == ENOENT) {
printf("Finished listing all mount namespaces\n");
exit(0);
}
die_errno("ioctl(NS_MNT_GET_NEXT) failed");
}
close(fd_mntns);
fd_mntns = ret;
last_mnt_id = 0;
printf("Listing %u mounts for mount namespace %d:%llu\n", info.nr_mounts, fd_mntns, info.mnt_ns_id);
goto next;
}
for (size_t cur = 0; cur < nr_mounts; cur++) {
struct statmount *stmnt;
last_mnt_id = list[cur];
stmnt = sys_statmount(last_mnt_id, info.mnt_ns_id,
STATMOUNT_SB_BASIC |
STATMOUNT_MNT_BASIC |
STATMOUNT_MNT_ROOT |
STATMOUNT_MNT_POINT |
STATMOUNT_MNT_NS_ID |
STATMOUNT_MNT_OPTS |
STATMOUNT_FS_TYPE,
0);
if (!stmnt) {
printf("Failed to statmount(%llu) in mount namespace(%llu)\n", last_mnt_id, info.mnt_ns_id);
continue;
}
printf("mnt_id(%u/%llu) | mnt_parent_id(%u/%llu): %s @ %s ==> %s with options: %s\n",
stmnt->mnt_id_old, stmnt->mnt_id,
stmnt->mnt_parent_id_old, stmnt->mnt_parent_id,
stmnt->str + stmnt->fs_type,
stmnt->str + stmnt->mnt_root,
stmnt->str + stmnt->mnt_point,
stmnt->str + stmnt->mnt_opts);
free(stmnt);
}
}
exit(0);
}Thanks!
Christian
---
---
base-commit: 720261cfc7329406a50c2a8536e0039b9dd9a4e5
change-id: 20240705-work-mount-namespace-126b73a11f5c
Christian Brauner
maintainerRFC PATCH 1/5 fs: use all available ids
The counter is unconditionally incremented for each mount allocation. If we set it to 1ULL << 32 we're losing 4294967296 as the first valid non-32 bit mount id.
--- fs/namespace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -1
@@ -70,7 +70,7 @@ static DEFINE_IDA(mnt_id_ida);static DEFINE_IDA (mnt_group_ida);/* Don't allow confusion with old 32bit mount ID */ -#define MNT_UNIQUE_ID_OFFSET (1ULL << 32) +#define MNT_UNIQUE_ID_OFFSET (1ULL << 31)static atomic64_t mnt_id_ctr =ATOMIC64_INIT (MNT_UNIQUE_ID_OFFSET );static struct hlist_head *mount_hashtable__ro_after_init ;
Signature · 1 line
2.43.0
Christian Brauner
maintainerRFC PATCH 2/5 fs: allow mount namespace fd
In reply to Christian BraunerWe already allow a mount namespace id, enable mount namespace file descriptors as well.
--- fs/namespace.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-)
fs/namespace.c +32 -7
@@ -5243,12 +5243,37 @@ static int copy_mnt_id_req(const struct mnt_id_req __user *req,* that, or if not simply grab a passive reference on our mount namespace and * return that. */ -static struct mnt_namespace *grab_requested_mnt_ns (u64 mnt_ns_id) +static struct mnt_namespace *grab_requested_mnt_ns (const struct mnt_id_req *kreq) { -if (mnt_ns_id) -return lookup_mnt_ns (mnt_ns_id); -refcount_inc (¤t->nsproxy->mnt_ns->passive); -return current->nsproxy->mnt_ns; +struct mnt_namespace *mnt_ns; + +if (kreq->mnt_ns_id && kreq->spare) +return ERR_PTR (-EINVAL ); + +if (kreq->mnt_ns_id) +return lookup_mnt_ns (kreq->mnt_ns_id); + +if (kreq->spare) { +struct ns_common *ns; + +CLASS (fd, f)(kreq->spare); +if (!f.file) +return ERR_PTR (-EBADF ); + +if (!proc_ns_file (f.file)) +return ERR_PTR (-EINVAL ); + + ns =get_proc_ns (file_inode (f.file)); +if (ns->ops->type !=CLONE_NEWNS ) +return ERR_PTR (-EINVAL ); + + mnt_ns =to_mnt_ns (ns); + }else { + mnt_ns = current->nsproxy->mnt_ns; + } + +refcount_inc (&mnt_ns->passive); +return mnt_ns; } SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,
@@ -5269,7 +5294,7 @@ SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,if (ret)return ret; - ns =grab_requested_mnt_ns (kreq.mnt_ns_id); + ns =grab_requested_mnt_ns (&kreq);if (!ns)return -ENOENT ;
@@ -5396,7 +5421,7 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req,if (!kmnt_ids)return -ENOMEM ; - ns =grab_requested_mnt_ns (kreq.mnt_ns_id); + ns =grab_requested_mnt_ns (&kreq);if (!ns)return -ENOENT ;
Signature · 1 line
2.43.0
Christian Brauner
maintainerRFC PATCH 3/5 fs: add put_mnt_ns() cleanup helper
In reply to Christian BraunerAdd a simple helper to put a mount namespace reference.
--- include/linux/mnt_namespace.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/mnt_namespace.h +4 -0
@@ -3,6 +3,9 @@#define _NAMESPACE_H_#ifdef __KERNEL__ +#include <linux/cleanup.h> +#include <linux/err.h> +struct mnt_namespace ;struct fs_struct ;struct user_namespace ;
@@ -11,6 +14,7 @@ struct ns_common;extern struct mnt_namespace *copy_mnt_ns (unsigned long ,struct mnt_namespace *,struct user_namespace *,struct fs_struct *);extern void put_mnt_ns (struct mnt_namespace *ns); +DEFINE_FREE(put_mnt_ns, struct mnt_namespace *, if (!IS_ERR_OR_NULL(_T)) put_mnt_ns(_T))extern struct ns_common *from_mnt_ns (struct mnt_namespace *);extern const struct file_operations proc_mounts_operations;
Signature · 1 line
2.43.0
Christian Brauner
maintainerRFC PATCH 4/5 file: add fput() cleanup helper
In reply to Christian BraunerAdd a simple helper to put a file reference.
--- include/linux/file.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/file.h +2 -0
@@ -11,6 +11,7 @@#include <linux/posix_types.h> #include <linux/errno.h> #include <linux/cleanup.h> +#include <linux/err.h> struct file ;
@@ -96,6 +97,7 @@ extern void put_unused_fd(unsigned int fd); DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T), get_unused_fd_flags(flags), unsigned flags) +DEFINE_FREE(fput, struct file *, if (!IS_ERR_OR_NULL(_T)) fput(_T))/* * take_fd() will take care to set @fd to -EBADF ensuring that
Signature · 1 line
2.43.0
Christian Brauner
maintainerRFC PATCH 5/5 nsfs: iterate through mount namespaces
In reply to Christian BraunerIt is already possible to list mounts in other mount namespaces and to retrieve namespace file descriptors without having to go through procfs by deriving them from pidfds.
Augment these abilities by adding the ability to retrieve information about a mount namespace via NS_MNT_GET_INFO. This will return the mount namespace id and the number of mounts currently in the mount namespace. The number of mounts can be used to size the buffer that needs to be used for listmount() and is in general useful without having to actually iterate through all the mounts. The structure is extensible.
And add the ability to iterate through all mount namespaces over which the caller holds privilege returning the file descriptor for the next or previous mount namespace.
To retrieve a mount namespace the caller must be privileged wrt to it's owning user namespace. This means that PID 1 on the host can list all mounts in all mount namespaces or that a container can list all mounts of its nested containers.
Optionally pass a structure for NS_MNT_GET_INFO with NS_MNT_GET_{PREV,NEXT} to retrieve information about the mount namespace in one go. Both ioctls can be implemented for other namespace types easily.
Together with recent api additions this means one can iterate through all mounts in all mount namespaces without ever touching procfs.
--- fs/mount.h | 13 ++++++ fs/namespace.c | 35 ++++++++++++++-- fs/nsfs.c | 102 +++++++++++++++++++++++++++++++++++++++++++++- include/uapi/linux/nsfs.h | 15 +++++++ 4 files changed, 159 insertions(+), 6 deletions(-)
fs/mount.h +13 -0
@@ -155,3 +155,16 @@ static inline void move_from_ns(struct mount *mnt, struct list_head *dt_list)extern void mnt_cursor_del (struct mnt_namespace *ns,struct mount *cursor);bool has_locked_children (struct mount *mnt,struct dentry *dentry); +struct mnt_namespace *__lookup_next_mnt_ns (struct mnt_namespace *mnt_ns,bool previous); +static inline struct mnt_namespace *lookup_next_mnt_ns (struct mnt_namespace *mntns) +{ +return __lookup_next_mnt_ns (mntns, false); +} +static inline struct mnt_namespace *lookup_prev_mnt_ns (struct mnt_namespace *mntns) +{ +return __lookup_next_mnt_ns (mntns, true); +} +static inline struct mnt_namespace *to_mnt_ns (struct ns_common *ns) +{ + return container_of(ns, struct mnt_namespace, ns); +}
fs/namespace.c +31 -4
@@ -2060,14 +2060,41 @@ static bool is_mnt_ns_file(struct dentry *dentry) dentry->d_fsdata == &mntns_operations; } -static struct mnt_namespace *to_mnt_ns (struct ns_common *ns) +struct ns_common *from_mnt_ns (struct mnt_namespace *mnt) { - return container_of(ns, struct mnt_namespace, ns); +return &mnt->ns; } -struct ns_common *from_mnt_ns (struct mnt_namespace *mnt) +struct mnt_namespace *__lookup_next_mnt_ns (struct mnt_namespace *mntns,bool previous) { -return &mnt->ns; +guard (read_lock)(&mnt_ns_tree_lock); +for (;;) { +struct rb_node *node; + +if (previous) + node =rb_prev (&mntns->mnt_ns_tree_node); +else + node =rb_next (&mntns->mnt_ns_tree_node); +if (!node) +return ERR_PTR (-ENOENT ); + + mntns =node_to_mnt_ns (node); + node = &mntns->mnt_ns_tree_node; + +if (!ns_capable_noaudit (mntns->user_ns,CAP_SYS_ADMIN )) +continue ; + +/* +* Holding mnt_ns_tree_lock prevents the mount namespace from +* being freed but it may well be on it's deathbed. We want an +* active reference, not just a passive one here as we're +* persisting the mount namespace. +*/ +if (!refcount_inc_not_zero (&mntns->ns.count)) +continue ; + +return mntns; + } }static bool mnt_ns_loop (struct dentry *dentry)
fs/nsfs.c +100 -2
132 lines not on this page · raw message
include/uapi/linux/nsfs.h +15 -0
@@ -26,4 +26,19 @@/* Return thread-group leader id of pid in the target pid namespace. */ #define NS_GET_TGID_IN_PIDNS _IOR(NSIO, 0x9, int) +struct mnt_ns_info { +__u32 size; +__u32 nr_mounts; +__u64 mnt_ns_id; +}; + +#define MNT_NS_INFO_SIZE_VER0 16/* size of first published struct */ + +/* Get information about namespace. */ +#define NS_MNT_GET_INFO _IOR(NSIO, 10, struct mnt_ns_info) +/* Get next namespace. */ +#define NS_MNT_GET_NEXT _IOR(NSIO, 11, struct mnt_ns_info) +/* Get previous namespace. */ +#define NS_MNT_GET_PREV _IOR(NSIO, 12, struct mnt_ns_info) +#endif /* __LINUX_NSFS_H */
Signature · 1 line
2.43.0
Quoted 26 lines · Christian Brauner
Hey,
Recently, we added the ability to list mounts in other mount namespaces and the ability to retrieve namespace file descriptors without having to go through procfs by deriving them from pidfds.
This extends nsfs in two ways:
(1) Add the ability to retrieve information about a mount namespace via
NS_MNT_GET_INFO. This will return the mount namespace id and the number of mounts currently in the mount namespace. The number of mounts can be used to size the buffer that needs to be used forlistmount()and is in general useful without having to actually iterate through all the mounts.The structure is extensible.
(2) Add the ability to iterate through all mount namespaces over which the caller holds privilege returning the file descriptor for the next or previous mount namespace.
To retrieve a mount namespace the caller must be privileged wrt to it's owning user namespace. This means that PID 1 on the host can list all mounts in all mount namespaces or that a container can list all mounts of its nested containers.
Quoted 6 lines
Optionally pass a structure for
NS_MNT_GET_INFOwith NS_MNT_GET_{PREV,NEXT} to retrieve information about the mount namespace in one go.(1) and (2) can be implemented for other namespace types easily.
Love this, I think the only thing is a comment in include/uapi/linux/mount.h to indicate what spare is used for with the new stuff. I'll update the man page when this stuff lands but it would be good to document it somewhere. Other than that you can add
Thanks,
Josef
Quoted 329 lines · Christian Brauner
Hey,
Recently, we added the ability to list mounts in other mount namespaces
and the ability to retrieve namespace file descriptors without having to
go through procfs by deriving them from pidfds.This extends nsfs in two ways:
(1) Add the ability to retrieve information about a mount namespace via
NS_MNT_GET_INFO. This will return the mount namespace id and the number of mounts currently in the mount namespace. The number of mounts can be used to size the buffer that needs to be used forlistmount()and is in general useful without having to actually iterate through all the mounts.The structure is extensible.
(2) Add the ability to iterate through all mount namespaces over which
the caller holds privilege returning the file descriptor for the next or previous mount namespace.To retrieve a mount namespace the caller must be privileged wrt to
it's owning user namespace. This means that PID 1 on the host can list all mounts in all mount namespaces or that a container can list
all mounts of its nested containers.Optionally pass a structure for
NS_MNT_GET_INFOwith NS_MNT_GET_{PREV,NEXT} to retrieve information about the mount namespace in one go.(1) and (2) can be implemented for other namespace types easily.
Together with recent api additions this means one can iterate through all mounts in all mount namespaces without ever touching procfs. Here's
a sample programlist_all_mounts_everywhere.c:// SPDX-License-Identifier: GPL-2.0-or-later
#define
_GNU_SOURCE
#include <asm/unistd.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <linux/stat.h>
#include <sched.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/pidfd.h>
#include <sys/stat.h>
#include <sys/statfs.h>#define die_errno(format,
...) \
do
{ \
fprintf(stderr, "%m | %s: %d: %s: " format "\n",__FILE__, \
__LINE__,__func__, ##__VA_ARGS__); \
exit(EXIT_FAILURE);
\
} while (0)/* Get the id for a mount namespace */
#defineNS_GET_MNTNS_ID_IO(0xb7, 0x5)
/* Get next mount namespace. */struct
mnt_ns_info{
__u32size;
__u32nr_mounts;
__u64mnt_ns_id;
};#define
MNT_NS_INFO_SIZE_VER016 /* size of first published struct *//* Get information about namespace. */
#defineNS_MNT_GET_INFO_IOR(0xb7, 10, structmnt_ns_info)
/* Get next namespace. */
#defineNS_MNT_GET_NEXT_IOR(0xb7, 11, structmnt_ns_info)
/* Get previous namespace. */
#defineNS_MNT_GET_PREV_IOR(0xb7, 12, structmnt_ns_info)#define
PIDFD_GET_MNT_NAMESPACE_IO(0xFF, 3)#define
STATX_MNT_ID_UNIQUE0x00004000U/* Want/got extendedstx_mount_id*/#define
__NR_listmount458
#define__NR_statmount457/*
* @mask bits for statmount(2)
*/
#defineSTATMOUNT_SB_BASIC0x00000001U /* Want/gotsb_... */
#defineSTATMOUNT_MNT_BASIC0x00000002U/* Want/gotmnt_... */
#defineSTATMOUNT_PROPAGATE_FROM0x00000004U/* Want/gotpropagate_from*/
#defineSTATMOUNT_MNT_ROOT0x00000008U/* Want/got
mnt_root */
#defineSTATMOUNT_MNT_POINT0x00000010U/* Want/gotmnt_point*/
#defineSTATMOUNT_FS_TYPE0x00000020U/* Want/gotfs_type*/
#define STATMOUNT_MNT_NS_ID 0x00000040U /* Want/gotmnt_ns_id*/
#define STATMOUNT_MNT_OPTS 0x00000080U /* Want/gotmnt_opts*/struct statmount {
__u32size; /* Total size, including strings */
__u32mnt_opts;
__u64mask; /* What results were written */
__u32sb_dev_major; /* Device ID */
__u32sb_dev_minor;
__u64sb_magic; /* ..._SUPER_MAGIC */
__u32sb_flags; /*
SB_{RDONLY,SYNCHRONOUS,DIRSYNC,LAZYTIME} */
__u32fs_type; /* [str] Filesystem type */
__u64mnt_id; /* Unique ID of mount */
__u64mnt_parent_id; /* Unique ID of parent (for root ==mnt_id) */
__u32mnt_id_old; /* Reused IDs used in
proc/.../mountinfo */
__u32mnt_parent_id_old;
__u64mnt_attr; /*MOUNT_ATTR_... */
__u64mnt_propagation; /*
MS_{SHARED,SLAVE,PRIVATE,UNBINDABLE} */
__u64mnt_peer_group; /* ID of shared peer group */
__u64mnt_master; /* Mount receives propagation from
this ID */
__u64propagate_from; /* Propagation from in current
namespace */
__u32mnt_root; /* [str] Root of mount
relative to root of fs */
__u32mnt_point; /* [str] Mountpoint relative to
current root */
__u64mnt_ns_id;
__u64__spare2[49];
char str[]; /* Variable size part containing
strings */
};struct
mnt_id_req{
__u32size;
__u32spare;
__u64mnt_id;
__u64param;
__u64mnt_ns_id;
};#define
MNT_ID_REQ_SIZE_VER132 /* sizeof second published struct
*/#define
LSMT_ROOT0xffffffffffffffff/* root
mount */static int __statmount(__u64
mnt_id,__u64mnt_ns_id,__u64mask, struct statmount *stmnt,size_tbufsize, unsigned int flags)
{
structmnt_id_reqreq = {
.size =MNT_ID_REQ_SIZE_VER1,
.mnt_id =mnt_id,
.param = mask,
.mnt_ns_id =mnt_ns_id,
};return
syscall(__NR_statmount, &req, stmnt, bufsize, flags); }static struct statmount *sys_statmount(__u64
mnt_id,__u64mnt_ns_id,
__u64mask, unsigned int flags)
{
size_tbufsize = 1 << 15;
struct statmount *stmnt = NULL, *tmp = NULL; int ret;for (;;) {
tmp =realloc(stmnt, bufsize);
if (!tmp)
goto out;stmnt = tmp;
ret =__statmount(mnt_id, mnt_ns_id, mask, stmnt, bufsize, flags);
if (!ret)
return stmnt;if (errno != EOVERFLOW)
goto out;bufsize <<= 1;
if (bufsize >=UINT_MAX/ 2)
goto out;}
out:
free(stmnt);
printf("statmount failed");
return NULL;
}static
ssize_tsys_listmount(__u64mnt_id,__u64last_mnt_id,__u64mnt_ns_id,
__u64list[],size_tnum, unsigned int flags)
{
structmnt_id_reqreq = {
.size =MNT_ID_REQ_SIZE_VER1,
.mnt_id =mnt_id,
.param =last_mnt_id,
.mnt_ns_id =mnt_ns_id,
};return
syscall(__NR_listmount, &req, list, num, flags); }int
main(int argc, char *argv[])
{
#defineLISTMNT_BUFFER10
__u64list[LISTMNT_BUFFER],last_mnt_id= 0; int ret, pidfd,fd_mntns;
structmnt_ns_infoinfo = {};pidfd =
pidfd_open(getpid(), 0);
if (pidfd < 0)
die_errno("pidfd_open failed");
fd_mntns=ioctl(pidfd, PIDFD_GET_MNT_NAMESPACE, 0); if (fd_mntns< 0)die_errno("ioctl(PIDFD_GET_MNT_NAMESPACE) failed");ret =
ioctl(fd_mntns, NS_MNT_GET_INFO, &info); if (ret < 0)die_errno("ioctl(NS_GET_MNTNS_ID) failed");
printf("Listing %u mounts for mount namespace %d:%llu\n", info.nr_mounts, fd_mntns, info.mnt_ns_id);
for (;;) {
ssize_tnr_mounts;
next:
nr_mounts=sys_listmount(LSMT_ROOT, last_mnt_id, info.mnt_ns_id, list, LISTMNT_BUFFER, 0);
if (nr_mounts<= 0) {
printf("Finished listing mounts for mount namespace %d:%llu\n\n", fd_mntns, info.mnt_ns_id);
ret =ioctl(fd_mntns, NS_MNT_GET_NEXT, 0); if (ret < 0)die_errno("ioctl(NS_MNT_GET_NEXT) failed");
close(ret);
ret =ioctl(fd_mntns, NS_MNT_GET_NEXT, &info);
if (ret < 0) {
if (errno == ENOENT) {printf("Finished listing all mount namespaces\n");
exit(0);
}die_errno("ioctl(NS_MNT_GET_NEXT) failed");
}
close(fd_mntns);
fd_mntns= ret;
last_mnt_id= 0;
printf("Listing %u mounts for mount namespace %d:%llu\n", info.nr_mounts, fd_mntns, info.mnt_ns_id); goto next;
}for (
size_tcur = 0; cur <nr_mounts; cur++) { struct statmount *stmnt;
last_mnt_id= list[cur];stmnt = sys_statmount(last_mnt_id, info.mnt_ns_id,
STATMOUNT_SB_BASIC|STATMOUNT_MNT_BASIC|STATMOUNT_MNT_ROOT|STATMOUNT_MNT_POINT|STATMOUNT_MNT_NS_ID|STATMOUNT_MNT_OPTS|STATMOUNT_FS_TYPE, 0);
if (!stmnt) {
printf("Failed to statmount(%llu) in mount namespace(%llu)\n", last_mnt_id, info.mnt_ns_id); continue;
}printf("mnt_id(%u/%llu) |
mnt_parent_id(%u/%llu): %s @ %s ==> %s with options: %s\n",stmnt->mnt_id_old,stmnt->mnt_id,stmnt->mnt_parent_id_old, stmnt-Quoted 1 line
mnt_parent_id,
stmnt->str+stmnt->fs_type,stmnt->str+stmnt->mnt_root,stmnt->str+stmnt->mnt_point,stmnt->str+stmnt->mnt_opts); free(stmnt);
}
}exit(0);
}
Quoted 9 lines
Thanks!
ChristianSigned-off-by Christian Brauner <[email protected]>---
---
base-commit:720261cfc7329406a50c2a8536e0039b9dd9a4e5
change-id: 20240705-work-mount-namespace-126b73a11f5c
This all looks pretty straightforward to me. I do wish that we had proper libc bindings for this...or maybe even a new userland library?
I just get the feeling that all of this syscall() and ioctl() usage is eventually going to bite us in the ass. I don't have any concrete proposal for that however, and we do have some immediate need for this functionality, so, you can add