linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: viro@zeniv.linux.org.uk
Cc: dhowells@redhat.com, torvalds@linux-foundation.org,
	raven@themaw.net, mszeredi@redhat.com, christian@brauner.io,
	jannh@google.com, darrick.wong@oracle.com, kzak@redhat.com,
	jlayton@redhat.com, linux-api@vger.kernel.org,
	linux-fsdevel@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 15/18] fsinfo: Add an attribute that lists all the visible mounts in a namespace [ver #21]
Date: Mon, 03 Aug 2020 14:38:34 +0100	[thread overview]
Message-ID: <159646191446.1784947.11228235431863356055.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <159646178122.1784947.11705396571718464082.stgit@warthog.procyon.org.uk>

Add a filesystem attribute that exports a list of all the visible mounts in
a namespace, given the caller's chroot setting.  The returned list is an
array of:

	struct fsinfo_mount_child {
		__u64	mnt_unique_id;
		__u32	mnt_id;
		__u32	parent_id;
		__u32	mnt_notify_sum;
		__u32	sb_notify_sum;
	};

where each element contains a once-in-a-system-lifetime unique ID, the
mount ID (which may get reused), the parent mount ID and sums of the
notification/change counters for the mount and its superblock.

This works with a read lock on the namespace_sem, but ideally would do it
under the RCU read lock only.

Signed-off-by: David Howells <dhowells@redhat.com>
---

 fs/fsinfo.c                 |    1 +
 fs/internal.h               |    1 +
 fs/namespace.c              |   37 +++++++++++++++++++++++++++++++++++++
 include/uapi/linux/fsinfo.h |    4 ++++
 samples/vfs/test-fsinfo.c   |   22 ++++++++++++++++++++++
 5 files changed, 65 insertions(+)

diff --git a/fs/fsinfo.c b/fs/fsinfo.c
index 0540cce89555..f230124ffdf5 100644
--- a/fs/fsinfo.c
+++ b/fs/fsinfo.c
@@ -296,6 +296,7 @@ static const struct fsinfo_attribute fsinfo_common_attributes[] = {
 	FSINFO_STRING	(FSINFO_ATTR_MOUNT_POINT,	fsinfo_generic_mount_point),
 	FSINFO_STRING	(FSINFO_ATTR_MOUNT_POINT_FULL,	fsinfo_generic_mount_point_full),
 	FSINFO_LIST	(FSINFO_ATTR_MOUNT_CHILDREN,	fsinfo_generic_mount_children),
+	FSINFO_LIST	(FSINFO_ATTR_MOUNT_ALL,		fsinfo_generic_mount_all),
 	{}
 };
 
diff --git a/fs/internal.h b/fs/internal.h
index cb5edcc7125a..267b4aaf0271 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -102,6 +102,7 @@ extern int fsinfo_generic_mount_topology(struct path *, struct fsinfo_context *)
 extern int fsinfo_generic_mount_point(struct path *, struct fsinfo_context *);
 extern int fsinfo_generic_mount_point_full(struct path *, struct fsinfo_context *);
 extern int fsinfo_generic_mount_children(struct path *, struct fsinfo_context *);
+extern int fsinfo_generic_mount_all(struct path *, struct fsinfo_context *);
 
 /*
  * fs_struct.c
diff --git a/fs/namespace.c b/fs/namespace.c
index 122c12f9512b..1f2e06507244 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4494,4 +4494,41 @@ int fsinfo_generic_mount_children(struct path *path, struct fsinfo_context *ctx)
 	return ctx->usage;
 }
 
+/*
+ * Return information about all the mounts in the namespace referenced by the
+ * path.
+ */
+int fsinfo_generic_mount_all(struct path *path, struct fsinfo_context *ctx)
+{
+	struct mnt_namespace *ns;
+	struct mount *m, *p;
+	struct path chroot;
+	bool allow;
+
+	m = real_mount(path->mnt);
+	ns = m->mnt_ns;
+
+	get_fs_root(current->fs, &chroot);
+	rcu_read_lock();
+	allow = are_paths_connected(&chroot, path) || capable(CAP_SYS_ADMIN);
+	rcu_read_unlock();
+	path_put(&chroot);
+	if (!allow)
+		return -EPERM;
+
+	down_read(&namespace_sem);
+
+	list_for_each_entry(p, &ns->list, mnt_list) {
+		struct path mnt_root;
+
+		mnt_root.mnt	= &p->mnt;
+		mnt_root.dentry	= p->mnt.mnt_root;
+		if (are_paths_connected(path, &mnt_root))
+			fsinfo_store_mount(ctx, p, p == m);
+	}
+
+	up_read(&namespace_sem);
+	return ctx->usage;
+}
+
 #endif /* CONFIG_FSINFO */
diff --git a/include/uapi/linux/fsinfo.h b/include/uapi/linux/fsinfo.h
index 81329de6905e..e40192d98648 100644
--- a/include/uapi/linux/fsinfo.h
+++ b/include/uapi/linux/fsinfo.h
@@ -37,6 +37,7 @@
 #define FSINFO_ATTR_MOUNT_POINT_FULL	0x203	/* Absolute path of mount (string) */
 #define FSINFO_ATTR_MOUNT_TOPOLOGY	0x204	/* Mount object topology */
 #define FSINFO_ATTR_MOUNT_CHILDREN	0x205	/* Children of this mount (list) */
+#define FSINFO_ATTR_MOUNT_ALL		0x206	/* List all mounts in a namespace (list) */
 
 #define FSINFO_ATTR_AFS_CELL_NAME	0x300	/* AFS cell name (string) */
 #define FSINFO_ATTR_AFS_SERVER_NAME	0x301	/* Name of the Nth server (string) */
@@ -128,6 +129,8 @@ struct fsinfo_mount_topology {
 /*
  * Information struct element for fsinfo(FSINFO_ATTR_MOUNT_CHILDREN).
  * - An extra element is placed on the end representing the parent mount.
+ *
+ * Information struct element for fsinfo(FSINFO_ATTR_MOUNT_ALL).
  */
 struct fsinfo_mount_child {
 	__u64	mnt_unique_id;		/* Kernel-lifetime unique mount ID */
@@ -139,6 +142,7 @@ struct fsinfo_mount_child {
 };
 
 #define FSINFO_ATTR_MOUNT_CHILDREN__STRUCT struct fsinfo_mount_child
+#define FSINFO_ATTR_MOUNT_ALL__STRUCT struct fsinfo_mount_child
 
 /*
  * Information struct for fsinfo(FSINFO_ATTR_STATFS).
diff --git a/samples/vfs/test-fsinfo.c b/samples/vfs/test-fsinfo.c
index 374825ab85b0..596fa5e71762 100644
--- a/samples/vfs/test-fsinfo.c
+++ b/samples/vfs/test-fsinfo.c
@@ -365,6 +365,27 @@ static void dump_fsinfo_generic_mount_children(void *reply, unsigned int size)
 	       (unsigned long long)r->mnt_notify_sum, mp);
 }
 
+static void dump_fsinfo_generic_mount_all(void *reply, unsigned int size)
+{
+	struct fsinfo_mount_child *r = reply;
+	ssize_t mplen;
+	char path[32], *mp;
+
+	struct fsinfo_params params = {
+		.flags		= FSINFO_FLAGS_QUERY_MOUNT,
+		.request	= FSINFO_ATTR_MOUNT_POINT_FULL,
+	};
+
+	sprintf(path, "%u", r->mnt_id);
+	mplen = get_fsinfo(path, "FSINFO_ATTR_MOUNT_POINT_FULL", &params, (void **)&mp);
+	if (mplen < 0)
+		mp = "-";
+
+	printf("%5x %5x %12llx %10llu %s\n",
+	       r->mnt_id, r->parent_id, (unsigned long long)r->mnt_unique_id,
+	       r->mnt_notify_sum, mp);
+}
+
 static void dump_afs_fsinfo_server_address(void *reply, unsigned int size)
 {
 	struct fsinfo_afs_server_address *f = reply;
@@ -492,6 +513,7 @@ static const struct fsinfo_attribute fsinfo_attributes[] = {
 	FSINFO_STRING_N	(FSINFO_ATTR_MOUNT_POINT,	string),
 	FSINFO_STRING_N	(FSINFO_ATTR_MOUNT_POINT_FULL,	string),
 	FSINFO_LIST	(FSINFO_ATTR_MOUNT_CHILDREN,	fsinfo_generic_mount_children),
+	FSINFO_LIST	(FSINFO_ATTR_MOUNT_ALL,		fsinfo_generic_mount_all),
 
 	FSINFO_STRING	(FSINFO_ATTR_AFS_CELL_NAME,	string),
 	FSINFO_STRING	(FSINFO_ATTR_AFS_SERVER_NAME,	string),



  parent reply	other threads:[~2020-08-03 13:38 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-03 13:36 [PATCH 00/18] VFS: Filesystem information [ver #21] David Howells
2020-08-03 13:36 ` [PATCH 01/18] fsinfo: Introduce a non-repeating system-unique superblock ID " David Howells
2020-08-04  9:34   ` Miklos Szeredi
2020-08-03 13:36 ` [PATCH 02/18] fsinfo: Add fsinfo() syscall to query filesystem information " David Howells
2020-08-04 10:16   ` Miklos Szeredi
2020-08-04 11:34   ` David Howells
2020-08-27 11:27   ` Michael Kerrisk (man-pages)
2020-08-03 13:36 ` [PATCH 03/18] fsinfo: Provide a bitmap of the features a filesystem supports " David Howells
2020-08-03 13:37 ` [PATCH 04/18] fsinfo: Allow retrieval of superblock devname, options and stats " David Howells
2020-08-03 13:37 ` [PATCH 05/18] fsinfo: Allow fsinfo() to look up a mount object by ID " David Howells
2020-08-04 10:33   ` Miklos Szeredi
2020-08-03 13:37 ` [PATCH 06/18] fsinfo: Add a uniquifier ID to struct mount " David Howells
2020-08-04 10:41   ` Miklos Szeredi
2020-08-04 12:32     ` Ian Kent
2020-08-05 14:13   ` David Howells
2020-08-05 14:46     ` Miklos Szeredi
2020-08-05 15:30     ` David Howells
2020-08-05 19:33       ` Matthew Wilcox
2020-08-06  5:43         ` Ian Kent
2020-08-03 13:37 ` [PATCH 07/18] fsinfo: Allow mount information to be queried " David Howells
2020-08-03 13:37 ` [PATCH 08/18] fsinfo: Allow mount topology and propagation info to be retrieved " David Howells
2020-08-04 13:38   ` Miklos Szeredi
2020-08-05 15:37   ` David Howells
2020-08-05 17:19     ` Miklos Szeredi
2020-08-03 13:37 ` [PATCH 09/18] watch_queue: Mount event counters " David Howells
2020-08-03 13:37 ` [PATCH 10/18] fsinfo: Provide notification overrun handling support " David Howells
2020-08-04 13:56   ` Miklos Szeredi
2020-08-05  2:05     ` Ian Kent
2020-08-05  2:46       ` Ian Kent
2020-08-05  7:45         ` Miklos Szeredi
2020-08-05 11:23           ` Ian Kent
2020-08-05 11:27             ` Miklos Szeredi
2020-08-06  1:47               ` Ian Kent
2020-08-05 16:06   ` David Howells
2020-08-05 17:26     ` Miklos Szeredi
2020-08-03 13:37 ` [PATCH 11/18] fsinfo: sample: Mount listing program " David Howells
2020-08-03 13:38 ` [PATCH 12/18] fsinfo: Add API documentation " David Howells
2020-08-03 13:38 ` [PATCH 13/18] fsinfo: Add support for AFS " David Howells
2020-08-03 13:38 ` [PATCH 14/18] fsinfo: Add support to ext4 " David Howells
2020-08-03 13:38 ` David Howells [this message]
2020-08-04 14:05   ` [PATCH 15/18] fsinfo: Add an attribute that lists all the visible mounts in a namespace " Miklos Szeredi
2020-08-05  0:59     ` Ian Kent
2020-08-05 16:44   ` David Howells
2020-08-03 13:38 ` [PATCH 16/18] errseq: add a new errseq_scrape function " David Howells
2020-08-03 13:38 ` [PATCH 17/18] vfs: allow fsinfo to fetch the current state of s_wb_err " David Howells
2020-08-03 13:39 ` [PATCH 18/18] samples: add error state information to test-fsinfo.c " David Howells
2020-08-04 15:39 ` [PATCH 00/18] VFS: Filesystem information " James Bottomley
2020-08-04 19:18   ` Miklos Szeredi
2020-08-05 17:13 ` David Howells

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=159646191446.1784947.11228235431863356055.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=christian@brauner.io \
    --cc=darrick.wong@oracle.com \
    --cc=jannh@google.com \
    --cc=jlayton@redhat.com \
    --cc=kzak@redhat.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mszeredi@redhat.com \
    --cc=raven@themaw.net \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).