From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from verein.lst.de (verein.lst.de [213.95.11.211]) (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 E3B42339719 for ; Wed, 27 May 2026 14:19:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.95.11.211 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779891556; cv=none; b=iPpXlPbj6UU48gieH6OvMPenww1dzbHBoCWwm0uBrMFhNIrs80/FJ8SIDyY2Ff/nZbf0qU6J1vjTXvrjxUGF/ddaGsqKkDoDqgrIFLcTW/i2WyqK13UyrQqxU8bnbu8+5lgXZdVOlpbSb9LSBuWLrX+qYOmqN1/rmoiOSAf8vLg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779891556; c=relaxed/simple; bh=SRhu8Ysjt7ci51Hskale8jhaXJU4ddYEXrfwD5RgyYs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=vCNUeWznaOZQrjZDCmO+iH5U7rpWMTf5/gdATBd5HFazCWJSWLlXovfPmO5SJN/Ny5SwydujSHDOBifP1RqFNY2zGUdrqubg//6W+ipQXNGinyk+Xr1pNR+ab4FotT1ayguoLQIgjoK7Yw1158toYwQEhykYYpd0lfPHxojdCZE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lst.de; spf=pass smtp.mailfrom=lst.de; arc=none smtp.client-ip=213.95.11.211 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lst.de Received: by verein.lst.de (Postfix, from userid 2407) id 9D72D68B05; Wed, 27 May 2026 16:19:09 +0200 (CEST) Date: Wed, 27 May 2026 16:19:09 +0200 From: Christoph Hellwig To: Chao Shi Cc: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, Sungwoo Kim , Dave Tian , Weidong Zhu Subject: Re: [PATCH] nvme: reject completions for requests that are not in flight Message-ID: <20260527141909.GA13578@lst.de> References: <20260522153034.2168862-1-coshi036@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260522153034.2168862-1-coshi036@gmail.com> User-Agent: Mutt/1.5.17 (2007-11-01) On Fri, May 22, 2026 at 11:30:34AM -0400, Chao Shi wrote: > nvme_find_rq() resolves a device-supplied command id to a request with > blk_mq_tag_to_rq(), which returns whatever request last used that tag - > possibly one that is no longer in flight (freed, or never dispatched and > thus with a NULL rq->mq_hctx). Commit e7006de6c238 ("nvme: code > command_id with a genctr for use-after-free validation") guards against > this, but its generation counter is only 4 bits wide and can be matched > by a malfunctioning or malicious device replaying command ids. The > driver then completes a request that is not outstanding, dereferencing a > NULL rq->mq_hctx or double-completing a command: I don't think an intentionally malicious device is part of the threat model here. This was added to protect against buggy devices. > + /* > + * blk_mq_tag_to_rq() returns whatever request last used this tag, which > + * may no longer be in flight if the device reports a bogus command id. > + * Completing it would deref a NULL rq->mq_hctx or double-complete a > + * command; the 4-bit genctr below only narrows the window. > + */ > + if (unlikely(blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)) { > + dev_err(nvme_req(rq)->ctrl->device, > + "completion for request %#x not in flight\n", tag); > + return NULL; > + } Although this check looks cheap enough that it should not hurt to add it. So I think this should be ok, but maybe respin with your planned commit message update.