Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp363432pxf; Thu, 11 Mar 2021 05:54:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJyboM0VE5V4Cq5Fdlkr+p9X5CuV3PKO024mqbip5W0J3wFDzcNQssxJrbP8MN/fuz4YtFXw X-Received: by 2002:a17:906:684b:: with SMTP id a11mr3093582ejs.329.1615470882808; Thu, 11 Mar 2021 05:54:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615470882; cv=none; d=google.com; s=arc-20160816; b=efPF3HnNaMqENLZmOsSuuPSXR77xjdx8yzhOdWuOY/xkrNn/uBYuweu6wlFqSvlk7h uqNkuBl36MWU6sYXwUYMPYWUM3PeJfig4Q1aTYXFM47VcKcuQHm6AEffbZLmeYiilZps PppJsRpxXP8GljHh4RvoOE+Qmx3bGz01ZiyOclp/bEjsTYQudfJlT50ds6zg2NC1F/tB zuHHzINylVcGxJNL7SCHR4AnNh/wz8EgaOchkaYk93/dPnQPhuxj9CG3xjiUzRbEiLMw UrTJCfzUSadIstJGmDAZFci+eYnJjPsiPq80Pw6VzYG1pGYSFxB1kQtNQpQsiT2/Wrae UTNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fU0jQeEMLvNYK1r/S5O2kpHjVv1Rt/n95eYPSXJ6zNc=; b=LWz5ANQ1PZwQ/4wO+pZYyTMlaSy+Ei+E7lFUsLln7qclmHbI3712eX3fk+cUlnZNaF Od4TQVIN7cF5QV32oIousxXr9FGHEFMJT8nSndMW7I7IhSM/AiP31VQO5pwLbBNEAehP 4BtsT7xeUTppXTZnzsgq9EXJk6KulnbwF3BFMw/HJLRk9gz6SxAmJBHcog8cK0E0uwGA XHb8htcl2nnxIPWU4AkCApQY00SMK2s6QycafHVRDZEI6b3U74i2AoVddpJ1dRa/n3ev 6ftijMVwWotsmxOK3OgwpEMRco95SdkjyZDuw5F+3bkTwyOhUL81dq/ODDLGWerXMv7J 5OGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Nbuf0tVK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f12si1915037ejx.75.2021.03.11.05.54.20; Thu, 11 Mar 2021 05:54:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Nbuf0tVK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233809AbhCKNxZ (ORCPT + 99 others); Thu, 11 Mar 2021 08:53:25 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:28103 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233805AbhCKNxJ (ORCPT ); Thu, 11 Mar 2021 08:53:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615470788; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fU0jQeEMLvNYK1r/S5O2kpHjVv1Rt/n95eYPSXJ6zNc=; b=Nbuf0tVK8E2IZkWLkoJ+ep8e5Wu+gc7294bPGqd4yii9k2QrBRpvaxtQXNogF2APHf/BH/ EgxF1ZHtajvPuTTHxm11MLjb+7KtP/7mgiPceceABZRdfGuM0bxwqgK0oKOU1DO0d7/Y91 gaZp8/24MdamOXkZ1vrLpDE4u52xS/s= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-306-VpN4r4CWMkiQ9mULivpb2w-1; Thu, 11 Mar 2021 08:53:07 -0500 X-MC-Unique: VpN4r4CWMkiQ9mULivpb2w-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D1A3B83DBE2; Thu, 11 Mar 2021 13:53:05 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-113-146.ams2.redhat.com [10.36.113.146]) by smtp.corp.redhat.com (Postfix) with ESMTP id 213AF196E3; Thu, 11 Mar 2021 13:53:03 +0000 (UTC) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Jason Wang , netdev@vger.kernel.org, Zhu Lingshan , "Michael S. Tsirkin" , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 1/2] vhost-vdpa: fix use-after-free of v->config_ctx Date: Thu, 11 Mar 2021 14:52:56 +0100 Message-Id: <20210311135257.109460-2-sgarzare@redhat.com> In-Reply-To: <20210311135257.109460-1-sgarzare@redhat.com> References: <20210311135257.109460-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the 'v->config_ctx' eventfd_ctx reference is released we didn't set it to NULL. So if the same character device (e.g. /dev/vhost-vdpa-0) is re-opened, the 'v->config_ctx' is invalid and calling again vhost_vdpa_config_put() causes use-after-free issues like the following refcount_t underflow: refcount_t: underflow; use-after-free. WARNING: CPU: 2 PID: 872 at lib/refcount.c:28 refcount_warn_saturate+0xae/0xf0 RIP: 0010:refcount_warn_saturate+0xae/0xf0 Call Trace: eventfd_ctx_put+0x5b/0x70 vhost_vdpa_release+0xcd/0x150 [vhost_vdpa] __fput+0x8e/0x240 ____fput+0xe/0x10 task_work_run+0x66/0xa0 exit_to_user_mode_prepare+0x118/0x120 syscall_exit_to_user_mode+0x21/0x50 ? __x64_sys_close+0x12/0x40 do_syscall_64+0x45/0x50 entry_SYSCALL_64_after_hwframe+0x44/0xae Fixes: 776f395004d8 ("vhost_vdpa: Support config interrupt in vdpa") Cc: lingshan.zhu@intel.com Cc: stable@vger.kernel.org Signed-off-by: Stefano Garzarella --- drivers/vhost/vdpa.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index ef688c8c0e0e..00796e4ecfdf 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -308,8 +308,10 @@ static long vhost_vdpa_get_vring_num(struct vhost_vdpa *v, u16 __user *argp) static void vhost_vdpa_config_put(struct vhost_vdpa *v) { - if (v->config_ctx) + if (v->config_ctx) { eventfd_ctx_put(v->config_ctx); + v->config_ctx = NULL; + } } static long vhost_vdpa_set_config_call(struct vhost_vdpa *v, u32 __user *argp) -- 2.29.2