Skip to contentSkip to conversation

fs: listmount()/statmount() fix and sample program

PATCH Applied Jeff Layton 2 participants 4 messages Nov 15, 2024 – Nov 20, 2024 VFS

lore
Share & tools
lore
#0 lore raw

v2 cover fs: listmount()/statmount() fix and sample program

We had some recent queries internally asking how to use the new statmount() and listmount() interfaces. I was doing some other work in this area, so I whipped up this tool.

My hope is that this will represent something of a "rosetta stone" for how to translate between mountinfo and statmount(), and an example for other people looking to use the new interfaces.

It may also be possible to use this as the basis for a listmount() and statmount() testcase. We can call this program, and compare its output to the mountinfo file.

The second patch adds security mount options to the existing mnt_opts in the statmount() interface, which I think is the final missing piece here. The alternative to doing that would be to add a new string field for that, but I'm not sure that's worthwhile.

---
Changes in v2:
- fixed off-by-one bug in listmount last_mnt_id handling
- patch to add the security mount options to statmount()
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jeff Layton (2):
samples: add a mountinfo program to demonstrate statmount()/listmount()
fs: prepend statmount.mnt_opts string with security_sb_mnt_opts()

 fs/namespace.c          |   4 +
 samples/vfs/.gitignore  |   1 +
 samples/vfs/Makefile    |   2 +-
 samples/vfs/mountinfo.c | 271 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 277 insertions(+), 1 deletion(-)

---
base-commit: 4be4eaeb1a60a7d52e66123f2f52f2da017c9881
change-id: 20241112-statmount-77808bc02302

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
#1 lore raw

PATCH v2 1/2 samples: add a mountinfo program to demonstrate statmount()/listmount()

Add a new "mountinfo" sample userland program that demonstrates how to use statmount() and listmount() to get at the same info that /proc/pid/mountinfo provides.

The output of the program tries to mimic the mountinfo procfile contents. With the -p flag, it can be pointed at an arbitrary pid to print out info about its mount namespace. With the -r flag it will attempt to walk all of the namespaces under the pid's mount namespace and dump out mount info from all of them.

---
 samples/vfs/.gitignore  |   1 +
 samples/vfs/Makefile    |   2 +-
 samples/vfs/mountinfo.c | 271 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+), 1 deletion(-)
samples/vfs/.gitignore +1 -0
index 79212d91285bca72b0ff85f28aaccd2e803ac092..33a03cffe072fe2466c9df30ad47e9c58b0eea7c 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 /test-fsmount
 /test-statx
+/mountinfo
samples/vfs/Makefile +1 -1
index 6377a678134acf0d682151d751d2f5042dbf5e0a..fb9bb33fdc751556e806aa897f0dbd48f7e3a4d8 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
-userprogs-always-y += test-fsmount test-statx
+userprogs-always-y += test-fsmount test-statx mountinfo
 
 userccflags += -I usr/include
samples/vfs/mountinfo.c new file +271 -0

271 lines not on this page · raw message

Signature · 1 line
2.47.0
#2 lore raw

PATCH v2 2/2 fs: prepend statmount.mnt_opts string with security_sb_mnt_opts()

In reply to Jeff Layton

Currently these mount options aren't accessible via statmount().

The read handler for /proc/#/mountinfo calls security_sb_show_options() to emit the security options after emitting superblock flag options, but before calling sb->s_op->show_options.

Have statmount_mnt_opts() call security_sb_show_options() before calling ->show_options.

---
 fs/namespace.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/namespace.c +4 -0
index 206fc54feeba3e782f49778bcc99774d5a9d50a4..aae04aa10f984c69090bd1017112be17aa709d0c 100644
@@ -5055,6 +5055,10 @@ static int statmount_mnt_opts(struct kstatmount *s, struct seq_file *seq)
 	if (sb->s_op->show_options) {
 		size_t start = seq->count;
 
+		err = security_sb_show_options(seq, sb);
+		if (err)
+			return err;
+
 		err = sb->s_op->show_options(seq, mnt->mnt_root);
 		if (err)
 			return err;
Signature · 1 line
2.47.0
maintainer Applied
#3 lore raw
In reply to Jeff Layton
Quoted 9 lines · Jeff Layton

We had some recent queries internally asking how to use the new statmount() and listmount() interfaces. I was doing some other work in this area, so I whipped up this tool.

My hope is that this will represent something of a "rosetta stone" for how to translate between mountinfo and statmount(), and an example for other people looking to use the new interfaces.

[...]

Applied to the vfs-6.14.misc branch of the vfs/vfs.git tree. Patches in the vfs-6.14.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.14.misc

[1/2] samples: add a mountinfo program to demonstrate statmount()/listmount()

      https://git.kernel.org/vfs/vfs/c/ec07dced06b6
[2/2] fs: prepend statmount.mnt_opts string with security_sb_mnt_opts()
      https://git.kernel.org/vfs/vfs/c/d2269a2bfe4a