Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2564677lkm; Mon, 20 Sep 2021 18:50:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJximRGq974FcLJR1fySDtditoQmXc9t01gVrDYIR9x8iIvLYQDtdRA0sM19WrFy+Wbu3LWg X-Received: by 2002:a6b:7212:: with SMTP id n18mr20336928ioc.175.1632188923879; Mon, 20 Sep 2021 18:48:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188923; cv=none; d=google.com; s=arc-20160816; b=znKOrUGqJbynwDHKiHZa4+SkcC0be0OpZjPduJ9+w55kRUDtZG3hd76Y/FOHfk4C3y 018AlTsUio2V3Aj4CTzMF8oMmkx2+DTDKrRCkbjrf939N/jXNaSqPfZexou0UDnzuH77 iy4bVDzChJfIJF8tHFDAV/S2uXntqoCKO9hVT6OZPyTwF5qJmypk/bnU+lRKE1SIeazo mt2Jjpr3oHauVIbrLY4C0DK0qivxQcH1VYVpTdAKbro7tYOeZOUrMCEvWDdPfjDINl/5 Dq7MhGqZsoJwAXNDsatZvKgBo/pCIbrZdSzicvyTzj3uMaaxTW74mnsitxtMnBnahd3A f/qQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LEYcnRl6FZRy+yAsLqsnrIpNzCcLqHkUyHTPOGsduTs=; b=fneXM8nW9NnOtSHR/fT50pnDppg6eh7NtHBPv7IilRNCAwY75i4SqizikQnqEyczeP EmaEI+iOqaB9uW528QYA9R65HdkwQKX4q2PkFy8NDaaKIfcQk8eRnyFscjjL3QYw00yn mFqck51HPL3F8yUjonseDmtHfnyRD0pqZ04FyWcl24q71UUrWrvvglLO+Lhtaenf3Gop jeABMPdN/+/qCxdaaM4B67UEQb3OpymZe5pXP9D+o+NNdxCyvUXNmzD0ifVhKUDnHVYc 8iPTxYnj0SlG+Q7jzHOEGwSdWahM+4ud21dRLdIlYDVzPF83DpjdtJBadKr6eYy9gZvd pfLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=J1naeVZx; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a7si3401200ilv.9.2021.09.20.18.48.33; Mon, 20 Sep 2021 18:48:43 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=J1naeVZx; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349785AbhITRqZ (ORCPT + 99 others); Mon, 20 Sep 2021 13:46:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:47394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353278AbhITRoS (ORCPT ); Mon, 20 Sep 2021 13:44:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 408856139E; Mon, 20 Sep 2021 17:09:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157776; bh=/GDFhvJmsiJgpH8qOeGu8nCnYFPDKYh/W8GGJaWxl0w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J1naeVZx55bHUBQbOd/yC+9/0ukWSSbdOjXCJbk/l2EUGOPgGeJ0tAfdSqnYo5vju Cs7zoeUfeSaHhFjbAEzyO7AlqUeC9ESkFsQpEuxtE24KMDLnj4lNH9ZJK3+jXAff/5 RGQ1u7rReJO57t9SBBT7BhKZViercgnLHIjezMIw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hulk Robot , Jorgen Hansen , Wang Hai Subject: [PATCH 4.19 147/293] VMCI: fix NULL pointer dereference when unmapping queue pair Date: Mon, 20 Sep 2021 18:41:49 +0200 Message-Id: <20210920163938.309457899@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163933.258815435@linuxfoundation.org> References: <20210920163933.258815435@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wang Hai commit a30dc6cf0dc51419021550152e435736aaef8799 upstream. I got a NULL pointer dereference report when doing fuzz test: Call Trace: qp_release_pages+0xae/0x130 qp_host_unregister_user_memory.isra.25+0x2d/0x80 vmci_qp_broker_unmap+0x191/0x320 ? vmci_host_do_alloc_queuepair.isra.9+0x1c0/0x1c0 vmci_host_unlocked_ioctl+0x59f/0xd50 ? do_vfs_ioctl+0x14b/0xa10 ? tomoyo_file_ioctl+0x28/0x30 ? vmci_host_do_alloc_queuepair.isra.9+0x1c0/0x1c0 __x64_sys_ioctl+0xea/0x120 do_syscall_64+0x34/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xae When a queue pair is created by the following call, it will not register the user memory if the page_store is NULL, and the entry->state will be set to VMCIQPB_CREATED_NO_MEM. vmci_host_unlocked_ioctl vmci_host_do_alloc_queuepair vmci_qp_broker_alloc qp_broker_alloc qp_broker_create // set entry->state = VMCIQPB_CREATED_NO_MEM; When unmapping this queue pair, qp_host_unregister_user_memory() will be called to unregister the non-existent user memory, which will result in a null pointer reference. It will also change VMCIQPB_CREATED_NO_MEM to VMCIQPB_CREATED_MEM, which should not be present in this operation. Only when the qp broker has mem, it can unregister the user memory when unmapping the qp broker. Only when the qp broker has no mem, it can register the user memory when mapping the qp broker. Fixes: 06164d2b72aa ("VMCI: queue pairs implementation.") Cc: stable Reported-by: Hulk Robot Reviewed-by: Jorgen Hansen Signed-off-by: Wang Hai Link: https://lore.kernel.org/r/20210818124845.488312-1-wanghai38@huawei.com Signed-off-by: Greg Kroah-Hartman --- drivers/misc/vmw_vmci/vmci_queue_pair.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/drivers/misc/vmw_vmci/vmci_queue_pair.c +++ b/drivers/misc/vmw_vmci/vmci_queue_pair.c @@ -2249,7 +2249,8 @@ int vmci_qp_broker_map(struct vmci_handl result = VMCI_SUCCESS; - if (context_id != VMCI_HOST_CONTEXT_ID) { + if (context_id != VMCI_HOST_CONTEXT_ID && + !QPBROKERSTATE_HAS_MEM(entry)) { struct vmci_qp_page_store page_store; page_store.pages = guest_mem; @@ -2356,7 +2357,8 @@ int vmci_qp_broker_unmap(struct vmci_han goto out; } - if (context_id != VMCI_HOST_CONTEXT_ID) { + if (context_id != VMCI_HOST_CONTEXT_ID && + QPBROKERSTATE_HAS_MEM(entry)) { qp_acquire_queue_mutex(entry->produce_q); result = qp_save_headers(entry); if (result < VMCI_SUCCESS)