From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-vk1-f169.google.com (mail-vk1-f169.google.com [209.85.221.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 656E019D092 for ; Mon, 20 Apr 2026 23:16:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776727012; cv=none; b=fn9ieBCyB6KnpHCpE4byPThIUj1RVctZOp0FVzw/39PST5zZDz5vQhkx7Xul6S744LFut8M2IMzJMIrKCngUv+cRYedaGqs0jXIV1mAWjVmlPTizN/K+VHpsW7cj1H7M3hLSzKrU6bvbTkN+Kvq54v9LhthXjUs4qrEW7Vc+bZE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776727012; c=relaxed/simple; bh=nuoDx2RkG40SNYIwOlZCPsgLMoYLWsZ3vCPZcV2ukzQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fGywZrmciOIJerPVSjb/zBTJlhJinPmUxZEEFnXOGwBOV2Skn3CQlMdTAhGSoVEuXZcDvWtchcEE9Zcu9UPo5tDmuNIMiIGOYYp0Meh8R2nPVitFmsmMilQBXyhr3AqhYAlLuRhF0C6swDgFZy2XE9FSC0h06P8CUy8vQivII1s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F4Bl1Xhu; arc=none smtp.client-ip=209.85.221.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F4Bl1Xhu" Received: by mail-vk1-f169.google.com with SMTP id 71dfb90a1353d-56d9ed609d2so1102168e0c.1 for ; Mon, 20 Apr 2026 16:16:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776727010; x=1777331810; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NCLQ/Lu1vC09JUaQSedZPeC4FWOIebO+etPRCyfSe24=; b=F4Bl1XhunG02LSFzfq37kC+hYk7iGm4LpUNno52i9h+yffFkqBe0iDETQWcM6lZnIt vz1VhacVhoNRTEjBPIfw1k+0JyroZ7Mtk9+EE1+ZSIwV8XCvw3yIpLzcxC1pX1Dd3YMt cFhznDfI/fGpGCHWgb3CHOLxE64ZrftPEDJBythJfghNO02x3EskjTf9XOPl03WV14c0 Uk4om7d3z/B45n4V4QXA2P19NQ2DulPB8yGNoL2udu/LAIqAlKbdo/7mJEaVrzRt4J6W XXY8xwsi955+/tvee2tFvic8Ka6UpFCsUgSvHDpK1nFElY/eh88cNDPeZBzd2z+Gzp9P N9NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776727010; x=1777331810; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=NCLQ/Lu1vC09JUaQSedZPeC4FWOIebO+etPRCyfSe24=; b=hDy+1CrCPTnvQwERW+r3OmbpWXKOKF+CT+CYgjMc/qL/6+mrOv2zHu3jfkVweuAt4m 6jCJrhiPAh1SRNUXv5sH39uVoevitRQCBMH6DbgZMItAqVuFzqWgEdCOy+k2Z/uEjqtd IaIKxf4Cw8jdtAkexxZwUiZj0UjR0yqUm/LIRHJPbvO1AvRMMptTu0r0w95dit7cv3R2 FeFaHytsPoFOGhKRZxHe0ijjxGsYIFBNiOwaCmsqvlfChHGm0qBWt7SosGmsVOZvdyab 4AOGpheqnM+fjeWJw9HGedMhMsG0PiKO2lBGOI6vR51utTGkHe0gg37A05ykH00Td7UN XGiQ== X-Forwarded-Encrypted: i=1; AFNElJ8EJYuStnXbjZFf1NNC2QN8MX1iGT3PKfMnqfLfBkKdUwxISQX70ypIXXrXjXYeCIKWgjRe8DOZT+YUs60=@vger.kernel.org X-Gm-Message-State: AOJu0YxQg7F3AeU40f2AjFeaUAHz10SAbtc1IAbuLgC7H/FxrQh3AGEC E/eJhlCXhMquIj7Vh3irH41zRAKH3uUWn69CGWuQELfhxPOr6E/U2lOL X-Gm-Gg: AeBDievSwprcFq1BBVmsIzKe2EZ5c9LEA6FQQCfFBiJKe0bs9Lkha58HwQxlv3Bc7M7 zKXHfsOv2gWFTYw51VgoC/oOo5TwDD6avvuH7FIgtejY9ElKTtwOEVvDApSeuTwgnMFRjqfz7Du TryihE6ys9l2hIzjIS2vVA7t0rmnIvESGKtDvnnJIaGbsImGFS/+yjKTlKIAyKJ2hEpSXbSOaNj yKObPaC9MMDaLQQ0fnVLmEszY0qeWGxAvYzFvueB/vKhMvlZwP4oFqUyLJPM8Md6AdfiNZ1NG1K botL7sVoEZHDdxAPukEiEDToK6kfjKuGhnGc3RVxVo1oSSKU2JOBK6aQL0odPgPfpwGh/hB4Fes 1QFZ8GqNU9FQDBtdPWiGV0hPCtR1ghXBzZtAZDezyyYCHCDraU8Lz3WJ6TJaEoJt8zWK5lY4GFH wiP1UM8W0LP215w6aFUMHDewqmh0DeP1Mn37JA/FiiLHfGr3Zf792q X-Received: by 2002:a05:6122:218e:b0:56c:d34e:bb1a with SMTP id 71dfb90a1353d-56fa5828722mr7648855e0c.4.1776727010282; Mon, 20 Apr 2026 16:16:50 -0700 (PDT) Received: from syssplab.cs.fiu.edu (nat1.cs.fiu.edu. [131.94.134.89]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-56fa93253aesm6835593e0c.14.2026.04.20.16.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2026 16:16:49 -0700 (PDT) From: Chao Shi To: Keith Busch , Jens Axboe , linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Daniel Wagner , Maurizio Lombardi , Chao Shi , Sungwoo Kim , Dave Tian , Weidong Zhu Subject: [PATCH v2 1/1] nvme: core: reject invalid LBA data size from Identify Namespace Date: Mon, 20 Apr 2026 19:11:14 -0400 Message-ID: <20260420231116.748204-2-coshi036@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260420231116.748204-1-coshi036@gmail.com> References: <20260418042835.420281-1-coshi036@gmail.com> <20260420231116.748204-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-Transfer-Encoding: 8bit nvme_update_ns_info_block() trusts id->lbaf[lbaf].ds from the controller and assigns it directly to ns->head->lba_shift without bounds checking. nvme_lba_to_sect() then does: return lba << (head->lba_shift - SECTOR_SHIFT); When called with lba = le64_to_cpu(id->nsze) to compute the device capacity, an attacker-controlled controller can choose ds < 9 or a combination of (ds, nsze) that makes the left shift overflow sector_t. The former is a C undefined behaviour that UBSAN reports as a BUG; the latter silently yields a bogus capacity that the block layer then trusts for bounds checking. Validate ds against SECTOR_SHIFT and use check_shl_overflow() to compute capacity so that any (ds, nsze) combination that would overflow sector_t is rejected. The namespace is skipped with -ENODEV instead of crashing the kernel. This is reachable by a malicious NVMe device, a buggy firmware, or an attacker-controlled NVMe-oF target. Stack trace (UBSAN, ds < 9 variant): RIP: nvme_lba_to_sect drivers/nvme/host/nvme.h:699 [inline] RIP: nvme_update_ns_info_block.cold+0x5/0x7 Call Trace: nvme_update_ns_info+0x175/0xd90 drivers/nvme/host/core.c:2467 nvme_validate_ns drivers/nvme/host/core.c:4299 [inline] nvme_scan_ns drivers/nvme/host/core.c:4350 nvme_scan_ns_async+0xa5/0xe0 drivers/nvme/host/core.c:4383 async_run_entry_fn process_one_work worker_thread kthread Found by Syzkaller. Acked-by: Sungwoo Kim Acked-by: Dave Tian Acked-by: Weidong Zhu Signed-off-by: Chao Shi --- drivers/nvme/host/core.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 1e33af94c24..d1711ef59fb 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -2407,9 +2407,28 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns, lim = queue_limits_start_update(ns->disk->queue); memflags = blk_mq_freeze_queue(ns->disk->queue); + if (id->lbaf[lbaf].ds == 0) { + dev_warn_once(ns->ctrl->device, + "LBA format not available, skipping namespace\n"); + ret = -ENODEV; + queue_limits_cancel_update(ns->disk->queue); + blk_mq_unfreeze_queue(ns->disk->queue, memflags); + goto out; + } + if (id->lbaf[lbaf].ds < SECTOR_SHIFT || + check_shl_overflow(le64_to_cpu(id->nsze), + id->lbaf[lbaf].ds - SECTOR_SHIFT, + &capacity)) { + dev_warn_once(ns->ctrl->device, + "invalid LBA data size %u, skipping namespace\n", + id->lbaf[lbaf].ds); + ret = -ENODEV; + queue_limits_cancel_update(ns->disk->queue); + blk_mq_unfreeze_queue(ns->disk->queue, memflags); + goto out; + } ns->head->lba_shift = id->lbaf[lbaf].ds; ns->head->nuse = le64_to_cpu(id->nuse); - capacity = nvme_lba_to_sect(ns->head, le64_to_cpu(id->nsze)); nvme_set_ctrl_limits(ns->ctrl, &lim, false); nvme_configure_metadata(ns->ctrl, ns->head, id, nvm, info); nvme_set_chunk_sectors(ns, id, &lim); -- 2.43.0