libfuse: more random fixes for fuse service containers
Subsystems (3)
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(-)
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.
--- lib/mount_fsmount.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/mount_fsmount.c +2 -1
@@ -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++) {
PATCH 2/7 mount_service: don't pass the chopped buffer to apply_fsconfig_mount_opts
In reply to Darrick J. WongFrom: 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.
--- util/mount_service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
util/mount_service.c +1 -1
@@ -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);
PATCH 3/7 mount_service: call the new fsmount ms_flags helpers in the right order
In reply to Darrick J. WongFrom: 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.
--- lib/mount_fsmount.c | 2 ++ util/mount_service.c | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-)
lib/mount_fsmount.c +2 -0
@@ -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
@@ -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 ;
PATCH 4/7 mountservice: improve checking of protocol version
In reply to Darrick J. WongFrom: 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.
--- lib/fuse_service.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
lib/fuse_service.c +19 -6
@@ -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 ; }
PATCH 5/7 mountservice: don't print bdev preparation error when quiet mode is enabled
In reply to Darrick J. WongFrom: 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.
--- util/mount_service.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
util/mount_service.c +13 -8
@@ -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,
PATCH 6/7 mountservice: advertise fuseblk support
In reply to Darrick J. WongFrom: 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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));
PATCH 7/7 libfuse: remove unmount command from the service api
In reply to Darrick J. WongFrom: 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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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