virtiofsd: prevent opening of special files (CVE-2020-35517)
PATCH v2 1/1 virtiofsd: prevent opening of special files (CVE-2020-35517)
A well-behaved FUSE client does not attempt to open special files with FUSE_OPEN because they are handled on the client side (e.g. device nodes are handled by client-side device drivers).
The check to prevent virtiofsd from opening special files is missing in a few cases, most notably FUSE_OPEN. A malicious client can cause virtiofsd to open a device node, potentially allowing the guest to escape. This can be exploited by a modified guest device driver. It is not exploitable from guest userspace since the guest kernel will handle special files inside the guest instead of sending FUSE requests.
This patch adds the missing checks to virtiofsd. This is a short-term solution because it does not prevent a compromised virtiofsd process from opening device nodes on the host.
---
v2:
* Add doc comment clarifying that symlinks are traversed client-side [Daniel]
This issue was diagnosed on public IRC and is therefore already known and not embargoed.
A stronger fix, and the long-term solution, is for users to mount the shared directory and any sub-mounts with nodev, as well as nosuid and noexec. Unfortunately virtiofsd cannot do this automatically because bind mounts added by the user after virtiofsd has launched would not be detected. I suggest the following:
1. Modify libvirt and Kata Containers to explicitly set these mount options.
2. Then modify virtiofsd to check that the shared directory has the necessary options at startup. Refuse to start if the options are missing so that the user is aware of the security requirements.
As a bonus this also increases the likelihood that other host processes besides virtiofsd will be protected by nosuid/noexec/nodev so that a malicious guest cannot drop these files in place and then arrange for a host process to come across them.
Additionally, user namespaces have been discussed. They seem like a worthwhile addition as an unprivileged or privilege-separated mode although there are limitations with respect to security xattrs and the actual uid/gid stored on the host file system not corresponding to the guest uid/gid.
--- tools/virtiofsd/passthrough_ll.c | 85 +++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 28 deletions(-)
tools/virtiofsd/passthrough_ll.c +57 -28
@@ -555,6 +555,30 @@ static int lo_fd(fuse_req_t req, fuse_ino_t ino)return fd; } +/* +* Open a file descriptor for an inode. Returns -EBADF if the inode is not a +* regular file or a directory. Use this helper function instead of raw +* openat(2) to prevent security issues when a malicious client opens special +* files such as block device nodes. Symlink inodes are also rejected since +* symlinks must already have been traversed on the client side. +*/ +static int lo_inode_open (struct lo_data *lo,struct lo_inode *inode, +int open_flags) +{ + g_autofree char *fd_str = g_strdup_printf("%d", inode->fd); +int fd; + +if (!S_ISREG (inode->filetype) && !S_ISDIR (inode->filetype)) { +return -EBADF ; + } + + fd =openat (lo->proc_self_fd, fd_str, open_flags); +if (fd <0 ) { +return -errno; + } +return fd; +} +static void lo_init (void *userdata,struct fuse_conn_info *conn) {struct lo_data *lo = (struct lo_data *)userdata;
@@ -684,8 +708,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,if (fi) { truncfd = fd; }else { -sprintf (procname,"%i" , ifd); - truncfd =openat (lo->proc_self_fd, procname,O_RDWR ); + truncfd =lo_inode_open (lo, inode,O_RDWR );if (truncfd <0 ) { goto out_err; }
@@ -1725,7 +1748,6 @@ static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo, pid_t pid, int *err) { struct lo_inode_plock *plock; -char procname[64 ];int fd; plock =
@@ -1742,12 +1764,10 @@ static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo, }/* Open another instance of file which can be used for ofd locks. */ -sprintf (procname,"%i" , inode->fd); -/* TODO: What if file is not writable? */ - fd =openat (lo->proc_self_fd, procname,O_RDWR ); -if (fd ==-1 ) { - *err = errno; + fd =lo_inode_open (lo, inode,O_RDWR ); +if (fd <0 ) { + *err = -fd;free (plock);return NULL ; }
@@ -1894,18 +1914,24 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) {int fd;ssize_t fh; -char buf[64 ];struct lo_data *lo =lo_data (req); +struct lo_inode *inode =lo_inode (req, ino);fuse_log (FUSE_LOG_DEBUG ,"lo_open(ino=%" PRIu64", flags=%d)\n" , ino, fi->flags); +if (!inode) { +fuse_reply_err (req,EBADF ); +return ; + } +update_open_flags (lo->writeback, lo->allow_direct_io, fi); -sprintf (buf,"%i" ,lo_fd (req, ino)); - fd =openat (lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW ); -if (fd ==-1 ) { -return (void )fuse_reply_err (req, errno); + fd =lo_inode_open (lo, inode, fi->flags & ~O_NOFOLLOW ); +if (fd <0 ) { +lo_inode_put (lo, &inode); +fuse_reply_err (req, -fd); +return ; }pthread_mutex_lock (&lo->mutex);
@@ -1913,6 +1939,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)pthread_mutex_unlock (&lo->mutex);if (fh ==-1 ) {close (fd); +lo_inode_put (lo, &inode);fuse_reply_err (req,ENOMEM );return ; }
@@ -1923,6 +1950,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) }else if (lo->cache ==CACHE_ALWAYS ) { fi->keep_cache =1 ; } +lo_inode_put (lo, &inode);fuse_reply_open (req, fi); }
@@ -1982,39 +2010,40 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)static void lo_fsync (fuse_req_t req,fuse_ino_t ino,int datasync,struct fuse_file_info *fi) { +struct lo_inode *inode =lo_inode (req, ino); +struct lo_data *lo =lo_data (req);int res;int fd; -char *buf;fuse_log (FUSE_LOG_DEBUG ,"lo_fsync(ino=%" PRIu64", fi=0x%p)\n" , ino, (void *)fi); +if (!inode) { +fuse_reply_err (req,EBADF ); +return ; + } +if (!fi) { -struct lo_data *lo =lo_data (req); - - res =asprintf (&buf,"%i" ,lo_fd (req, ino)); -if (res ==-1 ) { -return (void )fuse_reply_err (req, errno); - } - - fd =openat (lo->proc_self_fd, buf,O_RDWR ); -free (buf); -if (fd ==-1 ) { -return (void )fuse_reply_err (req, errno); + fd =lo_inode_open (lo, inode,O_RDWR ); +if (fd <0 ) { + res = -fd; + goto out; } }else { fd =lo_fi_fd (req, fi); }if (datasync) { - res =fdatasync (fd); + res =fdatasync (fd) ==-1 ? errno :0 ; }else { - res =fsync (fd); + res =fsync (fd) ==-1 ? errno :0 ; }if (!fi) {close (fd); } -fuse_reply_err (req, res ==-1 ? errno :0 ); +out: +lo_inode_put (lo, &inode); +fuse_reply_err (req, res); } static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
Signature · 1 line
2.29.2
Quoted 15 lines · Stefan Hajnoczi
A well-behaved FUSE client does not attempt to open special files with
FUSE_OPENbecause they are handled on the client side (e.g. device nodes are handled by client-side device drivers).The check to prevent virtiofsd from opening special files is missing in a few cases, most notably
FUSE_OPEN. A malicious client can cause virtiofsd to open a device node, potentially allowing the guest to escape. This can be exploited by a modified guest device driver. It is not exploitable from guest userspace since the guest kernel will handle special files inside the guest instead of sending FUSE requests.This patch adds the missing checks to virtiofsd. This is a short-term solution because it does not prevent a compromised virtiofsd process from opening device nodes on the host.
Quoted 5 lines
Reported-by Alex Xu <[email protected]>Fixes CVE-2020-35517Reviewed-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Vivek Goyal <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 11 lines · Stefan Hajnoczi
A well-behaved FUSE client does not attempt to open special files with
FUSE_OPENbecause they are handled on the client side (e.g. device nodes are handled by client-side device drivers).The check to prevent virtiofsd from opening special files is missing in a few cases, most notably
FUSE_OPEN. A malicious client can cause virtiofsd to open a device node, potentially allowing the guest to escape. This can be exploited by a modified guest device driver. It is not exploitable from guest userspace since the guest kernel will handle special files inside the guest instead of sending FUSE requests.
Quoted 6 lines
This patch adds the missing checks to virtiofsd. This is a short-term solution because it does not prevent a compromised virtiofsd process from opening device nodes on the host.
Reported-by Alex Xu <[email protected]>Fixes CVE-2020-35517
Should this also explicitly
Or would only a long-term fix target that?
Quoted 37 lines
Reviewed-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Vivek Goyal <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
v2:
* Add doc comment clarifying that symlinks are traversed client-side [Daniel]This issue was diagnosed on public IRC and is therefore already known and not embargoed.
A stronger fix, and the long-term solution, is for users to mount the shared directory and any sub-mounts with nodev, as well as nosuid and noexec. Unfortunately virtiofsd cannot do this automatically because bind mounts added by the user after virtiofsd has launched would not be detected. I suggest the following:
1. Modify libvirt and Kata Containers to explicitly set these mount options.
2. Then modify virtiofsd to check that the shared directory has the necessary options at startup. Refuse to start if the options are missing so that the user is aware of the security requirements.As a bonus this also increases the likelihood that other host processes besides virtiofsd will be protected by nosuid/noexec/nodev so that a malicious guest cannot drop these files in place and then arrange for a host process to come across them.
Additionally, user namespaces have been discussed. They seem like a worthwhile addition as an unprivileged or privilege-separated mode although there are limitations with respect to security xattrs and the actual uid/gid stored on the host file system not corresponding to the guest uid/gid.
--- tools/virtiofsd/passthrough_ll.c | 85 +++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 28 deletions(-)
Quoted 8 lines · On Tue, 26 Jan 2021 10:35:02 +0000 Stefan Hajnoczi <[email protected]> wrote:
A well-behaved FUSE client does not attempt to open special files with
FUSE_OPENbecause they are handled on the client side (e.g. device nodes are handled by client-side device drivers).The check to prevent virtiofsd from opening special files is missing in a few cases, most notably
FUSE_OPEN. A malicious client can cause virtiofsd to open a device node, potentially allowing the guest to escape.
or pretty much anything nasty you can think of, e.g. DoS if the malicious client repeatedly asks virtiofsd to open FIFOs the other side of which is never opened.
Quoted 8 lines
This can be exploited by a modified guest device driver. It is not exploitable from guest userspace since the guest kernel will handle special files inside the guest instead of sending FUSE requests.
This patch adds the missing checks to virtiofsd. This is a short-term solution because it does not prevent a compromised virtiofsd process from opening device nodes on the host.
Quoted 6 lines
Reported-by Alex Xu <[email protected]>Fixes CVE-2020-35517Reviewed-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Vivek Goyal <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
The patch looks pretty good to me. It just seems to be missing a change in lo_create():
fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
mode);A malicious guest could have created anything called ${name} in this directory before calling FUSE_CREATE and we'll open it blindly, or I'm missing something ?
Quoted 204 lines
v2:
* Add doc comment clarifying that symlinks are traversed client-side [Daniel]This issue was diagnosed on public IRC and is therefore already known and not embargoed.
A stronger fix, and the long-term solution, is for users to mount the shared directory and any sub-mounts with nodev, as well as nosuid and noexec. Unfortunately virtiofsd cannot do this automatically because bind mounts added by the user after virtiofsd has launched would not be detected. I suggest the following:
1. Modify libvirt and Kata Containers to explicitly set these mount options.
2. Then modify virtiofsd to check that the shared directory has the necessary options at startup. Refuse to start if the options are missing so that the user is aware of the security requirements.As a bonus this also increases the likelihood that other host processes besides virtiofsd will be protected by nosuid/noexec/nodev so that a malicious guest cannot drop these files in place and then arrange for a host process to come across them.
Additionally, user namespaces have been discussed. They seem like a worthwhile addition as an unprivileged or privilege-separated mode although there are limitations with respect to security xattrs and the actual uid/gid stored on the host file system not corresponding to the guest uid/gid.
--- tools/virtiofsd/passthrough_ll.c | 85 +++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 28 deletions(-)tools/virtiofsd/passthrough_ll.c +57
-28@@ -555,6 +555,30 @@ static int lo_fd(fuse_req_t req, fuse_ino_t ino)return fd; } +/* +* Open a file descriptor for an inode. Returns -EBADF if the inode is not a +* regular file or a directory. Use this helper function instead of raw +* openat(2) to prevent security issues when a malicious client opens special +* files such as block device nodes. Symlink inodes are also rejected since +* symlinks must already have been traversed on the client side. +*/ +static int lo_inode_open (struct lo_data *lo,struct lo_inode *inode, +int open_flags) +{ + g_autofree char *fd_str = g_strdup_printf("%d", inode->fd); +int fd; + +if (!S_ISREG (inode->filetype) && !S_ISDIR (inode->filetype)) { +return -EBADF ; + } + + fd =openat (lo->proc_self_fd, fd_str, open_flags); +if (fd <0 ) { +return -errno; + } +return fd; +} +static void lo_init (void *userdata,struct fuse_conn_info *conn) {struct lo_data *lo = (struct lo_data *)userdata;@@ -684,8 +708,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,if (fi) { truncfd = fd; }else { -sprintf (procname,"%i" , ifd); - truncfd =openat (lo->proc_self_fd, procname,O_RDWR ); + truncfd =lo_inode_open (lo, inode,O_RDWR );if (truncfd <0 ) { goto out_err; }@@ -1725,7 +1748,6 @@ static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo, pid_t pid, int *err) { struct lo_inode_plock *plock; -char procname[64 ];int fd; plock =@@ -1742,12 +1764,10 @@ static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo, }/* Open another instance of file which can be used for ofd locks. */ -sprintf (procname,"%i" , inode->fd); -/* TODO: What if file is not writable? */ - fd =openat (lo->proc_self_fd, procname,O_RDWR ); -if (fd ==-1 ) { - *err = errno; + fd =lo_inode_open (lo, inode,O_RDWR ); +if (fd <0 ) { + *err = -fd;free (plock);return NULL ; }@@ -1894,18 +1914,24 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) {int fd;ssize_t fh; -char buf[64 ];struct lo_data *lo =lo_data (req); +struct lo_inode *inode =lo_inode (req, ino);fuse_log (FUSE_LOG_DEBUG ,"lo_open(ino=%" PRIu64", flags=%d)\n" , ino, fi->flags); +if (!inode) { +fuse_reply_err (req,EBADF ); +return ; + } +update_open_flags (lo->writeback, lo->allow_direct_io, fi); -sprintf (buf,"%i" ,lo_fd (req, ino)); - fd =openat (lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW ); -if (fd ==-1 ) { -return (void )fuse_reply_err (req, errno); + fd =lo_inode_open (lo, inode, fi->flags & ~O_NOFOLLOW ); +if (fd <0 ) { +lo_inode_put (lo, &inode); +fuse_reply_err (req, -fd); +return ; }pthread_mutex_lock (&lo->mutex);@@ -1913,6 +1939,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)pthread_mutex_unlock (&lo->mutex);if (fh ==-1 ) {close (fd); +lo_inode_put (lo, &inode);fuse_reply_err (req,ENOMEM );return ; }@@ -1923,6 +1950,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) }else if (lo->cache ==CACHE_ALWAYS ) { fi->keep_cache =1 ; } +lo_inode_put (lo, &inode);fuse_reply_open (req, fi); }@@ -1982,39 +2010,40 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)static void lo_fsync (fuse_req_t req,fuse_ino_t ino,int datasync,struct fuse_file_info *fi) { +struct lo_inode *inode =lo_inode (req, ino); +struct lo_data *lo =lo_data (req);int res;int fd; -char *buf;fuse_log (FUSE_LOG_DEBUG ,"lo_fsync(ino=%" PRIu64", fi=0x%p)\n" , ino, (void *)fi); +if (!inode) { +fuse_reply_err (req,EBADF ); +return ; + } +if (!fi) { -struct lo_data *lo =lo_data (req); - - res =asprintf (&buf,"%i" ,lo_fd (req, ino)); -if (res ==-1 ) { -return (void )fuse_reply_err (req, errno); - } - - fd =openat (lo->proc_self_fd, buf,O_RDWR ); -free (buf); -if (fd ==-1 ) { -return (void )fuse_reply_err (req, errno); + fd =lo_inode_open (lo, inode,O_RDWR ); +if (fd <0 ) { + res = -fd; + goto out; } }else { fd =lo_fi_fd (req, fi); }if (datasync) { - res =fdatasync (fd); + res =fdatasync (fd) ==-1 ? errno :0 ; }else { - res =fsync (fd); + res =fsync (fd) ==-1 ? errno :0 ; }if (!fi) {close (fd); } -fuse_reply_err (req, res ==-1 ? errno :0 ); +out: +lo_inode_put (lo, &inode); +fuse_reply_err (req, res); } static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
Quoted 4 lines · Greg Kurz
On Tue, 26 Jan 2021 10:35:02 +0000
Stefan Hajnoczi <[email protected]> wrote:
Quoted 8 lines
The patch looks pretty good to me. It just seems to be missing a change in
lo_create():fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);A malicious guest could have created anything called ${name} in this directory before calling
FUSE_CREATEand we'll open it blindly, or I'm missing something ?
Right, this seems like an omission.
Also the "& ~O_NOFOLLOW" looks like a copy-paste bug, since unlike lo_open(), lo_create() is not opening a proc symlink.
So that should be replaced with "| O_NOFOLLOW"
Thanks,
Miklos
Quoted 14 lines · On Wed, 27 Jan 2021 10:25:28 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 4 lines · Greg Kurz
On Tue, 26 Jan 2021 10:35:02 +0000
Stefan Hajnoczi <[email protected]> wrote:Quoted 8 lines
The patch looks pretty good to me. It just seems to be missing a change in
lo_create():fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);A malicious guest could have created anything called ${name} in this directory before calling
FUSE_CREATEand we'll open it blindly, or I'm missing something ?
Quoted 7 lines
Right, this seems like an omission.
Also the "& ~O_NOFOLLOW" looks like a copy-paste bug, since unlike
lo_open(),lo_create()is not opening a proc symlink.So that should be replaced with "|
O_NOFOLLOW"
Yes, I've realized that later on. We should definitely enforce O_NOFOLLOW to avoid symlink escapes.
Then comes the case of special files... A well-known case is the FIFO that causes openat() to block as described in my response. FWIW, we addressed this one in 9P by adding O_NONBLOCK and fixing the flags to the client expectation with fcntl(F_SETFL). But this is just a protection against being blocked. Blindly opening a special file can lead to any kind of troubles you can think of... so it really looks that the only sane way to be safe from such an attack is to forbid openat() of special files at the filesystem level.
Quoted 3 lines
Thanks,
Miklos
Quoted 30 lines · Greg Kurz
Quoted 14 lines · On Wed, 27 Jan 2021 10:25:28 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 4 lines · Greg Kurz
On Tue, 26 Jan 2021 10:35:02 +0000
Stefan Hajnoczi <[email protected]> wrote:Quoted 8 lines
The patch looks pretty good to me. It just seems to be missing a change in
lo_create():fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);A malicious guest could have created anything called ${name} in this directory before calling
FUSE_CREATEand we'll open it blindly, or I'm missing something ?Quoted 7 lines
Right, this seems like an omission.
Also the "& ~O_NOFOLLOW" looks like a copy-paste bug, since unlike
lo_open(),lo_create()is not opening a proc symlink.So that should be replaced with "|
O_NOFOLLOW"Yes, I've realized that later on. We should definitely enforce
O_NOFOLLOWto avoid symlink escapes.
Quoted 8 lines
Then comes the case of special files... A well-known case is the FIFO that causes
openat()to block as described in my response. FWIW, we addressed this one in 9P by addingO_NONBLOCKand fixing the flags to the client expectation withfcntl(F_SETFL). But this is just a protection against being blocked. Blindly opening a special file can lead to any kind of troubles you can think of... so it really looks that the only sane way to be safe from such an attack is to forbidopenat()of special files at the filesystem level.
Another solution specifically for O_CREAT without O_EXCL would be to
turn it into an exclusive create. If that fails with EEXIST then try
the normal open path (open with O_PATH, fstat, open proc symlink). If that fails with ENOENT, then retry the whole thing a certain number of times. If it still fails then somebody is definitely messing with us and we can fail the request with EIO.
Rather ugly, but I can't think of anything better.
Thanks,
Miklos
Quoted 40 lines · On Wed, 27 Jan 2021 11:34:52 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 30 lines · Greg Kurz
Quoted 14 lines · On Wed, 27 Jan 2021 10:25:28 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 4 lines · Greg Kurz
On Tue, 26 Jan 2021 10:35:02 +0000
Stefan Hajnoczi <[email protected]> wrote:Quoted 8 lines
The patch looks pretty good to me. It just seems to be missing a change in
lo_create():fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);A malicious guest could have created anything called ${name} in this directory before calling
FUSE_CREATEand we'll open it blindly, or I'm missing something ?Quoted 7 lines
Right, this seems like an omission.
Also the "& ~O_NOFOLLOW" looks like a copy-paste bug, since unlike
lo_open(),lo_create()is not opening a proc symlink.So that should be replaced with "|
O_NOFOLLOW"Yes, I've realized that later on. We should definitely enforce
O_NOFOLLOWto avoid symlink escapes.Quoted 8 lines
Then comes the case of special files... A well-known case is the FIFO that causes
openat()to block as described in my response. FWIW, we addressed this one in 9P by addingO_NONBLOCKand fixing the flags to the client expectation withfcntl(F_SETFL). But this is just a protection against being blocked. Blindly opening a special file can lead to any kind of troubles you can think of... so it really looks that the only sane way to be safe from such an attack is to forbidopenat()of special files at the filesystem level.
Quoted 2 lines
Another solution specifically for
O_CREATwithoutO_EXCLwould be to turn it into an exclusive create.
Would this added O_EXCL then appear on the client side, e.g. to guest userspace doing fcntl(F_GETFL) ?
Quoted 2 lines
If that fails with EEXIST then try
the normal open path (open withO_PATH, fstat, open proc symlink). If
open(O_PATH | O_NOFOLLOW) + fstatat(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW) would indeed allow to filter out anything that isn't a directory and to safely open the proc symlink.
Quoted 1 line
that fails with ENOENT, then retry the whole thing a certain number of
Indeed someone could have unlinked the file in the meantime, in which case the open(O_PATH | O_NOFOLLOW) would fail, but if it succeeds then we cannot hit ENOENT anymore AFAICT.
Quoted 3 lines
times. If it still fails then somebody is definitely messing with us and we can fail the request with EIO.
Not sure what the retry+timeout is trying to mitigate here... why not returning EIO right away ?
Quoted 5 lines
Rather ugly, but I can't think of anything better.
Thanks,
Miklos
Quoted 9 lines · Greg Kurz
Quoted 2 lines · On Wed, 27 Jan 2021 11:34:52 +0100 Miklos Szeredi <[email protected]> wrote:
Another solution specifically for
O_CREATwithoutO_EXCLwould be to turn it into an exclusive create.Would this added
O_EXCLthen appear on the client side, e.g. to guest userspace doingfcntl(F_GETFL)?
No. Guest kernel keeps track of open flags.
Quoted 7 lines
Quoted 2 lines
If that fails with EEXIST then try
the normal open path (open withO_PATH, fstat, open proc symlink). If
open(O_PATH | O_NOFOLLOW)+fstatat(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW)would indeed allow to filter out anything that isn't a directory and to safely open the proc symlink.
Quoted 5 lines
Quoted 1 line
that fails with ENOENT, then retry the whole thing a certain number of
Indeed someone could have unlinked the file in the meantime, in which case the
open(O_PATH | O_NOFOLLOW)would fail, but if it succeeds then we cannot hit ENOENT anymore AFAICT.
Right.
Quoted 6 lines
Quoted 3 lines
times. If it still fails then somebody is definitely messing with us and we can fail the request with EIO.
Not sure what the retry+timeout is trying to mitigate here... why not returning EIO right away ?
The semantics of O_CREATE are that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.
However if we want to support shared access to a tree then O_CREAT semantics should work even in the face of races due to external modification of the tree. I.e. following race:
virtiofsd: open(foo, O_CREAT | O_EXCL) -> EEXIST other task: unlink(foo)
virtiofsd: open(foo, O_PATH | O_NOFOLLOW) -> ENOENT
To properly support the above the O_CREAT | O_EXCL open would need to be retried.
Thanks,
Miklos
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 3 lines · Greg Kurz
On Wed, 27 Jan 2021 11:34:52 +0100
Miklos Szeredi <[email protected]> wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Another solution specifically for
O_CREATwithoutO_EXCLwould be to turn it into an exclusive create.Would this added
O_EXCLthen appear on the client side, e.g. to guest userspace doingfcntl(F_GETFL)?No. Guest kernel keeps track of open flags.
That was my impression as well as I didn't find a FUSE_GETFL request. Thanks for confirming that !
Quoted 19 lines
Quoted 7 lines
Quoted 2 lines
If that fails with EEXIST then try
the normal open path (open withO_PATH, fstat, open proc symlink). If
open(O_PATH | O_NOFOLLOW)+fstatat(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW)would indeed allow to filter out anything that isn't a directory and to safely open the proc symlink.Quoted 5 lines
Quoted 1 line
that fails with ENOENT, then retry the whole thing a certain number of
Indeed someone could have unlinked the file in the meantime, in which case the
open(O_PATH | O_NOFOLLOW)would fail, but if it succeeds then we cannot hit ENOENT anymore AFAICT.Right.
Quoted 4 lines
Quoted 3 lines
times. If it still fails then somebody is definitely messing with us and we can fail the request with EIO.
Quoted 8 lines
Quoted 2 lines
Not sure what the retry+timeout is trying to mitigate here... why not returning EIO right away ?
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.
Wrong. O_CREAT can legitimately fail with ENOENT if one element of the pathname doesn't exist. And even if pathname only has one element, you can still have O_CREAT to fail the same way if the path of the parent directory is removed.
cat>enoent.c<<EOF
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
mkdir("foo", 0777);
chdir("foo");
rmdir("../foo");
open("bar", O_CREAT);
}EOF
make enoent
strace ./enoent
[...]
mkdir("foo", 0777) = 0
chdir("foo") = 0
rmdir("../foo") = 0openat(AT_FDCWD, "bar", O_RDONLY|O_CREAT, 0117130) = -1 ENOENT (No such file or directory)
Quoted 4 lines
However if we want to support shared access to a tree then
O_CREATsemantics should work even in the face of races due to external modification of the tree. I.e. following race:
Yeah, handling shared access is where the fun starts :)
Quoted 7 lines
virtiofsd:
open(foo, O_CREAT | O_EXCL)-> EEXIST other task: unlink(foo)
virtiofsd:open(foo, O_PATH | O_NOFOLLOW)-> ENOENTTo properly support the above the
O_CREAT|O_EXCLopen would need to be retried.
But in this case, it seems fine to return ENOENT since the guest userspace cannot really assume it never happens.
Quoted 3 lines
Thanks,
Miklos
Quoted 10 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.Wrong.
O_CREATcan legitimately fail with ENOENT if one element
Let me make my statement more precise:
O_CREAT cannot fail with ENOENT if parent directory exists throughout the operation.
I'm sure this property is used all over the place in userspace code, and hence should be supported in strict coherence (cache=none) mode.
For relaxed coherence (cache=auto) I'm not quite sure. NFS is usually the reference, we'd need to look into what guarantees it provides wrt. O_CREAT and remote racing unlink.
Thanks,
Miklos
Quoted 10 lines · On Wed, 27 Jan 2021 16:22:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 9 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.
Quoted 7 lines
Quoted 1 line
Wrong.
O_CREATcan legitimately fail with ENOENT if one elementLet me make my statement more precise:
O_CREATcannot fail with ENOENT if parent directory exists throughout the operation.
True, but I still don't see what guarantees guest userspace that the parent directory doesn't go away... I must have missed something. Please elaborate.
Quoted 10 lines
I'm sure this property is used all over the place in userspace code, and hence should be supported in strict coherence (cache=none) mode.
For relaxed coherence (cache=auto) I'm not quite sure. NFS is usually the reference, we'd need to look into what guarantees it provides wrt.
O_CREATand remote racing unlink.Thanks,
Miklos
Quoted 18 lines · Greg Kurz
Quoted 14 lines · On Wed, 27 Jan 2021 16:22:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 10 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.Wrong.
O_CREATcan legitimately fail with ENOENT if one elementLet me make my statement more precise:
Quoted 7 lines
Quoted 3 lines
O_CREATcannot fail with ENOENT if parent directory exists throughout the operation.True, but I still don't see what guarantees guest userspace that the parent directory doesn't go away... I must have missed something. Please elaborate.
Generally there's no guarantee, however there can be certain situations where the caller can indeed rely on the existence of the parent (e.g. /tmp).
Thanks,
Miklos
Quoted 24 lines · Miklos Szeredi
Quoted 22 lines · Greg Kurz
Quoted 17 lines · On Wed, 27 Jan 2021 16:22:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 10 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.Wrong.
O_CREATcan legitimately fail with ENOENT if one elementLet me make my statement more precise:
O_CREATcannot fail with ENOENT if parent directory exists throughout the operation.
Quoted 7 lines
Quoted 3 lines
True, but I still don't see what guarantees guest userspace that the parent directory doesn't go away... I must have missed something. Please elaborate.
Generally there's no guarantee, however there can be certain situations where the caller can indeed rely on the existence of the parent (e.g. /tmp).
Example from the virtiofs repo:
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L315
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L382
In that case breaking O_CREAT would be harmless, since no two instances are allowed anyway, so it would just give a confusing error. But it is breakage and it probably wouldn't be hard to find much worse breakage in real life applications.
Thanks,
Miklos
Quoted 35 lines · On Wed, 27 Jan 2021 16:52:56 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 24 lines · Miklos Szeredi
Quoted 22 lines · Greg Kurz
Quoted 17 lines · On Wed, 27 Jan 2021 16:22:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 10 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.Wrong.
O_CREATcan legitimately fail with ENOENT if one elementLet me make my statement more precise:
O_CREATcannot fail with ENOENT if parent directory exists throughout the operation.Quoted 7 lines
Quoted 3 lines
True, but I still don't see what guarantees guest userspace that the parent directory doesn't go away... I must have missed something. Please elaborate.
Generally there's no guarantee, however there can be certain situations where the caller can indeed rely on the existence of the parent (e.g. /tmp).
Example from the virtiofs repo:
Quoted 8 lines
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L315
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L382In that case breaking
O_CREATwould be harmless, since no two instances are allowed anyway, so it would just give a confusing error. But it is breakage and it probably wouldn't be hard to find much worse breakage in real life applications.
Ok, I get your point : a guest userspace application can't expect to hit ENOENT when doing open("/some_dir/foo", O_CREAT) even if someone else is doing unlink("/some_dir/foo"), which is a different case than somebody doing rmdir("/some_dir").
But we still have a TOCTOU : the open(O_CREAT|O_EXCL) acts as the check to use open(O_PATH) and retry+timeout can't fix it reliably.
A possible fix for the case where the race comes from the client itself would be to serialize FUSE requests that create/remove paths in the same directory. I don't know enough the code yet to assess if it's doable though.
Then this would leave the case where something else on the host is racing with virtiofsd. IMHO this belongs to the broader family of "bad things the host can do in our back". This requires a bigger hammer than adding band-aids here and there IMHO. So in the scope of this patch, I don't think we should retry and timeout, but just return whatever errno that makes sense.
Quoted 3 lines
Thanks,
Miklos
Quoted 48 lines · Greg Kurz
Quoted 35 lines · On Wed, 27 Jan 2021 16:52:56 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 24 lines · Miklos Szeredi
Quoted 22 lines · Greg Kurz
Quoted 17 lines · On Wed, 27 Jan 2021 16:22:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 10 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.Wrong.
O_CREATcan legitimately fail with ENOENT if one elementLet me make my statement more precise:
O_CREATcannot fail with ENOENT if parent directory exists throughout the operation.Quoted 7 lines
Quoted 3 lines
True, but I still don't see what guarantees guest userspace that the parent directory doesn't go away... I must have missed something. Please elaborate.
Generally there's no guarantee, however there can be certain situations where the caller can indeed rely on the existence of the parent (e.g. /tmp).
Example from the virtiofs repo:
Quoted 8 lines
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L315
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L382In that case breaking
O_CREATwould be harmless, since no two instances are allowed anyway, so it would just give a confusing error. But it is breakage and it probably wouldn't be hard to find much worse breakage in real life applications.
Quoted 8 lines
Ok, I get your point : a guest userspace application can't expect to hit ENOENT when doing
open("/some_dir/foo", O_CREAT)even if someone else is doingunlink("/some_dir/foo"), which is a different case than somebody doingrmdir("/some_dir").But we still have a TOCTOU : the
open(O_CREAT|O_EXCL)acts as the check to useopen(O_PATH)and retry+timeout can't fix it reliably.
Right.
Quoted 5 lines
A possible fix for the case where the race comes from the client itself would be to serialize FUSE requests that create/remove paths in the same directory. I don't know enough the code yet to assess if it's doable though.
Quoted 8 lines
Then this would leave the case where something else on the host is racing with virtiofsd. IMHO this belongs to the broader family of "bad things the host can do in our back". This requires a bigger hammer than adding band-aids here and there IMHO. So in the scope of this patch, I don't think we should retry and timeout, but just return whatever errno that makes sense.
I never suggested a timeout, that would indeed be nonsense.
Just do a simple retry loop with a counter. I'd set counter to a small number (5 or whatever), so that basically any accidental races are cared for, and the only case that would trigger the EIO is if the file was constantly removed and recreated (and even in that case it would be pretty difficult to trigger). This would add only minimal complexity or overhead.
The proper solution might be adding O_REGULAR, and it actually would be useful for other O_CREAT users, since it's probably what they want anyway (but existing semantics can't be changed).
Thanks,
Miklos
Quoted 74 lines · On Thu, 28 Jan 2021 15:00:58 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 48 lines · Greg Kurz
Quoted 35 lines · On Wed, 27 Jan 2021 16:52:56 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 24 lines · Miklos Szeredi
Quoted 22 lines · Greg Kurz
Quoted 17 lines · On Wed, 27 Jan 2021 16:22:49 +0100 Miklos Szeredi <[email protected]> wrote:
Quoted 10 lines · Greg Kurz
Quoted 5 lines · On Wed, 27 Jan 2021 15:09:50 +0100 Miklos Szeredi <[email protected]> wrote:
The semantics of
O_CREATEare that it can fail neither because the file exists nor because it doesn't. This doesn't matter if the exported tree is not modified outside of a single guest, because of locking provided by the guest kernel.Wrong.
O_CREATcan legitimately fail with ENOENT if one elementLet me make my statement more precise:
O_CREATcannot fail with ENOENT if parent directory exists throughout the operation.Quoted 7 lines
Quoted 3 lines
True, but I still don't see what guarantees guest userspace that the parent directory doesn't go away... I must have missed something. Please elaborate.
Generally there's no guarantee, however there can be certain situations where the caller can indeed rely on the existence of the parent (e.g. /tmp).
Example from the virtiofs repo:
Quoted 8 lines
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L315
https://gitlab.com/virtio-fs/ireg/-/blob/master/ireg.c#L382In that case breaking
O_CREATwould be harmless, since no two instances are allowed anyway, so it would just give a confusing error. But it is breakage and it probably wouldn't be hard to find much worse breakage in real life applications.Quoted 8 lines
Ok, I get your point : a guest userspace application can't expect to hit ENOENT when doing
open("/some_dir/foo", O_CREAT)even if someone else is doingunlink("/some_dir/foo"), which is a different case than somebody doingrmdir("/some_dir").But we still have a TOCTOU : the
open(O_CREAT|O_EXCL)acts as the check to useopen(O_PATH)and retry+timeout can't fix it reliably.Right.
Quoted 5 lines
A possible fix for the case where the race comes from the client itself would be to serialize FUSE requests that create/remove paths in the same directory. I don't know enough the code yet to assess if it's doable though.
Quoted 8 lines
Then this would leave the case where something else on the host is racing with virtiofsd. IMHO this belongs to the broader family of "bad things the host can do in our back". This requires a bigger hammer than adding band-aids here and there IMHO. So in the scope of this patch, I don't think we should retry and timeout, but just return whatever errno that makes sense.
Quoted 2 lines
I never suggested a timeout, that would indeed be nonsense.
Yeah sorry for that, by timeout I was lazily expressing "retry a bit and bail out if it doesn't work".
Quoted 7 lines
Just do a simple retry loop with a counter. I'd set counter to a small number (5 or whatever), so that basically any accidental races are cared for, and the only case that would trigger the EIO is if the file was constantly removed and recreated (and even in that case it would be pretty difficult to trigger). This would add only minimal complexity or overhead.
I still don't like the counter thing very much but I can't think of anything better that _works_ in all cases in the short term... so be it.
Quoted 4 lines
The proper solution might be adding
O_REGULAR, and it actually would be useful for otherO_CREATusers, since it's probably what they want anyway (but existing semantics can't be changed).
Yeah only the kernel can handle this race gracefully and O_REGULAR would be great, but it might take some time until this percolates up to QEMU.
Quoted 3 lines
Thanks,
Miklos
Quoted 31 lines · Greg Kurz
Quoted 8 lines · On Tue, 26 Jan 2021 10:35:02 +0000 Stefan Hajnoczi <[email protected]> wrote:
A well-behaved FUSE client does not attempt to open special files with
FUSE_OPENbecause they are handled on the client side (e.g. device nodes are handled by client-side device drivers).The check to prevent virtiofsd from opening special files is missing in a few cases, most notably
FUSE_OPEN. A malicious client can cause virtiofsd to open a device node, potentially allowing the guest to escape.or pretty much anything nasty you can think of, e.g. DoS if the malicious client repeatedly asks virtiofsd to open FIFOs the other side of which is never opened.
Quoted 8 lines
This can be exploited by a modified guest device driver. It is not exploitable from guest userspace since the guest kernel will handle special files inside the guest instead of sending FUSE requests.
This patch adds the missing checks to virtiofsd. This is a short-term solution because it does not prevent a compromised virtiofsd process from opening device nodes on the host.
Quoted 6 lines
Reported-by Alex Xu <[email protected]>Fixes CVE-2020-35517Reviewed-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Vivek Goyal <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
Quoted 8 lines
The patch looks pretty good to me. It just seems to be missing a change in
lo_create():fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);A malicious guest could have created anything called ${name} in this directory before calling
FUSE_CREATEand we'll open it blindly, or I'm missing something ?
Good point! I will send another revision.
Stefan