Skip to contentSkip to conversation

nvmet: fix the CSTS state iteration in debugfs

PATCH Applied [email protected] 4 participants 4 messages Nov 14 – Nov 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: fix the CSTS state iteration in debugfs

From: Shao Mingyin <[email protected]>

nvmet_ctrl_state_show() uses the size of a sparse name array as a bit count. Its last index is NVME_CSTS_PP (BIT(5)), so the loop has 33 iterations and evaluates BIT(32). This exceeds the width of an unsigned long on 32-bit systems. With GCC on x86 this can make a normal ready status print "ready|ready".

Iterate over the 32 bits of csts instead. Check the name array bounds before indexing it and print unknown bits as unsigned values. The out-of-bounds lookup requires a reserved status bit; the current target code does not set those bits.

Fixes 107a23185d99 ("objtool, nvmet: Fix out-of-bounds stack access in nvmet_ctrl_state_show()")

---
Tested on 7.3.0-next-20261030 (QEMU/TCG guest, KASAN) with the loop transport. No in-tree path sets a reserved csts bit, so the guest injects one through a test-only debugfs attribute added for this test (not part of the patch).

ORing BIT(31) and then BIT(6) into ctrl->csts and reading the controller state attribute:

  unpatched                            patched
  BUG: unable to handle page fault     csts_bit31='ready|2147483648'
  RIP: nvmet_ctrl_state_show+0x90      csts_bit6='ready|64|2147483648'
  Kernel panic - not syncing
 drivers/nvme/target/debugfs.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/nvme/target/debugfs.c +5 -4
index e85fe1d4c9f8..905a08c63669 100644
@@ -78,18 +78,19 @@ static int nvmet_ctrl_state_show(struct seq_file *m, void *p)
 	bool sep = false;
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(csts_state_names); i++) {
-		int state = BIT(i);
+	for (i = 0; i < BITS_PER_TYPE(u32); i++) {
+		u32 state = BIT(i);
 
 		if (!(ctrl->csts & state))
 			continue;
 		if (sep)
 			seq_puts(m, "|");
 		sep = true;
-		if (csts_state_names[state])
+		if (state < ARRAY_SIZE(csts_state_names) &&
+		    csts_state_names[state])
 			seq_puts(m, csts_state_names[state]);
 		else
-			seq_printf(m, "%d", state);
+			seq_printf(m, "%u", state);
 	}
 	if (sep)
 		seq_printf(m, "\n");
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Medium] nvmet: data race in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Medium] nvmet: data race in nvmet_ctrl_kato_show() [1] --

commit 7730565aae4019e36ef00daab9fa6f3d654b6597
Author: Shao Mingyin <[email protected]>

nvmet: fix the CSTS state iteration in debugfs

This commit fixes nvmet_ctrl_state_show() to iterate over the 32 bits of csts instead of the size of csts_state_names[], checking array bounds and printing unknown bits as unsigned values.

Quoted 5 lines
drivers/nvme/target/debugfs.c +0 -0
index e85fe1d4c9f8d..905a08c63669e 100644

drivers/nvme/target/debugfs.c static int nvmet_ctrl_state_show(struct seq_file *m, void *p) · line 85

Quoted 10 lines
drivers/nvme/target/debugfs.c +2 -2
@@ -78,18 +78,19 @@ static int nvmet_ctrl_state_show(struct seq_file *m, void *p)
 	bool sep = false;
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(csts_state_names); i++) {
-		int state = BIT(i);
+	for (i = 0; i < BITS_PER_TYPE(u32); i++) {
+		u32 state = BIT(i);
 
 		if (!(ctrl->csts & state))
 			continue;

[Severity: Medium]
This problem wasn't introduced by this patch, but can reading ctrl->csts across loop iterations in nvmet_ctrl_state_show() (as well as ctrl->kato in nvmet_ctrl_kato_show()) without synchronization or READ_ONCE() cause a data race [1] when administrative commands or state transitions concurrently update the controller state, resulting in inconsistent or torn debugfs output?

[1] https://sashiko.dev/#/bug/linux-2b037cd9-c44c-44de-95f8-41de5f514a0b

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#3 lore raw
In reply to [email protected]
Quoted 6 lines · On Sun, 15 Nov 2026 01:28:06 +0800 (CST), [email protected] wrote:

nvmet_ctrl_state_show() uses the size of a sparse name array as a bit count. Its last index is NVME_CSTS_PP (BIT(5)), so the loop has 33 iterations and evaluates BIT(32). This exceeds the width of an unsigned long on 32-bit systems. With GCC on x86 this can make a normal ready status print "ready|ready".

Quoted 6 lines

Iterate over the 32 bits of csts instead. Check the name array bounds before indexing it and print unknown bits as unsigned values. The out-of-bounds lookup requires a reserved status bit; the current target code does not set those bits.

[...]

Thanks, applied to nvme-7.4.

[1/1] nvmet: fix the CSTS state iteration in debugfs

      commit: fedfd360c2f6e00d7c566ee5ed52e2ee46c99b8d

--
Keith Busch <[email protected]>