From patchwork Sun Dec 17 23:40:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 10118277 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2FD0960390 for ; Mon, 18 Dec 2017 05:48:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 176DB28ECA for ; Mon, 18 Dec 2017 05:48:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 08D5B28DA2; Mon, 18 Dec 2017 05:48:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.4 required=2.0 tests=BAYES_00, DATE_IN_PAST_06_12, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F49128DA2 for ; Mon, 18 Dec 2017 05:48:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750903AbdLRFsS (ORCPT ); Mon, 18 Dec 2017 00:48:18 -0500 Received: from mga09.intel.com ([134.134.136.24]:4687 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750727AbdLRFsR (ORCPT ); Mon, 18 Dec 2017 00:48:17 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Dec 2017 21:48:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,420,1508828400"; d="scan'208";a="3350228" Received: from sky-ws.sh.intel.com (HELO localhost) ([10.239.48.141]) by fmsmga008.fm.intel.com with ESMTP; 17 Dec 2017 21:48:15 -0800 From: Lan Tianyu Cc: Lan Tianyu , pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, dvyukov@google.com, kernellwp@gmail.com Subject: [PATCH] KVM/Eventfd: Avoid crash when assign and deassign same eventfd in parallel. Date: Sun, 17 Dec 2017 18:40:07 -0500 Message-Id: <1513554007-12302-1-git-send-email-tianyu.lan@intel.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Syzroot reports crash in kvm_irqfd_assign() is caused by use-after-free. Because kvm_irqfd_assign() and kvm_irqfd_deassign() can't run in parallel for same eventfd. When assign path hasn't been finished after irqfd has been added to kvm->irqfds.items list, another thead may deassign the eventfd and free struct kvm_kernel_irqfd(). This causes assign path still uses struct kvm_kernel_irqfd freed by deassign path. To avoid such issue, add "initialized" flag in the struct kvm_kernel_irqfd and check the flag before deactivating irqfd. If irqfd is still in initialization, deassign path return fault. Reported-by: Dmitry Vyukov Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Dmitry Vyukov Cc: Wanpeng Li Signed-off-by: Tianyu Lan --- include/linux/kvm_irqfd.h | 1 + virt/kvm/eventfd.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/kvm_irqfd.h b/include/linux/kvm_irqfd.h index 76c2fbc..be6b254 100644 --- a/include/linux/kvm_irqfd.h +++ b/include/linux/kvm_irqfd.h @@ -66,6 +66,7 @@ struct kvm_kernel_irqfd { struct work_struct shutdown; struct irq_bypass_consumer consumer; struct irq_bypass_producer *producer; + u8 initialized:1; }; #endif /* __LINUX_KVM_IRQFD_H */ diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index a334399..80f06e6 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -421,6 +421,7 @@ int __attribute__((weak)) kvm_arch_update_irqfd_routing( } #endif + irqfd->initialized = 1; return 0; fail: @@ -525,6 +526,7 @@ void kvm_unregister_irq_ack_notifier(struct kvm *kvm, { struct kvm_kernel_irqfd *irqfd, *tmp; struct eventfd_ctx *eventfd; + int ret = 0; eventfd = eventfd_ctx_fdget(args->fd); if (IS_ERR(eventfd)) @@ -543,7 +545,12 @@ void kvm_unregister_irq_ack_notifier(struct kvm *kvm, write_seqcount_begin(&irqfd->irq_entry_sc); irqfd->irq_entry.type = 0; write_seqcount_end(&irqfd->irq_entry_sc); - irqfd_deactivate(irqfd); + + if (irqfd->initialized) + irqfd_deactivate(irqfd); + else + ret = -EFAULT; + } } @@ -557,7 +564,7 @@ void kvm_unregister_irq_ack_notifier(struct kvm *kvm, */ flush_workqueue(irqfd_cleanup_wq); - return 0; + return ret; } int