Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1548363pxb; Thu, 16 Sep 2021 09:41:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzP41RhP0+56TnJmkMs8DEbEYxZRaNr2dU9PoeBoFI1vV0jeoA7dFSPRitjt55hZjgmtjif X-Received: by 2002:a17:906:6c84:: with SMTP id s4mr7231351ejr.121.1631810471085; Thu, 16 Sep 2021 09:41:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631810471; cv=none; d=google.com; s=arc-20160816; b=ds1HGUHOjqzofbmg/nFTmbM2vmWTBHt3zy/9U0H5KF7JGLKZOu8BLKTKLnwy7NVhEk uJap6URwTzsFNGWCRWFOqd6Tl2ExAYN8/1xtgEVKVA+DXnBTrftItq8/a06SpBJMK2LL AJpWrLFPuP/1hJuZiI3rNTw6HzX5HNuzIMq8YxN4Q/7oSiQSWh0ro28ykR3yppp4EP2W Dpn3MZfvdGHzJsCfuM4GPw4U4iMpAchdBnc6T79mEsuwshMaf29AB0rHQ+rD6RlpKw30 hEE+GOFZDjk7Jg5Kn31OmpHteTtC15IIcuwGlXXnmUdsWlV4xJ4umb2m/YNpzi2o/Llt zcew== 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=3NPatZKDHas/SiqKI6ITFcc+wHAKzJZ9S/wYhQe1BK8=; b=gddjkTp9pZzzT5vH+ZaCpyCuRVUWEl2Qrzy16MJaqafH+2MNelii/jEc3DQL3JHqVQ 4Pr29GSFS1thnLeUImsx/tlZgLywuhYr6yl+ps5BDiE0+JdEogMlhDP77EH2xt8IHn8X kzw0NWvaraPjEb4fPa9KjJrzBO5uZXowAF0PL9QrFyQGex+aTl1iV4GoqIuV5zSp5KL2 dsEo2xHipqdYgvrei6E5gXMZN19gSSJbPkR7dZcyM8qKfwSl21pQTiMZUoW32ioZtRWy OJeoARWVAJymgy4+hIkbXBbdj2VdRjb4C3/Mz2Ha5180c+l1E94t2PNSDjQaB3leU/zx mW7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=z1E0pghq; 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 m8si4094630ejn.200.2021.09.16.09.40.44; Thu, 16 Sep 2021 09:41:11 -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=z1E0pghq; 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 S244985AbhIPQj7 (ORCPT + 99 others); Thu, 16 Sep 2021 12:39:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:44670 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242768AbhIPQd1 (ORCPT ); Thu, 16 Sep 2021 12:33:27 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A9A8861246; Thu, 16 Sep 2021 16:20:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631809219; bh=0c//bAemLoDvXKWr4N8pr+rWcNRrAJaNgqoPpGOPbAs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z1E0pghq/oV3+L67AEImUHksa7nQ5qHbBw+IDp/NnOrCiAAL3czD4mmgaAJf6c7NP UrNXqlPA1Zsh/6LrXVJPmbBFay26JsPc1LGEgLdMK38n4jtTIKzB6uwgKAsUSrFdwD 55ZQVXfSSJ0jpCh9FaViLLMg3Add/+vkw6V5QkC4= 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 5.13 043/380] VMCI: fix NULL pointer dereference when unmapping queue pair Date: Thu, 16 Sep 2021 17:56:40 +0200 Message-Id: <20210916155805.441101696@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155803.966362085@linuxfoundation.org> References: <20210916155803.966362085@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 @@ -2243,7 +2243,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; @@ -2350,7 +2351,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)