Blame SOURCES/0058-findmnt-add-option-to-list-all-fs-independent-flags.patch

6eff72
From 2f04609de018013a36396e6a10b317607fb0b625 Mon Sep 17 00:00:00 2001
6eff72
From: Roberto Bergantinos Corpas <rbergant@redhat.com>
6eff72
Date: Tue, 12 Jan 2021 11:58:53 +0100
6eff72
Subject: [PATCH 58/63] findmnt: add option to list all fs-independent flags
6eff72
6eff72
It might be useful for security auditing purposes list all possible
6eff72
mount flags/options including default set which are normally not listed.
6eff72
6eff72
This patch adds "--vfs-all" option to list all fs-independent flags
6eff72
on VFS-OPTIONS column, as well as libmount funcionality to accomplish
6eff72
it.
6eff72
6eff72
i.e.:
6eff72
6eff72
$ findmnt -o VFS-OPTIONS
6eff72
VFS-OPTIONS
6eff72
rw,relatime
6eff72
rw,nosuid,nodev,noexec,relatime
6eff72
rw,nosuid,nodev,noexec,relatime
6eff72
ro,nosuid,nodev,noexec
6eff72
...
6eff72
6eff72
$ findmnt --vfs-all -o VFS-OPTIONS
6eff72
VFS-OPTIONS
6eff72
rw,exec,suid,dev,async,loud,nomand,atime,noiversion,diratime,relatime,nostrictatime,nolazytime,symfollow
6eff72
rw,noexec,nosuid,nodev,async,loud,nomand,atime,noiversion,diratime,relatime,nostrictatime,nolazytime,symfollow
6eff72
rw,noexec,nosuid,nodev,async,loud,nomand,atime,noiversion,diratime,relatime,nostrictatime,nolazytime,symfollow
6eff72
ro,noexec,nosuid,nodev,async,loud,nomand,atime,noiversion,diratime,norelatime,nostrictatime,nolazytime,symfollow
6eff72
...
6eff72
6eff72
[kzak@redhat.com: - cleanup coding style and comments]
6eff72
6eff72
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1917852
6eff72
Upstream: http://github.com/karelzak/util-linux/commit/ff21f476f85ac9855452f4aac43a231c3c1e2ebc
6eff72
Signed-off-by: Roberto Bergantinos Corpas <rbergant@redhat.com>
6eff72
Signed-off-by: Karel Zak <kzak@redhat.com>
6eff72
---
6eff72
 libmount/docs/libmount-sections.txt |  1 +
6eff72
 libmount/src/fs.c                   | 32 +++++++++++++++++++++++++++++
6eff72
 libmount/src/libmount.h.in          |  1 +
6eff72
 libmount/src/libmount.sym           |  4 ++++
6eff72
 misc-utils/findmnt.8                |  6 ++++++
6eff72
 misc-utils/findmnt.c                | 15 +++++++++++---
6eff72
 misc-utils/findmnt.h                |  2 ++
6eff72
 7 files changed, 58 insertions(+), 3 deletions(-)
6eff72
6eff72
diff --git a/libmount/docs/libmount-sections.txt b/libmount/docs/libmount-sections.txt
6eff72
index dea724b2f..f296c0611 100644
6eff72
--- a/libmount/docs/libmount-sections.txt
6eff72
+++ b/libmount/docs/libmount-sections.txt
6eff72
@@ -224,6 +224,7 @@ mnt_fs_get_usedsize
6eff72
 mnt_fs_get_userdata
6eff72
 mnt_fs_get_user_options
6eff72
 mnt_fs_get_vfs_options
6eff72
+mnt_fs_get_vfs_options_all
6eff72
 mnt_fs_is_kernel
6eff72
 mnt_fs_is_netfs
6eff72
 mnt_fs_is_pseudofs
6eff72
diff --git a/libmount/src/fs.c b/libmount/src/fs.c
6eff72
index aae4961c3..34c09d66b 100644
6eff72
--- a/libmount/src/fs.c
6eff72
+++ b/libmount/src/fs.c
6eff72
@@ -924,6 +924,38 @@ const char *mnt_fs_get_vfs_options(struct libmnt_fs *fs)
6eff72
 	return fs ? fs->vfs_optstr : NULL;
6eff72
 }
6eff72
 
6eff72
+/**
6eff72
+ * mnt_fs_get_vfs_options_all:
6eff72
+ * @fs: fstab/mtab entry pointer
6eff72
+ *
6eff72
+ * Returns: pointer to newlly allocated string (can be freed by free(3)) or
6eff72
+ * NULL in case of error.  The string contains all (including defaults) mount
6eff72
+ * options.
6eff72
+ */
6eff72
+char *mnt_fs_get_vfs_options_all(struct libmnt_fs *fs)
6eff72
+{
6eff72
+	const struct libmnt_optmap *map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
6eff72
+	const struct libmnt_optmap *ent;
6eff72
+	const char *opts = mnt_fs_get_options(fs);
6eff72
+	char *result = NULL;
6eff72
+	unsigned long flags = 0;
6eff72
+
6eff72
+	if (!opts || mnt_optstr_get_flags(opts, &flags, map))
6eff72
+		return NULL;
6eff72
+
6eff72
+	for (ent = map ; ent && ent->name ; ent++){
6eff72
+		if (ent->id & flags) { /* non-default value */
6eff72
+			if (!(ent->mask & MNT_INVERT))
6eff72
+				mnt_optstr_append_option(&result, ent->name, NULL);
6eff72
+			else
6eff72
+				continue;
6eff72
+		} else if (ent->mask & MNT_INVERT)
6eff72
+			mnt_optstr_append_option(&result, ent->name, NULL);
6eff72
+	}
6eff72
+
6eff72
+	return result;
6eff72
+}
6eff72
+
6eff72
 /**
6eff72
  * mnt_fs_get_user_options:
6eff72
  * @fs: fstab/mtab entry pointer
6eff72
diff --git a/libmount/src/libmount.h.in b/libmount/src/libmount.h.in
6eff72
index c61514b59..1d9a053e0 100644
6eff72
--- a/libmount/src/libmount.h.in
6eff72
+++ b/libmount/src/libmount.h.in
6eff72
@@ -452,6 +452,7 @@ extern int mnt_fs_get_option(struct libmnt_fs *fs, const char *name,
6eff72
 extern const char *mnt_fs_get_fs_options(struct libmnt_fs *fs);
6eff72
 extern const char *mnt_fs_get_vfs_options(struct libmnt_fs *fs);
6eff72
 extern const char *mnt_fs_get_user_options(struct libmnt_fs *fs);
6eff72
+extern char *mnt_fs_get_vfs_options_all(struct libmnt_fs *fs);
6eff72
 
6eff72
 extern const char *mnt_fs_get_attributes(struct libmnt_fs *fs);
6eff72
 extern int mnt_fs_set_attributes(struct libmnt_fs *fs, const char *optstr);
6eff72
diff --git a/libmount/src/libmount.sym b/libmount/src/libmount.sym
6eff72
index ca16cafa1..636c564eb 100644
6eff72
--- a/libmount/src/libmount.sym
6eff72
+++ b/libmount/src/libmount.sym
6eff72
@@ -322,3 +322,7 @@ MOUNT_2.30 {
6eff72
 	mnt_context_enable_rwonly_mount;
6eff72
 	mnt_context_get_excode;
6eff72
 } MOUNT_2.28;
6eff72
+
6eff72
+MOUNT_2_37 {
6eff72
+	mnt_fs_get_vfs_options_all;
6eff72
+} MOUNT_2.30;
6eff72
diff --git a/misc-utils/findmnt.8 b/misc-utils/findmnt.8
6eff72
index 58dd38625..41a37cb5f 100644
6eff72
--- a/misc-utils/findmnt.8
6eff72
+++ b/misc-utils/findmnt.8
6eff72
@@ -249,6 +249,12 @@ It's possible to specify source (device) or target (mountpoint) to filter mount
6eff72
 .TP
6eff72
 .BR "\-\-verbose"
6eff72
 Force findmnt to print more information (\fB\-\-verify\fP only for now).
6eff72
+.TP
6eff72
+.B \-\-vfs-all
6eff72
+When used with
6eff72
+.BR VFS-OPTIONS
6eff72
+column, print all VFS (fs-independent) flags.  This option is designed for auditing purposes to
6eff72
+list also default VFS kernel mount options which are normally not listed.
6eff72
 .SH EXAMPLES
6eff72
 .IP "\fBfindmnt \-\-fstab \-t nfs\fP"
6eff72
 Prints all NFS filesystems defined in
6eff72
diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c
6eff72
index 184b6f7d7..a7b3af4f4 100644
6eff72
--- a/misc-utils/findmnt.c
6eff72
+++ b/misc-utils/findmnt.c
6eff72
@@ -542,7 +542,10 @@ static char *get_data(struct libmnt_fs *fs, int num)
6eff72
 		str = xstrdup(mnt_fs_get_options(fs));
6eff72
 		break;
6eff72
 	case COL_VFS_OPTIONS:
6eff72
-		str = xstrdup(mnt_fs_get_vfs_options(fs));
6eff72
+		if (flags & FL_VFS_ALL)
6eff72
+			str = mnt_fs_get_vfs_options_all(fs);
6eff72
+		else if (mnt_fs_get_vfs_options(fs))
6eff72
+			str = xstrdup(mnt_fs_get_vfs_options(fs));
6eff72
 		break;
6eff72
 	case COL_FS_OPTIONS:
6eff72
 		str = xstrdup(mnt_fs_get_fs_options(fs));
6eff72
@@ -1243,6 +1246,7 @@ static void __attribute__((__noreturn__)) usage(void)
6eff72
 	fputc('\n', out);
6eff72
 	fputs(_(" -x, --verify           verify mount table content (default is fstab)\n"), out);
6eff72
 	fputs(_("     --verbose          print more details\n"), out);
6eff72
+	fputs(_("     --vfs-all          print all VFS options\n"), out);
6eff72
 
6eff72
 	fputs(USAGE_SEPARATOR, out);
6eff72
 	printf(USAGE_HELP_OPTIONS(24));
6eff72
@@ -1271,8 +1275,9 @@ int main(int argc, char *argv[])
6eff72
 	struct libscols_table *table = NULL;
6eff72
 
6eff72
 	enum {
6eff72
-                FINDMNT_OPT_VERBOSE = CHAR_MAX + 1,
6eff72
-		FINDMNT_OPT_TREE
6eff72
+		FINDMNT_OPT_VERBOSE = CHAR_MAX + 1,
6eff72
+		FINDMNT_OPT_TREE,
6eff72
+		FINDMNT_OPT_VFS_ALL
6eff72
 	};
6eff72
 
6eff72
 	static const struct option longopts[] = {
6eff72
@@ -1313,6 +1318,7 @@ int main(int argc, char *argv[])
6eff72
 		{ "version",	    no_argument,       NULL, 'V'		 },
6eff72
 		{ "verbose",	    no_argument,       NULL, FINDMNT_OPT_VERBOSE },
6eff72
 		{ "tree",	    no_argument,       NULL, FINDMNT_OPT_TREE	 },
6eff72
+		{ "vfs-all",	    no_argument,       NULL, FINDMNT_OPT_VFS_ALL },
6eff72
 		{ NULL, 0, NULL, 0 }
6eff72
 	};
6eff72
 
6eff72
@@ -1479,6 +1485,9 @@ int main(int argc, char *argv[])
6eff72
 		case FINDMNT_OPT_TREE:
6eff72
 			force_tree = 1;
6eff72
 			break;
6eff72
+		case FINDMNT_OPT_VFS_ALL:
6eff72
+			flags |= FL_VFS_ALL;
6eff72
+			break;
6eff72
 		default:
6eff72
 			errtryhelp(EXIT_FAILURE);
6eff72
 		}
6eff72
diff --git a/misc-utils/findmnt.h b/misc-utils/findmnt.h
6eff72
index fbaa38e82..9a277b68a 100644
6eff72
--- a/misc-utils/findmnt.h
6eff72
+++ b/misc-utils/findmnt.h
6eff72
@@ -19,6 +19,8 @@ enum {
6eff72
 	FL_STRICTTARGET = (1 << 15),
6eff72
 	FL_VERBOSE	= (1 << 16),
6eff72
 
6eff72
+	FL_VFS_ALL	= (1 << 19),
6eff72
+
6eff72
 	/* basic table settings */
6eff72
 	FL_ASCII	= (1 << 20),
6eff72
 	FL_RAW		= (1 << 21),
6eff72
-- 
6eff72
2.31.1
6eff72