Skip to contentSkip to conversation

libfuse: more random fixes for fuse service containers

PATCH New Darrick J. Wong 1 participant 8 messages Jun 11

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 libfuse: more random fixes for fuse service containers

Hi all,

Here are some miscellaneous fixes for libfuse.

v3: correct complaints from maintainer, remove umount command
v2: add a few more patches

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D
---
Commits in this patchset:
* fsmount: don't truncate ms_flags in set_fsconfig_ms_flags
* mount_service: don't pass the chopped buffer to apply_fsconfig_mount_opts
* mount_service: call the new fsmount ms_flags helpers in the right order
* mountservice: improve checking of protocol version
* mountservice: don't print bdev preparation error when quiet mode is enabled
* mountservice: advertise fuseblk support
* libfuse: remove unmount command from the service api

---
 include/fuse_service.h      |   16 ++++---
 include/fuse_service_priv.h |    9 +---
 lib/fuse_service.c          |   86 +++++++++++++---------------------------
 lib/fuse_service_stub.c     |   10 ++---
 lib/fuse_versionscript      |    2 -
 lib/mount_fsmount.c         |    5 ++
 util/mount_service.c        |   93 ++++++++++---------------------------------
 7 files changed, 71 insertions(+), 150 deletions(-)
#1 lore raw

PATCH 1/7 fsmount: don't truncate ms_flags in set_fsconfig_ms_flags

From: Darrick J. Wong <[email protected]>

The mount(2) flags (i.e. MS_*) are unsigned long. Don't truncate them in this function by assigning to int.

Fixes 14cb7b93bb9688 ("Add support for the new linux mount API")
---
 lib/mount_fsmount.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/mount_fsmount.c +2 -1
index 0f64880e045ba0..83768f0b8d193b 100644
@@ -95,7 +95,8 @@ void log_fsconfig_kmsg(int fd)
 
 int set_fsconfig_ms_flags(int fsfd, unsigned long *ms_flags)
 {
-	int ret, flags = *ms_flags;
+	unsigned long flags = *ms_flags;
+	int ret;
 	int i;
 
 	for (i = 0; mount_flags[i].opt != NULL && flags != 0; i++) {
#2 lore raw

PATCH 2/7 mount_service: don't pass the chopped buffer to apply_fsconfig_mount_opts

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If mount_service_handle_mntopts_cmd previously looked at the mount options to screen for allow_{other,root}, then tokstr (aka oc->value) has been modified and not put back. This causes truncation of the mount options passed to fsconfig, so let's use the snapshot that we took previously. Codex noticed this.

Fixes 7211953256526f ("mount_service: use the fsmount API helpers from mount_fsmount.c")
---
 util/mount_service.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/mount_service.c +1 -1
index adcc7a296645ff..360a78bab3bd3d 100644
@@ -994,7 +994,7 @@ static int mount_service_handle_mntopts_cmd(struct mount_service *mo,
 
 #ifdef HAVE_NEW_MOUNT_API
 	if (mo->fsopenfd >= 0) {
-		int ret = apply_fsconfig_mount_opts(mo->fsopenfd, oc->value);
+		int ret = apply_fsconfig_mount_opts(mo->fsopenfd, mntopts);
 
 		if (ret < 0) {
 			free(mntopts);
#3 lore raw

PATCH 3/7 mount_service: call the new fsmount ms_flags helpers in the right order

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Codex noticed a discrepancy between the new fsmount code that Bernd wrote and my port of the even newer mount service code to use the helpers that Bernd wrote. Specifically, set_fsconfig_ms_flags only clears bits from MS_FLAGS if there's no corresponding MOUNT_ATTR_ flag, whereas ms_flags_to_mount_attrs always clears them.

In other words, set_fsconfig_ms_flags MUST be called before ms_flags_to_mount_attrs or we can lose the 'ro' option. Fix this.

Fixes 7211953256526f ("mount_service: use the fsmount API helpers from mount_fsmount.c")
---
 lib/mount_fsmount.c  |    2 ++
 util/mount_service.c |    5 ++---
 2 files changed, 4 insertions(+), 3 deletions(-)
lib/mount_fsmount.c +2 -0
index 83768f0b8d193b..89f5b1a112a9b7 100644
@@ -35,6 +35,7 @@
 #define MOUNT_ATTR_NOSYMFOLLOW  0x00200000
 #endif
 
+/* Must be called after set_fsconfig_ms_flags */
 unsigned long ms_flags_to_mount_attrs(unsigned long ms_flags,
 				      unsigned int *mount_attrs)
 {
@@ -93,6 +94,7 @@ void log_fsconfig_kmsg(int fd)
 	errno = err;
 }
 
+/* Must be called before ms_flags_to_mount_attrs */
 int set_fsconfig_ms_flags(int fsfd, unsigned long *ms_flags)
 {
 	unsigned long flags = *ms_flags;
util/mount_service.c +2 -3
index 360a78bab3bd3d..9890d6cbf028cf 100644
@@ -1410,14 +1410,12 @@ static int mount_service_fsopen_mount(struct mount_service *mo,
 				      const struct stat *stbuf)
 {
 	char tmp[64];
-	unsigned long ms_flags;
+	unsigned long ms_flags = ntohl(oc->ms_flags);
 	unsigned int attr_flags;
 	int mfd;
 	int error;
 	int ret;
 
-	ms_flags = ms_flags_to_mount_attrs(ntohl(oc->ms_flags), &attr_flags);
-
 	ret = set_fsconfig_ms_flags(mo->fsopenfd, &ms_flags);
 	if (ret) {
 		error = errno;
@@ -1429,6 +1427,7 @@ static int mount_service_fsopen_mount(struct mount_service *mo,
 	 * or string flags!  Return a magic code so the caller will fall back
 	 * to regular mount(2).
 	 */
+	ms_flags = ms_flags_to_mount_attrs(ms_flags, &attr_flags);
 	if (ms_flags)
 		return FUSE_MOUNT_FALLBACK_NEEDED;
 
#4 lore raw

PATCH 4/7 mountservice: improve checking of protocol version

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Codex complains that the protocol checking here is too stringent. For example, if the mount helper advertises min=1,max=2 but libfuse inside the container is older and only supports protocol version 1, the second check results in service mount failure when we could just use v1.

Fix this by requiring that maxver >= minver, and then looking for a lack of overlap between what libfuse supports and what the helper advertised.

Fixes 5d8b9a39a1cb3e ("mount_service: add systemd socket service mounting helper")
---
 lib/fuse_service.c |   25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
lib/fuse_service.c +19 -6
index 61d1cdf3f1eb86..7fd10211dbeab4 100644
@@ -449,6 +449,7 @@ static int negotiate_hello(struct fuse_service *sf)
 		.p.magic = htonl(FUSE_SERVICE_HELLO_REPLY),
 		.version = htons(FUSE_SERVICE_PROTO),
 	};
+	uint16_t minver, maxver;
 	uint32_t flags;
 	ssize_t size;
 
@@ -471,15 +472,27 @@ static int negotiate_hello(struct fuse_service *sf)
 		return -EBADMSG;
 	}
 
-	if (ntohs(hello.min_version) < FUSE_SERVICE_MIN_PROTO) {
-		fuse_log(FUSE_LOG_ERR, "fuse: unsupported min service protocol version %u\n",
-			ntohs(hello.min_version));
+	minver = ntohs(hello.min_version);
+	maxver = ntohs(hello.max_version);
+
+	if (minver > maxver) {
+		fuse_log(FUSE_LOG_ERR,
+"fuse: min service protocol version %u must be less than max %u\n",
+			 minver, maxver);
+		return -EOPNOTSUPP;
+	}
+
+	if (maxver < FUSE_SERVICE_MIN_PROTO) {
+		fuse_log(FUSE_LOG_ERR,
+"fuse: max service protocol version %u too old to support version %u\n",
+			 maxver, FUSE_SERVICE_MIN_PROTO);
 		return -EOPNOTSUPP;
 	}
 
-	if (ntohs(hello.max_version) > FUSE_SERVICE_MAX_PROTO) {
-		fuse_log(FUSE_LOG_ERR, "fuse: unsupported max service protocol version %u\n",
-			ntohs(hello.min_version));
+	if (minver > FUSE_SERVICE_MAX_PROTO) {
+		fuse_log(FUSE_LOG_ERR,
+"fuse: min service protocol version %u too new to support version %u\n",
+			 minver, FUSE_SERVICE_MAX_PROTO);
 		return -EOPNOTSUPP;
 	}
 
#5 lore raw

PATCH 5/7 mountservice: don't print bdev preparation error when quiet mode is enabled

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If the fuse server tries to open a file with FUSE_SERVICE_REQUEST_FILE_QUIET, that means they don't want the mount helper to emit any error messages about the file open() failing. We did this for the main open() call, but not for any of the code in prep_bdev. Oops.

Fixes 5d8b9a39a1cb3e ("mount_service: add systemd socket service mounting helper")
---
 util/mount_service.c |   21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
util/mount_service.c +13 -8
index 9890d6cbf028cf..df22c1182541b7 100644
@@ -710,23 +710,27 @@ static int mount_service_send_reply(const struct mount_service *mo, int error)
 }
 
 static int prepare_bdev(const struct mount_service *mo,
-			const struct fuse_service_open_command *oc, int fd)
+			const struct fuse_service_open_command *oc, int fd,
+			unsigned int request_flags)
 {
 	struct stat stbuf;
+	const bool quiet = (request_flags & FUSE_SERVICE_OPEN_QUIET);
 	int ret;
 
 	ret = fstat(fd, &stbuf);
 	if (ret) {
 		int error = errno;
 
-		fprintf(stderr, "%s: %s: %s\n",
-			mo->msgtag, oc->path, strerror(error));
+		if (!quiet)
+			fprintf(stderr, "%s: %s: %s\n",
+				mo->msgtag, oc->path, strerror(error));
 		return -error;
 	}
 
 	if (!S_ISBLK(stbuf.st_mode)) {
-		fprintf(stderr, "%s: %s: %s\n",
-			mo->msgtag, oc->path, strerror(ENOTBLK));
+		if (!quiet)
+			fprintf(stderr, "%s: %s: %s\n",
+				mo->msgtag, oc->path, strerror(ENOTBLK));
 		return -ENOTBLK;
 	}
 
@@ -739,8 +743,9 @@ static int prepare_bdev(const struct mount_service *mo,
 			int error = errno;
 
 			restore_privs();
-			fprintf(stderr, "%s: %s: %s\n",
-				mo->msgtag, oc->path, strerror(error));
+			if (!quiet)
+				fprintf(stderr, "%s: %s: %s\n",
+					mo->msgtag, oc->path, strerror(error));
 			return -error;
 		}
 		restore_privs();
@@ -798,7 +803,7 @@ static int mount_service_open_path(const struct mount_service *mo,
 	restore_privs();
 
 	if (S_ISBLK(expected_fmt)) {
-		ret = prepare_bdev(mo, oc, fd);
+		ret = prepare_bdev(mo, oc, fd, request_flags);
 		if (ret < 0) {
 			close(fd);
 			return mount_service_send_file_error(mo, -ret,
#6 lore raw

PATCH 6/7 mountservice: advertise fuseblk support

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Amazingly, permission for the use of the "blkdev" mount option (aka mounting with fuseblk.SUBTYPE instead of fuse.SUBTYPE) rests solely on a getuid()==0 check in userspace. That /might/ be an indication that the kernel also won't allow this, but as that's the criteria, let's advertise fuseblk explicitly so that a fuse server can discover the capability before calling fuse_service_session_mount().

---
 include/fuse_service.h      |    8 ++++++++
 include/fuse_service_priv.h |    4 +++-
 lib/fuse_service.c          |   10 ++++++++++
 lib/fuse_service_stub.c     |    5 +++++
 lib/fuse_versionscript      |    1 +
 util/mount_service.c        |    5 ++++-
 6 files changed, 31 insertions(+), 2 deletions(-)
include/fuse_service.h +8 -0
index bd89189cfe5d5b..0536312dc7120c 100644
@@ -61,6 +61,14 @@ static inline bool fuse_service_accepted(const struct fuse_service *sf)
  */
 bool fuse_service_can_allow_other(const struct fuse_service *sf);
 
+/**
+ * Will the mount service helper accept a fuseblk mount?
+ *
+ * @param sf service context
+ * @return true if it will, false if not
+ */
+bool fuse_service_can_fuseblk(const struct fuse_service *sf);
+
 /**
  * Release all resources associated with the service context.
  *
include/fuse_service_priv.h +3 -1
index 8560b1ac610143..cfbc444bf5dcbd 100644
@@ -54,8 +54,10 @@ struct fuse_service_packet {
 #define FUSE_SERVICE_MAX_PROTO	(1)
 
 #define FUSE_SERVICE_FLAG_ALLOW_OTHER	(1U << 0)
+#define FUSE_SERVICE_FLAG_FUSEBLK	(1U << 1)
 
-#define FUSE_SERVICE_FLAGS		(FUSE_SERVICE_FLAG_ALLOW_OTHER)
+#define FUSE_SERVICE_FLAGS		(FUSE_SERVICE_FLAG_ALLOW_OTHER | \
+					 FUSE_SERVICE_FLAG_FUSEBLK)
 
 struct fuse_service_hello {
 	struct fuse_service_packet p;
lib/fuse_service.c +10 -0
index 7fd10211dbeab4..2d7232c3484aea 100644
@@ -51,6 +51,9 @@ struct fuse_service {
 
 	/* can we use allow_other? */
 	bool allow_other;
+
+	/* can we use fuseblk? */
+	bool can_fuseblk;
 };
 
 static int __recv_fd(const struct fuse_service *sf,
@@ -505,6 +508,8 @@ static int negotiate_hello(struct fuse_service *sf)
 
 	if (flags & FUSE_SERVICE_FLAG_ALLOW_OTHER)
 		sf->allow_other = true;
+	if (flags & FUSE_SERVICE_FLAG_FUSEBLK)
+		sf->can_fuseblk = true;
 
 	size = __send_packet(sf, &reply, sizeof(reply));
 	if (size < 0) {
@@ -612,6 +617,11 @@ bool fuse_service_can_allow_other(const struct fuse_service *sf)
 	return sf->allow_other;
 }
 
+bool fuse_service_can_fuseblk(const struct fuse_service *sf)
+{
+	return sf->can_fuseblk;
+}
+
 int fuse_service_append_args(struct fuse_service *sf,
 			     struct fuse_args *existing_args)
 {
lib/fuse_service_stub.c +5 -0
index 1e27f5ea0c6b57..44aef47e351388 100644
@@ -59,6 +59,11 @@ bool fuse_service_can_allow_other(const struct fuse_service *sf)
 	return false;
 }
 
+bool fuse_service_can_fuseblk(const struct fuse_service *sf)
+{
+	return false;
+}
+
 int fuse_service_append_args(struct fuse_service *sf,
 			     struct fuse_args *existing_args)
 {
lib/fuse_versionscript +1 -0
index 19caa1fc9cb2cb..ab79209008595a 100644
@@ -236,6 +236,7 @@ FUSE_3.19 {
 		fuse_service_accept;
 		fuse_service_append_args;
 		fuse_service_can_allow_other;
+		fuse_service_can_fuseblk;
 		fuse_service_cmdline;
 		fuse_service_destroy;
 		fuse_service_exit;
util/mount_service.c +4 -1
index df22c1182541b7..6a6a8b608b7c6e 100644
@@ -367,7 +367,10 @@ static int mount_service_send_hello(const struct mount_service *mo)
 	struct fuse_service_hello_reply reply = { };
 	ssize_t size;
 
-	if (getuid() == 0 || user_allow_other)
+	if (getuid() == 0)
+		hello.flags |= htonl(FUSE_SERVICE_FLAG_FUSEBLK |
+				     FUSE_SERVICE_FLAG_ALLOW_OTHER);
+	else if (user_allow_other)
 		hello.flags |= htonl(FUSE_SERVICE_FLAG_ALLOW_OTHER);
 
 	size = __send_packet(mo, &hello, sizeof(hello));
#7 lore raw

PATCH 7/7 libfuse: remove unmount command from the service api

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Bernd received a complaint via github that the "unmount" command in the new fuse service API is prone to malicious symlink creation races because the umount2 call doesn't use UMOUNT_NOFOLLOW. Therefore, if you can trick a fuse service into asking the mount service helper to undo the mount just after you've replaced the mount with a symlink to a sensitive mount (e.g. /sys) then you whack a system hard.

I don't know why this unmounting functionality exists in libfuse, and apparently Bernd doesn't like it. Since the fuse service API has not yet been released, let's just rip out the command and pretend it never existed.

Fixes 5d8b9a39a1cb3e ("mount_service: add systemd socket service mounting helper")
---
 include/fuse_service.h      |    8 ------
 include/fuse_service_priv.h |    5 ----
 lib/fuse_service.c          |   51 -------------------------------------
 lib/fuse_service_stub.c     |    5 ----
 lib/fuse_versionscript      |    1 -
 util/mount_service.c        |   60 +------------------------------------------
 6 files changed, 1 insertion(+), 129 deletions(-)
include/fuse_service.h +0 -8
index 0536312dc7120c..d6aedea8f0f831 100644
@@ -218,14 +218,6 @@ int fuse_service_session_mount(struct fuse_service *sf, struct fuse_session *se,
 			       mode_t expected_fmt,
 			       struct fuse_cmdline_opts *opts);
 
-/**
- * Ask the mount helper to unmount th e filesystem.
- *
- * @param sf service context
- * @return 0 on success, or negative errno on failure
- */
-int fuse_service_session_unmount(const struct fuse_service *sf);
-
 /**
  * Bid farewell to the mount.service helper.  It is still necessary to call
  * fuse_service_destroy after this.
include/fuse_service_priv.h +0 -5
index cfbc444bf5dcbd..988f7c9251c86d 100644
@@ -37,7 +37,6 @@ struct fuse_service_memfd_argv {
 #define FUSE_SERVICE_MNTOPTS_CMD	0x4f505453	/* OPTS */
 #define FUSE_SERVICE_MNTPT_CMD		0x4d4e5450	/* MNTP */
 #define FUSE_SERVICE_MOUNT_CMD		0x444f4954	/* DOIT */
-#define FUSE_SERVICE_UNMOUNT_CMD	0x554d4e54	/* UMNT */
 #define FUSE_SERVICE_BYE_CMD		0x42594545	/* BYEE */
 #define FUSE_SERVICE_MTABOPTS_CMD	0x4d544142	/* MTAB */
 
@@ -150,10 +149,6 @@ struct fuse_service_mount_command {
 	uint32_t ms_flags;
 };
 
-struct fuse_service_unmount_command {
-	struct fuse_service_packet p;
-};
-
 int fuse_parse_cmdline_service(struct fuse_args *args,
 				 struct fuse_cmdline_opts *opts);
 
lib/fuse_service.c +0 -51

57 lines not on this page · raw message

lib/fuse_service_stub.c +0 -5
index 44aef47e351388..668c259891c729 100644
@@ -93,11 +93,6 @@ int fuse_service_session_mount(struct fuse_service *sf, struct fuse_session *se,
 	return -EOPNOTSUPP;
 }
 
-int fuse_service_session_unmount(const struct fuse_service *sf)
-{
-	return -EOPNOTSUPP;
-}
-
 void fuse_service_release(struct fuse_service *sf)
 {
 }
lib/fuse_versionscript +0 -1
index ab79209008595a..e8dc0066137dde 100644
@@ -250,7 +250,6 @@ FUSE_3.19 {
 		fuse_service_request_blockdev;
 		fuse_service_send_goodbye;
 		fuse_service_session_mount;
-		fuse_service_session_unmount;
 		fuse_req_secctx_count;
 		fuse_req_secctx_reset;
 		fuse_req_secctx_next;
util/mount_service.c +1 -59

78 lines not on this page · raw message