Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2745850imj; Mon, 11 Feb 2019 07:52:58 -0800 (PST) X-Google-Smtp-Source: AHgI3IaztlTH1lz1nPS33/uvgxY11jYV0s8Lnx4yeWTSZprXpc1eSXQSN2vX279WY6E03y5hPGb+ X-Received: by 2002:a17:902:a58c:: with SMTP id az12mr37023776plb.299.1549900378005; Mon, 11 Feb 2019 07:52:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549900377; cv=none; d=google.com; s=arc-20160816; b=l/hsHoBKzcB0NGqZa6ViDcb+NUGyiC1HpPMqAsRgoKT5ho0nHyUKyLjK0TcbfdtNbF sVBXH+AHWapO2/KTVBMGu4Mxr7rw47JMXLBWEFR+Qr+fW1dnchYVC4PsWvGqQnZdi3uS 0YHCZbMcHnYUbvULIo+jrnOyTiD5Nn2FwwdYZh7x8K9h/4TgW1eHpbCqObvzHtAVSU2g +76/fVWbOISQUHoEtI4M5epb27PmVwGkHV54Cns1PNM7EIQjPcrJ7oBughc47WYcMw1U 8ApW626uJwjHDA3VF8/v/SEHSCa3mWbojdnrWiWDCr6GEyWaoZZBef5ChvenqcrhExmK nAfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IZbadsLZFlouwkaxo5Sm3OGOKw2PL6xql2IrVSKVIEI=; b=gAqgQRwi8UacPirC5R8rkUumCjjuxELmSqAlQiXwob1suNKYDam5/qCegvGlZ0+bgR ly4xd7n5B89HKrgaY9Tsu3PP2yYH6wTAxInWaXs9t5JR91pghhthJoAgIWlKBBUwKUDd X5yxHpBhWbIzaOE0PtTxo2RHK1L194rhjwoHWQ4lKA0Fl2XJIR+muCjLNc8wbOM1acoE JMPicszDszt+LBbzLLk3WcKYMVbu9zt6LV8YuFYbR9+vxRLpZ+KqAmcMuT59j1wkKFr4 S5Ti/YuxreDClNXSmvtvioVly9len6Q2tKwimMgV11L8oTPHp4kVNSdB7tmgnpI4rJHq /iEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="hpfQHS/g"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w16si10238640pga.328.2019.02.11.07.52.41; Mon, 11 Feb 2019 07:52:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="hpfQHS/g"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731533AbfBKOht (ORCPT + 99 others); Mon, 11 Feb 2019 09:37:49 -0500 Received: from mail.kernel.org ([198.145.29.99]:47166 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728293AbfBKOhl (ORCPT ); Mon, 11 Feb 2019 09:37:41 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 70BE220844; Mon, 11 Feb 2019 14:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549895860; bh=XYXwHbHQdlFxc/nuF8m+XXFMhoV7LspRczpEizgzp+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hpfQHS/g/LtuBbb432YB/7GPwxnoxlZsYmEUO/Yr33sAXfQLTmzcjGJEe11rcEVCS GfTnj94GXimb8qs45YZ5I8MD1nUt8rTLaOTYxKW/eq7Y5EP32RAsZDQedkJyNVRpEP gLRCVH5KySBHKwqa7UwXBXIB97r2Kw6levHqzZZ8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, Jann Horn , Paolo Bonzini Subject: [PATCH 4.20 340/352] kvm: fix kvm_ioctl_create_device() reference counting (CVE-2019-6974) Date: Mon, 11 Feb 2019 15:19:27 +0100 Message-Id: <20190211141908.813141047@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn commit cfa39381173d5f969daf43582c95ad679189cbc9 upstream. kvm_ioctl_create_device() does the following: 1. creates a device that holds a reference to the VM object (with a borrowed reference, the VM's refcount has not been bumped yet) 2. initializes the device 3. transfers the reference to the device to the caller's file descriptor table 4. calls kvm_get_kvm() to turn the borrowed reference to the VM into a real reference The ownership transfer in step 3 must not happen before the reference to the VM becomes a proper, non-borrowed reference, which only happens in step 4. After step 3, an attacker can close the file descriptor and drop the borrowed reference, which can cause the refcount of the kvm object to drop to zero. This means that we need to grab a reference for the device before anon_inode_getfd(), otherwise the VM can disappear from under us. Fixes: 852b6d57dc7f ("kvm: add device control API") Cc: stable@kernel.org Signed-off-by: Jann Horn Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2919,8 +2919,10 @@ static int kvm_ioctl_create_device(struc if (ops->init) ops->init(dev); + kvm_get_kvm(kvm); ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC); if (ret < 0) { + kvm_put_kvm(kvm); mutex_lock(&kvm->lock); list_del(&dev->vm_node); mutex_unlock(&kvm->lock); @@ -2928,7 +2930,6 @@ static int kvm_ioctl_create_device(struc return ret; } - kvm_get_kvm(kvm); cd->fd = ret; return 0; }