From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09D3FC433EF for ; Tue, 14 Jun 2022 02:32:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355605AbiFNCcg (ORCPT ); Mon, 13 Jun 2022 22:32:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356718AbiFNCb1 (ORCPT ); Mon, 13 Jun 2022 22:31:27 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87A724476E; Mon, 13 Jun 2022 19:12:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A708DB8169F; Tue, 14 Jun 2022 02:11:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96AC0C36AFF; Tue, 14 Jun 2022 02:11:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655172700; bh=C7mABmTJjJjFAL9xAHiCDoEkCJ2cTkj1vecB2YNbIaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=luFT2HMOXUA74hcvKJhxYc6aYShWN9vGJWB1sC7Z/OSAdRGgciqgDORCHgykE52un gRO2dsysQRUu1Lgu/qbqGrSRhVCtE6mIhm0KBHgu5mdFVg73rURszhIOA1bk+DZH0g hVytUmQMN+X2NgiUy8b0sVn0+AJXQTNfbNa2PhgCJxXitsW6vm0o6e7HAHWzW20v2D HP9f0624YDrtezkE4+0X1jc1xeSGqNYOftfs1nzJpfrUIiPM2/7pZnt/ZHBPmM5AB1 Vy94FY5STgNYdian6w5X9pqru+qZvqknfI7YIv4icSIDHVe2u6qKMGIrPZZ8NJtmGc ntlA9nPiUqk3g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Alexey Kardashevskiy , Paolo Bonzini , Sasha Levin , kvm@vger.kernel.org Subject: [PATCH MANUALSEL 5.17 4/4] KVM: Don't null dereference ops->destroy Date: Mon, 13 Jun 2022 22:11:30 -0400 Message-Id: <20220614021130.1101425-4-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220614021130.1101425-1-sashal@kernel.org> References: <20220614021130.1101425-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexey Kardashevskiy [ Upstream commit e8bc2427018826e02add7b0ed0fc625a60390ae5 ] A KVM device cleanup happens in either of two callbacks: 1) destroy() which is called when the VM is being destroyed; 2) release() which is called when a device fd is closed. Most KVM devices use 1) but Book3s's interrupt controller KVM devices (XICS, XIVE, XIVE-native) use 2) as they need to close and reopen during the machine execution. The error handling in kvm_ioctl_create_device() assumes destroy() is always defined which leads to NULL dereference as discovered by Syzkaller. This adds a checks for destroy!=NULL and adds a missing release(). This is not changing kvm_destroy_devices() as devices with defined release() should have been removed from the KVM devices list by then. Suggested-by: Paolo Bonzini Signed-off-by: Alexey Kardashevskiy Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- virt/kvm/kvm_main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 717ee1b2e058..36ccd20c7fc5 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4277,8 +4277,11 @@ static int kvm_ioctl_create_device(struct kvm *kvm, kvm_put_kvm_no_destroy(kvm); mutex_lock(&kvm->lock); list_del(&dev->vm_node); + if (ops->release) + ops->release(dev); mutex_unlock(&kvm->lock); - ops->destroy(dev); + if (ops->destroy) + ops->destroy(dev); return ret; } -- 2.35.1