Stefan Hajnoczi [Wed, 31 Jul 2019 16:10:06 +0000 (17:10 +0100)]
virtiofsd: introduce inode refcount to prevent use-after-free
If thread A is using an inode it must not be deleted by thread B when
processing a FUSE_FORGET request.
The FUSE protocol itself already has a counter called nlookup that is
used in FUSE_FORGET messages. We cannot trust this counter since the
untrusted client can manipulate it via FUSE_FORGET messages.
Introduce a new refcount to keep inodes alive for the required lifespan.
lo_inode_put() must be called to release a reference. FUSE's nlookup
counter holds exactly one reference so that the inode stays alive as
long as the client still wants to remember it.
Note that the lo_inode->is_symlink field is moved to avoid creating a
hole in the struct due to struct field alignment.
If a new setmemtable command comes in once the vhost threads are
running, it will remap the guests address space and the threads
will now be looking in the wrong place.
Fortunately we're running this command under lock, so we can
update the queue mappings so that threads will look in the new-right
place.
Note: This doesn't fix things that the threads might be doing
without a lock (e.g. a readv/writev!) That's for another time.
Stefan Hajnoczi [Wed, 31 Jul 2019 16:10:04 +0000 (17:10 +0100)]
virtiofsd: rename inode->refcount to inode->nlookup
This reference counter plays a specific role in the FUSE protocol. It's
not a generic object reference counter and the FUSE kernel code calls it
"nlookup".
Stefan Hajnoczi [Fri, 26 Jul 2019 09:11:01 +0000 (10:11 +0100)]
virtiofsd: make lo_release() atomic
Hold the lock across both lo_map_get() and lo_map_remove() to prevent
races between two FUSE_RELEASE requests. In this case I don't see a
serious bug but it's safer to do things atomically.
Stefan Hajnoczi [Wed, 17 Jul 2019 15:05:57 +0000 (16:05 +0100)]
virtiofsd: prevent fv_queue_thread() vs virtio_loop() races
We call into libvhost-user from the virtqueue handler thread and the
vhost-user message processing thread without a lock. There is nothing
protecting the virtqueue handler thread if the vhost-user message
processing thread changes the virtqueue or memory table while it is
running.
This patch introduces a read-write lock. Virtqueue handler threads are
readers. The vhost-user message processing thread is a writer. This
will allow concurrency for multiqueue in the future while protecting
against fv_queue_thread() vs virtio_loop() races.
Note that the critical sections could be made smaller but it would be
more invasive and require libvhost-user changes. Let's start simple and
improve performance later, if necessary. Another option would be an
RCU-style approach with lighter-weight primitives.
Vivek Goyal [Mon, 10 Jun 2019 19:22:06 +0000 (15:22 -0400)]
virtiofsd: Support remote posix locks
Doing posix locks with-in guest kernel are not sufficient if a file/dir
is being shared by multiple guests. So we need the notion of daemon doing
the locks which are visible to rest of the guests.
Given posix locks are per process, one can not call posix lock API on host,
otherwise bunch of basic posix locks properties are broken. For example,
If two processes (A and B) in guest open the file and take locks on different
sections of file, if one of the processes closes the fd, it will close
fd on virtiofsd and all posix locks on file will go away. This means if
process A closes the fd, then locks of process B will go away too.
Similar other problems exist too.
This patch set tries to emulate posix locks while using open file
description locks provided on Linux.
Daemon provides two options (-o posix_lock, -o no_posix_lock) to enable
or disable posix locking in daemon. By default it is enabled.
There are few issues though.
- GETLK() returns pid of process holding lock. As we are emulating locks
using OFD, and these locks are not per process and don't return pid
of process, so GETLK() in guest does not reuturn process pid.
- As of now only F_SETLK is supported and not F_SETLKW. We can't block
the thread in virtiofsd for arbitrary long duration as there is only
one thread serving the queue. That means unlock request will not make
it to daemon and F_SETLKW will block infinitely and bring virtio-fs
to a halt. This is a solvable problem though and will require significant
changes in virtiofsd and kernel. Left as a TODO item for now.
The user will get confused about the situation and maybe the cause of the
unexpected problem. So it's better to prevent the multiple running.
Create a regular file under localstatedir directory to exclude the
vhost_user_socket. To create and lock the file, use qemu_write_pidfile()
because the API has some sanity checks and file lock.
Miklos Szeredi [Wed, 20 Nov 2019 14:14:29 +0000 (14:14 +0000)]
virtiofsd: fail when parent inode isn't known in lo_do_lookup()
The Linux file handle APIs (struct export_operations) can access inodes
that are not attached to parents because path name traversal is not
performed. Refuse if there is no parent in lo_do_lookup().
contrib/libvhost-user: Protect slave fd with mutex
In future patches we'll be performing commands on the slave-fd driven
by commands on queues, since those queues will be driven by individual
threads we need to make sure they don't attempt to use the slave-fd
for multiple commands in parallel.
virtiofsd: Add ID to the log with FUSE_LOG_DEBUG level
virtiofsd has some threads, so we see a lot of logs with debug option.
It would be useful for debugging if we can identify the specific thread
from the log.
Add ID, which is got by gettid(), to the log with FUSE_LOG_DEBUG level
so that we can grep the specific thread.
Signed-off-by: Masayoshi Mizuma <[email protected]> Signed-off-by: Dr. David Alan Gilbert <[email protected]>
added rework as suggested by Daniel P. Berrangé during review Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Dr. David Alan Gilbert <[email protected]>
Stefan Hajnoczi [Wed, 26 Jun 2019 09:25:54 +0000 (10:25 +0100)]
virtiofsd: add --syslog command-line option
Sometimes collecting output from stderr is inconvenient or does not fit
within the overall logging architecture. Add syslog(3) support for
cases where stderr cannot be used.
Stefan Hajnoczi [Fri, 22 Nov 2019 11:31:30 +0000 (11:31 +0000)]
virtiofsd: fix libfuse information leaks
Some FUSE message replies contain padding fields that are not
initialized by libfuse. This is fine in traditional FUSE applications
because the kernel is trusted. virtiofsd does not trust the guest and
must not expose uninitialized memory.
Use C struct initializers to automatically zero out memory. Not all of
these code changes are strictly necessary but they will prevent future
information leaks if the structs are extended.
Stefan Hajnoczi [Fri, 22 Mar 2019 15:54:13 +0000 (15:54 +0000)]
virtiofsd: set maximum RLIMIT_NOFILE limit
virtiofsd can exceed the default open file descriptor limit easily on
most systems. Take advantage of the fact that it runs as root to raise
the limit.
Vivek Goyal [Tue, 13 Aug 2019 19:29:44 +0000 (15:29 -0400)]
virtiofsd: Drop CAP_FSETID if client asked for it
If client requested killing setuid/setgid bits on file being written, drop
CAP_FSETID capability so that setuid/setgid bits are cleared upon write
automatically.
libcap-ng reads /proc during capng_get_caps_process, and virtiofsd's
sandboxing doesn't have /proc mounted; thus we have to do the
caps read before we sandbox it and save/restore the state.
Stefan Hajnoczi [Wed, 16 Oct 2019 16:01:57 +0000 (17:01 +0100)]
virtiofsd: move to a new pid namespace
virtiofsd needs access to /proc/self/fd. Let's move to a new pid
namespace so that a compromised process cannot see another other
processes running on the system.
One wrinkle in this approach: unshare(CLONE_NEWPID) affects *child*
processes and not the current process. Therefore we need to fork the
pid 1 process that will actually run virtiofsd and leave a parent in
waitpid(2). This is not the same thing as daemonization and parent
processes should not notice a difference.
Stefan Hajnoczi [Thu, 28 Feb 2019 16:38:31 +0000 (16:38 +0000)]
virtiofsd: check input buffer size in fuse_lowlevel.c ops
Each FUSE operation involves parsing the input buffer. Currently the
code assumes the input buffer is large enough for the expected
arguments. This patch uses fuse_mbuf_iter to check the size.
Most operations are simple to convert. Some are more complicated due to
variable-length inputs or different sizes depending on the protocol
version.
Stefan Hajnoczi [Thu, 28 Feb 2019 11:25:32 +0000 (11:25 +0000)]
virtiofsd: validate input buffer sizes in do_write_buf()
There is a small change in behavior: if fuse_write_in->size doesn't
match the input buffer size then the request is failed. Previously
write requests with 1 fuse_buf element would truncate to
fuse_write_in->size.
Stefan Hajnoczi [Thu, 28 Feb 2019 10:30:20 +0000 (10:30 +0000)]
virtiofsd: add fuse_mbuf_iter API
Introduce an API for consuming bytes from a buffer with size checks.
All FUSE operations will be converted to use this safe API instead of
void *inarg.
virtiofsd: Plumb fuse_bufvec through to do_write_buf
Let fuse_session_process_buf_int take a fuse_bufvec * instead of a
fuse_buf; and then through to do_write_buf - where in the best
case it can pass that straight through to op.write_buf without copying
(other than skipping a header).
Stefan Hajnoczi [Tue, 26 Feb 2019 17:58:59 +0000 (17:58 +0000)]
virtiofsd: validate path components
Several FUSE requests contain single path components. A correct FUSE
client sends well-formed path components but there is currently no input
validation in case something went wrong or the client is malicious.
Refuse ".", "..", and paths containing '/' when we expect a path
component.
Miklos Szeredi [Wed, 14 Nov 2018 15:52:03 +0000 (16:52 +0100)]
virtiofsd: passthrough_ll: add fallback for racy ops
We have two operations that cannot be done race-free on a symlink in
certain cases: utimes and link.
Add racy fallback for these if the race-free method doesn't work. We do
our best to avoid races even in this case:
- get absolute path by reading /proc/self/fd/NN symlink
- lookup parent directory: after this we are safe against renames in
ancestors
- lookup name in parent directory, and verify that we got to the original
inode, if not retry the whole thing
Both utimes(2) and link(2) hold i_lock on the inode across the operation,
so a racing rename/delete by this fuse instance is not possible, only from
other entities changing the filesystem.
If the "norace" option is given, then disable the racy fallbacks.
Stefan Hajnoczi [Thu, 31 Jan 2019 04:49:39 +0000 (12:49 +0800)]
virtiofsd: passthrough_ll: add lo_map for ino/fh indirection
A layer of indirection is needed because passthrough_ll cannot expose
pointers or file descriptor numbers to untrusted clients. Malicious
clients could send invalid pointers or file descriptors in order to
crash or exploit the file system daemon.
lo_map provides an integer key->value mapping. This will be used for
ino and fh fields in the patches that follow.
Stefan Hajnoczi [Tue, 27 Aug 2019 09:54:34 +0000 (10:54 +0100)]
virtiofsd: make -f (foreground) the default
According to vhost-user.rst "Backend program conventions", backend
programs should run in the foregound by default. Follow the
conventions so libvirt and other management tools can control virtiofsd
in a standard way.
Stefan Hajnoczi [Tue, 25 Jun 2019 16:18:00 +0000 (17:18 +0100)]
virtiofsd: add --fd=FDNUM fd passing option
Although --socket-path=PATH is useful for manual invocations, management
tools typically create the UNIX domain socket themselves and pass it to
the vhost-user device backend. This way QEMU can be launched
immediately with a valid socket. No waiting for the vhost-user device
backend is required when fd passing is used.
virtiofsd: Add Makefile wiring for virtiofsd contrib
Wire up the building of the virtiofsd in tools.
virtiofsd relies on Linux-specific system calls and seccomp. Anyone
wishing to port it to other host operating systems should do so
carefully and without reducing security.
Keep track of whether we sent a reply to a request; this is a bit
paranoid but it means:
a) We should always recycle an element even if there was an error
in the request
b) Never try and send two replies on one queue element
Vivek Goyal [Thu, 30 Aug 2018 18:22:10 +0000 (14:22 -0400)]
virtiofsd: Make fsync work even if only inode is passed in
If caller has not sent file handle in request, then using inode, retrieve
the fd opened using O_PATH and use that to open file again and issue
fsync. This will be needed when dax_flush() calls fsync. At that time
we only have inode information (and not file).
Xiao Yang [Mon, 13 Jan 2020 09:37:34 +0000 (17:37 +0800)]
vitriofsd/passthrough_ll: fix fallocate() ifdefs
1) Use correct CONFIG_FALLOCATE macro to check if fallocate() is supported.(i.e configure
script sets CONFIG_FALLOCATE intead of HAVE_FALLOCATE if fallocate() is supported)
2) Replace HAVE_POSIX_FALLOCATE with CONFIG_POSIX_FALLOCATE.
Signed-off-by: Xiao Yang <[email protected]> Signed-off-by: Dr. David Alan Gilbert <[email protected]>
Merged from two of Xiao Yang's patches
virtiofsd: Fix common header and define for QEMU builds
All of the fuse files include config.h and define GNU_SOURCE
where we don't have either under our build - remove them.
Fixup path to the kernel's fuse.h in the QEMUs world.
QEMU's compiler enables warnings/errors for ignored values
and the (void) trick used in the fuse code isn't enough.
Turn all the return values into a return value on the function.
Stefan Hajnoczi [Thu, 28 Feb 2019 11:22:58 +0000 (11:22 +0000)]
virtiofsd: remove unused notify reply support
Notify reply support is unused by virtiofsd. The code would need to be
updated to validate input buffer sizes. Remove this unused code since
changes to it are untestable.
Stefan Hajnoczi [Fri, 8 Mar 2019 13:24:31 +0000 (13:24 +0000)]
virtiofsd: remove mountpoint dummy argument
Classic FUSE file system daemons take a mountpoint argument but
virtiofsd exposes a vhost-user UNIX domain socket instead. The
mountpoint argument is not used by virtiofsd but the user is still
required to pass a dummy argument on the command-line.
Remove the mountpoint argument to clean up the command-line.
passthrough_ll is one of the examples in the upstream fuse project
and is the main part of our daemon here. It passes through requests
from fuse to the underlying filesystem, using syscalls as directly
as possible.
From libfuse fuse-3.8.0
Signed-off-by: Dr. David Alan Gilbert <[email protected]>
Fixed up 'GPL' to 'GPLv2' as per Dan's comments and consistent
with the 'LICENSE' file in libfuse; patch sent to libfuse to fix
it upstream. Reviewed-by: Daniel P. Berrangé <[email protected]> Signed-off-by: Dr. David Alan Gilbert <[email protected]>
fuse_lowlevel is one of the largest files from the library
and does most of the work. Add it separately to keep the diff
sizes small.
Again this is from upstream fuse-3.8.0
* remotes/vivier2/tags/linux-user-for-5.0-pull-request:
linux-user: Add support for read/clear RTC voltage low detector using ioctls
linux-user: Add support for getting/setting RTC PLL correction using ioctls
linux-user: Add support for getting/setting RTC wakeup alarm using ioctls
linux-user: Add support for getting/setting RTC periodic interrupt and epoch using ioctls
linux-user: Add support for getting/setting RTC time and alarm using ioctls
linux-user: Add support for enabling/disabling RTC features using ioctls
linux-user: Add support for TYPE_LONG and TYPE_ULONG in do_ioctl()
linux-user: Add support for KCOV_INIT_TRACE ioctl
linux-user: Add support for KCOV_<ENABLE|DISABLE> ioctls
configure: Detect kcov support and introduce CONFIG_KCOV
linux-user: Add support for FDFMT<BEG|TRK|END> ioctls
linux-user: Add support for FD<SETEMSGTRESH|SETMAXERRS|GETMAXERRS> ioctls
linux-user: Add support for FS_IOC32_<GET|SET>VERSION ioctls
linux-user: Add support for FS_IOC32_<GET|SET>FLAGS ioctls
linux-user: Add support for FS_IOC_<GET|SET>VERSION ioctls
linux-user: Reserve space for brk
linux-user:Fix align mistake when mmap guest space
Peter Maydell [Thu, 23 Jan 2020 13:01:14 +0000 (13:01 +0000)]
Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20200121' into staging
Remove another limit to NB_MMU_MODES.
Fix compilation using uclibc.
Fix defaulting of -accel parameters.
Tidy cputlb basic routines.
Adjust git.orderfile for decodetree.
# gpg: Signature made Wed 22 Jan 2020 02:44:18 GMT
# gpg: using RSA key 7A481E78868B4DB6A85A05C064DF38E8AF7E215F
# gpg: issuer "[email protected]"
# gpg: Good signature from "Richard Henderson <[email protected]>" [full]
# Primary key fingerprint: 7A48 1E78 868B 4DB6 A85A 05C0 64DF 38E8 AF7E 215F
* remotes/rth/tags/pull-tcg-20200121:
scripts/git.orderfile: Display decodetree before C source
cputlb: Hoist timestamp outside of loops over tlbs
cputlb: Initialize tlbs as flushed
cputlb: Partially merge tlb_dyn_init into tlb_init
cputlb: Split out tlb_mmu_flush_locked
cputlb: Hoist tlb portions in tlb_flush_one_mmuidx_locked
cputlb: Hoist tlb portions in tlb_mmu_resize_locked
cputlb: Pass CPUTLBDescFast to tlb_n_entries and sizeof_tlb
cputlb: Make tlb_n_entries private to cputlb.c
cputlb: Merge tlb_table_flush_by_mmuidx into tlb_flush_one_mmuidx_locked
vl: Only choose enabled accelerators in configure_accelerators
vl: Remove useless test in configure_accelerators
vl: Reduce scope of variables in configure_accelerators
vl: Remove unused variable in configure_accelerators
util/cacheinfo: fix crash when compiling with uClibc
cputlb: Handle NB_MMU_MODES > TARGET_PAGE_BITS_MIN
Filip Bozuta [Wed, 15 Jan 2020 19:36:40 +0000 (20:36 +0100)]
linux-user: Add support for read/clear RTC voltage low detector using ioctls
This patch implements functionalities of following ioctls:
RTC_VL_READ - Read voltage low detection information
Read the voltage low for RTCs that support voltage low.
The third ioctl's' argument points to an int in which
the voltage low is returned.
RTC_VL_CLR - Clear voltage low information
Clear the information about voltage low for RTCs that
support voltage low. The third ioctl(2) argument is
ignored.
Implementation notes:
Since one ioctl has a pointer to 'int' as its third agrument,
and another ioctl has NULL as its third argument, their
implementation was straightforward.