virtiofsd: sync FUSE_DESTROY with session destroy
PATCH 1/1 virtiofsd: sync FUSE_DESTROY with session destroy
Normally on guest shutdown, guest umounts virtiofs (FUSE_DESTROY) and waits for umount to return then cleans up virtio device.
But if guest gets killed, for whatever reason, right after sending FUSE_DESTROY, it's possible that virtiofsd cleans up session and virtio dev first, then fv_queue_worker() wants to send reply to guest and hits abort() in fuse_send_msg(), because fuse_lowlevel_is_virtio() returns false.
Fix it by acquiring se->init_rwlock first in fuse_session_destroy() to wait for inflight FUSE_DESTROY to be completed, then clean up session and virtio dev.
--- tools/virtiofsd/fuse_lowlevel.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/virtiofsd/fuse_lowlevel.c +7 -0
@@ -2660,6 +2660,13 @@ void fuse_session_destroy(struct fuse_session *se) se->op.destroy (se->userdata, se); } } + +/* +* Wait for inflight FUSE_DESTROY to be completed before cleaning up +* session and virtio dev. +*/ +pthread_rwlock_wrlock (&se->init_rwlock); +pthread_rwlock_destroy (&se->init_rwlock);pthread_mutex_destroy (&se->lock);free (se->cuse_data);
Signature · 1 line
2.14.4.44.g2045bb6
Quoted 9 lines · Eryu Guan
Normally on guest shutdown, guest umounts virtiofs (
FUSE_DESTROY) and waits for umount to return then cleans up virtio device.But if guest gets killed, for whatever reason, right after sending
FUSE_DESTROY, it's possible that virtiofsd cleans up session and virtio dev first, thenfv_queue_worker()wants to send reply to guest and hitsabort()infuse_send_msg(), becausefuse_lowlevel_is_virtio()returns false.
Quoted 6 lines
Fix it by acquiring
se->init_rwlockfirst infuse_session_destroy()to wait for inflightFUSE_DESTROYto be completed, then clean up session and virtio dev.Reported-by Qingming Su <[email protected]>Signed-off-by Eryu Guan <[email protected]>
Please ignore it for now, use-after-free is still possible. We need to make sure all fv_queue_thread()s and fv_queue_worker()s exit before virtio_loop() returns. I'll send a v2 patch.
Thanks,
Eryu