From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9D1E1C03 for ; Mon, 20 Mar 2023 14:57:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D41EC4339B; Mon, 20 Mar 2023 14:57:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1679324273; bh=33MTQM26p6quW6Ms0/EsPafZcx84hHLDhaysCRbTYCY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1uKQOdYdMfFWMgJ18zMwJlzlUTXPwnRjQaOkmiNtsLxgtSAvLRzxNreAQISoBNIkl 1ElHstuLilRgTKRfTFNTkhuGzZC196pkDuJ7YFyniCVA1L4jVvezf9ILjRCdspOJmv aBNFDpNwPgzyVAn/WdAb/E1ZcOokc5EYhHqy11Yk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Damien Le Moal , Chaitanya Kulkarni , Christoph Hellwig , Sasha Levin Subject: [PATCH 4.14 10/30] nvmet: avoid potential UAF in nvmet_req_complete() Date: Mon, 20 Mar 2023 15:54:34 +0100 Message-Id: <20230320145420.622482218@linuxfoundation.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230320145420.204894191@linuxfoundation.org> References: <20230320145420.204894191@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Damien Le Moal [ Upstream commit 6173a77b7e9d3e202bdb9897b23f2a8afe7bf286 ] An nvme target ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in a use after free of the request pointer when percpu_ref_put() is called in nvmet_req_complete(). Avoid such problem by using a local variable to save the sq pointer before calling __nvmet_req_complete(), thus avoiding dereferencing the req pointer after that function call. Fixes: a07b4970f464 ("nvmet: add a generic NVMe target") Signed-off-by: Damien Le Moal Reviewed-by: Chaitanya Kulkarni Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- drivers/nvme/target/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c index e02423d7b3b92..3d2dedd118a71 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -418,8 +418,10 @@ static void __nvmet_req_complete(struct nvmet_req *req, u16 status) void nvmet_req_complete(struct nvmet_req *req, u16 status) { + struct nvmet_sq *sq = req->sq; + __nvmet_req_complete(req, status); - percpu_ref_put(&req->sq->ref); + percpu_ref_put(&sq->ref); } EXPORT_SYMBOL_GPL(nvmet_req_complete); -- 2.39.2