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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A0BBC433F5 for ; Mon, 15 Nov 2021 17:45:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5117A63295 for ; Mon, 15 Nov 2021 17:45:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238864AbhKORs0 (ORCPT ); Mon, 15 Nov 2021 12:48:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:50588 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236930AbhKORTw (ORCPT ); Mon, 15 Nov 2021 12:19:52 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3BE3E63268; Mon, 15 Nov 2021 17:14:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1636996495; bh=7LodHMx0j3am7O18QWQ3deYaJwnUpwNYAJtaxw9CR+g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xcje/0uE1/5pwAA7a42qeAR0EnGDl+jik33CK9fTIeqs/OcKtxdT39FfswZbgH47n Yp+zHyJyGwg74zn9O9Sldha6crRr57Gv+E79TVTLK6eHv1hwkf0g27OAbfrlV8rjx1 CaZVNtPhbbrodEwG9VgV6cNU6ccTuUKCz+xaFlhs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Israel Rukshin , Max Gurtovoy , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.4 160/355] nvmet: fix use-after-free when a port is removed Date: Mon, 15 Nov 2021 18:01:24 +0100 Message-Id: <20211115165318.958980422@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211115165313.549179499@linuxfoundation.org> References: <20211115165313.549179499@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: Israel Rukshin [ Upstream commit e3e19dcc4c416d65f99f13d55be2b787f8d0050e ] When a port is removed through configfs, any connected controllers are starting teardown flow asynchronously and can still send commands. This causes a use-after-free bug for any command that dereferences req->port (like in nvmet_parse_io_cmd). To fix this, wait for all the teardown scheduled works to complete (like release_work at rdma/tcp drivers). This ensures there are no active controllers when the port is eventually removed. Signed-off-by: Israel Rukshin Reviewed-by: Max Gurtovoy Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- drivers/nvme/target/configfs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c index 98613a45bd3b4..baf8a3e4ed12a 100644 --- a/drivers/nvme/target/configfs.c +++ b/drivers/nvme/target/configfs.c @@ -1148,6 +1148,8 @@ static void nvmet_port_release(struct config_item *item) { struct nvmet_port *port = to_nvmet_port(item); + /* Let inflight controllers teardown complete */ + flush_scheduled_work(); list_del(&port->global_entry); kfree(port->ana_state); -- 2.33.0