From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BC57606A31A for ; Tue, 1 Dec 2026 11:54:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=hTt9q/Oe; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=iNDgl6QL DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=mcyqYiTBiWIYXUdM/ZDN4s+ItaFMdiq8KknJ2U/zFtU=; b=hTt9q/Oecs+/nb haI+kVMpEnIMieyosqrg75b3fYuqzKdqfDXZqgBaSliW7ETj5a0YgjUn2aG5yEt7GjU5C2zHhcge6 SE6+e+3GTcev/9x0EpcwPpimA7TDPvmp6iGQYvULIrltinpMv3WLcW4ttAV1shj/VbSRmPQDFD51V vJ2JCF1pHxJ8dvdMXUoTTE3oyDhR9/ngScCvrAuocffgFfgct6NdYqn3ELD/QTbsAkeZzpQ6nyAay AVvluoDkGwV3lUGSW9d3j1eX7+uy/EERUsiqe44RWebfulg/PW5V8II/7Ym3Y5dZ4RTa1h5bP9f4x A4Qx0oePB3PRzZprSeeQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYMQq-00000007fH8-1Et7; Tue, 01 Dec 2026 11:54:00 +0000 Received: from out30-124.freemail.mail.aliyun.com ([115.124.30.124]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYMQm-00000007fDg-1DqE for linux-nvme@lists.infradead.org; Tue, 01 Dec 2026 11:53:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796126031; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=mcyqYiTBiWIYXUdM/ZDN4s+ItaFMdiq8KknJ2U/zFtU=; b=iNDgl6QLTTSAhk6TDq9OTtn8exs/er0Fv1ymxnkzOAjSrOUB3hk530covQa18g1EC08uMkZNJZgUxzHMohlWVu6pX4mXdljVYt2dau7WQRATUQ/i6AsyYMpVlMnQ0tIOo+lOxG8pw2zlET8mEDRD0nHEw7QwnOBxn+xyAj9+Yx8= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam011083073210;MF=kanie@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XG-M2AM_1796126030; Received: from localhost(mailfrom:kanie@linux.alibaba.com fp:SMTPD_---0XG-M2AM_1796126030 cluster:ay36) by smtp.aliyun-inc.com; Tue, 01 Dec 2026 19:53:51 +0800 From: Guixin Liu To: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Nilay Shroff , Daniel Wagner , Hannes Reinecke Cc: linux-nvme@lists.infradead.org Subject: [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Date: Tue, 1 Dec 2026 19:53:42 +0800 Message-ID: <20261201115345.2349875-2-kanie@linux.alibaba.com> X-Mailer: git-send-email 2.43.7 In-Reply-To: <20261201115345.2349875-1-kanie@linux.alibaba.com> References: <20261201115345.2349875-1-kanie@linux.alibaba.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_035356_514193_C8D82E58 X-CRM114-Status: GOOD ( 11.64 ) X-Spam-Score: -17.6 (-----------------) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 nvme_strlen() strips the space padding from Identify Controller string fields without checking the remaining length. A device returning an all-space Model Number or Firmware Revision, which the NVMe spec's space padding rules allow, drives the index below zero and reads memory before the start of the buffer; with an all-space serial number in front of the model the walk keeps going into the preceding fields. The negative length then reaches dev_err() as a %.*s precision, which the kernel clamps to zero, so the field also prints empty. Stop the loop at the start of the string, like wwid_show() already does. Fixes: 2f0dad1719cbb ("nvme: add bug report info for global duplicate id") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- Found by code inspection; reproduced in a QEMU/KVM guest (7.3.0, KASAN + UBSAN) with nvmet-tcp loopback. Reproducer: two nvmet subsystems sharing a namespace UUID trigger the duplicate-ID check in nvme_init_ns_head(), which calls nvme_print_device_info(). Set the second subsystem's attr_model to one space and attr_serial to 20 spaces; the space-padded Identify data then carries all-space model and serial fields. A TEST-ONLY printk in the test tree (not part of this patch) printing the nvme_strlen() return value shows -20 unpatched (the walk runs through the 40-byte model and the 20-byte serial into the subnqn NUL) and 0 with this patch. A probe variant reading model[-1] also triggered "UBSAN: array-index-out-of-bounds, index -1 out of range for char [40]". The kernel clamps a negative %.*s precision to 0 (lib/vsprintf.c), so the field prints empty rather than leaking forward memory. Regression: duplicate-ID handling, normal model/serial printing, nvme-tcp connect and IO unchanged. drivers/nvme/host/nvme.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h index e069b6d661201..1a28055ac8bcb 100644 --- a/drivers/nvme/host/nvme.h +++ b/drivers/nvme/host/nvme.h @@ -747,7 +747,7 @@ static inline struct request *nvme_cid_to_rq(struct blk_mq_tags *tags, */ static inline int nvme_strlen(char *s, int len) { - while (s[len - 1] == ' ') + while (len > 0 && s[len - 1] == ' ') len--; return len; } -- 2.43.7