Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp3753635pxb; Mon, 21 Feb 2022 05:11:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJy2TMOjlx7NmBf8/bSyoUR07O9bsGC9iO9jm9AsEoo+P9kPEbg4wLwGOxymmv7mr3h+pP0K X-Received: by 2002:a17:90b:388d:b0:1b8:cc1b:3360 with SMTP id mu13-20020a17090b388d00b001b8cc1b3360mr25700455pjb.56.1645449101832; Mon, 21 Feb 2022 05:11:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645449101; cv=none; d=google.com; s=arc-20160816; b=Y0yLA4t040PvgFG1p16NOcZA6RwowbB0Wb0cKSRDRLnwwtOsuN5C8NQhLt9ggnZFvo vwaMTNPUNrj86HJU0W9HWGgpM55ha8iwd9ymy3ZXV2hzXq/BD4ivL3CaKgv9CC2OvNS5 XDZTUN0MIqsP9/QDSAiWb4zNuLn+BH+aXXvzxznltiRHmXU+jwlmNaNQMR/KK5hvGQKm GoV7Kk5ottsFPHs1Dhwj6faos7OT0ehLg8qeUiH9NgepCYNVdBW6VPkRFXgVjzHuXJLn 9Fr9+BJsRVlbaVpoPmYlrphHBgQ0Vwetpx48spupHuGUPydFtEAz/zWtY5DnSNeshHY5 L/vA== 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=L0ZFQ0tfKDKJAEQTirCtGeBk0KMWltxa/Xuq9V2JO5k=; b=MoeZWeH5yF22iK6d1pOyrAaajyHJvlGEm/6bA2Yka7H5ffOhLPxujKEd1FVGHTnE0c GW/yWGKfE1xXTgMH9pQ0pXzStH+MUdx7Ww6LryEinon36WBjXPgPT7lm+fu4Hpzbp6Eb crd2XpWsl2RuetVe5JN/GZtar46/4ovN0EqYrBcwhf4f2SaaxGL05X6TcCa5urR7ILXp bN8TJlHPuz7q/7+ACuQ+QNjVoG7b0clobj8P8LWOV5hP1RRHTOj7VIT0Rq202+qVqo+l jItpr1N6PL9w7uGqCvEO2Px5d/wCGvTapsqa69hj+ad5iEJGy5BtmLPHgMn6ka3tMqTp RrWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OgGkGDVS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b6si18423396pgw.490.2022.02.21.05.11.24; Mon, 21 Feb 2022 05:11:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OgGkGDVS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1348210AbiBUJLF (ORCPT + 99 others); Mon, 21 Feb 2022 04:11:05 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:41870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347437AbiBUJGZ (ORCPT ); Mon, 21 Feb 2022 04:06:25 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79B5931206; Mon, 21 Feb 2022 00:59:46 -0800 (PST) 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 C40BCB80E9F; Mon, 21 Feb 2022 08:59:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3D77C340E9; Mon, 21 Feb 2022 08:59:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645433978; bh=GzgRzBVPN10UsBMAviLvVNgKxRB0wrMMqXuJINL/qbI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OgGkGDVSu4FzoL5rTM5gjM8odGoiG8uKMqiu2+vbRZMsGCbgR/sQHNPGozJYoCHtG IbSR74HIY4/xPvi+0dmeAvn/5nfW+g5Gk88NgGhX4g+2WI7DlYJFGDXQa2jYaAGsTw wRIvsYwLTCRunOlKNJFVm7goO9GHm8aOh+FshCaU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sagi Grimberg , Sasha Levin Subject: [PATCH 5.4 18/80] nvme: fix a possible use-after-free in controller reset during load Date: Mon, 21 Feb 2022 09:48:58 +0100 Message-Id: <20220221084916.185626898@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084915.554151737@linuxfoundation.org> References: <20220221084915.554151737@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sagi Grimberg [ Upstream commit 0fa0f99fc84e41057cbdd2efbfe91c6b2f47dd9d ] Unlike .queue_rq, in .submit_async_event drivers may not check the ctrl readiness for AER submission. This may lead to a use-after-free condition that was observed with nvme-tcp. The race condition may happen in the following scenario: 1. driver executes its reset_ctrl_work 2. -> nvme_stop_ctrl - flushes ctrl async_event_work 3. ctrl sends AEN which is received by the host, which in turn schedules AEN handling 4. teardown admin queue (which releases the queue socket) 5. AEN processed, submits another AER, calling the driver to submit 6. driver attempts to send the cmd ==> use-after-free In order to fix that, add ctrl state check to validate the ctrl is actually able to accept the AER submission. This addresses the above race in controller resets because the driver during teardown should: 1. change ctrl state to RESETTING 2. flush async_event_work (as well as other async work elements) So after 1,2, any other AER command will find the ctrl state to be RESETTING and bail out without submitting the AER. Signed-off-by: Sagi Grimberg Signed-off-by: Sasha Levin --- drivers/nvme/host/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index a5b5a2305791d..6a9a42809f972 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -3896,7 +3896,14 @@ static void nvme_async_event_work(struct work_struct *work) container_of(work, struct nvme_ctrl, async_event_work); nvme_aen_uevent(ctrl); - ctrl->ops->submit_async_event(ctrl); + + /* + * The transport drivers must guarantee AER submission here is safe by + * flushing ctrl async_event_work after changing the controller state + * from LIVE and before freeing the admin queue. + */ + if (ctrl->state == NVME_CTRL_LIVE) + ctrl->ops->submit_async_event(ctrl); } static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl) -- 2.34.1